The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
loslib.cpp
Go to the documentation of this file.
1 /*
2 ** Standard Operating System library
3 ** See Copyright Notice in lua.h
4 */
5 
6 
7 #include <errno.h>
8 #include <locale.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <time.h>
12 
13 #define loslib_c
14 #define LUA_LIB
15 
16 #include "lua.h"
17 
18 #include "lauxlib.h"
19 #include "lualib.h"
20 
21 
22 /*
23 ** list of valid conversion specifiers for the 'strftime' function
24 */
25 #if !defined(LUA_STRFTIMEOPTIONS)
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 
79 static int os_execute (lua_State *L) {
80  const char *cmd = luaL_optstring(L, 1, NULL);
81  int stat = system(cmd);
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;
87  }
88 }
89 
90 
91 static int os_remove (lua_State *L) {
92  const char *filename = luaL_checkstring(L, 1);
93  return luaL_fileresult(L, remove(filename) == 0, filename);
94 }
95 
96 
97 static int os_rename (lua_State *L) {
98  const char *fromname = luaL_checkstring(L, 1);
99  const char *toname = luaL_checkstring(L, 2);
100  return luaL_fileresult(L, rename(fromname, toname) == 0, NULL);
101 }
102 
103 
104 static 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 
114 
115 static int os_getenv (lua_State *L) {
116  lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */
117  return 1;
118 }
119 
120 
121 static int os_clock (lua_State *L) {
122  lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
123  return 1;
124 }
125 
126 
127 /*
128 ** {======================================================
129 ** Time/Date operations
130 ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
131 ** wday=%w+1, yday=%j, isdst=? }
132 ** =======================================================
133 */
134 
135 static void setfield (lua_State *L, const char *key, int value) {
136  lua_pushinteger(L, value);
137  lua_setfield(L, -2, key);
138 }
139 
140 static void setboolfield (lua_State *L, const char *key, int value) {
141  if (value < 0) /* undefined? */
142  return; /* does not set field */
143  lua_pushboolean(L, value);
144  lua_setfield(L, -2, key);
145 }
146 
147 static int getboolfield (lua_State *L, const char *key) {
148  int res;
149  lua_getfield(L, -1, key);
150  res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);
151  lua_pop(L, 1);
152  return res;
153 }
154 
155 
156 static int getfield (lua_State *L, const char *key, int d) {
157  int res, isnum;
158  lua_getfield(L, -1, key);
159  res = (int)lua_tointegerx(L, -1, &isnum);
160  if (!isnum) {
161  if (d < 0)
162  return luaL_error(L, "field " LUA_QS " missing in date table", key);
163  res = d;
164  }
165  lua_pop(L, 1);
166  return res;
167 }
168 
169 
170 static const char *checkoption (lua_State *L, const char *conv, char *buff) {
171  static const char *const options[] = LUA_STRFTIMEOPTIONS;
172  unsigned int i;
173  for (i = 0; i < sizeof(options)/sizeof(options[0]); i += 2) {
174  if (*conv != '\0' && strchr(options[i], *conv) != NULL) {
175  buff[1] = *conv;
176  if (*options[i + 1] == '\0') { /* one-char conversion specifier? */
177  buff[2] = '\0'; /* end buffer */
178  return conv + 1;
179  }
180  else if (*(conv + 1) != '\0' &&
181  strchr(options[i + 1], *(conv + 1)) != NULL) {
182  buff[2] = *(conv + 1); /* valid two-char conversion specifier */
183  buff[3] = '\0'; /* end buffer */
184  return conv + 2;
185  }
186  }
187  }
188  luaL_argerror(L, 1,
189  lua_pushfstring(L, "invalid conversion specifier '%%%s'", conv));
190  return conv; /* to avoid warnings */
191 }
192 
193 
194 static int os_date (lua_State *L) {
195  const char *s = luaL_optstring(L, 1, "%c");
196  time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));
197  struct tm tmr, *stm;
198  if (*s == '!') { /* UTC? */
199  stm = l_gmtime(&t, &tmr);
200  s++; /* skip `!' */
201  }
202  else
203  stm = l_localtime(&t, &tmr);
204  if (stm == NULL) /* invalid date? */
205  lua_pushnil(L);
206  else if (strcmp(s, "*t") == 0) {
207  lua_createtable(L, 0, 9); /* 9 = number of fields */
208  setfield(L, "sec", stm->tm_sec);
209  setfield(L, "min", stm->tm_min);
210  setfield(L, "hour", stm->tm_hour);
211  setfield(L, "day", stm->tm_mday);
212  setfield(L, "month", stm->tm_mon+1);
213  setfield(L, "year", stm->tm_year+1900);
214  setfield(L, "wday", stm->tm_wday+1);
215  setfield(L, "yday", stm->tm_yday+1);
216  setboolfield(L, "isdst", stm->tm_isdst);
217  }
218  else {
219  char cc[4];
220  luaL_Buffer b;
221  cc[0] = '%';
222  luaL_buffinit(L, &b);
223  while (*s) {
224  if (*s != '%') /* no conversion specifier? */
225  luaL_addchar(&b, *s++);
226  else {
227  size_t reslen;
228  char buff[200]; /* should be big enough for any conversion result */
229  s = checkoption(L, s + 1, cc);
230  reslen = strftime(buff, sizeof(buff), cc, stm);
231  luaL_addlstring(&b, buff, reslen);
232  }
233  }
234  luaL_pushresult(&b);
235  }
236  return 1;
237 }
238 
239 
240 static int os_time (lua_State *L) {
241  time_t t;
242  if (lua_isnoneornil(L, 1)) /* called without args? */
243  t = time(NULL); /* get current time */
244  else {
245  struct tm ts;
246  luaL_checktype(L, 1, LUA_TTABLE);
247  lua_settop(L, 1); /* make sure table is at the top */
248  ts.tm_sec = getfield(L, "sec", 0);
249  ts.tm_min = getfield(L, "min", 0);
250  ts.tm_hour = getfield(L, "hour", 12);
251  ts.tm_mday = getfield(L, "day", -1);
252  ts.tm_mon = getfield(L, "month", -1) - 1;
253  ts.tm_year = getfield(L, "year", -1) - 1900;
254  ts.tm_isdst = getboolfield(L, "isdst");
255  t = mktime(&ts);
256  }
257  if (t == (time_t)(-1))
258  lua_pushnil(L);
259  else
260  lua_pushnumber(L, (lua_Number)t);
261  return 1;
262 }
263 
264 
265 static int os_difftime (lua_State *L) {
266  lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
267  (time_t)(luaL_optnumber(L, 2, 0))));
268  return 1;
269 }
270 
271 /* }====================================================== */
272 
273 
274 static int os_setlocale (lua_State *L) {
275  static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
276  LC_NUMERIC, LC_TIME};
277  static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
278  "numeric", "time", NULL};
279  const char *l = luaL_optstring(L, 1, NULL);
280  int op = luaL_checkoption(L, 2, "all", catnames);
281  lua_pushstring(L, setlocale(cat[op], l));
282  return 1;
283 }
284 
285 
286 static int os_exit (lua_State *L) {
287  int status;
288  if (lua_isboolean(L, 1))
289  status = (lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE);
290  else
291  status = luaL_optint(L, 1, EXIT_SUCCESS);
292  if (lua_toboolean(L, 2))
293  lua_close(L);
294  if (L) exit(status); /* 'if' to avoid warnings for unreachable 'return' */
295  return 0;
296 }
297 
298 
299 static const luaL_Reg syslib[] = {
300  {"clock", os_clock},
301  {"date", os_date},
302  {"difftime", os_difftime},
303  {"execute", os_execute},
304  {"exit", os_exit},
305  {"getenv", os_getenv},
306  {"remove", os_remove},
307  {"rename", os_rename},
308  {"setlocale", os_setlocale},
309  {"time", os_time},
310  {"tmpname", os_tmpname},
311  {NULL, NULL}
312 };
313 
314 /* }====================================================== */
315 
316 
317 
319  luaL_newlib(L, syslib);
320  return 1;
321 }
322 
#define LUA_STRFTIMEOPTIONS
Definition: loslib.cpp:28
#define lua_isnoneornil(L, n)
Definition: lua.h:337
size_t strftime(char *str, size_t count, const std::string &format, const std::tm *time)
Definition: strftime.cpp:132
LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
Definition: lapi.cpp:667
LUA_API void lua_getfield(lua_State *L, int idx, const char *k)
Definition: lapi.cpp:622
LUA_API void lua_settop(lua_State *L, int idx)
Definition: lapi.cpp:159
LUA_API void lua_pushboolean(lua_State *L, int b)
Definition: lapi.cpp:571
LUALIB_API void luaL_checktype(lua_State *L, int narg, int t)
Definition: lauxlib.cpp:347
LUALIB_API void luaL_pushresult(luaL_Buffer *B)
Definition: lauxlib.cpp:473
static int os_time(lua_State *L)
Definition: loslib.cpp:240
static int os_execute(lua_State *L)
Definition: loslib.cpp:79
#define d
GLdouble GLdouble t
Definition: glew.h:1366
#define lua_pop(L, n)
Definition: lua.h:322
GLdouble l
Definition: glew.h:6966
GLdouble GLdouble GLdouble b
Definition: glew.h:6966
LUA_API lua_Integer lua_tointegerx(lua_State *L, int idx, int *isnum)
Definition: lapi.cpp:343
#define l_localtime(t, r)
Definition: loslib.cpp:73
const config & options()
#define l_gmtime(t, r)
Definition: loslib.cpp:72
static int os_tmpname(lua_State *L)
Definition: loslib.cpp:104
LUAMOD_API int luaopen_os(lua_State *L)
Definition: loslib.cpp:318
LUALIB_API int luaL_fileresult(lua_State *L, int stat, const char *fname)
Definition: lauxlib.cpp:209
static int os_difftime(lua_State *L)
Definition: loslib.cpp:265
LUALIB_API int luaL_checkoption(lua_State *L, int narg, const char *def, const char *const lst[])
Definition: lauxlib.cpp:322
#define luaL_opt(L, f, n, d)
Definition: lauxlib.h:132
LUA_API int lua_toboolean(lua_State *L, int idx)
Definition: lapi.cpp:377
GLsizei const GLfloat * value
Definition: glew.h:1817
LUALIB_API void luaL_buffinit(lua_State *L, luaL_Buffer *B)
Definition: lauxlib.cpp:498
static int os_getenv(lua_State *L)
Definition: loslib.cpp:115
static int os_date(lua_State *L)
Definition: loslib.cpp:194
LUA_API void lua_close(lua_State *L)
Definition: lstate.cpp:316
LUALIB_API lua_Number luaL_checknumber(lua_State *L, int narg)
Definition: lauxlib.cpp:377
#define luaL_addchar(B, c)
Definition: lauxlib.h:152
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
LUALIB_API void luaL_addlstring(luaL_Buffer *B, const char *s, size_t l)
Definition: lauxlib.cpp:461
LUA_API void lua_pushnil(lua_State *L)
Definition: lapi.cpp:459
LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
Definition: lapi.cpp:467
#define lua_tmpnam(b, e)
Definition: loslib.cpp:56
static int os_rename(lua_State *L)
Definition: loslib.cpp:97
LUALIB_API lua_Number luaL_optnumber(lua_State *L, int narg, lua_Number def)
Definition: lauxlib.cpp:386
#define lua_isboolean(L, n)
Definition: lua.h:334
static int os_exit(lua_State *L)
Definition: loslib.cpp:286
GLuint res
Definition: glew.h:9258
#define lua_isnil(L, n)
Definition: lua.h:333
static int os_remove(lua_State *L)
Definition: loslib.cpp:91
#define LUA_QS
Definition: luaconf.h:199
static void setboolfield(lua_State *L, const char *key, int value)
Definition: loslib.cpp:140
logger & err()
Definition: log.cpp:79
#define LUAMOD_API
Definition: luaconf.h:163
#define LUA_TMPNAMBUFSIZE
Definition: loslib.cpp:55
LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *extramsg)
Definition: lauxlib.cpp:152
size_t i
Definition: function.cpp:1057
#define luaL_newlib(L, l)
Definition: lauxlib.h:111
LUALIB_API int luaL_error(lua_State *L, const char *fmt,...)
Definition: lauxlib.cpp:198
static const char * checkoption(lua_State *L, const char *conv, char *buff)
Definition: loslib.cpp:170
#define luaL_optint(L, n, d)
Definition: lauxlib.h:118
LUALIB_API int luaL_execresult(lua_State *L, int stat)
Definition: lauxlib.cpp:249
LUA_API const char * lua_pushfstring(lua_State *L, const char *fmt,...)
Definition: lapi.cpp:536
static int os_clock(lua_State *L)
Definition: loslib.cpp:121
static int getfield(lua_State *L, const char *key, int d)
Definition: loslib.cpp:156
GLdouble s
Definition: glew.h:1358
#define LUA_TTABLE
Definition: lua.h:82
static void setfield(lua_State *L, const char *key, int value)
Definition: loslib.cpp:135
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
Definition: lapi.cpp:477
#define luaL_optstring(L, n, d)
Definition: lauxlib.h:116
LUA_NUMBER lua_Number
Definition: lua.h:102
LUA_API const char * lua_pushstring(lua_State *L, const char *s)
Definition: lapi.cpp:507
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
Definition: lapi.cpp:752
static const luaL_Reg syslib[]
Definition: loslib.cpp:299
static int getboolfield(lua_State *L, const char *key)
Definition: loslib.cpp:147
static int os_setlocale(lua_State *L)
Definition: loslib.cpp:274
#define luaL_checkstring(L, n)
Definition: lauxlib.h:115