Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

324 lines
6.5 KiB

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