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.

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

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

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

char** ast_cli_completion_matches char *  ,
char * 
 

Definition at line 1018 of file cli.c.

References ast_cli_generator(), malloc, and realloc.

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

char* ast_cli_generator char *  ,
char *  ,
int 
 

Readline madness.

Definition at line 1129 of file cli.c.

Referenced by ast_cli_completion_matches(), and ast_cli_generatornummatches().

01130 {
01131    return __ast_cli_generator(text, word, state, 1);
01132 }

int ast_cli_generatornummatches char *  ,
char * 
 

Definition at line 1002 of file cli.c.

References ast_cli_generator().

01003 {
01004    int matches = 0, i = 0;
01005    char *buf = NULL, *oldbuf = NULL;
01006 
01007    while ( (buf = ast_cli_generator(text, word, i)) ) {
01008       if (++i > 1 && strcmp(buf,oldbuf) == 0)  {
01009             continue;
01010       }
01011       oldbuf = buf;
01012       matches++;
01013    }
01014 
01015    return matches;
01016 }

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 Wed Aug 10 11:36:33 2005 for Asterisk by  doxygen 1.4.4