Add lextra.c and extra library, with sleep() now part of it instead of in os
This commit is contained in:
parent
2d78ec849e
commit
4fb243af7b
5 changed files with 91 additions and 64 deletions
46
Makefile
46
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
|
CPPFLAGS = -D_DEFAULT_SOURCE -DLUA_USE_READLINE
|
||||||
LDFLAGS = -lm -lreadline
|
LDFLAGS = -lm -lreadline
|
||||||
|
|
||||||
OBJS = csto.o lcallisto.o lcl.o lenvironment.o lfile.o \
|
OBJS = csto.o lcallisto.o lcl.o lenvironment.o lextra.o \
|
||||||
ljson.o lmath.o los.o lprocess.o lsocket.o util.o
|
lfile.o ljson.o lmath.o los.o lprocess.o lsocket.o util.o
|
||||||
LIBS = liblua.a cjson.a socket.a
|
LIBS = liblua.a cjson.a socket.a
|
||||||
|
|
||||||
all: csto libcallisto.so
|
all: csto libcallisto.so
|
||||||
|
|
||||||
csto: ${OBJS} ${LIBS}
|
csto: ${LIBS} ${OBJS}
|
||||||
${CC} ${CFLAGS} -o $@ ${OBJS} ${LIBS} ${LDFLAGS}
|
${CC} ${CFLAGS} -o $@ ${OBJS} ${LIBS} ${LDFLAGS}
|
||||||
|
libcallisto.so: ${LIBS} ${OBJS}
|
||||||
libcallisto.so: ${OBJS} ${LIBS}
|
|
||||||
${CC} -shared ${CFLAGS} ${LDFLAGS} -o $@ ${OBJS} ${LIBS}
|
${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
|
util.o: util.c
|
||||||
${CC} ${CFLAGS} ${CPPFLAGS} -c util.c -o $@
|
|
||||||
|
|
||||||
cjson.a: external/json/*.c
|
cjson.a: external/json/*.c
|
||||||
${MAKE} -Cexternal/json
|
${MAKE} -Cexternal/json
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
static const luaL_Reg loadedlibs[] = {
|
static const luaL_Reg loadedlibs[] = {
|
||||||
{CALLISTO_CLLIBNAME, callistoopen_cl},
|
{CALLISTO_CLLIBNAME, callistoopen_cl},
|
||||||
{CALLISTO_ENVLIBNAME, callistoopen_environment},
|
{CALLISTO_ENVLIBNAME, callistoopen_environment},
|
||||||
|
{CALLISTO_EXTLIBNAME, callistoopen_extra},
|
||||||
{CALLISTO_FILELIBNAME, callistoopen_file},
|
{CALLISTO_FILELIBNAME, callistoopen_file},
|
||||||
{CALLISTO_JSONLIBNAME, callistoopen_json},
|
{CALLISTO_JSONLIBNAME, callistoopen_json},
|
||||||
{CALLISTO_MATHLIBNAME, callistoopen_math},
|
{CALLISTO_MATHLIBNAME, callistoopen_math},
|
||||||
|
@ -42,7 +43,6 @@ callisto_openlibs(lua_State *L)
|
||||||
lua_newtable(L);
|
lua_newtable(L);
|
||||||
lib->func(L); /* load library */
|
lib->func(L); /* load library */
|
||||||
lua_setglobal(L, lib->name);
|
lua_setglobal(L, lib->name);
|
||||||
//lua_pop(L, 1); /* remove lib */
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
|
|
||||||
#define CALLISTO_CLLIBNAME "cl"
|
#define CALLISTO_CLLIBNAME "cl"
|
||||||
#define CALLISTO_ENVLIBNAME "environment"
|
#define CALLISTO_ENVLIBNAME "environment"
|
||||||
|
#define CALLISTO_EXTLIBNAME "_G" /* global table */
|
||||||
#define CALLISTO_FILELIBNAME "file"
|
#define CALLISTO_FILELIBNAME "file"
|
||||||
#define CALLISTO_JSONLIBNAME "json"
|
#define CALLISTO_JSONLIBNAME "json"
|
||||||
#define CALLISTO_MATHLIBNAME "math"
|
#define CALLISTO_MATHLIBNAME "math"
|
||||||
|
@ -26,6 +27,7 @@
|
||||||
|
|
||||||
int callistoopen_cl(lua_State *);
|
int callistoopen_cl(lua_State *);
|
||||||
int callistoopen_environment(lua_State *);
|
int callistoopen_environment(lua_State *);
|
||||||
|
int callistoopen_extra(lua_State *);
|
||||||
int callistoopen_file(lua_State *);
|
int callistoopen_file(lua_State *);
|
||||||
int callistoopen_json(lua_State *);
|
int callistoopen_json(lua_State *);
|
||||||
int callistoopen_math(lua_State *);
|
int callistoopen_math(lua_State *);
|
||||||
|
|
68
lextra.c
Normal file
68
lextra.c
Normal file
|
@ -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 <time.h>
|
||||||
|
|
||||||
|
#include <lua.h>
|
||||||
|
#include <lauxlib.h>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
37
los.c
37
los.c
|
@ -40,46 +40,9 @@ os_hostname(lua_State *L)
|
||||||
|
|
||||||
return 1;
|
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[] = {
|
static const luaL_Reg oslib[] = {
|
||||||
{"hostname", os_hostname},
|
{"hostname", os_hostname},
|
||||||
{"sleep", os_sleep},
|
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue