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