2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 =============================================================================
32 =============================================================================
38 char *buffer,*script_p,*end_p;
42 #define MAX_INCLUDES 8
43 script_t scriptstack[MAX_INCLUDES];
49 qboolean tokenready; // only true if UnGetToken was just called
56 void AddScriptToStack (char *filename)
61 if (script == &scriptstack[MAX_INCLUDES])
62 Error ("script file exceeded MAX_INCLUDES");
63 strcpy (script->filename, ExpandPath (filename) );
65 size = LoadFile (script->filename, (void **)&script->buffer);
67 printf ("entering %s\n", script->filename);
71 script->script_p = script->buffer;
72 script->end_p = script->buffer + size;
81 void LoadScriptFile (char *filename)
84 AddScriptToStack (filename);
96 void ParseFromMemory (char *buffer, int size)
100 if (script == &scriptstack[MAX_INCLUDES])
101 Error ("script file exceeded MAX_INCLUDES");
102 strcpy (script->filename, "memory buffer" );
104 script->buffer = buffer;
106 script->script_p = script->buffer;
107 script->end_p = script->buffer + size;
118 Signals that the current token was not used, and should be reported
119 for the next GetToken. Note that
125 could cross a line boundary.
128 void UnGetToken (void)
134 qboolean EndOfScript (qboolean crossline)
137 Error ("Line %i is incomplete\n",scriptline);
139 if (!strcmp (script->filename, "memory buffer"))
145 free (script->buffer);
146 if (script == scriptstack+1)
152 scriptline = script->line;
153 printf ("returning to %s\n", script->filename);
154 return GetToken (crossline);
162 qboolean GetToken (qboolean crossline)
166 if (tokenready) // is a token allready waiting?
172 if (script->script_p >= script->end_p)
173 return EndOfScript (crossline);
179 while (*script->script_p <= 32)
181 if (script->script_p >= script->end_p)
182 return EndOfScript (crossline);
183 if (*script->script_p++ == '\n')
186 Error ("Line %i is incomplete\n",scriptline);
187 scriptline = script->line++;
191 if (script->script_p >= script->end_p)
192 return EndOfScript (crossline);
195 if (*script->script_p == ';' || *script->script_p == '#'
196 || ( script->script_p[0] == '/' && script->script_p[1] == '/') )
199 Error ("Line %i is incomplete\n",scriptline);
200 while (*script->script_p++ != '\n')
201 if (script->script_p >= script->end_p)
202 return EndOfScript (crossline);
207 if (script->script_p[0] == '/' && script->script_p[1] == '*')
210 Error ("Line %i is incomplete\n",scriptline);
212 while (script->script_p[0] != '*' && script->script_p[1] != '/')
215 if (script->script_p >= script->end_p)
216 return EndOfScript (crossline);
218 script->script_p += 2;
227 if (*script->script_p == '"')
231 while (*script->script_p != '"')
233 *token_p++ = *script->script_p++;
234 if (script->script_p == script->end_p)
236 if (token_p == &token[MAXTOKEN])
237 Error ("Token too large on line %i\n",scriptline);
241 else // regular token
242 while ( *script->script_p > 32 && *script->script_p != ';')
244 *token_p++ = *script->script_p++;
245 if (script->script_p == script->end_p)
247 if (token_p == &token[MAXTOKEN])
248 Error ("Token too large on line %i\n",scriptline);
253 if (!strcmp (token, "$include"))
256 AddScriptToStack (token);
257 return GetToken (crossline);
268 Returns true if there is another token on the line
271 qboolean TokenAvailable (void)
275 search_p = script->script_p;
277 if (search_p >= script->end_p)
280 while ( *search_p <= 32)
282 if (*search_p == '\n')
285 if (search_p == script->end_p)
290 if (*search_p == ';')