PageRenderTime 41ms CodeModel.GetById 14ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 1ms

/contrib/groff/contrib/groffer/groffer.sh

https://bitbucket.org/freebsd/freebsd-head/
Shell | 299 lines | 211 code | 36 blank | 52 comment | 26 complexity | 2afb38f97e501f09e3cd350ed95ac997 MD5 | raw file
  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
 35export _PROGRAM_VERSION;
 36export _LAST_UPDATE;
 37
 38export GROFFER_OPT;		# option environment for groffer
 39
 40export _CONF_FILE_ETC;		# configuration file in /etc
 41export _CONF_FILE_HOME;		# configuration file in $HOME
 42export _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
 49export _AT;
 50export _SP;
 51export _SQ;
 52export _TAB;
 53
 54_AT='@';
 55_SP=' ';
 56_SQ="'";
 57_TAB='	';
 58
 59export _ERROR;
 60_ERROR='7';			# for syntax errors; no `-1' in `ash'
 61
 62# @...@ constructs
 63
 64export _GROFF_VERSION
 65_GROFF_VERSION='@VERSION@';
 66if test _@VERSION@_ = _${_AT}VERSION${_AT}_
 67then
 68  _GROFF_VERSION='1.19.2';
 69fi;
 70
 71export _AT_BINDIR_AT;
 72export _AT_G_AT;
 73export _AT_LIBDIR_AT;
 74export _GROFFER_LIBDIR;
 75if test _@BINDIR@_ = _${_AT}BINDIR${_AT}_
 76then
 77  # script before `make'
 78  _AT_BINDIR_AT='.';
 79  _AT_G_AT='';
 80  _AT_LIBDIR_AT='';
 81  _GROFFER_LIBDIR='.';
 82else
 83  _AT_BINDIR_AT='@BINDIR@';
 84  _AT_G_AT='@g@';
 85  _AT_LIBDIR_AT='@libdir@';
 86  _GROFFER_LIBDIR="${_AT_LIBDIR_AT}"'/groff/groffer';
 87fi;
 88
 89export _GROFFER_SH;		# file name of this shell script
 90case "$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  ;;
 99esac;
100
101export _GROFFER2_SH;		# file name of the script that follows up
102_GROFFER2_SH="${_GROFFER_LIBDIR}"/groffer2.sh;
103
104export _NULL_DEV;
105if test -c /dev/null
106then
107  _NULL_DEV="/dev/null";
108else
109  _NULL_DEV="NUL";
110fi;
111
112
113# Test of the `$()' construct.
114if test _"$(echo "$(echo 'test')")"_ \
115     != _test_
116then
117  echo 'The "$()" construct did not work.' >&2;
118  exit "${_ERROR}";
119fi;
120
121# Test of sed program
122if test _"$(echo red | sed -e 's/r/s/')"_ != _sed_
123then
124  echo 'The sed program did not work.' >&2;
125  exit "${_ERROR}";
126fi;
127
128
129########################### configuration
130
131# read and transform the configuration files, execute the arising commands
132for f in "${_CONF_FILE_HOME}" "${_CONF_FILE_ETC}"
133do
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
142s/^['"${_SP}${_TAB}"']*//
143s/['"${_SP}${_TAB}"']*$//
144# Print all shell commands
145/^[^-]/p
146# Replace empty arguments
147s/^\(-[^ ]*\)=$/o="${o} \1 '"${_SQ}${_SQ}"'"/p
148# Replace division between option and argument by single space
149s/[='"${_SP}${_TAB}"']['"${_SP}${_TAB}"']*/'"${_SP}"'/
150# Handle lines without spaces
151s/^\(-[^'"${_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
155s/^\(-[^ ]*\) "\(.*\)"$/o="${o} \1 '"${_SQ}"'\2'"${_SQ}"'"/p
156# Encircle the remaining arguments with single quotes
157s/^\(-[^ ]*\) \(.*\)$/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;
169done;
170
171# integrate $GROFFER_OPT into the command line; it isn't needed any more
172if test _"${GROFFER_OPT}"_ != __
173then
174  eval set x "${GROFFER_OPT}" '"$@"';
175  shift;
176  GROFFER_OPT='';
177fi;
178
179
180########################### Determine the shell
181
182export _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}
247EOF
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}
261EOF
262    fi;
263    ;;
264  esac;
265`
266
267########################### test fast shells for automatic run
268
269if test _"${_SHELL}"_ = __
270then
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;
279fi;
280
281
282########################### start groffer2.sh
283
284if test _"${_SHELL}"_ = _empty_
285then
286  _SHELL='';
287fi;
288
289if test _"${_SHELL}"_ = __
290then
291  # no shell found, so start groffer2.sh normally
292  eval exec "'${_GROFFER2_SH}'" '"$@"';
293  exit;
294else
295  # start groffer2.sh with the found $_SHELL
296  # do not quote $_SHELL to allow arguments
297  eval exec "${_SHELL} '${_GROFFER2_SH}'" '"$@"';
298  exit;
299fi;