18aadd871c4a7514d7b5eac76a84b02b42adc037
[asterisk/asterisk.git] / cli.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Standard Command Line Interface
5  * 
6  * Copyright (C) 1999 - 2005, Digium, Inc.
7  *
8  * Mark Spencer <markster@digium.com>
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 <sys/signal.h>
17 #include <stdio.h>
18 #include <signal.h>
19 #include <string.h>
20 #include <ctype.h>
21
22 #include "asterisk/logger.h"
23 #include "asterisk/options.h"
24 #include "asterisk/cli.h"
25 #include "asterisk/module.h"
26 #include "asterisk/pbx.h"
27 #include "asterisk/channel.h"
28 #include "asterisk/manager.h"
29 #include "asterisk/utils.h"
30 #include "asterisk/lock.h"
31 /* For rl_filename_completion */
32 #include "editline/readline/readline.h"
33 /* For module directory */
34 #include "asterisk.h"
35 #include "asterisk/version.h"
36 #include "asterisk/build.h"
37
38 #define VERSION_INFO "Asterisk " ASTERISK_VERSION " built by " BUILD_USER "@" BUILD_HOSTNAME \
39         " on a " BUILD_MACHINE " running " BUILD_OS " on " BUILD_DATE
40         
41 extern unsigned long global_fin, global_fout;
42         
43 void ast_cli(int fd, char *fmt, ...)
44 {
45         char *stuff;
46         int res = 0;
47
48         va_list ap;
49         va_start(ap, fmt);
50 #ifdef SOLARIS
51         stuff = (char *)malloc(10240);
52         vsnprintf(stuff, 10240, fmt, ap);
53 #else
54         res = vasprintf(&stuff, fmt, ap);
55 #endif
56         va_end(ap);
57         if (res == -1) {
58                 ast_log(LOG_ERROR, "Out of memory\n");
59         } else {
60                 ast_carefulwrite(fd, stuff, strlen(stuff), 100);
61                 free(stuff);
62         }
63 }
64
65 AST_MUTEX_DEFINE_STATIC(clilock);
66
67 struct ast_cli_entry *helpers = NULL;
68
69 static char load_help[] = 
70 "Usage: load <module name>\n"
71 "       Loads the specified module into Asterisk.\n";
72
73 static char unload_help[] = 
74 "Usage: unload [-f|-h] <module name>\n"
75 "       Unloads the specified module from Asterisk.  The -f\n"
76 "       option causes the module to be unloaded even if it is\n"
77 "       in use (may cause a crash) and the -h module causes the\n"
78 "       module to be unloaded even if the module says it cannot, \n"
79 "       which almost always will cause a crash.\n";
80
81 static char help_help[] =
82 "Usage: help [topic]\n"
83 "       When called with a topic as an argument, displays usage\n"
84 "       information on the given command.  If called without a\n"
85 "       topic, it provides a list of commands.\n";
86
87 static char chanlist_help[] = 
88 "Usage: show channels [concise]\n"
89 "       Lists currently defined channels and some information about\n"
90 "       them.  If 'concise' is specified, format is abridged and in\n"
91 "       a more easily machine parsable format\n";
92
93 static char reload_help[] = 
94 "Usage: reload [module ...]\n"
95 "       Reloads configuration files for all listed modules which support\n"
96 "       reloading, or for all supported modules if none are listed.\n";
97
98 static char set_verbose_help[] = 
99 "Usage: set verbose <level>\n"
100 "       Sets level of verbose messages to be displayed.  0 means\n"
101 "       no messages should be displayed. Equivalent to -v[v[v...]]\n"
102 "       on startup\n";
103
104 static char set_debug_help[] = 
105 "Usage: set debug <level>\n"
106 "       Sets level of core debug messages to be displayed.  0 means\n"
107 "       no messages should be displayed. Equivalent to -d[d[d...]]\n"
108 "       on startup.\n";
109
110 static char softhangup_help[] =
111 "Usage: soft hangup <channel>\n"
112 "       Request that a channel be hung up.  The hangup takes effect\n"
113 "       the next time the driver reads or writes from the channel\n";
114
115 static int handle_load(int fd, int argc, char *argv[])
116 {
117         if (argc != 2)
118                 return RESULT_SHOWUSAGE;
119         if (ast_load_resource(argv[1])) {
120                 ast_cli(fd, "Unable to load module %s\n", argv[1]);
121                 return RESULT_FAILURE;
122         }
123         return RESULT_SUCCESS;
124 }
125
126 static int handle_reload(int fd, int argc, char *argv[])
127 {
128         int x;
129         int res;
130         if (argc < 1)
131                 return RESULT_SHOWUSAGE;
132         if (argc > 1) { 
133                 for (x=1;x<argc;x++) {
134                         res = ast_module_reload(argv[x]);
135                         switch(res) {
136                         case 0:
137                                 ast_cli(fd, "No such module '%s'\n", argv[x]);
138                                 break;
139                         case 1:
140                                 ast_cli(fd, "Module '%s' does not support reload\n", argv[x]);
141                                 break;
142                         }
143                 }
144         } else
145                 ast_module_reload(NULL);
146         return RESULT_SUCCESS;
147 }
148
149 static int handle_set_verbose(int fd, int argc, char *argv[])
150 {
151         int val = 0;
152         int oldval = 0;
153
154         /* Has a hidden 'at least' argument */
155         if ((argc != 3) && (argc != 4))
156                 return RESULT_SHOWUSAGE;
157         if ((argc == 4) && strcasecmp(argv[2], "atleast"))
158                 return RESULT_SHOWUSAGE;
159         oldval = option_verbose;
160         if (argc == 3)
161                 option_verbose = atoi(argv[2]);
162         else {
163                 val = atoi(argv[3]);
164                 if (val > option_verbose)
165                         option_verbose = val;
166         }
167         if (oldval != option_verbose && option_verbose > 0)
168                 ast_cli(fd, "Verbosity was %d and is now %d\n", oldval, option_verbose);
169         else if (oldval > 0 && option_verbose > 0)
170                 ast_cli(fd, "Verbosity is at least %d\n", option_verbose);
171         else if (oldval > 0 && option_verbose == 0)
172                 ast_cli(fd, "Verbosity is now OFF\n");
173         return RESULT_SUCCESS;
174 }
175
176 static int handle_set_debug(int fd, int argc, char *argv[])
177 {
178         int val = 0;
179         int oldval = 0;
180         /* Has a hidden 'at least' argument */
181         if ((argc != 3) && (argc != 4))
182                 return RESULT_SHOWUSAGE;
183         if ((argc == 4) && strcasecmp(argv[2], "atleast"))
184                 return RESULT_SHOWUSAGE;
185         oldval = option_debug;
186         if (argc == 3)
187                 option_debug = atoi(argv[2]);
188         else {
189                 val = atoi(argv[3]);
190                 if (val > option_debug)
191                         option_debug = val;
192         }
193         if (oldval != option_debug && option_debug > 0)
194                 ast_cli(fd, "Core debug was %d and is now %d\n", oldval, option_debug);
195         else if (oldval > 0 && option_debug > 0)
196                 ast_cli(fd, "Core debug is at least %d\n", option_debug);
197         else if (oldval > 0 && option_debug == 0)
198                 ast_cli(fd, "Core debug is now OFF\n");
199         return RESULT_SUCCESS;
200 }
201
202 static int handle_unload(int fd, int argc, char *argv[])
203 {
204         int x;
205         int force=AST_FORCE_SOFT;
206         if (argc < 2)
207                 return RESULT_SHOWUSAGE;
208         for (x=1;x<argc;x++) {
209                 if (argv[x][0] == '-') {
210                         switch(argv[x][1]) {
211                         case 'f':
212                                 force = AST_FORCE_FIRM;
213                                 break;
214                         case 'h':
215                                 force = AST_FORCE_HARD;
216                                 break;
217                         default:
218                                 return RESULT_SHOWUSAGE;
219                         }
220                 } else if (x !=  argc - 1) 
221                         return RESULT_SHOWUSAGE;
222                 else if (ast_unload_resource(argv[x], force)) {
223                         ast_cli(fd, "Unable to unload resource %s\n", argv[x]);
224                         return RESULT_FAILURE;
225                 }
226         }
227         return RESULT_SUCCESS;
228 }
229
230 #define MODLIST_FORMAT  "%-25s %-40.40s %-10d\n"
231 #define MODLIST_FORMAT2 "%-25s %-40.40s %-10s\n"
232
233 AST_MUTEX_DEFINE_STATIC(climodentrylock);
234 static int climodentryfd = -1;
235
236 static int modlist_modentry(char *module, char *description, int usecnt, char *like)
237 {
238         /* Comparing the like with the module */
239         if ( strstr(module,like) != NULL) {
240                 ast_cli(climodentryfd, MODLIST_FORMAT, module, description, usecnt);
241                 return 1;
242                 
243         } 
244         return 0;
245 }
246
247 static char modlist_help[] =
248 "Usage: show modules [like keyword]\n"
249 "       Shows Asterisk modules currently in use, and usage statistics.\n";
250
251 static char version_help[] =
252 "Usage: show version\n"
253 "       Shows Asterisk version information.\n";
254
255 static char uptime_help[] =
256 "Usage: show uptime [seconds]\n"
257 "       Shows Asterisk uptime information.\n"
258 "       The seconds word returns the uptime in seconds only.\n";
259
260 static char *format_uptimestr(time_t timeval)
261 {
262         int years = 0, weeks = 0, days = 0, hours = 0, mins = 0, secs = 0;
263         char timestr[256]="";
264         int bytes = 0;
265         int maxbytes = 0;
266         int offset = 0;
267 #define SECOND (1)
268 #define MINUTE (SECOND*60)
269 #define HOUR (MINUTE*60)
270 #define DAY (HOUR*24)
271 #define WEEK (DAY*7)
272 #define YEAR (DAY*365)
273 #define ESS(x) ((x == 1) ? "" : "s")
274
275         maxbytes = sizeof(timestr);
276         if (timeval < 0)
277                 return NULL;
278         if (timeval > YEAR) {
279                 years = (timeval / YEAR);
280                 timeval -= (years * YEAR);
281                 if (years > 0) {
282                         snprintf(timestr + offset, maxbytes, "%d year%s, ", years, ESS(years));
283                         bytes = strlen(timestr + offset);
284                         offset += bytes;
285                         maxbytes -= bytes;
286                 }
287         }
288         if (timeval > WEEK) {
289                 weeks = (timeval / WEEK);
290                 timeval -= (weeks * WEEK);
291                 if (weeks > 0) {
292                         snprintf(timestr + offset, maxbytes, "%d week%s, ", weeks, ESS(weeks));
293                         bytes = strlen(timestr + offset);
294                         offset += bytes;
295                         maxbytes -= bytes;
296                 }
297         }
298         if (timeval > DAY) {
299                 days = (timeval / DAY);
300                 timeval -= (days * DAY);
301                 if (days > 0) {
302                         snprintf(timestr + offset, maxbytes, "%d day%s, ", days, ESS(days));
303                         bytes = strlen(timestr + offset);
304                         offset += bytes;
305                         maxbytes -= bytes;
306                 }
307         }
308         if (timeval > HOUR) {
309                 hours = (timeval / HOUR);
310                 timeval -= (hours * HOUR);
311                 if (hours > 0) {
312                         snprintf(timestr + offset, maxbytes, "%d hour%s, ", hours, ESS(hours));
313                         bytes = strlen(timestr + offset);
314                         offset += bytes;
315                         maxbytes -= bytes;
316                 }
317         }
318         if (timeval > MINUTE) {
319                 mins = (timeval / MINUTE);
320                 timeval -= (mins * MINUTE);
321                 if (mins > 0) {
322                         snprintf(timestr + offset, maxbytes, "%d minute%s, ", mins, ESS(mins));
323                         bytes = strlen(timestr + offset);
324                         offset += bytes;
325                         maxbytes -= bytes;
326                 }
327         }
328         secs = timeval;
329
330         if (secs > 0) {
331                 snprintf(timestr + offset, maxbytes, "%d second%s", secs, ESS(secs));
332         }
333
334         return timestr ? strdup(timestr) : NULL;
335 }
336
337 static int handle_showuptime(int fd, int argc, char *argv[])
338 {
339         time_t curtime, tmptime;
340         char *timestr;
341         int printsec;
342
343         printsec = ((argc == 3) && (!strcasecmp(argv[2],"seconds")));
344         if ((argc != 2) && (!printsec))
345                 return RESULT_SHOWUSAGE;
346
347         time(&curtime);
348         if (ast_startuptime) {
349                 tmptime = curtime - ast_startuptime;
350                 if (printsec) {
351                         ast_cli(fd, "System uptime: %lu\n",tmptime);
352                 } else {
353                         timestr = format_uptimestr(tmptime);
354                         if (timestr) {
355                                 ast_cli(fd, "System uptime: %s\n", timestr);
356                                 free(timestr);
357                         }
358                 }
359         }               
360         if (ast_lastreloadtime) {
361                 tmptime = curtime - ast_lastreloadtime;
362                 if (printsec) {
363                         ast_cli(fd, "Last reload: %lu\n", tmptime);
364                 } else {
365                         timestr = format_uptimestr(tmptime);
366                         if ((timestr) && (!printsec)) {
367                                 ast_cli(fd, "Last reload: %s\n", timestr);
368                                 free(timestr);
369                         } 
370                 }
371         }
372         return RESULT_SUCCESS;
373 }
374
375 static int handle_modlist(int fd, int argc, char *argv[])
376 {
377         char *like = "";
378         if (argc == 3)
379                 return RESULT_SHOWUSAGE;
380         else if (argc >= 4) {
381                 if (strcmp(argv[2],"like")) 
382                         return RESULT_SHOWUSAGE;
383                 like = argv[3];
384         }
385                 
386         ast_mutex_lock(&climodentrylock);
387         climodentryfd = fd;
388         ast_cli(fd, MODLIST_FORMAT2, "Module", "Description", "Use Count");
389         ast_cli(fd,"%d modules loaded\n",ast_update_module_list(modlist_modentry,like));
390         climodentryfd = -1;
391         ast_mutex_unlock(&climodentrylock);
392         return RESULT_SUCCESS;
393 }
394
395 static int handle_version(int fd, int argc, char *argv[])
396 {
397         if (argc != 2)
398                 return RESULT_SHOWUSAGE;
399         ast_cli(fd, "%s\n", VERSION_INFO);
400         return RESULT_SUCCESS;
401 }
402 static int handle_chanlist(int fd, int argc, char *argv[])
403 {
404 #define FORMAT_STRING  "%15s  (%-10s %-12s %-4d) %7s %-12s  %-15s\n"
405 #define FORMAT_STRING2 "%15s  (%-10s %-12s %-4s) %7s %-12s  %-15s\n"
406 #define CONCISE_FORMAT_STRING  "%s:%s:%s:%d:%s:%s:%s:%s:%s:%d\n"
407
408         struct ast_channel *c=NULL;
409         int numchans = 0;
410         int concise = 0;
411         if (argc < 2 || argc > 3)
412                 return RESULT_SHOWUSAGE;
413         
414         concise = (argc == 3 && (!strcasecmp(argv[2],"concise")));
415         if(!concise)
416                 ast_cli(fd, FORMAT_STRING2, "Channel", "Context", "Extension", "Pri", "State", "Appl.", "Data");
417         while ( (c = ast_channel_walk_locked(c)) != NULL) {
418                 if(concise)
419                         ast_cli(fd, CONCISE_FORMAT_STRING, c->name, c->context, c->exten, c->priority, ast_state2str(c->_state),
420                                         c->appl ? c->appl : "(None)", c->data ? ( !ast_strlen_zero(c->data) ? c->data : "" ): "",
421                                         (c->cid.cid_num && !ast_strlen_zero(c->cid.cid_num)) ? c->cid.cid_num : "",
422                                         (c->accountcode && !ast_strlen_zero(c->accountcode)) ? c->accountcode : "",c->amaflags);
423                 else
424                         ast_cli(fd, FORMAT_STRING, c->name, c->context, c->exten, c->priority, ast_state2str(c->_state),
425                                         c->appl ? c->appl : "(None)", c->data ? ( !ast_strlen_zero(c->data) ? c->data : "(Empty)" ): "(None)");
426
427                 numchans++;
428                 ast_mutex_unlock(&c->lock);
429         }
430         if(!concise) {
431                 ast_cli(fd, "%d active channel(s)\n", numchans);
432                 if (option_maxcalls)
433                         ast_cli(fd, "%d of %d max active call(s) (%5.2f%% of capacity)\n", ast_active_calls(), option_maxcalls, ((float)ast_active_calls() / (float)option_maxcalls) * 100.0);
434                 else
435                         ast_cli(fd, "%d active call(s)\n", ast_active_calls());
436         }
437         return RESULT_SUCCESS;
438 }
439
440 static char showchan_help[] = 
441 "Usage: show channel <channel>\n"
442 "       Shows lots of information about the specified channel.\n";
443
444 static char debugchan_help[] = 
445 "Usage: debug channel <channel>\n"
446 "       Enables debugging on a specific channel.\n";
447
448 static char debuglevel_help[] = 
449 "Usage: debug level <level> [filename]\n"
450 "       Set debug to specified level (0 to disable).  If filename\n"
451 "is specified, debugging will be limited to just that file.\n";
452
453 static char nodebugchan_help[] = 
454 "Usage: no debug channel <channel>\n"
455 "       Disables debugging on a specific channel.\n";
456
457 static char commandcomplete_help[] = 
458 "Usage: _command complete \"<line>\" text state\n"
459 "       This function is used internally to help with command completion and should.\n"
460 "       never be called by the user directly.\n";
461
462 static char commandnummatches_help[] = 
463 "Usage: _command nummatches \"<line>\" text \n"
464 "       This function is used internally to help with command completion and should.\n"
465 "       never be called by the user directly.\n";
466
467 static char commandmatchesarray_help[] = 
468 "Usage: _command matchesarray \"<line>\" text \n"
469 "       This function is used internally to help with command completion and should.\n"
470 "       never be called by the user directly.\n";
471
472 static int handle_softhangup(int fd, int argc, char *argv[])
473 {
474         struct ast_channel *c=NULL;
475         if (argc != 3)
476                 return RESULT_SHOWUSAGE;
477         c = ast_get_channel_by_name_locked(argv[2]);
478         if (c) {
479                 ast_cli(fd, "Requested Hangup on channel '%s'\n", c->name);
480                 ast_softhangup(c, AST_SOFTHANGUP_EXPLICIT);
481                 ast_mutex_unlock(&c->lock);
482         } else
483                 ast_cli(fd, "%s is not a known channel\n", argv[2]);
484         return RESULT_SUCCESS;
485 }
486
487 static char *__ast_cli_generator(char *text, char *word, int state, int lock);
488
489 static int handle_commandmatchesarray(int fd, int argc, char *argv[])
490 {
491         char *buf, *obuf;
492         int buflen = 2048;
493         int len = 0;
494         char **matches;
495         int x, matchlen;
496
497         if (argc != 4)
498                 return RESULT_SHOWUSAGE;
499         buf = malloc(buflen);
500         if (!buf)
501                 return RESULT_FAILURE;
502         buf[len] = '\0';
503         matches = ast_cli_completion_matches(argv[2], argv[3]);
504         if (matches) {
505                 for (x=0; matches[x]; x++) {
506 #if 0
507                         printf("command matchesarray for '%s' %s got '%s'\n", argv[2], argv[3], matches[x]);
508 #endif
509                         matchlen = strlen(matches[x]) + 1;
510                         if (len + matchlen >= buflen) {
511                                 buflen += matchlen * 3;
512                                 obuf = buf;
513                                 buf = realloc(obuf, buflen);
514                                 if (!buf) 
515                                         /* Out of memory...  Just free old buffer and be done */
516                                         free(obuf);
517                         }
518                         if (buf)
519                                 len += sprintf( buf + len, "%s ", matches[x]);
520                         free(matches[x]);
521                         matches[x] = NULL;
522                 }
523                 free(matches);
524         }
525 #if 0
526         printf("array for '%s' %s got '%s'\n", argv[2], argv[3], buf);
527 #endif
528         
529         if (buf) {
530                 ast_cli(fd, "%s%s",buf, AST_CLI_COMPLETE_EOF);
531                 free(buf);
532         } else
533                 ast_cli(fd, "NULL\n");
534
535         return RESULT_SUCCESS;
536 }
537
538
539
540 static int handle_commandnummatches(int fd, int argc, char *argv[])
541 {
542         int matches = 0;
543
544         if (argc != 4)
545                 return RESULT_SHOWUSAGE;
546
547         matches = ast_cli_generatornummatches(argv[2], argv[3]);
548
549 #if 0
550         printf("Search for '%s' %s got '%d'\n", argv[2], argv[3], matches);
551 #endif
552         ast_cli(fd, "%d", matches);
553
554         return RESULT_SUCCESS;
555 }
556
557 static int handle_commandcomplete(int fd, int argc, char *argv[])
558 {
559         char *buf;
560 #if 0
561         printf("Search for %d args: '%s', '%s', '%s', '%s'\n", argc, argv[0], argv[1], argv[2], argv[3]);
562 #endif  
563         if (argc != 5)
564                 return RESULT_SHOWUSAGE;
565         buf = __ast_cli_generator(argv[2], argv[3], atoi(argv[4]), 0);
566 #if 0
567         printf("Search for '%s' %s %d got '%s'\n", argv[2], argv[3], atoi(argv[4]), buf);
568 #endif  
569         if (buf) {
570                 ast_cli(fd, buf);
571                 free(buf);
572         } else
573                 ast_cli(fd, "NULL\n");
574         return RESULT_SUCCESS;
575 }
576
577 static int handle_debuglevel(int fd, int argc, char *argv[])
578 {
579         int newlevel;
580         char *filename = "<any>";
581         if ((argc < 3) || (argc > 4))
582                 return RESULT_SHOWUSAGE;
583         if (sscanf(argv[2], "%d", &newlevel) != 1)
584                 return RESULT_SHOWUSAGE;
585         option_debug = newlevel;
586         if (argc == 4) {
587                 filename = argv[3];
588                 strncpy(debug_filename, filename, sizeof(debug_filename) - 1);
589         } else {
590                 debug_filename[0] = '\0';
591         }
592         ast_cli(fd, "Debugging level set to %d, file '%s'\n", newlevel, filename);
593         return RESULT_SUCCESS;
594 }
595
596 #define DEBUGCHAN_FLAG  0x80000000
597 /* XXX todo: merge next two functions!!! */
598 static int handle_debugchan(int fd, int argc, char *argv[])
599 {
600         struct ast_channel *c=NULL;
601         int is_all;
602         if (argc != 3)
603                 return RESULT_SHOWUSAGE;
604
605         is_all = !strcasecmp("all", argv[2]);
606         if (is_all) {
607                 global_fin |= DEBUGCHAN_FLAG;
608                 global_fout |= DEBUGCHAN_FLAG;
609                 c = ast_channel_walk_locked(NULL);
610         } else {
611                 c = ast_get_channel_by_name_locked(argv[2]);
612                 if (c == NULL)
613                         ast_cli(fd, "No such channel %s\n", argv[2]);
614         }
615         while(c) {
616                 if (!(c->fin & DEBUGCHAN_FLAG) || !(c->fout & DEBUGCHAN_FLAG)) {
617                         c->fin |= DEBUGCHAN_FLAG;
618                         c->fout |= DEBUGCHAN_FLAG;
619                         ast_cli(fd, "Debugging enabled on channel %s\n", c->name);
620                 }
621                 ast_mutex_unlock(&c->lock);
622                 if (!is_all)
623                         break;
624                 c = ast_channel_walk_locked(c);
625         }
626         ast_cli(fd, "Debugging on new channels is enabled\n");
627         return RESULT_SUCCESS;
628 }
629
630 static int handle_nodebugchan(int fd, int argc, char *argv[])
631 {
632         struct ast_channel *c=NULL;
633         int is_all;
634         if (argc != 4)
635                 return RESULT_SHOWUSAGE;
636         is_all = !strcasecmp("all", argv[3]);
637         if (is_all) {
638                 global_fin &= ~DEBUGCHAN_FLAG;
639                 global_fout &= ~DEBUGCHAN_FLAG;
640                 c = ast_channel_walk_locked(NULL);
641         } else {
642                 c = ast_get_channel_by_name_locked(argv[3]);
643                 if (c == NULL)
644                         ast_cli(fd, "No such channel %s\n", argv[3]);
645     }
646         while(c) {
647                 if ((c->fin & DEBUGCHAN_FLAG) || (c->fout & DEBUGCHAN_FLAG)) {
648                         c->fin &= ~DEBUGCHAN_FLAG;
649                         c->fout &= ~DEBUGCHAN_FLAG;
650                         ast_cli(fd, "Debugging disabled on channel %s\n", c->name);
651                 }
652                 ast_mutex_unlock(&c->lock);
653                 if (!is_all)
654                         break;
655                 c = ast_channel_walk_locked(c);
656         }
657         ast_cli(fd, "Debugging on new channels is disabled\n");
658         return RESULT_SUCCESS;
659 }
660                 
661         
662
663 static int handle_showchan(int fd, int argc, char *argv[])
664 {
665         struct ast_channel *c=NULL;
666         struct timeval now;
667         char buf[2048];
668         char cdrtime[256];
669         long elapsed_seconds=0;
670         int hour=0, min=0, sec=0;
671         
672         if (argc != 3)
673                 return RESULT_SHOWUSAGE;
674         gettimeofday(&now, NULL);
675         c = ast_get_channel_by_name_locked(argv[2]);
676         if (!c) {
677                 ast_cli(fd, "%s is not a known channel\n", argv[2]);
678                 return RESULT_SUCCESS;
679         }
680         if(c->cdr) {
681                 elapsed_seconds = now.tv_sec - c->cdr->start.tv_sec;
682                 hour = elapsed_seconds / 3600;
683                 min = (elapsed_seconds % 3600) / 60;
684                 sec = elapsed_seconds % 60;
685                 snprintf(cdrtime, sizeof(cdrtime), "%dh%dm%ds", hour, min, sec);
686         } else
687                 strncpy(cdrtime, "N/A", sizeof(cdrtime) -1);
688         ast_cli(fd, 
689                 " -- General --\n"
690                 "           Name: %s\n"
691                 "           Type: %s\n"
692                 "       UniqueID: %s\n"
693                 "      Caller ID: %s\n"
694                 " Caller ID Name: %s\n"
695                 "    DNID Digits: %s\n"
696                 "          State: %s (%d)\n"
697                 "          Rings: %d\n"
698                 "   NativeFormat: %d\n"
699                 "    WriteFormat: %d\n"
700                 "     ReadFormat: %d\n"
701                 "1st File Descriptor: %d\n"
702                 "      Frames in: %d%s\n"
703                 "     Frames out: %d%s\n"
704                 " Time to Hangup: %ld\n"
705                 "   Elapsed Time: %s\n"
706                 "  Direct Bridge: %s\n"
707                 "Indirect Bridge: %s\n"
708                 " --   PBX   --\n"
709                 "        Context: %s\n"
710                 "      Extension: %s\n"
711                 "       Priority: %d\n"
712                 "     Call Group: %d\n"
713                 "   Pickup Group: %d\n"
714                 "    Application: %s\n"
715                 "           Data: %s\n"
716                 "    Blocking in: %s\n",
717                 c->name, c->type, c->uniqueid,
718                 (c->cid.cid_num ? c->cid.cid_num : "(N/A)"),
719                 (c->cid.cid_name ? c->cid.cid_name : "(N/A)"),
720                 (c->cid.cid_dnid ? c->cid.cid_dnid : "(N/A)" ), ast_state2str(c->_state), c->_state, c->rings, c->nativeformats, c->writeformat, c->readformat,
721                 c->fds[0], c->fin & 0x7fffffff, (c->fin & 0x80000000) ? " (DEBUGGED)" : "",
722                 c->fout & 0x7fffffff, (c->fout & 0x80000000) ? " (DEBUGGED)" : "", (long)c->whentohangup,
723                 cdrtime, c->_bridge ? c->_bridge->name : "<none>", ast_bridged_channel(c) ? ast_bridged_channel(c)->name : "<none>", 
724                 c->context, c->exten, c->priority, c->callgroup, c->pickupgroup, ( c->appl ? c->appl : "(N/A)" ),
725                 ( c-> data ? (!ast_strlen_zero(c->data) ? c->data : "(Empty)") : "(None)"),
726                 (ast_test_flag(c, AST_FLAG_BLOCKING) ? c->blockproc : "(Not Blocking)"));
727         
728         if(pbx_builtin_serialize_variables(c,buf,sizeof(buf)))
729                 ast_cli(fd,"      Variables:\n%s\n",buf);
730         if(c->cdr && ast_cdr_serialize_variables(c->cdr,buf, sizeof(buf), '=', '\n', 1))
731                 ast_cli(fd,"  CDR Variables:\n%s\n",buf);
732         
733         ast_mutex_unlock(&c->lock);
734         return RESULT_SUCCESS;
735 }
736
737 static char *complete_ch_helper(char *line, char *word, int pos, int state, int rpos)
738 {
739         struct ast_channel *c = NULL;
740         int which=0;
741         char *ret = NULL;
742
743         if (pos != rpos)
744                 return NULL;
745         while ( (c = ast_channel_walk_locked(c)) != NULL) {
746                 if (!strncasecmp(word, c->name, strlen(word))) {
747                         if (++which > state) {
748                                 ret = strdup(c->name);
749                                 ast_mutex_unlock(&c->lock);
750                                 break;
751                         }
752                 }
753                 ast_mutex_unlock(&c->lock);
754         }
755         return ret;
756 }
757
758 static char *complete_ch_3(char *line, char *word, int pos, int state)
759 {
760         return complete_ch_helper(line, word, pos, state, 2);
761 }
762
763 static char *complete_ch_4(char *line, char *word, int pos, int state)
764 {
765         return complete_ch_helper(line, word, pos, state, 3);
766 }
767
768 static char *complete_mod_2(char *line, char *word, int pos, int state)
769 {
770         return ast_module_helper(line, word, pos, state, 1, 1);
771 }
772
773 static char *complete_mod_4(char *line, char *word, int pos, int state)
774 {
775         return ast_module_helper(line, word, pos, state, 3, 0);
776 }
777
778 static char *complete_fn(char *line, char *word, int pos, int state)
779 {
780         char *c;
781         char filename[256];
782         if (pos != 1)
783                 return NULL;
784         if (word[0] == '/')
785                 strncpy(filename, word, sizeof(filename)-1);
786         else
787                 snprintf(filename, sizeof(filename), "%s/%s", (char *)ast_config_AST_MODULE_DIR, word);
788         c = (char*)filename_completion_function(filename, state);
789         if (c && word[0] != '/')
790                 c += (strlen((char*)ast_config_AST_MODULE_DIR) + 1);
791         return c ? strdup(c) : c;
792 }
793
794 static int handle_help(int fd, int argc, char *argv[]);
795
796 static struct ast_cli_entry builtins[] = {
797         /* Keep alphabetized, with longer matches first (example: abcd before abc) */
798         { { "_command", "complete", NULL }, handle_commandcomplete, "Command complete", commandcomplete_help },
799         { { "_command", "nummatches", NULL }, handle_commandnummatches, "Returns number of command matches", commandnummatches_help },
800         { { "_command", "matchesarray", NULL }, handle_commandmatchesarray, "Returns command matches array", commandmatchesarray_help },
801         { { "debug", "channel", NULL }, handle_debugchan, "Enable debugging on a channel", debugchan_help, complete_ch_3 },
802         { { "debug", "level", NULL }, handle_debuglevel, "Set global debug level", debuglevel_help },
803         { { "help", NULL }, handle_help, "Display help list, or specific help on a command", help_help },
804         { { "load", NULL }, handle_load, "Load a dynamic module by name", load_help, complete_fn },
805         { { "no", "debug", "channel", NULL }, handle_nodebugchan, "Disable debugging on a channel", nodebugchan_help, complete_ch_4 },
806         { { "reload", NULL }, handle_reload, "Reload configuration", reload_help, complete_mod_2 },
807         { { "set", "debug", NULL }, handle_set_debug, "Set level of debug chattiness", set_debug_help },
808         { { "set", "verbose", NULL }, handle_set_verbose, "Set level of verboseness", set_verbose_help },
809         { { "show", "channel", NULL }, handle_showchan, "Display information on a specific channel", showchan_help, complete_ch_3 },
810         { { "show", "channels", NULL }, handle_chanlist, "Display information on channels", chanlist_help },
811         { { "show", "modules", NULL }, handle_modlist, "List modules and info", modlist_help },
812         { { "show", "modules", "like", NULL }, handle_modlist, "List modules and info", modlist_help, complete_mod_4 },
813         { { "show", "uptime", NULL }, handle_showuptime, "Show uptime information", uptime_help },
814         { { "show", "version", NULL }, handle_version, "Display version info", version_help },
815         { { "soft", "hangup", NULL }, handle_softhangup, "Request a hangup on a given channel", softhangup_help, complete_ch_3 },
816         { { "unload", NULL }, handle_unload, "Unload a dynamic module by name", unload_help, complete_fn },
817         { { NULL }, NULL, NULL, NULL }
818 };
819
820 static struct ast_cli_entry *find_cli(char *cmds[], int exact)
821 {
822         int x;
823         int y;
824         int match;
825         struct ast_cli_entry *e=NULL;
826         for (x=0;builtins[x].cmda[0];x++) {
827                 /* start optimistic */
828                 match = 1;
829                 for (y=0;match && cmds[y]; y++) {
830                         /* If there are no more words in the candidate command, then we're
831                            there.  */
832                         if (!builtins[x].cmda[y] && !exact)
833                                 break;
834                         /* If there are no more words in the command (and we're looking for
835                            an exact match) or there is a difference between the two words,
836                            then this is not a match */
837                         if (!builtins[x].cmda[y] || strcasecmp(builtins[x].cmda[y], cmds[y]))
838                                 match = 0;
839                 }
840                 /* If more words are needed to complete the command then this is not
841                    a candidate (unless we're looking for a really inexact answer  */
842                 if ((exact > -1) && builtins[x].cmda[y])
843                         match = 0;
844                 if (match)
845                         return &builtins[x];
846         }
847         for (e=helpers;e;e=e->next) {
848                 match = 1;
849                 for (y=0;match && cmds[y]; y++) {
850                         if (!e->cmda[y] && !exact)
851                                 break;
852                         if (!e->cmda[y] || strcasecmp(e->cmda[y], cmds[y]))
853                                 match = 0;
854                 }
855                 if ((exact > -1) && e->cmda[y])
856                         match = 0;
857                 if (match)
858                         break;
859         }
860         return e;
861 }
862
863 static void join(char *dest, size_t destsize, char *w[])
864 {
865         int x;
866         /* Join words into a string */
867         if (!dest || destsize < 1) {
868                 return;
869         }
870         dest[0] = '\0';
871         for (x=0;w[x];x++) {
872                 if (x)
873                         strncat(dest, " ", destsize - strlen(dest) - 1);
874                 strncat(dest, w[x], destsize - strlen(dest) - 1);
875         }
876 }
877
878 static void join2(char *dest, size_t destsize, char *w[])
879 {
880         int x;
881         /* Join words into a string */
882         if (!dest || destsize < 1) {
883                 return;
884         }
885         dest[0] = '\0';
886         for (x=0;w[x];x++) {
887                 strncat(dest, w[x], destsize - strlen(dest) - 1);
888         }
889 }
890
891 static char *find_best(char *argv[])
892 {
893         static char cmdline[80];
894         int x;
895         /* See how close we get, then print the  */
896         char *myargv[AST_MAX_CMD_LEN];
897         for (x=0;x<AST_MAX_CMD_LEN;x++)
898                 myargv[x]=NULL;
899         for (x=0;argv[x];x++) {
900                 myargv[x] = argv[x];
901                 if (!find_cli(myargv, -1))
902                         break;
903         }
904         join(cmdline, sizeof(cmdline), myargv);
905         return cmdline;
906 }
907
908 int ast_cli_unregister(struct ast_cli_entry *e)
909 {
910         struct ast_cli_entry *cur, *l=NULL;
911         ast_mutex_lock(&clilock);
912         cur = helpers;
913         while(cur) {
914                 if (e == cur) {
915                         if (e->inuse) {
916                                 ast_log(LOG_WARNING, "Can't remove command that is in use\n");
917                         } else {
918                                 /* Rewrite */
919                                 if (l)
920                                         l->next = e->next;
921                                 else
922                                         helpers = e->next;
923                                 e->next = NULL;
924                                 break;
925                         }
926                 }
927                 l = cur;
928                 cur = cur->next;
929         }
930         ast_mutex_unlock(&clilock);
931         return 0;
932 }
933
934 int ast_cli_register(struct ast_cli_entry *e)
935 {
936         struct ast_cli_entry *cur, *l=NULL;
937         char fulle[80] ="", fulltst[80] ="";
938         static int len;
939         ast_mutex_lock(&clilock);
940         join2(fulle, sizeof(fulle), e->cmda);
941         if (find_cli(e->cmda, -1)) {
942                 ast_mutex_unlock(&clilock);
943                 ast_log(LOG_WARNING, "Command '%s' already registered (or something close enough)\n", fulle);
944                 return -1;
945         }
946         cur = helpers;
947         while(cur) {
948                 join2(fulltst, sizeof(fulltst), cur->cmda);
949                 len = strlen(fulltst);
950                 if (strlen(fulle) < len)
951                         len = strlen(fulle);
952                 if (strncasecmp(fulle, fulltst, len) < 0) {
953                         if (l) {
954                                 e->next = l->next;
955                                 l->next = e;
956                         } else {
957                                 e->next = helpers;
958                                 helpers = e;
959                         }
960                         break;
961                 }
962                 l = cur;
963                 cur = cur->next;
964         }
965         if (!cur) {
966                 if (l)
967                         l->next = e;
968                 else
969                         helpers = e;
970                 e->next = NULL;
971         }
972         ast_mutex_unlock(&clilock);
973         return 0;
974 }
975
976 /*
977  * register/unregister an array of entries.
978  */
979 void ast_cli_register_multiple(struct ast_cli_entry *e, int len)
980 {
981         int i;
982
983         for (i=0; i < len; i++)
984                 ast_cli_register(e + i);
985 }
986
987 void ast_cli_unregister_multiple(struct ast_cli_entry *e, int len)
988 {
989         int i;
990
991         for (i=0; i < len; i++)
992                 ast_cli_unregister(e + i);
993 }
994
995 static int help_workhorse(int fd, char *match[])
996 {
997         char fullcmd1[80] = "";
998         char fullcmd2[80] = "";
999         char matchstr[80];
1000         char *fullcmd = NULL;
1001         struct ast_cli_entry *e, *e1, *e2;
1002         e1 = builtins;
1003         e2 = helpers;
1004         if (match)
1005                 join(matchstr, sizeof(matchstr), match);
1006         while(e1->cmda[0] || e2) {
1007                 if (e2)
1008                         join(fullcmd2, sizeof(fullcmd2), e2->cmda);
1009                 if (e1->cmda[0])
1010                         join(fullcmd1, sizeof(fullcmd1), e1->cmda);
1011                 if (!e1->cmda[0] || 
1012                                 (e2 && (strcmp(fullcmd2, fullcmd1) < 0))) {
1013                         /* Use e2 */
1014                         e = e2;
1015                         fullcmd = fullcmd2;
1016                         /* Increment by going to next */
1017                         e2 = e2->next;
1018                 } else {
1019                         /* Use e1 */
1020                         e = e1;
1021                         fullcmd = fullcmd1;
1022                         e1++;
1023                 }
1024                 /* Hide commands that start with '_' */
1025                 if (fullcmd[0] == '_')
1026                         continue;
1027                 if (match) {
1028                         if (strncasecmp(matchstr, fullcmd, strlen(matchstr))) {
1029                                 continue;
1030                         }
1031                 }
1032                 ast_cli(fd, "%25.25s  %s\n", fullcmd, e->summary);
1033         }
1034         return 0;
1035 }
1036
1037 static int handle_help(int fd, int argc, char *argv[]) {
1038         struct ast_cli_entry *e;
1039         char fullcmd[80];
1040         if ((argc < 1))
1041                 return RESULT_SHOWUSAGE;
1042         if (argc > 1) {
1043                 e = find_cli(argv + 1, 1);
1044                 if (e) {
1045                         if (e->usage)
1046                                 ast_cli(fd, e->usage);
1047                         else {
1048                                 join(fullcmd, sizeof(fullcmd), argv+1);
1049                                 ast_cli(fd, "No help text available for '%s'.\n", fullcmd);
1050                         }
1051                 } else {
1052                         if (find_cli(argv + 1, -1)) {
1053                                 return help_workhorse(fd, argv + 1);
1054                         } else {
1055                                 join(fullcmd, sizeof(fullcmd), argv+1);
1056                                 ast_cli(fd, "No such command '%s'.\n", fullcmd);
1057                         }
1058                 }
1059         } else {
1060                 return help_workhorse(fd, NULL);
1061         }
1062         return RESULT_SUCCESS;
1063 }
1064
1065 static char *parse_args(char *s, int *max, char *argv[])
1066 {
1067         char *dup, *cur;
1068         int x=0;
1069         int quoted=0;
1070         int escaped=0;
1071         int whitespace=1;
1072
1073         dup = strdup(s);
1074         if (dup) {
1075                 cur = dup;
1076                 while(*s) {
1077                         switch(*s) {
1078                         case '"':
1079                                 /* If it's escaped, put a literal quote */
1080                                 if (escaped) 
1081                                         goto normal;
1082                                 else 
1083                                         quoted = !quoted;
1084                                 if (quoted && whitespace) {
1085                                         /* If we're starting a quote, coming off white space start a new word, too */
1086                                         argv[x++] = cur;
1087                                         whitespace=0;
1088                                 }
1089                                 escaped = 0;
1090                                 break;
1091                         case ' ':
1092                         case '\t':
1093                                 if (!quoted && !escaped) {
1094                                         /* If we're not quoted, mark this as whitespace, and
1095                                            end the previous argument */
1096                                         whitespace = 1;
1097                                         *(cur++) = '\0';
1098                                 } else
1099                                         /* Otherwise, just treat it as anything else */ 
1100                                         goto normal;
1101                                 break;
1102                         case '\\':
1103                                 /* If we're escaped, print a literal, otherwise enable escaping */
1104                                 if (escaped) {
1105                                         goto normal;
1106                                 } else {
1107                                         escaped=1;
1108                                 }
1109                                 break;
1110                         default:
1111 normal:
1112                                 if (whitespace) {
1113                                         if (x >= AST_MAX_ARGS -1) {
1114                                                 ast_log(LOG_WARNING, "Too many arguments, truncating\n");
1115                                                 break;
1116                                         }
1117                                         /* Coming off of whitespace, start the next argument */
1118                                         argv[x++] = cur;
1119                                         whitespace=0;
1120                                 }
1121                                 *(cur++) = *s;
1122                                 escaped=0;
1123                         }
1124                         s++;
1125                 }
1126                 /* Null terminate */
1127                 *(cur++) = '\0';
1128                 argv[x] = NULL;
1129                 *max = x;
1130         }
1131         return dup;
1132 }
1133
1134 /* This returns the number of unique matches for the generator */
1135 int ast_cli_generatornummatches(char *text, char *word)
1136 {
1137         int matches = 0, i = 0;
1138         char *buf = NULL, *oldbuf = NULL;
1139
1140         while ( (buf = ast_cli_generator(text, word, i)) ) {
1141                 if (++i > 1 && strcmp(buf,oldbuf) == 0)  {
1142                                 continue;
1143                 }
1144                 oldbuf = buf;
1145                 matches++;
1146         }
1147
1148         return matches;
1149 }
1150
1151 char **ast_cli_completion_matches(char *text, char *word)
1152 {
1153         char **match_list = NULL, *retstr, *prevstr;
1154         size_t match_list_len, max_equal, which, i;
1155         int matches = 0;
1156
1157         match_list_len = 1;
1158         while ((retstr = ast_cli_generator(text, word, matches)) != NULL) {
1159                 if (matches + 1 >= match_list_len) {
1160                         match_list_len <<= 1;
1161                         match_list = realloc(match_list, match_list_len * sizeof(char *));
1162                 }
1163                 match_list[++matches] = retstr;
1164         }
1165
1166         if (!match_list)
1167                 return (char **) NULL;
1168
1169         which = 2;
1170         prevstr = match_list[1];
1171         max_equal = strlen(prevstr);
1172         for (; which <= matches; which++) {
1173                 for (i = 0; i < max_equal && toupper(prevstr[i]) == toupper(match_list[which][i]); i++)
1174                         continue;
1175                 max_equal = i;
1176         }
1177
1178         retstr = malloc(max_equal + 1);
1179         (void) strncpy(retstr, match_list[1], max_equal);
1180         retstr[max_equal] = '\0';
1181         match_list[0] = retstr;
1182
1183         if (matches + 1 >= match_list_len)
1184                 match_list = realloc(match_list, (match_list_len + 1) * sizeof(char *));
1185         match_list[matches + 1] = (char *) NULL;
1186
1187         return (match_list);
1188 }
1189
1190 static char *__ast_cli_generator(char *text, char *word, int state, int lock)
1191 {
1192         char *argv[AST_MAX_ARGS];
1193         struct ast_cli_entry *e, *e1, *e2;
1194         int x;
1195         int matchnum=0;
1196         char *dup, *res;
1197         char fullcmd1[80] = "";
1198         char fullcmd2[80] = "";
1199         char matchstr[80];
1200         char *fullcmd = NULL;
1201
1202         if ((dup = parse_args(text, &x, argv))) {
1203                 join(matchstr, sizeof(matchstr), argv);
1204                 if (lock)
1205                         ast_mutex_lock(&clilock);
1206                 e1 = builtins;
1207                 e2 = helpers;
1208                 while(e1->cmda[0] || e2) {
1209                         if (e2)
1210                                 join(fullcmd2, sizeof(fullcmd2), e2->cmda);
1211                         if (e1->cmda[0])
1212                                 join(fullcmd1, sizeof(fullcmd1), e1->cmda);
1213                         if (!e1->cmda[0] || 
1214                                         (e2 && (strcmp(fullcmd2, fullcmd1) < 0))) {
1215                                 /* Use e2 */
1216                                 e = e2;
1217                                 fullcmd = fullcmd2;
1218                                 /* Increment by going to next */
1219                                 e2 = e2->next;
1220                         } else {
1221                                 /* Use e1 */
1222                                 e = e1;
1223                                 fullcmd = fullcmd1;
1224                                 e1++;
1225                         }
1226                         if ((fullcmd[0] != '_') && !strncasecmp(matchstr, fullcmd, strlen(matchstr))) {
1227                                 /* We contain the first part of one or more commands */
1228                                 /* Now, what we're supposed to return is the next word... */
1229                                 if (!ast_strlen_zero(word) && x>0) {
1230                                         res = e->cmda[x-1];
1231                                 } else {
1232                                         res = e->cmda[x];
1233                                 }
1234                                 if (res) {
1235                                         matchnum++;
1236                                         if (matchnum > state) {
1237                                                 if (lock)
1238                                                         ast_mutex_unlock(&clilock);
1239                                                 free(dup);
1240                                                 return strdup(res);
1241                                         }
1242                                 }
1243                         }
1244                         if (e->generator && !strncasecmp(matchstr, fullcmd, strlen(fullcmd))) {
1245                                 /* We have a command in its entirity within us -- theoretically only one
1246                                    command can have this occur */
1247                                 fullcmd = e->generator(matchstr, word, (!ast_strlen_zero(word) ? (x - 1) : (x)), state);
1248                                 if (fullcmd) {
1249                                         if (lock)
1250                                                 ast_mutex_unlock(&clilock);
1251                                         free(dup);
1252                                         return fullcmd;
1253                                 }
1254                         }
1255                         
1256                 }
1257                 if (lock)
1258                         ast_mutex_unlock(&clilock);
1259                 free(dup);
1260         }
1261         return NULL;
1262 }
1263
1264 char *ast_cli_generator(char *text, char *word, int state)
1265 {
1266         return __ast_cli_generator(text, word, state, 1);
1267 }
1268
1269 int ast_cli_command(int fd, char *s)
1270 {
1271         char *argv[AST_MAX_ARGS];
1272         struct ast_cli_entry *e;
1273         int x;
1274         char *dup;
1275         x = AST_MAX_ARGS;
1276         if ((dup = parse_args(s, &x, argv))) {
1277                 /* We need at least one entry, or ignore */
1278                 if (x > 0) {
1279                         ast_mutex_lock(&clilock);
1280                         e = find_cli(argv, 0);
1281                         if (e)
1282                                 e->inuse++;
1283                         ast_mutex_unlock(&clilock);
1284                         if (e) {
1285                                 switch(e->handler(fd, x, argv)) {
1286                                 case RESULT_SHOWUSAGE:
1287                                         ast_cli(fd, e->usage);
1288                                         break;
1289                                 }
1290                         } else 
1291                                 ast_cli(fd, "No such command '%s' (type 'help' for help)\n", find_best(argv));
1292                         if (e) {
1293                                 ast_mutex_lock(&clilock);
1294                                 e->inuse--;
1295                                 ast_mutex_unlock(&clilock);
1296                         }
1297                 }
1298                 free(dup);
1299         } else {
1300                 ast_log(LOG_WARNING, "Out of memory\n");        
1301                 return -1;
1302         }
1303         return 0;
1304 }