Debian Codegraph @

code/botlib/l_script.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1999-2005 Id Software, Inc.
4
5 This file is part of Quake III Arena source code.
6
7 Quake III Arena source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake III Arena source code is distributed in the hope that it will be
13 useful, 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.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake III Arena source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22
23 /*****************************************************************************
24 * name: l_script.c
25 *
26 * desc: lexicographical parser
27 *
28 * $Archive: /MissionPack/code/botlib/l_script.c $
29 *
30 *****************************************************************************/
31
32 //#define SCREWUP
33 //#define BOTLIB
34 //#define MEQCC
35 //#define BSPC
36
37 #ifdef SCREWUP
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <limits.h>
41 #include <string.h>
42 #include <stdarg.h>
43 #include "l_memory.h"
44 #include "l_script.h"
45
46 typedef enum {qfalse, qtrue} qboolean;
47
48 #endif //SCREWUP
49
50 #ifdef BOTLIB
51 //include files for usage in the bot library
52 #include "../qcommon/q_shared.h"
53 #include "botlib.h"
54 #include "be_interface.h"
55 #include "l_script.h"
56 #include "l_memory.h"
57 #include "l_log.h"
58 #include "l_libvar.h"
59 #endif //BOTLIB
60
61 #ifdef MEQCC
62 //include files for usage in MrElusive's QuakeC Compiler
63 #include "qcc.h"
64 #include "l_script.h"
65 #include "l_memory.h"
66 #include "l_log.h"
67
68 #define qtrue true
69 #define qfalse false
70 #endif //MEQCC
71
72 #ifdef BSPC
73 //include files for usage in the BSP Converter
74 #include "../bspc/qbsp.h"
75 #include "../bspc/l_log.h"
76 #include "../bspc/l_mem.h"
77
78 #define qtrue true
79 #define qfalse false
80 #endif //BSPC
81
82
83 #define PUNCTABLE
84
85 //longer punctuations first
86 punctuation_t default_punctuations[] =
87 {
88 //binary operators
89 {">>=",P_RSHIFT_ASSIGN, NULL},
90 {"<<=",P_LSHIFT_ASSIGN, NULL},
91 //
92 {"...",P_PARMS, NULL},
93 //define merge operator
94 {"##",P_PRECOMPMERGE, NULL},
95 //logic operators
96 {"&&",P_LOGIC_AND, NULL},
97 {"||",P_LOGIC_OR, NULL},
98 {">=",P_LOGIC_GEQ, NULL},
99 {"<=",P_LOGIC_LEQ, NULL},
100 {"==",P_LOGIC_EQ, NULL},
101 {"!=",P_LOGIC_UNEQ, NULL},
102 //arithmatic operators
103 {"*=",P_MUL_ASSIGN, NULL},
104 {"/=",P_DIV_ASSIGN, NULL},
105 {"%=",P_MOD_ASSIGN, NULL},
106 {"+=",P_ADD_ASSIGN, NULL},
107 {"-=",P_SUB_ASSIGN, NULL},
108 {"++",P_INC, NULL},
109 {"--",P_DEC, NULL},
110 //binary operators
111 {"&=",P_BIN_AND_ASSIGN, NULL},
112 {"|=",P_BIN_OR_ASSIGN, NULL},
113 {"^=",P_BIN_XOR_ASSIGN, NULL},
114 {">>",P_RSHIFT, NULL},
115 {"<<",P_LSHIFT, NULL},
116 //reference operators
117 {"->",P_POINTERREF, NULL},
118 //C++
119 {"::",P_CPP1, NULL},
120 {".*",P_CPP2, NULL},
121 //arithmatic operators
122 {"*",P_MUL, NULL},
123 {"/",P_DIV, NULL},
124 {"%",P_MOD, NULL},
125 {"+",P_ADD, NULL},
126 {"-",P_SUB, NULL},
127 {"=",P_ASSIGN, NULL},
128 //binary operators
129 {"&",P_BIN_AND, NULL},
130 {"|",P_BIN_OR, NULL},
131 {"^",P_BIN_XOR, NULL},
132 {"~",P_BIN_NOT, NULL},
133 //logic operators
134 {"!",P_LOGIC_NOT, NULL},
135 {">",P_LOGIC_GREATER, NULL},
136 {"<",P_LOGIC_LESS, NULL},
137 //reference operator
138 {".",P_REF, NULL},
139 //seperators
140 {",",P_COMMA, NULL},
141 {";",P_SEMICOLON, NULL},
142 //label indication
143 {":",P_COLON, NULL},
144 //if statement
145 {"?",P_QUESTIONMARK, NULL},
146 //embracements
147 {"(",P_PARENTHESESOPEN, NULL},
148 {")",P_PARENTHESESCLOSE, NULL},
149 {"{",P_BRACEOPEN, NULL},
150 {"}",P_BRACECLOSE, NULL},
151 {"[",P_SQBRACKETOPEN, NULL},
152 {"]",P_SQBRACKETCLOSE, NULL},
153 //
154 {"\\",P_BACKSLASH, NULL},
155 //precompiler operator
156 {"#",P_PRECOMP, NULL},
157 #ifdef DOLLAR
158 {"$",P_DOLLAR, NULL},
159 #endif //DOLLAR
160 {NULL, 0}
161 };
162
163 #ifdef BOTLIB
164 char basefolder[MAX_QPATH];
165 #endif
166
167 //===========================================================================
168 //
169 // Parameter: -
170 // Returns: -
171 // Changes Globals: -
172 //===========================================================================
173 void PS_CreatePunctuationTable(script_t *script, punctuation_t *punctuations)
174 {
175 int i;
176 punctuation_t *p, *lastp, *newp;
177
178 //get memory for the table
179 if (!script->punctuationtable) script->punctuationtable = (punctuation_t **)
180 GetMemory(256 * sizeof(punctuation_t *));
181 Com_Memset(script->punctuationtable, 0, 256 * sizeof(punctuation_t *));
182 //add the punctuations in the list to the punctuation table
183 for (i = 0; punctuations[i].p; i++)
184 {
185 newp = &punctuations[i];
186 lastp = NULL;
187 //sort the punctuations in this table entry on length (longer punctuations first)
188 for (p = script->punctuationtable[(unsigned int) newp->p[0]]; p; p = p->next)
189 {
190 if (strlen(p->p) < strlen(newp->p))
191 {
192 newp->next = p;
193 if (lastp) lastp->next = newp;
194 else script->punctuationtable[(unsigned int) newp->p[0]] = newp;
195 break;
196 } //end if
197 lastp = p;
198 } //end for
199 if (!p)
200 {
201 newp->next = NULL;
202 if (lastp) lastp->next = newp;
203 else script->punctuationtable[(unsigned int) newp->p[0]] = newp;
204 } //end if
205 } //end for
206 } //end of the function PS_CreatePunctuationTable
207 //===========================================================================
208 //
209 // Parameter: -
210 // Returns: -
211 // Changes Globals: -
212 //===========================================================================
213 char *PunctuationFromNum(script_t *script, int num)
214 {
215 int i;
216
217 for (i = 0; script->punctuations[i].p; i++)
218 {
219 if (script->punctuations[i].n == num) return script->punctuations[i].p;
220 } //end for
221 return "unknown punctuation";
222 } //end of the function PunctuationFromNum
223 //===========================================================================
224 //
225 // Parameter: -
226 // Returns: -
227 // Changes Globals: -
228 //===========================================================================
229 void QDECL ScriptError(script_t *script, char *str, ...)
230 {
231 char text[1024];
232 va_list ap;
233
234 if (script->flags & SCFL_NOERRORS) return;
235
236 va_start(ap, str);
237 Q_vsnprintf(text, sizeof(text), str, ap);
238 va_end(ap);
239 #ifdef BOTLIB
240 botimport.Print(PRT_ERROR, "file %s, line %d: %s\n", script->filename, script->line, text);
241 #endif //BOTLIB
242 #ifdef MEQCC
243 printf("error: file %s, line %d: %s\n", script->filename, script->line, text);
244 #endif //MEQCC
245 #ifdef BSPC
246 Log_Print("error: file %s, line %d: %s\n", script->filename, script->line, text);
247 #endif //BSPC
248 } //end of the function ScriptError
249 //===========================================================================
250 //
251 // Parameter: -
252 // Returns: -
253 // Changes Globals: -
254 //===========================================================================
255 void QDECL ScriptWarning(script_t *script, char *str, ...)
256 {
257 char text[1024];
258 va_list ap;
259
260 if (script->flags & SCFL_NOWARNINGS) return;
261
262 va_start(ap, str);
263 Q_vsnprintf(text, sizeof(text), str, ap);
264 va_end(ap);
265 #ifdef BOTLIB
266 botimport.Print(PRT_WARNING, "file %s, line %d: %s\n", script->filename, script->line, text);
267 #endif //BOTLIB
268 #ifdef MEQCC
269 printf("warning: file %s, line %d: %s\n", script->filename, script->line, text);
270 #endif //MEQCC
271 #ifdef BSPC
272 Log_Print("warning: file %s, line %d: %s\n", script->filename, script->line, text);
273 #endif //BSPC
274 } //end of the function ScriptWarning
275 //===========================================================================
276 //
277 // Parameter: -
278 // Returns: -
279 // Changes Globals: -
280 //===========================================================================
281 void SetScriptPunctuations(script_t *script, punctuation_t *p)
282 {
283 #ifdef PUNCTABLE
284 if (p) PS_CreatePunctuationTable(script, p);
285 else PS_CreatePunctuationTable(script, default_punctuations);
286 #endif //PUNCTABLE
287 if (p) script->punctuations = p;
288 else script->punctuations = default_punctuations;
289 } //end of the function SetScriptPunctuations
290 //============================================================================
291 // Reads spaces, tabs, C-like comments etc.
292 // When a newline character is found the scripts line counter is increased.
293 //
294 // Parameter: -
295 // Returns: -
296 // Changes Globals: -
297 //============================================================================
298 int PS_ReadWhiteSpace(script_t *script)
299 {
300 while(1)
301 {
302 //skip white space
303 while(*script->script_p <= ' ')
304 {
305 if (!*script->script_p) return 0;
306 if (*script->script_p == '\n') script->line++;
307 script->script_p++;
308 } //end while
309 //skip comments
310 if (*script->script_p == '/')
311 {
312 //comments //
313 if (*(script->script_p+1) == '/')
314 {
315 script->script_p++;
316 do
317 {
318 script->script_p++;
319 if (!*script->script_p) return 0;
320 } //end do
321 while(*script->script_p != '\n');
322 script->line++;
323 script->script_p++;
324 if (!*script->script_p) return 0;
325 continue;
326 } //end if
327 //comments /* */
328 else if (*(script->script_p+1) == '*')
329 {
330 script->script_p++;
331 do
332 {
333 script->script_p++;
334 if (!*script->script_p) return 0;
335 if (*script->script_p == '\n') script->line++;
336 } //end do
337 while(!(*script->script_p == '*' && *(script->script_p+1) == '/'));
338 script->script_p++;
339 if (!*script->script_p) return 0;
340 script->script_p++;
341 if (!*script->script_p) return 0;
342 continue;
343 } //end if
344 } //end if
345 break;
346 } //end while
347 return 1;
348 } //end of the function PS_ReadWhiteSpace
349 //============================================================================
350 // Reads an escape character.
351 //
352 // Parameter: script : script to read from
353 // ch : place to store the read escape character
354 // Returns: -
355 // Changes Globals: -
356 //============================================================================
357 int PS_ReadEscapeCharacter(script_t *script, char *ch)
358 {
359 int c, val;
360
361 //step over the leading '\\'
362 script->script_p++;
363 //determine the escape character
364 switch(*script->script_p)
365 {
366 case '\\': c = '\\'; break;
367 case 'n': c = '\n'; break;
368 case 'r': c = '\r'; break;
369 case 't': c = '\t'; break;
370 case 'v': c = '\v'; break;
371 case 'b': c = '\b'; break;
372 case 'f': c = '\f'; break;
373 case 'a': c = '\a'; break;
374 case '\'': c = '\''; break;
375 case '\"': c = '\"'; break;
376 case '\?': c = '\?'; break;
377 case 'x':
378 {
379 script->script_p++;
380 for (val = 0; ; script->script_p++)
381 {
382 c = *script->script_p;
383 if (c >= '0' && c <= '9') c = c - '0';
384 else if (c >= 'A' && c <= 'Z') c = c - 'A' + 10;
385 else if (c >= 'a' && c <= 'z') c = c - 'a' + 10;
386 else break;
387 val = (val << 4) + c;
388 } //end for
389 script->script_p--;
390 if (val > 0xFF)
391 {
392 ScriptWarning(script, "too large value in escape character");
393 val = 0xFF;
394 } //end if
395 c = val;
396 break;
397 } //end case
398 default: //NOTE: decimal ASCII code, NOT octal
399 {
400 if (*script->script_p < '0' || *script->script_p > '9') ScriptError(script, "unknown escape char");
401 for (val = 0; ; script->script_p++)
402 {
403 c = *script->script_p;
404 if (c >= '0' && c <= '9') c = c - '0';
405 else break;
406 val = val * 10 + c;
407 } //end for
408 script->script_p--;
409 if (val > 0xFF)
410 {
411 ScriptWarning(script, "too large value in escape character");
412 val = 0xFF;
413 } //end if
414 c = val;
415 break;
416 } //end default
417 } //end switch
418 //step over the escape character or the last digit of the number
419 script->script_p++;
420 //store the escape character
421 *ch = c;
422 //successfully read escape character
423 return 1;
424 } //end of the function PS_ReadEscapeCharacter
425 //============================================================================
426 // Reads C-like string. Escape characters are interpretted.
427 // Quotes are included with the string.
428 // Reads two strings with a white space between them as one string.
429 //
430 // Parameter: script : script to read from
431 // token : buffer to store the string
432 // Returns: qtrue when a string was read successfully
433 // Changes Globals: -
434 //============================================================================
435 int PS_ReadString(script_t *script, token_t *token, int quote)
436 {
437 int len, tmpline;
438 char *tmpscript_p;
439
440 if (quote == '\"') token->type = TT_STRING;
441 else token->type = TT_LITERAL;
442
443 len = 0;
444 //leading quote
445 token->string[len++] = *script->script_p++;
446 //
447 while(1)
448 {
449 //minus 2 because trailing double quote and zero have to be appended
450 if (len >= MAX_TOKEN - 2)
451 {
452 ScriptError(script, "string longer than MAX_TOKEN = %d", MAX_TOKEN);
453 return 0;
454 } //end if
455 //if there is an escape character and
456 //if escape characters inside a string are allowed
457 if (*script->script_p == '\\' && !(script->flags & SCFL_NOSTRINGESCAPECHARS))
458 {
459 if (!PS_ReadEscapeCharacter(script, &token->string[len]))
460 {
461 token->string[len] = 0;
462 return 0;
463 } //end if
464 len++;
465 } //end if
466 //if a trailing quote
467 else if (*script->script_p == quote)
468 {
469 //step over the double quote
470 script->script_p++;
471 //if white spaces in a string are not allowed
472 if (script->flags & SCFL_NOSTRINGWHITESPACES) break;
473 //
474 tmpscript_p = script->script_p;
475 tmpline = script->line;
476 //read unusefull stuff between possible two following strings
477 if (!PS_ReadWhiteSpace(script))
478 {
479 script->script_p = tmpscript_p;
480 script->line = tmpline;
481 break;
482 } //end if
483 //if there's no leading double qoute
484 if (*script->script_p != quote)
485 {
486 script->script_p = tmpscript_p;
487 script->line = tmpline;
488 break;
489 } //end if
490 //step over the new leading double quote
491 script->script_p++;
492 } //end if
493 else
494 {
495 if (*script->script_p == '\0')
496 {
497 token->string[len] = 0;
498 ScriptError(script, "missing trailing quote");
499 return 0;
500 } //end if
501 if (*script->script_p == '\n')
502 {
503 token->string[len] = 0;
504 ScriptError(script, "newline inside string %s", token->string);
505 return 0;
506 } //end if
507 token->string[len++] = *script->script_p++;
508 } //end else
509 } //end while
510 //trailing quote
511 token->string[len++] = quote;
512 //end string with a zero
513 token->string[len] = '\0';
514 //the sub type is the length of the string
515 token->subtype = len;
516 return 1;
517 } //end of the function PS_ReadString
518 //============================================================================
519 //
520 // Parameter: -
521 // Returns: -
522 // Changes Globals: -
523 //============================================================================
524 int PS_ReadName(script_t *script, token_t *token)
525 {
526 int len = 0;
527 char c;
528
529 token->type = TT_NAME;
530 do
531 {
532 token->string[len++] = *script->script_p++;
533 if (len >= MAX_TOKEN)
534 {
535 ScriptError(script, "name longer than MAX_TOKEN = %d", MAX_TOKEN);
536 return 0;
537 } //end if
538 c = *script->script_p;
539 } while ((c >= 'a' && c <= 'z') ||
540 (c >= 'A' && c <= 'Z') ||
541 (c >= '0' && c <= '9') ||
542 c == '_');
543 token->string[len] = '\0';
544 //the sub type is the length of the name
545 token->subtype = len;
546 return 1;
547 } //end of the function PS_ReadName
548 //============================================================================
549 //
550 // Parameter: -
551 // Returns: -
552 // Changes Globals: -
553 //============================================================================
554 void NumberValue(char *string, int subtype, unsigned long int *intvalue,
555 float *floatvalue)
556 {
557 unsigned long int dotfound = 0;
558
559 *intvalue = 0;
560 *floatvalue = 0;
561 //floating point number
562 if (subtype & TT_FLOAT)
563 {
564 while(*string)
565 {
566 if (*string == '.')
567 {
568 if (dotfound) return;
569 dotfound = 10;
570 string++;
571 } //end if
572 if (dotfound)
573 {
574 *floatvalue = *floatvalue + (float) (*string - '0') /
575 (float) dotfound;
576 dotfound *= 10;
577 } //end if
578 else
579 {
580 *floatvalue = *floatvalue * 10.0 + (float) (*string - '0');
581 } //end else
582 string++;
583 } //end while
584 *intvalue = (unsigned long) *floatvalue;
585 } //end if
586 else if (subtype & TT_DECIMAL)
587 {
588 while(*string) *intvalue = *intvalue * 10 + (*string++ - '0');
589 *floatvalue = *intvalue;
590 } //end else if
591 else if (subtype & TT_HEX)
592 {
593 //step over the leading 0x or 0X
594 string += 2;
595 while(*string)
596 {
597 *intvalue <<= 4;
598 if (*string >= 'a' && *string <= 'f') *intvalue += *string - 'a' + 10;
599 else if (*string >= 'A' && *string <= 'F') *intvalue += *string - 'A' + 10;
600 else *intvalue += *string - '0';
601 string++;
602 } //end while
603 *floatvalue = *intvalue;
604 } //end else if
605 else if (subtype & TT_OCTAL)
606 {
607 //step over the first zero
608 string += 1;
609 while(*string) *intvalue = (*intvalue << 3) + (*string++ - '0');
610 *floatvalue = *intvalue;
611 } //end else if
612 else if (subtype & TT_BINARY)
613 {
614 //step over the leading 0b or 0B
615 string += 2;
616 while(*string) *intvalue = (*intvalue << 1) + (*string++ - '0');
617 *floatvalue = *intvalue;
618 } //end else if
619 } //end of the function NumberValue
620 //============================================================================
621 //
622 // Parameter: -
623 // Returns: -
624 // Changes Globals: -
625 //============================================================================
626 int PS_ReadNumber(script_t *script, token_t *token)
627 {
628 int len = 0, i;
629 int octal, dot;
630 char c;
631 // unsigned long int intvalue = 0;
632 // double floatvalue = 0;
633
634 token->type = TT_NUMBER;
635 //check for a hexadecimal number
636 if (*script->script_p == '0' &&
637 (*(script->script_p + 1) == 'x' ||
638 *(script->script_p + 1) == 'X'))
639 {
640 token->string[len++] = *script->script_p++;
641 token->string[len++] = *script->script_p++;
642 c = *script->script_p;
643 //hexadecimal
644 while((c >= '0' && c <= '9') ||
645 (c >= 'a' && c <= 'f') ||
646 (c >= 'A' && c <= 'F'))
647 {
648 token->string[len++] = *script->script_p++;
649 if (len >= MAX_TOKEN)
650 {
651 ScriptError(script, "hexadecimal number longer than MAX_TOKEN = %d", MAX_TOKEN);
652 return 0;
653 } //end if
654 c = *script->script_p;
655 } //end while
656 token->subtype |= TT_HEX;
657 } //end if
658 #ifdef BINARYNUMBERS
659 //check for a binary number
660 else if (*script->script_p == '0' &&
661 (*(script->script_p + 1) == 'b' ||
662 *(script->script_p + 1) == 'B'))
663 {
664 token->string[len++] = *script->script_p++;
665 token->string[len++] = *script->script_p++;
666 c = *script->script_p;
667 //binary
668 while(c == '0' || c == '1')
669 {
670 token->string[len++] = *script->script_p++;
671 if (len >= MAX_TOKEN)
672 {
673 ScriptError(script, "binary number longer than MAX_TOKEN = %d", MAX_TOKEN);
674 return 0;
675 } //end if
676 c = *script->script_p;
677 } //end while
678 token->subtype |= TT_BINARY;
679 } //end if
680 #endif //BINARYNUMBERS
681 else //decimal or octal integer or floating point number
682 {
683 octal = qfalse;
684 dot = qfalse;
685 if (*script->script_p == '0') octal = qtrue;
686 while(1)
687 {
688 c = *script->script_p;
689 if (c == '.') dot = qtrue;
690 else if (c == '8' || c == '9') octal = qfalse;
691 else if (c < '0' || c > '9') break;
692 token->string[len++] = *script->script_p++;
693 if (len >= MAX_TOKEN - 1)
694 {
695 ScriptError(script, "number longer than MAX_TOKEN = %d", MAX_TOKEN);
696 return 0;
697 } //end if
698 } //end while
699 if (octal) token->subtype |= TT_OCTAL;
700 else token->subtype |= TT_DECIMAL;
701 if (dot) token->subtype |= TT_FLOAT;
702 } //end else
703 for (i = 0; i < 2; i++)
704 {
705 c = *script->script_p;
706 //check for a LONG number
707 if ( (c == 'l' || c == 'L')
708 && !(token->subtype & TT_LONG))
709 {
710 script->script_p++;
711 token->subtype |= TT_LONG;
712 } //end if
713 //check for an UNSIGNED number
714 else if ( (c == 'u' || c == 'U')
715 && !(token->subtype & (TT_UNSIGNED | TT_FLOAT)))
716 {
717 script->script_p++;
718 token->subtype |= TT_UNSIGNED;
719 } //end if
720 } //end for
721 token->string[len] = '\0';
722 #ifdef NUMBERVALUE
723 NumberValue(token->string, token->subtype, &token->intvalue, &token->floatvalue);
724 #endif //NUMBERVALUE
725 if (!(token->subtype & TT_FLOAT)) token->subtype |= TT_INTEGER;
726 return 1;
727 } //end of the function PS_ReadNumber
728 //============================================================================
729 //
730 // Parameter: -
731 // Returns: -
732 // Changes Globals: -
733 //============================================================================
734 int PS_ReadLiteral(script_t *script, token_t *token)
735 {
736 token->type = TT_LITERAL;
737 //first quote
738 token->string[0] = *script->script_p++;
739 //check for end of file
740 if (!*script->script_p)
741 {
742 ScriptError(script, "end of file before trailing \'");
743 return 0;
744 } //end if
745 //if it is an escape character
746 if (*script->script_p == '\\')
747 {
748 if (!PS_ReadEscapeCharacter(script, &token->string[1])) return 0;
749 } //end if
750 else
751 {
752 token->string[1] = *script->script_p++;
753 } //end else
754 //check for trailing quote
755 if (*script->script_p != '\'')
756 {
757 ScriptWarning(script, "too many characters in literal, ignored");
758 while(*script->script_p &&
759 *script->script_p != '\'' &&
760 *script->script_p != '\n')
761 {
762 script->script_p++;
763 } //end while
764 if (*script->script_p == '\'') script->script_p++;
765 } //end if
766 //store the trailing quote
767 token->string[2] = *script->script_p++;
768 //store trailing zero to end the string
769 token->string[3] = '\0';
770 //the sub type is the integer literal value
771 token->subtype = token->string[1];
772 //
773 return 1;
774 } //end of the function PS_ReadLiteral
775 //============================================================================
776 //
777 // Parameter: -
778 // Returns: -
779 // Changes Globals: -
780 //============================================================================
781 int PS_ReadPunctuation(script_t *script, token_t *token)
782 {
783 int len;
784 char *p;
785 punctuation_t *punc;
786
787 #ifdef PUNCTABLE
788 for (punc = script->punctuationtable[(unsigned int)*script->script_p]; punc; punc = punc->next)
789 {
790 #else
791 int i;
792
793 for (i = 0; script->punctuations[i].p; i++)
794 {
795 punc = &script->punctuations[i];
796 #endif //PUNCTABLE
797 p = punc->p;
798 len = strlen(p);
799 //if the script contains at least as much characters as the punctuation
800 if (script->script_p + len <= script->end_p)
801 {
802 //if the script contains the punctuation
803 if (!strncmp(script->script_p, p, len))
804 {
805 Q_strncpyz(token->string, p, MAX_TOKEN);
806 script->script_p += len;
807 token->type = TT_PUNCTUATION;
808 //sub type is the number of the punctuation
809 token->subtype = punc->n;
810 return 1;
811 } //end if
812 } //end if
813 } //end for
814 return 0;
815 } //end of the function PS_ReadPunctuation
816 //============================================================================
817 //
818 // Parameter: -
819 // Returns: -
820 // Changes Globals: -
821 //============================================================================
822 int PS_ReadPrimitive(script_t *script, token_t *token)
823 {
824 int len;
825
826 len = 0;
827 while(*script->script_p > ' ' && *script->script_p != ';')
828 {
829 if (len >= MAX_TOKEN - 1)
830 {
831 ScriptError(script, "primitive token longer than MAX_TOKEN = %d", MAX_TOKEN);
832 return 0;
833 } //end if
834 token->string[len++] = *script->script_p++;
835 } //end while
836 token->string[len] = 0;
837 //copy the token into the script structure
838 Com_Memcpy(&script->token, token, sizeof(token_t));
839 //primitive reading successful
840 return 1;
841 } //end of the function PS_ReadPrimitive
842 //============================================================================
843 //
844 // Parameter: -
845 // Returns: -
846 // Changes Globals: -
847 //============================================================================
848 int PS_ReadToken(script_t *script, token_t *token)
849 {
850 //if there is a token available (from UnreadToken)
851 if (script->tokenavailable)
852 {
853 script->tokenavailable = 0;
854 Com_Memcpy(token, &script->token, sizeof(token_t));
855 return 1;
856 } //end if
857 //save script pointer
858 script->lastscript_p = script->script_p;
859 //save line counter
860 script->lastline = script->line;
861 //clear the token stuff
862 Com_Memset(token, 0, sizeof(token_t));
863 //start of the white space
864 script->whitespace_p = script->script_p;
865 token->whitespace_p = script->script_p;
866 //read unusefull stuff
867 if (!PS_ReadWhiteSpace(script)) return 0;
868 //end of the white space
869 script->endwhitespace_p = script->script_p;
870 token->endwhitespace_p = script->script_p;
871 //line the token is on
872 token->line = script->line;
873 //number of lines crossed before token
874 token->linescrossed = script->line - script->lastline;
875 //if there is a leading double quote
876 if (*script->script_p == '\"')
877 {
878 if (!PS_ReadString(script, token, '\"')) return 0;
879 } //end if
880 //if a literal
881 else if (*script->script_p == '\'')
882 {
883 //if (!PS_ReadLiteral(script, token)) return 0;
884 if (!PS_ReadString(script, token, '\'')) return 0;
885 } //end if
886 //if there is a number
887 else if ((*script->script_p >= '0' && *script->script_p <= '9') ||
888 (*script->script_p == '.' &&
889 (*(script->script_p + 1) >= '0' && *(script->script_p + 1) <= '9')))
890 {
891 if (!PS_ReadNumber(script, token)) return 0;
892 } //end if
893 //if this is a primitive script
894 else if (script->flags & SCFL_PRIMITIVE)
895 {
896 return PS_ReadPrimitive(script, token);
897 } //end else if
898 //if there is a name
899 else if ((*script->script_p >= 'a' && *script->script_p <= 'z') ||
900 (*script->script_p >= 'A' && *script->script_p <= 'Z') ||
901 *script->script_p == '_')
902 {
903 if (!PS_ReadName(script, token)) return 0;
904 } //end if
905 //check for punctuations
906 else if (!PS_ReadPunctuation(script, token))
907 {
908 ScriptError(script, "can't read token");
909 return 0;
910 } //end if
911 //copy the token into the script structure
912 Com_Memcpy(&script->token, token, sizeof(token_t));
913 //successfully read a token
914 return 1;
915 } //end of the function PS_ReadToken
916 //============================================================================
917 //
918 // Parameter: -
919 // Returns: -
920 // Changes Globals: -
921 //============================================================================
922 int PS_ExpectTokenString(script_t *script, char *string)
923 {
924 token_t token;
925
926 if (!PS_ReadToken(script, &token))
927 {
928 ScriptError(script, "couldn't find expected %s", string);
929 return 0;
930 } //end if
931
932 if (strcmp(token.string, string))
933 {
934 ScriptError(script, "expected %s, found %s", string, token.string);
935 return 0;
936 } //end if
937 return 1;
938 } //end of the function PS_ExpectToken
939 //============================================================================
940 //
941 // Parameter: -
942 // Returns: -
943 // Changes Globals: -
944 //============================================================================
945 int PS_ExpectTokenType(script_t *script, int type, int subtype, token_t *token)
946 {
947 char str[MAX_TOKEN];
948
949 if (!PS_ReadToken(script, token))
950 {
951 ScriptError(script, "couldn't read expected token");
952 return 0;
953 } //end if
954
955 if (token->type != type)
956 {
957 strcpy(str, "");
958 if (type == TT_STRING) strcpy(str, "string");
959 if (type == TT_LITERAL) strcpy(str, "literal");
960 if (type == TT_NUMBER) strcpy(str, "number");
961 if (type == TT_NAME) strcpy(str, "name");
962 if (type == TT_PUNCTUATION) strcpy(str, "punctuation");
963 ScriptError(script, "expected a %s, found %s", str, token->string);
964 return 0;
965 } //end if
966 if (token->type == TT_NUMBER)
967 {
968 if ((token->subtype & subtype) != subtype)
969 {
970 strcpy(str, "");
971 if (subtype & TT_DECIMAL) strcpy(str, "decimal");
972 if (subtype & TT_HEX) strcpy(str, "hex");
973 if (subtype & TT_OCTAL) strcpy(str, "octal");
974 if (subtype & TT_BINARY) strcpy(str, "binary");
975 if (subtype & TT_LONG) strcat(str, " long");
976 if (subtype & TT_UNSIGNED) strcat(str, " unsigned");
977 if (subtype & TT_FLOAT) strcat(str, " float");
978 if (subtype & TT_INTEGER) strcat(str, " integer");
979 ScriptError(script, "expected %s, found %s", str, token->string);
980 return 0;
981 } //end if
982 } //end if
983 else if (token->type == TT_PUNCTUATION)
984 {
985 if (subtype < 0)
986 {
987 ScriptError(script, "BUG: wrong punctuation subtype");
988 return 0;
989 } //end if
990 if (token->subtype != subtype)
991 {
992 ScriptError(script, "expected %s, found %s",
993 script->punctuations[subtype].p, token->string);
994 return 0;
995 } //end if
996 } //end else if
997 return 1;
998 } //end of the function PS_ExpectTokenType
999 //============================================================================
1000 //
1001 // Parameter: -
1002 // Returns: -
1003 // Changes Globals: -
1004 //============================================================================
1005 int PS_ExpectAnyToken(script_t *script, token_t *token)
1006 {
1007 if (!PS_ReadToken(script, token))
1008 {
1009 ScriptError(script, "couldn't read expected token");
1010 return 0;
1011 } //end if
1012 else
1013 {
1014 return 1;
1015 } //end else
1016 } //end of the function PS_ExpectAnyToken
1017 //============================================================================
1018 //
1019 // Parameter: -
1020 // Returns: -
1021 // Changes Globals: -
1022 //============================================================================
1023 int PS_CheckTokenString(script_t *script, char *string)
1024 {
1025 token_t tok;
1026
1027 if (!PS_ReadToken(script, &tok)) return 0;
1028 //if the token is available
1029 if (!strcmp(tok.string, string)) return 1;
1030 //token not available
1031 script->script_p = script->lastscript_p;
1032 return 0;
1033 } //end of the function PS_CheckTokenString
1034 //============================================================================
1035 //
1036 // Parameter: -
1037 // Returns: -
1038 // Changes Globals: -
1039 //============================================================================
1040 int PS_CheckTokenType(script_t *script, int type, int subtype, token_t *token)
1041 {
1042 token_t tok;
1043
1044 if (!PS_ReadToken(script, &tok)) return 0;
1045 //if the type matches
1046 if (tok.type == type &&
1047 (tok.subtype & subtype) == subtype)
1048 {
1049 Com_Memcpy(token, &tok, sizeof(token_t));
1050 return 1;
1051 } //end if
1052 //token is not available
1053 script->script_p = script->lastscript_p;
1054 return 0;
1055 } //end of the function PS_CheckTokenType
1056 //============================================================================
1057 //
1058 // Parameter: -
1059 // Returns: -
1060 // Changes Globals: -
1061 //============================================================================
1062 int PS_SkipUntilString(script_t *script, char *string)
1063 {
1064 token_t token;
1065
1066 while(PS_ReadToken(script, &token))
1067 {
1068 if (!strcmp(token.string, string)) return 1;
1069 } //end while
1070 return 0;
1071 } //end of the function PS_SkipUntilString
1072 //============================================================================
1073 //
1074 // Parameter: -
1075 // Returns: -
1076 // Changes Globals: -
1077 //============================================================================
1078 void PS_UnreadLastToken(script_t *script)
1079 {
1080 script->tokenavailable = 1;
1081 } //end of the function UnreadLastToken
1082 //============================================================================
1083 //
1084 // Parameter: -
1085 // Returns: -
1086 // Changes Globals: -
1087 //============================================================================
1088 void PS_UnreadToken(script_t *script, token_t *token)
1089 {
1090 Com_Memcpy(&script->token, token, sizeof(token_t));
1091 script->tokenavailable = 1;
1092 } //end of the function UnreadToken
1093 //============================================================================
1094 // returns the next character of the read white space, returns NULL if none
1095 //
1096 // Parameter: -
1097 // Returns: -
1098 // Changes Globals: -
1099 //============================================================================
1100 char PS_NextWhiteSpaceChar(script_t *script)
1101 {
1102 if (script->whitespace_p != script->endwhitespace_p)
1103 {
1104 return *script->whitespace_p++;
1105 } //end if
1106 else
1107 {
1108 return 0;
1109 } //end else
1110 } //end of the function PS_NextWhiteSpaceChar
1111 //============================================================================
1112 //
1113 // Parameter: -
1114 // Returns: -
1115 // Changes Globals: -
1116 //============================================================================
1117 void StripDoubleQuotes(char *string)
1118 {
1119 if (*string == '\"')
1120 {
1121 memmove(string, string+1, strlen(string));
1122 } //end if
1123 if (string[strlen(string)-1] == '\"')
1124 {
1125 string[strlen(string)-1] = '\0';
1126 } //end if
1127 } //end of the function StripDoubleQuotes
1128 //============================================================================
1129 //
1130 // Parameter: -
1131 // Returns: -
1132 // Changes Globals: -
1133 //============================================================================
1134 void StripSingleQuotes(char *string)
1135 {
1136 if (*string == '\'')
1137 {
1138 memmove(string, string+1, strlen(string));
1139 } //end if
1140 if (string[strlen(string)-1] == '\'')
1141 {
1142 string[strlen(string)-1] = '\0';
1143 } //end if
1144 } //end of the function StripSingleQuotes
1145 //============================================================================
1146 //
1147 // Parameter: -
1148 // Returns: -
1149 // Changes Globals: -
1150 //============================================================================
1151 float ReadSignedFloat(script_t *script)
1152 {
1153 token_t token;
1154 float sign = 1.0;
1155
1156 PS_ExpectAnyToken(script, &token);
1157 if (!strcmp(token.string, "-"))
1158 {
1159 if(!PS_ExpectAnyToken(script, &token))
1160 {
1161 ScriptError(script, "Missing float value");
1162 return 0;
1163 }
1164
1165 sign = -1.0;
1166 }
1167
1168 if (token.type != TT_NUMBER)
1169 {
1170 ScriptError(script, "expected float value, found %s", token.string);
1171 return 0;
1172 }
1173
1174 return sign * token.floatvalue;
1175 } //end of the function ReadSignedFloat
1176 //============================================================================
1177 //
1178 // Parameter: -
1179 // Returns: -
1180 // Changes Globals: -
1181 //============================================================================
1182 signed long int ReadSignedInt(script_t *script)
1183 {
1184 token_t token;
1185 signed long int sign = 1;
1186
1187 PS_ExpectAnyToken(script, &token);
1188 if (!strcmp(token.string, "-"))
1189 {
1190 if(!PS_ExpectAnyToken(script, &token))
1191 {
1192 ScriptError(script, "Missing integer value");
1193 return 0;
1194 }
1195
1196 sign = -1;
1197 }
1198
1199 if (token.type != TT_NUMBER || token.subtype == TT_FLOAT)
1200 {
1201 ScriptError(script, "expected integer value, found %s", token.string);
1202 return 0;
1203 }
1204
1205 return sign * token.intvalue;
1206 } //end of the function ReadSignedInt
1207 //============================================================================
1208 //
1209 // Parameter: -
1210 // Returns: -
1211 // Changes Globals: -
1212 //============================================================================
1213 void SetScriptFlags(script_t *script, int flags)
1214 {
1215 script->flags = flags;
1216 } //end of the function SetScriptFlags
1217 //============================================================================
1218 //
1219 // Parameter: -
1220 // Returns: -
1221 // Changes Globals: -
1222 //============================================================================
1223 int GetScriptFlags(script_t *script)
1224 {
1225 return script->flags;
1226 } //end of the function GetScriptFlags
1227 //============================================================================
1228 //
1229 // Parameter: -
1230 // Returns: -
1231 // Changes Globals: -
1232 //============================================================================
1233 void ResetScript(script_t *script)
1234 {
1235 //pointer in script buffer
1236 script->script_p = script->buffer;
1237 //pointer in script buffer before reading token
1238 script->lastscript_p = script->buffer;
1239 //begin of white space
1240 script->whitespace_p = NULL;
1241 //end of white space
1242 script->endwhitespace_p = NULL;
1243 //set if there's a token available in script->token
1244 script->tokenavailable = 0;
1245 //
1246 script->line = 1;
1247 script->lastline = 1;
1248 //clear the saved token
1249 Com_Memset(&script->token, 0, sizeof(token_t));
1250 } //end of the function ResetScript
1251 //============================================================================
1252 // returns true if at the end of the script
1253 //
1254 // Parameter: -
1255 // Returns: -
1256 // Changes Globals: -
1257 //============================================================================
1258 int EndOfScript(script_t *script)
1259 {
1260 return script->script_p >= script->end_p;
1261 } //end of the function EndOfScript
1262 //============================================================================
1263 //
1264 // Parameter: -
1265 // Returns: -
1266 // Changes Globals: -
1267 //============================================================================
1268 int NumLinesCrossed(script_t *script)
1269 {
1270 return script->line - script->lastline;
1271 } //end of the function NumLinesCrossed
1272 //============================================================================
1273 //
1274 // Parameter: -
1275 // Returns: -
1276 // Changes Globals: -
1277 //============================================================================
1278 int ScriptSkipTo(script_t *script, char *value)
1279 {
1280 int len;
1281 char firstchar;
1282
1283 firstchar = *value;
1284 len = strlen(value);
1285 do
1286 {
1287 if (!PS_ReadWhiteSpace(script)) return 0;
1288 if (*script->script_p == firstchar)
1289 {
1290 if (!strncmp(script->script_p, value, len))
1291 {
1292 return 1;
1293 } //end if
1294 } //end if
1295 script->script_p++;
1296 } while(1);
1297 } //end of the function ScriptSkipTo
1298 #ifndef BOTLIB
1299 //============================================================================
1300 //
1301 // Parameter: -
1302 // Returns: -
1303 // Changes Globals: -
1304 //============================================================================
1305 int FileLength(FILE *fp)
1306 {
1307 int pos;
1308 int end;
1309
1310 pos = ftell(fp);
1311 fseek(fp, 0, SEEK_END);
1312 end = ftell(fp);
1313 fseek(fp, pos, SEEK_SET);
1314
1315 return end;
1316 } //end of the function FileLength
1317 #endif
1318 //============================================================================
1319 //
1320 // Parameter: -
1321 // Returns: -
1322 // Changes Globals: -
1323 //============================================================================
1324 script_t *LoadScriptFile(const char *filename)
1325 {
1326 #ifdef BOTLIB
1327 fileHandle_t fp;
1328 char pathname[MAX_QPATH];
1329 #else
1330 FILE *fp;
1331 #endif
1332 int length;
1333 void *buffer;
1334 script_t *script;
1335
1336 #ifdef BOTLIB
1337 if (strlen(basefolder))
1338 Com_sprintf(pathname, sizeof(pathname), "%s/%s", basefolder, filename);
1339 else
1340 Com_sprintf(pathname, sizeof(pathname), "%s", filename);
1341 length = botimport.FS_FOpenFile( pathname, &fp, FS_READ );
1342 if (!fp) return NULL;
1343 #else
1344 fp = fopen(filename, "rb");
1345 if (!fp) return NULL;
1346
1347 length = FileLength(fp);
1348 #endif
1349
1350 buffer = GetClearedMemory(sizeof(script_t) + length + 1);
1351 script = (script_t *) buffer;
1352 Com_Memset(script, 0, sizeof(script_t));
1353 Q_strncpyz(script->filename, filename, sizeof(script->filename));
1354 script->buffer = (char *) buffer + sizeof(script_t);
1355 script->buffer[length] = 0;
1356 script->length = length;
1357 //pointer in script buffer
1358 script->script_p = script->buffer;
1359 //pointer in script buffer before reading token
1360 script->lastscript_p = script->buffer;
1361 //pointer to end of script buffer
1362 script->end_p = &script->buffer[length];
1363 //set if there's a token available in script->token
1364 script->tokenavailable = 0;
1365 //
1366 script->line = 1;
1367 script->lastline = 1;
1368 //
1369 SetScriptPunctuations(script, NULL);
1370 //
1371 #ifdef BOTLIB
1372 botimport.FS_Read(script->buffer, length, fp);
1373 botimport.FS_FCloseFile(fp);
1374 #else
1375 if (fread(script->buffer, length, 1, fp) != 1)
1376 {
1377 FreeMemory(buffer);
1378 script = NULL;
1379 } //end if
1380 fclose(fp);
1381 #endif
1382
1383 return script;
1384 } //end of the function LoadScriptFile
1385 //============================================================================
1386 //
1387 // Parameter: -
1388 // Returns: -
1389 // Changes Globals: -
1390 //============================================================================
1391 script_t *LoadScriptMemory(char *ptr, int length, char *name)
1392 {
1393 void *buffer;
1394 script_t *script;
1395
1396 buffer = GetClearedMemory(sizeof(script_t) + length + 1);
1397 script = (script_t *) buffer;
1398 Com_Memset(script, 0, sizeof(script_t));
1399 Q_strncpyz(script->filename, name, sizeof(script->filename));
1400 script->buffer = (char *) buffer + sizeof(script_t);
1401 script->buffer[length] = 0;
1402 script->length = length;
1403 //pointer in script buffer
1404 script->script_p = script->buffer;
1405 //pointer in script buffer before reading token
1406 script->lastscript_p = script->buffer;
1407 //pointer to end of script buffer
1408 script->end_p = &script->buffer[length];
1409 //set if there's a token available in script->token
1410 script->tokenavailable = 0;
1411 //
1412 script->line = 1;
1413 script->lastline = 1;
1414 //
1415 SetScriptPunctuations(script, NULL);
1416 //
1417 Com_Memcpy(script->buffer, ptr, length);
1418 //
1419 return script;
1420 } //end of the function LoadScriptMemory
1421 //============================================================================
1422 //
1423 // Parameter: -
1424 // Returns: -
1425 // Changes Globals: -
1426 //============================================================================
1427 void FreeScript(script_t *script)
1428 {
1429 #ifdef PUNCTABLE
1430 if (script->punctuationtable) FreeMemory(script->punctuationtable);
1431 #endif //PUNCTABLE
1432 FreeMemory(script);
1433 } //end of the function FreeScript
1434 //============================================================================
1435 //
1436 // Parameter: -
1437 // Returns: -
1438 // Changes Globals: -
1439 //============================================================================
1440 void PS_SetBaseFolder(char *path)
1441 {
1442 #ifdef BOTLIB
1443 Com_sprintf(basefolder, sizeof(basefolder), "%s", path);
1444 #endif
1445 } //end of the function PS_SetBaseFolder