Rototill global command matching -- switch from ad-hoc tomfoolery to a

table lookup.  This will make prefix matching and so on easier.

Expect a similar change for mode-specific commands before too long.

While there, rename some structures with misleading names.

Watch this space for more changes soon.
This commit is contained in:
jwise 1999-12-16 04:02:22 +00:00
parent e9bbfca96b
commit 06f376613f
6 changed files with 71 additions and 79 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: Makefile,v 1.19 1999/11/15 06:16:56 simonb Exp $ # $NetBSD: Makefile,v 1.20 1999/12/16 04:02:22 jwise Exp $
# @(#)Makefile 8.1 (Berkeley) 6/6/93 # @(#)Makefile 8.1 (Berkeley) 6/6/93
PROG= systat PROG= systat
@ -7,9 +7,9 @@ PROG= systat
CPPFLAGS+=-I${.CURDIR}/../../usr.bin/vmstat CPPFLAGS+=-I${.CURDIR}/../../usr.bin/vmstat
CWARNFLAGS+= -Wno-format-y2k CWARNFLAGS+= -Wno-format-y2k
SRCS= bufcache.c cmds.c cmdtab.c disks.c dkstats.c fetch.c icmp.c iostat.c \ SRCS= bufcache.c cmds.c cmdtab.c disks.c dkstats.c fetch.c globalcmds.c \
ip.c keyboard.c main.c mbufs.c netcmds.c netstat.c pigs.c ps.c swap.c \ icmp.c iostat.c ip.c keyboard.c main.c mbufs.c netcmds.c netstat.c \
tcp.c vmstat.c pigs.c ps.c swap.c tcp.c vmstat.c
DPADD= ${LIBCURSES} ${LIBM} ${LIBKVM} DPADD= ${LIBCURSES} ${LIBM} ${LIBKVM}
LDADD= -lcurses -lm -lkvm LDADD= -lcurses -lm -lkvm
BINGRP= kmem BINGRP= kmem

View File

