| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* cmdl.c Framework for handling command line options. |
| 4 |
* |
| 5 |
* Authors: Richard Alpe <[email protected]> |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <string.h> |
| 10 |
#include <errno.h> |
| 11 |
|
| 12 |
#include "cmdl.h" |
| 13 |
|
| 14 |
static const struct cmd *find_cmd(const struct cmd *cmds, char *str) |
| 15 |
{ |
| 16 |
const struct cmd *c; |
| 17 |
const struct cmd *match = NULL; |
| 18 |
|
| 19 |
for (c = cmds; c->cmd; c++) { |
| 20 |
if (strstr(c->cmd, str) != c->cmd) |
| 21 |
continue; |
| 22 |
if (match) |
| 23 |
return NULL; |
| 24 |
match = c; |
| 25 |
} |
| 26 |
|
| 27 |
return match; |
| 28 |
} |
| 29 |
|
| 30 |
struct opt *find_opt(struct opt *opts, char *str) |
| 31 |
{ |
| 32 |
struct opt *o; |
| 33 |
struct opt *match = NULL; |
| 34 |
|
| 35 |
for (o = opts; o->key; o++) { |
| 36 |
if (strstr(o->key, str) != o->key) |
| 37 |
continue; |
| 38 |
if (match) |
| 39 |
return NULL; |
| 40 |
|
| 41 |
match = o; |
| 42 |
} |
| 43 |
|
| 44 |
return match; |
| 45 |
} |
| 46 |
|
| 47 |
struct opt *get_opt(struct opt *opts, char *key) |
| 48 |
{ |
| 49 |
struct opt *o; |
| 50 |
|
| 51 |
for (o = opts; o->key; o++) { |
| 52 |
if (strcmp(o->key, key) == 0 && o->val) |
| 53 |
return o; |
| 54 |
} |
| 55 |
|
| 56 |
return NULL; |
| 57 |
} |
| 58 |
|
| 59 |
bool has_opt(struct opt *opts, char *key) |
| 60 |
{ |
| 61 |
return get_opt(opts, key) ? true : false; |
| 62 |
} |
| 63 |
|
| 64 |
char *shift_cmdl(struct cmdl *cmdl) |
| 65 |
{ |
| 66 |
int next; |
| 67 |
|
| 68 |
if (cmdl->optind < cmdl->argc) |
| 69 |
next = (cmdl->optind)++; |
| 70 |
else |
| 71 |
next = cmdl->argc; |
| 72 |
|
| 73 |
return cmdl->argv[next]; |
| 74 |
} |
| 75 |
|
| 76 |
/* Returns the number of options parsed or a negative error code upon failure */ |
| 77 |
int parse_opts(struct opt *opts, struct cmdl *cmdl) |
| 78 |
{ |
| 79 |
int i; |
| 80 |
int cnt = 0; |
| 81 |
|
| 82 |
for (i = cmdl->optind; i < cmdl->argc; i++) { |
| 83 |
struct opt *o; |
| 84 |
|
| 85 |
o = find_opt(opts, cmdl->argv[i]); |
| 86 |
if (!o) { |
| 87 |
fprintf(stderr, "error, invalid option \"%s\"\n", |
| 88 |
cmdl->argv[i]); |
| 89 |
return -EINVAL; |
| 90 |
} |
| 91 |
if (o->flag & OPT_KEYVAL) { |
| 92 |
cmdl->optind++; |
| 93 |
i++; |
| 94 |
} |
| 95 |
cnt++; |
| 96 |
o->val = cmdl->argv[i]; |
| 97 |
cmdl->optind++; |
| 98 |
} |
| 99 |
|
| 100 |
return cnt; |
| 101 |
} |
| 102 |
|
| 103 |
int run_cmd(struct nlmsghdr *nlh, const struct cmd *caller, |
| 104 |
const struct cmd *cmds, struct cmdl *cmdl, void *data) |
| 105 |
{ |
| 106 |
char *name; |
| 107 |
const struct cmd *cmd; |
| 108 |
|
| 109 |
if ((cmdl->optind) >= cmdl->argc) { |
| 110 |
if (caller->help) |
| 111 |
(caller->help)(cmdl); |
| 112 |
return -EINVAL; |
| 113 |
} |
| 114 |
name = cmdl->argv[cmdl->optind]; |
| 115 |
(cmdl->optind)++; |
| 116 |
|
| 117 |
cmd = find_cmd(cmds, name); |
| 118 |
if (!cmd) { |
| 119 |
/* Show help about last command if we don't find this one */ |
| 120 |
if (help_flag && caller->help) { |
| 121 |
(caller->help)(cmdl); |
| 122 |
} else { |
| 123 |
fprintf(stderr, "error, invalid command \"%s\"\n", name); |
| 124 |
fprintf(stderr, "use --help for command help\n"); |
| 125 |
} |
| 126 |
return -EINVAL; |
| 127 |
} |
| 128 |
|
| 129 |
return (cmd->func)(nlh, cmd, cmdl, data); |
| 130 |
} |