From 5cf7e899c4712faf6041b168f295a5d29d0fbf0d Mon Sep 17 00:00:00 2001 From: valenok Date: Tue, 21 Jun 2011 22:04:58 +0100 Subject: [PATCH] Allow config file to be specified as the first command line argument. Allow cmd line options be specified together with the config file. --- main.c | 17 +++++++++-------- mongoose.1 | 1 - test/test.pl | 13 ++++++------- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/main.c b/main.c index de8aee0b..54ffc70d 100644 --- a/main.c +++ b/main.c @@ -156,13 +156,14 @@ static void set_option(char **options, const char *name, const char *value) { static void process_command_line_arguments(char *argv[], char **options) { char line[512], opt[512], val[512], *p; FILE *fp = NULL; - size_t i, line_no = 0; + size_t i, cmd_line_opts_start = 1, line_no = 0; options[0] = NULL; // Should we use a config file ? - if (argv[1] != NULL && argv[2] == NULL) { + if (argv[1] != NULL && argv[1][0] != '-') { snprintf(config_file, sizeof(config_file), "%s", argv[1]); + cmd_line_opts_start = 2; } else if ((p = strrchr(argv[0], DIRSEP)) == NULL) { // No command line flags specified. Look where binary lives snprintf(config_file, sizeof(config_file), "%s", CONFIG_FILE); @@ -173,8 +174,8 @@ static void process_command_line_arguments(char *argv[], char **options) { fp = fopen(config_file, "r"); - // If config file was set in command line and open failed, exit - if (argv[1] != NULL && argv[2] == NULL && fp == NULL) { + // If config file was set in command line and open failed, die + if (cmd_line_opts_start == 2 && fp == NULL) { die("Cannot open config file %s: %s", config_file, strerror(errno)); } @@ -201,7 +202,7 @@ static void process_command_line_arguments(char *argv[], char **options) { } // Now handle command line flags. They override config file settings. - for (i = 1; argv[i] != NULL; i += 2) { + for (i = cmd_line_opts_start; argv[i] != NULL; i += 2) { if (argv[i][0] != '-' || argv[i + 1] == NULL) { show_usage_and_exit(); } @@ -218,8 +219,8 @@ static void start_mongoose(int argc, char *argv[]) { char *options[MAX_OPTIONS]; int i; - /* Edit passwords file if -A option is specified */ - if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'A') { + // Edit passwords file if -A option is specified + if (argc > 1 && !strcmp(argv[1], "-A")) { if (argc != 6) { show_usage_and_exit(); } @@ -227,7 +228,7 @@ static void start_mongoose(int argc, char *argv[]) { EXIT_SUCCESS : EXIT_FAILURE); } - /* Show usage if -h or --help options are specified */ + // Show usage if -h or --help options are specified if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) { show_usage_and_exit(); } diff --git a/mongoose.1 b/mongoose.1 index fbe6905d..03d8b25e 100644 --- a/mongoose.1 +++ b/mongoose.1 @@ -9,7 +9,6 @@ .Sh SYNOPSIS .Nm .Op Ar config_file -.Nm .Op Ar OPTIONS .Nm .Fl A Ar htpasswd_file domain_name user_name password diff --git a/test/test.pl b/test/test.pl index 96054d4d..040b161a 100644 --- a/test/test.pl +++ b/test/test.pl @@ -150,18 +150,17 @@ if (scalar(@ARGV) > 0 and $ARGV[0] eq 'embedded') { exit 0; } -# Make sure we load config file if no options are given -write_file($config, "listening_ports 12345\n"); -spawn("$exe -a access.log"); -my $saved_port = $port; -$port = 12345; +# Make sure we load config file if no options are given. +# Command line options override config files settings +write_file($config, "access_log_file access.log\nlistening_ports 12345\n"); +spawn("$exe -p $port"); o("GET /test/hello.txt HTTP/1.0\n\n", 'HTTP/1.1 200 OK', 'Loading config file'); -$port = $saved_port; unlink $config; kill_spawned_child(); # Spawn the server on port $port -my $cmd = "$exe -listening_ports $port -access_log_file access.log ". +write_file($config, ""); +my $cmd = "$exe $config -listening_ports $port -access_log_file access.log ". "-error_log_file debug.log ". "-cgi_environment CGI_FOO=foo,CGI_BAR=bar,CGI_BAZ=baz " . "-extra_mime_types .bar=foo/bar,.tar.gz=blah,.baz=foo " .