2003-11-12 09:45:14 +03:00
|
|
|
/* $NetBSD: command.c,v 1.4 2003/11/12 06:45:14 simonb Exp $ */
|
1995-03-18 15:28:06 +03:00
|
|
|
|
1997-09-15 05:29:26 +04:00
|
|
|
/*
|
1994-04-22 04:04:22 +04:00
|
|
|
* Copyright (c) 1994 Philip A. Nelson.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by Philip A. Nelson.
|
|
|
|
* 4. The name of Philip A. Nelson may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior written
|
|
|
|
* permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY PHILIP NELSON ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL PHILIP NELSON BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 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 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-09-15 05:29:26 +04:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#ifndef lint
|
2003-11-12 09:45:14 +03:00
|
|
|
__RCSID("$NetBSD: command.c,v 1.4 2003/11/12 06:45:14 simonb Exp $");
|
1997-09-15 05:29:26 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1994-04-22 04:04:22 +04:00
|
|
|
/*
|
1997-09-15 05:29:26 +04:00
|
|
|
* command.c - The routines to implement the command processor.
|
1994-04-22 04:04:22 +04:00
|
|
|
*
|
|
|
|
* The user must add to "cmdtable.h" all commands that are available.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ctype.h>
|
1997-09-15 05:29:26 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
1994-04-22 04:04:22 +04:00
|
|
|
#include "command.h"
|
|
|
|
#include "cmdtable.h"
|
1997-09-15 05:29:26 +04:00
|
|
|
#include "extern.h"
|
1994-04-22 04:04:22 +04:00
|
|
|
|
|
|
|
|
|
|
|
/* This allows the user to define the prompt in cmdtable.h */
|
|
|
|
#ifndef PROMPT
|
|
|
|
#define PROMPT "Command (? for help): "
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* Other procedure prototypes. */
|
1997-09-15 05:29:26 +04:00
|
|
|
const struct command *find_entry __P((char *));
|
|
|
|
void help_cmds __P((void));
|
|
|
|
int parse __P((char *, char **));
|
|
|
|
int StrCmp __P((char *, char *));
|
1994-04-22 04:04:22 +04:00
|
|
|
|
|
|
|
/* A pointer to a function to be called after every command.
|
1997-09-15 05:29:26 +04:00
|
|
|
A NULL pointer says no function is to be executed.
|
1994-04-22 04:04:22 +04:00
|
|
|
The int parameter is the "done" flag.. TRUE => last command */
|
|
|
|
|
1997-09-15 05:29:26 +04:00
|
|
|
void (*after_cmd) __P((int)) = NULL;
|
1994-04-22 04:04:22 +04:00
|
|
|
|
|
|
|
/* This is the command processor loop. */
|
1997-09-15 05:29:26 +04:00
|
|
|
void
|
|
|
|
command_loop()
|
1994-04-22 04:04:22 +04:00
|
|
|
{
|
1997-09-15 05:29:26 +04:00
|
|
|
char cmdline[LINELEN];
|
|
|
|
char *args[MAXARGS];
|
|
|
|
int numargs;
|
|
|
|
const struct command *cmd;
|
|
|
|
int done = FALSE;
|
|
|
|
|
|
|
|
while (!done) {
|
|
|
|
prompt(cmdline, LINELEN, PROMPT);
|
|
|
|
numargs = parse(cmdline, args);
|
|
|
|
if (numargs == BLANK_LINE)
|
|
|
|
continue;
|
|
|
|
cmd = find_entry(args[0]);
|
|
|
|
if (cmd != NULL) {
|
|
|
|
done = (*(cmd->fn)) (numargs, args, cmd->syntax);
|
|
|
|
if (after_cmd != NULL)
|
|
|
|
(*(after_cmd)) (done);
|
|
|
|
} else
|
|
|
|
printf("Unknown command\n");
|
|
|
|
}
|
1994-04-22 04:04:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This is the command processor for a single command. */
|
|
|
|
int
|
1997-09-15 05:29:26 +04:00
|
|
|
one_command(cmdline)
|
|
|
|
char *cmdline;
|
1994-04-22 04:04:22 +04:00
|
|
|
{
|
1997-09-15 05:29:26 +04:00
|
|
|
char *args[MAXARGS];
|
|
|
|
int numargs;
|
|
|
|
const struct command *cmd;
|
|
|
|
int done;
|
|
|
|
|
|
|
|
numargs = parse(cmdline, args);
|
|
|
|
if (numargs == BLANK_LINE)
|
|
|
|
return 0;
|
|
|
|
cmd = find_entry(args[0]);
|
|
|
|
if (cmd != NULL) {
|
|
|
|
done = (*(cmd->fn)) (numargs, args, cmd->syntax);
|
|
|
|
if (after_cmd != NULL)
|
|
|
|
(*(after_cmd)) (done);
|
|
|
|
return done;
|
|
|
|
} else
|
|
|
|
return 0;
|
1994-04-22 04:04:22 +04:00
|
|
|
}
|
|
|
|
|
1997-09-15 05:29:26 +04:00
|
|
|
/* prompt procedure.... Write out the prompt and then read a response. */
|
|
|
|
void
|
|
|
|
prompt(cmdline, linelen, promptstr)
|
|
|
|
char *cmdline;
|
|
|
|
int linelen;
|
|
|
|
const char *promptstr;
|
1994-04-22 04:04:22 +04:00
|
|
|
{
|
1997-09-15 05:29:26 +04:00
|
|
|
int incount = 0;
|
|
|
|
int inchar;
|
|
|
|
|
|
|
|
/* Give the prompt. */
|
|
|
|
printf("%s", promptstr);
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
/* Read chars until newline or EOF, toss chars that won't fit in the
|
|
|
|
* array. */
|
|
|
|
while (TRUE) {
|
|
|
|
inchar = getchar();
|
|
|
|
if (inchar == '\n' || inchar == EOF)
|
|
|
|
break;
|
|
|
|
if (++incount < linelen)
|
|
|
|
*cmdline++ = inchar;
|
|
|
|
}
|
|
|
|
*cmdline = 0;
|
1994-04-22 04:04:22 +04:00
|
|
|
}
|
|
|
|
|
1997-09-15 05:29:26 +04:00
|
|
|
/*
|
|
|
|
* parse the arguments on an input line. It returns an array of
|
|
|
|
* pointers to substrings in the original string. It puts the string
|
|
|
|
* terminator in the original line.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
parse(cmdline, args)
|
|
|
|
char *cmdline;
|
|
|
|
char **args;
|
1994-04-22 04:04:22 +04:00
|
|
|
{
|
2003-11-12 09:45:14 +03:00
|
|
|
int idx;
|
1997-09-15 05:29:26 +04:00
|
|
|
int argcnt = BLANK_LINE;
|
|
|
|
|
|
|
|
/* Initialize the args. */
|
2003-11-12 09:45:14 +03:00
|
|
|
for (idx = 0; idx < MAXARGS; idx++)
|
|
|
|
args[idx] = NULL;
|
1997-09-15 05:29:26 +04:00
|
|
|
|
|
|
|
/* Start looking for the commands */
|
|
|
|
while (*cmdline != 0) {
|
|
|
|
while (isspace(*cmdline))
|
|
|
|
cmdline++; /* skip blanks. */
|
|
|
|
if (*cmdline != 0) {
|
|
|
|
/* Start of new argument. */
|
|
|
|
if (argcnt < MAXARGS)
|
|
|
|
args[argcnt] = cmdline;
|
|
|
|
while (!isspace(*cmdline) && *cmdline != 0)
|
|
|
|
cmdline++;
|
|
|
|
if (*cmdline != 0)
|
|
|
|
*cmdline++ = 0;
|
|
|
|
argcnt++;
|
|
|
|
}
|
1994-04-22 04:04:22 +04:00
|
|
|
}
|
1997-09-15 05:29:26 +04:00
|
|
|
return (argcnt < MAXARGS ? argcnt : MAXARGS - 1);
|
1994-04-22 04:04:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Search cmd_tbl for 1) a command matching what the user typed or 2)
|
|
|
|
* a unique command which is a superstring of what the user typed.
|
|
|
|
*/
|
1997-09-15 05:29:26 +04:00
|
|
|
const struct command *
|
|
|
|
find_entry(name)
|
|
|
|
char *name;
|
1994-04-22 04:04:22 +04:00
|
|
|
{
|
1997-09-15 05:29:26 +04:00
|
|
|
const struct command *item, *save;
|
|
|
|
int subcount = 0;
|
|
|
|
|
|
|
|
save = NULL;
|
|
|
|
for (item = cmd_table; item < cmd_table + CMDLEN; item++)
|
|
|
|
switch (StrCmp(name, item->name)) {
|
|
|
|
case CMP_MATCH:
|
|
|
|
return item;
|
|
|
|
case CMP_SUBSTR:
|
|
|
|
subcount++;
|
|
|
|
save = item;
|
|
|
|
break;
|
|
|
|
case CMP_NOMATCH:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (subcount == 1) ? save : NULL;
|
1994-04-22 04:04:22 +04:00
|
|
|
}
|
|
|
|
|
1997-09-15 05:29:26 +04:00
|
|
|
/*
|
|
|
|
* Returns CMP_MATCH if strings are the same, CMP_SUBSTR if p1 is a
|
1994-04-22 04:04:22 +04:00
|
|
|
* proper substring of p2, and CMP_NOMATCH if neither.
|
|
|
|
*/
|
|
|
|
int
|
1997-09-15 05:29:26 +04:00
|
|
|
StrCmp(p1, p2)
|
|
|
|
char *p1, *p2;
|
1994-04-22 04:04:22 +04:00
|
|
|
{
|
1997-09-15 05:29:26 +04:00
|
|
|
while (*p1 == *p2 && *p1 != '\0') {
|
|
|
|
++p1;
|
|
|
|
++p2;
|
|
|
|
}
|
|
|
|
if (*p1 == '\0')
|
|
|
|
return (*p2 == '\0') ? CMP_MATCH : CMP_SUBSTR;
|
|
|
|
return CMP_NOMATCH;
|
1994-04-22 04:04:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1997-09-15 05:29:26 +04:00
|
|
|
/* Other routines that may be helpful in evaluating arguments.... */
|
1994-04-22 04:04:22 +04:00
|
|
|
int
|
1997-09-15 05:29:26 +04:00
|
|
|
Str2Int(str, num)
|
|
|
|
char *str;
|
|
|
|
int *num;
|
1994-04-22 04:04:22 +04:00
|
|
|
{
|
1997-09-15 05:29:26 +04:00
|
|
|
if (!isdigit(*str) && *str != '-')
|
|
|
|
return FALSE;
|
|
|
|
*num = atoi(str);
|
|
|
|
return TRUE;
|
1994-04-22 04:04:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef NO_HELP
|
1997-09-15 05:29:26 +04:00
|
|
|
/* "?" command handler. Print help and list of commands. */
|
|
|
|
|
|
|
|
void
|
|
|
|
help_cmds()
|
1994-04-22 04:04:22 +04:00
|
|
|
{
|
1997-09-15 05:29:26 +04:00
|
|
|
const struct command *q;
|
|
|
|
|
|
|
|
printf("\nFor additional help, type HELP <command>. Commands are:\n");
|
|
|
|
for (q = cmd_table; q < cmd_table + CMDLEN; ++q)
|
|
|
|
if ((q - cmd_table) % 5 == 4)
|
|
|
|
printf("%s\n", q->name);
|
|
|
|
else
|
|
|
|
printf("%-15s", q->name) ;
|
|
|
|
if ((q - cmd_table) % 5 != 0)
|
|
|
|
printf("\n");
|
|
|
|
printf("\n");
|
1994-04-22 04:04:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The default help routine. This may be redefined by the user. */
|
1997-09-15 05:29:26 +04:00
|
|
|
int
|
|
|
|
help(num, args, syntax)
|
|
|
|
int num;
|
|
|
|
char **args;
|
|
|
|
char *syntax;
|
1994-04-22 04:04:22 +04:00
|
|
|
{
|
1997-09-15 05:29:26 +04:00
|
|
|
const struct command *item;
|
2003-11-12 09:45:14 +03:00
|
|
|
int idx;
|
1997-09-15 05:29:26 +04:00
|
|
|
int show_cmds = FALSE;
|
|
|
|
char dummy[2];
|
|
|
|
|
|
|
|
if (args[0][0] == '?')
|
|
|
|
num = 1; /* Force "no args" for ?. */
|
|
|
|
|
|
|
|
/* Print help for each argument. */
|
|
|
|
if (num > 1)
|
2003-11-12 09:45:14 +03:00
|
|
|
for (idx = 1; idx < num; idx++) {
|
|
|
|
item = find_entry(args[idx]);
|
1997-09-15 05:29:26 +04:00
|
|
|
if (item != NULL) {
|
|
|
|
printf("\n");
|
|
|
|
if (item->syntax[0] != 0)
|
|
|
|
printf("Syntax: %s\n", item->syntax);
|
|
|
|
printf("%s\n\n", item->help);
|
2003-11-12 09:45:14 +03:00
|
|
|
if (idx < num - 1)
|
1997-09-15 05:29:26 +04:00
|
|
|
prompt(dummy, 1, "cr to continue:");
|
|
|
|
} else {
|
2003-11-12 09:45:14 +03:00
|
|
|
printf("unknow command %s.\n", args[idx]);
|
1997-09-15 05:29:26 +04:00
|
|
|
show_cmds = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (show_cmds || num == 1)
|
|
|
|
help_cmds();
|
|
|
|
|
|
|
|
return FALSE;
|
1994-04-22 04:04:22 +04:00
|
|
|
}
|
|
|
|
#endif
|