1999-12-19 22:38:55 +00:00
|
|
|
/*
|
2005-08-30 18:32:10 +00:00
|
|
|
* Asterisk -- An open source telephony toolkit.
|
1999-12-19 22:38:55 +00:00
|
|
|
*
|
2005-08-30 18:32:10 +00:00
|
|
|
* Copyright (C) 1999 - 2005, Digium, Inc.
|
|
|
|
*
|
|
|
|
* Mark Spencer <markster@digium.com>
|
1999-12-19 22:38:55 +00:00
|
|
|
*
|
2005-08-30 18:32:10 +00:00
|
|
|
* 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.
|
1999-12-19 22:38:55 +00:00
|
|
|
*
|
|
|
|
* This program is free software, distributed under the terms of
|
2005-08-30 18:32:10 +00:00
|
|
|
* the GNU General Public License Version 2. See the LICENSE file
|
|
|
|
* at the top of the source tree.
|
|
|
|
*/
|
|
|
|
|
2005-10-24 20:12:06 +00:00
|
|
|
/*! \file
|
|
|
|
* \brief Standard Command Line Interface
|
1999-12-19 22:38:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _ASTERISK_CLI_H
|
|
|
|
#define _ASTERISK_CLI_H
|
|
|
|
|
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2006-02-14 22:44:20 +00:00
|
|
|
#include "asterisk/linkedlists.h"
|
|
|
|
|
2007-02-13 05:57:52 +00:00
|
|
|
void ast_cli(int fd, const char *fmt, ...)
|
2003-09-10 05:24:49 +00:00
|
|
|
__attribute__ ((format (printf, 2, 3)));
|
1999-12-19 22:38:55 +00:00
|
|
|
|
|
|
|
#define RESULT_SUCCESS 0
|
|
|
|
#define RESULT_SHOWUSAGE 1
|
|
|
|
#define RESULT_FAILURE 2
|
|
|
|
|
2006-11-15 14:11:28 +00:00
|
|
|
#define CLI_SUCCESS (char *)RESULT_SUCCESS
|
|
|
|
#define CLI_SHOWUSAGE (char *)RESULT_SHOWUSAGE
|
|
|
|
#define CLI_FAILURE (char *)RESULT_FAILURE
|
|
|
|
|
1999-12-19 22:38:55 +00:00
|
|
|
#define AST_MAX_CMD_LEN 16
|
|
|
|
|
|
|
|
#define AST_MAX_ARGS 64
|
|
|
|
|
2004-04-06 07:42:01 +00:00
|
|
|
#define AST_CLI_COMPLETE_EOF "_EOF_"
|
|
|
|
|
2006-11-18 20:08:17 +00:00
|
|
|
/*!
|
|
|
|
* In many cases we need to print singular or plural
|
|
|
|
* words depending on a count. This macro helps us e.g.
|
|
|
|
* printf("we have %d object%s", n, ESS(n));
|
|
|
|
*/
|
|
|
|
#define ESS(x) ((x) == 1 ? "" : "s")
|
|
|
|
|
2007-07-16 02:51:56 +00:00
|
|
|
/*! \page CLI_command_API CLI command API
|
2006-11-14 16:10:32 +00:00
|
|
|
|
Bring in the improved internal API for the CLI.
WATCH OUT: this changes the binary interface (ABI) for modules,
so e.g. users of g729 codecs need a rebuilt module (but read below).
The new way to write CLI handlers is described in detail in cli.h,
and there are a few converted handlers in cli.c, look for NEW_CLI.
After converting a couple of commands i am convinced that
it is reasonably convenient to use, and it makes it easier to fix the
pending CLI issues.
On passing, note a bug with the current 'complete' architecture:
if a command is a prefix of multiple CLI entries, we miss some
of the possible options. As an example, "core set debug" can
continue with "channel" from one CLI entry, and "off" or "atleast"
from another one.
We address this problem in a separate commit
(when i have figured out a fix, that is).
ABI issues:
I asked Kevin if it was ok to make this change and he said yes.
While it would have been possible to make the change without breaking
the module ABI, the code would have been more convoluted.
I am happy to restore the old ABI (while still being able
to use the "new style" handlers) if there is demand.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@47606 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2006-11-14 15:23:35 +00:00
|
|
|
CLI commands are described by a struct ast_cli_entry that contains
|
|
|
|
all the components for their implementation.
|
2006-11-14 16:10:32 +00:00
|
|
|
|
Bring in the improved internal API for the CLI.
WATCH OUT: this changes the binary interface (ABI) for modules,
so e.g. users of g729 codecs need a rebuilt module (but read below).
The new way to write CLI handlers is described in detail in cli.h,
and there are a few converted handlers in cli.c, look for NEW_CLI.
After converting a couple of commands i am convinced that
it is reasonably convenient to use, and it makes it easier to fix the
pending CLI issues.
On passing, note a bug with the current 'complete' architecture:
if a command is a prefix of multiple CLI entries, we miss some
of the possible options. As an example, "core set debug" can
continue with "channel" from one CLI entry, and "off" or "atleast"
from another one.
We address this problem in a separate commit
(when i have figured out a fix, that is).
ABI issues:
I asked Kevin if it was ok to make this change and he said yes.
While it would have been possible to make the change without breaking
the module ABI, the code would have been more convoluted.
I am happy to restore the old ABI (while still being able
to use the "new style" handlers) if there is demand.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@47606 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2006-11-14 15:23:35 +00:00
|
|
|
In the "old-style" format, the record must contain:
|
|
|
|
- a NULL-terminated array of words constituting the command, e.g.
|
|
|
|
{ "set", "debug", "on", NULL },
|
|
|
|
- a summary string (short) and a usage string (longer);
|
|
|
|
- a handler which implements the command itself, invoked with
|
|
|
|
a file descriptor and argc/argv as typed by the user
|
|
|
|
- a 'generator' function which, given a partial string, can
|
|
|
|
generate legal completions for it.
|
|
|
|
An example is
|
|
|
|
|
|
|
|
int old_setdebug(int fd, int argc, char *argv[]);
|
|
|
|
char *dbg_complete(const char *line, const char *word, int pos, int n);
|
|
|
|
|
|
|
|
{ { "set", "debug", "on", NULL }, do_setdebug, "Enable debugging",
|
|
|
|
set_debug_usage, dbg_complete },
|
|
|
|
|
|
|
|
In the "new-style" format, all the above functionalities are implemented
|
|
|
|
by a single function, and the arguments tell which output is required.
|
2006-11-15 14:11:28 +00:00
|
|
|
The prototype is the following:
|
Bring in the improved internal API for the CLI.
WATCH OUT: this changes the binary interface (ABI) for modules,
so e.g. users of g729 codecs need a rebuilt module (but read below).
The new way to write CLI handlers is described in detail in cli.h,
and there are a few converted handlers in cli.c, look for NEW_CLI.
After converting a couple of commands i am convinced that
it is reasonably convenient to use, and it makes it easier to fix the
pending CLI issues.
On passing, note a bug with the current 'complete' architecture:
if a command is a prefix of multiple CLI entries, we miss some
of the possible options. As an example, "core set debug" can
continue with "channel" from one CLI entry, and "off" or "atleast"
from another one.
We address this problem in a separate commit
(when i have figured out a fix, that is).
ABI issues:
I asked Kevin if it was ok to make this change and he said yes.
While it would have been possible to make the change without breaking
the module ABI, the code would have been more convoluted.
I am happy to restore the old ABI (while still being able
to use the "new style" handlers) if there is demand.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@47606 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2006-11-14 15:23:35 +00:00
|
|
|
|
2006-11-15 14:11:28 +00:00
|
|
|
char *new_setdebug(const struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
|
Bring in the improved internal API for the CLI.
WATCH OUT: this changes the binary interface (ABI) for modules,
so e.g. users of g729 codecs need a rebuilt module (but read below).
The new way to write CLI handlers is described in detail in cli.h,
and there are a few converted handlers in cli.c, look for NEW_CLI.
After converting a couple of commands i am convinced that
it is reasonably convenient to use, and it makes it easier to fix the
pending CLI issues.
On passing, note a bug with the current 'complete' architecture:
if a command is a prefix of multiple CLI entries, we miss some
of the possible options. As an example, "core set debug" can
continue with "channel" from one CLI entry, and "off" or "atleast"
from another one.
We address this problem in a separate commit
(when i have figured out a fix, that is).
ABI issues:
I asked Kevin if it was ok to make this change and he said yes.
While it would have been possible to make the change without breaking
the module ABI, the code would have been more convoluted.
I am happy to restore the old ABI (while still being able
to use the "new style" handlers) if there is demand.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@47606 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2006-11-14 15:23:35 +00:00
|
|
|
|
|
|
|
...
|
2006-11-14 16:15:34 +00:00
|
|
|
// this is how we create the entry to register
|
2007-10-22 20:05:18 +00:00
|
|
|
AST_CLI_DEFINE(new_setdebug, "short description")
|
Bring in the improved internal API for the CLI.
WATCH OUT: this changes the binary interface (ABI) for modules,
so e.g. users of g729 codecs need a rebuilt module (but read below).
The new way to write CLI handlers is described in detail in cli.h,
and there are a few converted handlers in cli.c, look for NEW_CLI.
After converting a couple of commands i am convinced that
it is reasonably convenient to use, and it makes it easier to fix the
pending CLI issues.
On passing, note a bug with the current 'complete' architecture:
if a command is a prefix of multiple CLI entries, we miss some
of the possible options. As an example, "core set debug" can
continue with "channel" from one CLI entry, and "off" or "atleast"
from another one.
We address this problem in a separate commit
(when i have figured out a fix, that is).
ABI issues:
I asked Kevin if it was ok to make this change and he said yes.
While it would have been possible to make the change without breaking
the module ABI, the code would have been more convoluted.
I am happy to restore the old ABI (while still being able
to use the "new style" handlers) if there is demand.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@47606 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2006-11-14 15:23:35 +00:00
|
|
|
...
|
|
|
|
|
2006-11-15 14:11:28 +00:00
|
|
|
To help the transition, we make the pointer to the struct ast_cli_entry
|
|
|
|
available to old-style handlers via argv[-1].
|
Bring in the improved internal API for the CLI.
WATCH OUT: this changes the binary interface (ABI) for modules,
so e.g. users of g729 codecs need a rebuilt module (but read below).
The new way to write CLI handlers is described in detail in cli.h,
and there are a few converted handlers in cli.c, look for NEW_CLI.
After converting a couple of commands i am convinced that
it is reasonably convenient to use, and it makes it easier to fix the
pending CLI issues.
On passing, note a bug with the current 'complete' architecture:
if a command is a prefix of multiple CLI entries, we miss some
of the possible options. As an example, "core set debug" can
continue with "channel" from one CLI entry, and "off" or "atleast"
from another one.
We address this problem in a separate commit
(when i have figured out a fix, that is).
ABI issues:
I asked Kevin if it was ok to make this change and he said yes.
While it would have been possible to make the change without breaking
the module ABI, the code would have been more convoluted.
I am happy to restore the old ABI (while still being able
to use the "new style" handlers) if there is demand.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@47606 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2006-11-14 15:23:35 +00:00
|
|
|
|
|
|
|
An example of new-style handler is the following
|
|
|
|
|
|
|
|
\code
|
2006-11-15 14:11:28 +00:00
|
|
|
static char *test_new_cli(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
|
Bring in the improved internal API for the CLI.
WATCH OUT: this changes the binary interface (ABI) for modules,
so e.g. users of g729 codecs need a rebuilt module (but read below).
The new way to write CLI handlers is described in detail in cli.h,
and there are a few converted handlers in cli.c, look for NEW_CLI.
After converting a couple of commands i am convinced that
it is reasonably convenient to use, and it makes it easier to fix the
pending CLI issues.
On passing, note a bug with the current 'complete' architecture:
if a command is a prefix of multiple CLI entries, we miss some
of the possible options. As an example, "core set debug" can
continue with "channel" from one CLI entry, and "off" or "atleast"
from another one.
We address this problem in a separate commit
(when i have figured out a fix, that is).
ABI issues:
I asked Kevin if it was ok to make this change and he said yes.
While it would have been possible to make the change without breaking
the module ABI, the code would have been more convoluted.
I am happy to restore the old ABI (while still being able
to use the "new style" handlers) if there is demand.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@47606 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2006-11-14 15:23:35 +00:00
|
|
|
{
|
|
|
|
static char *choices = { "one", "two", "three", NULL };
|
|
|
|
|
2006-11-15 14:11:28 +00:00
|
|
|
switch (cmd) {
|
|
|
|
case CLI_INIT:
|
|
|
|
e->command = "do this well";
|
|
|
|
e->usage =
|
Bring in the improved internal API for the CLI.
WATCH OUT: this changes the binary interface (ABI) for modules,
so e.g. users of g729 codecs need a rebuilt module (but read below).
The new way to write CLI handlers is described in detail in cli.h,
and there are a few converted handlers in cli.c, look for NEW_CLI.
After converting a couple of commands i am convinced that
it is reasonably convenient to use, and it makes it easier to fix the
pending CLI issues.
On passing, note a bug with the current 'complete' architecture:
if a command is a prefix of multiple CLI entries, we miss some
of the possible options. As an example, "core set debug" can
continue with "channel" from one CLI entry, and "off" or "atleast"
from another one.
We address this problem in a separate commit
(when i have figured out a fix, that is).
ABI issues:
I asked Kevin if it was ok to make this change and he said yes.
While it would have been possible to make the change without breaking
the module ABI, the code would have been more convoluted.
I am happy to restore the old ABI (while still being able
to use the "new style" handlers) if there is demand.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@47606 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2006-11-14 15:23:35 +00:00
|
|
|
"Usage: do this well <arg>\n"
|
|
|
|
" typically multiline with body indented\n";
|
2006-11-15 14:11:28 +00:00
|
|
|
return NULL;
|
Bring in the improved internal API for the CLI.
WATCH OUT: this changes the binary interface (ABI) for modules,
so e.g. users of g729 codecs need a rebuilt module (but read below).
The new way to write CLI handlers is described in detail in cli.h,
and there are a few converted handlers in cli.c, look for NEW_CLI.
After converting a couple of commands i am convinced that
it is reasonably convenient to use, and it makes it easier to fix the
pending CLI issues.
On passing, note a bug with the current 'complete' architecture:
if a command is a prefix of multiple CLI entries, we miss some
of the possible options. As an example, "core set debug" can
continue with "channel" from one CLI entry, and "off" or "atleast"
from another one.
We address this problem in a separate commit
(when i have figured out a fix, that is).
ABI issues:
I asked Kevin if it was ok to make this change and he said yes.
While it would have been possible to make the change without breaking
the module ABI, the code would have been more convoluted.
I am happy to restore the old ABI (while still being able
to use the "new style" handlers) if there is demand.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@47606 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2006-11-14 15:23:35 +00:00
|
|
|
|
|
|
|
case CLI_GENERATE:
|
|
|
|
if (a->pos > e->args)
|
|
|
|
return NULL;
|
|
|
|
return ast_cli_complete(a->word, choices, a->n);
|
|
|
|
|
|
|
|
default:
|
|
|
|
// we are guaranteed to be called with argc >= e->args;
|
2006-11-15 14:11:28 +00:00
|
|
|
if (a->argc > e->args + 1) // we accept one extra argument
|
|
|
|
return CLI_SHOWUSAGE;
|
|
|
|
ast_cli(a->fd, "done this well for %s\n", e->args[argc-1]);
|
|
|
|
return CLI_SUCCESS;
|
Bring in the improved internal API for the CLI.
WATCH OUT: this changes the binary interface (ABI) for modules,
so e.g. users of g729 codecs need a rebuilt module (but read below).
The new way to write CLI handlers is described in detail in cli.h,
and there are a few converted handlers in cli.c, look for NEW_CLI.
After converting a couple of commands i am convinced that
it is reasonably convenient to use, and it makes it easier to fix the
pending CLI issues.
On passing, note a bug with the current 'complete' architecture:
if a command is a prefix of multiple CLI entries, we miss some
of the possible options. As an example, "core set debug" can
continue with "channel" from one CLI entry, and "off" or "atleast"
from another one.
We address this problem in a separate commit
(when i have figured out a fix, that is).
ABI issues:
I asked Kevin if it was ok to make this change and he said yes.
While it would have been possible to make the change without breaking
the module ABI, the code would have been more convoluted.
I am happy to restore the old ABI (while still being able
to use the "new style" handlers) if there is demand.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@47606 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2006-11-14 15:23:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
\endcode
|
2006-11-14 16:10:32 +00:00
|
|
|
|
Bring in the improved internal API for the CLI.
WATCH OUT: this changes the binary interface (ABI) for modules,
so e.g. users of g729 codecs need a rebuilt module (but read below).
The new way to write CLI handlers is described in detail in cli.h,
and there are a few converted handlers in cli.c, look for NEW_CLI.
After converting a couple of commands i am convinced that
it is reasonably convenient to use, and it makes it easier to fix the
pending CLI issues.
On passing, note a bug with the current 'complete' architecture:
if a command is a prefix of multiple CLI entries, we miss some
of the possible options. As an example, "core set debug" can
continue with "channel" from one CLI entry, and "off" or "atleast"
from another one.
We address this problem in a separate commit
(when i have figured out a fix, that is).
ABI issues:
I asked Kevin if it was ok to make this change and he said yes.
While it would have been possible to make the change without breaking
the module ABI, the code would have been more convoluted.
I am happy to restore the old ABI (while still being able
to use the "new style" handlers) if there is demand.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@47606 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2006-11-14 15:23:35 +00:00
|
|
|
*/
|
|
|
|
|
2007-07-16 02:51:56 +00:00
|
|
|
/*! \brief calling arguments for new-style handlers.
|
|
|
|
* \arg \ref CLI_command_API
|
2006-11-14 16:10:32 +00:00
|
|
|
*/
|
Bring in the improved internal API for the CLI.
WATCH OUT: this changes the binary interface (ABI) for modules,
so e.g. users of g729 codecs need a rebuilt module (but read below).
The new way to write CLI handlers is described in detail in cli.h,
and there are a few converted handlers in cli.c, look for NEW_CLI.
After converting a couple of commands i am convinced that
it is reasonably convenient to use, and it makes it easier to fix the
pending CLI issues.
On passing, note a bug with the current 'complete' architecture:
if a command is a prefix of multiple CLI entries, we miss some
of the possible options. As an example, "core set debug" can
continue with "channel" from one CLI entry, and "off" or "atleast"
from another one.
We address this problem in a separate commit
(when i have figured out a fix, that is).
ABI issues:
I asked Kevin if it was ok to make this change and he said yes.
While it would have been possible to make the change without breaking
the module ABI, the code would have been more convoluted.
I am happy to restore the old ABI (while still being able
to use the "new style" handlers) if there is demand.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@47606 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2006-11-14 15:23:35 +00:00
|
|
|
enum ast_cli_fn {
|
2006-11-15 14:11:28 +00:00
|
|
|
CLI_INIT = -2, /* return the usage string */
|
Bring in the improved internal API for the CLI.
WATCH OUT: this changes the binary interface (ABI) for modules,
so e.g. users of g729 codecs need a rebuilt module (but read below).
The new way to write CLI handlers is described in detail in cli.h,
and there are a few converted handlers in cli.c, look for NEW_CLI.
After converting a couple of commands i am convinced that
it is reasonably convenient to use, and it makes it easier to fix the
pending CLI issues.
On passing, note a bug with the current 'complete' architecture:
if a command is a prefix of multiple CLI entries, we miss some
of the possible options. As an example, "core set debug" can
continue with "channel" from one CLI entry, and "off" or "atleast"
from another one.
We address this problem in a separate commit
(when i have figured out a fix, that is).
ABI issues:
I asked Kevin if it was ok to make this change and he said yes.
While it would have been possible to make the change without breaking
the module ABI, the code would have been more convoluted.
I am happy to restore the old ABI (while still being able
to use the "new style" handlers) if there is demand.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@47606 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2006-11-14 15:23:35 +00:00
|
|
|
CLI_GENERATE = -3, /* behave as 'generator', remap argv to struct ast_cli_args */
|
2006-11-15 14:11:28 +00:00
|
|
|
CLI_HANDLER = -4, /* run the normal handler */
|
Bring in the improved internal API for the CLI.
WATCH OUT: this changes the binary interface (ABI) for modules,
so e.g. users of g729 codecs need a rebuilt module (but read below).
The new way to write CLI handlers is described in detail in cli.h,
and there are a few converted handlers in cli.c, look for NEW_CLI.
After converting a couple of commands i am convinced that
it is reasonably convenient to use, and it makes it easier to fix the
pending CLI issues.
On passing, note a bug with the current 'complete' architecture:
if a command is a prefix of multiple CLI entries, we miss some
of the possible options. As an example, "core set debug" can
continue with "channel" from one CLI entry, and "off" or "atleast"
from another one.
We address this problem in a separate commit
(when i have figured out a fix, that is).
ABI issues:
I asked Kevin if it was ok to make this change and he said yes.
While it would have been possible to make the change without breaking
the module ABI, the code would have been more convoluted.
I am happy to restore the old ABI (while still being able
to use the "new style" handlers) if there is demand.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@47606 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2006-11-14 15:23:35 +00:00
|
|
|
};
|
|
|
|
|
2006-11-15 14:11:28 +00:00
|
|
|
/* argument for new-style CLI handler */
|
|
|
|
struct ast_cli_args {
|
|
|
|
int fd;
|
|
|
|
int argc;
|
|
|
|
char **argv;
|
|
|
|
const char *line; /* the current input line */
|
|
|
|
const char *word; /* the word we want to complete */
|
|
|
|
int pos; /* position of the word to complete */
|
|
|
|
int n; /* the iteration count (n-th entry we generate) */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ast_cli_entry;
|
2007-11-28 20:27:40 +00:00
|
|
|
typedef char *(*cli_fn)(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
|
Bring in the improved internal API for the CLI.
WATCH OUT: this changes the binary interface (ABI) for modules,
so e.g. users of g729 codecs need a rebuilt module (but read below).
The new way to write CLI handlers is described in detail in cli.h,
and there are a few converted handlers in cli.c, look for NEW_CLI.
After converting a couple of commands i am convinced that
it is reasonably convenient to use, and it makes it easier to fix the
pending CLI issues.
On passing, note a bug with the current 'complete' architecture:
if a command is a prefix of multiple CLI entries, we miss some
of the possible options. As an example, "core set debug" can
continue with "channel" from one CLI entry, and "off" or "atleast"
from another one.
We address this problem in a separate commit
(when i have figured out a fix, that is).
ABI issues:
I asked Kevin if it was ok to make this change and he said yes.
While it would have been possible to make the change without breaking
the module ABI, the code would have been more convoluted.
I am happy to restore the old ABI (while still being able
to use the "new style" handlers) if there is demand.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@47606 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2006-11-14 15:23:35 +00:00
|
|
|
|
2007-07-16 02:51:56 +00:00
|
|
|
/*! \brief descriptor for a cli entry.
|
|
|
|
* \arg \ref CLI_command_API
|
2006-11-14 16:10:32 +00:00
|
|
|
*/
|
1999-12-19 22:38:55 +00:00
|
|
|
struct ast_cli_entry {
|
Bring in the improved internal API for the CLI.
WATCH OUT: this changes the binary interface (ABI) for modules,
so e.g. users of g729 codecs need a rebuilt module (but read below).
The new way to write CLI handlers is described in detail in cli.h,
and there are a few converted handlers in cli.c, look for NEW_CLI.
After converting a couple of commands i am convinced that
it is reasonably convenient to use, and it makes it easier to fix the
pending CLI issues.
On passing, note a bug with the current 'complete' architecture:
if a command is a prefix of multiple CLI entries, we miss some
of the possible options. As an example, "core set debug" can
continue with "channel" from one CLI entry, and "off" or "atleast"
from another one.
We address this problem in a separate commit
(when i have figured out a fix, that is).
ABI issues:
I asked Kevin if it was ok to make this change and he said yes.
While it would have been possible to make the change without breaking
the module ABI, the code would have been more convoluted.
I am happy to restore the old ABI (while still being able
to use the "new style" handlers) if there is demand.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@47606 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2006-11-14 15:23:35 +00:00
|
|
|
char * const cmda[AST_MAX_CMD_LEN]; /*!< words making up the command.
|
2007-11-28 20:27:40 +00:00
|
|
|
* set the first entry to NULL for a new-style entry. */
|
|
|
|
|
|
|
|
const char *summary; /*!< Summary of the command (< 60 characters) */
|
|
|
|
const char *usage; /*!< Detailed usage information */
|
|
|
|
|
2006-09-18 19:54:18 +00:00
|
|
|
struct ast_cli_entry *deprecate_cmd;
|
2006-11-14 16:10:32 +00:00
|
|
|
|
2007-11-28 20:27:40 +00:00
|
|
|
int inuse; /*!< For keeping track of usage */
|
|
|
|
struct module *module; /*!< module this belongs to */
|
|
|
|
char *_full_cmd; /*!< built at load time from cmda[] */
|
|
|
|
int cmdlen; /*!< len up to the first invalid char [<{% */
|
2006-11-14 16:10:32 +00:00
|
|
|
/*! \brief This gets set in ast_cli_register()
|
2006-09-18 19:54:18 +00:00
|
|
|
It then gets set to something different when the deprecated command
|
|
|
|
is run for the first time (ie; after we warn the user that it's deprecated)
|
|
|
|
*/
|
2007-11-28 20:27:40 +00:00
|
|
|
int args; /*!< number of non-null entries in cmda */
|
|
|
|
char *command; /*!< command, non-null for new-style entries */
|
2006-09-18 19:54:18 +00:00
|
|
|
int deprecated;
|
2007-11-28 20:27:40 +00:00
|
|
|
cli_fn handler;
|
|
|
|
char *_deprecated_by; /*!< copied from the "parent" _full_cmd, on deprecated commands */
|
2006-02-14 22:44:20 +00:00
|
|
|
/*! For linking */
|
|
|
|
AST_LIST_ENTRY(ast_cli_entry) list;
|
1999-12-19 22:38:55 +00:00
|
|
|
};
|
|
|
|
|
2006-11-15 15:02:56 +00:00
|
|
|
/* XXX the parser in gcc 2.95 gets confused if you don't put a space
|
|
|
|
* between the last arg before VA_ARGS and the comma */
|
2007-11-28 20:27:40 +00:00
|
|
|
#define AST_CLI_DEFINE(fn, txt , ... ) { .handler = fn, .summary = txt, ## __VA_ARGS__ }
|
Bring in the improved internal API for the CLI.
WATCH OUT: this changes the binary interface (ABI) for modules,
so e.g. users of g729 codecs need a rebuilt module (but read below).
The new way to write CLI handlers is described in detail in cli.h,
and there are a few converted handlers in cli.c, look for NEW_CLI.
After converting a couple of commands i am convinced that
it is reasonably convenient to use, and it makes it easier to fix the
pending CLI issues.
On passing, note a bug with the current 'complete' architecture:
if a command is a prefix of multiple CLI entries, we miss some
of the possible options. As an example, "core set debug" can
continue with "channel" from one CLI entry, and "off" or "atleast"
from another one.
We address this problem in a separate commit
(when i have figured out a fix, that is).
ABI issues:
I asked Kevin if it was ok to make this change and he said yes.
While it would have been possible to make the change without breaking
the module ABI, the code would have been more convoluted.
I am happy to restore the old ABI (while still being able
to use the "new style" handlers) if there is demand.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@47606 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2006-11-14 15:23:35 +00:00
|
|
|
|
2006-03-28 22:25:08 +00:00
|
|
|
/*!
|
Bring in the improved internal API for the CLI.
WATCH OUT: this changes the binary interface (ABI) for modules,
so e.g. users of g729 codecs need a rebuilt module (but read below).
The new way to write CLI handlers is described in detail in cli.h,
and there are a few converted handlers in cli.c, look for NEW_CLI.
After converting a couple of commands i am convinced that
it is reasonably convenient to use, and it makes it easier to fix the
pending CLI issues.
On passing, note a bug with the current 'complete' architecture:
if a command is a prefix of multiple CLI entries, we miss some
of the possible options. As an example, "core set debug" can
continue with "channel" from one CLI entry, and "off" or "atleast"
from another one.
We address this problem in a separate commit
(when i have figured out a fix, that is).
ABI issues:
I asked Kevin if it was ok to make this change and he said yes.
While it would have been possible to make the change without breaking
the module ABI, the code would have been more convoluted.
I am happy to restore the old ABI (while still being able
to use the "new style" handlers) if there is demand.
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@47606 65c4cc65-6c06-0410-ace0-fbb531ad65f3
2006-11-14 15:23:35 +00:00
|
|
|
* Helper function to generate cli entries from a NULL-terminated array.
|
2006-03-28 22:25:08 +00:00
|
|
|
* Returns the n-th matching entry from the array, or NULL if not found.
|
|
|
|
* Can be used to implement generate() for static entries as below
|
|
|
|
* (in this example we complete the word in position 2):
|
|
|
|
\code
|
|
|
|
char *my_generate(const char *line, const char *word, int pos, int n)
|
|
|
|
{
|
|
|
|
static char *choices = { "one", "two", "three", NULL };
|
|
|
|
if (pos == 2)
|
|
|
|
return ast_cli_complete(word, choices, n);
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
\endcode
|
|
|
|
*/
|
|
|
|
char *ast_cli_complete(const char *word, char *const choices[], int pos);
|
2006-01-18 03:53:10 +00:00
|
|
|
|
2007-07-16 02:51:56 +00:00
|
|
|
/*!
|
|
|
|
* \brief Interprets a command
|
2005-10-24 20:12:06 +00:00
|
|
|
* Interpret a command s, sending output to fd
|
2007-11-12 23:44:20 +00:00
|
|
|
* \param fd pipe
|
|
|
|
* \param s incoming string
|
2007-07-16 02:51:56 +00:00
|
|
|
* \retval 0 on success
|
|
|
|
* \retval -1 on failure
|
2001-10-31 15:28:08 +00:00
|
|
|
*/
|
2006-01-18 22:17:31 +00:00
|
|
|
int ast_cli_command(int fd, const char *s);
|
1999-12-19 22:38:55 +00:00
|
|
|
|
2007-07-16 02:51:56 +00:00
|
|
|
/*!
|
2007-10-21 22:52:20 +00:00
|
|
|
* \brief Executes multiple CLI commands
|
2007-11-12 23:44:20 +00:00
|
|
|
* Interpret strings separated by NULL and execute each one, sending output to fd
|
|
|
|
* \param fd pipe
|
2007-10-21 22:52:20 +00:00
|
|
|
* \param size is the total size of the string
|
2007-11-12 23:44:20 +00:00
|
|
|
* \param s incoming string
|
2007-10-21 22:52:20 +00:00
|
|
|
* \retval number of commands executed
|
|
|
|
*/
|
|
|
|
int ast_cli_command_multiple(int fd, size_t size, const char *s);
|
|
|
|
|
|
|
|
/*! \brief Registers a command or an array of commands
|
2007-11-12 23:44:20 +00:00
|
|
|
* \param e which cli entry to register.
|
2001-10-31 15:28:08 +00:00
|
|
|
* Register your own command
|
2007-07-16 02:51:56 +00:00
|
|
|
* \retval 0 on success
|
|
|
|
* \retval -1 on failure
|
2001-10-31 15:28:08 +00:00
|
|
|
*/
|
2006-01-18 03:53:10 +00:00
|
|
|
int ast_cli_register(struct ast_cli_entry *e);
|
1999-12-19 22:38:55 +00:00
|
|
|
|
2006-03-31 10:29:50 +00:00
|
|
|
/*!
|
2005-10-24 20:12:06 +00:00
|
|
|
* \brief Register multiple commands
|
2005-05-15 03:03:48 +00:00
|
|
|
* \param e pointer to first cli entry to register
|
|
|
|
* \param len number of entries to register
|
|
|
|
*/
|
2007-02-13 22:02:20 +00:00
|
|
|
int ast_cli_register_multiple(struct ast_cli_entry *e, int len);
|
2005-05-15 03:03:48 +00:00
|
|
|
|
2007-07-16 02:51:56 +00:00
|
|
|
/*!
|
|
|
|
* \brief Unregisters a command or an array of commands
|
2001-10-31 15:28:08 +00:00
|
|
|
* \param e which cli entry to unregister
|
2005-05-15 03:03:48 +00:00
|
|
|
* Unregister your own command. You must pass a completed ast_cli_entry structure
|
2007-11-12 23:44:20 +00:00
|
|
|
* \return 0
|
2001-10-31 15:28:08 +00:00
|
|
|
*/
|
2006-01-18 03:53:10 +00:00
|
|
|
int ast_cli_unregister(struct ast_cli_entry *e);
|
1999-12-19 22:38:55 +00:00
|
|
|
|
2005-05-15 03:03:48 +00:00
|
|
|
/*!
|
2005-10-24 20:12:06 +00:00
|
|
|
* \brief Unregister multiple commands
|
2005-05-15 03:03:48 +00:00
|
|
|
* \param e pointer to first cli entry to unregister
|
|
|
|
* \param len number of entries to unregister
|
|
|
|
*/
|
2007-02-13 22:02:20 +00:00
|
|
|
int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len);
|
2005-05-15 03:03:48 +00:00
|
|
|
|
2007-07-16 02:51:56 +00:00
|
|
|
/*!
|
|
|
|
* \brief Readline madness
|
2005-10-24 20:12:06 +00:00
|
|
|
* Useful for readline, that's about it
|
2007-07-16 02:51:56 +00:00
|
|
|
* \retval 0 on success
|
|
|
|
* \retval -1 on failure
|
2001-10-31 15:28:08 +00:00
|
|
|
*/
|
2006-01-18 22:17:31 +00:00
|
|
|
char *ast_cli_generator(const char *, const char *, int);
|
2006-01-18 03:53:10 +00:00
|
|
|
|
2006-01-18 22:17:31 +00:00
|
|
|
int ast_cli_generatornummatches(const char *, const char *);
|
2006-01-18 03:53:10 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Generates a NULL-terminated array of strings that
|
2006-03-31 10:29:50 +00:00
|
|
|
* 1) begin with the string in the second parameter, and
|
2006-01-18 03:53:10 +00:00
|
|
|
* 2) are valid in a command after the string in the first parameter.
|
|
|
|
*
|
|
|
|
* The first entry (offset 0) of the result is the longest common substring
|
|
|
|
* in the results, useful to extend the string that has been completed.
|
2007-11-12 23:44:20 +00:00
|
|
|
* Subsequent entries are all possible values, followed by a NULL.
|
2006-01-18 03:53:10 +00:00
|
|
|
* All strings and the array itself are malloc'ed and must be freed
|
|
|
|
* by the caller.
|
|
|
|
*/
|
2006-01-18 22:17:31 +00:00
|
|
|
char **ast_cli_completion_matches(const char *, const char *);
|
1999-12-19 22:38:55 +00:00
|
|
|
|
2006-02-16 17:37:03 +00:00
|
|
|
/*!
|
2007-11-12 23:44:20 +00:00
|
|
|
* \brief Command completion for the list of active channels.
|
2006-02-16 17:37:03 +00:00
|
|
|
*
|
|
|
|
* This can be called from a CLI command completion function that wants to
|
|
|
|
* complete from the list of active channels. 'rpos' is the required
|
|
|
|
* position in the command. This function will return NULL immediately if
|
|
|
|
* 'rpos' is not the same as the current position, 'pos'.
|
|
|
|
*/
|
|
|
|
char *ast_complete_channels(const char *line, const char *word, int pos, int state, int rpos);
|
2002-11-27 05:04:07 +00:00
|
|
|
|
1999-12-19 22:38:55 +00:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-08-30 18:32:10 +00:00
|
|
|
#endif /* _ASTERISK_CLI_H */
|