Debian Codegraph @

texk/web2c/bibtex.web

raw
1 % This program is copyright (C) 1985, 1988, 2010, 2025 by Oren Patashnik;
2 % all rights are reserved.
3 %
4 % This program, BibTeX, is available under the same terms as
5 % Donald Knuth's TeX program.
6 %
7 % (Request to implementors: The WEB system provides for alterations via
8 % an auxiliary file; the master file should stay intact.)
9 %
10 % See Appendix H of the WEB manual for hints on how to install this program.
11
12 % Version 0.98f was released in March 1985.
13 % Version 0.98g was released in April; it removed some system dependencies
14 % (introducing term_in and term_out in place of just tty, and removing
15 % some nonlocal goto's) and it gave context for certain parsing errors.
16 % Version 0.98h was released in April; it patched a bug in the output
17 % line-breaking routine that can arise with some nonstandard style files.
18 % Version 0.98i was released in May; its main change split up the main program
19 % and some procedures to help certain compilers cope with size
20 % limitations, among other things changing error and warning macros so
21 % they'd produce (much) less inline code; it also redefined the class of
22 % legal style-file identifiers---although this affects only the bizarre
23 % ones, it makes BibTeX's error messages more coherent; and it had many
24 % minor changes, including about a 15% speed-up on TOPS-20.
25 % Version 0.99a was released in January 1988. Its main changes: allowed the
26 % inclusion of entire .bib files (rather than just those entries
27 % \cited or \nocited); made the sorting algorithm stable; eliminated
28 % any case conversion for file names; allowed concatenation in database
29 % fields and string definitions; handled hyphenated names properly;
30 % handled accented characters properly; implemented new empty$,
31 % preamble$, text.length$, text.prefix$, and warning$ built-in functions;
32 % allowed a new cross-referencing feature; and made many minor fixes,
33 % including about a 40% speed-up on TOPS-20.
34 % Version 0.99b was released in February 1988. It changed text.length$ and
35 % text.prefix$ to not count braces as text characters, and it changed
36 % text.prefix$ to add any necessary matching right braces.
37 % Version 0.99c was released in February 1988. It removed two begin-end pairs
38 % that, for convention only, surrounded entire modules, but that elicited
39 % label-related complaints from some compilers.
40 % Version 0.99d was released in March 2010. It made output lines breakable
41 % only at white_space (so that, for example, URLs would not be broken).
42 % Other known bugs (all minor) will be fixed in a subsequent release.
43 % Updated bibtex.web was released on 8 December 2010. Still version
44 % 0.99d; this release clarified the license.
45 % Version 0.99e was released September 2025. Its only changes affected the
46 % typesetting of the program itself; there were no functional changes.
47
48 % Please report any bugs to biblio@@tug.org
49
50 % Although considerable effort has been expended to make the BibTeX program
51 % correct and reliable, no warranty is implied; the author disclaims any
52 % obligation or liability for damages, including but not limited to
53 % special, indirect, or consequential damages arising out of or in
54 % connection with the use or performance of this software.
55
56 % This program was written by Oren Patashnik, in consultation with Leslie
57 % Lamport, to be used with Lamport's LaTeX document preparation system.
58 % Some modules were taken from Knuth's TeX and TeXware with his permission.
59
60 % Here is TeX material that gets inserted after \input webmac
61 \def\hang{\hangindent 3em\indent\ignorespaces}
62 \font\ninerm=cmr9
63 \let\mc=\ninerm % medium caps for names like SAIL
64 \def\PASCAL{Pascal}
65 \def\ph{\hbox{Pascal-H}}
66 \def\<#1>{$\langle#1\rangle$}
67 \def\section{\mathhexbox278}
68
69 \def\(#1){} % this is used to make section names sort themselves better
70 \def\9#1{} % this is used for sort keys in the index via @@:sort key}{entry@@>
71
72 \font\sc=cmcsc10
73
74 % Use \TeX in defining \BibTeX and \LaTeX
75 \def\BibTeX{{\rm B\kern-.05em{\sc i\kern-.025em b}\kern-.08em \TeX}}
76
77 \def\LaTeX{{\rm L\kern-.36em\raise.3ex\hbox{\sc a}\kern-.15em \TeX}}
78
79 \def\title{\BibTeX\ }
80 \def\today{\ifcase\month\or
81 January\or February\or March\or April\or May\or June\or
82 July\or August\or September\or October\or November\or December\fi
83 \space\number\day, \number\year}
84 \def\topofcontents{\null\vfill
85 \centerline{\:\titlefont The {\:\ttitlefont \BibTeX} preprocessor}
86 \vskip 15pt \centerline{(Version 0.99e---\today)} \vfill}
87 \pageno=\contentspagenumber \advance\pageno by 1
88
89
90
91 @* Introduction.
92 @^documentation@>
93 @^space savings@>
94 @^system dependencies@>
95 @^wizard@>
96 @!@:BibTeX}{\BibTeX@>
97 @!@:BibTeX documentation}{\BibTeX\ documentation@>
98 @:LaTeX}{\LaTeX@>
99 \BibTeX\ is a preprocessor (with elements of postprocessing as
100 explained below) for the \LaTeX\ document-preparation system. It
101 handles most of the formatting decisions required to produce a
102 reference list, outputting a \.{.bbl} file that a user can edit to add
103 any finishing touches \BibTeX\ isn't designed to handle (in practice,
104 such editing almost never is needed); with this file \LaTeX\ actually
105 produces the reference list.
106
107 Here's how \BibTeX\ works. It takes as input (a)~an \.{.aux} file
108 produced by \LaTeX\ on an earlier run; (b)~a \.{.bst} file (the style
109 file), which specifies the general reference-list style and specifies
110 how to format individual entries, and which is written by a style
111 designer (called a wizard throughout this program) in a
112 special-purpose language described in the \BibTeX\ documentation---see
113 the file {\.{btxdoc.tex}}; and (c)~\.{.bib} file(s) constituting a
114 database of all reference-list entries the user might ever hope to
115 use. \BibTeX\ chooses from the \.{.bib} file(s) only those entries
116 specified by the \.{.aux} file (that is, those given by \LaTeX's
117 \.{\\cite} or \.{\\nocite} commands), and creates as output a \.{.bbl}
118 file containing these entries together with the formatting commands
119 specified by the \.{.bst} file (\BibTeX\ also creates a \.{.blg} log
120 file, which includes any error or warning messages, but this file
121 isn't used by any program). \LaTeX\ will use the \.{.bbl} file,
122 perhaps edited by the user, to produce the reference list.
123
124 Many modules of \BibTeX\ were taken from Knuth's \TeX\ and \TeX ware,
125 with his permission. All known system-dependent modules are marked in
126 the index entry ``system dependencies''; Dave Fuchs helped exorcise
127 unwanted ones. In addition, a few modules that can be changed to make
128 \BibTeX\ smaller are marked in the index entry ``space savings''.
129
130 Megathanks to Howard Trickey, for whose suggestions future users and
131 style writers would be eternally grateful, if only they knew.
132
133 The |banner| string defined here should be changed whenever \BibTeX\
134 gets modified.
135
136 @d banner=='This is BibTeX, Version 0.99e' {printed when the program starts}
137
138
139 @
140 @^system dependencies@>
141 Terminal output goes to the file |term_out|, while terminal input
142 comes from |term_in|. On our system, these (system-dependent) files
143 are already opened at the beginning of the program, and have the same
144 real name.
145
146 @d term_out == tty
147 @d term_in == tty
148
149
150 @
151 @^system dependencies@>
152 This program uses the term |print| instead of |write| when writing on
153 both the |log_file| and (system-dependent) |term_out| file, and it
154 uses |trace_pr| when in |trace| mode, for which it writes on just the
155 |log_file|. If you want to change where either set of macros writes
156 to, you should also change the other macros in this program for that
157 set; each such macro begins with |print_| or |trace_pr_|.
158
159 @d print(#) == begin write(log_file,#); write(term_out,#); end
160 @d print_ln(#) == begin write_ln(log_file,#); write_ln(term_out,#); end
161 @d print_newline == print_a_newline
162 {making this a procedure saves a little space}
163 @#
164 @d trace_pr(#) == begin write(log_file,#); end
165 @d trace_pr_ln(#) == begin write_ln(log_file,#); end
166 @d trace_pr_newline == begin write_ln(log_file); end
167
168 @<Procedures and functions for all file I/O, error messages, and such@>=
169 procedure print_a_newline;
170 begin
171 write_ln(log_file);
172 write_ln(term_out);
173 end;
174
175
176 @
177 @^debugging@>
178 @^statistics@>
179 Some of the code below is intended to be used only when diagnosing the
180 strange behavior that sometimes occurs when \BibTeX\ is being
181 installed or when system wizards are fooling around with \BibTeX\
182 without quite knowing what they are doing. Such code will not normally
183 be compiled; it is delimited by the codewords
184 `$|debug|\ldots|gubed|$', with apologies to people who wish to
185 preserve the purity of English. Similarly, there is some conditional
186 code delimited by `$|stat|\ldots|tats|$' that is intended only for use
187 when statistics are to be kept about \BibTeX's memory/cpu usage,
188 and there is conditional code delimited by `$|trace|\ldots|ecart|$'
189 that is intended to be a trace facility for use mainly when debugging
190 \.{.bst} files.
191
192 @d debug == @{ { remove the `|@{|' when debugging }
193 @d gubed == @t@>@} { remove the `|@}|' when debugging }
194 @f debug == begin
195 @f gubed == end
196 @#
197 @d stat == @{ { remove the `|@{|' when keeping statistics }
198 @d tats == @t@>@} { remove the `|@}|' when keeping statistics }
199 @f stat == begin
200 @f tats == end
201 @#
202 @d trace == @{ { remove the `|@{|' when in |trace| mode }
203 @d ecart == @t@>@} { remove the `|@}|' when in |trace| mode }
204 @f trace == begin
205 @f ecart == end
206
207
208 @
209 @^system dependencies@>
210 We assume that |case| statements may include a
211 default case that applies if no matching label is found,
212 since most \PASCAL\ compilers have plugged this hole in the language by
213 incorporating some sort of default mechanism. For example, the \ph\
214 compiler allows `|others|:' as a default label, and other \PASCAL s allow
215 syntaxes like `\ignorespaces|else|\unskip' or `\\{otherwise}' or
216 `\\{otherwise}:', etc. The definitions of |othercases| and |endcases|
217 should be changed to agree with local conventions. Note that no semicolon
218 appears before |endcases| in this program, so the definition of |endcases|
219 should include a semicolon if the compiler wants one. (Of course, if no
220 default mechanism is available, the |case| statements of \BibTeX\ will have
221 to be laboriously extended by listing all remaining cases. People who are
222 stuck with such \PASCAL s have in fact done this, successfully but not
223 happily!)
224
225 @d othercases == others: {default for cases not listed explicitly}
226 @d endcases == @+end {follows the default case in an extended |case| statement}
227 @f othercases == else
228 @f endcases == end
229
230
231 @
232 Labels are given symbolic names by the following definitions, so that
233 occasional |goto| statements will be meaningful. We insert the label
234 `|exit|:' just before the `\ignorespaces|end|\unskip' of a procedure
235 in which we have used the `|return|' statement defined below (and this
236 is the only place `|exit|:' appears). This label is sometimes used
237 for exiting loops that are set up with the |loop| construction defined
238 below. Another generic label is `|loop_exit|:'; it appears
239 immediately after a loop.
240
241 Incidentally, this program never declares a label that isn't actually used,
242 because some fussy \PASCAL\ compilers will complain about redundant labels.
243
244 @d exit=10 {go here to leave a procedure}
245 @d loop_exit=15 {go here to leave a loop within a procedure}
246 @d loop1_exit=16 {the first generic label for a procedure with two}
247 @d loop2_exit=17 {the second}
248
249
250 @
251 @^for loops@>
252 And |while| we're discussing loops: This program makes into |while|
253 loops many that would otherwise be |for| loops because of Standard
254 \PASCAL\ limitations (it's a bit complicated---standard \PASCAL\
255 doesn't allow a global variable as the index of a |for| loop inside a
256 procedure; furthermore, many compilers have fairly severe limitations
257 on the size of a block, including the main block of the program; so
258 most of the code in this program occurs inside procedures, and since
259 for other reasons this program must use primarily global variables, it
260 doesn't use many |for| loops).
261
262
263 @
264 @^program conventions@>
265 This program uses this convention: If there are several quantities in
266 a boolean expression, they are ordered by expected frequency (except
267 perhaps when an error message results) so that execution will be
268 fastest; this is more an attempt to understand the program than to
269 make it faster.
270
271
272 @
273 Here are some macros for common programming idioms.
274
275 @d incr(#) == #:=#+1 {increase a variable by unity}
276 @d decr(#) == #:=#-1 {decrease a variable by unity}
277 @d loop == @+ while true do@+ {repeat over and over until a |goto| happens}
278 @f loop == xclause
279 {\.{WEB}'s |xclause| acts like `\ignorespaces|while true do|\unskip'}
280 @d do_nothing == {empty statement}
281 @d return == goto exit {terminate a procedure call}
282 @f return == nil
283 @d empty=0 {symbolic name for a null constant}
284 @d any_value=0 {this appeases \PASCAL's boolean-evaluation scheme}
285
286
287
288 @* The main program.
289 @^system dependencies@>
290 @:LaTeX}{\LaTeX@>
291 This program first reads the \.{.aux} file that \LaTeX\ produces,
292 (\romannumeral1) determining which \.{.bib} file(s) and \.{.bst} file
293 to read and (\romannumeral2) constructing a list of cite keys in order
294 of occurrence. The \.{.aux} file may have other \.{.aux} files nested
295 within. Second, it reads and executes the \.{.bst} file,
296 (\romannumeral1) determining how and in which order to process the
297 database entries in the \.{.bib} file(s) corresponding to those cite
298 keys in the list (or in some cases, to all the entries in the \.{.bib}
299 file(s)), (\romannumeral2) determining what text to be output for each
300 entry and determining any additional text to be output, and
301 (\romannumeral3) actually outputting this text to the \.{.bbl} file.
302 In addition, the program sends error messages and other remarks to the
303 |log_file| and terminal.
304
305 @d close_up_shop=9998 {jump here after fatal errors}
306 @d exit_program=9999 {jump here if we couldn't even get started}
307
308 @p
309 @t\4@>@<Compiler directives@>@/
310 program BibTEX; {all files are opened dynamically}
311 label close_up_shop,@!exit_program @<Labels in the outer block@>;
312 const @<Constants in the outer block@>
313 type @<Types in the outer block@>
314 var @<Globals in the outer block@>@;
315 @<Procedures and functions for about everything@>@;
316 @<The procedure |initialize|@>
317 @#
318 begin
319 initialize;
320 print_ln(banner);@/
321 @<Read the \.{.aux} file@>;
322 @<Read and execute the \.{.bst} file@>;
323 close_up_shop:
324 @<Clean up and leave@>;
325 exit_program:
326 end.
327
328
329 @
330 @^overflow in arithmetic@>
331 @^system dependencies@>
332 If the first character of a \PASCAL\ comment is a dollar sign,
333 \ph\ treats the comment as a list of ``compiler directives'' that will
334 affect the translation of this program into machine language. The
335 directives shown below specify full checking and inclusion of the \PASCAL\
336 debugger when \BibTeX\ is being debugged,
337 but they cause range checking and other
338 redundant code to be eliminated when the production system is being generated.
339 Arithmetic overflow will be detected in all cases.
340
341 @<Compiler directives@>=
342 @{@&$C-,A+,D-@} {no range check, catch arithmetic overflow, no debug overhead}
343 @!debug @{@&$C+,D+@}@+ gubed {but turn everything on when debugging}
344
345
346 @
347 @^bottom up@>
348 @^gymnastics@>
349 @^mooning@>
350 All procedures in this program (except for |initialize|) are grouped
351 into one of the seven classes below, and these classes are dispersed
352 throughout the program. However: Much of this program is written top
353 down, yet \PASCAL\ wants its procedures bottom up. Since mooning is
354 neither a technically nor a socially acceptable solution to the
355 bottom-up problem, this section instead performs the topological
356 gymnastics that \.{WEB} allows, ordering these classes to satisfy
357 \PASCAL\ compilers. There are a few procedures still out of place
358 after this ordering, though, and the other modules that complete the
359 task have ``gymnastics'' as an index entry.
360
361 @<Procedures and functions for about everything@>=
362 @<Procedures and functions for all file I/O, error messages, and such@>@;
363 @<Procedures and functions for file-system interacting@>@;
364 @<Procedures and functions for handling numbers, characters, and strings@>@;
365 @<Procedures and functions for input scanning@>@;
366 @<Procedures and functions for name-string processing@>@;
367 @<Procedures and functions for style-file function execution@>@;
368 @<Procedures and functions for the reading and processing of input files@>
369
370
371 @
372 This procedure gets things started properly.
373
374 @<The procedure |initialize|@>=
375 procedure initialize;
376 var @<Local variables for initialization@>
377 begin
378 @<Check the \(co)``constant'' values for consistency@>;
379 if (bad > 0) then
380 begin
381 write_ln (term_out,bad:0,' is a bad bad');
382 goto exit_program;
383 end;
384 @<Set initial values of key variables@>;
385 pre_def_certain_strings;@/
386 get_the_top_level_aux_file_name;
387 end;
388
389
390 @
391 @^space savings@>
392 @^system dependencies@>
393 These parameters can be changed at compile time to extend or reduce
394 \BibTeX's capacity. They are set to accommodate about 750 cites when
395 used with the standard styles, although |pool_size| is usually the
396 first limitation to be a problem, often when there are 500 cites.
397
398 @<Constants in the outer block@>=
399 @!buf_size=1000; {maximum number of characters in an input line (or string)}
400 @!min_print_line=3; {minimum \.{.bbl} line length: must be |>=3|}
401 @!max_print_line=79; {the maximum: must be |>min_print_line| and |<buf_size|}
402 @!aux_stack_size=20; {maximum number of simultaneous open \.{.aux} files}
403 @!max_bib_files=20; {maximum number of \.{.bib} files allowed}
404 @!pool_size=65000; {maximum number of characters in strings}
405 @!max_strings=4000; {maximum number of strings, including pre-defined;
406 must be |<=hash_size|}
407 @!max_cites=750; {maximum number of distinct cite keys; must be
408 |<=max_strings|}
409 @!min_crossrefs=2; {minimum number of cross-refs required for automatic
410 |cite_list| inclusion}
411 @!wiz_fn_space=3000; {maximum amount of |wiz_defined|-function space}
412 @!single_fn_space=100; {maximum amount for a single |wiz_defined|-function}
413 @!max_ent_ints=3000; {maximum number of |int_entry_var|s
414 (entries $\times$ |int_entry_var|s)}
415 @!max_ent_strs=3000; {maximum number of |str_entry_var|s
416 (entries $\times$ |str_entry_var|s)}
417 @!ent_str_size=100; {maximum size of a |str_entry_var|; must be |<=buf_size|}
418 @!glob_str_size=1000; {maximum size of a |str_global_var|;
419 must be |<=buf_size|}
420 @!max_fields=17250; {maximum number of fields (entries $\times$ fields,
421 about |23*max_cites| for consistency)}
422 @!lit_stk_size=100; {maximum number of literal functions on the stack}
423
424
425 @
426 @^space savings@>
427 @^system dependencies@>
428 These parameters can also be changed at compile time, but they're
429 needed to define some \.{WEB} numeric macros so they must be so
430 defined themselves.
431
432 @d hash_size=5000 {must be |>= max_strings| and |>= hash_prime|}
433 @d hash_prime=4253 {a prime number about 85\% of |hash_size| and |>= 128|
434 and |< @t$2^{14}-2^6$@>|}
435 @d file_name_size=40 {file names shouldn't be longer than this}
436 @d max_glob_strs=10 {maximum number of |str_global_var| names}
437 @d max_glb_str_minus_1 = max_glob_strs-1 {to avoid wasting a |str_global_var|}
438
439
440 @
441 In case somebody has inadvertently made bad settings of the ``constants,''
442 \BibTeX\ checks them using a global variable called |bad|.
443
444 This is the first of many sections of \BibTeX\ where global variables are
445 defined.
446
447 @<Globals in the outer block@>=
448 @!bad:integer; {is some ``constant'' wrong?}
449
450
451 @
452 Each digit-value of |bad| has a specific meaning.
453
454 @<Check the \(co)``constant'' values for consistency@>=
455 bad := 0;
456 if (min_print_line < 3) then bad:=1;
457 if (max_print_line <= min_print_line) then bad:=10*bad+2;
458 if (max_print_line >= buf_size) then bad:=10*bad+3;
459 if (hash_prime < 128) then bad:=10*bad+4;
460 if (hash_prime > hash_size) then bad:=10*bad+5;
461 if (hash_prime >= (16384-64)) then bad:=10*bad+6;
462 if (max_strings > hash_size) then bad:=10*bad+7;
463 if (max_cites > max_strings) then bad:=10*bad+8;
464 if (ent_str_size > buf_size) then bad:=10*bad+9;
465 if (glob_str_size > buf_size) then bad:=100*bad+11;
466 {well, almost each}
467
468
469 @
470 A global variable called |history| will contain one of four values at
471 the end of every run: |spotless| means that no unusual messages were
472 printed; |warning_message| means that a message of possible interest
473 was printed but no serious errors were detected; |error_message| means
474 that at least one error was found; |fatal_message| means that the
475 program terminated abnormally. The value of |history| does not
476 influence the behavior of the program; it is simply computed for the
477 convenience of systems that might want to use such information.
478
479 @d spotless=0 {|history| value for normal jobs}
480 @d warning_message=1 {|history| value when non-serious info was printed}
481 @d error_message=2 {|history| value when an error was noted}
482 @d fatal_message=3 {|history| value when we had to stop prematurely}
483
484 @<Procedures and functions for all file I/O, error messages, and such@>=
485 procedure mark_warning;
486 begin
487 if (history = warning_message) then
488 incr(err_count)
489 else if (history = spotless) then
490 begin
491 history := warning_message;
492 err_count := 1;
493 end;
494 end;
495 @#
496 procedure mark_error;
497 begin
498 if (history < error_message) then
499 begin
500 history := error_message;
501 err_count := 1;
502 end
503 else {|history = error_message|}
504 incr(err_count);
505 end;
506 @#
507 procedure mark_fatal;
508 begin
509 history := fatal_message;
510 end;
511
512
513 @
514 For the two states |warning_message| and |error_message| we keep track
515 of the number of messages given; but since |warning_message|s aren't
516 so serious, we ignore them once we've seen an |error_message|. Hence
517 we need just the single variable |err_count| to keep track.
518
519
520 @<Globals in the outer block@>=
521 @!history:spotless..fatal_message; {how bad was this run?}
522 @!err_count:integer;
523
524
525 @
526 The |err_count| gets set or reset when |history| first changes to
527 |warning_message| or |error_message|, so we don't need to initialize
528 it.
529
530 @<Set initial values of key variables@>=
531 history := spotless;
532
533
534
535 @* The character set.
536 @^ASCII code@>
537 (The following material is copied (almost) verbatim from \TeX.
538 Thus, the same system-dependent changes should be made to both programs.)
539
540 In order to make \TeX\ readily portable between a wide variety of
541 computers, all of its input text is converted to an internal seven-bit
542 code that is essentially standard ASCII, the ``American Standard Code for
543 Information Interchange.'' This conversion is done immediately when each
544 character is read in. Conversely, characters are converted from ASCII to
545 the user's external representation just before they are output to a
546 text file.
547
548 Such an internal code is relevant to users of \TeX\ primarily because it
549 governs the positions of characters in the fonts. For example, the
550 character `\.A' has ASCII code $65=@'101$, and when \TeX\ typesets
551 this letter it specifies character number 65 in the current font.
552 If that font actually has `\.A' in a different position, \TeX\ doesn't
553 know what the real position is; the program that does the actual printing from
554 \TeX's device-independent files is responsible for converting from ASCII to
555 a particular font encoding.
556
557 \TeX's internal code is relevant also with respect to constants
558 that begin with a reverse apostrophe.
559
560
561 @
562 Characters of text that have been converted to \TeX's internal form
563 are said to be of type |ASCII_code|, which is a subrange of the integers.
564
565 @<Types in the outer block@>=
566 @!ASCII_code=0..127; {seven-bit numbers}
567
568
569 @
570 @^character set dependencies@>
571 @^system dependencies@>
572 The original \PASCAL\ compiler was designed in the late 60s, when six-bit
573 character sets were common, so it did not make provision for lower-case
574 letters. Nowadays, of course, we need to deal with both capital and small
575 letters in a convenient way, especially in a program for typesetting;
576 so the present specification of \TeX\ has been written under the assumption
577 that the \PASCAL\ compiler and run-time system permit the use of text files
578 with more than 64 distinguishable characters. More precisely, we assume that
579 the character set contains at least the letters and symbols associated
580 with ASCII codes @'40 through @'176; all of these characters are now
581 available on most computer terminals.
582
583 Since we are dealing with more characters than were present in the first
584 \PASCAL\ compilers, we have to decide what to call the associated data
585 type. Some \PASCAL s use the original name |char| for the
586 characters in text files, even though there now are more than 64 such
587 characters, while other \PASCAL s consider |char| to be a 64-element
588 subrange of a larger data type that has some other name.
589
590 In order to accommodate this difference, we shall use the name |text_char|
591 to stand for the data type of the characters that are converted to and
592 from |ASCII_code| when they are input and output. We shall also assume
593 that |text_char| consists of the elements |chr(first_text_char)| through
594 |chr(last_text_char)|, inclusive. The following definitions should be
595 adjusted if necessary.
596
597 @d text_char == char {the data type of characters in text files}
598 @d first_text_char=0 {ordinal number of the smallest element of |text_char|}
599 @d last_text_char=127 {ordinal number of the largest element of |text_char|}
600
601 @<Local variables for initialization@>=
602 i:0..last_text_char; {this is the first one declared}
603
604
605 @
606 The \TeX\ processor converts between ASCII code and
607 the user's external character set by means of arrays |xord| and |xchr|
608 that are analogous to \PASCAL's |ord| and |chr| functions.
609
610 @<Globals in the outer block@>=
611 @!xord: array [text_char] of ASCII_code;
612 {specifies conversion of input characters}
613 @!xchr: array [ASCII_code] of text_char;
614 {specifies conversion of output characters}
615
616
617 @
618 @^character set dependencies@>
619 @^system dependencies@>
620 Since we are assuming that our \PASCAL\ system is able to read and write the
621 visible characters of standard ASCII (although not necessarily using the
622 ASCII codes to represent them), the following assignment statements initialize
623 most of the |xchr| array properly, without needing any system-dependent
624 changes. On the other hand, it is possible to implement \TeX\ with
625 less complete character sets, and in such cases it will be necessary to
626 change something here.
627
628 @<Set initial values of key variables@>=
629 xchr[@'40]:=' ';
630 xchr[@'41]:='!';
631 xchr[@'42]:='"';
632 xchr[@'43]:='#';
633 xchr[@'44]:='$';
634 xchr[@'45]:='%';
635 xchr[@'46]:='&';
636 xchr[@'47]:='''';@/
637 xchr[@'50]:='(';
638 xchr[@'51]:=')';
639 xchr[@'52]:='*';
640 xchr[@'53]:='+';
641 xchr[@'54]:=',';
642 xchr[@'55]:='-';
643 xchr[@'56]:='.';
644 xchr[@'57]:='/';@/
645 xchr[@'60]:='0';
646 xchr[@'61]:='1';
647 xchr[@'62]:='2';
648 xchr[@'63]:='3';
649 xchr[@'64]:='4';
650 xchr[@'65]:='5';
651 xchr[@'66]:='6';
652 xchr[@'67]:='7';@/
653 xchr[@'70]:='8';
654 xchr[@'71]:='9';
655 xchr[@'72]:=':';
656 xchr[@'73]:=';';
657 xchr[@'74]:='<';
658 xchr[@'75]:='=';
659 xchr[@'76]:='>';
660 xchr[@'77]:='?';@/
661 xchr[@'100]:='@@';
662 xchr[@'101]:='A';
663 xchr[@'102]:='B';
664 xchr[@'103]:='C';
665 xchr[@'104]:='D';
666 xchr[@'105]:='E';
667 xchr[@'106]:='F';
668 xchr[@'107]:='G';@/
669 xchr[@'110]:='H';
670 xchr[@'111]:='I';
671 xchr[@'112]:='J';
672 xchr[@'113]:='K';
673 xchr[@'114]:='L';
674 xchr[@'115]:='M';
675 xchr[@'116]:='N';
676 xchr[@'117]:='O';@/
677 xchr[@'120]:='P';
678 xchr[@'121]:='Q';
679 xchr[@'122]:='R';
680 xchr[@'123]:='S';
681 xchr[@'124]:='T';
682 xchr[@'125]:='U';
683 xchr[@'126]:='V';
684 xchr[@'127]:='W';@/
685 xchr[@'130]:='X';
686 xchr[@'131]:='Y';
687 xchr[@'132]:='Z';
688 xchr[@'133]:='[';
689 xchr[@'134]:='\';
690 xchr[@'135]:=']';
691 xchr[@'136]:='^';
692 xchr[@'137]:='_';@/
693 xchr[@'140]:='`';
694 xchr[@'141]:='a';
695 xchr[@'142]:='b';
696 xchr[@'143]:='c';
697 xchr[@'144]:='d';
698 xchr[@'145]:='e';
699 xchr[@'146]:='f';
700 xchr[@'147]:='g';@/
701 xchr[@'150]:='h';
702 xchr[@'151]:='i';
703 xchr[@'152]:='j';
704 xchr[@'153]:='k';
705 xchr[@'154]:='l';
706 xchr[@'155]:='m';
707 xchr[@'156]:='n';
708 xchr[@'157]:='o';@/
709 xchr[@'160]:='p';
710 xchr[@'161]:='q';
711 xchr[@'162]:='r';
712 xchr[@'163]:='s';
713 xchr[@'164]:='t';
714 xchr[@'165]:='u';
715 xchr[@'166]:='v';
716 xchr[@'167]:='w';@/
717 xchr[@'170]:='x';
718 xchr[@'171]:='y';
719 xchr[@'172]:='z';
720 xchr[@'173]:='{';
721 xchr[@'174]:='|';
722 xchr[@'175]:='}';
723 xchr[@'176]:='~';@/
724 xchr[0]:=' '; xchr[@'177]:=' ';
725 {ASCII codes 0 and |@'177| do not appear in text}
726
727
728 @
729 @^character set dependencies@>
730 @^system dependencies@>
731 Some of the ASCII codes without visible characters have been given symbolic
732 names in this program because they are used with a special meaning. The
733 |tab| character may be system dependent.
734
735 @d null_code=@'0 {ASCII code that might disappear}
736 @d tab=@'11 {ASCII code treated as |white_space|}
737 @d space=@'40 {ASCII code treated as |white_space|}
738 @d invalid_code=@'177 {ASCII code that should not appear}
739
740
741 @
742 @^character set dependencies@>
743 @^system dependencies@>
744 @:TeXbook}{\sl The \TeX book@>
745 The ASCII code is ``standard'' only to a certain extent, since many
746 computer installations have found it advantageous to have ready access
747 to more than 94 printing characters. Appendix~C of {\sl The \TeX book\/}
748 gives a complete specification of the intended correspondence between
749 characters and \TeX's internal representation.
750
751 If \TeX\ is being used
752 on a garden-variety \PASCAL\ for which only standard ASCII
753 codes will appear in the input and output files, it doesn't really matter
754 what codes are specified in |xchr[1..@'37]|, but the safest policy is to
755 blank everything out by using the code shown below.
756
757 However, other settings of |xchr| will make \TeX\ more friendly on
758 computers that have an extended character set, so that users can type things
759 like `\.^^Z' instead of `\.{\\ne}'. At MIT, for example, it would be more
760 appropriate to substitute the code
761 $$\hbox{|for i:=1 to @'37 do xchr[i]:=chr(i);|}$$
762 \TeX's character set is essentially the same as MIT's, even with respect to
763 characters less than~@'40. People with extended character sets can
764 assign codes arbitrarily, giving an |xchr| equivalent to whatever
765 characters the users of \TeX\ are allowed to have in their input files.
766 It is best to make the codes correspond to the intended interpretations as
767 shown in Appendix~C whenever possible; but this is not necessary. For
768 example, in countries with an alphabet of more than 26 letters, it is
769 usually best to map the additional letters into codes less than~@'40.
770
771 @<Set initial values of key variables@>=
772 for i:=1 to @'37 do xchr[i]:=' ';
773 xchr[tab]:=chr(tab);
774
775
776 @
777 This system-independent code makes the |xord| array contain a suitable
778 inverse to the information in |xchr|. Note that if |xchr[i]=xchr[j]|
779 where |i<j<@'177|, the value of |xord[xchr[i]]| will turn out to be
780 |j| or more; hence, standard ASCII code numbers will be used instead
781 of codes below @'40 in case there is a coincidence.
782
783 @<Set initial values of key variables@>=
784 for i:=first_text_char to last_text_char do xord[chr(i)]:=invalid_code;
785 for i:=1 to @'176 do xord[xchr[i]]:=i;
786
787
788 @
789 Also, various characters are given symbolic names; all the ones this
790 program uses are collected here. We use the sharp sign as the
791 |concat_char|, rather than something more natural (like an ampersand),
792 for uniformity of database syntax (ampersand is a valid character in
793 identifiers).
794
795 @d double_quote = """" {delimits strings}
796 @d number_sign = "#" {marks an |int_literal|}
797 @d comment = "%" {ignore the rest of a \.{.bst} or \TeX\ line}
798 @d single_quote = "'" {marks a quoted function}
799 @d left_paren = "(" {optional database entry left delimiter}
800 @d right_paren = ")" {corresponding right delimiter}
801 @d comma = "," {separates various things}
802 @d minus_sign = "-" {for a negative number}
803 @d equals_sign = "=" {separates a field name from a field value}
804 @d at_sign = "@@" {the beginning of a database entry}
805 @d left_brace = "{" {left delimiter of many things}
806 @d right_brace = "}" {corresponding right delimiter}
807 @d period = "." {these are three}
808 @d question_mark = "?" {string-ending characters}
809 @d exclamation_mark = "!" {of interest in \.{add.period\$}}
810 @d tie = "~" {the default space char, in \.{format.name\$}}
811 @d hyphen = "-" {like |white_space|, in \.{format.name\$}}
812 @d star = "*" {for including entire database}
813 @d concat_char = "#" {for concatenating field tokens}
814 @d colon = ":" {for lower-casing (usually title) strings}
815 @d backslash = "\" {used to recognize accented characters}
816
817
818 @
819 These arrays give a lexical classification for the |ASCII_code|s;
820 |lex_class| is used for general scanning and |id_class| is used for
821 scanning identifiers.
822
823 @<Globals in the outer block@>=
824 @!lex_class: array [ASCII_code] of lex_type;
825 @!id_class: array [ASCII_code] of id_type;
826
827
828 @
829 Every character has two types of the lexical classifications. The
830 first type is general, and the second type tells whether the character
831 is legal in identifiers.
832
833 @d illegal = 0 {the unrecognized |ASCII_code|s}
834 @d white_space = 1 {things like |space|s that you can't see}
835 @d alpha = 2 {the upper- and lower-case letters}
836 @d numeric = 3 {the ten digits}
837 @d sep_char = 4 {things sometimes treated like |white_space|}
838 @d other_lex = 5 {when none of the above applies}
839 @d last_lex = 5 {the same number as on the line above}
840 @#
841 @d illegal_id_char = 0 {a few forbidden ones}
842 @d legal_id_char = 1 {most printing characters}
843
844 @<Types in the outer block@>=
845 @!lex_type = 0..last_lex;@/
846 @!id_type = 0..1;
847
848
849 @
850 @^character set dependencies@>
851 @^system dependencies@>
852 Now we initialize the system-dependent |lex_class| array. The |tab|
853 character may be system dependent. Note that the order of these
854 assignments is important here.
855
856 @<Set initial values of key variables@>=
857 for i:=0 to @'177 do lex_class[i] := other_lex;
858 for i:=0 to @'37 do lex_class[i] := illegal;
859 lex_class[invalid_code] := illegal;
860 lex_class[tab] := white_space;
861 lex_class[space] := white_space;
862 lex_class[tie] := sep_char;
863 lex_class[hyphen] := sep_char;
864 for i:=@'60 to @'71 do lex_class[i] := numeric;
865 for i:=@'101 to @'132 do lex_class[i] := alpha;
866 for i:=@'141 to @'172 do lex_class[i] := alpha;
867
868
869 @
870 @^character set dependencies@>
871 @^system dependencies@>
872 And now the |id_class| array.
873
874 @<Set initial values of key variables@>=
875 for i:=0 to @'177 do id_class[i] := legal_id_char;
876 for i:=0 to @'37 do id_class[i] := illegal_id_char;
877 id_class[space] := illegal_id_char;
878 id_class[tab] := illegal_id_char;
879 id_class[double_quote] := illegal_id_char;
880 id_class[number_sign] := illegal_id_char;
881 id_class[comment] := illegal_id_char;
882 id_class[single_quote] := illegal_id_char;
883 id_class[left_paren] := illegal_id_char;
884 id_class[right_paren] := illegal_id_char;
885 id_class[comma] := illegal_id_char;
886 id_class[equals_sign] := illegal_id_char;
887 id_class[left_brace] := illegal_id_char;
888 id_class[right_brace] := illegal_id_char;
889
890
891 @
892 The array |char_width| gives relative printing widths of each
893 |ASCII_code|, and |string_width| will be used later to sum up
894 |char_width|s in a string.
895
896 @<Globals in the outer block@>=
897 @!char_width : array [ASCII_code] of integer;
898 @!string_width : integer;
899
900
901 @
902 @^character set dependencies@>
903 @^system dependencies@>
904 Now we initialize the system-dependent |char_width| array, for which
905 |space| is the only |white_space| character given a nonzero printing
906 width. The widths here are taken from Stanford's June~'87
907 $cmr10$~font and represent hundredths of a point (rounded), but since
908 they're used only for relative comparisons, the units have no meaning.
909
910 @d ss_width = 500 {character |@'31|'s width in the $cmr10$ font}
911 @d ae_width = 722 {character |@'32|'s width in the $cmr10$ font}
912 @d oe_width = 778 {character |@'33|'s width in the $cmr10$ font}
913 @d upper_ae_width = 903 {character |@'35|'s width in the $cmr10$ font}
914 @d upper_oe_width = 1014 {character |@'36|'s width in the $cmr10$ font}
915
916 @<Set initial values of key variables@>=
917 for i:=0 to @'177 do char_width[i] := 0;
918 @#
919 char_width[@'40] := 278;
920 char_width[@'41] := 278;
921 char_width[@'42] := 500;
922 char_width[@'43] := 833;
923 char_width[@'44] := 500;
924 char_width[@'45] := 833;
925 char_width[@'46] := 778;
926 char_width[@'47] := 278;
927 char_width[@'50] := 389;
928 char_width[@'51] := 389;
929 char_width[@'52] := 500;
930 char_width[@'53] := 778;
931 char_width[@'54] := 278;
932 char_width[@'55] := 333;
933 char_width[@'56] := 278;
934 char_width[@'57] := 500;
935 char_width[@'60] := 500;
936 char_width[@'61] := 500;
937 char_width[@'62] := 500;
938 char_width[@'63] := 500;
939 char_width[@'64] := 500;
940 char_width[@'65] := 500;
941 char_width[@'66] := 500;
942 char_width[@'67] := 500;
943 char_width[@'70] := 500;
944 char_width[@'71] := 500;
945 char_width[@'72] := 278;
946 char_width[@'73] := 278;
947 char_width[@'74] := 278;
948 char_width[@'75] := 778;
949 char_width[@'76] := 472;
950 char_width[@'77] := 472;
951 char_width[@'100] := 778;
952 char_width[@'101] := 750;
953 char_width[@'102] := 708;
954 char_width[@'103] := 722;
955 char_width[@'104] := 764;
956 char_width[@'105] := 681;
957 char_width[@'106] := 653;
958 char_width[@'107] := 785;
959 char_width[@'110] := 750;
960 char_width[@'111] := 361;
961 char_width[@'112] := 514;
962 char_width[@'113] := 778;
963 char_width[@'114] := 625;
964 char_width[@'115] := 917;
965 char_width[@'116] := 750;
966 char_width[@'117] := 778;
967 char_width[@'120] := 681;
968 char_width[@'121] := 778;
969 char_width[@'122] := 736;
970 char_width[@'123] := 556;
971 char_width[@'124] := 722;
972 char_width[@'125] := 750;
973 char_width[@'126] := 750;
974 char_width[@'127] :=1028;
975 char_width[@'130] := 750;
976 char_width[@'131] := 750;
977 char_width[@'132] := 611;
978 char_width[@'133] := 278;
979 char_width[@'134] := 500;
980 char_width[@'135] := 278;
981 char_width[@'136] := 500;
982 char_width[@'137] := 278;
983 char_width[@'140] := 278;
984 char_width[@'141] := 500;
985 char_width[@'142] := 556;
986 char_width[@'143] := 444;
987 char_width[@'144] := 556;
988 char_width[@'145] := 444;
989 char_width[@'146] := 306;
990 char_width[@'147] := 500;
991 char_width[@'150] := 556;
992 char_width[@'151] := 278;
993 char_width[@'152] := 306;
994 char_width[@'153] := 528;
995 char_width[@'154] := 278;
996 char_width[@'155] := 833;
997 char_width[@'156] := 556;
998 char_width[@'157] := 500;
999 char_width[@'160] := 556;
1000 char_width[@'161] := 528;
1001 char_width[@'162] := 392;
1002 char_width[@'163] := 394;
1003 char_width[@'164] := 389;
1004 char_width[@'165] := 556;
1005 char_width[@'166] := 528;
1006 char_width[@'167] := 722;
1007 char_width[@'170] := 528;
1008 char_width[@'171] := 528;
1009 char_width[@'172] := 444;
1010 char_width[@'173] := 500;
1011 char_width[@'174] :=1000;
1012 char_width[@'175] := 500;
1013 char_width[@'176] := 500;
1014
1015
1016
1017 @* Input and output.
1018 The basic operations we need to do are
1019 (1)~inputting and outputting of text characters to or from a file;
1020 (2)~instructing the operating system to initiate (``open'')
1021 or to terminate (``close'') input or output to or from a specified file; and
1022 (3)~testing whether the end of an input file has been reached.
1023
1024 @<Types in the outer block@>=
1025 @!alpha_file=packed file of text_char; {files that contain textual data}
1026
1027
1028 @
1029 @^system dependencies@>
1030 Most of what we need to do with respect to input and output can be handled
1031 by the I/O facilities that are standard in \PASCAL, i.e., the routines
1032 called |get|, |put|, |eof|, and so on. But
1033 standard \PASCAL\ does not allow file variables to be associated with file
1034 names that are determined at run time, so it cannot be used to implement
1035 \BibTeX; some sort of extension to \PASCAL's ordinary |reset| and |rewrite|
1036 is crucial for our purposes. We shall assume that |name_of_file| is a variable
1037 of an appropriate type such that the \PASCAL\ run-time system being used to
1038 implement \BibTeX\ can open a file whose external name is specified by
1039 |name_of_file|. \BibTeX\ does no case conversion for file names.
1040
1041 @<Globals in the outer block@>=
1042 @!name_of_file:packed array[1..file_name_size] of char;
1043 {on some systems this is a \&{record} variable}
1044 @!name_length:0..file_name_size;
1045 {this many characters are relevant in |name_of_file| (the rest are blank)}
1046 @!name_ptr:0..file_name_size+1; {index variable into |name_of_file|}
1047
1048
1049 @
1050 @^system dependencies@>
1051 @:PASCAL H}{\ph@>
1052 The \ph\ compiler with which the present version of \TeX\ was prepared has
1053 extended the rules of \PASCAL\ in a very convenient way. To open file~|f|,
1054 we can write
1055 $$\vbox{\halign{#\hfil\qquad&#\hfil\cr
1056 |reset(f,@t\\{name}@>,'/O')|&for input;\cr
1057 |rewrite(f,@t\\{name}@>,'/O')|&for output.\cr}}$$
1058 The `\\{name}' parameter, which is of type `\ignorespaces|packed
1059 array[@t\<\\{any}>@>] of text_char|', stands for the name of
1060 the external file that is being opened for input or output.
1061 Blank spaces that might appear in \\{name} are ignored.
1062
1063 The `\.{/O}' parameter tells the operating system not to issue its own
1064 error messages if something goes wrong. If a file of the specified name
1065 cannot be found, or if such a file cannot be opened for some other reason
1066 (e.g., someone may already be trying to write the same file), we will have
1067 |@!erstat(f)<>0| after an unsuccessful |reset| or |rewrite|. This allows
1068 \TeX\ to undertake appropriate corrective action.
1069
1070 \TeX's file-opening procedures return |false| if no file identified by
1071 |name_of_file| could be opened.
1072
1073 @d reset_OK(#)==erstat(#)=0
1074 @d rewrite_OK(#)==erstat(#)=0
1075
1076 @<Procedures and functions for file-system interacting@>=
1077 function erstat(var f:file):integer; extern; {in the runtime library}
1078 @#@t\2@>
1079 function a_open_in(var f:alpha_file):boolean; {open a text file for input}
1080 begin reset(f,name_of_file,'/O'); a_open_in:=reset_OK(f);
1081 end;
1082 @#
1083 function a_open_out(var f:alpha_file):boolean; {open a text file for output}
1084 begin rewrite(f,name_of_file,'/O'); a_open_out:=rewrite_OK(f);
1085 end;
1086
1087
1088 @
1089 @^system dependencies@>
1090 Files can be closed with the \ph\ routine `|close(f)|', which should
1091 be used when all input or output with respect to |f| has been
1092 completed. This makes |f| available to be opened again, if desired;
1093 and if |f| was used for output, the |close| operation makes the
1094 corresponding external file appear on the user's area, ready to be
1095 read.
1096
1097 @<Procedures and functions for file-system interacting@>=
1098 procedure a_close(var f:alpha_file); {close a text file}
1099 begin close(f);
1100 end;
1101
1102
1103 @
1104 Text output is easy to do with the ordinary \PASCAL\ |put| procedure,
1105 so we don't have to make any other special arrangements.
1106 The treatment of text input is more difficult, however, because
1107 of the necessary translation to |ASCII_code| values, and because
1108 \TeX's conventions should be efficient and they should
1109 blend nicely with the user's operating environment.
1110
1111
1112 @
1113 Input from text files is read one line at a time, using a routine
1114 called |input_ln|. This function is defined in terms of global
1115 variables called |buffer| and |last|. The |buffer| array contains
1116 |ASCII_code| values, and |last| is an index into this array marking
1117 the end of a line of text. (Occasionally, |buffer| is used for
1118 something else, in which case it is copied to a temporary array.)
1119
1120 @<Globals in the outer block@>=
1121 @!buffer:buf_type; {usually, lines of characters being read}
1122 @!last:buf_pointer; {end of the line just input to |buffer|}
1123
1124
1125 @
1126 @^save space@>
1127 @^space savings@>
1128 @^system dependencies@>
1129 The type |buf_type| is used for |buffer|, for saved copies of it, or
1130 for scratch work. It's not |packed| because otherwise the program
1131 would run much slower on some systems (more than 25 percent slower,
1132 for example, on a TOPS-20 operating system). But on systems that are
1133 byte-addressable and that have a good compiler, packing |buf_type|
1134 would save lots of space without much loss of speed. Other modules
1135 that have packable arrays are also marked with a ``space savings''
1136 index entry.
1137
1138 @<Types in the outer block@>=
1139 @!buf_pointer = 0..buf_size; {an index into a |buf_type|}
1140 @!buf_type = array[buf_pointer] of ASCII_code; {for various buffers}
1141
1142
1143 @
1144 @^kludge@>
1145 And while we're at it, we declare another buffer for general use.
1146 Because buffers are not packed and can get large, we use |sv_buffer|
1147 several purposes; this is a bit kludgy, but it helps make the stack
1148 space not overflow on some machines. It's used when reading the
1149 entire database file (in the \.{read} command) and when doing
1150 name-handling (through the alias |name_buf|) in the |built_in|
1151 functions \.{format.names\$} and \.{num.names\$}.
1152
1153 @<Globals in the outer block@>=
1154 @!sv_buffer : buf_type;
1155 @!sv_ptr1 : buf_pointer;
1156 @!sv_ptr2 : buf_pointer;
1157 @!tmp_ptr,@!tmp_end_ptr : integer; {copy pointers only, usually for buffers}
1158
1159
1160 @
1161 @.BibTeX capacity exceeded@>
1162 When something in the program wants to be bigger or something out
1163 there wants to be smaller, it's time to call it a run. Here's the
1164 first of several macros that have associated procedures so that they
1165 produce less inline code.
1166
1167 @d overflow(#)==begin {fatal error---close up shop}
1168 print_overflow;
1169 print_ln(#:0);
1170 goto close_up_shop;
1171 end
1172
1173 @<Procedures and functions for all file I/O, error messages, and such@>=
1174 procedure print_overflow;
1175 begin
1176 print ('Sorry---you''ve exceeded BibTeX''s ');
1177 mark_fatal;
1178 end;
1179
1180
1181 @
1182 @.this can't happen@>
1183 When something happens that the program thinks is impossible,
1184 call the maintainer.
1185
1186 @d confusion(#)==begin {fatal error---close up shop}
1187 print (#);
1188 print_confusion;
1189 goto close_up_shop;
1190 end
1191
1192 @<Procedures and functions for all file I/O, error messages, and such@>=
1193 procedure print_confusion;
1194 begin
1195 print_ln ('---this can''t happen');
1196 print_ln ('*Please notify the BibTeX maintainer*');
1197 mark_fatal;
1198 end;
1199
1200
1201 @
1202 @:BibTeX capacity exceeded}{\quad buffer size@>
1203 When a buffer overflows, it's time to complain (and then quit).
1204
1205 @<Procedures and functions for all file I/O, error messages, and such@>=
1206 procedure buffer_overflow;
1207 begin
1208 overflow('buffer size ',buf_size);
1209 end;
1210
1211
1212 @
1213 @:BibTeX capacity exceeded}{\quad buffer size@>
1214 The |input_ln| function brings the next line of input from the
1215 specified file into available positions of the buffer array and
1216 returns the value |true|, unless the file has already been entirely
1217 read, in which case it returns |false| and sets |last:=0|. In
1218 general, the |ASCII_code| numbers that represent the next line of the
1219 file are input into |buffer[0]|, |buffer[1]|, \dots, |buffer[last-1]|;
1220 and the global variable |last| is set equal to the length of the line.
1221 Trailing |white_space| characters are removed from the line
1222 (|white_space| characters are explained in the character-set section%
1223 ---most likely they're blanks); thus, either |last=0| (in which case
1224 the line was entirely blank) or |lex_class[buffer[last-1]]<>white_space|.
1225 An overflow error is given if the normal actions of |input_ln| would
1226 make |last>buf_size|.
1227
1228 Standard \PASCAL\ says that a file should have |eoln| immediately
1229 before |eof|, but \BibTeX\ needs only a weaker restriction: If |eof|
1230 occurs in the middle of a line, the system function |eoln| should return
1231 a |true| result (even though |f^| will be undefined).
1232
1233 @<Procedures and functions for all file I/O, error messages, and such@>=
1234 function input_ln(var f:alpha_file) : boolean;
1235 {inputs the next line or returns |false|}
1236 label loop_exit;
1237 begin
1238 last:=0;
1239 if (eof(f)) then input_ln:=false
1240 else
1241 begin
1242 while (not eoln(f)) do
1243 begin
1244 if (last >= buf_size) then
1245 buffer_overflow;
1246 buffer[last]:=xord[f^];
1247 get(f); incr(last);
1248 end;
1249 get(f);
1250 while (last > 0) do {remove trailing |white_space|}
1251 if (lex_class[buffer[last-1]] = white_space) then
1252 decr(last)
1253 else
1254 goto loop_exit;
1255 loop_exit:
1256 input_ln:=true;
1257 end;
1258 end;
1259
1260
1261
1262 @* String handling.
1263 \BibTeX\ uses variable-length strings of seven-bit characters.
1264 Since \PASCAL\ does not have a well-developed string mechanism,
1265 \BibTeX\ does all its string processing by home-grown
1266 (predominantly \TeX's) methods.
1267 Unlike \TeX, however, \BibTeX\ does not use a |pool_file| for
1268 string storage; it creates its few pre-defined strings at run-time.
1269
1270 The necessary operations are handled with a simple data structure.
1271 The array |str_pool| contains all the (seven-bit) ASCII codes in all
1272 the strings \BibTeX\ must ever search for (generally identifiers
1273 names), and the array |str_start| contains indices of the starting
1274 points of each such string. Strings are referred to by integer
1275 numbers, so that string number |s| comprises the characters
1276 |str_pool[j]| for |str_start[s]<=j<str_start[s+1]|. Additional integer
1277 variables |pool_ptr| and |str_ptr| indicate the number of entries used
1278 so far in |str_pool| and |str_start|; locations |str_pool[pool_ptr]|
1279 and |str_start[str_ptr]| are ready for the next string to be
1280 allocated. Location |str_start[0]| is unused so that hashing will
1281 work correctly.
1282
1283 Elements of the |str_pool| array must be ASCII codes that can actually be
1284 printed; i.e., they must have an |xchr| equivalent in the local
1285 character set.
1286
1287 @<Globals in the outer block@>=
1288 @!str_pool : packed array[pool_pointer] of ASCII_code; {the characters}
1289 @!str_start : packed array[str_number] of pool_pointer; {the starting pointers}
1290 @!pool_ptr : pool_pointer; {first unused position in |str_pool|}
1291 @!str_ptr : str_number; {start of the current string being created}
1292 @!str_num : str_number; {general index variable into |str_start|}
1293 @!p_ptr1,@!p_ptr2 : pool_pointer; {several procedures use these locally}
1294
1295
1296 @
1297 Where |pool_pointer| and |str_number| are pointers into |str_pool| and
1298 |str_start|.
1299
1300 @<Types in the outer block@>=
1301 @!pool_pointer = 0..pool_size; {for variables that point into |str_pool|}
1302 @!str_number = 0..max_strings; {for variables that point into |str_start|}
1303
1304
1305 @
1306 These macros send a string in |str_pool| to an output file.
1307
1308 @d max_pop = 3 {---see the |built_in| functions section}
1309 @#
1310 @d print_pool_str(#) == print_a_pool_str(#)
1311 {making this a procedure saves a little space}
1312 @#
1313 @d trace_pr_pool_str(#) == begin
1314 out_pool_str(log_file,#);
1315 end
1316
1317
1318 @
1319 @^kludge@>
1320 @^system dependencies@>
1321 @:this can't happen}{\quad Illegal string number@>
1322 And here are the associated procedures. Note: The |term_out| file is
1323 system dependent.
1324
1325 @<Procedures and functions for all file I/O, error messages, and such@>=
1326 procedure out_pool_str (var f:alpha_file; @!s:str_number);
1327 var i:pool_pointer;
1328 begin {allowing |str_ptr <= s < str_ptr+max_pop| is a \.{.bst}-stack kludge}
1329 if ((s<0) or (s>=str_ptr+max_pop) or (s>=max_strings)) then
1330 confusion ('Illegal string number:',s:0);
1331 for i := str_start[s] to str_start[s+1]-1 do
1332 write(f,xchr[str_pool[i]]);
1333 end;
1334 @#
1335 procedure print_a_pool_str (@!s:str_number);
1336 begin
1337 out_pool_str(term_out,s);
1338 out_pool_str(log_file,s);
1339 end;
1340
1341
1342 @
1343 @.WEB@>
1344 Several of the elementary string operations are performed using \.{WEB}
1345 macros instead of using \PASCAL\ procedures, because many of the
1346 operations are done quite frequently and we want to avoid the
1347 overhead of procedure calls. For example, here is
1348 a simple macro that computes the length of a string.
1349
1350 @d length(#) == (str_start[#+1]-str_start[#])
1351 {the number of characters in string number \#}
1352
1353
1354 @
1355 @:BibTeX capacity exceeded}{\quad pool size@>
1356 Strings are created by appending character codes to |str_pool|.
1357 The macro called |append_char|, defined here, does not check to see if the
1358 value of |pool_ptr| has gotten too high; this test is supposed to be
1359 made before |append_char| is used.
1360
1361 To test if there is room to append |l| more characters to |str_pool|,
1362 we shall write |str_room(l)|, which aborts \BibTeX\ and gives an
1363 error message if there isn't enough room.
1364
1365 @d append_char(#) == {put |ASCII_code| \# at the end of |str_pool|}
1366 begin str_pool[pool_ptr]:=#; incr(pool_ptr);
1367 end
1368 @#
1369 @d str_room(#) == {make sure that the pool hasn't overflowed}
1370 begin
1371 if (pool_ptr+# > pool_size) then
1372 pool_overflow;
1373 end
1374
1375 @<Procedures and functions for all file I/O, error messages, and such@>=
1376 procedure pool_overflow;
1377 begin
1378 overflow('pool size ',pool_size);
1379 end;
1380
1381
1382 @
1383 @:BibTeX capacity exceeded}{\quad number of strings@>
1384 Once a sequence of characters has been appended to |str_pool|, it
1385 officially becomes a string when the function |make_string| is called.
1386 It returns the string number of the string it just made.
1387
1388 @<Procedures and functions for handling numbers, characters, and strings@>=
1389 function make_string : str_number; {current string enters the pool}
1390 begin
1391 if (str_ptr=max_strings) then
1392 overflow('number of strings ',max_strings);
1393 incr(str_ptr);
1394 str_start[str_ptr]:=pool_ptr;
1395 make_string := str_ptr - 1;
1396 end;
1397
1398
1399 @
1400 These macros destroy and recreate the string at the end of the pool.
1401
1402 @d flush_string == begin
1403 decr(str_ptr);
1404 pool_ptr := str_start[str_ptr];
1405 end
1406 @#
1407 @d unflush_string == begin
1408 incr(str_ptr);
1409 pool_ptr := str_start[str_ptr];
1410 end
1411
1412
1413 @
1414 This subroutine compares string |s| with another string that appears
1415 in the buffer |buf| between positions |bf_ptr| and |bf_ptr+len-1|; the
1416 result is |true| if and only if the strings are equal.
1417
1418 @<Procedures and functions for handling numbers, characters, and strings@>=
1419 function str_eq_buf (@!s:str_number; var buf:buf_type;
1420 @!bf_ptr,@!len:buf_pointer) : boolean;
1421 {test equality of strings}
1422 label exit;
1423 var i : buf_pointer; {running}
1424 @!j : pool_pointer; {indices}
1425 begin
1426 if (length(s) <> len) then {strings of unequal length}
1427 begin
1428 str_eq_buf := false;
1429 return;
1430 end;
1431 i := bf_ptr;
1432 j := str_start[s];
1433 while (j < str_start[s+1]) do
1434 begin
1435 if (str_pool[j] <> buf[i]) then
1436 begin
1437 str_eq_buf := false;
1438 return;
1439 end;
1440 incr(i);
1441 incr(j);
1442 end;
1443 str_eq_buf := true;
1444 exit:
1445 end;
1446
1447
1448 @
1449 This subroutine compares two |str_pool| strings and returns true
1450 |true| if and only if the strings are equal.
1451
1452 @<Procedures and functions for handling numbers, characters, and strings@>=
1453 function str_eq_str (@!s1,@!s2:str_number) : boolean;
1454 label exit;
1455 begin
1456 if (length(s1) <> length(s2)) then
1457 begin
1458 str_eq_str := false;
1459 return;
1460 end;
1461 p_ptr1 := str_start[s1];
1462 p_ptr2 := str_start[s2];
1463 while (p_ptr1 < str_start[s1+1]) do
1464 begin
1465 if (str_pool[p_ptr1] <> str_pool[p_ptr2]) then
1466 begin
1467 str_eq_str := false;
1468 return;
1469 end;
1470 incr(p_ptr1);
1471 incr(p_ptr2);
1472 end;
1473 str_eq_str:=true;
1474 exit:
1475 end;
1476
1477
1478 @
1479 @:BibTeX capacity exceeded}{\quad file name size@>
1480 This procedure copies file name |file_name| into the beginning of
1481 |name_of_file|, if it will fit. It also sets the global variable
1482 |name_length| to the appropriate value.
1483
1484 @<Procedures and functions for file-system interacting@>=
1485 procedure start_name (@!file_name:str_number);
1486 var p_ptr: pool_pointer; {running index}
1487 begin
1488 if (length(file_name) > file_name_size) then
1489 begin
1490 print ('File=');
1491 print_pool_str (file_name);
1492 print_ln (',');
1493 file_nm_size_overflow;
1494 end;
1495 name_ptr := 1;
1496 p_ptr := str_start[file_name];
1497 while (p_ptr < str_start[file_name+1]) do
1498 begin
1499 name_of_file[name_ptr] := chr (str_pool[p_ptr]);
1500 incr(name_ptr); incr(p_ptr);
1501 end;
1502 name_length := length(file_name);
1503 end;
1504
1505
1506 @
1507 @:BibTeX capacity exceeded}{\quad file name size@>
1508 Yet another complaint-before-quitting.
1509
1510 @<Procedures and functions for all file I/O, error messages, and such@>=
1511 procedure file_nm_size_overflow;
1512 begin
1513 overflow('file name size ',file_name_size);
1514 end;
1515
1516
1517 @
1518 @:BibTeX capacity exceeded}{\quad file name size@>
1519 This procedure copies file extension |ext| into the array
1520 |name_of_file| starting at position |name_length+1|. It also sets the
1521 global variable |name_length| to the appropriate value.
1522
1523 @<Procedures and functions for file-system interacting@>=
1524 procedure add_extension(@!ext:str_number);
1525 var p_ptr: pool_pointer; {running index}
1526 begin
1527 if (name_length + length(ext) > file_name_size) then
1528 begin
1529 print ('File=',name_of_file,', extension=');
1530 print_pool_str (ext); print_ln (',');
1531 file_nm_size_overflow;
1532 end;
1533 name_ptr := name_length + 1;
1534 p_ptr := str_start[ext];
1535 while (p_ptr < str_start[ext+1]) do
1536 begin
1537 name_of_file[name_ptr] := chr (str_pool[p_ptr]);
1538 incr(name_ptr); incr(p_ptr);
1539 end;
1540 name_length := name_length + length(ext);
1541 name_ptr := name_length+1;
1542 while (name_ptr <= file_name_size) do {pad with blanks}
1543 begin
1544 name_of_file[name_ptr] := ' ';
1545 incr(name_ptr);
1546 end;
1547 end;
1548
1549
1550 @
1551 @:BibTeX capacity exceeded}{\quad file name size@>
1552 This procedure copies the default logical area name |area| into the
1553 array |name_of_file| starting at position 1, after shifting up the
1554 rest of the filename. It also sets the global variable |name_length|
1555 to the appropriate value.
1556
1557 @<Procedures and functions for file-system interacting@>=
1558 procedure add_area(@!area:str_number);
1559 var p_ptr: pool_pointer; {running index}
1560 begin
1561 if (name_length + length(area) > file_name_size) then
1562 begin
1563 print ('File=');
1564 print_pool_str (area); print (name_of_file,',');
1565 file_nm_size_overflow;
1566 end;
1567 name_ptr := name_length;
1568 while (name_ptr > 0) do {shift up name}
1569 begin
1570 name_of_file[name_ptr+length(area)] := name_of_file[name_ptr];
1571 decr(name_ptr);
1572 end;
1573 name_ptr := 1;
1574 p_ptr := str_start[area];
1575 while (p_ptr < str_start[area+1]) do
1576 begin
1577 name_of_file[name_ptr] := chr (str_pool[p_ptr]);
1578 incr(name_ptr); incr(p_ptr);
1579 end;
1580 name_length := name_length + length(area);
1581 end;
1582
1583
1584 @
1585 This system-independent procedure converts upper-case characters to
1586 lower case for the specified part of |buf|. It is system independent
1587 because it uses only the internal representation for characters.
1588
1589 @d case_difference = "a" - "A"
1590
1591 @<Procedures and functions for handling numbers, characters, and strings@>=
1592 procedure lower_case (var buf:buf_type; @!bf_ptr,@!len:buf_pointer);
1593 var i:buf_pointer;
1594 begin
1595 if (len > 0) then
1596 for i := bf_ptr to bf_ptr+len-1 do
1597 if ((buf[i]>="A") and (buf[i]<="Z")) then
1598 buf[i] := buf[i] + case_difference;
1599 end;
1600
1601
1602 @
1603 This system-independent procedure is the same as the previous except
1604 that it converts lower- to upper-case letters.
1605
1606 @<Procedures and functions for handling numbers, characters, and strings@>=
1607 procedure upper_case (var buf:buf_type; @!bf_ptr,@!len:buf_pointer);
1608 var i:buf_pointer;
1609 begin
1610 if (len > 0) then
1611 for i := bf_ptr to bf_ptr+len-1 do
1612 if ((buf[i]>="a") and (buf[i]<="z")) then
1613 buf[i] := buf[i] - case_difference;
1614 end;
1615
1616
1617
1618 @* The hash table.
1619 All static strings that \BibTeX\ might have to search for, generally
1620 identifiers, are stored and retrieved by means of a fairly standard
1621 hash-table algorithm (but slightly altered here) called the method of
1622 ``coalescing lists''
1623 (cf.\ Algorithm 6.4C in {\sl The Art of Computer Programming}).
1624 Once a string enters the table, it is never removed. The actual
1625 sequence of characters forming a string is stored in the |str_pool|
1626 array.
1627
1628 The hash table consists of the four arrays |hash_next|, |hash_text|,
1629 |hash_ilk|, and |ilk_info|. The first array, |hash_next[p]|, points
1630 to the next identifier belonging to the same coalesced list as the
1631 identifier corresponding to~|p|. The second, |hash_text[p]|, points
1632 to the |str_start| entry for |p|'s string. If position~|p| of the hash
1633 table is empty, we have |hash_text[p]=0|; if position |p| is either
1634 empty or the end of a coalesced hash list, we have
1635 |hash_next[p]=empty|; an auxiliary pointer variable called |hash_used|
1636 is maintained in such a way that all locations |p>=hash_used| are
1637 nonempty. The third, |hash_ilk[p]|, tells how this string is used (as
1638 ordinary text, as a variable name, as an \.{.aux} file command, etc).
1639 The fourth, |ilk_info[p]|, contains information specific to the
1640 corresponding |hash_ilk|---for |integer_ilk|s: the integer's value;
1641 for |cite_ilk|s: a pointer into |cite_list|; for |lc_cite_ilk|s: a
1642 pointer to a |cite_ilk| string; for |command_ilk|s: a constant to be
1643 used in a |case| statement; for |bst_fn_ilk|s: function-specific
1644 information; for |macro_ilk|s: a pointer to its definition string; for
1645 |control_seq_ilk|s: a constant for use in a |case| statement; for all
1646 other |ilk|s it contains no information. This |ilk|-specific
1647 information is set in other parts of the program rather than here in
1648 the hashing routine.
1649
1650 @d hash_base = empty + 1 {lowest numbered hash-table location}
1651 @d hash_max = hash_base + hash_size - 1 {highest numbered hash-table location}
1652 @d hash_is_full == (hash_used=hash_base) {test if all positions are occupied}
1653 @#
1654 @d text_ilk = 0 {a string of ordinary text}
1655 @d integer_ilk = 1 {an integer (possibly with a |minus_sign|)}
1656 @d aux_command_ilk = 2 {an \.{.aux}-file command}
1657 @d aux_file_ilk = 3 {an \.{.aux} file name}
1658 @d bst_command_ilk = 4 {a \.{.bst}-file command}
1659 @d bst_file_ilk = 5 {a \.{.bst} file name}
1660 @d bib_file_ilk = 6 {a \.{.bib} file name}
1661 @d file_ext_ilk = 7 {one of \.{.aux}, \.{.bst}, \.{.bib}, \.{.bbl},
1662 or \.{.blg}}
1663 @d file_area_ilk = 8 {one of \.{texinputs:} or \.{texbib:}}
1664 @d cite_ilk = 9 {a \.{\\citation} argument}
1665 @d lc_cite_ilk = 10 {a \.{\\citation} argument converted to lower case}
1666 @d bst_fn_ilk = 11 {a \.{.bst} function name}
1667 @d bib_command_ilk = 12 {a \.{.bib}-file command}
1668 @d macro_ilk = 13 {a \.{.bst} macro or a \.{.bib} string}
1669 @d control_seq_ilk = 14 {a control sequence specifying a foreign character}
1670 @d last_ilk = 14 {the same number as on the line above}
1671
1672 @<Types in the outer block@>=
1673 @!hash_loc=hash_base..hash_max; {a location within the hash table}
1674 @!hash_pointer=empty..hash_max; {either |empty| or a |hash_loc|}
1675 @#
1676 @!str_ilk=0..last_ilk; {the legal string types}
1677
1678
1679 @
1680 @<Globals in the outer block@>=
1681 @!hash_next : packed array[hash_loc] of hash_pointer; {coalesced-list link}
1682 @!hash_text : packed array[hash_loc] of str_number; {pointer to a string}
1683 @!hash_ilk : packed array[hash_loc] of str_ilk; {the type of string}
1684 @!ilk_info : packed array[hash_loc] of integer; {|ilk|-specific info}
1685 @!hash_used : hash_base..hash_max+1; {allocation pointer for hash table}
1686 @!hash_found : boolean; {set to |true| if it's already in the hash table}
1687 @!dummy_loc : hash_loc; {receives |str_lookup| value whenever it's useless}
1688
1689
1690 @
1691 @<Local variables for initialization@>=
1692 @!k:hash_loc;
1693
1694
1695 @
1696 Now it's time to initialize the hash table; note that |str_start[0]|
1697 must be unused if |hash_text[k] := 0| is to have the desired effect.
1698
1699 @<Set initial values of key variables@>=
1700 for k:=hash_base to hash_max do
1701 begin
1702 hash_next[k] := empty;
1703 hash_text[k] := 0; {thus, no need to initialize |hash_ilk| or |ilk_info|}
1704 end;
1705 hash_used := hash_max + 1; {nothing in table initially}
1706
1707
1708 @
1709 Here is the subroutine that searches the hash table for a
1710 (string,~|str_ilk|) pair, where the string is of length |l>=0| and
1711 appears in |buffer[j..(j+l-1)]|. If it finds the pair, it returns the
1712 corresponding hash-table location and sets the global variable
1713 |hash_found| to |true|. Otherwise it sets |hash_found| to |false|,
1714 and if the parameter |insert_it| is |true|, it inserts the pair into
1715 the hash table, inserts the string into |str_pool| if not previously
1716 encountered, and returns its location. Note that two different pairs
1717 can have the same string but different |str_ilk|s, in which case the
1718 second pair encountered, if |insert_it| were |true|, would be inserted
1719 into the hash table though its string wouldn't be inserted into
1720 |str_pool| because it would already be there.
1721
1722 @d max_hash_value = hash_prime+hash_prime-2+127 {|h|'s maximum value}
1723 @d do_insert == true {insert string if not found in hash table}
1724 @d dont_insert == false {don't insert string}
1725 @#
1726 @d str_found = 40 {go here when you've found the string}
1727 @d str_not_found = 45 {go here when you haven't}
1728
1729 @<Procedures and functions for handling numbers, characters, and strings@>=
1730 function str_lookup(var buf:buf_type; @!j,@!l:buf_pointer; @!ilk:str_ilk;
1731 @!insert_it:boolean) : hash_loc; {search the hash table}
1732 label str_found,@!str_not_found;
1733 var h:0..max_hash_value; {hash code}
1734 @!p:hash_loc; {index into |hash_| arrays}
1735 @!k:buf_pointer; {index into |buf| array}
1736 @!old_string:boolean; {set to |true| if it's an already encountered string}
1737 @!str_num:str_number; {pointer to an already encountered string}
1738 begin
1739 @<Compute the hash code |h|@>;
1740 p:=h+hash_base; {start searching here; note that |0<=h<hash_prime|}
1741 hash_found := false;
1742 old_string := false;
1743 loop
1744 begin
1745 @<Process the string if we've already encountered it@>;
1746 if (hash_next[p]=empty) then {location |p| may or may not be empty}
1747 begin
1748 if (not insert_it) then goto str_not_found;
1749 @<Insert pair into hash table and make |p| point to it@>;
1750 goto str_found;
1751 end;
1752 p:=hash_next[p]; {old and new locations |p| are not empty}
1753 end;
1754 str_not_found: do_nothing; {don't insert pair; function value meaningless}
1755 str_found: str_lookup:=p;
1756 end;
1757
1758
1759 @
1760 @^for loops@>
1761 @.WEB@>
1762 The value of |hash_prime| should be roughly 85\% of |hash_size|, and
1763 it should be a prime number
1764 (it should also be less than $2^{14} + 2^{6} = 16320$ because of
1765 \.{WEB}'s simple-macro bound). The theory of hashing tells us to expect
1766 fewer than two table probes, on the average, when the search is
1767 successful.
1768
1769 @<Compute the hash code |h|@>=
1770 begin
1771 h := 0; {note that this works for zero-length strings}
1772 k := j;
1773 while (k < j+l) do {not a |for| loop in case |j = l = 0|}
1774 begin
1775 h:=h+h+buf[k];
1776 while (h >= hash_prime) do h:=h-hash_prime;
1777 incr(k);
1778 end;
1779 end
1780
1781
1782 @
1783 Here we handle the case in which we've already encountered this
1784 string; note that even if we have, we'll still have to insert the pair
1785 into the hash table if |str_ilk| doesn't match.
1786
1787 @<Process the string if we've already encountered it@>=
1788 begin
1789 if (hash_text[p]>0) then {there's something here}
1790 if (str_eq_buf(hash_text[p],buf,j,l)) then {it's the right string}
1791 if (hash_ilk[p] = ilk) then {it's the right |str_ilk|}
1792 begin
1793 hash_found := true;
1794 goto str_found;
1795 end
1796 else
1797 begin {it's the wrong |str_ilk|}
1798 old_string := true;
1799 str_num := hash_text[p];
1800 end;
1801 end
1802
1803
1804 @
1805 @^for loops@>
1806 @:BibTeX capacity exceeded}{\quad hash size@>
1807 This code inserts the pair in the appropriate unused location.
1808
1809 @<Insert pair into hash table and make |p| point to it@>=
1810 begin
1811 if (hash_text[p]>0) then {location |p| isn't empty}
1812 begin
1813 repeat if (hash_is_full) then overflow('hash size ',hash_size);
1814 decr(hash_used);
1815 until (hash_text[hash_used]=0); {search for an empty location}
1816 hash_next[p]:=hash_used;
1817 p:=hash_used;
1818 end; {now location |p| is empty}
1819 if (old_string) then {it's an already encountered string}
1820 hash_text[p] := str_num
1821 else
1822 begin {it's a new string}
1823 str_room(l); {make sure it'll fit in |str_pool|}
1824 k := j;
1825 while (k < j+l) do {not a |for| loop in case |j = l = 0|}
1826 begin
1827 append_char(buf[k]);
1828 incr(k);
1829 end;
1830 hash_text[p] := make_string; {and make it official}
1831 end;
1832 hash_ilk[p] := ilk;
1833 end
1834
1835
1836 @
1837 @^string pool@>
1838 Now that we've defined the hash-table workings we can initialize the
1839 string pool. Unlike \TeX, \BibTeX\ does not use a |pool_file| for
1840 string storage; instead it inserts its pre-defined strings into
1841 |str_pool|---this makes one file fewer for the \BibTeX\ implementor
1842 to deal with. This section initializes |str_pool|; the pre-defined
1843 strings will be inserted into it shortly; and other strings are
1844 inserted while processing the input files.
1845
1846 @<Set initial values of key variables@>=
1847 pool_ptr:=0; str_ptr:=1; {hash table must have |str_start[0]| unused}
1848 str_start[str_ptr]:=pool_ptr;
1849
1850
1851 @
1852 The longest pre-defined string determines type definitions used to
1853 insert the pre-defined strings into |str_pool|.
1854
1855 @d longest_pds=12 {the length of `\.{change.case\$}'}
1856
1857 @<Types in the outer block@>=
1858 @!pds_loc = 1..longest_pds;
1859 @!pds_len = 0..longest_pds;
1860 @!pds_type = packed array [pds_loc] of char;
1861
1862
1863 @
1864 The variables in this program beginning with |s_| specify the
1865 locations in |str_pool| for certain often-used strings. Those here
1866 have to do with the file system; the next section will actually insert
1867 them into |str_pool|.
1868
1869 @<Globals in the outer block@>=
1870 @!s_aux_extension : str_number; {\.{.aux}}
1871 @!s_log_extension : str_number; {\.{.blg}}
1872 @!s_bbl_extension : str_number; {\.{.bbl}}
1873 @!s_bst_extension : str_number; {\.{.bst}}
1874 @!s_bib_extension : str_number; {\.{.bib}}
1875 @!s_bst_area : str_number; {\.{texinputs:}}
1876 @!s_bib_area : str_number; {\.{texbib:}}
1877
1878
1879 @
1880 @^important note@>
1881 @^system dependencies@>
1882 It's time to insert some of the pre-defined strings into |str_pool|
1883 (and thus the hash table). These system-dependent strings should
1884 contain no upper-case letters, and they must all be exactly
1885 |longest_pds| characters long (even if fewer characters are actually
1886 stored). The |pre_define| routine appears shortly.
1887
1888 Important notes: These pre-definitions must not have any glitches or
1889 the program may bomb because the |log_file| hasn't been opened yet,
1890 and |text_ilk|s should be pre-defined later, for
1891 \.{.bst}-function-execution purposes.
1892
1893 @<Pre-define certain strings@>=
1894 pre_define('.aux ',4,file_ext_ilk);
1895 s_aux_extension := hash_text[pre_def_loc];
1896 pre_define('.bbl ',4,file_ext_ilk);
1897 s_bbl_extension := hash_text[pre_def_loc];
1898 pre_define('.blg ',4,file_ext_ilk);
1899 s_log_extension := hash_text[pre_def_loc];
1900 pre_define('.bst ',4,file_ext_ilk);
1901 s_bst_extension := hash_text[pre_def_loc];
1902 pre_define('.bib ',4,file_ext_ilk);
1903 s_bib_extension := hash_text[pre_def_loc];
1904 pre_define('texinputs: ',10,file_area_ilk);
1905 s_bst_area := hash_text[pre_def_loc];
1906 pre_define('texbib: ',7,file_area_ilk);
1907 s_bib_area := hash_text[pre_def_loc];
1908
1909
1910 @
1911 This global variable gives the hash-table location of pre-defined
1912 strings generated by calls to |str_lookup|.
1913
1914 @<Globals in the outer block@>=
1915 @!pre_def_loc : hash_loc;
1916
1917
1918 @
1919 This procedure initializes a pre-defined string of length at most
1920 |longest_pds|.
1921
1922 @<Procedures and functions for handling numbers, characters, and strings@>=
1923 procedure pre_define (@!pds:pds_type; @!len:pds_len; @!ilk:str_ilk);
1924 var i : pds_len;
1925 begin
1926 for i:=1 to len do
1927 buffer[i] := xord[pds[i]];
1928 pre_def_loc := str_lookup(buffer,1,len,ilk,do_insert);
1929 end;
1930
1931
1932 @
1933 These constants all begin with |n_| and are used for the |case|
1934 statement that determines which command to execute. The variable
1935 |command_num| is set to one of these and is used to do the branching,
1936 but it must have the full |integer| range because at times it can
1937 assume an arbitrary |ilk_info| value (though it will be one of the
1938 values here when we actually use it).
1939
1940 @d n_aux_bibdata = 0 {\.{\\bibdata}}
1941 @d n_aux_bibstyle = 1 {\.{\\bibstyle}}
1942 @d n_aux_citation = 2 {\.{\\citation}}
1943 @d n_aux_input = 3 {\.{\\@@input}}
1944 @#
1945 @d n_bst_entry = 0 {\.{entry}}
1946 @d n_bst_execute = 1 {\.{execute}}
1947 @d n_bst_function = 2 {\.{function}}
1948 @d n_bst_integers = 3 {\.{integers}}
1949 @d n_bst_iterate = 4 {\.{iterate}}
1950 @d n_bst_macro = 5 {\.{macro}}
1951 @d n_bst_read = 6 {\.{read}}
1952 @d n_bst_reverse = 7 {\.{reverse}}
1953 @d n_bst_sort = 8 {\.{sort}}
1954 @d n_bst_strings = 9 {\.{strings}}
1955 @#
1956 @d n_bib_comment = 0 {\.{comment}}
1957 @d n_bib_preamble = 1 {\.{preamble}}
1958 @d n_bib_string = 2 {\.{string}}
1959
1960 @<Globals in the outer block@>=
1961 @!command_num : integer;
1962
1963
1964 @
1965 @^important note@>
1966 Now we pre-define the command strings; they must all be exactly
1967 |longest_pds| characters long.
1968
1969 Important note: These pre-definitions must not have any glitches or
1970 the program may bomb because the |log_file| hasn't been opened yet.
1971
1972 @<Pre-define certain strings@>=
1973 pre_define('\citation ',9,aux_command_ilk);
1974 ilk_info[pre_def_loc] := n_aux_citation;
1975 pre_define('\bibdata ',8,aux_command_ilk);
1976 ilk_info[pre_def_loc] := n_aux_bibdata;
1977 pre_define('\bibstyle ',9,aux_command_ilk);
1978 ilk_info[pre_def_loc] := n_aux_bibstyle;
1979 pre_define('\@@input ',7,aux_command_ilk);
1980 ilk_info[pre_def_loc] := n_aux_input;
1981 @#
1982 pre_define('entry ',5,bst_command_ilk);
1983 ilk_info[pre_def_loc] := n_bst_entry;
1984 pre_define('execute ',7,bst_command_ilk);
1985 ilk_info[pre_def_loc] := n_bst_execute;
1986 pre_define('function ',8,bst_command_ilk);
1987 ilk_info[pre_def_loc] := n_bst_function;
1988 pre_define('integers ',8,bst_command_ilk);
1989 ilk_info[pre_def_loc] := n_bst_integers;
1990 pre_define('iterate ',7,bst_command_ilk);
1991 ilk_info[pre_def_loc] := n_bst_iterate;
1992 pre_define('macro ',5,bst_command_ilk);
1993 ilk_info[pre_def_loc] := n_bst_macro;
1994 pre_define('read ',4,bst_command_ilk);
1995 ilk_info[pre_def_loc] := n_bst_read;
1996 pre_define('reverse ',7,bst_command_ilk);
1997 ilk_info[pre_def_loc] := n_bst_reverse;
1998 pre_define('sort ',4,bst_command_ilk);
1999 ilk_info[pre_def_loc] := n_bst_sort;
2000 pre_define('strings ',7,bst_command_ilk);
2001 ilk_info[pre_def_loc] := n_bst_strings;
2002 @#
2003 pre_define('comment ',7,bib_command_ilk);
2004 ilk_info[pre_def_loc] := n_bib_comment;
2005 pre_define('preamble ',8,bib_command_ilk);
2006 ilk_info[pre_def_loc] := n_bib_preamble;
2007 pre_define('string ',6,bib_command_ilk);
2008 ilk_info[pre_def_loc] := n_bib_string;
2009
2010
2011
2012 @* Scanning an input line.
2013 This section describes the various |buffer| scanning routines. The
2014 two global variables |buf_ptr1| and |buf_ptr2| are used in scanning an
2015 input line. Between scans, |buf_ptr1| points to the first character
2016 of the current token and |buf_ptr2| points to that of the next. The
2017 global variable |last|, set by the function |input_ln|, marks the end
2018 of the current line; it equals 0 at the end of the current file. All
2019 the procedures and functions in this section will indicate an
2020 end-of-line when it's the end of the file.
2021
2022 @d token_len == (buf_ptr2 - buf_ptr1) {of the current token}
2023 @d scan_char == buffer[buf_ptr2] {the current character}
2024
2025 @<Globals in the outer block@>=
2026 @!buf_ptr1:buf_pointer; {points to the first position of the current token}
2027 @!buf_ptr2:buf_pointer; {used to find the end of the current token}
2028
2029
2030 @
2031 These macros send the current token, in |buffer[buf_ptr1]| to
2032 |buffer[buf_ptr2-1]|, to an output file.
2033
2034 @d print_token == print_a_token {making this a procedure saves a little space}
2035 @#
2036 @d trace_pr_token == begin
2037 out_token(log_file);
2038 end
2039
2040
2041 @
2042 @^system dependencies@>
2043 And here are the associated procedures. Note: The |term_out| file is
2044 system dependent.
2045
2046 @<Procedures and functions for all file I/O, error messages, and such@>=
2047 procedure out_token (var f:alpha_file);
2048 var i:buf_pointer;
2049 begin
2050 i := buf_ptr1;
2051 while (i < buf_ptr2) do
2052 begin
2053 write(f,xchr[buffer[i]]);
2054 incr(i);
2055 end;
2056 end;
2057 @#
2058 procedure print_a_token;
2059 begin
2060 out_token(term_out);
2061 out_token(log_file);
2062 end;
2063
2064
2065 @
2066 This function scans the |buffer| for the next token, starting at the
2067 global variable |buf_ptr2| and ending just before either the single
2068 specified stop-character or the end of the current line, whichever
2069 comes first, respectively returning |true| or |false|; afterward,
2070 |scan_char| is the first character following this token.
2071
2072 @<Procedures and functions for input scanning@>=
2073 function scan1 (@!char1:ASCII_code) : boolean;
2074 begin
2075 buf_ptr1 := buf_ptr2;
2076 {scan until end-of-line or the specified character}
2077 while ((scan_char <> char1) and (buf_ptr2 < last)) do
2078 incr(buf_ptr2);
2079 if (buf_ptr2 < last) then
2080 scan1 := true
2081 else
2082 scan1 := false;
2083 end;
2084
2085
2086 @
2087 This function is the same but stops at |white_space| characters as well.
2088
2089 @<Procedures and functions for input scanning@>=
2090 function scan1_white (@!char1:ASCII_code) : boolean;
2091 begin
2092 buf_ptr1 := buf_ptr2;
2093 {scan until end-of-line, the specified character, or |white_space|}
2094 while ((lex_class[scan_char] <> white_space) and (scan_char <> char1) and
2095 (buf_ptr2 < last)) do
2096 incr(buf_ptr2);
2097 if (buf_ptr2 < last) then
2098 scan1_white := true
2099 else
2100 scan1_white := false;
2101 end;
2102
2103
2104 @
2105 This function is similar to |scan1|, but stops at either of two
2106 stop-characters as well as the end of the current line.
2107
2108 @<Procedures and functions for input scanning@>=
2109 function scan2 (@!char1,@!char2:ASCII_code) : boolean;
2110 begin
2111 buf_ptr1 := buf_ptr2;
2112 {scan until end-of-line or the specified characters}
2113 while ((scan_char <> char1) and (scan_char <> char2) and (buf_ptr2 < last)) do
2114 incr(buf_ptr2);
2115 if (buf_ptr2 < last) then
2116 scan2 := true
2117 else
2118 scan2 := false;
2119 end;
2120
2121
2122 @
2123 This function is the same but stops at |white_space| characters as well.
2124
2125 @<Procedures and functions for input scanning@>=
2126 function scan2_white (@!char1,@!char2:ASCII_code) : boolean;
2127 begin
2128 buf_ptr1 := buf_ptr2;
2129 {scan until end-of-line, the specified characters, or |white_space|}
2130 while ((scan_char <> char1) and (scan_char <> char2) and
2131 (lex_class[scan_char] <> white_space) and (buf_ptr2 < last)) do
2132 incr(buf_ptr2);
2133 if (buf_ptr2 < last) then
2134 scan2_white := true
2135 else
2136 scan2_white := false;
2137 end;
2138
2139
2140 @
2141 This function is similar to |scan2|, but stops at either of three
2142 stop-characters as well as the end of the current line.
2143
2144 @<Procedures and functions for input scanning@>=
2145 function scan3 (@!char1,@!char2,@!char3:ASCII_code) : boolean;
2146 begin
2147 buf_ptr1 := buf_ptr2;
2148 {scan until end-of-line or the specified characters}
2149 while ((scan_char <> char1) and (scan_char <> char2) and
2150 (scan_char <> char3) and (buf_ptr2 < last)) do
2151 incr(buf_ptr2);
2152 if (buf_ptr2 < last) then
2153 scan3 := true
2154 else
2155 scan3 := false;
2156 end;
2157
2158
2159 @
2160 This function scans for letters, stopping at the first nonletter; it
2161 returns |true| if there is at least one letter.
2162
2163 @<Procedures and functions for input scanning@>=
2164 function scan_alpha : boolean;
2165 begin
2166 buf_ptr1 := buf_ptr2;
2167 {scan until end-of-line or a nonletter}
2168 while ((lex_class[scan_char] = alpha) and (buf_ptr2 < last)) do
2169 incr(buf_ptr2);
2170 if (token_len = 0) then
2171 scan_alpha := false
2172 else
2173 scan_alpha := true;
2174 end;
2175
2176
2177 @
2178 These are the possible values for |scan_result|; they're set by the
2179 |scan_identifier| procedure and are described in the next section.
2180
2181 @d id_null = 0
2182 @d specified_char_adjacent = 1
2183 @d other_char_adjacent = 2
2184 @d white_adjacent = 3
2185
2186 @<Globals in the outer block@>=
2187 @!scan_result : id_null..white_adjacent;
2188
2189
2190 @
2191 This procedure scans for an identifier, stopping at the first
2192 |illegal_id_char|, or stopping at the first character if it's
2193 |numeric|. It sets the global variable |scan_result| to |id_null| if
2194 the identifier is null, else to |white_adjacent| if it ended at a
2195 |white_space| character or an end-of-line, else to
2196 |specified_char_adjacent| if it ended at one of |char1| or |char2| or
2197 |char3|, else to |other_char_adjacent| if it ended at a nonspecified,
2198 non|white_space| |illegal_id_char|. By convention, when some calling
2199 code really wants just one or two ``specified'' characters, it merely
2200 repeats one of the characters.
2201
2202 @<Procedures and functions for input scanning@>=
2203 procedure scan_identifier (@!char1,@!char2,@!char3:ASCII_code);
2204 begin
2205 buf_ptr1 := buf_ptr2;
2206 if (lex_class[scan_char] <> numeric) then
2207 {scan until end-of-line or an |illegal_id_char|}
2208 while ((id_class[scan_char] = legal_id_char) and (buf_ptr2 < last)) do
2209 incr(buf_ptr2);
2210 if (token_len = 0) then
2211 scan_result := id_null
2212 else if ((lex_class[scan_char] = white_space) or (buf_ptr2 = last)) then
2213 scan_result := white_adjacent
2214 else if ((scan_char = char1) or (scan_char = char2) or (scan_char = char3))
2215 then
2216 scan_result := specified_char_adjacent
2217 else
2218 scan_result := other_char_adjacent;
2219 end;
2220
2221
2222 @
2223 The next two procedures scan for an integer, setting the global
2224 variable |token_value| to the corresponding integer.
2225
2226 @d char_value == (scan_char - "0") {the value of the digit being scanned}
2227
2228 @<Globals in the outer block@>=
2229 @!token_value : integer; {the numeric value of the current token}
2230
2231
2232 @
2233 This function scans for a nonnegative integer, stopping at the first
2234 nondigit; it sets the value of |token_value| accordingly. It returns
2235 |true| if the token was a legal nonnegative integer (i.e., consisted
2236 of one or more digits).
2237
2238 @<Procedures and functions for input scanning@>=
2239 function scan_nonneg_integer : boolean;
2240 begin
2241 buf_ptr1 := buf_ptr2;
2242 token_value := 0;
2243 {scan until end-of-line or a nondigit}
2244 while ((lex_class[scan_char] = numeric) and (buf_ptr2 < last)) do
2245 begin
2246 token_value := token_value*10 + char_value;
2247 incr(buf_ptr2);
2248 end;
2249 if (token_len = 0) then {there were no digits}
2250 scan_nonneg_integer := false
2251 else
2252 scan_nonneg_integer := true;
2253 end;
2254
2255
2256 @
2257 This procedure scans for an integer, stopping at the first nondigit;
2258 it sets the value of |token_value| accordingly. It returns |true| if
2259 the token was a legal integer (i.e., consisted of an optional
2260 |minus_sign| followed by one or more digits).
2261
2262 @d negative == (sign_length = 1) {if this integer is negative}
2263
2264 @<Procedures and functions for input scanning@>=
2265 function scan_integer : boolean;
2266 var sign_length : 0..1; {1 if there's a |minus_sign|, 0 if not}
2267 begin
2268 buf_ptr1 := buf_ptr2;
2269 if (scan_char = minus_sign) then {it's a negative number}
2270 begin
2271 sign_length := 1;
2272 incr(buf_ptr2); {skip over the |minus_sign|}
2273 end
2274 else
2275 sign_length := 0;
2276 token_value := 0;
2277 {scan until end-of-line or a nondigit}
2278 while ((lex_class[scan_char] = numeric) and (buf_ptr2 < last)) do
2279 begin
2280 token_value := token_value*10 + char_value;
2281 incr(buf_ptr2);
2282 end;
2283 if (negative) then
2284 token_value := -token_value;
2285 if (token_len = sign_length) then {there were no digits}
2286 scan_integer := false
2287 else
2288 scan_integer := true;
2289 end;
2290
2291
2292 @
2293 This function scans over |white_space| characters, stopping either at
2294 the first nonwhite character or the end of the line, respectively
2295 returning |true| or |false|.
2296
2297 @<Procedures and functions for input scanning@>=
2298 function scan_white_space : boolean;
2299 begin
2300 {scan until end-of-line or a nonwhite}
2301 while ((lex_class[scan_char] = white_space) and (buf_ptr2 < last)) do
2302 incr(buf_ptr2);
2303 if (buf_ptr2 < last) then
2304 scan_white_space := true
2305 else
2306 scan_white_space := false;
2307 end;
2308
2309
2310 @
2311 The |print_bad_input_line| procedure prints the current input line,
2312 splitting it at the character being scanned: It prints |buffer[0]|,
2313 |buffer[1]|, \dots, |buffer[buf_ptr2-1]| on one line and
2314 |buffer[buf_ptr2]|, \dots, |buffer[last-1]| on the next (and both
2315 lines start with a colon between two |space|s). Each |white_space|
2316 character is printed as a |space|.
2317
2318 @<Procedures and functions for all file I/O, error messages, and such@>=
2319 procedure print_bad_input_line;
2320 var bf_ptr : buf_pointer;
2321 begin
2322 print (' : ');
2323 bf_ptr := 0;
2324 while (bf_ptr < buf_ptr2) do
2325 begin
2326 if (lex_class[buffer[bf_ptr]] = white_space) then
2327 print (xchr[space])
2328 else
2329 print (xchr[buffer[bf_ptr]]);
2330 incr(bf_ptr);
2331 end;
2332 print_newline;
2333 print (' : ');
2334 bf_ptr := 0;
2335 while (bf_ptr < buf_ptr2) do
2336 begin
2337 print (xchr[space]);
2338 incr(bf_ptr);
2339 end;
2340 bf_ptr := buf_ptr2;
2341 while (bf_ptr < last) do
2342 begin
2343 if (lex_class[buffer[bf_ptr]] = white_space) then
2344 print (xchr[space])
2345 else
2346 print (xchr[buffer[bf_ptr]]);
2347 incr(bf_ptr);
2348 end;
2349 print_newline;@/
2350 bf_ptr := 0;
2351 while ((bf_ptr < buf_ptr2) and (lex_class[buffer[bf_ptr]] = white_space)) do
2352 incr(bf_ptr);
2353 if (bf_ptr = buf_ptr2) then
2354 print_ln ('(Error may have been on previous line)');
2355 mark_error;
2356 end;
2357
2358
2359 @
2360 This little procedure exists because it's used by at least two other
2361 procedures and thus saves some space.
2362
2363 @<Procedures and functions for all file I/O, error messages, and such@>=
2364 procedure print_skipping_whatever_remains;
2365 begin
2366 print ('I''m skipping whatever remains of this ');
2367 end;
2368
2369
2370
2371 @* Getting the top-level auxiliary file name.
2372 @^system dependencies@>
2373 These modules read the name of the top-level \.{.aux} file. Some
2374 systems will try to find this on the command line; if it's not there
2375 it will come from the user's terminal. In either case, the name goes
2376 into the |char| array |name_of_file|, and the files relevant to this
2377 name are opened.
2378
2379 @d aux_found=41 {go here when the \.{.aux} name is legit}
2380 @d aux_not_found=46 {go here when it's not}
2381
2382 @<Globals in the outer block@>=
2383 @!aux_name_length : 0..file_name_size+1; {\.{.aux} name sans extension}
2384
2385
2386 @
2387 @^system dependencies@>
2388 @^user abuse@>
2389 I mean, this is truly disgraceful. A user has to type something in to
2390 the terminal just once during the entire run. And it's not some
2391 complicated string where you have to get every last punctuation mark
2392 just right, and it's not some fancy list where you get nervous because
2393 if you forget one item you have to type the whole thing again; it's
2394 just a simple, ordinary, file name. Now you'd think a five-year-old
2395 could do it; you'd think it's so simple a user should be able to do it
2396 in his sleep. But noooooooooo. He had to sit there droning on and on
2397 about who knows what until he exceeded the bounds of common sense, and
2398 he probably didn't even realize it. Just pitiful. What's this world
2399 coming to? We should probably just delete all his files and be done
2400 with him. Note: The |term_out| file is system dependent.
2401
2402 @d sam_you_made_the_file_name_too_long == begin
2403 sam_too_long_file_name_print;
2404 goto aux_not_found;
2405 end
2406
2407 @<Procedures and functions for all file I/O, error messages, and such@>=
2408 procedure sam_too_long_file_name_print;
2409 begin
2410 write (term_out,'File name `');
2411 name_ptr := 1;
2412 while (name_ptr <= aux_name_length) do
2413 begin
2414 write (term_out,name_of_file[name_ptr]);
2415 incr(name_ptr);
2416 end;
2417 write_ln (term_out,''' is too long');
2418 end;
2419
2420
2421 @
2422 @^system dependencies@>
2423 @^user abuse@>
2424 We've abused the user enough for one section; suffice it to
2425 say here that most of what we said last module still applies.
2426 Note: The |term_out| file is system dependent.
2427
2428 @d sam_you_made_the_file_name_wrong == begin
2429 sam_wrong_file_name_print;
2430 goto aux_not_found;
2431 end
2432
2433 @<Procedures and functions for all file I/O, error messages, and such@>=
2434 procedure sam_wrong_file_name_print;
2435 begin
2436 write (term_out,'I couldn''t open file name `');
2437 name_ptr := 1;
2438 while (name_ptr <= name_length) do
2439 begin
2440 write (term_out,name_of_file[name_ptr]);
2441 incr(name_ptr);
2442 end;
2443 write_ln (term_out,'''');
2444 end;
2445
2446
2447 @
2448 @^system dependencies@>
2449 This procedure consists of a loop that reads and processes a (nonnull)
2450 \.{.aux} file name. It's this module and the next two that must be
2451 changed on those systems using command-line arguments. Note: The
2452 |term_out| and |term_in| files are system dependent.
2453
2454 @<Procedures and functions for the reading and processing of input files@>=
2455 procedure get_the_top_level_aux_file_name;
2456 label aux_found,@!aux_not_found;
2457 var @<Variables for possible command-line processing@>@/
2458 begin
2459 check_cmnd_line := false; {many systems will change this}
2460 loop
2461 begin
2462 if (check_cmnd_line) then
2463 @<Process a \(o)possible command line@>
2464 else
2465 begin
2466 write (term_out,'Please type input file name (no extension)--');
2467 if (eoln(term_in)) then {so the first |read| works}
2468 read_ln (term_in);
2469 aux_name_length := 0;
2470 while (not eoln(term_in)) do
2471 begin
2472 if (aux_name_length = file_name_size) then
2473 begin
2474 while (not eoln(term_in)) do {discard the rest of the line}
2475 get(term_in);
2476 sam_you_made_the_file_name_too_long;
2477 end;
2478 incr(aux_name_length);
2479 name_of_file[aux_name_length] := term_in^;
2480 get(term_in);
2481 end;
2482 end;
2483 @<Handle this \.{.aux} name@>;
2484 aux_not_found:
2485 check_cmnd_line := false;
2486 end;
2487 aux_found: {now we're ready to read the \.{.aux} file}
2488 end;
2489
2490
2491 @
2492 @^system dependencies@>
2493 The switch |check_cmnd_line| tells us whether we're to check for a
2494 possible command-line argument.
2495
2496 @<Variables for possible command-line processing@>=
2497 @!check_cmnd_line : boolean; {|true| if we're to check the command line}
2498
2499
2500 @
2501 @^system dependencies@>
2502 Here's where we do the real command-line work. Those systems needing
2503 more than a single module to handle the task should add the extras to
2504 the ``System-dependent changes'' section.
2505
2506 @<Process a \(o)possible command line@>=
2507 begin
2508 do_nothing; {the ``default system'' doesn't use the command line}
2509 end
2510
2511
2512 @
2513 Here we orchestrate this \.{.aux} name's handling: we add the various
2514 extensions, try to open the files with the resulting name, and
2515 store the name strings we'll need later.
2516
2517 @<Handle this \.{.aux} name@>=
2518 begin
2519 if ((aux_name_length + length(s_aux_extension) > file_name_size) or@|
2520 (aux_name_length + length(s_log_extension) > file_name_size) or@|
2521 (aux_name_length + length(s_bbl_extension) > file_name_size)) then
2522 sam_you_made_the_file_name_too_long;
2523 @<Add extensions and open files@>;
2524 @<Put this name into the hash table@>;
2525 goto aux_found;
2526 end
2527
2528
2529 @
2530 Here we set up definitions and declarations for files opened in this
2531 section. Each element in |aux_list| (except for
2532 |aux_list[aux_stack_size]|, which is always unused) is a pointer to
2533 the appropriate |str_pool| string representing the \.{.aux} file name.
2534 The array |aux_file| contains the corresponding \PASCAL\ |file|
2535 variables.
2536
2537 @d cur_aux_str == aux_list[aux_ptr] {shorthand for the current \.{.aux} file}
2538 @d cur_aux_file == aux_file[aux_ptr] {shorthand for the current |aux_file|}
2539 @d cur_aux_line == aux_ln_stack[aux_ptr] {line number of current \.{.aux} file}
2540
2541 @<Globals in the outer block@>=
2542 @!aux_file : array[aux_number] of alpha_file; {open \.{.aux} |file| variables}
2543 @!aux_list : array[aux_number] of str_number; {the open \.{.aux} file list}
2544 @!aux_ptr : aux_number; {points to the currently open \.{.aux} file}
2545 @!aux_ln_stack : array[aux_number] of integer; {open \.{.aux} line numbers}
2546 @#
2547 @!top_lev_str : str_number; {the top-level \.{.aux} file's name}
2548 @#
2549 @!log_file : alpha_file; {the |file| variable for the \.{.blg} file}
2550 @!bbl_file : alpha_file; {the |file| variable for the \.{.bbl} file}
2551
2552
2553 @
2554 Where |aux_number| is the obvious.
2555
2556 @<Types in the outer block@>=
2557 @!aux_number = 0..aux_stack_size; {gives the |aux_list| range}
2558
2559
2560 @
2561 @^system dependencies@>
2562 We must make sure the (top-level) \.{.aux}, \.{.blg}, and \.{.bbl}
2563 files can be opened.
2564
2565 @<Add extensions and open files@>=
2566 begin
2567 name_length := aux_name_length; {set to last used position}
2568 add_extension (s_aux_extension); {this also sets |name_length|}
2569 aux_ptr := 0; {initialize the \.{.aux} file stack}
2570 if (not a_open_in(cur_aux_file)) then
2571 sam_you_made_the_file_name_wrong;
2572 @#
2573 name_length := aux_name_length;
2574 add_extension (s_log_extension); {this also sets |name_length|}
2575 if (not a_open_out(log_file)) then
2576 sam_you_made_the_file_name_wrong;
2577 @#
2578 name_length := aux_name_length;
2579 add_extension (s_bbl_extension); {this also sets |name_length|}
2580 if (not a_open_out(bbl_file)) then
2581 sam_you_made_the_file_name_wrong;
2582 end
2583
2584
2585 @
2586 @:this can't happen}{\quad Already encountered auxiliary file@>
2587 This code puts the \.{.aux} file name, both with and without the
2588 extension, into the hash table, and it initializes |aux_list|. Note
2589 that all previous top-level \.{.aux}-file stuff must have been
2590 successful.
2591
2592 @<Put this name into the hash table@>=
2593 begin
2594 name_length := aux_name_length;
2595 add_extension (s_aux_extension); {this also sets |name_length|}
2596 name_ptr := 1;
2597 while (name_ptr <= name_length) do
2598 begin
2599 buffer[name_ptr] := xord[name_of_file[name_ptr]];
2600 incr(name_ptr);
2601 end;
2602 top_lev_str := hash_text[
2603 str_lookup(buffer,1,aux_name_length,text_ilk,do_insert)];
2604 cur_aux_str := hash_text[
2605 str_lookup(buffer,1,name_length,aux_file_ilk,do_insert)];
2606 {note that this has initialized |aux_list|}
2607 if (hash_found) then
2608 begin
2609 trace
2610 print_aux_name;
2611 ecart@/
2612 confusion ('Already encountered auxiliary file');
2613 end;
2614 cur_aux_line := 0; {this finishes initializing the top-level \.{.aux} file}
2615 end
2616
2617
2618 @
2619 Print the name of the current \.{.aux} file, followed by a |newline|.
2620
2621 @<Procedures and functions for all file I/O, error messages, and such@>=
2622 procedure print_aux_name;
2623 begin
2624 print_pool_str (cur_aux_str);
2625 print_newline;
2626 end;
2627
2628
2629
2630 @* Reading the auxiliary file(s).
2631 @^auxiliary-file commands@>
2632 Now it's time to read the \.{.aux} file. The only commands we handle
2633 are \.{\\citation} (there can be arbitrarily many, each having
2634 arbitrarily many arguments), \.{\\bibdata} (there can be just one, but
2635 it can have arbitrarily many arguments), \.{\\bibstyle} (there can be
2636 just one, and it can have just one argument), and \.{\\@@input} (there
2637 can be arbitrarily many, each with one argument, and they can be
2638 nested to a depth of |aux_stack_size|). Each of these commands is
2639 assumed to be on just a single line. The rest of the \.{.aux} file is
2640 ignored.
2641
2642 @d aux_done=31 {go here when finished with the \.{.aux} files}
2643
2644 @<Labels in the outer block@>=
2645 ,@!aux_done
2646
2647
2648 @
2649 We keep reading and processing input lines until none left. This is
2650 part of the main program; hence, because of the |aux_done| label,
2651 there's no conventional |begin|-|end| pair surrounding the entire
2652 module.
2653
2654 @<Read the \.{.aux} file@>=
2655 print ('The top-level auxiliary file: ');
2656 print_aux_name;
2657 loop
2658 begin {|pop_the_aux_stack| will exit the loop}
2659 incr(cur_aux_line);
2660 if (not input_ln(cur_aux_file)) then {end of current \.{.aux} file}
2661 pop_the_aux_stack
2662 else
2663 get_aux_command_and_process;
2664 end;
2665 trace
2666 trace_pr_ln ('Finished reading the auxiliary file(s)');
2667 ecart@/
2668 aux_done:
2669 last_check_for_aux_errors;
2670
2671
2672 @
2673 When we find a bug, we print a message and flush the rest of the line.
2674 This macro must be called from within a procedure that has an |exit|
2675 label.
2676
2677 @d aux_err_return == begin
2678 aux_err_print;
2679 return; {flush this input line}
2680 end
2681
2682 @d aux_err(#) == begin
2683 print (#);
2684 aux_err_return;
2685 end
2686
2687 @<Procedures and functions for all file I/O, error messages, and such@>=
2688 procedure aux_err_print;
2689 begin
2690 print ('---line ',cur_aux_line:0,' of file ');
2691 print_aux_name;@/
2692 print_bad_input_line; {this call does the |mark_error|}
2693 print_skipping_whatever_remains;
2694 print_ln ('command')
2695 end;
2696
2697
2698 @
2699 @:this can't happen}{\quad Illegal auxiliary-file command@>
2700 Here are a bunch of macros whose print statements are used at least
2701 twice. Thus we save space by making the statements procedures. This
2702 macro complains when there's a repeated command that's to be used just
2703 once.
2704
2705 @d aux_err_illegal_another(#) == begin
2706 aux_err_illegal_another_print (#);
2707 aux_err_return;
2708 end
2709
2710 @<Procedures and functions for all file I/O, error messages, and such@>=
2711 procedure aux_err_illegal_another_print (@!cmd_num : integer);
2712 begin
2713 print ('Illegal, another \bib');
2714 case (cmd_num) of
2715 n_aux_bibdata : print ('data');
2716 n_aux_bibstyle : print ('style');
2717 othercases
2718 confusion ('Illegal auxiliary-file command')
2719 endcases;
2720 print (' command');
2721 end;
2722
2723
2724 @
2725 This one complains when a command is missing its |right_brace|.
2726
2727 @d aux_err_no_right_brace == begin
2728 aux_err_no_right_brace_print;
2729 aux_err_return;
2730 end
2731
2732 @<Procedures and functions for all file I/O, error messages, and such@>=
2733 procedure aux_err_no_right_brace_print;
2734 begin
2735 print ('No "',xchr[right_brace],'"');
2736 end;
2737
2738
2739 @
2740 This one complains when a command has stuff after its |right_brace|.
2741
2742 @d aux_err_stuff_after_right_brace == begin
2743 aux_err_stuff_after_right_brace_print;
2744 aux_err_return;
2745 end
2746
2747 @<Procedures and functions for all file I/O, error messages, and such@>=
2748 procedure aux_err_stuff_after_right_brace_print;
2749 begin
2750 print ('Stuff after "',xchr[right_brace],'"');
2751 end;
2752
2753
2754 @
2755 And this one complains when a command has |white_space| in its
2756 argument.
2757
2758 @d aux_err_white_space_in_argument == begin
2759 aux_err_white_space_in_argument_print;
2760 aux_err_return;
2761 end
2762
2763 @<Procedures and functions for all file I/O, error messages, and such@>=
2764 procedure aux_err_white_space_in_argument_print;
2765 begin
2766 print ('White space in argument');
2767 end;
2768
2769
2770 @
2771 @^auxiliary-file commands@>
2772 @:this can't happen}{\quad Unknown auxiliary-file command@>
2773 We're not at the end of an \.{.aux} file, so we see if the current
2774 line might be a command of interest. A command of interest will be a
2775 line without blanks, consisting of a command name, a |left_brace|, one
2776 or more arguments separated by commas, and a |right_brace|.
2777
2778 @<Scan for and process an \.{.aux} command@>=
2779 procedure get_aux_command_and_process;
2780 label exit;
2781 begin
2782 buf_ptr2 := 0; {mark the beginning of the next token}
2783 if (not scan1(left_brace)) then {no |left_brace|---flush line}
2784 return;
2785 command_num := ilk_info[
2786 str_lookup(buffer,buf_ptr1,token_len,aux_command_ilk,dont_insert)];
2787 if (hash_found) then
2788 case (command_num) of
2789 n_aux_bibdata : aux_bib_data_command;
2790 n_aux_bibstyle : aux_bib_style_command;
2791 n_aux_citation : aux_citation_command;
2792 n_aux_input : aux_input_command;
2793 othercases
2794 confusion ('Unknown auxiliary-file command')
2795 endcases;
2796 exit:
2797 end;
2798
2799
2800 @
2801 Here we introduce some variables for processing a \.{\\bibdata}
2802 command. Each element in |bib_list| (except for
2803 |bib_list[max_bib_files]|, which is always unused) is a pointer to the
2804 appropriate |str_pool| string representing the \.{.bib} file name.
2805 The array |bib_file| contains the corresponding \PASCAL\ |file|
2806 variables.
2807
2808 @d cur_bib_str == bib_list[bib_ptr] {shorthand for current \.{.bib} file}
2809 @d cur_bib_file == bib_file[bib_ptr] {shorthand for current |bib_file|}
2810
2811 @<Globals in the outer block@>=
2812 @!bib_list : array[bib_number] of str_number; {the \.{.bib} file list}
2813 @!bib_ptr : bib_number; {pointer for the current \.{.bib} file}
2814 @!num_bib_files : bib_number; {the total number of \.{.bib} files}
2815 @!bib_seen : boolean; {|true| if we've already seen a \.{\\bibdata} command}
2816 @!bib_file : array[bib_number] of alpha_file; {corresponding |file| variables}
2817
2818
2819 @
2820 Where |bib_number| is the obvious.
2821
2822 @<Types in the outer block@>=
2823 @!bib_number = 0..max_bib_files; {gives the |bib_list| range}
2824
2825
2826 @
2827 @<Set initial values of key variables@>=
2828 bib_ptr := 0; {this makes |bib_list| empty}
2829 bib_seen := false; {we haven't seen a \.{\\bibdata} command yet}
2830
2831
2832 @
2833 @:auxiliary-file commands}{\quad \.{\\bibdata}@>
2834 A \.{\\bibdata} command will have its arguments between braces and
2835 separated by commas. There must be exactly one such command in the
2836 \.{.aux} file(s). All upper-case letters are converted to lower case.
2837
2838 @<Procedures and functions for the reading and processing of input files@>=
2839 procedure aux_bib_data_command;
2840 label exit;
2841 begin
2842 if (bib_seen) then
2843 aux_err_illegal_another (n_aux_bibdata);
2844 bib_seen := true; {now we've seen a \.{\\bibdata} command}
2845 while (scan_char <> right_brace) do
2846 begin
2847 incr(buf_ptr2); {skip over the previous stop-character}
2848 if (not scan2_white(right_brace,comma)) then
2849 aux_err_no_right_brace;
2850 if (lex_class[scan_char] = white_space) then
2851 aux_err_white_space_in_argument;
2852 if ((last > buf_ptr2+1) and (scan_char = right_brace)) then
2853 aux_err_stuff_after_right_brace;
2854 @<Open a \.{.bib} file@>;
2855 end;
2856 exit:
2857 end;
2858
2859
2860 @
2861 Here's a procedure we'll need shortly. It prints the name of the
2862 current \.{.bib} file, followed by a |newline|.
2863
2864 @<Procedures and functions for all file I/O, error messages, and such@>=
2865 procedure print_bib_name;
2866 begin
2867 print_pool_str (cur_bib_str);
2868 print_pool_str (s_bib_extension);
2869 print_newline;
2870 end;
2871
2872
2873 @
2874 This macro is similar to |aux_err| but it complains specifically about
2875 opening a file for a \.{\\bibdata} command.
2876
2877 @d open_bibdata_aux_err(#) == begin
2878 print (#);
2879 print_bib_name;
2880 aux_err_return; {this does the |mark_error|}
2881 end
2882
2883
2884 @
2885 @:BibTeX capacity exceeded}{\quad number of \.{.bib} files@>
2886 Now we add the just-found argument to |bib_list| if it hasn't already
2887 been encountered as a \.{\\bibdata} argument and if, after appending
2888 the |s_bib_extension| string, the resulting file name can be opened.
2889
2890 @<Open a \.{.bib} file@>=
2891 begin
2892 if (bib_ptr = max_bib_files) then
2893 overflow('number of database files ',max_bib_files);
2894 cur_bib_str := hash_text[
2895 str_lookup(buffer,buf_ptr1,token_len,bib_file_ilk,do_insert)];
2896 if (hash_found) then {already encountered this as a \.{\\bibdata} argument}
2897 open_bibdata_aux_err ('This database file appears more than once: ');
2898 start_name (cur_bib_str);
2899 add_extension (s_bib_extension);
2900 if (not a_open_in(cur_bib_file)) then
2901 begin
2902 add_area (s_bib_area);
2903 if (not a_open_in(cur_bib_file)) then
2904 open_bibdata_aux_err ('I couldn''t open database file ');
2905 end;
2906 trace
2907 trace_pr_pool_str (cur_bib_str);
2908 trace_pr_pool_str (s_bib_extension);
2909 trace_pr_ln (' is a bibdata file');
2910 ecart@/
2911 incr(bib_ptr);
2912 end
2913
2914
2915 @
2916 Here we introduce some variables for processing a \.{\\bibstyle}
2917 command.
2918
2919 @<Globals in the outer block@>=
2920 @!bst_seen : boolean; {|true| if we've already seen a \.{\\bibstyle} command}
2921 @!bst_str : str_number; {the string number for the \.{.bst} file}
2922 @!bst_file : alpha_file; {the corresponding |file| variable}
2923
2924
2925 @
2926 And we initialize.
2927
2928 @<Set initial values of key variables@>=
2929 bst_str := 0; {mark |bst_str| as unused}
2930 bst_seen := false; {we haven't seen a \.{\\bibstyle} command yet}
2931
2932
2933 @
2934 @:auxiliary-file commands}{\quad \.{\\bibstyle}@>
2935 A \.{\\bibstyle} command will have exactly one argument, and it will
2936 be between braces. There must be exactly one such command in the
2937 \.{.aux} file(s). All upper-case letters are converted to lower case.
2938
2939 @<Procedures and functions for the reading and processing of input files@>=
2940 procedure aux_bib_style_command;
2941 label exit;
2942 begin
2943 if (bst_seen) then
2944 aux_err_illegal_another (n_aux_bibstyle);
2945 bst_seen := true; {now we've seen a \.{\\bibstyle} command}
2946 incr(buf_ptr2); {skip over the |left_brace|}
2947 if (not scan1_white(right_brace)) then
2948 aux_err_no_right_brace;
2949 if (lex_class[scan_char] = white_space) then
2950 aux_err_white_space_in_argument;
2951 if (last > buf_ptr2+1) then
2952 aux_err_stuff_after_right_brace;
2953 @<Open the \.{.bst} file@>;
2954 exit:
2955 end;
2956
2957
2958 @
2959 @:this can't happen}{\quad Already encountered style file@>
2960 Now we open the file whose name is the just-found argument appended
2961 with the |s_bst_extension| string, if possible.
2962
2963 @<Open the \.{.bst} file@>=
2964 begin
2965 bst_str := hash_text[
2966 str_lookup(buffer,buf_ptr1,token_len,bst_file_ilk,do_insert)];
2967 if (hash_found) then
2968 begin
2969 trace
2970 print_bst_name;
2971 ecart@/
2972 confusion ('Already encountered style file');
2973 end;
2974 start_name (bst_str);
2975 add_extension (s_bst_extension);
2976 if (not a_open_in(bst_file)) then
2977 begin
2978 add_area (s_bst_area);
2979 if (not a_open_in(bst_file)) then
2980 begin
2981 print ('I couldn''t open style file ');
2982 print_bst_name;@/
2983 bst_str := 0; {mark as unused again}
2984 aux_err_return;
2985 end;
2986 end;
2987 print ('The style file: ');
2988 print_bst_name;
2989 end
2990
2991
2992 @
2993 Print the name of the \.{.bst} file, followed by a |newline|.
2994
2995 @<Procedures and functions for all file I/O, error messages, and such@>=
2996 procedure print_bst_name;
2997 begin
2998 print_pool_str (bst_str);
2999 print_pool_str (s_bst_extension);
3000 print_newline;
3001 end;
3002
3003
3004 @
3005 Here we introduce some variables for processing a \.{\\citation}
3006 command. Each element in |cite_list| (except for
3007 |cite_list[max_cites]|, which is always unused) is a pointer to the
3008 appropriate |str_pool| string. The cite-key list is kept in order of
3009 occurrence with duplicates removed.
3010
3011 @d cur_cite_str == cite_list[cite_ptr] {shorthand for the current cite key}
3012
3013 @<Globals in the outer block@>=
3014 @!cite_list : packed array[cite_number] of str_number; {the cite-key list}
3015 @!cite_ptr : cite_number; {pointer for the current cite key}
3016 @!entry_cite_ptr : cite_number; {cite pointer for the current entry}
3017 @!num_cites : cite_number; {the total number of distinct cite keys}
3018 @!old_num_cites : cite_number; {set to a previous |num_cites| value}
3019 @!citation_seen : boolean; {|true| if we've seen a \.{\\citation} command}
3020 @!cite_loc : hash_loc; {the hash-table location of a cite key}
3021 @!lc_cite_loc : hash_loc; {and of its lower-case equivalent}
3022 @!lc_xcite_loc : hash_loc; {a second |lc_cite_loc| variable}
3023 @!cite_found : boolean; {|true| if we've already seen this cite key}
3024 @!all_entries : boolean; {|true| if we're to use the entire database}
3025 @!all_marker : cite_number; {we put the other entries in |cite_list| here}
3026
3027
3028 @
3029 Where |cite_number| is the obvious.
3030
3031 @<Types in the outer block@>=
3032 @!cite_number = 0..max_cites; {gives the |cite_list| range}
3033
3034
3035 @
3036 @<Set initial values of key variables@>=
3037 cite_ptr := 0; {this makes |cite_list| empty}
3038 citation_seen := false; {we haven't seen a \.{\\citation} command yet}
3039 all_entries := false; {by default, use just the entries explicitly named}
3040
3041
3042 @
3043 @^case mismatch@>
3044 @^entire database inclusion@>
3045 @^whole database inclusion@>
3046 @:LaTeX}{\LaTeX@>
3047 @:auxiliary-file commands}{\quad \.{\\citation}@>
3048 A \.{\\citation} command will have its arguments between braces and
3049 separated by commas. Upper/lower cases are considered to be different
3050 for \.{\\citation} arguments, which is the same as the rest of \LaTeX\
3051 but different from the rest of \BibTeX. A cite key needn't exactly
3052 case-match its corresponding database key to work, although two cite
3053 keys that are case-mismatched will produce an error message.
3054 (A {\sl case mismatch\/} is a mismatch, but only because of a case
3055 difference.)
3056
3057 A \.{\\citation} command having \.{*} as an argument indicates that
3058 the entire database will be included (almost as if a \.{\\nocite}
3059 command that listed every cite key in the database, in order, had been
3060 given at the corresponding spot in the \.{.tex} file).
3061
3062 @d next_cite = 23 {read the next argument}
3063
3064 @<Procedures and functions for the reading and processing of input files@>=
3065 procedure aux_citation_command;
3066 label next_cite,@!exit;
3067 begin
3068 citation_seen := true; {now we've seen a \.{\\citation} command}
3069 while (scan_char <> right_brace) do
3070 begin
3071 incr(buf_ptr2); {skip over the previous stop-character}
3072 if (not scan2_white(right_brace,comma)) then
3073 aux_err_no_right_brace;
3074 if (lex_class[scan_char] = white_space) then
3075 aux_err_white_space_in_argument;
3076 if ((last > buf_ptr2+1) and (scan_char = right_brace)) then
3077 aux_err_stuff_after_right_brace;
3078 @<Check the \(ci)cite key@>;
3079 next_cite:
3080 end;
3081 exit:
3082 end;
3083
3084
3085 @
3086 @^kludge@>
3087 We must check if (the lower-case version of) this cite key has been
3088 previously encountered, and proceed accordingly. The alias kludge
3089 helps make the stack space not overflow on some machines.
3090
3091 @d ex_buf1== ex_buf {an alias, used only in this module}
3092
3093 @<Check the \(ci)cite key@>=
3094 begin
3095 trace
3096 trace_pr_token;
3097 trace_pr (' cite key encountered');
3098 ecart@/
3099 @<Check for entire database inclusion (and thus skip this cite key)@>;
3100 tmp_ptr := buf_ptr1;
3101 while (tmp_ptr < buf_ptr2) do
3102 begin
3103 ex_buf1[tmp_ptr] := buffer[tmp_ptr];
3104 incr(tmp_ptr);
3105 end;
3106 lower_case (ex_buf1, buf_ptr1, token_len); {convert to `canonical' form}
3107 lc_cite_loc := str_lookup(ex_buf1,buf_ptr1,token_len,lc_cite_ilk,do_insert);
3108 if (hash_found) then {already encountered this as a \.{\\citation} argument}
3109 @<Cite seen, don't add a cite key@>
3110 else
3111 @<Cite unseen, add a cite key@>;
3112 {it's a new cite key---add it to |cite_list|}
3113 end
3114
3115
3116 @
3117 Here we check for a \.{\\citation} command having \.{*} as an
3118 argument, indicating that the entire database will be included.
3119
3120 @<Check for entire database inclusion (and thus skip this cite key)@>=
3121 begin
3122 if (token_len = 1) then
3123 if (buffer[buf_ptr1] = star) then
3124 begin
3125 trace
3126 trace_pr_ln ('---entire database to be included');
3127 ecart@/
3128 if (all_entries) then
3129 begin
3130 print_ln ('Multiple inclusions of entire database');
3131 aux_err_return;
3132 end
3133 else
3134 begin
3135 all_entries := true;
3136 all_marker := cite_ptr;
3137 goto next_cite;
3138 end;
3139 end;
3140 end
3141
3142
3143 @
3144 @^case mismatch errors@>
3145 We've previously encountered the lower-case version, so we check that
3146 the actual version exactly matches the actual version of the
3147 previously-encountered cite key(s).
3148
3149 @<Cite seen, don't add a cite key@>=
3150 begin
3151 trace
3152 trace_pr_ln (' previously');
3153 ecart@/
3154 dummy_loc := str_lookup(buffer,buf_ptr1,token_len,cite_ilk,dont_insert);
3155 if (not hash_found) then {case mismatch error}
3156 begin
3157 print ('Case mismatch error between cite keys ');
3158 print_token;
3159 print (' and ');
3160 print_pool_str (cite_list[ilk_info[ilk_info[lc_cite_loc]]]);
3161 print_newline;
3162 aux_err_return;
3163 end;
3164 end
3165
3166
3167 @
3168 @:this can't happen}{\quad Cite hash error@>
3169 Now we add the just-found argument to |cite_list| if there isn't
3170 anything funny happening.
3171
3172 @<Cite unseen, add a cite key@>=
3173 begin
3174 trace
3175 trace_pr_newline;
3176 ecart@/
3177 cite_loc := str_lookup(buffer,buf_ptr1,token_len,cite_ilk,do_insert);
3178 if (hash_found) then
3179 hash_cite_confusion;
3180 check_cite_overflow (cite_ptr);
3181 cur_cite_str := hash_text[cite_loc];
3182 ilk_info[cite_loc] := cite_ptr;
3183 ilk_info[lc_cite_loc] := cite_loc;
3184 incr(cite_ptr);
3185 end
3186
3187
3188 @
3189 @:this can't happen}{\quad Cite hash error@>
3190 Here's a serious complaint (that is, a bug) concerning hash problems.
3191 This is the first of several similar bug-procedures that exist only
3192 because they save space.
3193
3194 @<Procedures and functions for all file I/O, error messages, and such@>=
3195 procedure hash_cite_confusion;
3196 begin
3197 confusion ('Cite hash error');
3198 end;
3199
3200
3201 @
3202 @^fetish@>
3203 @:BibTeX capacity exceeded}{\quad number of cite keys@>
3204 Complain if somebody's got a cite fetish. This procedure is called
3205 when were about to add another cite key to |cite_list|. It assumes
3206 that |cite_loc| gives the potential cite key's hash table location.
3207
3208 @<Procedures and functions for all file I/O, error messages, and such@>=
3209 procedure check_cite_overflow (@!last_cite : cite_number);
3210 begin
3211 if (last_cite = max_cites) then
3212 begin
3213 print_pool_str (hash_text[cite_loc]);
3214 print_ln (' is the key:');
3215 overflow('number of cite keys ',max_cites);
3216 end;
3217 end;
3218
3219
3220 @
3221 @:auxiliary-file commands}{\quad \.{\\\AT!input}@>
3222 An \.{\\@@input} command will have exactly one argument, it will
3223 be between braces, and it must have the |s_aux_extension|.
3224 All upper-case letters are converted to lower case.
3225
3226 @<Procedures and functions for the reading and processing of input files@>=
3227 procedure aux_input_command;
3228 label exit;
3229 var aux_extension_ok : boolean; {to check for a correct file extension}
3230 begin
3231 incr(buf_ptr2); {skip over the |left_brace|}
3232 if (not scan1_white(right_brace)) then
3233 aux_err_no_right_brace;
3234 if (lex_class[scan_char] = white_space) then
3235 aux_err_white_space_in_argument;
3236 if (last > buf_ptr2+1) then
3237 aux_err_stuff_after_right_brace;
3238 @<Push the \.{.aux} stack@>;
3239 exit:
3240 end;
3241
3242
3243 @
3244 @:BibTeX capacity exceeded}{\quad number of \.{.aux} files@>
3245 We must check that this potential \.{.aux} file won't overflow the
3246 stack, that it has the correct extension, that we haven't encountered
3247 it before (to prevent, among other things, an infinite loop).
3248
3249 @<Push the \.{.aux} stack@>=
3250 begin
3251 incr(aux_ptr);
3252 if (aux_ptr = aux_stack_size) then
3253 begin
3254 print_token; print (': ');
3255 overflow('auxiliary file depth ',aux_stack_size);
3256 end;
3257 aux_extension_ok := true;
3258 if (token_len < length(s_aux_extension)) then@/
3259 aux_extension_ok := false {else |str_eq_buf| might bomb the program}
3260 else if (not str_eq_buf(s_aux_extension, buffer,
3261 buf_ptr2-length(s_aux_extension), length(s_aux_extension))) then
3262 aux_extension_ok := false;
3263 if (not aux_extension_ok) then
3264 begin
3265 print_token;
3266 print (' has a wrong extension');
3267 decr(aux_ptr);
3268 aux_err_return;
3269 end;
3270 cur_aux_str := hash_text[
3271 str_lookup(buffer,buf_ptr1,token_len,aux_file_ilk,do_insert)];
3272 if (hash_found) then
3273 begin
3274 print ('Already encountered file ');
3275 print_aux_name;
3276 decr(aux_ptr);
3277 aux_err_return;
3278 end;
3279 @<Open this \.{.aux} file@>;
3280 end
3281
3282
3283 @
3284 We check that this \.{.aux} file can actually be opened, and then open it.
3285
3286 @<Open this \.{.aux} file@>=
3287 begin
3288 start_name (cur_aux_str); {extension already there for \.{.aux} files}
3289 name_ptr := name_length+1;
3290 while (name_ptr <= file_name_size) do {pad with blanks}
3291 begin
3292 name_of_file[name_ptr] := ' ';
3293 incr(name_ptr);
3294 end;
3295 if (not a_open_in(cur_aux_file)) then
3296 begin
3297 print ('I couldn''t open auxiliary file ');
3298 print_aux_name;
3299 decr(aux_ptr);
3300 aux_err_return;
3301 end;
3302 print ('A level-',aux_ptr:0,' auxiliary file: ');
3303 print_aux_name;
3304 cur_aux_line := 0;
3305 end
3306
3307
3308 @
3309 Here we close the current-level \.{.aux} file and go back up a level,
3310 if possible, by decrementing |aux_ptr|.
3311
3312 @<Procedures and functions for the reading and processing of input files@>=
3313 procedure pop_the_aux_stack;
3314 begin
3315 a_close (cur_aux_file);
3316 if (aux_ptr=0) then
3317 goto aux_done
3318 else
3319 decr(aux_ptr);
3320 end;
3321
3322
3323 @
3324 @^gymnastics@>
3325 That's it for processing \.{.aux} commands, except for finishing the
3326 procedural gymnastics.
3327
3328 @<Procedures and functions for the reading and processing of input files@>=
3329 @<Scan for and process an \.{.aux} command@>
3330
3331
3332 @
3333 We must complain if anything's amiss.
3334
3335 @d aux_end_err(#) == begin
3336 aux_end1_err_print;
3337 print (#);
3338 aux_end2_err_print;
3339 end
3340
3341 @<Procedures and functions for all file I/O, error messages, and such@>=
3342 procedure aux_end1_err_print;
3343 begin
3344 print ('I found no ');
3345 end;
3346 @#
3347 procedure aux_end2_err_print;
3348 begin
3349 print ('---while reading file ');
3350 print_aux_name;
3351 mark_error;
3352 end;
3353
3354
3355 @
3356 Before proceeding, we see if we have any complaints.
3357
3358 @<Procedures and functions for the reading and processing of input files@>=
3359 procedure last_check_for_aux_errors;
3360 begin
3361 num_cites := cite_ptr; {record the number of distinct cite keys}
3362 num_bib_files := bib_ptr; {and the number of \.{.bib} files}
3363 if (not citation_seen) then
3364 aux_end_err ('\citation commands')
3365 else if ((num_cites = 0) and (not all_entries)) then
3366 aux_end_err ('cite keys');
3367 if (not bib_seen) then
3368 aux_end_err ('\bibdata command')
3369 else if (num_bib_files = 0) then
3370 aux_end_err ('database files');
3371 if (not bst_seen) then
3372 aux_end_err ('\bibstyle command')
3373 else if (bst_str = 0) then
3374 aux_end_err ('style file');
3375 end;
3376
3377
3378
3379 @* Reading the style file.
3380 This part of the program reads the \.{.bst} file, which consists of a
3381 sequence of commands. Each \.{.bst} command consists of a name (for
3382 which case differences are ignored) followed by zero or more
3383 arguments, each enclosed in braces.
3384
3385 @d bst_done=32 {go here when finished with the \.{.bst} file}
3386 @d no_bst_file=9932 {go here when skipping the \.{.bst} file}
3387
3388 @<Labels in the outer block@>=
3389 ,@!bst_done,@!no_bst_file
3390
3391
3392 @
3393 The |bbl_line_num| gets initialized along with the |bst_line_num|, so
3394 it's declared here too.
3395
3396 @<Globals in the outer block@>=
3397 @!bbl_line_num : integer; {line number of the \.{.bbl} (output) file}
3398 @!bst_line_num : integer; {line number of the \.{.bst} file}
3399
3400
3401 @
3402 This little procedure exists because it's used by at least two other
3403 procedures and thus saves some space.
3404
3405 @<Procedures and functions for all file I/O, error messages, and such@>=
3406 procedure bst_ln_num_print;
3407 begin
3408 print ('--line ',bst_line_num:0,' of file ');
3409 print_bst_name;
3410 end;
3411
3412
3413 @
3414 When there's a serious error parsing the \.{.bst} file, we flush the
3415 rest of the current command; a blank line is assumed to mark the end
3416 of a command (but for the purposes of error recovery only). Thus,
3417 error recovery will be better if style designers leave blank lines
3418 between \.{.bst} commands. This macro must be called from within a
3419 procedure that has an |exit| label.
3420
3421 @d bst_err_print_and_look_for_blank_line_return ==
3422 begin
3423 bst_err_print_and_look_for_blank_line;
3424 return;
3425 end
3426
3427 @d bst_err(#) == begin {serious error during \.{.bst} parsing}
3428 print (#);
3429 bst_err_print_and_look_for_blank_line_return;
3430 end
3431
3432 @<Procedures and functions for all file I/O, error messages, and such@>=
3433 procedure bst_err_print_and_look_for_blank_line;
3434 begin
3435 print ('-');
3436 bst_ln_num_print;
3437 print_bad_input_line; {this call does the |mark_error|}
3438 while (last <> 0) do {look for a blank input line}
3439 if (not input_ln(bst_file)) then {or the end of the file}
3440 goto bst_done
3441 else
3442 incr(bst_line_num);
3443 buf_ptr2 := last; {to input the next line}
3444 end;
3445
3446
3447 @
3448 When there's a harmless error parsing the \.{.bst} file (harmless
3449 syntactically, at least) we give just a |warning_message|.
3450
3451 @d bst_warn(#) == begin {non-serious error during \.{.bst} parsing}
3452 print (#);
3453 bst_warn_print;
3454 end
3455
3456 @<Procedures and functions for all file I/O, error messages, and such@>=
3457 procedure bst_warn_print;
3458 begin
3459 bst_ln_num_print;
3460 mark_warning;
3461 end;
3462
3463
3464 @
3465 Here's the outer loop for reading the \.{.bst} file---it keeps reading
3466 and processing \.{.bst} commands until none left. This is part of the
3467 main program; hence, because of the |bst_done| label, there's no
3468 conventional |begin|-|end| pair surrounding the entire module.
3469
3470 @<Read and execute the \.{.bst} file@>=
3471 if (bst_str = 0) then {there's no \.{.bst} file to read}
3472 goto no_bst_file; {this is a |goto| so that |bst_done| is not in a block}
3473 bst_line_num := 0; {initialize things}
3474 bbl_line_num := 1; {best spot to initialize the output line number}
3475 buf_ptr2 := last; {to get the first input line}
3476 loop
3477 begin
3478 if (not eat_bst_white_space) then {the end of the \.{.bst} file}
3479 goto bst_done;
3480 get_bst_command_and_process;
3481 end;
3482 bst_done: a_close (bst_file);
3483 no_bst_file: a_close (bbl_file);
3484
3485
3486 @
3487 This \.{.bst}-specific scanning function skips over |white_space|
3488 characters (and comments) until hitting a nonwhite character or the
3489 end of the file, respectively returning |true| or |false|. It also
3490 updates |bst_line_num|, the line counter.
3491
3492 @<Procedures and functions for input scanning@>=
3493 function eat_bst_white_space : boolean;
3494 label exit;
3495 begin
3496 loop
3497 begin
3498 if (scan_white_space) then {hit a nonwhite character on this line}
3499 if (scan_char <> comment) then {it's not a comment character; return}
3500 begin
3501 eat_bst_white_space := true;
3502 return;
3503 end;
3504 if (not input_ln(bst_file)) then {end-of-file; return |false|}
3505 begin
3506 eat_bst_white_space := false;
3507 return;
3508 end;
3509 incr(bst_line_num);
3510 buf_ptr2 := 0;
3511 end;
3512 exit:
3513 end;
3514
3515
3516 @
3517 It's often illegal to end a \.{.bst} command in certain places, and
3518 this is where we come to check.
3519
3520 @d eat_bst_white_and_eof_check(#) ==
3521 begin
3522 if (not eat_bst_white_space) then
3523 begin
3524 eat_bst_print;
3525 bst_err (#);
3526 end;
3527 end
3528
3529 @<Procedures and functions for all file I/O, error messages, and such@>=
3530 procedure eat_bst_print;
3531 begin
3532 print ('Illegal end of style file in command: ');
3533 end;
3534
3535
3536 @
3537 We must attend to a few details before getting to work on this
3538 \.{.bst} command.
3539
3540 @<Scan for and process a \.{.bst} command@>=
3541 procedure get_bst_command_and_process;
3542 label exit;
3543 begin
3544 if (not scan_alpha) then
3545 bst_err ('"',xchr[scan_char],'" can''t start a style-file command');
3546 lower_case (buffer, buf_ptr1, token_len); {ignore case differences}
3547 command_num := ilk_info[
3548 str_lookup(buffer,buf_ptr1,token_len,bst_command_ilk,dont_insert)];
3549 if (not hash_found) then
3550 begin
3551 print_token;
3552 bst_err (' is an illegal style-file command');
3553 end;
3554 @<Process the appropriate \.{.bst} command@>;
3555 exit:
3556 end;
3557
3558
3559 @
3560 @^style-file commands@>
3561 @:this can't happen}{\quad Unknown style-file command@>
3562 Here we determine which \.{.bst} command we're about to process, and
3563 then go to it.
3564
3565 @<Process the appropriate \.{.bst} command@>=
3566 case (command_num) of
3567 n_bst_entry : bst_entry_command;
3568 n_bst_execute : bst_execute_command;
3569 n_bst_function : bst_function_command;
3570 n_bst_integers : bst_integers_command;
3571 n_bst_iterate : bst_iterate_command;
3572 n_bst_macro : bst_macro_command;
3573 n_bst_read : bst_read_command;
3574 n_bst_reverse : bst_reverse_command;
3575 n_bst_sort : bst_sort_command;
3576 n_bst_strings : bst_strings_command;
3577 othercases confusion ('Unknown style-file command')
3578 endcases
3579
3580
3581 @
3582 We need data structures for the function definitions, the entry
3583 variables, the global variables, and the actual entries corresponding
3584 to the cite-key list. First we define the classes of `function's
3585 used. Functions in all classes are of |bst_fn_ilk| except for
3586 |int_literal|s, which are of |integer_ilk|; and |str_literal|s, which
3587 are of |text_ilk|.
3588
3589 @d built_in = 0 {the `primitive' functions}
3590 @d wiz_defined = 1 {defined in the \.{.bst} file}
3591 @d int_literal = 2 {integer `constants'}
3592 @d str_literal = 3 {string `constants'}
3593 @d field = 4 {things like `author' and `title'}
3594 @d int_entry_var = 5 {integer entry variable}
3595 @d str_entry_var = 6 {string entry variable}
3596 @d int_global_var = 7 {integer global variable}
3597 @d str_global_var = 8 {string global variable}
3598 @d last_fn_class = 8 {the same number as on the line above}
3599
3600
3601 @
3602 @:this can't happen}{\quad Unknown function class@>
3603 Here's another bug report.
3604
3605 @<Procedures and functions for all file I/O, error messages, and such@>=
3606 procedure unknwn_function_class_confusion;
3607 begin
3608 confusion ('Unknown function class');
3609 end;
3610
3611
3612 @
3613 @:this can't happen}{\quad Unknown function class@>
3614 Occasionally we'll want to |print| the name of one of these function
3615 classes.
3616
3617 @<Procedures and functions for all file I/O, error messages, and such@>=
3618 procedure print_fn_class (@!fn_loc : hash_loc);
3619 begin
3620 case (fn_type[fn_loc]) of
3621 built_in : print ('built-in');
3622 wiz_defined : print ('wizard-defined');
3623 int_literal : print ('integer-literal');
3624 str_literal : print ('string-literal');
3625 field : print ('field');
3626 int_entry_var : print ('integer-entry-variable');
3627 str_entry_var : print ('string-entry-variable');
3628 int_global_var : print ('integer-global-variable');
3629 str_global_var : print ('string-global-variable');
3630 othercases unknwn_function_class_confusion
3631 endcases;
3632 end;
3633
3634
3635 @
3636 @:this can't happen}{\quad Unknown function class@>
3637 This version is for printing when in |trace| mode.
3638
3639 @<Procedures and functions for all file I/O, error messages, and such@>=
3640 trace
3641 procedure trace_pr_fn_class (@!fn_loc : hash_loc);
3642 begin
3643 case (fn_type[fn_loc]) of
3644 built_in : trace_pr ('built-in');
3645 wiz_defined : trace_pr ('wizard-defined');
3646 int_literal : trace_pr ('integer-literal');
3647 str_literal : trace_pr ('string-literal');
3648 field : trace_pr ('field');
3649 int_entry_var : trace_pr ('integer-entry-variable');
3650 str_entry_var : trace_pr ('string-entry-variable');
3651 int_global_var : trace_pr ('integer-global-variable');
3652 str_global_var : trace_pr ('string-global-variable');
3653 othercases unknwn_function_class_confusion
3654 endcases;
3655 end;
3656 ecart
3657
3658
3659 @
3660 Besides the function classes, we have types based on \BibTeX's
3661 capacity limitations and one based on what can go into the array
3662 |wiz_functions| explained below.
3663
3664 @d quote_next_fn = hash_base - 1 {special marker used in defining functions}
3665 @d end_of_def = hash_max + 1 {another such special marker}
3666
3667 @<Types in the outer block@>=
3668 @!fn_class = 0..last_fn_class; {the \.{.bst} function classes}
3669 @!wiz_fn_loc = 0..wiz_fn_space; {|wiz_defined|-function storage locations}
3670 @!int_ent_loc = 0..max_ent_ints; {|int_entry_var| storage locations}
3671 @!str_ent_loc = 0..max_ent_strs; {|str_entry_var| storage locations}
3672 @!str_glob_loc = 0..max_glb_str_minus_1; {|str_global_var| storage locations}
3673 @!field_loc = 0..max_fields; {individual field storage locations}
3674 @!hash_ptr2 = quote_next_fn..end_of_def; {a special marker or a |hash_loc|}
3675
3676
3677 @
3678 @^save space@>
3679 @^space savings@>
3680 @^system dependencies@>
3681 We store information about the \.{.bst} functions in arrays the same
3682 size as the hash-table arrays and in locations corresponding to their
3683 hash-table locations. The two arrays |fn_info| (an alias of
3684 |ilk_info| described earlier) and |fn_type| accomplish this: |fn_type|
3685 specifies one of the above classes, and |fn_info| gives information
3686 dependent on the class.
3687
3688 Six other arrays give the contents of functions: The array
3689 |wiz_functions| holds definitions for |wiz_defined| functions---each
3690 such function consists of a sequence of pointers to hash-table
3691 locations of other functions (with the two special-marker exceptions
3692 above); the array |entry_ints| contains the current values of
3693 |int_entry_var|s; the array |entry_strs| contains the current values
3694 of |str_entry_var|s; an element of the array |global_strs| contains
3695 the current value of a |str_global_var| if the corresponding
3696 |glb_str_ptr| entry is empty, otherwise the nonempty entry is a
3697 pointer to the string; and the array |field_info|, for each field of
3698 each entry, contains either a pointer to the string or the special
3699 value |missing|.
3700
3701 The array |global_strs| isn't packed (that is, it isn't |array| \dots\
3702 |of packed array| \dots$\,$) to increase speed on some systems;
3703 however, on systems that are byte-addressable and that have a good
3704 compiler, packing |global_strs| would save lots of space without much
3705 loss of speed.
3706
3707 @d fn_info == ilk_info {an alias used with functions}
3708 @#
3709 @d missing = empty {a special pointer for missing fields}
3710
3711 @<Globals in the outer block@>=
3712 @!fn_loc : hash_loc; {the hash-table location of a function}
3713 @!wiz_loc : hash_loc; {the hash-table location of a wizard function}
3714 @!literal_loc : hash_loc; {the hash-table location of a literal function}
3715 @!macro_name_loc : hash_loc; {the hash-table location of a macro name}
3716 @!macro_def_loc : hash_loc; {the hash-table location of a macro definition}
3717 @!fn_type : packed array[hash_loc] of fn_class;
3718 @!wiz_def_ptr : wiz_fn_loc; {storage location for the next wizard function}
3719 @!wiz_fn_ptr : wiz_fn_loc; {general |wiz_functions| location}
3720 @!wiz_functions : packed array[wiz_fn_loc] of hash_ptr2;
3721 @!int_ent_ptr : int_ent_loc; {general |int_entry_var| location}
3722 @!entry_ints : array[int_ent_loc] of integer;
3723 @!num_ent_ints : int_ent_loc; {the number of distinct |int_entry_var| names}
3724 @!str_ent_ptr : str_ent_loc; {general |str_entry_var| location}
3725 @!entry_strs : array[str_ent_loc] of
3726 packed array[0..ent_str_size] of ASCII_code;
3727 @!num_ent_strs : str_ent_loc; {the number of distinct |str_entry_var| names}
3728 @!str_glb_ptr : 0..max_glob_strs; {general |str_global_var| location}
3729 @!glb_str_ptr : array[str_glob_loc] of str_number;
3730 @!global_strs : array[str_glob_loc] of array[0..glob_str_size] of ASCII_code;
3731 @!glb_str_end : array[str_glob_loc] of 0..glob_str_size; {end markers}
3732 @!num_glb_strs : 0..max_glob_strs; {number of distinct |str_global_var| names}
3733 @!field_ptr : field_loc; {general |field_info| location}
3734 @!field_parent_ptr,@!field_end_ptr : field_loc; {two more for doing cross-refs}
3735 @!cite_parent_ptr,@!cite_xptr : cite_number; {two others for doing cross-refs}
3736 @!field_info : packed array[field_loc] of str_number;
3737 @!num_fields : field_loc; {the number of distinct field names}
3738 @!num_pre_defined_fields : field_loc; {so far, just one: \.{crossref}}
3739 @!crossref_num : field_loc; {the number given to \.{crossref}}
3740 @!no_fields : boolean; {used for |tr_print|ing entry information}
3741
3742
3743 @
3744 Now we initialize storage for the |wiz_defined| functions and we
3745 initialize variables so that the first |str_entry_var|,
3746 |int_entry_var|, |str_global_var|, and |field| name will be assigned
3747 the number~0. Note: The variables |num_ent_strs| and |num_fields|
3748 will also be set when pre-defining strings.
3749
3750 @<Set initial values of key variables@>=
3751 wiz_def_ptr := 0;
3752 num_ent_ints := 0;
3753 num_ent_strs := 0;
3754 num_fields := 0;
3755 str_glb_ptr := 0;
3756 while (str_glb_ptr < max_glob_strs) do {make |str_global_var|s empty}
3757 begin
3758 glb_str_ptr[str_glb_ptr] := 0;
3759 glb_str_end[str_glb_ptr] := 0;
3760 incr(str_glb_ptr);
3761 end;
3762 num_glb_strs := 0;
3763
3764
3765
3766 @* Style-file commands.
3767 @^style-file commands@>
3768 There are ten \.{.bst} commands: Five (\.{entry}, \.{function},
3769 \.{integers}, \.{macro}, and \.{strings}) declare and define
3770 functions, one (\.{read}) reads in the \.{.bib}-file entries, and four
3771 (\.{execute}, \.{iterate}, \.{reverse}, and \.{sort})
3772 manipulate the entries and produce output.
3773
3774 The boolean variables |entry_seen| and |read_seen| indicate whether
3775 we've yet encountered an \.{entry} and a \.{read} command. There must
3776 be exactly one of each of these, and the \.{entry} command, as well as
3777 any \.{macro} command, must precede the \.{read} command.
3778 Furthermore, the \.{read} command must precede the four that
3779 manipulate the entries and produce output.
3780
3781 @<Globals in the outer block@>=
3782 @!entry_seen : boolean; {|true| if we've already seen an \.{entry} command}
3783 @!read_seen : boolean; {|true| if we've already seen a \.{read} command}
3784 @!read_performed : boolean; {|true| if we started reading the database file(s)}
3785 @!reading_completed : boolean; {|true| if we made it all the way through}
3786 @!read_completed : boolean; {|true| if the database info didn't bomb \BibTeX}
3787
3788
3789 @
3790 And we initialize them.
3791
3792 @<Set initial values of key variables@>=
3793 entry_seen := false;
3794 read_seen := false;
3795 read_performed := false;
3796 reading_completed := false;
3797 read_completed := false;
3798
3799
3800 @
3801 @:this can't happen}{\quad Identifier scanning error@>
3802 Here's another bug.
3803
3804 @<Procedures and functions for all file I/O, error messages, and such@>=
3805 procedure id_scanning_confusion;
3806 begin
3807 confusion ('Identifier scanning error');
3808 end;
3809
3810
3811 @
3812 @:this can't happen}{\quad Identifier scanning error@>
3813 This macro is used to scan all \.{.bst} identifiers. The argument
3814 supplies the \.{.bst} command name. The associated procedure simply
3815 prints an error message.
3816
3817 @d bst_identifier_scan(#) ==
3818 begin
3819 scan_identifier (right_brace,comment,comment);
3820 if ((scan_result = white_adjacent) or
3821 (scan_result = specified_char_adjacent)) then
3822 do_nothing
3823 else
3824 begin
3825 bst_id_print;
3826 bst_err (#);
3827 end;
3828 end
3829
3830 @<Procedures and functions for all file I/O, error messages, and such@>=
3831 procedure bst_id_print;
3832 begin
3833 if (scan_result = id_null) then
3834 print ('"',xchr[scan_char],'" begins identifier, command: ')
3835 else if (scan_result = other_char_adjacent) then
3836 print ('"',xchr[scan_char],'" immediately follows identifier, command: ')
3837 else
3838 id_scanning_confusion;
3839 end;
3840
3841
3842 @
3843 This macro just makes sure we're at a |left_brace|.
3844
3845 @d bst_get_and_check_left_brace(#) ==
3846 begin
3847 if (scan_char <> left_brace) then
3848 begin
3849 bst_left_brace_print;
3850 bst_err (#);
3851 end;
3852 incr(buf_ptr2); {skip over the |left_brace|}
3853 end
3854
3855 @<Procedures and functions for all file I/O, error messages, and such@>=
3856 procedure bst_left_brace_print;
3857 begin
3858 print ('"',xchr[left_brace],'" is missing in command: ');
3859 end;
3860
3861
3862 @
3863 And this one, a |right_brace|.
3864
3865 @d bst_get_and_check_right_brace(#) ==
3866 begin
3867 if (scan_char <> right_brace) then
3868 begin
3869 bst_right_brace_print;
3870 bst_err (#);
3871 end;
3872 incr(buf_ptr2); {skip over the |right_brace|}
3873 end
3874
3875 @<Procedures and functions for all file I/O, error messages, and such@>=
3876 procedure bst_right_brace_print;
3877 begin
3878 print ('"',xchr[right_brace],'" is missing in command: ');
3879 end;
3880
3881
3882 @
3883 This macro complains if we've already encountered a function to be
3884 inserted into the hash table.
3885
3886 @d check_for_already_seen_function(#) ==
3887 begin
3888 if (hash_found) then {already encountered this as a \.{.bst} function}
3889 begin
3890 already_seen_function_print (#);
3891 return;
3892 end;
3893 end
3894
3895 @<Procedures and functions for all file I/O, error messages, and such@>=
3896 procedure already_seen_function_print (@!seen_fn_loc : hash_loc);
3897 label exit; {so the call to |bst_err| works}
3898 begin
3899 print_pool_str (hash_text[seen_fn_loc]);
3900 print (' is already a type "');
3901 print_fn_class (seen_fn_loc);
3902 print_ln ('" function name');
3903 bst_err_print_and_look_for_blank_line_return;
3904 exit:
3905 end;
3906
3907
3908 @
3909 @:style-file commands}{\quad \.{entry}@>
3910 An \.{entry} command has three arguments, each a (possibly empty) list
3911 of function names between braces (the names are separated by one or
3912 more |white_space| characters). All function names in this and other
3913 commands must be legal \.{.bst} identifiers. Upper/lower cases are
3914 considered to be the same for function names in these lists---all
3915 upper-case letters are converted to lower case. These arguments give
3916 lists of |field|s, |int_entry_var|s, and |str_entry_var|s.
3917
3918 @<Procedures and functions for the reading and processing of input files@>=
3919 procedure bst_entry_command;
3920 label exit;
3921 begin
3922 if (entry_seen) then
3923 bst_err ('Illegal, another entry command');
3924 entry_seen := true; {now we've seen an \.{entry} command}
3925 eat_bst_white_and_eof_check ('entry');
3926 @<Scan the list of |field|s@>;
3927 eat_bst_white_and_eof_check ('entry');
3928 if (num_fields = num_pre_defined_fields) then
3929 bst_warn ('Warning--I didn''t find any fields');
3930 @<Scan the list of |int_entry_var|s@>;
3931 eat_bst_white_and_eof_check ('entry');
3932 @<Scan the list of |str_entry_var|s@>;
3933 exit:
3934 end;
3935
3936
3937 @
3938 This module reads a |left_brace|, the list of |field|s, and a
3939 |right_brace|. The |field|s are those like `author' and `title.'
3940
3941 @<Scan the list of |field|s@>=
3942 begin
3943 bst_get_and_check_left_brace ('entry');
3944 eat_bst_white_and_eof_check ('entry');
3945 while (scan_char <> right_brace) do
3946 begin
3947 bst_identifier_scan ('entry');
3948 @<Insert a |field| into the hash table@>;
3949 eat_bst_white_and_eof_check ('entry');
3950 end;
3951 incr(buf_ptr2); {skip over the |right_brace|}
3952 end
3953
3954
3955 @
3956 @^secret agent man@>
3957 Here we insert the just found field name into the hash table, record
3958 it as a |field|, and assign it a number to be used in indexing into
3959 the |field_info| array.
3960
3961 @<Insert a |field| into the hash table@>=
3962 begin
3963 trace
3964 trace_pr_token;
3965 trace_pr_ln (' is a field');
3966 ecart@/
3967 lower_case (buffer, buf_ptr1, token_len); {ignore case differences}
3968 fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,do_insert);
3969 check_for_already_seen_function (fn_loc);
3970 fn_type[fn_loc] := field;@/
3971 fn_info[fn_loc] := num_fields; {give this field a number (take away its name)}
3972 incr(num_fields);
3973 end
3974
3975
3976 @
3977 This module reads a |left_brace|, the list of |int_entry_var|s,
3978 and a |right_brace|.
3979
3980 @<Scan the list of |int_entry_var|s@>=
3981 begin
3982 bst_get_and_check_left_brace ('entry');
3983 eat_bst_white_and_eof_check ('entry');
3984 while (scan_char <> right_brace) do
3985 begin
3986 bst_identifier_scan ('entry');
3987 @<Insert an |int_entry_var| into the hash table@>;
3988 eat_bst_white_and_eof_check ('entry');
3989 end;
3990 incr(buf_ptr2); {skip over the |right_brace|}
3991 end
3992
3993
3994 @
3995 Here we insert the just found |int_entry_var| name into the hash table
3996 and record it as an |int_entry_var|. An |int_entry_var| is one that
3997 the style designer wants a separate copy of for each entry.
3998
3999 @<Insert an |int_entry_var| into the hash table@>=
4000 begin
4001 trace
4002 trace_pr_token;
4003 trace_pr_ln (' is an integer entry-variable');
4004 ecart@/
4005 lower_case (buffer, buf_ptr1, token_len); {ignore case differences}
4006 fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,do_insert);
4007 check_for_already_seen_function (fn_loc);
4008 fn_type[fn_loc] := int_entry_var;@/
4009 fn_info[fn_loc] := num_ent_ints; {give this |int_entry_var| a number}
4010 incr(num_ent_ints);
4011 end
4012
4013
4014 @
4015 This module reads a |left_brace|, the list of |str_entry_var|s, and a
4016 |right_brace|. A |str_entry_var| is one that the style designer wants
4017 a separate copy of for each entry.
4018
4019 @<Scan the list of |str_entry_var|s@>=
4020 begin
4021 bst_get_and_check_left_brace ('entry');
4022 eat_bst_white_and_eof_check ('entry');
4023 while (scan_char <> right_brace) do
4024 begin
4025 bst_identifier_scan ('entry');
4026 @<Insert a |str_entry_var| into the hash table@>;
4027 eat_bst_white_and_eof_check ('entry');
4028 end;
4029 incr(buf_ptr2); {skip over the |right_brace|}
4030 end
4031
4032
4033 @
4034 Here we insert the just found |str_entry_var| name into the hash
4035 table, record it as a |str_entry_var|, and set its pointer into
4036 |entry_strs|.
4037
4038 @<Insert a |str_entry_var| into the hash table@>=
4039 begin
4040 trace
4041 trace_pr_token;
4042 trace_pr_ln (' is a string entry-variable');
4043 ecart@/
4044 lower_case (buffer, buf_ptr1, token_len); {ignore case differences}
4045 fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,do_insert);
4046 check_for_already_seen_function (fn_loc);
4047 fn_type[fn_loc] := str_entry_var;@/
4048 fn_info[fn_loc] := num_ent_strs; {give this |str_entry_var| a number}
4049 incr(num_ent_strs);
4050 end
4051
4052
4053 @
4054 A legal argument for an \.{execute}, \.{iterate}, or \.{reverse}
4055 command must exist and be |built_in| or |wiz_defined|.
4056 Here's where we check, returning |true| if the argument is illegal.
4057
4058 @<Procedures and functions for the reading and processing of input files@>=
4059 function bad_argument_token : boolean;
4060 label exit;
4061 begin
4062 bad_argument_token := true; {now it's easy to exit if necessary}
4063 lower_case (buffer, buf_ptr1, token_len); {ignore case differences}
4064 fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,dont_insert);
4065 if (not hash_found) then {unknown \.{.bst} function}
4066 begin
4067 print_token;
4068 bst_err (' is an unknown function');
4069 end
4070 else if ((fn_type[fn_loc] <> built_in) and
4071 (fn_type[fn_loc] <> wiz_defined)) then
4072 begin
4073 print_token;
4074 print (' has bad function type ');
4075 print_fn_class (fn_loc);
4076 bst_err_print_and_look_for_blank_line_return;
4077 end;
4078 bad_argument_token := false;
4079 exit:
4080 end;
4081
4082
4083 @
4084 @:style-file commands}{\quad \.{execute}@>
4085 An \.{execute} command has one argument, a single |built_in| or
4086 |wiz_defined| function name between braces. Upper/lower cases are
4087 considered to be the same---all upper-case letters are converted to
4088 lower case. Also, we must make sure we've already seen a \.{read}
4089 command.
4090
4091 This module reads a |left_brace|, a single function to be executed,
4092 and a |right_brace|.
4093
4094 @<Procedures and functions for the reading and processing of input files@>=
4095 procedure bst_execute_command;
4096 label exit;
4097 begin
4098 if (not read_seen) then
4099 bst_err ('Illegal, execute command before read command');
4100 eat_bst_white_and_eof_check ('execute');
4101 bst_get_and_check_left_brace ('execute');
4102 eat_bst_white_and_eof_check ('execute');
4103 bst_identifier_scan ('execute');
4104 @<Check the \(e)\.{execute}-command argument token@>;
4105 eat_bst_white_and_eof_check ('execute');
4106 bst_get_and_check_right_brace ('execute');
4107 @<Perform an \.{execute} command@>;
4108 exit:
4109 end;
4110
4111
4112 @
4113 Before executing the function, we must make sure it's a legal one. It
4114 must exist and be |built_in| or |wiz_defined|.
4115
4116 @<Check the \(e)\.{execute}-command argument token@>=
4117 begin
4118 trace
4119 trace_pr_token;
4120 trace_pr_ln (' is a to be executed function');
4121 ecart@/
4122 if (bad_argument_token) then
4123 return;
4124 end
4125
4126
4127 @
4128 @:style-file commands}{\quad \.{function}@>
4129 A \.{function} command has two arguments; the first is a
4130 |wiz_defined| function name between braces. Upper/lower cases are
4131 considered to be the same---all upper-case letters are converted to
4132 lower case. The second argument defines this function. It consists
4133 of a sequence of functions, between braces, separated by |white_space|
4134 characters. Upper/lower cases are considered to be the same for
4135 function names but not for |str_literal|s.
4136
4137 @<Procedures and functions for the reading and processing of input files@>=
4138 procedure bst_function_command;
4139 label exit;
4140 begin
4141 eat_bst_white_and_eof_check ('function');
4142 @<Scan the |wiz_defined| function name@>;
4143 eat_bst_white_and_eof_check ('function');
4144 bst_get_and_check_left_brace ('function');
4145 scan_fn_def(wiz_loc); {this scans the function definition}
4146 exit:
4147 end;
4148
4149
4150 @
4151 This module reads a |left_brace|, a |wiz_defined| function name, and
4152 a |right_brace|.
4153
4154 @<Scan the |wiz_defined| function name@>=
4155 begin
4156 bst_get_and_check_left_brace ('function');
4157 eat_bst_white_and_eof_check ('function');
4158 bst_identifier_scan ('function');
4159 @<Check the \(w)|wiz_defined| function name@>;
4160 eat_bst_white_and_eof_check ('function');
4161 bst_get_and_check_right_brace ('function');
4162 end
4163
4164
4165 @
4166 The function name must exist and be a new one; we mark it as
4167 |wiz_defined|. Also, see if it's the default entry-type function.
4168
4169 @<Check the \(w)|wiz_defined| function name@>=
4170 begin
4171 trace
4172 trace_pr_token;
4173 trace_pr_ln (' is a wizard-defined function');
4174 ecart@/
4175 lower_case (buffer, buf_ptr1, token_len); {ignore case differences}
4176 wiz_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,do_insert);
4177 check_for_already_seen_function (wiz_loc);
4178 fn_type[wiz_loc] := wiz_defined;
4179 if (hash_text[wiz_loc] = s_default) then {we've found the default entry-type}
4180 b_default := wiz_loc; {see the |built_in| functions for |b_default|}
4181 end
4182
4183
4184 @
4185 We're about to start scanning tokens in a function definition. When a
4186 function token is illegal, we skip until it ends; a |white_space|
4187 character, an end-of-line, a |right_brace|, or a |comment| marks the
4188 end of the current token.
4189
4190 @d next_token=25 {a bad function token; go read the next one}
4191 @#
4192 @d skip_token(#) == begin {not-so-serious error during \.{.bst} parsing}
4193 print (#);
4194 skip_token_print; {also, skip to the current token's end}
4195 goto next_token;
4196 end
4197
4198 @<Procedures and functions for input scanning@>=
4199 procedure skip_token_print;
4200 begin
4201 print ('-');
4202 bst_ln_num_print;
4203 mark_error;
4204 if (scan2_white(right_brace,comment)) then {ok if token ends line}
4205 do_nothing;
4206 end;
4207
4208
4209 @
4210 @^commented-out code@>
4211 @^for a good time, try comment-out code@>
4212 This macro is similar to the last one but is specifically for
4213 recursion in a |wiz_defined| function, which is illegal; it helps save
4214 space.
4215
4216 @d skip_recursive_token == begin
4217 print_recursion_illegal;
4218 goto next_token;
4219 end
4220
4221 @<Procedures and functions for input scanning@>=
4222 procedure print_recursion_illegal;
4223 begin
4224 trace
4225 trace_pr_newline;
4226 ecart@/
4227 print_ln ('Curse you, wizard, before you recurse me:');
4228 print ('function ');
4229 print_token;
4230 print_ln (' is illegal in its own definition');
4231 @{
4232 print_recursion_illegal;
4233 @}@/
4234 skip_token_print; {also, skip to the current token's end}
4235 end;
4236
4237
4238 @
4239 Here's another macro for saving some space when there's a problem with
4240 a token.
4241
4242 @d skip_token_unknown_function == begin
4243 skp_token_unknown_function_print;
4244 goto next_token;
4245 end
4246
4247 @<Procedures and functions for input scanning@>=
4248 procedure skp_token_unknown_function_print;
4249 begin
4250 print_token;
4251 print (' is an unknown function');
4252 skip_token_print; {also, skip to the current token's end}
4253 end;
4254
4255
4256 @
4257 And another.
4258
4259 @d skip_token_illegal_stuff_after_literal ==
4260 begin
4261 skip_illegal_stuff_after_token_print;
4262 goto next_token;
4263 end
4264
4265 @<Procedures and functions for input scanning@>=
4266 procedure skip_illegal_stuff_after_token_print;
4267 begin
4268 print ('"',xchr[scan_char],'" can''t follow a literal');
4269 skip_token_print; {also, skip to the current token's end}
4270 end;
4271
4272
4273 @
4274 This recursive function reads and stores the list of functions
4275 (separated by |white_space| characters or ends-of-line) that define
4276 this new function, and reads a |right_brace|.
4277
4278 @<Procedures and functions for input scanning@>=
4279 procedure scan_fn_def (@!fn_hash_loc : hash_loc);
4280 label next_token,@!exit;
4281 type @!fn_def_loc = 0..single_fn_space; {for a single |wiz_defined|-function}
4282 var singl_function : packed array[fn_def_loc] of hash_ptr2;
4283 @!single_ptr : fn_def_loc; {next storage location for this definition}
4284 @!copy_ptr : fn_def_loc; {dummy variable}
4285 @!end_of_num : buf_pointer; {the end of an implicit function's name}
4286 @!impl_fn_loc : hash_loc; {an implicit function's hash-table location}
4287 begin
4288 eat_bst_white_and_eof_check ('function');
4289 single_ptr := 0;
4290 while (scan_char <> right_brace) do
4291 begin
4292 @<Get the next function of the definition@>;
4293 next_token:
4294 eat_bst_white_and_eof_check ('function');
4295 end;
4296 @<Complete this function's definition@>;
4297 incr(buf_ptr2); {skip over the |right_brace|}
4298 exit:
4299 end;
4300
4301
4302 @
4303 @:BibTeX capacity exceeded}{\quad single function space@>
4304 This macro inserts a hash-table location (or one of the two
4305 special markers |quote_next_fn| and |end_of_def|) into the
4306 |singl_function| array, which will later be copied into the
4307 |wiz_functions| array.
4308
4309 @d insert_fn_loc(#) == begin
4310 singl_function[single_ptr] := #;
4311 if (single_ptr = single_fn_space) then
4312 singl_fn_overflow;
4313 incr(single_ptr);
4314 end
4315
4316 @<Procedures and functions for all file I/O, error messages, and such@>=
4317 procedure singl_fn_overflow;
4318 begin
4319 overflow('single function space ',single_fn_space);
4320 end;
4321
4322
4323 @
4324 There are five possibilities for the first character of the token
4325 representing the next function of the definition: If it's a
4326 |number_sign|, the token is an |int_literal|; if it's a
4327 |double_quote|, the token is a |str_literal|; if it's a
4328 |single_quote|, the token is a quoted function; if it's a
4329 |left_brace|, the token isn't really a token, but rather the start of
4330 another function definition (which will result in a recursive call to
4331 |scan_fn_def|); if it's anything else, the token is the name of an
4332 already-defined function. Note: To prevent the wizard from using
4333 recursion, we have to check that neither a quoted function nor an
4334 already-defined-function is actually the currently-being-defined
4335 function (which is stored at |wiz_loc|).
4336
4337 @<Get the next function of the definition@>=
4338 case (scan_char) of
4339 number_sign : @<Scan an |int_literal|@>;
4340 double_quote : @<Scan a |str_literal|@>;
4341 single_quote : @<Scan a quoted function@>;
4342 left_brace : @<Start a new function definition@>;
4343 othercases @<Scan an already-defined function@>
4344 endcases
4345
4346
4347 @
4348 An |int_literal| is preceded by a |number_sign|, consists of an
4349 integer (i.e., an optional |minus_sign| followed by one or more
4350 |numeric| characters), and is followed either by a |white_space|
4351 character, an end-of-line, or a |right_brace|. The array |fn_info|
4352 contains the value of the integer for |int_literal|s.
4353
4354 @<Scan an |int_literal|@>=
4355 begin
4356 incr(buf_ptr2); {skip over the |number_sign|}
4357 if (not scan_integer) then
4358 skip_token ('Illegal integer in integer literal');
4359 trace
4360 trace_pr ('#');
4361 trace_pr_token;
4362 trace_pr_ln (' is an integer literal with value ',token_value:0);
4363 ecart@/
4364 literal_loc := str_lookup(buffer,buf_ptr1,token_len,integer_ilk,do_insert);
4365 if (not hash_found) then
4366 begin
4367 fn_type[literal_loc] := int_literal; {set the |fn_class|}
4368 fn_info[literal_loc] := token_value; {the value of this integer}
4369 end;
4370 if ((lex_class[scan_char]<>white_space) and (buf_ptr2<last) and
4371 (scan_char<>right_brace) and@| (scan_char<>comment)) then
4372 skip_token_illegal_stuff_after_literal;
4373 insert_fn_loc (literal_loc); {add this function to |wiz_functions|}
4374 end
4375
4376
4377 @
4378 A |str_literal| is preceded by a |double_quote| and consists of all
4379 characters on this line up to the next |double_quote|. Also, there
4380 must be either a |white_space| character, an end-of-line, a
4381 |right_brace|, or a |comment| following (since functions in the
4382 definition must be separated by |white_space|). The array |fn_info|
4383 contains nothing for |str_literal|s.
4384
4385 @<Scan a |str_literal|@>=
4386 begin
4387 incr(buf_ptr2); {skip over the |double_quote|}
4388 if (not scan1(double_quote)) then
4389 skip_token ('No `',xchr[double_quote],''' to end string literal');
4390 trace
4391 trace_pr ('"');
4392 trace_pr_token;
4393 trace_pr ('"');
4394 trace_pr_ln (' is a string literal');
4395 ecart@/
4396 literal_loc := str_lookup(buffer,buf_ptr1,token_len,text_ilk,do_insert);@/
4397 fn_type[literal_loc] := str_literal; {set the |fn_class|}
4398 incr(buf_ptr2); {skip over the |double_quote|}
4399 if ((lex_class[scan_char]<>white_space) and (buf_ptr2<last) and
4400 (scan_char<>right_brace) and@| (scan_char<>comment)) then
4401 skip_token_illegal_stuff_after_literal;
4402 insert_fn_loc (literal_loc); {add this function to |wiz_functions|}
4403 end
4404
4405
4406 @
4407 A quoted function is preceded by a |single_quote| and consists of all
4408 characters up to the next |white_space| character, end-of-line,
4409 |right_brace|, or |comment|.
4410
4411 @<Scan a quoted function@>=
4412 begin
4413 incr(buf_ptr2); {skip over the |single_quote|}
4414 if (scan2_white(right_brace,comment)) then {ok if token ends line}
4415 do_nothing;
4416 trace
4417 trace_pr ('''');
4418 trace_pr_token;
4419 trace_pr (' is a quoted function ');
4420 ecart@/
4421 lower_case (buffer, buf_ptr1, token_len); {ignore case differences}
4422 fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,dont_insert);
4423 if (not hash_found) then {unknown \.{.bst} function}
4424 skip_token_unknown_function
4425 else
4426 @<Check and insert the quoted function@>;
4427 end
4428
4429
4430 @
4431 Here we check that this quoted function is a legal one---the function
4432 name must already exist, but it mustn't be the currently-being-defined
4433 function (which is stored at |wiz_loc|).
4434
4435 @<Check and insert the quoted function@>=
4436 begin
4437 if (fn_loc = wiz_loc) then
4438 skip_recursive_token
4439 else
4440 begin
4441 trace
4442 trace_pr ('of type ');
4443 trace_pr_fn_class (fn_loc);
4444 trace_pr_newline;
4445 ecart@/
4446 insert_fn_loc (quote_next_fn); {add special marker together with}
4447 insert_fn_loc (fn_loc); {this function to |wiz_functions|}
4448 end
4449 end
4450
4451
4452 @
4453 @^kludge@>
4454 @:this can't happen}{\quad Already encountered implicit function@>
4455 This module marks the implicit function as being quoted, generates a
4456 name, and stores it in the hash table. This name is strictly internal
4457 to this program, starts with a |single_quote| (since that will make
4458 this function name unique), and ends with the variable |impl_fn_num|
4459 converted to ASCII. The alias kludge helps make the stack space not
4460 overflow on some machines.
4461
4462 @d ex_buf2 == ex_buf {an alias, used only in this module}
4463
4464 @<Start a new function definition@>=
4465 begin
4466 ex_buf2[0] := single_quote;
4467 int_to_ASCII (impl_fn_num,ex_buf2,1,end_of_num);
4468 impl_fn_loc := str_lookup(ex_buf2,0,end_of_num,bst_fn_ilk,do_insert);
4469 if (hash_found) then
4470 confusion ('Already encountered implicit function');
4471 trace
4472 trace_pr_pool_str (hash_text[impl_fn_loc]);
4473 trace_pr_ln (' is an implicit function');
4474 ecart@/
4475 incr(impl_fn_num);
4476 fn_type[impl_fn_loc] := wiz_defined;@/
4477 insert_fn_loc (quote_next_fn); {all implicit functions are quoted}
4478 insert_fn_loc (impl_fn_loc); {add it to |wiz_functions|}
4479 incr(buf_ptr2); {skip over the |left_brace|}
4480 scan_fn_def (impl_fn_loc); {this is the recursive call}
4481 end
4482
4483
4484 @
4485 The variable |impl_fn_num| counts the number of implicit functions
4486 seen in the \.{.bst} file.
4487
4488 @<Globals in the outer block@>=
4489 @!impl_fn_num : integer; {the number of implicit functions seen so far}
4490
4491
4492 @
4493 Now we initialize it.
4494
4495 @<Set initial values of key variables@>=
4496 impl_fn_num := 0;
4497
4498
4499 @
4500 @:BibTeX capacity exceeded}{\quad buffer size@>
4501 This module appends a character to |int_buf| after checking to make
4502 sure it will fit; for use in |int_to_ASCII|.
4503
4504 @d append_int_char(#) == begin
4505 if (int_ptr = buf_size) then
4506 buffer_overflow;
4507 int_buf[int_ptr]:=#;
4508 incr(int_ptr);
4509 end
4510
4511
4512 @
4513 This procedure takes the integer |int|, copies the appropriate
4514 |ASCII_code| string into |int_buf| starting at |int_begin|, and sets
4515 the |var| parameter |int_end| to the first unused |int_buf| location.
4516 The ASCII string will consist of decimal digits, the first of which
4517 will be not be a~0 if the integer is nonzero, with a prepended minus
4518 sign if the integer is negative.
4519
4520 @<Procedures and functions for handling numbers, characters, and strings@>=
4521 procedure int_to_ASCII (@!int:integer; var int_buf:buf_type;
4522 @!int_begin:buf_pointer; var int_end:buf_pointer);
4523 var int_ptr,@!int_xptr : buf_pointer; {pointers into |int_buf|}
4524 @!int_tmp_val : ASCII_code; {the temporary element in an exchange}
4525 begin
4526 int_ptr := int_begin;
4527 if (int < 0) then {add the |minus_sign| and use the absolute value}
4528 begin
4529 append_int_char (minus_sign);
4530 int := -int;
4531 end;
4532 int_xptr := int_ptr;
4533 repeat {copy digits into |int_buf|}
4534 append_int_char ("0" + (int mod 10));
4535 int := int div 10;
4536 until (int = 0);
4537 int_end := int_ptr; {set the string length}
4538 decr(int_ptr);
4539 while (int_xptr < int_ptr) do {and reorder (flip) the digits}
4540 begin
4541 int_tmp_val := int_buf[int_xptr];
4542 int_buf[int_xptr] := int_buf[int_ptr];
4543 int_buf[int_ptr] := int_tmp_val;
4544 decr(int_ptr);
4545 incr(int_xptr);
4546 end
4547 end;
4548
4549
4550 @
4551 An already-defined function consists of all characters up to the next
4552 |white_space| character, end-of-line, |right_brace|, or |comment|.
4553 This function name must already exist, but it mustn't be the
4554 currently-being-defined function (which is stored at |wiz_loc|).
4555
4556 @<Scan an already-defined function@>=
4557 begin
4558 if (scan2_white(right_brace,comment)) then {ok if token ends line}
4559 do_nothing;
4560 trace
4561 trace_pr_token;
4562 trace_pr (' is a function ');
4563 ecart@/
4564 lower_case (buffer, buf_ptr1, token_len); {ignore case differences}
4565 fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,dont_insert);
4566 if (not hash_found) then {unknown \.{.bst} function}
4567 skip_token_unknown_function
4568 else if (fn_loc = wiz_loc) then
4569 skip_recursive_token
4570 else
4571 begin
4572 trace
4573 trace_pr ('of type ');
4574 trace_pr_fn_class (fn_loc);
4575 trace_pr_newline;
4576 ecart@/
4577 insert_fn_loc (fn_loc); {add this function to |wiz_functions|}
4578 end;
4579 end
4580
4581
4582 @
4583 @:BibTeX capacity exceeded}{\quad wizard-defined function space@>
4584 Now we add the |end_of_def| special marker, make sure this function will
4585 fit into |wiz_functions|, and put it there.
4586
4587 @<Complete this function's definition@>=
4588 begin
4589 insert_fn_loc (end_of_def); {add special marker ending the definition}
4590 if (single_ptr + wiz_def_ptr > wiz_fn_space) then
4591 begin
4592 print (single_ptr + wiz_def_ptr : 0,': ');
4593 overflow('wizard-defined function space ',wiz_fn_space);
4594 end;
4595 fn_info[fn_hash_loc] := wiz_def_ptr; {pointer into |wiz_functions|}
4596 copy_ptr := 0;
4597 while (copy_ptr < single_ptr) do {make this function official}
4598 begin
4599 wiz_functions[wiz_def_ptr] := singl_function[copy_ptr];
4600 incr(copy_ptr);
4601 incr(wiz_def_ptr);
4602 end;
4603 end
4604
4605
4606 @
4607 @:style-file commands}{\quad \.{integers}@>
4608 An \.{integers} command has one argument, a list of function names
4609 between braces (the names are separated by one or more |white_space|
4610 characters). Upper/lower cases are considered to be the same for
4611 function names in these lists---all upper-case letters are converted to
4612 lower case. Each name in this list specifies an |int_global_var|.
4613 There may be several \.{integers} commands in the \.{.bst} file.
4614
4615 This module reads a |left_brace|, a list of |int_global_var|s, and a
4616 |right_brace|.
4617
4618 @<Procedures and functions for the reading and processing of input files@>=
4619 procedure bst_integers_command;
4620 label exit;
4621 begin
4622 eat_bst_white_and_eof_check ('integers');
4623 bst_get_and_check_left_brace ('integers');
4624 eat_bst_white_and_eof_check ('integers');
4625 while (scan_char <> right_brace) do
4626 begin
4627 bst_identifier_scan ('integers');
4628 @<Insert an |int_global_var| into the hash table@>;
4629 eat_bst_white_and_eof_check ('integers');
4630 end;
4631 incr(buf_ptr2); {skip over the |right_brace|}
4632 exit:
4633 end;
4634
4635
4636 @
4637 Here we insert the just found |int_global_var| name into the hash
4638 table and record it as an |int_global_var|. Also, we initialize it by
4639 setting |fn_info[fn_loc]| to 0.
4640
4641 @<Insert an |int_global_var| into the hash table@>=
4642 begin
4643 trace
4644 trace_pr_token;
4645 trace_pr_ln (' is an integer global-variable');
4646 ecart@/
4647 lower_case (buffer, buf_ptr1, token_len); {ignore case differences}
4648 fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,do_insert);
4649 check_for_already_seen_function (fn_loc);
4650 fn_type[fn_loc] := int_global_var;@/
4651 fn_info[fn_loc] := 0; {initialize}
4652 end
4653
4654
4655 @
4656 @:style-file commands}{\quad \.{iterate}@>
4657 An \.{iterate} command has one argument, a single |built_in| or
4658 |wiz_defined| function name between braces. Upper/lower cases are
4659 considered to be the same---all upper-case letters are converted to
4660 lower case. Also, we must make sure we've already seen a \.{read}
4661 command.
4662
4663 This module reads a |left_brace|, a single function to be iterated,
4664 and a |right_brace|.
4665
4666 @<Procedures and functions for the reading and processing of input files@>=
4667 procedure bst_iterate_command;
4668 label exit;
4669 begin
4670 if (not read_seen) then
4671 bst_err ('Illegal, iterate command before read command');
4672 eat_bst_white_and_eof_check ('iterate');
4673 bst_get_and_check_left_brace ('iterate');
4674 eat_bst_white_and_eof_check ('iterate');
4675 bst_identifier_scan ('iterate');
4676 @<Check the \(i)\.{iterate}-command argument token@>;
4677 eat_bst_white_and_eof_check ('iterate');
4678 bst_get_and_check_right_brace ('iterate');
4679 @<Perform an \.{iterate} command@>;
4680 exit:
4681 end;
4682
4683
4684 @
4685 Before iterating the function, we must make sure it's a legal one. It
4686 must exist and be |built_in| or |wiz_defined|.
4687
4688 @<Check the \(i)\.{iterate}-command argument token@>=
4689 begin
4690 trace
4691 trace_pr_token;
4692 trace_pr_ln (' is a to be iterated function');
4693 ecart@/
4694 if (bad_argument_token) then
4695 return;
4696 end
4697
4698
4699 @
4700 @:style-file commands}{\quad \.{macro}@>
4701 A \.{macro} command, like a \.{function} command, has two arguments;
4702 the first is a macro name between braces. The name must be a legal
4703 \.{.bst} identifier. Upper/lower cases are considered to be the
4704 same---all upper-case letters are converted to lower case. The second
4705 argument defines this macro. It consists of a
4706 |double_quote|-delimited string (which must be on a single line)
4707 between braces, with optional |white_space| characters between the
4708 braces and the |double_quote|s. This |double_quote|-delimited string
4709 is parsed exactly as a |str_literal| is for the \.{function} command.
4710
4711 @<Procedures and functions for the reading and processing of input files@>=
4712 procedure bst_macro_command;
4713 label exit;
4714 begin
4715 if (read_seen) then
4716 bst_err ('Illegal, macro command after read command');
4717 eat_bst_white_and_eof_check ('macro');
4718 @<Scan the macro name@>;
4719 eat_bst_white_and_eof_check ('macro');
4720 @<Scan the macro's definition@>;
4721 exit:
4722 end;
4723
4724
4725 @
4726 This module reads a |left_brace|, a macro name, and a |right_brace|.
4727
4728 @<Scan the macro name@>=
4729 begin
4730 bst_get_and_check_left_brace ('macro');
4731 eat_bst_white_and_eof_check ('macro');
4732 bst_identifier_scan ('macro');
4733 @<Check the \(m)macro name@>;
4734 eat_bst_white_and_eof_check ('macro');
4735 bst_get_and_check_right_brace ('macro');
4736 end
4737
4738
4739 @
4740 The macro name must be a new one; we mark it as |macro_ilk|.
4741
4742 @<Check the \(m)macro name@>=
4743 begin
4744 trace
4745 trace_pr_token;
4746 trace_pr_ln (' is a macro');
4747 ecart@/
4748 lower_case (buffer, buf_ptr1, token_len); {ignore case differences}
4749 macro_name_loc := str_lookup(buffer,buf_ptr1,token_len,macro_ilk,do_insert);
4750 if (hash_found) then
4751 begin
4752 print_token;
4753 bst_err (' is already defined as a macro');
4754 end;
4755 ilk_info[macro_name_loc]:=hash_text[macro_name_loc]; {default in case of error}
4756 end
4757
4758
4759 @
4760 This module reads a |left_brace|, the |double_quote|-delimited string
4761 that defines this macro, and a |right_brace|.
4762
4763 @<Scan the macro's definition@>=
4764 begin
4765 bst_get_and_check_left_brace ('macro');
4766 eat_bst_white_and_eof_check ('macro');
4767 if (scan_char <> double_quote) then
4768 bst_err ('A macro definition must be ',xchr[double_quote],'-delimited');
4769 @<Scan the macro definition-string@>;
4770 eat_bst_white_and_eof_check ('macro');
4771 bst_get_and_check_right_brace ('macro');
4772 end
4773
4774
4775 @
4776 A macro definition-string is preceded by a |double_quote| and consists
4777 of all characters on this line up to the next |double_quote|. The
4778 array |ilk_info| contains a pointer to this string for the macro name.
4779
4780 @<Scan the macro definition-string@>=
4781 begin
4782 incr(buf_ptr2); {skip over the |double_quote|}
4783 if (not scan1(double_quote)) then
4784 bst_err ('There''s no `',xchr[double_quote],''' to end macro definition');
4785 trace
4786 trace_pr ('"');
4787 trace_pr_token;
4788 trace_pr ('"');
4789 trace_pr_ln (' is a macro string');
4790 ecart@/
4791 macro_def_loc := str_lookup(buffer,buf_ptr1,token_len,text_ilk,do_insert);@/
4792 fn_type[macro_def_loc] := str_literal; {set the |fn_class|}
4793 ilk_info[macro_name_loc] := hash_text[macro_def_loc];
4794 incr(buf_ptr2); {skip over the |double_quote|}
4795 end
4796
4797
4798 @
4799 @^gymnastics@>
4800 We need to include stuff for \.{.bib} reading here because that's done
4801 by the \.{read} command.
4802
4803 @<Procedures and functions for the reading and processing of input files@>=
4804 @<Scan for and process a \.{.bib} command or database entry@>
4805
4806
4807 @
4808 @:style-file commands}{\quad \.{read}@>
4809 The \.{read} command has no arguments so there's no more parsing to
4810 do. We must make sure we haven't seen a \.{read} command before and
4811 we've already seen an \.{entry} command.
4812
4813 @<Procedures and functions for the reading and processing of input files@>=
4814 procedure bst_read_command;
4815 label exit;
4816 begin
4817 if (read_seen) then
4818 bst_err ('Illegal, another read command');
4819 read_seen := true; {now we've seen a \.{read} command}
4820 if (not entry_seen) then
4821 bst_err ('Illegal, read command before entry command');
4822 sv_ptr1 := buf_ptr2; {save the contents of the \.{.bst} input line}
4823 sv_ptr2 := last;
4824 tmp_ptr := sv_ptr1;
4825 while (tmp_ptr < sv_ptr2) do
4826 begin
4827 sv_buffer[tmp_ptr] := buffer[tmp_ptr];
4828 incr(tmp_ptr);
4829 end;
4830 @<Read the \.{.bib} file(s)@>;
4831 buf_ptr2 := sv_ptr1; {and restore}
4832 last := sv_ptr2;
4833 tmp_ptr := buf_ptr2;
4834 while (tmp_ptr < last) do
4835 begin
4836 buffer[tmp_ptr] := sv_buffer[tmp_ptr];
4837 incr(tmp_ptr);
4838 end;
4839 exit:
4840 end;
4841
4842
4843 @
4844 @:style-file commands}{\quad \.{reverse}@>
4845 A \.{reverse} command has one argument, a single |built_in| or
4846 |wiz_defined| function name between braces. Upper/lower cases are
4847 considered to be the same---all upper-case letters are converted to
4848 lower case. Also, we must make sure we've already seen a \.{read}
4849 command.
4850
4851 This module reads a |left_brace|, a single function to be iterated in
4852 reverse, and a |right_brace|.
4853
4854 @<Procedures and functions for the reading and processing of input files@>=
4855 procedure bst_reverse_command;
4856 label exit;
4857 begin
4858 if (not read_seen) then
4859 bst_err ('Illegal, reverse command before read command');
4860 eat_bst_white_and_eof_check ('reverse');
4861 bst_get_and_check_left_brace ('reverse');
4862 eat_bst_white_and_eof_check ('reverse');
4863 bst_identifier_scan ('reverse');
4864 @<Check the \(r)\.{reverse}-command argument token@>;
4865 eat_bst_white_and_eof_check ('reverse');
4866 bst_get_and_check_right_brace ('reverse');
4867 @<Perform a \.{reverse} command@>;
4868 exit:
4869 end;
4870
4871
4872 @
4873 Before iterating the function in reverse, we must make sure it's a
4874 legal one. It must exist and be |built_in| or |wiz_defined|.
4875
4876 @<Check the \(r)\.{reverse}-command argument token@>=
4877 begin
4878 trace
4879 trace_pr_token;
4880 trace_pr_ln (' is a to be iterated in reverse function');
4881 ecart@/
4882 if (bad_argument_token) then
4883 return;
4884 end
4885
4886
4887 @
4888 @:style-file commands}{\quad \.{sort}@>
4889 The \.{sort} command has no arguments so there's no more parsing to
4890 do, but we must make sure we've already seen a \.{read} command.
4891
4892 @<Procedures and functions for the reading and processing of input files@>=
4893 procedure bst_sort_command;
4894 label exit;
4895 begin
4896 if (not read_seen) then
4897 bst_err ('Illegal, sort command before read command');
4898 @<Perform a \.{sort} command@>;
4899 exit:
4900 end;
4901
4902
4903 @
4904 @:style-file commands}{\quad \.{strings}@>
4905 A \.{strings} command has one argument, a list of function names
4906 between braces (the names are separated by one or more |white_space|
4907 characters). Upper/lower cases are considered to be the same for
4908 function names in these lists---all upper-case letters are converted to
4909 lower case. Each name in this list specifies a |str_global_var|.
4910 There may be several \.{strings} commands in the \.{.bst} file.
4911
4912 This module reads a |left_brace|, a list of |str_global_var|s,
4913 and a |right_brace|.
4914
4915 @<Procedures and functions for the reading and processing of input files@>=
4916 procedure bst_strings_command;
4917 label exit;
4918 begin
4919 eat_bst_white_and_eof_check ('strings');
4920 bst_get_and_check_left_brace ('strings');
4921 eat_bst_white_and_eof_check ('strings');
4922 while (scan_char <> right_brace) do
4923 begin
4924 bst_identifier_scan ('strings');
4925 @<Insert a |str_global_var| into the hash table@>;
4926 eat_bst_white_and_eof_check ('strings');
4927 end;
4928 incr(buf_ptr2); {skip over the |right_brace|}
4929 exit:
4930 end;
4931
4932
4933 @
4934 @:BibTeX capacity exceeded}{\quad number of string global-variables@>
4935 Here we insert the just found |str_global_var| name into the hash
4936 table, record it as a |str_global_var|, set its pointer into
4937 |global_strs|, and initialize its value there to the null string.
4938
4939 @d end_of_string = invalid_code {this illegal |ASCII_code| ends a string}
4940
4941 @<Insert a |str_global_var| into the hash table@>=
4942 begin
4943 trace
4944 trace_pr_token;
4945 trace_pr_ln (' is a string global-variable');
4946 ecart@/
4947 lower_case (buffer, buf_ptr1, token_len); {ignore case differences}
4948 fn_loc := str_lookup(buffer,buf_ptr1,token_len,bst_fn_ilk,do_insert);
4949 check_for_already_seen_function (fn_loc);
4950 fn_type[fn_loc] := str_global_var;@/
4951 fn_info[fn_loc] := num_glb_strs; {pointer into |global_strs|}
4952 if (num_glb_strs = max_glob_strs) then
4953 overflow('number of string global-variables ',max_glob_strs);
4954 incr(num_glb_strs);
4955 end
4956
4957
4958 @
4959 @^gymnastics@>
4960 That's it for processing \.{.bst} commands, except for finishing the
4961 procedural gymnastics. Note that this must topologically follow the
4962 stuff for \.{.bib} reading, because that's done by the \.{.bst}'s
4963 \.{read} command.
4964
4965 @<Procedures and functions for the reading and processing of input files@>=
4966 @<Scan for and process a \.{.bst} command@>
4967
4968
4969
4970 @* Reading the database file(s).
4971 This section reads the \.{.bib} file(s), each of which consists of a
4972 sequence of entries (perhaps with a few \.{.bib} commands thrown in,
4973 as explained later). Each entry consists of an |at_sign|, an entry
4974 type, and, between braces or parentheses and separated by |comma|s, a
4975 database key and a list of fields. Each field consists of a field
4976 name, an |equals_sign|, and nonempty list of field tokens separated by
4977 |concat_char|s. Each field token is either a nonnegative number, a
4978 macro name (like `jan'), or a brace-balanced string delimited by
4979 either |double_quote|s or braces. Finally, case differences are
4980 ignored for all but delimited strings and database keys, and
4981 |white_space| characters and ends-of-line may appear in all reasonable
4982 places (i.e., anywhere except within entry types, database keys, field
4983 names, and macro names); furthermore, comments may appear anywhere
4984 between entries (or before the first or after the last) as long as
4985 they contain no |at_sign|s.
4986
4987
4988 @
4989 These global variables are used while reading the \.{.bib} file(s).
4990 The elements of |type_list|, which indicate an entry's type (book,
4991 article, etc.), point either to a |hash_loc| or are one of two special
4992 markers: |empty|, from which |hash_base = empty + 1| was defined,
4993 means we haven't yet encountered the \.{.bib} entry corresponding to
4994 this cite key; and |undefined| means we've encountered it but it had
4995 an unknown entry type. Thus the array |type_list| is of type
4996 |hash_ptr2|, also defined earlier. An element of the boolean array
4997 |entry_exists| whose corresponding entry in |cite_list| gets
4998 overwritten (which happens only when |all_entries| is |true|)
4999 indicates whether we've encountered that entry of |cite_list| while
5000 reading the \.{.bib} file(s); this information is unused for entries
5001 that aren't (or more precisely, that have no chance of being)
5002 overwritten. When we're reading the database file, the array
5003 |cite_info| contains auxiliary information for |cite_list|. Later,
5004 |cite_info| will become |sorted_cites|, and this dual role imposes the
5005 (not-very-imposing) restriction |max_strings >= max_cites|.
5006
5007 @d undefined = hash_max + 1 {a special marker used for |type_list|}
5008
5009 @<Globals in the outer block@>=
5010 @!bib_line_num : integer; {line number of the \.{.bib} file}
5011 @!entry_type_loc : hash_loc; {the hash-table location of an entry type}
5012 @!type_list : packed array[cite_number] of hash_ptr2;
5013 @!type_exists : boolean; {|true| if this entry type is \.{.bst}-defined}
5014 @!entry_exists : packed array[cite_number] of boolean;
5015 @!store_entry : boolean; {|true| if we're to store info for this entry}
5016 @!field_name_loc : hash_loc; {the hash-table location of a field name}
5017 @!field_val_loc : hash_loc; {the hash-table location of a field value}
5018 @!store_field : boolean; {|true| if we're to store info for this field}
5019 @!store_token : boolean; {|true| if we're to store this macro token}
5020 @!right_outer_delim : ASCII_code; {either a |right_brace| or a |right_paren|}
5021 @!right_str_delim : ASCII_code; {either a |right_brace| or a |double_quote|}
5022 @!at_bib_command : boolean; {|true| for a command, false for an entry}
5023 @!cur_macro_loc : hash_loc; {|macro_loc| for a \.{string} being defined}
5024 @!cite_info : packed array[cite_number] of str_number; {extra |cite_list| info}
5025 @!cite_hash_found : boolean; {set to a previous |hash_found| value}
5026 @!preamble_ptr : bib_number; {pointer into the |s_preamble| array}
5027 @!num_preamble_strings : bib_number; {counts the |s_preamble| strings}
5028
5029
5030 @
5031 This little procedure exists because it's used by at least two other
5032 procedures and thus saves some space.
5033
5034 @<Procedures and functions for all file I/O, error messages, and such@>=
5035 procedure bib_ln_num_print;
5036 begin
5037 print ('--line ',bib_line_num:0,' of file ');
5038 print_bib_name;
5039 end;
5040
5041
5042 @
5043 When there's a serious error parsing a \.{.bib} file, we flush
5044 everything up to the beginning of the next entry.
5045
5046 @d bib_err(#) == begin {serious error during \.{.bib} parsing}
5047 print (#);
5048 bib_err_print;
5049 return;
5050 end
5051
5052 @<Procedures and functions for all file I/O, error messages, and such@>=
5053 procedure bib_err_print;
5054 begin
5055 print ('-');
5056 bib_ln_num_print;
5057 print_bad_input_line; {this call does the |mark_error|}
5058 print_skipping_whatever_remains;
5059 if (at_bib_command) then
5060 print_ln ('command')
5061 else
5062 print_ln ('entry');
5063 end;
5064
5065
5066 @
5067 When there's a harmless error parsing a \.{.bib} file, we just give a
5068 warning message. This is always called after other stuff has been
5069 printed out.
5070
5071 @d bib_warn(#) == begin {non-serious error during \.{.bst} parsing}
5072 print (#);
5073 bib_warn_print;
5074 end
5075 @#
5076 @d bib_warn_newline(#) == begin {same as above but with a newline}
5077 print_ln (#);
5078 bib_warn_print;
5079 end
5080
5081 @<Procedures and functions for all file I/O, error messages, and such@>=
5082 procedure bib_warn_print;
5083 begin
5084 bib_ln_num_print;
5085 mark_warning;
5086 end;
5087
5088
5089 @
5090 For all |num_bib_files| database files, we keep reading and processing
5091 \.{.bib} entries until none left.
5092
5093 @<Read the \.{.bib} file(s)@>=
5094 begin
5095 @<Final initialization for \.{.bib} processing@>;
5096 read_performed := true;
5097 bib_ptr := 0;
5098 while (bib_ptr < num_bib_files) do
5099 begin
5100 print ('Database file #',bib_ptr+1:0,': ');
5101 print_bib_name;@/
5102 bib_line_num := 0; {initialize to get the first input line}
5103 buf_ptr2 := last;
5104 while (not eof(cur_bib_file)) do
5105 get_bib_command_or_entry_and_process;
5106 a_close (cur_bib_file);
5107 incr(bib_ptr);
5108 end;
5109 reading_completed := true;
5110 trace
5111 trace_pr_ln ('Finished reading the database file(s)');
5112 ecart@/
5113 @<Final initialization for processing the entries@>;
5114 read_completed := true;
5115 end
5116
5117
5118 @
5119 We need to initialize the |field_info| array, and also various things
5120 associated with the |cite_list| array (but not |cite_list| itself).
5121
5122 @<Final initialization for \.{.bib} processing@>=
5123 begin
5124 @<Initialize the |field_info|@>;
5125 @<Initialize things for the |cite_list|@>;
5126 end
5127
5128
5129 @
5130 This module initializes all fields of all entries to |missing|, the
5131 value to which all fields are initialized.
5132
5133 @<Initialize the |field_info|@>=
5134 begin
5135 check_field_overflow (num_fields*num_cites);
5136 field_ptr := 0;
5137 while (field_ptr < max_fields) do
5138 begin
5139 field_info[field_ptr] := missing;
5140 incr(field_ptr);
5141 end;
5142 end
5143
5144
5145 @
5146 @^fetish@>
5147 @:BibTeX capacity exceeded}{\quad total number of fields@>
5148 Complain if somebody's got a field fetish.
5149
5150 @<Procedures and functions for all file I/O, error messages, and such@>=
5151 procedure check_field_overflow (@!total_fields : integer);
5152 begin
5153 if (total_fields > max_fields) then
5154 begin
5155 print_ln (total_fields:0,' fields:');
5156 overflow('total number of fields ',max_fields);
5157 end;
5158 end;
5159
5160
5161 @
5162 We must initialize the |type_list| array so that we can detect
5163 duplicate (or missing) entries for cite keys on |cite_list|. Also,
5164 when we're to include the entire database, we use the array
5165 |entry_exists| to detect those missing entries whose |cite_list| info
5166 will (or to be more precise, might) be overwritten; and we use the
5167 array |cite_info| to save the part of |cite_list| that will (might) be
5168 overwritten. We also use |cite_info| for counting cross~references
5169 when it's appropriate---when an entry isn't otherwise to be included
5170 on |cite_list| (that is, the entry isn't \.{\\cite}d or
5171 \.{\\nocite}d). Such an entry is included on the final |cite_list| if
5172 it's cross~referenced at least |min_crossrefs| times.
5173
5174 @<Initialize things for the |cite_list|@>=
5175 begin
5176 cite_ptr := 0;
5177 while (cite_ptr < max_cites) do
5178 begin
5179 type_list[cite_ptr] := empty;@/
5180 cite_info[cite_ptr] := any_value; {to appease \PASCAL's boolean evaluation}
5181 incr(cite_ptr);
5182 end;
5183 old_num_cites := num_cites;
5184 if (all_entries) then
5185 begin
5186 cite_ptr := all_marker;
5187 while (cite_ptr < old_num_cites) do
5188 begin
5189 cite_info[cite_ptr] := cite_list[cite_ptr];
5190 entry_exists[cite_ptr] := false;
5191 incr(cite_ptr);
5192 end;
5193 cite_ptr := all_marker; {we insert the ``other'' entries here}
5194 end
5195 else
5196 begin
5197 cite_ptr := num_cites; {we insert the cross-referenced entries here}
5198 all_marker := any_value; {to appease \PASCAL's boolean evaluation}
5199 end;
5200 end
5201
5202
5203 @
5204 Before we actually start the code for reading a database file, we must
5205 define this \.{.bib}-specific scanning function. It skips over
5206 |white_space| characters until hitting a nonwhite character or the end
5207 of the file, respectively returning |true| or |false|. It also
5208 updates |bib_line_num|, the line counter.
5209
5210 @<Procedures and functions for input scanning@>=
5211 function eat_bib_white_space : boolean;
5212 label exit;
5213 begin
5214 while (not scan_white_space) do {no characters left; read another line}
5215 begin
5216 if (not input_ln(cur_bib_file)) then {end-of-file; return |false|}
5217 begin
5218 eat_bib_white_space := false;
5219 return;
5220 end;
5221 incr(bib_line_num);
5222 buf_ptr2 := 0;
5223 end;
5224 eat_bib_white_space := true;
5225 exit:
5226 end;
5227
5228
5229 @
5230 It's often illegal to end a \.{.bib} command in certain places, and
5231 this is where we come to check.
5232
5233 @d eat_bib_white_and_eof_check ==
5234 begin
5235 if (not eat_bib_white_space) then
5236 begin
5237 eat_bib_print;
5238 return;
5239 end;
5240 end
5241
5242 @<Procedures and functions for all file I/O, error messages, and such@>=
5243 procedure eat_bib_print;
5244 label exit; {so the call to |bib_err| works}
5245 begin
5246 bib_err ('Illegal end of database file');
5247 exit:
5248 end;
5249
5250
5251 @
5252 And here are a bunch of error-message macros, each called more than
5253 once, that thus save space as implemented. This one is for when one
5254 of two possible characters is expected while scanning.
5255
5256 @d bib_one_of_two_expected_err(#) ==
5257 begin
5258 bib_one_of_two_print (#);
5259 return;
5260 end
5261
5262 @<Procedures and functions for all file I/O, error messages, and such@>=
5263 procedure bib_one_of_two_print (@!char1,@!char2:ASCII_code);
5264 label exit; {so the call to |bib_err| works}
5265 begin
5266 bib_err ('I was expecting a `',xchr[char1],''' or a `',xchr[char2],'''');
5267 exit:
5268 end;
5269
5270
5271 @
5272 This one's for an expected |equals_sign|.
5273
5274 @d bib_equals_sign_expected_err ==
5275 begin
5276 bib_equals_sign_print;
5277 return;
5278 end
5279
5280 @<Procedures and functions for all file I/O, error messages, and such@>=
5281 procedure bib_equals_sign_print;
5282 label exit; {so the call to |bib_err| works}
5283 begin
5284 bib_err ('I was expecting an "',xchr[equals_sign],'"');
5285 exit:
5286 end;
5287
5288
5289 @
5290 This complains about unbalanced braces.
5291
5292 @d bib_unbalanced_braces_err ==
5293 begin
5294 bib_unbalanced_braces_print;
5295 return;
5296 end
5297
5298 @<Procedures and functions for all file I/O, error messages, and such@>=
5299 procedure bib_unbalanced_braces_print;
5300 label exit; {so the call to |bib_err| works}
5301 begin
5302 bib_err ('Unbalanced braces');
5303 exit:
5304 end;
5305
5306
5307 @
5308 And this one about an overly exuberant field.
5309
5310 @d bib_field_too_long_err ==
5311 begin
5312 bib_field_too_long_print;
5313 return;
5314 end
5315
5316 @<Procedures and functions for all file I/O, error messages, and such@>=
5317 procedure bib_field_too_long_print;
5318 label exit; {so the call to |bib_err| works}
5319 begin
5320 bib_err ('Your field is more than ',buf_size:0,' characters');
5321 exit:
5322 end;
5323
5324
5325 @
5326 This one is just a warning, not an error. It's for when something
5327 isn't (or might not be) quite right with a macro name.
5328
5329 @d macro_name_warning(#) ==
5330 begin
5331 macro_warn_print;
5332 bib_warn_newline (#);
5333 end
5334
5335 @<Procedures and functions for all file I/O, error messages, and such@>=
5336 procedure macro_warn_print;
5337 begin
5338 print ('Warning--string name "');
5339 print_token;
5340 print ('" is ');
5341 end;
5342
5343
5344 @
5345 @:this can't happen}{\quad Identifier scanning error@>
5346 This macro is used to scan all \.{.bib} identifiers. The argument
5347 tells what was happening at the time. The associated procedure simply
5348 prints an error message.
5349
5350 @d bib_identifier_scan_check(#) ==
5351 begin
5352 if ((scan_result = white_adjacent) or
5353 (scan_result = specified_char_adjacent)) then
5354 do_nothing
5355 else
5356 begin
5357 bib_id_print;
5358 bib_err (#);
5359 end;
5360 end
5361
5362 @<Procedures and functions for all file I/O, error messages, and such@>=
5363 procedure bib_id_print;
5364 begin
5365 if (scan_result = id_null) then
5366 print ('You''re missing ')
5367 else if (scan_result = other_char_adjacent) then
5368 print ('"',xchr[scan_char],'" immediately follows ')
5369 else
5370 id_scanning_confusion;
5371 end;
5372
5373
5374 @
5375 This module either reads a database entry, whose three main components
5376 are an entry type, a database key, and a list of fields, or it reads a
5377 \.{.bib} command, whose structure is command dependent and explained
5378 later.
5379
5380 @d cite_already_set = 22 {this gets around \PASCAL\ limitations}
5381 @d first_time_entry = 26 {for checking for repeated database entries}
5382
5383 @<Scan for and process a \.{.bib} command or database entry@>=
5384 procedure get_bib_command_or_entry_and_process;
5385 label cite_already_set,@!first_time_entry,@!loop_exit,@!exit;
5386 begin
5387 at_bib_command := false;@/
5388 @<Skip to the next database entry or \.{.bib} command@>;
5389 @<Scan the entry type or scan and process the \.{.bib} command@>;
5390 eat_bib_white_and_eof_check;
5391 @<Scan the entry's database key@>;
5392 eat_bib_white_and_eof_check;
5393 @<Scan the entry's list of fields@>;
5394 exit:
5395 end;
5396
5397
5398 @
5399 This module skips over everything until hitting an |at_sign| or the
5400 end of the file. It also updates |bib_line_num|, the line counter.
5401
5402 @<Skip to the next database entry or \.{.bib} command@>=
5403 while (not scan1(at_sign)) do {no |at_sign|; get next line}
5404 begin
5405 if (not input_ln(cur_bib_file)) then {end-of-file}
5406 return;
5407 incr(bib_line_num);
5408 buf_ptr2 := 0;
5409 end
5410
5411
5412 @
5413 @:this can't happen}{\quad An at-sign disappeared@>
5414 This module reads an |at_sign| and an entry type (like `book' or
5415 `article') or a \.{.bib} command. If it's an entry type, it must be
5416 defined in the \.{.bst} file if this entry is to be included in the
5417 reference list.
5418
5419 @<Scan the entry type or scan and process the \.{.bib} command@>=
5420 begin
5421 if (scan_char <> at_sign) then
5422 confusion ('An "',xchr[at_sign],'" disappeared');
5423 incr(buf_ptr2); {skip over the |at_sign|}
5424 eat_bib_white_and_eof_check;
5425 scan_identifier (left_brace,left_paren,left_paren);
5426 bib_identifier_scan_check ('an entry type');
5427 trace
5428 trace_pr_token;
5429 trace_pr_ln (' is an entry type or a database-file command');
5430 ecart@/
5431 lower_case (buffer, buf_ptr1, token_len); {ignore case differences}
5432 command_num := ilk_info[
5433 str_lookup(buffer,buf_ptr1,token_len,bib_command_ilk,dont_insert)];
5434 if (hash_found) then
5435 @<Process a \(b)\.{.bib} command@>
5436 else
5437 begin {process an entry type}
5438 entry_type_loc := str_lookup(
5439 buffer,buf_ptr1,token_len,bst_fn_ilk,dont_insert);
5440 if ((not hash_found) or (fn_type[entry_type_loc]<>wiz_defined)) then@/
5441 type_exists := false {no such entry type defined in the \.{.bst} file}
5442 else
5443 type_exists := true;
5444 end;
5445 end
5446
5447
5448 @
5449 @^database-file commands@>
5450 @:this can't happen}{\quad Unknown database-file command@>
5451 Here we determine which \.{.bib} command we're about to process, then
5452 go to it.
5453
5454 @<Process a \(b)\.{.bib} command@>=
5455 begin
5456 at_bib_command := true;
5457 case (command_num) of
5458 n_bib_comment : @<Process a \(c)\.{comment} command@>;
5459 n_bib_preamble : @<Process a \(p)\.{preamble} command@>;
5460 n_bib_string : @<Process a \(s)\.{string} command@>;
5461 othercases bib_cmd_confusion
5462 endcases;
5463 end
5464
5465
5466 @
5467 @:this can't happen}{\quad Unknown database-file command@>
5468 Here's another bug.
5469
5470 @<Procedures and functions for all file I/O, error messages, and such@>=
5471 procedure bib_cmd_confusion;
5472 begin
5473 confusion ('Unknown database-file command');
5474 end;
5475
5476
5477 @
5478 @:database-file commands}{\quad \.{comment}@>
5479 The \.{comment} command is implemented for SCRIBE compatibility. It's
5480 not really needed because \BibTeX\ treats (flushes) everything not
5481 within an entry as a comment anyway.
5482
5483 @<Process a \(c)\.{comment} command@>=
5484 begin
5485 return; {flush comments}
5486 end
5487
5488
5489 @
5490 @:database-file commands}{\quad \.{preamble}@>
5491 The \.{preamble} command lets a user have \TeX\ stuff inserted (by the
5492 standard styles, at least) directly into the \.{.bbl} file. It is
5493 intended primarily for allowing \TeX\ macro definitions used within
5494 the bibliography entries (for better sorting, for example). One
5495 \.{preamble} command per \.{.bib} file should suffice.
5496
5497 A \.{preamble} command has either braces or parentheses as outer
5498 delimiters. Inside is the preamble string, which has the same syntax
5499 as a field value: a nonempty list of field tokens separated by
5500 |concat_char|s. There are three types of field tokens---nonnegative
5501 numbers, macro names, and delimited strings.
5502
5503 This module does all the scanning (that's not subcontracted), but the
5504 \.{.bib}-specific scanning function
5505 |scan_and_store_the_field_value_and_eat_white| actually stores the
5506 value.
5507
5508 @<Process a \(p)\.{preamble} command@>=
5509 begin
5510 if (preamble_ptr = max_bib_files) then
5511 bib_err ('You''ve exceeded ',max_bib_files:0,' preamble commands');
5512 eat_bib_white_and_eof_check;
5513 if (scan_char = left_brace) then
5514 right_outer_delim := right_brace
5515 else if (scan_char = left_paren) then
5516 right_outer_delim := right_paren
5517 else
5518 bib_one_of_two_expected_err (left_brace,left_paren);
5519 incr(buf_ptr2); {skip over the left-delimiter}
5520 eat_bib_white_and_eof_check;
5521 store_field := true;
5522 if (not scan_and_store_the_field_value_and_eat_white) then
5523 return;
5524 if (scan_char <> right_outer_delim) then
5525 bib_err ('Missing "',xchr[right_outer_delim],'" in preamble command');
5526 incr(buf_ptr2); {skip over the |right_outer_delim|}
5527 return;
5528 end
5529
5530
5531 @
5532 @:database-file commands}{\quad \.{string}@>
5533 The \.{string} command is implemented both for SCRIBE compatibility
5534 and for allowing a user: to override a \.{.bst}-file \.{macro}
5535 command, to define one that the \.{.bst} file doesn't, or to engage in
5536 good, wholesome, typing laziness.
5537
5538 The \.{string} command does mostly the same thing as the
5539 \.{.bst}-file's \.{macro} command (but the syntax is different and the
5540 \.{string} command compresses |white_space|). In fact, later in this
5541 program, the term ``macro'' refers to either a \.{.bst} ``macro'' or a
5542 \.{.bib} ``string'' (when it's clear from the context that it's not
5543 a \.{WEB} macro).
5544
5545 A \.{string} command has either braces or parentheses as outer
5546 delimiters. Inside is the string's name (it must be a legal
5547 identifier, and case differences are ignored---all upper-case letters
5548 are converted to lower case), then an |equals_sign|, and the string's
5549 definition, which has the same syntax as a field value: a nonempty
5550 list of field tokens separated by |concat_char|s. There are three
5551 types of field tokens---nonnegative numbers, macro names, and
5552 delimited strings.
5553
5554 @<Process a \(s)\.{string} command@>=
5555 begin
5556 eat_bib_white_and_eof_check;
5557 @<Scan the string's name@>;
5558 eat_bib_white_and_eof_check;
5559 @<Scan the string's definition field@>;
5560 return;
5561 end
5562
5563
5564 @
5565 This module reads a left outer-delimiter and a string name.
5566
5567 @<Scan the string's name@>=
5568 begin
5569 if (scan_char = left_brace) then
5570 right_outer_delim := right_brace
5571 else if (scan_char = left_paren) then
5572 right_outer_delim := right_paren
5573 else
5574 bib_one_of_two_expected_err (left_brace,left_paren);
5575 incr(buf_ptr2); {skip over the left-delimiter}
5576 eat_bib_white_and_eof_check;
5577 scan_identifier (equals_sign,equals_sign,equals_sign);
5578 bib_identifier_scan_check ('a string name');
5579 @<Store the string's name@>;
5580 end
5581
5582
5583 @
5584 @^commented-out code@>
5585 This module marks this string as |macro_ilk|; the commented-out code
5586 will give a warning message when overwriting a previously defined
5587 macro.
5588
5589 @<Store the string's name@>=
5590 begin
5591 trace
5592 trace_pr_token;
5593 trace_pr_ln (' is a database-defined macro');
5594 ecart@/
5595 lower_case (buffer, buf_ptr1, token_len); {ignore case differences}
5596 cur_macro_loc := str_lookup(buffer,buf_ptr1,token_len,macro_ilk,do_insert);
5597 ilk_info[cur_macro_loc] := hash_text[cur_macro_loc]; {default in case of error}
5598 @{
5599 if (hash_found) then {already seen macro}
5600 macro_name_warning ('having its definition overwritten');
5601 @}@/
5602 end
5603
5604
5605 @
5606 This module skips over the |equals_sign|, reads and stores the list of
5607 field tokens that defines this macro (compressing |white_space|), and
5608 reads a |right_outer_delim|.
5609
5610 @<Scan the string's definition field@>=
5611 begin
5612 if (scan_char <> equals_sign) then
5613 bib_equals_sign_expected_err;
5614 incr(buf_ptr2); {skip over the |equals_sign|}
5615 eat_bib_white_and_eof_check;
5616 store_field := true;
5617 if (not scan_and_store_the_field_value_and_eat_white) then
5618 return;
5619 if (scan_char <> right_outer_delim) then
5620 bib_err ('Missing "',xchr[right_outer_delim],'" in string command');
5621 incr(buf_ptr2); {skip over the |right_outer_delim|}
5622 end
5623
5624
5625 @
5626 @^kludge@>
5627 The variables for the function
5628 |scan_and_store_the_field_value_and_eat_white| must be global since
5629 the functions it calls use them too. The alias kludge helps make the
5630 stack space not overflow on some machines.
5631
5632 @d field_vl_str == ex_buf {aliases, used ``only'' for this function}
5633 @d field_end == ex_buf_ptr {the end marker for the field-value string}
5634 @d field_start == ex_buf_xptr {and the start marker}
5635
5636 @<Globals in the outer block@>=
5637 @!bib_brace_level : integer; {brace nesting depth (excluding |str_delim|s)}
5638
5639
5640 @
5641 @^gymnastics@>
5642 Since the function |scan_and_store_the_field_value_and_eat_white|
5643 calls several other yet-to-be-described functions (one directly and
5644 two indirectly), we must perform some topological gymnastics.
5645
5646 @<Procedures and functions for input scanning@>=
5647 @<The scanning function |compress_bib_white|@>@;
5648 @<The scanning function |scan_balanced_braces|@>@;
5649 @<The scanning function |scan_a_field_token_and_eat_white|@>
5650
5651
5652 @
5653 This function scans the list of field tokens that define the field
5654 value string. If |store_field| is |true| it accumulates (indirectly)
5655 in |field_vl_str| the concatenation of all the field tokens,
5656 compressing nonnull |white_space| to a single |space| and, if the
5657 field value is for a field (rather than a string definition), removing
5658 any leading or trailing |white_space|; when it's finished it puts the
5659 string into the hash table. It returns |false| if there was a serious
5660 syntax error.
5661
5662 @<Procedures and functions for input scanning@>=
5663 function scan_and_store_the_field_value_and_eat_white : boolean;
5664 label exit;
5665 begin
5666 scan_and_store_the_field_value_and_eat_white := false;
5667 {now it's easy to exit if necessary}
5668 field_end := 0;
5669 if (not scan_a_field_token_and_eat_white) then
5670 return;
5671 while (scan_char = concat_char) do {scan remaining field tokens}
5672 begin
5673 incr(buf_ptr2); {skip over the |concat_char|}
5674 eat_bib_white_and_eof_check;
5675 if (not scan_a_field_token_and_eat_white) then
5676 return;
5677 end;
5678 if (store_field) then
5679 @<Store the field value string@>;
5680 scan_and_store_the_field_value_and_eat_white := true;
5681 exit:
5682 end;
5683
5684
5685 @
5686 Each field token is either a nonnegative number, a macro name (like
5687 `jan'), or a brace-balanced string delimited by either |double_quote|s
5688 or braces. Thus there are four possibilities for the first character
5689 of the field token: If it's a |left_brace| or a |double_quote|, the
5690 token (with balanced braces, up to the matching |right_str_delim|) is
5691 a string; if it's |numeric|, the token is a number; if it's anything
5692 else, the token is a macro name (and should thus have been defined by
5693 either the \.{.bst}-file's \.{macro} command or the \.{.bib}-file's
5694 \.{string} command). This function returns |false| if there was a
5695 serious syntax error.
5696
5697 @<The scanning function |scan_a_field_token_and_eat_white|@>=
5698 function scan_a_field_token_and_eat_white : boolean;
5699 label exit;
5700 begin
5701 scan_a_field_token_and_eat_white := false; {now it's easy to exit if necessary}
5702 case (scan_char) of
5703 left_brace :
5704 begin
5705 right_str_delim := right_brace;
5706 if (not scan_balanced_braces) then
5707 return;
5708 end;
5709 double_quote :
5710 begin
5711 right_str_delim := double_quote;
5712 if (not scan_balanced_braces) then
5713 return;
5714 end;
5715 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" :
5716 @<Scan a number@>;
5717 othercases
5718 @<Scan a macro name@>
5719 endcases;
5720 eat_bib_white_and_eof_check;
5721 scan_a_field_token_and_eat_white := true;
5722 exit:
5723 end;
5724
5725
5726 @
5727 Now we come to the stuff that actually accumulates the field value to
5728 be stored. This module copies a character into |field_vl_str| if it
5729 will fit; since it's so low level, it's implemented as a macro.
5730
5731 @d copy_char(#) == begin
5732 if (field_end = buf_size) then
5733 bib_field_too_long_err
5734 else
5735 begin
5736 field_vl_str[field_end] := #;
5737 incr(field_end);
5738 end;
5739 end
5740
5741
5742 @
5743 The \.{.bib}-specific scanning function |compress_bib_white| skips
5744 over |white_space| characters within a string until hitting a nonwhite
5745 character; in fact, it does everything |eat_bib_white_space| does, but
5746 it also adds a |space| to |field_vl_str|. This function is never
5747 called if there are no |white_space| characters (or ends-of-line) to
5748 be scanned (though the associated macro might be). The function
5749 returns |false| if there is a serious syntax error.
5750
5751 @d check_for_and_compress_bib_white_space ==
5752 begin
5753 if ((lex_class[scan_char]=white_space) or (buf_ptr2=last)) then
5754 if (not compress_bib_white) then
5755 return;
5756 end
5757
5758 @<The scanning function |compress_bib_white|@>=
5759 function compress_bib_white : boolean;
5760 label exit;
5761 begin
5762 compress_bib_white := false; {now it's easy to exit if necessary}
5763 copy_char (space);
5764 while (not scan_white_space) do {no characters left; read another line}
5765 begin
5766 if (not input_ln(cur_bib_file)) then {end-of-file; complain}
5767 begin
5768 eat_bib_print;
5769 return;
5770 end;
5771 incr(bib_line_num);
5772 buf_ptr2 := 0;
5773 end;
5774 compress_bib_white := true;
5775 exit:
5776 end;
5777
5778
5779 @
5780 This \.{.bib}-specific function scans a string with balanced braces,
5781 stopping just past the matching |right_str_delim|. How much work it
5782 does depends on whether |store_field = true|. It returns |false| if
5783 there was a serious syntax error.
5784
5785 @<The scanning function |scan_balanced_braces|@>=
5786 function scan_balanced_braces : boolean;
5787 label loop_exit,@!exit;
5788 begin
5789 scan_balanced_braces := false; {now it's easy to exit if necessary}
5790 incr(buf_ptr2); {skip over the left-delimiter}
5791 check_for_and_compress_bib_white_space;
5792 if (field_end > 1) then
5793 if (field_vl_str[field_end-1] = space) then
5794 if (field_vl_str[field_end-2] = space) then {remove wrongly added |space|}
5795 decr(field_end);
5796 bib_brace_level := 0; {and we're at a non|white_space| character}
5797 if (store_field) then
5798 @<Do a full brace-balanced scan@>
5799 else
5800 @<Do a quick brace-balanced scan@>;
5801 incr(buf_ptr2); {skip over the |right_str_delim|}
5802 scan_balanced_braces := true;
5803 exit:
5804 end;
5805
5806
5807 @
5808 This module scans over a brace-balanced string without keeping track
5809 of anything but the brace level. It starts with |bib_brace_level = 0|
5810 and at a non|white_space| character.
5811
5812 @<Do a quick brace-balanced scan@>=
5813 begin
5814 while (scan_char <> right_str_delim) do {we're at |bib_brace_level = 0|}
5815 if (scan_char = left_brace) then
5816 begin
5817 incr(bib_brace_level);
5818 incr(buf_ptr2); {skip over the |left_brace|}
5819 eat_bib_white_and_eof_check;
5820 while (bib_brace_level > 0) do
5821 @<Do a quick scan with |bib_brace_level > 0|@>;
5822 end
5823 else if (scan_char = right_brace) then
5824 bib_unbalanced_braces_err
5825 else
5826 begin
5827 incr(buf_ptr2); {skip over some other character}
5828 if (not scan3 (right_str_delim, left_brace, right_brace)) then
5829 eat_bib_white_and_eof_check;
5830 end
5831 end
5832
5833
5834 @
5835 This module does the same as above but, because |bib_brace_level > 0|, it
5836 doesn't have to look for a |right_str_delim|.
5837
5838 @<Do a quick scan with |bib_brace_level > 0|@>=
5839 begin {top part of the |while| loop---we're always at a nonwhite character}
5840 if (scan_char = right_brace) then
5841 begin
5842 decr(bib_brace_level);
5843 incr(buf_ptr2); {skip over the |right_brace|}
5844 eat_bib_white_and_eof_check;
5845 end
5846 else if (scan_char = left_brace) then
5847 begin
5848 incr(bib_brace_level);
5849 incr(buf_ptr2); {skip over the |left_brace|}
5850 eat_bib_white_and_eof_check;
5851 end
5852 else
5853 begin
5854 incr(buf_ptr2); {skip over some other character}
5855 if (not scan2 (right_brace, left_brace)) then
5856 eat_bib_white_and_eof_check;
5857 end
5858 end
5859
5860
5861 @
5862 This module scans over a brace-balanced string, compressing multiple
5863 |white_space| characters into a single |space|. It starts with
5864 |bib_brace_level = 0| and starts at a non|white_space| character.
5865
5866 @<Do a full brace-balanced scan@>=
5867 begin
5868 while (scan_char <> right_str_delim) do
5869 case (scan_char) of
5870 left_brace :
5871 begin
5872 incr(bib_brace_level);
5873 copy_char (left_brace);@/
5874 incr(buf_ptr2); {skip over the |left_brace|}
5875 check_for_and_compress_bib_white_space;@/
5876 @<Do a full scan with |bib_brace_level > 0|@>;
5877 end;
5878 right_brace :
5879 bib_unbalanced_braces_err;
5880 othercases
5881 begin
5882 copy_char (scan_char);
5883 incr(buf_ptr2); {skip over some other character}
5884 check_for_and_compress_bib_white_space;
5885 end
5886 endcases;
5887 end
5888
5889
5890 @
5891 This module is similar to the last but starts with |bib_brace_level > 0|
5892 (and, like the last, it starts at a non|white_space| character).
5893
5894 @<Do a full scan with |bib_brace_level > 0|@>=
5895 begin
5896 loop
5897 case (scan_char) of
5898 right_brace :
5899 begin
5900 decr(bib_brace_level);
5901 copy_char (right_brace);@/
5902 incr(buf_ptr2); {skip over the |right_brace|}
5903 check_for_and_compress_bib_white_space;
5904 if (bib_brace_level = 0) then
5905 goto loop_exit;
5906 end;
5907 left_brace :
5908 begin
5909 incr(bib_brace_level);
5910 copy_char (left_brace);@/
5911 incr(buf_ptr2); {skip over the |left_brace|}
5912 check_for_and_compress_bib_white_space;
5913 end;
5914 othercases
5915 begin
5916 copy_char (scan_char);
5917 incr(buf_ptr2); {skip over some other character}
5918 check_for_and_compress_bib_white_space;
5919 end
5920 endcases;
5921 loop_exit:
5922 end
5923
5924
5925 @
5926 @:this can't happen}{\quad A digit disappeared@>
5927 This module scans a nonnegative number and copies it to |field_vl_str|
5928 if it's to store the field.
5929
5930 @<Scan a number@>=
5931 begin
5932 if (not scan_nonneg_integer) then
5933 confusion ('A digit disappeared');
5934 if (store_field) then
5935 begin
5936 tmp_ptr := buf_ptr1;
5937 while (tmp_ptr < buf_ptr2) do
5938 begin
5939 copy_char (buffer[tmp_ptr]);
5940 incr(tmp_ptr);
5941 end;
5942 end;
5943 end
5944
5945
5946 @
5947 This module scans a macro name and copies its string to |field_vl_str|
5948 if it's to store the field, complaining if the macro is recursive or
5949 undefined.
5950
5951 @<Scan a macro name@>=
5952 begin
5953 scan_identifier (comma,right_outer_delim,concat_char);
5954 bib_identifier_scan_check ('a field part');
5955 if (store_field) then
5956 begin
5957 lower_case (buffer, buf_ptr1, token_len); {ignore case differences}
5958 macro_name_loc := str_lookup(
5959 buffer,buf_ptr1,token_len,macro_ilk,dont_insert);
5960 store_token := true;
5961 if (at_bib_command) then
5962 if (command_num = n_bib_string) then
5963 if (macro_name_loc = cur_macro_loc) then
5964 begin
5965 store_token := false;
5966 macro_name_warning ('used in its own definition');
5967 end;
5968 if (not hash_found) then
5969 begin
5970 store_token := false;
5971 macro_name_warning ('undefined');
5972 end;
5973 if (store_token) then
5974 @<Copy the macro string to |field_vl_str|@>;
5975 end;
5976 end
5977
5978
5979 @
5980 The macro definition may have |white_space| that needs compressing,
5981 because it may have been defined in the \.{.bst} file.
5982
5983 @<Copy the macro string to |field_vl_str|@>=
5984 begin
5985 tmp_ptr := str_start[ilk_info[macro_name_loc]];
5986 tmp_end_ptr := str_start[ilk_info[macro_name_loc]+1];
5987 if (field_end = 0) then
5988 if ((lex_class[str_pool[tmp_ptr]] = white_space) and (tmp_ptr < tmp_end_ptr))
5989 then
5990 begin {compress leading |white_space| of first nonnull token}
5991 copy_char (space);
5992 incr(tmp_ptr);
5993 while ((lex_class[str_pool[tmp_ptr]] = white_space) and
5994 (tmp_ptr < tmp_end_ptr)) do
5995 incr(tmp_ptr);
5996 end; {the next remaining character is non|white_space|}
5997 while (tmp_ptr < tmp_end_ptr) do
5998 begin
5999 if (lex_class[str_pool[tmp_ptr]] <> white_space) then
6000 copy_char (str_pool[tmp_ptr])
6001 else if (field_vl_str[field_end-1] <> space) then
6002 copy_char (space);
6003 incr(tmp_ptr);
6004 end;
6005 end
6006
6007
6008 @
6009 @^ham and eggs@>
6010 Now it's time to store the field value in the hash table, and store an
6011 appropriate pointer to it (depending on whether it's for a database
6012 entry or command). But first, if necessary, we remove a trailing
6013 |space| and a leading |space| if these exist. (Hey, if we had some
6014 ham we could make ham-and-eggs if we had some eggs.)
6015
6016 @<Store the field value string@>=
6017 begin
6018 if (not at_bib_command) then {chop trailing |space| for a field}
6019 if (field_end > 0) then
6020 if (field_vl_str[field_end-1] = space) then
6021 decr(field_end);
6022 if ((not at_bib_command) and (field_vl_str[0] = space) and (field_end > 0))
6023 then {chop leading |space| for a field}
6024 field_start := 1
6025 else
6026 field_start := 0;
6027 field_val_loc := str_lookup(field_vl_str,field_start,field_end-field_start,
6028 text_ilk,do_insert);
6029 fn_type[field_val_loc] := str_literal; {set the |fn_class|}
6030 trace
6031 trace_pr ('"');
6032 trace_pr_pool_str (hash_text[field_val_loc]);
6033 trace_pr_ln ('" is a field value');
6034 ecart@/
6035 if (at_bib_command) then {for a \.{preamble} or \.{string} command}
6036 @<Store the field value for a command@>
6037 else {for a database entry}
6038 @<Store the field value for a database entry@>;
6039 end
6040
6041
6042 @
6043 @:this can't happen}{\quad Unknown database-file command@>
6044 Here's where we store the goods when we're dealing with a command
6045 rather than an entry.
6046
6047 @<Store the field value for a command@>=
6048 begin
6049 case (command_num) of
6050 n_bib_preamble :
6051 begin
6052 s_preamble[preamble_ptr] := hash_text[field_val_loc];
6053 incr(preamble_ptr);
6054 end;
6055 n_bib_string :
6056 ilk_info[cur_macro_loc] := hash_text[field_val_loc];
6057 othercases bib_cmd_confusion
6058 endcases;
6059 end
6060
6061
6062 @
6063 And here, an entry.
6064
6065 @<Store the field value for a database entry@>=
6066 begin
6067 field_ptr := entry_cite_ptr * num_fields + fn_info[field_name_loc];
6068 if (field_info[field_ptr] <> missing) then
6069 begin
6070 print ('Warning--I''m ignoring ');
6071 print_pool_str (cite_list[entry_cite_ptr]);
6072 print ('''s extra "');
6073 print_pool_str (hash_text[field_name_loc]);
6074 bib_warn_newline ('" field');
6075 end
6076 else
6077 begin {the field was empty, store its new value}
6078 field_info[field_ptr] := hash_text[field_val_loc];
6079 if ((fn_info[field_name_loc] = crossref_num) and (not all_entries)) then
6080 @<Add or update a cross reference on |cite_list| if necessary@>;
6081 end;
6082 end
6083
6084
6085 @
6086 @^kludge@>
6087 @:this can't happen}{\quad Cite hash error@>
6088 If the cross-referenced entry isn't already on |cite_list| we add it
6089 (at least temporarily); if it is already on |cite_list| we update the
6090 cross-reference count, if necessary. Note that |all_entries| is
6091 |false| here. The alias kludge helps make the stack space not
6092 overflow on some machines.
6093
6094 @d extra_buf == out_buf {an alias, used only in this module}
6095
6096 @<Add or update a cross reference on |cite_list| if necessary@>=
6097 begin
6098 tmp_ptr := field_start;
6099 while (tmp_ptr < field_end) do
6100 begin
6101 extra_buf[tmp_ptr] := field_vl_str[tmp_ptr];
6102 incr(tmp_ptr);
6103 end;
6104 lower_case (extra_buf, field_start, field_end-field_start);
6105 {convert to `canonical' form}
6106 lc_cite_loc := str_lookup(extra_buf,field_start,field_end-field_start,
6107 lc_cite_ilk,do_insert);
6108 if (hash_found) then
6109 begin
6110 cite_loc := ilk_info[lc_cite_loc]; {even if there's a case mismatch}
6111 if (ilk_info[cite_loc] >= old_num_cites) then {a previous \.{crossref}}
6112 incr(cite_info[ilk_info[cite_loc]]);
6113 end
6114 else
6115 begin {it's a new \.{crossref}}
6116 cite_loc := str_lookup(field_vl_str,field_start,field_end-field_start,
6117 cite_ilk,do_insert);
6118 if (hash_found) then
6119 hash_cite_confusion;
6120 add_database_cite (cite_ptr); {this increments |cite_ptr|}
6121 cite_info[ilk_info[cite_loc]] := 1; {the first cross-ref for this cite key}
6122 end;
6123 end
6124
6125
6126 @
6127 This procedure adds (or restores) to |cite_list| a cite key; it is
6128 called only when |all_entries| is |true| or when adding
6129 cross~references, and it assumes that |cite_loc| and |lc_cite_loc| are
6130 set. It also increments its argument.
6131
6132 @<Procedures and functions for handling numbers, characters, and strings@>=
6133 procedure add_database_cite (var new_cite : cite_number);
6134 begin
6135 check_cite_overflow (new_cite); {make sure this cite will fit}
6136 check_field_overflow (num_fields*new_cite);
6137 cite_list[new_cite] := hash_text[cite_loc];
6138 ilk_info[cite_loc] := new_cite;
6139 ilk_info[lc_cite_loc] := cite_loc;
6140 incr(new_cite);
6141 end;
6142
6143
6144 @
6145 And now, back to processing an entry (rather than a command). This
6146 module reads a left outer-delimiter and a database key.
6147
6148 @<Scan the entry's database key@>=
6149 begin
6150 if (scan_char = left_brace) then
6151 right_outer_delim := right_brace
6152 else if (scan_char = left_paren) then
6153 right_outer_delim := right_paren
6154 else
6155 bib_one_of_two_expected_err (left_brace,left_paren);
6156 incr(buf_ptr2); {skip over the left-delimiter}
6157 eat_bib_white_and_eof_check;
6158 if (right_outer_delim = right_paren) then {to allow it in a database key}
6159 begin
6160 if (scan1_white(comma)) then {ok if database key ends line}
6161 do_nothing;
6162 end
6163 else
6164 if (scan2_white(comma,right_brace)) then {|right_brace=right_outer_delim|}
6165 do_nothing;
6166 @<Check for a database key of interest@>;
6167 end
6168
6169
6170 @
6171 @^kludge@>
6172 The lower-case version of this database key must correspond to one in
6173 |cite_list|, or else |all_entries| must be |true|, if this entry is to
6174 be included in the reference list. Accordingly, this module sets
6175 |store_entry|, which determines whether the relevant information for
6176 this entry is stored. The alias kludge helps make the stack space not
6177 overflow on some machines.
6178
6179 @d ex_buf3 == ex_buf {an alias, used only in this module}
6180
6181 @<Check for a database key of interest@>=
6182 begin
6183 trace
6184 trace_pr_token;
6185 trace_pr_ln (' is a database key');
6186 ecart@/
6187 tmp_ptr := buf_ptr1;
6188 while (tmp_ptr < buf_ptr2) do
6189 begin
6190 ex_buf3[tmp_ptr] := buffer[tmp_ptr];
6191 incr(tmp_ptr);
6192 end;
6193 lower_case (ex_buf3, buf_ptr1, token_len); {convert to `canonical' form}
6194 if (all_entries) then
6195 lc_cite_loc := str_lookup(ex_buf3,buf_ptr1,token_len,lc_cite_ilk,do_insert)
6196 else
6197 lc_cite_loc := str_lookup(ex_buf3,buf_ptr1,token_len,lc_cite_ilk,
6198 dont_insert);
6199 if (hash_found) then
6200 begin
6201 entry_cite_ptr := ilk_info[ilk_info[lc_cite_loc]];
6202 @<Check for a duplicate or \.{crossref}-matching database key@>;
6203 end;
6204 store_entry := true; {unless |(not hash_found) and (not all_entries)|}
6205 if (all_entries) then
6206 @<Put this cite key in its place@>
6207 else if (not hash_found) then
6208 store_entry := false; {no such cite key exists on |cite_list|}
6209 if (store_entry) then
6210 @<Make sure this entry is ok before proceeding@>;
6211 end
6212
6213
6214 @
6215 @:this can't happen}{\quad The cite list is messed up@>
6216 It's illegal to have two (or more) entries with the same database key
6217 (even if there are case differences), and we skip the rest of the
6218 entry for such a repeat occurrence. Also, we make this entry's
6219 database key the official |cite_list| key if it's on |cite_list| only
6220 because of cross references.
6221
6222 @<Check for a duplicate or \.{crossref}-matching database key@>=
6223 begin
6224 if ((not all_entries) or (entry_cite_ptr < all_marker)
6225 or (entry_cite_ptr >= old_num_cites)) then
6226 begin
6227 if (type_list[entry_cite_ptr] = empty) then
6228 begin
6229 @<Make sure this entry's database key is on |cite_list|@>;
6230 goto first_time_entry;
6231 end;
6232 end
6233 else if (not entry_exists[entry_cite_ptr]) then
6234 begin
6235 @<Find the lower-case equivalent of the |cite_info| key@>;
6236 if (lc_xcite_loc = lc_cite_loc) then
6237 goto first_time_entry;
6238 end;@/
6239 {oops---repeated entry---issue a reprimand}
6240 if (type_list[entry_cite_ptr] = empty) then
6241 confusion ('The cite list is messed up');
6242 bib_err ('Repeated entry');
6243 first_time_entry: {note that when we leave normally, |hash_found| is |true|}
6244 end
6245
6246
6247 @
6248 An entry that's on |cite_list| only because of cross referencing must
6249 have its database key (rather than one of the \.{crossref} keys) as
6250 the official |cite_list| string. Here's where we assure that. The
6251 variable |hash_found| is |true| upon entrance to and exit from this
6252 module.
6253
6254 @<Make sure this entry's database key is on |cite_list|@>=
6255 begin
6256 if ((not all_entries) and (entry_cite_ptr >= old_num_cites)) then
6257 begin
6258 cite_loc := str_lookup(buffer,buf_ptr1,token_len,cite_ilk,do_insert);
6259 if (not hash_found) then
6260 begin {it's not on |cite_list|---put it there}
6261 ilk_info[lc_cite_loc] := cite_loc;
6262 ilk_info[cite_loc] := entry_cite_ptr;
6263 cite_list[entry_cite_ptr] := hash_text[cite_loc];@/
6264 hash_found := true; {restore this value for later use}
6265 end;
6266 end;
6267 end
6268
6269
6270 @
6271 @^kludge@>
6272 @:this can't happen}{\quad A cite key disappeared@>
6273 This module, a simpler version of the
6274 |find_cite_locs_for_this_cite_key| function, exists primarily to
6275 compute |lc_xcite_loc|. When this code is executed we have
6276 |(all_entries) and (entry_cite_ptr >= all_marker) and (not
6277 entry_exists[entry_cite_ptr])|. The alias kludge helps make the stack
6278 space not overflow on some machines.
6279
6280 @d ex_buf4 == ex_buf {aliases, used only}
6281 @d ex_buf4_ptr == ex_buf_ptr {in this module}
6282
6283 @<Find the lower-case equivalent of the |cite_info| key@>=
6284 begin
6285 ex_buf4_ptr := 0;
6286 tmp_ptr := str_start[cite_info[entry_cite_ptr]];
6287 tmp_end_ptr := str_start[cite_info[entry_cite_ptr]+1];
6288 while (tmp_ptr < tmp_end_ptr) do
6289 begin
6290 ex_buf4[ex_buf4_ptr] := str_pool[tmp_ptr];
6291 incr(ex_buf4_ptr);
6292 incr(tmp_ptr);
6293 end;
6294 lower_case (ex_buf4, 0, length(cite_info[entry_cite_ptr]));
6295 {convert to `canonical' form}
6296 lc_xcite_loc := str_lookup(ex_buf4,0,length(cite_info[entry_cite_ptr]),
6297 lc_cite_ilk,dont_insert);
6298 if (not hash_found) then
6299 cite_key_disappeared_confusion;
6300 end
6301
6302
6303 @
6304 @:this can't happen}{\quad A cite key disappeared@>
6305 Here's another bug complaint.
6306
6307 @<Procedures and functions for all file I/O, error messages, and such@>=
6308 procedure cite_key_disappeared_confusion;
6309 begin
6310 confusion ('A cite key disappeared');
6311 end;
6312
6313
6314 @
6315 @:this can't happen}{\quad Cite hash error@>
6316 This module, which gets executed only when |all_entries| is |true|,
6317 does one of three things, depending on whether or not, and where, the
6318 cite key appears on |cite_list|: If it's on |cite_list| before
6319 |all_marker|, there's nothing to be done; if it's after |all_marker|,
6320 it must be reinserted (at the current place) and we must note that its
6321 corresponding entry exists; and if it's not on |cite_list| at all, it
6322 must be inserted for the first time. The |goto| construct must stay
6323 as is, partly because some \PASCAL\ compilers might complain if
6324 ``|and|'' were to connect the two boolean expressions (since
6325 |entry_cite_ptr| could be uninitialized when |hash_found| is |false|).
6326
6327 @<Put this cite key in its place@>=
6328 begin
6329 if (hash_found) then
6330 begin
6331 if (entry_cite_ptr < all_marker) then
6332 goto cite_already_set {that is, do nothing}
6333 else
6334 begin
6335 entry_exists[entry_cite_ptr] := true;
6336 cite_loc := ilk_info[lc_cite_loc];
6337 end;
6338 end
6339 else
6340 begin {this is a new key}
6341 cite_loc := str_lookup(buffer,buf_ptr1,token_len,cite_ilk,do_insert);
6342 if (hash_found) then
6343 hash_cite_confusion;
6344 end;@/
6345 entry_cite_ptr := cite_ptr;
6346 add_database_cite (cite_ptr); {this increments |cite_ptr|}
6347 cite_already_set:
6348 end
6349
6350
6351 @
6352 @^case mismatch errors@>
6353 @^commented-out code@>
6354 We must give a warning if this entry~type doesn't exist. Also, we
6355 point the appropriate entry of |type_list| to the entry type just read
6356 above.
6357
6358 For SCRIBE compatibility, the code to give a warning for a case
6359 mismatch between a cite key and a database key has been commented out.
6360 In fact, SCRIBE is the reason that it doesn't produce an error message
6361 outright. (Note: Case mismatches between two cite keys produce
6362 full-blown errors.)
6363
6364 @<Make sure this entry is ok before proceeding@>=
6365 begin
6366 @{
6367 dummy_loc := str_lookup(buffer,buf_ptr1,token_len,cite_ilk,dont_insert);
6368 if (not hash_found) then {give a warning if there is a case difference}
6369 begin
6370 print ('Warning--case mismatch, database key "');
6371 print_token;
6372 print ('", cite key "');
6373 print_pool_str (cite_list[entry_cite_ptr]);
6374 bib_warn_newline ('"');
6375 end;
6376 @}@/
6377 if (type_exists) then
6378 type_list[entry_cite_ptr] := entry_type_loc
6379 else
6380 begin
6381 type_list[entry_cite_ptr] := undefined;
6382 print ('Warning--entry type for "');
6383 print_token;
6384 bib_warn_newline ('" isn''t style-file defined');
6385 end;
6386 end
6387
6388
6389 @
6390 This module reads a |comma| and a field as many times as it can, and
6391 then reads a |right_outer_delim|, ending the current entry.
6392
6393 @<Scan the entry's list of fields@>=
6394 begin
6395 while (scan_char <> right_outer_delim) do
6396 begin
6397 if (scan_char <> comma) then
6398 bib_one_of_two_expected_err (comma,right_outer_delim);
6399 incr(buf_ptr2); {skip over the |comma|}
6400 eat_bib_white_and_eof_check;
6401 if (scan_char = right_outer_delim) then
6402 goto loop_exit;
6403 @<Get the next field name@>;
6404 eat_bib_white_and_eof_check;
6405 if (not scan_and_store_the_field_value_and_eat_white) then
6406 return;
6407 end;
6408 loop_exit:
6409 incr(buf_ptr2); {skip over the |right_outer_delim|}
6410 end
6411
6412
6413 @
6414 This module reads a field name; its contents won't be stored unless it
6415 was declared in the \.{.bst} file and |store_entry = true|.
6416
6417 @<Get the next field name@>=
6418 begin
6419 scan_identifier (equals_sign,equals_sign,equals_sign);
6420 bib_identifier_scan_check ('a field name');
6421 trace
6422 trace_pr_token;
6423 trace_pr_ln (' is a field name');
6424 ecart@/
6425 store_field := false;
6426 if (store_entry) then
6427 begin
6428 lower_case (buffer, buf_ptr1, token_len); {ignore case differences}
6429 field_name_loc := str_lookup(
6430 buffer,buf_ptr1,token_len,bst_fn_ilk,dont_insert);
6431 if (hash_found) then
6432 if (fn_type[field_name_loc]=field) then@/
6433 store_field := true; {field name was pre-defined or \.{.bst}-declared}
6434 end;
6435 eat_bib_white_and_eof_check;
6436 if (scan_char <> equals_sign) then
6437 bib_equals_sign_expected_err;
6438 incr(buf_ptr2); {skip over the |equals_sign|}
6439 end
6440
6441
6442 @
6443 This gets things ready for further \.{.bst} processing.
6444
6445 @<Final initialization for processing the entries@>=
6446 begin
6447 num_cites := cite_ptr; {to include database and \.{crossref} cite keys, too}
6448 num_preamble_strings := preamble_ptr; {number of \.{preamble} commands seen}
6449 @<Add cross-reference information@>;
6450 @<Subtract cross-reference information@>;
6451 @<Remove missing entries or those cross referenced too few times@>;
6452 @<Initialize the |int_entry_var|s@>;
6453 @<Initialize the |str_entry_var|s@>;
6454 @<Initialize the |sorted_cites|@>;
6455 end
6456
6457
6458 @
6459 @^child entry@>
6460 @^cross references@>
6461 @^nested cross references@>
6462 @^parent entry@>
6463 Now we update any entry (here called a {\it child\/} entry) that
6464 cross~referenced another (here called a {\it parent\/} entry); this
6465 cross~referencing occurs when the child's \.{crossref} field (value)
6466 consists of the parent's database key. To do the update, we replace
6467 the child's |missing| fields by the corresponding fields of the
6468 parent. Also, we make sure the \.{crossref} field contains the
6469 case-correct version. Finally, although it is technically illegal to
6470 nest cross~references, and although we give a warning (a few modules
6471 hence) when someone tries, we do what we can to accommodate the
6472 attempt.
6473
6474 @<Add cross-reference information@>=
6475 begin
6476 cite_ptr := 0;
6477 while (cite_ptr < num_cites) do
6478 begin
6479 field_ptr := cite_ptr * num_fields + crossref_num;
6480 if (field_info[field_ptr] <> missing) then
6481 if (find_cite_locs_for_this_cite_key (field_info[field_ptr])) then
6482 begin
6483 cite_loc := ilk_info[lc_cite_loc];
6484 field_info[field_ptr] := hash_text[cite_loc];
6485 cite_parent_ptr := ilk_info[cite_loc];
6486 field_ptr := cite_ptr * num_fields + num_pre_defined_fields;
6487 field_end_ptr := field_ptr - num_pre_defined_fields + num_fields;
6488 field_parent_ptr := cite_parent_ptr * num_fields
6489 + num_pre_defined_fields;
6490 while (field_ptr < field_end_ptr) do
6491 begin
6492 if (field_info[field_ptr] = missing) then
6493 field_info[field_ptr] := field_info[field_parent_ptr];
6494 incr(field_ptr);
6495 incr(field_parent_ptr);
6496 end;
6497 end;
6498 incr(cite_ptr);
6499 end;
6500 end
6501
6502
6503 @
6504 @^kludge@>
6505 @^raisin@>
6506 Occasionally we need to figure out the hash-table location of a given
6507 cite-key string and its lower-case equivalent. This function does
6508 that. To perform the task it needs to borrow a buffer, a need that
6509 gives rise to the alias kludge---it helps make the stack space not
6510 overflow on some machines (and while it's at it, it'll borrow a
6511 pointer, too). Finally, the function returns |true| if the cite key
6512 exists on |cite_list|, and its sets |cite_hash_found| according to
6513 whether or not it found the actual version (before |lower_case|ing) of
6514 the cite key; however, its {\sl raison d'\^$\mkern-8mu$etre\/}
6515 (literally, ``to eat a raisin'') is to compute |cite_loc| and
6516 |lc_cite_loc|.
6517
6518 @d ex_buf5 == ex_buf {aliases, used only}
6519 @d ex_buf5_ptr == ex_buf_ptr {in this module}
6520
6521 @<Procedures and functions for handling numbers, characters, and strings@>=
6522 function find_cite_locs_for_this_cite_key (@!cite_str : str_number) : boolean;
6523 begin
6524 ex_buf5_ptr := 0;
6525 tmp_ptr := str_start[cite_str];
6526 tmp_end_ptr := str_start[cite_str+1];
6527 while (tmp_ptr < tmp_end_ptr) do
6528 begin
6529 ex_buf5[ex_buf5_ptr] := str_pool[tmp_ptr];
6530 incr(ex_buf5_ptr);
6531 incr(tmp_ptr);
6532 end;
6533 cite_loc := str_lookup(ex_buf5,0,length(cite_str),cite_ilk,dont_insert);
6534 cite_hash_found := hash_found;
6535 lower_case (ex_buf5, 0, length(cite_str)); {convert to `canonical' form}
6536 lc_cite_loc := str_lookup(ex_buf5,0,length(cite_str),lc_cite_ilk,dont_insert);
6537 if (hash_found) then
6538 find_cite_locs_for_this_cite_key := true
6539 else
6540 find_cite_locs_for_this_cite_key := false;
6541 end;
6542
6543
6544 @
6545 @:this can't happen}{\quad Cite hash error@>
6546 Here we remove the \.{crossref} field value for each child whose
6547 parent was cross~referenced too few times. We also issue any
6548 necessary warnings arising from a bad cross~reference.
6549
6550 @<Subtract cross-reference information@>=
6551 begin
6552 cite_ptr := 0;
6553 while (cite_ptr < num_cites) do
6554 begin
6555 field_ptr := cite_ptr * num_fields + crossref_num;
6556 if (field_info[field_ptr] <> missing) then
6557 if (not find_cite_locs_for_this_cite_key (field_info[field_ptr])) then
6558 begin {the parent is not on |cite_list|}
6559 if (cite_hash_found) then
6560 hash_cite_confusion;
6561 nonexistent_cross_reference_error;
6562 field_info[field_ptr] := missing; {remove the \.{crossref} ptr}
6563 end
6564 else
6565 begin {the parent exists on |cite_list|}
6566 if (cite_loc <> ilk_info[lc_cite_loc]) then
6567 hash_cite_confusion;
6568 cite_parent_ptr := ilk_info[cite_loc];
6569 if (type_list[cite_parent_ptr] = empty) then
6570 begin
6571 nonexistent_cross_reference_error;@/
6572 field_info[field_ptr] := missing; {remove the \.{crossref} ptr}
6573 end
6574 else
6575 begin {the parent exists in the database too}
6576 field_parent_ptr := cite_parent_ptr * num_fields + crossref_num;
6577 if (field_info[field_parent_ptr] <> missing) then
6578 @<Complain about a nested cross reference@>;
6579 if ((not all_entries) and (cite_parent_ptr >= old_num_cites) and
6580 (cite_info[cite_parent_ptr] < min_crossrefs)) then@/
6581 field_info[field_ptr] := missing; {remove the \.{crossref} ptr}
6582 end;
6583 end;
6584 incr(cite_ptr);
6585 end;
6586 end
6587
6588
6589 @
6590 This procedure exists to save space, since it's used twice---once for
6591 each of the two succeeding modules.
6592
6593 @<Procedures and functions for all file I/O, error messages, and such@>=
6594 procedure bad_cross_reference_print (@!s:str_number);
6595 begin
6596 print ('--entry "');
6597 print_pool_str (cur_cite_str);
6598 print_ln ('"');
6599 print ('refers to entry "');
6600 print_pool_str (s);
6601 end;
6602
6603
6604 @
6605 When an entry being cross referenced doesn't exist on |cite_list|, we
6606 complain.
6607
6608 @<Procedures and functions for all file I/O, error messages, and such@>=
6609 procedure nonexistent_cross_reference_error;
6610 begin
6611 print ('A bad cross reference-');
6612 bad_cross_reference_print (field_info[field_ptr]);
6613 print_ln ('", which doesn''t exist');
6614 mark_error;
6615 end;
6616
6617
6618 @
6619 We also complain when an entry being cross referenced has a
6620 non|missing| \.{crossref} field itself, but this one is just a
6621 warning, not a full-blown error.
6622
6623 @<Complain about a nested cross reference@>=
6624 begin
6625 print ('Warning--you''ve nested cross references');
6626 bad_cross_reference_print (cite_list[cite_parent_ptr]);
6627 print_ln ('", which also refers to something');
6628 mark_warning;
6629 end
6630
6631
6632 @
6633 We remove (and give a warning for) each cite key on the original
6634 |cite_list| without a corresponding database entry. And we remove any
6635 entry that was included on |cite_list| only because it was
6636 cross~referenced, yet was cross~referenced fewer than |min_crossrefs|
6637 times. Throughout this module, |cite_ptr| points to the next cite key
6638 to be checked and |cite_xptr| points to the next permanent spot on
6639 |cite_list|.
6640
6641 @<Remove missing entries or those cross referenced too few times@>=
6642 begin
6643 cite_ptr := 0;
6644 while (cite_ptr < num_cites) do
6645 begin
6646 if (type_list[cite_ptr] = empty) then
6647 print_missing_entry (cur_cite_str)
6648 else if ((all_entries) or (cite_ptr < old_num_cites) or
6649 (cite_info[cite_ptr] >= min_crossrefs)) then
6650 begin
6651 if (cite_ptr > cite_xptr) then
6652 @<Slide this cite key down to its permanent spot@>;
6653 incr(cite_xptr);
6654 end;
6655 incr(cite_ptr);
6656 end;
6657 num_cites := cite_xptr;
6658 if (all_entries) then
6659 @<Complain about missing entries whose cite keys got overwritten@>;
6660 end
6661
6662
6663 @
6664 When a cite key on the original |cite_list| (or added to |cite_list|
6665 because of cross~referencing) didn't appear in the database, complain.
6666
6667 @<Procedures and functions for all file I/O, error messages, and such@>=
6668 procedure print_missing_entry (@!s:str_number);
6669 begin
6670 print ('Warning--I didn''t find a database entry for "');
6671 print_pool_str (s);
6672 print_ln ('"');
6673 mark_warning;
6674 end;
6675
6676
6677 @
6678 @:this can't happen}{\quad A cite key disappeared@>
6679 @:this can't happen}{\quad Cite hash error@>
6680 We have to move to its final resting place all the entry information
6681 associated with the exact location in |cite_list| of this cite key.
6682
6683 @<Slide this cite key down to its permanent spot@>=
6684 begin
6685 cite_list[cite_xptr] := cite_list[cite_ptr];
6686 type_list[cite_xptr] := type_list[cite_ptr];
6687 if (not find_cite_locs_for_this_cite_key (cite_list[cite_ptr])) then
6688 cite_key_disappeared_confusion;
6689 if ((not cite_hash_found) or (cite_loc <> ilk_info[lc_cite_loc])) then
6690 hash_cite_confusion;
6691 ilk_info[cite_loc] := cite_xptr;@/
6692 field_ptr := cite_xptr * num_fields;
6693 field_end_ptr := field_ptr + num_fields;
6694 tmp_ptr := cite_ptr * num_fields;
6695 while (field_ptr < field_end_ptr) do
6696 begin
6697 field_info[field_ptr] := field_info[tmp_ptr];
6698 incr(field_ptr);
6699 incr(tmp_ptr);
6700 end;
6701 end
6702
6703
6704 @
6705 We need this module only when we're including the whole database.
6706 It's for missing entries whose cite key originally resided in
6707 |cite_list| at a spot that another cite key (might have) claimed.
6708
6709 @<Complain about missing entries whose cite keys got overwritten@>=
6710 begin
6711 cite_ptr := all_marker;
6712 while (cite_ptr < old_num_cites) do
6713 begin
6714 if (not entry_exists[cite_ptr]) then
6715 print_missing_entry (cite_info[cite_ptr]);
6716 incr(cite_ptr);
6717 end;
6718 end
6719
6720
6721 @
6722 @:BibTeX capacity exceeded}{\quad total number of integer entry-variables@>
6723 This module initializes all |int_entry_var|s of all entries to 0, the
6724 value to which all integers are initialized.
6725
6726 @<Initialize the |int_entry_var|s@>=
6727 begin
6728 if (num_ent_ints*num_cites > max_ent_ints) then
6729 begin
6730 print (num_ent_ints*num_cites,': ');
6731 overflow('total number of integer entry-variables ',max_ent_ints);
6732 end;
6733 int_ent_ptr := 0;
6734 while (int_ent_ptr < num_ent_ints*num_cites) do
6735 begin
6736 entry_ints[int_ent_ptr] := 0;
6737 incr(int_ent_ptr);
6738 end;
6739 end
6740
6741
6742 @
6743 @:BibTeX capacity exceeded}{\quad total number of string entry-variables@>
6744 This module initializes all |str_entry_var|s of all entries to the
6745 null string, the value to which all strings are initialized.
6746
6747 @<Initialize the |str_entry_var|s@>=
6748 begin
6749 if (num_ent_strs*num_cites > max_ent_strs) then
6750 begin
6751 print (num_ent_strs*num_cites,': ');
6752 overflow('total number of string entry-variables ',max_ent_strs);
6753 end;
6754 str_ent_ptr := 0;
6755 while (str_ent_ptr < num_ent_strs*num_cites) do
6756 begin
6757 entry_strs[str_ent_ptr][0] := end_of_string;
6758 incr(str_ent_ptr);
6759 end;
6760 end
6761
6762
6763 @
6764 The array |sorted_cites| initially specifies that the entries are to
6765 be processed in order of cite-key occurrence. The \.{sort} command
6766 may change this to whatever it likes (which, we hope, is whatever the
6767 style-designer instructs it to like). We make |sorted_cites| an alias
6768 to save space; this works fine because we're done with |cite_info|.
6769
6770 @d sorted_cites == cite_info {an alias used for the rest of the program}
6771
6772 @<Initialize the |sorted_cites|@>=
6773 begin
6774 cite_ptr := 0;
6775 while (cite_ptr < num_cites) do
6776 begin
6777 sorted_cites[cite_ptr] := cite_ptr;
6778 incr(cite_ptr);
6779 end;
6780 end
6781
6782
6783
6784 @* Executing the style file.
6785 This part of the program produces the output by executing the
6786 \.{.bst}-file commands \.{execute}, \.{iterate}, \.{reverse}, and
6787 \.{sort}. To do this it uses a stack (consisting of the two arrays
6788 |lit_stack| and |lit_stk_type|) for storing literals, a buffer
6789 |ex_buf| for manipulating strings, and an array |sorted_cites|
6790 for holding pointers to the sorted cite keys (|sorted_cites| is an
6791 alias of |cite_info|).
6792
6793 @<Globals in the outer block@>=
6794 @!lit_stack : array[lit_stk_loc] of integer; {the literal function stack}
6795 @!lit_stk_type : array[lit_stk_loc] of stk_type; {their corresponding types}
6796 @!lit_stk_ptr : lit_stk_loc; {points just above the top of the stack}
6797 @!cmd_str_ptr : str_number; {stores value of |str_ptr| during execution}
6798 @!ent_chr_ptr : 0..ent_str_size; {points at a |str_entry_var| character}
6799 @!glob_chr_ptr : 0..glob_str_size; {points at a |str_global_var| character}
6800 @!ex_buf : buf_type; {a buffer for manipulating strings}
6801 @!ex_buf_ptr : buf_pointer; {general |ex_buf| location}
6802 @!ex_buf_length : buf_pointer; {the length of the current string in |ex_buf|}
6803 @!out_buf : buf_type; {the \.{.bbl} output buffer}
6804 @!out_buf_ptr : buf_pointer; {general |out_buf| location}
6805 @!out_buf_length : buf_pointer; {the length of the current string in |out_buf|}
6806 @!mess_with_entries : boolean; {|true| if functions can use entry info}
6807 @!sort_cite_ptr : cite_number; {a loop index for the sorted cite keys}
6808 @!sort_key_num : str_ent_loc; {index for the |str_entry_var| \.{sort.key\$}}
6809 @!brace_level : integer; {the brace nesting depth within a string}
6810
6811
6812 @
6813 Where |lit_stk_loc| is a stack location, and where |stk_type| gives
6814 one of the three types of literals (an integer, a string, or a
6815 function) or a special marker. If a |lit_stk_type| element is a
6816 |stk_int| then the corresponding |lit_stack| element is an integer; if
6817 a |stk_str|, then a pointer to a |str_pool| string; and if a |stk_fn|,
6818 then a pointer to the function's hash-table location. However, if the
6819 literal should have been a |stk_str| that was the value of a field
6820 that happened to be |missing|, then the special value
6821 |stk_field_missing| goes on the stack instead; its corresponding
6822 |lit_stack| element is a pointer to the field-name's string. Finally,
6823 |stk_empty| is the type of a literal popped from an empty stack.
6824
6825 @d stk_int = 0 {an integer literal}
6826 @d stk_str = 1 {a string literal}
6827 @d stk_fn = 2 {a function literal}
6828 @d stk_field_missing = 3 {a special marker: a field value was missing}
6829 @d stk_empty = 4 {another: the stack was empty when this was popped}
6830 @d last_lit_type = 4 {the same number as on the line above}
6831
6832 @<Types in the outer block@>=
6833 @!lit_stk_loc = 0..lit_stk_size; {the stack range}
6834 @!stk_type = 0..last_lit_type; {the literal types}
6835
6836
6837 @
6838 And the first output line requires this initialization.
6839
6840 @<Set initial values of key variables@>=
6841 out_buf_length := 0;
6842
6843
6844 @
6845 When there's an error while executing \.{.bst} functions, what we do
6846 depends on whether the function is messing with the entries.
6847 Furthermore this error is serious enough to classify as an
6848 |error_message| instead of a |warning_message|. These messages (that
6849 is, from |bst_ex_warn|) are meant both for the user and for the style
6850 designer while debugging.
6851
6852 @d bst_ex_warn(#) == begin {error while executing some function}
6853 print (#);
6854 bst_ex_warn_print;
6855 end
6856
6857 @<Procedures and functions for all file I/O, error messages, and such@>=
6858 procedure bst_ex_warn_print;
6859 begin
6860 if (mess_with_entries) then
6861 begin
6862 print (' for entry ');
6863 print_pool_str (cur_cite_str);
6864 end;
6865 print_newline;
6866 print ('while executing-');
6867 bst_ln_num_print;
6868 mark_error;
6869 end;
6870
6871
6872 @
6873 When an error is so harmless, we print a |warning_message| instead of
6874 an |error_message|.
6875
6876 @d bst_mild_ex_warn(#) == begin {error while executing some function}
6877 print (#);
6878 bst_mild_ex_warn_print;
6879 end
6880
6881 @<Procedures and functions for all file I/O, error messages, and such@>=
6882 procedure bst_mild_ex_warn_print;
6883 begin
6884 if (mess_with_entries) then
6885 begin
6886 print (' for entry ');
6887 print_pool_str (cur_cite_str);
6888 end;
6889 print_newline;
6890 bst_warn ('while executing'); {This does the |mark_warning|}
6891 end;
6892
6893
6894 @
6895 It's illegal to mess with the entry information at certain times;
6896 here's a complaint for these times.
6897
6898 @<Procedures and functions for all file I/O, error messages, and such@>=
6899 procedure bst_cant_mess_with_entries_print;
6900 begin
6901 bst_ex_warn ('You can''t mess with entries here');
6902 end;
6903
6904
6905 @
6906 This module executes a single specified function once. It can't do
6907 anything with the entries.
6908
6909 @<Perform an \.{execute} command@>=
6910 begin
6911 init_command_execution;
6912 mess_with_entries := false;
6913 execute_fn (fn_loc);
6914 check_command_execution;
6915 end
6916
6917
6918 @
6919 This module iterates a single specified function for all entries
6920 specified by |cite_list|.
6921
6922 @<Perform an \.{iterate} command@>=
6923 begin
6924 init_command_execution;
6925 mess_with_entries := true;
6926 sort_cite_ptr := 0;
6927 while (sort_cite_ptr < num_cites) do
6928 begin
6929 cite_ptr := sorted_cites[sort_cite_ptr];
6930 trace
6931 trace_pr_pool_str (hash_text[fn_loc]);
6932 trace_pr (' to be iterated on ');
6933 trace_pr_pool_str (cur_cite_str);
6934 trace_pr_newline;
6935 ecart@/
6936 execute_fn (fn_loc);
6937 check_command_execution;
6938 incr(sort_cite_ptr);
6939 end;
6940 end
6941
6942
6943 @
6944 This module iterates a single specified function for all entries
6945 specified by |cite_list|, but does it in reverse order.
6946
6947 @<Perform a \.{reverse} command@>=
6948 begin
6949 init_command_execution;
6950 mess_with_entries := true;
6951 if (num_cites > 0) then
6952 begin
6953 sort_cite_ptr := num_cites;
6954 repeat
6955 decr(sort_cite_ptr);
6956 cite_ptr := sorted_cites[sort_cite_ptr];
6957 trace
6958 trace_pr_pool_str (hash_text[fn_loc]);
6959 trace_pr (' to be iterated in reverse on ');
6960 trace_pr_pool_str (cur_cite_str);
6961 trace_pr_newline;
6962 ecart@/
6963 execute_fn (fn_loc);
6964 check_command_execution;
6965 until (sort_cite_ptr = 0);
6966 end;
6967 end
6968
6969
6970 @
6971 This module sorts the entries based on \.{sort.key\$}; it is a stable
6972 sort.
6973
6974 @<Perform a \.{sort} command@>=
6975 begin
6976 trace
6977 trace_pr_ln ('Sorting the entries');
6978 ecart@/
6979 if (num_cites > 1) then
6980 quick_sort (0, num_cites-1);
6981 trace
6982 trace_pr_ln ('Done sorting');
6983 ecart@/
6984 end
6985
6986
6987 @
6988 These next two procedures (actually, one procedures and one function,
6989 but who's counting) are subroutines for |quick_sort|, which follows.
6990 The |swap| procedure exchanges the two elements its arguments point
6991 to.
6992
6993 @<Procedures and functions for handling numbers, characters, and strings@>=
6994 procedure swap (@!swap1,@!swap2 : cite_number);
6995 var innocent_bystander : cite_number; {the temporary element in an exchange}
6996 begin
6997 innocent_bystander := sorted_cites[swap2];
6998 sorted_cites[swap2] := sorted_cites[swap1];
6999 sorted_cites[swap1] := innocent_bystander;
7000 end;
7001
7002
7003 @
7004 @:this can't happen}{\quad Duplicate sort key@>
7005 The function |less_than| compares the two \.{sort.key\$}s indirectly
7006 pointed to by its arguments and returns |true| if the first argument's
7007 \.{sort.key\$} is lexicographically less than the second's (that is,
7008 alphabetically earlier). In case of ties the function compares the
7009 indices |arg1| and |arg2|, which are assumed to be different, and
7010 returns |true| if the first is smaller. This function uses
7011 |ASCII_code|s to compare, so it might give ``interesting'' results
7012 when handling nonletters.
7013
7014 @d compare_return(#) == begin {the compare is finished}
7015 less_than := #;
7016 return;
7017 end
7018
7019 @<Procedures and functions for handling numbers, characters, and strings@>=
7020 function less_than (@!arg1,@!arg2 : cite_number) : boolean;
7021 label exit;
7022 var char_ptr : 0..ent_str_size; {character index into compared strings}
7023 @!ptr1,@!ptr2 : str_ent_loc; {the two \.{sort.key\$} pointers}
7024 @!char1,@!char2 : ASCII_code; {the two characters being compared}
7025 begin
7026 ptr1 := arg1*num_ent_strs + sort_key_num;
7027 ptr2 := arg2*num_ent_strs + sort_key_num;
7028 char_ptr := 0;
7029 loop
7030 begin
7031 char1 := entry_strs[ptr1][char_ptr];
7032 char2 := entry_strs[ptr2][char_ptr];
7033 if (char1 = end_of_string) then
7034 if (char2 = end_of_string) then
7035 if (arg1 < arg2) then
7036 compare_return (true)
7037 else if (arg1 > arg2) then
7038 compare_return (false)
7039 else {|arg1 = arg2|}
7040 confusion ('Duplicate sort key')
7041 else {|char2 <> end_of_string|}
7042 compare_return (true)
7043 else {|char1 <> end_of_string|}
7044 if (char2 = end_of_string) then
7045 compare_return (false)
7046 else if (char1 < char2) then
7047 compare_return (true)
7048 else if (char1 > char2) then
7049 compare_return (false);
7050 incr(char_ptr);
7051 end;
7052 exit:
7053 end;
7054
7055
7056 @
7057 The recursive procedure |quick_sort| sorts the entries indirectly
7058 pointed to by the |sorted_cites| elements between |left_end| and
7059 |right_end|, inclusive, based on the value of the |str_entry_var|
7060 \.{sort.key\$}. It's a fairly standard quicksort (for example, see
7061 Algorithm 5.2.2Q in {\sl The Art of Computer Programming}), but uses
7062 the median-of-three method to choose the partition element just in
7063 case the entries are already sorted (or nearly sorted---humans and
7064 ASCII might have different ideas on lexicographic ordering); it is a
7065 stable sort. This code generally prefers clarity to assembler-type
7066 execution-time efficiency since |cite_list|s will rarely be huge.
7067
7068 The value |short_list|, which must be at least |2*end_offset + 2| for
7069 this code to work, tells us the list-length at which the list is small
7070 enough to warrant switching over to straight insertion sort from the
7071 recursive quicksort. The values here come from modest empirical tests
7072 aimed at minimizing, for large |cite_list|s (five hundred or so), the
7073 number of comparisons (between keys) plus the number of calls to
7074 |quick_sort|. The value |end_offset| must be positive; this helps
7075 avoid $n^2$ behavior observed when the list starts out nearly, but not
7076 completely, sorted (and fairly frequently large |cite_list|s come from
7077 entire databases, which fairly frequently are nearly sorted).
7078
7079 @d short_list = 10 {use straight insertion sort at or below this length}
7080 @d end_offset = 4 {the index end-offsets for choosing a median-of-three}
7081
7082 @<Check the \(co)``constant'' values for consistency@>=
7083 if (short_list < 2*end_offset + 2) then bad:=100*bad+22;
7084
7085
7086 @
7087 Here's the actual procedure.
7088
7089 @d next_insert = 24 {now insert the next element}
7090
7091 @<Procedures and functions for handling numbers, characters, and strings@>=
7092 procedure quick_sort (@!left_end,@!right_end : cite_number);
7093 label next_insert;
7094 var left,@!right : cite_number; {two general |sorted_cites| pointers}
7095 @!insert_ptr : cite_number; {the to-be-(straight)-inserted element}
7096 @!middle : cite_number; {the |(left_end+right_end) div 2| element}
7097 @!partition : cite_number; {the median-of-three partition element}
7098 begin
7099 trace
7100 trace_pr_ln ('Sorting ',left_end:0,' through ',right_end:0);
7101 ecart@/
7102 if (right_end - left_end < short_list) then
7103 @<Do a straight insertion sort@>
7104 else
7105 begin
7106 @<Draw out the median-of-three partition element@>;
7107 @<Do the partitioning and the recursive calls@>;
7108 end;
7109 end;
7110
7111
7112 @
7113 This code sorts the entries between |left_end| and |right_end| when
7114 the difference is less than |short_list|. Each iteration of the outer
7115 loop inserts the element indicated by |insert_ptr| into its proper
7116 place among the (sorted) elements from |left_end| through
7117 |insert_ptr-1|.
7118
7119 @<Do a straight insertion sort@>=
7120 begin
7121 for insert_ptr := left_end+1 to right_end do
7122 begin
7123 for right := insert_ptr downto left_end+1 do
7124 begin
7125 if (less_than (sorted_cites[right-1], sorted_cites[right])) then
7126 goto next_insert;
7127 swap (right-1, right);
7128 end;
7129 next_insert:
7130 end;
7131 end
7132
7133
7134 @
7135 Now we find the median of the three \.{sort.key\$}s to which the three
7136 elements |sorted_cites[left_end+end_offset]|,
7137 |sorted_cites[right_end]-end_offset|, and
7138 |sorted_cites[(left_end+right_end) div 2]| point (a nonzero
7139 |end_offset| avoids using as the leftmost of the three elements the
7140 one that was swapped there when the old partition element was swapped
7141 into its final spot; this turns out to avoid $n^2$ behavior when the
7142 list is nearly sorted to start with). This code determines which of
7143 the six possible permutations we're dealing with and moves the median
7144 element to |left_end|. The comments next to the |swap| actions give
7145 the known orderings of the corresponding elements of |sorted_cites|
7146 before the action.
7147
7148 @<Draw out the median-of-three partition element@>=
7149 begin
7150 left := left_end + end_offset;
7151 middle := (left_end+right_end) div 2;
7152 right := right_end - end_offset;
7153 if (less_than (sorted_cites[left], sorted_cites[middle])) then
7154 if (less_than (sorted_cites[middle], sorted_cites[right])) then
7155 {|left < middle < right|}
7156 swap(left_end,middle)
7157 else if (less_than (sorted_cites[left], sorted_cites[right])) then
7158 {|left < right < middle|}
7159 swap(left_end,right)
7160 else {|right < left < middle|}
7161 swap(left_end,left)
7162 else if (less_than (sorted_cites[right], sorted_cites[middle])) then
7163 {|right < middle < left|}
7164 swap(left_end,middle)
7165 else if (less_than (sorted_cites[right], sorted_cites[left])) then
7166 {|middle < right < left|}
7167 swap(left_end,right)
7168 else {|middle < left < right|}
7169 swap(left_end,left);
7170 end
7171
7172
7173 @
7174 This module uses the median-of-three computed above to partition the
7175 elements into those less than and those greater than the median.
7176 Equal \.{sort.key\$}s are sorted by order of occurrence (in
7177 |cite_list|).
7178
7179 @<Do the partitioning and the recursive calls@>=
7180 begin
7181 partition := sorted_cites[left_end];
7182 left := left_end + 1;
7183 right := right_end;
7184 repeat
7185 while (less_than (sorted_cites[left], partition)) do
7186 incr(left);
7187 while (less_than (partition, sorted_cites[right])) do
7188 decr(right);
7189 {now |sorted_cites[right] < partition < sorted_cites[left]|}
7190 if (left < right) then
7191 begin
7192 swap (left,right);
7193 incr(left);
7194 decr(right);
7195 end;
7196 until (left = right+1); {pointers have crossed}
7197 swap (left_end,right);{restoring the partition element to its |right|ful place}
7198 quick_sort (left_end,right-1);
7199 quick_sort (left,right_end);
7200 end
7201
7202
7203 @
7204 @:BibTeX capacity exceeded}{\quad literal-stack size@>
7205 @:this can't happen}{\quad Unknown literal type@>
7206 Ok, that's it for sorting; now we'll play with the literal stack.
7207 This procedure pushes a literal onto the stack, checking for stack
7208 overflow.
7209
7210 @<Procedures and functions for style-file function execution@>=
7211 procedure push_lit_stk (@!push_lt:integer; @!push_type:stk_type);
7212 trace
7213 var dum_ptr : lit_stk_loc; {used just as an index variable}
7214 ecart@/
7215 begin
7216 lit_stack[lit_stk_ptr] := push_lt;
7217 lit_stk_type[lit_stk_ptr] := push_type;
7218 trace
7219 for dum_ptr := 0 to lit_stk_ptr do
7220 trace_pr (' ');
7221 trace_pr ('Pushing ');
7222 case (lit_stk_type[lit_stk_ptr]) of
7223 stk_int : trace_pr_ln (lit_stack[lit_stk_ptr]:0);
7224 stk_str : begin
7225 trace_pr ('"');
7226 trace_pr_pool_str (lit_stack[lit_stk_ptr]);
7227 trace_pr_ln ('"');
7228 end;
7229 stk_fn : begin
7230 trace_pr ('`');
7231 trace_pr_pool_str (hash_text[lit_stack[lit_stk_ptr]]);
7232 trace_pr_ln ('''');
7233 end;
7234 stk_field_missing : begin
7235 trace_pr ('missing field `');
7236 trace_pr_pool_str (lit_stack[lit_stk_ptr]);
7237 trace_pr_ln ('''');
7238 end;
7239 stk_empty : trace_pr_ln ('a bad literal--popped from an empty stack');
7240 othercases unknwn_literal_confusion
7241 endcases;
7242 ecart@/
7243 if (lit_stk_ptr = lit_stk_size) then
7244 overflow('literal-stack size ',lit_stk_size);
7245 incr(lit_stk_ptr);
7246 end;
7247
7248
7249 @
7250 @^push the literal stack@>
7251 This macro pushes the last thing, necessarily a string, that was
7252 popped. And this module, along with others that push the literal
7253 stack without explicitly calling |push_lit_stack|, have an index entry
7254 under ``push the literal stack''; these implicit pushes collectively
7255 speed up the program by about ten percent.
7256
7257 @d repush_string == begin
7258 if (lit_stack[lit_stk_ptr] >= cmd_str_ptr) then
7259 unflush_string;
7260 incr(lit_stk_ptr);
7261 end
7262
7263
7264 @
7265 @:this can't happen}{\quad Nontop top of string stack@>
7266 This procedure pops the stack, checking for, and trying to recover
7267 from, stack underflow. (Actually, this procedure is really a
7268 function, since it returns the two values through its |var|
7269 parameters.) Also, if the literal being popped is a |stk_str| that's
7270 been created during the execution of the current \.{.bst} command, pop
7271 it from |str_pool| as well (it will be the string corresponding to
7272 |str_ptr-1|). Note that when this happens, the string is no longer
7273 `officially' available so that it must be used before anything else is
7274 added to |str_pool|.
7275
7276 @<Procedures and functions for style-file function execution@>=
7277 procedure pop_lit_stk (var pop_lit:integer; var pop_type:stk_type);
7278 begin
7279 if (lit_stk_ptr = 0) then
7280 begin
7281 bst_ex_warn ('You can''t pop an empty literal stack');@/
7282 pop_type := stk_empty; {this is an error recovery attempt}
7283 end
7284 else
7285 begin
7286 decr(lit_stk_ptr);
7287 pop_lit := lit_stack[lit_stk_ptr];
7288 pop_type := lit_stk_type[lit_stk_ptr];
7289 if (pop_type = stk_str) then
7290 if (pop_lit >= cmd_str_ptr) then
7291 begin
7292 if (pop_lit <> str_ptr-1) then
7293 confusion ('Nontop top of string stack');
7294 flush_string;
7295 end;
7296 end;
7297 end;
7298
7299
7300 @
7301 @:this can't happen}{\quad Illegal literal type@>
7302 @:this can't happen}{\quad Unknown literal type@>
7303 More bug complaints, this time about bad literals.
7304
7305 @<Procedures and functions for all file I/O, error messages, and such@>=
7306 procedure illegl_literal_confusion;
7307 begin
7308 confusion ('Illegal literal type');
7309 end;
7310 @#
7311 procedure unknwn_literal_confusion;
7312 begin
7313 confusion ('Unknown literal type');
7314 end;
7315
7316
7317 @
7318 @:this can't happen}{\quad Illegal literal type@>
7319 @:this can't happen}{\quad Unknown literal type@>
7320 Occasionally we'll want to know what's on the literal stack. Here we
7321 print out a stack literal, giving its type. This procedure should
7322 never be called after popping an empty stack.
7323
7324 @<Procedures and functions for all file I/O, error messages, and such@>=
7325 procedure print_stk_lit (@!stk_lt:integer; @!stk_tp:stk_type);
7326 begin
7327 case (stk_tp) of
7328 stk_int : print (stk_lt:0,' is an integer literal');
7329 stk_str : begin
7330 print ('"');
7331 print_pool_str (stk_lt);
7332 print ('" is a string literal');
7333 end;
7334 stk_fn : begin
7335 print ('`');
7336 print_pool_str (hash_text[stk_lt]);
7337 print (''' is a function literal');
7338 end;
7339 stk_field_missing : begin
7340 print ('`');
7341 print_pool_str (stk_lt);
7342 print (''' is a missing field');
7343 end;
7344 stk_empty : illegl_literal_confusion;
7345 othercases unknwn_literal_confusion
7346 endcases;
7347 end;
7348
7349
7350 @
7351 @:this can't happen}{\quad Illegal literal type@>
7352 @:this can't happen}{\quad Unknown literal type@>
7353 This procedure appropriately chastises the style designer; however, if
7354 the wrong literal came from popping an empty stack, the procedure
7355 |pop_lit_stack| will have already done the chastising (because this
7356 procedure is called only after popping the stack) so there's no need
7357 for more.
7358
7359 @<Procedures and functions for style-file function execution@>=
7360 procedure print_wrong_stk_lit (@!stk_lt:integer; @!stk_tp1,@!stk_tp2:stk_type);
7361 begin
7362 if (stk_tp1 <> stk_empty) then
7363 begin
7364 print_stk_lit (stk_lt, stk_tp1);
7365 case (stk_tp2) of
7366 stk_int : print (', not an integer,');
7367 stk_str : print (', not a string,');
7368 stk_fn : print (', not a function,');
7369 stk_field_missing,
7370 stk_empty : illegl_literal_confusion;
7371 othercases unknwn_literal_confusion
7372 endcases;
7373 bst_ex_warn_print;
7374 end;
7375 end;
7376
7377
7378 @
7379 @:this can't happen}{\quad Illegal literal type@>
7380 @:this can't happen}{\quad Unknown literal type@>
7381 This is similar to |print_stk_lit|, but here we don't give the
7382 literal's type, and here we end with a new line. This procedure
7383 should never be called after popping an empty stack.
7384
7385 @<Procedures and functions for all file I/O, error messages, and such@>=
7386 procedure print_lit (@!stk_lt:integer; @!stk_tp:stk_type);
7387 begin
7388 case (stk_tp) of
7389 stk_int : print_ln (stk_lt:0);
7390 stk_str : begin
7391 print_pool_str (stk_lt);
7392 print_newline;
7393 end;
7394 stk_fn : begin
7395 print_pool_str (hash_text[stk_lt]);
7396 print_newline;
7397 end;
7398 stk_field_missing : begin
7399 print_pool_str (stk_lt);
7400 print_newline;
7401 end;
7402 stk_empty : illegl_literal_confusion;
7403 othercases unknwn_literal_confusion
7404 endcases;
7405 end;
7406
7407
7408 @
7409 This procedure pops and prints the top of the stack; when the stack is
7410 empty the procedure |pop_lit_stk| complains.
7411
7412 @<Procedures and functions for style-file function execution@>=
7413 procedure pop_top_and_print;
7414 var stk_lt : integer;
7415 @!stk_tp : stk_type;
7416 begin
7417 pop_lit_stk (stk_lt,stk_tp);
7418 if (stk_tp = stk_empty) then
7419 print_ln ('Empty literal')
7420 else
7421 print_lit (stk_lt,stk_tp);
7422 end;
7423
7424
7425 @
7426 This procedure pops and prints the whole stack.
7427
7428 @<Procedures and functions for style-file function execution@>=
7429 procedure pop_whole_stack;
7430 begin
7431 while (lit_stk_ptr > 0) do
7432 pop_top_and_print;
7433 end;
7434
7435
7436 @
7437 At the beginning of a \.{.bst}-command execution we make the stack
7438 empty and record how much of |str_pool| has been used.
7439
7440 @<Procedures and functions for style-file function execution@>=
7441 procedure init_command_execution;
7442 begin
7443 lit_stk_ptr := 0; {make the stack empty}
7444 cmd_str_ptr := str_ptr; {we'll check this when we finish command execution}
7445 end;
7446
7447
7448 @
7449 @:this can't happen}{\quad Nonempty empty string stack@>
7450 At the end of a \.{.bst} command-execution we check that the stack and
7451 |str_pool| are still in good shape.
7452
7453 @<Procedures and functions for style-file function execution@>=
7454 procedure check_command_execution;
7455 begin
7456 if (lit_stk_ptr<>0) then
7457 begin
7458 print_ln ('ptr=',lit_stk_ptr:0,', stack=');
7459 pop_whole_stack;
7460 bst_ex_warn ('---the literal stack isn''t empty');
7461 end;
7462 if (cmd_str_ptr<>str_ptr) then
7463 begin
7464 trace
7465 print_ln ('Pointer is ',str_ptr:0,' but should be ',cmd_str_ptr:0);
7466 ecart@/
7467 confusion ('Nonempty empty string stack');
7468 end;
7469 end;
7470
7471
7472 @
7473 This procedure adds to |str_pool| the string from |ex_buf[0]| through
7474 |ex_buf[ex_buf_length-1]| if it will fit. It assumes the global
7475 variable |ex_buf_length| gives the length of the current string in
7476 |ex_buf|. It then pushes this string onto the literal stack.
7477
7478 @<Procedures and functions for style-file function execution@>=
7479 procedure add_pool_buf_and_push;
7480 begin
7481 str_room (ex_buf_length); {make sure this string will fit}
7482 ex_buf_ptr := 0;
7483 while (ex_buf_ptr < ex_buf_length) do
7484 begin
7485 append_char (ex_buf[ex_buf_ptr]);
7486 incr(ex_buf_ptr);
7487 end;
7488 push_lit_stk (make_string, stk_str); {and push it onto the stack}
7489 end;
7490
7491
7492 @
7493 @:BibTeX capacity exceeded}{\quad buffer size@>
7494 These macros append a character to |ex_buf|. Which is called depends
7495 on whether the character is known to fit.
7496
7497 @d append_ex_buf_char(#) == begin
7498 ex_buf[ex_buf_ptr] := #;
7499 incr(ex_buf_ptr);
7500 end
7501 @#
7502 @d append_ex_buf_char_and_check(#) ==
7503 begin
7504 if (ex_buf_ptr = buf_size) then
7505 buffer_overflow;
7506 append_ex_buf_char(#);
7507 end
7508
7509
7510 @
7511 @:BibTeX capacity exceeded}{\quad buffer size@>
7512 This procedure adds to the execution buffer the given string in
7513 |str_pool| if it will fit. It assumes the global variable
7514 |ex_buf_length| gives the length of the current string in |ex_buf|,
7515 and thus also gives the location of the next character.
7516
7517 @<Procedures and functions for style-file function execution@>=
7518 procedure add_buf_pool (@!p_str : str_number);
7519 begin
7520 p_ptr1 := str_start[p_str];
7521 p_ptr2 := str_start[p_str+1];
7522 if (ex_buf_length+(p_ptr2-p_ptr1) > buf_size) then
7523 buffer_overflow;
7524 ex_buf_ptr := ex_buf_length;
7525 while (p_ptr1 < p_ptr2) do
7526 begin {copy characters into the buffer}
7527 append_ex_buf_char (str_pool[p_ptr1]);
7528 incr(p_ptr1);
7529 end;
7530 ex_buf_length := ex_buf_ptr;
7531 end;
7532
7533
7534 @
7535 This procedure actually writes onto the \.{.bbl}~file a line of output
7536 (the characters from |out_buf[0]| to |out_buf[out_buf_length-1]|,
7537 after removing trailing |white_space| characters). It also updates
7538 |bbl_line_num|, the line counter. It writes a blank line if and only
7539 if |out_buf| is empty. The program uses this procedure in such a way
7540 that |out_buf| will be nonempty if there have been characters put in
7541 it since the most recent \.{newline\$}.
7542
7543 @<Procedures and functions for all file I/O, error messages, and such@>=
7544 procedure output_bbl_line;
7545 label loop_exit,@!exit;
7546 begin
7547 if (out_buf_length <> 0) then {the buffer's not empty}
7548 begin
7549 while (out_buf_length > 0) do {remove trailing |white_space|}
7550 if (lex_class[out_buf[out_buf_length-1]] = white_space) then
7551 decr(out_buf_length)
7552 else
7553 goto loop_exit;
7554 loop_exit:
7555 if (out_buf_length = 0) then {ignore a line of just |white_space|}
7556 return;
7557 out_buf_ptr := 0;
7558 while (out_buf_ptr < out_buf_length) do
7559 begin
7560 write (bbl_file, xchr[out_buf[out_buf_ptr]]);
7561 incr(out_buf_ptr);
7562 end;
7563 end;
7564 write_ln (bbl_file);
7565 incr(bbl_line_num); {update line number}
7566 out_buf_length := 0; {make the next line empty}
7567 exit:
7568 end;
7569
7570
7571 @
7572 @:BibTeX capacity exceeded}{\quad output buffer size@>
7573 This procedure adds to the output buffer the given string in
7574 |str_pool|. It assumes the global variable |out_buf_length| gives the
7575 length of the current string in |out_buf|, and thus also gives the
7576 location for the next character. If there are enough characters
7577 present in the output buffer, it writes one or more lines out to the
7578 \.{.bbl} file. It breaks a line only at a |white_space| character,
7579 and when it does, it adds two |space|s to the next output line.
7580
7581 @<Procedures and functions for style-file function execution@>=
7582 procedure add_out_pool (@!p_str : str_number);
7583 label loop1_exit,loop2_exit;
7584 var break_ptr : buf_pointer; {the first character following the line break}
7585 @!end_ptr : buf_pointer; {temporary end-of-buffer pointer}
7586 break_pt_found : boolean; {a suitable |white_space| character}
7587 unbreakable_tail : boolean; {as it contains no |white_space| character}
7588 begin
7589 p_ptr1 := str_start[p_str];
7590 p_ptr2 := str_start[p_str+1];
7591 if (out_buf_length+(p_ptr2-p_ptr1) > buf_size) then
7592 overflow('output buffer size ',buf_size);
7593 out_buf_ptr := out_buf_length;
7594 while (p_ptr1 < p_ptr2) do
7595 begin {copy characters into the buffer}
7596 out_buf[out_buf_ptr] := str_pool[p_ptr1];
7597 incr(p_ptr1);
7598 incr(out_buf_ptr);
7599 end;
7600 out_buf_length := out_buf_ptr;
7601 unbreakable_tail := false;
7602 while ((out_buf_length > max_print_line) and (not unbreakable_tail)) do
7603 @<Break that line@>;
7604 end;
7605
7606
7607 @
7608 Here we break the line by looking for a |white_space| character,
7609 backwards from |out_buf[max_print_line]| until
7610 |out_buf[min_print_line]|; we break at the |white_space| and indent
7611 the next line two |space|s. The next module handles things when
7612 there's no |white_space| character to break at. (It seems that the
7613 annoyances to the average user of a warning message when there's an
7614 output line longer than |max_print_line| outweigh the benefits, so we
7615 don't issue such warnings in the current code.)
7616
7617 @<Break that line@>=
7618 begin
7619 end_ptr := out_buf_length;
7620 out_buf_ptr := max_print_line;
7621 break_pt_found := false;
7622 while ((lex_class[out_buf[out_buf_ptr]] <> white_space) and
7623 (out_buf_ptr >= min_print_line)) do
7624 decr(out_buf_ptr);
7625 if (out_buf_ptr = min_print_line-1) then {no |white_space| character}
7626 @<Break that unbreakably long line@> {(if |white_space| follows)}
7627 else
7628 break_pt_found := true; {hit a |white_space| character}
7629 if (break_pt_found) then
7630 begin
7631 out_buf_length := out_buf_ptr;
7632 break_ptr := out_buf_length + 1;
7633 output_bbl_line; {output what we can}
7634 out_buf[0] := space;
7635 out_buf[1] := space; {start the next line with two |space|s}
7636 out_buf_ptr := 2;
7637 tmp_ptr := break_ptr;
7638 while (tmp_ptr < end_ptr) do {and slide the rest down}
7639 begin
7640 out_buf[out_buf_ptr] := out_buf[tmp_ptr];
7641 incr(out_buf_ptr);
7642 incr(tmp_ptr);
7643 end;
7644 out_buf_length := end_ptr - break_ptr + 2;
7645 end;
7646 end
7647
7648
7649 @
7650 If there's no |white_space| character up through
7651 |out_buf[max_print_line]|, we instead break the line at the first
7652 following |white_space| character, if one exists. And if, starting
7653 with that |white_space| character, there are multiple consecutive
7654 |white_space| characters, |out_buf_ptr| points to the last of them.
7655 If no |white_space| character exists, we haven't found a viable break
7656 point, so we don't break the line (yet).
7657
7658 @<Break that unbreakably long line@>=
7659 begin
7660 out_buf_ptr := max_print_line + 1; {|break_pt_found| is still |false|}
7661 while (out_buf_ptr < end_ptr) do
7662 if (lex_class[out_buf[out_buf_ptr]] <> white_space) then
7663 incr(out_buf_ptr)
7664 else
7665 goto loop1_exit;
7666 loop1_exit:
7667 if (out_buf_ptr = end_ptr) then
7668 unbreakable_tail := true {because no |white_space| character}
7669 else {at |white_space|, and |out_buf_ptr < end_ptr|}
7670 begin
7671 break_pt_found := true;
7672 while (out_buf_ptr+1 < end_ptr) do {look for more |white_space|}
7673 if (lex_class[out_buf[out_buf_ptr+1]] = white_space) then
7674 incr(out_buf_ptr) {which then points to |white_space|}
7675 else
7676 goto loop2_exit;
7677 loop2_exit:
7678 end;
7679 end
7680
7681
7682 @
7683 @^Tuesdays@>
7684 @^windows@>
7685 @:this can't happen}{\quad Unknown function class@>
7686 This procedure executes a single specified function; it is the single
7687 execution-primitive that does everything (except windows, and it takes
7688 Tuesdays off).
7689
7690 @<|execute_fn| itself@>=
7691 procedure execute_fn (@!ex_fn_loc : hash_loc);
7692 @<Declarations for executing |built_in| functions@>
7693 @!wiz_ptr : wiz_fn_loc; {general |wiz_functions| location}
7694 begin
7695 trace
7696 trace_pr ('execute_fn `');
7697 trace_pr_pool_str (hash_text[ex_fn_loc]);
7698 trace_pr_ln ('''');
7699 ecart@/
7700 case (fn_type[ex_fn_loc]) of
7701 built_in : @<Execute a \(b)|built_in| function@>;
7702 wiz_defined : @<Execute a \(w)|wiz_defined| function@>;
7703 int_literal : push_lit_stk (fn_info[ex_fn_loc], stk_int);
7704 str_literal : push_lit_stk (hash_text[ex_fn_loc], stk_str);
7705 field : @<Execute a \(f)field@>;
7706 int_entry_var : @<Execute an |int_entry_var|@>;
7707 str_entry_var : @<Execute a \(s)|str_entry_var|@>;
7708 int_global_var : push_lit_stk (fn_info[ex_fn_loc], stk_int);
7709 str_global_var : @<Execute a \(s)|str_global_var|@>;
7710 othercases unknwn_function_class_confusion
7711 endcases;
7712 end;
7713
7714
7715 @
7716 To execute a |wiz_defined| function, we just execute all those
7717 functions in its definition, except that the special marker
7718 |quote_next_fn| means we push the next function onto the stack.
7719
7720 @<Execute a \(w)|wiz_defined| function@>=
7721 begin
7722 wiz_ptr := fn_info[ex_fn_loc];
7723 while (wiz_functions[wiz_ptr] <> end_of_def) do
7724 begin
7725 if (wiz_functions[wiz_ptr] <> quote_next_fn) then
7726 execute_fn (wiz_functions[wiz_ptr])
7727 else
7728 begin
7729 incr(wiz_ptr);
7730 push_lit_stk (wiz_functions[wiz_ptr], stk_fn);
7731 end;
7732 incr(wiz_ptr);
7733 end;
7734 end
7735
7736
7737 @
7738 This module pushes the string given by the field onto the literal
7739 stack unless it's |missing|, in which case it pushes a special value
7740 onto the stack.
7741
7742 @<Execute a \(f)field@>=
7743 begin
7744 if (not mess_with_entries) then
7745 bst_cant_mess_with_entries_print
7746 else
7747 begin
7748 field_ptr := cite_ptr*num_fields + fn_info[ex_fn_loc];
7749 if (field_info[field_ptr] = missing) then
7750 push_lit_stk (hash_text[ex_fn_loc], stk_field_missing)
7751 else
7752 push_lit_stk (field_info[field_ptr], stk_str);
7753 end
7754 end
7755
7756
7757 @
7758 This module pushes the integer given by an |int_entry_var| onto the
7759 literal stack.
7760
7761 @<Execute an |int_entry_var|@>=
7762 begin
7763 if (not mess_with_entries) then
7764 bst_cant_mess_with_entries_print
7765 else
7766 push_lit_stk (entry_ints[cite_ptr*num_ent_ints+fn_info[ex_fn_loc]],
7767 stk_int);
7768 end
7769
7770
7771 @
7772 This module adds the string given by a |str_entry_var| to |str_pool|
7773 via the execution buffer and pushes it onto the literal stack.
7774
7775 @<Execute a \(s)|str_entry_var|@>=
7776 begin
7777 if (not mess_with_entries) then
7778 bst_cant_mess_with_entries_print
7779 else
7780 begin
7781 str_ent_ptr := cite_ptr*num_ent_strs + fn_info[ex_fn_loc];@/
7782 ex_buf_ptr := 0; {also serves as |ent_chr_ptr|}
7783 while (entry_strs[str_ent_ptr][ex_buf_ptr] <> end_of_string) do
7784 {copy characters into the buffer}
7785 append_ex_buf_char (entry_strs[str_ent_ptr][ex_buf_ptr]);
7786 ex_buf_length := ex_buf_ptr;
7787 add_pool_buf_and_push; {push this string onto the stack}
7788 end;
7789 end
7790
7791
7792 @
7793 This module pushes the string given by a |str_global_var| onto the
7794 literal stack, but it copies the string to |str_pool| (character by
7795 character) only if it has to---it {\it doesn't\/} have to if the
7796 string is static (that is, if the string isn't at the top, temporary
7797 part of the string pool).
7798
7799 @<Execute a \(s)|str_global_var|@>=
7800 begin
7801 str_glb_ptr := fn_info[ex_fn_loc];
7802 if (glb_str_ptr[str_glb_ptr] > 0) then {we're dealing with a static string}
7803 push_lit_stk (glb_str_ptr[str_glb_ptr],stk_str)
7804 else
7805 begin
7806 str_room(glb_str_end[str_glb_ptr]);
7807 glob_chr_ptr := 0;
7808 while (glob_chr_ptr < glb_str_end[str_glb_ptr]) do {copy the string}
7809 begin
7810 append_char (global_strs[str_glb_ptr][glob_chr_ptr]);
7811 incr(glob_chr_ptr);
7812 end;
7813 push_lit_stk (make_string, stk_str); {and push it onto the stack}
7814 end;
7815 end
7816
7817
7818
7819 @* The built-in functions.
7820 @^add a built-in function@>
7821 @^biblical procreation@>
7822 @^grade inflation@>
7823 This section gives the all the code for all the built-in functions
7824 (including pre-defined |field|s, |str_entry_var|s, and
7825 |int_global_var|s, which technically aren't classified as |built_in|).
7826 To modify or add one, we needn't go anywhere else (with one exception:
7827 The constant |max_pop|, which gives the maximum number of literals
7828 that any of these functions pops off the stack, is defined earlier
7829 because it's needed earlier; thus, if we need to update it, which will
7830 happen if some new |built_in| functions uses more than |max_pop|
7831 literals from the stack, we'll have to go outside this section).
7832 Adding a |built_in| function entails modifying (at least four of) the
7833 five modules marked by ``add a built-in function'' in the index, in
7834 addition to adding the code to execute the function.
7835
7836 These variables all begin with |b_| and specify the hash-table
7837 locations of the |built_in| functions, except that |b_default| is
7838 pseudo-|built_in|---either it will point to the no-op \.{skip\$} or to
7839 the \.{.bst}-defined function \.{default.type}; it's used when an
7840 entry has a type that's not defined in the \.{.bst} file.
7841
7842 @<Globals in the outer block@>=
7843 @!b_equals : hash_loc; {\.{=}}
7844 @!b_greater_than : hash_loc; {\.{>}}
7845 @!b_less_than : hash_loc; {\.{<}}
7846 @!b_plus : hash_loc; {\.{+} (this may be changed to an |a_minus|)}
7847 @!b_minus : hash_loc; {\.{-}}
7848 @!b_concatenate : hash_loc; {\.{*}}
7849 @!b_gets : hash_loc; {\.{:=} (formerly, |b_gat|)}
7850 @!b_add_period : hash_loc; {\.{add.period\$}}
7851 @!b_call_type : hash_loc; {\.{call.type\$}}
7852 @!b_change_case : hash_loc; {\.{change.case\$}}
7853 @!b_chr_to_int : hash_loc; {\.{chr.to.int\$}}
7854 @!b_cite : hash_loc; {\.{cite\$}}
7855 @!b_duplicate : hash_loc; {\.{duplicate\$}}
7856 @!b_empty : hash_loc; {\.{empty\$}}
7857 @!b_format_name : hash_loc; {\.{format.name\$}}
7858 @!b_if : hash_loc; {\.{if\$}}
7859 @!b_int_to_chr : hash_loc; {\.{int.to.chr\$}}
7860 @!b_int_to_str : hash_loc; {\.{int.to.str\$}}
7861 @!b_missing : hash_loc; {\.{missing\$}}
7862 @!b_newline : hash_loc; {\.{newline\$}}
7863 @!b_num_names : hash_loc; {\.{num.names\$}}
7864 @!b_pop : hash_loc; {\.{pop\$}}
7865 @!b_preamble : hash_loc; {\.{preamble\$}}
7866 @!b_purify : hash_loc; {\.{purify\$}}
7867 @!b_quote : hash_loc; {\.{quote\$}}
7868 @!b_skip : hash_loc; {\.{skip\$}}
7869 @!b_stack : hash_loc; {\.{stack\$}}
7870 @!b_substring : hash_loc; {\.{substring\$}}
7871 @!b_swap : hash_loc; {\.{swap\$}}
7872 @!b_text_length : hash_loc; {\.{text.length\$}}
7873 @!b_text_prefix : hash_loc; {\.{text.prefix\$}}
7874 @!b_top_stack : hash_loc; {\.{top\$}}
7875 @!b_type : hash_loc; {\.{type\$}}
7876 @!b_warning : hash_loc; {\.{warning\$}}
7877 @!b_while : hash_loc; {\.{while\$}}
7878 @!b_width : hash_loc; {\.{width\$}}
7879 @!b_write : hash_loc; {\.{write\$}}
7880 @!b_default : hash_loc; {either \.{skip\$} or \.{default.type}}
7881 @#
7882 stat
7883 @!blt_in_loc : array[blt_in_range] of hash_loc; {for execution counts}
7884 @!execution_count : array[blt_in_range] of integer; {the same}
7885 @!total_ex_count : integer; {the sum of all |execution_count|s}
7886 @!blt_in_ptr : blt_in_range; {a pointer into |blt_in_loc|}
7887 tats@/
7888
7889
7890 @
7891 Where |blt_in_range| gives the legal |built_in| function numbers.
7892
7893 @<Types in the outer block@>=
7894 @!blt_in_range = 0..num_blt_in_fns;
7895
7896
7897 @
7898 @^add a built-in function@>
7899 These constants all begin with |n_| and are used for the |case|
7900 statement that determines which |built_in| function to execute.
7901
7902 @d n_equals = 0 {\.{=}}
7903 @d n_greater_than = 1 {\.{>}}
7904 @d n_less_than = 2 {\.{<}}
7905 @d n_plus = 3 {\.{+}}
7906 @d n_minus = 4 {\.{-}}
7907 @d n_concatenate = 5 {\.{*}}
7908 @d n_gets = 6 {\.{:=}}
7909 @d n_add_period = 7 {\.{add.period\$}}
7910 @d n_call_type = 8 {\.{call.type\$}}
7911 @d n_change_case = 9 {\.{change.case\$}}
7912 @d n_chr_to_int = 10 {\.{chr.to.int\$}}
7913 @d n_cite = 11 {\.{cite\$} (this may start a riot)}
7914 @d n_duplicate = 12 {\.{duplicate\$}}
7915 @d n_empty = 13 {\.{empty\$}}
7916 @d n_format_name = 14 {\.{format.name\$}}
7917 @d n_if = 15 {\.{if\$}}
7918 @d n_int_to_chr = 16 {\.{int.to.chr\$}}
7919 @d n_int_to_str = 17 {\.{int.to.str\$}}
7920 @d n_missing = 18 {\.{missing\$}}
7921 @d n_newline = 19 {\.{newline\$}}
7922 @d n_num_names = 20 {\.{num.names\$}}
7923 @d n_pop = 21 {\.{pop\$}}
7924 @d n_preamble = 22 {\.{preamble\$}}
7925 @d n_purify = 23 {\.{purify\$}}
7926 @d n_quote = 24 {\.{quote\$}}
7927 @d n_skip = 25 {\.{skip\$}}
7928 @d n_stack = 26 {\.{stack\$}}
7929 @d n_substring = 27 {\.{substring\$}}
7930 @d n_swap = 28 {\.{swap\$}}
7931 @d n_text_length = 29 {\.{text.length\$}}
7932 @d n_text_prefix = 30 {\.{text.prefix\$}}
7933 @d n_top_stack = 31 {\.{top\$}}
7934 @d n_type = 32 {\.{type\$}}
7935 @d n_warning = 33 {\.{warning\$}}
7936 @d n_while = 34 {\.{while\$}}
7937 @d n_width = 35 {\.{width\$}}
7938 @d n_write = 36 {\.{write\$}}
7939
7940 @<Constants in the outer block@>=
7941 @!num_blt_in_fns = 37; {one more than the previous number}
7942
7943
7944 @
7945 @^add a built-in function@>
7946 @^important note@>
7947 It's time for us to insert more pre-defined strings into |str_pool|
7948 (and thus the hash table) and to insert the |built_in| functions into
7949 the hash table. The strings corresponding to these functions should
7950 contain no upper-case letters, and they must all be exactly
7951 |longest_pds| characters long. The |build_in| routine (to appear
7952 shortly) does the work.
7953
7954 Important note: These pre-definitions must not have any glitches or the
7955 program may bomb because the |log_file| hasn't been opened yet.
7956
7957 @<Pre-define certain strings@>=
7958 build_in('= ',1,b_equals,n_equals);
7959 build_in('> ',1,b_greater_than,n_greater_than);
7960 build_in('< ',1,b_less_than,n_less_than);
7961 build_in('+ ',1,b_plus,n_plus);
7962 build_in('- ',1,b_minus,n_minus);
7963 build_in('* ',1,b_concatenate,n_concatenate);
7964 build_in(':= ',2,b_gets,n_gets);
7965 build_in('add.period$ ',11,b_add_period,n_add_period);
7966 build_in('call.type$ ',10,b_call_type,n_call_type);
7967 build_in('change.case$',12,b_change_case,n_change_case);
7968 build_in('chr.to.int$ ',11,b_chr_to_int,n_chr_to_int);
7969 build_in('cite$ ',5,b_cite,n_cite);
7970 build_in('duplicate$ ',10,b_duplicate,n_duplicate);
7971 build_in('empty$ ',6,b_empty,n_empty);
7972 build_in('format.name$',12,b_format_name,n_format_name);
7973 build_in('if$ ',3,b_if,n_if);
7974 build_in('int.to.chr$ ',11,b_int_to_chr,n_int_to_chr);
7975 build_in('int.to.str$ ',11,b_int_to_str,n_int_to_str);
7976 build_in('missing$ ',8,b_missing,n_missing);
7977 build_in('newline$ ',8,b_newline,n_newline);
7978 build_in('num.names$ ',10,b_num_names,n_num_names);
7979 build_in('pop$ ',4,b_pop,n_pop);
7980 build_in('preamble$ ',9,b_preamble,n_preamble);
7981 build_in('purify$ ',7,b_purify,n_purify);
7982 build_in('quote$ ',6,b_quote,n_quote);
7983 build_in('skip$ ',5,b_skip,n_skip);
7984 build_in('stack$ ',6,b_stack,n_stack);
7985 build_in('substring$ ',10,b_substring,n_substring);
7986 build_in('swap$ ',5,b_swap,n_swap);
7987 build_in('text.length$',12,b_text_length,n_text_length);
7988 build_in('text.prefix$',12,b_text_prefix,n_text_prefix);
7989 build_in('top$ ',4,b_top_stack,n_top_stack);
7990 build_in('type$ ',5,b_type,n_type);
7991 build_in('warning$ ',8,b_warning,n_warning);
7992 build_in('width$ ',6,b_width,n_width);
7993 build_in('while$ ',6,b_while,n_while);
7994 build_in('width$ ',6,b_width,n_width);
7995 build_in('write$ ',6,b_write,n_write);
7996
7997
7998 @
7999 This procedure inserts a |built_in| function into the hash table and
8000 initializes the corresponding pre-defined string (of length at most
8001 |longest_pds|). The array |fn_info| contains a number from 0 through
8002 the number of |built_in| functions minus 1 (i.e., |num_blt_in_fns - 1|
8003 if we're keeping statistics); this number is used by a |case|
8004 statement to execute this function and is used for keeping execution
8005 counts when keeping statistics.
8006
8007 @<Procedures and functions for handling numbers, characters, and strings@>=
8008 procedure build_in (@!pds:pds_type; @!len:pds_len; var fn_hash_loc:hash_loc;
8009 @!blt_in_num:blt_in_range);
8010 begin
8011 pre_define (pds,len,bst_fn_ilk);@/
8012 fn_hash_loc := pre_def_loc; {the |pre_define| routine sets |pre_def_loc|}
8013 fn_type[fn_hash_loc] := built_in;
8014 fn_info[fn_hash_loc] := blt_in_num;
8015 stat
8016 blt_in_loc[blt_in_num] := fn_hash_loc;@/
8017 execution_count[blt_in_num] := 0; {initialize the function-execution count}
8018 tats@/
8019 end;
8020
8021
8022 @
8023 This is a procedure so that |initialize| is smaller.
8024
8025 @<Procedures and functions for handling numbers, characters, and strings@>=
8026 procedure pre_def_certain_strings;
8027 begin
8028 @<Pre-define certain strings@>@;
8029 end;
8030
8031
8032 @
8033 These variables all begin with |s_| and specify the locations in
8034 |str_pool| of certain often-used strings that the \.{.bst} commands
8035 need. The |s_preamble| array is big enough to allow an average of one
8036 \.{preamble\$} command per \.{.bib} file.
8037
8038 @<Globals in the outer block@>=
8039 @!s_null : str_number; {the null string}
8040 @!s_default : str_number; {\.{default.type}, for unknown entry types}
8041 @!s_t : str_number; {\.{t}, for |title_lowers| case conversion}
8042 @!s_l : str_number; {\.{l}, for |all_lowers| case conversion}
8043 @!s_u : str_number; {\.{u}, for |all_uppers| case conversion}
8044 @!s_preamble : array[bib_number] of str_number;
8045 {for the \.{preamble\$} |built_in| function}
8046
8047
8048 @
8049 These constants all begin with |n_| and are used for the |case|
8050 statement that determines which, if any, control sequence we're
8051 dealing with; a control sequence of interest will be either one of the
8052 undotted characters `\.{\\i}' or `\.{\\j}' or one of the foreign
8053 characters in Table~3.2 of the \LaTeX\ manual.
8054
8055 @d n_i = 0 {\.{i}, for the undotted character \.{\\i}}
8056 @d n_j = 1 {\.{j}, for the undotted character \.{\\j}}
8057 @d n_oe = 2 {\.{oe}, for the foreign character \.{\\oe}}
8058 @d n_oe_upper = 3 {\.{OE}, for the foreign character \.{\\OE}}
8059 @d n_ae = 4 {\.{ae}, for the foreign character \.{\\ae}}
8060 @d n_ae_upper = 5 {\.{AE}, for the foreign character \.{\\AE}}
8061 @d n_aa = 6 {\.{aa}, for the foreign character \.{\\aa}}
8062 @d n_aa_upper = 7 {\.{AA}, for the foreign character \.{\\AA}}
8063 @d n_o = 8 {\.{o}, for the foreign character \.{\\o}}
8064 @d n_o_upper = 9 {\.{O}, for the foreign character \.{\\O}}
8065 @d n_l = 10 {\.{l}, for the foreign character \.{\\l}}
8066 @d n_l_upper = 11 {\.{L}, for the foreign character \.{\\L}}
8067 @d n_ss = 12 {\.{ss}, for the foreign character \.{\\ss}}
8068
8069
8070 @
8071 @^important note@>
8072 @.default.type@>
8073 Here we pre-define a few strings used in executing the \.{.bst} file:
8074 the null string, which is sometimes pushed onto the stack; a string
8075 used for default entry types; and some control sequences used to spot
8076 foreign characters. We also initialize the |s_preamble| array to
8077 empty. These pre-defined strings must all be exactly |longest_pds|
8078 characters long.
8079
8080 Important note: These pre-definitions must not have any glitches or
8081 the program may bomb because the |log_file| hasn't been opened yet,
8082 and |text_ilk|s should be pre-defined here, not earlier, for
8083 \.{.bst}-function-execution purposes.
8084
8085 @<Pre-define certain strings@>=
8086 pre_define(' ',0,text_ilk); s_null := hash_text[pre_def_loc];
8087 fn_type[pre_def_loc] := str_literal;@/
8088 pre_define('default.type',12,text_ilk); s_default := hash_text[pre_def_loc];
8089 fn_type[pre_def_loc] := str_literal;@/
8090 b_default := b_skip; {this may be changed to the \.{default.type} function}
8091 preamble_ptr := 0; {initialize the |s_preamble| array}
8092 pre_define('i ',1,control_seq_ilk);
8093 ilk_info[pre_def_loc] := n_i;
8094 pre_define('j ',1,control_seq_ilk);
8095 ilk_info[pre_def_loc] := n_j;
8096 pre_define('oe ',2,control_seq_ilk);
8097 ilk_info[pre_def_loc] := n_oe;
8098 pre_define('OE ',2,control_seq_ilk);
8099 ilk_info[pre_def_loc] := n_oe_upper;
8100 pre_define('ae ',2,control_seq_ilk);
8101 ilk_info[pre_def_loc] := n_ae;
8102 pre_define('AE ',2,control_seq_ilk);
8103 ilk_info[pre_def_loc] := n_ae_upper;
8104 pre_define('aa ',2,control_seq_ilk);
8105 ilk_info[pre_def_loc] := n_aa;
8106 pre_define('AA ',2,control_seq_ilk);
8107 ilk_info[pre_def_loc] := n_aa_upper;
8108 pre_define('o ',1,control_seq_ilk);
8109 ilk_info[pre_def_loc] := n_o;
8110 pre_define('O ',1,control_seq_ilk);
8111 ilk_info[pre_def_loc] := n_o_upper;
8112 pre_define('l ',1,control_seq_ilk);
8113 ilk_info[pre_def_loc] := n_l;
8114 pre_define('L ',1,control_seq_ilk);
8115 ilk_info[pre_def_loc] := n_l_upper;
8116 pre_define('ss ',2,control_seq_ilk);
8117 ilk_info[pre_def_loc] := n_ss;
8118
8119
8120 @
8121 @^important note@>
8122 @.crossref@>
8123 @.entry.max\$@>
8124 @.global.max\$@>
8125 @.sort.key\$@>
8126 Now we pre-define any built-in |field|s, |str_entry_var|s, and
8127 |int_global_var|s; these strings must all be exactly |longest_pds|
8128 characters long. Note that although these are built-in functions, we
8129 classify them (in the |fn_type| array) otherwise.
8130
8131 Important note: These pre-definitions must not have any glitches or
8132 the program may bomb because the |log_file| hasn't been opened yet.
8133
8134 @<Pre-define certain strings@>=
8135 pre_define('crossref ',8,bst_fn_ilk);
8136 fn_type[pre_def_loc] := field;@/
8137 fn_info[pre_def_loc] := num_fields; {give this |field| a number}
8138 crossref_num := num_fields;
8139 incr(num_fields);@/
8140 num_pre_defined_fields := num_fields; {that's it for pre-defined |field|s}
8141 pre_define('sort.key$ ',9,bst_fn_ilk);
8142 fn_type[pre_def_loc] := str_entry_var;
8143 fn_info[pre_def_loc] := num_ent_strs; {give this |str_entry_var| a number}
8144 sort_key_num := num_ent_strs;
8145 incr(num_ent_strs);@/
8146 pre_define('entry.max$ ',10,bst_fn_ilk);
8147 fn_type[pre_def_loc] := int_global_var;
8148 fn_info[pre_def_loc] := ent_str_size; {initialize this |int_global_var|}
8149 pre_define('global.max$ ',11,bst_fn_ilk);
8150 fn_type[pre_def_loc] := int_global_var;
8151 fn_info[pre_def_loc] := glob_str_size; {initialize this |int_global_var|}
8152
8153
8154 @
8155 @^add a built-in function@>
8156 @:this can't happen}{\quad Unknown built-in function@>
8157 This module branches to the code for the appropriate |built_in|
8158 function. Only three---{\.{call.type\$}}, {\.{if\$}}, and
8159 {\.{while\$}}---do a recursive call.
8160
8161 @<Execute a \(b)|built_in| function@>=
8162 begin
8163 stat {update this function's execution count}
8164 incr(execution_count[fn_info[ex_fn_loc]]);
8165 tats@/
8166 case (fn_info[ex_fn_loc]) of
8167 n_equals : x_equals;
8168 n_greater_than : x_greater_than;
8169 n_less_than : x_less_than;
8170 n_plus : x_plus;
8171 n_minus : x_minus;
8172 n_concatenate : x_concatenate;
8173 n_gets : x_gets;
8174 n_add_period : x_add_period;
8175 n_call_type : @<|execute_fn|({\.{call.type\$}})@>;
8176 n_change_case : x_change_case;
8177 n_chr_to_int : x_chr_to_int;
8178 n_cite : x_cite;
8179 n_duplicate : x_duplicate;
8180 n_empty : x_empty;
8181 n_format_name : x_format_name;
8182 n_if : @<|execute_fn|({\.{if\$}})@>;
8183 n_int_to_chr : x_int_to_chr;
8184 n_int_to_str : x_int_to_str;
8185 n_missing : x_missing;
8186 n_newline : @<|execute_fn|({\.{newline\$}})@>;
8187 n_num_names : x_num_names;
8188 n_pop : @<|execute_fn|({\.{pop\$}})@>;
8189 n_preamble : x_preamble;
8190 n_purify : x_purify;
8191 n_quote : x_quote;
8192 n_skip : @<|execute_fn|({\.{skip\$}})@>;
8193 n_stack : @<|execute_fn|({\.{stack\$}})@>;
8194 n_substring : x_substring;
8195 n_swap : x_swap;
8196 n_text_length : x_text_length;
8197 n_text_prefix : x_text_prefix;
8198 n_top_stack : @<|execute_fn|({\.{top\$}})@>;
8199 n_type : x_type;
8200 n_warning : x_warning;
8201 n_while : @<|execute_fn|({\.{while\$}})@>;
8202 n_width : x_width;
8203 n_write : x_write;
8204 othercases confusion ('Unknown built-in function')
8205 endcases;
8206 end
8207
8208
8209 @
8210 @^add a built-in function@>
8211 @^gymnastics@>
8212 This extra level of module-pointing allows a uniformity of module
8213 names for the |built_in| functions, regardless of whether they do a
8214 recursive call to |execute_fn| or are trivial (a single statement).
8215 Those that do a recursive call are left as part of |execute_fn|,
8216 avoiding \PASCAL's forward procedure mechanism, and those that don't
8217 (except for the single-statement ones) are made into procedures so
8218 that |execute_fn| doesn't get too large.
8219
8220 @<Procedures and functions for style-file function execution@>=
8221 @<|execute_fn|({\.{=}})@>@;
8222 @<|execute_fn|({\.{>}})@>@;
8223 @<|execute_fn|({\.{<}})@>@;
8224 @<|execute_fn|({\.{+}})@>@;
8225 @<|execute_fn|({\.{-}})@>@;
8226 @<|execute_fn|({\.{*}})@>@;
8227 @<|execute_fn|({\.{:=}})@>@;
8228 @<|execute_fn|({\.{add.period\$}})@>@;
8229 @<|execute_fn|({\.{change.case\$}})@>@;
8230 @<|execute_fn|({\.{chr.to.int\$}})@>@;
8231 @<|execute_fn|({\.{cite\$}})@>@;
8232 @<|execute_fn|({\.{duplicate\$}})@>@;
8233 @<|execute_fn|({\.{empty\$}})@>@;
8234 @<|execute_fn|({\.{format.name\$}})@>@;
8235 @<|execute_fn|({\.{int.to.chr\$}})@>@;
8236 @<|execute_fn|({\.{int.to.str\$}})@>@;
8237 @<|execute_fn|({\.{missing\$}})@>@;
8238 @<|execute_fn|({\.{num.names\$}})@>@;
8239 @<|execute_fn|({\.{preamble\$}})@>@;
8240 @<|execute_fn|({\.{purify\$}})@>@;
8241 @<|execute_fn|({\.{quote\$}})@>@;
8242 @<|execute_fn|({\.{substring\$}})@>@;
8243 @<|execute_fn|({\.{swap\$}})@>@;
8244 @<|execute_fn|({\.{text.length\$}})@>@;
8245 @<|execute_fn|({\.{text.prefix\$}})@>@;
8246 @<|execute_fn|({\.{type\$}})@>@;
8247 @<|execute_fn|({\.{warning\$}})@>@;
8248 @<|execute_fn|({\.{width\$}})@>@;
8249 @<|execute_fn|({\.{write\$}})@>@;
8250 @<|execute_fn| itself@>
8251
8252
8253 @
8254 Now it's time to declare some things for executing |built_in|
8255 functions only. These (and only these) variables are used
8256 recursively, so they can't be global.
8257
8258 @d end_while = 51 {stop executing the \.{while\$} function}
8259
8260 @<Declarations for executing |built_in| functions@>=
8261 label end_while;
8262 var r_pop_lt1,@!r_pop_lt2 : integer; {stack literals for \.{while\$}}
8263 @!r_pop_tp1,@!r_pop_tp2 : stk_type; {stack types for \.{while\$}}
8264
8265
8266 @
8267 These are nonrecursive variables that |execute_fn| uses. Declaring
8268 them here (instead of in the previous module) saves execution time and
8269 stack space on most machines.
8270
8271 @d name_buf == sv_buffer {an alias, a buffer for manipulating names}
8272
8273 @<Globals in the outer block@>=
8274 @!pop_lit1,@!pop_lit2,@!pop_lit3 : integer; {stack literals}
8275 @!pop_typ1,@!pop_typ2,@!pop_typ3 : stk_type; {stack types}
8276 @!sp_ptr : pool_pointer; {for manipulating |str_pool| strings}
8277 @!sp_xptr1,@!sp_xptr2 : pool_pointer; {more of the same}
8278 @!sp_end : pool_pointer; {marks the end of a |str_pool| string}
8279 @!sp_length,sp2_length : pool_pointer; {lengths of |str_pool| strings}
8280 @!sp_brace_level : integer; {for scanning |str_pool| strings}
8281 @!ex_buf_xptr,@!ex_buf_yptr : buf_pointer; {extra |ex_buf| locations}
8282 @!control_seq_loc : hash_loc; {hash-table loc of a control sequence}
8283 @!preceding_white : boolean; {used in scanning strings}
8284 @!and_found : boolean; {to stop the loop that looks for an ``and''}
8285 @!num_names : integer; {for counting names}
8286 @!name_bf_ptr : buf_pointer; {general |name_buf| location}
8287 @!name_bf_xptr,@!name_bf_yptr : buf_pointer; {and two more}
8288 @!nm_brace_level : integer; {for scanning |name_buf| strings}
8289 @!name_tok : packed array[buf_pointer] of buf_pointer; {name-token ptr list}
8290 @!name_sep_char : packed array[buf_pointer] of ASCII_code; {token-ending chars}
8291 @!num_tokens : buf_pointer; {this counts name tokens}
8292 @!token_starting : boolean; {used in scanning name tokens}
8293 @!alpha_found : boolean; {used in scanning the format string}
8294 @!double_letter,@!end_of_group,@!to_be_written : boolean; {the same}
8295 @!first_start : buf_pointer; {start-ptr into |name_tok| for the first name}
8296 @!first_end : buf_pointer; {end-ptr into |name_tok| for the first name}
8297 @!last_end : buf_pointer; {end-ptr into |name_tok| for the last name}
8298 @!von_start : buf_pointer; {start-ptr into |name_tok| for the von name}
8299 @!von_end : buf_pointer; {end-ptr into |name_tok| for the von name}
8300 @!jr_end : buf_pointer; {end-ptr into |name_tok| for the jr name}
8301 @!cur_token,@!last_token : buf_pointer; {|name_tok| ptrs for outputting tokens}
8302 @!use_default : boolean; {for the inter-token intra-name part string}
8303 @!num_commas : buf_pointer; {used to determine the name syntax}
8304 @!comma1,@!comma2 : buf_pointer; {ptrs into |name_tok|}
8305 @!num_text_chars : buf_pointer; {special characters count as one}
8306
8307
8308 @
8309 The |built_in| function {\.{=}} pops the top two (integer or string)
8310 literals, compares them, and pushes the integer 1 if they're equal, 0
8311 otherwise. If they're not either both string or both integer, it
8312 complains and pushes the integer 0.
8313
8314 @<|execute_fn|({\.{=}})@>=
8315 procedure x_equals;
8316 begin
8317 pop_lit_stk (pop_lit1,pop_typ1);
8318 pop_lit_stk (pop_lit2,pop_typ2);
8319 if (pop_typ1 <> pop_typ2) then
8320 begin
8321 if ((pop_typ1 <> stk_empty) and (pop_typ2 <> stk_empty)) then
8322 begin
8323 print_stk_lit (pop_lit1,pop_typ1);
8324 print (', ');
8325 print_stk_lit (pop_lit2,pop_typ2);
8326 print_newline;
8327 bst_ex_warn ('---they aren''t the same literal types');
8328 end;
8329 push_lit_stk (0, stk_int);
8330 end
8331 else if ((pop_typ1 <> stk_int) and (pop_typ1 <> stk_str)) then
8332 begin
8333 if (pop_typ1 <> stk_empty) then
8334 begin
8335 print_stk_lit (pop_lit1,pop_typ1);
8336 bst_ex_warn (', not an integer or a string,');
8337 end;
8338 push_lit_stk (0, stk_int);
8339 end
8340 else if (pop_typ1 = stk_int) then
8341 if (pop_lit2 = pop_lit1) then
8342 push_lit_stk (1, stk_int)
8343 else
8344 push_lit_stk (0, stk_int)
8345 else
8346 if (str_eq_str (pop_lit2,pop_lit1)) then
8347 push_lit_stk (1, stk_int)
8348 else
8349 push_lit_stk (0, stk_int);
8350 end;
8351
8352
8353 @
8354 The |built_in| function {\.{>}} pops the top two (integer) literals,
8355 compares them, and pushes the integer 1 if the second is greater than
8356 the first, 0 otherwise. If either isn't an integer literal, it
8357 complains and pushes the integer 0.
8358
8359 @<|execute_fn|({\.{>}})@>=
8360 procedure x_greater_than;
8361 begin
8362 pop_lit_stk (pop_lit1,pop_typ1);
8363 pop_lit_stk (pop_lit2,pop_typ2);
8364 if (pop_typ1 <> stk_int) then
8365 begin
8366 print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
8367 push_lit_stk (0, stk_int);
8368 end
8369 else if (pop_typ2 <> stk_int) then
8370 begin
8371 print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int);
8372 push_lit_stk (0, stk_int);
8373 end
8374 else
8375 if (pop_lit2 > pop_lit1) then
8376 push_lit_stk (1, stk_int)
8377 else
8378 push_lit_stk (0, stk_int);
8379 end;
8380
8381
8382 @
8383 The |built_in| function {\.{<}} pops the top two (integer) literals,
8384 compares them, and pushes the integer 1 if the second is less than the
8385 first, 0 otherwise. If either isn't an integer literal, it complains
8386 and pushes the integer 0.
8387
8388 @<|execute_fn|({\.{<}})@>=
8389 procedure x_less_than;
8390 begin
8391 pop_lit_stk (pop_lit1,pop_typ1);
8392 pop_lit_stk (pop_lit2,pop_typ2);
8393 if (pop_typ1 <> stk_int) then
8394 begin
8395 print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
8396 push_lit_stk (0, stk_int);
8397 end
8398 else if (pop_typ2 <> stk_int) then
8399 begin
8400 print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int);
8401 push_lit_stk (0, stk_int);
8402 end
8403 else
8404 if (pop_lit2 < pop_lit1) then
8405 push_lit_stk (1, stk_int)
8406 else
8407 push_lit_stk (0, stk_int);
8408 end;
8409
8410
8411 @
8412 The |built_in| function {\.{+}} pops the top two (integer) literals
8413 and pushes their sum. If either isn't an integer literal, it
8414 complains and pushes the integer 0.
8415
8416 @<|execute_fn|({\.{+}})@>=
8417 procedure x_plus;
8418 begin
8419 pop_lit_stk (pop_lit1,pop_typ1);
8420 pop_lit_stk (pop_lit2,pop_typ2);
8421 if (pop_typ1 <> stk_int) then
8422 begin
8423 print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
8424 push_lit_stk (0, stk_int);
8425 end
8426 else if (pop_typ2 <> stk_int) then
8427 begin
8428 print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int);
8429 push_lit_stk (0, stk_int);
8430 end
8431 else
8432 push_lit_stk (pop_lit2+pop_lit1, stk_int);
8433 end;
8434
8435
8436 @
8437 The |built_in| function {\.{-}} pops the top two (integer) literals
8438 and pushes their difference (the first subtracted from the second).
8439 If either isn't an integer literal, it complains and pushes the
8440 integer 0.
8441
8442 @<|execute_fn|({\.{-}})@>=
8443 procedure x_minus;
8444 begin
8445 pop_lit_stk (pop_lit1,pop_typ1);
8446 pop_lit_stk (pop_lit2,pop_typ2);
8447 if (pop_typ1 <> stk_int) then
8448 begin
8449 print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
8450 push_lit_stk (0, stk_int);
8451 end
8452 else if (pop_typ2 <> stk_int) then
8453 begin
8454 print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int);
8455 push_lit_stk (0, stk_int);
8456 end
8457 else
8458 push_lit_stk (pop_lit2-pop_lit1, stk_int);
8459 end;
8460
8461
8462 @
8463 The |built_in| function {\.{*}} pops the top two (string) literals,
8464 concatenates them (in reverse order, that is, the order in which
8465 pushed), and pushes the resulting string back onto the stack. If
8466 either isn't a string literal, it complains and pushes the null
8467 string.
8468
8469 @<|execute_fn|({\.{*}})@>=
8470 procedure x_concatenate;
8471 begin
8472 pop_lit_stk (pop_lit1,pop_typ1);
8473 pop_lit_stk (pop_lit2,pop_typ2);
8474 if (pop_typ1 <> stk_str) then
8475 begin
8476 print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
8477 push_lit_stk (s_null, stk_str);
8478 end
8479 else if (pop_typ2 <> stk_str) then
8480 begin
8481 print_wrong_stk_lit (pop_lit2,pop_typ2,stk_str);
8482 push_lit_stk (s_null, stk_str);
8483 end
8484 else
8485 @<Concatenate the two strings and push@>;
8486 end;
8487
8488
8489 @
8490 @^push the literal stack@>
8491 Often both strings will be at the top of the string pool, in which
8492 case we just move some pointers. Furthermore, it's worth doing some
8493 special stuff in case either string is null, since empirically this
8494 seems to happen about $20\%$ of the time. In any case, we don't need
8495 the execution buffer---we simple move the strings around in the string
8496 pool when necessary.
8497
8498 @<Concatenate the two strings and push@>=
8499 begin
8500 if (pop_lit2 >= cmd_str_ptr) then
8501 if (pop_lit1 >= cmd_str_ptr) then
8502 begin
8503 str_start[pop_lit1] := str_start[pop_lit1+1];
8504 unflush_string;
8505 incr(lit_stk_ptr);
8506 end
8507 else if (length(pop_lit2) = 0) then
8508 push_lit_stk (pop_lit1, stk_str)
8509 else {|pop_lit2| is nonnull, only |pop_lit1| is below |cmd_str_ptr|}
8510 begin
8511 pool_ptr := str_start[pop_lit2+1];
8512 str_room (length(pop_lit1));
8513 sp_ptr := str_start[pop_lit1];
8514 sp_end := str_start[pop_lit1+1];
8515 while (sp_ptr < sp_end) do
8516 begin
8517 append_char (str_pool[sp_ptr]);
8518 incr(sp_ptr);
8519 end;
8520 push_lit_stk (make_string, stk_str); {and push it onto the stack}
8521 end
8522 else
8523 @<Concatenate them and push when |pop_lit2 < cmd_str_ptr|@>;
8524 end
8525
8526
8527 @
8528 @^push the literal stack@>
8529 We simply continue the previous module.
8530
8531 @<Concatenate them and push when |pop_lit2 < cmd_str_ptr|@>=
8532 begin
8533 if (pop_lit1 >= cmd_str_ptr) then
8534 if (length(pop_lit2) = 0) then
8535 begin
8536 unflush_string;
8537 lit_stack[lit_stk_ptr] := pop_lit1;
8538 incr(lit_stk_ptr);
8539 end
8540 else if (length(pop_lit1) = 0) then
8541 incr(lit_stk_ptr)
8542 else {both strings nonnull, only |pop_lit2| is below |cmd_str_ptr|}
8543 begin
8544 sp_length := length(pop_lit1);
8545 sp2_length := length(pop_lit2);
8546 str_room (sp_length + sp2_length);
8547 sp_ptr := str_start[pop_lit1+1];
8548 sp_end := str_start[pop_lit1];
8549 sp_xptr1 := sp_ptr + sp2_length;
8550 while (sp_ptr > sp_end) do {slide up |pop_lit1|}
8551 begin
8552 decr(sp_ptr);
8553 decr(sp_xptr1);
8554 str_pool[sp_xptr1] := str_pool[sp_ptr];
8555 end;
8556 sp_ptr := str_start[pop_lit2];
8557 sp_end := str_start[pop_lit2+1];
8558 while (sp_ptr < sp_end) do {slide up |pop_lit2|}
8559 begin
8560 append_char (str_pool[sp_ptr]);
8561 incr(sp_ptr);
8562 end;
8563 pool_ptr := pool_ptr + sp_length;
8564 push_lit_stk (make_string, stk_str); {and push it onto the stack}
8565 end
8566 else
8567 @<Concatenate them and push when |pop_lit1,pop_lit2 < cmd_str_ptr|@>;
8568 end
8569
8570
8571 @
8572 @^push the literal stack@>
8573 Again, we simply continue the previous module.
8574
8575 @<Concatenate them and push when |pop_lit1,pop_lit2 < cmd_str_ptr|@>=
8576 begin
8577 if (length(pop_lit1) = 0) then
8578 incr(lit_stk_ptr)
8579 else if (length(pop_lit2) = 0) then
8580 push_lit_stk (pop_lit1, stk_str)
8581 else {both strings are nonnull, and both are below |cmd_str_ptr|}
8582 begin
8583 str_room (length(pop_lit1) + length(pop_lit2));
8584 sp_ptr := str_start[pop_lit2];
8585 sp_end := str_start[pop_lit2+1];
8586 while (sp_ptr < sp_end) do {slide up |pop_lit2|}
8587 begin
8588 append_char (str_pool[sp_ptr]);
8589 incr(sp_ptr);
8590 end;
8591 sp_ptr := str_start[pop_lit1];
8592 sp_end := str_start[pop_lit1+1];
8593 while (sp_ptr < sp_end) do {slide up |pop_lit1|}
8594 begin
8595 append_char (str_pool[sp_ptr]);
8596 incr(sp_ptr);
8597 end;
8598 push_lit_stk (make_string, stk_str); {and push it onto the stack}
8599 end;
8600 end
8601
8602
8603 @
8604 The |built_in| function {\.{:=}} pops the top two literals and assigns
8605 to the first (which must be an |int_entry_var|, a |str_entry_var|, an
8606 |int_global_var|, or a |str_global_var|) the value of the second;
8607 it complains if the value isn't of the appropriate type.
8608
8609 @<|execute_fn|({\.{:=}})@>=
8610 procedure x_gets;
8611 begin
8612 pop_lit_stk (pop_lit1,pop_typ1);
8613 pop_lit_stk (pop_lit2,pop_typ2);
8614 if (pop_typ1 <> stk_fn) then
8615 print_wrong_stk_lit (pop_lit1,pop_typ1,stk_fn)
8616 else if ((not mess_with_entries) and
8617 ((fn_type[pop_lit1] = str_entry_var) or
8618 (fn_type[pop_lit1] = int_entry_var))) then
8619 bst_cant_mess_with_entries_print
8620 else
8621 case (fn_type[pop_lit1]) of
8622 int_entry_var : @<Assign to an |int_entry_var|@>;
8623 str_entry_var : @<Assign to a |str_entry_var|@>;
8624 int_global_var : @<Assign to an |int_global_var|@>;
8625 str_global_var : @<Assign to a |str_global_var|@>;
8626 othercases begin
8627 print ('You can''t assign to type ');
8628 print_fn_class (pop_lit1);
8629 bst_ex_warn (', a nonvariable function class');
8630 end
8631 endcases;
8632 end;
8633
8634
8635 @
8636 This module checks that what we're about to assign is really an
8637 integer, and then assigns.
8638
8639 @<Assign to an |int_entry_var|@>=
8640 if (pop_typ2 <> stk_int) then
8641 print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int)
8642 else
8643 entry_ints[cite_ptr*num_ent_ints+fn_info[pop_lit1]] := pop_lit2
8644
8645
8646 @
8647 @.String size exceeded@>
8648 It's time for a complaint if either of the two (entry or global)
8649 string lengths is exceeded.
8650
8651 @d bst_string_size_exceeded(#) == begin
8652 bst_1print_string_size_exceeded;
8653 print (#);
8654 bst_2print_string_size_exceeded;
8655 end
8656
8657 @<Procedures and functions for all file I/O, error messages, and such@>=
8658 procedure bst_1print_string_size_exceeded;
8659 begin
8660 print ('Warning--you''ve exceeded ');
8661 end;
8662 @#
8663 procedure bst_2print_string_size_exceeded;
8664 begin
8665 print ('-string-size,');
8666 bst_mild_ex_warn_print;
8667 print_ln ('*Please notify the bibstyle designer*');
8668 end;
8669
8670
8671 @
8672 @.entry string size exceeded@>
8673 @:String size exceeded}{\quad entry string size@>
8674 This module checks that what we're about to assign is really a
8675 string, and then assigns.
8676
8677 @<Assign to a |str_entry_var|@>=
8678 begin
8679 if (pop_typ2 <> stk_str) then
8680 print_wrong_stk_lit (pop_lit2,pop_typ2,stk_str)
8681 else
8682 begin
8683 str_ent_ptr := cite_ptr*num_ent_strs + fn_info[pop_lit1];
8684 ent_chr_ptr := 0;
8685 sp_ptr := str_start[pop_lit2];
8686 sp_xptr1 := str_start[pop_lit2+1];
8687 if (sp_xptr1-sp_ptr > ent_str_size) then
8688 begin
8689 bst_string_size_exceeded (ent_str_size:0,', the entry');
8690 sp_xptr1 := sp_ptr + ent_str_size;
8691 end;
8692 while (sp_ptr < sp_xptr1) do
8693 begin {copy characters into |entry_strs|}
8694 entry_strs[str_ent_ptr][ent_chr_ptr] := str_pool[sp_ptr];
8695 incr(ent_chr_ptr);
8696 incr(sp_ptr);
8697 end;
8698 entry_strs[str_ent_ptr][ent_chr_ptr] := end_of_string;
8699 end
8700 end
8701
8702
8703 @
8704 This module checks that what we're about to assign is really an
8705 integer, and then assigns.
8706
8707 @<Assign to an |int_global_var|@>=
8708 if (pop_typ2 <> stk_int) then
8709 print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int)
8710 else
8711 fn_info[pop_lit1] := pop_lit2
8712
8713
8714 @
8715 @.global string size exceeded@>
8716 @:String size exceeded}{\quad global string size@>
8717 This module checks that what we're about to assign is really a
8718 string, and then assigns.
8719
8720 @<Assign to a |str_global_var|@>=
8721 begin
8722 if (pop_typ2 <> stk_str) then
8723 print_wrong_stk_lit (pop_lit2,pop_typ2,stk_str)
8724 else
8725 begin
8726 str_glb_ptr := fn_info[pop_lit1];
8727 if (pop_lit2 < cmd_str_ptr) then
8728 glb_str_ptr[str_glb_ptr] := pop_lit2
8729 else
8730 begin
8731 glb_str_ptr[str_glb_ptr] := 0;
8732 glob_chr_ptr := 0;
8733 sp_ptr := str_start[pop_lit2];
8734 sp_end := str_start[pop_lit2+1];
8735 if (sp_end - sp_ptr > glob_str_size) then
8736 begin
8737 bst_string_size_exceeded (glob_str_size:0,', the global');
8738 sp_end := sp_ptr + glob_str_size;
8739 end;
8740 while (sp_ptr < sp_end) do
8741 begin {copy characters into |global_strs|}
8742 global_strs[str_glb_ptr][glob_chr_ptr] := str_pool[sp_ptr];
8743 incr(glob_chr_ptr);
8744 incr(sp_ptr);
8745 end;
8746 glb_str_end[str_glb_ptr] := glob_chr_ptr;
8747 end;
8748 end
8749 end
8750
8751
8752 @
8753 The |built_in| function {\.{add.period\$}} pops the top (string)
8754 literal, adds a |period| to a nonnull string if its last
8755 non|right_brace| character isn't a |period|, |question_mark|, or
8756 |exclamation_mark|, and pushes this resulting string back onto the
8757 stack. If the literal isn't a string, it complains and pushes the
8758 null string.
8759
8760 @<|execute_fn|({\.{add.period\$}})@>=
8761 procedure x_add_period;
8762 label loop_exit;
8763 begin
8764 pop_lit_stk (pop_lit1,pop_typ1);
8765 if (pop_typ1 <> stk_str) then
8766 begin
8767 print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
8768 push_lit_stk (s_null, stk_str);
8769 end
8770 else if (length(pop_lit1) = 0) then {don't add |period| to the null string}
8771 push_lit_stk (s_null, stk_str)
8772 else
8773 @<Add the |period|\(f), if necessary, and push@>;
8774 end;
8775
8776
8777 @
8778 @^push the literal stack@>
8779 Here we scan backwards from the end of the string, skipping
8780 non|right_brace| characters, to see if we have to add the |period|.
8781
8782 @<Add the |period|\(f), if necessary, and push@>=
8783 begin
8784 sp_ptr := str_start[pop_lit1+1];
8785 sp_end := str_start[pop_lit1];
8786 while (sp_ptr > sp_end) do {find a non|right_brace|}
8787 begin
8788 decr(sp_ptr);
8789 if (str_pool[sp_ptr] <> right_brace) then
8790 goto loop_exit;
8791 end;
8792 loop_exit:
8793 case (str_pool[sp_ptr]) of
8794 period,
8795 question_mark,
8796 exclamation_mark :
8797 repush_string;
8798 othercases
8799 @<Add the |period|\(t) (it's necessary) and push@>
8800 endcases;
8801 end
8802
8803
8804 @
8805 Ok guys, we really have to do it.
8806
8807 @<Add the |period|\(t) (it's necessary) and push@>=
8808 begin
8809 if (pop_lit1 < cmd_str_ptr) then
8810 begin
8811 str_room (length(pop_lit1)+1);
8812 sp_ptr := str_start[pop_lit1];
8813 sp_end := str_start[pop_lit1+1];
8814 while (sp_ptr < sp_end) do {slide |pop_lit1| atop the string pool}
8815 begin
8816 append_char (str_pool[sp_ptr]);
8817 incr(sp_ptr);
8818 end;
8819 end
8820 else {the string is already there}
8821 begin
8822 pool_ptr := str_start[pop_lit1+1];
8823 str_room (1);
8824 end;
8825 append_char (period);
8826 push_lit_stk (make_string, stk_str);
8827 end
8828
8829
8830 @
8831 The |built_in| function {\.{call.type\$}} executes the function
8832 specified in |type_list| for this entry unless it's |undefined|, in
8833 which case it executes the default function \.{default.type} defined
8834 in the \.{.bst} file, or unless it's |empty|, in which case it does
8835 nothing.
8836
8837 @<|execute_fn|({\.{call.type\$}})@>=
8838 begin
8839 if (not mess_with_entries) then
8840 bst_cant_mess_with_entries_print
8841 else
8842 if (type_list[cite_ptr] = undefined) then
8843 execute_fn (b_default)
8844 else if (type_list[cite_ptr] = empty) then
8845 do_nothing
8846 else
8847 execute_fn (type_list[cite_ptr]);
8848 end
8849
8850
8851 @
8852 The |built_in| function {\.{change.case\$}} pops the top two (string)
8853 literals; it changes the case of the second according to the
8854 specifications of the first, as follows. (Note: The word `letters' in
8855 the next sentence refers only to those at brace-level~0, the top-most
8856 brace level; no other characters are changed, except perhaps for
8857 special characters, described shortly.) If the first literal is the
8858 string~\.{t}, it converts to lower case all letters except the very
8859 first character in the string, which it leaves alone, and except the
8860 first character following any |colon| and then nonnull |white_space|,
8861 which it also leaves alone; if it's the string~\.{l}, it converts all
8862 letters to lower case; if it's the string~\.{u}, it converts all
8863 letters to upper case; and if it's anything else, it complains and
8864 does no conversion. It then pushes this resulting string. If either
8865 type is incorrect, it complains and pushes the null string; however,
8866 if both types are correct but the specification string (i.e., the
8867 first string) isn't one of the legal ones, it merely pushes the second
8868 back onto the stack, after complaining. (Another note: It ignores
8869 case differences in the specification string; for example, the strings
8870 \.{t} and \.{T} are equivalent for the purposes of this |built_in|
8871 function.)
8872
8873 @d ok_pascal_i_give_up = 21
8874
8875 @<|execute_fn|({\.{change.case\$}})@>=
8876 procedure x_change_case;
8877 label ok_pascal_i_give_up;
8878 begin
8879 pop_lit_stk (pop_lit1,pop_typ1);
8880 pop_lit_stk (pop_lit2,pop_typ2);
8881 if (pop_typ1 <> stk_str) then
8882 begin
8883 print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
8884 push_lit_stk (s_null, stk_str);
8885 end
8886 else if (pop_typ2 <> stk_str) then
8887 begin
8888 print_wrong_stk_lit (pop_lit2,pop_typ2,stk_str);
8889 push_lit_stk (s_null, stk_str);
8890 end
8891 else
8892 begin
8893 @<Determine the case-conversion type@>;
8894 ex_buf_length := 0;
8895 add_buf_pool (pop_lit2);
8896 @<Perform the case conversion@>;
8897 add_pool_buf_and_push; {push this string onto the stack}
8898 end;
8899 end;
8900
8901
8902 @
8903 First we define a few variables for case conversion. The constant
8904 definitions, to be used in |case| statements, are in order of probable
8905 frequency.
8906
8907 @d title_lowers = 0 {representing the string \.{t}}
8908 @d all_lowers = 1 {representing the string \.{l}}
8909 @d all_uppers = 2 {representing the string \.{u}}
8910 @d bad_conversion = 3 {representing any illegal case-conversion string}
8911
8912 @<Globals in the outer block@>=
8913 @!conversion_type : 0..bad_conversion; {the possible cases}
8914 @!prev_colon : boolean; {|true| if just past a |colon|}
8915
8916
8917 @
8918 Now we determine which of the three case-conversion types we're
8919 dealing with: \.{t},~\.{l}, or~\.{u}.
8920
8921 @<Determine the case-conversion type@>=
8922 begin
8923 case (str_pool[str_start[pop_lit1]]) of
8924 "t","T" : conversion_type := title_lowers;
8925 "l","L" : conversion_type := all_lowers;
8926 "u","U" : conversion_type := all_uppers;
8927 othercases conversion_type := bad_conversion
8928 endcases;
8929 if ((length(pop_lit1) <> 1) or (conversion_type = bad_conversion)) then
8930 begin
8931 conversion_type := bad_conversion;
8932 print_pool_str (pop_lit1);
8933 bst_ex_warn (' is an illegal case-conversion string');
8934 end;
8935 end
8936
8937
8938 @
8939 This procedure complains if the just-encountered |right_brace| would
8940 make |brace_level| negative.
8941
8942 @<Procedures and functions for name-string processing@>=
8943 procedure decr_brace_level (@!pop_lit_var : str_number);
8944 begin
8945 if (brace_level = 0) then
8946 braces_unbalanced_complaint (pop_lit_var)
8947 else
8948 decr(brace_level);
8949 end;
8950
8951
8952 @
8953 This complaint often arises because the style designer has to type
8954 lots of braces.
8955
8956 @<Procedures and functions for all file I/O, error messages, and such@>=
8957 procedure braces_unbalanced_complaint (@!pop_lit_var : str_number);
8958 begin
8959 print ('Warning--"');
8960 print_pool_str (pop_lit_var);
8961 bst_mild_ex_warn ('" isn''t a brace-balanced string');
8962 end;
8963
8964
8965 @
8966 This one makes sure that |brace_level=0| (it's called at a point in a
8967 string where braces must be balanced).
8968
8969 @<Procedures and functions for name-string processing@>=
8970 procedure check_brace_level (@!pop_lit_var : str_number);
8971 begin
8972 if (brace_level > 0) then
8973 braces_unbalanced_complaint (pop_lit_var);
8974 end;
8975
8976
8977 @
8978 Here's where we actually go through the string and do the case
8979 conversion.
8980
8981 @<Perform the case conversion@>=
8982 begin
8983 brace_level := 0; {this is the top level}
8984 ex_buf_ptr := 0; {we start with the string's first character}
8985 while (ex_buf_ptr < ex_buf_length) do
8986 begin
8987 if (ex_buf[ex_buf_ptr] = left_brace) then
8988 begin
8989 incr(brace_level);
8990 if (brace_level <> 1) then
8991 goto ok_pascal_i_give_up;
8992 if (ex_buf_ptr + 4 > ex_buf_length) then
8993 goto ok_pascal_i_give_up
8994 else if (ex_buf[ex_buf_ptr+1] <> backslash) then
8995 goto ok_pascal_i_give_up;
8996 if (conversion_type = title_lowers) then
8997 if (ex_buf_ptr = 0) then
8998 goto ok_pascal_i_give_up
8999 else if ((prev_colon) and
9000 (lex_class[ex_buf[ex_buf_ptr-1]] = white_space)) then
9001 goto ok_pascal_i_give_up;
9002 @<Convert a \(s)special character@>;
9003 ok_pascal_i_give_up:
9004 prev_colon := false;
9005 end
9006 else if (ex_buf[ex_buf_ptr] = right_brace) then
9007 begin
9008 decr_brace_level (pop_lit2);
9009 prev_colon := false;
9010 end
9011 else
9012 if (brace_level = 0) then
9013 @<Convert a \(b)|brace_level = 0| character@>;
9014 incr(ex_buf_ptr);
9015 end;
9016 check_brace_level (pop_lit2);
9017 end
9018
9019
9020 @
9021 @^special character@>
9022 We're dealing with a special character (usually either an undotted
9023 `\i' or `\j', or an accent like one in Table~3.1 of the \LaTeX\
9024 manual, or a foreign character like one in Table~3.2) if the first
9025 character after the |left_brace| is a |backslash|; the special
9026 character ends with the matching |right_brace|. How we handle what's
9027 in between depends on the special character. In general, this code
9028 will do reasonably well if there is other stuff, too, between braces,
9029 but it doesn't try to do anything special with |colon|s.
9030
9031 @<Convert a \(s)special character@>=
9032 begin
9033 incr(ex_buf_ptr); {skip over the |left_brace|}
9034 while ((ex_buf_ptr < ex_buf_length) and (brace_level > 0)) do
9035 begin
9036 incr(ex_buf_ptr); {skip over the |backslash|}
9037 ex_buf_xptr := ex_buf_ptr;
9038 while ((ex_buf_ptr < ex_buf_length) and
9039 (lex_class[ex_buf[ex_buf_ptr]] = alpha)) do
9040 incr(ex_buf_ptr); {this scans the control sequence}
9041 control_seq_loc := str_lookup(ex_buf,ex_buf_xptr,ex_buf_ptr-ex_buf_xptr,
9042 control_seq_ilk,dont_insert);
9043 if (hash_found) then
9044 @<Convert the accented or foreign character, if necessary@>;
9045 ex_buf_xptr := ex_buf_ptr;
9046 while ((ex_buf_ptr < ex_buf_length) and (brace_level > 0) and
9047 (ex_buf[ex_buf_ptr] <> backslash)) do
9048 begin {this scans to the next control sequence}
9049 if (ex_buf[ex_buf_ptr] = right_brace) then
9050 decr(brace_level)
9051 else if (ex_buf[ex_buf_ptr] = left_brace) then
9052 incr(brace_level);
9053 incr(ex_buf_ptr);
9054 end;
9055 @<Convert a \(n)noncontrol sequence@>;
9056 end;
9057 decr(ex_buf_ptr); {unskip the |right_brace|}
9058 end
9059
9060
9061 @
9062 @^control sequence@>
9063 @:this can't happen}{\quad Unknown type of case conversion@>
9064 A control sequence, for the purposes of this program, consists just of
9065 the consecutive alphabetic characters following the |backslash|; it
9066 might be empty (although ones in this section aren't).
9067
9068 @<Convert the accented or foreign character, if necessary@>=
9069 begin
9070 case (conversion_type) of
9071 title_lowers,
9072 all_lowers :
9073 case (ilk_info[control_seq_loc]) of
9074 n_l_upper,
9075 n_o_upper,
9076 n_oe_upper,
9077 n_ae_upper,
9078 n_aa_upper :
9079 lower_case (ex_buf, ex_buf_xptr, ex_buf_ptr-ex_buf_xptr);
9080 othercases
9081 do_nothing
9082 endcases;
9083 all_uppers :
9084 case (ilk_info[control_seq_loc]) of
9085 n_l,
9086 n_o,
9087 n_oe,
9088 n_ae,
9089 n_aa :
9090 upper_case (ex_buf, ex_buf_xptr, ex_buf_ptr-ex_buf_xptr);
9091 n_i,
9092 n_j,
9093 n_ss :
9094 @<Convert, then remove the control sequence@>;
9095 othercases
9096 do_nothing
9097 endcases;
9098 bad_conversion :
9099 do_nothing;
9100 othercases
9101 case_conversion_confusion
9102 endcases;
9103 end
9104
9105
9106 @
9107 @:this can't happen}{\quad Unknown type of case conversion@>
9108 Another bug complaint.
9109
9110 @<Procedures and functions for all file I/O, error messages, and such@>=
9111 procedure case_conversion_confusion;
9112 begin
9113 confusion ('Unknown type of case conversion');
9114 end;
9115
9116
9117 @
9118 After converting the control sequence, we need to remove the preceding
9119 |backslash| and any following |white_space|.
9120
9121 @<Convert, then remove the control sequence@>=
9122 begin
9123 upper_case (ex_buf, ex_buf_xptr, ex_buf_ptr-ex_buf_xptr);
9124 while (ex_buf_xptr < ex_buf_ptr) do
9125 begin {remove preceding |backslash| and shift down}
9126 ex_buf[ex_buf_xptr-1] := ex_buf[ex_buf_xptr];
9127 incr(ex_buf_xptr);
9128 end;
9129 decr(ex_buf_xptr);
9130 while ((ex_buf_ptr < ex_buf_length) and
9131 (lex_class[ex_buf[ex_buf_ptr]] = white_space)) do
9132 incr(ex_buf_ptr); {remove |white_space| trailing the control seq}
9133 tmp_ptr := ex_buf_ptr;
9134 while (tmp_ptr < ex_buf_length) do
9135 begin {more shifting down}
9136 ex_buf[tmp_ptr-(ex_buf_ptr-ex_buf_xptr)] := ex_buf[tmp_ptr];
9137 incr(tmp_ptr)
9138 end;
9139 ex_buf_length := tmp_ptr - (ex_buf_ptr - ex_buf_xptr);
9140 ex_buf_ptr := ex_buf_xptr;
9141 end
9142
9143
9144 @
9145 @:this can't happen}{\quad Unknown type of case conversion@>
9146 There are no control sequences in what we're about to convert,
9147 so a straight conversion suffices.
9148
9149 @<Convert a \(n)noncontrol sequence@>=
9150 begin
9151 case (conversion_type) of
9152 title_lowers,
9153 all_lowers :
9154 lower_case (ex_buf, ex_buf_xptr, ex_buf_ptr-ex_buf_xptr);
9155 all_uppers :
9156 upper_case (ex_buf, ex_buf_xptr, ex_buf_ptr-ex_buf_xptr);
9157 bad_conversion :
9158 do_nothing;
9159 othercases
9160 case_conversion_confusion
9161 endcases;
9162 end
9163
9164
9165 @
9166 @:this can't happen}{\quad Unknown type of case conversion@>
9167 This code does any needed conversion for an ordinary character; it
9168 won't touch nonletters.
9169
9170 @<Convert a \(b)|brace_level = 0| character@>=
9171 begin
9172 case (conversion_type) of
9173 title_lowers :
9174 begin
9175 if (ex_buf_ptr = 0) then
9176 do_nothing
9177 else if ((prev_colon) and
9178 (lex_class[ex_buf[ex_buf_ptr-1]] = white_space)) then
9179 do_nothing
9180 else
9181 lower_case (ex_buf, ex_buf_ptr, 1);
9182 if (ex_buf[ex_buf_ptr] = colon) then
9183 prev_colon := true
9184 else if (lex_class[ex_buf[ex_buf_ptr]] <> white_space) then
9185 prev_colon := false;
9186 end;
9187 all_lowers :
9188 lower_case (ex_buf, ex_buf_ptr, 1);
9189 all_uppers :
9190 upper_case (ex_buf, ex_buf_ptr, 1);
9191 bad_conversion :
9192 do_nothing;
9193 othercases
9194 case_conversion_confusion
9195 endcases;
9196 end
9197
9198
9199 @
9200 The |built_in| function {\.{chr.to.int\$}} pops the top (string)
9201 literal, makes sure it's a single character, converts it to the
9202 corresponding |ASCII_code| integer, and pushes this integer. If the
9203 literal isn't an appropriate string, it complains and pushes the
9204 integer~0.
9205
9206 @<|execute_fn|({\.{chr.to.int\$}})@>=
9207 procedure x_chr_to_int;
9208 begin
9209 pop_lit_stk (pop_lit1,pop_typ1);
9210 if (pop_typ1 <> stk_str) then
9211 begin
9212 print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
9213 push_lit_stk (0, stk_int);
9214 end
9215 else if (length(pop_lit1) <> 1) then
9216 begin
9217 print ('"');
9218 print_pool_str (pop_lit1);
9219 bst_ex_warn ('" isn''t a single character');
9220 push_lit_stk (0, stk_int);
9221 end
9222 else
9223 push_lit_stk (str_pool[str_start[pop_lit1]], stk_int);
9224 {push the (|ASCII_code|) integer}
9225 end;
9226
9227
9228 @
9229 The |built_in| function {\.{cite\$}} pushes the appropriate string
9230 from |cite_list| onto the stack.
9231
9232 @<|execute_fn|({\.{cite\$}})@>=
9233 procedure x_cite;
9234 begin
9235 if (not mess_with_entries) then
9236 bst_cant_mess_with_entries_print
9237 else
9238 push_lit_stk (cur_cite_str, stk_str);
9239 end;
9240
9241
9242 @
9243 @^push the literal stack@>
9244 The |built_in| function {\.{duplicate\$}} pops the top literal from
9245 the stack and pushes two copies of it.
9246
9247 @<|execute_fn|({\.{duplicate\$}})@>=
9248 procedure x_duplicate;
9249 begin
9250 pop_lit_stk (pop_lit1,pop_typ1);
9251 if (pop_typ1 <> stk_str) then
9252 begin
9253 push_lit_stk (pop_lit1, pop_typ1);
9254 push_lit_stk (pop_lit1, pop_typ1);
9255 end
9256 else
9257 begin
9258 repush_string;
9259 if (pop_lit1 < cmd_str_ptr) then
9260 push_lit_stk (pop_lit1, pop_typ1)
9261 else
9262 begin
9263 str_room (length(pop_lit1));
9264 sp_ptr := str_start[pop_lit1];
9265 sp_end := str_start[pop_lit1+1];
9266 while (sp_ptr < sp_end) do
9267 begin
9268 append_char (str_pool[sp_ptr]);
9269 incr(sp_ptr);
9270 end;
9271 push_lit_stk (make_string, stk_str); {and push it onto the stack}
9272 end;
9273 end;
9274 end;
9275
9276
9277 @
9278 The |built_in| function {\.{empty\$}} pops the top literal and pushes
9279 the integer 1 if it's a missing field or a string having no
9280 non|white_space| characters, 0 otherwise. If the literal isn't a
9281 missing field or a string, it complains and pushes 0.
9282
9283 @<|execute_fn|({\.{empty\$}})@>=
9284 procedure x_empty;
9285 label exit;
9286 begin
9287 pop_lit_stk (pop_lit1,pop_typ1);
9288 case (pop_typ1) of
9289 stk_str : @<Push 0 if the string has a non|white_space| char, else 1@>;
9290 stk_field_missing : push_lit_stk (1, stk_int);
9291 stk_empty : push_lit_stk (0, stk_int);
9292 othercases
9293 begin
9294 print_stk_lit (pop_lit1,pop_typ1);
9295 bst_ex_warn (', not a string or missing field,');
9296 push_lit_stk (0, stk_int);
9297 end
9298 endcases;
9299 exit:
9300 end;
9301
9302
9303 @
9304 When we arrive here we're dealing with a legitimate string. If it has
9305 no characters, or has nothing but |white_space| characters, we push~1,
9306 otherwise we push~0.
9307
9308 @<Push 0 if the string has a non|white_space| char, else 1@>=
9309 begin
9310 sp_ptr := str_start[pop_lit1];
9311 sp_end := str_start[pop_lit1+1];
9312 while (sp_ptr < sp_end) do
9313 begin
9314 if (lex_class[str_pool[sp_ptr]] <> white_space) then
9315 begin
9316 push_lit_stk (0, stk_int);
9317 return;
9318 end;
9319 incr(sp_ptr);
9320 end;
9321 push_lit_stk (1, stk_int);
9322 end
9323
9324
9325 @
9326 The |built_in| function {\.{format.name\$}} pops the top three
9327 literals (they are a string, an integer, and a string literal, in that
9328 order). The last string literal represents a name list (each name
9329 corresponding to a person), the integer literal specifies which name
9330 to pick from this list, and the first string literal specifies how to
9331 format this name, as described in the \BibTeX\ documentation.
9332 Finally, this function pushes the formatted name. If any of the types
9333 is incorrect, it complains and pushes the null string.
9334
9335 @d von_found = 52 {for when a von token is found}
9336
9337 @<|execute_fn|({\.{format.name\$}})@>=
9338 procedure x_format_name;
9339 label loop1_exit,@!loop2_exit,@!von_found;
9340 begin
9341 pop_lit_stk (pop_lit1,pop_typ1);
9342 pop_lit_stk (pop_lit2,pop_typ2);
9343 pop_lit_stk (pop_lit3,pop_typ3);
9344 if (pop_typ1 <> stk_str) then
9345 begin
9346 print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
9347 push_lit_stk (s_null, stk_str);
9348 end
9349 else if (pop_typ2 <> stk_int) then
9350 begin
9351 print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int);
9352 push_lit_stk (s_null, stk_str);
9353 end
9354 else if (pop_typ3 <> stk_str) then
9355 begin
9356 print_wrong_stk_lit (pop_lit3,pop_typ3,stk_str);
9357 push_lit_stk (s_null, stk_str);
9358 end
9359 else
9360 begin
9361 ex_buf_length := 0;
9362 add_buf_pool (pop_lit3);
9363 @<Isolate the desired name@>;
9364 @<Copy name and count |comma|s to determine syntax@>;
9365 @<Find the parts of the name@>;
9366 ex_buf_length := 0;
9367 add_buf_pool (pop_lit1);
9368 figure_out_the_formatted_name;@/
9369 add_pool_buf_and_push; {push the formatted string onto the stack}
9370 end;
9371 end;
9372
9373
9374 @
9375 This module skips over undesired names in |pop_lit3| and it throws
9376 away the ``and'' from the end of the name if it exists. When it's
9377 done, |ex_buf_xptr| points to its first character and |ex_buf_ptr|
9378 points just past its last.
9379
9380 @<Isolate the desired name@>=
9381 begin
9382 ex_buf_ptr := 0;
9383 num_names := 0;
9384 while ((num_names < pop_lit2) and (ex_buf_ptr < ex_buf_length)) do
9385 begin
9386 incr(num_names);
9387 ex_buf_xptr := ex_buf_ptr;
9388 name_scan_for_and (pop_lit3);
9389 end;
9390 if (ex_buf_ptr < ex_buf_length) then {remove the ``and''}
9391 ex_buf_ptr := ex_buf_ptr - 4;
9392 if (num_names < pop_lit2) then
9393 begin
9394 if (pop_lit2 = 1) then
9395 print ('There is no name in "')
9396 else
9397 print ('There aren''t ',pop_lit2:0,' names in "');
9398 print_pool_str (pop_lit3);
9399 bst_ex_warn ('"');
9400 end
9401 end
9402
9403
9404 @
9405 This module, starting at |ex_buf_ptr|, looks in |ex_buf| for an
9406 ``and'' surrounded by nonnull |white_space|. It stops either at
9407 |ex_buf_length| or just past the ``and'', whichever comes first,
9408 setting |ex_buf_ptr| accordingly. Its parameter |pop_lit_var| is
9409 either |pop_lit3| or |pop_lit1|, depending on whether
9410 {\.{format.name\$}} or {\.{num.names\$}} calls it.
9411
9412 @<Procedures and functions for name-string processing@>=
9413 procedure name_scan_for_and (@!pop_lit_var : str_number);
9414 begin
9415 brace_level := 0;
9416 preceding_white := false;
9417 and_found := false;
9418 while ((not and_found) and (ex_buf_ptr < ex_buf_length)) do
9419 case (ex_buf[ex_buf_ptr]) of
9420 "a", "A" :
9421 begin
9422 incr(ex_buf_ptr);
9423 if (preceding_white) then
9424 @<See if we have an ``and''@>; {if so, |and_found := true|}
9425 preceding_white := false;
9426 end;
9427 left_brace :
9428 begin
9429 incr(brace_level);
9430 incr(ex_buf_ptr);
9431 @<Skip over |ex_buf| stuff at |brace_level > 0|@>;
9432 preceding_white := false;
9433 end;
9434 right_brace :
9435 begin
9436 decr_brace_level (pop_lit_var); {this checks for an error}
9437 incr(ex_buf_ptr);
9438 preceding_white := false;
9439 end;
9440 othercases
9441 if (lex_class[ex_buf[ex_buf_ptr]] = white_space) then
9442 begin
9443 incr(ex_buf_ptr);
9444 preceding_white := true;
9445 end
9446 else
9447 begin
9448 incr(ex_buf_ptr);
9449 preceding_white := false;
9450 end
9451 endcases;
9452 check_brace_level (pop_lit_var);
9453 end;
9454
9455
9456 @
9457 When we come here |ex_buf_ptr| is just past the |left_brace|, and when
9458 we leave it's either at |ex_buf_length| or just past the matching
9459 |right_brace|.
9460
9461 @<Skip over |ex_buf| stuff at |brace_level > 0|@>=
9462 while ((brace_level > 0) and (ex_buf_ptr < ex_buf_length)) do
9463 begin
9464 if (ex_buf[ex_buf_ptr] = right_brace) then
9465 decr(brace_level)
9466 else if (ex_buf[ex_buf_ptr] = left_brace) then
9467 incr(brace_level);
9468 incr(ex_buf_ptr);
9469 end
9470
9471
9472 @
9473 When we come here |ex_buf_ptr| is just past the ``a'' or ``A'', and when
9474 we leave it's either at the same place or, if we found an ``and'', at
9475 the following |white_space| character.
9476
9477 @<See if we have an ``and''@>=
9478 begin
9479 if (ex_buf_ptr <= (ex_buf_length - 3)) then {enough characters are left}
9480 if ((ex_buf[ex_buf_ptr] = "n") or (ex_buf[ex_buf_ptr] = "N")) then
9481 if ((ex_buf[ex_buf_ptr+1] = "d") or (ex_buf[ex_buf_ptr+1] = "D")) then
9482 if (lex_class[ex_buf[ex_buf_ptr+2]] = white_space) then
9483 begin
9484 ex_buf_ptr := ex_buf_ptr + 2;
9485 and_found := true;
9486 end;
9487 end
9488
9489
9490 @
9491 When we arrive here, the desired name is in |ex_buf[ex_buf_xptr]|
9492 through |ex_buf[ex_buf_ptr-1]|. This module does its thing for
9493 characters only at |brace_level = 0|; the rest get processed verbatim.
9494 It removes leading |white_space| (and |sep_char|s), and trailing
9495 |white_space| (and |sep_char|s) and |comma|s, complaining for each
9496 trailing |comma|. It then copies the name into |name_buf|, removing
9497 all |white_space|, |sep_char|s and |comma|s, counting |comma|s, and
9498 constructing a list of name tokens, which are sequences of characters
9499 separated (at |brace_level=0|) by |white_space|, |sep_char|s or
9500 |comma|s. Each name token but the first has an associated
9501 |name_sep_char|, the character that separates it from the preceding
9502 token. If there are too many (more than two) |comma|s, a complaint is
9503 in order.
9504
9505 @<Copy name and count |comma|s to determine syntax@>=
9506 begin
9507 @<Remove leading and trailing junk, complaining if necessary@>;
9508 name_bf_ptr := 0;
9509 num_commas := 0;
9510 num_tokens := 0;@/
9511 token_starting := true; {to indicate that a name token is starting}
9512 while (ex_buf_xptr < ex_buf_ptr) do
9513 case (ex_buf[ex_buf_xptr]) of
9514 comma : @<Name-process a |comma|@>;
9515 left_brace : @<Name-process a |left_brace|@>;
9516 right_brace : @<Name-process a |right_brace|@>;
9517 othercases
9518 case (lex_class[ex_buf[ex_buf_xptr]]) of
9519 white_space : @<Name-process a |white_space|@>;
9520 sep_char : @<Name-process a |sep_char|@>;
9521 othercases @<Name-process some other character@>
9522 endcases
9523 endcases;
9524 name_tok[num_tokens] := name_bf_ptr; {this is an end-marker}
9525 end
9526
9527
9528 @
9529 This module removes all leading |white_space| (and |sep_char|s), and
9530 trailing |white_space| (and |sep_char|s) and |comma|s. It complains
9531 for each trailing |comma|.
9532
9533 @<Remove leading and trailing junk, complaining if necessary@>=
9534 begin
9535 while ((ex_buf_xptr < ex_buf_ptr) and
9536 (lex_class[ex_buf[ex_buf_ptr]] = white_space) and
9537 (lex_class[ex_buf[ex_buf_ptr]] = sep_char)) do
9538 incr(ex_buf_xptr); {this removes leading stuff}
9539 while (ex_buf_ptr > ex_buf_xptr) do {now remove trailing stuff}
9540 case (lex_class[ex_buf[ex_buf_ptr-1]]) of
9541 white_space,
9542 sep_char :
9543 decr(ex_buf_ptr);
9544 othercases
9545 if (ex_buf[ex_buf_ptr-1] = comma) then
9546 begin
9547 print ('Name ',pop_lit2:0,' in "');
9548 print_pool_str (pop_lit3);
9549 print ('" has a comma at the end');
9550 bst_ex_warn_print;
9551 decr(ex_buf_ptr);
9552 end
9553 else
9554 goto loop1_exit
9555 endcases;
9556 loop1_exit:
9557
9558 end
9559
9560 @
9561 Here we mark the token number at which this comma has occurred.
9562
9563 @<Name-process a |comma|@>=
9564 begin
9565 if (num_commas = 2) then
9566 begin
9567 print ('Too many commas in name ',pop_lit2:0,' of "');
9568 print_pool_str (pop_lit3);
9569 print ('"');
9570 bst_ex_warn_print;
9571 end
9572 else
9573 begin
9574 incr(num_commas);
9575 if (num_commas = 1) then
9576 comma1 := num_tokens
9577 else
9578 comma2 := num_tokens; {|num_commas = 2|}
9579 name_sep_char[num_tokens] := comma;
9580 end;
9581 incr(ex_buf_xptr);
9582 token_starting := true;
9583 end
9584
9585
9586 @
9587 We copy the stuff up through the matching |right_brace| verbatim.
9588
9589 @<Name-process a |left_brace|@>=
9590 begin
9591 incr(brace_level);
9592 if (token_starting) then
9593 begin
9594 name_tok[num_tokens] := name_bf_ptr;
9595 incr(num_tokens);
9596 end;
9597 name_buf[name_bf_ptr] := ex_buf[ex_buf_xptr];
9598 incr(name_bf_ptr);
9599 incr(ex_buf_xptr);
9600 while ((brace_level > 0) and (ex_buf_xptr < ex_buf_ptr)) do
9601 begin
9602 if (ex_buf[ex_buf_xptr] = right_brace) then
9603 decr(brace_level)
9604 else if (ex_buf[ex_buf_xptr] = left_brace) then
9605 incr(brace_level);
9606 name_buf[name_bf_ptr] := ex_buf[ex_buf_xptr];
9607 incr(name_bf_ptr);
9608 incr(ex_buf_xptr);
9609 end;
9610 token_starting := false;
9611 end
9612
9613
9614 @
9615 We don't copy an extra |right_brace|; this code will almost never be
9616 executed.
9617
9618 @<Name-process a |right_brace|@>=
9619 begin
9620 if (token_starting) then
9621 begin
9622 name_tok[num_tokens] := name_bf_ptr;
9623 incr(num_tokens);
9624 end;
9625 print ('Name ',pop_lit2:0,' of "');
9626 print_pool_str (pop_lit3);
9627 bst_ex_warn ('" isn''t brace balanced');
9628 incr(ex_buf_xptr);
9629 token_starting := false;
9630 end
9631
9632
9633 @
9634 A token will be starting soon in a buffer near you, one way$\ldots$
9635
9636 @<Name-process a |white_space|@>=
9637 begin
9638 if (not token_starting) then
9639 name_sep_char[num_tokens] := space;
9640 incr(ex_buf_xptr);
9641 token_starting := true;
9642 end
9643
9644
9645 @
9646 @^user abuse@>
9647 or another. If one of the valid |sep_char|s appears between tokens,
9648 we usually use it instead of a |space|. If the user has been silly
9649 enough to have multiple |sep_char|s, or to have both |white_space| and
9650 a |sep_char|, we use the first such character.
9651
9652 @<Name-process a |sep_char|@>=
9653 begin
9654 if (not token_starting) then
9655 name_sep_char[num_tokens] := ex_buf[ex_buf_xptr];
9656 incr(ex_buf_xptr);
9657 token_starting := true;
9658 end
9659
9660
9661 @
9662 For ordinary characters, we just copy the character.
9663
9664 @<Name-process some other character@>=
9665 begin
9666 if (token_starting) then
9667 begin
9668 name_tok[num_tokens] := name_bf_ptr;
9669 incr(num_tokens);
9670 end;
9671 name_buf[name_bf_ptr] := ex_buf[ex_buf_xptr];
9672 incr(name_bf_ptr);
9673 incr(ex_buf_xptr);
9674 token_starting := false;
9675 end
9676
9677
9678 @
9679 @:this can't happen}{\quad Illegal number of comma,s@>
9680 Here we set all the pointers for the various parts of the name,
9681 depending on which of the three possible syntaxes this name uses.
9682
9683 @<Find the parts of the name@>=
9684 begin
9685 if (num_commas = 0) then
9686 begin
9687 first_start := 0;
9688 last_end := num_tokens;
9689 jr_end := last_end;
9690 @<Determine where the first name ends and von name starts and ends@>;
9691 end
9692 else if (num_commas = 1) then
9693 begin
9694 von_start := 0;
9695 last_end := comma1;
9696 jr_end := last_end;
9697 first_start := jr_end;
9698 first_end := num_tokens;
9699 von_name_ends_and_last_name_starts_stuff;
9700 end
9701 else if (num_commas = 2) then
9702 begin
9703 von_start := 0;
9704 last_end := comma1;
9705 jr_end := comma2;
9706 first_start := jr_end;
9707 first_end := num_tokens;
9708 von_name_ends_and_last_name_starts_stuff;
9709 end
9710 else
9711 confusion ('Illegal number of comma,s');
9712 end
9713
9714
9715 @
9716 When there are no brace-level-0 |comma|s in the name, the von name
9717 starts with the first nonlast token whose first brace-level-0 letter
9718 is in lower case (for the purposes of this determination, an accented
9719 or foreign character at brace-level-1 that's in lower case will do, as
9720 well). A module following this one determines where the von name ends
9721 and the last starts.
9722
9723 @<Determine where the first name ends and von name starts and ends@>=
9724 begin
9725 von_start := 0;
9726 while (von_start < last_end-1) do
9727 begin
9728 name_bf_ptr := name_tok[von_start];
9729 name_bf_xptr := name_tok[von_start+1];
9730 if (von_token_found) then
9731 begin
9732 von_name_ends_and_last_name_starts_stuff;
9733 goto von_found;
9734 end;
9735 incr(von_start);
9736 end; {there's no von name, so}
9737 while (von_start > 0) do {backtrack if there are connected tokens}
9738 begin
9739 if ((lex_class[name_sep_char[von_start]] <> sep_char) or
9740 (name_sep_char[von_start] = tie)) then
9741 goto loop2_exit;
9742 decr(von_start);
9743 end;
9744 loop2_exit:
9745 von_end := von_start;
9746 von_found:
9747 first_end := von_start;
9748 end
9749
9750
9751 @
9752 @^special character@>
9753 It's a von token if there exists a first brace-level-0 letter (or
9754 brace-level-1 special character), and it's in lower case; in this case
9755 we return |true|. The token is in |name_buf|, starting at
9756 |name_bf_ptr| and ending just before |name_bf_xptr|.
9757
9758 @d return_von_found == begin
9759 von_token_found := true;
9760 return;
9761 end
9762
9763 @<Procedures and functions for name-string processing@>=
9764 function von_token_found : boolean;
9765 label exit;
9766 begin
9767 nm_brace_level := 0;
9768 von_token_found := false; {now it's easy to exit if necessary}
9769 while (name_bf_ptr < name_bf_xptr) do
9770 if ((name_buf[name_bf_ptr] >= "A") and
9771 (name_buf[name_bf_ptr] <= "Z")) then
9772 return
9773 else if ((name_buf[name_bf_ptr] >= "a") and
9774 (name_buf[name_bf_ptr] <= "z")) then
9775 return_von_found
9776 else if (name_buf[name_bf_ptr] = left_brace) then
9777 begin
9778 incr(nm_brace_level);
9779 incr(name_bf_ptr);
9780 if ((name_bf_ptr + 2 < name_bf_xptr) and
9781 (name_buf[name_bf_ptr] = backslash)) then
9782 @<Check the \(s)special character (and |return|)@>
9783 else
9784 @<Skip over |name_buf| stuff at |nm_brace_level > 0|@>;
9785 end
9786 else
9787 incr(name_bf_ptr);
9788 exit:
9789 end;
9790
9791
9792 @
9793 @^special character@>
9794 When we come here |name_bf_ptr| is just past the |left_brace|,
9795 but we always leave by |return|ing.
9796
9797 @<Check the \(s)special character (and |return|)@>=
9798 begin
9799 incr(name_bf_ptr); {skip over the |backslash|}
9800 name_bf_yptr := name_bf_ptr;
9801 while ((name_bf_ptr < name_bf_xptr) and
9802 (lex_class[name_buf[name_bf_ptr]] = alpha)) do
9803 incr(name_bf_ptr); {this scans the control sequence}
9804 control_seq_loc := str_lookup(name_buf,name_bf_yptr,name_bf_ptr-name_bf_yptr,
9805 control_seq_ilk,dont_insert);
9806 if (hash_found) then
9807 @<Handle this \(a)accented or foreign character (and |return|)@>;
9808 while ((name_bf_ptr < name_bf_xptr) and (nm_brace_level > 0)) do
9809 begin
9810 if ((name_buf[name_bf_ptr] >= "A") and
9811 (name_buf[name_bf_ptr] <= "Z")) then
9812 return
9813 else if ((name_buf[name_bf_ptr] >= "a") and
9814 (name_buf[name_bf_ptr] <= "z")) then
9815 return_von_found
9816 else if (name_buf[name_bf_ptr] = right_brace) then
9817 decr(nm_brace_level)
9818 else if (name_buf[name_bf_ptr] = left_brace) then
9819 incr(nm_brace_level);
9820 incr(name_bf_ptr);
9821 end;
9822 return;
9823 end
9824
9825
9826 @
9827 @:this can't happen}{\quad Control-sequence hash error@>
9828 The accented or foreign character is either `\.{\\i}' or `\.{\\j}' or
9829 one of the eleven alphabetic foreign characters in Table~3.2 of the
9830 \LaTeX\ manual.
9831
9832 @<Handle this \(a)accented or foreign character (and |return|)@>=
9833 begin
9834 case (ilk_info[control_seq_loc]) of
9835 n_oe_upper,
9836 n_ae_upper,
9837 n_aa_upper,
9838 n_o_upper,
9839 n_l_upper :
9840 return;
9841 n_i,
9842 n_j,
9843 n_oe,
9844 n_ae,
9845 n_aa,
9846 n_o,
9847 n_l,
9848 n_ss :
9849 return_von_found;
9850 othercases
9851 confusion ('Control-sequence hash error')
9852 endcases;
9853 end
9854
9855
9856 @
9857 When we come here |name_bf_ptr| is just past the |left_brace|; when we
9858 leave it's either at |name_bf_xptr| or just past the matching
9859 |right_brace|.
9860
9861 @<Skip over |name_buf| stuff at |nm_brace_level > 0|@>=
9862 while ((nm_brace_level > 0) and (name_bf_ptr < name_bf_xptr)) do
9863 begin
9864 if (name_buf[name_bf_ptr] = right_brace) then
9865 decr(nm_brace_level)
9866 else if (name_buf[name_bf_ptr] = left_brace) then
9867 incr(nm_brace_level);
9868 incr(name_bf_ptr);
9869 end
9870
9871
9872 @
9873 @^Casey Stengel would be proud@>
9874 @^special character@>
9875 @^Tuesdays@>
9876 The last name starts just past the last token, before the first
9877 |comma| (if there is no |comma|, there is deemed to be one at the end
9878 of the string), for which there exists a first brace-level-0 letter
9879 (or brace-level-1 special character), and it's in lower case, unless
9880 this last token is also the last token before the |comma|, in which
9881 case the last name starts with this token (unless this last token is
9882 connected by a |sep_char| other than a |tie| to the previous token, in
9883 which case the last name starts with as many tokens earlier as are
9884 connected by non|tie|s to this last one (except on Tuesdays
9885 $\ldots\,$), although this module never sees such a case). Note that
9886 if there are any tokens in either the von or last names, then the last
9887 name has at least one, even if it starts with a lower-case letter.
9888
9889 @<Procedures and functions for name-string processing@>=
9890 procedure von_name_ends_and_last_name_starts_stuff;
9891 label exit;
9892 begin {there may or may not be a von name}
9893 von_end := last_end - 1;
9894 while (von_end > von_start) do
9895 begin
9896 name_bf_ptr := name_tok[von_end-1];
9897 name_bf_xptr := name_tok[von_end];
9898 if (von_token_found) then
9899 return;
9900 decr(von_end);
9901 end;
9902 exit:
9903 end;
9904
9905
9906 @
9907 This module uses the information in |pop_lit1| to format the name.
9908 Everything at |sp_brace_level = 0| is copied verbatim to the formatted
9909 string; the rest is described in the succeeding modules.
9910
9911 @<Figure out the formatted name@>=
9912 begin
9913 ex_buf_ptr := 0;
9914 sp_brace_level := 0;
9915 sp_ptr := str_start[pop_lit1];
9916 sp_end := str_start[pop_lit1+1];
9917 while (sp_ptr < sp_end) do
9918 if (str_pool[sp_ptr] = left_brace) then
9919 begin
9920 incr(sp_brace_level);
9921 incr(sp_ptr);
9922 @<Format this part of the name@>;
9923 end
9924 else if (str_pool[sp_ptr] = right_brace) then
9925 begin
9926 braces_unbalanced_complaint (pop_lit1);
9927 incr(sp_ptr);
9928 end
9929 else
9930 begin
9931 append_ex_buf_char_and_check (str_pool[sp_ptr]);
9932 incr(sp_ptr);
9933 end;
9934 if (sp_brace_level > 0) then
9935 braces_unbalanced_complaint (pop_lit1);
9936 ex_buf_length := ex_buf_ptr;
9937 end
9938
9939
9940 @
9941 When we arrive here we're at |sp_brace_level = 1|, just past the
9942 |left_brace|. Letters at this |sp_brace_level| other than those
9943 denoting the parts of the name (i.e., the first letters of `first,'
9944 `last,' `von,' and `jr,' ignoring case) are illegal. We do two passes
9945 over this group; the first determines whether we're to output
9946 anything, and, if we are, the second actually outputs it.
9947
9948 @<Format this part of the name@>=
9949 begin
9950 sp_xptr1 := sp_ptr;
9951 alpha_found := false;
9952 double_letter := false;
9953 end_of_group := false;
9954 to_be_written := true;
9955 while ((not end_of_group) and (sp_ptr < sp_end)) do
9956 if (lex_class[str_pool[sp_ptr]] = alpha) then
9957 begin
9958 incr(sp_ptr);
9959 @<Figure out what this letter means@>;
9960 end
9961 else if (str_pool[sp_ptr] = right_brace) then
9962 begin
9963 decr(sp_brace_level);
9964 incr(sp_ptr);
9965 end_of_group := true;
9966 end
9967 else if (str_pool[sp_ptr] = left_brace) then
9968 begin
9969 incr(sp_brace_level);
9970 incr(sp_ptr);
9971 skip_stuff_at_sp_brace_level_greater_than_one;
9972 end
9973 else
9974 incr(sp_ptr);
9975 if ((end_of_group) and (to_be_written)) then {do the second pass}
9976 @<Finally format this part of the name@>;
9977 end
9978
9979
9980 @
9981 When we come here |sp_ptr| is just past the |left_brace|, and when we
9982 leave it's either at |sp_end| or just past the matching |right_brace|.
9983
9984 @<Procedures and functions for name-string processing@>=
9985 procedure skip_stuff_at_sp_brace_level_greater_than_one;
9986 begin
9987 while ((sp_brace_level > 1) and (sp_ptr < sp_end)) do
9988 begin
9989 if (str_pool[sp_ptr] = right_brace) then
9990 decr(sp_brace_level)
9991 else if (str_pool[sp_ptr] = left_brace) then
9992 incr(sp_brace_level);
9993 incr(sp_ptr);
9994 end;
9995 end;
9996
9997
9998 @
9999 We won't output anything for this part of the name if this is a second
10000 occurrence of an |sp_brace_level = 1| letter, if it's an illegal
10001 letter, or if there are no tokens corresponding to this part. We also
10002 determine if we're we to output complete tokens (indicated by a double
10003 letter).
10004
10005 @<Figure out what this letter means@>=
10006 begin
10007 if (alpha_found) then
10008 begin
10009 brace_lvl_one_letters_complaint;
10010 to_be_written := false;
10011 end
10012 else
10013 begin
10014 case (str_pool[sp_ptr-1]) of
10015 "f","F" : @<Figure out what tokens we'll output for the `first' name@>;
10016 "v","V" : @<Figure out what tokens we'll output for the `von' name@>;
10017 "l","L" : @<Figure out what tokens we'll output for the `last' name@>;
10018 "j","J" : @<Figure out what tokens we'll output for the `jr' name@>;
10019 othercases
10020 begin
10021 brace_lvl_one_letters_complaint;
10022 to_be_written := false;
10023 end
10024 endcases;
10025 if (double_letter) then
10026 incr(sp_ptr);
10027 end;
10028 alpha_found := true;
10029 end
10030
10031
10032 @
10033 At most one of the important letters, perhaps doubled, may appear at
10034 |sp_brace_level = 1|.
10035
10036 @<Procedures and functions for name-string processing@>=
10037 procedure brace_lvl_one_letters_complaint;
10038 begin
10039 print ('The format string "');
10040 print_pool_str (pop_lit1);
10041 bst_ex_warn ('" has an illegal brace-level-1 letter');
10042 end;
10043
10044
10045 @
10046 Here we set pointers into |name_tok| and note whether we'll be dealing
10047 with a full first-name tokens (|double_letter = true|) or
10048 abbreviations (|double_letter = false|).
10049
10050 @<Figure out what tokens we'll output for the `first' name@>=
10051 begin
10052 cur_token := first_start;
10053 last_token := first_end;
10054 if (cur_token = last_token) then
10055 to_be_written := false;
10056 if ((str_pool[sp_ptr] = "f") or (str_pool[sp_ptr] = "F")) then
10057 double_letter := true;
10058 end
10059
10060
10061 @
10062 The same as above but for von-name tokens.
10063
10064 @<Figure out what tokens we'll output for the `von' name@>=
10065 begin
10066 cur_token := von_start;
10067 last_token := von_end;
10068 if (cur_token = last_token) then
10069 to_be_written := false;
10070 if ((str_pool[sp_ptr] = "v") or (str_pool[sp_ptr] = "V")) then
10071 double_letter := true;
10072 end
10073
10074
10075 @
10076 The same as above but for last-name tokens.
10077
10078 @<Figure out what tokens we'll output for the `last' name@>=
10079 begin
10080 cur_token := von_end;
10081 last_token := last_end;
10082 if (cur_token = last_token) then
10083 to_be_written := false;
10084 if ((str_pool[sp_ptr] = "l") or (str_pool[sp_ptr] = "L")) then
10085 double_letter := true;
10086 end
10087
10088
10089 @
10090 The same as above but for jr-name tokens.
10091
10092 @<Figure out what tokens we'll output for the `jr' name@>=
10093 begin
10094 cur_token := last_end;
10095 last_token := jr_end;
10096 if (cur_token = last_token) then
10097 to_be_written := false;
10098 if ((str_pool[sp_ptr] = "j") or (str_pool[sp_ptr] = "J")) then
10099 double_letter := true;
10100 end
10101
10102
10103 @
10104 This is the second pass over this part of the name; here we actually
10105 write stuff out to |ex_buf|.
10106
10107 @<Finally format this part of the name@>=
10108 begin
10109 ex_buf_xptr := ex_buf_ptr;
10110 sp_ptr := sp_xptr1;
10111 sp_brace_level := 1;
10112 while (sp_brace_level > 0) do
10113 if ((lex_class[str_pool[sp_ptr]] = alpha) and (sp_brace_level = 1)) then
10114 begin
10115 incr(sp_ptr);
10116 @<Figure out how to output the name tokens, and do it@>;
10117 end
10118 else if (str_pool[sp_ptr] = right_brace) then
10119 begin
10120 decr(sp_brace_level);
10121 incr(sp_ptr);
10122 if (sp_brace_level > 0) then
10123 append_ex_buf_char_and_check (right_brace);
10124 end
10125 else if (str_pool[sp_ptr] = left_brace) then
10126 begin
10127 incr(sp_brace_level);
10128 incr(sp_ptr);
10129 append_ex_buf_char_and_check (left_brace);
10130 end
10131 else
10132 begin
10133 append_ex_buf_char_and_check (str_pool[sp_ptr]);
10134 incr(sp_ptr);
10135 end;
10136 if (ex_buf_ptr > 0) then
10137 if (ex_buf[ex_buf_ptr-1] = tie) then
10138 @<Handle a discretionary |tie|@>;
10139 end
10140
10141
10142 @
10143 When we come here, |sp_ptr| is just past the letter indicating the
10144 part of the name for which we're about to output tokens. When we
10145 leave, it's at the first character of the rest of the group.
10146
10147 @<Figure out how to output the name tokens, and do it@>=
10148 begin
10149 if (double_letter) then
10150 incr(sp_ptr);
10151 use_default := true;
10152 sp_xptr2 := sp_ptr;
10153 if (str_pool[sp_ptr] = left_brace) then {find the inter-token string}
10154 begin
10155 use_default := false;
10156 incr(sp_brace_level);
10157 incr(sp_ptr);
10158 sp_xptr1 := sp_ptr;
10159 skip_stuff_at_sp_brace_level_greater_than_one;
10160 sp_xptr2 := sp_ptr - 1;
10161 end;
10162 @<Finally output the name tokens@>;
10163 if (not use_default) then
10164 sp_ptr := sp_xptr2 + 1;
10165 end
10166
10167
10168 @
10169 Here, for each token in this part, we output either a full or an
10170 abbreviated token and the inter-token string for all but the last
10171 token of this part.
10172
10173 @<Finally output the name tokens@>=
10174 while (cur_token < last_token) do
10175 begin
10176 if (double_letter) then
10177 @<Finally output a full token@>
10178 else
10179 @<Finally output an abbreviated token@>;
10180 incr(cur_token);
10181 if (cur_token < last_token) then
10182 @<Finally output the inter-token string@>;
10183 end
10184
10185
10186 @
10187 @:BibTeX capacity exceeded}{\quad buffer size@>
10188 Here we output all the characters in the token, verbatim.
10189
10190 @<Finally output a full token@>=
10191 begin
10192 name_bf_ptr := name_tok[cur_token];
10193 name_bf_xptr := name_tok[cur_token+1];
10194 if (ex_buf_length+(name_bf_xptr-name_bf_ptr) > buf_size) then
10195 buffer_overflow;
10196 while (name_bf_ptr < name_bf_xptr) do
10197 begin
10198 append_ex_buf_char (name_buf[name_bf_ptr]);
10199 incr(name_bf_ptr);
10200 end;
10201 end
10202
10203
10204 @
10205 @^special character@>
10206 Here we output the first alphabetic or special character of the token;
10207 brace level is irrelevant for an alphabetic (but not a special)
10208 character.
10209
10210 @<Finally output an abbreviated token@>=
10211 begin
10212 name_bf_ptr := name_tok[cur_token];
10213 name_bf_xptr := name_tok[cur_token+1];
10214 while (name_bf_ptr < name_bf_xptr) do
10215 begin
10216 if (lex_class[name_buf[name_bf_ptr]] = alpha) then
10217 begin
10218 append_ex_buf_char_and_check (name_buf[name_bf_ptr]);
10219 goto loop_exit;
10220 end
10221 else if ((name_buf[name_bf_ptr] = left_brace) and
10222 (name_bf_ptr + 1 < name_bf_xptr)) then
10223 if (name_buf[name_bf_ptr+1] = backslash) then
10224 @<Finally output a special character and exit loop@>;
10225 incr(name_bf_ptr);
10226 end;
10227 loop_exit:
10228 end
10229
10230
10231 @
10232 @^special character@>
10233 @^user abuse@>
10234 @:BibTeX capacity exceeded}{\quad buffer size@>
10235 We output a special character here even if the user has been silly
10236 enough to make it nonalphabetic (and even if the user has been sillier
10237 still by not having a matching |right_brace|).
10238
10239 @<Finally output a special character and exit loop@>=
10240 begin
10241 if (ex_buf_ptr + 2 > buf_size) then
10242 buffer_overflow;
10243 append_ex_buf_char (left_brace);
10244 append_ex_buf_char (backslash);
10245 name_bf_ptr := name_bf_ptr + 2;
10246 nm_brace_level := 1;
10247 while ((name_bf_ptr < name_bf_xptr) and (nm_brace_level > 0)) do
10248 begin
10249 if (name_buf[name_bf_ptr] = right_brace) then
10250 decr(nm_brace_level)
10251 else if (name_buf[name_bf_ptr] = left_brace) then
10252 incr(nm_brace_level);
10253 append_ex_buf_char_and_check (name_buf[name_bf_ptr]);
10254 incr(name_bf_ptr);
10255 end;
10256 goto loop_exit;
10257 end
10258
10259
10260 @
10261 @:BibTeX capacity exceeded}{\quad buffer size@>
10262 Here we output either the \.{.bst} given string if it exists, or else
10263 the \.{.bib} |sep_char| if it exists, or else the default string. A
10264 |tie| is the default space character between the last two tokens of
10265 the name part, and between the first two tokens if the first token is
10266 short enough; otherwise, a |space| is the default.
10267
10268 @d long_token = 3 {a token this length or longer is ``long''}
10269
10270 @<Finally output the inter-token string@>=
10271 begin
10272 if (use_default) then
10273 begin
10274 if (not double_letter) then
10275 append_ex_buf_char_and_check (period);
10276 if (lex_class[name_sep_char[cur_token]] = sep_char) then
10277 append_ex_buf_char_and_check (name_sep_char[cur_token])
10278 else if ((cur_token = last_token-1) or
10279 (not enough_text_chars (long_token))) then
10280 append_ex_buf_char_and_check (tie)
10281 else
10282 append_ex_buf_char_and_check (space);
10283 end
10284 else
10285 begin
10286 if (ex_buf_length+(sp_xptr2-sp_xptr1) > buf_size) then
10287 buffer_overflow;
10288 sp_ptr := sp_xptr1;
10289 while (sp_ptr < sp_xptr2) do
10290 begin
10291 append_ex_buf_char (str_pool[sp_ptr]);
10292 incr(sp_ptr);
10293 end
10294 end;
10295 end
10296
10297
10298 @
10299 @^special character@>
10300 This function looks at the string in |ex_buf|, starting at
10301 |ex_buf_xptr| and ending just before |ex_buf_ptr|, and it returns
10302 |true| if there are |enough_chars|, where a special character (even if
10303 it's missing its matching |right_brace|) counts as a single character.
10304 This procedure is called only for strings that don't have too many
10305 |right_brace|s.
10306
10307 @<Procedures and functions for name-string processing@>=
10308 function enough_text_chars (@!enough_chars : buf_pointer) : boolean;
10309 begin
10310 num_text_chars := 0;
10311 ex_buf_yptr := ex_buf_xptr;
10312 while ((ex_buf_yptr < ex_buf_ptr) and (num_text_chars < enough_chars)) do
10313 begin
10314 incr(ex_buf_yptr);
10315 if (ex_buf[ex_buf_yptr-1] = left_brace) then
10316 begin
10317 incr(brace_level);
10318 if ((brace_level = 1) and (ex_buf_yptr < ex_buf_ptr)) then
10319 if (ex_buf[ex_buf_yptr] = backslash) then
10320 begin
10321 incr(ex_buf_yptr); {skip over the |backslash|}
10322 while ((ex_buf_yptr < ex_buf_ptr) and (brace_level > 0)) do
10323 begin
10324 if (ex_buf[ex_buf_yptr] = right_brace) then
10325 decr(brace_level)
10326 else if (ex_buf[ex_buf_yptr] = left_brace) then
10327 incr(brace_level);
10328 incr(ex_buf_yptr);
10329 end;
10330 end;
10331 end
10332 else if (ex_buf[ex_buf_yptr-1] = right_brace) then
10333 decr(brace_level);
10334 incr(num_text_chars);
10335 end;
10336 if (num_text_chars < enough_chars) then
10337 enough_text_chars := false
10338 else
10339 enough_text_chars := true;
10340 end;
10341
10342
10343 @
10344 If the last character output for this name part is a |tie| but the
10345 previous character it isn't, we're dealing with a discretionary |tie|;
10346 thus we replace it by a |space| if there are enough characters in the
10347 rest of the name part.
10348
10349 @d long_name = 3 {a name this length or longer is ``long''}
10350
10351 @<Handle a discretionary |tie|@>=
10352 begin
10353 decr(ex_buf_ptr); {remove the previous |tie|}
10354 if (ex_buf[ex_buf_ptr-1] = tie) then {it's not a discretionary |tie|}
10355 do_nothing
10356 else if (not enough_text_chars (long_name)) then {this is a short name part}
10357 incr(ex_buf_ptr) {so restore the |tie|}
10358 else {replace it by a |space|}
10359 append_ex_buf_char (space);
10360 end
10361
10362
10363 @
10364 This is a procedure so that |x_format_name| is smaller.
10365
10366 @<Procedures and functions for name-string processing@>=
10367 procedure figure_out_the_formatted_name;
10368 label loop_exit;
10369 begin
10370 @<Figure out the formatted name@>;
10371 end;
10372
10373
10374 @
10375 The |built_in| function {\.{if\$}} pops the top three literals (they
10376 are two function literals and an integer literal, in that order); if
10377 the integer is greater than 0, it executes the second literal, else it
10378 executes the first. If any of the types is incorrect, it complains
10379 but does nothing else.
10380
10381 @<|execute_fn|({\.{if\$}})@>=
10382 begin
10383 pop_lit_stk (pop_lit1,pop_typ1);
10384 pop_lit_stk (pop_lit2,pop_typ2);
10385 pop_lit_stk (pop_lit3,pop_typ3);
10386 if (pop_typ1 <> stk_fn) then
10387 print_wrong_stk_lit (pop_lit1,pop_typ1,stk_fn)
10388 else if (pop_typ2 <> stk_fn) then
10389 print_wrong_stk_lit (pop_lit2,pop_typ2,stk_fn)
10390 else if (pop_typ3 <> stk_int) then
10391 print_wrong_stk_lit (pop_lit3,pop_typ3,stk_int)
10392 else
10393 if (pop_lit3 > 0) then
10394 execute_fn (pop_lit2)
10395 else
10396 execute_fn (pop_lit1);
10397 end
10398
10399
10400 @
10401 The |built_in| function {\.{int.to.chr\$}} pops the top (integer)
10402 literal, interpreted as the |ASCII_code| of a single character,
10403 converts it to the corresponding single-character string, and pushes
10404 this string. If the literal isn't an appropriate integer, it
10405 complains and pushes the null string.
10406
10407 @<|execute_fn|({\.{int.to.chr\$}})@>=
10408 procedure x_int_to_chr;
10409 begin
10410 pop_lit_stk (pop_lit1,pop_typ1);
10411 if (pop_typ1 <> stk_int) then
10412 begin
10413 print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
10414 push_lit_stk (s_null, stk_str);
10415 end
10416 else if ((pop_lit1 < 0) or (pop_lit1 > 127)) then
10417 begin
10418 bst_ex_warn (pop_lit1:0,' isn''t valid ASCII');
10419 push_lit_stk (s_null, stk_str);
10420 end
10421 else
10422 begin
10423 str_room(1);
10424 append_char (pop_lit1);
10425 push_lit_stk (make_string, stk_str);
10426 end;
10427 end;
10428
10429
10430 @
10431 The |built_in| function {\.{int.to.str\$}} pops the top (integer)
10432 literal, converts it to its (unique) string equivalent, and pushes
10433 this string. If the literal isn't an integer, it complains and pushes
10434 the null string.
10435
10436 @<|execute_fn|({\.{int.to.str\$}})@>=
10437 procedure x_int_to_str;
10438 begin
10439 pop_lit_stk (pop_lit1,pop_typ1);
10440 if (pop_typ1 <> stk_int) then
10441 begin
10442 print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
10443 push_lit_stk (s_null, stk_str);
10444 end
10445 else
10446 begin
10447 int_to_ASCII (pop_lit1, ex_buf, 0, ex_buf_length);@/
10448 add_pool_buf_and_push; {push this string onto the stack}
10449 end;
10450 end;
10451
10452
10453 @
10454 The |built_in| function {\.{missing\$}} pops the top literal and
10455 pushes the integer 1 if it's a missing field, 0 otherwise. If the
10456 literal isn't a missing field or a string, it complains and pushes 0.
10457 Unlike \.{empty\$}, this function should be called only when
10458 |mess_with_entries| is true.
10459
10460 @<|execute_fn|({\.{missing\$}})@>=
10461 procedure x_missing;
10462 begin
10463 pop_lit_stk (pop_lit1,pop_typ1);
10464 if (not mess_with_entries) then
10465 bst_cant_mess_with_entries_print
10466 else if ((pop_typ1 <> stk_str) and (pop_typ1 <> stk_field_missing)) then
10467 begin
10468 if (pop_typ1 <> stk_empty) then
10469 begin
10470 print_stk_lit (pop_lit1,pop_typ1);
10471 bst_ex_warn (', not a string or missing field,');
10472 end;
10473 push_lit_stk (0, stk_int);
10474 end
10475 else
10476 if (pop_typ1 = stk_field_missing) then
10477 push_lit_stk (1, stk_int)
10478 else
10479 push_lit_stk (0, stk_int);
10480 end;
10481
10482
10483 @
10484 The |built_in| function {\.{newline\$}} writes whatever has
10485 accumulated in the output buffer |out_buf| onto the \.{.bbl} file.
10486
10487 @<|execute_fn|({\.{newline\$}})@>=
10488 begin
10489 output_bbl_line;
10490 end
10491
10492
10493 @
10494 The |built_in| function {\.{num.names\$}} pops the top (string)
10495 literal; it pushes the number of names the string represents---one
10496 plus the number of occurrences of the substring ``and'' (ignoring case
10497 differences) surrounded by nonnull |white_space| at the top brace
10498 level. If the literal isn't a string, it complains and pushes the
10499 value 0.
10500
10501 @<|execute_fn|({\.{num.names\$}})@>=
10502 procedure x_num_names;
10503 begin
10504 pop_lit_stk (pop_lit1,pop_typ1);
10505 if (pop_typ1 <> stk_str) then
10506 begin
10507 print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
10508 push_lit_stk (0, stk_int);
10509 end
10510 else
10511 begin
10512 ex_buf_length := 0;
10513 add_buf_pool (pop_lit1);
10514 @<Determine the number of names@>;
10515 push_lit_stk (num_names, stk_int);
10516 end;
10517 end;
10518
10519
10520 @
10521 This module, while scanning the list of names, counts the occurrences
10522 of ``and'' (ignoring case differences) surrounded by nonnull
10523 |white_space|, and adds 1.
10524
10525 @<Determine the number of names@>=
10526 begin
10527 ex_buf_ptr := 0;
10528 num_names := 0;
10529 while (ex_buf_ptr < ex_buf_length) do
10530 begin
10531 name_scan_for_and (pop_lit1);
10532 incr(num_names);
10533 end;
10534 end
10535
10536
10537 @
10538 The |built_in| function {\.{pop\$}} pops the top of the stack but
10539 doesn't print it.
10540
10541 @<|execute_fn|({\.{pop\$}})@>=
10542 begin
10543 pop_lit_stk (pop_lit1,pop_typ1);
10544 end
10545
10546
10547 @
10548 The |built_in| function {\.{preamble\$}} pushes onto the stack the
10549 concatenation of all the \.{preamble} strings read from the database
10550 files.
10551
10552 @<|execute_fn|({\.{preamble\$}})@>=
10553 procedure x_preamble;
10554 begin
10555 ex_buf_length := 0;
10556 preamble_ptr := 0;
10557 while (preamble_ptr < num_preamble_strings) do
10558 begin
10559 add_buf_pool (s_preamble[preamble_ptr]);
10560 incr(preamble_ptr);
10561 end;
10562 add_pool_buf_and_push; {push the concatenation string onto the stack}
10563 end;
10564
10565
10566 @
10567 @^special character@>
10568 The |built_in| function {\.{purify\$}} pops the top (string) literal,
10569 removes nonalphanumeric characters except for |white_space| and
10570 |sep_char| characters (these get converted to a |space|) and removes
10571 certain alphabetic characters contained in the control sequences
10572 associated with a special character, and pushes the resulting string.
10573 If the literal isn't a string, it complains and pushes the null
10574 string.
10575
10576 @<|execute_fn|({\.{purify\$}})@>=
10577 procedure x_purify;
10578 begin
10579 pop_lit_stk (pop_lit1,pop_typ1);
10580 if (pop_typ1 <> stk_str) then
10581 begin
10582 print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
10583 push_lit_stk (s_null, stk_str);
10584 end
10585 else
10586 begin
10587 ex_buf_length := 0;
10588 add_buf_pool (pop_lit1);
10589 @<Perform the purification@>;
10590 add_pool_buf_and_push; {push this string onto the stack}
10591 end;
10592 end;
10593
10594
10595 @
10596 @^special character@>
10597 The resulting string has nonalphanumeric characters removed, and each
10598 |white_space| or |sep_char| character converted to a |space|. The next
10599 module handles special characters. This code doesn't complain if the
10600 string isn't brace balanced.
10601
10602 @<Perform the purification@>=
10603 begin
10604 brace_level := 0; {this is the top level}
10605 ex_buf_xptr := 0; {this pointer is for the purified string}
10606 ex_buf_ptr := 0; {and this one is for the original string}
10607 while (ex_buf_ptr < ex_buf_length) do
10608 begin
10609 case (lex_class[ex_buf[ex_buf_ptr]]) of
10610 white_space,
10611 sep_char :
10612 begin
10613 ex_buf[ex_buf_xptr] := space;
10614 incr(ex_buf_xptr);
10615 end;
10616 alpha,
10617 numeric :
10618 begin
10619 ex_buf[ex_buf_xptr] := ex_buf[ex_buf_ptr];
10620 incr(ex_buf_xptr);
10621 end;
10622 othercases
10623 if (ex_buf[ex_buf_ptr] = left_brace) then
10624 begin
10625 incr(brace_level);
10626 if ((brace_level = 1) and
10627 (ex_buf_ptr + 1 < ex_buf_length)) then
10628 if (ex_buf[ex_buf_ptr+1] = backslash) then
10629 @<Purify a special character@>;
10630 end
10631 else if (ex_buf[ex_buf_ptr] = right_brace) then
10632 if (brace_level > 0) then
10633 decr(brace_level)
10634 endcases;
10635 incr(ex_buf_ptr);
10636 end;
10637 ex_buf_length := ex_buf_xptr;
10638 end
10639
10640
10641 @
10642 @^special character@>
10643 Special characters (even without a matching |right_brace|) are
10644 purified by removing the control sequences (but restoring the correct
10645 thing for `\.{\\i}' and `\.{\\j}' as well as the eleven alphabetic
10646 foreign characters in Table~3.2 of the \LaTeX\ manual) and removing
10647 all nonalphanumeric characters (including |white_space| and
10648 |sep_char|s).
10649
10650 @<Purify a special character@>=
10651 begin
10652 incr(ex_buf_ptr); {skip over the |left_brace|}
10653 while ((ex_buf_ptr < ex_buf_length) and (brace_level > 0)) do
10654 begin
10655 incr(ex_buf_ptr); {skip over the |backslash|}
10656 ex_buf_yptr := ex_buf_ptr; {mark the beginning of the control sequence}
10657 while ((ex_buf_ptr < ex_buf_length) and
10658 (lex_class[ex_buf[ex_buf_ptr]] = alpha)) do@/
10659 incr(ex_buf_ptr); {this scans the control sequence}
10660 control_seq_loc := str_lookup(ex_buf,ex_buf_yptr,ex_buf_ptr-ex_buf_yptr,
10661 control_seq_ilk,dont_insert);
10662 if (hash_found) then
10663 @<Purify this accented or foreign character@>;
10664 while ((ex_buf_ptr < ex_buf_length) and (brace_level > 0) and
10665 (ex_buf[ex_buf_ptr] <> backslash)) do
10666 begin {this scans to the next control sequence}
10667 case (lex_class[ex_buf[ex_buf_ptr]]) of
10668 alpha,
10669 numeric :
10670 begin
10671 ex_buf[ex_buf_xptr] := ex_buf[ex_buf_ptr];
10672 incr(ex_buf_xptr);
10673 end;
10674 othercases
10675 if (ex_buf[ex_buf_ptr] = right_brace) then
10676 decr(brace_level)
10677 else if (ex_buf[ex_buf_ptr] = left_brace) then
10678 incr(brace_level)
10679 endcases;
10680 incr(ex_buf_ptr);
10681 end;
10682 end;
10683 decr(ex_buf_ptr); {unskip the |right_brace| (or last character)}
10684 end
10685
10686
10687 @
10688 We consider the purified character to be either the first alphabetic
10689 character of its control sequence, or perhaps both alphabetic
10690 characters.
10691
10692 @<Purify this accented or foreign character@>=
10693 begin
10694 ex_buf[ex_buf_xptr] := ex_buf[ex_buf_yptr]; {the first alphabetic character}
10695 incr(ex_buf_xptr);
10696 case (ilk_info[control_seq_loc]) of
10697 n_oe,
10698 n_oe_upper,
10699 n_ae,
10700 n_ae_upper,
10701 n_ss :
10702 begin {and the second}
10703 ex_buf[ex_buf_xptr] := ex_buf[ex_buf_yptr+1];
10704 incr(ex_buf_xptr);
10705 end;
10706 othercases
10707 do_nothing
10708 endcases;
10709 end
10710
10711
10712 @
10713 The |built_in| function {\.{quote\$}} pushes the string consisting of
10714 the |double_quote| character.
10715
10716 @<|execute_fn|({\.{quote\$}})@>=
10717 procedure x_quote;
10718 begin
10719 str_room(1);
10720 append_char (double_quote);
10721 push_lit_stk (make_string, stk_str);
10722 end;
10723
10724
10725 @
10726 The |built_in| function {\.{skip\$}} is a no-op.
10727
10728 @<|execute_fn|({\.{skip\$}})@>=
10729 begin
10730 do_nothing;
10731 end
10732
10733
10734 @
10735 The |built_in| function {\.{stack\$}} pops and prints the whole stack;
10736 it's meant to be used for style designers while debugging.
10737
10738 @<|execute_fn|({\.{stack\$}})@>=
10739 begin
10740 pop_whole_stack;
10741 end
10742
10743
10744 @
10745 @^push the literal stack@>
10746 The |built_in| function {\.{substring\$}} pops the top three literals
10747 (they are the two integers literals |pop_lit1| and |pop_lit2| and a
10748 string literal, in that order). It pushes the substring of the (at
10749 most) |pop_lit1| consecutive characters starting at the |pop_lit2|th
10750 character (assuming 1-based indexing) if |pop_lit2| is positive, and
10751 ending at the |-pop_lit2|th character from the end if |pop_lit2| is
10752 negative (where the first character from the end is the last
10753 character). If any of the types is incorrect, it complain and pushes
10754 the null string.
10755
10756 @<|execute_fn|({\.{substring\$}})@>=
10757 procedure x_substring;
10758 label exit;
10759 begin
10760 pop_lit_stk (pop_lit1,pop_typ1);
10761 pop_lit_stk (pop_lit2,pop_typ2);
10762 pop_lit_stk (pop_lit3,pop_typ3);
10763 if (pop_typ1 <> stk_int) then
10764 begin
10765 print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
10766 push_lit_stk (s_null, stk_str);
10767 end
10768 else if (pop_typ2 <> stk_int) then
10769 begin
10770 print_wrong_stk_lit (pop_lit2,pop_typ2,stk_int);
10771 push_lit_stk (s_null, stk_str);
10772 end
10773 else if (pop_typ3 <> stk_str) then
10774 begin
10775 print_wrong_stk_lit (pop_lit3,pop_typ3,stk_str);
10776 push_lit_stk (s_null, stk_str);
10777 end
10778 else
10779 begin
10780 sp_length := length(pop_lit3);
10781 if (pop_lit1 >= sp_length) then
10782 if ((pop_lit2 = 1) or (pop_lit2 = -1)) then
10783 begin
10784 repush_string;
10785 return;
10786 end;
10787 if ((pop_lit1 <= 0) or (pop_lit2 = 0) or (pop_lit2 > sp_length) or
10788 (pop_lit2 < -sp_length)) then
10789 begin
10790 push_lit_stk (s_null, stk_str);
10791 return;
10792 end
10793 else
10794 @<Form the appropriate substring@>;
10795 end;
10796 exit:
10797 end;
10798
10799
10800 @
10801 @^push the literal stack@>
10802 This module finds the substring as described in the last section,
10803 and slides it into place in the string pool, if necessary.
10804
10805 @<Form the appropriate substring@>=
10806 begin
10807 if (pop_lit2 > 0) then
10808 begin
10809 if (pop_lit1 > sp_length - (pop_lit2-1)) then
10810 pop_lit1 := sp_length - (pop_lit2-1);
10811 sp_ptr := str_start[pop_lit3] + (pop_lit2-1);
10812 sp_end := sp_ptr + pop_lit1;
10813 if (pop_lit2 = 1) then
10814 if (pop_lit3 >= cmd_str_ptr) then {no shifting---merely change pointers}
10815 begin
10816 str_start[pop_lit3+1] := sp_end;
10817 unflush_string;
10818 incr(lit_stk_ptr);
10819 return;
10820 end;
10821 end
10822 else {|-ex_buf_length <= pop_lit2 < 0|}
10823 begin
10824 pop_lit2 := -pop_lit2;
10825 if (pop_lit1 > sp_length - (pop_lit2-1)) then
10826 pop_lit1 := sp_length - (pop_lit2-1);
10827 sp_end := str_start[pop_lit3+1] - (pop_lit2-1);
10828 sp_ptr := sp_end - pop_lit1;
10829 end;
10830 while (sp_ptr < sp_end) do {shift the substring}
10831 begin
10832 append_char (str_pool[sp_ptr]);
10833 incr(sp_ptr);
10834 end;
10835 push_lit_stk (make_string, stk_str); {and push it onto the stack}
10836 end
10837
10838
10839 @
10840 The |built_in| function {\.{swap\$}} pops the top two literals from
10841 the stack and pushes them back swapped.
10842
10843 @<|execute_fn|({\.{swap\$}})@>=
10844 procedure x_swap;
10845 begin
10846 pop_lit_stk (pop_lit1,pop_typ1);
10847 pop_lit_stk (pop_lit2,pop_typ2);
10848 if ((pop_typ1 <> stk_str) or (pop_lit1 < cmd_str_ptr)) then
10849 begin
10850 push_lit_stk (pop_lit1, pop_typ1);
10851 if ((pop_typ2 = stk_str) and (pop_lit2 >= cmd_str_ptr)) then
10852 unflush_string;
10853 push_lit_stk (pop_lit2, pop_typ2);
10854 end
10855 else if ((pop_typ2 <> stk_str) or (pop_lit2 < cmd_str_ptr)) then
10856 begin
10857 unflush_string; {this is |pop_lit1|}
10858 push_lit_stk (pop_lit1, stk_str);
10859 push_lit_stk (pop_lit2, pop_typ2);
10860 end
10861 else {bummer, both are recent strings}
10862 @<Swap the two strings (they're at the end of |str_pool|)@>;
10863 end;
10864
10865
10866 @
10867 We have to swap both (a)~the strings at the end of the string pool,
10868 and (b)~their pointers on the literal stack.
10869
10870 @<Swap the two strings (they're at the end of |str_pool|)@>=
10871 begin
10872 ex_buf_length := 0;
10873 add_buf_pool (pop_lit2); {save the second string}
10874 sp_ptr := str_start[pop_lit1];
10875 sp_end := str_start[pop_lit1+1];
10876 while (sp_ptr < sp_end) do {slide the first string down}
10877 begin
10878 append_char (str_pool[sp_ptr]);
10879 incr(sp_ptr);
10880 end;
10881 push_lit_stk (make_string, stk_str); {and push it onto the stack}
10882 add_pool_buf_and_push; {push second string onto the stack}
10883 end
10884
10885
10886 @
10887 @^special character@>
10888 The |built_in| function {\.{text.length\$}} pops the top (string)
10889 literal, and pushes the number of text characters it contains, where
10890 an accented character (more precisely, a ``special character''$\!$,
10891 defined earlier) counts as a single text character, even if it's
10892 missing its matching |right_brace|, and where braces don't count as
10893 text characters. If the literal isn't a string, it complains and
10894 pushes the null string.
10895
10896 @<|execute_fn|({\.{text.length\$}})@>=
10897 procedure x_text_length;
10898 begin
10899 pop_lit_stk (pop_lit1,pop_typ1);
10900 if (pop_typ1 <> stk_str) then
10901 begin
10902 print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
10903 push_lit_stk (s_null, stk_str);
10904 end
10905 else
10906 begin
10907 num_text_chars := 0;
10908 @<Count the text characters@>;
10909 push_lit_stk (num_text_chars, stk_int); {and push it onto the stack}
10910 end;
10911 end;
10912
10913
10914 @
10915 @^special character@>
10916 Here we determine the number of text characters in the string, where
10917 an entire special character counts as a single text character (even if
10918 it's missing its matching |right_brace|), and where braces don't count
10919 as text characters.
10920
10921 @<Count the text characters@>=
10922 begin
10923 sp_ptr := str_start[pop_lit1];
10924 sp_end := str_start[pop_lit1+1];
10925 sp_brace_level := 0;
10926 while (sp_ptr < sp_end) do
10927 begin
10928 incr(sp_ptr);
10929 if (str_pool[sp_ptr-1] = left_brace) then
10930 begin
10931 incr(sp_brace_level);
10932 if ((sp_brace_level = 1) and (sp_ptr < sp_end)) then
10933 if (str_pool[sp_ptr] = backslash) then
10934 begin
10935 incr(sp_ptr); {skip over the |backslash|}
10936 while ((sp_ptr < sp_end) and (sp_brace_level > 0)) do
10937 begin
10938 if (str_pool[sp_ptr] = right_brace) then
10939 decr(sp_brace_level)
10940 else if (str_pool[sp_ptr] = left_brace) then
10941 incr(sp_brace_level);
10942 incr(sp_ptr);
10943 end;
10944 incr(num_text_chars);
10945 end;
10946 end
10947 else if (str_pool[sp_ptr-1] = right_brace) then
10948 begin
10949 if (sp_brace_level > 0) then
10950 decr(sp_brace_level);
10951 end
10952 else
10953 incr(num_text_chars);
10954 end;
10955 end
10956
10957
10958 @
10959 @^special character@>
10960 The |built_in| function {\.{text.prefix\$}} pops the top two literals
10961 (the integer literal |pop_lit1| and a string literal, in that order).
10962 It pushes the substring of the (at most) |pop_lit1| consecutive text
10963 characters starting from the beginning of the string. This function
10964 is similar to {\.{substring\$}}, but this one considers an accented
10965 character (or more precisely, a ``special character''$\!$, even if
10966 it's missing its matching |right_brace|) to be a single text character
10967 (rather than however many |ASCII_code| characters it actually
10968 comprises), and this function doesn't consider braces to be text
10969 characters; furthermore, this function appends any needed matching
10970 |right_brace|s. If any of the types is incorrect, it complains and
10971 pushes the null string.
10972
10973 @<|execute_fn|({\.{text.prefix\$}})@>=
10974 procedure x_text_prefix;
10975 label exit;
10976 begin
10977 pop_lit_stk (pop_lit1,pop_typ1);
10978 pop_lit_stk (pop_lit2,pop_typ2);
10979 if (pop_typ1 <> stk_int) then
10980 begin
10981 print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
10982 push_lit_stk (s_null, stk_str);
10983 end
10984 else if (pop_typ2 <> stk_str) then
10985 begin
10986 print_wrong_stk_lit (pop_lit2,pop_typ2,stk_str);
10987 push_lit_stk (s_null, stk_str);
10988 end
10989 else if (pop_lit1 <= 0) then
10990 begin
10991 push_lit_stk (s_null, stk_str);
10992 return;
10993 end
10994 else
10995 @<Form the appropriate prefix@>;
10996 exit:
10997 end;
10998
10999
11000 @
11001 @^push the literal stack@>
11002 This module finds the prefix as described in the last section, and
11003 appends any needed matching |right_brace|s.
11004
11005 @<Form the appropriate prefix@>=
11006 begin
11007 sp_ptr := str_start[pop_lit2];
11008 sp_end := str_start[pop_lit2+1]; {this may change}
11009 @<Scan the appropriate number of characters@>;
11010 if (pop_lit2 >= cmd_str_ptr) then {no shifting---merely change pointers}
11011 pool_ptr := sp_end
11012 else
11013 while (sp_ptr < sp_end) do {shift the substring}
11014 begin
11015 append_char (str_pool[sp_ptr]);
11016 incr(sp_ptr);
11017 end;
11018 while (sp_brace_level > 0) do {add matching |right_brace|s}
11019 begin
11020 append_char (right_brace);
11021 decr(sp_brace_level);
11022 end;
11023 push_lit_stk (make_string, stk_str); {and push it onto the stack}
11024 end
11025
11026
11027 @
11028 @^special character@>
11029 This section scans |pop_lit1| text characters, where an entire special
11030 character counts as a single text character (even if it's missing its
11031 matching |right_brace|), and where braces don't count as text
11032 characters.
11033
11034 @<Scan the appropriate number of characters@>=
11035 begin
11036 num_text_chars := 0;
11037 sp_brace_level := 0;
11038 sp_xptr1 := sp_ptr;
11039 while ((sp_xptr1 < sp_end) and (num_text_chars < pop_lit1)) do
11040 begin
11041 incr(sp_xptr1);
11042 if (str_pool[sp_xptr1-1] = left_brace) then
11043 begin
11044 incr(sp_brace_level);
11045 if ((sp_brace_level = 1) and (sp_xptr1 < sp_end)) then
11046 if (str_pool[sp_xptr1] = backslash) then
11047 begin
11048 incr(sp_xptr1); {skip over the |backslash|}
11049 while ((sp_xptr1 < sp_end) and (sp_brace_level > 0)) do
11050 begin
11051 if (str_pool[sp_xptr1] = right_brace) then
11052 decr(sp_brace_level)
11053 else if (str_pool[sp_xptr1] = left_brace) then
11054 incr(sp_brace_level);
11055 incr(sp_xptr1);
11056 end;
11057 incr(num_text_chars);
11058 end;
11059 end
11060 else if (str_pool[sp_xptr1-1] = right_brace) then
11061 begin
11062 if (sp_brace_level > 0) then
11063 decr(sp_brace_level);
11064 end
11065 else
11066 incr(num_text_chars);
11067 end;
11068 sp_end := sp_xptr1;
11069 end
11070
11071
11072 @
11073 The |built_in| function {\.{top\$}} pops and prints the top of the
11074 stack.
11075
11076 @<|execute_fn|({\.{top\$}})@>=
11077 begin
11078 pop_top_and_print;
11079 end
11080
11081
11082 @
11083 The |built_in| function {\.{type\$}} pushes the appropriate string
11084 from |type_list| onto the stack (unless either it's |undefined| or
11085 |empty|, in which case it pushes the null string).
11086
11087 @<|execute_fn|({\.{type\$}})@>=
11088 procedure x_type;
11089 begin
11090 if (not mess_with_entries) then
11091 bst_cant_mess_with_entries_print
11092 else
11093 if ((type_list[cite_ptr] = undefined) or
11094 (type_list[cite_ptr] = empty)) then
11095 push_lit_stk (s_null, stk_str)
11096 else
11097 push_lit_stk (hash_text[type_list[cite_ptr]], stk_str);
11098 end;
11099
11100
11101 @
11102 The |built_in| function {\.{warning\$}} pops the top (string) literal
11103 and prints it following a warning message. This is implemented as a
11104 special |built_in| function rather than using the {\.{top\$}} function
11105 so that it can |mark_warning|.
11106
11107 @<|execute_fn|({\.{warning\$}})@>=
11108 procedure x_warning;
11109 begin
11110 pop_lit_stk (pop_lit1,pop_typ1);
11111 if (pop_typ1 <> stk_str) then
11112 print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str)
11113 else
11114 begin
11115 print ('Warning--');
11116 print_lit (pop_lit1,pop_typ1);
11117 mark_warning;
11118 end;
11119 end;
11120
11121
11122 @
11123 The |built_in| function {\.{while\$}} pops the top two (function)
11124 literals, and keeps executing the second as long as the (integer)
11125 value left on the stack by executing the first is greater than 0. If
11126 either type is incorrect, it complains but does nothing else.
11127
11128 @<|execute_fn|({\.{while\$}})@>=
11129 begin
11130 pop_lit_stk (r_pop_lt1,r_pop_tp1);
11131 pop_lit_stk (r_pop_lt2,r_pop_tp2);
11132 if (r_pop_tp1 <> stk_fn) then
11133 print_wrong_stk_lit (r_pop_lt1,r_pop_tp1,stk_fn)
11134 else if (r_pop_tp2 <> stk_fn) then
11135 print_wrong_stk_lit (r_pop_lt2,r_pop_tp2,stk_fn)
11136 else
11137 loop
11138 begin
11139 execute_fn (r_pop_lt2); {this is the \.{while\$} test}
11140 pop_lit_stk (pop_lit1,pop_typ1);
11141 if (pop_typ1 <> stk_int) then
11142 begin
11143 print_wrong_stk_lit (pop_lit1,pop_typ1,stk_int);
11144 goto end_while;
11145 end
11146 else
11147 if (pop_lit1 > 0) then
11148 execute_fn (r_pop_lt1) {this is the \.{while\$} body}
11149 else
11150 goto end_while;
11151 end;
11152 end_while: {justifies this |mean_while|}
11153 end
11154
11155
11156 @
11157 @^literal literal@>
11158 @^special character@>
11159 The |built_in| function {\.{width\$}} pops the top (string) literal
11160 and pushes the integer that represents its width in units specified by
11161 the |char_width| array. This function takes the literal literally;
11162 that is, it assumes each character in the string is to be printed as
11163 is, regardless of whether the character has a special meaning to \TeX,
11164 except that special characters (even without their |right_brace|s) are
11165 handled specially. If the literal isn't a string, it complains and
11166 pushes~0.
11167
11168 @<|execute_fn|({\.{width\$}})@>=
11169 procedure x_width;
11170 begin
11171 pop_lit_stk (pop_lit1,pop_typ1);
11172 if (pop_typ1 <> stk_str) then
11173 begin
11174 print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str);
11175 push_lit_stk (0, stk_int);
11176 end
11177 else
11178 begin
11179 ex_buf_length := 0;
11180 add_buf_pool (pop_lit1);
11181 string_width := 0;
11182 @<Add up the |char_width|s in this string@>;
11183 push_lit_stk (string_width, stk_int);
11184 end
11185 end;
11186
11187
11188 @
11189 We use the natural width for all but special characters, and we
11190 complain if the string isn't brace-balanced.
11191
11192 @<Add up the |char_width|s in this string@>=
11193 begin
11194 brace_level := 0; {we're at the top level}
11195 ex_buf_ptr := 0; {and the beginning of string}
11196 while (ex_buf_ptr < ex_buf_length) do
11197 begin
11198 if (ex_buf[ex_buf_ptr] = left_brace) then
11199 begin
11200 incr(brace_level);
11201 if ((brace_level = 1) and (ex_buf_ptr + 1 < ex_buf_length)) then
11202 if (ex_buf[ex_buf_ptr+1] = backslash) then
11203 @<Determine the width of this special character@>
11204 else
11205 string_width := string_width + char_width[left_brace]
11206 else
11207 string_width := string_width + char_width[left_brace];
11208 end
11209 else if (ex_buf[ex_buf_ptr] = right_brace) then
11210 begin
11211 decr_brace_level (pop_lit1);
11212 string_width := string_width + char_width[right_brace];
11213 end
11214 else
11215 string_width := string_width + char_width[ex_buf[ex_buf_ptr]];
11216 incr(ex_buf_ptr);
11217 end;
11218 check_brace_level (pop_lit1);
11219 end
11220
11221
11222 @
11223 @^special character@>
11224 We use the natural widths of all characters except that some
11225 characters have no width: braces, control sequences (except for the
11226 usual 13 accented and foreign characters, whose widths are given in
11227 the next module), and |white_space| following control sequences (even
11228 a null control sequence).
11229
11230 @<Determine the width of this special character@>=
11231 begin
11232 incr(ex_buf_ptr); {skip over the |left_brace|}
11233 while ((ex_buf_ptr < ex_buf_length) and (brace_level > 0)) do
11234 begin
11235 incr(ex_buf_ptr); {skip over the |backslash|}
11236 ex_buf_xptr := ex_buf_ptr;
11237 while ((ex_buf_ptr < ex_buf_length) and
11238 (lex_class[ex_buf[ex_buf_ptr]] = alpha)) do@/
11239 incr(ex_buf_ptr); {this scans the control sequence}
11240 if ((ex_buf_ptr < ex_buf_length) and (ex_buf_ptr = ex_buf_xptr)) then
11241 incr(ex_buf_ptr) {this skips a nonalpha control seq}
11242 else
11243 begin
11244 control_seq_loc := str_lookup(ex_buf,ex_buf_xptr,
11245 ex_buf_ptr-ex_buf_xptr,control_seq_ilk,dont_insert);
11246 if (hash_found) then
11247 @<Determine the width of this accented or foreign character@>;
11248 end;
11249 while ((ex_buf_ptr < ex_buf_length) and
11250 (lex_class[ex_buf[ex_buf_ptr]] = white_space)) do
11251 incr(ex_buf_ptr); {this skips following |white_space|}
11252 while ((ex_buf_ptr < ex_buf_length) and (brace_level > 0) and
11253 (ex_buf[ex_buf_ptr] <> backslash)) do
11254 begin {this scans to the next control sequence}
11255 if (ex_buf[ex_buf_ptr] = right_brace) then
11256 decr(brace_level)
11257 else if (ex_buf[ex_buf_ptr] = left_brace) then
11258 incr(brace_level)
11259 else
11260 string_width := string_width + char_width[ex_buf[ex_buf_ptr]];
11261 incr(ex_buf_ptr);
11262 end;
11263 end;
11264 decr(ex_buf_ptr); {unskip the |right_brace|}
11265 end
11266
11267
11268 @
11269 Five of the 13 possibilities resort to special information not present
11270 in the |char_width| array; the other eight simply use |char_width|'s
11271 information for the first letter of the control sequence.
11272
11273 @<Determine the width of this accented or foreign character@>=
11274 begin
11275 case (ilk_info[control_seq_loc]) of
11276 n_ss : string_width := string_width + ss_width;
11277 n_ae : string_width := string_width + ae_width;
11278 n_oe : string_width := string_width + oe_width;
11279 n_ae_upper : string_width := string_width + upper_ae_width;
11280 n_oe_upper : string_width := string_width + upper_oe_width;
11281 othercases
11282 string_width := string_width + char_width[ex_buf[ex_buf_xptr]]
11283 endcases;
11284 end
11285
11286
11287 @
11288 The |built_in| function {\.{write\$}} pops the top (string) literal
11289 and writes it onto the output buffer |out_buf| (which will result in
11290 stuff being written onto the \.{.bbl} file if the buffer fills up). If
11291 the literal isn't a string, it complains but does nothing else.
11292
11293 @<|execute_fn|({\.{write\$}})@>=
11294 procedure x_write;
11295 begin
11296 pop_lit_stk (pop_lit1,pop_typ1);
11297 if (pop_typ1 <> stk_str) then
11298 print_wrong_stk_lit (pop_lit1,pop_typ1,stk_str)
11299 else
11300 add_out_pool (pop_lit1);
11301 end;
11302
11303
11304
11305 @* Cleaning up.
11306 @^clich\'e-\`a-trois@>
11307 @^fat lady@>
11308 @^turn out lights@>
11309 @^Yogi@>
11310 This section does any last-minute printing and ends the program.
11311
11312 @<Clean up and leave@>=
11313 begin
11314 if ((read_performed) and (not reading_completed)) then
11315 begin
11316 print ('Aborted at line ',bib_line_num:0,' of file ');
11317 print_bib_name;
11318 end;
11319 trace_and_stat_printing;
11320 @<Print the job |history|@>;
11321 a_close (log_file);
11322 {turn out the lights, the fat lady has sung; it's over, Yogi}
11323 end
11324
11325
11326 @
11327 Here we print |trace| and/or |stat| information, if desired.
11328
11329 @<Procedures and functions for all file I/O, error messages, and such@>=
11330 procedure trace_and_stat_printing;
11331 begin
11332 @#
11333 trace
11334 @<Print all \.{.bib}- and \.{.bst}-file information@>;
11335 @<Print all |cite_list| and entry information@>;
11336 @<Print the |wiz_defined| functions@>;
11337 @<Print the string pool@>;
11338 ecart@/
11339 @#
11340 stat
11341 @<Print usage statistics@>;
11342 tats@/
11343 @#
11344 end;
11345
11346
11347 @
11348 This prints information obtained from the \.{.aux} file about the
11349 other files.
11350
11351 @<Print all \.{.bib}- and \.{.bst}-file information@>=
11352 begin
11353 if (num_bib_files = 1) then
11354 trace_pr_ln ('The 1 database file is')
11355 else
11356 trace_pr_ln ('The ',num_bib_files:0,' database files are');
11357 if (num_bib_files = 0) then
11358 trace_pr_ln (' undefined')
11359 else
11360 begin
11361 bib_ptr := 0;
11362 while (bib_ptr < num_bib_files) do
11363 begin
11364 trace_pr (' ');
11365 trace_pr_pool_str (cur_bib_str);
11366 trace_pr_pool_str (s_bib_extension);
11367 trace_pr_newline;
11368 incr(bib_ptr);
11369 end;
11370 end;
11371 trace_pr ('The style file is ');
11372 if (bst_str = 0) then
11373 trace_pr_ln ('undefined')
11374 else
11375 begin
11376 trace_pr_pool_str (bst_str);
11377 trace_pr_pool_str (s_bst_extension);
11378 trace_pr_newline;
11379 end;
11380 end
11381
11382
11383 @
11384 In entry-sorted order, this prints an entry's |cite_list| string and,
11385 indirectly, its entry type and entry variables.
11386
11387 @<Print all |cite_list| and entry information@>=
11388 begin
11389 if (all_entries) then
11390 trace_pr ('all_marker=',all_marker:0,', ');
11391 if (read_performed) then
11392 trace_pr_ln ('old_num_cites=',old_num_cites:0)
11393 else
11394 trace_pr_newline;
11395 trace_pr ('The ',num_cites:0);
11396 if (num_cites = 1) then
11397 trace_pr_ln (' entry:')
11398 else
11399 trace_pr_ln (' entries:');
11400 if (num_cites = 0) then
11401 trace_pr_ln (' undefined')
11402 else
11403 begin
11404 sort_cite_ptr := 0;
11405 while (sort_cite_ptr < num_cites) do
11406 begin
11407 if (not read_completed) then {we didn't finish the \.{read} command}
11408 cite_ptr := sort_cite_ptr
11409 else
11410 cite_ptr := sorted_cites[sort_cite_ptr];
11411 trace_pr_pool_str (cur_cite_str);
11412 if (read_performed) then
11413 @<Print entry information@>
11414 else
11415 trace_pr_newline;
11416 incr(sort_cite_ptr);
11417 end;
11418 end;
11419 end
11420
11421
11422 @
11423 This prints information gathered while reading the \.{.bst} and
11424 \.{.bib} files.
11425
11426 @<Print entry information@>=
11427 begin
11428 trace_pr (', entry-type ');
11429 if (type_list[cite_ptr] = undefined) then
11430 undefined : trace_pr ('unknown')
11431 else if (type_list[cite_ptr] = empty) then
11432 trace_pr ('--- no type found')
11433 else
11434 trace_pr_pool_str (hash_text[type_list[cite_ptr]]);
11435 trace_pr_ln (', has entry strings');
11436 @<Print entry strings@>;
11437 trace_pr (' has entry integers');
11438 @<Print entry integers@>;
11439 trace_pr_ln (' and has fields');
11440 @<Print fields@>;
11441 end
11442
11443
11444 @
11445 This prints, for the current entry, the strings declared by the
11446 \.{entry} command.
11447
11448 @<Print entry strings@>=
11449 begin
11450 if (num_ent_strs = 0) then
11451 trace_pr_ln (' undefined')
11452 else if (not read_completed) then
11453 trace_pr_ln (' uninitialized')
11454 else
11455 begin
11456 str_ent_ptr := cite_ptr * num_ent_strs;
11457 while (str_ent_ptr < (cite_ptr+1)*num_ent_strs) do
11458 begin
11459 ent_chr_ptr := 0;
11460 trace_pr (' "');
11461 while (entry_strs[str_ent_ptr][ent_chr_ptr] <> end_of_string) do
11462 begin
11463 trace_pr (xchr[entry_strs[str_ent_ptr][ent_chr_ptr]]);
11464 incr(ent_chr_ptr);
11465 end;
11466 trace_pr_ln ('"');
11467 incr(str_ent_ptr);
11468 end;
11469 end;
11470 end
11471
11472
11473 @
11474 This prints, for the current entry, the integers declared by the
11475 \.{entry} command.
11476
11477 @<Print entry integers@>=
11478 begin
11479 if (num_ent_ints = 0) then
11480 trace_pr (' undefined')
11481 else if (not read_completed) then
11482 trace_pr (' uninitialized')
11483 else
11484 begin
11485 int_ent_ptr := cite_ptr*num_ent_ints;
11486 while (int_ent_ptr < (cite_ptr+1)*num_ent_ints) do
11487 begin
11488 trace_pr (' ',entry_ints[int_ent_ptr]:0);
11489 incr(int_ent_ptr);
11490 end;
11491 end;
11492 trace_pr_newline;
11493 end
11494
11495
11496 @
11497 This prints the fields stored for the current entry.
11498
11499 @<Print fields@>=
11500 begin
11501 if (not read_performed) then
11502 trace_pr_ln (' uninitialized')
11503 else
11504 begin
11505 field_ptr := cite_ptr * num_fields;
11506 field_end_ptr := field_ptr + num_fields;
11507 no_fields := true;
11508 while (field_ptr < field_end_ptr) do
11509 begin
11510 if (field_info[field_ptr] <> missing) then
11511 begin
11512 trace_pr (' "');
11513 trace_pr_pool_str (field_info[field_ptr]);
11514 trace_pr_ln ('"');
11515 no_fields := false;
11516 end;
11517 incr(field_ptr);
11518 end;
11519 if (no_fields) then
11520 trace_pr_ln (' missing');
11521 end;
11522 end
11523
11524
11525 @
11526 This gives all the |wiz_defined| functions that appeared in the
11527 \.{.bst} file.
11528
11529 @<Print the |wiz_defined| functions@>=
11530 begin
11531 trace_pr_ln ('The wiz-defined functions are');
11532 if (wiz_def_ptr = 0) then
11533 trace_pr_ln (' nonexistent')
11534 else
11535 begin
11536 wiz_fn_ptr := 0;
11537 while (wiz_fn_ptr < wiz_def_ptr) do
11538 begin
11539 if (wiz_functions[wiz_fn_ptr] = end_of_def) then
11540 trace_pr_ln (wiz_fn_ptr:0,'--end-of-def--')
11541 else if (wiz_functions[wiz_fn_ptr] = quote_next_fn) then
11542 trace_pr (wiz_fn_ptr:0,' quote_next_function ')
11543 else
11544 begin
11545 trace_pr (wiz_fn_ptr:0,' `');
11546 trace_pr_pool_str (hash_text[wiz_functions[wiz_fn_ptr]]);
11547 trace_pr_ln ('''');
11548 end;
11549 incr(wiz_fn_ptr);
11550 end;
11551 end;
11552 end
11553
11554
11555 @
11556 This includes all the `static' strings (that is, those that are also
11557 in the hash table), but none of the dynamic strings (that is, those
11558 put on the stack while executing \.{.bst} commands).
11559
11560 @<Print the string pool@>=
11561 begin
11562 trace_pr_ln ('The string pool is');
11563 str_num := 1;
11564 while (str_num < str_ptr) do
11565 begin
11566 trace_pr (str_num:4, str_start[str_num]:6,' "');
11567 trace_pr_pool_str (str_num);
11568 trace_pr_ln ('"');
11569 incr(str_num);
11570 end;
11571 end
11572
11573
11574 @
11575 @^statistics@>
11576 These statistics can help determine how large some of the constants
11577 should be and can tell how useful certain |built_in| functions are.
11578 They are written to the same files as tracing information.
11579
11580 @d stat_pr == trace_pr
11581 @d stat_pr_ln == trace_pr_ln
11582 @d stat_pr_pool_str == trace_pr_pool_str
11583
11584 @<Print usage statistics@>=
11585 begin
11586 stat_pr ('You''ve used ',num_cites:0);
11587 if (num_cites = 1) then
11588 stat_pr_ln (' entry,')
11589 else
11590 stat_pr_ln (' entries,');
11591 stat_pr_ln (' ',wiz_def_ptr:0,' wiz_defined-function locations,');
11592 stat_pr_ln (' ',str_ptr:0,' strings with ',str_start[str_ptr]:0,
11593 ' characters,');
11594 blt_in_ptr := 0;
11595 total_ex_count := 0;
11596 while (blt_in_ptr < num_blt_in_fns) do
11597 begin
11598 total_ex_count := total_ex_count + execution_count[blt_in_ptr];
11599 incr(blt_in_ptr);
11600 end;
11601 stat_pr_ln ('and the built_in function-call counts, ', total_ex_count:0,
11602 ' in all, are:');
11603 blt_in_ptr := 0;
11604 while (blt_in_ptr < num_blt_in_fns) do
11605 begin
11606 stat_pr_pool_str (hash_text[blt_in_loc[blt_in_ptr]]);
11607 stat_pr_ln (' -- ',execution_count[blt_in_ptr]:0);
11608 incr(blt_in_ptr);
11609 end;
11610 end
11611
11612
11613 @
11614 @^bunk, history@>
11615 @^system dependencies@>
11616 @:this can't happen}{\quad History is bunk@>
11617 Some implementations may wish to pass the |history| value to the
11618 operating system so that it can be used to govern whether or not other
11619 programs are started. Here we simply report the history to the user.
11620
11621 @<Print the job |history|@>=
11622 case (history) of
11623 spotless : do_nothing;
11624 warning_message : begin
11625 if (err_count = 1) then
11626 print_ln ('(There was 1 warning)')
11627 else
11628 print_ln ('(There were ',err_count:0,' warnings)');
11629 end;
11630 error_message : begin
11631 if (err_count = 1) then
11632 print_ln ('(There was 1 error message)')
11633 else
11634 print_ln ('(There were ',err_count:0,
11635 ' error messages)');
11636 end;
11637 fatal_message : print_ln ('(That was a fatal error)');
11638 othercases begin
11639 print ('History is bunk');
11640 print_confusion;
11641 end
11642 endcases
11643
11644
11645
11646 @* System-dependent changes.
11647 @^system dependencies@>
11648 This section should be replaced, if necessary, by changes to the program
11649 that are necessary to make \BibTeX\ work at a particular installation.
11650 It is usually best to design your change file so that all changes to
11651 previous sections preserve the section numbering; then everybody's version
11652 will be consistent with the printed program. More extensive changes,
11653 which introduce new sections, can be inserted here; then only the index
11654 itself will get a new section number.
11655
11656
11657
11658 @* Index.
11659 @.this can't happen@>
11660 Here is where you can find all uses of each identifier in the program,
11661 with underlined entries pointing to where the identifier was defined.
11662 If the identifier is only one letter long, however, you get to see only
11663 the underlined entries. All references are to section numbers instead of
11664 page numbers.
11665
11666 This index also lists a few error messages and other aspects of the
11667 program that you might want to look up some day. For example, the
11668 entry for ``system dependencies'' lists all sections that should
11669 receive special attention from people who are installing \TeX\ in a
11670 new operating environment. A list of various things that can't happen
11671 appears under ``this can't happen''$\!$.