1## 2## This file is part of shtool and free software; you can redistribute 3## it and/or modify it under the terms of the GNU General Public 4## License as published by the Free Software Foundation; either version 5## 2 of the License, or (at your option) any later version. 6## 7## This file is distributed in the hope that it will be useful, 8## but WITHOUT ANY WARRANTY; without even the implied warranty of 9## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 10## General Public License for more details. 11## 12## You should have received a copy of the GNU General Public License 13## along with this program; if not, write to the Free Software 14## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 15## USA, or contact Ralf S. Engelschall <rse@engelschall.com>. 16## 17 18## 19## COMMON UTILITY CODE 20## 21 22# determine name of tool 23if [ ".$tool" != . ]; then 24 # used inside shtool script 25 toolcmd="$0 $tool" 26 toolcmdhelp="shtool $tool" 27 msgprefix="shtool:$tool" 28else 29 # used as standalone script 30 toolcmd="$0" 31 toolcmdhelp="sh $0" 32 msgprefix="$str_tool" 33fi 34 35# parse argument specification string 36eval `echo $arg_spec |\ 37 sed -e 's/^\([0-9]*\)\([+=]\)/arg_NUMS=\1; arg_MODE=\2/'` 38 39# parse option specification string 40eval `echo h.$opt_spec |\ 41 sed -e 's/\([a-zA-Z0-9]\)\([.:+]\)/opt_MODE_\1=\2;/g'` 42 43# interate over argument line 44opt_PREV='' 45while [ $# -gt 0 ]; do 46 # special option stops processing 47 if [ ".$1" = ".--" ]; then 48 shift 49 break 50 fi 51 52 # determine option and argument 53 opt_ARG_OK=no 54 if [ ".$opt_PREV" != . ]; then 55 # merge previous seen option with argument 56 opt_OPT="$opt_PREV" 57 opt_ARG="$1" 58 opt_ARG_OK=yes 59 opt_PREV='' 60 else 61 # split argument into option and argument 62 case "$1" in 63 -[a-zA-Z0-9]*) 64 eval `echo "x$1" |\ 65 sed -e 's/^x-\([a-zA-Z0-9]\)/opt_OPT="\1";/' \ 66 -e 's/";\(.*\)$/"; opt_ARG="\1"/'` 67 ;; 68 -[a-zA-Z0-9]) 69 opt_OPT=`echo "x$1" | cut -c3-` 70 opt_ARG='' 71 ;; 72 *) 73 break 74 ;; 75 esac 76 fi 77 78 # eat up option 79 shift 80 81 # determine whether option needs an argument 82 eval "opt_MODE=\$opt_MODE_${opt_OPT}" 83 if [ ".$opt_ARG" = . -a ".$opt_ARG_OK" != .yes ]; then 84 if [ ".$opt_MODE" = ".:" -o ".$opt_MODE" = ".+" ]; then 85 opt_PREV="$opt_OPT" 86 continue 87 fi 88 fi 89 90 # process option 91 case $opt_MODE in 92 '.' ) 93 # boolean option 94 eval "opt_${opt_OPT}=yes" 95 ;; 96 ':' ) 97 # option with argument (multiple occurances override) 98 eval "opt_${opt_OPT}=\"\$opt_ARG\"" 99 ;; 100 '+' ) 101 # option with argument (multiple occurances append) 102 eval "opt_${opt_OPT}=\"\$opt_${opt_OPT} \$opt_ARG\"" 103 ;; 104 * ) 105 echo "$msgprefix:Error: unknown option: \`-$opt_OPT'" 1>&2 106 echo "$msgprefix:Hint: run \`$toolcmdhelp -h' or \`man shtool' for details" 1>&2 107 exit 1 108 ;; 109 esac 110done 111if [ ".$opt_PREV" != . ]; then 112 echo "$msgprefix:Error: missing argument to option \`-$opt_PREV'" 1>&2 113 echo "$msgprefix:Hint: run \`$toolcmdhelp -h' or \`man shtool' for details" 1>&2 114 exit 1 115fi 116 117# process help option 118if [ ".$opt_h" = .yes ]; then 119 echo "Usage: $toolcmdhelp $str_usage" 120 exit 0 121fi 122 123# complain about incorrect number of arguments 124case $arg_MODE in 125 '=' ) 126 if [ $# -ne $arg_NUMS ]; then 127 echo "$msgprefix:Error: invalid number of arguments (exactly $arg_NUMS expected)" 1>&2 128 echo "$msgprefix:Hint: run \`$toolcmd -h' or \`man shtool' for details" 1>&2 129 exit 1 130 fi 131 ;; 132 '+' ) 133 if [ $# -lt $arg_NUMS ]; then 134 echo "$msgprefix:Error: invalid number of arguments (at least $arg_NUMS expected)" 1>&2 135 echo "$msgprefix:Hint: run \`$toolcmd -h' or \`man shtool' for details" 1>&2 136 exit 1 137 fi 138 ;; 139esac 140 141# establish a temporary file on request 142if [ ".$gen_tmpfile" = .yes ]; then 143 if [ ".$TMPDIR" != . ]; then 144 tmpdir="$TMPDIR" 145 elif [ ".$TEMPDIR" != . ]; then 146 tmpdir="$TEMPDIR" 147 else 148 tmpdir="/tmp" 149 fi 150 tmpfile="$tmpdir/.shtool.$$" 151 rm -f $tmpfile >/dev/null 2>&1 152 touch $tmpfile 153fi 154 155