2012-11-09 17:31:33 +00:00
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
Copyright 2006-2012 Chung, Hyung-Hwan.
|
|
|
|
This file is part of QSE.
|
|
|
|
|
|
|
|
QSE is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as
|
|
|
|
published by the Free Software Foundation, either version 3 of
|
|
|
|
the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
QSE is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with QSE. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2012-11-01 06:42:38 +00:00
|
|
|
#include <qse/awk/awk.h>
|
2012-10-21 16:19:03 +00:00
|
|
|
#include <qse/cmn/str.h>
|
2012-11-05 05:20:12 +00:00
|
|
|
#include <qse/cmn/time.h>
|
2012-10-21 16:19:03 +00:00
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
# include <windows.h>
|
|
|
|
# include <process.h>
|
|
|
|
#elif defined(__OS2__)
|
|
|
|
# define INCL_DOSPROCESS
|
|
|
|
# define INCL_DOSEXCEPTIONS
|
|
|
|
# define INCL_ERRORS
|
|
|
|
# include <os2.h>
|
|
|
|
#elif defined(__DOS__)
|
|
|
|
# include <dos.h>
|
|
|
|
#else
|
2012-11-05 05:20:12 +00:00
|
|
|
# include "../../lib/cmn/syscall.h"
|
|
|
|
# if defined(HAVE_SYS_SYSCALL_H)
|
|
|
|
# include <sys/syscall.h>
|
|
|
|
# endif
|
2012-10-21 16:19:03 +00:00
|
|
|
#endif
|
|
|
|
|
2012-11-05 05:20:12 +00:00
|
|
|
#include <stdlib.h> /* getenv */
|
|
|
|
|
|
|
|
|
2012-10-23 17:08:56 +00:00
|
|
|
static int fnc_fork (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
2012-10-22 09:36:15 +00:00
|
|
|
{
|
2012-10-31 08:31:58 +00:00
|
|
|
qse_long_t pid;
|
|
|
|
qse_awk_val_t* retv;
|
|
|
|
|
2012-10-22 09:36:15 +00:00
|
|
|
#if defined(_WIN32)
|
2012-10-31 08:31:58 +00:00
|
|
|
/* TOOD: implement this*/
|
|
|
|
pid = -1;
|
2012-10-22 09:36:15 +00:00
|
|
|
|
|
|
|
#elif defined(__OS2__)
|
2012-10-31 08:31:58 +00:00
|
|
|
/* TOOD: implement this*/
|
|
|
|
pid = -1;
|
2012-10-22 09:36:15 +00:00
|
|
|
|
|
|
|
#elif defined(__DOS__)
|
2012-10-31 08:31:58 +00:00
|
|
|
/* TOOD: implement this*/
|
|
|
|
pid = -1;
|
2012-10-22 09:36:15 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
pid = fork ();
|
2012-10-31 08:31:58 +00:00
|
|
|
#endif
|
|
|
|
|
2012-10-23 17:08:56 +00:00
|
|
|
retv = qse_awk_rtx_makeintval (rtx, pid);
|
|
|
|
if (retv == QSE_NULL) return -1;
|
2012-10-31 08:31:58 +00:00
|
|
|
|
2012-10-23 17:08:56 +00:00
|
|
|
qse_awk_rtx_setretval (rtx, retv);
|
2012-10-22 09:36:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-05 05:20:12 +00:00
|
|
|
static int fnc_wait (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
2012-10-31 13:51:32 +00:00
|
|
|
{
|
|
|
|
qse_long_t pid;
|
|
|
|
qse_awk_val_t* retv;
|
2012-11-05 05:20:12 +00:00
|
|
|
int rx;
|
|
|
|
|
|
|
|
/* TODO: handle more parameters */
|
2012-10-31 13:51:32 +00:00
|
|
|
|
2012-11-05 05:20:12 +00:00
|
|
|
rx = qse_awk_rtx_valtolong (rtx, qse_awk_rtx_getarg (rtx, 0), &pid);
|
|
|
|
if (rx >= 0)
|
|
|
|
{
|
2012-10-31 13:51:32 +00:00
|
|
|
#if defined(_WIN32)
|
2012-11-05 05:20:12 +00:00
|
|
|
/* TOOD: implement this*/
|
|
|
|
rx = -1;
|
2012-10-31 13:51:32 +00:00
|
|
|
#elif defined(__OS2__)
|
2012-11-05 05:20:12 +00:00
|
|
|
/* TOOD: implement this*/
|
|
|
|
rx = -1;
|
2012-10-31 13:51:32 +00:00
|
|
|
#elif defined(__DOS__)
|
2012-11-05 05:20:12 +00:00
|
|
|
/* TOOD: implement this*/
|
|
|
|
rx = -1;
|
2012-10-31 13:51:32 +00:00
|
|
|
#else
|
2012-11-05 05:20:12 +00:00
|
|
|
rx = waitpid (pid, QSE_NULL, 0);
|
2012-10-31 13:51:32 +00:00
|
|
|
#endif
|
2012-11-05 05:20:12 +00:00
|
|
|
}
|
2012-10-31 13:51:32 +00:00
|
|
|
|
2012-11-05 05:20:12 +00:00
|
|
|
retv = qse_awk_rtx_makeintval (rtx, rx);
|
2012-10-31 13:51:32 +00:00
|
|
|
if (retv == QSE_NULL) return -1;
|
|
|
|
|
|
|
|
qse_awk_rtx_setretval (rtx, retv);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-05 05:20:12 +00:00
|
|
|
static int fnc_kill (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
2012-10-22 09:36:15 +00:00
|
|
|
{
|
2012-11-05 05:20:12 +00:00
|
|
|
qse_long_t pid, sig;
|
2012-10-23 17:08:56 +00:00
|
|
|
qse_awk_val_t* retv;
|
2012-10-31 08:31:58 +00:00
|
|
|
int rx;
|
2012-10-22 09:36:15 +00:00
|
|
|
|
2012-11-05 05:20:12 +00:00
|
|
|
if (qse_awk_rtx_valtolong (rtx, qse_awk_rtx_getarg (rtx, 0), &pid) <= -1 ||
|
|
|
|
qse_awk_rtx_valtolong (rtx, qse_awk_rtx_getarg (rtx, 1), &sig) <= -1)
|
|
|
|
{
|
|
|
|
rx = -1;
|
|
|
|
}
|
|
|
|
else
|
2012-10-31 08:31:58 +00:00
|
|
|
{
|
2012-10-22 09:36:15 +00:00
|
|
|
#if defined(_WIN32)
|
2012-10-31 08:31:58 +00:00
|
|
|
/* TOOD: implement this*/
|
|
|
|
rx = -1;
|
2012-10-22 09:36:15 +00:00
|
|
|
#elif defined(__OS2__)
|
2012-10-31 08:31:58 +00:00
|
|
|
/* TOOD: implement this*/
|
|
|
|
rx = -1;
|
2012-10-22 09:36:15 +00:00
|
|
|
#elif defined(__DOS__)
|
2012-10-31 08:31:58 +00:00
|
|
|
/* TOOD: implement this*/
|
|
|
|
rx = -1;
|
|
|
|
#else
|
2012-11-05 05:20:12 +00:00
|
|
|
rx = kill (pid, sig);
|
2012-10-31 08:31:58 +00:00
|
|
|
#endif
|
|
|
|
}
|
2012-10-22 09:36:15 +00:00
|
|
|
|
2012-10-31 08:31:58 +00:00
|
|
|
retv = qse_awk_rtx_makeintval (rtx, rx);
|
|
|
|
if (retv == QSE_NULL) return -1;
|
|
|
|
|
|
|
|
qse_awk_rtx_setretval (rtx, retv);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-03 17:44:23 +00:00
|
|
|
static int fnc_getpgrp (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
|
|
|
{
|
|
|
|
qse_long_t pid;
|
|
|
|
qse_awk_val_t* retv;
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
/* TOOD: implement this*/
|
|
|
|
pid = -1;
|
|
|
|
|
|
|
|
#elif defined(__OS2__)
|
|
|
|
/* TOOD: implement this*/
|
|
|
|
pid = -1;
|
|
|
|
|
|
|
|
#elif defined(__DOS__)
|
|
|
|
/* TOOD: implement this*/
|
|
|
|
pid = -1;
|
|
|
|
|
|
|
|
#else
|
|
|
|
pid = getpgrp ();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
retv = qse_awk_rtx_makeintval (rtx, pid);
|
|
|
|
if (retv == QSE_NULL) return -1;
|
|
|
|
|
|
|
|
qse_awk_rtx_setretval (rtx, retv);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-05 05:20:12 +00:00
|
|
|
static int fnc_getpid (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
|
|
|
{
|
|
|
|
qse_long_t pid;
|
|
|
|
qse_awk_val_t* retv;
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
pid = GetCurrentProcessId();
|
|
|
|
|
|
|
|
#elif defined(__OS2__)
|
|
|
|
PTIB tib;
|
|
|
|
PPIB pib;
|
|
|
|
|
|
|
|
pid = (DosGetInfoBlocks (&tib, &pib) == NO_ERROR)?
|
|
|
|
pib->pib_ulpid: -1;
|
|
|
|
|
|
|
|
#elif defined(__DOS__)
|
|
|
|
/* TOOD: implement this*/
|
|
|
|
pid = -1;
|
|
|
|
|
|
|
|
#else
|
|
|
|
pid = getpid ();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
retv = qse_awk_rtx_makeintval (rtx, pid);
|
|
|
|
if (retv == QSE_NULL) return -1;
|
|
|
|
|
|
|
|
qse_awk_rtx_setretval (rtx, retv);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fnc_gettid (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
|
|
|
{
|
|
|
|
qse_long_t pid;
|
|
|
|
qse_awk_val_t* retv;
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
pid = GetCurrentThreadId();
|
|
|
|
|
|
|
|
#elif defined(__OS2__)
|
|
|
|
PTIB tib;
|
|
|
|
PPIB pib;
|
|
|
|
|
|
|
|
pid = (DosGetInfoBlocks (&tib, &pib) == NO_ERROR && tib->tib_ptib2)?
|
|
|
|
tib->tib_ptib2->tib2_ultid: -1;
|
|
|
|
|
|
|
|
#elif defined(__DOS__)
|
|
|
|
/* TOOD: implement this*/
|
|
|
|
pid = -1;
|
|
|
|
|
|
|
|
#else
|
|
|
|
#if defined(SYS_gettid) && defined(QSE_SYSCALL0)
|
|
|
|
QSE_SYSCALL0 (pid, SYS_gettid);
|
|
|
|
#elif defined(SYS_gettid)
|
|
|
|
pid = syscall (SYS_gettid);
|
|
|
|
#else
|
|
|
|
pid = -1;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
retv = qse_awk_rtx_makeintval (rtx, pid);
|
|
|
|
if (retv == QSE_NULL) return -1;
|
|
|
|
|
|
|
|
qse_awk_rtx_setretval (rtx, retv);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-03 17:44:23 +00:00
|
|
|
|
2012-10-31 13:51:32 +00:00
|
|
|
static int fnc_getppid (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
|
|
|
{
|
|
|
|
qse_long_t pid;
|
|
|
|
qse_awk_val_t* retv;
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
/* TOOD: implement this*/
|
|
|
|
pid = -1;
|
|
|
|
|
|
|
|
#elif defined(__OS2__)
|
|
|
|
/* TOOD: implement this*/
|
|
|
|
pid = -1;
|
|
|
|
|
|
|
|
#elif defined(__DOS__)
|
|
|
|
/* TOOD: implement this*/
|
|
|
|
pid = -1;
|
|
|
|
|
|
|
|
#else
|
|
|
|
pid = getppid ();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
retv = qse_awk_rtx_makeintval (rtx, pid);
|
|
|
|
if (retv == QSE_NULL) return -1;
|
|
|
|
|
|
|
|
qse_awk_rtx_setretval (rtx, retv);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-02 00:32:45 +00:00
|
|
|
static int fnc_getuid (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
|
|
|
{
|
|
|
|
qse_long_t uid;
|
|
|
|
qse_awk_val_t* retv;
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
/* TOOD: implement this*/
|
|
|
|
uid = -1;
|
|
|
|
|
|
|
|
#elif defined(__OS2__)
|
|
|
|
/* TOOD: implement this*/
|
|
|
|
uid = -1;
|
|
|
|
|
|
|
|
#elif defined(__DOS__)
|
|
|
|
/* TOOD: implement this*/
|
|
|
|
uid = -1;
|
|
|
|
|
|
|
|
#else
|
|
|
|
uid = getuid ();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
retv = qse_awk_rtx_makeintval (rtx, uid);
|
|
|
|
if (retv == QSE_NULL) return -1;
|
|
|
|
|
|
|
|
qse_awk_rtx_setretval (rtx, retv);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fnc_getgid (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
|
|
|
{
|
|
|
|
qse_long_t gid;
|
|
|
|
qse_awk_val_t* retv;
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
/* TOOD: implement this*/
|
|
|
|
gid = -1;
|
|
|
|
|
|
|
|
#elif defined(__OS2__)
|
|
|
|
/* TOOD: implement this*/
|
|
|
|
gid = -1;
|
|
|
|
|
|
|
|
#elif defined(__DOS__)
|
|
|
|
/* TOOD: implement this*/
|
|
|
|
gid = -1;
|
|
|
|
|
|
|
|
#else
|
|
|
|
gid = getgid ();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
retv = qse_awk_rtx_makeintval (rtx, gid);
|
|
|
|
if (retv == QSE_NULL) return -1;
|
|
|
|
|
|
|
|
qse_awk_rtx_setretval (rtx, retv);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-10-31 13:51:32 +00:00
|
|
|
|
2012-11-05 05:20:12 +00:00
|
|
|
static int fnc_geteuid (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
2012-10-31 08:31:58 +00:00
|
|
|
{
|
2012-11-05 05:20:12 +00:00
|
|
|
qse_long_t uid;
|
2012-10-31 08:31:58 +00:00
|
|
|
qse_awk_val_t* retv;
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
2012-11-05 05:20:12 +00:00
|
|
|
/* TOOD: implement this*/
|
|
|
|
uid = -1;
|
|
|
|
|
2012-10-31 08:31:58 +00:00
|
|
|
#elif defined(__OS2__)
|
2012-11-05 05:20:12 +00:00
|
|
|
/* TOOD: implement this*/
|
|
|
|
uid = -1;
|
|
|
|
|
2012-10-31 08:31:58 +00:00
|
|
|
#elif defined(__DOS__)
|
2012-11-05 05:20:12 +00:00
|
|
|
/* TOOD: implement this*/
|
|
|
|
uid = -1;
|
|
|
|
|
2012-10-22 09:36:15 +00:00
|
|
|
#else
|
2012-11-05 05:20:12 +00:00
|
|
|
uid = geteuid ();
|
2012-10-22 09:36:15 +00:00
|
|
|
#endif
|
|
|
|
|
2012-11-05 05:20:12 +00:00
|
|
|
retv = qse_awk_rtx_makeintval (rtx, uid);
|
|
|
|
if (retv == QSE_NULL) return -1;
|
|
|
|
|
|
|
|
qse_awk_rtx_setretval (rtx, retv);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fnc_getegid (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
|
|
|
{
|
|
|
|
qse_long_t gid;
|
|
|
|
qse_awk_val_t* retv;
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
/* TOOD: implement this*/
|
|
|
|
gid = -1;
|
|
|
|
|
|
|
|
#elif defined(__OS2__)
|
|
|
|
/* TOOD: implement this*/
|
|
|
|
gid = -1;
|
|
|
|
|
|
|
|
#elif defined(__DOS__)
|
|
|
|
/* TOOD: implement this*/
|
|
|
|
gid = -1;
|
|
|
|
|
|
|
|
#else
|
|
|
|
gid = getegid ();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
retv = qse_awk_rtx_makeintval (rtx, gid);
|
2012-10-23 17:08:56 +00:00
|
|
|
if (retv == QSE_NULL) return -1;
|
2012-10-22 09:36:15 +00:00
|
|
|
|
2012-10-23 17:08:56 +00:00
|
|
|
qse_awk_rtx_setretval (rtx, retv);
|
2012-10-22 09:36:15 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-23 17:08:56 +00:00
|
|
|
static int fnc_sleep (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
2012-10-21 16:19:03 +00:00
|
|
|
{
|
|
|
|
qse_long_t lv;
|
2012-11-11 16:07:34 +00:00
|
|
|
qse_flt_t fv;
|
2012-10-23 17:08:56 +00:00
|
|
|
qse_awk_val_t* retv;
|
2012-10-31 08:31:58 +00:00
|
|
|
int rx;
|
2012-10-21 16:19:03 +00:00
|
|
|
|
2012-11-11 16:07:34 +00:00
|
|
|
rx = qse_awk_rtx_valtonum (rtx, qse_awk_rtx_getarg (rtx, 0), &lv, &fv);
|
|
|
|
if (rx == 0)
|
2012-10-31 08:31:58 +00:00
|
|
|
{
|
2012-10-21 16:19:03 +00:00
|
|
|
#if defined(_WIN32)
|
2012-11-11 16:07:34 +00:00
|
|
|
Sleep ((DWORD)QSE_SEC_TO_MSEC(lv));
|
2012-10-31 08:31:58 +00:00
|
|
|
rx = 0;
|
2012-10-21 16:19:03 +00:00
|
|
|
#elif defined(__OS2__)
|
2012-11-11 16:07:34 +00:00
|
|
|
DosSleep ((ULONG)(QSE_SEC_TO_MSEC(lv));
|
2012-10-31 08:31:58 +00:00
|
|
|
rx = 0;
|
2012-10-22 09:36:15 +00:00
|
|
|
#elif defined(__DOS__)
|
2012-10-31 08:31:58 +00:00
|
|
|
rx = sleep (lv);
|
2012-11-11 16:07:34 +00:00
|
|
|
#elif defined(HAVE_NANOSLEEP)
|
|
|
|
struct timespec req;
|
|
|
|
req.tv_sec = lv;
|
|
|
|
req.tv_nsec = 0;
|
|
|
|
rx = nanosleep (&req, QSE_NULL);
|
2012-10-21 16:19:03 +00:00
|
|
|
#else
|
2012-10-31 08:31:58 +00:00
|
|
|
rx = sleep (lv);
|
2012-10-21 16:19:03 +00:00
|
|
|
#endif
|
2012-10-31 08:31:58 +00:00
|
|
|
}
|
2012-11-11 16:07:34 +00:00
|
|
|
else if (rx >= 1)
|
|
|
|
{
|
|
|
|
#if defined(_WIN32)
|
|
|
|
Sleep ((DWORD)QSE_SEC_TO_MSEC(fv));
|
|
|
|
rx = 0;
|
|
|
|
#elif defined(__OS2__)
|
|
|
|
DosSleep ((ULONG)QSE_SEC_TO_MSEC(fv));
|
|
|
|
rx = 0;
|
|
|
|
#elif defined(__DOS__)
|
|
|
|
/* no high-resolution sleep() is available */
|
|
|
|
rx = sleep ((qse_long_t)fv);
|
|
|
|
#elif defined(HAVE_NANOSLEEP)
|
|
|
|
struct timespec req;
|
|
|
|
req.tv_sec = (qse_long_t)fv;
|
|
|
|
req.tv_nsec = QSE_SEC_TO_NSEC(fv - req.tv_sec);
|
|
|
|
rx = nanosleep (&req, QSE_NULL);
|
|
|
|
#elif defined(HAVE_SELECT)
|
|
|
|
struct timeval req;
|
|
|
|
req.tv_sec = (qse_long_t)fv;
|
|
|
|
req.tv_nsec = QSE_SEC_TO_USEC(fv - req.tv_sec);
|
|
|
|
rx = select (0, QSE_NULL, QSE_NULL, QSE_NULL, &req);
|
|
|
|
#else
|
|
|
|
/* no high-resolution sleep() is available */
|
|
|
|
rx = sleep ((qse_long_t)fv);
|
|
|
|
#endif
|
|
|
|
}
|
2012-10-21 16:19:03 +00:00
|
|
|
|
2012-10-31 08:31:58 +00:00
|
|
|
retv = qse_awk_rtx_makeintval (rtx, rx);
|
2012-10-23 17:08:56 +00:00
|
|
|
if (retv == QSE_NULL) return -1;
|
2012-10-21 16:19:03 +00:00
|
|
|
|
2012-10-23 17:08:56 +00:00
|
|
|
qse_awk_rtx_setretval (rtx, retv);
|
2012-10-21 16:19:03 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-05 05:20:12 +00:00
|
|
|
static int fnc_gettime (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
|
|
|
{
|
|
|
|
qse_awk_val_t* retv;
|
|
|
|
qse_ntime_t now;
|
|
|
|
|
2012-11-11 16:07:34 +00:00
|
|
|
if (qse_gettime (&now) <= -1) now.sec = 0;
|
2012-11-05 05:20:12 +00:00
|
|
|
|
2012-11-11 16:07:34 +00:00
|
|
|
retv = qse_awk_rtx_makeintval (rtx, now.sec);
|
2012-11-05 05:20:12 +00:00
|
|
|
if (retv == QSE_NULL) return -1;
|
|
|
|
|
|
|
|
qse_awk_rtx_setretval (rtx, retv);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fnc_settime (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
|
|
|
{
|
|
|
|
qse_awk_val_t* retv;
|
2012-11-11 16:07:34 +00:00
|
|
|
qse_ntime_t now;
|
2012-11-05 05:20:12 +00:00
|
|
|
int rx;
|
|
|
|
|
2012-11-11 16:07:34 +00:00
|
|
|
now.nsec = 0;
|
|
|
|
|
|
|
|
if (qse_awk_rtx_valtolong (rtx, qse_awk_rtx_getarg (rtx, 0), &now.sec) <= -1 ||
|
|
|
|
qse_settime (&now) <= -1) rx = -1;
|
2012-11-05 05:20:12 +00:00
|
|
|
else rx = 0;
|
|
|
|
|
|
|
|
retv = qse_awk_rtx_makeintval (rtx, rx);
|
|
|
|
if (retv == QSE_NULL) return -1;
|
|
|
|
|
|
|
|
qse_awk_rtx_setretval (rtx, retv);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fnc_getenv (qse_awk_rtx_t* rtx, const qse_awk_fnc_info_t* fi)
|
|
|
|
{
|
|
|
|
qse_mchar_t* var;
|
|
|
|
qse_size_t len;
|
|
|
|
qse_awk_val_t* retv;
|
|
|
|
|
|
|
|
var = qse_awk_rtx_valtombsdup (
|
|
|
|
rtx, qse_awk_rtx_getarg (rtx, 0), &len);
|
|
|
|
if (var)
|
|
|
|
{
|
|
|
|
qse_mchar_t* val;
|
|
|
|
|
|
|
|
val = getenv (var);
|
|
|
|
if (val)
|
|
|
|
{
|
|
|
|
retv = qse_awk_rtx_makestrvalwithmbs (rtx, val);
|
|
|
|
if (retv == QSE_NULL) return -1;
|
|
|
|
|
|
|
|
qse_awk_rtx_setretval (rtx, retv);
|
|
|
|
}
|
|
|
|
|
|
|
|
qse_awk_rtx_freemem (rtx, var);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-23 17:08:56 +00:00
|
|
|
typedef struct fnctab_t fnctab_t;
|
|
|
|
struct fnctab_t
|
|
|
|
{
|
|
|
|
const qse_char_t* name;
|
|
|
|
qse_awk_mod_sym_fnc_t info;
|
|
|
|
};
|
|
|
|
|
2012-10-26 15:44:23 +00:00
|
|
|
typedef struct inttab_t inttab_t;
|
|
|
|
struct inttab_t
|
|
|
|
{
|
|
|
|
const qse_char_t* name;
|
|
|
|
qse_awk_mod_sym_int_t info;
|
|
|
|
};
|
|
|
|
|
2012-10-23 17:08:56 +00:00
|
|
|
static fnctab_t fnctab[] =
|
|
|
|
{
|
2012-11-05 09:49:54 +00:00
|
|
|
/* keep this table sorted for binary search in query(). */
|
|
|
|
|
2012-11-03 17:44:23 +00:00
|
|
|
{ QSE_T("fork"), { { 0, 0, QSE_NULL }, fnc_fork, 0 } },
|
2012-11-05 05:20:12 +00:00
|
|
|
{ QSE_T("getegid"), { { 0, 0, QSE_NULL }, fnc_getegid, 0 } },
|
|
|
|
{ QSE_T("getenv"), { { 1, 1, QSE_NULL }, fnc_getenv, 0 } },
|
|
|
|
{ QSE_T("geteuid"), { { 0, 0, QSE_NULL }, fnc_geteuid, 0 } },
|
2012-11-03 17:44:23 +00:00
|
|
|
{ QSE_T("getgid"), { { 0, 0, QSE_NULL }, fnc_getgid, 0 } },
|
|
|
|
{ QSE_T("getpgrp"), { { 0, 0, QSE_NULL }, fnc_getpgrp, 0 } },
|
2012-11-03 17:49:34 +00:00
|
|
|
{ QSE_T("getpid"), { { 0, 0, QSE_NULL }, fnc_getpid, 0 } },
|
2012-11-03 17:44:23 +00:00
|
|
|
{ QSE_T("getppid"), { { 0, 0, QSE_NULL }, fnc_getppid, 0 } },
|
2012-11-05 05:20:12 +00:00
|
|
|
{ QSE_T("gettid"), { { 0, 0, QSE_NULL }, fnc_gettid, 0 } },
|
|
|
|
{ QSE_T("gettime"), { { 0, 0, QSE_NULL }, fnc_gettime, 0 } },
|
2012-11-03 17:44:23 +00:00
|
|
|
{ QSE_T("getuid"), { { 0, 0, QSE_NULL }, fnc_getuid, 0 } },
|
|
|
|
{ QSE_T("kill"), { { 2, 2, QSE_NULL }, fnc_kill, 0 } },
|
2012-11-05 05:20:12 +00:00
|
|
|
{ QSE_T("settime"), { { 1, 1, QSE_NULL }, fnc_settime, 0 } },
|
2012-11-03 17:44:23 +00:00
|
|
|
{ QSE_T("sleep"), { { 1, 1, QSE_NULL }, fnc_sleep, 0 } },
|
|
|
|
{ QSE_T("wait"), { { 1, 1, QSE_NULL }, fnc_wait, 0 } }
|
2012-10-26 15:44:23 +00:00
|
|
|
};
|
|
|
|
|
2012-10-31 08:31:58 +00:00
|
|
|
#if !defined(SIGHUP)
|
|
|
|
# define SIGHUP 1
|
|
|
|
#endif
|
|
|
|
#if !defined(SIGINT)
|
|
|
|
# define SIGINT 2
|
|
|
|
#endif
|
|
|
|
#if !defined(SIGQUIT)
|
|
|
|
# define SIGQUIT 3
|
|
|
|
#endif
|
|
|
|
#if !defined(SIGABRT)
|
|
|
|
# define SIGABRT 6
|
|
|
|
#endif
|
|
|
|
#if !defined(SIGKILL)
|
|
|
|
# define SIGKILL 9
|
|
|
|
#endif
|
|
|
|
#if !defined(SIGSEGV)
|
|
|
|
# define SIGSEGV 11
|
|
|
|
#endif
|
|
|
|
#if !defined(SIGALRM)
|
|
|
|
# define SIGALRM 14
|
|
|
|
#endif
|
|
|
|
#if !defined(SIGTERM)
|
2012-11-01 15:03:02 +00:00
|
|
|
# define SIGTERM 15
|
2012-10-31 08:31:58 +00:00
|
|
|
#endif
|
|
|
|
|
2012-10-26 15:44:23 +00:00
|
|
|
static inttab_t inttab[] =
|
|
|
|
{
|
2012-11-05 09:49:54 +00:00
|
|
|
/* keep this table sorted for binary search in query(). */
|
|
|
|
|
2012-10-31 08:31:58 +00:00
|
|
|
{ QSE_T("SIGABRT"), { SIGABRT } },
|
|
|
|
{ QSE_T("SIGALRM"), { SIGALRM } },
|
|
|
|
{ QSE_T("SIGHUP"), { SIGHUP } },
|
|
|
|
{ QSE_T("SIGINT"), { SIGINT } },
|
|
|
|
{ QSE_T("SIGKILL"), { SIGKILL } },
|
2012-11-05 09:49:54 +00:00
|
|
|
{ QSE_T("SIGQUIT"), { SIGQUIT } },
|
2012-10-31 08:31:58 +00:00
|
|
|
{ QSE_T("SIGSEGV"), { SIGSEGV } },
|
2012-11-05 09:49:54 +00:00
|
|
|
{ QSE_T("SIGTERM"), { SIGTERM } }
|
2012-10-31 08:31:58 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
{ QSE_T("WNOHANG"), { WNOHANG } },
|
|
|
|
*/
|
2012-10-23 17:08:56 +00:00
|
|
|
};
|
|
|
|
|
2012-10-22 09:36:15 +00:00
|
|
|
static int query (qse_awk_mod_t* mod, qse_awk_t* awk, const qse_char_t* name, qse_awk_mod_sym_t* sym)
|
2012-10-21 16:19:03 +00:00
|
|
|
{
|
|
|
|
qse_cstr_t ea;
|
2012-11-05 09:49:54 +00:00
|
|
|
int left, right, mid, n;
|
2012-10-21 16:19:03 +00:00
|
|
|
|
2012-11-05 09:49:54 +00:00
|
|
|
left = 0; right = QSE_COUNTOF(fnctab) - 1;
|
|
|
|
|
|
|
|
while (left <= right)
|
2012-10-22 09:36:15 +00:00
|
|
|
{
|
2012-11-05 09:49:54 +00:00
|
|
|
mid = (left + right) / 2;
|
|
|
|
|
|
|
|
n = qse_strcmp (fnctab[mid].name, name);
|
|
|
|
if (n > 0) right = mid - 1;
|
|
|
|
else if (n < 0) left = mid + 1;
|
|
|
|
else
|
2012-10-23 17:08:56 +00:00
|
|
|
{
|
|
|
|
sym->type = QSE_AWK_MOD_FNC;
|
2012-11-05 09:49:54 +00:00
|
|
|
sym->u.fnc = fnctab[mid].info;
|
2012-10-23 17:08:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-11-05 09:49:54 +00:00
|
|
|
}
|
2012-10-23 17:08:56 +00:00
|
|
|
|
2012-11-05 09:49:54 +00:00
|
|
|
left = 0; right = QSE_COUNTOF(inttab) - 1;
|
|
|
|
while (left <= right)
|
2012-10-22 09:36:15 +00:00
|
|
|
{
|
2012-11-05 09:49:54 +00:00
|
|
|
mid = (left + right) / 2;
|
|
|
|
|
|
|
|
n = qse_strcmp (inttab[mid].name, name);
|
|
|
|
if (n > 0) right = mid - 1;
|
|
|
|
else if (n < 0) left = mid + 1;
|
|
|
|
else
|
2012-10-26 15:44:23 +00:00
|
|
|
{
|
|
|
|
sym->type = QSE_AWK_MOD_INT;
|
2012-11-05 09:49:54 +00:00
|
|
|
sym->u.in = inttab[mid].info;
|
2012-10-26 15:44:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-11-05 09:49:54 +00:00
|
|
|
}
|
2012-10-21 16:19:03 +00:00
|
|
|
|
|
|
|
ea.ptr = name;
|
|
|
|
ea.len = qse_strlen(name);
|
|
|
|
qse_awk_seterror (awk, QSE_AWK_ENOENT, &ea, QSE_NULL);
|
|
|
|
return -1;
|
|
|
|
}
|
2012-10-22 09:36:15 +00:00
|
|
|
|
|
|
|
/* TODO: proper resource management */
|
|
|
|
|
2012-11-01 06:42:38 +00:00
|
|
|
static int init (qse_awk_mod_t* mod, qse_awk_rtx_t* rtx)
|
2012-10-22 09:36:15 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-01 06:42:38 +00:00
|
|
|
static void fini (qse_awk_mod_t* mod, qse_awk_rtx_t* rtx)
|
2012-10-22 09:36:15 +00:00
|
|
|
{
|
|
|
|
/* TODO:
|
|
|
|
for (each pid for rtx) kill (pid, SIGKILL);
|
|
|
|
for (each pid for rtx) waitpid (pid, QSE_NULL, 0);
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
static void unload (qse_awk_mod_t* mod, qse_awk_t* awk)
|
|
|
|
{
|
|
|
|
/* TODO: anything */
|
|
|
|
}
|
|
|
|
|
2012-11-01 15:03:02 +00:00
|
|
|
QSE_EXPORT int load (qse_awk_mod_t* mod, qse_awk_t* awk)
|
2012-10-22 09:36:15 +00:00
|
|
|
{
|
|
|
|
mod->query = query;
|
|
|
|
mod->unload = unload;
|
|
|
|
|
|
|
|
mod->init = init;
|
|
|
|
mod->fini = fini;
|
|
|
|
/*
|
|
|
|
mod->ctx...
|
|
|
|
*/
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-05 05:20:12 +00:00
|
|
|
#if defined(__DOS__)
|
|
|
|
/* kind of DllMain() for Causeway DLL */
|
|
|
|
int main (int eax) { return 0; }
|
|
|
|
#endif
|