diff options
author | Jakub Sitnicki <jkbs@redhat.com> | 2018-07-19 15:51:14 +0200 |
---|---|---|
committer | Ben Pfaff <blp@ovn.org> | 2018-07-23 16:09:43 -0700 |
commit | a95199f26a773a90d5ff71be7d9734f9fb963170 (patch) | |
tree | 2f7d429846f7460c18f89fe55c7e389fef40f33d /lib | |
parent | 6441935951b35cef5ef654e5d19963ae338f9bb7 (diff) | |
download | openvswitch-a95199f26a773a90d5ff71be7d9734f9fb963170.tar.gz |
db-ctl-base: Propagate errors from the commands parser.
Let the caller decide how to handle the error. Prepare for using the
parser in ovn-nbctl daemon mode.
Signed-off-by: Jakub Sitnicki <jkbs@redhat.com>
Signed-off-by: Ben Pfaff <blp@ovn.org>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/db-ctl-base.c | 30 | ||||
-rw-r--r-- | lib/db-ctl-base.h | 6 |
2 files changed, 24 insertions, 12 deletions
diff --git a/lib/db-ctl-base.c b/lib/db-ctl-base.c index f92da7c61..4e0eb9c5c 100644 --- a/lib/db-ctl-base.c +++ b/lib/db-ctl-base.c @@ -2251,22 +2251,24 @@ ctl_add_cmd_options(struct option **options_p, size_t *n_options_p, } /* Parses command-line input for commands. */ -struct ctl_command * +char * ctl_parse_commands(int argc, char *argv[], struct shash *local_options, - size_t *n_commandsp) + struct ctl_command **commandsp, size_t *n_commandsp) { struct ctl_command *commands; size_t n_commands, allocated_commands; int i, start; + char *error; commands = NULL; n_commands = allocated_commands = 0; + *commandsp = NULL; + *n_commandsp = 0; + for (start = i = 0; i <= argc; i++) { if (i == argc || !strcmp(argv[i], "--")) { if (i > start) { - char *error; - if (n_commands >= allocated_commands) { struct ctl_command *c; @@ -2277,21 +2279,31 @@ ctl_parse_commands(int argc, char *argv[], struct shash *local_options, } } error = parse_command(i - start, &argv[start], local_options, - &commands[n_commands++]); + &commands[n_commands]); if (error) { - ctl_fatal("%s", error); + struct ctl_command *c; + + for (c = commands; c < &commands[n_commands]; c++) { + shash_destroy_free_data(&c->options); + } + free(commands); + + return error; } + + n_commands++; } else if (!shash_is_empty(local_options)) { - ctl_fatal("missing command name (use --help for help)"); + return xstrdup("missing command name (use --help for help)"); } start = i + 1; } } if (!n_commands) { - ctl_fatal("missing command name (use --help for help)"); + return xstrdup("missing command name (use --help for help)"); } + *commandsp = commands; *n_commandsp = n_commands; - return commands; + return NULL; } /* Prints all registered commands. */ diff --git a/lib/db-ctl-base.h b/lib/db-ctl-base.h index ba771a180..284b573d0 100644 --- a/lib/db-ctl-base.h +++ b/lib/db-ctl-base.h @@ -166,9 +166,9 @@ void ctl_print_options(const struct option *); void ctl_add_cmd_options(struct option **, size_t *n_options_p, size_t *allocated_options_p, int opt_val); void ctl_register_commands(const struct ctl_command_syntax *); -struct ctl_command *ctl_parse_commands(int argc, char *argv[], - struct shash *local_options, - size_t *n_commandsp); +char * OVS_WARN_UNUSED_RESULT ctl_parse_commands( + int argc, char *argv[], struct shash *local_options, + struct ctl_command **commandsp, size_t *n_commandsp); /* Sometimes, it is desirable to print the table with weak reference to * rows in a 'cmd_show_table' table. In that case, the 'weak_ref_table' |