Asterisk - The Open Source Telephony Project  21.4.1
Data Structures | Macros | Enumerations | Functions
cli.h File Reference

Standard Command Line Interface. More...

#include "asterisk/linkedlists.h"
#include "asterisk/strings.h"

Go to the source code of this file.

Data Structures

struct  ast_cli_args
 
struct  ast_cli_entry
 descriptor for a cli entry. More...
 

Macros

#define ast_cli_command(fd, s)   ast_cli_command_full(CLI_NO_PERMS, CLI_NO_PERMS, fd, s)
 
#define ast_cli_command_multiple(fd, size, s)   ast_cli_command_multiple_full(CLI_NO_PERMS, CLI_NO_PERMS, fd, size, s)
 
#define AST_CLI_COMPLETE_EOF   "_EOF_"
 
#define AST_CLI_DEFINE(fn, txt, ...)   { .handler = fn, .summary = txt, ## __VA_ARGS__ }
 
#define AST_CLI_ONOFF(x)   (x) ? "On" : "Off"
 return On or Off depending on the argument. This is used in many places in CLI command, having a function to generate this helps maintaining a consistent output (and possibly emitting the output in other languages, at some point).
 
#define ast_cli_register(e)   __ast_cli_register(e, AST_MODULE_SELF)
 Registers a command or an array of commands. More...
 
#define ast_cli_register_multiple(e, len)   __ast_cli_register_multiple(e, len, AST_MODULE_SELF)
 Register multiple commands. More...
 
#define AST_CLI_YESNO(x)   AST_YESNO(x)
 Return Yes or No depending on the argument. More...
 
#define AST_MAX_ARGS   64
 
#define AST_MAX_CMD_LEN   16
 
#define CLI_FAILURE   (char *)RESULT_FAILURE
 
#define CLI_NO_PERMS   -1
 
#define CLI_SHOWUSAGE   (char *)RESULT_SHOWUSAGE
 
#define CLI_SUCCESS   (char *)RESULT_SUCCESS
 
#define ESS(x)   ((x) == 1 ? "" : "s")
 
#define RESULT_FAILURE   2
 
#define RESULT_SHOWUSAGE   1
 
#define RESULT_SUCCESS   0
 

Enumerations

enum  ast_cli_command { CLI_INIT = -2, CLI_GENERATE = -3, CLI_HANDLER = -4 }
 calling arguments for new-style handlers. More...
 

Functions

int __ast_cli_register (struct ast_cli_entry *e, struct ast_module *mod)
 
int __ast_cli_register_multiple (struct ast_cli_entry *e, int len, struct ast_module *mod)
 
void ast_cli (int fd, const char *fmt,...)
 
int ast_cli_allow_at_shutdown (struct ast_cli_entry *e)
 Allow a CLI command to be executed while Asterisk is shutting down. More...
 
int ast_cli_command_full (int uid, int gid, int fd, const char *s)
 Interprets a command Interpret a command s, sending output to fd if uid:gid has permissions to run this command. uid = CLI_NO_PERMS to avoid checking user permissions gid = CLI_NO_PERMS to avoid checking group permissions. More...
 
int ast_cli_command_multiple_full (int uid, int gid, int fd, size_t size, const char *s)
 Executes multiple CLI commands Interpret strings separated by NULL and execute each one, sending output to fd if uid has permissions, uid = CLI_NO_PERMS to avoid checking users permissions. gid = CLI_NO_PERMS to avoid checking group permissions. More...
 
char * ast_cli_complete (const char *word, const char *const choices[], int pos)
 
int ast_cli_completion_add (char *value)
 Add a result to a request for completion options. More...
 
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. More...
 
struct ast_vector_stringast_cli_completion_vector (const char *text, const char *word)
 Generates a vector of strings for CLI completion. More...
 
char * ast_cli_generator (const char *, const char *, int)
 Readline madness Useful for readline, that's about it. More...
 
void ast_cli_print_timestr_fromseconds (int fd, int seconds, const char *prefix)
 Print on cli a duration in seconds in format s year(s), s week(s), s day(s), s hour(s), s second(s) More...
 
int ast_cli_unregister (struct ast_cli_entry *e)
 Unregisters a command or an array of commands. More...
 
int ast_cli_unregister_multiple (struct ast_cli_entry *e, int len)
 Unregister multiple commands. More...
 
char * ast_complete_channels (const char *line, const char *word, int pos, int state, int rpos)
 Command completion for the list of active channels. More...
 

Detailed Description

