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