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.
This commit is contained in:
Aaron Traas 2017-07-02 02:46:04 -04:00 committed by Eric S. Raymond
parent d23111daba
commit 7eaefce61d
5 changed files with 31 additions and 25 deletions

View file

@ -163,6 +163,16 @@ struct game_t {
long prop[NOBJECTS + 1]; 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 { struct command_t {
enum speechpart part; enum speechpart part;
vocab_t verb; vocab_t verb;
@ -172,8 +182,7 @@ struct command_t {
}; };
extern struct game_t game; extern struct game_t game;
extern FILE *logfp; extern struct settings_t settings;
extern bool oldstyle, prompt;
extern char* xstrdup(const char* s); extern char* xstrdup(const char* s);
extern void* xmalloc(size_t size); extern void* xmalloc(size_t size);
@ -218,7 +227,6 @@ extern int restore(FILE *);
extern long initialise(void); extern long initialise(void);
extern int action(struct command_t *command); extern int action(struct command_t *command);
/* Alas, declaring this static confuses the coverage analyzer */
void bug(enum bugtype, const char *) __attribute__((__noreturn__)); void bug(enum bugtype, const char *) __attribute__((__noreturn__));
/* end */ /* end */

View file

@ -10,10 +10,6 @@
#include <stdbool.h> #include <stdbool.h>
#include "advent.h" #include "advent.h"
FILE *logfp = NULL;
bool oldstyle = false;
bool prompt = true;
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int ch; int ch;

8
init.c
View file

@ -10,6 +10,12 @@
* Initialisation * Initialisation
*/ */
struct settings_t settings = {
.logfp = NULL,
.oldstyle = false,
.prompt = true
};
struct game_t game = { struct game_t game = {
.dloc[1] = LOC_KINGHALL, .dloc[1] = LOC_KINGHALL,
.dloc[2] = LOC_WESTBANK, .dloc[2] = LOC_WESTBANK,
@ -41,7 +47,7 @@ struct game_t game = {
long initialise(void) long initialise(void)
{ {
if (oldstyle) if (settings.oldstyle)
printf("Initialising...\n"); printf("Initialising...\n");
srand(time(NULL)); srand(time(NULL));

20
main.c
View file

@ -25,17 +25,13 @@
#define DIM(a) (sizeof(a)/sizeof(a[0])) #define DIM(a) (sizeof(a)/sizeof(a[0]))
FILE *logfp = NULL;
bool oldstyle = false;
bool prompt = true;
// LCOV_EXCL_START // LCOV_EXCL_START
// exclude from coverage analysis because it requires interactivity to test // exclude from coverage analysis because it requires interactivity to test
static void sig_handler(int signo) static void sig_handler(int signo)
{ {
if (signo == SIGINT) { if (signo == SIGINT) {
if (logfp != NULL) if (settings.logfp != NULL)
fflush(logfp); fflush(settings.logfp);
} }
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -71,16 +67,16 @@ int main(int argc, char *argv[])
while ((ch = getopt(argc, argv, opts)) != EOF) { while ((ch = getopt(argc, argv, opts)) != EOF) {
switch (ch) { switch (ch) {
case 'l': case 'l':
logfp = fopen(optarg, "w"); settings.logfp = fopen(optarg, "w");
if (logfp == NULL) if (settings.logfp == NULL)
fprintf(stderr, fprintf(stderr,
"advent: can't open logfile %s for write\n", "advent: can't open logfile %s for write\n",
optarg); optarg);
signal(SIGINT, sig_handler); signal(SIGINT, sig_handler);
break; break;
case 'o': case 'o':
oldstyle = true; settings.oldstyle = true;
prompt = false; settings.prompt = false;
break; break;
#ifndef ADVENT_NOSAVE #ifndef ADVENT_NOSAVE
case 'r': case 'r':
@ -121,8 +117,8 @@ int main(int argc, char *argv[])
} }
#endif #endif
if (logfp) if (settings.logfp)
fprintf(logfp, "seed %ld\n", seedval); fprintf(settings.logfp, "seed %ld\n", seedval);
/* interpret commands until EOF or interrupt */ /* interpret commands until EOF or interrupt */
for (;;) { for (;;) {

10
misc.c
View file

@ -325,7 +325,7 @@ char* get_input()
{ {
// Set up the prompt // Set up the prompt
char input_prompt[] = "> "; char input_prompt[] = "> ";
if (!prompt) if (!settings.prompt)
input_prompt[0] = '\0'; input_prompt[0] = '\0';
// Print a blank line if game.blklin tells us to. // Print a blank line if game.blklin tells us to.
@ -353,8 +353,8 @@ char* get_input()
if (!isatty(0)) if (!isatty(0))
echo_input(stdout, input_prompt, input); echo_input(stdout, input_prompt, input);
if (logfp) if (settings.logfp)
echo_input(logfp, "", input); echo_input(settings.logfp, "", input);
return (input); return (input);
} }
@ -459,7 +459,7 @@ int get_motion_vocab_id(const char* word)
{ {
for (int i = 0; i < NMOTIONS; ++i) { for (int i = 0; i < NMOTIONS; ++i) {
for (int j = 0; j < motions[i].words.n; ++j) { 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); return (i);
} }
} }
@ -485,7 +485,7 @@ int get_action_vocab_id(const char* word)
{ {
for (int i = 0; i < NACTIONS; ++i) { for (int i = 0; i < NACTIONS; ++i) {
for (int j = 0; j < actions[i].words.n; ++j) { 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); return (i);
} }
} }