Standard Command Line Interface.

Definition in file cli.h.

Macro Definition Documentation

#define ast_cli_register (   e)    __ast_cli_register(e, AST_MODULE_SELF)

Registers a command or an array of commands.

Parameters
ewhich cli entry to register. Register your own command
Return values
0on success
-1on failure

Definition at line 256 of file cli.h.

Referenced by load_config().

#define ast_cli_register_multiple (   e,
  len 
)    __ast_cli_register_multiple(e, len, AST_MODULE_SELF)
#define AST_CLI_YESNO (   x)    AST_YESNO(x)

Return Yes or No depending on the argument.

Note that this should probably still be used for CLI commands instead of AST_YESNO(), in the off chance we someday want to translate the CLI.

Parameters
xBoolean value
Returns
"Yes" if x is true (non-zero)
"No" if x is false (zero)
Examples:
app_skel.c.

Definition at line 71 of file cli.h.

Referenced by action_coresettings(), and handle_manager_show_settings().

#define ESS (   x)    ((x) == 1 ? "" : "s")

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 59 of file cli.h.

Referenced by auto_unload_resource().

Enumeration Type Documentation

calling arguments for new-style handlers.

Definition at line 151 of file cli.h.

151  {
152  CLI_INIT = -2, /* return the usage string */
153  CLI_GENERATE = -3, /* behave as 'generator', remap argv to struct ast_cli_args */
154  CLI_HANDLER = -4, /* run the normal handler */
155 };

Function Documentation

int ast_cli_allow_at_shutdown ( struct ast_cli_entry e)

Allow a CLI command to be executed while Asterisk is shutting down.

CLI commands by defeault are disabled when Asterisk is shutting down. This is to ensure the safety of the shutdown since CLI commands may attempt to access resources that have been freed as a result of the shutdown.

If a CLI command should be allowed at shutdown, then the best way to enable this is to call ast_cli_allow_at_shutdown during the CLI_INIT state of the CLI handler.

Definition at line 3061 of file main/cli.c.

References AST_VECTOR_APPEND.

3062 {
3063  int res;
3064 
3065  ast_rwlock_wrlock(&shutdown_commands_lock);
3066  res = AST_VECTOR_APPEND(&shutdown_commands, e);
3067  ast_rwlock_unlock(&shutdown_commands_lock);
3068 
3069  return res;
3070 }
#define AST_VECTOR_APPEND(vec, elem)
Append an element to a vector, growing the vector if needed.
Definition: vector.h:256
int ast_cli_command_full ( int  uid,
int  gid,
int  fd,
const char *  s 
)

Interprets a command Interpret a command s, sending output to fd if uid:gid has permissions to run this command. uid = CLI_NO_PERMS to avoid checking user permissions gid = CLI_NO_PERMS to avoid checking group permissions.

Parameters
uidUser ID that is trying to run the command.
gidGroup ID that is trying to run the command.
fdpipe
sincoming string
Return values
0on success
-1on failure

Definition at line 2974 of file main/cli.c.

References ast_atomic_fetchadd_int(), ast_join, ast_module_running_ref, ast_module_unref, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, ast_shutting_down(), find_best(), ast_cli_entry::inuse, ast_cli_entry::module, S_OR, and ast_cli_entry::usage.

Referenced by ast_cli_command_multiple_full().

