home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 2
/
FFMCD02.bin
/
new
/
dev
/
misc
/
cweb
/
examples
/
wmerge.c.save
< prev
next >
Wrap
Text File
|
1993-12-21
|
14KB
|
738 lines
#define buf_size 100 \
#define max_include_depth 10 \
\
#define max_file_name_length 256 \
#define cur_file file[include_depth]
#define cur_file_name file_name[include_depth]
#define cur_line line[include_depth]
#define web_file file[0]
#define web_file_name file_name[0] \
#define lines_dont_match (change_limit-change_buffer!=limit-buffer|| \
strncmp(buffer,change_buffer,limit-buffer) ) \
#define PATH_SEPARATOR ','
#define DIR_SEPARATOR '/'
#define DEVICE_SEPARATOR ':' \
#define too_long() {include_depth--; \
err_print("! Include file name too long") ;goto restart;} \
#define spotless 0
#define harmless_message 1
#define error_message 2
#define fatal_message 3
#define mark_harmless {if(history==spotless) history= harmless_message;}
#define mark_error history= error_message \
#define fatal(s,t) { \
fprintf(stderr,s) ;err_print(t) ; \
history= fatal_message;exit(wrap_up() ) ; \
} \
\
#define RETURN_OK 0
#define RETURN_WARN 5
#define RETURN_ERROR 10
#define RETURN_FAIL 20 \
#define show_banner flags['b']
#define show_happiness flags['h'] \
#define update_terminal fflush(stderr) \
\
#define MAXPATHLENGTH 4095
#define CWEBINPUTS ",CWeb:,CWeb:include,CWeb:inputs" \
/*1:*/
#line 13 "wmerge.w"
#line 63 "wmerge-p.ch"
#include <stdio.h>
#include <string.h>
#line 15 "wmerge.w"
#include <stdlib.h>
#include <ctype.h>
/*2:*/
#line 35 "wmerge.w"
typedef short boolean;
typedef unsigned char eight_bits;
typedef char ASCII;
/*:2*//*5:*/
#line 68 "wmerge.w"
ASCII buffer[buf_size];
ASCII*buffer_end= buffer+buf_size-2;
ASCII*limit;
ASCII*loc;
/*:5*//*7:*/
#line 134 "wmerge.w"
int include_depth;
FILE*file[max_include_depth];
FILE*change_file;
char file_name[max_include_depth][max_file_name_length];
char change_file_name[max_file_name_length];
char alt_web_file_name[max_file_name_length];
int line[max_include_depth];
int change_line;
int change_depth;
boolean input_has_ended;
boolean changing;
boolean web_file_open= 0;
/*:7*//*8:*/
#line 160 "wmerge.w"
char change_buffer[buf_size];
char*change_limit;
/*:8*//*23:*/
#line 478 "wmerge.w"
int history= spotless;
/*:23*//*30:*/
#line 575 "wmerge.w"
int argc;
char**argv;
char out_file_name[max_file_name_length];
#line 299 "wmerge-p.ch"
boolean flags[256];
#line 580 "wmerge.w"
/*:30*//*40:*/
#line 692 "wmerge.w"
FILE*out_file;
/*:40*//*44:*/
#line 372 "wmerge-p.ch"
char include_path[MAXPATHLENGTH+1]= CWEBINPUTS;
char*p,*path_prefix,*next_path_prefix;
/*:44*/
#line 17 "wmerge.w"
/*4:*/
#line 51 "wmerge.w"
#line 52 "wmerge.w"
/*:4*//*24:*/
#line 239 "wmerge-p.ch"
#line 240 "wmerge-p.ch"
void err_print(char*);
/*:24*//*32:*/
#line 320 "wmerge-p.ch"
#line 321 "wmerge-p.ch"
void scan_args(void);
/*:32*//*45:*/
#line 379 "wmerge-p.ch"
int input_ln(FILE*fp);
void prime_the_change_buffer(void);
void check_change(void);
void reset_input(void);
int get_line(void);
void put_line(void);
void check_complete(void);
void err_print(char*s);
int wrap_up(void);
void scan_args(void);
static boolean set_path(char*ptr,char*override);
int main(int argc,char**argv);
/*:45*/
#line 18 "wmerge.w"
/*6:*/
#line 93 "wmerge.w"
#line 80 "wmerge-p.ch"
int input_ln(FILE*fp)
#line 96 "wmerge.w"
{
register int c= EOF;
register char*k;
if(feof(fp))return(0);
limit= k= buffer;
while(k<=buffer_end&&(c= getc(fp))!=EOF&&c!='\n')
if((*(k++)= c)!=' ')limit= k;
if(k>buffer_end)
if((c= getc(fp))!=EOF&&c!='\n'){
ungetc(c,fp);loc= buffer;err_print("! Input line too long");
}
if(c==EOF&&limit==buffer)return(0);
return(1);
}
/*:6*//*9:*/
#line 171 "wmerge.w"
#line 97 "wmerge-p.ch"
void prime_the_change_buffer(void)
#line 174 "wmerge.w"
{
change_limit= change_buffer;
/*10:*/
#line 185 "wmerge.w"
while(1){
change_line++;
if(!input_ln(change_file))return;
if(limit<buffer+2)continue;
if(buffer[0]!='@')continue;
if(isupper(buffer[1]))buffer[1]= tolower(buffer[1]);
if(buffer[1]=='x')break;
if(buffer[1]=='y'||buffer[1]=='z'||buffer[1]=='i'){
loc= buffer+2;
err_print("! Missing @x in change file");
}
}
/*:10*/
#line 176 "wmerge.w"
;
/*11:*/
#line 202 "wmerge.w"
do{
change_line++;
if(!input_ln(change_file)){
err_print("! Change file ended after @x");
return;
}
}while(limit==buffer);
/*:11*/
#line 177 "wmerge.w"
;
/*12:*/
#line 212 "wmerge.w"
{
change_limit= change_buffer-buffer+limit;
strncpy(change_buffer,buffer,limit-buffer+1);
}
/*:12*/
#line 178 "wmerge.w"
;
}
/*:9*//*13:*/
#line 230 "wmerge.w"
#line 105 "wmerge-p.ch"
void check_change(void)
#line 233 "wmerge.w"
{
int n= 0;
if(lines_dont_match)return;
while(1){
changing= 1;change_line++;
if(!input_ln(change_file)){
err_print("! Change file ended before @y");
change_limit= change_buffer;changing= 0;
return;
}
if(limit>buffer+1&&buffer[0]=='@'){
if(isupper(buffer[1]))buffer[1]= tolower(buffer[1]);
/*14:*/
#line 263 "wmerge.w"
if(buffer[1]=='x'||buffer[1]=='z'){
loc= buffer+2;err_print("! Where is the matching @y?");
}
else if(buffer[1]=='y'){
if(n>0){
loc= buffer+2;
fprintf(stderr,"\n! Hmm... %d ",n);
err_print("of the preceding lines failed to match");
}
change_depth= include_depth;
return;
}
/*:14*/
#line 247 "wmerge.w"
;
}
/*12:*/
#line 212 "wmerge.w"
{
change_limit= change_buffer-buffer+limit;
strncpy(change_buffer,buffer,limit-buffer+1);
}
/*:12*/
#line 249 "wmerge.w"
;
changing= 0;cur_line++;
while(!input_ln(cur_file)){
if(include_depth==0){
err_print("! CWEB file ended during a change");
input_has_ended= 1;return;
}
include_depth--;cur_line++;
}
if(lines_dont_match)n++;
}
}
/*:13*//*15:*/
#line 282 "wmerge.w"
#line 113 "wmerge-p.ch"
void reset_input(void)
#line 285 "wmerge.w"
{
limit= buffer;loc= buffer+1;buffer[0]= ' ';
/*16:*/
#line 297 "wmerge.w"
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);
}
web_file_open= 1;
if((change_file= fopen(change_file_name,"r"))==NULL)
fatal("! Cannot open change file ",change_file_name);
/*:16*/
#line 287 "wmerge.w"
;
include_depth= 0;cur_line= 0;change_line= 0;
change_depth= include_depth;
changing= 1;prime_the_change_buffer();changing= !changing;
limit= buffer;loc= buffer+1;buffer[0]= ' ';input_has_ended= 0;
}
/*:15*//*17:*/
#line 315 "wmerge.w"
get_line()
{
restart:
if(changing&&include_depth==change_depth)
/*21:*/
#line 423 "wmerge.w"
{
change_line++;
if(!input_ln(change_file)){
err_print("! Change file ended without @z");
buffer[0]= '@';buffer[1]= 'z';limit= buffer+2;
}
if(limit>buffer){
*limit= ' ';
if(buffer[0]=='@'){
if(isupper(buffer[1]))buffer[1]= tolower(buffer[1]);
if(buffer[1]=='x'||buffer[1]=='y'){
loc= buffer+2;
err_print("! Where is the matching @z?");
}
else if(buffer[1]=='z'){
prime_the_change_buffer();changing= !changing;
}
}
}
}
/*:21*/
#line 320 "wmerge.w"
;
if(!changing||include_depth>change_depth){
/*20:*/
#line 407 "wmerge.w"
{
cur_line++;
while(!input_ln(cur_file)){
if(include_depth==0){input_has_ended= 1;break;}
else{
fclose(cur_file);include_depth--;
if(changing&&include_depth==change_depth)break;
cur_line++;
}
}
if(!changing&&!input_has_ended)
if(limit-buffer==change_limit-change_buffer)
if(buffer[0]==change_buffer[0])
if(change_limit>change_buffer)check_change();
}
/*:20*/
#line 322 "wmerge.w"
;
if(changing&&include_depth==change_depth)goto restart;
}
loc= buffer;*limit= ' ';
if(*buffer=='@'&&(*(buffer+1)=='i'||*(buffer+1)=='I')){
loc= buffer+2;
while(loc<=limit&&(*loc==' '||*loc=='\t'||*loc=='"'))loc++;
if(loc>=limit){
err_print("! Include file name not given");
goto restart;
}
if(include_depth>=max_include_depth-1){
err_print("! Too many nested includes");
goto restart;
}
include_depth++;
/*19:*/
#line 366 "wmerge.w"
{
char temp_file_name[max_file_name_length];
char*cur_file_name_end= cur_file_name+max_file_name_length-1;
char*k= cur_file_name,*kk;
int l;
while(*loc!=' '&&*loc!='\t'&&*loc!='"'&&k<=cur_file_name_end)*k++= *loc++;
if(k>cur_file_name_end)too_long();
*k= '\0';
if((cur_file= fopen(cur_file_name,"r"))!=NULL){
cur_line= 0;
goto restart;
}
#line 197 "wmerge-p.ch"
if(0==set_path(include_path,getenv("CWEBINPUTS"))){
include_depth--;goto restart;
}
path_prefix= include_path;
while(path_prefix){
for(kk= temp_file_name,p= pa