dvitomp fix from Akira
[mplib] / src / texk / web2c / cwebdir / comm-w2c.ch
1 % Kpathsea changes for CWEB by Wlodek Bzyl and Olaf Weber
2 % Copyright 2002 Wlodek Bzyl and Olaf Weber
3 % This file is in the Public Domain.
4
5 @x l.20
6 \def\title{Common code for CTANGLE and CWEAVE (Version 3.64)}
7 \def\topofcontents{\null\vfill
8   \centerline{\titlefont Common code for {\ttitlefont CTANGLE} and
9     {\ttitlefont CWEAVE}}
10   \vskip 15pt
11   \centerline{(Version 3.64)}
12   \vfill}
13 @y
14 \def\Kpathsea/{{\mc KPATHSEA\spacefactor1000}}
15 \def\title{Common code for CTANGLE and CWEAVE (Version 3.64k)}
16 \def\topofcontents{\null\vfill
17   \centerline{\titlefont Common code for {\ttitlefont CTANGLE} and
18     {\ttitlefont CWEAVE}}
19   \vskip 15pt
20   \centerline{(Version 3.64k)}
21   \vfill}
22 @z
23
24 This change can not be applied when `tie' is  used
25 (TOC file can not be typeset).
26
27 %@x l.42
28 %\let\maybe=\iftrue
29 %@y
30 %\let\maybe=\iffalse % print only changed modules
31 %@z
32
33
34 Section 2. 
35 We use the definition from `kpathsea/types.h':
36
37   typedef enum { false = 0, true = 1 } boolean;
38
39 Note that this definition also occurs in common.h.
40 @x l.74
41 typedef short boolean;
42 @y
43 @z
44
45
46 Section 4.
47
48 @x l.91
49 common_init()
50 @y
51 common_init P1H(void)
52 @z
53
54 @x l.93
55   @<Initialize pointers@>;
56 @y
57   @<Initialize pointers@>;
58   @<Set up |PROGNAME| feature and initialize the search path mechanism@>;
59 @z
60
61 Section 9.
62
63 @x l.173
64 int input_ln(fp) /* copies a line into |buffer| or returns 0 */
65 FILE *fp; /* what file to read from */
66 @y
67 int input_ln P1C(FILE *, fp) /* copies a line into |buffer| or returns 0 */
68 @z
69
70 Section 10.
71
72 @x l.207 - max_file_name_length is way too small.
73 @d max_file_name_length 60
74 @y
75 @d max_file_name_length 1024
76 @z
77
78 @x l.221 - no alt_web_file_name needed.
79 char alt_web_file_name[max_file_name_length]; /* alternate name to try */
80 @y
81 @z
82
83 Section 12.
84
85 @x l.254
86 prime_the_change_buffer()
87 @y
88 prime_the_change_buffer P1H(void)
89 @z
90
91 Section 16.
92
93 @x l.322
94 check_change() /* switches to |change_file| if the buffers match */
95 @y
96 check_change P1H(void) /* switches to |change_file| if the buffers match */
97 @z
98
99 Section 18.
100
101 @x l.380
102 reset_input()
103 @y
104 reset_input P1H(void)
105 @z
106
107 Section 19.
108
109 @x l.394
110 if ((web_file=fopen(web_file_name,"r"))==NULL) {
111   strcpy(web_file_name,alt_web_file_name);
112   if ((web_file=fopen(web_file_name,"r"))==NULL)
113        fatal("! Cannot open input file ", web_file_name);
114 }
115 @y
116 if ((found_filename=kpse_find_cweb(web_file_name))==NULL ||
117     (web_file=fopen(found_filename,"r"))==NULL) {
118   fatal("! Cannot open input file ", web_file_name);
119 } else if (strlen(found_filename) < max_file_name_length) {
120   strcpy(web_file_name, found_filename);
121   free(found_filename);
122 }
123 @z
124
125 @x l.402
126 if ((change_file=fopen(change_file_name,"r"))==NULL)
127        fatal("! Cannot open change file ", change_file_name);
128 @y
129 if ((found_filename=kpse_find_cweb(change_file_name))==NULL ||
130     (change_file=fopen(found_filename,"r"))==NULL) {
131   fatal("! Cannot open change file ", change_file_name);
132 } else if (strlen(found_filename) < max_file_name_length) {
133   strcpy(change_file_name, found_filename);
134   free(found_filename);
135 }
136 @z
137
138 Section 21.
139
140 @x l.427
141 int get_line() /* inputs the next line */
142 @y
143 int get_line P1H(void) /* inputs the next line */
144 @z
145
146 Section 22.
147
148 @x l.472
149 #include <stdlib.h> /* declaration of |getenv| and |exit| */
150 @y
151 #include <stdlib.h> /* declaration of |getenv| and |exit| */
152 #define CWEB
153 #include "cpascal.h"
154 #include <kpathsea/kpathsea.h> /* include every \Kpathsea/ header */
155 #include "help.h"
156
157 @ The \.{ctangle} and \.{cweave} programs from the original \.{CWEB}
158 package use the compile-time default directory or the value of the
159 environment variable \.{CWEBINPUTS} as an alternative place to be
160 searched for files, if they could not be found in the current
161 directory.
162
163 This version uses the \Kpathsea/ mechanism for searching files. 
164 The directories to be searched for come from three sources:
165
166  (a)~a user-set environment variable \.{CWEBINPUTS}
167     (overriden by \.{CWEBINPUTS\_cweb});\par
168  (b)~a line in \Kpathsea/ configuration file \.{texmf.cnf},\hfil\break
169     e.g. \.{CWEBINPUTS=.:$TEXMF/texmf/cweb//}
170     or \.{CWEBINPUTS.cweb=.:$TEXMF/texmf/cweb//};\hangindent=2\parindent\par
171  (c)~compile-time default directories \.{.:$TEXMF/texmf/cweb//}
172     (specified in \.{texmf.in}).
173
174
175 @d kpse_find_cweb(name) kpse_find_file(name,kpse_cweb_format,true)
176
177 @ The simple file searching is replaced by `path searching' mechanism
178 that \Kpathsea/ library provides.
179
180 We set |kpse_program_name| to a |"cweb"|.  This means if the
181 variable |CWEBINPUTS.cweb| is present in \.{texmf.cnf} (or |CWEBINPUTS_cweb|
182 in the environment) its value will be used as the search path for
183 filenames.  This allows different flawors of \.{CWEB} to have
184 different search paths.
185
186 FIXME: Not sure this is the best way to go about this.
187
188 @<Set up |PROGNAME| feature and initialize the search path mechanism@>=
189 kpse_set_program_name(argv[0], "cweb"); 
190 @z
191
192
193 Section 23.
194
195 @x l.475
196   char temp_file_name[max_file_name_length];
197   char *cur_file_name_end=cur_file_name+max_file_name_length-1;
198   char *k=cur_file_name, *kk;
199   int l; /* length of file name */
200 @y
201   char *cur_file_name_end=cur_file_name+max_file_name_length-1;
202   char *k=cur_file_name;
203 @z
204
205 @x l.489
206   if ((cur_file=fopen(cur_file_name,"r"))!=NULL) {
207 @y
208   if ((found_filename=kpse_find_cweb(cur_file_name))!=NULL &&
209       (cur_file=fopen(found_filename,"r"))!=NULL) {
210     /* Copy name for #line directives. */
211     if (strlen(found_filename) < max_file_name_length) {
212       strcpy(cur_file_name, found_filename);
213       free(found_filename);
214     }
215 @z
216
217 Replaced by Kpathsea `kpse_find_file'
218
219 @x l.493
220   kk=getenv("CWEBINPUTS");
221   if (kk!=NULL) {
222     if ((l=strlen(kk))>max_file_name_length-2) too_long();
223     strcpy(temp_file_name,kk);
224   }
225   else {
226 #ifdef CWEBINPUTS
227     if ((l=strlen(CWEBINPUTS))>max_file_name_length-2) too_long();
228     strcpy(temp_file_name,CWEBINPUTS);
229 #else
230     l=0;
231 #endif /* |CWEBINPUTS| */
232   }
233   if (l>0) {
234     if (k+l+2>=cur_file_name_end)  too_long();
235 @.Include file name ...@>
236     for (; k>= cur_file_name; k--) *(k+l+1)=*k;
237     strcpy(cur_file_name,temp_file_name);
238     cur_file_name[l]='/'; /* \UNIX/ pathname separator */
239     if ((cur_file=fopen(cur_file_name,"r"))!=NULL) {
240       cur_line=0; print_where=1;
241       goto restart; /* success */
242     }
243   }
244 @y
245 @z
246
247 Section 26.
248
249 @x l.571
250 check_complete(){
251 @y
252 check_complete P1H(void) {
253 @z
254
255 Section 33.
256
257 @x l.651
258 extern int names_match();
259 @y
260 extern int names_match P4H(name_pointer, char*, int, char);
261 @z
262
263 Section 35.
264
265 @x l.661
266 id_lookup(first,last,t) /* looks up a string in the identifier table */
267 char *first; /* first character of string */
268 char *last; /* last character of string plus one */
269 char t; /* the |ilk|; used by \.{CWEAVE} only */
270 @y
271 /* looks up a string in the identifier table */
272 id_lookup P3C(char*,first, char*,last, char,t)
273 @z
274
275 Section 38.
276
277 @x l.704
278 void init_p();
279 @y
280 extern void init_p P2C(name_pointer,p, char,t);
281 @z
282
283 Section 42.
284
285 @x l.766
286 print_section_name(p)
287 name_pointer p;
288 @y
289 print_section_name P1C(name_pointer, p)
290 @z
291
292 Section 43.
293
294 @x l.785
295 sprint_section_name(dest,p)
296   char*dest;
297   name_pointer p;
298 @y
299 sprint_section_name P2C(char*,dest, name_pointer,p)
300 @z
301
302 Section 44.
303
304 @x l.806
305 print_prefix_name(p)
306 name_pointer p;
307 @y
308 print_prefix_name P1C(name_pointer,p)
309 @z
310
311 Section 45.
312
313 @x l.826
314 int web_strcmp(j,j_len,k,k_len) /* fuller comparison than |strcmp| */
315   char *j, *k; /* beginning of first and second strings */
316   int j_len, k_len; /* length of strings */
317 @y
318 /* fuller comparison than |strcmp| */
319 int web_strcmp P4C(char*,j, int,j_len, char*,k, int,k_len)
320 @z
321
322 Section 46.
323
324 @x l.853
325 extern void init_node();
326 @y
327 extern void init_node P1C(name_pointer,node);
328 @z
329
330 Section 47.
331
332 @x l.857
333 add_section_name(par,c,first,last,ispref) /* install a new node in the tree */
334 name_pointer par; /* parent of new node */
335 int c; /* right or left? */
336 char *first; /* first character of section name */
337 char *last; /* last character of section name, plus one */
338 int ispref; /* are we adding a prefix or a full name? */
339 @y
340 /* install a new node in the tree */
341 add_section_name P5C(name_pointer,par, int,c, char*,first, char*,last,
342                      int,ispref)
343 @z
344
345 Section 48.
346
347 @x l.886
348 extend_section_name(p,first,last,ispref)
349 name_pointer p; /* name to be extended */
350 char *first; /* beginning of extension text */
351 char *last; /* one beyond end of extension text */
352 int ispref; /* are we adding a prefix or a full name? */
353 @y
354 extend_section_name P4C(name_pointer,p, char*,first, char*,last, int,ispref)
355 @z
356
357 Section 49.
358
359 @x l.914
360 section_lookup(first,last,ispref) /* find or install section name in tree */
361 char *first, *last; /* first and last characters of new name */
362 int ispref; /* is the new name a prefix or a full name? */
363 @y
364 /* find or install section name in tree */
365 section_lookup P3C(char*,first, char*,last, int,ispref)
366 @z
367
368 Section 53.
369
370 @x l.1018
371 int section_name_cmp();
372 @y
373 int section_name_cmp P3H(char**, int, name_pointer);
374 @z
375
376 Section 54.
377
378 @x l.1021
379 int section_name_cmp(pfirst,len,r)
380 char **pfirst; /* pointer to beginning of comparison string */
381 int len; /* length of string */
382 name_pointer r; /* section name being compared */
383 @y
384 int section_name_cmp P3C(char**,pfirst, int,len, name_pointer,r)
385 @z
386
387 Section 57.
388
389 @x l.1093
390 void  err_print();
391 @y
392 void  err_print P1H(char*);
393 @z
394
395 Section 58.
396
397 @x l.1098
398 err_print(s) /* prints `\..' and location of error message */
399 char *s;
400 @y
401 err_print P1C(char*,s) /* prints `\..' and location of error message */
402 @z
403
404 Section 60.
405
406 @x l.1141
407 int wrap_up();
408 extern void print_stats();
409 @y
410 int wrap_up P1H(void);
411 extern void print_stats P1H(void);
412 @z
413
414 Section 61.
415
416 @x l.1151
417 int wrap_up() {
418 @y
419 int wrap_up P1H(void) {
420 @z
421
422 Section 63.
423
424 @x l.1174
425 void fatal(), overflow();
426 @y
427 void fatal P2H(char*,char*);
428 void overflow(char*);
429 @z
430
431 Section 64.
432
433 @x l.1180
434 fatal(s,t)
435   char *s,*t;
436 @y
437 fatal P2C(char*,s, char*,t)
438 @z
439
440 Section 65.
441
442 @x l.1191
443 overflow(t)
444   char *t;
445 @y
446 overflow P1C(char*,t)
447 @z
448
449 Section 67.
450
451 @x l.1212
452 the names of those files. Most of the 128 flags are undefined but available
453 for future extensions.
454 @y
455 the names of those files. Most of the 128 flags are undefined but available
456 for future extensions.
457
458 We use `kpathsea' library functions to find literate sources and
459 NLS configuration files. When the files you expect are not
460 being found, the thing to do is to enable `kpathsea' runtime
461 debugging by assigning to |kpathsea_debug| variable a small number
462 via `\.{-d}' option. The meaning of number is shown below. To set
463 more than one debugging options sum the corresponding numbers.
464 $$\halign{\hskip5em\tt\hfil#&&\qquad\tt#\cr
465  1&report `\.{stat}' calls\cr
466  2&report lookups in all hash tables\cr
467  4&report file openings and closings\cr
468  8&report path information\cr
469 16&report directory list\cr
470 32&report on each file search\cr
471 64&report values of variables being looked up\cr}$$
472 Debugging output is always written to |stderr|, and begins with the string
473 `\.{kdebug:}'.
474 @z
475
476 @x l.1218
477 @d show_happiness flags['h'] /* should lack of errors be announced? */
478 @y
479 @d show_happiness flags['h'] /* should lack of errors be announced? */
480 @d show_kpathsea_debug flags['d']
481   /* should results of file searching be shown? */
482 @z
483
484 @x l.1234
485 show_banner=show_happiness=show_progress=1;
486 @y
487 show_banner=show_happiness=show_progress=1;
488 @z
489
490 Section 69.
491
492 @x l.1252
493 void scan_args();
494 @y
495 void scan_args P1H(void);
496 @z
497
498
499 Section 70.
500
501 @x l.1257
502 scan_args()
503 @y
504 scan_args P1H(void)
505 @z
506
507
508 Section 71.
509
510 @x l.1282 - use a define for /dev/null
511   if (found_change<=0) strcpy(change_file_name,"/dev/null");
512 @y
513   if (found_change<=0) strcpy(change_file_name,DEV_NULL);
514 @z
515
516 @x l.1302 - no alt_web_file_name
517   sprintf(alt_web_file_name,"%s.web",*argv);
518 @y
519 @z
520
521
522 Section 74.
523
524 @x l.1344
525 @ @<Handle flag...@>=
526 {
527 @y
528 @ @<Handle flag...@>=
529 {
530   if (strcmp("-help",*argv)==0 || strcmp("--help",*argv)==0)
531     @<Display help message and exit@>;
532   if (strcmp("-version",*argv)==0 || strcmp("--version",*argv)==0)
533     @<Display version information and exit@>;
534 @z
535
536 @x l.1347
537   else flag_change=1;
538 @y
539   else flag_change=1;
540   if (*(*argv+1)=='d')
541     if (sscanf(*argv+2,"%u",&kpathsea_debug)!=1) @<Print usage error...@>;
542 @z
543
544 @x l.1349
545     flags[*dot_pos]=flag_change;
546 @y
547     flags[(unsigned char)*dot_pos]=flag_change;
548 @z
549
550 Section 75.
551
552 @x l.1354
553 if (program==ctangle)
554   fatal(
555 "! Usage: ctangle [options] webfile[.w] [{changefile[.ch]|-} [outfile[.c]]]\n"
556    ,"");
557 @.Usage:@>
558 else fatal(
559 "! Usage: cweave [options] webfile[.w] [{changefile[.ch]|-} [outfile[.tex]]]\n"
560    ,"");
561 @y
562 if (program==ctangle) {
563   fprintf(stderr, "ctangle: Need one to three file arguments.\n");
564   usage("ctangle");
565 } else {
566   fprintf(stderr, "cweave: Need one to three file arguments.\n");
567   usage("cweave");
568 }
569 @z
570
571 Section 77.
572
573 @x l.1375
574 FILE *active_file; /* currently active file for \.{CWEAVE} output */
575 @y
576 FILE *active_file; /* currently active file for \.{CWEAVE} output */
577 char *found_filename; /* filename found by |kpse_find_file| */
578 @z
579
580
581 Section 81. (removed)
582
583 @x l.1403
584 @ We predeclare several standard system functions here instead of including
585 their system header files, because the names of the header files are not as
586 standard as the names of the functions. (For example, some \CEE/ environments
587 have \.{<string.h>} where others have \.{<strings.h>}.)
588
589 @<Predecl...@>=
590 extern int strlen(); /* length of string */
591 extern int strcmp(); /* compare strings lexicographically */
592 extern char* strcpy(); /* copy one string to another */
593 extern int strncmp(); /* compare up to $n$ string characters */
594 extern char* strncpy(); /* copy up to $n$ string characters */
595 @y
596 @z
597
598 @x
599 @** Index.
600 @y
601 @** System dependent changes.
602
603 @ Modules for dealing with help messages and version info.
604
605 @<Display help message and exit@>=
606 usagehelp(program==ctangle ? CTANGLEHELP : CWEAVEHELP, NULL);
607 @.--help@>
608
609 @ Will have to change these if the version numbers change (ouch).
610
611 @d ctangle_banner "This is CTANGLE, Version 3.64"
612 @d cweave_banner "This is CWEAVE, Version 3.64"
613
614 @<Display version information and exit@>=
615 printversionandexit((program==ctangle ? ctangle_banner : cweave_banner),
616   "Silvio Levy and Donald E. Knuth", NULL, NULL);
617 @.--version@>
618
619 @** Index.
620 @z