aboutsummaryrefslogtreecommitdiff
path: root/src/sysbin/lx/lxsyslib.c
blob: 5bfec3e6e29c2b769deeba27c20b6d3cd5297e1e (plain) (blame)
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
/*
** Lua eXtended system library
*/

#define lxsyslib_c
#define LUA_LIB

#include <lua/lprefix.h>


#include <errno.h>
#include <locale.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include <lua/lua.h>

#include <lua/lauxlib.h>
#include <lua/lualib.h>

#include "lxlib.h"



static void setintfield (lua_State *L, const char *key, int value) {
  lua_pushinteger(L, value);
  lua_setfield(L, -2, key);
}

static void setstrfield (lua_State *L, const char *key, const char* value) {
  lua_pushstring(L, value);
  lua_setfield(L, -2, key);
}


static int sys_dbg_print(lua_State *L) {
  const char *str = luaL_checkstring(L, 1);
  sc_dbg_print(str);
  return 0;
}

static int sys_yield(lua_State *L) {
  sc_yield();
  return 0;
}

static int sys_exit(lua_State *L) {
  int code = luaL_checkinteger(L, 1);
  sc_exit(code);
  return 0;
}

static int sys_usleep(lua_State *L) {
  int usecs = luaL_checkinteger(L, 1);
  sc_usleep(usecs);
  return 0;
}

/*
    Missing syscalls :
    - sys_new_thread, exit_thread -> into specific threading library
    - mmap, mmap_file, mchmap, munmap -> into specific memory-related library ??
*/

static int sys_create(lua_State *L) {
  const char *name = luaL_checkstring(L, 1);
  int type = luaL_checkinteger(L, 2);
  lua_pushboolean(L, sc_create(name, type));
  return 1;
}

static int sys_delete(lua_State *L) {
  const char *name = luaL_checkstring(L, 1);
  lua_pushboolean(L, sc_delete(name));
  return 1;
}

static int sys_move(lua_State *L) {
  const char *oldname = luaL_checkstring(L, 1);
  const char *newname = luaL_checkstring(L, 2);
  lua_pushboolean(L, sc_move(oldname, newname));
  return 1;
}

static int sys_stat(lua_State *L) {
  const char* name = luaL_checkstring(L, 1);
  stat_t s;
  if (sc_stat(name, &s)) {
    lua_createtable(L, 0, 3);
    setintfield(L, "type", s.type);
    setintfield(L, "access", s.access);
    setintfield(L, "size", s.size);
  } else {
    lua_pushnil(L);
  }
  return 1;
}


static int sys_open(lua_State *L) {
  const char* name = luaL_checkstring(L, 1);
  int mode = luaL_checkinteger(L, 2);
  fd_t fh = sc_open(name, mode);
  if (fh) {
    lua_pushinteger(L, fh);
  } else {
    lua_pushnil(L);
  }
  return 1;
}

static int sys_close(lua_State *L) {
  int fd = luaL_checkinteger(L, 1);
  sc_close(fd);
  return 0;
}

static int sys_read(lua_State *L) {
  int fd = luaL_checkinteger(L, 1);
  int offset = luaL_checkinteger(L, 2);
  int len = luaL_checkinteger(L, 3);

  luaL_Buffer b;
  char* bufptr = luaL_buffinitsize(L, &b, len);
  int ret = sc_read(fd, offset, len, bufptr);

  luaL_pushresultsize(&b, ret);
  lua_pushinteger(L, ret);
  return 2;
}

static int sys_write(lua_State *L) {
  int fd = luaL_checkinteger(L, 1);
  int offset = luaL_checkinteger(L, 2);
  const char* str = luaL_checkstring(L, 3);
  int len = luaL_len(L, 3);

  int ret = sc_write(fd, offset, len, str);
  lua_pushinteger(L, ret);
  return 1;
}

static int sys_readdir(lua_State *L) {
  int fd = luaL_checkinteger(L, 1);
  int ent_no = luaL_checkinteger(L, 2);

  dirent_t d;
  bool ret = sc_readdir(fd, ent_no, &d);
  if (ret) {
    lua_createtable(L, 0, 4);
    setstrfield(L, "name", d.name);
    setintfield(L, "type", d.st.type);
    setintfield(L, "access", d.st.access);
    setintfield(L, "size", d.st.size);
  } else {
    lua_pushnil(L);
  }
  return 1;
}

static int sys_stat_open(lua_State *L) {
  int fd = luaL_checkinteger(L, 1);
  stat_t s;
  if (sc_stat_open(fd, &s)) {
    lua_createtable(L, 0, 3);
    setintfield(L, "type", s.type);
    setintfield(L, "access", s.access);
    setintfield(L, "size", s.size);
  } else {
    lua_pushnil(L);
  }
  return 1;
}

static int sys_ioctl(lua_State *L) {
  // TODO
  return 0;
}

static int sys_fctl(lua_State *L) {
  // TODO
  return 0;
}

static int sys_select(lua_State *L) {
  // TODO
  return 0;
}


static const luaL_Reg syslib[] = {
  {"dbg_print", sys_dbg_print},
  {"yield",     sys_yield},
  {"exit",      sys_exit},
  {"usleep",    sys_usleep},
  {"create",    sys_create},
  {"delete",    sys_delete},
  {"move",      sys_move},
  {"stat",      sys_stat},
  {"open",      sys_open},
  {"close",     sys_close},
  {"read",      sys_read},
  {"write",     sys_write},
  {"readdir",   sys_readdir},
  {"stat_open", sys_stat_open},

  {NULL, NULL}
};

/* }====================================================== */



LUAMOD_API int lx_open_sys (lua_State *L) {
  luaL_newlib(L, syslib);
  return 1;
}


/* vim: set sts=2 ts=2 sw=2 tw=0 et :*/