~ubuntu-branches/ubuntu/jaunty/luatex/jaunty

« back to all changes in this revision

Viewing changes to src/texk/kpathsea/tex-file.c

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Preining
  • Date: 2007-09-24 12:56:11 UTC
  • Revision ID: james.westby@ubuntu.com-20070924125611-a8ge689azbptxvla
Tags: upstream-0.11.2
ImportĀ upstreamĀ versionĀ 0.11.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* tex-file.c: high-level file searching by format.
 
2
 
 
3
    Copyright 1998-2005 Olaf Weber.
 
4
    Copyright 1993, 94, 95, 96, 97 Karl Berry.
 
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
 
17
    License along with this library; if not, write to the Free Software
 
18
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 
 
20
*/
 
21
 
 
22
#include <kpathsea/config.h>
 
23
 
 
24
#include <kpathsea/c-fopen.h>
 
25
#include <kpathsea/c-pathch.h>
 
26
#include <kpathsea/c-vararg.h>
 
27
#include <kpathsea/cnf.h>
 
28
#include <kpathsea/concatn.h>
 
29
#include <kpathsea/default.h>
 
30
#include <kpathsea/expand.h>
 
31
#include <kpathsea/fontmap.h>
 
32
#include <kpathsea/paths.h>
 
33
#include <kpathsea/pathsearch.h>
 
34
#include <kpathsea/tex-file.h>
 
35
#include <kpathsea/tex-make.h>
 
36
#include <kpathsea/variable.h>
 
37
 
 
38
 
 
39
/* See tex-file.h.  */
 
40
const_string kpse_fallback_font = NULL;
 
41
const_string kpse_fallback_resolutions_string = NULL;
 
42
unsigned *kpse_fallback_resolutions = NULL;
 
43
kpse_format_info_type kpse_format_info[kpse_last_format];
 
44
 
 
45
/* These are not in the structure
 
46
   because it's annoying to initialize lists in C.  */
 
47
#define GF_ENVS "GFFONTS", GLYPH_ENVS
 
48
#define PK_ENVS "PKFONTS", "TEXPKS", GLYPH_ENVS
 
49
#define GLYPH_ENVS "GLYPHFONTS", "TEXFONTS"
 
50
#define TFM_ENVS "TFMFONTS", "TEXFONTS"
 
51
#define AFM_ENVS "AFMFONTS", "TEXFONTS"
 
52
#define BASE_ENVS "MFBASES", "TEXMFINI"
 
53
#define BIB_ENVS "BIBINPUTS", "TEXBIB"
 
54
#define BST_ENVS "BSTINPUTS"
 
55
#define CNF_ENVS "TEXMFCNF"
 
56
#define DB_ENVS "TEXMFDBS"
 
57
#define FMT_ENVS "TEXFORMATS", "TEXMFINI"
 
58
#define FONTMAP_ENVS "TEXFONTMAPS", "TEXFONTS"
 
59
#define MEM_ENVS "MPMEMS", "TEXMFINI"
 
60
#define MF_ENVS "MFINPUTS"
 
61
#define MFPOOL_ENVS "MFPOOL", "TEXMFINI"
 
62
#define MFT_ENVS "MFTINPUTS"
 
63
#define MP_ENVS "MPINPUTS"
 
64
#define MPPOOL_ENVS "MPPOOL", "TEXMFINI" 
 
65
#define MPSUPPORT_ENVS "MPSUPPORT"
 
66
#define OCP_ENVS "OCPINPUTS"
 
67
#define OFM_ENVS "OFMFONTS", "TEXFONTS"
 
68
#define OPL_ENVS "OPLFONTS", "TEXFONTS"
 
69
#define OTP_ENVS "OTPINPUTS"
 
70
#define OVF_ENVS "OVFFONTS", "TEXFONTS"
 
71
#define OVP_ENVS "OVPFONTS", "TEXFONTS"
 
72
#define PICT_ENVS "TEXPICTS", TEX_ENVS
 
73
#define TEX_ENVS "TEXINPUTS"
 
74
#define TEXDOC_ENVS "TEXDOCS"
 
75
#define TEXPOOL_ENVS "TEXPOOL", "TEXMFINI"
 
76
#define TEXSOURCE_ENVS "TEXSOURCES"
 
77
#define TEX_PS_HEADER_ENVS "TEXPSHEADERS", "PSHEADERS"
 
78
#define TROFF_FONT_ENVS "TRFONTS"
 
79
#define TYPE1_ENVS "T1FONTS", "T1INPUTS", "TEXFONTS", TEX_PS_HEADER_ENVS
 
80
#define VF_ENVS "VFFONTS", "TEXFONTS"
 
81
#define DVIPS_CONFIG_ENVS "TEXCONFIG"
 
82
#define IST_ENVS "TEXINDEXSTYLE", "INDEXSTYLE"
 
83
#define TRUETYPE_ENVS "TTFONTS", "TEXFONTS"
 
84
#define TYPE42_ENVS "T42FONTS", "TEXFONTS"
 
85
#define WEB2C_ENVS "WEB2C"
 
86
#define MISCFONTS_ENVS "MISCFONTS", "TEXFONTS"
 
87
#define WEB_ENVS "WEBINPUTS"
 
88
#define CWEB_ENVS "CWEBINPUTS"
 
89
#define ENC_ENVS "ENCFONTS", "TEXFONTS"
 
90
#define CMAP_ENVS "CMAPFONTS", "TEXFONTS"
 
