Asterisk - The Open Source Telephony Project GIT-master-67613d1
Data Structures | Macros | Enumerations | Functions
cli.h File Reference

Standard Command Line Interface. More...

#include "asterisk/linkedlists.h"
#include "asterisk/strings.h"
Include dependency graph for cli.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). More...
 
#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

◆ ast_cli_command

#define ast_cli_command (   fd,
 
)    ast_cli_command_full(CLI_NO_PERMS, CLI_NO_PERMS, fd, s)

Definition at line 232 of file cli.h.

◆ ast_cli_command_multiple

#define ast_cli_command_multiple (   fd,
  size,
 
)    ast_cli_command_multiple_full(CLI_NO_PERMS, CLI_NO_PERMS, fd, size, s)

Definition at line 248 of file cli.h.

◆ AST_CLI_COMPLETE_EOF

#define AST_CLI_COMPLETE_EOF   "_EOF_"

Definition at line 52 of file cli.h.

◆ AST_CLI_DEFINE

#define AST_CLI_DEFINE (   fn,
  txt,
  ... 
)    { .handler = fn, .summary = txt, ## __VA_ARGS__ }
Examples
app_skel.c.

Definition at line 197 of file cli.h.

◆ AST_CLI_ONOFF

#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).

Definition at line 78 of file cli.h.

◆ ast_cli_register

#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.

◆ ast_cli_register_multiple

#define ast_cli_register_multiple (   e,
  len 
)     __ast_cli_register_multiple(e, len, AST_MODULE_SELF)

Register multiple commands.

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

Definition at line 265 of file cli.h.

◆ AST_CLI_YESNO

#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.

◆ AST_MAX_ARGS

#define AST_MAX_ARGS   64

Definition at line 50 of file cli.h.

◆ AST_MAX_CMD_LEN

#define AST_MAX_CMD_LEN   16

Definition at line 48 of file cli.h.

◆ CLI_FAILURE

#define CLI_FAILURE   (char *)RESULT_FAILURE

Definition at line 46 of file cli.h.

◆ CLI_NO_PERMS

#define CLI_NO_PERMS   -1

Definition at line 38 of file cli.h.

◆ CLI_SHOWUSAGE

#define CLI_SHOWUSAGE   (char *)RESULT_SHOWUSAGE

Definition at line 45 of file cli.h.

◆ CLI_SUCCESS

#define CLI_SUCCESS   (char *)RESULT_SUCCESS
Examples
app_skel.c.

Definition at line 44 of file cli.h.

◆ ESS

#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.

◆ RESULT_FAILURE

#define RESULT_FAILURE   2

Definition at line 42 of file cli.h.

◆ RESULT_SHOWUSAGE

#define RESULT_SHOWUSAGE   1

Definition at line 41 of file cli.h.

◆ RESULT_SUCCESS

#define RESULT_SUCCESS   0

Definition at line 40 of file cli.h.

Enumeration Type Documentation

◆ ast_cli_command

calling arguments for new-style handlers.

Enumerator
CLI_INIT 
CLI_GENERATE 
CLI_HANDLER 

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};
@ CLI_HANDLER
Definition: cli.h:154
@ CLI_INIT
Definition: cli.h:152
@ CLI_GENERATE
Definition: cli.h:153

Function Documentation

◆ __ast_cli_register()

int __ast_cli_register ( struct ast_cli_entry e,
struct ast_module mod 
)

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

