From 015ed09c5a948c759a41832d4df4e735111d727a Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Tue, 3 Mar 2020 08:03:04 +0000 Subject: [PATCH] added a new global variable SCRIPTNAME which intends to store the first script name loaded --- hawk/bin/main.c | 23 ++++++++++++++++++++++- hawk/lib/Hawk.cpp | 17 +++++++++++++++-- hawk/lib/Hawk.hpp | 5 +++-- hawk/lib/HawkStd.cpp | 12 ++++-------- hawk/lib/hawk.h | 30 ++++++++++++++++++++++++------ hawk/lib/parse.c | 1 + hawk/lib/run.c | 35 +++++++++++++++++++++++++++++++++-- hawk/lib/std.c | 12 ++++++------ hawk/samples/hawk51.cpp | 8 +++++++- hawk/scripts/tcp-proxy.awk | 3 ++- 10 files changed, 117 insertions(+), 29 deletions(-) diff --git a/hawk/bin/main.c b/hawk/bin/main.c index 122ec66c..9cc66fc8 100644 --- a/hawk/bin/main.c +++ b/hawk/bin/main.c @@ -373,6 +373,8 @@ static int add_gvs_to_awk (hawk_t* awk, arg_t* arg) static int apply_fs_and_gvs_to_rtx (hawk_rtx_t* rtx, arg_t* arg) { + hawk_oow_t i; + if (arg->fs) { hawk_val_t* fs; @@ -407,6 +409,25 @@ static int apply_fs_and_gvs_to_rtx (hawk_rtx_t* rtx, arg_t* arg) } } + for (i = 0; arg->psin[i].type != HAWK_PARSESTD_NULL; i++) + { + if (arg->psin[i].type == HAWK_PARSESTD_FILE) + { + if (hawk_rtx_setscriptnamewithoochars(rtx, arg->psin[i].u.file.path, hawk_count_oocstr(arg->psin[i].u.file.path)) <= -1) return -1; + break; + } + else if (arg->psin[i].type == HAWK_PARSESTD_FILEB) + { + if (hawk_rtx_setscriptnamewithbchars(rtx, arg->psin[i].u.fileb.path, hawk_count_bcstr(arg->psin[i].u.fileb.path)) <= -1) return -1; + break; + } + else if (arg->psin[i].type == HAWK_PARSESTD_FILEU) + { + if (hawk_rtx_setscriptnamewithuchars(rtx, arg->psin[i].u.fileu.path, hawk_count_ucstr(arg->psin[i].u.fileu.path)) <= -1) return -1; + break; + } + } + return 0; } @@ -1180,7 +1201,7 @@ static HAWK_INLINE int execute_hawk (int argc, hawk_bch_t* argv[]) } rtx = hawk_rtx_openstdwithbcstr( - awk, 0, "hawk", + awk, 0, argv[0], (arg.call? HAWK_NULL: arg.icf.ptr), /* console input */ arg.ocf.ptr, /* console output */ arg.console_cmgr diff --git a/hawk/lib/Hawk.cpp b/hawk/lib/Hawk.cpp index b3362620..344d0b9d 100644 --- a/hawk/lib/Hawk.cpp +++ b/hawk/lib/Hawk.cpp @@ -1295,11 +1295,24 @@ int Hawk::Run::setGlobal (int id, hawk_flt_t v) return n; } -int Hawk::Run::setGlobal (int id, const hawk_ooch_t* ptr, hawk_oow_t len) +int Hawk::Run::setGlobal (int id, const hawk_uch_t* ptr, hawk_oow_t len) { HAWK_ASSERT (this->rtx != HAWK_NULL); - hawk_val_t* tmp = hawk_rtx_makestrvalwithoochars(this->rtx, ptr, len); + hawk_val_t* tmp = hawk_rtx_makestrvalwithuchars(this->rtx, ptr, len); + if (tmp == HAWK_NULL) return -1; + + hawk_rtx_refupval (this->rtx, tmp); + int n = hawk_rtx_setgbl (this->rtx, id, tmp); + hawk_rtx_refdownval (this->rtx, tmp); + return n; +} + +int Hawk::Run::setGlobal (int id, const hawk_bch_t* ptr, hawk_oow_t len) +{ + HAWK_ASSERT (this->rtx != HAWK_NULL); + + hawk_val_t* tmp = hawk_rtx_makestrvalwithbchars(this->rtx, ptr, len); if (tmp == HAWK_NULL) return -1; hawk_rtx_refupval (this->rtx, tmp); diff --git a/hawk/lib/Hawk.hpp b/hawk/lib/Hawk.hpp index fb415bf6..12d1d15c 100644 --- a/hawk/lib/Hawk.hpp +++ b/hawk/lib/Hawk.hpp @@ -1238,7 +1238,8 @@ public: /// \a ptr. /// \return 0 on success, -1 on failure /// - int setGlobal (int id, const hawk_ooch_t* ptr, hawk_oow_t len); + int setGlobal (int id, const hawk_uch_t* ptr, hawk_oow_t len); + int setGlobal (int id, const hawk_bch_t* ptr, hawk_oow_t len); /// /// The setGlobal() function sets a global variable @@ -1251,7 +1252,7 @@ public: /// The getGlobal() function gets the value of a global /// variable identified by \a id and stores it in \a v. /// \return 0 on success, -1 on failure - /// + /// int getGlobal (int id, Value& v) const; protected: diff --git a/hawk/lib/HawkStd.cpp b/hawk/lib/HawkStd.cpp index 886e4b5a..2990ca8d 100644 --- a/hawk/lib/HawkStd.cpp +++ b/hawk/lib/HawkStd.cpp @@ -235,7 +235,7 @@ HawkStd::Run* HawkStd::parse (Source& in, Source& out) } if (run && make_additional_globals(run) <= -1) return HAWK_NULL; - + return run; } @@ -245,10 +245,7 @@ int HawkStd::build_argcv (Run* run) for (hawk_oow_t i = 0; i < this->runarg.len; i++) { - if (argv.setIndexedStr ( - Value::IntIndex(i), - this->runarg.ptr[i].ptr, - this->runarg.ptr[i].len, true) <= -1) return -1; + if (argv.setIndexedStr(Value::IntIndex(i), this->runarg.ptr[i].ptr, this->runarg.ptr[i].len, true) <= -1) return -1; } run->setGlobal (this->gbl_argc, (hawk_int_t)this->runarg.len); @@ -344,9 +341,8 @@ int HawkStd::make_additional_globals (Run* run) { /* TODO: use wenviron where it's available */ - if (build_argcv(run) <= -1 || - build_environ(run, ::environ) <= -1) return -1; - + if (this->build_argcv(run) <= -1 || this->build_environ(run, ::environ) <= -1) return -1; + return 0; } diff --git a/hawk/lib/hawk.h b/hawk/lib/hawk.h index fd975d27..df46c7db 100644 --- a/hawk/lib/hawk.h +++ b/hawk/lib/hawk.h @@ -57,12 +57,11 @@ * hawk_sio_cbs_t sio; // need to initialize it with callback functions * hawk_rio_cbs_t rio; // need to initialize it with callback functions * - * awk = hawk_open (mmgr, 0, prm); // create an interpreter + * awk = hawk_open(mmgr, 0, prm); // create an interpreter * hawk_parse (awk, &sio); // parse a script * rtx = hawk_rtx_open (awk, 0, &rio); // create a runtime context * retv = hawk_rtx_loop (rtx); // run a standard AWK loop - * if (retv != HAWK_NULL) - * hawk_rtx_refdownval (rtx, retv); // free return value + * if (retv) hawk_rtx_refdownval (rtx, retv); // free return value * hawk_rtx_close (rtx); // destroy the runtime context * hawk_close (awk); // destroy the interpreter * \endcode @@ -1315,6 +1314,7 @@ enum hawk_gbl_id_t HAWK_GBL_RLENGTH, HAWK_GBL_RS, HAWK_GBL_RSTART, + HAWK_GBL_SCRIPTNAME, HAWK_GBL_STRIPRECSPC, HAWK_GBL_SUBSEP, @@ -2072,7 +2072,7 @@ static HAWK_INLINE void hawk_rtx_setcmgr (hawk_rtx_t* rtx, hawk_cmgr_t* cmgr) { * * The example shows typical usage of the function. * \code - * rtx = hawk_rtx_open (awk, 0, rio); + * rtx = hawk_rtx_open(awk, 0, rio); * if (rtx) * { * retv = hawk_rtx_loop (rtx); @@ -2133,7 +2133,7 @@ HAWK_EXPORT hawk_val_t* hawk_rtx_callfun ( * * The example shows typical usage of the function. * \code - * rtx = hawk_rtx_open (awk, 0, rio); + * rtx = hawk_rtx_open(awk, 0, rio); * if (rtx) * { * v = hawk_rtx_callwithucstr (rtx, HAWK_UT("init"), HAWK_NULL, 0); @@ -2161,7 +2161,7 @@ HAWK_EXPORT hawk_val_t* hawk_rtx_callwithucstr ( * * The example shows typical usage of the function. * \code - * rtx = hawk_rtx_open (awk, 0, rio); + * rtx = hawk_rtx_open(awk, 0, rio); * if (rtx) * { * v = hawk_rtx_callwithbcstr (rtx, HAWK_BT("init"), HAWK_NULL, 0); @@ -2381,6 +2381,24 @@ HAWK_EXPORT int hawk_rtx_setofilename ( hawk_oow_t len /**< name length */ ); +HAWK_EXPORT int hawk_rtx_setscriptnamewithuchars ( + hawk_rtx_t* rtx, /**< runtime context */ + const hawk_uch_t* str, /**< name pointer */ + hawk_oow_t len /**< name length */ +); + +HAWK_EXPORT int hawk_rtx_setscriptnamewithbchars ( + hawk_rtx_t* rtx, /**< runtime context */ + const hawk_bch_t* str, /**< name pointer */ + hawk_oow_t len /**< name length */ +); + +#if defined(HAWK_OOCH_IS_UCH) +# define hawk_rtx_setscriptnamewithoochars hawk_rtx_setscriptnamewithuchars +#else +# define hawk_rtx_setscriptnamewithoochars hawk_rtx_setscriptnamewithbchars +#endif + /** * The hawk_rtx_getnvmap() gets the map of named variables */ diff --git a/hawk/lib/parse.c b/hawk/lib/parse.c index e4154c05..7d33357b 100644 --- a/hawk/lib/parse.c +++ b/hawk/lib/parse.c @@ -367,6 +367,7 @@ static global_t gtab[] = { HAWK_T("RSTART"), 6, 0 }, + { HAWK_T("SCRIPTNAME"), 10, 0 }, /* it decides the field construction behavior when FS is a regular expression and * the field splitter is composed of whitespaces only. e.g) FS="[ \t]*"; * if set to a non-zero value, remove leading spaces and trailing spaces off a record diff --git a/hawk/lib/run.c b/hawk/lib/run.c index 138d14e0..aa7c5798 100644 --- a/hawk/lib/run.c +++ b/hawk/lib/run.c @@ -633,6 +633,36 @@ HAWK_INLINE int hawk_rtx_setgbl (hawk_rtx_t* rtx, int id, hawk_val_t* val) return set_global(rtx, id, HAWK_NULL, val, 0); } +int hawk_rtx_setscriptnamewithuchars (hawk_rtx_t* rtx, const hawk_uch_t* name, hawk_oow_t len) +{ + hawk_val_t* tmp; + int n; + + tmp = hawk_rtx_makestrvalwithuchars(rtx, name, len); + if (tmp == HAWK_NULL) return -1; + + hawk_rtx_refupval (rtx, tmp); + n = hawk_rtx_setgbl (rtx, HAWK_GBL_SCRIPTNAME, tmp); + hawk_rtx_refdownval (rtx, tmp); + + return n; +} + +int hawk_rtx_setscriptnamewithbchars (hawk_rtx_t* rtx, const hawk_bch_t* name, hawk_oow_t len) +{ + hawk_val_t* tmp; + int n; + + tmp = hawk_rtx_makestrvalwithbchars(rtx, name, len); + if (tmp == HAWK_NULL) return -1; + + hawk_rtx_refupval (rtx, tmp); + n = hawk_rtx_setgbl (rtx, HAWK_GBL_SCRIPTNAME, tmp); + hawk_rtx_refdownval (rtx, tmp); + + return n; +} + int hawk_rtx_setfilename (hawk_rtx_t* rtx, const hawk_ooch_t* name, hawk_oow_t len) { hawk_val_t* tmp; @@ -1205,7 +1235,7 @@ static int defaultify_globals (hawk_rtx_t* rtx) int idx; const hawk_ooch_t* str[2]; }; - static struct gtab_t gtab[7] = + static struct gtab_t gtab[8] = { { HAWK_GBL_CONVFMT, { DEFAULT_CONVFMT, DEFAULT_CONVFMT } }, { HAWK_GBL_FILENAME, { HAWK_NULL, HAWK_NULL } }, @@ -1213,7 +1243,8 @@ static int defaultify_globals (hawk_rtx_t* rtx) { HAWK_GBL_OFMT, { DEFAULT_OFMT, DEFAULT_OFMT } }, { HAWK_GBL_OFS, { DEFAULT_OFS, DEFAULT_OFS } }, { HAWK_GBL_ORS, { DEFAULT_ORS, DEFAULT_ORS_CRLF } }, - { HAWK_GBL_SUBSEP, { DEFAULT_SUBSEP, DEFAULT_SUBSEP } }, + { HAWK_GBL_SCRIPTNAME, { HAWK_NULL, HAWK_NULL } }, + { HAWK_GBL_SUBSEP, { DEFAULT_SUBSEP, DEFAULT_SUBSEP } } }; hawk_val_t* tmp; diff --git a/hawk/lib/std.c b/hawk/lib/std.c index 7ed52cb1..b15da5b3 100644 --- a/hawk/lib/std.c +++ b/hawk/lib/std.c @@ -2109,8 +2109,8 @@ static int open_rio_console (hawk_rtx_t* rtx, hawk_rio_arg_t* riod) file = as.ptr; sio = (file[0] == HAWK_T('-') && file[1] == HAWK_T('\0'))? - open_sio_std_rtx (rtx, HAWK_SIO_STDIN, HAWK_SIO_READ | HAWK_SIO_IGNOREECERR): - open_sio_rtx (rtx, file, HAWK_SIO_READ | HAWK_SIO_IGNOREECERR); + open_sio_std_rtx(rtx, HAWK_SIO_STDIN, HAWK_SIO_READ | HAWK_SIO_IGNOREECERR): + open_sio_rtx(rtx, file, HAWK_SIO_READ | HAWK_SIO_IGNOREECERR); if (sio == HAWK_NULL) { hawk_rtx_freevaloocstr (rtx, v, as.ptr); @@ -2332,7 +2332,7 @@ static int build_argcv (hawk_rtx_t* rtx, int argc_id, int argv_id, const hawk_oo hawk_ooch_t key[HAWK_SIZEOF(hawk_int_t)*8+2]; hawk_oow_t key_len; - v_argv = hawk_rtx_makemapval (rtx); + v_argv = hawk_rtx_makemapval(rtx); if (v_argv == HAWK_NULL) return -1; hawk_rtx_refupval (rtx, v_argv); @@ -2350,7 +2350,7 @@ static int build_argcv (hawk_rtx_t* rtx, int argc_id, int argv_id, const hawk_oo hawk_rtx_refupval (rtx, v_tmp); key_len = hawk_copy_oocstr(key, HAWK_COUNTOF(key), HAWK_T("0")); - if (hawk_htb_upsert (((hawk_val_map_t*)v_argv)->map, key, key_len, v_tmp, 0) == HAWK_NULL) + if (hawk_htb_upsert(((hawk_val_map_t*)v_argv)->map, key, key_len, v_tmp, 0) == HAWK_NULL) { /* if the assignment operation fails, decrements * the reference of v_tmp to free it */ @@ -2377,12 +2377,12 @@ static int build_argcv (hawk_rtx_t* rtx, int argc_id, int argv_id, const hawk_oo return -1; } - key_len = hawk_int_to_oocstr (argc, 10, HAWK_NULL, key, HAWK_COUNTOF(key)); + key_len = hawk_int_to_oocstr(argc, 10, HAWK_NULL, key, HAWK_COUNTOF(key)); HAWK_ASSERT (key_len != (hawk_oow_t)-1); hawk_rtx_refupval (rtx, v_tmp); - if (hawk_htb_upsert (((hawk_val_map_t*)v_argv)->map, key, key_len, v_tmp, 0) == HAWK_NULL) + if (hawk_htb_upsert(((hawk_val_map_t*)v_argv)->map, key, key_len, v_tmp, 0) == HAWK_NULL) { hawk_rtx_refdownval (rtx, v_tmp); hawk_rtx_refdownval (rtx, v_argv); diff --git a/hawk/samples/hawk51.cpp b/hawk/samples/hawk51.cpp index 071d0e33..cb7233eb 100644 --- a/hawk/samples/hawk51.cpp +++ b/hawk/samples/hawk51.cpp @@ -418,7 +418,13 @@ static int hawk_main (MyHawk& hawk, int argc, hawk_bch_t* argv[]) in = (cmdline.ins)? (MyHawk::Source*)&in_str: (MyHawk::Source*)&in_file; out = (cmdline.outf)? (MyHawk::Source*)&out_file: &MyHawk::Source::NONE; run = hawk.parse(*in, *out); - if (run == HAWK_NULL) + if (!run) + { + print_error (hawk); + return -1; + } + + if (cmdline.inf && run->setGlobal(HAWK_GBL_SCRIPTNAME, cmdline.inf, hawk_count_bcstr(cmdline.inf)) <= -1) { print_error (hawk); return -1; diff --git a/hawk/scripts/tcp-proxy.awk b/hawk/scripts/tcp-proxy.awk index c0daac53..d146ad2e 100644 --- a/hawk/scripts/tcp-proxy.awk +++ b/hawk/scripts/tcp-proxy.awk @@ -278,7 +278,8 @@ function main (localaddr, remoteaddr, c) if (ARGC != 3) { - printf ("Usage: %s local-address remote-address\n", ARGV[0]); ## TODO: add SCRIPTNAME to hawk interpreter + ###printf ("Usage: %s -f %s local-address remote-address\n", ARGV[0], SCRIPTNAME); + printf ("Usage: %s local-address remote-address\n", SCRIPTNAME); return -1; }