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