2453{
2454 struct ast_cli_entry *cur;
2455 int i, lf, ret = -1;
2456
2457 struct ast_cli_args a; /* fake argument */
2458 char **dst = (char **)e->cmda; /* need to cast as the entry is readonly */
2459 char *s;
2460
2462
2463 if (cli_is_registered(e)) {
2464 ast_log(LOG_WARNING, "Command '%s' already registered (the same ast_cli_entry)\n",
2465 S_OR(e->_full_cmd, e->command));
2466 ret = 0; /* report success */
2467 goto done;
2468 }
2469
2470 memset(&a, '\0', sizeof(a));
2471
2472 e->module = module;
2473 /* No module reference needed here, the module called us. */
2474 e->handler(e, CLI_INIT, &a);
2475
2476 /* XXX check that usage and command are filled up */
2477 s = ast_skip_blanks(e->command);
2478 s = e->command = ast_strdup(s);
2479 for (i=0; !ast_strlen_zero(s) && i < AST_MAX_CMD_LEN-1; i++) {
2480 *dst++ = s; /* store string */
2481 s = ast_skip_nonblanks(s);
2482 if (*s == '\0') /* we are done */
2483 break;
2484 *s++ = '\0';
2485 s = ast_skip_blanks(s);
2486 }
2487 *dst++ = NULL;
2488
2489 if (find_cli(e->cmda, 1)) {
2490 ast_log(LOG_WARNING, "Command '%s' already registered (or something close enough)\n",
2491 S_OR(e->_full_cmd, e->command));
2492 goto done;
2493 }
2494 if (set_full_cmd(e)) {
2495 ast_log(LOG_WARNING, "Error registering CLI Command '%s'\n",
2496 S_OR(e->_full_cmd, e->command));
2497 goto done;
2498 }
2499
2500 lf = e->cmdlen;
2502 int len = cur->cmdlen;
2503 if (lf < len)
2504 len = lf;
2505 if (strncasecmp(e->_full_cmd, cur->_full_cmd, len) < 0) {
2507 break;
2508 }
2509 }
2511
2512 if (!cur)
2514 ret = 0; /* success */
2515
2516done:
2518 if (ret) {
2519 ast_free(e->command);
2520 e->command = NULL;
2521 }
2522
2523 return ret;
2524}
#define ast_free(a)
Definition: astmm.h:180
#define ast_strdup(str)
A wrapper for strdup()
Definition: astmm.h:241
#define ast_log
Definition: astobj2.c:42
#define AST_MAX_CMD_LEN
Definition: cli.h:48
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
#define LOG_WARNING
#define AST_RWLIST_TRAVERSE_SAFE_BEGIN
Definition: linkedlists.h:545
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:52
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:151
#define AST_RWLIST_TRAVERSE_SAFE_END
Definition: linkedlists.h:617
#define AST_RWLIST_INSERT_TAIL
Definition: linkedlists.h:741
#define AST_RWLIST_INSERT_BEFORE_CURRENT
Definition: linkedlists.h:610
static struct ast_cli_entry * find_cli(const char *const cmds[], int match_type)
Definition: main/cli.c:2348
static int set_full_cmd(struct ast_cli_entry *e)
Definition: main/cli.c:2067
static int cli_is_registered(struct ast_cli_entry *e)
Definition: main/cli.c:2410
#define NULL
Definition: resample.c:96
char * ast_skip_nonblanks(const char *str)
Gets a pointer to first whitespace character in a string.
Definition: strings.h:204
#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
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
char * ast_skip_blanks(const char *str)
Gets a pointer to the first non-whitespace character in a string.
Definition: strings.h:161
descriptor for a cli entry.
Definition: cli.h:171
char *(* handler)(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
Definition: cli.h:187
int cmdlen
Definition: cli.h:182
struct ast_module * module
Definition: cli.h:180
char * _full_cmd
Definition: cli.h:181
const char *const cmda[AST_MAX_CMD_LEN]
Definition: cli.h:172
char * command
Definition: cli.h:186
int done
Definition: test_amihooks.c:48
static struct test_val a

References ast_cli_entry::_full_cmd, a, ast_free, ast_log, AST_MAX_CMD_LEN, AST_RWLIST_INSERT_BEFORE_CURRENT, AST_RWLIST_INSERT_TAIL, AST_RWLIST_TRAVERSE_SAFE_BEGIN, AST_RWLIST_TRAVERSE_SAFE_END, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_skip_blanks(), ast_skip_nonblanks(), ast_strdup, ast_strlen_zero(), CLI_INIT, cli_is_registered(), ast_cli_entry::cmda, ast_cli_entry::cmdlen, ast_cli_entry::command, done, find_cli(), ast_cli_entry::handler, len(), LOG_WARNING, ast_cli_entry::module, NULL, S_OR, and set_full_cmd().

Referenced by __ast_cli_register_multiple().

◆ __ast_cli_register_multiple()

int __ast_cli_register_multiple ( struct ast_cli_entry e,
int  len,
struct ast_module mod 
)

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

2530{
2531 int i, res = 0;
2532
2533 for (i = 0; i < len; i++) {
2534 res |= __ast_cli_register(e + i, module);
2535 }
2536
2537 return res;
2538}
int __ast_cli_register(struct ast_cli_entry *e, struct ast_module *module)
Definition: main/cli.c:2452

References __ast_cli_register(), and len().

◆ ast_cli()

void ast_cli ( int  fd,
const char *  fmt,
  ... 
)
Examples
app_skel.c.

Definition at line 6 of file clicompat.c.

7{
8}

References ast_carefulwrite(), ast_cli_buf, AST_CLI_INITLEN, AST_DYNSTR_BUILD_FAILED, ast_str_buffer(), ast_str_set_va(), ast_str_strlen(), ast_str_thread_get(), and buf.

Referenced by __iax2_show_peers(), __say_cli_init(), _iax2_show_peers_one(), _stun_show_status(), aeap_cli_show(), agent_handle_logoff_cmd(), agent_handle_show_specific(), agent_show_requested(), alias_show(), aoc_cli_debug_enable(), ari_mkpasswd(), ari_set_debug(), ari_show(), ari_show_apps(), ari_show_user(), ari_show_users(), ast_acl_output(), ast_cli_command_full(), ast_cli_netstats(), ast_console_toggle_mute(), ast_ha_output(), ast_pbx_hangup_handler_headers(), ast_pbx_hangup_handler_show(), ast_sip_cli_traverse_objects(), bridge_show_specific_print_channel(), cc_cli_output_status(), cc_cli_print_monitor_stats(), channel_set_debug(), cli_console_active(), cli_console_answer(), cli_console_autoanswer(), cli_console_dial(), cli_console_flash(), cli_console_hangup(), cli_console_mute(), cli_console_sendtext(), cli_display_named_acl(), cli_display_named_acl_list(), cli_display_parking_global(), cli_display_parking_lot(), cli_display_parking_lot_list(), cli_eprofile_show_all(), cli_fax_set_debug(), cli_fax_show_capabilities(), cli_fax_show_session(), cli_fax_show_sessions(), cli_fax_show_settings(), cli_fax_show_stats(), cli_fax_show_version(), cli_list_available(), cli_list_devices(), cli_list_subscriptions_detail(), cli_list_subscriptions_inout(), cli_match_char_tree(), cli_mute_unmute_helper(), cli_notify(), cli_odbc_read(), cli_odbc_write(), cli_profile_show_all(), cli_qualify(), cli_realtime_destroy(), cli_realtime_load(), cli_realtime_store(), cli_realtime_update(), cli_realtime_update2(), cli_register(), cli_reload_qualify_aor(), cli_reload_qualify_endpoint(), cli_show_channel(), cli_show_channels(), cli_show_endpoint_identifiers(), cli_show_module_options(), cli_show_module_type(), cli_show_module_types(), cli_show_modules(), cli_show_monitors(), cli_show_qualify_aor(), cli_show_qualify_endpoint(), cli_show_settings(), cli_show_subscription_common(), cli_show_subscriptions_detail(), cli_show_subscriptions_inout(), cli_show_tasks(), cli_subsystem_alert_report(), cli_tn_show_all(), cli_tps_ping(), cli_tps_report(), cli_tps_reset_stats(), cli_tps_reset_stats_all(), cli_unregister(), client_config_show_all(), complete_ari_app(), complete_ari_user(), config_object_cli_show(), corosync_show_config(), corosync_show_members(), crypto_show_cli_store(), dahdi_create_channels(), dahdi_destroy_channels(), dahdi_set_dnd(), dahdi_set_hwgain(), dahdi_set_mwi(), dahdi_set_swgain(), dahdi_show_channel(), dahdi_show_channels(), dahdi_show_status(), dahdi_show_version(), display_entry_list(), display_parked_call(), display_parking_lot(), display_results(), display_single_entry(), do_print(), dump_str_and_free(), dundi_do_lookup(), dundi_do_precache(), dundi_do_query(), dundi_flush(), dundi_set_debug(), dundi_show_cache(), dundi_show_entityid(), dundi_show_hints(), dundi_show_mappings(), dundi_show_peer(), dundi_show_peers(), dundi_show_precache(), dundi_show_requests(), dundi_show_trans(), dundi_store_history(), firmware_show_callback(), forward_message_from_mailbox(), gen_events(), geoloc_config_cli_reload(), geoloc_config_list_locations(), geoloc_config_list_profiles(), geoloc_config_show_profiles(), group_show_channels(), handle_bridge_kick_channel(), handle_bridge_show_all(), handle_bridge_show_specific(), handle_bridge_technology_show(), handle_bridge_technology_suspend(), handle_cdr_pgsql_status(), handle_chanlist(), handle_cli_agi_add_cmd(), handle_cli_agi_debug(), handle_cli_agi_dump_html(), handle_cli_agi_show(), handle_cli_check_permissions(), handle_cli_confbridge_kick(), handle_cli_confbridge_list(), handle_cli_confbridge_list_item(), handle_cli_confbridge_lock(), handle_cli_confbridge_show_bridge_profile(), handle_cli_confbridge_show_bridge_profiles(), handle_cli_confbridge_show_menu(), handle_cli_confbridge_show_menus(), handle_cli_confbridge_show_user_profile(), handle_cli_confbridge_show_user_profiles(), handle_cli_confbridge_start_record(), handle_cli_confbridge_stop_record(), handle_cli_confbridge_unlock(), handle_cli_config_list(), handle_cli_core_show_channeltype(), handle_cli_core_show_channeltypes(), handle_cli_core_show_config_mappings(), handle_cli_core_show_file_formats(), handle_cli_core_show_translation(), handle_cli_database_del(), handle_cli_database_deltree(), handle_cli_database_get(), handle_cli_database_put(), handle_cli_database_show(), handle_cli_database_showkey(), handle_cli_debug(), handle_cli_devstate_change(), handle_cli_devstate_list(), handle_cli_dialplan_add_extension(), handle_cli_dialplan_add_ignorepat(), handle_cli_dialplan_add_include(), handle_cli_dialplan_reload(), handle_cli_dialplan_remove_context(), handle_cli_dialplan_remove_extension(), handle_cli_dialplan_remove_ignorepat(), handle_cli_dialplan_remove_include(), handle_cli_dialplan_save(), handle_cli_dynamic_level_test(), handle_cli_file_convert(), handle_cli_iax2_provision(), handle_cli_iax2_prune_realtime(), handle_cli_iax2_set_debug(), handle_cli_iax2_set_debug_jb(), handle_cli_iax2_set_debug_trunk(), handle_cli_iax2_set_mtu(), handle_cli_iax2_show_cache(), handle_cli_iax2_show_callno_limits(), handle_cli_iax2_show_channels(), handle_cli_iax2_show_firmware(), handle_cli_iax2_show_netstats(), handle_cli_iax2_show_peer(), handle_cli_iax2_show_registry(), handle_cli_iax2_show_stats(), handle_cli_iax2_show_threads(), handle_cli_iax2_show_users(), handle_cli_iax2_unregister(), handle_cli_indication_show(), handle_cli_keys_show(), handle_cli_locks_show(), handle_cli_malloc_trim(), handle_cli_mixmonitor(), handle_cli_mobile_cusd(), handle_cli_mobile_rfcomm(), handle_cli_mobile_search(), handle_cli_mobile_show_devices(), handle_cli_moh_show_classes(), handle_cli_moh_show_files(), handle_cli_moh_unregister_class(), handle_cli_odbc_show(), handle_cli_ooh323_set_debug(), handle_cli_ooh323_show_config(), handle_cli_ooh323_show_gk(), handle_cli_ooh323_show_peer(), handle_cli_ooh323_show_peers(), handle_cli_ooh323_show_user(), handle_cli_ooh323_show_users(), handle_cli_performance_test(), handle_cli_presencestate_change(), handle_cli_presencestate_list(), handle_cli_queue_test(), handle_cli_realtime_mysql_cache(), handle_cli_realtime_mysql_status(), handle_cli_realtime_pgsql_cache(), handle_cli_realtime_pgsql_status(), handle_cli_recalc(), handle_cli_refresh(), handle_cli_rtcp_set_debug(), handle_cli_rtcp_set_stats(), handle_cli_rtp_set_debug(), handle_cli_rtp_settings(), handle_cli_sched_bench(), handle_cli_show_config(), handle_cli_show_permissions(), handle_cli_sound_show(), handle_cli_sounds_show(), handle_cli_status(), handle_cli_stun_set_debug(), handle_cli_submit(), handle_cli_test_locales(), handle_cli_transcoder_show(), handle_cli_udptl_set_debug(), handle_cli_ulimit(), handle_cli_wait_fullybooted(), handle_commandmatchesarray(), handle_core_set_debug_channel(), handle_core_show_image_formats(), handle_dahdi_show_cadences(), handle_debug_dialplan(), handle_debug_or_trace(), handle_dump_frames(), handle_eval_function(), handle_exec(), handle_export_primitives(), handle_feature_show(), handle_gml_show(), handle_help(), handle_load(), handle_logger_add_channel(), handle_logger_chanloggroup_filter(), handle_logger_filter_reset(), handle_logger_filter_show(), handle_logger_reload(), handle_logger_remove_channel(), handle_logger_rotate(), handle_logger_set_level(), handle_logger_show_channels(), handle_logger_show_levels(), handle_manager_show_event(), handle_manager_show_events(), handle_manager_show_settings(), handle_mandebug(), handle_minivm_list_templates(), handle_minivm_reload(), handle_minivm_show_settings(), handle_minivm_show_stats(), handle_minivm_show_users(), handle_minivm_show_zones(), handle_modlist(), handle_pjproject_set_log_level(), handle_pjproject_show_buildopts(), handle_pjproject_show_log_level(), handle_pjproject_show_log_mappings(), handle_pjsip_show_version(), handle_queue_add_member(), handle_queue_change_priority_caller(), handle_queue_pause_member(), handle_queue_remove_member(), handle_queue_rule_show(), handle_queue_set_member_penalty(), handle_queue_set_member_ringinuse(), handle_redirect(), handle_refresh(), handle_reload(), handle_remb_set(), handle_restart_when_convenient(), handle_set_chanvar(), handle_set_extenpatternmatchnew(), handle_set_global(), handle_show_application(), handle_show_applications(), handle_show_calendar(), handle_show_calendars(), handle_show_calendars_types(), handle_show_chanvar(), handle_show_dialplan(), handle_show_function(), handle_show_functions(), handle_show_globals(), handle_show_hangup_channel(), handle_show_hint(), handle_show_hints(), handle_show_http(), handle_show_parking_lot_cmd(), handle_show_profile(), handle_show_routes(), handle_show_settings(), handle_show_switches(), handle_show_sysinfo(), handle_show_threads(), handle_show_translation_path(), handle_show_translation_table(), handle_showcalls(), handle_showchan(), handle_showmanager(), handle_showmanagers(), handle_showmancmd(), handle_showmancmds(), handle_showmanconn(), handle_showmaneventq(), handle_skel_show_config(), handle_skel_show_games(), handle_skel_show_levels(), handle_softhangup(), handle_stop_when_convenient(), handle_unload(), handle_unset_extenpatternmatchnew(), handle_version(), handle_voicemail_reload(), handle_voicemail_show_aliases(), handle_voicemail_show_users(), handle_voicemail_show_zones(), help1(), help_workhorse(), iax_show_provisioning(), locals_show(), log_forwarder(), media_cache_handle_create_item(), media_cache_handle_delete_item(), media_cache_handle_refresh_item(), media_cache_handle_show_all(), media_cache_handle_show_item(), media_cache_prnt_summary(), meetme_show_cmd(), modlist_modentry(), move_message_from_mailbox(), orig_app(), orig_exten(), output_tests(), pjsip_disable_logger(), pjsip_enable_logger_all(), pjsip_enable_logger_host(), pjsip_enable_logger_method(), pjsip_set_history(), pjsip_set_logger_pcap(), pjsip_set_logger_verbose(), pjsip_show_history(), print_acl(), print_app_docs(), print_applicationmap(), print_event_instance(), print_featuregroup(), print_featuregroups(), print_file(), print_stats_cb(), print_uptimestr(), prometheus_show_metrics(), prometheus_show_status(), remove_message_from_mailbox(), route_list_cb(), rtcp_do_debug_ip(), rtp_do_debug_ip(), show_codec(), show_codecs(), show_debug_helper(), show_dialplan_helper(), show_dialplan_helper_extension_output(), show_license(), show_mailbox_details(), show_mailbox_snapshot(), show_messages_for_mailbox(), show_sound_info_cb(), show_sounds_cb(), show_users_cb(), show_users_realtime(), show_warranty(), sla_show_stations(), sla_show_trunks(), sorcery_memory_cache_cli_thrash(), sorcery_memory_cache_dump(), sorcery_memory_cache_expire(), sorcery_memory_cache_populate(), sorcery_memory_cache_print_object(), sorcery_memory_cache_show(), sorcery_memory_cache_stale(), spandsp_fax_cli_show_capabilities(), spandsp_fax_cli_show_session(), spandsp_fax_cli_show_stats(), stasis_app_to_cli(), stasis_show_topic(), stasis_show_topics(), status_debug_verbose(), timing_test(), tps_report_taskprocessor_list_helper(), unistim_do_debug(), unistim_show_devices(), unistim_show_info(), unistim_sp(), xmpp_cli_create_collection(), xmpp_cli_create_leafnode(), xmpp_cli_delete_pubsub_node(), xmpp_cli_list_pubsub_nodes(), xmpp_cli_purge_pubsub_nodes(), xmpp_do_set_debug(), xmpp_show_buddies(), and xmpp_show_clients().

◆ ast_cli_allow_at_shutdown()

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

3059{
3060 int res;
3061
3065
3066 return res;
3067}
#define ast_rwlock_wrlock(a)
Definition: lock.h:236
#define ast_rwlock_unlock(a)
Definition: lock.h:234
static ast_rwlock_t shutdown_commands_lock
Definition: main/cli.c:112
static struct @332 shutdown_commands
#define AST_VECTOR_APPEND(vec, elem)
Append an element to a vector, growing the vector if needed.
Definition: vector.h:256

References ast_rwlock_unlock, ast_rwlock_wrlock, AST_VECTOR_APPEND, shutdown_commands, and shutdown_commands_lock.

Referenced by handle_abort_shutdown(), handle_restart_gracefully(), handle_restart_now(), handle_restart_when_convenient(), handle_stop_gracefully(), handle_stop_now(), and handle_stop_when_convenient().

◆ ast_cli_command_full()

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

2972{
2973 const char *args[AST_MAX_ARGS + 1];
2974 struct ast_cli_entry *e = NULL;
2975 int x;
2976 char *duplicate = parse_args(s, &x, args + 1, AST_MAX_ARGS, NULL);
2977 char tmp[AST_MAX_ARGS + 1];
2978 char *retval = CLI_FAILURE;
2979 struct ast_cli_args a = {
2980 .fd = fd, .argc = x, .argv = args+1 };
2981
2982 if (duplicate == NULL)
2983 return RESULT_FAILURE;
2984
2985 if (x < 1) /* We need at least one entry, otherwise ignore */
2986 goto done;
2987
2989 e = find_cli(args + 1, 0);
2990 if (e)
2993 if (e == NULL) {
2994 ast_cli(fd, "No such command '%s' (type 'core show help %s' for other possible commands)\n", s, find_best(args + 1));
2995 goto done;
2996 }
2997
2999 ast_cli(fd, "Command '%s' cannot be run during shutdown\n", s);
3000 goto done;
3001 }
3002
3003 ast_join(tmp, sizeof(tmp), args + 1);
3004 /* Check if the user has rights to run this command. */
3005 if (!cli_has_permissions(uid, gid, tmp)) {
3006 ast_cli(fd, "You don't have permissions to run '%s' command\n", tmp);
3007 goto done;
3008 }
3009
3010 /*
3011 * Within the handler, argv[-1] contains a pointer to the ast_cli_entry.
3012 * Remember that the array returned by parse_args is NULL-terminated.
3013 */
3014 args[0] = (char *)e;
3015
3016 /* If the command is in a module it must be running. */
3017 if (!e->module || ast_module_running_ref(e->module)) {
3018 retval = e->handler(e, CLI_HANDLER, &a);
3020 }
3021
3022 if (retval == CLI_SHOWUSAGE) {
3023 ast_cli(fd, "%s", S_OR(e->usage, "Invalid usage, but no usage information available.\n"));
3024 } else if (retval == CLI_FAILURE) {
3025 ast_cli(fd, "Command '%s' failed.\n", s);
3026 }
3027
3028done:
3029 if (e) {
3031 }
3032 ast_free(duplicate);
3034}
int ast_shutting_down(void)
Definition: asterisk.c:1872
static int tmp()
Definition: bt_open.c:389
#define CLI_SHOWUSAGE
Definition: cli.h:45
#define CLI_SUCCESS
Definition: cli.h:44
#define AST_MAX_ARGS
Definition: cli.h:50
#define RESULT_SUCCESS
Definition: cli.h:40
#define CLI_FAILURE
Definition: cli.h:46
#define RESULT_FAILURE
Definition: cli.h:42
static ENTRY retval
Definition: hsearch.c:50
#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
static char * find_best(const char *argv[])
Definition: main/cli.c:2392
void ast_cli(int fd, const char *fmt,...)
Definition: main/cli.c:118
static int allowed_on_shutdown(struct ast_cli_entry *e)
Definition: main/cli.c:2954
static int cli_has_permissions(int uid, int gid, const char *command)
Definition: main/cli.c:183
static char * parse_args(const char *s, int *argc, const char *argv[], int max, int *trailingwhitespace)
Definition: main/cli.c:2630
#define ast_module_unref(mod)
Release a reference to the module.
Definition: module.h:469
#define ast_module_running_ref(mod)
Hold a reference to the module if it is running.
Definition: module.h:455
#define ast_join(s, len, w)
Join an array of strings into a single string.
Definition: strings.h:520
const int fd
Definition: cli.h:159
int inuse
Definition: cli.h:179
const char * usage
Definition: cli.h:177
const char * args

