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.
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
11 \centerline{(Version 3.64)}
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
20 \centerline{(Version 3.64k)}
24 This change can not be applied when `tie' is used
25 (TOC file can not be typeset).
30 %\let\maybe=\iffalse % print only changed modules
35 We use the definition from `kpathsea/types.h':
37 typedef enum { false = 0, true = 1 } boolean;
39 Note that this definition also occurs in common.h.
41 typedef short boolean;
55 @<Initialize pointers@>;
57 @<Initialize pointers@>;
58 @<Set up |PROGNAME| feature and initialize the search path mechanism@>;
64 int input_ln(fp) /* copies a line into |buffer| or returns 0 */
65 FILE *fp; /* what file to read from */
67 int input_ln P1C(FILE *, fp) /* copies a line into |buffer| or returns 0 */
72 @x l.207 - max_file_name_length is way too small.
73 @d max_file_name_length 60
75 @d max_file_name_length 1024
78 @x l.221 - no alt_web_file_name needed.
79 char alt_web_file_name[max_file_name_length]; /* alternate name to try */
86 prime_the_change_buffer()
88 prime_the_change_buffer P1H(void)
94 check_change() /* switches to |change_file| if the buffers match */
96 check_change P1H(void) /* switches to |change_file| if the buffers match */
104 reset_input P1H(void)
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);
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);
126 if ((change_file=fopen(change_file_name,"r"))==NULL)
127 fatal("! Cannot open change file ", change_file_name);
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);
141 int get_line() /* inputs the next line */
143 int get_line P1H(void) /* inputs the next line */
149 #include <stdlib.h> /* declaration of |getenv| and |exit| */
151 #include <stdlib.h> /* declaration of |getenv| and |exit| */
154 #include <kpathsea/kpathsea.h> /* include every \Kpathsea/ header */
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
163 This version uses the \Kpathsea/ mechanism for searching files.
164 The directories to be searched for come from three sources:
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}).
175 @d kpse_find_cweb(name) kpse_find_file(name,kpse_cweb_format,true)
177 @ The simple file searching is replaced by `path searching' mechanism
178 that \Kpathsea/ library provides.
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.
186 FIXME: Not sure this is the best way to go about this.
188 @<Set up |PROGNAME| feature and initialize the search path mechanism@>=
189 kpse_set_program_name(argv[0], "cweb");
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 */
201 char *cur_file_name_end=cur_file_name+max_file_name_length-1;
202 char *k=cur_file_name;
206 if ((cur_file=fopen(cur_file_name,"r"))!=NULL) {
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);
217 Replaced by Kpathsea `kpse_find_file'
220 kk=getenv("CWEBINPUTS");
222 if ((l=strlen(kk))>max_file_name_length-2) too_long();
223 strcpy(temp_file_name,kk);
227 if ((l=strlen(CWEBINPUTS))>max_file_name_length-2) too_long();
228 strcpy(temp_file_name,CWEBINPUTS);
231 #endif /* |CWEBINPUTS| */
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 */
252 check_complete P1H(void) {
258 extern int names_match();
260 extern int names_match P4H(name_pointer, char*, int, char);
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 */
271 /* looks up a string in the identifier table */
272 id_lookup P3C(char*,first, char*,last, char,t)
280 extern void init_p P2C(name_pointer,p, char,t);
286 print_section_name(p)
289 print_section_name P1C(name_pointer, p)
295 sprint_section_name(dest,p)
299 sprint_section_name P2C(char*,dest, name_pointer,p)
308 print_prefix_name P1C(name_pointer,p)
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 */
318 /* fuller comparison than |strcmp| */
319 int web_strcmp P4C(char*,j, int,j_len, char*,k, int,k_len)
325 extern void init_node();
327 extern void init_node P1C(name_pointer,node);
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? */
340 /* install a new node in the tree */
341 add_section_name P5C(name_pointer,par, int,c, char*,first, char*,last,
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? */
354 extend_section_name P4C(name_pointer,p, char*,first, char*,last, int,ispref)
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? */
364 /* find or install section name in tree */
365 section_lookup P3C(char*,first, char*,last, int,ispref)
371 int section_name_cmp();
373 int section_name_cmp P3H(char**, int, name_pointer);
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 */
384 int section_name_cmp P3C(char**,pfirst, int,len, name_pointer,r)
392 void err_print P1H(char*);
398 err_print(s) /* prints `\..' and location of error message */
401 err_print P1C(char*,s) /* prints `\..' and location of error message */
408 extern void print_stats();
410 int wrap_up P1H(void);
411 extern void print_stats P1H(void);
419 int wrap_up P1H(void) {
425 void fatal(), overflow();
427 void fatal P2H(char*,char*);
428 void overflow(char*);
437 fatal P2C(char*,s, char*,t)
446 overflow P1C(char*,t)
452 the names of those files. Most of the 128 flags are undefined but available
453 for future extensions.
455 the names of those files. Most of the 128 flags are undefined but available
456 for future extensions.
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
477 @d show_happiness flags['h'] /* should lack of errors be announced? */
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? */
485 show_banner=show_happiness=show_progress=1;
487 show_banner=show_happiness=show_progress=1;
495 void scan_args P1H(void);
510 @x l.1282 - use a define for /dev/null
511 if (found_change<=0) strcpy(change_file_name,"/dev/null");
513 if (found_change<=0) strcpy(change_file_name,DEV_NULL);
516 @x l.1302 - no alt_web_file_name
517 sprintf(alt_web_file_name,"%s.web",*argv);
525 @ @<Handle flag...@>=
528 @ @<Handle flag...@>=
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@>;
541 if (sscanf(*argv+2,"%u",&kpathsea_debug)!=1) @<Print usage error...@>;
545 flags[*dot_pos]=flag_change;
547 flags[(unsigned char)*dot_pos]=flag_change;
553 if (program==ctangle)
555 "! Usage: ctangle [options] webfile[.w] [{changefile[.ch]|-} [outfile[.c]]]\n"
559 "! Usage: cweave [options] webfile[.w] [{changefile[.ch]|-} [outfile[.tex]]]\n"
562 if (program==ctangle) {
563 fprintf(stderr, "ctangle: Need one to three file arguments.\n");
566 fprintf(stderr, "cweave: Need one to three file arguments.\n");
574 FILE *active_file; /* currently active file for \.{CWEAVE} output */
576 FILE *active_file; /* currently active file for \.{CWEAVE} output */
577 char *found_filename; /* filename found by |kpse_find_file| */
581 Section 81. (removed)
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>}.)
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 */
601 @** System dependent changes.
603 @ Modules for dealing with help messages and version info.
605 @<Display help message and exit@>=
606 usagehelp(program==ctangle ? CTANGLEHELP : CWEAVEHELP, NULL);
609 @ Will have to change these if the version numbers change (ouch).
611 @d ctangle_banner "This is CTANGLE, Version 3.64"
612 @d cweave_banner "This is CWEAVE, Version 3.64"
614 @<Display version information and exit@>=
615 printversionandexit((program==ctangle ? ctangle_banner : cweave_banner),
616 "Silvio Levy and Donald E. Knuth", NULL, NULL);