![]() |
Home page |
Mailing list |
Docs
Asterisk developer's documentation :: Codename Pineapple
cli.h File Reference
Definition in file cli.h.
#include <stdarg.h>
#include "asterisk/linkedlists.h"
Include dependency graph for cli.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.
Data Structures | |
| struct | ast_cli_args |
| struct | ast_cli_entry |
| descriptor for a cli entry See CLI_command_API More... | |
Defines | |
| #define | AST_CLI_COMPLETE_EOF "_EOF_" |
| #define | AST_MAX_ARGS 64 |
| #define | AST_MAX_CMD_LEN 16 |
| #define | CLI_FAILURE (char *)RESULT_FAILURE |
| #define | CLI_SHOWUSAGE (char *)RESULT_SHOWUSAGE |
| #define | CLI_SUCCESS (char *)RESULT_SUCCESS |
| #define | ESS(x) ((x) == 1 ? "" : "s") |
| #define | NEW_CLI(fn, txt,) { .new_handler = fn, .summary = txt, ## __VA_ARGS__ } |
| #define | RESULT_FAILURE 2 |
| #define | RESULT_SHOWUSAGE 1 |
| #define | RESULT_SUCCESS 0 |
Typedefs | |
| typedef char *(* | new_cli_fn )(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) |
| typedef int(* | old_cli_fn )(int fd, int argc, char *argv[]) |
Enumerations | |
| enum | ast_cli_fn { CLI_INIT = -2, CLI_GENERATE = -3, CLI_HANDLER = -4 } |
| calling arguments for new-style handlers See CLI_command_API More... | |
Functions | |
| void | ast_cli (int fd, char *fmt,...) __attribute__((format(printf |
| int | ast_cli_command (int fd, const char *s) |
| Interprets a command Interpret a command s, sending output to fd Returns 0 on succes, -1 on failure. | |
| char * | ast_cli_complete (const char *word, char *const choices[], int pos) |
| char ** | ast_cli_completion_matches (const char *, const char *) |
| Generates a NULL-terminated array of strings that 1) begin with the string in the second parameter, and 2) are valid in a command after the string in the first parameter. | |
| char * | ast_cli_generator (const char *, const char *, int) |
| Readline madness Useful for readline, that's about it Returns 0 on success, -1 on failure. | |
| int | ast_cli_generatornummatches (const char *, const char *) |
| Return the number of unique matches for the generator. | |
| int | ast_cli_register (struct ast_cli_entry *e) |
| Registers a command or an array of commands. | |
| void | ast_cli_register_multiple (struct ast_cli_entry *e, int len) |
| Register multiple commands. | |
| int | ast_cli_unregister (struct ast_cli_entry *e) |
| Unregisters a command or an array of commands. | |
| void | ast_cli_unregister_multiple (struct ast_cli_entry *e, int len) |
| Unregister multiple commands. | |
| char * | ast_complete_channels (const char *line, const char *word, int pos, int state, int rpos) |
| Command completion for the list of active channels. | |
|
|
Definition at line 49 of file cli.h. Referenced by ast_el_strtoarr(), and cli_complete(). |
|
|
Definition at line 47 of file cli.h. Referenced by __ast_cli_generator(), and ast_cli_command(). |
|
|
Definition at line 45 of file cli.h. Referenced by __ast_cli_register(), and find_best(). |
|
|
Definition at line 43 of file cli.h. Referenced by console_answer(), console_autoanswer(), console_dial(), console_flash(), console_hangup(), console_sendtext(), handle_load(), and handle_unload(). |
|
|
Definition at line 42 of file cli.h. Referenced by ast_cli_command(), console_answer(), console_autoanswer(), console_dial(), console_flash(), console_hangup(), console_mute(), console_sendtext(), handle_chanlist(), handle_core_set_debug_channel(), handle_debugchan_deprecated(), handle_load(), handle_modlist(), handle_nodebugchan_deprecated(), handle_showuptime(), handle_unload(), and handle_verbose(). |
|
|
Definition at line 41 of file cli.h. Referenced by ast_cli_command(), console_answer(), console_autoanswer(), console_dial(), console_flash(), console_hangup(), console_mute(), console_sendtext(), handle_chanlist(), handle_load(), handle_reload(), handle_showuptime(), handle_verbose(), and help1(). |
|
|
In many cases we need to print singular or plural words depending on a count. This macro helps us e.g. printf("we have %d object%s", n, ESS(n)); Definition at line 56 of file cli.h. Referenced by cli_realtime_update(), group_show_channels(), handle_chanlist(), handle_cli_status(), and print_uptimestr(). |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
calling arguments for new-style handlers See CLI_command_API
Definition at line 129 of file cli.h. 00129 { 00130 CLI_INIT = -2, /* return the usage string */ 00131 CLI_GENERATE = -3, /* behave as 'generator', remap argv to struct ast_cli_args */ 00132 CLI_HANDLER = -4, /* run the normal handler */ 00133 };
|
|
||||||||||||||||
|
|
|
||||||||||||
|
Interprets a command Interpret a command s, sending output to fd Returns 0 on succes, -1 on failure.
Definition at line 1682 of file cli.c. References ast_cli_entry::_deprecated_by, ast_cli_entry::_full_cmd, ast_cli(), AST_LIST_LOCK, AST_LIST_UNLOCK, AST_MAX_ARGS, CLI_HANDLER, CLI_SHOWUSAGE, CLI_SUCCESS, ast_cli_entry::deprecated, ast_cli_args::fd, find_best(), find_cli(), free, ast_cli_entry::handler, ast_cli_entry::inuse, ast_cli_entry::new_handler, parse_args(), RESULT_FAILURE, RESULT_SHOWUSAGE, RESULT_SUCCESS, S_OR, and ast_cli_entry::usage. Referenced by action_command(), cli_activate(), consolehandler(), and exit_completely(). 01683 { 01684 char *args[AST_MAX_ARGS + 1]; 01685 struct ast_cli_entry *e; 01686 int x; 01687 int res; 01688 char *dup = parse_args(s, &x, args + 1, AST_MAX_ARGS, NULL); 01689 01690 if (dup == NULL) 01691 return -1; 01692 01693 if (x < 1) /* We need at least one entry, otherwise ignore */ 01694 goto done; 01695 01696 AST_LIST_LOCK(&helpers); 01697 e = find_cli(args + 1, 0); 01698 if (e) 01699 ast_atomic_fetchadd_int(&e->inuse, 1); 01700 AST_LIST_UNLOCK(&helpers); 01701 if (e == NULL) { 01702 ast_cli(fd, "No such command '%s' (type 'help' for help)\n", find_best(args + 1)); 01703 goto done; 01704 } 01705 /* 01706 * Within the handler, argv[-1] contains a pointer to the ast_cli_entry. 01707 * Remember that the array returned by parse_args is NULL-terminated. 01708 */ 01709 args[0] = (char *)e; 01710 01711 if (!e->new_handler) /* old style */ 01712 res = e->handler(fd, x, args + 1); 01713 else { 01714 struct ast_cli_args a = { 01715 .fd = fd, .argc = x, .argv = args+1 }; 01716 char *retval = e->new_handler(e, CLI_HANDLER, &a); 01717 01718 if (retval == CLI_SUCCESS) 01719 res = RESULT_SUCCESS; 01720 else if (retval == CLI_SHOWUSAGE) 01721 res = RESULT_SHOWUSAGE; 01722 else 01723 res = RESULT_FAILURE; 01724 } 01725 switch (res) { 01726 case RESULT_SHOWUSAGE: 01727 ast_cli(fd, "%s", S_OR(e->usage, "Invalid usage, but no usage information available.\n")); 01728 break; 01729 01730 case RESULT_FAILURE: 01731 ast_cli(fd, "Command '%s' failed.\n", s); 01732 /* FALLTHROUGH */ 01733 default: 01734 AST_LIST_LOCK(&helpers); 01735 if (e->deprecated == 1) { 01736 ast_cli(fd, "The '%s' command is deprecated and will be removed in a future release. Please use '%s' instead.\n", e->_full_cmd, e->_deprecated_by); 01737 e->deprecated = 2; 01738 } 01739 AST_LIST_UNLOCK(&helpers); 01740 break; 01741 } 01742 ast_atomic_fetchadd_int(&e->inuse, -1); 01743 done: 01744 free(dup); 01745 return 0; 01746 }
|
|
||||||||||||||||
|
Helper function to generate cli entries from a NULL-terminated array. Returns the n-th matching entry from the array, or NULL if not found. Can be used to implement generate() for static entries as below (in this example we complete the word in position 2): char *my_generate(const char *line, const char *word, int pos, int n) { static char *choices = { "one", "two", "three", NULL }; if (pos == 2) return ast_cli_complete(word, choices, n); else return NULL; } Definition at line 858 of file cli.c. References ast_strdup, ast_strlen_zero(), and len. Referenced by complete_orig(), and complete_show_applications(). 00859 { 00860 int i, which = 0, len; 00861 len = ast_strlen_zero(word) ? 0 : strlen(word); 00862 00863 for (i = 0; choices[i]; i++) { 00864 if ((!len || !strncasecmp(word, choices[i], len)) && ++which > state) 00865 return ast_strdup(choices[i]); 00866 } 00867 return NULL; 00868 }
|
|
||||||||||||
|
Generates a NULL-terminated array of strings that 1) begin with the string in the second parameter, and 2) are valid in a command after the string in the first parameter. The first entry (offset 0) of the result is the longest common substring in the results, useful to extend the string that has been completed. Subsequent entries are all possible values, followe by a NULL. All strings and the array itself are malloc'ed and must be freed by the caller. Definition at line 1535 of file cli.c. References ast_cli_generator(), and ast_realloc. Referenced by cli_complete(), and handle_commandmatchesarray(). 01536 { 01537 char **match_list = NULL, *retstr, *prevstr; 01538 size_t match_list_len, max_equal, which, i; 01539 int matches = 0; 01540 01541 /* leave entry 0 free for the longest common substring */ 01542 match_list_len = 1; 01543 while ((retstr = ast_cli_generator(text, word, matches)) != NULL) { 01544 if (matches + 1 >= match_list_len) { 01545 match_list_len <<= 1; 01546 if (!(match_list = ast_realloc(match_list, match_list_len * sizeof(*match_list)))) 01547 return NULL; 01548 } 01549 match_list[++matches] = retstr; 01550 } 01551 01552 if (!match_list) 01553 return match_list; /* NULL */ 01554 01555 /* Find the longest substring that is common to all results 01556 * (it is a candidate for completion), and store a copy in entry 0. 01557 */ 01558 prevstr = match_list[1]; 01559 max_equal = strlen(prevstr); 01560 for (which = 2; which <= matches; which++) { 01561 for (i = 0; i < max_equal && toupper(prevstr[i]) == toupper(match_list[which][i]); i++) 01562 continue; 01563 max_equal = i; 01564 } 01565 01566 if (!(retstr = ast_malloc(max_equal + 1))) 01567 return NULL; 01568 01569 ast_copy_string(retstr, match_list[1], max_equal + 1); 01570 match_list[0] = retstr; 01571 01572 /* ensure that the array is NULL terminated */ 01573 if (matches + 1 >= match_list_len) { 01574 if (!(match_list = ast_realloc(match_list, (match_list_len + 1) * sizeof(*match_list)))) 01575 return NULL; 01576 } 01577 match_list[matches + 1] = NULL; 01578 01579 return match_list; 01580 }
|
|
||||||||||||||||
|
Readline madness Useful for readline, that's about it Returns 0 on success, -1 on failure.
Definition at line 1677 of file cli.c. References __ast_cli_generator(). Referenced by ast_cli_completion_matches(), and ast_cli_generatornummatches(). 01678 { 01679 return __ast_cli_generator(text, word, state, 1); 01680 }
|
|
||||||||||||
|
Return the number of unique matches for the generator.
Definition at line 1518 of file cli.c. References ast_cli_generator(), and free. Referenced by handle_commandnummatches(). 01519 { 01520 int matches = 0, i = 0; 01521 char *buf = NULL, *oldbuf = NULL; 01522 01523 while ((buf = ast_cli_generator(text, word, i++))) { 01524 if (!oldbuf || strcmp(buf,oldbuf)) 01525 matches++; 01526 if (oldbuf) 01527 free(oldbuf); 01528 oldbuf = buf; 01529 } 01530 if (oldbuf) 01531 free(oldbuf); 01532 return matches; 01533 }
|
|
|
Registers a command or an array of commands.
Definition at line 1349 of file cli.c. References __ast_cli_register(). Referenced by ast_cdr_engine_init(), ast_cli_register_multiple(), dnsmgr_init(), do_reload(), and load_module(). 01350 { 01351 return __ast_cli_register(e, NULL); 01352 }
|
|
||||||||||||
|
Register multiple commands.
Definition at line 1357 of file cli.c. References ast_cli_register(). Referenced by __ast_register_translator(), ast_builtins_init(), ast_channels_init(), ast_file_init(), ast_http_init(), ast_image_init(), ast_rtp_init(), ast_udptl_init(), astdb_init(), crypto_init(), iax_provision_init(), init_framer(), init_logger(), init_manager(), load_module(), load_pbx(), register_config_cli(), and sip_cli_and_manager_commands_register(). 01358 { 01359 int i; 01360 01361 for (i = 0; i < len; i++) 01362 ast_cli_register(e + i); 01363 }
|
|
|
Unregisters a command or an array of commands.
Definition at line 1343 of file cli.c. References __ast_cli_unregister(). Referenced by ast_cli_unregister_multiple(), do_reload(), load_module(), and unload_module(). 01344 { 01345 return __ast_cli_unregister(e, NULL); 01346 }
|
|
||||||||||||
|
Unregister multiple commands.
Definition at line 1365 of file cli.c. References ast_cli_unregister(). Referenced by __unload_module(), iax_provision_unload(), load_module(), sip_cli_and_manager_commands_unregister(), and unload_module(). 01366 { 01367 int i; 01368 01369 for (i = 0; i < len; i++) 01370 ast_cli_unregister(e + i); 01371 }
|
|
||||||||||||||||||||||||
|
Command completion for the list of active channels. This can be called from a CLI command completion function that wants to complete from the list of active channels. 'rpos' is the required position in the command. This function will return NULL immediately if 'rpos' is not the same as the current position, 'pos'. Definition at line 870 of file cli.c. References ast_channel_unlock, ast_channel_walk_locked(), and ast_strdup. Referenced by complete_ch_3(), complete_ch_4(), and handle_core_set_debug_channel(). 00871 { 00872 struct ast_channel *c = NULL; 00873 int which = 0; 00874 int wordlen; 00875 char notfound = '\0'; 00876 char *ret = ¬found; /* so NULL can break the loop */ 00877 00878 if (pos != rpos) 00879 return NULL; 00880 00881 wordlen = strlen(word); 00882 00883 while (ret == ¬found && (c = ast_channel_walk_locked(c))) { 00884 if (!strncasecmp(word, c->name, wordlen) && ++which > state) 00885 ret = ast_strdup(c->name); 00886 ast_channel_unlock(c); 00887 } 00888 return ret == ¬found ? NULL : ret; 00889 }
|