-
Notifications
You must be signed in to change notification settings - Fork 11
/
common.w
1286 lines (1130 loc) · 46.7 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 4.12 --- August 2024
% 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.
% Amendments to 'common.w' resulting in this updated version were created
% by numerous collaborators over the course of many years.
% Please send comments, suggestions, etc. to tex-k@@tug.org.
\def\v{\char'174} % vertical (|) in typewriter font
\def\title{Common code for CTANGLE and CWEAVE (Version 4.12)}
\def\topofcontents{\null\vfill
\centerline{\titlefont Common code for {\ttitlefont CTANGLE} and
{\ttitlefont CWEAVE}}
\vskip 15pt
\centerline{(Version 4.12)}
\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
@* 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
@<Common code for \.{CWEAVE} and \.{CTANGLE}@>@/
@<Global variables@>@/
@<Predeclaration of procedures@>
@ The details will be filled in due course. The interface |"common.h"| of this
\.{COMMON} module is included first. It is also used by the main programs.
@i common.h
@ 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|.
@<Global var...@>=
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.
@<Global var...@>=
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(void)
{
@<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.
A few character pairs are encoded internally as single characters,
using the definitions in the interface sections above. 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 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@>
@<Global var...@>=
char section_text[longest_name+1]; /* text being sought for */
char *section_text_end = section_text+longest_name; /* end of |section_text| */
char *id_first; /* where the current identifier begins in the buffer */
char *id_loc; /* just after the current identifier in the buffer */
@* 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 |true| if the read is successful and
|false| 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.
@<Global var...@>=
char buffer[long_buf_size]; /* where each line of input goes */
char *buffer_end=buffer+buf_size-2; /* end of |buffer| */
char *loc=buffer; /* points to the next character to be read from the buffer */
char *limit=buffer; /* points to the last character in the buffer */
@ In the unlikely event that your standard I/O library does not
support |feof| and |getc| you may have to change things here.
@^system dependencies@>
@c
static boolean input_ln( /* copies a line into |buffer| or returns |false| */
FILE *fp) /* what file to read from */
{
int c=EOF; /* character read; initialized so some compilers won't complain */
char *k; /* where next character goes */
if (feof(fp)) return false; /* 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) {
while ((c=getc(fp))!=EOF && c!='\n'); /* discard rest of line */
loc=buffer; err_print("! Input line too long");
@.Input line too long@>
}
if (c==EOF && limit==buffer) return false; /* there was nothing after
the last newline */
return true;
}
@ @<Predecl...@>=@+static boolean input_ln(FILE *);
@* File handling. 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}.
@<Global var...@>=
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 */
static 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=false; /* if the web file is being read */
@ When |changing==false|, 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, (size_t)(limit-buffer)) != 0)
@<Global var...@>=
static char change_buffer[buf_size]; /* next line of |change_file| */
static 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 |true|; hence error messages will be reported correctly.
@c
static void
prime_the_change_buffer(void)
{
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|@>@;
}
@ @<Predecl...@>=@+static void prime_the_change_buffer(void);
@ 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(true) {
change_line++;
if (!input_ln(change_file)) return;
if (limit<buffer+2) continue;
if (buffer[0]!='@@') continue;
if (xisupper(buffer[1])) buffer[1]=tolower((int)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+(ptrdiff_t)(limit-buffer);
strncpy(change_buffer,buffer,(size_t)(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 |false|.
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
static void
check_change(void) /* switches to |change_file| if the buffers match */
{
int n=0; /* the number of discrepancies found */
if (lines_dont_match) return;
change_pending=false;
if (!changed_section[section_count]) {
if_section_start_make_pending(true);
if (!change_pending) changed_section[section_count]=true;
}
while (true) {
changing=print_where=true; change_line++;
if (!input_ln(change_file)) {
err_print("! Change file ended before @@y");
@.Change file ended...@>
change_limit=change_buffer; changing=false;
return;
}
if (limit>buffer+1 && buffer[0]=='@@') {
if (xisupper(buffer[1])) buffer[1]=tolower((int)buffer[1]);
@<If the current line starts with \.{@@y},
report any discrepancies and |return|@>@;
}
@<Move |buffer| and |limit|...@>@;
changing=false; 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=true; return;
}
include_depth--; cur_line++;
}
if (lines_dont_match) n++;
}
}
@ @<Predecl...@>=@+static void check_change(void);
@ @<If the current line starts with \.{@@y}...@>=
if (buffer[1]=='x' || buffer[1]=='z') {
loc=buffer+2; err_print("! Where is the matching @@y?");
@.Where is the match...@>
}
else if (buffer[1]=='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 |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.
@c
boolean get_line(void) /* 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 false;
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 true;
}
@ 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;}
@<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 *kk, *k=cur_file_name;
size_t 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=true;
goto restart; /* success */
}
if ((kk=getenv("CWEBINPUTS"))!=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 */
@^system dependencies@>
if ((cur_file=fopen(cur_file_name,"r"))!=NULL) {
cur_line=0; print_where=true;
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=true;
if (include_depth==0) {input_has_ended=true; 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(false);
if (change_pending) {
changed_section[section_count]=true; change_pending=false;
}
}
*limit=' ';
if (buffer[0]=='@@') {
if (xisupper(buffer[1])) buffer[1]=tolower((int)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=true;
}
}
}
}
@ 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(void) {
if (change_limit!=change_buffer) { /* |changing| is |false| */
strncpy(buffer,change_buffer,(size_t)(change_limit-change_buffer+1));
limit=buffer+(ptrdiff_t)(change_limit-change_buffer);
changing=true; change_depth=include_depth; loc=buffer;
err_print("! Change file entry did not match");
@.Change file entry did not match@>
}
}
@ 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(void)
{
limit=buffer; loc=buffer+1; buffer[0]=' ';
@<Open input files@>@;
include_depth=cur_line=change_line=0;
change_depth=include_depth;
changing=true; prime_the_change_buffer(); changing=!changing;
limit=buffer; loc=buffer+1; buffer[0]=' '; input_has_ended=false;
}
@ 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=true;
if ((change_file=fopen(change_file_name,"r"))==NULL)
fatal("! Cannot open change file ", change_file_name);
@ @<Global var...@>=
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=false; /* should \.{CTANGLE} print line and file info? */
@* 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|. You find the
complete layout of |name_info| in the interface sections above.
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 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.
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|.
The last component of |name_info| is different for \.{CTANGLE} and
\.{CWEAVE}. In \.{CTANGLE}, if |p| is a pointer to a section name,
|p->equiv| is a pointer to its replacement text, an element of the
array |text_info|. In \.{CWEAVE}, on the other hand, if
|p| points to an identifier, |p->xref| is a pointer to its
list of cross-references, an element of the array |xmem|. The make-up
of |text_info| and |xmem| is discussed in the \.{CTANGLE} and \.{CWEAVE}
source files, respectively; here we just declare a common field
|equiv_or_xref| as a pointer to |void|.
@<Global var...@>=
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 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|.
@<Global var...@>=
char *byte_ptr; /* first unused position in |byte_mem| */
name_pointer name_ptr; /* first unused position in |name_dir| */
@ @<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 */
root=NULL; /* the binary search tree starts out with nothing in it */
@ 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 */
@<Global var...@>=
name_pointer hash[hash_size]={NULL}; /* heads of hash lists */
hash_pointer hash_end = hash+hash_size-1; /* end of |hash| */
hash_pointer hash_ptr; /* index into hash-head array */
@ Initially all the hash lists are empty.
@ Here is the main procedure for finding identifiers:
@c
name_pointer
id_lookup( /* looks up a string in the identifier table */
const char *first, /* first character of string */
const char *last, /* last character of string plus one */
eight_bits t) /* the |ilk|; used by \.{CWEAVE} only */
{
const char *i=first; /* position in |buffer| */
int h; /* hash code */
size_t 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=(size_t)(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=(int)((eight_bits)*i);
while (++i<last) h=(h+h+(int)((eight_bits)*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}.
@<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) p->ilk=t, init_node(p);
}
@ 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) (size_t)((eight_bits)*((p)->byte_start)*256 +
(eight_bits)*((p)->byte_start+1))
@d set_prefix_length(p,m) (*((p)->byte_start)=(char)((m)/256),
*((p)->byte_start+1)=(char)((m)%256))
@c
void
print_section_name(
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,q=NULL;
term_write(s,(size_t)(ss-s));
s = p->byte_start;
}
if (q) term_write("...",3); /* complete name not yet known */
}
@ @c
void
sprint_section_name(
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,(size_t)(ss-s)), dest+=ss-s;
s = p->byte_start;
}
*dest='\0';
}
@ @c
void
print_prefix_name(
name_pointer p)
{
char *s = first_chunk(p);
size_t 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 */
@<Predecl...@>=
static int web_strcmp(char *,size_t,char *,size_t);@/
static name_pointer add_section_name(name_pointer,int,char *,char *,boolean);@/
static void extend_section_name(name_pointer,char *,char *,boolean);
@ @c
static int web_strcmp( /* fuller comparison than |strcmp| */
char *j, /* beginning of first string */
size_t j_len, /* length of first string */
char *k, /* beginning of second string */
size_t k_len) /* length of second string */
{
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}.
@c
static name_pointer
add_section_name( /* 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 */
boolean ispref) /* are we adding a prefix or a full name? */
{
name_pointer p=name_ptr; /* new node */
char *s=first_chunk(p);
size_t name_len=(size_t)(last-first+(int)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=p->rlink=NULL;
init_node(p);
return par==NULL ? (root=p) : c==less ? (par->llink=p) : (par->rlink=p);
}
@ @c
static void
extend_section_name(
name_pointer p, /* name to be extended */
char *first, /* beginning of extension text */
char *last, /* one beyond end of extension text */
boolean ispref) /* are we adding a prefix or a full name? */
{
char *s;
name_pointer q=p+1;
size_t name_len=(size_t)(last-first+(int)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( /* find or install section name in tree */
char *first,char *last, /* first and last characters of new name */
boolean ispref) /* is the new name a prefix or a full name? */
{
int c=less; /* 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| */
size_t name_len=(size_t)(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("%s","\n! Ambiguous prefix: matches <");
@.Ambiguous prefix ... @>
print_prefix_name(p);
printf("%s",">\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("%s","\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: break;
case extension: if (!ispref || first<=last)
extend_section_name(r,first,last+1,ispref);
break;
case bad_extension:
printf("%s","\n! New name extends <");
@.New name extends...@>
print_section_name(r);
err_print(">");
break;
default: /* no match: illegal */
printf("%s","\n! Section name incompatible with <");
@.Section name incompatible...@>
print_prefix_name(r);
printf("%s",">,\n which abbreviates <");
print_section_name(r);
err_print(">");
}
return r;
@ The return codes of |section_name_cmp|, which compares a string with
the full name of a section, are those of |web_strcmp| plus
|bad_extension|, used when the string is an extension of a
supposedly already complete section name. This function has a side
effect when the comparison string is an extension: It advances the
address of the first character of the string by an amount equal to
the length of the known part of the section name.
The name \.{@@<foo...@@>} should be an acceptable ``abbreviation''
for \.{@@<foo@@>}. If such an abbreviation comes after the complete
name, there's no trouble recognizing it. If it comes before the
complete name, we simply append a null chunk. This logic requires
us to regard \.{@@<foo...@@>} as an ``extension'' of itself.
@d bad_extension 5
@c
static int section_name_cmp(
char **pfirst, /* pointer to beginning of comparison string */
size_t len, /* length of string */
name_pointer r) /* section name being compared */
{
char *first=*pfirst; /* beginning of comparison string */
name_pointer q=r+1; /* access to subsequent chunks */
char *ss, *s=first_chunk(r);
int c=less; /* comparison */
boolean ispref; /* is chunk |r| a prefix? */
while (true) {
ss=(r+1)->byte_start-1;
if (*ss==' ' && ss>=r->byte_start) ispref=true,q=q->link;
else ispref=false,ss++,q=name_dir;
switch(c=web_strcmp(first,len,s,(size_t)(ss-s))) {
case equal: if (q==name_dir)
if (ispref) {
*pfirst=first+(ptrdiff_t)(ss-s);
return extension; /* null extension */
} else return equal;
else return length(q)==0? equal: prefix;
case extension:
if (!ispref) return bad_extension;
first += ss-s;
if (q!=name_dir) {len -= (int)(ss-s); s=q->byte_start; r=q; continue;}
*pfirst=first; return extension;
default: return c;
}
}
}
@ @<Predec...@>=@+static int section_name_cmp(char **,size_t,name_pointer);
@* Reporting errors to the user.
A global variable called |history| will contain one of four values
at the end of every run: |spotless| means that no unusual messages were
printed; |harmless_message| means that a message of possible interest
was printed but no serious errors were detected; |error_message| means that
at least one error was found; |fatal_message| means that the program
terminated abnormally. The value of |history| does not influence the
behavior of the program; it is simply computed for the convenience
of systems that might want to use such information.
@<Global var...@>=
int history=spotless; /* indicates how bad this run was */