A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 207 lines 5.2 kB view raw
1/* 2** $Id: loslib.c,v 1.19.1.3 2008/01/18 16:38:18 roberto Exp $ 3** Standard Operating System library 4** See Copyright Notice in lua.h 5*/ 6 7 8#include <errno.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 22static int os_pushresult (lua_State *L, int i, const char *filename) { 23 int en = errno; /* calls to Lua API may change this value */ 24 if (i) { 25 lua_pushboolean(L, 1); 26 return 1; 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 } 34} 35 36 37static int os_remove (lua_State *L) { 38 const char *filename = luaL_checkstring(L, 1); 39 return os_pushresult(L, rb->remove(filename) == 0, filename); 40} 41 42 43static int os_rename (lua_State *L) { 44 const char *fromname = luaL_checkstring(L, 1); 45 const char *toname = luaL_checkstring(L, 2); 46 return os_pushresult(L, rb->rename(fromname, toname) == 0, fromname); 47} 48 49 50/* 51** {====================================================== 52** Time/Date operations 53** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S, 54** wday=%w+1, yday=%j, isdst=? } 55** ======================================================= 56*/ 57 58static void setfield (lua_State *L, const char *key, int value) { 59 lua_pushinteger(L, value); 60 lua_setfield(L, -2, key); 61} 62 63static void setboolfield (lua_State *L, const char *key, int value) { 64 if (value < 0) /* undefined? */ 65 return; /* does not set field */ 66 lua_pushboolean(L, value); 67 lua_setfield(L, -2, key); 68} 69 70#if CONFIG_RTC 71static int getboolfield (lua_State *L, const char *key) { 72 int res; 73 lua_getfield(L, -1, key); 74 res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1); 75 lua_pop(L, 1); 76 return res; 77} 78 79 80static int getfield (lua_State *L, const char *key, int d) { 81 int res; 82 lua_getfield(L, -1, key); 83 if (lua_isnumber(L, -1)) 84 res = (int)lua_tointeger(L, -1); 85 else { 86 if (d < 0) 87 return luaL_error(L, "field " LUA_QS " missing in date table", key); 88 res = d; 89 } 90 lua_pop(L, 1); 91 return res; 92} 93#endif 94 95 96static int os_date (lua_State *L) { 97 const char *s = luaL_optstring(L, 1, "%c"); 98 time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, 99#if CONFIG_RTC 100 rb->mktime(rb->get_time()) 101#else 102 0 103#endif 104 ); 105 struct tm *stm; 106 if (*s == '!') /* UTC? */ /* Rockbox doesn't support timezones */ 107 s++; /* skip `!' */ 108 stm = gmtime(&t); 109 if (stm == NULL) /* invalid date? */ 110 lua_pushnil(L); 111 else if (strcmp(s, "*t") == 0) { 112 lua_createtable(L, 0, 9); /* 9 = number of fields */ 113 setfield(L, "sec", stm->tm_sec); 114 setfield(L, "min", stm->tm_min); 115 setfield(L, "hour", stm->tm_hour); 116 setfield(L, "day", stm->tm_mday); 117 setfield(L, "month", stm->tm_mon+1); 118 setfield(L, "year", stm->tm_year+1900); 119 setfield(L, "wday", stm->tm_wday+1); 120 setfield(L, "yday", stm->tm_yday+1); 121 setboolfield(L, "isdst", stm->tm_isdst); 122 } 123 else { 124 char cc[3]; 125 luaL_Buffer b; 126 cc[0] = '%'; cc[2] = '\0'; 127 luaL_buffinit(L, &b); 128 for (; *s; s++) { 129 if (*s != '%' || *(s + 1) == '\0') /* no conversion specifier? */ 130 luaL_addchar(&b, *s); 131 else { 132 size_t reslen; 133 char buff[200]; /* should be big enough for any conversion result */ 134 cc[1] = *(++s); 135 reslen = strftime(buff, sizeof(buff), cc, stm); 136 luaL_addlstring(&b, buff, reslen); 137 } 138 } 139 luaL_pushresult(&b); 140 } 141 return 1; 142} 143 144static int os_time (lua_State *L) { 145 time_t t = -1; 146#if CONFIG_RTC 147 if (lua_isnoneornil(L, 1)) /* called without args? */ 148 t = rb->mktime(rb->get_time()); /* get current time */ 149 else { 150 struct tm ts; 151 luaL_checktype(L, 1, LUA_TTABLE); 152 lua_settop(L, 1); /* make sure table is at the top */ 153 ts.tm_sec = getfield(L, "sec", 0); 154 ts.tm_min = getfield(L, "min", 0); 155 ts.tm_hour = getfield(L, "hour", 12); 156 ts.tm_mday = getfield(L, "day", -1); 157 ts.tm_mon = getfield(L, "month", -1) - 1; 158 ts.tm_year = getfield(L, "year", -1) - 1900; 159 ts.tm_isdst = getboolfield(L, "isdst"); 160 t = rb->mktime(&ts); 161 } 162#endif 163 if (t == (time_t)(-1)) 164 lua_pushnil(L); 165 else 166 lua_pushnumber(L, (lua_Number)t); 167 return 1; 168} 169 170 171/* }====================================================== */ 172 173 174static int os_exit (lua_State *L) { 175 lua_settop(L, 2); 176 int status = luaL_optint(L, 1, EXIT_SUCCESS); 177 if (status != EXIT_SUCCESS && lua_type (L, 2) != LUA_TSTRING) 178 lua_pushfstring(L, "exit (%d)", status); 179 lua_pushvalue(L, 1); /* put exit status on top of stack */ 180 exit(status); 181 return EXIT_SUCCESS; /* never reached, surpress warning */ 182} 183 184static const luaL_Reg syslib[] = { 185 //{"clock", os_clock}, 186 {"date", os_date}, 187 //{"difftime", os_difftime}, 188 //{"execute", os_execute}, 189 {"exit", os_exit}, 190 //{"getenv", os_getenv}, 191 {"remove", os_remove}, 192 {"rename", os_rename}, 193 //{"setlocale", os_setlocale}, 194 {"time", os_time}, 195 //{"tmpname", os_tmpname}, 196 {NULL, NULL} 197}; 198 199/* }====================================================== */ 200 201 202 203LUALIB_API int luaopen_os (lua_State *L) { 204 luaL_register(L, LUA_OSLIBNAME, syslib); 205 return 1; 206} 207