w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

loslib.c
Go to the documentation of this file.
1 /*
2 ** $Id: loslib.c,v 1.65.1.1 2017/04/19 17:29:57 roberto Exp $
3 ** Standard Operating System library
4 ** See Copyright Notice in lua.h
5 */
6 
7 #define loslib_c
8 #define LUA_LIB
9 
10 #include "lprefix.h"
11 
12 
13 #include <errno.h>
14 #include <locale.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <time.h>
18 
19 #include "lua.h"
20 
21 #include "lauxlib.h"
22 #include "lualib.h"
23 
24 
25 /*
26 ** {==================================================================
27 ** List of valid conversion specifiers for the 'strftime' function;
28 ** options are grouped by length; group of length 2 start with '||'.
29 ** ===================================================================
30 */
31 #if !defined(LUA_STRFTIMEOPTIONS) /* { */
32 
33 /* options for ANSI C 89 (only 1-char options) */
34 #define L_STRFTIMEC89 "aAbBcdHIjmMpSUwWxXyYZ%"
35 
36 /* options for ISO C 99 and POSIX */
37 #define L_STRFTIMEC99 "aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%" \
38  "||" "EcECExEXEyEY" "OdOeOHOIOmOMOSOuOUOVOwOWOy" /* two-char options */
39 
40 /* options for Windows */
41 #define L_STRFTIMEWIN "aAbBcdHIjmMpSUwWxXyYzZ%" \
42  "||" "#c#x#d#H#I#j#m#M#S#U#w#W#y#Y" /* two-char options */
43 
44 #if defined(LUA_USE_WINDOWS)
45 #define LUA_STRFTIMEOPTIONS L_STRFTIMEWIN
46 #elif defined(LUA_USE_C89)
47 #define LUA_STRFTIMEOPTIONS L_STRFTIMEC89
48 #else /* C99 specification */
49 #define LUA_STRFTIMEOPTIONS L_STRFTIMEC99
50 #endif
51 
52 #endif /* } */
53 /* }================================================================== */
54 
55 
56 /*
57 ** {==================================================================
58 ** Configuration for time-related stuff
59 ** ===================================================================
60 */
61 
62 #if !defined(l_time_t) /* { */
63 /*
64 ** type to represent time_t in Lua
65 */
66 #define l_timet lua_Integer
67 #define l_pushtime(L,t) lua_pushinteger(L,(lua_Integer)(t))
68 
69 static time_t l_checktime (lua_State *L, int arg) {
71  luaL_argcheck(L, (time_t)t == t, arg, "time out-of-bounds");
72  return (time_t)t;
73 }
74 
75 #endif /* } */
76 
77 
78 #if !defined(l_gmtime) /* { */
79 /*
80 ** By default, Lua uses gmtime/localtime, except when POSIX is available,
81 ** where it uses gmtime_r/localtime_r
82 */
83 
84 #if defined(LUA_USE_POSIX) /* { */
85 
86 #define l_gmtime(t,r) gmtime_r(t,r)
87 #define l_localtime(t,r) localtime_r(t,r)
88 
89 #else /* }{ */
90 
91 /* ISO C definitions */
92 #define l_gmtime(t,r) ((void)(r)->tm_sec, gmtime(t))
93 #define l_localtime(t,r) ((void)(r)->tm_sec, localtime(t))
94 
95 #endif /* } */
96 
97 #endif /* } */
98 
99 /* }================================================================== */
100 
101 
102 /*
103 ** {==================================================================
104 ** Configuration for 'tmpnam':
105 ** By default, Lua uses tmpnam except when POSIX is available, where
106 ** it uses mkstemp.
107 ** ===================================================================
108 */
109 #if !defined(lua_tmpnam) /* { */
110 
111 #if defined(LUA_USE_POSIX) /* { */
112 
113 #include <unistd.h>
114 
115 #define LUA_TMPNAMBUFSIZE 32
116 
117 #if !defined(LUA_TMPNAMTEMPLATE)
118 #define LUA_TMPNAMTEMPLATE "/tmp/lua_XXXXXX"
119 #endif
120 
121 #define lua_tmpnam(b,e) { \
122  strcpy(b, LUA_TMPNAMTEMPLATE); \
123  e = mkstemp(b); \
124  if (e != -1) close(e); \
125  e = (e == -1); }
126 
127 #else /* }{ */
128 
129 /* ISO C definitions */
130 #define LUA_TMPNAMBUFSIZE L_tmpnam
131 #define lua_tmpnam(b,e) { e = (tmpnam(b) == NULL); }
132 
133 #endif /* } */
134 
135 #endif /* } */
136 /* }================================================================== */
137 
138 #if defined(_WIN32) && !defined(__MINGW32__)
139 extern int win32_system(const char * cmd);
140 #endif
141 
142 
143 
144 static int os_execute (lua_State *L) {
145  const char *cmd = luaL_optstring(L, 1, NULL);
146 #if defined(_WIN32) && !defined(__MINGW32__)
147  int stat = win32_system(cmd);
148 #else
149  int stat = system(cmd);
150 #endif
151  if (cmd != NULL)
152  return luaL_execresult(L, stat);
153  else {
154  lua_pushboolean(L, stat); /* true if there is a shell */
155  return 1;
156  }
157 }
158 
159 
160 static int os_remove (lua_State *L) {
161  const char *filename = luaL_checkstring(L, 1);
162  return luaL_fileresult(L, remove(filename) == 0, filename);
163 }
164 
165 
166 static int os_rename (lua_State *L) {
167  const char *fromname = luaL_checkstring(L, 1);
168  const char *toname = luaL_checkstring(L, 2);
169  return luaL_fileresult(L, rename(fromname, toname) == 0, NULL);
170 }
171 
172 
173 static int os_tmpname (lua_State *L) {
174  char buff[LUA_TMPNAMBUFSIZE];
175  int err;
176  lua_tmpnam(buff, err);
177  if (err)
178  return luaL_error(L, "unable to generate a unique filename");
180  return 1;
181 }
182 
183 
184 static int os_getenv (lua_State *L) {
185  lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */
186  return 1;
187 }
188 
189 
190 static int os_clock (lua_State *L) {
192  return 1;
193 }
194 
195 
196 /*
197 ** {======================================================
198 ** Time/Date operations
199 ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
200 ** wday=%w+1, yday=%j, isdst=? }
201 ** =======================================================
202 */
203 
204 static void setfield (lua_State *L, const char *key, int value) {
206  lua_setfield(L, -2, key);
207 }
208 
209 static void setboolfield (lua_State *L, const char *key, int value) {
210  if (value < 0) /* undefined? */
211  return; /* does not set field */
213  lua_setfield(L, -2, key);
214 }
215 
216 
217 /*
218 ** Set all fields from structure 'tm' in the table on top of the stack
219 */
220 static void setallfields (lua_State *L, struct tm *stm) {
221  setfield(L, "sec", stm->tm_sec);
222  setfield(L, "min", stm->tm_min);
223  setfield(L, "hour", stm->tm_hour);
224  setfield(L, "day", stm->tm_mday);
225  setfield(L, "month", stm->tm_mon + 1);
226  setfield(L, "year", stm->tm_year + 1900);
227  setfield(L, "wday", stm->tm_wday + 1);
228  setfield(L, "yday", stm->tm_yday + 1);
229  setboolfield(L, "isdst", stm->tm_isdst);
230 }
231 
232 
233 static int getboolfield (lua_State *L, const char *key) {
234  int res;
235  res = (lua_getfield(L, -1, key) == LUA_TNIL) ? -1 : lua_toboolean(L, -1);
236  lua_pop(L, 1);
237  return res;
238 }
239 
240 
241 /* maximum value for date fields (to avoid arithmetic overflows with 'int') */
242 #if !defined(L_MAXDATEFIELD)
243 #define L_MAXDATEFIELD (INT_MAX / 2)
244 #endif
245 
246 static int getfield (lua_State *L, const char *key, int d, int delta) {
247  int isnum;
248  int t = lua_getfield(L, -1, key); /* get field and its type */
249  lua_Integer res = lua_tointegerx(L, -1, &isnum);
250  if (!isnum) { /* field is not an integer? */
251  if (t != LUA_TNIL) /* some other value? */
252  return luaL_error(L, "field '%s' is not an integer", key);
253  else if (d < 0) /* absent field; no default? */
254  return luaL_error(L, "field '%s' missing in date table", key);
255  res = d;
256  }
257  else {
258  if (!(-L_MAXDATEFIELD <= res && res <= L_MAXDATEFIELD))
259  return luaL_error(L, "field '%s' is out-of-bound", key);
260  res -= delta;
261  }
262  lua_pop(L, 1);
263  return (int)res;
264 }
265 
266 
267 static const char *checkoption (lua_State *L, const char *conv,
268  ptrdiff_t convlen, char *buff) {
269  const char *option = LUA_STRFTIMEOPTIONS;
270  int oplen = 1; /* length of options being checked */
271  for (; *option != '\0' && oplen <= convlen; option += oplen) {
272  if (*option == '|') /* next block? */
273  oplen++; /* will check options with next length (+1) */
274  else if (memcmp(conv, option, oplen) == 0) { /* match? */
275  memcpy(buff, conv, oplen); /* copy valid option to buffer */
276  buff[oplen] = '\0';
277  return conv + oplen; /* return next item */
278  }
279  }
280  luaL_argerror(L, 1,
281  lua_pushfstring(L, "invalid conversion specifier '%%%s'", conv));
282  return conv; /* to avoid warnings */
283 }
284 
285 
286 /* maximum size for an individual 'strftime' item */
287 #define SIZETIMEFMT 250
288 
289 
290 static int os_date (lua_State *L) {
291  size_t slen;
292  const char *s = luaL_optlstring(L, 1, "%c", &slen);
294  const char *se = s + slen; /* 's' end */
295  struct tm tmr, *stm;
296  if (*s == '!') { /* UTC? */
297  stm = l_gmtime(&t, &tmr);
298  s++; /* skip '!' */
299  }
300  else
301  stm = l_localtime(&t, &tmr);
302  if (stm == NULL) /* invalid date? */
303  return luaL_error(L,
304  "time result cannot be represented in this installation");
305  if (strcmp(s, "*t") == 0) {
306  lua_createtable(L, 0, 9); /* 9 = number of fields */
307  setallfields(L, stm);
308  }
309  else {
310  char cc[4]; /* buffer for individual conversion specifiers */
311  luaL_Buffer b;
312  cc[0] = '%';
313  luaL_buffinit(L, &b);
314  while (s < se) {
315  if (*s != '%') /* not a conversion specifier? */
316  luaL_addchar(&b, *s++);
317  else {
318  size_t reslen;
319  char *buff = luaL_prepbuffsize(&b, SIZETIMEFMT);
320  s++; /* skip '%' */
321  s = checkoption(L, s, se - s, cc + 1); /* copy specifier to 'cc' */
322  reslen = strftime(buff, SIZETIMEFMT, cc, stm);
323  luaL_addsize(&b, reslen);
324  }
325  }
326  luaL_pushresult(&b);
327  }
328  return 1;
329 }
330 
331 
332 static int os_time (lua_State *L) {
333  time_t t;
334  if (lua_isnoneornil(L, 1)) /* called without args? */
335  t = time(NULL); /* get current time */
336  else {
337  struct tm ts;
339  lua_settop(L, 1); /* make sure table is at the top */
340  ts.tm_sec = getfield(L, "sec", 0, 0);
341  ts.tm_min = getfield(L, "min", 0, 0);
342  ts.tm_hour = getfield(L, "hour", 12, 0);
343  ts.tm_mday = getfield(L, "day", -1, 0);
344  ts.tm_mon = getfield(L, "month", -1, 1);
345  ts.tm_year = getfield(L, "year", -1, 1900);
346  ts.tm_isdst = getboolfield(L, "isdst");
347  t = mktime(&ts);
348  setallfields(L, &ts); /* update fields with normalized values */
349  }
350  if (t != (time_t)(l_timet)t || t == (time_t)(-1))
351  return luaL_error(L,
352  "time result cannot be represented in this installation");
353  l_pushtime(L, t);
354  return 1;
355 }
356 
357 
358 static int os_difftime (lua_State *L) {
359  time_t t1 = l_checktime(L, 1);
360  time_t t2 = l_checktime(L, 2);
361  lua_pushnumber(L, (lua_Number)difftime(t1, t2));
362  return 1;
363 }
364 
365 /* }====================================================== */
366 
367 
368 static int os_setlocale (lua_State *L) {
369  static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
370  LC_NUMERIC, LC_TIME};
371  static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
372  "numeric", "time", NULL};
373  const char *l = luaL_optstring(L, 1, NULL);
374  int op = luaL_checkoption(L, 2, "all", catnames);
376  return 1;
377 }
378 
379 
380 static int os_exit (lua_State *L) {
381  int status;
382  if (lua_isboolean(L, 1))
384  else
386  if (lua_toboolean(L, 2))
387  lua_close(L);
388  if (L) exit(status); /* 'if' to avoid warnings for unreachable 'return' */
389  return 0;
390 }
391 
392 
393 static const luaL_Reg syslib[] = {
394  {"clock", os_clock},
395  {"date", os_date},
396  {"difftime", os_difftime},
397  {"execute", os_execute},
398  {"exit", os_exit},
399  {"getenv", os_getenv},
400  {"remove", os_remove},
401  {"rename", os_rename},
402  {"setlocale", os_setlocale},
403  {"time", os_time},
404  {"tmpname", os_tmpname},
405  {NULL, NULL}
406 };
407 
408 /* }====================================================== */
409 
410 
411 
413  luaL_newlib(L, syslib);
414  return 1;
415 }
416 
int ptrdiff_t
Definition: CPAL.d:3845
#define b
Definition: jpegint.h:372
int strcmp()
Definition: coll.cpp:143
char oplen[128]
Definition: dvibook.c:690
static double conv
Definition: dvicore.c:53
#define s
Definition: afcover.h:80
#define t
Definition: afcover.h:96
static char buff[2 *10240]
Definition: genind.c:46
#define d(n)
Definition: gpos-common.c:151
#define memcmp(s1, s2, n)
Definition: gsftopk.c:66
#define memcpy(d, s, n)
Definition: gsftopk.c:64
#define slen
Definition: hpcdtoppm.c:1329
#define EXIT_SUCCESS
Definition: cdjpeg.h:175
#define EXIT_FAILURE
Definition: cdjpeg.h:169
#define NULL
Definition: ftobjs.h:61
void exit()
char * getenv()
int win32_system(const char *cmd)
Definition: win32lib.c:403
#define system(p)
Definition: win32lib.h:269
#define setlocale(A, B)
Definition: cwebboot.c:31
long time_t
Definition: types.h:18
int remove()
static struct tm tm
Definition: localtime.c:216
time_t mktime(struct tm *const tmp)
Definition: localtime.c:1932
#define cmd(arg)
Definition: mitsu.h:44
@ err
Definition: mtxline.h:24
static int delta
Definition: pbmtolj.c:36
char * filename[256]
Definition: pbmtopk.c:46
#define res(length)
Definition: picttoppm.c:287
#define t1
#define t2
logical rename
Definition: pmxab.c:724
time_t time()
#define status
void lua_pushinteger(lua_State *L, lua_Integer n)
Definition: lapi.c:466
void lua_setfield(lua_State *L, int idx, const char *k)
Definition: lapi.c:777
const char * lua_pushfstring(lua_State *L, const char *fmt,...)
Definition: lapi.c:519
void lua_pushnumber(lua_State *L, lua_Number n)
Definition: lapi.c:458
lua_Integer lua_tointegerx(lua_State *L, int idx, int *pisnum)
Definition: lapi.c:356
void lua_settop(lua_State *L, int idx)
Definition: lapi.c:172
const char * lua_pushstring(lua_State *L, const char *s)
Definition: lapi.c:491
void lua_createtable(lua_State *L, int narray, int nrec)
Definition: lapi.c:684
int lua_getfield(lua_State *L, int idx, const char *k)
Definition: lapi.c:622
void lua_pushboolean(lua_State *L, int b)
Definition: lapi.c:557
int lua_toboolean(lua_State *L, int idx)
Definition: lapi.c:367
int luaL_argerror(lua_State *L, int arg, const char *extramsg)
Definition: lauxlib.c:164
char * luaL_prepbuffsize(luaL_Buffer *B, size_t sz)
Definition: lauxlib.c:505
const char * luaL_optlstring(lua_State *L, int arg, const char *def, size_t *len)
Definition: lauxlib.c:397
int luaL_error(lua_State *L, const char *fmt,...)
Definition: lauxlib.c:223
void luaL_pushresult(luaL_Buffer *B)
Definition: lauxlib.c:542
lua_Integer luaL_checkinteger(lua_State *L, int arg)
Definition: lauxlib.c:430
int luaL_fileresult(lua_State *L, int stat, const char *fname)
Definition: lauxlib.c:234
int luaL_execresult(lua_State *L, int stat)
Definition: lauxlib.c:274
lua_Integer luaL_optinteger(lua_State *L, int arg, lua_Integer def)
Definition: lauxlib.c:440
void luaL_checktype(lua_State *L, int arg, int t)
Definition: lauxlib.c:378
int luaL_checkoption(lua_State *L, int arg, const char *def, const char *const lst[])
Definition: lauxlib.c:348
void luaL_buffinit(lua_State *L, luaL_Buffer *B)
Definition: lauxlib.c:569
#define luaL_argcheck(L, cond, arg, extramsg)
Definition: lauxlib.h:122
#define luaL_newlib(L, l)
Definition: lauxlib.h:119
#define luaL_addchar(B, c)
Definition: lauxlib.h:157
#define luaL_optstring(L, n, d)
Definition: lauxlib.h:125
#define luaL_addsize(B, s)
Definition: lauxlib.h:161
#define luaL_opt(L, f, n, d)
Definition: lauxlib.h:137
#define luaL_checkstring(L, n)
Definition: lauxlib.h:124
int luaopen_os(lua_State *L)
Definition: loslib.c:412
void lua_close(lua_State *L)
Definition: lstate.c:341
#define LUA_TTABLE
Definition: lua.h:68
LUA_INTEGER lua_Integer
Definition: lua.h:92
#define lua_pop(L, n)
Definition: lua.h:343
#define LUA_TNIL
Definition: lua.h:63
#define lua_isboolean(L, n)
Definition: lua.h:355
#define lua_isnoneornil(L, n)
Definition: lua.h:358
double lua_Number
Definition: lua.h:88
#define LUAMOD_API
Definition: luaconf.h:266
#define SIZETIMEFMT
Definition: loslib.c:287
#define LUA_TMPNAMBUFSIZE
Definition: loslib.c:130
static int os_setlocale(lua_State *L)
Definition: loslib.c:368
static int os_execute(lua_State *L)
Definition: loslib.c:144
static int os_getenv(lua_State *L)
Definition: loslib.c:184
static int getboolfield(lua_State *L, const char *key)
Definition: loslib.c:233
static int os_date(lua_State *L)
Definition: loslib.c:290
static void setboolfield(lua_State *L, const char *key, int value)
Definition: loslib.c:209
static const luaL_Reg syslib[]
Definition: loslib.c:393
#define L_MAXDATEFIELD
Definition: loslib.c:243
#define l_localtime(t, r)
Definition: loslib.c:93
static void setfield(lua_State *L, const char *key, int value)
Definition: loslib.c:204
#define l_timet
Definition: loslib.c:66
static int os_tmpname(lua_State *L)
Definition: loslib.c:173
static int os_exit(lua_State *L)
Definition: loslib.c:380
#define l_gmtime(t, r)
Definition: loslib.c:92
static int os_rename(lua_State *L)
Definition: loslib.c:166
static int getfield(lua_State *L, const char *key, int d, int delta)
Definition: loslib.c:246
static const char * checkoption(lua_State *L, const char *conv, ptrdiff_t convlen, char *buff)
Definition: loslib.c:267
static int os_difftime(lua_State *L)
Definition: loslib.c:358
#define LUA_STRFTIMEOPTIONS
Definition: loslib.c:49
#define l_pushtime(L, t)
Definition: loslib.c:67
static int os_clock(lua_State *L)
Definition: loslib.c:190
static int os_time(lua_State *L)
Definition: loslib.c:332
static void setallfields(lua_State *L, struct tm *stm)
Definition: loslib.c:220
static time_t l_checktime(lua_State *L, int arg)
Definition: loslib.c:69
static int os_remove(lua_State *L)
Definition: loslib.c:160
#define lua_tmpnam(b, e)
Definition: loslib.c:131
#define CLOCKS_PER_SEC
Definition: sh.h:439
Definition: tokst.h:327
Definition: sh.h:1226
Definition: getopt.h:95
Definition: sh2.c:920
Definition: dvips.h:235
#define key
Definition: tex2xindy.c:753
return() int(((double) *(font_tbl[cur_fnt].wtbl+(int)(*(font_tbl[cur_fnt].char_wi+(int)(ch - font_tbl[cur_fnt].char_f)% 256)))/(double)(1L<< 20)) *(double) font_tbl[cur_fnt].scale)
@ L
Definition: ubidiimp.h:45
Definition: obx.h:51