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];
};
/*
* 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 */

View file

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

8
init.c
View file

@ -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));

20
main.c
View file

@ -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 (;;) {

10
misc.c
View file

@ -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);
}
}