sqstdsystem.cpp 13 KB

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