beta-0.89.2
[luatex.git] / source / texk / web2c / tiedir / tie.c
blobccf39c03c63139f7270731cedb8452f0ecfd28c9
1 #define banner "This is TIE, CWEB Version 2.4."
2 #define copyright \
3 "Copyright (c) 1989,1992 by THD/ITI. All rights reserved." \
6 #define incr(v)v+= 1
7 #define decr(v)v-= 1
8 #define loop while(1)
9 #define do_nothing
10 #define false 0
11 #define true 1
12 #define spotless 0
13 #define troublesome 1
14 #define fatal 2 \
16 #define first_text_char 0
17 #define last_text_char 255 \
19 #define map_xchr(c)(text_char)(c) \
21 #define map_xord(c)(ASCII_Code)(c) \
24 #define tab_mark 9
25 #define nl_mark 10
26 #define form_feed 12 \
29 #define term_out stdout
30 #define print(a)fprintf(term_out,a)
31 #define print2(a,b)fprintf(term_out,a,b)
32 #define print3(a,b,c)fprintf(term_out,a,b,c)
33 #define print_c(v)fputc(v,term_out);
34 #define new_line(v)fputc('\n',v)
35 #define term_new_line new_line(term_out) \
37 #define print_ln(v){fprintf(term_out,v);term_new_line;} \
39 #define print2_ln(a,b){print2(a,b);term_new_line;}
40 #define print3_ln(a,b,c){print3(a,b,c);term_new_line;} \
42 #define print_nl(v){term_new_line;print(v);} \
44 #define print2_nl(a,b){term_new_line;print2(a,b);} \
47 #define update_terminal fflush(term_out) \
53 #define error_loc(m)err_loc(m);history= troublesome;}
54 #define err_print(m){print_nl(m);error_loc \
56 #define fatal_error(m){ \
57 print(m);print_c('.');history= fatal; \
58 term_new_line;jump_out(); \
59 } \
62 #define jump_out()exit(1) \
68 #define none (max_file_index+1) \
70 /*2:*/
71 #line 112 "tie.w"
73 /*15:*/
74 #line 478 "tie.w"
76 #include <stdio.h>
79 /*:15*//*16:*/
80 #line 486 "tie.w"
82 #ifdef __STDC__
83 #include <stdlib.h>
84 #else
85 #include <malloc.h>
86 #endif
88 /*:16*/
89 #line 113 "tie.w"
91 /*5:*/
92 #line 142 "tie.w"
94 #define buf_size 512
95 #define max_file_index 9
100 /*:5*/
101 #line 114 "tie.w"
103 /*4:*/
104 #line 133 "tie.w"
106 typedef int boolean;
107 typedef char*string;
110 /*:4*//*7:*/
111 #line 224 "tie.w"
113 #define max_ASCII (126+1)
114 typedef unsigned char ASCII_Code;
118 /*:7*//*8:*/
119 #line 250 "tie.w"
121 typedef unsigned char text_char;
122 typedef FILE*text_file;
125 /*:8*//*18:*/
126 #line 529 "tie.w"
128 #define search 0
129 #define test 1
130 #define reading 2
131 #define ignore 3
132 typedef int in_file_modes;
133 #define unknown 0
134 #define master 1
135 #define chf 2
136 typedef int file_types;
139 /*:18*//*19:*/
140 #line 547 "tie.w"
142 #define normal 0
143 #define pre 1
144 #define post 2
145 typedef int out_md_type;
148 /*:19*//*20:*/
149 #line 557 "tie.w"
151 typedef int buffer_index;
152 typedef int file_index;
155 /*:20*//*21:*/
156 #line 566 "tie.w"
158 typedef struct _idsc{
159 string name_of_file;
160 ASCII_Code buffer[buf_size];
161 in_file_modes mode;
162 long line;
163 file_types type_of_file;
164 buffer_index limit;
165 text_file the_file;
166 }input_description;
169 /*:21*/
170 #line 115 "tie.w"
172 /*6:*/
173 #line 159 "tie.w"
175 static int history= spotless;
179 /*:6*//*9:*/
180 #line 270 "tie.w"
182 static ASCII_Code xord[last_text_char+1];
184 static text_char xchr[max_ASCII+1];
188 /*:9*//*22:*/
189 #line 582 "tie.w"
191 static file_index actual_input,test_input,no_ch;
192 static file_types prod_chf= unknown;
193 static out_md_type out_mode;
196 /*:22*//*23:*/
197 #line 594 "tie.w"
199 static input_description*input_organization[max_file_index+1];
205 /*:23*//*26:*/
206 #line 641 "tie.w"
208 static boolean input_has_ended= false;
211 /*:26*//*35:*/
212 #line 801 "tie.w"
214 static text_file out_file;
215 static string out_name;
218 /*:35*/
219 #line 116 "tie.w"
221 /*31:*/
222 #line 744 "tie.w"
224 void err_loc(i)
225 int i;
227 print3_ln(" (file %s, l.%ld).",
228 input_organization[i]->name_of_file,
229 input_organization[i]->line);
233 /*:31*/
234 #line 117 "tie.w"
236 /*24:*/
237 #line 616 "tie.w"
239 void get_line(i)
240 file_index i;
241 {register input_description*inp_desc= input_organization[i];
242 if(inp_desc->mode==ignore)return;
243 if(feof(inp_desc->the_file))
244 /*25:*/
245 #line 630 "tie.w"
248 inp_desc->mode= ignore;
249 inp_desc->limit= -1;
250 if(inp_desc->type_of_file==master)input_has_ended= true;
251 return;
255 /*:25*/
256 #line 622 "tie.w"
258 /*27:*/
259 #line 655 "tie.w"
261 {int final_limit;
262 int c;
263 /*28:*/
264 #line 679 "tie.w"
266 incr(inp_desc->line);
267 if(inp_desc->type_of_file==master&&inp_desc->line%100==0){
268 if(inp_desc->line%500==0)print2("%ld",inp_desc->line);
269 else print_c('.');
270 update_terminal;
274 /*:28*/
275 #line 659 "tie.w"
277 inp_desc->limit= final_limit= 0;
278 while(inp_desc->limit<buf_size){
279 c= fgetc(inp_desc->the_file);
280 /*29:*/
281 #line 696 "tie.w"
283 if(c==EOF){
284 if(inp_desc->limit<=0){
285 inp_desc->mode= ignore;
286 inp_desc->limit= -1;
287 if(inp_desc->type_of_file==master)input_has_ended= true;
288 return;
289 }else{
290 c= nl_mark;
291 break;
296 /*:29*/
297 #line 664 "tie.w"
299 inp_desc->buffer[inp_desc->limit++]= c= map_xord(c);
300 if(c==nl_mark)break;
301 if(c!=32&&c!=tab_mark)
302 final_limit= inp_desc->limit;
304 /*30:*/
305 #line 712 "tie.w"
307 if(c!=nl_mark){
308 err_print("! Input line too long")(i);
310 while((c= fgetc(inp_desc->the_file))!=EOF&&map_xord(c)!=nl_mark)
311 do_nothing;
318 /*:30*/
319 #line 670 "tie.w"
321 inp_desc->limit= final_limit;
325 /*:27*/
326 #line 623 "tie.w"
331 /*:24*//*38:*/
332 #line 850 "tie.w"
334 boolean lines_dont_match(i,j)
335 file_index i,j;
337 buffer_index k,lmt;
338 if(input_organization[i]->limit!=input_organization[j]->limit)
339 return(true);
340 lmt= input_organization[i]->limit;
341 for(k= 0;k<lmt;k++)
342 if(input_organization[i]->buffer[k]!=input_organization[j]->buffer[k])
343 return(true);
344 return(false);
348 /*:38*//*39:*/
349 #line 871 "tie.w"
351 void init_change_file(i,b)
352 file_index i;boolean b;
353 {register input_description*inp_desc= input_organization[i];
354 /*40:*/
355 #line 885 "tie.w"
357 loop{ASCII_Code c;
358 get_line(i);
359 if(inp_desc->mode==ignore)return;
360 if(inp_desc->limit<2)continue;
361 if(inp_desc->buffer[0]!=64)continue;
362 c= inp_desc->buffer[1];
363 if(c>=88&&c<=90)
364 c+= 122-90;
365 if(c==120)break;
366 if(c==121||c==122)
367 if(b)
368 err_print("! Where is the matching @x?")(i);
373 /*:40*/
374 #line 875 "tie.w"
376 /*41:*/
377 #line 904 "tie.w"
380 get_line(i);
381 if(inp_desc->mode==ignore){
382 err_print("! Change file ended after @x")(i);
384 return;
386 }while(inp_desc->limit<=0);
389 /*:41*/
390 #line 876 "tie.w"
395 /*:39*//*42:*/
396 #line 918 "tie.w"
398 void put_line(j)
399 file_index j;
400 {buffer_index i;
401 buffer_index lmt;
402 ASCII_Code*p;
403 lmt= input_organization[j]->limit;
404 p= input_organization[j]->buffer;
405 for(i= 0;i<lmt;i++)fputc(map_xchr(*p++),out_file);
406 new_line(out_file);
410 /*:42*//*43:*/
411 #line 934 "tie.w"
413 boolean e_of_ch_module(i)
414 file_index i;
415 {register input_description*inp_desc= input_organization[i];
416 if(inp_desc->limit<0){
417 print_nl("! At the end of change file missing @z ");
419 print2("%s",input_organization[i]->name_of_file);
420 term_new_line;
421 return(true);
422 }else if(inp_desc->limit>=2)if(inp_desc->buffer[0]==64&&
423 (inp_desc->buffer[1]==90||inp_desc->buffer[1]==122))
424 return(true);
425 return(false);
429 /*:43*//*44:*/
430 #line 954 "tie.w"
432 boolean e_of_ch_preamble(i)
433 file_index i;
434 {register input_description*inp_desc= input_organization[i];
435 if(inp_desc->limit>=2&&inp_desc->buffer[0]==64)
436 if(inp_desc->buffer[1]==89||inp_desc->buffer[1]==121)return(true);
437 return(false);
442 /*:44*//*55:*/
443 #line 1157 "tie.w"
445 void usage()
447 print("Usage: tie -[mc] outfile master changefile(s)");
448 term_new_line;
449 jump_out();
453 /*:55*/
454 #line 118 "tie.w"
456 /*59:*/
457 #line 1235 "tie.w"
459 main(argc,argv)
460 int argc;string*argv;
461 {{/*12:*/
462 #line 408 "tie.w"
464 int i;
467 /*:12*/
468 #line 1238 "tie.w"
470 /*10:*/
471 #line 296 "tie.w"
473 xchr[32]= ' ';
474 xchr[33]= '!';
475 xchr[34]= '\"';
476 xchr[35]= '#';
477 xchr[36]= '$';
478 xchr[37]= '%';
479 xchr[38]= '&';
480 xchr[39]= '\'';
481 xchr[40]= '(';
482 xchr[41]= ')';
483 xchr[42]= '*';
484 xchr[43]= '+';
485 xchr[44]= ',';
486 xchr[45]= '-';
487 xchr[46]= '.';
488 xchr[47]= '/';
489 xchr[48]= '0';
490 xchr[49]= '1';
491 xchr[50]= '2';
492 xchr[51]= '3';
493 xchr[52]= '4';
494 xchr[53]= '5';
495 xchr[54]= '6';
496 xchr[55]= '7';
497 xchr[56]= '8';
498 xchr[57]= '9';
499 xchr[58]= ':';
500 xchr[59]= ';';
501 xchr[60]= '<';
502 xchr[61]= '=';
503 xchr[62]= '>';
504 xchr[63]= '?';
505 xchr[64]= '@';
506 xchr[65]= 'A';
507 xchr[66]= 'B';
508 xchr[67]= 'C';
509 xchr[68]= 'D';
510 xchr[69]= 'E';
511 xchr[70]= 'F';
512 xchr[71]= 'G';
513 xchr[72]= 'H';
514 xchr[73]= 'I';
515 xchr[74]= 'J';
516 xchr[75]= 'K';
517 xchr[76]= 'L';
518 xchr[77]= 'M';
519 xchr[78]= 'N';
520 xchr[79]= 'O';
521 xchr[80]= 'P';
522 xchr[81]= 'Q';
523 xchr[82]= 'R';
524 xchr[83]= 'S';
525 xchr[84]= 'T';
526 xchr[85]= 'U';
527 xchr[86]= 'V';
528 xchr[87]= 'W';
529 xchr[88]= 'X';
530 xchr[89]= 'Y';
531 xchr[90]= 'Z';
532 xchr[91]= '[';
533 xchr[92]= '\\';
534 xchr[93]= ']';
535 xchr[94]= '^';
536 xchr[95]= '_';
537 xchr[96]= '`';
538 xchr[97]= 'a';
539 xchr[98]= 'b';
540 xchr[99]= 'c';
541 xchr[100]= 'd';
542 xchr[101]= 'e';
543 xchr[102]= 'f';
544 xchr[103]= 'g';
545 xchr[104]= 'h';
546 xchr[105]= 'i';
547 xchr[106]= 'j';
548 xchr[107]= 'k';
549 xchr[108]= 'l';
550 xchr[109]= 'm';
551 xchr[110]= 'n';
552 xchr[111]= 'o';
553 xchr[112]= 'p';
554 xchr[113]= 'q';
555 xchr[114]= 'r';
556 xchr[115]= 's';
557 xchr[116]= 't';
558 xchr[117]= 'u';
559 xchr[118]= 'v';
560 xchr[119]= 'w';
561 xchr[120]= 'x';
562 xchr[121]= 'y';
563 xchr[122]= 'z';
564 xchr[123]= '{';
565 xchr[124]= '|';
566 xchr[125]= '}';
567 xchr[126]= '~';
568 xchr[0]= ' ';xchr[0x7F]= ' ';
571 /*:10*//*13:*/
572 #line 429 "tie.w"
574 for(i= 1;i<32;xchr[i++]= ' ');
575 xchr[tab_mark]= '\t';
576 xchr[form_feed]= '\f';
577 xchr[nl_mark]= '\n';
580 /*:13*//*14:*/
581 #line 440 "tie.w"
583 for(i= first_text_char;i<=last_text_char;xord[i++]= 32)do_nothing;
584 for(i= 1;i<=126;i++)xord[xchr[i]]= i;
590 /*:14*/
591 #line 1239 "tie.w"
594 print_ln(banner);
595 print_ln(copyright);
596 actual_input= 0;
597 out_mode= normal;
598 /*56:*/
599 #line 1177 "tie.w"
601 {int act_arg;
602 if(argc<5||argc>max_file_index+4-1)usage();
603 no_ch= -1;
604 for(act_arg= 1;act_arg<argc;act_arg++){
605 if(argv[act_arg][0]=='-')/*57:*/
606 #line 1195 "tie.w"
608 if(prod_chf!=unknown)usage();
609 else
610 switch(argv[act_arg][1]){
611 case'c':
612 case'C':prod_chf= chf;break;
613 case'm':
614 case'M':prod_chf= master;break;
615 default:usage();
619 /*:57*/
620 #line 1182 "tie.w"
622 else/*58:*/
623 #line 1211 "tie.w"
625 {if(no_ch==(-1)){
626 out_name= argv[act_arg];
627 }else{register input_description*inp_desc;
628 inp_desc= (input_description*)
629 malloc(sizeof(input_description));
630 if(inp_desc==NULL)
631 fatal_error("! No memory for descriptor");
633 inp_desc->mode= search;
634 inp_desc->line= 0;
635 inp_desc->type_of_file= chf;
636 inp_desc->limit= 0;
637 inp_desc->name_of_file= argv[act_arg];
638 input_organization[no_ch]= inp_desc;
640 incr(no_ch);
644 /*:58*/
645 #line 1183 "tie.w"
648 if(no_ch<=0||prod_chf==unknown)usage();
652 /*:56*/
653 #line 1245 "tie.w"
655 /*34:*/
656 #line 788 "tie.w"
659 out_file= fopen(out_name,"w");
660 if(out_file==NULL){
661 fatal_error("! Could not open/create output file");
667 /*:34*/
668 #line 1246 "tie.w"
670 /*36:*/
671 #line 809 "tie.w"
673 {input_organization[0]->the_file=
674 fopen(input_organization[0]->name_of_file,"r");
675 if(input_organization[0]->the_file==NULL)
676 fatal_error("! Could not open master file");
678 print2("(%s)",input_organization[0]->name_of_file);
679 term_new_line;
680 input_organization[0]->type_of_file= master;
681 get_line(0);
684 /*:36*/
685 #line 1247 "tie.w"
687 /*37:*/
688 #line 825 "tie.w"
690 {file_index i;
691 i= 1;
692 while(i<no_ch){
693 input_organization[i]->the_file=
694 fopen(input_organization[i]->name_of_file,"r");
695 if(input_organization[i]->the_file==NULL)
696 fatal_error("!Could not open change file");
698 print2("(%s)",input_organization[i]->name_of_file);
699 term_new_line;
700 init_change_file(i,true);
701 incr(i);
709 /*:37*/
710 #line 1248 "tie.w"
712 /*53:*/
713 #line 1128 "tie.w"
715 actual_input= 0;
716 input_has_ended= false;
717 while(input_has_ended==false||actual_input!=0)
718 /*45:*/
719 #line 970 "tie.w"
721 {file_index test_file;
722 /*46:*/
723 #line 985 "tie.w"
725 {register input_description*inp_desc;
726 while(actual_input>0&&e_of_ch_module(actual_input)){
727 inp_desc= input_organization[actual_input];
728 if(inp_desc->type_of_file==master){
730 fatal_error("! This can't happen: change file is master file");
733 inp_desc->mode= search;
734 init_change_file(actual_input,true);
735 while((input_organization[actual_input]->mode!=reading
736 &&actual_input>0))decr(actual_input);
741 /*:46*/
742 #line 972 "tie.w"
744 if(input_has_ended&&actual_input==0)break;
745 /*47:*/
746 #line 1009 "tie.w"
748 test_input= none;
749 test_file= actual_input;
750 while(test_input==none&&test_file<no_ch-1){
751 incr(test_file);
752 switch(input_organization[test_file]->mode){
753 case search:if(lines_dont_match(actual_input,test_file)==false){
754 input_organization[test_file]->mode= test;
755 test_input= test_file;
757 break;
758 case test:if(lines_dont_match(actual_input,test_file)==true){
760 input_organization[test_file]->mode= search;
761 err_print("! Sections do not match")(actual_input);
763 err_loc(test_file);
764 init_change_file(test_file,false);
765 }else test_input= test_file;
766 break;
767 case reading:do_nothing;
768 break;
769 case ignore:do_nothing;
770 break;
775 /*:47*/
776 #line 974 "tie.w"
778 /*48:*/
779 #line 1043 "tie.w"
781 if(prod_chf==chf){
782 loop{
783 /*49:*/
784 #line 1057 "tie.w"
786 if(out_mode==normal){
787 if(test_input!=none){
788 fputc(map_xchr(64),out_file);fputc(map_xchr(120),out_file);
789 new_line(out_file);
790 out_mode= pre;
791 }else break;
795 /*:49*/
796 #line 1046 "tie.w"
798 /*50:*/
799 #line 1071 "tie.w"
802 if(out_mode==pre){
803 if(test_input==none){
804 fputc(map_xchr(64),out_file);fputc(map_xchr(121),out_file);
805 new_line(out_file);
806 out_mode= post;
807 }else{
808 if(input_organization[actual_input]->type_of_file==master)
809 put_line(actual_input);
810 break;
815 /*:50*/
816 #line 1047 "tie.w"
818 /*51:*/
819 #line 1092 "tie.w"
821 if(out_mode==post){
822 if(input_organization[actual_input]->type_of_file==chf){
823 if(test_input==none)put_line(actual_input);
824 break;
825 }else{
826 fputc(map_xchr(64),out_file);fputc(map_xchr(122),out_file);
827 new_line(out_file);
828 new_line(out_file);
829 out_mode= normal;
834 /*:51*/
835 #line 1048 "tie.w"
838 }else
839 if(test_input==none)put_line(actual_input);
842 /*:48*/
843 #line 975 "tie.w"
845 /*52:*/
846 #line 1109 "tie.w"
848 get_line(actual_input);
849 if(test_input!=none){
850 get_line(test_input);
851 if(e_of_ch_preamble(test_input)==true){
852 get_line(test_input);
853 input_organization[test_input]->mode= reading;
854 actual_input= test_input;
855 test_input= none;
860 /*:52*/
861 #line 976 "tie.w"
866 /*:45*/
867 #line 1132 "tie.w"
869 if(out_mode==post){
870 fputc(map_xchr(64),out_file);fputc(map_xchr(122),out_file);
871 new_line(out_file);
875 /*:53*/
876 #line 1249 "tie.w"
878 /*54:*/
879 #line 1143 "tie.w"
881 {file_index i;
882 for(i= 1;i<no_ch;i++){
883 if(input_organization[i]->mode!=ignore)
884 err_print("! Change file entry did not match")(i);
890 /*:54*/
891 #line 1250 "tie.w"
893 /*60:*/
894 #line 1261 "tie.w"
896 {string msg;
897 switch(history){
898 case spotless:msg= "No errors were found";break;
899 case troublesome:msg= "Pardon me, but I think I spotted something wrong.";
900 break;
901 case fatal:msg= "That was a fatal error, my friend";break;
903 print2_nl("(%s.)",msg);term_new_line;
904 exit(history==spotless?0:1);
911 /*:60*/
912 #line 1251 "tie.w"
916 /*:59*/
917 #line 119 "tie.w"
920 /*:2*/