Debian Codegraph @

texmf-dist/tex/generic/pdftexcmds/pdftexcmds.lua

raw
1 --
2 -- This is file `pdftexcmds.lua',
3 -- generated with the docstrip utility.
4 --
5 -- The original source files were:
6 --
7 -- pdftexcmds.dtx (with options: `lua')
8 --
9 -- This is a generated file.
10 --
11 -- Project: pdftexcmds
12 -- Version: 2020-06-27 v0.33
13 --
14 -- Copyright (C)
15 -- 2007, 2009-2011 Heiko Oberdiek
16 -- 2016-2019 Oberdiek Package Support Group
17 --
18 -- This work may be distributed and/or modified under the
19 -- conditions of the LaTeX Project Public License, either
20 -- version 1.3c of this license or (at your option) any later
21 -- version. This version of this license is in
22 -- https://www.latex-project.org/lppl/lppl-1-3c.txt
23 -- and the latest version of this license is in
24 -- https://www.latex-project.org/lppl.txt
25 -- and version 1.3 or later is part of all distributions of
26 -- LaTeX version 2005/12/01 or later.
27 --
28 -- This work has the LPPL maintenance status "maintained".
29 --
30 -- The Current Maintainers of this work are
31 -- Heiko Oberdiek and the Oberdiek Package Support Group
32 -- https://github.com/ho-tex/pdftexcmds/issues
33 --
34 -- The Base Interpreter refers to any `TeX-Format',
35 -- because some files are installed in TDS:tex/generic//.
36 --
37 -- This work consists of the main source file pdftexcmds.dtx
38 -- and the derived files
39 -- pdftexcmds.sty, pdftexcmds.pdf, pdftexcmds.ins, pdftexcmds.drv,
40 -- pdftexcmds.lua.
41 --
42 oberdiek = oberdiek or {}
43 local pdftexcmds = oberdiek.pdftexcmds or {}
44 oberdiek.pdftexcmds = pdftexcmds
45 local systemexitstatus
46 function pdftexcmds.getversion()
47 tex.write("2020-06-27 v0.33")
48 end
49 function pdftexcmds.strcmp(A, B)
50 if A == B then
51 tex.write("0")
52 elseif A < B then
53 tex.write("-1")
54 else
55 tex.write("1")
56 end
57 end
58 local function utf8_to_byte(str)
59 local i = 0
60 local n = string.len(str)
61 local t = {}
62 while i < n do
63 i = i + 1
64 local a = string.byte(str, i)
65 if a < 128 then
66 table.insert(t, string.char(a))
67 else
68 if a >= 192 and i < n then
69 i = i + 1
70 local b = string.byte(str, i)
71 if b < 128 or b >= 192 then
72 i = i - 1
73 elseif a == 194 then
74 table.insert(t, string.char(b))
75 elseif a == 195 then
76 table.insert(t, string.char(b + 64))
77 end
78 end
79 end
80 end
81 return table.concat(t)
82 end
83 function pdftexcmds.escapehex(str, mode)
84 if mode == "byte" then
85 str = utf8_to_byte(str)
86 end
87 tex.write((string.gsub(str, ".",
88 function (ch)
89 return string.format("%02X", string.byte(ch))
90 end
91 )))
92 end
93 function pdftexcmds.unescapehex(str, mode, patch)
94 local a = 0
95 local first = true
96 local result = {}
97 for i = 1, string.len(str), 1 do
98 local ch = string.byte(str, i)
99 if ch >= 48 and ch <= 57 then
100 ch = ch - 48
101 elseif ch >= 65 and ch <= 70 then
102 ch = ch - 55
103 elseif ch >= 97 and ch <= 102 then
104 ch = ch - 87
105 else
106 ch = nil
107 end
108 if ch then
109 if first then
110 a = ch * 16
111 first = false
112 else
113 table.insert(result, a + ch)
114 first = true
115 end
116 end
117 end
118 if not first then
119 table.insert(result, a)
120 end
121 if patch == 1 then
122 local temp = {}
123 for i, a in ipairs(result) do
124 if a == 0 then
125 table.insert(temp, 1)
126 table.insert(temp, 1)
127 else
128 if a == 1 then
129 table.insert(temp, 1)
130 table.insert(temp, 2)
131 else
132 table.insert(temp, a)
133 end
134 end
135 end
136 result = temp
137 end
138 if mode == "byte" then
139 local utf8 = {}
140 for i, a in ipairs(result) do
141 if a < 128 then
142 table.insert(utf8, a)
143 else
144 if a < 192 then
145 table.insert(utf8, 194)
146 a = a - 128
147 else
148 table.insert(utf8, 195)
149 a = a - 192
150 end
151 table.insert(utf8, a + 128)
152 end
153 end
154 result = utf8
155 end
156 local unpack = _G["unpack"] or table.unpack
157 tex.settoks(pdftexcmds.toks, string.char(unpack(result)))
158 end
159 function pdftexcmds.escapestring(str, mode)
160 if mode == "byte" then
161 str = utf8_to_byte(str)
162 end
163 tex.write((string.gsub(str, ".",
164 function (ch)
165 local b = string.byte(ch)
166 if b < 33 or b > 126 then
167 return string.format("\\%.3o", b)
168 end
169 if b == 40 or b == 41 or b == 92 then
170 return "\\" .. ch
171 end
172 return nil
173 end
174 )))
175 end
176 function pdftexcmds.escapename(str, mode)
177 if mode == "byte" then
178 str = utf8_to_byte(str)
179 end
180 tex.write((string.gsub(str, ".",
181 function (ch)
182 local b = string.byte(ch)
183 if b == 0 then
184 return ""
185 end
186 if b <= 32 or b >= 127
187 or b == 35 or b == 37 or b == 40 or b == 41
188 or b == 47 or b == 60 or b == 62 or b == 91
189 or b == 93 or b == 123 or b == 125 then
190 return string.format("#%.2X", b)
191 else
192 return nil
193 end
194 end
195 )))
196 end
197 function pdftexcmds.filesize(filename)
198 local foundfile = kpse.find_file(filename, "tex", true)
199 if foundfile then
200 local size = lfs.attributes(foundfile, "size")
201 if size then
202 tex.write(size)
203 end
204 end
205 end
206 function pdftexcmds.filemoddate(filename)
207 local foundfile = kpse.find_file(filename, "tex", true)
208 if foundfile then
209 local date = lfs.attributes(foundfile, "modification")
210 if date then
211 local d = os.date("*t", date)
212 if d.sec >= 60 then
213 d.sec = 59
214 end
215 local u = os.date("!*t", date)
216 local off = 60 * (d.hour - u.hour) + d.min - u.min
217 if d.year ~= u.year then
218 if d.year > u.year then
219 off = off + 1440
220 else
221 off = off - 1440
222 end
223 elseif d.yday ~= u.yday then
224 if d.yday > u.yday then
225 off = off + 1440
226 else
227 off = off - 1440
228 end
229 end
230 local timezone
231 if off == 0 then
232 timezone = "Z"
233 else
234 local hours = math.floor(off / 60)
235 local mins = math.abs(off - hours * 60)
236 timezone = string.format("%+03d'%02d'", hours, mins)
237 end
238 tex.write(string.format("D:%04d%02d%02d%02d%02d%02d%s",
239 d.year, d.month, d.day, d.hour, d.min, d.sec, timezone))
240 end
241 end
242 end
243 function pdftexcmds.filedump(offset, length, filename)
244 length = tonumber(length)
245 if length and length > 0 then
246 local foundfile = kpse.find_file(filename, "tex", true)
247 if foundfile then
248 offset = tonumber(offset)
249 if not offset then
250 offset = 0
251 end
252 local filehandle = io.open(foundfile, "rb")
253 if filehandle then
254 if offset > 0 then
255 filehandle:seek("set", offset)
256 end
257 local dump = filehandle:read(length)
258 pdftexcmds.escapehex(dump)
259 filehandle:close()
260 end
261 end
262 end
263 end
264 function pdftexcmds.mdfivesum(str, mode)
265 if mode == "byte" then
266 str = utf8_to_byte(str)
267 end
268 pdftexcmds.escapehex(md5.sum(str))
269 end
270 function pdftexcmds.filemdfivesum(filename)
271 local foundfile = kpse.find_file(filename, "tex", true)
272 if foundfile then
273 local filehandle = io.open(foundfile, "rb")
274 if filehandle then
275 local contents = filehandle:read("*a")
276 pdftexcmds.escapehex(md5.sum(contents))
277 filehandle:close()
278 end
279 end
280 end
281 local basetime = 0
282 function pdftexcmds.resettimer()
283 basetime = os.clock()
284 end
285 function pdftexcmds.elapsedtime()
286 local val = (os.clock() - basetime) * 65536 + .5
287 if val > 2147483647 then
288 val = 2147483647
289 end
290 tex.write(string.format("%d", math.floor(val)))
291 end
292 function pdftexcmds.shellescape()
293 if os.execute then
294 if status
295 and status.luatex_version
296 and status.luatex_version >= 68 then
297 tex.write(os.execute())
298 else
299 local result = os.execute()
300 if result == 0 then
301 tex.write("0")
302 else
303 if result == nil then
304 tex.write("0")
305 else
306 tex.write("1")
307 end
308 end
309 end
310 else
311 tex.write("0")
312 end
313 end
314 function pdftexcmds.system(cmdline)
315 systemexitstatus = nil
316 texio.write_nl("log", "system(" .. cmdline .. ") ")
317 if os.execute then
318 texio.write("log", "executed.")
319 systemexitstatus = os.execute(cmdline)
320 else
321 texio.write("log", "disabled.")
322 end
323 end
324 function pdftexcmds.lastsystemstatus()
325 local result = tonumber(systemexitstatus)
326 if result then
327 local x = math.floor(result / 256)
328 tex.write(result - 256 * math.floor(result / 256))
329 end
330 end
331 function pdftexcmds.lastsystemexit()
332 local result = tonumber(systemexitstatus)
333 if result then
334 tex.write(math.floor(result / 256))
335 end
336 end
337 function pdftexcmds.pipe(cmdline, patch)
338 local result
339 systemexitstatus = nil
340 texio.write_nl("log", "pipe(" .. cmdline ..") ")
341 if io.popen then
342 texio.write("log", "executed.")
343 local handle = io.popen(cmdline, "r")
344 if handle then
345 result = handle:read("*a")
346 handle:close()
347 end
348 else
349 texio.write("log", "disabled.")
350 end
351 if result then
352 if patch == 1 then
353 local temp = {}
354 for i, a in ipairs(result) do
355 if a == 0 then
356 table.insert(temp, 1)
357 table.insert(temp, 1)
358 else
359 if a == 1 then
360 table.insert(temp, 1)
361 table.insert(temp, 2)
362 else
363 table.insert(temp, a)
364 end
365 end
366 end
367 result = temp
368 end
369 tex.settoks(pdftexcmds.toks, result)
370 else
371 tex.settoks(pdftexcmds.toks, "")
372 end
373 end
374 --
375 -- End of File `pdftexcmds.lua'.