cli.c File Reference

#include <unistd.h>
#include <stdlib.h>
#include <asterisk/logger.h>
#include <asterisk/options.h>
#include <asterisk/cli.h>
#include <asterisk/module.h>
#include <asterisk/channel.h>
#include <asterisk/channel_pvt.h>
#include <asterisk/manager.h>
#include <asterisk/utils.h>
#include <asterisk/lock.h>
#include <sys/signal.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <ctype.h>
#include "editline/readline/readline.h"
#include "asterisk.h"
#include "build.h"
#include "astconf.h"

Include dependency graph for cli.c:

Go to the source code of this file.

Defines

#define VERSION_INFO
#define MODLIST_FORMAT   "%-25s %-40.40s %-10d\n"
#define MODLIST_FORMAT2   "%-25s %-40.40s %-10s\n"
#define SECOND   (1)
#define MINUTE   (SECOND*60)
#define HOUR   (MINUTE*60)
#define DAY   (HOUR*24)
#define WEEK   (DAY*7)
#define YEAR   (DAY*365)
#define ESS(x)   ((x == 1) ? "" : "s")
#define FORMAT_STRING   "%15s (%-10s %-12s %-4d) %7s %-12s %-15s\n"
#define FORMAT_STRING2   "%15s (%-10s %-12s %-4s) %7s %-12s %-15s\n"
#define CONCISE_FORMAT_STRING   "%s:%s:%s:%d:%s:%s:%s:%s:%s:%d\n"

Functions

void ast_cli (int fd, char *fmt,...)
 AST_MUTEX_DEFINE_STATIC (clilock)
 AST_MUTEX_DEFINE_STATIC (climodentrylock)
int ast_cli_unregister (struct ast_cli_entry *e)
 Unregisters a command.
int ast_cli_register (struct ast_cli_entry *e)
 Registers a command.
int ast_cli_generatornummatches (char *text, char *word)
char ** ast_cli_completion_matches (char *text, char *word)
char * ast_cli_generator (char *text, char *word, int state)
 Readline madness.
int ast_cli_command (int fd, char *s)
 Interprets a command.

Variables

struct ast_cli_entryhelpers = NULL


Define Documentation

#define CONCISE_FORMAT_STRING   "%s:%s:%s:%d:%s:%s:%s:%s:%s:%d\n"

#define DAY   (HOUR*24)

#define ESS (  )     ((x == 1) ? "" : "s")

#define FORMAT_STRING   "%15s (%-10s %-12s %-4d) %7s %-12s %-15s\n"

#define FORMAT_STRING2   "%15s (%-10s %-12s %-4s) %7s %-12s %-15s\n"

#define HOUR   (MINUTE*60)

#define MINUTE   (SECOND*60)

#define MODLIST_FORMAT   "%-25s %-40.40s %-10d\n"

Definition at line 212 of file cli.c.

#define MODLIST_FORMAT2   "%-25s %-40.40s %-10s\n"

Definition at line 213 of file cli.c.

#define SECOND   (1)

#define VERSION_INFO

Value:

"Asterisk " ASTERISK_VERSION " built by " BUILD_USER "@" BUILD_HOSTNAME \
   " on a " BUILD_MACHINE " running " BUILD_OS

Definition at line 37 of file cli.c.

#define WEEK   (DAY*7)

#define YEAR   (DAY*365)


Function Documentation

void ast_cli ( int  fd,
char *  fmt,
  ... 
)

Definition at line 40 of file cli.c.

00041 {
00042    char *stuff;
00043    int res = 0;
00044 
00045    va_list ap;
00046    va_start(ap, fmt);
00047    res = vasprintf(&stuff, fmt, ap);
00048    va_end(ap);
00049    if (res == -1) {
00050       ast_log(LOG_ERROR, "Out of memory\n");
00051    } else {
00052       ast_carefulwrite(fd, stuff, strlen(stuff), 100);
00053       free(stuff);
00054    }
00055 }

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.

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

char** ast_cli_completion_matches ( char *  text,
char *  word 
)

Definition at line 1022 of file cli.c.

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

char* ast_cli_generator ( char *  text,
char *  word,
int  state 
)

Readline madness.

Definition at line 1134 of file cli.c.

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

int ast_cli_generatornummatches ( char *  text,
char *  word 
)

Definition at line 1005 of file cli.c.

01006 {
01007    int matches = 0, i = 0;
01008    char *buf = NULL, *oldbuf = NULL;
01009 
01010    while ( (buf = ast_cli_generator(text, word, i++)) ) {
01011       if (!oldbuf || strcmp(buf,oldbuf))
01012          matches++;
01013       if (oldbuf)
01014          free(oldbuf);
01015       oldbuf = buf;
01016    }
01017    if (oldbuf)
01018       free(oldbuf);
01019    return matches;
01020 }

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

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

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

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

AST_MUTEX_DEFINE_STATIC ( climodentrylock   ) 

AST_MUTEX_DEFINE_STATIC ( clilock   ) 


Variable Documentation

struct ast_cli_entry* helpers = NULL

Definition at line 59 of file cli.c.


Generated on Wed Oct 28 17:00:52 2009 for Asterisk by  doxygen 1.5.6