@ -1,4 +1,4 @@
/* $NetBSD: cmds.c,v 1.10 1999/08/02 02:01:57 sommerfeld Exp $ */ /* $NetBSD: cmds.c,v 1.11 1999/12/16 04:02:22 jwise Exp $ */
/*- /*-
* Copyright (c) 1980, 1992, 1993 * Copyright (c) 1980, 1992, 1993
@ -38,7 +38,7 @@
#if 0 #if 0
static char sccsid[] = "@(#)cmds.c 8.2 (Berkeley) 4/29/95"; static char sccsid[] = "@(#)cmds.c 8.2 (Berkeley) 4/29/95";
#endif #endif
__RCSID("$NetBSD: cmds.c,v 1.10 1999/08/02 02:01:57 sommerfeld Exp $"); __RCSID("$NetBSD: cmds.c,v 1.11 1999/12/16 04:02:22 jwise Exp $");
#endif /* not lint */ #endif /* not lint */
#include <stdlib.h> #include <stdlib.h>
@ -53,7 +53,8 @@ void
command(cmd) command(cmd)
char *cmd; char *cmd;
{ {
struct cmdtab *p; struct command *c;
struct mode *p;
char *cp; char *cp;
int interval; int interval;
sigset_t set; sigset_t set;
@ -69,33 +70,14 @@ command(cmd)
return; return;
for (; *cp && isspace((unsigned char)*cp); cp++) for (; *cp && isspace((unsigned char)*cp); cp++)
; ;
if (strcmp(cmd, "quit") == 0 || strcmp(cmd, "q") == 0)
die(0);
if (strcmp(cmd, "load") == 0) {
load();
goto done;
}
if (strcmp(cmd, "stop") == 0) {
alarm(0);
mvaddstr(CMDLINE, 0, "Refresh disabled.");
clrtoeol();
goto done;
}
if (strcmp(cmd, "help") == 0) {
int col, len;
move(CMDLINE, col = 0); for (c = global_commands; c->c_name; c++) {
for (p = cmdtab; p->c_name; p++) { if (strcmp(cmd, c->c_name) == 0) {
len = strlen(p->c_name); (c->c_cmd)();
if (col + len > COLS) goto done;
break;
addstr(p->c_name); col += len;
if (col + 1 < COLS)
addch(' ');
} }
clrtoeol();
goto done;
} }
interval = atoi(cmd); interval = atoi(cmd);
if (interval <= 0 && if (interval <= 0 &&
(strcmp(cmd, "start") == 0 || strcmp(cmd, "interval") == 0)) { (strcmp(cmd, "start") == 0 || strcmp(cmd, "interval") == 0)) {
@ -113,24 +95,24 @@ command(cmd)
goto done; goto done;
} }
p = lookup(cmd); p = lookup(cmd);
if (p == (struct cmdtab *)-1) { if (p == (struct mode *)-1) {
error("%s: Ambiguous command.", cmd); error("%s: Ambiguous command.", cmd);
goto done; goto done;
} }
if (p) { if (p) {
if (curcmd == p) if (curmode == p)
goto done; goto done;
alarm(0); alarm(0);
(*curcmd->c_close)(wnd); (*curmode->c_close)(wnd);
wnd = (*p->c_open)(); wnd = (*p->c_open)();
if (wnd == 0) { if (wnd == 0) {
error("Couldn't open new display"); error("Couldn't open new display");
wnd = (*curcmd->c_open)(); wnd = (*curmode->c_open)();
if (wnd == 0) { if (wnd == 0) {
error("Couldn't change back to previous cmd"); error("Couldn't change back to previous cmd");
exit(1); exit(1);
} }
p = curcmd; p = curmode;
} }
if ((p->c_flags & CF_INIT) == 0) { if ((p->c_flags & CF_INIT) == 0) {
if ((*p->c_init)()) if ((*p->c_init)())
@ -138,30 +120,30 @@ command(cmd)
else else
goto done; goto done;
} }
curcmd = p; curmode = p;
labels(); labels();
display(0); display(0);
status(); status();
goto done; goto done;
} }
if (curcmd->c_cmd == 0 || !(*curcmd->c_cmd)(cmd, cp)) if (curmode->c_cmd == 0 || !(*curmode->c_cmd)(cmd, cp))
error("%s: Unknown command.", cmd); error("%s: Unknown command.", cmd);
done: done:
sigprocmask(SIG_UNBLOCK, &set, NULL); sigprocmask(SIG_UNBLOCK, &set, NULL);
} }
struct cmdtab * struct mode *
lookup(name) lookup(name)
char *name; char *name;
{ {
char *p, *q; char *p, *q;
struct cmdtab *c, *found; struct mode *c, *found;
int nmatches, longest; int nmatches, longest;
longest = 0; longest = 0;
nmatches = 0; nmatches = 0;
found = (struct cmdtab *) 0; found = (struct mode *) 0;
for (c = cmdtab; (p = c->c_name); c++) { for (c = modes; (p = c->c_name); c++) {
for (q = name; *q == *p++; q++) for (q = name; *q == *p++; q++)
if (*q == 0) /* exact match? */ if (*q == 0) /* exact match? */
return (c); return (c);
@ -175,14 +157,14 @@ lookup(name)
} }
} }
if (nmatches > 1) if (nmatches > 1)
return ((struct cmdtab *)-1); return ((struct mode *)-1);
return (found); return (found);
} }
void void
status() status()
{ {
error("Showing %s, refresh every %d seconds.", curcmd->c_name, naptime); error("Showing %s, refresh every %d seconds.", curmode->c_name, naptime);
} }
/* case insensitive prefix comparison */ /* case insensitive prefix comparison */

View File

