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