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