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;
35 char quote
= 0; /* 0 or ' or " */
36 bool backslash
= false; /* whether we've just seen a backslash */
37 bool separator
= false; /* whether we've just seen a separator */
42 /* Bail early if called after last value or with no input */
44 goto finish_force_terminate
;
47 separators
= WHITESPACE
;
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. */
54 if (flags
& EXTRACT_DONT_COALESCE_SEPARATORS
)
55 if (!GREEDY_REALLOC(s
, allocated
, sz
+1))
61 goto finish_force_terminate
;
62 else if (strchr(separators
, c
)) {
64 if (flags
& EXTRACT_DONT_COALESCE_SEPARATORS
)
65 goto finish_force_next
;
67 /* We found a non-blank character, so we will always
68 * want to return a string (even if it is empty),
69 * allocate it here. */
70 if (!GREEDY_REALLOC(s
, allocated
, sz
+1))
80 if (!GREEDY_REALLOC(s
, allocated
, sz
+7))
84 if ((flags
& EXTRACT_CUNESCAPE_RELAX
) &&
85 (!quote
|| flags
& EXTRACT_RELAX
)) {
86 /* If we find an unquoted trailing backslash and we're in
87 * EXTRACT_CUNESCAPE_RELAX mode, keep it verbatim in the
90 * Unbalanced quotes will only be allowed in EXTRACT_RELAX
91 * mode, EXTRACT_CUNESCAPE_RELAX mode does not allow them.
94 goto finish_force_terminate
;
96 if (flags
& EXTRACT_RELAX
)
97 goto finish_force_terminate
;
101 if (flags
& EXTRACT_CUNESCAPE
) {
104 r
= cunescape_one(*p
, (size_t) -1, &c
, &u
);
106 if (flags
& EXTRACT_CUNESCAPE_RELAX
) {
115 s
[sz
++] = c
; /* normal explicit char */
117 sz
+= utf8_encode_unichar(s
+ sz
, u
); /* unicode chars we'll encode as utf8 */
124 } else if (quote
) { /* inside either single or double quotes */
126 if (flags
& EXTRACT_RELAX
)
127 goto finish_force_terminate
;
129 } else if (c
== quote
) /* found the end quote */
134 if (!GREEDY_REALLOC(s
, allocated
, sz
+2))
140 } else if (separator
) {
142 goto finish_force_terminate
;
143 if (!strchr(separators
, c
))
148 goto finish_force_terminate
;
149 else if ((c
== '\'' || c
== '"') && (flags
& EXTRACT_QUOTES
))
153 else if (strchr(separators
, c
)) {
154 if (flags
& EXTRACT_DONT_COALESCE_SEPARATORS
) {
156 goto finish_force_next
;
160 if (!GREEDY_REALLOC(s
, allocated
, sz
+2))
170 finish_force_terminate
:
187 int extract_first_word_and_warn(
190 const char *separators
,
193 const char *filename
,
195 const char *rvalue
) {
197 /* Try to unquote it, if it fails, warn about it and try again
198 * but this time using EXTRACT_CUNESCAPE_RELAX to keep the
199 * backslashes verbatim in invalid escape sequences. */
205 r
= extract_first_word(p
, ret
, separators
, flags
);
209 if (r
== -EINVAL
&& !(flags
& EXTRACT_CUNESCAPE_RELAX
)) {
211 /* Retry it with EXTRACT_CUNESCAPE_RELAX. */
213 r
= extract_first_word(p
, ret
, separators
, flags
|EXTRACT_CUNESCAPE_RELAX
);
215 /* It worked this time, hence it must have been an invalid escape sequence we could correct. */
216 log_syntax(unit
, LOG_WARNING
, filename
, line
, EINVAL
, "Invalid escape sequences in line, correcting: \"%s\"", rvalue
);
220 /* If it's still EINVAL; then it must be unbalanced quoting, report this. */
222 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Unbalanced quoting, ignoring: \"%s\"", rvalue
);
225 /* Can be any error, report it */
226 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Unable to decode word \"%s\", ignoring: %m", rvalue
);
229 int extract_many_words(const char **p
, const char *separators
, ExtractFlags flags
, ...) {
234 /* Parses a number of words from a string, stripping any
235 * quotes if necessary. */
239 /* Count how many words are expected */
242 if (!va_arg(ap
, char **))
251 /* Read all words into a temporary array */
253 for (c
= 0; c
< n
; c
++) {
255 r
= extract_first_word(p
, &l
[c
], separators
, flags
);
259 for (j
= 0; j
< c
; j
++)
269 /* If we managed to parse all words, return them in the passed
272 for (i
= 0; i
< n
; i
++) {
275 v
= va_arg(ap
, char **);