-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatform-posix.c
306 lines (254 loc) · 6.28 KB
/
platform-posix.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include <signal.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <termios.h>
#include <sys/ioctl.h>
// Lua
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include "common/LM.h"
void keyboard_interrupt(void);
static void sigint (int i)
{
keyboard_interrupt();
}
void enable_keyboard_interrupt_handler (void)
{
signal(SIGINT, sigint);
}
void disable_keyboard_interrupt_handler (void)
{
signal(SIGINT, SIG_DFL);
}
#include <sys/types.h>
#include <unistd.h>
static int os_pipe (lua_State *L)
{
int fds[2];
if (pipe(fds) < 0) {
const char *msg = strerror (errno);
lua_pushnil (L);
lua_pushstring (L, msg);
return 2;
}
lua_pushnumber(L, fds[0]);
lua_pushnumber(L, fds[1]);
return 2;
}
static int os_getpid (lua_State *L)
{
pid_t pid = getpid();
lua_pushinteger(L, pid);
return 1;
}
#include <sys/time.h>
static int os_time_unix (lua_State *L)
{
struct timeval tp;
if (gettimeofday(&tp, NULL) < 0)
return luaLM_posix_error (L, "gettimeofday");
double t = tp.tv_sec + tp.tv_usec / 1.0e6;
lua_pushnumber (L, t);
return 1;
}
#if defined(__APPLE__)
#include <util.h>
#elif defined(__linux__)
#include <pty.h>
#endif
static int os_forkpty (lua_State *L)
{
int mpty;
int pid = forkpty(&mpty, NULL, NULL, NULL);
if (pid < 0) luaLM_posix_error (L, "forkpty");
lua_pushnumber(L, pid);
lua_pushnumber(L, mpty);
return 2;
}
static int io_raw_read (lua_State *L)
{
int fd = luaLM_checkfd (L, 1);
int n = luaL_optint(L, 2, 100*1024);
char buffer[n];
int ret = read (fd, buffer, n);
if (!ret) { // EOF
lua_pushnil (L);
lua_pushliteral (L, "eof");
return 2;
}
if (ret < 0) { // error
const char *msg = strerror (errno);
lua_pushnil (L);
lua_pushstring (L, msg);
return 2;
}
lua_pushlstring (L, buffer, ret);
return 1;
}
static int io_raw_write (lua_State *L)
{
int fd = luaLM_checkfd(L, 1);
size_t n = 0;
const char *s = luaL_checklstring(L, 2, &n);
size_t off = luaL_optnumber(L, 3, 1) - 1;
if (off > n) off = n;
int ret = write(fd, s + off, n - off);
if (ret < 0) { // error
const char *msg = strerror(errno);
lua_pushnil(L);
lua_pushstring(L, msg);
return 2;
}
// successful (though maybe partial) write
lua_pushnumber(L, ret);
return 1;
}
static int io_raw_close (lua_State *L)
{
int fd = luaLM_checkfd (L, 1);
int ret = close(fd);
if (!ret) {
const char *msg = strerror (errno);
lua_pushnil (L);
lua_pushstring (L, msg);
return 2;
}
return 0;
}
#include <fcntl.h>
static int io_setinherit (lua_State *L)
{
int fd = luaLM_checkfd (L, 1);
int inherit = lua_toboolean(L, 2);
int flags = inherit ? 0 : FD_CLOEXEC;
int ret = fcntl(fd, F_SETFD, flags);
if (ret < 0) {
const char *msg = strerror (errno);
lua_pushnil (L);
lua_pushstring (L, msg);
return 2;
}
return 0;
}
static int io_fsync (lua_State *L)
{
int fd = luaLM_getfd (L, 1);
if(fsync(fd) < 0) {
const char *msg = strerror (errno);
lua_pushnil (L);
lua_pushstring (L, msg);
return 2;
}
lua_pushboolean(L, 1);
return 1;
}
/* Use this variable to remember original terminal attributes. */
static struct termios saved_stdin_attributes;
void reset_stdin (void)
{
tcsetattr (0, TCSANOW, &saved_stdin_attributes);
}
int io_immediate_stdin (lua_State *L)
{
int raw = lua_toboolean(L, 1);
if (!raw) {
reset_stdin();
return 0;
}
tcgetattr (0, &saved_stdin_attributes);
atexit (reset_stdin);
struct termios tattr;
tcgetattr (0, &tattr);
tattr.c_lflag &= ~(ICANON|ECHO);
tattr.c_cc[VMIN] = 1;
tattr.c_cc[VTIME] = 0;
tcsetattr (0, TCSAFLUSH, &tattr);
return 0;
}
int io_tty_noecho (lua_State *L)
{
int fd = luaLM_getfd (L, 1);
static struct termios attrs;
tcgetattr (fd, &attrs);
lua_pushlstring(L, (void *)&attrs, sizeof(attrs));
attrs.c_lflag &= ~(ECHO);
attrs.c_oflag &= ~(ONLCR); // disables the lf->crlf conversion
tcsetattr (fd, TCSAFLUSH, &attrs);
return 1;
}
int io_tty_restore (lua_State *L)
{
int fd = luaLM_getfd (L, 1);
size_t n = 0;
const void *attrs = luaL_checklstring(L, 2, &n);
if (n != sizeof(struct termios)) return luaL_error(L, "invalid attribute string length");
tcsetattr (fd, TCSAFLUSH, attrs);
return 0;
}
#include <sys/stat.h>
int io_file_mtime_size (lua_State *L)
{
const char *path=luaL_checkstring(L, 1);
struct stat st;
if(stat(path, &st) < 0)
return luaLM_posix_error(L, "stat");
#ifdef __APPLE__
double mtime = st.st_mtimespec.tv_sec + st.st_mtimespec.tv_nsec / 1.0e9;
#else
double mtime = st.st_mtim.tv_sec + st.st_mtim.tv_nsec / 1.0e9;
#endif
lua_pushnumber(L, mtime);
lua_pushnumber(L, st.st_size);
return 2;
}
static int _ioctl(lua_State *L, int fd, int code, void *arg, const char *name)
{
if(ioctl (fd, code, arg) < 0)
return luaLM_posix_error (L, name);
return 0;
}
int io_get_term_size (lua_State *L)
{
struct winsize ws;
_ioctl(L, 1, TIOCGWINSZ, &ws, __FUNCTION__);
lua_pushnumber(L, ws.ws_row);
lua_pushnumber(L, ws.ws_col);
return 2;
}
int luaopen_posix_c(lua_State *L);
int luaopen_socket_unix(lua_State *L);
int luaopen_serial(lua_State *L);
const struct luaL_reg platform_posix_preloads[] = {
{ "posix", luaopen_posix_c },
{ "socket.unix", luaopen_socket_unix },
{ "serial", luaopen_serial },
{ 0, 0 },
};
void lua_init_platform_posix(lua_State *L)
{
luaLM_preload (L, platform_posix_preloads);
const struct luaL_reg os_additions[] = {
{ "pipe", os_pipe },
{ "getpid", os_getpid },
{ "time_unix", os_time_unix },
{ "forkpty", os_forkpty },
{ 0, 0 },
};
luaL_register (L, "os", os_additions);
const struct luaL_reg io_additions[] = {
{ "raw_read", io_raw_read },
{ "raw_write", io_raw_write },
{ "raw_close", io_raw_close },
{ "setinherit", io_setinherit },
{ "fsync", io_fsync },
{ "immediate_stdin", io_immediate_stdin },
{ "get_term_size", io_get_term_size },
{ "tty_noecho", io_tty_noecho },
{ "tty_restore", io_tty_restore },
{ "file_mtime_size", io_file_mtime_size },
{ 0, 0 },
};
luaL_register (L, "io", io_additions);
}