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 */
41 /* Bail early if called after last value or with no input */
43 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))
58 for (;; (*p
) ++, c
= **p
) {
60 goto finish_force_terminate
;
61 else if (strchr(separators
, c
)) {
62 if (flags
& EXTRACT_DONT_COALESCE_SEPARATORS
) {
64 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))
76 for (;; (*p
) ++, c
= **p
) {
78 if (!GREEDY_REALLOC(s
, allocated
, sz
+7))
82 if ((flags
& EXTRACT_CUNESCAPE_RELAX
) &&
83 (!quote
|| flags
& EXTRACT_RELAX
)) {
84 /* If we find an unquoted trailing backslash and we're in
85 * EXTRACT_CUNESCAPE_RELAX mode, keep it verbatim in the
88 * Unbalanced quotes will only be allowed in EXTRACT_RELAX
89 * mode, EXTRACT_CUNESCAPE_RELAX mode does not allow them.
92 goto finish_force_terminate
;
94 if (flags
& EXTRACT_RELAX
)
95 goto finish_force_terminate
;
99 if (flags
& EXTRACT_CUNESCAPE
) {
102 r
= cunescape_one(*p
, (size_t) -1, &c
, &u
);
104 if (flags
& EXTRACT_CUNESCAPE_RELAX
) {
113 s
[sz
++] = c
; /* normal explicit char */
115 sz
+= utf8_encode_unichar(s
+ sz
, u
); /* unicode chars we'll encode as utf8 */
122 } else if (quote
) { /* inside either single or double quotes */
123 for (;; (*p
) ++, c
= **p
) {
125 if (flags
& EXTRACT_RELAX
)
126 goto finish_force_terminate
;
128 } else if (c
== quote
) { /* found the end quote */
131 } else if (c
== '\\') {
135 if (!GREEDY_REALLOC(s
, allocated
, sz
+2))
143 for (;; (*p
) ++, c
= **p
) {
145 goto finish_force_terminate
;
146 else if ((c
== '\'' || c
== '"') && (flags
& EXTRACT_QUOTES
)) {
149 } else if (c
== '\\') {
152 } else if (strchr(separators
, c
)) {
153 if (flags
& EXTRACT_DONT_COALESCE_SEPARATORS
) {
155 goto finish_force_next
;
157 /* Skip additional coalesced separators. */
158 for (;; (*p
) ++, c
= **p
) {
160 goto finish_force_terminate
;
161 if (!strchr(separators
, c
))
167 if (!GREEDY_REALLOC(s
, allocated
, sz
+2))
176 finish_force_terminate
:
193 int extract_first_word_and_warn(
196 const char *separators
,
199 const char *filename
,
201 const char *rvalue
) {
203 /* Try to unquote it, if it fails, warn about it and try again
204 * but this time using EXTRACT_CUNESCAPE_RELAX to keep the
205 * backslashes verbatim in invalid escape sequences. */
211 r
= extract_first_word(p
, ret
, separators
, flags
);
215 if (r
== -EINVAL
&& !(flags
& EXTRACT_CUNESCAPE_RELAX
)) {
217 /* Retry it with EXTRACT_CUNESCAPE_RELAX. */
219 r
= extract_first_word(p
, ret
, separators
, flags
|EXTRACT_CUNESCAPE_RELAX
);
221 /* It worked this time, hence it must have been an invalid escape sequence we could correct. */
222 log_syntax(unit
, LOG_WARNING
, filename
, line
, EINVAL
, "Invalid escape sequences in line, correcting: \"%s\"", rvalue
);
226 /* If it's still EINVAL; then it must be unbalanced quoting, report this. */
228 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Unbalanced quoting, ignoring: \"%s\"", rvalue
);
231 /* Can be any error, report it */
232 return log_syntax(unit
, LOG_ERR
, filename
, line
, r
, "Unable to decode word \"%s\", ignoring: %m", rvalue
);
235 int extract_many_words(const char **p
, const char *separators
, ExtractFlags flags
, ...) {
240 /* Parses a number of words from a string, stripping any
241 * quotes if necessary. */
245 /* Count how many words are expected */
248 if (!va_arg(ap
, char **))
257 /* Read all words into a temporary array */
259 for (c
= 0; c
< n
; c
++) {
261 r
= extract_first_word(p
, &l
[c
], separators
, flags
);
265 for (j
= 0; j
< c
; j
++)
275 /* If we managed to parse all words, return them in the passed
278 for (i
= 0; i
< n
; i
++) {
281 v
= va_arg(ap
, char **);