sqstdsystem.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* see copyright notice in squirrel.h */
  2. #include <squirrel.h>
  3. #include <string.h>
  4. #include "sqstdblobimpl.h"
  5. #include <time.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <sqstdsystem.h>
  9. #ifdef _WIN32
  10. #include <windows.h>
  11. #endif
  12. #ifdef SQUNICODE
  13. #include <wchar.h>
  14. #define scgetenv _wgetenv
  15. #define scsystem _wsystem
  16. #define scasctime _wasctime
  17. #define scremove _wremove
  18. #define screname _wrename
  19. #else
  20. #define scgetenv getenv
  21. #define scsystem system
  22. #define scasctime asctime
  23. #define scremove remove
  24. #define screname rename
  25. #endif
  26. SQ_OPT_STRING_STRLEN();
  27. static SQInteger _system_getenv(HSQUIRRELVM v)
  28. {
  29. const SQChar *s;
  30. if(SQ_SUCCEEDED(sq_getstring(v,2,&s))){
  31. sq_pushstring(v,scgetenv(s),-1);
  32. return 1;
  33. }
  34. return 0;
  35. }
  36. static SQInteger _system_system(HSQUIRRELVM v)
  37. {
  38. const SQChar *s;
  39. if(SQ_SUCCEEDED(sq_getstring(v,2,&s))){
  40. sq_pushinteger(v,scsystem(s));
  41. return 1;
  42. }
  43. return sq_throwerror(v,_SC("wrong param"));
  44. }
  45. static SQInteger _system_clock(HSQUIRRELVM v)
  46. {
  47. sq_pushfloat(v,((SQFloat)clock())/(SQFloat)CLOCKS_PER_SEC);
  48. return 1;
  49. }
  50. static SQInteger _system_time(HSQUIRRELVM v)
  51. {
  52. time_t t;
  53. time(&t);
  54. sq_pushinteger(v,*((SQInteger *)&t));
  55. return 1;
  56. }
  57. static SQRESULT _system_difftime (HSQUIRRELVM v) {
  58. SQ_FUNC_VARS(v);
  59. SQ_GET_FLOAT(v, 2, t1);
  60. SQ_OPT_FLOAT(v, 3, t2, 0);
  61. sq_pushfloat(v, difftime( (time_t)t1, (time_t)t2));
  62. return 1;
  63. }
  64. static SQInteger _system_remove(HSQUIRRELVM v)
  65. {
  66. const SQChar *s;
  67. sq_getstring(v,2,&s);
  68. if(scremove(s)==-1)
  69. return sq_throwerror(v,_SC("remove() failed"));
  70. return 0;
  71. }
  72. static SQInteger _system_rename(HSQUIRRELVM v)
  73. {
  74. const SQChar *oldn,*newn;
  75. sq_getstring(v,2,&oldn);
  76. sq_getstring(v,3,&newn);
  77. if(screname(oldn,newn)==-1)
  78. return sq_throwerror(v,_SC("rename() failed"));
  79. return 0;
  80. }
  81. static void _set_integer_slot(HSQUIRRELVM v,const SQChar *name,SQInteger val)
  82. {
  83. sq_pushstring(v,name,-1);
  84. sq_pushinteger(v,val);
  85. sq_rawset(v,-3);
  86. }
  87. static SQInteger _system_date(HSQUIRRELVM v)
  88. {
  89. SQ_FUNC_VARS(v);
  90. SQ_OPT_STRING(v, 2, arg_format, _SC("%c"));
  91. SQ_OPT_FLOAT(v, 3, arg_time, time(NULL));
  92. time_t t = (time_t)arg_time;
  93. struct tm *stm;
  94. if (*arg_format == _SC('!')) { /* UTC? */
  95. stm = gmtime(&t);
  96. arg_format++; /* skip `!' */
  97. }
  98. else
  99. stm = localtime(&t);
  100. if (stm == NULL) /* invalid date? */
  101. sq_pushnull(v);
  102. else if (scstrcmp(arg_format, _SC("*t")) == 0) {
  103. sq_newtable(v);
  104. _set_integer_slot(v, _SC("sec"), stm->tm_sec);
  105. _set_integer_slot(v, _SC("min"), stm->tm_min);
  106. _set_integer_slot(v, _SC("hour"), stm->tm_hour);
  107. _set_integer_slot(v, _SC("day"), stm->tm_mday);
  108. _set_integer_slot(v, _SC("month"), stm->tm_mon+1);
  109. _set_integer_slot(v, _SC("year"), stm->tm_year+1900);
  110. _set_integer_slot(v, _SC("wday"), stm->tm_wday+1);
  111. _set_integer_slot(v, _SC("yday"), stm->tm_yday+1);
  112. sq_pushliteral(v, _SC("isdst"));
  113. sq_pushbool(v, stm->tm_isdst);
  114. sq_rawset(v, -3);
  115. }
  116. else {
  117. SQChar cc[3];
  118. SQBlob b(0, BLOB_BUFSIZE);
  119. cc[0] = _SC('%'); cc[2] = _SC('\0');
  120. for (; *arg_format; arg_format++) {
  121. if (*arg_format != _SC('%') || *(arg_format + 1) == _SC('\0')) /* no conversion specifier? */
  122. b.WriteChar(*arg_format);
  123. else {
  124. size_t reslen;
  125. char buff[200]; /* should be big enough for any conversion result */
  126. cc[1] = *(++arg_format);
  127. reslen = strftime(buff, sizeof(buff), cc, stm);
  128. b.Write(buff, reslen);
  129. }
  130. }
  131. sq_pushstring(v, (const SQChar*)b.GetBuf(), b.Len());
  132. }
  133. return 1;
  134. }
  135. static SQRESULT _system_exit (HSQUIRRELVM v) {
  136. SQRESULT status = 0;
  137. SQ_FUNC_VARS(v);
  138. if(_top_ > 1){
  139. SQObjectType ptype = sq_gettype(v, 1);
  140. if (ptype == OT_BOOL){
  141. SQ_GET_BOOL(v,2, b);
  142. status = b ? EXIT_SUCCESS : EXIT_FAILURE;
  143. }
  144. else
  145. sq_getinteger(v, 2, &status);
  146. SQ_OPT_BOOL(v, 3, bclose, false);
  147. if (bclose) sq_close(v);
  148. }
  149. exit(status);
  150. }
  151. #if defined(SC_USE_MKSTEMP)
  152. #include <unistd.h>
  153. #define SQ_TMPNAMBUFSIZE 32
  154. #define sq_tmpnam(b,e) { \
  155. strcpy(b, "/tmp/lua_XXXXXX"); \
  156. e = mkstemp(b); \
  157. if (e != -1) close(e); \
  158. e = (e == -1); }
  159. #else
  160. #define SQ_TMPNAMBUFSIZE L_tmpnam
  161. #define sq_tmpnam(b,e) { e = (tmpnam(b) == NULL); }
  162. #endif
  163. static SQRESULT _system_tmpname (HSQUIRRELVM v) {
  164. char buff[SQ_TMPNAMBUFSIZE];
  165. int err;
  166. sq_tmpnam(buff, err);
  167. if (err)
  168. return sq_throwerror(v, "unable to generate a unique filename");
  169. sq_pushstring(v, buff, -1);
  170. return 1;
  171. }
  172. #include <locale.h>
  173. static SQRESULT _system_setlocale (HSQUIRRELVM v) {
  174. static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
  175. LC_NUMERIC, LC_TIME};
  176. static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
  177. "numeric", "time", NULL};
  178. SQ_FUNC_VARS(v);
  179. SQ_OPT_STRING(v, 2, l, NULL);
  180. SQ_OPT_STRING(v, 3, name, "all");
  181. for (int i=0; catnames[i]; i++)
  182. if (strcmp(catnames[i], name) == 0){
  183. sq_pushstring(v, setlocale(cat[i], l), -1);
  184. return 1;
  185. }
  186. return sq_throwerror(v, "invalid option %s for param %d", name, 3);
  187. }
  188. /*-------------------------------------------------------------------------*\
  189. * Sleep for n seconds.
  190. \*-------------------------------------------------------------------------*/
  191. static SQRESULT _system_sleep(HSQUIRRELVM v)
  192. {
  193. SQ_FUNC_VARS_NO_TOP(v);
  194. SQ_GET_FLOAT(v, 2, n);
  195. #ifdef _WIN32
  196. Sleep((int)(n*1000));
  197. #else
  198. struct timespec t, r;
  199. t.tv_sec = (int) n;
  200. n -= t.tv_sec;
  201. t.tv_nsec = (int) (n * 1000000000);
  202. if (t.tv_nsec >= 1000000000) t.tv_nsec = 999999999;
  203. while (nanosleep(&t, &r) != 0) {
  204. t.tv_sec = r.tv_sec;
  205. t.tv_nsec = r.tv_nsec;
  206. }
  207. #endif
  208. return 0;
  209. }
  210. #define _DECL_FUNC(name,nparams,pmask) {_SC(#name),_system_##name,nparams,pmask}
  211. static SQRegFunction systemlib_funcs[]={
  212. _DECL_FUNC(getenv,2,_SC(".s")),
  213. _DECL_FUNC(system,2,_SC(".s")),
  214. _DECL_FUNC(clock,0,NULL),
  215. _DECL_FUNC(time,1,NULL),
  216. _DECL_FUNC(difftime,-2,_SC(".nn")),
  217. _DECL_FUNC(date,-1,_SC(".sn")),
  218. _DECL_FUNC(remove,2,_SC(".s")),
  219. _DECL_FUNC(rename,3,_SC(".ss")),
  220. _DECL_FUNC(exit, -1,_SC(". b|i b")),
  221. _DECL_FUNC(sleep, 2,_SC(".n")),
  222. _DECL_FUNC(tmpname,1,_SC(".")),
  223. _DECL_FUNC(setlocale,-1,_SC(".ss")),
  224. {0,0}
  225. };
  226. #undef _DECL_FUNC
  227. SQInteger sqstd_register_systemlib(HSQUIRRELVM v)
  228. {
  229. sq_pushstring(v,_SC("os"),-1);
  230. sq_newclass(v,SQFalse);
  231. SQInteger i=0;
  232. while(systemlib_funcs[i].name!=0)
  233. {
  234. sq_pushstring(v,systemlib_funcs[i].name,-1);
  235. sq_newclosure(v,systemlib_funcs[i].f,0);
  236. sq_setparamscheck(v,systemlib_funcs[i].nparamscheck,systemlib_funcs[i].typemask);
  237. sq_setnativeclosurename(v,-1,systemlib_funcs[i].name);
  238. sq_newslot(v,-3,SQFalse);
  239. i++;
  240. }
  241. sq_newslot(v,-3,SQTrue); //insert os
  242. return 1;
  243. }