Version 0.3.0 from FTP
[asterisk/asterisk.git] / cli.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Standard Command Line Interface
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13
14 #include <unistd.h>
15 #include <stdlib.h>
16 #include <asterisk/logger.h>
17 #include <asterisk/options.h>
18 #include <asterisk/cli.h>
19 #include <asterisk/module.h>
20 #include <asterisk/channel.h>
21 #include <asterisk/channel_pvt.h>
22 #include <sys/signal.h>
23 #include <stdio.h>
24 #include <signal.h>
25 #include <string.h>
26 #include <pthread.h>
27 /* For rl_filename_completion */
28 #include <readline/readline.h>
29 /* For module directory */
30 #include "asterisk.h"
31 #include "build.h"
32
33 #define VERSION_INFO "Asterisk " ASTERISK_VERSION " built by " BUILD_USER "@" BUILD_HOSTNAME \
34         " on a " BUILD_MACHINE " running " BUILD_OS
35         
36
37 void ast_cli(int fd, char *fmt, ...)
38 {
39         char stuff[4096];
40         va_list ap;
41         va_start(ap, fmt);
42         vsnprintf(stuff, sizeof(stuff), fmt, ap);
43         va_end(ap);
44         write(fd, stuff, strlen(stuff));
45 }
46
47 pthread_mutex_t clilock = AST_MUTEX_INITIALIZER;
48
49
50 struct ast_cli_entry *helpers = NULL;
51
52 static char load_help[] = 
53 "Usage: load <module name>\n"
54 "       Loads the specified module into Asterisk.\n";
55
56 static char unload_help[] = 
57 "Usage: unload [-f|-h] <module name>\n"
58 "       Unloads the specified module from Asterisk.  The -f\n"
59 "       option causes the module to be unloaded even if it is\n"
60 "       in use (may cause a crash) and the -h module causes the\n"
61 "       module to be unloaded even if the module says it cannot, \n"
62 "       which almost always will cause a crash.\n";
63
64 static char help_help[] =
65 "Usage: help [topic]\n"
66 "       When called with a topic as an argument, displays usage\n"
67 "       information on the given command.  If called without a\n"
68 "       topic, it provides a list of commands.\n";
69
70 static char chanlist_help[] = 
71 "Usage: show channels\n"
72 "       Lists currently defined channels and some information about\n"
73 "       them.\n";
74
75 static char reload_help[] = 
76 "Usage: reload\n"
77 "       Reloads configuration files for all modules which support\n"
78 "       reloading.\n";
79
80 static char set_verbose_help[] = 
81 "Usage: set verbose <level>\n"
82 "       Sets level of verbose messages to be displayed.  0 means\n"
83 "       no messages should be displayed.\n";
84
85 static char softhangup_help[] =
86 "Usage: soft hangup <channel>\n"
87 "       Request that a channel be hung up.  The hangup takes effect\n"
88 "       the next time the driver reads or writes from the channel\n";
89
90 static int handle_load(int fd, int argc, char *argv[])
91 {
92         if (argc != 2)
93                 return RESULT_SHOWUSAGE;
94         if (ast_load_resource(argv[1])) {
95                 ast_cli(fd, "Unable to load module %s\n", argv[1]);
96                 return RESULT_FAILURE;
97         }
98         return RESULT_SUCCESS;
99 }
100
101 static int handle_reload(int fd, int argc, char *argv[])
102 {
103         if (argc != 1)
104                 return RESULT_SHOWUSAGE;
105         ast_module_reload();
106         return RESULT_SUCCESS;
107 }
108
109 static int handle_set_verbose(int fd, int argc, char *argv[])
110 {
111         int val;
112         /* Has a hidden 'at least' argument */
113         if ((argc != 3) && (argc != 4))
114                 return RESULT_SHOWUSAGE;
115         if ((argc == 4) && strcasecmp(argv[2], "atleast"))
116                 return RESULT_SHOWUSAGE;
117         if (argc == 3)
118                 option_verbose = atoi(argv[2]);
119         else {
120                 val = atoi(argv[3]);
121                 if (val > option_verbose)
122                         option_verbose = val;
123         }
124         return RESULT_SUCCESS;
125 }
126
127 static int handle_unload(int fd, int argc, char *argv[])
128 {
129         int x;
130         int force=AST_FORCE_SOFT;
131         if (argc < 2)
132                 return RESULT_SHOWUSAGE;
133         for (x=1;x<argc;x++) {
134                 if (argv[x][0] == '-') {
135                         switch(argv[x][1]) {
136                         case 'f':
137                                 force = AST_FORCE_FIRM;
138                                 break;
139                         case 'h':
140                                 force = AST_FORCE_HARD;
141                                 break;
142                         default:
143                                 return RESULT_SHOWUSAGE;
144                         }
145                 } else if (x !=  argc - 1) 
146                         return RESULT_SHOWUSAGE;
147                 else if (ast_unload_resource(argv[x], force)) {
148                         ast_cli(fd, "Unable to unload resource %s\n", argv[x]);
149                         return RESULT_FAILURE;
150                 }
151         }
152         return RESULT_SUCCESS;
153 }
154
155 #define MODLIST_FORMAT  "%-20s %-40.40s %-10d\n"
156 #define MODLIST_FORMAT2 "%-20s %-40.40s %-10s\n"
157
158 static pthread_mutex_t climodentrylock = AST_MUTEX_INITIALIZER;
159 static int climodentryfd = -1;
160
161 static int modlist_modentry(char *module, char *description, int usecnt)
162 {
163         ast_cli(climodentryfd, MODLIST_FORMAT, module, description, usecnt);
164         return 0;
165 }
166
167 static char modlist_help[] =
168 "Usage: show modules\n"
169 "       Shows Asterisk modules currently in use, and usage "
170 "statistics.\n";
171
172 static char version_help[] =
173 "Usage: show version\n"
174 "       Shows Asterisk version information.\n ";
175
176 static int handle_modlist(int fd, int argc, char *argv[])
177 {
178         if (argc != 2)
179                 return RESULT_SHOWUSAGE;
180         ast_pthread_mutex_lock(&climodentrylock);
181         climodentryfd = fd;
182         ast_cli(fd, MODLIST_FORMAT2, "Module", "Description", "Use Count");
183         ast_update_module_list(modlist_modentry);
184         climodentryfd = -1;
185         ast_pthread_mutex_unlock(&climodentrylock);
186         return RESULT_SUCCESS;
187 }
188
189 static int handle_version(int fd, int argc, char *argv[])
190 {
191         if (argc != 2)
192                 return RESULT_SHOWUSAGE;
193         ast_cli(fd, "%s\n", VERSION_INFO);
194         return RESULT_SUCCESS;
195 }
196 static int handle_chanlist(int fd, int argc, char *argv[])
197 {
198 #define FORMAT_STRING  "%15s  (%-10s %-12s %-4d) %7s %-12s  %-15s\n"
199 #define FORMAT_STRING2 "%15s  (%-10s %-12s %-4s) %7s %-12s  %-15s\n"
200         struct ast_channel *c=NULL;
201         if (argc != 2)
202                 return RESULT_SHOWUSAGE;
203         c = ast_channel_walk(NULL);
204         ast_cli(fd, FORMAT_STRING2, "Channel", "Context", "Extension", "Pri", "State", "Appl.", "Data");
205         while(c) {
206                 ast_cli(fd, FORMAT_STRING, c->name, c->context, c->exten, c->priority, ast_state2str(c->_state),
207                 c->appl ? c->appl : "(None)", c->data ? ( strlen(c->data) ? c->data : "(Empty)" ): "(None)");
208                 c = ast_channel_walk(c);
209         }
210         return RESULT_SUCCESS;
211 }
212
213 static char showchan_help[] = 
214 "Usage: show channel <channel>\n"
215 "       Shows lots of information about the specified channel.\n";
216
217 static char commandcomplete_help[] = 
218 "Usage: _command complete \"<line>\" text state\n"
219 "       This function is used internally to help with command completion and should.\n"
220 "       never be called by the user directly.\n";
221
222 static int handle_softhangup(int fd, int argc, char *argv[])
223 {
224         struct ast_channel *c=NULL;
225         if (argc != 3)
226                 return RESULT_SHOWUSAGE;
227         c = ast_channel_walk(NULL);
228         while(c) {
229                 if (!strcasecmp(c->name, argv[2])) {
230                         ast_cli(fd, "Requested Hangup on channel '%s'\n", c->name);
231                         ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
232                         break;
233                 }
234                 c = ast_channel_walk(c);
235         }
236         if (!c) 
237                 ast_cli(fd, "%s is not a known channel\n", argv[2]);
238         return RESULT_SUCCESS;
239 }
240
241 static char *__ast_cli_generator(char *text, char *word, int state, int lock);
242
243 static int handle_commandcomplete(int fd, int argc, char *argv[])
244 {
245         char *buf;
246 #if 0
247         printf("Search for %d args: '%s', '%s', '%s', '%s'\n", argc, argv[0], argv[1], argv[2], argv[3]);
248 #endif  
249         if (argc != 5)
250                 return RESULT_SHOWUSAGE;
251         buf = __ast_cli_generator(argv[2], argv[3], atoi(argv[4]), 0);
252 #if 0
253         printf("Search for '%s' %s %d got '%s'\n", argv[2], argv[3], atoi(argv[4]), buf);
254 #endif  
255         if (buf) {
256                 ast_cli(fd, buf);
257                 free(buf);
258         } else
259                 ast_cli(fd, "NULL\n");
260         return RESULT_SUCCESS;
261 }
262
263 static int handle_showchan(int fd, int argc, char *argv[])
264 {
265         struct ast_channel *c=NULL;
266         if (argc != 3)
267                 return RESULT_SHOWUSAGE;
268         c = ast_channel_walk(NULL);
269         while(c) {
270                 if (!strcasecmp(c->name, argv[2])) {
271                         ast_cli(fd, 
272         " -- General --\n"
273         "           Name: %s\n"
274         "           Type: %s\n"
275         "      Caller ID: %s\n"
276         "    DNID Digits: %s\n"
277         "          State: %s (%d)\n"
278         "          Rings: %d\n"
279         "    WriteFormat: %d\n"
280         "     ReadFormat: %d\n"
281         "   NativeFormat: %d\n"
282         "1st File Descriptor: %d\n"
283         " --   PBX   --\n"
284         "        Context: %s\n"
285         "      Extension: %s\n"
286         "       Priority: %d\n"
287         "    Application: %s\n"
288         "           Data: %s\n"
289         "          Stack: %d\n"
290         "    Blocking in: %s\n",
291         c->name, c->type, 
292         (c->callerid ? c->callerid : "(N/A)"),
293         (c->dnid ? c->dnid : "(N/A)" ), ast_state2str(c->_state), c->_state, c->rings, c->nativeformats, c->writeformat, c->readformat,
294         c->fds[0], c->context, c->exten, c->priority, ( c->appl ? c->appl : "(N/A)" ),
295         ( c-> data ? (strlen(c->data) ? c->data : "(Empty)") : "(None)"),
296         c->stack, (c->blocking ? c->blockproc : "(Not Blocking)"));
297         
298                 break;
299                 }
300                 c = ast_channel_walk(c);
301         }
302         if (!c) 
303                 ast_cli(fd, "%s is not a known channel\n", argv[2]);
304         return RESULT_SUCCESS;
305 }
306
307 static char *complete_ch(char *line, char *word, int pos, int state)
308 {
309         struct ast_channel *c;
310         int which=0;
311         c = ast_channel_walk(NULL);
312         while(c) {
313                 if (!strncasecmp(word, c->name, strlen(word))) {
314                         if (++which > state)
315                                 break;
316                 }
317                 c = ast_channel_walk(c);
318         }
319         return c ? strdup(c->name) : NULL;
320 }
321
322 static char *complete_fn(char *line, char *word, int pos, int state)
323 {
324         char *c;
325         char filename[256];
326         if (pos != 1)
327                 return NULL;
328         if (word[0] == '/')
329                 strncpy(filename, word, sizeof(filename)-1);
330         else
331                 snprintf(filename, sizeof(filename), "%s/%s", AST_MODULE_DIR, word);
332         c = (char*)filename_completion_function(filename, state);
333         if (c && word[0] != '/')
334                 c += (strlen(AST_MODULE_DIR) + 1);
335         return c ? strdup(c) : c;
336 }
337
338 static int handle_help(int fd, int argc, char *argv[]);
339
340 static struct ast_cli_entry builtins[] = {
341         /* Keep alphabetized */
342         { { "help", NULL }, handle_help, "Display help list, or specific help on a command", help_help },
343         { { "_command", "complete", NULL }, handle_commandcomplete, "Command complete", commandcomplete_help },
344         { { "load", NULL }, handle_load, "Load a dynamic module by name", load_help, complete_fn },
345         { { "reload", NULL }, handle_reload, "Reload configuration", reload_help },
346         { { "set", "verbose", NULL }, handle_set_verbose, "Set level of verboseness", set_verbose_help },
347         { { "show", "channel", NULL }, handle_showchan, "Display information on a specific channel", showchan_help, complete_ch },
348         { { "show", "channels", NULL }, handle_chanlist, "Display information on channels", chanlist_help },
349         { { "show", "modules", NULL }, handle_modlist, "List modules and info", modlist_help },
350         { { "show", "version", NULL }, handle_version, "Display version info", version_help },
351         { { "soft", "hangup", NULL }, handle_softhangup, "Request a hangup on a given channel", softhangup_help, complete_ch },
352         { { "unload", NULL }, handle_unload, "Unload a dynamic module by name", unload_help, complete_fn },
353         { { NULL }, NULL, NULL, NULL }
354 };
355
356 static struct ast_cli_entry *find_cli(char *cmds[], int exact)
357 {
358         int x;
359         int y;
360         int match;
361         struct ast_cli_entry *e=NULL;
362         for (x=0;builtins[x].cmda[0];x++) {
363                 /* start optimistic */
364                 match = 1;
365                 for (y=0;match && cmds[y]; y++) {
366                         /* If there are no more words in the candidate command, then we're
367                            there.  */
368                         if (!builtins[x].cmda[y] && !exact)
369                                 break;
370                         /* If there are no more words in the command (and we're looking for
371                            an exact match) or there is a difference between the two words,
372                            then this is not a match */
373                         if (!builtins[x].cmda[y] || strcasecmp(builtins[x].cmda[y], cmds[y]))
374                                 match = 0;
375                 }
376                 /* If more words are needed to complete the command then this is not
377                    a candidate (unless we're looking for a really inexact answer  */
378                 if ((exact > -1) && builtins[x].cmda[y])
379                         match = 0;
380                 if (match)
381                         return &builtins[x];
382         }
383         for (e=helpers;e;e=e->next) {
384                 match = 1;
385                 for (y=0;match && cmds[y]; y++) {
386                         if (!e->cmda[y] && !exact)
387                                 break;
388                         if (!e->cmda[y] || strcasecmp(e->cmda[y], cmds[y]))
389                                 match = 0;
390                 }
391                 if ((exact > -1) && e->cmda[y])
392                         match = 0;
393                 if (match)
394                         break;
395         }
396         return e;
397 }
398
399 static void join(char *s, int len, char *w[])
400 {
401         int x;
402         /* Join words into a string */
403         strcpy(s, "");
404         for (x=0;w[x];x++) {
405                 if (x)
406                         strncat(s, " ", len - strlen(s));
407                 strncat(s, w[x], len - strlen(s));
408         }
409 }
410
411 static void join2(char *s, int len, char *w[])
412 {
413         int x;
414         /* Join words into a string */
415         strcpy(s, "");
416         for (x=0;w[x];x++) {
417                 strncat(s, w[x], len - strlen(s));
418         }
419 }
420
421 static char *find_best(char *argv[])
422 {
423         static char cmdline[80];
424         int x;
425         /* See how close we get, then print the  */
426         char *myargv[AST_MAX_CMD_LEN];
427         for (x=0;x<AST_MAX_CMD_LEN;x++)
428                 myargv[x]=NULL;
429         for (x=0;argv[x];x++) {
430                 myargv[x] = argv[x];
431                 if (!find_cli(myargv, -1))
432                         break;
433         }
434         join(cmdline, sizeof(cmdline), myargv);
435         return cmdline;
436 }
437
438 int ast_cli_unregister(struct ast_cli_entry *e)
439 {
440         struct ast_cli_entry *cur, *l=NULL;
441         ast_pthread_mutex_lock(&clilock);
442         cur = helpers;
443         while(cur) {
444                 if (e == cur) {
445                         if (e->inuse) {
446                                 ast_log(LOG_WARNING, "Can't remove command that is in use\n");
447                         } else {
448                                 /* Rewrite */
449                                 if (l)
450                                         l->next = e->next;
451                                 else
452                                         helpers = e->next;
453                                 e->next = NULL;
454                                 break;
455                         }
456                 }
457                 l = cur;
458                 cur = cur->next;
459         }
460         ast_pthread_mutex_unlock(&clilock);
461         return 0;
462 }
463
464 int ast_cli_register(struct ast_cli_entry *e)
465 {
466         struct ast_cli_entry *cur, *l=NULL;
467         char fulle[80] ="", fulltst[80] ="";
468         static int len;
469         ast_pthread_mutex_lock(&clilock);
470         join2(fulle, sizeof(fulle), e->cmda);
471         if (find_cli(e->cmda, -1)) {
472                 ast_pthread_mutex_unlock(&clilock);
473                 ast_log(LOG_WARNING, "Command '%s' already registered (or something close enough)\n", fulle);
474                 return -1;
475         }
476         cur = helpers;
477         while(cur) {
478                 join2(fulltst, sizeof(fulltst), cur->cmda);
479                 len = strlen(fulltst);
480                 if (strlen(fulle) < len)
481                         len = strlen(fulle);
482                 if (strncasecmp(fulle, fulltst, len) < 0) {
483                         if (l) {
484                                 e->next = l->next;
485                                 l->next = e;
486                         } else {
487                                 e->next = helpers;
488                                 helpers = e;
489                         }
490                         break;
491                 }
492                 l = cur;
493                 cur = cur->next;
494         }
495         if (!cur) {
496                 if (l)
497                         l->next = e;
498                 else
499                         helpers = e;
500                 e->next = NULL;
501         }
502         ast_pthread_mutex_unlock(&clilock);
503         return 0;
504 }
505
506 static int help_workhorse(int fd, char *match[])
507 {
508         char fullcmd1[80];
509         char fullcmd2[80];
510         char matchstr[80];
511         char *fullcmd;
512         struct ast_cli_entry *e, *e1, *e2;
513         e1 = builtins;
514         e2 = helpers;
515         if (match)
516                 join(matchstr, sizeof(matchstr), match);
517         while(e1->cmda[0] || e2) {
518                 if (e2)
519                         join(fullcmd2, sizeof(fullcmd2), e2->cmda);
520                 if (e1->cmda[0])
521                         join(fullcmd1, sizeof(fullcmd1), e1->cmda);
522                 if (!e1->cmda[0] || 
523                                 (e2 && (strcmp(fullcmd2, fullcmd1) < 0))) {
524                         /* Use e2 */
525                         e = e2;
526                         fullcmd = fullcmd2;
527                         /* Increment by going to next */
528                         e2 = e2->next;
529                 } else {
530                         /* Use e1 */
531                         e = e1;
532                         fullcmd = fullcmd1;
533                         e1++;
534                 }
535                 /* Hide commands that start with '_' */
536                 if (fullcmd[0] == '_')
537                         continue;
538                 if (match) {
539                         if (strncasecmp(matchstr, fullcmd, strlen(matchstr))) {
540                                 continue;
541                         }
542                 }
543                 ast_cli(fd, "%20.20s   %s\n", fullcmd, e->summary);
544         }
545         return 0;
546 }
547
548 static int handle_help(int fd, int argc, char *argv[]) {
549         struct ast_cli_entry *e;
550         char fullcmd[80];
551         if ((argc < 1))
552                 return RESULT_SHOWUSAGE;
553         if (argc > 1) {
554                 e = find_cli(argv + 1, 1);
555                 if (e) 
556                         ast_cli(fd, e->usage);
557                 else {
558                         if (find_cli(argv + 1, -1)) {
559                                 return help_workhorse(fd, argv + 1);
560                         } else {
561                                 join(fullcmd, sizeof(fullcmd), argv+1);
562                                 ast_cli(fd, "No such command '%s'.\n", fullcmd);
563                         }
564                 }
565         } else {
566                 return help_workhorse(fd, NULL);
567         }
568         return RESULT_SUCCESS;
569 }
570
571 static char *parse_args(char *s, int *max, char *argv[])
572 {
573         char *dup, *cur;
574         int x=0;
575         int quoted=0;
576         int escaped=0;
577         int whitespace=1;
578
579         dup = strdup(s);
580         if (dup) {
581                 cur = dup;
582                 while(*s) {
583                         switch(*s) {
584                         case '"':
585                                 /* If it's escaped, put a literal quote */
586                                 if (escaped) 
587                                         goto normal;
588                                 else 
589                                         quoted = !quoted;
590                                 if (quoted && whitespace) {
591                                         /* If we're starting a quote, coming off white space start a new word, too */
592                                         argv[x++] = cur;
593                                         whitespace=0;
594                                 }
595                                 escaped = 0;
596                                 break;
597                         case ' ':
598                         case '\t':
599                                 if (!quoted && !escaped) {
600                                         /* If we're not quoted, mark this as whitespace, and
601                                            end the previous argument */
602                                         whitespace = 1;
603                                         *(cur++) = '\0';
604                                 } else
605                                         /* Otherwise, just treat it as anything else */ 
606                                         goto normal;
607                                 break;
608                         case '\\':
609                                 /* If we're escaped, print a literal, otherwise enable escaping */
610                                 if (escaped) {
611                                         goto normal;
612                                 } else {
613                                         escaped=1;
614                                 }
615                                 break;
616                         default:
617 normal:
618                                 if (whitespace) {
619                                         if (x >= AST_MAX_ARGS -1) {
620                                                 ast_log(LOG_WARNING, "Too many arguments, truncating\n");
621                                                 break;
622                                         }
623                                         /* Coming off of whitespace, start the next argument */
624                                         argv[x++] = cur;
625                                         whitespace=0;
626                                 }
627                                 *(cur++) = *s;
628                                 escaped=0;
629                         }
630                         s++;
631                 }
632                 /* Null terminate */
633                 *(cur++) = '\0';
634                 argv[x] = NULL;
635                 *max = x;
636         }
637         return dup;
638 }
639
640 static char *__ast_cli_generator(char *text, char *word, int state, int lock)
641 {
642         char *argv[AST_MAX_ARGS];
643         struct ast_cli_entry *e, *e1, *e2;
644         int x;
645         int matchnum=0;
646         char *dup, *res;
647         char fullcmd1[80];
648         char fullcmd2[80];
649         char matchstr[80];
650         char *fullcmd;
651
652         if ((dup = parse_args(text, &x, argv))) {
653                 join(matchstr, sizeof(matchstr), argv);
654                 if (lock)
655                         ast_pthread_mutex_lock(&clilock);
656                 e1 = builtins;
657                 e2 = helpers;
658                 while(e1->cmda[0] || e2) {
659                         if (e2)
660                                 join(fullcmd2, sizeof(fullcmd2), e2->cmda);
661                         if (e1->cmda[0])
662                                 join(fullcmd1, sizeof(fullcmd1), e1->cmda);
663                         if (!e1->cmda[0] || 
664                                         (e2 && (strcmp(fullcmd2, fullcmd1) < 0))) {
665                                 /* Use e2 */
666                                 e = e2;
667                                 fullcmd = fullcmd2;
668                                 /* Increment by going to next */
669                                 e2 = e2->next;
670                         } else {
671                                 /* Use e1 */
672                                 e = e1;
673                                 fullcmd = fullcmd1;
674                                 e1++;
675                         }
676                         if ((fullcmd[0] != '_') && !strncasecmp(matchstr, fullcmd, strlen(matchstr))) {
677                                 /* We contain the first part of one or more commands */
678                                 matchnum++;
679                                 if (matchnum > state) {
680                                         /* Now, what we're supposed to return is the next word... */
681                                         if (strlen(word)) {
682                                                 res = e->cmda[x-1];
683                                         } else {
684                                                 res = e->cmda[x];
685                                         }
686                                         if (res) {
687                                                 if (lock)
688                                                         ast_pthread_mutex_unlock(&clilock);
689                                                 return res ? strdup(res) : NULL;
690                                         }
691                                 }
692                         }
693                         if (e->generator && !strncasecmp(matchstr, fullcmd, strlen(fullcmd))) {
694                                 /* We have a command in its entirity within us -- theoretically only one
695                                    command can have this occur */
696                                 fullcmd = e->generator(text, word, (strlen(word) ? (x - 1) : (x)), state);
697                                 if (lock)
698                                         ast_pthread_mutex_unlock(&clilock);
699                                 return fullcmd;
700                         }
701                         
702                 }
703                 if (lock)
704                         ast_pthread_mutex_unlock(&clilock);
705                 free(dup);
706         }
707         return NULL;
708 }
709
710 char *ast_cli_generator(char *text, char *word, int state)
711 {
712         return __ast_cli_generator(text, word, state, 1);
713 }
714
715 int ast_cli_command(int fd, char *s)
716 {
717         char *argv[AST_MAX_ARGS];
718         struct ast_cli_entry *e;
719         int x;
720         char *dup;
721         x = AST_MAX_ARGS;
722         if ((dup = parse_args(s, &x, argv))) {
723                 /* We need at least one entry, or ignore */
724                 if (x > 0) {
725                         ast_pthread_mutex_lock(&clilock);
726                         e = find_cli(argv, 0);
727                         if (e)
728                                 e->inuse++;
729                         ast_pthread_mutex_unlock(&clilock);
730                         if (e) {
731                                 switch(e->handler(fd, x, argv)) {
732                                 case RESULT_SHOWUSAGE:
733                                         ast_cli(fd, e->usage);
734                                         break;
735                                 default:
736                                 }
737                         } else 
738                                 ast_cli(fd, "No such command '%s' (type 'help' for help)\n", find_best(argv));
739                         if (e) {
740                                 ast_pthread_mutex_lock(&clilock);
741                                 e->inuse--;
742                                 ast_pthread_mutex_unlock(&clilock);
743                         }
744                 }
745                 free(dup);
746         } else {
747                 ast_log(LOG_WARNING, "Out of memory\n");        
748                 return -1;
749         }
750         return 0;
751 }