Debian Codegraph @

parse.c

raw symbols SCIP
1 /*** parse.c ******************************************************************
2 **
3 ** This file is part of BibTool.
4 ** It is distributed under the GNU General Public License.
5 ** See the file COPYING for details.
6 **
7 ** (c) 1996-2020 Gerd Neugebauer
8 **
9 ** Net: [email protected]
10 **
11 ** This program is free software; you can redistribute it and/or modify
12 ** it under the terms of the GNU General Public License as published by
13 ** the Free Software Foundation; either version 2, or (at your option)
14 ** any later version.
15 **
16 ** This program is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ** GNU General Public License for more details.
20 **
21 ** You should have received a copy of the GNU General Public License
22 ** along with this program; if not, write to the Free Software
23 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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 /* Internal Programs */
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)); /* parse.c */
55 bool see_bib _ARG((String fname)); /* parse.c */
56 bool seen _ARG((void)); /* parse.c */
57 int parse_bib _ARG((Record rec)); /* parse.c */
58 static bool parse_block _ARG((int quotep)); /* parse.c */
59 static bool parse_equation _ARG((Record rec)); /* parse.c */
60 static bool parse_key _ARG((int alpha)); /* parse.c */
61 static bool parse_rhs _ARG((void)); /* parse.c */
62 static bool parse_string _ARG((int quotep)); /* parse.c */
63 static bool parse_symbol _ARG((int alpha)); /* parse.c */
64 static bool parse_value _ARG((void)); /* parse.c */
65 static bool see_rsc _ARG((String fname)); /* parse.c */
66 static int fill_line _ARG((void)); /* parse.c */
67 static int see_bib_msg _ARG((char *s)); /* parse.c */
68 static int skip _ARG((int inc)); /* parse.c */
69 static int skip_c _ARG((void)); /* parse.c */
70 static int skip_nl _ARG((void)); /* parse.c */
71 static void init___ _ARG((char ***pathp,char **pattern,char **envvp,char *env));/* parse.c*/
72 static void init_parse _ARG((void)); /* parse.c */
73 static void parse_number _ARG((void)); /* parse.c */
74 void init_read _ARG((void)); /* parse.c */
75 void set_rsc_path _ARG((String val)); /* parse.c */
76
77 /*****************************************************************************/
78 /* External Programs */
79 /*===========================================================================*/
80
81 /*---------------------------------------------------------------------------*/
82
83 /*-----------------------------------------------------------------------------
84 ** Variable*: parse_sb
85 ** Purpose: This string buffer is used temporaliry during parsing.
86 **___________________________________________________ */
87 static StringBuffer * parse_sb = (StringBuffer*)NULL;
88
89 #define FLBLEN 80 /* initial size and increment of line buffer */
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 ** Function*: init___()
139 ** Purpose: Initialize the reading apparatus.
140 ** Arguments:
141 ** pathp the path array
142 ** pattern the pattern array
143 ** envvp the environment array
144 ** env the environment string
145 ** Returns: nothing
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 ** Function: init_read()
189 ** Purpose: Initialize the reading apparatus.
190 ** Primarily try to figure out the file search path.
191 **
192 ** Note that this function is for internal purposes
193 ** mainly. The normal user should call |init_bibtool()|
194 ** instead. Just in case the search paths are changed
195 ** afterwards this function has to be called again to
196 ** propagate the information.
197 ** Arguments: none
198 ** Returns: nothing
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 ** Function*: see_bib_msg()
212 ** Purpose: Message function for use with |px_fopen()|.
213 ** Arguments:
214 ** s String to print
215 ** Returns: |true| to indicate that continuation is desired.
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 ** Function: see_bib()
226 ** Purpose: Open a \BibTeX{} file to read from.
227 ** If the argument is |NULL| then |stdin| is used as
228 ** input stream.
229 **
230 ** This function has to be called before |parse()| can be
231 ** called. It initializes the parser routine and takes
232 ** care that the next reading is done from the given
233 ** file.
234 **
235 ** The file opened with this function has to be closed
236 ** with |seen()|.
237 **
238 ** This function is for internal purposes mainly. See
239 ** |read_db()| for a higher level function to read a
240 ** database.
241 ** Arguments:
242 ** fname Name of the file or |NULL|.
243 ** Returns: |true| iff the file could be opened for reading.
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 ** Function: seen()
275 ** Purpose: Close input file for the \BibTeX{} reading apparatus.
276 ** After this function has been called |parse()| might
277 ** not return sensible results.
278 **
279 ** This function is for internal purposes mainly. See
280 ** |read_db()| for a higher level function to read a
281 ** database.
282 ** Arguments: none
283 ** Returns: |false| if an attempt was made to close an already
284 ** closed file.
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 ** Function*: init_parse()
309 ** Purpose: Initialize the parser.
310 ** This function has to be invoked before the parser can be used.
311 ** Arguments: none
312 ** Returns: nothing
313 **___________________________________________________ */
314 static void init_parse() /* */
315 { /* */
316 if (fl_size != 0) return; /* Already initialzed? */
317 /* */
318 fl_size = FLBLEN; /* */
319 if ((file_line_buffer=(String)malloc(fl_size * sizeof(Uchar)))/* */
320 == NULL) /* */
321 { OUT_OF_MEMORY("line buffer"); } /* Allocate line buffer */
322 /* or message and exit. */
323 if (parse_sb == (StringBuffer*)0 && /* Try to initialize the */
324 (parse_sb=sbopen()) == (StringBuffer*)0) /* string buffer for the */
325 { OUT_OF_MEMORY("parser"); /* parser or exit */
326 } /* with error message. */
327 } /*------------------------*/
328
329 /*-----------------------------------------------------------------------------
330 ** Function*: fill_line()
331 ** Purpose: Filling the line buffer until end-of-line or end-of-file
332 ** encountered.
333 **
334 ** Since I don't want to use a fixed line length the algorithm
335 ** is a little bit more complicated.
336 ** First I try to read into the file_line_buffer as it is.
337 ** If this succeeds I check to see what caused |fgets| to
338 ** terminate. Either a end-of-line is in the buffer or
339 ** the buffer is not filled, i.e. end-of-file has been
340 ** encountered, then |0| is returned.
341 ** Otherwise the buffer has been filled and I try to enlarge the
342 ** buffer and read another chunk of bytes.
343 ** Arguments: none
344 ** Returns: Returns 0 iff a character has been read.
345 **___________________________________________________' */
346 static int fill_line() /* */
347 { register size_t len; /* */
348 /* */
349 flp = file_line_buffer; /* Reset line pointer */
350 ++flno; /* Increase line number */
351 /* */
352 if (fgets((char*)file_line_buffer, fl_size,file) /* */
353 == NULL) /*Get first chunk */
354 { ClearLine; /* or report EOF */
355 DebugPrint1("Reading failed for first line."); /* */
356 return 1; /* */
357 } /* */
358 /* */
359 FOREVER /* */
360 { for (len = 0; /* Find the end */
361 file_line_buffer[len] != '\0'; /* of the buffer and */
362 ++len) ; /* count the length. */
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) /* Try to enlarge */
375 realloc((char*)file_line_buffer, /* the line buffer */
376 fl_size+=FLBLEN)) == NULL) /* */
377 { OUT_OF_MEMORY("line buffer"); } /* */
378 flp = file_line_buffer; /* Reset line pointer */
379 /* */
380 if (fgets((char*)file_line_buffer + len, /* */
381 FLBLEN + 1, /* */
382 file) /* */
383 == NULL) /* */
384 { return 0; } /* */
385 } /* */
386 } /*------------------------*/
387
388 /*-----------------------------------------------------------------------------
389 ** Function*: skip()
390 ** Purpose: Skip over spaces. Return the next non-space character or |EOF|.
391 ** Arguments:
392 ** inc If inc is |true| point to the first character after the one
393 ** returned.
394 ** Returns: The next character
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 ** Function*: skip_c()
408 ** Purpose: Return the next character or EOF.
409 ** Arguments: none
410 ** Returns: the next character or |EOF|
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 ** Function*: skip_nl()
422 ** Purpose: Return the next character or EOF.
423 ** Any number of spaces is returned as a single space.
424 ** Doubled newlines are preserved.
425 ** Arguments: none
426 ** Returns: the next character or EOF
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 ** Function*: parse_symbol()
464 ** Purpose: Parse a symbol and push it to the stack.
465 ** Upon failure issue an appropriate message.
466 ** Arguments:
467 ** alpha indicator that the symbol has to start with an alpha character
468 ** Returns: Success status
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 ** Function*: parse_key()
490 ** Purpose: Parse a symbol and push it to the stack.
491 ** Upon failure issue an appropriate message.
492 ** Arguments:
493 ** alpha indicator that the symbol has to start with an alpha character
494 ** Returns: Success status
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 ** Function*: parse_number()
528 ** Purpose: Parse a number and push it to the stack.
529 ** This function is called when at least one digit has been seen.
530 ** Thus no error can occur in this function.
531 ** Arguments: none
532 ** Returns: nothing
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 ** Function*: parse_string()
548 ** Purpose: Parse a string and push it to the stack.
549 ** A string is something enclosed in ""
550 ** Consider the brace level to determine the end of the string.
551 ** Arguments:
552 ** quotep Boolean. |true| iff the leading " has already been stripped
553 ** off the input stream.
554 ** Returns: Success status
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 ** Function*: parse_block()
589 ** Purpose: Parse a block and push it to the stack.
590 ** A block is something enclosed in {}
591 ** Consider the brace level to determine the end of the string.
592 ** Arguments:
593 ** quotep Boolean. |true| iff the leading { has already been stripped off
594 ** the input stream.
595 ** Returns: Success status
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 ** Function*: parse_rhs()
626 ** Purpose: Parse the right hand side of an item.
627 ** This can be composed of strings, blocks, numbers, and symbols
628 ** separated by #
629 ** Arguments: none
630 ** Returns: Success status
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 ** Function*: parse_equation()
685 ** Purpose: Parse a pair separated by an equals sign.
686 ** Arguments:
687 ** rec The record to store the result in
688 ** Returns: Success status
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 ** Function: parse_bib()
706 ** Purpose: Read one entry and fill the internal record structure.
707 ** Return the type of the entry read.
708 **
709 ** |BIB_EOF| is returned if nothing could be read and
710 ** the end of the file has been encountered.
711 **
712 ** |BIB_NOOP| is returned when an error has occurred. This is
713 ** an indicator that no record has been read but the
714 ** error recovery is ready to try it again.
715 **
716 ** This function is for internal purposes mainly. See
717 ** |read_db()| for a higher level function to read a
718 ** database.
719 ** Arguments:
720 ** rec Record to store the result in.
721 ** Returns: The type of the entry read, |BIB_EOF|, or |BIB_NOOP|.
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()) != '@') /* Skip to next @ */
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: /* This code is not used */
806 UnGetC; /* any more. */
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 ** Function: set_rsc_path()
918 ** Purpose: Initialize the resource file reading apparatus.
919 ** Primarily try to figure out the file search path.
920 ** Arguments:
921 ** val The string representation of the file search path.
922 ** Returns: nothing
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 ** Function*: see_rsc()
936 ** Purpose: Open a rsc file to read from. The resource file path
937 ** and the optional extensions are used to construct the
938 ** full file name.
939 ** Arguments:
940 ** fname The file name to take into account.
941 ** Returns: |true| iff the operation succeeds.
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 ** Function*: parse_value()
961 ** Purpose:
962 **
963 **
964 ** Arguments: none
965 ** Returns:
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 ** Function: read_rsc()
1003 ** Purpose: Read a resource file and evaluate all instructions
1004 ** contained.
1005 **
1006 ** The characters |#|, |%|, and |;| start an endline
1007 ** comment but only between resource instructions. They
1008 ** are not recognized between a resource instruction
1009 ** and its value or inside the value braces.
1010 **
1011 ** This function is contained in this module because it
1012 ** shares several functions with the \BibTeX{} parsing
1013 ** routines.
1014 ** Arguments:
1015 ** name Name of the file to read from.
1016 ** Returns: |true| if an error has occured
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 /* Save the old state in */
1030 /* local variables to */
1031 /* restore it later. */
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; /* = is optional */
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 } /*------------------------*/