monotone

monotone Mtn Source Tree

Root/luaext_platform.cc

1
2#include "lua.hh"
3
4#include <signal.h>
5#include <cstdlib>
6
7#include "platform.hh"
8
9using std::malloc;
10using std::free;
11
12LUAEXT(get_ostype, )
13{
14 std::string str;
15 get_system_flavour(str);
16 lua_pushstring(L, str.c_str());
17 return 1;
18}
19
20LUAEXT(existsonpath, )
21{
22 const char *exe = luaL_checkstring(L, -1);
23 lua_pushnumber(L, existsonpath(exe));
24 return 1;
25}
26
27LUAEXT(is_executable, )
28{
29 const char *path = luaL_checkstring(L, -1);
30 lua_pushboolean(L, is_executable(path));
31 return 1;
32}
33
34LUAEXT(make_executable, )
35{
36 const char *path = luaL_checkstring(L, -1);
37 lua_pushnumber(L, make_executable(path));
38 return 1;
39}
40
41LUAEXT(spawn, )
42{
43 int n = lua_gettop(L);
44 const char *path = luaL_checkstring(L, 1);
45 char **argv = (char**)malloc((n+1)*sizeof(char*));
46 int i;
47 pid_t ret;
48 if (argv==NULL)
49 return 0;
50 argv[0] = (char*)path;
51 for (i=1; i<n; i++) argv[i] = (char*)luaL_checkstring(L, i+1);
52 argv[i] = NULL;
53 ret = process_spawn(argv);
54 free(argv);
55 lua_pushnumber(L, ret);
56 return 1;
57}
58
59LUAEXT(spawn_redirected, )
60{
61 int n = lua_gettop(L);
62 char const * infile = luaL_checkstring(L, 1);
63 char const * outfile = luaL_checkstring(L, 2);
64 char const * errfile = luaL_checkstring(L, 3);
65 const char *path = luaL_checkstring(L, 4);
66 n -= 3;
67 char **argv = (char**)malloc((n+1)*sizeof(char*));
68 int i;
69 pid_t ret;
70 if (argv==NULL)
71 return 0;
72 argv[0] = (char*)path;
73 for (i=1; i<n; i++) argv[i] = (char*)luaL_checkstring(L, i+4);
74 argv[i] = NULL;
75 ret = process_spawn_redirected(infile, outfile, errfile, argv);
76 free(argv);
77 lua_pushnumber(L, ret);
78 return 1;
79}
80
81// borrowed from lua/liolib.cc
82// Note that making C functions that return FILE* in Lua is tricky
83// There is a Lua FAQ entitled:
84// "Why does my library-created file segfault on :close() but work otherwise?"
85
86#define topfile(L)((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE))
87
88static int io_fclose (lua_State *L) {
89 FILE **p = topfile(L);
90 int ok = (fclose(*p) == 0);
91 *p = NULL;
92 lua_pushboolean(L, ok);
93 return 1;
94}
95
96static FILE **newfile (lua_State *L) {
97 FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *));
98 *pf = NULL; /* file handle is currently `closed' */
99 luaL_getmetatable(L, LUA_FILEHANDLE);
100 lua_setmetatable(L, -2);
101
102 lua_pushcfunction(L, io_fclose);
103 lua_setfield(L, LUA_ENVIRONINDEX, "__close");
104
105 return pf;
106}
107
108LUAEXT(spawn_pipe, )
109{
110 int n = lua_gettop(L);
111 char **argv = (char**)malloc((n+1)*sizeof(char*));
112 int i;
113 pid_t pid;
114 if (argv==NULL)
115 return 0;
116 if (n<1)
117 return 0;
118 for (i=0; i<n; i++) argv[i] = (char*)luaL_checkstring(L, i+1);
119 argv[i] = NULL;
120
121 int infd;
122 FILE **inpf = newfile(L);
123 int outfd;
124 FILE **outpf = newfile(L);
125
126 pid = process_spawn_pipe(argv, inpf, outpf);
127 free(argv);
128
129 lua_pushnumber(L, pid);
130
131 return 3;
132}
133
134LUAEXT(wait, )
135{
136 pid_t pid = static_cast<pid_t>(luaL_checknumber(L, -1));
137 int res;
138 int ret;
139 ret = process_wait(pid, &res);
140 lua_pushnumber(L, res);
141 lua_pushnumber(L, ret);
142 return 2;
143}
144
145LUAEXT(kill, )
146{
147 int n = lua_gettop(L);
148 pid_t pid = static_cast<pid_t>(luaL_checknumber(L, -2));
149 int sig;
150 if (n>1)
151 sig = static_cast<int>(luaL_checknumber(L, -1));
152 else
153 sig = SIGTERM;
154 lua_pushnumber(L, process_kill(pid, sig));
155 return 1;
156}
157
158LUAEXT(sleep, )
159{
160 int seconds = static_cast<int>(luaL_checknumber(L, -1));
161 lua_pushnumber(L, process_sleep(seconds));
162 return 1;
163}
164
165
166
167// Local Variables:
168// mode: C++
169// fill-column: 76
170// c-file-style: "gnu"
171// indent-tabs-mode: nil
172// End:
173// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:
174

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status