This repository has been archived by the owner on Nov 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
lpc.c
231 lines (199 loc) · 4.26 KB
/
lpc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
** LuaProcessCall
** Copyright DarkGod 2007
**
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#define topfile(L) ((FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE))
static FILE *tofile (lua_State *L) {
FILE **f = topfile(L);
if (*f == NULL)
luaL_error(L, "attempt to use a closed file");
return *f;
}
static int pushresult (lua_State *L, int i, const char *filename) {
int en = errno; /* calls to Lua API may change this value */
if (i) {
lua_pushboolean(L, 1);
return 1;
}
else {
lua_pushnil(L);
if (filename)
lua_pushfstring(L, "%s: %s", filename, strerror(en));
else
lua_pushfstring(L, "%s", strerror(en));
lua_pushinteger(L, en);
return 3;
}
}
static int io_fclose (lua_State *L) {
FILE **p = topfile(L);
int ok = (fclose(*p) == 0);
*p = NULL;
return pushresult(L, ok, NULL);
}
static FILE **newfile_fd(lua_State *L, int fd, const char *mode)
{
FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *));
*pf = NULL; /* file handle is currently `closed' */
luaL_getmetatable(L, LUA_FILEHANDLE);
lua_setmetatable(L, -2);
*pf = fdopen(fd, mode);
return pf;
}
static int lpc_run(lua_State *L)
{
int p_out[2];
int p_in[2];
int pid;
if (pipe(p_out) == -1) { lua_pushnil(L); return 1; }
if (pipe(p_in) == -1) { lua_pushnil(L); return 1; }
if ((pid = fork()) == -1) { lua_pushnil(L); return 1; }
else if (pid == 0)
{
char **args;
int n = lua_gettop(L); /* number of arguments */
int i;
args = malloc((n + 1) * sizeof(char*));
for (i = 1; i <= n; i++)
{
args[i - 1] = (char*)luaL_checkstring(L, i);
}
args[n] = NULL;
close(p_out[1]);
close(p_in[0]);
dup2(p_out[0], 0);
dup2(p_in[1], 1);
close(p_out[0]);
close(p_in[1]);
execvp(args[0], args);
perror("LPC child error");
_exit(1);
return 0;
}
else
{
FILE **in;
FILE **out;
/* Cleanup */
close(p_out[0]);
close(p_in[1]);
lua_pushnumber(L, pid);
out = newfile_fd(L, p_out[1], "w");
in = newfile_fd(L, p_in[0], "r");
return 3;
}
}
static int lpc_wait(lua_State *L)
{
int ret;
int pid = luaL_checkinteger(L, 1);
int nonblock = luaL_optinteger(L, 2, 0);
if (waitpid(pid, &ret, (nonblock == 1) ? WNOHANG : 0) == pid)
{
lua_pushnumber(L, ret);
}
else
{
lua_pushnil(L);
}
return 1;
}
static int f_readable(lua_State *L)
{
FILE *f = tofile(L);
struct timeval tv;
fd_set rfds;
int retval;
/* Wait up to five seconds. */
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&rfds);
FD_SET(fileno(f), &rfds);
retval = select(1 + fileno(f), &rfds, NULL, NULL, &tv);
if (retval == 1)
{
lua_pushboolean(L, 1);
}
else
{
lua_pushnil(L);
}
return 1;
}
static int f_writable(lua_State *L)
{
FILE *f = tofile(L);
struct timeval tv;
fd_set wfds;
int retval;
/* Wait up to five seconds. */
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO(&wfds);
FD_SET(fileno(f), &wfds);
retval = select(1 + fileno(f), NULL, &wfds, NULL, &tv);
if (retval == 1)
{
lua_pushboolean(L, 1);
}
else
{
lua_pushboolean(L, 0);
}
return 1;
}
/*
** Assumes the table is on top of the stack.
*/
static void set_info (lua_State *L)
{
lua_pushliteral (L, "_COPYRIGHT");
lua_pushliteral (L, "Copyright (C) 2007 DarkGod");
lua_settable (L, -3);
lua_pushliteral (L, "_DESCRIPTION");
lua_pushliteral (L, "LPC allows to run external processes and catch both input and output streams");
lua_settable (L, -3);
lua_pushliteral (L, "_VERSION");
lua_pushliteral (L, "LPC 1.0.0");
lua_settable (L, -3);
}
static const struct luaL_reg lpclib[] =
{
{"run", lpc_run},
{"wait", lpc_wait},
{NULL, NULL},
};
static const luaL_Reg io_add_flib[] = {
{"readable", f_readable},
{"writable", f_writable},
{NULL, NULL}
};
static void createmeta(lua_State *L)
{
luaL_getmetatable(L, LUA_FILEHANDLE); /* get IO's metatable for file handles */
luaL_register(L, NULL, io_add_flib); /* file methods */
}
int luaopen_lpc (lua_State *L)
{
createmeta(L);
luaL_openlib(L, "lpc", lpclib, 0);
/* create environment for 'run' */
lua_getfield(L, -1, "run");
lua_createtable(L, 0, 1);
lua_pushcfunction(L, io_fclose);
lua_setfield(L, -2, "__close");
lua_setfenv(L, -2);
lua_pop(L, 1); /* pop 'run' */
set_info(L);
return 1;
}