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, 173 insertions, 50 deletions
diff --git a/apps/plugins/lua/loslib.c b/apps/plugins/lua/loslib.c
index 6cb8c0541b..350e848d38 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.19.1.3 2008/01/18 16:38:18 roberto Exp $ 2** $Id: loslib.c,v 1.40.1.1 2013/04/12 18:48:47 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,31 +19,109 @@
19#include "lualib.h" 19#include "lualib.h"
20 20
21 21
22static int os_pushresult (lua_State *L, int i, const char *filename) { 22/*
23 int en = errno; /* calls to Lua API may change this value */ 23** list of valid conversion specifiers for the 'strftime' function
24 if (i) { 24*/
25 lua_pushboolean(L, 1); 25#if !defined(LUA_STRFTIMEOPTIONS)
26 return 1; 26
27 } 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);
28 else { 84 else {
29 lua_pushnil(L); 85 lua_pushboolean(L, stat); /* true if there is a shell */
30 lua_pushfstring(L, "%s: %s", filename, strerror(en)); 86 return 1;
31 lua_pushinteger(L, en);
32 return 3;
33 } 87 }
34} 88}
35 89
36 90
37static int os_remove (lua_State *L) { 91static int os_remove (lua_State *L) {
38 const char *filename = luaL_checkstring(L, 1); 92 const char *filename = luaL_checkstring(L, 1);
39 return os_pushresult(L, rb->remove(filename) == 0, filename); 93 return luaL_fileresult(L, rb->remove(filename) == 0, filename);
40} 94}
41 95
42 96
43static int os_rename (lua_State *L) { 97static int os_rename (lua_State *L) {
44 const char *fromname = luaL_checkstring(L, 1); 98 const char *fromname = luaL_checkstring(L, 1);
45 const char *toname = luaL_checkstring(L, 2); 99 const char *toname = luaL_checkstring(L, 2);
46 return os_pushresult(L, rb->rename(fromname, toname) == 0, fromname); 100 return luaL_fileresult(L, rb->rename(fromname, toname) == 0, NULL);
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;
47} 125}
48 126
49 127
@@ -67,7 +145,6 @@ static void setboolfield (lua_State *L, const char *key, int value) {
67 lua_setfield(L, -2, key); 145 lua_setfield(L, -2, key);
68} 146}
69 147
70#if CONFIG_RTC
71static int getboolfield (lua_State *L, const char *key) { 148static int getboolfield (lua_State *L, const char *key) {
72 int res; 149 int res;
73 lua_getfield(L, -1, key); 150 lua_getfield(L, -1, key);
@@ -78,11 +155,10 @@ static int getboolfield (lua_State *L, const char *key) {
78 155
79 156
80static int getfield (lua_State *L, const char *key, int d) { 157static int getfield (lua_State *L, const char *key, int d) {
81 int res; 158 int res, isnum;
82 lua_getfield(L, -1, key); 159 lua_getfield(L, -1, key);
83 if (lua_isnumber(L, -1)) 160 res = (int)lua_tointegerx(L, -1, &isnum);
84 res = (int)lua_tointeger(L, -1); 161 if (!isnum) {
85 else {
86 if (d < 0) 162 if (d < 0)
87 return luaL_error(L, "field " LUA_QS " missing in date table", key); 163 return luaL_error(L, "field " LUA_QS " missing in date table", key);
88 res = d; 164 res = d;
@@ -90,22 +166,42 @@ static int getfield (lua_State *L, const char *key, int d) {
90 lua_pop(L, 1); 166 lua_pop(L, 1);
91 return res; 167 return res;
92} 168}
93#endif 169
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}
94 193
95 194
96static int os_date (lua_State *L) { 195static int os_date (lua_State *L) {
97 const char *s = luaL_optstring(L, 1, "%c"); 196 const char *s = luaL_optstring(L, 1, "%c");
98 time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, 197 time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));
99#if CONFIG_RTC 198 struct tm tmr, *stm;
100 rb->mktime(rb->get_time()) 199 if (*s == '!') { /* UTC? */
101#else 200 stm = l_gmtime(&t, &tmr);
102 0
103#endif
104 );
105 struct tm *stm;
106 if (*s == '!') /* UTC? */ /* Rockbox doesn't support timezones */
107 s++; /* skip `!' */ 201 s++; /* skip `!' */
108 stm = gmtime(&t); 202 }
203 else
204 stm = l_localtime(&t, &tmr);
109 if (stm == NULL) /* invalid date? */ 205 if (stm == NULL) /* invalid date? */
110 lua_pushnil(L); 206 lua_pushnil(L);
111 else if (strcmp(s, "*t") == 0) { 207 else if (strcmp(s, "*t") == 0) {
@@ -121,17 +217,17 @@ static int os_date (lua_State *L) {
121 setboolfield(L, "isdst", stm->tm_isdst); 217 setboolfield(L, "isdst", stm->tm_isdst);
122 } 218 }
123 else { 219 else {
124 char cc[3]; 220 char cc[4];
125 luaL_Buffer b; 221 luaL_Buffer b;
126 cc[0] = '%'; cc[2] = '\0'; 222 cc[0] = '%';
127 luaL_buffinit(L, &b); 223 luaL_buffinit(L, &b);
128 for (; *s; s++) { 224 while (*s) {
129 if (*s != '%' || *(s + 1) == '\0') /* no conversion specifier? */ 225 if (*s != '%') /* no conversion specifier? */
130 luaL_addchar(&b, *s); 226 luaL_addchar(&b, *s++);
131 else { 227 else {
132 size_t reslen; 228 size_t reslen;
133 char buff[200]; /* should be big enough for any conversion result */ 229 char buff[200]; /* should be big enough for any conversion result */
134 cc[1] = *(++s); 230 s = checkoption(L, s + 1, cc);
135 reslen = strftime(buff, sizeof(buff), cc, stm); 231 reslen = strftime(buff, sizeof(buff), cc, stm);
136 luaL_addlstring(&b, buff, reslen); 232 luaL_addlstring(&b, buff, reslen);
137 } 233 }
@@ -141,11 +237,11 @@ static int os_date (lua_State *L) {
141 return 1; 237 return 1;
142} 238}
143 239
240
144static int os_time (lua_State *L) { 241static int os_time (lua_State *L) {
145 time_t t = -1; 242 time_t t;
146#if CONFIG_RTC
147 if (lua_isnoneornil(L, 1)) /* called without args? */ 243 if (lua_isnoneornil(L, 1)) /* called without args? */
148 t = rb->mktime(rb->get_time()); /* get current time */ 244 t = time(NULL); /* get current time */
149 else { 245 else {
150 struct tm ts; 246 struct tm ts;
151 luaL_checktype(L, 1, LUA_TTABLE); 247 luaL_checktype(L, 1, LUA_TTABLE);
@@ -157,9 +253,8 @@ static int os_time (lua_State *L) {
157 ts.tm_mon = getfield(L, "month", -1) - 1; 253 ts.tm_mon = getfield(L, "month", -1) - 1;
158 ts.tm_year = getfield(L, "year", -1) - 1900; 254 ts.tm_year = getfield(L, "year", -1) - 1900;
159 ts.tm_isdst = getboolfield(L, "isdst"); 255 ts.tm_isdst = getboolfield(L, "isdst");
160 t = rb->mktime(&ts); 256 t = mktime(&ts);
161 } 257 }
162#endif
163 if (t == (time_t)(-1)) 258 if (t == (time_t)(-1))
164 lua_pushnil(L); 259 lua_pushnil(L);
165 else 260 else
@@ -168,26 +263,54 @@ static int os_time (lua_State *L) {
168} 263}
169 264
170 265
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
171/* }====================================================== */ 272/* }====================================================== */
172 273
173 274
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
174static int os_exit (lua_State *L) { 289static int os_exit (lua_State *L) {
175 exit(luaL_optint(L, 1, EXIT_SUCCESS)); 290 int status;
176 return EXIT_SUCCESS; /* never reached, surpress warning */ 291 if (lua_isboolean(L, 1))
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;
177} 299}
178 300
301
179static const luaL_Reg syslib[] = { 302static const luaL_Reg syslib[] = {
180 //{"clock", os_clock}, 303 {"clock", os_clock},
181 {"date", os_date}, 304 {"date", os_date},
182 //{"difftime", os_difftime}, 305 {"difftime", os_difftime},
183 //{"execute", os_execute}, 306 {"execute", os_execute},
184 {"exit", os_exit}, 307 {"exit", os_exit},
185 //{"getenv", os_getenv}, 308 {"getenv", os_getenv},
186 {"remove", os_remove}, 309 {"remove", os_remove},
187 {"rename", os_rename}, 310 {"rename", os_rename},
188 //{"setlocale", os_setlocale}, 311 /*{"setlocale", os_setlocale},*/
189 {"time", os_time}, 312 {"time", os_time},
190 //{"tmpname", os_tmpname}, 313 /*{"tmpname", os_tmpname},*/
191 {NULL, NULL} 314 {NULL, NULL}
192}; 315};
193 316
@@ -195,8 +318,8 @@ static const luaL_Reg syslib[] = {
195 318
196 319
197 320
198LUALIB_API int luaopen_os (lua_State *L) { 321LUAMOD_API int luaopen_os (lua_State *L) {
199 luaL_register(L, LUA_OSLIBNAME, syslib); 322 luaL_newlib(L, syslib);
200 return 1; 323 return 1;
201} 324}
202 325