sqstdsystem.cpp 16 KB

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