Allow config file to be specified as the first command line argument. Allow cmd line options be specified together with the config file.

This commit is contained in:
valenok committed 2011-06-21 22:04:58 +01:00
1 parent 81a5e3f342
commit 5cf7e899c4
3 files changed
+15 -16

No files matched your search

+9 -8
View File
@@ -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) { static void process_command_line_arguments(char *argv[], char **options) {
char line[512], opt[512], val[512], *p; char line[512], opt[512], val[512], *p;
FILE *fp = NULL; FILE *fp = NULL;
size_t i, line_no = 0; size_t i, cmd_line_opts_start = 1, line_no = 0;
options[0] = NULL; options[0] = NULL;
// Should we use a config file ? // 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]); snprintf(config_file, sizeof(config_file), "%s", argv[1]);
cmd_line_opts_start = 2;
} else if ((p = strrchr(argv[0], DIRSEP)) == NULL) { } else if ((p = strrchr(argv[0], DIRSEP)) == NULL) {
// No command line flags specified. Look where binary lives // No command line flags specified. Look where binary lives
snprintf(config_file, sizeof(config_file), "%s", CONFIG_FILE); 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"); fp = fopen(config_file, "r");
// If config file was set in command line and open failed, exit // If config file was set in command line and open failed, die
if (argv[1] != NULL && argv[2] == NULL && fp == NULL) { if (cmd_line_opts_start == 2 && fp == NULL) {
die("Cannot open config file %s: %s", config_file, strerror(errno)); 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. // 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) { if (argv[i][0] != '-' || argv[i + 1] == NULL) {
show_usage_and_exit(); show_usage_and_exit();
} }
@@ -218,8 +219,8 @@ static void start_mongoose(int argc, char *argv[]) {
char *options[MAX_OPTIONS]; char *options[MAX_OPTIONS];
int i; int i;
/* Edit passwords file if -A option is specified */ // Edit passwords file if -A option is specified
if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 'A') { if (argc > 1 && !strcmp(argv[1], "-A")) {
if (argc != 6) { if (argc != 6) {
show_usage_and_exit(); show_usage_and_exit();
} }
@@ -227,7 +228,7 @@ static void start_mongoose(int argc, char *argv[]) {
EXIT_SUCCESS : EXIT_FAILURE); 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"))) { if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
show_usage_and_exit(); show_usage_and_exit();
} }
-1
View File
@@ -9,7 +9,6 @@
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Op Ar config_file .Op Ar config_file
.Nm
.Op Ar OPTIONS .Op Ar OPTIONS
.Nm .Nm
.Fl A Ar htpasswd_file domain_name user_name password .Fl A Ar htpasswd_file domain_name user_name password
+6 -7
View File
@@ -150,18 +150,17 @@ if (scalar(@ARGV) > 0 and $ARGV[0] eq 'embedded') {
exit 0; exit 0;
} }
# Make sure we load config file if no options are given # Make sure we load config file if no options are given.
write_file($config, "listening_ports 12345\n"); # Command line options override config files settings
spawn("$exe -a access.log"); write_file($config, "access_log_file access.log\nlistening_ports 12345\n");
my $saved_port = $port; spawn("$exe -p $port");
$port = 12345;
o("GET /test/hello.txt HTTP/1.0\n\n", 'HTTP/1.1 200 OK', 'Loading config file'); o("GET /test/hello.txt HTTP/1.0\n\n", 'HTTP/1.1 200 OK', 'Loading config file');
$port = $saved_port;
unlink $config; unlink $config;
kill_spawned_child(); kill_spawned_child();
# Spawn the server on port $port # 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 ". "-error_log_file debug.log ".
"-cgi_environment CGI_FOO=foo,CGI_BAR=bar,CGI_BAZ=baz " . "-cgi_environment CGI_FOO=foo,CGI_BAR=bar,CGI_BAZ=baz " .
"-extra_mime_types .bar=foo/bar,.tar.gz=blah,.baz=foo " . "-extra_mime_types .bar=foo/bar,.tar.gz=blah,.baz=foo " .