1#! /bin/sh 2# 3# This is a kludge to fix helper apps in mozilla. See mozilla bugs #57420 4# and also #78919. 5# 6# It's also useful for tar files with Netscape 4.x 7# 8# Copyright (c) 2002-2004 Paul Vojta 9# 10# Permission is hereby granted, free of charge, to any person obtaining a copy 11# of this software and associated documentation files (the "Software"), to 12# deal in the Software without restriction, including without limitation the 13# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 14# sell copies of the Software, and to permit persons to whom the Software is 15# furnished to do so, subject to the following conditions: 16# 17# The above copyright notice and this permission notice shall be included in 18# all copies or substantial portions of the Software. 19# 20# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 22# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 23# IN NO EVENT SHALL PAUL VOJTA OR ANY OTHER AUTHOR OF OR CONTRIBUTOR TO 24# THIS SOFTWARE BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 25# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 26# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 27# IN THE SOFTWARE. 28 29# Some changes suggested by Thomas Esser included by 30# <stefanulrich@users.sourceforge.net>. 31 32IN_FILE= 33NO_RM= 34TMP_DIR= 35progname=xdvizilla 36 37do_cleanup() 38{ 39exit 0 40 exitval=$? 41 if [ -z "$NO_RM" -a -n "$IN_FILE" ] ; then 42 rm -f "$IN_FILE" 43 fi 44 test -n "$TMP_DIR" && rm -rf "$TMP_DIR" 45 exit $exitval 46} 47 48do_abort() 49{ 50 xmessage -nearmouse "$progname: $1" 51 do_cleanup 52 exit 1 53} 54 55usage() 56{ 57 xmessage -nearmouse "Usage: $progname [-no-rm] <file>" 58 do_cleanup 59 exit 1 60} 61 62 63# Solaris' file command fails to identify DVI files (bug #1508963), 64# so add an extra check for these; otherwise, output result of 'file' command. 65get_filetype() 66{ 67 f="$1" 68 have_solaris=`uname -a | grep -i sunos` 69 if [ "$have_solaris"xxx = "xxx" ] 70 then 71 ret=`file "$f"` 72 else 73 # we're on solaris, get first 2 bytes of file 74 magic=`od -N 2 -x "$f" | cut -d ' ' -f 2 | sed 1q | tr 'ABCDEF' 'abcdef'` 75 case "$magic" in 76 "f702" | "02f7") 77 ret="TeX DVI file" 78 ;; 79 *) 80 ret=`file "$f"` 81 ;; 82 esac 83 fi 84 echo "$ret" 85} 86 87trap 'do_cleanup' 1 2 3 7 13 15 88 89### create a temporary directory only read/writable by user 90### try mktemp if it's available 91TMP_DIR=${TMPDIR-${TEMP-${TMP-/tmp}}}/${progname}.XXXXXX 92TMP_DIR=`mktemp -d "$TMP_DIR" 2> /dev/null` 93if [ $? -ne 0 ]; then 94 ### fall back to unsafe creation 95 TMP_DIR=${TMPDIR-${TEMP-${TMP-/tmp}}}/${progname}.$$ 96 (umask 077; mkdir "$TMP_DIR") || do_abort "Could not create directory \`$TMP_DIR'" 97fi 98 99### we hard-wire the magic for DVI files here since some "file" implementations 100### (e.g. on Solaris 9) don't recognize DVI files (bug #1508963) 101TMP_MAGIC_FILE="$TMP_DIR"/tmp-magic 102### Note: 3 tabs in the following line! 103echo '0 string \367\002 TeX DVI file' > "$TMP_MAGIC_FILE" 104 105if [ $# -gt 1 -a "x$1" = "x-no-rm" ]; then 106 NO_RM=y 107 shift 108fi 109 110if [ $# -ne 1 ]; then 111 usage 112fi 113 114DIR=`dirname "$0"` 115 116if [ "$DIR" = . ]; then 117 DIR= 118elif [ -f "$DIR"/xdvi -a -x "$DIR"/xdvi ]; then 119 DIR="$DIR"/ 120else 121 DIR= 122fi 123 124# set -x 125 126# need to preserve IN_FILE for eventual deletion 127IN_FILE="$1" 128TMP_FILE="$IN_FILE" 129 130while [ 1 ]; do 131 [ -f "$TMP_FILE" ] || do_abort "$TMP_FILE: File not found." 132 #FILETYPE=`file -m "$TMP_MAGIC_FILE" "$TMP_FILE"` 133 FILETYPE=`get_filetype "$TMP_FILE"` 134 case "$FILETYPE" in 135 *"gzip compressed data"*) 136 out="$TMP_DIR"/tmp-gz 137 gunzip -c "$TMP_FILE" > "$out" 138 TMP_FILE="$out" 139 ;; 140 *"compressed data"* | *"compress'd data"*) 141 out="$TMP_DIR"/tmp-compress 142 uncompress -c "$TMP_FILE" > "$out" 143 TMP_FILE="$out" 144 ;; 145 "$TMP_FILE: empty") 146 do_abort "$TMP_FILE is an empty file 147(probably a bug in Mozilla?)" 148 ;; 149 *" tar archive") 150 ### do sanity checks on the tar archive, to avoid overwriting user files: 151 dangerous=`tar tf "$TMP_FILE" | egrep '^(/|.*\.\./)'` 152 [ -z "$dangerous" ] || do_abort "Tar file contains files with absolute paths or \`../' components, 153which may overwrite user files. Not unpacking it." 154 ### also check for gzipped DVI files inside the archive ... 155 out="$TMP_DIR"/`tar tf "$TMP_FILE" | egrep '\.(dvi|dvi.gz|dvi.Z)$' | head -1` 156 if [ -z "$out" ]; then 157 do_abort "Tar file does not contain a dvi file." 158 else 159 cat "$TMP_FILE" | (cd "$TMP_DIR"; tar xf -) 160 TMP_FILE="$out" 161 fi 162 ;; 163 *"DVI file"*) 164 "$DIR"xdvi -safer "$TMP_FILE" 165 break 166 ;; 167 *) 168 do_abort "$TMP_FILE: Unrecognized file format!" 169 ;; 170 esac 171done 172 173do_cleanup 174 175exit 0 176