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

cli.h File Reference

#include <stdarg.h>

Go to the source code of this file.

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

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 1139 of file cli.c.

References ast_cli(), ast_log(), AST_MAX_ARGS, ast_mutex_lock, ast_mutex_unlock, free, ast_cli_entry::handler, ast_cli_entry::inuse, LOG_WARNING, RESULT_SHOWUSAGE, and ast_cli_entry::usage.

01140 {
01141    char *argv[AST_MAX_ARGS];
01142    struct ast_cli_entry *e;
01143    int x;
01144    char *dup;
01145    x = AST_MAX_ARGS;
01146    if ((dup = parse_args(s, &x, argv))) {
01147       /* We need at least one entry, or ignore */
01148       if (x > 0) {
01149          ast_mutex_lock(&clilock);
01150          e = find_cli(argv, 0);
01151          if (e)
01152             e->inuse++;
01153          ast_mutex_unlock(&clilock);
01154          if (e) {
01155             switch(e->handler(fd, x, argv)) {
01156             case RESULT_SHOWUSAGE:
01157                ast_cli(fd, e->usage);
01158                break;
01159             }
01160          } else 
01161             ast_cli(fd, "No such command '%s' (type 'help' for help)\n", find_best(argv));
01162          if (e) {
01163             ast_mutex_lock(&clilock);
01164             e->inuse--;
01165             ast_mutex_unlock(&clilock);
01166          }
01167       }
01168       free(dup);
01169    } else {
01170       ast_log(LOG_WARNING, "Out of memory\n");  
01171       return -1;
01172    }
01173    return 0;
01174 }

char** ast_cli_completion_matches char *  ,
char * 
 

Definition at line 1023 of file cli.c.

References ast_cli_generator(), malloc, and realloc.

01024 {
01025    char **match_list = NULL, *retstr, *prevstr;
01026    size_t match_list_len, max_equal, which, i;
01027    int matches = 0;
01028 
01029    match_list_len = 1;
01030    while ((retstr = ast_cli_generator(text, word, matches)) != NULL) {
01031       if (matches + 1 >= match_list_len) {
01032          match_list_len <<= 1;
01033          match_list = realloc(match_list, match_list_len * sizeof(char *));
01034       }
01035       match_list[++matches] = retstr;
01036    }
01037 
01038    if (!match_list)
01039       return (char **) NULL;
01040 
01041    which = 2;
01042    prevstr = match_list[1];
01043    max_equal = strlen(prevstr);
01044    for (; which <= matches; which++) {
01045       for (i = 0; i < max_equal && toupper(prevstr[i]) == toupper(match_list[which][i]); i++)
01046          continue;
01047       max_equal = i;
01048    }
01049 
01050    retstr = malloc(max_equal + 1);
01051    (void) strncpy(retstr, match_list[1], max_equal);
01052    retstr[max_equal] = '\0';
01053    match_list[0] = retstr;
01054 
01055    if (matches + 1 >= match_list_len)
01056       match_list = realloc(match_list, (match_list_len + 1) * sizeof(char *));
01057    match_list[matches + 1] = (char *) NULL;
01058 
01059    return (match_list);
01060 }

char* ast_cli_generator char *  ,
char *  ,
int 
 

Readline madness.

Definition at line 1134 of file cli.c.

Referenced by ast_cli_completion_matches(), and ast_cli_generatornummatches().

01135 {
01136    return __ast_cli_generator(text, word, state, 1);
01137 }

int ast_cli_generatornummatches char *  ,
char * 
 

Definition at line 1007 of file cli.c.

References ast_cli_generator().

01008 {
01009    int matches = 0, i = 0;
01010    char *buf = NULL, *oldbuf = NULL;
01011 
01012    while ( (buf = ast_cli_generator(text, word, i)) ) {
01013       if (++i > 1 && strcmp(buf,oldbuf) == 0)  {
01014             continue;
01015       }
01016       oldbuf = buf;
01017       matches++;
01018    }
01019 
01020    return matches;
01021 }

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 830 of file cli.c.

References ast_log(), ast_mutex_lock, ast_mutex_unlock, ast_cli_entry::cmda, 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().

00831 {
00832    struct ast_cli_entry *cur, *l=NULL;
00833    char fulle[80] ="", fulltst[80] ="";
00834    static int len;
00835    ast_mutex_lock(&clilock);
00836    join2(fulle, sizeof(fulle), e->cmda);
00837    if (find_cli(e->cmda, -1)) {
00838       ast_mutex_unlock(&clilock);
00839       ast_log(LOG_WARNING, "Command '%s' already registered (or something close enough)\n", fulle);
00840       return -1;
00841    }
00842    cur = helpers;
00843    while(cur) {
00844       join2(fulltst, sizeof(fulltst), cur->cmda);
00845       len = strlen(fulltst);
00846       if (strlen(fulle) < len)
00847          len = strlen(fulle);
00848       if (strncasecmp(fulle, fulltst, len) < 0) {
00849          if (l) {
00850             e->next = l->next;
00851             l->next = e;
00852          } else {
00853             e->next = helpers;
00854             helpers = e;
00855          }
00856          break;
00857       }
00858       l = cur;
00859       cur = cur->next;
00860    }
00861    if (!cur) {
00862       if (l)
00863          l->next = e;
00864       else
00865          helpers = e;
00866       e->next = NULL;
00867    }
00868    ast_mutex_unlock(&clilock);
00869    return 0;
00870 }

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 804 of file cli.c.

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

00805 {
00806    struct ast_cli_entry *cur, *l=NULL;
00807    ast_mutex_lock(&clilock);
00808    cur = helpers;
00809    while(cur) {
00810       if (e == cur) {
00811          if (e->inuse) {
00812             ast_log(LOG_WARNING, "Can't remove command that is in use\n");
00813          } else {
00814             /* Rewrite */
00815             if (l)
00816                l->next = e->next;
00817             else
00818                helpers = e->next;
00819             e->next = NULL;
00820             break;
00821          }
00822       }
00823       l = cur;
00824       cur = cur->next;
00825    }
00826    ast_mutex_unlock(&clilock);
00827    return 0;
00828 }


Generated on Sat Nov 25 19:10:01 2006 for Asterisk by  doxygen 1.4.2