Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / contrib / groff / contrib / groffer / groffer.sh
1 #! /bin/sh
2
3 # groffer - display groff files
4
5 # Source file position: <groff-source>/contrib/groffer/groffer.sh
6
7 # Copyright (C) 2001,2002,2003,2004,2005
8 # Free Software Foundation, Inc.
9 # Written by Bernd Warken
10
11 # This file is part of `groffer', which is part of `groff' version
12 # @VERSION@.  See $_GROFF_VERSION.
13
14 # `groff' is free software; you can redistribute it and/or modify it
15 # under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 2, or (at your option)
17 # any later version.
18
19 # `groff' is distributed in the hope that it will be useful, but
20 # WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22 # General Public License for more details.
23
24 # You should have received a copy of the GNU General Public License
25 # along with `groff'; see the files COPYING and LICENSE in the top
26 # directory of the `groff' source.  If not, write to the Free Software
27 # Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301,
28 # USA.
29
30 ########################################################################
31
32 _PROGRAM_VERSION='0.9.22';
33 _LAST_UPDATE='22 August 2005';
34
35 export _PROGRAM_VERSION;
36 export _LAST_UPDATE;
37
38 export GROFFER_OPT;             # option environment for groffer
39
40 export _CONF_FILE_ETC;          # configuration file in /etc
41 export _CONF_FILE_HOME;         # configuration file in $HOME
42 export _CONF_FILES;             # configuration files
43 _CONF_FILE_ETC='/etc/groff/groffer.conf';
44 _CONF_FILE_HOME="${HOME}/.groff/groffer.conf";
45 _CONF_FILES="${_CONF_FILE_ETC} ${_CONF_FILE_HOME}";
46
47 # characters
48
49 export _AT;
50 export _SP;
51 export _SQ;
52 export _TAB;
53
54 _AT='@';
55 _SP=' ';
56 _SQ="'";
57 _TAB='  ';
58
59 export _ERROR;
60 _ERROR='7';                     # for syntax errors; no `-1' in `ash'
61
62 # @...@ constructs
63
64 export _GROFF_VERSION
65 _GROFF_VERSION='@VERSION@';
66 if test _@VERSION@_ = _${_AT}VERSION${_AT}_
67 then
68   _GROFF_VERSION='1.19.2';
69 fi;
70
71 export _AT_BINDIR_AT;
72 export _AT_G_AT;
73 export _AT_LIBDIR_AT;
74 export _GROFFER_LIBDIR;
75 if test _@BINDIR@_ = _${_AT}BINDIR${_AT}_
76 then
77   # script before `make'
78   _AT_BINDIR_AT='.';
79   _AT_G_AT='';
80   _AT_LIBDIR_AT='';
81   _GROFFER_LIBDIR='.';
82 else
83   _AT_BINDIR_AT='@BINDIR@';
84   _AT_G_AT='@g@';
85   _AT_LIBDIR_AT='@libdir@';
86   _GROFFER_LIBDIR="${_AT_LIBDIR_AT}"'/groff/groffer';
87 fi;
88
89 export _GROFFER_SH;             # file name of this shell script
90 case "$0" in
91 *groffer*)
92   _GROFFER_SH="$0";
93   # was: _GROFFER_SH="${_AT_BINDIR_AT}/groffer";
94   ;;
95 *)
96   echo 'The groffer script should be started directly.' >&2
97   exit 1;
98   ;;
99 esac;
100
101 export _GROFFER2_SH;            # file name of the script that follows up
102 _GROFFER2_SH="${_GROFFER_LIBDIR}"/groffer2.sh;
103
104 export _NULL_DEV;
105 if test -c /dev/null
106 then
107   _NULL_DEV="/dev/null";
108 else
109   _NULL_DEV="NUL";
110 fi;
111
112
113 # Test of the `$()' construct.
114 if test _"$(echo "$(echo 'test')")"_ \
115      != _test_
116 then
117   echo 'The "$()" construct did not work.' >&2;
118   exit "${_ERROR}";
119 fi;
120
121 # Test of sed program
122 if test _"$(echo red | sed -e 's/r/s/')"_ != _sed_
123 then
124   echo 'The sed program did not work.' >&2;
125   exit "${_ERROR}";
126 fi;
127
128
129 ########################### configuration
130
131 # read and transform the configuration files, execute the arising commands
132 for f in "${_CONF_FILE_HOME}" "${_CONF_FILE_ETC}"
133 do
134   if test -f "$f"
135   then
136     o="";                       # $o means groffer option
137     # use "" quotes because of ksh and posh
138     eval "$(cat "$f" | sed -n -e '
139 # Ignore comments
140 /^['"${_SP}${_TAB}"']*#/d
141 # Delete leading and final space
142 s/^['"${_SP}${_TAB}"']*//
143 s/['"${_SP}${_TAB}"']*$//
144 # Print all shell commands
145 /^[^-]/p
146 # Replace empty arguments
147 s/^\(-[^ ]*\)=$/o="${o} \1 '"${_SQ}${_SQ}"'"/p
148 # Replace division between option and argument by single space
149 s/[='"${_SP}${_TAB}"']['"${_SP}${_TAB}"']*/'"${_SP}"'/
150 # Handle lines without spaces
151 s/^\(-[^'"${_SP}"']*\)$/o="${o} \1"/p
152 # Print options that have their argument encircled with single quotes
153 /^-[^ ]* '"${_SQ}"'.*'"${_SQ}"'$/s/^.*$/o="${o} &"/p
154 # Replace encircled double quotes by single quotes and print the result
155 s/^\(-[^ ]*\) "\(.*\)"$/o="${o} \1 '"${_SQ}"'\2'"${_SQ}"'"/p
156 # Encircle the remaining arguments with single quotes
157 s/^\(-[^ ]*\) \(.*\)$/o="${o} \1 '"${_SQ}"'\2'"${_SQ}"'"/p
158 ')"
159     if test _"${o}"_ != __
160     then
161       if test _"{GROFFER_OPT}"_ = __
162       then
163         GROFFER_OPT="${o}";
164       else
165         GROFFER_OPT="${o} ${GROFFER_OPT}";
166       fi;
167     fi;
168   fi;
169 done;
170
171 # integrate $GROFFER_OPT into the command line; it isn't needed any more
172 if test _"${GROFFER_OPT}"_ != __
173 then
174   eval set x "${GROFFER_OPT}" '"$@"';
175   shift;
176   GROFFER_OPT='';
177 fi;
178
179
180 ########################### Determine the shell
181
182 export _SHELL;
183
184 # use "``" instead of "$()" for using the case ")" construct
185 # do not use "" quotes because of ksh
186 _SHELL=`
187   # $x means list.
188   # $s means shell.
189   # The command line arguments are taken over.
190   # Shifting herein does not have an effect outside.
191   export x;  
192   case " $*" in
193   *\ --sh*)                     # abbreviation for --shell
194     x='';
195     s='';
196     # determine all --shell arguments, store them in $x in reverse order
197     while test $# != 0
198     do
199       case "$1" in
200       --shell|--sh|--she|--shel)
201         if test "$#" -ge 2
202         then
203           s="$2";
204           shift;
205         fi;
206         ;;
207       --shell=*|--sh=*|--she=*|--shel=*)
208         # delete up to first "=" character
209         s="$(echo x"$1" | sed -e 's/^x[^=]*=//')";
210         ;;
211       *)
212         shift;
213         continue;
214       esac;
215       if test _"${x}"_ = __
216       then
217         x="'${s}'";
218       else
219         x="'${s}' ${x}";
220       fi;
221       shift;
222     done;
223
224     # from all possible shells in $x determine the first being a shell
225     # or being empty
226     s="$(
227       # "" quotes because of posh
228       eval set x "${x}";
229       shift;
230       if test $# != 0
231       then
232         for i
233         do
234           if test _"$i"_ = __
235           then
236             # use the empty argument as the default shell
237             echo empty;
238             break;
239           else
240             # test $i on being a shell program;
241             # use this kind of quoting for posh
242             if test _"$(eval "$i -c 'echo ok'" 2>${_NULL_DEV})"_ = _ok_ >&2
243             then
244               # shell found
245               cat <<EOF
246 ${i}
247 EOF
248               break;
249             else
250               # if not being a shell go on searching
251               continue;
252             fi;
253           fi;
254         done;
255       fi;
256     )";
257     if test _"${s}"_ != __
258     then
259       cat <<EOF
260 ${s}
261 EOF
262     fi;
263     ;;
264   esac;
265 `
266
267 ########################### test fast shells for automatic run
268
269 if test _"${_SHELL}"_ = __
270 then
271   for s in ksh ash dash pdksh zsh posh
272   do
273     if test _"$(eval "$s -c 'echo ok'" 2>${_NULL_DEV})"_ = _ok_ >&2
274     then
275       _SHELL="$s";
276       break;
277     fi;
278   done;
279 fi;
280
281
282 ########################### start groffer2.sh
283
284 if test _"${_SHELL}"_ = _empty_
285 then
286   _SHELL='';
287 fi;
288
289 if test _"${_SHELL}"_ = __
290 then
291   # no shell found, so start groffer2.sh normally
292   eval exec "'${_GROFFER2_SH}'" '"$@"';
293   exit;
294 else
295   # start groffer2.sh with the found $_SHELL
296   # do not quote $_SHELL to allow arguments
297   eval exec "${_SHELL} '${_GROFFER2_SH}'" '"$@"';
298   exit;
299 fi;