| 1 |
#!/usr/bin/env texlua |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
NAME = "runtexshebang" |
| 26 |
VERSION = "20231117 v0.5" |
| 27 |
USAGE = [[ |
| 28 |
Usage: runtexshebang [input.tex] |
| 29 |
|
| 30 |
Options: |
| 31 |
--dry-run print the TeX-style shebang line of [input.tex] |
| 32 |
-h, --help print help |
| 33 |
-v, --version print version |
| 34 |
|
| 35 |
See also: |
| 36 |
The command |
| 37 |
texdoc runtexshebang |
| 38 |
should give you access to the README. |
| 39 |
]] |
| 40 |
|
| 41 |
function whoami() |
| 42 |
print(NAME .. " " .. VERSION) |
| 43 |
end |
| 44 |
|
| 45 |
function help() |
| 46 |
whoami() |
| 47 |
print() |
| 48 |
print(USAGE) |
| 49 |
end |
| 50 |
|
| 51 |
if #arg == 0 then |
| 52 |
help() |
| 53 |
os.exit(0) |
| 54 |
end |
| 55 |
|
| 56 |
|
| 57 |
is_dryrun = false |
| 58 |
texfilename = "" |
| 59 |
narg = 1 |
| 60 |
repeat |
| 61 |
this_arg = arg[narg] |
| 62 |
|
| 63 |
this_arg = string.gsub(this_arg, "^%-%-", "-") |
| 64 |
|
| 65 |
if this_arg == "-h" or this_arg == "-help" then |
| 66 |
help() |
| 67 |
os.exit(0) |
| 68 |
elseif this_arg == "-v" or this_arg == "-version" then |
| 69 |
whoami() |
| 70 |
os.exit(0) |
| 71 |
elseif this_arg == "-dry-run" then |
| 72 |
is_dryrun = true |
| 73 |
else |
| 74 |
texfilename = this_arg |
| 75 |
end |
| 76 |
narg = narg+1 |
| 77 |
until narg > #arg |
| 78 |
|
| 79 |
|
| 80 |
whoami() |
| 81 |
|
| 82 |
if ( texfilename == "" ) then |
| 83 |
print("No filename argument given, exiting.\n") |
| 84 |
os.exit(1) |
| 85 |
end |
| 86 |
|
| 87 |
line_ctr = 0 |
| 88 |
for line in io.lines(texfilename) do |
| 89 |
line_ctr = line_ctr + 1 |
| 90 |
if line_ctr > 20 then break end |
| 91 |
|
| 92 |
if string.match(line, "^%%#!") then |
| 93 |
tex_cmd, err=string.gsub(line, "%%#!", "") |
| 94 |
|
| 95 |
if is_dryrun then |
| 96 |
print(tex_cmd .. "\n") |
| 97 |
os.exit(0) |
| 98 |
end |
| 99 |
|
| 100 |
tex_return = os.execute(tex_cmd) |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
if os.type == 'windows' and tex_return == -1 then |
| 105 |
print("Invalid PATH setting found. Please ensure that cmd.exe can be found.\n") |
| 106 |
os.exit(1) |
| 107 |
end |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
os.exit(0) |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
end |
| 119 |
end |
| 120 |
|
| 121 |
print("TeX-style shebang not found.\n") |
| 122 |
os.exit(2) |