From 8398b7d9b09bfd19eb63e602f67b51373d2b85b2 Mon Sep 17 00:00:00 2001 From: Jim Warner Date: Wed, 20 Jun 2018 00:00:00 -0500 Subject: [PATCH] top: refactor some of that configuration files support The 'config_file()' function was getting a little long in the tooth, so this commit simply renames/rearranges some stuff anticipating 'other filters' in the rcfile. Signed-off-by: Jim Warner --- top/top.c | 161 +++++++++++++++++++++++++++++------------------------- top/top.h | 3 +- 2 files changed, 89 insertions(+), 75 deletions(-) diff --git a/top/top.c b/top/top.c index 424bfd72..8d2bf16a 100644 --- a/top/top.c +++ b/top/top.c @@ -3157,7 +3157,7 @@ static void before (char *me) { /* - * A config_file *Helper* function responsible for converting + * A configs_file *Helper* function responsible for converting * a single window's old rc stuff into a new style rcfile entry */ static int config_cvt (WIN_t *q) { static struct { @@ -3229,10 +3229,90 @@ static int config_cvt (WIN_t *q) { } // end: config_cvt + /* + * A configs_file *Helper* function responsible for reading + * and validating a configuration file's 'Inspection' entries */ +static int config_insp (FILE *fp, char *buf, size_t size) { + int i; + + // we'll start off Inspect stuff with 1 'potential' blank line + // ( only realized if we end up with Inspect.total > 0 ) + for (i = 0, Inspect.raw = alloc_s("\n");;) { + #define iT(element) Inspect.tab[i].element + size_t lraw = strlen(Inspect.raw) +1; + int n, x; + char *s; + + if (i < 0 || (size_t)i >= INT_MAX / sizeof(struct I_ent)) break; + if (lraw >= INT_MAX - size) break; + + if (!fgets(buf, size, fp)) break; + lraw += strlen(buf) +1; + Inspect.raw = alloc_r(Inspect.raw, lraw); + strcat(Inspect.raw, buf); + + if (buf[0] == '#' || buf[0] == '\n') continue; + Inspect.tab = alloc_r(Inspect.tab, sizeof(struct I_ent) * (i + 1)); + + // part of this is used in a show_special() call, so let's sanitize it + for (n = 0, x = strlen(buf); n < x; n++) { + if ((buf[n] != '\t' && buf[n] != '\n') + && (buf[n] < ' ')) { + buf[n] = '.'; + Rc_questions = 1; + } + } + if (!(s = strtok(buf, "\t\n"))) { Rc_questions = 1; continue; } + iT(type) = alloc_s(s); + if (!(s = strtok(NULL, "\t\n"))) { Rc_questions = 1; continue; } + iT(name) = alloc_s(s); + if (!(s = strtok(NULL, "\t\n"))) { Rc_questions = 1; continue; } + iT(fmts) = alloc_s(s); + + switch (toupper(buf[0])) { + case 'F': + iT(func) = insp_do_file; + break; + case 'P': + iT(func) = insp_do_pipe; + break; + default: + Rc_questions = 1; + continue; + } + iT(farg) = (strstr(iT(fmts), "%d")) ? 1 : 0; + iT(fstr) = alloc_c(FNDBUFSIZ); + iT(flen) = 0; + + ++i; + #undef iT + } // end: for ('inspect' entries) + + Inspect.total = i; +#ifndef INSP_OFFDEMO + if (!Inspect.total) { + #define mkS(n) N_txt(YINSP_demo ## n ## _txt) + const char *sels[] = { mkS(01), mkS(02), mkS(03) }; + Inspect.total = Inspect.demo = MAXTBL(sels); + Inspect.tab = alloc_c(sizeof(struct I_ent) * Inspect.total); + for (i = 0; i < Inspect.total; i++) { + Inspect.tab[i].type = alloc_s(N_txt(YINSP_deqtyp_txt)); + Inspect.tab[i].name = alloc_s(sels[i]); + Inspect.tab[i].func = insp_do_demo; + Inspect.tab[i].fmts = alloc_s(N_txt(YINSP_deqfmt_txt)); + Inspect.tab[i].fstr = alloc_c(FNDBUFSIZ); + } + #undef mkS + } +#endif + return 0; +} // end: config_insp + + /* * A configs_reads *Helper* function responsible for processing * a configuration file (personal or system-wide default) */ -static const char *config_file (FILE *fp, const char *name, float *delay) { +static const char *configs_file (FILE *fp, const char *name, float *delay) { char fbuf[LRGBUFSIZ]; int i, tmp_whole, tmp_fract; const char *p = NULL; @@ -3339,78 +3419,11 @@ static const char *config_file (FILE *fp, const char *name, float *delay) { if (Rc.zero_suppress < 0 || Rc.zero_suppress > 1) Rc.zero_suppress = 0; - // we'll start off Inspect stuff with 1 'potential' blank line - // ( only realized if we end up with Inspect.total > 0 ) - for (i = 0, Inspect.raw = alloc_s("\n");;) { - #define iT(element) Inspect.tab[i].element - size_t lraw = strlen(Inspect.raw) +1; - int n, x; - char *s; + // lastly, let's process any optional glob(s) ... + config_insp(fp, fbuf, sizeof(fbuf)); - if (i < 0 || (size_t)i >= INT_MAX / sizeof(struct I_ent)) break; - if (lraw >= INT_MAX - sizeof(fbuf)) break; - - if (!fgets(fbuf, sizeof(fbuf), fp)) break; - lraw += strlen(fbuf) +1; - Inspect.raw = alloc_r(Inspect.raw, lraw); - strcat(Inspect.raw, fbuf); - - if (fbuf[0] == '#' || fbuf[0] == '\n') continue; - Inspect.tab = alloc_r(Inspect.tab, sizeof(struct I_ent) * (i + 1)); - - // part of this is used in a show_special() call, so let's sanitize it - for (n = 0, x = strlen(fbuf); n < x; n++) { - if ((fbuf[n] != '\t' && fbuf[n] != '\n') - && (fbuf[n] < ' ')) { - fbuf[n] = '.'; - Rc_questions = 1; - } - } - if (!(s = strtok(fbuf, "\t\n"))) { Rc_questions = 1; continue; } - iT(type) = alloc_s(s); - if (!(s = strtok(NULL, "\t\n"))) { Rc_questions = 1; continue; } - iT(name) = alloc_s(s); - if (!(s = strtok(NULL, "\t\n"))) { Rc_questions = 1; continue; } - iT(fmts) = alloc_s(s); - - switch (toupper(fbuf[0])) { - case 'F': - iT(func) = insp_do_file; - break; - case 'P': - iT(func) = insp_do_pipe; - break; - default: - Rc_questions = 1; - continue; - } - iT(farg) = (strstr(iT(fmts), "%d")) ? 1 : 0; - iT(fstr) = alloc_c(FNDBUFSIZ); - iT(flen) = 0; - - ++i; - #undef iT - } // end: for ('inspect' entries) - - Inspect.total = i; -#ifndef INSP_OFFDEMO - if (!Inspect.total) { - #define mkS(n) N_txt(YINSP_demo ## n ## _txt) - const char *sels[] = { mkS(01), mkS(02), mkS(03) }; - Inspect.total = Inspect.demo = MAXTBL(sels); - Inspect.tab = alloc_c(sizeof(struct I_ent) * Inspect.total); - for (i = 0; i < Inspect.total; i++) { - Inspect.tab[i].type = alloc_s(N_txt(YINSP_deqtyp_txt)); - Inspect.tab[i].name = alloc_s(sels[i]); - Inspect.tab[i].func = insp_do_demo; - Inspect.tab[i].fmts = alloc_s(N_txt(YINSP_deqfmt_txt)); - Inspect.tab[i].fstr = alloc_c(FNDBUFSIZ); - } - #undef mkS - } -#endif return NULL; -} // end: config_file +} // end: configs_file /* @@ -3493,14 +3506,14 @@ static void configs_reads (void) { } if (fp) { - p = config_file(fp, Rc_name, &tmp_delay); + p = configs_file(fp, Rc_name, &tmp_delay); fclose(fp); if (p) goto default_or_error; } else { system_default: fp = fopen(SYS_RCDEFAULTS, "r"); if (fp) { - p = config_file(fp, SYS_RCDEFAULTS, &tmp_delay); + p = configs_file(fp, SYS_RCDEFAULTS, &tmp_delay); fclose(fp); if (p) goto default_or_error; } diff --git a/top/top.h b/top/top.h index c97bc8ec..f433549c 100644 --- a/top/top.h +++ b/top/top.h @@ -622,7 +622,8 @@ typedef struct WIN_t { /*------ Startup routines ----------------------------------------------*/ //atic void before (char *me); //atic int config_cvt (WIN_t *q); -//atic const char *config_file (FILE *fp, const char *name, float *delay); +//atic int config_insp (FILE *fp, char *buf, size_t size); +//atic const char *configs_file (FILE *fp, const char *name, float *delay); //atic int configs_path (const char *const fmts, ...); //atic void configs_reads (void); //atic void parse_args (char **args);