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

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

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

00992 {
00993    char *argv[AST_MAX_ARGS];
00994    struct ast_cli_entry *e;
00995    int x;
00996    char *dup;
00997    x = AST_MAX_ARGS;
00998    if ((dup = parse_args(s, &x, argv))) {
00999       /* We need at least one entry, or ignore */
01000       if (x > 0) {
01001          ast_mutex_lock(&clilock);
01002          e = find_cli(argv, 0);
01003          if (e)
01004             e->inuse++;
01005          ast_mutex_unlock(&clilock);
01006          if (e) {
01007             switch(e->handler(fd, x, argv)) {
01008             case RESULT_SHOWUSAGE:
01009                ast_cli(fd, e->usage);
01010                break;
01011             }
01012          } else 
01013             ast_cli(fd, "No such command '%s' (type 'help' for help)\n", find_best(argv));
01014          if (e) {
01015             ast_mutex_lock(&clilock);
01016             e->inuse--;
01017             ast_mutex_unlock(&clilock);
01018          }
01019       }
01020       free(dup);
01021    } else {
01022       ast_log(LOG_WARNING, "Out of memory\n");  
01023       return -1;
01024    }
01025    return 0;
01026 }

char** ast_cli_completion_matches char *  ,
char * 
 

Definition at line 876 of file cli.c.

References ast_cli_generator(), malloc, and realloc.

00877 {
00878    char **match_list = NULL, *retstr, *prevstr;
00879    size_t match_list_len, max_equal, which, i;
00880    int matches = 0;
00881 
00882    match_list_len = 1;
00883    while ((retstr = ast_cli_generator(text, word, matches)) != NULL) {
00884       if (matches + 1 >= match_list_len) {
00885          match_list_len <<= 1;
00886          match_list = realloc(match_list, match_list_len * sizeof(char *));
00887       }
00888       match_list[++matches] = retstr;
00889    }
00890 
00891    if (!match_list)
00892       return (char **) NULL;
00893 
00894    which = 2;
00895    prevstr = match_list[1];
00896    max_equal = strlen(prevstr);
00897    for (; which <= matches; which++) {
00898       for (i = 0; i < max_equal && prevstr[i] == match_list[which][i]; i++)
00899          continue;
00900       max_equal = i;
00901    }
00902 
00903    retstr = malloc(max_equal + 1);
00904    (void) strncpy(retstr, match_list[1], max_equal);
00905    retstr[max_equal] = '\0';
00906    match_list[0] = retstr;
00907 
00908    if (matches + 1 >= match_list_len)
00909       match_list = realloc(match_list, (match_list_len + 1) * sizeof(char *));
00910    match_list[matches + 1] = (char *) NULL;
00911 
00912    return (match_list);
00913 }

char* ast_cli_generator char *  ,
char *  ,
int 
 

Readline madness.

Definition at line 986 of file cli.c.

Referenced by ast_cli_completion_matches(), and ast_cli_generatornummatches().

00987 {
00988    return __ast_cli_generator(text, word, state, 1);
00989 }

int ast_cli_generatornummatches char *  ,
char * 
 

Definition at line 859 of file cli.c.

References ast_cli_generator().

00860 {
00861    int matches = 0, i = 0;
00862    char *buf, *oldbuf = NULL;
00863 
00864 
00865    while ( (buf = ast_cli_generator(text, word, i)) ) {
00866       if (++i > 1 && strcmp(buf,oldbuf) == 0)  {
00867             continue;
00868       }
00869       oldbuf = buf;
00870       matches++;
00871    }
00872 
00873    return matches;
00874 }

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

References ast_log(), ast_mutex_lock, ast_mutex_unlock, clilock, 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(), and main().

00683 {
00684    struct ast_cli_entry *cur, *l=NULL;
00685    char fulle[80] ="", fulltst[80] ="";
00686    static int len;
00687    ast_mutex_lock(&clilock);
00688    join2(fulle, sizeof(fulle), e->cmda);
00689    if (find_cli(e->cmda, -1)) {
00690       ast_mutex_unlock(&clilock);
00691       ast_log(LOG_WARNING, "Command '%s' already registered (or something close enough)\n", fulle);
00692       return -1;
00693    }
00694    cur = helpers;
00695    while(cur) {
00696       join2(fulltst, sizeof(fulltst), cur->cmda);
00697       len = strlen(fulltst);
00698       if (strlen(fulle) < len)
00699          len = strlen(fulle);
00700       if (strncasecmp(fulle, fulltst, len) < 0) {
00701          if (l) {
00702             e->next = l->next;
00703             l->next = e;
00704          } else {
00705             e->next = helpers;
00706             helpers = e;
00707          }
00708          break;
00709       }
00710       l = cur;
00711       cur = cur->next;
00712    }
00713    if (!cur) {
00714       if (l)
00715          l->next = e;
00716       else
00717          helpers = e;
00718       e->next = NULL;
00719    }
00720    ast_mutex_unlock(&clilock);
00721    return 0;
00722 }

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

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

00657 {
00658    struct ast_cli_entry *cur, *l=NULL;
00659    ast_mutex_lock(&clilock);
00660    cur = helpers;
00661    while(cur) {
00662       if (e == cur) {
00663          if (e->inuse) {
00664             ast_log(LOG_WARNING, "Can't remove command that is in use\n");
00665          } else {
00666             /* Rewrite */
00667             if (l)
00668                l->next = e->next;
00669             else
00670                helpers = e->next;
00671             e->next = NULL;
00672             break;
00673          }
00674       }
00675       l = cur;
00676       cur = cur->next;
00677    }
00678    ast_mutex_unlock(&clilock);
00679    return 0;
00680 }


Generated on Fri Feb 27 12:19:47 2004 for Asterisk by doxygen 1.3.5