91
#define SFD_ENVS "SFDFONTS", "TEXFONTS"
 
92
#define OPENTYPE_ENVS "OPENTYPEFONTS", "TEXFONTS"
 
93
#define PDFTEXCONFIG_ENVS "PDFTEXCONFIG"
 
94
#define LIG_ENVS "LIGFONTS", "TEXFONTS"
 
95
#define TEXMFSCRIPTS_ENVS "TEXMFSCRIPTS"
 
96
 
 
97
/* The compiled-in default list, DEFAULT_FONT_SIZES, is intended to be
 
98
   set from the command line (presumably via the Makefile).  */
 
99
 
 
100
#ifndef DEFAULT_FONT_SIZES
 
101
#define DEFAULT_FONT_SIZES ""
 
102
#endif
 
103
 
 
104
void
 
105
kpse_init_fallback_resolutions P1C(string, envvar)
 
106
{
 
107
  string size;
 
108
  const_string size_var = ENVVAR (envvar, "TEXSIZES");
 
109
  string size_str = getenv (size_var);
 
110
  unsigned *last_resort_sizes = NULL;
 
111
  unsigned size_count = 0;
 
112
  const_string default_sizes = kpse_fallback_resolutions_string
 
113
                         ? kpse_fallback_resolutions_string
 
114
                         : DEFAULT_FONT_SIZES; 
 
115
  string size_list = kpse_expand_default (size_str, default_sizes);
 
116
  
 
117
  /* Initialize the list of last-resort sizes.  */
 
118
  for (size = kpse_path_element (size_list); size != NULL;
 
119
       size = kpse_path_element (NULL))
 
120
    {
 
121
      unsigned s;
 
122
      if (! *size) /* Skip empty elements.  */
 
123
        continue;
 
124
      
 
125
      s = atoi (size);
 
126
      if (size_count && s < last_resort_sizes[size_count - 1]) {
 
127
    WARNING1 ("kpathsea: last resort size %s not in ascending order, ignored",
 
128
          size);
 
129
      } else {
 
130
        size_count++;
 
131
        XRETALLOC (last_resort_sizes, size_count, unsigned);
 
132
        last_resort_sizes[size_count - 1] = atoi (size);
 
133
      }
 
134
    }
 
135
 
 
136
  /* Add a zero to mark the end of the list.  */
 
137
  size_count++;
 
138
  XRETALLOC (last_resort_sizes, size_count, unsigned);
 
139
  last_resort_sizes[size_count - 1] = 0;
 
140
 
 
141
  free (size_list);
 
142
    
 
143
  kpse_fallback_resolutions = last_resort_sizes;
 
144
}
 
145
 
 
146
/* We should be able to set the program arguments in the same way.  Not
 
147
   to mention the path values themselves.  */
 
148
 
 
149
void
 
150
kpse_set_program_enabled P3C(kpse_file_format_type, fmt,  boolean, value,
 
151
                             kpse_src_type, level)
 
152
{
 
153
  kpse_format_info_type *f = &kpse_format_info[fmt];
 
154
  if (level >= f->program_enable_level) {
 
155
    f->program_enabled_p = value;
 
156
    f->program_enable_level = level;
 
157
  }
 
158
}
 
159
 
 
160
 
 
161
/* Web2c and kpsewhich have command-line options to set this stuff.  May
 
162
   as well have a common place.  */
 
163
 
 
164
void
 
165
kpse_maketex_option P2C(const_string, fmtname,  boolean, value)
 
