From: David Griffith Date: Tue, 10 Jul 2012 02:16:56 +0000 (-0700) Subject: converted various global vars to use struct f_setup instead X-Git-Url: https://scope-eye.net/git/?a=commitdiff_plain;h=fcd5acd2c3017596312c1393a1e8482ba553de99;p=liskon_frotz.git converted various global vars to use struct f_setup instead --- diff --git a/src/common/fastmem.c b/src/common/fastmem.c index 081c360..4c295a1 100644 --- a/src/common/fastmem.c +++ b/src/common/fastmem.c @@ -70,7 +70,7 @@ extern void (*op1_opcodes[]) (void); extern void (*op2_opcodes[]) (void); extern void (*var_opcodes[]) (void); -char save_name[MAX_FILE_NAME + 1] = DEFAULT_SAVE_NAME; +/* char save_name[MAX_FILE_NAME + 1] = DEFAULT_SAVE_NAME; */ char auxilary_name[MAX_FILE_NAME + 1] = DEFAULT_AUXILARY_NAME; zbyte far *zmp = NULL; @@ -250,7 +250,7 @@ void init_memory (void) /* Open story file */ - if ((story_fp = os_path_open(story_name, "rb")) == NULL) + if ((story_fp = os_path_open(f_setup.story_file, "rb")) == NULL) os_fatal ("Cannot open story file"); /* Allocate memory for story header */ @@ -597,7 +597,7 @@ static void get_default_name (char *default_name, zword addr) if (strchr (default_name, '.') == NULL) strcpy (default_name + i, ".AUX"); - } else strcpy (default_name, auxilary_name); + } else strcpy (default_name, f_setup.aux_name); }/* get_default_name */ @@ -627,7 +627,7 @@ void z_restore (void) if (os_read_file_name (new_name, default_name, FILE_LOAD_AUX) == 0) goto finished; - strcpy (auxilary_name, default_name); + strcpy (f_setup.aux_name, default_name); /* Open auxilary file */ @@ -651,10 +651,10 @@ void z_restore (void) /* Get the file name */ - if (os_read_file_name (new_name, save_name, FILE_RESTORE) == 0) + if (os_read_file_name (new_name, f_setup.save_name, FILE_RESTORE) == 0) goto finished; - strcpy (save_name, new_name); + strcpy (f_setup.save_name, new_name); /* Open game file */ @@ -918,7 +918,7 @@ void z_save (void) if (os_read_file_name (new_name, default_name, FILE_SAVE_AUX) == 0) goto finished; - strcpy (auxilary_name, default_name); + strcpy (f_setup.aux_name, default_name); /* Open auxilary file */ @@ -943,10 +943,10 @@ void z_save (void) /* Get the file name */ - if (os_read_file_name (new_name, save_name, FILE_SAVE) == 0) + if (os_read_file_name (new_name, f_setup.save_name, FILE_SAVE) == 0) goto finished; - strcpy (save_name, new_name); + strcpy (f_setup.save_name, new_name); /* Open game file */ diff --git a/src/common/files.c b/src/common/files.c index 06aa1bd..c32317a 100644 --- a/src/common/files.c +++ b/src/common/files.c @@ -34,8 +34,8 @@ extern bool is_terminator (zchar); extern bool read_yes_or_no (const char *); -char script_name[MAX_FILE_NAME + 1] = DEFAULT_SCRIPT_NAME; -char command_name[MAX_FILE_NAME + 1] = DEFAULT_COMMAND_NAME; +/* char script_name[MAX_FILE_NAME + 1] = DEFAULT_SCRIPT_NAME; */ +/* char command_name[MAX_FILE_NAME + 1] = DEFAULT_COMMAND_NAME; */ #ifdef __MSDOS__ extern char latin1_to_ibm[]; @@ -71,16 +71,17 @@ void script_open (void) if (h_version >= V5 || !script_valid) { - if (!os_read_file_name (new_name, script_name, FILE_SCRIPT)) + if (!os_read_file_name (new_name, f_setup.script_name, FILE_SCRIPT)) goto done; - strcpy (script_name, new_name); + strcpy (f_setup.script_name, new_name); } /* Opening in "at" mode doesn't work for script_erase_input... */ - if ((sfp = fopen (script_name, "r+t")) != NULL || (sfp = fopen (script_name, "w+t")) != NULL) { + if ((sfp = fopen (f_setup.script_name, "r+t")) != NULL || + (sfp = fopen (f_setup.script_name, "w+t")) != NULL) { fseek (sfp, 0, SEEK_END); @@ -291,9 +292,9 @@ void record_open (void) { char new_name[MAX_FILE_NAME + 1]; - if (os_read_file_name (new_name, command_name, FILE_RECORD)) { + if (os_read_file_name (new_name, f_setup.command_name, FILE_RECORD)) { - strcpy (command_name, new_name); + strcpy (f_setup.command_name, new_name); if ((rfp = fopen (new_name, "wt")) != NULL) ostream_record = TRUE; @@ -415,9 +416,9 @@ void replay_open (void) { char new_name[MAX_FILE_NAME + 1]; - if (os_read_file_name (new_name, command_name, FILE_PLAYBACK)) { + if (os_read_file_name (new_name, f_setup.command_name, FILE_PLAYBACK)) { - strcpy (command_name, new_name); + strcpy (f_setup.command_name, new_name); if ((pfp = fopen (new_name, "rt")) != NULL) { diff --git a/src/common/frotz.h b/src/common/frotz.h index fd5d781..f41125a 100644 --- a/src/common/frotz.h +++ b/src/common/frotz.h @@ -456,8 +456,6 @@ extern zword hx_back_colour; /*** Various data ***/ -extern char *story_name; - extern enum story story_id; extern long story_size; diff --git a/src/common/setup.h b/src/common/setup.h index 28e0d25..7fd7db1 100644 --- a/src/common/setup.h +++ b/src/common/setup.h @@ -20,6 +20,16 @@ typedef struct frotz_setup_struct { int save_quetzal; /* done */ int sound; /* done */ int err_report_mode; /* done */ + + char *story_file; + char *story_name; + char *story_base; + char *script_name; + char *command_name; + char *save_name; + char *aux_name; + char *story_path; + char *zcode_path; } f_setup_t; extern f_setup_t f_setup; diff --git a/src/curses/ux_audio_oss.c b/src/curses/ux_audio_oss.c index bccb7b5..db66c8c 100644 --- a/src/curses/ux_audio_oss.c +++ b/src/curses/ux_audio_oss.c @@ -209,18 +209,18 @@ void os_prepare_sample (int number) free(sound_buffer); sound_buffer = NULL; - filename = malloc(strlen(story_name) + 10); + filename = malloc(strlen(f_setup.story_name) + 10); if (! filename) return; - basename = strrchr(story_name, '/'); - if (basename) basename++; else basename = story_name; + basename = strrchr(f_setup.story_name, '/'); + if (basename) basename++; else basename = f_setup.story_name; dotpos = strrchr(basename, '.'); namelen = (dotpos ? dotpos - basename : strlen(basename)); if (namelen > 6) namelen = 6; sprintf(filename, "%.*ssound/%.*s%02d.snd", - basename - story_name, story_name, + basename - f_setup.story_name, f_setup.story_name, namelen, basename, number); diff --git a/src/curses/ux_file.c b/src/curses/ux_file.c index cfb5202..6dc43a4 100644 --- a/src/curses/ux_file.c +++ b/src/curses/ux_file.c @@ -61,9 +61,9 @@ FILE *os_path_open(const char *name, const char *mode) /* If zcodepath is defined in a config file, check that path. */ /* If we find the file a match in that path, great. */ /* Otherwise, check some environmental variables. */ - if (option_zcode_path != NULL) { - if ((fp = pathopen(name, option_zcode_path, mode, buf)) != NULL) { - strncpy(story_name, buf, FILENAME_MAX); + if (f_setup.zcode_path != NULL) { + if ((fp = pathopen(name, f_setup.zcode_path, mode, buf)) != NULL) { + strncpy(f_setup.story_name, buf, FILENAME_MAX); return fp; } } @@ -73,7 +73,7 @@ FILE *os_path_open(const char *name, const char *mode) if (p != NULL) { fp = pathopen(name, p, mode, buf); - strncpy(story_name, buf, FILENAME_MAX); + strncpy(f_setup.story_name, buf, FILENAME_MAX); return fp; } return NULL; /* give up */ diff --git a/src/curses/ux_init.c b/src/curses/ux_init.c index 5e0584e..6ad9e7e 100644 --- a/src/curses/ux_init.c +++ b/src/curses/ux_init.c @@ -105,10 +105,10 @@ void os_fatal (const char *s, ...) }/* os_fatal */ -extern char script_name[]; -extern char command_name[]; -extern char save_name[]; -extern char auxilary_name[]; +/* extern char script_name[]; */ +/* extern char command_name[]; */ +/* extern char save_name[];*/ +/*extern char auxilary_name[];*/ /* * os_process_arguments @@ -278,38 +278,60 @@ void os_process_arguments (int argc, char *argv[]) /* Save the story file name */ - story_name = malloc(FILENAME_MAX + 1); - strcpy(story_name, argv[optind]); + f_setup.story_file = strdup(argv[optind]); + f_setup.story_name = strdup((char *)basename(argv[optind])); - /* Strip path off the story file name */ + /* Now strip off the extension. */ + p = rindex(f_setup.story_name, '.'); - p = (char *)story_name; - - for (i = 0; story_name[i] != 0; i++) - if (story_name[i] == '/') - p = (char *)story_name + i + 1; - - for (i = 0; p[i] != '\0'; i++) - semi_stripped_story_name[i] = p[i]; - semi_stripped_story_name[i] = '\0'; + /* Get rid of extensions with 1 to 6 character extensions. */ + /* This will take care of an extension like ".zblorb". */ + /* More than that, there might be something weird going on */ + /* which is not our concern. */ + if (p != NULL) { + if (strlen(p) >= 2 && strlen(p) <= 7) { + *p = '\0'; /* extension removed */ + } + } - for (i = 0; p[i] != '\0' && p[i] != '.'; i++) - stripped_story_name[i] = p[i]; - stripped_story_name[i] = '\0'; + f_setup.story_path = strdup((char *)dirname(argv[optind])); /* Create nice default file names */ - strcpy (script_name, stripped_story_name); - strcpy (command_name, stripped_story_name); - strcpy (save_name, stripped_story_name); - strcpy (auxilary_name, stripped_story_name); - - /* Don't forget the extensions */ - - strcat (script_name, ".scr"); - strcat (command_name, ".rec"); - strcat (save_name, ".sav"); - strcat (auxilary_name, ".aux"); + u_setup.blorb_name = malloc(strlen(f_setup.story_name) * sizeof(char) + 5); + strncpy(u_setup.blorb_name, f_setup.story_name, + strlen(f_setup.story_name) +1); + strncat(u_setup.blorb_name, EXT_BLORB, strlen(EXT_BLORB)); + + u_setup.blorb_file = malloc(strlen(f_setup.story_path) * + sizeof(char) + strlen(u_setup.blorb_name) * sizeof(char) + 4); + strncpy(u_setup.blorb_file, f_setup.story_path, + strlen(f_setup.story_path)); + strncat(u_setup.blorb_file, "/", 1); + strncat(u_setup.blorb_file, u_setup.blorb_name, + strlen(u_setup.blorb_name) + 1); + + f_setup.script_name = malloc(strlen(f_setup.story_name) * sizeof(char) + 5); + strncpy(f_setup.script_name, f_setup.story_name, strlen(f_setup.story_name)); + strncat(f_setup.script_name, EXT_SCRIPT, strlen(EXT_SCRIPT)); + + f_setup.command_name = malloc(strlen(f_setup.story_name) * sizeof(char) + 5); + strncpy(f_setup.command_name, f_setup.story_name, strlen(f_setup.story_name)); + strncat(f_setup.command_name, EXT_COMMAND, strlen(EXT_COMMAND)); + + f_setup.save_name = malloc(strlen(f_setup.story_name) * sizeof(char) + 5); + strncpy(f_setup.save_name, f_setup.story_name, strlen(f_setup.story_name)); + strncat(f_setup.save_name, EXT_SAVE, strlen(EXT_SAVE)); + + f_setup.aux_name = malloc(strlen(f_setup.story_name) * sizeof(char) + 5); + strncpy(f_setup.aux_name, f_setup.story_name, strlen(f_setup.story_name)); + strncat(f_setup.aux_name, EXT_AUX, strlen(EXT_AUX)); + + switch (ux_init_blorb()) { + case bb_err_Format: + printf("Blorb file loaded, but unable to build map.\n\n"); + break; + } }/* os_process_arguments */ @@ -346,8 +368,12 @@ void os_init_screen (void) { /*trace(TRACE_CALLS);*/ + + if (initscr() == NULL) { /* Set up curses */ + os_fatal("Unable to initialize curses. Maybe your $TERM setting is bad."); + exit(1); + } u_setup.curses_active = 1; /* Let os_fatal know curses is running */ - initscr(); /* Set up curses */ cbreak(); /* Raw input mode, no line processing */ noecho(); /* No input echo */ nonl(); /* No newline translation */ @@ -443,7 +469,7 @@ void os_init_screen (void) (u_setup.background_color ==-1) ? BACKGROUND_DEF : u_setup.background_color; } else -#endif +#endif /* COLOR_SUPPORT */ { /* Set these per spec 8.3.2. */ h_default_foreground = WHITE_COLOUR; @@ -532,9 +558,9 @@ FILE *os_path_open(const char *name, const char *mode) /* If zcodepath is defined in a config file, check that path. */ /* If we find the file a match in that path, great. */ /* Otherwise, check some environmental variables. */ - if (option_zcode_path != NULL) { - if ((fp = pathopen(name, option_zcode_path, mode, buf)) != NULL) { - strncpy(story_name, buf, FILENAME_MAX); + if (f_setup.zcode_path != NULL) { + if ((fp = pathopen(name, f_setup.zcode_path, mode, buf)) != NULL) { + strncpy(f_setup.story_name, buf, FILENAME_MAX); return fp; } } @@ -544,7 +570,7 @@ FILE *os_path_open(const char *name, const char *mode) if (p != NULL) { fp = pathopen(name, p, mode, buf); - strncpy(story_name, buf, FILENAME_MAX); + strncpy(f_setup.story_name, buf, FILENAME_MAX); return fp; } return NULL; /* give up */ @@ -571,7 +597,7 @@ FILE *os_load_story(void) fp = fopen(u_setup.blorb_file, "rb"); fseek(fp, blorb_res.data.startpos, SEEK_SET); } else { - fp = fopen(story_name, "rb"); + fp = fopen(f_setup.story_name, "rb"); } return fp; } @@ -760,8 +786,8 @@ int getconfig(char *configfile) /* now for really stringtype variable */ else if (strcmp(varname, "zcode_path") == 0) { - option_zcode_path = malloc(strlen(value) * sizeof(char) + 1); - strncpy(option_zcode_path, value, strlen(value) * sizeof(char)); + f_setup.zcode_path = malloc(strlen(value) * sizeof(char) + 1); + strncpy(f_setup.zcode_path, value, strlen(value) * sizeof(char)); } /* The big nasty if-else thingy is finished */ @@ -934,6 +960,9 @@ void os_init_setup(void) f_setup.sound = 1; f_setup.err_report_mode = ERR_DEFAULT_REPORT_MODE; + u_setup.use_blorb = 0; + u_setup.exec_in_blorb = 0; + u_setup.disable_color = 0; u_setup.force_color = 0; u_setup.foreground_color = -1; @@ -957,3 +986,33 @@ void os_init_setup(void) } +int ux_init_blorb(void) +{ + FILE *blorbfile; + + /* If the filename given on the command line is the same as our + * computed blorb filename, then we will assume the executable + * is contained in the blorb file. + */ + + if (strncmp((char *)basename(f_setup.story_file), + (char *)basename(u_setup.blorb_file), 55) == 0) { + if ((blorbfile = fopen(u_setup.blorb_file, "rb")) == NULL) + return bb_err_Read; + blorb_err = bb_create_map(blorbfile, &blorb_map); + if (blorb_err != bb_err_None) + return bb_err_Format; + + /* Now we need to locate the EXEC chunk within the blorb file + * and present it to the rest of the program as a file stream. + */ + + blorb_err = bb_load_chunk_by_type(blorb_map, bb_method_FilePos, + &blorb_res, bb_make_id('Z','C','O','D'), 0); + + if (blorb_err == bb_err_None) { + u_setup.exec_in_blorb = 1; + u_setup.use_blorb = 1; + } + } +} diff --git a/src/curses/ux_pic.c b/src/curses/ux_pic.c index ebfe08d..451a620 100644 --- a/src/curses/ux_pic.c +++ b/src/curses/ux_pic.c @@ -89,15 +89,15 @@ bool unix_init_pictures (void) const char *basename, *dotpos; int namelen; - if ((filename = malloc(2 * strlen(story_name) + 10)) == NULL) + if ((filename = malloc(2 * strlen(f_setup.story_name) + 10)) == NULL) return FALSE; - basename = strrchr(story_name, '/'); - if (basename) basename++; else basename = story_name; + basename = strrchr(f_setup.story_name, '/'); + if (basename) basename++; else basename = f_setup.story_name; dotpos = strrchr(basename, '.'); namelen = (dotpos ? dotpos - basename : strlen(basename)); sprintf(filename, "%.*sgraphics/%.*s.mg1", - basename - story_name, story_name, namelen, basename); + basename - f_setup.story_name, f_setup.story_name, namelen, basename); do { int i, entry_size, flags, x_scale, y_scale;