#!/bin/sh # # Return the mailcap action for the supplied MIME type. # - Cameron Simpson 14nov2002 # mailcap=$HOME/.mailcap action=1 subfile= needattrs= rejectattrs= cmd=$0 usage="Usage: $cmd [-a] [+a] [-c] [+c] [-m mailcap] [-s subfile] mimetype [[!]attribute...] -a Just print the action portion of the mailcap entry (default). +a Print the whole mailcap entry. -c Select the \"copiousoutput\" mailcap entry (default). +c Avoid the \"copiousoutput\" mailcap entry. -m mailcap Specify the mailcap file. -s subfile Replace any %s with subfile, correctly quoted. Actions with no %s get the file as stdin." badopts= aflag= cflag= subcmd="\"\$0\" $aflag $cflag -m \"\$mailcap\"" while : do case $1 in -a) action=1 ;; +a) action= ;; -c) needattrs="$needattrs copiousoutput" ;; +c) rejectattrs="$rejectattrs copiousoutput" ;; -m) mailcap=$2; shift ;; -s) subfile=$2; shift ;; --) shift; break ;; -?*) echo "$cmd: unrecognised option: $1" >&2; badopts=1 ;; *) break ;; esac shift done if [ $# = 0 ] then echo "$cmd: missing mime type" >&2; badopts=1 else mtype=$1; shift fi # gather up accept/reject attribute lists for attr do case "$attr" in !*) rejectattrs="$rejectattrs "`expr "x$attr" : 'x.\(.*\)'` ;; *) needattrs="$needattrs $attr" ;; esac done [ $badopts ] && { echo "$usage" >&2; exit 2; } grep "^$mtype *;" <"$mailcap" \ | \ { ok= while oIFS=$IFS IFS=';' read cap_type cap_action cap_attrs || break IFS=$oIFS do ok=1 # make sure all the needed attributes are present for attr in $needattrs do case " $cap_attrs " in *" $attr "*) ;; *) ok=; break ;; esac done [ $ok ] || continue # make sure none of the reject attributes are present for attr in $rejectattrs do case " $cap_attrs " in *" $attr "*) ok=; break ;; esac done [ $ok ] || continue break done [ $ok ] || exit 1 [ $action ] || { echo "$cap_type; $cap_action; $cap_attrs"; exit 0; } [ -n "$subfile" ] || { echo "$cap_action"; exit 0; } # insert hook for $subfile qsubfile=`shqstr "$subfile"` case "$cap_action" in *'%s'*) # protect sed-specials case "$qsubfile" in *[\&\\]*) sedsubfile=`sedstrs 's/[&\\]/\\\\&/g' "$qsubfile"` ;; *) sedsubfile=$qsubfile ;; esac cap_action=`sedstrs "s%s$sedsubfileg" "$cap_action"` ;; *) [ "x$subfile" = x/dev/fd/0 ] || cap_action="exec <$qsubfile; $cap_action" ;; esac echo "$cap_action" }