]> git.pond.sub.org Git - empserver/blobdiff - src/lib/gen/emp_config.c
Update copyright notice.
[empserver] / src / lib / gen / emp_config.c
index 398322cfbf50f11369991c65f3ec4078a058e1f1..1006dd98d4e22bec07a8f3711d7d4f7d67976c9a 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2005, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -19,9 +19,9 @@
  *
  *  ---
  *
- *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
- *  related information and legal notices. It is expected that any future
- *  projects/authors will amend these files as needed.
+ *  See files README, COPYING and CREDITS in the root of the source
+ *  tree for related information and legal notices.  It is expected
+ *  that future projects/authors will amend these files as needed.
  *
  *  ---
  *
 /*
  * STILL TO DO
  *
- * 1. Change other constants - such as Num Countries etc.
- *    Just requires variables to be assigned, then dynamic allocation in
- *    a few places. Some checks needed in the server to check the world
- *    hasn't changed size etc.
- * 2. Could look at loading in planes, units etc. Should be easy enough.
- *
+ * Change other constants - such as MAXNOC etc.
+ * Just requires variables to be assigned, then dynamic allocation in
+ * a few places.  Some checks needed in the server to check the world
+ * hasn't changed size etc.
  */
 
+#include <config.h>
+
 #include <assert.h>
+#include <ctype.h>
 #include <errno.h>
 #include <stdio.h>
-#include <stdlib.h>            /* atoi free atol */
 #include <string.h>
+#ifdef _WIN32
+#include <direct.h>
+#else
+#include <unistd.h>
+#endif
 
-#include "misc.h"
 #include "file.h"
+#include "misc.h"
 #include "optlist.h"
-#include "gen.h"               /* parse */
+#include "prototypes.h"
 
 /* Dummy one */
 static int emp_config_dummy;
@@ -64,7 +69,8 @@ struct keymatch configkeys[] = {
 #undef EMP_CONFIG_C_OUTPUT
 };
 
-static struct keymatch *keylookup(s_char *key, struct keymatch tbl[]);
+static struct keymatch *keylookup(char *key, struct keymatch tbl[]);
+static void set_dirs(char *);
 
 /*
  * read in empire configuration
@@ -86,13 +92,13 @@ emp_config(char *file)
     errno = 0;
     if ((fp = fopen(file, "r")) == NULL) {
        if (file == dflt_econfig && errno == ENOENT)
-           return 0;
+           goto done;
        fprintf(stderr, "Can't open %s for reading (%s)\n",
                file, strerror(errno));
        return -1;
     }
 
-    while (fgets(buf, sizeof buf, fp) != NULL) {
+    while (fgets(buf, sizeof(buf), fp) != NULL) {
        ++lno;
        for (i = 0; buf[i] && isspace(buf[i]); ++i) ;
        if (!buf[i] || buf[i] == '#')
@@ -143,17 +149,21 @@ emp_config(char *file)
            errors = 1;
        }
     }
+
     fclose(fp);
-    WORLD_X &= ~1;             /* make even */
+
+done:
+    WORLD_X &= ~1;             /* force even */
+    set_dirs(file);
 
     return -errors;
 }
 
 /* find the key in the table */
 static struct keymatch *
-keylookup(register s_char *command, struct keymatch *tbl)
+keylookup(char *command, struct keymatch *tbl)
 {
-    register struct keymatch *kp;
+    struct keymatch *kp;
 
     if (command == 0 || *command == 0)
        return 0;
@@ -164,6 +174,39 @@ keylookup(register s_char *command, struct keymatch *tbl)
     return NULL;
 }
 
+static void
+set_dirs(char *econfig)
+{
+    char *slash;
+    char *cwd = getcwd(NULL, 0);
+
+#ifdef _WIN32
+    econfig = _fullpath(NULL, econfig, 0);
+    slash = strrchr(econfig, '\\');
+    configdir = malloc(slash - econfig + 1);
+    memcpy(configdir, econfig, slash - econfig);
+    configdir[slash - econfig] = 0;
+#else
+    if ((slash = strrchr(econfig, '/'))) {
+       configdir = malloc(slash - econfig + 1);
+       memcpy(configdir, econfig, slash - econfig);
+       configdir[slash - econfig] = 0;
+    } else
+       configdir = strdup(cwd);
+
+    if (configdir[0] != '/') {
+       char *tmp = configdir;
+       size_t len = strlen(cwd);
+
+       configdir = malloc(len + 1 + strlen(tmp) + 1);
+       sprintf(configdir, "%s/%s", cwd, tmp);
+       free(tmp);
+    }
+#endif /* !_WIN32 */
+
+    free(cwd);
+}
+
 void
 print_config(FILE *fp)
 {
@@ -172,7 +215,7 @@ print_config(FILE *fp)
     fprintf(fp, "# Empire Configuration File:\n");
     for (kp = configkeys; kp->km_key; kp++) {
        if (kp->km_comment) {
-           if (kp->km_comment[0] != '\n')
+           if (kp->km_comment[0] != '\n' && kp->km_comment[0] != '#')
                fprintf(fp, "\n# ");
            fprintf(fp, "%s\n", kp->km_comment);
        }