Add `t' to swap the WAIT column with RTABLE (and vice versa);  WAIT
is wide enough to fit RTABLE, somewhat adds additional value to STATE
and seems therefore most appropiate to hide in favour of RTABLE.

Internally, I renamed the existing CMD_rtable command to filter routing
tables into CMD_rtableid in order to use CMD_rtable for showing them as
that seems in line with how CMD_threads is named to show threads, etc.

format_header() semantics are slightly reworked/improved now that there
are two changing fields;  instead of conditionally changing, it now
always updates it accordingly - i think that makes it clearer overall.

format_next_process() now uses strlcpy() instead of snprintf() for plain
strings as I had to touch those lines anyway.

Filtering rtables with `T' does not toggle the column, just like
filtering users with `u' does not toggle between user and thread id.

Feedback? OK?

Index: display.c
===================================================================
RCS file: /cvs/src/usr.bin/top/display.c,v
retrieving revision 1.64
diff -u -p -r1.64 display.c
--- display.c   23 Aug 2020 21:11:55 -0000      1.64
+++ display.c   23 Aug 2020 22:39:47 -0000
@@ -824,6 +824,7 @@ show_help(void)
            "r count pid  - renice process `pid' to nice value `count'\n"
            "S            - toggle the display of system processes\n"
            "s time       - change delay between displays to `time' seconds\n"
+           "t            - toggle the display of routing tables\n"
            "T [-]rtable  - show processes associated with routing table 
`rtable'\n"
            "               (T+ shows all, T -rtable hides rtable)\n"
            "u [-]user    - show processes for `user' (u+ shows all, u -user 
hides user)\n"
Index: machine.c
===================================================================
RCS file: /cvs/src/usr.bin/top/machine.c,v
retrieving revision 1.108
diff -u -p -r1.108 machine.c
--- machine.c   23 Aug 2020 21:11:55 -0000      1.108
+++ machine.c   23 Aug 2020 22:38:15 -0000
@@ -75,8 +75,9 @@ struct handle {
 static char header[] =
        "  PID X        PRI NICE  SIZE   RES STATE     WAIT      TIME    CPU 
COMMAND";
 
-/* 0123456   -- field to fill in starts at header+6 */
+/* offsets in the header line to start alternative columns */
 #define UNAME_START 6
+#define RTABLE_START 46
 
 #define Proc_format \
        "%5d %-8.8s %3d %4d %5s %5s %-9s %-7.7s %6s %5.2f%% %s"
@@ -226,16 +227,20 @@ machine_init(struct statics *statics)
 }
 
 char *
-format_header(char *second_field)
+format_header(char *second_field, char *eighth_field)
 {
-       char *field_name, *thread_field = "     TID";
-       char *ptr;
+       char *second_fieldp = second_field, *eighth_fieldp = eighth_field, *ptr;
 
-       field_name = second_field ? second_field : thread_field;
-
-       ptr = header + UNAME_START;
-       while (*field_name != '\0')
-               *ptr++ = *field_name++;
+       if (second_field != NULL) {
+               ptr = header + UNAME_START;
+               while (*second_fieldp != '\0')
+                       *ptr++ = *second_fieldp++;
+       }
+       if (eighth_field != NULL) {
+               ptr = header + RTABLE_START;
+               while (*eighth_fieldp != '\0')
+                       *ptr++ = *eighth_fieldp++;
+       }
        return (header);
 }
 
