version 1.21, 2019/04/28 20:05:50 |
version 1.22, 2019/05/23 14:03:44 |
|
|
args_print_add_value(char **buf, size_t *len, struct args_entry *entry, |
args_print_add_value(char **buf, size_t *len, struct args_entry *entry, |
struct args_value *value) |
struct args_value *value) |
{ |
{ |
static const char quoted[] = " #\"';$"; |
char *escaped; |
char *escaped; |
|
int flags; |
|
|
|
if (**buf != '\0') |
if (**buf != '\0') |
args_print_add(buf, len, " -%c ", entry->flag); |
args_print_add(buf, len, " -%c ", entry->flag); |
else |
else |
args_print_add(buf, len, "-%c ", entry->flag); |
args_print_add(buf, len, "-%c ", entry->flag); |
|
|
flags = VIS_OCTAL|VIS_TAB|VIS_NL; |
escaped = args_escape(value->value); |
if (value->value[strcspn(value->value, quoted)] != '\0') |
args_print_add(buf, len, "%s", escaped); |
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); |
|
free(escaped); |
free(escaped); |
} |
} |
|
|
|
|
static void |
static void |
args_print_add_argument(char **buf, size_t *len, const char *argument) |
args_print_add_argument(char **buf, size_t *len, const char *argument) |
{ |
{ |
static const char quoted[] = " #\"';$"; |
char *escaped; |
char *escaped; |
|
int flags; |
|
|
|
if (**buf != '\0') |
if (**buf != '\0') |
args_print_add(buf, len, " "); |
args_print_add(buf, len, " "); |
|
|
flags = VIS_OCTAL|VIS_TAB|VIS_NL; |
escaped = args_escape(argument); |
if (argument[strcspn(argument, quoted)] != '\0') |
args_print_add(buf, len, "%s", escaped); |
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); |
|
free(escaped); |
free(escaped); |
} |
} |
|
|
|
|
args_print_add_argument(&buf, &len, args->argv[i]); |
args_print_add_argument(&buf, &len, args->argv[i]); |
|
|
return (buf); |
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. */ |
/* Return if an argument is present. */ |