]> git.pond.sub.org Git - empserver/blobdiff - src/lib/gen/emp_config.c
Update copyright notice.
[empserver] / src / lib / gen / emp_config.c
index 54b1ad96c8178aa89b972457e6c30bdd8976b82e..1006dd98d4e22bec07a8f3711d7d4f7d67976c9a 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2004, 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 */
-
-/* for systems without strdup  */
-#ifdef NOSTRDUP
-extern char *strdup();
-#endif /* NOSTRDUP */
+#include "prototypes.h"
 
 /* Dummy one */
 static int emp_config_dummy;
@@ -69,9 +69,8 @@ struct keymatch configkeys[] = {
 #undef EMP_CONFIG_C_OUTPUT
 };
 
-static void fixup_files(void);
-static struct keymatch *keylookup(s_char *key, struct keymatch tbl[]);
-static int set_option(const char *, int);
+static struct keymatch *keylookup(char *key, struct keymatch tbl[]);
+static void set_dirs(char *);
 
 /*
  * read in empire configuration
@@ -81,29 +80,30 @@ emp_config(char *file)
 {
     FILE *fp;
     char scanspace[1024];
-    char *av[65];
-    char buf[BUFSIZ];
+    char *av[128];
+    char buf[1024];
     struct keymatch *kp;
     int lno = 0;
     int errors = 0;
     int i;
 
-    if (file == NULL) {
-       fixup_files();
-       return 0;
-    }
+    if (!file)
+       file = dflt_econfig;
+    errno = 0;
     if ((fp = fopen(file, "r")) == NULL) {
+       if (file == dflt_econfig && errno == ENOENT)
+           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] == '#')
            continue;
-       if (parse(buf, av, 0, scanspace, 0) < 0) {
+       if (parse(buf, av, NULL, scanspace, NULL) < 0) {
            fprintf(stderr, "%s:%d: Can't parse line %s", file, lno, buf);
            errors = 1;
            continue;
@@ -140,15 +140,6 @@ emp_config(char *file)
            *(char **)kp->km_data = strdup(av[1]);
            kp->km_flags |= KM_ALLOC;
            break;
-       case NSC_NOTYPE:
-           for (i = 1; av[i]; ++i) {
-               if (set_option(av[i], kp->km_key[0] != 'n') < 0) {
-                   fprintf(stderr, "%s:%d: Unknown option %s\n",
-                           file, lno, av[i]);
-                   errors = 1;
-               }
-           }
-           break;
        default:
            assert(0);
        }
@@ -158,69 +149,21 @@ emp_config(char *file)
            errors = 1;
        }
     }
-    fclose(fp);
-    fixup_files();
-    WORLD_X &= ~1;             /* make even */
-
-    return -errors;
-}
-
-struct otherfiles {
-    char **files;
-    char *name;
-};
 
-/* list of other well known files... -maybe tailor these oneday
- * anyway - meantime they are all relative to datadir */
-static struct otherfiles ofiles[] = {
-    {&motdfil, "motd"},
-    {&downfil, "down"},
-    {&disablefil, "disable"},
-    {&banfil, "ban"},
-    {&authfil, "auth"},
-    {&annfil, "ann"},
-    {&timestampfil, "timestamp"},
-    {&teldir, "tel"},
-#if !defined(_WIN32)
-    {&telfil, "tel/tel"},
-#else
-    {&telfil, "tel\\tel"},
-#endif
-    {NULL, NULL}
-};
+    fclose(fp);
 
-/* fix up the empfile struct to reference full path names */
-static void
-fixup_files(void)
-{
-    struct empfile *ep;
-    struct otherfiles *op;
-    s_char buf[1024];
+done:
+    WORLD_X &= ~1;             /* force even */
+    set_dirs(file);
 
-    for (ep = empfile; ep < &empfile[EF_MAX]; ep++) {
-#if !defined(_WIN32)
-       sprintf(buf, "%s/%s", datadir, ep->name);
-#else
-       sprintf(buf, "%s\\%s", datadir, ep->name);
-#endif
-       ep->file = strdup(buf);
-    }
-
-    for (op = ofiles; op->files; op++) {
-#if !defined(_WIN32)
-       sprintf(buf, "%s/%s", datadir, op->name);
-#else
-       sprintf(buf, "%s\\%s", datadir, op->name);
-#endif
-       *op->files = strdup(buf);
-    }
+    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;
@@ -231,18 +174,48 @@ 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)
 {
-    struct empfile *ep;
-    struct option_list *op;
-    struct otherfiles *ofp;
     struct keymatch *kp;
 
     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);
        }
@@ -264,36 +237,10 @@ print_config(FILE *fp)
        case NSC_LONG:
            fprintf(fp, "%s %ld\n", kp->km_key, *(long *)kp->km_data);
            break;
-       case NSC_NOTYPE:
-           for (op = Options; op->opt_key; op++)
-               if (*op->opt_valuep != (kp->km_key[0] == 'n'))
-                   fprintf(fp, "%s %s\n", kp->km_key, op->opt_key);
-           break;
        default:
            assert(0);
        }
     }
 
     fprintf(fp, "\n");
-    for (ep = empfile; ep < &empfile[EF_MAX]; ep++)
-       fprintf(fp, "# File %s -> %s\n", ep->name, ep->file);
-    for (ofp = ofiles; ofp->files; ofp++)
-       fprintf(fp, "# File %s -> %s\n", ofp->name, *(ofp->files));
-
-}
-
-
-/* Set option S to value VAL; return 0 on success, -1 on failure.  */
-static int
-set_option(const char *s, int val)
-{
-    struct option_list *op;
-
-    for (op = Options; op->opt_key; op++) {
-       if (strcmp(op->opt_key, s) == 0) {
-           *op->opt_valuep = val;
-           return 0;
-       }
-    }
-    return -1;
 }