X-Git-Url: https://jxself.org/git/?a=blobdiff_plain;f=src%2Ffiles.c;h=06e85d07031e821b8abc1de0ec7a8c94bae3ab0e;hb=8e63120c630c94c598d4e2d6ba823dac59bce8fa;hp=b027dd40f97916e74b90e2a8d28bf2279fa72189;hpb=d11f2f726ed7feea617476d99cf7505ddd9a27ce;p=inform.git diff --git a/src/files.c b/src/files.c index b027dd4..06e85d0 100644 --- a/src/files.c +++ b/src/files.c @@ -7,8 +7,8 @@ /* routines in "inform.c", since they are tied up with ICL */ /* settings and are very host OS-dependent. */ /* */ -/* Part of Inform 6.35 */ -/* copyright (c) Graham Nelson 1993 - 2021 */ +/* Part of Inform 6.40 */ +/* copyright (c) Graham Nelson 1993 - 2022 */ /* */ /* Inform is free software: you can redistribute it and/or modify */ /* it under the terms of the GNU General Public License as published by */ @@ -48,10 +48,9 @@ static int checksum_count; /* similarly */ /* level is only concerned with file names and handles. */ /* ------------------------------------------------------------------------- */ -FileId *InputFiles=NULL; /* Ids for all the source files */ -static char *filename_storage, /* Translated filenames */ - *filename_storage_p; -static int filename_storage_left; +FileId *InputFiles=NULL; /* Ids for all the source files + Allocated to total_files */ +static memory_list InputFiles_memlist; /* ------------------------------------------------------------------------- */ /* When emitting debug information, we won't have addresses of routines, */ @@ -87,13 +86,6 @@ static debug_backpatch_accumulator global_backpatch_accumulator; static debug_backpatch_accumulator array_backpatch_accumulator; static debug_backpatch_accumulator grammar_backpatch_accumulator; -/* ------------------------------------------------------------------------- */ -/* File handles and names for temporary files. */ -/* ------------------------------------------------------------------------- */ - -FILE *Temp1_fp=NULL, *Temp2_fp=NULL, *Temp3_fp=NULL; -char Temp1_Name[PATHLEN], Temp2_Name[PATHLEN], Temp3_Name[PATHLEN]; - /* ------------------------------------------------------------------------- */ /* Opening and closing source code files */ /* ------------------------------------------------------------------------- */ @@ -118,8 +110,7 @@ extern void load_sourcefile(char *filename_given, int same_directory_flag) int x = 0; FILE *handle; - if (total_files == MAX_SOURCE_FILES) - memoryerror("MAX_SOURCE_FILES", MAX_SOURCE_FILES); + ensure_memory_list_available(&InputFiles_memlist, total_files+1); do { x = translate_in_filename(x, name, filename_given, same_directory_flag, @@ -127,14 +118,8 @@ extern void load_sourcefile(char *filename_given, int same_directory_flag) handle = fopen(name,"r"); } while ((handle == NULL) && (x != 0)); - if (filename_storage_left <= (int)strlen(name)) - memoryerror("MAX_SOURCE_FILES", MAX_SOURCE_FILES); - - filename_storage_left -= strlen(name)+1; - strcpy(filename_storage_p, name); - InputFiles[total_files].filename = filename_storage_p; - - filename_storage_p += strlen(name)+1; + InputFiles[total_files].filename = my_malloc(strlen(name)+1, "filename storage"); + strcpy(InputFiles[total_files].filename, name); if (debugfile_switch) { debug_file_printf("", total_files); @@ -157,8 +142,10 @@ extern void load_sourcefile(char *filename_given, int same_directory_flag) fatalerror_named("Couldn't open source file", name); InputFiles[total_files].is_input = TRUE; + InputFiles[total_files].initial_buffering = TRUE; - if (line_trace_level > 0) printf("\nOpening file \"%s\"\n",name); + if (files_trace_setting > 0) + printf("Opening file \"%s\"\n",name); total_files++; total_input_files++; @@ -169,7 +156,8 @@ static void close_sourcefile(int file_number) { if (InputFiles[file_number-1].handle == NULL) return; - /* Close this file. */ + /* Close this file. But keep the InputFiles entry around, including + its filename. */ if (ferror(InputFiles[file_number-1].handle)) fatalerror_named("I/O failure: couldn't read from source file", @@ -179,7 +167,10 @@ static void close_sourcefile(int file_number) InputFiles[file_number-1].handle = NULL; - if (line_trace_level > 0) printf("\nClosing file\n"); + if (files_trace_setting > 0) { + char *str = (InputFiles[file_number-1].initial_buffering ? " (in initial buffering)" : ""); + printf("Closing file \"%s\"%s\n", InputFiles[file_number-1].filename, str); + } } extern void close_all_source(void) @@ -219,17 +210,10 @@ extern int register_orig_sourcefile(char *filename) name = filename; /* no translation */ - if (total_files == MAX_SOURCE_FILES) - memoryerror("MAX_SOURCE_FILES", MAX_SOURCE_FILES); - - if (filename_storage_left <= (int)strlen(name)) - memoryerror("MAX_SOURCE_FILES", MAX_SOURCE_FILES); - - filename_storage_left -= strlen(name)+1; - strcpy(filename_storage_p, name); - InputFiles[total_files].filename = filename_storage_p; + ensure_memory_list_available(&InputFiles_memlist, total_files+1); - filename_storage_p += strlen(name)+1; + InputFiles[total_files].filename = my_malloc(strlen(name)+1, "filename storage"); + strcpy(InputFiles[total_files].filename, name); if (debugfile_switch) { debug_file_printf("", total_files); @@ -242,6 +226,7 @@ extern int register_orig_sourcefile(char *filename) InputFiles[total_files].handle = NULL; InputFiles[total_files].is_input = FALSE; + InputFiles[total_files].initial_buffering = FALSE; total_files++; current_origsource_file = total_files; @@ -403,7 +388,7 @@ static void output_compression(int entnum, int32 *size, int *count) } static void output_file_z(void) -{ FILE *fin=NULL; char new_name[PATHLEN]; +{ char new_name[PATHLEN]; int32 length, blanks=0, size, i, j, offset; uint32 code_length, size_before_code, next_cons_check; int use_function; @@ -457,14 +442,6 @@ static void output_file_z(void) /* (2) Output the compiled code area. */ - if (temporary_files_switch) - { fclose(Temp2_fp); - Temp2_fp = NULL; - fin=fopen(Temp2_Name,"rb"); - if (fin==NULL) - fatalerror("I/O failure: couldn't reopen temporary file 2"); - } - if (!OMIT_UNUSED_ROUTINES) { /* This is the old-fashioned case, which is easy. All of zcode_area (zmachine_pc bytes) will be output. next_cons_check will be @@ -495,11 +472,11 @@ static void output_file_z(void) for (i=0; i= 0x80) long_flag = FALSE; backpatch_marker &= 0x7f; offset = offset + (backpatch_marker/32)*0x10000; @@ -514,17 +491,13 @@ static void output_file_z(void) while (j static_strings_extent || ch < 0) compiler_error("Read too much not-yet-compressed text."); @@ -1191,13 +1087,13 @@ game features require version 0x%08lx", (long)requested_glulx_version, (long)Ver int32 val, ix, jx; for (ix=0, jx=0; ix%s " "(superseded replacement)", - symbs[symbol_index]); + symbols[symbol_index].name); if (fseek(Debug_fp, 0L, SEEK_END)) { fatalerror("I/O failure: can't seek in debugging information file"); } } fgetpos - (Debug_fp, &replacement_debug_backpatch_positions[symbol_index].position); - replacement_debug_backpatch_positions[symbol_index].valid = TRUE; - debug_file_printf("%s", symbs[symbol_index]); + (Debug_fp, &symbol_debug_info[symbol_index].replacement_backpatch_pos.position); + symbol_debug_info[symbol_index].replacement_backpatch_pos.valid = TRUE; + debug_file_printf("%s", symbols[symbol_index].name); /* Space for: artificial="true" (superseded replacement) */ debug_file_printf(" "); } extern void write_debug_symbol_backpatch(int32 symbol_index) -{ if (symbol_debug_backpatch_positions[symbol_index].valid) { +{ if (symbol_debug_info[symbol_index].backpatch_pos.valid) { compiler_error("Symbol entry incorrectly reused in debug information " "file backpatching"); } - fgetpos(Debug_fp, &symbol_debug_backpatch_positions[symbol_index].position); - symbol_debug_backpatch_positions[symbol_index].valid = TRUE; + fgetpos(Debug_fp, &symbol_debug_info[symbol_index].backpatch_pos.position); + symbol_debug_info[symbol_index].backpatch_pos.valid = TRUE; /* Reserve space for up to 10 digits plus a negative sign. */ debug_file_printf("*BACKPATCH*"); } extern void write_debug_symbol_optional_backpatch(int32 symbol_index) -{ if (symbol_debug_backpatch_positions[symbol_index].valid) { +{ if (symbol_debug_info[symbol_index].backpatch_pos.valid) { compiler_error("Symbol entry incorrectly reused in debug information " "file backpatching"); } @@ -1614,8 +1510,8 @@ extern void write_debug_symbol_optional_backpatch(int32 symbol_index) so that we'll be in the same case as above if the symbol is eventually defined. */ debug_file_printf(""); - fgetpos(Debug_fp, &symbol_debug_backpatch_positions[symbol_index].position); - symbol_debug_backpatch_positions[symbol_index].valid = TRUE; + fgetpos(Debug_fp, &symbol_debug_info[symbol_index].backpatch_pos.position); + symbol_debug_info[symbol_index].backpatch_pos.valid = TRUE; debug_file_printf("*BACKPATCH*"); } @@ -1731,18 +1627,18 @@ extern void end_writing_debug_sections(int32 end_address) } extern void write_debug_undef(int32 symbol_index) -{ if (!symbol_debug_backpatch_positions[symbol_index].valid) +{ if (!symbol_debug_info[symbol_index].backpatch_pos.valid) { compiler_error ("Attempt to erase debugging information never written or since " "erased"); } - if (stypes[symbol_index] != CONSTANT_T) + if (symbols[symbol_index].type != CONSTANT_T) { compiler_error ("Attempt to erase debugging information for a non-constant " "because of an #undef"); } if (fsetpos - (Debug_fp, &symbol_debug_backpatch_positions[symbol_index].position)) + (Debug_fp, &symbol_debug_info[symbol_index].backpatch_pos.position)) { fatalerror("I/O failure: can't seek in debugging information file"); } /* There are 7 characters in ``''. */ @@ -1752,7 +1648,7 @@ extern void write_debug_undef(int32 symbol_index) /* Overwrite: *BACKPATCH* */ debug_file_printf(" "); nullify_debug_file_position - (&symbol_debug_backpatch_positions[symbol_index]); + (&symbol_debug_info[symbol_index].backpatch_pos); if (fseek(Debug_fp, 0L, SEEK_END)) { fatalerror("I/O failure: can't seek in debugging information file"); } @@ -1783,14 +1679,13 @@ static void apply_debug_information_backpatches static void apply_debug_information_symbol_backpatches() { int backpatch_symbol; for (backpatch_symbol = no_symbols; backpatch_symbol--;) - { if (symbol_debug_backpatch_positions[backpatch_symbol].valid) + { if (symbol_debug_info[backpatch_symbol].backpatch_pos.valid) { if (fsetpos(Debug_fp, - &symbol_debug_backpatch_positions - [backpatch_symbol].position)) + &symbol_debug_info[backpatch_symbol].backpatch_pos.position)) { fatalerror ("I/O failure: can't seek in debugging information file"); } - debug_file_printf("%11d", svals[backpatch_symbol]); + debug_file_printf("%11d", symbols[backpatch_symbol].value); } } } @@ -1834,57 +1729,6 @@ extern void end_debug_file() close_debug_file(); } -/* ------------------------------------------------------------------------- */ -/* Temporary storage files: */ -/* */ -/* Temp file 1 is used to hold the static strings area, as compiled */ -/* 2 to hold compiled routines of Z-code */ -/* 3 to hold the link data table (but only for modules) */ -/* */ -/* (Though annoying, this procedure typically saves about 200K of memory, */ -/* an important point for Amiga and sub-386 PC users of Inform) */ -/* ------------------------------------------------------------------------- */ - -extern void open_temporary_files(void) -{ translate_temp_filename(1); - Temp1_fp=fopen(Temp1_Name,"wb"); - if (Temp1_fp==NULL) fatalerror_named("Couldn't open temporary file 1", - Temp1_Name); - translate_temp_filename(2); - Temp2_fp=fopen(Temp2_Name,"wb"); - if (Temp2_fp==NULL) fatalerror_named("Couldn't open temporary file 2", - Temp2_Name); - - if (!module_switch) return; - translate_temp_filename(3); - Temp3_fp=fopen(Temp3_Name,"wb"); - if (Temp3_fp==NULL) fatalerror_named("Couldn't open temporary file 3", - Temp3_Name); -} - -extern void check_temp_files(void) -{ - if (ferror(Temp1_fp)) - fatalerror("I/O failure: couldn't write to temporary file 1"); - if (ferror(Temp2_fp)) - fatalerror("I/O failure: couldn't write to temporary file 2"); - if (module_switch && ferror(Temp3_fp)) - fatalerror("I/O failure: couldn't write to temporary file 3"); -} - -extern void remove_temp_files(void) -{ if (Temp1_fp != NULL) fclose(Temp1_fp); - Temp1_fp = NULL; - if (Temp2_fp != NULL) fclose(Temp2_fp); - Temp2_fp = NULL; - remove(Temp1_Name); remove(Temp2_Name); - if (module_switch) - { if (Temp3_fp != NULL) fclose(Temp3_fp); - Temp3_fp = NULL; - remove(Temp3_Name); - } -} - /* ========================================================================= */ /* Data structure management routines */ /* ------------------------------------------------------------------------- */ @@ -1908,8 +1752,6 @@ extern void files_begin_prepass(void) extern void files_begin_pass(void) { total_chars_read=0; - if (temporary_files_switch) - open_temporary_files(); } static void initialise_accumulator @@ -1927,10 +1769,9 @@ static void initialise_accumulator } extern void files_allocate_arrays(void) -{ filename_storage = my_malloc(MAX_SOURCE_FILES*64, "filename storage"); - filename_storage_p = filename_storage; - filename_storage_left = MAX_SOURCE_FILES*64; - InputFiles = my_malloc(MAX_SOURCE_FILES*sizeof(FileId), +{ + initialise_memory_list(&InputFiles_memlist, + sizeof(FileId), 16, (void**)&InputFiles, "input file storage"); if (debugfile_switch) { if (glulx_mode) @@ -1959,8 +1800,14 @@ static void tear_down_accumulator(debug_backpatch_accumulator *accumulator) } extern void files_free_arrays(void) -{ my_free(&filename_storage, "filename storage"); - my_free(&InputFiles, "input file storage"); +{ + int ix; + for (ix=0; ix