]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/extract-word.c
basic/extract-word: add EXTRACT_UNESCAPE_SEPARATORS mode
[thirdparty/systemd.git] / src / basic / extract-word.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <stdarg.h>
5 #include <stdbool.h>
6 #include <stddef.h>
7 #include <stdint.h>
8 #include <stdlib.h>
9 #include <syslog.h>
10
11 #include "alloc-util.h"
12 #include "escape.h"
13 #include "extract-word.h"
14 #include "log.h"
15 #include "macro.h"
16 #include "string-util.h"
17 #include "utf8.h"
18
19 int extract_first_word(const char **p, char **ret, const char *separators, ExtractFlags flags) {
20 _cleanup_free_ char *s = NULL;
21 size_t allocated = 0, sz = 0;
22 char c;
23 int r;
24
25 char quote = 0; /* 0 or ' or " */
26 bool backslash = false; /* whether we've just seen a backslash */
27
28 assert(p);
29 assert(ret);
30
31 /* Bail early if called after last value or with no input */
32 if (!*p)
33 goto finish;
34 c = **p;
35
36 if (!separators)
37 separators = WHITESPACE;
38
39 /* Parses the first word of a string, and returns it in
40 * *ret. Removes all quotes in the process. When parsing fails
41 * (because of an uneven number of quotes or similar), leaves
42 * the pointer *p at the first invalid character. */
43
44 if (flags & EXTRACT_DONT_COALESCE_SEPARATORS)
45 if (!GREEDY_REALLOC(s, allocated, sz+1))
46 return -ENOMEM;
47
48 for (;; (*p)++, c = **p) {
49 if (c == 0)
50 goto finish_force_terminate;
51 else if (strchr(separators, c)) {
52 if (flags & EXTRACT_DONT_COALESCE_SEPARATORS) {
53 (*p)++;
54 goto finish_force_next;
55 }
56 } else {
57 /* We found a non-blank character, so we will always
58 * want to return a string (even if it is empty),
59 * allocate it here. */
60 if (!GREEDY_REALLOC(s, allocated, sz+1))
61 return -ENOMEM;
62 break;
63 }
64 }
65
66 for (;; (*p)++, c = **p) {
67 if (backslash) {
68 if (!GREEDY_REALLOC(s, allocated, sz+7))
69 return -ENOMEM;
70
71 if (c == 0) {
72 if ((flags & EXTRACT_CUNESCAPE_RELAX) &&
73 (!quote || flags & EXTRACT_RELAX)) {
74 /* If we find an unquoted trailing backslash and we're in
75 * EXTRACT_CUNESCAPE_RELAX mode, keep it verbatim in the
76 * output.
77 *
78 * Unbalanced quotes will only be allowed in EXTRACT_RELAX
79 * mode, EXTRACT_CUNESCAPE_RELAX mode does not allow them.
80 */
81 s[sz++] = '\\';
82 goto finish_force_terminate;
83 }
84 if (flags & EXTRACT_RELAX)
85 goto finish_force_terminate;
86 return -EINVAL;
87 }
88
89 if (flags & (EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_SEPARATORS)) {
90 bool eight_bit = false;
91 char32_t u;
92
93 if ((flags & EXTRACT_CUNESCAPE) &&
94 (r = cunescape_one(*p, (size_t) -1, &u, &eight_bit, false)) >= 0) {
95 /* A valid escaped sequence */
96 assert(r >= 1);
97
98 (*p) += r - 1;
99
100 if (eight_bit)
101 s[sz++] = u;
102 else
103 sz += utf8_encode_unichar(s + sz, u);
104 } else if ((flags & EXTRACT_UNESCAPE_SEPARATORS) &&
105 strchr(separators, **p))
106 /* An escaped separator char */
107 s[sz++] = c;
108 else if (flags & EXTRACT_CUNESCAPE_RELAX) {
109 s[sz++] = '\\';
110 s[sz++] = c;
111 } else
112 return -EINVAL;
113 } else
114 s[sz++] = c;
115
116 backslash = false;
117
118 } else if (quote) { /* inside either single or double quotes */
119 for (;; (*p)++, c = **p) {
120 if (c == 0) {
121 if (flags & EXTRACT_RELAX)
122 goto finish_force_terminate;
123 return -EINVAL;
124 } else if (c == quote) { /* found the end quote */
125 quote = 0;
126 break;
127 } else if (c == '\\' && !(flags & EXTRACT_RETAIN_ESCAPE)) {
128 backslash = true;
129 break;
130 } else {
131 if (!GREEDY_REALLOC(s, allocated, sz+2))
132 return -ENOMEM;
133
134 s[sz++] = c;
135 }
136 }
137
138 } else {
139 for (;; (*p)++, c = **p) {
140 if (c == 0)
141 goto finish_force_terminate;
142 else if (IN_SET(c, '\'', '"') && (flags & EXTRACT_UNQUOTE)) {
143 quote = c;
144 break;
145 } else if (c == '\\' && !(flags & EXTRACT_RETAIN_ESCAPE)) {
146 backslash = true;
147 break;
148 } else if (strchr(separators, c)) {
149 if (flags & EXTRACT_DONT_COALESCE_SEPARATORS) {
150 (*p)++;
151 goto finish_force_next;
152 }
153 /* Skip additional coalesced separators. */
154 for (;; (*p)++, c = **p) {
155 if (c == 0)
156 goto finish_force_terminate;
157 if (!strchr(separators, c))
158 break;
159 }
160 goto finish;
161
162 } else {
163 if (!GREEDY_REALLOC(s, allocated, sz+2))
164 return -ENOMEM;
165
166 s[sz++] = c;
167 }
168 }
169 }
170 }
171
172 finish_force_terminate:
173 *p = NULL;
174 finish:
175 if (!s) {
176 *p = NULL;
177 *ret = NULL;
178 return 0;
179 }
180
181 finish_force_next:
182 s[sz] = 0;
183 *ret = TAKE_PTR(s);
184
185 return 1;
186 }
187
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 }