Skip to content
Snippets Groups Projects
cli.c 39.9 KiB
Newer Older
Mark Spencer's avatar
Mark Spencer committed
/*
 * Asterisk -- An open source telephony toolkit.
Mark Spencer's avatar
Mark Spencer committed
 *
 * Copyright (C) 1999 - 2006, Digium, Inc.
Mark Spencer's avatar
Mark Spencer committed
 *
 * Mark Spencer <markster@digium.com>
Mark Spencer's avatar
Mark Spencer committed
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
Mark Spencer's avatar
Mark Spencer committed
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

 * \brief Standard Command Line Interface
 *
 * \author Mark Spencer <markster@digium.com> 
Mark Spencer's avatar
Mark Spencer committed
 */

#include <unistd.h>
#include <stdlib.h>
#include <sys/signal.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <ctype.h>
#include "asterisk.h"

Kevin P. Fleming's avatar
Kevin P. Fleming committed
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/logger.h"
#include "asterisk/options.h"
#include "asterisk/cli.h"
#include "asterisk/linkedlists.h"
#include "asterisk/module.h"
#include "asterisk/pbx.h"
#include "asterisk/channel.h"
#include "asterisk/utils.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 */
Mark Spencer's avatar
Mark Spencer committed
void ast_cli(int fd, char *fmt, ...)
{
Mark Spencer's avatar
Mark Spencer committed
	va_list ap;
Mark Spencer's avatar
Mark Spencer committed
	va_start(ap, fmt);
	res = vasprintf(&stuff, fmt, ap);
Mark Spencer's avatar
Mark Spencer committed
	va_end(ap);
		ast_log(LOG_ERROR, "Memory allocation failure\n");
		ast_carefulwrite(fd, stuff, strlen(stuff), 100);
		free(stuff);
	}
static AST_LIST_HEAD_STATIC(helpers, ast_cli_entry);
Mark Spencer's avatar
Mark Spencer committed

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"
Mark Spencer's avatar
Mark Spencer committed
"       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"
Mark Spencer's avatar
Mark Spencer committed
"       topic, it provides a list of commands.\n";

static char chanlist_help[] = 
"Usage: show channels [concise|verbose]\n"
"       Lists currently defined channels and some information about them. If\n"
"       'concise' is specified, the format is abridged and in a more easily\n"
"       machine parsable format. If 'verbose' is specified, the output includes\n"
"       more and longer fields.\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";
static char logger_mute_help[] = 
"Usage: logger mute\n"
"       Disables logging output to the current console, making it possible to\n"
"       gather information without being disturbed by scrolling lines.\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"
Mark Spencer's avatar
Mark Spencer committed
"       the next time the driver reads or writes from the channel\n";

static char group_show_channels_help[] = 
"Usage: group show channels [pattern]\n"
"       Lists all currently active channels with channel group(s) specified.\n"
"       Optional regular expression pattern is matched to group names for each\n"
"       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[])
{
	int oldval = option_verbose;
	/* "set verbose [atleast] N" */
Mark Spencer's avatar
Mark Spencer committed
	if (argc == 3)
		option_verbose = atoi(argv[2]);
	else if (argc == 4) {
		if (strcasecmp(argv[2], "atleast"))
			return RESULT_SHOWUSAGE;
Mark Spencer's avatar
Mark Spencer committed
		val = atoi(argv[3]);
		if (val > option_verbose)
			option_verbose = val;
	} else
		return RESULT_SHOWUSAGE;
	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 = option_debug;

	/* "set debug [atleast] N" */
	if (argc == 3)
		option_debug = atoi(argv[2]);
	else if (argc == 4) {
Loading
Loading full blame...