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