home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cweb28.zip / examples / wmerge.w < prev    next >
Text File  |  1992-07-08  |  22KB  |  613 lines

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