| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "input.h" |
| 9 |
#include "config.h" |
| 10 |
#include "alu.h" |
| 11 |
#include "dynabuf.h" |
| 12 |
#include "global.h" |
| 13 |
#include "platform.h" |
| 14 |
#include "section.h" |
| 15 |
#include "symbol.h" |
| 16 |
#include "tree.h" |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
enum inputstate { |
| 23 |
INPUTSTATE_SOF, |
| 24 |
INPUTSTATE_NORMAL, |
| 25 |
INPUTSTATE_AGAIN, |
| 26 |
INPUTSTATE_SKIPBLANKS, |
| 27 |
INPUTSTATE_LF, |
| 28 |
INPUTSTATE_CR, |
| 29 |
INPUTSTATE_SKIPLF, |
| 30 |
INPUTSTATE_COMMENT, |
| 31 |
INPUTSTATE_EOB, |
| 32 |
INPUTSTATE_EOF, |
| 33 |
INPUTSTATE_BOMCHECK, |
| 34 |
INPUTSTATE_BOMFAIL, |
| 35 |
}; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
const char FILE_READBINARY[] = "rb"; |
| 41 |
#define CHAR_LF (10) // line feed (in file) |
| 42 |
|
| 43 |
#define CHAR_CR (13) // carriage return (in file) |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
static struct input current_input = { |
| 49 |
{ |
| 50 |
"<none>", |
| 51 |
0, |
| 52 |
|
| 53 |
}, |
| 54 |
INPUTSRC_NONE, |
| 55 |
INPUTSTATE_EOF, |
| 56 |
{ |
| 57 |
NULL |
| 58 |
}, |
| 59 |
FALSE |
| 60 |
}; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
char GotByte; |
| 66 |
|
| 67 |
|
| 68 |
const char *input_plat_pathref_filename = ""; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
#define HEXBUFSIZE 9 // actually, 4+1 is enough, but for systems without snprintf(), let's be extra-safe. |
| 77 |
#define IF_WANTED_REPORT_SRCCHAR(c) do { if (current_input.report) report_srcchar(c); } while(0) |
| 78 |
|
| 79 |
static void report_printline(int line_number) |
| 80 |
{ |
| 81 |
int ii; |
| 82 |
char hex_address[HEXBUFSIZE]; |
| 83 |
char hexdump[2 * REPORT_BINBUFSIZE + 2 + 1]; |
| 84 |
|
| 85 |
|
| 86 |
if (report->bin_used == 0) { |
| 87 |
if ((report->asc_buf[0] == '\0') |
| 88 |
|| ((report->asc_buf[0] == ' ') && (report->asc_buf[1] == '\0'))) { |
| 89 |
report->asc_used = 0; |
| 90 |
return; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
fprintf(report->fd, "%6d ", line_number); |
| 98 |
|
| 99 |
if (report->bin_used) { |
| 100 |
#if _BSD_SOURCE || _XOPEN_SOURCE >= 500 || _ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L |
| 101 |
snprintf(hex_address, HEXBUFSIZE, "%04x", report->bin_address); |
| 102 |
#else |
| 103 |
sprintf(hex_address, "%04x", report->bin_address); |
| 104 |
#endif |
| 105 |
} else { |
| 106 |
hex_address[0] = '\0'; |
| 107 |
} |
| 108 |
|
| 109 |
hexdump[0] = '\0'; |
| 110 |
for (ii = 0; ii < report->bin_used; ++ii) |
| 111 |
sprintf(hexdump + 2 * ii, "%02x", (unsigned int) (unsigned char) (report->bin_buf[ii])); |
| 112 |
|
| 113 |
if (report->bin_used == REPORT_BINBUFSIZE) |
| 114 |
sprintf(hexdump + 2 * (REPORT_BINBUFSIZE - 1), "..."); |
| 115 |
|
| 116 |
fprintf(report->fd, "%-4s %-19s", hex_address, hexdump); |
| 117 |
|
| 118 |
|
| 119 |
if (report->asc_used == REPORT_ASCBUFSIZE) |
| 120 |
--report->asc_used; |
| 121 |
report->asc_buf[report->asc_used] = '\0'; |
| 122 |
fprintf(report->fd, "%s\n", report->asc_buf); |
| 123 |
report->asc_used = 0; |
| 124 |
report->bin_used = 0; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
static void report_flush(void) |
| 129 |
{ |
| 130 |
if (current_input.report) { |
| 131 |
report_printline(current_input.location.line_number); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
static void report_inputchange(const char prefix[], const char filename[]) |
| 137 |
{ |
| 138 |
if (current_input.report) { |
| 139 |
fprintf(report->fd, "\n; ******** %s%s\n", prefix, filename); |
| 140 |
report->new_input = TRUE; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
static void report_srcchar(int new_char) |
| 145 |
{ |
| 146 |
static char prev_char = '\0'; |
| 147 |
|
| 148 |
if (new_char == EOF) |
| 149 |
new_char = '\n'; |
| 150 |
|
| 151 |
if (report->new_input) { |
| 152 |
report->new_input = FALSE; |
| 153 |
prev_char = '\0'; |
| 154 |
} |
| 155 |
if (prev_char == '\n') { |
| 156 |
report_printline(current_input.location.line_number - 1); |
| 157 |
} |
| 158 |
if (new_char != '\n' && new_char != '\r') { |
| 159 |
if (report->asc_used < REPORT_ASCBUFSIZE) |
| 160 |
report->asc_buf[report->asc_used++] = new_char; |
| 161 |
} |
| 162 |
prev_char = new_char; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
#define UTF8BOM_1ST 0xef |
| 171 |
#define UTF8BOM_2ND 0xbb |
| 172 |
#define UTF8BOM_3RD 0xbf |
| 173 |
|
| 174 |
static char get_processed_from_file(void) |
| 175 |
{ |
| 176 |
static int from_file = 0; |
| 177 |
|
| 178 |
for (;;) { |
| 179 |
switch (current_input.state) { |
| 180 |
case INPUTSTATE_SOF: |
| 181 |
|
| 182 |
from_file = getc(current_input.u.fd); |
| 183 |
|
| 184 |
if (from_file == UTF8BOM_1ST) { |
| 185 |
current_input.state = INPUTSTATE_BOMCHECK; |
| 186 |
break; |
| 187 |
} |
| 188 |
IF_WANTED_REPORT_SRCCHAR(from_file); |
| 189 |
|
| 190 |
if (from_file == '#') { |
| 191 |
|
| 192 |
current_input.state = INPUTSTATE_COMMENT; |
| 193 |
return CHAR_EOS; |
| 194 |
} |
| 195 |
current_input.state = INPUTSTATE_AGAIN; |
| 196 |
break; |
| 197 |
case INPUTSTATE_NORMAL: |
| 198 |
|
| 199 |
from_file = getc(current_input.u.fd); |
| 200 |
IF_WANTED_REPORT_SRCCHAR(from_file); |
| 201 |
|
| 202 |
|
| 203 |
case INPUTSTATE_AGAIN: |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
current_input.state = INPUTSTATE_NORMAL; |
| 211 |
|
| 212 |
|
| 213 |
if (from_file == EOF) { |
| 214 |
|
| 215 |
current_input.state = INPUTSTATE_EOF; |
| 216 |
return CHAR_EOS; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
if (BYTE_IS_SYNTAX_CHAR(from_file) == 0) |
| 222 |
return (char) from_file; |
| 223 |
|
| 224 |
|
| 225 |
switch (from_file) { |
| 226 |
case '\t': |
| 227 |
case ' ': |
| 228 |
|
| 229 |
current_input.state = INPUTSTATE_SKIPBLANKS; |
| 230 |
return ' '; |
| 231 |
|
| 232 |
case CHAR_LF: |
| 233 |
|
| 234 |
current_input.state = INPUTSTATE_LF; |
| 235 |
return CHAR_EOS; |
| 236 |
|
| 237 |
case CHAR_CR: |
| 238 |
|
| 239 |
current_input.state = INPUTSTATE_CR; |
| 240 |
return CHAR_EOS; |
| 241 |
|
| 242 |
case CHAR_EOB: |
| 243 |
|
| 244 |
current_input.state = INPUTSTATE_EOB; |
| 245 |
return CHAR_EOS; |
| 246 |
|
| 247 |
case '/': |
| 248 |
|
| 249 |
from_file = getc(current_input.u.fd); |
| 250 |
IF_WANTED_REPORT_SRCCHAR(from_file); |
| 251 |
if (from_file != '/') { |
| 252 |
|
| 253 |
current_input.state = INPUTSTATE_AGAIN; |
| 254 |
return '/'; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
case ';': |
| 259 |
|
| 260 |
current_input.state = INPUTSTATE_COMMENT; |
| 261 |
return CHAR_EOS; |
| 262 |
|
| 263 |
case ':': |
| 264 |
|
| 265 |
return CHAR_EOS; |
| 266 |
|
| 267 |
default: |
| 268 |
|
| 269 |
throw_error("Source file contains illegal character."); |
| 270 |
return (char) from_file; |
| 271 |
} |
| 272 |
case INPUTSTATE_SKIPBLANKS: |
| 273 |
|
| 274 |
do { |
| 275 |
from_file = getc(current_input.u.fd); |
| 276 |
IF_WANTED_REPORT_SRCCHAR(from_file); |
| 277 |
} while ((from_file == '\t') || (from_file == ' ')); |
| 278 |
|
| 279 |
current_input.state = INPUTSTATE_AGAIN; |
| 280 |
break; |
| 281 |
case INPUTSTATE_LF: |
| 282 |
|
| 283 |
current_input.state = INPUTSTATE_NORMAL; |
| 284 |
return CHAR_SOL; |
| 285 |
|
| 286 |
case INPUTSTATE_CR: |
| 287 |
|
| 288 |
current_input.state = INPUTSTATE_SKIPLF; |
| 289 |
return CHAR_SOL; |
| 290 |
|
| 291 |
case INPUTSTATE_SKIPLF: |
| 292 |
from_file = getc(current_input.u.fd); |
| 293 |
IF_WANTED_REPORT_SRCCHAR(from_file); |
| 294 |
|
| 295 |
|
| 296 |
if (from_file == CHAR_LF) |
| 297 |
current_input.state = INPUTSTATE_NORMAL; |
| 298 |
else |
| 299 |
current_input.state = INPUTSTATE_AGAIN; |
| 300 |
break; |
| 301 |
case INPUTSTATE_COMMENT: |
| 302 |
|
| 303 |
do { |
| 304 |
from_file = getc(current_input.u.fd); |
| 305 |
IF_WANTED_REPORT_SRCCHAR(from_file); |
| 306 |
} while ((from_file != EOF) && (from_file != CHAR_CR) && (from_file != CHAR_LF)); |
| 307 |
|
| 308 |
current_input.state = INPUTSTATE_AGAIN; |
| 309 |
break; |
| 310 |
case INPUTSTATE_EOB: |
| 311 |
|
| 312 |
current_input.state = INPUTSTATE_NORMAL; |
| 313 |
return CHAR_EOB; |
| 314 |
|
| 315 |
case INPUTSTATE_EOF: |
| 316 |
|
| 317 |
current_input.state = INPUTSTATE_NORMAL; |
| 318 |
return CHAR_EOF; |
| 319 |
|
| 320 |
|
| 321 |
case INPUTSTATE_BOMCHECK: |
| 322 |
|
| 323 |
from_file = getc(current_input.u.fd); |
| 324 |
if (from_file != UTF8BOM_2ND) { |
| 325 |
|
| 326 |
IF_WANTED_REPORT_SRCCHAR(UTF8BOM_1ST); |
| 327 |
IF_WANTED_REPORT_SRCCHAR(from_file); |
| 328 |
current_input.state = INPUTSTATE_AGAIN; |
| 329 |
return UTF8BOM_1ST; |
| 330 |
} |
| 331 |
|
| 332 |
from_file = getc(current_input.u.fd); |
| 333 |
if (from_file != UTF8BOM_3RD) { |
| 334 |
|
| 335 |
IF_WANTED_REPORT_SRCCHAR(UTF8BOM_1ST); |
| 336 |
IF_WANTED_REPORT_SRCCHAR(UTF8BOM_2ND); |
| 337 |
IF_WANTED_REPORT_SRCCHAR(from_file); |
| 338 |
current_input.state = INPUTSTATE_BOMFAIL; |
| 339 |
return UTF8BOM_1ST; |
| 340 |
} |
| 341 |
|
| 342 |
current_input.state = INPUTSTATE_NORMAL; |
| 343 |
break; |
| 344 |
|
| 345 |
case INPUTSTATE_BOMFAIL: |
| 346 |
|
| 347 |
|
| 348 |
current_input.state = INPUTSTATE_AGAIN; |
| 349 |
return UTF8BOM_2ND; |
| 350 |
|
| 351 |
default: |
| 352 |
BUG("StrangeInputMode", current_input.state); |
| 353 |
} |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
int subst_chars_left = 0; |
| 358 |
boolean subst_enabled = TRUE; |
| 359 |
const char *subst_read_ptr = NULL; |
| 360 |
STRUCT_DYNABUF_REF(subst_buffer, 40); |
| 361 |
|
| 362 |
|
| 363 |
static char subst_get_char(void) |
| 364 |
{ |
| 365 |
if (subst_chars_left-- == 0) |
| 366 |
BUG("NoSubstLeft", 0); |
| 367 |
return *(subst_read_ptr++); |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
static void subst_substitute(void) |
| 379 |
{ |
| 380 |
scope_t tmp_scope; |
| 381 |
struct symbol *tmp_symbol; |
| 382 |
boolean parenthesized; |
| 383 |
|
| 384 |
if (GotByte != '?') |
| 385 |
BUG("NotQuestionMark", GotByte); |
| 386 |
|
| 387 |
GetByte(); |
| 388 |
|
| 389 |
if (GotByte == '(') { |
| 390 |
GetByte(); |
| 391 |
parenthesized = TRUE; |
| 392 |
} else { |
| 393 |
parenthesized = FALSE; |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
dynabuf_clear(subst_buffer); |
| 399 |
dynabuf_add_bytes(subst_buffer, GlobalDynaBuf->buffer, GlobalDynaBuf->size); |
| 400 |
|
| 401 |
|
| 402 |
if (input_read_scope_and_symbol_name(&tmp_scope)) { |
| 403 |
tmp_symbol = NULL; |
| 404 |
} else { |
| 405 |
tmp_symbol = symbol_find(tmp_scope); |
| 406 |
tmp_symbol->has_been_read = TRUE; |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
dynabuf_clear(GlobalDynaBuf); |
| 411 |
dynabuf_add_bytes(GlobalDynaBuf, subst_buffer->buffer, subst_buffer->size); |
| 412 |
|
| 413 |
|
| 414 |
dynabuf_clear(subst_buffer); |
| 415 |
if (tmp_symbol == NULL) { |
| 416 |
goto fail; |
| 417 |
} |
| 418 |
if (tmp_symbol->object.type == NULL) { |
| 419 |
throw_error("Substitution symbol is undefined."); |
| 420 |
|
| 421 |
goto fail; |
| 422 |
} else if (tmp_symbol->object.type == &type_number) { |
| 423 |
if (tmp_symbol->object.u.number.ntype != NUMTYPE_INT) { |
| 424 |
throw_error("Substitution symbol is undefined or not integer."); |
| 425 |
goto fail; |
| 426 |
} |
| 427 |
dynabuf_add_signed_long(subst_buffer, (long) tmp_symbol->object.u.number.val.intval); |
| 428 |
} else if (tmp_symbol->object.type == &type_string) { |
| 429 |
dynabuf_add_bytes(subst_buffer, tmp_symbol->object.u.string->payload, tmp_symbol->object.u.string->length); |
| 430 |
} else { |
| 431 |
throw_error("Substitution symbol is neither number nor string."); |
| 432 |
goto fail; |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
if (parenthesized) { |
| 437 |
SKIPSPACE(); |
| 438 |
if (GotByte == ')') { |
| 439 |
GetByte(); |
| 440 |
} else { |
| 441 |
throw_error("Substitution does not end with ')' character."); |
| 442 |
goto fail; |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
dynabuf_append(subst_buffer, GotByte); |
| 448 |
subst_chars_left = subst_buffer->size; |
| 449 |
subst_read_ptr = subst_buffer->buffer; |
| 450 |
|
| 451 |
|
| 452 |
return; |
| 453 |
fail: |
| 454 |
dynabuf_clear(subst_buffer); |
| 455 |
dynabuf_append(subst_buffer, GotByte); |
| 456 |
subst_chars_left = subst_buffer->size; |
| 457 |
subst_read_ptr = subst_buffer->buffer; |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
char GetByte(void) |
| 465 |
{ |
| 466 |
|
| 467 |
|
| 468 |
if (subst_chars_left) { |
| 469 |
GotByte = subst_get_char(); |
| 470 |
} else { |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
switch (current_input.srctype) { |
| 479 |
case INPUTSRC_RAM: |
| 480 |
GotByte = *(current_input.u.ram_ptr++); |
| 481 |
if (current_input.state== INPUTSTATE_NORMAL) |
| 482 |
IF_WANTED_REPORT_SRCCHAR(GotByte); |
| 483 |
break; |
| 484 |
case INPUTSRC_FILE: |
| 485 |
GotByte = get_processed_from_file(); |
| 486 |
break; |
| 487 |
default: |
| 488 |
BUG("IllegalInputSrc", current_input.srctype); |
| 489 |
} |
| 490 |
|
| 491 |
if (GotByte == CHAR_SOL) |
| 492 |
current_input.location.line_number++; |
| 493 |
} |
| 494 |
|
| 495 |
if ((GotByte == '?') && (subst_enabled) && (subst_chars_left == 0)) { |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
subst_enabled = FALSE; |
| 509 |
subst_substitute(); |
| 510 |
subst_enabled = TRUE; |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
GotByte = subst_get_char(); |
| 515 |
} |
| 516 |
return GotByte; |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
static void get_quoted_byte(void) |
| 523 |
{ |
| 524 |
int from_file; |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
if (subst_chars_left) { |
| 529 |
GotByte = subst_get_char(); |
| 530 |
return; |
| 531 |
} |
| 532 |
|
| 533 |
switch (current_input.srctype) { |
| 534 |
case INPUTSRC_RAM: |
| 535 |
|
| 536 |
|
| 537 |
GotByte = *(current_input.u.ram_ptr++); |
| 538 |
IF_WANTED_REPORT_SRCCHAR(GotByte); |
| 539 |
break; |
| 540 |
case INPUTSRC_FILE: |
| 541 |
|
| 542 |
from_file = getc(current_input.u.fd); |
| 543 |
IF_WANTED_REPORT_SRCCHAR(from_file); |
| 544 |
switch (from_file) { |
| 545 |
case EOF: |
| 546 |
|
| 547 |
current_input.state = INPUTSTATE_EOF; |
| 548 |
GotByte = CHAR_EOS; |
| 549 |
break; |
| 550 |
case CHAR_LF: |
| 551 |
|
| 552 |
current_input.state = INPUTSTATE_LF; |
| 553 |
GotByte = CHAR_EOS; |
| 554 |
break; |
| 555 |
case CHAR_CR: |
| 556 |
|
| 557 |
current_input.state = INPUTSTATE_CR; |
| 558 |
GotByte = CHAR_EOS; |
| 559 |
break; |
| 560 |
default: |
| 561 |
GotByte = from_file; |
| 562 |
} |
| 563 |
break; |
| 564 |
default: |
| 565 |
BUG("IllegalInputSrc", current_input.srctype); |
| 566 |
} |
| 567 |
|
| 568 |
if (GotByte == CHAR_EOS) |
| 569 |
throw_error("Quotes still open at end of line."); |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
void parser_ensure_EOS(void) |
| 575 |
{ |
| 576 |
SKIPSPACE(); |
| 577 |
if (GotByte) { |
| 578 |
|
| 579 |
char buf[80]; |
| 580 |
char quote; |
| 581 |
|
| 582 |
|
| 583 |
quote = (GotByte == '\'') ? '"' : '\''; |
| 584 |
sprintf(buf, "Expected end-of-statement, found %c%c%c instead.", quote, GotByte, quote); |
| 585 |
throw_error(buf); |
| 586 |
parser_skip_remainder(); |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
static int quoted_to_dynabuf(char closing_quote) |
| 593 |
{ |
| 594 |
boolean escaped = FALSE; |
| 595 |
|
| 596 |
|
| 597 |
for (;;) { |
| 598 |
get_quoted_byte(); |
| 599 |
if (GotByte == CHAR_EOS) |
| 600 |
return 1; |
| 601 |
|
| 602 |
if (escaped) { |
| 603 |
|
| 604 |
escaped = FALSE; |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
} else { |
| 611 |
|
| 612 |
if (GotByte == closing_quote) |
| 613 |
return 0; |
| 614 |
|
| 615 |
if (GotByte == '\\') { |
| 616 |
if (config.dialect >= V0_97__BACKSLASH_ESCAPING) { |
| 617 |
escaped = TRUE; |
| 618 |
} else { |
| 619 |
|
| 620 |
} |
| 621 |
} |
| 622 |
} |
| 623 |
DYNABUF_APPEND(GlobalDynaBuf, GotByte); |
| 624 |
} |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
static int shift_hex_nibble(char *target, char digit) |
| 630 |
{ |
| 631 |
if (digit >= '0' && digit <= '9') { |
| 632 |
digit -= '0'; |
| 633 |
} else if (digit >= 'a' && digit <= 'f') { |
| 634 |
digit = digit - 'a' + 10; |
| 635 |
} else if (digit >= 'A' && digit <= 'F') { |
| 636 |
digit = digit - 'A' + 10; |
| 637 |
} else { |
| 638 |
throw_error("Invalid hex digit after \\x, expected 0-9/a-f."); |
| 639 |
return 1; |
| 640 |
} |
| 641 |
*target = (*target << 4) | digit; |
| 642 |
return 0; |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
enum escape { |
| 647 |
ESCAPE_NONE, |
| 648 |
ESCAPE_BACKSLASH, |
| 649 |
ESCAPE_HEX1, |
| 650 |
ESCAPE_HEX2, |
| 651 |
}; |
| 652 |
|
| 653 |
|
| 654 |
int unescape_dynabuf(void) |
| 655 |
{ |
| 656 |
int read_index = 0, |
| 657 |
write_index = 0; |
| 658 |
char byte; |
| 659 |
enum escape escape_state = ESCAPE_NONE; |
| 660 |
char hexbyte = 0; |
| 661 |
int err = 0; |
| 662 |
|
| 663 |
|
| 664 |
while (read_index < GlobalDynaBuf->size) { |
| 665 |
byte = GLOBALDYNABUF_CURRENT[read_index++]; |
| 666 |
switch (escape_state) { |
| 667 |
case ESCAPE_NONE: |
| 668 |
if (byte == '\\') { |
| 669 |
escape_state = ESCAPE_BACKSLASH; |
| 670 |
} else { |
| 671 |
GLOBALDYNABUF_CURRENT[write_index++] = byte; |
| 672 |
} |
| 673 |
break; |
| 674 |
case ESCAPE_BACKSLASH: |
| 675 |
escape_state = ESCAPE_NONE; |
| 676 |
switch (byte) { |
| 677 |
case '\\': |
| 678 |
case '\'': |
| 679 |
case '"': |
| 680 |
break; |
| 681 |
case '0': |
| 682 |
byte = 0; |
| 683 |
break; |
| 684 |
case 't': |
| 685 |
byte = 9; |
| 686 |
break; |
| 687 |
case 'n': |
| 688 |
byte = 10; |
| 689 |
break; |
| 690 |
case 'r': |
| 691 |
byte = 13; |
| 692 |
break; |
| 693 |
case 'x': |
| 694 |
escape_state = ESCAPE_HEX1; |
| 695 |
|
| 696 |
break; |
| 697 |
|
| 698 |
default: |
| 699 |
throw_error("Unsupported backslash sequence."); |
| 700 |
err = 1; |
| 701 |
} |
| 702 |
GLOBALDYNABUF_CURRENT[write_index++] = byte; |
| 703 |
break; |
| 704 |
case ESCAPE_HEX1: |
| 705 |
hexbyte = 0; |
| 706 |
if (shift_hex_nibble(&hexbyte, byte)) { |
| 707 |
escape_state = ESCAPE_NONE; |
| 708 |
err = 1; |
| 709 |
} else { |
| 710 |
escape_state = ESCAPE_HEX2; |
| 711 |
} |
| 712 |
break; |
| 713 |
case ESCAPE_HEX2: |
| 714 |
if (shift_hex_nibble(&hexbyte, byte)) { |
| 715 |
err = 1; |
| 716 |
} else { |
| 717 |
|
| 718 |
GLOBALDYNABUF_CURRENT[write_index - 1] = hexbyte; |
| 719 |
} |
| 720 |
escape_state = ESCAPE_NONE; |
| 721 |
break; |
| 722 |
default: |
| 723 |
BUG("StrangeEscapeState1", escape_state); |
| 724 |
} |
| 725 |
} |
| 726 |
GlobalDynaBuf->size = write_index; |
| 727 |
switch (escape_state) { |
| 728 |
case ESCAPE_NONE: |
| 729 |
break; |
| 730 |
case ESCAPE_BACKSLASH: |
| 731 |
BUG("PartialEscapeSequence", 0); |
| 732 |
case ESCAPE_HEX1: |
| 733 |
case ESCAPE_HEX2: |
| 734 |
throw_error("Expected two hex digits after \\x sequence."); |
| 735 |
err = 1; |
| 736 |
break; |
| 737 |
default: |
| 738 |
BUG("StrangeEscapeState2", escape_state); |
| 739 |
} |
| 740 |
return err; |
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
int input_read_string_literal(char closing_quote) |
| 748 |
{ |
| 749 |
dynabuf_clear(GlobalDynaBuf); |
| 750 |
if (quoted_to_dynabuf(closing_quote)) |
| 751 |
return 1; |
| 752 |
|
| 753 |
|
| 754 |
GetByte(); |
| 755 |
|
| 756 |
|
| 757 |
if (config.dialect >= V0_97__BACKSLASH_ESCAPING) { |
| 758 |
return unescape_dynabuf(); |
| 759 |
} else { |
| 760 |
return 0; |
| 761 |
} |
| 762 |
} |
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
void parser_skip_remainder(void) |
| 768 |
{ |
| 769 |
|
| 770 |
|
| 771 |
dynabuf_clear(GlobalDynaBuf); |
| 772 |
while (GotByte != CHAR_EOS) { |
| 773 |
|
| 774 |
if ((GotByte == '"') || (GotByte == '\'')) { |
| 775 |
if (quoted_to_dynabuf(GotByte)) |
| 776 |
break; |
| 777 |
} |
| 778 |
GetByte(); |
| 779 |
} |
| 780 |
dynabuf_clear(GlobalDynaBuf); |
| 781 |
} |
| 782 |
|
| 783 |
|
| 784 |
void input_read_statement(char terminator) |
| 785 |
{ |
| 786 |
int err; |
| 787 |
|
| 788 |
SKIPSPACE(); |
| 789 |
dynabuf_clear(GlobalDynaBuf); |
| 790 |
while ((GotByte != terminator) && (GotByte != CHAR_EOS)) { |
| 791 |
|
| 792 |
DYNABUF_APPEND(GlobalDynaBuf, GotByte); |
| 793 |
if ((GotByte == '"') || (GotByte == '\'')) { |
| 794 |
err = quoted_to_dynabuf(GotByte); |
| 795 |
|
| 796 |
DYNABUF_APPEND(GlobalDynaBuf, GotByte); |
| 797 |
if (err) |
| 798 |
break; |
| 799 |
} |
| 800 |
GetByte(); |
| 801 |
} |
| 802 |
dynabuf_append(GlobalDynaBuf, CHAR_EOS); |
| 803 |
} |
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
static void block_to_dynabuf(void) |
| 810 |
{ |
| 811 |
char byte; |
| 812 |
int depth = 1; |
| 813 |
boolean substflagbuf = subst_enabled; |
| 814 |
|
| 815 |
|
| 816 |
subst_enabled = FALSE; |
| 817 |
|
| 818 |
|
| 819 |
dynabuf_clear(GlobalDynaBuf); |
| 820 |
do { |
| 821 |
byte = GetByte(); |
| 822 |
|
| 823 |
DYNABUF_APPEND(GlobalDynaBuf, byte); |
| 824 |
|
| 825 |
switch (byte) { |
| 826 |
case CHAR_EOF: |
| 827 |
throw_serious_error(exception_no_right_brace); |
| 828 |
|
| 829 |
case '"': |
| 830 |
case '\'': |
| 831 |
quoted_to_dynabuf(byte); |
| 832 |
DYNABUF_APPEND(GlobalDynaBuf, GotByte); |
| 833 |
break; |
| 834 |
case CHAR_SOB: |
| 835 |
++depth; |
| 836 |
break; |
| 837 |
case CHAR_EOB: |
| 838 |
--depth; |
| 839 |
break; |
| 840 |
} |
| 841 |
} while (depth); |
| 842 |
|
| 843 |
|
| 844 |
subst_enabled = substflagbuf; |
| 845 |
} |
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
void input_block_skip(void) |
| 850 |
{ |
| 851 |
block_to_dynabuf(); |
| 852 |
} |
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
void input_block_getcopy(struct block *block) |
| 858 |
{ |
| 859 |
|
| 860 |
block->line_number = current_input.location.line_number; |
| 861 |
|
| 862 |
block_to_dynabuf(); |
| 863 |
|
| 864 |
dynabuf_append(GlobalDynaBuf, CHAR_EOS); |
| 865 |
dynabuf_append(GlobalDynaBuf, CHAR_EOF); |
| 866 |
|
| 867 |
block->body = dynabuf_get_copy(GlobalDynaBuf); |
| 868 |
} |
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
static int append_keyword_to_global_dynabuf(const char error_for_zero_length[]) |
| 874 |
{ |
| 875 |
int length = 0; |
| 876 |
|
| 877 |
|
| 878 |
while (BYTE_CONTINUES_KEYWORD(GotByte)) { |
| 879 |
DYNABUF_APPEND(GlobalDynaBuf, GotByte); |
| 880 |
++length; |
| 881 |
GetByte(); |
| 882 |
} |
| 883 |
if (length == 0) |
| 884 |
throw_error(error_for_zero_length); |
| 885 |
return length; |
| 886 |
} |
| 887 |
|
| 888 |
const char exception_no_symbol_name[] = "Expected symbol name."; |
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
int input_append_symbol_name_to_global_dynabuf(void) |
| 894 |
{ |
| 895 |
if ((GotByte == LOCAL_PREFIX) |
| 896 |
|| (GotByte == CHEAP_PREFIX)) { |
| 897 |
dynabuf_append(GlobalDynaBuf, GotByte); |
| 898 |
GetByte(); |
| 899 |
} else if (!BYTE_STARTS_KEYWORD(GotByte)) { |
| 900 |
|
| 901 |
throw_error(exception_no_symbol_name); |
| 902 |
return 1; |
| 903 |
} |
| 904 |
return append_keyword_to_global_dynabuf(exception_no_symbol_name) == 0; |
| 905 |
} |
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
int input_readscopeandsymbolname(scope_t *scope, boolean dotkluge) |
| 910 |
{ |
| 911 |
int err; |
| 912 |
|
| 913 |
SKIPSPACE(); |
| 914 |
dynabuf_clear(GlobalDynaBuf); |
| 915 |
|
| 916 |
if (dotkluge) { |
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
dynabuf_append(GlobalDynaBuf, '.'); |
| 921 |
err = append_keyword_to_global_dynabuf(exception_no_symbol_name) == 0; |
| 922 |
} else { |
| 923 |
err = input_append_symbol_name_to_global_dynabuf(); |
| 924 |
} |
| 925 |
|
| 926 |
dynabuf_append(GlobalDynaBuf, '\0'); |
| 927 |
if (err) { |
| 928 |
*scope = SCOPE_GLOBAL; |
| 929 |
return 1; |
| 930 |
} |
| 931 |
if (GLOBALDYNABUF_CURRENT[0] == LOCAL_PREFIX) { |
| 932 |
*scope = section_now->local_scope; |
| 933 |
} else if (GLOBALDYNABUF_CURRENT[0] == CHEAP_PREFIX) { |
| 934 |
*scope = section_now->cheap_scope; |
| 935 |
} else { |
| 936 |
*scope = SCOPE_GLOBAL; |
| 937 |
} |
| 938 |
return 0; |
| 939 |
} |
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
int parser_read_keyword(void) |
| 946 |
{ |
| 947 |
int length; |
| 948 |
|
| 949 |
dynabuf_clear(GlobalDynaBuf); |
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
length = append_keyword_to_global_dynabuf("Expected keyword or symbol name."); |
| 954 |
|
| 955 |
dynabuf_append(GlobalDynaBuf, '\0'); |
| 956 |
return length; |
| 957 |
} |
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
int parser_read_and_lower_keyword(void) |
| 964 |
{ |
| 965 |
int length; |
| 966 |
|
| 967 |
dynabuf_clear(GlobalDynaBuf); |
| 968 |
length = append_keyword_to_global_dynabuf("Expected keyword."); |
| 969 |
|
| 970 |
dynabuf_append(GlobalDynaBuf, '\0'); |
| 971 |
dynabuf_to_lower(GlobalDynaBuf, GlobalDynaBuf); |
| 972 |
return length; |
| 973 |
} |
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
static int read_filename_shared_end(boolean *absolute) |
| 982 |
{ |
| 983 |
|
| 984 |
if (GlobalDynaBuf->size == 0) { |
| 985 |
throw_error("No file name given."); |
| 986 |
return 1; |
| 987 |
} |
| 988 |
|
| 989 |
|
| 990 |
dynabuf_append(GlobalDynaBuf, '\0'); |
| 991 |
|
| 992 |
|
| 993 |
dynabuf_append(GlobalDynaBuf, '\0'); |
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
platform_convert_path(absolute, GLOBALDYNABUF_CURRENT); |
| 1001 |
|
| 1002 |
return 0; |
| 1003 |
} |
| 1004 |
|
| 1005 |
|
| 1006 |
static int get_string(void) |
| 1007 |
{ |
| 1008 |
struct object object; |
| 1009 |
|
| 1010 |
if (config.dialect >= V0_97__BACKSLASH_ESCAPING) { |
| 1011 |
|
| 1012 |
ALU_any_result(&object); |
| 1013 |
if (object.type == &type_string) { |
| 1014 |
dynabuf_clear(GlobalDynaBuf); |
| 1015 |
dynabuf_add_bytes(GlobalDynaBuf, object.u.string->payload, object.u.string->length); |
| 1016 |
} else { |
| 1017 |
throw_error("Expression did not return a string."); |
| 1018 |
return 1; |
| 1019 |
} |
| 1020 |
} else { |
| 1021 |
|
| 1022 |
if (GotByte != '"') { |
| 1023 |
throw_error("Quotes not found."); |
| 1024 |
return 1; |
| 1025 |
} |
| 1026 |
if (input_read_string_literal('"')) { |
| 1027 |
return 1; |
| 1028 |
} |
| 1029 |
} |
| 1030 |
return 0; |
| 1031 |
} |
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
int input_read_input_filename(struct filespecflags *flags) |
| 1043 |
{ |
| 1044 |
SKIPSPACE(); |
| 1045 |
if (GotByte == '<') { |
| 1046 |
|
| 1047 |
flags->uses_lib = TRUE; |
| 1048 |
if (input_read_string_literal('>')) |
| 1049 |
return 1; |
| 1050 |
} else { |
| 1051 |
|
| 1052 |
flags->uses_lib = FALSE; |
| 1053 |
if (get_string()) |
| 1054 |
return 1; |
| 1055 |
} |
| 1056 |
|
| 1057 |
return read_filename_shared_end(&flags->absolute); |
| 1058 |
} |
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
int parser_accept_comma(void) |
| 1063 |
{ |
| 1064 |
SKIPSPACE(); |
| 1065 |
if (GotByte != ',') |
| 1066 |
return FALSE; |
| 1067 |
|
| 1068 |
NEXTANDSKIPSPACE(); |
| 1069 |
return TRUE; |
| 1070 |
} |
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
int parser_expect(int chr) |
| 1076 |
{ |
| 1077 |
|
| 1078 |
|
| 1079 |
if (GotByte == chr) { |
| 1080 |
GetByte(); |
| 1081 |
return TRUE; |
| 1082 |
} |
| 1083 |
throw_error(exception_syntax); |
| 1084 |
return FALSE; |
| 1085 |
} |
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
void input_force_eof(void) |
| 1090 |
{ |
| 1091 |
current_input.state = INPUTSTATE_EOF; |
| 1092 |
} |
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
void input_set_report_enabled(boolean new_state) |
| 1098 |
{ |
| 1099 |
current_input.report = new_state; |
| 1100 |
} |
| 1101 |
|
| 1102 |
|
| 1103 |
static STRUCT_DYNABUF_REF(pathbuf, 256); |
| 1104 |
|
| 1105 |
|
| 1106 |
static void library_path_to_pathbuf(void) |
| 1107 |
{ |
| 1108 |
dynabuf_clear(pathbuf); |
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
if ((PLATFORM_USE_ENV_VAR) && (config.platform_lib_prefix == NULL)) { |
| 1113 |
throw_error("Library path not set. Use --libpath or create an environment variable."); |
| 1114 |
} else { |
| 1115 |
dynabuf_add_string(pathbuf, config.platform_lib_prefix); |
| 1116 |
} |
| 1117 |
} |
| 1118 |
|
| 1119 |
|
| 1120 |
static void default_path_to_pathbuf(void) |
| 1121 |
{ |
| 1122 |
const char *start = input_plat_pathref_filename, |
| 1123 |
*readptr, |
| 1124 |
*found; |
| 1125 |
|
| 1126 |
dynabuf_clear(pathbuf); |
| 1127 |
if (config.dialect >= V0_98__PATHS_AND_SYMBOLCHANGE) { |
| 1128 |
|
| 1129 |
readptr = start; |
| 1130 |
found = NULL; |
| 1131 |
while (*readptr) { |
| 1132 |
if ((*readptr == DIRECTORY_SEPARATOR) |
| 1133 |
|| (*readptr == ALTERNATIVE_DIR_SEP)) { |
| 1134 |
found = readptr; |
| 1135 |
} |
| 1136 |
++readptr; |
| 1137 |
} |
| 1138 |
if (found) { |
| 1139 |
|
| 1140 |
dynabuf_add_bytes(pathbuf, start, found - start + 1); |
| 1141 |
} |
| 1142 |
} else { |
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
} |
| 1147 |
} |
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
int input_read_output_filename(void) |
| 1160 |
{ |
| 1161 |
boolean absolute; |
| 1162 |
|
| 1163 |
SKIPSPACE(); |
| 1164 |
if (GotByte == '<') { |
| 1165 |
|
| 1166 |
throw_error("Writing to library not supported."); |
| 1167 |
return 1; |
| 1168 |
} |
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
if (get_string()) |
| 1175 |
return 1; |
| 1176 |
|
| 1177 |
|
| 1178 |
if (read_filename_shared_end(&absolute)) |
| 1179 |
return 1; |
| 1180 |
|
| 1181 |
if (absolute) { |
| 1182 |
|
| 1183 |
} else { |
| 1184 |
|
| 1185 |
default_path_to_pathbuf(); |
| 1186 |
|
| 1187 |
dynabuf_add_string(pathbuf, GLOBALDYNABUF_CURRENT); |
| 1188 |
|
| 1189 |
dynabuf_append(pathbuf, '\0'); |
| 1190 |
|
| 1191 |
dynabuf_clear(GlobalDynaBuf); |
| 1192 |
dynabuf_add_string(GlobalDynaBuf, pathbuf->buffer); |
| 1193 |
dynabuf_append(GlobalDynaBuf, '\0'); |
| 1194 |
} |
| 1195 |
|
| 1196 |
return 0; |
| 1197 |
} |
| 1198 |
|
| 1199 |
|
| 1200 |
void input_get_location(struct location *target) |
| 1201 |
{ |
| 1202 |
*target = current_input.location; |
| 1203 |
} |
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
void inputchange_new_file(struct inputchange_buf *icb, FILE *fd, const char *eternal_plat_filename) |
| 1210 |
{ |
| 1211 |
report_flush(); |
| 1212 |
|
| 1213 |
|
| 1214 |
icb->input = current_input; |
| 1215 |
icb->gb = GotByte; |
| 1216 |
|
| 1217 |
current_input.location.plat_filename = eternal_plat_filename; |
| 1218 |
current_input.location.line_number = 1; |
| 1219 |
current_input.srctype = INPUTSRC_FILE; |
| 1220 |
current_input.state = INPUTSTATE_SOF; |
| 1221 |
current_input.u.fd = fd; |
| 1222 |
|
| 1223 |
report_inputchange("Source: ", eternal_plat_filename); |
| 1224 |
} |
| 1225 |
|
| 1226 |
void inputchange_new_ram(struct inputchange_buf *icb) |
| 1227 |
{ |
| 1228 |
report_flush(); |
| 1229 |
|
| 1230 |
|
| 1231 |
icb->input = current_input; |
| 1232 |
icb->gb = GotByte; |
| 1233 |
|
| 1234 |
current_input.srctype = INPUTSRC_RAM; |
| 1235 |
current_input.u.ram_ptr = NULL; |
| 1236 |
} |
| 1237 |
|
| 1238 |
|
| 1239 |
void inputchange_set_ram(int line_num, const char *body) |
| 1240 |
{ |
| 1241 |
current_input.location.line_number = line_num; |
| 1242 |
current_input.u.ram_ptr = body; |
| 1243 |
input_set_report_enabled(FALSE); |
| 1244 |
} |
| 1245 |
|
| 1246 |
void inputchange_macro1_params(const struct location *def, const char *params) |
| 1247 |
{ |
| 1248 |
current_input.location = *def; |
| 1249 |
current_input.u.ram_ptr = params; |
| 1250 |
current_input.state = INPUTSTATE_NORMAL; |
| 1251 |
|
| 1252 |
report_inputchange("macro from ", def->plat_filename); |
| 1253 |
} |
| 1254 |
|
| 1255 |
void inputchange_macro2_body(const char *macro_body) |
| 1256 |
{ |
| 1257 |
current_input.u.ram_ptr = macro_body; |
| 1258 |
current_input.state = INPUTSTATE_NORMAL; |
| 1259 |
} |
| 1260 |
|
| 1261 |
void inputchange_back(const struct inputchange_buf *icb) |
| 1262 |
{ |
| 1263 |
report_flush(); |
| 1264 |
|
| 1265 |
|
| 1266 |
current_input = icb->input; |
| 1267 |
GotByte = icb->gb; |
| 1268 |
|
| 1269 |
switch (current_input.srctype) { |
| 1270 |
case INPUTSRC_NONE: |
| 1271 |
report_inputchange("done", ""); |
| 1272 |
break; |
| 1273 |
case INPUTSRC_FILE: |
| 1274 |
report_inputchange("Source: ", current_input.location.plat_filename); |
| 1275 |
break; |
| 1276 |
case INPUTSRC_RAM: |
| 1277 |
report_inputchange("back", ""); |
| 1278 |
break; |
| 1279 |
default: |
| 1280 |
BUG("IllegalInputSrc", current_input.srctype); |
| 1281 |
} |
| 1282 |
} |
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
struct ipi { |
| 1289 |
struct ipi *next, |
| 1290 |
*prev; |
| 1291 |
const char *path; |
| 1292 |
}; |
| 1293 |
|
| 1294 |
|
| 1295 |
static struct ipi ipi_head = {&ipi_head, &ipi_head, NULL}; |
| 1296 |
|
| 1297 |
|
| 1298 |
void includepaths_add(const char *path) |
| 1299 |
{ |
| 1300 |
struct ipi *ipi; |
| 1301 |
|
| 1302 |
ipi = safe_malloc(sizeof(*ipi)); |
| 1303 |
ipi->path = path; |
| 1304 |
ipi->next = &ipi_head; |
| 1305 |
ipi->prev = ipi_head.prev; |
| 1306 |
ipi->next->prev = ipi; |
| 1307 |
ipi->prev->next = ipi; |
| 1308 |
} |
| 1309 |
|
| 1310 |
|
| 1311 |
static FILE *combine_and_open_ro(void) |
| 1312 |
{ |
| 1313 |
FILE *stream; |
| 1314 |
|
| 1315 |
|
| 1316 |
if (pathbuf->size |
| 1317 |
&& (pathbuf->buffer[pathbuf->size - 1] != DIRECTORY_SEPARATOR) |
| 1318 |
&& (pathbuf->buffer[pathbuf->size - 1] != ALTERNATIVE_DIR_SEP)) { |
| 1319 |
dynabuf_append(pathbuf, DIRECTORY_SEPARATOR); |
| 1320 |
} |
| 1321 |
|
| 1322 |
dynabuf_add_string(pathbuf, GLOBALDYNABUF_CURRENT); |
| 1323 |
|
| 1324 |
dynabuf_append(pathbuf, '\0'); |
| 1325 |
|
| 1326 |
stream = fopen(pathbuf->buffer, FILE_READBINARY); |
| 1327 |
if (config.process_verbosity >= 9) |
| 1328 |
printf("Trying \"%s\"...%s\n", pathbuf->buffer, stream ? " OK!" : ""); |
| 1329 |
return stream; |
| 1330 |
} |
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
FILE *includepaths_open_ro(struct filespecflags *flags) |
| 1338 |
{ |
| 1339 |
FILE *stream; |
| 1340 |
struct ipi *ipi; |
| 1341 |
|
| 1342 |
if (flags->uses_lib) { |
| 1343 |
|
| 1344 |
library_path_to_pathbuf(); |
| 1345 |
stream = combine_and_open_ro(); |
| 1346 |
} else { |
| 1347 |
|
| 1348 |
if (flags->absolute) |
| 1349 |
dynabuf_clear(pathbuf); |
| 1350 |
else |
| 1351 |
default_path_to_pathbuf(); |
| 1352 |
stream = combine_and_open_ro(); |
| 1353 |
if (stream == NULL) { |
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
for (ipi = ipi_head.next; ipi != &ipi_head; ipi = ipi->next) { |
| 1359 |
dynabuf_clear(pathbuf); |
| 1360 |
dynabuf_add_string(pathbuf, ipi->path); |
| 1361 |
stream = combine_and_open_ro(); |
| 1362 |
if (stream) { |
| 1363 |
break; |
| 1364 |
} |
| 1365 |
} |
| 1366 |
} |
| 1367 |
} |
| 1368 |
if (stream) { |
| 1369 |
|
| 1370 |
dynabuf_clear(GlobalDynaBuf); |
| 1371 |
dynabuf_add_string(GlobalDynaBuf, pathbuf->buffer); |
| 1372 |
dynabuf_append(GlobalDynaBuf, '\0'); |
| 1373 |
} else { |
| 1374 |
|
| 1375 |
dynabuf_clear(pathbuf); |
| 1376 |
dynabuf_add_string(pathbuf, "Cannot open input file "); |
| 1377 |
dynabuf_append(pathbuf, flags->uses_lib ? '<' : '\"'); |
| 1378 |
dynabuf_add_string(pathbuf, GLOBALDYNABUF_CURRENT); |
| 1379 |
dynabuf_append(pathbuf, flags->uses_lib ? '>' : '\"'); |
| 1380 |
dynabuf_append(pathbuf, '.'); |
| 1381 |
dynabuf_append(pathbuf, '\0'); |
| 1382 |
throw_error(pathbuf->buffer); |
| 1383 |
} |
| 1384 |
|
| 1385 |
return stream; |
| 1386 |
} |