upated the standard source script handler to honor the INCLUDEDIRS option

This commit is contained in:
hyung-hwan 2022-06-10 08:53:42 +00:00
parent 4de6b5dc52
commit e1c482a632
9 changed files with 102 additions and 20 deletions

View File

@ -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"); fprintf (out, " --console-encoding string specify console encoding name\n");
#endif #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, " --modlibdirs specify directories to look for module files in\n");
fprintf (out, " --modern run in the modern mode(default)\n"); fprintf (out, " --modern run in the modern mode(default)\n");
fprintf (out, " --classic run in the classic mode\n"); fprintf (out, " --classic run in the classic mode\n");

View File

@ -49,13 +49,13 @@ pkginclude_HEADERS = \
hawk-htb.h \ hawk-htb.h \
hawk-map.h \ hawk-map.h \
hawk-rbt.h \ hawk-rbt.h \
hawk-pack1.h \ hawk-pac1.h \
hawk-skad.h \ hawk-skad.h \
hawk-utl.h \ hawk-utl.h \
hawk-sed.h \ hawk-sed.h \
hawk-std.h \ hawk-std.h \
hawk-tre.h \ hawk-tre.h \
hawk-unpack.h \ hawk-upac.h \
hawk-xma.h hawk-xma.h

View File

@ -230,8 +230,8 @@ libhawk_la_DEPENDENCIES = $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_1) \
$(am__append_12) $(am__append_13) $(am__append_14) $(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 \ 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-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-map.h hawk-rbt.h hawk-pac1.h hawk-skad.h hawk-utl.h \
hawk-sed.h hawk-std.h hawk-tre.h hawk-unpack.h hawk-xma.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 \ 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 \ 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 \ htb.c gem.c gem-nwif.c gem-nwif2.c hawk-prv.h hawk.c \
@ -390,8 +390,8 @@ am__can_run_installinfo = \
esac esac
am__pkginclude_HEADERS_DIST = hawk.h hawk-arr.h hawk-chr.h hawk-cmn.h \ 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-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-map.h hawk-rbt.h hawk-pac1.h hawk-skad.h hawk-utl.h \
hawk-sed.h hawk-std.h hawk-tre.h hawk-unpack.h hawk-xma.h \ hawk-sed.h hawk-std.h hawk-tre.h hawk-upac.h hawk-xma.h \
Hawk.hpp Hawk-Sed.hpp Hawk.hpp Hawk-Sed.hpp
HEADERS = $(pkginclude_HEADERS) HEADERS = $(pkginclude_HEADERS)
am__tagged_files = $(HEADERS) $(SOURCES) $(TAGS_FILES) $(LISP) \ am__tagged_files = $(HEADERS) $(SOURCES) $(TAGS_FILES) $(LISP) \
@ -601,8 +601,8 @@ CXXFLAGS_ALL_COMMON =
@WIN32_TRUE@ $(am__append_1) $(am__append_2) @WIN32_TRUE@ $(am__append_1) $(am__append_2)
pkginclude_HEADERS = hawk.h hawk-arr.h hawk-chr.h hawk-cmn.h \ 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-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-map.h hawk-rbt.h hawk-pac1.h hawk-skad.h hawk-utl.h \
hawk-sed.h hawk-std.h hawk-tre.h hawk-unpack.h hawk-xma.h \ hawk-sed.h hawk-std.h hawk-tre.h hawk-upac.h hawk-xma.h \
$(am__append_7) $(am__append_7)
pkglib_LTLIBRARIES = libhawk.la $(am__append_15) pkglib_LTLIBRARIES = libhawk.la $(am__append_15)
libhawk_la_SOURCES = $(pkginclude_HEADERS) arr.c chr.c dir.c ecs-imp.h \ libhawk_la_SOURCES = $(pkginclude_HEADERS) arr.c chr.c dir.c ecs-imp.h \

View File

@ -69,13 +69,13 @@ typedef struct hawk_mtx_t hawk_mtx_t;
typedef unsigned long hawk_mtx_hnd_t; typedef unsigned long hawk_mtx_hnd_t;
# endif # endif
# else # else
# include <hawk-pack1.h> # include <hawk-pac1.h>
struct hawk_mtx_hnd_t struct hawk_mtx_hnd_t
{ {
hawk_uint8_t b[HAWK_SIZEOF_PTHREAD_MUTEX_T]; hawk_uint8_t b[HAWK_SIZEOF_PTHREAD_MUTEX_T];
}; };
typedef struct hawk_mtx_hnd_t hawk_mtx_hnd_t; typedef struct hawk_mtx_hnd_t hawk_mtx_hnd_t;
# include <hawk-unpack.h> # include <hawk-upac.h>
# endif # endif
#endif #endif

View File

@ -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.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) if (hawk->log.len > 0)
{ {
/* flush pending log message that could be generated by the fini /* flush pending log message that could be generated by the fini

View File

@ -103,6 +103,7 @@ hawk_sio_t* hawk_sio_openstd (hawk_gem_t* gem, hawk_oow_t xtnsize, hawk_sio_std_
return sio; return sio;
} }
void hawk_sio_close (hawk_sio_t* sio) void hawk_sio_close (hawk_sio_t* sio)
{ {
hawk_sio_fini (sio); hawk_sio_fini (sio);

View File

@ -112,6 +112,8 @@ typedef struct xtn_t
const hawk_uch_t* end; const hawk_uch_t* end;
} ucs; } ucs;
} u; } u;
hawk_ooecs_t tbuf;
} in; } in;
struct struct
@ -376,8 +378,8 @@ void* hawk_stdmodopen (hawk_t* hawk, const hawk_mod_spec_t* spec)
hawk_ooch_t* modpath; hawk_ooch_t* modpath;
const hawk_ooch_t* tmp[6]; const hawk_ooch_t* tmp[6];
int count; int count;
static hawk_ooch_t ds[][2] = { { '\\', '\0' }, { '/', '\0' } } static hawk_ooch_t ds[][2] = { { '\\', '\0' }, { '/', '\0' } };
false 0 treu 1
count = 0; count = 0;
if (spec->libdir) 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); 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); xtn_t* xtn = GET_XTN(hawk);
if (xtn->stdmod_up) if (xtn->stdmod_up)
@ -942,11 +944,13 @@ static void fini_xtn (hawk_t* hawk)
xtn->stdmod_up = 0; 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); if ((xtn->log.fd_flag & LOGFD_OPENED_HERE) && xtn->log.fd >= 0) close (xtn->log.fd);
reset_log_to_default (xtn); reset_log_to_default (xtn);
} }
static void clear_xtn (hawk_t* hawk) static void clear_xtn (hawk_t* hawk, void* ctx)
{ {
/* nothing to do */ /* nothing to do */
} }
@ -989,6 +993,7 @@ hawk_t* hawk_openstdwithmmgr (hawk_mmgr_t* mmgr, hawk_oow_t xtnsize, hawk_cmgr_t
/* initialize extension */ /* initialize extension */
xtn = GET_XTN(hawk); xtn = GET_XTN(hawk);
hawk_ooecs_init (&xtn->s.in.tbuf, hawk_getgem(hawk), 0);
reset_log_to_default (xtn); reset_log_to_default (xtn);
/* add intrinsic global variables and functions */ /* 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.close = fini_xtn;
xtn->ecb.clear = clear_xtn; xtn->ecb.clear = clear_xtn;
xtn->ecb.ctx = HAWK_NULL;
hawk_pushecb (hawk, &xtn->ecb); hawk_pushecb (hawk, &xtn->ecb);
return hawk; return hawk;
@ -1221,6 +1227,63 @@ static int fill_sio_arg_unique_id (hawk_t* hawk, hawk_sio_arg_t* arg, const hawk
#endif #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) static hawk_ooi_t sf_in_open (hawk_t* hawk, hawk_sio_arg_t* arg, xtn_t* xtn)
{ {
if (arg->prev == HAWK_NULL) 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; const hawk_ooch_t* base;
base = hawk_get_base_name_oocstr(outer); 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; 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)); xpath = hawk_addsionamewithoochars(hawk, path, hawk_count_oocstr(path));
if (dbuf) hawk_freemem (hawk, dbuf); if (dbuf) hawk_freemem (hawk, dbuf);
} }
else 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; 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; arg->path = xpath;
/* TODO: use the system handle(file descriptor) instead of the path? */ /* TODO: use the system handle(file descriptor) instead of the path? */
/*syshnd = hawk_sio_gethnd(arg->handle);*/ /*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; 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; break;
} }
case HAWK_SIO_CMD_CLOSE: case HAWK_SIO_CMD_CLOSE:
{ {
switch (xtn->s.out.x->type) 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; 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); rxtn_t* rxtn = GET_RXTN(rtx);
/*xtn_t* xtn = (xtn_t*)GET_XTN(hawk_rtx_gethawk(rtx));*/ /*xtn_t* xtn = (xtn_t*)GET_XTN(hawk_rtx_gethawk(rtx));*/