166
{
 
167
  kpse_file_format_type fmt = kpse_last_format;
 
168
  
 
169
  /* Trying to match up with the suffix lists unfortunately doesn't work
 
170
     well, since that would require initializing the formats.  */
 
171
  /* FIXME: Currently the function silently ignores unrecognized arguments.*/
 
172
  if (FILESTRCASEEQ (fmtname, "pk")) {
 
173
    fmt = kpse_pk_format;
 
174
  } else if (FILESTRCASEEQ (fmtname, "mf")) {
 
175
    fmt = kpse_mf_format;
 
176
  } else if (FILESTRCASEEQ (fmtname, "tex")) {
 
177
    fmt = kpse_tex_format;
 
178
  } else if (FILESTRCASEEQ (fmtname, "tfm")) {
 
179
    fmt = kpse_tfm_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
 
 
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.  */
 
296
/* Some times it is convenient to modify the list of searched suffixes.
 
297
   *** Don't complain if this function goes away. ***
 
298
 */
 
299
void
 
300
kpse_set_suffixes PVAR2C(kpse_file_format_type, format,
 
301
  boolean, alternate,  ap)
 
302
{
 
303
  const_string **list;
 
304
  const_string s;
 
305
  int count = 0;
 
306
 
 
307
  if (alternate) {
 
308
    list = &kpse_format_info[format].alt_suffix;
 
309
  } 
 
310
  else {
 
311
    list = &kpse_format_info[format].suffix;
 
312
  }
 
313
 
 
314
  while ((s = va_arg (ap, string)) != NULL) {
 
315
    count++;
 
316
    XRETALLOC (*list, count + 1, const_string);
 
317
    (*list)[count - 1] = s;
 
318
  }
 
319
  va_end (ap);
 
320
  (*list)[count] = NULL;
 
321
 
 
322
}}
 
323
 
 
324
/* The path spec we are defining, one element of the global array.  */
 
325
#define FMT_INFO kpse_format_info[format]
 
326
/* Call kpse_set_add_suffixes.  */
 
327
#define SUFFIXES(args) kpse_set_suffixes(format, false, args, NULL)
 
328
#define ALT_SUFFIXES(args) kpse_set_suffixes(format, true, args, NULL)
 
329
 
 
330
/* Call `init_path', including appending the trailing NULL to the envvar
 
331
   list. Also initialize the fields not needed in setting the path.  */
 
332
#define INIT_FORMAT(text, default_path, envs) \
 
333
  FMT_INFO.type = text; \
 
334
  init_path (&FMT_INFO, default_path, envs, NULL)
 
335
 
 
336
 
 
337
/* A few file types allow for runtime generation by an external program.
 
338
   kpse_init_prog may have already initialized it (the `program'
 
339
   member).  Here we allow people to turn it off or on in the config
 
340
   file, by setting the variable whose name is the uppercasified program
 
341
   name to 0 or 1.  */
 
342
 
 
343
static void
 
344
init_maketex PVAR2C(kpse_file_format_type, fmt,  const_string, dflt_prog, ap)
 
345
{
 
346
  kpse_format_info_type *f = &kpse_format_info[fmt];
 
347
  const_string prog = f->program ? f->program : dflt_prog; /* mktexpk */
 
348
  string PROG = uppercasify (prog);             /* MKTEXPK */
 
349
  string progval = kpse_var_value (PROG);       /* ENV/cnf{"MKTEXPK"} */
 
350
  const_string arg;
 
351
 
 
352
  /* Doesn't hurt to always set this info.  */
 
353
  f->program = prog;
 
354
 
 
355
  /* Set up the argument vector. */
 
356
  f->argc = 0;
 
357
  f->argv = XTALLOC(2, const_string);
 
358
  f->argv[f->argc++] = dflt_prog;
 
359
  while ((arg = va_arg (ap, string)) != NULL) {
 
360
    f->argc++;
 
361
    XRETALLOC (f->argv, f->argc + 1, const_string);
 
362
    f->argv[f->argc - 1] = arg;
 
363
  }
 
364
  f->argv[f->argc] = NULL;
 
365
 
 
366
  if (progval && *progval) {
 
367
    /* This might actually be from an environment variable value, but in
 
368
       that case, we'll have previously set it from kpse_init_prog.  */
 
369
    kpse_set_program_enabled (fmt, *progval == '1', kpse_src_client_cnf);
 
370
  }
 
371
  
 
372
  free (PROG);
 
373
}}
 
374
 
 
375
/* We need this twice, so ... */
 
376
#define MKTEXPK_ARGS \
 
377
  "--mfmode","$MAKETEX_MODE",\
 
378
  "--bdpi","$MAKETEX_BASE_DPI",\
 
379
  "--mag","$MAKETEX_MAG",\
 
380
  "--dpi","$KPATHSEA_DPI",\
 
381
  NULL
 
382
 
 
383
static string
 
384
remove_dbonly P1C(const_string, path)
 
385
{
 
386
  string ret = XTALLOC(strlen (path) + 1, char), q=ret;
 
387
  const_string p=path;
 
388
  boolean new_elt=true;
 
389
 
 
390
  while (*p) {
 
391
    if (new_elt && *p && *p == '!' && *(p+1) == '!')
 
392
      p += 2;
 
393
    else {
 
394
      new_elt = (*p == ENV_SEP);
 
395
      *q++ = *p++;
 
396
    }
 
397
  }
 
398
  *q = '\0';
 
399
  return(ret);
 
400
}
 
401
 
 
402
/* Initialize everything for FORMAT.  */
 
403
 
 
404
const_string
 
405
kpse_init_format P1C(kpse_file_format_type, format)
 
