shithub: lua9

Download patch

ref: 58417631c1338bce2a8f63ffb68a462a31794995
parent: 553c8b384de1e6afd0463eecbf812a39a8071dcf
author: telephil9 <telephil9@gmail.com>
date: Tue Oct 27 16:23:50 EDT 2020

New plan9 module to export some libc functions/constants

	This new module will export plan9 libc functions to lua.
	Exported globals are currently OREAD and OWRITE.
	A fork() function is provided. It takes a parameter-less function as a parameter
	and runs it in a subprocess.

--- /dev/null
+++ b/lib9.c
@@ -1,0 +1,42 @@
+#include <u.h>
+#include <lib9.h>
+#include <lua.h>
+#include <lauxlib.h>
+#include <lualib.h>
+#include "llib9.h"
+#include "utils.h"
+
+static int
+lfork(lua_State *L)
+{
+	char err[128];
+
+	if(lua_isfunction(L, 1) == 0)
+		return luaL_argerror(L, 1, "expected a function");
+	switch(rfork(RFFDG|RFREND|RFPROC)){
+	case -1:
+		errstr(err, sizeof err);
+		lua_pushfstring(L, "fork failed: %s", err);
+		return lua_error(L);
+	case 0:
+		break;
+	default:
+		lua_call(L, 0, 0);
+	}
+	return 0;
+}
+
+
+static const struct luaL_Reg lib9 [] = {
+	{ "fork", lfork },
+	{ NULL, NULL }
+};
+
+int
+openlib9(lua_State *L)
+{
+	luaL_newlib(L, lib9);
+	pushglobal(L, "OREAD", 0);
+	pushglobal(L, "OWRITE", 1);
+	return 1;
+}
--- /dev/null
+++ b/llib9.h
@@ -1,0 +1,6 @@
+#ifndef LLIB9_H__
+#define LLIB9_H__
+
+int openlib9(lua_State *L);
+
+#endif
--- a/lua9.c
+++ b/lua9.c
@@ -11,6 +11,7 @@
 #include <lualib.h>
 #include "ldraw.h"
 #include "lplumb.h"
+#include "llib9.h"
 #include "utils.h"
 
 static const luaL_Reg libs[] = {
@@ -20,6 +21,7 @@
 	{ "key",  openlibkey },
 	{ "color", openlibcolor },
 	{ "plumb", openlibplumb },
+	{ "plan9", openlib9 },
 	{ NULL, NULL },
 };
 
--- a/mkfile
+++ b/mkfile
@@ -17,6 +17,7 @@
 	plumb.$O \
 	plumbmsg.$O \
 	plumbattr.$O \
+	lib9.$O \
 	utils.$O \
 	lua9.$O