| 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 |
#include <bibtool/general.h> |
| 28 |
#include <bibtool/symbols.h> |
| 29 |
#include <bibtool/record.h> |
| 30 |
#include <bibtool/entry.h> |
| 31 |
#include <bibtool/error.h> |
| 32 |
#include <bibtool/pxfile.h> |
| 33 |
#include <bibtool/rsc.h> |
| 34 |
#include <bibtool/stack.h> |
| 35 |
#include <bibtool/sbuffer.h> |
| 36 |
#include <bibtool/macros.h> |
| 37 |
#include <bibtool/print.h> |
| 38 |
#ifdef HAVE_LIBKPATHSEA |
| 39 |
#ifdef __STDC__ |
| 40 |
#define HAVE_PROTOTYPES |
| 41 |
#endif |
| 42 |
#include <kpathsea/tex-file.h> |
| 43 |
#endif |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
#ifdef __STDC__ |
| 50 |
#define _ARG(A) A |
| 51 |
#else |
| 52 |
#define _ARG(A) () |
| 53 |
#endif |
| 54 |
bool read_rsc _ARG((String name)); |
| 55 |
bool see_bib _ARG((String fname)); |
| 56 |
bool seen _ARG((void)); |
| 57 |
int parse_bib _ARG((Record rec)); |
| 58 |
static bool parse_block _ARG((int quotep)); |
| 59 |
static bool parse_equation _ARG((Record rec)); |
| 60 |
static bool parse_key _ARG((int alpha)); |
| 61 |
static bool parse_rhs _ARG((void)); |
| 62 |
static bool parse_string _ARG((int quotep)); |
| 63 |
static bool parse_symbol _ARG((int alpha)); |
| 64 |
static bool parse_value _ARG((void)); |
| 65 |
static bool see_rsc _ARG((String fname)); |
| 66 |
static int fill_line _ARG((void)); |
| 67 |
static int see_bib_msg _ARG((char *s)); |
| 68 |
static int skip _ARG((int inc)); |
| 69 |
static int skip_c _ARG((void)); |
| 70 |
static int skip_nl _ARG((void)); |
| 71 |
static void init___ _ARG((char ***pathp,char **pattern,char **envvp,char *env)); |
| 72 |
static void init_parse _ARG((void)); |
| 73 |
static void parse_number _ARG((void)); |
| 74 |
void init_read _ARG((void)); |
| 75 |
void set_rsc_path _ARG((String val)); |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
static StringBuffer * parse_sb = (StringBuffer*)NULL; |
| 88 |
|
| 89 |
#define FLBLEN 80 |
| 90 |
|
| 91 |
static Symbol filename; |
| 92 |
static FILE *file; |
| 93 |
static String file_line_buffer; |
| 94 |
static String flp; |
| 95 |
static size_t fl_size = 0; |
| 96 |
static int flno = 0; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
#define EmptyC (*flp=='\0') |
| 101 |
#define CurrentC *flp |
| 102 |
#define FutureC *(flp+1) |
| 103 |
#define ClearLine (*flp='\0') |
| 104 |
#define GetC skip(true) |
| 105 |
#define NextC *(flp++) |
| 106 |
#define SkipC ++flp |
| 107 |
#define TestC skip(false) |
| 108 |
#define UnGetC flp-- |
| 109 |
|
| 110 |
#define InitLine *file_line_buffer = '\0'; \ |
| 111 |
flp = file_line_buffer; \ |
| 112 |
flno = 0; |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
static String str_unexpected = (String)"Unexpected character encountered"; |
| 117 |
static String str_stdin = (String)"<stdin>"; |
| 118 |
|
| 119 |
#define Error3(X,Y,Z) error(ERR_ERROR|ERR_POINT|ERR_FILE \ |
| 120 |
| (rsc_parse_exit ? ERR_EXIT : ERR_NONE), \ |
| 121 |
(String)X, (String)Y, (String)Z, \ |
| 122 |
file_line_buffer, flp, flno, filename) |
| 123 |
#define Error(X) error(ERR_ERROR|ERR_POINT|ERR_FILE \ |
| 124 |
| (rsc_parse_exit ? ERR_EXIT : ERR_NONE), \ |
| 125 |
(String)X, s_empty, s_empty, \ |
| 126 |
file_line_buffer, flp, flno, filename) |
| 127 |
#define Warning(X) error(ERR_WARN|ERR_POINT|ERR_FILE,(String)X, \ |
| 128 |
s_empty, s_empty, \ |
| 129 |
file_line_buffer, flp, flno, filename) |
| 130 |
#define UnterminatedError(X,LINE) \ |
| 131 |
error(ERR_ERROR|ERR_FILE \ |
| 132 |
| (rsc_parse_exit ? ERR_EXIT : ERR_NONE), \ |
| 133 |
(String)X, s_empty, s_empty, \ |
| 134 |
NULL, NULL, LINE, filename) |
| 135 |
#define UnexpectedError Error(str_unexpected) |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
static void init___(pathp,pattern,envvp,env) |
| 148 |
char ***pathp; |
| 149 |
char **pattern; |
| 150 |
char **envvp; |
| 151 |
char *env; |
| 152 |
{ register char **cpp, |
| 153 |
*cp; |
| 154 |
|
| 155 |
if (*pathp != (char**)0) |
| 156 |
{ free((char*)*pathp); |
| 157 |
*pathp = (char**)0; |
| 158 |
} |
| 159 |
|
| 160 |
if ((cp = getenv(env)) != NULL) |
| 161 |
{ *envvp = cp; } |
| 162 |
|
| 163 |
if (*envvp) |
| 164 |
{ *pathp = px_s2p(*envvp,*rsc_env_sep); |
| 165 |
if (*pathp == (char**)0) |
| 166 |
{ WARNING2(env,"search path extension failed."); |
| 167 |
} |
| 168 |
DebugPrint2("Path extension ",env); |
| 169 |
} |
| 170 |
|
| 171 |
if (*rsc_dir_file_sep != '/') |
| 172 |
{ for (cpp = pattern; *cpp; ++cpp) |
| 173 |
{ *cpp = new_string(*cpp); |
| 174 |
for (cp = *cpp; *cp; ++cp) |
| 175 |
{ if (*cp == '/') *cp = *rsc_dir_file_sep; |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
#ifndef HAVE_LIBKPATHSEA |
| 182 |
static char **f_path = (char**)0; |
| 183 |
static char *f_pattern[] = |
| 184 |
{ "%s/%s", "%s/%s.bib", NULL }; |
| 185 |
#endif |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
void init_read() |
| 201 |
{ |
| 202 |
#ifndef HAVE_LIBKPATHSEA |
| 203 |
init___(&f_path, |
| 204 |
f_pattern, |
| 205 |
(char**)&rsc_v_bibtex, |
| 206 |
(char*)rsc_e_bibtex ); |
| 207 |
#endif |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
static int see_bib_msg(s) |
| 218 |
register char *s; |
| 219 |
{ |
| 220 |
if (rsc_verbose) VerbosePrint2("Trying ", s); |
| 221 |
return true; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
bool see_bib(fname) |
| 246 |
register String fname; |
| 247 |
{ |
| 248 |
init_parse(); |
| 249 |
InitLine; |
| 250 |
if (fname == NULL) |
| 251 |
{ |
| 252 |
filename = str_stdin; |
| 253 |
file = stdin; |
| 254 |
return true; |
| 255 |
} |
| 256 |
#ifdef HAVE_LIBKPATHSEA |
| 257 |
filename = (String)kpse_find_file((char*)fname, |
| 258 |
kpse_bib_format, |
| 259 |
TRUE); |
| 260 |
if (filename == NULL) return false; |
| 261 |
file = fopen((char*)filename, "r"); |
| 262 |
#else |
| 263 |
file = px_fopen((char*)fname, |
| 264 |
"r", |
| 265 |
f_pattern, |
| 266 |
f_path, |
| 267 |
see_bib_msg); |
| 268 |
filename = (String)px_filename; |
| 269 |
#endif |
| 270 |
return (file != NULL); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
bool seen() |
| 287 |
{ |
| 288 |
if (file == stdin) |
| 289 |
{ file = NULL; |
| 290 |
return true; |
| 291 |
} |
| 292 |
else if (file) |
| 293 |
{ (void)fclose(file); |
| 294 |
file = NULL; |
| 295 |
return true; |
| 296 |
} |
| 297 |
return false; |
| 298 |
} |
| 299 |
|
| 300 |
#define Expect(C,N) if (GetC != C) { UnexpectedError; return(N); } |
| 301 |
#define ExpectSymbol(C,N) if (!parse_symbol(C)) return (N) |
| 302 |
#define ExpectKey(C,N) if (!parse_key(C)) return (N) |
| 303 |
#define ExpectRhs(N) if (!parse_rhs()) return (N) |
| 304 |
#define ExpectEq(R,N) if (!parse_equation(R)) return (N) |
| 305 |
#define ExpectEqMac(R,N) if (!parse_equation(R)) return (N) |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
static void init_parse() |
| 315 |
{ |
| 316 |
if (fl_size != 0) return; |
| 317 |
|
| 318 |
fl_size = FLBLEN; |
| 319 |
if ((file_line_buffer=(String)malloc(fl_size * sizeof(Uchar))) |
| 320 |
== NULL) |
| 321 |
{ OUT_OF_MEMORY("line buffer"); } |
| 322 |
|
| 323 |
if (parse_sb == (StringBuffer*)0 && |
| 324 |
(parse_sb=sbopen()) == (StringBuffer*)0) |
| 325 |
{ OUT_OF_MEMORY("parser"); |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
static int fill_line() |
| 347 |
{ register size_t len; |
| 348 |
|
| 349 |
flp = file_line_buffer; |
| 350 |
++flno; |
| 351 |
|
| 352 |
if (fgets((char*)file_line_buffer, fl_size,file) |
| 353 |
== NULL) |
| 354 |
{ ClearLine; |
| 355 |
DebugPrint1("Reading failed for first line."); |
| 356 |
return 1; |
| 357 |
} |
| 358 |
|
| 359 |
FOREVER |
| 360 |
{ for (len = 0; |
| 361 |
file_line_buffer[len] != '\0'; |
| 362 |
++len) ; |
| 363 |
|
| 364 |
#ifdef DEBUG |
| 365 |
ErrPrintF2("+++ BibTool: line buffer: used %d of %d\n", |
| 366 |
len + 1, |
| 367 |
fl_size); |
| 368 |
#endif |
| 369 |
|
| 370 |
if (file_line_buffer[len-1] == '\n' |
| 371 |
|| len < fl_size - 1) |
| 372 |
{ return 0; } |
| 373 |
|
| 374 |
if ((file_line_buffer = (String) |
| 375 |
realloc((char*)file_line_buffer, |
| 376 |
fl_size+=FLBLEN)) == NULL) |
| 377 |
{ OUT_OF_MEMORY("line buffer"); } |
| 378 |
flp = file_line_buffer; |
| 379 |
|
| 380 |
if (fgets((char*)file_line_buffer + len, |
| 381 |
FLBLEN + 1, |
| 382 |
file) |
| 383 |
== NULL) |
| 384 |
{ return 0; } |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
static int skip(inc) |
| 397 |
register bool inc; |
| 398 |
{ |
| 399 |
FOREVER |
| 400 |
{ if (EmptyC && fill_line()) return EOF; |
| 401 |
else if (is_space(CurrentC)) SkipC; |
| 402 |
else return (inc ? NextC : CurrentC); |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
static int skip_c() |
| 413 |
{ |
| 414 |
FOREVER |
| 415 |
{ if (EmptyC && fill_line()) return EOF; |
| 416 |
else { return NextC; } |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
static int skip_nl() |
| 429 |
{ static bool state = false; |
| 430 |
int c; |
| 431 |
|
| 432 |
if (state) { state = false; return '\n'; } |
| 433 |
|
| 434 |
FOREVER |
| 435 |
{ if (EmptyC && fill_line()) return EOF; |
| 436 |
else if (is_space(CurrentC)) |
| 437 |
{ |
| 438 |
for (c = skip_c(); |
| 439 |
c != EOF && is_space(c) && c != '\n'; |
| 440 |
c = skip_c()) {} |
| 441 |
if (c == EOF) return EOF; |
| 442 |
if (c != '\n') { UnGetC; return ' '; } |
| 443 |
|
| 444 |
for (c = skip_c(); |
| 445 |
c != EOF && is_space(c) && c != '\n'; |
| 446 |
c = skip_c()) {} |
| 447 |
if (c == EOF) return EOF; |
| 448 |
if (c != '\n') { UnGetC; return ' '; } |
| 449 |
|
| 450 |
for (c = skip_c(); |
| 451 |
c != EOF && is_space(c); |
| 452 |
c = skip_c()) {} |
| 453 |
if (c == EOF) return EOF; |
| 454 |
UnGetC; |
| 455 |
state = true; |
| 456 |
return '\n'; |
| 457 |
} |
| 458 |
else { return NextC; } |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
static bool parse_symbol(alpha) |
| 471 |
register int alpha; |
| 472 |
{ register Uchar c; |
| 473 |
register String cp; |
| 474 |
|
| 475 |
c = GetC; |
| 476 |
cp = flp - 1; |
| 477 |
if (alpha && (! is_alpha(c))) |
| 478 |
{ Warning("Symbol does not start with a letter"); |
| 479 |
} |
| 480 |
while (is_allowed(CurrentC)) { SkipC; } |
| 481 |
c = CurrentC; |
| 482 |
CurrentC = '\0'; |
| 483 |
push_string(symbol(lower(cp))); |
| 484 |
CurrentC = c; |
| 485 |
return true; |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
static bool parse_key(alpha) |
| 497 |
register int alpha; |
| 498 |
{ register Uchar c; |
| 499 |
register String cp; |
| 500 |
Symbol name; |
| 501 |
|
| 502 |
c = GetC; |
| 503 |
cp = flp - 1; |
| 504 |
if (alpha && (! is_alpha(c))) |
| 505 |
{ Error("Key does not start with a letter"); |
| 506 |
return false; |
| 507 |
} |
| 508 |
while (is_allowed(CurrentC) || CurrentC == '\'') |
| 509 |
{ SkipC; } |
| 510 |
c = CurrentC; |
| 511 |
CurrentC = '\0'; |
| 512 |
if (rsc_key_case) |
| 513 |
{ Symbol val; |
| 514 |
val = symbol(cp); |
| 515 |
name = symbol(lower(cp)); |
| 516 |
save_key(name, val); |
| 517 |
} |
| 518 |
else |
| 519 |
{ name = symbol(lower(cp)); |
| 520 |
} |
| 521 |
push_string(name); |
| 522 |
CurrentC = c; |
| 523 |
return true; |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
static void parse_number() |
| 535 |
{ register Uchar c; |
| 536 |
register String cp; |
| 537 |
|
| 538 |
cp = flp; |
| 539 |
while (is_digit(CurrentC)) { SkipC; } |
| 540 |
c = CurrentC; |
| 541 |
CurrentC = '\0'; |
| 542 |
push_string(symbol(cp)); |
| 543 |
CurrentC = c; |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
static bool parse_string(quotep) |
| 557 |
int quotep; |
| 558 |
{ int c; |
| 559 |
int left; |
| 560 |
int start_flno = flno; |
| 561 |
|
| 562 |
left = 0; |
| 563 |
if (quotep) (void)sbputchar('"', parse_sb); |
| 564 |
do |
| 565 |
{ switch (c = skip_nl()) |
| 566 |
{ case EOF: |
| 567 |
UnterminatedError("Unterminated double quote", |
| 568 |
start_flno); |
| 569 |
return false; |
| 570 |
case '{': left++; (void)sbputchar((char)c,parse_sb); break; |
| 571 |
case '}': if (left-- < 0) |
| 572 |
{ Warning("Expecting \" here"); } |
| 573 |
(void)sbputchar((char)c,parse_sb); |
| 574 |
break; |
| 575 |
case '\\': (void)sbputchar((char)c,parse_sb); c = NextC; |
| 576 |
(void)sbputchar((char)c,parse_sb); c = ' '; |
| 577 |
break; |
| 578 |
case '"': if (!quotep) break; |
| 579 |
default: (void)sbputchar((char)c,parse_sb); |
| 580 |
} |
| 581 |
} while (c != '"'); |
| 582 |
|
| 583 |
if (left) Warning("Unbalanced parenthesis"); |
| 584 |
return true; |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
static bool parse_block(quotep) |
| 598 |
bool quotep; |
| 599 |
{ int c; |
| 600 |
int left; |
| 601 |
int start_flno = flno; |
| 602 |
|
| 603 |
left = 1; |
| 604 |
if (quotep) (void)sbputchar('{',parse_sb); |
| 605 |
|
| 606 |
FOREVER |
| 607 |
{ switch (c = skip_nl()) |
| 608 |
{ case EOF: |
| 609 |
UnterminatedError("Unterminated open brace", |
| 610 |
start_flno); |
| 611 |
return false; |
| 612 |
case '{': left++; break; |
| 613 |
case '}': |
| 614 |
if (--left < 1) |
| 615 |
{ if (quotep) (void)sbputchar('}', |
| 616 |
parse_sb); |
| 617 |
return true; |
| 618 |
} |
| 619 |
} |
| 620 |
(void)sbputchar(c, parse_sb); |
| 621 |
} |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
static bool parse_rhs() |
| 633 |
{ int start_flno = flno; |
| 634 |
Symbol sym; |
| 635 |
|
| 636 |
sbrewind(parse_sb); |
| 637 |
do |
| 638 |
{ if (sbtell(parse_sb) != 0) |
| 639 |
{ (void)sbputs(" # ", parse_sb); } |
| 640 |
|
| 641 |
switch (GetC) |
| 642 |
{ case EOF: |
| 643 |
UnterminatedError("Unterminated value", |
| 644 |
start_flno); |
| 645 |
return false; |
| 646 |
|
| 647 |
case '"': |
| 648 |
if (!parse_string(true)) return false; |
| 649 |
break; |
| 650 |
|
| 651 |
case '{': |
| 652 |
if (!parse_block(true)) return false; |
| 653 |
break; |
| 654 |
|
| 655 |
case '0': case '1': case '2': case '3': case '4': |
| 656 |
case '5': case '6': case '7': case '8': case '9': |
| 657 |
UnGetC; |
| 658 |
parse_number(); |
| 659 |
sym = pop_string(); |
| 660 |
(void)sbputs((char*)SymbolValue(sym), |
| 661 |
parse_sb); |
| 662 |
break; |
| 663 |
|
| 664 |
default: |
| 665 |
UnGetC; |
| 666 |
ExpectSymbol(true, false); |
| 667 |
{ sym = pop_string(); |
| 668 |
#ifdef OLD |
| 669 |
(void)look_macro(mac,1); |
| 670 |
#endif |
| 671 |
(void)sbputs((char*)SymbolValue(sym), |
| 672 |
parse_sb); |
| 673 |
} |
| 674 |
} |
| 675 |
} while (GetC == '#'); |
| 676 |
|
| 677 |
push_string(symbol((String)sbflush(parse_sb))); |
| 678 |
sbrewind(parse_sb); |
| 679 |
UnGetC; |
| 680 |
return true; |
| 681 |
} |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
static bool parse_equation(rec) |
| 691 |
Record rec; |
| 692 |
{ Symbol s, t; |
| 693 |
|
| 694 |
ExpectSymbol(true, false); |
| 695 |
Expect('=', false); |
| 696 |
ExpectRhs(false); |
| 697 |
|
| 698 |
t = pop_string(); |
| 699 |
s = pop_string(); |
| 700 |
push_to_record(rec, s, t, true); |
| 701 |
return true; |
| 702 |
} |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
int parse_bib(rec) |
| 724 |
Record rec; |
| 725 |
{ register rec_type type; |
| 726 |
register int n, |
| 727 |
c; |
| 728 |
bool again; |
| 729 |
long ignored = 0L; |
| 730 |
String name; |
| 731 |
int line; |
| 732 |
char buffer[32]; |
| 733 |
static StringBuffer * comment_sb = (StringBuffer*)NULL; |
| 734 |
|
| 735 |
if (file == NULL) return BIB_EOF; |
| 736 |
if (comment_sb == (StringBuffer*)NULL) |
| 737 |
{ comment_sb = sbopen(); } |
| 738 |
|
| 739 |
RecordOldKey(rec) = NULL; |
| 740 |
RecordFree(rec) = 0; |
| 741 |
RecordComment(rec) = sym_empty; |
| 742 |
|
| 743 |
do |
| 744 |
{ init_parse(); |
| 745 |
|
| 746 |
while ((c=skip_c()) != '@') |
| 747 |
{ if (c == EOF) |
| 748 |
{ char *s, *t; |
| 749 |
if (ignored == 0) return BIB_EOF; |
| 750 |
RecordType(rec) = BIB_COMMENT; |
| 751 |
|
| 752 |
s = t = sbflush(comment_sb); |
| 753 |
while (*s) s++; |
| 754 |
while (t <= --s && is_space(*s)) *s = '\0'; |
| 755 |
if (*t) |
| 756 |
RecordComment(rec) = symbol((String)t); |
| 757 |
sbrewind(comment_sb); |
| 758 |
return BIB_COMMENT; |
| 759 |
} |
| 760 |
if (!is_space(c)) ++ignored; |
| 761 |
if (ignored > 0 && rsc_pass_comment) |
| 762 |
{ sbputchar(c, comment_sb); } |
| 763 |
} |
| 764 |
|
| 765 |
if (ignored != 0L) |
| 766 |
{ if (rsc_pass_comment) |
| 767 |
{ sbputchar('\n', comment_sb); } |
| 768 |
else |
| 769 |
{ (void)sprintf(buffer,"%ld",ignored); |
| 770 |
error(ERR_WARN|ERR_FILE, (String)buffer, |
| 771 |
(String)" non-space characters ignored.", |
| 772 |
StringNULL,StringNULL,StringNULL, |
| 773 |
flno, filename); |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
RecordLineno(rec) = flno; |
| 778 |
DebugPrint2("Look-up type ", flp); |
| 779 |
|
| 780 |
if ((type=find_entry_type(flp)) == BIB_NOOP) |
| 781 |
{ Error("Unknown entry type"); |
| 782 |
return BIB_NOOP; |
| 783 |
} |
| 784 |
|
| 785 |
flp += symlen(EntryName(type)); |
| 786 |
|
| 787 |
if (type == BIB_COMMENT && rsc_pass_comment) |
| 788 |
{ sbputchar('@', comment_sb); |
| 789 |
sbputs((char*)SymbolValue(EntryName(type)), |
| 790 |
comment_sb); |
| 791 |
} |
| 792 |
} while (type == BIB_COMMENT); |
| 793 |
|
| 794 |
c = GetC; |
| 795 |
if (c != '{' && c != '(') |
| 796 |
{ Error("Expected '{' or '(' missing"); |
| 797 |
return BIB_NOOP; |
| 798 |
} |
| 799 |
line = flno; |
| 800 |
name = filename; |
| 801 |
|
| 802 |
RecordType(rec) = type; |
| 803 |
|
| 804 |
switch (type) |
| 805 |
{ case BIB_COMMENT: |
| 806 |
UnGetC; |
| 807 |
(void)parse_rhs(); |
| 808 |
push_to_record(rec, pop_string(), |
| 809 |
NO_SYMBOL, true); |
| 810 |
return type; |
| 811 |
|
| 812 |
case BIB_PREAMBLE: |
| 813 |
ExpectRhs(BIB_NOOP); |
| 814 |
push_to_record(rec, pop_string(), |
| 815 |
NO_SYMBOL, true); |
| 816 |
break; |
| 817 |
|
| 818 |
case BIB_STRING: |
| 819 |
ExpectEqMac(rec, BIB_NOOP); |
| 820 |
break; |
| 821 |
|
| 822 |
case BIB_ALIAS: |
| 823 |
ExpectEq(rec, BIB_NOOP); |
| 824 |
break; |
| 825 |
|
| 826 |
case BIB_INCLUDE: |
| 827 |
ExpectRhs(BIB_NOOP); |
| 828 |
push_to_record(rec, pop_string(), |
| 829 |
NO_SYMBOL, true); |
| 830 |
break; |
| 831 |
|
| 832 |
case BIB_MODIFY: |
| 833 |
default: |
| 834 |
if (TestC == ',') |
| 835 |
{ Warning("Missing reference key"); |
| 836 |
push_to_record(rec, sym_empty, |
| 837 |
NO_SYMBOL, true); |
| 838 |
(void)GetC; |
| 839 |
} |
| 840 |
else |
| 841 |
{ ExpectKey(false, BIB_NOOP); |
| 842 |
Expect(',', BIB_NOOP); |
| 843 |
push_to_record(rec, pop_string(), |
| 844 |
NO_SYMBOL, true); |
| 845 |
} |
| 846 |
|
| 847 |
do |
| 848 |
{ ExpectEq(rec, BIB_NOOP); |
| 849 |
for (n = 0; GetC == ','; n++) |
| 850 |
{ if (n == 1) |
| 851 |
{ Warning("Multiple ',' ignored."); } |
| 852 |
} |
| 853 |
UnGetC; |
| 854 |
switch (TestC) |
| 855 |
{ case EOF: |
| 856 |
case '}': |
| 857 |
case ')': again = false; break; |
| 858 |
default: again = true; |
| 859 |
if (n == 0) |
| 860 |
{ Warning("Missing ',' assumed."); } |
| 861 |
} |
| 862 |
} while (again); |
| 863 |
} |
| 864 |
|
| 865 |
switch (GetC) |
| 866 |
{ case '}': |
| 867 |
if(c != '{') |
| 868 |
{ Warning("Parenthesis '(' closed by '}'"); } |
| 869 |
break; |
| 870 |
case ')': |
| 871 |
if(c != '(') |
| 872 |
{ Warning("Parenthesis '{' closed by ')'"); } |
| 873 |
break; |
| 874 |
case EOF: |
| 875 |
{ String s; |
| 876 |
if (c == '{') { s = (String)"'{'"; } |
| 877 |
else { s = (String)"'('"; } |
| 878 |
error(ERR_ERROR|ERR_FILE, s, |
| 879 |
(String)" not closed at end of file.", |
| 880 |
StringNULL,StringNULL,StringNULL, |
| 881 |
line,name); |
| 882 |
} |
| 883 |
break; |
| 884 |
default: |
| 885 |
{ String s; |
| 886 |
if (c == '{') { s = (String)"'{'"; } |
| 887 |
else { s = (String)"'('"; } |
| 888 |
error(ERR_ERROR|ERR_FILE, |
| 889 |
s, |
| 890 |
(String)" not properly terminated.", |
| 891 |
StringNULL, StringNULL, StringNULL, |
| 892 |
line,name); |
| 893 |
} |
| 894 |
return BIB_NOOP; |
| 895 |
} |
| 896 |
|
| 897 |
{ String s, t; |
| 898 |
s = t = (String)sbflush(comment_sb); |
| 899 |
while (*s) s++; |
| 900 |
while (t <= --s && is_space(*s)) *s = '\0'; |
| 901 |
if (*t) RecordComment(rec) = symbol(t); |
| 902 |
} |
| 903 |
sbrewind(comment_sb); |
| 904 |
return type; |
| 905 |
} |
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
static char **r_path = (char**)0; |
| 913 |
static char *r_pattern[] = |
| 914 |
{ "%s/%s", "%s/%s.rsc", NULL }; |
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
void set_rsc_path(val) |
| 925 |
String val; |
| 926 |
{ |
| 927 |
rsc_v_rsc = val; |
| 928 |
init___(&r_path, |
| 929 |
r_pattern, |
| 930 |
(char**)&rsc_v_rsc, |
| 931 |
(char*)rsc_e_rsc); |
| 932 |
} |
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
static bool see_rsc(fname) |
| 944 |
String fname; |
| 945 |
{ |
| 946 |
if (fname == StringNULL) return false; |
| 947 |
|
| 948 |
init_parse(); |
| 949 |
InitLine; |
| 950 |
file = px_fopen((char*)fname, |
| 951 |
"r", |
| 952 |
r_pattern, |
| 953 |
r_path, |
| 954 |
see_bib_msg); |
| 955 |
filename = (String)px_filename; |
| 956 |
return (file != NULL); |
| 957 |
} |
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
static bool parse_value() |
| 968 |
{ int start_flno = flno; |
| 969 |
|
| 970 |
sbrewind(parse_sb); |
| 971 |
switch (GetC) |
| 972 |
{ case EOF: |
| 973 |
UnterminatedError("Unterminated value", |
| 974 |
start_flno); |
| 975 |
return false; |
| 976 |
|
| 977 |
case '"': |
| 978 |
if (!parse_string(false)) return false; |
| 979 |
push_string(symbol((String)sbflush(parse_sb))); |
| 980 |
sbrewind(parse_sb); |
| 981 |
break; |
| 982 |
|
| 983 |
case '0': case '1': case '2': case '3': case '4': |
| 984 |
case '5': case '6': case '7': case '8': case '9': |
| 985 |
UnGetC; parse_number(); |
| 986 |
break; |
| 987 |
|
| 988 |
case '{': |
| 989 |
if (!parse_block(false)) return false; |
| 990 |
push_string(symbol((String)sbflush(parse_sb))); |
| 991 |
sbrewind(parse_sb); |
| 992 |
break; |
| 993 |
|
| 994 |
default: |
| 995 |
UnGetC; ExpectSymbol(true, false); |
| 996 |
} |
| 997 |
|
| 998 |
return true; |
| 999 |
} |
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
bool read_rsc(name) |
| 1019 |
String name; |
| 1020 |
{ int c; |
| 1021 |
Symbol token; |
| 1022 |
String s_filename; |
| 1023 |
FILE *s_file; |
| 1024 |
String s_file_line_buffer; |
| 1025 |
size_t s_fl_size; |
| 1026 |
int s_flno; |
| 1027 |
String s_flp; |
| 1028 |
bool ret; |
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
s_filename = filename; |
| 1033 |
s_file = file; |
| 1034 |
s_file_line_buffer = file_line_buffer; |
| 1035 |
s_fl_size = fl_size; |
| 1036 |
s_flno = flno; |
| 1037 |
s_flp = flp; |
| 1038 |
|
| 1039 |
fl_size = 0; |
| 1040 |
|
| 1041 |
init_parse(); |
| 1042 |
|
| 1043 |
if (see_rsc(name)) |
| 1044 |
{ |
| 1045 |
while ((c=TestC) != EOF) |
| 1046 |
{ switch (c) |
| 1047 |
{ case '#': case '%': case ';': |
| 1048 |
ClearLine; break; |
| 1049 |
default: |
| 1050 |
if (!parse_symbol(c)) |
| 1051 |
{ (void)seen(); |
| 1052 |
return true; |
| 1053 |
} |
| 1054 |
token = pop_string(); |
| 1055 |
if (TestC == '=') (void)GetC; |
| 1056 |
if (!parse_value()) |
| 1057 |
{ (void)seen(); |
| 1058 |
return true; |
| 1059 |
} |
| 1060 |
(void)set_rsc(token, pop_string()); |
| 1061 |
} |
| 1062 |
} |
| 1063 |
(void)seen(); |
| 1064 |
ret = true; |
| 1065 |
} |
| 1066 |
else |
| 1067 |
{ ret = false; |
| 1068 |
} |
| 1069 |
if (fl_size > 0) |
| 1070 |
{ free((char*)file_line_buffer); |
| 1071 |
} |
| 1072 |
filename = s_filename; |
| 1073 |
file = s_file; |
| 1074 |
file_line_buffer = s_file_line_buffer; |
| 1075 |
fl_size = s_fl_size; |
| 1076 |
flno = s_flno; |
| 1077 |
flp = s_flp; |
| 1078 |
|
| 1079 |
return ret; |
| 1080 |
} |