luat-fio.lmt /size: 3140 b    last modification: 2021-10-28 13:51
1if not modules then modules = { } end modules ['luat-fio'] = {
2    version   = 1.001,
3    comment   = "companion to luat-lib.mkiv",
4    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
5    copyright = "PRAGMA ADE / ConTeXt Development Team",
6    license   = "see context related readme files"
7}
8
9local format = string.format
10local concat = table.concat
11
12if not resolvers.initialized() then
13
14    resolvers.reset()
15
16    -- we now load the file database as we might need files other than
17    -- tex and lua file on the given path
18
19    resolvers.load()
20
21    if callback then
22
23        local findbinfile = resolvers.findbinfile
24        local findtexfile = resolvers.findtexfile
25        local opentexfile = resolvers.opentexfile
26        local register    = callbacks.register
27
28        local ioflush     = io.flush
29        local ioread      = io.read
30        local writenl     = texio.writenl
31
32        local function terminal()
33            writenl("\ntex console > ")
34            ioflush()
35            local line = ioread()
36            writenl("")
37            ioflush()
38            return line
39        end
40
41     -- local function find_data_file(name)
42     --     if not name or name == "" then
43     --         return "context terminal"
44     --     else
45     --         return findbinfile(name,"tex")
46     --     end
47     -- end
48     --
49     -- local function open_data_file(name)
50     --     if not name or name == "" or name == "context terminal" then
51     --         return {
52     --             reader   = terminal,
53     --             noflines = 1,
54     --             filename = name,
55     --         }
56     --     else
57     --         name = opentexfile(name)
58     --         return name ~= "" and name or false
59     --     end
60     -- end
61
62        local function open_data_file(name)
63            if not name or name == "" then
64                return {
65                    reader   = terminal,
66                    noflines = 1,
67                    filename = "context terminal"
68                }
69            else
70                local fullname = findbinfile(name,"tex")
71                if fullname then
72                    return opentexfile(fullname)
73                else
74                    return false
75                end
76            end
77        end
78
79        local function find_any_file(name)
80            return name
81        end
82
83        register('find_log_file'   , find_any_file,  true) -- why do we have this callback if we just return the name
84        register('find_format_file', find_any_file,  true) -- why do we have this callback if we just return the name
85     -- register('find_data_file'  , find_data_file, true)
86        register('open_data_file'  , open_data_file, true)
87
88    end
89
90end
91
92statistics.register("resource resolver", function()
93    local scandata = resolvers.scandata()
94    return format("loadtime %s seconds, %s scans with scantime %s seconds, %s shared scans, %s found files, scanned paths: %s",
95        resolvers.loadtime(),
96        scandata.n,
97        scandata.time,
98        scandata.shared,
99        #resolvers.foundintrees(),
100        #scandata.paths > 0 and concat(scandata.paths," ") or "<none>"
101    )
102end)
103