reduce indentation on a large function.

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@47732 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Luigi Rizzo
2006-11-16 14:58:24 +00:00
parent e58079b067
commit 5fb52f8244

View File

@@ -1441,7 +1441,10 @@ static char *parse_args(const char *s, int *argc, char *argv[], int max, int *tr
int quoted = 0; int quoted = 0;
int escaped = 0; int escaped = 0;
int whitespace = 1; int whitespace = 1;
int dummy = 0;
if (trailingwhitespace == NULL)
trailingwhitespace = &dummy;
*trailingwhitespace = 0; *trailingwhitespace = 0;
if (s == NULL) /* invalid, though! */ if (s == NULL) /* invalid, though! */
return NULL; return NULL;
@@ -1573,7 +1576,7 @@ static char *__ast_cli_generator(const char *text, const char *word, int state,
int tws = 0; int tws = 0;
char *dup = parse_args(text, &x, argv, sizeof(argv) / sizeof(argv[0]), &tws); char *dup = parse_args(text, &x, argv, sizeof(argv) / sizeof(argv[0]), &tws);
if (!dup) /* error */ if (!dup) /* malloc error */
return NULL; return NULL;
argindex = (!ast_strlen_zero(word) && x>0) ? x-1 : x; argindex = (!ast_strlen_zero(word) && x>0) ? x-1 : x;
/* rebuild the command, ignore tws */ /* rebuild the command, ignore tws */
@@ -1629,46 +1632,49 @@ int ast_cli_command(int fd, const char *s)
char *args[AST_MAX_ARGS + 1]; char *args[AST_MAX_ARGS + 1];
struct ast_cli_entry *e; struct ast_cli_entry *e;
int x; int x;
char *dup; int res;
int tws; char *dup = parse_args(s, &x, args + 1, AST_MAX_ARGS, NULL);
if (!(dup = parse_args(s, &x, args + 1, AST_MAX_ARGS, &tws))) if (dup == NULL)
return -1; return -1;
/* We need at least one entry, or ignore */ if (x < 1) /* We need at least one entry, otherwise ignore */
if (x > 0) { goto done;
AST_LIST_LOCK(&helpers); AST_LIST_LOCK(&helpers);
e = find_cli(args + 1, 0); e = find_cli(args + 1, 0);
if (e) if (e)
ast_atomic_fetchadd_int(&e->inuse, 1); ast_atomic_fetchadd_int(&e->inuse, 1);
AST_LIST_UNLOCK(&helpers); AST_LIST_UNLOCK(&helpers);
if (e) { if (e == NULL) {
int res; ast_cli(fd, "No such command '%s' (type 'help' for help)\n", find_best(args + 1));
/* within calling the handler, argv[-1] contains a pointer goto done;
* to the cli entry, and the array is null-terminated }
/*
* Within the handler, argv[-1] contains a pointer to the ast_cli_entry.
* Remember that the array returned by parse_args is NULL-terminated.
*/ */
args[0] = (char *)e; args[0] = (char *)e;
if (e->new_handler) { /* new style */
char *retval; if (!e->new_handler) /* old style */
res = e->handler(fd, x, args + 1);
else {
struct ast_cli_args a = { struct ast_cli_args a = {
.fd = fd, .argc = x, .argv = args+1 }; .fd = fd, .argc = x, .argv = args+1 };
retval = e->new_handler(e, CLI_HANDLER, &a); char *retval = e->new_handler(e, CLI_HANDLER, &a);
if (retval == CLI_SUCCESS) if (retval == CLI_SUCCESS)
res = RESULT_SUCCESS; res = RESULT_SUCCESS;
else if (retval == CLI_SHOWUSAGE) else if (retval == CLI_SHOWUSAGE)
res = RESULT_SHOWUSAGE; res = RESULT_SHOWUSAGE;
else else
res = RESULT_FAILURE; res = RESULT_FAILURE;
} else { /* old style */
res = e->handler(fd, x, args + 1);
} }
switch (res) { switch (res) {
case RESULT_SHOWUSAGE: case RESULT_SHOWUSAGE:
if (e->usage) ast_cli(fd, "%s", S_OR(e->usage, "Invalid usage, but no usage information available.\n"));
ast_cli(fd, "%s", e->usage);
else
ast_cli(fd, "Invalid usage, but no usage information available.\n");
break; break;
case RESULT_FAILURE: case RESULT_FAILURE:
ast_cli(fd, "Command '%s' failed.\n", s); ast_cli(fd, "Command '%s' failed.\n", s);
/* FALLTHROUGH */ /* FALLTHROUGH */
@@ -1681,12 +1687,8 @@ int ast_cli_command(int fd, const char *s)
AST_LIST_UNLOCK(&helpers); AST_LIST_UNLOCK(&helpers);
break; break;
} }
} else
ast_cli(fd, "No such command '%s' (type 'help' for help)\n", find_best(args + 1));
if (e)
ast_atomic_fetchadd_int(&e->inuse, -1); ast_atomic_fetchadd_int(&e->inuse, -1);
} done:
free(dup); free(dup);
return 0; return 0;
} }