1% Change file for BibTeX in C, originally by Howard Trickey.
2%
3% 05/28/84      Initial implementation, version 0.41 of BibTeX
4% 07/01/84      Version 0.41a of BibTeX.
5% 12/17/84      Version 0.97c of BibTeX.
6% 02/12/85      Version 0.98c of BibTeX.
7% 02/25/85      Newer version 0.98c of BibTeX.
8% 03/25/85      Version 0.98f of BibTeX
9% 05/23/85      Version 0.98i of BibTeX
10% 02/11/88      Version 0.99b of BibTeX
11% 04/04/88      Version 0.99c; converted for use with web2c (ETM).
12% 11/30/89      Use FILENAMESIZE instead of 1024 (KB).
13% 03/09/90	`int' is a bad variable name for C.
14% (more recent changes in the ChangeLog)
15%
16%  5/ 2/89      Version 0.20 of JBiBTeX by Shouichi Matsui
17% 12/25/90      Version 0.30 of JBibTeX by Shouichi Matsui
18%  1/ 1/91      last update for JBibTeX 0.30 by Shouichi Matsui
19% 10/30/92      last update for JBibTeX 0.31 for bug fix by Shouichi Matsui
20% 11/02/94      Version 0.32 for use with web2c 6.1, by Takafumi Sakurai
21%
22% 2009          pTeXenc, pbibtex N. Tsuchimura
23
24@x [0] only print chnages
25\def\title{\BibTeX\ }
26@y
27\let\maybe=\iffalse
28\def\title{J\BibTeX\ 0.33 Changes for C Version \BibTeX\ }
29@z
30
31@x
32 \def\titlepage{F}
33 \centerline{\:\titlefont The {\:\ttitlefont \BibTeX} preprocessor}
34 \vskip 15pt \centerline{(Version 0.99d---\today)} \vfill}
35@y
36 \def\titlepage{F}
37 \centerline{\:\titlefont The {\:\ttitlefont J\BibTeX} preprocessor}
38 \vskip 15pt \centerline{(Version 0.33 based on C Version \BibTeX 0.99d---\today)} \vfill}
39@z
40
41%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42% [1] banner
43%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
44@x
45@d my_name=='bibtex'
46@d banner=='This is BibTeX, Version 0.99d' {printed when the program starts}
47@y
48@d my_name=='pbibtex'
49@d banner=='This is pBibTeX, Version 0.99d-j0.33'
50  {printed when the program starts}
51@z
52
53@x
54  print (banner);
55@y
56  print (banner);
57  print (' (', conststringcast(get_enc_string), ')');
58@z
59
60@x Changes for JBibTeX by Shouichi Matsui [27]
61for i:=0 to @'37 do xchr[i]:=chr(i);
62for i:=@'177 to @'377 do xchr[i]:=chr(i);
63@y
64for i:=1 to @'37 do xchr[i]:=' ';
65xchr[tab]:=chr(tab);
66for i:=@'200 to @'237 do xchr[i]:=chr(i-@'200);
67for i:=@'240 to 255 do xchr[i]:=chr(i);
68xchr[@'33]:=chr(@'33); {escape char}
69@z
70
71@x Changes for JBibTeX by Shouichi Matsui [28]
72for i:=first_text_char to last_text_char do xord[xchr[i]]:=i;
73@y
74for i:=first_text_char to last_text_char do xord[i]:=invalid_code;
75for i:=1 to @'176 do xord[i]:= i;
76for i:=@'200 to @'237 do xord[i]:= i-@'200;
77for i:=@'240 to 255 do xord[i]:=i;
78@z
79
80
81@x Changes for JBibTeX by Shouichi Matsui  for ver. 0.30
82@d period = "."                 {these are three}
83@d question_mark = "?"          {string-ending characters}
84@d exclamation_mark = "!"       {of interest in \.{add.period\$}}
85@y
86@d period = "."                 {these are three}
87@d question_mark = "?"          {string-ending characters}
88@d exclamation_mark = "!"       {of interest in \.{add.period\$}}
89@d zen_pun_first = 161          {Zenkaku punctuation first byte; in EUC}
90@d zen_space = 161              {Zenkaku space first, second byte; in EUC}
91@d zen_kuten = 162              {Zenkaku kuten second byte; in EUC}
92@d zen_ten = 163                {Zenkaku ten second byte; in EUC}
93@d zen_comma = 164              {Zenkaku comman second byte; in EUC}
94@d zen_period = 165             {Zenkaku period second byte; in EUC}
95@d zen_question = 169           {Zenkaku question mark second byte; in EUC}
96@d zen_exclamation = 170        {Zenkaku exclamation mark second byte; in EUC}
97@z
98
99% [32] Make RET a `white_space' character, so we won't choke on DOS
100% files, which use CR/LF for line endings.
101@x Changes for JBibTeX by Shouichi Matsui [32]
102@<Set initial values of key variables@>=
103for i:=0 to @'177 do lex_class[i] := other_lex;
104for i:=@'200 to @'377 do lex_class[i] := alpha;
105for i:=0 to @'37 do lex_class[i] := illegal;
106lex_class[invalid_code] := illegal;
107lex_class[tab] := white_space;
108lex_class[13] := white_space;
109lex_class[space] := white_space;
110lex_class[tie] := sep_char;
111lex_class[hyphen] := sep_char;
112for i:=@'60 to @'71 do lex_class[i] := numeric;
113for i:=@'101 to @'132 do lex_class[i] := alpha;
114for i:=@'141 to @'172 do lex_class[i] := alpha;
115@y
116@<Set initial values of key variables@>=
117for i:=0 to @'377 do lex_class[i] := other_lex;
118for i:=0 to @'37 do lex_class[i] := illegal;
119lex_class[invalid_code] := illegal;
120lex_class[tab] := white_space;
121lex_class[13] := white_space;
122lex_class[space] := white_space;
123lex_class[tie] := sep_char;
124lex_class[hyphen] := sep_char;
125for i:=@'60 to @'71 do lex_class[i] := numeric;
126for i:=@'101 to @'132 do lex_class[i] := alpha;
127for i:=@'141 to @'172 do lex_class[i] := alpha;
128for i:=@'200 to @'237 do lex_class[i] := illegal;
129for i:=@'240 to 255 do lex_class[i] := alpha;
130lex_class[@'33]:=alpha;
131@z
132
133@x Changes for JBibTeX by Shouichi Matsui [33]
134@<Set initial values of key variables@>=
135for i:=0 to @'377 do id_class[i] := legal_id_char;
136for i:=0 to @'37 do id_class[i] := illegal_id_char;
137id_class[space] := illegal_id_char;
138id_class[tab] := illegal_id_char;
139id_class[double_quote] := illegal_id_char;
140id_class[number_sign] := illegal_id_char;
141id_class[comment] := illegal_id_char;
142id_class[single_quote] := illegal_id_char;
143id_class[left_paren] := illegal_id_char;
144id_class[right_paren] := illegal_id_char;
145id_class[comma] := illegal_id_char;
146id_class[equals_sign] := illegal_id_char;
147id_class[left_brace] := illegal_id_char;
148id_class[right_brace] := illegal_id_char;
149@y
150@<Set initial values of key variables@>=
151for i:=0 to @'377 do id_class[i] := legal_id_char;
152for i:=0 to @'37 do id_class[i] := illegal_id_char;
153for i:=@'200 to @'237 do id_class[i] := illegal_id_char;
154id_class[@'33] := legal_id_char;
155id_class[space] := illegal_id_char;
156id_class[tab] := illegal_id_char;
157id_class[double_quote] := illegal_id_char;
158id_class[number_sign] := illegal_id_char;
159id_class[comment] := illegal_id_char;
160id_class[single_quote] := illegal_id_char;
161id_class[left_paren] := illegal_id_char;
162id_class[right_paren] := illegal_id_char;
163id_class[comma] := illegal_id_char;
164id_class[equals_sign] := illegal_id_char;
165id_class[left_brace] := illegal_id_char;
166id_class[right_brace] := illegal_id_char;
167@z
168
169@x Changes for JBibTeX by Shouichi Matsui [35]
170char_width[@'167] := 722;
171char_width[@'170] := 528;
172char_width[@'171] := 528;
173char_width[@'172] := 444;
174char_width[@'173] := 500;
175char_width[@'174] :=1000;
176char_width[@'175] := 500;
177char_width[@'176] := 500;
178@y
179char_width[@'167] := 722;
180char_width[@'170] := 528;
181char_width[@'171] := 528;
182char_width[@'172] := 444;
183char_width[@'173] := 500;
184char_width[@'174] :=1000;
185char_width[@'175] := 500;
186char_width[@'176] := 500;
187for i:=@'240 to 254 do char_width[i]:=514;
188@z
189
190@x [48] JBibTeX and dynamic buf_size.
191label loop_exit;
192@y
193label loop_exit;
194var i:c_int_type;
195@z
196@x [still 48] JBibTeX and dynamic buf_size.
197  while (not eoln(f)) do
198    begin
199    if (last >= buf_size) then
200        buffer_overflow;
201    buffer[last] := xord[getc (f)];
202    incr (last);
203    end;
204  vgetc (f); {skip the eol}
205@y
206  last := input_line2(f,ustringcast(buffer),last,buf_size,address_of(i));
207  while (not eof(f)) and (last > 0) and (i <> 10) and (i <> 13) do
208  begin
209    buffer_overflow;
210    last := input_line2(f,ustringcast(buffer),last,buf_size,address_of(i));
211  end;
212@z
213
214@x
215@<Procedures and functions for the reading and processing of input files@>=
216procedure get_the_top_level_aux_file_name;
217label aux_found,@!aux_not_found;
218begin
219  @<Process a possible command line@>
220@y
221@<Procedures and functions for the reading and processing of input files@>=
222procedure get_the_top_level_aux_file_name;
223label aux_found,@!aux_not_found;
224begin
225  if (not set_enc_string (nil,'EUC')) then uexit(1);
226  @<Process a possible command line@>
227@z
228
229@x
230parse_arguments;
231@y
232init_kanji;
233parse_arguments;
234@z
235
236@x Changes for JBibTeX by Shouichi Matsui [332]
237@!b_write : hash_loc;           {\.{write\$}}
238@!b_default : hash_loc;         {either \.{skip\$} or \.{default.type}}
239@y
240@!b_write : hash_loc;           {\.{write\$}}
241@!b_is_kanji_str : hash_loc;    {\.{is.kanji.str\$}}
242@!b_default : hash_loc;         {either \.{skip\$} or \.{default.type}}
243@z
244
245@x Changes for JBibTeX by Shouichi Matsui [334]
246@d n_write = 36         {\.{write\$}}
247
248@<Constants in the outer block@>=
249@!num_blt_in_fns = 37;  {one more than the previous number}
250@y
251@d n_write = 36         {\.{write\$}}
252@d n_is_kanji_str = 37  {\.{is.kanji.str\$}}
253
254@<Constants in the outer block@>=
255@!num_blt_in_fns = 38;  {one more than the previous number}
256@z
257
258@x Changes for JBibTeX by Shouichi Matsui [335]
259build_in('write$      ',6,b_write,n_write);
260@y
261build_in('write$      ',6,b_write,n_write);
262build_in('is.kanji.str$',13,b_is_kanji_str,n_is_kanji_str);
263@z
264
265@x Changes for JBibTeX by Shouichi Matsui [342]
266    n_write :           x_write;
267    othercases confusion ('Unknown built-in function')
268endcases;
269end
270@y
271    n_write :           x_write;
272    n_is_kanji_str:     x_is_kanji_str;
273    othercases confusion ('Unknown built-in function')
274endcases;
275end
276@z
277
278@x Changes for JBibTeX by Shouichi Matsui [343]
279@<|execute_fn|({\.{write\$}})@>@;
280@<|execute_fn| itself@>
281@y
282@<|execute_fn|({\.{write\$}})@>@;
283@<|execute_fn|({\.{is.kanji.str\$}})@>@;
284@<|execute_fn| itself@>
285@z
286
287@x Changes for JBibTeX by Shouichi Matsui   --- add.period$ for ver. 0.30
288case (str_pool[sp_ptr]) of
289    period,
290    question_mark,
291    exclamation_mark :
292        repush_string;
293    othercases
294        @<Add the |period| (it's necessary) and push@>
295@y
296case (str_pool[sp_ptr]) of
297    period,
298    question_mark,
299    exclamation_mark :
300        repush_string;
301    zen_ten,
302    zen_period,
303    zen_question,
304    zen_exclamation:
305        if( str_pool[sp_ptr-1] = zen_pun_first ) then
306            repush_string
307        else
308            @<Add the |period| (it's necessary) and push@>;
309    othercases
310        @<Add the |period| (it's necessary) and push@>
311@z
312
313@x Changes for JBibTeX by Shouichi Matsui [377]
314else if (length(pop_lit1) <> 1) then
315    begin
316    print ('"');
317    print_pool_str (pop_lit1);
318    bst_ex_warn ('" isn''t a single character');
319    push_lit_stk (0, stk_int);
320    end
321@y
322else if (length(pop_lit1) <> 1) then
323    if(str_pool[str_start[pop_lit1]]>127) then { a KANJI char is 2byte long }
324        push_lit_stk(str_pool[str_start[pop_lit1]],stk_int)
325    else begin
326        print ('"');
327        print_pool_str (pop_lit1);
328        bst_ex_warn ('" isn''t a single character');
329        push_lit_stk (0, stk_int);
330    end
331@z
332
333@x by Shouichi Matsui for Zenkaku comma
334if (ex_buf_ptr < ex_buf_length) then            {remove the ``and''}
335    ex_buf_ptr := ex_buf_ptr - 4;
336@y
337if (ex_buf_ptr < ex_buf_length) then    {remove the ``and'', or Zenkau comma}
338  begin
339    if( (ex_buf[ex_buf_ptr-1]=zen_comma) or (ex_buf[ex_buf_ptr-1]=zen_kuten) )
340    then ex_buf_ptr := ex_buf_ptr - 2
341    else ex_buf_ptr := ex_buf_ptr - 4;
342  end;
343@z
344
345@x Changes for JBibTeX by Shouichi Matsui for Zenkaku comma
346    "a", "A" :
347        begin
348        incr(ex_buf_ptr);
349        if (preceding_white) then
350            @<See if we have an ``and''@>;      {if so, |and_found := true|}
351        preceding_white := false;
352        end;
353@y
354    "a", "A" :
355        begin
356        incr(ex_buf_ptr);
357        if (preceding_white) then
358            @<See if we have an ``and''@>;      {if so, |and_found := true|}
359        preceding_white := false;
360        end;
361     zen_pun_first:
362        begin
363          if((ex_buf[ex_buf_ptr+1]=zen_comma) or
364             (ex_buf[ex_buf_ptr+1]=zen_kuten) ) then
365                begin
366                  preceding_white := false;
367                  and_found  := true
368                end
369          else if(ex_buf[ex_buf_ptr+1]=zen_space) then
370               begin
371                  ex_buf[ex_buf_ptr]   := space;
372                  ex_buf[ex_buf_ptr+1] := space;
373                  preceding_white := true;
374               end;
375          ex_buf_ptr := ex_buf_ptr + 2;
376        end;
377@z
378
379@x Changes for JBibTeX by Shouichi Matsui for Zenkaku comma[385]
380    othercases
381        if (lex_class[ex_buf[ex_buf_ptr]] = white_space) then
382            begin
383            incr(ex_buf_ptr);
384            preceding_white := true;
385            end
386        else
387            begin
388            incr(ex_buf_ptr);
389            preceding_white := false;
390            end
391  endcases;
392check_brace_level (pop_lit_var);
393end;
394@y
395    othercases
396        if (lex_class[ex_buf[ex_buf_ptr]] = white_space) then
397            begin
398            incr(ex_buf_ptr);
399            preceding_white := true;
400            end
401        else
402            begin
403                if( ex_buf[ex_buf_ptr] > 127 ) then
404                        ex_buf_ptr := ex_buf_ptr +2
405                else
406                        incr(ex_buf_ptr);
407            preceding_white := false;
408            end
409  endcases;
410check_brace_level (pop_lit_var);
411end;
412@z
413
414@x Changes for JBibTeX by Shouichi Matsui [415]
415    if (lex_class[name_buf[name_bf_ptr]] = alpha) then
416        begin
417        append_ex_buf_char_and_check (name_buf[name_bf_ptr]);
418        goto loop_exit;
419        end
420    else if ((name_buf[name_bf_ptr] = left_brace) and
421@y
422    if (lex_class[name_buf[name_bf_ptr]] = alpha) then
423        begin
424            if name_buf[name_bf_ptr]>127 then begin
425                append_ex_buf_char_and_check (name_buf[name_bf_ptr]);
426                incr(name_bf_ptr);
427                append_ex_buf_char_and_check (name_buf[name_bf_ptr]);
428            end
429            else
430                append_ex_buf_char_and_check (name_buf[name_bf_ptr]);
431        goto loop_exit;
432        end
433    else if ((name_buf[name_bf_ptr] = left_brace) and
434@z
435
436@x Changes for JBibTeX by Shouichi Matsui [437]
437@<|execute_fn|({\.{substring\$}})@>=
438procedure x_substring;
439label exit;
440begin
441@y
442@<|execute_fn|({\.{substring\$}})@>=
443procedure x_substring;
444label exit;
445var tps,tpe:pool_pointer; {temporary pointer}
446begin
447@z
448
449@x Changes for JBibTeX by Shouichi Matsui [438]
450str_room(sp_end - sp_ptr);
451while (sp_ptr < sp_end) do                      {shift the substring}
452    begin
453    append_char (str_pool[sp_ptr]);
454    incr(sp_ptr);
455    end;
456@y
457{ 2 bytes Kanji code break check }
458tps:=str_start[pop_lit3];
459while (tps < sp_ptr) do begin
460    if str_pool[tps] > 127
461    then tps := tps + 2
462    else incr(tps);
463end;
464tpe:=tps;
465while (tpe < sp_end) do begin
466    if str_pool[tpe] > 127
467    then tpe := tpe+2
468    else incr(tpe);
469end;
470if tps<>sp_ptr then begin
471    if tps>str_start[pop_lit3]
472    then decr(sp_ptr)
473    else incr(sp_ptr);
474end;
475if tpe<>sp_end then begin
476    if tpe<str_start[pop_lit3+1]
477    then incr(sp_end)
478    else decr(sp_end);
479end;
480
481str_room(sp_end - sp_ptr);
482while (sp_ptr < sp_end) do                      {shift the substring}
483    begin
484    if str_pool[sp_ptr] >127 then begin
485         append_char (str_pool[sp_ptr]); incr(sp_ptr);
486         append_char (str_pool[sp_ptr]); incr(sp_ptr);
487         end
488    else begin
489         append_char (str_pool[sp_ptr]); incr(sp_ptr);
490         end;
491    end;
492@z
493
494@x Changes for JBibTeX by Shouichi Matsui [445]
495    else if (str_pool[sp_xptr1-1] = right_brace) then
496        begin
497        if (sp_brace_level > 0) then
498            decr(sp_brace_level);
499        end
500    else
501        incr(num_text_chars);
502    end;
503@y
504    else if (str_pool[sp_xptr1-1] = right_brace) then
505        begin
506        if (sp_brace_level > 0) then
507            decr(sp_brace_level);
508        end
509    else if (str_pool[sp_xptr1-1]>127) then
510        begin {kanji char}
511            incr(sp_xptr1); num_text_chars:=num_text_chars+2;
512        end
513    else
514        incr(num_text_chars);
515    end;
516@z
517
518@x
519const n_options = 4; {Pascal won't count array lengths for us.}
520@y
521const n_options = 6; {Pascal won't count array lengths for us.}
522@z
523
524@x
525      usage_help (BIBTEX_HELP, nil);
526@y
527      usage_help (PBIBTEX_HELP, nil);
528@z
529
530@x
531    end; {Else it was a flag; |getopt| has already done the assignment.}
532@y
533    end else if argument_is ('kanji') then begin
534      if (not set_enc_string(optarg, nil)) then
535        write_ln('Bad kanji encoding "', stringcast(optarg), '".');
536
537    end; {Else it was a flag; |getopt| has already done the assignment.}
538@z
539
540@x
541long_options[current_option].name := 'version';
542long_options[current_option].has_arg := 0;
543long_options[current_option].flag := 0;
544long_options[current_option].val := 0;
545incr (current_option);
546@y
547long_options[current_option].name := 'version';
548long_options[current_option].has_arg := 0;
549long_options[current_option].flag := 0;
550long_options[current_option].val := 0;
551incr (current_option);
552
553@ Shift-JIS terminal (the flag is ignored except for WIN32).
554@.-sjis-terminal@>
555
556@<Define the option...@> =
557long_options[current_option].name := 'sjis-terminal';
558long_options[current_option].has_arg := 0;
559long_options[current_option].flag := address_of (sjis_terminal);
560long_options[current_option].val := 1;
561incr (current_option);
562
563@ Kanji option.
564@.-kanji@>
565
566@<Define the option...@> =
567long_options[current_option].name := 'kanji';
568long_options[current_option].has_arg := 1;
569long_options[current_option].flag := 0;
570long_options[current_option].val := 0;
571incr(current_option);
572@z
573
574@x
575begin kpse_set_program_name (argv[0], 'bibtex');
576@y
577begin kpse_set_program_name (argv[0], 'pbibtex');
578@z
579
580@x
581  until j_prime;
582  incr (k);
583  hash_prime := j;
584  primes[k] := hash_prime;
585  end;
586end;
587
588@y
589  until j_prime;
590  incr (k);
591  hash_prime := j;
592  primes[k] := hash_prime;
593  end;
594end;
595
596@ modules for JBibTeX
597
598@<|execute_fn|({\.{is.kanji.str\$}})@>=
599procedure x_is_kanji_str;
600label exit;
601begin
602    pop_lit_stk(pop_lit1, pop_typ1);
603    if pop_typ1<> stk_str then
604    begin print_wrong_stk_lit(pop_lit1,pop_typ1,stk_str);
605          push_lit_stk(0,stk_int);
606    end else begin
607        sp_ptr := str_start[pop_lit1];
608        sp_end := str_start[pop_lit1+1];
609        while sp_ptr<sp_end do begin
610            if str_pool[sp_ptr]>127 then begin
611                push_lit_stk(1,stk_int);
612                return;
613            end else begin
614                incr(sp_ptr);
615            end;
616        end;
617        push_lit_stk(0,stk_int);
618    end;
619exit:end;
620@z
621