sqstdsystem.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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 <signal.h>
  9. #include <sqstdsystem.h>
  10. #ifdef _WIN32
  11. #include <windows.h>
  12. #endif
  13. #ifdef SQUNICODE
  14. #include <wchar.h>
  15. #define scgetenv _wgetenv
  16. #define scsystem _wsystem
  17. #define scasctime _wasctime
  18. #define scstrftime _wstrftime
  19. #define scremove _wremove
  20. #define screname _wrename
  21. #else
  22. #define scgetenv getenv
  23. #define scsystem system
  24. #define scasctime asctime
  25. #define scstrftime strftime
  26. #define scremove remove
  27. #define screname rename
  28. #endif
  29. SQ_OPT_STRING_STRLEN();
  30. static SQRESULT _system_getenv(HSQUIRRELVM v)
  31. {
  32. const SQChar *s;
  33. if(SQ_SUCCEEDED(sq_getstring(v,2,&s))){
  34. sq_pushstring(v,scgetenv(s),-1);
  35. return 1;
  36. }
  37. return 0;
  38. }
  39. static SQRESULT _system_system(HSQUIRRELVM v)
  40. {
  41. const SQChar *s;
  42. if(SQ_SUCCEEDED(sq_getstring(v,2,&s))){
  43. sq_pushinteger(v,scsystem(s));
  44. return 1;
  45. }
  46. return sq_throwerror(v,_SC("wrong param"));
  47. }
  48. static SQRESULT _system_clock(HSQUIRRELVM v)
  49. {
  50. sq_pushfloat(v,((SQFloat)clock())/(SQFloat)CLOCKS_PER_SEC);
  51. return 1;
  52. }
  53. /*
  54. static SQRESULT _system_time(HSQUIRRELVM v)
  55. {
  56. time_t t;
  57. time(&t);
  58. sq_pushinteger(v,*((SQInteger *)&t));
  59. return 1;
  60. }
  61. */
  62. static SQRESULT get_int_field (HSQUIRRELVM v, const SQChar *key, int dflt) {
  63. SQInteger res;
  64. SQObjectType stype;
  65. sq_pushstring(v, key, -1);
  66. if(sq_get(v, -2) == SQ_OK){
  67. stype = sq_gettype(v, -1);
  68. if (stype & SQOBJECT_NUMERIC){
  69. sq_getinteger(v, -1, &res);
  70. sq_poptop(v);
  71. return res;
  72. }
  73. sq_poptop(v);
  74. }
  75. if(dflt < 0) return sq_throwerror(v, _SC("field %s is missing"), key);
  76. return dflt;
  77. }
  78. static SQRESULT _system_time(HSQUIRRELVM v) {
  79. time_t t;
  80. if (sq_gettop(v) == 1) /* called without args? */
  81. t = time(NULL); /* get current time */
  82. else {
  83. struct tm ts;
  84. if(sq_gettype(v, 2) != OT_TABLE)
  85. return sq_throwerror(v, _SC("table expected as parameter but got (%s)"), sq_gettypename(v, 2));
  86. sq_settop(v, 2); /* make sure table is at the top */
  87. SQInteger rc = get_int_field(v, "sec", 0);
  88. if(rc == SQ_ERROR) return rc;
  89. else ts.tm_sec = rc;
  90. rc = get_int_field(v, "min", 0);
  91. if(rc == SQ_ERROR) return rc;
  92. else ts.tm_min = rc;
  93. rc = get_int_field(v, "hour", 12);
  94. if(rc == SQ_ERROR) return rc;
  95. else ts.tm_hour = rc;
  96. rc = get_int_field(v, "day", -1);
  97. if(rc == SQ_ERROR) return rc;
  98. else ts.tm_mday = rc;
  99. rc = get_int_field(v, "month", -1);
  100. if(rc == SQ_ERROR) return rc;
  101. else ts.tm_mon = rc;
  102. rc = get_int_field(v, "year", -1);
  103. if(rc == SQ_ERROR) return rc;
  104. else ts.tm_year = rc - 1900;
  105. SQBool isdst = SQTrue;
  106. sq_pushstring(v, _SC("isdst"), -1);
  107. if(sq_get(v, 2) == SQ_OK){
  108. if(sq_getbool(v, -1, &isdst) == SQ_OK){
  109. ts.tm_isdst = isdst;
  110. }
  111. sq_poptop(v);
  112. }
  113. ts.tm_isdst = isdst;
  114. t = mktime(&ts);
  115. }
  116. if (t == (time_t)(-1))
  117. sq_pushnull(v);
  118. else sq_pushinteger(v,(SQInteger)t);
  119. return 1;
  120. }
  121. static SQRESULT _system_difftime (HSQUIRRELVM v) {
  122. SQ_FUNC_VARS(v);
  123. SQ_GET_FLOAT(v, 2, t1);
  124. SQ_OPT_FLOAT(v, 3, t2, 0);
  125. sq_pushfloat(v, difftime( (time_t)t1, (time_t)t2));
  126. return 1;
  127. }
  128. static SQRESULT _system_remove(HSQUIRRELVM v)
  129. {
  130. const SQChar *s;
  131. sq_getstring(v,2,&s);
  132. if(scremove(s)==-1)
  133. return sq_throwerror(v,_SC("remove() failed"));
  134. return 0;
  135. }
  136. static SQRESULT _system_rename(HSQUIRRELVM v)
  137. {
  138. const SQChar *oldn,*newn;
  139. sq_getstring(v,2,&oldn);
  140. sq_getstring(v,3,&newn);
  141. if(screname(oldn,newn)==-1)
  142. return sq_throwerror(v,_SC("rename() failed"));
  143. return 0;
  144. }
  145. static void _set_integer_slot(HSQUIRRELVM v,const SQChar *name,SQInteger val)
  146. {
  147. sq_pushstring(v,name,-1);
  148. sq_pushinteger(v,val);
  149. sq_rawset(v,-3);
  150. }
  151. static SQRESULT _system_date(HSQUIRRELVM v)
  152. {
  153. SQ_FUNC_VARS(v);
  154. SQ_OPT_STRING(v, 2, arg_format, _SC("%c"));
  155. SQ_OPT_FLOAT(v, 3, arg_time, time(NULL));
  156. time_t t = (time_t)arg_time;
  157. struct tm *stm;
  158. if (*arg_format == _SC('!')) { /* UTC? */
  159. stm = gmtime(&t);
  160. arg_format++; /* skip `!' */
  161. }
  162. else
  163. stm = localtime(&t);
  164. if (stm == NULL) /* invalid date? */
  165. sq_pushnull(v);
  166. else if (scstrcmp(arg_format, _SC("*t")) == 0) {
  167. sq_newtableex(v, 9); /* 9 = number of fields */
  168. _set_integer_slot(v, _SC("sec"), stm->tm_sec);
  169. _set_integer_slot(v, _SC("min"), stm->tm_min);
  170. _set_integer_slot(v, _SC("hour"), stm->tm_hour);
  171. _set_integer_slot(v, _SC("day"), stm->tm_mday);
  172. _set_integer_slot(v, _SC("month"), stm->tm_mon);
  173. _set_integer_slot(v, _SC("year"), stm->tm_year+1900);
  174. _set_integer_slot(v, _SC("wday"), stm->tm_wday);
  175. _set_integer_slot(v, _SC("yday"), stm->tm_yday);
  176. sq_pushliteral(v, _SC("isdst"));
  177. sq_pushbool(v, stm->tm_isdst);
  178. sq_rawset(v, -3);
  179. }
  180. else {
  181. SQChar cc[3];
  182. SQBlob b(0, BLOB_BUFSIZE);
  183. cc[0] = _SC('%'); cc[2] = _SC('\0');
  184. for (; *arg_format; arg_format++) {
  185. if (*arg_format != _SC('%') || *(arg_format + 1) == _SC('\0')) /* no conversion specifier? */
  186. b.WriteChar(*arg_format);
  187. else {
  188. size_t reslen;
  189. SQChar buff[200]; /* should be big enough for any conversion result */
  190. cc[1] = *(++arg_format);
  191. reslen = scstrftime(buff, sizeof(buff)/sizeof(SQChar), cc, stm);
  192. b.Write(buff, reslen);
  193. }
  194. }
  195. sq_pushstring(v, (const SQChar*)b.GetBuf(), b.Len());
  196. }
  197. return 1;
  198. }
  199. static SQRESULT _system_exit (HSQUIRRELVM v) {
  200. SQRESULT status = 0;
  201. SQ_FUNC_VARS(v);
  202. if(_top_ > 1){
  203. SQObjectType ptype = sq_gettype(v, 1);
  204. if (ptype == OT_BOOL){
  205. SQ_GET_BOOL(v,2, b);
  206. status = b ? EXIT_SUCCESS : EXIT_FAILURE;
  207. }
  208. else
  209. sq_getinteger(v, 2, &status);
  210. SQ_OPT_BOOL(v, 3, bclose, false);
  211. if (bclose) sq_close(v);
  212. }
  213. exit(status);
  214. }
  215. #if defined(SC_USE_MKSTEMP)
  216. #include <unistd.h>
  217. #define SQ_TMPNAMBUFSIZE 32
  218. #define sq_tmpnam(b,e) { \
  219. strcpy(b, "/tmp/lua_XXXXXX"); \
  220. e = mkstemp(b); \
  221. if (e != -1) close(e); \
  222. e = (e == -1); }
  223. #else
  224. #define SQ_TMPNAMBUFSIZE L_tmpnam
  225. #define sq_tmpnam(b,e) { e = (tmpnam(b) == NULL); }
  226. #endif
  227. static SQRESULT _system_tmpname (HSQUIRRELVM v) {
  228. char buff[SQ_TMPNAMBUFSIZE];
  229. int err;
  230. sq_tmpnam(buff, err);
  231. if (err)
  232. return sq_throwerror(v, "unable to generate a unique filename");
  233. sq_pushstring(v, buff, -1);
  234. return 1;
  235. }
  236. #include <locale.h>
  237. static SQRESULT _system_setlocale (HSQUIRRELVM v) {
  238. static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
  239. LC_NUMERIC, LC_TIME};
  240. static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
  241. "numeric", "time", NULL};
  242. SQ_FUNC_VARS(v);
  243. SQ_OPT_STRING(v, 2, l, NULL);
  244. SQ_OPT_STRING(v, 3, name, "all");
  245. for (int i=0; catnames[i]; i++)
  246. if (strcmp(catnames[i], name) == 0){
  247. sq_pushstring(v, setlocale(cat[i], l), -1);
  248. return 1;
  249. }
  250. return sq_throwerror(v, "invalid option %s for param %d", name, 3);
  251. }
  252. /*-------------------------------------------------------------------------*\
  253. * Sleep for n seconds.
  254. \*-------------------------------------------------------------------------*/
  255. static SQRESULT _system_sleep(HSQUIRRELVM v)
  256. {
  257. SQ_FUNC_VARS_NO_TOP(v);
  258. SQ_GET_FLOAT(v, 2, n);
  259. #ifdef _WIN32
  260. Sleep((int)(n*1000));
  261. #else
  262. struct timespec t, r;
  263. t.tv_sec = (int) n;
  264. n -= t.tv_sec;
  265. t.tv_nsec = (int) (n * 1000000000);
  266. if (t.tv_nsec >= 1000000000) t.tv_nsec = 999999999;
  267. while (nanosleep(&t, &r) != 0) {
  268. t.tv_sec = r.tv_sec;
  269. t.tv_nsec = r.tv_nsec;
  270. }
  271. #endif
  272. return 0;
  273. }
  274. #include <sys/timeb.h>
  275. int GetMilliCount()
  276. {
  277. // Something like GetTickCount but portable
  278. // It rolls over every ~ 12.1 days (0x100000/24/60/60)
  279. // Use GetMilliSpan to correct for rollover
  280. timeb tb;
  281. ftime( &tb );
  282. int nCount = tb.millitm + (tb.time & 0xfffff) * 1000;
  283. return nCount;
  284. }
  285. int GetMilliSpan( int nTimeStart )
  286. {
  287. int nSpan = GetMilliCount() - nTimeStart;
  288. if ( nSpan < 0 )
  289. nSpan += 0x100000 * 1000;
  290. return nSpan;
  291. }
  292. static SQRESULT _system_getmillicount (HSQUIRRELVM v) {
  293. sq_pushinteger(v, GetMilliCount());
  294. return 1;
  295. }
  296. static SQRESULT _system_getmillispan (HSQUIRRELVM v) {
  297. SQ_FUNC_VARS_NO_TOP(v);
  298. SQ_GET_INTEGER(v, 2, nTimeStart);
  299. sq_pushinteger(v, GetMilliSpan(nTimeStart));
  300. return 1;
  301. }
  302. #if 0
  303. struct sq_signal_list
  304. {
  305. char *name; /* name of the signal */
  306. int sig; /* the signal */
  307. };
  308. static const struct sq_signal_list sq_signals_list[] = {
  309. /* ANSI C signals */
  310. #ifdef SIGABRT
  311. {"SIGABRT", SIGABRT},
  312. #endif
  313. #ifdef SIGFPE
  314. {"SIGFPE", SIGFPE},
  315. #endif
  316. #ifdef SIGILL
  317. {"SIGILL", SIGILL},
  318. #endif
  319. #ifdef SIGINT
  320. {"SIGINT", SIGINT},
  321. #endif
  322. #ifdef SIGSEGV
  323. {"SIGSEGV", SIGSEGV},
  324. #endif
  325. #ifdef SIGTERM
  326. {"SIGTERM", SIGTERM},
  327. #endif
  328. /* posix signals */
  329. #ifdef SIGHUP
  330. {"SIGHUP", SIGHUP},
  331. #endif
  332. #ifdef SIGQUIT
  333. {"SIGQUIT", SIGQUIT},
  334. #endif
  335. #ifdef SIGTRAP
  336. {"SIGTRAP", SIGTRAP},
  337. #endif
  338. #ifdef SIGKILL
  339. {"SIGKILL", SIGKILL},
  340. #endif
  341. #ifdef SIGUSR1
  342. {"SIGUSR1", SIGUSR1},
  343. #endif
  344. #ifdef SIGUSR2
  345. {"SIGUSR2", SIGUSR2},
  346. #endif
  347. #ifdef SIGPIPE
  348. {"SIGPIPE", SIGPIPE},
  349. #endif
  350. #ifdef SIGALRM
  351. {"SIGALRM", SIGALRM},
  352. #endif
  353. #ifdef SIGCHLD
  354. {"SIGCHLD", SIGCHLD},
  355. #endif
  356. #ifdef SIGCONT
  357. {"SIGCONT", SIGCONT},
  358. #endif
  359. #ifdef SIGSTOP
  360. {"SIGSTOP", SIGSTOP},
  361. #endif
  362. #ifdef SIGTTIN
  363. {"SIGTTIN", SIGTTIN},
  364. #endif
  365. #ifdef SIGTTOU
  366. {"SIGTTOU", SIGTTOU},
  367. #endif
  368. /* some BSD signals */
  369. #ifdef SIGIOT
  370. {"SIGIOT", SIGIOT},
  371. #endif
  372. #ifdef SIGBUS
  373. {"SIGBUS", SIGBUS},
  374. #endif
  375. #ifdef SIGCLD
  376. {"SIGCLD", SIGCLD},
  377. #endif
  378. #ifdef SIGURG
  379. {"SIGURG", SIGURG},
  380. #endif
  381. #ifdef SIGXCPU
  382. {"SIGXCPU", SIGXCPU},
  383. #endif
  384. #ifdef SIGXFSZ
  385. {"SIGXFSZ", SIGXFSZ},
  386. #endif
  387. #ifdef SIGVTALRM
  388. {"SIGVTALRM", SIGVTALRM},
  389. #endif
  390. #ifdef SIGPROF
  391. {"SIGPROF", SIGPROF},
  392. #endif
  393. #ifdef SIGWINCH
  394. {"SIGWINCH", SIGWINCH},
  395. #endif
  396. #ifdef SIGPOLL
  397. {"SIGPOLL", SIGPOLL},
  398. #endif
  399. #ifdef SIGIO
  400. {"SIGIO", SIGIO},
  401. #endif
  402. /* add odd signals */
  403. #ifdef SIGSTKFLT
  404. {"SIGSTKFLT", SIGSTKFLT}, /* stack fault */
  405. #endif
  406. #ifdef SIGSYS
  407. {"SIGSYS", SIGSYS},
  408. #endif
  409. {NULL, 0}
  410. };
  411. static HSQUIRRELVM vsig = NULL;
  412. static void sq_sig_handle(int sig)
  413. {
  414. if(vsig){
  415. sq_pushliteral(vsig, _SC("sq_sig_handler"));
  416. if(sq_getonregistrytable(vsig) == SQ_OK){
  417. SQObjectType ptype = sq_gettype(v, -1);
  418. if(ptype == OT_CLOSURE || ptype == OT_NATIVECLOSURE){
  419. sq_pushroottable(v);
  420. sq_pushinteger(v, sig);
  421. sq_call(v, 2, SQFalse, SQFalse);
  422. }
  423. }
  424. }
  425. }
  426. /*
  427. * _system_raise == raise(signal)
  428. *
  429. * signal = signal number or string
  430. */
  431. static int _system_raise(HSQUIRRELVM v)
  432. {
  433. SQ_FUNC_VARS(v);
  434. switch(sq_gettype(v, 2)){
  435. case OT_INTEGER:{
  436. SQ_GET_INTEGER(v, 2, iparam);
  437. sq_pushinteger(v, raise(iparam));
  438. }
  439. break;
  440. case OT_STRING:{
  441. int sig = 0;
  442. SQ_GET_STRING(v, 2, signame);
  443. for(int i=0, len = sizeof(sq_signals_list)/sizeof(sq_signal_list); i < len; ++i){
  444. if(strcmp(sq_signals_list[i].name, signame) == 0){
  445. sig = sq_signals_list[i].sig;
  446. break;
  447. }
  448. }
  449. if(sig) sq_pushinteger(v, raise(sig));
  450. else return sq_throwerror(v, _SC("invalid signal (%s)"), signame);
  451. }
  452. break;
  453. default:
  454. return sq_throwerror(v, _SC("invalid paramter (%s)"), sq_gettypename(v, 2));
  455. }
  456. return 1;
  457. }
  458. #endif
  459. #define _DECL_FUNC(name,nparams,pmask) {_SC(#name),_system_##name,nparams,pmask}
  460. static SQRegFunction systemlib_funcs[]={
  461. _DECL_FUNC(getenv,2,_SC(".s")),
  462. _DECL_FUNC(system,2,_SC(".s")),
  463. _DECL_FUNC(clock,0,NULL),
  464. _DECL_FUNC(time,-1,_SC(".t")),
  465. _DECL_FUNC(difftime,-2,_SC(".nn")),
  466. _DECL_FUNC(date,-1,_SC(".sn")),
  467. _DECL_FUNC(remove,2,_SC(".s")),
  468. _DECL_FUNC(rename,3,_SC(".ss")),
  469. _DECL_FUNC(exit, -1,_SC(". b|i b")),
  470. _DECL_FUNC(sleep, 2,_SC(".n")),
  471. _DECL_FUNC(tmpname,1,_SC(".")),
  472. _DECL_FUNC(setlocale,-1,_SC(".ss")),
  473. _DECL_FUNC(getmillicount,1,_SC(".")),
  474. _DECL_FUNC(getmillispan,2,_SC(".i")),
  475. {0,0}
  476. };
  477. #undef _DECL_FUNC
  478. SQInteger sqstd_register_systemlib(HSQUIRRELVM v)
  479. {
  480. sq_pushstring(v,_SC("os"),-1);
  481. sq_newtable(v);
  482. SQInteger i=0;
  483. while(systemlib_funcs[i].name!=0)
  484. {
  485. sq_pushstring(v,systemlib_funcs[i].name,-1);
  486. sq_newclosure(v,systemlib_funcs[i].f,0);
  487. sq_setparamscheck(v,systemlib_funcs[i].nparamscheck,systemlib_funcs[i].typemask);
  488. sq_setnativeclosurename(v,-1,systemlib_funcs[i].name);
  489. sq_newslot(v,-3,SQFalse);
  490. i++;
  491. }
  492. sq_newslot(v,-3,SQTrue); //insert os
  493. return 1;
  494. }