1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include "alloc-util.h"
24 #include "extract-word.h"
25 #include "string-util.h"
29 int extract_first_word(const char **p
, char **ret
, const char *separators
, ExtractFlags flags
) {
30 _cleanup_free_
char *s
= NULL
;
31 size_t allocated
= 0, sz
= 0;
34 char quote
= 0; /* 0 or ' or " */
35 bool backslash
= false; /* whether we've just seen a backslash */
36 bool separator
= false; /* whether we've just seen a separator */
37 bool start
= true; /* false means we're looking at a value */
43 separators
= WHITESPACE
;
45 /* Bail early if called after last value or with no input */
47 goto finish_force_terminate
;
49 /* Parses the first word of a string, and returns it in
50 * *ret. Removes all quotes in the process. When parsing fails
51 * (because of an uneven number of quotes or similar), leaves
52 * the pointer *p at the first invalid character. */
58 if (flags
& EXTRACT_DONT_COALESCE_SEPARATORS
)
59 if (!GREEDY_REALLOC(s
, allocated
, sz
+1))
63 goto finish_force_terminate
;
64 else if (strchr(separators
, c
)) {
66 if (flags
& EXTRACT_DONT_COALESCE_SEPARATORS
)
67 goto finish_force_next
;
71 /* We found a non-blank character, so we will always
72 * want to return a string (even if it is empty),
73 * allocate it here. */
74 if (!GREEDY_REALLOC(s
, allocated
, sz
+1))
81 if (!GREEDY_REALLOC(s
, allocated
, sz
+7))
85 if ((flags
& EXTRACT_CUNESCAPE_RELAX
) &&
86 (!quote
|| flags
& EXTRACT_RELAX
)) {
87 /* If we find an unquoted trailing backslash and we're in
88 * EXTRACT_CUNESCAPE_RELAX mode, keep it verbatim in the
91 * Unbalanced quotes will only be allowed in EXTRACT_RELAX
92 * mode, EXTRACT_CUNESCAPE_RELAX mode does not allow them.
95 goto finish_force_terminate
;
97 if (flags
& EXTRACT_RELAX
)
98 goto finish_force_terminate
;
102 if (flags
& EXTRACT_CUNESCAPE
) {
105 r
= cunescape_one(*p
, (size_t) -1, &c
, &u
);
107 if (flags
& EXTRACT_CUNESCAPE_RELAX
) {
118 s
[sz
++] = c
; /* normal explicit char */
120 sz
+= utf8_encode_unichar(s
+ sz
, u
); /* unicode chars we'll encode as utf8 */
127 } else if (quote
) { /* inside either single or double quotes */
129 if (flags
& EXTRACT_RELAX
)
130 goto finish_force_terminate
;
132 } else if (c
== quote
) /* found the end quote */
137 if (!GREEDY_REALLOC(s
, allocated
, sz
+2))
143 } else if (separator
) {
145 goto finish_force_terminate
;
146 if (!strchr(separators
, c
))
151 goto finish_force_terminate
;
152 else if ((c
== '\'' || c
== '"') && (flags
& EXTRACT_QUOTES
))
156 else if (strchr(separators
, c
)) {
157 if (flags
& EXTRACT_DONT_COALESCE_SEPARATORS
) {
159 goto finish_force_next
;
163 if (!GREEDY_REALLOC(s
, allocated
, sz
+2))
173 finish_force_terminate
:
190 int extract_first_word_and_warn(
193 const char *separators
,
196 const char *filename
,
198 const char *rvalue
) {
200 /* Try to unquote it, if it fails, warn about it and try again
201 * but this time using EXTRACT_CUNESCAPE_RELAX to keep the
202 * backslashes verbatim in invalid escape sequences. */
208 r
= extract_first_word(p
, ret
, separators
, flags
);
212 if (r
== -EINVAL
&& !(flags
& EXTRACT_CUNESCAPE_RELAX
)) {
214 /* Retry it with EXTRACT_CUNESCAPE_RELAX. */
216 r
= extract_first_word(p
, ret
, separators
, flags
|EXTRACT_CUNESCAPE_RELAX
);
218 /* It worked this time, hence it must have been an invalid escape sequence we could correct. */
219 log_syntax(unit
, LOG_WARNING
, filename
, line
, EINVAL
, "Invalid escape sequences in line, correcting: \"%s\"", rvalue
);
223 /* If it's still EINVAL; then it must be unbalanced quoting, report this. */
225 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Unbalanced quoting, ignoring: \"%s\"", rvalue
);
228 /* Can be any error, report it */
229 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Unable to decode word \"%s\", ignoring: %m", rvalue
);
232 int extract_many_words(const char **p
, const char *separators
, ExtractFlags flags
, ...) {
237 /* Parses a number of words from a string, stripping any
238 * quotes if necessary. */
242 /* Count how many words are expected */
245 if (!va_arg(ap
, char **))
254 /* Read all words into a temporary array */
256 for (c
= 0; c
< n
; c
++) {
258 r
= extract_first_word(p
, &l
[c
], separators
, flags
);
262 for (j
= 0; j
< c
; j
++)
272 /* If we managed to parse all words, return them in the passed
275 for (i
= 0; i
< n
; i
++) {
278 v
= va_arg(ap
, char **);