Main Page | Alphabetical List | Data Structures | File List | Data Fields | Globals

cli.h File Reference

#include <stdarg.h>

Include dependency graph for cli.h:

Include dependency graph

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

Included by dependency graph

Go to the source code of this file.

Data Structures

struct  ast_cli_entry
 A command line entry */. More...


Defines

#define RESULT_SUCCESS   0
#define RESULT_SHOWUSAGE   1
#define RESULT_FAILURE   2
#define AST_MAX_CMD_LEN   16
#define AST_MAX_ARGS   64
#define AST_CLI_COMPLETE_EOF   "_EOF_"

Functions

void ast_cli (int fd, char *fmt,...) __attribute__((format(printf
int ast_cli_command (int fd, char *s)
 Interprets a command.

int ast_cli_register (struct ast_cli_entry *e)
 Registers a command.

int ast_cli_unregister (struct ast_cli_entry *e)
 Unregisters a command.

char * ast_cli_generator (char *, char *, int)
 Readline madness.

int ast_cli_generatornummatches (char *, char *)
char ** ast_cli_completion_matches (char *, char *)


Define Documentation

#define AST_CLI_COMPLETE_EOF   "_EOF_"
 

Definition at line 34 of file cli.h.

#define AST_MAX_ARGS   64
 

Definition at line 32 of file cli.h.

Referenced by ast_cli_command().

#define AST_MAX_CMD_LEN   16
 

Definition at line 30 of file cli.h.

#define RESULT_FAILURE   2
 

Definition at line 28 of file cli.h.

#define RESULT_SHOWUSAGE   1
 

Definition at line 27 of file cli.h.

Referenced by ast_cli_command().

#define RESULT_SUCCESS   0
 

Definition at line 26 of file cli.h.


Function Documentation

void ast_cli int  fd,
char *  fmt,
  ...
 

Referenced by ast_cli_command(), astman_send_error(), astman_send_response(), main(), and manager_event().

int ast_cli_command int  fd,
char *  s
 

Interprets a command.

Interpret a command s, sending output to fd Returns 0 on succes, -1 on failure

Definition at line 1019 of file cli.c.

References ast_cli(), ast_log(), AST_MAX_ARGS, ast_mutex_lock, ast_mutex_unlock, free, LOG_WARNING, RESULT_SHOWUSAGE, and s.

01020 { 01021 char *argv[AST_MAX_ARGS]; 01022 struct ast_cli_entry *e; 01023 int x; 01024 char *dup; 01025 x = AST_MAX_ARGS; 01026 if ((dup = parse_args(s, &x, argv))) { 01027 /* We need at least one entry, or ignore */ 01028 if (x > 0) { 01029 ast_mutex_lock(&clilock); 01030 e = find_cli(argv, 0); 01031 if (e) 01032 e->inuse++; 01033 ast_mutex_unlock(&clilock); 01034 if (e) { 01035 switch(e->handler(fd, x, argv)) { 01036 case RESULT_SHOWUSAGE: 01037 ast_cli(fd, e->usage); 01038 break; 01039 } 01040 } else 01041 ast_cli(fd, "No such command '%s' (type 'help' for help)\n", find_best(argv)); 01042 if (e) { 01043 ast_mutex_lock(&clilock); 01044 e->inuse--; 01045 ast_mutex_unlock(&clilock); 01046 } 01047 } 01048 free(dup); 01049 } else { 01050 ast_log(LOG_WARNING, "Out of memory\n"); 01051 return -1; 01052 } 01053 return 0; 01054 }

char** ast_cli_completion_matches char *  ,
char * 
 

Definition at line 903 of file cli.c.

References ast_cli_generator(), malloc, and realloc.

00904 { 00905 char **match_list = NULL, *retstr, *prevstr; 00906 size_t match_list_len, max_equal, which, i; 00907 int matches = 0; 00908 00909 match_list_len = 1; 00910 while ((retstr = ast_cli_generator(text, word, matches)) != NULL) { 00911 if (matches + 1 >= match_list_len) { 00912 match_list_len <<= 1; 00913 match_list = realloc(match_list, match_list_len * sizeof(char *)); 00914 } 00915 match_list[++matches] = retstr; 00916 } 00917 00918 if (!match_list) 00919 return (char **) NULL; 00920 00921 which = 2; 00922 prevstr = match_list[1]; 00923 max_equal = strlen(prevstr); 00924 for (; which <= matches; which++) { 00925 for (i = 0; i < max_equal && prevstr[i] == match_list[which][i]; i++) 00926 continue; 00927 max_equal = i; 00928 } 00929 00930 retstr = malloc(max_equal + 1); 00931 (void) strncpy(retstr, match_list[1], max_equal); 00932 retstr[max_equal] = '\0'; 00933 match_list[0] = retstr; 00934 00935 if (matches + 1 >= match_list_len) 00936 match_list = realloc(match_list, (match_list_len + 1) * sizeof(char *)); 00937 match_list[matches + 1] = (char *) NULL; 00938 00939 return (match_list); 00940 }

char* ast_cli_generator char *  ,
char *  ,
int 
 

Readline madness.

Definition at line 1014 of file cli.c.

Referenced by ast_cli_completion_matches(), and ast_cli_generatornummatches().

01015 { 01016 return __ast_cli_generator(text, word, state, 1); 01017 }

int ast_cli_generatornummatches char *  ,
char * 
 

Definition at line 886 of file cli.c.

References ast_cli_generator().

00887 { 00888 int matches = 0, i = 0; 00889 char *buf, *oldbuf = NULL; 00890 00891 00892 while ( (buf = ast_cli_generator(text, word, i)) ) { 00893 if (++i > 1 && strcmp(buf,oldbuf) == 0) { 00894 continue; 00895 } 00896 oldbuf = buf; 00897 matches++; 00898 } 00899 00900 return matches; 00901 }

int ast_cli_register struct ast_cli_entry e  ) 
 

Registers a command.

Parameters:
fd File descriptor that I/O is done to
s string given at prompt Register your own command Returns 0 on success, -1 on failure

Definition at line 709 of file cli.c.

References ast_log(), ast_mutex_lock, ast_mutex_unlock, ast_cli_entry::cmda, helpers, LOG_WARNING, and ast_cli_entry::next.

Referenced by ast_image_init(), ast_register_translator(), astdb_init(), init_framer(), init_logger(), init_manager(), load_pbx(), and main().

00710 { 00711 struct ast_cli_entry *cur, *l=NULL; 00712 char fulle[80] ="", fulltst[80] =""; 00713 static int len; 00714 ast_mutex_lock(&clilock); 00715 join2(fulle, sizeof(fulle), e->cmda); 00716 if (find_cli(e->cmda, -1)) { 00717 ast_mutex_unlock(&clilock); 00718 ast_log(LOG_WARNING, "Command '%s' already registered (or something close enough)\n", fulle); 00719 return -1; 00720 } 00721 cur = helpers; 00722 while(cur) { 00723 join2(fulltst, sizeof(fulltst), cur->cmda); 00724 len = strlen(fulltst); 00725 if (strlen(fulle) < len) 00726 len = strlen(fulle); 00727 if (strncasecmp(fulle, fulltst, len) < 0) { 00728 if (l) { 00729 e->next = l->next; 00730 l->next = e; 00731 } else { 00732 e->next = helpers; 00733 helpers = e; 00734 } 00735 break; 00736 } 00737 l = cur; 00738 cur = cur->next; 00739 } 00740 if (!cur) { 00741 if (l) 00742 l->next = e; 00743 else 00744 helpers = e; 00745 e->next = NULL; 00746 } 00747 ast_mutex_unlock(&clilock); 00748 return 0; 00749 }

int ast_cli_unregister struct ast_cli_entry e  ) 
 

Unregisters a command.

Parameters:
e which cli entry to unregister Unregister your own command. You must pass a completed ast_cli_entry structur Returns 0 on success, -1 on failure

Definition at line 683 of file cli.c.

References ast_log(), ast_mutex_lock, ast_mutex_unlock, helpers, ast_cli_entry::inuse, LOG_WARNING, and ast_cli_entry::next.

00684 { 00685 struct ast_cli_entry *cur, *l=NULL; 00686 ast_mutex_lock(&clilock); 00687 cur = helpers; 00688 while(cur) { 00689 if (e == cur) { 00690 if (e->inuse) { 00691 ast_log(LOG_WARNING, "Can't remove command that is in use\n"); 00692 } else { 00693 /* Rewrite */ 00694 if (l) 00695 l->next = e->next; 00696 else 00697 helpers = e->next; 00698 e->next = NULL; 00699 break; 00700 } 00701 } 00702 l = cur; 00703 cur = cur->next; 00704 } 00705 ast_mutex_unlock(&clilock); 00706 return 0; 00707 }


Generated on Sat Jun 12 16:41:11 2004 for Asterisk by doxygen 1.3.7