References a, allowed_on_shutdown(), args, ast_atomic_fetchadd_int(), ast_cli(), ast_free, ast_join, AST_MAX_ARGS, ast_module_running_ref, ast_module_unref, AST_RWLIST_RDLOCK, AST_RWLIST_UNLOCK, ast_shutting_down(), CLI_FAILURE, CLI_HANDLER, cli_has_permissions(), CLI_SHOWUSAGE, CLI_SUCCESS, done, ast_cli_args::fd, find_best(), find_cli(), ast_cli_entry::handler, ast_cli_entry::inuse, ast_cli_entry::module, NULL, parse_args(), RESULT_FAILURE, RESULT_SUCCESS, retval, S_OR, tmp(), and ast_cli_entry::usage.

Referenced by ast_cli_command_multiple_full().

◆ ast_cli_command_multiple_full()

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

3037{
3038 char cmd[512];
3039 int x, y = 0, count = 0;
3040
3041 for (x = 0; x < size; x++) {
3042 cmd[y] = s[x];
3043 y++;
3044 if (s[x] == '\0') {
3045 ast_cli_command_full(uid, gid, fd, cmd);
3046 y = 0;
3047 count++;
3048 }
3049 }
3050 return count;
3051}
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:2971

References ast_cli_command_full(), and ast_cli_args::fd.

