top: avoid library shame with refactored 'Ctrl' window

Well darn it, whoever wrote that new library caught me
with my pants down (again?). Shoot, they were not just
down but somehow missing altogether. Here's the story.

Any item from that library supported by dynamic memory
can only be represented in user's stacks exactly once.

Should any string based enumerator be duplicated among
the items array, for any instance beyond the first the
library will return '[ duplicate ENUM ]' for a result.

That's where I lost my pants. While command lines were
given special handling (and never duplicated) I failed
to turn on CGROUPS, SUPGRPS & ENVIRON when testing the
Ctrl-G, Ctrl-U & Ctrl-N keys. If any of those 3 are on
that's when a Ctrl window sees a 'duplicate' notation.

[ and who runs top with such fields displayed anyway ]

In responding to this oops, the internals were changed
quite dramatically & vastly simplified in the process.

More importantly, the 'duplicate' results are no more.

Signed-off-by: Jim Warner <james.warner@comcast.net>
This commit is contained in:
Jim Warner 2022-05-04 00:00:00 -05:00 committed by Craig Small
parent b47693c078
commit bc4b499ed2
2 changed files with 39 additions and 92 deletions

127
top/top.c
View File

@ -106,15 +106,14 @@ static int Screen_cols, Screen_rows, Max_lines;
#define SCREEN_ROWS ( Screen_rows - Tagged_rsvd ) #define SCREEN_ROWS ( Screen_rows - Tagged_rsvd )
// 1 for horizontal separator // 1 for horizontal separator
#define TAGGED_RSVD ( 1 ) #define TAGGED_RSVD ( 1 )
#define TAGGED_KEEP Tagged_func = NULL #define TAGGED_KEEP Tagged_show = 0
#define TAGGED_TOSS do { Tagged_func = NULL; \ #define TAGGED_TOSS do { Tagged_show = 0; \
Tagged_task = Tagged_rsvd = Tagged_enum = 0; \ Tagged_task = Tagged_rsvd = Tagged_enum = 0; } while(0)
Fieldstab[eu_GENERIC].item = PIDS_extra; } while(0) static int Tagged_show,
static int Tagged_task,
Tagged_rsvd, Tagged_rsvd,
Tagged_enum; Tagged_task;
enum pflag Tagged_enum;
static char *Tagged_name; static char *Tagged_name;
static void(*Tagged_func)(void);
/* This is really the number of lines needed to display the summary /* This is really the number of lines needed to display the summary
information (0 - nn), but is used as the relative row where we information (0 - nn), but is used as the relative row where we
@ -1902,14 +1901,12 @@ static struct {
#define eu_TREE_HID eu_LAST +4 #define eu_TREE_HID eu_LAST +4
#define eu_TREE_LVL eu_LAST +5 #define eu_TREE_LVL eu_LAST +5
#define eu_TREE_ADD eu_LAST +6 #define eu_TREE_ADD eu_LAST +6
#define eu_GENERIC eu_LAST +7
, { -1, -1, -1, PIDS_CMDLINE } // str ( if Show_CMDLIN, eu_CMDLINE ) , { -1, -1, -1, PIDS_CMDLINE } // str ( if Show_CMDLIN, eu_CMDLINE )
, { -1, -1, -1, PIDS_TICS_ALL_C } // ull_int ( if Show_CTIMES, eu_TICS_ALL_C ) , { -1, -1, -1, PIDS_TICS_ALL_C } // ull_int ( if Show_CTIMES, eu_TICS_ALL_C )
, { -1, -1, -1, PIDS_ID_FUID } // u_int ( if a usrseltyp, eu_ID_FUID ) , { -1, -1, -1, PIDS_ID_FUID } // u_int ( if a usrseltyp, eu_ID_FUID )
, { -1, -1, -1, PIDS_extra } // s_ch ( if Show_FOREST, eu_TREE_HID ) , { -1, -1, -1, PIDS_extra } // s_ch ( if Show_FOREST, eu_TREE_HID )
, { -1, -1, -1, PIDS_extra } // s_int ( if Show_FOREST, eu_TREE_LVL ) , { -1, -1, -1, PIDS_extra } // s_int ( if Show_FOREST, eu_TREE_LVL )
, { -1, -1, -1, PIDS_extra } // s_int ( if Show_FOREST, eu_TREE_ADD ) , { -1, -1, -1, PIDS_extra } // s_int ( if Show_FOREST, eu_TREE_ADD )
, { -1, -1, -1, PIDS_extra } // str { special 'tag', eu_GENERIC }
#undef A_left #undef A_left
#undef A_right #undef A_right
}; };
@ -4630,41 +4627,10 @@ static void wins_stage_2 (void) {
/* /*
* This guy manages the bottom margin window | * This guy manages the bottom margin window,
* & the tagged process command line display | */ * showing miscellaneous variable width data. */
static void wins_tag_cmdline (void) { static void wins_tag_show (void) {
char buf[SMLBUFSIZ]; #define maxRSVD ( Screen_rows - 1 )
const char *p;
int i;
for (i = 0; i < PIDSmaxt; i++) {
if (Tagged_task == PID_VAL(EU_PID, s_int, Curwin->ppt[i]))
break;
}
if (i < PIDSmaxt) {
snprintf(buf, sizeof(buf), " command line for pid %d, %s"
, Tagged_task, PID_VAL(EU_CMD, str, Curwin->ppt[i]));
p = PID_VAL(eu_CMDLINE, str, Curwin->ppt[i]);
if (!p || !*p) p = "n/a";
Tagged_rsvd = 1 + TAGGED_RSVD + (strlen(p) / Screen_cols);
putp(fmtmk("%s%s%-*s", tg2(0, SCREEN_ROWS), Curwin->capclr_hdr, Screen_cols, buf));
putp(fmtmk("%s%s", tg2(0, SCREEN_ROWS + 1), Cap_clr_eos));
putp(fmtmk("%s%s", tg2(0, SCREEN_ROWS + 1), Cap_norm));
fputs(p, stdout);
#ifdef TAGGED_BRIEF
} else
TAGGED_TOSS;
#else
}
TAGGED_KEEP;
#endif
} // end: wins_tag_cmdline
/*
* This guy manages the bottom margin window |
* showing miscellaneous variable width data | */
static void wins_tag_generic (void) {
char buf[SMLBUFSIZ]; char buf[SMLBUFSIZ];
const char *p; const char *p;
int i; int i;
@ -4676,9 +4642,10 @@ static void wins_tag_generic (void) {
if (i < PIDSmaxt) { if (i < PIDSmaxt) {
snprintf(buf, sizeof(buf), " %s for pid %d, %s" snprintf(buf, sizeof(buf), " %s for pid %d, %s"
, Tagged_name, Tagged_task, PID_VAL(EU_CMD, str, Curwin->ppt[i])); , Tagged_name, Tagged_task, PID_VAL(EU_CMD, str, Curwin->ppt[i]));
p = PID_VAL(eu_GENERIC, str, Curwin->ppt[i]); p = PID_VAL(Tagged_enum, str, Curwin->ppt[i]);
if (!p || !*p || !strcmp(p, "-")) p = "n/a"; if (!p || !*p || !strcmp(p, "-")) p = "n/a";
Tagged_rsvd = 1 + TAGGED_RSVD + (strlen(p) / Screen_cols); Tagged_rsvd = 1 + TAGGED_RSVD + (strlen(p) / Screen_cols);
if (Tagged_rsvd > maxRSVD) Tagged_rsvd = maxRSVD;
putp(fmtmk("%s%s%-*s", tg2(0, SCREEN_ROWS), Curwin->capclr_hdr, Screen_cols, buf)); putp(fmtmk("%s%s%-*s", tg2(0, SCREEN_ROWS), Curwin->capclr_hdr, Screen_cols, buf));
putp(fmtmk("%s%s", tg2(0, SCREEN_ROWS + 1), Cap_clr_eos)); putp(fmtmk("%s%s", tg2(0, SCREEN_ROWS + 1), Cap_clr_eos));
putp(fmtmk("%s%s", tg2(0, SCREEN_ROWS + 1), Cap_norm)); putp(fmtmk("%s%s", tg2(0, SCREEN_ROWS + 1), Cap_norm));
@ -4690,7 +4657,26 @@ static void wins_tag_generic (void) {
} }
TAGGED_KEEP; TAGGED_KEEP;
#endif #endif
} // end: wins_tag_generic #undef maxRSVD
} // end: wins_tag_show
/*
* This guy toggles between displaying a Ctrl
* bottom window or arranging to turn it off. */
static void wins_tag_toggle (enum pflag enu, const char *str) {
int pid = PID_VAL(EU_PID, s_int, Curwin->ppt[Curwin->begtask]);
// if already targeted, assume user wants to turn it off ...
if (Tagged_enum == enu) {
TAGGED_TOSS;
} else {
Tagged_task = pid;
Tagged_enum = enu;
Tagged_name = (char*)str;
Tagged_show = 1;
}
} // end: wins_tag_toggle
/* /*
@ -5317,42 +5303,13 @@ static void keys_global (int ch) {
#endif #endif
break; break;
case kbd_CtrlG: case kbd_CtrlG:
def = PID_VAL(EU_PID, s_int, w->ppt[w->begtask]); wins_tag_toggle(EU_CGR, "control groups");
// if already targeted, assume user wants to turn it off ...
if (Tagged_task && Fieldstab[eu_GENERIC].item == PIDS_CGROUP) {
TAGGED_TOSS;
} else {
Tagged_task = def;
Tagged_enum = eu_GENERIC;
Tagged_name = "control groups";
Tagged_func = wins_tag_generic;
Fieldstab[eu_GENERIC].item = PIDS_CGROUP;
}
break; break;
case kbd_CtrlK: case kbd_CtrlK:
def = PID_VAL(EU_PID, s_int, w->ppt[w->begtask]); wins_tag_toggle(eu_CMDLINE, "command line");
// if already targeted, assume user wants to turn it off ...
if (Tagged_task && Tagged_enum == eu_CMDLINE) {
TAGGED_TOSS;
} else {
Tagged_task = def;
Tagged_enum = eu_CMDLINE;
Tagged_func = wins_tag_cmdline;
Fieldstab[eu_GENERIC].item = PIDS_extra;
}
break; break;
case kbd_CtrlN: case kbd_CtrlN:
def = PID_VAL(EU_PID, s_int, w->ppt[w->begtask]); wins_tag_toggle(EU_ENV, "environment");
// if already targeted, assume user wants to turn it off ...
if (Tagged_task && Fieldstab[eu_GENERIC].item == PIDS_ENVIRON) {
TAGGED_TOSS;
} else {
Tagged_task = def;
Tagged_enum = eu_GENERIC;
Tagged_name = "environment";
Tagged_func = wins_tag_generic;
Fieldstab[eu_GENERIC].item = PIDS_ENVIRON;
}
break; break;
case kbd_CtrlR: case kbd_CtrlR:
if (Secure_mode) if (Secure_mode)
@ -5381,17 +5338,7 @@ static void keys_global (int ch) {
} }
break; break;
case kbd_CtrlU: case kbd_CtrlU:
def = PID_VAL(EU_PID, s_int, w->ppt[w->begtask]); wins_tag_toggle(EU_SGN, "supplementary groups");
// if already targeted, assume user wants to turn it off ...
if (Tagged_task && Fieldstab[eu_GENERIC].item == PIDS_SUPGROUPS) {
TAGGED_TOSS;
} else {
Tagged_task = def;
Tagged_enum = eu_GENERIC;
Tagged_name = "supplementary groups";
Tagged_func = wins_tag_generic;
Fieldstab[eu_GENERIC].item = PIDS_SUPGROUPS;
}
break; break;
case kbd_ENTER: // these two have the effect of waking us case kbd_ENTER: // these two have the effect of waking us
case kbd_SPACE: // from 'pselect', refreshing the display case kbd_SPACE: // from 'pselect', refreshing the display
@ -6895,7 +6842,7 @@ static void frame_make (void) {
} }
if (CHKw(w, View_SCROLL) && VIZISw(Curwin)) show_scroll(); if (CHKw(w, View_SCROLL) && VIZISw(Curwin)) show_scroll();
if (Tagged_func) Tagged_func(); if (Tagged_show) wins_tag_show();
fflush(stdout); fflush(stdout);
/* we'll deem any terminal not supporting tgoto as dumb and disable /* we'll deem any terminal not supporting tgoto as dumb and disable

View File

@ -726,8 +726,8 @@ typedef struct WIN_t {
//atic void wins_reflag (int what, int flg); //atic void wins_reflag (int what, int flg);
//atic void wins_stage_1 (void); //atic void wins_stage_1 (void);
//atic void wins_stage_2 (void); //atic void wins_stage_2 (void);
//atic void wins_tag_cmdline (void); //atic void wins_tag_show (void);
//atic void wins_tag_generic (void); //atic void wins_tag_toggle (enum pflag enu, const char *str);
//atic inline int wins_usrselect (const WIN_t *q, int idx); //atic inline int wins_usrselect (const WIN_t *q, int idx);
/*------ Forest View support -------------------------------------------*/ /*------ Forest View support -------------------------------------------*/
//atic void forest_adds (const int self, int level); //atic void forest_adds (const int self, int level);