From e1c482a63225afad1192e5ad8e9ac8f6fdf78d58 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Fri, 10 Jun 2022 08:53:42 +0000 Subject: [PATCH] upated the standard source script handler to honor the INCLUDEDIRS option --- hawk/bin/hawk.c | 2 +- hawk/lib/Makefile.am | 4 +- hawk/lib/Makefile.in | 12 +-- hawk/lib/hawk-mtx.h | 4 +- hawk/lib/{hawk-pack1.h => hawk-pac1.h} | 0 hawk/lib/{hawk-unpack.h => hawk-upac.h} | 0 hawk/lib/hawk.c | 2 + hawk/lib/sio.c | 1 + hawk/lib/std.c | 97 ++++++++++++++++++++++--- 9 files changed, 102 insertions(+), 20 deletions(-) rename hawk/lib/{hawk-pack1.h => hawk-pac1.h} (100%) rename hawk/lib/{hawk-unpack.h => hawk-upac.h} (100%) diff --git a/hawk/bin/hawk.c b/hawk/bin/hawk.c index 7c42f552..ff7a73b4 100644 --- a/hawk/bin/hawk.c +++ b/hawk/bin/hawk.c @@ -544,7 +544,7 @@ static void print_usage (FILE* out, const hawk_bch_t* argv0) fprintf (out, " --console-encoding string specify console encoding name\n"); #endif - fprintf (out, " --includedirs specify directories to look for includes files in\n"); + fprintf (out, " --includedirs specify directories to look for includes files in\n"); fprintf (out, " --modlibdirs specify directories to look for module files in\n"); fprintf (out, " --modern run in the modern mode(default)\n"); fprintf (out, " --classic run in the classic mode\n"); diff --git a/hawk/lib/Makefile.am b/hawk/lib/Makefile.am index fe8f76d4..335907ef 100644 --- a/hawk/lib/Makefile.am +++ b/hawk/lib/Makefile.am @@ -49,13 +49,13 @@ pkginclude_HEADERS = \ hawk-htb.h \ hawk-map.h \ hawk-rbt.h \ - hawk-pack1.h \ + hawk-pac1.h \ hawk-skad.h \ hawk-utl.h \ hawk-sed.h \ hawk-std.h \ hawk-tre.h \ - hawk-unpack.h \ + hawk-upac.h \ hawk-xma.h diff --git a/hawk/lib/Makefile.in b/hawk/lib/Makefile.in index df0d1f48..10d2aa09 100644 --- a/hawk/lib/Makefile.in +++ b/hawk/lib/Makefile.in @@ -230,8 +230,8 @@ libhawk_la_DEPENDENCIES = $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_1) \ $(am__append_12) $(am__append_13) $(am__append_14) am__libhawk_la_SOURCES_DIST = hawk.h hawk-arr.h hawk-chr.h hawk-cmn.h \ hawk-dir.h hawk-ecs.h hawk-fmt.h hawk-gem.h hawk-htb.h \ - hawk-map.h hawk-rbt.h hawk-pack1.h hawk-skad.h hawk-utl.h \ - hawk-sed.h hawk-std.h hawk-tre.h hawk-unpack.h hawk-xma.h \ + hawk-map.h hawk-rbt.h hawk-pac1.h hawk-skad.h hawk-utl.h \ + hawk-sed.h hawk-std.h hawk-tre.h hawk-upac.h hawk-xma.h \ Hawk.hpp Hawk-Sed.hpp arr.c chr.c dir.c ecs-imp.h ecs.c \ err-prv.h err.c err-sys.c fmt-imp.h fmt.c fnc-prv.h fnc.c \ htb.c gem.c gem-nwif.c gem-nwif2.c hawk-prv.h hawk.c \ @@ -390,8 +390,8 @@ am__can_run_installinfo = \ esac am__pkginclude_HEADERS_DIST = hawk.h hawk-arr.h hawk-chr.h hawk-cmn.h \ hawk-dir.h hawk-ecs.h hawk-fmt.h hawk-gem.h hawk-htb.h \ - hawk-map.h hawk-rbt.h hawk-pack1.h hawk-skad.h hawk-utl.h \ - hawk-sed.h hawk-std.h hawk-tre.h hawk-unpack.h hawk-xma.h \ + hawk-map.h hawk-rbt.h hawk-pac1.h hawk-skad.h hawk-utl.h \ + hawk-sed.h hawk-std.h hawk-tre.h hawk-upac.h hawk-xma.h \ Hawk.hpp Hawk-Sed.hpp HEADERS = $(pkginclude_HEADERS) am__tagged_files = $(HEADERS) $(SOURCES) $(TAGS_FILES) $(LISP) \ @@ -601,8 +601,8 @@ CXXFLAGS_ALL_COMMON = @WIN32_TRUE@ $(am__append_1) $(am__append_2) pkginclude_HEADERS = hawk.h hawk-arr.h hawk-chr.h hawk-cmn.h \ hawk-dir.h hawk-ecs.h hawk-fmt.h hawk-gem.h hawk-htb.h \ - hawk-map.h hawk-rbt.h hawk-pack1.h hawk-skad.h hawk-utl.h \ - hawk-sed.h hawk-std.h hawk-tre.h hawk-unpack.h hawk-xma.h \ + hawk-map.h hawk-rbt.h hawk-pac1.h hawk-skad.h hawk-utl.h \ + hawk-sed.h hawk-std.h hawk-tre.h hawk-upac.h hawk-xma.h \ $(am__append_7) pkglib_LTLIBRARIES = libhawk.la $(am__append_15) libhawk_la_SOURCES = $(pkginclude_HEADERS) arr.c chr.c dir.c ecs-imp.h \ diff --git a/hawk/lib/hawk-mtx.h b/hawk/lib/hawk-mtx.h index 2629618d..b329d31d 100644 --- a/hawk/lib/hawk-mtx.h +++ b/hawk/lib/hawk-mtx.h @@ -69,13 +69,13 @@ typedef struct hawk_mtx_t hawk_mtx_t; typedef unsigned long hawk_mtx_hnd_t; # endif # else -# include +# include struct hawk_mtx_hnd_t { hawk_uint8_t b[HAWK_SIZEOF_PTHREAD_MUTEX_T]; }; typedef struct hawk_mtx_hnd_t hawk_mtx_hnd_t; -# include +# include # endif #endif diff --git a/hawk/lib/hawk-pack1.h b/hawk/lib/hawk-pac1.h similarity index 100% rename from hawk/lib/hawk-pack1.h rename to hawk/lib/hawk-pac1.h diff --git a/hawk/lib/hawk-unpack.h b/hawk/lib/hawk-upac.h similarity index 100% rename from hawk/lib/hawk-unpack.h rename to hawk/lib/hawk-upac.h diff --git a/hawk/lib/hawk.c b/hawk/lib/hawk.c index 4331b78e..b310888d 100644 --- a/hawk/lib/hawk.c +++ b/hawk/lib/hawk.c @@ -320,6 +320,8 @@ void hawk_fini (hawk_t* hawk) if (hawk->opt.mod[i].ptr) hawk_freemem (hawk, hawk->opt.mod[i].ptr); } + if (hawk->opt.includedirs.ptr) hawk_freemem (hawk, hawk->opt.includedirs.ptr); + if (hawk->log.len > 0) { /* flush pending log message that could be generated by the fini diff --git a/hawk/lib/sio.c b/hawk/lib/sio.c index d1e1bb2c..4712ba81 100644 --- a/hawk/lib/sio.c +++ b/hawk/lib/sio.c @@ -103,6 +103,7 @@ hawk_sio_t* hawk_sio_openstd (hawk_gem_t* gem, hawk_oow_t xtnsize, hawk_sio_std_ return sio; } + void hawk_sio_close (hawk_sio_t* sio) { hawk_sio_fini (sio); diff --git a/hawk/lib/std.c b/hawk/lib/std.c index 074b5c70..7777279d 100644 --- a/hawk/lib/std.c +++ b/hawk/lib/std.c @@ -112,6 +112,8 @@ typedef struct xtn_t const hawk_uch_t* end; } ucs; } u; + + hawk_ooecs_t tbuf; } in; struct @@ -376,8 +378,8 @@ void* hawk_stdmodopen (hawk_t* hawk, const hawk_mod_spec_t* spec) hawk_ooch_t* modpath; const hawk_ooch_t* tmp[6]; int count; - static hawk_ooch_t ds[][2] = { { '\\', '\0' }, { '/', '\0' } } - false 0 treu 1 + static hawk_ooch_t ds[][2] = { { '\\', '\0' }, { '/', '\0' } }; + count = 0; if (spec->libdir) { @@ -933,7 +935,7 @@ hawk_t* hawk_openstd (hawk_oow_t xtnsize, hawk_errnum_t* errnum) return hawk_openstdwithmmgr(&sys_mmgr, xtnsize, hawk_get_cmgr_by_id(HAWK_CMGR_UTF8), errnum); } -static void fini_xtn (hawk_t* hawk) +static void fini_xtn (hawk_t* hawk, void* ctx) { xtn_t* xtn = GET_XTN(hawk); if (xtn->stdmod_up) @@ -942,11 +944,13 @@ static void fini_xtn (hawk_t* hawk) xtn->stdmod_up = 0; } + hawk_ooecs_fini (&xtn->s.in.tbuf); + if ((xtn->log.fd_flag & LOGFD_OPENED_HERE) && xtn->log.fd >= 0) close (xtn->log.fd); reset_log_to_default (xtn); } -static void clear_xtn (hawk_t* hawk) +static void clear_xtn (hawk_t* hawk, void* ctx) { /* nothing to do */ } @@ -989,6 +993,7 @@ hawk_t* hawk_openstdwithmmgr (hawk_mmgr_t* mmgr, hawk_oow_t xtnsize, hawk_cmgr_t /* initialize extension */ xtn = GET_XTN(hawk); + hawk_ooecs_init (&xtn->s.in.tbuf, hawk_getgem(hawk), 0); reset_log_to_default (xtn); /* add intrinsic global variables and functions */ @@ -1010,6 +1015,7 @@ hawk_t* hawk_openstdwithmmgr (hawk_mmgr_t* mmgr, hawk_oow_t xtnsize, hawk_cmgr_t xtn->ecb.close = fini_xtn; xtn->ecb.clear = clear_xtn; + xtn->ecb.ctx = HAWK_NULL; hawk_pushecb (hawk, &xtn->ecb); return hawk; @@ -1221,6 +1227,63 @@ static int fill_sio_arg_unique_id (hawk_t* hawk, hawk_sio_arg_t* arg, const hawk #endif } +static int plain_file_exists (hawk_t* hawk, const hawk_ooch_t* file) +{ +#if defined(_WIN32) + DWORD attr; + + attr = GetFileAttributes(file); + if (attr == INVALID_FILE_ATTRIBUTES) return 0; + if (attr & FILE_ATTRIBUTE_DIRECTORY) return 0; /* not a plain file. it's a directory */ + return 1; +#else + + struct stat st; +#if defined(HAWK_OOCH_IS_UCH) + hawk_bch_t* tmp; + int n; + tmp = hawk_duputobcstr(hawk, file, HAWK_NULL); + if (!tmp) return 0; + n = stat(tmp, &st); + hawk_freemem (hawk, tmp); + if (n == -1) return 0; +#else + if (stat(file, &st) == -1) return 0; +#endif + if (S_ISDIR(st.st_mode)) return 0; /* not a plain file, it's a directory */ + return 1; +#endif +} + +static const hawk_ooch_t* get_file_in_dirs_noseterr (hawk_t* hawk, const hawk_oocs_t* dirs, const hawk_ooch_t* file) +{ + xtn_t* xtn = GET_XTN(hawk); + const hawk_ooch_t* ptr = dirs->ptr; + const hawk_ooch_t* dirend = dirs->ptr + dirs->len; + const hawk_ooch_t* colon, * endptr; + + while (1) + { + colon = hawk_find_oochar_in_oocstr(ptr, ':'); + endptr = colon? colon: dirend; + + hawk_ooecs_clear (&xtn->s.in.tbuf); + if (hawk_ooecs_ncat(&xtn->s.in.tbuf, ptr, endptr - ptr) == (hawk_oow_t)-1 || + hawk_ooecs_ccat(&xtn->s.in.tbuf, '/') == (hawk_oow_t)-1 || + hawk_ooecs_cat(&xtn->s.in.tbuf, file) == (hawk_oow_t)-1) break; /* the failure in hawk_ooecs_XXX functions still sets the error code */ + + if (plain_file_exists(hawk, HAWK_OOECS_PTR(&xtn->s.in.tbuf))) + { + return HAWK_OOECS_PTR(&xtn->s.in.tbuf); + } + + if (!colon) break; + ptr = colon + 1; + } + + return HAWK_NULL; +} + static hawk_ooi_t sf_in_open (hawk_t* hawk, hawk_sio_arg_t* arg, xtn_t* xtn) { if (arg->prev == HAWK_NULL) @@ -1257,7 +1320,7 @@ static hawk_ooi_t sf_in_open (hawk_t* hawk, hawk_sio_arg_t* arg, xtn_t* xtn) const hawk_ooch_t* base; base = hawk_get_base_name_oocstr(outer); - if (base != outer && arg->name[0] != HAWK_T('/')) + if (base != outer && arg->name[0] != '/') { hawk_oow_t tmplen, totlen, dirlen; @@ -1276,12 +1339,29 @@ static hawk_ooi_t sf_in_open (hawk_t* hawk, hawk_sio_arg_t* arg, xtn_t* xtn) } } + if (!plain_file_exists(hawk, path) && hawk->opt.includedirs.len > 0 && arg->name[0] != '/') + { + const hawk_ooch_t* tmp; + tmp = get_file_in_dirs_noseterr(hawk, &hawk->opt.includedirs, arg->name); + if (tmp) path = tmp; + } + xpath = hawk_addsionamewithoochars(hawk, path, hawk_count_oocstr(path)); if (dbuf) hawk_freemem (hawk, dbuf); } else { - xpath = hawk_addsionamewithoochars(hawk, arg->name, hawk_count_oocstr(arg->name)); + const hawk_ooch_t* path; + + path = arg->name; + if (!plain_file_exists(hawk, path) && hawk->opt.includedirs.len > 0 && arg->name[0] != '/') + { + const hawk_ooch_t* tmp; + tmp = get_file_in_dirs_noseterr(hawk, &hawk->opt.includedirs, arg->name); + if (tmp) path = tmp; + } + + xpath = hawk_addsionamewithoochars(hawk, path, hawk_count_oocstr(path)); } if (!xpath) goto fail; @@ -1298,7 +1378,7 @@ static hawk_ooi_t sf_in_open (hawk_t* hawk, hawk_sio_arg_t* arg, xtn_t* xtn) arg->path = xpath; /* TODO: use the system handle(file descriptor) instead of the path? */ /*syshnd = hawk_sio_gethnd(arg->handle);*/ - fill_sio_arg_unique_id(hawk, arg, xpath); /* ignore failure */ + fill_sio_arg_unique_id (hawk, arg, xpath); /* ignore failure */ return 0; } @@ -1599,7 +1679,6 @@ static hawk_ooi_t sf_out (hawk_t* hawk, hawk_sio_cmd_t cmd, hawk_sio_arg_t* arg, break; } - case HAWK_SIO_CMD_CLOSE: { switch (xtn->s.out.x->type) @@ -2551,7 +2630,7 @@ static hawk_ooi_t hawk_rio_console (hawk_rtx_t* rtx, hawk_rio_cmd_t cmd, hawk_ri return -1; } -static void fini_rxtn (hawk_rtx_t* rtx) +static void fini_rxtn (hawk_rtx_t* rtx, void* ctx) { rxtn_t* rxtn = GET_RXTN(rtx); /*xtn_t* xtn = (xtn_t*)GET_XTN(hawk_rtx_gethawk(rtx));*/