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:
Eitan Adler 2018-06-12 06:53:31 +00:00
parent 1bb577b4c2
commit c8aa5e526f
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 '?'.
*/
static const struct command all_commands[] =
const struct command all_commands[] =
{
{'C', "toggle the displaying of weighted CPU percentage", false },
{'d', "change number of displays to show", false},
{'e', "list errors generated by last \"kill\" or \"renice\" command", false},
{'H', "toggle the displaying of threads", false},
{'h', "show this help text", true},
{'?', "show this help text", true},
{'i', "toggle the displaying of idle processes", false},
{'I', "toggle the displaying of idle processes", false},
{'j', "toggle the displaying of jail ID", false},
{'J', "display processes for only one jail (+ selects all jails)", false},
{'k', "kill processes; send a signal to a list of processes", false},
{'q', "quit" , true},
{'m', "toggle the display between 'cpu' and 'io' modes", false},
{'n', "change number of processes to display", false},
{'#', "change number of processes to display", false},
{'o', "specify the sort order", false},
{'p', "display one process (+ selects all processes)", false},
{'P', "toggle the displaying of per-CPU statistics", false},
{'r', "renice a process", false},
{'s', "change number of seconds to delay between updates", false},
{'S', "toggle the displaying of system processes", false},
{'a', "toggle the displaying of process titles", false},
{'T', "toggle the displaying of thread IDs", false},
{'t', "toggle the display of this process", false},
{'u', "display processes for only one user (+ selects all users)", false},
{'w', "toggle the display of swap use for each process", false},
{'z', "toggle the displaying of the system idle process", false },
{0, NULL, true}
{'C', "toggle the displaying of weighted CPU percentage", false, CMD_wcputog},
{'d', "change number of displays to show", false, CMD_displays},
{'e', "list errors generated by last \"kill\" or \"renice\" command", false, CMD_errors},
{'H', "toggle the displaying of threads", false, CMD_thrtog},
{'h', "show this help text", true, CMD_help},
{'?', "show this help text", true, CMD_help},
{'i', "toggle the displaying of idle processes", false, CMD_idletog},
{'I', "toggle the displaying of idle processes", false, CMD_idletog},
{'j', "toggle the displaying of jail ID", false, CMD_jidtog},
{'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, CMD_kill},
{'q', "quit" , true, CMD_quit},
{'m', "toggle the display between 'cpu' and 'io' modes", false, CMD_viewtog},
{'n', "change number of processes to display", false, CMD_number},
{'#', "change number of processes to display", false, CMD_number},
{'o', "specify the sort order", false, CMD_order},
{'p', "display one process (+ selects all processes)", false, CMD_pid},
{'P', "toggle the displaying of per-CPU statistics", false, CMD_pcputog},
{'r', "renice a process", false, CMD_renice},
{'s', "change number of seconds to delay between updates", false, CMD_delay},
{'S', "toggle the displaying of system processes", false, CMD_viewsys},
{'a', "toggle the displaying of process titles", false, CMD_showargs},
{'T', "toggle the displaying of thread IDs", false, CMD_toggletid},
{'t', "toggle the display of this process", false, CMD_selftog},
{'u', "display processes for only one user (+ selects all users)", false, CMD_user},
{'w', "toggle the display of swap use for each process", false, CMD_swaptog},
{'z', "toggle the displaying of the system idle process", false, CMD_kidletog},
{' ', "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
show_help(void)

View File

@ -18,10 +18,43 @@ void show_errors(void);
int error_count(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 {
char c;
const char * const desc;
bool available_to_dumb;
enum cmd_id id;
};
extern const struct command all_commands[];
#endif /* COMMANDS_H */

View File

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

View File

@ -20,6 +20,7 @@
#include <sys/select.h>
#include <sys/signal.h>
#include <assert.h>
#include <errno.h>
#include <getopt.h>
#include <jail.h>
@ -225,7 +226,6 @@ main(int argc, char *argv[])
{
int i;
int active_procs;
int change;
struct system_info system_info;
struct statics statics;
@ -253,48 +253,12 @@ main(int argc, char *argv[])
char warnings = 0;
char topn_specified = false;
char ch;
char *iptr;
char no_command = 1;
struct timeval timeout;
char *order_name = NULL;
int order_index = 0;
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 */
#ifdef DEBUG
extern FILE *debug;
@ -843,6 +807,7 @@ _Static_assert(sizeof(command_chars) == CMD_toggletid + 2, "command chars size")
{
int newval;
const char *errmsg;
const struct command *cptr;
/* something to read -- clear the message area first */
clear_message();
@ -856,28 +821,30 @@ _Static_assert(sizeof(command_chars) == CMD_toggletid + 2, "command chars size")
putchar('\r');
quit(1);
}
if ((iptr = strchr(command_chars, ch)) == NULL)
{
if (ch != '\r' && ch != '\n')
{
/* illegal command */
new_message(MT_standout, " Command not understood");
if (ch == '\r' || ch == '\n') {
continue;
}
putchar('\r');
no_command = true;
}
else
{
change = iptr - command_chars;
if (overstrike && change > CMD_OSLIMIT)
cptr = all_commands;
while (cptr->c != '\0') {
if (cptr->c == ch) {
break;
}
cptr++;
}
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,
" Command cannot be handled by this terminal");
putchar('\r');
no_command = true;
no_command = true;
}
else switch(change)
if (!no_command) {
switch(cptr->id)
{
case CMD_redraw: /* redraw screen */
reset_display();
@ -893,12 +860,11 @@ _Static_assert(sizeof(command_chars) == CMD_toggletid + 2, "command chars size")
}
break;
case CMD_quit: /* quit */
case CMD_quit:
quit(0);
break;
case CMD_help1: /* help */
case CMD_help2:
case CMD_help:
reset_display();
top_clear();
show_help();
@ -926,8 +892,7 @@ _Static_assert(sizeof(command_chars) == CMD_toggletid + 2, "command chars size")
}
break;
case CMD_number1: /* new number */
case CMD_number2:
case CMD_number:
new_message(MT_standout,
"Number of processes to show: ");
newval = readline(tempbuf1, 8, true);
@ -1019,7 +984,6 @@ _Static_assert(sizeof(command_chars) == CMD_toggletid + 2, "command chars size")
break;
case CMD_idletog:
case CMD_idletog2:
ps.idle = !ps.idle;
new_message(MT_standout | MT_delayed,
" %sisplaying idle processes.",
@ -1204,9 +1168,9 @@ _Static_assert(sizeof(command_chars) == CMD_toggletid + 2, "command chars size")
} else
clear_message();
break;
default:
new_message(MT_standout, " BAD CASE IN SWITCH!");
putchar('\r');
case CMD_NONE:
assert("reached switch without command");
}
}
}
@ -1214,7 +1178,6 @@ _Static_assert(sizeof(command_chars) == CMD_toggletid + 2, "command chars size")
fflush(stdout);
}
}
}
}
#ifdef DEBUG