summaryrefslogtreecommitdiffstats
path: root/arguments.c
diff options
context:
space:
mode:
authorThomas Adam <thomas@xteddy.org>2019-05-23 17:02:25 +0100
committerThomas Adam <thomas@xteddy.org>2019-05-23 17:02:25 +0100
commit43431e7e8458091c762792db39af3fef65e14412 (patch)
tree9887dff5841a98b0f31e561bd663bbedce9f2c38 /arguments.c
parent389cf63cbc027c995900bbdd6e68f4f5a96e5c08 (diff)
parenteb8b51effcd2dee7b95c811c894bf29387a272c9 (diff)
Merge branch 'obsd-master'
Diffstat (limited to 'arguments.c')
-rw-r--r--arguments.c61
1 files changed, 39 insertions, 22 deletions
diff --git a/arguments.c b/arguments.c
index ebb746a0..bc978c34 100644
--- a/arguments.c
+++ b/arguments.c
@@ -140,23 +140,15 @@ static void
args_print_add_value(char **buf, size_t *len, struct args_entry *entry,
struct args_value *value)
{
- static const char quoted[] = " #\"';$";
- char *escaped;
- int flags;
+ char *escaped;
if (**buf != '\0')
args_print_add(buf, len, " -%c ", entry->flag);
else
args_print_add(buf, len, "-%c ", entry->flag);
- flags = VIS_OCTAL|VIS_TAB|VIS_NL;
- if (value->value[strcspn(value->value, quoted)] != '\0')
- flags |= VIS_DQ;
- utf8_stravis(&escaped, value->value, flags);
- if (flags & VIS_DQ)
- args_print_add(buf, len, "\"%s\"", escaped);
- else
- args_print_add(buf, len, "%s", escaped);
+ escaped = args_escape(value->value);
+ args_print_add(buf, len, "%s", escaped);
free(escaped);
}
@@ -164,21 +156,13 @@ args_print_add_value(char **buf, size_t *len, struct args_entry *entry,
static void
args_print_add_argument(char **buf, size_t *len, const char *argument)
{
- static const char quoted[] = " #\"';$";
- char *escaped;
- int flags;
+ char *escaped;
if (**buf != '\0')
args_print_add(buf, len, " ");
- flags = VIS_OCTAL|VIS_TAB|VIS_NL;
- if (argument[strcspn(argument, quoted)] != '\0')
- flags |= VIS_DQ;
- utf8_stravis(&escaped, argument, flags);
- if (flags & VIS_DQ)
- args_print_add(buf, len, "\"%s\"", escaped);
- else
- args_print_add(buf, len, "%s", escaped);
+ escaped = args_escape(argument);
+ args_print_add(buf, len, "%s", escaped);
free(escaped);
}
@@ -218,6 +202,39 @@ args_print(struct args *args)
return (buf);
}
+/* Escape an argument. */
+char *
+args_escape(const char *s)
+{
+ static const char quoted[] = " #\"';$";
+ char *escaped, *result;
+ int flags;
+
+ if ((strchr(quoted, s[0]) != NULL || s[0] == '~') && s[1] == '\0') {
+ xasprintf(&escaped, "\\%c", s[0]);
+ return (escaped);
+ }
+
+ flags = VIS_OCTAL|VIS_TAB|VIS_NL;
+ if (s[strcspn(s, quoted)] != '\0')
+ flags |= VIS_DQ;
+ utf8_stravis(&escaped, s, flags);
+
+ if (flags & VIS_DQ) {
+ if (*escaped == '~')
+ xasprintf(&result, "\"\\%s\"", escaped);
+ else
+ xasprintf(&result, "\"%s\"", escaped);
+ } else {
+ if (*escaped == '~')
+ xasprintf(&result, "\\%s", escaped);
+ else
+ result = xstrdup(escaped);
+ }
+ free(escaped);
+ return (result);
+}
+
/* Return if an argument is present. */
int
args_has(struct args *args, u_char ch)