Referenced by netconsole().

◆ ast_cli_complete()

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

char *my_generate(const char *line, const char *word, int pos, int n)
{
static const char * const choices[] = { "one", "two", "three", NULL };
if (pos == 2)
return ast_cli_complete(word, choices, n);
else
return NULL;
}
char * ast_cli_complete(const char *word, const char *const choices[], int pos)
Definition: main/cli.c:1843
short word

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

1844{
1845 int i, which = 0, len;
1846 len = ast_strlen_zero(word) ? 0 : strlen(word);
1847
1848 for (i = 0; choices[i]; i++) {
1849 if ((!len || !strncasecmp(word, choices[i], len)) && ++which > state) {
1850 if (state != -1) {
1851 return ast_strdup(choices[i]);
1852 }
1853
1854 if (ast_cli_completion_add(ast_strdup(choices[i]))) {
1855 return NULL;
1856 }
1857 }
1858 }
1859 return NULL;
1860}
int ast_cli_completion_add(char *value)
Add a result to a request for completion options.
Definition: main/cli.c:2758

References ast_cli_completion_add(), ast_strdup, ast_strlen_zero(), len(), and NULL.

Referenced by cli_odbc_read(), cli_odbc_write(), handle_bridge_kick_channel(), handle_cli_core_show_translation(), handle_cli_devstate_change(), handle_cli_iax2_prune_realtime(), handle_cli_presencestate_change(), handle_core_set_debug_channel(), handle_orig(), handle_showcalls(), handle_softhangup(), pjsip_set_logger(), and sorcery_memory_cache_stale().