@@ -414,7 +419,7 @@ get_process_info(struct system_info *si,
     int (*compare) (const void *, const void *))
 {
        int show_idle, show_system, show_threads, show_uid, show_pid, show_cmd;
-       int show_rtable, hide_rtable, hide_uid;
+       int show_rtableid, hide_rtableid, hide_uid;
        int total_procs, active_procs;
        struct kinfo_proc **prefp, *pp;
        int what = KERN_PROC_ALL;
@@ -446,8 +451,8 @@ get_process_info(struct system_info *si,
        show_uid = sel->uid != (uid_t)-1;
        hide_uid = sel->huid != (uid_t)-1;
        show_pid = sel->pid != (pid_t)-1;
-       show_rtable = sel->rtableid != -1;
-       hide_rtable = sel->hrtableid != -1;
+       show_rtableid = sel->rtableid != -1;
+       hide_rtableid = sel->hrtableid != -1;
        show_cmd = sel->command != NULL;
 
        /* count up process states and get pointers to interesting procs */
@@ -476,8 +481,8 @@ get_process_info(struct system_info *si,
                            (!hide_uid || pp->p_ruid != sel->huid) &&
                            (!show_uid || pp->p_ruid == sel->uid) &&
                            (!show_pid || pp->p_pid == sel->pid) &&
-                           (!hide_rtable || pp->p_rtableid != sel->hrtableid) 
&&
-                           (!show_rtable || pp->p_rtableid == sel->rtableid) &&
+                           (!hide_rtableid || pp->p_rtableid != 
sel->hrtableid) &&
+                           (!show_rtableid || pp->p_rtableid == sel->rtableid) 
&&
                            (!show_cmd || cmd_matches(pp, sel->command))) {
                                *prefp++ = pp;
                                active_procs++;
@@ -544,13 +549,12 @@ skip_processes(struct handle *hndl, int 
 
 char *
 format_next_process(struct handle *hndl, const char *(*get_userid)(uid_t, int),
-    pid_t *pid)
+    int rtable, pid_t *pid)
 {
-       char *p_wait;
        struct kinfo_proc *pp;
        int cputime;
        double pct;
-       char buf[16];
+       char second_buf[16], eighth_buf[8];
 
        /* find and remember the next proc structure */
        pp = *(hndl->next_proc++);
@@ -560,24 +564,27 @@ format_next_process(struct handle *hndl,
        /* calculate the base for cpu percentages */
        pct = (double)pp->p_pctcpu / fscale;
 
-       if (pp->p_wmesg[0])
-               p_wait = pp->p_wmesg;
+       if (get_userid == NULL)
+               snprintf(second_buf, sizeof(second_buf), "%8d", pp->p_tid);
        else
-               p_wait = "-";
+               strlcpy(second_buf, (*get_userid)(pp->p_ruid, 0),
+                   sizeof(second_buf));
 
-       if (get_userid == NULL)
-               snprintf(buf, sizeof(buf), "%8d", pp->p_tid);
+       if (rtable)
+               snprintf(eighth_buf, sizeof(eighth_buf), "%7d",
+                   pp->p_rtableid);
        else
-               snprintf(buf, sizeof(buf), "%s", (*get_userid)(pp->p_ruid, 0));
+               strlcpy(eighth_buf, pp->p_wmesg[0] == '\0' ? "-" : pp->p_wmesg,
+                   sizeof(eighth_buf));
 
        /* format this entry */
-       snprintf(fmt, sizeof(fmt), Proc_format, pp->p_pid, buf,
+       snprintf(fmt, sizeof(fmt), Proc_format, pp->p_pid, second_buf,
            pp->p_priority - PZERO, pp->p_nice - NZERO,
            format_k(pagetok(PROCSIZE(pp))),
            format_k(pagetok(pp->p_vm_rssize)),
            (pp->p_stat == SSLEEP && pp->p_slptime > maxslp) ?
            "idle" : state_abbr(pp),
-           p_wait, format_time(cputime), 100.0 * pct,
+           eighth_buf, format_time(cputime), 100.0 * pct,
            printable(format_comm(pp)));
 
        *pid = pp->p_pid;
Index: machine.h
===================================================================
RCS file: /cvs/src/usr.bin/top/machine.h,v
retrieving revision 1.30
diff -u -p -r1.30 machine.h
--- machine.h   23 Aug 2020 21:11:55 -0000      1.30
+++ machine.h   23 Aug 2020 22:35:18 -0000
@@ -77,6 +77,7 @@ struct process_select {
        uid_t           uid;    /* only this uid (unless uid == -1) */
        uid_t           huid;   /* hide this uid (unless huid == -1) */
        pid_t           pid;    /* only this pid (unless pid == -1) */
+       int             rtable; /* show routing tables */
        int             rtableid;       /* only this rtable (unless rtableid == 
-1) */
        int             hrtableid;      /* hide this rtable (unless hrtableid 
== -1) */
        char           *command;/* only this command (unless == NULL) */
@@ -87,14 +88,14 @@ extern int      display_init(struct stat
 
 /* machine.c */
 extern int      machine_init(struct statics *);
-extern char    *format_header(char *);
+extern char    *format_header(char *, char *);
 extern void     get_system_info(struct system_info *);
 extern struct handle
 *get_process_info(struct system_info *, struct process_select *,
                 int (*) (const void *, const void *));
 extern void     skip_processes(struct handle *, int);
 extern char    *format_next_process(struct handle *,
-                const char *(*)(uid_t, int), pid_t *);
+                const char *(*)(uid_t, int), int, pid_t *);
 extern uid_t    proc_owner(pid_t);
 
 extern struct kinfo_proc       *getprocs(int, int, int *);
Index: top.1
===================================================================
RCS file: /cvs/src/usr.bin/top/top.1,v
retrieving revision 1.76
diff -u -p -r1.76 top.1
--- top.1       23 Aug 2020 21:11:55 -0000      1.76
+++ top.1       23 Aug 2020 21:17:28 -0000
@@ -383,6 +383,8 @@ seconds.
 .It T Oo - Oc Ns Ar rtable
 Display only processes associated with the specified routing table
 .Ar rtable .
+.It t
+Toggle the display of routing tables.
 .Sq T+
 shows processes associated with all routing tables.
 The
@@ -463,6 +465,10 @@ number on which the process is bound.
 .It WAIT
 A description of the wait channel the process is sleeping on if it's
 asleep.
+.It RTABLE
+The routing table, used instead of WAIT if
+.Fl t
+is specified.
 .It TIME
 The number of system and user CPU seconds that the process has used.
 .It CPU
Index: top.c
===================================================================
RCS file: /cvs/src/usr.bin/top/top.c,v
retrieving revision 1.105
diff -u -p -r1.105 top.c
--- top.c       23 Aug 2020 21:11:55 -0000      1.105
+++ top.c       23 Aug 2020 22:41:44 -0000
@@ -133,7 +133,8 @@ struct statics  statics;
 #define CMD_pagedown   26
 #define CMD_pageup     27
 #define CMD_grep2      28
-#define CMD_rtable     29
+#define CMD_rtableid   29
+#define CMD_rtable     30
 
 static void
 usage(void)
@@ -141,7 +142,7 @@ usage(void)
        extern char *__progname;
 
        fprintf(stderr,
-           "usage: %s [-1bCHIinqSu] [-d count] [-g string] [-o [-]field] "
+           "usage: %s [-1bCHIinqStu] [-d count] [-g string] [-o [-]field] "
            "[-p pid] [-s time]\n\t[-T [-]rtable] [-U [-]user] [number]\n",
            __progname);
 }
@@ -241,7 +242,7 @@ parseargs(int ac, char **av)
        char *endp;
        int i;
 
-       while ((i = getopt(ac, av, "1SHICbinqus:d:p:U:o:g:T:")) != -1) {
+       while ((i = getopt(ac, av, "1SHICbinqus:d:p:U:o:g:T:t")) != -1) {
                switch (i) {
                case '1':
                        combine_cpus = 1;
@@ -339,6 +340,10 @@ parseargs(int ac, char **av)
                                    "%s: invalid routing table", optarg);
                        break;
 
+               case 't':
+                       ps.rtable = true;
+                       break;
+
                default:
                        usage();
                        exit(1);
@@ -370,7 +375,9 @@ parseargs(int ac, char **av)
 int
 main(int argc, char *argv[])
 {
-       char *uname_field = "USERNAME", *header_text, *env_top;
+       char *header_text, *env_top;
+       char *uname_field = "USERNAME", *thread_field = "     TID";
+       char *wait_field = "WAIT   ", *rtable_field = " RTABLE";
        const char *(*get_userid)(uid_t, int) = user_from_uid;
        char **preset_argv = NULL, **av = argv;
        int preset_argc = 0, ac = argc, active_procs, i, ncpuonline_now;
@@ -391,6 +398,7 @@ main(int argc, char *argv[])
        ps.uid = (uid_t)-1;
        ps.huid = (uid_t)-1;
        ps.pid = (pid_t)-1;
+       ps.rtable = false;
        ps.rtableid = -1;
        ps.hrtableid = -1;
        ps.command = NULL;
@@ -573,7 +581,9 @@ restart:
                i_message();
 
                /* get the string to use for the process area header */
-               header_text = format_header(ps.threads ? NULL : uname_field);
+               header_text = format_header(
+                   ps.threads ? thread_field : uname_field,
+                   ps.rtable ? rtable_field : wait_field);
 
                /* update the header area */
                i_header(header_text);
@@ -613,7 +623,8 @@ restart:
                                char * s;
 
                                s = format_next_process(processes,
-                                   ps.threads ? NULL : get_userid, &pid);
+                                   ps.threads ? NULL : get_userid, ps.rtable,
+                                   &pid);
                                i_process(i, s, pid == hlpid);
                        }
                }
@@ -668,7 +679,7 @@ rundisplay(void)
        char ch, *iptr;
        int change, i;
        struct pollfd pfd[1];
-       static char command_chars[] = "\f qh?en#sdkriIuSopCHg+P109)(/T";
+       static char command_chars[] = "\f qh?en#sdkriIuSopCHg+P109)(/Tt";
 
        /*
         * assume valid command unless told
@@ -1035,7 +1046,7 @@ rundisplay(void)
                        if (skip < 0)
                                skip = 0;
                        break;
-               case CMD_rtable:
+               case CMD_rtableid:
                        new_message(MT_standout,
                            "Routing table: ");
                        if (readline(tempbuf, sizeof(tempbuf)) > 0) {
@@ -1052,6 +1063,12 @@ rundisplay(void)
                                putr();
                        } else
                                clear_message();
+                       break;
+               case CMD_rtable:
+                       ps.rtable = !ps.rtable;
+                       new_message(MT_standout | MT_delayed,
+                           " %sisplaying routing tables.",
+                           ps.rtable ? "D" : "Not d");
                        break;
                default:
                        new_message(MT_standout, " BAD CASE IN SWITCH!");

Reply via email to