Skip to content

Commit 36dfe81

Browse files
author
Rob Sanders
committed
Forgot to include actual source in last commit
1 parent 38bab91 commit 36dfe81

1 file changed

Lines changed: 27 additions & 23 deletions

File tree

libcli.c

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ static int cli_int_validate_pipeline(struct cli_def *cli, struct cli_pipeline *p
168168
static int cli_int_execute_pipeline(struct cli_def *cli, struct cli_pipeline *pipeline);
169169
inline void cli_int_show_pipeline(struct cli_def *cli, struct cli_pipeline *pipeline);
170170
static void cli_int_free_pipeline(struct cli_pipeline *pipeline);
171-
static void cli_register_command_core(struct cli_def *cli, struct cli_command *parent, struct cli_command *c);
171+
static struct cli_command *cli_register_command_core(struct cli_def *cli, struct cli_command *parent, struct cli_command *c);
172172
static void cli_int_wrap_help_line(char *nameptr, char *helpptr, struct cli_comphelp *comphelp);
173173

174174
static char DELIM_OPT_START[] = "[";
@@ -193,15 +193,14 @@ static ssize_t _write(int fd, const void *buf, size_t count) {
193193
return written;
194194
}
195195

196-
char *cli_command_name(struct cli_def *cli, struct cli_command *command) {
196+
char *cli_int_command_name(struct cli_def *cli, struct cli_command *command) {
197197
char *name;
198198
char *o;
199-
200-
if (cli->commandname) {
201-
free(cli->commandname);
202-
cli->commandname = NULL;
199+
200+
if (command->full_command_name) {
201+
free(command->full_command_name);
202+
command->full_command_name = NULL;
203203
}
204-
name = cli->commandname;
205204

206205
if (!(name = calloc(1, 1))) return NULL;
207206

@@ -215,10 +214,13 @@ char *cli_command_name(struct cli_def *cli, struct cli_command *command) {
215214
command = command->parent;
216215
free(o);
217216
}
218-
cli->commandname = name;
219217
return name;
220218
}
221219

220+
char *cli_command_name(struct cli_def *cli, struct cli_command *command) {
221+
return command->full_command_name;
222+
}
223+
222224
void cli_set_auth_callback(struct cli_def *cli, int (*auth_callback)(const char *, const char *)) {
223225
cli->auth_callback = auth_callback;
224226
}
@@ -364,13 +366,20 @@ int cli_set_configmode(struct cli_def *cli, int mode, const char *config_desc) {
364366
return old;
365367
}
366368

367-
void cli_register_command_core(struct cli_def *cli, struct cli_command *parent, struct cli_command *c) {
369+
struct cli_command *cli_register_command_core(struct cli_def *cli, struct cli_command *parent, struct cli_command *c) {
368370
struct cli_command *p = NULL;
369371

370-
if (!c) return;
372+
if (!c) return NULL;
371373

372374
c->parent = parent;
373-
375+
376+
/* Go build the 'full command name' now that told it who its parent is.
377+
* If this fails, clean it up and return a NULL w/o proceeding.
378+
*/
379+
if (!(c->full_command_name = cli_int_command_name(cli, c))) {
380+
cli_free_command(cli, c);
381+
return NULL;
382+
}
374383
/*
375384
* Figure out we have a chain, or would be the first element on it.
376385
* If we'd be the first element, assign as such.
@@ -401,7 +410,7 @@ void cli_register_command_core(struct cli_def *cli, struct cli_command *parent,
401410
p->next = c;
402411
c->previous = p;
403412
}
404-
return;
413+
return c;
405414
}
406415

407416
struct cli_command *cli_register_command(struct cli_def *cli, struct cli_command *parent, const char *command,
@@ -426,9 +435,8 @@ struct cli_command *cli_register_command(struct cli_def *cli, struct cli_command
426435
free(c);
427436
return NULL;
428437
}
429-
430-
cli_register_command_core(cli, parent, c);
431-
return c;
438+
439+
return cli_register_command_core(cli, parent, c);
432440
}
433441

434442
static void cli_free_command(struct cli_def *cli, struct cli_command *cmd) {
@@ -443,7 +451,7 @@ static void cli_free_command(struct cli_def *cli, struct cli_command *cmd) {
443451
free(cmd->command);
444452
if (cmd->help) free(cmd->help);
445453
if (cmd->optargs) cli_unregister_all_optarg(cmd);
446-
454+
if (cmd->full_command_name) free(cmd->full_command_name);
447455
/*
448456
* Ok, update the pointers of anyone who pointed to us.
449457
* We have 3 pointers to worry about - parent, previous, and next.
@@ -721,7 +729,6 @@ int cli_done(struct cli_def *cli) {
721729

722730
if (cli->buildmode) cli_int_free_buildmode(cli);
723731
cli_unregister_tree(cli, cli->commands, CLI_ANY_COMMAND);
724-
free_z(cli->commandname);
725732
free_z(cli->modestring);
726733
free_z(cli->banner);
727734
free_z(cli->promptchar);
@@ -2125,8 +2132,7 @@ struct cli_command *cli_register_filter(struct cli_def *cli, const char *command
21252132
}
21262133

21272134
// Filters are all registered at the top level.
2128-
cli_register_command_core(cli, NULL, c);
2129-
return c;
2135+
return cli_register_command_core(cli, NULL, c);
21302136
}
21312137

21322138
int cli_unregister_filter(struct cli_def *cli, const char *command) {
@@ -2353,7 +2359,6 @@ void cli_int_free_buildmode(struct cli_def *cli) {
23532359
if (!cli || !cli->buildmode) return;
23542360
cli_unregister_tree(cli, cli->commands, CLI_BUILDMODE_COMMAND);
23552361
cli->mode = cli->buildmode->mode;
2356-
free_z(cli->buildmode->cname);
23572362
free_z(cli->buildmode->mode_text);
23582363
cli_int_free_found_optargs(&cli->buildmode->found_optargs);
23592364
free_z(cli->buildmode);
@@ -2432,7 +2437,7 @@ int cli_int_enter_buildmode(struct cli_def *cli, struct cli_pipeline_stage *stag
24322437
}
24332438
}
24342439
}
2435-
cli->buildmode->cname = strdup(cli_command_name(cli, stage->command));
2440+
cli->buildmode->cname = cli_command_name(cli, stage->command);
24362441
// Now add the four 'always there' commands to cancel current mode and to execute the command, show settings, and
24372442
// unset
24382443
c = cli_int_register_buildmode_command(cli, NULL, "cancel", cli_int_buildmode_cancel_cback, 0, PRIVILEGE_UNPRIVILEGED,
@@ -2504,8 +2509,7 @@ struct cli_command *cli_int_register_buildmode_command(struct cli_def *cli, stru
25042509
}
25052510

25062511
// Buildmode commmands are all registered at the top level
2507-
cli_register_command_core(cli, NULL, c);
2508-
return c;
2512+
return cli_register_command_core(cli, NULL, c);
25092513
}
25102514

25112515
int cli_int_execute_buildmode(struct cli_def *cli) {

0 commit comments

Comments
 (0)