◆ ast_cli_completion_add()

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

2759{
2760 return cli_completion_vector_add(ast_threadstorage_get_ptr(&completion_storage), value);
2761}
static int cli_completion_vector_add(struct ast_vector_string *vec, char *value)
Definition: main/cli.c:2737
int value
Definition: syslog.c:37
void * ast_threadstorage_get_ptr(struct ast_threadstorage *ts)
Retrieve a raw pointer from threadstorage.

References ast_threadstorage_get_ptr(), cli_completion_vector_add(), and value.

Referenced by aeap_tab_complete_name(), ast_cli_complete(), ast_complete_applications(), ast_complete_channels(), category_complete(), cli_complete_endpoint(), cli_complete_show(), cli_complete_uri(), complete_bridge_live_search(), complete_bridge_participant(), complete_bridge_technology(), complete_channeltypes(), complete_config_module(), complete_config_option(), complete_config_type(), complete_core_id(), complete_country(), complete_indications(), complete_show_sorcery_object(), complete_trans_path_choice(), config_object_tab_complete_name(), handle_cli_config_reload(), handle_cli_sound_show(), handle_debug_category(), handle_manager_show_event(), handle_show_named_acl_cmd(), handle_showmanager(), handle_showmancmd(), module_load_helper_on_file(), topic_complete_name(), and tps_taskprocessor_tab_complete().

