Debian Codegraph @

texk/kpathsea/kpsewhich.c

raw
1 /* kpsewhich -- standalone path lookup and variable expansion for Kpathsea.
2 Ideas from Thomas Esser, Pierre MacKay, and many others.
3
4 Copyright 1995-2026 Karl Berry & 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 #include <kpathsea/c-ctype.h>
21 #include <kpathsea/c-pathch.h>
22 #include <kpathsea/cnf.h>
23 #include <kpathsea/expand.h>
24 #include <kpathsea/getopt.h>
25 #include <kpathsea/line.h>
26 #include <kpathsea/pathsearch.h>
27 #include <kpathsea/proginit.h>
28 #include <kpathsea/str-list.h>
29 #include <kpathsea/tex-file.h>
30 #include <kpathsea/tex-glyph.h>
31 #include <kpathsea/variable.h>
32 #include <kpathsea/version.h>
33
34 /* For variable and path expansion. (-expand-var, -expand-path,
35 -show-path, etc.) */
36 string var_to_expand = NULL;
37 string braces_to_expand = NULL;
38 string path_to_expand = NULL;
39 string path_to_show = NULL;
40 string var_to_value = NULL;
41 string var_to_brace_value = NULL;
42
43 /* Array/count of cnf lines from the command line. (--cnf-line) */
44 static string *user_cnf_lines = NULL;
45 static unsigned user_cnf_nlines = 0;
46
47 /* Base resolution. (-D, -dpi) */
48 unsigned dpi = 600;
49
50 /* The engine name, for `$engine' construct in texmf.cnf. (-engine) */
51 string engine = NULL;
52
53 /* Interactively ask for names to look up? (-interactive) */
54 boolean interactive = false;
55
56 /* The device name, for $MAKETEX_MODE. (-mode) */
57 string mode = NULL;
58
59 /* Search the disk as well as ls-R? (-must-exist, -mktex) */
60 boolean must_exist = false;
61
62 /* The program name, for `.PROG' construct in texmf.cnf. (-program) */
63 string progname = NULL;
64
65 /* Safe input and output names to check. (-safe-in-name, -safe-out-name) */
66 string safe_in_name = NULL;
67 string safe_out_name = NULL;
68
69 /* Check TEXMF[SYS]VAR too. (-safe-extended-in-name,-safe-extended-out-name) */
70 string safe_extended_in_name = NULL;
71 string safe_extended_out_name = NULL;
72
73 /* Return all matches, not just the first one? (-all) */
74 boolean show_all = false;
75
76 /* Only match files in given subdirs. (-subdir) */
77 str_list_type subdir_paths;
78
79 /* The file type and path for lookups. (-format, -path) */
80 kpse_file_format_type user_format = kpse_last_format;
81 string user_format_string;
82 string user_path;
83
84
85
86 /* Define one-word abbreviations for those format types which
87 can otherwise only be specified by strings containing spaces. */
88
89 typedef struct
90 {
91 const_string abbr;
92 kpse_file_format_type format;
93 } format_abbr_type;
94
95 static format_abbr_type format_abbrs[]
96 = { { "bitmapfont", kpse_any_glyph_format },
97 { "mpsupport", kpse_mpsupport_format },
98 { "psheader", kpse_tex_ps_header_format },
99 { "doc", kpse_texdoc_format },
100 { "source", kpse_texsource_format },
101 { "src", kpse_texsource_format },
102 { "trofffont", kpse_troff_font_format },
103 { "type1", kpse_type1_format },
104 { "pfb", kpse_type1_format },
105 { "dvipsconfig", kpse_dvips_config_format },
106 { "truetype", kpse_truetype_format, },
107 { "ttf", kpse_truetype_format, },
108 { "type42", kpse_type42_format, },
109 { "web2c", kpse_web2c_format },
110 { "othertext", kpse_program_text_format },
111 { "otherbin", kpse_program_binary_format },
112 { "miscfont", kpse_miscfonts_format },
113 { "enc", kpse_enc_format },
114 { "cmap", kpse_cmap_format },
115 { "sfd", kpse_sfd_format, },
116 { "opentype", kpse_opentype_format, },
117 { "otf", kpse_opentype_format, },
118 { "pdftexconfig", kpse_pdftex_config_format },
119 { "lig", kpse_lig_format },
120 { "fea", kpse_fea_format },
121 { "cidmap", kpse_cid_format },
122 { NULL, kpse_last_format } };
123
124 /* The function to look up STR in the abbr table above.
125 This is called only on a user-specified format string.
126 Return `kpse_last_format' if no match. */
127
128 static kpse_file_format_type
129 format_abbr (const_string str)
130 {
131 kpse_file_format_type ret = kpse_last_format;
132 unsigned a = 0;
133
134 while (format_abbrs[a].abbr != NULL) {
135 if (STREQ (str, format_abbrs[a].abbr)) {
136 ret = format_abbrs[a].format;
137 break;
138 }
139 a++;
140 }
141
142 return ret;
143 }
144
145
146
147 /* Return the <number> substring in `<name>.<number><stuff>', if S has
148 that form. If it doesn't, return 0. */
149
150 static unsigned
151 find_dpi (string s)
152 {
153 unsigned dpi_number = 0;
154 const_string extension = find_suffix (s);
155
156 if (extension != NULL)
157 sscanf (extension, "%u", &dpi_number);
158
159 return dpi_number;
160 }
161
162
163
164 /* Return true if FTRY (the candidate suffix) matches NAME. If
165 IS_FILENAME is true, the check is simply that FTRY is a suffix of
166 NAME. If false (that is, NAME is a format), then FTRY and NAME must
167 be entirely equal. */
168
169 static boolean
170 try_suffix (boolean is_filename, string name, unsigned name_len,
171 const_string ftry)
172 {
173 unsigned try_len;
174
175 if (!ftry || ! *ftry) {
176 return false;
177 }
178
179 try_len = strlen (ftry);
180 if (try_len > name_len) {
181 /* Candidate is longer than what we're looking for. */
182 return false;
183 }
184 if (!is_filename && try_len < name_len) {
185 /* We're doing format names, not file names, and candidate is
186 shorter than what we're looking for. E.g., do not find `lua'
187 when looking for `clua'. */
188 return false;
189 }
190
191 if (FILESTRCASEEQ (name + name_len - try_len, ftry)) {
192 return true;
193 }
194
195 return false;
196 }
197
198
199
200 /* Use the file type from -format if that was previously determined
201 (i.e., the user_format global variable), else guess dynamically from
202 NAME. Return kpse_last_format if undeterminable. This function is
203 also used to parse the -format string, a case we distinguish via
204 is_filename being false.
205
206 A few filenames have been hard-coded for format types that
207 differ from what would be inferred from their extensions. */
208
209 static kpse_file_format_type
210 find_format (kpathsea kpse, string name, boolean is_filename)
211 {
212 kpse_file_format_type ret = kpse_last_format;
213
214 if (is_filename && user_format != kpse_last_format) {
215 ret = user_format; /* just return what we already computed */
216
217 } else if (FILESTRCASEEQ (name, "config.ps")) {
218 ret = kpse_dvips_config_format;
219 } else if (FILESTRCASEEQ (name, "fmtutil.cnf")) {
220 ret = kpse_web2c_format;
221 } else if (FILESTRCASEEQ (name, "glyphlist.txt")) {
222 ret = kpse_fontmap_format;
223 } else if (FILESTRCASEEQ (name, "mktex.cnf")) {
224 ret = kpse_web2c_format;
225 } else if (FILESTRCASEEQ (name, "pdfglyphlist.txt")) {
226 ret = kpse_fontmap_format;
227 } else if (FILESTRCASEEQ (name, "pdftex.cfg")) {
228 ret = kpse_pdftex_config_format;
229 } else if (FILESTRCASEEQ (name, "texglyphlist.txt")) {
230 ret = kpse_fontmap_format;
231 } else if (FILESTRCASEEQ (name, "texmf.cnf")) {
232 ret = kpse_cnf_format;
233 } else if (FILESTRCASEEQ (name, "updmap.cfg")) {
234 ret = kpse_web2c_format;
235 } else if (FILESTRCASEEQ (name, "XDvi")) {
236 ret = kpse_program_text_format;
237 } else {
238 if (!is_filename) {
239 /* Look for kpsewhich-specific format abbreviations. */
240 ret = format_abbr (name);
241 }
242
243 if (ret == kpse_last_format) {
244 int f = 0; /* kpse_file_format_type */
245 unsigned name_len = strlen (name);
246
247 while (f != kpse_last_format) {
248 const_string *ext;
249 const_string ftry;
250 boolean found = false;
251
252 if (!kpse->format_info[f].type)
253 kpathsea_init_format (kpse, (kpse_file_format_type) f);
254
255 /* Just to abbreviate this lengthy call. */
256 #define TRY_SUFFIX(ftry) try_suffix (is_filename, name, name_len, (ftry))
257
258 if (!is_filename) {
259 /* Allow the long name, but only in the format options. We don't
260 want a filename confused with a format name. */
261 ftry = kpse->format_info[f].type;
262 found = TRY_SUFFIX (ftry);
263 }
264 for (ext = kpse->format_info[f].suffix; !found && ext && *ext; ext++) {
265 found = TRY_SUFFIX (*ext);
266 }
267 for (ext=kpse->format_info[f].alt_suffix; !found && ext && *ext;ext++){
268 found = TRY_SUFFIX (*ext);
269 }
270
271 if (found)
272 break;
273 f++;
274 }
275 ret = f;
276 }
277 }
278 return ret;
279 }
280
281
282
283 /* Return newly-allocated NULL-terminated list of strings from MATCHES
284 that are prefixed with any of the subdirectories in SUBDIRS. That
285 is, for a string S in MATCHES, its dirname must end with one of the
286 elements in SUBDIRS. For instance, if subdir=foo/bar, that will
287 match a string foo/bar/baz or /some/texmf/foo/bar/baz.
288
289 We don't reallocate the actual strings, just the list elements.
290 Perhaps later we will implement wildcards or // or something. */
291
292 #if defined(WIN32)
293 static string *
294 kpathsea_subdir_match (kpathsea kpse, str_list_type subdirs, string *matches)
295 {
296 string *ret = XTALLOC1 (string);
297 unsigned len = 1;
298 unsigned e;
299 unsigned m;
300 string p;
301
302 for (e = 0; e < STR_LIST_LENGTH (subdirs); e++) {
303 for (p = STR_LIST_ELT (subdirs, e); *p; p++) {
304 if (*p == '\\')
305 *p = '/';
306 else if (kpathsea_IS_KANJI(kpse, p))
307 p++;
308 }
309 }
310
311 for (m = 0; matches[m]; m++) {
312 unsigned loc;
313 string s = xstrdup (matches[m]);
314 for (loc = strlen (s); loc > 0 && !IS_DIR_SEP_CH (s[loc-1]); loc--)
315 ;
316 while (loc > 0 && IS_DIR_SEP_CH (s[loc-1])) {
317 loc--;
318 }
319 s[loc] = 0; /* wipe out basename */
320
321 for (e = 0; e < STR_LIST_LENGTH (subdirs); e++) {
322 string subdir = STR_LIST_ELT (subdirs, e);
323 unsigned subdir_len = strlen (subdir);
324 while (subdir_len > 0 && IS_DIR_SEP_CH (subdir[subdir_len-1])) {
325 subdir_len--;
326 subdir[subdir_len] = 0; /* remove trailing slashes from subdir spec */
327 }
328 if (FILESTRCASEEQ (subdir, s + loc - subdir_len)) {
329 /* matched, save this one. */
330 XRETALLOC (ret, len + 1, string);
331 ret[len-1] = matches[m];
332 len++;
333 }
334 }
335 free (s);
336 }
337 ret[len-1] = NULL;
338 return ret;
339 }
340
341 #else /* WIN32 */
342 static string *
343 subdir_match (str_list_type subdirs, string *matches)
344 {
345 string *ret = XTALLOC1 (string);
346 unsigned len = 1;
347 unsigned e;
348 unsigned m;
349
350 for (m = 0; matches[m]; m++) {
351 unsigned loc;
352 string s = xstrdup (matches[m]);
353 for (loc = strlen (s); loc > 0 && !IS_DIR_SEP_CH (s[loc-1]); loc--)
354 ;
355 while (loc > 0 && IS_DIR_SEP_CH (s[loc-1])) {
356 loc--;
357 }
358 s[loc] = 0; /* wipe out basename */
359
360 for (e = 0; e < STR_LIST_LENGTH (subdirs); e++) {
361 string subdir = STR_LIST_ELT (subdirs, e);
362 unsigned subdir_len = strlen (subdir);
363 while (subdir_len > 0 && IS_DIR_SEP_CH (subdir[subdir_len-1])) {
364 subdir_len--;
365 subdir[subdir_len] = 0; /* remove trailing slashes from subdir spec */
366 }
367 if (FILESTRCASEEQ (subdir, s + loc - subdir_len)) {
368 /* matched, save this one. */
369 XRETALLOC (ret, len + 1, string);
370 ret[len-1] = matches[m];
371 len++;
372 }
373 }
374 free (s);
375 }
376 ret[len-1] = NULL;
377 return ret;
378 }
379 #endif /* WIN32 */
380
381
382 /* Look up a single filename NAME, filtering by given subdirectories if
383 -subdir was specified. Print all matches if global `show_all' is
384 true, else just the first match. Return 0 if success, 1 if failure. */
385
386 static unsigned
387 lookup (kpathsea kpse, string name)
388 {
389 int i;
390 string ret = NULL;
391 string *ret_list = NULL;
392
393 if (user_path) {
394 /* Translate ; to : if that's our ENV_SEP. See cnf.c. */
395 if (IS_ENV_SEP (':')) {
396 string loc;
397 for (loc = user_path; *loc; loc++) {
398 if (*loc == ';')
399 *loc = ':';
400 }
401 }
402 user_path = kpathsea_path_expand (kpse, user_path);
403 if (show_all) {
404 ret_list = kpathsea_all_path_search (kpse, user_path, name);
405 } else {
406 ret = kpathsea_path_search (kpse, user_path, name, must_exist);
407 }
408
409 } else {
410 /* No user-specified search path, check user format or guess from NAME. */
411 kpse_file_format_type fmt = find_format (kpse, name, true);
412
413 switch (fmt) {
414 case kpse_pk_format:
415 case kpse_gf_format:
416 case kpse_any_glyph_format:
417 {
418 kpse_glyph_file_type glyph_ret;
419 string temp = remove_suffix (name);
420 /* Try to extract the resolution from the name. */
421 unsigned local_dpi = find_dpi (name);
422 if (!local_dpi)
423 local_dpi = dpi;
424 if (show_all) {
425 /* Because the whole glyph lookup thing is too complicated
426 to try all the alternatives (cmr10.600pk vs.
427 dpi600/cmr10.pk, just for starters). Patches welcome :). */
428 WARNING1 ("kpsewhich: Ignoring --all for bitmap font, sorry: %s",
429 name);
430 }
431 ret = kpathsea_find_glyph (kpse, temp,
432 local_dpi, fmt, &glyph_ret);
433 if (temp != name)
434 free (temp);
435 }
436 break;
437
438 case kpse_last_format:
439 /* If the suffix isn't recognized, assume it's a tex file. */
440 fmt = kpse_tex_format;
441 /* fall through */
442
443 default:
444 if (show_all) {
445 ret_list = kpathsea_find_file_generic (kpse, name, fmt,
446 must_exist, true);
447 } else {
448 ret = kpathsea_find_file (kpse, name, fmt, must_exist);
449 }
450 }
451 }
452
453 /* Turn single return into a null-terminated list for uniform treatment. */
454 if (ret) {
455 ret_list = XTALLOC (2, string);
456 ret_list[0] = ret;
457 ret_list[1] = NULL;
458 }
459
460 /* Filter by subdirectories, if specified. */
461 if (!STR_LIST_EMPTY (subdir_paths)) {
462 #if defined(WIN32)
463 string *new_list = kpathsea_subdir_match (kpse, subdir_paths, ret_list);
464 #else
465 string *new_list = subdir_match (subdir_paths, ret_list);
466 #endif /* WIN32 */
467 free (ret_list);
468 ret_list = new_list;
469 }
470
471 /* Print output. */
472 if (ret_list) {
473 for (i = 0; ret_list[i]; i++)
474 #ifdef WIN32
475 kpathsea_win32_puts (kpse, ret_list[i]);
476 #else
477 puts (ret_list[i]);
478 #endif
479 /* Save whether we found anything */
480 ret = ret_list[0];
481 free (ret_list);
482 }
483
484 return ret == NULL;
485 }
486
487 /* Help message. */
488
489 #define USAGE "\n\
490 Standalone path lookup and expansion for the Kpathsea library.\n\
491 The default is to look up each FILENAME in turn and report its\n\
492 first match (if any) to standard output.\n\
493 \n\
494 If multiple FILENAMEs are given and a given file is not found, a blank\n\
495 line is written to standard output.\n\
496 \n\
497 When looking up format (.fmt/.base/.mem) files, it is usually necessary\n\
498 to also use -engine, or nothing will be returned; in particular,\n\
499 -engine=/ will return matching format files for any engine.\n\
500 \n\
501 The exit status is 0 if all files are found, nonzero otherwise.\n\
502 \n\
503 -all output all matches, one per line (no effect with pk/gf).\n\
504 [-no]-casefold-search fall back to case-insensitive search if no exact match.\n\
505 -cnf-line=STRING handle STRING as a configuration file line.\n\
506 -debug=NUM set debugging flags.\n\
507 -D, -dpi=NUM use a base resolution of NUM; default 600.\n\
508 -engine=STRING set engine name to STRING.\n\
509 -expand-braces=STRING output variable and brace expansion of STRING.\n\
510 -expand-path=STRING output complete path expansion of STRING.\n\
511 -expand-var=STRING output variable expansion of STRING.\n\
512 -format=NAME use file type NAME (list shown by -help-formats).\n\
513 -help display this message and exit.\n\
514 -help-formats display information about all supported file formats.\n\
515 -interactive ask for additional filenames to look up.\n\
516 [-no]-mktex=FMT disable/enable mktexFMT generation (FMT=pk/mf/tex/tfm).\n\
517 -mode=STRING set device name for $MAKETEX_MODE to STRING; no default.\n\
518 -must-exist search the disk as well as ls-R if necessary.\n\
519 -path=STRING search in the path STRING.\n\
520 -progname=STRING set program name to STRING.\n\
521 -safe-in-name=STRING check if STRING is ok to open for input.\n\
522 -safe-out-name=STRING check if STRING is ok to open for output.\n\
523 -safe-extended-in-name=STRING also check TEXMF[SYS]VAR.\n\
524 -safe-extended-out-name=STRING also check TEXMF[SYS]VAR.\n\
525 -show-path=TYPE output search path for file type TYPE\n\
526 (list shown by -help-formats).\n\
527 -subdir=STRING only output matches whose directory ends with STRING.\n\
528 -var-brace-value=STRING output brace-expanded value of variable STRING.\n\
529 -var-value=STRING output variable-expanded value of variable STRING.\n\
530 -version display version information number and exit.\n \
531 "
532
533 static void
534 help_message (kpathsea kpse, string *argv)
535 {
536 printf ("Usage: %s [OPTION]... [FILENAME]...\n", argv[0]);
537 fputs (USAGE, stdout);
538 putchar ('\n');
539 fputs (kpathsea_bug_address, stdout);
540 fputs ("Kpathsea home page: https://tug.org/kpathsea/\n", stdout);
541 exit (0);
542 }
543
544 static void
545 help_formats (kpathsea kpse, string *argv)
546 {
547 int f; /* kpse_file_format_type */
548
549 /* Have to set this for init_format to work. */
550 kpathsea_set_program_name (kpse, argv[0], progname);
551
552 puts (kpathsea_version_string);
553 puts ("\nRecognized Kpathsea format names and their (abbreviations) and suffixes:");
554 for (f = 0; f < kpse_last_format; f++) {
555 const_string *ext;
556
557 const_string envvar_list =
558 kpathsea_init_format_return_varlist (kpse, (kpse_file_format_type) f);
559 printf ("%s", kpse->format_info[f].type);
560
561 /* Show abbreviation if we accept one. We repeatedly go through the
562 abbr list here, but it's so short, it doesn't matter. */
563 {
564 unsigned a = 0;
565 while (format_abbrs[a].abbr != NULL) {
566 if (f == format_abbrs[a].format) {
567 printf (" (%s)", format_abbrs[a].abbr);
568 break;
569 }
570 a++;
571 }
572 }
573
574 /* Regular suffixes. */
575 putchar (':');
576 for (ext = kpse->format_info[f].suffix; ext && *ext; ext++) {
577 putchar (' ');
578 #ifdef WIN32
579 kpathsea_win32_fputs (kpse, *ext, stdout);
580 #else
581 fputs (*ext, stdout);
582 #endif
583 }
584
585 if (kpse->format_info[f].alt_suffix) {
586 /* leave extra space between default and alt suffixes */
587 putchar (' ');
588 }
589 for (ext = kpse->format_info[f].alt_suffix; ext && *ext; ext++) {
590 putchar (' ');
591 #ifdef WIN32
592 kpathsea_win32_fputs (kpse, *ext, stdout);
593 #else
594 fputs (*ext, stdout);
595 #endif
596 }
597
598 printf (" [variables: %s]\n", envvar_list);
599
600 printf (" [original path (from %s) = %s]\n",
601 kpse->format_info[f].path_source, kpse->format_info[f].raw_path);
602 }
603
604 fputs ("\nTo see paths after expansion, use --show-path=FMT.\n\n", stdout);
605 fputs (kpathsea_bug_address, stdout);
606 exit (0);
607 }
608
609
610 /* Reading the options. */
611
612 /* This macro tests whether getopt found an option ``A''.
613 Assumes the option index is in the variable `option_index', and the
614 option table in a variable `long_options'. */
615 #define ARGUMENT_IS(a) STREQ (long_options[option_index].name, a)
616
617 /* If S is NULL or the empty string, abort. */
618 #define ENSURE_NONEMPTY_STRING(s) do { \
619 if (!(s) || ! *(s)) { \
620 fprintf (stderr, "kpsewhich: empty argument for %s\n", \
621 long_options[option_index].name); \
622 fputs ("Try `kpsewhich --help' for more information.\n", stderr); \
623 exit (EXIT_FAILURE); } } while (0)
624
625 /* SunOS cc can't initialize automatic structs. */
626 static struct option long_options[]
627 = { { "D", 1, 0, 0 },
628 { "all", 0, (int *) &show_all, 1 },
629 { "casefold-search", 0, 0, 0 },
630 { "cnf-line", 1, 0, 0 },
631 { "debug", 1, 0, 0 },
632 { "dpi", 1, 0, 0 },
633 { "engine", 1, 0, 0 },
634 { "expand-braces", 1, 0, 0 },
635 { "expand-path", 1, 0, 0 },
636 { "expand-var", 1, 0, 0 },
637 { "format", 1, 0, 0 },
638 { "help", 0, 0, 0 },
639 { "help-formats", 0, 0, 0 },
640 { "interactive", 0, (int *) &interactive, 1 },
641 { "mktex", 1, 0, 0 },
642 { "mode", 1, 0, 0 },
643 { "must-exist", 0, (int *) &must_exist, 1 },
644 { "path", 1, 0, 0 },
645 { "no-casefold-search", 0, 0, 0 },
646 { "no-mktex", 1, 0, 0 },
647 { "progname", 1, 0, 0 },
648 { "safe-extended-in-name",1, 0, 0 },
649 { "safe-extended-out-name",1,0, 0 },
650 { "safe-in-name", 1, 0, 0 },
651 { "safe-out-name", 1, 0, 0 },
652 { "subdir", 1, 0, 0 },
653 { "show-path", 1, 0, 0 },
654 { "var-brace-value", 1, 0, 0 },
655 { "var-value", 1, 0, 0 },
656 { "version", 0, 0, 0 },
657 { 0, 0, 0, 0 } };
658
659 static void
660 read_command_line (kpathsea kpse, int argc, string *argv)
661 {
662 int g; /* `getopt' return code. */
663 int option_index;
664
665 for (;;) {
666 g = getopt_long_only (argc, argv, "", long_options, &option_index);
667
668 if (g == -1)
669 break;
670
671 if (g == '?')
672 exit (EXIT_FAILURE); /* Unknown option. */
673
674 assert (g == 0); /* We have no short option names. */
675
676 if (ARGUMENT_IS ("casefold-search")) {
677 /* We can't use a boolean for casefold-search because we want to
678 distinguish it being set with an option vs. leaving the default
679 (by default). */
680 xputenv ("texmf_casefold_search", "1");
681
682 } else if (ARGUMENT_IS ("cnf-line")) {
683 if (user_cnf_lines == NULL) {
684 user_cnf_nlines = 1;
685 user_cnf_lines = xmalloc (sizeof (const_string));
686 } else {
687 user_cnf_nlines++;
688 user_cnf_lines = xrealloc (user_cnf_lines,
689 user_cnf_nlines * sizeof (const_string));
690 }
691 user_cnf_lines[user_cnf_nlines-1] = xstrdup (optarg);
692
693 } else if (ARGUMENT_IS ("debug")) {
694 kpse->debug |= atoi (optarg);
695
696 } else if (ARGUMENT_IS ("dpi") || ARGUMENT_IS ("D")) {
697 dpi = atoi (optarg);
698
699 } else if (ARGUMENT_IS ("engine")) {
700 engine = optarg;
701
702 } else if (ARGUMENT_IS ("expand-braces")) {
703 braces_to_expand = optarg;
704
705 } else if (ARGUMENT_IS ("expand-path")) {
706 path_to_expand = optarg;
707
708 } else if (ARGUMENT_IS ("expand-var")) {
709 var_to_expand = optarg;
710
711 } else if (ARGUMENT_IS ("format")) {
712 ENSURE_NONEMPTY_STRING (optarg);
713 user_format_string = optarg;
714
715 } else if (ARGUMENT_IS ("help")) {
716 help_message (kpse, argv);
717
718 } else if (ARGUMENT_IS ("help-formats")) {
719 help_formats (kpse, argv);
720
721 } else if (ARGUMENT_IS ("mktex")) {
722 kpathsea_maketex_option (kpse, optarg, true);
723 must_exist = 1; /* otherwise it never gets called */
724
725 } else if (ARGUMENT_IS ("mode")) {
726 mode = optarg;
727
728 } else if (ARGUMENT_IS ("no-casefold-search")) {
729 xputenv ("texmf_casefold_search", "0");
730
731 } else if (ARGUMENT_IS ("no-mktex")) {
732 kpathsea_maketex_option (kpse, optarg, false);
733 must_exist = 0;
734
735 } else if (ARGUMENT_IS ("path")) {
736 user_path = optarg;
737
738 } else if (ARGUMENT_IS ("progname")) {
739 progname = optarg;
740
741 } else if (ARGUMENT_IS ("safe-extended-in-name")) {
742 ENSURE_NONEMPTY_STRING (optarg);
743 safe_extended_in_name = optarg;
744
745 } else if (ARGUMENT_IS ("safe-extended-out-name")) {
746 ENSURE_NONEMPTY_STRING (optarg);
747 safe_extended_out_name = optarg;
748
749 } else if (ARGUMENT_IS ("safe-in-name")) {
750 ENSURE_NONEMPTY_STRING (optarg);
751 safe_in_name = optarg;
752
753 } else if (ARGUMENT_IS ("safe-out-name")) {
754 ENSURE_NONEMPTY_STRING (optarg);
755 safe_out_name = optarg;
756
757 } else if (ARGUMENT_IS ("show-path")) {
758 path_to_show = optarg;
759 user_format_string = optarg;
760
761 } else if (ARGUMENT_IS ("subdir")) {
762 str_list_add (&subdir_paths, optarg);
763
764 } else if (ARGUMENT_IS ("var-brace-value")) {
765 ENSURE_NONEMPTY_STRING (optarg);
766 var_to_brace_value = optarg;
767
768 } else if (ARGUMENT_IS ("var-value")) {
769 ENSURE_NONEMPTY_STRING (optarg);
770 var_to_value = optarg;
771
772 } else if (ARGUMENT_IS ("version")) {
773 puts (kpathsea_version_string);
774 puts ("Copyright 2026 Karl Berry & Olaf Weber.\n\
775 License LGPLv2.1+: GNU Lesser GPL version 2.1 or later <https://gnu.org/licenses/lgpl.html>\n\
776 This is free software: you are free to change and redistribute it.\n\
777 There is NO WARRANTY, to the extent permitted by law.\n");
778 exit (0);
779 }
780
781 /* Else it was just a flag; getopt has already done the assignment. */
782 }
783
784 if (user_path && user_format_string) {
785 fprintf (stderr,
786 "kpsewhich: -path (%s) and -format (%s) are mutually exclusive.\n",
787 user_path, user_format_string);
788 fputs ("Try `kpsewhich --help' for more information.\n", stderr);
789 exit (EXIT_FAILURE);
790 }
791
792 if (optind == argc
793 && !var_to_expand && !braces_to_expand && !path_to_expand
794 && !path_to_show && !var_to_value && !var_to_brace_value
795 && !safe_extended_in_name && !safe_extended_out_name
796 && !safe_in_name && !safe_out_name) {
797 fputs ("Missing argument. Try `kpsewhich --help' for more information.\n",
798 stderr);
799 exit (EXIT_FAILURE);
800 }
801 }
802
803
804
805 /* Initializations that may depend on the options. */
806
807 static void
808 init_more (kpathsea kpse)
809 {
810 if (engine)
811 kpathsea_xputenv (kpse, "engine", engine);
812
813 /* We want config lines from the command line to override config files. */
814 if (user_cnf_lines) {
815 unsigned i;
816 for (i = 0; i < user_cnf_nlines; i++) {
817 kpathsea_cnf_line_env_progname (kpse, user_cnf_lines[i]);
818 free (user_cnf_lines[i]);
819 }
820 }
821
822 /* Disable all mktex programs unless they were explicitly enabled on our
823 command line. */
824 #define DISABLE_MKTEX(fmt) \
825 kpathsea_set_program_enabled (kpse, fmt, false, kpse_src_cmdline - 1)
826 DISABLE_MKTEX (kpse_pk_format);
827 DISABLE_MKTEX (kpse_mf_format);
828 DISABLE_MKTEX (kpse_tex_format);
829 DISABLE_MKTEX (kpse_tfm_format);
830 DISABLE_MKTEX (kpse_fmt_format);
831 DISABLE_MKTEX (kpse_ofm_format);
832 DISABLE_MKTEX (kpse_ocp_format);
833
834 /* NULL for no fallback font. */
835 kpathsea_init_prog (kpse, uppercasify (kpse->program_name), dpi, mode, NULL);
836
837 /* Have to do this after setting the program name. */
838 if (user_format_string) {
839 user_format = find_format (kpse, user_format_string, false);
840 if (user_format == kpse_last_format) {
841 WARNING1 ("kpsewhich: Ignoring unknown file type `%s'",
842 user_format_string);
843 }
844 }
845 }
846
847
848
849 int
850 main (int argc, string *argv)
851 {
852 #ifdef WIN32
853 #define puts(s) kpathsea_win32_puts(kpse, (s))
854 string *av, enc;
855 int ac;
856 #endif /* WIN32 */
857 unsigned unfound = 0;
858 kpathsea kpse = kpathsea_new ();
859
860 /* Read options, then dependent initializations. */
861 read_command_line (kpse, argc, argv);
862
863 kpathsea_set_program_name (kpse, argv[0], progname);
864 #ifdef WIN32
865 if(strstr(kpse->program_name,"xetex") || strstr(kpse->program_name,"xelatex")
866 || strstr(kpse->program_name,"uptex") || strstr(kpse->program_name,"uplatex")
867 || strstr(kpse->program_name,"pdftex") || strstr(kpse->program_name,"pdflatex")
868 || strstr(kpse->program_name,"dvipdfm") || strstr(kpse->program_name,"extractbb")
869 || strstr(kpse->program_name,"xbb") || strstr(kpse->program_name,"ebb")
870 || strstr(kpse->program_name,"dvips") || strstr(kpse->program_name,"upmendex")
871 || strstr(kpse->program_name,"bibtex8") || strstr(kpse->program_name,"bibtexu"))
872 {
873 if (strstr(kpse->program_name,"upmendex"))
874 enc = "utf-8";
875 else
876 enc = kpathsea_var_value (kpse, "command_line_encoding");
877 if (kpathsea_get_command_line_args_utf8(kpse, enc, &ac, &av)) {
878 optind = 0;
879 read_command_line (kpse, ac, av);
880 argv = av;
881 argc = ac;
882 }
883 }
884 #endif /* WIN32 */
885 init_more (kpse);
886
887
888 /* Perform actions. */
889
890 /* Variable expansion. */
891 if (var_to_expand)
892 puts (kpathsea_var_expand (kpse, var_to_expand));
893
894 /* Brace expansion. */
895 if (braces_to_expand)
896 puts (kpathsea_brace_expand (kpse, braces_to_expand));
897
898 /* Path expansion. */
899 if (path_to_expand)
900 puts (kpathsea_path_expand (kpse, path_to_expand));
901
902 /* Show a search path. */
903 if (path_to_show) {
904 if (user_format != kpse_last_format) {
905 if (!kpse->format_info[user_format].type) {
906 /* needed if arg was numeric */
907 kpathsea_init_format (kpse, user_format);
908 }
909 puts (kpse->format_info[user_format].path);
910 } else {
911 WARNING1 ("kpsewhich: Unknown file type, cannot show path: %s",
912 path_to_show);
913 }
914 }
915
916 /* Var to value. */
917 if (var_to_value) {
918 const_string value = kpathsea_var_value (kpse, var_to_value);
919 if (!value) {
920 unfound++;
921 value = "";
922 }
923 puts (value);
924 }
925
926 /* Var to brace-expanded value. This is separate from --var-value for
927 compatibility; people use --var-value for non-path values, where
928 changing commas to colons, which brace expansion does, is not right. */
929 if (var_to_brace_value) {
930 const_string value = kpathsea_var_value (kpse, var_to_brace_value);
931 if (!value) {
932 unfound++;
933 value = "";
934 } else {
935 /* Sometimes users want the fully-expanded (as a string, no
936 filesystem checks) value. We can't call brace_expand as part of
937 kpathsea_var_value, though, because unfortunately it is not
938 reentrant. We use var_value in lots of places in the source,
939 and it clobbers the static buffer in the kpse structure. */
940 value = kpathsea_brace_expand (kpse, value);
941 }
942 puts (value);
943 }
944
945 if (safe_in_name) {
946 if (!kpathsea_in_name_ok_silent (kpse, safe_in_name))
947 unfound++;
948 }
949
950 if (safe_out_name) {
951 if (!kpathsea_out_name_ok_silent (kpse, safe_out_name))
952 unfound++;
953 }
954
955 if (safe_extended_in_name) {
956 if (!kpathsea_in_name_ok_silent_extended (kpse, safe_extended_in_name))
957 unfound++;
958 }
959
960 if (safe_extended_out_name) {
961 if (!kpathsea_out_name_ok_silent_extended (kpse, safe_extended_out_name))
962 unfound++;
963 }
964
965 /* --subdir must imply --all, since we filter here after doing the
966 search, rather than inside the search itself. */
967 if (!STR_LIST_EMPTY (subdir_paths)) {
968 show_all = 1;
969 }
970
971 /* Usual case: look up each given filename. */
972 for (; optind < argc; optind++) {
973 unsigned status = lookup (kpse, argv[optind]);
974 unfound += status;
975 if (status == 1 && optind + 1 < argc) {
976 /* If not found, and more than one input file is specified,
977 output blank line so the return strings can be matched
978 up with the input strings. */
979 puts ("");
980 }
981 }
982
983 if (interactive) {
984 for (;;) {
985 string name = read_line (stdin);
986 if (!name || STREQ (name, "q") || STREQ (name, "quit"))
987 break;
988 unfound += lookup (kpse, name);
989 free (name);
990 }
991 }
992
993 kpathsea_finish (kpse);
994 return unfound > 255 ? 1 : unfound;
995 }