diff src/yadex.cc @ 80:2f1ecc1c5f72

Huge cleanup -- move some global variables into a struct.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 26 Sep 2011 17:39:49 +0300
parents 626678d4ecf3
children 154714f3ab2e
line wrap: on
line diff
--- a/src/yadex.cc	Mon Sep 26 17:14:54 2011 +0300
+++ b/src/yadex.cc	Mon Sep 26 17:39:49 2011 +0300
@@ -60,92 +60,82 @@
 #include <readline/history.h>
 #endif
 
-/*
- *        Constants (declared in yadex.h)
- */
-const char *const log_file = "yadex.log";
-const char *const msg_unexpected = "unexpected error";
-const char *const msg_nomem = "Not enough memory";
-
-
-/*
- *        Not real variables -- just unique pointer values
- *        used by functions that return pointers to 
- */
-char error_non_unique[1];        // Found more than one
-char error_none[1];                // Found none
-char error_invalid[1];                // Invalid parameter
+const char *const yadex_log_filename = "yadex.log";
 
 
 /*
  *        Global variables
  */
-const char *install_dir = 0;        // Where Yadex is installed
-FILE *logfile = NULL;                // Filepointer to the error log
-bool Registered = false;        // Registered or shareware game?
-int screen_lines = 24;                // Lines that our TTY can display
-int remind_to_build_nodes = 0;        // Remind user to build nodes
-Wad_res wad_res(&MasterDir);
+Wad_res      wad_res(&cfg.MasterDir);
+YadexConfig  cfg;
+
+void InitializeConfig()
+{
+    cfg.install_dir = NULL,        // Where Yadex is installed
+    cfg.logfile = NULL,                // Filepointer to the error log
+    cfg.Registered = false,        // Registered or shareware game?
+    cfg.screen_lines = 24,                // Lines that our TTY can display
+    cfg.remind_to_build_nodes = false,        // Remind user to build nodes
+    cfg.config_file = NULL;
+    cfg.Debug = false;
+
+    // Set from command line and/or config file
+    cfg.autoscroll = false;
+    cfg.autoscroll_amp = 10;
+    cfg.autoscroll_edge = 30;
+    cfg.copy_linedef_reuse_sidedefs = false;
+    cfg.cpu_big_endian = false;
+
+    cfg.def.ceiling_height = 128;
+    strcpy(cfg.def.ceiling_texture, "CEIL3_5");
+    cfg.def.floor_height = 0;
+    strcpy(cfg.def.floor_texture, "FLOOR4_8");
+    cfg.def.light_level = 144;
+    strcpy(cfg.def.lower_texture, "STARTAN3");
+    strcpy(cfg.def.middle_texture, "STARTAN3");
+    strcpy(cfg.def.upper_texture, "STARTAN3");
+    cfg.def.thing = 3004;
+    cfg.double_click_timeout = 200;
+    cfg.Expert = false;
+    cfg.Game = NULL;
 
-// Set from command line and/or config file
-bool autoscroll = 0;
-unsigned long autoscroll_amp = 10;
-unsigned long autoscroll_edge = 30;
-const char *config_file = NULL;
-int copy_linedef_reuse_sidedefs = 0;
-int cpu_big_endian = 0;
-bool Debug = false;
-int default_ceiling_height = 128;
-char default_ceiling_texture[WAD_FLAT_NAME + 1] = "CEIL3_5";
-int default_floor_height = 0;
-char default_floor_texture[WAD_FLAT_NAME + 1] = "FLOOR4_8";
-int default_light_level = 144;
-char default_lower_texture[WAD_TEX_NAME + 1] = "STARTAN3";
-char default_middle_texture[WAD_TEX_NAME + 1] = "STARTAN3";
-char default_upper_texture[WAD_TEX_NAME + 1] = "STARTAN3";
-int default_thing = 3004;
-int double_click_timeout = 200;
-bool Expert = false;
-const char *Game = NULL;
-int grid_pixels_min = 10;
-int GridMin = 2;
-int GridMax = 128;
-int idle_sleep_ms = 50;
-bool InfoShown = true;
-int zoom_default = 0;                // 0 means fit
-int zoom_step = 0;                // 0 means sqrt(2)
-int digit_zoom_base = 100;
-int digit_zoom_step = 0;        // 0 means sqrt(2)
-confirm_t insert_vertex_split_linedef = YC_ASK_ONCE;
-confirm_t insert_vertex_merge_vertices = YC_ASK_ONCE;
-bool blindly_swap_sidedefs = false;
-const char *Iwad1 = NULL;
-const char *Iwad2 = NULL;
-const char *Iwad3 = NULL;
-const char *Iwad4 = NULL;
-const char *Iwad5 = NULL;
-const char *Iwad6 = NULL;
-const char *Iwad7 = NULL;
-const char *Iwad8 = NULL;
-const char *Iwad9 = NULL;
-const char *Iwad10 = NULL;
-const char *MainWad = NULL;
+    cfg.PatchWads = NULL;
+    cfg.Quiet = false;
+    cfg.Quieter = false;
+    cfg.scroll_less = 10;
+    cfg.scroll_more = 90;
+    cfg.Select0 = false;
+    cfg.show_help = false;
+    cfg.sprite_scale = 100;
+    cfg.SwapButtons = false;
+    cfg.verbose = 0;
+    cfg.welcome_message = 1;
+    cfg.bench = NULL;
+
+    memset(cfg.Iwad, 0, sizeof(cfg.Iwad));
+    cfg.MainWad = NULL;
+    
+    
+    cfg.grid_pixels_min = 10;
+    cfg.GridMin = 2;
+    cfg.GridMax = 128;
+    cfg.idle_sleep_ms = 50;
+    cfg.InfoShown = true;
+    cfg.zoom_default = 0;                // 0 means fit
+    cfg.zoom_step = 0;                // 0 means sqrt(2)
+    cfg.digit_zoom_base = 100;
+    cfg.digit_zoom_step = 0;        // 0 means sqrt(2)
+
 #ifdef AYM_MOUSE_HACKS
-int MouseMickeysH = 5;
-int MouseMickeysV = 5;
+    cfg.MouseMickeysH = 5;
+    cfg.MouseMickeysV = 5;
 #endif
-char **PatchWads = NULL;
-bool Quiet = false;
-bool Quieter = false;
-unsigned long scroll_less = 10;
-unsigned long scroll_more = 90;
-bool Select0 = false;
-int show_help = 0;
-int sprite_scale = 100;
-bool SwapButtons = false;
-int verbose = 0;
-int welcome_message = 1;
-const char *bench = 0;
+
+    cfg.insert_vertex_split_linedef = YC_ASK_ONCE;
+    cfg.insert_vertex_merge_vertices = YC_ASK_ONCE;
+    cfg.blindly_swap_sidedefs = false;
+}
+
 
 // Global variables declared in game.h
 yglf_t yg_level_format = YGLF__;
