diff --git a/qse/README b/qse/README index cca8111f..13e1c479 100644 --- a/qse/README +++ b/qse/README @@ -1,9 +1,19 @@ -QSE provides a script engine for various scripting languages and utility programs. It aims to produce a flexible script engine framework that can be embedded into an application. A hosting application can access various aspects of the embedded script engine and vice versa. +QSE is a code library that implements various Unix utilities in an +embeddable form and provides a set of APIs to embed them into an application. +The APIs have been designed to be flexible enough to access various aspects of +a hosting application and an embedded object from each other. By embedding +a Unix utility into an application, a developer is relieved of the need to +invoke an external command and is given tighter control over it. -The library is licended under the Apache License, Version 2.0. +Currently the library implements the following utilities: +- SED Stream Editor +- AWK Interpreter + +As the library grows, more utilities will be added. + +The library is licensed under the Apache License, Version 2.0. The project webpage: http://qse.googlecode.com/ -For furthur information, contact: +For further information, contact: Chung, Hyung-Hwan - diff --git a/qse/cmd/lsp/lsp.c b/qse/cmd/lsp/lsp.c index 184825ac..4d9acab1 100644 --- a/qse/cmd/lsp/lsp.c +++ b/qse/cmd/lsp/lsp.c @@ -242,14 +242,14 @@ int lsp_main (int argc, qse_char_t* argv[]) return -1; } - prmfns.mmgr.data = &prmfns_data; + prmfns.mmgr.udd = &prmfns_data; #else - prmfns.mmgr.data = QSE_NULL; + prmfns.mmgr.udd = QSE_NULL; #endif prmfns.misc.sprintf = custom_lsp_sprintf; prmfns.misc.dprintf = custom_lsp_dprintf; - prmfns.misc.data = QSE_NULL; + prmfns.misc.udd = QSE_NULL; lsp = qse_lsp_open (&prmfns, opt_memsize, opt_meminc); if (lsp == QSE_NULL) diff --git a/qse/configure b/qse/configure index f61feede..a6115768 100755 --- a/qse/configure +++ b/qse/configure @@ -17067,7 +17067,9 @@ fi -for ac_header in stddef.h wchar.h wctype.h errno.h signal.h sys/syscall.h + + +for ac_header in stddef.h wchar.h wctype.h errno.h signal.h time.h sys/time.h sys/syscall.h do as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh` if { as_var=$as_ac_Header; eval "test \"\${$as_var+set}\" = set"; }; then diff --git a/qse/configure.ac b/qse/configure.ac index 0d2e620f..1f37391d 100644 --- a/qse/configure.ac +++ b/qse/configure.ac @@ -87,7 +87,7 @@ AC_SUBST(LIBM, $LIBM) dnl check header files. AC_HEADER_STDC -AC_CHECK_HEADERS([stddef.h wchar.h wctype.h errno.h signal.h sys/syscall.h]) +AC_CHECK_HEADERS([stddef.h wchar.h wctype.h errno.h signal.h time.h sys/time.h sys/syscall.h]) dnl check data types AC_CHECK_TYPE([wchar_t], diff --git a/qse/include/qse/cmn/time.h b/qse/include/qse/cmn/time.h index b8b71622..7bfc50c3 100644 --- a/qse/include/qse/cmn/time.h +++ b/qse/include/qse/cmn/time.h @@ -1,5 +1,5 @@ /* - * $Id: time.h 186 2009-06-06 13:42:57Z hyunghwan.chung $ + * $Id: time.h 187 2009-06-07 05:03:44Z hyunghwan.chung $ * Copyright 2006-2009 Chung, Hyung-Hwan. @@ -29,6 +29,9 @@ #define QSE_BTIME_YEAR_BASE (1900) +#define QSE_DAYS_PER_NORMYEAR (365) +#define QSE_DAYS_PER_LEAPYEAR (366) + #define QSE_DAYS_PER_WEEK (7) #define QSE_MONS_PER_YEAR (12) #define QSE_HOURS_PER_DAY (24) @@ -47,7 +50,8 @@ #define QSE_USECS_PER_SEC (QSE_USECS_PER_MSEC*QSE_MSECS_PER_SEC) #define QSE_IS_LEAPYEAR(year) ((!((year)%4) && ((year)%100)) || !((year)%400)) -#define QSE_DAYS_PER_YEAR(year) (QSE_IS_LEAPYEAR(year)? 366: 365) +#define QSE_DAYS_PER_YEAR(year) \ + (QSE_IS_LEAPYEAR(year)? QSE_DAYS_PER_LEAPYEAR: QSE_DAYS_PER_NORMYEAR) /* number of milliseconds since the Epoch (00:00:00 UTC, Jan 1, 1970) */ typedef qse_long_t qse_ntime_t; diff --git a/qse/include/qse/config.h.in b/qse/include/qse/config.h.in index fa8df1b2..d8689b97 100644 --- a/qse/include/qse/config.h.in +++ b/qse/include/qse/config.h.in @@ -135,6 +135,9 @@ /* Define to 1 if you have the header file. */ #undef HAVE_SYS_SYSCALL_H +/* Define to 1 if you have the header file. */ +#undef HAVE_SYS_TIME_H + /* Define to 1 if you have the header file. */ #undef HAVE_SYS_TYPES_H @@ -153,6 +156,9 @@ /* Define to 1 if you have the `timelocal' function. */ #undef HAVE_TIMELOCAL +/* Define to 1 if you have the header file. */ +#undef HAVE_TIME_H + /* Define to 1 if you have the header file. */ #undef HAVE_UNISTD_H diff --git a/qse/include/qse/lsp/lsp.h b/qse/include/qse/lsp/lsp.h index 23ce0d8e..0f1cc0cf 100644 --- a/qse/include/qse/lsp/lsp.h +++ b/qse/include/qse/lsp/lsp.h @@ -54,7 +54,7 @@ struct qse_lsp_prmfns_t { qse_lsp_sprintf_t sprintf; qse_lsp_dprintf_t dprintf; - void* data; + void* udd; } misc; }; diff --git a/qse/lib/cmn/syscall.h b/qse/lib/cmn/syscall.h index 6bf0d40f..c6aae625 100644 --- a/qse/lib/cmn/syscall.h +++ b/qse/lib/cmn/syscall.h @@ -1,5 +1,5 @@ /* - * $Id: syscall.h 97 2009-03-10 10:39:18Z hyunghwan.chung $ + * $Id: syscall.h 187 2009-06-07 05:03:44Z hyunghwan.chung $ * Copyright 2006-2009 Chung, Hyung-Hwan. @@ -36,6 +36,12 @@ #ifdef HAVE_ERRNO_H # include #endif +#ifdef HAVE_TIME_H +# include +#endif +#ifdef HAVE_SYS_TIME_H +# include +#endif #if defined(QSE_USE_SYSCALL) && defined(HAVE_SYS_SYSCALL_H) # include @@ -189,4 +195,17 @@ # define QSE_cHROOT(path) chroot(path) #endif +#ifdef SYS_gettimeofday +# define QSE_GETTIMEOFDAY(tv,tz) syscall(SYS_gettimeofday, tv, tz) +#else +# define QSE_GETTIMEOFDAY(tv,tz) gettimeofday(tv,tz) +#endif + +#ifdef SYS_settimeofday +# define QSE_SETTIMEOFDAY(tv,tz) syscall(SYS_settimeofday, tv, tz) +#else +# define QSE_SETTIMEOFDAY(tv,tz) settimeofday(tv,tz) +#endif + + #endif diff --git a/qse/lib/cmn/time.c b/qse/lib/cmn/time.c index 4c3c7201..92da2e38 100644 --- a/qse/lib/cmn/time.c +++ b/qse/lib/cmn/time.c @@ -1,5 +1,5 @@ /* - * $Id: time.c 186 2009-06-06 13:42:57Z hyunghwan.chung $ + * $Id: time.c 187 2009-06-07 05:03:44Z hyunghwan.chung $ * Copyright 2006-2009 Chung, Hyung-Hwan. @@ -52,14 +52,23 @@ static const int mdays_tot[2][QSE_MONS_PER_YEAR] = { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 } }; -/* number of days from the beginning of the year to the end of of - * a previous month. adjust for leap years in the code. */ +/* number of days from beginning of a previous month to the end of + * the year. adjust for leap years in the code. */ static const int mdays_rtot[2][QSE_MONS_PER_YEAR] = { { 334, 306, 275, 245, 214, 184, 153, 122, 92, 61, 31, 0 }, { 335, 306, 275, 245, 214, 184, 153, 122, 92, 61, 31, 0 } }; +/* get number of extra days for leap years between fy and ty inclusive */ +static int get_leap_days (int fy, int ty) +{ + fy--; ty--; + return (ty / 4 - fy / 4) - + (ty / 100 - fy / 100) + + (ty / 400 - fy / 400); +} + int qse_gettime (qse_ntime_t* t) { #ifdef _WIN32 @@ -80,11 +89,7 @@ int qse_gettime (qse_ntime_t* t) struct timeval tv; int n; -#ifdef SYS_gettimeofday - n = syscall (SYS_gettimeofday, &tv, QSE_NULL); -#else - n = gettimeofday (&tv, QSE_NULL); -#endif + n = QSE_GETTIMEOFDAY (&tv, QSE_NULL); if (n == -1) return -1; *t = (qse_ntime_t)tv.tv_sec*QSE_MSECS_PER_SEC + @@ -112,24 +117,16 @@ int qse_settime (qse_ntime_t t) /* #if defined CLOCK_REALTIME && HAVE_CLOCK_SETTIME - { - int r = clock_settime (CLOCK_REALTIME, ts); - if (r == 0 || errno == EPERM) - return r; - } + { + int r = clock_settime (CLOCK_REALTIME, ts); + if (r == 0 || errno == EPERM) + return r; + } #elif HAVE_STIME - / * This fails to compile on OSF1 V5.1, due to stime requiring - a `long int*' and tv_sec is `int'. But that system does provide - settimeofday. * / - return stime (&ts->tv_sec); + return stime (&ts->tv_sec); #else */ - -#ifdef SYS_settimeofday - n = syscall (SYS_settimeofday, &tv, QSE_NULL); -#else - n = settimeofday (&tv, QSE_NULL); -#endif + n = QSE_SETTIMEOFDAY (&tv, QSE_NULL); if (n == -1) return -1; return 0; #endif @@ -294,14 +291,20 @@ int qse_timegm (const qse_btime_t* bt, qse_ntime_t* nt) int y = bt->year + QSE_BTIME_YEAR_BASE; int midx = QSE_IS_LEAPYEAR(y)? 1: 0; + QSE_ASSERT (bt->mon >= 0 && bt->mon < QSE_MONS_PER_YEAR); + if (y < QSE_EPOCH_YEAR) { int x; - for (x = y; x < QSE_EPOCH_YEAR - 1; x++) - n += QSE_DAYS_PER_YEAR(x); - for (x = bt->mon + 1; x < QSE_MONS_PER_YEAR; x++) - n += mdays[midx][x]; + /*for (x = y; x < QSE_EPOCH_YEAR - 1; x++) + n += QSE_DAYS_PER_YEAR(x);*/ + n = QSE_DAYS_PER_NORMYEAR * (QSE_EPOCH_YEAR - 1 - y) + + get_leap_days (y, QSE_EPOCH_YEAR - 1); + + /*for (x = bt->mon + 1; x < QSE_MONS_PER_YEAR; x++) + n += mdays[midx][x];*/ + n += mdays_rtot[midx][bt->mon]; n += mdays[midx][bt->mon] - bt->mday; if (midx == 1) n -= 1; @@ -318,8 +321,10 @@ int qse_timegm (const qse_btime_t* bt, qse_ntime_t* nt) { int x; - for (x = QSE_EPOCH_YEAR; x < y; x++) - n += QSE_DAYS_PER_YEAR(x); + /*for (x = QSE_EPOCH_YEAR; x < y; x++) + n += QSE_DAYS_PER_YEAR(x);*/ + n = QSE_DAYS_PER_NORMYEAR * (y - QSE_EPOCH_YEAR) + + get_leap_days (QSE_EPOCH_YEAR, y); /*for (x = 0; x < bt->mon; x++) n += mdays[midx][x];*/ n += mdays_tot[midx][bt->mon]; diff --git a/qse/lib/lsp/err.c b/qse/lib/lsp/err.c index fa004a67..05d70473 100644 --- a/qse/lib/lsp/err.c +++ b/qse/lib/lsp/err.c @@ -64,7 +64,7 @@ void qse_lsp_seterror ( { case 0: lsp->prmfns.misc.sprintf ( - lsp->prmfns.misc.data, + lsp->prmfns.misc.udd, lsp->errmsg, QSE_COUNTOF(lsp->errmsg), errfmt); @@ -72,7 +72,7 @@ void qse_lsp_seterror ( case 1: lsp->prmfns.misc.sprintf ( - lsp->prmfns.misc.data, + lsp->prmfns.misc.udd, lsp->errmsg, QSE_COUNTOF(lsp->errmsg), errfmt, @@ -81,7 +81,7 @@ void qse_lsp_seterror ( case 2: lsp->prmfns.misc.sprintf ( - lsp->prmfns.misc.data, + lsp->prmfns.misc.udd, lsp->errmsg, QSE_COUNTOF(lsp->errmsg), errfmt, @@ -91,7 +91,7 @@ void qse_lsp_seterror ( case 3: lsp->prmfns.misc.sprintf ( - lsp->prmfns.misc.data, + lsp->prmfns.misc.udd, lsp->errmsg, QSE_COUNTOF(lsp->errmsg), errfmt, @@ -102,7 +102,7 @@ void qse_lsp_seterror ( case 4: lsp->prmfns.misc.sprintf ( - lsp->prmfns.misc.data, + lsp->prmfns.misc.udd, lsp->errmsg, QSE_COUNTOF(lsp->errmsg), errfmt, @@ -114,7 +114,7 @@ void qse_lsp_seterror ( case 5: lsp->prmfns.misc.sprintf ( - lsp->prmfns.misc.data, + lsp->prmfns.misc.udd, lsp->errmsg, QSE_COUNTOF(lsp->errmsg), errfmt, diff --git a/qse/lib/lsp/lsp.c b/qse/lib/lsp/lsp.c index 5f9b8ed4..272b5c30 100644 --- a/qse/lib/lsp/lsp.c +++ b/qse/lib/lsp/lsp.c @@ -34,7 +34,7 @@ qse_lsp_t* qse_lsp_open ( lsp = (qse_lsp_t*) malloc (QSE_SIZEOF(qse_lsp_t)); #else lsp = (qse_lsp_t*) prmfns->mmgr.alloc ( - prmfns->mmgr.data, QSE_SIZEOF(qse_lsp_t)); + prmfns->mmgr.udd, QSE_SIZEOF(qse_lsp_t)); #endif if (lsp == QSE_NULL) return QSE_NULL; diff --git a/qse/lib/lsp/print.c b/qse/lib/lsp/print.c index 65aea3e7..5467cc48 100644 --- a/qse/lib/lsp/print.c +++ b/qse/lib/lsp/print.c @@ -45,22 +45,22 @@ static int __print (qse_lsp_t* lsp, const qse_lsp_obj_t* obj, qse_bool_t prt_con case QSE_LSP_OBJ_INT: #if QSE_SIZEOF_LONG_LONG > 0 lsp->prmfns.misc.sprintf ( - lsp->prmfns.misc.data, + lsp->prmfns.misc.udd, buf, QSE_COUNTOF(buf), QSE_T("%lld"), (long long)QSE_LSP_IVAL(obj)); #elif QSE_SIZEOF___INT64 > 0 lsp->prmfns.misc.sprintf ( - lsp->prmfns.misc.data, + lsp->prmfns.misc.udd, buf, QSE_COUNTOF(buf), QSE_T("%I64d"), (__int64)QSE_LSP_IVAL(obj)); #elif QSE_SIZEOF_LONG > 0 lsp->prmfns.misc.sprintf ( - lsp->prmfns.misc.data, + lsp->prmfns.misc.udd, buf, QSE_COUNTOF(buf), QSE_T("%ld"), (long)QSE_LSP_IVAL(obj)); #elif QSE_SIZEOF_INT > 0 lsp->prmfns.misc.sprintf ( - lsp->prmfns.misc.data, + lsp->prmfns.misc.udd, buf, QSE_COUNTOF(buf), QSE_T("%d"), (int)QSE_LSP_IVAL(obj)); #else @@ -71,7 +71,7 @@ static int __print (qse_lsp_t* lsp, const qse_lsp_obj_t* obj, qse_bool_t prt_con case QSE_LSP_OBJ_REAL: lsp->prmfns.misc.sprintf ( - lsp->prmfns.misc.data, + lsp->prmfns.misc.udd, buf, QSE_COUNTOF(buf), QSE_T("%Lf"), #ifdef __MINGW32__ @@ -141,7 +141,7 @@ static int __print (qse_lsp_t* lsp, const qse_lsp_obj_t* obj, qse_bool_t prt_con default: lsp->prmfns.misc.sprintf ( - lsp->prmfns.misc.data, + lsp->prmfns.misc.udd, buf, QSE_COUNTOF(buf), QSE_T("unknown object type: %d"), QSE_LSP_TYPE(obj)); OUTPUT_STR (lsp, buf);