Debian Codegraph @

texk/web2c/cwebdir/common.w

raw
1 % This file is part of CWEB.
2 % This program by Silvio Levy and Donald E. Knuth
3 % is based on a program by Knuth.
4 % It is distributed WITHOUT ANY WARRANTY, express or implied.
5 % Version 4.12.2 --- july 2025
6
7 % Copyright (C) 1987,1990,1993,2000 Silvio Levy and Donald E. Knuth
8
9 % Permission is granted to make and distribute verbatim copies of this
10 % document provided that the copyright notice and this permission notice
11 % are preserved on all copies.
12
13 % Permission is granted to copy and distribute modified versions of this
14 % document under the conditions for verbatim copying, provided that the
15 % entire resulting derived work is given a different name and distributed
16 % under the terms of a permission notice identical to this one.
17
18 % Amendments to 'common.w' resulting in this updated version were created
19 % by numerous collaborators over the course of many years.
20
21 % Please send comments, suggestions, etc. to tex-k@@tug.org.
22
23 \def\v{\char'174} % vertical (|) in typewriter font
24
25 \def\title{Common code for CTANGLE and CWEAVE (Version 4.12.2)}
26 \def\topofcontents{\null\vfill
27 \centerline{\titlefont Common code for {\ttitlefont CTANGLE} and
28 {\ttitlefont CWEAVE}}
29 \vskip 15pt
30 \centerline{(Version 4.12.2)}
31 \vfill}
32 \def\botofcontents{\vfill
33 \noindent
34 Copyright \copyright\ 1987, 1990, 1993, 2000 Silvio Levy and Donald E. Knuth
35 \bigskip\noindent
36 Permission is granted to make and distribute verbatim copies of this
37 document provided that the copyright notice and this permission notice
38 are preserved on all copies.
39
40 \smallskip\noindent
41 Permission is granted to copy and distribute modified versions of this
42 document under the conditions for verbatim copying, provided that the
43 entire resulting derived work is given a different name and distributed
44 under the terms of a permission notice identical to this one.
45 }
46
47 \pageno=\contentspagenumber \advance\pageno by 1
48 \let\maybe=\iftrue
49
50 @* Introduction. This file contains code common
51 to both \.{CTANGLE} and \.{CWEAVE}, which roughly concerns the following
52 problems: character uniformity, input routines, error handling and
53 parsing of command line. We have tried to concentrate in this file
54 all the system dependencies, so as to maximize portability.
55
56 In the texts below we will
57 sometimes use \.{CWEB} to refer to either of the two component
58 programs, if no confusion can arise.
59
60 The file begins with a few basic definitions.
61
62 @c
63 @<Include files@>@/
64 @h
65 @<Common code for \.{CWEAVE} and \.{CTANGLE}@>@/
66 @<Global variables@>@/
67 @<Predeclaration of procedures@>
68
69 @ The details will be filled in due course. The interface |"common.h"| of this
70 \.{COMMON} module is included first. It is also used by the main programs.
71
72 @i common.h
73
74 @ In certain cases \.{CTANGLE} and \.{CWEAVE} should do almost, but not
75 quite, the same thing. In these cases we've written common code for
76 both, differentiating between the two by means of the global variable
77 |program|.
78
79 @<Global var...@>=
80 bool program; /* \.{CWEAVE} or \.{CTANGLE}? */
81
82 @ \.{CWEAVE} operates in three phases: First it inputs the source
83 file and stores cross-reference data, then it inputs the source once again and
84 produces the \TEX/ output file, and finally it sorts and outputs the index.
85 Similarly, \.{CTANGLE} operates in two phases.
86 The global variable |phase| tells which phase we are in.
87
88 @<Global var...@>=
89 int phase; /* which phase are we in? */
90
91 @ There's an initialization procedure that gets both \.{CTANGLE} and
92 \.{CWEAVE} off to a good start. We will fill in the details of this
93 procedure later.
94
95 @c
96 void
97 common_init(void)
98 {
99 @<Initialize pointers@>@;
100 @<Set the default options common to \.{CTANGLE} and \.{CWEAVE}@>@;
101 @<Scan arguments and open output files@>@;
102 }
103
104 @* The character set.
105 \.{CWEB} uses the conventions of \CEE/ programs found in the standard
106 \.{ctype.h} header file.
107
108 A few character pairs are encoded internally as single characters,
109 using the definitions in the interface sections above. These definitions
110 are consistent with an extension of ASCII code originally developed at
111 MIT and explained in Appendix~C of {\sl The \TEX/book\/}; thus, users
112 who have such a character set can type things like \.{\char'32} and
113 \.{\char'4} instead of \.{!=} and \.{\&\&}. (However, their files will
114 not be too portable until more people adopt the extended code.)
115
116 If the character set is not ASCII, the definitions given may conflict
117 with existing characters; in such cases, other arbitrary codes should be
118 substituted. The indexes to \.{CTANGLE} and \.{CWEAVE} mention every
119 case where similar codes may have to be changed in order to
120 avoid character conflicts. Look for the entry ``ASCII code dependencies''
121 in those indexes.
122
123 @^ASCII code dependencies@>
124 @^system dependencies@>
125
126 @<Global var...@>=
127 char section_text[longest_name+1]; /* text being sought for */
128 char *section_text_end = section_text+longest_name; /* end of |section_text| */
129 char *id_first; /* where the current identifier begins in the buffer */
130 char *id_loc; /* just after the current identifier in the buffer */
131
132 @* Input routines. The lowest level of input to the \.{CWEB} programs
133 is performed by |input_ln|, which must be told which file to read from.
134 The return value of |input_ln| is |true| if the read is successful and
135 |false| if not (generally this means the file has ended). The conventions
136 of \TEX/ are followed; i.e., the characters of the next line of the file
137 are copied into the |buffer| array,
138 and the global variable |limit| is set to the first unoccupied position.
139 Trailing blanks are ignored. The value of |limit| must be strictly less
140 than |buf_size|, so that |buffer[buf_size-1]| is never filled.
141
142 Since |buf_size| is strictly less than |long_buf_size|,
143 some of \.{CWEB}'s routines use the fact that it is safe to refer to
144 |*(limit+2)| without overstepping the bounds of the array.
145
146 @<Global var...@>=
147 char buffer[long_buf_size]; /* where each line of input goes */
148 char *buffer_end=buffer+buf_size-2; /* end of |buffer| */
149 char *loc=buffer; /* points to the next character to be read from the buffer */
150 char *limit=buffer; /* points to the last character in the buffer */
151
152 @ In the unlikely event that your standard I/O library does not
153 support |feof| and |getc| you may have to change things here.
154 @^system dependencies@>
155
156 @c
157 static bool input_ln( /* copies a line into |buffer| or returns |false| */
158 FILE *fp) /* what file to read from */
159 {
160 int c=EOF; /* character read; initialized so some compilers won't complain */
161 char *k; /* where next character goes */
162 if (feof(fp)) return false; /* we have hit end-of-file */
163 limit = k = buffer; /* beginning of buffer */
164 while (k<=buffer_end && (c=getc(fp)) != EOF && c!='\n')
165 if ((*(k++) = c) != ' ') limit = k;
166 if (k>buffer_end) {
167 while ((c=getc(fp))!=EOF && c!='\n'); /* discard rest of line */
168 loc=buffer; err_print("! Input line too long");
169 @.Input line too long@>
170 }
171 if (c==EOF && limit==buffer) return false; /* there was nothing after
172 the last newline */
173 return true;
174 }
175
176 @ @<Predecl...@>=@+static bool input_ln(FILE *);
177
178 @* File handling. Now comes the problem of deciding which file to read from
179 next. Recall that the actual text that \.{CWEB} should process comes from two
180 streams: a |web_file|, which can contain possibly nested include
181 commands~\.{@@i}, and a |change_file|, which might also contain
182 includes. The |web_file| together with the currently open include
183 files form a stack |file|, whose names are stored in a parallel stack
184 |file_name|. The boolean |changing| tells whether or not we're reading
185 from the |change_file|.
186
187 The line number of each open file is also kept for error reporting and
188 for the benefit of \.{CTANGLE}.
189
190 @<Global var...@>=
191 int include_depth; /* current level of nesting */
192 FILE *file[max_include_depth]; /* stack of non-change files */
193 FILE *change_file; /* change file */
194 char file_name[max_include_depth][max_file_name_length];
195 /* stack of non-change file names */
196 char change_file_name[max_file_name_length]; /* name of change file */
197 static char alt_web_file_name[max_file_name_length]; /* alternate name to try */
198 int line[max_include_depth]; /* number of current line in the stacked files */
199 int change_line; /* number of current line in change file */
200 int change_depth; /* where \.{@@y} originated during a change */
201 bool input_has_ended; /* if there is no more input */
202 bool changing; /* if the current line is from |change_file| */
203 bool web_file_open=false; /* if the web file is being read */
204
205 @ When |changing==false|, the next line of |change_file| is kept in
206 |change_buffer|, for purposes of comparison with the next
207 line of |cur_file|. After the change file has been completely input, we
208 set |change_limit=change_buffer|,
209 so that no further matches will be made.
210
211 Here's a shorthand expression for inequality between the two lines:
212
213 @d lines_dont_match (change_limit-change_buffer != limit-buffer @| ||
214 strncmp(buffer, change_buffer, (size_t)(limit-buffer)) != 0)
215
216 @<Global var...@>=
217 static char change_buffer[buf_size]; /* next line of |change_file| */
218 static char *change_limit; /* points to the last character in |change_buffer| */
219
220 @ Procedure |prime_the_change_buffer|
221 sets |change_buffer| in preparation for the next matching operation.
222 Since blank lines in the change file are not used for matching, we have
223 |(change_limit==change_buffer && !changing)| if and only if
224 the change file is exhausted. This procedure is called only when
225 |changing| is |true|; hence error messages will be reported correctly.
226
227 @c
228 static void
229 prime_the_change_buffer(void)
230 {
231 change_limit=change_buffer; /* this value is used if the change file ends */
232 @<Skip over comment lines in the change file; |return| if end of file@>@;
233 @<Skip to the next nonblank line; |return| if end of file@>@;
234 @<Move |buffer| and |limit| to |change_buffer| and |change_limit|@>@;
235 }
236
237 @ @<Predecl...@>=@+static void prime_the_change_buffer(void);
238
239 @ While looking for a line that begins with \.{@@x} in the change file, we
240 allow lines that begin with \.{@@}, as long as they don't begin with \.{@@y},
241 \.{@@z}, or \.{@@i} (which would probably mean that the change file is fouled up).
242
243 @<Skip over comment lines in the change file...@>=
244 while(true) {
245 change_line++;
246 if (!input_ln(change_file)) return;
247 if (limit<buffer+2) continue;
248 if (buffer[0]!='@@') continue;
249 if (xisupper(buffer[1])) buffer[1]=tolower((int)buffer[1]);
250 if (buffer[1]=='x') break;
251 if (buffer[1]=='y' || buffer[1]=='z' || buffer[1]=='i') {
252 loc=buffer+2;
253 err_print("! Missing @@x in change file");
254 @.Missing @@x...@>
255 }
256 }
257
258 @ Here we are looking at lines following the \.{@@x}.
259
260 @<Skip to the next nonblank line...@>=
261 do {
262 change_line++;
263 if (!input_ln(change_file)) {
264 err_print("! Change file ended after @@x");
265 @.Change file ended...@>
266 return;
267 }
268 } while (limit==buffer);
269
270 @ @<Move |buffer| and |limit| to |change_buffer| and |change_limit|@>=
271 change_limit=change_buffer+(ptrdiff_t)(limit-buffer);
272 strncpy(change_buffer,buffer,(size_t)(limit-buffer+1));
273
274 @ The following procedure is used to see if the next change entry should
275 go into effect; it is called only when |changing| is |false|.
276 The idea is to test whether or not the current
277 contents of |buffer| matches the current contents of |change_buffer|.
278 If not, there's nothing more to do; but if so, a change is called for:
279 All of the text down to the \.{@@y} is supposed to match. An error
280 message is issued if any discrepancy is found. Then the procedure
281 prepares to read the next line from |change_file|.
282
283 When a match is found, the current section is marked as changed unless
284 the first line after the \.{@@x} and after the \.{@@y} both start with
285 either `\.{@@*}' or `\.{@@\ }' (possibly preceded by whitespace).
286
287 This procedure is called only when |buffer<limit|, i.e., when the
288 current line is nonempty.
289
290 @d if_section_start_make_pending(b)@/
291 *limit='!';@+
292 for (loc=buffer;xisspace(*loc);loc++) ;@+
293 *limit=' ';@/
294 if (*loc=='@@' && (xisspace(*(loc+1)) || *(loc+1)=='*')) change_pending=b
295
296 @c
297 static void
298 check_change(void) /* switches to |change_file| if the buffers match */
299 {
300 int n=0; /* the number of discrepancies found */
301 if (lines_dont_match) return;
302 change_pending=false;
303 if (!changed_section[section_count]) {
304 if_section_start_make_pending(true);
305 if (!change_pending) changed_section[section_count]=true;
306 }
307 while (true) {
308 changing=print_where=true; change_line++;
309 if (!input_ln(change_file)) {
310 err_print("! Change file ended before @@y");
311 @.Change file ended...@>
312 change_limit=change_buffer; changing=false;
313 return;
314 }
315 if (limit>buffer+1 && buffer[0]=='@@') {
316 if (xisupper(buffer[1])) buffer[1]=tolower((int)buffer[1]);
317 @<If the current line starts with \.{@@y},
318 report any discrepancies and |return|@>@;
319 }
320 @<Move |buffer| and |limit|...@>@;
321 changing=false; cur_line++;
322 while (!input_ln(cur_file)) { /* pop the stack or quit */
323 if (include_depth==0) {
324 err_print("! CWEB file ended during a change");
325 @.CWEB file ended...@>
326 input_has_ended=true; return;
327 }
328 include_depth--; cur_line++;
329 }
330 if (lines_dont_match) n++;
331 }
332 }
333
334 @ @<Predecl...@>=@+static void check_change(void);
335
336 @ @<If the current line starts with \.{@@y}...@>=
337 if (buffer[1]=='x' || buffer[1]=='z') {
338 loc=buffer+2; err_print("! Where is the matching @@y?");
339 @.Where is the match...@>
340 }
341 else if (buffer[1]=='y') {
342 if (n>0) {
343 loc=buffer+2;
344 printf("\n! Hmm... %d ",n);
345 err_print("of the preceding lines failed to match");
346 @.Hmm... n of the preceding...@>
347 }
348 change_depth=include_depth;
349 return;
350 }
351
352 @ The |get_line| procedure is called when |loc>limit|; it puts the next
353 line of merged input into the buffer and updates the other variables
354 appropriately. A space is placed at the right end of the line.
355 This procedure returns |!input_has_ended| because we often want to
356 check the value of that variable after calling the procedure.
357
358 If we've just changed from the |cur_file| to the |change_file|, or if
359 the |cur_file| has changed, we tell \.{CTANGLE} to print this
360 information in the \CEE/ file by means of the |print_where| flag.
361
362 @c
363 bool get_line(void) /* inputs the next line */
364 {
365 restart:
366 if (changing && include_depth==change_depth)
367 @<Read from |change_file| and maybe turn off |changing|@>@;
368 if (! changing || include_depth>change_depth) {
369 @<Read from |cur_file| and maybe turn on |changing|@>@;
370 if (changing && include_depth==change_depth) goto restart;
371 }
372 if (input_has_ended) return false;
373 loc=buffer; *limit=' ';
374 if (buffer[0]=='@@' && (buffer[1]=='i' || buffer[1]=='I')) {
375 loc=buffer+2; *limit='"';
376 while (*loc==' '||*loc=='\t') loc++;
377 if (loc>=limit) {
378 err_print("! Include file name not given");
379 @.Include file name ...@>
380 goto restart;
381 }
382 if (include_depth>=max_include_depth-1) {
383 err_print("! Too many nested includes");
384 @.Too many nested includes@>
385 goto restart;
386 }
387 include_depth++; /* push input stack */
388 @<Try to open include file, abort push if unsuccessful, go to |restart|@>@;
389 }
390 return true;
391 }
392
393 @ When an \.{@@i} line is found in the |cur_file|, we must temporarily
394 stop reading it and start reading from the named include file. The
395 \.{@@i} line should give a complete file name with or without
396 double quotes.
397 If the environment variable |CWEBINPUTS| is set, or if the compiler flag
398 of the same name was defined at compile time,
399 \.{CWEB} will look for include files in the directory thus named, if
400 it cannot find them in the current directory.
401 (Colon-separated paths are not supported.)
402 The remainder of the \.{@@i} line after the file name is ignored.
403
404 @d too_long() {include_depth--;
405 err_print("! Include file name too long"); goto restart;}
406
407 @<Try to open...@>= {
408 char temp_file_name[max_file_name_length];
409 char *cur_file_name_end=cur_file_name+max_file_name_length-1;
410 char *kk, *k=cur_file_name;
411 size_t l; /* length of file name */
412
413 if (*loc=='"') {
414 loc++;
415 while (*loc!='"' && k<=cur_file_name_end) *k++=*loc++;
416 if (loc==limit) k=cur_file_name_end+1; /* unmatched quote is `too long' */
417 } else
418 while (*loc!=' '&&*loc!='\t'&&*loc!='"'&&k<=cur_file_name_end) *k++=*loc++;
419 if (k>cur_file_name_end) too_long();
420 @.Include file name ...@>
421 *k='\0';
422 if ((cur_file=fopen(cur_file_name,"r"))!=NULL) {
423 cur_line=0; print_where=true;
424 goto restart; /* success */
425 }
426 if ((kk=getenv("CWEBINPUTS"))!=NULL) {
427 if ((l=strlen(kk))>max_file_name_length-2) too_long();
428 strcpy(temp_file_name,kk);
429 }
430 else {
431 #ifdef CWEBINPUTS
432 if ((l=strlen(CWEBINPUTS))>max_file_name_length-2) too_long();
433 strcpy(temp_file_name,CWEBINPUTS);
434 #else
435 l=0;
436 #endif /* |CWEBINPUTS| */
437 }
438 if (l>0) {
439 if (k+l+2>=cur_file_name_end) too_long();
440 @.Include file name ...@>
441 for (; k>=cur_file_name; k--) *(k+l+1)=*k;
442 strcpy(cur_file_name,temp_file_name);
443 cur_file_name[l]='/'; /* \UNIX/ pathname separator */
444 @^system dependencies@>
445 if ((cur_file=fopen(cur_file_name,"r"))!=NULL) {
446 cur_line=0; print_where=true;
447 goto restart; /* success */
448 }
449 }
450 include_depth--; err_print("! Cannot open include file"); goto restart;
451 }
452
453 @ @<Read from |cur_file|...@>= {
454 cur_line++;
455 while (!input_ln(cur_file)) { /* pop the stack or quit */
456 print_where=true;
457 if (include_depth==0) {input_has_ended=true; break;}
458 else {
459 fclose(cur_file); include_depth--;
460 if (changing && include_depth==change_depth) break;
461 cur_line++;
462 }
463 }
464 if (!changing && !input_has_ended)
465 if (limit-buffer==change_limit-change_buffer)
466 if (buffer[0]==change_buffer[0])
467 if (change_limit>change_buffer) check_change();
468 }
469
470 @ @<Read from |change_file|...@>= {
471 change_line++;
472 if (!input_ln(change_file)) {
473 err_print("! Change file ended without @@z");
474 @.Change file ended...@>
475 buffer[0]='@@'; buffer[1]='z'; limit=buffer+2;
476 }
477 if (limit>buffer) { /* check if the change has ended */
478 if (change_pending) {
479 if_section_start_make_pending(false);
480 if (change_pending) {
481 changed_section[section_count]=true; change_pending=false;
482 }
483 }
484 *limit=' ';
485 if (buffer[0]=='@@') {
486 if (xisupper(buffer[1])) buffer[1]=tolower((int)buffer[1]);
487 if (buffer[1]=='x' || buffer[1]=='y') {
488 loc=buffer+2;
489 err_print("! Where is the matching @@z?");
490 @.Where is the match...@>
491 }
492 else if (buffer[1]=='z') {
493 prime_the_change_buffer(); changing=!changing; print_where=true;
494 }
495 }
496 }
497 }
498
499 @ At the end of the program, we will tell the user if the change file
500 had a line that didn't match any relevant line in |web_file|.
501
502 @c
503 void
504 check_complete(void) {
505 if (change_limit!=change_buffer) { /* |changing| is |false| */
506 strncpy(buffer,change_buffer,(size_t)(change_limit-change_buffer+1));
507 limit=buffer+(ptrdiff_t)(change_limit-change_buffer);
508 changing=true; change_depth=include_depth; loc=buffer;
509 err_print("! Change file entry did not match");
510 @.Change file entry did not match@>
511 }
512 }
513
514 @ The |reset_input| procedure, which gets \.{CWEB} ready to read the
515 user's \.{CWEB} input, is used at the beginning of phase one of \.{CTANGLE},
516 phases one and two of \.{CWEAVE}.
517
518 @c
519 void
520 reset_input(void)
521 {
522 limit=buffer; loc=buffer+1; buffer[0]=' ';
523 @<Open input files@>@;
524 include_depth=cur_line=change_line=0;
525 change_depth=include_depth;
526 changing=true; prime_the_change_buffer(); changing=!changing;
527 limit=buffer; loc=buffer+1; buffer[0]=' '; input_has_ended=false;
528 }
529
530 @ The following code opens the input files.
531 @^system dependencies@>
532
533 @<Open input files@>=
534 if ((web_file=fopen(web_file_name,"r"))==NULL) {
535 strcpy(web_file_name,alt_web_file_name);
536 if ((web_file=fopen(web_file_name,"r"))==NULL)
537 fatal("! Cannot open input file ", web_file_name);
538 }
539 @.Cannot open input file@>
540 @.Cannot open change file@>
541 web_file_open=true;
542 if ((change_file=fopen(change_file_name,"r"))==NULL)
543 fatal("! Cannot open change file ", change_file_name);
544
545 @ @<Global var...@>=
546 sixteen_bits section_count; /* the current section number */
547 bool changed_section[max_sections]; /* is the section changed? */
548 bool change_pending; /* if the current change is not yet recorded in
549 |changed_section[section_count]| */
550 bool print_where=false; /* should \.{CTANGLE} print line and file info? */
551
552 @* Storage of names and strings.
553 Both \.{CWEAVE} and \.{CTANGLE} store identifiers, section names and
554 other strings in a large array of |char|s, called |byte_mem|.
555 Information about the names is kept in the array |name_dir|, whose
556 elements are structures of type |name_info|, containing a pointer into
557 the |byte_mem| array (the address where the name begins) and other data.
558 A |name_pointer| variable is a pointer into |name_dir|. You find the
559 complete layout of |name_info| in the interface sections above.
560
561 The actual sequence of characters in the name pointed to by a |name_pointer
562 p| appears in positions |p->byte_start| to |(p+1)->byte_start-1|, inclusive.
563
564 The names of identifiers are found by computing a hash address |h| and
565 then looking at strings of bytes signified by the |name_pointer|s
566 |hash[h]|, |hash[h]->link|, |hash[h]->link->link|, \dots,
567 until either finding the desired name or encountering the null pointer.
568
569 The names of sections are stored in |byte_mem| together
570 with the identifier names, but a hash table is not used for them because
571 \.{CTANGLE} needs to be able to recognize a section name when given a prefix of
572 that name. A conventional binary search tree is used to retrieve section names,
573 with fields called |llink| and |rlink| (where |llink| takes the place
574 of |link|). The root of this tree is stored in |name_dir->rlink|;
575 this will be the only information in |name_dir[0]|.
576
577 Since the space used by |rlink| has a different function for
578 identifiers than for section names, we declare it as a |union|.
579
580 The last component of |name_info| is different for \.{CTANGLE} and
581 \.{CWEAVE}. In \.{CTANGLE}, if |p| is a pointer to a section name,
582 |p->equiv| is a pointer to its replacement text, an element of the
583 array |text_info|. In \.{CWEAVE}, on the other hand, if
584 |p| points to an identifier, |p->xref| is a pointer to its
585 list of cross-references, an element of the array |xmem|. The make-up
586 of |text_info| and |xmem| is discussed in the \.{CTANGLE} and \.{CWEAVE}
587 source files, respectively; here we just declare a common field
588 |equiv_or_xref| as a pointer to |void|.
589
590 @<Global var...@>=
591 char byte_mem[max_bytes]; /* characters of names */
592 char *byte_mem_end = byte_mem+max_bytes-1; /* end of |byte_mem| */
593 name_info name_dir[max_names]; /* information about names */
594 name_pointer name_dir_end = name_dir+max_names-1; /* end of |name_dir| */
595
596 @ The first unused position in |byte_mem| and |name_dir| is
597 kept in |byte_ptr| and |name_ptr|, respectively. Thus we
598 usually have |name_ptr->byte_start==byte_ptr|, and certainly
599 we want to keep |name_ptr<=name_dir_end| and |byte_ptr<=byte_mem_end|.
600
601 @<Global var...@>=
602 char *byte_ptr; /* first unused position in |byte_mem| */
603 name_pointer name_ptr; /* first unused position in |name_dir| */
604
605 @ @<Init...@>=
606 name_dir->byte_start=byte_ptr=byte_mem; /* position zero in both arrays */
607 name_ptr=name_dir+1; /* |name_dir[0]| will be used only for error recovery */
608 name_ptr->byte_start=byte_mem; /* this makes name 0 of length zero */
609 root=NULL; /* the binary search tree starts out with nothing in it */
610
611 @ The hash table itself
612 consists of |hash_size| entries of type |name_pointer|, and is
613 updated by the |id_lookup| procedure, which finds a given identifier
614 and returns the appropriate |name_pointer|. The matching is done by the
615 function |names_match|, which is slightly different in
616 \.{CWEAVE} and \.{CTANGLE}. If there is no match for the identifier,
617 it is inserted into the table.
618
619 @d hash_size 353 /* should be prime */
620
621 @<Global var...@>=
622 name_pointer hash[hash_size]={NULL}; /* heads of hash lists */
623 hash_pointer hash_end = hash+hash_size-1; /* end of |hash| */
624 hash_pointer hash_ptr; /* index into hash-head array */
625
626 @ Initially all the hash lists are empty.
627
628 @ Here is the main procedure for finding identifiers:
629
630 @c
631 name_pointer
632 id_lookup( /* looks up a string in the identifier table */
633 const char *first, /* first character of string */
634 const char *last, /* last character of string plus one */
635 eight_bits t) /* the |ilk|; used by \.{CWEAVE} only */
636 {
637 const char *i=first; /* position in |buffer| */
638 int h; /* hash code */
639 size_t l; /* length of the given identifier */
640 name_pointer p; /* where the identifier is being sought */
641 if (last==NULL) for (last=first; *last!='\0'; last++);
642 l=(size_t)(last-first); /* compute the length */
643 @<Compute the hash code |h|@>@;
644 @<Compute the name location |p|@>@;
645 if (p==name_ptr) @<Enter a new name into the table at position |p|@>@;
646 return p;
647 }
648
649 @ A simple hash code is used: If the sequence of
650 character codes is $c_1c_2\ldots c_n$, its hash value will be
651 $$(2^{n-1}c_1+2^{n-2}c_2+\cdots+c_n)\,\bmod\,|hash_size|.$$
652
653 @<Compute the hash...@>=
654 h=(int)((eight_bits)*i);
655 while (++i<last) h=(h+h+(int)((eight_bits)*i)) % hash_size;
656 @^high-bit character handling@>
657
658 @ If the identifier is new, it will be placed in position |p=name_ptr|,
659 otherwise |p| will point to its existing location.
660
661 @<Compute the name location...@>=
662 p=hash[h];
663 while (p && !names_match(p,first,l,t)) p=p->link;
664 if (p==NULL) {
665 p=name_ptr; /* the current identifier is new */
666 p->link=hash[h]; hash[h]=p; /* insert |p| at beginning of hash list */
667 }
668
669 @ The information associated with a new identifier must be initialized
670 in a slightly different way in \.{CWEAVE} than in \.{CTANGLE}.
671
672 @<Enter a new name...@>= {
673 if (byte_ptr+l>byte_mem_end) overflow("byte memory");
674 if (name_ptr>=name_dir_end) overflow("name");
675 strncpy(byte_ptr,first,l);
676 (++name_ptr)->byte_start=byte_ptr+=l;
677 if (program==cweave) p->ilk=t, init_node(p);
678 }
679
680 @ If |p| is a |name_pointer| variable, as we have seen,
681 |p->byte_start| is the beginning of the area where the name
682 corresponding to |p| is stored. However, if |p| refers to a section
683 name, the name may need to be stored in chunks, because it may
684 ``grow'': a prefix of the section name may be encountered before
685 the full name. Furthermore we need to know the length of the shortest
686 prefix of the name that was ever encountered.
687
688 We solve this problem by inserting two extra bytes at |p->byte_start|,
689 representing the length of the shortest prefix, when |p| is a
690 section name. Furthermore, the last byte of the name will be a blank
691 space if |p| is a prefix. In the latter case, the name pointer
692 |p+1| will allow us to access additional chunks of the name:
693 The second chunk will begin at the name pointer |(p+1)->link|,
694 and if it too is a prefix (ending with blank) its |link| will point
695 to additional chunks in the same way. Null links are represented by
696 |name_dir|.
697
698 @d first_chunk(p) ((p)->byte_start+2)
699 @d prefix_length(p) (size_t)((eight_bits)*((p)->byte_start)*256 +
700 (eight_bits)*((p)->byte_start+1))
701 @d set_prefix_length(p,m) (*((p)->byte_start)=(char)((m)/256),
702 *((p)->byte_start+1)=(char)((m)%256))
703
704 @c
705 void
706 print_section_name(
707 name_pointer p)
708 {
709 char *ss, *s = first_chunk(p);
710 name_pointer q = p+1;
711 while (p!=name_dir) {
712 ss = (p+1)->byte_start-1;
713 if (*ss==' ' && ss>=s) p=q->link,q=p;
714 else ss++,p=name_dir,q=NULL;
715 term_write(s,(size_t)(ss-s));
716 s = p->byte_start;
717 }
718 if (q) term_write("...",3); /* complete name not yet known */
719 }
720
721 @ @c
722 void
723 sprint_section_name(
724 char *dest,
725 name_pointer p)
726 {
727 char *ss, *s = first_chunk(p);
728 name_pointer q = p+1;
729 while (p!=name_dir) {
730 ss = (p+1)->byte_start-1;
731 if (*ss==' ' && ss>=s) p=q->link,q=p;
732 else ss++,p=name_dir;
733 strncpy(dest,s,(size_t)(ss-s)), dest+=ss-s;
734 s = p->byte_start;
735 }
736 *dest='\0';
737 }
738
739 @ @c
740 void
741 print_prefix_name(
742 name_pointer p)
743 {
744 char *s = first_chunk(p);
745 size_t l = prefix_length(p);
746 term_write(s,l);
747 if (s+l<(p+1)->byte_start) term_write("...",3);
748 }
749
750 @ When we compare two section names, we'll need a function analogous to
751 |strcmp|. But we do not assume the strings
752 are null-terminated, and we keep an eye open for prefixes and extensions.
753
754 @d less 0 /* the first name is lexicographically less than the second */
755 @d equal 1 /* the first name is equal to the second */
756 @d greater 2 /* the first name is lexicographically greater than the second */
757 @d prefix 3 /* the first name is a proper prefix of the second */
758 @d extension 4 /* the first name is a proper extension of the second */
759
760 @<Predecl...@>=
761 static int web_strcmp(char *,size_t,char *,size_t);@/
762 static name_pointer add_section_name(name_pointer,int,char *,char *,bool);@/
763 static void extend_section_name(name_pointer,char *,char *,bool);
764
765 @ @c
766 static int web_strcmp( /* fuller comparison than |strcmp| */
767 char *j, /* beginning of first string */
768 size_t j_len, /* length of first string */
769 char *k, /* beginning of second string */
770 size_t k_len) /* length of second string */
771 {
772 char *j1=j+j_len, *k1=k+k_len;
773 while (k<k1 && j<j1 && *j==*k) k++, j++;
774 if (k==k1) if (j==j1) return equal;
775 else return extension;
776 else if (j==j1) return prefix;
777 else if (*j<*k) return less;
778 else return greater;
779 }
780
781 @ Adding a section name to the tree is straightforward if we know its
782 parent and whether it's the |rlink| or |llink| of the parent. As a
783 special case, when the name is the first section being added, we set the
784 ``parent'' to |NULL|. When a section name is created, it has only one
785 chunk, which however may be just a prefix; the full name will
786 hopefully be unveiled later. Obviously, |prefix_length| starts
787 out as the length of the first chunk, though it may decrease later.
788
789 The information associated with a new node must be initialized
790 differently in \.{CWEAVE} and \.{CTANGLE}; hence the
791 |init_node| procedure, which is defined differently in \.{cweave.w}
792 and \.{ctangle.w}.
793
794 @c
795 static name_pointer
796 add_section_name( /* install a new node in the tree */
797 name_pointer par, /* parent of new node */
798 int c, /* right or left? */
799 char *first, /* first character of section name */
800 char *last, /* last character of section name, plus one */
801 bool ispref) /* are we adding a prefix or a full name? */
802 {
803 name_pointer p=name_ptr; /* new node */
804 char *s=first_chunk(p);
805 size_t name_len=(size_t)(last-first+(int)ispref); /* length of section name */
806 if (s+name_len>byte_mem_end) overflow("byte memory");
807 if (name_ptr+1>=name_dir_end) overflow("name");
808 (++name_ptr)->byte_start=byte_ptr=s+name_len;
809 if (ispref) {
810 *(byte_ptr-1)=' ';
811 name_len--;
812 name_ptr->link=name_dir;
813 (++name_ptr)->byte_start=byte_ptr;
814 }
815 set_prefix_length(p,name_len);
816 strncpy(s,first,name_len);
817 p->llink=p->rlink=NULL;
818 init_node(p);
819 return par==NULL ? (root=p) : c==less ? (par->llink=p) : (par->rlink=p);
820 }
821
822 @ @c
823 static void
824 extend_section_name(
825 name_pointer p, /* name to be extended */
826 char *first, /* beginning of extension text */
827 char *last, /* one beyond end of extension text */
828 bool ispref) /* are we adding a prefix or a full name? */
829 {
830 char *s;
831 name_pointer q=p+1;
832 size_t name_len=(size_t)(last-first+(int)ispref);
833 if (name_ptr>=name_dir_end) overflow("name");
834 while (q->link!=name_dir) q=q->link;
835 q->link=name_ptr;
836 s=name_ptr->byte_start;
837 name_ptr->link=name_dir;
838 if (s+name_len>byte_mem_end) overflow("byte memory");
839 (++name_ptr)->byte_start=byte_ptr=s+name_len;
840 strncpy(s,first,name_len);
841 if (ispref) *(byte_ptr-1)=' ';
842 }
843
844 @ The |section_lookup| procedure is supposed to find a
845 section name that matches a new name, installing the new name if
846 it doesn't match an existing one. The new name is the string
847 between |first| and |last|; a ``match'' means that the new name
848 exactly equals or is a prefix or extension of a name in the tree.
849
850 @c
851 name_pointer
852 section_lookup( /* find or install section name in tree */
853 char *first,char *last, /* first and last characters of new name */
854 bool ispref) /* is the new name a prefix or a full name? */
855 {
856 int c=less; /* comparison between two names; initialized so some compilers won't complain */
857 name_pointer p=root; /* current node of the search tree */
858 name_pointer q=NULL; /* another place to look in the tree */
859 name_pointer r=NULL; /* where a match has been found */
860 name_pointer par=NULL; /* parent of |p|, if |r| is |NULL|;
861 otherwise parent of |r| */
862 size_t name_len=(size_t)(last-first+1);
863 @<Look for matches for new name among shortest prefixes, complaining
864 if more than one is found@>@;
865 @<If no match found, add new name to tree@>@;
866 @<If one match found, check for compatibility and return match@>@;
867 }
868
869 @ A legal new name matches an existing section name if and only if it
870 matches the shortest prefix of that section name. Therefore we can
871 limit our search for matches to shortest prefixes, which eliminates
872 the need for chunk-chasing at this stage.
873
874 @<Look for matches for new name among...@>=
875 while (p) { /* compare shortest prefix of |p| with new name */
876 c=web_strcmp(first,name_len,first_chunk(p),prefix_length(p));
877 if (c==less || c==greater) { /* new name does not match |p| */
878 if (r==NULL) /* no previous matches have been found */
879 par=p;
880 p=(c==less?p->llink:p->rlink);
881 } else { /* new name matches |p| */
882 if (r!=NULL) { /* and also |r|: illegal */
883 printf("%s","\n! Ambiguous prefix: matches <");
884 @.Ambiguous prefix ... @>
885 print_prefix_name(p);
886 printf("%s",">\n and <");
887 print_prefix_name(r);
888 err_print(">");
889 return name_dir; /* the unsection */
890 }
891 r=p; /* remember match */
892 p=p->llink; /* try another */
893 q=r->rlink; /* we'll get back here if the new |p| doesn't match */
894 }
895 if (p==NULL)
896 p=q, q=NULL; /* |q| held the other branch of |r| */
897 }
898
899 @ @<If no match ...@>=
900 if (r==NULL) /* no matches were found */
901 return add_section_name(par,c,first,last+1,ispref);
902
903 @ Although error messages are given in anomalous cases, we do return the
904 unique best match when a discrepancy is found, because users often
905 change a title in one place while forgetting to change it elsewhere.
906
907 @<If one match found, check for compatibility and return match@>=
908 switch(section_name_cmp(&first,name_len,r)) {
909 /* compare all of |r| with new name */
910 case prefix:
911 if (!ispref) {
912 printf("%s","\n! New name is a prefix of <");
913 @.New name is a prefix...@>
914 print_section_name(r);
915 err_print(">");
916 }
917 else if (name_len<prefix_length(r)) set_prefix_length(r,name_len);
918 @=/* fall through */@>@;
919 case equal: break;
920 case extension: if (!ispref || first<=last)
921 extend_section_name(r,first,last+1,ispref);
922 break;
923 case bad_extension:
924 printf("%s","\n! New name extends <");
925 @.New name extends...@>
926 print_section_name(r);
927 err_print(">");
928 break;
929 default: /* no match: illegal */
930 printf("%s","\n! Section name incompatible with <");
931 @.Section name incompatible...@>
932 print_prefix_name(r);
933 printf("%s",">,\n which abbreviates <");
934 print_section_name(r);
935 err_print(">");
936 }
937 return r;
938
939 @ The return codes of |section_name_cmp|, which compares a string with
940 the full name of a section, are those of |web_strcmp| plus
941 |bad_extension|, used when the string is an extension of a
942 supposedly already complete section name. This function has a side
943 effect when the comparison string is an extension: It advances the
944 address of the first character of the string by an amount equal to
945 the length of the known part of the section name.
946
947 The name \.{@@<foo...@@>} should be an acceptable ``abbreviation''
948 for \.{@@<foo@@>}. If such an abbreviation comes after the complete
949 name, there's no trouble recognizing it. If it comes before the
950 complete name, we simply append a null chunk. This logic requires
951 us to regard \.{@@<foo...@@>} as an ``extension'' of itself.
952
953 @d bad_extension 5
954
955 @c
956 static int section_name_cmp(
957 char **pfirst, /* pointer to beginning of comparison string */
958 size_t len, /* length of string */
959 name_pointer r) /* section name being compared */
960 {
961 char *first=*pfirst; /* beginning of comparison string */
962 name_pointer q=r+1; /* access to subsequent chunks */
963 char *ss, *s=first_chunk(r);
964 int c=less; /* comparison */
965 bool ispref; /* is chunk |r| a prefix? */
966 while (true) {
967 ss=(r+1)->byte_start-1;
968 if (*ss==' ' && ss>=r->byte_start) ispref=true,q=q->link;
969 else ispref=false,ss++,q=name_dir;
970 switch(c=web_strcmp(first,len,s,(size_t)(ss-s))) {
971 case equal: if (q==name_dir)
972 if (ispref) {
973 *pfirst=first+(ptrdiff_t)(ss-s);
974 return extension; /* null extension */
975 } else return equal;
976 else return length(q)==0? equal: prefix;
977 case extension:
978 if (!ispref) return bad_extension;
979 first += ss-s;
980 if (q!=name_dir) {len -= (int)(ss-s); s=q->byte_start; r=q; continue;}
981 *pfirst=first; return extension;
982 default: return c;
983 }
984 }
985 }
986
987 @ @<Predec...@>=@+static int section_name_cmp(char **,size_t,name_pointer);
988
989 @* Reporting errors to the user.
990 A global variable called |history| will contain one of four values
991 at the end of every run: |spotless| means that no unusual messages were
992 printed; |harmless_message| means that a message of possible interest
993 was printed but no serious errors were detected; |error_message| means that
994 at least one error was found; |fatal_message| means that the program
995 terminated abnormally. The value of |history| does not influence the
996 behavior of the program; it is simply computed for the convenience
997 of systems that might want to use such information.
998
999 @<Global var...@>=
1000 int history=spotless; /* indicates how bad this run was */
1001
1002 @ The command `|err_print("! Error message")|' will report a syntax error to
1003 the user, by printing the error message at the beginning of a new line and
1004 then giving an indication of where the error was spotted in the source file.
1005 Note that no period follows the error message, since the error routine
1006 will automatically supply a period. A newline is automatically supplied
1007 if the string begins with |"!"|.
1008
1009 @c
1010 void
1011 err_print( /* prints `\..' and location of error message */
1012 const char *s)
1013 {
1014 printf(*s=='!' ? "\n%s" : "%s",s);
1015 if (web_file_open) @<Print error location based on input buffer@>@;
1016 update_terminal(); mark_error();
1017 }
1018
1019 @ The error locations can be indicated by using the global variables
1020 |loc|, |cur_line|, |cur_file_name| and |changing|,
1021 which tell respectively the first
1022 unlooked-at position in |buffer|, the current line number, the current
1023 file, and whether the current line is from |change_file| or |cur_file|.
1024 This routine should be modified on systems whose standard text editor
1025 has special line-numbering conventions.
1026 @^system dependencies@>
1027
1028 @<Print error location based on input buffer@>=
1029 {char *k,*l; /* pointers into |buffer| */
1030 if (changing && include_depth==change_depth && change_line>0)
1031 printf(". (l. %d of change file)\n", change_line);
1032 else if (cur_line>0) {
1033 if (include_depth==0) printf(". (l. %d)\n", cur_line);
1034 else printf(". (l. %d of include file %s)\n", cur_line, cur_file_name);
1035 }
1036 l= (loc>=limit? limit: loc);
1037 if (l>buffer) {
1038 for (k=buffer; k<l; k++)
1039 if (*k=='\t') putchar(' ');
1040 else putchar(*k); /* print the characters already read */
1041 new_line();
1042 for (k=buffer; k<l; k++) putchar(' '); /* space out the next line */
1043 }
1044 for (k=l; k<limit; k++) putchar(*k); /* print the part not yet read */
1045 if (*limit=='|') putchar('|'); /* end of \CEE/ text in section names */
1046 putchar(' '); /* to separate the message from future asterisks */
1047 }
1048
1049 @ When no recovery from some error has been provided, we have to wrap
1050 up and quit as graciously as possible. This is done by calling the
1051 function |wrap_up| at the end of the code.
1052
1053 \.{CTANGLE} and \.{CWEAVE} have their own notions about how to
1054 print the job statistics. See the function(s) |print_stats| in the
1055 interface above and in the index.
1056
1057 Some implementations may wish to pass the |history| value to the
1058 operating system so that it can be used to govern whether or not other
1059 programs are started. Here, for instance, we pass the operating system
1060 a status of |EXIT_SUCCESS| if and only if only harmless messages were printed.
1061 @^system dependencies@>
1062
1063 @c
1064 int wrap_up(void) {
1065 if (show_progress || show_happiness || history != spotless) new_line();
1066 if (show_stats)
1067 print_stats(); /* print statistics about memory usage */
1068 @<Print the job |history|@>@;
1069 if (history > harmless_message) return EXIT_FAILURE;
1070 else return EXIT_SUCCESS;
1071 }
1072
1073 @ @<Print the job |history|@>=
1074 switch (history) {
1075 case spotless:
1076 if (show_happiness) puts("(No errors were found.)"); break;
1077 case harmless_message:
1078 puts("(Did you see the warning message above?)"); break;
1079 case error_message:
1080 puts("(Pardon me, but I think I spotted something wrong.)"); break;
1081 case fatal_message: default:
1082 puts("(That was a fatal error, my friend.)");
1083 }
1084
1085 @ When there is no way to recover from an error, the |fatal| subroutine is
1086 invoked. This happens most often when |overflow| occurs.
1087
1088 The two parameters to |fatal| are strings that are essentially
1089 concatenated to print the final error message.
1090
1091 @c void
1092 fatal(
1093 const char *s,const char *t)
1094 {
1095 if (*s) printf("%s",s);
1096 err_print(t);
1097 history=fatal_message; exit(wrap_up());
1098 }
1099
1100 @ An overflow stop occurs if \.{CWEB}'s tables aren't large enough.
1101
1102 @c void
1103 overflow(
1104 const char *t)
1105 {
1106 printf("\n! Sorry, %s capacity exceeded",t); fatal("","");
1107 }
1108 @.Sorry, capacity exceeded@>
1109
1110 @ Sometimes the program's behavior is far different from what it should be,
1111 and \.{CWEB} prints an error message that is really for the \.{CWEB}
1112 maintenance person, not the user. In such cases the program says
1113 |confusion("indication of where we are")|.
1114
1115 @* Command line arguments.
1116 The user calls \.{CWEAVE} and \.{CTANGLE} with arguments on the command line.
1117 These are either file names or flags to be turned off (beginning with |"-"|)
1118 or flags to be turned on (beginning with |"+"|).
1119 The following globals are for communicating the user's desires to the rest
1120 of the program. The various file name variables contain strings with
1121 the names of those files. Most of the 128 flags are undefined but available
1122 for future extensions.
1123
1124 @<Global var...@>=
1125 int argc; /* copy of |ac| parameter to |main| */
1126 char **argv; /* copy of |av| parameter to |main| */
1127 char C_file_name[max_file_name_length]; /* name of |C_file| */
1128 char tex_file_name[max_file_name_length]; /* name of |tex_file| */
1129 char idx_file_name[max_file_name_length]; /* name of |idx_file| */
1130 char scn_file_name[max_file_name_length]; /* name of |scn_file| */
1131 bool flags[128]; /* an option for each 7-bit code */
1132
1133 @ The |flags| will be initially |false|. Some of them are set to~|true| before
1134 scanning the arguments; if additional flags are |true| by default they
1135 should be set before calling |common_init|.
1136
1137 @<Set the default options common to \.{CTANGLE} and \.{CWEAVE}@>=
1138 show_banner=show_happiness=show_progress=make_xrefs=true;
1139
1140 @ We now must look at the command line arguments and set the file names
1141 accordingly. At least one file name must be present: the \.{CWEB}
1142 file. It may have an extension, or it may omit the extension to get |".w"| or
1143 |".web"| added. The \TEX/ output file name is formed by replacing the \.{CWEB}
1144 file name extension by |".tex"|, and the \CEE/ file name by replacing
1145 the extension by |".c"|, after removing the directory name (if any).
1146
1147 If there is a second file name present among the arguments, it is the
1148 change file, again either with an extension or without one to get |".ch"|.
1149 An omitted change file argument means that |"/dev/null"| should be used,
1150 when no changes are desired.
1151 @^system dependencies@>
1152
1153 If there's a third file name, it will be the output file.
1154
1155 @c
1156 static void
1157 scan_args(void)
1158 {
1159 char *dot_pos; /* position of |'.'| in the argument */
1160 char *name_pos; /* file name beginning, sans directory */
1161 char *s; /* pointer for scanning strings */
1162 bool found_web=false,found_change=false,found_out=false;
1163 /* have these names been seen? */
1164
1165 strcpy(change_file_name,"/dev/null");
1166 while (--argc > 0) {
1167 if ((**(++argv)=='-'||**argv=='+')&&*(*argv+1)) @<Handle flag argument@>@;
1168 else {
1169 s=name_pos=*argv;@+dot_pos=NULL;
1170 while (*s)
1171 if (*s=='.') dot_pos=s++;
1172 else if (*s=='/') dot_pos=NULL,name_pos=++s;
1173 else s++;
1174 if (!found_web) @<Make
1175 |web_file_name|, |tex_file_name|, and |C_file_name|@>@;
1176 else if (!found_change) @<Make |change_file_name|@>@;
1177 else if (!found_out) @<Override |tex_file_name| and |C_file_name|@>@;
1178 else @<Print usage error message and quit@>@;
1179 }
1180 }
1181 if (!found_web) @<Print usage error message and quit@>@;
1182 }
1183
1184 @ @<Pred...@>=@+static void scan_args(void);
1185
1186 @ We use all of |*argv| for the |web_file_name| if there is a |'.'| in it,
1187 otherwise we add |".w"|. If this file can't be opened, we prepare an
1188 |alt_web_file_name| by adding |"web"| after the dot.
1189 The other file names come from adding other things
1190 after the dot. We must check that there is enough room in
1191 |web_file_name| and the other arrays for the argument.
1192
1193 @<Make |web_file_name|...@>=
1194 {
1195 if (s-*argv > max_file_name_length-5)
1196 @<Complain about argument length@>@;
1197 if (dot_pos==NULL)
1198 sprintf(web_file_name,"%s.w",*argv);
1199 else {
1200 strcpy(web_file_name,*argv);
1201 *dot_pos='\0'; /* string now ends where the dot was */
1202 }
1203 sprintf(alt_web_file_name,"%s.web",*argv);
1204 sprintf(tex_file_name,"%s.tex",name_pos); /* strip off directory name */
1205 if (make_xrefs) { /* indexes will be generated */
1206 sprintf(idx_file_name,"%s.idx",name_pos);
1207 sprintf(scn_file_name,"%s.scn",name_pos);
1208 }
1209 sprintf(C_file_name,"%s.c",name_pos);
1210 found_web=true;
1211 }
1212
1213 @ @<Make |change_file_name|...@>=
1214 {
1215 if (strcmp(*argv,"-")!=0) {
1216 if (s-*argv > max_file_name_length-4)
1217 @<Complain about argument length@>@;
1218 if (dot_pos==NULL)
1219 sprintf(change_file_name,"%s.ch",*argv);
1220 else strcpy(change_file_name,*argv);
1221 }
1222 found_change=true;
1223 }
1224
1225 @ @<Override...@>=
1226 {
1227 if (s-*argv > max_file_name_length-5)
1228 @<Complain about argument length@>@;
1229 if (dot_pos==NULL) {
1230 sprintf(tex_file_name,"%s.tex",*argv);
1231 sprintf(C_file_name,"%s.c",*argv);
1232 } else {
1233 strcpy(tex_file_name,*argv);
1234 strcpy(C_file_name,*argv);
1235 *dot_pos='\0'; /* string now ends where the dot was */
1236 }
1237 if (make_xrefs) { /* indexes will be generated */
1238 sprintf(idx_file_name,"%s.idx",*argv);
1239 sprintf(scn_file_name,"%s.scn",*argv);
1240 }
1241 found_out=true;
1242 }
1243
1244 @ @d flag_change (**argv!='-')
1245 @<Handle flag...@>=
1246 for(dot_pos=*argv+1;*dot_pos>'\0';dot_pos++)
1247 flags[(eight_bits)*dot_pos]=flag_change;
1248
1249 @ @<Print usage error message and quit@>=
1250 {
1251 if (program==ctangle)
1252 fatal(
1253 "! Usage: ctangle [options] webfile[.w] [{changefile[.ch]|-} [outfile[.c]]]\n"
1254 ,"");
1255 @.Usage:@>
1256 else fatal(
1257 "! Usage: cweave [options] webfile[.w] [{changefile[.ch]|-} [outfile[.tex]]]\n"
1258 ,"");
1259 }
1260
1261 @ @<Complain about arg...@>= fatal("! Filename too long\n", *argv);
1262 @.Filename too long@>
1263
1264 @* Output. Here is the code that opens the output file:
1265 @^system dependencies@>
1266
1267 @<Global var...@>=
1268 FILE *C_file; /* where output of \.{CTANGLE} goes */
1269 FILE *tex_file; /* where output of \.{CWEAVE} goes */
1270 FILE *idx_file; /* where index from \.{CWEAVE} goes */
1271 FILE *scn_file; /* where list of sections from \.{CWEAVE} goes */
1272 FILE *active_file; /* currently active file for \.{CWEAVE} output */
1273
1274 @ @<Scan arguments and open output files@>=
1275 scan_args();
1276 if (program==ctangle) {
1277 if ((C_file=fopen(C_file_name,"wb"))==NULL)
1278 fatal("! Cannot open output file ", C_file_name);
1279 @.Cannot open output file@>
1280 }
1281 else {
1282 if ((tex_file=fopen(tex_file_name,"wb"))==NULL)
1283 fatal("! Cannot open output file ", tex_file_name);
1284 }
1285
1286 @* Index.