version 1.46, 2021/08/27 17:25:55 |
version 1.47, 2021/09/09 06:57:48 |
|
|
|
|
static int |
static int |
cmd_parse_expand_alias(struct cmd_parse_command *cmd, |
cmd_parse_expand_alias(struct cmd_parse_command *cmd, |
struct cmd_parse_input *pi, struct cmd_parse_result *pr, |
struct cmd_parse_input *pi, struct cmd_parse_result *pr) |
struct cmd_list **cmdlist) |
|
{ |
{ |
struct cmd_parse_argument *arg, *arg1, *first, *after; |
struct cmd_parse_argument *arg, *arg1, *first; |
struct cmd_parse_commands *cmds; |
struct cmd_parse_commands *cmds; |
struct cmd_parse_command *last; |
struct cmd_parse_command *last; |
char *alias, *name, *cause; |
char *alias, *name, *cause; |
|
|
*cmdlist = NULL; |
memset(pr, 0, sizeof *pr); |
|
|
first = TAILQ_FIRST(&cmd->arguments); |
first = TAILQ_FIRST(&cmd->arguments); |
if (first == NULL || first->type != CMD_PARSE_STRING) { |
if (first == NULL || first->type != CMD_PARSE_STRING) { |
|
|
|
|
last = TAILQ_LAST(cmds, cmd_parse_commands); |
last = TAILQ_LAST(cmds, cmd_parse_commands); |
if (last == NULL) { |
if (last == NULL) { |
*cmdlist = cmd_list_new(); |
pr->status = CMD_PARSE_SUCCESS; |
|
pr->cmdlist = cmd_list_new(); |
return (1); |
return (1); |
} |
} |
|
|
TAILQ_REMOVE(&cmd->arguments, first, entry); |
TAILQ_REMOVE(&cmd->arguments, first, entry); |
cmd_parse_free_argument(first); |
cmd_parse_free_argument(first); |
|
|
after = TAILQ_FIRST(&last->arguments); |
|
TAILQ_FOREACH_SAFE(arg, &cmd->arguments, entry, arg1) { |
TAILQ_FOREACH_SAFE(arg, &cmd->arguments, entry, arg1) { |
TAILQ_REMOVE(&cmd->arguments, arg, entry); |
TAILQ_REMOVE(&cmd->arguments, arg, entry); |
if (after == NULL) |
TAILQ_INSERT_TAIL(&last->arguments, arg, entry); |
TAILQ_INSERT_TAIL(&last->arguments, arg, entry); |
|
else |
|
TAILQ_INSERT_AFTER(&last->arguments, after, arg, entry); |
|
after = arg; |
|
} |
} |
cmd_parse_log_commands(cmds, __func__); |
cmd_parse_log_commands(cmds, __func__); |
|
|
cmd_parse_build_commands(cmds, pi, pr); |
cmd_parse_build_commands(cmds, pi, pr); |
if (pr->status != CMD_PARSE_SUCCESS) |
|
*cmdlist = pr->cmdlist; |
|
return (1); |
return (1); |
} |
} |
|
|
static struct cmd_list * |
static void |
cmd_parse_build_command(struct cmd_parse_command *cmd, |
cmd_parse_build_command(struct cmd_parse_command *cmd, |
struct cmd_parse_input *pi, struct cmd_parse_result *pr) |
struct cmd_parse_input *pi, struct cmd_parse_result *pr) |
{ |
{ |
struct cmd_parse_argument *arg; |
struct cmd_parse_argument *arg; |
struct cmd_list *cmdlist = NULL; |
|
struct cmd *add; |
struct cmd *add; |
char *cause; |
char *cause; |
struct args_value *values = NULL; |
struct args_value *values = NULL; |
u_int count = 0, idx; |
u_int count = 0, idx; |
|
|
if (cmd_parse_expand_alias(cmd, pi, pr, &cmdlist)) |
memset(pr, 0, sizeof *pr); |
return (cmdlist); |
|
|
|
|
if (cmd_parse_expand_alias(cmd, pi, pr)) |
|
return; |
|
|
TAILQ_FOREACH(arg, &cmd->arguments, entry) { |
TAILQ_FOREACH(arg, &cmd->arguments, entry) { |
values = xrecallocarray(values, count, count + 1, |
values = xrecallocarray(values, count, count + 1, |
sizeof *values); |
sizeof *values); |
|
|
free(cause); |
free(cause); |
goto out; |
goto out; |
} |
} |
cmdlist = cmd_list_new(); |
pr->status = CMD_PARSE_SUCCESS; |
cmd_list_append(cmdlist, add); |
pr->cmdlist = cmd_list_new(); |
|
cmd_list_append(pr->cmdlist, add); |
|
|
out: |
out: |
for (idx = 0; idx < count; idx++) |
for (idx = 0; idx < count; idx++) |
args_free_value(&values[idx]); |
args_free_value(&values[idx]); |
free(values); |
free(values); |
return (cmdlist); |
|
} |
} |
|
|
static void |
static void |
|
|
{ |
{ |
struct cmd_parse_command *cmd; |
struct cmd_parse_command *cmd; |
u_int line = UINT_MAX; |
u_int line = UINT_MAX; |
struct cmd_list *current = NULL, *result, *add; |
struct cmd_list *current = NULL, *result; |
char *s; |
char *s; |
|
|
|
memset(pr, 0, sizeof *pr); |
|
|
/* Check for an empty list. */ |
/* Check for an empty list. */ |
if (TAILQ_EMPTY(cmds)) { |
if (TAILQ_EMPTY(cmds)) { |
pr->status = CMD_PARSE_SUCCESS; |
pr->status = CMD_PARSE_SUCCESS; |
|
|
current = cmd_list_new(); |
current = cmd_list_new(); |
line = pi->line = cmd->line; |
line = pi->line = cmd->line; |
|
|
add = cmd_parse_build_command(cmd, pi, pr); |
cmd_parse_build_command(cmd, pi, pr); |
if (add == NULL) { |
if (pr->status != CMD_PARSE_SUCCESS) { |
cmd_list_free(result); |
cmd_list_free(result); |
cmd_list_free(current); |
cmd_list_free(current); |
return; |
return; |
} |
} |
cmd_list_append_all(current, add); |
cmd_list_append_all(current, pr->cmdlist); |
cmd_list_free(add); |
cmd_list_free(pr->cmdlist); |
} |
} |
if (current != NULL) { |
if (current != NULL) { |
cmd_parse_print_commands(pi, current); |
cmd_parse_print_commands(pi, current); |
|
|
memset(&input, 0, sizeof input); |
memset(&input, 0, sizeof input); |
pi = &input; |
pi = &input; |
} |
} |
|
memset(&pr, 0, sizeof pr); |
|
|
cmds = cmd_parse_new_commands(); |
cmds = cmd_parse_new_commands(); |
|
|