| 1 |
#!/usr/bin/env texlua |
| 2 |
|
| 3 |
VERSION = "0.23b" |
| 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 |
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 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
local dvips = "dvips -e0" |
| 227 |
|
| 228 |
|
| 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 |
| 240 |
index = false |
| 241 |
latex = false |
| 242 |
force_engine = false |
| 243 |
override = "" |
| 244 |
passes = 2 |
| 245 |
quiet = "" |
| 246 |
end |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
if #arg == 0 then |
| 251 |
whoami() |
| 252 |
usage() |
| 253 |
os.exit(0) |
| 254 |
end |
| 255 |
|
| 256 |
|
| 257 |
|
| 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") |
| 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 |
|
| 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 |
|
| 474 |
|
| 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 |
|
| 494 |
OK = OK and (execute(tex .. " " .. filename) == 0) |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 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 |
|
| 524 |
|
| 525 |
|
| 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 |
|
| 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 |
|
| 552 |
|
| 553 |
|
| 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]+" |
| 559 |
hit = io.open(filename) |
| 560 |
if hit then break end |
| 561 |
filename = oldfilename:gsub("\n","",1) |
| 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 |
| 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 = {} |
| 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) |
| 614 |
if tex then |
| 615 |
if tex:match"pdf" then dvi = nil end |
| 616 |
if not dvi then ps2pdf = nil end |
| 617 |
|
| 618 |
|
| 619 |
if not override:match"l" then latex = tex:match"latex" end |
| 620 |
if not force_engine then |
| 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 |
| 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 ) |