| 1 | #!/bin/bash |
| 2 | |
| 3 | version=2.04 |
| 4 | myname=$(basename "$0") |
| 5 | |
| 6 | <<'DOC' |
| 7 | = ltxfileinfo - print version information for a LaTeX file |
| 8 | |
| 9 | = Synopsis |
| 10 | ltxfileinfo [options] filename |
| 11 | |
| 12 | Options: |
| 13 | -h,--help print this help and exit |
| 14 | -H,--Help print full documentation via less and exit |
| 15 | -V print this script's version and exit |
| 16 | -d,--date print file's date only |
| 17 | -v,--version print file's version only |
| 18 | -i,--info print file's description text only |
| 19 | -l,--location print file's full path only (same output as kpsewhich) |
| 20 | -f,--flat output as 1 line with 4 tab-separated file, date, version and info fields |
| 21 | -s,--star mark mal-formatted data with a star |
| 22 | -c,--color mark mal-formatted data with red color (ANSI coloring) |
| 23 | -D,--debug for debugging, keep temporary files |
| 24 | |
| 25 | = Description |
| 26 | ltxfileinfo displays version information for LaTeX files. If no path |
| 27 | information is given, the file is searched using kpsewhich. As an extra, |
| 28 | for developers, the script will (use the |--star| or |--color| options) |
| 29 | check the valididity of the |\Provides...| statement in the files. |
| 30 | The script uses code from Uwe Lück's |readprov.sty|. |
| 31 | |
| 32 | Without an option, the output will be of the form: |
| 33 | $ ltxfileinfo ctable.sty |
| 34 | name: ctable.sty |
| 35 | date: 2012/08/23 |
| 36 | vers: v1.24 |
| 37 | info: Easy, key=value directed, option-rich, typesetting of floats |
| 38 | loca: /usr/local/texlive/2012/../texmf-local/tex/latex/ctable/ctable.sty |
| 39 | |
| 40 | Missing information is represented by |--|: |
| 41 | $ ltxfileinfo yhmath.sty |
| 42 | name: yhmath.sty |
| 43 | date: -- |
| 44 | vers: -- |
| 45 | info: -- |
| 46 | loca: /usr/local/texlive/2012/texmf-dist/tex/latex/yhmath/yhmath.sty |
| 47 | |
| 48 | ltxfileinfo tries to detect (and report) mal-formatted dates and versions; |
| 49 | use the |--star| option to mark such fields with a star, of use the |--color| |
| 50 | option to color invalid fields red, if you work in a terminal with ANSI-coloring |
| 51 | capabilities. |
| 52 | These options are useful for developers who want to check the correctness |
| 53 | of their |\Provides...| statements. For example: |
| 54 | |
| 55 | $ ltxfileinfo -s arfonts.sty |
| 56 | file: ARfonts.sty* (case difference in \Provides statement) |
| 57 | date: 2006/01/01 |
| 58 | vers: --* |
| 59 | info: Part of the Arabi package |
| 60 | loca: /usr/local/texlive/2012/texmf-dist/tex/latex/arabi/arfonts.sty |
| 61 | |
| 62 | We see here, that the |\ProvidesPackage| statement has an incorrect first |
| 63 | argument and has no version information. |
| 64 | |
| 65 | = Other output formats |
| 66 | With the |--date| option, only the file's date will be shown, unlabeled. |
| 67 | The |--version|, |--location|, |--info| options are treated analogously. |
| 68 | |
| 69 | $ ltxfileinfo -v chronology.sty |
| 70 | v1.0 |
| 71 | |
| 72 | The |--flat| option prints the fields (except loca:, the last field) on one |
| 73 | line, unlabeled and tab-separated: |
| 74 | |
| 75 | $ ltxfileinfo --star --flat chronology.sty |
| 76 | chronology.sty 2010/6/12* v1.0 Horizontal timeline |
| 77 | |
| 78 | The date is starred, because its format is not yyyy/mm/dd (and the |--star| |
| 79 | option is given.) |
| 80 | |
| 81 | = Bugs |
| 82 | |
| 83 | On my system, I have a total of 8699 kpsewhich-detectable files that contain a |\Provides...| statement. |
| 84 | I ran them all through |ltxfileinfo| and made the following summary of detected errors: |
| 85 | |
| 86 | 8695 Total \Provides... containing files tested |
| 87 | |
| 88 | 660 \Provides... could not be interpreted; reason: |
| 89 | 160 Argument of \Provides... not equal to file's name |
| 90 | 8 Latex3 package (not handled yet) |
| 91 | 480 Unidentified problem with \ProvidesPackage statement |
| 92 | 12 \Provides... used in file without a ... extension |
| 93 | |
| 94 | 8035 Files could be evaluated |
| 95 | 482 had no date |
| 96 | 95 had a mal-formatted date |
| 97 | 2666 had no version |
| 98 | 357 had a mal-formatted version |
| 99 | 765 had a \Provides... first argument different from the filename |
| 100 | |
| 101 | The .dtx files have more problems than other files: |
| 102 | |
| 103 | 1345 .dtx files: |
| 104 | |
| 105 | 320 \Provides... could not be interpreted; reason: |
| 106 | 81 Argument of \Provides... not equal to... |
| 107 | 6 Latex3 package (not handled yet) |
| 108 | 233 Unidentified problem with \ProvidesPackage statement |
| 109 | |
| 110 | 1025 Files could be evaluated |
| 111 | 49 had no date |
| 112 | 15 had a mal-formatted date |
| 113 | 99 had no version |
| 114 | 52 had a mal-formatted version |
| 115 | 468 had a \Provides... first argument different from the filename |
| 116 | |
| 117 | = Author and copyright |
| 118 | Author Wybo Dekker |
| 119 | Email U{[email protected]}{[email protected]} |
| 120 | License Released under the U{www.gnu.org/copyleft/gpl.html}{GNU General Public License} |
| 121 | DOC |
| 122 | |
| 123 | die() { echo -e "$myname: $Err${*}$Nor" 1>&2; exit 1; } |
| 124 | help() { sed -n '/^= Synopsis/,/^= /p' "$0"|sed '1s/.*/Usage:/;/^= /d'; exit; } |
| 125 | helpall() { sed -n '/^<<.DOC.$/,/^DOC$/p' "$0"|sed -n '1d;$d;p'|less; exit; } |
| 126 | version() { echo $version; exit; } |
| 127 | install() { which instscript>&/dev/null && instscript --zip --pdf --markdown "$myname"; exit; } |
| 128 | |
| 129 | Nor='\e[0m' # reset color ] |
| 130 | Err='\e[31;1m' # light red ] |
| 131 | [[ ${BASH_VERSINFO[0]} -ge 4 ]] || die "Need bash version >= 4 (you have $BASH_VERSION)" |
| 132 | |
| 133 | # remove leading and trailing whitespace from a string |
| 134 | function trim { |
| 135 | local var="$*" |
| 136 | var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace |
| 137 | var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace |
| 138 | echo -n "$var" |
| 139 | } |
| 140 | |
| 141 | # mark a string by appending a * (--star and --color options) |
| 142 | function mark { |
| 143 | echo "$mark1${*}$mark2" |
| 144 | } |
| 145 | |
| 146 | # special treatment for .mbs and .bst files: |
| 147 | function dombsbst { |
| 148 | # look at the first \ProvidesFile only: |
| 149 | out="File: "$(grep '\ProvidesFile{' "$loca" |head -1 |sed 's/^[ %]*\\ProvidesFile{//;s/}\[/ /;s/\]$//') |
| 150 | } |
| 151 | |
| 152 | # unpack the fields |
| 153 | function extract { |
| 154 | shift # remove File: |
| 155 | file=$1 |
| 156 | shift # remove filename |
| 157 | if [[ -z $1 ]]; then |
| 158 | date=--; vers=--; info=--; return |
| 159 | elif [[ $1 =~ ^[0-9]+/[0-9]+/[0-9]+ ]]; then |
| 160 | date="$1" |
| 161 | shift |
| 162 | else |
| 163 | date="--" |
| 164 | fi |
| 165 | if [[ -z $1 ]]; then |
| 166 | vers=--; info=--; return |
| 167 | elif [[ $1 =~ ^v?[[:digit:]][.[:digit:]]+[a-z]* ]]; then |
| 168 | vers="$1" |
| 169 | shift |
| 170 | info="${*:---}" |
| 171 | return |
| 172 | else |
| 173 | fixed=false |
| 174 | # sometimes the version is of the form v.1.3 or vers:1.3 or such: |
| 175 | for i in version ver. ver: ver v. V v; do |
| 176 | if [[ $1 =~ $i[[:digit:]][.[:digit:]]*[a-z]* ]]; then |
| 177 | vers="$1" |
| 178 | shift |
| 179 | fixed=true |
| 180 | break |
| 181 | fi |
| 182 | done |
| 183 | if ! $fixed; then |
| 184 | # sometimes the version is reported as "v 1.3" or "vers: 1.3" or such: |
| 185 | for i in version ver: ver v. V v; do |
| 186 | if [[ $1 == "$i" ]]; then |
| 187 | if [[ $2 =~ [[:digit:]][.[:digit:]]+[a-z]* ]]; then |
| 188 | vers="$1 $2" |
| 189 | shift 2 |
| 190 | fixed=true |
| 191 | break |
| 192 | fi |
| 193 | fi |
| 194 | done |
| 195 | fi |
| 196 | info="${*:---}" |
| 197 | if ! $fixed; then |
| 198 | vers=-- |
| 199 | fi |
| 200 | fi |
| 201 | } |
| 202 | |
| 203 | # display the results |
| 204 | function display { |
| 205 | # file should be equal to arg (the argument) |
| 206 | if [[ $file != "$arg" ]] && [[ -n $mark2 ]]; then |
| 207 | file="$(mark "$file") (case difference in \\Provides statement)" |
| 208 | fi |
| 209 | |
| 210 | # to be correct, date must be of the form yyyy/mm/dd |
| 211 | if [[ $date == -- ]]; then |
| 212 | date="$(mark $date) (file modification date: $(stat -c %z "$loca"|sed -e 's/\..*//'))" |
| 213 | elif [[ -n $mark2 ]]; then |
| 214 | local wrong=false add='' |
| 215 | if [[ ! $date =~ ^[[:digit:]]{4}/[[:digit:]]{2}/[[:digit:]]{2}$ ]]; then |
| 216 | add+=" (wrong format)" |
| 217 | wrong=true |
| 218 | fi |
| 219 | if [ "$(date -d "$date" +%arg 2>&1 | grep invalid)" != "" ]; then |
| 220 | # even if it matches, the date must be valid: |
| 221 | add+=" (invalid date)" |
| 222 | wrong=true |
| 223 | fi |
| 224 | if $wrong; then date="$(mark "$date")$add"; fi |
| 225 | fi |
| 226 | |
| 227 | # version should be 1.2 or 1.2.3, maybe prefixed with v and suffixed with zero or more lower case letters |
| 228 | if [[ ! $vers =~ ^v?[[:digit:]]+.[[:digit:]]+(.[[:digit:]]+)?[[:lower:]]*$ ]]; then vers=$(mark $vers); fi |
| 229 | |
| 230 | if [ ! "$key" ]; then |
| 231 | if $flat; then |
| 232 | echo -e "$file\t$date\t$vers\t$info" |
| 233 | else |
| 234 | cat <<-EOF |
| 235 | file: $file |
| 236 | date: $date |
| 237 | vers: $vers |
| 238 | info: $info |
| 239 | loca: $loca |
| 240 | EOF |
| 241 | fi |
| 242 | else |
| 243 | eval "echo \$$key" |
| 244 | fi |
| 245 | } |
| 246 | |
| 247 | if ! options=$(getopt\ |
| 248 | -n "$myname" \ |
| 249 | -o hHiVIdvlifcsD \ |
| 250 | -l help,Help,date,version,location,info,flat,star,color,debug -- "$@" |
| 251 | ); then exit 1; fi |
| 252 | eval set -- "$options" |
| 253 | debug=false flat=false |
| 254 | while [ $# -gt 0 ]; do |
| 255 | case $1 in |
| 256 | (-h|--help) # print this help and exit |
| 257 | help |
| 258 | ;; |
| 259 | (-H|--Help) # print full documentation via less and exit |
| 260 | helpall |
| 261 | ;; |
| 262 | (-V) version |
| 263 | ;; |
| 264 | (-d|--date) # print file's date only |
| 265 | key=date |
| 266 | ;; |
| 267 | (-v|--version) # print file's version only |
| 268 | key=vers |
| 269 | ;; |
| 270 | (-i|--info) # print file's description text only |
| 271 | key=info |
| 272 | ;; |
| 273 | (-l|--location) # print file's full path only (same output as kpsewhich) |
| 274 | key=loca |
| 275 | ;; |
| 276 | (-f|--flat) # output as 1 line with 4 tab-separated file, date, version and info fields |
| 277 | flat=true |
| 278 | ;; |
| 279 | (-s|--star) # mark mal-formatted data with a star |
| 280 | mark1='' |
| 281 | mark2='*' |
| 282 | ;; |
| 283 | (-c|--color) # mark mal-formatted data with red color (ANSI coloring) |
| 284 | mark1='[1;31m' |
| 285 | mark2='[0m' |
| 286 | ;; |
| 287 | (-D|--debug) # for debugging, keep temporary files |
| 288 | debug=true |
| 289 | ;; |
| 290 | (-I) install |
| 291 | ;; |
| 292 | (--) shift |
| 293 | break |
| 294 | ;; |
| 295 | (*) break |
| 296 | ;; |
| 297 | esac |
| 298 | shift |
| 299 | done |
| 300 | |
| 301 | arg=$1 |
| 302 | case $arg in |
| 303 | (*/*) loca=$arg |
| 304 | [[ -e $loca ]] || die "$loca: file does not exist";; |
| 305 | ('') help;; |
| 306 | (*) loca=$(kpsewhich "$arg") || die "$arg: not found by kpsewhich" |
| 307 | esac |
| 308 | loca=$(kpsewhich "$arg") |
| 309 | prov=$(grep '\\Provides' "$loca") || die "no \\Provides..." |
| 310 | [[ $prov =~ ExplPackage ]] && die 'Latex3 package (not handled yet)' |
| 311 | |
| 312 | # the file must contain a \ProvidesXXX statement, where XXX is Class, Package, or File: |
| 313 | [[ $prov =~ \\Provides(Class|Package|File)[[:space:]]*\{ ]] || |
| 314 | die "Found no \\ProvidesClass/Package/File statement" |
| 315 | |
| 316 | # readprov.sty does not work on .mbs and .bst files: special treatment; |
| 317 | # \ProvideFile statements in them mostly refer to merlin.mbs, or other names |
| 318 | [[ $arg =~ \.(mbs|bst)$ ]] && dombsbst |
| 319 | |
| 320 | dir=$(mktemp -d -t "$myname.XXXXXXXXXX") |
| 321 | if $debug; then |
| 322 | echo "Running in $dir" |
| 323 | else |
| 324 | trap 'rm -rf $dir' 0 1 2 15 |
| 325 | fi |
| 326 | cp "$loca" "$dir" |
| 327 | cd "$dir" || die "Could not cd to $dir" |
| 328 | |
| 329 | # The following code is mostly from Uwe Lueck's readprov.sty: |
| 330 | printf %s ' |
| 331 | \makeatletter |
| 332 | \def\GetFileInfo#1{% |
| 333 | \def\filename{#1}% |
| 334 | \def\@tempb##1 ##2 ##3\relax##4\relax{% |
| 335 | \def\filedate{##1}% |
| 336 | \def\fileversion{##2}% |
| 337 | \def\fileinfo{##3}}% |
| 338 | \read@file@info\@tempb{#1}} |
| 339 | \newcommand*{\read@file@info}[2]{% |
| 340 | \expandafter \expandafter \expandafter |
| 341 | #1\csname ver@#2\endcsname \relax? ? \relax\relax} |
| 342 | \newcommand*{\ReadFileInfos}[1]{% |
| 343 | \begingroup |
| 344 | \let\RP@@provfile\@providesfile |
| 345 | \def\@providesfile##1[##2]{\RP@@provfile{##1}[{##2}]\endinput}% |
| 346 | \def\ProvidesClass ##1{\ProvidesFile{##1.\@clsextension}}% |
| 347 | \def\ProvidesPackage##1{\ProvidesFile{##1.\@pkgextension}}% |
| 348 | \@for\@tempa:=#1\do{% |
| 349 | \edef\@tempa{\expandafter\read@no@spaces\@tempa\@nil}% |
| 350 | \input{\@tempa}% |
| 351 | \global\let\@gtempa\@tempa}% |
| 352 | \endgroup |
| 353 | \GetFileInfo\@gtempa% |
| 354 | } |
| 355 | \def\read@no@spaces#1#2\@nil{#1#2}% |
| 356 | \def\NeedsTeXFormat#1{\expandafter\@needsformat} |
| 357 | \ReadFileInfos{'"$loca"'} |
| 358 | \endinput |
| 359 | ' > ltxfileinfo.tex |
| 360 | |
| 361 | pdflatex -interaction=batchmode ltxfileinfo.tex >& /dev/null |
| 362 | |
| 363 | IFS= # do not remove any whitespace |
| 364 | base=${arg##*/} # strip the path |
| 365 | base=${base%.*} # strip the extension |
| 366 | shopt -s nocasematch # MS people don't pay attention to case differences in file names... |
| 367 | while read -r line; do |
| 368 | if [[ $line =~ ^File:.$base ]]; then |
| 369 | out="$line" |
| 370 | while [ ${#line} -eq 79 ]; do # gather continuation lines |
| 371 | read -r line |
| 372 | out="$out$line" |
| 373 | done |
| 374 | fi |
| 375 | done <ltxfileinfo.log |
| 376 | IFS=' ' # back to |
| 377 | shopt -u nocasematch # normal |
| 378 | |
| 379 | # if the \Provides statement is not recognized, try to find out why, and die: |
| 380 | if [[ -z $out ]]; then |
| 381 | read -r provtype provarg <<<"$(sed -n '/\Provides\(Class\|Package\|File\)\s*{[^}]*}/s/.*\(Class\|Package\|File\)\s*{\([^}]*\)}.*/\1 \2/p' "$loca")" |
| 382 | if [[ -z $provarg || $provarg =~ \\ ]]; then die Unidentified problem with \\ProvidesPackage statement; fi |
| 383 | case $provtype in |
| 384 | (Package) |
| 385 | if [[ ! $arg =~ \.(sty|dtx)$ ]]; then |
| 386 | die \\ProvidesPackage used in file without a .sty extension |
| 387 | elif [[ $provarg =~ \.sty$ ]]; then |
| 388 | die "Argument of \\ProvidesPackage ($provarg) should not contain .sty extension" |
| 389 | elif [[ $provarg != "$base" ]]; then |
| 390 | die "Argument of \\ProvidesPackage ($provarg) not equal to file's base name ($base)" |
| 391 | else |
| 392 | die Unidentified problem with \\ProvidesPackage statement |
| 393 | fi |
| 394 | ;; |
| 395 | (Class) |
| 396 | if [[ ! $arg =~ \.(cls|dtx)$ ]]; then |
| 397 | die \\ProvidesClass used in file without a .cls extension |
| 398 | elif [[ $provarg =~ \.cls$ ]]; then |
| 399 | die "Argument of \\ProvidesClass ($provarg) should not contain .cls extension" |
| 400 | elif [[ $provarg != "$base" ]]; then |
| 401 | die "Argument of \\ProvidesClass ($provarg) not equal to file's base name ($base)" |
| 402 | else |
| 403 | die Unidentified problem with \\ProvidesPackage statement |
| 404 | fi |
| 405 | ;; |
| 406 | (File) |
| 407 | if [[ $arg != "$provarg" ]]; then |
| 408 | die "Argument of \\ProvidesFile ($provarg) not equal to file's name ($arg)" |
| 409 | else |
| 410 | die Unidentified problem with \\ProvidesPackage statement |
| 411 | fi |
| 412 | ;; |
| 413 | (*) |
| 414 | die "Found \\Provides$provarg statement - cannot handle it" |
| 415 | ;; |
| 416 | esac |
| 417 | fi |
| 418 | |
| 419 | out=($out) |
| 420 | extract "${out[@]}" |
| 421 | display |