sqstdsystem.cpp 13 KB

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