mtx-kpse.lua /size: 4617 b    last modification: 2021-10-28 13:50
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
80if argument("progname") or argument("programname") then
81    kpse.set_program_name(argument("progname"))
82else
83    application.help()
84    return
85end
86
87local function printtable(result)
88    if type(result) == "table" then
89        for i=1,#result do
90            print(result[i])
91        end
92    end
93end
94
95if argument("exporthelp") then
96    application.export(environment.argument("exporthelp"),target)
97elseif argument("filetypes") or argument("formats") then
98    print(table.concat(kpse.get_file_types()," "))
99elseif type(target) == "string" and target ~= "" then
100    if argument("findfiles") or argument("find-files") then
101        printtable(kpse.find_files(argument("path"),target))
102    elseif argument("findfile") or argument("find-file") then
103        print(kpse.find_file(target,argument("format")))
104    elseif argument("expandpath") or argument("expand-path") then
105        local result = kpse.expand_path(target)
106        if result and argument("split") then
107            printtable(string.split(result,";"))
108        else
109            print(result)
110        end
111    elseif argument("expandvar") or argument("expand-var") then
112        print(kpse.expand_var(target))
113    elseif argument("expandbraces") or argument("expand-braces") then
114        print(kpse.expand_braces(target))
115    elseif argument("varvalue") or argument("var-value") then
116        print(kpse.var_value(target))
117    elseif argument("readablefile") or argument("readable-file") then
118        print(kpse.readable_file(target))
119    else
120        application.help()
121    end
122else
123    application.help()
124end
125