2975 {
2976  const char *args[AST_MAX_ARGS + 1];
2977  struct ast_cli_entry *e = NULL;
2978  int x;
2979  char *duplicate = parse_args(s, &x, args + 1, AST_MAX_ARGS, NULL);
2980  char tmp[AST_MAX_ARGS + 1];
2981  char *retval = CLI_FAILURE;
2982  struct ast_cli_args a = {
2983  .fd = fd, .argc = x, .argv = args+1 };
2984 
2985  if (duplicate == NULL)
2986  return RESULT_FAILURE;
2987 
2988  if (x < 1) /* We need at least one entry, otherwise ignore */
2989  goto done;
2990 
2992  e = find_cli(args + 1, 0);
2993  if (e)
2996  if (e == NULL) {
2997  ast_cli(fd, "No such command '%s' (type 'core show help %s' for other possible commands)\n", s, find_best(args + 1));
2998  goto done;
2999  }
3000 
3001  if (ast_shutting_down() && !allowed_on_shutdown(e)) {
3002  ast_cli(fd, "Command '%s' cannot be run during shutdown\n", s);
3003  goto done;
3004  }
3005 
3006  ast_join(tmp, sizeof(tmp), args + 1);
3007  /* Check if the user has rights to run this command. */
3008  if (!cli_has_permissions(uid, gid, tmp)) {
3009  ast_cli(fd, "You don't have permissions to run '%s' command\n", tmp);
3010  goto done;
3011  }
3012 
3013  /*
3014  * Within the handler, argv[-1] contains a pointer to the ast_cli_entry.
3015  * Remember that the array returned by parse_args is NULL-terminated.
3016  */
3017  args[0] = (char *)e;
3018 
3019  /* If the command is in a module it must be running. */
3020  if (!e->module || ast_module_running_ref(e->module)) {
3021  retval = e->handler(e, CLI_HANDLER, &a);
3023  }
3024 
3025  if (retval == CLI_SHOWUSAGE) {
3026  ast_cli(fd, "%s", S_OR(e->usage, "Invalid usage, but no usage information available.\n"));
3027  } else if (retval == CLI_FAILURE) {
3028  ast_cli(fd, "Command '%s' failed.\n", s);
3029  }
3030 
3031 done:
3032  if (e) {
3033  ast_atomic_fetchadd_int(&e->inuse, -1);
3034  }
3035  ast_free(duplicate);
3036  return retval == CLI_SUCCESS ? RESULT_SUCCESS : RESULT_FAILURE;
3037 }
int ast_shutting_down(void)
Definition: asterisk.c:1876
#define ast_join(s, len, w)
Join an array of strings into a single string.
Definition: strings.h:520
descriptor for a cli entry.
Definition: cli.h:171
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:78
int ast_atomic_fetchadd_int(volatile int *p, int v)
Atomically add v to *p and return the previous value of *p.
Definition: lock.h:757
int inuse
Definition: cli.h:179
static struct callattempt * find_best(struct callattempt *outgoing)
find the entry with the best metric, or NULL
Definition: app_queue.c:4885
const char * usage
Definition: cli.h:177
#define ast_module_unref(mod)
Release a reference to the module.
Definition: module.h:483
#define ast_module_running_ref(mod)
Hold a reference to the module if it is running.
Definition: module.h:469
#define S_OR(a, b)
returns the equivalent of logic or for strings: first one if not empty, otherwise second one...
Definition: strings.h:80
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
struct ast_module * module
Definition: cli.h:180
int ast_cli_command_multiple_full ( int  uid,
int  gid,
int  fd,
size_t  size,
const char *  s 
)

Executes multiple CLI commands Interpret strings separated by NULL and execute each one, sending output to fd if uid has permissions, uid = CLI_NO_PERMS to avoid checking users permissions. gid = CLI_NO_PERMS to avoid checking group permissions.

Parameters
uidUser ID that is trying to run the command.
gidGroup ID that is trying to run the command.
fdpipe
sizeis the total size of the string
sincoming string
Returns
number of commands executed

Definition at line 3039 of file main/cli.c.

References ast_cli_command_full().

3040 {
3041  char cmd[512];
3042  int x, y = 0, count = 0;
3043 
3044  for (x = 0; x < size; x++) {
3045  cmd[y] = s[x];
3046  y++;
3047  if (s[x] == '\0') {
3048  ast_cli_command_full(uid, gid, fd, cmd);
3049  y = 0;
3050  count++;
3051  }
3052  }
3053  return count;
3054 }
int ast_cli_command_full(int uid, int gid, int fd, const char *s)
Interprets a command Interpret a command s, sending output to fd if uid:gid has permissions to run th...
Definition: main/cli.c:2974
char* ast_cli_complete ( const char *  word,
const char *const  choices[],
int  pos 
)

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):

1 char *my_generate(const char *line, const char *word, int pos, int n)
2 {
3  static const char * const choices[] = { "one", "two", "three", NULL };
4 if (pos == 2)
5  return ast_cli_complete(word, choices, n);
6 else
7  return NULL;
8 }

Definition at line 1846 of file main/cli.c.

References ast_cli_completion_add(), and ast_strdup.

Referenced by handle_orig().

1847 {
1848  int i, which = 0, len;
1849  len = ast_strlen_zero(word) ? 0 : strlen(word);
1850 
1851  for (i = 0; choices[i]; i++) {
1852  if ((!len || !strncasecmp(word, choices[i], len)) && ++which > state) {
1853  if (state != -1) {
1854  return ast_strdup(choices[i]);
1855  }
1856 
1857  if (ast_cli_completion_add(ast_strdup(choices[i]))) {
1858  return NULL;
1859  }
1860  }
1861  }
1862  return NULL;
1863 }
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
int ast_cli_completion_add(char *value)
Add a result to a request for completion options.
Definition: main/cli.c:2761
int ast_cli_completion_add ( char *  value)