◆ ast_cli_completion_matches()

char ** ast_cli_completion_matches ( const char *  text,
const char *  word 
)

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

2703{
2705 char **match_list;
2706
2707 if (!vec) {
2708 return NULL;
2709 }
2710
2711 if (AST_VECTOR_APPEND(vec, NULL)) {
2712 /* We failed to NULL terminate the elements */
2715
2716 return NULL;
2717 }
2718
2719 match_list = AST_VECTOR_STEAL_ELEMENTS(vec);
2721
2722 return match_list;
2723}
char * text
Definition: app_queue.c:1639
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:2763
String vector definitions.
Definition: vector.h:55
#define AST_VECTOR_STEAL_ELEMENTS(vec)
Steal the elements from a vector and reinitialize.
Definition: vector.h:140
#define AST_VECTOR_PTR_FREE(vec)
Deallocates this vector pointer.
Definition: vector.h:189
#define AST_VECTOR_CALLBACK_VOID(vec, callback,...)
Execute a callback on every element in a vector disregarding callback return.
Definition: vector.h:862

References ast_cli_completion_vector(), ast_free, AST_VECTOR_APPEND, AST_VECTOR_CALLBACK_VOID, AST_VECTOR_PTR_FREE, AST_VECTOR_STEAL_ELEMENTS, NULL, and text.

