tentative fix for issue 3 (ex 53)
[mplib] / src / texk / kpathsea / tex-file.c
1 /* tex-file.c: high-level file searching by format.
2
3    Copyright 1993, 1994, 1995, 1996, 1997, 2007, 2008 Karl Berry.
4    Copyright 1998-2005 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/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>
34
35
36 /* See tex-file.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];
41
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"
94 \f
95 /* The compiled-in default list, DEFAULT_FONT_SIZES, is intended to be
96    set from the command line (presumably via the Makefile).  */
97
98 #ifndef DEFAULT_FONT_SIZES
99 #define DEFAULT_FONT_SIZES ""
100 #endif
101
102 void
103 kpse_init_fallback_resolutions P1C(string, envvar)
104 {
105   string size;
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);
114   
115   /* Initialize the list of last-resort sizes.  */
116   for (size = kpse_path_element (size_list); size != NULL;
117        size = kpse_path_element (NULL))
118     {
119       unsigned s;
120       if (! *size) /* Skip empty elements.  */
121         continue;
122       
123       s = atoi (size);
124       if (size_count && s < last_resort_sizes[size_count - 1]) {
125     WARNING1 ("kpathsea: last resort size %s not in ascending order, ignored",
126           size);
127       } else {
128         size_count++;
129         XRETALLOC (last_resort_sizes, size_count, unsigned);
130         last_resort_sizes[size_count - 1] = atoi (size);
131       }
132     }
133
134   /* Add a zero to mark the end of the list.  */
135   size_count++;
136   XRETALLOC (last_resort_sizes, size_count, unsigned);
137   last_resort_sizes[size_count - 1] = 0;
138
139   free (size_list);
140     
141   kpse_fallback_resolutions = last_resort_sizes;
142 }
143 \f
144 /* We should be able to set the program arguments in the same way.  Not
145    to mention the path values themselves.  */
146
147 void
148 kpse_set_program_enabled P3C(kpse_file_format_type, fmt,  boolean, value,
149                              kpse_src_type, level)
150 {
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;
155   }
156 }
157
158
159 /* Web2c and kpsewhich have command-line options to set this stuff.  May
160    as well have a common place.  */
161
162 void
163 kpse_maketex_option P2C(const_string, fmtname,  boolean, value)
164 {
165   kpse_file_format_type fmt = kpse_last_format;
166   
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;
184   }
185   if (fmt != kpse_last_format) {
186     kpse_set_program_enabled (fmt, value, kpse_src_cmdline);
187   }
188 }
189 \f
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; \
194 } while (0)
195
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)                 \
199   if (try_path) {                                               \
200       info->raw_path = try_path;                                \
201       info->path = kpse_expand_default (try_path, info->path);  \
202       info->path_source = source_string;                        \
203   }
204
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.  */
210
211 static void
212 init_path PVAR2C(kpse_format_info_type *, info, const_string, default_path, ap)
213 {
214   string env_name;
215   string env_value = NULL;
216   string var = NULL;
217   
218   info->default_path = default_path;
219
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.  */
228     if (!var) {
229       /* Try PATH.prog. */
230       string evar = concat3 (env_name, ".", kpse_program_name);
231       env_value = getenv (evar);
232       if (env_value && *env_value) {
233         var = evar;
234       } else {
235         free (evar);
236         /* Try PATH_prog. */
237         evar = concat3 (env_name, "_", kpse_program_name);
238         env_value = getenv (evar);
239         if (env_value && *env_value) {
240           var = evar;
241         } else {
242           free (evar);
243           /* Try simply PATH.  */
244           env_value = getenv (env_name);
245           if (env_value && *env_value) {
246             var = env_name;        
247           }
248         }
249       }
250     }
251     
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);
256       
257     if (var && info->cnf_path)
258       break;
259   }
260   va_end (ap);
261   
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.  */
267
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";
271
272   /* Translate ';' in the envvar into ':' if that's our ENV_SEP. */
273   if (IS_ENV_SEP(':') && env_value) {
274       string loc;
275       env_value = xstrdup(env_value);  /* Freed below. */
276       for (loc = env_value; *loc; loc++) {
277           if (*loc == ';')
278               *loc = ':';
279       }
280   }
281   
282   EXPAND_DEFAULT (info->cnf_path, "texmf.cnf");
283   EXPAND_DEFAULT (info->client_path, "program config file");
284   if (var)
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);
288
289   /* Free the copied env_value. */
290   if (IS_ENV_SEP(':') && env_value)
291       free(env_value);
292 }}
293
294
295 /* Some file types have more than one suffix, and sometimes it is
296    convenient to modify the list of searched suffixes.  */ 
297
298 void
299 kpse_set_suffixes PVAR2C(kpse_file_format_type, format,
300   boolean, alternate,  ap)
301 {
302   const_string **list;
303   const_string s;
304   int count = 0;
305
306   if (alternate) {
307     list = &kpse_format_info[format].alt_suffix;
308   } else {
309     list = &kpse_format_info[format].suffix;
310   }
311
312   while ((s = va_arg (ap, string)) != NULL) {
313     count++;
314     XRETALLOC (*list, count + 1, const_string);
315     (*list)[count - 1] = s;
316   }
317   va_end (ap);
318   (*list)[count] = NULL;
319
320 }}
321
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)
327
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)
333
334
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
339    name to 0 or 1.  */
340
341 static void
342 init_maketex PVAR2C(kpse_file_format_type, fmt,  const_string, dflt_prog, ap)
343 {
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"} */
348   const_string arg;
349
350   /* Doesn't hurt to always set this info.  */
351   f->program = prog;
352
353   /* Set up the argument vector. */
354   f->argc = 0;
355   f->argv = XTALLOC(2, const_string);
356   f->argv[f->argc++] = dflt_prog;
357   while ((arg = va_arg (ap, string)) != NULL) {
358     f->argc++;
359     XRETALLOC (f->argv, f->argc + 1, const_string);
360     f->argv[f->argc - 1] = arg;
361   }
362   f->argv[f->argc] = NULL;
363
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);
368   }
369   
370   free (PROG);
371 }}
372
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",\
379   NULL
380
381 static string
382 remove_dbonly P1C(const_string, path)
383 {
384   string ret = XTALLOC(strlen (path) + 1, char), q=ret;
385   const_string p=path;
386   boolean new_elt=true;
387
388   while (*p) {
389     if (new_elt && *p && *p == '!' && *(p+1) == '!')
390       p += 2;
391     else {
392       new_elt = (*p == ENV_SEP);
393       *q++ = *p++;
394     }
395   }
396   *q = '\0';
397   return(ret);
398 }
399
400 /* Initialize everything for FORMAT.  */
401
402 const_string
403 kpse_init_format P1C(kpse_file_format_type, format)
404 {
405   /* If we get called twice, don't redo all the work.  */
406   if (FMT_INFO.path)
407     return FMT_INFO.path;
408     
409   switch (format)
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.  */
412     case kpse_gf_format:
413       INIT_FORMAT ("gf", DEFAULT_GFFONTS, GF_ENVS);
414       SUFFIXES ("gf");
415       FMT_INFO.suffix_search_only = true;
416       FMT_INFO.binmode = true;
417       break;
418     case kpse_pk_format:
419       init_maketex (format, "mktexpk", MKTEXPK_ARGS);
420       INIT_FORMAT ("pk", DEFAULT_PKFONTS, PK_ENVS);
421       SUFFIXES ("pk");
422       FMT_INFO.suffix_search_only = true;
423       FMT_INFO.binmode = true;
424       break;
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;
430       break;
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);
435       SUFFIXES (".tfm");
436       FMT_INFO.suffix_search_only = true;
437       FMT_INFO.binmode = true;
438       break;
439     case kpse_afm_format:
440       INIT_FORMAT ("afm", DEFAULT_AFMFONTS, AFM_ENVS);
441       SUFFIXES (".afm");
442       break;
443     case kpse_base_format:
444       init_maketex (format, "mktexfmt", NULL);
445       INIT_FORMAT ("base", DEFAULT_MFBASES, BASE_ENVS);
446       SUFFIXES (".base");
447       FMT_INFO.binmode = true;
448       break;
449     case kpse_bib_format:
450       INIT_FORMAT ("bib", DEFAULT_BIBINPUTS, BIB_ENVS);
451       SUFFIXES (".bib");
452       FMT_INFO.suffix_search_only = true;
453       break;
454     case kpse_bst_format:
455       INIT_FORMAT ("bst", DEFAULT_BSTINPUTS, BST_ENVS);
456       SUFFIXES (".bst");
457       break;
458     case kpse_cnf_format:
459       INIT_FORMAT ("cnf", DEFAULT_TEXMFCNF, CNF_ENVS);
460       SUFFIXES (".cnf");
461       break;
462     case kpse_db_format:
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);
467       break;
468     case kpse_fmt_format:
469       init_maketex (format, "mktexfmt", NULL);
470       INIT_FORMAT ("fmt", DEFAULT_TEXFORMATS, FMT_ENVS);
471       SUFFIXES (".fmt");
472       FMT_INFO.binmode = true;
473       break;
474     case kpse_fontmap_format:
475       INIT_FORMAT ("map", DEFAULT_TEXFONTMAPS, FONTMAP_ENVS);
476       SUFFIXES (".map");
477       break;
478     case kpse_mem_format:
479       init_maketex (format, "mktexfmt", NULL);
480       INIT_FORMAT ("mem", DEFAULT_MPMEMS, MEM_ENVS);
481       SUFFIXES (".mem");
482       FMT_INFO.binmode = true;
483       break;
484     case kpse_mf_format:
485       init_maketex (format, "mktexmf", NULL);
486       INIT_FORMAT ("mf", DEFAULT_MFINPUTS, MF_ENVS);
487       SUFFIXES (".mf");
488       break;
489     case kpse_mft_format:
490       INIT_FORMAT ("mft", DEFAULT_MFTINPUTS, MFT_ENVS);
491       SUFFIXES (".mft");
492       break;
493     case kpse_mfpool_format:
494       INIT_FORMAT ("mfpool", DEFAULT_MFPOOL, MFPOOL_ENVS);
495       SUFFIXES (".pool");
496       break;
497     case kpse_mp_format:
498       INIT_FORMAT ("mp", DEFAULT_MPINPUTS, MP_ENVS);
499       SUFFIXES (".mp");
500       break;
501     case kpse_mppool_format:
502       INIT_FORMAT ("mppool", DEFAULT_MPPOOL, MPPOOL_ENVS);
503       SUFFIXES (".pool");
504       break;
505     case kpse_mpsupport_format:
506       INIT_FORMAT ("MetaPost support", DEFAULT_MPSUPPORT, MPSUPPORT_ENVS);
507       break;
508     case kpse_ocp_format:
509       init_maketex (format, "mkocp", NULL);
510       INIT_FORMAT ("ocp", DEFAULT_OCPINPUTS, OCP_ENVS);
511       SUFFIXES (".ocp");
512       FMT_INFO.suffix_search_only = true;
513       FMT_INFO.binmode = true;
514       break;
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;
522       break;
523     case kpse_opl_format:
524       INIT_FORMAT ("opl", DEFAULT_OPLFONTS, OPL_ENVS);
525       SUFFIXES (".opl");
526       FMT_INFO.suffix_search_only = true;
527       break;
528     case kpse_otp_format:
529       INIT_FORMAT ("otp", DEFAULT_OTPINPUTS, OTP_ENVS);
530       SUFFIXES (".otp");
531       FMT_INFO.suffix_search_only = true;
532       break;
533     case kpse_ovf_format:
534       INIT_FORMAT ("ovf", DEFAULT_OVFFONTS, OVF_ENVS);
535       SUFFIXES (".ovf");
536       FMT_INFO.suffix_search_only = true;
537       FMT_INFO.binmode = true;
538       break;
539     case kpse_ovp_format:
540       INIT_FORMAT ("ovp", DEFAULT_OVPFONTS, OVP_ENVS);
541       SUFFIXES (".ovp");
542       FMT_INFO.suffix_search_only = true;
543       break;
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;
549       break;
550     case kpse_tex_format:
551       init_maketex (format, "mktextex", NULL);
552       INIT_FORMAT ("tex", DEFAULT_TEXINPUTS, TEX_ENVS);
553       SUFFIXES (".tex");
554       /* We don't maintain a list of alternate TeX suffixes.  Such a list
555          could never be complete.  */
556       break;
557     case kpse_tex_ps_header_format:
558       INIT_FORMAT ("PostScript header", DEFAULT_TEXPSHEADERS,
559                    TEX_PS_HEADER_ENVS);
560 /* Unfortunately, dvipsk uses this format for type1 fonts.  */
561 #define TEXPSHEADER_SUFFIXES ".pro"
562       ALT_SUFFIXES (TEXPSHEADER_SUFFIXES);
563       FMT_INFO.binmode = true;
564       break;
565     case kpse_texdoc_format:
566       INIT_FORMAT ("TeX system documentation", DEFAULT_TEXDOCS, TEXDOC_ENVS);
567       break;
568     case kpse_texpool_format:
569       INIT_FORMAT ("texpool", DEFAULT_TEXPOOL, TEXPOOL_ENVS);
570       SUFFIXES (".pool");
571       break;
572     case kpse_texsource_format:
573       INIT_FORMAT ("TeX system sources", DEFAULT_TEXSOURCES, TEXSOURCE_ENVS);
574       break;
575     case kpse_troff_font_format:
576       INIT_FORMAT ("Troff fonts", DEFAULT_TRFONTS, TROFF_FONT_ENVS);
577       FMT_INFO.binmode = true;
578       break;
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;
584       break;
585     case kpse_vf_format:
586       INIT_FORMAT ("vf", DEFAULT_VFFONTS, VF_ENVS);
587       SUFFIXES (".vf");
588       FMT_INFO.suffix_search_only = true;
589       FMT_INFO.binmode = true;
590       break;
591     case kpse_dvips_config_format:
592       INIT_FORMAT ("dvips config", DEFAULT_TEXCONFIG, DVIPS_CONFIG_ENVS);
593       break;
594     case kpse_ist_format:
595       INIT_FORMAT ("ist", DEFAULT_INDEXSTYLE, IST_ENVS);
596       SUFFIXES (".ist");
597       break;
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;
604       break;
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;
610       break;
611     case kpse_web2c_format:
612       INIT_FORMAT ("web2c files", DEFAULT_WEB2C, WEB2C_ENVS);
613       break;
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"));
619       break;
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;
626       break;
627     case kpse_miscfonts_format:
628       INIT_FORMAT ("misc fonts", DEFAULT_MISCFONTS, MISCFONTS_ENVS);
629       FMT_INFO.binmode = true;
630       break;
631     case kpse_web_format:
632       INIT_FORMAT ("web", DEFAULT_WEBINPUTS, WEB_ENVS);
633       SUFFIXES (".web");
634       ALT_SUFFIXES (".ch");
635       break;
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");
641       break;
642     case kpse_enc_format:
643       INIT_FORMAT ("enc files", DEFAULT_ENCFONTS, ENC_ENVS);
644       SUFFIXES (".enc");
645       break;
646     case kpse_cmap_format:
647       INIT_FORMAT ("cmap files", DEFAULT_CMAPFONTS, CMAP_ENVS);
648       SUFFIXES (".cmap");      
649       break;
650     case kpse_sfd_format:
651       INIT_FORMAT ("subfont definition files", DEFAULT_SFDFONTS, SFD_ENVS);
652       SUFFIXES (".sfd");
653       break;
654     case kpse_opentype_format:
655       INIT_FORMAT ("opentype fonts", DEFAULT_OPENTYPEFONTS, OPENTYPE_ENVS);
656       SUFFIXES (".otf");
657       FMT_INFO.binmode = true;
658       break;
659     case kpse_pdftex_config_format:
660       INIT_FORMAT ("pdftex config", DEFAULT_PDFTEXCONFIG, PDFTEXCONFIG_ENVS);
661       break;
662     case kpse_lig_format:
663       INIT_FORMAT ("lig files", DEFAULT_LIGFONTS, LIG_ENVS);
664       SUFFIXES (".lig");
665       break;
666     case kpse_texmfscripts_format:
667       INIT_FORMAT ("texmfscripts", DEFAULT_TEXMFSCRIPTS, TEXMFSCRIPTS_ENVS);
668       break;
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;
674       break;
675     default:
676       FATAL1 ("kpse_init_format: Unknown format %d", format);
677     }
678
679 #ifdef KPSE_DEBUG
680 #define MAYBE(member) (FMT_INFO.member ? FMT_INFO.member : "(none)")
681
682   /* Describe the monster we've created.  */
683   if (KPSE_DEBUG_P (KPSE_DEBUG_PATHS))
684     {
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) {
695         const_string *ext;
696         for (ext = FMT_INFO.suffix; ext && *ext; ext++) {
697           fprintf (stderr, " %s", *ext);
698         }
699         putc ('\n', stderr);
700       } else {
701         fputs (" (none)\n", stderr);
702       }
703       DEBUGF  ("  other suffixes =");
704       if (FMT_INFO.alt_suffix) {
705         const_string *alt;
706         for (alt = FMT_INFO.alt_suffix; alt && *alt; alt++) {
707           fprintf (stderr, " %s", *alt);
708         }
709         putc ('\n', stderr);
710       } else {
711         fputs (" (none)\n", stderr);
712       }
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 =");
717       if (FMT_INFO.argv) {
718         const_string *arg;
719         for (arg = FMT_INFO.argv; *arg; arg++) {
720           fprintf (stderr, " %s", *arg);
721         }
722         putc ('\n', stderr);
723       } else {
724           fputs(" (none)\n", stderr);
725       }
726       DEBUGF1 ("  program enabled = %d\n", FMT_INFO.program_enabled_p);
727       DEBUGF1 ("  program enable level = %d\n", FMT_INFO.program_enable_level);
728     }
729 #endif /* KPSE_DEBUG */
730
731   return FMT_INFO.path;
732 }
733 \f
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.  */
738    
739 string
740 kpse_find_file P3C(const_string, name,  kpse_file_format_type, format,
741                    boolean, must_exist)
742 {
743   string *ret_list = kpse_find_file_generic (name, format, must_exist, false);
744   string ret = *ret_list;
745   free (ret_list);
746   return ret;
747 }
748
749 /* As with `kpse_find_file', but also allow passing ALL for the search,
750    hence we always return a NULL-terminated list.  */
751
752 string *
753 kpse_find_file_generic P4C(const_string, name,  kpse_file_format_type, format,
754                            boolean, must_exist,  boolean, all)
755 {
756   const_string *ext;
757   string mapped_name;
758   string *mapped_names;
759   const_string *target;
760   unsigned count;
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);
767   string *ret = NULL;
768
769   /* NAME being NULL is a programming bug somewhere.  NAME can be empty,
770      though; this happens with constructs like `\input\relax'.  */
771   assert (name);
772   
773   if (FMT_INFO.path == NULL)
774     kpse_init_format (format);
775
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);
779
780   /* Do variable and tilde expansion. */
781   name = kpse_expand (name);
782   
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));
790     }
791   }
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));
797     }
798   }
799
800   /* Set up list of target names to search for. */
801   count = 0;
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);
811       if (use_fontmaps
812           && (mapped_names = kpse_fontmap_lookup (name_with_suffix)) != NULL)
813       {
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);
818         }
819       }
820     }
821   }
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);
832       }
833     }
834   }
835   /* Terminate list. */
836   target[count] = NULL;
837
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]);
844     count = 0;
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);
851     }
852     if (name_has_suffix_already || !FMT_INFO.suffix_search_only) {
853       target[count++] = xstrdup (name);
854     }
855     target[count] = NULL;
856     ret = kpse_path_search_list_generic (FMT_INFO.path, target, true, all);
857   }
858   
859   /* Free the list we created. */
860   for (count = 0; target[count]; count++)
861     free ((void *) target[count]);
862   free (target);
863   
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);
869     if (ret[0]) {
870       ret[1] = NULL;
871     }
872   }
873
874   free ((void *) name);
875
876   return ret;
877 }
878
879
880 \f
881 /* Open NAME along the search path for TYPE for reading and return the
882    resulting file, or exit with an error message.  */
883
884 FILE *
885 kpse_open_file P2C(const_string, name,  kpse_file_format_type, type)
886 {
887   string fullname = kpse_find_file (name, type, true);
888   const_string mode = kpse_format_info[type].binmode
889                       ? FOPEN_RBIN_MODE
890                       : FOPEN_R_MODE;
891   FILE *f = fullname ? fopen (fullname, mode) : NULL;
892   if (!f) {
893     if (fullname) {
894       perror (fullname);
895       exit (1);
896     } else {
897       FATAL2 ("%s file `%s' not found", kpse_format_info[type].type, name);
898     }
899   }
900   
901   return f;
902 }
903 \f
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.
909
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. */
913 void
914 kpse_reset_program_name P1C(const_string, progname)
915 {
916   int i;
917
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))
922     return;
923
924   free (kpse_program_name);
925   kpse_program_name = xstrdup (progname);
926   xputenv("progname", kpse_program_name);
927   
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)
935       continue;
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;
941     }
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;
946     }
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. */
950   }
951 }