Add a result to a request for completion options.

Parameters
valueA completion option text.
Return values
0Success
-1Failure

This is an alternative to returning individual values from CLI_GENERATE. Instead of repeatedly being asked for the next match and having to start over, you can call this function repeatedly from your own stateful loop. When all matches have been added you can return NULL from the CLI_GENERATE function.

Note
This function always eventually results in calling ast_free on value.

Definition at line 2761 of file main/cli.c.

References ast_threadstorage_get_ptr().

Referenced by ast_cli_complete(), ast_complete_applications(), ast_complete_channels(), cli_complete_show(), handle_cli_sound_show(), and handle_show_named_acl_cmd().

2762 {
2763  return cli_completion_vector_add(ast_threadstorage_get_ptr(&completion_storage), value);
2764 }
void * ast_threadstorage_get_ptr(struct ast_threadstorage *ts)
Retrieve a raw pointer from threadstorage.
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.

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, followed by a NULL. All strings and the array itself are malloc'ed and must be freed by the caller.

Warning
This function cannot be called recursively so it will always fail if called from a CLI_GENERATE callback.

Definition at line 2705 of file main/cli.c.

References ast_cli_completion_vector(), AST_VECTOR_APPEND, AST_VECTOR_CALLBACK_VOID, AST_VECTOR_PTR_FREE, and AST_VECTOR_STEAL_ELEMENTS.

2706 {
2707  struct ast_vector_string *vec = ast_cli_completion_vector(text, word);
2708  char **match_list;
2709 
2710  if (!vec) {
2711  return NULL;
2712  }
2713 
2714  if (AST_VECTOR_APPEND(vec, NULL)) {
2715  /* We failed to NULL terminate the elements */
2716  AST_VECTOR_CALLBACK_VOID(vec, ast_free);
2717  AST_VECTOR_PTR_FREE(vec);
2718 
2719  return NULL;
2720  }
2721 
2722  match_list = AST_VECTOR_STEAL_ELEMENTS(vec);
2723  AST_VECTOR_PTR_FREE(vec);
2724 
2725  return match_list;
2726 }
#define AST_VECTOR_APPEND(vec, elem)
Append an element to a vector, growing the vector if needed.
Definition: vector.h:256
#define AST_VECTOR_STEAL_ELEMENTS(vec)
Steal the elements from a vector and reinitialize.
Definition: vector.h:140
struct ast_vector_string * ast_cli_completion_vector(const char *text, const char *word)
Generates a vector of strings for CLI completion.
Definition: main/cli.c:2766
#define AST_VECTOR_PTR_FREE(vec)
Deallocates this vector pointer.
Definition: vector.h:189
String vector definitions.
Definition: vector.h:55
#define AST_VECTOR_CALLBACK_VOID(vec, callback,...)
Execute a callback on every element in a vector disregarding callback return.
Definition: vector.h:862
struct ast_vector_string* ast_cli_completion_vector ( const char *  text,
const char *  word 
)

Generates a vector of strings for CLI completion.

Parameters
textComplete input being matched.
wordCurrent word being matched

The results contain strings that both: 1) Begin with the string in word. 2) Are valid in a command after the string in text.

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.

Note
All strings and the vector itself are malloc'ed and must be freed by the caller.
The vector is sorted and does not contain any duplicates.
Warning
This function cannot be called recursively so it will always fail if called from a CLI_GENERATE callback.

Definition at line 2766 of file main/cli.c.

References ast_calloc, ast_cli_generator(), ast_strndup, ast_threadstorage_get_ptr(), ast_threadstorage_set_ptr(), AST_VECTOR_CALLBACK_VOID, AST_VECTOR_GET, AST_VECTOR_INSERT_AT, AST_VECTOR_PTR_FREE, AST_VECTOR_REMOVE, and AST_VECTOR_SIZE.

Referenced by ast_cli_completion_matches().

