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

01004 {
01005    char *argv[AST_MAX_ARGS];
01006    struct ast_cli_entry *e;
01007    int x;
01008    char *dup;
01009    x = AST_MAX_ARGS;
01010    if ((dup = parse_args(s, &x, argv))) {
01011       /* We need at least one entry, or ignore */
01012       if (x > 0) {
01013          ast_mutex_lock(&clilock);
01014          e = find_cli(argv, 0);
01015          if (e)
01016             e->inuse++;
01017          ast_mutex_unlock(&clilock);
01018          if (e) {
01019             switch(e->handler(fd, x, argv)) {
01020             case RESULT_SHOWUSAGE:
01021                ast_cli(fd, e->usage);
01022                break;
01023             }
01024          } else 
01025             ast_cli(fd, "No such command '%s' (type 'help' for help)\n", find_best(argv));
01026          if (e) {
01027             ast_mutex_lock(&clilock);
01028             e->inuse--;
01029             ast_mutex_unlock(&clilock);
01030          }
01031       }
01032       free(dup);
01033    } else {
01034       ast_log(LOG_WARNING, "Out of memory\n");  
01035       return -1;
01036    }
01037    return 0;
01038 }

char** ast_cli_completion_matches char *  ,
char * 
 

Definition at line 887 of file cli.c.

References ast_cli_generator(), malloc, and realloc.

00888 {
00889    char **match_list = NULL, *retstr, *prevstr;
00890    size_t match_list_len, max_equal, which, i;
00891    int matches = 0;
00892 
00893    match_list_len = 1;
00894    while ((retstr = ast_cli_generator(text, word, matches)) != NULL) {
00895       if (matches + 1 >= match_list_len) {
00896          match_list_len <<= 1;
00897          match_list = realloc(match_list, match_list_len * sizeof(char *));
00898       }
00899       match_list[++matches] = retstr;
00900    }
00901 
00902    if (!match_list)
00903       return (char **) NULL;
00904 
00905    which = 2;
00906    prevstr = match_list[1];
00907    max_equal = strlen(prevstr);
00908    for (; which <= matches; which++) {
00909       for (i = 0; i < max_equal && prevstr[i] == match_list[which][i]; i++)
00910          continue;
00911       max_equal = i;
00912    }
00913 
00914    retstr = malloc(max_equal + 1);
00915    (void) strncpy(retstr, match_list[1], max_equal);
00916    retstr[max_equal] = '\0';
00917    match_list[0] = retstr;
00918 
00919    if (matches + 1 >= match_list_len)
00920       match_list = realloc(match_list, (match_list_len + 1) * sizeof(char *));
00921    match_list[matches + 1] = (char *) NULL;
00922 
00923    return (match_list);
00924 }

char* ast_cli_generator char *  ,
char *  ,
int 
 

Readline madness.

Definition at line 998 of file cli.c.

Referenced by ast_cli_completion_matches(), and ast_cli_generatornummatches().

00999 {
01000    return __ast_cli_generator(text, word, state, 1);
01001 }

int ast_cli_generatornummatches char *  ,
char * 
 

Definition at line 870 of file cli.c.

References ast_cli_generator().

00871 {
00872    int matches = 0, i = 0;
00873    char *buf, *oldbuf = NULL;
00874 
00875 
00876    while ( (buf = ast_cli_generator(text, word, i)) ) {
00877       if (++i > 1 && strcmp(buf,oldbuf) == 0)  {
00878             continue;
00879       }
00880       oldbuf = buf;
00881       matches++;
00882    }
00883 
00884    return matches;
00885 }

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 693 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().

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

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

00668 {
00669    struct ast_cli_entry *cur, *l=NULL;
00670    ast_mutex_lock(&clilock);
00671    cur = helpers;
00672    while(cur) {
00673       if (e == cur) {
00674          if (e->inuse) {
00675             ast_log(LOG_WARNING, "Can't remove command that is in use\n");
00676          } else {
00677             /* Rewrite */
00678             if (l)
00679                l->next = e->next;
00680             else
00681                helpers = e->next;
00682             e->next = NULL;
00683             break;
00684          }
00685       }
00686       l = cur;
00687       cur = cur->next;
00688    }
00689    ast_mutex_unlock(&clilock);
00690    return 0;
00691 }


Generated on Sun Apr 18 23:34:02 2004 for Asterisk by doxygen 1.3.6-20040222