Team Fortress 2 Source Code as on 22/4/2020
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.

292 lines
6.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // SYS_SCRIPTLIB.CPP
  4. //
  5. //
  6. //=====================================================================================//
  7. #include "vxconsole.h"
  8. char g_sys_token[MAXTOKEN];
  9. char* g_sys_scriptbuff;
  10. char* g_sys_scriptptr;
  11. char* g_sys_scriptendptr;
  12. int g_sys_scriptsize;
  13. int g_sys_scriptline;
  14. bool g_sys_endofscript;
  15. bool g_sys_tokenready;
  16. int g_sys_oldscriptline;
  17. char* g_sys_oldscriptptr;
  18. //-----------------------------------------------------------------------------
  19. // Sys_FreeScriptFile
  20. //
  21. //-----------------------------------------------------------------------------
  22. void Sys_FreeScriptFile(void)
  23. {
  24. if (g_sys_scriptbuff)
  25. {
  26. Sys_Free(g_sys_scriptbuff);
  27. g_sys_scriptbuff = NULL;
  28. }
  29. }
  30. //-----------------------------------------------------------------------------
  31. // Sys_LoadScriptFile
  32. //
  33. //-----------------------------------------------------------------------------
  34. void Sys_LoadScriptFile(const char* filename)
  35. {
  36. g_sys_scriptsize = Sys_LoadFile(filename,(void **)&g_sys_scriptbuff);
  37. Sys_ResetParser();
  38. }
  39. //-----------------------------------------------------------------------------
  40. // Sys_SetScriptData
  41. //
  42. //-----------------------------------------------------------------------------
  43. void Sys_SetScriptData(const char* data, int length)
  44. {
  45. g_sys_scriptbuff = (char *)data;
  46. g_sys_scriptsize = length;
  47. Sys_ResetParser();
  48. }
  49. //-----------------------------------------------------------------------------
  50. // Sys_UnGetToken
  51. //
  52. //-----------------------------------------------------------------------------
  53. void Sys_UnGetToken(void)
  54. {
  55. g_sys_tokenready = true;
  56. }
  57. //-----------------------------------------------------------------------------
  58. // Sys_GetToken
  59. //
  60. //-----------------------------------------------------------------------------
  61. char* Sys_GetToken(bool crossline)
  62. {
  63. char* tokenptr;
  64. if (g_sys_tokenready)
  65. {
  66. g_sys_tokenready = false;
  67. return (g_sys_token);
  68. }
  69. g_sys_token[0] = '\0';
  70. if (g_sys_scriptptr >= g_sys_scriptendptr)
  71. {
  72. g_sys_endofscript = true;
  73. return (NULL);
  74. }
  75. skipspace:
  76. while (*g_sys_scriptptr <= ' ')
  77. {
  78. if (g_sys_scriptptr >= g_sys_scriptendptr)
  79. {
  80. g_sys_endofscript = true;
  81. return (NULL);
  82. }
  83. if (*g_sys_scriptptr++ == '\n')
  84. {
  85. if (!crossline)
  86. {
  87. // unexpected newline at g_sys_scriptline
  88. return (NULL);
  89. }
  90. g_sys_scriptline++;
  91. }
  92. }
  93. if (g_sys_scriptptr >= g_sys_scriptendptr)
  94. {
  95. g_sys_endofscript = true;
  96. return (NULL);
  97. }
  98. // skip commented line
  99. if ((g_sys_scriptptr[0] == ';') || (g_sys_scriptptr[0] == '/' && g_sys_scriptptr[1] == '/'))
  100. {
  101. if (!crossline)
  102. {
  103. // unexpected newline at g_sys_scriptline
  104. return (NULL);
  105. }
  106. while (*g_sys_scriptptr++ != '\n')
  107. {
  108. if (g_sys_scriptptr >= g_sys_scriptendptr)
  109. {
  110. g_sys_endofscript = true;
  111. return (NULL);
  112. }
  113. }
  114. g_sys_scriptline++;
  115. goto skipspace;
  116. }
  117. tokenptr = g_sys_token;
  118. if (g_sys_scriptptr[0] == '\"' && g_sys_scriptptr[1])
  119. {
  120. // copy quoted token
  121. do
  122. {
  123. *tokenptr++ = *g_sys_scriptptr++;
  124. if (g_sys_scriptptr == g_sys_scriptendptr)
  125. break;
  126. if (tokenptr == &g_sys_token[MAXTOKEN])
  127. {
  128. // token too large
  129. return NULL;
  130. }
  131. }
  132. while (*g_sys_scriptptr >= ' ' && *g_sys_scriptptr != '\"');
  133. if (g_sys_scriptptr[0] == '\"')
  134. *tokenptr++ = *g_sys_scriptptr++;
  135. }
  136. else
  137. {
  138. // copy token
  139. while (*g_sys_scriptptr > ' ' && *g_sys_scriptptr != ';' && *g_sys_scriptptr != '\"')
  140. {
  141. *tokenptr++ = *g_sys_scriptptr++;
  142. if (g_sys_scriptptr == g_sys_scriptendptr)
  143. break;
  144. if (tokenptr == &g_sys_token[MAXTOKEN])
  145. {
  146. // token too large
  147. return NULL;
  148. }
  149. }
  150. }
  151. *tokenptr = '\0';
  152. return (g_sys_token);
  153. }
  154. //-----------------------------------------------------------------------------
  155. // Sys_SkipRestOfLine
  156. //
  157. //-----------------------------------------------------------------------------
  158. void Sys_SkipRestOfLine(void)
  159. {
  160. while (*g_sys_scriptptr++ != '\n')
  161. {
  162. if (g_sys_scriptptr >= g_sys_scriptendptr)
  163. {
  164. break;
  165. }
  166. }
  167. g_sys_scriptline++;
  168. // flush any queued token
  169. g_sys_tokenready = false;
  170. }
  171. //-----------------------------------------------------------------------------
  172. // Sys_TokenAvailable
  173. //
  174. // Returns (TRUE) if token available on line.
  175. //-----------------------------------------------------------------------------
  176. bool Sys_TokenAvailable (void)
  177. {
  178. char* ptr;
  179. ptr = g_sys_scriptptr;
  180. while (*ptr <= ' ')
  181. {
  182. if (ptr >= g_sys_scriptendptr)
  183. {
  184. g_sys_endofscript = true;
  185. return (false);
  186. }
  187. if (*ptr++ == '\n')
  188. return (false);
  189. }
  190. return (true);
  191. }
  192. //-----------------------------------------------------------------------------
  193. // Sys_EndOfScript
  194. //
  195. // Returns (TRUE) at end of script
  196. //-----------------------------------------------------------------------------
  197. bool Sys_EndOfScript(void)
  198. {
  199. if (g_sys_scriptptr >= g_sys_scriptendptr)
  200. {
  201. g_sys_endofscript = true;
  202. return (true);
  203. }
  204. return (false);
  205. }
  206. //-----------------------------------------------------------------------------
  207. // Sys_ResetParser
  208. //
  209. //-----------------------------------------------------------------------------
  210. void Sys_ResetParser(void)
  211. {
  212. g_sys_scriptptr = g_sys_scriptbuff;
  213. g_sys_scriptendptr = g_sys_scriptptr + g_sys_scriptsize;
  214. g_sys_scriptline = 1;
  215. g_sys_endofscript = false;
  216. g_sys_tokenready = false;
  217. }
  218. //-----------------------------------------------------------------------------
  219. // Sys_SaveParser
  220. //
  221. //-----------------------------------------------------------------------------
  222. void Sys_SaveParser(void)
  223. {
  224. g_sys_oldscriptline = g_sys_scriptline;
  225. g_sys_oldscriptptr = g_sys_scriptptr;
  226. }
  227. //-----------------------------------------------------------------------------
  228. // Sys_RestoreParser
  229. //
  230. //-----------------------------------------------------------------------------
  231. void Sys_RestoreParser(void)
  232. {
  233. g_sys_scriptline = g_sys_oldscriptline;
  234. g_sys_scriptptr = g_sys_oldscriptptr;
  235. g_sys_tokenready = false;
  236. }
  237. //-----------------------------------------------------------------------------
  238. // Sys_StripQuotesFromToken
  239. //
  240. //-----------------------------------------------------------------------------
  241. void Sys_StripQuotesFromToken( char *pToken )
  242. {
  243. int len;
  244. len = strlen( pToken );
  245. if ( len >= 2 && pToken[0] == '\"' )
  246. {
  247. memcpy( pToken, pToken+1, len-1 );
  248. pToken[len-2] = '\0';
  249. }
  250. }