1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
/* splitup -- take TeX or MF in C as a single stream on stdin,
and it produces several .c and .h files in the current directory
as its output.
$Id$
Tim Morgan September 19, 1987. */
#include "config.h"
#include <kpathsea/getopt.h>
#if defined (FATAL)
#undef FATAL
#endif
#define FATAL(str) do { \
fprintf (stderr, "%s: fatal: ", argv[0]); \
fputs (str, stderr); \
fputs (".\n", stderr); exit (1); } while (0)
#if defined (FATAL1)
#undef FATAL1
#endif
#define FATAL1(str, e1) do { \
fprintf (stderr, "%s: fatal: ", argv[0]); \
fprintf (stderr, str, e1); \
fputs (".\n", stderr); exit (1); } while (0)
#ifdef VMS
#define unlink delete
#endif
int filenumber = 0, ifdef_nesting = 0, lines_in_file = 0;
char *output_name = NULL;
boolean has_ini;
/* This used to be a fixed 2000, but since bibtex.c is almost 10000 lines
(200+K), we may as well decrease the number of split files we create.
Probably faster for the compiler, definitely faster for the linker,
simpler for the Makefiles, and generally better. Now we specify this
in 'convert'. */
long int max_lines;
/* Do we split out a separate *ini.c file? */
boolean do_ini;
/* Don't need long filenames, since we generate them all. */
char buffer[1024], tempfile[100], filename[100], ini_name[100];
FILE *out, *ini, *temp;
/*
* Read a line of input into the buffer, returning `false' on EOF.
* If the line is of the form "#ifdef INI...", we set "has_ini"
* `true' else `false'. We also keep up with the #ifdef/#endif nesting
* so we know when it's safe to finish writing the current file.
*/
static int
read_line (void)
{
if (fgets (buffer, sizeof (buffer), stdin) == NULL)
return false;
if (strncmp (buffer, "#ifdef", 6) == 0
|| strncmp (buffer, "#ifndef", 7) == 0)
{
++ifdef_nesting;
if (strncmp (&buffer[7], "INI", 3) == 0)
has_ini = true;
}
else if (strncmp (buffer, "#endif", 6) == 0)
--ifdef_nesting;
return true;
}
int
main (int argc, string *argv)
{
const_string coerce;
unsigned coerce_len;
int option;
while ((option = getopt(argc, argv, "il:")) != -1) {
switch (option) {
case 'i':
do_ini = true;
break;
case 'l':
max_lines = atoi(optarg);
if (max_lines <= 0)
FATAL("[-i] [-l lines] name");
break;
default:
FATAL("[-i] [-l lines] name");
break;
}
}
if (optind + 1 != argc)
FATAL("[-i] [-l lines] name");
output_name = argv[optind];
sprintf (filename, "%sd.h", output_name);
sprintf (tempfile, "%s.tmp", output_name);
out = xfopen (filename, FOPEN_W_MODE);
fputs ("#undef TRIP\n#undef TRAP\n", out);
/* We have only one binary that can do both ini stuff and vir stuff. */
fputs ("#define STAT\n#define INI\n", out);
if (STREQ (output_name, "mf")) {
fputs ("#define INIMF\n#define MF\n", out);
coerce = "mfcoerce.h";
} else if (STREQ (output_name, "tex")) {
fputs ("#define INITEX\n#define TeX\n", out);
coerce = "texcoerce.h";
} else if (STREQ (output_name, "aleph")) {
fputs ("#define INITEX\n#define TeX\n#define Aleph\n", out);
coerce = "alephcoerce.h";
} else if (STREQ (output_name, "etex")) {
fputs ("#define INITEX\n#define TeX\n#define eTeX\n", out);
coerce = "etexcoerce.h";
} else if (STREQ (output_name, "pdftex")) {
fputs ("#define INITEX\n#define TeX\n#define pdfTeX\n", out);
coerce = "pdftexcoerce.h";
} else if (STREQ (output_name, "ptex")) {
fputs ("#define INITEX\n#define TeX\n#define pTeX\n", out);
coerce = "ptexcoerce.h";
} else if (STREQ (output_name, "xetex")) {
fputs ("#define INITEX\n#define TeX\n#define XeTeX\n", out);
coerce = "xetexcoerce.h";
} else
FATAL1 ("Can only split mf, tex, aleph, etex, pdftex, ptex, or xetex,\n not %s", output_name);
coerce_len = strlen (coerce);
/* Read everything up to coerce.h. */
while (fgets (buffer, sizeof (buffer), stdin))
{
if (strncmp (&buffer[10], coerce, coerce_len) == 0)
break;
if (buffer[0] == '#' || buffer[0] == '\n' || buffer[0] == '}'
|| buffer[0] == '/' || buffer[0] == ' '
|| strncmp (buffer, "typedef", 7) == 0)
/*nothing */ ;
else
fputs ("EXTERN ", out);
fputs (buffer, out);
}
if (strncmp (&buffer[10], coerce, coerce_len) != 0)
FATAL1 ("No #include %s line", coerce);
fputs (buffer, out);
xfclose (out, filename);
if (do_ini) {
sprintf (ini_name, "%sini.c", output_name);
ini = xfopen (ini_name, FOPEN_W_MODE);
fputs ("#define EXTERN extern\n", ini);
fprintf (ini, "#include \"%sd.h\"\n\n", output_name);
}
sprintf (filename, "%s0.c", output_name);
out = xfopen (filename, FOPEN_W_MODE);
fputs ("#define EXTERN extern\n", out);
fprintf (out, "#include \"%sd.h\"\n\n", output_name);
do
{
/* Read one routine into a temp file */
has_ini = false;
temp = xfopen (tempfile, "w+");
while (read_line ())
{
fputs (buffer, temp);
if (buffer[0] == '}')
break; /* End of procedure */
}
while (ifdef_nesting > 0 && read_line ())
fputs (buffer, temp);
rewind (temp);
if (do_ini && has_ini)
{ /* Contained "#ifdef INI..." */
while (fgets (buffer, sizeof (buffer), temp))
fputs (buffer, ini);
}
else
{ /* Doesn't contain "#ifdef INI..." */
while (fgets (buffer, sizeof (buffer), temp))
{
fputs (buffer, out);
lines_in_file++;
}
}
xfclose (temp, tempfile);
/* Switch to new output file. */
if (max_lines && lines_in_file > max_lines)
{
xfclose (out, filename);
sprintf (filename, "%s%d.c", output_name, ++filenumber);
out = xfopen (filename, FOPEN_W_MODE);
fputs ("#define EXTERN extern\n", out);
fprintf (out, "#include \"%sd.h\"\n\n", output_name);
lines_in_file = 0;
}
}
while (!feof (stdin));
xfclose (out, filename);
if (lines_in_file == 0)
unlink (filename);
if (do_ini)
xfclose (ini, ini_name);
if (unlink (tempfile)) {
perror (tempfile);
exit (EXIT_FAILURE);
}
return EXIT_SUCCESS;
}
|