| 1 |
% Modified 16 Jan 2002 to agree with COMMON version 3.64 |
| 2 |
|
| 3 |
\def\9#1{} % this hack is explained in CWEB manual Appendix F11 |
| 4 |
|
| 5 |
@* Introduction. This file contains the program \.{wmerge}, |
| 6 |
which takes two or more files and merges them according |
| 7 |
to the conventions of \.{CWEB}. Namely, it takes an ordinary \.{.w} |
| 8 |
file and an optional \.{.ch} file and sends the corresponding |
| 9 |
\.{.w}-style file to standard output (or to a named file), |
| 10 |
expanding all ``includes'' |
| 11 |
that might be specified by \.{@@i} in the original \.{.w} file. |
| 12 |
(A more precise description appears in the section on ``command line |
| 13 |
arguments'' below.) |
| 14 |
|
| 15 |
@c |
| 16 |
#include <stdio.h> |
| 17 |
#include <stdbool.h> |
| 18 |
#include <stdlib.h> /* declaration of |getenv| */ |
| 19 |
#include <string.h> /* declaration of |strlen| */ |
| 20 |
#include <ctype.h> /* definition of |isalpha|, |isdigit| and so on */ |
| 21 |
@<Definitions@>@; |
| 22 |
@<Predeclarations of functions@>@; |
| 23 |
@<Functions@>@; |
| 24 |
int main ( |
| 25 |
int ac, char **av) |
| 26 |
{ |
| 27 |
argc=ac; argv=av; |
| 28 |
@<Set the default options@>; |
| 29 |
@<Scan arguments and open output file@>; |
| 30 |
reset_input(); |
| 31 |
while (get_line()) |
| 32 |
put_line(); |
| 33 |
fflush(out_file); |
| 34 |
check_complete(); |
| 35 |
fflush(out_file); |
| 36 |
return wrap_up(); |
| 37 |
} |
| 38 |
|
| 39 |
@ @<Definitions@>= |
| 40 |
typedef unsigned char eight_bits; |
| 41 |
typedef char ASCII; /* type of characters inside \.{WEB} */ |
| 42 |
|
| 43 |
@ @<Predec...@>= |
| 44 |
bool input_ln(FILE *fp); |
| 45 |
bool wrap_up(void); |
| 46 |
void prime_the_change_buffer(void); |
| 47 |
void check_change(void); |
| 48 |
void reset_input(void); |
| 49 |
bool get_line(void); |
| 50 |
void put_line(void); |
| 51 |
void check_complete(void); |
| 52 |
|
| 53 |
@ The lowest level of input to the \.{WEB} programs |
| 54 |
is performed by |input_ln|, which must be told which file to read from. |
| 55 |
The return value of |input_ln| is 1 if the read is successful and 0 if |
| 56 |
not (generally this means the file has ended). |
| 57 |
The characters of the next line of the file |
| 58 |
are copied into the |buffer| array, |
| 59 |
and the global variable |limit| is set to the first unoccupied position. |
| 60 |
Trailing blanks are ignored. The value of |limit| must be strictly less |
| 61 |
than |buf_size|, so that |buffer[buf_size-1]| is never filled. |
| 62 |
|
| 63 |
Some of the routines use the fact that it is safe to refer to |
| 64 |
|*(limit+2)| without overstepping the bounds of the array. |
| 65 |
|
| 66 |
@d buf_size 4096 |
| 67 |
|
| 68 |
@<Definitions...@>= |
| 69 |
ASCII buffer[buf_size]; /* where each line of input goes */ |
| 70 |
ASCII *buffer_end=buffer+buf_size-2; /* end of |buffer| */ |
| 71 |
ASCII *limit; /* points to the last character in the buffer */ |
| 72 |
ASCII *loc; /* points to the next character to be read from the buffer */ |
| 73 |
|
| 74 |
@ In the unlikely event that your standard I/O library does not |
| 75 |
support |feof|, |getc| and |ungetc|, you may have to change things here. |
| 76 |
@^system dependencies@> |
| 77 |
|
| 78 |
Incidentally, here's a curious fact about \.{CWEB} for those of you |
| 79 |
who are reading this file as an example of \.{CWEB} programming. |
| 80 |
The file \.{stdio.h} includes a typedef for |
| 81 |
the identifier |FILE|, which is not, strictly speaking, part of \CEE/. |
| 82 |
It turns out \.{CWEAVE} knows that |FILE| is a reserved word (after all, |
| 83 |
|FILE| is almost as common as |int|); indeed, \.{CWEAVE} knows all |
| 84 |
the types of the ISO standard \CEE/ library. But |
| 85 |
if you're using other types like {\bf caddr\_t}, |
| 86 |
@:caddr_t}{\bf caddr\_t@> |
| 87 |
which is defined in \.{/usr/include/sys/types.h}, you should let |
| 88 |
\.{WEAVE} know that this is a type, either by including the \.{.h} file |
| 89 |
at \.{WEB} time (saying \.{@@i /usr/include/sys/types.h}), or by |
| 90 |
using \.{WEB}'s format command (saying \.{@@f caddr\_t int}). Either of |
| 91 |
these will make {\bf caddr\_t} be treated in the same way as |int|. |
| 92 |
|
| 93 |
@<Func...@>= |
| 94 |
bool input_ln( /* copies a line into |buffer| or returns 0 */ |
| 95 |
FILE *fp) /* what file to read from */ |
| 96 |
{ |
| 97 |
register int c=EOF; /* character read; initialized so some compilers won't complain */ |
| 98 |
register char *k; /* where next character goes */ |
| 99 |
if (feof(fp)) return false; /* we have hit end-of-file */ |
| 100 |
limit = k = buffer; /* beginning of buffer */ |
| 101 |
while (k<=buffer_end && (c=getc(fp)) != EOF && c!='\n') |
| 102 |
if ((*(k++) = c) != ' ') limit = k; |
| 103 |
if (k>buffer_end) |
| 104 |
if ((c=getc(fp))!=EOF && c!='\n') { |
| 105 |
ungetc(c,fp); loc=buffer; err_print("! Input line too long"); |
| 106 |
@.Input line too long@> |
| 107 |
} |
| 108 |
if (c==EOF && limit==buffer) return false; /* there was nothing after |
| 109 |
the last newline */ |
| 110 |
return true; |
| 111 |
} |
| 112 |
|
| 113 |
@ Now comes the problem of deciding which file to read from next. |
| 114 |
Recall that the actual text that \.{CWEB} should process comes from two |
| 115 |
streams: a |web_file|, which can contain possibly nested include |
| 116 |
commands \.{@@i}, and a |change_file|, which might also contain |
| 117 |
includes. The |web_file| together with the currently open include |
| 118 |
files form a stack |file|, whose names are stored in a parallel stack |
| 119 |
|file_name|. The boolean |changing| tells whether or not we're reading |
| 120 |
from the |change_file|. |
| 121 |
|
| 122 |
The line number of each open file is also kept for error reporting. |
| 123 |
|
| 124 |
@f line x /* make |line| an unreserved word */ |
| 125 |
@d max_include_depth 10 /* maximum number of source files open |
| 126 |
simultaneously, not counting the change file */ |
| 127 |
@d max_file_name_length 60 |
| 128 |
@d cur_file file[include_depth] /* current file */ |
| 129 |
@d cur_file_name file_name[include_depth] /* current file name */ |
| 130 |
@d cur_line line[include_depth] /* number of current line in current file */ |
| 131 |
@d web_file file[0] /* main source file */ |
| 132 |
@d web_file_name file_name[0] /* main source file name */ |
| 133 |
|
| 134 |
@<Definitions...@>= |
| 135 |
int include_depth; /* current level of nesting */ |
| 136 |
FILE *file[max_include_depth]; /* stack of non-change files */ |
| 137 |
FILE *change_file; /* change file */ |
| 138 |
char file_name[max_include_depth][max_file_name_length]; |
| 139 |
/* stack of non-change file names */ |
| 140 |
char change_file_name[max_file_name_length]; /* name of change file */ |
| 141 |
char alt_web_file_name[max_file_name_length]; /* alternate name to try */ |
| 142 |
int line[max_include_depth]; /* number of current line in the stacked files */ |
| 143 |
int change_line; /* number of current line in change file */ |
| 144 |
int change_depth; /* where \.{@@y} originated during a change */ |
| 145 |
bool input_has_ended; /* if there is no more input */ |
| 146 |
bool changing; /* if the current line is from |change_file| */ |
| 147 |
bool web_file_open=false; /* if the web file is being read */ |
| 148 |
|
| 149 |
@ When |changing=false|, the next line of |change_file| is kept in |
| 150 |
|change_buffer|, for purposes of comparison with the next |
| 151 |
line of |cur_file|. After the change file has been completely input, we |
| 152 |
set |change_limit=change_buffer|, |
| 153 |
so that no further matches will be made. |
| 154 |
|
| 155 |
Here's a shorthand expression for inequality between the two lines: |
| 156 |
|
| 157 |
@d lines_dont_match (change_limit-change_buffer != limit-buffer || |
| 158 |
strncmp(buffer, change_buffer, limit-buffer)) |
| 159 |
|
| 160 |
@<Def...@>= |
| 161 |
char change_buffer[buf_size]; /* next line of |change_file| */ |
| 162 |
char *change_limit; /* points to the last character in |change_buffer| */ |
| 163 |
|
| 164 |
@ Procedure |prime_the_change_buffer| sets |change_buffer| in |
| 165 |
preparation for the next matching operation. Since blank lines in the change |
| 166 |
file are not used for matching, we have |
| 167 |
|(change_limit==change_buffer && !changing)| if and only if |
| 168 |
the change file is exhausted. This procedure is called only when |
| 169 |
|changing| is |true|; hence error messages will be reported correctly. |
| 170 |
|
| 171 |
@<Func...@>= |
| 172 |
void |
| 173 |
prime_the_change_buffer(void) |
| 174 |
{ |
| 175 |
change_limit=change_buffer; /* this value is used if the change file ends */ |
| 176 |
@<Skip over comment lines in the change file; |return| if end of file@>; |
| 177 |
@<Skip to the next nonblank line; |return| if end of file@>; |
| 178 |
@<Move |buffer| and |limit| to |change_buffer| and |change_limit|@>; |
| 179 |
} |
| 180 |
|
| 181 |
@ While looking for a line that begins with \.{@@x} in the change file, we |
| 182 |
allow lines that begin with \.{@@}, as long as they don't begin with \.{@@y}, |
| 183 |
\.{@@z} or \.{@@i} (which would probably mean that the change file is fouled up). |
| 184 |
|
| 185 |
@<Skip over comment lines in the change file...@>= |
| 186 |
while (true) { |
| 187 |
change_line++; |
| 188 |
if (!input_ln(change_file)) return; |
| 189 |
if (limit<buffer+2) continue; |
| 190 |
if (buffer[0]!='@@') continue; |
| 191 |
if (isupper(buffer[1])) buffer[1]=tolower(buffer[1]); |
| 192 |
if (buffer[1]=='x') break; |
| 193 |
if (buffer[1]=='y' || buffer[1]=='z' || buffer[1]=='i') { |
| 194 |
loc=buffer+2; |
| 195 |
err_print("! Missing @@x in change file"); |
| 196 |
@.Missing @@x...@> |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
@ Here we are looking at lines following the \.{@@x}. |
| 201 |
|
| 202 |
@<Skip to the next nonblank line...@>= |
| 203 |
do { |
| 204 |
change_line++; |
| 205 |
if (!input_ln(change_file)) { |
| 206 |
err_print("! Change file ended after @@x"); |
| 207 |
@.Change file ended...@> |
| 208 |
return; |
| 209 |
} |
| 210 |
} while (limit==buffer); |
| 211 |
|
| 212 |
@ @<Move |buffer| and |limit| to |change_buffer| and |change_limit|@>= |
| 213 |
{ |
| 214 |
change_limit=change_buffer+(limit-buffer); |
| 215 |
strncpy(change_buffer,buffer,limit-buffer+1); |
| 216 |
} |
| 217 |
|
| 218 |
@ The following procedure is used to see if the next change entry should |
| 219 |
go into effect; it is called only when |changing| is |false|. |
| 220 |
The idea is to test whether or not the current |
| 221 |
contents of |buffer| matches the current contents of |change_buffer|. |
| 222 |
If not, there's nothing more to do; but if so, a change is called for: |
| 223 |
All of the text down to the \.{@@y} is supposed to match. An error |
| 224 |
message is issued if any discrepancy is found. Then the procedure |
| 225 |
prepares to read the next line from |change_file|. |
| 226 |
|
| 227 |
This procedure is called only when |buffer<limit|, i.e., when the |
| 228 |
current line is nonempty. |
| 229 |
|
| 230 |
@<Func...@>= |
| 231 |
void |
| 232 |
check_change(void) /* switches to |change_file| if the buffers match */ |
| 233 |
{ |
| 234 |
int n=0; /* the number of discrepancies found */ |
| 235 |
if (lines_dont_match) return; |
| 236 |
while (true) { |
| 237 |
changing=true; change_line++; |
| 238 |
if (!input_ln(change_file)) { |
| 239 |
err_print("! Change file ended before @@y"); |
| 240 |
@.Change file ended...@> |
| 241 |
change_limit=change_buffer; changing=false; |
| 242 |
return; |
| 243 |
} |
| 244 |
if (limit>buffer+1 && buffer[0]=='@@') { |
| 245 |
char xyz_code=isupper(buffer[1])? tolower(buffer[1]): buffer[1]; |
| 246 |
@<If the current line starts with \.{@@y}, |
| 247 |
report any discrepancies and |return|@>; |
| 248 |
} |
| 249 |
@<Move |buffer| and |limit|...@>; |
| 250 |
changing=false; cur_line++; |
| 251 |
while (!input_ln(cur_file)) { /* pop the stack or quit */ |
| 252 |
if (include_depth==0) { |
| 253 |
err_print("! CWEB file ended during a change"); |
| 254 |
@.CWEB file ended...@> |
| 255 |
input_has_ended=1; return; |
| 256 |
} |
| 257 |
include_depth--; cur_line++; |
| 258 |
} |
| 259 |
if (lines_dont_match) n++; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
@ @<If the current line starts with \.{@@y}...@>= |
| 264 |
if (xyz_code=='x' || xyz_code=='z') { |
| 265 |
loc=buffer+2; err_print("! Where is the matching @@y?"); |
| 266 |
@.Where is the match...@> |
| 267 |
} |
| 268 |
else if (xyz_code=='y') { |
| 269 |
if (n>0) { |
| 270 |
loc=buffer+2; |
| 271 |
fprintf(stderr,"\n! Hmm... %d ",n); |
| 272 |
err_print("of the preceding lines failed to match"); |
| 273 |
@.Hmm... n of the preceding...@> |
| 274 |
} |
| 275 |
change_depth=include_depth; |
| 276 |
return; |
| 277 |
} |
| 278 |
|
| 279 |
@ The |reset_input| procedure gets the program ready to read the |
| 280 |
user's \.{WEB} input. |
| 281 |
|
| 282 |
@<Func...@>= |
| 283 |
void |
| 284 |
reset_input(void) |
| 285 |
{ |
| 286 |
limit=buffer; loc=buffer+1; buffer[0]=' '; |
| 287 |
@<Open input files@>; |
| 288 |
include_depth=0; cur_line=0; change_line=0; |
| 289 |
change_depth=include_depth; |
| 290 |
changing=true; prime_the_change_buffer(); changing=!changing; |
| 291 |
limit=buffer; loc=buffer+1; buffer[0]=' '; input_has_ended=0; |
| 292 |
} |
| 293 |
|
| 294 |
@ The following code opens the input files. |
| 295 |
@^system dependencies@> |
| 296 |
|
| 297 |
@<Open input files@>= |
| 298 |
if ((web_file=fopen(web_file_name,"r"))==NULL) { |
| 299 |
strcpy(web_file_name,alt_web_file_name); |
| 300 |
if ((web_file=fopen(web_file_name,"r"))==NULL) |
| 301 |
fatal("! Cannot open input file ", web_file_name); |
| 302 |
} |
| 303 |
@.Cannot open input file@> |
| 304 |
@.Cannot open change file@> |
| 305 |
web_file_open=1; |
| 306 |
if ((change_file=fopen(change_file_name,"r"))==NULL) |
| 307 |
fatal("! Cannot open change file ", change_file_name); |
| 308 |
|
| 309 |
@ The |get_line| procedure is called when |loc>limit|; it puts the next |
| 310 |
line of merged input into the buffer and updates the other variables |
| 311 |
appropriately. A space is placed at the right end of the line. |
| 312 |
This procedure returns |!input_has_ended| because we often want to |
| 313 |
check the value of that variable after calling the procedure. |
| 314 |
|
| 315 |
@<Fun...@>= |
| 316 |
bool get_line(void) /* inputs the next line */ |
| 317 |
{ |
| 318 |
restart: |
| 319 |
if (changing && include_depth==change_depth) |
| 320 |
@<Read from |change_file| and maybe turn off |changing|@>; |
| 321 |
if (! changing || include_depth>change_depth) { |
| 322 |
@<Read from |cur_file| and maybe turn on |changing|@>; |
| 323 |
if (changing && include_depth==change_depth) goto restart; |
| 324 |
} |
| 325 |
if (input_has_ended) return false; |
| 326 |
loc=buffer; *limit=' '; |
| 327 |
if (buffer[0]=='@@' && (buffer[1]=='i' || buffer[1]=='I')) { |
| 328 |
loc=buffer+2; *limit='"'; |
| 329 |
while (*loc==' '||*loc=='\t') loc++; |
| 330 |
if (loc>=limit) { |
| 331 |
err_print("! Include file name not given"); |
| 332 |
@.Include file name ...@> |
| 333 |
goto restart; |
| 334 |
} |
| 335 |
if (include_depth>=max_include_depth-1) { |
| 336 |
err_print("! Too many nested includes"); |
| 337 |
@.Too many nested includes@> |
| 338 |
goto restart; |
| 339 |
} |
| 340 |
include_depth++; /* push input stack */ |
| 341 |
@<Try to open include file, abort push if unsuccessful, go to |restart|@>; |
| 342 |
} |
| 343 |
return true; |
| 344 |
} |
| 345 |
|
| 346 |
void put_line(void) |
| 347 |
{ |
| 348 |
char *ptr=buffer; |
| 349 |
while (ptr<limit) putc(*ptr++,out_file); |
| 350 |
putc('\n',out_file); |
| 351 |
} |
| 352 |
|
| 353 |
@ When an \.{@@i} line is found in the |cur_file|, we must temporarily |
| 354 |
stop reading it and start reading from the named include file. The |
| 355 |
\.{@@i} line should give a complete file name with or without |
| 356 |
double quotes. |
| 357 |
If the environment variable \.{CWEBINPUTS} is set, or if the compiler flag |
| 358 |
of the same name was defined at compile time, |
| 359 |
\.{CWEB} will look for include files in the directory thus named, if |
| 360 |
it cannot find them in the current directory. |
| 361 |
(Colon-separated paths are not supported.) |
| 362 |
The remainder of the \.{@@i} line after the file name is ignored. |
| 363 |
|
| 364 |
@d too_long() {include_depth--; |
| 365 |
err_print("! Include file name too long"); goto restart;} |
| 366 |
|
| 367 |
@ @<Try to open...@>= { |
| 368 |
char temp_file_name[max_file_name_length]; |
| 369 |
char *cur_file_name_end=cur_file_name+max_file_name_length-1; |
| 370 |
char *k=cur_file_name, *kk; |
| 371 |
int l; /* length of file name */ |
| 372 |
|
| 373 |
if (*loc=='"') { |
| 374 |
loc++; |
| 375 |
while (*loc!='"' && k<=cur_file_name_end) *k++=*loc++; |
| 376 |
if (loc==limit) k=cur_file_name_end+1; /* unmatched quote is `too long' */ |
| 377 |
} else |
| 378 |
while (*loc!=' '&&*loc!='\t'&&*loc!='"'&&k<=cur_file_name_end) *k++=*loc++; |
| 379 |
if (k>cur_file_name_end) too_long(); |
| 380 |
@.Include file name ...@> |
| 381 |
*k='\0'; |
| 382 |
if ((cur_file=fopen(cur_file_name,"r"))!=NULL) { |
| 383 |
cur_line=0; |
| 384 |
goto restart; /* success */ |
| 385 |
} |
| 386 |
kk=getenv("CWEBINPUTS"); |
| 387 |
if (kk!=NULL) { |
| 388 |
if ((l=strlen(kk))>max_file_name_length-2) too_long(); |
| 389 |
strcpy(temp_file_name,kk); |
| 390 |
} |
| 391 |
else { |
| 392 |
#ifdef CWEBINPUTS |
| 393 |
if ((l=strlen(CWEBINPUTS))>max_file_name_length-2) too_long(); |
| 394 |
strcpy(temp_file_name,CWEBINPUTS); |
| 395 |
#else |
| 396 |
l=0; |
| 397 |
#endif /* |CWEBINPUTS| */ |
| 398 |
} |
| 399 |
if (l>0) { |
| 400 |
if (k+l+2>=cur_file_name_end) too_long(); |
| 401 |
@.Include file name ...@> |
| 402 |
for (; k>= cur_file_name; k--) *(k+l+1)=*k; |
| 403 |
strcpy(cur_file_name,temp_file_name); |
| 404 |
cur_file_name[l]='/'; /* \UNIX/ pathname separator */ |
| 405 |
if ((cur_file=fopen(cur_file_name,"r"))!=NULL) { |
| 406 |
cur_line=0; |
| 407 |
goto restart; /* success */ |
| 408 |
} |
| 409 |
} |
| 410 |
include_depth--; err_print("! Cannot open include file"); goto restart; |
| 411 |
} |
| 412 |
|
| 413 |
@ @<Read from |cur_file|...@>= { |
| 414 |
cur_line++; |
| 415 |
while (!input_ln(cur_file)) { /* pop the stack or quit */ |
| 416 |
if (include_depth==0) {input_has_ended=1; break;} |
| 417 |
else { |
| 418 |
fclose(cur_file); include_depth--; |
| 419 |
if (changing && include_depth==change_depth) break; |
| 420 |
cur_line++; |
| 421 |
} |
| 422 |
} |
| 423 |
if (!changing && !input_has_ended) |
| 424 |
if (limit-buffer==change_limit-change_buffer) |
| 425 |
if (buffer[0]==change_buffer[0]) |
| 426 |
if (change_limit>change_buffer) check_change(); |
| 427 |
} |
| 428 |
|
| 429 |
@ @<Read from |change_file|...@>= { |
| 430 |
change_line++; |
| 431 |
if (!input_ln(change_file)) { |
| 432 |
err_print("! Change file ended without @@z"); |
| 433 |
@.Change file ended...@> |
| 434 |
buffer[0]='@@'; buffer[1]='z'; limit=buffer+2; |
| 435 |
} |
| 436 |
if (limit>buffer) { /* check if the change has ended */ |
| 437 |
*limit=' '; |
| 438 |
if (buffer[0]=='@@') { |
| 439 |
if (isupper(buffer[1])) buffer[1]=tolower(buffer[1]); |
| 440 |
if (buffer[1]=='x' || buffer[1]=='y') { |
| 441 |
loc=buffer+2; |
| 442 |
err_print("! Where is the matching @@z?"); |
| 443 |
@.Where is the match...@> |
| 444 |
} |
| 445 |
else if (buffer[1]=='z') { |
| 446 |
prime_the_change_buffer(); changing=!changing; |
| 447 |
} |
| 448 |
} |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
@ At the end of the program, we will tell the user if the change file |
| 453 |
had a line that didn't match any relevant line in |web_file|. |
| 454 |
|
| 455 |
@<Funct...@>= |
| 456 |
void |
| 457 |
check_complete(void) |
| 458 |
{ |
| 459 |
if (change_limit!=change_buffer) { /* |changing| is |false| */ |
| 460 |
strncpy(buffer,change_buffer,change_limit-change_buffer+1); |
| 461 |
limit=buffer+(int)(change_limit-change_buffer); |
| 462 |
changing=true; change_depth=include_depth; loc=buffer; |
| 463 |
err_print("! Change file entry did not match"); |
| 464 |
@.Change file entry did not match@> |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
@* Reporting errors to the user. |
| 469 |
A global variable called |history| will contain one of four values |
| 470 |
at the end of every run: |spotless| means that no unusual messages were |
| 471 |
printed; |harmless_message| means that a message of possible interest |
| 472 |
was printed but no serious errors were detected; |error_message| means that |
| 473 |
at least one error was found; |fatal_message| means that the program |
| 474 |
terminated abnormally. The value of |history| does not influence the |
| 475 |
behavior of the program; it is simply computed for the convenience |
| 476 |
of systems that might want to use such information. |
| 477 |
|
| 478 |
@d spotless 0 /* |history| value for normal jobs */ |
| 479 |
@d harmless_message 1 /* |history| value when non-serious info was printed */ |
| 480 |
@d error_message 2 /* |history| value when an error was noted */ |
| 481 |
@d fatal_message 3 /* |history| value when we had to stop prematurely */ |
| 482 |
@d mark_harmless {if (history==spotless) history=harmless_message;} |
| 483 |
@d mark_error history=error_message |
| 484 |
|
| 485 |
@<Definit...@>= |
| 486 |
int history=spotless; /* indicates how bad this run was */ |
| 487 |
|
| 488 |
@ The command `|err_print("! Error message")|' will report a syntax error to |
| 489 |
the user, by printing the error message at the beginning of a new line and |
| 490 |
then giving an indication of where the error was spotted in the source file. |
| 491 |
Note that no period follows the error message, since the error routine |
| 492 |
will automatically supply a period. A newline is automatically supplied |
| 493 |
if the string begins with |"!"|. |
| 494 |
|
| 495 |
The actual error indications are provided by a procedure called |error|. |
| 496 |
|
| 497 |
@<Predecl...@>= |
| 498 |
void err_print(char *s); |
| 499 |
|
| 500 |
@ @<Functions...@>= |
| 501 |
void |
| 502 |
err_print( /* prints `\..' and location of error message */ |
| 503 |
char *s) |
| 504 |
{ |
| 505 |
char *k,*l; /* pointers into |buffer| */ |
| 506 |
fprintf(stderr,*s=='!'? "\n%s" : "%s",s); |
| 507 |
if(web_file_open) @<Print error location based on input buffer@>@; |
| 508 |
else putc('\n',stderr); |
| 509 |
update_terminal; mark_error; |
| 510 |
} |
| 511 |
|
| 512 |
@ The error locations can be indicated by using the global variables |
| 513 |
|loc|, |cur_line|, |cur_file_name| and |changing|, |
| 514 |
which tell respectively the first |
| 515 |
unlooked-at position in |buffer|, the current line number, the current |
| 516 |
file, and whether the current line is from |change_file| or |cur_file|. |
| 517 |
This routine should be modified on systems whose standard text editor |
| 518 |
has special line-numbering conventions. |
| 519 |
@^system dependencies@> |
| 520 |
|
| 521 |
@<Print error location based on input buffer@>= |
| 522 |
{if (changing && include_depth==change_depth) |
| 523 |
fprintf(stderr,". (l. %d of change file)\n", change_line); |
| 524 |
else if (include_depth==0) fprintf(stderr,". (l. %d)\n", cur_line); |
| 525 |
else fprintf(stderr,". (l. %d of include file %s)\n", cur_line, cur_file_name); |
| 526 |
l= (loc>=limit? limit: loc); |
| 527 |
if (l>buffer) { |
| 528 |
for (k=buffer; k<l; k++) |
| 529 |
if (*k=='\t') putc(' ',stderr); |
| 530 |
else putc(*k,stderr); /* print the characters already read */ |
| 531 |
putc('\n',stderr); |
| 532 |
for (k=buffer; k<l; k++) putc(' ',stderr); /* space out the next line */ |
| 533 |
} |
| 534 |
for (k=l; k<limit; k++) putc(*k,stderr); /* print the part not yet read */ |
| 535 |
putc('\n',stderr); |
| 536 |
} |
| 537 |
|
| 538 |
@ When no recovery from some error has been provided, we have to wrap |
| 539 |
up and quit as graciously as possible. This is done by calling the |
| 540 |
function |wrap_up| at the end of the code. |
| 541 |
|
| 542 |
@d fatal(s,t) { |
| 543 |
fprintf(stderr,s); err_print(t); |
| 544 |
history=fatal_message; exit(wrap_up()); |
| 545 |
} |
| 546 |
|
| 547 |
@ Some implementations may wish to pass the |history| value to the |
| 548 |
operating system so that it can be used to govern whether or not other |
| 549 |
programs are started. Here, for instance, we pass the operating system |
| 550 |
a status of 0 if and only if only harmless messages were printed. |
| 551 |
@^system dependencies@> |
| 552 |
|
| 553 |
@<Func...@>= |
| 554 |
bool wrap_up(void) { |
| 555 |
@<Print the job |history|@>; |
| 556 |
if (history > harmless_message) return true; |
| 557 |
else return false; |
| 558 |
} |
| 559 |
|
| 560 |
@ @<Print the job |history|@>= |
| 561 |
switch (history) { |
| 562 |
case spotless: if (show_happiness) fprintf(stderr,"(No errors were found.)\n"); break; |
| 563 |
case harmless_message: |
| 564 |
fprintf(stderr,"(Did you see the warning message above?)\n"); break; |
| 565 |
case error_message: |
| 566 |
fprintf(stderr,"(Pardon me, but I think I spotted something wrong.)\n"); break; |
| 567 |
case fatal_message: fprintf(stderr,"(That was a fatal error, my friend.)\n"); |
| 568 |
} /* there are no other cases */ |
| 569 |
|
| 570 |
@* Command line arguments. |
| 571 |
The user calls \.{wmerge} with arguments on the command line. |
| 572 |
These are either file names or flags to be turned off (beginning with |"-"|) |
| 573 |
or flags to be turned on (beginning with |"+"|. |
| 574 |
The following globals are for communicating the user's desires to the rest |
| 575 |
of the program. The various file name variables contain strings with |
| 576 |
the names of those files. Most of the 128 flags are undefined but available |
| 577 |
for future extensions. |
| 578 |
|
| 579 |
@d show_banner flags['b'] /* should the banner line be printed? */ |
| 580 |
@d show_happiness flags['h'] /* should lack of errors be announced? */ |
| 581 |
|
| 582 |
@<Defin...@>= |
| 583 |
int argc; /* copy of |ac| parameter to |main| */ |
| 584 |
char **argv; /* copy of |av| parameter to |main| */ |
| 585 |
char out_file_name[max_file_name_length]; /* name of |out_file| */ |
| 586 |
bool flags[128]; /* an option for each 7-bit code */ |
| 587 |
|
| 588 |
@ The |flags| will be initially 1. |
| 589 |
|
| 590 |
@<Set the default options@>= |
| 591 |
show_banner=show_happiness=1; |
| 592 |
|
| 593 |
@ We now must look at the command line arguments and set the file names |
| 594 |
accordingly. At least one file name must be present: the \.{WEB} |
| 595 |
file. It may have an extension, or it may omit it to get |'.w'| |
| 596 |
added. |
| 597 |
|
| 598 |
If there is another file name present among the arguments, it is the |
| 599 |
change file, again either with an extension or without one to get |'.ch'| |
| 600 |
An omitted change file argument means that |'/dev/null'| should be used, |
| 601 |
when no changes are desired. |
| 602 |
@^system dependencies@> |
| 603 |
|
| 604 |
If there's a third file name, it will be the output file. |
| 605 |
|
| 606 |
@<Pred...@>= |
| 607 |
void scan_args(void); |
| 608 |
|
| 609 |
@ @<Function...@>= |
| 610 |
void |
| 611 |
scan_args(void) |
| 612 |
{ |
| 613 |
char *dot_pos; /* position of |'.'| in the argument */ |
| 614 |
register char *s; /* register for scanning strings */ |
| 615 |
bool found_web=false,found_change=false,found_out=false; |
| 616 |
/* have these names have been seen? */ |
| 617 |
bool flag_change; |
| 618 |
|
| 619 |
while (--argc > 0) { |
| 620 |
if (**(++argv)=='-' || **argv=='+') @<Handle flag argument@>@; |
| 621 |
else { |
| 622 |
s=*argv;@+dot_pos=NULL; |
| 623 |
while (*s) { |
| 624 |
if (*s=='.') dot_pos=s++; |
| 625 |
else if (*s=='/') dot_pos=NULL,++s; |
| 626 |
else s++; |
| 627 |
} |
| 628 |
if (!found_web) @<Make |web_file_name|@>@; |
| 629 |
else if (!found_change) @<Make |change_file_name| from |fname|@>@; |
| 630 |
else if (!found_out) @<Override output file name@>@; |
| 631 |
else @<Print usage error message and quit@>; |
| 632 |
} |
| 633 |
} |
| 634 |
if (!found_web) @<Print usage error message and quit@>; |
| 635 |
if (!found_change) strcpy(change_file_name,"/dev/null"); |
| 636 |
} |
| 637 |
|
| 638 |
@ We use all of |*argv| for the |web_file_name| if there is a |'.'| in it, |
| 639 |
otherwise we add |".w"|. If this file can't be opened, we prepare an |
| 640 |
|alt_web_file_name| by adding |"web"| after the dot. |
| 641 |
The other file names come from adding other things |
| 642 |
after the dot. We must check that there is enough room in |
| 643 |
|web_file_name| and the other arrays for the argument. |
| 644 |
|
| 645 |
@<Make |web_file_name|...@>= |
| 646 |
{ |
| 647 |
if (s-*argv > max_file_name_length-5) |
| 648 |
@<Complain about argument length@>; |
| 649 |
if (dot_pos==NULL) |
| 650 |
sprintf(web_file_name,"%s.w",*argv); |
| 651 |
else { |
| 652 |
strcpy(web_file_name,*argv); |
| 653 |
*dot_pos=0; /* string now ends where the dot was */ |
| 654 |
} |
| 655 |
sprintf(alt_web_file_name,"%s.web",*argv); |
| 656 |
*out_file_name='\0'; /* this will print to stdout */ |
| 657 |
found_web=true; |
| 658 |
} |
| 659 |
|
| 660 |
@ @<Make |change_file_name|...@>= |
| 661 |
{ |
| 662 |
if (s-*argv > max_file_name_length-4) |
| 663 |
@<Complain about argument length@>; |
| 664 |
if (dot_pos==NULL) |
| 665 |
sprintf(change_file_name,"%s.ch",*argv); |
| 666 |
else strcpy(change_file_name,*argv); |
| 667 |
found_change=true; |
| 668 |
} |
| 669 |
|
| 670 |
@ @<Override...@>= |
| 671 |
{ |
| 672 |
if (s-*argv > max_file_name_length-5) |
| 673 |
@<Complain about argument length@>; |
| 674 |
if (dot_pos==NULL) sprintf(out_file_name,"%s.out",*argv); |
| 675 |
else strcpy(out_file_name,*argv); |
| 676 |
found_out=true; |
| 677 |
} |
| 678 |
|
| 679 |
@ @<Handle flag...@>= |
| 680 |
{ |
| 681 |
if (**argv=='-') flag_change=0; |
| 682 |
else flag_change=1; |
| 683 |
for(dot_pos=*argv+1;*dot_pos>'\0';dot_pos++) |
| 684 |
flags[(int)*dot_pos]=flag_change; |
| 685 |
} |
| 686 |
|
| 687 |
@ @<Print usage error message and quit@>= |
| 688 |
{ |
| 689 |
fatal("! Usage: wmerge webfile[.w] [changefile[.ch] [outfile[.out]]]\n","")@; |
| 690 |
} |
| 691 |
|
| 692 |
@ @<Complain about arg...@>= fatal("! Filename too long\n", *argv); |
| 693 |
|
| 694 |
@* Output. Here is the code that opens the output file: |
| 695 |
@^system dependencies@> |
| 696 |
|
| 697 |
@<Defin...@>= |
| 698 |
FILE *out_file; /* where output goes */ |
| 699 |
|
| 700 |
@ @<Scan arguments and open output file@>= |
| 701 |
scan_args(); |
| 702 |
if (out_file_name[0]=='\0') out_file=stdout; |
| 703 |
else if ((out_file=fopen(out_file_name,"w"))==NULL) |
| 704 |
fatal("! Cannot open output file ", out_file_name); |
| 705 |
@.Cannot open output file@> |
| 706 |
|
| 707 |
@ The |update_terminal| procedure is called when we want |
| 708 |
to make sure that everything we have output to the terminal so far has |
| 709 |
actually left the computer's internal buffers and been sent. |
| 710 |
@^system dependencies@> |
| 711 |
|
| 712 |
@d update_terminal fflush(stderr) /* empty the terminal output buffer */ |
| 713 |
|
| 714 |
@* Index. |