2767 {
2768  char *retstr, *prevstr;
2769  size_t max_equal;
2770  size_t which = 0;
2771  struct ast_vector_string *vec = ast_calloc(1, sizeof(*vec));
2772 
2773  /* Recursion into this function is a coding error. */
2774  ast_assert(!ast_threadstorage_get_ptr(&completion_storage));
2775 
2776  if (!vec) {
2777  return NULL;
2778  }
2779 
2780  if (ast_threadstorage_set_ptr(&completion_storage, vec)) {
2781  ast_log(LOG_ERROR, "Failed to initialize threadstorage for completion.\n");
2782  ast_free(vec);
2783 
2784  return NULL;
2785  }
2786 
2787  while ((retstr = ast_cli_generator(text, word, which)) != NULL) {
2788  if (cli_completion_vector_add(vec, retstr)) {
2789  ast_threadstorage_set_ptr(&completion_storage, NULL);
2790 
2791  goto vector_cleanup;
2792  }
2793 
2794  ++which;
2795  }
2796 
2797  ast_threadstorage_set_ptr(&completion_storage, NULL);
2798 
2799  if (!AST_VECTOR_SIZE(vec)) {
2800  AST_VECTOR_PTR_FREE(vec);
2801 
2802  return NULL;
2803  }
2804 
2805  prevstr = AST_VECTOR_GET(vec, 0);
2806  max_equal = strlen(prevstr);
2807  which = 1;
2808 
2809  /* Find the longest substring that is common to all results
2810  * (it is a candidate for completion), and store a copy in entry 0.
2811  */
2812  while (which < AST_VECTOR_SIZE(vec)) {
2813  size_t i = 0;
2814 
2815  retstr = AST_VECTOR_GET(vec, which);
2816  /* Check for and remove duplicate strings. */
2817  if (!strcasecmp(prevstr, retstr)) {
2818  AST_VECTOR_REMOVE(vec, which, 1);
2819  ast_free(retstr);
2820 
2821  continue;
2822  }
2823 
2824  while (i < max_equal && toupper(prevstr[i]) == toupper(retstr[i])) {
2825  i++;
2826  }
2827 
2828  max_equal = i;
2829  prevstr = retstr;
2830  ++which;
2831  }
2832 
2833  /* Insert longest match to position 0. */
2834  retstr = ast_strndup(AST_VECTOR_GET(vec, 0), max_equal);
2835  if (!retstr || AST_VECTOR_INSERT_AT(vec, 0, retstr)) {
2836  ast_free(retstr);
2837 
2838  goto vector_cleanup;
2839  }
2840 
2841  return vec;
2842 
2843 vector_cleanup:
2844  AST_VECTOR_CALLBACK_VOID(vec, ast_free);
2845  AST_VECTOR_PTR_FREE(vec);
2846 
2847  return NULL;
2848 }
char * ast_cli_generator(const char *text, const char *word, int state)
Readline madness Useful for readline, that's about it.
Definition: main/cli.c:2952
int ast_threadstorage_set_ptr(struct ast_threadstorage *ts, void *ptr)
Set a raw pointer from threadstorage.
#define ast_strndup(str, len)
A wrapper for strndup()
Definition: astmm.h:256
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
void * ast_threadstorage_get_ptr(struct ast_threadstorage *ts)
Retrieve a raw pointer from threadstorage.
#define AST_VECTOR_PTR_FREE(vec)
Deallocates this vector pointer.
Definition: vector.h:189
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition: vector.h:680
#define AST_VECTOR_INSERT_AT(vec, idx, elem)
Insert an element at a specific position in a vector, growing the vector if needed.
Definition: vector.h:338
String vector definitions.
Definition: vector.h:55
#define AST_VECTOR_REMOVE(vec, idx, preserve_ordered)
Remove an element from a vector by index.
Definition: vector.h:412
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition: vector.h:609
#define AST_VECTOR_CALLBACK_VOID(vec, callback,...)
Execute a callback on every element in a vector disregarding callback return.
Definition: vector.h:862
char* ast_cli_generator ( const char *  ,
const char *  ,
int   
)

Readline madness Useful for readline, that's about it.

Return values
0on success
-1on failure

Only call this function to proxy the CLI generator to another.

Definition at line 2952 of file main/cli.c.

Referenced by ast_cli_completion_vector(), cli_alias_passthrough(), and handle_cli_check_permissions().

2953 {
2954  return __ast_cli_generator(text, word, state, 1);
2955 }
void ast_cli_print_timestr_fromseconds ( int  fd,
int  seconds,
const char *  prefix 
)

Print on cli a duration in seconds in format s year(s), s week(s), s day(s), s hour(s), s second(s)

Since
13.8
Parameters
fdfd to print by ast_cli
secondsThe time (in seconds) to print
prefixA Prefix string to add before of duration formatted

