1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* main.c
|
|
|
|
*
|
|
|
|
* Copyright (c) 1996-1999 Whistle Communications, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Subject to the following obligations and disclaimer of warranty, use and
|
|
|
|
* redistribution of this software, in source or object code forms, with or
|
|
|
|
* without modifications are expressly permitted by Whistle Communications;
|
|
|
|
* provided, however, that:
|
|
|
|
* 1. Any and all reproductions of the source or object code must include the
|
|
|
|
* copyright notice above and the following disclaimer of warranties; and
|
|
|
|
* 2. No rights are granted, in any manner or form, to use Whistle
|
|
|
|
* Communications, Inc. trademarks, including the mark "WHISTLE
|
|
|
|
* COMMUNICATIONS" on advertising, endorsements, or otherwise except as
|
|
|
|
* such appears in the above copyright notice or in the software.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
|
|
|
|
* TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
|
|
|
|
* REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
|
|
|
|
* INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
|
|
|
|
* WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
|
|
|
|
* REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
|
|
|
|
* SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
|
|
|
|
* IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
|
|
|
|
* RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
|
|
|
|
* WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
|
|
|
* PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
|
|
|
|
* OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
1999-11-30 02:45:32 +00:00
|
|
|
* $Whistle: main.c,v 1.12 1999/11/29 19:17:46 archie Exp $
|
1999-10-21 09:06:11 +00:00
|
|
|
*/
|
|
|
|
|
2006-05-24 14:46:55 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sysexits.h>
|
|
|
|
#include <unistd.h>
|
2006-08-07 14:17:05 +00:00
|
|
|
#ifdef EDITLINE
|
|
|
|
#include <signal.h>
|
|
|
|
#include <histedit.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
2006-05-24 14:46:55 +00:00
|
|
|
|
|
|
|
#include <netgraph.h>
|
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
#include "ngctl.h"
|
|
|
|
|
1999-11-30 02:45:32 +00:00
|
|
|
#define PROMPT "+ "
|
|
|
|
#define MAX_ARGS 512
|
|
|
|
#define WHITESPACE " \t\r\n\v\f"
|
|
|
|
#define DUMP_BYTES_PER_LINE 16
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
/* Internal functions */
|
|
|
|
static int ReadFile(FILE *fp);
|
2006-08-08 07:04:40 +00:00
|
|
|
static void ReadSockets(fd_set *);
|
2008-06-28 12:31:30 +00:00
|
|
|
static int DoParseCommand(const char *line);
|
1999-10-21 09:06:11 +00:00
|
|
|
static int DoCommand(int ac, char **av);
|
|
|
|
static int DoInteractive(void);
|
|
|
|
static const struct ngcmd *FindCommand(const char *string);
|
1999-11-30 02:45:32 +00:00
|
|
|
static int MatchCommand(const struct ngcmd *cmd, const char *s);
|
1999-10-21 09:06:11 +00:00
|
|
|
static void Usage(const char *msg);
|
|
|
|
static int ReadCmd(int ac, char **av);
|
|
|
|
static int HelpCmd(int ac, char **av);
|
|
|
|
static int QuitCmd(int ac, char **av);
|
2006-08-07 14:17:05 +00:00
|
|
|
#ifdef EDITLINE
|
|
|
|
static sig_atomic_t unblock;
|
|
|
|
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
|
|
|
|
#endif
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
/* List of commands */
|
|
|
|
static const struct ngcmd *const cmds[] = {
|
2000-07-03 13:34:18 +00:00
|
|
|
&config_cmd,
|
1999-10-21 09:06:11 +00:00
|
|
|
&connect_cmd,
|
|
|
|
&debug_cmd,
|
2004-01-09 03:19:40 +00:00
|
|
|
&dot_cmd,
|
1999-10-21 09:06:11 +00:00
|
|
|
&help_cmd,
|
|
|
|
&list_cmd,
|
|
|
|
&mkpeer_cmd,
|
1999-11-30 02:45:32 +00:00
|
|
|
&msg_cmd,
|
1999-10-21 09:06:11 +00:00
|
|
|
&name_cmd,
|
|
|
|
&read_cmd,
|
|
|
|
&rmhook_cmd,
|
|
|
|
&show_cmd,
|
|
|
|
&shutdown_cmd,
|
|
|
|
&status_cmd,
|
|
|
|
&types_cmd,
|
2002-01-22 22:01:25 +00:00
|
|
|
&write_cmd,
|
1999-10-21 09:06:11 +00:00
|
|
|
&quit_cmd,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Commands defined in this file */
|
|
|
|
const struct ngcmd read_cmd = {
|
|
|
|
ReadCmd,
|
|
|
|
"read <filename>",
|
|
|
|
"Read and execute commands from a file",
|
1999-11-30 02:45:32 +00:00
|
|
|
NULL,
|
|
|
|
{ "source", "." }
|
1999-10-21 09:06:11 +00:00
|
|
|
};
|
|
|
|
const struct ngcmd help_cmd = {
|
|
|
|
HelpCmd,
|
|
|
|
"help [command]",
|
|
|
|
"Show command summary or get more help on a specific command",
|
1999-11-30 02:45:32 +00:00
|
|
|
NULL,
|
|
|
|
{ "?" }
|
1999-10-21 09:06:11 +00:00
|
|
|
};
|
|
|
|
const struct ngcmd quit_cmd = {
|
|
|
|
QuitCmd,
|
|
|
|
"quit",
|
|
|
|
"Exit program",
|
1999-11-30 02:45:32 +00:00
|
|
|
NULL,
|
|
|
|
{ "exit" }
|
1999-10-21 09:06:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Our control and data sockets */
|
|
|
|
int csock, dsock;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* main()
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
main(int ac, char *av[])
|
|
|
|
{
|
2003-11-12 13:04:44 +00:00
|
|
|
char name[NG_NODESIZ];
|
1999-10-21 09:06:11 +00:00
|
|
|
int interactive = isatty(0) && isatty(1);
|
|
|
|
FILE *fp = NULL;
|
2005-02-04 20:09:11 +00:00
|
|
|
int ch, rtn = 0;
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
/* Set default node name */
|
|
|
|
snprintf(name, sizeof(name), "ngctl%d", getpid());
|
|
|
|
|
|
|
|
/* Parse command line */
|
2007-02-06 08:48:28 +00:00
|
|
|
while ((ch = getopt(ac, av, "df:n:")) != -1) {
|
1999-10-21 09:06:11 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'd':
|
|
|
|
NgSetDebug(NgSetDebug(-1) + 1);
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
if (strcmp(optarg, "-") == 0)
|
|
|
|
fp = stdin;
|
|
|
|
else if ((fp = fopen(optarg, "r")) == NULL)
|
|
|
|
err(EX_NOINPUT, "%s", optarg);
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
snprintf(name, sizeof(name), "%s", optarg);
|
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
Usage((char *)NULL);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ac -= optind;
|
|
|
|
av += optind;
|
|
|
|
|
|
|
|
/* Create a new socket node */
|
2000-01-28 00:50:28 +00:00
|
|
|
if (NgMkSockNode(name, &csock, &dsock) < 0)
|
1999-10-21 09:06:11 +00:00
|
|
|
err(EX_OSERR, "can't create node");
|
2005-02-04 20:09:11 +00:00
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
/* Do commands as requested */
|
|
|
|
if (ac == 0) {
|
|
|
|
if (fp != NULL) {
|
|
|
|
rtn = ReadFile(fp);
|
|
|
|
} else if (interactive) {
|
|
|
|
rtn = DoInteractive();
|
|
|
|
} else
|
|
|
|
Usage("no command specified");
|
|
|
|
} else {
|
|
|
|
rtn = DoCommand(ac, av);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert command return code into system exit code */
|
|
|
|
switch (rtn) {
|
|
|
|
case CMDRTN_OK:
|
|
|
|
case CMDRTN_QUIT:
|
|
|
|
rtn = 0;
|
|
|
|
break;
|
|
|
|
case CMDRTN_USAGE:
|
|
|
|
rtn = EX_USAGE;
|
|
|
|
break;
|
|
|
|
case CMDRTN_ERROR:
|
|
|
|
rtn = EX_OSERR;
|
|
|
|
break;
|
|
|
|
}
|
2006-06-28 10:38:38 +00:00
|
|
|
return (rtn);
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process commands from a file
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ReadFile(FILE *fp)
|
|
|
|
{
|
|
|
|
char line[LINE_MAX];
|
|
|
|
int num, rtn;
|
|
|
|
|
|
|
|
for (num = 1; fgets(line, sizeof(line), fp) != NULL; num++) {
|
|
|
|
if (*line == '#')
|
|
|
|
continue;
|
|
|
|
if ((rtn = DoParseCommand(line)) != 0) {
|
|
|
|
warnx("line %d: error in file", num);
|
2006-06-28 10:38:38 +00:00
|
|
|
return (rtn);
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
}
|
2006-06-28 10:38:38 +00:00
|
|
|
return (CMDRTN_OK);
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
2006-08-07 14:17:05 +00:00
|
|
|
#ifdef EDITLINE
|
|
|
|
/* Signal handler for Monitor() thread. */
|
|
|
|
static void
|
|
|
|
Unblock(int signal)
|
|
|
|
{
|
2006-08-08 07:04:40 +00:00
|
|
|
|
2006-08-07 14:17:05 +00:00
|
|
|
unblock = 1;
|
|
|
|
}
|
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
/*
|
2006-08-07 14:17:05 +00:00
|
|
|
* Thread that monitors csock and dsock while main thread
|
|
|
|
* can be blocked in el_gets().
|
|
|
|
*/
|
|
|
|
static void *
|
|
|
|
Monitor(void *v)
|
|
|
|
{
|
|
|
|
struct sigaction act;
|
|
|
|
const int maxfd = MAX(csock, dsock) + 1;
|
|
|
|
|
|
|
|
act.sa_handler = Unblock;
|
|
|
|
sigemptyset(&act.sa_mask);
|
|
|
|
act.sa_flags = 0;
|
|
|
|
sigaction(SIGUSR1, &act, NULL);
|
|
|
|
|
|
|
|
pthread_mutex_lock(&mutex);
|
|
|
|
for (;;) {
|
|
|
|
fd_set rfds;
|
|
|
|
|
|
|
|
/* See if any data or control messages are arriving. */
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(csock, &rfds);
|
|
|
|
FD_SET(dsock, &rfds);
|
|
|
|
unblock = 0;
|
|
|
|
if (select(maxfd, &rfds, NULL, NULL, NULL) <= 0) {
|
|
|
|
if (errno == EINTR) {
|
|
|
|
if (unblock == 1)
|
|
|
|
pthread_cond_wait(&cond, &mutex);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
err(EX_OSERR, "select");
|
|
|
|
}
|
2006-08-08 07:04:40 +00:00
|
|
|
ReadSockets(&rfds);
|
2006-08-07 14:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
Prompt(EditLine *el)
|
|
|
|
{
|
2006-08-08 07:04:40 +00:00
|
|
|
|
|
|
|
return (PROMPT);
|
2006-08-07 14:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Here we start a thread, that will monitor the netgraph
|
|
|
|
* sockets and catch any unexpected messages or data on them,
|
|
|
|
* that can arrive while user edits his/her commands.
|
|
|
|
*
|
|
|
|
* Whenever we expect data on netgraph sockets, we send signal
|
|
|
|
* to monitoring thread. The signal forces it to exit select()
|
|
|
|
* system call and sleep on condvar until we wake it. While
|
|
|
|
* monitoring thread sleeps, we can do our work with netgraph
|
|
|
|
* sockets.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
DoInteractive(void)
|
|
|
|
{
|
|
|
|
pthread_t monitor;
|
|
|
|
EditLine *el;
|
|
|
|
History *hist;
|
|
|
|
HistEvent hev = { 0, "" };
|
|
|
|
|
|
|
|
(*help_cmd.func)(0, NULL);
|
|
|
|
pthread_create(&monitor, NULL, Monitor, NULL);
|
|
|
|
el = el_init(getprogname(), stdin, stdout, stderr);
|
|
|
|
if (el == NULL)
|
|
|
|
return (CMDRTN_ERROR);
|
|
|
|
el_set(el, EL_PROMPT, Prompt);
|
|
|
|
el_set(el, EL_SIGNAL, 1);
|
|
|
|
el_set(el, EL_EDITOR, "emacs");
|
|
|
|
hist = history_init();
|
|
|
|
if (hist == NULL)
|
|
|
|
return (CMDRTN_ERROR);
|
|
|
|
history(hist, &hev, H_SETSIZE, 100);
|
|
|
|
history(hist, &hev, H_SETUNIQUE, 1);
|
|
|
|
el_set(el, EL_HIST, history, (const char *)hist);
|
|
|
|
el_source(el, NULL);
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
const char *buf;
|
|
|
|
int count;
|
|
|
|
|
|
|
|
if ((buf = el_gets(el, &count)) == NULL) {
|
|
|
|
printf("\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
history(hist, &hev, H_ENTER, buf);
|
|
|
|
pthread_kill(monitor, SIGUSR1);
|
|
|
|
pthread_mutex_lock(&mutex);
|
2008-06-28 12:31:30 +00:00
|
|
|
if (DoParseCommand(buf) == CMDRTN_QUIT)
|
2006-08-07 14:17:05 +00:00
|
|
|
break;
|
|
|
|
pthread_cond_signal(&cond);
|
|
|
|
pthread_mutex_unlock(&mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
history_end(hist);
|
|
|
|
el_end(el);
|
|
|
|
pthread_cancel(monitor);
|
|
|
|
|
|
|
|
return (CMDRTN_QUIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* !EDITLINE */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Interactive mode w/o libedit functionality.
|
1999-10-21 09:06:11 +00:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
DoInteractive(void)
|
|
|
|
{
|
1999-11-30 02:45:32 +00:00
|
|
|
const int maxfd = MAX(csock, dsock) + 1;
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
(*help_cmd.func)(0, NULL);
|
1999-11-30 02:45:32 +00:00
|
|
|
while (1) {
|
|
|
|
struct timeval tv;
|
|
|
|
fd_set rfds;
|
|
|
|
|
|
|
|
/* See if any data or control messages are arriving */
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(csock, &rfds);
|
|
|
|
FD_SET(dsock, &rfds);
|
|
|
|
memset(&tv, 0, sizeof(tv));
|
|
|
|
if (select(maxfd, &rfds, NULL, NULL, &tv) <= 0) {
|
|
|
|
|
|
|
|
/* Issue prompt and wait for anything to happen */
|
|
|
|
printf("%s", PROMPT);
|
|
|
|
fflush(stdout);
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_SET(0, &rfds);
|
|
|
|
FD_SET(csock, &rfds);
|
|
|
|
FD_SET(dsock, &rfds);
|
|
|
|
if (select(maxfd, &rfds, NULL, NULL, NULL) < 0)
|
|
|
|
err(EX_OSERR, "select");
|
|
|
|
|
|
|
|
/* If not user input, print a newline first */
|
|
|
|
if (!FD_ISSET(0, &rfds))
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
2006-08-08 07:04:40 +00:00
|
|
|
ReadSockets(&rfds);
|
1999-11-30 02:45:32 +00:00
|
|
|
|
|
|
|
/* Get any user input */
|
|
|
|
if (FD_ISSET(0, &rfds)) {
|
2001-03-17 08:59:31 +00:00
|
|
|
char buf[LINE_MAX];
|
|
|
|
|
|
|
|
if (fgets(buf, sizeof(buf), stdin) == NULL) {
|
|
|
|
printf("\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (DoParseCommand(buf) == CMDRTN_QUIT)
|
1999-11-30 02:45:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-06-28 10:38:38 +00:00
|
|
|
return (CMDRTN_QUIT);
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
2006-08-07 14:17:05 +00:00
|
|
|
#endif /* !EDITLINE */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read and process data on netgraph control and data sockets.
|
|
|
|
*/
|
|
|
|
static void
|
2006-08-08 07:04:40 +00:00
|
|
|
ReadSockets(fd_set *rfds)
|
2006-08-07 14:17:05 +00:00
|
|
|
{
|
|
|
|
/* Display any incoming control message. */
|
2006-08-08 07:04:40 +00:00
|
|
|
if (FD_ISSET(csock, rfds))
|
2006-08-07 14:17:05 +00:00
|
|
|
MsgRead();
|
|
|
|
|
|
|
|
/* Display any incoming data packet. */
|
2006-08-08 07:04:40 +00:00
|
|
|
if (FD_ISSET(dsock, rfds)) {
|
2006-08-07 14:17:05 +00:00
|
|
|
char hook[NG_HOOKSIZ];
|
|
|
|
u_char *buf;
|
|
|
|
int rl;
|
|
|
|
|
|
|
|
/* Read packet from socket. */
|
|
|
|
if ((rl = NgAllocRecvData(dsock, &buf, hook)) < 0)
|
|
|
|
err(EX_OSERR, "reading hook \"%s\"", hook);
|
|
|
|
if (rl == 0)
|
|
|
|
errx(EX_OSERR, "EOF from hook \"%s\"?", hook);
|
|
|
|
|
|
|
|
/* Write packet to stdout. */
|
|
|
|
printf("Rec'd data packet on hook \"%s\":\n", hook);
|
|
|
|
DumpAscii(buf, rl);
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
}
|
1999-10-21 09:06:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse a command line and execute the command
|
|
|
|
*/
|
|
|
|
static int
|
2008-06-28 12:31:30 +00:00
|
|
|
DoParseCommand(const char *line)
|
1999-10-21 09:06:11 +00:00
|
|
|
{
|
|
|
|
char *av[MAX_ARGS];
|
|
|
|
int ac;
|
|
|
|
|
|
|
|
/* Parse line */
|
2008-06-28 12:31:30 +00:00
|
|
|
for (ac = 0, av[0] = strtok((char *)line, WHITESPACE);
|
1999-10-21 09:06:11 +00:00
|
|
|
ac < MAX_ARGS - 1 && av[ac];
|
|
|
|
av[++ac] = strtok(NULL, WHITESPACE));
|
|
|
|
|
|
|
|
/* Do command */
|
2006-06-28 10:38:38 +00:00
|
|
|
return (DoCommand(ac, av));
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Execute the command
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
DoCommand(int ac, char **av)
|
|
|
|
{
|
|
|
|
const struct ngcmd *cmd;
|
|
|
|
int rtn;
|
|
|
|
|
|
|
|
if (ac == 0 || *av[0] == 0)
|
2006-06-28 10:38:38 +00:00
|
|
|
return (CMDRTN_OK);
|
1999-10-21 09:06:11 +00:00
|
|
|
if ((cmd = FindCommand(av[0])) == NULL)
|
2006-06-28 10:38:38 +00:00
|
|
|
return (CMDRTN_ERROR);
|
1999-10-21 09:06:11 +00:00
|
|
|
if ((rtn = (*cmd->func)(ac, av)) == CMDRTN_USAGE)
|
|
|
|
warnx("usage: %s", cmd->cmd);
|
2006-06-28 10:38:38 +00:00
|
|
|
return (rtn);
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find a command
|
|
|
|
*/
|
|
|
|
static const struct ngcmd *
|
|
|
|
FindCommand(const char *string)
|
|
|
|
{
|
1999-11-30 02:45:32 +00:00
|
|
|
int k, found = -1;
|
|
|
|
|
|
|
|
for (k = 0; cmds[k] != NULL; k++) {
|
|
|
|
if (MatchCommand(cmds[k], string)) {
|
1999-10-21 09:06:11 +00:00
|
|
|
if (found != -1) {
|
|
|
|
warnx("\"%s\": ambiguous command", string);
|
2006-06-28 10:38:38 +00:00
|
|
|
return (NULL);
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
found = k;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (found == -1) {
|
|
|
|
warnx("\"%s\": unknown command", string);
|
2006-06-28 10:38:38 +00:00
|
|
|
return (NULL);
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
2006-06-28 10:38:38 +00:00
|
|
|
return (cmds[found]);
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
1999-11-30 02:45:32 +00:00
|
|
|
/*
|
|
|
|
* See if string matches a prefix of "cmd" (or an alias) case insensitively
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
MatchCommand(const struct ngcmd *cmd, const char *s)
|
|
|
|
{
|
|
|
|
int a;
|
|
|
|
|
|
|
|
/* Try to match command, ignoring the usage stuff */
|
|
|
|
if (strlen(s) <= strcspn(cmd->cmd, WHITESPACE)) {
|
|
|
|
if (strncasecmp(s, cmd->cmd, strlen(s)) == 0)
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Try to match aliases */
|
|
|
|
for (a = 0; a < MAX_CMD_ALIAS && cmd->aliases[a] != NULL; a++) {
|
|
|
|
if (strlen(cmd->aliases[a]) >= strlen(s)) {
|
|
|
|
if (strncasecmp(s, cmd->aliases[a], strlen(s)) == 0)
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No match */
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
/*
|
|
|
|
* ReadCmd()
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
ReadCmd(int ac, char **av)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
int rtn;
|
|
|
|
|
|
|
|
/* Open file */
|
|
|
|
switch (ac) {
|
|
|
|
case 2:
|
2000-10-10 01:34:20 +00:00
|
|
|
if ((fp = fopen(av[1], "r")) == NULL) {
|
1999-10-21 09:06:11 +00:00
|
|
|
warn("%s", av[1]);
|
2006-06-28 10:38:38 +00:00
|
|
|
return (CMDRTN_ERROR);
|
2000-10-10 01:34:20 +00:00
|
|
|
}
|
|
|
|
break;
|
1999-10-21 09:06:11 +00:00
|
|
|
default:
|
2006-06-28 10:38:38 +00:00
|
|
|
return (CMDRTN_USAGE);
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Process it */
|
|
|
|
rtn = ReadFile(fp);
|
|
|
|
fclose(fp);
|
2006-06-28 10:38:38 +00:00
|
|
|
return (rtn);
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* HelpCmd()
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
HelpCmd(int ac, char **av)
|
|
|
|
{
|
|
|
|
const struct ngcmd *cmd;
|
|
|
|
int k;
|
|
|
|
|
|
|
|
switch (ac) {
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
/* Show all commands */
|
|
|
|
printf("Available commands:\n");
|
|
|
|
for (k = 0; cmds[k] != NULL; k++) {
|
|
|
|
char *s, buf[100];
|
|
|
|
|
|
|
|
cmd = cmds[k];
|
|
|
|
snprintf(buf, sizeof(buf), "%s", cmd->cmd);
|
|
|
|
for (s = buf; *s != '\0' && !isspace(*s); s++);
|
|
|
|
*s = '\0';
|
|
|
|
printf(" %-10s %s\n", buf, cmd->desc);
|
|
|
|
}
|
2006-06-28 10:38:38 +00:00
|
|
|
return (CMDRTN_OK);
|
1999-10-21 09:06:11 +00:00
|
|
|
default:
|
|
|
|
/* Show help on a specific command */
|
|
|
|
if ((cmd = FindCommand(av[1])) != NULL) {
|
2002-04-22 13:44:47 +00:00
|
|
|
printf("usage: %s\n", cmd->cmd);
|
1999-11-30 02:45:32 +00:00
|
|
|
if (cmd->aliases[0] != NULL) {
|
|
|
|
int a = 0;
|
|
|
|
|
|
|
|
printf("Aliases: ");
|
|
|
|
while (1) {
|
|
|
|
printf("%s", cmd->aliases[a++]);
|
|
|
|
if (a == MAX_CMD_ALIAS
|
|
|
|
|| cmd->aliases[a] == NULL) {
|
|
|
|
printf("\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
printf(", ");
|
|
|
|
}
|
|
|
|
}
|
1999-10-21 09:06:11 +00:00
|
|
|
printf("Summary: %s\n", cmd->desc);
|
|
|
|
if (cmd->help != NULL) {
|
|
|
|
const char *s;
|
|
|
|
char buf[65];
|
|
|
|
int tot, len, done;
|
|
|
|
|
|
|
|
printf("Description:\n");
|
|
|
|
for (s = cmd->help; *s != '\0'; s += len) {
|
|
|
|
while (isspace(*s))
|
|
|
|
s++;
|
|
|
|
tot = snprintf(buf,
|
|
|
|
sizeof(buf), "%s", s);
|
|
|
|
len = strlen(buf);
|
|
|
|
done = len == tot;
|
|
|
|
if (!done) {
|
|
|
|
while (len > 0
|
|
|
|
&& !isspace(buf[len-1]))
|
|
|
|
buf[--len] = '\0';
|
|
|
|
}
|
|
|
|
printf(" %s\n", buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-06-28 10:38:38 +00:00
|
|
|
return (CMDRTN_OK);
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* QuitCmd()
|
|
|
|
*/
|
|
|
|
static int
|
2004-01-26 10:27:18 +00:00
|
|
|
QuitCmd(int ac __unused, char **av __unused)
|
1999-10-21 09:06:11 +00:00
|
|
|
{
|
2006-06-28 10:38:38 +00:00
|
|
|
return (CMDRTN_QUIT);
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|
|
|
|
|
1999-11-30 02:45:32 +00:00
|
|
|
/*
|
|
|
|
* Dump data in hex and ASCII form
|
|
|
|
*/
|
2000-06-20 18:51:38 +00:00
|
|
|
void
|
1999-11-30 02:45:32 +00:00
|
|
|
DumpAscii(const u_char *buf, int len)
|
|
|
|
{
|
|
|
|
char ch, sbuf[100];
|
|
|
|
int k, count;
|
|
|
|
|
|
|
|
for (count = 0; count < len; count += DUMP_BYTES_PER_LINE) {
|
|
|
|
snprintf(sbuf, sizeof(sbuf), "%04x: ", count);
|
|
|
|
for (k = 0; k < DUMP_BYTES_PER_LINE; k++) {
|
|
|
|
if (count + k < len) {
|
|
|
|
snprintf(sbuf + strlen(sbuf),
|
|
|
|
sizeof(sbuf) - strlen(sbuf),
|
|
|
|
"%02x ", buf[count + k]);
|
|
|
|
} else {
|
|
|
|
snprintf(sbuf + strlen(sbuf),
|
|
|
|
sizeof(sbuf) - strlen(sbuf), " ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
snprintf(sbuf + strlen(sbuf), sizeof(sbuf) - strlen(sbuf), " ");
|
|
|
|
for (k = 0; k < DUMP_BYTES_PER_LINE; k++) {
|
|
|
|
if (count + k < len) {
|
|
|
|
ch = isprint(buf[count + k]) ?
|
|
|
|
buf[count + k] : '.';
|
|
|
|
snprintf(sbuf + strlen(sbuf),
|
|
|
|
sizeof(sbuf) - strlen(sbuf), "%c", ch);
|
|
|
|
} else {
|
|
|
|
snprintf(sbuf + strlen(sbuf),
|
|
|
|
sizeof(sbuf) - strlen(sbuf), " ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("%s\n", sbuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-10-21 09:06:11 +00:00
|
|
|
/*
|
|
|
|
* Usage()
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
Usage(const char *msg)
|
|
|
|
{
|
|
|
|
if (msg)
|
|
|
|
warnx("%s", msg);
|
2004-01-04 16:11:29 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"usage: ngctl [-d] [-f file] [-n name] [command ...]\n");
|
|
|
|
exit(EX_USAGE);
|
1999-10-21 09:06:11 +00:00
|
|
|
}
|