sq.c 7.4 KB

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