top(1): move command mapping to commands.c

This eliminates the difficult to follow mapping of a string list. It
moves numbers from "#define" into (more) debuggable enums. More
generally, it follows the trend of moving more data into a more central
mechanism.

The help output is a little worse: " " is not rendered well, and there
are duplicate entries, but that will be fixed in a followup.
This commit is contained in:
eadler 2018-06-12 06:53:31 +00:00
parent 4a3b73bc53
commit 5246c095ce
4 changed files with 89 additions and 94 deletions

View File

@ -50,38 +50,38 @@ static int str_addarg(char *str, int len, char *arg, bool first);
* either 'h' or '?'. * either 'h' or '?'.
*/ */
static const struct command all_commands[] = const struct command all_commands[] =
{ {
{'C', "toggle the displaying of weighted CPU percentage", false }, {'C', "toggle the displaying of weighted CPU percentage", false, CMD_wcputog},
{'d', "change number of displays to show", false}, {'d', "change number of displays to show", false, CMD_displays},
{'e', "list errors generated by last \"kill\" or \"renice\" command", false}, {'e', "list errors generated by last \"kill\" or \"renice\" command", false, CMD_errors},
{'H', "toggle the displaying of threads", false}, {'H', "toggle the displaying of threads", false, CMD_thrtog},
{'h', "show this help text", true}, {'h', "show this help text", true, CMD_help},
{'?', "show this help text", true}, {'?', "show this help text", true, CMD_help},
{'i', "toggle the displaying of idle processes", false}, {'i', "toggle the displaying of idle processes", false, CMD_idletog},
{'I', "toggle the displaying of idle processes", false}, {'I', "toggle the displaying of idle processes", false, CMD_idletog},
{'j', "toggle the displaying of jail ID", false}, {'j', "toggle the displaying of jail ID", false, CMD_jidtog},
{'J', "display processes for only one jail (+ selects all jails)", false}, {'J', "display processes for only one jail (+ selects all jails)", false, CMD_jail},
{'k', "kill processes; send a signal to a list of processes", false}, {'k', "kill processes; send a signal to a list of processes", false, CMD_kill},
{'q', "quit" , true}, {'q', "quit" , true, CMD_quit},
{'m', "toggle the display between 'cpu' and 'io' modes", false}, {'m', "toggle the display between 'cpu' and 'io' modes", false, CMD_viewtog},
{'n', "change number of processes to display", false}, {'n', "change number of processes to display", false, CMD_number},
{'#', "change number of processes to display", false}, {'#', "change number of processes to display", false, CMD_number},
{'o', "specify the sort order", false}, {'o', "specify the sort order", false, CMD_order},
{'p', "display one process (+ selects all processes)", false}, {'p', "display one process (+ selects all processes)", false, CMD_pid},
{'P', "toggle the displaying of per-CPU statistics", false}, {'P', "toggle the displaying of per-CPU statistics", false, CMD_pcputog},
{'r', "renice a process", false}, {'r', "renice a process", false, CMD_renice},
{'s', "change number of seconds to delay between updates", false}, {'s', "change number of seconds to delay between updates", false, CMD_delay},
{'S', "toggle the displaying of system processes", false}, {'S', "toggle the displaying of system processes", false, CMD_viewsys},
{'a', "toggle the displaying of process titles", false}, {'a', "toggle the displaying of process titles", false, CMD_showargs},
{'T', "toggle the displaying of thread IDs", false}, {'T', "toggle the displaying of thread IDs", false, CMD_toggletid},
{'t', "toggle the display of this process", false}, {'t', "toggle the display of this process", false, CMD_selftog},
{'u', "display processes for only one user (+ selects all users)", false}, {'u', "display processes for only one user (+ selects all users)", false, CMD_user},
{'w', "toggle the display of swap use for each process", false}, {'w', "toggle the display of swap use for each process", false, CMD_swaptog},
{'z', "toggle the displaying of the system idle process", false }, {'z', "toggle the displaying of the system idle process", false, CMD_kidletog},
{0, NULL, true} {' ', "update the display", false, CMD_update},
{0, NULL, true, CMD_NONE}
}; };
/* XXX: eventually remove command_chars, but assert they are the same for now */
void void
show_help(void) show_help(void)

View File

@ -18,10 +18,43 @@ void show_errors(void);
int error_count(void); int error_count(void);
void show_help(void); void show_help(void);
enum cmd_id {
CMD_NONE,
CMD_redraw,
CMD_update,
CMD_quit,
CMD_help,
CMD_errors,
CMD_number,
CMD_delay,
CMD_displays,
CMD_kill,
CMD_renice,
CMD_idletog,
CMD_user,
CMD_selftog,
CMD_thrtog,
CMD_viewtog,
CMD_viewsys,
CMD_wcputog,
CMD_showargs,
CMD_jidtog,
CMD_kidletog,
CMD_pcputog,
CMD_jail,
CMD_swaptog,
CMD_order,
CMD_pid ,
CMD_toggletid,
};
struct command { struct command {
char c; char c;
const char * const desc; const char * const desc;
bool available_to_dumb; bool available_to_dumb;
enum cmd_id id;
}; };
extern const struct command all_commands[];
#endif /* COMMANDS_H */ #endif /* COMMANDS_H */

View File

@ -37,7 +37,6 @@ int screen_width;
char ch_erase; char ch_erase;
char ch_kill; char ch_kill;
char smart_terminal; char smart_terminal;
char PC;
static char termcap_buf[1024]; static char termcap_buf[1024];
static char string_buffer[1024]; static char string_buffer[1024];
static char home[15]; static char home[15];

View File

