2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
\r
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
\r
5 This file is part of GtkRadiant.
\r
7 GtkRadiant is free software; you can redistribute it and/or modify
\r
8 it under the terms of the GNU General Public License as published by
\r
9 the Free Software Foundation; either version 2 of the License, or
\r
10 (at your option) any later version.
\r
12 GtkRadiant is distributed in the hope that it will be useful,
\r
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
15 GNU General Public License for more details.
\r
17 You should have received a copy of the GNU General Public License
\r
18 along with GtkRadiant; if not, write to the Free Software
\r
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
\r
26 #include "scriplib.h"
\r
29 =============================================================================
\r
33 =============================================================================
\r
38 char filename[1024];
\r
39 char *buffer,*script_p,*end_p;
\r
43 #define MAX_INCLUDES 8
\r
44 script_t scriptstack[MAX_INCLUDES];
\r
48 char token[MAXTOKEN];
\r
49 qboolean endofscript;
\r
50 qboolean tokenready; // only true if UnGetScriptToken was just called
\r
57 void AddScriptToStack (char *filename)
\r
62 if (script == &scriptstack[MAX_INCLUDES])
\r
63 Error ("script file exceeded MAX_INCLUDES");
\r
64 strcpy (script->filename, ExpandPath (filename) );
\r
66 size = LoadFile (script->filename, (void **)&script->buffer);
\r
68 printf ("entering %s\n", script->filename);
\r
72 script->script_p = script->buffer;
\r
73 script->end_p = script->buffer + size;
\r
82 void LoadScriptFile (char *filename)
\r
84 script = scriptstack;
\r
85 AddScriptToStack (filename);
\r
87 endofscript = false;
\r
97 void ParseFromMemory (char *buffer, int size)
\r
99 script = scriptstack;
\r
101 if (script == &scriptstack[MAX_INCLUDES])
\r
102 Error ("script file exceeded MAX_INCLUDES");
\r
103 strcpy (script->filename, "memory buffer" );
\r
105 script->buffer = buffer;
\r
107 script->script_p = script->buffer;
\r
108 script->end_p = script->buffer + size;
\r
110 endofscript = false;
\r
111 tokenready = false;
\r
119 Signals that the current token was not used, and should be reported
\r
120 for the next GetScriptToken. Note that
\r
122 GetScriptToken (true);
\r
123 UnGetScriptToken ();
\r
124 GetScriptToken (false);
\r
126 could cross a line boundary.
\r
129 void UnGetScriptToken (void)
\r
135 qboolean EndOfScript (qboolean crossline)
\r
138 Error ("Line %i is incomplete\n",scriptline);
\r
140 if (!strcmp (script->filename, "memory buffer"))
\r
142 endofscript = true;
\r
146 free (script->buffer);
\r
147 if (script == scriptstack+1)
\r
149 endofscript = true;
\r
153 scriptline = script->line;
\r
154 printf ("returning to %s\n", script->filename);
\r
155 return GetScriptToken (crossline);
\r
163 qboolean GetScriptToken (qboolean crossline)
\r
167 if (tokenready) // is a token allready waiting?
\r
169 tokenready = false;
\r
173 if (script->script_p >= script->end_p)
\r
174 return EndOfScript (crossline);
\r
180 while (*script->script_p <= 32)
\r
182 if (script->script_p >= script->end_p)
\r
183 return EndOfScript (crossline);
\r
184 if (*script->script_p++ == '\n')
\r
187 Error ("Line %i is incomplete\n",scriptline);
\r
188 scriptline = script->line++;
\r
192 if (script->script_p >= script->end_p)
\r
193 return EndOfScript (crossline);
\r
196 if (*script->script_p == ';' || *script->script_p == '#'
\r
197 || ( script->script_p[0] == '/' && script->script_p[1] == '/') )
\r
200 Error ("Line %i is incomplete\n",scriptline);
\r
201 while (*script->script_p++ != '\n')
\r
202 if (script->script_p >= script->end_p)
\r
203 return EndOfScript (crossline);
\r
208 if (script->script_p[0] == '/' && script->script_p[1] == '*')
\r
211 Error ("Line %i is incomplete\n",scriptline);
\r
212 script->script_p+=2;
\r
213 while (script->script_p[0] != '*' && script->script_p[1] != '/')
\r
215 script->script_p++;
\r
216 if (script->script_p >= script->end_p)
\r
217 return EndOfScript (crossline);
\r
219 script->script_p += 2;
\r
228 if (*script->script_p == '"')
\r
231 script->script_p++;
\r
232 while (*script->script_p != '"')
\r
234 *token_p++ = *script->script_p++;
\r
235 if (script->script_p == script->end_p)
\r
237 if (token_p == &token[MAXTOKEN])
\r
238 Error ("Token too large on line %i\n",scriptline);
\r
240 script->script_p++;
\r
242 else // regular token
\r
243 while ( *script->script_p > 32 && *script->script_p != ';')
\r
245 *token_p++ = *script->script_p++;
\r
246 if (script->script_p == script->end_p)
\r
248 if (token_p == &token[MAXTOKEN])
\r
249 Error ("Token too large on line %i\n",scriptline);
\r
254 if (!strcmp (token, "$include"))
\r
256 GetScriptToken (false);
\r
257 AddScriptToStack (token);
\r
258 return GetScriptToken (crossline);
\r
267 ScriptTokenAvailable
\r
269 Returns true if there is another token on the line
\r
272 qboolean ScriptTokenAvailable (void)
\r
276 search_p = script->script_p;
\r
278 if (search_p >= script->end_p)
\r
281 while ( *search_p <= 32)
\r
283 if (*search_p == '\n')
\r
286 if (search_p == script->end_p)
\r
291 if (*search_p == ';')
\r