]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
84ac7bea | 2 | |
11c3a366 TA |
3 | #include <errno.h> |
4 | #include <stdarg.h> | |
5 | #include <stdbool.h> | |
6 | #include <stddef.h> | |
7 | #include <stdint.h> | |
8 | #include <stdlib.h> | |
11c3a366 TA |
9 | #include <syslog.h> |
10 | ||
b5efdb8a | 11 | #include "alloc-util.h" |
4f5dd394 | 12 | #include "escape.h" |
b11d6a7b | 13 | #include "extract-word.h" |
11c3a366 TA |
14 | #include "log.h" |
15 | #include "macro.h" | |
b11d6a7b | 16 | #include "string-util.h" |
84ac7bea | 17 | #include "utf8.h" |
84ac7bea LP |
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; | |
b85e1c25 | 22 | char c; |
84ac7bea LP |
23 | int r; |
24 | ||
25 | char quote = 0; /* 0 or ' or " */ | |
26 | bool backslash = false; /* whether we've just seen a backslash */ | |
84ac7bea LP |
27 | |
28 | assert(p); | |
29 | assert(ret); | |
30 | ||
84ac7bea LP |
31 | /* Bail early if called after last value or with no input */ |
32 | if (!*p) | |
c58bd76a | 33 | goto finish; |
93de9eb7 | 34 | c = **p; |
84ac7bea | 35 | |
8372da44 FB |
36 | if (!separators) |
37 | separators = WHITESPACE; | |
38 | ||
84ac7bea LP |
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 | ||
b85e1c25 FB |
44 | if (flags & EXTRACT_DONT_COALESCE_SEPARATORS) |
45 | if (!GREEDY_REALLOC(s, allocated, sz+1)) | |
46 | return -ENOMEM; | |
84ac7bea | 47 | |
313cefa1 | 48 | for (;; (*p)++, c = **p) { |
b85e1c25 FB |
49 | if (c == 0) |
50 | goto finish_force_terminate; | |
51 | else if (strchr(separators, c)) { | |
93de9eb7 | 52 | if (flags & EXTRACT_DONT_COALESCE_SEPARATORS) { |
313cefa1 | 53 | (*p)++; |
b85e1c25 | 54 | goto finish_force_next; |
93de9eb7 | 55 | } |
b85e1c25 | 56 | } else { |
84ac7bea LP |
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; | |
b85e1c25 | 62 | break; |
84ac7bea | 63 | } |
b85e1c25 FB |
64 | } |
65 | ||
313cefa1 | 66 | for (;; (*p)++, c = **p) { |
84ac7bea LP |
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 | ||
1e198efc | 89 | if (flags & (EXTRACT_CUNESCAPE|EXTRACT_UNESCAPE_SEPARATORS)) { |
3565e095 | 90 | bool eight_bit = false; |
c932fb71 | 91 | char32_t u; |
84ac7bea | 92 | |
1e198efc ZJS |
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 | ||
3ff13c29 FB |
98 | (*p) += r - 1; |
99 | ||
3565e095 ZJS |
100 | if (eight_bit) |
101 | s[sz++] = u; | |
3ff13c29 | 102 | else |
3565e095 | 103 | sz += utf8_encode_unichar(s + sz, u); |
1e198efc ZJS |
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; | |
84ac7bea LP |
113 | } else |
114 | s[sz++] = c; | |
115 | ||
84ac7bea LP |
116 | backslash = false; |
117 | ||
118 | } else if (quote) { /* inside either single or double quotes */ | |
313cefa1 | 119 | for (;; (*p)++, c = **p) { |
27fc921b FB |
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; | |
c89f52ac | 127 | } else if (c == '\\' && !(flags & EXTRACT_RETAIN_ESCAPE)) { |
27fc921b FB |
128 | backslash = true; |
129 | break; | |
130 | } else { | |
131 | if (!GREEDY_REALLOC(s, allocated, sz+2)) | |
132 | return -ENOMEM; | |
84ac7bea | 133 | |
27fc921b FB |
134 | s[sz++] = c; |
135 | } | |
84ac7bea LP |
136 | } |
137 | ||
84ac7bea | 138 | } else { |
313cefa1 | 139 | for (;; (*p)++, c = **p) { |
27fc921b FB |
140 | if (c == 0) |
141 | goto finish_force_terminate; | |
4ec85141 | 142 | else if (IN_SET(c, '\'', '"') && (flags & EXTRACT_UNQUOTE)) { |
27fc921b FB |
143 | quote = c; |
144 | break; | |
c89f52ac | 145 | } else if (c == '\\' && !(flags & EXTRACT_RETAIN_ESCAPE)) { |
27fc921b FB |
146 | backslash = true; |
147 | break; | |
148 | } else if (strchr(separators, c)) { | |
149 | if (flags & EXTRACT_DONT_COALESCE_SEPARATORS) { | |
313cefa1 | 150 | (*p)++; |
27fc921b FB |
151 | goto finish_force_next; |
152 | } | |
0247447e | 153 | /* Skip additional coalesced separators. */ |
313cefa1 | 154 | for (;; (*p)++, c = **p) { |
0247447e FB |
155 | if (c == 0) |
156 | goto finish_force_terminate; | |
157 | if (!strchr(separators, c)) | |
158 | break; | |
159 | } | |
160 | goto finish; | |
161 | ||
27fc921b FB |
162 | } else { |
163 | if (!GREEDY_REALLOC(s, allocated, sz+2)) | |
164 | return -ENOMEM; | |
84ac7bea | 165 | |
27fc921b FB |
166 | s[sz++] = c; |
167 | } | |
84ac7bea LP |
168 | } |
169 | } | |
84ac7bea LP |
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; | |
ae2a15bc | 183 | *ret = TAKE_PTR(s); |
84ac7bea LP |
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 | ||
dea7b6b0 LP |
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 | ||
84ac7bea LP |
202 | const char *save; |
203 | int r; | |
204 | ||
205 | save = *p; | |
206 | r = extract_first_word(p, ret, separators, flags); | |
dea7b6b0 LP |
207 | if (r >= 0) |
208 | return r; | |
209 | ||
210 | if (r == -EINVAL && !(flags & EXTRACT_CUNESCAPE_RELAX)) { | |
84ac7bea LP |
211 | |
212 | /* Retry it with EXTRACT_CUNESCAPE_RELAX. */ | |
213 | *p = save; | |
214 | r = extract_first_word(p, ret, separators, flags|EXTRACT_CUNESCAPE_RELAX); | |
dea7b6b0 | 215 | if (r >= 0) { |
330785f5 ZJS |
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); | |
dea7b6b0 LP |
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); | |
84ac7bea LP |
224 | } |
225 | ||
dea7b6b0 LP |
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); | |
84ac7bea LP |
228 | } |
229 | ||
e4d85dbb ZJS |
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, ...) { | |
84ac7bea LP |
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 | } |