summaryrefslogtreecommitdiff
path: root/support/proof/proof
blob: 0af73fc017fdc0312e9586ebf816f6dd8e5a03a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#!/bin/bash

# $Id: proof,v 1.2 2002/09/30$

################################################################################
#            Proof - A simple but smart editor interface to common             #
#              TeX/LaTeX and METAFONT/MetaPost processing tasks                #
#                                                                              #
#                    (Copyright (C) 2002 Klaus Bosau)                          #
################################################################################

function info
{
echo \
"Usage: proof [-P] [-abfp123xyzs] [-u routine] file
   or: proof -T file
   or: proof [-hv]

Process and view TeX, LaTeX, METAFONT and MetaPost sourcecode  

  -P   enter preview mode
  -T   leave preview mode and exit

  -a   leave auxiliary files untouched
  -b   create back-up file
  -f   use formatfile 'mylatex'
  -p   use PostScript-Type-1 fonts
  -n   METAFONT proofing mode n, where n can be 1, 2 or 3
  -x   generate dvi-file
  -y   generate ps-file (default)
  -z   generate pdf-file
  -u   call userdefined conversion routine
  -s   don't call viewer

  -h   print this help message
  -v   print version information

Options -abfp123xyzus may be handed over also in the sourcecode itself.
(see Proof manual page on how to do this) However, if you'd like to make
use of this feature, you should keep in mind that this would make both ad-
ditional small letter commandline options (except -h and -v) and existing
environmental settings ineffective.

Please send bug reports or suggestions to kbosau@web.de."
}

configfile=~/.proofrc                      # default configuration file

function error
{
  echo -e "--> ${0##*/}: $1"
  [ "$2" ] && { [ "$action" = 'watch' ] && read; exit 1; }
}

[ ! -e $configfile ] && error "Can't find configuration file!" halt

# global initializations

. $configfile

optionlist=':PTfbap123xyzu:sv'             # 'getopts' option list
nfsscmd='\table'                           # 'nfssfont' test command
FORMATFILEDIR=$(eval echo $FORMATFILEDIR)  # assign absolute path

# dvips
if [ "${DVI2PS%% *}" = 'dvips' ]; then
  PDF_OPT='-P pdf -G0'                     # enable distilling to PDF
  PSFONTS_OPT='-P cmz -P amz'              # use PostScript-Type-1 fonts
fi

# TeX
comp='tex'
init='tex --ini &latex mylatex.ltx'
virt='virtex --fmt latex'
myvirt='virtex --fmt mylatex'
formatfile='latex.fmt'
myformatfile='mylatex.fmt'
if [ "$USE_ETEX" = 'yes' ]; then
  comp='etex'
  init='etex --ini &elatex mylatex.ltx'
  virt='evirtex --efmt elatex'
  myvirt='evirtex --efmt mylatex'
  formatfile='elatex.efmt'
  myformatfile='mylatex.efmt'
fi

# pdfTeX
pdfcomp='pdftex'
pdfvirt='pdfvirtex --fmt pdflatex'
if [ "$USE_ETEX" = 'yes' ]; then
  pdfcomp='pdfetex'
  pdfvirt='pdfevirtex --efmt pdfelatex'
fi


function option  # checks if option is set
{
  local opt

  function opts
  {
    local line

    if line=$(head -1 $_source$_ext | grep '(-'); then
      echo $line | sed 's/^.*(\(-[^)]*\)).*/\1/'  # document
    elif [ "$OPTS" ]; then
      echo $OPTS                                  # commandline
    else
      echo $PROOFOPTS                             # environment
    fi
  }

  [ ! -e $_source$_ext ] && return 1

  if [ "$1" = 'y' ]; then
    ! option x && ! option z && return 0  # enables default output filetype
  else
    OPTIND=1
    while getopts $optionlist opt $(opts); do
      [ $opt = $1 ] && return 0
    done
  fi
  return 1
}