@ -1,4 +1,4 @@
/* $NetBSD: cmdtab.c,v 1.8 1999/11/15 06:16:56 simonb Exp $ */ /* $NetBSD: cmdtab.c,v 1.9 1999/12/16 04:02:22 jwise Exp $ */
/*- /*-
* Copyright (c) 1980, 1992, 1993 * Copyright (c) 1980, 1992, 1993
@ -38,13 +38,13 @@
#if 0 #if 0
static char sccsid[] = "@(#)cmdtab.c 8.1 (Berkeley) 6/6/93"; static char sccsid[] = "@(#)cmdtab.c 8.1 (Berkeley) 6/6/93";
#endif #endif
__RCSID("$NetBSD: cmdtab.c,v 1.8 1999/11/15 06:16:56 simonb Exp $"); __RCSID("$NetBSD: cmdtab.c,v 1.9 1999/12/16 04:02:22 jwise Exp $");
#endif /* not lint */ #endif /* not lint */
#include "systat.h" #include "systat.h"
#include "extern.h" #include "extern.h"
struct cmdtab cmdtab[] = { struct mode modes[] = {
/* "pigs" is the default, it must be first. */ /* "pigs" is the default, it must be first. */
{ "pigs", showpigs, fetchpigs, labelpigs, { "pigs", showpigs, fetchpigs, labelpigs,
initpigs, openpigs, closepigs, 0, initpigs, openpigs, closepigs, 0,
@ -84,4 +84,14 @@ struct cmdtab cmdtab[] = {
0 }, 0 },
{ 0 } { 0 }
}; };
struct cmdtab *curcmd = &cmdtab[0]; struct mode *curmode = &modes[0];
struct command global_commands[] = {
{ "help", global_help, "show help"},
{ "load", global_load, "show system load averages"},
{ "quit", global_quit, "exit systat"},
/* until prefix matching works, handle the same special case */
{ "q", global_quit, "exit systat"},
{ "stop", global_stop, "stop updating display"},
{ 0 }
};

View File

@ -1,4 +1,4 @@
/* $NetBSD: extern.h,v 1.11 1999/11/15 06:16:56 simonb Exp $ */ /* $NetBSD: extern.h,v 1.12 1999/12/16 04:02:23 jwise Exp $ */
/*- /*-
* Copyright (c) 1991, 1993 * Copyright (c) 1991, 1993
@ -39,8 +39,9 @@
#include <fcntl.h> #include <fcntl.h>
#include <kvm.h> #include <kvm.h>
extern struct cmdtab *curcmd; extern struct command global_commands[];
extern struct cmdtab cmdtab[]; extern struct mode *curmode;
extern struct mode modes[];
extern struct text *xtext; extern struct text *xtext;
extern WINDOW *wnd; extern WINDOW *wnd;
extern char **dr_name; extern char **dr_name;
@ -76,7 +77,7 @@ void closetcp __P ((WINDOW *));
int cmdiostat __P((char *, char *)); int cmdiostat __P((char *, char *));
int cmdkre __P((char *, char *)); int cmdkre __P((char *, char *));
int cmdnetstat __P((char *, char *)); int cmdnetstat __P((char *, char *));
struct cmdtab *lookup __P((char *)); struct mode *lookup __P((char *));
void command __P((char *)); void command __P((char *));
void die __P((int)); void die __P((int));
void display __P((int)); void display __P((int));
@ -93,6 +94,10 @@ void fetchnetstat __P((void));
void fetchpigs __P((void)); void fetchpigs __P((void));
void fetchswap __P((void)); void fetchswap __P((void));
void fetchtcp __P((void)); void fetchtcp __P((void));
void global_help __P((void));
void global_load __P((void));
void global_quit __P((void));
void global_stop __P((void));
int initbufcache __P((void)); int initbufcache __P((void));
int initicmp __P((void)); int initicmp __P((void));
int initiostat __P((void)); int initiostat __P((void));
@ -118,7 +123,6 @@ void labels __P((void));
void labelswap __P((void)); void labelswap __P((void));
void labeltcp __P((void)); void labeltcp __P((void));
void labeltcpsyn __P((void)); void labeltcpsyn __P((void));
void load __P((void));
int netcmd __P((char *, char *)); int netcmd __P((char *, char *));
void nlisterr __P((struct nlist [])); void nlisterr __P((struct nlist []));
WINDOW *openbufcache __P((void)); WINDOW *openbufcache __P((void));

View File

@ -1,4 +1,4 @@
/* $NetBSD: main.c,v 1.17 1999/11/11 03:06:04 soren Exp $ */ /* $NetBSD: main.c,v 1.18 1999/12/16 04:02:23 jwise Exp $ */
/*- /*-
* Copyright (c) 1980, 1992, 1993 * Copyright (c) 1980, 1992, 1993
@ -40,7 +40,7 @@ __COPYRIGHT("@(#) Copyright (c) 1980, 1992, 1993\n\
#if 0 #if 0
static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93"; static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
#endif #endif
__RCSID("$NetBSD: main.c,v 1.17 1999/11/11 03:06:04 soren Exp $"); __RCSID("$NetBSD: main.c,v 1.18 1999/12/16 04:02:23 jwise Exp $");
#endif /* not lint */ #endif /* not lint */
#include <sys/param.h> #include <sys/param.h>
@ -125,14 +125,14 @@ main(argc, argv)
if (naptime <= 0) if (naptime <= 0)
naptime = 5; naptime = 5;
} else { } else {
struct cmdtab *p; struct mode *p;
p = lookup(&argv[0][0]); p = lookup(&argv[0][0]);
if (p == (struct cmdtab *)-1) if (p == (struct mode *)-1)
errx(1, "ambiguous request: %s", &argv[0][0]); errx(1, "ambiguous request: %s", &argv[0][0]);
if (p == 0) if (p == 0)
errx(1, "unknown request: %s", &argv[0][0]); errx(1, "unknown request: %s", &argv[0][0]);
curcmd = p; curmode = p;
} }
argc--, argv++; argc--, argv++;
} }
@ -182,7 +182,7 @@ main(argc, argv)
} }
CMDLINE = LINES - 1; CMDLINE = LINES - 1;
wnd = (*curcmd->c_open)(); wnd = (*curmode->c_open)();
if (wnd == NULL) { if (wnd == NULL) {
warnx("couldn't initialize display"); warnx("couldn't initialize display");
die(0); die(0);
@ -196,8 +196,8 @@ main(argc, argv)
hostname[sizeof(hostname) - 1] = '\0'; hostname[sizeof(hostname) - 1] = '\0';
NREAD(X_HZ, &hz, sizeof hz); NREAD(X_HZ, &hz, sizeof hz);
NREAD(X_STATHZ, &stathz, sizeof stathz); NREAD(X_STATHZ, &stathz, sizeof stathz);
(*curcmd->c_init)(); (*curmode->c_init)();
curcmd->c_flags |= CF_INIT; curmode->c_flags |= CF_INIT;
labels(); labels();
dellave = 0.0; dellave = 0.0;
@ -222,12 +222,12 @@ usage()
void void
labels() labels()
{ {
if (curcmd->c_flags & CF_LOADAV) { if (curmode->c_flags & CF_LOADAV) {
mvaddstr(2, 20, mvaddstr(2, 20,
"/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10"); "/0 /1 /2 /3 /4 /5 /6 /7 /8 /9 /10");
mvaddstr(3, 5, "Load Average"); mvaddstr(3, 5, "Load Average");
} }
(*curcmd->c_label)(); (*curmode->c_label)();
#ifdef notdef #ifdef notdef
mvprintw(21, 25, "CPU usage on %s", hostname); mvprintw(21, 25, "CPU usage on %s", hostname);
#endif #endif
@ -242,8 +242,8 @@ display(signo)
/* Get the load average over the last minute. */ /* Get the load average over the last minute. */
(void)getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0])); (void)getloadavg(avenrun, sizeof(avenrun) / sizeof(avenrun[0]));
(*curcmd->c_fetch)(); (*curmode->c_fetch)();
if (curcmd->c_flags & CF_LOADAV) { if (curmode->c_flags & CF_LOADAV) {
j = 5.0*avenrun[0] + 0.5; j = 5.0*avenrun[0] + 0.5;
dellave -= avenrun[0]; dellave -= avenrun[0];
if (dellave >= 0.0) if (dellave >= 0.0)
@ -261,8 +261,8 @@ display(signo)
if (j > 50) if (j > 50)
wprintw(wload, " %4.1f", avenrun[0]); wprintw(wload, " %4.1f", avenrun[0]);
} }
(*curcmd->c_refresh)(); (*curmode->c_refresh)();
if (curcmd->c_flags & CF_LOADAV) if (curmode->c_flags & CF_LOADAV)
wrefresh(wload); wrefresh(wload);
wrefresh(wnd); wrefresh(wnd);
move(CMDLINE, col); move(CMDLINE, col);
@ -284,16 +284,6 @@ redraw(signo)
sigprocmask(SIG_UNBLOCK, &set, NULL); sigprocmask(SIG_UNBLOCK, &set, NULL);
} }
void
load()
{
(void)getloadavg(avenrun, sizeof(avenrun)/sizeof(avenrun[0]));
mvprintw(CMDLINE, 0, "%4.1f %4.1f %4.1f",
avenrun[0], avenrun[1], avenrun[2]);
clrtoeol();
}
void void
die(signo) die(signo)
int signo; int signo;

View File

@ -1,4 +1,4 @@
/* $NetBSD: systat.h,v 1.3 1997/07/21 07:05:08 mrg Exp $ */ /* $NetBSD: systat.h,v 1.4 1999/12/16 04:02:23 jwise Exp $ */
/*- /*-
* Copyright (c) 1980, 1989, 1992, 1993 * Copyright (c) 1980, 1989, 1992, 1993
@ -37,7 +37,7 @@
#include <curses.h> #include <curses.h>
struct cmdtab { struct mode {
char *c_name; /* command name */ char *c_name; /* command name */
void (*c_refresh) __P((void)); /* display refresh */ void (*c_refresh) __P((void)); /* display refresh */
void (*c_fetch) __P((void)); /* sets up data structures */ void (*c_fetch) __P((void)); /* sets up data structures */
@ -49,6 +49,12 @@ struct cmdtab {
char c_flags; /* see below */ char c_flags; /* see below */
}; };
struct command {
char *c_name;
void (*c_cmd) __P((void));
char *helptext;
};
#define CF_INIT 0x1 /* been initialized */ #define CF_INIT 0x1 /* been initialized */
#define CF_LOADAV 0x2 /* display w/ load average */ #define CF_LOADAV 0x2 /* display w/ load average */