From 4fb243af7b9e62d46782da68c1c5ca9740769b71 Mon Sep 17 00:00:00 2001 From: Jeremy Baxter Date: Sat, 1 Jul 2023 09:52:01 +1200 Subject: [PATCH] Add lextra.c and extra library, with sleep() now part of it instead of in os --- Makefile | 46 ++++++++++++++++-------------------- lcallisto.c | 2 +- lcallisto.h | 2 ++ lextra.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++ los.c | 37 ----------------------------- 5 files changed, 91 insertions(+), 64 deletions(-) create mode 100644 lextra.c diff --git a/Makefile b/Makefile index 129ea72..88e1bc2 100644 --- a/Makefile +++ b/Makefile @@ -5,40 +5,34 @@ CFLAGS = -std=c99 -pedantic -fpic -O2 -Wall -Wextra -Wno-override-init -I. -Il CPPFLAGS = -D_DEFAULT_SOURCE -DLUA_USE_READLINE LDFLAGS = -lm -lreadline -OBJS = csto.o lcallisto.o lcl.o lenvironment.o lfile.o \ - ljson.o lmath.o los.o lprocess.o lsocket.o util.o +OBJS = csto.o lcallisto.o lcl.o lenvironment.o lextra.o \ + lfile.o ljson.o lmath.o los.o lprocess.o lsocket.o util.o LIBS = liblua.a cjson.a socket.a all: csto libcallisto.so -csto: ${OBJS} ${LIBS} +csto: ${LIBS} ${OBJS} ${CC} ${CFLAGS} -o $@ ${OBJS} ${LIBS} ${LDFLAGS} - -libcallisto.so: ${OBJS} ${LIBS} +libcallisto.so: ${LIBS} ${OBJS} ${CC} -shared ${CFLAGS} ${LDFLAGS} -o $@ ${OBJS} ${LIBS} -csto.o: csto.c lcallisto.h - ${CC} ${CFLAGS} ${CPPFLAGS} -c csto.c -lcallisto.o: lcallisto.c lcallisto.h - ${CC} ${CFLAGS} ${CPPFLAGS} -c lcallisto.c -lcl.o: lcl.c lcallisto.h - ${CC} ${CFLAGS} ${CPPFLAGS} -c lcl.c -lenvironment.o: lenvironment.c lcallisto.h - ${CC} ${CFLAGS} ${CPPFLAGS} -c lenvironment.c -lfile.o: lfile.c lcallisto.h - ${CC} ${CFLAGS} ${CPPFLAGS} -c lfile.c -ljson.o: ljson.c lcallisto.h - ${CC} ${CFLAGS} ${CPPFLAGS} -c ljson.c -lmath.o: lmath.c lcallisto.h - ${CC} ${CFLAGS} ${CPPFLAGS} -c lmath.c -los.o: los.c lcallisto.h - ${CC} ${CFLAGS} ${CPPFLAGS} -c los.c -lprocess.o: lprocess.c lcallisto.h - ${CC} ${CFLAGS} ${CPPFLAGS} -c lprocess.c -lsocket.o: lsocket.c lcallisto.h - ${CC} ${CFLAGS} ${CPPFLAGS} -c lsocket.c +.SUFFIXES: .o + +.c.o: + ${CC} ${CFLAGS} ${CPPFLAGS} -c $< + +csto.o: csto.c lcallisto.h +lcallisto.o: lcallisto.c lcallisto.h +lcl.o: lcl.c lcallisto.h +lextra.o: lextra.c lcallisto.h +lenvironment.o: lenvironment.c lcallisto.h +lfile.o: lfile.c lcallisto.h +ljson.o: ljson.c lcallisto.h +lmath.o: lmath.c lcallisto.h +los.o: los.c lcallisto.h +lprocess.o: lprocess.c lcallisto.h +lsocket.o: lsocket.c lcallisto.h util.o: util.c - ${CC} ${CFLAGS} ${CPPFLAGS} -c util.c -o $@ cjson.a: external/json/*.c ${MAKE} -Cexternal/json diff --git a/lcallisto.c b/lcallisto.c index 779243a..6543d21 100644 --- a/lcallisto.c +++ b/lcallisto.c @@ -12,6 +12,7 @@ static const luaL_Reg loadedlibs[] = { {CALLISTO_CLLIBNAME, callistoopen_cl}, {CALLISTO_ENVLIBNAME, callistoopen_environment}, + {CALLISTO_EXTLIBNAME, callistoopen_extra}, {CALLISTO_FILELIBNAME, callistoopen_file}, {CALLISTO_JSONLIBNAME, callistoopen_json}, {CALLISTO_MATHLIBNAME, callistoopen_math}, @@ -42,7 +43,6 @@ callisto_openlibs(lua_State *L) lua_newtable(L); lib->func(L); /* load library */ lua_setglobal(L, lib->name); - //lua_pop(L, 1); /* remove lib */ } } diff --git a/lcallisto.h b/lcallisto.h index 10b49fb..29c5180 100644 --- a/lcallisto.h +++ b/lcallisto.h @@ -17,6 +17,7 @@ #define CALLISTO_CLLIBNAME "cl" #define CALLISTO_ENVLIBNAME "environment" +#define CALLISTO_EXTLIBNAME "_G" /* global table */ #define CALLISTO_FILELIBNAME "file" #define CALLISTO_JSONLIBNAME "json" #define CALLISTO_MATHLIBNAME "math" @@ -26,6 +27,7 @@ int callistoopen_cl(lua_State *); int callistoopen_environment(lua_State *); +int callistoopen_extra(lua_State *); int callistoopen_file(lua_State *); int callistoopen_json(lua_State *); int callistoopen_math(lua_State *); diff --git a/lextra.c b/lextra.c new file mode 100644 index 0000000..df10fc6 --- /dev/null +++ b/lextra.c @@ -0,0 +1,68 @@ +/*** + * Extra functions that don't + * fit into any other category. + * + * This module does not provide its + * own table; all functions here are + * found in the global table. + * + * @module extra + */ + +#include + +#include +#include + +#include "lcallisto.h" +#include "util.h" + + +/*** + * Waits the specified amount of seconds. + * + * @function sleep + * @usage +local minutes = 5 +sleep(minutes * 60) -- 5 minutes + * @tparam number seconds The amount of seconds to wait. + */ +static int +extra_sleep(lua_State *L) +{ + /* Implementation from luasocket */ + double n; + struct timespec t, r; + + n = luaL_checknumber(L, 1); + + if (n < 0.0) + n = 0.0; + if (n > INT_MAX) + n = INT_MAX; + + t.tv_sec = (int)n; + n -= t.tv_sec; + t.tv_nsec = (int)(n * 1000000000); + + if (t.tv_nsec >= 1000000000) + t.tv_nsec = 999999999; + + while (nanosleep(&t, &r) != 0) { + t.tv_sec = r.tv_sec; + t.tv_nsec = r.tv_nsec; + } + return 0; +} + +static const luaL_Reg extlib[] = { + {"sleep", extra_sleep}, + {NULL, NULL} +}; + +int +callistoopen_extra(lua_State *L) +{ + newoverride(L, extlib, CALLISTO_EXTLIBNAME); + return 1; +} diff --git a/los.c b/los.c index f2780ff..e46401e 100644 --- a/los.c +++ b/los.c @@ -40,46 +40,9 @@ os_hostname(lua_State *L) return 1; } -/*** - * Waits the specified amount of seconds. - * - * @function sleep - * @usage -local minutes = 5 -os.sleep(minutes * 60) -- 5 minutes - * @tparam number seconds The amount of seconds to wait. - */ -static int -os_sleep(lua_State *L) -{ - /* Implementation from luasocket */ - double n; - struct timespec t, r; - - n = luaL_checknumber(L, 1); - - if (n < 0.0) - n = 0.0; - if (n > INT_MAX) - n = INT_MAX; - - t.tv_sec = (int)n; - n -= t.tv_sec; - t.tv_nsec = (int)(n * 1000000000); - - if (t.tv_nsec >= 1000000000) - t.tv_nsec = 999999999; - - while (nanosleep(&t, &r) != 0) { - t.tv_sec = r.tv_sec; - t.tv_nsec = r.tv_nsec; - } - return 0; -} static const luaL_Reg oslib[] = { {"hostname", os_hostname}, - {"sleep", os_sleep}, {NULL, NULL} };