From owner-svn-src-all@freebsd.org Tue Jun 12 06:53:33 2018 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 42C791002D2D; Tue, 12 Jun 2018 06:53:33 +0000 (UTC) (envelope-from eadler@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id ECCDE70D86; Tue, 12 Jun 2018 06:53:32 +0000 (UTC) (envelope-from eadler@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id CF41126F56; Tue, 12 Jun 2018 06:53:32 +0000 (UTC) (envelope-from eadler@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w5C6rWUs095280; Tue, 12 Jun 2018 06:53:32 GMT (envelope-from eadler@FreeBSD.org) Received: (from eadler@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w5C6rWkw095276; Tue, 12 Jun 2018 06:53:32 GMT (envelope-from eadler@FreeBSD.org) Message-Id: <201806120653.w5C6rWkw095276@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: eadler set sender to eadler@FreeBSD.org using -f From: Eitan Adler Date: Tue, 12 Jun 2018 06:53:32 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r334988 - head/usr.bin/top X-SVN-Group: head X-SVN-Commit-Author: eadler X-SVN-Commit-Paths: head/usr.bin/top X-SVN-Commit-Revision: 334988 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.26 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 12 Jun 2018 06:53:33 -0000 Author: eadler Date: Tue Jun 12 06:53:31 2018 New Revision: 334988 URL: https://svnweb.freebsd.org/changeset/base/334988 Log: 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. Modified: head/usr.bin/top/commands.c head/usr.bin/top/commands.h head/usr.bin/top/screen.c head/usr.bin/top/top.c Modified: head/usr.bin/top/commands.c ============================================================================== --- head/usr.bin/top/commands.c Tue Jun 12 06:46:03 2018 (r334987) +++ head/usr.bin/top/commands.c Tue Jun 12 06:53:31 2018 (r334988) @@ -50,38 +50,38 @@ static int str_addarg(char *str, int len, char *arg, b * 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) Modified: head/usr.bin/top/commands.h ============================================================================== --- head/usr.bin/top/commands.h Tue Jun 12 06:46:03 2018 (r334987) +++ head/usr.bin/top/commands.h Tue Jun 12 06:53:31 2018 (r334988) @@ -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 */ Modified: head/usr.bin/top/screen.c ============================================================================== --- head/usr.bin/top/screen.c Tue Jun 12 06:46:03 2018 (r334987) +++ head/usr.bin/top/screen.c Tue Jun 12 06:53:31 2018 (r334988) @@ -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]; Modified: head/usr.bin/top/top.c ============================================================================== --- head/usr.bin/top/top.c Tue Jun 12 06:46:03 2018 (r334987) +++ head/usr.bin/top/top.c Tue Jun 12 06:53:31 2018 (r334988) @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -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 @@ restart: { int newval; const char *errmsg; + const struct command *cptr; /* something to read -- clear the message area first */ clear_message(); @@ -856,28 +821,30 @@ restart: putchar('\r'); quit(1); } - if ((iptr = strchr(command_chars, ch)) == NULL) - { - if (ch != '\r' && ch != '\n') - { - /* illegal command */ + if (ch == '\r' || ch == '\n') { + continue; + } + 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; } - putchar('\r'); - no_command = true; - } - else - { - change = iptr - command_chars; - if (overstrike && change > CMD_OSLIMIT) + 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 @@ restart: } 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 @@ restart: } 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 @@ restart: break; case CMD_idletog: - case CMD_idletog2: ps.idle = !ps.idle; new_message(MT_standout | MT_delayed, " %sisplaying idle processes.", @@ -1204,17 +1168,16 @@ restart: } else clear_message(); break; - default: - new_message(MT_standout, " BAD CASE IN SWITCH!"); - putchar('\r'); + case CMD_NONE: + assert("reached switch without command"); } + } } /* flush out stuff that may have been written */ fflush(stdout); } } - } } #ifdef DEBUG