top: refactored some of that 'other filtering' support

If we are to support preserving 'other filter' entries
in the rcfile, then the current logic setting up those
osel entries for a WIN_t must be shareable for startup
and when interacting with a user. So, this commit just
repositions this current code in a shareable function.

[ along the way, we give the prior guy a proper name ]

Signed-off-by: Jim Warner <james.warner@comcast.net>
This commit is contained in:
Jim Warner 2018-06-20 00:00:00 -05:00 committed by Craig Small
parent 016f10cb93
commit 5e8bf3c028
2 changed files with 81 additions and 65 deletions

143
top/top.c
View File

@ -3007,6 +3007,71 @@ struct osel_s {
int enu; // field (procflag) to filter int enu; // field (procflag) to filter
}; };
/*
* A function to parse, validate and build a single 'other filter' */
static const char *osel_add (int ch, char *glob) {
int (*rel)(const char *, const char *);
char *(*sel)(const char *, const char *);
char raw[MEDBUFSIZ], ops, *pval;
struct osel_s *osel;
int inc, enu;
if (ch == 'o') {
rel = strcasecmp;
sel = strcasestr;
} else {
rel = strcmp;
sel = strstr;
}
if (!snprintf(raw, sizeof(raw), "%s", glob))
return NULL;
for (osel = Curwin->osel_1st; osel; ) {
if (!strcmp(osel->raw, raw)) // #1: is criteria duplicate?
return N_txt(OSEL_errdups_txt);
osel = osel->nxt;
}
if (*glob != '!') inc = 1; // #2: is it include/exclude?
else { ++glob; inc = 0; }
if (!(pval = strpbrk(glob, "<=>"))) // #3: do we see a delimiter?
return fmtmk(N_fmt(OSEL_errdelm_fmt)
, inc ? N_txt(WORD_include_txt) : N_txt(WORD_exclude_txt));
ops = *(pval);
*(pval++) = '\0';
for (enu = 0; enu < EU_MAXPFLGS; enu++) // #4: is this a valid field?
if (!STRCMP(N_col(enu), glob)) break;
if (enu == EU_MAXPFLGS)
return fmtmk(N_fmt(XTRA_badflds_fmt), glob);
if (!(*pval)) // #5: did we get some value?
return fmtmk(N_fmt(OSEL_errvalu_fmt)
, inc ? N_txt(WORD_include_txt) : N_txt(WORD_exclude_txt));
if (Curwin->osel_prt && strlen(Curwin->osel_prt) >= INT_MAX - (sizeof(raw) + 6))
return NULL;
osel = alloc_c(sizeof(struct osel_s));
osel->inc = inc;
osel->enu = enu;
osel->ops = ops;
if (ops == '=') osel->val = alloc_s(pval);
else osel->val = alloc_s(justify_pad(pval, Fieldstab[enu].width, Fieldstab[enu].align));
osel->rel = rel;
osel->sel = sel;
osel->raw = alloc_s(raw);
osel->nxt = Curwin->osel_1st;
Curwin->osel_1st = osel;
Curwin->osel_tot += 1;
if (!Curwin->osel_prt) Curwin->osel_prt = alloc_c(strlen(raw) + 3);
else Curwin->osel_prt = alloc_r(Curwin->osel_prt, strlen(Curwin->osel_prt) + strlen(raw) + 6);
strcat(Curwin->osel_prt, fmtmk("%s'%s'", (Curwin->osel_tot > 1) ? " + " : "", raw));
return NULL;
} // end: osel_add
/* /*
* A function to turn off entire other filtering in the given window */ * A function to turn off entire other filtering in the given window */
@ -3421,6 +3486,7 @@ static const char *configs_file (FILE *fp, const char *name, float *delay) {
Rc.zero_suppress = 0; Rc.zero_suppress = 0;
// lastly, let's process any optional glob(s) ... // lastly, let's process any optional glob(s) ...
fbuf[0] = '\0';
config_insp(fp, fbuf, sizeof(fbuf)); config_insp(fp, fbuf, sizeof(fbuf));
return NULL; return NULL;
@ -4202,76 +4268,25 @@ signify_that:
} // end: help_view } // end: help_view
static void other_selection (int ch) { static void other_filters (int ch) {
int (*rel)(const char *, const char *); const char *txt, *p;
char *(*sel)(const char *, const char *); char *glob;
char raw[MEDBUFSIZ], ops, *glob, *pval;
struct osel_s *osel;
const char *typ;
int inc, enu;
if (ch == 'o') { if (ch == 'o') txt = N_txt(OSEL_casenot_txt);
typ = N_txt(OSEL_casenot_txt); else txt = N_txt(OSEL_caseyes_txt);
rel = strcasecmp;
sel = strcasestr; glob = ioline(fmtmk(N_fmt(OSEL_prompts_fmt), Curwin->osel_tot + 1, txt));
} else { if (*glob == kbd_ESC || !*glob)
typ = N_txt(OSEL_caseyes_txt);
rel = strcmp;
sel = strstr;
}
glob = ioline(fmtmk(N_fmt(OSEL_prompts_fmt), Curwin->osel_tot + 1, typ));
if (*glob == kbd_ESC
|| !snprintf(raw, sizeof(raw), "%s", glob))
return; return;
for (osel = Curwin->osel_1st; osel; ) {
if (!strcmp(osel->raw, glob)) { // #1: is criteria duplicate? if ((p = osel_add(ch, glob))) {
show_msg(N_txt(OSEL_errdups_txt)); show_msg(p);
return;
}
osel = osel->nxt;
}
if (*glob != '!') inc = 1; // #2: is it include/exclude?
else { ++glob; inc = 0; }
if (!(pval = strpbrk(glob, "<=>"))) { // #3: do we see a delimiter?
show_msg(fmtmk(N_fmt(OSEL_errdelm_fmt)
, inc ? N_txt(WORD_include_txt) : N_txt(WORD_exclude_txt)));
return; return;
} }
ops = *(pval);
*(pval++) = '\0';
for (enu = 0; enu < EU_MAXPFLGS; enu++) // #4: is this a valid field?
if (!STRCMP(N_col(enu), glob)) break;
if (enu == EU_MAXPFLGS) {
show_msg(fmtmk(N_fmt(XTRA_badflds_fmt), glob));
return;
}
if (!(*pval)) { // #5: did we get some value?
show_msg(fmtmk(N_fmt(OSEL_errvalu_fmt)
, inc ? N_txt(WORD_include_txt) : N_txt(WORD_exclude_txt)));
return;
}
if (Curwin->osel_prt && strlen(Curwin->osel_prt) >= INT_MAX - (sizeof(raw) + 6)) {
return;
}
osel = alloc_c(sizeof(struct osel_s));
osel->inc = inc;
osel->enu = enu;
osel->ops = ops;
if (ops == '=') osel->val = alloc_s(pval);
else osel->val = alloc_s(justify_pad(pval, Fieldstab[enu].width, Fieldstab[enu].align));
osel->rel = rel;
osel->sel = sel;
osel->raw = alloc_s(raw);
osel->nxt = Curwin->osel_1st;
Curwin->osel_1st = osel;
Curwin->osel_tot += 1;
if (!Curwin->osel_prt) Curwin->osel_prt = alloc_c(strlen(raw) + 3);
else Curwin->osel_prt = alloc_r(Curwin->osel_prt, strlen(Curwin->osel_prt) + strlen(raw) + 6);
strcat(Curwin->osel_prt, fmtmk("%s'%s'", (Curwin->osel_tot > 1) ? " + " : "", raw));
#ifndef USE_X_COLHDR #ifndef USE_X_COLHDR
SETw(Curwin, NOHISEL_xxx); SETw(Curwin, NOHISEL_xxx);
#endif #endif
} // end: other_selection } // end: other_filters
static void write_rcfile (void) { static void write_rcfile (void) {
@ -4610,7 +4625,7 @@ static void keys_task (int ch) {
break; break;
case 'O': case 'O':
case 'o': case 'o':
if (VIZCHKw(w)) other_selection(ch); if (VIZCHKw(w)) other_filters(ch);
break; break;
case 'U': case 'U':
case 'u': case 'u':

View File

@ -618,6 +618,7 @@ typedef struct WIN_t {
//atic int insp_view_choice (struct pids_stack *obj); //atic int insp_view_choice (struct pids_stack *obj);
//atic void inspection_utility (int pid); //atic void inspection_utility (int pid);
/*------ Other Filtering ------------------------------------------------*/ /*------ Other Filtering ------------------------------------------------*/
//atic const char *osel_add (int ch, char *glob);
//atic void osel_clear (WIN_t *q); //atic void osel_clear (WIN_t *q);
//atic inline int osel_matched (const WIN_t *q, FLG_t enu, const char *str); //atic inline int osel_matched (const WIN_t *q, FLG_t enu, const char *str);
/*------ Startup routines ----------------------------------------------*/ /*------ Startup routines ----------------------------------------------*/
@ -644,7 +645,7 @@ typedef struct WIN_t {
//atic inline int find_ofs (const WIN_t *q, const char *buf); //atic inline int find_ofs (const WIN_t *q, const char *buf);
//atic void find_string (int ch); //atic void find_string (int ch);
//atic void help_view (void); //atic void help_view (void);
//atic void other_selection (int ch); //atic void other_filters (int ch);
//atic void write_rcfile (void); //atic void write_rcfile (void);
/*------ Interactive Input Secondary support (do_key helpers) ----------*/ /*------ Interactive Input Secondary support (do_key helpers) ----------*/
//atic void keys_global (int ch); //atic void keys_global (int ch);