1 /* tex-file.c: high-level file searching by format.
3 Copyright 1993, 1994, 1995, 1996, 1997, 2007, 2008 Karl Berry.
4 Copyright 1998-2005 Olaf Weber.
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.
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.
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/>. */
19 #include <kpathsea/config.h>
21 #include <kpathsea/c-fopen.h>
22 #include <kpathsea/c-pathch.h>
23 #include <kpathsea/c-vararg.h>
24 #include <kpathsea/cnf.h>
25 #include <kpathsea/concatn.h>
26 #include <kpathsea/default.h>
27 #include <kpathsea/expand.h>
28 #include <kpathsea/fontmap.h>
29 #include <kpathsea/paths.h>
30 #include <kpathsea/pathsearch.h>
31 #include <kpathsea/tex-file.h>
32 #include <kpathsea/tex-make.h>
33 #include <kpathsea/variable.h>
37 const_string kpse_fallback_font = NULL;
38 const_string kpse_fallback_resolutions_string = NULL;
39 unsigned *kpse_fallback_resolutions = NULL;
40 kpse_format_info_type kpse_format_info[kpse_last_format];
42 /* These are not in the structure
43 because it's annoying to initialize lists in C. */
44 #define GF_ENVS "GFFONTS", GLYPH_ENVS
45 #define PK_ENVS "PKFONTS", "TEXPKS", GLYPH_ENVS
46 #define GLYPH_ENVS "GLYPHFONTS", "TEXFONTS"
47 #define TFM_ENVS "TFMFONTS", "TEXFONTS"
48 #define AFM_ENVS "AFMFONTS", "TEXFONTS"
49 #define BASE_ENVS "MFBASES", "TEXMFINI"
50 #define BIB_ENVS "BIBINPUTS", "TEXBIB"
51 #define BST_ENVS "BSTINPUTS"
52 #define CNF_ENVS "TEXMFCNF"
53 #define DB_ENVS "TEXMFDBS"
54 #define FMT_ENVS "TEXFORMATS", "TEXMFINI"
55 #define FONTMAP_ENVS "TEXFONTMAPS", "TEXFONTS"
56 #define MEM_ENVS "MPMEMS", "TEXMFINI"
57 #define MF_ENVS "MFINPUTS"
58 #define MFPOOL_ENVS "MFPOOL", "TEXMFINI"
59 #define MFT_ENVS "MFTINPUTS"
60 #define MP_ENVS "MPINPUTS"
61 #define MPPOOL_ENVS "MPPOOL", "TEXMFINI"
62 #define MPSUPPORT_ENVS "MPSUPPORT"
63 #define OCP_ENVS "OCPINPUTS"
64 #define OFM_ENVS "OFMFONTS", "TEXFONTS"
65 #define OPL_ENVS "OPLFONTS", "TEXFONTS"
66 #define OTP_ENVS "OTPINPUTS"
67 #define OVF_ENVS "OVFFONTS", "TEXFONTS"
68 #define OVP_ENVS "OVPFONTS", "TEXFONTS"
69 #define PICT_ENVS "TEXPICTS", TEX_ENVS
70 #define TEX_ENVS "TEXINPUTS"
71 #define TEXDOC_ENVS "TEXDOCS"
72 #define TEXPOOL_ENVS "TEXPOOL", "TEXMFINI"
73 #define TEXSOURCE_ENVS "TEXSOURCES"
74 #define TEX_PS_HEADER_ENVS "TEXPSHEADERS", "PSHEADERS"
75 #define TROFF_FONT_ENVS "TRFONTS"
76 #define TYPE1_ENVS "T1FONTS", "T1INPUTS", "TEXFONTS", TEX_PS_HEADER_ENVS
77 #define VF_ENVS "VFFONTS", "TEXFONTS"
78 #define DVIPS_CONFIG_ENVS "TEXCONFIG"
79 #define IST_ENVS "TEXINDEXSTYLE", "INDEXSTYLE"
80 #define TRUETYPE_ENVS "TTFONTS", "TEXFONTS"
81 #define TYPE42_ENVS "T42FONTS", "TEXFONTS"
82 #define WEB2C_ENVS "WEB2C"
83 #define MISCFONTS_ENVS "MISCFONTS", "TEXFONTS"
84 #define WEB_ENVS "WEBINPUTS"
85 #define CWEB_ENVS "CWEBINPUTS"
86 #define ENC_ENVS "ENCFONTS", "TEXFONTS"
87 #define CMAP_ENVS "CMAPFONTS", "TEXFONTS"
88 #define SFD_ENVS "SFDFONTS", "TEXFONTS"
89 #define OPENTYPE_ENVS "OPENTYPEFONTS", "TEXFONTS"
90 #define PDFTEXCONFIG_ENVS "PDFTEXCONFIG"
91 #define LIG_ENVS "LIGFONTS", "TEXFONTS"
92 #define TEXMFSCRIPTS_ENVS "TEXMFSCRIPTS"
93 #define LUA_ENVS "LUAINPUTS"
95 /* The compiled-in default list, DEFAULT_FONT_SIZES, is intended to be
96 set from the command line (presumably via the Makefile). */
98 #ifndef DEFAULT_FONT_SIZES
99 #define DEFAULT_FONT_SIZES ""
103 kpse_init_fallback_resolutions P1C(string, envvar)
106 const_string size_var = ENVVAR (envvar, "TEXSIZES");
107 string size_str = getenv (size_var);
108 unsigned *last_resort_sizes = NULL;
109 unsigned size_count = 0;
110 const_string default_sizes = kpse_fallback_resolutions_string
111 ? kpse_fallback_resolutions_string
112 : DEFAULT_FONT_SIZES;
113 string size_list = kpse_expand_default (size_str, default_sizes);
115 /* Initialize the list of last-resort sizes. */
116 for (size = kpse_path_element (size_list); size != NULL;
117 size = kpse_path_element (NULL))
120 if (! *size) /* Skip empty elements. */
124 if (size_count && s < last_resort_sizes[size_count - 1]) {
125 WARNING1 ("kpathsea: last resort size %s not in ascending order, ignored",
129 XRETALLOC (last_resort_sizes, size_count, unsigned);
130 last_resort_sizes[size_count - 1] = atoi (size);
134 /* Add a zero to mark the end of the list. */
136 XRETALLOC (last_resort_sizes, size_count, unsigned);
137 last_resort_sizes[size_count - 1] = 0;
141 kpse_fallback_resolutions = last_resort_sizes;
144 /* We should be able to set the program arguments in the same way. Not
145 to mention the path values themselves. */
148 kpse_set_program_enabled P3C(kpse_file_format_type, fmt, boolean, value,
149 kpse_src_type, level)
151 kpse_format_info_type *f = &kpse_format_info[fmt];
152 if (level >= f->program_enable_level) {
153 f->program_enabled_p = value;
154 f->program_enable_level = level;
159 /* Web2c and kpsewhich have command-line options to set this stuff. May
160 as well have a common place. */
163 kpse_maketex_option P2C(const_string, fmtname, boolean, value)
165 kpse_file_format_type fmt = kpse_last_format;
167 /* Trying to match up with the suffix lists unfortunately doesn't work
168 well, since that would require initializing the formats. */
169 /* FIXME: Currently the function silently ignores unrecognized arguments.*/
170 if (FILESTRCASEEQ (fmtname, "pk")) {
171 fmt = kpse_pk_format;
172 } else if (FILESTRCASEEQ (fmtname, "mf")) {
173 fmt = kpse_mf_format;
174 } else if (FILESTRCASEEQ (fmtname, "tex")) {
175 fmt = kpse_tex_format;
176 } else if (FILESTRCASEEQ (fmtname, "tfm")) {
177 fmt = kpse_tfm_format;
178 } else if (FILESTRCASEEQ (fmtname, "fmt")) {
179 fmt = kpse_fmt_format;
180 } else if (FILESTRCASEEQ (fmtname, "ofm")) {
181 fmt = kpse_ofm_format;
182 } else if (FILESTRCASEEQ (fmtname, "ocp")) {
183 fmt = kpse_ocp_format;
185 if (fmt != kpse_last_format) {
186 kpse_set_program_enabled (fmt, value, kpse_src_cmdline);
190 /* Macro subroutines for `init_path'. TRY_ENV checks if an envvar ENAME
191 is set and non-null, and sets var to ENAME if so. */
192 #define TRY_ENV(ename) do { \
193 string evar = ename; \
196 /* And EXPAND_DEFAULT calls kpse_expand_default on try_path and the
197 present info->path. */
198 #define EXPAND_DEFAULT(try_path, source_string) \
200 info->raw_path = try_path; \
201 info->path = kpse_expand_default (try_path, info->path); \
202 info->path_source = source_string; \
205 /* Find the final search path to use for the format entry INFO, given
206 the compile-time default (DEFAULT_PATH), and the environment
207 variables to check (the remaining arguments, terminated with NULL).
208 We set the `path' and `path_source' members of INFO. The
209 `client_path' member must already be set upon entry. */
212 init_path PVAR2C(kpse_format_info_type *, info, const_string, default_path, ap)
215 string env_value = NULL;
218 info->default_path = default_path;
220 /* First envvar that's set to a nonempty value will exit the loop. If
221 none are set, we want the first cnf entry that matches. Find the
222 cnf entries simultaneously, to avoid having to go through envvar
223 list twice -- because of the PVAR?C macro, that would mean having
224 to create a str_list and then use it twice. Yuck. */
225 while ((env_name = va_arg (ap, string)) != NULL) {
226 /* Since sh doesn't like envvar names with `.', check PATH_prog
227 as well as PATH.prog. */
230 string evar = concat3 (env_name, ".", kpse_program_name);
231 env_value = getenv (evar);
232 if (env_value && *env_value) {
237 evar = concat3 (env_name, "_", kpse_program_name);
238 env_value = getenv (evar);
239 if (env_value && *env_value) {
243 /* Try simply PATH. */
244 env_value = getenv (env_name);
245 if (env_value && *env_value) {
252 /* If we are initializing the cnf path, don't try to get any
253 values from the cnf files; that's infinite loop time. */
254 if (!info->cnf_path && info != &kpse_format_info[kpse_cnf_format])
255 info->cnf_path = kpse_cnf_get (env_name);
257 if (var && info->cnf_path)
262 /* Expand any extra :'s. For each level, we replace an extra : with
263 the path at the next lower level. For example, an extra : in a
264 user-set envvar should be replaced with the path from the cnf file.
265 things are complicated because none of the levels above the very
266 bottom are guaranteed to exist. */
268 /* Assume we can reliably start with the compile-time default. */
269 info->path = info->raw_path = info->default_path;
270 info->path_source = "compile-time paths.h";
272 /* Translate ';' in the envvar into ':' if that's our ENV_SEP. */
273 if (IS_ENV_SEP(':') && env_value) {
275 env_value = xstrdup(env_value); /* Freed below. */
276 for (loc = env_value; *loc; loc++) {
282 EXPAND_DEFAULT (info->cnf_path, "texmf.cnf");
283 EXPAND_DEFAULT (info->client_path, "program config file");
285 EXPAND_DEFAULT (env_value, concat (var, " environment variable"));
286 EXPAND_DEFAULT (info->override_path, "application override variable");
287 info->path = kpse_brace_expand (info->path);
289 /* Free the copied env_value. */
290 if (IS_ENV_SEP(':') && env_value)
295 /* Some file types have more than one suffix, and sometimes it is
296 convenient to modify the list of searched suffixes. */
299 kpse_set_suffixes PVAR2C(kpse_file_format_type, format,
300 boolean, alternate, ap)
307 list = &kpse_format_info[format].alt_suffix;
309 list = &kpse_format_info[format].suffix;
312 while ((s = va_arg (ap, string)) != NULL) {
314 XRETALLOC (*list, count + 1, const_string);
315 (*list)[count - 1] = s;
318 (*list)[count] = NULL;
322 /* The path spec we are defining, one element of the global array. */
323 #define FMT_INFO kpse_format_info[format]
324 /* Call kpse_set_add_suffixes. */
325 #define SUFFIXES(args) kpse_set_suffixes(format, false, args, NULL)
326 #define ALT_SUFFIXES(args) kpse_set_suffixes(format, true, args, NULL)
328 /* Call `init_path', including appending the trailing NULL to the envvar
329 list. Also initialize the fields not needed in setting the path. */
330 #define INIT_FORMAT(text, default_path, envs) \
331 FMT_INFO.type = text; \
332 init_path (&FMT_INFO, default_path, envs, NULL)
335 /* A few file types allow for runtime generation by an external program.
336 kpse_init_prog may have already initialized it (the `program'
337 member). Here we allow people to turn it off or on in the config
338 file, by setting the variable whose name is the uppercasified program
342 init_maketex PVAR2C(kpse_file_format_type, fmt, const_string, dflt_prog, ap)
344 kpse_format_info_type *f = &kpse_format_info[fmt];
345 const_string prog = f->program ? f->program : dflt_prog; /* mktexpk */
346 string PROG = uppercasify (prog); /* MKTEXPK */
347 string progval = kpse_var_value (PROG); /* ENV/cnf{"MKTEXPK"} */
350 /* Doesn't hurt to always set this info. */
353 /* Set up the argument vector. */
355 f->argv = XTALLOC(2, const_string);
356 f->argv[f->argc++] = dflt_prog;
357 while ((arg = va_arg (ap, string)) != NULL) {
359 XRETALLOC (f->argv, f->argc + 1, const_string);
360 f->argv[f->argc - 1] = arg;
362 f->argv[f->argc] = NULL;
364 if (progval && *progval) {
365 /* This might actually be from an environment variable value, but in
366 that case, we'll have previously set it from kpse_init_prog. */
367 kpse_set_program_enabled (fmt, *progval == '1', kpse_src_client_cnf);
373 /* We need this twice, so ... */
374 #define MKTEXPK_ARGS \
375 "--mfmode","$MAKETEX_MODE",\
376 "--bdpi","$MAKETEX_BASE_DPI",\
377 "--mag","$MAKETEX_MAG",\
378 "--dpi","$KPATHSEA_DPI",\
382 remove_dbonly P1C(const_string, path)
384 string ret = XTALLOC(strlen (path) + 1, char), q=ret;
386 boolean new_elt=true;
389 if (new_elt && *p && *p == '!' && *(p+1) == '!')
392 new_elt = (*p == ENV_SEP);
400 /* Initialize everything for FORMAT. */
403 kpse_init_format P1C(kpse_file_format_type, format)
405 /* If we get called twice, don't redo all the work. */
407 return FMT_INFO.path;
410 { /* We might be able to avoid repeating `gf' or whatever so many
411 times with token pasting, but it doesn't seem worth it. */
413 INIT_FORMAT ("gf", DEFAULT_GFFONTS, GF_ENVS);
415 FMT_INFO.suffix_search_only = true;
416 FMT_INFO.binmode = true;
419 init_maketex (format, "mktexpk", MKTEXPK_ARGS);
420 INIT_FORMAT ("pk", DEFAULT_PKFONTS, PK_ENVS);
422 FMT_INFO.suffix_search_only = true;
423 FMT_INFO.binmode = true;
425 case kpse_any_glyph_format:
426 init_maketex (format, "mktexpk", MKTEXPK_ARGS);
427 INIT_FORMAT ("bitmap font", DEFAULT_GLYPHFONTS, GLYPH_ENVS);
428 FMT_INFO.suffix_search_only = true;
429 FMT_INFO.binmode = true;
431 case kpse_tfm_format:
432 /* Must come before kpse_ofm_format. */
433 init_maketex (format, "mktextfm", NULL);
434 INIT_FORMAT ("tfm", DEFAULT_TFMFONTS, TFM_ENVS);
436 FMT_INFO.suffix_search_only = true;
437 FMT_INFO.binmode = true;
439 case kpse_afm_format:
440 INIT_FORMAT ("afm", DEFAULT_AFMFONTS, AFM_ENVS);
443 case kpse_base_format:
444 init_maketex (format, "mktexfmt", NULL);
445 INIT_FORMAT ("base", DEFAULT_MFBASES, BASE_ENVS);
447 FMT_INFO.binmode = true;
449 case kpse_bib_format:
450 INIT_FORMAT ("bib", DEFAULT_BIBINPUTS, BIB_ENVS);
452 FMT_INFO.suffix_search_only = true;
454 case kpse_bst_format:
455 INIT_FORMAT ("bst", DEFAULT_BSTINPUTS, BST_ENVS);
458 case kpse_cnf_format:
459 INIT_FORMAT ("cnf", DEFAULT_TEXMFCNF, CNF_ENVS);
463 INIT_FORMAT ("ls-R", DEFAULT_TEXMFDBS, DB_ENVS);
464 #define LSR_SUFFIXES "ls-R", "ls-r"
465 SUFFIXES (LSR_SUFFIXES);
466 FMT_INFO.path = remove_dbonly (FMT_INFO.path);
468 case kpse_fmt_format:
469 init_maketex (format, "mktexfmt", NULL);
470 INIT_FORMAT ("fmt", DEFAULT_TEXFORMATS, FMT_ENVS);
472 FMT_INFO.binmode = true;
474 case kpse_fontmap_format:
475 INIT_FORMAT ("map", DEFAULT_TEXFONTMAPS, FONTMAP_ENVS);
478 case kpse_mem_format:
479 init_maketex (format, "mktexfmt", NULL);
480 INIT_FORMAT ("mem", DEFAULT_MPMEMS, MEM_ENVS);
482 FMT_INFO.binmode = true;
485 init_maketex (format, "mktexmf", NULL);
486 INIT_FORMAT ("mf", DEFAULT_MFINPUTS, MF_ENVS);
489 case kpse_mft_format:
490 INIT_FORMAT ("mft", DEFAULT_MFTINPUTS, MFT_ENVS);
493 case kpse_mfpool_format:
494 INIT_FORMAT ("mfpool", DEFAULT_MFPOOL, MFPOOL_ENVS);
498 INIT_FORMAT ("mp", DEFAULT_MPINPUTS, MP_ENVS);
501 case kpse_mppool_format:
502 INIT_FORMAT ("mppool", DEFAULT_MPPOOL, MPPOOL_ENVS);
505 case kpse_mpsupport_format:
506 INIT_FORMAT ("MetaPost support", DEFAULT_MPSUPPORT, MPSUPPORT_ENVS);
508 case kpse_ocp_format:
509 init_maketex (format, "mkocp", NULL);
510 INIT_FORMAT ("ocp", DEFAULT_OCPINPUTS, OCP_ENVS);
512 FMT_INFO.suffix_search_only = true;
513 FMT_INFO.binmode = true;
515 case kpse_ofm_format:
516 init_maketex (format, "mkofm", NULL);
517 INIT_FORMAT ("ofm", DEFAULT_OFMFONTS, OFM_ENVS);
518 #define OFM_SUFFIXES ".ofm", ".tfm"
519 SUFFIXES (OFM_SUFFIXES);
520 FMT_INFO.suffix_search_only = true;
521 FMT_INFO.binmode = true;
523 case kpse_opl_format:
524 INIT_FORMAT ("opl", DEFAULT_OPLFONTS, OPL_ENVS);
526 FMT_INFO.suffix_search_only = true;
528 case kpse_otp_format:
529 INIT_FORMAT ("otp", DEFAULT_OTPINPUTS, OTP_ENVS);
531 FMT_INFO.suffix_search_only = true;
533 case kpse_ovf_format:
534 INIT_FORMAT ("ovf", DEFAULT_OVFFONTS, OVF_ENVS);
536 FMT_INFO.suffix_search_only = true;
537 FMT_INFO.binmode = true;
539 case kpse_ovp_format:
540 INIT_FORMAT ("ovp", DEFAULT_OVPFONTS, OVP_ENVS);
542 FMT_INFO.suffix_search_only = true;
544 case kpse_pict_format:
545 INIT_FORMAT ("graphic/figure", DEFAULT_TEXINPUTS, PICT_ENVS);
546 #define PICT_SUFFIXES ".eps", ".epsi"
547 ALT_SUFFIXES (PICT_SUFFIXES);
548 FMT_INFO.binmode = true;
550 case kpse_tex_format:
551 init_maketex (format, "mktextex", NULL);
552 INIT_FORMAT ("tex", DEFAULT_TEXINPUTS, TEX_ENVS);
554 /* We don't maintain a list of alternate TeX suffixes. Such a list
555 could never be complete. */
557 case kpse_tex_ps_header_format:
558 INIT_FORMAT ("PostScript header", DEFAULT_TEXPSHEADERS,
560 /* Unfortunately, dvipsk uses this format for type1 fonts. */
561 #define TEXPSHEADER_SUFFIXES ".pro"
562 ALT_SUFFIXES (TEXPSHEADER_SUFFIXES);
563 FMT_INFO.binmode = true;
565 case kpse_texdoc_format:
566 INIT_FORMAT ("TeX system documentation", DEFAULT_TEXDOCS, TEXDOC_ENVS);
568 case kpse_texpool_format:
569 INIT_FORMAT ("texpool", DEFAULT_TEXPOOL, TEXPOOL_ENVS);
572 case kpse_texsource_format:
573 INIT_FORMAT ("TeX system sources", DEFAULT_TEXSOURCES, TEXSOURCE_ENVS);
575 case kpse_troff_font_format:
576 INIT_FORMAT ("Troff fonts", DEFAULT_TRFONTS, TROFF_FONT_ENVS);
577 FMT_INFO.binmode = true;
579 case kpse_type1_format:
580 INIT_FORMAT ("type1 fonts", DEFAULT_T1FONTS, TYPE1_ENVS);
581 #define TYPE1_SUFFIXES ".pfa", ".pfb"
582 SUFFIXES (TYPE1_SUFFIXES);
583 FMT_INFO.binmode = true;
586 INIT_FORMAT ("vf", DEFAULT_VFFONTS, VF_ENVS);
588 FMT_INFO.suffix_search_only = true;
589 FMT_INFO.binmode = true;
591 case kpse_dvips_config_format:
592 INIT_FORMAT ("dvips config", DEFAULT_TEXCONFIG, DVIPS_CONFIG_ENVS);
594 case kpse_ist_format:
595 INIT_FORMAT ("ist", DEFAULT_INDEXSTYLE, IST_ENVS);
598 case kpse_truetype_format:
599 INIT_FORMAT ("truetype fonts", DEFAULT_TTFONTS, TRUETYPE_ENVS);
600 #define TRUETYPE_SUFFIXES ".ttf", ".ttc", ".TTF", ".TTC"
601 SUFFIXES (TRUETYPE_SUFFIXES);
602 FMT_INFO.suffix_search_only = false;
603 FMT_INFO.binmode = true;
605 case kpse_type42_format:
606 INIT_FORMAT ("type42 fonts", DEFAULT_T42FONTS, TYPE42_ENVS);
607 #define TYPE42_SUFFIXES ".t42", ".T42"
608 SUFFIXES (TYPE42_SUFFIXES);
609 FMT_INFO.binmode = true;
611 case kpse_web2c_format:
612 INIT_FORMAT ("web2c files", DEFAULT_WEB2C, WEB2C_ENVS);
614 case kpse_program_text_format:
615 INIT_FORMAT ("other text files",
616 concatn (".", ENV_SEP_STRING, "$TEXMF/",
617 kpse_program_name, "//", NULL),
618 concat (uppercasify (kpse_program_name), "INPUTS"));
620 case kpse_program_binary_format:
621 INIT_FORMAT ("other binary files",
622 concatn (".", ENV_SEP_STRING, "$TEXMF/",
623 kpse_program_name, "//", NULL),
624 concat (uppercasify (kpse_program_name), "INPUTS"));
625 FMT_INFO.binmode = true;
627 case kpse_miscfonts_format:
628 INIT_FORMAT ("misc fonts", DEFAULT_MISCFONTS, MISCFONTS_ENVS);
629 FMT_INFO.binmode = true;
631 case kpse_web_format:
632 INIT_FORMAT ("web", DEFAULT_WEBINPUTS, WEB_ENVS);
634 ALT_SUFFIXES (".ch");
636 case kpse_cweb_format:
637 INIT_FORMAT ("cweb", DEFAULT_CWEBINPUTS, CWEB_ENVS);
638 #define CWEB_SUFFIXES ".w", ".web"
639 SUFFIXES (CWEB_SUFFIXES);
640 ALT_SUFFIXES (".ch");
642 case kpse_enc_format:
643 INIT_FORMAT ("enc files", DEFAULT_ENCFONTS, ENC_ENVS);
646 case kpse_cmap_format:
647 INIT_FORMAT ("cmap files", DEFAULT_CMAPFONTS, CMAP_ENVS);
650 case kpse_sfd_format:
651 INIT_FORMAT ("subfont definition files", DEFAULT_SFDFONTS, SFD_ENVS);
654 case kpse_opentype_format:
655 INIT_FORMAT ("opentype fonts", DEFAULT_OPENTYPEFONTS, OPENTYPE_ENVS);
657 FMT_INFO.binmode = true;
659 case kpse_pdftex_config_format:
660 INIT_FORMAT ("pdftex config", DEFAULT_PDFTEXCONFIG, PDFTEXCONFIG_ENVS);
662 case kpse_lig_format:
663 INIT_FORMAT ("lig files", DEFAULT_LIGFONTS, LIG_ENVS);
666 case kpse_texmfscripts_format:
667 INIT_FORMAT ("texmfscripts", DEFAULT_TEXMFSCRIPTS, TEXMFSCRIPTS_ENVS);
669 case kpse_lua_format:
670 INIT_FORMAT ("lua", DEFAULT_LUAINPUTS, LUA_ENVS);
671 #define LUA_SUFFIXES ".luc", ".luctex", ".texluc", ".lua", ".luatex", ".texlua"
672 SUFFIXES (LUA_SUFFIXES);
673 FMT_INFO.suffix_search_only = true;
676 FATAL1 ("kpse_init_format: Unknown format %d", format);
680 #define MAYBE(member) (FMT_INFO.member ? FMT_INFO.member : "(none)")
682 /* Describe the monster we've created. */
683 if (KPSE_DEBUG_P (KPSE_DEBUG_PATHS))
685 DEBUGF2 ("Search path for %s files (from %s)\n",
686 FMT_INFO.type, FMT_INFO.path_source);
687 DEBUGF1 (" = %s\n", FMT_INFO.path);
688 DEBUGF1 (" before expansion = %s\n", FMT_INFO.raw_path);
689 DEBUGF1 (" application override path = %s\n", MAYBE (override_path));
690 DEBUGF1 (" application config file path = %s\n", MAYBE (client_path));
691 DEBUGF1 (" texmf.cnf path = %s\n", MAYBE (cnf_path));
692 DEBUGF1 (" compile-time path = %s\n", MAYBE (default_path));
693 DEBUGF (" default suffixes =");
694 if (FMT_INFO.suffix) {
696 for (ext = FMT_INFO.suffix; ext && *ext; ext++) {
697 fprintf (stderr, " %s", *ext);
701 fputs (" (none)\n", stderr);
703 DEBUGF (" other suffixes =");
704 if (FMT_INFO.alt_suffix) {
706 for (alt = FMT_INFO.alt_suffix; alt && *alt; alt++) {
707 fprintf (stderr, " %s", *alt);
711 fputs (" (none)\n", stderr);
713 DEBUGF1 (" search only with suffix = %d\n",FMT_INFO.suffix_search_only);
714 DEBUGF1 (" numeric format value = %d\n", format);
715 DEBUGF1 (" runtime generation program = %s\n", MAYBE (program));
716 DEBUGF (" runtime generation command =");
719 for (arg = FMT_INFO.argv; *arg; arg++) {
720 fprintf (stderr, " %s", *arg);
724 fputs(" (none)\n", stderr);
726 DEBUGF1 (" program enabled = %d\n", FMT_INFO.program_enabled_p);
727 DEBUGF1 (" program enable level = %d\n", FMT_INFO.program_enable_level);
729 #endif /* KPSE_DEBUG */
731 return FMT_INFO.path;
734 /* Look up a file NAME of type FORMAT, and the given MUST_EXIST. This
735 initializes the path spec for FORMAT if it's the first lookup of that
736 type. Return the filename found, or NULL. This is the most likely
737 thing for clients to call. */
740 kpse_find_file P3C(const_string, name, kpse_file_format_type, format,
743 string *ret_list = kpse_find_file_generic (name, format, must_exist, false);
744 string ret = *ret_list;
749 /* As with `kpse_find_file', but also allow passing ALL for the search,
750 hence we always return a NULL-terminated list. */
753 kpse_find_file_generic P4C(const_string, name, kpse_file_format_type, format,
754 boolean, must_exist, boolean, all)
758 string *mapped_names;
759 const_string *target;
761 unsigned name_len = 0;
762 boolean name_has_suffix_already = false;
763 boolean use_fontmaps = (format == kpse_tfm_format
764 || format == kpse_gf_format
765 || format == kpse_pk_format
766 || format == kpse_ofm_format);
769 /* NAME being NULL is a programming bug somewhere. NAME can be empty,
770 though; this happens with constructs like `\input\relax'. */
773 if (FMT_INFO.path == NULL)
774 kpse_init_format (format);
776 if (KPSE_DEBUG_P (KPSE_DEBUG_SEARCH))
777 DEBUGF3 ("kpse_find_file: searching for %s of type %s (from %s)\n",
778 name, FMT_INFO.type, FMT_INFO.path_source);
780 /* Do variable and tilde expansion. */
781 name = kpse_expand (name);
783 /* Does NAME already end in a possible suffix? */
784 name_len = strlen (name);
785 if (FMT_INFO.suffix) {
786 for (ext = FMT_INFO.suffix; !name_has_suffix_already && *ext; ext++) {
787 unsigned suffix_len = strlen (*ext);
788 name_has_suffix_already = (name_len >= suffix_len
789 && FILESTRCASEEQ (*ext, name + name_len - suffix_len));
792 if (!name_has_suffix_already && FMT_INFO.alt_suffix) {
793 for (ext = FMT_INFO.alt_suffix; !name_has_suffix_already && *ext; ext++) {
794 unsigned suffix_len = strlen (*ext);
795 name_has_suffix_already = (name_len >= suffix_len
796 && FILESTRCASEEQ (*ext, name + name_len - suffix_len));
800 /* Set up list of target names to search for. */
802 target = XTALLOC1 (const_string);
803 /* Case #1: NAME doesn't have a suffix which is equal to a "standard"
804 suffix. For example, foo.bar, but not foo.tex. We look for the
805 name with the standard suffixes appended. */
806 if (!name_has_suffix_already && FMT_INFO.suffix) {
807 for (ext = FMT_INFO.suffix; *ext; ext++) {
808 string name_with_suffix = concat (name, *ext);
809 target[count++] = name_with_suffix;
810 XRETALLOC (target, count+1, const_string);
812 && (mapped_names = kpse_fontmap_lookup (name_with_suffix)) != NULL)
814 /* FIXME: we leak mapped_names and its elements, some of the time */
815 while ((mapped_name = *mapped_names++) != NULL) {
816 target[count++] = xstrdup (mapped_name);
817 XRETALLOC (target, count+1, const_string);
822 /* Case #2: Just look for the name we've been given, provided non-suffix
823 searches are allowed or the name already includes a suffix. */
824 if (name_has_suffix_already || !FMT_INFO.suffix_search_only) {
825 target[count++] = xstrdup (name);
826 XRETALLOC (target, count+1, const_string);
827 if (use_fontmaps && (mapped_names = kpse_fontmap_lookup (name)) != NULL) {
828 /* FIXME: we leak mapped_names and its elements, some of the time */
829 while ((mapped_name = *mapped_names++) != NULL) {
830 target[count++] = xstrdup (mapped_name);
831 XRETALLOC (target, count+1, const_string);
835 /* Terminate list. */
836 target[count] = NULL;
838 /* Search, trying to minimize disk-pounding. */
839 ret = kpse_path_search_list_generic (FMT_INFO.path, target, false, all);
840 /* Do we need to pound the disk? */
841 if (! *ret && must_exist) {
842 for (count = 0; target[count]; count++)
843 free ((void *) target[count]);
845 /* We look for a subset of the previous set of names, so the
846 target array is large enough. In particular, we don't pound
847 the disk for alternate names from the fontmaps. */
848 if (!name_has_suffix_already && FMT_INFO.suffix_search_only) {
849 for (ext = FMT_INFO.suffix; *ext; ext++)
850 target[count++] = concat (name, *ext);
852 if (name_has_suffix_already || !FMT_INFO.suffix_search_only) {
853 target[count++] = xstrdup (name);
855 target[count] = NULL;
856 ret = kpse_path_search_list_generic (FMT_INFO.path, target, true, all);
859 /* Free the list we created. */
860 for (count = 0; target[count]; count++)
861 free ((void *) target[count]);
864 /* If nothing was found, call mktex* to create a missing file. Since
865 this returns a single string, morph it into a list. */
866 if (! *ret && must_exist) {
867 ret = XTALLOC (2, string);
868 ret[0] = kpse_make_tex (format, name);
874 free ((void *) name);
881 /* Open NAME along the search path for TYPE for reading and return the
882 resulting file, or exit with an error message. */
885 kpse_open_file P2C(const_string, name, kpse_file_format_type, type)
887 string fullname = kpse_find_file (name, type, true);
888 const_string mode = kpse_format_info[type].binmode
891 FILE *f = fullname ? fopen (fullname, mode) : NULL;
897 FATAL2 ("%s file `%s' not found", kpse_format_info[type].type, name);
904 /* When using the %&<format> construct, we'd like to use the paths for
905 that format, rather than those for the name we were called with.
906 Of course this happens after various initializations have been
907 performed, so we have this function to force the issue. Note that
908 the paths for kpse_cnf_format and kpse_db_format are not cleared.
910 This function is defined here, and not in progname.c, because it
911 need kpse_format_info, and would cause all of tex-file to be pulled
912 in by programs that do not need it. */
914 kpse_reset_program_name P1C(const_string, progname)
918 /* It is a fatal error for either of these to be NULL. */
919 assert (progname && kpse_program_name);
920 /* Do nothing if the name is unchanged. */
921 if (STREQ(kpse_program_name, progname))
924 free (kpse_program_name);
925 kpse_program_name = xstrdup (progname);
926 xputenv("progname", kpse_program_name);
928 /* Clear paths -- do we want the db path to be cleared? */
929 for (i = 0; i != kpse_last_format; ++i) {
930 /* Do not erase the cnf of db paths. This means that the filename
931 database is not rebuilt, nor are different configuration files
932 searched. The alternative is to tolerate a memory leak of up
933 to 100k if this function is called. */
934 if (i == kpse_cnf_format || i == kpse_db_format)
936 /* Wipe the path (it is tested) and the cnf_path and because their
937 values may differ with the new program name. */
938 if (kpse_format_info[i].path != NULL) {
939 free ((string)kpse_format_info[i].path);
940 kpse_format_info[i].path = NULL;
942 /* We cannot free the cnf_path: it points into the cnf hash, which
943 means all hell will break loose if we did. */
944 if (kpse_format_info[i].cnf_path != NULL) {
945 kpse_format_info[i].cnf_path = NULL;
947 /* We do not wipe the override_path at this point, though arguably
948 we should provide new values. It is not likely to matter for
949 the programs that call this function. */