406
{
 
407
  /* If we get called twice, don't redo all the work.  */
 
408
  if (FMT_INFO.path)
 
409
    return FMT_INFO.path;
 
410
    
 
411
  switch (format)
 
412
    { /* We might be able to avoid repeating `gf' or whatever so many
 
413
         times with token pasting, but it doesn't seem worth it.  */
 
414
    case kpse_gf_format:
 
415
      INIT_FORMAT ("gf", DEFAULT_GFFONTS, GF_ENVS);
 
416
      SUFFIXES ("gf");
 
417
      FMT_INFO.suffix_search_only = true;
 
418
      FMT_INFO.binmode = true;
 
419
      break;
 
420
    case kpse_pk_format:
 
421
      init_maketex (format, "mktexpk", MKTEXPK_ARGS);
 
422
      INIT_FORMAT ("pk", DEFAULT_PKFONTS, PK_ENVS);
 
423
      SUFFIXES ("pk");
 
424
      FMT_INFO.suffix_search_only = true;
 
425
      FMT_INFO.binmode = true;
 
426
      break;
 
427
    case kpse_any_glyph_format:
 
428
      init_maketex (format, "mktexpk", MKTEXPK_ARGS);
 
429
      INIT_FORMAT ("bitmap font", DEFAULT_GLYPHFONTS, GLYPH_ENVS);
 
430
      FMT_INFO.suffix_search_only = true;
 
431
      FMT_INFO.binmode = true;
 
432
      break;
 
433
    case kpse_tfm_format:
 
434
      /* Must come before kpse_ofm_format. */
 
435
      init_maketex (format, "mktextfm", NULL);
 
436
      INIT_FORMAT ("tfm", DEFAULT_TFMFONTS, TFM_ENVS);
 
437
      SUFFIXES (".tfm");
 
438
      FMT_INFO.suffix_search_only = true;
 
439
      FMT_INFO.binmode = true;
 
440
      break;
 
441
    case kpse_afm_format:
 
442
      INIT_FORMAT ("afm", DEFAULT_AFMFONTS, AFM_ENVS);
 
443
      SUFFIXES (".afm");
 
444
      break;
 
445
    case kpse_base_format:
 
446
      init_maketex (format, "mktexfmt", NULL);
 
447
      INIT_FORMAT ("base", DEFAULT_MFBASES, BASE_ENVS);
 
448
      SUFFIXES (".base");
 
449
      FMT_INFO.binmode = true;
 
450
      break;
 
451
    case kpse_bib_format:
 
452
      INIT_FORMAT ("bib", DEFAULT_BIBINPUTS, BIB_ENVS);
 
453
      SUFFIXES (".bib");
 
454
      FMT_INFO.suffix_search_only = true;
 
455
      break;
 
456
    case kpse_bst_format:
 
457
      INIT_FORMAT ("bst", DEFAULT_BSTINPUTS, BST_ENVS);
 
458
      SUFFIXES (".bst");
 
459
      break;
 
460
    case kpse_cnf_format:
 
461
      INIT_FORMAT ("cnf", DEFAULT_TEXMFCNF, CNF_ENVS);
 
462
      SUFFIXES (".cnf");
 
463
      break;
 
464
    case kpse_db_format:
 
465
      INIT_FORMAT ("ls-R", DEFAULT_TEXMFDBS, DB_ENVS);
 
466
#define LSR_SUFFIXES "ls-R", "ls-r"
 
467
      SUFFIXES (LSR_SUFFIXES);
 
468
      FMT_INFO.path = remove_dbonly (FMT_INFO.path);
 
469
      break;
 
470
    case kpse_fmt_format:
 
471
      init_maketex (format, "mktexfmt", NULL);
 
472
      INIT_FORMAT ("fmt", DEFAULT_TEXFORMATS, FMT_ENVS);
 
473
      SUFFIXES (".fmt");
 
474
#define FMT_SUFFIXES ".efmt",".efm",".ofmt",".ofm",".oft",".eofmt",".eoft",".eof",".pfmt",".pfm",".epfmt",".epf",".xpfmt",".xpf",".afmt",".afm"
 
475
      ALT_SUFFIXES (FMT_SUFFIXES);
 
476
      FMT_INFO.binmode = true;
 
477
      break;
 
478
    case kpse_fontmap_format:
 
479
      INIT_FORMAT ("map", DEFAULT_TEXFONTMAPS, FONTMAP_ENVS);
 
480
      SUFFIXES (".map");
 
481
      break;
 
482
    case kpse_mem_format:
 
483
      init_maketex (format, "mktexfmt", NULL);
 
484
      INIT_FORMAT ("mem", DEFAULT_MPMEMS, MEM_ENVS);
 
485
      SUFFIXES (".mem");
 
486
      FMT_INFO.binmode = true;
 
487
      break;
 
488
    case kpse_mf_format:
 
489
      init_maketex (format, "mktexmf", NULL);
 
490
      INIT_FORMAT ("mf", DEFAULT_MFINPUTS, MF_ENVS);
 
491
      SUFFIXES (".mf");
 
492
      break;
 
493
    case kpse_mft_format:
 
494
      INIT_FORMAT ("mft", DEFAULT_MFTINPUTS, MFT_ENVS);
 
495
      SUFFIXES (".mft");
 
496
      break;
 
497
    case kpse_mfpool_format:
 
498
      INIT_FORMAT ("mfpool", DEFAULT_MFPOOL, MFPOOL_ENVS);
 
499
      SUFFIXES (".pool");
 
500
      break;
 
501
    case kpse_mp_format:
 
502
      INIT_FORMAT ("mp", DEFAULT_MPINPUTS, MP_ENVS);
 
503
      SUFFIXES (".mp");
 
504
      break;
 
505
    case kpse_mppool_format:
 
506
      INIT_FORMAT ("mppool", DEFAULT_MPPOOL, MPPOOL_ENVS);
 
507
      SUFFIXES (".pool");
 
508
      break;
 
509
    case kpse_mpsupport_format:
 
510
      INIT_FORMAT ("MetaPost support", DEFAULT_MPSUPPORT, MPSUPPORT_ENVS);
 
511
      break;
 
512
    case kpse_ocp_format:
 
513
      init_maketex (format, "mkocp", NULL);
 
514
      INIT_FORMAT ("ocp", DEFAULT_OCPINPUTS, OCP_ENVS);
 
515
      SUFFIXES (".ocp");
 
516
      FMT_INFO.suffix_search_only = true;
 
517
      FMT_INFO.binmode = true;
 
518
      break;
 
519
    case kpse_ofm_format:
 
520
      init_maketex (format, "mkofm", NULL);
 
521
      INIT_FORMAT ("ofm", DEFAULT_OFMFONTS, OFM_ENVS);
 
522
#define OFM_SUFFIXES ".ofm", ".tfm"
 
523
      SUFFIXES (OFM_SUFFIXES);
 
524
      FMT_INFO.suffix_search_only = true;
 
525
      FMT_INFO.binmode = true;
 
526
      break;
 
527
    case kpse_opl_format:
 
528
      INIT_FORMAT ("opl", DEFAULT_OPLFONTS, OPL_ENVS);
 
529
      SUFFIXES (".opl");
 
530
      FMT_INFO.suffix_search_only = true;
 
531
      break;
 
532
    case kpse_otp_format:
 
533
      INIT_FORMAT ("otp", DEFAULT_OTPINPUTS, OTP_ENVS);
 
534
      SUFFIXES (".otp");
 
535
      FMT_INFO.suffix_search_only = true;
 
536
      break;
 
537
    case kpse_ovf_format:
 
538
      INIT_FORMAT ("ovf", DEFAULT_OVFFONTS, OVF_ENVS);
 
539
      SUFFIXES (".ovf");
 
540
      FMT_INFO.suffix_search_only = true;
 
541
      FMT_INFO.binmode = true;
 
542
      break;
 
543
    case kpse_ovp_format:
 
544
      INIT_FORMAT ("ovp", DEFAULT_OVPFONTS, OVP_ENVS);
 
545
      SUFFIXES (".ovp");
 
546
      FMT_INFO.suffix_search_only = true;
 
547
      break;
 
548
    case kpse_pict_format:
 
549
      INIT_FORMAT ("graphic/figure", DEFAULT_TEXINPUTS, PICT_ENVS);
 
550
#define PICT_SUFFIXES ".eps", ".epsi"
 
551
      ALT_SUFFIXES (PICT_SUFFIXES);
 
552
      FMT_INFO.binmode = true;
 
553
      break;
 
554
    case kpse_tex_format:
 
555
      init_maketex (format, "mktextex", NULL);
 
556
      INIT_FORMAT ("tex", DEFAULT_TEXINPUTS, TEX_ENVS);
 
557
      SUFFIXES (".tex");
 
558
      /* We don't maintain a list of alternate TeX suffixes.  Such a list
 
559
         could never be complete.  */
 
560
      break;
 
561
    case kpse_tex_ps_header_format:
 
562
      INIT_FORMAT ("PostScript header", DEFAULT_TEXPSHEADERS,
 
563
                   TEX_PS_HEADER_ENVS);
 
564
/* Unfortunately, dvipsk uses this format for type1 fonts.  */
 
565
#define TEXPSHEADER_SUFFIXES ".pro"
 
566
      ALT_SUFFIXES (TEXPSHEADER_SUFFIXES);
 
567
      FMT_INFO.binmode = true;
 
568
      break;
 
569
    case kpse_texdoc_format:
 
570
      INIT_FORMAT ("TeX system documentation", DEFAULT_TEXDOCS, TEXDOC_ENVS);
 
571
      break;
 
572
    case kpse_texpool_format:
 
573
      INIT_FORMAT ("texpool", DEFAULT_TEXPOOL, TEXPOOL_ENVS);
 
574
      SUFFIXES (".pool");
 
575
      break;
 
576
    case kpse_texsource_format:
 
577
      INIT_FORMAT ("TeX system sources", DEFAULT_TEXSOURCES, TEXSOURCE_ENVS);
 
578
      break;
 
579
    case kpse_troff_font_format:
 
580
      INIT_FORMAT ("Troff fonts", DEFAULT_TRFONTS, TROFF_FONT_ENVS);
 
581
      FMT_INFO.binmode = true;
 
582
      break;
 
583
    case kpse_type1_format:
 
584
      INIT_FORMAT ("type1 fonts", DEFAULT_T1FONTS, TYPE1_ENVS);
 
585
#define TYPE1_SUFFIXES ".pfa", ".pfb"
 
586
      SUFFIXES (TYPE1_SUFFIXES);
 
587
      FMT_INFO.binmode = true;
 
588
      break;
 
589
    case kpse_vf_format:
 
590
      INIT_FORMAT ("vf", DEFAULT_VFFONTS, VF_ENVS);
 
591
      SUFFIXES (".vf");
 
592
      FMT_INFO.suffix_search_only = true;
 
593
      FMT_INFO.binmode = true;
 
594
      break;
 
595
    case kpse_dvips_config_format:
 
596
      INIT_FORMAT ("dvips config", DEFAULT_TEXCONFIG, DVIPS_CONFIG_ENVS);
 
597
      break;
 
598
    case kpse_ist_format:
 
599
      INIT_FORMAT ("ist", DEFAULT_INDEXSTYLE, IST_ENVS);
 
600
      SUFFIXES (".ist");
 
601
      break;
 
602
    case kpse_truetype_format:
 
603
      INIT_FORMAT ("truetype fonts", DEFAULT_TTFONTS, TRUETYPE_ENVS);
 
604
#define TRUETYPE_SUFFIXES ".ttf", ".ttc", ".TTF", ".TTC"
 
605
      SUFFIXES (TRUETYPE_SUFFIXES);
 
606
      FMT_INFO.suffix_search_only = false;
 
607
      FMT_INFO.binmode = true;
 
608
      break;
 
609
    case kpse_type42_format:
 
610
      INIT_FORMAT ("type42 fonts", DEFAULT_T42FONTS, TYPE42_ENVS);
 
611
      FMT_INFO.binmode = true;
 
612
      break;
 
613
    case kpse_web2c_format:
 
614
      INIT_FORMAT ("web2c files", DEFAULT_WEB2C, WEB2C_ENVS);
 
615
      break;
 
616
    case kpse_program_text_format:
 
617
      INIT_FORMAT ("other text files",
 
618
                   concatn (".", ENV_SEP_STRING, "$TEXMF/",
 
619
                            kpse_program_name, "//", NULL),
 
620
                   concat (uppercasify (kpse_program_name), "INPUTS"));
 
621
      break;
 
622
    case kpse_program_binary_format:
 
623
      INIT_FORMAT ("other binary files",
 
624
                   concatn (".", ENV_SEP_STRING, "$TEXMF/",
 
625
                            kpse_program_name, "//", NULL),
 
626
                   concat (uppercasify (kpse_program_name), "INPUTS"));
 
627
      FMT_INFO.binmode = true;
 
628
      break;
 
629
    case kpse_miscfonts_format:
 
630
      INIT_FORMAT ("misc fonts", DEFAULT_MISCFONTS, MISCFONTS_ENVS);
 
631
      FMT_INFO.binmode = true;
 
632
      break;
 
633
    case kpse_web_format:
 
634
      INIT_FORMAT ("web", DEFAULT_WEBINPUTS, WEB_ENVS);
 
635
      SUFFIXES (".web");
 
636
      ALT_SUFFIXES (".ch");
 
637
      break;
 
638
    case kpse_cweb_format:
 
639
      INIT_FORMAT ("cweb", DEFAULT_CWEBINPUTS, CWEB_ENVS);
 
640
#define CWEB_SUFFIXES ".w", ".web"
 
641
      SUFFIXES (CWEB_SUFFIXES);
 
642
      ALT_SUFFIXES (".ch");
 
643
      break;
 
644
    case kpse_enc_format:
 
645
      INIT_FORMAT ("enc files", DEFAULT_ENCFONTS, ENC_ENVS);
 
646
      SUFFIXES (".enc");
 
647
      break;
 
648
    case kpse_cmap_format:
 
649
      INIT_FORMAT ("cmap files", DEFAULT_CMAPFONTS, CMAP_ENVS);
 
650
      SUFFIXES (".cmap");      
 
651
      break;
 
652
    case kpse_sfd_format:
 
653
      INIT_FORMAT ("subfont definition files", DEFAULT_SFDFONTS, SFD_ENVS);
 
654
      SUFFIXES (".sfd");
 
655
      break;
 
656
    case kpse_opentype_format:
 
657
      INIT_FORMAT ("opentype fonts", DEFAULT_OPENTYPEFONTS, OPENTYPE_ENVS);
 
658
      SUFFIXES (".otf");
 
659
      FMT_INFO.binmode = true;
 
660
      break;
 
661
    case kpse_pdftex_config_format:
 
662
      INIT_FORMAT ("pdftex config", DEFAULT_PDFTEXCONFIG, PDFTEXCONFIG_ENVS);
 
663
      break;
 
664
    case kpse_lig_format:
 
665
      INIT_FORMAT ("lig files", DEFAULT_LIGFONTS, LIG_ENVS);
 
666
      SUFFIXES (".lig");
 
667
      break;
 
668
    case kpse_texmfscripts_format:
 
669
      INIT_FORMAT ("texmfscripts", DEFAULT_TEXMFSCRIPTS, TEXMFSCRIPTS_ENVS);
 
670
      break;
 
671
    default:
 
672
      FATAL1 ("kpse_init_format: Unknown format %d", format);
 
673
    }
 
674
 
 
675
#ifdef KPSE_DEBUG
 
676
#define MAYBE(member) (FMT_INFO.member ? FMT_INFO.member : "(none)")
 
677
 
 
678
  /* Describe the monster we've created.  */
 
679
  if (KPSE_DEBUG_P (KPSE_DEBUG_PATHS))
 
680
    {
 
681
      DEBUGF2 ("Search path for %s files (from %s)\n",
 
682
              FMT_INFO.type, FMT_INFO.path_source);
 
683
      DEBUGF1 ("  = %s\n", FMT_INFO.path);
 
684
      DEBUGF1 ("  before expansion = %s\n", FMT_INFO.raw_path);
 
685
      DEBUGF1 ("  application override path = %s\n", MAYBE (override_path));
 
686
      DEBUGF1 ("  application config file path = %s\n", MAYBE (client_path));
 
687
      DEBUGF1 ("  texmf.cnf path = %s\n", MAYBE (cnf_path));
 
688
      DEBUGF1 ("  compile-time path = %s\n", MAYBE (default_path));
 
689
      DEBUGF  ("  default suffixes =");
 
690
      if (FMT_INFO.suffix) {
 
691
        const_string *ext;
 
692
        for (ext = FMT_INFO.suffix; ext && *ext; ext++) {
 
693
          fprintf (stderr, " %s", *ext);
 
694
        }
 
695
        putc ('\n', stderr);
 
696
      } else {
 
697
        fputs (" (none)\n", stderr);
 
698
      }
 
699
      DEBUGF  ("  other suffixes =");
 
700
      if (FMT_INFO.alt_suffix) {
 
701
        const_string *alt;
 
702
        for (alt = FMT_INFO.alt_suffix; alt && *alt; alt++) {
 
703
          fprintf (stderr, " %s", *alt);
 
704
        }
 
705
        putc ('\n', stderr);
 
706
      } else {
 
707
        fputs (" (none)\n", stderr);
 
708
      }
 
709
      DEBUGF1 ("  search only with suffix = %d\n",FMT_INFO.suffix_search_only);
 
710
      DEBUGF1 ("  numeric format value = %d\n", format);
 
711
      DEBUGF1 ("  runtime generation program = %s\n", MAYBE (program));
 
712
      DEBUGF  ("  runtime generation command =");
 
713
      if (FMT_INFO.argv) {
 
714
        const_string *arg;
 
715
        for (arg = FMT_INFO.argv; *arg; arg++) {
 
716
          fprintf (stderr, " %s", *arg);
 
717
        }
 
718
        putc ('\n', stderr);
 
719
      } else {
 
720
          fputs(" (none)\n", stderr);
 
721
      }
 
722
      DEBUGF1 ("  program enabled = %d\n", FMT_INFO.program_enabled_p);
 
723
      DEBUGF1 ("  program enable level = %d\n", FMT_INFO.program_enable_level);
 
724
    }
 
725
#endif /* KPSE_DEBUG */
 
726
 
 
727
  return FMT_INFO.path;
 
728
}
 
