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

int ast_cli_register (struct ast_cli_entry *e)
 Registers a command. More...

int ast_cli_unregister (struct ast_cli_entry *e)
 Unregisters a command. More...

char * ast_cli_generator (char *, char *, int)
 Readline madness. More...

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 1132 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, s, and ast_cli_entry::usage.

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

char** ast_cli_completion_matches char *   ,
char *   
 

Definition at line 1016 of file cli.c.

References ast_cli_generator(), malloc, and realloc.

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

char* ast_cli_generator char *   ,
char *   ,
int   
 

Readline madness.

Definition at line 1127 of file cli.c.

Referenced by ast_cli_completion_matches(), and ast_cli_generatornummatches().

01128 {
01129    return __ast_cli_generator(text, word, state, 1);
01130 }

int ast_cli_generatornummatches char *   ,
char *   
 

Definition at line 999 of file cli.c.

References ast_cli_generator().

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

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

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

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

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

00797 {
00798    struct ast_cli_entry *cur, *l=NULL;
00799    ast_mutex_lock(&clilock);
00800    cur = helpers;
00801    while(cur) {
00802       if (e == cur) {
00803          if (e->inuse) {
00804             ast_log(LOG_WARNING, "Can't remove command that is in use\n");
00805          } else {
00806             /* Rewrite */
00807             if (l)
00808                l->next = e->next;
00809             else
00810                helpers = e->next;
00811             e->next = NULL;
00812             break;
00813          }
00814       }
00815       l = cur;
00816       cur = cur->next;
00817    }
00818    ast_mutex_unlock(&clilock);
00819    return 0;
00820 }


Generated on Thu Oct 28 11:33:01 2004 for Asterisk by doxygen1.2.15