sqstdsystem.cpp 13 KB

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