aboutsummaryrefslogtreecommitdiff
path: root/jim-posix.c
diff options
context:
space:
mode:
authorSteve Bennett <steveb@workware.net.au>2009-07-28 15:46:40 +1000
committerSteve Bennett <steveb@workware.net.au>2010-10-15 10:11:02 +1000
commit462954132b1830a7e470436bf751fe8e86291c72 (patch)
treee4a917e94147dda69ecf9b74df2a9f2605edc99c /jim-posix.c
parent981bd5980a5f2ef5e1a527dd3bd90d9f6cfaa632 (diff)
downloadjimtcl-462954132b1830a7e470436bf751fe8e86291c72.zip
jimtcl-462954132b1830a7e470436bf751fe8e86291c72.tar.gz
jimtcl-462954132b1830a7e470436bf751fe8e86291c72.tar.bz2
Lots of improvements
jim --- Add a unique id facility to jim (Jim_GetId()) Remove unused jim_vasprintf(), Jim_AppendString_sprintf() Remove duplicate output in Jim_Panic() Add support for catch -signal aio --- Use this for aio handles aio automatically creates stdin, stdout and stderr channels Jim_AioFilehandle() returns the 'FILE *' filehandle for a channel If JIM_TCL_COMPAT is set, create top level open, close, read, puts, etc. load ---- load core command split out into jim-load.c package ------- package core command split out into jim-package.c added 'package list' posix ----- Remove os.sleep, os.usleep, os.sethostname, os.signal, pit, Jpit Most of these are in 'signal'
Diffstat (limited to 'jim-posix.c')
-rw-r--r--jim-posix.c294
1 files changed, 0 insertions, 294 deletions
diff --git a/jim-posix.c b/jim-posix.c
index f280f3e..9a81a55 100644
--- a/jim-posix.c
+++ b/jim-posix.c
@@ -50,21 +50,6 @@ static int Jim_PosixForkCommand(Jim_Interp *interp, int argc,
return JIM_OK;
}
-static int Jim_PosixSleepCommand(Jim_Interp *interp, int argc,
- Jim_Obj *const *argv)
-{
- long longValue;
-
- if (argc != 2) {
- Jim_WrongNumArgs(interp, 1, argv, "?seconds?");
- return JIM_ERR;
- }
- if (Jim_GetLong(interp, argv[1], &longValue) != JIM_OK)
- return JIM_ERR;
- sleep(longValue);
- return JIM_OK;
-}
-
static int Jim_PosixGetidsCommand(Jim_Interp *interp, int argc,
Jim_Obj *const *argv)
{
@@ -103,292 +88,13 @@ static int Jim_PosixGethostnameCommand(Jim_Interp *interp, int argc,
return JIM_OK;
}
-static int Jim_PosixSethostnameCommand(Jim_Interp *interp, int argc,
- Jim_Obj *const *argv)
-{
- const char *hostname;
- int len;
-
- if (argc != 2) {
- Jim_WrongNumArgs(interp, 1, argv, "hostname");
- return JIM_ERR;
- }
- hostname = Jim_GetString(argv[1], &len);
- if (sethostname(hostname, len) == -1) {
- Jim_PosixSetError(interp);
- return JIM_ERR;
- }
- return JIM_OK;
-}
-// added Commands, some linux specific
-
-// uses POSIX.1b
-#define S_NS 1000000000
-#define S_US 1000000
-#define MSNS 1000000
-#define USNS 1000
-#define NSNS 1
-
-static int Jim_LinuxUSleepCommand(Jim_Interp *interp, int argc,
- Jim_Obj *const *argv)
-{
- int mult = NSNS ;
- double floatValue ;
- const char *units ;
- long long delay ;
- int len ;
- struct timespec tv;
-
- switch (argc) {
- case 3:
- units = Jim_GetString(argv[2], &len);
- if (units == NULL)
- return JIM_ERR;
- switch (*units) {
- case 's': mult = S_NS; break;
- case 'm': mult = MSNS; break;
- case 'u': mult = USNS; break;
- case 'n': mult = NSNS; break;
- default:
- Jim_WrongNumArgs(interp, 1, argv, "arg3: ms us ns or empty");
- return JIM_ERR;
- }
- // fallthrough
- case 2: if (Jim_GetDouble(interp, argv[1], &floatValue) != JIM_OK)
- return JIM_ERR;
- delay = (long long)(floatValue * mult);
- break;
- default:
- Jim_WrongNumArgs(interp, 1, argv, "?useconds?");
- return JIM_ERR;
- }
- tv.tv_sec = delay / S_NS;
- tv.tv_nsec = delay % S_NS;
- fprintf(stderr,"delay: %lld mult: %d sec: %ld ns: %ld\n", delay, mult, tv.tv_sec,tv.tv_nsec );
- nanosleep(&tv,NULL);
- return JIM_OK;
-}
-
-static int Jim_PointInTimeCommand(Jim_Interp *interp, int argc,
- Jim_Obj *const *argv)
-{
- struct timezone tz = { 0, 0 };
- double pit ;
- struct timeval tv;
-
- gettimeofday(&tv,&tz);
- pit = (double)tv.tv_usec;
- pit /= (double)S_US;
- pit +=(double)tv.tv_sec;
-
- Jim_SetResult(interp, Jim_NewDoubleObj(interp, pit));
- return JIM_OK;
-}
-
-static int Jim_PointInTimeJulianCommand(Jim_Interp *interp, int argc,
- Jim_Obj *const *argv)
-{
- struct timezone tz = { 0, 0 };
- double pit ;
- struct timeval tv;
-
- gettimeofday(&tv,&tz);
- pit = (double)tv.tv_usec;
- pit /= (double)S_US;
- pit += (double)tv.tv_sec;
- pit /= (double)( 60 * 60 * 24 );
- pit += 2440587.5;
-
- Jim_SetResult(interp, Jim_NewDoubleObj(interp, pit));
- return JIM_OK;
-}
-
-#if 0
-// signal stuff
-// signal <signame>
-
- static const char *signames[] = {
- "SIGHUP", "SIGINT", "SIGQUIT", "SIGILL",
- "SIGABRT", "SIGFPE", "SIGKILL", "SIGSEGV",
- "SIGPIPE", "SIGALRM", "SIGTERM",
- "SIGUSR1", "SIGUSR2",
- "SIGCHLD", "SIGCONT", "SIGSTOP",
- "SIGTSTP", "SIGTTIN" "SIGTTOU",
- "SIGBUS",
-#ifdef SIGPOLL
- "SIGPOLL",
-#endif
- "SIGPROF", "SIGSYS",
- "SIGTRAP", "SIGURG", "SIGVTALRM", "SIGXCPU",
- "SIGXFSZ",
- "SIGIOT",
-#ifdef SIGEMT
- "SIGEMT",
-#endif
- "SIGSTKFLT", "SIGIO",
- "SIGCLD", "SIGPWR",
-#ifdef SIGINFO
- "SIGINFO",
-#endif
-#ifdef SIGLOST
- "SIGLOST",
-#endif
- "SIGWINCH", "SIGUNUSED",
- "SIGRT32", "SIGRT33", "SIGRT34", "SIGRT35",
- "SIGRT36", "SIGRT37", "SIGRT38", "SIGRT39",
- NULL
- } ;
-static int signums[] = {
- SIGHUP, SIGINT, SIGQUIT, SIGILL,
- SIGABRT, SIGFPE, SIGKILL, SIGSEGV,
- SIGPIPE, SIGALRM, SIGTERM,
- SIGUSR1, SIGUSR2,
- SIGCHLD, SIGCONT, SIGSTOP,
- SIGTSTP, SIGTTIN, SIGTTOU,
- SIGBUS,
-#ifdef SIGPOLL
- SIGPOLL,
-#endif
- SIGPROF, SIGSYS,
- SIGTRAP, SIGURG, SIGVTALRM, SIGXCPU,
- SIGXFSZ,
- SIGIOT,
-#ifdef SIGEMT
- SIGEMT,
-#endif
- SIGSTKFLT, SIGIO,
- SIGCLD, SIGPWR,
-#ifdef SIGINFO
- SIGINFO,
-#endif
-#ifdef SIGLOST
- SIGLOST,
-#endif
- SIGWINCH, SIGUNUSED,
-#ifdef SIGRT
- SIGRT32, SIGRT33, SIGRT34, SIGRT35,
- SIGRT36, SIGRT37, SIGRT38, SIGRT39,
-#endif
- 0
- } ;
-
-#if 0
-enum {
- HUP, INT, QUIT, ILL,
- ABRT, FPE, KILL, SEGV,
- PIPE, ALRM, TERM,
- USR1, USR2,
- CHLD, CONT, STOP,
- TSTP, TTIN, TTOU,
- BUS, POLL, PROF, SYS,
- TRAP, URG, VTALRM, XCPU,
- XFSZ,
- IOT,
-#ifdef SIGEMT
- EMT,
-#endif
- STKFLT, IO,
- CLD, PWR,
-#ifdef SIGINFO
- INFO,
-#endif
-#ifdef SIGLOST
- LOST,
-#endif
- WINCH, UNUSED,
-#ifdef SIGRT
- RT32, RT33, RT34, RT35,
- RT36, RT37, RT38, RT39,
-#endif
- ISEND
- } ;
-#endif
-#endif
-
-static void Jim_Posix_SigHandler(int signal)
-{
-#if 0
- int i;
- for (i=0; i<sizeof(signums)/sizeof(*signums) && (signums[i] != signal);i++) ;
-#endif
- //fprintf(stderr,"signal %d %s\n", signal,signames[i]);
- fprintf(stderr,"signal %d\n", signal);
-}
-
-typedef void (*sighandler_t)(int);
-
-static int Jim_PosixSignalCommand(Jim_Interp *interp, int argc,
- Jim_Obj *const *argv)
-{
- int sig;
- int signum;
- sighandler_t lastaction;
- sighandler_t nextaction = SIG_DFL;
- const char *op;
- int strlen = 0;
-
- if (argc < 2) {
- Jim_WrongNumArgs(interp, 1, argv, "signame ?action ...?");
- return JIM_ERR;
- }
- if (Jim_GetEnum(interp, argv[1], signames, &sig, "Signal Names",
- JIM_ERRMSG) != JIM_OK)
- return JIM_ERR;
-
- signum = signums[sig];
-
- switch (argc) {
- case 3:
- if (op = Jim_GetString(argv[2], &strlen),strlen == 0) {
- return JIM_ERR;
- }
- if (strcmp("default",op) == 0) {
- nextaction = SIG_DFL;
- } else if (strcmp("ignore",op) == 0) {
- nextaction = SIG_IGN;
- } else if (strcmp("debug",op) == 0) {
- nextaction = Jim_Posix_SigHandler;
- } else {
- // this is the place to insert a script! UK
- }
- // fall through to query case:
- case 2:
- lastaction = signal(signum, nextaction);
- if (argc==2)
- signal(signum, lastaction);
- if (lastaction == SIG_ERR) {
- return JIM_ERR;
- }
- if (lastaction == SIG_DFL) {
- Jim_SetResultString(interp, "default", -1);
- return JIM_OK;
- }
- if (lastaction == SIG_IGN) {
- Jim_SetResultString(interp, "ignore", -1);
- return JIM_OK;
- }
- Jim_SetResultString(interp, "function", -1);
- return JIM_OK;
- }
-
- return JIM_OK;
-}
-
-
-
// end added
int Jim_posixInit(Jim_Interp *interp)
{
if (Jim_PackageProvide(interp, "posix", "1.0", JIM_ERRMSG) != JIM_OK)
return JIM_ERR;
Jim_CreateCommand(interp, "os.fork", Jim_PosixForkCommand, NULL, NULL);
- Jim_CreateCommand(interp, "os.sleep", Jim_PosixSleepCommand, NULL, NULL);
Jim_CreateCommand(interp, "os.getids", Jim_PosixGetidsCommand, NULL, NULL);
Jim_CreateCommand(interp, "os.gethostname", Jim_PosixGethostnameCommand, NULL, NULL);
- Jim_CreateCommand(interp, "os.sethostname", Jim_PosixSethostnameCommand, NULL, NULL);
- Jim_CreateCommand(interp, "os.usleep", Jim_LinuxUSleepCommand, NULL, NULL);
- Jim_CreateCommand(interp, "os.signal", Jim_PosixSignalCommand, NULL, NULL);
- Jim_CreateCommand(interp, "pit", Jim_PointInTimeCommand, NULL, NULL);
- Jim_CreateCommand(interp, "Jpit", Jim_PointInTimeJulianCommand, NULL, NULL);
return JIM_OK;
}