Skip to content
Snippets Groups Projects
cli.c 32.2 KiB
Newer Older
Mark Spencer's avatar
Mark Spencer committed
/*
 * Asterisk -- A telephony toolkit for Linux.
 *
 * Standard Command Line Interface
 * 
 * Copyright (C) 1999-2004, Digium, Inc.
Mark Spencer's avatar
Mark Spencer committed
 *
 * Mark Spencer <markster@digium.com>
Mark Spencer's avatar
Mark Spencer committed
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License
 */

#include <unistd.h>
#include <stdlib.h>
#include <asterisk/logger.h>
#include <asterisk/options.h>
#include <asterisk/cli.h>
#include <asterisk/module.h>
#include <asterisk/pbx.h>
Mark Spencer's avatar
Mark Spencer committed
#include <asterisk/channel.h>
Mark Spencer's avatar
Mark Spencer committed
#include <asterisk/channel_pvt.h>
#include <asterisk/manager.h>
#include <asterisk/utils.h>
Mark Spencer's avatar
Mark Spencer committed
#include <sys/signal.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
Mark Spencer's avatar
Mark Spencer committed
#include <ctype.h>
Mark Spencer's avatar
Mark Spencer committed
/* For rl_filename_completion */
Mark Spencer's avatar
Mark Spencer committed
#include "editline/readline/readline.h"
Mark Spencer's avatar
Mark Spencer committed
/* For module directory */
#include "asterisk.h"
Mark Spencer's avatar
Mark Spencer committed
#include "build.h"
Mark Spencer's avatar
Mark Spencer committed
#include "astconf.h"
Mark Spencer's avatar
Mark Spencer committed

#define VERSION_INFO "Asterisk " ASTERISK_VERSION " built by " BUILD_USER "@" BUILD_HOSTNAME \
	" on a " BUILD_MACHINE " running " BUILD_OS
	
