1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
if not modules then modules = { } end modules ['mlib-run'] = {
version = 1.001,
comment = "companion to mlib-ctx.mkiv",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files",
}
local type = type
local find = string.find
local concat = table.concat
local trace_terminal = false trackers.register("metapost.terminal", function(v) trace_terminal = v end)
local report_metapost = logs.reporter("metapost")
local report_terminal = logs.reporter("metapost","terminal")
local report_logger = logs.reporter("metapost","log")
local report_error = logs.reporter("metapost","error")
mplib.realtimelogging = false
local l, nl, dl = { }, 0, false
local t, nt, dt = { }, 0, false
local e, ne, de = { }, 0, false
local function logger(target,str)
if target == 1 then
-- log
elseif target == 2 or target == 3 then
-- term
if str == "\n" then
mplib.realtimelogging = true
if nl > 0 then
report_logger(concat(l,"",1,nl))
nl, dl = 0, false
elseif not dl then
report_logger("")
dl = true
end
else
nl = nl + 1
l[nl] = str
end
elseif target == 4 then
report_error(str)
end
end
local finders = { }
mplib.finders = finders -- also used in meta-lua.lua
local new_instance = mplib.new
local function validftype(ftype)
if ftype == "mp" then
return "mp"
else
return nil
end
end
finders.file = function(specification,name,mode,ftype)
return resolvers.findfile(name,validftype(ftype))
end
local findtexfile = resolvers.findtexfile
local opentexfile = resolvers.opentexfile
local splitlines = string.splitlines
local function writetoterminal(terminaldata,maxterm,d)
local t = type(d)
local n = 0
if t == "string" then
d = splitlines(d)
n = #d
for i=1,#d do
maxterm = maxterm + 1
terminaldata[maxterm] = d[i]
end
elseif t == "table" then
for i=1,#d do
local l = d[i]
if find(l,"[\n\r]") then
local s = splitlines(l)
local m = #s
for i=1,m do
maxterm = maxterm + 1
terminaldata[maxterm] = s[i]
end
n = n + m
else
maxterm = maxterm + 1
terminaldata[maxterm] = d[i]
n = 1
end
end
end
if trace_terminal then
report_metapost("writing %i lines, in cache %s",n,maxterm)
end
return maxterm
end
local function readfromterminal(terminaldata,maxterm,nowterm)
if nowterm >= maxterm then
terminaldata[nowterm] = false
maxterm = 0
nowterm = 0
if trace_terminal then
report_metapost("resetting, maxcache %i",#terminaldata)
end
return maxterm, nowterm, nil
else
if nowterm > 0 then
terminaldata[nowterm] = false
end
nowterm = nowterm + 1
local s = terminaldata[nowterm]
if trace_terminal then
report_metapost("reading line %i: %s",nowterm,s)
end
return maxterm, nowterm, s
end
end
local function fileopener()
-- these can go into the table itself
local terminaldata = { }
local maxterm = 0
local nowterm = 0
local terminal = {
name = "terminal",
close = function()
-- terminal = { }
-- maxterm = 0
-- nowterm = 0
end,
reader = function()
local line
maxterm, nowterm, line = readfromterminal(terminaldata,maxterm,nowterm)
return line
end,
writer = function(d)
maxterm = writetoterminal(terminaldata,maxterm,d)
end,
}
return function(name,mode,kind)
if name == "terminal" then
-- report_metapost("opening terminal")
return terminal
elseif mode == "w" then
local f = io.open(name,"wb")
if f then
-- report_metapost("opening file %a for writing",full)
return {
name = full,
writer = function(s) return f:write(s) end, -- io.write(f,s)
close = function() f:close() end,
}
end
else
local full = findtexfile(name,validftype(ftype))
if full then
-- report_metapost("opening file %a for reading",full)
return opentexfile(full)
end
end
end
end
local function finder(name,mode,kind)
return findtexfile(name,kind)
end
function mplib.new(specification)
local openfile = fileopener()
specification.find_file = finder
specification.run_logger = logger
specification.open_file = openfile
specification.interaction = "silent"
specification.halt_on_error = true
local instance = new_instance(specification)
return instance, openfile("terminal")
end
mplib.finder = finder
|