diff --git a/hawk/Makefile.in b/hawk/Makefile.in index 3d664f88..53017675 100644 --- a/hawk/Makefile.in +++ b/hawk/Makefile.in @@ -353,7 +353,6 @@ pdfdir = @pdfdir@ prefix = @prefix@ program_transform_name = @program_transform_name@ psdir = @psdir@ -runstatedir = @runstatedir@ sbindir = @sbindir@ sharedstatedir = @sharedstatedir@ srcdir = @srcdir@ diff --git a/hawk/bin/Makefile.in b/hawk/bin/Makefile.in index 5b284ebd..7ba2a9e3 100644 --- a/hawk/bin/Makefile.in +++ b/hawk/bin/Makefile.in @@ -322,7 +322,6 @@ pdfdir = @pdfdir@ prefix = @prefix@ program_transform_name = @program_transform_name@ psdir = @psdir@ -runstatedir = @runstatedir@ sbindir = @sbindir@ sharedstatedir = @sharedstatedir@ srcdir = @srcdir@ diff --git a/hawk/configure b/hawk/configure index 5988c00a..dc58f387 100755 --- a/hawk/configure +++ b/hawk/configure @@ -784,7 +784,6 @@ infodir docdir oldincludedir includedir -runstatedir localstatedir sharedstatedir sysconfdir @@ -886,7 +885,6 @@ datadir='${datarootdir}' sysconfdir='${prefix}/etc' sharedstatedir='${prefix}/com' localstatedir='${prefix}/var' -runstatedir='${localstatedir}/run' includedir='${prefix}/include' oldincludedir='/usr/include' docdir='${datarootdir}/doc/${PACKAGE_TARNAME}' @@ -1139,15 +1137,6 @@ do | -silent | --silent | --silen | --sile | --sil) silent=yes ;; - -runstatedir | --runstatedir | --runstatedi | --runstated \ - | --runstate | --runstat | --runsta | --runst | --runs \ - | --run | --ru | --r) - ac_prev=runstatedir ;; - -runstatedir=* | --runstatedir=* | --runstatedi=* | --runstated=* \ - | --runstate=* | --runstat=* | --runsta=* | --runst=* | --runs=* \ - | --run=* | --ru=* | --r=*) - runstatedir=$ac_optarg ;; - -sbindir | --sbindir | --sbindi | --sbind | --sbin | --sbi | --sb) ac_prev=sbindir ;; -sbindir=* | --sbindir=* | --sbindi=* | --sbind=* | --sbin=* \ @@ -1285,7 +1274,7 @@ fi for ac_var in exec_prefix prefix bindir sbindir libexecdir datarootdir \ datadir sysconfdir sharedstatedir localstatedir includedir \ oldincludedir docdir infodir htmldir dvidir pdfdir psdir \ - libdir localedir mandir runstatedir + libdir localedir mandir do eval ac_val=\$$ac_var # Remove trailing slashes. @@ -1438,7 +1427,6 @@ Fine tuning of the installation directories: --sysconfdir=DIR read-only single-machine data [PREFIX/etc] --sharedstatedir=DIR modifiable architecture-independent data [PREFIX/com] --localstatedir=DIR modifiable single-machine data [PREFIX/var] - --runstatedir=DIR modifiable per-process data [LOCALSTATEDIR/run] --libdir=DIR object code libraries [EPREFIX/lib] --includedir=DIR C header files [PREFIX/include] --oldincludedir=DIR C header files for non-gcc [/usr/include] @@ -18083,7 +18071,7 @@ else We can't simply define LARGE_OFF_T to be 9223372036854775807, since some C++ compilers masquerading as C compilers incorrectly reject 9223372036854775807. */ -#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31)) +#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1]; @@ -18129,7 +18117,7 @@ else We can't simply define LARGE_OFF_T to be 9223372036854775807, since some C++ compilers masquerading as C compilers incorrectly reject 9223372036854775807. */ -#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31)) +#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1]; @@ -18153,7 +18141,7 @@ rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext We can't simply define LARGE_OFF_T to be 9223372036854775807, since some C++ compilers masquerading as C compilers incorrectly reject 9223372036854775807. */ -#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31)) +#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1]; @@ -18198,7 +18186,7 @@ else We can't simply define LARGE_OFF_T to be 9223372036854775807, since some C++ compilers masquerading as C compilers incorrectly reject 9223372036854775807. */ -#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31)) +#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1]; @@ -18222,7 +18210,7 @@ rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext We can't simply define LARGE_OFF_T to be 9223372036854775807, since some C++ compilers masquerading as C compilers incorrectly reject 9223372036854775807. */ -#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31)) +#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1]; diff --git a/hawk/lib/Makefile.in b/hawk/lib/Makefile.in index e838eed0..3597f0fb 100644 --- a/hawk/lib/Makefile.in +++ b/hawk/lib/Makefile.in @@ -561,7 +561,6 @@ pdfdir = @pdfdir@ prefix = @prefix@ program_transform_name = @program_transform_name@ psdir = @psdir@ -runstatedir = @runstatedir@ sbindir = @sbindir@ sharedstatedir = @sharedstatedir@ srcdir = @srcdir@ diff --git a/hawk/lib/fnc.c b/hawk/lib/fnc.c index 6c0333b2..856c9e69 100644 --- a/hawk/lib/fnc.c +++ b/hawk/lib/fnc.c @@ -418,13 +418,15 @@ skip_close: return 0; } -static int flush_io (hawk_rtx_t* rtx, int rio, const hawk_ooch_t* name, int n) +static int flush_io (hawk_rtx_t* rtx, hawk_out_type_t out_type, const hawk_ooch_t* name, int n) { int n2; + hawk_rio_type_t rio; + rio = hawk_rtx_outtoriotype(rtx, out_type); if (rtx->rio.handler[rio]) { - n2 = hawk_rtx_flushio(rtx, rio, name); + n2 = hawk_rtx_flushio(rtx, out_type, name); if (n2 <= -1) { /* @@ -434,7 +436,7 @@ static int flush_io (hawk_rtx_t* rtx, int rio, const hawk_ooch_t* name, int n) if (n != 0) n = -2; } else n = -99; - */ + */ if (hawk_rtx_geterrnum(rtx) == HAWK_EIONMNF) { if (n != 0) n = -2; diff --git a/hawk/lib/hawk-prv.h b/hawk/lib/hawk-prv.h index f192d439..6f2ca193 100644 --- a/hawk/lib/hawk-prv.h +++ b/hawk/lib/hawk-prv.h @@ -173,16 +173,6 @@ typedef struct hawk_tree_t hawk_tree_t; #endif -enum hawk_rio_type_t -{ - /* rio types available */ - HAWK_RIO_PIPE, - HAWK_RIO_FILE, - HAWK_RIO_CONSOLE, - - /* reserved for internal use only */ - HAWK_RIO_NUM -}; struct hawk_tree_t { diff --git a/hawk/lib/mod-hawk.c b/hawk/lib/mod-hawk.c index e72f330a..4decd0cd 100644 --- a/hawk/lib/mod-hawk.c +++ b/hawk/lib/mod-hawk.c @@ -89,6 +89,7 @@ static hawk_oow_t push_args_from_stack (hawk_rtx_t* rtx, hawk_nde_fncall_t* call static int fnc_call (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) { +/* TODO: support to call a module function such as sys::getpid() */ hawk_fun_t* fun; hawk_oow_t nargs; hawk_nde_fncall_t call; @@ -126,24 +127,33 @@ static int fnc_call (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) return 0; } -/* hawk::function_exists("xxxx"); */ +/* hawk::function_exists("xxxx"); + * hawk::function_exists("sys::getpid") */ static int fnc_function_exists (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) { hawk_val_t* a0; - hawk_ooch_t* str; - hawk_oow_t len; + hawk_oocs_t name; int rx; a0 = hawk_rtx_getarg(rtx, 0); - str = hawk_rtx_getvaloocstr(rtx, a0, &len); - if (HAWK_UNLIKELY(!str)) + name.ptr = hawk_rtx_getvaloocstr(rtx, a0, &name.len); + if (HAWK_UNLIKELY(!name.ptr)) { rx = 0; } else { - rx = (hawk_rtx_findfunwithoocstr(rtx, str) != HAWK_NULL); - hawk_rtx_freevaloocstr (rtx, a0, str); + if (hawk_count_oocstr(name.ptr) != name.len) rx = 0; + else + { + rx = (hawk_rtx_findfunwithoocstr(rtx, name.ptr) != HAWK_NULL); + if (!rx) + { + hawk_mod_sym_t sym; + rx = (hawk_query_module_with_name(hawk_rtx_gethawk(rtx), &name, &sym) != HAWK_NULL); + } + } + hawk_rtx_freevaloocstr (rtx, a0, name.ptr); } hawk_rtx_setretval (rtx, hawk_rtx_makeintval(rtx, rx)); diff --git a/hawk/lib/parse-prv.h b/hawk/lib/parse-prv.h index 564004a8..295251d0 100644 --- a/hawk/lib/parse-prv.h +++ b/hawk/lib/parse-prv.h @@ -116,6 +116,13 @@ void hawk_clearsionames ( hawk_t* hawk ); + +hawk_mod_t* hawk_query_module_with_name ( + hawk_t* hawk, + const hawk_oocs_t* name, + hawk_mod_sym_t* sym +); + #if defined(__cplusplus) } #endif diff --git a/hawk/lib/parse.c b/hawk/lib/parse.c index 3a913cbe..59808927 100644 --- a/hawk/lib/parse.c +++ b/hawk/lib/parse.c @@ -254,7 +254,6 @@ static int flush_out (hawk_t* hawk); static hawk_mod_t* query_module (hawk_t* hawk, const hawk_oocs_t segs[], int nsegs, hawk_mod_sym_t* sym); typedef struct kwent_t kwent_t; - struct kwent_t { hawk_oocs_t name; @@ -3061,7 +3060,7 @@ static hawk_nde_t* parse_print (hawk_t* hawk, const hawk_loc_t* xloc) hawk_nde_print_t* nde; hawk_nde_t* args = HAWK_NULL; hawk_nde_t* out = HAWK_NULL; - int out_type; + hawk_out_type_t out_type; hawk_nde_type_t type; hawk_loc_t eloc; @@ -3224,7 +3223,6 @@ static hawk_nde_t* parse_print (hawk_t* hawk, const hawk_loc_t* xloc) out = ep->right; out_type = tab[i].out; - hawk_freemem (hawk, tmp); break; } @@ -7433,7 +7431,7 @@ static hawk_mod_t* query_module (hawk_t* hawk, const hawk_oocs_t segs[], int nse HAWK_ASSERT (nsegs == 2); - pair = hawk_rbt_search (hawk->modtab, segs[0].ptr, segs[0].len); + pair = hawk_rbt_search(hawk->modtab, segs[0].ptr, segs[0].len); if (pair) { mdp = (hawk_mod_data_t*)HAWK_RBT_VPTR(pair); @@ -7468,7 +7466,7 @@ static hawk_mod_t* query_module (hawk_t* hawk, const hawk_oocs_t segs[], int nse /* TODO: binary search ... */ for (n = 0; n < HAWK_COUNTOF(static_modtab); n++) { - if (hawk_comp_oocstr(static_modtab[n].modname, segs[0].ptr, 0) == 0) + if (hawk_comp_oochars_oocstr(segs[0].ptr, segs[0].len, static_modtab[n].modname) == 0) { load = static_modtab[n].modload; break; @@ -7491,7 +7489,7 @@ static hawk_mod_t* query_module (hawk_t* hawk, const hawk_oocs_t segs[], int nse /* i copy-insert 'md' into the table before calling 'load'. * to pass the same address to load(), query(), etc */ pair = hawk_rbt_insert(hawk->modtab, segs[0].ptr, segs[0].len, &md, HAWK_SIZEOF(md)); - if (pair == HAWK_NULL) return HAWK_NULL; + if (!pair) return HAWK_NULL; mdp = (hawk_mod_data_t*)HAWK_RBT_VPTR(pair); if (load(&mdp->mod, hawk) <= -1) @@ -7601,3 +7599,26 @@ done: } return &mdp->mod; } + + +hawk_mod_t* hawk_query_module_with_name (hawk_t* hawk, const hawk_oocs_t* name, hawk_mod_sym_t* sym) +{ + const hawk_ooch_t* dc; + hawk_oocs_t segs[2]; +/*TOOD: non-module builtin function? fnc? */ + + dc = hawk_find_oochars_in_oochars(name->ptr, name->len, HAWK_T("::"), 2, 0); + if (!dc) + { + hawk_seterrfmt (hawk, HAWK_NULL, HAWK_EINVAL, HAWK_T("invalid module name - %js"), name); + return HAWK_NULL; + } + + segs[0].ptr = name->ptr; + segs[0].len = dc - name->ptr; + + segs[1].ptr = segs[0].ptr + segs[0].len + 2; + segs[1].len = name->len - segs[0].len - 2; + + return query_module(hawk, segs, 2, sym); +} diff --git a/hawk/lib/rio-prv.h b/hawk/lib/rio-prv.h index f8ed8f9a..f7ec038b 100644 --- a/hawk/lib/rio-prv.h +++ b/hawk/lib/rio-prv.h @@ -27,38 +27,53 @@ #ifndef _HAWK_RIO_PRV_H_ #define _HAWK_RIO_PRV_H_ +enum hawk_rio_type_t +{ + /* rio types available */ + HAWK_RIO_PIPE = 0, + HAWK_RIO_FILE = 1, + HAWK_RIO_CONSOLE = 2, + + /* reserved for internal use only */ + HAWK_RIO_NUM +}; +typedef enum hawk_rio_type_t hawk_rio_type_t; + #if defined(__cplusplus) extern "C" { #endif +hawk_rio_type_t hawk_rtx_intoriotype (hawk_rtx_t* rtx, hawk_in_type_t in_type); +hawk_rio_type_t hawk_rtx_outtoriotype (hawk_rtx_t* rtx, hawk_in_type_t out_type); + int hawk_rtx_readio ( - hawk_rtx_t* rtx, int in_type, + hawk_rtx_t* rtx, hawk_in_type_t in_type, const hawk_ooch_t* name, hawk_ooecs_t* buf); int hawk_rtx_readiobytes ( - hawk_rtx_t* rtx, int in_type, + hawk_rtx_t* rtx, hawk_in_type_t in_type, const hawk_ooch_t* name, hawk_becs_t* buf); int hawk_rtx_writeioval ( - hawk_rtx_t* rtx, int out_type, + hawk_rtx_t* rtx, hawk_out_type_t out_type, const hawk_ooch_t* name, hawk_val_t* v); int hawk_rtx_writeiostr ( - hawk_rtx_t* rtx, int out_type, + hawk_rtx_t* rtx, hawk_out_type_t out_type, const hawk_ooch_t* name, hawk_ooch_t* str, hawk_oow_t len); int hawk_rtx_writeiobytes ( - hawk_rtx_t* rtx, int out_type, + hawk_rtx_t* rtx, hawk_out_type_t out_type, const hawk_ooch_t* name, hawk_bch_t* str, hawk_oow_t len); int hawk_rtx_flushio ( - hawk_rtx_t* rtx, int out_type, const hawk_ooch_t* name); + hawk_rtx_t* rtx, hawk_out_type_t out_type, const hawk_ooch_t* name); int hawk_rtx_nextio_read ( - hawk_rtx_t* rtx, int in_type, const hawk_ooch_t* name); + hawk_rtx_t* rtx, hawk_in_type_t in_type, const hawk_ooch_t* name); int hawk_rtx_nextio_write ( - hawk_rtx_t* rtx, int out_type, const hawk_ooch_t* name); + hawk_rtx_t* rtx, hawk_out_type_t out_type, const hawk_ooch_t* name); int hawk_rtx_closeio ( hawk_rtx_t* rtx, diff --git a/hawk/lib/rio.c b/hawk/lib/rio.c index 07855cc4..ae3303ba 100644 --- a/hawk/lib/rio.c +++ b/hawk/lib/rio.c @@ -34,7 +34,7 @@ enum io_mask_t IO_MASK_CLEAR = 0x00FF }; -static int in_type_map[] = +static hawk_rio_type_t in_type_map[] = { /* the order should match the order of the * HAWK_IN_XXX values in tree.h */ @@ -62,7 +62,7 @@ static int in_mask_map[] = IO_MASK_READ }; -static int out_type_map[] = +static hawk_rio_type_t out_type_map[] = { /* the order should match the order of the * HAWK_OUT_XXX values in tree.h */ @@ -93,8 +93,18 @@ static int out_mask_map[] = IO_MASK_WRITE }; +hawk_rio_type_t hawk_rtx_intoriotype (hawk_rtx_t* rtx, hawk_in_type_t in_type) +{ + return in_type_map[in_type]; +} + +hawk_rio_type_t hawk_rtx_outtoriotype (hawk_rtx_t* rtx, hawk_in_type_t out_type) +{ + return out_type_map[out_type]; +} + static int find_rio_in ( - hawk_rtx_t* rtx, int in_type, const hawk_ooch_t* name, + hawk_rtx_t* rtx, hawk_in_type_t in_type, const hawk_ooch_t* name, int mbs_if_new, hawk_rio_arg_t** rio, hawk_rio_impl_t* fun) { hawk_rio_arg_t* p = rtx->rio.chain; @@ -353,7 +363,7 @@ static HAWK_INLINE int match_long_brs(hawk_rtx_t* rtx, hawk_becs_t* buf, hawk_ri return ret; } -int hawk_rtx_readio (hawk_rtx_t* rtx, int in_type, const hawk_ooch_t* name, hawk_ooecs_t* buf) +int hawk_rtx_readio (hawk_rtx_t* rtx, hawk_in_type_t in_type, const hawk_ooch_t* name, hawk_ooecs_t* buf) { hawk_rio_arg_t* p; hawk_rio_impl_t handler; @@ -671,7 +681,7 @@ int hawk_rtx_readio (hawk_rtx_t* rtx, int in_type, const hawk_ooch_t* name, hawk } -int hawk_rtx_readiobytes (hawk_rtx_t* rtx, int in_type, const hawk_ooch_t* name, hawk_becs_t* buf) +int hawk_rtx_readiobytes (hawk_rtx_t* rtx, hawk_in_type_t in_type, const hawk_ooch_t* name, hawk_becs_t* buf) { hawk_rio_arg_t* p; hawk_rio_impl_t handler; @@ -989,7 +999,7 @@ int hawk_rtx_readiobytes (hawk_rtx_t* rtx, int in_type, const hawk_ooch_t* name, } -int hawk_rtx_writeioval (hawk_rtx_t* rtx, int out_type, const hawk_ooch_t* name, hawk_val_t* v) +int hawk_rtx_writeioval (hawk_rtx_t* rtx, hawk_out_type_t out_type, const hawk_ooch_t* name, hawk_val_t* v) { hawk_val_type_t vtype; vtype = HAWK_RTX_GETVALTYPE (rtx, v); @@ -1034,7 +1044,7 @@ struct write_io_data_t }; typedef struct write_io_data_t write_io_data_t; -static int prepare_for_write_io_data (hawk_rtx_t* rtx, int out_type, const hawk_ooch_t* name, write_io_data_t* wid) +static int prepare_for_write_io_data (hawk_rtx_t* rtx, hawk_out_type_t out_type, const hawk_ooch_t* name, write_io_data_t* wid) { hawk_rio_arg_t* p = rtx->rio.chain; hawk_rio_impl_t handler; @@ -1122,7 +1132,7 @@ static int prepare_for_write_io_data (hawk_rtx_t* rtx, int out_type, const hawk_ return 1; } -int hawk_rtx_writeiostr (hawk_rtx_t* rtx, int out_type, const hawk_ooch_t* name, hawk_ooch_t* str, hawk_oow_t len) +int hawk_rtx_writeiostr (hawk_rtx_t* rtx, hawk_out_type_t out_type, const hawk_ooch_t* name, hawk_ooch_t* str, hawk_oow_t len) { int x; write_io_data_t wid; @@ -1149,7 +1159,7 @@ int hawk_rtx_writeiostr (hawk_rtx_t* rtx, int out_type, const hawk_ooch_t* name, return 1; } -int hawk_rtx_writeiobytes (hawk_rtx_t* rtx, int out_type, const hawk_ooch_t* name, hawk_bch_t* str, hawk_oow_t len) +int hawk_rtx_writeiobytes (hawk_rtx_t* rtx, hawk_out_type_t out_type, const hawk_ooch_t* name, hawk_bch_t* str, hawk_oow_t len) { int x; write_io_data_t wid; @@ -1176,7 +1186,7 @@ int hawk_rtx_writeiobytes (hawk_rtx_t* rtx, int out_type, const hawk_ooch_t* nam return 1; } -int hawk_rtx_flushio (hawk_rtx_t* rtx, int out_type, const hawk_ooch_t* name) +int hawk_rtx_flushio (hawk_rtx_t* rtx, hawk_out_type_t out_type, const hawk_ooch_t* name) { hawk_rio_arg_t* p = rtx->rio.chain; hawk_rio_impl_t handler; @@ -1226,7 +1236,7 @@ int hawk_rtx_flushio (hawk_rtx_t* rtx, int out_type, const hawk_ooch_t* name) return -1; } -int hawk_rtx_nextio_read (hawk_rtx_t* rtx, int in_type, const hawk_ooch_t* name) +int hawk_rtx_nextio_read (hawk_rtx_t* rtx, hawk_in_type_t in_type, const hawk_ooch_t* name) { hawk_rio_arg_t* p = rtx->rio.chain; hawk_rio_impl_t handler; @@ -1295,7 +1305,7 @@ int hawk_rtx_nextio_read (hawk_rtx_t* rtx, int in_type, const hawk_ooch_t* name) } } -int hawk_rtx_nextio_write (hawk_rtx_t* rtx, int out_type, const hawk_ooch_t* name) +int hawk_rtx_nextio_write (hawk_rtx_t* rtx, hawk_out_type_t out_type, const hawk_ooch_t* name) { hawk_rio_arg_t* p = rtx->rio.chain; hawk_rio_impl_t handler; @@ -1360,7 +1370,7 @@ int hawk_rtx_nextio_write (hawk_rtx_t* rtx, int out_type, const hawk_ooch_t* nam } } -int hawk_rtx_closio_read (hawk_rtx_t* rtx, int in_type, const hawk_ooch_t* name) +int hawk_rtx_closio_read (hawk_rtx_t* rtx, hawk_in_type_t in_type, const hawk_ooch_t* name) { hawk_rio_arg_t* p = rtx->rio.chain, * px = HAWK_NULL; hawk_rio_impl_t handler; @@ -1417,7 +1427,7 @@ int hawk_rtx_closio_read (hawk_rtx_t* rtx, int in_type, const hawk_ooch_t* name) return -1; } -int hawk_rtx_closio_write (hawk_rtx_t* rtx, int out_type, const hawk_ooch_t* name) +int hawk_rtx_closio_write (hawk_rtx_t* rtx, hawk_out_type_t out_type, const hawk_ooch_t* name) { hawk_rio_arg_t* p = rtx->rio.chain, * px = HAWK_NULL; hawk_rio_impl_t handler; diff --git a/hawk/lib/run.c b/hawk/lib/run.c index 8bf2550c..2afdd92e 100644 --- a/hawk/lib/run.c +++ b/hawk/lib/run.c @@ -101,8 +101,8 @@ static int run_reset (hawk_rtx_t* rtx, hawk_nde_reset_t* nde); static int run_print (hawk_rtx_t* rtx, hawk_nde_print_t* nde); static int run_printf (hawk_rtx_t* rtx, hawk_nde_print_t* nde); -static int output_formatted (hawk_rtx_t* run, int out_type, const hawk_ooch_t* dst, const hawk_ooch_t* fmt, hawk_oow_t fmt_len, hawk_nde_t* args); -static int output_formatted_bytes (hawk_rtx_t* run, int out_type, const hawk_ooch_t* dst, const hawk_bch_t* fmt, hawk_oow_t fmt_len, hawk_nde_t* args); +static int output_formatted (hawk_rtx_t* rtx, hawk_out_type_t out_type, const hawk_ooch_t* dst, const hawk_ooch_t* fmt, hawk_oow_t fmt_len, hawk_nde_t* args); +static int output_formatted_bytes (hawk_rtx_t* rtx, hawk_out_type_t out_type, const hawk_ooch_t* dst, const hawk_bch_t* fmt, hawk_oow_t fmt_len, hawk_nde_t* args); static hawk_val_t* eval_expression (hawk_rtx_t* rtx, hawk_nde_t* nde); static hawk_val_t* eval_expression0 (hawk_rtx_t* rtx, hawk_nde_t* nde); @@ -3307,8 +3307,7 @@ static int run_print (hawk_rtx_t* rtx, hawk_nde_print_t* nde) /* check if print is followed by any arguments */ if (!nde->args) { - /* if it doesn't have any arguments, print the entire - * input record */ + /* if it doesn't have any arguments, print the entire input record */ n = hawk_rtx_writeiostr(rtx, nde->out_type, out.ptr, HAWK_OOECS_PTR(&rtx->inrec.line), HAWK_OOECS_LEN(&rtx->inrec.line)); if (n <= -1 /*&& rtx->errinf.num != HAWK_EIOIMPL*/) { @@ -3456,7 +3455,7 @@ static int run_printf (hawk_rtx_t* rtx, hawk_nde_print_t* nde) v = eval_expression(rtx, head); if (HAWK_UNLIKELY(!v)) goto oops_1; - + hawk_rtx_refupval (rtx, v); vtype = HAWK_RTX_GETVALTYPE(rtx, v); switch (vtype) @@ -3525,7 +3524,7 @@ oops_1: } static int output_formatted ( - hawk_rtx_t* rtx, int out_type, const hawk_ooch_t* dst, + hawk_rtx_t* rtx, hawk_out_type_t out_type, const hawk_ooch_t* dst, const hawk_ooch_t* fmt, hawk_oow_t fmt_len, hawk_nde_t* args) { hawk_ooch_t* ptr; @@ -3545,7 +3544,7 @@ static int output_formatted ( } static int output_formatted_bytes ( - hawk_rtx_t* rtx, int out_type, const hawk_ooch_t* dst, + hawk_rtx_t* rtx, hawk_out_type_t out_type, const hawk_ooch_t* dst, const hawk_bch_t* fmt, hawk_oow_t fmt_len, hawk_nde_t* args) { hawk_bch_t* ptr; diff --git a/hawk/lib/std.c b/hawk/lib/std.c index 1a25f4e1..1d4e3918 100644 --- a/hawk/lib/std.c +++ b/hawk/lib/std.c @@ -313,206 +313,254 @@ static void* std_mod_open_checked (hawk_t* hawk, const hawk_mod_spec_t* spec) void* hawk_stdmodopen (hawk_t* hawk, const hawk_mod_spec_t* spec) { #if defined(USE_LTDL) - void* h; - lt_dladvise adv; - hawk_bch_t* modpath; - const hawk_ooch_t* tmp[6]; - int count; - static hawk_ooch_t ds[] = { '/', '\0' }; - - count = 0; - if (spec->libdir) + if (spec) { - hawk_oow_t len; + void* h; + lt_dladvise adv; + hawk_bch_t* modpath; + const hawk_ooch_t* tmp[6]; + int count; + static hawk_ooch_t ds[] = { '/', '\0' }; - tmp[count++] = spec->libdir; - len = hawk_count_oocstr(spec->libdir); - if (len > 0 && spec->libdir[len - 1] != '/') tmp[count++] = ds; + count = 0; + if (spec->libdir) + { + hawk_oow_t len; + + tmp[count++] = spec->libdir; + len = hawk_count_oocstr(spec->libdir); + if (len > 0 && spec->libdir[len - 1] != '/') tmp[count++] = ds; + } + if (spec->prefix) tmp[count++] = spec->prefix; + tmp[count++] = spec->name; + if (spec->postfix) tmp[count++] = spec->postfix; + tmp[count] = HAWK_NULL; + + #if defined(HAWK_OOCH_IS_BCH) + modpath = hawk_dupbcstrarr(hawk, tmp, HAWK_NULL); + #else + modpath = hawk_dupucstrarrtobcstr(hawk, tmp, HAWK_NULL) + #endif + if (!modpath) return HAWK_NULL; + + if (lt_dladvise_init(&adv) != 0) + { + hawk_seterrfmt (hawk, HAWK_NULL, HAWK_ESYSERR, HAWK_T("%hs"), lt_dlerror()); + return HAWK_NULL; + } + + lt_dladvise_ext (&adv); + /*lt_dladvise_resident (&adv); useful for debugging with valgrind */ + + h = lt_dlopenadvise(modpath, adv); + if (!h) hawk_seterrfmt (hawk, HAWK_NULL, HAWK_ESYSERR, HAWK_T("%hs"), lt_dlerror()); + + lt_dladvise_destroy (&adv); + + if (modpath) hawk_freemem(hawk, modpath); + return h; } - if (spec->prefix) tmp[count++] = spec->prefix; - tmp[count++] = spec->name; - if (spec->postfix) tmp[count++] = spec->postfix; - tmp[count] = HAWK_NULL; - - #if defined(HAWK_OOCH_IS_BCH) - modpath = hawk_dupbcstrarr(hawk, tmp, HAWK_NULL); - #else - modpath = hawk_dupucstrarrtobcstr(hawk, tmp, HAWK_NULL) - #endif - if (!modpath) return HAWK_NULL; - - if (lt_dladvise_init(&adv) != 0) + else { - hawk_seterrfmt (hawk, HAWK_NULL, HAWK_ESYSERR, HAWK_T("%hs"), lt_dlerror()); - return HAWK_NULL; + void* h; + h = lt_dlopen(NULL); + if (HAWK_UNLIKELY(!h)) hawk_seterrfmt (hawk, HAWK_NULL, HAWK_ESYSERR, HAWK_T("%hs"), lt_dlerror()); + return h; } - lt_dladvise_ext (&adv); - /*lt_dladvise_resident (&adv); useful for debugging with valgrind */ - - h = lt_dlopenadvise(modpath, adv); - - lt_dladvise_destroy (&adv); - - if (modpath) hawk_freemem(hawk, modpath); - return h; - #elif defined(_WIN32) - HMODULE h; - hawk_ooch_t* modpath; - const hawk_ooch_t* tmp[6]; - int count; - static hawk_ooch_t ds[][2] = { { '\\', '\0' }, { '/', '\0' } } - false 0 treu 1 - count = 0; - if (spec->libdir) + if (spec) { - hawk_oow_t len; - - tmp[count++] = spec->libdir; - len = hawk_count_oocstr(spec->libdir); - if (len > 0 && (spec->libdir[len - 1] != '/' && spec->libdir[len - 1] != '\\')) + HMODULE h; + hawk_ooch_t* modpath; + const hawk_ooch_t* tmp[6]; + int count; + static hawk_ooch_t ds[][2] = { { '\\', '\0' }, { '/', '\0' } } + false 0 treu 1 + count = 0; + if (spec->libdir) { - tmp[count++] = ds[hawk_find_oochar_in_oocstr(spec->libdir, '/') != HAWK_NULL]; + hawk_oow_t len; + + tmp[count++] = spec->libdir; + len = hawk_count_oocstr(spec->libdir); + if (len > 0 && (spec->libdir[len - 1] != '/' && spec->libdir[len - 1] != '\\')) + { + tmp[count++] = ds[hawk_find_oochar_in_oocstr(spec->libdir, '/') != HAWK_NULL]; + } } + if (spec->prefix) tmp[count++] = spec->prefix; + tmp[count++] = spec->name; + if (spec->postfix) tmp[count++] = spec->postfix; + tmp[count] = HAWK_NULL; + + modpath = hawk_dupoocstrarr(hawk, tmp, HAWK_NULL); + if (!modpath) return HAWK_NULL; + + h = LoadLibrary(modpath); + if (!h) hawk_seterrnum (hawk, HAWK_NULL, hawk_syserr_to_errnum(GetLastError()); + + hawk_freemem (hawk, modpath); + + HAWK_ASSERT (HAWK_SIZEOF(h) <= HAWK_SIZEOF(void*)); + return h; + } + else + { + HMODULE h; + h = GetModuleHandle(NULL); + if (!h) hawk_seterrnum (hawk, HAWK_NULL, hawk_syserr_to_errnum(GetLastError()); + return h; } - if (spec->prefix) tmp[count++] = spec->prefix; - tmp[count++] = spec->name; - if (spec->postfix) tmp[count++] = spec->postfix; - tmp[count] = HAWK_NULL; - - modpath = hawk_dupoocstrarr(hawk, tmp, HAWK_NULL); - if (!modpath) return HAWK_NULL; - - h = LoadLibrary(modpath); - if (!h) hawk_seterrnum (hawk, HAWK_NULL, hawk_syserr_to_errnum(GetLastError()); - - hawk_freemem (hawk, modpath); - - HAWK_ASSERT (HAWK_SIZEOF(h) <= HAWK_SIZEOF(void*)); - return h; #elif defined(__OS2__) - HMODULE h; - hawk_bch_t* modpath; - const hawk_ooch_t* tmp[6]; - int count; - char errbuf[CCHMAXPATH]; - APIRET rc; - static hawk_ooch_t ds[] = { '\\', '\0' }; - - count = 0; - if (spec->libdir) + if (spec) { - hawk_oow_t len; + HMODULE h; + hawk_bch_t* modpath; + const hawk_ooch_t* tmp[6]; + int count; + char errbuf[CCHMAXPATH]; + APIRET rc; + static hawk_ooch_t ds[] = { '\\', '\0' }; - tmp[count++] = spec->libdir; - len = hawk_count_oocstr(spec->libdir); - if (len > 0 && spec->libdir[len - 1] != '\\') tmp[count++] = ds; + count = 0; + if (spec->libdir) + { + hawk_oow_t len; + + tmp[count++] = spec->libdir; + len = hawk_count_oocstr(spec->libdir); + if (len > 0 && spec->libdir[len - 1] != '\\') tmp[count++] = ds; + } + if (spec->prefix) tmp[count++] = spec->prefix; + tmp[count++] = spec->name; + if (spec->postfix) tmp[count++] = spec->postfix; + tmp[count] = HAWK_NULL; + + #if defined(HAWK_OOCH_IS_BCH) + modpath = hawk_dupbcstrarr(hawk, tmp, HAWK_NULL); + #else + modpath = hawk_dupucstrarrtobcstr(hawk, tmp, HAWK_NULL); + #endif + if (HAWK_UNLIKELY(!modpath)) return HAWK_NULL; + + /* DosLoadModule() seems to have severe limitation on + * the file name it can load (max-8-letters.xxx) */ + rc = DosLoadModule(errbuf, HAWK_COUNTOF(errbuf) - 1, modpath, &h); + if (rc != NO_ERROR) + { + h = HAWK_NULL; + hawk_seterrnum (hawk, HAWK_NULL, hawk_syserr_to_errnum(rc)); + } + + hawk_freemem (hawk, modpath); + + HAWK_ASSERT (HAWK_SIZEOF(h) <= HAWK_SIZEOF(void*)); + return h; } - if (spec->prefix) tmp[count++] = spec->prefix; - tmp[count++] = spec->name; - if (spec->postfix) tmp[count++] = spec->postfix; - tmp[count] = HAWK_NULL; - - #if defined(HAWK_OOCH_IS_BCH) - modpath = hawk_dupbcstrarr(hawk, tmp, HAWK_NULL); - #else - modpath = hawk_dupucstrarrtobcstr(hawk, tmp, HAWK_NULL); - #endif - if (!modpath) return HAWK_NULL; - - /* DosLoadModule() seems to have severe limitation on - * the file name it can load (max-8-letters.xxx) */ - rc = DosLoadModule(errbuf, HAWK_COUNTOF(errbuf) - 1, modpath, &h); - if (rc != NO_ERROR) + else { - h = HAWK_NULL; - hawk_seterrnum (hawk, HAWK_NULL, hawk_syserr_to_errnum(rc)); + hawk_seterrnum (hawk, HAWK_NULL, HAWK_ENOIMPL); + return HAWK_NULL; } - hawk_freemem (hawk, modpath); - - HAWK_ASSERT (HAWK_SIZEOF(h) <= HAWK_SIZEOF(void*)); - return h; - #elif defined(__DOS__) && defined(HAWK_ENABLE_DOS_DYNAMIC_MODULE) - /* the DOS code here is not generic enough. it's for a specific - * dos-extender only. the best is not to use dynamic loading - * when building for DOS. */ - void* h; - hawk_bch_t* modpath; - const hawk_ooch_t* tmp[6]; - int count; - static hawk_ooch_t ds[] = { '\\', '\0' }; - - count = 0; - if (spec->libdir) + if (spec) { - hawk_oow_t len; + /* the DOS code here is not generic enough. it's for a specific + * dos-extender only. the best is not to use dynamic loading + * when building for DOS. */ + void* h; + hawk_bch_t* modpath; + const hawk_ooch_t* tmp[6]; + int count; + static hawk_ooch_t ds[] = { '\\', '\0' }; - tmp[count++] = spec->libdir; - len = hawk_count_oocstr(spec->libdir); - if (len > 0 && spec->libdir[len - 1] != '/') tmp[count++] = ds; + count = 0; + if (spec->libdir) + { + hawk_oow_t len; + + tmp[count++] = spec->libdir; + len = hawk_count_oocstr(spec->libdir); + if (len > 0 && spec->libdir[len - 1] != '/') tmp[count++] = ds; + } + if (spec->prefix) tmp[count++] = spec->prefix; + tmp[count++] = spec->name; + if (spec->postfix) tmp[count++] = spec->postfix; + tmp[count] = HAWK_NULL; + + #if defined(HAWK_OOCH_IS_BCH) + modpath = hawk_dupbcstrarr(hawk, tmp, HAWK_NULL); + #else + modpath = hawk_dupucstrarrtobcstr(hawk, tmp, HAWK_NULL); + #endif + if (!modpath) return HAWK_NULL; + + h = LoadModule(modpath); + if (!h) hawk_seterrnum (hawk, HAWK_NULL, HAWK_ESYSERR); + + hawk_freemem (hawk, modpath); + + HAWK_ASSERT (HAWK_SIZEOF(h) <= HAWK_SIZEOF(void*)); + return h; + } + else + { + void* h; + h = GetModuleHandle(NULL); + if (!h) hawk_seterrnum (hawk, HAWK_NULL, HAWK_ESYSERR); + return h; } - if (spec->prefix) tmp[count++] = spec->prefix; - tmp[count++] = spec->name; - if (spec->postfix) tmp[count++] = spec->postfix; - tmp[count] = HAWK_NULL; - - #if defined(HAWK_OOCH_IS_BCH) - modpath = hawk_dupbcstrarr(hawk, tmp, HAWK_NULL); - #else - modpath = hawk_dupucstrarrtobcstr(hawk, tmp, HAWK_NULL); - #endif - if (!modpath) return HAWK_NULL; - - h = LoadModule(modpath); - if (!h) hawk_seterrnum (hawk, HAWK_NULL, HAWK_ESYSERR); - - hawk_freemem (hawk, modpath); - - HAWK_ASSERT (HAWK_SIZEOF(h) <= HAWK_SIZEOF(void*)); - return h; #elif defined(USE_DLFCN) - void* h; - hawk_bch_t* modpath; - const hawk_ooch_t* tmp[6]; - int count; - static hawk_ooch_t ds[] = { '/', '\0' }; - - count = 0; - if (spec->libdir) + if (spec) { - hawk_oow_t len; + void* h; + hawk_bch_t* modpath; + const hawk_ooch_t* tmp[6]; + int count; + static hawk_ooch_t ds[] = { '/', '\0' }; - tmp[count++] = spec->libdir; - len = hawk_count_oocstr(spec->libdir); - if (len > 0 && spec->libdir[len - 1] != '/') tmp[count++] = ds; + count = 0; + if (spec->libdir) + { + hawk_oow_t len; + + tmp[count++] = spec->libdir; + len = hawk_count_oocstr(spec->libdir); + if (len > 0 && spec->libdir[len - 1] != '/') tmp[count++] = ds; + } + if (spec->prefix) tmp[count++] = spec->prefix; + tmp[count++] = spec->name; + if (spec->postfix) tmp[count++] = spec->postfix; + tmp[count] = HAWK_NULL; + + #if defined(HAWK_OOCH_IS_BCH) + modpath = hawk_dupbcstrarr(hawk, tmp, HAWK_NULL); + #else + modpath = hawk_dupucstrarrtobcstr(hawk, tmp, HAWK_NULL); + #endif + if (!modpath) return HAWK_NULL; + + h = dlopen(modpath, RTLD_NOW | RTLD_LOCAL); + if (!h) hawk_seterrfmt (hawk, HAWK_NULL, HAWK_ESYSERR, HAWK_T("%hs"), dlerror()); + + hawk_freemem (hawk, modpath); + + return h; + } + else + { + void* h; + h = dlopen(NULL, RTLD_NOW | RTLD_LOCAL); + if (!h) hawk_seterrfmt (hawk, HAWK_NULL, HAWK_ESYSERR, HAWK_T("%hs"), dlerror()); + return h; } - if (spec->prefix) tmp[count++] = spec->prefix; - tmp[count++] = spec->name; - if (spec->postfix) tmp[count++] = spec->postfix; - tmp[count] = HAWK_NULL; - - #if defined(HAWK_OOCH_IS_BCH) - modpath = hawk_dupbcstrarr(hawk, tmp, HAWK_NULL); - #else - modpath = hawk_dupucstrarrtobcstr(hawk, tmp, HAWK_NULL); - #endif - if (!modpath) return HAWK_NULL; - - h = dlopen(modpath, RTLD_NOW | RTLD_LOCAL); - if (!h) hawk_seterrfmt (hawk, HAWK_NULL, HAWK_ESYSERR, HAWK_T("%hs"), dlerror()); - - hawk_freemem (hawk, modpath); - - return h; - #else hawk_seterrnum (hawk, HAWK_NULL, HAWK_ENOIMPL); return HAWK_NULL; @@ -524,11 +572,11 @@ void hawk_stdmodclose (hawk_t* hawk, void* handle) #if defined(USE_LTDL) lt_dlclose (handle); #elif defined(_WIN32) - FreeLibrary ((HMODULE)handle); + if (handle != GetModuleHandle(NULL)) FreeLibrary ((HMODULE)handle); #elif defined(__OS2__) DosFreeModule ((HMODULE)handle); #elif defined(__DOS__) && defined(HAWK_ENABLE_DOS_DYNAMIC_MODULE) - FreeModule (handle); + if (handle != GetModuleHandle(NULL)) FreeModule (handle); #elif defined(USE_DLFCN) dlclose (handle); #else diff --git a/hawk/lib/tree-prv.h b/hawk/lib/tree-prv.h index e00ca84a..22f27f3d 100644 --- a/hawk/lib/tree-prv.h +++ b/hawk/lib/tree-prv.h @@ -27,6 +27,7 @@ #ifndef _HAWK_TREE_PRV_H_ #define _HAWK_TREE_PRV_H_ + enum hawk_in_type_t { /* the order of these values match @@ -37,6 +38,8 @@ enum hawk_in_type_t HAWK_IN_FILE, HAWK_IN_CONSOLE }; +typedef enum hawk_in_type_t hawk_in_type_t; + enum hawk_out_type_t { @@ -49,6 +52,7 @@ enum hawk_out_type_t HAWK_OUT_APFILE, /* file for appending */ HAWK_OUT_CONSOLE }; +typedef enum hawk_out_type_t hawk_out_type_t; typedef struct hawk_nde_blk_t hawk_nde_blk_t; typedef struct hawk_nde_grp_t hawk_nde_grp_t; @@ -74,7 +78,7 @@ typedef struct hawk_nde_getline_t hawk_nde_getline_t; typedef struct hawk_nde_if_t hawk_nde_if_t; typedef struct hawk_nde_while_t hawk_nde_while_t; typedef struct hawk_nde_for_t hawk_nde_for_t; -typedef struct hawk_nde_forin_t hawk_nde_forin_t; +typedef struct hawk_nde_forin_t hawk_nde_forin_t; typedef struct hawk_nde_break_t hawk_nde_break_t; typedef struct hawk_nde_continue_t hawk_nde_continue_t; typedef struct hawk_nde_return_t hawk_nde_return_t; @@ -258,7 +262,7 @@ struct hawk_nde_getline_t HAWK_NDE_HDR; hawk_nde_t* var; int mbs; - int in_type; /* HAWK_IN_CONSOLE, HAWK_IN_FILE, etc */ + hawk_in_type_t in_type; /* HAWK_IN_CONSOLE, HAWK_IN_FILE, etc */ hawk_nde_t* in; }; @@ -356,7 +360,7 @@ struct hawk_nde_print_t { HAWK_NDE_HDR; hawk_nde_t* args; - int out_type; /* HAWK_OUT_XXX */ + hawk_out_type_t out_type; /* HAWK_OUT_XXX */ hawk_nde_t* out; }; diff --git a/hawk/mod/Makefile.in b/hawk/mod/Makefile.in index b4985644..b4ccaac3 100644 --- a/hawk/mod/Makefile.in +++ b/hawk/mod/Makefile.in @@ -398,7 +398,6 @@ pdfdir = @pdfdir@ prefix = @prefix@ program_transform_name = @program_transform_name@ psdir = @psdir@ -runstatedir = @runstatedir@ sbindir = @sbindir@ sharedstatedir = @sharedstatedir@ srcdir = @srcdir@ diff --git a/hawk/mod/mod-ffi.c b/hawk/mod/mod-ffi.c index 67c3c086..93ff9706 100644 --- a/hawk/mod/mod-ffi.c +++ b/hawk/mod/mod-ffi.c @@ -194,21 +194,28 @@ static ffi_node_t* new_ffi_node (hawk_rtx_t* rtx, ffi_list_t* ffi_list, const ha { hawk_t* hawk = hawk_rtx_gethawk(rtx); ffi_node_t* ffi_node; - hawk_mod_spec_t spec; + void* handle; ffi_node = __new_ffi_node(rtx, ffi_list); if (!ffi_node) return HAWK_NULL; - HAWK_MEMSET (&spec, 0, HAWK_SIZEOF(spec)); - spec.name = name; - - handle = hawk->prm.modopen(hawk, &spec); + if (name) + { + hawk_mod_spec_t spec; + HAWK_MEMSET (&spec, 0, HAWK_SIZEOF(spec)); + spec.name = name; + handle = hawk->prm.modopen(hawk, &spec); + } + else + { + handle = hawk->prm.modopen(hawk, HAWK_NULL); + } if (!handle) { const hawk_ooch_t* olderrmsg; olderrmsg = hawk_backuperrmsg(hawk); - hawk_rtx_seterrfmt (rtx, HAWK_NULL, HAWK_ENOMEM, HAWK_T("unable to open the '%js' ffi module - %js"), name, olderrmsg); + hawk_rtx_seterrfmt (rtx, HAWK_NULL, HAWK_ENOMEM, HAWK_T("unable to open the '%js' ffi module - %js"), (name? name: HAWK_T("")), olderrmsg); __free_ffi_node (rtx, ffi_list, ffi_node); return HAWK_NULL; @@ -318,18 +325,26 @@ static int fnc_open (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) ffi_list = rtx_to_ffi_list(rtx, fi); - a0 = hawk_rtx_getarg(rtx, 0); - name.ptr = hawk_rtx_getvaloocstr(rtx, a0, &name.len); - if (HAWK_UNLIKELY(!name.ptr)) + if (hawk_rtx_getnargs(rtx) >= 1) { - ret = copy_error_to_ffi_list (rtx, ffi_list); - goto done; - } + a0 = hawk_rtx_getarg(rtx, 0); + name.ptr = hawk_rtx_getvaloocstr(rtx, a0, &name.len); + if (HAWK_UNLIKELY(!name.ptr)) + { + ret = copy_error_to_ffi_list (rtx, ffi_list); + goto done; + } - if (name.len != hawk_count_oocstr(name.ptr)) + if (name.len != hawk_count_oocstr(name.ptr)) + { + ret = set_error_on_ffi_list(rtx, ffi_list, HAWK_EINVAL, HAWK_T("invalid ffi module name '%.*js'"), name.len, name.ptr); + goto done; + } + } + else { - ret = set_error_on_ffi_list(rtx, ffi_list, HAWK_EINVAL, HAWK_T("invalid ffi module name '%.*js'"), name.len, name.ptr); - goto done; + name.ptr = HAWK_NULL; + a0 = HAWK_NULL; } ffi_node = new_ffi_node(rtx, ffi_list, name.ptr); @@ -623,7 +638,7 @@ oops: return -1; } -static int capture_return (hawk_rtx_t* rtx, ffi_list_t* ffi_list, ffi_node_t* ffi_node, hawk_ooch_t fmtc, int _unsigned, hawk_int_t* rx) +static int capture_return (hawk_rtx_t* rtx, ffi_list_t* ffi_list, ffi_node_t* ffi_node, hawk_ooch_t fmtc, int _unsigned, hawk_oow_t refidx, hawk_int_t* rx) { hawk_val_t* r; ffi_t* ffi = &ffi_node->ffi; @@ -646,7 +661,7 @@ static int capture_return (hawk_rtx_t* rtx, ffi_list_t* ffi_list, ffi_node_t* ff case FMTC_INT64: #if (HAWK_SIZEOF_INT64_T > 0) - r = hawk_rtx_makeintval(rtx, _unsigned? ffi->ret_sv.ui64: ffi->ret_sv.i64); + r = hawk_rtx_makeintval(rtx, _unsigned? ffi->ret_sv.ui64: ffi->ret_sv.i64); break; #else hawk_rtx_seterrbfmt (hawk, HAWK_NULL, HAWK_EINVAL, "invalid return type signature - %jc", fmtc); @@ -688,7 +703,7 @@ static int capture_return (hawk_rtx_t* rtx, ffi_list_t* ffi_list, ffi_node_t* ff } if (HAWK_UNLIKELY(!r)) goto oops; - if (hawk_rtx_setrefval(rtx, (hawk_val_ref_t*)hawk_rtx_getarg(rtx, 1), r) <= -1) goto oops; + if (hawk_rtx_setrefval(rtx, (hawk_val_ref_t*)hawk_rtx_getarg(rtx, refidx), r) <= -1) goto oops; return 0; oops: @@ -733,8 +748,8 @@ static int fnc_call (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) hawk_t* hawk = hawk_rtx_gethawk(rtx); ffi_list_t* ffi_list; ffi_node_t* ffi_node; - ffi_t* ffi = HAWK_NULL; - hawk_int_t ret = -1; + ffi_t* ffi; + hawk_int_t ret; hawk_val_t* a2; hawk_oocs_t fun; @@ -749,6 +764,15 @@ static int fnc_call (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) ffi_status fs; +#define FNC_CALL_ARG_BASE 4 + + ret = -1; + ffi = HAWK_NULL; + fun.ptr = HAWK_NULL; + sig.ptr = HAWK_NULL; + a2 = HAWK_NULL; + a3 = HAWK_NULL; + /* ffi::call (ffi-handle, return-value, "function name", "signature", argument....); */ ffi_list = rtx_to_ffi_list(rtx, fi); ffi_node = get_ffi_list_node_with_arg(rtx, ffi_list, hawk_rtx_getarg(rtx, 0), &ret); @@ -762,6 +786,12 @@ static int fnc_call (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) goto done; } + if (hawk_count_oocstr(fun.ptr) != fun.len) + { + ret = set_error_on_ffi_list(rtx, ffi_list, HAWK_EINVAL, HAWK_T("invalid function name - %.*js"), fun.len, fun.ptr); + goto done; + } + a3 = hawk_rtx_getarg(rtx, 3); sig.ptr = hawk_rtx_getvaloocstr(rtx, a3, &sig.len); if (HAWK_UNLIKELY(!sig.ptr)) @@ -770,12 +800,6 @@ static int fnc_call (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) goto done; } - if (hawk_count_oocstr(fun.ptr) != fun.len) - { - ret = set_error_on_ffi_list(rtx, ffi_list, HAWK_EINVAL, HAWK_T("invalid function name - %.*js"), fun.len, fun.ptr); - goto done; - } - ffi = &ffi_node->ffi; ffi->arg_count = 0; @@ -815,13 +839,13 @@ static int fnc_call (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) } /* more items in signature than the actual argument */ - if (j >= hawk_rtx_getnargs(rtx) - 4) + if (j >= hawk_rtx_getnargs(rtx) - FNC_CALL_ARG_BASE) { ret = set_error_on_ffi_list(rtx, ffi_list, HAWK_EINVAL, HAWK_T("two few arguments for the argument signature")); goto done; } - if (add_ffi_arg(rtx, ffi_list, ffi_node, fmtc, _unsigned, hawk_rtx_getarg(rtx, j + 4), &ret) <= -1) goto done; + if (add_ffi_arg(rtx, ffi_list, ffi_node, fmtc, _unsigned, hawk_rtx_getarg(rtx, j + FNC_CALL_ARG_BASE), &ret) <= -1) goto done; _unsigned = 0; j++; } @@ -854,7 +878,7 @@ static int fnc_call (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) ffi_call (&ffi->cif, FFI_FN(funx), &ffi->ret_sv, ffi->arg_values); - if (fmtc != FMTC_NULL && capture_return(rtx, ffi_list, ffi_node, fmtc, _unsigned, &ret) <= -1) goto done; + if (fmtc != FMTC_NULL && capture_return(rtx, ffi_list, ffi_node, fmtc, _unsigned, 1, &ret) <= -1) goto done; ret = 0; done: @@ -869,6 +893,9 @@ done: } } + if (sig.ptr) hawk_rtx_freevaloocstr(rtx, a3, sig.ptr); + if (fun.ptr) hawk_rtx_freevaloocstr(rtx, a2, fun.ptr); + hawk_rtx_setretval (rtx, hawk_rtx_makeintval(rtx, ret)); return 0; } @@ -891,13 +918,10 @@ static int fnc_errmsg (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) static hawk_mod_fnc_tab_t fnctab[] = { -/* - * TODO: implement ffi::_call() that calls in the current module without open... - { HAWK_T("_call"), { { 3, A_MAX, HAWK_T("rv") }, fnc_call, 0 } }, */ { HAWK_T("call"), { { 4, A_MAX, HAWK_T("vrv") }, fnc_call, 0 } }, { HAWK_T("close"), { { 1, 1, HAWK_NULL }, fnc_close, 0 } }, { HAWK_T("errmsg"), { { 0, 0, HAWK_NULL }, fnc_errmsg, 0 } }, - { HAWK_T("open"), { { 1, 1, HAWK_NULL }, fnc_open, 0 } }, + { HAWK_T("open"), { { 0, 1, HAWK_NULL }, fnc_open, 0 } }, }; /* ------------------------------------------------------------------------ */ diff --git a/hawk/samples/Makefile.in b/hawk/samples/Makefile.in index 62e199ef..e2515d70 100644 --- a/hawk/samples/Makefile.in +++ b/hawk/samples/Makefile.in @@ -351,7 +351,6 @@ pdfdir = @pdfdir@ prefix = @prefix@ program_transform_name = @program_transform_name@ psdir = @psdir@ -runstatedir = @runstatedir@ sbindir = @sbindir@ sharedstatedir = @sharedstatedir@ srcdir = @srcdir@ diff --git a/hawk/t/Makefile.in b/hawk/t/Makefile.in index 88dcf61c..7ffd0539 100644 --- a/hawk/t/Makefile.in +++ b/hawk/t/Makefile.in @@ -557,7 +557,6 @@ pdfdir = @pdfdir@ prefix = @prefix@ program_transform_name = @program_transform_name@ psdir = @psdir@ -runstatedir = @runstatedir@ sbindir = @sbindir@ sharedstatedir = @sharedstatedir@ srcdir = @srcdir@ diff --git a/hawk/tools/Makefile.in b/hawk/tools/Makefile.in index d8eb9b32..d6d3f2f4 100644 --- a/hawk/tools/Makefile.in +++ b/hawk/tools/Makefile.in @@ -328,7 +328,6 @@ pdfdir = @pdfdir@ prefix = @prefix@ program_transform_name = @program_transform_name@ psdir = @psdir@ -runstatedir = @runstatedir@ sbindir = @sbindir@ sharedstatedir = @sharedstatedir@ srcdir = @srcdir@