summaryrefslogtreecommitdiff
path: root/src/basic
diff options
context:
space:
mode:
authorFilipe Brandenburger <filbranden@google.com>2015-06-08 21:31:43 -0700
committerFilipe Brandenburger <filbranden@google.com>2015-06-17 11:12:12 -0700
commitb59292b296ad71a20a40d7c347b6ca71df48892d (patch)
tree64e88630263d09fa31c3c5db9eb00e2e277e513a /src/basic
parentd6293c070e6e4b83d8e7ec56e465b0b215d55d98 (diff)
util: Introduce unquote_first_word_and_warn
It will try to unquot_first_word, but if it runs into escaping problems it will retry it adding UNQUOTE_CUNESCAPE_RELAX to the flags. If it succeeds on the second try, it will log a warning about it. If it fails both times, it will log an error. Add test cases to confirm it behaves as expected.
Diffstat (limited to 'src/basic')
-rw-r--r--src/basic/util.c30
-rw-r--r--src/basic/util.h1
2 files changed, 31 insertions, 0 deletions
diff --git a/src/basic/util.c b/src/basic/util.c
index 46a48c4d60..727be56f58 100644
--- a/src/basic/util.c
+++ b/src/basic/util.c
@@ -5320,6 +5320,36 @@ finish:
return 1;
}
+int unquote_first_word_and_warn(
+ const char **p,
+ char **ret,
+ UnquoteFlags flags,
+ const char *unit,
+ const char *filename,
+ unsigned line,
+ const char *rvalue) {
+ /* Try to unquote it, if it fails, warn about it and try again but this
+ * time using UNQUOTE_CUNESCAPE_RELAX to keep the backslashes verbatim
+ * in invalid escape sequences. */
+ const char *save;
+ int r;
+
+ save = *p;
+ r = unquote_first_word(p, ret, flags);
+ if (r < 0 && !(flags&UNQUOTE_CUNESCAPE_RELAX)) {
+ /* Retry it with UNQUOTE_CUNESCAPE_RELAX. */
+ *p = save;
+ r = unquote_first_word(p, ret, flags|UNQUOTE_CUNESCAPE_RELAX);
+ if (r < 0)
+ log_syntax(unit, LOG_ERR, filename, line, EINVAL,
+ "Unbalanced quoting in command line, ignoring: \"%s\"", rvalue);
+ else
+ log_syntax(unit, LOG_WARNING, filename, line, EINVAL,
+ "Invalid escape sequences in command line: \"%s\"", rvalue);
+ }
+ return r;
+}
+
int unquote_many_words(const char **p, UnquoteFlags flags, ...) {
va_list ap;
char **l;
diff --git a/src/basic/util.h b/src/basic/util.h
index 748f22f1a2..a1d1dd15c3 100644
--- a/src/basic/util.h
+++ b/src/basic/util.h
@@ -845,6 +845,7 @@ typedef enum UnquoteFlags {
} UnquoteFlags;
int unquote_first_word(const char **p, char **ret, UnquoteFlags flags);
+int unquote_first_word_and_warn(const char **p, char **ret, UnquoteFlags flags, const char *unit, const char *filename, unsigned line, const char *rvalue);
int unquote_many_words(const char **p, UnquoteFlags flags, ...) _sentinel_;
int free_and_strdup(char **p, const char *s);