@@ -164,9 +154,7 @@
 /*
  *        Prototypes of private functions
  */
-static int parse_environment_vars();
 static void MainLoop();
-static void print_error_message(const char *fmt, va_list args);
 static void add_base_colours();
 static const Wad_file *wad_by_name(const char *pathname);
 static bool wad_already_loaded(const char *pathname);
@@ -174,23 +162,41 @@
 typedef struct
 {
     const char *game, *wadname;
-    const char **pwadloc;
 } GameInfo;
 
-GameInfo gameList[] = {
-    {"doom", "doom.wad", &Iwad1},
-    {"doom2", "doom2.wad", &Iwad2},
-    {"heretic", "heretic.wad", &Iwad3},
-    {"hexen", "hexen.wad", &Iwad4},
-    {"strife", "strife1.wad", &Iwad5},
-    {"doom02", "Doom alpha 0.2 IWAD", &Iwad6},
-    {"doom04", "Doom alpha 0.4 IWAD", &Iwad7},
-    {"doom05", "Doom alpha 0.5 IWAD", &Iwad8},
-    {"doompr", "Doom Press Release IWAD", &Iwad9},
-    {"strife10", "strife1.wad", &Iwad10}
+GameInfo gameList[IWAD_LAST] = {
+    {"doom",       "doom.wad"},
+    {"doom2",      "doom2.wad"},
+    {"heretic",    "heretic.wad"},
+    {"hexen",      "hexen.wad"},
+    {"strife",     "strife1.wad"},
+    {"doom02",     "Doom alpha 0.2 IWAD"},
+    {"doom04",     "Doom alpha 0.4 IWAD"},
+    {"doom05",     "Doom alpha 0.5 IWAD"},
+    {"doompr",     "Doom Press Release IWAD"},
+    {"strife10",   "strife1.wad"}
 };
 
-const int ngameList = sizeof(gameList) / sizeof(gameList[0]);
+
+/*
+ *        parse_environment_vars
+ *        Check certain environment variables.
+ */
+static void parse_environment_vars(void)
+{
+    char *value;
+
+    if ((value = getenv("YADEX_GAME")) != NULL)
+        cfg.Game = value;
+
+    if ((value = getenv("LINES")) != NULL)
+        cfg.screen_lines = atoi(value);
+    else
+        cfg.screen_lines = 0;
+
+    if (cfg.screen_lines == 0)
+        cfg.screen_lines = 24;
+}
 
 
 /*
@@ -201,21 +207,12 @@
 {
     int r;
 
-// Set <screen_lines>
-    if (getenv("LINES") != NULL)
-        screen_lines = atoi(getenv("LINES"));
-    else
-        screen_lines = 0;
-    if (screen_lines == 0)
-        screen_lines = 24;
+    InitializeConfig();
+    parse_environment_vars();
 
-// First detect manually --help and --version
-// because parse_command_line_options() cannot.
     if (argc == 2 && strcmp(argv[1], "--help") == 0)
     {
         print_usage(stdout);
-        if (fflush(stdout) != 0)
-            fatal_error("stdout: %s", strerror(errno));
         exit(0);
     }
     if (argc == 2 && strcmp(argv[1], "--version") == 0)
@@ -223,18 +220,16 @@
         puts(what());
         puts(config_file_magic);
         puts(ygd_file_magic);
-        if (fflush(stdout) != 0)
-            fatal_error("stdout: %s", strerror(errno));
         exit(0);
     }
 
-// Second a quick pass through the command line
-// arguments to detect -?, -f and -help.
+    // Second a quick pass through the command line
+    // arguments to detect -?, -f and -help.
     r = parse_command_line_options(argc - 1, argv + 1, 1);
     if (r)
         goto syntax_error;
 
-    if (show_help)
+    if (cfg.show_help)
     {
         print_usage(stdout);
         exit(1);
@@ -242,26 +237,24 @@
 
     printf("%s\n", what());
 
-// Where am I installed ? (the config file might be there)
+    // Where am I installed ? (the config file might be there)
 #if defined Y_DOS
     install_dir = spec_path(argv[0]);
 #endif
 
-// The config file provides some values.
-    if (config_file != NULL)
-        r = parse_config_file_user(config_file);
+    // The config file provides some values.
+    if (cfg.config_file != NULL)
+        r = parse_config_file_user(cfg.config_file);
     else
         r = parse_config_file_default();
+
     if (r == 0)
     {
         // Environment variables can override them.
-        r = parse_environment_vars();
-        if (r == 0)
-        {
-            // And the command line argument can override both.
-            r = parse_command_line_options(argc - 1, argv + 1, 2);
-        }
+        parse_environment_vars();
+        r = parse_command_line_options(argc - 1, argv + 1, 2);
     }
+
     if (r != 0)
     {
       syntax_error:
@@ -269,15 +262,15 @@
         exit(1);
     }
 
-    if (Game != NULL)
+    if (cfg.Game != NULL)
     {
         int n;
-        for (n = 0; n < ngameList; n++)
+        for (n = 0; n < IWAD_LAST; n++)
         {
             GameInfo *info = &gameList[n];
-            if (strcmp(Game, info->game) == 0)
+            if (strcmp(cfg.Game, info->game) == 0)
             {
-                if (*(info->pwadloc) == NULL)
+                if (cfg.Iwad[n] == NULL)
                 {
                     err("You have to tell me where %s is.", info->wadname);
                     fprintf(stderr,
@@ -285,83 +278,93 @@
                             n + 1, n + 1);
                     exit(1);
                 }
-                MainWad = *(info->pwadloc);
+                cfg.MainWad = cfg.Iwad[n];
             }
         }
     }
 
-    if (MainWad == NULL)
+    if (cfg.MainWad == NULL)
     {
         int n;
-        if (Game == NULL)
+        if (cfg.Game == NULL)
             err("You didn't say for which game you want to edit.");
         else
-            err("Unknown game \"%s\"", Game);
+            err("Unknown game \"%s\"", cfg.Game);
+
         fprintf(stderr,
                 "Use \"-g <game>\" on the command line or put \"game=<game>\" in yadex.cfg\n"
                 "where <game> is one of: ");
-        for (n = 0; n < ngameList; n++)
+
+        for (n = 0; n < IWAD_LAST; n++)
         {
             fprintf(stderr, "%s\"%s\"", n > 0 ? ", " : "", gameList[n].game);
         }
         fprintf(stderr, ".\n");
         exit(1);
     }
-    if (Debug)
+
+    if (cfg.Debug)
     {
-        logfile = fopen(log_file, "a");
-        if (logfile == NULL)
-            warn("can't open log file \"%s\" (%s)", log_file,
+        cfg.logfile = fopen(yadex_log_filename, "a");
+        if (cfg.logfile == NULL)
+        {
+            warn("Can't open log file \"%s\" (%s)", yadex_log_filename,
                  strerror(errno));
+        }
+
+        LogMessage(": Main IWAD = '%s'\n", cfg.MainWad);
         LogMessage(": Welcome to Yadex!\n");
     }
-    if (Quieter)
-        Quiet = true;
 
-// Sanity checks (useful when porting).
+    if (cfg.Quieter)
+        cfg.Quiet = true;
+
+    // Sanity checks (useful when porting).
     check_types();
     check_charset();
 
-// Misc. things done only once.
-    cpu_big_endian = native_endianness();
+    // Misc. things done only once.
+    cfg.cpu_big_endian = native_endianness();
     add_base_colours();
 
-// Load game definitions (*.ygd).
+    // Load game definitions (*.ygd).
     InitGameDefs();
-    LoadGameDefs(Game);
+    LoadGameDefs(cfg.Game);
 
-// Load the iwad and the pwads.
-    if (OpenMainWad(MainWad))
+    // Load the iwad and the pwads.
+    if (OpenMainWad(cfg.MainWad))
         fatal_error("If you don't give me an iwad, I'll quit. I'm serious.");
-    if (PatchWads)
+
+    if (cfg.PatchWads)
     {
         const char *const *pwad_name;
-        for (pwad_name = PatchWads; *pwad_name; pwad_name++)
+        for (pwad_name = cfg.PatchWads; *pwad_name; pwad_name++)
             OpenPatchWad(*pwad_name);
     }
-/* sanity check */
+
+    /* sanity check */
     CloseUnusedWadFiles();
 
-// BRANCH 1 : benchmarking (-b)
-    if (bench != 0)
+    // BRANCH 1 : benchmarking (-b)
+    if (cfg.bench != NULL)
     {
-        benchmark(bench);
+        benchmark(cfg.bench);
         return 0;                // Exit successfully
     }
 
 // BRANCH 2 : normal use ("yadex:" prompt)
     else
     {
-        if (welcome_message)
+        if (cfg.welcome_message)
             print_welcome(stdout);
 
-        if (strcmp(Game, "hexen") == 0)
+        if (strcmp(cfg.Game, "hexen") == 0)
             printf
                 ("WARNING: Hexen mode is experimental. Don't expect to be able to do any\n"
                  "real Hexen editing with it. You can edit levels but you can't save them.\n"
                  "And there might be other bugs... BE CAREFUL !\n\n");
 
-        if (strcmp(Game, "strife") == 0)
+        if (strcmp(cfg.Game, "strife") == 0)
             printf
                 ("WARNING: Strife mode is experimental. Many thing types, linedef types,\n"
                  "etc. are missing or wrong. And be careful, there might be bugs.\n\n");
@@ -374,9 +377,11 @@
     CloseWadFiles();
     FreeGameDefs();
     LogMessage(": The end!\n\n\n");
-    if (logfile != NULL)
-        fclose(logfile);
-    if (remind_to_build_nodes)
+
+    if (cfg.logfile != NULL)
+        fclose(cfg.logfile);
+
+    if (cfg.remind_to_build_nodes)
         printf("\n"
                "** You have made changes to one or more wads. Don't forget to pass\n"
                "** them through a nodes builder (E.G. BSP) before running them.\n"
@@ -386,22 +391,6 @@
 
 
 /*
- *        parse_environment_vars
- *        Check certain environment variables.
- *        Returns 0 on success, <>0 on error.
- */
-static int parse_environment_vars()
-{
-    char *value;
-
-    value = getenv("YADEX_GAME");
-    if (value != NULL)
-        Game = value;
-    return 0;
-}
-
-
-/*
    play a fascinating tune
 */
 
@@ -433,6 +422,28 @@
 
 
 /*
+ *        print_error_message
+ *        Print an error message to stderr.
+ */
+static void print_error_message(const char *fmt, va_list args)
+{
+    fflush(stdout);
+    fputs("Error: ", stderr);
+    vfprintf(stderr, fmt, args);
+    fputc('\n', stderr);
+    fflush(stderr);
+
+    if (cfg.Debug && cfg.logfile != NULL)
+    {
+        fputs("Error: ", cfg.logfile);
+        vfprintf(cfg.logfile, fmt, args);
+        fputc('\n', cfg.logfile);
+        fflush(cfg.logfile);
+    }
+}
+
+
+/*
  *        fatal_error
  *        Print an error message and terminate the program with code 2.
  */
@@ -471,27 +482,6 @@
 
 
 /*
- *        print_error_message
- *        Print an error message to stderr.
- */
-static void print_error_message(const char *fmt, va_list args)
-{
-    fflush(stdout);
-    fputs("Error: ", stderr);
-    vfprintf(stderr, fmt, args);
-    fputc('\n', stderr);
-    fflush(stderr);
-    if (Debug && logfile != NULL)
-    {
-        fputs("Error: ", logfile);
-        vfprintf(logfile, fmt, args);
-        fputc('\n', logfile);
-        fflush(logfile);
-    }
-}
-
-
-/*
  *        nf_bug
  *        Report about a non-fatal bug to stderr. The message
  *        should not expand to more than 80 characters.
@@ -551,7 +541,7 @@
     time_t tval;
     char *tstr;
 
-    if (Debug && logfile != NULL)
+    if (cfg.Debug && cfg.logfile != NULL)
     {
         va_start(args, logstr);
         /* if the message begins with ":", output the current date & time first */
@@ -560,10 +550,10 @@
             time(&tval);
             tstr = ctime(&tval);
             tstr[strlen(tstr) - 1] = '\0';
-            fprintf(logfile, "%s", tstr);
+            fprintf(cfg.logfile, "%s", tstr);
         }
-        vfprintf(logfile, logstr, args);
-        fflush(logfile);        /* AYM 19971031 */
+        vfprintf(cfg.logfile, logstr, args);
+        fflush(cfg.logfile);        /* AYM 19971031 */
     }
 }
 
