Debian Codegraph @

texk/kpathsea/tex-make.c

raw
1 /* tex-make.c: run external programs to make TeX-related files.
2
3 Copyright 1993, 1994, 1995, 1996, 1997, 2008-2023 Karl Berry.
4 Copyright 1997, 1998, 2001-05 Olaf Weber.
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with this library; if not, see <http://www.gnu.org/licenses/>. */
18
19 #include <kpathsea/config.h>
20
21 #include <kpathsea/absolute.h>
22 #include <kpathsea/c-fopen.h>
23 #include <kpathsea/c-pathch.h>
24 #include <kpathsea/db.h>
25 #include <kpathsea/fn.h>
26 #include <kpathsea/magstep.h>
27 #include <kpathsea/readable.h>
28 #include <kpathsea/tex-make.h>
29 #include <kpathsea/variable.h>
30
31 #if !defined (AMIGA) && !(defined (MSDOS) && !defined(__DJGPP__)) && !defined (WIN32)
32 #include <sys/wait.h>
33 #endif
34
35
36 /* We set the envvar MAKETEX_MAG, which is part of the default spec for
37 MakeTeXPK above, based on KPATHSEA_DPI and MAKETEX_BASE_DPI. */
38
39 static void
40 set_maketex_mag (kpathsea kpse)
41 {
42 char q[MAX_INT_LENGTH * 3 + 3];
43 int m;
44 string dpi_str = getenv ("KPATHSEA_DPI");
45 string bdpi_str = getenv ("MAKETEX_BASE_DPI");
46 unsigned dpi = dpi_str ? atoi (dpi_str) : 0;
47 unsigned bdpi = bdpi_str ? atoi (bdpi_str) : 0;
48
49 /* If the environment variables aren't set, it's a bug. */
50 assert (dpi != 0 && bdpi != 0);
51
52 /* Fix up for roundoff error. Hopefully the driver has already fixed
53 up DPI, but may as well be safe, and also get the magstep number. */
54 (void) kpathsea_magstep_fix (kpse, dpi, bdpi, &m);
55
56 if (m == 0) {
57 if (bdpi <= 4000) {
58 sprintf(q, "%u+%u/%u", dpi / bdpi, dpi % bdpi, bdpi);
59 } else {
60 unsigned f = bdpi/4000;
61 unsigned r = bdpi%4000;
62
63 if (f > 1) {
64 if (r > 0) {
65 sprintf(q, "%u+%u/(%u*%u+%u)",
66 dpi/bdpi, dpi%bdpi, f, (bdpi - r)/f, r);
67 } else {
68 sprintf(q, "%u+%u/(%u*%u)", dpi/bdpi, dpi%bdpi, f, bdpi/f);
69 }
70 } else {
71 sprintf(q, "%u+%u/(4000+%u)", dpi/bdpi, dpi%bdpi, r);
72 }
73 }
74 } else {
75 /* m is encoded with LSB being a ``half'' bit (see magstep.h). Are
76 we making an assumption here about two's complement? Probably.
77 In any case, if m is negative, we have to put in the sign
78 explicitly, since m/2==0 if m==-1. */
79 const_string sign = "";
80 if (m < 0) {
81 m *= -1;
82 sign = "-";
83 }
84 sprintf(q, "magstep\\(%s%d.%d\\)", sign, m / 2, (m & 1) * 5);
85 }
86 kpathsea_xputenv (kpse, "MAKETEX_MAG", q);
87 }
88
89 /* This mktex... program was disabled, or the script failed. If this
90 was a font creation (according to FORMAT), append CMD
91 to a file missfont.log in the current directory. */
92
93 static void
94 misstex (kpathsea kpse, kpse_file_format_type format, string *args)
95 {
96 string *s;
97
98 /* If we weren't trying to make a font, do nothing. Maybe should
99 allow people to specify what they want recorded? */
100 if (format != kpse_gf_format
101 && format != kpse_pk_format
102 && format != kpse_any_glyph_format
103 && format != kpse_tfm_format
104 && format != kpse_vf_format)
105 return;
106
107 /* If this is the first time, have to open the log file. But don't
108 bother logging anything if they were discarding errors. */
109 if (!kpse->missfont && !kpse->make_tex_discard_errors) {
110 string first_name;
111 const_string missfont_name = kpathsea_var_value (kpse, "MISSFONT_LOG");
112
113 if (!missfont_name || *missfont_name == '1') {
114 missfont_name = "missfont.log"; /* take default name */
115 } else if (missfont_name
116 && (*missfont_name == 0 || *missfont_name == '0')) {
117 missfont_name = NULL; /* user requested no missfont.log */
118 } /* else use user's name */
119
120 /* If the given name is not already absolute,
121 and TEXMF_OUTPUT_DIRECTORY is set, use that instead of cwd.
122 This is only an envvar, not a config file value. */
123 if (!kpathsea_absolute_p (kpse, missfont_name, false)
124 && getenv ("TEXMF_OUTPUT_DIRECTORY")) {
125 first_name = concat3 (getenv ("TEXMF_OUTPUT_DIRECTORY"),
126 DIR_SEP_STRING, missfont_name);
127 } else {
128 first_name = xstrdup (missfont_name);
129 }
130 kpse->missfont
131 = missfont_name ? fopen (first_name, FOPEN_A_MODE) : NULL;
132
133 /* If that succeeded, save the name for output below. */
134 if (kpse->missfont) {
135 missfont_name = first_name;
136
137 /* If that failed, try TEXMFOUTPUT. */
138 } else if (kpathsea_var_value (kpse, "TEXMFOUTPUT")) {
139 missfont_name = concat3 (kpathsea_var_value (kpse, "TEXMFOUTPUT"),
140 DIR_SEP_STRING, missfont_name);
141 kpse->missfont = fopen (missfont_name, FOPEN_A_MODE);
142 }
143
144 if (kpse->missfont)
145 fprintf (stderr, "kpathsea: Appending font creation commands to %s.\n",
146 missfont_name);
147
148 /* Uselessly save memory. */
149 free (first_name);
150 }
151
152 /* Write the command if we have a log file. */
153 if (kpse->missfont) {
154 fputs (args[0], kpse->missfont);
155 for (s = &args[1]; *s != NULL; s++) {
156 putc(' ', kpse->missfont);
157 fputs (*s, kpse->missfont);
158 }
159 putc ('\n', kpse->missfont);
160 }
161 }
162
163
164 /* Assume the script outputs the filename it creates (and nothing
165 else) on standard output; hence, we run the script with `popen'. */
166
167 static string
168 maketex (kpathsea kpse, kpse_file_format_type format, string* args)
169 {
170 /* New implementation, use fork/exec pair instead of popen, since
171 * the latter is virtually impossible to make safe.
172 */
173 unsigned len;
174 string *s;
175 string ret = NULL;
176 string fn;
177 #if defined(WIN32)
178 char fullbin[256], *wrp;
179
180 wrp = kpathsea_var_value(kpse, "SELFAUTOLOC");
181 if(wrp == NULL) {
182 fprintf(stderr, "I cannot get SELFAUTOLOC\n");
183 exit(100);
184 }
185
186 strcpy(fullbin, wrp);
187 free(wrp);
188 for(wrp=fullbin; *wrp; wrp++) {
189 if(*wrp == '/') *wrp = '\\';
190 }
191 strcat(fullbin, "\\");
192 strcat(fullbin, args[0]);
193 #endif
194 if (!kpse->make_tex_discard_errors) {
195 fprintf (stderr, "\nkpathsea: Running");
196 for (s = &args[0]; *s != NULL; s++)
197 fprintf (stderr, " %s", *s);
198 fputc('\n', stderr);
199 }
200
201 #if defined (AMIGA)
202 /* Amiga has a different interface. */
203 {
204 string cmd;
205 string newcmd;
206 cmd = xstrdup(args[0]);
207 for (s = &args[1]; *s != NULL; s++) {
208 newcmd = concat(cmd, *s);
209 free (cmd);
210 cmd = newcmd;
211 }
212 ret = system(cmd) == 0 ? getenv ("LAST_FONT_CREATED"): NULL;
213 free (cmd);
214 }
215 #elif defined (MSDOS) && !defined(__DJGPP__)
216 #error Implement new MSDOS mktex call interface here
217 #else /* WIN32 or Unix */
218 {
219 #if defined (WIN32)
220 /* spawnvp(_P_NOWAIT, ...) and pipe --ak 2002/12/15 */
221
222 unsigned long nexitcode = STILL_ACTIVE;
223 HANDLE hchild;
224 int hstdout, childpipe[2];
225 int hstderr = -1;
226 FILE *Hnul = NULL;
227
228 fn = NULL;
229
230 if(_pipe(childpipe, 1024, O_TEXT | _O_NOINHERIT) == -1) {
231 perror("kpathsea: pipe()");
232 goto labeldone;
233 }
234
235 hstdout = _dup(fileno(stdout));
236 if(_dup2(childpipe[1], fileno(stdout)) != 0) {
237 close(hstdout);
238 close(childpipe[0]);
239 close(childpipe[1]);
240 goto labeldone;
241 }
242
243 close(childpipe[1]);
244
245 if(kpse->make_tex_discard_errors) {
246 Hnul = fopen("nul", "w");
247 if(!Hnul) {
248 perror("kpathsea: fopen(\"nul\")");
249 }
250 else {
251 hstderr = _dup(fileno(stderr));
252 _dup2(fileno(Hnul), fileno(stderr));
253 }
254 }
255 fprintf(stderr, "\nThe command name is %s\n", fullbin);
256 hchild = (HANDLE)_spawnvp(_P_NOWAIT, fullbin, (const char * const *) args);
257
258 _dup2(hstdout, fileno(stdout));
259 close(hstdout);
260
261 if(hchild == (HANDLE)(-1)) {
262 close(childpipe[0]);
263 goto labeldone;
264 }
265
266 if(hchild) {
267 char buf[1024+1];
268 int num;
269
270 fn = xstrdup("");
271 while(nexitcode == STILL_ACTIVE) {
272 num = read(childpipe[0], buf, sizeof(buf)-1);
273 if(num) {
274 string newfn;
275 buf[num] = '\0';
276 newfn = concat(fn, buf);
277 free(fn);
278 fn = newfn;
279 }
280 if(!GetExitCodeProcess(hchild, &nexitcode)) {
281 fn = NULL;
282 close(childpipe[0]);
283 goto labeldone;
284 }
285 }
286 close(childpipe[0]);
287 }
288
289 labeldone:
290 if(kpse->make_tex_discard_errors && Hnul) {
291 _dup2(hstderr, fileno(stderr));
292 close(hstderr);
293 fclose(Hnul);
294 }
295 #else /* !WIN32 */
296 /* Standard input for the child. Set to /dev/null */
297 int childin;
298 /* Standard output for the child, what we're interested in. */
299 int childout[2];
300 /* Standard error for the child, same as parent or /dev/null */
301 int childerr;
302 /* Child pid. */
303 pid_t childpid;
304
305 /* Open the channels that the child will use. */
306 /* A fairly horrible uses of gotos for here for the error case. */
307 if ((childin = open("/dev/null", O_RDONLY)) < 0) {
308 perror("kpathsea: open(\"/dev/null\", O_RDONLY)");
309 goto error_childin;
310 }
311 if (pipe(childout) < 0) {
312 perror("kpathsea: pipe()");
313 goto error_childout;
314 }
315 if ((childerr = open("/dev/null", O_WRONLY)) < 0) {
316 perror("kpathsea: open(\"/dev/null\", O_WRONLY)");
317 goto error_childerr;
318 }
319 if ((childpid = fork()) < 0) {
320 perror("kpathsea: fork()");
321 close(childerr);
322 error_childerr:
323 close(childout[0]);
324 close(childout[1]);
325 error_childout:
326 close(childin);
327 error_childin:
328 fn = NULL;
329 } else if (childpid == 0) {
330 /* Child
331 *
332 * We can use vfork, provided we're careful about what we
333 * do here: do not return from this function, do not modify
334 * variables, call _exit if there is a problem.
335 *
336 * Complete setting up the file descriptors.
337 * We use dup(2) so the order in which we do this matters.
338 */
339 close(childout[0]);
340 /* stdin -- the child will not receive input from this */
341 if (childin != 0) {
342 close(0);
343 if (dup(childin) != 0) {
344 perror("kpathsea: dup(2) failed for stdin");
345 close(childin);
346 _exit(1);
347 }
348 close(childin);
349 }
350 /* stdout -- the output of the child's action */
351 if (childout[1] != 1) {
352 close(1);
353 if (dup(childout[1]) != 1) {
354 perror("kpathsea: dup(2) failed for stdout");
355 close(childout[1]);
356 _exit(1);
357 }
358 close(childout[1]);
359 }
360 /* stderr -- use /dev/null if we discard errors */
361 if (childerr != 2) {
362 if (kpse->make_tex_discard_errors) {
363 close(2);
364 if (dup(childerr) != 2) {
365 perror("kpathsea: dup(2) failed for stderr");
366 close(childerr);
367 _exit(1);
368 }
369 }
370 close(childerr);
371 }
372 /* FIXME: We could/should close all other file descriptors as well. */
373 /* exec -- on failure a call of _exit(2) it is the only option */
374 if (execvp(args[0], args))
375 perror(args[0]);
376 _exit(1);
377 } else {
378 /* Parent */
379 char buf[1024+1];
380 int num;
381
382 /* Clean up child file descriptors that we won't use anyway. */
383 close(childin);
384 close(childout[1]);
385 close(childerr);
386 /* Get stdout of child from the pipe. */
387 fn = xstrdup("");
388 while ((num = read(childout[0],buf,sizeof(buf)-1)) != 0) {
389 if (num == -1) {
390 if (errno != EINTR) {
391 perror("kpathsea: read()");
392 break;
393 }
394 } else {
395 string newfn;
396 buf[num] = '\0';
397 newfn = concat(fn, buf);
398 free(fn);
399 fn = newfn;
400 }
401 }
402 /* End of file on pipe, child should have exited at this point. */
403 close(childout[0]);
404 /* We don't really care about the exit status at this point. */
405 wait(NULL);
406 }
407 #endif /* !WIN32 */
408
409 if (fn) {
410 len = strlen(fn);
411
412 /* Remove trailing newlines and returns. */
413 while (len && (fn[len - 1] == '\n' || fn[len - 1] == '\r')) {
414 fn[len - 1] = '\0';
415 len--;
416 }
417
418 ret = len == 0 ? NULL : kpathsea_readable_file (kpse, fn);
419 if (!ret && len > 1) {
420 WARNING2 ("kpathsea: %s output `%s' instead of a filename",
421 args[0], fn);
422 }
423
424 /* Free the name if we're not returning it. */
425 if (fn != ret)
426 free (fn);
427 }
428 }
429 #endif /* WIN32 or Unix */
430
431 if (ret == NULL)
432 misstex (kpse, format, args);
433 else
434 kpathsea_db_insert (kpse, ret);
435
436 return ret;
437 }
438
439
440
441 /* Create BASE in FORMAT and return the generated filename, or
442 return NULL. We used to emit warnings for names we declined to pass
443 on to the scripts, but such names are common with system fonts, so
444 now we are silent (just returning NULL). That is arguably better
445 behavior anyway. Presumably the caller always reports "font not
446 found" anyway. */
447
448 string
449 kpathsea_make_tex (kpathsea kpse, kpse_file_format_type format,
450 const_string base)
451 {
452 kpse_format_info_type spec; /* some compilers lack struct initialization */
453 string ret = NULL;
454
455 spec = kpse->format_info[format];
456 if (!spec.type) { /* Not initialized yet? */
457 kpathsea_init_format (kpse, format);
458 spec = kpse->format_info[format];
459 }
460
461 if (spec.program && spec.program_enabled_p) {
462 /* See the documentation for the envvars we're dealing with here. */
463 /* Number of arguments is spec.argc + 1, plus the trailing NULL. */
464 string *args = XTALLOC (spec.argc + 2, string);
465 /* Helpers */
466 int argnum;
467 int i;
468
469 /* Check whether the name we were given is likely to be a problem.
470 All could be fixed in the scripts and/or invocation, but in
471 practice our names are simple, so let's err on the side of strictness:
472 - may not start with a hyphen
473 - allowed are: alphanumeric, underscore, hyphen, period, plus
474 - also allowed: DIRSEP, as we can be fed that when creating pk fonts
475
476 For example, system fonts are likely to contain spaces, and
477 (for filename lookups) be enclosed in square brackets. We don't
478 want to try calling our mktex* scripts on those.
479 */
480 if (base[0] == '-' /* || IS_DIR_SEP(base[0]) */) {
481 return NULL;
482 }
483 for (i = 0; base[i]; i++) {
484 if (!ISALNUM(base[i])
485 && base[i] != '-'
486 && base[i] != '+'
487 && base[i] != '_'
488 && base[i] != '.'
489 && !IS_DIR_SEP(base[i]))
490 {
491 return NULL;
492 }
493 }
494
495 if (format == kpse_gf_format
496 || format == kpse_pk_format
497 || format == kpse_any_glyph_format)
498 set_maketex_mag (kpse);
499
500 /* Here's an awful kludge: if the mode is `/', mktexpk recognizes
501 it as a special case. `kpse_prog_init' sets it to this in the
502 first place when no mode is otherwise specified; this is so
503 when the user defines a resolution, they don't also have to
504 specify a mode; instead, mktexpk's guesses will take over.
505 They use / for the value because then when it is expanded as
506 part of the PKFONTS et al. path values, we'll wind up searching
507 all the pk directories. We put $MAKETEX_MODE in the path
508 values in the first place so that sites with two different
509 devices with the same resolution can find the right fonts; but
510 such sites are uncommon, so they shouldn't make things harder
511 for everyone else. */
512 for (argnum = 0; argnum < spec.argc; argnum++) {
513 args[argnum] = kpathsea_var_expand (kpse, spec.argv[argnum]);
514 }
515 args[argnum++] = xstrdup(base);
516 args[argnum] = NULL;
517
518 ret = maketex (kpse, format, args);
519
520 for (argnum = 0; args[argnum] != NULL; argnum++)
521 free (args[argnum]);
522 free (args);
523 }
524
525 return ret;
526 }
527
528 #if defined (KPSE_COMPAT_API)
529 string
530 kpse_make_tex (kpse_file_format_type format, const_string base)
531 {
532 return kpathsea_make_tex (kpse_def, format, base);
533 }
534 #endif
535
536
537 #ifdef TEST
538
539 void
540 test_make_tex (kpathsea kpse, kpse_file_format_type fmt, const_string base)
541 {
542 string answer;
543
544 printf ("\nAttempting %s in format %d:\n", base, fmt);
545
546 answer = kpathsea_make_tex (kpse, fmt, base);
547 puts (answer ? answer : "(nil)");
548 }
549
550
551 int
552 main (int argc, char **argv)
553 {
554 kpathsea kpse = xcalloc(1, sizeof(kpathsea_instance));
555 kpathsea_set_program_name (kpse, argv[0], NULL);
556 kpathsea_xputenv (kpse, "KPATHSEA_DPI", "781"); /* call mktexpk */
557 kpathsea_xputenv (kpse,"MAKETEX_BASE_DPI", "300"); /* call mktexpk */
558 kpathsea_set_program_enabled (kpse, kpse_pk_format, 1, kpse_src_env);
559
560 /* Succeed. */
561 test_make_tex (kpse, kpse_pk_format, "cmr10");
562
563 /* Fail with mktextfm, writing missfont.log to a different directory. */
564 kpathsea_set_program_enabled (kpse, kpse_tfm_format, 1, kpse_src_env);
565 kpathsea_xputenv (kpse,"TEXMF_OUTPUT_DIRECTORY", "/tmp");
566 test_make_tex (kpse, kpse_tfm_format, "foozout99");
567
568 /* Since the missfont.log descriptor is cached, can only save in one
569 place in a given run, so don't bother doing it twice.
570 test_make_tex (kpse, kpse_tfm_format, "foozler98");
571 */
572
573 /* Call something disabled. */
574 test_make_tex (kpse, kpse_bst_format, "no-way");
575
576 return 0;
577 }
578
579 #endif /* TEST */
580
581
582 /*
583 Local variables:
584 standalone-compile-command: "gcc -g -I. -I.. -I$wk/.. -DTEST -DMAKE_KPSE_DLL tex-make.c $wk/.libs/libkpathsea.a && ./a.out"
585 End:
586 */