1 /*
2 Copyright (C) 1994-1995 Apogee Software, Ltd.
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 
13 See the GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 
19 */
20 // scriplib.c
21 
22 #ifdef DOS
23 #include <io.h>
24 #include <dos.h>
25 #endif
26 
27 #include <fcntl.h>
28 #include <string.h>
29 
30 #include "rt_def.h"
31 #include "scriplib.h"
32 #include "rt_util.h"
33 //MED
34 #include "memcheck.h"
35 
36 /*
37 =============================================================================
38 
39 						PARSING STUFF
40 
41 =============================================================================
42 */
43 
44 char    token[MAXTOKEN];
45 char    name[MAXTOKEN*2];
46 char    scriptfilename[30];
47 char    *scriptbuffer,*script_p,*scriptend_p;
48 int     scriptline;
49 boolean endofscript;
50 boolean tokenready;                     // only true if UnGetToken was just called
51 
52 /*
53 ==============
54 =
55 = LoadScriptFile
56 =
57 ==============
58 */
59 
LoadScriptFile(char * filename)60 void LoadScriptFile (char *filename)
61 {
62 	long            size;
63 
64 	size = LoadFile (filename, (void **)&scriptbuffer);
65 
66    strcpy(&scriptfilename[0],filename);
67 	script_p = scriptbuffer;
68 	scriptend_p = script_p + size;
69 	scriptline = 1;
70 	endofscript = false;
71 	tokenready = false;
72 }
73 
74 
75 /*
76 ==============
77 =
78 = UnGetToken
79 =
80 = Signals that the current token was not used, and should be reported
81 = for the next GetToken.  Note that
82 
83 GetToken (true);
84 UnGetToken ();
85 GetToken (false);
86 
87 = could cross a line boundary.
88 =
89 ==============
90 */
91 
UnGetToken(void)92 void UnGetToken (void)
93 {
94 	tokenready = true;
95 }
96 
97 
98 /*
99 ==============
100 =
101 = GetToken
102 =
103 ==============
104 */
105 
GetToken(boolean crossline)106 void GetToken (boolean crossline)
107 {
108 	char    *token_p;
109 
110 	if (tokenready)                         // is a token allready waiting?
111 	{
112 		tokenready = false;
113 		return;
114 	}
115 
116 	if (script_p >= scriptend_p)
117 	{
118 		if (!crossline)
119          Error ("Line %i is incomplete\nin file %s\n",
120                  scriptline,scriptfilename);
121 		endofscript = true;
122 		return;
123 	}
124 
125 //
126 // skip space
127 //
128 skipspace:
129 	while (*script_p <= 32)
130 	{
131 		if (script_p >= scriptend_p)
132 		{
133 			if (!crossline)
134             Error ("Line %i is incomplete\nin file %s\n",
135                    scriptline,scriptfilename);
136 			endofscript = true;
137 			return;
138 		}
139 		if (*script_p++ == '\n')
140 		{
141 			if (!crossline)
142             Error ("Line %i is incomplete\nin file %s\n",
143                   scriptline,scriptfilename);
144 			scriptline++;
145 		}
146 	}
147 
148 	if (script_p >= scriptend_p)
149 	{
150 		if (!crossline)
151          Error ("Line %i is incomplete\nin file %s\n",
152                  scriptline,scriptfilename);
153 		endofscript = true;
154 		return;
155 	}
156 
157 	if (*script_p == ';')   // semicolon is comment field
158 	{
159 		if (!crossline)
160          Error ("Line %i is incomplete\nin file %s\n",
161                  scriptline,scriptfilename);
162 		while (*script_p++ != '\n')
163 			if (script_p >= scriptend_p)
164 			{
165 				endofscript = true;
166 				return;
167 			}
168 		goto skipspace;
169 	}
170 
171 //
172 // copy token
173 //
174 	token_p = token;
175 
176 	while ( *script_p > 32 && *script_p != ';')
177 	{
178 		*token_p++ = *script_p++;
179 		if (script_p == scriptend_p)
180 			break;
181 		if (token_p == &token[MAXTOKEN])
182          Error ("Token too large on line %i\nin file %s\n",
183                  scriptline,scriptfilename);
184    }
185 
186 	*token_p = 0;
187 }
188 
189 
190 
191 /*
192 ==============
193 =
194 = GetTokenEOL
195 =
196 ==============
197 */
198 
GetTokenEOL(boolean crossline)199 void GetTokenEOL (boolean crossline)
200 {
201    char    *name_p;
202 
203 	if (tokenready)                         // is a token allready waiting?
204 	{
205 		tokenready = false;
206 		return;
207 	}
208 
209 	if (script_p >= scriptend_p)
210 	{
211 		if (!crossline)
212          Error ("Line %i is incomplete\nin file %s\n",
213                  scriptline,scriptfilename);
214       endofscript = true;
215 		return;
216 	}
217 
218 //
219 // skip space
220 //
221 skipspace:
222 	while (*script_p <= 32)
223 	{
224 		if (script_p >= scriptend_p)
225 		{
226 			if (!crossline)
227             Error ("Line %i is incomplete\nin file %s\n",
228                    scriptline,scriptfilename);
229 			endofscript = true;
230 			return;
231 		}
232 		if (*script_p++ == '\n')
233 		{
234 			if (!crossline)
235             Error ("Line %i is incomplete\nin file %s\n",
236                    scriptline,scriptfilename);
237 			scriptline++;
238 		}
239 	}
240 
241 	if (script_p >= scriptend_p)
242 	{
243 		if (!crossline)
244          Error ("Line %i is incomplete\nin file %s\n",
245                  scriptline,scriptfilename);
246 		endofscript = true;
247 		return;
248 	}
249 
250 	if (*script_p == ';')   // semicolon is comment field
251 	{
252 		if (!crossline)
253          Error ("Line %i is incomplete\nin file %s\n",
254                  scriptline,scriptfilename);
255 		while (*script_p++ != '\n')
256 			if (script_p >= scriptend_p)
257 			{
258 				endofscript = true;
259 				return;
260 			}
261 		goto skipspace;
262 	}
263 
264 //
265 // copy token
266 //
267    name_p  = name;
268 
269 	while (*script_p >= 32)
270 	{
271 		*name_p++ = *script_p++;
272 		if (script_p == scriptend_p)
273 			break;
274 		if (name_p == &name[MAXTOKEN*2])
275          Error ("Name too large on line %i\nin file %s\n",
276                  scriptline,scriptfilename);
277 	}
278 
279 	*name_p = 0;
280 }
281 
282 
283 
284 /*
285 ==============
286 =
287 = TokenAvailable
288 =
289 = Returns true if there is another token on the line
290 =
291 ==============
292 */
293 
TokenAvailable(void)294 boolean TokenAvailable (void)
295 {
296 	char    *search_p;
297 
298 	search_p = script_p;
299 
300 	if (search_p >= scriptend_p)
301 		return false;
302 
303 	while ( *search_p <= 32)
304 	{
305 		if (*search_p == '\n')
306 			return false;
307 		search_p++;
308 		if (search_p == scriptend_p)
309 			return false;
310 
311 	}
312 
313 	if (*search_p == ';')
314 		return false;
315 
316 	return true;
317 }
318 
319 
320