PageRenderTime 30ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/build-aux/texi2dvi

https://bitbucket.org/prologin/stechec
#! | 1307 lines | 1155 code | 152 blank | 0 comment | 0 complexity | 6542575d68a7cc60bf53a00ac8b07cd0 MD5 | raw file
  1. #! /usr/bin/env bash
  2. # texi2dvi --- produce DVI (or PDF) files from Texinfo (or (La)TeX) sources.
  3. # $Id: texi2dvi,v 1.65 2005/10/24 00:33:43 karl Exp $
  4. #
  5. # Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001,
  6. # 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2, or (at your option)
  11. # any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, you can either send email to this
  20. # program's maintainer or write to: The Free Software Foundation,
  21. # Inc.; 51 Franklin Street, Fifth Floor; Boston, MA 02110-1301, USA.
  22. #
  23. # Original author: Noah Friedman.
  24. #
  25. # Please send bug reports, etc. to bug-texinfo@gnu.org.
  26. # If possible, please send a copy of the output of the script called with
  27. # the `--debug' option when making a bug report.
  28. test -f /bin/ksh && test -z "$RUNNING_KSH" \
  29. && { UNAMES=`uname -s`; test "x$UNAMES" = xULTRIX; } 2>/dev/null \
  30. && { RUNNING_KSH=true; export RUNNING_KSH; exec /bin/ksh $0 ${1+"$@"}; }
  31. unset RUNNING_KSH
  32. # This string is expanded by rcs automatically when this file is checked out.
  33. rcs_revision='$Revision: 1.65 $'
  34. rcs_version=`set - $rcs_revision; echo $2`
  35. program=`echo $0 | sed -e 's!.*/!!'`
  36. version="texi2dvi (GNU Texinfo 4.8) $rcs_version
  37. Copyright (C) 2005 Free Software Foundation, Inc.
  38. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
  39. PARTICULAR PURPOSE. You may redistribute copies of GNU texinfo
  40. under the terms of the GNU General Public License.
  41. For more information about these matters, see the file named COPYING.
  42. "
  43. build_mode=${TEXI2DVI_BUILD_MODE:-local}
  44. build_dir=${TEXI2DVI_BUILD_DIRECTORY:-.}
  45. usage="Usage: $program [OPTION]... FILE...
  46. Run each Texinfo or (La)TeX FILE through TeX in turn until all
  47. cross-references are resolved, building all indices. The directory
  48. containing each FILE is searched for included files. The suffix of FILE
  49. is used to determine its language ((La)TeX or Texinfo). To process
  50. (e)plain TeX files, set the environment variable LATEX=tex.
  51. In order to make texi2dvi a drop-in replacement of TeX/LaTeX in AUC-TeX,
  52. the FILE may also be composed of the following simple TeX commands.
  53. \`\\input{FILE}' the actual file to compile
  54. \`\\nonstopmode\ same as --batch
  55. Makeinfo is used to perform Texinfo macro expansion before running TeX
  56. when needed.
  57. Operation modes:
  58. -b, --batch no interaction
  59. -D, --debug turn on shell debugging (set -x)
  60. -h, --help display this help and exit successfully
  61. -o, --output=OFILE leave output in OFILE (implies --clean);
  62. Only one input FILE may be specified in this case
  63. -q, --quiet no output unless errors (implies --batch)
  64. -s, --silent same as --quiet
  65. -v, --version display version information and exit successfully
  66. -V, --verbose report on what is done
  67. TeX tuning:
  68. -@ use @input instead of \input; for preloaded Texinfo
  69. -e, -E, --expand force macro expansion using makeinfo
  70. -I DIR search DIR for Texinfo files
  71. -l, --language=LANG specify the LANG of FILE (LaTeX or Texinfo)
  72. -p, --pdf use pdftex or pdflatex for processing
  73. -r, --recode call recode before TeX to translate input characters
  74. -t, --command=CMD insert CMD in copy of input file
  75. or --texinfo=CMD multiple values accumulate
  76. Build modes:
  77. --build=MODE specify the treatment of auxiliary files [$build_mode]
  78. --tidy same as --build=tidy
  79. -c, --clean same as --build=clean
  80. --build-dir=DIR specify where the tidy compilation is performed.
  81. implies --tidy
  82. defaults to TEXI2DVI_BUILD_DIRECTORY [$build_dir]
  83. The MODE specifies where the TeX compilation takes place, and, as a
  84. consequence, how auxiliary files are treated. The build mode
  85. can also be set using the environment variable TEXI2DVI_BUILD_MODE.
  86. Valid MODEs are:
  87. \`local' compile in the current directory, leaving all the auxiliary
  88. files around. This is the traditional TeX use.
  89. \`tidy' compile in a local *.t2d directory, where the auxiliary files
  90. are left. Output files are copied back to the original file.
  91. \`clean' same as \`tidy', but remove the auxiliary directory afterwards.
  92. Every compilation therefore requires the full cycle.
  93. Using the \`tidy' mode brings several advantages:
  94. - the current directory is not cluttered with plethora of temporary files.
  95. - clutter can be even reduced using --build-dir=dir: all the *.t2d
  96. directories are stored there.
  97. - clutter can be reduced to zero using --build-dir=/tmp/\$USER.t2d or
  98. --build-dir=$HOME/.t2d.
  99. - the output file is updated after every succesful TeX run, for
  100. sake of concurrent visualization of the output. In a \`local' build
  101. the viewer stops during the whole TeX run.
  102. - if the compilation fails, the previous state of the output file
  103. is preserved.
  104. - PDF and DVI compilation are kept in separate subdirectories
  105. preventing any possibility of auxiliary file incompatibility.
  106. On the other hand, because \`tidy' compilation takes place in another
  107. directory, occasionally TeX won't be able to find some files (e.g., when
  108. using \\graphicspath): in that case use -I to specify the additional
  109. directories to consider.
  110. The values of the BIBTEX, LATEX (or PDFLATEX), MAKEINDEX, MAKEINFO,
  111. TEX (or PDFTEX), TEXINDEX, and THUMBPDF environment variables are used
  112. to run those commands, if they are set. Any CMD strings are added
  113. after @setfilename for Texinfo input, in the first line for LaTeX input.
  114. Email bug reports to <bug-texinfo@gnu.org>,
  115. general questions and discussion to <help-texinfo@gnu.org>.
  116. Texinfo home page: http://www.gnu.org/software/texinfo/"
  117. # Initialize variables for option overriding and otherwise.
  118. # Don't use `unset' since old bourne shells don't have this command.
  119. # Instead, assign them an empty value.
  120. line_error=true # Pass --file-line-error to TeX.
  121. batch=false # true for batch mode
  122. debug=false
  123. escape="\\"
  124. expand= # t for expansion via makeinfo
  125. includes=
  126. out_lang=dvi
  127. oname= # --output
  128. quiet=false # by default let the tools' message be displayed
  129. recode=false
  130. set_language=
  131. textra= # Extra TeX commands to insert in the input file.
  132. txiprereq=19990129 # minimum texinfo.tex version with macro expansion
  133. verb=false # true for verbose mode
  134. orig_pwd=`pwd`
  135. # Systems which define $COMSPEC or $ComSpec use semicolons to separate
  136. # directories in TEXINPUTS -- except for Cygwin, where COMSPEC might be
  137. # inherited, but : is used.
  138. if test -n "$COMSPEC$ComSpec" && uname | grep -iv cygwin >/dev/null; then
  139. path_sep=";"
  140. else
  141. path_sep=":"
  142. fi
  143. # Pacify verbose cds.
  144. CDPATH=${ZSH_VERSION+.}$path_sep
  145. # In case someone crazy insists on using grep -E.
  146. : ${EGREP=egrep}
  147. ## --------------------- ##
  148. ## Auxiliary functions. ##
  149. ## --------------------- ##
  150. # In case `local' is not supported by the shell.
  151. (
  152. foo=bar
  153. test_local () {
  154. local foo=foo
  155. }
  156. test_local
  157. test $foo = bar
  158. ) || local () {
  159. case $1 in
  160. *=*) eval "$1";;
  161. esac
  162. }
  163. # func_dirname FILE
  164. # -----------------
  165. # Return the directory part of FILE.
  166. func_dirname ()
  167. {
  168. dirname "$1" 2>/dev/null \
  169. || { echo "$1" | sed 's!/[^/]*$!!;s!^$!.!'; }
  170. }
  171. # absolute NAME -> ABS-NAME
  172. # -------------------------
  173. # Return an absolute path to NAME.
  174. absolute ()
  175. {
  176. case $1 in
  177. [\\/]* | ?:[\\/]*)
  178. # Absolute paths don't need to be expanded.
  179. echo "$1"
  180. ;;
  181. *)
  182. (cd "$orig_pwd/`func_dirname $1`" 2>/dev/null &&
  183. echo `pwd`/`basename "$1"`)
  184. ;;
  185. esac
  186. }
  187. # findprog PROG
  188. # -------------
  189. # Return true if PROG is somewhere in PATH, else false.
  190. findprog ()
  191. {
  192. local saveIFS=$IFS
  193. IFS=$path_sep # break path components at the path separator
  194. for dir in $PATH; do
  195. IFS=$saveIFS
  196. # The basic test for an executable is `test -f $f && test -x $f'.
  197. # (`test -x' is not enough, because it can also be true for directories.)
  198. # We have to try this both for $1 and $1.exe.
  199. #
  200. # Note: On Cygwin and DJGPP, `test -x' also looks for .exe. On Cygwin,
  201. # also `test -f' has this enhancement, bot not on DJGPP. (Both are
  202. # design decisions, so there is little chance to make them consistent.)
  203. # Thusly, it seems to be difficult to make use of these enhancements.
  204. #
  205. if { test -f "$dir/$1" && test -x "$dir/$1"; } ||
  206. { test -f "$dir/$1.exe" && test -x "$dir/$1.exe"; }; then
  207. return 0
  208. fi
  209. done
  210. return 1
  211. }
  212. # report LINE1 LINE2...
  213. # ---------------------
  214. # Report some information on stderr.
  215. report ()
  216. {
  217. for i in "$@"
  218. do
  219. echo >&2 "$0: $i"
  220. done
  221. }
  222. # Report some verbose information.
  223. verbose ()
  224. {
  225. $verb && echo >&2 "$0: $@"
  226. }
  227. # fatal EXIT_STATUS LINE1 LINE2...
  228. # --------------------------------
  229. # Report an error and exit with failure.
  230. fatal ()
  231. {
  232. s=$1
  233. shift
  234. report "$@"
  235. exit $s
  236. }
  237. # ensure_dir DIR1 DIR2...
  238. # -----------------------
  239. # Make sure the directories exist.
  240. ensure_dir ()
  241. {
  242. for dir
  243. do
  244. test -d "$dir" \
  245. || mkdir "$dir" \
  246. || fatal 1 "cannot create directory: $dir"
  247. done
  248. }
  249. ## ---------------- ##
  250. ## Handling lists. ##
  251. ## ---------------- ##
  252. # list_append LIST ELEM
  253. # ---------------------
  254. list_append ()
  255. {
  256. local la_l="$1"
  257. shift
  258. eval set X \$$la_l "$@"
  259. shift
  260. eval $la_l=\""$@"\"
  261. eval echo list_append: $la_l=\$$la_l
  262. }
  263. # list_prefix LIST SEP -> STRING
  264. # ----------------------------
  265. list_prefix ()
  266. {
  267. local lp_p=$2
  268. eval set X \$$1
  269. shift
  270. local lp_res
  271. for i
  272. do
  273. lp_res="$lp_res \"$lp_p\" \"$i\""
  274. done
  275. echo "$lp_res"
  276. }
  277. # list_infix LIST SEP -> STRING
  278. # ----------------------------
  279. list_infix ()
  280. {
  281. eval set X \$$1
  282. shift
  283. local la_IFS=$IFS
  284. IFS=$path_sep
  285. echo "$*"
  286. IFS=$la_IFS
  287. }
  288. # list_dir_to_abs LIST
  289. # --------------------
  290. # Convert the list to using only absolute dir names.
  291. list_dir_to_abs ()
  292. {
  293. local ld_l=$1
  294. eval set X \$$ld_l
  295. shift
  296. local ld_res
  297. for dir
  298. do
  299. dir=`absolute "$dir"`
  300. test -d "$dir" || continue
  301. ld_res="$ld_res \"$dir\""
  302. done
  303. echo "list_dir_to_abs: $ld_res"
  304. set X $ld_res; shift
  305. eval $ld_l=\"$@\"
  306. eval echo $ld_l=\$$ld_l
  307. }
  308. ## ------------------------- ##
  309. ## TeX auxiliary functions. ##
  310. ## ------------------------- ##
  311. # Save TEXINPUTS so we can construct a new TEXINPUTS path for each file.
  312. # Likewise for bibtex and makeindex.
  313. tex_envvars="BIBINPUTS BSTINPUTS INDEXSTYLE TEXINPUTS"
  314. for var in $tex_envvars; do
  315. eval ${var}_orig=\$$var
  316. export $var
  317. done
  318. # get_index_files FILE1 FILE2...
  319. # ------------------------------
  320. # Select the files that are indexes to run texindex/makeindex onto.
  321. get_index_files ()
  322. {
  323. for file
  324. do
  325. case `sed '1q' $file` in
  326. "\\entry{"*|"\\indexentry{"*) echo "$file";;
  327. esac
  328. done
  329. }
  330. # absolute_filenames TEX-PATH -> TEX-PATH
  331. # ---------------------------------------
  332. # Convert relative paths to absolute paths, so we can run in another
  333. # directory (e.g., in tidy build mode, or during the macro-support
  334. # detection).
  335. #
  336. # Empty path components are meaningful to tex. We rewrite them
  337. # as `EMPTY' so they don't get lost when we split on $path_sep.
  338. # Hopefully no one will have an actual directory named EMPTY.
  339. absolute_filenames ()
  340. {
  341. local replace_empty="-e 's/^$path_sep/EMPTY$path_sep/g' \
  342. -e 's/$path_sep\$/${path_sep}EMPTY/g' \
  343. -e 's/$path_sep$path_sep/${path_sep}EMPTY:/g'"
  344. local res=`echo "$1" | eval sed $replace_empty`
  345. save_IFS=$IFS
  346. IFS=$path_sep
  347. set x $res; shift
  348. res=.
  349. for dir
  350. do
  351. case $dir in
  352. EMPTY)
  353. res=$res$path_sep
  354. ;;
  355. *)
  356. test -d $dir && res=$res$path_sep`absolute "$dir"`
  357. ;;
  358. esac
  359. done
  360. echo "$res"
  361. }
  362. # output_base_name FILE
  363. # ---------------------
  364. # The name of the FILE possibly renamed to satisfy --output.
  365. output_base_name ()
  366. {
  367. case $oname in
  368. '') echo "$1";;
  369. *) local out_noext=`echo "$oname" | sed 's/\.[^.]*$//'`
  370. local file_ext=`echo "$1" | sed 's/^.*\.//'`
  371. echo "$out_noext.$file_ext"
  372. ;;
  373. esac
  374. }
  375. # move_to_dest FILE
  376. # -----------------
  377. # Move the FILE to the place where the user expects it. FILE can be
  378. # the principal output (in which case -o directly apply), or an auxiliary
  379. # file with the same base name.
  380. move_to_dest ()
  381. {
  382. local dest
  383. case $tidy:$oname in
  384. true:) dest=$orig_pwd;;
  385. false:) dest=;;
  386. *:*) dest=$(output_base_name "$1");;
  387. esac
  388. if test ! -f "$1"; then
  389. fatal 1 "no such file or directory: $1"
  390. fi
  391. if test -n "$dest"; then
  392. verbose "Copying $1 to $dest"
  393. cp -p "$1" "$dest"
  394. fi
  395. }
  396. ## --------------------- ##
  397. ## Managing xref files. ##
  398. ## --------------------- ##
  399. # xref_files_get FILENAME-NOEXT
  400. # -----------------------------
  401. # Compute the list of xref files (indexes, tables and lists).
  402. xref_files_get ()
  403. {
  404. if $tidy; then
  405. # In a tidy build, all the files around as actual outputs.
  406. ls * 2>/dev/null
  407. else
  408. # Find all files having root filename with a two-letter extension,
  409. # saves the ones that are really Texinfo-related files.
  410. # - .?o?
  411. # .toc, .log, LaTeX tables and lists, FiXme's .lox, maybe more.
  412. # - bu[0-9]*.aux
  413. # Sub bibliography when using the LaTeX bibunits package.
  414. ls "$1".?o? "$1".aux bu[0-9]*.aux "$1".?? "$1".idx 2>/dev/null
  415. fi |
  416. while read file; do
  417. # If the file is not suitable to be an index or xref file, don't
  418. # process it. It's suitable if the first character is a
  419. # backslash or right quote or at, as long as the first line isn't
  420. # \input texinfo.
  421. case `sed '1q' $file` in
  422. "\\input texinfo"*) ;;
  423. [\\''@]*) echo "./$file";;
  424. esac
  425. done
  426. }
  427. # xref_files_save
  428. # ---------------
  429. # Save the xref files.
  430. xref_files_save ()
  431. {
  432. # Save copies of auxiliary files for later comparison.
  433. xref_files_orig=`xref_files_get "$in_noext"`
  434. if test -n "$xref_files_orig"; then
  435. verbose "Backing up xref files: $xref_files_orig"
  436. cp $xref_files_orig "$work_bak"
  437. fi
  438. }
  439. # xref_files_changed
  440. # ------------------
  441. # Whether the xref files were changed since the previous run.
  442. xref_files_changed ()
  443. {
  444. # LaTeX (and the package changebar) report in the LOG file if it
  445. # should be rerun. This is needed for files included from
  446. # subdirs, since texi2dvi does not try to compare xref files in
  447. # subdirs. Performing xref files test is still good since LaTeX
  448. # does not report changes in xref files.
  449. grep "Rerun to get" "$in_noext.log" >&6 2>&1 &&
  450. return 0
  451. # If old and new lists don't at least have the same file list,
  452. # then one file or another has definitely changed.
  453. xref_files_new=`xref_files_get "$in_noext"`
  454. verbose "Original xref files = $xref_files_orig"
  455. verbose "New xref files = $xref_files_new"
  456. test "x$xref_files_orig" != "x$xref_files_new" &&
  457. return 0
  458. # Compare each file until we find a difference.
  459. for this_file in $xref_files_new; do
  460. verbose "Comparing xref file `echo $this_file | sed 's|\./||g'` ..."
  461. # cmp -s returns nonzero exit status if files differ.
  462. if cmp -s "$this_file" "$work_bak/$this_file"; then :; else
  463. verbose "xref file `echo $this_file | sed 's|\./||g'` differed ..."
  464. $debug && diff -u "$work_bak/$this_file" "$this_file"
  465. return 0
  466. fi
  467. done
  468. # No change.
  469. return 1
  470. }
  471. ## ----------------------- ##
  472. ## Running the TeX suite. ##
  473. ## ----------------------- ##
  474. # run_tex ()
  475. # ----------
  476. # Run TeX as "$tex $in_input", taking care of errors and logs.
  477. run_tex ()
  478. {
  479. case $language:$out_lang in
  480. latex:dvi) tex=${LATEX:-latex};;
  481. latex:pdf) tex=${PDFLATEX:-pdflatex};;
  482. texinfo:dvi)
  483. # MetaPost also uses the TEX environment variable. If the user
  484. # has set TEX=latex for that reason, don't bomb out.
  485. case $TEX in
  486. *latex) tex=tex;; # don't bother trying to find etex
  487. *) tex=$TEX
  488. esac;;
  489. texinfo:pdf) tex=$PDFTEX;;
  490. *) fatal 1 "$out_lang not supported for $language";;
  491. esac
  492. # Beware of aux files in subdirectories that require the
  493. # subdirectory to exist.
  494. case $language:$tidy in
  495. latex:true)
  496. sed -n 's|^[ ]*\\include{\(.*\)/.*}.*|\1|p' $in_input |
  497. sort -u |
  498. while read d
  499. do
  500. ensure_dir "$work_build/$d"
  501. done
  502. ;;
  503. esac
  504. # Note that this will be used via an eval: quote properly.
  505. local cmd=$tex
  506. # If possible, make TeX report error locations in GNU format.
  507. if test "${tex_help:+set}" != set; then
  508. # Go to a temporary directory to try --help, since old versions that
  509. # don't accept --help will generate a texput.log.
  510. tex_help_dir=$t2ddir/tex_help
  511. ensure_dir "$tex_help_dir"
  512. tex_help=`cd "$tex_help_dir" >&6 && $tex --help </dev/null 2>&1`
  513. fi
  514. $line_error && case $tex_help in
  515. *file-line-error*) cmd="$cmd --file-line-error";;
  516. esac
  517. # Tell TeX to be batch if requested.
  518. if $batch; then
  519. # \batchmode does not show terminal output at all, so we don't
  520. # want that. And even in batch mode, TeX insists on having input
  521. # from the user. Close its stdin to make it impossible.
  522. cmd="$cmd </dev/null '${escape}nonstopmode' '${escape}input'"
  523. fi
  524. # TeX's \input does not support white spaces in file names. Our
  525. # intensive use of absolute file names makes this worse: the
  526. # enclosing directory names may include white spaces. Improve the
  527. # situation using a symbolic link. Do not alter in_input.
  528. case $tidy:`func_dirname "$in_input"` in
  529. true:*' '*)
  530. _run_tex_file_name=`basename "$in_input"`
  531. if test ! -f "$_run_tex_file_name"; then
  532. verbose ln -sf "$in_input"
  533. ln -sf "$_run_tex_file_name"
  534. fi
  535. cmd="$cmd '$_run_tex_file_name'"
  536. ;;
  537. *)
  538. cmd="$cmd '$in_input'"
  539. ;;
  540. esac
  541. verbose "Running $cmd ..."
  542. if eval "$cmd" >&5; then
  543. move_to_dest "$in_noext.$out_lang"
  544. else
  545. fatal 1 "$tex exited with bad status, quitting."
  546. fi
  547. }
  548. # run_bibtex ()
  549. # -------------
  550. # Run bibtex on current file.
  551. # - If its input (AUX) exists.
  552. # - If some citations are missing (LOG contains `Citation').
  553. # or the LOG complains of a missing .bbl
  554. #
  555. # We run bibtex first, because I can see reasons for the indexes
  556. # to change after bibtex is run, but I see no reason for the
  557. # converse.
  558. #
  559. # Don't try to be too smart:
  560. #
  561. # 1. Running bibtex only if the bbl file exists and is older than
  562. # the LaTeX file is wrong, since the document might include files
  563. # that have changed.
  564. #
  565. # 3. Because there can be several AUX (if there are \include's),
  566. # but a single LOG, looking for missing citations in LOG is
  567. # easier, though we take the risk to match false messages.
  568. run_bibtex ()
  569. {
  570. case $language in
  571. latex) bibtex=${BIBTEX:-bibtex};;
  572. texinfo) return;;
  573. esac
  574. # "Citation undefined" is for LaTeX, "Undefined citation" for btxmac.tex.
  575. if test -r "$in_noext.aux" \
  576. && test -r "$in_noext.log" \
  577. && (grep 'Warning:.*Citation.*undefined' "$in_noext.log" \
  578. || grep '.*Undefined citation' "$in_noext.log" \
  579. || grep 'No file .*\.bbl\.' "$in_noext.log") \
  580. >&6 2>&1; \
  581. then
  582. # If using the bibunits package, we might have to run bibtex
  583. # on subfiles.
  584. for f in "$in_noext".aux bu[0-9]*.aux
  585. do
  586. if test -s "$f" && \
  587. (grep '^\\bibstyle[{]' "$f" \
  588. && grep '^\\citation[{]' "$f"
  589. ## The following line is suspicious: fails when there
  590. ## are citations in sub aux files. We need to be
  591. ## smarter in this case.
  592. ## && grep '^\\bibdata[{]' "$f" \
  593. ) >&6 2>&1; then
  594. verbose "Running $bibtex $f ..."
  595. $bibtex "$f" >&5 ||
  596. fatal 1 "$bibtex exited with bad status, quitting."
  597. fi
  598. done
  599. fi
  600. }
  601. # run_index ()
  602. # ------------
  603. # Run texindex (or makeindex) on current index files. If they already
  604. # exist, and after running TeX a first time the index files don't
  605. # change, then there's no reason to run TeX again. But we won't know
  606. # that if the index files are out of date or nonexistent.
  607. run_index ()
  608. {
  609. case $language in
  610. latex) texindex=${MAKEINDEX:-makeindex};;
  611. texinfo) texindex=${TEXINDEX:-texindex};;
  612. esac
  613. index_files=`get_index_files $xref_files_orig`
  614. if test -n "$texindex" && test -n "$index_files"; then
  615. verbose "Running $texindex $index_files ..."
  616. $texindex $index_files 2>&5 1>&2 ||
  617. fatal 1 "$texindex exited with bad status, quitting."
  618. fi
  619. }
  620. # run_thumbpdf ()
  621. # ---------------
  622. run_thumbpdf ()
  623. {
  624. if test $out_lang = pdf \
  625. && test -r "$in_noext.log" \
  626. && grep 'thumbpdf\.sty' "$in_noext.log" >&6 2>&1; \
  627. then
  628. thumbpdf=${THUMBPDF:-thumbpdf}
  629. verbose "Running $thumbpdf $in_noext ..."
  630. if $thumbpdf "$in_noext" >&5; then
  631. run_tex
  632. else
  633. report "$thumbpdf exited with bad status." \
  634. "Ignoring its output."
  635. fi
  636. fi
  637. }
  638. # run_tex_suite ()
  639. # ----------------
  640. # Run the TeX tools until a fix point is reached.
  641. run_tex_suite ()
  642. {
  643. # Count the number of cycles.
  644. local cycle=0
  645. while :; do
  646. cycle=`expr $cycle + 1`
  647. verbose "Cycle $cycle for $command_line_filename"
  648. xref_files_save
  649. run_bibtex
  650. run_index
  651. run_tex
  652. xref_files_changed || break
  653. done
  654. # If we were using thumbpdf and producing PDF, then run thumbpdf
  655. # and TeX one last time.
  656. run_thumbpdf
  657. }
  658. ## -------------------------------- ##
  659. ## TeX processing auxiliary tools. ##
  660. ## -------------------------------- ##
  661. # A sed script that preprocesses Texinfo sources in order to keep the
  662. # iftex sections only. We want to remove non TeX sections, and comment
  663. # (with `@c texi2dvi') TeX sections so that makeinfo does not try to
  664. # parse them. Nevertheless, while commenting TeX sections, don't
  665. # comment @macro/@end macro so that makeinfo does propagate them.
  666. # Unfortunately makeinfo --iftex --no-ifinfo doesn't work well enough
  667. # (yet), makeinfo can't parse the TeX commands, so work around with sed.
  668. #
  669. comment_iftex=\
  670. '/^@tex/,/^@end tex/{
  671. s/^/@c texi2dvi/
  672. }
  673. /^@iftex/,/^@end iftex/{
  674. s/^/@c texi2dvi/
  675. /^@c texi2dvi@macro/,/^@c texi2dvi@end macro/{
  676. s/^@c texi2dvi//
  677. }
  678. }
  679. /^@ifnottex/,/^@end ifnottex/{
  680. s/^/@c (texi2dvi)/
  681. }
  682. /^@ifinfo/,/^@end ifinfo/{
  683. /^@node/p
  684. /^@menu/,/^@end menu/p
  685. t
  686. s/^/@c (texi2dvi)/
  687. }
  688. s/^@ifnotinfo/@c texi2dvi@ifnotinfo/
  689. s/^@end ifnotinfo/@c texi2dvi@end ifnotinfo/'
  690. # Uncommenting is simple: Remove any leading `@c texi2dvi'.
  691. uncomment_iftex='s/^@c texi2dvi//'
  692. # run_makeinfo ()
  693. # ---------------
  694. # Expand macro commands in the original source file using Makeinfo.
  695. # Always use `end' footnote style, since the `separate' style
  696. # generates different output (arguably this is a bug in -E). Discard
  697. # main info output, the user asked to run TeX, not makeinfo.
  698. run_makeinfo ()
  699. {
  700. case $language in
  701. texinfo)
  702. # Unless required by the user, makeinfo expansion is wanted only
  703. # if texinfo.tex is too old.
  704. if test "$expand" = t; then
  705. makeinfo=${MAKEINFO:-makeinfo}
  706. else
  707. # Check if texinfo.tex performs macro expansion by looking for
  708. # its version. The version is a date of the form YEAR-MO-DA.
  709. # We don't need to use [0-9] to match the digits since anyway
  710. # the comparison with $txiprereq, a number, will fail with non
  711. # digits.
  712. # Run in a temporary directory to avoid leaving files.
  713. version_test_dir=$t2ddir/version_test
  714. ensure_dir "$version_test_dir"
  715. (
  716. cd "$version_test_dir"
  717. echo '\input texinfo.tex @bye' >txiversion.tex
  718. # Be sure that if tex wants to fail, it is not interactive:
  719. # close stdin.
  720. tex txiversion.tex </dev/null >txiversion.out 2>txiversion.err
  721. )
  722. if test $? != 0; then
  723. cat $version_test_dir/txiversion.out
  724. cat $version_test_dir/txiversion.err >&2
  725. fatal 1 "texinfo.tex appears to be broken, quitting."
  726. fi
  727. eval `sed -n 's/^.*\[\(.*\)version \(....\)-\(..\)-\(..\).*$/txiformat=\1 txiversion="\2\3\4"/p' $version_test_dir/txiversion.out`
  728. verbose "texinfo.tex preloaded as \`$txiformat', version is \`$txiversion' ..."
  729. if test "$txiprereq" -le "$txiversion" >&6 2>&1; then
  730. makeinfo=
  731. else
  732. makeinfo=${MAKEINFO:-makeinfo}
  733. fi
  734. # As long as we had to run TeX, offer the user this convenience:
  735. test "$txiformat" = Texinfo && escape=@
  736. fi
  737. if test -n "$makeinfo"; then
  738. # in_src: the file with macros expanded.
  739. # Use the same basename to generate the same aux file names.
  740. work_src=$workdir/src
  741. ensure_dir "$work_src"
  742. in_src=$work_src/$in_base
  743. local $miincludes=`list_prefix includes -I`
  744. verbose "Macro-expanding $command_line_filename to $in_src ..."
  745. sed "$comment_iftex" "$command_line_filename" \
  746. | $makeinfo --footnote-style=end -I "$in_dir" $miincludes \
  747. -o /dev/null --macro-expand=- \
  748. | sed "$uncomment_iftex" >"$in_src"
  749. # Continue only if everything succeeded.
  750. if test $? -ne 0 \
  751. || test ! -r "$in_src"; then
  752. verbose "Expansion failed, ignored...";
  753. else
  754. in_input=$in_src
  755. fi
  756. fi
  757. ;;
  758. esac
  759. }
  760. # insert_commands ()
  761. # ------------------
  762. # Used most commonly for @finalout, @smallbook, etc.
  763. insert_commands ()
  764. {
  765. local textra_cmd
  766. case $language in
  767. latex) textra_cmd=1i;;
  768. texinfo) textra_cmd='/^@setfilename/a';;
  769. *) echo "$0: internal error, unknown language: $language" >&2;
  770. esac
  771. if test -n "$textra"; then
  772. # _xtr. The file with the user's extra commands.
  773. work_xtr=$workdir/xtr
  774. in_xtr=$work_xtr/$in_base
  775. ensure_dir "$work_xtr"
  776. verbose "Inserting extra commands: $textra"
  777. sed "$textra_cmd\\
  778. $textra" "$in_input" >"$in_xtr"
  779. in_input=$in_xtr
  780. fi
  781. }
  782. # run_recode ()
  783. # -------------
  784. # If this is a Texinfo file with a specified input encoding, and
  785. # recode is available, then recode to plain 7 bit Texinfo.
  786. run_recode ()
  787. {
  788. if test $language = texinfo; then
  789. pgm='s/^ *@documentencoding *\([^ ][^ ]*\) *$/\1/
  790. t found
  791. d
  792. :found
  793. q'
  794. encoding=`sed -e "$pgm" "$in_input"`
  795. if $recode && test -n "$encoding" && findprog recode; then
  796. verbose "Recoding from $encoding to Texinfo."
  797. # _rcd. The Texinfo file recoded in 7bit.
  798. work_rcd=$workdir/recode
  799. in_rcd=$work_rcd/$in_base
  800. ensure_dir "$work_rcd"
  801. if recode "$encoding"..texinfo <"$in_input" >"$in_rcd" \
  802. && test -s "$in_rcd"; then
  803. in_input=$in_rcd
  804. else
  805. verbose "Recoding failed, using original input."
  806. fi
  807. fi
  808. fi
  809. }
  810. # compute_language FILENAME
  811. # -------------------------
  812. # Return the short string describing the language in which FILENAME
  813. # is written: `texinfo' or `latex'.
  814. compute_language ()
  815. {
  816. # If the user explicitly specified the language, use that.
  817. # Otherwise, if the first line is \input texinfo, assume it's texinfo.
  818. # Otherwise, guess from the file extension.
  819. if test -n "$set_language"; then
  820. echo $set_language
  821. elif sed 1q "$1" | grep 'input texinfo' >&6; then
  822. echo texinfo
  823. else
  824. # Get the type of the file (latex or texinfo) from the given language
  825. # we just guessed, or from the file extension if not set yet.
  826. case $1 in
  827. *.ltx | *.tex | *.drv | *.dtx) echo latex;;
  828. *) echo texinfo;;
  829. esac
  830. fi
  831. }
  832. # run_to_html ()
  833. # --------------
  834. # Convert to HTML.
  835. run_to_html ()
  836. {
  837. hevea=${HEVEA:-hevea}
  838. # Compiling to the tmp directory enables to preserve a previous
  839. # successful compilation. Unfortunately it makes it hard to move
  840. # the image back to the destination directory. So compile to the
  841. # actual destination.
  842. local to_html="$hevea -fix -noiso -O -o '$out_name'"
  843. to_html="$to_html `list_prefix includes -I`"
  844. to_html="$to_html '$in_input'"
  845. verbose "running $to_html"
  846. if eval "$to_html" >&5; then :; else
  847. fatal 1 "$hevea exited with bad status, quitting."
  848. fi
  849. }
  850. # run_conversion ()
  851. # -----------------
  852. # Run the TeX tools until a fix point is reached.
  853. run_conversion ()
  854. {
  855. # Move to the working directory.
  856. if $tidy; then
  857. verbose "cd $work_build"
  858. cd "$work_build" || exit 1
  859. fi
  860. case $out_lang in
  861. dvi|pdf) run_tex_suite;;
  862. html ) run_to_html;;
  863. esac
  864. # In case $orig_pwd is on a different drive (for DOS).
  865. cd /
  866. # Return to the original directory so that
  867. # - the next file is processed in correct conditions
  868. # - the temporary file can be removed
  869. cd "$orig_pwd" || exit 1
  870. }
  871. ## ---------------------- ##
  872. ## Command line parsing. ##
  873. ## ---------------------- ##
  874. # Push a token among the arguments that will be used to notice when we
  875. # ended options/arguments parsing.
  876. # Use "set dummy ...; shift" rather than 'set - ..." because on
  877. # Solaris set - turns off set -x (but keeps set -e).
  878. # Use ${1+"$@"} rather than "$@" because Digital Unix and Ultrix 4.3
  879. # still expand "$@" to a single argument (the empty string) rather
  880. # than nothing at all.
  881. arg_sep="$$--$$"
  882. set dummy ${1+"$@"} "$arg_sep"; shift
  883. #
  884. # Parse command line arguments.
  885. while test x"$1" != x"$arg_sep"; do
  886. # Handle --option=value by splitting apart and putting back on argv.
  887. case "$1" in
  888. --*=*)
  889. opt=`echo "$1" | sed -e 's/=.*//'`
  890. val=`echo "$1" | sed -e 's/[^=]*=//'`
  891. shift
  892. set dummy "$opt" "$val" ${1+"$@"}; shift
  893. ;;
  894. esac
  895. # This recognizes --quark as --quiet. So what.
  896. case "$1" in
  897. -@ ) escape=@;;
  898. # Silently and without documentation accept -b and --b[atch] as synonyms.
  899. -b | --batch) batch=true;;
  900. --build) shift; build_mode=$1;;
  901. --build-dir) shift; build_dir=$1; build_mode=tidy;;
  902. -c | --clean) build_mode=clean;;
  903. -D | --debug) debug=true;;
  904. -e | -E | --expand) expand=t;;
  905. -h | --help) echo "$usage"; exit 0;;
  906. --html) out_lang=html;;
  907. -I | --I*)
  908. shift
  909. list_append includes "$1"
  910. ;;
  911. -l | --lang | --language) shift; set_language=$1;;
  912. -o | --out | --output)
  913. shift
  914. # Make it absolute, just in case we also have --clean, or whatever.
  915. oname=`absolute "$1"`;;
  916. -p | --pdf) out_lang=pdf;;
  917. -q | -s | --quiet | --silent) quiet=true; batch=true;;
  918. -r | --recode) recode=true;;
  919. -t | --texinfo | --command ) shift; textra="$textra\\
  920. "`echo "$1" | sed 's/\\\\/\\\\\\\\/g'`;;
  921. --tidy) build_mode=tidy;;
  922. -v | --vers*) echo "$version"; exit 0;;
  923. -V | --verb*) verb=true;;
  924. --) # What remains are not options.
  925. shift
  926. while test x"$1" != x"$arg_sep"; do
  927. set dummy ${1+"$@"} "$1"; shift
  928. shift
  929. done
  930. break;;
  931. -*)
  932. fatal 1 "Unknown or ambiguous option \`$1'." \
  933. "Try \`--help' for more information."
  934. ;;
  935. *) set dummy ${1+"$@"} "$1"; shift;;
  936. esac
  937. shift
  938. done
  939. # Pop the token
  940. shift
  941. # $tidy: compile in a t2d directory.
  942. # $clean: remove the t2d directory afterward.
  943. case $build_mode in
  944. local) clean=true; tidy=false;;
  945. tidy) clean=false; tidy=true;;
  946. clean) clean=true; tidy=true;;
  947. *) fatal 1 "invalid build mode: $build_mode";;
  948. esac
  949. # Interpret remaining command line args as filenames.
  950. case $# in
  951. 0)
  952. fatal 2 "Missing file arguments." "Try \`--help' for more information."
  953. ;;
  954. 1) ;;
  955. *)
  956. if test -n "$oname"; then
  957. fatal 2 "Can't use option \`--output' with more than one argument."
  958. fi
  959. ;;
  960. esac
  961. # Use absolute dir names in the includes.
  962. list_dir_to_abs includes
  963. # We can't do much without tex.
  964. #
  965. if findprog ${TEX:-tex}; then :; else cat <<EOM
  966. You don't have a working TeX binary (${TEX:-tex}) installed anywhere in
  967. your PATH, and texi2dvi cannot proceed without one. If you want to use
  968. this script, you'll need to install TeX (if you don't have it) or change
  969. your PATH or TEX environment variable (if you do). See the --help
  970. output for more details.
  971. For information about obtaining TeX, please see http://www.tug.org. If
  972. you happen to be using Debian, you can get it with this command:
  973. apt-get install tetex-bin
  974. EOM
  975. exit 1
  976. fi
  977. # We want to use etex (or pdftex) if they are available, and the user
  978. # didn't explicitly specify. We don't check for elatex and pdfelatex
  979. # because (as of 2003), the LaTeX team has asked that new distributions
  980. # use etex by default anyway.
  981. #
  982. # End up with the TEX and PDFTEX variables set to what we are going to use.
  983. if test -z "$TEX"; then
  984. if findprog etex; then TEX=etex; else TEX=tex; fi
  985. fi
  986. #
  987. if test -z "$PDFTEX"; then
  988. if findprog pdfetex; then PDFTEX=pdfetex; else PDFTEX=pdftex; fi
  989. fi
  990. # File descriptor usage:
  991. # 0 standard input
  992. # 1 standard output (--verbose messages)
  993. # 2 standard error
  994. # 3 some systems may open it to /dev/tty
  995. # 4 used on the Kubota Titan
  996. # 5 tools output (turned off by --quiet)
  997. # 6 tracing/debugging (set -x output, etc.)
  998. # Main tools' output (TeX, etc.) that TeX users are used to seeing.
  999. #
  1000. # If quiet, discard, else redirect to the message flow.
  1001. if $quiet; then
  1002. exec 5>/dev/null
  1003. else
  1004. exec 5>&1
  1005. fi
  1006. # Enable tracing, and auxiliary tools output.
  1007. #
  1008. # Should be used where you'd typically use /dev/null to throw output
  1009. # away. But sometimes it is convenient to see that output (e.g., from
  1010. # a grep) to aid debugging. Especially debugging at distance, via the
  1011. # user.
  1012. if $debug; then
  1013. exec 6>&1
  1014. set -x
  1015. else
  1016. exec 6>/dev/null
  1017. fi
  1018. #
  1019. # input_file_name_decode
  1020. # ----------------------
  1021. # Decode COMMAND_LINE_FILENAME, and compute:
  1022. # - COMMAND_LINE_FILENAME clean of TeX commands
  1023. # - IN_DIR
  1024. # The directory to the input file, possibly absolute if needed.
  1025. # - IN_DIR_ABS
  1026. # The absolute directory of the input file.
  1027. # - IN_BASE
  1028. # The input file base name (no directory part).
  1029. # - IN_NOEXT
  1030. # The input file name without extensions (nor directory part).
  1031. # - IN_INPUT
  1032. # Defaults to COMMAND_LINE_FILENAME, but might change if the
  1033. # input is preprocessed (recode etc.). With directory, possibly absolute.
  1034. input_file_name_decode ()
  1035. {
  1036. # See if we are run from within AUC-Tex, in which case we are
  1037. # passed `\input{FOO.tex}' or even `\nonstopmode\input{FOO.tex}'.
  1038. case $command_line_filename in
  1039. *\\nonstopmode*)
  1040. batch=true;;
  1041. esac
  1042. case $command_line_filename in
  1043. *\\input{*}*)
  1044. # Let AUC-TeX error parser deal with line numbers.
  1045. line_error=false
  1046. command_line_filename=`\
  1047. expr X"$command_line_filename" : X'.*input{\([^}]*\)}'`
  1048. ;;
  1049. esac
  1050. # If the COMMAND_LINE_FILENAME is not absolute (e.g., --debug.tex),
  1051. # prepend `./' in order to avoid that the tools take it as an option.
  1052. echo "$command_line_filename" | $EGREP '^(/|[A-z]:/)' >&6 \
  1053. || command_line_filename="./$command_line_filename"
  1054. # See if the file exists. If it doesn't we're in trouble since, even
  1055. # though the user may be able to reenter a valid filename at the tex
  1056. # prompt (assuming they're attending the terminal), this script won't
  1057. # be able to find the right xref files and so forth.
  1058. test -r "$command_line_filename" ||
  1059. fatal 1 "cannot read $command_line_filename, skipping."
  1060. # Get the name of the current directory.
  1061. in_dir=`func_dirname "$command_line_filename"`
  1062. in_dir_abs=`absolute "$in_dir"`
  1063. # In a clean build, we `cd', so get an absolute file name.
  1064. $tidy && in_dir=$in_dir_abs
  1065. # Strip directory part but leave extension.
  1066. in_base=`basename "$command_line_filename"`
  1067. # Strip extension.
  1068. in_noext=`echo "$in_base" | sed 's/\.[^.]*$//'`
  1069. # The normalized file name to compile. Must always point to the
  1070. # file to actually compile (in case of recoding, macro-expansion etc.).
  1071. in_input=$in_dir/$in_base
  1072. # Compute the output file name.
  1073. if test x"$oname" != x; then
  1074. out_name=$oname
  1075. else
  1076. out_name=$in_noext.$out_lang
  1077. fi
  1078. out_dir=`func_dirname "$out_name"`
  1079. out_dir_abs=`absolute "$out_dir"`
  1080. out_base=`basename "$out_name"`
  1081. out_noext=`echo "$out_base" | sed 's/\.[^.]*$//'`
  1082. }
  1083. ## -------------- ##
  1084. ## TeXify files. ##
  1085. ## -------------- ##
  1086. for command_line_filename in ${1+"$@"}; do
  1087. verbose "Processing $command_line_filename ..."
  1088. input_file_name_decode
  1089. # An auxiliary directory used for all the auxiliary tasks involved
  1090. # in compiling this document.
  1091. case $build_dir in
  1092. '' | . ) t2ddir=$out_noext.t2d ;;
  1093. *) # Avoid collisions between multiple occurrences of the same
  1094. # file.
  1095. t2ddir=$build_dir/`echo "$out_dir_abs/$out_noext.t2d" | sed 's,/,!,g'`
  1096. esac
  1097. # Remove it at exit if clean mode.
  1098. $clean &&
  1099. trap "cd / && rm -rf $t2ddir" 0 1 2 15
  1100. ensure_dir "$build_dir" "$t2ddir"
  1101. # We will change directory, better work with an absolute path...
  1102. t2ddir=`absolute "$t2ddir"`
  1103. # Sometimes there are incompatibilities between auxiliary files for
  1104. # DVI and PDF. The contents can also change whether we work on PDF
  1105. # and/or DVI. So keep separate spaces for each.
  1106. workdir=$t2ddir/$out_lang
  1107. ensure_dir "$workdir"
  1108. # _build. In a tidy build, where the auxiliary files are output.
  1109. if $tidy; then
  1110. work_build=$workdir/build
  1111. else
  1112. work_build=.
  1113. fi
  1114. # _bak. Copies of the previous auxiliary files (another round is
  1115. # run if they differ from the new ones).
  1116. work_bak=$workdir/bak
  1117. # Make those directories.
  1118. ensure_dir "$work_build" "$work_bak"
  1119. # Source file might include additional sources.
  1120. # We want `.:$orig_pwd' before anything else. (We'll add `.:' later
  1121. # after all other directories have been turned into absolute paths.)
  1122. # `.' goes first to ensure that any old .aux, .cps,
  1123. # etc. files in ${directory} don't get used in preference to fresher
  1124. # files in `.'. Include orig_pwd in case we are in clean build mode, where
  1125. # we've cd'd to a temp directory.
  1126. txincludes=`list_infix includes $path_sep`
  1127. common="$orig_pwd$path_sep$in_dir$path_sep$txincludes$path_sep"
  1128. for var in $tex_envvars; do
  1129. eval val="\$common\$${var}_orig"
  1130. # Convert relative paths to absolute paths, so we can run in another
  1131. # directory (e.g., in clean build mode, or during the macro-support
  1132. # detection).
  1133. val=`absolute_filenames "$val"`
  1134. eval $var="$val"
  1135. eval export $var
  1136. eval verbose "$var=\'\$${var}\'"
  1137. done
  1138. # `texinfo' or `latex'?
  1139. language=`compute_language $command_line_filename`
  1140. # --expand
  1141. run_makeinfo
  1142. # --command, --texinfo
  1143. insert_commands
  1144. # --recode
  1145. run_recode
  1146. # Run until a fix point is reached.
  1147. run_conversion
  1148. # Remove temporary files.
  1149. if $clean; then
  1150. verbose "Removing $t2ddir"
  1151. rm -rf "$t2ddir"
  1152. fi
  1153. done
  1154. verbose "done."
  1155. exit 0 # exit successfully, not however we ended the loop.