1 % Changes to adapt CTIE to web2c.
2 % Copyright 2002,2003 Julian Gilbey
5 % This file is distributed WITHOUT ANY WARRANTY, express or implied.
7 % Permission is granted to make and distribute verbatim copies of this
8 % file provided that the copyright notice and this permission notice
9 % are preserved on all copies.
11 % Permission is granted to copy and distribute modified versions of this
12 % file under the conditions for verbatim copying, provided that the
13 % entire resulting derived work is distributed under the terms of a
14 % permission notice identical to this one.
16 % This file is based heavily on tie.ch by Olaf Weber to adapt tie.w to
17 % the web2c system and on comm-w2c.ch from the Web2C 7.4.5 distribution
18 % by Wlodek Bzyl and Olaf Weber.
20 @x l.19 Add macro definitions
21 \def\title{The CTIE processor}
23 \def\Kpathsea/{{\mc KPATHSEA\spacefactor1000}}
25 \def\title{The CTIE processor}
30 int argc; string *argv;
32 int main @,P2C(int, argc, string *, argv)
35 @x l.105 Set up kpathsea stuff
36 @<Initialise parameters@>;
38 @<Set up |PROGNAME| feature and initialize the search path mechanism@>;
39 @<Initialise parameters@>;
42 These are defined by kpathsea; we replace this by the path-searching
43 initialisation code taken almost verbatim from comm-w2c.ch.
45 @ We include the additional types |boolean| and |string|. \.{CTIE}
46 replaces the complex \.{TIE} character set handling (based on that of
47 the original \.{WEB} system) with the standard \.{CWEB} behaviour, and
48 so uses the |char| type for input and output.
57 @ The \.{ctie} program from the original \.{CTIE} package uses the
58 compile-time default directory or the value of the environment
59 variable \.{CWEBINPUTS} as an alternative place to be searched for
60 files, if they could not be found in the current directory.
62 This version uses the \Kpathsea/ mechanism for searching files.
63 The directories to be searched for come from three sources:
65 (a)~a user-set environment variable \.{CWEBINPUTS}
66 (overriden by \.{CWEBINPUTS\_ctie});\par
67 (b)~a line in \Kpathsea/ configuration file \.{texmf.cnf},\hfil\break
68 e.g. \.{CWEBINPUTS=.:$TEXMF/texmf/cweb//}
69 or \.{CWEBINPUTS.ctie=.:$TEXMF/texmf/cweb//};\hangindent=2\parindent\par
70 (c)~compile-time default directories \.{.:$TEXMF/texmf/cweb//}
71 (specified in \.{texmf.in}).
73 @d kpse_find_cweb(name) kpse_find_file(name, kpse_cweb_format, true)
75 @ The simple file searching is replaced by the `path searching'
76 mechanism that the \Kpathsea/ library provides.
78 We set |kpse_program_name| to |"ctie"|. This means if the variable
79 |CWEBINPUTS.ctie| is present in \.{texmf.cnf} (or |CWEBINPUTS_ctie| in
80 the environment) its value will be used as the search path for
81 filenames. This allows different flawors of \.{CTIE} to have
82 different search paths.
84 @<Set up |PROGNAME| feature and initialize the search path mechanism@>=
85 kpse_set_program_name(argv[0], "ctie");
87 @ We include the additional types |boolean| and |string|. \.{CTIE}
88 replaces the complex \.{TIE} character set handling (based on that of
89 the original \.{WEB} system) with the standard \.{CWEB} behaviour, and
90 so uses the |char| type for input and output.
92 The |kpathsea| library (version 3.4.5) defines the |true|, |false|,
93 |boolean| and |string| types in \.{kpathsea/types.h}, so we do not
94 actually need to define them here.
97 @x l.129 The kpathsea include files find the right header file for these.
98 @ We predeclare some standard string-handling functions here instead of
99 including their system header files, because the names of the header files
100 are not as standard as the names of the functions. (There's confusion
101 between \.{<string.h>} and \.{<strings.h>}.)
104 extern int strlen(); /* length of string */
105 extern char* strcpy(); /* copy one string to another */
106 extern int strncmp(); /* compare up to $n$ string characters */
107 extern char* strncpy(); /* copy up to $n$ string characters */
108 extern char *strerror();
110 @ We don't need to predeclare any string handling functions here, as
111 the \.{kpathsea} headers do the right thing.
115 @ And we need dynamic memory allocation.
116 This should cause no trouble in any \CEE/ program.
117 @^system dependencies@>
119 @<Global \&{\#include}s@>=
126 @ And we need dynamic memory allocation.
127 This should cause no trouble in any \CEE/ program.
128 The \.{kpathsea} include files handle the definition of |malloc()|,
130 @^system dependencies@>
133 @x l.284 way too short!
134 @d max_file_name_length 60
136 @d max_file_name_length 1024
140 boolean get_line(i, do_includes)
141 file_index i; boolean do_includes;
143 boolean get_line @,P2C(file_index, i, boolean, do_includes)
146 The next piece is simplified using the kpathsea kpse_find_file
150 If the environment variable \.{CWEBINPUTS} is set, or if the compiler flag
151 of the same name was defined at compile time,
152 \.{CWEB} will look for include files in the directory thus named, if
153 it cannot find them in the current directory.
154 (Colon-separated paths are not supported.)
156 We use the \Kpathsea/ library (in particular, the \.{CWEBINPUTS}
157 variable) to search for this file.
160 @x l.510 Don't need the same variables any longer
161 char temp_file_name[max_file_name_length];
164 int l; /* length of file name */
171 @x l.534 Replace with kpse_find_file
172 if ((new_inc->the_file=fopen(new_inc->file_name, "r"))!=NULL) {
174 fullname=kpse_find_cweb(new_inc->file_name);
176 new_inc->the_file=fopen(fullname, "r");
177 if (fullname!=NULL && new_inc->the_file!=NULL) {
181 @x l.539 And this part is replaced by kpse_find_file
182 kk=getenv("CWEBINPUTS");
184 if ((l=strlen(kk))>max_file_name_length-2) too_long();
185 strcpy(temp_file_name, kk);
189 if ((l=strlen(CWEBINPUTS))>max_file_name_length-2) too_long();
190 strcpy(temp_file_name, CWEBINPUTS);
193 #endif /* |CWEBINPUTS| */
196 if (k+l+2>=file_name_end) too_long();
197 for (; k>= new_inc->file_name; k--) *(k+l+1)=*k;
198 strcpy(new_inc->file_name, temp_file_name);
199 new_inc->file_name[l]='/'; /* \UNIX/ pathname separator */
200 if ((new_inc->the_file=fopen(new_inc->file_name, "r"))!=NULL) {
201 new_inc->parent=inp_desc->current_include; /* link it in */
202 inp_desc->current_include=new_inc;
203 goto restart; /* success */
209 @x l.565 slightly more useful error message
210 err_print(i, "! Cannot open include file");
214 err_print(i, "! Cannot open include file");
216 err_print(i, "! Cannot find include file");
223 void err_print @,P2H(file_index, char *);
227 void err_print(i, s) /* prints `\..' and location of error message */
228 file_index i; char *s;
230 void err_print @,P2C(file_index, i, char *, s)
231 /* prints `\..' and location of error message */
237 int wrap_up @,P1H(void)
243 int wrap_up @,P1H(void);
249 void pfatal_error @,P2H(char *, char *);
253 void pfatal_error(s, t)
256 void pfatal_error @,P2C(char *, s, char *, t)
259 @x l.747 Use the kpathsea library to do this
260 @ For the master file we start by reading its first line into the
261 buffer, if we could open it.
263 @<Get the master file started@>=
265 input_organisation[0]->the_file=
266 fopen(input_organisation[0]->file_name, "r");
268 if (input_organisation[0]->the_file==NULL)
269 pfatal_error("! Cannot open master file ",
270 input_organisation[0]->file_name);
271 @.Cannot open master file@>
273 @ For the master file we start by reading its first line into the
274 buffer, if we could open it. We use the \.{kpathsea} library to find
277 @<Get the master file started@>=
281 fullname = kpse_find_cweb(input_organisation[0]->file_name);
283 input_organisation[0]->the_file = fopen(fullname, "r");
285 if (fullname==NULL || input_organisation[0]->the_file==NULL) {
287 pfatal_error("! Cannot open master file ",
288 input_organisation[0]->file_name);
290 fatal_error(-1, "! Cannot find master file ",
291 input_organisation[0]->file_name);
295 @.Cannot open master file@>
296 @.Cannot find master file@>
300 @<Prepare the change files@>=
306 input_organisation[i]->the_file=
307 fopen(input_organisation[i]->file_name, "r");
308 if (input_organisation[i]->the_file==NULL)
309 pfatal_error("! Cannot open change file ",
310 input_organisation[i]->file_name);
311 @.Cannot open change file@>
313 @<Prepare the change files@>=
320 fullname = kpse_find_cweb(input_organisation[i]->file_name);
322 input_organisation[i]->the_file = fopen(fullname, "r");
324 if (fullname==NULL || input_organisation[i]->the_file==NULL) {
326 pfatal_error("! Cannot open change file ",
327 input_organisation[i]->file_name);
329 fatal_error(-1, "! Cannot find change file ",
330 input_organisation[i]->file_name);
334 @.Cannot open change file@>
335 @.Cannot find change file@>
339 boolean lines_dont_match(i, j)
342 boolean lines_dont_match @,P2C(file_index, i, file_index, j)
346 void init_change_file(i)
349 void init_change_file @,P1C(file_index, i)
356 void put_line @,P1C(file_index, j)
360 boolean e_of_ch_module(i)
363 boolean e_of_ch_module @,P1C(file_index, i)
367 boolean e_of_ch_preamble(i)
370 boolean e_of_ch_preamble @,P1C(file_index, i)
376 void usage_error @,P1H(void)
379 @x l.1119 Add Web2C version to banner string
380 printf("%s\n", banner); /* print a ``banner line'' */
383 extern KPSEDLL string kpathsea_version_string; /* from kpathsea/version.c */
384 printf("%s (%s)\n", banner, kpathsea_version_string); /* print a ``banner line'' */
390 void print_version_and_exit();
392 void usage_help @,P1H(void);
393 void print_version_and_exit @,P2H(string, string);
399 void usage_help @,P1H(void)
403 void print_version_and_exit(name, version)
404 string name, version;
406 printf ("%s %s\n", name, version);
408 puts ("Copyright (C) 2002,2003 Julian Gilbey.");
410 puts ("There is NO warranty. This is free software. See the source");
411 puts ("code of CTIE for redistribution conditions.");
416 void print_version_and_exit @,P2C(string, name, string, version)
418 extern KPSEDLL string kpathsea_version_string; /* from kpathsea/version.c */
419 printf ("%s %s\n", name, version);
420 puts (kpathsea_version_string);
422 puts ("Copyright (C) 2002,2003 Julian Gilbey.");
423 puts ("Kpathsea is copyright (C) 1999 Free Software Foundation, Inc.");
425 puts ("There is NO warranty. This is free software.");
426 puts ("Redistribution of this software is covered by the terms of");
427 puts ("both the CTIE copyright and the GNU General Public Licence.");
428 puts ("For more information about these matters, see the files");
429 puts ("named COPYING and the CTIE source.");
430 puts ("Primary authors of CTIE: Julian Gilbey.");
431 puts ("Kpathsea written by Karl Berry and others.\n");
438 @* System-dependent changes.
439 This section should be replaced, if necessary, by
440 changes to the program that are necessary to make \.{CTIE}
441 work at a particular installation. It is usually best to
442 design your change file so that all changes to previous
443 modules preserve the module numbering; then everybody's
444 version will be consistent with the printed program. More
445 extensive changes, which introduce new modules, can be
446 inserted here; then only the index itself will get a new
448 @^system dependencies@>
450 @* System-dependent changes.
451 We modify the program to use the \.{kpathsea} library.
452 We need to include the headers first.
454 @<Global \&{\#include}s@>=
455 #include <kpathsea/kpathsea.h>
457 @ A global variable for the file read mode.
459 @<Global variables@>=
460 string file_open_mode;