mtx-kpse.lua /size: 4628 b    last modification: 2025-02-21 11:03
1if not modules then modules = { } end modules ['mtx-kpse'] = {
2    version   = 1.001,
3    comment   = "companion to mtxrun.lua",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9-- I decided to make this module after a report on the mailing list about
10-- a clash with texmf-var on a system that had texlive installed. One way
11-- to figure that out is to use kpse. We had the code anyway so next time
12-- there is some issue ...
13
14trackers.enable("resolvers.lib.silent")
15
16local kpse = LUATEXENGINE == "luametatex" and require("libs-imp-kpse.lmt")
17
18if type(kpse) ~= "table" then
19    return
20end
21
22local helpinfo = [[
23<?xml version="1.0"?>
24<application>
25 <metadata>
26  <entry name="name">mtx-kpse</entry>
27  <entry name="detail">ConTeXt KPSE checking utility</entry>
28  <entry name="version">1.00</entry>
29 </metadata>
30 <flags>
31  <category name="basic">
32   <subcategory>
33    <flag name="progname"><short>mandate, set the program name (e.g. pdftex)</short></flag>
34    <flag name="findfile"><short>report the fully qualified path of the given file</short></flag>
35    <flag name="findfiles"><short>report a list of all full names of the given file</short></flag>
36    <flag name="expandpath"><short>expand the given path variable</short></flag>
37    <flag name="expandvar"><short>expand a variable</short></flag>
38    <flag name="expandbraces"><short>expand a complex variable specification</short></flag>
39    <flag name="varvalue"><short>show the value of a variable</short></flag>
40    <flag name="readablefile"><short>report if a file is readable</short></flag>
41    <flag name="filetypes"><short>list all supported formats</short></flag>
42   </subcategory>
43  </category>
44  <category name="additional">
45   <subcategory>
46    <flag name="format"><short>format type</short></flag>
47    <flag name="path"><short>path variable</short></flag>
48    <flag name="split"><short>split result in lines</short></flag>
49   </subcategory>
50  </category>
51 </flags>
52 <examples>
53  <category>
54   <title>Examples</title>
55   <subcategory>
56    <example><command>mtxrun --script kpse --progname=pdftex --findfile  context.mkii</command></example>
57    <example><command>mtxrun --script kpse --progname=pdftex --findfile  context.mkii --format=tex</command></example>
58    <example><command>mtxrun --script kpse --progname=pdftex --findfiles context.mkii --path=$TEXINPUTS</command></example>
59   </subcategory>
60   <subcategory>
61    <example><command>mtxrun --script kpse --progname=pdftex --expandpath $TEXMFVAR</command></example>
62    <example><command>mtxrun --script kpse --progname=pdftex --expandpath $TEXINPUTS -- split</command></example>
63   </subcategory>
64  </category>
65 </examples>
66</application>
67]]
68
69local application = logs.application {
70    name     = "mtx-kpse",
71    banner   = "ConTeXt KPSE checking utility",
72    helpinfo = helpinfo,
73}
74
75local report   = application.report
76local argument = environment.argument
77local files    = environment.files
78local target   = files[1]
79
80local function printtable(result)
81    if type(result) == "table" then
82        for i=1,#result do
83            print(result[i])
84        end
85    end
86end
87
88if argument("exporthelp") then
89    application.export(environment.argument("exporthelp"),target)
90    return
91elseif argument("progname") or argument("programname") then
92    kpse.set_program_name(argument("progname"))
93else
94    application.help()
95    return
96end
97
98if argument("filetypes") or argument("formats") then
99    print(table.concat(kpse.get_file_types()," "))
100elseif type(target) == "string" and target ~= "" then
101    if argument("findfiles") or argument("find-files") then
102        printtable(kpse.find_files(argument("path"),target))
103    elseif argument("findfile") or argument("find-file") then
104        print(kpse.find_file(target,argument("format")))
105    elseif argument("expandpath") or argument("expand-path") then
106        local result = kpse.expand_path(target)
107        if result and argument("split") then
108            printtable(string.split(result,";"))
109        else
110            print(result)
111        end
112    elseif argument("expandvar") or argument("expand-var") then
113        print(kpse.expand_var(target))
114    elseif argument("expandbraces") or argument("expand-braces") then
115        print(kpse.expand_braces(target))
116    elseif argument("varvalue") or argument("var-value") then
117        print(kpse.var_value(target))
118    elseif argument("readablefile") or argument("readable-file") then
119        print(kpse.readable_file(target))
120    else
121        application.help()
122    end
123else
124    application.help()
125end
126