Debian Codegraph @

texk/web2c/luatexdir/lua/lkpselib.c

raw
1 /* lkpselib.c
2
3 Copyright 2006-2008 Taco Hoekwater <[email protected]>
4
5 This file is part of LuaTeX.
6
7 LuaTeX is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2 of the License, or (at your
10 option) any later version.
11
12 LuaTeX is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 License for more details.
16
17 You should have received a copy of the GNU General Public License along
18 with LuaTeX; if not, see <http://www.gnu.org/licenses/>. */
19
20 #ifdef MF_LUA
21 #define EXTERN extern
22 #if defined(JIT)
23 #include "mfluajitd.h"
24 #else
25 #include "mfluad.h"
26 #endif
27 #include <kpathsea/version.h>
28 #define xfree(p) do { if (p != NULL) free(p); p = NULL; } while (0)
29 #else
30 #include "ptexlib.h"
31 #include "lua/luatex-api.h"
32 #endif
33 #include <kpathsea/expand.h>
34 #include <kpathsea/variable.h>
35 #include <kpathsea/tex-glyph.h>
36 #include <kpathsea/readable.h>
37 #include <kpathsea/pathsearch.h>
38 #include <kpathsea/str-list.h>
39 #include <kpathsea/tex-file.h>
40 #include <kpathsea/paths.h>
41 #include <kpathsea/absolute.h>
42
43
44 static const unsigned filetypes[] = {
45 kpse_gf_format,
46 kpse_pk_format,
47 kpse_any_glyph_format,
48 kpse_tfm_format,
49 kpse_afm_format,
50 kpse_base_format,
51 kpse_bib_format,
52 kpse_bst_format,
53 kpse_cnf_format,
54 kpse_db_format,
55 kpse_fmt_format,
56 kpse_fontmap_format,
57 kpse_mem_format,
58 kpse_mf_format,
59 kpse_mfpool_format,
60 kpse_mft_format,
61 kpse_mp_format,
62 kpse_mppool_format,
63 kpse_mpsupport_format,
64 kpse_ocp_format,
65 kpse_ofm_format,
66 kpse_opl_format,
67 kpse_otp_format,
68 kpse_ovf_format,
69 kpse_ovp_format,
70 kpse_pict_format,
71 kpse_tex_format,
72 kpse_texdoc_format,
73 kpse_texpool_format,
74 kpse_texsource_format,
75 kpse_tex_ps_header_format,
76 kpse_troff_font_format,
77 kpse_type1_format,
78 kpse_vf_format,
79 kpse_dvips_config_format,
80 kpse_ist_format,
81 kpse_truetype_format,
82 kpse_type42_format,
83 kpse_web2c_format,
84 kpse_program_text_format,
85 kpse_program_binary_format,
86 kpse_miscfonts_format,
87 kpse_web_format,
88 kpse_cweb_format,
89 kpse_enc_format,
90 kpse_cmap_format,
91 kpse_sfd_format,
92 kpse_opentype_format,
93 kpse_pdftex_config_format,
94 kpse_lig_format,
95 kpse_texmfscripts_format,
96 kpse_lua_format,
97 kpse_fea_format,
98 kpse_cid_format,
99 kpse_mlbib_format,
100 kpse_mlbst_format,
101 kpse_clua_format
102 };
103
104 static const char *const filetypenames[] = {
105 "gf",
106 "pk",
107 "bitmap font",
108 "tfm",
109 "afm",
110 "base",
111 "bib",
112 "bst",
113 "cnf",
114 "ls-R",
115 "fmt",
116 "map",
117 "mem",
118 "mf",
119 "mfpool",
120 "mft",
121 "mp",
122 "mppool",
123 "MetaPost support",
124 "ocp",
125 "ofm",
126 "opl",
127 "otp",
128 "ovf",
129 "ovp",
130 "graphic/figure",
131 "tex",
132 "TeX system documentation",
133 "texpool",
134 "TeX system sources",
135 "PostScript header",
136 "Troff fonts",
137 "type1 fonts",
138 "vf",
139 "dvips config",
140 "ist",
141 "truetype fonts",
142 "type42 fonts",
143 "web2c files",
144 "other text files",
145 "other binary files",
146 "misc fonts",
147 "web",
148 "cweb",
149 "enc files",
150 "cmap files",
151 "subfont definition files",
152 "opentype fonts",
153 "pdftex config",
154 "lig files",
155 "texmfscripts",
156 "lua",
157 "font feature files",
158 "cid maps",
159 "mlbib",
160 "mlbst",
161 "clua",
162 NULL
163 };
164
165
166 #ifdef MF
167 #define KPATHSEA_METATABLE "mflua.kpathsea"
168 #else
169 #define KPATHSEA_METATABLE "luatex.kpathsea"
170 #endif
171
172 /* set to 1 by the |program_name| function */
173
174 #ifdef MF
175 int program_name_set = 1;
176 #else
177 int program_name_set = 0;
178 #endif
179
180 #define TEST_PROGRAM_NAME_SET do { \
181 if (! program_name_set) { \
182 return luaL_error(L, "Please call kpse.set_program_name() before using the library"); \
183 } \
184 } while (0)
185
186 static int find_file(lua_State * L)
187 {
188 int i, t;
189 const char *st;
190 unsigned ftype = kpse_tex_format;
191 int mexist = 0;
192 TEST_PROGRAM_NAME_SET;
193 if (lua_type(L, 1) != LUA_TSTRING) {
194 luaL_error(L, "not a file name");
195 }
196 st = lua_tostring(L, 1);
197 i = lua_gettop(L);
198 while (i > 1) {
199 t = lua_type(L, i) ;
200 if (t == LUA_TBOOLEAN) {
201 mexist = lua_toboolean(L, i);
202 } else if (t == LUA_TNUMBER) {
203 mexist = (int) lua_tointeger(L, i);
204 } else if (t == LUA_TSTRING) {
205 int op = luaL_checkoption(L, i, NULL, filetypenames);
206 ftype = filetypes[op];
207 } else {
208 /* ignore */
209 }
210 i--;
211 }
212 if (ftype == kpse_pk_format ||
213 ftype == kpse_gf_format || ftype == kpse_any_glyph_format) {
214 /* ret.format, ret.name, ret.dpi */
215 kpse_glyph_file_type ret;
216 if (output_directory && !kpse_absolute_p(st, false)) {
217 char *res ;
218 char *ftemp = concat3(output_directory, DIR_SEP_STRING, st);
219 res = kpse_find_glyph(ftemp, 0, ftype, &ret);
220 if (res && strlen(res)>0) {
221 lua_pushstring(L, res);
222 } else {
223 lua_pushstring(L, kpse_find_glyph(st, (unsigned) mexist, ftype, &ret));
224 }
225 xfree(res);
226 xfree(ftemp);
227 } else {
228 lua_pushstring(L, kpse_find_glyph(st, (unsigned) mexist, ftype, &ret));
229 }
230 } else {
231 if (mexist > 0)
232 mexist = 1;
233 if (mexist < 0)
234 mexist = 0;
235 if (output_directory && !kpse_absolute_p(st, false)) {
236 char *res ;
237 char *ftemp = concat3(output_directory, DIR_SEP_STRING, st);
238 res = kpse_find_file(ftemp, ftype, 0);
239 if (res && strlen(res)>0) {
240 lua_pushstring(L, res);
241 } else {
242 lua_pushstring(L, kpse_find_file(st, ftype, mexist));
243 }
244 xfree(res);
245 xfree(ftemp);
246 } else {
247 lua_pushstring(L, kpse_find_file(st, ftype, mexist));
248 }
249 }
250 return 1;
251 }
252
253
254 static int lua_kpathsea_find_file(lua_State * L)
255 {
256 int i, t;
257 unsigned ftype = kpse_tex_format;
258 int mexist = 0;
259 kpathsea *kp = (kpathsea *) luaL_checkudata(L, 1, KPATHSEA_METATABLE);
260 const char *st = luaL_checkstring(L, 2);
261 i = lua_gettop(L);
262 while (i > 2) {
263 t = lua_type(L, i) ;
264 if (t == LUA_TBOOLEAN) {
265 mexist = (boolean) lua_toboolean(L, i);
266 } else if (t == LUA_TNUMBER) {
267 mexist = (int) lua_tointeger(L, i);
268 } else if (t == LUA_TSTRING) {
269 int op = luaL_checkoption(L, i, NULL, filetypenames);
270 ftype = filetypes[op];
271 } else {
272 /* ignore */
273 }
274 i--;
275 }
276 if (ftype == kpse_pk_format || ftype == kpse_gf_format || ftype == kpse_any_glyph_format) {
277 /* ret.format, ret.name, ret.dpi */
278 kpse_glyph_file_type ret;
279 if (output_directory && !kpse_absolute_p(st, false)) {
280 char *res ;
281 char *ftemp = concat3(output_directory, DIR_SEP_STRING, st);
282 res = kpathsea_find_glyph(*kp, ftemp, (unsigned) mexist, ftype, &ret) ;
283 if (res && strlen(res)>0) {
284 lua_pushstring(L, res);
285 } else {
286 lua_pushstring(L, kpathsea_find_glyph(*kp, st, (unsigned) mexist, ftype, &ret));
287 }
288 xfree(res);
289 xfree(ftemp);
290 } else {
291 lua_pushstring(L, kpathsea_find_glyph(*kp, st, (unsigned) mexist, ftype, &ret));
292 }
293 } else {
294 if (mexist > 0)
295 mexist = 1;
296 if (mexist < 0)
297 mexist = 0;
298 if (output_directory && !kpse_absolute_p(st, false)) {
299 char *res ;
300 char *ftemp = concat3(output_directory, DIR_SEP_STRING, st);
301 res = kpathsea_find_file(*kp, ftemp, ftype, mexist);
302 if (res && strlen(res)>0) {
303 lua_pushstring(L, res);
304 } else {
305 lua_pushstring(L, kpathsea_find_file(*kp, st, ftype, mexist));
306 }
307 xfree(res);
308 xfree(ftemp);
309 } else {
310 lua_pushstring(L, kpathsea_find_file(*kp, st, ftype, mexist));
311 }
312 }
313 return 1;
314
315 }
316
317 static int show_texmfcnf(lua_State * L)
318 {
319 lua_pushstring(L, DEFAULT_TEXMFCNF);
320 return 1;
321 }
322
323 static int show_path(lua_State * L)
324 {
325 int op = luaL_checkoption(L, -1, "tex", filetypenames);
326 unsigned user_format = filetypes[op];
327 TEST_PROGRAM_NAME_SET;
328 if (!kpse_format_info[user_format].type) /* needed if arg was numeric */
329 kpse_init_format(user_format);
330 lua_pushstring(L, kpse_format_info[user_format].path);
331 return 1;
332 }
333
334 static int lua_kpathsea_show_path(lua_State * L)
335 {
336 kpathsea *kp = (kpathsea *) luaL_checkudata(L, 1, KPATHSEA_METATABLE);
337 int op = luaL_checkoption(L, -1, "tex", filetypenames);
338 unsigned user_format = filetypes[op];
339 if (!(*kp)->format_info[user_format].type) /* needed if arg was numeric */
340 kpathsea_init_format(*kp, user_format);
341 lua_pushstring(L, (*kp)->format_info[user_format].path);
342 return 1;
343 }
344
345 static int in_name_ok(lua_State * L)
346 {
347 const char *st = luaL_checkstring(L, 1);
348 TEST_PROGRAM_NAME_SET;
349 lua_pushboolean(L, kpse_in_name_ok(st));
350 return 1;
351 }
352
353 static int in_name_ok_silent_extended(lua_State * L)
354 {
355 const char *st = luaL_checkstring(L, 1);
356 TEST_PROGRAM_NAME_SET;
357 lua_pushboolean(L, kpse_in_name_ok_silent_extended(st));
358 return 1;
359 }
360
361
362 static int lua_kpathsea_in_name_ok(lua_State * L)
363 {
364 kpathsea *kp = (kpathsea *) luaL_checkudata(L, 1, KPATHSEA_METATABLE);
365 const char *st = luaL_checkstring(L, 2);
366 lua_pushboolean(L, kpathsea_in_name_ok(*kp,st));
367 return 1;
368 }
369
370 static int lua_kpathsea_in_name_ok_silent_extended(lua_State * L)
371 {
372 kpathsea *kp = (kpathsea *) luaL_checkudata(L, 1, KPATHSEA_METATABLE);
373 const char *st = luaL_checkstring(L, 2);
374 lua_pushboolean(L, kpathsea_in_name_ok_silent_extended(*kp,st));
375 return 1;
376 }
377
378
379
380
381 static int out_name_ok(lua_State * L)
382 {
383 const char *st = luaL_checkstring(L, 1);
384 TEST_PROGRAM_NAME_SET;
385 lua_pushboolean(L, kpse_out_name_ok(st));
386 return 1;
387 }
388
389 static int out_name_ok_silent_extended(lua_State * L)
390 {
391 const char *st = luaL_checkstring(L, 1);
392 TEST_PROGRAM_NAME_SET;
393 lua_pushboolean(L, kpse_out_name_ok_silent_extended(st));
394 return 1;
395 }
396
397 static int lua_kpathsea_out_name_ok(lua_State * L)
398 {
399 kpathsea *kp = (kpathsea *) luaL_checkudata(L, 1, KPATHSEA_METATABLE);
400 const char *st = luaL_checkstring(L, 2);
401 lua_pushboolean(L, kpathsea_out_name_ok(*kp,st));
402 return 1;
403 }
404
405 static int lua_kpathsea_out_name_ok_silent_extended(lua_State * L)
406 {
407 kpathsea *kp = (kpathsea *) luaL_checkudata(L, 1, KPATHSEA_METATABLE);
408 const char *st = luaL_checkstring(L, 2);
409 lua_pushboolean(L, kpathsea_out_name_ok_silent_extended(*kp,st));
410 return 1;
411 }
412
413
414
415
416 static int expand_path(lua_State * L)
417 {
418 const char *st = luaL_checkstring(L, 1);
419 TEST_PROGRAM_NAME_SET;
420 lua_pushstring(L, kpse_path_expand(st));
421 return 1;
422 }
423
424 static int lua_kpathsea_expand_path(lua_State * L)
425 {
426 kpathsea *kp = (kpathsea *) luaL_checkudata(L, 1, KPATHSEA_METATABLE);
427 const char *st = luaL_checkstring(L, 2);
428 lua_pushstring(L, kpathsea_path_expand(*kp, st));
429 return 1;
430 }
431
432 static int expand_braces(lua_State * L)
433 {
434 const char *st = luaL_checkstring(L, 1);
435 TEST_PROGRAM_NAME_SET;
436 lua_pushstring(L, kpse_brace_expand(st));
437 return 1;
438 }
439
440 static int lua_kpathsea_expand_braces(lua_State * L)
441 {
442 kpathsea *kp = (kpathsea *) luaL_checkudata(L, 1, KPATHSEA_METATABLE);
443 const char *st = luaL_checkstring(L, 2);
444 lua_pushstring(L, kpathsea_brace_expand(*kp, st));
445 return 1;
446 }
447
448
449 static int expand_var(lua_State * L)
450 {
451 const char *st = luaL_checkstring(L, 1);
452 TEST_PROGRAM_NAME_SET;
453 lua_pushstring(L, kpse_var_expand(st));
454 return 1;
455 }
456
457 static int lua_kpathsea_expand_var(lua_State * L)
458 {
459 kpathsea *kp = (kpathsea *) luaL_checkudata(L, 1, KPATHSEA_METATABLE);
460 const char *st = luaL_checkstring(L, 2);
461 lua_pushstring(L, kpathsea_var_expand(*kp, st));
462 return 1;
463 }
464
465
466 static int var_value(lua_State * L)
467 {
468 const char *st = luaL_checkstring(L, 1);
469 TEST_PROGRAM_NAME_SET;
470 lua_pushstring(L, kpse_var_value(st));
471 return 1;
472 }
473
474 static int lua_kpathsea_var_value(lua_State * L)
475 {
476 kpathsea *kp = (kpathsea *) luaL_checkudata(L, 1, KPATHSEA_METATABLE);
477 const char *st = luaL_checkstring(L, 2);
478 lua_pushstring(L, kpathsea_var_value(*kp, st));
479 return 1;
480 }
481
482 static unsigned find_dpi(const_string s)
483 {
484 unsigned dpi_number = 0;
485 const_string extension = find_suffix(s);
486
487 if (extension != NULL)
488 sscanf(extension, "%u", &dpi_number);
489
490 return dpi_number;
491 }
492
493 /*
494 Return newly-allocated NULL-terminated list of strings from MATCHES
495 that are prefixed with any of the subdirectories in SUBDIRS. That
496 is, for a string S in MATCHES, its dirname must end with one of the
497 elements in SUBDIRS. For instance, if subdir=foo/bar, that will
498 match a string foo/bar/baz or /some/texmf/foo/bar/baz.
499
500 We don't reallocate the actual strings, just the list elements.
501 Perhaps later we will implement wildcards or "//" or something.
502 */
503
504 static string *subdir_match(str_list_type subdirs, string * matches)
505 {
506 string *ret = XTALLOC1(string);
507 unsigned len = 1;
508 unsigned m;
509
510 for (m = 0; matches[m]; m++) {
511 size_t loc;
512 unsigned e;
513 string s = xstrdup(matches[m]);
514 for (loc = strlen(s); loc > 0 && !IS_DIR_SEP(s[loc - 1]); loc--);
515 while (loc > 0 && IS_DIR_SEP(s[loc - 1])) {
516 loc--;
517 }
518 s[loc] = 0; /* wipe out basename */
519
520 for (e = 0; e < STR_LIST_LENGTH(subdirs); e++) {
521 string subdir = STR_LIST_ELT(subdirs, e);
522 size_t subdir_len = strlen(subdir);
523 while (subdir_len > 0 && IS_DIR_SEP(subdir[subdir_len - 1])) {
524 subdir_len--;
525 subdir[subdir_len] = 0; /* remove trailing slashes from subdir spec */
526 }
527 if (FILESTRCASEEQ(subdir, s + loc - subdir_len)) {
528 /* matched, save this one. */
529 XRETALLOC(ret, len + 1, string);
530 ret[len - 1] = matches[m];
531 len++;
532 }
533 }
534 free(s);
535 }
536 ret[len - 1] = NULL;
537 return ret;
538 }
539
540 /* Use the file type from -format if that was specified (i.e., the
541 user_format global variable), else guess dynamically from NAME.
542 Return kpse_last_format if undeterminable. This function is also
543 used to parse the -format string, a case which we distinguish by
544 setting is_filename to false.
545
546 A few filenames have been hard-coded for format types that
547 differ from what would be inferred from their extensions. */
548
549 static kpse_file_format_type
550 find_format(kpathsea kpse, const_string name, boolean is_filename)
551 {
552 kpse_file_format_type ret;
553
554 if (FILESTRCASEEQ(name, "config.ps")) {
555 ret = kpse_dvips_config_format;
556 } else if (FILESTRCASEEQ(name, "dvipdfmx.cfg")) {
557 ret = kpse_program_text_format;
558 } else if (FILESTRCASEEQ(name, "fmtutil.cnf")) {
559 ret = kpse_web2c_format;
560 } else if (FILESTRCASEEQ(name, "glyphlist.txt")) {
561 ret = kpse_fontmap_format;
562 } else if (FILESTRCASEEQ(name, "mktex.cnf")) {
563 ret = kpse_web2c_format;
564 } else if (FILESTRCASEEQ(name, "pdfglyphlist.txt")) {
565 ret = kpse_fontmap_format;
566 } else if (FILESTRCASEEQ(name, "pdftex.cfg")) {
567 ret = kpse_pdftex_config_format;
568 } else if (FILESTRCASEEQ(name, "texmf.cnf")) {
569 ret = kpse_cnf_format;
570 } else if (FILESTRCASEEQ(name, "updmap.cfg")) {
571 ret = kpse_web2c_format;
572 } else if (FILESTRCASEEQ(name, "XDvi")) {
573 ret = kpse_program_text_format;
574 } else {
575 int f = 0; /* kpse_file_format_type */
576 size_t name_len = strlen(name);
577
578 /* Have to rely on `try_len' being declared here, since we can't assume
579 GNU C and statement expressions. */
580 #define TRY_SUFFIX(ftry) (\
581 try_len = (ftry) ? strlen (ftry) : 0, \
582 (ftry) && try_len <= name_len \
583 && FILESTRCASEEQ (ftry, name + name_len - try_len))
584
585 while (f != kpse_last_format) {
586 size_t try_len;
587 const_string *ext;
588 const_string ftry;
589 boolean found = false;
590
591 if (!kpse->format_info[f].type)
592 kpathsea_init_format(kpse, (kpse_file_format_type) f);
593
594 if (!is_filename) {
595 /* Allow the long name, but only in the -format option. We don't
596 want a filename confused with a format name. */
597 ftry = kpse->format_info[f].type;
598 found = TRY_SUFFIX(ftry);
599 }
600 for (ext = kpse->format_info[f].suffix; !found && ext && *ext;
601 ext++) {
602 found = TRY_SUFFIX(*ext);
603 }
604 for (ext = kpse->format_info[f].alt_suffix; !found && ext && *ext;
605 ext++) {
606 found = TRY_SUFFIX(*ext);
607 }
608
609 if (found)
610 break;
611
612 /* Some trickery here: the extensions for kpse_fmt_format can
613 * clash with other extensions in use, and we prefer for those
614 * others to be preferred. And we don't want to change the
615 * integer value of kpse_fmt_format. So skip it when first
616 * enountered, then use it when we've done everything else,
617 * and use it as the end-guard.
618 */
619 if (f == kpse_fmt_format) {
620 f = kpse_last_format;
621 } else if (++f == kpse_fmt_format) {
622 f++;
623 } else if (f == kpse_last_format) {
624 f = kpse_fmt_format;
625 }
626 }
627
628 /* If there was a match, f will be one past the correct value. */
629 ret = f;
630 }
631
632 return ret;
633 }
634
635 /* kpse:lookup("plain.tex", {}) */
636 static int do_lua_kpathsea_lookup(lua_State * L, kpathsea kpse, int idx)
637 {
638 int i;
639 string ret = NULL;
640 string *ret_list = NULL;
641 const_string name = NULL;
642 string user_path = NULL;
643 boolean show_all = false;
644 boolean must_exist = false;
645 kpse_file_format_type user_format = kpse_last_format;
646 int dpi = 600;
647 str_list_type subdir_paths = { 0, NULL };
648 unsigned saved_debug = kpse->debug;
649 int saved_mktexpk = kpse->format_info[kpse_pk_format].program_enabled_p;
650 int saved_mktexmf = kpse->format_info[kpse_mf_format].program_enabled_p;
651 int saved_mktextex = kpse->format_info[kpse_tex_format].program_enabled_p;
652 int saved_mktextfm = kpse->format_info[kpse_tfm_format].program_enabled_p;
653 name = luaL_checkstring(L, idx);
654 /* todo: fetch parameter values */
655
656 if (lua_type(L, idx + 1) == LUA_TTABLE) {
657 lua_pushstring(L, "format");
658 lua_gettable(L, idx + 1);
659 if (lua_type(L, -1) == LUA_TSTRING) {
660 int op = luaL_checkoption(L, -1, NULL, filetypenames);
661 user_format = filetypes[op];
662 }
663 lua_pop(L, 1);
664 lua_pushstring(L, "dpi");
665 lua_gettable(L, idx + 1);
666 if (lua_type(L, -1) == LUA_TNUMBER) {
667 dpi = (int) lua_tointeger(L, -1);
668 }
669 lua_pop(L, 1);
670 lua_pushstring(L, "debug");
671 lua_gettable(L, idx + 1);
672 if (lua_type(L, -1) == LUA_TNUMBER) {
673 int d = 0;
674 d = (int) lua_tointeger(L, -1);
675 kpse->debug |= d;
676 }
677 lua_pop(L, 1);
678 lua_pushstring(L, "path");
679 lua_gettable(L, idx + 1);
680 if (lua_type(L, -1) == LUA_TSTRING) {
681 user_path = xstrdup(lua_tostring(L, -1));
682 }
683 lua_pop(L, 1);
684 lua_pushstring(L, "all");
685 lua_gettable(L, idx + 1);
686 if (lua_type(L, -1) == LUA_TBOOLEAN) {
687 show_all = lua_toboolean(L, -1);
688 }
689 lua_pop(L, 1);
690
691 lua_pushstring(L, "mktexpk");
692 lua_gettable(L, idx + 1);
693 if (lua_type(L, -1) == LUA_TBOOLEAN) {
694 kpathsea_maketex_option(kpse, "pk", lua_toboolean(L, -1));
695 }
696 lua_pop(L, 1);
697
698 lua_pushstring(L, "mktextex");
699 lua_gettable(L, idx + 1);
700 if (lua_type(L, -1) == LUA_TBOOLEAN) {
701 kpathsea_maketex_option(kpse, "tex", lua_toboolean(L, -1));
702 }
703 lua_pop(L, 1);
704
705 lua_pushstring(L, "mktexmf");
706 lua_gettable(L, idx + 1);
707 if (lua_type(L, -1) == LUA_TBOOLEAN) {
708 kpathsea_maketex_option(kpse, "mf", lua_toboolean(L, -1));
709 }
710 lua_pop(L, 1);
711
712 lua_pushstring(L, "mktextfm");
713 lua_gettable(L, idx + 1);
714 if (lua_type(L, -1) == LUA_TBOOLEAN) {
715 kpathsea_maketex_option(kpse, "tfm", lua_toboolean(L, -1));
716 }
717 lua_pop(L, 1);
718
719
720 lua_pushstring(L, "mustexist");
721 lua_gettable(L, idx + 1);
722 if (lua_type(L, -1) == LUA_TBOOLEAN) {
723 must_exist = lua_toboolean(L, -1);
724 }
725 lua_pop(L, 1);
726 lua_pushstring(L, "subdir");
727 lua_gettable(L, idx + 1);
728 if (lua_istable(L, -1)) {
729 lua_pushnil(L);
730 while (lua_next(L, -2) != 0) { /* numeric value */
731 if (lua_type(L, -1) == LUA_TSTRING) {
732 char *s = xstrdup(lua_tostring(L, -1));
733 str_list_add(&subdir_paths, s);
734 xfree(s);
735 }
736 lua_pop(L, 1);
737 }
738 } else if (lua_type(L, -1) == LUA_TSTRING) {
739 char *s = xstrdup(lua_tostring(L, -1));
740 str_list_add(&subdir_paths, s);
741 xfree(s);
742 }
743 lua_pop(L, 1);
744 if (STR_LIST_LENGTH(subdir_paths) > 0) {
745 show_all = 1;
746 }
747 }
748 if (user_path) {
749 /* Translate ; to : if that's our ENV_SEP. See cnf.c. */
750 if (IS_ENV_SEP(':')) {
751 string loc;
752 for (loc = user_path; *loc; loc++) {
753 if (*loc == ';')
754 *loc = ':';
755 }
756 }
757 user_path = kpathsea_path_expand(kpse, user_path);
758 if (output_directory && !kpse_absolute_p(user_path , false)) {
759 string ftemp = concat3(output_directory, DIR_SEP_STRING, user_path);
760 if (show_all) {
761 string *ret_list1;
762 string *ret_list2;
763 unsigned l1 = 0;
764 unsigned l2 = 0;
765 ret_list1 = kpathsea_all_path_search(kpse, ftemp, name);
766 ret_list2 = kpathsea_all_path_search(kpse, user_path, name);
767 l1 = 0;
768 while (ret_list1[l1])
769 l1++;
770 l2 = 0;
771 while (ret_list2[l2])
772 l2++;
773 ret_list = xmalloc((l1+l2+1)*sizeof(string));
774 l1 = 0;
775 while (ret_list1[l1]) {
776 ret_list[l1] = ret_list1[l1];
777 l1++;
778 }
779 l2 = 0;
780 while (ret_list2[l2]) {
781 ret_list[l1] = ret_list2[l2];
782 l1++;
783 l2++;
784 }
785 ret_list[l1] = NULL;
786 xfree(ret_list1);
787 xfree(ret_list2);
788 } else {
789 string ret1;
790 string ret2;
791 ret1 = kpathsea_path_search(kpse, ftemp, name, must_exist);
792 ret2 = kpathsea_path_search(kpse, user_path, name, must_exist);
793 ret = concat3(ret1,NULL,ret2);
794 xfree(ret1);
795 xfree(ret2);
796 }
797 xfree(ftemp);
798 } else {
799 if (show_all) {
800 ret_list = kpathsea_all_path_search(kpse, user_path, name);
801 } else {
802 ret = kpathsea_path_search(kpse, user_path, name, must_exist);
803 }
804 }
805 free(user_path);
806 } else {
807 /* No user-specified search path, check user format or guess from NAME. */
808 kpse_file_format_type fmt;
809 if (user_format != kpse_last_format)
810 fmt = user_format;
811 else
812 fmt = find_format(kpse, name, true);
813
814 switch (fmt) {
815 case kpse_pk_format:
816 case kpse_gf_format:
817 case kpse_any_glyph_format:
818 {
819 kpse_glyph_file_type glyph_ret;
820 string temp = remove_suffix (name);
821 /* Try to extract the resolution from the name. */
822 unsigned local_dpi = find_dpi(name);
823 if (!local_dpi)
824 local_dpi = (unsigned) dpi;
825 ret =
826 kpathsea_find_glyph(kpse, temp, local_dpi,
827 fmt, &glyph_ret);
828 if (temp != name)
829 free (temp);
830 }
831 break;
832
833 case kpse_last_format:
834 /* If the suffix isn't recognized, assume it's a tex file. */
835 fmt = kpse_tex_format;
836 /* fall through */
837
838 default:
839 if (show_all) {
840 ret_list =
841 kpathsea_find_file_generic(kpse, name, fmt, must_exist,
842 true);
843 } else {
844 ret = kpathsea_find_file(kpse, name, fmt, must_exist);
845 }
846 }
847 }
848
849 /* Turn single return into a null-terminated list for uniform treatment. */
850 if (ret) {
851 ret_list = XTALLOC(2, string);
852 ret_list[0] = ret;
853 ret_list[1] = NULL;
854 }
855
856 /* Filter by subdirectories, if specified. */
857 if (STR_LIST_LENGTH(subdir_paths) > 0) {
858 string *new_list = subdir_match(subdir_paths, ret_list);
859 free(ret_list);
860 ret_list = new_list;
861 }
862 kpse->debug = saved_debug;
863 kpse->format_info[kpse_pk_format].program_enabled_p = saved_mktexpk;
864 kpse->format_info[kpse_mf_format].program_enabled_p = saved_mktexmf;
865 kpse->format_info[kpse_tex_format].program_enabled_p = saved_mktextex;
866 kpse->format_info[kpse_tfm_format].program_enabled_p = saved_mktextfm;
867
868 /* Print output. */
869 i = 0;
870 if (ret_list) {
871 for (; ret_list[i]; i++) {
872 lua_pushstring(L, ret_list[i]);
873 }
874 free(ret_list);
875 }
876 if (i == 0) {
877 i++;
878 lua_pushnil(L);
879 }
880 return i;
881 }
882
883
884 static int lua_kpathsea_lookup(lua_State * L)
885 {
886 kpathsea *kpsep = (kpathsea *) luaL_checkudata(L, 1, KPATHSEA_METATABLE);
887 kpathsea kpse = *kpsep;
888 return do_lua_kpathsea_lookup(L, kpse, 2);
889 }
890
891 static int lua_kpse_lookup(lua_State * L)
892 {
893 TEST_PROGRAM_NAME_SET;
894 return do_lua_kpathsea_lookup(L, kpse_def, 1);
895 }
896
897
898 /* Engine support is a bit of a problem, because we do not want
899 * to interfere with the normal format discovery of |luatex|.
900 * Current approach: run |os.setenv()| if you have to.
901 */
902
903 static int set_program_name(lua_State * L)
904 {
905 const char *exe_name = luaL_checkstring(L, 1);
906 const char *prog_name = luaL_optstring(L, 2, exe_name);
907 if (!program_name_set) {
908 kpse_set_program_name(exe_name, prog_name);
909 program_name_set = 1;
910 } else {
911 kpse_reset_program_name(prog_name);
912 }
913 /* fix up the texconfig entry */
914 lua_checkstack(L, 3);
915 lua_getglobal(L, "texconfig");
916 if (lua_istable(L, -1)) {
917 lua_pushstring(L, "kpse_init");
918 lua_pushboolean(L, 0);
919 lua_rawset(L, -3);
920 }
921 lua_pop(L, 1);
922 return 0;
923 }
924
925 static int init_prog(lua_State * L)
926 {
927 const char *prefix = luaL_checkstring(L, 1);
928 unsigned dpi = (unsigned) luaL_checkinteger(L, 2);
929 const char *mode = luaL_checkstring(L, 3);
930 const char *fallback = luaL_optstring(L, 4, NULL);
931 TEST_PROGRAM_NAME_SET;
932 kpse_init_prog(prefix, dpi, mode, fallback);
933 return 0;
934 }
935
936 static int lua_kpathsea_init_prog(lua_State * L)
937 {
938 kpathsea *kp = (kpathsea *) luaL_checkudata(L, 1, KPATHSEA_METATABLE);
939 const char *prefix = luaL_checkstring(L, 2);
940 unsigned dpi = (unsigned) luaL_checkinteger(L, 3);
941 const char *mode = luaL_checkstring(L, 4);
942 const char *fallback = luaL_optstring(L, 5, NULL);
943 kpathsea_init_prog(*kp, prefix, dpi, mode, fallback);
944 return 0;
945 }
946
947 static int lua_kpse_version(lua_State * L)
948 {
949 lua_pushstring(L, kpathsea_version_string);
950 return 1;
951 }
952
953 static int readable_file(lua_State * L)
954 {
955 char *name = xstrdup(luaL_checkstring(L, 1));
956 TEST_PROGRAM_NAME_SET;
957 lua_pushstring(L, kpse_readable_file(name));
958 free(name);
959 return 1;
960 }
961
962 static int lua_kpathsea_readable_file(lua_State * L)
963 {
964 kpathsea *kp = (kpathsea *) luaL_checkudata(L, 1, KPATHSEA_METATABLE);
965 char *name = xstrdup(luaL_checkstring(L, 2));
966 lua_pushstring(L, kpathsea_readable_file(*kp, name));
967 free(name);
968 return 1;
969 }
970
971
972 static int lua_kpathsea_finish(lua_State * L)
973 {
974 kpathsea *kp = (kpathsea *) luaL_checkudata(L, 1, KPATHSEA_METATABLE);
975 kpathsea_finish(*kp);
976 return 0;
977 }
978
979 static int lua_kpathsea_new(lua_State * L)
980 {
981 kpathsea kpse = NULL;
982 kpathsea *kp = NULL;
983 const char *av = luaL_checkstring(L, 1);
984 const char *liar = luaL_optstring(L, 2, av);
985 kpse = kpathsea_new();
986 kpathsea_set_program_name(kpse, av, liar);
987 kp = (kpathsea *) lua_newuserdata(L, sizeof(kpathsea *));
988 *kp = kpse;
989 luaL_getmetatable(L, KPATHSEA_METATABLE);
990 lua_setmetatable(L, -2);
991 return 1;
992 }
993
994 static int lua_record_input_file(lua_State * L)
995 {
996 const char *name = lua_tostring(L, 1);
997 if (name != NULL) {
998 recorder_record_input(name);
999 }
1000 return 0;
1001 }
1002
1003 static int lua_record_output_file(lua_State * L)
1004 {
1005 const char *name = lua_tostring(L, 1);
1006 if (name != NULL) {
1007 recorder_record_output(name);
1008 }
1009 return 0;
1010 }
1011
1012 /*tex moved here */
1013
1014 static int lua_check_permissions(lua_State *L)
1015 {
1016 const char *filename = luaL_checkstring(L, 1);
1017 if (filename == NULL) {
1018 lua_pushboolean(L,0);
1019 lua_pushliteral(L,"no command name given");
1020 } else if (shellenabledp <= 0) {
1021 lua_pushboolean(L,0);
1022 lua_pushliteral(L,"all command execution is disabled");
1023 } else if (restrictedshell == 0) {
1024 lua_pushboolean(L,1);
1025 lua_pushstring(L,filename);
1026 } else {
1027 char *safecmd = NULL;
1028 char *cmdname = NULL;
1029 switch (shell_cmd_is_allowed(filename, &safecmd, &cmdname)) {
1030 case 0:
1031 lua_pushboolean(L,0);
1032 lua_pushliteral(L, "specific command execution disabled");
1033 break;
1034 case 1:
1035 /* doesn't happen */
1036 lua_pushboolean(L,1);
1037 lua_pushstring(L,filename);
1038 break;
1039 case 2:
1040 lua_pushboolean(L,1);
1041 lua_pushstring(L,safecmd);
1042 break;
1043 default:
1044 /* -1 */
1045 lua_pushboolean(L,0);
1046 lua_pushliteral(L, "bad command line quoting");
1047 break;
1048 }
1049 }
1050 return 2;
1051 }
1052
1053 static const struct luaL_Reg kpselib_m[] = {
1054 {"__gc", lua_kpathsea_finish},
1055 {"init_prog", lua_kpathsea_init_prog},
1056 {"readable_file", lua_kpathsea_readable_file},
1057 {"find_file", lua_kpathsea_find_file},
1058 {"expand_path", lua_kpathsea_expand_path},
1059 {"expand_var", lua_kpathsea_expand_var},
1060 {"expand_braces", lua_kpathsea_expand_braces},
1061 {"in_name_ok", lua_kpathsea_in_name_ok},
1062 {"in_name_ok_silent_extended", lua_kpathsea_in_name_ok_silent_extended},
1063 {"out_name_ok", lua_kpathsea_out_name_ok},
1064 {"out_name_ok_silent_extended", lua_kpathsea_out_name_ok_silent_extended},
1065 {"var_value", lua_kpathsea_var_value},
1066 {"show_path", lua_kpathsea_show_path},
1067 {"lookup", lua_kpathsea_lookup},
1068 {"version", lua_kpse_version},
1069 {"default_texmfcnf", show_texmfcnf},
1070 {"record_input_file", lua_record_input_file},
1071 {"record_output_file", lua_record_output_file},
1072 /* sentinel */
1073 {NULL, NULL}
1074 };
1075
1076 static const struct luaL_Reg kpselib_l[] = {
1077 {"new", lua_kpathsea_new},
1078 {"set_program_name", set_program_name},
1079 {"init_prog", init_prog},
1080 {"readable_file", readable_file},
1081 {"find_file", find_file},
1082 {"expand_path", expand_path},
1083 {"expand_var", expand_var},
1084 {"expand_braces", expand_braces},
1085 {"in_name_ok",in_name_ok},
1086 {"in_name_ok_silent_extended",in_name_ok_silent_extended},
1087 {"out_name_ok",out_name_ok},
1088 {"out_name_ok_silent_extended",out_name_ok_silent_extended},
1089 {"var_value", var_value},
1090 {"show_path", show_path},
1091 {"lookup", lua_kpse_lookup},
1092 {"version", lua_kpse_version},
1093 {"default_texmfcnf", show_texmfcnf},
1094 {"record_input_file", lua_record_input_file},
1095 {"record_output_file", lua_record_output_file},
1096 /* extra */
1097 {"check_permission", lua_check_permissions},
1098 /* sentinel */
1099 {NULL, NULL}
1100 };
1101
1102 int luaopen_kpse(lua_State * L)
1103 {
1104 luaL_newmetatable(L, KPATHSEA_METATABLE);
1105 lua_pushvalue(L, -1);
1106 lua_setfield(L, -2, "__index");
1107 luaL_openlib(L, NULL, kpselib_m, 0);
1108 luaL_openlib(L, "kpse", kpselib_l, 0);
1109 return 1;
1110 }