| 1 |
(* This file is part of ClutTeX. *) |
| 2 |
structure Main = struct |
| 3 |
val CLUTTEX_VERSION = "v0.7.0" |
| 4 |
|
| 5 |
val COPYRIGHT_NOTICE = |
| 6 |
"Copyright (C) 2016-2024 ARATA Mizuki\n\ |
| 7 |
\\n\ |
| 8 |
\This program is free software: you can redistribute it and/or modify\n\ |
| 9 |
\it under the terms of the GNU General Public License as published by\n\ |
| 10 |
\the Free Software Foundation, either version 3 of the License, or\n\ |
| 11 |
\(at your option) any later version.\n\ |
| 12 |
\\n\ |
| 13 |
\This program is distributed in the hope that it will be useful,\n\ |
| 14 |
\but WITHOUT ANY WARRANTY; without even the implied warranty of\n\ |
| 15 |
\MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\ |
| 16 |
\GNU General Public License for more details.\n\ |
| 17 |
\\n\ |
| 18 |
\You should have received a copy of the GNU General Public License\n\ |
| 19 |
\along with this program. If not, see <http://www.gnu.org/licenses/>.\n"; |
| 20 |
|
| 21 |
exception Abort |
| 22 |
|
| 23 |
(* Workaround for recent Universal CRT *) |
| 24 |
val () = Lua.call0 Lua.Lib.os.setlocale #[Lua.fromString "", Lua.fromString "ctype"] |
| 25 |
|
| 26 |
fun getEnvMulti [] = NONE |
| 27 |
| getEnvMulti (name :: xs) = case OS.Process.getEnv name of |
| 28 |
SOME x => SOME x |
| 29 |
| NONE => getEnvMulti xs |
| 30 |
|
| 31 |
fun genOutputDirectory (temporary_directory : string option, xs : string list) |
| 32 |
= let val message = String.concatWith "\000" xs |
| 33 |
val hash = MD5.md5AsLowerHex (Byte.stringToBytes message) |
| 34 |
val tmpdir = case temporary_directory of |
| 35 |
SOME tmpdir => tmpdir |
| 36 |
| NONE => case getEnvMulti ["TMPDIR", "TMP", "TEMP"] of |
| 37 |
SOME tmpdir => tmpdir |
| 38 |
| NONE => case getEnvMulti ["HOME", "USERPROFILE"] of |
| 39 |
SOME home => OS.Path.joinDirFile { dir = home, file = ".latex-build-temp" } (* $XDG_CACHE_HOME/cluttex, $HOME/.cache/cluttex *) |
| 40 |
| NONE => raise Fail "environment variable 'TMPDIR' not set!" |
| 41 |
in OS.Path.joinDirFile { dir = tmpdir, file = "cluttex-" ^ hash } |
| 42 |
end |
| 43 |
|
| 44 |
fun showUsage () = let val progName = CommandLine.name () |
| 45 |
in TextIO.output (TextIO.stdErr, |
| 46 |
"ClutTeX: Process TeX files without cluttering your working directory\n\ |
| 47 |
\\n\ |
| 48 |
\Usage:\n\ |
| 49 |
\ " ^ progName ^ " [options] [--] FILE.tex\n\ |
| 50 |
\\n\ |
| 51 |
\Options:\n\ |
| 52 |
\ -e, --engine=ENGINE Specify which TeX engine to use.\n\ |
| 53 |
\ ENGINE is one of the following:\n\ |
| 54 |
\ pdflatex, pdftex,\n\ |
| 55 |
\ lualatex, luatex, luajittex,\n\ |
| 56 |
\ xelatex, xetex, latex, etex, tex,\n\ |
| 57 |
\ platex, eptex, ptex,\n\ |
| 58 |
\ uplatex, euptex, uptex,\n\ |
| 59 |
\ --engine-executable=COMMAND+OPTIONs\n\ |
| 60 |
\ The actual TeX command to use.\n\ |
| 61 |
\ [default: ENGINE]\n\ |
| 62 |
\ -o, --output=FILE The name of output file.\n\ |
| 63 |
\ [default: JOBNAME.pdf or JOBNAME.dvi]\n\ |
| 64 |
\ --fresh Clean intermediate files before running TeX.\n\ |
| 65 |
\ Cannot be used with --output-directory.\n\ |
| 66 |
\ --max-iterations=N Maximum number of running TeX to resolve\n\ |
| 67 |
\ cross-references. [default: 3]\n\ |
| 68 |
\ --start-with-draft Start with draft mode.\n\ |
| 69 |
\ --[no-]change-directory Change directory before running TeX.\n\ |
| 70 |
\ --watch[=ENGINE] Watch input files for change. Requires fswatch\n\ |
| 71 |
\ or inotifywait to be installed. ENGINE is one of\n\ |
| 72 |
\ `fswatch', `inotifywait' or `auto' [default: `auto']\n\ |
| 73 |
\ --tex-option=OPTION Pass OPTION to TeX as a single option.\n\ |
| 74 |
\ --tex-options=OPTIONs Pass OPTIONs to TeX as multiple options.\n\ |
| 75 |
\ --dvipdfmx-option[s]=OPTION[s] Same for dvipdfmx.\n\ |
| 76 |
\ --makeindex=COMMAND+OPTIONs Command to generate index, such as\n\ |
| 77 |
\ `makeindex' or `mendex'.\n\ |
| 78 |
\ --bibtex=COMMAND+OPTIONs Command for BibTeX, such as\n\ |
| 79 |
\ `bibtex' or `pbibtex'.\n\ |
| 80 |
\ --biber[=COMMAND+OPTIONs] Command for Biber.\n\ |
| 81 |
\ --makeglossaries[=COMMAND+OPTIONs] Command for makeglossaries.\n\ |
| 82 |
\ -h, --help Print this message and exit.\n\ |
| 83 |
\ -v, --version Print version information and exit.\n\ |
| 84 |
\ -V, --verbose Be more verbose.\n\ |
| 85 |
\ --color[=WHEN] Make ClutTeX's message colorful. WHEN is one of\n\ |
| 86 |
\ `always', `auto', or `never'.\n\ |
| 87 |
\ [default: `auto' if --color is omitted,\n\ |
| 88 |
\ `always' if WHEN is omitted]\n\ |
| 89 |
\ --includeonly=NAMEs Insert '\\includeonly{NAMEs}'.\n\ |
| 90 |
\ --make-depends=FILE Write dependencies as a Makefile rule.\n\ |
| 91 |
\ --print-output-directory Print the output directory and exit.\n\ |
| 92 |
\ --package-support=PKG1[,PKG2,...]\n\ |
| 93 |
\ Enable special support for some shell-escaping\n\ |
| 94 |
\ packages.\n\ |
| 95 |
\ Currently supported: minted, epstopdf\n\ |
| 96 |
\ --check-driver=DRIVER Check that the correct driver file is loaded.\n\ |
| 97 |
\ DRIVER is one of `dvipdfmx', `dvips', `dvisvgm'.\n\ |
| 98 |
\ --source-date-epoch=TIME\n\ |
| 99 |
\ Set SOURCE_DATE_EPOCH variable.\n\ |
| 100 |
\ TIME is `now' or an integer.\n\ |
| 101 |
\\n\ |
| 102 |
\ --[no-]shell-escape\n\ |
| 103 |
\ --shell-restricted\n\ |
| 104 |
\ --synctex=NUMBER\n\ |
| 105 |
\ --fmt=FMTNAME\n\ |
| 106 |
\ --[no-]file-line-error [default: yes]\n\ |
| 107 |
\ --[no-]halt-on-error [default: yes]\n\ |
| 108 |
\ --interaction=STRING [default: nonstopmode]\n\ |
| 109 |
\ --jobname=STRING\n\ |
| 110 |
\ --output-directory=DIR [default: somewhere in the temporary directory]\n\ |
| 111 |
\ --output-format=FORMAT FORMAT is `pdf' or `dvi'. [default: pdf]\n\ |
| 112 |
\\n" ^ COPYRIGHT_NOTICE) |
| 113 |
; OS.Process.exit OS.Process.success |
| 114 |
end |
| 115 |
|
| 116 |
structure HandleOptions = HandleOptions (fun showMessageAndFail message = (TextIO.output (TextIO.stdErr, message ^ "\n"); OS.Process.exit OS.Process.failure) |
| 117 |
val showUsage = showUsage |
| 118 |
fun showVersion () = (TextIO.output (TextIO.stdErr, "cluttex " ^ CLUTTEX_VERSION ^ "\n"); OS.Process.exit OS.Process.success) |
| 119 |
) |
| 120 |
|
| 121 |
(*: val pathInOutputDirectory : AppOptions.options * string -> string *) |
| 122 |
fun pathInOutputDirectory (options : AppOptions.options, ext) = PathUtil.join2 (#output_directory options, #jobname options ^ "." ^ ext) |
| 123 |
(*: val executeCommand : string * (unit -> bool) option -> unit *) |
| 124 |
(* |
| 125 |
fun executeCommand (command, recover) |
| 126 |
= let val () = Message.exec command |
| 127 |
val status = OS.Process.system command |
| 128 |
val success_or_recoverd = if OS.Process.isSuccess status then |
| 129 |
true |
| 130 |
else |
| 131 |
case recover of |
| 132 |
SOME f => f () |
| 133 |
| NONE => false |
| 134 |
in if success_or_recoverd then |
| 135 |
() |
| 136 |
else |
| 137 |
( Message.error "Command exited abnormally" (* TODO: show status code: Unix.fromStatus *) |
| 138 |
; raise Abort |
| 139 |
) |
| 140 |
end |
| 141 |
*) |
| 142 |
fun executeCommand (command, recover) |
| 143 |
= let val () = Message.exec command |
| 144 |
val (success, termination, status_or_signal) = Lua.call3 Lua.Lib.os.execute #[Lua.fromString command] |
| 145 |
val (success, termination, status_or_signal) : bool * string option * Lua.value |
| 146 |
= if Lua.typeof success = "number" then (* Lua 5.1 or LuaTeX *) |
| 147 |
(Lua.== (success, Lua.fromInt 0), NONE, success) |
| 148 |
else |
| 149 |
(Lua.unsafeFromValue success, SOME (Lua.unsafeFromValue termination), status_or_signal) |
| 150 |
val success_or_recovered = success orelse (case recover of |
| 151 |
SOME f => f () |
| 152 |
| NONE => false |
| 153 |
) |
| 154 |
in if success_or_recovered then |
| 155 |
() |
| 156 |
else |
| 157 |
( case termination of |
| 158 |
SOME "exit" => Message.error ("Command exited abnormally: exit status " ^ Lua.unsafeFromValue (Lua.call1 Lua.Lib.tostring #[status_or_signal])) |
| 159 |
| SOME "signal" => Message.error ("Command exited abnormally: signal " ^ Lua.unsafeFromValue (Lua.call1 Lua.Lib.tostring #[status_or_signal])) |
| 160 |
| _ => Message.error ("Command exited abnormally: " ^ Lua.unsafeFromValue (Lua.call1 Lua.Lib.tostring #[status_or_signal])) |
| 161 |
; raise Abort |
| 162 |
) |
| 163 |
end |
| 164 |
|
| 165 |
(* The value to be used for SOURCE_DATE_EPOCH *) |
| 166 |
fun getTimeSinceEpoch () : string |
| 167 |
= Lua.unsafeFromValue (Lua.call1 Lua.Lib.tostring #[Lua.call1 Lua.Lib.os.time #[]]) |
| 168 |
|
| 169 |
type run_params = { options : AppOptions.options |
| 170 |
, inputfile : string |
| 171 |
, engine : TeXEngine.engine |
| 172 |
, tex_options : TeXEngine.run_options |
| 173 |
, recorderfile : string |
| 174 |
, recorderfile2 : string |
| 175 |
, original_wd : string |
| 176 |
, output_extension : string |
| 177 |
, source_date_epoch_info : { time_since_epoch : string, time : Time.time } ref option |
| 178 |
} |
| 179 |
datatype single_run_result = SHOULD_RERUN of Reruncheck.aux_status StringMap.map |
| 180 |
| NO_NEED_TO_RERUN |
| 181 |
| NO_PAGES_OF_OUTPUT |
| 182 |
|
| 183 |
(* Run TeX command ( *tex, *latex) *) |
| 184 |
(*: val singleRun : run_params * Reruncheck.aux_status StringMap.map * int -> single_run_result *) |
| 185 |
fun singleRun ({ options, inputfile, engine, tex_options, recorderfile, recorderfile2, original_wd, source_date_epoch_info, ... } : run_params, auxstatus, iteration) |
| 186 |
= let val mainauxfile = pathInOutputDirectory (options, "aux") |
| 187 |
val { filelist, auxstatus, minted, epstopdf, pdfx, bibtex_aux_hash } |
| 188 |
= if FSUtil.isFile recorderfile then |
| 189 |
(* Recorder file already exists *) |
| 190 |
let val recorded = Reruncheck.parseRecorderFile { file = recorderfile, options = options } |
| 191 |
val recorded = if TeXEngine.isLuaTeX engine andalso FSUtil.isFile recorderfile2 then |
| 192 |
Reruncheck.parseRecorderFileContinued { file = recorderfile2, options = options, previousResult = recorded } |
| 193 |
else |
| 194 |
recorded |
| 195 |
val (filelist, filemap) = Reruncheck.getFileInfo recorded |
| 196 |
val auxstatus = Reruncheck.collectFileInfo (filelist, auxstatus) |
| 197 |
val { minted, epstopdf, pdfx } = |
| 198 |
List.foldl (fn ({ path, ... }, { minted, epstopdf, pdfx }) => |
| 199 |
{ minted = minted orelse String.isSuffix "minted/minted.sty" path |
| 200 |
, epstopdf = epstopdf orelse String.isSuffix "epstopdf.sty" path |
| 201 |
, pdfx = pdfx orelse String.isSuffix "pdfx.sty" path |
| 202 |
} |
| 203 |
) { minted = false, epstopdf = false, pdfx = false } filelist |
| 204 |
val bibtex_aux_hash = case #bibtex_or_biber options of |
| 205 |
SOME (AppOptions.BIBTEX _) => |
| 206 |
let val biblines = AuxFile.extractBibTeXLines { auxfile = mainauxfile, outdir = #output_directory options } |
| 207 |
in SOME (MD5.compute (Byte.stringToBytes (String.concatWith "\n" biblines))) |
| 208 |
end |
| 209 |
| _ => NONE |
| 210 |
in { filelist, auxstatus, minted, epstopdf, pdfx, bibtex_aux_hash } |
| 211 |
end |
| 212 |
else |
| 213 |
(* This is the first execution *) |
| 214 |
if StringMap.isEmpty auxstatus then |
| 215 |
{ filelist = [], auxstatus = StringMap.empty, minted = false, epstopdf = false, pdfx = false, bibtex_aux_hash = NONE } |
| 216 |
else |
| 217 |
( Message.error "Recorder file was not generated during the execution!" |
| 218 |
; raise Abort |
| 219 |
) |
| 220 |
|
| 221 |
(* |
| 222 |
* Set SOURCE_DATE_EPOCH if |
| 223 |
* * --source-date-epoch=now is set, or |
| 224 |
* * --source-date-epoch is not set but `pdfx' package is used and SOURCE_DATE_EPOCH is not already set. |
| 225 |
* The value will be the newer of these: |
| 226 |
* * The time when the program started (see main()). |
| 227 |
* * The time we are processing after one of the input files was modified. |
| 228 |
*) |
| 229 |
val () = case source_date_epoch_info of |
| 230 |
NONE => () (* already set in main () *) |
| 231 |
| SOME r => |
| 232 |
let val should_set_source_date_epoch = case #source_date_epoch options of |
| 233 |
SOME AppOptions.SourceDateEpoch.NOW => true |
| 234 |
| SOME (AppOptions.SourceDateEpoch.RAW _) => false (* should not occur *) |
| 235 |
| NONE => pdfx orelse #pdfx (#package_support options) |
| 236 |
in if should_set_source_date_epoch then |
| 237 |
let val input_time = List.foldl (fn ({ abspath, kind = Reruncheck.INPUT, ... }, acc) => |
| 238 |
(case StringMap.find (auxstatus, abspath) of |
| 239 |
SOME { mtime, ... } => |
| 240 |
(case (mtime, acc) of |
| 241 |
(SOME mtime', SOME t) => |
| 242 |
if Time.< (t, mtime') then |
| 243 |
mtime |
| 244 |
else |
| 245 |
acc |
| 246 |
| (NONE, _) => acc |
| 247 |
| (_, NONE) => mtime |
| 248 |
) |
| 249 |
| NONE => acc |
| 250 |
) |
| 251 |
| (_, acc) => acc |
| 252 |
) NONE filelist |
| 253 |
val info = case input_time of |
| 254 |
SOME input_time => if Time.< (#time (!r), input_time) then (* input file was changed since the last run *) |
| 255 |
let val new_info = { time_since_epoch = getTimeSinceEpoch (), time = input_time } |
| 256 |
in if Message.getVerbosity () >= 1 then |
| 257 |
Message.info "Input file was modified; Updating SOURCE_DATE_EPOCH..." |
| 258 |
else |
| 259 |
() |
| 260 |
; r := new_info |
| 261 |
; new_info |
| 262 |
end |
| 263 |
else |
| 264 |
!r |
| 265 |
| NONE => !r |
| 266 |
in if Message.getVerbosity () >= 1 then |
| 267 |
Message.info ("Setting SOURCE_DATE_EPOCH to " ^ #time_since_epoch info) |
| 268 |
else |
| 269 |
() |
| 270 |
; OSUtil.setEnv ("SOURCE_DATE_EPOCH", #time_since_epoch info) |
| 271 |
end |
| 272 |
else |
| 273 |
() |
| 274 |
end |
| 275 |
|
| 276 |
val tex_injection = case #includeonly options of |
| 277 |
SOME io => "\\includeonly{" ^ io ^ "}" |
| 278 |
| NONE => "" |
| 279 |
val tex_injection = if minted orelse #minted (#package_support options) then |
| 280 |
let val () = if not (#minted (#package_support options)) then |
| 281 |
Message.diag "You may want to use --package-support=minted option." |
| 282 |
else |
| 283 |
() |
| 284 |
val outdir = #output_directory options |
| 285 |
val outdir = if OSUtil.isWindows then |
| 286 |
String.map (fn #"\\" => #"/" | c => c) outdir (* Use forward slashes *) |
| 287 |
else |
| 288 |
outdir |
| 289 |
in tex_injection ^ "\\PassOptionsToPackage{outputdir=" ^ outdir ^ "}{minted}" |
| 290 |
end |
| 291 |
else |
| 292 |
tex_injection |
| 293 |
val tex_injection = if epstopdf orelse #epstopdf (#package_support options) then |
| 294 |
let val () = if not (#epstopdf (#package_support options)) then |
| 295 |
Message.diag "You may want to use --package-support=epstopdf option." |
| 296 |
else |
| 297 |
() |
| 298 |
val outdir = #output_directory options |
| 299 |
val outdir = if OSUtil.isWindows then |
| 300 |
String.map (fn #"\\" => #"/" | c => c) outdir (* Use forward slashes *) |
| 301 |
else |
| 302 |
outdir |
| 303 |
val outdir = if String.isSuffix "/" outdir then |
| 304 |
outdir |
| 305 |
else |
| 306 |
outdir ^ "/" (* Must end with a directory separator *) |
| 307 |
in tex_injection ^ "\\PassOptionsToPackage{outdir=" ^ outdir ^ "}{epstopdf}" |
| 308 |
end |
| 309 |
else |
| 310 |
tex_injection |
| 311 |
val inputline = tex_injection ^ SafeName.safeInput { name = inputfile, isPdfTeX = TeXEngine.isPdfTeX engine } |
| 312 |
val (current_tex_options, lightweight_mode) |
| 313 |
= if iteration = 1 andalso #start_with_draft options then |
| 314 |
if #supports_draftmode engine then |
| 315 |
({ tex_options where draftmode = true, interaction = SOME InteractionMode.BATCHMODE }, true) |
| 316 |
else |
| 317 |
({ tex_options where interaction = SOME InteractionMode.BATCHMODE }, true) |
| 318 |
else |
| 319 |
({ tex_options where draftmode = false }, false) |
| 320 |
val command = TeXEngine.buildCommand (engine, inputline, current_tex_options) |
| 321 |
val execlogCache = ref NONE |
| 322 |
fun getExecLog () = case !execlogCache of |
| 323 |
NONE => let val ins = TextIO.openIn (pathInOutputDirectory (options, "log")) |
| 324 |
val log = TextIO.inputAll ins |
| 325 |
val () = TextIO.closeIn ins |
| 326 |
in execlogCache := SOME log |
| 327 |
; log |
| 328 |
end |
| 329 |
| SOME log => log |
| 330 |
val recovered = ref false |
| 331 |
fun recover () = let val execlog = getExecLog () |
| 332 |
val r = Recovery.tryRecovery { options = options, execlog = execlog, auxfile = pathInOutputDirectory (options, "aux"), originalWorkingDirectory = original_wd } |
| 333 |
in recovered := true |
| 334 |
; r |
| 335 |
end |
| 336 |
val () = executeCommand (command, SOME recover) |
| 337 |
in if !recovered then |
| 338 |
SHOULD_RERUN StringMap.empty |
| 339 |
else |
| 340 |
let val recorded = Reruncheck.parseRecorderFile { file = recorderfile, options = options } |
| 341 |
val recorded = if TeXEngine.isLuaTeX engine andalso FSUtil.isFile recorderfile2 then |
| 342 |
Reruncheck.parseRecorderFileContinued { file = recorderfile2, options = options, previousResult = recorded } |
| 343 |
else |
| 344 |
recorded |
| 345 |
val (filelist, filemap) = Reruncheck.getFileInfo recorded |
| 346 |
val execlog = getExecLog () |
| 347 |
|
| 348 |
(* Check driver *) |
| 349 |
val () = case #check_driver options of |
| 350 |
NONE => () |
| 351 |
| SOME driver => CheckDriver.checkDriver (driver, List.map (fn { path, abspath, kind } => { path = path, kind = case kind of Reruncheck.INPUT => "input" | Reruncheck.OUTPUT => "output" | Reruncheck.AUXILIARY => "auxiliary"}) filelist) |
| 352 |
|
| 353 |
(* makeindex *) |
| 354 |
val filelist = case #makeindex options of |
| 355 |
NONE => (* Check log file *) |
| 356 |
( if Lua.isFalsy (Lua.call1 Lua.Lib.string.find #[Lua.fromString execlog, Lua.fromString "No file [^\n]+%.ind%."]) then |
| 357 |
() |
| 358 |
else |
| 359 |
Message.diag "You may want to use --makeindex option." |
| 360 |
; filelist |
| 361 |
) |
| 362 |
| SOME makeindex => |
| 363 |
let fun go (file, filelist_acc) (* Look for .idx files and run MakeIndex *) |
| 364 |
= if PathUtil.ext (#path file) = "idx" then |
| 365 |
(* Run makeindex if the .idx file is new or updated *) |
| 366 |
let val idxfileinfo = { path = #path file, abspath = #abspath file, kind = Reruncheck.AUXILIARY } |
| 367 |
val output_ind = PathUtil.replaceext { path = #abspath file, newext = "ind" } |
| 368 |
in if #1 (Reruncheck.compareFileInfo ([idxfileinfo], auxstatus)) orelse Reruncheck.compareFileTime { srcAbs = #abspath file, dst = output_ind, auxstatus = auxstatus } then |
| 369 |
let val idx_dir = PathUtil.dirname (#abspath file) |
| 370 |
val makeindex_command = [ |
| 371 |
"cd", ShellUtil.escape idx_dir, "&&", |
| 372 |
makeindex, (* Do not escape `makeindex` to allow additional options *) |
| 373 |
"-o", PathUtil.basename output_ind, |
| 374 |
PathUtil.basename (#abspath file) |
| 375 |
] |
| 376 |
in executeCommand (String.concatWith " " makeindex_command, NONE) |
| 377 |
; { path = output_ind, abspath = output_ind, kind = Reruncheck.AUXILIARY } :: filelist_acc |
| 378 |
end |
| 379 |
else |
| 380 |
( FSUtil.touch output_ind handle Lua.Error err => Message.warn ("Failed to touch " ^ output_ind ^ " (" ^ Lua.unsafeFromValue err ^ ")") |
| 381 |
; filelist_acc |
| 382 |
) |
| 383 |
end |
| 384 |
else |
| 385 |
filelist_acc |
| 386 |
in List.foldl go filelist filelist |
| 387 |
end |
| 388 |
|
| 389 |
(* makeglossaries *) |
| 390 |
val filelist = case #makeglossaries options of |
| 391 |
NONE => (* Check log file *) |
| 392 |
( if Lua.isFalsy (Lua.call1 Lua.Lib.string.find #[Lua.fromString execlog, Lua.fromString "No file [^\n]+%.gls%."]) then |
| 393 |
() |
| 394 |
else |
| 395 |
Message.diag "You may want to use --makeglossaries option." |
| 396 |
; filelist |
| 397 |
) |
| 398 |
| SOME makeglossaries => |
| 399 |
let fun go (file, filelist_acc) (* Look for .glo files and run makeglossaries *) |
| 400 |
= if PathUtil.ext (#path file) = "glo" then |
| 401 |
(* Run makeglossaries if the .glo file is new or updated *) |
| 402 |
let val glofileinfo = { path = #path file, abspath = #abspath file, kind = Reruncheck.AUXILIARY } |
| 403 |
val output_gls = PathUtil.replaceext { path = #abspath file, newext = "gls" } |
| 404 |
in if #1 (Reruncheck.compareFileInfo ([glofileinfo], auxstatus)) orelse Reruncheck.compareFileTime { srcAbs = #abspath file, dst = output_gls, auxstatus = auxstatus } then |
| 405 |
let val makeglossaries_command = [ |
| 406 |
makeglossaries, |
| 407 |
"-d", ShellUtil.escape (#output_directory options), |
| 408 |
PathUtil.trimext (PathUtil.basename (#path file)) |
| 409 |
] |
| 410 |
in executeCommand (String.concatWith " " makeglossaries_command, NONE) |
| 411 |
; { path = output_gls, abspath = output_gls, kind = Reruncheck.AUXILIARY } :: filelist_acc |
| 412 |
end |
| 413 |
else |
| 414 |
( FSUtil.touch output_gls handle Lua.Error err => Message.warn ("Failed to touch " ^ output_gls ^ " (" ^ Lua.unsafeFromValue err ^ ")") |
| 415 |
; filelist_acc |
| 416 |
) |
| 417 |
end |
| 418 |
else |
| 419 |
filelist_acc |
| 420 |
in List.foldl go filelist filelist |
| 421 |
end |
| 422 |
|
| 423 |
(* bibtex/biber *) |
| 424 |
val filelist = case #bibtex_or_biber options of |
| 425 |
NONE => ( if Lua.isFalsy (Lua.call1 Lua.Lib.string.find #[Lua.fromString execlog, Lua.fromString "No file [^\n]+%.bbl%."]) then |
| 426 |
() |
| 427 |
else |
| 428 |
Message.diag "You may want to use --bibtex or biber option." |
| 429 |
; filelist |
| 430 |
) |
| 431 |
| SOME (AppOptions.BIBTEX bibtex) => |
| 432 |
let val biblines2 = AuxFile.extractBibTeXLines { auxfile = mainauxfile, outdir = #output_directory options } |
| 433 |
val bibtex_aux_hash2 = if List.null biblines2 then |
| 434 |
NONE |
| 435 |
else |
| 436 |
SOME (MD5.compute (Byte.stringToBytes (String.concatWith "\n" biblines2))) |
| 437 |
val output_bbl = pathInOutputDirectory (options, "bbl") |
| 438 |
in if bibtex_aux_hash <> bibtex_aux_hash2 orelse Reruncheck.compareFileTime { srcAbs = PathUtil.abspath { path = mainauxfile, cwd = NONE }, dst = output_bbl, auxstatus = auxstatus } then |
| 439 |
(* The input for BibTeX command has changed... *) |
| 440 |
let val bibtex_command = [ |
| 441 |
"cd", ShellUtil.escape (#output_directory options), "&&", |
| 442 |
bibtex, |
| 443 |
PathUtil.basename mainauxfile |
| 444 |
] |
| 445 |
in executeCommand (String.concatWith " " bibtex_command, NONE) |
| 446 |
end |
| 447 |
else |
| 448 |
( if Message.getVerbosity () >= 1 then |
| 449 |
Message.info "No need to run BibTeX." |
| 450 |
else |
| 451 |
() |
| 452 |
; FSUtil.touch output_bbl handle Lua.Error err => Message.warn ("Failed to touch " ^ output_bbl ^ " (" ^ Lua.unsafeFromValue err ^ ")") |
| 453 |
) |
| 454 |
; filelist |
| 455 |
end |
| 456 |
| SOME (AppOptions.BIBER biber) => |
| 457 |
let fun go (file, filelist_acc) |
| 458 |
(* Usual compilation with biber |
| 459 |
* tex -> pdflatex tex -> aux,bcf,pdf,run.xml |
| 460 |
* bcf -> biber bcf -> bbl |
| 461 |
* tex,bbl -> pdflatex tex -> aux,bcf,pdf,run.xml |
| 462 |
*) |
| 463 |
= if PathUtil.ext (#path file) = "bcf" then |
| 464 |
(* Run biber if the .bcf file is new or updated *) |
| 465 |
let val bcffileinfo = { path = #path file, abspath = #abspath file, kind = Reruncheck.AUXILIARY } |
| 466 |
val output_bbl = PathUtil.replaceext { path = #abspath file, newext = "bbl" } |
| 467 |
fun check_bib_update abspath |
| 468 |
= let val ins = TextIO.openIn abspath |
| 469 |
fun go updated_dot_bib |
| 470 |
= case TextIO.inputLine ins of |
| 471 |
NONE => updated_dot_bib |
| 472 |
| SOME l => |
| 473 |
let val bib = Lua.call1 Lua.Lib.string.match #[Lua.fromString l, Lua.fromString "<bcf:datasource .*>(.*)</bcf:datasource>"] |
| 474 |
in if Lua.isFalsy bib then |
| 475 |
go updated_dot_bib (* continue *) |
| 476 |
else |
| 477 |
let val bib = Lua.unsafeFromValue bib : string |
| 478 |
val bibfile = PathUtil.join2 (original_wd, bib) |
| 479 |
val updated_dot_bib = if FSUtil.isFile bibfile then |
| 480 |
let val updated_dot_bib_tmp = not (Reruncheck.compareFileTime { srcAbs = PathUtil.abspath { path = mainauxfile, cwd = NONE }, dst = bibfile, auxstatus = auxstatus }) |
| 481 |
in if updated_dot_bib_tmp then |
| 482 |
Message.info (bibfile ^ " is newer than aux") |
| 483 |
else |
| 484 |
() |
| 485 |
; updated_dot_bib orelse updated_dot_bib_tmp |
| 486 |
end |
| 487 |
else |
| 488 |
( Message.warn (bibfile ^ " is not accessible") |
| 489 |
; updated_dot_bib |
| 490 |
) |
| 491 |
in go updated_dot_bib |
| 492 |
end |
| 493 |
end |
| 494 |
in go false before TextIO.closeIn ins |
| 495 |
end |
| 496 |
val updated_dot_bib = check_bib_update (#abspath file) |
| 497 |
in if updated_dot_bib orelse #1 (Reruncheck.compareFileInfo ([bcffileinfo], auxstatus)) orelse Reruncheck.compareFileTime { srcAbs = #abspath file, dst = output_bbl, auxstatus = auxstatus } then |
| 498 |
let val biber_command = [ |
| 499 |
biber, (* Do not escape `biber` to allow additional options *) |
| 500 |
"--output-directory", ShellUtil.escape (#output_directory options), |
| 501 |
PathUtil.basename (#abspath file) |
| 502 |
] |
| 503 |
in executeCommand (String.concatWith " " biber_command, NONE) |
| 504 |
; { path = output_bbl, abspath = output_bbl, kind = Reruncheck.AUXILIARY } :: filelist |
| 505 |
end |
| 506 |
else |
| 507 |
( FSUtil.touch output_bbl handle Lua.Error err => Message.warn ("Failed to touch " ^ output_bbl ^ " (" ^ Lua.unsafeFromValue err ^ ")") |
| 508 |
; filelist_acc |
| 509 |
) |
| 510 |
end |
| 511 |
else |
| 512 |
filelist_acc |
| 513 |
in List.foldl go filelist filelist |
| 514 |
end |
| 515 |
|
| 516 |
in if String.isSubstring "No pages of output." execlog then |
| 517 |
NO_PAGES_OF_OUTPUT |
| 518 |
else |
| 519 |
let val (should_rerun, auxstatus) = Reruncheck.compareFileInfo (filelist, auxstatus) |
| 520 |
in if should_rerun orelse lightweight_mode then |
| 521 |
SHOULD_RERUN auxstatus |
| 522 |
else |
| 523 |
NO_NEED_TO_RERUN |
| 524 |
end |
| 525 |
end |
| 526 |
end |
| 527 |
|
| 528 |
(* Run (La)TeX (possibly multiple times) and produce a PDF/DVI file. *) |
| 529 |
(*: val doTypeset : run_params -> unit *) |
| 530 |
fun doTypeset (run_params as { options, engine, output_extension, recorderfile, recorderfile2, source_date_epoch_info, ... } : run_params) |
| 531 |
= let fun loop (iteration, auxstatus) |
| 532 |
= let val iteration = iteration + 1 |
| 533 |
in case singleRun (run_params, auxstatus, iteration) of |
| 534 |
NO_PAGES_OF_OUTPUT => ( Message.warn "No pages of output." |
| 535 |
; false |
| 536 |
) |
| 537 |
| NO_NEED_TO_RERUN => true |
| 538 |
| SHOULD_RERUN auxstatus => if iteration >= #max_iterations options then |
| 539 |
( Message.warn "LaTeX should be run once more." |
| 540 |
; true |
| 541 |
) |
| 542 |
else |
| 543 |
loop (iteration, auxstatus) |
| 544 |
end |
| 545 |
in if loop (0, StringMap.empty) then |
| 546 |
(* Successful *) |
| 547 |
( if #output_format options = OutputFormat.DVI orelse #supports_pdf_generation engine then |
| 548 |
(* Output file (DVI/PDF) is generated in the output directory *) |
| 549 |
let val outfile = pathInOutputDirectory (options, output_extension) |
| 550 |
val onCopyError = if OSUtil.isWindows then |
| 551 |
SOME (fn () => let val output_format = case #output_format options of |
| 552 |
OutputFormat.DVI => "DVI" |
| 553 |
| OutputFormat.PDF => "PDF" |
| 554 |
in Message.error ("Failed to copy file. Some applications may be locking the " ^ output_format ^ " file.") |
| 555 |
; false |
| 556 |
end |
| 557 |
) |
| 558 |
else |
| 559 |
NONE |
| 560 |
in executeCommand (FSUtil.copyCommand { from = outfile, to = #output options }, onCopyError) |
| 561 |
; if List.null (#dvipdfmx_extraoptions options) then |
| 562 |
() |
| 563 |
else |
| 564 |
Message.warn "--dvipdfmx-option[s] are ignored." |
| 565 |
end |
| 566 |
else |
| 567 |
(* DVI file is generated, but PDF file is wanted *) |
| 568 |
let val dvifile = pathInOutputDirectory (options, "dvi") |
| 569 |
val dvipdfmx_command = "dvipdfmx" :: "-o" :: ShellUtil.escape (#output options) :: #dvipdfmx_extraoptions options @ [ShellUtil.escape dvifile] |
| 570 |
in executeCommand (String.concatWith " " dvipdfmx_command, NONE) |
| 571 |
end |
| 572 |
; (* Copy SyncTeX file if necessary *) |
| 573 |
if #output_format options = OutputFormat.PDF then |
| 574 |
let val synctex = Lua.unsafeFromValue (Lua.call1 Lua.Lib.tonumber #[Lua.fromString (Option.getOpt (#synctex options, "0"))]) : int |
| 575 |
val synctex_ext = if synctex > 0 then |
| 576 |
(* Compressed SyncTeX file (.synctex.gz) *) |
| 577 |
SOME "synctex.gz" |
| 578 |
else if synctex < 0 then |
| 579 |
(* Uncompressed SyncTeX file (.synctex) *) |
| 580 |
SOME "synctex" |
| 581 |
else |
| 582 |
NONE |
| 583 |
in case synctex_ext of |
| 584 |
SOME ext => executeCommand (FSUtil.copyCommand { from = pathInOutputDirectory (options, ext), to = PathUtil.replaceext { path = #output options, newext = ext } }, NONE) |
| 585 |
| NONE => () |
| 586 |
end |
| 587 |
else |
| 588 |
() |
| 589 |
; (* Write dependencies file *) |
| 590 |
case #make_depends options of |
| 591 |
SOME make_depends => |
| 592 |
let val recorded = Reruncheck.parseRecorderFile { file = recorderfile, options = options } |
| 593 |
val recorded = if TeXEngine.isLuaTeX engine andalso FSUtil.isFile recorderfile2 then |
| 594 |
Reruncheck.parseRecorderFileContinued { file = recorderfile2, options = options, previousResult = recorded } |
| 595 |
else |
| 596 |
recorded |
| 597 |
val (filelist, _) = Reruncheck.getFileInfo recorded |
| 598 |
val outs = TextIO.openOut make_depends |
| 599 |
in TextIO.output (outs, #output options ^ ":") (* TODO: quote *) |
| 600 |
; List.app (fn { path, abspath = _, kind = Reruncheck.INPUT } => TextIO.output (outs, " " ^ path) (* TODO: quote *) |
| 601 |
| _ => ()) filelist |
| 602 |
; TextIO.output (outs, "\n") |
| 603 |
; TextIO.closeOut outs |
| 604 |
end |
| 605 |
| NONE => () |
| 606 |
; (* Successful *) |
| 607 |
if Message.getVerbosity () >= 1 then |
| 608 |
Message.info "Command exited successfully" |
| 609 |
else |
| 610 |
() |
| 611 |
) |
| 612 |
else |
| 613 |
(* No pages of output. *) |
| 614 |
() |
| 615 |
end |
| 616 |
|
| 617 |
(*: val doWatchWindows : Lua.value -> string list -> bool *) |
| 618 |
fun doWatchWindows fswatcherlib files |
| 619 |
= let val watcher = Lua.call1 Lua.Lib.assert (Lua.call (Lua.field (fswatcherlib, "new")) #[]) |
| 620 |
val () = List.app (fn file => Lua.call0 Lua.Lib.assert (Lua.method (watcher, "add") #[Lua.fromString file])) files |
| 621 |
val result = Lua.call1 Lua.Lib.assert (Lua.method (watcher, "next") #[]) |
| 622 |
val () = if Message.getVerbosity () >= 2 then |
| 623 |
Message.info (Lua.unsafeFromValue (Lua.field (result, "action")) ^ " " ^ Lua.unsafeFromValue (Lua.field (result, "path"))) |
| 624 |
else |
| 625 |
() |
| 626 |
val () = Lua.method0 (watcher, "close") #[] |
| 627 |
in true |
| 628 |
end |
| 629 |
|
| 630 |
(*: val doWatchFswatch : string list -> bool *) |
| 631 |
fun doWatchFswatch files |
| 632 |
= let val fswatch_command = "fswatch" :: "--one-event" :: "--event=Updated" :: "--" :: List.map ShellUtil.escape files |
| 633 |
val fswatch_command_str = String.concatWith " " fswatch_command |
| 634 |
val () = if Message.getVerbosity () >= 1 then |
| 635 |
Message.exec fswatch_command_str |
| 636 |
else |
| 637 |
() |
| 638 |
val fswatch = Lua.call1 Lua.Lib.assert (Lua.call Lua.Lib.io.popen #[Lua.fromString fswatch_command_str, Lua.fromString "r"]) |
| 639 |
val readLine = Lua.method1 (fswatch, "lines") #[] |
| 640 |
fun go () = let val l = Lua.call1 readLine #[] |
| 641 |
in if Lua.isFalsy l then |
| 642 |
false |
| 643 |
else if List.exists (fn path => Lua.unsafeFromValue l = path) files then |
| 644 |
true |
| 645 |
else |
| 646 |
go () |
| 647 |
end |
| 648 |
in go () before Lua.method0 (fswatch, "close") #[] |
| 649 |
end |
| 650 |
|
| 651 |
(*: val doWatchInotifywait : string list -> bool *) |
| 652 |
fun doWatchInotifywait files |
| 653 |
= let val inotifywait_command = "inotifywait" :: "--event=modify" :: "--event=attrib" :: "--format=%w" :: "--quiet" :: List.map ShellUtil.escape files |
| 654 |
val inotifywait_command_str = String.concatWith " " inotifywait_command |
| 655 |
val () = if Message.getVerbosity () >= 1 then |
| 656 |
Message.exec inotifywait_command_str |
| 657 |
else |
| 658 |
() |
| 659 |
val inotifywait = Lua.call1 Lua.Lib.assert (Lua.call Lua.Lib.io.popen #[Lua.fromString inotifywait_command_str, Lua.fromString "r"]) |
| 660 |
val readLine = Lua.method1 (inotifywait, "lines") #[] |
| 661 |
fun go () = let val l = Lua.call1 readLine #[] |
| 662 |
in if Lua.isFalsy l then |
| 663 |
false |
| 664 |
else if List.exists (fn path => Lua.unsafeFromValue l = path) files then |
| 665 |
true |
| 666 |
else |
| 667 |
go () |
| 668 |
end |
| 669 |
in go () before Lua.method0 (inotifywait, "close") #[] |
| 670 |
end |
| 671 |
|
| 672 |
(*: val runWatchMode : AppOptions.WatchEngine.engine * run_params -> unit *) |
| 673 |
fun runWatchMode (watch_engine, run_params as { options, engine, recorderfile, recorderfile2, ... } : run_params) |
| 674 |
= let val fswatcherlib = if OSUtil.isWindows then |
| 675 |
(* Windows: Try built-in filesystem watcher *) |
| 676 |
let val (succ, result) = Lua.call2 Lua.Lib.pcall #[Lua.Lib.require, Lua.fromString "texrunner.fswatcher_windows"] |
| 677 |
in if Lua.isFalsy succ then |
| 678 |
( if Message.getVerbosity () >= 1 then |
| 679 |
Message.warn ("Failed to load texrunner.fswatcher_windows: " ^ Lua.unsafeFromValue result) |
| 680 |
else |
| 681 |
() |
| 682 |
; NONE |
| 683 |
) |
| 684 |
else |
| 685 |
SOME result |
| 686 |
end |
| 687 |
else |
| 688 |
NONE |
| 689 |
val doWatch = case fswatcherlib of |
| 690 |
SOME fswatcherlib => |
| 691 |
( if Message.getVerbosity () >= 2 then |
| 692 |
Message.info "Using built-in filesystem watcher for Windows" |
| 693 |
else |
| 694 |
() |
| 695 |
; doWatchWindows fswatcherlib |
| 696 |
) |
| 697 |
| NONE => if ShellUtil.hasCommand "fswatch" andalso (watch_engine = AppOptions.WatchEngine.AUTO orelse watch_engine = AppOptions.WatchEngine.AUTO) then |
| 698 |
( if Message.getVerbosity () >= 2 then |
| 699 |
Message.info "Using `fswatch' command" |
| 700 |
else |
| 701 |
() |
| 702 |
; doWatchFswatch |
| 703 |
) |
| 704 |
else if ShellUtil.hasCommand "inotifywait" andalso (watch_engine = AppOptions.WatchEngine.AUTO orelse watch_engine = AppOptions.WatchEngine.INOTIFYWAIT) then |
| 705 |
( if Message.getVerbosity () >= 2 then |
| 706 |
Message.info "Using `inotifywait' command" |
| 707 |
else |
| 708 |
() |
| 709 |
; doWatchInotifywait |
| 710 |
) |
| 711 |
else |
| 712 |
( case watch_engine of |
| 713 |
AppOptions.WatchEngine.AUTO => Message.error "Could not watch files because neither `fswatch' nor `inotifywait' was installed." |
| 714 |
| AppOptions.WatchEngine.FSWATCH => Message.error "Could not watch files because your selected engine `fswatch' was not installed." |
| 715 |
| AppOptions.WatchEngine.INOTIFYWAIT => Message.error "Could not watch files because your selected engine `inotifywait' was not installed." |
| 716 |
; Message.info "See ClutTeX's manual for details." |
| 717 |
; OS.Process.exit OS.Process.failure |
| 718 |
) |
| 719 |
|
| 720 |
val _ = (doTypeset run_params; true) handle Abort => false |
| 721 |
(* TODO: filenames here can be UTF-8 if command_line_encoding=utf-8 *) |
| 722 |
val recorded = Reruncheck.parseRecorderFile { file = recorderfile, options = options } |
| 723 |
val recorded = if TeXEngine.isLuaTeX engine andalso FSUtil.isFile recorderfile2 then |
| 724 |
Reruncheck.parseRecorderFileContinued { file = recorderfile2, options = options, previousResult = recorded } |
| 725 |
else |
| 726 |
recorded |
| 727 |
val (filelist, _) = Reruncheck.getFileInfo recorded |
| 728 |
val inputFilesToWatch = List.mapPartial (fn { path = _, abspath, kind = Reruncheck.INPUT } => SOME abspath | _ => NONE) filelist |
| 729 |
fun loop inputFilesToWatch |
| 730 |
= if doWatch inputFilesToWatch then |
| 731 |
let val success = (doTypeset run_params; true) handle Abort => false |
| 732 |
in if success then |
| 733 |
let val recorded = Reruncheck.parseRecorderFile { file = recorderfile, options = options } |
| 734 |
val recorded = if TeXEngine.isLuaTeX engine andalso FSUtil.isFile recorderfile2 then |
| 735 |
Reruncheck.parseRecorderFileContinued { file = recorderfile2, options = options, previousResult = recorded } |
| 736 |
else |
| 737 |
recorded |
| 738 |
val (filelist, _) = Reruncheck.getFileInfo recorded |
| 739 |
val inputFilesToWatch = List.mapPartial (fn { path = _, abspath, kind = Reruncheck.INPUT } => SOME abspath | _ => NONE) filelist |
| 740 |
in loop inputFilesToWatch |
| 741 |
end |
| 742 |
else |
| 743 |
loop inputFilesToWatch (* error; watch the same files again *) |
| 744 |
end |
| 745 |
else |
| 746 |
() (* exit *) |
| 747 |
in loop inputFilesToWatch |
| 748 |
end |
| 749 |
|
| 750 |
fun getConfigFilePath (SOME configFilePath) = SOME configFilePath |
| 751 |
| getConfigFilePath NONE = case OS.Process.getEnv "CLUTTEX_CONFIG_FILE" of |
| 752 |
SOME f => SOME f |
| 753 |
| NONE => if OSUtil.isWindows then |
| 754 |
case OS.Process.getEnv "APPDATA" of |
| 755 |
SOME appData => SOME (appData ^ "\\cluttex\\config.toml") |
| 756 |
| NONE => NONE |
| 757 |
else |
| 758 |
case OS.Process.getEnv "XDG_CONFIG_HOME" of |
| 759 |
SOME xdgConfigHome => SOME (xdgConfigHome ^ "/cluttex/config.toml") |
| 760 |
| NONE => case OS.Process.getEnv "HOME" of |
| 761 |
SOME home => SOME (home ^ "/.config/cluttex/config.toml") |
| 762 |
| NONE => NONE |
| 763 |
|
| 764 |
fun loadConfig configFileOpt = case getConfigFilePath configFileOpt of |
| 765 |
NONE => ConfigFile.defaultConfig |
| 766 |
| SOME path => (ConfigFile.loadConfig path handle IO.Io _ => ConfigFile.defaultConfig |
| 767 |
| ValidateUtf8.InvalidUtf8 => (Message.error ("Config file " ^ path ^ " is not UTF-8 encoded."); ConfigFile.defaultConfig) |
| 768 |
| TomlParseError.ParseError e => (Message.error ("Config file " ^ path ^ " is not a valid TOML file: " ^ TomlParseError.toString e); ConfigFile.defaultConfig) |
| 769 |
) |
| 770 |
|
| 771 |
fun main () = let val (options, rest) = HandleOptions.parse (AppOptions.init, CommandLine.arguments ()) |
| 772 |
val config = loadConfig (#config_file options) |
| 773 |
|
| 774 |
(* Apply colors *) |
| 775 |
val () = Option.app Message.setTypeStyle (#type_ (#color config)) |
| 776 |
val () = Option.app Message.setExecuteStyle (#execute (#color config)) |
| 777 |
val () = Option.app Message.setErrorStyle (#error (#color config)) |
| 778 |
val () = Option.app Message.setWarningStyle (#warning (#color config)) |
| 779 |
val () = Option.app Message.setDiagnosticStyle (#diagnostic (#color config)) |
| 780 |
val () = Option.app Message.setInformationStyle (#information (#color config)) |
| 781 |
|
| 782 |
val watch = #watch options |
| 783 |
val () = case #color options of |
| 784 |
NONE => Message.setColors Message.AUTO |
| 785 |
| _ => () |
| 786 |
val inputfile = case rest of |
| 787 |
[] => showUsage () (* No input file given *) |
| 788 |
| [input] => input |
| 789 |
| _ => ( Message.error "Multiple input files are not supported." |
| 790 |
; OS.Process.exit OS.Process.failure |
| 791 |
) |
| 792 |
val engine = case #engine options of |
| 793 |
SOME name => (case TeXEngine.get name of |
| 794 |
SOME engine => engine |
| 795 |
| NONE => ( Message.error ("Unknown engine name '" ^ name ^ "'.") |
| 796 |
; OS.Process.exit OS.Process.failure |
| 797 |
) |
| 798 |
) |
| 799 |
| NONE => let val name = CommandLine.name () |
| 800 |
val basename = PathUtil.trimext (PathUtil.basename name) |
| 801 |
(* If run as 'cl<engine name>' (e.g. 'cllualatex'), then the default engine is <engine name>. *) |
| 802 |
fun notSpecified () = ( Message.error "Engine not specified." |
| 803 |
; OS.Process.exit OS.Process.failure |
| 804 |
) |
| 805 |
in if String.isPrefix "cl" basename andalso CharVector.all Char.isAlphaNum basename then |
| 806 |
case TeXEngine.get (String.extract (basename, 2, NONE)) of |
| 807 |
NONE => notSpecified () |
| 808 |
| SOME engine => engine |
| 809 |
else |
| 810 |
notSpecified () |
| 811 |
end |
| 812 |
val output_format = Option.getOpt (#output_format options, OutputFormat.PDF) |
| 813 |
val check_driver = case output_format of |
| 814 |
OutputFormat.PDF => |
| 815 |
( case #check_driver options of |
| 816 |
NONE => () |
| 817 |
| SOME _ => ( Message.error ("--check-driver can only be used when the output format is DVI.") |
| 818 |
; OS.Process.exit OS.Process.failure |
| 819 |
) |
| 820 |
; if #supports_pdf_generation engine then |
| 821 |
if TeXEngine.isLuaTeX engine then |
| 822 |
SOME CheckDriver.LUATEX |
| 823 |
else if TeXEngine.isXeTeX engine then |
| 824 |
SOME CheckDriver.XETEX |
| 825 |
else if TeXEngine.isPdfTeX engine then |
| 826 |
SOME CheckDriver.PDFTEX |
| 827 |
else |
| 828 |
( Message.warn ("Unknown engine: " ^ #name engine) |
| 829 |
; Message.warn "Driver check will not work." |
| 830 |
; NONE |
| 831 |
) |
| 832 |
else |
| 833 |
(* ClutTeX uses dvipdfmx to generate PDF from DVI output *) |
| 834 |
SOME CheckDriver.DVIPDFMX |
| 835 |
) |
| 836 |
| OutputFormat.DVI => |
| 837 |
case #check_driver options of |
| 838 |
SOME AppOptions.DviDriver.DVIPDFMX => SOME CheckDriver.DVIPDFMX |
| 839 |
| SOME AppOptions.DviDriver.DVIPS => SOME CheckDriver.DVIPS |
| 840 |
| SOME AppOptions.DviDriver.DVISVGM => SOME CheckDriver.DVISVGM |
| 841 |
| NONE => NONE |
| 842 |
val (jobname, jobname_for_output) = case #jobname options of |
| 843 |
SOME jobname => (jobname, jobname) |
| 844 |
| NONE => let val basename = PathUtil.basename (PathUtil.trimext inputfile) |
| 845 |
in (SafeName.escapeJobname basename, basename) |
| 846 |
end |
| 847 |
val output_extension = case output_format of |
| 848 |
OutputFormat.DVI => #dvi_extension engine (* "dvi" or "xdv" *) |
| 849 |
| OutputFormat.PDF => "pdf" |
| 850 |
val output_from_original_wd = case #output options of |
| 851 |
NONE => jobname_for_output ^ "." ^ output_extension |
| 852 |
| SOME output => output |
| 853 |
val output_directory_from_original_wd |
| 854 |
= case #output_directory options of |
| 855 |
SOME dir => if #fresh options then |
| 856 |
( Message.error "--fresh and --output-directory cannot be used together." |
| 857 |
; OS.Process.exit OS.Process.failure |
| 858 |
) |
| 859 |
else |
| 860 |
dir |
| 861 |
| NONE => let val inputfile_abs = PathUtil.abspath { path = inputfile, cwd = NONE } |
| 862 |
val output_directory = genOutputDirectory (#temporary_directory config, [inputfile_abs, jobname, Option.getOpt (#engine_executable options, #executable engine)]) |
| 863 |
in if not (FSUtil.isDirectory output_directory) then |
| 864 |
FSUtil.mkDirRec output_directory |
| 865 |
else if #fresh options then |
| 866 |
( if Message.getVerbosity () >= 1 then |
| 867 |
Message.info ("Cleaning '" ^ output_directory ^ "'...") |
| 868 |
else |
| 869 |
() |
| 870 |
; FSUtil.removeRec output_directory |
| 871 |
; OS.FileSys.mkDir output_directory |
| 872 |
) |
| 873 |
else |
| 874 |
() |
| 875 |
; output_directory |
| 876 |
end |
| 877 |
|
| 878 |
val () = if #print_output_directory options then |
| 879 |
( print (output_directory_from_original_wd ^ "\n") |
| 880 |
; OS.Process.exit OS.Process.success |
| 881 |
) |
| 882 |
else |
| 883 |
() |
| 884 |
|
| 885 |
val pathsep = if OSUtil.isWindows then |
| 886 |
";" |
| 887 |
else |
| 888 |
":" |
| 889 |
|
| 890 |
val original_wd = OS.FileSys.getDir () |
| 891 |
val (output, output_directory, tex_output_directory) |
| 892 |
= if Option.getOpt (#change_directory options, false) then |
| 893 |
let val TEXINPUTS = Option.getOpt (OS.Process.getEnv "TEXINPUTS", "") |
| 894 |
val LUAINPUTS = Option.getOpt (OS.Process.getEnv "LUAINPUTS", "") |
| 895 |
val () = OS.FileSys.chDir output_directory_from_original_wd |
| 896 |
val () = OSUtil.setEnv ("TEXINPUTS", original_wd ^ pathsep ^ TEXINPUTS) |
| 897 |
val () = OSUtil.setEnv ("LUAINPUTS", original_wd ^ pathsep ^ LUAINPUTS) |
| 898 |
in (PathUtil.abspath { path = output_from_original_wd, cwd = SOME original_wd }, ".", NONE) |
| 899 |
end |
| 900 |
else |
| 901 |
(output_from_original_wd, output_directory_from_original_wd, SOME output_directory_from_original_wd) |
| 902 |
val output = case #bibtex_or_biber options of |
| 903 |
SOME _ => let val BIBINPUTS = Option.getOpt (OS.Process.getEnv "BIBINPUTS", "") |
| 904 |
val () = OSUtil.setEnv ("BIBINPUTS", original_wd ^ pathsep ^ BIBINPUTS) |
| 905 |
in PathUtil.abspath { path = output_from_original_wd, cwd = SOME original_wd } (* Is this needed? *) |
| 906 |
end |
| 907 |
| NONE => output |
| 908 |
|
| 909 |
(* |
| 910 |
* Set `max_print_line' environment variable if not already set. |
| 911 |
* |
| 912 |
* According to texmf.cnf: |
| 913 |
* 45 < error_line < 255, |
| 914 |
* 30 < half_error_line < error_line - 15, |
| 915 |
* 60 <= max_print_line. |
| 916 |
* |
| 917 |
* On TeX Live 2023, (u)(p)bibtex fails if max_print_line >= 20000. |
| 918 |
*) |
| 919 |
val () = case OS.Process.getEnv "max_print_line" of |
| 920 |
NONE => OSUtil.setEnv ("max_print_line", "16384") |
| 921 |
| SOME _ => () |
| 922 |
|
| 923 |
fun pathInOutputDirectory ext = PathUtil.join2 (output_directory, jobname ^ "." ^ ext) |
| 924 |
|
| 925 |
val recorderfile = pathInOutputDirectory "fls" |
| 926 |
val recorderfile2 = pathInOutputDirectory "cluttex-fls" |
| 927 |
|
| 928 |
val tex_output_format = case output_format of |
| 929 |
OutputFormat.DVI => OutputFormat.DVI |
| 930 |
| OutputFormat.PDF => if #supports_pdf_generation engine then |
| 931 |
OutputFormat.PDF |
| 932 |
else |
| 933 |
OutputFormat.DVI |
| 934 |
|
| 935 |
(* Setup LuaTeX initialization script *) |
| 936 |
val lua_initialization_script |
| 937 |
= if TeXEngine.isLuaTeX engine then |
| 938 |
let val initscriptfile = pathInOutputDirectory "cluttexinit.lua" |
| 939 |
in LuaTeXInit.createInitializationScript (initscriptfile, { file_line_error = #file_line_error options, halt_on_error = #halt_on_error options, output_directory = output_directory, jobname = jobname }) |
| 940 |
; SOME initscriptfile |
| 941 |
end |
| 942 |
else |
| 943 |
NONE |
| 944 |
|
| 945 |
(* Set SOURCE_DATE_EPOCH if --source-date-epoch=<timestamp> is set *) |
| 946 |
val source_date_epoch_info = case #source_date_epoch options of |
| 947 |
SOME (AppOptions.SourceDateEpoch.RAW raw) => |
| 948 |
(OSUtil.setEnv ("SOURCE_DATE_EPOCH", raw); NONE) |
| 949 |
| _ => if #source_date_epoch options = SOME AppOptions.SourceDateEpoch.NOW orelse OS.Process.getEnv "SOURCE_DATE_EPOCH" = NONE then |
| 950 |
SOME (ref { time_since_epoch = getTimeSinceEpoch (), time = Time.now () }) |
| 951 |
else |
| 952 |
NONE |
| 953 |
|
| 954 |
val tex_options : TeXEngine.run_options |
| 955 |
= { engine_executable = #engine_executable options |
| 956 |
, interaction = SOME (Option.getOpt (#interaction options, InteractionMode.NONSTOPMODE)) |
| 957 |
, file_line_error = #file_line_error options |
| 958 |
, halt_on_error = #halt_on_error options |
| 959 |
, synctex = #synctex options |
| 960 |
, output_directory = tex_output_directory |
| 961 |
, shell_escape = #shell_escape options |
| 962 |
, jobname = SOME jobname |
| 963 |
, fmt = #fmt options |
| 964 |
, extra_options = #tex_extraoptions options |
| 965 |
, output_format = tex_output_format |
| 966 |
, draftmode = false |
| 967 |
, lua_initialization_script = lua_initialization_script |
| 968 |
} |
| 969 |
val options : AppOptions.options |
| 970 |
= { engine = engine |
| 971 |
, engine_executable = #engine_executable options |
| 972 |
, output = output |
| 973 |
, fresh = #fresh options |
| 974 |
, max_iterations = Option.getOpt (#max_iterations options, 4) |
| 975 |
, start_with_draft = #start_with_draft options |
| 976 |
, watch = #watch options |
| 977 |
, change_directory = Option.getOpt (#change_directory options, false) |
| 978 |
, includeonly = #includeonly options |
| 979 |
, make_depends = #make_depends options |
| 980 |
, print_output_directory = #print_output_directory options |
| 981 |
, package_support = #package_support options |
| 982 |
, check_driver = check_driver |
| 983 |
, source_date_epoch = #source_date_epoch options |
| 984 |
, synctex = #synctex options |
| 985 |
, file_line_error = #file_line_error options |
| 986 |
, interaction = Option.getOpt (#interaction options, InteractionMode.NONSTOPMODE) |
| 987 |
, halt_on_error = #halt_on_error options |
| 988 |
, shell_escape = #shell_escape options |
| 989 |
, jobname = jobname |
| 990 |
, fmt = #fmt options |
| 991 |
, output_directory = output_directory |
| 992 |
, output_format = output_format |
| 993 |
, tex_extraoptions = #tex_extraoptions options |
| 994 |
, dvipdfmx_extraoptions = #dvipdfmx_extraoptions options |
| 995 |
, makeindex = #makeindex options |
| 996 |
, bibtex_or_biber = #bibtex_or_biber options |
| 997 |
, makeglossaries = #makeglossaries options |
| 998 |
} |
| 999 |
val run_params = { options = options |
| 1000 |
, inputfile = inputfile |
| 1001 |
, engine = engine |
| 1002 |
, tex_options = tex_options |
| 1003 |
, recorderfile = recorderfile |
| 1004 |
, recorderfile2 = recorderfile2 |
| 1005 |
, original_wd = original_wd |
| 1006 |
, output_extension = output_extension |
| 1007 |
, source_date_epoch_info = source_date_epoch_info |
| 1008 |
} |
| 1009 |
in case watch of |
| 1010 |
NONE => (doTypeset run_params handle Abort => OS.Process.exit OS.Process.failure) |
| 1011 |
| SOME watch_engine => runWatchMode (watch_engine, run_params) |
| 1012 |
end |
| 1013 |
end; |
| 1014 |
val () = Main.main (); |