annotate src/lua/loslib.c @ 132:d16cf9d829dd

converted rival-name letter-map to hexadecimal
author Robert McIntyre <rlm@mit.edu>
date Sat, 17 Mar 2012 21:32:29 -0500
parents 27763b933818
children
rev   line source
rlm@1 1 /*
rlm@1 2 ** $Id: loslib.c,v 1.19.1.3 2008/01/18 16:38:18 roberto Exp $
rlm@1 3 ** Standard Operating System library
rlm@1 4 ** See Copyright Notice in lua.h
rlm@1 5 */
rlm@1 6
rlm@1 7
rlm@1 8 #include <errno.h>
rlm@1 9 #include <locale.h>
rlm@1 10 #include <stdlib.h>
rlm@1 11 #include <string.h>
rlm@1 12 #include <time.h>
rlm@1 13
rlm@1 14 #define loslib_c
rlm@1 15 #define LUA_LIB
rlm@1 16
rlm@1 17 #include "lua.h"
rlm@1 18
rlm@1 19 #include "lauxlib.h"
rlm@1 20 #include "lualib.h"
rlm@1 21
rlm@1 22
rlm@1 23 static int os_pushresult (lua_State *L, int i, const char *filename) {
rlm@1 24 int en = errno; /* calls to Lua API may change this value */
rlm@1 25 if (i) {
rlm@1 26 lua_pushboolean(L, 1);
rlm@1 27 return 1;
rlm@1 28 }
rlm@1 29 else {
rlm@1 30 lua_pushnil(L);
rlm@1 31 lua_pushfstring(L, "%s: %s", filename, strerror(en));
rlm@1 32 lua_pushinteger(L, en);
rlm@1 33 return 3;
rlm@1 34 }
rlm@1 35 }
rlm@1 36
rlm@1 37
rlm@1 38 static int os_execute (lua_State *L) {
rlm@1 39 lua_pushinteger(L, system(luaL_optstring(L, 1, NULL)));
rlm@1 40 return 1;
rlm@1 41 }
rlm@1 42
rlm@1 43
rlm@1 44 static int os_remove (lua_State *L) {
rlm@1 45 const char *filename = luaL_checkstring(L, 1);
rlm@1 46 return os_pushresult(L, remove(filename) == 0, filename);
rlm@1 47 }
rlm@1 48
rlm@1 49
rlm@1 50 static int os_rename (lua_State *L) {
rlm@1 51 const char *fromname = luaL_checkstring(L, 1);
rlm@1 52 const char *toname = luaL_checkstring(L, 2);
rlm@1 53 return os_pushresult(L, rename(fromname, toname) == 0, fromname);
rlm@1 54 }
rlm@1 55
rlm@1 56
rlm@1 57 static int os_tmpname (lua_State *L) {
rlm@1 58 char buff[LUA_TMPNAMBUFSIZE];
rlm@1 59 int err;
rlm@1 60 lua_tmpnam(buff, err);
rlm@1 61 if (err)
rlm@1 62 return luaL_error(L, "unable to generate a unique filename");
rlm@1 63 lua_pushstring(L, buff);
rlm@1 64 return 1;
rlm@1 65 }
rlm@1 66
rlm@1 67
rlm@1 68 static int os_getenv (lua_State *L) {
rlm@1 69 lua_pushstring(L, getenv(luaL_checkstring(L, 1))); /* if NULL push nil */
rlm@1 70 return 1;
rlm@1 71 }
rlm@1 72
rlm@1 73
rlm@1 74 static int os_clock (lua_State *L) {
rlm@1 75 lua_pushnumber(L, ((lua_Number)clock())/(lua_Number)CLOCKS_PER_SEC);
rlm@1 76 return 1;
rlm@1 77 }
rlm@1 78
rlm@1 79
rlm@1 80 /*
rlm@1 81 ** {======================================================
rlm@1 82 ** Time/Date operations
rlm@1 83 ** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
rlm@1 84 ** wday=%w+1, yday=%j, isdst=? }
rlm@1 85 ** =======================================================
rlm@1 86 */
rlm@1 87
rlm@1 88 static void setfield (lua_State *L, const char *key, int value) {
rlm@1 89 lua_pushinteger(L, value);
rlm@1 90 lua_setfield(L, -2, key);
rlm@1 91 }
rlm@1 92
rlm@1 93 static void setboolfield (lua_State *L, const char *key, int value) {
rlm@1 94 if (value < 0) /* undefined? */
rlm@1 95 return; /* does not set field */
rlm@1 96 lua_pushboolean(L, value);
rlm@1 97 lua_setfield(L, -2, key);
rlm@1 98 }
rlm@1 99
rlm@1 100 static int getboolfield (lua_State *L, const char *key) {
rlm@1 101 int res;
rlm@1 102 lua_getfield(L, -1, key);
rlm@1 103 res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);
rlm@1 104 lua_pop(L, 1);
rlm@1 105 return res;
rlm@1 106 }
rlm@1 107
rlm@1 108
rlm@1 109 static int getfield (lua_State *L, const char *key, int d) {
rlm@1 110 int res;
rlm@1 111 lua_getfield(L, -1, key);
rlm@1 112 if (lua_isnumber(L, -1))
rlm@1 113 res = (int)lua_tointeger(L, -1);
rlm@1 114 else {
rlm@1 115 if (d < 0)
rlm@1 116 return luaL_error(L, "field " LUA_QS " missing in date table", key);
rlm@1 117 res = d;
rlm@1 118 }
rlm@1 119 lua_pop(L, 1);
rlm@1 120 return res;
rlm@1 121 }
rlm@1 122
rlm@1 123
rlm@1 124 static int os_date (lua_State *L) {
rlm@1 125 const char *s = luaL_optstring(L, 1, "%c");
rlm@1 126 time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));
rlm@1 127 struct tm *stm;
rlm@1 128 if (*s == '!') { /* UTC? */
rlm@1 129 stm = gmtime(&t);
rlm@1 130 s++; /* skip `!' */
rlm@1 131 }
rlm@1 132 else
rlm@1 133 stm = localtime(&t);
rlm@1 134 if (stm == NULL) /* invalid date? */
rlm@1 135 lua_pushnil(L);
rlm@1 136 else if (strcmp(s, "*t") == 0) {
rlm@1 137 lua_createtable(L, 0, 9); /* 9 = number of fields */
rlm@1 138 setfield(L, "sec", stm->tm_sec);
rlm@1 139 setfield(L, "min", stm->tm_min);
rlm@1 140 setfield(L, "hour", stm->tm_hour);
rlm@1 141 setfield(L, "day", stm->tm_mday);
rlm@1 142 setfield(L, "month", stm->tm_mon+1);
rlm@1 143 setfield(L, "year", stm->tm_year+1900);
rlm@1 144 setfield(L, "wday", stm->tm_wday+1);
rlm@1 145 setfield(L, "yday", stm->tm_yday+1);
rlm@1 146 setboolfield(L, "isdst", stm->tm_isdst);
rlm@1 147 }
rlm@1 148 else {
rlm@1 149 char cc[3];
rlm@1 150 luaL_Buffer b;
rlm@1 151 cc[0] = '%'; cc[2] = '\0';
rlm@1 152 luaL_buffinit(L, &b);
rlm@1 153 for (; *s; s++) {
rlm@1 154 if (*s != '%' || *(s + 1) == '\0') /* no conversion specifier? */
rlm@1 155 luaL_addchar(&b, *s);
rlm@1 156 else {
rlm@1 157 size_t reslen;
rlm@1 158 char buff[200]; /* should be big enough for any conversion result */
rlm@1 159 cc[1] = *(++s);
rlm@1 160 reslen = strftime(buff, sizeof(buff), cc, stm);
rlm@1 161 luaL_addlstring(&b, buff, reslen);
rlm@1 162 }
rlm@1 163 }
rlm@1 164 luaL_pushresult(&b);
rlm@1 165 }
rlm@1 166 return 1;
rlm@1 167 }
rlm@1 168
rlm@1 169
rlm@1 170 static int os_time (lua_State *L) {
rlm@1 171 time_t t;
rlm@1 172 if (lua_isnoneornil(L, 1)) /* called without args? */
rlm@1 173 t = time(NULL); /* get current time */
rlm@1 174 else {
rlm@1 175 struct tm ts;
rlm@1 176 luaL_checktype(L, 1, LUA_TTABLE);
rlm@1 177 lua_settop(L, 1); /* make sure table is at the top */
rlm@1 178 ts.tm_sec = getfield(L, "sec", 0);
rlm@1 179 ts.tm_min = getfield(L, "min", 0);
rlm@1 180 ts.tm_hour = getfield(L, "hour", 12);
rlm@1 181 ts.tm_mday = getfield(L, "day", -1);
rlm@1 182 ts.tm_mon = getfield(L, "month", -1) - 1;
rlm@1 183 ts.tm_year = getfield(L, "year", -1) - 1900;
rlm@1 184 ts.tm_isdst = getboolfield(L, "isdst");
rlm@1 185 t = mktime(&ts);
rlm@1 186 }
rlm@1 187 if (t == (time_t)(-1))
rlm@1 188 lua_pushnil(L);
rlm@1 189 else
rlm@1 190 lua_pushnumber(L, (lua_Number)t);
rlm@1 191 return 1;
rlm@1 192 }
rlm@1 193
rlm@1 194
rlm@1 195 static int os_difftime (lua_State *L) {
rlm@1 196 lua_pushnumber(L, difftime((time_t)(luaL_checknumber(L, 1)),
rlm@1 197 (time_t)(luaL_optnumber(L, 2, 0))));
rlm@1 198 return 1;
rlm@1 199 }
rlm@1 200
rlm@1 201 /* }====================================================== */
rlm@1 202
rlm@1 203
rlm@1 204 static int os_setlocale (lua_State *L) {
rlm@1 205 static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
rlm@1 206 LC_NUMERIC, LC_TIME};
rlm@1 207 static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
rlm@1 208 "numeric", "time", NULL};
rlm@1 209 const char *l = luaL_optstring(L, 1, NULL);
rlm@1 210 int op = luaL_checkoption(L, 2, "all", catnames);
rlm@1 211 lua_pushstring(L, setlocale(cat[op], l));
rlm@1 212 return 1;
rlm@1 213 }
rlm@1 214
rlm@1 215
rlm@1 216 static int os_exit (lua_State *L) {
rlm@1 217 exit(luaL_optint(L, 1, EXIT_SUCCESS));
rlm@1 218 }
rlm@1 219
rlm@1 220 static const luaL_Reg syslib[] = {
rlm@1 221 {"clock", os_clock},
rlm@1 222 {"date", os_date},
rlm@1 223 {"difftime", os_difftime},
rlm@1 224 {"execute", os_execute},
rlm@1 225 {"exit", os_exit},
rlm@1 226 {"getenv", os_getenv},
rlm@1 227 {"remove", os_remove},
rlm@1 228 {"rename", os_rename},
rlm@1 229 {"setlocale", os_setlocale},
rlm@1 230 {"time", os_time},
rlm@1 231 {"tmpname", os_tmpname},
rlm@1 232 {NULL, NULL}
rlm@1 233 };
rlm@1 234
rlm@1 235 /* }====================================================== */
rlm@1 236
rlm@1 237
rlm@1 238
rlm@1 239 LUALIB_API int luaopen_os (lua_State *L) {
rlm@1 240 luaL_register(L, LUA_OSLIBNAME, syslib);
rlm@1 241 return 1;
rlm@1 242 }
rlm@1 243