sqstdsystem.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* see copyright notice in squirrel.h */
  2. #include <squirrel.h>
  3. #include <time.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <sqstdsystem.h>
  7. #if defined(__APPLE__) && !defined(IOS)
  8. #include <TargetConditionals.h>
  9. #if TARGET_OS_IPHONE
  10. #define IOS
  11. #endif
  12. #endif
  13. #ifdef SQUNICODE
  14. #include <wchar.h>
  15. #define scgetenv _wgetenv
  16. #define scsystem _wsystem
  17. #define scasctime _wasctime
  18. #define scremove _wremove
  19. #define screname _wrename
  20. #else
  21. #define scgetenv getenv
  22. #define scsystem system
  23. #define scasctime asctime
  24. #define scremove remove
  25. #define screname rename
  26. #endif
  27. #ifdef IOS
  28. #include <spawn.h>
  29. extern char **environ;
  30. #endif
  31. static SQInteger _system_getenv(HSQUIRRELVM v)
  32. {
  33. const SQChar *s;
  34. if(SQ_SUCCEEDED(sq_getstring(v,2,&s))){
  35. sq_pushstring(v,scgetenv(s),-1);
  36. return 1;
  37. }
  38. return 0;
  39. }
  40. static SQInteger _system_system(HSQUIRRELVM v)
  41. {
  42. const SQChar *s;
  43. if(SQ_SUCCEEDED(sq_getstring(v,2,&s))){
  44. #ifdef IOS
  45. pid_t pid;
  46. posix_spawn(&pid, s, NULL, NULL, NULL, environ);
  47. sq_pushinteger(v, 0);
  48. #else
  49. sq_pushinteger(v,scsystem(s));
  50. #endif
  51. return 1;
  52. }
  53. return sq_throwerror(v,_SC("wrong param"));
  54. }
  55. static SQInteger _system_clock(HSQUIRRELVM v)
  56. {
  57. sq_pushfloat(v,((SQFloat)clock())/(SQFloat)CLOCKS_PER_SEC);
  58. return 1;
  59. }
  60. static SQInteger _system_time(HSQUIRRELVM v)
  61. {
  62. SQInteger t = (SQInteger)time(NULL);
  63. sq_pushinteger(v,t);
  64. return 1;
  65. }
  66. static SQInteger _system_remove(HSQUIRRELVM v)
  67. {
  68. const SQChar *s;
  69. sq_getstring(v,2,&s);
  70. if(scremove(s)==-1)
  71. return sq_throwerror(v,_SC("remove() failed"));
  72. return 0;
  73. }
  74. static SQInteger _system_rename(HSQUIRRELVM v)
  75. {
  76. const SQChar *oldn,*newn;
  77. sq_getstring(v,2,&oldn);
  78. sq_getstring(v,3,&newn);
  79. if(screname(oldn,newn)==-1)
  80. return sq_throwerror(v,_SC("rename() failed"));
  81. return 0;
  82. }
  83. static void _set_integer_slot(HSQUIRRELVM v,const SQChar *name,SQInteger val)
  84. {
  85. sq_pushstring(v,name,-1);
  86. sq_pushinteger(v,val);
  87. sq_rawset(v,-3);
  88. }
  89. static SQInteger _system_date(HSQUIRRELVM v)
  90. {
  91. time_t t;
  92. SQInteger it;
  93. SQInteger format = 'l';
  94. if(sq_gettop(v) > 1) {
  95. sq_getinteger(v,2,&it);
  96. t = it;
  97. if(sq_gettop(v) > 2) {
  98. sq_getinteger(v,3,(SQInteger*)&format);
  99. }
  100. }
  101. else {
  102. time(&t);
  103. }
  104. tm *date;
  105. if(format == 'u')
  106. date = gmtime(&t);
  107. else
  108. date = localtime(&t);
  109. if(!date)
  110. return sq_throwerror(v,_SC("crt api failure"));
  111. sq_newtable(v);
  112. _set_integer_slot(v, _SC("sec"), date->tm_sec);
  113. _set_integer_slot(v, _SC("min"), date->tm_min);
  114. _set_integer_slot(v, _SC("hour"), date->tm_hour);
  115. _set_integer_slot(v, _SC("day"), date->tm_mday);
  116. _set_integer_slot(v, _SC("month"), date->tm_mon);
  117. _set_integer_slot(v, _SC("year"), date->tm_year+1900);
  118. _set_integer_slot(v, _SC("wday"), date->tm_wday);
  119. _set_integer_slot(v, _SC("yday"), date->tm_yday);
  120. return 1;
  121. }
  122. #define _DECL_FUNC(name,nparamsmin,nparamsmax,pmask) {_SC(#name),_system_##name,nparamsmin,nparamsmax,pmask}
  123. static const SQRegFunction systemlib_funcs[]={
  124. _DECL_FUNC(getenv,2,2,_SC(".s")),
  125. _DECL_FUNC(system,2,2,_SC(".s")),
  126. _DECL_FUNC(clock,0,0,NULL),
  127. _DECL_FUNC(time,1,1,NULL),
  128. _DECL_FUNC(date,1,0,_SC(".nn")),
  129. _DECL_FUNC(remove,2,2,_SC(".s")),
  130. _DECL_FUNC(rename,3,3,_SC(".ss")),
  131. {NULL,(SQFUNCTION)0,0,0,NULL}
  132. };
  133. #undef _DECL_FUNC
  134. SQInteger sqstd_register_systemlib(HSQUIRRELVM v)
  135. {
  136. SQInteger i=0;
  137. while(systemlib_funcs[i].name!=0)
  138. {
  139. sq_pushstring(v,systemlib_funcs[i].name,-1);
  140. sq_newclosure(v,systemlib_funcs[i].f,0);
  141. sq_setparamscheck(v,systemlib_funcs[i].nparamscheckmin,systemlib_funcs[i].nparamscheckmax,systemlib_funcs[i].typemask);
  142. sq_setnativeclosurename(v,-1,systemlib_funcs[i].name);
  143. sq_newslot(v,-3,SQFalse);
  144. i++;
  145. }
  146. return 1;
  147. }