function tex2dvi
{
  local cmd=$comp fdir=${FORMATFILEDIR:-$PWD}

  function _mv
  {
    [ $2 = $PWD ] && return 0
    mv $1 $2
  }

  if grep '^\(\\documentclass\|\\documentstyle\)' >/dev/null; then
    cmd=$virt  # LaTeX source
    if option f; then
      if [ ! -e $fdir/$myformatfile ]; then
        echo "${0##*/}: Building formatfile..."
        [ ! -d $fdir ] && \
          { error "Can't find directory $fdir!"; return 1; }
        $init $_source && _mv $myformatfile $fdir || \
          { error 'Unable to install formatfile!'; return 1; }
        rm -f mylatex.log; new_ff=yes
      fi
      cmd=$myvirt  # read formatfile
    fi
  fi
  $cmd --interaction=$REPORTMODE $1
} <$_source$_ext


function tex2pdf
{
  local cmd=$pdfcomp
  grep '^\(\\documentclass\|\\documentstyle\)' >/dev/null && cmd=$pdfvirt
  $cmd --interaction=$REPORTMODE $1
} <$_source$_ext


function pid  # returns PID's matching given pattern
{
  $PROCSTAT | grep -v 'grep' | grep "$*" | sed 's/^\ *\([0-9]*\)\ .*$/\1/'
}


function view_file
{
  local cmd

  function tell  # starts process only if not yet running (untested)
  {
    # force reread
    [ -n "$(pid ghostview $2)" ] && return 0
    kill -SIGUSR1 $(pid xdvi.bin $2) >/dev/null 2>&1 && return 0
    kill -SIGHUP $(pid gv $2) >/dev/null 2>&1 && return 0

    # start anew
    $* &
  }

  case "${1##*.}" in
    dvi)  cmd=$DVI_VIEWER; last=x;;
    ps )  cmd=$PS_VIEWER; last=y;;
    pdf)  cmd=$PDF_VIEWER; last=z;;
  esac
  option s || tell $cmd $1
}


function view  # conversion and viewing
{

  local dvi2ps=$DVI2PS

function mk_test
{
$virt --interaction=$REPORTMODE $1 <<EOF
  $(echo $2 | tr ':' '\012')
EOF
if option x; then
  view_file $1.dvi
else
  $dvi2ps $1.dvi && \
  view_file $1.ps
fi
}

  [ $TERM != 'xterm' ] && { error 'No graphical user interface!'; return 1; }

  [ "$last" ] && { option $last || cleanup; }

  option p && dvi2ps="$dvi2ps $PSFONTS_OPT"

  if option u; then
    if type $OPTARG &>/dev/null; then
      $OPTARG $_source && view_file $_source.${ftypes##*->}
    else
      error "Can't find conversion routine $OPTARG!"
    fi
    return
  fi

  case $_ext in

    .mf )  if option 2; then
             mk_test nfssfont "${_source}:${nfsscmd}:\bye"
             ftypes='*pk,tfm->'
           elif option 3; then
             mk_test fontsmpl "${_source}"
           else
             $MF --interaction=$REPORTMODE "\mode=proof; input $_source;" && \
             gftodvi $_source.*gf && \
             if option x; then
               view_file $_source.dvi
               ftypes='log,*gf,*pk,tfm,dvi->'
             else
               $dvi2ps $_source.dvi && \
               view_file $_source.ps
               ftypes='log,*gf,*pk,tfm,dvi,ps->'
             fi
           fi;;

    .mp )  $MP --interaction=$REPORTMODE $_source && \
           tex2dvi "mproof $(ls $_source.[0-9]*)" && \
           if option x; then
             view_file mproof.dvi
           else
             $dvi2ps mproof.dvi && \
             view_file mproof.ps
           fi
           ftypes='log,mpx->[0-9]';;

    .tex)  if option x; then
             tex2dvi $_source.tex && \
             view_file $_source.dvi
             ftypes='log,aux->dvi'
           elif option z; then
             case "$TEXPDFMODE" in
               2)  tex2dvi $_source.tex && \
                   $DVI2PDF $_source.dvi
                   ftypes='log,aux,dvi->pdf';;
               3)  tex2dvi $_source.tex && \
                   $DVI2PS $PDF_OPT $_source.dvi && \
                   $PS2PDF $_source.ps
                   ftypes='log,aux,dvi->pdf';;
               *)  tex2pdf $_source.tex
                   ftypes='log,aux->pdf';;
             esac && view_file $_source.pdf
           else
             tex2dvi $_source.tex && \
             $dvi2ps $_source.dvi && \
             view_file $_source.ps
             ftypes='log,dvi,aux,idx->ps'
           fi;;

    .dvi)  if option y; then
             $dvi2ps $_source.dvi && \
             view_file $_source.ps
             ftypes='->ps'
           elif option z; then
             case "$DVIPDFMODE" in
               2)  $DVI2PS $PDF_OPT $_source.dvi && \
                   $PS2PDF $_source.ps
                   ftypes='ps->pdf';;
               *)  $DVI2PDF $_source.dvi
                   ftypes='->pdf';;
             esac && view_file $_source.pdf
           else
             view_file $_source.dvi
           fi;;

    .ps )  if option z; then
             $PS2PDF $_source.ps && \
             view_file $_source.pdf
             ftypes='->pdf'
           else
             view_file $_source.ps
           fi;;

    .pdf)  view_file $_source.pdf;;

  esac

}


