* release of groffer 0.9.20
[s-roff.git] / contrib / groffer / groffer.sh
blob4a9cdab449c996b6142aa9536b05aea1a8d11370
1 #! /bin/sh
3 # groffer - display groff files
5 # Source file position: <groff-source>/contrib/groffer/groffer.sh
7 # Copyright (C) 2001,2002,2003,2004,2005
8 # Free Software Foundation, Inc.
9 # Written by Bernd Warken
11 # This file is part of groff version @VERSION@ (eventually 1.19.2).
13 # groff is free software; you can redistribute it and/or modify it
14 # under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2, or (at your option)
16 # any later version.
18 # groff is distributed in the hope that it will be useful, but WITHOUT
19 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
21 # License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with groff; see the files COPYING and LICENSE in the top
25 # directory of the groff source. If not, write to the Free Software
26 # Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA.
28 _PROGRAM_NAME='groffer';
29 _PROGRAM_VERSION='0.9.20';
30 _LAST_UPDATE='30 July 2005';
33 ########################################################################
34 # Determine the shell under which to run this script from the command
35 # line arguments or $GROFF_OPT; if none is specified, just go on with
36 # the starting shell.
38 if test _"${_GROFFER_RUN}"_ = __;
39 then
40 # only reached during the first run of the script
42 export _GROFFER_RUN; # counter for the runs of groffer
43 _GROFFER_RUN='first';
45 export _PROGRAM_NAME;
46 export _PROGRAM_VERSION;
47 export _LAST_UPDATE;
49 export GROFFER_OPT; # option environment for groffer
50 export _GROFFER_SH; # file name of this shell script
51 export _OUTPUT_FILE_NAME; # output generated, see main_set_res..()
53 export _CONF_FILES; # configuration files
54 _CONF_FILES="/etc/groff/groffer.conf ${HOME}/.groff/groffer.conf";
56 case "$0" in
57 *${_PROGRAM_NAME}*)
58 _GROFFER_SH="$0";
59 # was: _GROFFER_SH="@BINDIR@/${_PROGRAM_NAME}";
62 echo "The ${_PROGRAM_NAME} script should be started directly." >&2
63 exit 1;
65 esac;
67 export _NULL_DEV;
68 if test -c /dev/null;
69 then
70 _NULL_DEV="/dev/null";
71 else
72 _NULL_DEV="NUL";
73 fi;
76 # test of `unset'
77 export _UNSET;
78 export _foo;
79 _foo=bar;
80 _res="$(unset _foo 2>&1)";
81 if unset _foo >${_NULL_DEV} 2>&1 && \
82 test _"${_res}"_ = __ && test _"${_foo}"_ = __
83 then
84 _UNSET='unset';
85 eval "${_UNSET}" _res;
86 else
87 _UNSET=':';
88 fi;
91 ###########################
92 # _get_opt_shell ("$@")
94 # Determine whether `--shell' was specified in $GROFF_OPT or in $*;
95 # if so, echo its argument.
97 # Output: the shell name if it was specified
99 _get_opt_shell()
101 case " ${GROFFER_OPT} $*" in
102 *\ --shell\ *|*\ --shell=*)
104 eval set x "${GROFFER_OPT}" '"$@"';
105 shift;
106 _sh='';
107 while test $# != 0
109 case "$1" in
110 --shell)
111 if test "$#" -ge 2;
112 then
113 _sh="$2";
114 shift;
117 --shell=?*)
118 # delete up to first `=' character
119 _sh="$(echo x"$1" | sed -e '
120 s/^x//
121 s/^[^=]*=//
122 ')";
124 esac;
125 shift;
126 done;
127 cat <<EOF
128 ${_sh}
132 esac;
136 ###########################
137 # _test_on_shell (<name>)
139 # Test whether <name> is a shell program of Bourne type (POSIX sh).
141 _test_on_shell()
143 if test "$#" -le 0 || test _"$1"_ = __;
144 then
145 return 1;
147 # do not quote $1 to allow arguments
148 test _"$(eval $1 -c "'"'s=ok; echo $s'"'" 2>${_NULL_DEV})"_ = _ok_;
152 ###########################
153 # do the shell determination from command line and $GROFFER_OPT
154 _shell="$(_get_opt_shell "$@")";
155 if test _"${_shell}"_ = __;
156 then
157 # none found, so look at the `--shell' lines in configuration files
158 export f;
159 # for f in $_CONF_FILES
160 for f in $(eval set x ${_CONF_FILES}; shift; echo "$@")
162 if test -f "$f";
163 then
164 _all="$(cat "$f" | sed -n -e 's/^--shell[= ] *\([^ ]*\)$/\1/p')"
165 # for s in $_all
166 for s in $(eval set x ${_all}; shift; echo "$@")
168 _shell="$s";
169 done;
171 done;
172 eval ${_UNSET} f;
173 eval ${_UNSET} s;
174 eval ${_UNSET} _all;
177 export _GROFFER2_SH; # file name of the script that follows up
178 _GROFFER2_SH='@libdir@/groff/groffer/groffer2.sh';
180 # restart the script with the last found $_shell, if it is a shell
181 if _test_on_shell "${_shell}";
182 then
183 _GROFFER_RUN='second';
184 # do not quote $_shell to allow arguments
185 eval exec ${_shell} "'${_GROFFER2_SH}'" '"$@"';
186 exit;
189 _GROFFER_RUN='second';
190 eval ${_UNSET} _shell;
191 eval exec "'${_GROFFER2_SH}'" '"$@"';
193 fi; # end of first run
195 if test _"${_GROFFER_RUN}"_ != _second_;
196 then
197 echo "$_GROFFER_RUN should be 'second' here." >&2
198 exit 1
201 eval ${_UNSET} _GROFFER_RUN