sq.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /* see copyright notice in squirrel.h */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdarg.h>
  6. #if defined(_MSC_VER) && defined(_DEBUG)
  7. #include <crtdbg.h>
  8. #include <conio.h>
  9. #elif defined(_TINSPIRE)
  10. #include <nspireio/nspireio.h>
  11. #endif
  12. #include <squirrel.h>
  13. #include <sqstdblob.h>
  14. #include <sqstdsystem.h>
  15. #include <sqstdio.h>
  16. #include <sqstdmath.h>
  17. #include <sqstdstring.h>
  18. #include <sqstdaux.h>
  19. #ifdef SQUNICODE
  20. #define scfprintf fwprintf
  21. #define scvprintf vfwprintf
  22. #elif defined(_TINSPIRE)
  23. #define scfprintf nio_fprintf
  24. #define scvprintf nio_fprintf
  25. #else
  26. #define scfprintf fprintf
  27. #define scvprintf vfprintf
  28. #endif
  29. void PrintVersionInfos();
  30. #if defined(_MSC_VER) && defined(_DEBUG)
  31. int MemAllocHook( int allocType, void *userData, size_t size, int blockType,
  32. long requestNumber, const unsigned char *filename, int lineNumber)
  33. {
  34. //if(requestNumber==769)_asm int 3;
  35. return 1;
  36. }
  37. #endif
  38. #if defined(_TINSPIRE)
  39. nio_console *csl;
  40. #endif
  41. SQInteger quit(HSQUIRRELVM v)
  42. {
  43. int *done;
  44. sq_getuserpointer(v,-1,(SQUserPointer*)&done);
  45. *done=1;
  46. return 0;
  47. }
  48. void printfunc(HSQUIRRELVM SQ_UNUSED_ARG(v),const SQChar *s,...)
  49. {
  50. va_list vl;
  51. va_start(vl, s);
  52. #if defined(_TINSPIRE)
  53. char buffer[1000];
  54. vsprintf(buffer, s, vl);
  55. scprintf(buffer);
  56. #else
  57. scvprintf(stdout, s, vl);
  58. #endif
  59. va_end(vl);
  60. (void)v; /* UNUSED */
  61. }
  62. void errorfunc(HSQUIRRELVM SQ_UNUSED_ARG(v),const SQChar *s,...)
  63. {
  64. va_list vl;
  65. va_start(vl, s);
  66. #if defined(_TINSPIRE)
  67. char buffer[1000];
  68. vsprintf(buffer, s, vl);
  69. scprintf(buffer);
  70. #else
  71. scvprintf(stderr, s, vl);
  72. #endif
  73. va_end(vl);
  74. }
  75. void PrintVersionInfos()
  76. {
  77. #ifdef _TINSPIRE
  78. scfprintf(nio_get_default(),_SC("%s %s (%d bits)\n"),SQUIRREL_VERSION,SQUIRREL_COPYRIGHT,((int)(sizeof(SQInteger)*8)));
  79. #else
  80. scfprintf(stdout,_SC("%s %s (%d bits)\n"),SQUIRREL_VERSION,SQUIRREL_COPYRIGHT,((int)(sizeof(SQInteger)*8)));
  81. #endif
  82. }
  83. void PrintUsage()
  84. {
  85. // One can't call an Ndless program with arguments IIRC
  86. #ifndef _TINSPIRE
  87. scfprintf(stderr,_SC("usage: sq <options> <scriptpath [args]>.\n")
  88. _SC("Available options are:\n")
  89. _SC(" -c compiles the file to bytecode(default output 'out.cnut')\n")
  90. _SC(" -o specifies output file for the -c option\n")
  91. _SC(" -c compiles only\n")
  92. _SC(" -d generates debug infos\n")
  93. _SC(" -v displays version infos\n")
  94. _SC(" -h prints help\n"));
  95. #endif
  96. }
  97. #define _INTERACTIVE 0
  98. #define _DONE 2
  99. #define _ERROR 3
  100. //<<FIXME>> this func is a mess
  101. int getargs(HSQUIRRELVM v,int argc, char* argv[],SQInteger *retval)
  102. {
  103. int i;
  104. int compiles_only = 0;
  105. #ifdef SQUNICODE
  106. static SQChar temp[500];
  107. #endif
  108. char * output = NULL;
  109. *retval = 0;
  110. if(argc>1)
  111. {
  112. int arg=1,exitloop=0;
  113. while(arg < argc && !exitloop)
  114. {
  115. if(argv[arg][0]=='-')
  116. {
  117. switch(argv[arg][1])
  118. {
  119. case 'd': //DEBUG(debug infos)
  120. sq_enabledebuginfo(v,1);
  121. break;
  122. case 'c':
  123. compiles_only = 1;
  124. break;
  125. case 'o':
  126. if(arg < argc) {
  127. arg++;
  128. output = argv[arg];
  129. }
  130. break;
  131. case 'v':
  132. PrintVersionInfos();
  133. return _DONE;
  134. case 'h':
  135. PrintVersionInfos();
  136. PrintUsage();
  137. return _DONE;
  138. default:
  139. PrintVersionInfos();
  140. scprintf(_SC("unknown prameter '-%c'\n"),argv[arg][1]);
  141. PrintUsage();
  142. *retval = -1;
  143. return _ERROR;
  144. }
  145. }else break;
  146. arg++;
  147. }
  148. // src file
  149. if(arg<argc) {
  150. const SQChar *filename=NULL;
  151. #ifdef SQUNICODE
  152. mbstowcs(temp,argv[arg],strlen(argv[arg]));
  153. filename=temp;
  154. #else
  155. filename=argv[arg];
  156. #endif
  157. arg++;
  158. //sq_pushstring(v,_SC("ARGS"),-1);
  159. //sq_newarray(v,0);
  160. //sq_createslot(v,-3);
  161. //sq_pop(v,1);
  162. if(compiles_only) {
  163. if(SQ_SUCCEEDED(sqstd_loadfile(v,filename,SQTrue))){
  164. const SQChar *outfile = _SC("out.cnut");
  165. if(output) {
  166. #ifdef SQUNICODE
  167. int len = (int)(strlen(output)+1);
  168. mbstowcs(sq_getscratchpad(v,len*sizeof(SQChar)),output,len);
  169. outfile = sq_getscratchpad(v,-1);
  170. #else
  171. outfile = output;
  172. #endif
  173. }
  174. if(SQ_SUCCEEDED(sqstd_writeclosuretofile(v,outfile)))
  175. return _DONE;
  176. }
  177. }
  178. else {
  179. //if(SQ_SUCCEEDED(sqstd_dofile(v,filename,SQFalse,SQTrue))) {
  180. //return _DONE;
  181. //}
  182. if(SQ_SUCCEEDED(sqstd_loadfile(v,filename,SQTrue))) {
  183. int callargs = 1;
  184. sq_pushroottable(v);
  185. for(i=arg;i<argc;i++)
  186. {
  187. const SQChar *a;
  188. #ifdef SQUNICODE
  189. int alen=(int)strlen(argv[i]);
  190. a=sq_getscratchpad(v,(int)(alen*sizeof(SQChar)));
  191. mbstowcs(sq_getscratchpad(v,-1),argv[i],alen);
  192. sq_getscratchpad(v,-1)[alen] = _SC('\0');
  193. #else
  194. a=argv[i];
  195. #endif
  196. sq_pushstring(v,a,-1);
  197. callargs++;
  198. //sq_arrayappend(v,-2);
  199. }
  200. if(SQ_SUCCEEDED(sq_call(v,callargs,SQTrue,SQTrue))) {
  201. SQObjectType type = sq_gettype(v,-1);
  202. if(type == OT_INTEGER) {
  203. *retval = type;
  204. sq_getinteger(v,-1,retval);
  205. }
  206. return _DONE;
  207. }
  208. else{
  209. return _ERROR;
  210. }
  211. }
  212. }
  213. //if this point is reached an error occured
  214. {
  215. const SQChar *err;
  216. sq_getlasterror(v);
  217. if(SQ_SUCCEEDED(sq_getstring(v,-1,&err))) {
  218. scprintf(_SC("Error [%s]\n"),err);
  219. *retval = -2;
  220. return _ERROR;
  221. }
  222. }
  223. }
  224. }
  225. return _INTERACTIVE;
  226. }
  227. void Interactive(HSQUIRRELVM v)
  228. {
  229. #define MAXINPUT 1024
  230. SQChar buffer[MAXINPUT];
  231. SQInteger blocks =0;
  232. SQInteger string=0;
  233. SQInteger retval=0;
  234. SQInteger done=0;
  235. PrintVersionInfos();
  236. sq_pushroottable(v);
  237. sq_pushstring(v,_SC("quit"),-1);
  238. sq_pushuserpointer(v,&done);
  239. sq_newclosure(v,quit,1);
  240. sq_setparamscheck(v,1,NULL);
  241. sq_newslot(v,-3,SQFalse);
  242. sq_pop(v,1);
  243. while (!done)
  244. {
  245. SQInteger i = 0;
  246. scprintf(_SC("\nsq>"));
  247. for(;;) {
  248. int c;
  249. if(done)return;
  250. #ifdef _TINSPIRE
  251. c = nio_getchar();
  252. #else
  253. c = getchar();
  254. #endif
  255. if (c == _SC('\n') || c == 0/*nspireio*/) {
  256. if (i>0 && buffer[i-1] == _SC('\\'))
  257. {
  258. buffer[i-1] = _SC('\n');
  259. }
  260. else if(blocks==0)break;
  261. buffer[i++] = _SC('\n');
  262. }
  263. else if (c==_SC('}')) {blocks--; buffer[i++] = (SQChar)c;}
  264. else if(c==_SC('{') && !string){
  265. blocks++;
  266. buffer[i++] = (SQChar)c;
  267. }
  268. else if(c==_SC('"') || c==_SC('\'')){
  269. string=!string;
  270. buffer[i++] = (SQChar)c;
  271. }
  272. else if (i >= MAXINPUT-1) {
  273. #ifdef _TINSPIRE
  274. scprintf(_SC("sq : input line too long\n"));
  275. #else
  276. scfprintf(stderr, _SC("sq : input line too long\n"));
  277. #endif
  278. break;
  279. }
  280. else{
  281. buffer[i++] = (SQChar)c;
  282. }
  283. }
  284. buffer[i] = _SC('\0');
  285. if(buffer[0]==_SC('=')){
  286. scsprintf(sq_getscratchpad(v,MAXINPUT),(size_t)MAXINPUT,_SC("return (%s)"),&buffer[1]);
  287. memcpy(buffer,sq_getscratchpad(v,-1),(scstrlen(sq_getscratchpad(v,-1))+1)*sizeof(SQChar));
  288. retval=1;
  289. }
  290. i=scstrlen(buffer);
  291. if(i>0){
  292. SQInteger oldtop=sq_gettop(v);
  293. if(SQ_SUCCEEDED(sq_compilebuffer(v,buffer,i,_SC("interactive console"),SQTrue))){
  294. sq_pushroottable(v);
  295. if(SQ_SUCCEEDED(sq_call(v,1,retval,SQTrue)) && retval){
  296. scprintf(_SC("\n"));
  297. sq_pushroottable(v);
  298. sq_pushstring(v,_SC("print"),-1);
  299. sq_get(v,-2);
  300. sq_pushroottable(v);
  301. sq_push(v,-4);
  302. sq_call(v,2,SQFalse,SQTrue);
  303. retval=0;
  304. scprintf(_SC("\n"));
  305. }
  306. }
  307. sq_settop(v,oldtop);
  308. }
  309. }
  310. }
  311. int main(int argc, char* argv[])
  312. {
  313. HSQUIRRELVM v;
  314. SQInteger retval = 0;
  315. #if defined(_TINSPIRE)
  316. nio_init(csl, NIO_MAX_COLS, NIO_MAX_ROWS, 0, 0, NIO_COLOR_BLACK, NIO_COLOR_WHITE, TRUE);
  317. #endif
  318. #if defined(_MSC_VER) && defined(_DEBUG)
  319. _CrtSetAllocHook(MemAllocHook);
  320. #endif
  321. v=sq_open(1024);
  322. sq_setprintfunc(v,printfunc,errorfunc);
  323. sq_pushroottable(v);
  324. sqstd_register_bloblib(v);
  325. sqstd_register_iolib(v);
  326. sqstd_register_systemlib(v);
  327. sqstd_register_mathlib(v);
  328. sqstd_register_stringlib(v);
  329. register_n2dlib(v);
  330. //aux library
  331. //sets error handlers
  332. sqstd_seterrorhandlers(v);
  333. //gets arguments
  334. switch(getargs(v,argc,argv,&retval))
  335. {
  336. case _INTERACTIVE:
  337. Interactive(v);
  338. break;
  339. case _DONE:
  340. case _ERROR:
  341. default:
  342. break;
  343. }
  344. sq_close(v);
  345. #if defined(_MSC_VER) && defined(_DEBUG)
  346. _getch();
  347. _CrtMemDumpAllObjectsSince( NULL );
  348. #endif
  349. #if defined(_TINSPIRE)
  350. nio_printf("\n---\nPress a key to exit.");
  351. nio_getch(nio_get_default());
  352. nio_free(csl);
  353. #endif
  354. return retval;
  355. }