aboutsummaryrefslogtreecommitdiffstats
path: root/main/cli.c
diff options
context:
space:
mode:
authormvanbaak <mvanbaak@f38db490-d61c-443f-a65b-d21fe96a405b>2008-12-10 17:09:15 +0000
committermvanbaak <mvanbaak@f38db490-d61c-443f-a65b-d21fe96a405b>2008-12-10 17:09:15 +0000
commit1c65c0fc8d245917760ba88c43349244d866cd93 (patch)
treea79a97f4472f8e606ac36304d9e344dfd5ff68bb /main/cli.c
parent3bb9d2d34cf974fd781c5f8a548579009f0cbbbd (diff)
add tab completion for 'core set debug X filename.c'
(closes issue #13969) Reported by: jtodd Patches: 20081205__bug13969.diff.txt uploaded by Corydon76 (license 14) Tested by: mvanbaak, eliel git-svn-id: http://svn.digium.com/svn/asterisk/trunk@162687 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'main/cli.c')
-rw-r--r--main/cli.c80
1 files changed, 72 insertions, 8 deletions
diff --git a/main/cli.c b/main/cli.c
index e590046e8..063eeaae2 100644
--- a/main/cli.c
+++ b/main/cli.c
@@ -317,6 +317,46 @@ static struct ast_debug_file *find_debug_file(const char *fn, unsigned int debug
return df;
}
+static char *complete_number(const char *partial, unsigned int min, unsigned int max, int n)
+{
+ int i, count = 0;
+ unsigned int prospective[2];
+ unsigned int part = strtoul(partial, NULL, 10);
+ char next[12];
+
+ if (part < min || part > max) {
+ return NULL;
+ }
+
+ for (i = 0; i < 21; i++) {
+ if (i == 0) {
+ prospective[0] = prospective[1] = part;
+ } else if (part == 0 && !ast_strlen_zero(partial)) {
+ break;
+ } else if (i < 11) {
+ prospective[0] = prospective[1] = part * 10 + (i - 1);
+ } else {
+ prospective[0] = (part * 10 + (i - 11)) * 10;
+ prospective[1] = prospective[0] + 9;
+ }
+ if (i < 11 && (prospective[0] < min || prospective[0] > max)) {
+ continue;
+ } else if (prospective[1] < min || prospective[0] > max) {
+ continue;
+ }
+
+ if (++count > n) {
+ if (i < 11) {
+ snprintf(next, sizeof(next), "%u", prospective[0]);
+ } else {
+ snprintf(next, sizeof(next), "%u...", prospective[0] / 10);
+ }
+ return ast_strdup(next);
+ }
+ }
+ return NULL;
+}
+
static char *handle_verbose(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
{
int oldval;
@@ -325,6 +365,7 @@ static char *handle_verbose(struct ast_cli_entry *e, int cmd, struct ast_cli_arg
int fd = a->fd;
int argc = a->argc;
char **argv = a->argv;
+ char *argv3 = a->argv ? S_OR(a->argv[3], "") : "";
int *dst;
char *what;
struct debug_file_list *dfl;
@@ -333,7 +374,7 @@ static char *handle_verbose(struct ast_cli_entry *e, int cmd, struct ast_cli_arg
switch (cmd) {
case CLI_INIT:
- e->command = "core set {debug|verbose} [off|atleast]";
+ e->command = "core set {debug|verbose}";
e->usage =
"Usage: core set {debug|verbose} [atleast] <level> [filename]\n"
" core set {debug|verbose} off\n"
@@ -344,6 +385,29 @@ static char *handle_verbose(struct ast_cli_entry *e, int cmd, struct ast_cli_arg
return NULL;
case CLI_GENERATE:
+ if (a->pos == 3 || (a->pos == 4 && !strcasecmp(a->argv[3], "atleast"))) {
+ char *pos = a->pos == 3 ? argv3 : S_OR(a->argv[4], "");
+ int numbermatch = (ast_strlen_zero(pos) || strchr("123456789", pos[0])) ? 0 : 21;
+ if (a->n < 21 && numbermatch == 0) {
+ return complete_number(pos, 0, 0x7fffffff, a->n);
+ } else if (pos[0] == '0') {
+ if (a->n == 0) {
+ return ast_strdup("0");
+ } else {
+ return NULL;
+ }
+ } else if (a->n == (21 - numbermatch)) {
+ if (a->pos == 3 && !strncasecmp(argv3, "off", strlen(argv3))) {
+ return ast_strdup("off");
+ } else if (a->pos == 3 && !strncasecmp(argv3, "atleast", strlen(argv3))) {
+ return ast_strdup("atleast");
+ }
+ } else if (a->n == (22 - numbermatch) && a->pos == 3 && ast_strlen_zero(argv3)) {
+ return ast_strdup("atleast");
+ }
+ } else if (a->pos == 4 || (a->pos == 5 && !strcasecmp(argv3, "atleast"))) {
+ return ast_complete_source_filename(a->pos == 4 ? S_OR(a->argv[4], "") : S_OR(a->argv[5], ""), a->n);
+ }
return NULL;
}
/* all the above return, so we proceed with the handler.
@@ -352,7 +416,7 @@ static char *handle_verbose(struct ast_cli_entry *e, int cmd, struct ast_cli_arg
if (argc < e->args)
return CLI_SHOWUSAGE;
- if (!strcasecmp(argv[e->args - 2], "debug")) {
+ if (!strcasecmp(argv[e->args - 1], "debug")) {
dst = &option_debug;
oldval = option_debug;
what = "Core debug";
@@ -361,7 +425,7 @@ static char *handle_verbose(struct ast_cli_entry *e, int cmd, struct ast_cli_arg
oldval = option_verbose;
what = "Verbosity";
}
- if (argc == e->args && !strcasecmp(argv[e->args - 1], "off")) {
+ if (argc == e->args + 1 && !strcasecmp(argv[e->args], "off")) {
unsigned int debug = (*what == 'C');
newlevel = 0;
@@ -375,17 +439,17 @@ static char *handle_verbose(struct ast_cli_entry *e, int cmd, struct ast_cli_arg
goto done;
}
- if (!strcasecmp(argv[e->args-1], "atleast"))
+ if (!strcasecmp(argv[e->args], "atleast"))
atleast = 1;
- if (argc != e->args + atleast && argc != e->args + atleast + 1)
+ if (argc != e->args + atleast + 1 && argc != e->args + atleast + 2)
return CLI_SHOWUSAGE;
- if (sscanf(argv[e->args + atleast - 1], "%d", &newlevel) != 1)
+ if (sscanf(argv[e->args + atleast], "%d", &newlevel) != 1)
return CLI_SHOWUSAGE;
- if (argc == e->args + atleast + 1) {
+ if (argc == e->args + atleast + 2) {
unsigned int debug = (*what == 'C');
dfl = debug ? &debug_files : &verbose_files;
- fn = argv[e->args + atleast];
+ fn = argv[e->args + atleast + 1];
AST_RWLIST_WRLOCK(dfl);