version 1.11, 2015/08/29 23:19:52 |
version 1.12, 2015/11/27 15:06:43 |
|
|
free(args); |
free(args); |
} |
} |
|
|
|
/* Add to string. */ |
|
static void printflike(3, 4) |
|
args_print_add(char **buf, size_t *len, const char *fmt, ...) |
|
{ |
|
va_list ap; |
|
char *s; |
|
size_t slen; |
|
|
|
va_start(ap, fmt); |
|
slen = xvasprintf(&s, fmt, ap); |
|
va_end(ap); |
|
|
|
*len += slen; |
|
*buf = xrealloc(*buf, *len); |
|
|
|
strlcat(*buf, s, *len); |
|
free(s); |
|
} |
|
|
/* Print a set of arguments. */ |
/* Print a set of arguments. */ |
size_t |
char * |
args_print(struct args *args, char *buf, size_t len) |
args_print(struct args *args) |
{ |
{ |
size_t off, used; |
size_t len; |
|
char *buf; |
int i; |
int i; |
const char *quotes; |
|
struct args_entry *entry; |
struct args_entry *entry; |
|
|
/* There must be at least one byte at the start. */ |
len = 1; |
if (len == 0) |
buf = xcalloc(1, len); |
return (0); |
|
off = 0; |
|
|
|
/* Process the flags first. */ |
/* Process the flags first. */ |
buf[off++] = '-'; |
|
RB_FOREACH(entry, args_tree, &args->tree) { |
RB_FOREACH(entry, args_tree, &args->tree) { |
if (entry->value != NULL) |
if (entry->value != NULL) |
continue; |
continue; |
|
|
if (off == len - 1) { |
if (*buf == '\0') |
buf[off] = '\0'; |
args_print_add(&buf, &len, "-"); |
return (len); |
args_print_add(&buf, &len, "%c", entry->flag); |
} |
|
buf[off++] = entry->flag; |
|
buf[off] = '\0'; |
|
} |
} |
if (off == 1) |
|
buf[--off] = '\0'; |
|
|
|
/* Then the flags with arguments. */ |
/* Then the flags with arguments. */ |
RB_FOREACH(entry, args_tree, &args->tree) { |
RB_FOREACH(entry, args_tree, &args->tree) { |
if (entry->value == NULL) |
if (entry->value == NULL) |
continue; |
continue; |
|
|
if (off >= len) { |
if (*buf != '\0') |
/* snprintf will have zero terminated. */ |
args_print_add(&buf, &len, " -%c ", entry->flag); |
return (len); |
else |
} |
args_print_add(&buf, &len, "-%c ", entry->flag); |
|
|
if (strchr(entry->value, ' ') != NULL) |
if (strchr(entry->value, ' ') != NULL) |
quotes = "\""; |
args_print_add(&buf, &len, "\"%s\"", entry->value); |
else |
else |
quotes = ""; |
args_print_add(&buf, &len, "%s", entry->value); |
used = xsnprintf(buf + off, len - off, "%s-%c %s%s%s", |
|
off != 0 ? " " : "", entry->flag, quotes, entry->value, |
|
quotes); |
|
if (used > len - off) |
|
used = len - off; |
|
off += used; |
|
} |
} |
|
|
/* And finally the argument vector. */ |
/* And finally the argument vector. */ |
for (i = 0; i < args->argc; i++) { |
for (i = 0; i < args->argc; i++) { |
if (off >= len) { |
if (*buf != '\0') |
/* snprintf will have zero terminated. */ |
args_print_add(&buf, &len, " "); |
return (len); |
|
} |
|
|
|
if (strchr(args->argv[i], ' ') != NULL) |
if (strchr(args->argv[i], ' ') != NULL) |
quotes = "\""; |
args_print_add(&buf, &len, "\"%s\"", args->argv[i]); |
else |
else |
quotes = ""; |
args_print_add(&buf, &len, "%s", args->argv[i]); |
used = xsnprintf(buf + off, len - off, "%s%s%s%s", |
|
off != 0 ? " " : "", quotes, args->argv[i], quotes); |
|
if (used > len - off) |
|
used = len - off; |
|
off += used; |
|
} |
} |
|
|
return (off); |
return (buf); |
} |
} |
|
|
/* Return if an argument is present. */ |
/* Return if an argument is present. */ |