version 1.37, 2021/08/20 18:59:53 |
version 1.38, 2021/08/20 19:50:16 |
|
|
RB_ENTRY(args_entry) entry; |
RB_ENTRY(args_entry) entry; |
}; |
}; |
|
|
|
struct args { |
|
struct args_tree tree; |
|
int argc; |
|
char **argv; |
|
}; |
|
|
static struct args_entry *args_find(struct args *, u_char); |
static struct args_entry *args_find(struct args *, u_char); |
|
|
static int args_cmp(struct args_entry *, struct args_entry *); |
static int args_cmp(struct args_entry *, struct args_entry *); |
|
|
|
|
/* Parse an argv and argc into a new argument set. */ |
/* Parse an argv and argc into a new argument set. */ |
struct args * |
struct args * |
args_parse(const char *template, int argc, char **argv) |
args_parse(const char *template, int argc, char **argv, int lower, int upper) |
{ |
{ |
struct args *args; |
struct args *args; |
int opt; |
int opt; |
|
|
args->argc = argc; |
args->argc = argc; |
args->argv = cmd_copy_argv(argc, argv); |
args->argv = cmd_copy_argv(argc, argv); |
|
|
|
if ((lower != -1 && argc < lower) || (upper != -1 && argc > upper)) { |
|
args_free(args); |
|
return (NULL); |
|
} |
return (args); |
return (args); |
} |
} |
|
|
|
|
free(args); |
free(args); |
} |
} |
|
|
|
/* Convert arguments to vector. */ |
|
void |
|
args_vector(struct args *args, int *argc, char ***argv) |
|
{ |
|
*argc = args->argc; |
|
*argv = cmd_copy_argv(args->argc, args->argv); |
|
} |
|
|
/* Add to string. */ |
/* Add to string. */ |
static void printflike(3, 4) |
static void printflike(3, 4) |
args_print_add(char **buf, size_t *len, const char *fmt, ...) |
args_print_add(char **buf, size_t *len, const char *fmt, ...) |
|
|
free(s); |
free(s); |
} |
} |
|
|
/* Add value to string. */ |
|
static void |
|
args_print_add_value(char **buf, size_t *len, struct args_entry *entry, |
|
struct args_value *value) |
|
{ |
|
char *escaped; |
|
|
|
if (**buf != '\0') |
|
args_print_add(buf, len, " -%c ", entry->flag); |
|
else |
|
args_print_add(buf, len, "-%c ", entry->flag); |
|
|
|
escaped = args_escape(value->value); |
|
args_print_add(buf, len, "%s", escaped); |
|
free(escaped); |
|
} |
|
|
|
/* Add argument to string. */ |
/* Add argument to string. */ |
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) |
|
|
|
|
/* Then the flags with arguments. */ |
/* Then the flags with arguments. */ |
RB_FOREACH(entry, args_tree, &args->tree) { |
RB_FOREACH(entry, args_tree, &args->tree) { |
TAILQ_FOREACH(value, &entry->values, entry) |
TAILQ_FOREACH(value, &entry->values, entry) { |
args_print_add_value(&buf, &len, entry, value); |
if (*buf != '\0') |
|
args_print_add(&buf, &len, " -%c", entry->flag); |
|
else |
|
args_print_add(&buf, &len, "-%c", entry->flag); |
|
args_print_add_argument(&buf, &len, value->value); |
|
} |
} |
} |
|
|
/* And finally the argument vector. */ |
/* And finally the argument vector. */ |
|
|
if (*entry == NULL) |
if (*entry == NULL) |
return (0); |
return (0); |
return ((*entry)->flag); |
return ((*entry)->flag); |
|
} |
|
|
|
/* Get argument count. */ |
|
u_int |
|
args_count(struct args *args) |
|
{ |
|
return (args->argc); |
|
} |
|
|
|
/* Return argument as string. */ |
|
const char * |
|
args_string(struct args *args, u_int idx) |
|
{ |
|
if (idx >= (u_int)args->argc) |
|
return (NULL); |
|
return (args->argv[idx]); |
} |
} |
|
|
/* Get first value in argument. */ |
/* Get first value in argument. */ |