From ddcdd3904eb1a4948afa8c8b007eec5f0d1b92c2 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Tue, 17 Mar 2020 08:12:01 +0000 Subject: [PATCH] enhanced hawk_makemapvalwithdata() to handle integers of various size enhanced sys::stat() --- hawk/README.md | 10 +++++ hawk/configure | 2 +- hawk/configure.ac | 2 +- hawk/lib/hawk.h | 4 +- hawk/lib/mod-sys.c | 106 +++++++++++++++++++++++++++++++++++++++------ hawk/lib/val.c | 18 +++++++- 6 files changed, 124 insertions(+), 18 deletions(-) diff --git a/hawk/README.md b/hawk/README.md index 1e29d29a..913af8c1 100644 --- a/hawk/README.md +++ b/hawk/README.md @@ -526,6 +526,16 @@ Directory traversal is easy. } } + +You can get information of a network interface. + + BEGIN { + if (sys::getnwifcfg("lo", sys::NWIFCFG_IN6, x) <= -1) + print sys::errmsg(); + else + for (i in x) print i, x[i]; + } + Socket functions are available. BEGIN diff --git a/hawk/configure b/hawk/configure index 795a917b..cc96e709 100755 --- a/hawk/configure +++ b/hawk/configure @@ -1490,7 +1490,7 @@ Optional Features: no:4 (default. yes) --enable-debug compile the code in the debug mode (default. no) --enable-intmax use hawk_intmax_t for integers (default. no) - --enable-awk-fltmax use hawk_fltmax_t for floating-point numbers + --enable-fltmax use hawk_fltmax_t for floating-point numbers (default. no) --enable-cxx build the library for C++ if a C++ compiler is available (default. yes) diff --git a/hawk/configure.ac b/hawk/configure.ac index ee44b6ec..a1d7ef05 100644 --- a/hawk/configure.ac +++ b/hawk/configure.ac @@ -863,7 +863,7 @@ fi dnl ===== enable-fltmax ====== AC_ARG_ENABLE([fltmax], - [AS_HELP_STRING([--enable-awk-fltmax],[use hawk_fltmax_t for floating-point numbers (default. no)])], + [AS_HELP_STRING([--enable-fltmax],[use hawk_fltmax_t for floating-point numbers (default. no)])], enable_fltmax_is=$enableval, enable_fltmax_is=no ) diff --git a/hawk/lib/hawk.h b/hawk/lib/hawk.h index 5c3bdd2f..ace19064 100644 --- a/hawk/lib/hawk.h +++ b/hawk/lib/hawk.h @@ -322,7 +322,9 @@ typedef enum hawk_val_map_data_type_t hawk_val_map_data_type_t; struct hawk_val_map_data_t { hawk_oocs_t key; - hawk_val_map_data_type_t type; + hawk_val_map_data_type_t type: 8; + unsigned int type_size: 16; /* size information that supplements the type */ + /* maybe there are some unused bits here as this struct is not packed */ void* vptr; }; diff --git a/hawk/lib/mod-sys.c b/hawk/lib/mod-sys.c index 421b71ab..19f430e8 100644 --- a/hawk/lib/mod-sys.c +++ b/hawk/lib/mod-sys.c @@ -1998,7 +1998,7 @@ static int fnc_gettime (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) hawk_val_t* retv; hawk_ntime_t now; - if (hawk_get_time (&now) <= -1) now.sec = 0; + if (hawk_get_time(&now) <= -1) now.sec = 0; retv = hawk_rtx_makeintval(rtx, now.sec); if (!retv) return -1; @@ -2016,7 +2016,7 @@ static int fnc_settime (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) now.nsec = 0; - if (hawk_rtx_valtoint(rtx, hawk_rtx_getarg (rtx, 0), &tmp) <= -1) rx = -1; + if (hawk_rtx_valtoint(rtx, hawk_rtx_getarg(rtx, 0), &tmp) <= -1) rx = -1; else { now.sec = tmp; @@ -2460,14 +2460,14 @@ static int fnc_getifcfg (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) md[0].key.ptr = HAWK_T("index"); md[0].key.len = 5; md[0].type = HAWK_VAL_MAP_DATA_INT; - index = cfg.index; - md[0].vptr = &index; + md[0].type_size = HAWK_SIZEOF(cfg.index); + md[0].vptr = &cfg.index; md[1].key.ptr = HAWK_T("mtu"); md[1].key.len = 3; md[1].type = HAWK_VAL_MAP_DATA_INT; - mtu = cfg.mtu; - md[1].vptr = &mtu; + md[1].type_size = HAWK_SIZEOF(cfg.mtu); + md[1].vptr = &cfg.mtu; md[2].key.ptr = HAWK_T("addr"); md[2].key.len = 4; @@ -2981,8 +2981,8 @@ static int fnc_stat (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) hawk_oow_t len1; hawk_val_t* tmp; hawk_stat_t stbuf; - hawk_val_map_data_t md[10]; - hawk_int_t dev, ino, size; + hawk_val_map_data_t md[14]; + hawk_flt_t atime_f, mtime_f, ctime_f; int x; str1 = hawk_rtx_getvalbcstr(rtx, a0, &len1); @@ -3012,20 +3012,98 @@ static int fnc_stat (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) md[0].key.ptr = HAWK_T("dev"); md[0].key.len = 3; md[0].type = HAWK_VAL_MAP_DATA_INT; - dev = stbuf.st_dev; - md[0].vptr = &dev; + md[0].type_size = HAWK_SIZEOF(stbuf.st_dev); + md[0].vptr = &stbuf.st_dev; md[1].key.ptr = HAWK_T("ino"); md[1].key.len = 3; md[1].type = HAWK_VAL_MAP_DATA_INT; - ino = stbuf.st_ino; - md[1].vptr = &ino; + md[1].type_size = HAWK_SIZEOF(stbuf.st_ino); + md[1].vptr = &stbuf.st_ino; md[2].key.ptr = HAWK_T("size"); md[2].key.len = 4; + md[2].type = HAWK_VAL_MAP_DATA_INT; + md[2].type_size = HAWK_SIZEOF(stbuf.st_size); + md[2].vptr = &stbuf.st_size; + + md[3].key.ptr = HAWK_T("mode"); + md[3].key.len = 4; md[3].type = HAWK_VAL_MAP_DATA_INT; - size = stbuf.st_size; - md[2].vptr = &size; + md[3].type_size = HAWK_SIZEOF(stbuf.st_mode); + md[3].vptr = &stbuf.st_mode; + + md[4].key.ptr = HAWK_T("nlink"); + md[4].key.len = 5; + md[4].type = HAWK_VAL_MAP_DATA_INT; + md[4].type_size = HAWK_SIZEOF(stbuf.st_nlink); + md[4].vptr = &stbuf.st_nlink; + + md[5].key.ptr = HAWK_T("uid"); + md[5].key.len = 3; + md[5].type = HAWK_VAL_MAP_DATA_INT; + md[5].type_size = HAWK_SIZEOF(stbuf.st_uid); + md[5].vptr = &stbuf.st_uid; + + md[6].key.ptr = HAWK_T("gid"); + md[6].key.len = 3; + md[6].type = HAWK_VAL_MAP_DATA_INT; + md[6].type_size = HAWK_SIZEOF(stbuf.st_gid); + md[6].vptr = &stbuf.st_gid; + + md[7].key.ptr = HAWK_T("rdev"); + md[7].key.len = 4; + md[7].type = HAWK_VAL_MAP_DATA_INT; + md[7].type_size = HAWK_SIZEOF(stbuf.st_rdev); + md[7].vptr = &stbuf.st_rdev; + + md[8].key.ptr = HAWK_T("blksize"); + md[8].key.len = 7; + md[8].type = HAWK_VAL_MAP_DATA_INT; + md[8].type_size = HAWK_SIZEOF(stbuf.st_blksize); + md[8].vptr = &stbuf.st_blksize; + + md[9].key.ptr = HAWK_T("blocks"); + md[9].key.len = 6; + md[9].type = HAWK_VAL_MAP_DATA_INT; + md[9].type_size = HAWK_SIZEOF(stbuf.st_blocks); + md[9].vptr = &stbuf.st_blocks; + + md[10].key.ptr = HAWK_T("atime"); + md[10].key.len = 5; + #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC) + md[10].type = HAWK_VAL_MAP_DATA_FLT; + atime_f = (hawk_flt_t)stbuf.st_atim.tv_sec + ((hawk_flt_t)stbuf.st_atim.tv_nsec / HAWK_NSECS_PER_SEC); + md[10].vptr = &atime_f; + #else + md[10].type = HAWK_VAL_MAP_DATA_INT; + md[10].type_size = HAWK_SIZEOF(stbuf.st_atime); + md[10].vptr = &stbuf.st_atime; + #endif + + md[11].key.ptr = HAWK_T("mtime"); + md[11].key.len = 5; + #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC) + md[11].type = HAWK_VAL_MAP_DATA_FLT; + mtime_f = (hawk_flt_t)stbuf.st_mtim.tv_sec + ((hawk_flt_t)stbuf.st_mtim.tv_nsec / HAWK_NSECS_PER_SEC); + md[11].vptr = &mtime_f; + #else + md[11].type = HAWK_VAL_MAP_DATA_INT; + md[11].type_size = HAWK_SIZEOF(stbuf.st_mtime); + md[11].vptr = &stbuf.st_mtime; + #endif + + md[12].key.ptr = HAWK_T("ctime"); + md[12].key.len = 5; + #if defined(HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC) + md[12].type = HAWK_VAL_MAP_DATA_FLT; + ctime_f = (hawk_flt_t)stbuf.st_ctim.tv_sec + ((hawk_flt_t)stbuf.st_ctim.tv_nsec / HAWK_NSECS_PER_SEC); + md[12].vptr = &ctime_f; + #else + md[12].type = HAWK_VAL_MAP_DATA_INT; + md[12].type_size = HAWK_SIZEOF(stbuf.st_ctime); + md[12].vptr = &stbuf.st_ctime; + #endif tmp = hawk_rtx_makemapvalwithdata(rtx, md); if (!tmp) goto fail; diff --git a/hawk/lib/val.c b/hawk/lib/val.c index 53f0fd7f..690a3537 100644 --- a/hawk/lib/val.c +++ b/hawk/lib/val.c @@ -588,8 +588,24 @@ hawk_val_t* hawk_rtx_makemapvalwithdata (hawk_rtx_t* rtx, hawk_val_map_data_t da switch (p->type) { case HAWK_VAL_MAP_DATA_INT: - tmp = hawk_rtx_makeintval(rtx, *(hawk_int_t*)p->vptr); + { + hawk_int_t iv = 0; + + if (p->type_size > 0 && p->type_size <= HAWK_SIZEOF(iv)) + { + #if defined(HAWK_ENDIAN_LITTLE) + HAWK_MEMCPY (&iv, p->vptr, p->type_size); + #else + HAWK_MEMCPY ((hawk_uint8_t*)&iv + (HAWK_SIZEOF(iv) - p->type_size), p->vptr, p->type_size); + #endif + } + else + { + iv = *(hawk_int_t*)p->vptr; + } + tmp = hawk_rtx_makeintval(rtx, iv); break; + } case HAWK_VAL_MAP_DATA_FLT: tmp = hawk_rtx_makefltval(rtx, *(hawk_flt_t*)p->vptr);