sqstdsystem.cpp 16 KB

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