Read symbols with config file handler

Also, make config file error handler able to cope with recursion.
This commit is contained in:
Pasi Kallinen
2017-09-17 10:15:08 +03:00
parent 2db7e20116
commit 11863dd5e9
+86 -44
View File
@@ -198,6 +198,7 @@ boolean FDECL(proc_wizkit_line, (char *));
boolean FDECL(parse_config_line, (char *)); boolean FDECL(parse_config_line, (char *));
STATIC_DCL boolean FDECL(parse_conf_file, (FILE *, boolean (*proc)(char *))); STATIC_DCL boolean FDECL(parse_conf_file, (FILE *, boolean (*proc)(char *)));
STATIC_DCL FILE *NDECL(fopen_sym_file); STATIC_DCL FILE *NDECL(fopen_sym_file);
boolean FDECL(proc_symset_line, (char *));
STATIC_DCL void FDECL(set_symhandling, (char *, int)); STATIC_DCL void FDECL(set_symhandling, (char *, int));
#ifdef NOCWD_ASSUMPTIONS #ifdef NOCWD_ASSUMPTIONS
STATIC_DCL void FDECL(adjust_prefix, (char *, int)); STATIC_DCL void FDECL(adjust_prefix, (char *, int));
@@ -2706,13 +2707,18 @@ const char *filename;
} }
#endif /* USER_SOUNDS */ #endif /* USER_SOUNDS */
static int config_err_line_num = 0; struct _config_error_frame {
static int config_err_num_errors = 0; int line_num;
static boolean config_err_origline_shown = FALSE; int num_errors;
static boolean config_err_fromfile = FALSE; boolean origline_shown;
static boolean config_err_secure = FALSE; boolean fromfile;
static char config_err_origline[4 * BUFSZ]; boolean secure;
static char config_err_source[BUFSZ]; char origline[4 * BUFSZ];
char source[BUFSZ];
struct _config_error_frame *next;
};
struct _config_error_frame *config_error_data = (struct _config_error_frame *)0;
void void
config_error_init(from_file, sourcename, secure) config_error_init(from_file, sourcename, secure)
@@ -2720,31 +2726,41 @@ boolean from_file;
const char *sourcename; const char *sourcename;
boolean secure; boolean secure;
{ {
config_err_line_num = 0; struct _config_error_frame *tmp = (struct _config_error_frame *)
config_err_num_errors = 0; alloc(sizeof(struct _config_error_frame));
config_err_origline_shown = FALSE;
config_err_fromfile = from_file; tmp->line_num = 0;
config_err_secure = secure; tmp->num_errors = 0;
config_err_origline[0] = '\0'; tmp->origline_shown = FALSE;
tmp->fromfile = from_file;
tmp->secure = secure;
tmp->origline[0] = '\0';
if (sourcename && sourcename[0]) if (sourcename && sourcename[0])
Strcpy(config_err_source, sourcename); Strcpy(tmp->source, sourcename);
else else
config_err_source[0] = '\0'; tmp->source[0] = '\0';
tmp->next = config_error_data;
config_error_data = tmp;
} }
STATIC_OVL boolean STATIC_OVL boolean
config_error_nextline(line) config_error_nextline(line)
const char *line; const char *line;
{ {
if (config_err_num_errors && config_err_secure) if (!config_error_data)
return FALSE; return FALSE;
config_err_line_num++; if (config_error_data->num_errors
config_err_origline_shown = FALSE; && config_error_data->secure)
return FALSE;
config_error_data->line_num++;
config_error_data->origline_shown = FALSE;
if (line && line[0]) if (line && line[0])
Strcpy(config_err_origline, line); Strcpy(config_error_data->origline, line);
else else
config_err_origline[0] = '\0'; config_error_data->origline[0] = '\0';
return TRUE; return TRUE;
} }
@@ -2761,17 +2777,22 @@ VA_DECL(const char *, str)
Vsprintf(buf, str, VA_ARGS); Vsprintf(buf, str, VA_ARGS);
config_err_num_errors++; if (!config_error_data)
if (!config_err_origline_shown && !config_err_secure) { return;
pline("\n%s", config_err_origline);
config_err_origline_shown = TRUE; config_error_data->num_errors++;
if (!config_error_data->origline_shown
&& !config_error_data->secure) {
pline("\n%s", config_error_data->origline);
config_error_data->origline_shown = TRUE;
} }
if (config_err_line_num > 0 && !config_err_secure) { if (config_error_data->line_num > 0
Sprintf(lineno, "Line %i: ", config_err_line_num); && !config_error_data->secure) {
Sprintf(lineno, "Line %i: ", config_error_data->line_num);
} else } else
lineno[0] = '\0'; lineno[0] = '\0';
pline("%s %s%s.", pline("%s %s%s.",
config_err_secure ? "Error:" : " *", config_error_data->secure ? "Error:" : " *",
lineno, lineno,
*buf ? buf : "Unknown error"); *buf ? buf : "Unknown error");
@@ -2781,14 +2802,24 @@ VA_DECL(const char *, str)
int int
config_error_done() config_error_done()
{ {
int n = config_err_num_errors; int n;
struct _config_error_frame *tmp = config_error_data;
if (!config_error_data)
return 0;
n = config_error_data->num_errors;
if (n) { if (n) {
pline("\n%i error%s in %s.\n", n, pline("\n%i error%s in %s.\n", n,
(n > 1) ? "s" : "", (n > 1) ? "s" : "",
*config_err_source ? config_err_source : configfile); *config_error_data->source
? config_error_data->source : configfile);
wait_synch(); wait_synch();
} }
config_error_init(FALSE, "", FALSE);
config_error_data = tmp->next;
free(tmp);
return n; return n;
} }
@@ -3088,6 +3119,7 @@ extern const char *known_handling[]; /* drawing.c */
extern const char *known_restrictions[]; /* drawing.c */ extern const char *known_restrictions[]; /* drawing.c */
static int symset_count = 0; /* for pick-list building only */ static int symset_count = 0; /* for pick-list building only */
static boolean chosen_symset_start = FALSE, chosen_symset_end = FALSE; static boolean chosen_symset_start = FALSE, chosen_symset_end = FALSE;
static int symset_which_set = 0;
STATIC_OVL STATIC_OVL
FILE * FILE *
@@ -3107,7 +3139,6 @@ int
read_sym_file(which_set) read_sym_file(which_set)
int which_set; int which_set;
{ {
char buf[4 * BUFSZ];
FILE *fp; FILE *fp;
if (!(fp = fopen_sym_file())) if (!(fp = fopen_sym_file()))
@@ -3115,13 +3146,13 @@ int which_set;
symset_count = 0; symset_count = 0;
chosen_symset_start = chosen_symset_end = FALSE; chosen_symset_start = chosen_symset_end = FALSE;
while (fgets(buf, 4 * BUFSZ, fp)) { symset_which_set = which_set;
if (!parse_sym_line(buf, which_set)) {
raw_printf("Bad symbol line: \"%.50s\"", buf); config_error_init(TRUE, "symbols", FALSE);
wait_synch();
} parse_conf_file(fp, proc_symset_line);
}
(void) fclose(fp); (void) fclose(fp);
if (!chosen_symset_start && !chosen_symset_end) { if (!chosen_symset_start && !chosen_symset_end) {
/* name caller put in symset[which_set].name was not found; /* name caller put in symset[which_set].name was not found;
if it looks like "Default symbols", null it out and return if it looks like "Default symbols", null it out and return
@@ -3131,17 +3162,26 @@ int which_set;
" -_", TRUE) " -_", TRUE)
|| !strcmpi(symset[which_set].name, "default"))) || !strcmpi(symset[which_set].name, "default")))
clear_symsetentry(which_set, TRUE); clear_symsetentry(which_set, TRUE);
config_error_done();
return (symset[which_set].name == 0) ? 1 : 0; return (symset[which_set].name == 0) ? 1 : 0;
} }
if (!chosen_symset_end) { if (!chosen_symset_end)
raw_printf("Missing finish for symset \"%s\"", config_error_add("Missing finish for symset \"%s\"",
symset[which_set].name ? symset[which_set].name symset[which_set].name ? symset[which_set].name
: "unknown"); : "unknown");
wait_synch();
} config_error_done();
return 1; return 1;
} }
boolean
proc_symset_line(buf)
char *buf;
{
return !((boolean) parse_sym_line(buf, symset_which_set));
}
/* returns 0 on error */ /* returns 0 on error */
int int
parse_sym_line(buf, which_set) parse_sym_line(buf, which_set)
@@ -3155,8 +3195,7 @@ int which_set;
/* convert each instance of whitespace (tabs, consecutive spaces) /* convert each instance of whitespace (tabs, consecutive spaces)
into a single space; leading and trailing spaces are stripped */ into a single space; leading and trailing spaces are stripped */
mungspaces(buf); mungspaces(buf);
if (!*buf || *buf == '#' || !strcmp(buf, " "))
return 1;
/* remove trailing comment, if any (this isn't strictly needed for /* remove trailing comment, if any (this isn't strictly needed for
individual symbols, and it won't matter if "X#comment" without individual symbols, and it won't matter if "X#comment" without
separating space slips through; for handling or set description, separating space slips through; for handling or set description,
@@ -3178,6 +3217,7 @@ int which_set;
chosen_symset_start = FALSE; chosen_symset_start = FALSE;
return 1; return 1;
} }
config_error_add("No \"finish\"");
return 0; return 0;
} }
/* skip '=' and space which follows, if any */ /* skip '=' and space which follows, if any */
@@ -3186,8 +3226,10 @@ int which_set;
++bufp; ++bufp;
symp = match_sym(buf); symp = match_sym(buf);
if (!symp) if (!symp) {
config_error_add("Unknown sym keyword");
return 0; return 0;
}
if (!symset[which_set].name) { if (!symset[which_set].name) {
/* A null symset name indicates that we're just /* A null symset name indicates that we're just