Referenced by handle_commandmatchesarray().

◆ ast_cli_completion_vector()

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

2764{
2765 char *retstr, *prevstr;
2766 size_t max_equal;
2767 size_t which = 0;
2768 struct ast_vector_string *vec = ast_calloc(1, sizeof(*vec));
2769
2770 /* Recursion into this function is a coding error. */
2771 ast_assert(!ast_threadstorage_get_ptr(&completion_storage));
2772
2773 if (!vec) {
2774 return NULL;
2775 }
2776
2777 if (ast_threadstorage_set_ptr(&completion_storage, vec)) {
2778 ast_log(LOG_ERROR, "Failed to initialize threadstorage for completion.\n");
2779 ast_free(vec);
2780
2781 return NULL;
2782 }
2783
2784 while ((retstr = ast_cli_generator(text, word, which)) != NULL) {
2785 if (cli_completion_vector_add(vec, retstr)) {
2786 ast_threadstorage_set_ptr(&completion_storage, NULL);
2787
2788 goto vector_cleanup;
2789 }
2790
2791 ++which;
2792 }
2793
2794 ast_threadstorage_set_ptr(&completion_storage, NULL);
2795
2796 if (!AST_VECTOR_SIZE(vec)) {
2798
2799 return NULL;
2800 }
2801
2802 prevstr = AST_VECTOR_GET(vec, 0);
2803 max_equal = strlen(prevstr);
2804 which = 1;
2805
2806 /* Find the longest substring that is common to all results
2807 * (it is a candidate for completion), and store a copy in entry 0.
2808 */
2809 while (which < AST_VECTOR_SIZE(vec)) {
2810 size_t i = 0;
2811
2812 retstr = AST_VECTOR_GET(vec, which);
2813 /* Check for and remove duplicate strings. */
2814 if (!strcasecmp(prevstr, retstr)) {
2815 AST_VECTOR_REMOVE(vec, which, 1);
2816 ast_free(retstr);
2817
2818 continue;
2819 }
2820
2821 while (i < max_equal && toupper(prevstr[i]) == toupper(retstr[i])) {
2822 i++;
2823 }
2824
2825 max_equal = i;
2826 prevstr = retstr;
2827 ++which;
2828 }
2829
2830 /* Insert longest match to position 0. */
2831 retstr = ast_strndup(AST_VECTOR_GET(vec, 0), max_equal);
2832 if (!retstr || AST_VECTOR_INSERT_AT(vec, 0, retstr)) {
2833 ast_free(retstr);
2834
2835 goto vector_cleanup;
2836 }
2837
2838 return vec;
2839
2840vector_cleanup:
2843
2844 return NULL;
2845}
#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
#define LOG_ERROR
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:2949
int ast_threadstorage_set_ptr(struct ast_threadstorage *ts, void *ptr)
Set a raw pointer from threadstorage.
#define ast_assert(a)
Definition: utils.h:739
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition: vector.h:609
#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
#define AST_VECTOR_REMOVE(vec, idx, preserve_ordered)
Remove an element from a vector by index.
Definition: vector.h:412
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition: vector.h:680

