Debian Codegraph @

texk/kpathsea/tex-file.c

raw
1 /* tex-file.c: high-level file searching by format.
2
3 Copyright 1993, 1994, 1995, 1996, 1997, 2007, 2008, 2009, 2010, 2011
4 2012, 2014, 2016, 2017, 2019, 2024 Karl Berry.
5 Copyright 1998-2005 Olaf Weber.
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with this library; if not, see <http://www.gnu.org/licenses/>. */
19
20 #include <kpathsea/config.h>
21
22 #include <kpathsea/c-fopen.h>
23 #include <kpathsea/c-pathch.h>
24 #include <stdarg.h>
25 #include <kpathsea/cnf.h>
26 #include <kpathsea/absolute.h>
27 #include <kpathsea/concatn.h>
28 #include <kpathsea/default.h>
29 #include <kpathsea/expand.h>
30 #include <kpathsea/fontmap.h>
31 #include <kpathsea/paths.h>
32 #include <kpathsea/pathsearch.h>
33 #include <kpathsea/tex-file.h>
34 #include <kpathsea/tex-make.h>
35 #include <kpathsea/variable.h>
36 #include <kpathsea/c-ctype.h>
37
38 /* These are not in the structure
39 because it's annoying to initialize lists in C. */
40 #define GF_ENVS "GFFONTS", GLYPH_ENVS
41 #define PK_ENVS "PKFONTS", "TEXPKS", GLYPH_ENVS
42 #define GLYPH_ENVS "GLYPHFONTS", "TEXFONTS"
43 #define TFM_ENVS "TFMFONTS", "TEXFONTS"
44 #define AFM_ENVS "AFMFONTS", "TEXFONTS"
45 #define BASE_ENVS "MFBASES", "TEXMFINI"
46 #define BIB_ENVS "BIBINPUTS", "TEXBIB"
47 #define BST_ENVS "BSTINPUTS"
48 #define CNF_ENVS "TEXMFCNF"
49 #define DB_ENVS "TEXMFDBS"
50 #define FMT_ENVS "TEXFORMATS", "TEXMFINI"
51 #define FONTMAP_ENVS "TEXFONTMAPS", "TEXFONTS"
52 #define MEM_ENVS "MPMEMS", "TEXMFINI"
53 #define MF_ENVS "MFINPUTS"
54 #define MFPOOL_ENVS "MFPOOL", "TEXMFINI"
55 #define MFT_ENVS "MFTINPUTS"
56 #define MP_ENVS "MPINPUTS"
57 #define MPPOOL_ENVS "MPPOOL", "TEXMFINI"
58 #define MPSUPPORT_ENVS "MPSUPPORT"
59 #define OCP_ENVS "OCPINPUTS"
60 #define OFM_ENVS "OFMFONTS", "TEXFONTS"
61 #define OPL_ENVS "OPLFONTS", "TEXFONTS"
62 #define OTP_ENVS "OTPINPUTS"
63 #define OVF_ENVS "OVFFONTS", "TEXFONTS"
64 #define OVP_ENVS "OVPFONTS", "TEXFONTS"
65 #define PICT_ENVS "TEXPICTS", TEX_ENVS
66 #define TEX_ENVS "TEXINPUTS"
67 #define TEXDOC_ENVS "TEXDOCS"
68 #define TEXPOOL_ENVS "TEXPOOL", "TEXMFINI"
69 #define TEXSOURCE_ENVS "TEXSOURCES"
70 #define TEX_PS_HEADER_ENVS "TEXPSHEADERS", "PSHEADERS"
71 #define TROFF_FONT_ENVS "TRFONTS"
72 #define TYPE1_ENVS "T1FONTS", "T1INPUTS", "TEXFONTS", TEX_PS_HEADER_ENVS
73 #define VF_ENVS "VFFONTS", "TEXFONTS"
74 #define DVIPS_CONFIG_ENVS "TEXCONFIG"
75 #define IST_ENVS "TEXINDEXSTYLE", "INDEXSTYLE"
76 #define TRUETYPE_ENVS "TTFONTS", "TEXFONTS"
77 #define TYPE42_ENVS "T42FONTS", "TEXFONTS"
78 #define WEB2C_ENVS "WEB2C"
79 #define MISCFONTS_ENVS "MISCFONTS", "TEXFONTS"
80 #define WEB_ENVS "WEBINPUTS"
81 #define CWEB_ENVS "CWEBINPUTS"
82 #define ENC_ENVS "ENCFONTS", "TEXFONTS"
83 #define CMAP_ENVS "CMAPFONTS", "TEXFONTS"
84 #define SFD_ENVS "SFDFONTS", "TEXFONTS"
85 #define OPENTYPE_ENVS "OPENTYPEFONTS", "TEXFONTS"
86 #define PDFTEXCONFIG_ENVS "PDFTEXCONFIG"
87 #define LIG_ENVS "LIGFONTS", "TEXFONTS"
88 #define TEXMFSCRIPTS_ENVS "TEXMFSCRIPTS"
89 #define LUA_ENVS "LUAINPUTS"
90 #define FONTFEATURES_ENVS "FONTFEATURES"
91 #define FONTCIDMAPS_ENVS "FONTCIDMAPS"
92 #define MLBIB_ENVS "MLBIBINPUTS", BIB_ENVS
93 #define MLBST_ENVS "MLBSTINPUTS", BST_ENVS
94 #define CLUA_ENVS "CLUAINPUTS"
95 #define RIS_ENVS "RISINPUTS"
96 #define BLTXML_ENVS "BLTXMLINPUTS"
97
98 /* The compiled-in default list, DEFAULT_FONT_SIZES, is intended to be
99 set from the command line (presumably via the Makefile). */
100
101 #ifndef DEFAULT_FONT_SIZES
102 #define DEFAULT_FONT_SIZES ""
103 #endif
104
105 void
106 kpathsea_init_fallback_resolutions (kpathsea kpse, string envvar)
107 {
108 string size;
109 const_string size_var = ENVVAR (envvar, "TEXSIZES");
110 string size_str = getenv (size_var);
111 unsigned *last_resort_sizes = NULL;
112 unsigned size_count = 0;
113 const_string default_sizes = kpse->fallback_resolutions_string
114 ? kpse->fallback_resolutions_string
115 : DEFAULT_FONT_SIZES;
116 string size_list = kpathsea_expand_default (kpse, size_str, default_sizes);
117
118 /* Initialize the list of last-resort sizes. */
119 for (size = kpathsea_path_element (kpse, size_list); size != NULL;
120 size = kpathsea_path_element (kpse, NULL))
121 {
122 unsigned s;
123 if (! *size) /* Skip empty elements. */
124 continue;
125
126 s = atoi (size);
127 if (size_count && s < last_resort_sizes[size_count - 1]) {
128 WARNING1 ("kpathsea: last resort size %s not in ascending order; ignored",
129 size);
130 } else {
131 size_count++;
132 XRETALLOC (last_resort_sizes, size_count, unsigned);
133 last_resort_sizes[size_count - 1] = atoi (size);
134 }
135 }
136
137 /* Add a zero to mark the end of the list. */
138 size_count++;
139 XRETALLOC (last_resort_sizes, size_count, unsigned);
140 last_resort_sizes[size_count - 1] = 0;
141
142 free (size_list);
143
144 kpse->fallback_resolutions = last_resort_sizes;
145 }
146
147 #if defined (KPSE_COMPAT_API)
148 void
149 kpse_init_fallback_resolutions ( string envvar)
150 {
151 kpathsea_init_fallback_resolutions (kpse_def, envvar);
152 }
153 #endif
154
155
156 /* We should be able to set the program arguments in the same way. Not
157 to mention the path values themselves. */
158
159 void
160 kpathsea_set_program_enabled (kpathsea kpse, kpse_file_format_type fmt,
161 boolean value, kpse_src_type level)
162 {
163 kpse_format_info_type *f = &(kpse->format_info[fmt]);
164 if (level >= f->program_enable_level) {
165 f->program_enabled_p = value;
166 f->program_enable_level = level;
167 }
168 }
169
170
171 #if defined (KPSE_COMPAT_API)
172 void
173 kpse_set_program_enabled (kpse_file_format_type fmt,
174 boolean value, kpse_src_type level)
175 {
176 kpathsea_set_program_enabled (kpse_def, fmt, value, level);
177 }
178
179 #endif
180
181
182 /* Web2c and kpsewhich have command-line options to set this stuff. May
183 as well have a common place. */
184
185 void
186 kpathsea_maketex_option (kpathsea kpse, const_string fmtname, boolean value)
187 {
188 kpse_file_format_type fmt = kpse_last_format;
189
190 /* Trying to match up with the suffix lists unfortunately doesn't work
191 well, since that would require initializing the formats. */
192 if (FILESTRCASEEQ (fmtname, "pk")) {
193 fmt = kpse_pk_format;
194 } else if (FILESTRCASEEQ (fmtname, "mf")) {
195 fmt = kpse_mf_format;
196 } else if (FILESTRCASEEQ (fmtname, "tex")) {
197 fmt = kpse_tex_format;
198 } else if (FILESTRCASEEQ (fmtname, "tfm")) {
199 fmt = kpse_tfm_format;
200 } else if (FILESTRCASEEQ (fmtname, "fmt")) {
201 fmt = kpse_fmt_format;
202 } else if (FILESTRCASEEQ (fmtname, "ofm")) {
203 fmt = kpse_ofm_format;
204 } else if (FILESTRCASEEQ (fmtname, "ocp")) {
205 fmt = kpse_ocp_format;
206 } else {
207 fprintf (stderr, "\nkpathsea: Unknown mktex format: %s\n", fmtname);
208 }
209
210 if (fmt != kpse_last_format) {
211 kpathsea_set_program_enabled (kpse, fmt, value, kpse_src_cmdline);
212 }
213 }
214
215 #if defined (KPSE_COMPAT_API)
216 void
217 kpse_maketex_option (const_string fmtname, boolean value)
218 {
219 kpathsea_maketex_option (kpse_def, fmtname, value);
220 }
221 #endif
222
223
224 /* Macro subroutine for `init_path'. EXPAND_DEFAULT calls
225 kpse_expand_default on try_path and the present info->path. */
226 #define EXPAND_DEFAULT(try_path, source_string) \
227 if (try_path) { \
228 info->raw_path = try_path; \
229 tmp = info->path; \
230 info->path = kpathsea_expand_default (kpse, try_path, info->path); \
231 free (tmp); \
232 info->path_source = source_string; \
233 }
234
235 /* Find the final search path to use for the format entry INFO, given
236 the compile-time default (DEFAULT_PATH), and the environment
237 variables to check (the remaining arguments, terminated with NULL).
238 We set the `path' and `path_source' members of INFO. The
239 `client_path' member must already be set upon entry. */
240
241 static void
242 init_path (kpathsea kpse, kpse_format_info_type *info,
243 const_string default_path, ...)
244 {
245 string env_name;
246 string env_value = NULL;
247 string var = NULL;
248 string tmp;
249 va_list ap;
250
251 info->default_path = default_path;
252
253 va_start (ap, default_path);
254 /* First envvar that's set to a nonempty value will exit the loop. If
255 none are set, we want the first cnf entry that matches. Find the
256 cnf value simultaneously with the envvar value, to avoid having to
257 go through the envvar list twice,
258 that would mean having to create a str_list and use it twice. */
259 while ((env_name = va_arg (ap, string)) != NULL) {
260 /* Since sh doesn't like envvar names with `.', check PATH_prog
261 as well as PATH.prog. */
262 if (!var) { /* Try PATH.prog. */
263 string evar = concat3 (env_name, ".", kpse->program_name);
264 env_value = getenv (evar);
265 if (env_value && *env_value) {
266 var = evar;
267 } else { /* Try PATH_prog. */
268 free (evar);
269 evar = concat3 (env_name, "_", kpse->program_name);
270 env_value = getenv (evar);
271 if (env_value && *env_value) {
272 var = evar;
273 } else { /* Try simply PATH. */
274 free (evar);
275 env_value = getenv (env_name);
276 if (env_value && *env_value) {
277 var = env_name;
278 }
279 }
280 }
281 }
282
283 /* If we are initializing the cnf path, don't try to get any
284 values from the cnf files; that's infinite loop time. */
285 if (!info->cnf_path && info != &(kpse->format_info[kpse_cnf_format]))
286 info->cnf_path = kpathsea_cnf_get (kpse, env_name);
287
288 if (var && info->cnf_path)
289 break;
290 }
291 va_end (ap);
292
293 /* Expand any extra :'s. For each level, we replace an extra : with
294 the path at the next lower level. For example, an extra : in a
295 user-set envvar should be replaced with the path from the cnf file.
296 things are complicated because none of the levels above the very
297 bottom are guaranteed to exist. */
298
299 /* Assume we can reliably start with the compile-time default. */
300 info->raw_path = info->default_path;
301 info->path = xstrdup (info->raw_path);
302 info->path_source = "compile-time paths.h";
303
304 EXPAND_DEFAULT (info->cnf_path, "texmf.cnf");
305 EXPAND_DEFAULT (info->client_path, "program config file");
306
307 if (var) {
308 /* Translate `;' in the envvar into `:' if that's our ENV_SEP. */
309 if (IS_ENV_SEP (':')) {
310 string loc;
311 env_value = xstrdup (env_value); /* Freed below. */
312 for (loc = env_value; *loc; loc++) {
313 if (*loc == ';')
314 *loc = ':';
315 }
316 }
317 EXPAND_DEFAULT (env_value, concat (var, " environment variable"));
318 /* Do not free the copied env_value, because EXPAND_DEFAULT set
319 raw_path to point to it. If it gets overwritten again, tough. */
320 }
321
322 EXPAND_DEFAULT (info->override_path, "application override variable");
323 tmp = info->path;
324 info->path = kpathsea_brace_expand (kpse, info->path);
325 free (tmp);
326 }
327
328
329 /* Some file types have more than one suffix, and sometimes it is
330 convenient to modify the list of searched suffixes. */
331
332 static void
333 kpathsea_set_suffixes_va_list (kpathsea kpse, kpse_file_format_type format,
334 boolean alternate, va_list ap)
335 {
336 const_string **list;
337 const_string s;
338 int count = 0;
339
340 if (alternate) {
341 list = &(kpse->format_info[format].alt_suffix);
342 } else {
343 list = &(kpse->format_info[format].suffix);
344 }
345
346 while ((s = va_arg (ap, string)) != NULL) {
347 count++;
348 /* This is essentially
349 XRETALLOC (*list, count + 1, const_string);
350 except that MSVC warns without the cast to `void *'. */
351 *list = (const_string *) xrealloc ((void *) *list,
352 (count + 1) * sizeof(const_string));
353 (*list)[count - 1] = s;
354 }
355 (*list)[count] = NULL;
356 }
357
358 void
359 kpathsea_set_suffixes (kpathsea kpse, kpse_file_format_type format,
360 boolean alternate, ...)
361 {
362 va_list ap;
363 va_start (ap, alternate);
364 kpathsea_set_suffixes_va_list (kpse, format, alternate, ap);
365 va_end (ap);
366 }
367
368
369 #if defined (KPSE_COMPAT_API)
370 void
371 kpse_set_suffixes (kpse_file_format_type format,
372 boolean alternate, ...)
373 {
374 va_list ap;
375 va_start (ap, alternate);
376 kpathsea_set_suffixes_va_list (kpse_def, format, alternate, ap);
377 va_end (ap);
378 }
379 #endif
380
381
382 /* The path spec we are defining, one element of the global array. */
383 #define FMT_INFO (kpse->format_info[format])
384 /* Call kpse_set_add_suffixes. */
385 #define SUFFIXES(args) kpathsea_set_suffixes(kpse, format, false, args, NULL)
386 #define ALT_SUFFIXES(args) kpathsea_set_suffixes(kpse, format, true, args, NULL)
387
388 /* Call `init_path', including appending the trailing NULL to the envvar
389 list. Also initialize the fields not needed in setting the path. */
390 #define INIT_FORMAT(text, default_path, envs) \
391 FMT_INFO.type = text; \
392 init_path (kpse, &FMT_INFO, default_path, envs, NULL); \
393 envvar_list = concatn_with_spaces (envs, NULL);
394
395
396 /* A few file types allow for runtime generation by an external program.
397 kpse_init_prog may have already initialized it (the `program'
398 member). Here we allow people to turn it off or on in the config
399 file, by setting the variable whose name is the uppercasified program
400 name to 0 or 1. */
401
402 static void
403 init_maketex (kpathsea kpse, kpse_file_format_type fmt,
404 const_string dflt_prog, ...)
405 {
406 kpse_format_info_type *f = &(kpse->format_info[fmt]);
407 const_string prog = f->program ? f->program : dflt_prog; /* mktexpk */
408 string PROG = uppercasify (prog); /* MKTEXPK */
409 string progval = kpathsea_var_value (kpse, PROG); /* $ENV/cnf{"MKTEXPK"} */
410 const_string arg;
411 va_list ap;
412
413 /* Doesn't hurt to always set this info. */
414 f->program = prog;
415
416 /* Set up the argument vector. */
417 f->argc = 0;
418 f->argv = XTALLOC (2, const_string);
419 f->argv[f->argc++] = dflt_prog;
420 va_start (ap, dflt_prog);
421 while ((arg = va_arg (ap, string)) != NULL) {
422 f->argc++;
423 f->argv = (const_string *) xrealloc ((void *) f->argv,
424 (f->argc + 1) * sizeof(const_string));
425 f->argv[f->argc - 1] = arg;
426 }
427 va_end (ap);
428 f->argv[f->argc] = NULL;
429
430 if (progval && *progval) {
431 /* This might actually be from an environment variable value, but in
432 that case, we'll have previously set it from kpse_init_prog. */
433 kpathsea_set_program_enabled (kpse, fmt, *progval == '1',
434 kpse_src_client_cnf);
435 }
436
437 free (PROG);
438 }
439
440 /* We need this twice, so ... */
441 #define MKTEXPK_ARGS \
442 "--mfmode","$MAKETEX_MODE",\
443 "--bdpi","$MAKETEX_BASE_DPI",\
444 "--mag","$MAKETEX_MAG",\
445 "--dpi","$KPATHSEA_DPI",\
446 NULL
447
448 static string
449 remove_dbonly (const_string path)
450 {
451 string ret = XTALLOC(strlen (path) + 1, char), q=ret;
452 const_string p=path;
453 boolean new_elt=true;
454
455 while (*p) {
456 if (new_elt && *p && *p == '!' && *(p+1) == '!')
457 p += 2;
458 else {
459 new_elt = (*p == ENV_SEP);
460 *q++ = *p++;
461 }
462 }
463 *q = '\0';
464 return(ret);
465 }
466
467 /* Same as concatn but puts a space between each element. All this just
468 for nice debugging output. But it's useful. */
469
470 static string
471 concatn_with_spaces (const_string str1, ...)
472 {
473 string arg;
474 string ret;
475 va_list ap;
476
477 if (!str1)
478 return NULL;
479
480 ret = xstrdup (str1);
481
482 va_start (ap, str1);
483 while ((arg = va_arg (ap, string)) != NULL)
484 {
485 string temp = concat3 (ret, " ", arg);
486 free (ret);
487 ret = temp;
488 }
489 va_end (ap);
490
491 return ret;
492 }
493
494
495 /* Initialize everything for FORMAT. Return its search path. */
496
497 const_string
498 kpathsea_init_format (kpathsea kpse, kpse_file_format_type format)
499 {
500 /* If we get called twice, don't redo all the work. */
501 if (! FMT_INFO.path) {
502 /* Don't care about the list of variable names. */
503 (void) kpathsea_init_format_return_varlist (kpse, format);
504 }
505
506 return FMT_INFO.path;
507 }
508
509 /* Initialize everything for FORMAT. Return the list of
510 environment/config variable names considered, which is not otherwise
511 saved. Although in principle we could add that list to our format
512 struct, it seems a waste, since this is only used by kpsewhich --help. */
513
514 const_string
515 kpathsea_init_format_return_varlist(kpathsea kpse,kpse_file_format_type format)
516 {
517 string envvar_list; /* only for debug output, set in INIT_FORMAT */
518
519 switch (format)
520 { /* We might be able to avoid repeating `gf' or whatever so many
521 times with token pasting, but it doesn't seem worth it. */
522 case kpse_gf_format:
523 INIT_FORMAT ("gf", DEFAULT_GFFONTS, GF_ENVS);
524 SUFFIXES ("gf");
525 FMT_INFO.suffix_search_only = true;
526 FMT_INFO.binmode = true;
527 break;
528 case kpse_pk_format:
529 init_maketex (kpse, format, "mktexpk", MKTEXPK_ARGS);
530 INIT_FORMAT ("pk", DEFAULT_PKFONTS, PK_ENVS);
531 SUFFIXES ("pk");
532 FMT_INFO.suffix_search_only = true;
533 FMT_INFO.binmode = true;
534 break;
535 case kpse_any_glyph_format:
536 init_maketex (kpse, format, "mktexpk", MKTEXPK_ARGS);
537 INIT_FORMAT ("bitmap font", DEFAULT_GLYPHFONTS, GLYPH_ENVS);
538 FMT_INFO.suffix_search_only = true;
539 FMT_INFO.binmode = true;
540 break;
541 case kpse_tfm_format:
542 /* Must come before kpse_ofm_format. */
543 init_maketex (kpse, format, "mktextfm", NULL);
544 INIT_FORMAT ("tfm", DEFAULT_TFMFONTS, TFM_ENVS);
545 SUFFIXES (".tfm");
546 FMT_INFO.suffix_search_only = true;
547 FMT_INFO.binmode = true;
548 break;
549 case kpse_afm_format:
550 INIT_FORMAT ("afm", DEFAULT_AFMFONTS, AFM_ENVS);
551 SUFFIXES (".afm");
552 break;
553 case kpse_base_format:
554 init_maketex (kpse, format, "mktexfmt", NULL);
555 INIT_FORMAT ("base", DEFAULT_MFBASES, BASE_ENVS);
556 SUFFIXES (".base");
557 FMT_INFO.binmode = true;
558 break;
559 case kpse_bib_format:
560 INIT_FORMAT ("bib", DEFAULT_BIBINPUTS, BIB_ENVS);
561 SUFFIXES (".bib");
562 FMT_INFO.suffix_search_only = true;
563 break;
564 case kpse_bst_format:
565 INIT_FORMAT ("bst", DEFAULT_BSTINPUTS, BST_ENVS);
566 SUFFIXES (".bst");
567 break;
568 case kpse_cnf_format:
569 INIT_FORMAT ("cnf", DEFAULT_TEXMFCNF, CNF_ENVS);
570 SUFFIXES (".cnf");
571 break;
572 case kpse_db_format:
573 INIT_FORMAT ("ls-R", DEFAULT_TEXMFDBS, DB_ENVS);
574 #define LSR_SUFFIXES "ls-R", "ls-r"
575 SUFFIXES (LSR_SUFFIXES);
576 FMT_INFO.path = remove_dbonly (FMT_INFO.path);
577 break;
578 case kpse_fmt_format:
579 init_maketex (kpse, format, "mktexfmt", NULL);
580 INIT_FORMAT ("fmt", DEFAULT_TEXFORMATS, FMT_ENVS);
581 SUFFIXES (".fmt");
582 FMT_INFO.binmode = true;
583 break;
584 case kpse_fontmap_format:
585 INIT_FORMAT ("map", DEFAULT_TEXFONTMAPS, FONTMAP_ENVS);
586 SUFFIXES (".map");
587 break;
588 case kpse_mem_format:
589 init_maketex (kpse, format, "mktexfmt", NULL);
590 INIT_FORMAT ("mem", DEFAULT_MPMEMS, MEM_ENVS);
591 SUFFIXES (".mem");
592 FMT_INFO.binmode = true;
593 break;
594 case kpse_mf_format:
595 init_maketex (kpse, format, "mktexmf", NULL);
596 INIT_FORMAT ("mf", DEFAULT_MFINPUTS, MF_ENVS);
597 SUFFIXES (".mf");
598 break;
599 case kpse_mft_format:
600 INIT_FORMAT ("mft", DEFAULT_MFTINPUTS, MFT_ENVS);
601 SUFFIXES (".mft");
602 break;
603 case kpse_mfpool_format:
604 INIT_FORMAT ("mfpool", DEFAULT_MFPOOL, MFPOOL_ENVS);
605 SUFFIXES (".pool");
606 break;
607 case kpse_mp_format:
608 INIT_FORMAT ("mp", DEFAULT_MPINPUTS, MP_ENVS);
609 SUFFIXES (".mp");
610 break;
611 case kpse_mppool_format:
612 INIT_FORMAT ("mppool", DEFAULT_MPPOOL, MPPOOL_ENVS);
613 SUFFIXES (".pool");
614 break;
615 case kpse_mpsupport_format:
616 INIT_FORMAT ("MetaPost support", DEFAULT_MPSUPPORT, MPSUPPORT_ENVS);
617 break;
618 case kpse_ocp_format:
619 init_maketex (kpse, format, "mkocp", NULL);
620 INIT_FORMAT ("ocp", DEFAULT_OCPINPUTS, OCP_ENVS);
621 SUFFIXES (".ocp");
622 FMT_INFO.suffix_search_only = true;
623 FMT_INFO.binmode = true;
624 break;
625 case kpse_ofm_format:
626 init_maketex (kpse, format, "mkofm", NULL);
627 INIT_FORMAT ("ofm", DEFAULT_OFMFONTS, OFM_ENVS);
628 #define OFM_SUFFIXES ".ofm", ".tfm"
629 SUFFIXES (OFM_SUFFIXES);
630 FMT_INFO.suffix_search_only = true;
631 FMT_INFO.binmode = true;
632 break;
633 case kpse_opl_format:
634 INIT_FORMAT ("opl", DEFAULT_OPLFONTS, OPL_ENVS);
635 SUFFIXES (".opl");
636 ALT_SUFFIXES (".pl");
637 FMT_INFO.suffix_search_only = true;
638 break;
639 case kpse_otp_format:
640 INIT_FORMAT ("otp", DEFAULT_OTPINPUTS, OTP_ENVS);
641 SUFFIXES (".otp");
642 FMT_INFO.suffix_search_only = true;
643 break;
644 case kpse_ovf_format:
645 INIT_FORMAT ("ovf", DEFAULT_OVFFONTS, OVF_ENVS);
646 #define OVF_SUFFIXES ".ovf", ".vf"
647 SUFFIXES (OVF_SUFFIXES);
648 FMT_INFO.suffix_search_only = true;
649 FMT_INFO.binmode = true;
650 break;
651 case kpse_ovp_format:
652 INIT_FORMAT ("ovp", DEFAULT_OVPFONTS, OVP_ENVS);
653 SUFFIXES (".ovp");
654 ALT_SUFFIXES (".vpl");
655 FMT_INFO.suffix_search_only = true;
656 break;
657 case kpse_pict_format:
658 INIT_FORMAT ("graphic/figure", DEFAULT_TEXINPUTS, PICT_ENVS);
659 #define ALT_PICT_SUFFIXES ".eps", ".epsi"
660 ALT_SUFFIXES (ALT_PICT_SUFFIXES);
661 FMT_INFO.binmode = true;
662 break;
663 case kpse_tex_format:
664 init_maketex (kpse, format, "mktextex", NULL);
665 INIT_FORMAT ("tex", DEFAULT_TEXINPUTS, TEX_ENVS);
666 SUFFIXES (".tex");
667 /* TeX files can have any obscure suffix in the world (or none at
668 all). Only check for the most common ones. */
669 #define ALT_TEX_SUFFIXES ".sty",".cls",".fd",".aux",".bbl",".def",".clo",".ldf"
670 ALT_SUFFIXES (ALT_TEX_SUFFIXES);
671 break;
672 case kpse_tex_ps_header_format:
673 INIT_FORMAT ("PostScript header", DEFAULT_TEXPSHEADERS,
674 TEX_PS_HEADER_ENVS);
675 /* Unfortunately, at one time dvips used this format for type1 fonts. */
676 #define ALT_TEXPSHEADER_SUFFIXES ".pro"
677 ALT_SUFFIXES (ALT_TEXPSHEADER_SUFFIXES);
678 FMT_INFO.binmode = true;
679 break;
680 case kpse_texdoc_format:
681 INIT_FORMAT ("TeX system documentation", DEFAULT_TEXDOCS, TEXDOC_ENVS);
682 break;
683 case kpse_texpool_format:
684 INIT_FORMAT ("texpool", DEFAULT_TEXPOOL, TEXPOOL_ENVS);
685 SUFFIXES (".pool");
686 break;
687 case kpse_texsource_format:
688 INIT_FORMAT ("TeX system sources", DEFAULT_TEXSOURCES, TEXSOURCE_ENVS);
689 #define ALT_SOURCES_SUFFIXES ".dtx", ".ins"
690 ALT_SUFFIXES (ALT_SOURCES_SUFFIXES);
691 break;
692 case kpse_troff_font_format:
693 INIT_FORMAT ("Troff fonts", DEFAULT_TRFONTS, TROFF_FONT_ENVS);
694 FMT_INFO.binmode = true;
695 break;
696 case kpse_type1_format:
697 INIT_FORMAT ("type1 fonts", DEFAULT_T1FONTS, TYPE1_ENVS);
698 #define TYPE1_SUFFIXES ".pfa", ".pfb"
699 SUFFIXES (TYPE1_SUFFIXES);
700 FMT_INFO.binmode = true;
701 break;
702 case kpse_vf_format:
703 INIT_FORMAT ("vf", DEFAULT_VFFONTS, VF_ENVS);
704 SUFFIXES (".vf");
705 FMT_INFO.suffix_search_only = true;
706 FMT_INFO.binmode = true;
707 break;
708 case kpse_dvips_config_format:
709 INIT_FORMAT ("dvips config", DEFAULT_TEXCONFIG, DVIPS_CONFIG_ENVS);
710 break;
711 case kpse_ist_format:
712 INIT_FORMAT ("ist", DEFAULT_INDEXSTYLE, IST_ENVS);
713 SUFFIXES (".ist");
714 break;
715 case kpse_truetype_format:
716 INIT_FORMAT ("truetype fonts", DEFAULT_TTFONTS, TRUETYPE_ENVS);
717 #define TRUETYPE_SUFFIXES ".ttf", ".ttc", ".TTF", ".TTC", ".dfont"
718 SUFFIXES (TRUETYPE_SUFFIXES);
719 FMT_INFO.suffix_search_only = false;
720 FMT_INFO.binmode = true;
721 break;
722 case kpse_type42_format:
723 INIT_FORMAT ("type42 fonts", DEFAULT_T42FONTS, TYPE42_ENVS);
724 #define TYPE42_SUFFIXES ".t42", ".T42"
725 SUFFIXES (TYPE42_SUFFIXES);
726 FMT_INFO.binmode = true;
727 break;
728 case kpse_web2c_format:
729 INIT_FORMAT ("web2c files", DEFAULT_WEB2C, WEB2C_ENVS);
730 break;
731 case kpse_program_text_format:
732 INIT_FORMAT ("other text files",
733 concatn (".", ENV_SEP_STRING, "$TEXMF", DIR_SEP_STRING,
734 kpse->program_name,
735 DIR_SEP_STRING, DIR_SEP_STRING, NULL),
736 concat (uppercasify (kpse->program_name), "INPUTS"));
737 break;
738 case kpse_program_binary_format:
739 INIT_FORMAT ("other binary files",
740 concatn (".", ENV_SEP_STRING, "$TEXMF", DIR_SEP_STRING,
741 kpse->program_name,
742 DIR_SEP_STRING, DIR_SEP_STRING, NULL),
743 concat (uppercasify (kpse->program_name), "INPUTS"));
744 FMT_INFO.binmode = true;
745 break;
746 case kpse_miscfonts_format:
747 INIT_FORMAT ("misc fonts", DEFAULT_MISCFONTS, MISCFONTS_ENVS);
748 FMT_INFO.binmode = true;
749 break;
750 case kpse_web_format:
751 INIT_FORMAT ("web", DEFAULT_WEBINPUTS, WEB_ENVS);
752 SUFFIXES (".web");
753 ALT_SUFFIXES (".ch");
754 break;
755 case kpse_cweb_format:
756 INIT_FORMAT ("cweb", DEFAULT_CWEBINPUTS, CWEB_ENVS);
757 #define CWEB_SUFFIXES ".w", ".web"
758 SUFFIXES (CWEB_SUFFIXES);
759 ALT_SUFFIXES (".ch");
760 break;
761 case kpse_enc_format:
762 INIT_FORMAT ("enc files", DEFAULT_ENCFONTS, ENC_ENVS);
763 SUFFIXES (".enc");
764 FMT_INFO.suffix_search_only = true;
765 break;
766 case kpse_cmap_format:
767 INIT_FORMAT ("cmap files", DEFAULT_CMAPFONTS, CMAP_ENVS);
768 break;
769 case kpse_sfd_format:
770 INIT_FORMAT ("subfont definition files", DEFAULT_SFDFONTS, SFD_ENVS);
771 SUFFIXES (".sfd");
772 FMT_INFO.suffix_search_only = true;
773 break;
774 case kpse_opentype_format:
775 INIT_FORMAT ("opentype fonts", DEFAULT_OPENTYPEFONTS, OPENTYPE_ENVS);
776 #define OPENTYPE_SUFFIXES ".otf", ".OTF"
777 SUFFIXES (OPENTYPE_SUFFIXES);
778 FMT_INFO.suffix_search_only = true;
779 FMT_INFO.binmode = true;
780 break;
781 case kpse_pdftex_config_format:
782 INIT_FORMAT ("pdftex config", DEFAULT_PDFTEXCONFIG, PDFTEXCONFIG_ENVS);
783 break;
784 case kpse_lig_format:
785 INIT_FORMAT ("lig files", DEFAULT_LIGFONTS, LIG_ENVS);
786 SUFFIXES (".lig");
787 FMT_INFO.suffix_search_only = true;
788 break;
789 case kpse_texmfscripts_format:
790 INIT_FORMAT ("texmfscripts", DEFAULT_TEXMFSCRIPTS, TEXMFSCRIPTS_ENVS);
791 break;
792 case kpse_lua_format:
793 INIT_FORMAT ("lua", DEFAULT_LUAINPUTS, LUA_ENVS);
794 #define LUA_SUFFIXES \
795 ".lua", ".luatex", ".luc", ".luctex", ".texlua", ".texluc", ".tlu"
796 SUFFIXES (LUA_SUFFIXES);
797 FMT_INFO.suffix_search_only = true;
798 break;
799 case kpse_fea_format:
800 INIT_FORMAT ("font feature files", DEFAULT_FONTFEATURES,
801 FONTFEATURES_ENVS);
802 SUFFIXES (".fea");
803 FMT_INFO.suffix_search_only = true;
804 break;
805 case kpse_cid_format:
806 INIT_FORMAT ("cid maps", DEFAULT_FONTCIDMAPS, FONTCIDMAPS_ENVS);
807 #define CID_SUFFIXES ".cid", ".cidmap"
808 SUFFIXES (CID_SUFFIXES);
809 FMT_INFO.suffix_search_only = true;
810 break;
811 case kpse_mlbib_format:
812 INIT_FORMAT ("mlbib", DEFAULT_MLBIBINPUTS, MLBIB_ENVS);
813 #define MLBIB_SUFFIXES ".mlbib", ".bib"
814 SUFFIXES (MLBIB_SUFFIXES);
815 FMT_INFO.suffix_search_only = true;
816 break;
817 case kpse_mlbst_format:
818 INIT_FORMAT ("mlbst", DEFAULT_MLBSTINPUTS, MLBST_ENVS);
819 #define MLBST_SUFFIXES ".mlbst", ".bst"
820 SUFFIXES (MLBST_SUFFIXES);
821 FMT_INFO.suffix_search_only = true;
822 break;
823 case kpse_clua_format:
824 INIT_FORMAT ("clua", DEFAULT_CLUAINPUTS, CLUA_ENVS);
825 #define CLUA_SUFFIXES ".dll", ".so"
826 SUFFIXES (CLUA_SUFFIXES);
827 FMT_INFO.suffix_search_only = true;
828 break;
829 case kpse_ris_format:
830 INIT_FORMAT ("ris", DEFAULT_RISINPUTS, RIS_ENVS);
831 SUFFIXES (".ris");
832 FMT_INFO.suffix_search_only = true;
833 break;
834 case kpse_bltxml_format:
835 INIT_FORMAT ("bltxml", DEFAULT_BLTXMLINPUTS, BLTXML_ENVS);
836 SUFFIXES (".bltxml");
837 FMT_INFO.suffix_search_only = true;
838 break;
839 default:
840 LIB_FATAL1 ("kpse_init_format: Unknown format %d", format);
841 }
842
843 #ifdef KPSE_DEBUG
844 #define MAYBE(member) (FMT_INFO.member ? FMT_INFO.member : "(none)")
845
846 /* Describe the monster we've created. */
847 if (KPATHSEA_DEBUG_P (KPSE_DEBUG_PATHS)) {
848 if (format == kpse_cnf_format)
849 fputs ("\n", stderr); /* always mid-search, so break log output */
850
851 DEBUGF2 ("Search path for %s files (from %s)\n",
852 FMT_INFO.type, FMT_INFO.path_source);
853 DEBUGF1 (" = %s\n", FMT_INFO.path);
854 DEBUGF1 (" before expansion = %s\n", FMT_INFO.raw_path);
855 DEBUGF1 (" application override path = %s\n", MAYBE (override_path));
856 DEBUGF1 (" application config file path = %s\n", MAYBE (client_path));
857 DEBUGF1 (" texmf.cnf path = %s\n", MAYBE (cnf_path));
858 DEBUGF1 (" compile-time path = %s\n", MAYBE (default_path));
859 DEBUGF1 (" environment variables = %s\n", envvar_list);
860 DEBUGF (" default suffixes =");
861 if (FMT_INFO.suffix) {
862 const_string *ext;
863 for (ext = FMT_INFO.suffix; ext && *ext; ext++) {
864 fprintf (stderr, " %s", *ext);
865 }
866 putc ('\n', stderr);
867 } else {
868 fputs (" (none)\n", stderr);
869 }
870 DEBUGF (" other suffixes =");
871 if (FMT_INFO.alt_suffix) {
872 const_string *alt;
873 for (alt = FMT_INFO.alt_suffix; alt && *alt; alt++) {
874 fprintf (stderr, " %s", *alt);
875 }
876 putc ('\n', stderr);
877 } else {
878 fputs (" (none)\n", stderr);
879 }
880 DEBUGF1 (" search only with suffix = %d\n",FMT_INFO.suffix_search_only);
881 DEBUGF1 (" runtime generation program = %s\n", MAYBE (program));
882 DEBUGF (" runtime generation command =");
883 if (FMT_INFO.argv) {
884 const_string *arg;
885 for (arg = FMT_INFO.argv; *arg; arg++) {
886 fprintf (stderr, " %s", *arg);
887 }
888 putc ('\n', stderr);
889 } else {
890 fputs(" (none)\n", stderr);
891 }
892 DEBUGF1 (" program enabled = %d\n", FMT_INFO.program_enabled_p);
893 DEBUGF1 (" program enable level = %d\n", FMT_INFO.program_enable_level);
894 DEBUGF1 (" open files in binary mode = %d\n", FMT_INFO.binmode);
895 DEBUGF1 (" numeric format value = %d\n", format);
896 }
897 #endif /* KPSE_DEBUG */
898
899 return envvar_list;
900 }
901
902 #if defined (KPSE_COMPAT_API)
903 const_string
904 kpse_init_format (kpse_file_format_type format)
905 {
906 return kpathsea_init_format (kpse_def, format);
907 }
908 #endif
909
910
911 /* These are subroutines called twice when finding file, to construct
912 the list of names to search for. */
913
914 /* We don't even use fontmaps any more in practice, they were for things
915 like the lcircle10/lcirc10 name change many years ago, but let's keep
916 the support working nonetheless. */
917
918 static void
919 target_fontmaps (kpathsea kpse, string **target, unsigned *count,
920 const_string name)
921 {
922 const_string *mapped_names = kpathsea_fontmap_lookup (kpse, name);
923
924 if (mapped_names != NULL) {
925 const_string mapped_name;
926 /* We leak mapped_names and its elements, some of the time. */
927 while ((mapped_name = *mapped_names++) != NULL) {
928 (*target)[(*count)] = xstrdup (mapped_name);
929 (*count)++;
930 XRETALLOC ((*target), (*count)+1, string);
931 }
932 }
933 }
934
935
936 /* Possibly add NAME (and any fontmap equivalents) to the string list
937 in TARGET, depending on the various other parameters. */
938
939 static void
940 target_asis_name (kpathsea kpse, string **target, unsigned *count,
941 kpse_file_format_type format,
942 const_string name, boolean use_fontmaps, boolean has_potential_suffix,
943 string has_any_suffix)
944 {
945 (void) has_any_suffix; /* -Wunused */
946 /* Look for the name we've been given, provided non-suffix
947 searches are allowed or the name already includes a suffix. */
948 if (has_potential_suffix || !FMT_INFO.suffix_search_only) {
949 (*target)[(*count)] = xstrdup (name);
950 (*count)++;
951 XRETALLOC ((*target), (*count)+1, string);
952
953 if (use_fontmaps) {
954 target_fontmaps (kpse, target, count, name);
955 }
956 }
957 }
958
959
960 /* Possibly add NAME (and any fontmap equivalents), with any suffixes
961 for this FORMAT appended, to TARGET -- if it doesn't already have one
962 of the potential suffixes for FORMAT. */
963
964 static void
965 target_suffixed_names (kpathsea kpse, string **target, unsigned *count,
966 kpse_file_format_type format,
967 const_string name, boolean use_fontmaps, boolean has_potential_suffix)
968 {
969 const_string *ext;
970 if (has_potential_suffix || !FMT_INFO.suffix) {
971 return;
972 }
973
974 for (ext = FMT_INFO.suffix; *ext; ext++) {
975 string name_with_suffix = concat (name, *ext);
976 (*target)[(*count)] = name_with_suffix;
977 (*count)++;
978 XRETALLOC ((*target), (*count)+1, string);
979
980 if (use_fontmaps) {
981 target_fontmaps (kpse, target, count, name_with_suffix);
982 }
983 }
984 }
985
986 /* Look up a file NAME of type FORMAT, and the given MUST_EXIST. This
987 initializes the path spec for FORMAT if it's the first lookup of that
988 type. Return the filename found, or NULL. This is the most likely
989 thing for clients to call. */
990
991 string
992 kpathsea_find_file (kpathsea kpse, const_string name,
993 kpse_file_format_type format, boolean must_exist)
994 {
995 string *ret_list = kpathsea_find_file_generic (kpse, name, format,
996 must_exist, false);
997 string ret = *ret_list;
998 free (ret_list);
999 return ret;
1000 }
1001
1002 #if defined (KPSE_COMPAT_API)
1003 string
1004 kpse_find_file (const_string name, kpse_file_format_type format,
1005 boolean must_exist)
1006 {
1007 return kpathsea_find_file(kpse_def, name, format, must_exist);
1008 }
1009 #endif
1010
1011 /* As with `kpse_find_file', but also allow passing ALL for the search,
1012 hence we always return a NULL-terminated list. */
1013
1014 string *
1015 kpathsea_find_file_generic (kpathsea kpse, const_string const_name,
1016 kpse_file_format_type format,
1017 boolean must_exist, boolean all)
1018 {
1019 #if defined(_WIN32) && !defined(__MINGW32__)
1020 char tmpbuffer1[512], tmpbuffer2[512];
1021 #endif
1022 string *target, name;
1023 const_string *ext;
1024 unsigned count;
1025 unsigned name_len = 0;
1026 boolean has_potential_suffix = false;
1027 string has_any_suffix = NULL;
1028 string try_std_extension_first = NULL;
1029 boolean use_fontmaps = (format == kpse_tfm_format
1030 || format == kpse_gf_format
1031 || format == kpse_pk_format
1032 || format == kpse_ofm_format);
1033 string *ret = NULL;
1034
1035 /* NAME being NULL is a programming bug somewhere. NAME can be empty,
1036 though; this happens with constructs like `\input\relax'. */
1037 assert (const_name);
1038
1039 if (FMT_INFO.path == NULL)
1040 kpathsea_init_format (kpse, format);
1041
1042 #ifdef KPSE_DEBUG
1043 if (KPATHSEA_DEBUG_P (KPSE_DEBUG_SEARCH))
1044 DEBUGF3 ("kpse_find_file: searching for %s of type %s (from %s)\n",
1045 const_name, FMT_INFO.type, FMT_INFO.path_source);
1046 #endif /* KPSE_DEBUG */
1047
1048 /* Do variable and tilde expansion. */
1049 name = kpathsea_expand (kpse, const_name);
1050
1051 has_any_suffix = strrchr (name, '.');
1052 if (has_any_suffix) {
1053 string p = strchr (has_any_suffix, DIR_SEP);
1054 if (p) {
1055 has_any_suffix = NULL;
1056 }
1057 }
1058
1059 /* Does NAME already end in a possible suffix? */
1060 name_len = strlen (name);
1061 if (FMT_INFO.suffix) {
1062 for (ext = FMT_INFO.suffix; !has_potential_suffix && *ext; ext++) {
1063 unsigned suffix_len = strlen (*ext);
1064 has_potential_suffix = (name_len >= suffix_len
1065 && FILESTRCASEEQ (*ext, name + name_len - suffix_len));
1066 }
1067 }
1068 if (!has_potential_suffix && FMT_INFO.alt_suffix) {
1069 for (ext = FMT_INFO.alt_suffix; !has_potential_suffix && *ext; ext++) {
1070 unsigned suffix_len = strlen (*ext);
1071 has_potential_suffix = (name_len >= suffix_len
1072 && FILESTRCASEEQ (*ext, name + name_len - suffix_len));
1073 }
1074 }
1075
1076 /* Set up list of target names to search for, the order depending on
1077 try_std_extension_first. */
1078 count = 0;
1079 target = XTALLOC1 (string);
1080 try_std_extension_first
1081 = kpathsea_var_value (kpse, "try_std_extension_first");
1082
1083 if (has_any_suffix && !kpse_cnf_p (try_std_extension_first)) {
1084 target_asis_name (kpse, &target, &count, format, name, use_fontmaps,
1085 has_potential_suffix, has_any_suffix);
1086 target_suffixed_names (kpse, &target, &count, format, name, use_fontmaps,
1087 has_potential_suffix);
1088 } else {
1089 target_suffixed_names (kpse, &target, &count, format, name, use_fontmaps,
1090 has_potential_suffix);
1091 target_asis_name (kpse, &target, &count, format, name, use_fontmaps,
1092 has_potential_suffix, has_any_suffix);
1093 }
1094
1095 /* Terminate list. */
1096 target[count] = NULL;
1097
1098 if (try_std_extension_first) {
1099 free (try_std_extension_first);
1100 }
1101
1102 /* Search, trying to minimize disk-pounding. */
1103 ret = kpathsea_path_search_list_generic (kpse, FMT_INFO.path,
1104 target, false, all);
1105
1106 /* Do we need to pound the disk? */
1107 if (! *ret && must_exist) {
1108 for (count = 0; target[count]; count++)
1109 free (target[count]);
1110 count = 0;
1111 /* We look for a subset of the previous set of names, so the
1112 target array is large enough. In particular, we don't pound
1113 the disk for alternate names from the fontmaps. */
1114 if (!has_potential_suffix && FMT_INFO.suffix_search_only) {
1115 for (ext = FMT_INFO.suffix; *ext; ext++)
1116 target[count++] = concat (name, *ext);
1117 }
1118 if (has_potential_suffix || !FMT_INFO.suffix_search_only) {
1119 target[count++] = xstrdup (name);
1120 }
1121 target[count] = NULL;
1122 ret = kpathsea_path_search_list_generic (kpse, FMT_INFO.path,
1123 target, true, all);
1124 }
1125
1126 /* Free the list we created. */
1127 for (count = 0; target[count]; count++)
1128 free (target[count]);
1129 free (target);
1130
1131 /* If nothing was found, call mktex* to create a missing file. Since
1132 this returns a single string, morph it into a list. */
1133 if (! *ret && must_exist) {
1134 ret = XTALLOC (2, string);
1135 ret[0] = kpathsea_make_tex (kpse, format, name);
1136 if (ret[0]) {
1137 ret[1] = NULL;
1138 }
1139 }
1140
1141 free (name);
1142 #if defined(_WIN32) && !defined(__MINGW32__)
1143 if (ret && *ret) {
1144 if (all) {
1145 for (count = 0; ret[count] != NULL; count++) {
1146 strcpy (tmpbuffer2, ret[count]);
1147 if (kpathsea_getlongpath (kpse, tmpbuffer1, tmpbuffer2, 500)) {
1148 if (strlen (tmpbuffer1) > strlen (ret[count]))
1149 ret[count] = realloc (ret[count], strlen (tmpbuffer1) + 1);
1150 strcpy (ret[count], tmpbuffer1);
1151 }
1152 }
1153 } else {
1154 strcpy (tmpbuffer2, *ret);
1155 if (kpathsea_getlongpath (kpse, tmpbuffer1, tmpbuffer2, 500)) {
1156 if (strlen (tmpbuffer1) > strlen (*ret))
1157 *ret = realloc (*ret, strlen (tmpbuffer1) + 1);
1158 strcpy (*ret, tmpbuffer1);
1159 }
1160 }
1161 }
1162 #endif
1163 return ret;
1164 }
1165
1166 #if defined (KPSE_COMPAT_API)
1167 string *
1168 kpse_find_file_generic (const_string name, kpse_file_format_type format,
1169 boolean must_exist, boolean all)
1170 {
1171 return kpathsea_find_file_generic(kpse_def, name, format, must_exist, all);
1172 }
1173 #endif
1174
1175
1176 /* Helper types for kpathsea_name_ok, just below. */
1177 typedef enum ok_type {
1178 ok_reading,
1179 ok_writing
1180 } ok_type;
1181
1182 static const_string ok_type_name[] = {
1183 "reading from",
1184 "writing to"
1185 };
1186
1187
1188 /* Helper subroutine to check if absolute pathname FNAME is acceptable
1189 in CHECKDIR. An absolute pathname is ok only if
1190 CHECKDIR is set, and
1191 CHECKDIR is non-empty, and
1192 CHECKDIR is the beginning of FNAME
1193 (e.g., disallow /somedir/file.tex against /anotherdir), and
1194 either the next character in FNAME is a directory separator
1195 (e.g., disallow /somedirx/file.tex against /somedir).
1196 or we're at the end of CHECKDIR
1197 (e.g., allow /somedir against /somedir, without requiring /somedir/).
1198
1199 This last case, of allowing FNAME == CHECKDIR, comes up with LuaTeX,
1200 where we might want to lfs.mkdir(TEXMFVAR) within Lua, not
1201 necessarily with a trailing /, although that suffices. */
1202
1203 static boolean
1204 abs_fname_ok (const_string fname, const_string checkdir)
1205 {
1206 return
1207 checkdir /* checkdir must be non-null */
1208 && *checkdir != '\0' /* checkdir must be non-empty */
1209 && fname == strstr (fname, checkdir) /* fname must begin checkdir */
1210 && (IS_DIR_SEP (fname[strlen (checkdir)]) /* and be followed by / */
1211 || strlen (fname) == strlen (checkdir) /* or be the whole string */
1212 );
1213 }
1214
1215 /* Here is the general internal subroutine, kpathsea_name_ok, for
1216 checking if a filename is acceptable to open for reading or writing.
1217 This is string-based, not file-based, and does not look on the filesystem.
1218 Analogous implementations in minted and memoize do check for the
1219 actual file, not just the filename as a string.
1220
1221 Parameters:
1222 kpse - the usual structure.
1223 fname - the filename to check.
1224 check_var - the config variable to check, openin_any or openout_any.
1225 default_choice - if the variable is not set, one of the settings below.
1226 action - what to check for: either ok_reading or ok_writing.
1227 silent - whether to write a message to stderr if not ok.
1228 extended - whether to also allow TEXMF[SYS]VAR; see comments below for more.
1229
1230 As of 2026, if ACTION is ok_reading, we simply return true, that is,
1231 we allow anything. A specific example as to why:
1232 Because this function is string-based, with openin_any=p,
1233 \input /usr/local/texlive/2025/texmf-dist/tex/plain/knuth-lib/story.tex
1234 will fail because it's an absolute filename, even though
1235 \input story
1236 finds that same file. We would have to implement completely
1237 different to make this work as expected. Meanwhile, no one seems to
1238 have any requirement for restricted reading. So skip it.
1239 More comments at openin_any in texmf.cnf about this.
1240
1241 The public functions below (and declared in tex-file.h), provide
1242 convenience calls with defaults for the various combinations.
1243
1244 The general idea is described in the Kpathsea manual,
1245 https://tug.org/texinfohtml/kpathsea.html#Calling-sequence.
1246 */
1247
1248
1249 static boolean
1250 kpathsea_name_ok (kpathsea kpse, const_string fname, const_string check_var,
1251 const_string default_choice, ok_type action,
1252 boolean silent, boolean extended)
1253 {
1254 const_string open_choice;
1255 const_string expanded_fname = NULL;
1256
1257 /* As mentioned above, as of 2026, allow anything for reading.
1258 See more comments in texmf.cnf. */
1259 if (action == ok_reading) {
1260 return true;
1261 }
1262
1263 /* We distinguish three cases:
1264 'a' (any) allows any file to be opened.
1265 'r' (restricted) means disallowing special filenames.
1266 'p' (paranoid) means being more paranoid: disallowing special
1267 filenames, and restricting files to be a relative
1268 name ("mydoc.tex", "story.tex") that might ultimately
1269 be found along a path,
1270 or $TEXMFOUTPUT or $TEXMF_OUTPUT_DIRECTORY;
1271 or, only if EXTENDED is true, $TEXMFVAR and $TEXMFSYSVAR.
1272 We default to "paranoid" for writing.
1273
1274 The above paranoia works ok for output files, which is the
1275 important case.
1276
1277 The above checks on TEXMFVAR and TEXMFSYSVAR are the only places in
1278 all of Kpathsea where the "tree" variables set in texmf.cnf are
1279 embedded. It would be nicer if we didn't have to.
1280
1281 This function contains several return and goto statements, be careful.
1282 Paranoia originally supplied by Charles Karney. */
1283
1284 open_choice = kpathsea_var_value (kpse, check_var);
1285 if (!open_choice)
1286 open_choice = default_choice;
1287
1288 /* If setting is a(nything), we're done. */
1289 if (*open_choice == 'a' || *open_choice == 'y' || *open_choice == '1')
1290 return true;
1291
1292 /* Now we need to expand FNAME before doing the absolute-path checks,
1293 since TEXMFVAR is typically ~/.texliveYYYY, and we want to allow it. */
1294 expanded_fname = kpathsea_expand (kpse, fname);
1295
1296 #if !defined(MSDOS) && !defined(_WIN32)
1297 { /* On non-Windows ... */
1298 /* Disallow .rhosts, .login, .ssh/, ..somefile, ..somedir/somefile,
1299 etc. Specially allow /foo/.whatever if EXTENDED, since it
1300 might match against
1301 TEXMF[SYS]VAR below. */
1302 const_string q;
1303 const_string qq = fname;
1304 while ((q = strchr (qq, '.'))) { /* at each dot */
1305 if ((q == fname || IS_DIR_SEP (*(q - 1))) /* start or / precedes dot? */
1306 && !IS_DIR_SEP (*(q + 1)) /* ok if /./ */
1307 && !(*(q + 1) == '.' && IS_DIR_SEP (*(q + 2))) /* ok if /../ */
1308 && !(extended && kpathsea_absolute_p (kpse, expanded_fname, false))
1309 /* Don't quit if EXTENDED and the input is absolute,
1310 because we want to allow TEXMFVAR=~/.texliveYYYY. This
1311 is safe because absolute input paths will be checked
1312 below, and relative input paths will still be rejected here.
1313
1314 This does not allow the case where TEXMFVAR=.foo and
1315 the input is .foo/bar, which in principle should be
1316 accepted, but since that's not useful in practice,
1317 let's not worry about it. */
1318 ) {
1319 goto not_ok;
1320 }
1321 qq = q + 1;
1322 }
1323 }
1324 #else /* !MSDOS && !_WIN32 */
1325 /* Other OSs don't have special names? */
1326 #endif /* !MSDOS && !_WIN32 */
1327
1328 /* If setting is only r(estricted), we're done. (Not a useful setting
1329 in practice, but no reason to take it out now.) */
1330 if (*open_choice == 'r' || *open_choice == 'n' || *open_choice == '0')
1331 return true;
1332
1333 /* Continuing on with p(aranoid) checks. */
1334 if (kpathsea_absolute_p (kpse, expanded_fname, false)) {
1335 /* We'll check TEXMF_OUTPUT_DIRECTORY first. This must be an
1336 environment variable, not a configuration file setting. */
1337 const_string texmfoutdir = getenv ("TEXMF_OUTPUT_DIRECTORY");
1338 if (!abs_fname_ok (expanded_fname, texmfoutdir)) {
1339 /* That failed. Next, check TEXMFOUTPUT, but this can be in the
1340 configuration file (i.e., call kpse_var_value instead of getenv). */
1341 const_string texmfoutput = kpathsea_var_value (kpse, "TEXMFOUTPUT");
1342 if (!abs_fname_ok (expanded_fname, texmfoutput)) {
1343 /* That failed too. If `extended' is set, try TEXMFVAR and
1344 TEXMFSYSVAR. */
1345 if (extended) {
1346 const_string texmfvar = kpathsea_var_value (kpse, "TEXMFVAR");
1347 if (!abs_fname_ok (expanded_fname, texmfvar)) {
1348 const_string texmfsysvar
1349 = kpathsea_var_value (kpse, "TEXMFSYSVAR");
1350 if (!abs_fname_ok (expanded_fname, texmfsysvar)) {
1351 goto not_ok; /* nothing left to check. */
1352 }
1353 }
1354 } else {
1355 goto not_ok; /* not extended */
1356 }
1357 }
1358 }
1359 }
1360 /* else {
1361 fprintf (stderr, "kpathsea_name_ok: not absolute %s\n", expanded_fname);
1362 }
1363 fprintf (stderr, "passed check abs_fname_ok(%s)\n", expanded_fname);
1364 */
1365
1366 /* For all pathnames, we disallow "../" at the beginning or "/../"
1367 anywhere. We need to check this for absolute paths, so fall through
1368 from above if the absolute check succeeded.
1369
1370 We don't try to resolve relative path elements and see if we end up
1371 in an acceptable directory, but rather simply consider the value as
1372 a string. (It's ok if other programs do such resolution, though.)
1373
1374 Here it does not matter in practice if we check against the
1375 original or expanded FNAME, so just use the original. */
1376 if (fname[0] == '.' && fname[1] == '.' && IS_DIR_SEP(fname[2]))
1377 goto not_ok;
1378 else {
1379 /* Check for "/../". Since more than one character can be matched
1380 by IS_DIR_SEP, we cannot use "/../" itself. */
1381 const_string dotpair = strstr (fname, "..");
1382 while (dotpair) {
1383 /* If dotpair[2] == DIR_SEP, then dotpair[-1] is well-defined,
1384 because the "../" case was handled above. */
1385 if (IS_DIR_SEP (dotpair[2]) && IS_DIR_SEP (dotpair[-1]))
1386 goto not_ok;
1387 /* Continue after the dotpair. */
1388 dotpair = strstr (dotpair+2, "..");
1389 }
1390 }
1391
1392 /* We passed all tests. */
1393 if (expanded_fname) free ((void *) expanded_fname);
1394 return true;
1395
1396 not_ok: /* Some test failed. */
1397 if (!silent)
1398 fprintf (stderr, "\n%s: Not %s %s (%s = %s; %sextended check).\n",
1399 kpse->invocation_name, ok_type_name[action], fname,
1400 check_var, open_choice, extended ? "" : "no ");
1401 if (expanded_fname) free ((void *) expanded_fname);
1402 return false;
1403 }
1404
1405 /* For input, default to anything being ok. */
1406
1407 boolean
1408 kpathsea_in_name_ok_silent (kpathsea kpse, const_string fname)
1409 {
1410 /* For input default to all. */
1411 return
1412 kpathsea_name_ok (kpse, fname, "openin_any", "a", ok_reading, true, false);
1413 }
1414
1415 boolean
1416 kpathsea_in_name_ok (kpathsea kpse, const_string fname)
1417 {
1418 return
1419 kpathsea_name_ok (kpse, fname, "openin_any", "a", ok_reading, false, false);
1420 }
1421
1422 boolean
1423 kpathsea_in_name_ok_silent_extended (kpathsea kpse, const_string fname)
1424 {
1425 return
1426 kpathsea_name_ok (kpse, fname, "openin_any", "a", ok_reading, true, true);
1427 }
1428
1429 boolean
1430 kpathsea_in_name_ok_extended (kpathsea kpse, const_string fname)
1431 {
1432 return
1433 kpathsea_name_ok (kpse, fname, "openin_any", "a", ok_reading, false, true);
1434 }
1435
1436
1437 /* Output name checks. */
1438 #if defined(WIN32) || defined(__CYGWIN__)
1439 static int
1440 Isspace (char c)
1441 {
1442 return (c == ' ' || c == '\t');
1443 }
1444
1445 static boolean
1446 executable_filep (kpathsea kpse, const_string fname, boolean silent)
1447 {
1448 const_string fn;
1449 string p, q, base;
1450 string *pp;
1451
1452 /* check openout_any */
1453 p = kpathsea_var_value (kpse, "openout_any");
1454 if (p && *p == 'p') {
1455 free (p);
1456 /* get base name
1457 we cannot use xbasename() for abnormal names.
1458 */
1459 p = strrchr (fname, '/');
1460 if (p)
1461 fn = p + 1;
1462 else
1463 fn = fname;
1464 p = strrchr (fn, '\\');
1465 if (p)
1466 fn = p + 1;
1467 base = xstrdup (fn);
1468 #if defined(__CYGWIN__)
1469 for (p = base; *p; p++)
1470 *p = TOLOWER (*p);
1471 p = base;
1472 #else
1473 p = (char *) strlwr (base);
1474 #endif
1475 for (q = p + strlen (p) - 1;
1476 (q >= p) && ((*q == '.') || (Isspace (*q))); q--) {
1477 *q = '\0'; /* remove trailing '.' , ' ' and '\t' */
1478 }
1479 q = strrchr (p, '.'); /* get extension part */
1480 pp = kpse->suffixlist;
1481 if (pp && q) {
1482 while (*pp) {
1483 if (!strcmp (q, *pp)) {
1484 if (!silent)
1485 fprintf (stderr, "\n%s: Forbidden to open for writing\n", fname);
1486 free (base);
1487 return true;
1488 }
1489 pp++;
1490 }
1491 }
1492 free (base);
1493 } else if (p) {
1494 free (p);
1495 }
1496 return false;
1497 }
1498 #endif /* WIN32 || __CYGWIN__ */
1499
1500 static boolean
1501 kpathsea_out_name_ok_1 (kpathsea kpse, const_string fname,
1502 boolean silent, boolean extended)
1503 {
1504 #if defined(WIN32) || defined(__CYGWIN__)
1505 /* Output of an executable file is restricted on Windows */
1506 if (executable_filep (kpse, fname, silent))
1507 return false;
1508 #endif /* WIN32 || __CYGWIN__ */
1509 /* For output, default to paranoid. */
1510 return kpathsea_name_ok (kpse, fname, "openout_any", "p", ok_writing,
1511 silent, extended);
1512 }
1513
1514 boolean
1515 kpathsea_out_name_ok_silent (kpathsea kpse, const_string fname)
1516 {
1517 return kpathsea_out_name_ok_1 (kpse, fname, true, false);
1518 }
1519
1520 boolean
1521 kpathsea_out_name_ok (kpathsea kpse, const_string fname)
1522 {
1523 return kpathsea_out_name_ok_1 (kpse, fname, false, false);
1524 }
1525
1526 boolean
1527 kpathsea_out_name_ok_silent_extended (kpathsea kpse, const_string fname)
1528 {
1529 return kpathsea_out_name_ok_1 (kpse, fname, true, true);
1530 }
1531
1532 boolean
1533 kpathsea_out_name_ok_extended (kpathsea kpse, const_string fname)
1534 {
1535 return kpathsea_out_name_ok_1 (kpse, fname, false, true);
1536 }
1537
1538 #if defined (KPSE_COMPAT_API)
1539 boolean kpse_in_name_ok (const_string fname)
1540 { return kpathsea_in_name_ok (kpse_def, fname); }
1541
1542 boolean kpse_out_name_ok (const_string fname)
1543 { return kpathsea_out_name_ok (kpse_def, fname); }
1544
1545 boolean kpse_in_name_ok_silent (const_string fname)
1546 { return kpathsea_in_name_ok_silent (kpse_def, fname); }
1547
1548 boolean kpse_out_name_ok_silent (const_string fname)
1549 { return kpathsea_out_name_ok_silent (kpse_def, fname); }
1550
1551 boolean kpse_in_name_ok_extended (const_string fname)
1552 { return kpathsea_in_name_ok_extended (kpse_def, fname); }
1553
1554 boolean kpse_out_name_ok_extended (const_string fname)
1555 { return kpathsea_out_name_ok_extended (kpse_def, fname); }
1556
1557 boolean kpse_in_name_ok_silent_extended (const_string fname)
1558 { return kpathsea_in_name_ok_silent_extended (kpse_def, fname); }
1559
1560 boolean kpse_out_name_ok_silent_extended (const_string fname)
1561 { return kpathsea_out_name_ok_silent_extended (kpse_def, fname); }
1562
1563 #endif /* KPSE_COMPAT_API */
1564
1565
1566
1567 /* Open NAME along the search path for TYPE for reading and return the
1568 resulting file, or exit with an error message. */
1569
1570 FILE *
1571 kpathsea_open_file (kpathsea kpse, const_string name,
1572 kpse_file_format_type type)
1573 {
1574 string fullname = kpathsea_find_file (kpse, name, type, true);
1575 const_string mode = kpse->format_info[type].binmode
1576 ? FOPEN_RBIN_MODE
1577 : FOPEN_R_MODE;
1578 FILE *f = fullname ? fopen (fullname, mode) : NULL;
1579 if (!f) {
1580 if (fullname) {
1581 perror (fullname);
1582 exit (1);
1583 } else {
1584 LIB_FATAL2 ("%s file `%s' not found", kpse->format_info[type].type, name);
1585 }
1586 }
1587
1588 return f;
1589 }
1590
1591 #if defined (KPSE_COMPAT_API)
1592 FILE *
1593 kpse_open_file (const_string name, kpse_file_format_type type)
1594 {
1595 return kpathsea_open_file(kpse_def, name, type);
1596 }
1597 #endif
1598
1599
1600 /* When using the %&<format> construct, we'd like to use the paths for
1601 that format, rather than those for the name we were called with.
1602 Of course this happens after various initializations have been
1603 performed, so we have this function to force the issue. Note that
1604 the paths for kpse_cnf_format and kpse_db_format are not cleared.
1605
1606 This function is defined here, and not in progname.c, because it
1607 needs format_info, and would cause all of tex-file to be pulled
1608 in by programs that do not need it. */
1609
1610 void
1611 kpathsea_reset_program_name (kpathsea kpse, const_string progname)
1612 {
1613 int i;
1614
1615 /* It is a fatal error for either of these to be NULL. */
1616 assert (progname && kpse->program_name);
1617 /* Do nothing if the name is unchanged. */
1618 if (STREQ(kpse->program_name, progname))
1619 return;
1620
1621 free (kpse->program_name);
1622 kpse->program_name = xstrdup (progname);
1623 kpathsea_xputenv (kpse, "progname", kpse->program_name);
1624
1625 /* Go through all paths ... */
1626 for (i = 0; i != kpse_last_format; ++i) {
1627 /* Do not erase the cnf or db paths. This means that the filename
1628 database is not rebuilt, nor are different configuration files
1629 searched. The alternative is to tolerate a memory leak of up
1630 to 100k if this function is called. */
1631 if (i == kpse_cnf_format || i == kpse_db_format)
1632 continue;
1633 /* Wipe the path (it is tested) and the cnf_path because their
1634 values may differ with the new program name. */
1635 if (kpse->format_info[i].path != NULL) {
1636 free (kpse->format_info[i].path);
1637 kpse->format_info[i].path = NULL;
1638 }
1639 /* We cannot free cnf_path: it points into the cnf hash. */
1640 if (kpse->format_info[i].cnf_path != NULL) {
1641 kpse->format_info[i].cnf_path = NULL;
1642 }
1643 /* We do not wipe the override_path at this point, though arguably
1644 we should provide new values. It is not likely to matter for
1645 the programs that call this function. */
1646 }
1647 }
1648
1649 #if defined (KPSE_COMPAT_API)
1650 void
1651 kpse_reset_program_name (const_string progname)
1652 {
1653 kpathsea_reset_program_name (kpse_def, progname);
1654 }
1655 #endif