From: Jim Warner Date: Sun, 17 Jun 2018 05:00:00 +0000 (-0500) Subject: top: refactored some of that 'other filtering' support X-Git-Tag: v3.3.16~61 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=80057ec06f419468c7b79367b363e4da47221eb3;p=procps-ng 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 --- diff --git a/top/top.c b/top/top.c index 9861b1d9..72e70548 100644 --- a/top/top.c +++ b/top/top.c @@ -3484,6 +3484,71 @@ struct osel_s { 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 */ @@ -3889,6 +3954,7 @@ static const char *configs_file (FILE *fp, const char *name, float *delay) { Rc.zero_suppress = 0; // lastly, let's process any optional glob(s) ... + fbuf[0] = '\0'; config_insp(fp, fbuf, sizeof(fbuf)); return NULL; @@ -4655,76 +4721,25 @@ signify_that: } // end: help_view -static void other_selection (int ch) { - int (*rel)(const char *, const char *); - char *(*sel)(const char *, const char *); - char raw[MEDBUFSIZ], ops, *glob, *pval; - struct osel_s *osel; - const char *typ; - int inc, enu; +static void other_filters (int ch) { + const char *txt, *p; + char *glob; - if (ch == 'o') { - typ = N_txt(OSEL_casenot_txt); - rel = strcasecmp; - sel = strcasestr; - } else { - 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; - for (osel = Curwin->osel_1st; osel; ) { - if (!strcmp(osel->raw, glob)) { // #1: is criteria duplicate? - show_msg(N_txt(OSEL_errdups_txt)); - 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; - } - 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))); + if (ch == 'o') txt = N_txt(OSEL_casenot_txt); + else txt = N_txt(OSEL_caseyes_txt); + + glob = ioline(fmtmk(N_fmt(OSEL_prompts_fmt), Curwin->osel_tot + 1, txt)); + if (*glob == kbd_ESC || !*glob) return; - } - if (Curwin->osel_prt && strlen(Curwin->osel_prt) >= INT_MAX - (sizeof(raw) + 6)) { + + if ((p = osel_add(ch, glob))) { + show_msg(p); 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 SETw(Curwin, NOHISEL_xxx); #endif -} // end: other_selection +} // end: other_filters static void write_rcfile (void) { @@ -5063,7 +5078,7 @@ static void keys_task (int ch) { break; case 'O': case 'o': - if (VIZCHKw(w)) other_selection(ch); + if (VIZCHKw(w)) other_filters(ch); break; case 'U': case 'u': diff --git a/top/top.h b/top/top.h index e2fea140..f4fa89aa 100644 --- a/top/top.h +++ b/top/top.h @@ -757,6 +757,7 @@ typedef struct WIN_t { //atic int insp_view_choice (proc_t *obj); //atic void inspection_utility (int pid); /*------ Other Filtering ------------------------------------------------*/ +//atic const char *osel_add (int ch, char *glob); //atic void osel_clear (WIN_t *q); //atic inline int osel_matched (const WIN_t *q, FLG_t enu, const char *str); /*------ Startup routines ----------------------------------------------*/ @@ -782,7 +783,7 @@ typedef struct WIN_t { //atic inline int find_ofs (const WIN_t *q, const char *buf); //atic void find_string (int ch); //atic void help_view (void); -//atic void other_selection (int ch); +//atic void other_filters (int ch); //atic void write_rcfile (void); /*------ Interactive Input Secondary support (do_key helpers) ----------*/ //atic void keys_global (int ch);