chiark / gitweb /
tree-wide: remove Lennart's copyright lines
[elogind.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 <string.h>
10 #include <syslog.h>
11
12 #include "alloc-util.h"
13 #include "escape.h"
14 #include "extract-word.h"
15 #include "log.h"
16 #include "macro.h"
17 #include "string-util.h"
18 #include "utf8.h"
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;
23         char c;
24         int r;
25
26         char quote = 0;                 /* 0 or ' or " */
27         bool backslash = false;         /* whether we've just seen a backslash */
28
29         assert(p);
30         assert(ret);
31
32         /* Bail early if called after last value or with no input */
33         if (!*p)
34                 goto finish;
35         c = **p;
36
37         if (!separators)
38                 separators = WHITESPACE;
39
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
45         if (flags & EXTRACT_DONT_COALESCE_SEPARATORS)
46                 if (!GREEDY_REALLOC(s, allocated, sz+1))
47                         return -ENOMEM;
48
49         for (;; (*p)++, c = **p) {
50                 if (c == 0)
51                         goto finish_force_terminate;
52                 else if (strchr(separators, c)) {
53                         if (flags & EXTRACT_DONT_COALESCE_SEPARATORS) {
54                                 (*p)++;
55                                 goto finish_force_next;
56                         }
57                 } else {
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;
63                         break;
64                 }
65         }
66
67         for (;; (*p)++, c = **p) {
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) {
91                                 bool eight_bit = false;
92                                 char32_t u;
93
94                                 r = cunescape_one(*p, (size_t) -1, &u, &eight_bit);
95                                 if (r < 0) {
96                                         if (flags & EXTRACT_CUNESCAPE_RELAX) {
97                                                 s[sz++] = '\\';
98                                                 s[sz++] = c;
99                                         } else
100                                                 return -EINVAL;
101                                 } else {
102                                         (*p) += r - 1;
103
104                                         if (eight_bit)
105                                                 s[sz++] = u;
106                                         else
107                                                 sz += utf8_encode_unichar(s + sz, u);
108                                 }
109                         } else
110                                 s[sz++] = c;
111
112                         backslash = false;
113
114                 } else if (quote) {     /* inside either single or double quotes */
115                         for (;; (*p)++, c = **p) {
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;
123                                 } else if (c == '\\' && !(flags & EXTRACT_RETAIN_ESCAPE)) {
124                                         backslash = true;
125                                         break;
126                                 } else {
127                                         if (!GREEDY_REALLOC(s, allocated, sz+2))
128                                                 return -ENOMEM;
129
130                                         s[sz++] = c;
131                                 }
132                         }
133
134                 } else {
135                         for (;; (*p)++, c = **p) {
136                                 if (c == 0)
137                                         goto finish_force_terminate;
138                                 else if (IN_SET(c, '\'', '"') && (flags & EXTRACT_QUOTES)) {
139                                         quote = c;
140                                         break;
141                                 } else if (c == '\\' && !(flags & EXTRACT_RETAIN_ESCAPE)) {
142                                         backslash = true;
143                                         break;
144                                 } else if (strchr(separators, c)) {
145                                         if (flags & EXTRACT_DONT_COALESCE_SEPARATORS) {
146                                                 (*p)++;
147                                                 goto finish_force_next;
148                                         }
149                                         /* Skip additional coalesced separators. */
150                                         for (;; (*p)++, c = **p) {
151                                                 if (c == 0)
152                                                         goto finish_force_terminate;
153                                                 if (!strchr(separators, c))
154                                                         break;
155                                         }
156                                         goto finish;
157
158                                 } else {
159                                         if (!GREEDY_REALLOC(s, allocated, sz+2))
160                                                 return -ENOMEM;
161
162                                         s[sz++] = c;
163                                 }
164                         }
165                 }
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;
179         *ret = TAKE_PTR(s);
180
181         return 1;
182 }
183
184 #if 0 /// UNNEEDED by elogind
185 int extract_first_word_and_warn(
186                 const char **p,
187                 char **ret,
188                 const char *separators,
189                 ExtractFlags flags,
190                 const char *unit,
191                 const char *filename,
192                 unsigned line,
193                 const char *rvalue) {
194
195         /* Try to unquote it, if it fails, warn about it and try again
196          * but this time using EXTRACT_CUNESCAPE_RELAX to keep the
197          * backslashes verbatim in invalid escape sequences. */
198
199         const char *save;
200         int r;
201
202         save = *p;
203         r = extract_first_word(p, ret, separators, flags);
204         if (r >= 0)
205                 return r;
206
207         if (r == -EINVAL && !(flags & EXTRACT_CUNESCAPE_RELAX)) {
208
209                 /* Retry it with EXTRACT_CUNESCAPE_RELAX. */
210                 *p = save;
211                 r = extract_first_word(p, ret, separators, flags|EXTRACT_CUNESCAPE_RELAX);
212                 if (r >= 0) {
213                         /* It worked this time, hence it must have been an invalid escape sequence. */
214                         log_syntax(unit, LOG_WARNING, filename, line, EINVAL, "Ignoring unknown escape sequences: \"%s\"", *ret);
215                         return r;
216                 }
217
218                 /* If it's still EINVAL; then it must be unbalanced quoting, report this. */
219                 if (r == -EINVAL)
220                         return log_syntax(unit, LOG_ERR, filename, line, r, "Unbalanced quoting, ignoring: \"%s\"", rvalue);
221         }
222
223         /* Can be any error, report it */
224         return log_syntax(unit, LOG_ERR, filename, line, r, "Unable to decode word \"%s\", ignoring: %m", rvalue);
225 }
226
227 /* We pass ExtractFlags as unsigned int (to avoid undefined behaviour when passing
228  * an object that undergoes default argument promotion as an argument to va_start).
229  * Let's make sure that ExtractFlags fits into an unsigned int. */
230 assert_cc(sizeof(enum ExtractFlags) <= sizeof(unsigned));
231
232 int extract_many_words(const char **p, const char *separators, unsigned flags, ...) {
233         va_list ap;
234         char **l;
235         int n = 0, i, c, r;
236
237         /* Parses a number of words from a string, stripping any
238          * quotes if necessary. */
239
240         assert(p);
241
242         /* Count how many words are expected */
243         va_start(ap, flags);
244         for (;;) {
245                 if (!va_arg(ap, char **))
246                         break;
247                 n++;
248         }
249         va_end(ap);
250
251         if (n <= 0)
252                 return 0;
253
254         /* Read all words into a temporary array */
255         l = newa0(char*, n);
256         for (c = 0; c < n; c++) {
257
258                 r = extract_first_word(p, &l[c], separators, flags);
259                 if (r < 0) {
260                         int j;
261
262                         for (j = 0; j < c; j++)
263                                 free(l[j]);
264
265                         return r;
266                 }
267
268                 if (r == 0)
269                         break;
270         }
271
272         /* If we managed to parse all words, return them in the passed
273          * in parameters */
274         va_start(ap, flags);
275         for (i = 0; i < n; i++) {
276                 char **v;
277
278                 v = va_arg(ap, char **);
279                 assert(v);
280
281                 *v = l[i];
282         }
283         va_end(ap);
284
285         return c;
286 }
287 #endif // 0