improve configuration parsing error

This commit is contained in:
Omar Polo 2021-01-27 16:45:45 +00:00
parent 40dc608f1f
commit 13ed2fb63f
4 changed files with 46 additions and 37 deletions

33
gmid.c
View File

@ -32,7 +32,7 @@
struct vhost hosts[HOSTSLEN];
int exfd, foreground, goterror;
int exfd, foreground;
struct conf conf;
@ -291,37 +291,6 @@ load_local_cert(const char *hostname, const char *dir)
hosts[0].domain = hostname;
}
void
yyerror(const char *msg)
{
goterror = 1;
fprintf(stderr, "%d: %s\n", yylineno, msg);
}
int
parse_portno(const char *p)
{
const char *errstr;
int n;
n = strtonum(p, 0, UINT16_MAX, &errstr);
if (errstr != NULL)
errx(1, "port number is %s: %s", errstr, p);
return n;
}
void
parse_conf(const char *path)
{
if ((yyin = fopen(path, "r")) == NULL)
fatal("cannot open config %s", path);
yyparse();
fclose(yyin);
if (goterror)
exit(1);
}
void
load_vhosts(void)
{

7
gmid.h
View File

@ -171,9 +171,6 @@ void gen_certificate(const char*, const char*, const char*);
void mkdirs(const char*);
char *data_dir(void);
void load_local_cert(const char*, const char*);
void yyerror(const char*);
int parse_portno(const char*);
void parse_conf(const char*);
void load_vhosts(void);
int make_socket(int, int);
void setup_tls(void);
@ -188,6 +185,10 @@ extern int yylineno;
extern int yyparse(void);
extern int yylex(void);
void yyerror(const char*);
int parse_portno(const char*);
void parse_conf(const char*);
/* mime.c */
void init_mime(struct mime*);
void add_mime(struct mime*, const char*, const char*);

2
lex.l
View File

@ -76,7 +76,7 @@ auto return TAUTO;
[ \t]+ ;
. errx(1, "%d: unexpected character %c", yylineno, *yytext);
. yyerror("unexpected character"); exit(1);
%%

41
parse.y
View File

@ -33,7 +33,12 @@ size_t ihost = 0;
struct location *loc = &hosts[0].locations[0];
size_t iloc = 0;
extern void yyerror(const char*);
int goterror = 0;
const char *config_path;
void yyerror(const char*);
int parse_portno(const char*);
void parse_conf(const char*);
%}
@ -145,3 +150,37 @@ locopt : TDEFAULT TTYPE TSTRING {
}
| TAUTO TINDEX TBOOL { loc->auto_index = $3 ? 1 : -1; }
;
%%
void
yyerror(const char *msg)
{
goterror = 1;
fprintf(stderr, "%s:%d: %s\n", config_path, yylineno, msg);
}
int
parse_portno(const char *p)
{
const char *errstr;
int n;
n = strtonum(p, 0, UINT16_MAX, &errstr);
if (errstr != NULL)
errx(1, "port number is %s: %s", errstr, p);
return n;
}
void
parse_conf(const char *path)
{
config_path = path;
if ((yyin = fopen(path, "r")) == NULL)
fatal("cannot open config %s", path);
yyparse();
fclose(yyin);
if (goterror)
exit(1);
}