@@ -684,7 +674,7 @@
         /* user asked to quit */
         else if (!strcmp(com, "quit") || !strcmp(com, "q"))
         {
-            if (!Registered)
+            if (!cfg.Registered)
                 printf("Remember to register your copy of the game !\n");
             break;
         }
@@ -707,7 +697,7 @@
             }
             else
             {
-                MDirPtr entry = FindMasterDir(MasterDir, com);
+                MDirPtr entry = FindMasterDir(cfg.MasterDir, com);
                 if (entry != NULL)
                 {
                     level_name = strdup(entry->dir.name);
@@ -719,10 +709,10 @@
                     // Hint absent-minded users
                     if ((tolower(*com) == 'e' && yg_level_name == YGLN_MAP01) ||
                         (tolower(*com) == 'm' && yg_level_name == YGLN_E1M1))
-                        printf(" You are in %s mode.", Game);
+                        printf(" You are in %s mode.", cfg.Game);
                     else
 
-                    if (tolower(*com) == 'e' && com[1] > '1' && !Registered)
+                    if (tolower(*com) == 'e' && com[1] > '1' && !cfg.Registered)
                         printf(" You have the shareware IWAD.");
 
                     printf("\n");
@@ -997,7 +987,7 @@
             while (is.key != YE_EXPOSE);
             com = strtok(NULL, " ");
             force_window_not_pixmap();        // FIXME quick hack
-            patch_dir.refresh(MasterDir);
+            patch_dir.refresh(cfg.MasterDir);
             {
                 char buf[WAD_NAME + 1];
                 *buf = '\0';
@@ -1025,7 +1015,7 @@
             while (is.key != YE_EXPOSE);
             com = strtok(NULL, " ");
             force_window_not_pixmap();        // FIXME quick hack
-            patch_dir.refresh(MasterDir);
+            patch_dir.refresh(cfg.MasterDir);
             {
                 char buf[WAD_TEX_NAME + 1];
                 *buf = '\0';