version 1.42, 2019/04/25 18:18:55 |
version 1.43, 2019/04/26 11:38:51 |
|
|
#define OPTIONS_IS_STYLE(o) \ |
#define OPTIONS_IS_STYLE(o) \ |
((o)->tableentry != NULL && \ |
((o)->tableentry != NULL && \ |
(o)->tableentry->type == OPTIONS_TABLE_STYLE) |
(o)->tableentry->type == OPTIONS_TABLE_STYLE) |
|
#define OPTIONS_IS_COMMAND(o) \ |
|
((o)->tableentry != NULL && \ |
|
(o)->tableentry->type == OPTIONS_TABLE_COMMAND) |
|
|
#define OPTIONS_IS_ARRAY(o) \ |
#define OPTIONS_IS_ARRAY(o) \ |
((o)->tableentry != NULL && \ |
((o)->tableentry != NULL && \ |
|
|
{ |
{ |
if (OPTIONS_IS_STRING(o)) |
if (OPTIONS_IS_STRING(o)) |
free(ov->string); |
free(ov->string); |
|
if (OPTIONS_IS_COMMAND(o) && ov->cmdlist != NULL) |
|
cmd_list_free(ov->cmdlist); |
} |
} |
|
|
static char * |
static char * |
|
|
{ |
{ |
char *s; |
char *s; |
|
|
|
if (OPTIONS_IS_COMMAND(o)) |
|
return (cmd_list_print(ov->cmdlist)); |
if (OPTIONS_IS_STYLE(o)) |
if (OPTIONS_IS_STYLE(o)) |
return (xstrdup(style_tostring(&ov->style))); |
return (xstrdup(style_tostring(&ov->style))); |
if (OPTIONS_IS_NUMBER(o)) { |
if (OPTIONS_IS_NUMBER(o)) { |
|
|
break; |
break; |
case OPTIONS_TABLE_STRING: |
case OPTIONS_TABLE_STRING: |
case OPTIONS_TABLE_STYLE: |
case OPTIONS_TABLE_STYLE: |
|
case OPTIONS_TABLE_COMMAND: |
fatalx("not a number option type"); |
fatalx("not a number option type"); |
} |
} |
return (s); |
return (s); |
|
|
ov = &o->value; |
ov = &o->value; |
|
|
if (oe->flags & OPTIONS_TABLE_IS_ARRAY) { |
if (oe->flags & OPTIONS_TABLE_IS_ARRAY) { |
if (oe->default_arr != NULL) { |
if (oe->default_arr == NULL) { |
for (i = 0; oe->default_arr[i] != NULL; i++) |
options_array_assign(o, oe->default_str, NULL); |
options_array_set(o, i, oe->default_arr[i], 0); |
return (o); |
} else |
} |
options_array_assign(o, oe->default_str); |
for (i = 0; oe->default_arr[i] != NULL; i++) |
|
options_array_set(o, i, oe->default_arr[i], 0, NULL); |
return (o); |
return (o); |
} |
} |
|
|
|
|
|
|
int |
int |
options_array_set(struct options_entry *o, u_int idx, const char *value, |
options_array_set(struct options_entry *o, u_int idx, const char *value, |
int append) |
int append, char **cause) |
{ |
{ |
struct options_array_item *a; |
struct options_array_item *a; |
char *new; |
char *new; |
|
struct cmd_list *cmdlist; |
|
|
if (!OPTIONS_IS_ARRAY(o)) |
if (!OPTIONS_IS_ARRAY(o)) { |
|
*cause = xstrdup("not an array"); |
return (-1); |
return (-1); |
|
} |
|
|
|
if (OPTIONS_IS_COMMAND(o)) { |
|
cmdlist = cmd_string_parse(value, NULL, 0, cause); |
|
if (cmdlist == NULL && *cause != NULL) |
|
return (-1); |
|
} |
|
|
a = options_array_item(o, idx); |
a = options_array_item(o, idx); |
if (value == NULL) { |
if (value == NULL) { |
if (a != NULL) |
if (a != NULL) |
|
|
return (0); |
return (0); |
} |
} |
|
|
if (a == NULL) { |
if (OPTIONS_IS_STRING(o)) { |
a = xcalloc(1, sizeof *a); |
|
a->index = idx; |
|
a->value.string = xstrdup(value); |
|
RB_INSERT(options_array, &o->value.array, a); |
|
} else { |
|
options_value_free(o, &a->value); |
|
if (a != NULL && append) |
if (a != NULL && append) |
xasprintf(&new, "%s%s", a->value.string, value); |
xasprintf(&new, "%s%s", a->value.string, value); |
else |
else |
new = xstrdup(value); |
new = xstrdup(value); |
a->value.string = new; |
|
} |
} |
|
|
|
if (a == NULL) { |
|
a = xcalloc(1, sizeof *a); |
|
a->index = idx; |
|
RB_INSERT(options_array, &o->value.array, a); |
|
} else |
|
options_value_free(o, &a->value); |
|
|
|
if (OPTIONS_IS_STRING(o)) |
|
a->value.string = new; |
|
else if (OPTIONS_IS_COMMAND(o)) |
|
a->value.cmdlist = cmdlist; |
return (0); |
return (0); |
} |
} |
|
|
void |
int |
options_array_assign(struct options_entry *o, const char *s) |
options_array_assign(struct options_entry *o, const char *s, char **cause) |
{ |
{ |
const char *separator; |
const char *separator; |
char *copy, *next, *string; |
char *copy, *next, *string; |
|
|
separator = o->tableentry->separator; |
separator = o->tableentry->separator; |
if (separator == NULL) |
if (separator == NULL) |
separator = " ,"; |
separator = " ,"; |
|
if (*separator == '\0') { |
|
if (*s == '\0') |
|
return (0); |
|
for (i = 0; i < UINT_MAX; i++) { |
|
if (options_array_item(o, i) == NULL) |
|
break; |
|
} |
|
return (options_array_set(o, i, s, 0, cause)); |
|
} |
|
|
|
if (*s == '\0') |
|
return (0); |
copy = string = xstrdup(s); |
copy = string = xstrdup(s); |
while ((next = strsep(&string, separator)) != NULL) { |
while ((next = strsep(&string, separator)) != NULL) { |
if (*next == '\0') |
if (*next == '\0') |
|
|
} |
} |
if (i == UINT_MAX) |
if (i == UINT_MAX) |
break; |
break; |
options_array_set(o, i, next, 0); |
if (options_array_set(o, i, next, 0, cause) != 0) { |
|
free(copy); |
|
return (-1); |
|
} |
} |
} |
free(copy); |
free(copy); |
|
return (0); |
} |
} |
|
|
struct options_array_item * |
struct options_array_item * |