OSDN Git Service

[lfsja-git] r11.1-69 対応。
[lfsbookja/lfsja-git.git] / ctie / ctie-k.ch
1 % Changes to adapt CTIE to web2c.
2 % Copyright 2002,2003 Julian Gilbey
3 % All rights reserved.
4 %
5 % This file is distributed WITHOUT ANY WARRANTY, express or implied.
6 %
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.
10 %
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.
15 %
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.
19
20 @x l.19 Add macro definitions
21 \def\title{The CTIE processor}
22 @y
23 \def\Kpathsea/{{\mc KPATHSEA\spacefactor1000}}
24
25 \def\title{The CTIE processor}
26 @z
27
28 @x l.102
29 main(argc, argv)
30         int argc; string *argv;
31 @y
32 int main @,P2C(int, argc, string *, argv)
33 @z
34
35 @x l.105 Set up kpathsea stuff
36     @<Initialise parameters@>;
37 @y
38     @<Set up |PROGNAME| feature and initialize the search path mechanism@>;
39     @<Initialise parameters@>;
40 @z
41
42 These are defined by kpathsea; we replace this by the path-searching
43 initialisation code taken almost verbatim from comm-w2c.ch.
44 @x l.116
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.
49
50 @d false 0
51 @d true 1
52
53 @<Global types@>=
54 typedef int boolean;
55 typedef char* string;
56 @y
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.
61
62 This version uses the \Kpathsea/ mechanism for searching files. 
63 The directories to be searched for come from three sources:
64
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}).
72
73 @d kpse_find_cweb(name) kpse_find_file(name, kpse_cweb_format, true)
74
75 @ The simple file searching is replaced by the `path searching'
76 mechanism that the \Kpathsea/ library provides.
77
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.
83
84 @<Set up |PROGNAME| feature and initialize the search path mechanism@>=
85 kpse_set_program_name(argv[0], "ctie");
86
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.
91
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.
95 @z
96
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>}.)
102
103 @<Predecl...@>=
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();
109 @y
110 @ We don't need to predeclare any string handling functions here, as
111 the \.{kpathsea} headers do the right thing.
112 @z
113
114 @x l.176 And this.
115 @ And we need dynamic memory allocation.
116 This should cause no trouble in any \CEE/ program.
117 @^system dependencies@>
118
119 @<Global \&{\#include}s@>=
120 #ifdef __STDC__
121 #include <stdlib.h>
122 #else
123 #include <malloc.h>
124 #endif
125 @y
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()|,
129 too.
130 @^system dependencies@>
131 @z
132
133 @x l.284 way too short!
134 @d max_file_name_length 60
135 @y
136 @d max_file_name_length 1024
137 @z
138
139 @x l.329
140 boolean get_line(i, do_includes)
141         file_index i; boolean do_includes;
142 @y
143 boolean get_line @,P2C(file_index, i, boolean, do_includes)
144 @z
145
146 The next piece is simplified using the kpathsea kpse_find_file
147 function.
148
149 @x l.497
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.)
155 @y
156 We use the \Kpathsea/ library (in particular, the \.{CWEBINPUTS}
157 variable) to search for this file.
158 @z
159
160 @x l.510 Don't need the same variables any longer
161     char temp_file_name[max_file_name_length]; 
162     char *file_name_end;
163     char *k, *kk;
164     int l; /* length of file name */
165 @y
166     char *file_name_end;
167     string fullname;
168     char *k;
169 @z
170
171 @x l.534 Replace with kpse_find_file
172     if ((new_inc->the_file=fopen(new_inc->file_name, "r"))!=NULL) {
173 @y
174     fullname=kpse_find_cweb(new_inc->file_name);
175     if (fullname)
176         new_inc->the_file=fopen(fullname, "r");
177     if (fullname!=NULL && new_inc->the_file!=NULL) {
178         free(fullname);
179 @z
180
181 @x l.539 And this part is replaced by kpse_find_file
182     kk=getenv("CWEBINPUTS");
183     if (kk!=NULL) {
184         if ((l=strlen(kk))>max_file_name_length-2) too_long();
185         strcpy(temp_file_name, kk);
186     }
187     else {
188 #ifdef CWEBINPUTS
189         if ((l=strlen(CWEBINPUTS))>max_file_name_length-2) too_long();
190         strcpy(temp_file_name, CWEBINPUTS);
191 #else
192         l=0; 
193 #endif /* |CWEBINPUTS| */
194     }
195     if (l>0) {
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 */
204         }
205     }
206 @y
207 @z
208
209 @x l.565 slightly more useful error message
210     err_print(i, "! Cannot open include file");
211 @y
212     if (fullname) {
213         free(fullname);
214         err_print(i, "! Cannot open include file");
215     } else
216         err_print(i, "! Cannot find include file");
217 @z
218
219
220 @x l.585
221 void err_print();
222 @y
223 void err_print @,P2H(file_index, char *);
224 @z
225
226 @x l.590
227 void err_print(i, s) /* prints `\..' and location of error message */
228 file_index i; char *s;
229 @y
230 void err_print @,P2C(file_index, i, char *, s)
231 /* prints `\..' and location of error message */
232 @z
233
234 @x l.664
235 int wrap_up()
236 @y
237 int wrap_up @,P1H(void)
238 @z
239
240 @x l.674
241 int wrap_up();
242 @y
243 int wrap_up @,P1H(void);
244 @z
245
246 @x l.697
247 void pfatal_error();
248 @y
249 void pfatal_error @,P2H(char *, char *);
250 @z
251
252 @x l.700
253 void pfatal_error(s, t)
254 char *s, *t;
255 @y
256 void pfatal_error @,P2C(char *, s, char *, t)
257 @z
258
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.
262
263 @<Get the master file started@>=
264 {
265     input_organisation[0]->the_file=
266         fopen(input_organisation[0]->file_name, "r");
267
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@>
272 @y
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
275 the file.
276
277 @<Get the master file started@>=
278 {
279     string fullname;
280
281     fullname = kpse_find_cweb(input_organisation[0]->file_name);
282     if (fullname)
283         input_organisation[0]->the_file = fopen(fullname, "r");
284
285     if (fullname==NULL || input_organisation[0]->the_file==NULL) {
286         if (fullname) {
287             pfatal_error("! Cannot open master file ",
288                 input_organisation[0]->file_name);
289         } else {
290             fatal_error(-1, "! Cannot find master file ",
291                 input_organisation[0]->file_name);
292         }
293     }
294     else free(fullname);
295 @.Cannot open master file@>
296 @.Cannot find master file@>
297 @z
298
299 @x l.768 And this
300 @<Prepare the change files@>=
301 {
302     file_index i;
303
304     i=1;
305     while (i<no_ch) {
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@>
312 @y
313 @<Prepare the change files@>=
314 {
315     file_index i;
316     string fullname;
317
318     i=1;
319     while (i<no_ch) {
320         fullname = kpse_find_cweb(input_organisation[i]->file_name);
321         if (fullname)
322             input_organisation[i]->the_file = fopen(fullname, "r");
323
324         if (fullname==NULL || input_organisation[i]->the_file==NULL) {
325             if (fullname) {
326                 pfatal_error("! Cannot open change file ",
327                     input_organisation[i]->file_name);
328             } else {
329                 fatal_error(-1, "! Cannot find change file ",
330                     input_organisation[i]->file_name);
331             }
332         }
333         else free(fullname);
334 @.Cannot open change file@>
335 @.Cannot find change file@>
336 @z
337
338 @x l.792
339 boolean lines_dont_match(i, j)
340         file_index i, j;
341 @y
342 boolean lines_dont_match @,P2C(file_index, i, file_index, j)
343 @z
344
345 @x l.809
346 void init_change_file(i)
347         file_index i;
348 @y
349 void init_change_file @,P1C(file_index, i)
350 @z
351
352 @x l.858
353 void put_line(j)
354        file_index j;
355 @y
356 void put_line @,P1C(file_index, j)
357 @z
358
359 @x l.873
360 boolean e_of_ch_module(i)
361         file_index i;
362 @y
363 boolean e_of_ch_module @,P1C(file_index, i)
364 @z
365
366 @x l.894
367 boolean e_of_ch_preamble(i)
368         file_index i;
369 @y
370 boolean e_of_ch_preamble @,P1C(file_index, i)
371 @z
372
373 @x l.1106
374 void usage_error()
375 @y
376 void usage_error @,P1H(void)
377 @z
378
379 @x l.1119 Add Web2C version to banner string
380 printf("%s\n", banner); /* print a ``banner line'' */
381 @y
382 {
383     extern KPSEDLL string kpathsea_version_string; /* from kpathsea/version.c */
384     printf("%s (%s)\n", banner, kpathsea_version_string); /* print a ``banner line'' */
385 }
386 @z
387
388 @x l.1233
389 void usage_help();
390 void print_version_and_exit();
391 @y
392 void usage_help @,P1H(void);
393 void print_version_and_exit @,P2H(string, string);
394 @z
395
396 @x l.1238
397 void usage_help()
398 @y
399 void usage_help @,P1H(void)
400 @z
401
402 @x l.1253
403 void print_version_and_exit(name, version)
404         string name, version;
405 {
406     printf ("%s %s\n", name, version);
407
408     puts ("Copyright (C) 2002,2003 Julian Gilbey.");
409
410     puts ("There is NO warranty.  This is free software.  See the source");
411     puts ("code of CTIE for redistribution conditions.");
412
413     exit (0);
414 }
415 @y
416 void print_version_and_exit @,P2C(string, name, string, version)
417 {
418     extern KPSEDLL string kpathsea_version_string; /* from kpathsea/version.c */
419     printf ("%s %s\n", name, version);
420     puts (kpathsea_version_string);
421
422     puts ("Copyright (C) 2002,2003 Julian Gilbey.");
423     puts ("Kpathsea is copyright (C) 1999 Free Software Foundation, Inc.");
424
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");
432
433     exit (0);
434 }
435 @z
436
437 @x l.1267
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
447 module number.
448 @^system dependencies@>
449 @y
450 @* System-dependent changes.
451 We modify the program to use the \.{kpathsea} library.
452 We need to include the headers first.
453
454 @<Global \&{\#include}s@>=
455 #include <kpathsea/kpathsea.h>
456
457 @ A global variable for the file read mode.
458
459 @<Global variables@>=
460 string file_open_mode;
461 @z