summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/loslib.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lua/loslib.c')
-rw-r--r--apps/plugins/lua/loslib.c223
1 files changed, 50 insertions, 173 deletions
diff --git a/apps/plugins/lua/loslib.c b/apps/plugins/lua/loslib.c
index 350e848d38..6cb8c0541b 100644
--- a/apps/plugins/lua/loslib.c
+++ b/apps/plugins/lua/loslib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: loslib.c,v 1.40.1.1 2013/04/12 18:48:47 roberto Exp $ 2** $Id: loslib.c,v 1.19.1.3 2008/01/18 16:38:18 roberto Exp $
3** Standard Operating System library 3** Standard Operating System library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -19,109 +19,31 @@
19#include "lualib.h" 19#include "lualib.h"
20 20
21 21
22/* 22static int os_pushresult (lua_State *L, int i, const char *filename) {
23** list of valid conversion specifiers for the 'strftime' function 23 int en = errno; /* calls to Lua API may change this value */
24*/ 24 if (i) {
25#if !defined(LUA_STRFTIMEOPTIONS) 25 lua_pushboolean(L, 1);
26
27#if !defined(LUA_USE_POSIX)
28#define LUA_STRFTIMEOPTIONS { "aAbBcdHIjmMpSUwWxXyYz%", "" }
29#else
30#define LUA_STRFTIMEOPTIONS \
31 { "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%", "" \
32 "", "E", "cCxXyY", \
33 "O", "deHImMSuUVwWy" }
34#endif
35
36#endif
37
38
39
40/*
41** By default, Lua uses tmpnam except when POSIX is available, where it
42** uses mkstemp.
43*/
44#if defined(LUA_USE_MKSTEMP)
45#include <unistd.h>
46#define LUA_TMPNAMBUFSIZE 32
47#define lua_tmpnam(b,e) { \
48 strcpy(b, "/tmp/lua_XXXXXX"); \
49 e = mkstemp(b); \
50 if (e != -1) close(e); \
51 e = (e == -1); }
52
53#elif !defined(lua_tmpnam)
54
55#define LUA_TMPNAMBUFSIZE L_tmpnam
56#define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); }
57
58#endif
59
60
61/*
62** By default, Lua uses gmtime/localtime, except when POSIX is available,
63** where it uses gmtime_r/localtime_r
64*/
65#if defined(LUA_USE_GMTIME_R)
66
67#define l_gmtime(t,r) gmtime_r(t,r)
68#define l_localtime(t,r) localtime_r(t,r)
69
70#elif !defined(l_gmtime)
71
72#define l_gmtime(t,r) ((void)r, gmtime(t))
73#define l_localtime(t,r) ((void)r, localtime(t))
74
75#endif
76
77
78
79static int os_execute (lua_State *L) {
80 const char *cmd = luaL_optstring(L, 1, NULL);
81 int stat = -1;
82 if (cmd != NULL)
83 return luaL_execresult(L, stat);
84 else {
85 lua_pushboolean(L, stat); /* true if there is a shell */
86 return 1; 26 return 1;
87 } 27 }
28 else {
29 lua_pushnil(L);
30 lua_pushfstring(L, "%s: %s", filename, strerror(en));
31 lua_pushinteger(L, en);
32 return 3;
33 }
88} 34}
89 35
90 36
91static int os_remove (lua_State *L) { 37static int os_remove (lua_State *L) {
92 const char *filename = luaL_checkstring(L, 1); 38 const char *filename = luaL_checkstring(L, 1);
93 return luaL_fileresult(L, rb->remove(filename) == 0, filename); 39 return os_pushresult(L, rb->remove(filename) == 0, filename);
94} 40}
95 41
96 42
97static int os_rename (lua_State *L) { 43static int os_rename (lua_State *L) {
98 const char *fromname = luaL_checkstring(L, 1); 44 const char *fromname = luaL_checkstring(L, 1);
99 const char *toname = luaL_checkstring(L, 2); 45 const char *toname = luaL_checkstring(L, 2);
100 return luaL_fileresult(L, rb->rename(fromname, toname) == 0, NULL); 46 return os_pushresult(L, rb->rename(fromname, toname) == 0, fromname);
101}
102
103#if 0
104static int os_tmpname (lua_State *L) {
105 char buff[LUA_TMPNAMBUFSIZE];
106 int err;
107 lua_tmpnam(buff, err);
108 if (err)
109 return luaL_error(L, "unable to generate a unique filename");
110 lua_pushstring(L, buff);
111 return 1;
112}
113#endif
114
115
116static int os_getenv (lua_State *L) {
117 lua_pushnil(L);
118 return 1;
119}
120
121
122static int os_clock (lua_State *L) {
123 lua_pushnumber(L, 0);
124 return 1;
125} 47}
126 48
127 49
@@ -145,6 +67,7 @@ static void setboolfield (lua_State *L, const char *key, int value) {
145 lua_setfield(L, -2, key); 67 lua_setfield(L, -2, key);
146} 68}
147 69
70#if CONFIG_RTC
148static int getboolfield (lua_State *L, const char *key) { 71static int getboolfield (lua_State *L, const char *key) {
149 int res; 72 int res;
150 lua_getfield(L, -1, key); 73 lua_getfield(L, -1, key);
@@ -155,10 +78,11 @@ static int getboolfield (lua_State *L, const char *key) {
155 78
156 79
157static int getfield (lua_State *L, const char *key, int d) { 80static int getfield (lua_State *L, const char *key, int d) {
158 int res, isnum; 81 int res;
159 lua_getfield(L, -1, key); 82 lua_getfield(L, -1, key);
160 res = (int)lua_tointegerx(L, -1, &isnum); 83 if (lua_isnumber(L, -1))
161 if (!isnum) { 84 res = (int)lua_tointeger(L, -1);
85 else {
162 if (d < 0) 86 if (d < 0)
163 return luaL_error(L, "field " LUA_QS " missing in date table", key); 87 return luaL_error(L, "field " LUA_QS " missing in date table", key);
164 res = d; 88 res = d;
@@ -166,42 +90,22 @@ static int getfield (lua_State *L, const char *key, int d) {
166 lua_pop(L, 1); 90 lua_pop(L, 1);
167 return res; 91 return res;
168} 92}
169 93#endif
170
171static const char *checkoption (lua_State *L, const char *conv, char *buff) {
172 static const char *const options[] = LUA_STRFTIMEOPTIONS;
173 unsigned int i;
174 for (i = 0; i < sizeof(options)/sizeof(options[0]); i += 2) {
175 if (*conv != '\0' && strchr(options[i], *conv) != NULL) {
176 buff[1] = *conv;
177 if (*options[i + 1] == '\0') { /* one-char conversion specifier? */
178 buff[2] = '\0'; /* end buffer */
179 return conv + 1;
180 }
181 else if (*(conv + 1) != '\0' &&
182 strchr(options[i + 1], *(conv + 1)) != NULL) {
183 buff[2] = *(conv + 1); /* valid two-char conversion specifier */
184 buff[3] = '\0'; /* end buffer */
185 return conv + 2;
186 }
187 }
188 }
189 luaL_argerror(L, 1,
190 lua_pushfstring(L, "invalid conversion specifier '%%%s'", conv));
191 return conv; /* to avoid warnings */
192}
193 94
194 95
195static int os_date (lua_State *L) { 96static int os_date (lua_State *L) {
196 const char *s = luaL_optstring(L, 1, "%c"); 97 const char *s = luaL_optstring(L, 1, "%c");
197 time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL)); 98 time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2,
198 struct tm tmr, *stm; 99#if CONFIG_RTC
199 if (*s == '!') { /* UTC? */ 100 rb->mktime(rb->get_time())
200 stm = l_gmtime(&t, &tmr); 101#else
102 0
103#endif
104 );
105 struct tm *stm;
106 if (*s == '!') /* UTC? */ /* Rockbox doesn't support timezones */
201 s++; /* skip `!' */ 107 s++; /* skip `!' */
202 } 108 stm = gmtime(&t);
203 else
204 stm = l_localtime(&t, &tmr);
205 if (stm == NULL) /* invalid date? */ 109 if (stm == NULL) /* invalid date? */
206 lua_pushnil(L); 110 lua_pushnil(L);
207 else if (strcmp(s, "*t") == 0) { 111 else if (strcmp(s, "*t") == 0) {
@@ -217,17 +121,17 @@ static int os_date (lua_State *L) {
217 setboolfield(L, "isdst", stm->tm_isdst); 121 setboolfield(L, "isdst", stm->tm_isdst);
218 } 122 }
219 else { 123 else {
220 char cc[4]; 124 char cc[3];
221 luaL_Buffer b; 125 luaL_Buffer b;
222 cc[0] = '%'; 126 cc[0] = '%'; cc[2] = '\0';
223 luaL_buffinit(L, &b); 127 luaL_buffinit(L, &b);
224 while (*s) { 128 for (; *s; s++) {
225 if (*s != '%') /* no conversion specifier? */ 129 if (*s != '%' || *(s + 1) == '\0') /* no conversion specifier? */
226 luaL_addchar(&b, *s++); 130 luaL_addchar(&b, *s);
227 else { 131 else {
228 size_t reslen; 132 size_t reslen;
229 char buff[200]; /* should be big enough for any conversion result */ 133 char buff[200]; /* should be big enough for any conversion result */
230 s = checkoption(L, s + 1, cc); 134 cc[1] = *(++s);
231 reslen = strftime(buff, sizeof(buff), cc, stm); 135 reslen = strftime(buff, sizeof(buff), cc, stm);
232 luaL_addlstring(&b, buff, reslen); 136 luaL_addlstring(&b, buff, reslen);
233 } 137 }
@@ -237,11 +141,11 @@ static int os_date (lua_State *L) {
237 return 1; 141 return 1;
238} 142}
239 143
240
241static int os_time (lua_State *L) { 144static int os_time (lua_State *L) {
242 time_t t; 145 time_t t = -1;
146#if CONFIG_RTC
243 if (lua_isnoneornil(L, 1)) /* called without args? */ 147 if (lua_isnoneornil(L, 1)) /* called without args? */
244 t = time(NULL); /* get current time */ 148 t = rb->mktime(rb->get_time()); /* get current time */
245 else { 149 else {
246 struct tm ts; 150 struct tm ts;
247 luaL_checktype(L, 1, LUA_TTABLE); 151 luaL_checktype(L, 1, LUA_TTABLE);
@@ -253,8 +157,9 @@ static int os_time (lua_State *L) {
253 ts.tm_mon = getfield(L, "month", -1) - 1; 157 ts.tm_mon = getfield(L, "month", -1) - 1;
254 ts.tm_year = getfield(L, "year", -1) - 1900; 158 ts.tm_year = getfield(L, "year", -1) - 1900;
255 ts.tm_isdst = getboolfield(L, "isdst"); 159 ts.tm_isdst = getboolfield(L, "isdst");
256 t = mktime(&ts); 160 t = rb->mktime(&ts);
257 } 161 }
162#endif
258 if (t == (time_t)(-1)) 163 if (t == (time_t)(-1))
259 lua_pushnil(L); 164 lua_pushnil(L);
260 else 165 else
@@ -263,54 +168,26 @@ static int os_time (lua_State *L) {
263} 168}
264 169
265 170
266static int os_difftime (lua_State *L) {
267 lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
268 (time_t)(luaL_optnumber(L, 2, 0))));
269 return 1;
270}
271
272/* }====================================================== */ 171/* }====================================================== */
273 172
274 173
275#if 0
276static int os_setlocale (lua_State *L) {
277 static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
278 LC_NUMERIC, LC_TIME};
279 static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
280 "numeric", "time", NULL};
281 const char *l = luaL_optstring(L, 1, NULL);
282 int op = luaL_checkoption(L, 2, "all", catnames);
283 lua_pushstring(L, setlocale(cat[op], l));
284 return 1;
285}
286#endif
287
288
289static int os_exit (lua_State *L) { 174static int os_exit (lua_State *L) {
290 int status; 175 exit(luaL_optint(L, 1, EXIT_SUCCESS));
291 if (lua_isboolean(L, 1)) 176 return EXIT_SUCCESS; /* never reached, surpress warning */
292 status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE);
293 else
294 status = luaL_optint(L, 1, EXIT_SUCCESS);
295 if (lua_toboolean(L, 2))
296 lua_close(L);
297 if (L) exit(status); /* 'if' to avoid warnings for unreachable 'return' */
298 return 0;
299} 177}
300 178
301
302static const luaL_Reg syslib[] = { 179static const luaL_Reg syslib[] = {
303 {"clock", os_clock}, 180 //{"clock", os_clock},
304 {"date", os_date}, 181 {"date", os_date},
305 {"difftime", os_difftime}, 182 //{"difftime", os_difftime},
306 {"execute", os_execute}, 183 //{"execute", os_execute},
307 {"exit", os_exit}, 184 {"exit", os_exit},
308 {"getenv", os_getenv}, 185 //{"getenv", os_getenv},
309 {"remove", os_remove}, 186 {"remove", os_remove},
310 {"rename", os_rename}, 187 {"rename", os_rename},
311 /*{"setlocale", os_setlocale},*/ 188 //{"setlocale", os_setlocale},
312 {"time", os_time}, 189 {"time", os_time},
313 /*{"tmpname", os_tmpname},*/ 190 //{"tmpname", os_tmpname},
314 {NULL, NULL} 191 {NULL, NULL}
315}; 192};
316 193
@@ -318,8 +195,8 @@ static const luaL_Reg syslib[] = {
318 195
319 196
320 197
321LUAMOD_API int luaopen_os (lua_State *L) { 198LUALIB_API int luaopen_os (lua_State *L) {
322 luaL_newlib(L, syslib); 199 luaL_register(L, LUA_OSLIBNAME, syslib);
323 return 1; 200 return 1;
324} 201}
325 202