Debian Codegraph @

texk/web2c/ctiedir/ctie-k.ch

raw
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 [0] l.19 Add macro definitions
21 \def\title{The CTIE processor}
22 \def\botofcontents{%
23 \vfill
24 $\copyright$ 2002,2003 Julian Gilbey
25
26 All rights reserved.
27
28 This program is distributed WITHOUT ANY WARRANTY, express or implied.
29
30 Permission is granted to make and distribute verbatim copies of this
31 program provided that the copyright notice and this permission notice
32 are preserved on all copies.
33
34 Permission is granted to copy and distribute modified versions of this
35 program under the conditions for verbatim copying, provided that the
36 entire resulting derived work is distributed under the terms of a
37 permission notice identical to this one.
38 }
39 @y
40 \def\Kpathsea/{{\mc KPATHSEA\spacefactor1000}}
41 \let\maybe=\iftrue
42 \def\title{The CTIE processor}
43 \def\topofcontents{\null\vfill
44 \centerline{\titlefont The {\ttitlefont CTIE} processor}
45 \vskip 15pt
46 \centerline{(Version 1.1 [\TeX~Live])}
47 \vfill}
48 \def\covernote{\vbox{\ninerm\noindent
49 Copyright \copyright\ 2002, 2003 Julian Gilbey
50 \smallskip\noindent
51 All rights reserved.
52 \bigskip\noindent
53 This program is distributed WITHOUT ANY WARRANTY, express or implied.
54 \smallskip\noindent
55 Permission is granted to make and distribute verbatim copies of this
56 program provided that the copyright notice and this permission notice
57 are preserved on all copies.
58 \smallskip\noindent
59 Permission is granted to copy and distribute modified versions of this
60 program under the conditions for verbatim copying, provided that the
61 entire resulting derived work is distributed under the terms of a
62 permission notice identical to this one.
63 }}
64 \datecontentspage
65 @z
66
67 @x [1] l.81
68 @d copyright
69 "Copyright 2002,2003 Julian Gilbey. All rights reserved. There is no warranty.\n\
70 Run with the --version option for other important information."
71 @y
72 @d copyright
73 "Copyright 2002,2003 Julian Gilbey."@|
74 " All rights reserved. There is no warranty.\n"@|
75 "Run with the --version option for other important information."
76 @z
77
78 @x [3] l.102
79 main(argc, argv)
80 int argc; string *argv;
81 @y
82 int main (int argc, string *argv)
83 @z
84
85 @x [3] l.105 Set up kpathsea stuff
86 @<Initialise parameters@>;
87 @y
88 @<Set up |PROGNAME| feature and initialise the search path mechanism@>@;
89 @<Initialise parameters@>@;
90 @z
91
92 @x [3] l.107
93 @<Print the banners@>;
94 @y
95 @<Print the banners@>@;
96 @z
97
98 @x [4] l.116 boolean and string are defined by kpathsea.
99 @ We include the additional types |boolean| and |string|. \.{CTIE}
100 replaces the complex \.{TIE} character set handling (based on that of
101 the original \.{WEB} system) with the standard \.{CWEB} behaviour, and
102 so uses the |char| type for input and output.
103
104 @d false 0
105 @d true 1
106
107 @<Global types@>=
108 typedef int boolean;
109 typedef char* string;
110 @y
111 @ We include the additional types |boolean| and |string|. \.{CTIE}
112 replaces the complex \.{TIE} character set handling (based on that of
113 the original \.{WEB} system) with the standard \.{CWEB} behaviour, and
114 so uses the |char| type for input and output.
115
116 The \.{kpathsea} library (version 3.4.5 and higher) defines the |@!boolean|
117 (with the values |@!true| and |@!false|) and |@!string| (and |@!const_string|)
118 types in \.{<kpathsea/simpletypes.h>}, so we do not actually need to define
119 them here.
120
121 @s boolean int
122 @s string int
123 @s const_string int
124 @z
125
126 @x [5] l.129 The kpathsea include files find the right header file for these.
127 @ We predeclare some standard string-handling functions here instead of
128 including their system header files, because the names of the header files
129 are not as standard as the names of the functions. (There's confusion
130 between \.{<string.h>} and \.{<strings.h>}.)
131
132 @<Predecl...@>=
133 extern int strlen(); /* length of string */
134 extern char* strcpy(); /* copy one string to another */
135 extern int strncmp(); /* compare up to $n$ string characters */
136 extern char* strncpy(); /* copy up to $n$ string characters */
137 extern char *strerror();
138 @y
139 @ We don't need to predeclare any string handling functions here, as
140 the \.{kpathsea} headers do the right thing by including \.{<string.h>}
141 behind the scenes.
142 @z
143
144 @x [6] l.149
145 @d xisupper(c) (isupper(c)&&((unsigned char)c<0200))
146 @y
147 @d xisupper(c) (isupper((unsigned char)c)&&((unsigned char)c<0200))
148 @z
149
150 @x [7] l.155
151 This variable must be initialized.
152 @y
153 This variable must be initialised.
154 @z
155
156 @x [7] l.158
157 @d spotless 0
158 @d troublesome 1
159 @d fatal 2
160
161 @<Global variables@>=
162 int history=spotless;
163 @y
164 @<Global variables@>=
165 typedef enum {
166 @!spotless,
167 @!troublesome,
168 @!fatal } return_code;
169 static return_code history=spotless;
170 @z
171
172 @x [8] l.170 The kpathsea include files must be first.
173 predefined as we include the \.{stdio.h} definitions.
174
175 @<Global \&{\#include}s@>=
176 #include <stdio.h>
177 @y
178 predefined as we include the \.{<stdio.h>} definitions
179 through the \.{kpathsea} interface.
180
181 @<Global \&{\#include}s@>=
182 #include <kpathsea/kpathsea.h>
183 #include <w2c/config.h>
184 #include <lib/lib.h>
185 @z
186
187 @x [9] l.176 And this.
188 @ And we need dynamic memory allocation.
189 This should cause no trouble in any \CEE/ program.
190 @^system dependencies@>
191
192 @<Global \&{\#include}s@>=
193 #ifdef __STDC__
194 #include <stdlib.h>
195 #else
196 #include <malloc.h>
197 #endif
198 @y
199 @ And we need dynamic memory allocation.
200 This should cause no trouble in any \CEE/ program.
201 The \.{kpathsea} include files handle the definition of |@!malloc|, too.
202 @^system dependencies@>
203 @z
204
205 @x [10] l.190
206 files) are treated the same way. To organize the
207 @y
208 files) are treated the same way. To organise the
209 @z
210
211 Sections 10 and 11: use enum as requested in ctie.w
212
213 @x [10] l.204
214 \leavevmode |file_types| is used to describe whether a file
215 @y
216 \leavevmode \&{file\_types} is used to describe whether a file
217 @z
218
219 @x [10] l.208
220 the kind of output. (this would even be necessary if we
221 @y
222 the kind of output. (This would even be necessary if we
223 @z
224
225 @x [10] l.212
226 #define search 0
227 #define test 1
228 #define reading 2
229 #define ignore 3
230 typedef int in_file_modes; /* should be |enum(search, test, reading, ignore)| */
231 #define unknown 0
232 #define master 1
233 #define chf 2
234 typedef int file_types; /* should be |enum(unknown, master, chf)| */
235 @y
236 typedef enum {
237 @!search,
238 @!test,
239 @!reading,
240 @!ignore } in_file_modes;
241 typedef enum {
242 @!unknown,
243 @!master,
244 @!chf } file_types;
245 @z
246
247 @x [11] l.223
248 @ A variable of type |out_md_type| will tell us in what state the output
249 @y
250 @ A variable of type \&{out\_md\_type} will tell us in what state the output
251 @z
252
253 @x [11] l.230 dito
254 #define normal 0
255 #define pre 1
256 #define post 2
257 typedef int out_md_type; /* should be |enum(normal, pre, post)| */
258 @y
259 typedef enum {
260 @!normal,
261 @!pre,
262 @!post } out_md_type;
263 @z
264
265 @x [15] l.284 way too short!
266 @d max_file_name_length 60
267 @y
268 @d max_file_name_length 1024
269 @z
270
271 @x [19] l.329
272 boolean get_line(i, do_includes)
273 file_index i; boolean do_includes;
274 @y
275 static boolean
276 get_line (file_index i, boolean do_includes)
277 @z
278
279 @x [20] l.361
280 replacement part of a change file, or in an incomplerte check if the
281 @y
282 replacement part of a change file, or in an incomplete check if the
283 @z
284
285 @x [20] l.376 Handle input lines with CRLF
286 if ((*(k++) = c) != ' ') inp_desc->limit = k;
287 @y
288 if ((*(k++) = c) != ' ' && c != '\r') inp_desc->limit = k;
289 @z
290
291 @x [20] l.386
292 @<Increment the line number and print a progess report at
293 @y
294 @<Increment the line number and print a progress report at
295 @z
296
297 @x [24] l.436
298 if ((*(k++) = c) != ' ') inp_desc->limit = k;
299 @y
300 if ((*(k++) = c) != ' ' && c != '\r') inp_desc->limit = k;
301 @z
302
303 The next piece is simplified using the kpathsea kpse_find_file
304 function.
305
306 @x [27] l.497
307 If the environment variable \.{CWEBINPUTS} is set, or if the compiler flag
308 of the same name was defined at compile time,
309 \.{CWEB} will look for include files in the directory thus named, if
310 it cannot find them in the current directory.
311 (Colon-separated paths are not supported.)
312 @y
313 We use the \Kpathsea/ library (in particular, the \.{CWEBINPUTS}
314 variable) to search for this [email protected]@>
315 @z
316
317 @x [27] l.510 Don't need the same variables any longer
318 char temp_file_name[max_file_name_length];
319 char *file_name_end;
320 char *k, *kk;
321 int l; /* length of file name */
322 @y
323 char *file_name_end;
324 string fullname;
325 char *k;
326 @z
327
328 @x [27] l.534 Replace with kpse_find_file
329 if ((new_inc->the_file=fopen(new_inc->file_name, "r"))!=NULL) {
330 @y
331 if ((fullname=kpse_find_cweb(new_inc->file_name))!=NULL @|
332 && (new_inc->the_file=fopen(fullname, "r"))!=NULL) {
333 free(fullname);
334 @z
335
336 @x [27] l.539 And this part is replaced by kpse_find_file
337 kk=getenv("CWEBINPUTS");
338 if (kk!=NULL) {
339 if ((l=strlen(kk))>max_file_name_length-2) too_long();
340 strcpy(temp_file_name, kk);
341 }
342 else {
343 #ifdef CWEBINPUTS
344 if ((l=strlen(CWEBINPUTS))>max_file_name_length-2) too_long();
345 strcpy(temp_file_name, CWEBINPUTS);
346 #else
347 l=0;
348 #endif /* |CWEBINPUTS| */
349 }
350 if (l>0) {
351 if (k+l+2>=file_name_end) too_long();
352 for (; k>= new_inc->file_name; k--) *(k+l+1)=*k;
353 strcpy(new_inc->file_name, temp_file_name);
354 new_inc->file_name[l]='/'; /* \UNIX/ pathname separator */
355 if ((new_inc->the_file=fopen(new_inc->file_name, "r"))!=NULL) {
356 new_inc->parent=inp_desc->current_include; /* link it in */
357 inp_desc->current_include=new_inc;
358 goto restart; /* success */
359 }
360 }
361 @y
362 @z
363
364 @x [27] l.565 slightly more useful error message
365 err_print(i, "! Cannot open include file");
366 @y
367 if (fullname) {
368 free(fullname);
369 err_print(i, "! Cannot open include file");
370 } else
371 err_print(i, "! Cannot find include file");
372 @z
373
374
375 @x [28] l.585
376 void err_print();
377 @y
378 void err_print (file_index, const char *);
379 @z
380
381 @x [29] l.590
382 void err_print(i, s) /* prints `\..' and location of error message */
383 file_index i; char *s;
384 @y
385 void err_print (file_index i, const char *s)
386 /* prints `\..' and location of error message */
387 @z
388
389 @x [32] l.664
390 int wrap_up()
391 @y
392 int wrap_up (void)
393 @z
394
395 @x [32] l.667
396 if (history > spotless) return 1;
397 else return 0;
398 @y
399 if (history > spotless) return EXIT_FAILURE;
400 else return EXIT_SUCCESS;
401 @z
402
403 @x [33] l.674
404 int wrap_up();
405 @y
406 int wrap_up (void);
407 @z
408
409 @x [35] l.697
410 void pfatal_error();
411 @y
412 void pfatal_error (const char *, const char *);
413 @z
414
415 @x [36] l.700
416 void pfatal_error(s, t)
417 char *s, *t;
418 @y
419 void pfatal_error (const char *s, const char *t)
420 @z
421
422 @x [36] l.707
423 else fprintf(stderr, "\n");
424 @y
425 else putc('\n', stderr);
426 @z
427
428 @x [37] l.713
429 @ We need an include file for the above.
430
431 @<Global \&{\#include}s@>=
432 #include <errno.h>
433 @y
434 @ The \.{<errno.h>} include file for the above comes via the \.{kpathsea}
435 interface.
436 @z
437
438 @x [38] l.731 Use binary mode for output files
439 out_file=fopen(out_name, "w");
440 if (out_file==NULL) {
441 @y
442 if ((out_file=fopen(out_name, "wb"))==NULL) {
443 @z
444
445 @x [39] l.739
446 @ The name of the file and the file desciptor are stored in
447 @y
448 @ The name of the file and the file descriptor are stored in
449 @z
450
451 @x [40] l.747 Use the kpathsea library to do this
452 @ For the master file we start by reading its first line into the
453 buffer, if we could open it.
454
455 @<Get the master file started@>=
456 {
457 input_organisation[0]->the_file=
458 fopen(input_organisation[0]->file_name, "r");
459
460 if (input_organisation[0]->the_file==NULL)
461 pfatal_error("! Cannot open master file ",
462 input_organisation[0]->file_name);
463 @.Cannot open master file@>
464 @y
465 @ For the master file we start by reading its first line into the
466 buffer, if we could open it. We use the \.{kpathsea} library to find
467 the file.
468
469 @<Get the master file started@>=
470 {
471 string fullname;
472
473 if ((fullname = kpse_find_cweb(input_organisation[0]->file_name))!=NULL) {
474 if ((input_organisation[0]->the_file = fopen(fullname, "r"))==NULL)
475 pfatal_error("! Cannot open master file ",
476 input_organisation[0]->file_name);
477 free(fullname);
478 } @+ else {
479 fatal_error(-1, "! Cannot find master file ",
480 input_organisation[0]->file_name);
481 }
482 @.Cannot open master file@>
483 @.Cannot find master file@>
484 @z
485
486 @x [41] l.768 And this
487 @<Prepare the change files@>=
488 {
489 file_index i;
490
491 i=1;
492 while (i<no_ch) {
493 input_organisation[i]->the_file=
494 fopen(input_organisation[i]->file_name, "r");
495 if (input_organisation[i]->the_file==NULL)
496 pfatal_error("! Cannot open change file ",
497 input_organisation[i]->file_name);
498 @.Cannot open change file@>
499 @y
500 @<Prepare the change files@>=
501 {
502 file_index i;
503 string fullname;
504
505 i=1;
506 while (i<no_ch) {
507 if ((fullname = kpse_find_cweb(input_organisation[i]->file_name))
508 !=NULL) {
509 if ((input_organisation[i]->the_file = fopen(fullname, "r"))==NULL)
510 pfatal_error("! Cannot open change file ",
511 input_organisation[i]->file_name);
512 free(fullname);
513 } @+ else {
514 fatal_error(-1, "! Cannot find change file ",
515 input_organisation[i]->file_name);
516 }
517 @.Cannot open change file@>
518 @.Cannot find change file@>
519 @z
520
521 @x [42] l.792
522 boolean lines_dont_match(i, j)
523 file_index i, j;
524 @y
525 static boolean
526 lines_dont_match (file_index i, file_index j)
527 @z
528
529 @x [43] l.809
530 void init_change_file(i)
531 file_index i;
532 @y
533 static void
534 init_change_file (file_index i)
535 @z
536
537 @x [44] l.833
538 if (xisupper(ccode)) ccode=tolower(ccode);
539 @y
540 if (xisupper(ccode)) ccode=tolower((unsigned char)ccode);
541 @z
542
543 @x [46] l.858
544 void put_line(j)
545 file_index j;
546 @y
547 static void
548 put_line (file_index j)
549 @z
550
551 @x [47] l.873
552 boolean e_of_ch_module(i)
553 file_index i;
554 @y
555 static boolean
556 e_of_ch_module (file_index i)
557 @z
558
559 @x [48] l.894
560 boolean e_of_ch_preamble(i)
561 file_index i;
562 @y
563 static boolean
564 e_of_ch_preamble (file_index i)
565 @z
566
567 @x [57] l.1079
568 if (out_mode==post) /* last line has been changed */
569 fprintf(out_file, "@@z\n");
570 @y
571 if (out_mode==pre) /* last line has been deleted */
572 fprintf(out_file, "@@y\n"), out_mode=post;
573 if (out_mode==post) /* last line has been changed */
574 fprintf(out_file, "@@z\n");
575 @z
576
577 @x [59] l.1106
578 void usage_error()
579 {
580 @<Print the banners@>;
581 fprintf(stderr, "Usage: ctie -[mc] outfile master changefile(s)\n");
582 @y
583 static void
584 usage_error (void)
585 {
586 @<Print the banners@>;
587 fprintf(stderr, "Usage: ctie -m|-c outfile master changefile(s)\n");
588 @z
589
590 @x [59] l.1111
591 exit(1);
592 @y
593 exit(EXIT_FAILURE);
594 @z
595
596 @x [60] l.1119 Add Web2C version to banner string
597 printf("%s\n", banner); /* print a ``banner line'' */
598 @y
599 printf("%s%s\n", banner, versionstring); /* print a ``banner line'' */
600 @z
601
602 Section 63: use 'none' more than once.
603
604 @x [63] l.1169
605 (which is the case if |no_ch==(-1)|) or if the next element of
606 @y
607 (which is the case if |no_ch==none|) or if the next element of
608 @z
609
610 @x [63] l.1174
611 if (no_ch==(-1)) {
612 @y
613 if (no_ch==none) {
614 @z
615
616 @x [66] l.1218
617 string CTIEHELP[] = {
618 "Usage: ctie -[mc] outfile master changefile(s)",
619 @y
620 const_string CTIEHELP[] = {
621 "Usage: ctie -m|-c outfile master changefile(s)",
622 @z
623
624 @x [67] l.1233
625 void usage_help();
626 void print_version_and_exit();
627 @y
628 static void usage_help (void);
629 static void print_version_and_exit (const_string, const_string);
630 @z
631
632 @x [68] l.1238
633 void usage_help()
634 {
635 string *message=CTIEHELP;
636 @y
637 static void
638 usage_help (void)
639 {
640 const_string *message=CTIEHELP;
641 @z
642
643 @x [68] l.1248
644 exit(0);
645 @y
646 exit(EXIT_SUCCESS);
647 @z
648
649 @x [69] l.1253
650 void print_version_and_exit(name, version)
651 string name, version;
652 {
653 printf ("%s %s\n", name, version);
654
655 puts ("Copyright (C) 2002,2003 Julian Gilbey.");
656
657 puts ("There is NO warranty. This is free software. See the source");
658 puts ("code of CTIE for redistribution conditions.");
659
660 exit (0);
661 }
662 @y
663 static void
664 print_version_and_exit (const_string name, const_string version)
665 {
666 printf ("%s %s%s\n", name, version, versionstring);
667 puts (kpathsea_version_string);
668
669 puts ("Copyright (C) 2002,2003 Julian Gilbey.");
670 puts ("Kpathsea is copyright (C) 1999 Free Software Foundation, Inc.");
671
672 puts ("There is NO warranty. This is free software.");
673 puts ("Redistribution of this software is covered by the terms of");
674 puts ("both the CTIE copyright and the GNU General Public Licence.");
675 puts ("For more information about these matters, see the files");
676 puts ("named COPYING and the CTIE source.");
677 puts ("Primary author of CTIE: Julian Gilbey.");
678 puts ("Kpathsea written by Karl Berry and others.");
679
680 exit (EXIT_SUCCESS);
681 }
682 @z
683
684 We use the path-searching initialisation code
685 taken almost verbatim from comm-w2c.ch.
686
687 @x [70] l.1267
688 @* System-dependent changes.
689 This section should be replaced, if necessary, by
690 changes to the program that are necessary to make \.{CTIE}
691 work at a particular installation. It is usually best to
692 design your change file so that all changes to previous
693 modules preserve the module numbering; then everybody's
694 version will be consistent with the printed program. More
695 extensive changes, which introduce new modules, can be
696 inserted here; then only the index itself will get a new
697 module number.
698 @^system dependencies@>
699 @y
700 @* System-dependent changes.@^system dependencies@>@.CWEBINPUTS@>
701 The \.{ctie} program from the original \.{CTIE} package uses the
702 compile-time default directory or the value of the environment
703 variable \.{CWEBINPUTS} as an alternative place to be searched for
704 files, if they could not be found in the current directory.
705
706 This version uses the \Kpathsea/ mechanism for searching files.
707 The directories to be searched for come from three sources:
708 \smallskip
709 {\parindent5em
710 \item{(a)} a user-set environment variable \.{CWEBINPUTS}
711 (overridden by \.{CWEBINPUTS\_ctie});
712 \item{(b)} a line in \Kpathsea/ configuration file \.{texmf.cnf},\hfil\break
713 e.g., \.{CWEBINPUTS=\$TEXMFDOTDIR:\$TEXMF/texmf/cweb//}\hfil\break
714 or \.{CWEBINPUTS.ctie=\$TEXMFDOTDIR:\$TEXMF/texmf/cweb//};
715 \item{(c)} compile-time default directories (specified in
716 \.{texmf.in}),\hfil\break
717 i.e., \.{\$TEXMFDOTDIR:\$TEXMF/texmf/cweb//}.\par}
718
719 @d kpse_find_cweb(name) kpse_find_file(name, kpse_cweb_format, true)
720
721 @ The simple file searching is replaced by the `path searching'
722 mechanism that the \Kpathsea/ library provides.
723
724 We set |kpse_program_name| to `\.{ctie}'. This means if the variable
725 \.{CWEBINPUTS.ctie} is present in \.{texmf.cnf} (or \.{CWEBINPUTS\_ctie}
726 in the environment) its value will be used as the search path for
727 filenames. This allows different flavors of \.{CTIE} to have
728 different search [email protected]@>
729
730 @<Set up |PROGNAME| feature...@>=
731 kpse_set_program_name(argv[0], "ctie");
732 @z