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-17 00:00:00 -05:00 committed by Craig Small
parent e691f75b81
commit 2b7779fdeb
2 changed files with 89 additions and 75 deletions

161
top/top.c
View File

@ -3625,7 +3625,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 {
@ -3697,10 +3697,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;
@ -3807,78 +3887,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
/*
@ -3961,14 +3974,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;
}

View File

@ -761,7 +761,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);