function cleanup
{
  local aux all ign="\($_ext\|,v\|~\)$" files

  # clean up!
  aux=${ftypes%->*}
  ! option a && [ "$aux" ] && eval rm -f "$_source.{$aux,zzz}"
  rm -f {fontsmpl,nfssfont,mproof}.* tmp*

  # list files!
  all="\($(echo $ftypes | tr -d '*' | sed 's/->/,/g' | \
      sed 's/\(^,\|,$\)//;s/,/\\\|/g')\)$"
  if [ "$ftypes" ] && files=$(ls $_source.* | grep -v "$ign" | grep "$all"); then
    echo "${0##*/}: $PWD <-- "$files
  else
    echo "${0##*/}: no output"
  fi
  [ "$new_ff" ] && echo "${0##*/}: ${FORMATFILEDIR:-$PWD} <-- $myformatfile"

  # ftypes='aux,dvi->ps' --> 
  # --> delete {aux,dvi} if requested and show {aux,dvi,ps} if found
}


function backup
{
  function backup_file
  {
    declare -i n=$BACKUP_FILES
  
    cp $1 $1.0~
    until [ -e $1.$n~ ]; do
      n=$((n-1))
    done
    n=$((n+1))
    
    declare -i i=$n
    while [ $i -gt 0 ]; do
      mv $1.$((i-1))~ $1.$i~
      i=$((i-1))
    done
  
    [ $n -gt $BACKUP_FILES ] && rm -f $1.$n~
  }

  if [ "$USE_RCS" = 'yes' ] && type 'ci' &>/dev/null; then
    # call RCS
    echo "${0##*/}: RCS:"; ci -l $_source$_ext
  else
    # default back-up method
    [ "$USE_RCS" = 'yes' ] && \
      error "Can't find RCS! Using default back-up procedure instead..."
    backup_file $_source$_ext
    echo "${0##*/}: $PWD <-- $_source$_ext.1~"
  fi
}


function terminate  # log out procedure
{
  cleanup
  option b && backup
  [ "$action" = 'watch' ] && read
}


# process options!
while getopts $optionlist opt; do
  case $opt in
    P                    )  action=watch;;
    T                    )  action=quit;;
    v                    )  echo 'proof v1.2'; exit 0;;
    \?                   )  info; exit 1;;
  esac
  OPTS=$*
done
OPTS="${OPTS% *}"
shift $(($OPTIND - 1))

# check syntax!
[ -z "$1" ] && error 'Please specify your document!' halt
! echo $1 | grep '\(.mf\|.mp\|.tex\|.dvi\|.ps\|.pdf\)$' >/dev/null && \
  error "Unknown file type!" halt
[ "$action" != 'quit' ] && [ ! -e "$1" ] && error "Can't find file $1!" halt

# go!
case "$action" in
 
   quit )  kill -TERM $(pid "${0##*/}"'.*'-P'.*'"$1") >/dev/null 2>&1;;
 
   *    )  # set shell variables!
           cd $(dirname $1) || exit 1
           _ext=".${1##*.}"
           _source=$(basename $1 $_ext)
           current=/tmp/proof-$$  # preserves st_mtime

           # set signal trap!
           trap 'rm -f $current; terminate; exit 0' INT TERM
 
           # wait until file exists!
           [ ! -e "$1" ] && until [ -e $_source$_ext ]; do sleep 1; done
 
           view
 
           # view only?
           [ -z "$action" ] && { terminate; exit 0; }
 
           # watch!
           echo >$current
           while :; do
             until [ $_source$_ext -nt $current ]; do
               sleep 1
             done
             view; echo >$current
           done;;
 
esac