729
 
 
730
/* Look up a file NAME of type FORMAT, and the given MUST_EXIST.  This
 
731
   initializes the path spec for FORMAT if it's the first lookup of that
 
732
   type.  Return the filename found, or NULL.  This is the most likely
 
733
   thing for clients to call.  */
 
734
   
 
735
string
 
736
kpse_find_file P3C(const_string, name,  kpse_file_format_type, format,
 
737
                   boolean, must_exist)
 
738
{
 
739
  const_string *ext;
 
740
  unsigned name_len = 0;
 
741
  boolean name_has_suffix_already = false;
 
742
  string mapped_name;
 
743
  string *mapped_names;
 
744
  const_string *target;
 
745
  unsigned count;
 
746
  boolean use_fontmaps = (format == kpse_tfm_format
 
747
                          || format == kpse_gf_format
 
748
                          || format == kpse_pk_format
 
749
                          || format == kpse_ofm_format);
 
750
  string ret = NULL;
 
751
 
 
752
  /* NAME being NULL is a programming bug somewhere.  NAME can be empty,
 
753
     though; this happens with constructs like `\input\relax'.  */
 
754
  assert (name);
 
755
  
 
756
  if (FMT_INFO.path == NULL)
 
757
    kpse_init_format (format);
 
758
 
 
759
  if (KPSE_DEBUG_P(KPSE_DEBUG_SEARCH))
 
760
    DEBUGF3 ("kpse_find_file: searching for %s of type %s (from %s)\n",
 
761
             name, FMT_INFO.type, FMT_INFO.path_source);
 
762
 
 
763
  /* Do variable and tilde expansion. */
 
764
  name = kpse_expand(name);
 
765
  
 
766
  /* Does NAME already end in a possible suffix?  */
 
767
  name_len = strlen (name);
 
768
  if (FMT_INFO.suffix) {
 
769
    for (ext = FMT_INFO.suffix; !name_has_suffix_already && *ext; ext++) {
 
770
      unsigned suffix_len = strlen (*ext);
 
771
      name_has_suffix_already = (name_len >= suffix_len
 
772
          && FILESTRCASEEQ (*ext, name + name_len - suffix_len));
 
773
    }
 
774
  }
 
775
  if (!name_has_suffix_already && FMT_INFO.alt_suffix) {
 
776
    for (ext = FMT_INFO.alt_suffix; !name_has_suffix_already && *ext; ext++) {
 
777
      unsigned suffix_len = strlen (*ext);
 
778
      name_has_suffix_already = (name_len >= suffix_len
 
779
          && FILESTRCASEEQ (*ext, name + name_len - suffix_len));
 
780
    }
 
781
  }
 
782
 
 
783
  /* Set up target list. */
 
784
  count = 0;
 
785
  target = XTALLOC1 (const_string);
 
786
  /* Case #1: NAME doesn't have a suffix which is equal to a "standard"
 
787
     suffix.  For example, foo.bar, but not foo.tex.  We look for the
 
788
     name with the standard suffixes appended. */
 
789
  if (!name_has_suffix_already && FMT_INFO.suffix) {
 
790
    for (ext = FMT_INFO.suffix; *ext; ext++) {
 
791
      string name_with_suffix = concat (name, *ext);
 
792
      target[count++] = name_with_suffix;
 
793
      XRETALLOC(target, count+1, const_string);
 
794
      if (use_fontmaps
 
795
          && (mapped_names = kpse_fontmap_lookup (name_with_suffix)) != NULL)
 
796
      {
 
797
        /* FIXME: we leak mapped_names and its elements, some of the time */
 
798
        while ((mapped_name = *mapped_names++) != NULL) {
 
799
          target[count++] = xstrdup (mapped_name);
 
800
          XRETALLOC(target, count+1, const_string);
 
801
        }
 
802
      }
 
803
    }
 
804
  }
 
805
  /* Case #2: Just look for the name we've been given, provided non-suffix
 
806
     searches are allowed or the name already includes a suffix. */
 
807
  if (name_has_suffix_already || !FMT_INFO.suffix_search_only) {
 
808
    target[count++] = xstrdup (name);
 
809
    XRETALLOC(target, count+1, const_string);
 
810
    if (use_fontmaps && (mapped_names = kpse_fontmap_lookup (name)) != NULL) {
 
811
      /* FIXME: we leak mapped_names and its elements, some of the time */
 
812
      while ((mapped_name = *mapped_names++) != NULL) {
 
813
        target[count++] = xstrdup (mapped_name);
 
814
        XRETALLOC(target, count+1, const_string);
 
815
      }
 
816
    }
 
817
  }
 
818
  /* Terminate list. */
 
819
  target[count] = NULL;
 
820
 
 
821
  /* Search. */
 
822
#if 0
 
823
  /* Simple version, just do the search for the list, and pound disk
 
824
   * then and there if appropriate.
 
825
   */
 
826
  ret = kpse_path_search_list (FMT_INFO.path, target, must_exist);
 
827
#elif 0
 
828
  /* Variant where we delay disk-pounding. */
 
829
  ret = kpse_path_search_list (FMT_INFO.path, target, false);
 
830
  if (!ret && must_exist)
 
831
      ret = kpse_path_search_list (FMT_INFO.path, target, true);
 
832
#else
 
833
  /* Variant that tries the match the original behaviour more closely.
 
834
   * The distinction is that we pound the disk for a far shorter list
 
835
   * of names.
 
836
   *
 
837
   * This is the fastest variant, because it does fewer disk accesses
 
838
   * than the two variants above -- for example some 750 calls of
 
839
   * access(2) compared to 1500.
 
840
   */
 
841
  ret = kpse_path_search_list (FMT_INFO.path, target, false);
 
842
  /* Do we need to pound the disk? */
 
843
  if (!ret && must_exist) {
 
844
    for (count = 0; target[count]; count++)
 
845
      free((void*)target[count]);
 
846
    count = 0;
 
847
    /* We look for a subset of the previous set of names, so the
 
848
     * target array is large enough.  In particular, we don't pound
 
849
     * the disk for alternate names from the fontmaps.  Perhaps we
 
850
     * should?
 
851
     */
 
852
    if (!name_has_suffix_already && FMT_INFO.suffix_search_only) {
 
853
      for (ext = FMT_INFO.suffix; !ret && *ext; ext++)
 
854
        target[count++] = concat (name, *ext);
 
855
    }
 
856
    if (name_has_suffix_already || !FMT_INFO.suffix_search_only) {
 
857
      target[count++] = xstrdup (name);
 
858
    }
 
859
    target[count] = NULL;
 
860
    ret = kpse_path_search_list (FMT_INFO.path, target, true);
 
861
  }
 
862
#endif  
 
863
  
 
864
  /* Free the list we created. */
 
865
  for (count = 0; target[count]; count++)
 
866
    free((void*)target[count]);
 
867
  free(target);
 
868
  
 
869
  /* If nothing was found, call mktex* to create a missing file.  */
 
870
  if (!ret && must_exist) {
 
871
    ret = kpse_make_tex (format, name);
 
872
  }
 
873
 
 
874
  free((void*)name);
 
875
 
 
876
  return ret;
 
877
}
 
