Version 0.1.12 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 = AST_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 = AST_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)-1);
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                         if (e->inuse) {
445                                 ast_log(LOG_WARNING, "Can't remove command that is in use\n");
446                         } else {
447                                 /* Rewrite */
448                                 if (l)
449                                         l->next = e->next;
450                                 else
451                                         helpers = e->next;
452                                 e->next = NULL;
453                                 break;
454                         }
455                 }
456                 l = cur;
457                 cur = cur->next;
458         }
459         ast_pthread_mutex_unlock(&clilock);
460         return 0;
461 }
462
463 int ast_cli_register(struct ast_cli_entry *e)
464 {
465         struct ast_cli_entry *cur, *l=NULL;
466         char fulle[80], fulltst[80];
467         static int len;
468         ast_pthread_mutex_lock(&clilock);
469         join2(fulle, sizeof(fulle), e->cmda);
470         if (find_cli(e->cmda, -1)) {
471                 ast_pthread_mutex_unlock(&clilock);
472                 ast_log(LOG_WARNING, "Command '%s' already registered (or something close enough)\n", fulle);
473                 return -1;
474         }
475         cur = helpers;
476         while(cur) {
477                 join2(fulltst, sizeof(fulltst), cur->cmda);
478                 len = strlen(fulltst);
479                 if (strlen(fulle) < len)
480                         len = strlen(fulle);
481                 if (strncasecmp(fulle, fulltst, len) < 0) {
482                         if (l) {
483                                 e->next = l->next;
484                                 l->next = e;
485                         } else {
486                                 e->next = helpers;
487                                 helpers = e;
488                         }
489                         break;
490                 }
491                 l = cur;
492                 cur = cur->next;
493         }
494         if (!cur) {
495                 if (l)
496                         l->next = e;
497                 else
498                         helpers = e;
499                 e->next = NULL;
500         }
501         ast_pthread_mutex_unlock(&clilock);
502         return 0;
503 }
504
505 static int help_workhorse(int fd, char *match[])
506 {
507         char fullcmd1[80];
508         char fullcmd2[80];
509         char matchstr[80];
510         char *fullcmd;
511         struct ast_cli_entry *e, *e1, *e2;
512         e1 = builtins;
513         e2 = helpers;
514         if (match)
515                 join(matchstr, sizeof(matchstr), match);
516         while(e1->cmda[0] || e2) {
517                 if (e2)
518                         join(fullcmd2, sizeof(fullcmd2), e2->cmda);
519                 if (e1->cmda[0])
520                         join(fullcmd1, sizeof(fullcmd1), e1->cmda);
521                 if (!e1->cmda[0] || 
522                                 (e2 && (strcmp(fullcmd2, fullcmd1) < 0))) {
523                         /* Use e2 */
524                         e = e2;
525                         fullcmd = fullcmd2;
526                         /* Increment by going to next */
527                         e2 = e2->next;
528                 } else {
529                         /* Use e1 */
530                         e = e1;
531                         fullcmd = fullcmd1;
532                         e1++;
533                 }
534                 /* Hide commands that start with '_' */
535                 if (fullcmd[0] == '_')
536                         continue;
537                 if (match) {
538                         if (strncasecmp(matchstr, fullcmd, strlen(matchstr))) {
539                                 continue;
540                         }
541                 }
542                 ast_cli(fd, "%20.20s   %s\n", fullcmd, e->summary);
543         }
544         return 0;
545 }
546
547 static int handle_help(int fd, int argc, char *argv[]) {
548         struct ast_cli_entry *e;
549         char fullcmd[80];
550         if ((argc < 1))
551                 return RESULT_SHOWUSAGE;
552         if (argc > 1) {
553                 e = find_cli(argv + 1, 1);
554                 if (e) 
555                         ast_cli(fd, e->usage);
556                 else {
557                         if (find_cli(argv + 1, -1)) {
558                                 return help_workhorse(fd, argv + 1);
559                         } else {
560                                 join(fullcmd, sizeof(fullcmd), argv+1);
561                                 ast_cli(fd, "No such command '%s'.\n", fullcmd);
562                         }
563                 }
564         } else {
565                 return help_workhorse(fd, NULL);
566         }
567         return RESULT_SUCCESS;
568 }
569
570 static char *parse_args(char *s, int *max, char *argv[])
571 {
572         char *dup, *cur;
573         int x=0;
574         int quoted=0;
575         int escaped=0;
576         int whitespace=1;
577
578         dup = strdup(s);
579         if (dup) {
580                 cur = dup;
581                 while(*s) {
582                         switch(*s) {
583                         case '"':
584                                 /* If it's escaped, put a literal quote */
585                                 if (escaped) 
586                                         goto normal;
587                                 else 
588                                         quoted = !quoted;
589                                 if (quoted && whitespace) {
590                                         /* If we're starting a quote, coming off white space start a new word, too */
591                                         argv[x++] = cur;
592                                         whitespace=0;
593                                 }
594                                 escaped = 0;
595                                 break;
596                         case ' ':
597                         case '\t':
598                                 if (!quoted && !escaped) {
599                                         /* If we're not quoted, mark this as whitespace, and
600                                            end the previous argument */
601                                         whitespace = 1;
602                                         *(cur++) = '\0';
603                                 } else
604                                         /* Otherwise, just treat it as anything else */ 
605                                         goto normal;
606                                 break;
607                         case '\\':
608                                 /* If we're escaped, print a literal, otherwise enable escaping */
609                                 if (escaped) {
610                                         goto normal;
611                                 } else {
612                                         escaped=1;
613                                 }
614                                 break;
615                         default:
616 normal:
617                                 if (whitespace) {
618                                         if (x >= AST_MAX_ARGS -1) {
619                                                 ast_log(LOG_WARNING, "Too many arguments, truncating\n");
620                                                 break;
621                                         }
622                                         /* Coming off of whitespace, start the next argument */
623                                         argv[x++] = cur;
624                                         whitespace=0;
625                                 }
626                                 *(cur++) = *s;
627                                 escaped=0;
628                         }
629                         s++;
630                 }
631                 /* Null terminate */
632                 *(cur++) = '\0';
633                 argv[x] = NULL;
634                 *max = x;
635         }
636         return dup;
637 }
638
639 static char *__ast_cli_generator(char *text, char *word, int state, int lock)
640 {
641         char *argv[AST_MAX_ARGS];
642         struct ast_cli_entry *e, *e1, *e2;
643         int x;
644         int matchnum=0;
645         char *dup, *res;
646         char fullcmd1[80];
647         char fullcmd2[80];
648         char matchstr[80];
649         char *fullcmd;
650
651         if ((dup = parse_args(text, &x, argv))) {
652                 join(matchstr, sizeof(matchstr), argv);
653                 if (lock)
654                         ast_pthread_mutex_lock(&clilock);
655                 e1 = builtins;
656                 e2 = helpers;
657                 while(e1->cmda[0] || e2) {
658                         if (e2)
659                                 join(fullcmd2, sizeof(fullcmd2), e2->cmda);
660                         if (e1->cmda[0])
661                                 join(fullcmd1, sizeof(fullcmd1), e1->cmda);
662                         if (!e1->cmda[0] || 
663                                         (e2 && (strcmp(fullcmd2, fullcmd1) < 0))) {
664                                 /* Use e2 */
665                                 e = e2;
666                                 fullcmd = fullcmd2;
667                                 /* Increment by going to next */
668                                 e2 = e2->next;
669                         } else {
670                                 /* Use e1 */
671                                 e = e1;
672                                 fullcmd = fullcmd1;
673                                 e1++;
674                         }
675                         if ((fullcmd[0] != '_') && !strncasecmp(matchstr, fullcmd, strlen(matchstr))) {
676                                 /* We contain the first part of one or more commands */
677                                 matchnum++;
678                                 if (matchnum > state) {
679                                         /* Now, what we're supposed to return is the next word... */
680                                         if (strlen(word)) {
681                                                 res = e->cmda[x-1];
682                                         } else {
683                                                 res = e->cmda[x];
684                                         }
685                                         if (res) {
686                                                 if (lock)
687                                                         ast_pthread_mutex_unlock(&clilock);
688                                                 return res ? strdup(res) : NULL;
689                                         }
690                                 }
691                         }
692                         if (e->generator && !strncasecmp(matchstr, fullcmd, strlen(fullcmd))) {
693                                 /* We have a command in its entirity within us -- theoretically only one
694                                    command can have this occur */
695                                 fullcmd = e->generator(text, word, (strlen(word) ? (x - 1) : (x)), state);
696                                 if (lock)
697                                         ast_pthread_mutex_unlock(&clilock);
698                                 return fullcmd;
699                         }
700                         
701                 }
702                 if (lock)
703                         ast_pthread_mutex_unlock(&clilock);
704                 free(dup);
705         }
706         return NULL;
707 }
708
709 char *ast_cli_generator(char *text, char *word, int state)
710 {
711         return __ast_cli_generator(text, word, state, 1);
712 }
713
714 int ast_cli_command(int fd, char *s)
715 {
716         char *argv[AST_MAX_ARGS];
717         struct ast_cli_entry *e;
718         int x;
719         char *dup;
720         x = AST_MAX_ARGS;
721         if ((dup = parse_args(s, &x, argv))) {
722                 /* We need at least one entry, or ignore */
723                 if (x > 0) {
724                         ast_pthread_mutex_lock(&clilock);
725                         e = find_cli(argv, 0);
726                         if (e)
727                                 e->inuse++;
728                         ast_pthread_mutex_unlock(&clilock);
729                         if (e) {
730                                 switch(e->handler(fd, x, argv)) {
731                                 case RESULT_SHOWUSAGE:
732                                         ast_cli(fd, e->usage);
733                                         break;
734                                 default:
735                                 }
736                         } else 
737                                 ast_cli(fd, "No such command '%s' (type 'help' for help)\n", find_best(argv));
738                         if (e) {
739                                 ast_pthread_mutex_lock(&clilock);
740                                 e->inuse--;
741                                 ast_pthread_mutex_unlock(&clilock);
742                         }
743                 }
744                 free(dup);
745         } else {
746                 ast_log(LOG_WARNING, "Out of memory\n");        
747                 return -1;
748         }
749         return 0;
750 }