瀏覽代碼

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

valenok 14 年之前
父節點
當前提交
5cf7e899c4
共有 3 個文件被更改,包括 15 次插入16 次删除
  1. 9 8
      main.c
  2. 0 1
      mongoose.1
  3. 6 7
      test/test.pl

+ 9 - 8
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();
   }

+ 0 - 1
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

+ 6 - 7
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 " .