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