chiark / gitweb /
basic/strv: use SWAP_TWO() macro (#3602)
[elogind.git] / src / basic / extract-word.c
1 /***
2   This file is part of systemd.
3
4   Copyright 2010 Lennart Poettering
5
6   systemd is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as published by
8   the Free Software Foundation; either version 2.1 of the License, or
9   (at your option) any later version.
10
11   systemd is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public License
17   along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <stdarg.h>
22 #include <stdbool.h>
23 #include <stddef.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <syslog.h>
28
29 #include "alloc-util.h"
30 #include "escape.h"
31 #include "extract-word.h"
32 #include "log.h"
33 #include "macro.h"
34 #include "string-util.h"
35 #include "utf8.h"
36
37 int extract_first_word(const char **p, char **ret, const char *separators, ExtractFlags flags) {
38         _cleanup_free_ char *s = NULL;
39         size_t allocated = 0, sz = 0;
40         char c;
41         int r;
42
43         char quote = 0;                 /* 0 or ' or " */
44         bool backslash = false;         /* whether we've just seen a backslash */
45
46         assert(p);
47         assert(ret);
48
49         /* Bail early if called after last value or with no input */
50         if (!*p)
51                 goto finish_force_terminate;
52         c = **p;
53
54         if (!separators)
55                 separators = WHITESPACE;
56
57         /* Parses the first word of a string, and returns it in
58          * *ret. Removes all quotes in the process. When parsing fails
59          * (because of an uneven number of quotes or similar), leaves
60          * the pointer *p at the first invalid character. */
61
62         if (flags & EXTRACT_DONT_COALESCE_SEPARATORS)
63                 if (!GREEDY_REALLOC(s, allocated, sz+1))
64                         return -ENOMEM;
65
66         for (;; (*p)++, c = **p) {
67                 if (c == 0)
68                         goto finish_force_terminate;
69                 else if (strchr(separators, c)) {
70                         if (flags & EXTRACT_DONT_COALESCE_SEPARATORS) {
71                                 (*p)++;
72                                 goto finish_force_next;
73                         }
74                 } else {
75                         /* We found a non-blank character, so we will always
76                          * want to return a string (even if it is empty),
77                          * allocate it here. */
78                         if (!GREEDY_REALLOC(s, allocated, sz+1))
79                                 return -ENOMEM;
80                         break;
81                 }
82         }
83
84         for (;; (*p)++, c = **p) {
85                 if (backslash) {
86                         if (!GREEDY_REALLOC(s, allocated, sz+7))
87                                 return -ENOMEM;
88
89                         if (c == 0) {
90                                 if ((flags & EXTRACT_CUNESCAPE_RELAX) &&
91                                     (!quote || flags & EXTRACT_RELAX)) {
92                                         /* If we find an unquoted trailing backslash and we're in
93                                          * EXTRACT_CUNESCAPE_RELAX mode, keep it verbatim in the
94                                          * output.
95                                          *
96                                          * Unbalanced quotes will only be allowed in EXTRACT_RELAX
97                                          * mode, EXTRACT_CUNESCAPE_RELAX mode does not allow them.
98                                          */
99                                         s[sz++] = '\\';
100                                         goto finish_force_terminate;
101                                 }
102                                 if (flags & EXTRACT_RELAX)
103                                         goto finish_force_terminate;
104                                 return -EINVAL;
105                         }
106
107                         if (flags & EXTRACT_CUNESCAPE) {
108                                 bool eight_bit = false;
109                                 char32_t u;
110
111                                 r = cunescape_one(*p, (size_t) -1, &u, &eight_bit);
112                                 if (r < 0) {
113                                         if (flags & EXTRACT_CUNESCAPE_RELAX) {
114                                                 s[sz++] = '\\';
115                                                 s[sz++] = c;
116                                         } else
117                                                 return -EINVAL;
118                                 } else {
119                                         (*p) += r - 1;
120
121                                         if (eight_bit)
122                                                 s[sz++] = u;
123                                         else
124                                                 sz += utf8_encode_unichar(s + sz, u);
125                                 }
126                         } else
127                                 s[sz++] = c;
128
129                         backslash = false;
130
131                 } else if (quote) {     /* inside either single or double quotes */
132                         for (;; (*p)++, c = **p) {
133                                 if (c == 0) {
134                                         if (flags & EXTRACT_RELAX)
135                                                 goto finish_force_terminate;
136                                         return -EINVAL;
137                                 } else if (c == quote) {        /* found the end quote */
138                                         quote = 0;
139                                         break;
140                                 } else if (c == '\\' && !(flags & EXTRACT_RETAIN_ESCAPE)) {
141                                         backslash = true;
142                                         break;
143                                 } else {
144                                         if (!GREEDY_REALLOC(s, allocated, sz+2))
145                                                 return -ENOMEM;
146
147                                         s[sz++] = c;
148                                 }
149                         }
150
151                 } else {
152                         for (;; (*p)++, c = **p) {
153                                 if (c == 0)
154                                         goto finish_force_terminate;
155                                 else if ((c == '\'' || c == '"') && (flags & EXTRACT_QUOTES)) {
156                                         quote = c;
157                                         break;
158                                 } else if (c == '\\' && !(flags & EXTRACT_RETAIN_ESCAPE)) {
159                                         backslash = true;
160                                         break;
161                                 } else if (strchr(separators, c)) {
162                                         if (flags & EXTRACT_DONT_COALESCE_SEPARATORS) {
163                                                 (*p)++;
164                                                 goto finish_force_next;
165                                         }
166                                         /* Skip additional coalesced separators. */
167                                         for (;; (*p)++, c = **p) {
168                                                 if (c == 0)
169                                                         goto finish_force_terminate;
170                                                 if (!strchr(separators, c))
171                                                         break;
172                                         }
173                                         goto finish;
174
175                                 } else {
176                                         if (!GREEDY_REALLOC(s, allocated, sz+2))
177                                                 return -ENOMEM;
178
179                                         s[sz++] = c;
180                                 }
181                         }
182                 }
183         }
184
185 finish_force_terminate:
186         *p = NULL;
187 finish:
188         if (!s) {
189                 *p = NULL;
190                 *ret = NULL;
191                 return 0;
192         }
193
194 finish_force_next:
195         s[sz] = 0;
196         *ret = s;
197         s = NULL;
198
199         return 1;
200 }
201
202 #if 0 /// UNNEEDED by elogind
203 int extract_first_word_and_warn(
204                 const char **p,
205                 char **ret,
206                 const char *separators,
207                 ExtractFlags flags,
208                 const char *unit,
209                 const char *filename,
210                 unsigned line,
211                 const char *rvalue) {
212
213         /* Try to unquote it, if it fails, warn about it and try again
214          * but this time using EXTRACT_CUNESCAPE_RELAX to keep the
215          * backslashes verbatim in invalid escape sequences. */
216
217         const char *save;
218         int r;
219
220         save = *p;
221         r = extract_first_word(p, ret, separators, flags);
222         if (r >= 0)
223                 return r;
224
225         if (r == -EINVAL && !(flags & EXTRACT_CUNESCAPE_RELAX)) {
226
227                 /* Retry it with EXTRACT_CUNESCAPE_RELAX. */
228                 *p = save;
229                 r = extract_first_word(p, ret, separators, flags|EXTRACT_CUNESCAPE_RELAX);
230                 if (r >= 0) {
231                         /* It worked this time, hence it must have been an invalid escape sequence we could correct. */
232                         log_syntax(unit, LOG_WARNING, filename, line, EINVAL, "Invalid escape sequences in line, correcting: \"%s\"", rvalue);
233                         return r;
234                 }
235
236                 /* If it's still EINVAL; then it must be unbalanced quoting, report this. */
237                 if (r == -EINVAL)
238                         return log_syntax(unit, LOG_ERR, filename, line, r, "Unbalanced quoting, ignoring: \"%s\"", rvalue);
239         }
240
241         /* Can be any error, report it */
242         return log_syntax(unit, LOG_ERR, filename, line, r, "Unable to decode word \"%s\", ignoring: %m", rvalue);
243 }
244
245 int extract_many_words(const char **p, const char *separators, ExtractFlags flags, ...) {
246         va_list ap;
247         char **l;
248         int n = 0, i, c, r;
249
250         /* Parses a number of words from a string, stripping any
251          * quotes if necessary. */
252
253         assert(p);
254
255         /* Count how many words are expected */
256         va_start(ap, flags);
257         for (;;) {
258                 if (!va_arg(ap, char **))
259                         break;
260                 n++;
261         }
262         va_end(ap);
263
264         if (n <= 0)
265                 return 0;
266
267         /* Read all words into a temporary array */
268         l = newa0(char*, n);
269         for (c = 0; c < n; c++) {
270
271                 r = extract_first_word(p, &l[c], separators, flags);
272                 if (r < 0) {
273                         int j;
274
275                         for (j = 0; j < c; j++)
276                                 free(l[j]);
277
278                         return r;
279                 }
280
281                 if (r == 0)
282                         break;
283         }
284
285         /* If we managed to parse all words, return them in the passed
286          * in parameters */
287         va_start(ap, flags);
288         for (i = 0; i < n; i++) {
289                 char **v;
290
291                 v = va_arg(ap, char **);
292                 assert(v);
293
294                 *v = l[i];
295         }
296         va_end(ap);
297
298         return c;
299 }
300 #endif // 0