| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 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 |
| 49 |
|
| 50 |
#ifdef BOTLIB |
| 51 |
|
| 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 |
| 60 |
|
| 61 |
#ifdef MEQCC |
| 62 |
|
| 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 |
| 71 |
|
| 72 |
#ifdef BSPC |
| 73 |
|
| 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 |
| 81 |
|
| 82 |
|
| 83 |
#define PUNCTABLE |
| 84 |
|
| 85 |
|
| 86 |
punctuation_t default_punctuations[] = |
| 87 |
{ |
| 88 |
|
| 89 |
{">>=",P_RSHIFT_ASSIGN, NULL}, |
| 90 |
{"<<=",P_LSHIFT_ASSIGN, NULL}, |
| 91 |
|
| 92 |
{"...",P_PARMS, NULL}, |
| 93 |
|
| 94 |
{"##",P_PRECOMPMERGE, NULL}, |
| 95 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 117 |
{"->",P_POINTERREF, NULL}, |
| 118 |
|
| 119 |
{"::",P_CPP1, NULL}, |
| 120 |
{".*",P_CPP2, NULL}, |
| 121 |
|
| 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 |
|
| 129 |
{"&",P_BIN_AND, NULL}, |
| 130 |
{"|",P_BIN_OR, NULL}, |
| 131 |
{"^",P_BIN_XOR, NULL}, |
| 132 |
{"~",P_BIN_NOT, NULL}, |
| 133 |
|
| 134 |
{"!",P_LOGIC_NOT, NULL}, |
| 135 |
{">",P_LOGIC_GREATER, NULL}, |
| 136 |
{"<",P_LOGIC_LESS, NULL}, |
| 137 |
|
| 138 |
{".",P_REF, NULL}, |
| 139 |
|
| 140 |
{",",P_COMMA, NULL}, |
| 141 |
{";",P_SEMICOLON, NULL}, |
| 142 |
|
| 143 |
{":",P_COLON, NULL}, |
| 144 |
|
| 145 |
{"?",P_QUESTIONMARK, NULL}, |
| 146 |
|
| 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 |
|
| 156 |
{"#",P_PRECOMP, NULL}, |
| 157 |
#ifdef DOLLAR |
| 158 |
{"$",P_DOLLAR, NULL}, |
| 159 |
#endif |
| 160 |
{NULL, 0} |
| 161 |
}; |
| 162 |
|
| 163 |
#ifdef BOTLIB |
| 164 |
char basefolder[MAX_QPATH]; |
| 165 |
#endif |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
void PS_CreatePunctuationTable(script_t *script, punctuation_t *punctuations) |
| 174 |
{ |
| 175 |
int i; |
| 176 |
punctuation_t *p, *lastp, *newp; |
| 177 |
|
| 178 |
|
| 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 |
|
| 183 |
for (i = 0; punctuations[i].p; i++) |
| 184 |
{ |
| 185 |
newp = &punctuations[i]; |
| 186 |
lastp = NULL; |
| 187 |
|
| 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 |
} |
| 197 |
lastp = p; |
| 198 |
} |
| 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 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 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 |
} |
| 221 |
return "unknown punctuation"; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 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 |
| 242 |
#ifdef MEQCC |
| 243 |
printf("error: file %s, line %d: %s\n", script->filename, script->line, text); |
| 244 |
#endif |
| 245 |
#ifdef BSPC |
| 246 |
Log_Print("error: file %s, line %d: %s\n", script->filename, script->line, text); |
| 247 |
#endif |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 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 |
| 268 |
#ifdef MEQCC |
| 269 |
printf("warning: file %s, line %d: %s\n", script->filename, script->line, text); |
| 270 |
#endif |
| 271 |
#ifdef BSPC |
| 272 |
Log_Print("warning: file %s, line %d: %s\n", script->filename, script->line, text); |
| 273 |
#endif |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 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 |
| 287 |
if (p) script->punctuations = p; |
| 288 |
else script->punctuations = default_punctuations; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
int PS_ReadWhiteSpace(script_t *script) |
| 299 |
{ |
| 300 |
while(1) |
| 301 |
{ |
| 302 |
|
| 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 |
} |
| 309 |
|
| 310 |
if (*script->script_p == '/') |
| 311 |
{ |
| 312 |
|
| 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 |
} |
| 321 |
while(*script->script_p != '\n'); |
| 322 |
script->line++; |
| 323 |
script->script_p++; |
| 324 |
if (!*script->script_p) return 0; |
| 325 |
continue; |
| 326 |
} |
| 327 |
|
| 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 |
} |
| 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 |
} |
| 344 |
} |
| 345 |
break; |
| 346 |
} |
| 347 |
return 1; |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
int PS_ReadEscapeCharacter(script_t *script, char *ch) |
| 358 |
{ |
| 359 |
int c, val; |
| 360 |
|
| 361 |
|
| 362 |
script->script_p++; |
| 363 |
|
| 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 |
} |
| 389 |
script->script_p--; |
| 390 |
if (val > 0xFF) |
| 391 |
{ |
| 392 |
ScriptWarning(script, "too large value in escape character"); |
| 393 |
val = 0xFF; |
| 394 |
} |
| 395 |
c = val; |
| 396 |
break; |
| 397 |
} |
| 398 |
default: |
| 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 |
} |
| 408 |
script->script_p--; |
| 409 |
if (val > 0xFF) |
| 410 |
{ |
| 411 |
ScriptWarning(script, "too large value in escape character"); |
| 412 |
val = 0xFF; |
| 413 |
} |
| 414 |
c = val; |
| 415 |
break; |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
script->script_p++; |
| 420 |
|
| 421 |
*ch = c; |
| 422 |
|
| 423 |
return 1; |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 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 |
|
| 445 |
token->string[len++] = *script->script_p++; |
| 446 |
|
| 447 |
while(1) |
| 448 |
{ |
| 449 |
|
| 450 |
if (len >= MAX_TOKEN - 2) |
| 451 |
{ |
| 452 |
ScriptError(script, "string longer than MAX_TOKEN = %d", MAX_TOKEN); |
| 453 |
return 0; |
| 454 |
} |
| 455 |
|
| 456 |
|
| 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 |
} |
| 464 |
len++; |
| 465 |
} |
| 466 |
|
| 467 |
else if (*script->script_p == quote) |
| 468 |
{ |
| 469 |
|
| 470 |
script->script_p++; |
| 471 |
|
| 472 |
if (script->flags & SCFL_NOSTRINGWHITESPACES) break; |
| 473 |
|
| 474 |
tmpscript_p = script->script_p; |
| 475 |
tmpline = script->line; |
| 476 |
|
| 477 |
if (!PS_ReadWhiteSpace(script)) |
| 478 |
{ |
| 479 |
script->script_p = tmpscript_p; |
| 480 |
script->line = tmpline; |
| 481 |
break; |
| 482 |
} |
| 483 |
|
| 484 |
if (*script->script_p != quote) |
| 485 |
{ |
| 486 |
script->script_p = tmpscript_p; |
| 487 |
script->line = tmpline; |
| 488 |
break; |
| 489 |
} |
| 490 |
|
| 491 |
script->script_p++; |
| 492 |
} |
| 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 |
} |
| 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 |
} |
| 507 |
token->string[len++] = *script->script_p++; |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
token->string[len++] = quote; |
| 512 |
|
| 513 |
token->string[len] = '\0'; |
| 514 |
|
| 515 |
token->subtype = len; |
| 516 |
return 1; |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 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 |
} |
| 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 |
|
| 545 |
token->subtype = len; |
| 546 |
return 1; |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 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 |
|
| 562 |
if (subtype & TT_FLOAT) |
| 563 |
{ |
| 564 |
while(*string) |
| 565 |
{ |
| 566 |
if (*string == '.') |
| 567 |
{ |
| 568 |
if (dotfound) return; |
| 569 |
dotfound = 10; |
| 570 |
string++; |
| 571 |
} |
| 572 |
if (dotfound) |
| 573 |
{ |
| 574 |
*floatvalue = *floatvalue + (float) (*string - '0') / |
| 575 |
(float) dotfound; |
| 576 |
dotfound *= 10; |
| 577 |
} |
| 578 |
else |
| 579 |
{ |
| 580 |
*floatvalue = *floatvalue * 10.0 + (float) (*string - '0'); |
| 581 |
} |
| 582 |
string++; |
| 583 |
} |
| 584 |
*intvalue = (unsigned long) *floatvalue; |
| 585 |
} |
| 586 |
else if (subtype & TT_DECIMAL) |
| 587 |
{ |
| 588 |
while(*string) *intvalue = *intvalue * 10 + (*string++ - '0'); |
| 589 |
*floatvalue = *intvalue; |
| 590 |
} |
| 591 |
else if (subtype & TT_HEX) |
| 592 |
{ |
| 593 |
|
| 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 |
} |
| 603 |
*floatvalue = *intvalue; |
| 604 |
} |
| 605 |
else if (subtype & TT_OCTAL) |
| 606 |
{ |
| 607 |
|
| 608 |
string += 1; |
| 609 |
while(*string) *intvalue = (*intvalue << 3) + (*string++ - '0'); |
| 610 |
*floatvalue = *intvalue; |
| 611 |
} |
| 612 |
else if (subtype & TT_BINARY) |
| 613 |
{ |
| 614 |
|
| 615 |
string += 2; |
| 616 |
while(*string) *intvalue = (*intvalue << 1) + (*string++ - '0'); |
| 617 |
*floatvalue = *intvalue; |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 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 |
|
| 632 |
|
| 633 |
|
| 634 |
token->type = TT_NUMBER; |
| 635 |
|
| 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 |
|
| 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 |
} |
| 654 |
c = *script->script_p; |
| 655 |
} |
| 656 |
token->subtype |= TT_HEX; |
| 657 |
} |
| 658 |
#ifdef BINARYNUMBERS |
| 659 |
|
| 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 |
|
| 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 |
} |
| 676 |
c = *script->script_p; |
| 677 |
} |
| 678 |
token->subtype |= TT_BINARY; |
| 679 |
} |
| 680 |
#endif |
| 681 |
else |
| 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 |
} |
| 698 |
} |
| 699 |
if (octal) token->subtype |= TT_OCTAL; |
| 700 |
else token->subtype |= TT_DECIMAL; |
| 701 |
if (dot) token->subtype |= TT_FLOAT; |
| 702 |
} |
| 703 |
for (i = 0; i < 2; i++) |
| 704 |
{ |
| 705 |
c = *script->script_p; |
| 706 |
|
| 707 |
if ( (c == 'l' || c == 'L') |
| 708 |
&& !(token->subtype & TT_LONG)) |
| 709 |
{ |
| 710 |
script->script_p++; |
| 711 |
token->subtype |= TT_LONG; |
| 712 |
} |
| 713 |
|
| 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 |
} |
| 720 |
} |
| 721 |
token->string[len] = '\0'; |
| 722 |
#ifdef NUMBERVALUE |
| 723 |
NumberValue(token->string, token->subtype, &token->intvalue, &token->floatvalue); |
| 724 |
#endif |
| 725 |
if (!(token->subtype & TT_FLOAT)) token->subtype |= TT_INTEGER; |
| 726 |
return 1; |
| 727 |
} |
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
int PS_ReadLiteral(script_t *script, token_t *token) |
| 735 |
{ |
| 736 |
token->type = TT_LITERAL; |
| 737 |
|
| 738 |
token->string[0] = *script->script_p++; |
| 739 |
|
| 740 |
if (!*script->script_p) |
| 741 |
{ |
| 742 |
ScriptError(script, "end of file before trailing \'"); |
| 743 |
return 0; |
| 744 |
} |
| 745 |
|
| 746 |
if (*script->script_p == '\\') |
| 747 |
{ |
| 748 |
if (!PS_ReadEscapeCharacter(script, &token->string[1])) return 0; |
| 749 |
} |
| 750 |
else |
| 751 |
{ |
| 752 |
token->string[1] = *script->script_p++; |
| 753 |
} |
| 754 |
|
| 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 |
} |
| 764 |
if (*script->script_p == '\'') script->script_p++; |
| 765 |
} |
| 766 |
|
| 767 |
token->string[2] = *script->script_p++; |
| 768 |
|
| 769 |
token->string[3] = '\0'; |
| 770 |
|
| 771 |
token->subtype = token->string[1]; |
| 772 |
|
| 773 |
return 1; |
| 774 |
} |
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 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 |
| 797 |
p = punc->p; |
| 798 |
len = strlen(p); |
| 799 |
|
| 800 |
if (script->script_p + len <= script->end_p) |
| 801 |
{ |
| 802 |
|
| 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 |
|
| 809 |
token->subtype = punc->n; |
| 810 |
return 1; |
| 811 |
} |
| 812 |
} |
| 813 |
} |
| 814 |
return 0; |
| 815 |
} |
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 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 |
} |
| 834 |
token->string[len++] = *script->script_p++; |
| 835 |
} |
| 836 |
token->string[len] = 0; |
| 837 |
|
| 838 |
Com_Memcpy(&script->token, token, sizeof(token_t)); |
| 839 |
|
| 840 |
return 1; |
| 841 |
} |
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
int PS_ReadToken(script_t *script, token_t *token) |
| 849 |
{ |
| 850 |
|
| 851 |
if (script->tokenavailable) |
| 852 |
{ |
| 853 |
script->tokenavailable = 0; |
| 854 |
Com_Memcpy(token, &script->token, sizeof(token_t)); |
| 855 |
return 1; |
| 856 |
} |
| 857 |
|
| 858 |
script->lastscript_p = script->script_p; |
| 859 |
|
| 860 |
script->lastline = script->line; |
| 861 |
|
| 862 |
Com_Memset(token, 0, sizeof(token_t)); |
| 863 |
|
| 864 |
script->whitespace_p = script->script_p; |
| 865 |
token->whitespace_p = script->script_p; |
| 866 |
|
| 867 |
if (!PS_ReadWhiteSpace(script)) return 0; |
| 868 |
|
| 869 |
script->endwhitespace_p = script->script_p; |
| 870 |
token->endwhitespace_p = script->script_p; |
| 871 |
|
| 872 |
token->line = script->line; |
| 873 |
|
| 874 |
token->linescrossed = script->line - script->lastline; |
| 875 |
|
| 876 |
if (*script->script_p == '\"') |
| 877 |
{ |
| 878 |
if (!PS_ReadString(script, token, '\"')) return 0; |
| 879 |
} |
| 880 |
|
| 881 |
else if (*script->script_p == '\'') |
| 882 |
{ |
| 883 |
|
| 884 |
if (!PS_ReadString(script, token, '\'')) return 0; |
| 885 |
} |
| 886 |
|
| 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 |
} |
| 893 |
|
| 894 |
else if (script->flags & SCFL_PRIMITIVE) |
| 895 |
{ |
| 896 |
return PS_ReadPrimitive(script, token); |
| 897 |
} |
| 898 |
|
| 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 |
} |
| 905 |
|
| 906 |
else if (!PS_ReadPunctuation(script, token)) |
| 907 |
{ |
| 908 |
ScriptError(script, "can't read token"); |
| 909 |
return 0; |
| 910 |
} |
| 911 |
|
| 912 |
Com_Memcpy(&script->token, token, sizeof(token_t)); |
| 913 |
|
| 914 |
return 1; |
| 915 |
} |
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 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 |
} |
| 931 |
|
| 932 |
if (strcmp(token.string, string)) |
| 933 |
{ |
| 934 |
ScriptError(script, "expected %s, found %s", string, token.string); |
| 935 |
return 0; |
| 936 |
} |
| 937 |
return 1; |
| 938 |
} |
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 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 |
} |
| 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 |
} |
| 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 |
} |
| 982 |
} |
| 983 |
else if (token->type == TT_PUNCTUATION) |
| 984 |
{ |
| 985 |
if (subtype < 0) |
| 986 |
{ |
| 987 |
ScriptError(script, "BUG: wrong punctuation subtype"); |
| 988 |
return 0; |
| 989 |
} |
| 990 |
if (token->subtype != subtype) |
| 991 |
{ |
| 992 |
ScriptError(script, "expected %s, found %s", |
| 993 |
script->punctuations[subtype].p, token->string); |
| 994 |
return 0; |
| 995 |
} |
| 996 |
} |
| 997 |
return 1; |
| 998 |
} |
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 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 |
} |
| 1012 |
else |
| 1013 |
{ |
| 1014 |
return 1; |
| 1015 |
} |
| 1016 |
} |
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 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 |
|
| 1029 |
if (!strcmp(tok.string, string)) return 1; |
| 1030 |
|
| 1031 |
script->script_p = script->lastscript_p; |
| 1032 |
return 0; |
| 1033 |
} |
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 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 |
|
| 1046 |
if (tok.type == type && |
| 1047 |
(tok.subtype & subtype) == subtype) |
| 1048 |
{ |
| 1049 |
Com_Memcpy(token, &tok, sizeof(token_t)); |
| 1050 |
return 1; |
| 1051 |
} |
| 1052 |
|
| 1053 |
script->script_p = script->lastscript_p; |
| 1054 |
return 0; |
| 1055 |
} |
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 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 |
} |
| 1070 |
return 0; |
| 1071 |
} |
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
void PS_UnreadLastToken(script_t *script) |
| 1079 |
{ |
| 1080 |
script->tokenavailable = 1; |
| 1081 |
} |
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 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 |
} |
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
char PS_NextWhiteSpaceChar(script_t *script) |
| 1101 |
{ |
| 1102 |
if (script->whitespace_p != script->endwhitespace_p) |
| 1103 |
{ |
| 1104 |
return *script->whitespace_p++; |
| 1105 |
} |
| 1106 |
else |
| 1107 |
{ |
| 1108 |
return 0; |
| 1109 |
} |
| 1110 |
} |
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
void StripDoubleQuotes(char *string) |
| 1118 |
{ |
| 1119 |
if (*string == '\"') |
| 1120 |
{ |
| 1121 |
memmove(string, string+1, strlen(string)); |
| 1122 |
} |
| 1123 |
if (string[strlen(string)-1] == '\"') |
| 1124 |
{ |
| 1125 |
string[strlen(string)-1] = '\0'; |
| 1126 |
} |
| 1127 |
} |
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
void StripSingleQuotes(char *string) |
| 1135 |
{ |
| 1136 |
if (*string == '\'') |
| 1137 |
{ |
| 1138 |
memmove(string, string+1, strlen(string)); |
| 1139 |
} |
| 1140 |
if (string[strlen(string)-1] == '\'') |
| 1141 |
{ |
| 1142 |
string[strlen(string)-1] = '\0'; |
| 1143 |
} |
| 1144 |
} |
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 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 |
} |
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 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 |
} |
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
void SetScriptFlags(script_t *script, int flags) |
| 1214 |
{ |
| 1215 |
script->flags = flags; |
| 1216 |
} |
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
int GetScriptFlags(script_t *script) |
| 1224 |
{ |
| 1225 |
return script->flags; |
| 1226 |
} |
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
void ResetScript(script_t *script) |
| 1234 |
{ |
| 1235 |
|
| 1236 |
script->script_p = script->buffer; |
| 1237 |
|
| 1238 |
script->lastscript_p = script->buffer; |
| 1239 |
|
| 1240 |
script->whitespace_p = NULL; |
| 1241 |
|
| 1242 |
script->endwhitespace_p = NULL; |
| 1243 |
|
| 1244 |
script->tokenavailable = 0; |
| 1245 |
|
| 1246 |
script->line = 1; |
| 1247 |
script->lastline = 1; |
| 1248 |
|
| 1249 |
Com_Memset(&script->token, 0, sizeof(token_t)); |
| 1250 |
} |
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
int EndOfScript(script_t *script) |
| 1259 |
{ |
| 1260 |
return script->script_p >= script->end_p; |
| 1261 |
} |
| 1262 |
|
| 1263 |
|
| 1264 |
|
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
int NumLinesCrossed(script_t *script) |
| 1269 |
{ |
| 1270 |
return script->line - script->lastline; |
| 1271 |
} |
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 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 |
} |
| 1294 |
} |
| 1295 |
script->script_p++; |
| 1296 |
} while(1); |
| 1297 |
} |
| 1298 |
#ifndef BOTLIB |
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 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 |
} |
| 1317 |
#endif |
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 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 |
|
| 1358 |
script->script_p = script->buffer; |
| 1359 |
|
| 1360 |
script->lastscript_p = script->buffer; |
| 1361 |
|
| 1362 |
script->end_p = &script->buffer[length]; |
| 1363 |
|
| 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 |
} |
| 1380 |
fclose(fp); |
| 1381 |
#endif |
| 1382 |
|
| 1383 |
return script; |
| 1384 |
} |
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 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 |
|
| 1404 |
script->script_p = script->buffer; |
| 1405 |
|
| 1406 |
script->lastscript_p = script->buffer; |
| 1407 |
|
| 1408 |
script->end_p = &script->buffer[length]; |
| 1409 |
|
| 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 |
} |
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
void FreeScript(script_t *script) |
| 1428 |
{ |
| 1429 |
#ifdef PUNCTABLE |
| 1430 |
if (script->punctuationtable) FreeMemory(script->punctuationtable); |
| 1431 |
#endif |
| 1432 |
FreeMemory(script); |
| 1433 |
} |
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
void PS_SetBaseFolder(char *path) |
| 1441 |
{ |
| 1442 |
#ifdef BOTLIB |
| 1443 |
Com_sprintf(basefolder, sizeof(basefolder), "%s", path); |
| 1444 |
#endif |
| 1445 |
} |