References ast_assert, ast_calloc, ast_cli_generator(), ast_free, ast_log, 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, AST_VECTOR_SIZE, cli_completion_vector_add(), LOG_ERROR, NULL, and text.

Referenced by ast_cli_completion_matches(), AST_TEST_DEFINE(), and cli_complete().

◆ ast_cli_generator()

char * ast_cli_generator ( const char *  text,
const char *  word,
int  state 
)

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

2950{
2951 return __ast_cli_generator(text, word, state, 1);
2952}
static char * __ast_cli_generator(const char *text, const char *word, int state, int lock)
Definition: main/cli.c:2861

References __ast_cli_generator(), and text.

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

◆ ast_cli_print_timestr_fromseconds()

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

3054{
3055 print_uptimestr(fd, ast_tv(seconds, 0), prefix, 0);
3056}
static char prefix[MAX_PREFIX]
Definition: http.c:144
static void print_uptimestr(int fd, struct timeval timeval, const char *prefix, int printsec)
Definition: main/cli.c:908
struct timeval ast_tv(ast_time_t sec, ast_suseconds_t usec)
Returns a timeval from sec, usec.
Definition: time.h:235

References ast_tv(), ast_cli_args::fd, prefix, and print_uptimestr().

Referenced by handle_cdr_pgsql_status(), handle_cli_realtime_pgsql_status(), and realtime_ldap_status().

◆ ast_cli_unregister()

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

2430{
2431 if (e->inuse) {
2432 ast_log(LOG_WARNING, "Can't remove command that is in use\n");
2433 } else {
2435 AST_RWLIST_REMOVE(&helpers, e, list);
2438 ast_free(e->_full_cmd);
2439 e->_full_cmd = NULL;
2440 if (e->handler) {
2441 /* this is a new-style entry. Reset fields and free memory. */
2442 char *cmda = (char *) e->cmda;
2443 memset(cmda, '\0', sizeof(e->cmda));
2444 ast_free(e->command);
2445 e->command = NULL;
2446 e->usage = NULL;
2447 }
2448 }
2449 return 0;
2450}
#define AST_RWLIST_REMOVE
Definition: linkedlists.h:885
static void remove_shutdown_command(struct ast_cli_entry *e)
Definition: main/cli.c:2422

References ast_cli_entry::_full_cmd, ast_free, ast_log, AST_RWLIST_REMOVE, AST_RWLIST_UNLOCK, AST_RWLIST_WRLOCK, ast_cli_entry::cmda, ast_cli_entry::command, ast_cli_entry::handler, ast_cli_entry::inuse, ast_cli_entry::list, LOG_WARNING, NULL, remove_shutdown_command(), and ast_cli_entry::usage.

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

◆ ast_cli_unregister_multiple()

int ast_cli_unregister_multiple ( struct ast_cli_entry e,
int  len 
)

◆ ast_complete_channels()

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

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

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

Referenced by handle_cli_agi_add_cmd(), handle_cli_mixmonitor(), handle_core_set_debug_channel(), handle_dump_frames(), handle_redirect(), handle_remb_set(), handle_set_chanvar(), handle_show_chanvar(), handle_show_hangup_channel(), handle_showchan(), and handle_softhangup().