Mark Spencer's avatar
Mark Spencer committed
void ast_cli(int fd, char *fmt, ...)
{
Mark Spencer's avatar
Mark Spencer committed
	va_list ap;
	va_start(ap, fmt);
#ifdef SOLARIS
        stuff = (char *)malloc(10240);
        vsnprintf(stuff, 10240, fmt, ap);
#else
	res = vasprintf(&stuff, fmt, ap);
Mark Spencer's avatar
Mark Spencer committed
	va_end(ap);
	if (res == -1) {
		ast_log(LOG_ERROR, "Out of memory\n");
		ast_carefulwrite(fd, stuff, strlen(stuff), 100);
		free(stuff);
	}
AST_MUTEX_DEFINE_STATIC(clilock);
Mark Spencer's avatar
Mark Spencer committed

struct ast_cli_entry *helpers = NULL;

static char load_help[] = 
"Usage: load <module name>\n"
"       Loads the specified module into Asterisk.\n";

static char unload_help[] = 
"Usage: unload [-f|-h] <module name>\n"
"       Unloads the specified module from Asterisk.  The -f\n"
"       option causes the module to be unloaded even if it is\n"
"       in use (may cause a crash) and the -h module causes the\n"
"       module to be unloaded even if the module says it cannot, \n"
"       which almost always will cause a crash.\n";

static char help_help[] =
"Usage: help [topic]\n"
"       When called with a topic as an argument, displays usage\n"
"       information on the given command.  If called without a\n"
"       topic, it provides a list of commands.\n";

static char chanlist_help[] = 
"Usage: show channels [concise]\n"
Mark Spencer's avatar
Mark Spencer committed
"       Lists currently defined channels and some information about\n"
"       them.  If 'concise' is specified, format is abridged and in\n"
"       a more easily machine parsable format\n";
Mark Spencer's avatar
Mark Spencer committed
static char reload_help[] = 
"Usage: reload [module ...]\n"
"       Reloads configuration files for all listed modules which support\n"
"       reloading, or for all supported modules if none are listed.\n";
Mark Spencer's avatar
Mark Spencer committed

static char set_verbose_help[] = 
"Usage: set verbose <level>\n"
"       Sets level of verbose messages to be displayed.  0 means\n"
"       no messages should be displayed. Equivalent to -v[v[v...]]\n"
"       on startup\n";

static char set_debug_help[] = 
"Usage: set debug <level>\n"
"       Sets level of core debug messages to be displayed.  0 means\n"
"       no messages should be displayed. Equivalent to -d[d[d...]]\n"
"       on startup.\n";
Mark Spencer's avatar
Mark Spencer committed

static char softhangup_help[] =
"Usage: soft hangup <channel>\n"
"       Request that a channel be hung up.  The hangup takes effect\n"
"       the next time the driver reads or writes from the channel\n";

Mark Spencer's avatar
Mark Spencer committed
static int handle_load(int fd, int argc, char *argv[])
{
	if (argc != 2)
		return RESULT_SHOWUSAGE;
	if (ast_load_resource(argv[1])) {
		ast_cli(fd, "Unable to load module %s\n", argv[1]);
		return RESULT_FAILURE;
	}
	return RESULT_SUCCESS;
}

Mark Spencer's avatar
Mark Spencer committed
static int handle_reload(int fd, int argc, char *argv[])
{
Mark Spencer's avatar
Mark Spencer committed
		return RESULT_SHOWUSAGE;
		for (x=1;x<argc;x++) {
			res = ast_module_reload(argv[x]);
			switch(res) {
			case 0:
				ast_cli(fd, "No such module '%s'\n", argv[x]);
				break;
			case 1:
				ast_cli(fd, "Module '%s' does not support reload\n", argv[x]);
				break;
			}
		}
Mark Spencer's avatar
Mark Spencer committed
	return RESULT_SUCCESS;
}

static int handle_set_verbose(int fd, int argc, char *argv[])
{
Mark Spencer's avatar
Mark Spencer committed
	/* Has a hidden 'at least' argument */
	if ((argc != 3) && (argc != 4))
		return RESULT_SHOWUSAGE;
	if ((argc == 4) && strcasecmp(argv[2], "atleast"))
		return RESULT_SHOWUSAGE;
Mark Spencer's avatar
Mark Spencer committed
	if (argc == 3)
		option_verbose = atoi(argv[2]);
	else {
		val = atoi(argv[3]);
		if (val > option_verbose)
			option_verbose = val;
	}
	if (oldval != option_verbose && option_verbose > 0)
		ast_cli(fd, "Verbosity was %d and is now %d\n", oldval, option_verbose);
	else if (oldval > 0 && option_verbose > 0)
		ast_cli(fd, "Verbosity is at least %d\n", option_verbose);
	else if (oldval > 0 && option_verbose == 0)
		ast_cli(fd, "Verbosity is now OFF\n");
	return RESULT_SUCCESS;
}

static int handle_set_debug(int fd, int argc, char *argv[])
{
	int val = 0;
	int oldval = 0;
	/* Has a hidden 'at least' argument */
	if ((argc != 3) && (argc != 4))
		return RESULT_SHOWUSAGE;
	if ((argc == 4) && strcasecmp(argv[2], "atleast"))
		return RESULT_SHOWUSAGE;
	oldval = option_debug;
	if (argc == 3)
		option_debug = atoi(argv[2]);
	else {
		val = atoi(argv[3]);
		if (val > option_debug)
			option_debug = val;
	}
	if (oldval != option_debug && option_debug > 0)
		ast_cli(fd, "Core debug was %d and is now %d\n", oldval, option_debug);
	else if (oldval > 0 && option_debug > 0)
		ast_cli(fd, "Core debug is at least %d\n", option_debug);
	else if (oldval > 0 && option_debug == 0)
		ast_cli(fd, "Core debug is now OFF\n");
Mark Spencer's avatar
Mark Spencer committed
	return RESULT_SUCCESS;
}
Loading
Loading full blame...