sq.c 10 KB

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