chiark / gitweb /
Prep v234: Apply missing upstream fixes in src/basic (1/6)
[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;
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. */
232                         log_syntax(unit, LOG_WARNING, filename, line, EINVAL, "Ignoring unknown escape sequences: \"%s\"", *ret);
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 /* We pass ExtractFlags as unsigned int (to avoid undefined behaviour when passing
246  * an object that undergoes default argument promotion as an argument to va_start).
247  * Let's make sure that ExtractFlags fits into an unsigned int. */
248 assert_cc(sizeof(enum ExtractFlags) <= sizeof(unsigned));
249
250 int extract_many_words(const char **p, const char *separators, unsigned flags, ...) {
251         va_list ap;
252         char **l;
253         int n = 0, i, c, r;
254
255         /* Parses a number of words from a string, stripping any
256          * quotes if necessary. */
257
258         assert(p);
259
260         /* Count how many words are expected */
261         va_start(ap, flags);
262         for (;;) {
263                 if (!va_arg(ap, char **))
264                         break;
265                 n++;
266         }
267         va_end(ap);
268
269         if (n <= 0)
270                 return 0;
271
272         /* Read all words into a temporary array */
273         l = newa0(char*, n);
274         for (c = 0; c < n; c++) {
275
276                 r = extract_first_word(p, &l[c], separators, flags);
277                 if (r < 0) {
278                         int j;
279
280                         for (j = 0; j < c; j++)
281                                 free(l[j]);
282
283                         return r;
284                 }
285
286                 if (r == 0)
287                         break;
288         }
289
290         /* If we managed to parse all words, return them in the passed
291          * in parameters */
292         va_start(ap, flags);
293         for (i = 0; i < n; i++) {
294                 char **v;
295
296                 v = va_arg(ap, char **);
297                 assert(v);
298
299                 *v = l[i];
300         }
301         va_end(ap);
302
303         return c;
304 }
305 #endif // 0