| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#include "cmdlib.h" |
| 24 |
#include "inout.h" |
| 25 |
#include "scriplib.h" |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
typedef struct |
| 36 |
{ |
| 37 |
char filename[1024]; |
| 38 |
char *buffer,*script_p,*end_p; |
| 39 |
int line; |
| 40 |
} script_t; |
| 41 |
|
| 42 |
#define MAX_INCLUDES 8 |
| 43 |
script_t scriptstack[MAX_INCLUDES]; |
| 44 |
script_t *script; |
| 45 |
int scriptline; |
| 46 |
|
| 47 |
char token[MAXTOKEN]; |
| 48 |
qboolean endofscript; |
| 49 |
qboolean tokenready; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
void AddScriptToStack( char *filename ){ |
| 57 |
int size; |
| 58 |
|
| 59 |
script++; |
| 60 |
if ( script == &scriptstack[MAX_INCLUDES] ) { |
| 61 |
Error( "script file exceeded MAX_INCLUDES" ); |
| 62 |
} |
| 63 |
strcpy( script->filename, ExpandPath( filename ) ); |
| 64 |
|
| 65 |
size = LoadFile( script->filename, (void **)&script->buffer ); |
| 66 |
|
| 67 |
printf( "entering %s\n", script->filename ); |
| 68 |
|
| 69 |
script->line = 1; |
| 70 |
|
| 71 |
script->script_p = script->buffer; |
| 72 |
script->end_p = script->buffer + size; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
void LoadScriptFile( char *filename ){ |
| 82 |
script = scriptstack; |
| 83 |
AddScriptToStack( filename ); |
| 84 |
|
| 85 |
endofscript = false; |
| 86 |
tokenready = false; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
void ParseFromMemory( char *buffer, int size ){ |
| 96 |
script = scriptstack; |
| 97 |
script++; |
| 98 |
if ( script == &scriptstack[MAX_INCLUDES] ) { |
| 99 |
Error( "script file exceeded MAX_INCLUDES" ); |
| 100 |
} |
| 101 |
strcpy( script->filename, "memory buffer" ); |
| 102 |
|
| 103 |
script->buffer = buffer; |
| 104 |
script->line = 1; |
| 105 |
script->script_p = script->buffer; |
| 106 |
script->end_p = script->buffer + size; |
| 107 |
|
| 108 |
endofscript = false; |
| 109 |
tokenready = false; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
void UnGetToken( void ){ |
| 128 |
tokenready = true; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
qboolean EndOfScript( qboolean crossline ){ |
| 133 |
if ( !crossline ) { |
| 134 |
Error( "Line %i is incomplete\n",scriptline ); |
| 135 |
} |
| 136 |
|
| 137 |
if ( !strcmp( script->filename, "memory buffer" ) ) { |
| 138 |
endofscript = true; |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
free( script->buffer ); |
| 143 |
if ( script == scriptstack + 1 ) { |
| 144 |
endofscript = true; |
| 145 |
return false; |
| 146 |
} |
| 147 |
script--; |
| 148 |
scriptline = script->line; |
| 149 |
printf( "returning to %s\n", script->filename ); |
| 150 |
return GetToken( crossline ); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
qboolean GetToken( qboolean crossline ){ |
| 159 |
char *token_p; |
| 160 |
|
| 161 |
if ( tokenready ) { |
| 162 |
tokenready = false; |
| 163 |
return true; |
| 164 |
} |
| 165 |
|
| 166 |
if ( script->script_p >= script->end_p ) { |
| 167 |
return EndOfScript( crossline ); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
skipspace: |
| 174 |
while ( *script->script_p <= 32 ) |
| 175 |
{ |
| 176 |
if ( script->script_p >= script->end_p ) { |
| 177 |
return EndOfScript( crossline ); |
| 178 |
} |
| 179 |
if ( *script->script_p++ == '\n' ) { |
| 180 |
if ( !crossline ) { |
| 181 |
Error( "Line %i is incomplete\n",scriptline ); |
| 182 |
} |
| 183 |
scriptline = script->line++; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
if ( script->script_p >= script->end_p ) { |
| 188 |
return EndOfScript( crossline ); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
if ( *script->script_p == ';' || *script->script_p == '#' |
| 193 |
|| ( script->script_p[0] == '/' && script->script_p[1] == '/' ) ) { |
| 194 |
if ( !crossline ) { |
| 195 |
Error( "Line %i is incomplete\n",scriptline ); |
| 196 |
} |
| 197 |
while ( *script->script_p++ != '\n' ) |
| 198 |
if ( script->script_p >= script->end_p ) { |
| 199 |
return EndOfScript( crossline ); |
| 200 |
} |
| 201 |
goto skipspace; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
if ( script->script_p[0] == '/' && script->script_p[1] == '*' ) { |
| 206 |
if ( !crossline ) { |
| 207 |
Error( "Line %i is incomplete\n",scriptline ); |
| 208 |
} |
| 209 |
script->script_p += 2; |
| 210 |
while ( script->script_p[0] != '*' && script->script_p[1] != '/' ) |
| 211 |
{ |
| 212 |
script->script_p++; |
| 213 |
if ( script->script_p >= script->end_p ) { |
| 214 |
return EndOfScript( crossline ); |
| 215 |
} |
| 216 |
} |
| 217 |
script->script_p += 2; |
| 218 |
goto skipspace; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
token_p = token; |
| 225 |
|
| 226 |
if ( *script->script_p == '"' ) { |
| 227 |
|
| 228 |
script->script_p++; |
| 229 |
while ( *script->script_p != '"' ) |
| 230 |
{ |
| 231 |
*token_p++ = *script->script_p++; |
| 232 |
if ( script->script_p == script->end_p ) { |
| 233 |
break; |
| 234 |
} |
| 235 |
if ( token_p == &token[MAXTOKEN] ) { |
| 236 |
Error( "Token too large on line %i\n",scriptline ); |
| 237 |
} |
| 238 |
} |
| 239 |
script->script_p++; |
| 240 |
} |
| 241 |
else{ |
| 242 |
while ( *script->script_p > 32 && *script->script_p != ';' ) |
| 243 |
{ |
| 244 |
*token_p++ = *script->script_p++; |
| 245 |
if ( script->script_p == script->end_p ) { |
| 246 |
break; |
| 247 |
} |
| 248 |
if ( token_p == &token[MAXTOKEN] ) { |
| 249 |
Error( "Token too large on line %i\n",scriptline ); |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
*token_p = 0; |
| 255 |
|
| 256 |
if ( !strcmp( token, "$include" ) ) { |
| 257 |
GetToken( false ); |
| 258 |
AddScriptToStack( token ); |
| 259 |
return GetToken( crossline ); |
| 260 |
} |
| 261 |
|
| 262 |
return true; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
qboolean TokenAvailable( void ){ |
| 274 |
char *search_p; |
| 275 |
|
| 276 |
search_p = script->script_p; |
| 277 |
|
| 278 |
if ( search_p >= script->end_p ) { |
| 279 |
return false; |
| 280 |
} |
| 281 |
|
| 282 |
while ( *search_p <= 32 ) |
| 283 |
{ |
| 284 |
if ( *search_p == '\n' ) { |
| 285 |
return false; |
| 286 |
} |
| 287 |
search_p++; |
| 288 |
if ( search_p == script->end_p ) { |
| 289 |
return false; |
| 290 |
} |
| 291 |
|
| 292 |
} |
| 293 |
|
| 294 |
if ( *search_p == ';' ) { |
| 295 |
return false; |
| 296 |
} |
| 297 |
|
| 298 |
return true; |
| 299 |
} |