From b15c46e764a499aa65183b5a1d81b5afb1ec2708 Mon Sep 17 00:00:00 2001 From: marcobaye Date: Tue, 12 Mar 2024 00:11:32 +0000 Subject: [PATCH] added check for absolute paths so current dir is not used as prefix git-svn-id: https://svn.code.sf.net/p/acme-crossass/code-0/trunk@359 4df02467-bbd4-4a76-a152-e7ce94205b78 --- src/cpu.h | 1 + src/input.c | 50 ++++++++++++++++++++++++++++----------------- src/input.h | 15 ++++++++------ src/mnemo.c | 6 +++--- src/pseudoopcodes.c | 18 ++++++++-------- src/version.h | 2 +- 6 files changed, 54 insertions(+), 38 deletions(-) diff --git a/src/cpu.h b/src/cpu.h index 17ee4ce..7524539 100644 --- a/src/cpu.h +++ b/src/cpu.h @@ -18,6 +18,7 @@ struct cpu_type { boolean (*keyword_is_mnemonic)(int); bits flags; // see below for bit meanings unsigned char default_align_value; + // TODO - add "reserved_keywords_maxlen" }; #define CPUFLAG_INDIRECTJMPBUGGY (1u << 0) // warn if "jmp ($xxff)" is assembled #define CPUFLAG_SUPPORTSLONGREGS (1u << 1) // allow "!al" and "!rl" pseudo opcodes diff --git a/src/input.c b/src/input.c index 1962349..981d766 100644 --- a/src/input.c +++ b/src/input.c @@ -57,7 +57,7 @@ void input_parse_and_close_platform_file(const char *eternal_plat_filename, FILE // be verbose if (config.process_verbosity >= 3) - printf("Parsing source file '%s'.\n", eternal_plat_filename); + printf("Parsing source file \"%s\".\n", eternal_plat_filename); // set up new input new_input.plat_pathref_filename = eternal_plat_filename; new_input.location.plat_filename = eternal_plat_filename; @@ -666,21 +666,19 @@ static int read_filename_shared_end(void) // try to read a file name for an input file. // library access by using <...> quoting is allowed. -// if library access is used, TRUE will be stored via the "uses_lib" ptr. -// if library access is not used, FALSE will be stored via the "uses_lib" ptr. +// flags for "library access" and "absolute path" will be set accordingly. // The file name given in the assembler source code is converted from // UNIX style to platform style. // Returns nonzero on error. Filename in GlobalDynaBuf. // Errors are handled and reported, but caller should call // input_skip_remainder() then. -int input_read_input_filename(boolean *uses_lib) +int input_read_input_filename(struct filespecflags *flags) { - dynabuf_clear(GlobalDynaBuf); SKIPSPACE(); if (GotByte == '<') { // library access: - *uses_lib = TRUE; + flags->uses_lib = TRUE; // read file name string (must be a single string ) if (input_quoted_to_dynabuf('>')) return 1; // unterminated or escaping error @@ -688,7 +686,7 @@ int input_read_input_filename(boolean *uses_lib) GetByte(); // eat '>' terminator } else { // "normal", non-library access: - *uses_lib = FALSE; + flags->uses_lib = FALSE; // old algo (do not merge with similar parts from "if" block!): if (GotByte != '"') { Throw_error("File name quotes not found (\"\" or <>)."); @@ -705,6 +703,14 @@ int input_read_input_filename(boolean *uses_lib) // FIXME - use expression parser to read filename string! // see lines 416 and 1317 in pseudoopcodes.c for two more possible callers! } + + // check if absolute + if ((GlobalDynaBuf->size) + && (GlobalDynaBuf->buffer[0] == '/')) + flags->absolute = TRUE; + else + flags->absolute = FALSE; + // check length, unescape, terminate, do platform conversion return read_filename_shared_end(); } @@ -852,6 +858,8 @@ void includepaths_add(const char *path) // add filename (from GlobalDynaBuf) to pathbuf and try to open file: static FILE *combine_and_open_ro(void) { + FILE *stream; + // if path does not end with directory separator, add one: if (pathbuf->size && (pathbuf->buffer[pathbuf->size - 1] != DIRECTORY_SEPARATOR) @@ -863,26 +871,32 @@ static FILE *combine_and_open_ro(void) // terminate dynabuf_append(pathbuf, '\0'); // try to open for reading - return fopen(pathbuf->buffer, FILE_READBINARY); + stream = fopen(pathbuf->buffer, FILE_READBINARY); + if (config.process_verbosity >= 9) + printf("Trying \"%s\"...%s\n", pathbuf->buffer, stream ? " OK!" : ""); + return stream; } // open file for reading -// "uses_lib" tells whether to use library prefix or to use search paths -// file name is expected in GlobalDynaBuf, in platform style and terminated +// "flags" decide whether library access, search paths or absolute path is wanted. +// file name is expected in GlobalDynaBuf, in platform style and terminated. // returns NULL or open stream // on success, GlobalDynaBuf contains full file name in platform style -FILE *includepaths_open_ro(boolean uses_lib) +FILE *includepaths_open_ro(struct filespecflags *flags) { FILE *stream; struct ipi *ipi; - if (uses_lib) { + if (flags->uses_lib) { // use library prefix library_path_to_pathbuf(); stream = combine_and_open_ro(); } else { // first try current default prefix - default_path_to_pathbuf(); + if (flags->absolute) + dynabuf_clear(pathbuf); // which is "" if absolute + else + default_path_to_pathbuf(); // or current path if relative stream = combine_and_open_ro(); if (stream == NULL) { // default prefix failed, so try list entries: @@ -890,12 +904,8 @@ FILE *includepaths_open_ro(boolean uses_lib) dynabuf_clear(pathbuf); dynabuf_add_string(pathbuf, ipi->path); stream = combine_and_open_ro(); - //printf("trying <<%s>> - ", pathbuf->buffer); if (stream) { - //printf("ok\n"); break; - } else { - //printf("failed\n"); } } } @@ -908,9 +918,11 @@ FILE *includepaths_open_ro(boolean uses_lib) } else { // CAUTION, I'm re-using the path dynabuf to assemble the error message: dynabuf_clear(pathbuf); - dynabuf_add_string(pathbuf, "Cannot open input file \""); + dynabuf_add_string(pathbuf, "Cannot open input file "); + dynabuf_append(pathbuf, flags->uses_lib ? '<' : '\"'); dynabuf_add_string(pathbuf, GLOBALDYNABUF_CURRENT); - dynabuf_add_string(pathbuf, "\"."); + dynabuf_append(pathbuf, flags->uses_lib ? '>' : '\"'); + dynabuf_append(pathbuf, '.'); dynabuf_append(pathbuf, '\0'); Throw_error(pathbuf->buffer); } diff --git a/src/input.h b/src/input.h index 09dfe97..23f2dce 100644 --- a/src/input.h +++ b/src/input.h @@ -44,6 +44,10 @@ struct input { char *ram_ptr; // RAM read ptr (loop or macro block) } src; }; +struct filespecflags { + boolean uses_lib; // file name was given in <...> instead of "..." + boolean absolute; // file name started with '/' +}; // Constants @@ -126,14 +130,13 @@ extern int input_read_and_lower_keyword(void); // try to read a file name for an input file. // library access by using <...> quoting is allowed. -// if library access is used, TRUE will be stored via the "uses_lib" ptr. -// if library access is not used, FALSE will be stored via the "uses_lib" ptr. +// flags for "library access" and "absolute path" will be set accordingly. // The file name given in the assembler source code is converted from // UNIX style to platform style. // Returns nonzero on error. Filename in GlobalDynaBuf. // Errors are handled and reported, but caller should call // input_skip_remainder() then. -extern int input_read_input_filename(boolean *uses_lib); +extern int input_read_input_filename(struct filespecflags *flags); // try to read a file name for an output file ("!to" and "!sl" only). // library access by using <...> quoting is forbidden. @@ -161,11 +164,11 @@ extern bits input_get_force_bit(void); extern void includepaths_add(const char *path); // open file for reading -// "uses_lib" tells whether to use library prefix or to use search paths -// file name is expected in GlobalDynaBuf, in platform style and terminated +// "flags" decide whether library access, search paths or absolute path is wanted. +// file name is expected in GlobalDynaBuf, in platform style and terminated. // returns NULL or open stream // on success, GlobalDynaBuf contains full file name in platform style -extern FILE *includepaths_open_ro(boolean uses_lib); +extern FILE *includepaths_open_ro(struct filespecflags *flags); #endif diff --git a/src/mnemo.c b/src/mnemo.c index 526a17d..06a3090 100644 --- a/src/mnemo.c +++ b/src/mnemo.c @@ -1168,13 +1168,13 @@ static boolean check_mnemo_tree(struct ronode *tree, struct dynabuf *dyna_buf) if (!tree_easy_scan(tree, &node_body, dyna_buf)) return FALSE; - code = ((int) node_body) & CODEMASK; // get opcode or table index - flags = ((int) node_body) & FLAGSMASK; // get immediate mode flags and prefix flags + code = ((intval_t) node_body) & CODEMASK; // get opcode or table index + flags = ((intval_t) node_body) & FLAGSMASK; // get immediate mode flags and prefix flags if (flags & PREFIX_NEGNEG) { output_byte(0x42); output_byte(0x42); } - switch (GROUP((long) node_body)) { + switch (GROUP((intval_t) node_body)) { case GROUP_ACCU: // main accumulator stuff group_main(code, flags); break; diff --git a/src/pseudoopcodes.c b/src/pseudoopcodes.c index 0bd15c7..d4775e8 100644 --- a/src/pseudoopcodes.c +++ b/src/pseudoopcodes.c @@ -362,18 +362,18 @@ static enum eos po_cbm(void) // (allows for block, so must be reentrant) static enum eos use_encoding_from_file(void) { - boolean uses_lib; + struct filespecflags flags; FILE *stream; unsigned char local_table[256], *buffered_table; const struct encoder *buffered_encoder; // read file name and convert from UNIX style to platform style - if (input_read_input_filename(&uses_lib)) + if (input_read_input_filename(&flags)) return SKIP_REMAINDER; // if missing or unterminated, give up // read from file - stream = includepaths_open_ro(uses_lib); + stream = includepaths_open_ro(&flags); if (stream) { // try to load encoding table from given file if (fread(local_table, sizeof(char), 256, stream) != 256) @@ -538,7 +538,7 @@ static enum eos po_scrxor(void) // FIXME - split this into "parser" and "worker" fn and move worker fn somewhere else. static enum eos po_binary(void) { - boolean uses_lib; + struct filespecflags flags; FILE *stream; int byte; struct number size, @@ -548,11 +548,11 @@ static enum eos po_binary(void) skip.val.intval = 0; // read file name and convert from UNIX style to platform style - if (input_read_input_filename(&uses_lib)) + if (input_read_input_filename(&flags)) return SKIP_REMAINDER; // if missing or unterminated, give up // try to open file - stream = includepaths_open_ro(uses_lib); + stream = includepaths_open_ro(&flags); if (stream == NULL) return SKIP_REMAINDER; @@ -924,7 +924,7 @@ static enum eos po_subzone(void) // include source file ("!source" or "!src"). has to be re-entrant. static enum eos po_source(void) // now GotByte = illegal char { - boolean uses_lib; + struct filespecflags flags; FILE *stream; const char *eternal_plat_filename; char local_gotbyte; @@ -935,11 +935,11 @@ static enum eos po_source(void) // now GotByte = illegal char Throw_serious_error("Too deeply nested. Recursive \"!source\"?"); // read file name and convert from UNIX style to platform style - if (input_read_input_filename(&uses_lib)) + if (input_read_input_filename(&flags)) return SKIP_REMAINDER; // if missing or unterminated, give up // if file could be opened, parse it. otherwise, complain - stream = includepaths_open_ro(uses_lib); + stream = includepaths_open_ro(&flags); if (stream) { eternal_plat_filename = dynabuf_get_copy(GlobalDynaBuf); local_gotbyte = GotByte; // CAUTION - ugly kluge diff --git a/src/version.h b/src/version.h index 6aa6fb1..050a8e0 100644 --- a/src/version.h +++ b/src/version.h @@ -9,7 +9,7 @@ #define RELEASE "0.97" // update before release FIXME #define CODENAME "Zem" // update before release -#define CHANGE_DATE "2 Mar" // update before release FIXME +#define CHANGE_DATE "3 Mar" // update before release FIXME #define CHANGE_YEAR "2024" // update before release //#define HOME_PAGE "http://home.pages.de/~mac_bacon/smorbrod/acme/" #define HOME_PAGE "http://sourceforge.net/p/acme-crossass/" // FIXME