From: Aaron Traas Date: Sun, 2 Jul 2017 06:46:04 +0000 (-0400) Subject: Moved application settings to settings_t struct X-Git-Tag: takebird~107 X-Git-Url: https://jxself.org/git/?a=commitdiff_plain;h=7eaefce61d74fbc73daabd3f42f048038366b5ad;p=open-adventure.git Moved application settings to settings_t struct Since logfp, oldstyle, and prompt were application settings, rather than have them all as global vars, move them to a single global var, seperate from game state, as they aren't, technically, game state, but are application settings. --- diff --git a/advent.h b/advent.h index a8d53f2..a7f5ac4 100644 --- a/advent.h +++ b/advent.h @@ -163,6 +163,16 @@ struct game_t { long prop[NOBJECTS + 1]; }; +/* + * Game application settings - settings, but not state of the game, per se. + * This data is not saved in a saved game. + */ +struct settings_t { + FILE *logfp; + bool oldstyle; + bool prompt; +}; + struct command_t { enum speechpart part; vocab_t verb; @@ -172,8 +182,7 @@ struct command_t { }; extern struct game_t game; -extern FILE *logfp; -extern bool oldstyle, prompt; +extern struct settings_t settings; extern char* xstrdup(const char* s); extern void* xmalloc(size_t size); @@ -218,7 +227,6 @@ extern int restore(FILE *); extern long initialise(void); extern int action(struct command_t *command); -/* Alas, declaring this static confuses the coverage analyzer */ void bug(enum bugtype, const char *) __attribute__((__noreturn__)); /* end */ diff --git a/cheat.c b/cheat.c index f62dba9..f9a34d4 100644 --- a/cheat.c +++ b/cheat.c @@ -10,10 +10,6 @@ #include #include "advent.h" -FILE *logfp = NULL; -bool oldstyle = false; -bool prompt = true; - int main(int argc, char *argv[]) { int ch; diff --git a/init.c b/init.c index 812e943..5372714 100644 --- a/init.c +++ b/init.c @@ -10,6 +10,12 @@ * Initialisation */ +struct settings_t settings = { + .logfp = NULL, + .oldstyle = false, + .prompt = true +}; + struct game_t game = { .dloc[1] = LOC_KINGHALL, .dloc[2] = LOC_WESTBANK, @@ -41,7 +47,7 @@ struct game_t game = { long initialise(void) { - if (oldstyle) + if (settings.oldstyle) printf("Initialising...\n"); srand(time(NULL)); diff --git a/main.c b/main.c index cf5d396..1269efa 100644 --- a/main.c +++ b/main.c @@ -25,17 +25,13 @@ #define DIM(a) (sizeof(a)/sizeof(a[0])) -FILE *logfp = NULL; -bool oldstyle = false; -bool prompt = true; - // LCOV_EXCL_START // exclude from coverage analysis because it requires interactivity to test static void sig_handler(int signo) { if (signo == SIGINT) { - if (logfp != NULL) - fflush(logfp); + if (settings.logfp != NULL) + fflush(settings.logfp); } exit(EXIT_FAILURE); } @@ -71,16 +67,16 @@ int main(int argc, char *argv[]) while ((ch = getopt(argc, argv, opts)) != EOF) { switch (ch) { case 'l': - logfp = fopen(optarg, "w"); - if (logfp == NULL) + settings.logfp = fopen(optarg, "w"); + if (settings.logfp == NULL) fprintf(stderr, "advent: can't open logfile %s for write\n", optarg); signal(SIGINT, sig_handler); break; case 'o': - oldstyle = true; - prompt = false; + settings.oldstyle = true; + settings.prompt = false; break; #ifndef ADVENT_NOSAVE case 'r': @@ -121,8 +117,8 @@ int main(int argc, char *argv[]) } #endif - if (logfp) - fprintf(logfp, "seed %ld\n", seedval); + if (settings.logfp) + fprintf(settings.logfp, "seed %ld\n", seedval); /* interpret commands until EOF or interrupt */ for (;;) { diff --git a/misc.c b/misc.c index 51760b5..5e76921 100644 --- a/misc.c +++ b/misc.c @@ -325,7 +325,7 @@ char* get_input() { // Set up the prompt char input_prompt[] = "> "; - if (!prompt) + if (!settings.prompt) input_prompt[0] = '\0'; // Print a blank line if game.blklin tells us to. @@ -353,8 +353,8 @@ char* get_input() if (!isatty(0)) echo_input(stdout, input_prompt, input); - if (logfp) - echo_input(logfp, "", input); + if (settings.logfp) + echo_input(settings.logfp, "", input); return (input); } @@ -459,7 +459,7 @@ int get_motion_vocab_id(const char* word) { for (int i = 0; i < NMOTIONS; ++i) { for (int j = 0; j < motions[i].words.n; ++j) { - if (strcasecmp(word, motions[i].words.strs[j]) == 0 && (strlen(word) > 1 || strchr(ignore, word[0]) == NULL || !oldstyle)) + if (strcasecmp(word, motions[i].words.strs[j]) == 0 && (strlen(word) > 1 || strchr(ignore, word[0]) == NULL || !settings.oldstyle)) return (i); } } @@ -485,7 +485,7 @@ int get_action_vocab_id(const char* word) { for (int i = 0; i < NACTIONS; ++i) { for (int j = 0; j < actions[i].words.n; ++j) { - if (strcasecmp(word, actions[i].words.strs[j]) == 0 && (strlen(word) > 1 || strchr(ignore, word[0]) == NULL || !oldstyle)) + if (strcasecmp(word, actions[i].words.strs[j]) == 0 && (strlen(word) > 1 || strchr(ignore, word[0]) == NULL || !settings.oldstyle)) return (i); } }