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