summaryrefslogtreecommitdiff
path: root/web/latexspider/tie.web
blob: 3739558e1962a2a406f6406178694a0f428c2599 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
% This is TIE.CWB                         as of 25 Oct 89
%---------------------------------------------------------
% (c) 1989 by THD/ITI. Put into the public domain.

%
% Version 1.7 was built alike TIE.WEB Version 1.6                  (89-01-27)
%             but used a command line interface only
% Version 2.0 was revised to include optional tab expansion        (89-02-01)
% Version 2.1 deleted WEB relicts. (-js)			   (89-10-25)
%

% Converted to spidery web Dave Love Feb 91 & bug fixed at line 990
% and took out a couple of `@+' after `*' which break spidery weave

% Here is TeX material that gets inserted after \input cwebmac

\def\hang{\hangindent 3em\indent\ignorespaces}
\font\mc=cmr9
\def\PASCAL{Pascal}
\def\Cl{{\rm C}}
\def\ASCII{{\mc ASCII}}

\def\title{TIE}
\def\topofcontents{\null\vfill
  \centerline{\titlefont The {\ttitlefont TIE} processor}
  \vskip 15pt
  \centerline{(CWEB Version 2.1)}
  \vfill}
\def\botofcontents{
\null\vfill
\item{$\copyright$}1989
   by Technische Hochschule Darmstadt,\hfill\break
Fachbereich Informatik, Institut f\"ur Theoretische Informatik\hfill\break
All rights reserved.\hfill\break
This program is put into the public domain and may be used freely
for any non commercial purposes.
}





@* Introduction.

\noindent Whenever a programmer wants to change a given
\.{WEB} or \.{CWEB} program (referred to as a \.{WEB} program throughout
this program) because of system dependencies, he will
create a new change file.  In addition there may be a second
change file to modify system independent modules of the
program.  But the \.{WEB} file cannot be tangled and weaved
with more than one change file simultaneously.  Therefore,
we introduce the present program to merge a \.{WEB} file and
several change files producing a new \.{WEB} file.  Since
the input files are tied together, the program is called
\.{TIE}.  Furthermore, the program can be used to merge
several change files giving a new single change file.  This
method seems to be more important because it doesn't modify
the original source file.  The use of \.{TIE} can be
expanded to other programming languages since this processor
only knows about the structure of change files and does not
interpret the master file at all.

The program \.{TIE} has to read lines from several input
files to bring them in some special ordering.  For this
purpose an algorithm is used which looks a little bit
complicated.  But the method used only needs one buffer line
for each input file.  Thus the storage requirement of
\.{TIE} does not depend on the input data.

The program is written in \Cl\ and uses only few features of a
particular environment that may need to be changed in other 
installations.
E.g.\ it will not use the |enum| type declarations.
The changes needed may refer to the access of the command line
if this can be not supported by any \Cl\ compiler.

The ``banner line'' defined here should be changed whenever
\.{TIE} is modified.  This program is put into the public
domain.  Nevertheless the copyright notice must not be
replaced or modified.

@d banner="This is TIE, CWEB Version 2.1."
@d copyright=
    "Copyright (c) 1989 by THD/ITI. All rights reserved."


@ The main outline of the program is given in the next section.
This can be used more or less for any \Cl\ program.
@c
@<Global |#include|s@>
@<Global constants@>
@<Global types@>
@<Global variables@>
@<Error handling functions@>
@<Internal functions@>
@<The main function@>

@ Here are some macros for common programming idioms.

@d incr(a) = a+=1 /* increase a variable by unity */
@d decr(a) = a-=1 /* decrease a variable by unity */
@d loop = @+ while (1)@+ /* repeat over and over until a |break| happens */
@d do_nothing =  /* empty statement */
@f loop repeat


@ Furthermore we include the additional types |boolean| and |string|.

@<Global types@>=
#define @!false 0
#define @!true 1
typedef int @!boolean;
typedef char*  string;


@ The following parameters should be sufficient for most
applications of \.{TIE}.
@^system dependencies@>

@<Global constants@>=
#define @!buf_size 512 /* maximum length of one input line */
#define @!max_file_index 9
/* we don't think that anyone needs more than 9 change files,
    but \dots\ just change it */


@ We introduce a history variable that allows us to set a
return code if the operating system can use it.
First we introduce the coded values for the history.
This variable must be initialized.
(We do this even if the value given may be the default for
variables, just to document the need for the initial value.)
@d spotless=0
@d troublesome=1
@d fatal=2

@<Global variables@>=
int @!history=spotless;



@* The character set.

\noindent One of the main goals in the design of \.{TIE} has
been to make it readily portable between a wide variety of
computers.  Yet \.{TIE} by its very nature must use a
greater variety of characters than most computer programs
deal with, and character encoding is one of the areas in
which existing machines differ most widely from each other.