878
 
 
879
/* Open NAME along the search path for TYPE for reading and return the
 
880
   resulting file, or exit with an error message.  */
 
881
 
 
882
FILE *
 
883
kpse_open_file P2C(const_string, name,  kpse_file_format_type, type)
 
884
{
 
885
  string fullname = kpse_find_file (name, type, true);
 
886
  const_string mode = kpse_format_info[type].binmode
 
887
                      ? FOPEN_RBIN_MODE
 
888
                      : FOPEN_R_MODE;
 
889
  FILE *f = fullname ? fopen (fullname, mode) : NULL;
 
890
  if (!f) {
 
891
    if (fullname) {
 
892
      perror (fullname);
 
893
      exit (1);
 
894
    } else {
 
895
      FATAL2 ("%s file `%s' not found", kpse_format_info[type].type, name);
 
896
    }
 
897
  }
 
898
  
 
899
  return f;
 
900
}
 
901
 
 
902
/* When using the %&<format> construct, we'd like to use the paths for
 
903
   that format, rather than those for the name we were called with.
 
904
   Of course this happens after various initializations have been
 
905
   performed, so we have this function to force the issue.  Note that
 
906
   the paths for kpse_cnf_format and kpse_db_format are not cleared.
 
907
 
 
908
   This function is defined here, and not in progname.c, because it
 
909
   need kpse_format_info, and would cause all of tex-file to be pulled
 
910
   in by programs that do not need it. */
 
