move parse_portno to gmid.c

it's used only to parse the -p flag.  While there add check_port_num
to check the range for the port.
This commit is contained in:
Omar Polo 2021-07-09 13:06:58 +00:00
parent e3b2a0f8de
commit 391825e360
3 changed files with 24 additions and 15 deletions

12
gmid.c
View File

@ -532,6 +532,18 @@ setup_configless(int argc, char **argv, const char *cgi)
imsg_flush(&logibuf);
}
static int
parse_portno(const char *p)
{
const char *errstr;
int n;
n = strtonum(p, 0, UINT16_MAX, &errstr);
if (errstr != NULL)
yyerror("port number is %s: %s", errstr, p);
return n;
}
int
main(int argc, char **argv)
{

1
gmid.h
View File

@ -317,7 +317,6 @@ void free_config(void);
void drop_priv(void);
void yyerror(const char*, ...);
int parse_portno(const char*);
void parse_conf(const char*);
int cmdline_symset(char *);

26
parse.y
View File

@ -78,11 +78,11 @@ char *symget(const char *);
struct vhost *new_vhost(void);
struct location *new_location(void);
int parse_portno(const char*);
char *ensure_absolute_path(char*);
int check_block_code(int);
char *check_block_fmt(char*);
int check_strip_no(int);
int check_port_num(int);
int check_prefork_num(int);
void advance_loc(void);
void only_once(const void*, const char*);
@ -190,7 +190,7 @@ option : TCHROOT string { conf.chroot = $2; }
add_mime(&conf.mime, $2, $3);
}
| TMAP string TTOEXT string { add_mime(&conf.mime, $2, $4); }
| TPORT NUM { conf.port = $2; }
| TPORT NUM { conf.port = check_port_num($2); }
| TPREFORK NUM { conf.prefork = check_prefork_num($2); }
| TPROTOCOLS string {
if (tls_config_parse_protocols(&conf.protos, $2) == -1)
@ -864,18 +864,6 @@ new_location(void)
return l;
}
int
parse_portno(const char *p)
{
const char *errstr;
int n;
n = strtonum(p, 0, UINT16_MAX, &errstr);
if (errstr != NULL)
yyerror("port number is %s: %s", errstr, p);
return n;
}
char *
ensure_absolute_path(char *path)
{
@ -923,6 +911,16 @@ check_strip_no(int n)
return n;
}
int
check_port_num(int n)
{
if (n <= 0 || n >= UINT16_MAX)
yyerror("port number is %s: %d",
n <= 0 ? "too small" : "too large",
n);
return n;
}
int
check_prefork_num(int n)
{