Debian Codegraph @

texk/texlive/linked_scripts/musixtex/musixtex.lua

raw symbols SCIP
1 #!/usr/bin/env texlua
2
3 VERSION = "0.23b"
4
5 --[[
6 musixtex.lua: processes MusiXTeX files using xml2pmx and/or prepmx and/or pmxab
7 and/or autosp as pre-processors (and deletes intermediate files)
8
9 (c) Copyright 2011-2021 Bob Tennent [email protected]
10 and Dirk Laurie [email protected]
11
12 This program is free software; you can redistribute it and/or modify it
13 under the terms of the GNU General Public License as published by the
14 Free Software Foundation; either version 2 of the License, or (at your
15 option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
20 Public License for more details.
21
22 You should have received a copy of the GNU General Public License along
23 with this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25
26 --]]
27
28 --[[
29
30 ChangeLog:
31
32 version 0.23b 2020-05-27 RDT
33 improve the -h output
34
35 version 0.23a 2020-08-14 RDT
36 pmxprep files now deleted by xml2pmx
37
38 version 0.23 2020-05-21 RDT
39 added support for xml2pmx pre-preprocessing
40
41 version 0.22 2020-03-20 RDT
42 add -X option
43 add -version, --version, -help, --help options
44
45 version 0.21 2018-07-27 RDT
46 add -P option.
47
48 version 0.20 2018-06-11 RDT
49 remove .mx1 file before tex processing
50
51 version 0.19 2017-12-10 RDT
52 Allow non-standard extensions.
53 Add -M and -A options.
54
55 version 0.18 2017-06-13 RDT
56 Allow autosp to generate .ltx files
57
58 version 0.17a 2017-01-08 RDT
59 Added -D option.
60 Avoid writing or concatenating a nil value.
61
62 version 0.16e 2016-03-02 DL
63 missing version information (caused by batchmode in 0.16c) fixed
64
65 version 0.16d 2016-03-02 RDT
66 filename argument in autosp failure message fixed
67
68 version 0.16c 2016-02-24 RDT
69 -interaction batchmode for -q
70 report_error reports only the first error
71
72 version 0.16b 2016-02-20 DL
73 Improved help message as suggested by Bob Tennent.
74
75 version 0.16a 2015-12-30 DL
76 Corrects bug in -g option reported by Christian Mondrup.
77
78 version 0.16 2015-12-24 DL
79 Versions read from tempfile and written to musixtex.log.
80
81 version 0.15 2015-12-03 DL
82 Option -q added, which redirects most screen output into
83 a temporary file. If an error occurs at the TeX stage, processing
84 halts immediately and the tail of the log file is sent to stderr.
85
86 version 0.14 2015-12-01 DL
87 Writes `musixtex.log` and deletes other logfiles (except with -i)
88 Hierarchy of extensions emphasized in usage() and consistently
89 used elsewhere.
90 Unknown option-like arguments reported and ignored, not treated
91 as filenames
92 Warnings and error messages generated by musixtex.lua start with
93 respctively `!` and `!!`.
94
95 version 0.13 2015-11-30 RDT
96 Process .mtx and .pmx files (suggested by Dirk Laurie)
97 exit_code is now an error count
98 Use pmxab exit code to distinguish between errors and warnings
99
100 version 0.12 2015-11-28 RDT
101 Process .ltx files, with -l implied
102
103 version 0.11 2015-07-16 RDT
104 Automatic autosp preprocessing.
105
106 version 0.10 2015-04-23 RDT
107 Add -a option to preprocess using autosp
108
109 version 0.9 2015-02-13 RDT
110 Add an additional latex pass to resolve cross-references.
111 Add -e0 option to dvips as given in musixdoc.tex
112 Add -x option to call makeindex
113
114 version 0.8 2014-05-18 RDT
115 Add -g option
116
117 version 0.7 2013-12-11 RDT
118 Add -F fmt option
119
120 version 0.6 2012-09-14 RDT
121 Add -1 (one-pass [pdf][la]tex processing) option.
122
123 version 0.5 2011-11-28 RDT
124 Add -i (retain intermediate files) option.
125
126 version 0.4 2011-04-30 RDT
127 Allow multiple filenames (and options).
128 Add -f (default) and -l (latex) options.
129
130 version 0.3 2011-04-25 RDT
131 Add -d (dvipdfm) and -s (stop at dvi) options.
132
133 version 0.2 2011-04-21 RDT
134 Allow basename.tex as filename.
135 Add -p option for pdfetex processing.
136 Add standard -v -h options.
137 --]]
138 local orig_print = print
139
140 function usage()
141 orig_print
142 [[
143 Usage: [texlua] musixtex.lua { option | basename[ .xml | .mtx | .pmx | .aspc | .tex | .ltx] } ...
144 When no extension is given, extensions are tried in the above order
145 until a source file is found. Preprocessing goes
146 .xml - .pmx - .tex
147 .mtx - .pmx - .tex
148 .pmx - .tex
149 .aspc - .tex
150 with the entry point determined by the filename extension.
151 The normal route after preprocessing goes .tex - .dvi - .ps - .pdf,
152 but shorter routes are also available; see the options.
153 The default 3-pass processing route for .tex files is
154 etex - musixflx - etex.
155 .ltx files, possibly after autosp preprocessing, are treated as latex
156 source and processed by latex (or pdflatex) rather than etex.
157 Options: -v, --version version
158 -h, --help help
159 -l latex source; implied by .ltx extension
160 -p direct tex-pdf (pdftex etc)
161 -F fmt use fmt as the TeX processor
162 -d tex-dvi-pdf (using dvipdfm if -D not used)
163 -D dvixx use dvixx as the dvi processor
164 -P ps2pdfxx use ps2pdfxx as the Postscript processor
165 -c preprocess pmx file using pmxchords
166 -m stop at pmx
167 -M prepmxx use prepmxx as the mtx preprocessor
168 -A autospx use autospx as the aspc preprocessor
169 -X pmxabx use pmxabx as the pmx preprocessor
170 -L xmlx use xmlx as the xml preprocessor
171 -t stop at tex/mid
172 -s stop at dvi
173 -g stop at ps
174 -i retain intermediate and log files
175 -q quiet mode (redirect screen logs, write summary on musixtex.log)
176 -1 one-pass [pdf][la]tex processing
177 -x run makeindex
178 -f restore default processing
179 Four TeX engines are available via the -l and -p options.
180 etex default
181 latex -l
182 pdfetex -p
183 pdflatex -l -p
184 If the -F option is used, options -l and -p need to be set if the engine
185 name does not contain "latex" and "pdf" respectively. For example, the
186 above four engines can be replaced by:
187 -F "luatex --output-format=dvi"
188 -F "lualatex --output-format=dvi"
189 -F "luatex" -p
190 -F "lualatex" -p
191 ]]
192 end
193
194 function whoami ()
195 print("This is musixtex.lua version ".. VERSION .. ".")
196 end
197
198 function exists (filename, nolog)
199 local f = io.open(filename, "r")
200 if f then
201 f:close()
202 if not nolog then musixlog:write("Processing " .. filename,'\n' ) end
203 return true
204 else
205 return false
206 end
207 end
208
209 -- System commands for the various programs are mostly
210 -- set to nil if the step is to be omitted, which can be
211 -- tested by a simple "if" statement.
212 -- Exceptions:
213 -- 'tex' is the command for processing a TeX file, but it is important
214 -- to know whether the user has explicitly specified an option that
215 -- affects this choice, in which case the automatic selection of the
216 -- command is restricted or disabled.
217 -- force_engine "use the command provided"
218 -- override records when 'l' and/or 'p' options are in effect
219 -- 'dvi' is the command for processing a DVI file, but there are two
220 -- reasons for not needing it and it is important for the automatic
221 -- selection of a TeX engine to know which is in effect. This is
222 -- possible by exploiting the the fact that Lua has two false values.
223 -- dvi == nil "do not produce a DVI file" (but maybe PDF)
224 -- dvi == false "do not process the DVI file" (but stop after TeX)
225
226 local dvips = "dvips -e0"
227 -- option -e0 suppresses dvips "feature" of adjusting location to align
228 -- characters in words of text
229
230 function defaults()
231 xml2pmx = "xml2pmx"
232 prepmx = "prepmx"
233 pmx = "pmxab"
234 autosp = "autosp"
235 tex = "etex"
236 musixflx = "musixflx"
237 dvi = dvips
238 ps2pdf = "ps2pdf"
239 cleanup = true -- clean up intermediate and log files
240 index = false
241 latex = false
242 force_engine = false -- indicates whether -F is specified
243 override = ""
244 passes = 2
245 quiet = "" -- set by "-q"
246 end
247
248 ------------------------------------------------------------------------
249
250 if #arg == 0 then
251 whoami()
252 usage()
253 os.exit(0)
254 end
255
256 -- Logging on `musixtex.log` everything that is printed, all os calls,
257 -- and warnings from other log files.
258
259 musixlog = io.open("musixtex.log","w")
260 if not musixlog then
261 print"!! Can't open files for writing in current directory, aborting"
262 os.exit(1)
263 end
264
265 ------------------------------------------------------------------------
266
267 print = function(...)
268 orig_print(...)
269 musixlog:write(...,"\n") -- only the first argument gets written!
270 end
271
272 local orig_remove = os.remove
273 remove = function(filename)
274 if exists(filename,"nolog") then
275 musixlog:write(" removing ",filename,"\n")
276 return orig_remove(filename)
277 end
278 end
279
280 local orig_execute = os.execute
281 execute = function(command)
282 musixlog:write(" ",command,"\n")
283 return orig_execute(command .. quiet)
284 end
285
286 function report_warnings(filename,pattern)
287 local log = io.open(filename)
288 if not log then
289 print("! No file "..filename)
290 return
291 end
292 for line in log:lines() do
293 if line:match(pattern) then
294 musixlog:write("! "..line,"\n")
295 end
296 end
297 end
298
299 function report_error(filename)
300 local log = io.open(filename)
301 if not log then
302 print("! No file "..filename)
303 return
304 end
305 local trigger = false
306 for line in log:lines() do
307 if trigger and line:match"^!" then
308 -- report just the first error
309 break
310 end
311 trigger = trigger or line:match"^!"
312 if trigger then
313 io.stderr:write("! "..line,"\n")
314 end
315 end
316 end
317
318 function process_option(this_arg)
319 if this_arg == "-v" or this_arg == "-version" or this_arg == "--version" then
320 os.exit(0)
321 elseif this_arg == "-h" or this_arg == "-help" or this_arg == "--help" then
322 usage()
323 os.exit(0)
324 elseif this_arg == "-l" then
325 latex = true
326 override = override .. 'l'
327 elseif this_arg == "-p" then
328 dvi = nil
329 override = override .. 'p'
330 elseif this_arg == "-d" then
331 dvi = "dvipdfm"; ps2pdf = nil
332 elseif this_arg == "-D" then
333 narg = narg+1
334 dvi = arg[narg]
335 elseif this_arg == "-c" then
336 pmx = "pmxchords"
337 elseif this_arg == "-F" then
338 narg = narg+1
339 tex = arg[narg]
340 force_engine = true
341 elseif this_arg == "-s" then
342 dvi = false; ps2pdf = nil; protect.dvi = true
343 elseif this_arg == "-g" then
344 dvi = dvips; ps2pdf = nil; protect.ps = true
345 elseif this_arg == "-i" then
346 cleanup = false
347 elseif this_arg == "-x" then
348 index = true
349 elseif this_arg == "-1" then
350 passes = 1
351 elseif this_arg == "-f" then
352 defaults()
353 elseif this_arg == "-t" then
354 tex, dvi, ps2pdf = nil,nil,nil
355 protect.tex = true
356 elseif this_arg == "-m" then
357 pmx, tex, dvi, ps2pdf = nil,nil,nil,nil
358 protect.pmx = true
359 elseif this_arg == "-M" then
360 narg = narg+1
361 prepmx = arg[narg]
362 elseif this_arg == "-A" then
363 narg = narg+1
364 autosp = arg[narg]
365 elseif this_arg == "-L" then
366 narg = narg+1
367 xml2pmx = arg[narg]
368 elseif this_arg == "-q" then
369 if not tempname then
370 tempname = tempname or os.tmpname()
371 print("Redirecting screen output to "..tempname)
372 end
373 if dvi == dvips then dvi = dvi .. " -q" end
374 quiet = " >> " .. tempname
375 elseif this_arg == "-P" then
376 narg = narg+1
377 ps2pdf = arg[narg]
378 elseif this_arg == "-X" then
379 narg = narg+1
380 pmx = arg[narg]
381 else
382 print("! Unknown option "..this_arg.." ignored")
383 end
384 end
385
386 function find_file(this_arg)
387 basename, extension = this_arg:match"(.*)%.(.*)"
388 extensions = {["xml"] = true, ["mtx"] = true, ["pmx"] = true, ["aspc"] = true, ["tex"] = true, ["ltx"] = true}
389 if extensions[extension] then
390 return basename, extension
391 end
392 basename, extension = this_arg, null
393 for ext in ("xml,mtx,pmx,aspc,tex,ltx"):gmatch"[^,]+" do
394 if exists (basename .. "." .. ext) then
395 extension = ext
396 break
397 end
398 end
399 if extension == null then
400 print("!! No file " .. basename .. ".[xml|mtx|pmx|aspc|tex|ltx]")
401 exit_code = exit_code+1
402 return
403 end
404 return basename, extension
405 end
406
407 function preprocess(basename,extension)
408 if not (basename and extension) then return end
409 if extension == "xml" then
410 if execute(xml2pmx .. " " .. basename .. ".xml" .. " " .. basename .. ".pmx" ) == 0 then
411 extension = "pmx"
412 else
413 print ("!! xml2pmx preprocessing of " .. basename .. ".xml fails.")
414 return
415 end
416 elseif extension == "mtx" then
417 if execute(prepmx .. " " .. basename ) == 0 then
418 extension = "pmx"
419 else
420 print ("!! prepmx preprocessing of " .. basename .. ".mtx fails.")
421 return
422 end
423 end
424 if extension == "pmx" then
425 local OK = true
426 if pmx then
427 local code = execute(pmx .. " " .. basename)
428 local pmxaerr = io.open("pmxaerr.dat", "r")
429 if (not pmxaerr) then
430 OK = false
431 print("!! No pmx log file.")
432 else
433 extension = "tex"
434 local linebuf = pmxaerr:read()
435 local err = tonumber(linebuf)
436 if (code == 0) then
437 if err ~=0 then
438 print ("! pmx produced a warning on line "..err..
439 " of "..basename..".pmx")
440 end
441 else
442 OK = false
443 print ("!! pmx processing of " .. basename .. ".pmx fails.")
444 end
445 pmxaerr:close()
446 end
447 end
448 if not OK then
449 exit_code = exit_code+1
450 return
451 end
452 end
453 if extension == "aspc" then
454 if execute (autosp .. " " .. basename .. ".aspc" ) == 0 then
455 if exists ( basename .. ".ltx")
456 then extension = "ltx"
457 else extension = "tex"
458 end
459 else
460 print ("!! autosp preprocessing of " .. basename .. ".aspc fails.")
461 exit_code = exit_code+1
462 return
463 end
464 end
465 return extension
466 end
467
468 function tex_process(tex,basename,extension)
469 if not (extension == "tex" or extension == "ltx") or not tex then return end
470 remove(basename .. ".mx1")
471 remove(basename .. ".mx2")
472 local filename = basename .. "." ..extension
473 -- .ltx extension re-selects engine only for the current file, and only
474 -- if default processing is plain TeX
475 local latex = latex
476 if extension == "ltx" then
477 if not force_engine and not latex then
478 if dvi then tex = "latex" else tex = "pdflatex" end
479 end
480 latex = true
481 end
482 if quiet ~= "" then
483 tex = tex .. " -interaction batchmode"
484 end
485 local OK = (execute(tex .. " " .. filename) == 0)
486 if passes ~= 1 then
487 OK = OK and (execute(musixflx .. " " .. basename) == 0)
488 and (execute(tex .. " " .. filename) == 0)
489 if latex and index then
490 OK = OK and (execute("makeindex -q " .. basename) == 0)
491 and (execute(tex .. " " .. filename) == 0)
492 if exists (basename .. ".toc","nolog") then
493 -- an extra line for the index will have been added
494 OK = OK and (execute(tex .. " " .. filename) == 0)
495 -- there is a tiny possibility that the extra line for the index
496 -- has changed the pagination. If you feel this should not be
497 -- ignored, here are the possibilities:
498 -- a. Do an extra TeX pass regardless.
499 -- b. Provide a user option -4 to force the extra pass.
500 -- c. Compare aux files to find out.
501 end
502 end
503 end
504 if (quiet ~= "") and not OK then
505 report_error(basename..".log")
506 end
507 if OK and latex then
508 report_warnings(basename..".log","LaTeX.*Warning")
509 end
510 if dvi then OK = OK and (execute(dvi .. " " .. basename) == 0) end
511 if dvi and ps2pdf then
512 OK = OK and (execute(ps2pdf .. " " .. basename .. ".ps") == 0)
513 if OK then
514 print(basename .. ".pdf generated by " .. ps2pdf .. ".")
515 end
516 end
517 if not OK then
518 print("!! Processing of " .. filename .. " fails.\n")
519 exit_code = exit_code+1
520 end
521 end
522
523 ---- Report version information on musixtex.log
524
525 -- File names and message signatures to be looked for in a TeX log file.
526 logtargets = {
527 mtxtex = {"mtx%.tex","mtxTeX"},
528 mtxlatex = {"mtxlatex%.sty","mtxLaTeX"},
529 pmxtex = {"pmx%.tex","PMX"},
530 musixtex = {"musixtex%.tex","MusiXTeX"},
531 musixltx = {"musixltx%.tex","MusiXLaTeX"},
532 musixlyr = {"musixlyr%.tex","MusiXLYR"}
533 }
534
535 -- Signatures of messages displayed on standard output by programs
536 capturetargets = {
537 MTx = "This is (M%-Tx.->)",
538 PMX = "This is (PMX[^\n]+)",
539 pdftex = "This is (pdfTeX[^\n]+)",
540 musixflx = "Musixflx%S+",
541 autosp = "This is autosp.*$",
542 index = "autosp,MTx,PMX,musixflx,pdftex"
543 }
544
545 function report_texfiles(logname)
546 local log = logname and io.open(logname)
547 if not log then return end
548 local lines =
549 {"--- TeX files actually included according to "..logname.." ---"}
550 log = log:read"*a"
551 -- The following pattern matches filenames input by TeX, even if
552 -- interrupted by a line break. It may include the first word of
553 -- an \immediate\write10 message emitted by the file.
554 for pos,filename in log:gmatch"%(()(%.?[/]%a[%-/%.%w\n]+)" do
555 local hit
556 repeat
557 local oldfilename = filename
558 filename = oldfilename:match"[^\n]+" -- up to next line break
559 hit = io.open(filename) -- success if the file exists
560 if hit then break end
561 filename = oldfilename:gsub("\n","",1) -- remove line break
562 until filename==oldfilename
563 if hit then
564 for target,sig in pairs(logtargets) do
565 if filename:match(sig[1]) then
566 local i,j = log:find(sig[2].."[^\n]+",pos)
567 if j then lines[#lines+1] = filename.."\n "..log:sub(i,j) end
568 end
569 end
570 end
571 end
572 return table.concat(lines,'\n').."\n"
573 end
574
575 function report_versions(tempname)
576 if not tempname then return end -- only available with -q
577 local logs = io.open(tempname)
578 if not logs then
579 musixlog:write ("No version information: could not open "..tempname)
580 return
581 end
582 local versions = {}
583 musixlog:write("--- Programs actually executed according to "..tempname.." ---\n")
584 for line in logs:lines() do
585 for target in capturetargets.index:gmatch"[^,]+" do
586 if not versions[target] then
587 local found = line:match(capturetargets[target])
588 if found then
589 versions[target] = found
590 musixlog:write(found,"\n")
591 end
592 end
593 end
594 end
595 logs:close()
596 return
597 end
598
599 ------------------------------------------------------------------------
600
601 whoami()
602
603 defaults()
604
605 exit_code = 0
606 narg = 1
607 protect = {} -- extensions not to be deleted, even if cleanup requested
608
609 repeat
610 this_arg = arg[narg]
611 if this_arg:match"^%-" then process_option(this_arg)
612 else
613 basename, extension = find_file(this_arg) -- nil,nil if not found
614 if tex then -- tex output enabled, now select engine
615 if tex:match"pdf" then dvi = nil end
616 if not dvi then ps2pdf = nil end
617 -- .ltx extension will be taken into account later, in `process`
618 -- deduce tex/latex from current engine name if -l is not specified
619 if not override:match"l" then latex = tex:match"latex" end
620 if not force_engine then -- select appropriate default engine
621 if latex then
622 if dvi==nil then tex = "pdflatex" else tex = "latex" end
623 else
624 if dvi==nil then tex = "pdfetex" else tex = "etex" end
625 end
626 end
627 end
628 extension = preprocess(basename, extension)
629 tex_process(tex,basename,extension)
630 if basename and io.open(basename..".log") then -- to be printed later
631 versions = report_texfiles(basename..".log")
632 end
633 if basename and cleanup then
634 remove("pmxaerr.dat")
635 for ext in ("mx1,mx2,dvi,ps,idx,log,ilg,pml"):gmatch"[^,]+" do
636 if not protect[ext] then remove(basename.."."..ext)
637 end
638 end
639 end
640 protect = {}
641 end
642 narg = narg+1
643 until narg > #arg
644
645 if versions then musixlog:write(versions) end
646 report_versions(tempname)
647 musixlog:close()
648 os.exit( exit_code )