added ase_gettime() and ase_settime(), also added many builtin functions to ase_awk_opensimple()
This commit is contained in:
		| @ -1,5 +1,5 @@ | ||||
| /* | ||||
|  * $Id: awk.c 496 2008-12-15 09:56:48Z baconevi $ | ||||
|  * $Id: awk.c 499 2008-12-16 09:42:48Z baconevi $ | ||||
|  */ | ||||
|  | ||||
| #include <ase/awk/awk.h> | ||||
| @ -14,7 +14,6 @@ | ||||
| #include <string.h> | ||||
| #include <signal.h> | ||||
| #include <stdarg.h> | ||||
| #include <math.h> | ||||
| #include <stdlib.h> | ||||
|  | ||||
| #define ABORT(label) goto label | ||||
| @ -34,16 +33,21 @@ | ||||
| 	#include <unistd.h> | ||||
| #endif | ||||
|  | ||||
| static ase_awk_t* app_awk = NULL; | ||||
| static ase_awk_run_t* app_run = NULL; | ||||
| static int app_debug = 0; | ||||
|  | ||||
| static void dprint (const ase_char_t* fmt, ...) | ||||
| { | ||||
| 	va_list ap; | ||||
| 	va_start (ap, fmt); | ||||
| 	ase_vfprintf (stderr, fmt, ap); | ||||
| 	va_end (ap); | ||||
| 	if (app_debug) | ||||
| 	{ | ||||
| 		va_list ap; | ||||
| 		va_start (ap, fmt); | ||||
| 		ase_vfprintf (stderr, fmt, ap); | ||||
| 		va_end (ap); | ||||
| 	} | ||||
| } | ||||
|  | ||||
| ase_awk_t* app_awk = NULL; | ||||
| ase_awk_run_t* app_run = NULL; | ||||
|  | ||||
| #ifdef _WIN32 | ||||
| static BOOL WINAPI stop_run (DWORD ctrl_type) | ||||
| @ -176,9 +180,11 @@ static void print_usage (const ase_char_t* argv0) | ||||
| 	ase_printf (ASE_T("Usage: %s [options] -f sourcefile [ -- ] [datafile]*\n"), argv0); | ||||
| 	ase_printf (ASE_T("       %s [options] [ -- ] sourcestring [datafile]*\n"), argv0); | ||||
| 	ase_printf (ASE_T("Where options are:\n")); | ||||
| 	ase_printf (ASE_T(" -f sourcefile   --file=sourcefile\n")); | ||||
| 	ase_printf (ASE_T(" -d deparsedfile --deparsed-file=deparsedfile\n")); | ||||
| 	ase_printf (ASE_T(" -F string       --field-separator=string\n")); | ||||
| 	ase_printf (ASE_T(" -h                                print this message\n")); | ||||
| 	ase_printf (ASE_T(" -d                                show extra information\n")); | ||||
| 	ase_printf (ASE_T(" -f/--file            sourcefile   set the source script file\n")); | ||||
| 	ase_printf (ASE_T(" -o/--deparsed-file   deparsedfile set the deparsing output file\n")); | ||||
| 	ase_printf (ASE_T(" -F/--field-separator string       set a field separator(FS)\n")); | ||||
|  | ||||
| 	ase_printf (ASE_T("\nYou may specify the following options to change the behavior of the interpreter.\n")); | ||||
| 	for (j = 0; j < ASE_COUNTOF(otab); j++) | ||||
| @ -263,7 +269,7 @@ static int handle_args (int argc, ase_char_t* argv[], struct argout_t* ao) | ||||
| 		{ ASE_T(":main"),            ASE_T('m') }, | ||||
| 		{ ASE_T(":file"),            ASE_T('f') }, | ||||
| 		{ ASE_T(":field-separator"), ASE_T('F') }, | ||||
| 		{ ASE_T(":deparsed-file"),   ASE_T('d') }, | ||||
| 		{ ASE_T(":deparsed-file"),   ASE_T('o') }, | ||||
| 		{ ASE_T(":assign"),          ASE_T('v') }, | ||||
|  | ||||
| 		{ ASE_T("help"),             ASE_T('h') } | ||||
| @ -271,7 +277,7 @@ static int handle_args (int argc, ase_char_t* argv[], struct argout_t* ao) | ||||
|  | ||||
| 	static ase_opt_t opt =  | ||||
| 	{ | ||||
| 		ASE_T("hm:f:F:d:v:"), | ||||
| 		ASE_T("hdm:f:F:o:v:"), | ||||
| 		lng | ||||
| 	}; | ||||
|  | ||||
| @ -321,6 +327,12 @@ static int handle_args (int argc, ase_char_t* argv[], struct argout_t* ao) | ||||
| 				if (vm != ASE_NULL) ase_map_close (vm); | ||||
| 				return 1; | ||||
|  | ||||
| 			case ASE_T('d'): | ||||
| 			{ | ||||
| 				app_debug = 1; | ||||
| 				break; | ||||
| 			} | ||||
|  | ||||
| 			case ASE_T('f'): | ||||
| 			{ | ||||
| 				if (isfl >= isfc-1) /* -1 for last ASE_NULL */ | ||||
| @ -347,7 +359,7 @@ static int handle_args (int argc, ase_char_t* argv[], struct argout_t* ao) | ||||
| 				break; | ||||
| 			} | ||||
|  | ||||
| 			case ASE_T('d'): | ||||
| 			case ASE_T('o'): | ||||
| 			{ | ||||
| 				osf = opt.arg; | ||||
| 				break; | ||||
|  | ||||
							
								
								
									
										310
									
								
								ase/configure
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										310
									
								
								ase/configure
									
									
									
									
										vendored
									
									
								
							| @ -21425,6 +21425,316 @@ fi | ||||
| done | ||||
|  | ||||
|  | ||||
| OLDLIBS="$LIBS" | ||||
| LIBS="$LIBM $LIBS" | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
| for ac_func in powl sinl cosl tanl atanl atan2l logl expl sqrtl | ||||
| do | ||||
| as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh` | ||||
| { echo "$as_me:$LINENO: checking for $ac_func" >&5 | ||||
| echo $ECHO_N "checking for $ac_func... $ECHO_C" >&6; } | ||||
| if { as_var=$as_ac_var; eval "test \"\${$as_var+set}\" = set"; }; then | ||||
|   echo $ECHO_N "(cached) $ECHO_C" >&6 | ||||
| else | ||||
|   cat >conftest.$ac_ext <<_ACEOF | ||||
| /* confdefs.h.  */ | ||||
| _ACEOF | ||||
| cat confdefs.h >>conftest.$ac_ext | ||||
| cat >>conftest.$ac_ext <<_ACEOF | ||||
| /* end confdefs.h.  */ | ||||
| /* Define $ac_func to an innocuous variant, in case <limits.h> declares $ac_func. | ||||
|    For example, HP-UX 11i <limits.h> declares gettimeofday.  */ | ||||
| #define $ac_func innocuous_$ac_func | ||||
|  | ||||
| /* System header to define __stub macros and hopefully few prototypes, | ||||
|     which can conflict with char $ac_func (); below. | ||||
|     Prefer <limits.h> to <assert.h> if __STDC__ is defined, since | ||||
|     <limits.h> exists even on freestanding compilers.  */ | ||||
|  | ||||
| #ifdef __STDC__ | ||||
| # include <limits.h> | ||||
| #else | ||||
| # include <assert.h> | ||||
| #endif | ||||
|  | ||||
| #undef $ac_func | ||||
|  | ||||
| /* Override any GCC internal prototype to avoid an error. | ||||
|    Use char because int might match the return type of a GCC | ||||
|    builtin and then its argument prototype would still apply.  */ | ||||
| #ifdef __cplusplus | ||||
| extern "C" | ||||
| #endif | ||||
| char $ac_func (); | ||||
| /* The GNU C library defines this for functions which it implements | ||||
|     to always fail with ENOSYS.  Some functions are actually named | ||||
|     something starting with __ and the normal name is an alias.  */ | ||||
| #if defined __stub_$ac_func || defined __stub___$ac_func | ||||
| choke me | ||||
| #endif | ||||
|  | ||||
| int | ||||
| main () | ||||
| { | ||||
| return $ac_func (); | ||||
|   ; | ||||
|   return 0; | ||||
| } | ||||
| _ACEOF | ||||
| rm -f conftest.$ac_objext conftest$ac_exeext | ||||
| if { (ac_try="$ac_link" | ||||
| case "(($ac_try" in | ||||
|   *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;; | ||||
|   *) ac_try_echo=$ac_try;; | ||||
| esac | ||||
| eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5 | ||||
|   (eval "$ac_link") 2>conftest.er1 | ||||
|   ac_status=$? | ||||
|   grep -v '^ *+' conftest.er1 >conftest.err | ||||
|   rm -f conftest.er1 | ||||
|   cat conftest.err >&5 | ||||
|   echo "$as_me:$LINENO: \$? = $ac_status" >&5 | ||||
|   (exit $ac_status); } && { | ||||
| 	 test -z "$ac_c_werror_flag" || | ||||
| 	 test ! -s conftest.err | ||||
|        } && test -s conftest$ac_exeext && | ||||
|        $as_test_x conftest$ac_exeext; then | ||||
|   eval "$as_ac_var=yes" | ||||
| else | ||||
|   echo "$as_me: failed program was:" >&5 | ||||
| sed 's/^/| /' conftest.$ac_ext >&5 | ||||
|  | ||||
| 	eval "$as_ac_var=no" | ||||
| fi | ||||
|  | ||||
| rm -f core conftest.err conftest.$ac_objext conftest_ipa8_conftest.oo \ | ||||
|       conftest$ac_exeext conftest.$ac_ext | ||||
| fi | ||||
| ac_res=`eval echo '${'$as_ac_var'}'` | ||||
| 	       { echo "$as_me:$LINENO: result: $ac_res" >&5 | ||||
| echo "${ECHO_T}$ac_res" >&6; } | ||||
| if test `eval echo '${'$as_ac_var'}'` = yes; then | ||||
|   cat >>confdefs.h <<_ACEOF | ||||
| #define `echo "HAVE_$ac_func" | $as_tr_cpp` 1 | ||||
| _ACEOF | ||||
|  | ||||
| fi | ||||
| done | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
| for ac_func in pow sin cos tan atan atan2 log exp sqrt | ||||
| do | ||||
| as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh` | ||||
| { echo "$as_me:$LINENO: checking for $ac_func" >&5 | ||||
| echo $ECHO_N "checking for $ac_func... $ECHO_C" >&6; } | ||||
| if { as_var=$as_ac_var; eval "test \"\${$as_var+set}\" = set"; }; then | ||||
|   echo $ECHO_N "(cached) $ECHO_C" >&6 | ||||
| else | ||||
|   cat >conftest.$ac_ext <<_ACEOF | ||||
| /* confdefs.h.  */ | ||||
| _ACEOF | ||||
| cat confdefs.h >>conftest.$ac_ext | ||||
| cat >>conftest.$ac_ext <<_ACEOF | ||||
| /* end confdefs.h.  */ | ||||
| /* Define $ac_func to an innocuous variant, in case <limits.h> declares $ac_func. | ||||
|    For example, HP-UX 11i <limits.h> declares gettimeofday.  */ | ||||
| #define $ac_func innocuous_$ac_func | ||||
|  | ||||
| /* System header to define __stub macros and hopefully few prototypes, | ||||
|     which can conflict with char $ac_func (); below. | ||||
|     Prefer <limits.h> to <assert.h> if __STDC__ is defined, since | ||||
|     <limits.h> exists even on freestanding compilers.  */ | ||||
|  | ||||
| #ifdef __STDC__ | ||||
| # include <limits.h> | ||||
| #else | ||||
| # include <assert.h> | ||||
| #endif | ||||
|  | ||||
| #undef $ac_func | ||||
|  | ||||
| /* Override any GCC internal prototype to avoid an error. | ||||
|    Use char because int might match the return type of a GCC | ||||
|    builtin and then its argument prototype would still apply.  */ | ||||
| #ifdef __cplusplus | ||||
| extern "C" | ||||
| #endif | ||||
| char $ac_func (); | ||||
| /* The GNU C library defines this for functions which it implements | ||||
|     to always fail with ENOSYS.  Some functions are actually named | ||||
|     something starting with __ and the normal name is an alias.  */ | ||||
| #if defined __stub_$ac_func || defined __stub___$ac_func | ||||
| choke me | ||||
| #endif | ||||
|  | ||||
| int | ||||
| main () | ||||
| { | ||||
| return $ac_func (); | ||||
|   ; | ||||
|   return 0; | ||||
| } | ||||
| _ACEOF | ||||
| rm -f conftest.$ac_objext conftest$ac_exeext | ||||
| if { (ac_try="$ac_link" | ||||
| case "(($ac_try" in | ||||
|   *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;; | ||||
|   *) ac_try_echo=$ac_try;; | ||||
| esac | ||||
| eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5 | ||||
|   (eval "$ac_link") 2>conftest.er1 | ||||
|   ac_status=$? | ||||
|   grep -v '^ *+' conftest.er1 >conftest.err | ||||
|   rm -f conftest.er1 | ||||
|   cat conftest.err >&5 | ||||
|   echo "$as_me:$LINENO: \$? = $ac_status" >&5 | ||||
|   (exit $ac_status); } && { | ||||
| 	 test -z "$ac_c_werror_flag" || | ||||
| 	 test ! -s conftest.err | ||||
|        } && test -s conftest$ac_exeext && | ||||
|        $as_test_x conftest$ac_exeext; then | ||||
|   eval "$as_ac_var=yes" | ||||
| else | ||||
|   echo "$as_me: failed program was:" >&5 | ||||
| sed 's/^/| /' conftest.$ac_ext >&5 | ||||
|  | ||||
| 	eval "$as_ac_var=no" | ||||
| fi | ||||
|  | ||||
| rm -f core conftest.err conftest.$ac_objext conftest_ipa8_conftest.oo \ | ||||
|       conftest$ac_exeext conftest.$ac_ext | ||||
| fi | ||||
| ac_res=`eval echo '${'$as_ac_var'}'` | ||||
| 	       { echo "$as_me:$LINENO: result: $ac_res" >&5 | ||||
| echo "${ECHO_T}$ac_res" >&6; } | ||||
| if test `eval echo '${'$as_ac_var'}'` = yes; then | ||||
|   cat >>confdefs.h <<_ACEOF | ||||
| #define `echo "HAVE_$ac_func" | $as_tr_cpp` 1 | ||||
| _ACEOF | ||||
|  | ||||
| fi | ||||
| done | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
|  | ||||
| for ac_func in powf sinf cosf tanf atanf atan2f logf expf sqrtf | ||||
| do | ||||
| as_ac_var=`echo "ac_cv_func_$ac_func" | $as_tr_sh` | ||||
| { echo "$as_me:$LINENO: checking for $ac_func" >&5 | ||||
| echo $ECHO_N "checking for $ac_func... $ECHO_C" >&6; } | ||||
| if { as_var=$as_ac_var; eval "test \"\${$as_var+set}\" = set"; }; then | ||||
|   echo $ECHO_N "(cached) $ECHO_C" >&6 | ||||
| else | ||||
|   cat >conftest.$ac_ext <<_ACEOF | ||||
| /* confdefs.h.  */ | ||||
| _ACEOF | ||||
| cat confdefs.h >>conftest.$ac_ext | ||||
| cat >>conftest.$ac_ext <<_ACEOF | ||||
| /* end confdefs.h.  */ | ||||
| /* Define $ac_func to an innocuous variant, in case <limits.h> declares $ac_func. | ||||
|    For example, HP-UX 11i <limits.h> declares gettimeofday.  */ | ||||
| #define $ac_func innocuous_$ac_func | ||||
|  | ||||
| /* System header to define __stub macros and hopefully few prototypes, | ||||
|     which can conflict with char $ac_func (); below. | ||||
|     Prefer <limits.h> to <assert.h> if __STDC__ is defined, since | ||||
|     <limits.h> exists even on freestanding compilers.  */ | ||||
|  | ||||
| #ifdef __STDC__ | ||||
| # include <limits.h> | ||||
| #else | ||||
| # include <assert.h> | ||||
| #endif | ||||
|  | ||||
| #undef $ac_func | ||||
|  | ||||
| /* Override any GCC internal prototype to avoid an error. | ||||
|    Use char because int might match the return type of a GCC | ||||
|    builtin and then its argument prototype would still apply.  */ | ||||
| #ifdef __cplusplus | ||||
| extern "C" | ||||
| #endif | ||||
| char $ac_func (); | ||||
| /* The GNU C library defines this for functions which it implements | ||||
|     to always fail with ENOSYS.  Some functions are actually named | ||||
|     something starting with __ and the normal name is an alias.  */ | ||||
| #if defined __stub_$ac_func || defined __stub___$ac_func | ||||
| choke me | ||||
| #endif | ||||
|  | ||||
| int | ||||
| main () | ||||
| { | ||||
| return $ac_func (); | ||||
|   ; | ||||
|   return 0; | ||||
| } | ||||
| _ACEOF | ||||
| rm -f conftest.$ac_objext conftest$ac_exeext | ||||
| if { (ac_try="$ac_link" | ||||
| case "(($ac_try" in | ||||
|   *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;; | ||||
|   *) ac_try_echo=$ac_try;; | ||||
| esac | ||||
| eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5 | ||||
|   (eval "$ac_link") 2>conftest.er1 | ||||
|   ac_status=$? | ||||
|   grep -v '^ *+' conftest.er1 >conftest.err | ||||
|   rm -f conftest.er1 | ||||
|   cat conftest.err >&5 | ||||
|   echo "$as_me:$LINENO: \$? = $ac_status" >&5 | ||||
|   (exit $ac_status); } && { | ||||
| 	 test -z "$ac_c_werror_flag" || | ||||
| 	 test ! -s conftest.err | ||||
|        } && test -s conftest$ac_exeext && | ||||
|        $as_test_x conftest$ac_exeext; then | ||||
|   eval "$as_ac_var=yes" | ||||
| else | ||||
|   echo "$as_me: failed program was:" >&5 | ||||
| sed 's/^/| /' conftest.$ac_ext >&5 | ||||
|  | ||||
| 	eval "$as_ac_var=no" | ||||
| fi | ||||
|  | ||||
| rm -f core conftest.err conftest.$ac_objext conftest_ipa8_conftest.oo \ | ||||
|       conftest$ac_exeext conftest.$ac_ext | ||||
| fi | ||||
| ac_res=`eval echo '${'$as_ac_var'}'` | ||||
| 	       { echo "$as_me:$LINENO: result: $ac_res" >&5 | ||||
| echo "${ECHO_T}$ac_res" >&6; } | ||||
| if test `eval echo '${'$as_ac_var'}'` = yes; then | ||||
|   cat >>confdefs.h <<_ACEOF | ||||
| #define `echo "HAVE_$ac_func" | $as_tr_cpp` 1 | ||||
| _ACEOF | ||||
|  | ||||
| fi | ||||
| done | ||||
|  | ||||
| LIBS="$OLDLIBS" | ||||
|  | ||||
| { echo "$as_me:$LINENO: checking for char" >&5 | ||||
| echo $ECHO_N "checking for char... $ECHO_C" >&6; } | ||||
| if test "${ac_cv_type_char+set}" = set; then | ||||
|  | ||||
| @ -91,6 +91,13 @@ AC_CHECK_FUNCS([mbrlen mbrtowc wcrtomb]) | ||||
| AC_CHECK_FUNCS([mbsnrtowcs mbsrtowcs wcsnrtombs wcsrtombs]) | ||||
| AC_CHECK_FUNCS([lseek64 stat64 fstat64 ftruncate64]) | ||||
|  | ||||
| OLDLIBS="$LIBS" | ||||
| LIBS="$LIBM $LIBS" | ||||
| AC_CHECK_FUNCS([powl sinl cosl tanl atanl atan2l logl expl sqrtl]) | ||||
| AC_CHECK_FUNCS([pow sin cos tan atan atan2 log exp sqrt]) | ||||
| AC_CHECK_FUNCS([powf sinf cosf tanf atanf atan2f logf expf sqrtf]) | ||||
| LIBS="$OLDLIBS" | ||||
|  | ||||
| dnl Checks the size of primitive data types | ||||
| AC_CHECK_SIZEOF(char) | ||||
| AC_CHECK_SIZEOF(short) | ||||
|  | ||||
| @ -1,5 +1,5 @@ | ||||
| /* | ||||
|  * $Id: StdAwk.hpp 468 2008-12-10 10:19:59Z baconevi $ | ||||
|  * $Id: StdAwk.hpp 499 2008-12-16 09:42:48Z baconevi $ | ||||
|  * | ||||
|  * {License} | ||||
|  */ | ||||
| @ -23,6 +23,7 @@ class StdAwk: public Awk | ||||
| public: | ||||
| 	StdAwk (); | ||||
| 	int open (); | ||||
| 	int run (const char_t* main, const char_t** args, size_t nargs); | ||||
|  | ||||
| protected: | ||||
|  | ||||
|  | ||||
| @ -1,5 +1,5 @@ | ||||
| /* | ||||
|  * $Id: awk.h 496 2008-12-15 09:56:48Z baconevi $ | ||||
|  * $Id: awk.h 499 2008-12-16 09:42:48Z baconevi $ | ||||
|  * | ||||
|  * {License} | ||||
|  */ | ||||
| @ -1142,17 +1142,68 @@ void ase_awk_refdownval_nofree (ase_awk_run_t* run, ase_awk_val_t* val); | ||||
| void ase_awk_freevalchunk (ase_awk_run_t* run, ase_awk_val_chunk_t* chunk); | ||||
|  | ||||
| ase_bool_t ase_awk_valtobool ( | ||||
| 	ase_awk_run_t* run, ase_awk_val_t* val); | ||||
| 	ase_awk_run_t* run, | ||||
| 	ase_awk_val_t* val | ||||
| ); | ||||
|  | ||||
| ase_char_t* ase_awk_valtostr ( | ||||
| 	ase_awk_run_t* run, ase_awk_val_t* val,  | ||||
| 	int opt, ase_str_t* buf, ase_size_t* len); | ||||
| 	ase_awk_run_t* run, | ||||
| 	ase_awk_val_t* val,  | ||||
| 	int opt,  | ||||
| 	ase_str_t* buf, | ||||
| 	ase_size_t* len | ||||
| ); | ||||
|  | ||||
| /****f* ase.awk/ase_awk_valtonum | ||||
|  * NAME | ||||
|  *  ase_awk_valtonum - convert a value to a number | ||||
|  * | ||||
|  * DESCRIPTION | ||||
|  *  The ase_awk_valtonum() function converts a value to a number.  | ||||
|  *  The converted value is stored into the variable pointed to by | ||||
|  *  either l or r depending on the type of the number. If the value | ||||
|  *  is converted to a long number, the function returns 0 and l is | ||||
|  *  set with the converted number. If the value is converted to a real number, | ||||
|  *  the function returns 1 and r is set with a real number. | ||||
|  *  | ||||
|  * RETURN | ||||
|  *  The ase_awk_valtonum() function returns -1 on error, 0 if the converted | ||||
|  *  value is a long number and 1 if it is a real number. | ||||
|  * | ||||
|  * EXAMPLES | ||||
|  *  ase_long_t l; | ||||
|  *  ase_real_t r; | ||||
|  *  int n; | ||||
|  * | ||||
|  *  n = ase_awk_valtonum (v, &l, &r); | ||||
|  *  if (n == -1) error (); | ||||
|  *  else if (n == 0) do_long (l); | ||||
|  *  else if (n == 1) do_real (r); | ||||
|  * | ||||
|  * SYNOPSIS | ||||
|  */ | ||||
| int ase_awk_valtonum ( | ||||
| 	ase_awk_run_t* run, ase_awk_val_t* v, ase_long_t* l, ase_real_t* r); | ||||
| 	ase_awk_run_t* run, | ||||
| 	ase_awk_val_t* v   /* the value to convert to a number */, | ||||
| 	ase_long_t*    l   /* a pointer to a long number */,  | ||||
| 	ase_real_t*    r   /* a pointer to a ase_real_t */ | ||||
| ); | ||||
| /******/ | ||||
|  | ||||
| /****f* ase.awk/ase_awk_strtonum | ||||
|  * NAME | ||||
|  *  ase_awk_strtonum - convert a string to a number | ||||
|  * | ||||
|  * SYNOPSIS | ||||
|  */ | ||||
| int ase_awk_strtonum ( | ||||
| 	ase_awk_run_t* run, const ase_char_t* ptr, ase_size_t len,  | ||||
| 	ase_long_t* l, ase_real_t* r); | ||||
| 	ase_awk_run_t*    run, | ||||
| 	const ase_char_t* ptr, | ||||
| 	ase_size_t        len,  | ||||
| 	ase_long_t*       l,  | ||||
| 	ase_real_t*       r | ||||
| ); | ||||
| /******/ | ||||
|  | ||||
| #ifdef __cplusplus | ||||
| } | ||||
|  | ||||
| @ -1,5 +1,5 @@ | ||||
|  | ||||
| pkginclude_HEADERS = mem.h chr.h str.h lda.h map.h rex.h sll.h dll.h opt.h fio.h tio.h sio.h | ||||
| pkginclude_HEADERS = mem.h chr.h str.h lda.h map.h rex.h sll.h dll.h opt.h fio.h tio.h sio.h time.h | ||||
|  | ||||
| pkgincludedir= $(includedir)/ase/cmn | ||||
|  | ||||
|  | ||||
| @ -178,7 +178,7 @@ sysconfdir = @sysconfdir@ | ||||
| target_alias = @target_alias@ | ||||
| top_builddir = @top_builddir@ | ||||
| top_srcdir = @top_srcdir@ | ||||
| pkginclude_HEADERS = mem.h chr.h str.h lda.h map.h rex.h sll.h dll.h opt.h fio.h tio.h sio.h | ||||
| pkginclude_HEADERS = mem.h chr.h str.h lda.h map.h rex.h sll.h dll.h opt.h fio.h tio.h sio.h time.h | ||||
| CLEANFILES = *dist | ||||
| all: all-am | ||||
|  | ||||
|  | ||||
							
								
								
									
										47
									
								
								ase/include/ase/cmn/time.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								ase/include/ase/cmn/time.h
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,47 @@ | ||||
| /* | ||||
|  * $Id$ | ||||
|  */ | ||||
|  | ||||
| #ifndef _ASE_CMN_TIME_H_ | ||||
| #define _ASE_CMN_TIME_H_ | ||||
|  | ||||
| #include <ase/types.h> | ||||
| #include <ase/macros.h> | ||||
|  | ||||
| #define ASE_EPOCH_YEAR  ((ase_time_t)1970) | ||||
| #define ASE_EPOCH_MON   ((ase_time_t)1) | ||||
| #define ASE_EPOCH_DAY   ((ase_time_t)1) | ||||
| #define ASE_EPOCH_WDAY  ((ase_time_t)4) | ||||
|  | ||||
| #define ASE_DAY_IN_WEEK  ((ase_time_t)7) | ||||
| #define ASE_MON_IN_YEAR  ((ase_time_t)12) | ||||
| #define ASE_HOUR_IN_DAY  ((ase_time_t)24) | ||||
| #define ASE_MIN_IN_HOUR  ((ase_time_t)60) | ||||
| #define ASE_MIN_IN_DAY   (ASE_MIN_IN_HOUR * ASE_HOUR_IN_DAY) | ||||
| #define ASE_SEC_IN_MIN   ((ase_time_t)60) | ||||
| #define ASE_SEC_IN_HOUR  (ASE_SEC_IN_MIN * ASE_MIN_IN_HOUR) | ||||
| #define ASE_SEC_IN_DAY   (ASE_SEC_IN_MIN * ASE_MIN_IN_DAY) | ||||
| #define ASE_MSEC_IN_SEC  ((ase_time_t)1000) | ||||
| #define ASE_MSEC_IN_MIN  (ASE_MSEC_IN_SEC * ASE_SEC_IN_MIN) | ||||
| #define ASE_MSEC_IN_HOUR (ASE_MSEC_IN_SEC * ASE_SEC_IN_HOUR) | ||||
| #define ASE_MSEC_IN_DAY  (ASE_MSEC_IN_SEC * ASE_SEC_IN_DAY) | ||||
|  | ||||
| #define ASE_USEC_IN_MSEC ((ase_time_t)1000) | ||||
| #define ASE_NSEC_IN_USEC ((ase_time_t)1000) | ||||
| #define ASE_USEC_IN_SEC  ((ase_time_t)ASE_USEC_IN_MSEC * ASE_MSEC_IN_SEC) | ||||
|  | ||||
| /* number of milliseconds since the Epoch (00:00:00 UTC, Jan 1, 1970) */ | ||||
| typedef ase_long_t ase_time_t; | ||||
|  | ||||
| #ifdef __cplusplus | ||||
| extern "C" { | ||||
| #endif | ||||
|  | ||||
| int ase_gettime (ase_time_t* t); | ||||
| int ase_settime (ase_time_t t); | ||||
|  | ||||
| #ifdef __cplusplus | ||||
| } | ||||
| #endif | ||||
|  | ||||
| #endif | ||||
| @ -78,9 +78,45 @@ | ||||
| /* patch level */ | ||||
| #undef ASE_VERSION_PATCH | ||||
|  | ||||
| /* Define to 1 if you have the `atan' function. */ | ||||
| #undef HAVE_ATAN | ||||
|  | ||||
| /* Define to 1 if you have the `atan2' function. */ | ||||
| #undef HAVE_ATAN2 | ||||
|  | ||||
| /* Define to 1 if you have the `atan2f' function. */ | ||||
| #undef HAVE_ATAN2F | ||||
|  | ||||
| /* Define to 1 if you have the `atan2l' function. */ | ||||
| #undef HAVE_ATAN2L | ||||
|  | ||||
| /* Define to 1 if you have the `atanf' function. */ | ||||
| #undef HAVE_ATANF | ||||
|  | ||||
| /* Define to 1 if you have the `atanl' function. */ | ||||
| #undef HAVE_ATANL | ||||
|  | ||||
| /* Define to 1 if you have the `cos' function. */ | ||||
| #undef HAVE_COS | ||||
|  | ||||
| /* Define to 1 if you have the `cosf' function. */ | ||||
| #undef HAVE_COSF | ||||
|  | ||||
| /* Define to 1 if you have the `cosl' function. */ | ||||
| #undef HAVE_COSL | ||||
|  | ||||
| /* Define to 1 if you have the <dlfcn.h> header file. */ | ||||
| #undef HAVE_DLFCN_H | ||||
|  | ||||
| /* Define to 1 if you have the `exp' function. */ | ||||
| #undef HAVE_EXP | ||||
|  | ||||
| /* Define to 1 if you have the `expf' function. */ | ||||
| #undef HAVE_EXPF | ||||
|  | ||||
| /* Define to 1 if you have the `expl' function. */ | ||||
| #undef HAVE_EXPL | ||||
|  | ||||
| /* Define to 1 if you have the `fstat64' function. */ | ||||
| #undef HAVE_FSTAT64 | ||||
|  | ||||
| @ -90,6 +126,15 @@ | ||||
| /* Define to 1 if you have the <inttypes.h> header file. */ | ||||
| #undef HAVE_INTTYPES_H | ||||
|  | ||||
| /* Define to 1 if you have the `log' function. */ | ||||
| #undef HAVE_LOG | ||||
|  | ||||
| /* Define to 1 if you have the `logf' function. */ | ||||
| #undef HAVE_LOGF | ||||
|  | ||||
| /* Define to 1 if you have the `logl' function. */ | ||||
| #undef HAVE_LOGL | ||||
|  | ||||
| /* Define to 1 if you have the `lseek64' function. */ | ||||
| #undef HAVE_LSEEK64 | ||||
|  | ||||
| @ -108,6 +153,33 @@ | ||||
| /* Define to 1 if you have the <memory.h> header file. */ | ||||
| #undef HAVE_MEMORY_H | ||||
|  | ||||
| /* Define to 1 if you have the `pow' function. */ | ||||
| #undef HAVE_POW | ||||
|  | ||||
| /* Define to 1 if you have the `powf' function. */ | ||||
| #undef HAVE_POWF | ||||
|  | ||||
| /* Define to 1 if you have the `powl' function. */ | ||||
| #undef HAVE_POWL | ||||
|  | ||||
| /* Define to 1 if you have the `sin' function. */ | ||||
| #undef HAVE_SIN | ||||
|  | ||||
| /* Define to 1 if you have the `sinf' function. */ | ||||
| #undef HAVE_SINF | ||||
|  | ||||
| /* Define to 1 if you have the `sinl' function. */ | ||||
| #undef HAVE_SINL | ||||
|  | ||||
| /* Define to 1 if you have the `sqrt' function. */ | ||||
| #undef HAVE_SQRT | ||||
|  | ||||
| /* Define to 1 if you have the `sqrtf' function. */ | ||||
| #undef HAVE_SQRTF | ||||
|  | ||||
| /* Define to 1 if you have the `sqrtl' function. */ | ||||
| #undef HAVE_SQRTL | ||||
|  | ||||
| /* Define to 1 if you have the `stat64' function. */ | ||||
| #undef HAVE_STAT64 | ||||
|  | ||||
| @ -135,6 +207,15 @@ | ||||
| /* Define to 1 if you have the <sys/types.h> header file. */ | ||||
| #undef HAVE_SYS_TYPES_H | ||||
|  | ||||
| /* Define to 1 if you have the `tan' function. */ | ||||
| #undef HAVE_TAN | ||||
|  | ||||
| /* Define to 1 if you have the `tanf' function. */ | ||||
| #undef HAVE_TANF | ||||
|  | ||||
| /* Define to 1 if you have the `tanl' function. */ | ||||
| #undef HAVE_TANL | ||||
|  | ||||
| /* Define to 1 if you have the <unistd.h> header file. */ | ||||
| #undef HAVE_UNISTD_H | ||||
|  | ||||
|  | ||||
| @ -1,5 +1,5 @@ | ||||
| /* | ||||
|  * $Id: StdAwk.cpp 468 2008-12-10 10:19:59Z baconevi $ | ||||
|  * $Id: StdAwk.cpp 499 2008-12-16 09:42:48Z baconevi $ | ||||
|  * | ||||
|  * {License} | ||||
|  */ | ||||
| @ -10,6 +10,7 @@ | ||||
|  | ||||
| #include <ase/awk/StdAwk.hpp> | ||||
| #include <ase/cmn/str.h> | ||||
| #include <ase/cmn/time.h> | ||||
| #include <ase/utl/stdio.h> | ||||
|  | ||||
| #include <stdlib.h> | ||||
| @ -26,11 +27,8 @@ | ||||
| ASE_BEGIN_NAMESPACE(ASE) | ||||
| ///////////////////////////////// | ||||
|  | ||||
|  | ||||
| StdAwk::StdAwk () | ||||
| { | ||||
| 	seed = (unsigned int)::time(NULL); | ||||
| 	::srand (seed); | ||||
| } | ||||
|  | ||||
| #define ADD_FUNC(name,min,max,impl) \ | ||||
| @ -67,52 +65,144 @@ int StdAwk::open () | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| int StdAwk::run (const char_t* main, const char_t** args, size_t nargs) | ||||
| { | ||||
| 	ase_time_t now; | ||||
|  | ||||
| 	if (ase_gettime(&now) == -1) this->seed = 0; | ||||
| 	else this->seed = (unsigned int)now; | ||||
|  | ||||
| 	::srand (this->seed); | ||||
|  | ||||
| 	return Awk::run (main, args, nargs); | ||||
| } | ||||
|  | ||||
| int StdAwk::sin (Run& run, Return& ret, const Argument* args, size_t nargs,  | ||||
| 	const char_t* name, size_t len) | ||||
| { | ||||
| 	return ret.set ((real_t)::sin(args[0].toReal())); | ||||
| 	return ret.set ( | ||||
| 	#if defined(HAVE_SINL) | ||||
| 		(real_t)::sinl(args[0].toReal()) | ||||
| 	#elif defined(HAVE_SIN) | ||||
| 		(real_t)::sin(args[0].toReal()) | ||||
| 	#elif defined(HAVE_SINF) | ||||
| 		(real_t)::sinf(args[0].toReal()) | ||||
| 	#else | ||||
| 		#error ### no sin function available ### | ||||
| 	#endif | ||||
| 	); | ||||
| } | ||||
|  | ||||
| int StdAwk::cos (Run& run, Return& ret, const Argument* args, size_t nargs, | ||||
| 	const char_t* name, size_t len) | ||||
| { | ||||
| 	return ret.set ((real_t)::cos(args[0].toReal())); | ||||
| 	return ret.set ( | ||||
| 	#if defined(HAVE_COSL) | ||||
| 		(real_t)::cosl(args[0].toReal()) | ||||
| 	#elif defined(HAVE_COS) | ||||
| 		(real_t)::cos(args[0].toReal()) | ||||
| 	#elif defined(HAVE_COSF) | ||||
| 		(real_t)::cosf(args[0].toReal()) | ||||
| 	#else | ||||
| 		#error ### no cos function available ### | ||||
| 	#endif | ||||
| 	); | ||||
| } | ||||
|  | ||||
| int StdAwk::tan (Run& run, Return& ret, const Argument* args, size_t nargs, | ||||
| 	const char_t* name, size_t len) | ||||
| { | ||||
| 	return ret.set ((real_t)::tan(args[0].toReal())); | ||||
| 	return ret.set ( | ||||
| 	#if defined(HAVE_TANL) | ||||
| 		(real_t)::tanl(args[0].toReal()) | ||||
| 	#elif defined(HAVE_TAN) | ||||
| 		(real_t)::tan(args[0].toReal()) | ||||
| 	#elif defined(HAVE_TANF) | ||||
| 		(real_t)::tanf(args[0].toReal()) | ||||
| 	#else | ||||
| 		#error ### no tan function available ### | ||||
| 	#endif | ||||
| 	); | ||||
| } | ||||
|  | ||||
| int StdAwk::atan (Run& run, Return& ret, const Argument* args, size_t nargs, | ||||
| 	const char_t* name, size_t len) | ||||
| { | ||||
| 	return ret.set ((real_t)::atan(args[0].toReal())); | ||||
| 	return ret.set ( | ||||
| 	#if defined(HAVE_ATANL) | ||||
| 		(real_t)::atanl(args[0].toReal()) | ||||
| 	#elif defined(HAVE_ATAN) | ||||
| 		(real_t)::atan(args[0].toReal()) | ||||
| 	#elif defined(HAVE_ATANF) | ||||
| 		(real_t)::atanf(args[0].toReal()) | ||||
| 	#else | ||||
| 		#error ### no atan function available ### | ||||
| 	#endif | ||||
| 	); | ||||
| } | ||||
|  | ||||
| int StdAwk::atan2 (Run& run, Return& ret, const Argument* args, size_t nargs, | ||||
| 	const char_t* name, size_t len) | ||||
| { | ||||
| 	return ret.set ((real_t)::atan2(args[0].toReal(), args[1].toReal())); | ||||
| 	return ret.set ( | ||||
| 	#if defined(HAVE_ATAN2L) | ||||
| 		(real_t)::atan2l(args[0].toReal(), args[1].toReal()) | ||||
| 	#elif defined(HAVE_ATAN2) | ||||
| 		(real_t)::atan2(args[0].toReal(), args[1].toReal()) | ||||
| 	#elif defined(HAVE_ATAN2F) | ||||
| 		(real_t)::atan2f(args[0].toReal(), args[1].toReal()) | ||||
| 	#else | ||||
| 		#error ### no atan2 function available ### | ||||
| 	#endif | ||||
| 	); | ||||
| } | ||||
|  | ||||
| int StdAwk::log (Run& run, Return& ret, const Argument* args, size_t nargs, | ||||
| 	const char_t* name, size_t len) | ||||
| { | ||||
| 	return ret.set ((real_t)::log(args[0].toReal())); | ||||
| 	return ret.set ( | ||||
| 	#if defined(HAVE_LOGL) | ||||
| 		(real_t)::logl(args[0].toReal()) | ||||
| 	#elif defined(HAVE_LOG) | ||||
| 		(real_t)::log(args[0].toReal()) | ||||
| 	#elif defined(HAVE_LOGF) | ||||
| 		(real_t)::logf(args[0].toReal()) | ||||
| 	#else | ||||
| 		#error ### no log function available ### | ||||
| 	#endif | ||||
| 	); | ||||
| } | ||||
|  | ||||
| int StdAwk::exp (Run& run, Return& ret, const Argument* args, size_t nargs, | ||||
| 	const char_t* name, size_t len) | ||||
| { | ||||
| 	return ret.set ((real_t)::exp(args[0].toReal())); | ||||
| 	return ret.set ( | ||||
| 	#if defined(HAVE_EXPL) | ||||
| 		(real_t)::expl(args[0].toReal()) | ||||
| 	#elif defined(HAVE_EXP) | ||||
| 		(real_t)::exp(args[0].toReal()) | ||||
| 	#elif defined(HAVE_EXPF) | ||||
| 		(real_t)::expf(args[0].toReal()) | ||||
| 	#else | ||||
| 		#error ### no exp function available ### | ||||
| 	#endif | ||||
| 	); | ||||
| } | ||||
|  | ||||
| int StdAwk::sqrt (Run& run, Return& ret, const Argument* args, size_t nargs, | ||||
| 	const char_t* name, size_t len) | ||||
| { | ||||
| 	return ret.set ((real_t)::sqrt(args[0].toReal())); | ||||
| 	return ret.set ( | ||||
| 	#if defined(HAVE_SQRTL) | ||||
| 		(real_t)::sqrtl(args[0].toReal()) | ||||
| 	#elif defined(HAVE_SQRT) | ||||
| 		(real_t)::sqrt(args[0].toReal()) | ||||
| 	#elif defined(HAVE_SQRTF) | ||||
| 		(real_t)::sqrtf(args[0].toReal()) | ||||
| 	#else | ||||
| 		#error ### no sqrt function available ### | ||||
| 	#endif | ||||
| 	); | ||||
| } | ||||
|  | ||||
| int StdAwk::fnint (Run& run, Return& ret, const Argument* args, size_t nargs, | ||||
| @ -130,12 +220,22 @@ int StdAwk::rand (Run& run, Return& ret, const Argument* args, size_t nargs, | ||||
| int StdAwk::srand (Run& run, Return& ret, const Argument* args, size_t nargs, | ||||
| 	const char_t* name, size_t len) | ||||
| { | ||||
| 	unsigned int prevSeed = seed; | ||||
| 	unsigned int prevSeed = this->seed; | ||||
|  | ||||
| 	seed = (nargs == 0)?  | ||||
| 		(unsigned int)::time(NULL): | ||||
| 		(unsigned int)args[0].toInt(); | ||||
| 	::srand (seed); | ||||
| 	if (nargs == 0) | ||||
| 	{ | ||||
| 		ase_time_t now; | ||||
|  | ||||
| 		if (ase_gettime (&now) == -1) | ||||
| 			this->seed = (unsigned int)now; | ||||
| 		else this->seed >>= 1; | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		this->seed = (unsigned int)args[0].toInt(); | ||||
| 	} | ||||
|  | ||||
| 	::srand (this->seed); | ||||
| 	return ret.set ((long_t)prevSeed); | ||||
| } | ||||
|  | ||||
| @ -149,7 +249,9 @@ int StdAwk::srand (Run& run, Return& ret, const Argument* args, size_t nargs, | ||||
| int StdAwk::systime (Run& run, Return& ret, const Argument* args, size_t nargs, | ||||
| 	const char_t* name, size_t len) | ||||
| { | ||||
| 	return ret.set ((long_t)::time(NULL)); | ||||
| 	ase_time_t now; | ||||
| 	if (ase_gettime (&now) == -1) now = 0; | ||||
| 	return ret.set ((long_t)now / ASE_MSEC_IN_SEC); | ||||
| } | ||||
|  | ||||
| int StdAwk::strftime (Run& run, Return& ret, const Argument* args, size_t nargs, | ||||
|  | ||||
| @ -1,5 +1,5 @@ | ||||
| /* | ||||
|  * $Id: func.c 391 2008-09-27 09:51:23Z baconevi $ | ||||
|  * $Id: func.c 499 2008-12-16 09:42:48Z baconevi $ | ||||
|  * | ||||
|  * {License} | ||||
|  */ | ||||
| @ -802,6 +802,7 @@ static int bfn_split ( | ||||
| 			((ase_awk_val_map_t*)t1)->map,  | ||||
| 			key, key_len, t2, 0) == ASE_NULL) | ||||
| 		{ | ||||
| 			/* assignment failed. restore the reference counter */ | ||||
| 			ase_awk_refdownval (run, t2); | ||||
|  | ||||
| 			if (str_free != ASE_NULL)  | ||||
|  | ||||
| @ -4,9 +4,12 @@ | ||||
|  | ||||
| #include "awk.h" | ||||
| #include <ase/cmn/sio.h> | ||||
| #include <ase/cmn/str.h> | ||||
| #include <ase/cmn/time.h> | ||||
|  | ||||
| #include <math.h> | ||||
| #include <stdarg.h> | ||||
| #include <stdlib.h> | ||||
| #include <ase/utl/stdio.h> | ||||
|  | ||||
| typedef struct xtn_t | ||||
| @ -14,9 +17,22 @@ typedef struct xtn_t | ||||
| 	ase_awk_prmfns_t prmfns; | ||||
| } xtn_t; | ||||
|  | ||||
| typedef struct rxtn_t | ||||
| { | ||||
| 	unsigned int seed;	 | ||||
| } rxtn_t; | ||||
|  | ||||
| static ase_real_t custom_awk_pow (void* custom, ase_real_t x, ase_real_t y) | ||||
| { | ||||
| #if defined(HAVE_POWL) | ||||
| 	return powl (x, y); | ||||
| #elif defined(HAVE_POW) | ||||
| 	return pow (x, y); | ||||
| #elif defined(HAVE_POWF) | ||||
| 	return powf (x, y); | ||||
| #else | ||||
| 	#error ### no pow function available ### | ||||
| #endif | ||||
| } | ||||
|  | ||||
| static int custom_awk_sprintf ( | ||||
| @ -33,6 +49,8 @@ static int custom_awk_sprintf ( | ||||
| 	return n; | ||||
| } | ||||
|  | ||||
| static int add_functions (ase_awk_t* awk); | ||||
|  | ||||
| ase_awk_t* ase_awk_opensimple (ase_size_t xtnsize) | ||||
| { | ||||
| 	ase_awk_t* awk; | ||||
| @ -52,6 +70,12 @@ ase_awk_t* ase_awk_opensimple (ase_size_t xtnsize) | ||||
| 		ASE_AWK_IMPLICIT | ASE_AWK_EXTIO | ASE_AWK_NEWLINE |  | ||||
| 		ASE_AWK_BASEONE | ASE_AWK_PABLOCK); | ||||
|  | ||||
| 	if (add_functions (awk) == -1) | ||||
| 	{ | ||||
| 		ase_awk_close (awk); | ||||
| 		return ASE_NULL; | ||||
| 	} | ||||
|  | ||||
| 	return awk; | ||||
| } | ||||
|  | ||||
| @ -683,6 +707,8 @@ int ase_awk_runsimple (ase_awk_t* awk, ase_char_t** icf, ase_awk_runcbs_t* cbs) | ||||
| { | ||||
| 	ase_awk_runios_t ios; | ||||
| 	runio_data_t rd; | ||||
| 	rxtn_t rxtn; | ||||
| 	ase_time_t now; | ||||
|  | ||||
| 	rd.ic.files = icf; | ||||
| 	rd.ic.index = 0; | ||||
| @ -692,21 +718,249 @@ int ase_awk_runsimple (ase_awk_t* awk, ase_char_t** icf, ase_awk_runcbs_t* cbs) | ||||
| 	ios.console = awk_extio_console; | ||||
| 	ios.data = &rd; | ||||
|  | ||||
| 	if (ase_gettime (&now) == -1) rxtn.seed = 0; | ||||
| 	else rxtn.seed = (unsigned int)now; | ||||
| 	srand (rxtn.seed); | ||||
|  | ||||
| 	return ase_awk_run ( | ||||
| 		awk,  | ||||
| 		ASE_NULL/*mfn*/, | ||||
| 		&ios, | ||||
| 		cbs, | ||||
| 		ASE_NULL/*runarg*/, | ||||
| 		ASE_NULL | ||||
| 		&rxtn/*ASE_NULL*/ | ||||
| 	); | ||||
| } | ||||
|  | ||||
|  | ||||
| /*** EXTRA BUILTIN FUNCTIONS ***/ | ||||
| #if 0 | ||||
| int aes_awk_func_sleep ( | ||||
| 	ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl) | ||||
| enum | ||||
| { | ||||
| 	BFN_MATH_LD, | ||||
| 	BFN_MATH_D, | ||||
| 	BFN_MATH_F | ||||
| }; | ||||
|  | ||||
| static int bfn_math_1 ( | ||||
| 	ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl, int type, void* f) | ||||
| { | ||||
| 	ase_size_t nargs; | ||||
| 	ase_awk_val_t* a0; | ||||
| 	ase_long_t lv; | ||||
| 	ase_real_t rv; | ||||
| 	ase_awk_val_t* r; | ||||
| 	int n; | ||||
|  | ||||
| 	nargs = ase_awk_getnargs (run); | ||||
| 	ASE_ASSERT (nargs == 1); | ||||
|  | ||||
| 	a0 = ase_awk_getarg (run, 0); | ||||
|  | ||||
| 	n = ase_awk_valtonum (run, a0, &lv, &rv); | ||||
| 	if (n == -1) return -1; | ||||
| 	if (n == 0) rv = (ase_real_t)lv; | ||||
|  | ||||
| 	if (type == BFN_MATH_LD) | ||||
| 	{ | ||||
| 		long double (*rf) (long double) =  | ||||
| 			(long double(*)(long double))f; | ||||
| 		r = ase_awk_makerealval (run, rf(rv)); | ||||
| 	} | ||||
| 	else if (type == BFN_MATH_D) | ||||
| 	{ | ||||
| 		double (*rf) (double) = (double(*)(double))f; | ||||
| 		r = ase_awk_makerealval (run, rf(rv)); | ||||
| 	} | ||||
| 	else  | ||||
| 	{ | ||||
| 		ASE_ASSERT (type == BFN_MATH_F); | ||||
| 		float (*rf) (float) = (float(*)(float))f; | ||||
| 		r = ase_awk_makerealval (run, rf(rv)); | ||||
| 	} | ||||
| 	 | ||||
| 	if (r == ASE_NULL) | ||||
| 	{ | ||||
| 		ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	ase_awk_setretval (run, r); | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| static int bfn_math_2 ( | ||||
| 	ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl, int type, void* f) | ||||
| { | ||||
| 	ase_size_t nargs; | ||||
| 	ase_awk_val_t* a0, * a1; | ||||
| 	ase_long_t lv0, lv1; | ||||
| 	ase_real_t rv0, rv1; | ||||
| 	ase_awk_val_t* r; | ||||
| 	int n; | ||||
|  | ||||
| 	nargs = ase_awk_getnargs (run); | ||||
| 	ASE_ASSERT (nargs == 2); | ||||
|  | ||||
| 	a0 = ase_awk_getarg (run, 0); | ||||
| 	a1 = ase_awk_getarg (run, 1); | ||||
|  | ||||
| 	n = ase_awk_valtonum (run, a0, &lv0, &rv0); | ||||
| 	if (n == -1) return -1; | ||||
| 	if (n == 0) rv0 = (ase_real_t)lv0; | ||||
|  | ||||
| 	n = ase_awk_valtonum (run, a1, &lv1, &rv1); | ||||
| 	if (n == -1) return -1; | ||||
| 	if (n == 0) rv1 = (ase_real_t)lv1; | ||||
|  | ||||
| 	if (type == BFN_MATH_LD) | ||||
| 	{ | ||||
| 		long double (*rf) (long double,long double) =  | ||||
| 			(long double(*)(long double,long double))f; | ||||
| 		r = ase_awk_makerealval (run, rf(rv0,rv1)); | ||||
| 	} | ||||
| 	else if (type == BFN_MATH_D) | ||||
| 	{ | ||||
| 		double (*rf) (double,double) = (double(*)(double,double))f; | ||||
| 		r = ase_awk_makerealval (run, rf(rv0,rv1)); | ||||
| 	} | ||||
| 	else  | ||||
| 	{ | ||||
| 		ASE_ASSERT (type == BFN_MATH_F); | ||||
| 		float (*rf) (float,float) = (float(*)(float,float))f; | ||||
| 		r = ase_awk_makerealval (run, rf(rv0,rv1)); | ||||
| 	} | ||||
| 	 | ||||
| 	if (r == ASE_NULL) | ||||
| 	{ | ||||
| 		ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	ase_awk_setretval (run, r); | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| static int bfn_sin (ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl) | ||||
| { | ||||
| 	return bfn_math_1 (run, fnm, fnl,  | ||||
| 	#if defined(HAVE_SINL) | ||||
| 		BFN_MATH_LD, sinl | ||||
| 	#elif defined(HAVE_SIN) | ||||
| 		BFN_MATH_D, sin | ||||
| 	#elif defined(HAVE_SINF) | ||||
| 		BFN_MATH_F, sinf | ||||
| 	#else | ||||
| 		#error ### no sin function available ### | ||||
| 	#endif | ||||
| 	); | ||||
| } | ||||
|  | ||||
| static int bfn_cos (ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl) | ||||
| { | ||||
| 	return bfn_math_1 (run, fnm, fnl,  | ||||
| 	#if defined(HAVE_COSL) | ||||
| 		BFN_MATH_LD, cosl | ||||
| 	#elif defined(HAVE_COS) | ||||
| 		BFN_MATH_D, cos | ||||
| 	#elif defined(HAVE_COSF) | ||||
| 		BFN_MATH_F, cosf | ||||
| 	#else | ||||
| 		#error ### no cos function available ### | ||||
| 	#endif | ||||
| 	); | ||||
| } | ||||
|  | ||||
| static int bfn_tan (ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl) | ||||
| { | ||||
| 	return bfn_math_1 (run, fnm, fnl,  | ||||
| 	#if defined(HAVE_TANL) | ||||
| 		BFN_MATH_LD, tanl | ||||
| 	#elif defined(HAVE_TAN) | ||||
| 		BFN_MATH_D, tan | ||||
| 	#elif defined(HAVE_TANF) | ||||
| 		BFN_MATH_F, tanf | ||||
| 	#else | ||||
| 		#error ### no tan function available ### | ||||
| 	#endif | ||||
| 	); | ||||
| } | ||||
|  | ||||
| static int bfn_atan (ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl) | ||||
| { | ||||
| 	return bfn_math_1 (run, fnm, fnl,  | ||||
| 	#if defined(HAVE_ATANL) | ||||
| 		BFN_MATH_LD, atanl | ||||
| 	#elif defined(HAVE_ATAN) | ||||
| 		BFN_MATH_D, atan | ||||
| 	#elif defined(HAVE_ATANF) | ||||
| 		BFN_MATH_F, atanf | ||||
| 	#else | ||||
| 		#error ### no atan function available ### | ||||
| 	#endif | ||||
| 	); | ||||
| } | ||||
|  | ||||
| static int bfn_atan2 (ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl) | ||||
| { | ||||
| 	return bfn_math_2 (run, fnm, fnl,  | ||||
| 	#if defined(HAVE_ATAN2L) | ||||
| 		BFN_MATH_LD, atan2l | ||||
| 	#elif defined(HAVE_ATAN2) | ||||
| 		BFN_MATH_D, atan2 | ||||
| 	#elif defined(HAVE_ATAN2F) | ||||
| 		BFN_MATH_F, atan2f | ||||
| 	#else | ||||
| 		#error ### no atan2 function available ### | ||||
| 	#endif | ||||
| 	); | ||||
| } | ||||
|  | ||||
| static int bfn_log (ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl) | ||||
| { | ||||
| 	return bfn_math_1 (run, fnm, fnl,  | ||||
| 	#if defined(HAVE_LOGL) | ||||
| 		BFN_MATH_LD, logl | ||||
| 	#elif defined(HAVE_LOG) | ||||
| 		BFN_MATH_D, log | ||||
| 	#elif defined(HAVE_LOGF) | ||||
| 		BFN_MATH_F, logf | ||||
| 	#else | ||||
| 		#error ### no log function available ### | ||||
| 	#endif | ||||
| 	); | ||||
| } | ||||
|  | ||||
| static int bfn_exp (ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl) | ||||
| { | ||||
| 	return bfn_math_1 (run, fnm, fnl,  | ||||
| 	#if defined(HAVE_EXPL) | ||||
| 		BFN_MATH_LD, expl | ||||
| 	#elif defined(HAVE_EXP) | ||||
| 		BFN_MATH_D, exp | ||||
| 	#elif defined(HAVE_EXPF) | ||||
| 		BFN_MATH_F, expf | ||||
| 	#else | ||||
| 		#error ### no exp function available ### | ||||
| 	#endif | ||||
| 	); | ||||
| } | ||||
|  | ||||
| static int bfn_sqrt (ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl) | ||||
| { | ||||
| 	return bfn_math_1 (run, fnm, fnl,  | ||||
| 	#if defined(HAVE_SQRTL) | ||||
| 		BFN_MATH_LD, sqrtl | ||||
| 	#elif defined(HAVE_SQRT) | ||||
| 		BFN_MATH_D, sqrt | ||||
| 	#elif defined(HAVE_SQRTF) | ||||
| 		BFN_MATH_F, sqrtf | ||||
| 	#else | ||||
| 		#error ### no sqrt function available ### | ||||
| 	#endif | ||||
| 	); | ||||
| } | ||||
|  | ||||
| static int bfn_int (ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl) | ||||
| { | ||||
| 	ase_size_t nargs; | ||||
| 	ase_awk_val_t* a0; | ||||
| @ -724,14 +978,7 @@ int aes_awk_func_sleep ( | ||||
| 	if (n == -1) return -1; | ||||
| 	if (n == 1) lv = (ase_long_t)rv; | ||||
|  | ||||
| #ifdef _WIN32 | ||||
| 	Sleep ((DWORD)(lv * 1000)); | ||||
| 	n = 0; | ||||
| #else | ||||
| 	n = sleep (lv);	 | ||||
| #endif | ||||
|  | ||||
| 	r = ase_awk_makeintval (run, n); | ||||
| 	r = ase_awk_makeintval (run, lv); | ||||
| 	if (r == ASE_NULL) | ||||
| 	{ | ||||
| 		ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM); | ||||
| @ -741,4 +988,113 @@ int aes_awk_func_sleep ( | ||||
| 	ase_awk_setretval (run, r); | ||||
| 	return 0; | ||||
| } | ||||
| #endif | ||||
|  | ||||
| static int bfn_rand (ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl) | ||||
| { | ||||
| 	ase_awk_val_t* r; | ||||
|  | ||||
| 	r = ase_awk_makeintval (run, rand()); | ||||
| 	if (r == ASE_NULL) | ||||
| 	{ | ||||
| 		ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	ase_awk_setretval (run, r); | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| static int bfn_srand (ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl) | ||||
| { | ||||
| 	ase_size_t nargs; | ||||
| 	ase_awk_val_t* a0; | ||||
| 	ase_long_t lv; | ||||
| 	ase_real_t rv; | ||||
| 	ase_awk_val_t* r; | ||||
| 	int n; | ||||
| 	unsigned int prev; | ||||
| 	rxtn_t* rxtn; | ||||
|  | ||||
| 	rxtn = ase_awk_getrundata (run); | ||||
| 	nargs = ase_awk_getnargs (run); | ||||
| 	ASE_ASSERT (nargs == 0 || nargs == 1); | ||||
|  | ||||
| 	prev = rxtn->seed; | ||||
|  | ||||
| 	if (nargs == 1) | ||||
| 	{ | ||||
| 		a0 = ase_awk_getarg (run, 0); | ||||
|  | ||||
| 		n = ase_awk_valtonum (run, a0, &lv, &rv); | ||||
| 		if (n == -1) return -1; | ||||
| 		if (n == 1) lv = (ase_long_t)rv; | ||||
|  | ||||
| 		rxtn->seed = lv; | ||||
| 	} | ||||
| 	else | ||||
| 	{ | ||||
| 		ase_time_t now; | ||||
|  | ||||
| 		if (ase_gettime(&now) == -1) rxtn->seed >>= 1; | ||||
| 		else rxtn->seed = (unsigned int)now; | ||||
| 	} | ||||
|  | ||||
|         srand (rxtn->seed); | ||||
|  | ||||
| 	r = ase_awk_makeintval (run, prev); | ||||
| 	if (r == ASE_NULL) | ||||
| 	{ | ||||
| 		ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	ase_awk_setretval (run, r); | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| static int bfn_systime (ase_awk_run_t* run, const ase_char_t* fnm, ase_size_t fnl) | ||||
| { | ||||
| 	ase_awk_val_t* r; | ||||
| 	ase_time_t now; | ||||
| 	int n; | ||||
| 	 | ||||
| 	if (ase_gettime(&now) == -1) now = 0; | ||||
|  | ||||
| 	r = ase_awk_makeintval (run, now / ASE_MSEC_IN_SEC); | ||||
| 	if (r == ASE_NULL) | ||||
| 	{ | ||||
| 		ase_awk_setrunerrnum (run, ASE_AWK_ENOMEM); | ||||
| 		return -1; | ||||
| 	} | ||||
|  | ||||
| 	ase_awk_setretval (run, r); | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| #define ADD_FUNC(awk,name,min,max,bfn) \ | ||||
|         if (ase_awk_addfunc (\ | ||||
| 		(awk), (name), ase_strlen(name), \ | ||||
| 		0, (min), (max), ASE_NULL, (bfn)) == ASE_NULL) return -1; | ||||
|  | ||||
| static int add_functions (ase_awk_t* awk) | ||||
| { | ||||
|         ADD_FUNC (awk, ASE_T("sin"),        1, 1, bfn_sin); | ||||
|         ADD_FUNC (awk, ASE_T("cos"),        1, 1, bfn_cos); | ||||
|         ADD_FUNC (awk, ASE_T("tan"),        1, 1, bfn_tan); | ||||
|         ADD_FUNC (awk, ASE_T("atan"),       1, 1, bfn_atan); | ||||
|         ADD_FUNC (awk, ASE_T("atan2"),      2, 2, bfn_atan2); | ||||
|         ADD_FUNC (awk, ASE_T("log"),        1, 1, bfn_log); | ||||
|         ADD_FUNC (awk, ASE_T("exp"),        1, 1, bfn_exp); | ||||
|         ADD_FUNC (awk, ASE_T("sqrt"),       1, 1, bfn_sqrt); | ||||
|         ADD_FUNC (awk, ASE_T("int"),        1, 1, bfn_int); | ||||
|         ADD_FUNC (awk, ASE_T("rand"),       0, 0, bfn_rand); | ||||
|         ADD_FUNC (awk, ASE_T("srand"),      0, 1, bfn_srand); | ||||
|         ADD_FUNC (awk, ASE_T("systime"),    0, 0, bfn_systime); | ||||
| /* | ||||
|         ADD_FUNC (awk, ASE_T("strftime"),   0, 2, bfn_strftime); | ||||
|         ADD_FUNC (awk, ASE_T("strfgmtime"), 0, 2, bfn_strfgmtime); | ||||
|         ADD_FUNC (awk, ASE_T("system"),     1, 1, bfn_system); | ||||
| */ | ||||
|  | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| @ -7,6 +7,7 @@ libasecmn_la_SOURCES = mem.h chr.h \ | ||||
| 	mem.c chr.c chr_cnv.c rex.c str_bas.c str_cnv.c str_dyn.c \ | ||||
| 	lda.c map.c sll.c dll.c opt.c \ | ||||
| 	fio.c sio.c tio.c tio_get.c tio_put.c \ | ||||
| 	time.c \ | ||||
| 	misc.c | ||||
| libasecmn_la_LDFLAGS = -version-info 1:0:0 -no-undefined | ||||
|  | ||||
|  | ||||
| @ -53,7 +53,7 @@ LTLIBRARIES = $(lib_LTLIBRARIES) | ||||
| libasecmn_la_LIBADD = | ||||
| am_libasecmn_la_OBJECTS = mem.lo chr.lo chr_cnv.lo rex.lo str_bas.lo \ | ||||
| 	str_cnv.lo str_dyn.lo lda.lo map.lo sll.lo dll.lo opt.lo \ | ||||
| 	fio.lo sio.lo tio.lo tio_get.lo tio_put.lo misc.lo | ||||
| 	fio.lo sio.lo tio.lo tio_get.lo tio_put.lo time.lo misc.lo | ||||
| libasecmn_la_OBJECTS = $(am_libasecmn_la_OBJECTS) | ||||
| libasecmn_la_LINK = $(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) \ | ||||
| 	$(LIBTOOLFLAGS) --mode=link $(CCLD) $(AM_CFLAGS) $(CFLAGS) \ | ||||
| @ -204,6 +204,7 @@ libasecmn_la_SOURCES = mem.h chr.h \ | ||||
| 	mem.c chr.c chr_cnv.c rex.c str_bas.c str_cnv.c str_dyn.c \ | ||||
| 	lda.c map.c sll.c dll.c opt.c \ | ||||
| 	fio.c sio.c tio.c tio_get.c tio_put.c \ | ||||
| 	time.c \ | ||||
| 	misc.c | ||||
|  | ||||
| libasecmn_la_LDFLAGS = -version-info 1:0:0 -no-undefined | ||||
| @ -291,6 +292,7 @@ distclean-compile: | ||||
| @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str_bas.Plo@am__quote@ | ||||
| @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str_cnv.Plo@am__quote@ | ||||
| @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/str_dyn.Plo@am__quote@ | ||||
| @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/time.Plo@am__quote@ | ||||
| @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tio.Plo@am__quote@ | ||||
| @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tio_get.Plo@am__quote@ | ||||
| @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/tio_put.Plo@am__quote@ | ||||
|  | ||||
							
								
								
									
										76
									
								
								ase/lib/cmn/time.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										76
									
								
								ase/lib/cmn/time.c
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,76 @@ | ||||
| /* | ||||
|  * $Id$ | ||||
|  */ | ||||
|  | ||||
| #include <ase/cmn/time.h> | ||||
|  | ||||
| #ifdef _WIN32 | ||||
| #include <windows.h> | ||||
| #else | ||||
| #include <sys/time.h> | ||||
| #include <time.h> | ||||
| #endif | ||||
|  | ||||
| #if defined(ASE_USE_SYSCALL) && defined(HAVE_SYS_SYSCALL_H) | ||||
| #include <sys/syscall.h> | ||||
| #endif | ||||
|  | ||||
| int ase_gettime (ase_time_t* t) | ||||
| { | ||||
| #ifdef _WIN32 | ||||
| 	SYSTEMTIME st; | ||||
| 	FILETIME ft; | ||||
|  | ||||
| 	/*  | ||||
| 	 * MSDN: The FILETIME structure is a 64-bit value representing the  | ||||
| 	 *       number of 100-nanosecond intervals since January 1, 1601 (UTC). | ||||
| 	 */ | ||||
|  | ||||
| 	GetSystemTime (&st); | ||||
| 	if (SystemTimeToFileTime (&st, &ft) == FALSE) return -1; | ||||
| 	*t = ((ase_time_t)(*((ase_int64_t*)&ft)) / (10 * 1000)); | ||||
| 	*t -= EPOCH_DIFF_MSECS; | ||||
| 	return 0; | ||||
| #else | ||||
| 	struct timeval tv; | ||||
| 	int n; | ||||
|  | ||||
| #ifdef SYS_gettimeofday | ||||
| 	n = syscall (SYS_gettimeofday, &tv, ASE_NULL); | ||||
| #else | ||||
| 	n = gettimeofday (&tv, ASE_NULL); | ||||
| #endif | ||||
| 	if (n == -1) return -1; | ||||
|  | ||||
| 	*t = tv.tv_sec * ASE_MSEC_IN_SEC + tv.tv_usec / ASE_USEC_IN_MSEC; | ||||
| 	return 0; | ||||
| #endif | ||||
| } | ||||
|  | ||||
| int ase_settime (ase_time_t t) | ||||
| { | ||||
| #ifdef _WIN32 | ||||
| 	FILETIME ft; | ||||
| 	SYSTEMTIME st; | ||||
|  | ||||
| 	*((ase_int64_t*)&ft) = ((value + EPOCH_DIFF_MSECS) * (10 * 1000)); | ||||
| 	if (FileTimeToSystemTime (&ft, &st) == FALSE) return -1; | ||||
| 	if (SetSystemTime(&st) == FALSE) return -1; | ||||
| 	return 0; | ||||
| #else | ||||
| 	struct timeval tv; | ||||
| 	int n; | ||||
|  | ||||
| 	tv.tv_sec = t / ASE_MSEC_IN_SEC; | ||||
| 	tv.tv_usec = (t % ASE_MSEC_IN_SEC) * ASE_USEC_IN_MSEC; | ||||
|  | ||||
| #ifdef SYS_settimeofday | ||||
| 	n = syscall (SYS_settimeofday, &tv, ASE_NULL); | ||||
| #else | ||||
| 	n = settimeofday (&tv, ASE_NULL); | ||||
| #endif | ||||
| 	if (n == -1) return -1; | ||||
| 	return 0; | ||||
| #endif | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user