Definition at line 3056 of file main/cli.c.

References ast_tv().

Referenced by handle_cdr_pgsql_status(), and realtime_ldap_status().

3057 {
3058  print_uptimestr(fd, ast_tv(seconds, 0), prefix, 0);
3059 }
struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec)
Returns a timeval from sec, usec.
Definition: time.h:235
int ast_cli_unregister ( struct ast_cli_entry e)

Unregisters a command or an array of commands.

Parameters
ewhich cli entry to unregister Unregister your own command. You must pass a completed ast_cli_entry structure
Returns
0

Definition at line 2432 of file main/cli.c.

References ast_cli_entry::_full_cmd, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_cli_entry::cmda, ast_cli_entry::command, ast_cli_entry::inuse, and ast_cli_entry::usage.

Referenced by alias_unregister_cb(), ast_cli_unregister_multiple(), unload_module(), and xmldoc_unload_documentation().

2433 {
2434  if (e->inuse) {
2435  ast_log(LOG_WARNING, "Can't remove command that is in use\n");
2436  } else {
2438  AST_RWLIST_REMOVE(&helpers, e, list);
2440  remove_shutdown_command(e);
2441  ast_free(e->_full_cmd);
2442  e->_full_cmd = NULL;
2443  if (e->handler) {
2444  /* this is a new-style entry. Reset fields and free memory. */
2445  char *cmda = (char *) e->cmda;
2446  memset(cmda, '\0', sizeof(e->cmda));
2447  ast_free(e->command);
2448  e->command = NULL;
2449  e->usage = NULL;
2450  }
2451  }
2452  return 0;
2453 }
char * _full_cmd
Definition: cli.h:181
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:52
int inuse
Definition: cli.h:179
char * command
Definition: cli.h:186
const char * usage
Definition: cli.h:177
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
const char *const cmda[AST_MAX_CMD_LEN]
Definition: cli.h:172
int ast_cli_unregister_multiple ( struct ast_cli_entry e,
int  len 
)

Unregister multiple commands.

Parameters
epointer to first cli entry to unregister
lennumber of entries to unregister
Examples:
app_skel.c.

Definition at line 30 of file clicompat.c.

References ast_cli_unregister().

Referenced by ast_ari_cli_unregister(), close_logger(), codec_shutdown(), conf_destroy_config(), load_module(), pjsip_channel_cli_unregister(), rtp_reload(), stasis_cleanup(), unload_module(), and unload_parking_ui().

31 {
32  return 0;
33 }
char* ast_complete_channels ( const char *  line,
const char *  word,
int  pos,
int  state,
int  rpos 
)

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 1865 of file main/cli.c.

References ao2_iterator_destroy(), ao2_iterator_init(), ao2_ref, ast_cli_completion_add(), ast_strdup, ast_channel_snapshot::base, ast_channel_snapshot_base::name, and ast_channel_snapshot::state.

Referenced by handle_cli_agi_add_cmd(), handle_redirect(), handle_show_chanvar(), handle_show_hangup_channel(), and handle_showchan().

1866 {
1867  int wordlen = strlen(word), which = 0;
1868  struct ao2_container *cached_channels;
1869  char *ret = NULL;
1870  struct ao2_iterator iter;
1871  struct ast_channel_snapshot *snapshot;
1872 
1873  if (pos != rpos) {
1874  return NULL;
1875  }
1876 
1877  cached_channels = ast_channel_cache_all();
1878 
1879  iter = ao2_iterator_init(cached_channels, 0);
1880  for (; (snapshot = ao2_iterator_next(&iter)); ao2_ref(snapshot, -1)) {
1881  if (!strncasecmp(word, snapshot->base->name, wordlen) && (++which > state)) {
1882  if (state != -1) {
1883  ret = ast_strdup(snapshot->base->name);
1884  ao2_ref(snapshot, -1);
1885  break;
1886  }
1887 
1888  if (ast_cli_completion_add(ast_strdup(snapshot->base->name))) {
1889  ao2_ref(snapshot, -1);
1890  break;
1891  }
1892  }
1893  }
1894  ao2_iterator_destroy(&iter);
1895  ao2_ref(cached_channels, -1);
1896 
1897  return ret;
1898 }
struct ast_channel_snapshot_base * base
Structure representing a snapshot of channel state.
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
enum ast_channel_state state
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1821
Generic container type.
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
const ast_string_field name
int ast_cli_completion_add(char *value)
Add a result to a request for completion options.
Definition: main/cli.c:2761