From 6b58e1bb56d9a1c0b50770e32fff698d7b862d1e Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Mon, 8 Dec 2014 23:30:14 +0100 Subject: MEDIUM: httpterm: simplify the cmdline parser We always add the config line by line, so make use a central function to parse all that stuff. --- src/haproxy.c | 46 ++++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/src/haproxy.c b/src/haproxy.c index 16098d5..e825fbb 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -501,6 +501,25 @@ void dump(struct sig_handler *sh) pool_gc2(); } +int parse_cfg_line(int (*parse)(const char *file, int linenum, char **args, int kwm), + const char *file, int linenum, ...) +{ + va_list args; + char *local_args[MAX_LINE_ARGS]; + int pos = 0; + + va_start(args, linenum); + + while (pos < MAX_LINE_ARGS - 1) { + local_args[pos] = va_arg(args, char *); + if (!local_args[pos]) + break; + pos++; + } + local_args[pos] = ""; + return parse(file, linenum, local_args, 0); +} + /* * This function initializes all the necessary variables. It only returns * if everything is OK. If something fails, it exits. @@ -704,35 +723,18 @@ void init(int argc, char **argv) init_default_instance(); if (cmd_listener) { - char *args[10]; - - args[0] = "frontend"; - args[1] = "cmdline"; - args[2] = "\0"; + int line = 1; - if (cfg_parse_listen("command_line", 1, args, 0) != 0) + if (parse_cfg_line(cfg_parse_listen, "command_line", line++, "frontend", "cmdline", NULL) != 0) exit(1); - args[0] = "bind"; - args[1] = cmd_listener; - args[2] = "\0"; - - if (cfg_parse_listen("command_line", 2, args, 0) != 0) + if (parse_cfg_line(cfg_parse_listen, "command_line", line++, "bind", cmd_listener, NULL) != 0) exit(1); - args[0] = "mode"; - args[1] = "http"; - args[2] = "\0"; - - if (cfg_parse_listen("command_line", 3, args, 0) != 0) + if (parse_cfg_line(cfg_parse_listen, "command_line", line++, "mode", "http", NULL) != 0) exit(1); - args[0] = "timeout"; - args[1] = "client"; - args[2] = "10s"; - args[3] = "\0"; - - if (cfg_parse_listen("command_line", 4, args, 0) != 0) + if (parse_cfg_line(cfg_parse_listen, "command_line", line++, "timeout", "client", "10s", NULL) != 0) exit(1); } -- 1.7.12.1