sq.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. //
  2. // sq with import
  3. //
  4. // This is a modified version of sq that includes the import command.
  5. // Aside from the inclusion of import no other changes have been made.
  6. //
  7. //
  8. // Copyright (c) 2009 Alberto Demichelis, Brandon Jones
  9. //
  10. // This software is provided 'as-is', without any express or implied
  11. // warranty. In no event will the authors be held liable for any damages
  12. // arising from the use of this software.
  13. //
  14. // Permission is granted to anyone to use this software for any purpose,
  15. // including commercial applications, and to alter it and redistribute it
  16. // freely, subject to the following restrictions:
  17. //
  18. // 1. The origin of this software must not be misrepresented; you must not
  19. // claim that you wrote the original software. If you use this software
  20. // in a product, an acknowledgment in the product documentation would be
  21. // appreciated but is not required.
  22. //
  23. // 2. Altered source versions must be plainly marked as such, and must not be
  24. // misrepresented as being the original software.
  25. //
  26. // 3. This notice may not be removed or altered from any source
  27. // distribution.
  28. //
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <stdarg.h>
  33. #if defined(_MSC_VER) && defined(_DEBUG)
  34. #include <crtdbg.h>
  35. #include <conio.h>
  36. #endif
  37. #include <squirrel.h>
  38. #include <sqstdblob.h>
  39. #include <sqstdsystem.h>
  40. #include <sqstdio.h>
  41. #include <sqstdmath.h>
  42. #include <sqstdstring.h>
  43. #include <sqstdaux.h>
  44. // IMPORT SPECIFIC
  45. #include <sqratimport.h>
  46. #ifdef SQUNICODE
  47. #define scfprintf fwprintf
  48. #define scfopen _wfopen
  49. #define scvprintf vwprintf
  50. #else
  51. #define scfprintf fprintf
  52. #define scfopen fopen
  53. #define scvprintf vprintf
  54. #endif
  55. void PrintVersionInfos();
  56. #if defined(_MSC_VER) && defined(_DEBUG)
  57. int MemAllocHook( int allocType, void *userData, size_t size, int blockType,
  58. long requestNumber, const unsigned char *filename, int lineNumber)
  59. {
  60. // if(requestNumber==585)_asm int 3;
  61. return 1;
  62. }
  63. #endif
  64. SQInteger quit(HSQUIRRELVM v)
  65. {
  66. int *done;
  67. sq_getuserpointer(v,-1,(SQUserPointer*)&done);
  68. *done=1;
  69. return 0;
  70. }
  71. void printfunc(HSQUIRRELVM v,const SQChar *s,...)
  72. {
  73. va_list vl;
  74. va_start(vl, s);
  75. scvprintf( s, vl);
  76. va_end(vl);
  77. }
  78. void PrintVersionInfos()
  79. {
  80. scfprintf(stdout,_SC("%s %s (%d bits)\n"),SQUIRREL_VERSION,SQUIRREL_COPYRIGHT,sizeof(SQInteger)*8);
  81. if(sizeof(SQFloat) != sizeof(float)) {
  82. scfprintf(stdout,_SC("[%d bits floats]\n"),sizeof(SQFloat)*8);
  83. }
  84. }
  85. void PrintUsage()
  86. {
  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. }
  96. #define _INTERACTIVE 0
  97. #define _DONE 2
  98. //<<FIXME>> this func is a mess
  99. int getargs(HSQUIRRELVM v,int argc, char* argv[])
  100. {
  101. int i;
  102. int compiles_only = 0;
  103. static SQChar temp[500];
  104. const SQChar *ret=NULL;
  105. char * output = NULL;
  106. int lineinfo=0;
  107. if(argc>1)
  108. {
  109. int arg=1,exitloop=0;
  110. while(arg < argc && !exitloop)
  111. {
  112. if(argv[arg][0]=='-')
  113. {
  114. switch(argv[arg][1])
  115. {
  116. case 'd': //DEBUG(debug infos)
  117. sq_enabledebuginfo(v,1);
  118. break;
  119. case 'c':
  120. compiles_only = 1;
  121. break;
  122. case 'o':
  123. if(arg < argc) {
  124. arg++;
  125. output = argv[arg];
  126. }
  127. break;
  128. case 'v':
  129. PrintVersionInfos();
  130. return _DONE;
  131. case 'h':
  132. PrintVersionInfos();
  133. PrintUsage();
  134. return _DONE;
  135. default:
  136. PrintVersionInfos();
  137. scprintf(_SC("unknown prameter '-%c'\n"),argv[arg][1]);
  138. PrintUsage();
  139. return _DONE;
  140. }
  141. }else break;
  142. arg++;
  143. }
  144. // src file
  145. if(arg<argc) {
  146. const SQChar *filename=NULL;
  147. #ifdef SQUNICODE
  148. mbstowcs(temp,argv[arg],strlen(argv[arg]));
  149. filename=temp;
  150. #else
  151. filename=argv[arg];
  152. #endif
  153. arg++;
  154. sq_pushroottable(v);
  155. sq_pushstring(v,_SC("ARGS"),-1);
  156. sq_newarray(v,0);
  157. for(i=arg;i<argc;i++)
  158. {
  159. const SQChar *a;
  160. #ifdef SQUNICODE
  161. int alen=(int)strlen(argv[i]);
  162. a=sq_getscratchpad(v,(int)(alen*sizeof(SQChar)));
  163. mbstowcs(sq_getscratchpad(v,-1),argv[i],alen);
  164. sq_getscratchpad(v,-1)[alen] = _SC('\0');
  165. #else
  166. a=argv[i];
  167. #endif
  168. sq_pushstring(v,a,-1);
  169. sq_arrayappend(v,-2);
  170. }
  171. sq_createslot(v,-3);
  172. sq_pop(v,1);
  173. if(compiles_only) {
  174. if(SQ_SUCCEEDED(sqstd_loadfile(v,filename,SQTrue))){
  175. SQChar *outfile = _SC("out.cnut");
  176. if(output) {
  177. #ifdef SQUNICODE
  178. int len = (int)(strlen(output)+1);
  179. mbstowcs(sq_getscratchpad(v,len*sizeof(SQChar)),output,len);
  180. outfile = sq_getscratchpad(v,-1);
  181. #else
  182. outfile = output;
  183. #endif
  184. }
  185. if(SQ_SUCCEEDED(sqstd_writeclosuretofile(v,outfile)))
  186. return _DONE;
  187. }
  188. }
  189. else {
  190. if(SQ_SUCCEEDED(sqstd_dofile(v,filename,SQFalse,SQTrue))) {
  191. return _DONE;
  192. }
  193. }
  194. //if this point is reached an error occured
  195. {
  196. const SQChar *err;
  197. sq_getlasterror(v);
  198. if(SQ_SUCCEEDED(sq_getstring(v,-1,&err))) {
  199. scprintf(_SC("Error [%s]\n"),err);
  200. return _DONE;
  201. }
  202. }
  203. }
  204. }
  205. return _INTERACTIVE;
  206. }
  207. void Interactive(HSQUIRRELVM v)
  208. {
  209. #define MAXINPUT 1024
  210. SQChar buffer[MAXINPUT];
  211. SQInteger blocks =0;
  212. SQInteger string=0;
  213. SQInteger retval=0;
  214. SQInteger done=0;
  215. PrintVersionInfos();
  216. sq_pushroottable(v);
  217. sq_pushstring(v,_SC("quit"),-1);
  218. sq_pushuserpointer(v,&done);
  219. sq_newclosure(v,quit,1);
  220. sq_setparamscheck(v,1,NULL);
  221. sq_createslot(v,-3);
  222. sq_pop(v,1);
  223. while (!done)
  224. {
  225. SQInteger i = 0;
  226. scprintf(_SC("\nsq>"));
  227. for(;;) {
  228. int c;
  229. if(done)return;
  230. c = getchar();
  231. if (c == _SC('\n')) {
  232. if (i>0 && buffer[i-1] == _SC('\\'))
  233. {
  234. buffer[i-1] = _SC('\n');
  235. }
  236. else if(blocks==0)break;
  237. buffer[i++] = _SC('\n');
  238. }
  239. else if (c==_SC('}')) {blocks--; buffer[i++] = (SQChar)c;}
  240. else if(c==_SC('{') && !string){
  241. blocks++;
  242. buffer[i++] = (SQChar)c;
  243. }
  244. else if(c==_SC('"') || c==_SC('\'')){
  245. string=!string;
  246. buffer[i++] = (SQChar)c;
  247. }
  248. else if (i >= MAXINPUT-1) {
  249. scfprintf(stderr, _SC("sq : input line too long\n"));
  250. break;
  251. }
  252. else{
  253. buffer[i++] = (SQChar)c;
  254. }
  255. }
  256. buffer[i] = _SC('\0');
  257. if(buffer[0]==_SC('=')){
  258. scsprintf(sq_getscratchpad(v,MAXINPUT),_SC("return (%s)"),&buffer[1]);
  259. memcpy(buffer,sq_getscratchpad(v,-1),(scstrlen(sq_getscratchpad(v,-1))+1)*sizeof(SQChar));
  260. retval=1;
  261. }
  262. i=scstrlen(buffer);
  263. if(i>0){
  264. SQInteger oldtop=sq_gettop(v);
  265. if(SQ_SUCCEEDED(sq_compilebuffer(v,buffer,i,_SC("interactive console"),SQTrue))){
  266. sq_pushroottable(v);
  267. if(SQ_SUCCEEDED(sq_call(v,1,retval,SQTrue)) && retval){
  268. scprintf(_SC("\n"));
  269. sq_pushroottable(v);
  270. sq_pushstring(v,_SC("print"),-1);
  271. sq_get(v,-2);
  272. sq_pushroottable(v);
  273. sq_push(v,-4);
  274. sq_call(v,2,SQFalse,SQTrue);
  275. retval=0;
  276. scprintf(_SC("\n"));
  277. }
  278. }
  279. sq_settop(v,oldtop);
  280. }
  281. }
  282. }
  283. int main(int argc, char* argv[])
  284. {
  285. HSQUIRRELVM v;
  286. const SQChar *filename=NULL;
  287. #if defined(_MSC_VER) && defined(_DEBUG)
  288. _CrtSetAllocHook(MemAllocHook);
  289. #endif
  290. v=sq_open(1024);
  291. #if SQUIRREL_VERSION_NUMBER >= 300
  292. sq_setprintfunc(v,printfunc,printfunc);
  293. #else
  294. sq_setprintfunc(v,printfunc);
  295. #endif
  296. sq_pushroottable(v);
  297. sqstd_register_bloblib(v);
  298. sqstd_register_iolib(v);
  299. sqstd_register_systemlib(v);
  300. sqstd_register_mathlib(v);
  301. sqstd_register_stringlib(v);
  302. // IMPORT SPECIFIC
  303. sqrat_register_importlib(v);
  304. //aux library
  305. //sets error handlers
  306. sqstd_seterrorhandlers(v);
  307. //gets arguments
  308. switch(getargs(v,argc,argv))
  309. {
  310. case _INTERACTIVE:
  311. Interactive(v);
  312. break;
  313. case _DONE:
  314. default:
  315. break;
  316. }
  317. sq_close(v);
  318. #if defined(_MSC_VER) && defined(_DEBUG)
  319. _getch();
  320. _CrtMemDumpAllObjectsSince( NULL );
  321. #endif
  322. return 0;
  323. }