summaryrefslogtreecommitdiff
path: root/Build/source/texk/xdvik/xdvizilla
blob: fb0005085aff47e80f59e35556795fdaf5ea5eb9 (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
#! /bin/sh
#
# This is a kludge to fix helper apps in mozilla.  See mozilla bugs #57420
# and also #78919.
#
# It's also useful for tar files with Netscape 4.x
#
# Copyright (c) 2002-2004  Paul Vojta
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL PAUL VOJTA OR ANY OTHER AUTHOR OF OR CONTRIBUTOR TO
# THIS SOFTWARE BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

# Some changes suggested by Thomas Esser included by
# <stefanulrich@users.sourceforge.net>.

IN_FILE=
NO_RM=
TMP_DIR=
progname=xdvizilla

do_cleanup()
{
exit 0
    exitval=$?
    if [ -z "$NO_RM" -a -n "$IN_FILE" ] ; then
	rm -f "$IN_FILE"
    fi
    test -n "$TMP_DIR" && rm -rf "$TMP_DIR"
    exit $exitval
}

do_abort()
{
    xmessage -nearmouse "$progname: $1"
    do_cleanup
    exit 1
}

usage()
{
    xmessage -nearmouse "Usage: $progname [-no-rm] <file>"
    do_cleanup
    exit 1
}


# Solaris' file command fails to identify DVI files (bug #1508963),
# so add an extra check for these; otherwise, output result of 'file' command.
get_filetype()
{
    f="$1"
    have_solaris=`uname -a | grep -i sunos`
    if [ "$have_solaris"xxx = "xxx" ]
    then
	ret=`file "$f"`
    else
        # we're on solaris, get first 2 bytes of file
	magic=`od -N 2 -x "$f" | cut -d ' ' -f 2 | sed 1q | tr 'ABCDEF' 'abcdef'`
        case "$magic" in
        "f702" | "02f7")
            ret="TeX DVI file"
            ;;
        *)
            ret=`file "$f"`
            ;;
        esac
    fi
    echo "$ret"
}

trap 'do_cleanup' 1 2 3 7 13 15

### create a temporary directory only read/writable by user
### try mktemp if it's available
TMP_DIR=${TMPDIR-${TEMP-${TMP-/tmp}}}/${progname}.XXXXXX
TMP_DIR=`mktemp -d "$TMP_DIR" 2> /dev/null`
if [ $? -ne 0 ]; then
    ### fall back to unsafe creation
    TMP_DIR=${TMPDIR-${TEMP-${TMP-/tmp}}}/${progname}.$$
    (umask 077; mkdir "$TMP_DIR") || do_abort "Could not create directory \`$TMP_DIR'"
fi

### we hard-wire the magic for DVI files here since some "file" implementations
### (e.g. on Solaris 9) don't recognize DVI files (bug #1508963)
TMP_MAGIC_FILE="$TMP_DIR"/tmp-magic
### Note: 3 tabs in the following line!
echo '0	string	\367\002	TeX DVI file' > "$TMP_MAGIC_FILE"

if [ $# -gt 1 -a "x$1" = "x-no-rm" ]; then
  NO_RM=y
  shift
fi

if [ $# -ne 1 ]; then
  usage
fi

DIR=`dirname "$0"`

if [ "$DIR" = . ]; then
  DIR=
elif [ -f "$DIR"/xdvi -a -x "$DIR"/xdvi ]; then
  DIR="$DIR"/
else
  DIR=
fi

# set -x

# need to preserve IN_FILE for eventual deletion
IN_FILE="$1"
TMP_FILE="$IN_FILE"

while [ 1 ]; do
    [ -f "$TMP_FILE" ] || do_abort "$TMP_FILE: File not found."    
    #FILETYPE=`file -m "$TMP_MAGIC_FILE" "$TMP_FILE"`
    FILETYPE=`get_filetype "$TMP_FILE"`
    case "$FILETYPE" in
    *"gzip compressed data"*)
        out="$TMP_DIR"/tmp-gz
        gunzip -c "$TMP_FILE" > "$out"
        TMP_FILE="$out"
        ;;
    *"compressed data"* | *"compress'd data"*)
        out="$TMP_DIR"/tmp-compress
        uncompress -c "$TMP_FILE" > "$out"
        TMP_FILE="$out"
        ;;
    "$TMP_FILE: empty")
        do_abort "$TMP_FILE is an empty file
(probably a bug in Mozilla?)"
        ;;
    *" tar archive")
	### do sanity checks on the tar archive, to avoid overwriting user files:
	dangerous=`tar tf "$TMP_FILE" | egrep '^(/|.*\.\./)'`
	[ -z "$dangerous" ] || do_abort "Tar file contains files with absolute paths or \`../' components,
which may overwrite user files. Not unpacking it."
        ### also check for gzipped DVI files inside the archive ...
        out="$TMP_DIR"/`tar tf "$TMP_FILE" | egrep '\.(dvi|dvi.gz|dvi.Z)$' | head -1`
        if [ -z "$out" ]; then
	    do_abort "Tar file does not contain a dvi file."
        else
	    cat "$TMP_FILE" | (cd "$TMP_DIR"; tar xf -)
	    TMP_FILE="$out"
        fi
        ;;
    *"DVI file"*)
        "$DIR"xdvi -safer "$TMP_FILE"
        break
        ;;
    *)
        do_abort "$TMP_FILE: Unrecognized file format!"
        ;;
    esac
done

do_cleanup

exit 0