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

cli.h File Reference

#include <stdarg.h>

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

01062 { 01063 char *argv[AST_MAX_ARGS]; 01064 struct ast_cli_entry *e; 01065 int x; 01066 char *dup; 01067 x = AST_MAX_ARGS; 01068 if ((dup = parse_args(s, &x, argv))) { 01069 /* We need at least one entry, or ignore */ 01070 if (x > 0) { 01071 ast_mutex_lock(&clilock); 01072 e = find_cli(argv, 0); 01073 if (e) 01074 e->inuse++; 01075 ast_mutex_unlock(&clilock); 01076 if (e) { 01077 switch(e->handler(fd, x, argv)) { 01078 case RESULT_SHOWUSAGE: 01079 ast_cli(fd, e->usage); 01080 break; 01081 } 01082 } else 01083 ast_cli(fd, "No such command '%s' (type 'help' for help)\n", find_best(argv)); 01084 if (e) { 01085 ast_mutex_lock(&clilock); 01086 e->inuse--; 01087 ast_mutex_unlock(&clilock); 01088 } 01089 } 01090 free(dup); 01091 } else { 01092 ast_log(LOG_WARNING, "Out of memory\n"); 01093 return -1; 01094 } 01095 return 0; 01096 }

char** ast_cli_completion_matches char *  ,
char * 
 

Definition at line 945 of file cli.c.

References ast_cli_generator(), malloc, and realloc.

00946 { 00947 char **match_list = NULL, *retstr, *prevstr; 00948 size_t match_list_len, max_equal, which, i; 00949 int matches = 0; 00950 00951 match_list_len = 1; 00952 while ((retstr = ast_cli_generator(text, word, matches)) != NULL) { 00953 if (matches + 1 >= match_list_len) { 00954 match_list_len <<= 1; 00955 match_list = realloc(match_list, match_list_len * sizeof(char *)); 00956 } 00957 match_list[++matches] = retstr; 00958 } 00959 00960 if (!match_list) 00961 return (char **) NULL; 00962 00963 which = 2; 00964 prevstr = match_list[1]; 00965 max_equal = strlen(prevstr); 00966 for (; which <= matches; which++) { 00967 for (i = 0; i < max_equal && prevstr[i] == match_list[which][i]; i++) 00968 continue; 00969 max_equal = i; 00970 } 00971 00972 retstr = malloc(max_equal + 1); 00973 (void) strncpy(retstr, match_list[1], max_equal); 00974 retstr[max_equal] = '\0'; 00975 match_list[0] = retstr; 00976 00977 if (matches + 1 >= match_list_len) 00978 match_list = realloc(match_list, (match_list_len + 1) * sizeof(char *)); 00979 match_list[matches + 1] = (char *) NULL; 00980 00981 return (match_list); 00982 }

char* ast_cli_generator char *  ,
char *  ,
int 
 

Readline madness.

Definition at line 1056 of file cli.c.

Referenced by ast_cli_completion_matches(), and ast_cli_generatornummatches().

01057 { 01058 return __ast_cli_generator(text, word, state, 1); 01059 }

int ast_cli_generatornummatches char *  ,
char * 
 

Definition at line 928 of file cli.c.

References ast_cli_generator().

00929 { 00930 int matches = 0, i = 0; 00931 char *buf, *oldbuf = NULL; 00932 00933 00934 while ( (buf = ast_cli_generator(text, word, i)) ) { 00935 if (++i > 1 && strcmp(buf,oldbuf) == 0) { 00936 continue; 00937 } 00938 oldbuf = buf; 00939 matches++; 00940 } 00941 00942 return matches; 00943 }

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 751 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_file_init(), ast_image_init(), ast_register_translator(), astdb_init(), init_framer(), init_logger(), init_manager(), load_pbx(), main(), and register_config_cli().

00752 { 00753 struct ast_cli_entry *cur, *l=NULL; 00754 char fulle[80] ="", fulltst[80] =""; 00755 static int len; 00756 ast_mutex_lock(&clilock); 00757 join2(fulle, sizeof(fulle), e->cmda); 00758 if (find_cli(e->cmda, -1)) { 00759 ast_mutex_unlock(&clilock); 00760 ast_log(LOG_WARNING, "Command '%s' already registered (or something close enough)\n", fulle); 00761 return -1; 00762 } 00763 cur = helpers; 00764 while(cur) { 00765 join2(fulltst, sizeof(fulltst), cur->cmda); 00766 len = strlen(fulltst); 00767 if (strlen(fulle) < len) 00768 len = strlen(fulle); 00769 if (strncasecmp(fulle, fulltst, len) < 0) { 00770 if (l) { 00771 e->next = l->next; 00772 l->next = e; 00773 } else { 00774 e->next = helpers; 00775 helpers = e; 00776 } 00777 break; 00778 } 00779 l = cur; 00780 cur = cur->next; 00781 } 00782 if (!cur) { 00783 if (l) 00784 l->next = e; 00785 else 00786 helpers = e; 00787 e->next = NULL; 00788 } 00789 ast_mutex_unlock(&clilock); 00790 return 0; 00791 }

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

00726 { 00727 struct ast_cli_entry *cur, *l=NULL; 00728 ast_mutex_lock(&clilock); 00729 cur = helpers; 00730 while(cur) { 00731 if (e == cur) { 00732 if (e->inuse) { 00733 ast_log(LOG_WARNING, "Can't remove command that is in use\n"); 00734 } else { 00735 /* Rewrite */ 00736 if (l) 00737 l->next = e->next; 00738 else 00739 helpers = e->next; 00740 e->next = NULL; 00741 break; 00742 } 00743 } 00744 l = cur; 00745 cur = cur->next; 00746 } 00747 ast_mutex_unlock(&clilock); 00748 return 0; 00749 }


Generated on Tue Aug 17 16:13:55 2004 for Asterisk by doxygen 1.3.8