911
void
 
912
kpse_reset_program_name P1C(const_string, progname)
 
913
{
 
914
  int i;
 
915
 
 
916
  /* It is a fatal error for either of these to be NULL. */
 
917
  assert (progname && kpse_program_name);
 
918
  /* Do nothing if the name is unchanged. */
 
919
  if (STREQ(kpse_program_name, progname))
 
920
    return;
 
921
 
 
922
  free (kpse_program_name);
 
923
  kpse_program_name = xstrdup (progname);
 
924
  xputenv("progname", kpse_program_name);
 
925
  
 
926
  /* Clear paths -- do we want the db path to be cleared? */
 
927
  for (i = 0; i != kpse_last_format; ++i) {
 
928
    /* Do not erase the cnf of db paths.  This means that the filename
 
929
       database is not rebuilt, nor are different configuration files
 
930
       searched.  The alternative is to tolerate a memory leak of up
 
931
       to 100k if this function is called. */
 
932
    if (i == kpse_cnf_format || i == kpse_db_format)
 
933
      continue;
 
934
    /* Wipe the path (it is tested) and the cnf_path and because their
 
935
       values may differ with the new program name.  */
 
936
    if (kpse_format_info[i].path != NULL) {
 
937
      free ((string)kpse_format_info[i].path);
 
938
      kpse_format_info[i].path = NULL;
 
939
    }
 
940
    /* We cannot free the cnf_path: it points into the cnf hash, which
 
941
       means all hell will break loose if we did. */
 
942
    if (kpse_format_info[i].cnf_path != NULL) {
 
943
      kpse_format_info[i].cnf_path = NULL;
 
944
    }
 
945
    /* We do not wipe the override_path at this point, though arguably
 
946
       we should provide new values.  It is not likely to matter for
 
947
       the programs that call this function. */
 
948
  }
 
949
}