1 #define version_number "1.1"
2 #define banner "This is CTIE, Version 1.1"
4 "Copyright 2002,2003 Julian Gilbey. All rights reserved. There is no warranty.\n\
5 Run with the --version option for other important information." \
8 #define kpse_find_cweb(name) kpse_find_file(name,kpse_cweb_format,true) \
11 #define max_file_index 32 \
13 #define xisupper(c) (isupper(c) &&((unsigned char) c<0200) ) \
20 #define max_include_files 20 \
22 #define max_file_name_length 1024 \
24 #define too_long() {total_include_files--;free(new_inc) ; \
25 err_print(i,"! Include file name too long") ;goto restart;} \
27 #define fatal_error(i,s,t) { \
28 fprintf(stderr,"\n%s",s) ; \
46 #line 126 "./ctie-k.ch"
54 #line 454 "./ctie-k.ch"
56 #include <kpathsea/kpathsea.h>
68 typedef int in_file_modes;
72 typedef int file_types;
81 typedef int out_md_type;
87 typedef int file_index;
93 typedef struct _indsc{
94 char file_name[max_file_name_length];
104 typedef struct _idsc{
106 char buffer[buf_size];
109 file_types type_of_file;
110 include_description*current_include;
125 #line 223 "./ctie-k.ch"
126 void err_print P2H(file_index,char*);
132 #line 243 "./ctie-k.ch"
133 int wrap_up P1H(void);
139 #line 249 "./ctie-k.ch"
140 void pfatal_error P2H(char*,char*);
144 #line 1232 "./ctie.w"
146 #line 392 "./ctie-k.ch"
147 void usage_help P1H(void);
148 void print_version_and_exit P2H(string,string);
149 #line 1235 "./ctie.w"
158 int history= spotless;
164 int total_include_files= 0;
169 file_index actual_input,test_input,no_ch;
170 file_types prod_chf= unknown;
171 out_md_type out_mode;
176 input_description*input_organisation[max_file_index+1];
182 boolean input_has_ended= false;
192 #line 273 "./ctie-k.ch"
194 #line 1217 "./ctie.w"
197 "Usage: ctie -[mc] outfile master changefile(s)",
198 " Create a new master file or change file from the given",
199 " master (C)WEB file and changefiles.",
200 " All filenames are taken literally; no suffixes are added.",
202 "-m create a new master file from original (C)WEB and change file(s)",
203 "-c create a master change file for original (C)WEB file from changefile(s)",
204 "--help display this help and exit",
205 "--version display version information and exit",
211 #line 459 "./ctie-k.ch"
213 string file_open_mode;
214 #line 1278 "./ctie.w"
223 #line 230 "./ctie-k.ch"
224 void err_print P2C(file_index,i,char*,s)
229 fprintf(stderr,*s=='!'?"\n%s":"%s",s);
234 register input_description*inp_desc= input_organisation[i];
235 register include_description*inc_desc= inp_desc->current_include;
238 fprintf(stderr,". (l. %ld of include file %s",inc_desc->line,
239 inc_desc->file_name);
240 fprintf(stderr," included from l. %ld of %s file %s)\n",
242 inp_desc->type_of_file==master?"master":"change",
243 inp_desc->file_name);
246 fprintf(stderr,". (l. %ld of %s file %s)\n",inp_desc->line,
247 inp_desc->type_of_file==master?"master":"change",
248 inp_desc->file_name);
249 l= (inp_desc->loc>=inp_desc->limit?inp_desc->limit:inp_desc->loc);
250 if(l> inp_desc->buffer){
251 for(k= inp_desc->buffer;k<l;k++)
252 if(*k=='\t')putc(' ',stderr);
253 else putc(*k,stderr);
255 for(k= inp_desc->buffer;k<l;k++)
258 for(k= l;k<inp_desc->limit;k++)
267 else putc('\n',stderr);
269 history= troublesome;
275 #line 256 "./ctie-k.ch"
276 void pfatal_error P2C(char*,s,char*,t)
279 char*strerr= strerror(errno);
281 fprintf(stderr,"\n%s%s",s,t);
282 if(strerr)fprintf(stderr," (%s)\n",strerr);
283 else fprintf(stderr,"\n");
295 #line 143 "./ctie-k.ch"
296 boolean get_line P2C(file_index,i,boolean,do_includes)
299 register input_description*inp_desc= input_organisation[i];
302 if(inp_desc->mode==ignore)return false;
305 if(inp_desc->current_include!=NULL){
306 register include_description*inc_desc= inp_desc->current_include;
308 fp= inc_desc->the_file;
321 include_description*temp= inc_desc->parent;
325 total_include_files--;
326 inp_desc->current_include= temp;
335 inp_desc->limit= k= inp_desc->buffer;
336 while(k<=inp_desc->buffer_end&&(c= getc(fp))!=EOF&&c!='\n')
337 if((*(k++)= c)!=' ')inp_desc->limit= k;
338 if(k> inp_desc->buffer_end)
339 if((c= getc(fp))!=EOF&&c!='\n'){
340 ungetc(c,fp);inp_desc->loc= inp_desc->buffer;
341 err_print(i,"! Input line too long");
344 if(c==EOF&&inp_desc->limit==inp_desc->buffer)
349 include_description*temp= inc_desc->parent;
353 total_include_files--;
354 inp_desc->current_include= temp;
371 fp= inp_desc->the_file;
384 inp_desc->mode= ignore;
385 inp_desc->limit= NULL;
386 if(inp_desc->type_of_file==master)input_has_ended= true;
396 inp_desc->limit= k= inp_desc->buffer;
397 while(k<=inp_desc->buffer_end&&(c= getc(fp))!=EOF&&c!='\n')
398 if((*(k++)= c)!=' ')inp_desc->limit= k;
399 if(k> inp_desc->buffer_end)
400 if((c= getc(fp))!=EOF&&c!='\n'){
401 ungetc(c,fp);inp_desc->loc= inp_desc->buffer;
402 err_print(i,"! Input line too long");
405 if(c==EOF&&inp_desc->limit==inp_desc->buffer)
410 inp_desc->mode= ignore;
411 inp_desc->limit= NULL;
412 if(inp_desc->type_of_file==master)input_has_ended= true;
426 if(inp_desc->type_of_file==master&&inp_desc->line%100==0){
427 if(inp_desc->line%500==0)printf("%ld",inp_desc->line);
449 inp_desc->loc= inp_desc->buffer;
450 *inp_desc->limit= ' ';
451 if(*inp_desc->buffer=='@'&&
452 (inp_desc->buffer[1]=='i'||inp_desc->buffer[1]=='I')){
453 inp_desc->loc= inp_desc->buffer+2;
454 *inp_desc->limit= '"';
455 while(*inp_desc->loc==' '||*inp_desc->loc=='\t')
457 if(inp_desc->loc>=inp_desc->limit){
458 err_print(i,"! Include file name not given");
462 if(total_include_files>=max_include_files){
463 err_print(i,"! Too many nested includes");
467 total_include_files++;
472 include_description*new_inc;
473 #line 166 "./ctie-k.ch"
479 new_inc= (include_description*)malloc(sizeof(include_description));
481 fatal_error(i,"! No memory for new include descriptor","");
483 k= new_inc->file_name;
484 file_name_end= k+max_file_name_length-1;
486 if(*inp_desc->loc=='"'){
488 while(*inp_desc->loc!='"'&&k<=file_name_end)
489 *k++= *inp_desc->loc++;
490 if(inp_desc->loc==inp_desc->limit)
493 while(*inp_desc->loc!=' '&&*inp_desc->loc!='\t'&&
494 *inp_desc->loc!='"'&&k<=file_name_end)*k++= *inp_desc->loc++;
495 if(k> file_name_end)too_long();
498 #line 174 "./ctie-k.ch"
499 fullname= kpse_find_cweb(new_inc->file_name);
501 new_inc->the_file= fopen(fullname,"r");
502 if(fullname!=NULL&&new_inc->the_file!=NULL){
505 new_inc->parent= inp_desc->current_include;
506 inp_desc->current_include= new_inc;
510 total_include_files--;
512 #line 212 "./ctie-k.ch"
515 err_print(i,"! Cannot open include file");
517 err_print(i,"! Cannot find include file");
542 #line 237 "./ctie-k.ch"
543 int wrap_up P1H(void)
551 printf("\n(No errors were found.)\n");break;
553 printf("\n(Pardon me, but I think I spotted something wrong.)\n");break;
554 case fatal:printf("(That was a fatal error, my friend.)\n");
561 if(history> spotless)return 1;
568 #line 342 "./ctie-k.ch"
569 boolean lines_dont_match P2C(file_index,i,file_index,j)
572 register input_description*iptr= input_organisation[i],
573 *jptr= input_organisation[j];
575 if(iptr->limit-iptr->buffer!=jptr->limit-jptr->buffer)
577 return strncmp(iptr->buffer,jptr->buffer,iptr->limit-iptr->buffer);
584 #line 349 "./ctie-k.ch"
585 void init_change_file P1C(file_index,i)
588 register input_description*inp_desc= input_organisation[i];
591 inp_desc->limit= inp_desc->buffer;
596 if(!get_line(i,false))return;
597 if(inp_desc->limit<inp_desc->buffer+2)continue;
598 if(inp_desc->buffer[0]!='@')continue;
599 ccode= inp_desc->buffer[1];
600 if(xisupper(ccode))ccode= tolower(ccode);
602 if(ccode=='y'||ccode=='z'||ccode=='i'){
603 inp_desc->loc= inp_desc->buffer+2;
604 err_print(i,"! Missing @x in change file");
616 if(!get_line(i,true)){
617 err_print(i,"! Change file ended after @x");
621 }while(inp_desc->limit==inp_desc->buffer);
627 inp_desc->dont_match= 0;
634 #line 356 "./ctie-k.ch"
635 void put_line P1C(file_index,j)
638 char*ptr= input_organisation[j]->buffer;
639 char*lmt= input_organisation[j]->limit;
641 while(ptr<lmt)putc(*ptr++,out_file);
649 #line 363 "./ctie-k.ch"
650 boolean e_of_ch_module P1C(file_index,i)
653 register input_description*inp_desc= input_organisation[i];
655 if(inp_desc->limit==NULL){
656 err_print(i,"! Change file ended without @z");
659 }else if(inp_desc->limit>=inp_desc->buffer+2)
660 if(inp_desc->buffer[0]=='@'&&
661 (inp_desc->buffer[1]=='Z'||inp_desc->buffer[1]=='z'))
670 #line 370 "./ctie-k.ch"
671 boolean e_of_ch_preamble P1C(file_index,i)
674 register input_description*inp_desc= input_organisation[i];
676 if(inp_desc->limit>=inp_desc->buffer+2&&inp_desc->buffer[0]=='@')
677 if(inp_desc->buffer[1]=='Y'||inp_desc->buffer[1]=='y'){
678 if(inp_desc->dont_match> 0){
679 inp_desc->loc= inp_desc->buffer+2;
680 fprintf(stderr,"\n! Hmm... %d ",inp_desc->dont_match);
681 err_print(i,"of the preceding lines failed to match");
691 #line 1105 "./ctie.w"
693 #line 376 "./ctie-k.ch"
694 void usage_error P1H(void)
695 #line 1107 "./ctie.w"
698 #line 1118 "./ctie.w"
700 #line 382 "./ctie-k.ch"
702 extern KPSEDLL string kpathsea_version_string;
703 printf("%s (%s)\n",banner,kpathsea_version_string);
705 #line 1120 "./ctie.w"
706 printf("%s\n",copyright);
710 #line 1108 "./ctie.w"
712 fprintf(stderr,"Usage: ctie -[mc] outfile master changefile(s)\n");
713 fprintf(stderr,"Type ctie --help for more information\n");
724 #line 32 "./ctie-k.ch"
725 int main P2C(int,argc,string*,argv)
728 #line 38 "./ctie-k.ch"
730 #line 84 "./ctie-k.ch"
732 kpse_set_program_name(argv[0],"ctie");
735 #line 38 "./ctie-k.ch"
744 #line 39 "./ctie-k.ch"
748 #line 1135 "./ctie.w"
751 if(argc> max_file_index+5-1)usage_error();
755 if(strcmp("-help",*argv)==0||strcmp("--help",*argv)==0)
757 #line 1202 "./ctie.w"
764 #line 1142 "./ctie.w"
766 if(strcmp("-version",*argv)==0||strcmp("--version",*argv)==0)
768 #line 1208 "./ctie.w"
771 print_version_and_exit("CTIE",version_number);
777 #line 1144 "./ctie.w"
779 if(**argv=='-')/*64:*/
780 #line 1158 "./ctie.w"
782 if(prod_chf!=unknown)usage_error();
785 case'c':case'C':prod_chf= chf;break;
786 case'm':case'M':prod_chf= master;break;
787 default:usage_error();
792 #line 1145 "./ctie.w"
795 #line 1172 "./ctie.w"
801 register input_description*inp_desc;
803 inp_desc= (input_description*)malloc(sizeof(input_description));
805 fatal_error(-1,"! No memory for input descriptor","");
807 inp_desc->mode= search;
809 inp_desc->type_of_file= chf;
810 inp_desc->limit= inp_desc->buffer;
811 inp_desc->buffer[0]= ' ';
812 inp_desc->loc= inp_desc->buffer+1;
813 inp_desc->buffer_end= inp_desc->buffer+buf_size-2;
814 inp_desc->file_name= *argv;
815 inp_desc->current_include= NULL;
816 input_organisation[no_ch]= inp_desc;
823 #line 1146 "./ctie.w"
826 if(no_ch<=0||prod_chf==unknown)usage_error();
834 #line 1118 "./ctie.w"
836 #line 382 "./ctie-k.ch"
838 extern KPSEDLL string kpathsea_version_string;
839 printf("%s (%s)\n",banner,kpathsea_version_string);
841 #line 1120 "./ctie.w"
842 printf("%s\n",copyright);
849 #line 277 "./ctie-k.ch"
854 fullname= kpse_find_cweb(input_organisation[0]->file_name);
856 input_organisation[0]->the_file= fopen(fullname,"r");
858 if(fullname==NULL||input_organisation[0]->the_file==NULL){
860 pfatal_error("! Cannot open master file ",
861 input_organisation[0]->file_name);
863 fatal_error(-1,"! Cannot find master file ",
864 input_organisation[0]->file_name);
871 printf("(%s)\n",input_organisation[0]->file_name);
872 input_organisation[0]->type_of_file= master;
881 #line 313 "./ctie-k.ch"
889 fullname= kpse_find_cweb(input_organisation[i]->file_name);
891 input_organisation[i]->the_file= fopen(fullname,"r");
893 if(fullname==NULL||input_organisation[i]->the_file==NULL){
895 pfatal_error("! Cannot open change file ",
896 input_organisation[i]->file_name);
898 fatal_error(-1,"! Cannot find change file ",
899 input_organisation[i]->file_name);
906 printf("(%s)\n",input_organisation[i]->file_name);
920 out_file= fopen(out_name,"w");
922 pfatal_error("! Cannot open/create output file","");
932 #line 1074 "./ctie.w"
935 input_has_ended= false;
936 while(input_has_ended==false||actual_input!=0)
941 file_index test_file;
947 register input_description*inp_desc;
948 while(actual_input> 0&&e_of_ch_module(actual_input)){
949 inp_desc= input_organisation[actual_input];
950 if(inp_desc->type_of_file==master){
952 fatal_error(-1,"! This can't happen: change file is master file","");
955 inp_desc->mode= search;
956 init_change_file(actual_input);
957 while((input_organisation[actual_input]->mode!=reading
967 if(input_has_ended&&actual_input==0)break;
972 test_file= actual_input;
973 while(test_input==none&&test_file<no_ch-1){
975 switch(input_organisation[test_file]->mode){
977 if(lines_dont_match(actual_input,test_file)==false){
978 input_organisation[test_file]->mode= test;
979 test_input= test_file;
983 if(lines_dont_match(actual_input,test_file)){
985 input_organisation[test_file]->dont_match++;
987 test_input= test_file;
1001 #line 993 "./ctie.w"
1006 #line 1007 "./ctie.w"
1008 if(out_mode==normal){
1009 if(test_input!=none){
1010 fprintf(out_file,"@x\n");
1017 #line 996 "./ctie.w"
1020 #line 1021 "./ctie.w"
1023 if(test_input==none){
1024 fprintf(out_file,"@y\n");
1027 if(input_organisation[actual_input]->type_of_file==master)
1028 put_line(actual_input);
1035 #line 997 "./ctie.w"
1038 #line 1040 "./ctie.w"
1041 if(input_organisation[actual_input]->type_of_file==chf){
1042 if(test_input==none)put_line(actual_input);
1045 fprintf(out_file,"@z\n\n");
1052 #line 998 "./ctie.w"
1056 if(test_input==none)put_line(actual_input);
1060 #line 924 "./ctie.w"
1063 #line 1055 "./ctie.w"
1065 get_line(actual_input,true);
1066 if(test_input!=none){
1067 get_line(test_input,true);
1068 if(e_of_ch_preamble(test_input)==true){
1069 get_line(test_input,true);
1070 input_organisation[test_input]->mode= reading;
1071 actual_input= test_input;
1078 #line 925 "./ctie.w"
1084 #line 1078 "./ctie.w"
1087 fprintf(out_file,"@z\n");
1091 #line 111 "./ctie.w"
1094 #line 1087 "./ctie.w"
1099 for(i= 1;i<no_ch;i++){
1100 if(input_organisation[i]->mode!=ignore){
1101 input_organisation[i]->loc= input_organisation[i]->buffer;
1102 err_print(i,"! Change file entry did not match");
1110 #line 112 "./ctie.w"
1115 #line 57 "./ctie-k.ch"
1121 #line 1237 "./ctie.w"
1123 #line 399 "./ctie-k.ch"
1124 void usage_help P1H(void)
1125 #line 1239 "./ctie.w"
1127 string*message= CTIEHELP;
1130 fputs(*message,stdout);
1140 #line 1252 "./ctie.w"
1142 #line 416 "./ctie-k.ch"
1143 void print_version_and_exit P2C(string,name,string,version)
1145 extern KPSEDLL string kpathsea_version_string;
1146 printf("%s %s\n",name,version);
1147 puts(kpathsea_version_string);
1149 puts("Copyright (C) 2002,2003 Julian Gilbey.");
1150 puts("Kpathsea is copyright (C) 1999 Free Software Foundation, Inc.");
1152 puts("There is NO warranty. This is free software.");
1153 puts("Redistribution of this software is covered by the terms of");
1154 puts("both the CTIE copyright and the GNU General Public Licence.");
1155 puts("For more information about these matters, see the files");
1156 puts("named COPYING and the CTIE source.");
1157 puts("Primary authors of CTIE: Julian Gilbey.");
1158 puts("Kpathsea written by Karl Berry and others.\n");
1162 #line 1265 "./ctie.w"
1165 #line 450 "./ctie-k.ch"