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 <james.warner@comcast.net>
This commit is contained in:
Jim Warner 2018-06-20 00:00:00 -05:00 committed by Craig Small
parent 567a9b6240
commit 8398b7d9b0
2 changed files with 89 additions and 75 deletions

161
top/top.c
View File

@ -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 */ * a single window's old rc stuff into a new style rcfile entry */
static int config_cvt (WIN_t *q) { static int config_cvt (WIN_t *q) {
static struct { static struct {
@ -3229,10 +3229,90 @@ static int config_cvt (WIN_t *q) {
} // end: config_cvt } // 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 configs_reads *Helper* function responsible for processing
* a configuration file (personal or system-wide default) */ * 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]; char fbuf[LRGBUFSIZ];
int i, tmp_whole, tmp_fract; int i, tmp_whole, tmp_fract;
const char *p = NULL; 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) if (Rc.zero_suppress < 0 || Rc.zero_suppress > 1)
Rc.zero_suppress = 0; Rc.zero_suppress = 0;
// we'll start off Inspect stuff with 1 'potential' blank line // lastly, let's process any optional glob(s) ...
// ( only realized if we end up with Inspect.total > 0 ) config_insp(fp, fbuf, sizeof(fbuf));
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 - 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; return NULL;
} // end: config_file } // end: configs_file
/* /*
@ -3493,14 +3506,14 @@ static void configs_reads (void) {
} }
if (fp) { if (fp) {
p = config_file(fp, Rc_name, &tmp_delay); p = configs_file(fp, Rc_name, &tmp_delay);
fclose(fp); fclose(fp);
if (p) goto default_or_error; if (p) goto default_or_error;
} else { } else {
system_default: system_default:
fp = fopen(SYS_RCDEFAULTS, "r"); fp = fopen(SYS_RCDEFAULTS, "r");
if (fp) { if (fp) {
p = config_file(fp, SYS_RCDEFAULTS, &tmp_delay); p = configs_file(fp, SYS_RCDEFAULTS, &tmp_delay);
fclose(fp); fclose(fp);
if (p) goto default_or_error; if (p) goto default_or_error;
} }

View File

@ -622,7 +622,8 @@ typedef struct WIN_t {
/*------ Startup routines ----------------------------------------------*/ /*------ Startup routines ----------------------------------------------*/
//atic void before (char *me); //atic void before (char *me);
//atic int config_cvt (WIN_t *q); //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 int configs_path (const char *const fmts, ...);
//atic void configs_reads (void); //atic void configs_reads (void);
//atic void parse_args (char **args); //atic void parse_args (char **args);