Debian Codegraph @

texk/kpathsea/win32/mktextfm.c

raw
1 /* mktextfm.c
2
3 Copyright 2000, 2020 Akira Kakuto.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this library; if not, see <http://www.gnu.org/licenses/>.
17
18 Assumes the ljfour mode in mf.base .
19 Usage: mktextfm [--destdir DESTDIR] name
20 */
21
22 #include <kpathsea/kpathsea.h>
23 #ifndef __MINGW32__
24 #include <web2c/w2c/c-auto.h>
25 #endif
26 #include "mktex.h"
27
28 #define TBUF 512
29
30 char *progname;
31
32 static void
33 usage (void)
34 {
35 fprintf (stderr, "Usage: %s [--destdir destdir] name\n", progname);
36 return;
37 }
38
39 static void
40 version (void)
41 {
42 fprintf (stderr, "%s, (C version 1.6 --ak 2009-2020)\n", progname);
43 fprintf (stderr, KPSEVERSION WEB2CVERSION "\n");
44 return;
45 }
46
47 static void
48 help (void)
49 {
50 fprintf (stderr, "Usage (1): %s name\n", progname);
51 fprintf (stderr, " tfm is saved following TDS\n");
52 fprintf (stderr, "Usage (2): %s --destdir destdir name\n", progname);
53 fprintf (stderr, " destdir must be an absolute path of dir\n");
54 fprintf (stderr, "Usage (3): %s --version\n", progname);
55 fprintf (stderr, "Usage (4): %s --help\n", progname);
56
57 return;
58 }
59
60 static void
61 relmem (char **v)
62 {
63 int j;
64 for (j = 0; j < 4; j++)
65 free (v[j]);
66 return;
67 }
68
69 int
70 main (int ac, char **av)
71 {
72 char rbuff[TBUF];
73 char buff[TBUF];
74 char savebuff[TBUF];
75 char cmd[TBUF];
76 char mffile[TBUF];
77 char *arg[4];
78 static char execfile[TBUF];
79
80 char kpsedot[TBUF];
81 char currdir[TBUF];
82 char *tmp;
83 int cdrive, tdrive;
84
85 FILE *fr, *fw, *fnul;
86
87 int i, savo, savi;
88 char *p, *fp, *fpp;
89 int issetdest;
90 char fontname[TBUF];
91
92 char texbindir[TBUF];
93 char fullbin[TBUF];
94 char *extra_info;
95
96 kpse_set_program_name (av[0], NULL);
97 progname = kpse_program_name;
98
99 /*
100 * disable output_directory in mktextfm (2024/05/09)
101 */
102 xputenv("TEXMF_OUTPUT_DIRECTORY", "");
103
104 /*
105 * get tex binary dir
106 *
107 */
108 p = kpse_var_value("SELFAUTOLOC");
109 if(p == 0) {
110 fprintf(stderr, "I cannot get SELFAUTOLOC\n");
111 exit(100);
112 }
113 strcpy(texbindir, p);
114 free(p);
115 for(p=texbindir; *p; p++) {
116 if(*p == '/') *p = '\\';
117 }
118 *p = '\\';
119 *(p+1) = '\0';
120
121 tmp = getenv ("TEMP");
122 if (!tmp)
123 tmp = getenv ("TMP");
124 if (!tmp)
125 tmp = getenv ("TMPDIR");
126 if (!tmp) {
127 fprintf (stderr, "Please define TEMP | TMP | TMPDIR.\n");
128 return (100);
129 }
130
131 tmp = xstrdup(tmp);
132
133 for (fpp = tmp; *fpp; fpp++) {
134 if (IS_KANJI(fpp))
135 fpp++;
136 else if (*fpp == '\\')
137 *fpp = '/';
138 }
139 /*
140 issetdest = 0 : TDS
141 issetdest = 1 : user setting
142 issetdest = 2 : current directory
143 */
144
145 issetdest = 0;
146
147 if (ac < 2) {
148 usage ();
149 free(tmp);
150 return (100);
151 }
152
153 if ((!strcmp (av[1], "--version")) || (!strcmp (av[1], "-version"))) {
154 version ();
155 free(tmp);
156 return (100);
157 }
158
159 if ((!strcmp (av[1], "--help")) || (!strcmp (av[1], "-help"))) {
160 help ();
161 free(tmp);
162 return (100);
163 }
164
165 for (i = 0; i < 4; i++)
166 arg[i] = (char *) malloc (TBUF);
167
168 if ((!strcmp (av[1], "--destdir")) || (!strcmp (av[1], "-destdir"))) {
169 if (ac != 4) {
170 usage ();
171 relmem (arg);
172 free(tmp);
173 return (100);
174 }
175 issetdest = 1;
176 if (strlen(av[2]) > TBUF - 1 || strlen(av[3]) > TBUF - 150) {
177 fprintf (stderr, "Too long a string.\n");
178 return (100);
179 }
180 strcpy (buff, av[2]);
181 strcpy (fontname, av[3]);
182 for (p = buff; *p; p++) {
183 if (IS_KANJI(p))
184 p++;
185 else if (*p == '\\')
186 *p = '/';
187 }
188 } else {
189 if (strlen(av[1]) > TBUF - 150) {
190 fprintf (stderr, "Too long a string.\n");
191 return (100);
192 }
193 strcpy (fontname, av[1]);
194 }
195
196
197 /* fontname = font name
198 */
199 if ((p = strrchr (fontname, '.')))
200 *p = '\0';
201
202 /* mffile is METAFONT file name
203 */
204 strcpy (mffile, fontname);
205 strcat (mffile, ".mf");
206
207 if (!(fp = kpse_var_value ("MFINPUTS"))) {
208 fprintf (stderr, "Cannot get value of MFINPUTS\n");
209 relmem (arg);
210 free(tmp);
211 return (100);
212 }
213
214 free (fp);
215
216 xputenv("MKTEXMF", "1");
217 if (!(p = kpse_find_file (mffile, kpse_mf_format, 1))) {
218 fprintf (stderr, "Cannot find %s.\n", mffile);
219 relmem (arg);
220 free(tmp);
221 return (100);
222 }
223
224 fpp = _getcwd (currdir, TBUF);
225 if (!fpp) {
226 fprintf (stderr, "Failed to get current working directory.\n");
227 relmem (arg);
228 free(tmp);
229 return (100);
230 }
231 for (fpp = currdir; *fpp; fpp++) {
232 if (IS_KANJI(fpp))
233 fpp++;
234 else if (*fpp == '\\')
235 *fpp = '/';
236 }
237
238 i = (int)strlen (currdir);
239 if (currdir[i - 1] == '/')
240 currdir[i - 1] = '\0';
241 strcpy (kpsedot, "KPSE_DOT=.;");
242 strcat (kpsedot, currdir);
243 _putenv (kpsedot);
244
245 if ((p[0] == '.') && (p[1] == '/') && (issetdest != 1)) {
246 issetdest = 2;
247 strcpy (buff, currdir);
248 }
249
250 if (issetdest == 0) {
251 /* now path of ${name}.mf is in p */
252 strcpy (arg[0], "Dummy");
253 strcpy (arg[1], "tfm");
254 strcpy (arg[2], p);
255
256 if (!(p = getdestdir (3, arg))) {
257 fprintf (stderr, "Cannot get destination directory name.\n");
258 relmem (arg);
259 free(tmp);
260 return (100);
261 }
262 strcpy (buff, p);
263 }
264
265 /* Now buff is the destdir */
266 p = buff;
267
268 i = (int)strlen (p);
269
270 if (p[i - 1] != '/')
271 strcat (p, "/");
272 strcat (p, fontname);
273 strcat (p, ".tfm");
274
275 /* now p (or buff) is the full path name of the tfm font */
276 /* check if it exists */
277 if (_access (p, 0) == 0) {
278 fprintf (stderr, "%s exists\n", p);
279 printf ("%s\n", p);
280 relmem (arg);
281 free(tmp);
282 return (0);
283 }
284
285 cdrive = _getdrive ();
286 if (tmp[1] == ':') {
287 tdrive = tolower (*tmp) - 'a' + 1;
288 _chdrive (tdrive);
289 }
290 _chdir (tmp);
291
292 /* save stdout and stdin */
293 savo = _dup (fileno (stdout));
294 savi = _dup (fileno (stdin));
295
296 /* connect stdout to stderr */
297 _dup2 (fileno (stderr), fileno (stdout));
298
299 /* connect stdin to nul device */
300 if (!(fnul = fopen ("nul", "r"))) {
301 fprintf (stderr, "Cannot open nul device to read\n");
302 relmem (arg);
303 _chdrive (cdrive);
304 _chdir (currdir);
305 free(tmp);
306 return (100);
307 }
308 _dup2 (fileno (fnul), fileno (stdin));
309
310 /* METAFONT command line */
311 /*
312 The idea here is to provide a programmatic way to get the
313 codingscheme and other so-called Xerox-world information into the
314 tfm: if the envvar MF_MODE_EXTRA_INFO is set, then modes.mf (as of
315 the 3.9 release in January 2020) will arrange for that. We do not
316 do this by default because Knuth objected.
317 */
318 extra_info = getenv("MF_MODE_EXTRA_INFO");
319 strcpy (cmd, "--progname=mf --base=mf ");
320 strcat (cmd, "\\mode:=ljfour; mag:=1; ");
321 if (extra_info) {
322 strcat(cmd, "if known mode_include_extra_info_available: ");
323 strcat(cmd, "mode_include_extra_info fi; ");
324 }
325 strcat (cmd, "nonstopmode; input ");
326 strcat (cmd, fontname);
327 strcat (cmd, ";");
328
329 strcpy (execfile, "mf-nowin.exe");
330 fprintf (stderr, "%s %s\n", execfile, cmd);
331 strcpy(fullbin, texbindir);
332 strcat(fullbin, execfile);
333 (void) _spawnlp (_P_WAIT, fullbin, execfile, cmd, NULL);
334
335 /* return to original stdout and stdin */
336 _dup2 (savo, fileno (stdout));
337 close (savo);
338 _dup2 (savi, fileno (stdin));
339 close (savi);
340
341 /* close nul device */
342 fclose (fnul);
343
344 /* check consistency */
345 strcpy (cmd, fontname);
346 strcat (cmd, ".600gf");
347
348 if (_access (cmd, 0) == -1) {
349 fprintf (stderr, "METAFONT failed to make gf font.\n");
350 relmem (arg);
351 _chdrive (cdrive);
352 _chdir (currdir);
353 free(tmp);
354 return (100);
355 }
356
357 remove (cmd);
358
359 strcpy (cmd, fontname);
360 strcat (cmd, ".tfm");
361
362 /* copy the tfm file */
363 if (!(fr = fopen (cmd, "rb"))) {
364 fprintf (stderr, "Cannot open %s to read\n", cmd);
365 _chdrive (cdrive);
366 _chdir (currdir);
367 relmem (arg);
368 free(tmp);
369 return (100);
370 }
371 if (!(fw = fopen (buff, "wb"))) {
372 fprintf (stderr, "Cannot open %s to write\n", buff);
373 _chdrive (cdrive);
374 _chdir (currdir);
375 relmem (arg);
376 free(tmp);
377 return (100);
378 }
379
380 while ((i = (int)fread (rbuff, 1, TBUF, fr)))
381 fwrite (rbuff, 1, i, fw);
382 fclose (fr);
383 fclose (fw);
384
385 strcpy(savebuff, buff);
386
387 /*
388 copy log file into the current directory
389 in the case that issetdest == 2,
390 because feynmf package requires the
391 log file.
392 */
393
394 if(issetdest == 2) {
395 i = (int)strlen(buff);
396 if(i > 3) {
397 i -= 4;
398 buff[i] = '\0';
399 strcat(buff, ".log");
400 strcpy(cmd, fontname);
401 strcat(cmd, ".log");
402 if (!(fr = fopen (cmd, "rb"))) {
403 fprintf (stderr, "Cannot open %s to read\n", cmd);
404 _chdrive (cdrive);
405 _chdir (currdir);
406 relmem (arg);
407 free(tmp);
408 return (100);
409 }
410 if (!(fw = fopen (buff, "wb"))) {
411 fprintf (stderr, "Cannot open %s to write\n", buff);
412 _chdrive (cdrive);
413 _chdir (currdir);
414 relmem (arg);
415 free(tmp);
416 return (100);
417 }
418 while ((i = (int)fread (rbuff, 1, TBUF, fr)))
419 fwrite (rbuff, 1, i, fw);
420 fclose (fr);
421 fclose (fw);
422 }
423 }
424
425 relmem (arg);
426 if(issetdest != 2)
427 mktexupd (savebuff);
428
429 /* erase files */
430 strcpy (cmd, fontname);
431 strcat (cmd, ".log");
432 remove (cmd);
433
434 strcpy (cmd, fontname);
435 strcat (cmd, ".tfm");
436 remove (cmd);
437
438 _chdrive (cdrive);
439 _chdir (currdir);
440
441 /* send message to Kpathsea */
442 printf ("%s\n", savebuff);
443 free(tmp);
444
445 return (0);
446 }