@ -20,6 +20,7 @@
#include <sys/select.h> #include <sys/select.h>
#include <sys/signal.h> #include <sys/signal.h>
#include <assert.h>
#include <errno.h> #include <errno.h>
#include <getopt.h> #include <getopt.h>
#include <jail.h> #include <jail.h>
@ -225,7 +226,6 @@ main(int argc, char *argv[])
{ {
int i; int i;
int active_procs; int active_procs;
int change;
struct system_info system_info; struct system_info system_info;
struct statics statics; struct statics statics;
@ -253,48 +253,12 @@ main(int argc, char *argv[])
char warnings = 0; char warnings = 0;
char topn_specified = false; char topn_specified = false;
char ch; char ch;
char *iptr;
char no_command = 1; char no_command = 1;
struct timeval timeout; struct timeval timeout;
char *order_name = NULL; char *order_name = NULL;
int order_index = 0; int order_index = 0;
fd_set readfds; fd_set readfds;
static const char command_chars[] = "\f qh?en#sdkriIutHmSCajzPJwopT";
/* these defines enumerate the "strchr"s of the commands in command_chars */
#define CMD_redraw 0
#define CMD_update 1
#define CMD_quit 2
#define CMD_help1 3
#define CMD_help2 4
#define CMD_OSLIMIT 4 /* terminals with OS can only handle commands */
#define CMD_errors 5 /* less than or equal to CMD_OSLIMIT */
#define CMD_number1 6
#define CMD_number2 7
#define CMD_delay 8
#define CMD_displays 9
#define CMD_kill 10
#define CMD_renice 11
#define CMD_idletog 12
#define CMD_idletog2 13
#define CMD_user 14
#define CMD_selftog 15
#define CMD_thrtog 16
#define CMD_viewtog 17
#define CMD_viewsys 18
#define CMD_wcputog 19
#define CMD_showargs 20
#define CMD_jidtog 21
#define CMD_kidletog 22
#define CMD_pcputog 23
#define CMD_jail 24
#define CMD_swaptog 25
#define CMD_order 26
#define CMD_pid 27
#define CMD_toggletid 28
_Static_assert(sizeof(command_chars) == CMD_toggletid + 2, "command chars size");
/* set the buffer for stdout */ /* set the buffer for stdout */
#ifdef DEBUG #ifdef DEBUG
extern FILE *debug; extern FILE *debug;
@ -843,6 +807,7 @@ _Static_assert(sizeof(command_chars) == CMD_toggletid + 2, "command chars size")
{ {
int newval; int newval;
const char *errmsg; const char *errmsg;
const struct command *cptr;
/* something to read -- clear the message area first */ /* something to read -- clear the message area first */
clear_message(); clear_message();
@ -856,28 +821,30 @@ _Static_assert(sizeof(command_chars) == CMD_toggletid + 2, "command chars size")
putchar('\r'); putchar('\r');
quit(1); quit(1);
} }
if ((iptr = strchr(command_chars, ch)) == NULL) if (ch == '\r' || ch == '\n') {
{ continue;
if (ch != '\r' && ch != '\n')
{
/* illegal command */
new_message(MT_standout, " Command not understood");
} }
putchar('\r'); cptr = all_commands;
no_command = true; while (cptr->c != '\0') {
} if (cptr->c == ch) {
else break;
{ }
change = iptr - command_chars; cptr++;
if (overstrike && change > CMD_OSLIMIT) }
if (cptr->c == '\0') {
new_message(MT_standout, " Command not understood");
putchar('\r');
no_command = true;
}
if (overstrike && !cptr->available_to_dumb)
{ {
/* error */
new_message(MT_standout, new_message(MT_standout,
" Command cannot be handled by this terminal"); " Command cannot be handled by this terminal");
putchar('\r'); putchar('\r');
no_command = true; no_command = true;
} }
else switch(change) if (!no_command) {
switch(cptr->id)
{ {
case CMD_redraw: /* redraw screen */ case CMD_redraw: /* redraw screen */
reset_display(); reset_display();
@ -893,12 +860,11 @@ _Static_assert(sizeof(command_chars) == CMD_toggletid + 2, "command chars size")
} }
break; break;
case CMD_quit: /* quit */ case CMD_quit:
quit(0); quit(0);
break; break;
case CMD_help1: /* help */ case CMD_help:
case CMD_help2:
reset_display(); reset_display();
top_clear(); top_clear();
show_help(); show_help();
@ -926,8 +892,7 @@ _Static_assert(sizeof(command_chars) == CMD_toggletid + 2, "command chars size")
} }
break; break;
case CMD_number1: /* new number */ case CMD_number:
case CMD_number2:
new_message(MT_standout, new_message(MT_standout,
"Number of processes to show: "); "Number of processes to show: ");
newval = readline(tempbuf1, 8, true); newval = readline(tempbuf1, 8, true);
@ -1019,7 +984,6 @@ _Static_assert(sizeof(command_chars) == CMD_toggletid + 2, "command chars size")
break; break;
case CMD_idletog: case CMD_idletog:
case CMD_idletog2:
ps.idle = !ps.idle; ps.idle = !ps.idle;
new_message(MT_standout | MT_delayed, new_message(MT_standout | MT_delayed,
" %sisplaying idle processes.", " %sisplaying idle processes.",
@ -1204,9 +1168,9 @@ _Static_assert(sizeof(command_chars) == CMD_toggletid + 2, "command chars size")
} else } else
clear_message(); clear_message();
break; break;
default: case CMD_NONE:
new_message(MT_standout, " BAD CASE IN SWITCH!"); assert("reached switch without command");
putchar('\r'); }
} }
} }
@ -1214,7 +1178,6 @@ _Static_assert(sizeof(command_chars) == CMD_toggletid + 2, "command chars size")
fflush(stdout); fflush(stdout);
} }
} }
}
} }
#ifdef DEBUG #ifdef DEBUG