Debian Codegraph @

texk/bibtex-x/bibtex.c

raw
1 /*-
2 ******************************************************************************
3 ******************************************************************************
4 **
5 ** MODULE
6 **
7 ** file: bibtex.c
8 **
9 ** DESCRIPTION
10 **
11 ** A 32-bit implementation of BibTeX v0.99c for MS-DOS, OS/2 2.x,
12 ** Unix and VMS. This C language implementation is based on the
13 ** original WEB source but it has been enhanced to support 8-bit input
14 ** characters and a very large processing capacity.
15 **
16 ** For documentation describing how to use and build this program,
17 ** see the 00README.TXT file that accompanies this distribution.
18 **
19 ** MODULE CONTENTS
20 **
21 ** This module contains the system-dependent C code for BibTeX.
22 ** There should NOT be any such code code in any of the bibtex-#.c
23 ** modules. Functions defined in this module are:
24 **
25 ** a_close
26 ** a_open_in
27 ** a_open_out
28 ** eoln
29 ** main
30 **
31 ** AUTHORS
32 **
33 ** Original WEB translation to C, conversion to "big" (32-bit) capacity,
34 ** addition of run-time selectable capacity and 8-bit support extensions
35 ** by:
36 **
37 ** Niel Kempson
38 ** Snowy Owl Systems Limited, Cheltenham, England
39 ** E-mail: [email protected]
40 **
41 ** 8-bit support extensions also by:
42 **
43 ** Alejandro Aguilar-Sierra
44 ** Centro de Ciencias de la Atm\'osfera,
45 ** Universidad Nacional Aut\'onoma de M\'exico, M\'exico
46 ** E-mail: [email protected]
47 **
48 ** COPYRIGHT
49 **
50 ** This implementation copyright (c) 1991-1995 by Niel Kempson
51 ** and copyright (c) 1995 by Alejandro Aguilar-Sierra.
52 **
53 ** This program is free software; you can redistribute it and/or
54 ** modify it under the terms of the GNU General Public License as
55 ** published by the Free Software Foundation; either version 1, or
56 ** (at your option) any later version.
57 **
58 ** This program is distributed in the hope that it will be useful,
59 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
60 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
61 ** General Public License for more details.
62 **
63 ** You should have received a copy of the GNU General Public License
64 ** along with this program; if not, write to the Free Software
65 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
66 **
67 ** In other words, you are welcome to use, share and improve this
68 ** program. You are forbidden to forbid anyone else to use, share
69 ** and improve what you give them. Help stamp out software-hoarding!
70 **
71 ** ACKNOWLEDGEMENT
72 **
73 ** The original BibTeX was written by Oren Patashnik using Donald
74 ** Knuth's WEB system. This format produces a PASCAL program for
75 ** execution and a TeX documented version of the source code. This
76 ** program started as a (manual) translation of the WEB source into C.
77 **
78 ** CHANGE LOG
79 **
80 ** $Log: bibtex.c,v $
81 ** Revision 3.71 1996/08/18 20:37:06 kempson
82 ** Official release 3.71 (see HISTORY file for details).
83 **
84 ** Revision 3.70 1996/04/08 10:08:40 kempson
85 ** Final documentation & cosmetic changes for official release 3.70.
86 **
87 ** Revision 3.5 1995/09/24 20:44:37 kempson
88 ** Many changes for final beta test version.
89 **
90 ** Revision 3.4 1995/04/09 22:15:42 kempson
91 ** Placed under RCS control
92 **
93 ******************************************************************************
94 ******************************************************************************
95 */
96
97 #define __BIBTEX_C__
98
99 #ifdef HAVE_CONFIG_H
100 #include <config.h>
101 #endif
102
103 #include "sysdep.h"
104 #include "bibtex.h"
105 #include "datatype.h"
106 #include "gblprocs.h"
107 #include "gblvars.h"
108 #include "utils.h"
109 #include "version.h"
110
111
112 /***************************************************************************
113 * WEB section number: 39
114 * ~~~~~~~~~~~~~~~~~~~
115 * Close the text file.
116 ***************************************************************************/
117 void a_close (const AlphaFile_T file_pointer)
118 BEGIN
119
120 close_file (file_pointer);
121
122 END
123 /*^^^^^^^^^^^^^^^^^^^^^^^^^^ END OF SECTION 39 ^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
124
125
126 /***************************************************************************
127 * WEB section number: 38
128 * ~~~~~~~~~~~~~~~~~~~
129 * Open a text (ASCII) file in the current working directory for input.
130 ***************************************************************************/
131 Boolean_T a_open_in (AlphaFile_T *file_pointer,
132 Integer_T search_path)
133 BEGIN
134 *file_pointer = open_ip_file (search_path);
135
136 if (*file_pointer != NULL)
137 return TRUE;
138 else
139 return FALSE;
140 END
141 /*^^^^^^^^^^^^^^^^^^^^^^^^^^ END OF SECTION 39 ^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
142
143
144
145 /***************************************************************************
146 * WEB section number: 38
147 * ~~~~~~~~~~~~~~~~~~~
148 * Open a text (ASCII) file for output in the current directory.
149 ***************************************************************************/
150 Boolean_T a_open_out (AlphaFile_T *file_pointer)
151 BEGIN
152
153 *file_pointer = open_op_file ();
154
155 if (*file_pointer != (AlphaFile_T) NULL)
156 return TRUE;
157 else
158 return FALSE;
159
160 END
161 /*^^^^^^^^^^^^^^^^^^^^^^^^^^ END OF SECTION 39 ^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
162
163
164
165 /***************************************************************************
166 * WEB section number: 47
167 * ~~~~~~~~~~~~~~~~~~~
168 * Standard \PASCAL\ says that a file should have |eoln| immediately
169 * before |eof|, but \BibTeX\ needs only a weaker restriction: If |eof|
170 * occurs in the middle of a line, the system function |eoln| should
171 * return a |true| result (even though |f^| will be undefined).
172 ***************************************************************************/
173 Boolean_T eoln (const AlphaFile_T file_pointer)
174 BEGIN
175 int c;
176
177 if (feof (file_pointer))
178 BEGIN
179 return (TRUE);
180 END
181 c = getc (file_pointer);
182 if (c != EOF)
183 BEGIN
184 (void) ungetc (c, file_pointer);
185 END
186 if (c == '\n' || c == '\r' || c == EOF)
187 BEGIN
188 return (TRUE);
189 END
190 else
191 BEGIN
192 return (FALSE);
193 END
194 END
195 /*^^^^^^^^^^^^^^^^^^^^^^^^^^ END OF SECTION 47 ^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
196
197
198
199
200 /***************************************************************************
201 * WEB section number: 10
202 * ~~~~~~~~~~~~~~~~~~~
203 * This program first reads the .aux file that \LaTeX\ produces,
204 *
205 * 1. determining which .bib file(s) and .bst file to read and
206 * 2. constructing a list of cite keys in order of occurrence.
207 *
208 * The .aux file may have other .aux files nested
209 * within. Second, it reads and executes the .bst file,
210 *
211 * 1. determining how and in which order to process the
212 * database entries in the .bib file(s) corresponding to those cite
213 * keys in the list (or in some cases, to all the entries in the .bib
214 * file(s)),
215 * 2. determining what text to be output for each
216 * entry and determining any additional text to be output, and
217 * 3. outputting this text to the .bbl file.
218 *
219 * In addition, the program sends error messages and other remarks to the
220 * |log_file| and terminal.
221 ***************************************************************************/
222 int main (int argc, char **argv)
223 BEGIN
224 extern Integer8_T history;
225 int exit_status;
226
227 /*-
228 **------------------------------------------------------------------------
229 ** Set |log_file| initially to point to NULL. All writes to the log
230 ** check to see that |log_file| isn't NULL before writing.
231 **-----------------------------------------------------------------
232 */
233 log_file = NULL;
234
235 /*-----------------------------------------------------------------
236 * Set up a global label PROGRAM_EXIT which can be used if an
237 * error requiring an emergency stop is encountered.
238 *-----------------------------------------------------------------*/
239 if (setjmp (Exit_Program_Flag) == 1)
240 BEGIN
241 debug_msg (DBG_MISC, "arrived at setjmp (Exit_Program_Flag)");
242 goto Exit_Program_Label;
243 END
244
245 /*-----------------------------------------------------------------
246 * Set up a global label CLOSE_UP_SHOP which may be used when a
247 * fatal error is detected. Used by BIBTEX_OVERFLOW & CONFUSION.
248 *-----------------------------------------------------------------*/
249 if (setjmp (Close_Up_Shop_Flag) == 1)
250 BEGIN
251 debug_msg (DBG_MISC, "arrived at setjmp (Close_Up_Shop_Flag)");
252 goto Close_Up_Shop_Label;
253 END
254
255 history = SPOTLESS;
256 parse_cmd_line (argc, argv);
257
258 set_array_sizes ();
259 report_search_paths ();
260
261 initialize ();
262
263 if (log_file != NULL) {
264 FPRINTF (log_file, "%s-x%s (%s)\n", BANNER, EXT_VERSION, TL_VERSION);
265 FPRINTF (log_file, "Implementation: %s\n", IMPLEMENTATION);
266 FPRINTF (log_file, "Release version: %s\n", VERSION);
267 #ifdef UTF_8
268 {
269 UVersionInfo icuVersion;
270 char icu_version[U_MAX_VERSION_STRING_LENGTH] = "";
271 UErrorCode err1 = U_ZERO_ERROR;
272 u_getVersion(icuVersion);
273 u_versionToString(icuVersion, icu_version);
274 FPRINTF (log_file, "Compiled with: ICU version %s\n", icu_version);
275
276 if (Flag_location)
277 u_coll = ucol_open(Str_location, &err1);
278 else
279 u_coll = ucol_open(NULL, &err1);
280 if (!U_SUCCESS(err1)) {
281 FPRINTF (log_file, "Error in opening ICU collator.\n");
282 exit(FATAL_EXIT_STATUS);
283 }
284 }
285 #endif
286 FPRINTF (log_file, "\n");
287 }
288
289 #ifdef SUPPORT_8BIT
290 /*
291 ** Read the CS file and setup the 8-bit character set handling.
292 ** Similar initialisation is done in the original initialise(), but
293 ** we do it in a separate function to simplify the code.
294 */
295 c8initialise ();
296 #endif /* SUPPORT_8BIT */
297
298 if (Flag_stats)
299 report_bibtex_capacity ();
300
301
302
303 /***************************************************************************
304 * WEB section number: 110
305 * ~~~~~~~~~~~~~~~~~~~
306 * We keep reading and processing input lines until none left. This is part
307 * of the main program; hence, because of the |aux_done| label, there's no
308 * BEGIN - END pair surrounding the entire module.
309 ***************************************************************************/
310 PRINT ("The top-level auxiliary file: ");
311 print_aux_name ();
312
313 /*-----------------------------------------------------------------
314 * Set up a global label AUX_DONE which can be used if an error is
315 * encountered while the .AUX file is being processed.
316 *-----------------------------------------------------------------*/
317 if (setjmp (Aux_Done_Flag) == 1)
318 BEGIN
319 debug_msg (DBG_MISC, "arrived at setjmp (Aux_Done_Flag)");
320 goto Aux_Done_Label;
321 END
322 LOOP
323 BEGIN
324 INCR (CUR_AUX_LINE);
325 if ( ! input_ln (CUR_AUX_FILE))
326 BEGIN
327 pop_the_aux_stack ();
328 END
329 else
330 BEGIN
331 get_aux_command_and_process ();
332 END
333 END
334
335 Aux_Done_Label:
336
337 #ifdef TRACE
338 if (Flag_trace)
339 TRACE_PR_LN ("Finished reading the auxiliary file(s)");
340 #endif /* TRACE */
341
342 last_check_for_aux_errors ();
343 /*^^^^^^^^^^^^^^^^^^^^^^^^^^ END OF SECTION 110 ^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
344
345 /***************************************************************************
346 * WEB section number: 151
347 * ~~~~~~~~~~~~~~~~~~~
348 * Here's the outer loop for reading the .bst file - it keeps reading and
349 * processing .bst commands until none left. This is part of the main
350 * program; hence, because of the |bst_done| label, there's no conventional
351 * BEGIN - END pair surrounding the entire module.
352 ***************************************************************************/
353 if (bst_str == 0)
354 BEGIN
355 goto No_Bst_File_Label;
356 END
357 bst_line_num = 0;
358 bbl_line_num = 1;
359 buf_ptr2 = last;
360
361 /*-----------------------------------------------------------------
362 * Set up a global label BST_DONE which can be used if an error is
363 * encountered while the .BST file is being processed.
364 *-----------------------------------------------------------------*/
365 if (setjmp (Bst_Done_Flag) == 1)
366 BEGIN
367 debug_msg (DBG_MISC, "arrived at setjmp (Bst_Done_Flag)");
368 goto Bst_Done_Label;
369 END
370 LOOP
371 BEGIN
372 if (!eat_bst_white_space ())
373 BEGIN
374 goto Bst_Done_Label;
375 END
376 get_bst_command_and_process ();
377 END
378 #ifdef UTF_8
379 ucol_close(u_coll);
380 #endif
381 Bst_Done_Label:
382 a_close (bst_file);
383 No_Bst_File_Label:
384 a_close (bbl_file);
385 /*^^^^^^^^^^^^^^^^^^^^^^^^^^ END OF SECTION 151 ^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
386
387 Close_Up_Shop_Label:
388
389 /***************************************************************************
390 * WEB section number: 455
391 * ~~~~~~~~~~~~~~~~~~~
392 * This section does any last-minute printing and ends the program.
393 ***************************************************************************/
394 BEGIN
395 if ((read_performed) && ( ! reading_completed))
396 BEGIN
397 PRINT2 ("Aborted at line %ld of file ", (long) bib_line_num);
398 print_bib_name ();
399 END
400
401 trace_and_stat_printing ();
402
403 /*-
404 ** Report on BibTeX memory usage to log file.
405 */
406 TRACE_PR_NEWLINE;
407 TRACE_PR_LN ("Here's how much of BibTeX's memory you used:");
408 TRACE_PR_LN3 (" Cites: %6ld out of %ld",
409 (long) num_cites, (long) Max_Cites);
410 TRACE_PR_LN3 (" Fields: %6ld out of %ld",
411 (long) field_ptr, (long) Max_Fields);
412 TRACE_PR_LN3 (" Hash table: %6ld out of %ld",
413 (long) hash_used, (long) Hash_Size);
414 TRACE_PR_LN3 (" Strings: %6ld out of %ld",
415 (long) str_ptr, (long) Max_Strings);
416 TRACE_PR_LN3 (" Free string pool: %6ld out of %ld",
417 (long) pool_ptr, (long) Pool_Size);
418 TRACE_PR_LN3 (" Wizard functions: %6ld out of %ld",
419 (long) wiz_def_ptr, (long) Wiz_Fn_Space);
420
421
422 /***************************************************************************
423 * WEB section number: 466
424 * ~~~~~~~~~~~~~~~~~~~
425 * Some implementations may wish to pass the |history| value to the
426 * operating system so that it can be used to govern whether or not other
427 * programs are started. Here we simply report the history to the user.
428 * The appropriate exit status will be passed to the operating system just
429 * before exiting.
430 ***************************************************************************/
431 switch (history)
432 BEGIN
433 case SPOTLESS:
434 DO_NOTHING;
435 break;
436 case WARNING_MESSAGE:
437 if (err_count == 1)
438 BEGIN
439 PRINT_LN ("(There was 1 warning)");
440 END
441 else
442 BEGIN
443 PRINT_LN2 ("(There were %ld warnings)", (long) err_count);
444 END
445 break;
446 case ERROR_MESSAGE:
447 if (err_count == 1)
448 BEGIN
449 PRINT_LN ("(There was 1 error message)");
450 END
451 else
452 BEGIN
453 PRINT_LN2 ("(There were %ld error messages)", (long) err_count);
454 END
455 break;
456 case FATAL_MESSAGE:
457 PRINT_LN ("(That was a fatal error)");
458 break;
459 default:
460 PRINT ("History is bunk");
461 print_confusion ();
462 break;
463 END
464 /*^^^^^^^^^^^^^^^^^^^^^^^^^^ END OF SECTION 466 ^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
465
466 a_close (log_file);
467 END
468 /*^^^^^^^^^^^^^^^^^^^^^^^^^^ END OF SECTION 455 ^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
469
470 Exit_Program_Label:
471
472 switch (history)
473 BEGIN
474 case SPOTLESS:
475 exit_status = NORMAL_EXIT_STATUS;
476 break;
477 case WARNING_MESSAGE:
478 exit_status = WARNING_EXIT_STATUS;
479 break;
480 case ERROR_MESSAGE:
481 exit_status = ERROR_EXIT_STATUS;
482 break;
483 case FATAL_MESSAGE:
484 default:
485 exit_status = FATAL_EXIT_STATUS;
486 break;
487 END
488
489 exit (exit_status);
490 return (exit_status);
491 END
492
493 /*^^^^^^^^^^^^^^^^^^^^^^^^^^ END OF SECTION 10 ^^^^^^^^^^^^^^^^^^^^^^^^^^^*/