To resolve this problem, all input to \.{TIE} is converted to an
internal seven-bit code that is essentially standard \ASCII{}, the
``American Standard Code for Information Interchange.'' The conversion
is done immediately when each character is read in.  Conversely,
characters are converted from \ASCII{} to the user's external
representation just before they are output.  But the algorithm is
prepared for the usage of eight-bit data. 

\noindent Here is a table of the standard visible \ASCII{} codes:
$$\def\:{\char\count255\global\advance\count255 by 1}
\count255='40
\vbox{
\hbox{\hbox to 40pt{\it\hfill0\/\hfill}%
\hbox to 40pt{\it\hfill1\/\hfill}%
\hbox to 40pt{\it\hfill2\/\hfill}%
\hbox to 40pt{\it\hfill3\/\hfill}%
\hbox to 40pt{\it\hfill4\/\hfill}%
\hbox to 40pt{\it\hfill5\/\hfill}%
\hbox to 40pt{\it\hfill6\/\hfill}%
\hbox to 40pt{\it\hfill7\/\hfill}}
\vskip 4pt
\hrule
\def\^{\vrule height 10.5pt depth 4.5pt}
\halign{\hbox to 0pt{\hskip -24pt\O{#0}\hfill}&\^
\hbox to 40pt{\tt\hfill#\hfill\^}&
&\hbox to 40pt{\tt\hfill#\hfill\^}\cr
04&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
05&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
06&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
07&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
10&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
11&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
12&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
13&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
14&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
15&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
16&\:&\:&\:&\:&\:&\:&\:&\:\cr\noalign{\hrule}
17&\:&\:&\:&\:&\:&\:&\:\cr}
\hrule width 280pt}$$
(Actually, of course, code @'040 is an invisible blank space.)  Code @'136
was once an upward arrow (\.{\char'13}), and code @'137 was
once a left arrow (\.^^X), in olden times when the first draft
of ASCII code was prepared; but \.{TIE} works with today's standard
ASCII in which those codes represent circumflex and underline as shown.
The maximum value used is also defined, it must be changed if an
extended \ASCII{} is used.

If the \Cl\ compiler is not able to process \&{unsigned char}'s, you
should define |ASCII_Code| as \&{short}.
@^system dependencies@>

@<Global types@>=
#define max_ASCII @`~'+1
typedef unsigned char @!ASCII_Code; 
         /* eight-bit numbers, a subrange of the integers */


@ \Cl\ was first implemented on a machine that uses the
\ASCII{} representation for characters. But to make it readily
available also for other machines (big brother is watching?)\
we add a conversion that may be undone for most installations.
\.{TIE} assumes that it is being used
with a character set that contains at least the
characters of standard \ASCII{} as listed above.

In order to accommodate this difference, we shall use the
name |text_char| to stand for the data type of the
characters in the input and output files.  We shall also
assume that |text_char| consists of the elements
|first_text_char| through |last_text_char|,
inclusive.  The following definitions should be adjusted if
necessary.
@^system dependencies@>

@d text_char = @+ unsigned char /* the data type of characters in text files */
@d first_text_char=0 /* ordinal number of the smallest element of |text_char|*/
@d last_text_char=255 /* ordinal number of the largest element of |text_char|*/
@f FILE int

@<Global types@>=
typedef FILE* @!text_file;


@ The \.{TIE} processor converts between \ASCII{} code and
the user's external character set by means of arrays |xord|
and |xchr| that are analogous to \PASCAL's |ord| and |chr|
functions.

The mapping may be disabled by changing the following macro
definitions to just a cast. If your \Cl\ compiler does not support
\&{unsigned char}'s, you should incorporate a binary and with @"ff.
@^system dependencies@>

@d map_xchr(a)=xchr[a] 
         /* change this to |(text_char)(a)| on \ASCII{} machines */
@d map_xord(a)=xord[a] 
         /* change this to |(ASCII_Code)(a)| on \ASCII{} machines */

@<Global variables@>=
ASCII_Code @!xord[last_text_char+1];
         /* specifies conversion of input characters */
text_char @!xchr[max_ASCII+1];
         /* specifies conversion of output characters */


@ If we assume that every system using \.{WEB} is able to
read and write the visible characters of standard \ASCII{}
(although not necessarily using the \ASCII{} codes to
represent them), the following assignment statements
initialize most of the |xchr| array properly, without
needing any system-dependent changes.  For example, the
statement |xchr[@`A']='A'| that appears in the present
\.{WEB} file might be encoded in, say, {\mc EBCDIC} code on
the external medium on which it resides, but \.{CTANGLE} will
convert from this external code to \ASCII{} and back again.
Therefore the assignment statement |xchr[65]='A'| will
appear in the corresponding \Cl\ file, and \Cl\ will
compile this statement so that |xchr[65]| receives the
character \.A in the external code.  Note that it
would be quite incorrect to say |xchr[@`A']=@`A'|, because
|@`A'| is a constant of type |int| not \&{char}, and
because we have |@`A'==65| regardless of the external
character set.

@<Set initial values@>=
xchr[@` ']=' ';
xchr[@`!']='!';
xchr[@`"']='"';
xchr[@`#']='#';@/
xchr[@`$']='$';
xchr[@`%']='%';
xchr[@`&']='&';
xchr[@`'']='\'';@/
xchr[@`(']='(';
xchr[@`)']=')';
xchr[@`*']='*';
xchr[@`+']='+';@/
xchr[@`,']=',';
xchr[@`-']='-';
xchr[@`.']='.';
xchr[@`/']='/';@/
xchr[@`0']='0';
xchr[@`1']='1';
xchr[@`2']='2';
xchr[@`3']='3';@/
xchr[@`4']='4';
xchr[@`5']='5';
xchr[@`6']='6';
xchr[@`7']='7';@/
xchr[@`8']='8';
xchr[@`9']='9';
xchr[@`:']=':';
xchr[@`;']=';';@/
xchr[@`<']='<';
xchr[@`=']='=';
xchr[@`>']='>';
xchr[@`?']='?';@/
xchr[@`@@']='@@';
xchr[@`A']='A';
xchr[@`B']='B';
xchr[@`C']='C';@/
xchr[@`D']='D';
xchr[@`E']='E';
xchr[@`F']='F';
xchr[@`G']='G';@/
xchr[@`H']='H';
xchr[@`I']='I';
xchr[@`J']='J';
xchr[@`K']='K';@/
xchr[@`L']='L';
xchr[@`M']='M';
xchr[@`N']='N';
xchr[@`O']='O';@/
xchr[@`P']='P';
xchr[@`Q']='Q';
xchr[@`R']='R';
xchr[@`S']='S';@/
xchr[@`T']='T';
xchr[@`U']='U';
xchr[@`V']='V';
xchr[@`W']='W';@/
xchr[@`X']='X';
xchr[@`Y']='Y';
xchr[@`Z']='Z';
xchr[@`[']='[';@/
xchr[@`\\']='\\';
xchr[@`]']=']';
xchr[@`^']='^';
xchr[@`_']='_';@/
xchr[@`'']='`';
xchr[@`a']='a';
xchr[@`b']='b';
xchr[@`c']='c';@/
xchr[@`d']='d';
xchr[@`e']='e';
xchr[@`f']='f';
xchr[@`g']='g';@/
xchr[@`h']='h';
xchr[@`i']='i';
xchr[@`j']='j';
xchr[@`k']='k';@/
xchr[@`l']='l';
xchr[@`m']='m';
xchr[@`n']='n';
xchr[@`o']='o';@/
xchr[@`p']='p';
xchr[@`q']='q';
xchr[@`r']='r';
xchr[@`s']='s';@/
xchr[@`t']='t';
xchr[@`u']='u';
xchr[@`v']='v';
xchr[@`w']='w';@/
xchr[@`x']='x';
xchr[@`y']='y';
xchr[@`z']='z';
xchr[@`{']='{';@/
xchr[@`|']='|';
xchr[@`}']='}';
xchr[@`~']='~';@/
xchr[0]=' '; xchr[@"7F]=' '; /* these \ASCII{} codes are not used */


@ Some of the \ASCII{} codes below @"20 have been given a
symbolic name in \.{TIE} because they are used with a special
meaning.

@d tab_mark  = @"09 /* \ASCII{} code used as tab-skip */
@d nl_mark   = @"0A /* \ASCII{} code used as line end marker */
@d form_feed = @"0C /* \ASCII{} code used as page eject */


@ When we initialize the |xord| array and the remaining
parts of |xchr|, it will be convenient to make use of an
index variable, |i|.

@<Local variables for initialisation@>=
int i;


@ Here now is the system-dependent part of the character
set.  If \.{TIE} is being implemented on a garden-variety
\Cl\ for which only standard \ASCII{} codes will appear
in the input and output files, you don't need to make any
changes here.
@^system dependencies@>

Changes to the present module will make \.{TIE} more
friendly on computers that have an extended character set,
so that one can type things like \.^^Z.  If you have an
extended set of characters that are easily incorporated into
text files, you can assign codes arbitrarily here, giving an
|xchr| equivalent to whatever characters the users of
\.{TIE} are allowed to have in their input files, provided
that unsuitable characters do not correspond to special
codes like |tab_mark| that are listed above.

@<Set init...@>=
for (i=1;i<@` ';xchr[i++]=' ');
xchr[tab_mark]='\t';
xchr[form_feed]='\f';
xchr[nl_mark]='\n';


@ The following system-independent code makes the |xord|
array contain a suitable inverse to the information in
|xchr|.

@<Set init...@>=
for ( i=first_text_char ; i<=last_text_char ; xord[i++]=@` ' )  do_nothing;
for ( i=1 ; i<=@`~' ; i++ )  xord[xchr[i]] = i;





@* Input and output.

\noindent Output for the user is done by writing on file |term_out|,
which is assumed to consist of characters of type \&{text\_char}.  It
should be linked to |stdout| usually.  Terminal input is not needed in
this version of \.{TIE}.  |stdin| and |stdout| are predefined if we
include the \.{stdio.h} definitions.  Although I/O redirection for
|stdout| is usually available you may lead output to another file if
you change the definition of |term_out|.  Also we define some macros
for terminating an output line and writing strings to the user.  These
distinguish the number of parameters used to ease translation with a
\.{CWEB} like Sylvio Levy's that does not support a variable number of
parameters for macros. 

@^system dependencies@>
@d term_out=stdout
@d print(a)=fprintf(term_out,a) /* `|print|' means write on the terminal */
@d print2(a)=fprintf(term_out,a) /* same with two arguments */
@d print3(a)=fprintf(term_out,a) /* same with three arguments */
@d print_c(a)=fputc(a,term_out); /* print a single character */
@d new_line(a)=fputc('\n',a) /* start new line */
@d term_new_line=new_line(term_out) /* start new line of the terminal */
@d print_ln(a)={fprintf(term_out,a);term_new_line;}
       /* `|print|' and then start new line */
@d print2_ln(a)={print2(a);term_new_line;} /* same with two arguments */
@d print3_ln(a)={print3(a);term_new_line;} /* same with three arguments */
@d print_nl(a)=  /* print information starting on a new line */
	 {term_new_line; print(a);}
@d print2_nl(a)=  /* same for two arguments */
	 {term_new_line; print2(a);}

@<Global |#include|s@>=
#include <stdio.h>


@ And we need dynamic memory allocation.
This should cause no trouble in any \Cl\ program.
@^system dependencies@>

@<Global |#include|s@>=
extern char *malloc();


@ The |update_terminal| function is called when we want to
make sure that everything we have output to the terminal so
far has actually left the computer's internal buffers and
been sent.
@^system dependencies@>

@d update_terminal = fflush(term_out) /* empty the terminal output buffer */





@* Data structures.

\noindent The multiple input files (master file and change
files) are treated the same way.  To organize the
simultaneous usage of several input files, we introduce the
data type \&{in\_file\_modes}.

The mode |search| indicates that \.{TIE} searches for a
match of the input line with any line of an input file in
|reading| mode.  |test| is used whenever a match is found
and it has to be tested if the next input lines do match
also.  |reading| describes that the lines can be read without
any check for matching other lines.  |ignore| denotes
that the file cannot be used.  This may happen because an
error has been detected or because the end of the file has
been found.

\leavevmode |file_types| is used to describe whether a file
is a master file or a change file. The value |unknown| is added
to this type to set an initial mode for the output file.
This enables us to check whether any option was used to select
the kind of output. (this would even be necessary if we
would assume a default action for missing options.)

@<Global types@>=
#define search 0
#define test 1
#define reading 2
#define ignore 3
typedef int @!in_file_modes; /* should be |enum(search,test,reading,ignore)| */
#define unknown 0
#define master 1
#define chf 2
typedef int @!file_types; /* should be |enum(unknown,master,chf)| */


@ A variable of type |out_md_type| will tell us in what state the output
change file is during processing. |normal| will be the state, when we
did not yet start a change, |pre| will be set when we write the lines
to be changes and |post| will indicate that the replacement lines are
written.

@<Global types@>=
#define normal 0
#define pre 1
#define post 2
typedef int @!out_md_type; /* should be |enum(normal,pre,post)| */


@ Two more types will indicate variables used as an index into either
the file buffer or the file table.

@<Global types@>=
typedef int @!buffer_index; /* |-1..buf_size| */
typedef int @!file_index;  /* |-1..max_file_index+1| */


@ The following data structure joins all informations needed to use
these input files.

@<Global types@>=
typedef struct _idsc{
    string @!name_of_file;
    ASCII_Code @!buffer[buf_size];
    in_file_modes @!mode;
    long @!line;
    file_types @!type_of_file;
    buffer_index @!limit;
    text_file the_file;
    } @!input_description;


@ These data types are used in the global variable section.
They refer to the files in action, the number of change files,
the mode of operation and the current output state.

@<Global variables@>=
file_index @!actual_input,@!test_input,@!no_ch;
file_types @!prod_chf=unknown;
out_md_type @!out_mode;


@ All input files (including the master file) are recorded
in the following structure.
Mostly the components are accessed through a local pointer variable.
This corresponds to \PASCAL's \&{with}-statement
and results in a one-time-computation of the index expression.

@f input_description int

@<Global variables@>=
input_description *@!input_organization[max_file_index+1];





@* File I/O.

\noindent The basic function |get_line| can be used to get a line from
an input file.  The line is stored in the |buffer| part of the
descriptor.  The components |limit| and |line| are updated.  If the
end of the file is reached |mode| is set to |ignore|.  On some systems
it might be useful to replace tab characters by a proper number of
spaces since several editors used to create change files insert tab
characters into a source file not under control of the user.  So it
might be a problem to create a matching change file. 
@^tab character expansion@>

We define |get_line| to read a line from a file specified by
the corresponding file descriptor.

@<Internal functions@>=
@h void get_line(i)
	file_index i;
{register input_description *inp_desc=input_organization[i];
    if (inp_desc->mode==ignore) return;
    if (feof(inp_desc->the_file))
        @<Handle end of file and return@>;
    @<Get line into buffer@>;
}


@ End of file is special if this file is the master file.
Then we set the global flag variable |input_has_ended|.

@<Handle end of file ...@>=
{
  inp_desc->mode=ignore;
  inp_desc->limit=-1;  /* mark end-of-file */
  if (inp_desc->type_of_file==master) input_has_ended=true;
  return;
}


@ This variable must be declared for global access.

@<Global variables@>=
static boolean input_has_ended=false;


@ Lines must fit into the buffer completely.
We read all characters sequentially until an end of line is found
(but do not forget to check for |EOF|!).
Too long input lines will be truncated.
This will result in a damaged output if they occur in the
replacement part of a change file, or in an incomplerte check if the
matching part is concerned.
Tab character expansion might be done here.
@^tab character expansion@>

@<Get line into buffer@>=
{int final_limit; /* used to delete trailing spaces */
 int c; /* the actual character read */
 @<Increment the line number and print a progess report at
  certain times@>
 inp_desc->limit=final_limit=0;
 while (inp_desc->limit<buf_size) {
   c=fgetc(inp_desc->the_file);
   @<Check |c| for |EOF|, |return| if line was empty, otherwise
	|break| to process last line@>
   inp_desc->buffer[inp_desc->limit++]=c=map_xord(c);
   if (c==nl_mark) break; /* end of line found */
   if (c!=@` ' && c!=tab_mark)
      final_limit=inp_desc->limit;
 }
 @<Test for truncated line, skip to end of line@>
 inp_desc->limit=final_limit;
}


@ This section does what its name says. Every 100 lines
in the master file we print a dot, every 500 lines the number
of lines is shown.

@<Increment the line number and print ...@>=
incr(inp_desc->line);
if (inp_desc->type_of_file==master && inp_desc->line % 100==0) {
   if (inp_desc->line % 500 == 0)  print2("%ld",inp_desc->line);
   else  print_c('.');
   update_terminal;
}


@ There may be incomplete lines of the editor used does
not make sure that the last character before end of file
is an end of line. In such a case we must process the
final line. Of the current line is empty, we just can \&{return}.
Note that this test must be done {\sl before} the character read
is translated.
@^system dependencies@>

@<Check |c| for |EOF|...@>=
  if (c==EOF) {
    if (inp_desc->limit<=0) {
       inp_desc->mode=ignore;
       inp_desc->limit=-1;  /* mark end-of-file */
       if (inp_desc->type_of_file==master) input_has_ended=true;
       return;
    } else { /* add end of line mark */
	c=nl_mark;
	break;
    }
  }


@ If the line is truncated we must skip the rest
(and still watch for |EOF|!).
@<Test for truncated line, skip to end of line@>=
if (c!=nl_mark) {
   err_print("! Input line too long")(i);
@.Input line too long@>
   while ( (c=fgetc(inp_desc->the_file)) != EOF  &&  map_xord(c) != nl_mark )
      do_nothing; /* skip to end */
   }





@* Reporting errors to the user.

\noindent There may be errors if a line in a given change
file does not match a line in the master file or a
replacement in a previous change file.  Such errors are
reported to the user by saying
$$
   \hbox{|err_print("! Error message")(file_no)|;}
$$
where |file_no| is the number of the file which is concerned by the
error.  Please note that no trailing dot is supplied by the error
message because it is appended by |err_print|.

This function is implemented as a macro.  It gives a message and an
indication of the offending file.  The actions to determine the error
location are provided by a function called |err_loc|. 

@d error_loc(a) = err_loc(a); history=troublesome; @+ }
@d err_print(a) = { @+ print_nl(a); error_loc

@<Error handling...@>=
@h void err_loc(i) /* prints location of error */
        int i;
{
    print3_ln(" (file %s, l.%ld).",
	input_organization[i]->name_of_file,
	input_organization[i]->line);
}


@ Non recoverable errors are handled by calling |fatal_error| that
outputs a message and then calls `|jump_out|'.  |err_print| will print
the error message followed by an indication of where the error was
spotted in the source files.  |fatal_error| cannot state any files
because the problem is usually to access these. 

@d fatal_error(a) = { 
         print(a); print_c('.'); history=fatal;
	 term_new_line; jump_out();
	 }


@ |jump_out| just cuts across all active procedure levels and jumps
out of the program.  It is used when no recovery from a particular
error has been provided.  The return code from this program should be
regarded by the caller. 

@d jump_out(a) = exit(1)





@* Handling multiple change files.

\noindent In the standard version we take the name of the
files from the command line.
It is assumed that filenames can be used as given in the
command line without changes.

First there are some sections to open all files.
If a file is not accessible, the run will be aborted.
Otherwise the name of the open file will be displayed.

@<Prepare the output file@>=
{
    out_file=fopen(out_name,"w");
    if (out_file==NULL) {
	 fatal_error("! Could not open/create output file");
@.Could not open/create output file@>
    }
}


@ The name of the file and the file desciptor are stored in
global variables.

@<Global variables@>=
text_file @!out_file;
string @!out_name;


@ For the master file we start just reading its first line
into the buffer, if we could open it.

@<Get the master file started@>=
{  input_organization[0]->the_file=
	  fopen(input_organization[0]->name_of_file,"r");
   if (input_organization[0]->the_file==NULL)
    fatal_error("! Could not open master file");
@.Could not open master file@>
   print2("(%s)",input_organization[0]->name_of_file);
   term_new_line;
   input_organization[0]->type_of_file=master;
   get_line(0);
}

@ For the change files we must skip the comment part and
see, whether we can find any change in it.
This is done by |init_change_file|.

@<Prepare the change files@>=
{file_index i;
  i=1;
  while (i<no_ch) {
    input_organization[i]->the_file=
	fopen(input_organization[i]->name_of_file,"r");
    if (input_organization[i]->the_file==NULL)
	fatal_error("!Could not open change file");
@.Could not open change file@>
    print2("(%s)",input_organization[i]->name_of_file);
    term_new_line;
    init_change_file(i,true);
    incr(i);
  }
}





@*Input/output organization.

\noindent Here's a simple function that checks if two lines
are different.

@<Internal functions@>=
@h boolean lines_dont_match(i,j) 
	file_index i,j;
{
   buffer_index k,lmt;
   if (input_organization[i]->limit != input_organization[j]->limit)
      return(true);
   lmt=input_organization[i]->limit;
   for ( k=0 ; k<lmt ; k++ )
      if (input_organization[i]->buffer[k] != input_organization[j]->buffer[k])
         return(true);
  return(false);
}


@ Function |init_change_file(i,b)| is used to ignore all
lines of the input file with index |i| until the next change
module is found.  The boolean parameter |b| indicates
whether we do not want to see \.{@@x} or \.{@@y} entries during
our skip.

@<Internal functions@>=
@h void init_change_file(i,b)
	file_index i; boolean b;
{register input_description *inp_desc=input_organization[i];
  @<Skip over comment lines; |return| if end of file@>;
  @<Skip to the next nonblank line; |return| if end of file@>;
}


@ 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} or \.{@@z} (which would
probably indicate that the change file is fouled up).

@<Skip over comment lines...@>=
loop@+  {ASCII_Code c;
  get_line(i);
  if (inp_desc->mode==ignore) return;
  if (inp_desc->limit<2) continue;
  if (inp_desc->buffer[0] != @`@@') continue;
  c=inp_desc->buffer[1];
  if (c>=@`X'  && c<=@`Z')
    c+=@`z'-@`Z'; /*lowercasify*/
  if (c==@`x') break;
  if (c==@`y' || c==@`z')
    if (b) /* scanning for start of change */
          err_print("! Where is the matching @@x?")(i);
@.Where is the match...@>
}


@ Here we are looking at lines following the \.{@@x}.

@<Skip to the next nonblank line...@>=
do{
  get_line(i);
  if (inp_desc->mode==ignore) {
	  err_print("! Change file ended after @@x")(i);
@.Change file ended...@>
          return;
  }
} while (inp_desc->limit<=0);


@ The |put_line| function is used to write a line from
input buffer |j| to the output file.

@<Internal functions@>=
@h void put_line(j)
	file_index j;
{buffer_index i; /* index into the buffer */
 buffer_index lmt; /* line length */
 string p; /* output pointer */
  lmt=input_organization[j]->limit;
  p=input_organization[j]->buffer;
  for (i=0;i<lmt;i++) fputc(map_xchr(*p++),out_file);
  new_line(out_file);
}


@ The function |e_of_ch_module| returns true if the input
line from file |i| starts with \.{@@z}.

@<Internal functions@>=
@h boolean e_of_ch_module(i)
	file_index i;
{register input_description *inp_desc=input_organization[i];
    if (inp_desc->limit<0) {
      print_nl("! At the end of change file missing @@z ");
@.At the end of change file...@>
      print2("%s",input_organization[i]->name_of_file);
      term_new_line;
      return(true);
    } else if (inp_desc->limit>=2)  if (inp_desc->buffer[0]==@`@@' &&
	    (inp_desc->buffer[1]==@`Z' || inp_desc->buffer[1]==@`z'))
        return(true);
    return(false);
}


@ The function |e_of_ch_preamble| returns |true| if the input
line from file |i| starts with \.{@@y}.

@<Internal functions@>=
@h boolean e_of_ch_preamble(i)
	file_index i;
{register input_description *inp_desc=input_organization[i];
    if (inp_desc->limit>=2 && inp_desc->buffer[0]==@`@@')
      if (inp_desc->buffer[1]==@`Y'||inp_desc->buffer[1]==@`y') return(true);
    return(false);
}



@ To process the input file the next section
reads a line of the actual input file and updates the
|input_organization| for all files with index |test_file| greater
|actual_input|.

@<Process a line, |break| when end of source reached@>=
{file_index test_file;
  @<Check the current files for any ends of changes@>
  if (input_has_ended && actual_input==0) break; /* all done */
  @<Scan all other files for changes to be done@>
  @<Handle output@>
  @<Step to next line@>
}


@ Any of the current change files may have reached the end of change.
In such a case intermediate lines must be skipped and the next start
of change is to be found. This may make a change file inactive if
it reaches end of file.

@<Check the...@>=
{register input_description *inp_desc;
while (e_of_ch_module(actual_input)) {
  inp_desc=input_organization[actual_input];
  if (inp_desc->type_of_file==master) {
        /* emergency exit, everything mixed up!*/
        fatal_error("! This can't happen: change file is master file");
@.This can't happen...@>
  }
  inp_desc->mode=search;
  init_change_file(actual_input,true);
  while ((input_organization[actual_input]->mode!=reading
           && actual_input>0))  decr(actual_input);
}
}


@ Now we will set |test_input| to the file that has another match
for the current line. This depends on the state of the other
change files. If no other file matches, |actual_input| refers to
a line to write and |test_input| ist set to |none|.

@d none = (max_file_index+1)

@<Scan all other files...@>=
test_input=none;
test_file=actual_input;
while (test_input==none && test_file</*no\_ch*/no_ch-1) { /* bug fix DL */
  incr(test_file);
  switch (input_organization[test_file]->mode) {
  case search: if (lines_dont_match(actual_input,test_file)==false) {
	      input_organization[test_file]->mode=test;
	      test_input=test_file;
               }
              break;
  case test:  if (lines_dont_match(actual_input, test_file)==true) {
                /* error, sections do not match */
		input_organization[test_file]->mode=search;
                err_print("! Sections do not match")(actual_input);
@.Sections do not match@>
		err_loc(test_file);
		init_change_file(test_file,false);
	      } else test_input=test_file;
              break;
  case reading: do_nothing; /* this can't happen */
	      break;
  case ignore: do_nothing; /* nothing to do */
	      break;
  }
}


@ For the output we must distinguish whether we create a new change
file or a new master file.  The change file creation needs some closer
inspection because we may be before a change, in the pattern part or
in the replacement part.  For a master file we have to write the line
from the current actual input. 

@<Handle output@>=
if (prod_chf==chf) {
  loop @+ {
    @<Test for normal, |break| when done@>
    @<Test for pre, |break| when done@>
    @<Test for post, |break| when done@>
  }
} else
if (test_input==none) put_line(actual_input);


@ Check whether we have to start a change file entry.
Without a match nothing must be done.

@<Test for normal...@>=
if (out_mode==normal) {
    if (test_input!=none) {
	fputc(map_xchr(@`@@'),out_file); fputc(map_xchr(@`x'),out_file);
	new_line(out_file);
        out_mode=pre;
    } else break;
}


@ Check whether we have to start the replacement text.
This is the case when we have no more matching line.
Otherwise the master file source line must be copied to the
change file.
@<Test for pre...@>=

  if (out_mode==pre) {
    if (test_input==none) {
      fputc(map_xchr(@`@@'),out_file); fputc(map_xchr(@`y'),out_file);
      new_line(out_file);
      out_mode=post;
    } else {
      if (input_organization[actual_input]->type_of_file==master)
        put_line(actual_input);
      break;
    }
  }


@ Check whether an entry from a change file is complete.
If the current change was a change for a change file in effect,
then this change file line must be written.
If the actual input has been reset to the master file we
can finish this change.

@<Test for post...@>=
  if (out_mode==post) {
    if (input_organization[actual_input]->type_of_file==chf) {
	if (test_input==none)  put_line(actual_input);
        break;
    } else {
	fputc(map_xchr(@`@@'),out_file); fputc(map_xchr(@`z'),out_file);
	new_line(out_file);
	new_line(out_file);
        out_mode=normal;
    }
  }


@ If we had a change, we must proceed in the actual file
to be changed and in the change file in effect.

@<Step to next line@>=
get_line(actual_input);
if (test_input!=none) {
  get_line(test_input);
  if (e_of_ch_preamble(test_input)==true) {
    get_line(test_input); /* update current changing file */
    input_organization[test_input]->mode=reading;
    actual_input=test_input;
    test_input=none;
  }
}


@ To create the new output file we have to scan the whole
master file and all changes in effect when it ends.
At the very end it is wise to check for all changes
to have completed--in case the last line of the master file
was to be changed.

@<Process the input@>=
actual_input=0;
input_has_ended=false;
while (input_has_ended==false||actual_input!=0)
   @<Process a line...@>
if (out_mode==post) { /* last line has been changed */
   fputc(map_xchr(@`@@'),out_file); fputc(map_xchr(@`z'),out_file);
   new_line(out_file);
   }


@ 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 the master file or any of the change files.

@<Check that all changes have been read@>=
{file_index i;
for (i=1;i<no_ch;i++) {/* all change files */
   if (input_organization[i]->mode!=ignore)
      err_print("! Change file entry did not match")(i);
@.Change file entry ...@>
   }
}


@ We want to tell the user about our command line options.  This is
done by the |usage()| function.  It contains merely the necessary
print statement and exits afterwards. 

@<Intern...@>=
@h void usage()
{
   print("Usage: tie -[mc] outfile master changefile(s)");
   term_new_line;
   jump_out();
}


@ We must scan through the list of parameters, given in |argv|.  The
number is in |argc|.  We must pay attention to the flag parameter.  We
need at least 5~parameters and can handle up to |max_file_index|
change files.  The names fo the file parameters will be inserted into
the structure of |input_organization|.  The first file is special.  It
indicates the output file.  When we allow flags at any position, we
must find out which name is for what purpose.  The master file is
already part of the |input_organization| structure (index~0).  As long
as the number of files found (counted in |no_ch|) is |-1| we have not
yet found the output file name. 

@<Scan the parameters@>=
{int act_arg,act_orgpart;
    if ( argc < 5  ||  argc > max_file_index+4-1 )  usage();
    no_ch = -1; /* fill this part of |input_organization| */
    for ( act_arg=1 ; act_arg<argc ; act_arg++ ) {
	if (argv[act_arg][0]=='-') @<Set a flag@>
	else @<Get a file name@>
    }
    if (no_ch<=0|| prod_chf==unknown)  usage();
}


@ The flag is about to determine the processing mode.
We must make sure that this flag has not been set before.
Further flags might be introduced to avoid/force overwriting of
output files.
Currently we just have to set the processing flag properly.

@<Set a flag@>=
if (prod_chf!=unknown)  usage();
else
   switch (argv[act_arg][1]) {
      case 'c':
      case 'C': prod_chf=chf;  break;
      case 'm':
      case 'M':	prod_chf=master;  break;
      default:  usage(); 
      }


@ We have to distinguish whether this is the very first file name
(known if |no_ch==(-1)|) or if the next element of |input_organization|
must be filled.

@<Get a file name@>=
{   if (no_ch==(-1)) {
	out_name=argv[act_arg];
    } else { register input_description *inp_desc;
	inp_desc=(input_description *)
		malloc(sizeof(input_description));
	if (inp_desc==NULL)
		fatal_error("! No memory for descriptor");
@.No memory for descriptor@>
	inp_desc->mode=search;
	inp_desc->line=0;
	inp_desc->type_of_file=chf;
	inp_desc->limit=0;
	inp_desc->name_of_file=argv[act_arg];
	input_organization[no_ch]=inp_desc;
    }
    incr(no_ch);
}


@* The main program.

\noindent Here is where \.{TIE} starts, and where it ends.

@<The main function@>=
@h main(argc,argv)
        int argc; string *argv;
{{@<Local variables for initialisation@>
  @<Set initial...@>
 }
  print_ln(banner); /* print a ``banner line'' */
  print_ln(copyright); /* include the copyright notice */
  actual_input=0;
  out_mode=normal;
  @<Scan the parameters@>
  @<Prepare the output file@>
  @<Get the master file started@>;
  @<Prepare the change files@>
  @<Process the input@>;
  @<Check that all changes have been read@>;
  @<Print the job |history|@>;
}

@ We want to pass the |history| value to the operating system so that
it can be used to govern whether or not other programs are started. 
Additionaly we report the history to the user, although this may not
be ``{\mc UNIX}'' style---but we are in best companion: \.{WEB} and
\TeX{} do the same. 
@^system dependencies@>

@<Print the job |history|@>=
{string msg;
   switch (history) {
      case spotless: msg="No errors were found"; break;
      case troublesome: msg="Pardon me, but I think I spotted something wrong.";
	        break;
      case fatal: msg="That was a fatal error, my friend";  break;
      } /* there are no other cases */
   print2_nl("(%s.)",msg);  term_new_line;
   exit ( history == spotless  ?  0 : 1 );
}





@* System-dependent changes.

\noindent This section should be replaced, if necessary, by
changes to the program that are necessary to make \.{TIE}
work at a particular installation.  It is usually best to
design your change file so that all changes to previous
modules preserve the module numbering; then everybody's
version will be consistent with the printed program.  More
extensive changes, which introduce new modules, can be
inserted here; then only the index itself will get a new
module number.
@^system dependencies@>





@* Index.

\noindent Here is the cross-reference table for the \.{TIE}
processor.