]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/extract-word.c
tree-wide: drop string.h when string-util.h or friends are included
[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) {
90 bool eight_bit = false;
91 char32_t u;
92
93 r = cunescape_one(*p, (size_t) -1, &u, &eight_bit);
94 if (r < 0) {
95 if (flags & EXTRACT_CUNESCAPE_RELAX) {
96 s[sz++] = '\\';
97 s[sz++] = c;
98 } else
99 return -EINVAL;
100 } else {
101 (*p) += r - 1;
102
103 if (eight_bit)
104 s[sz++] = u;
105 else
106 sz += utf8_encode_unichar(s + sz, u);
107 }
108 } else
109 s[sz++] = c;
110
111 backslash = false;
112
113 } else if (quote) { /* inside either single or double quotes */
114 for (;; (*p)++, c = **p) {
115 if (c == 0) {
116 if (flags & EXTRACT_RELAX)
117 goto finish_force_terminate;
118 return -EINVAL;
119 } else if (c == quote) { /* found the end quote */
120 quote = 0;
121 break;
122 } else if (c == '\\' && !(flags & EXTRACT_RETAIN_ESCAPE)) {
123 backslash = true;
124 break;
125 } else {
126 if (!GREEDY_REALLOC(s, allocated, sz+2))
127 return -ENOMEM;
128
129 s[sz++] = c;
130 }
131 }
132
133 } else {
134 for (;; (*p)++, c = **p) {
135 if (c == 0)
136 goto finish_force_terminate;
137 else if (IN_SET(c, '\'', '"') && (flags & EXTRACT_UNQUOTE)) {
138 quote = c;
139 break;
140 } else if (c == '\\' && !(flags & EXTRACT_RETAIN_ESCAPE)) {
141 backslash = true;
142 break;
143 } else if (strchr(separators, c)) {
144 if (flags & EXTRACT_DONT_COALESCE_SEPARATORS) {
145 (*p)++;
146 goto finish_force_next;
147 }
148 /* Skip additional coalesced separators. */
149 for (;; (*p)++, c = **p) {
150 if (c == 0)
151 goto finish_force_terminate;
152 if (!strchr(separators, c))
153 break;
154 }
155 goto finish;
156
157 } else {
158 if (!GREEDY_REALLOC(s, allocated, sz+2))
159 return -ENOMEM;
160
161 s[sz++] = c;
162 }
163 }
164 }
165 }
166
167 finish_force_terminate:
168 *p = NULL;
169 finish:
170 if (!s) {
171 *p = NULL;
172 *ret = NULL;
173 return 0;
174 }
175
176 finish_force_next:
177 s[sz] = 0;
178 *ret = TAKE_PTR(s);
179
180 return 1;
181 }
182
183 int extract_first_word_and_warn(
184 const char **p,
185 char **ret,
186 const char *separators,
187 ExtractFlags flags,
188 const char *unit,
189 const char *filename,
190 unsigned line,
191 const char *rvalue) {
192
193 /* Try to unquote it, if it fails, warn about it and try again
194 * but this time using EXTRACT_CUNESCAPE_RELAX to keep the
195 * backslashes verbatim in invalid escape sequences. */
196
197 const char *save;
198 int r;
199
200 save = *p;
201 r = extract_first_word(p, ret, separators, flags);
202 if (r >= 0)
203 return r;
204
205 if (r == -EINVAL && !(flags & EXTRACT_CUNESCAPE_RELAX)) {
206
207 /* Retry it with EXTRACT_CUNESCAPE_RELAX. */
208 *p = save;
209 r = extract_first_word(p, ret, separators, flags|EXTRACT_CUNESCAPE_RELAX);
210 if (r >= 0) {
211 /* It worked this time, hence it must have been an invalid escape sequence. */
212 log_syntax(unit, LOG_WARNING, filename, line, EINVAL, "Ignoring unknown escape sequences: \"%s\"", *ret);
213 return r;
214 }
215
216 /* If it's still EINVAL; then it must be unbalanced quoting, report this. */
217 if (r == -EINVAL)
218 return log_syntax(unit, LOG_ERR, filename, line, r, "Unbalanced quoting, ignoring: \"%s\"", rvalue);
219 }
220
221 /* Can be any error, report it */
222 return log_syntax(unit, LOG_ERR, filename, line, r, "Unable to decode word \"%s\", ignoring: %m", rvalue);
223 }
224
225 /* We pass ExtractFlags as unsigned int (to avoid undefined behaviour when passing
226 * an object that undergoes default argument promotion as an argument to va_start).
227 * Let's make sure that ExtractFlags fits into an unsigned int. */
228 assert_cc(sizeof(enum ExtractFlags) <= sizeof(unsigned));
229
230 int extract_many_words(const char **p, const char *separators, unsigned flags, ...) {
231 va_list ap;
232 char **l;
233 int n = 0, i, c, r;
234
235 /* Parses a number of words from a string, stripping any
236 * quotes if necessary. */
237
238 assert(p);
239
240 /* Count how many words are expected */
241 va_start(ap, flags);
242 for (;;) {
243 if (!va_arg(ap, char **))
244 break;
245 n++;
246 }
247 va_end(ap);
248
249 if (n <= 0)
250 return 0;
251
252 /* Read all words into a temporary array */
253 l = newa0(char*, n);
254 for (c = 0; c < n; c++) {
255
256 r = extract_first_word(p, &l[c], separators, flags);
257 if (r < 0) {
258 int j;
259
260 for (j = 0; j < c; j++)
261 free(l[j]);
262
263 return r;
264 }
265
266 if (r == 0)
267 break;
268 }
269
270 /* If we managed to parse all words, return them in the passed
271 * in parameters */
272 va_start(ap, flags);
273 for (i = 0; i < n; i++) {
274 char **v;
275
276 v = va_arg(ap, char **);
277 assert(v);
278
279 *v = l[i];
280 }
281 va_end(ap);
282
283 return c;
284 }