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"
26 #include "extract-word.h"
28 int extract_first_word(const char **p
, char **ret
, const char *separators
, ExtractFlags flags
) {
29 _cleanup_free_
char *s
= NULL
;
30 size_t allocated
= 0, sz
= 0;
33 char quote
= 0; /* 0 or ' or " */
34 bool backslash
= false; /* whether we've just seen a backslash */
35 bool separator
= false; /* whether we've just seen a separator */
36 bool start
= true; /* false means we're looking at a value */
42 separators
= WHITESPACE
;
44 /* Bail early if called after last value or with no input */
46 goto finish_force_terminate
;
48 /* Parses the first word of a string, and returns it in
49 * *ret. Removes all quotes in the process. When parsing fails
50 * (because of an uneven number of quotes or similar), leaves
51 * the pointer *p at the first invalid character. */
57 if (flags
& EXTRACT_DONT_COALESCE_SEPARATORS
)
58 if (!GREEDY_REALLOC(s
, allocated
, sz
+1))
62 goto finish_force_terminate
;
63 else if (strchr(separators
, c
)) {
65 if (flags
& EXTRACT_DONT_COALESCE_SEPARATORS
)
66 goto finish_force_next
;
70 /* We found a non-blank character, so we will always
71 * want to return a string (even if it is empty),
72 * allocate it here. */
73 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
) {
117 s
[sz
++] = c
; /* normal explicit char */
119 sz
+= utf8_encode_unichar(s
+ sz
, u
); /* unicode chars we'll encode as utf8 */
126 } else if (quote
) { /* inside either single or double quotes */
128 if (flags
& EXTRACT_RELAX
)
129 goto finish_force_terminate
;
131 } else if (c
== quote
) /* found the end quote */
136 if (!GREEDY_REALLOC(s
, allocated
, sz
+2))
142 } else if (separator
) {
144 goto finish_force_terminate
;
145 if (!strchr(separators
, c
))
150 goto finish_force_terminate
;
151 else if ((c
== '\'' || c
== '"') && (flags
& EXTRACT_QUOTES
))
155 else if (strchr(separators
, c
)) {
156 if (flags
& EXTRACT_DONT_COALESCE_SEPARATORS
) {
158 goto finish_force_next
;
162 if (!GREEDY_REALLOC(s
, allocated
, sz
+2))
172 finish_force_terminate
:
189 int extract_first_word_and_warn(
192 const char *separators
,
195 const char *filename
,
197 const char *rvalue
) {
199 /* Try to unquote it, if it fails, warn about it and try again
200 * but this time using EXTRACT_CUNESCAPE_RELAX to keep the
201 * backslashes verbatim in invalid escape sequences. */
207 r
= extract_first_word(p
, ret
, separators
, flags
);
211 if (r
== -EINVAL
&& !(flags
& EXTRACT_CUNESCAPE_RELAX
)) {
213 /* Retry it with EXTRACT_CUNESCAPE_RELAX. */
215 r
= extract_first_word(p
, ret
, separators
, flags
|EXTRACT_CUNESCAPE_RELAX
);
217 /* It worked this time, hence it must have been an invalid escape sequence we could correct. */
218 log_syntax(unit
, LOG_WARNING
, filename
, line
, EINVAL
, "Invalid escape sequences in line, correcting: \"%s\"", rvalue
);
222 /* If it's still EINVAL; then it must be unbalanced quoting, report this. */
224 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Unbalanced quoting, ignoring: \"%s\"", rvalue
);
227 /* Can be any error, report it */
228 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Unable to decode word \"%s\", ignoring: %m", rvalue
);
231 int extract_many_words(const char **p
, const char *separators
, ExtractFlags flags
, ...) {
236 /* Parses a number of words from a string, stripping any
237 * quotes if necessary. */
241 /* Count how many words are expected */
244 if (!va_arg(ap
, char **))
253 /* Read all words into a temporary array */
255 for (c
= 0; c
< n
; c
++) {
257 r
= extract_first_word(p
, &l
[c
], separators
, flags
);
261 for (j
= 0; j
< c
; j
++)
271 /* If we managed to parse all words, return them in the passed
274 for (i
= 0; i
< n
; i
++) {
277 v
= va_arg(ap
, char **);