-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.w
1418 lines (1241 loc) · 50.9 KB
/
common.w
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% This file is part of CWEB.
% This program by Silvio Levy and Donald E. Knuth
% is based on a program by Knuth.
% It is distributed WITHOUT ANY WARRANTY, express or implied.
% Version 3.64 --- January 2002
% Copyright (C) 1987,1990,1993,2000 Silvio Levy and Donald E. Knuth
% Permission is granted to make and distribute verbatim copies of this
% document provided that the copyright notice and this permission notice
% are preserved on all copies.
% Permission is granted to copy and distribute modified versions of this
% document under the conditions for verbatim copying, provided that the
% entire resulting derived work is given a different name and distributed
% under the terms of a permission notice identical to this one.
\def\v{\char'174} % vertical (|) in typewriter font
\def\title{Common code for CTANGLE and CWEAVE (Version 3.64)}
\def\topofcontents{\null\vfill
\centerline{\titlefont Common code for {\ttitlefont CTANGLE} and
{\ttitlefont CWEAVE}}
\vskip 15pt
\centerline{(Version 3.64)}
\vfill}
\def\botofcontents{\vfill
\noindent
Copyright \copyright\ 1987, 1990, 1993, 2000 Silvio Levy and Donald E. Knuth
\bigskip\noindent
Permission is granted to make and distribute verbatim copies of this
document provided that the copyright notice and this permission notice
are preserved on all copies.
\smallskip\noindent
Permission is granted to copy and distribute modified versions of this
document under the conditions for verbatim copying, provided that the
entire resulting derived work is given a different name and distributed
under the terms of a permission notice identical to this one.
}
\pageno=\contentspagenumber \advance\pageno by 1
\let\maybe=\iftrue
@s not_eq normal @q unreserve a C++ keyword @>
@** Introduction. This file contains code common
to both \.{CTANGLE} and \.{CWEAVE}, which roughly concerns the following
problems: character uniformity, input routines, error handling and
parsing of command line. We have tried to concentrate in this file
all the system dependencies, so as to maximize portability.
In the texts below we will
sometimes use \.{CWEB} to refer to either of the two component
programs, if no confusion can arise.
The file begins with a few basic definitions.
@c
@<Include files@>@/
@h
@<Definitions that should agree with \.{CTANGLE} and \.{CWEAVE}@>@/
@<Other definitions@>@/
@<Predeclaration of procedures@>@/
@ In certain cases \.{CTANGLE} and \.{CWEAVE} should do almost, but not
quite, the same thing. In these cases we've written common code for
both, differentiating between the two by means of the global variable
|program|.
@d ctangle 0
@d cweave 1
@<Definitions...@>=
typedef short boolean;
boolean program; /* \.{CWEAVE} or \.{CTANGLE}? */
@ \.{CWEAVE} operates in three phases: First it inputs the source
file and stores cross-reference data, then it inputs the source once again and
produces the \TEX/ output file, and finally it sorts and outputs the index.
Similarly, \.{CTANGLE} operates in two phases.
The global variable |phase| tells which phase we are in.
@<Other...@>= int phase; /* which phase are we in? */
@ There's an initialization procedure that gets both \.{CTANGLE} and
\.{CWEAVE} off to a good start. We will fill in the details of this
procedure later.
@c
void
common_init()
{
@<Initialize pointers@>;
@<Set the default options common to \.{CTANGLE} and \.{CWEAVE}@>;
@<Scan arguments and open output files@>;
}
@* The character set.
\.{CWEB} uses the conventions of \CEE/ programs found in the standard
\.{ctype.h} header file.
@<Include files@>=
#include <ctype.h>
@ A few character pairs are encoded internally as single characters,
using the definitions below. These definitions are consistent with
an extension of ASCII code originally developed at MIT and explained in
Appendix~C of {\sl The \TEX/book\/}; thus, users who have such a
character set can type things like \.{\char'32} and \.{\char'4} instead
of \.{!=} and \.{\&\&}. (However, their files will not be too portable
until more people adopt the extended code.)
If the character set is not ASCII, the definitions given here may conflict
with existing characters; in such cases, other arbitrary codes should be
substituted. The indexes to \.{CTANGLE} and \.{CWEAVE} mention every
case where similar codes may have to be changed in order to
avoid character conflicts. Look for the entry ``ASCII code dependencies''
in those indexes.
@^ASCII code dependencies@>
@^system dependencies@>
@d and_and 04 /* `\.{\&\&}'\,; corresponds to MIT's {\tentex\char'4} */
@d lt_lt 020 /* `\.{<<}'\,; corresponds to MIT's {\tentex\char'20} */
@d gt_gt 021 /* `\.{>>}'\,; corresponds to MIT's {\tentex\char'21} */
@d plus_plus 013 /* `\.{++}'\,; corresponds to MIT's {\tentex\char'13} */
@d minus_minus 01 /* `\.{--}'\,; corresponds to MIT's {\tentex\char'1} */
@d minus_gt 031 /* `\.{->}'\,; corresponds to MIT's {\tentex\char'31} */
@d not_eq 032 /* `\.{!=}'\,; corresponds to MIT's {\tentex\char'32} */
@d lt_eq 034 /* `\.{<=}'\,; corresponds to MIT's {\tentex\char'34} */
@d gt_eq 035 /* `\.{>=}'\,; corresponds to MIT's {\tentex\char'35} */
@d eq_eq 036 /* `\.{==}'\,; corresponds to MIT's {\tentex\char'36} */
@d or_or 037 /* `\.{\v\v}'\,; corresponds to MIT's {\tentex\char'37} */
@d dot_dot_dot 016 /* `\.{...}'\,; corresponds to MIT's {\tentex\char'16} */
@d colon_colon 06 /* `\.{::}'\,; corresponds to MIT's {\tentex\char'6} */
@d period_ast 026 /* `\.{.*}'\,; corresponds to MIT's {\tentex\char'26} */
@d minus_gt_ast 027 /* `\.{->*}'\,; corresponds to MIT's {\tentex\char'27} */
@** Input routines. The lowest level of input to the \.{CWEB} programs
is performed by |input_ln|, which must be told which file to read from.
The return value of |input_ln| is 1 if the read is successful and 0 if
not (generally this means the file has ended). The conventions
of \TEX/ are followed; i.e., the characters of the next line of the file
are copied into the |buffer| array,
and the global variable |limit| is set to the first unoccupied position.
Trailing blanks are ignored. The value of |limit| must be strictly less
than |buf_size|, so that |buffer[buf_size-1]| is never filled.
Since |buf_size| is strictly less than |long_buf_size|,
some of \.{CWEB}'s routines use the fact that it is safe to refer to
|*(limit+2)| without overstepping the bounds of the array.
@d buf_size 100 /* for \.{CWEAVE} and \.{CTANGLE} */
@d longest_name 10000
@d long_buf_size (buf_size+longest_name) /* for \.{CWEAVE} */
@d xisspace(c) (isspace(c)&&((unsigned char)c<0200))
@d xisupper(c) (isupper(c)&&((unsigned char)c<0200))
@<Definitions...@>=
char buffer[long_buf_size]; /* where each line of input goes */
char *buffer_end=buffer+buf_size-2; /* end of |buffer| */
char *limit=buffer; /* points to the last character in the buffer */
char *loc=buffer; /* points to the next character to be read from the buffer */
@ @<Include files@>=
#include <stdio.h>
@ In the unlikely event that your standard I/O library does not
support |feof|, |getc|, and |ungetc| you may have to change things here.
@^system dependencies@>
@c
int input_ln(fp) /* copies a line into |buffer| or returns 0 */
FILE *fp; /* what file to read from */
{
register int c=EOF; /* character read; initialized so some compilers won't complain */
register char *k; /* where next character goes */
if (feof(fp)) return(0); /* we have hit end-of-file */
limit = k = buffer; /* beginning of buffer */
while (k<=buffer_end && (c=getc(fp)) != EOF && c!='\n')
if ((*(k++) = c) != ' ') limit = k;
if (k>buffer_end)
if ((c=getc(fp))!=EOF && c!='\n') {
ungetc(c,fp); loc=buffer; err_print("! Input line too long");
@.Input line too long@>
}
if (c==EOF && limit==buffer) return(0); /* there was nothing after
the last newline */
return(1);
}
@ Now comes the problem of deciding which file to read from next.
Recall that the actual text that \.{CWEB} should process comes from two
streams: a |web_file|, which can contain possibly nested include
commands \.{@@i}, and a |change_file|, which might also contain
includes. The |web_file| together with the currently open include
files form a stack |file|, whose names are stored in a parallel stack
|file_name|. The boolean |changing| tells whether or not we're reading
from the |change_file|.
The line number of each open file is also kept for error reporting and
for the benefit of \.{CTANGLE}.
@f line x /* make |line| an unreserved word */
@d max_include_depth 10 /* maximum number of source files open
simultaneously, not counting the change file */
@d max_file_name_length 60
@d cur_file file[include_depth] /* current file */
@d cur_file_name file_name[include_depth] /* current file name */
@d cur_line line[include_depth] /* number of current line in current file */
@d web_file file[0] /* main source file */
@d web_file_name file_name[0] /* main source file name */
@<Definitions...@>=
int include_depth; /* current level of nesting */
FILE *file[max_include_depth]; /* stack of non-change files */
FILE *change_file; /* change file */
char file_name[max_include_depth][max_file_name_length];
/* stack of non-change file names */
char change_file_name[max_file_name_length]; /* name of change file */
char alt_web_file_name[max_file_name_length]; /* alternate name to try */
int line[max_include_depth]; /* number of current line in the stacked files */
int change_line; /* number of current line in change file */
int change_depth; /* where \.{@@y} originated during a change */
boolean input_has_ended; /* if there is no more input */
boolean changing; /* if the current line is from |change_file| */
boolean web_file_open=0; /* if the web file is being read */
@ When |changing==0|, the next line of |change_file| is kept in
|change_buffer|, for purposes of comparison with the next
line of |cur_file|. After the change file has been completely input, we
set |change_limit=change_buffer|,
so that no further matches will be made.
Here's a shorthand expression for inequality between the two lines:
@d lines_dont_match (change_limit-change_buffer != limit-buffer ||
strncmp(buffer, change_buffer, limit-buffer))
@<Other...@>=
char change_buffer[buf_size]; /* next line of |change_file| */
char *change_limit; /* points to the last character in |change_buffer| */
@ Procedure |prime_the_change_buffer|
sets |change_buffer| in preparation for the next matching operation.
Since blank lines in the change file are not used for matching, we have
|(change_limit==change_buffer && !changing)| if and only if
the change file is exhausted. This procedure is called only when
|changing| is 1; hence error messages will be reported correctly.
@c
void
prime_the_change_buffer()
{
change_limit=change_buffer; /* this value is used if the change file ends */
@<Skip over comment lines in the change file; |return| if end of file@>;
@<Skip to the next nonblank line; |return| if end of file@>;
@<Move |buffer| and |limit| to |change_buffer| and |change_limit|@>;
}
@ While looking for a line that begins with \.{@@x} in the change file, we
allow lines that begin with \.{@@}, as long as they don't begin with \.{@@y},
\.{@@z}, or \.{@@i} (which would probably mean that the change file is fouled up).
@<Skip over comment lines in the change file...@>=
while(1) {
change_line++;
if (!input_ln(change_file)) return;
if (limit<buffer+2) continue;
if (buffer[0]!='@@') continue;
if (xisupper(buffer[1])) buffer[1]=tolower(buffer[1]);
if (buffer[1]=='x') break;
if (buffer[1]=='y' || buffer[1]=='z' || buffer[1]=='i') {
loc=buffer+2;
err_print("! Missing @@x in change file");
@.Missing @@x...@>
}
}
@ Here we are looking at lines following the \.{@@x}.
@<Skip to the next nonblank line...@>=
do {
change_line++;
if (!input_ln(change_file)) {
err_print("! Change file ended after @@x");
@.Change file ended...@>
return;
}
} while (limit==buffer);
@ @<Move |buffer| and |limit| to |change_buffer| and |change_limit|@>=
{
change_limit=change_buffer+(limit-buffer);
strncpy(change_buffer,buffer,limit-buffer+1);
}
@ The following procedure is used to see if the next change entry should
go into effect; it is called only when |changing| is 0.
The idea is to test whether or not the current
contents of |buffer| matches the current contents of |change_buffer|.
If not, there's nothing more to do; but if so, a change is called for:
All of the text down to the \.{@@y} is supposed to match. An error
message is issued if any discrepancy is found. Then the procedure
prepares to read the next line from |change_file|.
When a match is found, the current section is marked as changed unless
the first line after the \.{@@x} and after the \.{@@y} both start with
either |'@@*'| or |'@@ '| (possibly preceded by whitespace).
This procedure is called only when |buffer<limit|, i.e., when the
current line is nonempty.
@d if_section_start_make_pending(b) {@+*limit='!';
for (loc=buffer;xisspace(*loc);loc++) ;
*limit=' ';
if (*loc=='@@' && (xisspace(*(loc+1)) || *(loc+1)=='*')) change_pending=b;
}
@c
void
check_change() /* switches to |change_file| if the buffers match */
{
int n=0; /* the number of discrepancies found */
if (lines_dont_match) return;
change_pending=0;
if (!changed_section[section_count]) {
if_section_start_make_pending(1);
if (!change_pending) changed_section[section_count]=1;
}
while (1) {
changing=1; print_where=1; change_line++;
if (!input_ln(change_file)) {
err_print("! Change file ended before @@y");
@.Change file ended...@>
change_limit=change_buffer; changing=0;
return;
}
if (limit>buffer+1 && buffer[0]=='@@') {
char xyz_code=xisupper(buffer[1])? tolower(buffer[1]): buffer[1];
@<If the current line starts with \.{@@y},
report any discrepancies and |return|@>;
}
@<Move |buffer| and |limit|...@>;
changing=0; cur_line++;
while (!input_ln(cur_file)) { /* pop the stack or quit */
if (include_depth==0) {
err_print("! CWEB file ended during a change");
@.CWEB file ended...@>
input_has_ended=1; return;
}
include_depth--; cur_line++;
}
if (lines_dont_match) n++;
}
}
@ @<If the current line starts with \.{@@y}...@>=
if (xyz_code=='x' || xyz_code=='z') {
loc=buffer+2; err_print("! Where is the matching @@y?");
@.Where is the match...@>
}
else if (xyz_code=='y') {
if (n>0) {
loc=buffer+2;
printf("\n! Hmm... %d ",n);
err_print("of the preceding lines failed to match");
@.Hmm... n of the preceding...@>
}
change_depth=include_depth;
return;
}
@ The |reset_input| procedure, which gets \.{CWEB} ready to read the
user's \.{CWEB} input, is used at the beginning of phase one of \.{CTANGLE},
phases one and two of \.{CWEAVE}.
@c
void
reset_input()
{
limit=buffer; loc=buffer+1; buffer[0]=' ';
@<Open input files@>;
include_depth=0; cur_line=0; change_line=0;
change_depth=include_depth;
changing=1; prime_the_change_buffer(); changing=!changing;
limit=buffer; loc=buffer+1; buffer[0]=' '; input_has_ended=0;
}
@ The following code opens the input files.
@^system dependencies@>
@<Open input files@>=
if ((web_file=fopen(web_file_name,"r"))==NULL) {
strcpy(web_file_name,alt_web_file_name);
if ((web_file=fopen(web_file_name,"r"))==NULL)
fatal("! Cannot open input file ", web_file_name);
}
@.Cannot open input file@>
@.Cannot open change file@>
web_file_open=1;
if ((change_file=fopen(change_file_name,"r"))==NULL)
fatal("! Cannot open change file ", change_file_name);
@ The |get_line| procedure is called when |loc>limit|; it puts the next
line of merged input into the buffer and updates the other variables
appropriately. A space is placed at the right end of the line.
This procedure returns |!input_has_ended| because we often want to
check the value of that variable after calling the procedure.
If we've just changed from the |cur_file| to the |change_file|, or if
the |cur_file| has changed, we tell \.{CTANGLE} to print this
information in the \CEE/ file by means of the |print_where| flag.
@d max_sections 2000 /* number of identifiers, strings, section names;
must be less than 10240 */
@<Defin...@>=
typedef unsigned short sixteen_bits;
sixteen_bits section_count; /* the current section number */
boolean changed_section[max_sections]; /* is the section changed? */
boolean change_pending; /* if the current change is not yet recorded in
|changed_section[section_count]| */
boolean print_where=0; /* should \.{CTANGLE} print line and file info? */
@ @c
int get_line() /* inputs the next line */
{
restart:
if (changing && include_depth==change_depth)
@<Read from |change_file| and maybe turn off |changing|@>;
if (! changing || include_depth>change_depth) {
@<Read from |cur_file| and maybe turn on |changing|@>;
if (changing && include_depth==change_depth) goto restart;
}
if (input_has_ended) return 0;
loc=buffer; *limit=' ';
if (buffer[0]=='@@' && (buffer[1]=='i' || buffer[1]=='I')) {
loc=buffer+2; *limit='"';
while (*loc==' '||*loc=='\t') loc++;
if (loc>=limit) {
err_print("! Include file name not given");
@.Include file name ...@>
goto restart;
}
if (include_depth>=max_include_depth-1) {
err_print("! Too many nested includes");
@.Too many nested includes@>
goto restart;
}
include_depth++; /* push input stack */
@<Try to open include file, abort push if unsuccessful, go to |restart|@>;
}
return 1;
}
@ When an \.{@@i} line is found in the |cur_file|, we must temporarily
stop reading it and start reading from the named include file. The
\.{@@i} line should give a complete file name with or without
double quotes.
If the environment variable \.{CWEBINPUTS} is set, or if the compiler flag
of the same name was defined at compile time,
\.{CWEB} will look for include files in the directory thus named, if
it cannot find them in the current directory.
(Colon-separated paths are not supported.)
The remainder of the \.{@@i} line after the file name is ignored.
@d too_long() {include_depth--;
err_print("! Include file name too long"); goto restart;}
@<Include...@>=
#include <stdlib.h> /* declaration of |getenv| and |exit| */
@ @<Try to open...@>= {
char temp_file_name[max_file_name_length];
char *cur_file_name_end=cur_file_name+max_file_name_length-1;
char *k=cur_file_name, *kk;
int l; /* length of file name */
if (*loc=='"') {
loc++;
while (*loc!='"' && k<=cur_file_name_end) *k++=*loc++;
if (loc==limit) k=cur_file_name_end+1; /* unmatched quote is `too long' */
} else
while (*loc!=' '&&*loc!='\t'&&*loc!='"'&&k<=cur_file_name_end) *k++=*loc++;
if (k>cur_file_name_end) too_long();
@.Include file name ...@>
*k='\0';
if ((cur_file=fopen(cur_file_name,"r"))!=NULL) {
cur_line=0; print_where=1;
goto restart; /* success */
}
kk=getenv("CWEBINPUTS");
if (kk!=NULL) {
if ((l=strlen(kk))>max_file_name_length-2) too_long();
strcpy(temp_file_name,kk);
}
else {
#ifdef CWEBINPUTS
if ((l=strlen(CWEBINPUTS))>max_file_name_length-2) too_long();
strcpy(temp_file_name,CWEBINPUTS);
#else
l=0;
#endif /* |CWEBINPUTS| */
}
if (l>0) {
if (k+l+2>=cur_file_name_end) too_long();
@.Include file name ...@>
for (; k>= cur_file_name; k--) *(k+l+1)=*k;
strcpy(cur_file_name,temp_file_name);
cur_file_name[l]='/'; /* \UNIX/ pathname separator */
if ((cur_file=fopen(cur_file_name,"r"))!=NULL) {
cur_line=0; print_where=1;
goto restart; /* success */
}
}
include_depth--; err_print("! Cannot open include file"); goto restart;
}
@ @<Read from |cur_file|...@>= {
cur_line++;
while (!input_ln(cur_file)) { /* pop the stack or quit */
print_where=1;
if (include_depth==0) {input_has_ended=1; break;}
else {
fclose(cur_file); include_depth--;
if (changing && include_depth==change_depth) break;
cur_line++;
}
}
if (!changing && !input_has_ended)
if (limit-buffer==change_limit-change_buffer)
if (buffer[0]==change_buffer[0])
if (change_limit>change_buffer) check_change();
}
@ @<Read from |change_file|...@>= {
change_line++;
if (!input_ln(change_file)) {
err_print("! Change file ended without @@z");
@.Change file ended...@>
buffer[0]='@@'; buffer[1]='z'; limit=buffer+2;
}
if (limit>buffer) { /* check if the change has ended */
if (change_pending) {
if_section_start_make_pending(0);
if (change_pending) {
changed_section[section_count]=1; change_pending=0;
}
}
*limit=' ';
if (buffer[0]=='@@') {
if (xisupper(buffer[1])) buffer[1]=tolower(buffer[1]);
if (buffer[1]=='x' || buffer[1]=='y') {
loc=buffer+2;
err_print("! Where is the matching @@z?");
@.Where is the match...@>
}
else if (buffer[1]=='z') {
prime_the_change_buffer(); changing=!changing; print_where=1;
}
}
}
}
@ At the end of the program, we will tell the user if the change file
had a line that didn't match any relevant line in |web_file|.
@c
void
check_complete(){
if (change_limit!=change_buffer) { /* |changing| is 0 */
strncpy(buffer,change_buffer,change_limit-change_buffer+1);
limit=buffer+(int)(change_limit-change_buffer);
changing=1; change_depth=include_depth; loc=buffer;
err_print("! Change file entry did not match");
@.Change file entry did not match@>
}
}
@** Storage of names and strings.
Both \.{CWEAVE} and \.{CTANGLE} store identifiers, section names and
other strings in a large array of |char|s, called |byte_mem|.
Information about the names is kept in the array |name_dir|, whose
elements are structures of type |name_info|, containing a pointer into
the |byte_mem| array (the address where the name begins) and other data.
A |name_pointer| variable is a pointer into |name_dir|.
@d max_bytes 90000 /* the number of bytes in identifiers,
index entries, and section names; must be less than $2^{24}$ */
@d max_names 4000 /* number of identifiers, strings, section names;
must be less than 10240 */
@<Definitions that...@>=
typedef struct name_info {
char *byte_start; /* beginning of the name in |byte_mem| */
@<More elements of |name_info| structure@>@;
} name_info; /* contains information about an identifier or section name */
typedef name_info *name_pointer; /* pointer into array of |name_info|s */
char byte_mem[max_bytes]; /* characters of names */
char *byte_mem_end = byte_mem+max_bytes-1; /* end of |byte_mem| */
name_info name_dir[max_names]; /* information about names */
name_pointer name_dir_end = name_dir+max_names-1; /* end of |name_dir| */
@ The actual sequence of characters in the name pointed to by a |name_pointer
p| appears in positions |p->byte_start| to |(p+1)->byte_start-1|, inclusive.
The |print_id| macro prints this text on the user's terminal.
@d length(c) (c+1)->byte_start-(c)->byte_start /* the length of a name */
@d print_id(c) term_write((c)->byte_start,length((c))) /* print identifier */
@ The first unused position in |byte_mem| and |name_dir| is
kept in |byte_ptr| and |name_ptr|, respectively. Thus we
usually have |name_ptr->byte_start==byte_ptr|, and certainly
we want to keep |name_ptr<=name_dir_end| and |byte_ptr<=byte_mem_end|.
@<Defini...@>=
name_pointer name_ptr; /* first unused position in |name_dir| */
char *byte_ptr; /* first unused position in |byte_mem| */
@ @<Init...@>=
name_dir->byte_start=byte_ptr=byte_mem; /* position zero in both arrays */
name_ptr=name_dir+1; /* |name_dir[0]| will be used only for error recovery */
name_ptr->byte_start=byte_mem; /* this makes name 0 of length zero */
@ The names of identifiers are found by computing a hash address |h| and
then looking at strings of bytes signified by the |name_pointer|s
|hash[h]|, |hash[h]->link|, |hash[h]->link->link|, \dots,
until either finding the desired name or encountering the null pointer.
@<More elements of |name...@>=
struct name_info *link;
@ The hash table itself
consists of |hash_size| entries of type |name_pointer|, and is
updated by the |id_lookup| procedure, which finds a given identifier
and returns the appropriate |name_pointer|. The matching is done by the
function |names_match|, which is slightly different in
\.{CWEAVE} and \.{CTANGLE}. If there is no match for the identifier,
it is inserted into the table.
@d hash_size 353 /* should be prime */
@<Defini...@>=
typedef name_pointer *hash_pointer;
name_pointer hash[hash_size]; /* heads of hash lists */
hash_pointer hash_end = hash+hash_size-1; /* end of |hash| */
hash_pointer h; /* index into hash-head array */
@ @<Predec...@>=
extern int names_match();
@ Initially all the hash lists are empty.
@<Init...@>=
for (h=hash; h<=hash_end; *h++=NULL) ;
@ Here is the main procedure for finding identifiers:
@c
name_pointer
id_lookup(first,last,t) /* looks up a string in the identifier table */
char *first; /* first character of string */
char *last; /* last character of string plus one */
char t; /* the |ilk|; used by \.{CWEAVE} only */
{
char *i=first; /* position in |buffer| */
int h; /* hash code */
int l; /* length of the given identifier */
name_pointer p; /* where the identifier is being sought */
if (last==NULL) for (last=first; *last!='\0'; last++);
l=last-first; /* compute the length */
@<Compute the hash code |h|@>;
@<Compute the name location |p|@>;
if (p==name_ptr) @<Enter a new name into the table at position |p|@>;
return(p);
}
@ A simple hash code is used: If the sequence of
character codes is $c_1c_2\ldots c_n$, its hash value will be
$$(2^{n-1}c_1+2^{n-2}c_2+\cdots+c_n)\,\bmod\,|hash_size|.$$
@<Compute the hash...@>=
h=(unsigned char)*i;
while (++i<last) h=(h+h+(int)((unsigned char)*i)) % hash_size;
@^high-bit character handling@>
@ If the identifier is new, it will be placed in position |p=name_ptr|,
otherwise |p| will point to its existing location.
@<Compute the name location...@>=
p=hash[h];
while (p && !names_match(p,first,l,t)) p=p->link;
if (p==NULL) {
p=name_ptr; /* the current identifier is new */
p->link=hash[h]; hash[h]=p; /* insert |p| at beginning of hash list */
}
@ The information associated with a new identifier must be initialized
in a slightly different way in \.{CWEAVE} than in \.{CTANGLE}; hence the
|init_p| procedure.
@<Pred...@>=
void init_p();
@ @<Enter a new name...@>= {
if (byte_ptr+l>byte_mem_end) overflow("byte memory");
if (name_ptr>=name_dir_end) overflow("name");
strncpy(byte_ptr,first,l);
(++name_ptr)->byte_start=byte_ptr+=l;
if (program==cweave) init_p(p,t);
}
@ The names of sections are stored in |byte_mem| together
with the identifier names, but a hash table is not used for them because
\.{CTANGLE} needs to be able to recognize a section name when given a prefix of
that name. A conventional binary search tree is used to retrieve section names,
with fields called |llink| and |rlink| (where |llink| takes the place
of |link|). The root of this tree is stored in |name_dir->rlink|;
this will be the only information in |name_dir[0]|.
Since the space used by |rlink| has a different function for
identifiers than for section names, we declare it as a |union|.
@d llink link /* left link in binary search tree for section names */
@d rlink dummy.Rlink /* right link in binary search tree for section names */
@d root name_dir->rlink /* the root of the binary search tree
for section names */
@<More elements of |name...@>=
union {
struct name_info *Rlink; /* right link in binary search tree for section
names */
char Ilk; /* used by identifiers in \.{CWEAVE} only */
} dummy;
@ @<Init...@>=
root=NULL; /* the binary search tree starts out with nothing in it */
@ If |p| is a |name_pointer| variable, as we have seen,
|p->byte_start| is the beginning of the area where the name
corresponding to |p| is stored. However, if |p| refers to a section
name, the name may need to be stored in chunks, because it may
``grow'': a prefix of the section name may be encountered before
the full name. Furthermore we need to know the length of the shortest
prefix of the name that was ever encountered.
We solve this problem by inserting two extra bytes at |p->byte_start|,
representing the length of the shortest prefix, when |p| is a
section name. Furthermore, the last byte of the name will be a blank
space if |p| is a prefix. In the latter case, the name pointer
|p+1| will allow us to access additional chunks of the name:
The second chunk will begin at the name pointer |(p+1)->link|,
and if it too is a prefix (ending with blank) its |link| will point
to additional chunks in the same way. Null links are represented by
|name_dir|.
@d first_chunk(p) ((p)->byte_start+2)
@d prefix_length(p) (int)((unsigned char)*((p)->byte_start)*256 +
(unsigned char)*((p)->byte_start+1))
@d set_prefix_length(p,m) (*((p)->byte_start)=(m)/256,
*((p)->byte_start+1)=(m)%256)
@c
void
print_section_name(p)
name_pointer p;
{
char *ss, *s = first_chunk(p);
name_pointer q = p+1;
while (p!=name_dir) {
ss = (p+1)->byte_start-1;
if (*ss==' ' && ss>=s) {
term_write(s,ss-s); p=q->link; q=p;
} else {
term_write(s,ss+1-s); p=name_dir; q=NULL;
}
s = p->byte_start;
}
if (q) term_write("...",3); /* complete name not yet known */
}
@ @c
void
sprint_section_name(dest,p)
char*dest;
name_pointer p;
{
char *ss, *s = first_chunk(p);
name_pointer q = p+1;
while (p!=name_dir) {
ss = (p+1)->byte_start-1;
if (*ss==' ' && ss>=s) {
p=q->link; q=p;
} else {
ss++; p=name_dir;
}
strncpy(dest,s,ss-s), dest+=ss-s;
s = p->byte_start;
}
*dest='\0';
}
@ @c
void
print_prefix_name(p)
name_pointer p;
{
char *s = first_chunk(p);
int l = prefix_length(p);
term_write(s,l);
if (s+l<(p+1)->byte_start) term_write("...",3);
}
@ When we compare two section names, we'll need a function analogous to
|strcmp|. But we do not assume the strings
are null-terminated, and we keep an eye open for prefixes and extensions.
@d less 0 /* the first name is lexicographically less than the second */
@d equal 1 /* the first name is equal to the second */
@d greater 2 /* the first name is lexicographically greater than the second */
@d prefix 3 /* the first name is a proper prefix of the second */
@d extension 4 /* the first name is a proper extension of the second */
@c
int web_strcmp(j,j_len,k,k_len) /* fuller comparison than |strcmp| */
char *j, *k; /* beginning of first and second strings */
int j_len, k_len; /* length of strings */
{
char *j1=j+j_len, *k1=k+k_len;
while (k<k1 && j<j1 && *j==*k) k++, j++;
if (k==k1) if (j==j1) return equal;
else return extension;
else if (j==j1) return prefix;
else if (*j<*k) return less;
else return greater;
}
@ Adding a section name to the tree is straightforward if we know its
parent and whether it's the |rlink| or |llink| of the parent. As a
special case, when the name is the first section being added, we set the
``parent'' to |NULL|. When a section name is created, it has only one
chunk, which however may be just a prefix; the full name will
hopefully be unveiled later. Obviously, |prefix_length| starts
out as the length of the first chunk, though it may decrease later.
The information associated with a new node must be initialized
differently in \.{CWEAVE} and \.{CTANGLE}; hence the
|init_node| procedure, which is defined differently in \.{cweave.w}
and \.{ctangle.w}.
@<Prede...@>=
extern void init_node();
@ @c
name_pointer
add_section_name(par,c,first,last,ispref) /* install a new node in the tree */
name_pointer par; /* parent of new node */
int c; /* right or left? */
char *first; /* first character of section name */
char *last; /* last character of section name, plus one */
int ispref; /* are we adding a prefix or a full name? */
{
name_pointer p=name_ptr; /* new node */
char *s=first_chunk(p);
int name_len=last-first+ispref; /* length of section name */
if (s+name_len>byte_mem_end) overflow("byte memory");
if (name_ptr+1>=name_dir_end) overflow("name");
(++name_ptr)->byte_start=byte_ptr=s+name_len;
if (ispref) {
*(byte_ptr-1)=' ';
name_len--;
name_ptr->link=name_dir;
(++name_ptr)->byte_start=byte_ptr;
}
set_prefix_length(p,name_len);
strncpy(s,first,name_len);
p->llink=NULL;
p->rlink=NULL;
init_node(p);
return par==NULL ? (root=p) : c==less ? (par->llink=p) : (par->rlink=p);
}
@ @c
void
extend_section_name(p,first,last,ispref)
name_pointer p; /* name to be extended */
char *first; /* beginning of extension text */
char *last; /* one beyond end of extension text */
int ispref; /* are we adding a prefix or a full name? */
{
char *s;
name_pointer q=p+1;
int name_len=last-first+ispref;
if (name_ptr>=name_dir_end) overflow("name");
while (q->link!=name_dir) q=q->link;
q->link=name_ptr;
s=name_ptr->byte_start;
name_ptr->link=name_dir;
if (s+name_len>byte_mem_end) overflow("byte memory");
(++name_ptr)->byte_start=byte_ptr=s+name_len;
strncpy(s,first,name_len);
if (ispref) *(byte_ptr-1)=' ';
}
@ The |section_lookup| procedure is supposed to find a
section name that matches a new name, installing the new name if
it doesn't match an existing one. The new name is the string
between |first| and |last|; a ``match'' means that the new name
exactly equals or is a prefix or extension of a name in the tree.
@c
name_pointer
section_lookup(first,last,ispref) /* find or install section name in tree */
char *first, *last; /* first and last characters of new name */
int ispref; /* is the new name a prefix or a full name? */
{
int c=0; /* comparison between two names; initialized so some compilers won't complain */
name_pointer p=root; /* current node of the search tree */
name_pointer q=NULL; /* another place to look in the tree */
name_pointer r=NULL; /* where a match has been found */
name_pointer par=NULL; /* parent of |p|, if |r| is |NULL|;
otherwise parent of |r| */
int name_len=last-first+1;
@<Look for matches for new name among shortest prefixes, complaining
if more than one is found@>;
@<If no match found, add new name to tree@>;
@<If one match found, check for compatibility and return match@>;
}
@ A legal new name matches an existing section name if and only if it
matches the shortest prefix of that section name. Therefore we can
limit our search for matches to shortest prefixes, which eliminates
the need for chunk-chasing at this stage.
@<Look for matches for new name among...@>=
while (p) { /* compare shortest prefix of |p| with new name */
c=web_strcmp(first,name_len,first_chunk(p),prefix_length(p));
if (c==less || c==greater) { /* new name does not match |p| */
if (r==NULL) /* no previous matches have been found */
par=p;
p=(c==less?p->llink:p->rlink);
} else { /* new name matches |p| */
if (r!=NULL) { /* and also |r|: illegal */
printf("\n! Ambiguous prefix: matches <");
@.Ambiguous prefix ... @>
print_prefix_name(p);
printf(">\n and <");
print_prefix_name(r);
err_print(">");
return name_dir; /* the unsection */
}
r=p; /* remember match */
p=p->llink; /* try another */
q=r->rlink; /* we'll get back here if the new |p| doesn't match */
}
if (p==NULL)
p=q, q=NULL; /* |q| held the other branch of |r| */
}
@ @<If no match ...@>=
if (r==NULL) /* no matches were found */
return add_section_name(par,c,first,last+1,ispref);
@ Although error messages are given in anomalous cases, we do return the
unique best match when a discrepancy is found, because users often
change a title in one place while forgetting to change it elsewhere.
@<If one match found, check for compatibility and return match@>=
switch(section_name_cmp(&first,name_len,r)) {
/* compare all of |r| with new name */
case prefix:
if (!ispref) {
printf("\n! New name is a prefix of <");
@.New name is a prefix...@>
print_section_name(r);
err_print(">");
}
else if (name_len<prefix_length(r)) set_prefix_length(r,name_len);
/* fall through */
case equal: return r;
case extension: if (!ispref || first<=last)
extend_section_name(r,first,last+1,ispref);
return r;
case bad_extension:
printf("\n! New name extends <");
@.New name extends...@>
print_section_name(r);
err_print(">");
return r;
default: /* no match: illegal */
printf("\n! Section name incompatible with <");
@.Section name incompatible...@>
print_prefix_name(r);
printf(">,\n which abbreviates <");
print_section_name(r);
err_print(">");
return r;
}