1151497Sru#! /bin/sh
2151497Sru
3151497Sru# groffer - display groff files
4151497Sru
5151497Sru# Source file position: <groff-source>/contrib/groffer/groffer2.sh
6151497Sru# Installed position: <prefix>/lib/groff/groffer/groffer2.sh
7151497Sru
8151497Sru# This file should not be run independently.  It is called by
9151497Sru# `groffer.sh' in the source or by the installed `groffer' program.
10151497Sru
11151497Sru# Copyright (C) 2001,2002,2003,2004,2005
12151497Sru# Free Software Foundation, Inc.
13151497Sru# Written by Bernd Warken
14151497Sru
15151497Sru# Last update: 22 August 2005
16151497Sru
17151497Sru# This file is part of `groffer', which is part of `groff'.
18151497Sru
19151497Sru# `groff' is free software; you can redistribute it and/or modify it
20151497Sru# under the terms of the GNU General Public License as published by
21151497Sru# the Free Software Foundation; either version 2, or (at your option)
22151497Sru# any later version.
23151497Sru
24151497Sru# `groff' is distributed in the hope that it will be useful, but
25151497Sru# WITHOUT ANY WARRANTY; without even the implied warranty of
26151497Sru# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27151497Sru# General Public License for more details.
28151497Sru
29151497Sru# You should have received a copy of the GNU General Public License
30151497Sru# along with `groff'; see the files COPYING and LICENSE in the top
31151497Sru# directory of the `groff' source.  If not, write to the Free Software
32151497Sru# Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301,
33151497Sru# USA.
34151497Sru
35151497Sru
36151497Sru########################################################################
37151497Sru#             Test of rudimentary shell functionality
38151497Sru########################################################################
39151497Sru
40151497Sru
41151497Sru########################################################################
42151497Sru# Test of `unset'
43151497Sru#
44151497Sruexport _UNSET;
45151497Sruexport _foo;
46151497Sru_foo=bar;
47151497Sru_res="$(unset _foo 2>&1)";
48151497Sruif unset _foo >${_NULL_DEV} 2>&1 && \
49151497Sru   test _"${_res}"_ = __ && test _"${_foo}"_ = __
50151497Sruthen
51151497Sru  _UNSET='unset';
52151497Sru  eval "${_UNSET}" _foo;
53151497Sru  eval "${_UNSET}" _res;
54151497Sruelse
55151497Sru  _UNSET=':';
56151497Srufi;
57151497Sru
58151497Sru
59151497Sru########################################################################
60151497Sru# Test of `test'.
61151497Sru#
62151497Sruif test a = a && test a != b && test -f "${_GROFFER_SH}"
63151497Sruthen
64151497Sru  :;
65151497Sruelse
66151497Sru  echo '"test" did not work.' >&2;
67151497Sru  exit "${_ERROR}";
68151497Srufi;
69151497Sru
70151497Sru
71151497Sru########################################################################
72151497Sru# Test of `echo' and the `$()' construct.
73151497Sru#
74151497Sruif echo '' >${_NULL_DEV}
75151497Sruthen
76151497Sru  :;
77151497Sruelse
78151497Sru  echo '"echo" did not work.' >&2;
79151497Sru  exit "${_ERROR}";
80151497Srufi;
81151497Sruif test _"$(t1="$(echo te)" &&
82151497Sru            t2="$(echo '')" &&
83151497Sru            t3="$(echo 'st')" &&
84151497Sru            echo "${t1}${t2}${t3}")"_ \
85151497Sru     != _test_
86151497Sruthen
87151497Sru  echo 'The "$()" construct did not work' >&2;
88151497Sru  exit "${_ERROR}";
89151497Srufi;
90151497Sru
91151497Sru
92151497Sru########################################################################
93151497Sru# Test of sed program; test in groffer.sh is not valid here.
94151497Sru#
95151497Sruif test _"$(echo red | sed -e 's/r/s/')"_ != _sed_
96151497Sruthen
97151497Sru  echo 'The sed program did not work.' >&2;
98151497Sru  exit "${_ERROR}";
99151497Srufi;
100151497Sru
101151497Sru
102151497Sru########################################################################
103151497Sru# Test of function definitions.
104151497Sru#
105151497Sru_t_e_s_t_f_u_n_c_()
106151497Sru{
107151497Sru  return 0;
108151497Sru}
109151497Sru
110151497Sruif _t_e_s_t_f_u_n_c_ 2>${_NULL_DEV}
111151497Sruthen
112151497Sru  :;
113151497Sruelse
114151497Sru  echo 'Shell '"${_SHELL}"' does not support function definitions.' >&2;
115151497Sru  exit "${_ERROR}";
116151497Srufi;
117151497Sru
118151497Sru
119151497Sru########################################################################
120151497Sru#                    debug - diagnostic messages
121151497Sru########################################################################
122151497Sru
123151497Sruexport _DEBUG_STACKS;
124151497Sru_DEBUG_STACKS='no';		# disable stack output in each function
125151497Sru#_DEBUG_STACKS='yes';		# enable stack output in each function
126151497Sru
127151497Sruexport _DEBUG_LM;
128151497Sru_DEBUG_LM='no';			# disable landmark messages
129151497Sru#_DEBUG_LM='yes';		# enable landmark messages
130151497Sru
131151497Sruexport _DEBUG_KEEP_FILES;
132151497Sru_DEBUG_KEEP_FILES='no'		# disable file keeping in temporary dir
133151497Sru#_DEBUG_KEEP_FILES='yes'	# enable file keeping in temporary dir
134151497Sru
135151497Sruexport _DEBUG_PRINT_PARAMS;
136151497Sru_DEBUG_PRINT_PARAMS='no';	# disable printing of all parameters
137151497Sru#_DEBUG_PRINT_PARAMS='yes';	# enable printing of all parameters
138151497Sru
139151497Sruexport _DEBUG_PRINT_SHELL;
140151497Sru_DEBUG_PRINT_SHELL='no';	# disable printing of the shell name
141151497Sru#_DEBUG_PRINT_SHELL='yes';	# enable printing of the shell name
142151497Sru
143151497Sruexport _DEBUG_PRINT_TMPDIR;
144151497Sru_DEBUG_PRINT_TMPDIR='no';	# disable printing of the temporary dir
145151497Sru#_DEBUG_PRINT_TMPDIR='yes';	# enable printing of the temporary dir
146151497Sru
147151497Sruexport _DEBUG_USER_WITH_STACK;
148151497Sru_DEBUG_USER_WITH_STACK='no';	# disable stack dump in error_user()
149151497Sru#_DEBUG_USER_WITH_STACK='yes';	# enable stack dump in error_user()
150151497Sru
151151497Sru# determine all --debug* options
152151497Srucase " $*" in
153151497Sru*\ --debug*)
154151497Sru  case " $* " in
155151497Sru  *' --debug '*)
156151497Sru    # _DEBUG_STACKS='yes';
157151497Sru    # _DEBUG_LM='yes';
158151497Sru    _DEBUG_KEEP_FILES='yes';
159151497Sru    _DEBUG_PRINT_PARAMS='yes';
160151497Sru    _DEBUG_PRINT_SHELL='yes';
161151497Sru    _DEBUG_PRINT_TMPDIR='yes';
162151497Sru    _DEBUG_USER_WITH_STACK='yes';
163151497Sru    ;;
164151497Sru  esac;
165151497Sru  d=' --debug-all --debug-keep --debug-lm --debug-params --debug-shell '\
166151497Sru'--debug-stacks --debug-tmpdir --debug-user ';
167151497Sru  for i
168151497Sru  do
169151497Sru    case "$i" in
170151497Sru    --debug-s)
171151497Sru      echo 'The abbreviation --debug-s has multiple options: '\
172151497Sru'--debug-shell and --debug-stacks.' >&2
173151497Sru      exit "${_ERROR}";
174151497Sru      ;;
175151497Sru    esac;
176151497Sru    case "$d" in
177151497Sru    *\ ${i}*)
178151497Sru      # extract whole word of abbreviation $i
179151497Sru      s="$(cat <<EOF | sed -n -e 's/^.* \('"$i"'[^ ]*\) .*/\1/p'
180151497Sru$d
181151497SruEOF
182151497Sru)"
183151497Sru      case "$s" in
184151497Sru      '') continue; ;;
185151497Sru      --debug-all)
186151497Sru        _DEBUG_STACKS='yes';
187151497Sru        _DEBUG_LM='yes';
188151497Sru        _DEBUG_KEEP_FILES='yes';
189151497Sru        _DEBUG_PRINT_PARAMS='yes';
190151497Sru        _DEBUG_PRINT_SHELL='yes';
191151497Sru        _DEBUG_PRINT_TMPDIR='yes';
192151497Sru        _DEBUG_USER_WITH_STACK='yes';
193151497Sru        ;;
194151497Sru      --debug-keep)
195151497Sru        _DEBUG_PRINT_TMPDIR='yes';
196151497Sru        _DEBUG_KEEP_FILES='yes';
197151497Sru        ;;
198151497Sru      --debug-lm)
199151497Sru        _DEBUG_LM='yes';
200151497Sru        ;;
201151497Sru      --debug-params)
202151497Sru        _DEBUG_PRINT_PARAMS='yes';
203151497Sru        ;;
204151497Sru      --debug-shell)
205151497Sru        _DEBUG_PRINT_SHELL='yes';
206151497Sru        ;;
207151497Sru      --debug-stacks)
208151497Sru        _DEBUG_STACKS='yes';
209151497Sru        ;;
210151497Sru      --debug-tmpdir)
211151497Sru        _DEBUG_PRINT_TMPDIR='yes';
212151497Sru        ;;
213151497Sru      --debug-user)
214151497Sru        _DEBUG_USER_WITH_STACK='yes';
215151497Sru        ;;
216151497Sru      esac;
217151497Sru      ;;
218151497Sru    esac;
219151497Sru  done
220151497Sru  ;;
221151497Sruesac;
222151497Sru
223151497Sruif test _"${_DEBUG_PRINT_PARAMS}"_ = _yes_
224151497Sruthen
225151497Sru  echo "parameters: $@" >&2;
226151497Srufi;
227151497Sru
228151497Sruif test _"${_DEBUG_PRINT_SHELL}"_ = _yes_
229151497Sruthen
230151497Sru  if test _"${_SHELL}"_ = __
231151497Sru  then
232151497Sru    if test _"${POSIXLY_CORRECT}"_ = _y_
233151497Sru    then
234151497Sru      echo 'shell: bash as /bin/sh (none specified)' >&2;
235151497Sru    else
236151497Sru      echo 'shell: /bin/sh (none specified)' >&2;
237151497Sru    fi;
238151497Sru  else
239151497Sru    echo "shell: ${_SHELL}" >&2;
240151497Sru  fi;
241151497Srufi;
242151497Sru
243151497Sru
244151497Sru########################################################################
245151497Sru#                       Environment Variables
246151497Sru########################################################################
247151497Sru
248151497Sru# Environment variables that exist only for this file start with an
249151497Sru# underscore letter.  Global variables to this file are written in
250151497Sru# upper case letters, e.g. $_GLOBAL_VARIABLE; temporary variables
251151497Sru# start with an underline and use only lower case letters and
252151497Sru# underlines, e.g.  $_local_variable .
253151497Sru
254151497Sru#   [A-Z]*     system variables,      e.g. $MANPATH
255151497Sru#   _[A-Z_]*   global file variables, e.g. $_MAN_PATH
256151497Sru#   _[a-z_]*   temporary variables,   e.g. $_manpath
257151497Sru
258151497Sru# Due to incompatibilities of the `ash' shell, the name of loop
259151497Sru# variables in `for' must be single character
260151497Sru#   [a-z]      local loop variables,   e.g. $i
261151497Sru
262151497Sru
263151497Sru########################################################################
264151497Sru# read-only variables (global to this file)
265151497Sru########################################################################
266151497Sru
267151497Sru# function return values; `0' means ok; other values are error codes
268151497Sruexport _ALL_EXIT;
269151497Sruexport _BAD;
270151497Sruexport _GOOD;
271151497Sruexport _NO;
272151497Sruexport _OK;
273151497Sruexport _YES;
274151497Sru
275151497Sru_GOOD='0';			# return ok
276151497Sru_BAD='1';			# return negatively, error code `1'
277151497Sru# $_ERROR was already defined as `7' in groffer.sh.
278151497Sru
279151497Sru_NO="${_BAD}";
280151497Sru_YES="${_GOOD}";
281151497Sru_OK="${_GOOD}";
282151497Sru
283151497Sru# quasi-functions, call with `eval', e.g `eval "${return_ok}"'
284151497Sruexport return_ok;
285151497Sruexport return_good;
286151497Sruexport return_bad;
287151497Sruexport return_yes;
288151497Sruexport return_no;
289151497Sruexport return_error;
290151497Sruexport return_var;
291151497Srureturn_ok="func_pop; return ${_OK}";
292151497Srureturn_good="func_pop; return ${_GOOD}";
293151497Srureturn_bad="func_pop; return ${_BAD}";
294151497Srureturn_yes="func_pop; return ${_YES}";
295151497Srureturn_no="func_pop; return ${_NO}";
296151497Srureturn_error="func_pop; return ${_ERROR}";
297151497Srureturn_var="func_pop; return";	# add number, e.g. `eval "${return_var} $n'
298151497Sru
299151497Sru
300151497Sruexport _DEFAULT_MODES;
301151497Sru_DEFAULT_MODES='x,ps,tty';
302151497Sruexport _DEFAULT_RESOLUTION;
303151497Sru_DEFAULT_RESOLUTION='75';
304151497Sru
305151497Sruexport _DEFAULT_TTY_DEVICE;
306151497Sru_DEFAULT_TTY_DEVICE='latin1';
307151497Sru
308151497Sru# _VIEWER_* viewer programs for different modes (only X is necessary)
309151497Sru# _VIEWER_* a comma-separated list of viewer programs (with options)
310151497Sruexport _VIEWER_DVI;		# viewer program for dvi mode
311151497Sruexport _VIEWER_HTML_TTY;	# viewer program for html mode in tty
312151497Sruexport _VIEWER_HTML_X;		# viewer program for html mode in X
313151497Sruexport _VIEWER_PDF;		# viewer program for pdf mode
314151497Sruexport _VIEWER_PS;		# viewer program for ps mode
315151497Sruexport _VIEWER_X;		# viewer program for X mode
316151497Sru_VIEWER_DVI='kdvi,xdvi,dvilx';
317151497Sru_VIEWER_HTML_TTY='lynx';
318151497Sru_VIEWER_HTML_X='konqueror,mozilla,netscape,galeon,opera,amaya,arena';
319151497Sru_VIEWER_PDF='kghostview --scale 1.45,ggv,xpdf,acroread,kpdf';
320151497Sru_VIEWER_PS='kghostview --scale 1.45,ggv,gv,ghostview,gs_x11,gs';
321151497Sru_VIEWER_X='gxditview,xditview';
322151497Sru
323151497Sru# Search automatically in standard sections `1' to `8', and in the
324151497Sru# traditional sections `9', `n', and `o'.  On many systems, there
325151497Sru# exist even more sections, mostly containing a set of man pages
326151497Sru# special to a specific program package.  These aren't searched for
327151497Sru# automatically, but must be specified on the command line.
328151497Sruexport _MAN_AUTO_SEC_LIST;
329151497Sru_MAN_AUTO_SEC_LIST="'1' '2' '3' '4' '5' '6' '7' '8' '9' 'n' 'o'";
330151497Sruexport _MAN_AUTO_SEC_CHARS;
331151497Sru_MAN_AUTO_SEC_CHARS='[123456789no]';
332151497Sru
333151497Sruexport _SPACE_SED;
334151497Sru_SPACE_SED='['"${_SP}${_TAB}"']';
335151497Sru
336151497Sruexport _SPACE_CASE;
337151497Sru_SPACE_CASE='[\'"${_SP}"'\'"${_TAB}"']';
338151497Sru
339151497Sruexport _PROCESS_ID;		# for shutting down the program
340151497Sru_PROCESS_ID="$$";
341151497Sru
342151497Sru
343151497Sru############ the command line options of the involved programs
344151497Sru#
345151497Sru# The naming scheme for the options environment names is
346151497Sru# $_OPTS_<prog>_<length>[_<argspec>]
347151497Sru#
348151497Sru# <prog>:    program name GROFFER, GROFF, or CMDLINE (for all
349151497Sru#            command line options)
350151497Sru# <length>:  LONG (long options) or SHORT (single character options)
351151497Sru# <argspec>: ARG for options with argument, NA for no argument;
352151497Sru#            without _<argspec> both the ones with and without arg.
353151497Sru#
354151497Sru# Each option that takes an argument must be specified with a
355151497Sru# trailing : (colon).
356151497Sru
357151497Sru# exports
358151497Sruexport _OPTS_GROFFER_SHORT_NA;
359151497Sruexport _OPTS_GROFFER_SHORT_ARG;
360151497Sruexport _OPTS_GROFFER_LONG_NA;
361151497Sruexport _OPTS_GROFFER_LONG_ARG;
362151497Sruexport _OPTS_GROFF_SHORT_NA;
363151497Sruexport _OPTS_GROFF_SHORT_ARG;
364151497Sruexport _OPTS_GROFF_LONG_NA;
365151497Sruexport _OPTS_GROFF_LONG_ARG;
366151497Sruexport _OPTS_X_SHORT_ARG;
367151497Sruexport _OPTS_X_SHORT_NA;
368151497Sruexport _OPTS_X_LONG_ARG;
369151497Sruexport _OPTS_X_LONG_NA;
370151497Sruexport _OPTS_MAN_SHORT_ARG;
371151497Sruexport _OPTS_MAN_SHORT_NA;
372151497Sruexport _OPTS_MAN_LONG_ARG;
373151497Sruexport _OPTS_MAN_LONG_NA;
374151497Sruexport _OPTS_MANOPT_SHORT_ARG;
375151497Sruexport _OPTS_MANOPT_SHORT_NA;
376151497Sruexport _OPTS_MANOPT_LONG_ARG;
377151497Sruexport _OPTS_MANOPT_LONG_NA;
378151497Sruexport _OPTS_CMDLINE_SHORT_NA;
379151497Sruexport _OPTS_CMDLINE_SHORT_ARG;
380151497Sruexport _OPTS_CMDLINE_LONG_NA;
381151497Sruexport _OPTS_CMDLINE_LONG_ARG;
382151497Sru
383151497Sru###### groffer native options
384151497Sru
385151497Sru_OPTS_GROFFER_SHORT_NA="'h' 'Q' 'v' 'V' 'X' 'Z'";
386151497Sru_OPTS_GROFFER_SHORT_ARG="'T'";
387151497Sru
388151497Sru_OPTS_GROFFER_LONG_NA="'auto' \
389151497Sru'apropos' 'apropos-data' 'apropos-devel' 'apropos-progs' \
390151497Sru'debug' 'debug-all' 'debug-keep' 'debug-lm' 'debug-params' 'debug-shell' \
391151497Sru'debug-stacks' 'debug-tmpdir' 'debug-user' 'default' 'do-nothing' 'dvi' \
392151497Sru'groff' 'help' 'intermediate-output' 'html' 'man' \
393151497Sru'no-location' 'no-man' 'no-special' 'pdf' 'ps' 'rv' 'source' \
394151497Sru'text' 'text-device' \
395151497Sru'tty' 'tty-device' 'version' 'whatis' 'where' 'www' 'x' 'X'";
396151497Sru
397151497Sru_OPTS_GROFFER_LONG_ARG="\
398151497Sru'default-modes' 'device' 'dvi-viewer' 'dvi-viewer-tty' 'extension' 'fg' \
399151497Sru'fn' 'font' 'foreground' 'html-viewer' 'html-viewer-tty' 'mode' \
400151497Sru'pdf-viewer' 'pdf-viewer-tty' 'print' 'ps-viewer' 'ps-viewer-tty' 'shell' \
401151497Sru'title' 'tty-viewer' 'tty-viewer-tty' 'www-viewer' 'www-viewer-tty' \
402151497Sru'x-viewer' 'x-viewer-tty' 'X-viewer' 'X-viewer-tty'";
403151497Sru
404151497Sru##### groffer options inhereted from groff
405151497Sru
406151497Sru_OPTS_GROFF_SHORT_NA="'a' 'b' 'c' 'C' 'e' 'E' 'g' 'G' 'i' 'l' 'N' 'p' \
407151497Sru'R' 's' 'S' 't' 'U' 'z'";
408151497Sru_OPTS_GROFF_SHORT_ARG="'d' 'f' 'F' 'I' 'L' 'm' 'M' 'n' 'o' 'P' 'r' \
409151497Sru'w' 'W'";
410151497Sru_OPTS_GROFF_LONG_NA="";
411151497Sru_OPTS_GROFF_LONG_ARG="";
412151497Sru
413151497Sru##### groffer options inhereted from the X Window toolkit
414151497Sru
415151497Sru_OPTS_X_SHORT_NA="";
416151497Sru_OPTS_X_SHORT_ARG="";
417151497Sru
418151497Sru_OPTS_X_LONG_NA="'iconic' 'rv'";
419151497Sru
420151497Sru_OPTS_X_LONG_ARG="'background' 'bd' 'bg' 'bordercolor' 'borderwidth' \
421151497Sru'bw' 'display' 'fg' 'fn' 'font' 'foreground' 'ft' 'geometry' \
422151497Sru'resolution' 'title' 'xrm'";
423151497Sru
424151497Sru###### groffer options inherited from man
425151497Sru
426151497Sru_OPTS_MAN_SHORT_NA="";
427151497Sru_OPTS_MAN_SHORT_ARG="";
428151497Sru
429151497Sru_OPTS_MAN_LONG_NA="'all' 'ascii' 'catman' 'ditroff' \
430151497Sru'local-file' 'location' 'troff' 'update'";
431151497Sru
432151497Sru_OPTS_MAN_LONG_ARG="'locale' 'manpath' \
433151497Sru'pager' 'preprocessor' 'prompt' 'sections' 'systems' 'troff-device'";
434151497Sru
435151497Sru###### additional options for parsing $MANOPT only
436151497Sru
437151497Sru_OPTS_MANOPT_SHORT_NA="'7' 'a' 'c' 'd' 'D' 'f' 'h' 'k' 'l' 't' 'u' \
438151497Sru'V' 'w' 'Z'";
439151497Sru_OPTS_MANOPT_SHORT_ARG="'e' 'L' 'm' 'M' 'p' 'P' 'r' 'S' 'T'";
440151497Sru
441151497Sru_OPTS_MANOPT_LONG_NA="${_OPTS_MAN_LONG_NA} \
442151497Sru'apropos' 'debug' 'default' 'help' 'html' 'ignore-case' 'location-cat' \
443151497Sru'match-case' 'troff' 'update' 'version' 'whatis' 'where' 'where-cat'";
444151497Sru
445151497Sru_OPTS_MANOPT_LONG_ARG="${_OPTS_MAN_LONG_NA} \
446151497Sru'config_file' 'encoding' 'extension' 'locale'";
447151497Sru
448151497Sru###### collections of command line options
449151497Sru
450151497Sru_OPTS_CMDLINE_SHORT_NA="${_OPTS_GROFFER_SHORT_NA} \
451151497Sru${_OPTS_GROFF_SHORT_NA} ${_OPTS_X_SHORT_NA} ${_OPTS_MAN_SHORT_NA}";
452151497Sru_OPTS_CMDLINE_SHORT_ARG="${_OPTS_GROFFER_SHORT_ARG} \
453151497Sru${_OPTS_GROFF_SHORT_ARG} ${_OPTS_X_SHORT_ARG} ${_OPTS_MAN_SHORT_ARG}";
454151497Sru
455151497Sru_OPTS_CMDLINE_LONG_NA="${_OPTS_GROFFER_LONG_NA} \
456151497Sru${_OPTS_GROFF_LONG_NA} ${_OPTS_X_LONG_NA} ${_OPTS_MAN_LONG_NA}";
457151497Sru_OPTS_CMDLINE_LONG_ARG="${_OPTS_GROFFER_LONG_ARG} \
458151497Sru${_OPTS_GROFF_LONG_ARG} ${_OPTS_MAN_LONG_ARG} ${_OPTS_X_LONG_ARG}";
459151497Sru
460151497Sru
461151497Sru########################################################################
462151497Sru# read-write variables (global to this file)
463151497Sru########################################################################
464151497Sru
465151497Sruexport _ALL_PARAMS;		# All options and file name parameters
466151497Sruexport _ADDOPTS_GROFF;		# Transp. options for groff (`eval').
467151497Sruexport _ADDOPTS_POST;		# Transp. options postproc (`eval').
468151497Sruexport _ADDOPTS_X;		# Transp. options X postproc (`eval').
469151497Sruexport _APROPOS_PROG;		# Program to run apropos.
470151497Sruexport _APROPOS_SECTIONS;	# Sections for different --apropos-*.
471151497Sruexport _DEFAULT_MODES;		# Set default modes.
472151497Sruexport _DISPLAY_MODE;		# Display mode.
473151497Sruexport _DISPLAY_PROG;		# Viewer program to be used for display.
474151497Sruexport _DISPLAY_ARGS;		# X resources for the viewer program.
475151497Sruexport _FILEARGS;		# Stores filespec parameters.
476151497Sruexport _FILESPEC_ARG;		# Stores the actual filespec parameter.
477151497Sruexport _FUNC_STACK;		# Store debugging information.
478151497Sruexport _REGISTERED_TITLE;	# Processed file names.
479151497Sru# _HAS_* from availability tests
480151497Sruexport _HAS_COMPRESSION;	# `yes' if gzip compression is available
481151497Sruexport _HAS_BZIP;		# `yes' if bzip2 compression is available
482151497Sru# _MAN_* finally used configuration of man searching
483151497Sruexport _MAN_ALL;		# search all man pages per filespec
484151497Sruexport _MAN_ENABLE;		# enable search for man pages
485151497Sruexport _MAN_EXT;		# extension for man pages
486151497Sruexport _MAN_FORCE;		# force file parameter to be man pages
487151497Sruexport _MAN_IS_SETUP;		# setup man variables only once
488151497Sruexport _MAN_LANG;		# language for man pages
489151497Sruexport _MAN_LANG2;		# language for man pages
490151497Sruexport _MAN_LANG_DONE;		# language dirs added to man path
491151497Sruexport _MAN_PATH;		# search path for man pages
492151497Sruexport _MAN_SEC;		# sections for man pages; sep. `:'
493151497Sruexport _MAN_SEC_DONE;		# sections added to man path
494151497Sruexport _MAN_SYS;		# system names for man pages; sep. `,'
495151497Sruexport _MAN_SYS;		# system names added to man path
496151497Sru# _MANOPT_* as parsed from $MANOPT
497151497Sruexport _MANOPT_ALL;		# $MANOPT --all
498151497Sruexport _MANOPT_EXTENSION;	# $MANOPT --extension
499151497Sruexport _MANOPT_LANG;		# $MANOPT --locale
500151497Sruexport _MANOPT_PATH;		# $MANOPT --manpath
501151497Sruexport _MANOPT_PAGER;		# $MANOPT --pager
502151497Sruexport _MANOPT_SEC;		# $MANOPT --sections
503151497Sruexport _MANOPT_SYS;		# $MANOPT --systems
504151497Sru# _OPT_* as parsed from groffer command line
505151497Sruexport _OPT_ALL;		# display all suitable man pages.
506151497Sruexport _OPT_APROPOS;		# call `apropos' program.
507151497Sruexport _OPT_BD;			# set border color in some modes.
508151497Sruexport _OPT_BG;			# set background color in some modes.
509151497Sruexport _OPT_BW;			# set border width in some modes.
510151497Sruexport _OPT_DEFAULT_MODES;	# `,'-list of modes when no mode given.
511151497Sruexport _OPT_DEVICE;		# device option.
512151497Sruexport _OPT_DO_NOTHING;		# do nothing in main_display().
513151497Sruexport _OPT_DISPLAY;		# set X display.
514151497Sruexport _OPT_FG;			# set foreground color in some modes.
515151497Sruexport _OPT_FN;			# set font in some modes.
516151497Sruexport _OPT_GEOMETRY;		# set size and position of viewer in X.
517151497Sruexport _OPT_ICONIC;		# -iconic option for X viewers.
518151497Sruexport _OPT_LANG;		# set language for man pages
519151497Sruexport _OPT_LOCATION;		# print processed file names to stderr
520151497Sruexport _OPT_MODE;		# values: X, tty, Q, Z, ""
521151497Sruexport _OPT_MANPATH;		# manual setting of path for man-pages
522151497Sruexport _OPT_PAGER;		# specify paging program for tty mode
523151497Sruexport _OPT_RESOLUTION;		# set X resolution in dpi
524151497Sruexport _OPT_RV;			# reverse fore- and background colors.
525151497Sruexport _OPT_SECTIONS;		# sections for man page search
526151497Sruexport _OPT_SYSTEMS;		# man pages of different OS's
527151497Sruexport _OPT_TITLE;		# title for gxditview window
528151497Sruexport _OPT_TEXT_DEVICE;	# set device for tty mode.
529151497Sruexport _OPT_V;			# groff option -V.
530151497Sruexport _OPT_VIEWER_DVI;		# viewer program for dvi mode
531151497Sruexport _OPT_VIEWER_PDF;		# viewer program for pdf mode
532151497Sruexport _OPT_VIEWER_PS;		# viewer program for ps mode
533151497Sruexport _OPT_VIEWER_HTML;	# viewer program for html mode
534151497Sruexport _OPT_VIEWER_X;		# viewer program for x mode
535151497Sruexport _OPT_WHATIS;		# print the man description
536151497Sruexport _OPT_XRM;		# specify X resource.
537151497Sruexport _OPT_Z;			# groff option -Z.
538151497Sruexport _OUTPUT_FILE_NAME;	# output generated, see main_set_res..()
539151497Sruexport _VIEWER_TERMINAL;	# viewer options for terminal (--*-viewer-tty)
540151497Sru# _TMP_* temporary directory and files
541151497Sruexport _TMP_DIR;		# groffer directory for temporary files
542151497Sruexport _TMP_CAT;		# stores concatenation of everything
543151497Sruexport _TMP_STDIN;		# stores stdin, if any
544151497Sru
545151497Sru# these variables are preset in section `Preset' after the rudim. test
546151497Sru
547151497Sru
548151497Sru########################################################################
549151497Sru# Preset and reset of read-write global variables
550151497Sru########################################################################
551151497Sru
552151497Sru
553151497Sruexport _START_DIR;		# directory at start time of the script
554151497Sru_START_DIR="$(pwd)";
555151497Sru
556151497Sru# For variables that can be reset by option `--default', see reset().
557151497Sru
558151497Sru_FILEARGS='';
559151497Sru
560151497Sru# _HAS_* from availability tests
561151497Sru_HAS_COMPRESSION='';
562151497Sru_HAS_BZIP='';
563151497Sru
564151497Sru# _TMP_* temporary files
565151497Sru_TMP_DIR='';
566151497Sru_TMP_CAT='';
567151497Sru_TMP_CONF='';
568151497Sru_TMP_STDIN='';
569151497Sru
570151497Sru
571151497Sru########################################################################
572151497Sru# reset ()
573151497Sru#
574151497Sru# Reset the variables that can be affected by options to their default.
575151497Sru#
576151497Srureset()
577151497Sru{
578151497Sru  if test "$#" -ne 0
579151497Sru  then
580151497Sru    error "reset() does not have arguments.";
581151497Sru  fi;
582151497Sru
583151497Sru  _ADDOPTS_GROFF='';
584151497Sru  _ADDOPTS_POST='';
585151497Sru  _ADDOPTS_X='';
586151497Sru  _APROPOS_PROG='';
587151497Sru  _APROPOS_SECTIONS='';
588151497Sru  _DISPLAY_ARGS='';
589151497Sru  _DISPLAY_MODE='';
590151497Sru  _DISPLAY_PROG='';
591151497Sru  _REGISTERED_TITLE='';
592151497Sru
593151497Sru  # _MAN_* finally used configuration of man searching
594151497Sru  _MAN_ALL='no';
595151497Sru  _MAN_ENABLE='yes';		# do search for man-pages
596151497Sru  _MAN_EXT='';
597151497Sru  _MAN_FORCE='no';		# first local file, then search man page
598151497Sru  _MAN_IS_SETUP='no';
599151497Sru  _MAN_LANG='';
600151497Sru  _MAN_LANG2='';
601151497Sru  _MAN_PATH='';
602151497Sru  _MAN_SEC='';
603151497Sru  _MAN_SEC_DONE='no';
604151497Sru  _MAN_SYS='';
605151497Sru  _MAN_SYS_DONE='no';
606151497Sru
607151497Sru  # _MANOPT_* as parsed from $MANOPT
608151497Sru  _MANOPT_ALL='no';
609151497Sru  _MANOPT_EXTENSION='';
610151497Sru  _MANOPT_LANG='';
611151497Sru  _MANOPT_PATH='';
612151497Sru  _MANOPT_PAGER='';
613151497Sru  _MANOPT_SEC='';
614151497Sru  _MANOPT_SYS='';
615151497Sru
616151497Sru  # _OPT_* as parsed from groffer command line
617151497Sru  _OPT_ALL='no';
618151497Sru  _OPT_APROPOS='no';
619151497Sru  _OPT_BD='';
620151497Sru  _OPT_BG='';
621151497Sru  _OPT_BW='';
622151497Sru  _OPT_DEFAULT_MODES='';
623151497Sru  _OPT_DEVICE='';
624151497Sru  _OPT_DISPLAY='';
625151497Sru  _OPT_DO_NOTHING='no';
626151497Sru  _OPT_FG='';
627151497Sru  _OPT_FN='';
628151497Sru  _OPT_GEOMETRY='';
629151497Sru  _OPT_ICONIC='no';
630151497Sru  _OPT_LANG='';
631151497Sru  _OPT_LOCATION='no';
632151497Sru  _OPT_MODE='';
633151497Sru  _OPT_MANPATH='';
634151497Sru  _OPT_PAGER='';
635151497Sru  _OPT_RESOLUTION='';
636151497Sru  _OPT_RV='no';
637151497Sru  _OPT_SECTIONS='';
638151497Sru  _OPT_SYSTEMS='';
639151497Sru  _OPT_TITLE='';
640151497Sru  _OPT_TEXT_DEVICE='';
641151497Sru  _OPT_V='no';
642151497Sru  _OPT_VIEWER_DVI='';
643151497Sru  _OPT_VIEWER_PDF='';
644151497Sru  _OPT_VIEWER_PS='';
645151497Sru  _OPT_VIEWER_HTML='';
646151497Sru  _OPT_VIEWER_X='';
647151497Sru  _OPT_WHATIS='no';
648151497Sru  _OPT_XRM='';
649151497Sru  _OPT_Z='no';
650151497Sru  _VIEWER_TERMINAL='no';
651151497Sru}
652151497Sru
653151497Srureset;
654151497Sru
655151497Sru
656151497Sru########################################################################
657151497Sru#          Functions for error handling and debugging
658151497Sru########################################################################
659151497Sru
660151497Sru
661151497Sru##############
662151497Sru# echo1 (<text>*)
663151497Sru#
664151497Sru# Output to stdout.
665151497Sru#
666151497Sru# Arguments : arbitrary text including `-'.
667151497Sru#
668151497Sruecho1()
669151497Sru{
670151497Sru  cat <<EOF
671151497Sru$@
672151497SruEOF
673151497Sru}
674151497Sru
675151497Sru
676151497Sru##############
677151497Sru# echo2 (<text>*)
678151497Sru#
679151497Sru# Output to stderr.
680151497Sru#
681151497Sru# Arguments : arbitrary text.
682151497Sru#
683151497Sruecho2()
684151497Sru{
685151497Sru  cat >&2 <<EOF
686151497Sru$@
687151497SruEOF
688151497Sru}
689151497Sru
690151497Sru
691151497Sru##############
692151497Sru# landmark (<text>)
693151497Sru#
694151497Sru# Print <text> to standard error as a debugging aid.
695151497Sru#
696151497Sru# Globals: $_DEBUG_LM
697151497Sru#
698151497Srulandmark()
699151497Sru{
700151497Sru  if test _"${_DEBUG_LM}"_ = _yes_
701151497Sru  then
702151497Sru    echo2 "LM: $*";
703151497Sru  fi;
704151497Sru}
705151497Sru
706151497Srulandmark "1: debugging functions";
707151497Sru
708151497Sru
709151497Sru##############
710151497Sru# clean_up ()
711151497Sru#
712151497Sru# Clean up at exit.
713151497Sru#
714151497Sruclean_up()
715151497Sru{
716151497Sru  cd "${_START_DIR}" >"${_NULL_DEV}" 2>&1;
717151497Sru  if test _${_DEBUG_KEEP_FILES}_ = _yes_
718151497Sru  then
719151497Sru    echo2 "Kept temporary directory ${_TMP_DIR}."
720151497Sru  else
721151497Sru    if test _"${_TMP_DIR}"_ != __
722151497Sru    then
723151497Sru      if test -d "${_TMP_DIR}" || test -f "${_TMP_DIR}"
724151497Sru      then
725151497Sru        rm -f -r "${_TMP_DIR}" >${_NULL_DEV} 2>&1;
726151497Sru      fi; 
727151497Sru    fi;
728151497Sru  fi;
729151497Sru}
730151497Sru
731151497Sru
732151497Sru#############
733151497Sru# diag (text>*)
734151497Sru#
735151497Sru# Output a diagnostic message to stderr
736151497Sru#
737151497Srudiag()
738151497Sru{
739151497Sru  echo2 '>>>>>'"$*";
740151497Sru}
741151497Sru
742151497Sru
743151497Sru#############
744151497Sru# error (<text>*)
745151497Sru#
746151497Sru# Print an error message to standard error, print the function stack,
747151497Sru# exit with an error condition.  The argument should contain the name
748151497Sru# of the function from which it was called.  This is for system errors.
749151497Sru#
750151497Sruerror()
751151497Sru{
752151497Sru  case "$#" in
753151497Sru    1) echo2 'groffer error: '"$1"; ;;
754151497Sru    *) echo2 'groffer error: wrong number of arguments in error().'; ;;
755151497Sru  esac;
756151497Sru  func_stack_dump;
757151497Sru  if test _"${_TMP_DIR}"_ != __ && test -d "${_TMP_DIR}"
758151497Sru  then
759151497Sru    : >"${_TMP_DIR}"/,error;
760151497Sru  fi;
761151497Sru  exit "${_ERROR}";
762151497Sru}
763151497Sru
764151497Sru
765151497Sru#############
766151497Sru# error_user (<text>*)
767151497Sru#
768151497Sru# Print an error message to standard error; exit with an error condition.
769151497Sru# The error is supposed to be produce by the user.  So the funtion stack
770151497Sru# is omitted.
771151497Sru#
772151497Sruerror_user()
773151497Sru{
774151497Sru  case "$#" in
775151497Sru    1)
776151497Sru      echo2 'groffer error: '"$1";
777151497Sru       ;;
778151497Sru    *)
779151497Sru      echo2 'groffer error: wrong number of arguments in error_user().';
780151497Sru      ;;
781151497Sru  esac;
782151497Sru  if test _"${_DEBUG_USER_WITH_STACK}"_ = _yes_
783151497Sru  then
784151497Sru    func_stack_dump;
785151497Sru  fi;
786151497Sru  if test _"${_TMP_DIR}"_ != __ && test -d "${_TMP_DIR}"
787151497Sru  then
788151497Sru    : >"${_TMP_DIR}"/,error;
789151497Sru  fi;
790151497Sru  exit "${_ERROR}";
791151497Sru}
792151497Sru
793151497Sru
794151497Sru#############
795151497Sru# exit_test ()
796151497Sru#
797151497Sru# Test whether the former command ended with error().  Exit again.
798151497Sru#
799151497Sru# Globals: $_ERROR
800151497Sru#
801151497Sruexit_test()
802151497Sru{
803151497Sru  if test "$?" = "${_ERROR}"
804151497Sru  then
805151497Sru    exit ${_ERROR};
806151497Sru  fi;
807151497Sru  if test _"${_TMP_DIR}"_ != __ && test -f "${_TMP_DIR}"/,error
808151497Sru  then
809151497Sru    exit ${_ERROR};
810151497Sru  fi;
811151497Sru}
812151497Sru
813151497Sru
814151497Sru#############
815151497Sru# func_check (<func_name> <rel_op> <nr_args> "$@")
816151497Sru#
817151497Sru# Check number of arguments and register to _FUNC_STACK.
818151497Sru#
819151497Sru# Arguments: >=3
820151497Sru#   <func_name>: name of the calling function.
821151497Sru#   <rel_op>:    a relational operator: = != < > <= >=
822151497Sru#   <nr_args>:   number of arguments to be checked against <operator>
823151497Sru#   "$@":        the arguments of the calling function.
824151497Sru#
825151497Sru# Variable prefix: fc
826151497Sru#
827151497Srufunc_check()
828151497Sru{
829151497Sru  if test "$#" -lt 3
830151497Sru  then
831151497Sru    error 'func_check() needs at least 3 arguments.';
832151497Sru  fi;
833151497Sru  fc_fname="$1";
834151497Sru  case "$3" in
835151497Sru    1)
836151497Sru      fc_nargs="$3";
837151497Sru      fc_s='';
838151497Sru      ;;
839151497Sru    0|[2-9])
840151497Sru      fc_nargs="$3";
841151497Sru      fc_s='s';
842151497Sru      ;;
843151497Sru    *)
844151497Sru      error "func_check(): third argument must be a digit.";
845151497Sru      ;;
846151497Sru  esac;
847151497Sru  case "$2" in
848151497Sru    '='|'-eq')
849151497Sru      fc_op='-eq';
850151497Sru      fc_comp='exactly';
851151497Sru      ;;
852151497Sru    '>='|'-ge')
853151497Sru      fc_op='-ge';
854151497Sru      fc_comp='at least';
855151497Sru      ;;
856151497Sru    '<='|'-le')
857151497Sru      fc_op='-le';
858151497Sru      fc_comp='at most';
859151497Sru      ;;
860151497Sru    '<'|'-lt')
861151497Sru      fc_op='-lt';
862151497Sru      fc_comp='less than';
863151497Sru      ;;
864151497Sru    '>'|'-gt')
865151497Sru      fc_op='-gt';
866151497Sru      fc_comp='more than';
867151497Sru      ;;
868151497Sru    '!='|'-ne')
869151497Sru      fc_op='-ne';
870151497Sru      fc_comp='not';
871151497Sru      ;;
872151497Sru    *)
873151497Sru      error \
874151497Sru        'func_check(): second argument is not a relational operator.';
875151497Sru      ;;
876151497Sru  esac;
877151497Sru  shift;
878151497Sru  shift;
879151497Sru  shift;
880151497Sru  if test "$#" "${fc_op}" "${fc_nargs}"
881151497Sru  then
882151497Sru    do_nothing;
883151497Sru  else
884151497Sru    error "func_check(): \
885151497Sru${fc_fname}"'() needs '"${fc_comp} ${fc_nargs}"' argument'"${fc_s}"'.';
886151497Sru  fi;
887151497Sru  func_push "${fc_fname}";
888151497Sru  if test _"${_DEBUG_STACKS}"_ = _yes_
889151497Sru  then
890151497Sru    echo2 '+++ '"${fc_fname} $@";
891151497Sru    echo2 '>>> '"${_FUNC_STACK}";
892151497Sru  fi;
893151497Sru  eval ${_UNSET} fc_comp;
894151497Sru  eval ${_UNSET} fc_fname;
895151497Sru  eval ${_UNSET} fc_nargs;
896151497Sru  eval ${_UNSET} fc_op;
897151497Sru  eval ${_UNSET} fc_s;
898151497Sru}
899151497Sru
900151497Sru
901151497Sru#############
902151497Sru# func_pop ()
903151497Sru#
904151497Sru# Retrieve the top element from the stack.
905151497Sru#
906151497Sru# The stack elements are separated by `!'; the popped element is
907151497Sru# identical to the original element, except that all `!' characters
908151497Sru# were removed.
909151497Sru#
910151497Sru# Arguments: 1
911151497Sru#
912151497Srufunc_pop()
913151497Sru{
914151497Sru  if test "$#" -ne 0
915151497Sru  then
916151497Sru    error 'func_pop() does not have arguments.';
917151497Sru  fi;
918151497Sru  case "${_FUNC_STACK}" in
919151497Sru  '')
920151497Sru    if test _"${_DEBUG_STACKS}"_ = _yes_
921151497Sru    then
922151497Sru      error 'func_pop(): stack is empty.';
923151497Sru    fi;
924151497Sru    ;;
925151497Sru  *!*)
926151497Sru    # split at first bang `!'.
927151497Sru    _FUNC_STACK="$(echo1 "${_FUNC_STACK}" | sed -e 's/^[^!]*!//')";
928151497Sru    exit_test;
929151497Sru    ;;
930151497Sru  *)
931151497Sru    _FUNC_STACK='';
932151497Sru    ;;
933151497Sru  esac;
934151497Sru  if test _"${_DEBUG_STACKS}"_ = _yes_
935151497Sru  then
936151497Sru    echo2 '<<< '"${_FUNC_STACK}";
937151497Sru  fi;
938151497Sru}
939151497Sru
940151497Sru
941151497Sru#############
942151497Sru# func_push (<element>)
943151497Sru#
944151497Sru# Store another element to stack.
945151497Sru#
946151497Sru# The stack elements are separated by `!'; if <element> contains a `!'
947151497Sru# it is removed first.
948151497Sru#
949151497Sru# Arguments: 1
950151497Sru#
951151497Sru# Variable prefix: fp
952151497Sru#
953151497Srufunc_push()
954151497Sru{
955151497Sru  if test "$#" -ne 1
956151497Sru  then
957151497Sru    error 'func_push() needs 1 argument.';
958151497Sru  fi;
959151497Sru  case "$1" in
960151497Sru  *'!'*)
961151497Sru    # remove all bangs `!'.
962151497Sru    fp_element="$(echo1 "$1" | sed -e 's/!//g')";
963151497Sru    exit_test;
964151497Sru    ;;
965151497Sru  *)
966151497Sru    fp_element="$1";
967151497Sru    ;;
968151497Sru  esac;
969151497Sru  if test _"${_FUNC_STACK}"_ = __
970151497Sru  then
971151497Sru    _FUNC_STACK="${fp_element}";
972151497Sru  else
973151497Sru    _FUNC_STACK="${fp_element}!${_FUNC_STACK}";
974151497Sru  fi;
975151497Sru  eval ${_UNSET} fp_element;
976151497Sru}
977151497Sru
978151497Sru
979151497Sru#############
980151497Sru# func_stack_dump ()
981151497Sru#
982151497Sru# Print the content of the stack.  Ignore the arguments.
983151497Sru#
984151497Srufunc_stack_dump()
985151497Sru{
986151497Sru  diag 'call stack: '"${_FUNC_STACK}";
987151497Sru}
988151497Sru
989151497Sru
990151497Sru########################################################################
991151497Sru#                        System Test
992151497Sru########################################################################
993151497Sru
994151497Srulandmark "2: system test";
995151497Sru
996151497Sru# Test the availability of the system utilities used in this script.
997151497Sru
998151497Sru
999151497Sru########################################################################
1000151497Sru# Test of function `sed'.
1001151497Sru#
1002151497Sru
1003151497Sruif test _"$(echo xTesTx \
1004151497Sru           | sed -e 's/^.\([Tt]e*x*sTT*\).*$/\1/' \
1005151497Sru           | sed -e 's|T|t|g')"_ != _test_
1006151497Sruthen
1007151497Sru  error 'Test of "sed" command failed.';
1008151497Srufi;
1009151497Sru
1010151497Sru
1011151497Sru########################################################################
1012151497Sru# Test of function `cat'.
1013151497Sru#
1014151497Sruif test _"$(echo test | cat)"_ != _test_
1015151497Sruthen
1016151497Sru  error 'Test of "cat" command failed.';
1017151497Srufi;
1018151497Sru
1019151497Sru
1020151497Sru########################################################################
1021151497Sru# Test for compression.
1022151497Sru#
1023151497Sruif test _"$(echo 'test' | gzip -c -d -f - 2>${_NULL_DEV})"_ = _test_
1024151497Sruthen
1025151497Sru  _HAS_COMPRESSION='yes';
1026151497Sru  if echo1 'test' | bzip2 -c 2>${_NULL_DEV} | bzip2 -t 2>${_NULL_DEV} \
1027151497Sru     && test _"$(echo 'test' | bzip2 -c 2>${_NULL_DEV} \
1028151497Sru                             | bzip2 -d -c 2>${_NULL_DEV})"_ \
1029151497Sru             = _test_
1030151497Sru  then
1031151497Sru    _HAS_BZIP='yes';
1032151497Sru  else
1033151497Sru    _HAS_BZIP='no';
1034151497Sru  fi;
1035151497Sruelse
1036151497Sru  _HAS_COMPRESSION='no';
1037151497Sru  _HAS_BZIP='no';
1038151497Srufi;
1039151497Sru
1040151497Sru
1041151497Sru########################################################################
1042151497Sru#       Definition of normal Functions in alphabetical order
1043151497Sru########################################################################
1044151497Srulandmark "3: functions";
1045151497Sru
1046151497Sru########################################################################
1047151497Sru# apropos_filespec ()
1048151497Sru#
1049151497Sru# Setup for the --apropos* options
1050151497Sru#
1051151497Sruapropos_filespec()
1052151497Sru{
1053151497Sru
1054151497Sru  func_check apropos_filespec '=' 0 "$@";
1055151497Sru  if obj _OPT_APROPOS is_yes
1056151497Sru  then
1057151497Sru    eval to_tmp_line \
1058151497Sru      "'.SH $(echo1 "${_FILESPEC_ARG}" | sed 's/[^\\]-/\\-/g')'";
1059151497Sru    exit_test;
1060151497Sru    if obj _APROPOS_PROG is_empty
1061151497Sru    then
1062151497Sru      error 'apropos_filespec: apropos_setup() must be run first.';
1063151497Sru    fi;
1064151497Sru    if obj _APROPOS_SECTIONS is_empty
1065151497Sru    then
1066151497Sru      if obj _OPT_SECTIONS is_empty
1067151497Sru      then
1068151497Sru        s='^.*(.*).*$';
1069151497Sru      else
1070151497Sru        s='^.*(['"$(echo1 "${_OPT_SECTIONS}" | sed -e 's/://g')"']';
1071151497Sru      fi;
1072151497Sru    else
1073151497Sru      s='^.*(['"${_APROPOS_SECTIONS}"']';
1074151497Sru    fi;
1075151497Sru    eval "${_APROPOS_PROG}" "'${_FILESPEC_ARG}'" | \
1076151497Sru      sed -n -e '
1077151497Sru/^'"${_FILESPEC_ARG}"': /p
1078151497Sru/'"$s"'/p
1079151497Sru' | \
1080151497Sru      sort |\
1081151497Sru      sed -e '
1082151497Srus/^\(.* (..*)\)  *-  *\(.*\)$/\.br\n\.TP 15\n\.BR \1\n\2/
1083151497Sru' >>"${_TMP_CAT}";
1084151497Sru  fi;
1085151497Sru  eval "${return_ok}";
1086151497Sru}
1087151497Sru
1088151497Sru
1089151497Sru########################################################################
1090151497Sru# apropos_setup ()
1091151497Sru#
1092151497Sru# Setup for the --apropos* options
1093151497Sru#
1094151497Sruapropos_setup()
1095151497Sru{
1096151497Sru  func_check apropos_setup '=' 0 "$@";
1097151497Sru  if obj _OPT_APROPOS is_yes
1098151497Sru  then
1099151497Sru    if is_prog apropos
1100151497Sru    then
1101151497Sru      _APROPOS_PROG='apropos';
1102151497Sru    elif is_prog man
1103151497Sru    then
1104151497Sru      if man --apropos man >${_NULL_DEV} 2>${_NULL_DEV}
1105151497Sru      then
1106151497Sru        _APROPOS_PROG='man --apropos';
1107151497Sru      elif man -k man >${_NULL_DEV} 2>${_NULL_DEV}
1108151497Sru      then
1109151497Sru        _APROPOS_PROG='man -k';
1110151497Sru      fi;
1111151497Sru    fi;
1112151497Sru    if obj _APROPOS_PROG is_empty
1113151497Sru    then
1114151497Sru      error 'apropos_setup: no apropos program available.';
1115151497Sru    fi;
1116151497Sru    to_tmp_line '.TH GROFFER APROPOS';
1117151497Sru  fi;
1118151497Sru  eval "${return_ok}";
1119151497Sru}
1120151497Sru
1121151497Sru
1122151497Sru########################################################################
1123151497Sru# base_name (<path>)
1124151497Sru#
1125151497Sru# Get the file name part of <path>, i.e. delete everything up to last
1126151497Sru# `/' from the beginning of <path>.  Remove final slashes, too, to get a
1127151497Sru# non-empty output.
1128151497Sru#
1129151497Sru# Arguments : 1
1130151497Sru# Output    : the file name part (without slashes)
1131151497Sru#
1132151497Sru# Variable prefix: bn
1133151497Sru#
1134151497Srubase_name()
1135151497Sru{
1136151497Sru  func_check base_name = 1 "$@";
1137151497Sru  bn_name="$1";
1138151497Sru  case "${bn_name}" in
1139151497Sru    */)
1140151497Sru      # delete all final slashes
1141151497Sru      bn_name="$(echo1 "${bn_name}" | sed -e 's|//*$||')";
1142151497Sru      exit_test;
1143151497Sru      ;;
1144151497Sru  esac;
1145151497Sru  case "${bn_name}" in
1146151497Sru    /|'')
1147151497Sru      eval ${_UNSET} bn_name;
1148151497Sru      eval "${return_bad}";
1149151497Sru      ;;
1150151497Sru    */*)
1151151497Sru      # delete everything before and including the last slash `/'.
1152151497Sru      echo1 "${bn_name}" | sed -e 's|^.*//*\([^/]*\)$|\1|';
1153151497Sru      ;;
1154151497Sru    *)
1155151497Sru      obj bn_name echo1;
1156151497Sru      ;;
1157151497Sru  esac;
1158151497Sru  eval ${_UNSET} bn_name;
1159151497Sru  eval "${return_ok}";
1160151497Sru}
1161151497Sru
1162151497Sru
1163151497Sru########################################################################
1164151497Sru# cat_z (<file>)
1165151497Sru#
1166151497Sru# Decompress if possible or just print <file> to standard output.
1167151497Sru#
1168151497Sru# gzip, bzip2, and .Z decompression is supported.
1169151497Sru#
1170151497Sru# Arguments: 1, a file name.
1171151497Sru# Output: the content of <file>, possibly decompressed.
1172151497Sru#
1173151497Sruif test _"${_HAS_COMPRESSION}"_ = _yes_
1174151497Sruthen
1175151497Sru  cat_z()
1176151497Sru  {
1177151497Sru    func_check cat_z = 1 "$@";
1178151497Sru    case "$1" in
1179151497Sru      '')
1180151497Sru        error 'cat_z(): empty file name';
1181151497Sru        ;;
1182151497Sru      '-')
1183151497Sru        error 'cat_z(): for standard input use save_stdin()';
1184151497Sru        ;;
1185151497Sru    esac;
1186151497Sru    if obj _HAS_BZIP is_yes
1187151497Sru    then
1188151497Sru      if bzip2 -t "$1" 2>${_NULL_DEV}
1189151497Sru      then
1190151497Sru        bzip2 -c -d "$1" 2>${_NULL_DEV};
1191151497Sru        eval "${return_ok}";
1192151497Sru      fi;
1193151497Sru    fi;
1194151497Sru    gzip -c -d -f "$1" 2>${_NULL_DEV};
1195151497Sru    eval "${return_ok}";
1196151497Sru  }
1197151497Sruelse
1198151497Sru  cat_z()
1199151497Sru  {
1200151497Sru    func_check cat_z = 1 "$@";
1201151497Sru    cat "$1";
1202151497Sru    eval "${return_ok}";
1203151497Sru  }
1204151497Srufi;
1205151497Sru
1206151497Sru
1207151497Sru########################################################################
1208151497Sru# clean_up ()
1209151497Sru#
1210151497Sru# Do the final cleaning up before exiting; used by the trap calls.
1211151497Sru#
1212151497Sru# defined above
1213151497Sru
1214151497Sru
1215151497Sru########################################################################
1216151497Sru# diag (<text>*)
1217151497Sru#
1218151497Sru# Print marked message to standard error; useful for debugging.
1219151497Sru#
1220151497Sru# defined above
1221151497Sru
1222151497Sru
1223151497Sru########################################################################
1224151497Srulandmark '4: dirname()*';
1225151497Sru########################################################################
1226151497Sru
1227151497Sru#######################################################################
1228151497Sru# dirname_append (<dir> <name>)
1229151497Sru#
1230151497Sru# Append `name' to `dir' with clean handling of `/'.
1231151497Sru#
1232151497Sru# Arguments : 2
1233151497Sru# Output    : the generated new directory name <dir>/<name>
1234151497Sru#
1235151497Srudirname_append()
1236151497Sru{
1237151497Sru  func_check dirname_append = 2 "$@";
1238151497Sru  if is_empty "$1"
1239151497Sru  then
1240151497Sru    error "dir_append(): first argument is empty.";
1241151497Sru  fi;
1242151497Sru  if is_empty "$2"
1243151497Sru  then
1244151497Sru    echo1 "$1";
1245151497Sru  else
1246151497Sru    dirname_chop "$1"/"$2";
1247151497Sru  fi;
1248151497Sru  eval "${return_ok}";
1249151497Sru}
1250151497Sru
1251151497Sru
1252151497Sru########################################################################
1253151497Sru# dirname_chop (<name>)
1254151497Sru#
1255151497Sru# Remove unnecessary slashes from directory name.
1256151497Sru#
1257151497Sru# Argument: 1, a directory name.
1258151497Sru# Output:   path without double, or trailing slashes.
1259151497Sru#
1260151497Sru# Variable prefix: dc
1261151497Sru#
1262151497Srudirname_chop()
1263151497Sru{
1264151497Sru  func_check dirname_chop = 1 "$@";
1265151497Sru  # replace all multiple slashes by a single slash `/'.
1266151497Sru  dc_res="$(echo1 "$1" | sed -e 's|///*|/|g')";
1267151497Sru  exit_test;
1268151497Sru  case "${dc_res}" in
1269151497Sru  ?*/)
1270151497Sru    # remove trailing slash '/';
1271151497Sru    echo1 "${dc_res}" | sed -e 's|/$||';
1272151497Sru    ;;
1273151497Sru  *)
1274151497Sru    obj dc_res echo1
1275151497Sru    ;;
1276151497Sru  esac;
1277151497Sru  eval ${_UNSET} dc_res;
1278151497Sru  eval "${return_ok}";
1279151497Sru}
1280151497Sru
1281151497Sru
1282151497Sru########################################################################
1283151497Sru# do_filearg (<filearg>)
1284151497Sru#
1285151497Sru# Append the file, man-page, or standard input corresponding to the
1286151497Sru# argument to the temporary file.  If this is compressed in the gzip
1287151497Sru# or Z format it is decompressed.  A title element is generated.
1288151497Sru#
1289151497Sru# Argument either:
1290151497Sru#   - name of an existing file.
1291151497Sru#   - `-' to represent standard input (several times allowed).
1292151497Sru#   - `man:name.(section)' the man-page for `name' in `section'.
1293151497Sru#   - `man:name.section' the man-page for `name' in `section'.
1294151497Sru#   - `man:name' the man-page for `name' in the lowest `section'.
1295151497Sru#   - `name.section' the man-page for `name' in `section'.
1296151497Sru#   - `name' the man-page for `name' in the lowest `section'.
1297151497Sru# Globals :
1298151497Sru#   $_TMP_STDIN, $_MAN_ENABLE, $_MAN_IS_SETUP, $_OPT_MAN
1299151497Sru#
1300151497Sru# Output  : none
1301151497Sru# Return  : $_GOOD if found, ${_BAD} otherwise.
1302151497Sru#
1303151497Sru# Variable prefix: df
1304151497Sru#
1305151497Srudo_filearg()
1306151497Sru{
1307151497Sru  func_check do_filearg = 1 "$@";
1308151497Sru  df_filespec="$1";
1309151497Sru  # store sequence into positional parameters
1310151497Sru  case "${df_filespec}" in
1311151497Sru  '')
1312151497Sru    eval ${_UNSET} df_filespec;
1313151497Sru    eval "${return_good}";
1314151497Sru    ;;
1315151497Sru  '-')
1316151497Sru    register_file '-';
1317151497Sru    eval ${_UNSET} df_filespec;
1318151497Sru    eval "${return_good}";
1319151497Sru    ;;
1320151497Sru  */*)			       # with directory part; so no man search
1321151497Sru    set 'File';
1322151497Sru    ;;
1323151497Sru  *)
1324151497Sru    if obj _MAN_ENABLE is_yes
1325151497Sru    then
1326151497Sru      if obj _MAN_FORCE is_yes
1327151497Sru      then
1328151497Sru        set 'Manpage' 'File';
1329151497Sru      else
1330151497Sru        set 'File' 'Manpage';
1331151497Sru      fi;
1332151497Sru      else
1333151497Sru      set 'File';
1334151497Sru    fi;
1335151497Sru    ;;
1336151497Sru  esac;
1337151497Sru  for i
1338151497Sru  do
1339151497Sru    case "$i" in
1340151497Sru    File)
1341151497Sru      if test -f "${df_filespec}"
1342151497Sru      then
1343151497Sru        if test -r "${df_filespec}"
1344151497Sru        then
1345151497Sru          register_file "${df_filespec}";
1346151497Sru          eval ${_UNSET} df_filespec;
1347151497Sru          eval ${_UNSET} df_no_man;
1348151497Sru          eval "${return_good}";
1349151497Sru        else
1350151497Sru          echo2 "could not read \`${df_filespec}'";
1351151497Sru          eval ${_UNSET} df_filespec;
1352151497Sru          eval ${_UNSET} df_no_man;
1353151497Sru          eval "${return_bad}";
1354151497Sru        fi;
1355151497Sru      else
1356151497Sru        if obj df_no_man is_not_empty
1357151497Sru        then
1358151497Sru          if obj _OPT_WHATIS is_yes
1359151497Sru          then
1360151497Sru            to_tmp_line "This is neither a file nor a man page."
1361151497Sru          else
1362151497Sru            echo2 "\`${df_filespec}' is neither a file nor a man page."
1363151497Sru          fi;
1364151497Sru        fi;
1365151497Sru        df_no_file=yes;
1366151497Sru        continue;
1367151497Sru      fi;
1368151497Sru      ;;
1369151497Sru    Manpage)			# parse filespec as man page
1370151497Sru      if obj _MAN_IS_SETUP is_not_yes
1371151497Sru      then
1372151497Sru        man_setup;
1373151497Sru      fi;
1374151497Sru      if man_do_filespec "${df_filespec}"
1375151497Sru      then
1376151497Sru        eval ${_UNSET} df_filespec;
1377151497Sru        eval ${_UNSET} df_no_file;
1378151497Sru        eval "${return_good}";
1379151497Sru      else
1380151497Sru        if obj df_no_file is_not_empty
1381151497Sru        then
1382151497Sru          if obj _OPT_WHATIS is_yes
1383151497Sru          then
1384151497Sru            to_tmp_line "This is neither a file nor a man page."
1385151497Sru          else
1386151497Sru            echo2 "\`${df_filespec}' is neither a file nor a man page."
1387151497Sru          fi;
1388151497Sru        fi;
1389151497Sru        df_no_man=yes;
1390151497Sru        continue;
1391151497Sru      fi;
1392151497Sru      ;;
1393151497Sru    esac;
1394151497Sru  done;
1395151497Sru  eval ${_UNSET} df_filespec;
1396151497Sru  eval ${_UNSET} df_no_file;
1397151497Sru  eval ${_UNSET} df_no_man;
1398151497Sru  eval "${return_bad}";
1399151497Sru} # do_filearg()
1400151497Sru
1401151497Sru
1402151497Sru########################################################################
1403151497Sru# do_nothing ()
1404151497Sru#
1405151497Sru# Dummy function.
1406151497Sru#
1407151497Srudo_nothing()
1408151497Sru{
1409151497Sru  eval return "${_OK}";
1410151497Sru}
1411151497Sru
1412151497Sru
1413151497Sru########################################################################
1414151497Sru# echo2 (<text>*)
1415151497Sru#
1416151497Sru# Print to standard error with final line break.
1417151497Sru#
1418151497Sru# defined above
1419151497Sru
1420151497Sru
1421151497Sru########################################################################
1422151497Sru# error (<text>*)
1423151497Sru#
1424151497Sru# Print error message and exit with error code.
1425151497Sru#
1426151497Sru# defined above
1427151497Sru
1428151497Sru
1429151497Sru########################################################################
1430151497Sru# exit_test ()
1431151497Sru#
1432151497Sru# Test whether the former command ended with error().  Exit again.
1433151497Sru#
1434151497Sru# defined above
1435151497Sru
1436151497Sru
1437151497Sru########################################################################
1438151497Sru# func_check (<func_name> <rel_op> <nr_args> "$@")
1439151497Sru#
1440151497Sru# Check number of arguments and register to _FUNC_STACK.
1441151497Sru#
1442151497Sru# Arguments: >=3
1443151497Sru#   <func_name>: name of the calling function.
1444151497Sru#   <rel_op>:    a relational operator: = != < > <= >=
1445151497Sru#   <nr_args>:   number of arguments to be checked against <operator>
1446151497Sru#   "$@":        the arguments of the calling function.
1447151497Sru#
1448151497Sru# defined above
1449151497Sru
1450151497Sru#########################################################################
1451151497Sru# func_pop ()
1452151497Sru#
1453151497Sru# Delete the top element from the function call stack.
1454151497Sru#
1455151497Sru# defined above
1456151497Sru
1457151497Sru
1458151497Sru########################################################################
1459151497Sru# func_push (<element>)
1460151497Sru#
1461151497Sru# Store another element to function call stack.
1462151497Sru#
1463151497Sru# defined above
1464151497Sru
1465151497Sru
1466151497Sru########################################################################
1467151497Sru# func_stack_dump ()
1468151497Sru#
1469151497Sru# Print the content of the stack.
1470151497Sru#
1471151497Sru# defined above
1472151497Sru
1473151497Sru
1474151497Sru########################################################################
1475151497Sru# get_first_essential (<arg>*)
1476151497Sru#
1477151497Sru# Retrieve first non-empty argument.
1478151497Sru#
1479151497Sru# Return  : `1' if all arguments are empty, `0' if found.
1480151497Sru# Output  : the retrieved non-empty argument.
1481151497Sru#
1482151497Sru# Variable prefix: gfe
1483151497Sru#
1484151497Sruget_first_essential()
1485151497Sru{
1486151497Sru  func_check get_first_essential '>=' 0 "$@";
1487151497Sru  if is_equal "$#" 0
1488151497Sru  then
1489151497Sru    eval "${return_ok}";
1490151497Sru  fi;
1491151497Sru  for i
1492151497Sru  do
1493151497Sru    gfe_var="$i";
1494151497Sru    if obj gfe_var is_not_empty
1495151497Sru    then
1496151497Sru      obj gfe_var echo1;
1497151497Sru      eval ${_UNSET} gfe_var;
1498151497Sru      eval "${return_ok}";
1499151497Sru    fi;
1500151497Sru  done;
1501151497Sru  eval ${_UNSET} gfe_var;
1502151497Sru  eval "${return_bad}";
1503151497Sru}
1504151497Sru
1505151497Sru
1506151497Sru########################################################################
1507151497Srulandmark '5: is_*()';
1508151497Sru########################################################################
1509151497Sru
1510151497Sru########################################################################
1511151497Sru# is_dir (<name>)
1512151497Sru#
1513151497Sru# Test whether `name' is a directory.
1514151497Sru#
1515151497Sru# Arguments : 1
1516151497Sru# Return    : `0' if arg1 is a directory, `1' otherwise.
1517151497Sru#
1518151497Sruis_dir()
1519151497Sru{
1520151497Sru  func_check is_dir '=' 1 "$@";
1521151497Sru  if test _"$1"_ != __ && test -d "$1" && test -r "$1"
1522151497Sru  then
1523151497Sru    eval "${return_yes}";
1524151497Sru  fi;
1525151497Sru  eval "${return_no}";
1526151497Sru}
1527151497Sru
1528151497Sru
1529151497Sru########################################################################
1530151497Sru# is_empty (<string>)
1531151497Sru#
1532151497Sru# Test whether `string' is empty.
1533151497Sru#
1534151497Sru# Arguments : <=1
1535151497Sru# Return    : `0' if arg1 is empty or does not exist, `1' otherwise.
1536151497Sru#
1537151497Sruis_empty()
1538151497Sru{
1539151497Sru  func_check is_empty '=' 1 "$@";
1540151497Sru  if test _"$1"_ = __
1541151497Sru  then
1542151497Sru    eval "${return_yes}";
1543151497Sru  fi;
1544151497Sru  eval "${return_no}";
1545151497Sru}
1546151497Sru
1547151497Sru
1548151497Sru########################################################################
1549151497Sru# is_equal (<string1> <string2>)
1550151497Sru#
1551151497Sru# Test whether `string1' is equal to <string2>.
1552151497Sru#
1553151497Sru# Arguments : 2
1554151497Sru# Return    : `0' both arguments are equal strings, `1' otherwise.
1555151497Sru#
1556151497Sruis_equal()
1557151497Sru{
1558151497Sru  func_check is_equal '=' 2 "$@";
1559151497Sru  if test _"$1"_ = _"$2"_
1560151497Sru  then
1561151497Sru    eval "${return_yes}";
1562151497Sru  fi;
1563151497Sru  eval "${return_no}";
1564151497Sru}
1565151497Sru
1566151497Sru
1567151497Sru########################################################################
1568151497Sru# is_existing (<name>)
1569151497Sru#
1570151497Sru# Test whether `name' is an existing file or directory.  Solaris 2.5 does
1571151497Sru# not have `test -e'.
1572151497Sru#
1573151497Sru# Arguments : 1
1574151497Sru# Return    : `0' if arg1 exists, `1' otherwise.
1575151497Sru#
1576151497Sruis_existing()
1577151497Sru{
1578151497Sru  func_check is_existing '=' 1 "$@";
1579151497Sru  if test _"$1"_ = __
1580151497Sru  then
1581151497Sru    eval "${return_no}";
1582151497Sru  fi;
1583151497Sru  if test -f "$1" || test -d "$1" || test -c "$1"
1584151497Sru  then
1585151497Sru    eval "${return_yes}";
1586151497Sru  fi;
1587151497Sru  eval "${return_no}";
1588151497Sru}
1589151497Sru
1590151497Sru
1591151497Sru########################################################################
1592151497Sru# is_file (<name>)
1593151497Sru#
1594151497Sru# Test whether `name' is a readable file.
1595151497Sru#
1596151497Sru# Arguments : 1
1597151497Sru# Return    : `0' if arg1 is a readable file, `1' otherwise.
1598151497Sru#
1599151497Sruis_file()
1600151497Sru{
1601151497Sru  func_check is_file '=' 1 "$@";
1602151497Sru  if is_not_empty "$1" && test -f "$1" && test -r "$1"
1603151497Sru  then
1604151497Sru    eval "${return_yes}";
1605151497Sru  fi;
1606151497Sru  eval "${return_no}";
1607151497Sru}
1608151497Sru
1609151497Sru
1610151497Sru########################################################################
1611151497Sru# is_non_empty_file (<file_name>)
1612151497Sru#
1613151497Sru# Test whether `file_name' is a non-empty existing file.
1614151497Sru#
1615151497Sru# Arguments : <=1
1616151497Sru# Return    :
1617151497Sru#   `0' if arg1 is a non-empty existing file
1618151497Sru#   `1' otherwise
1619151497Sru#
1620151497Sruis_non_empty_file()
1621151497Sru{
1622151497Sru  func_check is_non_empty_file '=' 1 "$@";
1623151497Sru  if is_file "$1" && test -s "$1"
1624151497Sru  then
1625151497Sru    eval "${return_yes}";
1626151497Sru  fi;
1627151497Sru  eval "${return_no}";
1628151497Sru}
1629151497Sru
1630151497Sru
1631151497Sru########################################################################
1632151497Sru# is_not_dir (<name>)
1633151497Sru#
1634151497Sru# Test whether `name' is not a readable directory.
1635151497Sru#
1636151497Sru# Arguments : 1
1637151497Sru# Return    : `0' if arg1 is a directory, `1' otherwise.
1638151497Sru#
1639151497Sruis_not_dir()
1640151497Sru{
1641151497Sru  func_check is_not_dir '=' 1 "$@";
1642151497Sru  if is_dir "$1"
1643151497Sru  then
1644151497Sru    eval "${return_no}";
1645151497Sru  fi;
1646151497Sru  eval "${return_yes}";
1647151497Sru}
1648151497Sru
1649151497Sru
1650151497Sru########################################################################
1651151497Sru# is_not_empty (<string>)
1652151497Sru#
1653151497Sru# Test whether `string' is not empty.
1654151497Sru#
1655151497Sru# Arguments : <=1
1656151497Sru# Return    : `0' if arg1 exists and is not empty, `1' otherwise.
1657151497Sru#
1658151497Sruis_not_empty()
1659151497Sru{
1660151497Sru  func_check is_not_empty '=' 1 "$@";
1661151497Sru  if is_empty "$1"
1662151497Sru  then
1663151497Sru    eval "${return_no}";
1664151497Sru  fi;
1665151497Sru  eval "${return_yes}";
1666151497Sru}
1667151497Sru
1668151497Sru
1669151497Sru########################################################################
1670151497Sru# is_not_equal (<string1> <string2>)
1671151497Sru#
1672151497Sru# Test whether `string1' differs from `string2'.
1673151497Sru#
1674151497Sru# Arguments : 2
1675151497Sru#
1676151497Sruis_not_equal()
1677151497Sru{
1678151497Sru  func_check is_not_equal '=' 2 "$@";
1679151497Sru  if is_equal "$1" "$2"
1680151497Sru  then
1681151497Sru    eval "${return_no}";
1682151497Sru  fi
1683151497Sru  eval "${return_yes}";
1684151497Sru}
1685151497Sru
1686151497Sru
1687151497Sru########################################################################
1688151497Sru# is_not_file (<filename>)
1689151497Sru#
1690151497Sru# Test whether `name' is a not readable file.
1691151497Sru#
1692151497Sru# Arguments : 1 (empty allowed)
1693151497Sru#
1694151497Sruis_not_file()
1695151497Sru{
1696151497Sru  func_check is_not_file '=' 1 "$@";
1697151497Sru  if is_file "$1"
1698151497Sru  then
1699151497Sru    eval "${return_no}";
1700151497Sru  fi;
1701151497Sru  eval "${return_yes}";
1702151497Sru}
1703151497Sru
1704151497Sru
1705151497Sru########################################################################
1706151497Sru# is_not_prog ([<name> [<arg>*]])
1707151497Sru#
1708151497Sru# Verify that arg is a not program in $PATH.
1709151497Sru#
1710151497Sru# Arguments : >=0 (empty allowed)
1711151497Sru#   more args are ignored, this allows to specify progs with arguments
1712151497Sru#
1713151497Sruis_not_prog()
1714151497Sru{
1715151497Sru  func_check is_not_prog '>=' 0 "$@";
1716151497Sru  case "$#" in
1717151497Sru  0)
1718151497Sru    eval "${return_yes}";
1719151497Sru    ;;
1720151497Sru  *)
1721151497Sru    if where_is "$1" >${_NULL_DEV}
1722151497Sru    then
1723151497Sru      eval "${return_no}";
1724151497Sru    fi;
1725151497Sru    ;;
1726151497Sru  esac
1727151497Sru  eval "${return_yes}";
1728151497Sru}
1729151497Sru
1730151497Sru
1731151497Sru########################################################################
1732151497Sru# is_not_writable (<name>)
1733151497Sru#
1734151497Sru# Test whether `name' is a not a writable file or directory.
1735151497Sru#
1736151497Sru# Arguments : >=1 (empty allowed), more args are ignored
1737151497Sru#
1738151497Sruis_not_writable()
1739151497Sru{
1740151497Sru  func_check is_not_writable '>=' 1 "$@";
1741151497Sru  if is_writable "$1"
1742151497Sru  then
1743151497Sru    eval "${return_no}";
1744151497Sru  fi;
1745151497Sru  eval "${return_yes}";
1746151497Sru}
1747151497Sru
1748151497Sru
1749151497Sru########################################################################
1750151497Sru# is_not_X ()
1751151497Sru#
1752151497Sru# Test whether not running in X Window by checking $DISPLAY
1753151497Sru#
1754151497Sruis_not_X()
1755151497Sru{
1756151497Sru  func_check is_X '=' 0 "$@";
1757151497Sru  if obj DISPLAY is_empty
1758151497Sru  then
1759151497Sru    eval "${return_yes}";
1760151497Sru  fi;
1761151497Sru  eval "${return_no}";
1762151497Sru}
1763151497Sru
1764151497Sru
1765151497Sru########################################################################
1766151497Sru# is_not_yes (<string>)
1767151497Sru#
1768151497Sru# Test whether `string' is not "yes".
1769151497Sru#
1770151497Sru# Arguments : 1
1771151497Sru#
1772151497Sruis_not_yes()
1773151497Sru{
1774151497Sru  func_check is_not_yes = 1 "$@";
1775151497Sru  if is_yes "$1"
1776151497Sru  then
1777151497Sru    eval "${return_no}";
1778151497Sru  fi;
1779151497Sru  eval "${return_yes}";
1780151497Sru}
1781151497Sru
1782151497Sru
1783151497Sru########################################################################
1784151497Sru# is_prog ([<name> [<arg>*]])
1785151497Sru#
1786151497Sru# Determine whether <name> is a program in $PATH
1787151497Sru#
1788151497Sru# Arguments : >=0 (empty allowed)
1789151497Sru#   <arg>* are ignored, this allows to specify progs with arguments.
1790151497Sru#
1791151497Sruis_prog()
1792151497Sru{
1793151497Sru  func_check is_prog '>=' 0 "$@";
1794151497Sru  case "$#" in
1795151497Sru  0)
1796151497Sru    eval "${return_no}";
1797151497Sru    ;;
1798151497Sru  *)
1799151497Sru    if where_is "$1" >${_NULL_DEV}
1800151497Sru    then
1801151497Sru      eval "${return_yes}";
1802151497Sru    fi;
1803151497Sru    ;;
1804151497Sru  esac
1805151497Sru  eval "${return_no}";
1806151497Sru}
1807151497Sru
1808151497Sru
1809151497Sru########################################################################
1810151497Sru# is_writable (<name>)
1811151497Sru#
1812151497Sru# Test whether `name' is a writable file or directory.
1813151497Sru#
1814151497Sru# Arguments : >=1 (empty allowed), more args are ignored
1815151497Sru#
1816151497Sruis_writable()
1817151497Sru{
1818151497Sru  func_check is_writable '>=' 1 "$@";
1819151497Sru  if test _"$1"_ = __
1820151497Sru  then
1821151497Sru    eval "${return_no}";
1822151497Sru  fi;
1823151497Sru  if test -r "$1"
1824151497Sru  then
1825151497Sru    if test -w "$1"
1826151497Sru    then
1827151497Sru      eval "${return_yes}";
1828151497Sru    fi;
1829151497Sru  fi;
1830151497Sru  eval "${return_no}";
1831151497Sru}
1832151497Sru
1833151497Sru
1834151497Sru########################################################################
1835151497Sru# is_X ()
1836151497Sru#
1837151497Sru# Test whether running in X Window by checking $DISPLAY
1838151497Sru#
1839151497Sruis_X()
1840151497Sru{
1841151497Sru  func_check is_X '=' 0 "$@";
1842151497Sru  if obj DISPLAY is_not_empty
1843151497Sru  then
1844151497Sru    eval "${return_yes}";
1845151497Sru  fi;
1846151497Sru  eval "${return_no}";
1847151497Sru}
1848151497Sru
1849151497Sru
1850151497Sru########################################################################
1851151497Sru# is_yes (<string>)
1852151497Sru#
1853151497Sru# Test whether `string' has value "yes".
1854151497Sru#
1855151497Sru# Return    : `0' if arg1 is `yes', `1' otherwise.
1856151497Sru#
1857151497Sruis_yes()
1858151497Sru{
1859151497Sru  func_check is_yes '=' 1 "$@";
1860151497Sru  if is_equal "$1" 'yes'
1861151497Sru  then
1862151497Sru    eval "${return_yes}";
1863151497Sru  fi;
1864151497Sru  eval "${return_no}";
1865151497Sru}
1866151497Sru
1867151497Sru
1868151497Sru########################################################################
1869151497Sru# landmark ()
1870151497Sru#
1871151497Sru# Print debugging information on standard error if $_DEBUG_LM is `yes'.
1872151497Sru#
1873151497Sru# Globals: $_DEBUG_LM
1874151497Sru#
1875151497Sru# Defined in section `Debugging functions'.
1876151497Sru
1877151497Sru
1878151497Sru########################################################################
1879151497Sru# leave ([<code>])
1880151497Sru#
1881151497Sru# Clean exit without an error or with <code>.
1882151497Sru#
1883151497Sruleave()
1884151497Sru{
1885151497Sru  clean_up;
1886151497Sru  if test $# = 0
1887151497Sru  then
1888151497Sru    exit "${_OK}";
1889151497Sru  else
1890151497Sru    exit "$1";
1891151497Sru  fi;
1892151497Sru}
1893151497Sru
1894151497Sru
1895151497Sru########################################################################
1896151497Srulandmark '6: list_*()';
1897151497Sru########################################################################
1898151497Sru#
1899151497Sru# `list' is an object class that represents an array or list.  Its
1900151497Sru# data consists of space-separated single-quoted elements.  So a list
1901151497Sru# has the form "'first' 'second' '...' 'last'".  See list_append() for
1902151497Sru# more details on the list structure.  The array elements of `list'
1903151497Sru# can be get by `eval set x "$list"; shift`.
1904151497Sru
1905151497Sru
1906151497Sru########################################################################
1907151497Sru# list_append (<list> <element>...)
1908151497Sru#
1909151497Sru# Arguments: >=2
1910151497Sru#   <list>: a variable name for a list of single-quoted elements
1911151497Sru#   <element>:  some sequence of characters.
1912151497Sru# Output: none, but $<list> is set to
1913151497Sru#   if <list> is empty:  "'<element>' '...'"
1914151497Sru#   otherwise:           "$list '<element>' ..."
1915151497Sru#
1916151497Sru# Variable prefix: la
1917151497Sru#
1918151497Srulist_append()
1919151497Sru{
1920151497Sru  func_check list_append '>=' 2 "$@";
1921151497Sru  la_name="$1";
1922151497Sru  eval la_list='"${'$1'}"';
1923151497Sru  shift;
1924151497Sru  for s
1925151497Sru  do
1926151497Sru    la_s="$s";
1927151497Sru    case "${la_s}" in
1928151497Sru    *\'*)
1929151497Sru      # escape each single quote by replacing each
1930151497Sru      # "'" (squote) by "'\''" (squote bslash squote squote);
1931151497Sru      # note that the backslash must be doubled in the following `sed'
1932151497Sru      la_element="$(echo1 "${la_s}" | sed -e 's/'"${_SQ}"'/&\\&&/g')";
1933151497Sru      exit_test;
1934151497Sru      ;;
1935151497Sru    '')
1936151497Sru      la_element="";
1937151497Sru      ;;
1938151497Sru    *)
1939151497Sru      la_element="${la_s}";
1940151497Sru      ;;
1941151497Sru    esac;
1942151497Sru    if obj la_list is_empty
1943151497Sru    then
1944151497Sru      la_list="'${la_element}'";
1945151497Sru    else
1946151497Sru      la_list="${la_list} '${la_element}'";
1947151497Sru    fi;
1948151497Sru  done;
1949151497Sru  eval "${la_name}"='"${la_list}"';
1950151497Sru  eval ${_UNSET} la_element;
1951151497Sru  eval ${_UNSET} la_list;
1952151497Sru  eval ${_UNSET} la_name;
1953151497Sru  eval ${_UNSET} la_s;
1954151497Sru  eval "${return_ok}";
1955151497Sru}
1956151497Sru
1957151497Sru
1958151497Sru########################################################################
1959151497Sru# list_from_cmdline (<pre_name_of_opt_lists> [<cmdline_arg>...])
1960151497Sru#
1961151497Sru# Transform command line arguments into a normalized form.
1962151497Sru#
1963151497Sru# Options, option arguments, and file parameters are identified and
1964151497Sru# output each as a single-quoted argument of its own.  Options and
1965151497Sru# file parameters are separated by a '--' argument.
1966151497Sru#
1967151497Sru# Arguments: >=1
1968151497Sru#   <pre_name>: common part of a set of 4 environment variable names:
1969151497Sru#     $<pre_name>_SHORT_NA:  list of short options without an arg.
1970151497Sru#     $<pre_name>_SHORT_ARG: list of short options that have an arg.
1971151497Sru#     $<pre_name>_LONG_NA:   list of long options without an arg.
1972151497Sru#     $<pre_name>_LONG_ARG:  list of long options that have an arg.
1973151497Sru#   <cmdline_arg>...: the arguments from a command line, such as "$@",
1974151497Sru#                     the content of a variable, or direct arguments.
1975151497Sru#
1976151497Sru# Output: ['-[-]opt' ['optarg']]... '--' ['filename']...
1977151497Sru#
1978151497Sru# Example:
1979151497Sru#   list_from_cmdline PRE 'a b' 'c' '' 'long' -a f1 -bcarg --long=larg f2
1980151497Sru# If $PRE_SHORT_NA, $PRE_SHORT_ARG, $PRE_LONG_NA, and $PRE_LONG_ARG are
1981151497Sru# none-empty option lists, this will result in printing:
1982151497Sru#     '-a' '-b' '-c' 'arg' '--long' 'larg' '--' 'f1' 'f2'
1983151497Sru#
1984151497Sru#   Use this function in the following way:
1985151497Sru#     eval set x "$(args_norm PRE_NAME "$@")";
1986151497Sru#     shift;
1987151497Sru#     while test "$1" != '--'; do
1988151497Sru#       case "$1" in
1989151497Sru#       ...
1990151497Sru#       esac;
1991151497Sru#       shift;
1992151497Sru#     done;
1993151497Sru#     shift;         #skip '--'
1994151497Sru#     # all positional parameters ("$@") left are file name parameters.
1995151497Sru#
1996151497Sru# Variable prefix: lfc
1997151497Sru#
1998151497Srulist_from_cmdline()
1999151497Sru{
2000151497Sru  func_check list_from_cmdline '>=' 1 "$@";
2001151497Sru  lfc_short_n="$(obj_data "$1"_SHORT_NA)";  # short options, no argument
2002151497Sru  lfc_short_a="$(obj_data "$1"_SHORT_ARG)"; # short options, with argument
2003151497Sru  lfc_long_n="$(obj_data "$1"_LONG_NA)";    # long options, no argument
2004151497Sru  lfc_long_a="$(obj_data "$1"_LONG_ARG)";   # long options, with argument
2005151497Sru  exit_test;
2006151497Sru  if obj lfc_short_n is_empty
2007151497Sru  then
2008151497Sru    error 'list_from_cmdline(): no $'"$1"'_SHORT_NA options.';
2009151497Sru  fi;
2010151497Sru  if obj lfc_short_a is_empty
2011151497Sru  then
2012151497Sru    error 'list_from_cmdline(): no $'"$1"'_SHORT_ARG options.';
2013151497Sru  fi;
2014151497Sru  if obj lfc_long_n is_empty
2015151497Sru  then
2016151497Sru    error 'list_from_cmdline(): no $'"$1"'_LONG_NA options.';
2017151497Sru  fi;
2018151497Sru  if obj lfc_long_a is_empty
2019151497Sru  then
2020151497Sru    error 'list_from_cmdline(): no $'"$1"'_LONG_ARG options.';
2021151497Sru  fi;
2022151497Sru
2023151497Sru  shift;
2024151497Sru  if is_equal "$#" 0
2025151497Sru  then
2026151497Sru    echo1 --
2027151497Sru    eval ${_UNSET} lfc_fparams;
2028151497Sru    eval ${_UNSET} lfc_short_a;
2029151497Sru    eval ${_UNSET} lfc_short_n;
2030151497Sru    eval ${_UNSET} lfc_long_a;
2031151497Sru    eval ${_UNSET} lfc_long_n;
2032151497Sru    eval ${_UNSET} lfc_result;
2033151497Sru    eval "${return_ok}";
2034151497Sru  fi;
2035151497Sru
2036151497Sru  lfc_fparams='';
2037151497Sru  lfc_result='';
2038151497Sru  while test "$#" -ge 1
2039151497Sru  do
2040151497Sru    lfc_arg="$1";
2041151497Sru    shift;
2042151497Sru    case "${lfc_arg}" in
2043151497Sru    --) break; ;;
2044151497Sru    --*=*)
2045151497Sru      # delete leading '--';
2046151497Sru      lfc_abbrev="$(echo1 "${lfc_arg}" | sed -e 's/^--//')";
2047151497Sru      lfc_with_equal="${lfc_abbrev}";
2048151497Sru      # extract option by deleting from the first '=' to the end
2049151497Sru      lfc_abbrev="$(echo1 "${lfc_with_equal}" | \
2050151497Sru                    sed -e 's/^\([^=]*\)=.*$/\1/')";
2051151497Sru      lfc_opt="$(list_single_from_abbrev lfc_long_a "${lfc_abbrev}")";
2052151497Sru      exit_test;
2053151497Sru      if obj lfc_opt is_empty
2054151497Sru      then
2055151497Sru        error_user "--${lfc_abbrev} is not an option.";
2056151497Sru      else
2057151497Sru        # get the option argument by deleting up to first `='
2058151497Sru        lfc_optarg="$(echo1 "${lfc_with_equal}" | sed -e 's/^[^=]*=//')";
2059151497Sru        exit_test;
2060151497Sru        list_append lfc_result "--${lfc_opt}" "${lfc_optarg}";
2061151497Sru        continue;
2062151497Sru      fi;
2063151497Sru      ;;
2064151497Sru    --*)
2065151497Sru      # delete leading '--';
2066151497Sru      lfc_abbrev="$(echo1 "${lfc_arg}" | sed -e 's/^--//')";
2067151497Sru      if list_has lfc_long_n "${lfc_abbrev}"
2068151497Sru      then
2069151497Sru        lfc_opt="${lfc_abbrev}";
2070151497Sru      else
2071151497Sru        exit_test;
2072151497Sru        lfc_opt="$(list_single_from_abbrev lfc_long_n "${lfc_abbrev}")";
2073151497Sru        exit_test;
2074151497Sru        if obj lfc_opt is_not_empty && is_not_equal "$#" 0
2075151497Sru        then
2076151497Sru          a="$(list_single_from_abbrev lfc_long_a "${lfc_abbrev}")";
2077151497Sru          exit_test;
2078151497Sru          if obj a is_not_empty
2079151497Sru          then
2080151497Sru            error_user "The abbreviation ${lfc_arg} \
2081151497Sruhas multiple options: --${lfc_opt} and --${a}.";
2082151497Sru          fi;
2083151497Sru        fi;
2084151497Sru      fi;
2085151497Sru      if obj lfc_opt is_not_empty
2086151497Sru      then
2087151497Sru        # long option, no argument
2088151497Sru        list_append lfc_result "--${lfc_opt}";
2089151497Sru        continue;
2090151497Sru      fi;
2091151497Sru      lfc_opt="$(list_single_from_abbrev lfc_long_a "${lfc_abbrev}")";
2092151497Sru      exit_test;
2093151497Sru      if obj lfc_opt is_not_empty
2094151497Sru      then
2095151497Sru        # long option with argument
2096151497Sru        if test "$#" -le 0
2097151497Sru        then
2098151497Sru          error_user "no argument for option --${lfc_opt}."
2099151497Sru        fi;
2100151497Sru        list_append lfc_result "--${lfc_opt}" "$1";
2101151497Sru        shift;
2102151497Sru        continue;
2103151497Sru      fi;
2104151497Sru      error_user "${lfc_arg} is not an option.";
2105151497Sru      ;;
2106151497Sru    -?*)			# short option (cluster)
2107151497Sru      # delete leading `-';
2108151497Sru      lfc_rest="$(echo1 "${lfc_arg}" | sed -e 's/^-//')";
2109151497Sru      exit_test;
2110151497Sru      while obj lfc_rest is_not_empty
2111151497Sru      do
2112151497Sru        # get next short option from cluster (first char of $lfc_rest)
2113151497Sru        lfc_optchar="$(echo1 "${lfc_rest}" | sed -e 's/^\(.\).*$/\1/')";
2114151497Sru        # remove first character from ${lfc_rest};
2115151497Sru        lfc_rest="$(echo1 "${lfc_rest}" | sed -e 's/^.//')";
2116151497Sru        exit_test;
2117151497Sru        if list_has lfc_short_n "${lfc_optchar}"
2118151497Sru        then
2119151497Sru          list_append lfc_result "-${lfc_optchar}";
2120151497Sru          continue;
2121151497Sru        elif list_has lfc_short_a "${lfc_optchar}"
2122151497Sru        then
2123151497Sru          if obj lfc_rest is_empty
2124151497Sru          then
2125151497Sru            if test "$#" -ge 1
2126151497Sru            then
2127151497Sru              list_append lfc_result "-${lfc_optchar}" "$1";
2128151497Sru              shift;
2129151497Sru              continue;
2130151497Sru            else
2131151497Sru              error_user "no argument for option -${lfc_optchar}.";
2132151497Sru            fi;
2133151497Sru          else			# rest is the argument
2134151497Sru            list_append lfc_result "-${lfc_optchar}" "${lfc_rest}";
2135151497Sru            lfc_rest='';
2136151497Sru            continue;
2137151497Sru          fi;
2138151497Sru        else
2139151497Sru          error_user "unknown option -${lfc_optchar}.";
2140151497Sru        fi;
2141151497Sru      done;
2142151497Sru      ;;
2143151497Sru    *)
2144151497Sru      # Here, $lfc_arg is not an option, so a file parameter.
2145151497Sru      list_append lfc_fparams "${lfc_arg}";
2146151497Sru
2147151497Sru      # Ignore the strange POSIX option handling to end option
2148151497Sru      # parsing after the first file name argument.  To reuse it, do
2149151497Sru      # a `break' here if $POSIXLY_CORRECT of `bash' is not empty.
2150151497Sru      # When `bash' is called as `sh' $POSIXLY_CORRECT is set
2151151497Sru      # automatically to `y'.
2152151497Sru      ;;
2153151497Sru    esac;
2154151497Sru  done;
2155151497Sru  list_append lfc_result '--';
2156151497Sru  if obj lfc_fparams is_not_empty
2157151497Sru  then
2158151497Sru    lfc_result="${lfc_result} ${lfc_fparams}";
2159151497Sru  fi;
2160151497Sru  if test "$#" -gt 0
2161151497Sru  then
2162151497Sru    list_append lfc_result "$@";
2163151497Sru  fi;
2164151497Sru  obj lfc_result echo1;
2165151497Sru  eval ${_UNSET} lfc_abbrev;
2166151497Sru  eval ${_UNSET} lfc_fparams;
2167151497Sru  eval ${_UNSET} lfc_short_a;
2168151497Sru  eval ${_UNSET} lfc_short_n;
2169151497Sru  eval ${_UNSET} lfc_long_a;
2170151497Sru  eval ${_UNSET} lfc_long_n;
2171151497Sru  eval ${_UNSET} lfc_result;
2172151497Sru  eval ${_UNSET} lfc_arg;
2173151497Sru  eval ${_UNSET} lfc_opt;
2174151497Sru  eval ${_UNSET} lfc_opt_arg;
2175151497Sru  eval ${_UNSET} lfc_opt_char;
2176151497Sru  eval ${_UNSET} lfc_with_equal;
2177151497Sru  eval ${_UNSET} lfc_rest;
2178151497Sru  eval "${return_ok}";
2179151497Sru} # list_from_cmdline()
2180151497Sru
2181151497Sru
2182151497Sru########################################################################
2183151497Sru# list_from_split (<string> <separator>)
2184151497Sru#
2185151497Sru# In <string>, escape all white space characters and replace each
2186151497Sru# <separator> by space.
2187151497Sru#
2188151497Sru# Arguments: 2: a <string> that is to be split into parts divided by
2189151497Sru#               <separator>
2190151497Sru# Output:    the resulting list string
2191151497Sru#
2192151497Sru# Variable prefix: lfs
2193151497Sru#
2194151497Srulist_from_split()
2195151497Sru{
2196151497Sru  func_check list_from_split = 2 "$@";
2197151497Sru
2198151497Sru  # precede each space or tab by a backslash `\' (doubled for `sed')
2199151497Sru  lfs_s="$(echo1 "$1" | sed -e 's/\('"${_SPACE_SED}"'\)/\\\1/g')";
2200151497Sru  exit_test;
2201151497Sru
2202151497Sru  # replace split character of string by the list separator ` ' (space).
2203151497Sru  case "$2" in
2204151497Sru    /)				# cannot use normal `sed' separator
2205151497Sru      echo1 "${lfs_s}" | sed -e 's|'"$2"'| |g';
2206151497Sru      ;;
2207151497Sru    ?)				# use normal `sed' separator
2208151497Sru      echo1 "${lfs_s}" | sed -e 's/'"$2"'/ /g';
2209151497Sru      ;;
2210151497Sru    ??*)
2211151497Sru      error 'list_from_split(): separator must be a single character.';
2212151497Sru      ;;
2213151497Sru  esac;
2214151497Sru  eval ${_UNSET} lfs_s;
2215151497Sru  eval "${return_ok}";
2216151497Sru}
2217151497Sru
2218151497Sru
2219151497Sru########################################################################
2220151497Sru# list_get (<list>)
2221151497Sru#
2222151497Sru# Check whether <list> is a space-separated list of '-quoted elements.
2223151497Sru#
2224151497Sru# If the test fails an error is raised.
2225151497Sru# If the test succeeds the argument is echoed.
2226151497Sru#
2227151497Sru# Testing criteria:
2228151497Sru#   A list has the form "'first' 'second' '...' 'last'".  So it has a
2229151497Sru#   leading and a final quote and the elements are separated by "' '"
2230151497Sru#   constructs.  If these are all removed there should not be any
2231151497Sru#   unescaped single-quotes left.  Watch out for escaped single
2232151497Sru#   quotes; they have the form '\'' (sq bs sq sq).
2233151497Sru
2234151497Sru# Arguments: 1
2235151497Sru# Output: the argument <list> unchanged, if the check succeeded.
2236151497Sru#
2237151497Sru# Variable prefix: lg
2238151497Sru#
2239151497Srulist_get()
2240151497Sru{
2241151497Sru  func_check list_get = 1 "$@";
2242151497Sru  eval lg_list='"${'$1'}"';
2243151497Sru  # remove leading and final space characters
2244151497Sru  lg_list="$(echo1 "${lg_list}" | sed -e '
2245151497Srus/^'"${_SPACE_SED}"'*//
2246151497Srus/'"${_SPACE_SED}"'*$//
2247151497Sru')";
2248151497Sru  exit_test;
2249151497Sru  case "${lg_list}" in
2250151497Sru  '')
2251151497Sru    eval ${_UNSET} lg_list;
2252151497Sru    eval "${return_ok}";
2253151497Sru    ;;
2254151497Sru  \'*\')
2255151497Sru    obj lg_list echo1;
2256151497Sru    eval ${_UNSET} lg_list;
2257151497Sru    eval "${return_ok}";
2258151497Sru    ;;
2259151497Sru  *)
2260151497Sru    error "list_get(): bad list: $1"
2261151497Sru    ;;
2262151497Sru  esac;
2263151497Sru  eval ${_UNSET} lg_list;
2264151497Sru  eval "${return_ok}";
2265151497Sru}
2266151497Sru
2267151497Sru
2268151497Sru########################################################################
2269151497Sru# list_has (<var_name> <element>)
2270151497Sru#
2271151497Sru# Test whether the list <var_name> has the element <element>.
2272151497Sru#
2273151497Sru# Arguments: 2
2274151497Sru#   <var_name>: a variable name for a list of single-quoted elements
2275151497Sru#   <element>:  some sequence of characters.
2276151497Sru#
2277151497Sru# Variable prefix: lh
2278151497Sru#
2279151497Srulist_has()
2280151497Sru{
2281151497Sru  func_check list_has = 2 "$@";
2282151497Sru  eval lh_list='"${'$1'}"';
2283151497Sru  if obj lh_list is_empty
2284151497Sru  then
2285151497Sru    eval "${_UNSET}" lh_list;
2286151497Sru    eval "${return_no}";
2287151497Sru  fi;
2288151497Sru  case "$2" in
2289151497Sru    \'*\')  lh_element=" $2 "; ;;
2290151497Sru    *)      lh_element=" '$2' "; ;;
2291151497Sru  esac;
2292151497Sru  if string_contains " ${lh_list} " "${lh_element}"
2293151497Sru  then
2294151497Sru    eval "${_UNSET}" lh_list;
2295151497Sru    eval "${_UNSET}" lh_element;
2296151497Sru    eval "${return_yes}";
2297151497Sru  else
2298151497Sru    eval "${_UNSET}" lh_list;
2299151497Sru    eval "${_UNSET}" lh_element;
2300151497Sru    eval "${return_no}";
2301151497Sru  fi;
2302151497Sru}
2303151497Sru
2304151497Sru
2305151497Sru########################################################################
2306151497Sru# list_has_abbrev (<var_name> <abbrev>)
2307151497Sru#
2308151497Sru# Test whether the list <var_name> has an element starting with <abbrev>.
2309151497Sru#
2310151497Sru# Arguments: 2
2311151497Sru#   <var_name>: a variable name for a list of single-quoted elements
2312151497Sru#   <abbrev>:   some sequence of characters.
2313151497Sru#
2314151497Sru# Variable prefix: lha
2315151497Sru#
2316151497Srulist_has_abbrev()
2317151497Sru{
2318151497Sru  func_check list_has_abbrev = 2 "$@";
2319151497Sru  eval lha_list='"${'$1'}"';
2320151497Sru  if obj lha_list is_empty
2321151497Sru  then
2322151497Sru    eval "${_UNSET}" lha_list;
2323151497Sru    eval "${return_no}";
2324151497Sru  fi;
2325151497Sru  case "$2" in
2326151497Sru    \'*)
2327151497Sru      lha_element="$(echo1 "$2" | sed -e 's/'"${_SQ}"'$//')";
2328151497Sru      exit_test;
2329151497Sru      ;;
2330151497Sru    *) lha_element="'$2"; ;;
2331151497Sru  esac;
2332151497Sru  if string_contains " ${lha_list}" " ${lha_element}"
2333151497Sru  then
2334151497Sru    eval "${_UNSET}" lha_list;
2335151497Sru    eval "${_UNSET}" lha_element;
2336151497Sru    eval "${return_yes}";
2337151497Sru  else
2338151497Sru    eval "${_UNSET}" lha_list;
2339151497Sru    eval "${_UNSET}" lha_element;
2340151497Sru    eval "${return_no}";
2341151497Sru  fi;
2342151497Sru  eval "${return_ok}";
2343151497Sru}
2344151497Sru
2345151497Sru
2346151497Sru########################################################################
2347151497Sru# list_has_not (<list> <element>)
2348151497Sru#
2349151497Sru# Test whether <list> has no <element>.
2350151497Sru#
2351151497Sru# Arguments: 2
2352151497Sru#   <list>:    a space-separated list of single-quoted elements.
2353151497Sru#   <element>: some sequence of characters.
2354151497Sru#
2355151497Sru# Variable prefix: lhn
2356151497Sru#
2357151497Srulist_has_not()
2358151497Sru{
2359151497Sru  func_check list_has_not = 2 "$@";
2360151497Sru  eval lhn_list='"${'$1'}"';
2361151497Sru  if obj lhn_list is_empty
2362151497Sru  then
2363151497Sru    eval "${_UNSET}" lhn_list;
2364151497Sru    eval "${return_yes}";
2365151497Sru  fi;
2366151497Sru  case "$2" in
2367151497Sru    \'*\') lhn_element=" $2 "; ;;
2368151497Sru    *)     lhn_element=" '$2' "; ;;
2369151497Sru  esac;
2370151497Sru  if string_contains " ${lhn_list} " "${lhn_element}"
2371151497Sru  then
2372151497Sru    eval "${_UNSET}" lhn_list;
2373151497Sru    eval "${_UNSET}" lhn_element;
2374151497Sru    eval "${return_no}";
2375151497Sru  else
2376151497Sru    eval "${_UNSET}" lhn_list;
2377151497Sru    eval "${_UNSET}" lhn_element;
2378151497Sru    eval "${return_yes}";
2379151497Sru  fi;
2380151497Sru}
2381151497Sru
2382151497Sru
2383151497Sru########################################################################
2384151497Sru# list_single_from_abbrev (<list> <abbrev>)
2385151497Sru#
2386151497Sru# Check whether the list has an element starting with <abbrev>.  If
2387151497Sru# there are more than a single element an error is created.
2388151497Sru#
2389151497Sru# Arguments: 2
2390151497Sru#   <list>:   a variable name for a list of single-quoted elements
2391151497Sru#   <abbrev>: some sequence of characters.
2392151497Sru#
2393151497Sru# Output: the found element.
2394151497Sru#
2395151497Sru# Variable prefix: lsfa
2396151497Sru#
2397151497Srulist_single_from_abbrev()
2398151497Sru{
2399151497Sru  func_check list_single_from_abbrev = 2 "$@";
2400151497Sru  eval lsfa_list='"${'$1'}"';
2401151497Sru  if obj lsfa_list is_empty
2402151497Sru  then
2403151497Sru    eval "${_UNSET}" lsfa_list;
2404151497Sru    eval "${return_no}";
2405151497Sru  fi;
2406151497Sru  lsfa_abbrev="$2";
2407151497Sru  if list_has lsfa_list "${lsfa_abbrev}"
2408151497Sru  then
2409151497Sru    obj lsfa_abbrev echo1;
2410151497Sru    eval "${_UNSET}" lsfa_abbrev;
2411151497Sru    eval "${_UNSET}" lsfa_list;
2412151497Sru    eval "${return_yes}";
2413151497Sru  fi;
2414151497Sru  if list_has_abbrev lsfa_list "${lsfa_abbrev}"
2415151497Sru  then
2416151497Sru    lsfa_element='';
2417151497Sru    eval set x "${lsfa_list}";
2418151497Sru    shift;
2419151497Sru    for i
2420151497Sru    do
2421151497Sru      case "$i" in
2422151497Sru      ${lsfa_abbrev}*)
2423151497Sru        if obj lsfa_element is_not_empty
2424151497Sru        then
2425151497Sru          error_user "The abbreviation --${lsfa_abbrev} \
2426151497Sruhas multiple options: --${lsfa_element} and --${i}.";
2427151497Sru        fi;
2428151497Sru        lsfa_element="$i";
2429151497Sru        ;;
2430151497Sru      esac;
2431151497Sru    done;
2432151497Sru    obj lsfa_element echo1;
2433151497Sru    eval "${_UNSET}" lsfa_abbrev;
2434151497Sru    eval "${_UNSET}" lsfa_element;
2435151497Sru    eval "${_UNSET}" lsfa_list;
2436151497Sru    eval "${return_yes}";
2437151497Sru  else
2438151497Sru    eval "${_UNSET}" lsfa_abbrev;
2439151497Sru    eval "${_UNSET}" lsfa_element;
2440151497Sru    eval "${_UNSET}" lsfa_list;
2441151497Sru    eval "${return_no}";
2442151497Sru  fi;
2443151497Sru}
2444151497Sru
2445151497Sru
2446151497Sru########################################################################
2447151497Srulandmark '7: man_*()';
2448151497Sru########################################################################
2449151497Sru
2450151497Sru########################################################################
2451151497Sru# man_do_filespec (<filespec>)
2452151497Sru#
2453151497Sru# Print suitable man page(s) for filespec to $_TMP_CAT.
2454151497Sru#
2455151497Sru# Arguments : 2
2456151497Sru#   <filespec>: argument of the form `man:name.section', `man:name',
2457151497Sru#               `man:name(section)', `name.section', `name'.
2458151497Sru#
2459151497Sru# Globals   : $_OPT_ALL
2460151497Sru#
2461151497Sru# Output    : none.
2462151497Sru# Return    : `0' if man page was found, `1' else.
2463151497Sru#
2464151497Sru# Only called from do_fileargs(), checks on $MANPATH and $_MAN_ENABLE
2465151497Sru# are assumed (see man_setup()).
2466151497Sru#
2467151497Sru# Variable prefix: mdf
2468151497Sru#
2469151497Sruman_do_filespec()
2470151497Sru{
2471151497Sru  func_check man_do_filespec = 1 "$@";
2472151497Sru  if obj _MAN_PATH is_empty
2473151497Sru  then
2474151497Sru    eval "${return_bad}";
2475151497Sru  fi;
2476151497Sru  if is_empty "$1"
2477151497Sru  then
2478151497Sru    eval "${return_bad}";
2479151497Sru  fi;
2480151497Sru  mdf_spec="$1";
2481151497Sru  mdf_name='';
2482151497Sru  mdf_section='';
2483151497Sru  case "${mdf_spec}" in
2484151497Sru  */*)				# not a man spec with containing '/'
2485151497Sru    eval ${_UNSET} mdf_got_one;
2486151497Sru    eval ${_UNSET} mdf_name;
2487151497Sru    eval ${_UNSET} mdf_section;
2488151497Sru    eval ${_UNSET} mdf_spec;
2489151497Sru    eval "${return_bad}";
2490151497Sru    ;;
2491151497Sru  man:?*\(?*\))			# man:name(section)
2492151497Sru    mdf_name="$(echo1 "${mdf_spec}" \
2493151497Sru                | sed -e 's/^man:\(..*\)(\(..*\))$/\1/')";
2494151497Sru    mdf_section="$(echo1 "${mdf_spec}" \
2495151497Sru                   | sed -e 's/^man:\(..*\)(\(..*\))$/\2/')";
2496151497Sru    exit_test;
2497151497Sru    ;;
2498151497Sru  man:?*.${_MAN_AUTO_SEC_CHARS}) # man:name.section
2499151497Sru    mdf_name="$(echo1 "${mdf_spec}" \
2500151497Sru                | sed -e 's/^man:\(..*\)\..$/\1/')";
2501151497Sru    mdf_section="$(echo1 "${mdf_spec}" \
2502151497Sru                   | sed -e 's/^.*\(.\)$/\1/')";
2503151497Sru    exit_test;
2504151497Sru    ;;
2505151497Sru  man:?*)			# man:name
2506151497Sru    mdf_name="$(echo1 "${mdf_spec}" | sed -e 's/^man://')";
2507151497Sru    exit_test;
2508151497Sru    ;;
2509151497Sru  ?*\(?*\))			# name(section)
2510151497Sru    mdf_name="$(echo1 "${mdf_spec}" \
2511151497Sru                | sed -e 's/^\(..*\)(\(..*\))$/\1/')";
2512151497Sru    mdf_section="$(echo1 "${mdf_spec}" \
2513151497Sru                   | sed -e 's/^\(..*\)(\(..*\))$/\2/')";
2514151497Sru    exit_test;
2515151497Sru    ;;
2516151497Sru  ?*.${_MAN_AUTO_SEC_CHARS})	# name.section
2517151497Sru    mdf_name="$(echo1 "${mdf_spec}" \
2518151497Sru                | sed -e 's/^\(..*\)\..$/\1/')";
2519151497Sru    mdf_section="$(echo1 "${mdf_spec}" \
2520151497Sru                   | sed -e 's/^.*\(.\)$/\1/')";
2521151497Sru    exit_test;
2522151497Sru    ;;
2523151497Sru  ?*)
2524151497Sru    mdf_name="${mdf_spec}";
2525151497Sru    ;;
2526151497Sru  esac;
2527151497Sru  if obj mdf_name is_empty
2528151497Sru  then
2529151497Sru    eval ${_UNSET} mdf_got_one;
2530151497Sru    eval ${_UNSET} mdf_name;
2531151497Sru    eval ${_UNSET} mdf_section;
2532151497Sru    eval ${_UNSET} mdf_spec;
2533151497Sru    eval "${return_bad}";
2534151497Sru  fi;
2535151497Sru  mdf_got_one='no';
2536151497Sru  if obj mdf_section is_empty
2537151497Sru  then
2538151497Sru    if obj _OPT_SECTIONS is_empty
2539151497Sru    then
2540151497Sru      eval set x "${_MAN_AUTO_SEC_LIST}";
2541151497Sru    else
2542151497Sru      # use --sections when no section is given to filespec
2543151497Sru      eval set x "$(echo1 "${_OPT_SECTIONS}" | sed -e 's/:/ /g')";
2544151497Sru    fi;
2545151497Sru    shift;
2546151497Sru    for s
2547151497Sru    do
2548151497Sru      mdf_s="$s";
2549151497Sru      if man_search_section "${mdf_name}" "${mdf_s}"
2550151497Sru      then			# found
2551151497Sru        if obj _MAN_ALL is_yes
2552151497Sru        then
2553151497Sru          mdf_got_one='yes';
2554151497Sru        else
2555151497Sru          eval ${_UNSET} mdf_got_one;
2556151497Sru          eval ${_UNSET} mdf_name;
2557151497Sru          eval ${_UNSET} mdf_s;
2558151497Sru          eval ${_UNSET} mdf_section;
2559151497Sru          eval ${_UNSET} mdf_spec;
2560151497Sru          eval "${return_good}";
2561151497Sru        fi;
2562151497Sru      fi;
2563151497Sru    done;
2564151497Sru  else
2565151497Sru    if man_search_section "${mdf_name}" "${mdf_section}"
2566151497Sru    then
2567151497Sru      eval ${_UNSET} mdf_got_one;
2568151497Sru      eval ${_UNSET} mdf_name;
2569151497Sru      eval ${_UNSET} mdf_s;
2570151497Sru      eval ${_UNSET} mdf_section;
2571151497Sru      eval ${_UNSET} mdf_spec;
2572151497Sru      eval "${return_good}";
2573151497Sru    else
2574151497Sru      eval ${_UNSET} mdf_got_one;
2575151497Sru      eval ${_UNSET} mdf_name;
2576151497Sru      eval ${_UNSET} mdf_section;
2577151497Sru      eval ${_UNSET} mdf_spec;
2578151497Sru      eval "${return_bad}";
2579151497Sru    fi;
2580151497Sru  fi;
2581151497Sru  if obj _MAN_ALL is_yes && obj mdf_got_one is_yes
2582151497Sru  then
2583151497Sru    eval ${_UNSET} mdf_got_one;
2584151497Sru    eval ${_UNSET} mdf_name;
2585151497Sru    eval ${_UNSET} mdf_s;
2586151497Sru    eval ${_UNSET} mdf_section;
2587151497Sru    eval ${_UNSET} mdf_spec;
2588151497Sru    eval "${return_good}";
2589151497Sru  fi;
2590151497Sru  eval ${_UNSET} mdf_got_one;
2591151497Sru  eval ${_UNSET} mdf_name;
2592151497Sru  eval ${_UNSET} mdf_s;
2593151497Sru  eval ${_UNSET} mdf_section;
2594151497Sru  eval ${_UNSET} mdf_spec;
2595151497Sru  eval "${return_bad}";
2596151497Sru} # man_do_filespec()
2597151497Sru
2598151497Sru
2599151497Sru########################################################################
2600151497Sru# man_register_file (<file> <name> [<section>])
2601151497Sru#
2602151497Sru# Write a found man page file and register the title element.
2603151497Sru#
2604151497Sru# Arguments: 1, 2, or 3; maybe empty
2605151497Sru# Output: none
2606151497Sru#
2607151497Sruman_register_file()
2608151497Sru{
2609151497Sru  func_check man_register_file '>=' 2 "$@";
2610151497Sru  case "$#" in
2611151497Sru    2|3) do_nothing; ;;
2612151497Sru    *)
2613151497Sru      error "man_register_file() expects 2 or 3 arguments.";
2614151497Sru      ;;
2615151497Sru  esac;
2616151497Sru  if is_empty "$1"
2617151497Sru  then
2618151497Sru    error 'man_register_file(): file name is empty';
2619151497Sru  fi;
2620151497Sru  to_tmp "$1";
2621151497Sru  case "$#" in
2622151497Sru    2)
2623151497Sru       register_title "man:$2";
2624151497Sru       eval "${return_ok}";
2625151497Sru       ;;
2626151497Sru    3)
2627151497Sru       register_title "$2.$3";
2628151497Sru       eval "${return_ok}";
2629151497Sru       ;;
2630151497Sru  esac;
2631151497Sru  eval "${return_ok}";
2632151497Sru}
2633151497Sru
2634151497Sru
2635151497Sru########################################################################
2636151497Sru# man_search_section (<name> <section>)
2637151497Sru#
2638151497Sru# Retrieve man pages.
2639151497Sru#
2640151497Sru# Arguments : 2
2641151497Sru# Globals   : $_MAN_PATH, $_MAN_EXT
2642151497Sru# Return    : 0 if found, 1 otherwise
2643151497Sru#
2644151497Sru# Variable prefix: mss
2645151497Sru#
2646151497Sruman_search_section()
2647151497Sru{
2648151497Sru  func_check man_search_section = 2 "$@";
2649151497Sru  if obj _MAN_PATH is_empty
2650151497Sru  then
2651151497Sru    eval "${return_bad}";
2652151497Sru  fi;
2653151497Sru  if is_empty "$1"
2654151497Sru  then
2655151497Sru    eval "${return_bad}";
2656151497Sru  fi;
2657151497Sru  if is_empty "$2"
2658151497Sru  then
2659151497Sru    eval "${return_bad}";
2660151497Sru  fi;
2661151497Sru  mss_name="$1";
2662151497Sru  mss_section="$2";
2663151497Sru  eval set x "$(path_split "${_MAN_PATH}")";
2664151497Sru  exit_test;
2665151497Sru  shift;
2666151497Sru  mss_got_one='no';
2667151497Sru  if obj _MAN_EXT is_empty
2668151497Sru  then
2669151497Sru    for d
2670151497Sru    do
2671151497Sru      mss_dir="$(dirname_append "$d" "man${mss_section}")";
2672151497Sru      exit_test;
2673151497Sru      if obj mss_dir is_dir
2674151497Sru      then
2675151497Sru        mss_prefix="$(\
2676151497Sru          dirname_append "${mss_dir}" "${mss_name}.${mss_section}")";
2677151497Sru        if obj _OPT_WHATIS is_yes
2678151497Sru        then
2679151497Sru          mss_files="$(eval ls "${mss_prefix}"'*' 2>${_NULL_DEV} |
2680151497Sru                       sed -e '\| found|s|.*||'
2681151497Sru                       )";
2682151497Sru        else
2683151497Sru          mss_files="$(eval ls "'${mss_prefix}'"'*' 2>${_NULL_DEV} |
2684151497Sru                       sed -e '\| found|s|.*||'
2685151497Sru                       )";
2686151497Sru        fi;
2687151497Sru        exit_test;
2688151497Sru        if obj mss_files is_not_empty
2689151497Sru        then
2690151497Sru          # for f in $mss_files
2691151497Sru          for f in $(eval set x ${mss_files}; shift; echo1 "$@")
2692151497Sru          do
2693151497Sru            exit_test;
2694151497Sru            mss_f="$f";
2695151497Sru            if obj mss_f is_file
2696151497Sru            then
2697151497Sru              if is_yes "${mss_got_one}"
2698151497Sru              then
2699151497Sru                register_file "${mss_f}";
2700151497Sru              elif obj _MAN_ALL is_yes
2701151497Sru              then
2702151497Sru                man_register_file "${mss_f}" "${mss_name}";
2703151497Sru              else
2704151497Sru                man_register_file "${mss_f}" "${mss_name}" "${mss_section}";
2705151497Sru                eval ${_UNSET} mss_dir;
2706151497Sru                eval ${_UNSET} mss_ext;
2707151497Sru                eval ${_UNSET} mss_f;
2708151497Sru                eval ${_UNSET} mss_files;
2709151497Sru                eval ${_UNSET} mss_got_one;
2710151497Sru                eval ${_UNSET} mss_name;
2711151497Sru                eval ${_UNSET} mss_prefix;
2712151497Sru                eval ${_UNSET} mss_section;
2713151497Sru                eval "${return_good}";
2714151497Sru              fi;
2715151497Sru              mss_got_one='yes';
2716151497Sru            fi;
2717151497Sru          done;
2718151497Sru        fi;
2719151497Sru      fi;
2720151497Sru    done;
2721151497Sru  else
2722151497Sru    mss_ext="${_MAN_EXT}";
2723151497Sru    # check for directory name having trailing extension
2724151497Sru    for d
2725151497Sru    do
2726151497Sru      mss_dir="$(dirname_append $d man${mss_section}${mss_ext})";
2727151497Sru      exit_test;
2728151497Sru      if obj mss_dir is_dir
2729151497Sru      then
2730151497Sru        mss_prefix=\
2731151497Sru          "$(dirname_append "${mss_dir}" "${mss_name}.${mss_section}")";
2732151497Sru        mss_files="$( eval ls "${mss_prefix}"'*' 2>${_NULL_DEV} |
2733151497Sru                     sed -e '\|not found|s|.*||'
2734151497Sru                     )";
2735151497Sru        exit_test;
2736151497Sru        if obj mss_files is_not_empty
2737151497Sru        then
2738151497Sru          # for f in $mss_files
2739151497Sru          for f in $(eval set x ${mss_files}; shift; echo1 "$@")
2740151497Sru          do
2741151497Sru            mss_f="$f";
2742151497Sru            if obj mss_f is_file
2743151497Sru            then
2744151497Sru              if is_yes "${mss_got_one}"
2745151497Sru              then
2746151497Sru                register_file "${mss_f}";
2747151497Sru              elif obj _MAN_ALL is_yes
2748151497Sru              then
2749151497Sru                man_register_file "${mss_f}" "${mss_name}";
2750151497Sru              else
2751151497Sru                man_register_file "${mss_f}" "${mss_name}" "${mss_section}";
2752151497Sru                eval ${_UNSET} mss_dir;
2753151497Sru                eval ${_UNSET} mss_ext;
2754151497Sru                eval ${_UNSET} mss_f;
2755151497Sru                eval ${_UNSET} mss_files;
2756151497Sru                eval ${_UNSET} mss_got_one;
2757151497Sru                eval ${_UNSET} mss_name;
2758151497Sru                eval ${_UNSET} mss_prefix;
2759151497Sru                eval ${_UNSET} mss_section;
2760151497Sru                eval "${return_good}";
2761151497Sru              fi;
2762151497Sru              mss_got_one='yes';
2763151497Sru            fi;
2764151497Sru          done;
2765151497Sru        fi;
2766151497Sru      fi;
2767151497Sru    done;
2768151497Sru    # check for files with extension in directories without extension
2769151497Sru    for d
2770151497Sru    do
2771151497Sru      mss_dir="$(dirname_append "$d" "man${mss_section}")";
2772151497Sru      exit_test;
2773151497Sru      if obj mss_dir is_dir
2774151497Sru      then
2775151497Sru        mss_prefix="$(dirname_append "${mss_dir}" \
2776151497Sru                        "${mss_name}.${mss_section}${mss_ext}")";
2777151497Sru        mss_files="$(eval ls "${mss_prefix}"'*' 2>${_NULL_DEV} |
2778151497Sru                     sed -e '\|not found|s|.*||'
2779151497Sru                     )";
2780151497Sru        exit_test;
2781151497Sru        if obj mss_files is_not_empty
2782151497Sru        then
2783151497Sru          # for f in $mss_files
2784151497Sru          for f in $(eval set x ${mss_files}; shift; echo1 "$@")
2785151497Sru          do
2786151497Sru            mss_f="$f";
2787151497Sru            if obj mss_f is_file
2788151497Sru            then
2789151497Sru              if is_yes "${mss_got_one}"
2790151497Sru              then
2791151497Sru                register_file "${mss_f}";
2792151497Sru              elif obj _MAN_ALL is_yes
2793151497Sru              then
2794151497Sru                man_register_file "${mss_f}" "${mss_name}";
2795151497Sru              else
2796151497Sru                man_register_file "${mss_f}" "${mss_name}" "${mss_section}";
2797151497Sru                eval ${_UNSET} mss_dir;
2798151497Sru                eval ${_UNSET} mss_ext;
2799151497Sru                eval ${_UNSET} mss_f;
2800151497Sru                eval ${_UNSET} mss_files;
2801151497Sru                eval ${_UNSET} mss_got_one;
2802151497Sru                eval ${_UNSET} mss_name;
2803151497Sru                eval ${_UNSET} mss_prefix;
2804151497Sru                eval ${_UNSET} mss_section;
2805151497Sru                eval "${return_good}";
2806151497Sru              fi;
2807151497Sru              mss_got_one='yes';
2808151497Sru            fi;
2809151497Sru          done;
2810151497Sru        fi;
2811151497Sru      fi;
2812151497Sru    done;
2813151497Sru  fi;
2814151497Sru  if obj _MAN_ALL is_yes && is_yes "${mss_got_one}"
2815151497Sru  then
2816151497Sru    eval ${_UNSET} mss_dir;
2817151497Sru    eval ${_UNSET} mss_ext;
2818151497Sru    eval ${_UNSET} mss_f;
2819151497Sru    eval ${_UNSET} mss_files;
2820151497Sru    eval ${_UNSET} mss_got_one;
2821151497Sru    eval ${_UNSET} mss_name;
2822151497Sru    eval ${_UNSET} mss_prefix;
2823151497Sru    eval ${_UNSET} mss_section;
2824151497Sru    eval "${return_good}";
2825151497Sru  fi;
2826151497Sru  eval ${_UNSET} mss_dir;
2827151497Sru  eval ${_UNSET} mss_ext;
2828151497Sru  eval ${_UNSET} mss_f;
2829151497Sru  eval ${_UNSET} mss_files;
2830151497Sru  eval ${_UNSET} mss_got_one;
2831151497Sru  eval ${_UNSET} mss_name;
2832151497Sru  eval ${_UNSET} mss_prefix;
2833151497Sru  eval ${_UNSET} mss_section;
2834151497Sru  eval "${return_bad}";
2835151497Sru} # man_search_section()
2836151497Sru
2837151497Sru
2838151497Sru########################################################################
2839151497Sru# man_setup ()
2840151497Sru#
2841151497Sru# Setup the variables $_MAN_* needed for man page searching.
2842151497Sru#
2843151497Sru# Globals:
2844151497Sru#   in:     $_OPT_*, $_MANOPT_*, $LANG, $LC_MESSAGES, $LC_ALL,
2845151497Sru#           $MANPATH, $MANROFFSEQ, $MANSEC, $PAGER, $SYSTEM, $MANOPT.
2846151497Sru#   out:    $_MAN_PATH, $_MAN_LANG, $_MAN_SYS, $_MAN_LANG, $_MAN_LANG2,
2847151497Sru#           $_MAN_SEC, $_MAN_ALL
2848151497Sru#   in/out: $_MAN_ENABLE
2849151497Sru#
2850151497Sru# The precedence for the variables related to `man' is that of GNU
2851151497Sru# `man', i.e.
2852151497Sru#
2853151497Sru# $LANG; overridden by
2854151497Sru# $LC_MESSAGES; overridden by
2855151497Sru# $LC_ALL; this has the same precedence as
2856151497Sru# $MANPATH, $MANROFFSEQ, $MANSEC, $PAGER, $SYSTEM; overridden by
2857151497Sru# $MANOPT; overridden by
2858151497Sru# the groffer command line options.
2859151497Sru#
2860151497Sru# Variable prefix: ms
2861151497Sru#
2862151497Sruman_setup()
2863151497Sru{
2864151497Sru  func_check main_man_setup = 0 "$@";
2865151497Sru
2866151497Sru  if obj _MAN_IS_SETUP is_yes
2867151497Sru  then
2868151497Sru    eval "${return_ok}";
2869151497Sru  fi;
2870151497Sru  _MAN_IS_SETUP='yes';
2871151497Sru
2872151497Sru  if obj _MAN_ENABLE is_not_yes
2873151497Sru  then
2874151497Sru    eval "${return_ok}";
2875151497Sru  fi;
2876151497Sru
2877151497Sru  # determine basic path for man pages
2878151497Sru  _MAN_PATH="$(get_first_essential \
2879151497Sru               "${_OPT_MANPATH}" "${_MANOPT_PATH}" "${MANPATH}")";
2880151497Sru  exit_test;
2881151497Sru  if obj _MAN_PATH is_empty
2882151497Sru  then
2883151497Sru    manpath_set_from_path;
2884151497Sru  else
2885151497Sru    _MAN_PATH="$(path_clean "${_MAN_PATH}")";
2886151497Sru    exit_test;
2887151497Sru  fi;
2888151497Sru  if obj _MAN_PATH is_empty
2889151497Sru  then
2890151497Sru    if is_prog 'manpath'
2891151497Sru    then
2892151497Sru      _MAN_PATH="$(manpath 2>${_NULL_DEV})"; # not always available
2893151497Sru      exit_test;
2894151497Sru    fi;
2895151497Sru  fi;
2896151497Sru  if obj _MAN_PATH is_empty
2897151497Sru  then
2898151497Sru    _MAN_ENABLE="no";
2899151497Sru    eval "${return_ok}";
2900151497Sru  fi;
2901151497Sru
2902151497Sru  _MAN_ALL="$(get_first_essential "${_OPT_ALL}" "${_MANOPT_ALL}")";
2903151497Sru  exit_test;
2904151497Sru  if obj _MAN_ALL is_empty
2905151497Sru  then
2906151497Sru    _MAN_ALL='no';
2907151497Sru  fi;
2908151497Sru
2909151497Sru  _MAN_SYS="$(get_first_essential \
2910151497Sru              "${_OPT_SYSTEMS}" "${_MANOPT_SYS}" "${SYSTEM}")";
2911151497Sru  ms_lang="$(get_first_essential \
2912151497Sru           "${_OPT_LANG}" "${LC_ALL}" "${LC_MESSAGES}" "${LANG}")";
2913151497Sru  exit_test;
2914151497Sru  case "${ms_lang}" in
2915151497Sru    C|POSIX)
2916151497Sru      _MAN_LANG="";
2917151497Sru      _MAN_LANG2="";
2918151497Sru      ;;
2919151497Sru    ?)
2920151497Sru      _MAN_LANG="${ms_lang}";
2921151497Sru      _MAN_LANG2="";
2922151497Sru      ;;
2923151497Sru    *)
2924151497Sru      _MAN_LANG="${ms_lang}";
2925151497Sru      # get first two characters of $ms_lang
2926151497Sru      _MAN_LANG2="$(echo1 "${ms_lang}" | sed -e 's/^\(..\).*$/\1/')";
2927151497Sru      exit_test;
2928151497Sru      ;;
2929151497Sru  esac;
2930151497Sru  # from now on, use only $_LANG, forget about $_OPT_LANG, $LC_*.
2931151497Sru
2932151497Sru  manpath_add_lang_sys;		# this is very slow
2933151497Sru
2934151497Sru  _MAN_SEC="$(get_first_essential \
2935151497Sru              "${_OPT_SECT}" "${_MANOPT_SEC}" "${MANSEC}")";
2936151497Sru  exit_test;
2937151497Sru  if obj _MAN_PATH is_empty
2938151497Sru  then
2939151497Sru    _MAN_ENABLE="no";
2940151497Sru    eval ${_UNSET} ms_lang;
2941151497Sru    eval "${return_ok}";
2942151497Sru  fi;
2943151497Sru
2944151497Sru  _MAN_EXT="$(get_first_essential \
2945151497Sru              "${_OPT_EXTENSION}" "${_MANOPT_EXTENSION}")";
2946151497Sru  exit_test;
2947151497Sru  eval ${_UNSET} ms_lang;
2948151497Sru  eval "${return_ok}";
2949151497Sru} # man_setup()
2950151497Sru
2951151497Sru
2952151497Sru########################################################################
2953151497Srulandmark '8: manpath_*()';
2954151497Sru########################################################################
2955151497Sru
2956151497Sru########################################################################
2957151497Sru# manpath_add_lang_sys ()
2958151497Sru#
2959151497Sru# Add language and operating system specific directories to man path.
2960151497Sru#
2961151497Sru# Arguments : 0
2962151497Sru# Output    : none
2963151497Sru# Globals:
2964151497Sru#   in:     $_MAN_SYS: has the form `os1,os2,...', a comma separated
2965151497Sru#             list of names of operating systems.
2966151497Sru#           $_MAN_LANG and $_MAN_LANG2: each a single name
2967151497Sru#   in/out: $_MAN_PATH: has the form `dir1:dir2:...', a colon
2968151497Sru#             separated list of directories.
2969151497Sru#
2970151497Sru# Variable prefix: mals
2971151497Sru#
2972151497Srumanpath_add_lang_sys()
2973151497Sru{
2974151497Sru  func_check manpath_add_lang_sys = 0 "$@";
2975151497Sru  if obj _MAN_PATH is_empty
2976151497Sru  then
2977151497Sru    eval "${return_ok}";
2978151497Sru  fi;
2979151497Sru  # twice test both sys and lang
2980151497Sru  eval set x "$(path_split "${_MAN_PATH}")";
2981151497Sru  shift;
2982151497Sru  exit_test;
2983151497Sru  mals_mp='';
2984151497Sru  for p
2985151497Sru  do				# loop on man path directories
2986151497Sru    mals_mp="$(_manpath_add_lang_sys_single "${mals_mp}" "$p")";
2987151497Sru    exit_test;
2988151497Sru  done;
2989151497Sru  eval set x "$(path_split "${mals_mp}")";
2990151497Sru  shift;
2991151497Sru  exit_test;
2992151497Sru  for p
2993151497Sru  do				# loop on man path directories
2994151497Sru    mals_mp="$(_manpath_add_lang_sys_single "${mals_mp}" "$p")";
2995151497Sru    exit_test;
2996151497Sru  done;
2997151497Sru  _MAN_PATH="$(path_chop "${mals_mp}")";
2998151497Sru  exit_test;
2999151497Sru  eval ${_UNSET} mals_mp;
3000151497Sru  eval "${return_ok}";
3001151497Sru}
3002151497Sru
3003151497Sru
3004151497Sru# To the directory in $1 append existing sys/lang subdirectories
3005151497Sru# Function is necessary to split the OS list.
3006151497Sru#
3007151497Sru# globals: in: $_MAN_SYS, $_MAN_LANG, $_MAN_LANG2
3008151497Sru# argument: 2: `man_path' and `dir'
3009151497Sru# output: colon-separated path of the retrieved subdirectories
3010151497Sru#
3011151497Sru# Variable prefix: _mals
3012151497Sru#
3013151497Sru_manpath_add_lang_sys_single()
3014151497Sru{
3015151497Sru  func_check _manpath_add_lang_sys_single = 2 "$@";
3016151497Sru  _mals_res="$1";
3017151497Sru  _mals_parent="$2";
3018151497Sru  eval set x "$(list_from_split "${_MAN_SYS}" ',')";
3019151497Sru  shift;
3020151497Sru  exit_test;
3021151497Sru  for d in "$@" "${_MAN_LANG}" "${_MAN_LANG2}"
3022151497Sru  do
3023151497Sru    _mals_dir="$(dirname_append "${_mals_parent}" "$d")";
3024151497Sru    exit_test;
3025151497Sru    if obj _mals_res path_not_contains "${_mals_dir}" && \
3026151497Sru       obj _mals_dir is_dir
3027151497Sru    then
3028151497Sru      _mals_res="${_mals_res}:${_mals_dir}";
3029151497Sru    fi;
3030151497Sru  done;
3031151497Sru  if path_not_contains "${_mals_res}" "${_mals_parent}"
3032151497Sru  then
3033151497Sru    _mals_res="${_mals_res}:${_mals_parent}";
3034151497Sru  fi;
3035151497Sru  path_chop "${_mals_res}";
3036151497Sru  eval ${_UNSET} _mals_dir;
3037151497Sru  eval ${_UNSET} _mals_parent;
3038151497Sru  eval ${_UNSET} _mals_res;
3039151497Sru  eval "${return_ok}";
3040151497Sru}
3041151497Sru
3042151497Sru# end manpath_add_lang_sys ()
3043151497Sru
3044151497Sru
3045151497Sru########################################################################
3046151497Sru# manpath_set_from_path ()
3047151497Sru#
3048151497Sru# Determine basic search path for man pages from $PATH.
3049151497Sru#
3050151497Sru# Return:    `0' if a valid man path was retrieved.
3051151497Sru# Output:    none
3052151497Sru# Globals:
3053151497Sru#   in:  $PATH
3054151497Sru#   out: $_MAN_PATH
3055151497Sru#
3056151497Sru# Variable prefix: msfp
3057151497Sru#
3058151497Srumanpath_set_from_path()
3059151497Sru{
3060151497Sru  func_check manpath_set_from_path = 0 "$@";
3061151497Sru
3062151497Sru  msfp_manpath='';
3063151497Sru
3064151497Sru  # get a basic man path from $PATH
3065151497Sru  if obj PATH is_not_empty
3066151497Sru  then
3067151497Sru    eval set x "$(path_split "${PATH}")";
3068151497Sru    shift;
3069151497Sru    exit_test;
3070151497Sru    for d
3071151497Sru    do
3072151497Sru      # delete the final `/bin' part
3073151497Sru      msfp_base="$(echo1 "$d" | sed -e 's|//*bin/*$||')";
3074151497Sru      exit_test;
3075151497Sru      for e in /share/man /man
3076151497Sru      do
3077151497Sru        msfp_mandir="${msfp_base}$e";
3078151497Sru        if test -d "${msfp_mandir}" && test -r "${msfp_mandir}"
3079151497Sru        then
3080151497Sru          msfp_manpath="${msfp_manpath}:${msfp_mandir}";
3081151497Sru        fi;
3082151497Sru      done;
3083151497Sru    done;
3084151497Sru  fi;
3085151497Sru
3086151497Sru  # append some default directories
3087151497Sru  for d in /usr/local/share/man /usr/local/man \
3088151497Sru           /usr/share/man /usr/man \
3089151497Sru           /usr/X11R6/man /usr/openwin/man \
3090151497Sru           /opt/share/man /opt/man \
3091151497Sru           /opt/gnome/man /opt/kde/man
3092151497Sru  do
3093151497Sru    msfp_d="$d";
3094151497Sru    if obj msfp_manpath path_not_contains "${msfp_d}" && obj mfsp_d is_dir
3095151497Sru    then
3096151497Sru      msfp_manpath="${msfp_manpath}:${mfsp_d}";
3097151497Sru    fi;
3098151497Sru  done;
3099151497Sru
3100151497Sru  _MAN_PATH="${msfp_manpath}";
3101151497Sru  eval ${_UNSET} msfp_base;
3102151497Sru  eval ${_UNSET} msfp_d;
3103151497Sru  eval ${_UNSET} msfp_mandir;
3104151497Sru  eval ${_UNSET} msfp_manpath;
3105151497Sru  eval "${return_ok}";
3106151497Sru} # manpath_set_from_path()
3107151497Sru
3108151497Sru
3109151497Sru########################################################################
3110151497Srulandmark '9: obj_*()';
3111151497Sru########################################################################
3112151497Sru
3113151497Sru########################################################################
3114151497Sru# obj (<object> <call_name> <arg>...)
3115151497Sru#
3116151497Sru# This works like a method (object function) call for an object.
3117151497Sru# Run "<call_name> $<object> <arg> ...".
3118151497Sru#
3119151497Sru# The first argument represents an object whose data is given as first
3120151497Sru# argument to <call_name>().
3121151497Sru#
3122151497Sru# Argument: >=2
3123151497Sru#           <object>: variable name
3124151497Sru#           <call_name>: a program or function name
3125151497Sru#
3126151497Sru# Variable prefix: o
3127151497Sru#
3128151497Sruobj()
3129151497Sru{
3130151497Sru  func_check obj '>=' 2 "$@";
3131151497Sru  eval o_arg1='"${'$1'}"';
3132151497Sru  if is_empty "$2"
3133151497Sru  then
3134151497Sru    error "obj(): function name is empty."
3135151497Sru  else
3136151497Sru    o_func="$2";
3137151497Sru  fi;
3138151497Sru  shift;
3139151497Sru  shift;
3140151497Sru  eval "${o_func}"' "${o_arg1}" "$@"';
3141151497Sru  n="$?";
3142151497Sru  eval ${_UNSET} o_arg1;
3143151497Sru  eval ${_UNSET} o_func;
3144151497Sru  eval "${return_var} $n";
3145151497Sru} # obj()
3146151497Sru
3147151497Sru
3148151497Sru########################################################################
3149151497Sru# obj_data (<object>)
3150151497Sru#
3151151497Sru# Print the data of <object>, i.e. the content of $<object>.
3152151497Sru# For possible later extensions.
3153151497Sru#
3154151497Sru# Arguments: 1
3155151497Sru#            <object>: a variable name
3156151497Sru# Output:    the data of <object>
3157151497Sru#
3158151497Sru# Variable prefix: od
3159151497Sru#
3160151497Sruobj_data()
3161151497Sru{
3162151497Sru  func_check obj '=' 1 "$@";
3163151497Sru  if is_empty "$1"
3164151497Sru  then
3165151497Sru    error "obj_data(): object name is empty."
3166151497Sru  fi;
3167151497Sru  eval od_res='"${'$1'}"';
3168151497Sru  obj od_res echo1;
3169151497Sru  eval ${_UNSET} od_res;
3170151497Sru  eval "${return_ok}";
3171151497Sru}
3172151497Sru
3173151497Sru
3174151497Sru########################################################################
3175151497Sru# obj_from_output (<object> <call_name> <arg>...)
3176151497Sru#
3177151497Sru# Run '$<object>="$(<call_name> <arg>...)"' to set the result of a
3178151497Sru# function call to a global variable.
3179151497Sru#
3180151497Sru# Arguments: >=2
3181151497Sru#            <object>: a variable name
3182151497Sru#            <call_name>: the name of a function or program
3183151497Sru#            <arg>: optional argument to <call_name>
3184151497Sru# Output:    none
3185151497Sru#
3186151497Sru# Variable prefix: ofo
3187151497Sru#
3188151497Sruobj_from_output()
3189151497Sru{
3190151497Sru  func_check obj_from_output '>=' 2 "$@";
3191151497Sru  if is_empty "$1"
3192151497Sru  then
3193151497Sru    error "res(): variable name is empty.";
3194151497Sru  elif is_empty "$2"
3195151497Sru  then
3196151497Sru    error "res(): function name is empty."
3197151497Sru  else
3198151497Sru    ofo_result_name="$1";
3199151497Sru  fi;
3200151497Sru  shift;
3201151497Sru  eval "${ofo_result_name}"'="$('"$@"')"';
3202151497Sru  exit_test;
3203151497Sru  eval "${return_ok}";
3204151497Sru}
3205151497Sru
3206151497Sru
3207151497Sru########################################################################
3208151497Sru# obj_set (<object> <data>)
3209151497Sru#
3210151497Sru# Set the data of <object>, i.e. call "$<object>=<data>".
3211151497Sru#
3212151497Sru# Arguments: 2
3213151497Sru#            <object>: a variable name
3214151497Sru#            <data>: a string
3215151497Sru# Output::   none
3216151497Sru#
3217151497Sruobj_set()
3218151497Sru{
3219151497Sru  func_check obj_set '=' 2 "$@";
3220151497Sru  if is_empty "$1"
3221151497Sru  then
3222151497Sru    error "obj_set(): object name is empty."
3223151497Sru  fi;
3224151497Sru  eval "$1"='"$2"';
3225151497Sru  eval "${return_ok}";
3226151497Sru}
3227151497Sru
3228151497Sru
3229151497Sru########################################################################
3230151497Sru# path_chop (<path>)
3231151497Sru#
3232151497Sru# Remove unnecessary colons from path.
3233151497Sru#
3234151497Sru# Argument: 1, a colon separated path.
3235151497Sru# Output:   path without leading, double, or trailing colons.
3236151497Sru#
3237151497Srupath_chop()
3238151497Sru{
3239151497Sru  func_check path_chop = 1 "$@";
3240151497Sru
3241151497Sru  # replace multiple colons by a single colon `:'
3242151497Sru  # remove leading and trailing colons
3243151497Sru  echo1 "$1" | sed -e '
3244151497Srus/^:*//
3245151497Srus/:::*/:/g
3246151497Srus/:*$//
3247151497Sru';
3248151497Sru  eval "${return_ok}";
3249151497Sru}
3250151497Sru
3251151497Sru
3252151497Sru########################################################################
3253151497Sru# path_clean (<path>)
3254151497Sru#
3255151497Sru# Remove non-existing directories from a colon-separated list.
3256151497Sru#
3257151497Sru# Argument: 1, a colon separated path.
3258151497Sru# Output:   colon-separated list of existing directories.
3259151497Sru#
3260151497Sru# Variable prefix: pc
3261151497Sru#
3262151497Srupath_clean()
3263151497Sru{
3264151497Sru  func_check path_clean = 1 "$@";
3265151497Sru  if is_not_equal "$#" 1
3266151497Sru  then
3267151497Sru    error 'path_clean() needs 1 argument.';
3268151497Sru  fi;
3269151497Sru  pc_arg="$1";
3270151497Sru  eval set x "$(path_split "${pc_arg}")";
3271151497Sru  exit_test;
3272151497Sru  shift;
3273151497Sru  pc_res="";
3274151497Sru  for i
3275151497Sru  do
3276151497Sru    pc_i="$i";
3277151497Sru    if obj pc_i is_not_empty \
3278151497Sru       && obj pc_res path_not_contains "${pc_i}" \
3279151497Sru       && obj pc_i is_dir
3280151497Sru    then
3281151497Sru      case "${pc_i}" in
3282151497Sru      ?*/)
3283151497Sru        pc_res="${pc_res}$(dirname_chop "${pc_i}")";
3284151497Sru        exit_test;
3285151497Sru        ;;
3286151497Sru      *)
3287151497Sru        pc_res="${pc_res}:${pc_i}";
3288151497Sru        exit_test;
3289151497Sru        ;;
3290151497Sru      esac;
3291151497Sru    fi;
3292151497Sru  done;
3293151497Sru  eval ${_UNSET} pc_arg;
3294151497Sru  eval ${_UNSET} pc_i;
3295151497Sru  eval ${_UNSET} pc_res;
3296151497Sru  if path_chop "${pc_res}"
3297151497Sru  then
3298151497Sru    eval "${return_ok}";
3299151497Sru  else
3300151497Sru    eval "${return_bad}";
3301151497Sru  fi;
3302151497Sru}
3303151497Sru
3304151497Sru
3305151497Sru########################################################################
3306151497Sru# path_contains (<path> <dir>)
3307151497Sru#-
3308151497Sru# Test whether `dir' is contained in `path', a list separated by `:'.
3309151497Sru#
3310151497Sru# Arguments : 2 arguments.
3311151497Sru# Return    : `0' if arg2 is substring of arg1, `1' otherwise.
3312151497Sru#
3313151497Srupath_contains()
3314151497Sru{
3315151497Sru  func_check path_contains = 2 "$@";
3316151497Sru  case ":$1:" in
3317151497Sru    *":$2:"*)
3318151497Sru      eval "${return_yes}";
3319151497Sru      ;;
3320151497Sru    *)
3321151497Sru      eval "${return_no}";
3322151497Sru      ;;
3323151497Sru  esac;
3324151497Sru  eval "${return_ok}";
3325151497Sru}
3326151497Sru
3327151497Sru
3328151497Sru########################################################################
3329151497Sru# path_not_contains (<path> <dir>)
3330151497Sru#
3331151497Sru# Test whether `dir' is not contained in colon separated `path'.
3332151497Sru#
3333151497Sru# Arguments : 2 arguments.
3334151497Sru#
3335151497Srupath_not_contains()
3336151497Sru{
3337151497Sru  func_check path_not_contains = 2 "$@";
3338151497Sru  if path_contains "$1" "$2"
3339151497Sru  then
3340151497Sru    eval "${return_no}";
3341151497Sru  else
3342151497Sru    eval "${return_yes}";
3343151497Sru  fi;
3344151497Sru  eval "${return_ok}";
3345151497Sru}
3346151497Sru
3347151497Sru
3348151497Sru########################################################################
3349151497Sru# path_split (<path>)
3350151497Sru#
3351151497Sru# In `path' escape white space and replace each colon by a space.
3352151497Sru#
3353151497Sru# Arguments: 1: a colon-separated path
3354151497Sru# Output:    the resulting list, process with `eval set'
3355151497Sru#
3356151497Srupath_split()
3357151497Sru{
3358151497Sru  func_check path_split = 1 "$@";
3359151497Sru  list_from_split "$1" ':';
3360151497Sru  eval "${return_ok}";
3361151497Sru}
3362151497Sru
3363151497Sru
3364151497Sru########################################################################
3365151497Srulandmark '10: register_*()';
3366151497Sru########################################################################
3367151497Sru
3368151497Sru########################################################################
3369151497Sru# register_file (<filename>)
3370151497Sru#
3371151497Sru# Write a found file and register the title element.
3372151497Sru#
3373151497Sru# Arguments: 1: a file name
3374151497Sru# Output: none
3375151497Sru#
3376151497Sruregister_file()
3377151497Sru{
3378151497Sru  func_check register_file = 1 "$@";
3379151497Sru  if is_empty "$1"
3380151497Sru  then
3381151497Sru    error 'register_file(): file name is empty';
3382151497Sru  fi;
3383151497Sru  if is_equal "$1" '-'
3384151497Sru  then
3385151497Sru    to_tmp "${_TMP_STDIN}";
3386151497Sru    register_title 'stdin';
3387151497Sru  else
3388151497Sru    to_tmp "$1";
3389151497Sru    register_title "$(base_name "$1")";
3390151497Sru    exit_test;
3391151497Sru  fi;
3392151497Sru  eval "${return_ok}";
3393151497Sru} # register_file()
3394151497Sru
3395151497Sru
3396151497Sru########################################################################
3397151497Sru# register_title (<filespec>)
3398151497Sru#
3399151497Sru# Create title element from <filespec> and append to $_REGISTERED_TITLE
3400151497Sru#
3401151497Sru# Globals: $_REGISTERED_TITLE (rw)
3402151497Sru#
3403151497Sru# Variable prefix: rt
3404151497Sru#
3405151497Sruregister_title()
3406151497Sru{
3407151497Sru  func_check register_title '=' 1 "$@";
3408151497Sru  if is_empty "$1"
3409151497Sru  then
3410151497Sru    eval "${return_ok}";
3411151497Sru  fi;
3412151497Sru
3413151497Sru  case "${_REGISTERED_TITLE}" in
3414151497Sru  *\ *\ *\ *)
3415151497Sru    eval "${return_ok}";
3416151497Sru    ;;
3417151497Sru  esac;
3418151497Sru
3419151497Sru  # remove directory part
3420151497Sru  rt_title="$(base_name "$1")";
3421151497Sru  # replace space characters by `_'
3422151497Sru  rt_title="$(echo1 "${rt_title}" | sed -e 's/[ 	]/_/g')";
3423151497Sru  # remove extension `.bz2'
3424151497Sru  rt_title="$(echo1 "${rt_title}" | sed -e 's/\.bz2$//')";
3425151497Sru  # remove extension `.gz'
3426151497Sru  rt_title="$(echo1 "${rt_title}" | sed -e 's/\.gz$//')";
3427151497Sru  # remove extension `.Z'
3428151497Sru  rt_title="$(echo1 "${rt_title}" | sed -e 's/\.Z$//')";
3429151497Sru  exit_test;
3430151497Sru
3431151497Sru  if obj rt_title is_empty
3432151497Sru  then
3433151497Sru    eval ${_UNSET} rt_title;
3434151497Sru    eval "${return_ok}";
3435151497Sru  fi;
3436151497Sru  if obj _REGISTERED_TITLE is_empty
3437151497Sru  then
3438151497Sru    _REGISTERED_TITLE="${rt_title}";
3439151497Sru  else
3440151497Sru    _REGISTERED_TITLE="${_REGISTERED_TITLE} ${rt_title}";
3441151497Sru  fi;
3442151497Sru  eval ${_UNSET} rt_title;
3443151497Sru  eval "${return_ok}";
3444151497Sru} # register_title()
3445151497Sru
3446151497Sru
3447151497Sru########################################################################
3448151497Sru# reset ()
3449151497Sru#
3450151497Sru# Reset the variables that can be affected by options to their default.
3451151497Sru#
3452151497Sru#
3453151497Sru# Defined in section `Preset' after the rudimentary shell tests.
3454151497Sru
3455151497Sru
3456151497Sru########################################################################
3457151497Sru# rm_file (<file_name>)
3458151497Sru#
3459151497Sru# Remove file if $_DEBUG_KEEP_FILES allows it.
3460151497Sru#
3461151497Sru# Globals: $_DEBUG_KEEP_FILES
3462151497Sru#
3463151497Srurm_file()
3464151497Sru{
3465151497Sru  func_check rm_file '=' 1 "$@";
3466151497Sru  if is_file "$1"
3467151497Sru  then
3468151497Sru    rm -f "$1" >${_NULL_DEV} 2>&1;
3469151497Sru  fi;
3470151497Sru  if is_existing "$1"
3471151497Sru  then
3472151497Sru    eval "${return_bad}";
3473151497Sru  else
3474151497Sru    eval "${return_good}";
3475151497Sru  fi;
3476151497Sru}
3477151497Sru
3478151497Sru
3479151497Sru########################################################################
3480151497Sru# rm_file_with_debug (<file_name>)
3481151497Sru#
3482151497Sru# Remove file if $_DEBUG_KEEP_FILES allows it.
3483151497Sru#
3484151497Sru# Globals: $_DEBUG_KEEP_FILES
3485151497Sru#
3486151497Srurm_file_with_debug()
3487151497Sru{
3488151497Sru  func_check rm_file_with_debug '=' 1 "$@";
3489151497Sru  if obj _DEBUG_KEEP_FILES is_not_yes
3490151497Sru  then
3491151497Sru    if is_file "$1"
3492151497Sru    then
3493151497Sru      rm -f "$1" >${_NULL_DEV} 2>&1;
3494151497Sru    fi;
3495151497Sru  fi;
3496151497Sru  if is_existing "$1"
3497151497Sru  then
3498151497Sru    eval "${return_bad}";
3499151497Sru  else
3500151497Sru    eval "${return_good}";
3501151497Sru  fi;
3502151497Sru}
3503151497Sru
3504151497Sru
3505151497Sru########################################################################
3506151497Sru# rm_tree (<dir_name>)
3507151497Sru#
3508151497Sru# Remove file if $_DEBUG_KEEP_FILES allows it.
3509151497Sru#
3510151497Sru# Globals: $_DEBUG_KEEP_FILES
3511151497Sru#
3512151497Srurm_tree()
3513151497Sru{
3514151497Sru  func_check rm_tree '=' 1 "$@";
3515151497Sru  if is_existing "$1"
3516151497Sru  then
3517151497Sru    rm -f -r "$1" >${_NULL_DEV} 2>&1;
3518151497Sru  fi; 
3519151497Sru  if is_existing "$1"
3520151497Sru  then
3521151497Sru    eval "${return_bad}";
3522151497Sru  else
3523151497Sru    eval "${return_good}";
3524151497Sru  fi;
3525151497Sru}
3526151497Sru
3527151497Sru
3528151497Sru########################################################################
3529151497Sru# save_stdin ()
3530151497Sru#
3531151497Sru# Store standard input to temporary file (with decompression).
3532151497Sru#
3533151497Sru# Variable prefix: ss
3534151497Sru#
3535151497Sruif obj _HAS_COMPRESSION is_yes
3536151497Sruthen
3537151497Sru  save_stdin()
3538151497Sru  {
3539151497Sru    func_check save_stdin '=' 0 "$@";
3540151497Sru    ss_f="${_TMP_DIR}"/INPUT;
3541151497Sru    cat >"${ss_f}";
3542151497Sru    cat_z "${ss_f}" >"${_TMP_STDIN}";
3543151497Sru    rm_file "${ss_f}";
3544151497Sru    eval ${_UNSET} ss_f;
3545151497Sru    eval "${return_ok}";
3546151497Sru  }
3547151497Sruelse
3548151497Sru  save_stdin()
3549151497Sru  {
3550151497Sru    func_check save_stdin = 0 "$@";
3551151497Sru    cat >"${_TMP_STDIN}";
3552151497Sru    eval "${return_ok}";
3553151497Sru  }
3554151497Srufi;
3555151497Sru
3556151497Sru
3557151497Sru########################################################################
3558151497Sru# special_filespec ()
3559151497Sru#
3560151497Sru# Handle special modes like whatis and apropos.
3561151497Sru#
3562151497Sruspecial_filespec()
3563151497Sru{
3564151497Sru  func_check special_setup '=' 0 "$@";
3565151497Sru  if obj _OPT_APROPOS is_yes
3566151497Sru  then
3567151497Sru    if obj _OPT_WHATIS is_yes
3568151497Sru    then
3569151497Sru      error \
3570151497Sru        'special_setup: $_OPT_APROPOS and $_OPT_WHATIS are both "yes"';
3571151497Sru    fi;
3572151497Sru    apropos_filespec;
3573151497Sru    eval "${return_ok}";
3574151497Sru  fi;
3575151497Sru  if obj _OPT_WHATIS is_yes
3576151497Sru  then
3577151497Sru    whatis_filespec;
3578151497Sru  fi;
3579151497Sru  eval "${return_ok}";
3580151497Sru}
3581151497Sru
3582151497Sru
3583151497Sru########################################################################
3584151497Sru# special_setup ()
3585151497Sru#
3586151497Sru# Handle special modes like whatis and apropos.
3587151497Sru#
3588151497Sruspecial_setup()
3589151497Sru{
3590151497Sru  func_check special_setup '=' 0 "$@";
3591151497Sru  if obj _OPT_APROPOS is_yes
3592151497Sru  then
3593151497Sru    if obj _OPT_WHATIS is_yes
3594151497Sru    then
3595151497Sru      error \
3596151497Sru        'special_setup: $_OPT_APROPOS and $_OPT_WHATIS are both "yes"';
3597151497Sru    fi;
3598151497Sru    apropos_setup;
3599151497Sru    eval "${return_ok}";
3600151497Sru  fi;
3601151497Sru  if obj _OPT_WHATIS is_yes
3602151497Sru  then
3603151497Sru    whatis_header;
3604151497Sru  fi;  
3605151497Sru  eval "${return_ok}";
3606151497Sru}
3607151497Sru
3608151497Sru
3609151497Sru########################################################################
3610151497Srulandmark '11: stack_*()';
3611151497Sru########################################################################
3612151497Sru
3613151497Sru########################################################################
3614151497Sru# string_contains (<string> <part>)
3615151497Sru#
3616151497Sru# Test whether `part' is contained in `string'.
3617151497Sru#
3618151497Sru# Arguments : 2 text arguments.
3619151497Sru# Return    : `0' if arg2 is substring of arg1, `1' otherwise.
3620151497Sru#
3621151497Srustring_contains()
3622151497Sru{
3623151497Sru  func_check string_contains '=' 2 "$@";
3624151497Sru  case "$1" in
3625151497Sru    *"$2"*)
3626151497Sru      eval "${return_yes}";
3627151497Sru      ;;
3628151497Sru    *)
3629151497Sru      eval "${return_no}";
3630151497Sru      ;;
3631151497Sru  esac;
3632151497Sru  eval "${return_ok}";
3633151497Sru}
3634151497Sru
3635151497Sru
3636151497Sru########################################################################
3637151497Sru# string_not_contains (<string> <part>)
3638151497Sru#
3639151497Sru# Test whether `part' is not substring of `string'.
3640151497Sru#
3641151497Sru# Arguments : 2 text arguments.
3642151497Sru# Return    : `0' if arg2 is substring of arg1, `1' otherwise.
3643151497Sru#
3644151497Srustring_not_contains()
3645151497Sru{
3646151497Sru  func_check string_not_contains '=' 2 "$@";
3647151497Sru  if string_contains "$1" "$2"
3648151497Sru  then
3649151497Sru    eval "${return_no}";
3650151497Sru  else
3651151497Sru    eval "${return_yes}";
3652151497Sru  fi;
3653151497Sru  eval "${return_ok}";
3654151497Sru}
3655151497Sru
3656151497Sru
3657151497Sru########################################################################
3658151497Srulandmark '12: tmp_*()';
3659151497Sru########################################################################
3660151497Sru
3661151497Sru########################################################################
3662151497Sru# tmp_cat ()
3663151497Sru#
3664151497Sru# output the temporary cat file (the concatenation of all input)
3665151497Sru#
3666151497Srutmp_cat()
3667151497Sru{
3668151497Sru  func_check tmp_cat '=' 0 "$@";
3669151497Sru  cat "${_TMP_CAT}";
3670151497Sru  eval "${return_var}" "$?";
3671151497Sru}
3672151497Sru
3673151497Sru
3674151497Sru########################################################################
3675151497Sru# tmp_create (<suffix>?)
3676151497Sru#
3677151497Sru# Create temporary file.
3678151497Sru#
3679151497Sru# It's safe to use the shell process ID together with a suffix to
3680151497Sru# have multiple temporary files.
3681151497Sru#
3682151497Sru# Globals: $_TMP_DIR
3683151497Sru#
3684151497Sru# Output : name of created file
3685151497Sru#
3686151497Sru# Variable prefix: tc
3687151497Sru#
3688151497Srutmp_create()
3689151497Sru{
3690151497Sru  func_check tmp_create '<=' 1 "$@";
3691151497Sru  # the output file does not have `,' as first character, so these are
3692151497Sru  # different names from the output file.
3693151497Sru  tc_tmp="${_TMP_DIR}/,$1";
3694151497Sru  : >"${tc_tmp}"
3695151497Sru  obj tc_tmp echo1;
3696151497Sru  eval ${_UNSET} tc_tmp;
3697151497Sru  eval "${return_ok}";
3698151497Sru}
3699151497Sru
3700151497Sru
3701151497Sru########################################################################
3702151497Sru# to_tmp (<filename>)
3703151497Sru#
3704151497Sru# print file (decompressed) to the temporary cat file
3705151497Sru#
3706151497Sruto_tmp()
3707151497Sru{
3708151497Sru  func_check to_tmp '=' 1 "$@";
3709151497Sru  if obj _TMP_CAT is_empty
3710151497Sru  then
3711151497Sru    error 'to_tmp_line: $_TMP_CAT is not yet set';
3712151497Sru  fi;
3713151497Sru  if is_file "$1"
3714151497Sru  then
3715151497Sru    if obj _OPT_LOCATION is_yes
3716151497Sru    then
3717151497Sru      echo2 "$1";
3718151497Sru    fi;
3719151497Sru    if obj _OPT_WHATIS is_yes
3720151497Sru    then
3721151497Sru      whatis_filename "$1" >>"${_TMP_CAT}";
3722151497Sru    else
3723151497Sru      cat_z "$1" >>"${_TMP_CAT}";
3724151497Sru    fi;
3725151497Sru  else
3726151497Sru    error "to_tmp(): could not read file \`$1'.";
3727151497Sru  fi;
3728151497Sru  eval "${return_ok}";
3729151497Sru}
3730151497Sru
3731151497Sru
3732151497Sru########################################################################
3733151497Sru# to_tmp_line ([<text>])
3734151497Sru#
3735151497Sru# print line to the temporary cat file
3736151497Sru#
3737151497Sruto_tmp_line()
3738151497Sru{
3739151497Sru  func_check to_tmp '>=' 0 "$@";
3740151497Sru  if obj _TMP_CAT is_empty
3741151497Sru  then
3742151497Sru    error 'to_tmp_line: $_TMP_CAT is not yet set';
3743151497Sru  fi;
3744151497Sru  echo1 "$*" >>"${_TMP_CAT}";
3745151497Sru  eval "${return_ok}";
3746151497Sru}
3747151497Sru
3748151497Sru
3749151497Sru########################################################################
3750151497Sru# trap_set
3751151497Sru#
3752151497Sru# call function on signal 0
3753151497Sru#
3754151497Srutrap_set()
3755151497Sru{
3756151497Sru  func_check trap_set '=' 0 "$@";
3757151497Sru  trap 'clean_up' 0 2>${_NULL_DEV} || :;
3758151497Sru  eval "${return_ok}";
3759151497Sru}
3760151497Sru
3761151497Sru
3762151497Sru########################################################################
3763151497Sru# trap_unset ()
3764151497Sru#
3765151497Sru# disable trap on signal 0.
3766151497Sru#
3767151497Srutrap_unset()
3768151497Sru{
3769151497Sru  func_check trap_unset '=' 0 "$@";
3770151497Sru  trap '' 0 2>${_NULL_DEV} || :;
3771151497Sru  eval "${return_ok}";
3772151497Sru}
3773151497Sru
3774151497Sru
3775151497Sru########################################################################
3776151497Sru# usage ()
3777151497Sru#
3778151497Sru# print usage information to stderr; for groffer option --help.
3779151497Sru#
3780151497Sruusage()
3781151497Sru{
3782151497Sru  func_check usage = 0 "$@";
3783151497Sru  echo;
3784151497Sru  version;
3785151497Sru  echo1 'Usage: groffer [option]... [filespec]...';
3786151497Sru  cat <<EOF
3787151497Sru
3788151497SruDisplay roff files, standard input, and/or Unix manual pages with a X
3789151497SruWindow viewer or in several text modes.  All input is decompressed
3790151497Sruon-the-fly with all formats that gzip can handle.
3791151497Sru
3792151497Sru"filespec" is one of
3793151497Sru  "filename"       name of a readable file
3794151497Sru  "-"              for standard input
3795151497Sru  "man:name.n"     man page "name" in section "n"
3796151497Sru  "man:name"       man page "name" in first section found
3797151497Sru  "name.n"         man page "name" in section "n"
3798151497Sru  "name"           man page "name" in first section found
3799151497Sruand some more (see groffer(1) for details).
3800151497Sru
3801151497Sru-h --help         print this usage message.
3802151497Sru-Q --source       output as roff source.
3803151497Sru-T --device=name  pass to groff using output device "name".
3804151497Sru-v --version      print version information.
3805151497Sru-V                display the groff execution pipe instead of formatting.
3806151497Sru-X                display with "gxditview" using groff -X.
3807151497Sru-Z --ditroff --intermediate-output
3808151497Sru                  generate groff intermediate output without
3809151497Sru                  post-processing and viewing, like groff -Z.
3810151497SruAll other short options are interpreted as "groff" formatting options.
3811151497Sru
3812151497SruThe most important groffer long options are
3813151497Sru
3814151497Sru--apropos=name    start man's "apropos" program for "name".
3815151497Sru--apropos-data=name
3816151497Sru                  "apropos" for "name" in man's data sections 4, 5, 7.
3817151497Sru--apropos-devel=name
3818151497Sru                  "apropos" for "name" in development sections 2, 3, 9.
3819151497Sru--apropos-progs=name
3820151497Sru                  "apropos" for "name" in man's program sections 1, 6, 8.
3821151497Sru--auto            choose mode automatically from the default mode list.
3822151497Sru--default         reset all options to the default value.
3823151497Sru--default-modes=mode1,mode2,...
3824151497Sru                  set sequence of automatically tried modes.
3825151497Sru--dvi             display in a viewer for TeX device independent format.
3826151497Sru--dvi-viewer=prog choose the viewer program for dvi mode.
3827151497Sru--groff           process like groff, disable viewing features.
3828151497Sru--help            display this helping output.
3829151497Sru--html            display in a web browser.
3830151497Sru--html-viewer=program
3831151497Sru                  choose the web browser for html mode.
3832151497Sru--man             check file parameters first whether they are man pages.
3833151497Sru--mode=auto|dvi|groff|html|pdf|ps|source|text|tty|www|x|X
3834151497Sru                  choose display mode.
3835151497Sru--no-man          disable man-page facility.
3836151497Sru--no-special      disable --all, --apropos*, and --whatis
3837151497Sru--pager=program   preset the paging program for tty mode.
3838151497Sru--pdf             display in a PDF viewer.
3839151497Sru--pdf-viewer=prog choose the viewer program for pdf mode.
3840151497Sru--ps              display in a Postscript viewer.
3841151497Sru--ps-viewer=prog  choose the viewer program for ps mode.
3842151497Sru--shell=program   specify a shell under which to run groffer2.sh.
3843151497Sru--text            output in a text device without a pager.
3844151497Sru--tty             display with a pager on text terminal even when in X.
3845151497Sru--tty-viewer=prog select a pager for tty mode; same as --pager.
3846151497Sru--whatis          display the file name and description of man pages
3847151497Sru--www             same as --html.
3848151497Sru--www-viewer=prog same as --html-viewer
3849151497Sru--x --X           display with "gxditview" using an X* device.
3850151497Sru--x-viewer=prog   choose viewer program for x mode (X mode).
3851151497Sru--X-viewer=prog   same as "--xviewer".
3852151497Sru
3853151497SruDevelopment options that are not useful for normal usage:
3854151497Sru--debug, --debug-all, --debug-keep, --debug-lm, --debug-params,
3855151497Sru--debug-shell, --debug-stacks, --debug-tmpdir, --debug-user,
3856151497Sru--do-nothing, --print=text
3857151497Sru
3858151497SruViewer programs for the different modes that run on the terminal:
3859151497Sru--dvi-viewer-tty=prog, --html-viewer-tty=prog, --pdf-viewer-tty=prog,
3860151497Sru--ps-viewer-tty=prog, --tty-viewer-tty, --X-viewer-tty=prog,
3861151497Sru--x-viewer-tty=prog, --www-viewer-tty=prog
3862151497Sru
3863151497SruThe usual X Windows toolkit options transformed into GNU long options:
3864151497Sru--background=color, --bd=size, --bg=color, --bordercolor=color,
3865151497Sru--borderwidth=size, --bw=size, --display=Xdisplay, --fg=color,
3866151497Sru--fn=font, --font=font, --foreground=color, --geometry=geom, --iconic,
3867151497Sru--resolution=dpi, --rv, --title=text, --xrm=resource
3868151497Sru
3869151497SruLong options of GNU "man":
3870151497Sru--all, --ascii, --ditroff, --extension=suffix, --locale=language,
3871151497Sru--local-file=name, --location, --manpath=dir1:dir2:...,
3872151497Sru--sections=s1:s2:..., --systems=s1,s2,..., --where, ...
3873151497Sru
3874151497SruEOF
3875151497Sru  eval "${return_ok}";
3876151497Sru}
3877151497Sru
3878151497Sru
3879151497Sru########################################################################
3880151497Sru# version ()
3881151497Sru#
3882151497Sru# print version information to stderr
3883151497Sru#
3884151497Sruversion()
3885151497Sru{
3886151497Sru  func_check version = 0 "$@";
3887151497Sru  echo1 "groffer ${_PROGRAM_VERSION} of ${_LAST_UPDATE}";
3888151497Sru  # also display groff's version, but not the called subprograms
3889151497Sru  groff -v 2>&1 | sed -e '/^ *$/q' | sed -e '1s/^/is part of /';
3890151497Sru  eval "${return_ok}";
3891151497Sru}
3892151497Sru
3893151497Sru
3894151497Sru########################################################################
3895151497Sru# warning (<string>)
3896151497Sru#
3897151497Sru# Print warning to stderr
3898151497Sru#
3899151497Sruwarning()
3900151497Sru{
3901151497Sru  echo2 "warning: $*";
3902151497Sru}
3903151497Sru
3904151497Sru
3905151497Sru########################################################################
3906151497Sru# whatis_filename (<filename>)
3907151497Sru#
3908151497Sru# Interpret <filename> as a man page and display its `whatis'
3909151497Sru# information as a fragment written in the groff language.
3910151497Sru#
3911151497Sru# Variable prefix: wf
3912151497Sru#
3913151497Sruwhatis_filename()
3914151497Sru{
3915151497Sru  func_check whatis_filename = 1 "$@";
3916151497Sru  wf_arg="$1";
3917151497Sru  if obj wf_arg is_not_file
3918151497Sru  then
3919151497Sru    error "whatis_filename(): argument is not a readable file."
3920151497Sru  fi;
3921151497Sru  wf_dot='^\.'"${_SPACE_SED}"'*';
3922151497Sru  if obj _FILESPEC_ARG is_equal '-'
3923151497Sru  then
3924151497Sru    wf_arg='stdin';
3925151497Sru  fi;
3926151497Sru  cat <<EOF
3927151497Sru\f[CR]${wf_arg}\f[]:
3928151497Sru.br
3929151497SruEOF
3930151497Sru
3931151497Sru  # get the parts of the file name
3932151497Sru  wf_name="$(base_name $1)";
3933151497Sru  wf_section="$(echo1 $1 | sed -n -e '
3934151497Srus|^.*/man\('"${_MAN_AUTO_SEC_CHARS}"'\).*$|\1|p
3935151497Sru')";
3936151497Sru  if obj wf_section is_not_empty
3937151497Sru  then
3938151497Sru    case "${wf_name}" in
3939151497Sru    *.${wf_section}*)
3940151497Sru      s='yes';
3941151497Sru      ;;
3942151497Sru    *)
3943151497Sru      s='';
3944151497Sru      wf_section='';
3945151497Sru      ;;
3946151497Sru    esac
3947151497Sru    if obj s is_yes
3948151497Sru    then
3949151497Sru      wf_name="$(echo1 ${wf_name} | sed -e '
3950151497Srus/^\(.*\)\.'${wf_section}'.*$/\1/
3951151497Sru')";
3952151497Sru    fi;
3953151497Sru  fi;
3954151497Sru
3955151497Sru  # traditional man style; grep the line containing `.TH' macro, if any
3956151497Sru  wf_res="$(cat_z "$1" | sed -e '
3957151497Sru/'"${wf_dot}"'TH /p
3958151497Srud
3959151497Sru')";
3960151497Sru  exit_test;
3961151497Sru  if obj wf_res is_not_empty
3962151497Sru  then				# traditional man style
3963151497Sru    # get the first line after the first `.SH' macro, by
3964151497Sru    # - delete up to first .SH;
3965151497Sru    # - print all lines before the next .SH;
3966151497Sru    # - quit.
3967151497Sru    wf_res="$(cat_z "$1" | sed -n -e '
3968151497Sru1,/'"${wf_dot}"'SH/d
3969151497Sru/'"${wf_dot}"'SH/q
3970151497Srup
3971151497Sru')";
3972151497Sru
3973151497Sru    if obj wf_section is_not_empty
3974151497Sru    then
3975151497Sru      case "${wf_res}" in
3976151497Sru      ${wf_name}${_SPACE_CASE}*-${_SPACE_CASE}*)
3977151497Sru        s='yes';
3978151497Sru        ;;
3979151497Sru      *)
3980151497Sru        s='';
3981151497Sru        ;;
3982151497Sru      esac;
3983151497Sru      if obj s is_yes
3984151497Sru      then
3985151497Sru        wf_res="$(obj wf_res echo1 | sed -e '
3986151497Srus/^'"${wf_name}${_SPACE_SED}"'[^-]*-'"${_SPACE_SED}"'*\(.*\)$/'"${wf_name}"' ('"${wf_section}"') \\[em] \1/
3987151497Sru')";
3988151497Sru      fi;
3989151497Sru    fi;
3990151497Sru    obj wf_res echo1;
3991151497Sru    echo;
3992151497Sru    eval ${_UNSET} wf_arg;
3993151497Sru    eval ${_UNSET} wf_dot;
3994151497Sru    eval ${_UNSET} wf_name;
3995151497Sru    eval ${_UNSET} wf_res;
3996151497Sru    eval ${_UNSET} wf_section;
3997151497Sru    eval "${return_ok}";
3998151497Sru  fi;
3999151497Sru
4000151497Sru  # mdoc style (BSD doc); grep the line containing `.Nd' macro, if any
4001151497Sru  wf_res="$(cat_z "$1" | sed -n -e '/'"${wf_dot}"'Nd /s///p')";
4002151497Sru  exit_test;
4003151497Sru  if obj wf_res is_not_empty
4004151497Sru  then				# BSD doc style
4005151497Sru    if obj wf_section is_not_empty
4006151497Sru    then
4007151497Sru      wf_res="$(obj wf_res echo1 | sed -n -e '
4008151497Srus/^\(.*\)$/'"${wf_name}"' ('"${wf_section}"') \\[em] \1/p
4009151497Sru')";
4010151497Sru    fi;
4011151497Sru    obj wf_res echo1;
4012151497Sru    echo;
4013151497Sru    eval ${_UNSET} wf_arg;
4014151497Sru    eval ${_UNSET} wf_dot;
4015151497Sru    eval ${_UNSET} wf_name;
4016151497Sru    eval ${_UNSET} wf_res;
4017151497Sru    eval ${_UNSET} wf_section;
4018151497Sru    eval "${return_ok}";
4019151497Sru  fi;
4020151497Sru  echo1 'is not a man page';
4021151497Sru  echo;
4022151497Sru  eval ${_UNSET} wf_arg;
4023151497Sru  eval ${_UNSET} wf_dot;
4024151497Sru  eval ${_UNSET} wf_name;
4025151497Sru  eval ${_UNSET} wf_res;
4026151497Sru  eval ${_UNSET} wf_section;
4027151497Sru  eval "${return_bad}";
4028151497Sru}
4029151497Sru
4030151497Sru
4031151497Sru########################################################################
4032151497Sru# whatis_filespec ()
4033151497Sru#
4034151497Sru# Print the filespec name as .SH to the temporary cat file.
4035151497Sru#
4036151497Sruwhatis_filespec()
4037151497Sru{
4038151497Sru  func_check whatis_filespec '=' 0 "$@";
4039151497Sru  if obj _OPT_WHATIS is_yes
4040151497Sru  then
4041151497Sru    eval to_tmp_line \
4042151497Sru      "'.SH $(echo1 "${_FILESPEC_ARG}" | sed 's/[^\\]-/\\-/g')'";
4043151497Sru    exit_test;
4044151497Sru  fi;
4045151497Sru  eval "${return_ok}";
4046151497Sru}
4047151497Sru
4048151497Sru
4049151497Sru########################################################################
4050151497Sru# whatis_header ()
4051151497Sru#
4052151497Sru# Print the whatis header to the temporary cat file.
4053151497Sru#
4054151497Sruwhatis_header()
4055151497Sru{
4056151497Sru  func_check whatis_header '=' 0 "$@";
4057151497Sru  if obj _OPT_WHATIS is_yes
4058151497Sru  then
4059151497Sru     to_tmp_line '.TH GROFFER WHATIS';
4060151497Sru  fi;
4061151497Sru  eval "${return_ok}";
4062151497Sru}
4063151497Sru
4064151497Sru
4065151497Sru########################################################################
4066151497Sru# where_is (<program>)
4067151497Sru#
4068151497Sru# Output path of a program if in $PATH.
4069151497Sru#
4070151497Sru# Arguments : >=1 (empty allowed)
4071151497Sru#   more args are ignored, this allows to specify progs with arguments
4072151497Sru# Return    : `0' if arg1 is a program in $PATH, `1' otherwise.
4073151497Sru#
4074151497Sru# Variable prefix: w
4075151497Sru#
4076151497Sruwhere_is()
4077151497Sru{
4078151497Sru  func_check where_is '>=' 1 "$@";
4079151497Sru  w_arg="$1";
4080151497Sru  if obj w_arg is_empty
4081151497Sru  then
4082151497Sru    eval ${_UNSET} w_arg;
4083151497Sru    eval "${return_bad}";
4084151497Sru  fi;
4085151497Sru  case "${w_arg}" in
4086151497Sru    /*)
4087151497Sru      eval ${_UNSET} w_arg;
4088151497Sru      eval ${_UNSET} w_file;
4089151497Sru      if test -f "${w_arg}" && test -x "${w_arg}"
4090151497Sru      then
4091151497Sru        eval "${return_ok}";
4092151497Sru      else
4093151497Sru        eval "${return_bad}";
4094151497Sru      fi;
4095151497Sru      ;;
4096151497Sru  esac;
4097151497Sru  eval set x "$(path_split "${PATH}")";
4098151497Sru  exit_test;
4099151497Sru  shift;
4100151497Sru  for p
4101151497Sru  do
4102151497Sru    case "$p" in
4103151497Sru      */) w_file=${p}${w_arg}; ;;
4104151497Sru      *)  w_file=${p}/${w_arg}; ;;
4105151497Sru    esac;
4106151497Sru    if test -f "${w_file}" && test -x "${w_file}"
4107151497Sru    then
4108151497Sru      obj w_file echo1;
4109151497Sru      eval ${_UNSET} w_arg;
4110151497Sru      eval ${_UNSET} w_file;
4111151497Sru      eval "${return_ok}";
4112151497Sru    fi;
4113151497Sru  done;
4114151497Sru  eval ${_UNSET} w_arg;
4115151497Sru  eval ${_UNSET} w_file;
4116151497Sru  eval "${return_bad}";
4117151497Sru}
4118151497Sru
4119151497Sru
4120151497Sru########################################################################
4121151497Sru#                        main* Functions
4122151497Sru########################################################################
4123151497Sru
4124151497Sru# The main area contains the following parts:
4125151497Sru# - main_init(): initialize temporary files and set exit trap
4126151497Sru# - main_parse_MANOPT(): parse $MANOPT
4127151497Sru# - main_parse_args(): argument parsing
4128151497Sru# - main_set_mode (): determine the display mode
4129151497Sru# - main_do_fileargs(): process filespec arguments
4130151497Sru# - main_set_resources(): setup X resources
4131151497Sru# - main_display(): do the displaying
4132151497Sru# - main(): the main function that calls all main_*()
4133151497Sru
4134151497Sru
4135151497Sru#######################################################################
4136151497Sru# main_init ()
4137151497Sru#
4138151497Sru# set exit trap and create temporary files
4139151497Sru#
4140151497Sru# Globals: $_TMP_DIR, $_TMP_CAT, $_TMP_STDIN
4141151497Sru#
4142151497Sru# Variable prefix: mi
4143151497Sru#
4144151497Srumain_init()
4145151497Sru{
4146151497Sru  func_check main_init = 0 "$@";
4147151497Sru  # call clean_up() on shell termination.
4148151497Sru  trap_set;
4149151497Sru
4150151497Sru  # create temporary directory
4151151497Sru  umask 0022;
4152151497Sru  _TMP_DIR='';
4153151497Sru  for d in "${GROFF_TMPDIR}" "${TMPDIR}" "${TMP}" "${TEMP}" \
4154151497Sru           "${TEMPDIR}" "${HOME}"'/tmp' '/tmp' "${HOME}" '.'
4155151497Sru  do
4156151497Sru    mi_dir="$d";
4157151497Sru    if obj mi_dir is_empty || obj mi_dir is_not_dir || \
4158151497Sru       obj mi_dir is_not_writable
4159151497Sru    then
4160151497Sru      continue;
4161151497Sru    fi;
4162151497Sru
4163151497Sru    case "${mi_dir}" in
4164151497Sru    */)
4165151497Sru      _TMP_DIR="${mi_dir}";
4166151497Sru      ;;
4167151497Sru    *)
4168151497Sru      _TMP_DIR="${mi_dir}"'/';
4169151497Sru      ;;
4170151497Sru    esac;
4171151497Sru    _TMP_DIR="${_TMP_DIR}groffer${_PROCESS_ID}";
4172151497Sru    if obj _TMP_DIR rm_tree
4173151497Sru    then
4174151497Sru      :
4175151497Sru    else
4176151497Sru      mi_tdir_="${_TMP_DIR}"_;
4177151497Sru      mi_n=1;
4178151497Sru      mi_tdir_n="${mi_tdir_}${mi_n}";
4179151497Sru      while obj mi_tdir_n is_existing
4180151497Sru      do
4181151497Sru        if obj mi_tdir_n rm_tree
4182151497Sru        then
4183151497Sru          # directory could not be removed
4184151497Sru          mi_n="$(expr "${mi_n}" + 1)";
4185151497Sru          mi_tdir_n="${mi_tdir_}${mi_n}";
4186151497Sru          continue;
4187151497Sru        fi;
4188151497Sru      done;
4189151497Sru      _TMP_DIR="${mi_tdir_n}";
4190151497Sru    fi;
4191151497Sru    eval mkdir "${_TMP_DIR}";
4192151497Sru    if is_not_equal "$?" 0
4193151497Sru    then
4194151497Sru      obj _TMP_DIR rm_tree;
4195151497Sru      _TMP_DIR='';
4196151497Sru      continue;
4197151497Sru    fi;
4198151497Sru    if obj _TMP_DIR is_dir && obj _TMP_DIR is_writable
4199151497Sru    then
4200151497Sru      # $_TMP_DIR can now be used as temporary directory
4201151497Sru      break;
4202151497Sru    fi;
4203151497Sru    obj _TMP_DIR rm_tree;
4204151497Sru    _TMP_DIR='';
4205151497Sru    continue;
4206151497Sru  done;
4207151497Sru  if obj _TMP_DIR is_empty
4208151497Sru  then
4209151497Sru    error "main_init: \
4210151497SruCouldn't create a directory for storing temporary files.";
4211151497Sru  fi;
4212151497Sru  if obj _DEBUG_PRINT_TMPDIR is_yes
4213151497Sru  then
4214151497Sru    echo2 "temporary directory: ${_TMP_DIR}";
4215151497Sru  fi;
4216151497Sru
4217151497Sru  _TMP_CAT="$(tmp_create groffer_cat)";
4218151497Sru  _TMP_STDIN="$(tmp_create groffer_input)";
4219151497Sru  exit_test;
4220151497Sru
4221151497Sru  eval ${_UNSET} mi_dir;
4222151497Sru  eval ${_UNSET} mi_n;
4223151497Sru  eval ${_UNSET} mi_tdir_;
4224151497Sru  eval ${_UNSET} mi_tdir_n;
4225151497Sru  eval "${return_ok}";
4226151497Sru} # main_init()
4227151497Sru
4228151497Sru
4229151497Sru########################################################################
4230151497Sru# main_parse_MANOPT ()
4231151497Sru#
4232151497Sru# Parse $MANOPT to retrieve man options, but only if it is a non-empty
4233151497Sru# string; found man arguments can be overwritten by the command line.
4234151497Sru#
4235151497Sru# Globals:
4236151497Sru#   in: $MANOPT, $_OPTS_MANOPT_*
4237151497Sru#   out: $_MANOPT_*
4238151497Sru#
4239151497Sru# Variable prefix: mpm
4240151497Sru#
4241151497Srumain_parse_MANOPT()
4242151497Sru{
4243151497Sru  func_check main_parse_MANOPT = 0 "$@";
4244151497Sru
4245151497Sru  if obj MANOPT is_not_empty
4246151497Sru  then
4247151497Sru    # Delete leading and final spaces
4248151497Sru    MANOPT="$(echo1 "${MANOPT}" | sed -e '
4249151497Srus/^'"${_SPACE_SED}"'*//
4250151497Srus/'"${_SPACE_SED}"'*$//
4251151497Sru')";
4252151497Sru  exit_test;
4253151497Sru  fi;
4254151497Sru  if obj MANOPT is_empty
4255151497Sru  then
4256151497Sru    eval "${return_ok}";
4257151497Sru  fi;
4258151497Sru
4259151497Sru  mpm_list='';
4260151497Sru  # add arguments in $MANOPT by mapping them to groffer options
4261151497Sru  eval set x "$(list_from_cmdline _OPTS_MANOPT "${MANOPT}")";
4262151497Sru  exit_test;
4263151497Sru  shift;
4264151497Sru  until test "$#" -le 0 || is_equal "$1" '--'
4265151497Sru  do
4266151497Sru    mpm_opt="$1";
4267151497Sru    shift;
4268151497Sru    case "${mpm_opt}" in
4269151497Sru    -7|--ascii)
4270151497Sru      list_append mpm_list '--ascii';
4271151497Sru      ;;
4272151497Sru    -a|--all)
4273151497Sru      list_append mpm_list '--all';
4274151497Sru      ;;
4275151497Sru    -c|--catman)
4276151497Sru      do_nothing;
4277151497Sru      shift;
4278151497Sru      ;;
4279151497Sru    -d|--debug)
4280151497Sru      do_nothing;
4281151497Sru      ;;
4282151497Sru    -D|--default)
4283151497Sru      # undo all man options so far
4284151497Sru      mpm_list='';
4285151497Sru      ;;
4286151497Sru    -e|--extension)
4287151497Sru      list_append mpm_list '--extension';
4288151497Sru      shift;
4289151497Sru      ;;
4290151497Sru    -f|--whatis)
4291151497Sru      list_append mpm_list '--whatis';
4292151497Sru      shift;
4293151497Sru      ;;
4294151497Sru    -h|--help)
4295151497Sru      do_nothing;
4296151497Sru      shift;
4297151497Sru      ;;
4298151497Sru    -k|--apropos)
4299151497Sru      # groffer's --apropos takes an argument, but man's does not, so
4300151497Sru      do_nothing;
4301151497Sru      ;;
4302151497Sru    -l|--local-file)
4303151497Sru      do_nothing;
4304151497Sru      ;;
4305151497Sru    -L|--locale)
4306151497Sru      list_append mpm_list '--locale' "$1";
4307151497Sru      shift;
4308151497Sru      ;;
4309151497Sru    -m|--systems)
4310151497Sru      list_append mpm_list '--systems' "$1";
4311151497Sru      shift;
4312151497Sru      ;;
4313151497Sru    -M|--manpath)
4314151497Sru      list_append mpm_list '--manpath' "$1";
4315151497Sru      shift;
4316151497Sru      ;;
4317151497Sru    -p|--preprocessor)
4318151497Sru      do_nothing;
4319151497Sru      shift;
4320151497Sru      ;;
4321151497Sru    -P|--pager)
4322151497Sru      list_append mpm_list '--pager' "$1";
4323151497Sru      shift;
4324151497Sru      ;;
4325151497Sru    -r|--prompt)
4326151497Sru      do_nothing;
4327151497Sru      shift;
4328151497Sru      ;;
4329151497Sru    -S|--sections)
4330151497Sru      list_append mpm_list '--sections' "$1";
4331151497Sru      shift;
4332151497Sru      ;;
4333151497Sru    -t|--troff)
4334151497Sru      do_nothing;
4335151497Sru      shift;
4336151497Sru      ;;
4337151497Sru    -T|--device)
4338151497Sru      list_append mpm_list '-T' "$1";
4339151497Sru      shift;
4340151497Sru      ;;
4341151497Sru    -u|--update)
4342151497Sru      do_nothing;
4343151497Sru      shift;
4344151497Sru      ;;
4345151497Sru    -V|--version)
4346151497Sru      do_nothing;
4347151497Sru      ;;
4348151497Sru    -w|--where|--location)
4349151497Sru      list_append mpm_list '--location';
4350151497Sru      ;;
4351151497Sru    -Z|--ditroff)
4352151497Sru      do_nothing;
4353151497Sru      ;;
4354151497Sru    # ignore all other options
4355151497Sru    esac;
4356151497Sru  done;
4357151497Sru
4358151497Sru  # prepend $mpm_list to the command line
4359151497Sru  if obj mpm_list is_not_empty
4360151497Sru  then
4361151497Sru    eval set x "${mpm_list}" '"$@"';
4362151497Sru    shift;
4363151497Sru  fi;
4364151497Sru
4365151497Sru  eval ${_UNSET} mpm_list;
4366151497Sru  eval ${_UNSET} mpm_opt;
4367151497Sru  eval "${return_ok}";
4368151497Sru} # main_parse_MANOPT()
4369151497Sru
4370151497Sru
4371151497Sru########################################################################
4372151497Sru# main_parse_args (<command_line_args>*)
4373151497Sru#
4374151497Sru# Parse arguments; process options and filespec parameters
4375151497Sru#
4376151497Sru# Arguments: pass the command line arguments unaltered.
4377151497Sru# Globals:
4378151497Sru#   in:  $_OPTS_*
4379151497Sru#   out: $_OPT_*, $_ADDOPTS, $_FILEARGS
4380151497Sru#
4381151497Sru# Variable prefix: mpa
4382151497Sru#
4383151497Srumain_parse_args()
4384151497Sru{
4385151497Sru  func_check main_parse_args '>=' 0 "$@";
4386151497Sru  _ALL_PARAMS="$(list_from_cmdline _OPTS_CMDLINE "$@")";
4387151497Sru  exit_test;
4388151497Sru  if obj _DEBUG_PRINT_PARAMS is_yes
4389151497Sru  then
4390151497Sru    echo2 "parameters: ${_ALL_PARAMS}";
4391151497Sru  fi;
4392151497Sru  eval set x "${_ALL_PARAMS}";
4393151497Sru  shift;
4394151497Sru
4395151497Sru  # By the call of `eval', unnecessary quoting was removed.  So the
4396151497Sru  # positional shell parameters ($1, $2, ...) are now guaranteed to
4397151497Sru  # represent an option or an argument to the previous option, if any;
4398151497Sru  # then a `--' argument for separating options and
4399151497Sru  # parameters; followed by the filespec parameters if any.
4400151497Sru
4401151497Sru  # Note, the existence of arguments to options has already been checked.
4402151497Sru  # So a check for `$#' or `--' should not be done for arguments.
4403151497Sru
4404151497Sru  until test "$#" -le 0 || is_equal "$1" '--'
4405151497Sru  do
4406151497Sru    mpa_opt="$1";		# $mpa_opt is fed into the option handler
4407151497Sru    shift;
4408151497Sru    case "${mpa_opt}" in
4409151497Sru    -h|--help)
4410151497Sru      usage;
4411151497Sru      leave;
4412151497Sru      ;;
4413151497Sru    -Q|--source)		# output source code (`Quellcode').
4414151497Sru      _OPT_MODE='source';
4415151497Sru      ;;
4416151497Sru    -T|--device|--troff-device) # device; arg
4417151497Sru      _OPT_DEVICE="$1";
4418151497Sru      _check_device_with_mode;
4419151497Sru      shift;
4420151497Sru      ;;
4421151497Sru    -v|--version)
4422151497Sru      version;
4423151497Sru      leave;
4424151497Sru      ;;
4425151497Sru    -V)
4426151497Sru      _OPT_V='yes';
4427151497Sru      ;;
4428151497Sru    -Z|--ditroff|--intermediate-output) # groff intermediate output
4429151497Sru      _OPT_Z='yes';
4430151497Sru      ;;
4431151497Sru    -X)
4432151497Sru      if is_X
4433151497Sru      then
4434151497Sru        _OPT_MODE=X;
4435151497Sru      fi;
4436151497Sru      ;;
4437151497Sru    -?)
4438151497Sru      # delete leading `-'
4439151497Sru      mpa_optchar="$(echo1 "${mpa_opt}" | sed -e 's/^-//')";
4440151497Sru      exit_test;
4441151497Sru      if list_has _OPTS_GROFF_SHORT_NA "${mpa_optchar}"
4442151497Sru      then
4443151497Sru        list_append _ADDOPTS_GROFF "${mpa_opt}";
4444151497Sru      elif list_has _OPTS_GROFF_SHORT_ARG "${mpa_optchar}"
4445151497Sru      then
4446151497Sru        list_append _ADDOPTS_GROFF "${mpa_opt}" "$1";
4447151497Sru        shift;
4448151497Sru      else
4449151497Sru        error "main_parse_args(): Unknown option : \`$1'";
4450151497Sru      fi;
4451151497Sru      ;;
4452151497Sru    --all)
4453151497Sru        _OPT_ALL='yes';
4454151497Sru        ;;
4455151497Sru    --apropos)			# run `apropos'
4456151497Sru      _OPT_APROPOS='yes';
4457151497Sru      _APROPOS_SECTIONS='';
4458151497Sru      _OPT_WHATIS='no';
4459151497Sru      ;;
4460151497Sru    --apropos-data)		# run `apropos' for data sections
4461151497Sru      _OPT_APROPOS='yes';
4462151497Sru      _APROPOS_SECTIONS='457';
4463151497Sru      _OPT_WHATIS='no';
4464151497Sru      ;;
4465151497Sru    --apropos-devel)		# run `apropos' for development sections
4466151497Sru      _OPT_APROPOS='yes';
4467151497Sru      _APROPOS_SECTIONS='239';
4468151497Sru      _OPT_WHATIS='no';
4469151497Sru      ;;
4470151497Sru    --apropos-progs)		# run `apropos' for program sections
4471151497Sru      _OPT_APROPOS='yes';
4472151497Sru      _APROPOS_SECTIONS='168';
4473151497Sru      _OPT_WHATIS='no';
4474151497Sru      ;;
4475151497Sru    --ascii)
4476151497Sru      list_append _ADDOPTS_GROFF '-mtty-char';
4477151497Sru      if obj _OPT_MODE is_empty
4478151497Sru      then
4479151497Sru        _OPT_MODE='text';
4480151497Sru      fi;
4481151497Sru      ;;
4482151497Sru    --auto)			# the default automatic mode
4483151497Sru      _OPT_MODE='';
4484151497Sru      ;;
4485151497Sru    --bd)			# border color for viewers, arg;
4486151497Sru      _OPT_BD="$1";
4487151497Sru      shift;
4488151497Sru      ;;
4489151497Sru    --bg|--backgroud)		# background color for viewers, arg;
4490151497Sru      _OPT_BG="$1";
4491151497Sru      shift;
4492151497Sru      ;;
4493151497Sru    --bw)			# border width for viewers, arg;
4494151497Sru      _OPT_BW="$1";
4495151497Sru      shift;
4496151497Sru      ;;
4497151497Sru    --debug|--debug-all|--debug-keep|--debug-lm|--debug-params|\
4498151497Sru--debug-shell|--debug-stacks|--debug-tmpdir|--debug-user)
4499151497Sru      # debug is handled at the beginning
4500151497Sru      :;
4501151497Sru      ;;
4502151497Sru    --default)			# reset variables to default
4503151497Sru      reset;
4504151497Sru      ;;
4505151497Sru    --default-modes)		# sequence of modes in auto mode; arg
4506151497Sru      _OPT_DEFAULT_MODES="$1";
4507151497Sru      shift;
4508151497Sru      ;;
4509151497Sru    --display)			# set X display, arg
4510151497Sru      _OPT_DISPLAY="$1";
4511151497Sru      shift;
4512151497Sru      ;;
4513151497Sru    --do-nothing)
4514151497Sru      _OPT_DO_NOTHING='yes';
4515151497Sru      ;;
4516151497Sru    --dvi)
4517151497Sru      if is_X
4518151497Sru      then
4519151497Sru        _OPT_MODE='dvi';
4520151497Sru      fi;
4521151497Sru      ;;
4522151497Sru    --dvi-viewer)		# viewer program for dvi mode; arg
4523151497Sru      _VIEWER_TERMINAL='no';
4524151497Sru      _OPT_VIEWER_DVI="$1";
4525151497Sru      shift;
4526151497Sru      ;;
4527151497Sru    --dvi-viewer-tty)		# viewer program for dvi mode in tty; arg
4528151497Sru      _VIEWER_TERMINAL='yes';
4529151497Sru      _OPT_VIEWER_DVI="$1";
4530151497Sru      shift;
4531151497Sru      ;;
4532151497Sru    --extension)		# the extension for man pages, arg
4533151497Sru      _OPT_EXTENSION="$1";
4534151497Sru      shift;
4535151497Sru      ;;
4536151497Sru    --fg|--foreground)		# foreground color for viewers, arg;
4537151497Sru      _OPT_FG="$1";
4538151497Sru      shift;
4539151497Sru      ;;
4540151497Sru    --fn|--font)		# set font for viewers, arg;
4541151497Sru      _OPT_FN="$1";
4542151497Sru      shift;
4543151497Sru      ;;
4544151497Sru    --geometry)			# window geometry for viewers, arg;
4545151497Sru      _OPT_GEOMETRY="$1";
4546151497Sru      shift;
4547151497Sru      ;;
4548151497Sru    --groff)
4549151497Sru      _OPT_MODE='groff';
4550151497Sru      ;;
4551151497Sru    --html|--www)		# display with web browser
4552151497Sru      _OPT_MODE=html;
4553151497Sru      ;;
4554151497Sru    --html-viewer|--www-viewer) # viewer program for html mode; arg
4555151497Sru      _VIEWER_TERMINAL='no';
4556151497Sru      _OPT_VIEWER_HTML="$1";
4557151497Sru      shift;
4558151497Sru      ;;
4559151497Sru    --html-viewer-tty|--www-viewer-tty) # viewer for html mode in tty; arg
4560151497Sru      _VIEWER_TERMINAL='yes';
4561151497Sru      _OPT_VIEWER_HTML="$1";
4562151497Sru      shift;
4563151497Sru      ;;
4564151497Sru    --iconic)			# start viewers as icons
4565151497Sru      _OPT_ICONIC='yes';
4566151497Sru      ;;
4567151497Sru    --locale)			# set language for man pages, arg
4568151497Sru      # argument is xx[_territory[.codeset[@modifier]]] (ISO 639,...)
4569151497Sru      _OPT_LANG="$1";
4570151497Sru      shift;
4571151497Sru      ;;
4572151497Sru    --local-file)		# force local files; same as `--no-man'
4573151497Sru      _MAN_FORCE='no';
4574151497Sru      _MAN_ENABLE='no';
4575151497Sru      ;;
4576151497Sru    --location|--where)		# print file locations to stderr
4577151497Sru      _OPT_LOCATION='yes';
4578151497Sru      ;;
4579151497Sru    --man)		       # force all file params to be man pages
4580151497Sru      _MAN_ENABLE='yes';
4581151497Sru      _MAN_FORCE='yes';
4582151497Sru      ;;
4583151497Sru    --manpath)		      # specify search path for man pages, arg
4584151497Sru      # arg is colon-separated list of directories
4585151497Sru      _OPT_MANPATH="$1";
4586151497Sru      shift;
4587151497Sru      ;;
4588151497Sru    --mode)			# display mode
4589151497Sru      mpa_arg="$1";
4590151497Sru      shift;
4591151497Sru      case "${mpa_arg}" in
4592151497Sru      auto|'')		     # search mode automatically among default
4593151497Sru        _OPT_MODE='';
4594151497Sru        ;;
4595151497Sru      groff)			# pass input to plain groff
4596151497Sru        _OPT_MODE='groff';
4597151497Sru        ;;
4598151497Sru      html|www)			# display with a web browser
4599151497Sru        _OPT_MODE='html';
4600151497Sru        ;;
4601151497Sru      dvi)			# display with xdvi viewer
4602151497Sru        if is_X
4603151497Sru        then
4604151497Sru          _OPT_MODE='dvi';
4605151497Sru        fi;
4606151497Sru        ;;
4607151497Sru      pdf)			# display with PDF viewer
4608151497Sru        if is_X
4609151497Sru        then
4610151497Sru          _OPT_MODE='pdf';
4611151497Sru        fi;
4612151497Sru        ;;
4613151497Sru      ps)			# display with Postscript viewer
4614151497Sru        if is_X
4615151497Sru        then
4616151497Sru          _OPT_MODE='ps';
4617151497Sru        fi;
4618151497Sru        ;;
4619151497Sru      text)			# output on terminal
4620151497Sru        _OPT_MODE='text';
4621151497Sru        ;;
4622151497Sru      tty)			# output on terminal
4623151497Sru        _OPT_MODE='tty';
4624151497Sru        ;;
4625151497Sru      X|x)			# output on X roff viewer
4626151497Sru        if is_X
4627151497Sru        then
4628151497Sru          _OPT_MODE='x';
4629151497Sru        fi;
4630151497Sru        ;;
4631151497Sru      Q|source)			# display source code
4632151497Sru        _OPT_MODE="source";
4633151497Sru        ;;
4634151497Sru      *)
4635151497Sru        error "main_parse_args(): unknown mode ${mpa_arg}";
4636151497Sru        ;;
4637151497Sru      esac;
4638151497Sru      ;;
4639151497Sru    --no-location)		# disable former call to `--location'
4640151497Sru      _OPT_LOCATION='yes';
4641151497Sru      ;;
4642151497Sru    --no-man)			# disable search for man pages
4643151497Sru      # the same as --local-file
4644151497Sru      _MAN_FORCE='no';
4645151497Sru      _MAN_ENABLE='no';
4646151497Sru      ;;
4647151497Sru    --no-special)		# disable some special former calls
4648151497Sru      _OPT_ALL='no'
4649151497Sru      _OPT_APROPOS='no'
4650151497Sru      _OPT_WHATIS='no'
4651151497Sru      ;;
4652151497Sru    --pager|--tty-viewer|--tty-viewer-tty)
4653151497Sru      # set paging program for tty mode, arg
4654151497Sru      _VIEWER_TERMINAL='yes';
4655151497Sru      _OPT_PAGER="$1";
4656151497Sru      shift;
4657151497Sru      ;;
4658151497Sru    --pdf)
4659151497Sru      if is_X
4660151497Sru      then
4661151497Sru        _OPT_MODE='pdf';
4662151497Sru      fi;
4663151497Sru      ;;
4664151497Sru    --pdf-viewer)		# viewer program for ps mode; arg
4665151497Sru      _VIEWER_TERMINAL='no';
4666151497Sru      _OPT_VIEWER_PDF="$1";
4667151497Sru      shift;
4668151497Sru      ;;
4669151497Sru    --pdf-viewer-tty)		# viewer program for ps mode in tty; arg
4670151497Sru      _VIEWER_TERMINAL='yes';
4671151497Sru      _OPT_VIEWER_PDF="$1";
4672151497Sru      shift;
4673151497Sru      ;;
4674151497Sru    --print)			# for argument test
4675151497Sru      echo2 "$1";
4676151497Sru      shift;
4677151497Sru      ;;
4678151497Sru    --ps)
4679151497Sru      if is_X
4680151497Sru      then
4681151497Sru        _OPT_MODE='ps';
4682151497Sru      fi;
4683151497Sru      ;;
4684151497Sru    --ps-viewer)		# viewer program for ps mode; arg
4685151497Sru      _VIEWER_TERMINAL='no';
4686151497Sru      _OPT_VIEWER_PS="$1";
4687151497Sru      shift;
4688151497Sru      ;;
4689151497Sru    --ps-viewer-tty)		# viewer program for ps mode in tty; arg
4690151497Sru      _VIEWER_TERMINAL='yes';
4691151497Sru      _OPT_VIEWER_PS="$1";
4692151497Sru      shift;
4693151497Sru      ;;
4694151497Sru    --resolution)		# set resolution for X devices, arg
4695151497Sru      mpa_arg="$1";
4696151497Sru      shift;
4697151497Sru      case "${mpa_arg}" in
4698151497Sru      75|75dpi)
4699151497Sru        mpa_dpi=75;
4700151497Sru        ;;
4701151497Sru      100|100dpi)
4702151497Sru        mpa_dpi=100;
4703151497Sru        ;;
4704151497Sru      *)
4705151497Sru        error "main_parse_args(): \
4706151497Sruonly resoutions of 75 or 100 dpi are supported";
4707151497Sru        ;;
4708151497Sru      esac;
4709151497Sru      _OPT_RESOLUTION="${mpa_dpi}";
4710151497Sru      ;;
4711151497Sru    --rv)
4712151497Sru      _OPT_RV='yes';
4713151497Sru      ;;
4714151497Sru    --sections)			# specify sections for man pages, arg
4715151497Sru      # arg is colon-separated list of section names
4716151497Sru      _OPT_SECTIONS="$1";
4717151497Sru      shift;
4718151497Sru      ;;
4719151497Sru    --shell)
4720151497Sru      # already done during the first run; so ignore the argument
4721151497Sru      shift;
4722151497Sru      ;;
4723151497Sru    --systems)			# man pages for different OS's, arg
4724151497Sru      # argument is a comma-separated list
4725151497Sru      _OPT_SYSTEMS="$1";
4726151497Sru      shift;
4727151497Sru      ;;
4728151497Sru    --text)			# text mode without pager
4729151497Sru      _OPT_MODE=text;
4730151497Sru      ;;
4731151497Sru    --title)			# title for X viewers; arg
4732151497Sru      _OPT_TITLE="$1";
4733151497Sru      shift;
4734151497Sru      ;;
4735151497Sru    --tty)			# tty mode, text with pager
4736151497Sru      _OPT_MODE=tty;
4737151497Sru      ;;
4738151497Sru    --text-device|--tty-device) # device for tty mode; arg
4739151497Sru      _OPT_TEXT_DEVICE="$1";
4740151497Sru      shift;
4741151497Sru      ;;
4742151497Sru    --whatis)
4743151497Sru      _OPT_WHATIS='yes';
4744151497Sru      _OPT_ALL='yes';
4745151497Sru      _OPT_APROPOS='no';
4746151497Sru      ;;
4747151497Sru    --X|--x)
4748151497Sru      if is_X
4749151497Sru      then
4750151497Sru        _OPT_MODE=x;
4751151497Sru      fi;
4752151497Sru      ;;
4753151497Sru    --xrm)			# pass X resource string, arg;
4754151497Sru      list_append _OPT_XRM "$1";
4755151497Sru      shift;
4756151497Sru      ;;
4757151497Sru    --x-viewer|--X-viewer)	# viewer program for x mode; arg
4758151497Sru      _VIEWER_TERMINAL='no';
4759151497Sru      _OPT_VIEWER_X="$1";
4760151497Sru      shift;
4761151497Sru      ;;
4762151497Sru    --x-viewer-tty|--X-viewer-tty) # viewer program for x mode in tty; arg
4763151497Sru      _VIEWER_TERMINAL='yes';
4764151497Sru      _OPT_VIEWER_X="$1";
4765151497Sru      shift;
4766151497Sru      ;;
4767151497Sru    *)
4768151497Sru      error 'main_parse_args(): error on argument parsing : '"\`$*'";
4769151497Sru      ;;
4770151497Sru    esac;
4771151497Sru  done;
4772151497Sru  shift;			# remove `--' argument
4773151497Sru
4774151497Sru  if obj _OPT_DO_NOTHING is_yes
4775151497Sru  then
4776151497Sru    leave;
4777151497Sru  fi;
4778151497Sru
4779151497Sru  # Remaining arguments are file names (filespecs).
4780151497Sru  # Save them to list $_FILEARGS
4781151497Sru  if is_equal "$#" 0
4782151497Sru  then				# use "-" for standard input
4783151497Sru    set x '-';
4784151497Sru    shift;
4785151497Sru  fi;
4786151497Sru  _FILEARGS='';
4787151497Sru  list_append _FILEARGS "$@";
4788151497Sru  if list_has _FILEARGS '-'
4789151497Sru  then
4790151497Sru    save_stdin;
4791151497Sru  fi;
4792151497Sru  # $_FILEARGS must be retrieved with `eval set x "$_FILEARGS"; shift;'
4793151497Sru  eval ${_UNSET} mpa_arg;
4794151497Sru  eval ${_UNSET} mpa_dpi;
4795151497Sru  eval ${_UNSET} mpa_opt;
4796151497Sru  eval ${_UNSET} mpa_optchar;
4797151497Sru  eval "${return_ok}";
4798151497Sru} # main_parse_args()
4799151497Sru
4800151497Sru
4801151497Sru# Called from main_parse_args() because double `case' is not possible.
4802151497Sru# Globals: $_OPT_DEVICE, $_OPT_MODE
4803151497Sru_check_device_with_mode()
4804151497Sru{
4805151497Sru  func_check _check_device_with_mode = 0 "$@";
4806151497Sru  case "${_OPT_DEVICE}" in
4807151497Sru  dvi)
4808151497Sru    _OPT_MODE=dvi;
4809151497Sru    eval "${return_ok}";
4810151497Sru    ;;
4811151497Sru  html)
4812151497Sru    _OPT_MODE=html;
4813151497Sru    eval "${return_ok}";
4814151497Sru    ;;
4815151497Sru  lbp|lj4)
4816151497Sru    _OPT_MODE=groff;
4817151497Sru    eval "${return_ok}";
4818151497Sru    ;;
4819151497Sru  ps)
4820151497Sru    _OPT_MODE=ps;
4821151497Sru    eval "${return_ok}";
4822151497Sru    ;;
4823151497Sru  ascii|cp1047|latin1|utf8)
4824151497Sru    if obj _OPT_MODE is_not_equal text
4825151497Sru    then
4826151497Sru      _OPT_MODE=tty;		# default text mode
4827151497Sru    fi;
4828151497Sru    eval "${return_ok}";
4829151497Sru    ;;
4830151497Sru  X*)
4831151497Sru    _OPT_MODE=x;
4832151497Sru    eval "${return_ok}";
4833151497Sru    ;;
4834151497Sru  *)				# unknown device, go to groff mode
4835151497Sru    _OPT_MODE=groff;
4836151497Sru    eval "${return_ok}";
4837151497Sru    ;;
4838151497Sru  esac;
4839151497Sru  eval "${return_error}";
4840151497Sru} # _check_device_with_mode() of main_parse_args()
4841151497Sru
4842151497Sru
4843151497Sru########################################################################
4844151497Sru# main_set_mode ()
4845151497Sru#
4846151497Sru# Determine the display mode.
4847151497Sru#
4848151497Sru# Globals:
4849151497Sru#   in:  $DISPLAY, $_OPT_MODE, $_OPT_DEVICE
4850151497Sru#   out: $_DISPLAY_MODE
4851151497Sru#
4852151497Sru# Variable prefix: msm
4853151497Sru#
4854151497Srumain_set_mode()
4855151497Sru{
4856151497Sru  func_check main_set_mode = 0 "$@";
4857151497Sru
4858151497Sru  # set display
4859151497Sru  if obj _OPT_DISPLAY is_not_empty
4860151497Sru  then
4861151497Sru    DISPLAY="${_OPT_DISPLAY}";
4862151497Sru  fi;
4863151497Sru
4864151497Sru  if obj _OPT_V is_yes
4865151497Sru  then
4866151497Sru    list_append _ADDOPTS_GROFF '-V';
4867151497Sru  fi;
4868151497Sru  if obj _OPT_Z is_yes
4869151497Sru  then
4870151497Sru    _DISPLAY_MODE='groff';
4871151497Sru    list_append _ADDOPTS_GROFF '-Z';
4872151497Sru  fi;
4873151497Sru  if obj _OPT_MODE is_equal 'groff'
4874151497Sru  then
4875151497Sru    _DISPLAY_MODE='groff';
4876151497Sru  fi;
4877151497Sru  if obj _DISPLAY_MODE is_equal 'groff'
4878151497Sru  then
4879151497Sru    eval ${_UNSET} msm_modes;
4880151497Sru    eval ${_UNSET} msm_viewer;
4881151497Sru    eval ${_UNSET} msm_viewers;
4882151497Sru    eval "${return_ok}";
4883151497Sru  fi;
4884151497Sru
4885151497Sru  if obj _OPT_MODE is_equal 'source'
4886151497Sru  then
4887151497Sru    _DISPLAY_MODE='source';
4888151497Sru    eval ${_UNSET} msm_modes;
4889151497Sru    eval ${_UNSET} msm_viewer;
4890151497Sru    eval ${_UNSET} msm_viewers;
4891151497Sru    eval "${return_ok}";
4892151497Sru  fi;
4893151497Sru
4894151497Sru  case "${_OPT_MODE}" in
4895151497Sru  '')				# automatic mode
4896151497Sru    case "${_OPT_DEVICE}" in
4897151497Sru    X*)
4898151497Sru     if is_not_X
4899151497Sru      then
4900151497Sru        error_user "no X display found for device ${_OPT_DEVICE}";
4901151497Sru      fi;
4902151497Sru      _DISPLAY_MODE='x';
4903151497Sru      eval ${_UNSET} msm_modes;
4904151497Sru      eval ${_UNSET} msm_viewer;
4905151497Sru      eval ${_UNSET} msm_viewers;
4906151497Sru      eval "${return_ok}";
4907151497Sru      ;;
4908151497Sru    ascii|cp1047|latin1|utf8)
4909151497Sru      if obj _DISPLAY_MODE is_not_equal 'text'
4910151497Sru      then
4911151497Sru        _DISPLAY_MODE='tty';
4912151497Sru      fi;
4913151497Sru      eval ${_UNSET} msm_modes;
4914151497Sru      eval ${_UNSET} msm_viewer;
4915151497Sru      eval ${_UNSET} msm_viewers;
4916151497Sru      eval "${return_ok}";
4917151497Sru      ;;
4918151497Sru    esac;
4919151497Sru    if is_not_X
4920151497Sru    then
4921151497Sru      _DISPLAY_MODE='tty';
4922151497Sru      eval ${_UNSET} msm_modes;
4923151497Sru      eval ${_UNSET} msm_viewer;
4924151497Sru      eval ${_UNSET} msm_viewers;
4925151497Sru      eval "${return_ok}";
4926151497Sru    fi;
4927151497Sru
4928151497Sru    if obj _OPT_DEFAULT_MODES is_empty
4929151497Sru    then
4930151497Sru      msm_modes="${_DEFAULT_MODES}";
4931151497Sru    else
4932151497Sru      msm_modes="${_OPT_DEFAULT_MODES}";
4933151497Sru    fi;
4934151497Sru    ;;
4935151497Sru  text)
4936151497Sru    _DISPLAY_MODE='text';
4937151497Sru    eval ${_UNSET} msm_modes;
4938151497Sru    eval ${_UNSET} msm_viewer;
4939151497Sru    eval ${_UNSET} msm_viewers;
4940151497Sru    eval "${return_ok}";
4941151497Sru    ;;
4942151497Sru  tty)
4943151497Sru    _DISPLAY_MODE='tty';
4944151497Sru    eval ${_UNSET} msm_modes;
4945151497Sru    eval ${_UNSET} msm_viewer;
4946151497Sru    eval ${_UNSET} msm_viewers;
4947151497Sru    eval "${return_ok}";
4948151497Sru    ;;
4949151497Sru  html)
4950151497Sru    _DISPLAY_MODE='html';
4951151497Sru    msm_modes="${_OPT_MODE}";
4952151497Sru    ;;
4953151497Sru  *)				# display mode was given
4954151497Sru    if is_not_X
4955151497Sru    then
4956151497Sru      error_user "You must be in X Window for ${_OPT_MODE} mode.";
4957151497Sru    fi;
4958151497Sru    msm_modes="${_OPT_MODE}";
4959151497Sru    ;;
4960151497Sru  esac;
4961151497Sru
4962151497Sru  # only viewer modes are left
4963151497Sru  eval set x "$(list_from_split "${msm_modes}" ',')";
4964151497Sru  exit_test;
4965151497Sru  shift;
4966151497Sru  while test "$#" -gt 0
4967151497Sru  do
4968151497Sru    m="$1";
4969151497Sru    shift;
4970151497Sru    case "$m" in
4971151497Sru    dvi)
4972151497Sru      if obj _OPT_VIEWER_DVI is_not_empty
4973151497Sru      then
4974151497Sru        msm_viewer="${_OPT_VIEWER_DVI}";
4975151497Sru      else
4976151497Sru        msm_viewer="$(_get_first_prog "$_VIEWER_DVI}")";
4977151497Sru        exit_test;
4978151497Sru      fi;
4979151497Sru      if obj msm_viewer is_empty
4980151497Sru      then
4981151497Sru        error 'No viewer for dvi mode available.';
4982151497Sru      fi;
4983151497Sru      if is_not_equal "$?" 0
4984151497Sru      then
4985151497Sru        continue;
4986151497Sru      fi;
4987151497Sru      _DISPLAY_PROG="${msm_viewer}";
4988151497Sru      _DISPLAY_MODE="dvi";
4989151497Sru      eval ${_UNSET} msm_modes;
4990151497Sru      eval ${_UNSET} msm_viewer;
4991151497Sru      eval ${_UNSET} msm_viewers;
4992151497Sru      eval "${return_ok}";
4993151497Sru      ;;
4994151497Sru    html)
4995151497Sru      if obj _OPT_VIEWER_HTML is_not_empty
4996151497Sru      then
4997151497Sru        msm_viewer="${_OPT_VIEWER_HTML}";
4998151497Sru      else
4999151497Sru        if is_X
5000151497Sru        then
5001151497Sru          msm_viewers="${_VIEWER_HTML_X}";
5002151497Sru        else
5003151497Sru          msm_viewers="${_VIEWER_HTML_TTY}";
5004151497Sru        fi;
5005151497Sru        msm_viewer="$(_get_first_prog "${msm_viewers}")";
5006151497Sru        exit_test;
5007151497Sru      fi;
5008151497Sru      if obj msm_viewer is_empty
5009151497Sru      then
5010151497Sru        error 'No viewer for html mode available.';
5011151497Sru      fi;
5012151497Sru      if is_not_equal "$?" 0
5013151497Sru      then
5014151497Sru        continue;
5015151497Sru      fi;
5016151497Sru      _DISPLAY_PROG="${msm_viewer}";
5017151497Sru      _DISPLAY_MODE=html;
5018151497Sru      eval ${_UNSET} msm_modes;
5019151497Sru      eval ${_UNSET} msm_viewer;
5020151497Sru      eval ${_UNSET} msm_viewers;
5021151497Sru      eval "${return_ok}";
5022151497Sru      ;;
5023151497Sru    pdf)
5024151497Sru      if obj _OPT_VIEWER_PDF is_not_empty
5025151497Sru      then
5026151497Sru        msm_viewer="${_OPT_VIEWER_PDF}";
5027151497Sru      else
5028151497Sru        msm_viewer="$(_get_first_prog "${_VIEWER_PDF}")";
5029151497Sru        exit_test;
5030151497Sru      fi;
5031151497Sru      if obj msm_viewer is_empty
5032151497Sru      then
5033151497Sru        error 'No viewer for pdf mode available.';
5034151497Sru      fi;
5035151497Sru      if is_not_equal "$?" 0
5036151497Sru      then
5037151497Sru        continue;
5038151497Sru      fi;
5039151497Sru      _DISPLAY_PROG="${msm_viewer}";
5040151497Sru      _DISPLAY_MODE="pdf";
5041151497Sru      eval ${_UNSET} msm_modes;
5042151497Sru      eval ${_UNSET} msm_viewer;
5043151497Sru      eval ${_UNSET} msm_viewers;
5044151497Sru      eval "${return_ok}";
5045151497Sru      ;;
5046151497Sru    ps)
5047151497Sru      if obj _OPT_VIEWER_PS is_not_empty
5048151497Sru      then
5049151497Sru        msm_viewer="${_OPT_VIEWER_PS}";
5050151497Sru      else
5051151497Sru        msm_viewer="$(_get_first_prog "${_VIEWER_PS}")";
5052151497Sru        exit_test;
5053151497Sru      fi;
5054151497Sru      if obj msm_viewer is_empty
5055151497Sru      then
5056151497Sru        error 'No viewer for ps mode available.';
5057151497Sru      fi;
5058151497Sru      if is_not_equal "$?" 0
5059151497Sru      then
5060151497Sru        continue;
5061151497Sru      fi;
5062151497Sru      _DISPLAY_PROG="${msm_viewer}";
5063151497Sru      _DISPLAY_MODE="ps";
5064151497Sru     eval ${_UNSET} msm_modes;
5065151497Sru      eval ${_UNSET} msm_viewer;
5066151497Sru      eval ${_UNSET} msm_viewers;
5067151497Sru      eval "${return_ok}";
5068151497Sru      ;;
5069151497Sru    text)
5070151497Sru      _DISPLAY_MODE='text';
5071151497Sru      eval ${_UNSET} msm_modes;
5072151497Sru      eval ${_UNSET} msm_viewer;
5073151497Sru      eval ${_UNSET} msm_viewers;
5074151497Sru      eval "${return_ok}";
5075151497Sru      ;;
5076151497Sru    tty)
5077151497Sru      _DISPLAY_MODE='tty';
5078151497Sru      eval ${_UNSET} msm_modes;
5079151497Sru      eval ${_UNSET} msm_viewer;
5080151497Sru      eval ${_UNSET} msm_viewers;
5081151497Sru      eval "${return_ok}";
5082151497Sru      ;;
5083151497Sru    x)
5084151497Sru      if obj _OPT_VIEWER_X is_not_empty
5085151497Sru      then
5086151497Sru        msm_viewer="${_OPT_VIEWER_X}";
5087151497Sru      else
5088151497Sru        msm_viewer="$(_get_first_prog "${_VIEWER_X}")";
5089151497Sru        exit_test;
5090151497Sru      fi;
5091151497Sru      if obj msm_viewer is_empty
5092151497Sru      then
5093151497Sru        error 'No viewer for x mode available.';
5094151497Sru      fi;
5095151497Sru      if is_not_equal "$?" 0
5096151497Sru      then
5097151497Sru        continue;
5098151497Sru      fi;
5099151497Sru      _DISPLAY_PROG="${msm_viewer}";
5100151497Sru      _DISPLAY_MODE='x';
5101151497Sru      eval ${_UNSET} msm_modes;
5102151497Sru      eval ${_UNSET} msm_viewer;
5103151497Sru      eval ${_UNSET} msm_viewers;
5104151497Sru      eval "${return_ok}";
5105151497Sru      ;;
5106151497Sru    X)
5107151497Sru      _DISPLAY_MODE='X';
5108151497Sru      eval ${_UNSET} msm_modes;
5109151497Sru      eval ${_UNSET} msm_viewer;
5110151497Sru      eval ${_UNSET} msm_viewers;
5111151497Sru      eval "${return_ok}";
5112151497Sru      ;;
5113151497Sru    esac;
5114151497Sru  done;
5115151497Sru  eval ${_UNSET} msm_modes;
5116151497Sru  eval ${_UNSET} msm_viewer;
5117151497Sru  eval ${_UNSET} msm_viewers;
5118151497Sru  error_user "No suitable display mode found.";
5119151497Sru} # main_set_mode()
5120151497Sru
5121151497Sru
5122151497Sru# _get_first_prog (<proglist>)
5123151497Sru#
5124151497Sru# Retrieve first argument that represents an existing program in $PATH.
5125151497Sru# Local function for main_set_mode().
5126151497Sru#
5127151497Sru# Arguments: 1; a comma-separated list of commands (with options),
5128151497Sru#               like $_VIEWER_*.
5129151497Sru#
5130151497Sru# Return  : `1' if none found, `0' if found.
5131151497Sru# Output  : the argument that succeded.
5132151497Sru#
5133151497Sru# Variable prefix: _gfp
5134151497Sru#
5135151497Sru_get_first_prog()
5136151497Sru{
5137151497Sru  if is_equal "$#" 0
5138151497Sru  then
5139151497Sru    error "_get_first_prog() needs 1 argument.";
5140151497Sru  fi;
5141151497Sru  if is_empty "$1"
5142151497Sru  then
5143151497Sru    return "${_BAD}";
5144151497Sru  fi;
5145151497Sru  eval set x "$(list_from_split "$1" ',')";
5146151497Sru  exit_test;
5147151497Sru  shift;
5148151497Sru  for i
5149151497Sru  do
5150151497Sru    _gfp_i="$i";
5151151497Sru    if obj _gfp_i is_empty
5152151497Sru    then
5153151497Sru      continue;
5154151497Sru    fi;
5155151497Sru    if eval is_prog "$(get_first_essential ${_gfp_i})"
5156151497Sru    then
5157151497Sru      exit_test;
5158151497Sru      obj _gfp_i echo1;
5159151497Sru      eval ${_UNSET} _gfp_i;
5160151497Sru      return "${_GOOD}";
5161151497Sru    fi;
5162151497Sru  done;
5163151497Sru  eval ${_UNSET} _gfp_i;
5164151497Sru  return "${_BAD}";
5165151497Sru} # _get_first_prog() of main_set_mode()
5166151497Sru
5167151497Sru
5168151497Sru#######################################################################
5169151497Sru# main_do_fileargs ()
5170151497Sru#
5171151497Sru# Process filespec arguments in $_FILEARGS.
5172151497Sru#
5173151497Sru# Globals:
5174151497Sru#   in: $_FILEARGS (process with `eval set x "$_FILEARGS"; shift;')
5175151497Sru#
5176151497Sru# Variable prefix: mdfa
5177151497Sru#
5178151497Srumain_do_fileargs()
5179151497Sru{
5180151497Sru  func_check main_do_fileargs = 0 "$@";
5181151497Sru  special_setup;
5182151497Sru  eval set x "${_FILEARGS}";
5183151497Sru  shift;
5184151497Sru  eval ${_UNSET} _FILEARGS;
5185151497Sru  # temporary storage of all input to $_TMP_CAT
5186151497Sru  while test "$#" -ge 2
5187151497Sru  do
5188151497Sru    # test for `s name' arguments, with `s' a 1-char standard section
5189151497Sru    mdfa_filespec="$1";
5190151497Sru    _FILESPEC_ARG="$1";
5191151497Sru    shift;
5192151497Sru    case "${mdfa_filespec}" in
5193151497Sru    '')
5194151497Sru      continue;
5195151497Sru      ;;
5196151497Sru    '-')
5197151497Sru      special_filespec;
5198151497Sru      if obj _OPT_APROPOS is_yes
5199151497Sru      then
5200151497Sru        continue;
5201151497Sru      fi;
5202151497Sru      register_file '-'
5203151497Sru      continue;
5204151497Sru      ;;
5205151497Sru    ?)
5206151497Sru      if obj _OPT_APROPOS is_yes
5207151497Sru      then
5208151497Sru        special_filespec;
5209151497Sru        continue;
5210151497Sru      fi;
5211151497Sru      if list_has_not _MAN_AUTO_SEC_LIST "${mdfa_filespec}"
5212151497Sru      then
5213151497Sru        special_filespec;
5214151497Sru        do_filearg "${mdfa_filespec}"
5215151497Sru        continue;
5216151497Sru      fi;
5217151497Sru      mdfa_name="$1";
5218151497Sru      _FILESPEC_ARG="${_FILESPEC_ARG} $1";
5219151497Sru      special_filespec;
5220151497Sru      case "${mdfa_name}" in
5221151497Sru      */*|man:*|*\(*\)|*."${mdfa_filespec}")
5222151497Sru        do_filearg "${mdfa_filespec}"
5223151497Sru        continue;
5224151497Sru        ;;
5225151497Sru      esac;
5226151497Sru      shift;
5227151497Sru      if do_filearg "man:${mdfa_name}(${mdfa_filespec})"
5228151497Sru      then
5229151497Sru        continue;
5230151497Sru      else
5231151497Sru        do_filearg "${mdfa_filespec}"
5232151497Sru        continue;
5233151497Sru      fi;
5234151497Sru      ;;
5235151497Sru    *)
5236151497Sru      special_filespec;
5237151497Sru      if obj _OPT_APROPOS is_yes
5238151497Sru      then
5239151497Sru        continue;
5240151497Sru      fi;
5241151497Sru      do_filearg "${mdfa_filespec}"
5242151497Sru      continue;
5243151497Sru      ;;
5244151497Sru    esac;
5245151497Sru  done;				# end of `s name' test
5246151497Sru  while test "$#" -gt 0
5247151497Sru  do
5248151497Sru    mdfa_filespec="$1";
5249151497Sru    _FILESPEC_ARG="$1";
5250151497Sru    shift;
5251151497Sru    special_filespec;
5252151497Sru    if obj _OPT_APROPOS is_yes
5253151497Sru    then
5254151497Sru      continue;
5255151497Sru    fi;
5256151497Sru    do_filearg "${mdfa_filespec}"
5257151497Sru  done;
5258151497Sru  obj _TMP_STDIN rm_file_with_debug;
5259151497Sru  eval ${_UNSET} mdfa_filespec;
5260151497Sru  eval ${_UNSET} mdfa_name;
5261151497Sru  eval "${return_ok}";
5262151497Sru} # main_do_fileargs()
5263151497Sru
5264151497Sru
5265151497Sru########################################################################
5266151497Sru# main_set_resources ()
5267151497Sru#
5268151497Sru# Determine options for setting X resources with $_DISPLAY_PROG.
5269151497Sru#
5270151497Sru# Globals: $_DISPLAY_PROG, $_OUTPUT_FILE_NAME
5271151497Sru#
5272151497Sru# Variable prefix: msr
5273151497Sru#
5274151497Srumain_set_resources()
5275151497Sru{
5276151497Sru  func_check main_set_resources = 0 "$@";
5277151497Sru  # $msr_prog   viewer program
5278151497Sru  # $msr_rl     resource list
5279151497Sru  msr_title="$(get_first_essential \
5280151497Sru                 "${_OPT_TITLE}" "${_REGISTERED_TITLE}")";
5281151497Sru  exit_test;
5282151497Sru  _OUTPUT_FILE_NAME='';
5283151497Sru  eval set x "${msr_title}";
5284151497Sru  shift;
5285151497Sru  until is_equal "$#" 0
5286151497Sru  do
5287151497Sru    msr_n="$1";
5288151497Sru    case "${msr_n}" in
5289151497Sru    '')
5290151497Sru      continue;
5291151497Sru      ;;
5292151497Sru    ,*)
5293151497Sru      msr_n="$(echo1 "$1" | sed -e 's/^,,*//')";
5294151497Sru      exit_test;
5295151497Sru      ;;
5296151497Sru    esac
5297151497Sru    if obj msr_n is_empty
5298151497Sru    then
5299151497Sru      continue;
5300151497Sru    fi;
5301151497Sru    if obj _OUTPUT_FILE_NAME is_not_empty
5302151497Sru    then
5303151497Sru      _OUTPUT_FILE_NAME="${_OUTPUT_FILE_NAME}"',';
5304151497Sru    fi;
5305151497Sru    _OUTPUT_FILE_NAME="${_OUTPUT_FILE_NAME}${msr_n}";
5306151497Sru    shift;
5307151497Sru  done;
5308151497Sru  case "${_OUTPUT_FILE_NAME}" in
5309151497Sru  '')
5310151497Sru    _OUTPUT_FILE_NAME='-';
5311151497Sru    ;;
5312151497Sru  ,*)
5313151497Sru    error "main_set_resources(): ${_OUTPUT_FILE_NAME} starts with a comma.";
5314151497Sru    ;;
5315151497Sru  esac;
5316151497Sru  _OUTPUT_FILE_NAME="${_TMP_DIR}/${_OUTPUT_FILE_NAME}";
5317151497Sru
5318151497Sru  if obj _DISPLAY_PROG is_empty
5319151497Sru  then				# for example, for groff mode
5320151497Sru    _DISPLAY_ARGS='';
5321151497Sru    eval ${_UNSET} msr_n;
5322151497Sru    eval ${_UNSET} msr_prog;
5323151497Sru    eval ${_UNSET} msr_rl;
5324151497Sru    eval ${_UNSET} msr_title;
5325151497Sru    eval "${return_ok}";
5326151497Sru  fi;
5327151497Sru
5328151497Sru  eval set x "${_DISPLAY_PROG}";
5329151497Sru  shift;
5330151497Sru  msr_prog="$(base_name "$1")";
5331151497Sru  exit_test;
5332151497Sru  shift;
5333151497Sru  if test $# != 0
5334151497Sru  then
5335151497Sru    if obj _DISPLAY_PROG is_empty
5336151497Sru    then
5337151497Sru      _DISPLAY_ARGS="$*";
5338151497Sru    else
5339151497Sru      _DISPLAY_ARGS="$* ${_DISPLAY_ARGS}";
5340151497Sru    fi;
5341151497Sru  fi;
5342151497Sru  msr_rl='';
5343151497Sru  if obj _OPT_BD is_not_empty
5344151497Sru  then
5345151497Sru    case "${msr_prog}" in
5346151497Sru    ghostview|gv|gxditview|xditview|xdvi)
5347151497Sru      list_append msr_rl '-bd' "${_OPT_BD}";
5348151497Sru      ;;
5349151497Sru    esac;
5350151497Sru  fi;
5351151497Sru  if obj _OPT_BG is_not_empty
5352151497Sru  then
5353151497Sru    case "${msr_prog}" in
5354151497Sru    ghostview|gv|gxditview|xditview|xdvi)
5355151497Sru      list_append msr_rl '-bg' "${_OPT_BG}";
5356151497Sru      ;;
5357151497Sru    kghostview)
5358151497Sru      list_append msr_rl '--bg' "${_OPT_BG}";
5359151497Sru      ;;
5360151497Sru    xpdf)
5361151497Sru      list_append msr_rl '-papercolor' "${_OPT_BG}";
5362151497Sru      ;;
5363151497Sru    esac;
5364151497Sru  fi;
5365151497Sru  if obj _OPT_BW is_not_empty
5366151497Sru  then
5367151497Sru    case "${msr_prog}" in
5368151497Sru    ghostview|gv|gxditview|xditview|xdvi)
5369151497Sru      _list_append msr_rl '-bw' "${_OPT_BW}";
5370151497Sru      ;;
5371151497Sru    esac;
5372151497Sru  fi;
5373151497Sru  if obj _OPT_FG is_not_empty
5374151497Sru  then
5375151497Sru    case "${msr_prog}" in
5376151497Sru    ghostview|gv|gxditview|xditview|xdvi)
5377151497Sru      list_append msr_rl '-fg' "${_OPT_FG}";
5378151497Sru      ;;
5379151497Sru    kghostview)
5380151497Sru      list_append msr_rl '--fg' "${_OPT_FG}";
5381151497Sru      ;;
5382151497Sru    esac;
5383151497Sru  fi;
5384151497Sru  if is_not_empty "${_OPT_FN}"
5385151497Sru  then
5386151497Sru    case "${msr_prog}" in
5387151497Sru    ghostview|gv|gxditview|xditview|xdvi)
5388151497Sru      list_append msr_rl '-fn' "${_OPT_FN}";
5389151497Sru      ;;
5390151497Sru    kghostview)
5391151497Sru      list_append msr_rl '--fn' "${_OPT_FN}";
5392151497Sru      ;;
5393151497Sru    esac;
5394151497Sru  fi;
5395151497Sru  if is_not_empty "${_OPT_GEOMETRY}"
5396151497Sru  then
5397151497Sru    case "${msr_prog}" in
5398151497Sru    ghostview|gv|gxditview|xditview|xdvi|xpdf)
5399151497Sru      list_append msr_rl '-geometry' "${_OPT_GEOMETRY}";
5400151497Sru      ;;
5401151497Sru    kghostview)
5402151497Sru      list_append msr_rl '--geometry' "${_OPT_GEOMETRY}";
5403151497Sru      ;;
5404151497Sru    esac;
5405151497Sru  fi;
5406151497Sru  if is_empty "${_OPT_RESOLUTION}"
5407151497Sru  then
5408151497Sru    _OPT_RESOLUTION="${_DEFAULT_RESOLUTION}";
5409151497Sru    case "${msr_prog}" in
5410151497Sru    gxditview|xditview)
5411151497Sru      list_append msr_rl '-resolution' "${_DEFAULT_RESOLUTION}";
5412151497Sru      ;;
5413151497Sru    xpdf)
5414151497Sru      case "${_DEFAULT_RESOLUTION}" in
5415151497Sru      75)
5416151497Sru        # 72dpi is '100'
5417151497Sru        list_append msr_rl '-z' '104';
5418151497Sru        ;;
5419151497Sru      100)
5420151497Sru        list_append msr_rl '-z' '139';
5421151497Sru        ;;
5422151497Sru      esac;
5423151497Sru      ;;
5424151497Sru    esac;
5425151497Sru  else
5426151497Sru    case "${msr_prog}" in
5427151497Sru    ghostview|gv|gxditview|xditview|xdvi)
5428151497Sru      list_append msr_rl '-resolution' "${_OPT_RESOLUTION}";
5429151497Sru      ;;
5430151497Sru    xpdf)
5431151497Sru      case "${_OPT_RESOLUTION}" in
5432151497Sru      75)
5433151497Sru        list_append msr_rl '-z' '104';
5434151497Sru        # '100' corresponds to 72dpi
5435151497Sru        ;;
5436151497Sru      100)
5437151497Sru        list_append msr_rl '-z' '139';
5438151497Sru        ;;
5439151497Sru      esac;
5440151497Sru      ;;
5441151497Sru    esac;
5442151497Sru  fi;
5443151497Sru  if is_yes "${_OPT_ICONIC}"
5444151497Sru  then
5445151497Sru    case "${msr_prog}" in
5446151497Sru    ghostview|gv|gxditview|xditview|xdvi)
5447151497Sru      list_append msr_rl '-iconic';
5448151497Sru      ;;
5449151497Sru    esac;
5450151497Sru  fi;
5451151497Sru  if is_yes "${_OPT_RV}"
5452151497Sru  then
5453151497Sru    case "${msr_prog}" in
5454151497Sru    ghostview|gv|gxditview|xditview|xdvi)
5455151497Sru      list_append msr_rl '-rv';
5456151497Sru      ;;
5457151497Sru    esac;
5458151497Sru  fi;
5459151497Sru  if is_not_empty "${_OPT_XRM}"
5460151497Sru  then
5461151497Sru    case "${msr_prog}" in
5462151497Sru    ghostview|gv|gxditview|xditview|xdvi|xpdf)
5463151497Sru      eval set x "${_OPT_XRM}";
5464151497Sru      shift;
5465151497Sru      for i
5466151497Sru      do
5467151497Sru        list_append msr_rl '-xrm' "$i";
5468151497Sru      done;
5469151497Sru      ;;
5470151497Sru    esac;
5471151497Sru  fi;
5472151497Sru  if is_not_empty "${msr_title}"
5473151497Sru  then
5474151497Sru    case "${msr_prog}" in
5475151497Sru    gxditview|xditview)
5476151497Sru      list_append msr_rl '-title' "${msr_title}";
5477151497Sru      ;;
5478151497Sru    esac;
5479151497Sru  fi;
5480151497Sru  _DISPLAY_ARGS="${msr_rl}";
5481151497Sru  eval ${_UNSET} msr_n;
5482151497Sru  eval ${_UNSET} msr_prog;
5483151497Sru  eval ${_UNSET} msr_rl;
5484151497Sru  eval ${_UNSET} msr_title;
5485151497Sru  eval "${return_ok}";
5486151497Sru} # main_set_resources
5487151497Sru
5488151497Sru
5489151497Sru########################################################################
5490151497Sru# main_display ()
5491151497Sru#
5492151497Sru# Do the actual display of the whole thing.
5493151497Sru#
5494151497Sru# Globals:
5495151497Sru#   in: $_DISPLAY_MODE, $_OPT_DEVICE,
5496151497Sru#       $_ADDOPTS_GROFF, $_ADDOPTS_POST, $_ADDOPTS_X,
5497151497Sru#       $_TMP_CAT, $_OPT_PAGER, $PAGER, $_MANOPT_PAGER,
5498151497Sru#       $_OUTPUT_FILE_NAME
5499151497Sru#
5500151497Sru# Variable prefix: md
5501151497Sru#
5502151497Srumain_display()
5503151497Sru{
5504151497Sru  func_check main_display = 0 "$@";
5505151497Sru
5506151497Sru  export md_addopts;
5507151497Sru  export md_groggy;
5508151497Sru  export md_modefile;
5509151497Sru
5510151497Sru  if obj _TMP_CAT is_non_empty_file
5511151497Sru  then
5512151497Sru    md_modefile="${_OUTPUT_FILE_NAME}";
5513151497Sru  else
5514151497Sru    echo2 'groffer: empty input.';
5515151497Sru    clean_up;
5516151497Sru    eval ${_UNSET} md_modefile;
5517151497Sru    eval "${return_ok}";
5518151497Sru  fi;
5519151497Sru
5520151497Sru  # go to the temporary directory to be able to access internal data files
5521151497Sru  cd "${_TMP_DIR}" >"${_NULL_DEV}" 2>&1;
5522151497Sru
5523151497Sru  case "${_DISPLAY_MODE}" in
5524151497Sru  groff)
5525151497Sru    _ADDOPTS_GROFF="${_ADDOPTS_GROFF} ${_ADDOPTS_POST}";
5526151497Sru    if obj _OPT_DEVICE is_not_empty
5527151497Sru    then
5528151497Sru      _ADDOPTS_GROFF="${_ADDOPTS_GROFF} -T${_OPT_DEVICE}";
5529151497Sru    fi;
5530151497Sru    md_groggy="$(tmp_cat | eval grog "${md_options}")";
5531151497Sru    exit_test;
5532151497Sru    _do_opt_V;
5533151497Sru
5534151497Sru    obj md_modefile rm_file;
5535151497Sru    mv "${_TMP_CAT}" "${md_modefile}";
5536151497Sru    trap_unset;
5537151497Sru    cat "${md_modefile}" | \
5538151497Sru    {
5539151497Sru      trap_set;
5540151497Sru      eval "${md_groggy}" "${_ADDOPTS_GROFF}";
5541151497Sru    } &
5542151497Sru    ;;
5543151497Sru  text|tty)
5544151497Sru    case "${_OPT_DEVICE}" in
5545151497Sru    '')
5546151497Sru      md_device="$(get_first_essential \
5547151497Sru                     "${_OPT_TEXT_DEVICE}" "${_DEFAULT_TTY_DEVICE}")";
5548151497Sru      exit_test;
5549151497Sru      ;;
5550151497Sru    ascii|cp1047|latin1|utf8)
5551151497Sru      md_device="${_OPT_DEVICE}";
5552151497Sru      ;;
5553151497Sru    *)
5554151497Sru      warning "main_display(): \
5555151497Sruwrong device for ${_DISPLAY_MODE} mode: ${_OPT_DEVICE}";
5556151497Sru      ;;
5557151497Sru    esac;
5558151497Sru    md_addopts="${_ADDOPTS_GROFF} ${_ADDOPTS_POST}";
5559151497Sru    md_groggy="$(tmp_cat | grog -T${md_device})";
5560151497Sru    exit_test;
5561151497Sru    if obj _DISPLAY_MODE is_equal 'text'
5562151497Sru    then
5563151497Sru      _do_opt_V;
5564151497Sru      tmp_cat | eval "${md_groggy}" "${md_addopts}";
5565151497Sru    else
5566151497Sru      md_pager='';
5567151497Sru      for p in "${_OPT_PAGER}" "${PAGER}" "${_MANOPT_PAGER}" \
5568151497Sru               'less -r -R' 'more' 'pager' 'cat'
5569151497Sru      do
5570151497Sru        md_p="$p";
5571151497Sru        if eval is_prog ${md_p}
5572151497Sru        then		      # no "" for is_prog() allows args for $p
5573151497Sru          md_pager="${md_p}";
5574151497Sru          break;
5575151497Sru        fi;
5576151497Sru      done;
5577151497Sru      if obj md_pager is_empty
5578151497Sru      then
5579151497Sru        error 'main_display(): no pager program found for tty mode';
5580151497Sru      fi;
5581151497Sru      _do_opt_V;
5582151497Sru      tmp_cat | eval "${md_groggy}" "${md_addopts}" | \
5583151497Sru                eval "${md_pager}";
5584151497Sru    fi;
5585151497Sru    clean_up;
5586151497Sru    ;;
5587151497Sru  source)
5588151497Sru    tmp_cat;
5589151497Sru    clean_up;
5590151497Sru    ;;
5591151497Sru
5592151497Sru  #### viewer modes
5593151497Sru
5594151497Sru  dvi)
5595151497Sru    case "${_OPT_DEVICE}" in
5596151497Sru    ''|dvi) do_nothing; ;;
5597151497Sru    *)
5598151497Sru      warning "main_display(): \
5599151497Sruwrong device for ${_DISPLAY_MODE} mode: ${_OPT_DEVICE}"
5600151497Sru      ;;
5601151497Sru    esac;
5602151497Sru    md_modefile="${md_modefile}".dvi;
5603151497Sru    md_groggy="$(tmp_cat | grog -Tdvi)";
5604151497Sru    exit_test;
5605151497Sru    _do_display;
5606151497Sru    ;;
5607151497Sru  html)
5608151497Sru    case "${_OPT_DEVICE}" in
5609151497Sru    ''|html) do_nothing; ;;
5610151497Sru    *)
5611151497Sru      warning "main_display(): \
5612151497Sruwrong device for ${_DISPLAY_MODE} mode: ${_OPT_DEVICE}";
5613151497Sru      ;;
5614151497Sru    esac;
5615151497Sru    md_modefile="${md_modefile}".html;
5616151497Sru    md_groggy="$(tmp_cat | grog -Thtml)";
5617151497Sru    exit_test;
5618151497Sru    _do_display;
5619151497Sru    ;;
5620151497Sru  pdf)
5621151497Sru    case "${_OPT_DEVICE}" in
5622151497Sru    ''|ps)
5623151497Sru      do_nothing;
5624151497Sru      ;;
5625151497Sru    *)
5626151497Sru      warning "main_display(): \
5627151497Sruwrong device for ${_DISPLAY_MODE} mode: ${_OPT_DEVICE}";
5628151497Sru      ;;
5629151497Sru    esac;
5630151497Sru    md_groggy="$(tmp_cat | grog -Tps)";
5631151497Sru    exit_test;
5632151497Sru    _do_display _make_pdf;
5633151497Sru    ;;
5634151497Sru  ps)
5635151497Sru    case "${_OPT_DEVICE}" in
5636151497Sru    ''|ps)
5637151497Sru      do_nothing;
5638151497Sru      ;;
5639151497Sru    *)
5640151497Sru      warning "main_display(): \
5641151497Sruwrong device for ${_DISPLAY_MODE} mode: ${_OPT_DEVICE}";
5642151497Sru      ;;
5643151497Sru    esac;
5644151497Sru    md_modefile="${md_modefile}".ps;
5645151497Sru    md_groggy="$(tmp_cat | grog -Tps)";
5646151497Sru    exit_test;
5647151497Sru    _do_display;
5648151497Sru    ;;
5649151497Sru  x)
5650151497Sru    case "${_OPT_DEVICE}" in
5651151497Sru    X*)
5652151497Sru      md_device="${_OPT_DEVICE}"
5653151497Sru      ;;
5654151497Sru    *)
5655151497Sru      case "${_OPT_RESOLUTION}" in
5656151497Sru      100)
5657151497Sru        md_device='X100';
5658151497Sru        if obj _OPT_GEOMETRY is_empty
5659151497Sru        then
5660151497Sru          case "${_DISPLAY_PROG}" in
5661151497Sru          gxditview|xditview)
5662151497Sru            # add width of 800dpi for resolution of 100dpi to the args
5663151497Sru            list_append _DISPLAY_ARGS '-geometry' '800';
5664151497Sru            ;;
5665151497Sru          esac;
5666151497Sru        fi;
5667151497Sru        ;;
5668151497Sru      *)
5669151497Sru        md_device='X75-12';
5670151497Sru        ;;
5671151497Sru      esac
5672151497Sru    esac;
5673151497Sru    md_groggy="$(tmp_cat | grog -T${md_device} -Z)";
5674151497Sru    exit_test;
5675151497Sru    _do_display;
5676151497Sru    ;;
5677151497Sru  X)
5678151497Sru    case "${_OPT_DEVICE}" in
5679151497Sru    '')
5680151497Sru      md_groggy="$(tmp_cat | grog -X)";
5681151497Sru      exit_test;
5682151497Sru      ;;
5683151497Sru    X*|dvi|html|lbp|lj4|ps)
5684151497Sru      # these devices work with 
5685151497Sru      md_groggy="$(tmp_cat | grog -T"${_OPT_DEVICE}" -X)";
5686151497Sru      exit_test;
5687151497Sru      ;;
5688151497Sru    *)
5689151497Sru      warning "main_display(): \
5690151497Sruwrong device for ${_DISPLAY_MODE} mode: ${_OPT_DEVICE}";
5691151497Sru      md_groggy="$(tmp_cat | grog -Z)";
5692151497Sru      exit_test;
5693151497Sru      ;;
5694151497Sru    esac;
5695151497Sru    _do_display;
5696151497Sru    ;;
5697151497Sru  *)
5698151497Sru    error "main_display(): unknown mode \`${_DISPLAY_MODE}'";
5699151497Sru    ;;
5700151497Sru  esac;
5701151497Sru  eval ${_UNSET} md_addopts;
5702151497Sru  eval ${_UNSET} md_device;
5703151497Sru  eval ${_UNSET} md_groggy;
5704151497Sru  eval ${_UNSET} md_modefile;
5705151497Sru  eval ${_UNSET} md_options;
5706151497Sru  eval ${_UNSET} md_p;
5707151497Sru  eval ${_UNSET} md_pager;
5708151497Sru  eval "${return_ok}";
5709151497Sru} # main_display()
5710151497Sru
5711151497Sru
5712151497Sru########################
5713151497Sru# _do_display ([<prog>])
5714151497Sru#
5715151497Sru# Perform the generation of the output and view the result.  If an
5716151497Sru# argument is given interpret it as a function name that is called in
5717151497Sru# the midst (actually only for `pdf').
5718151497Sru#
5719151497Sru# Globals: $md_modefile, $md_groggy (from main_display())
5720151497Sru#
5721151497Sru_do_display()
5722151497Sru{
5723151497Sru  func_check _do_display '>=' 0 "$@";
5724151497Sru  _do_opt_V;
5725151497Sru  if obj _DISPLAY_PROG is_empty
5726151497Sru  then
5727151497Sru    trap_unset;
5728151497Sru    {
5729151497Sru      trap_set;
5730151497Sru      eval "${md_groggy}" "${_ADDOPTS_GROFF}" "${_TMP_CAT}";
5731151497Sru    } &
5732151497Sru  else
5733151497Sru    obj md_modefile rm_file;
5734151497Sru    cat "${_TMP_CAT}" | \
5735151497Sru      eval "${md_groggy}" "${_ADDOPTS_GROFF}" > "${md_modefile}";
5736151497Sru    if is_not_empty "$1"
5737151497Sru    then
5738151497Sru      eval "$1";
5739151497Sru    fi;
5740151497Sru    obj _TMP_CAT rm_file_with_debug;
5741151497Sru    if obj _VIEWER_TERMINAL is_yes # for programs that run on tty
5742151497Sru    then
5743151497Sru      eval "${_DISPLAY_PROG}" ${_DISPLAY_ARGS} "\"${md_modefile}\"";
5744151497Sru    else
5745151497Sru      case "${_DISPLAY_PROG}" in
5746151497Sru#      lynx\ *|less\ *|more\ *) # programs known to run on the terminal
5747151497Sru#        eval "${_DISPLAY_PROG}" ${_DISPLAY_ARGS} "\"${md_modefile}\"";
5748151497Sru#        ;;
5749151497Sru      *)
5750151497Sru        trap_unset;
5751151497Sru        {
5752151497Sru          trap_set;
5753151497Sru          eval "${_DISPLAY_PROG}" ${_DISPLAY_ARGS} "\"${md_modefile}\"";
5754151497Sru        } &
5755151497Sru        ;;
5756151497Sru      esac;
5757151497Sru    fi;
5758151497Sru  fi;
5759151497Sru  eval "${return_ok}";
5760151497Sru} # _do_display() of main_display()
5761151497Sru
5762151497Sru
5763151497Sru#############
5764151497Sru# _do_opt_V ()
5765151497Sru#
5766151497Sru# Check on option `-V'; if set print the corresponding output and leave.
5767151497Sru#
5768151497Sru# Globals: $_ALL_PARAMS, $_ADDOPTS_GROFF, $_DISPLAY_MODE, $_DISPLAY_PROG,
5769151497Sru#          $_DISPLAY_ARGS, $md_groggy,  $md_modefile
5770151497Sru#
5771151497Sru# Variable prefix: _doV
5772151497Sru#
5773151497Sru_do_opt_V()
5774151497Sru{
5775151497Sru  func_check _do_opt_V '=' 0 "$@";
5776151497Sru  if obj _OPT_V is_yes
5777151497Sru  then
5778151497Sru    _OPT_V='no';
5779151497Sru    echo1 "Parameters:     ${_ALL_PARAMS}";
5780151497Sru    echo1 "Display mode:   ${_DISPLAY_MODE}";
5781151497Sru    echo1 "Output file:    ${md_modefile}";
5782151497Sru    echo1 "Display prog:   ${_DISPLAY_PROG} ${_DISPLAY_ARGS}";
5783151497Sru    a="$(eval echo1 "'${_ADDOPTS_GROFF}'")";
5784151497Sru    exit_test;
5785151497Sru    echo1 "Output of grog: ${md_groggy} $a";
5786151497Sru    _doV_res="$(eval "${md_groggy}" "${_ADDOPTS_GROFF}")";
5787151497Sru    exit_test;
5788151497Sru    echo1 "groff -V:       ${_doV_res}"
5789151497Sru    leave;
5790151497Sru  fi;
5791151497Sru  eval "${return_ok}";
5792151497Sru} # _do_opt_V() of main_display()
5793151497Sru
5794151497Sru
5795151497Sru##############
5796151497Sru# _make_pdf ()
5797151497Sru#
5798151497Sru# Transform to pdf format; for pdf mode in _do_display().
5799151497Sru#
5800151497Sru# Globals: $md_modefile (from main_display())
5801151497Sru# 
5802151497Sru# Variable prefix: _mp
5803151497Sru#
5804151497Sru_make_pdf()
5805151497Sru{
5806151497Sru  func_check _do_display '=' 0 "$@";
5807151497Sru  _mp_psfile="${md_modefile}";
5808151497Sru  md_modefile="${md_modefile}.pdf";
5809151497Sru  obj md_modefile rm_file;
5810151497Sru  if gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite \
5811151497Sru        -sOutputFile="${md_modefile}" -c save pop -f "${_mp_psfile}"
5812151497Sru  then
5813151497Sru    :;
5814151497Sru  else
5815151497Sru    error '_make_pdf: could not transform into pdf format.';
5816151497Sru  fi;
5817151497Sru  obj _mp_psfile rm_file_with_debug;
5818151497Sru  eval ${_UNSET} _mp_psfile;
5819151497Sru  eval "${return_ok}";
5820151497Sru} # _make_pdf() of main_display()
5821151497Sru
5822151497Sru
5823151497Sru########################################################################
5824151497Sru# main (<command_line_args>*)
5825151497Sru#
5826151497Sru# The main function for groffer.
5827151497Sru#
5828151497Sru# Arguments:
5829151497Sru#
5830151497Srumain()
5831151497Sru{
5832151497Sru  func_check main '>=' 0 "$@";
5833151497Sru  # Do not change the sequence of the following functions!
5834151497Sru  landmark '13: main_init()';
5835151497Sru  main_init;
5836151497Sru  landmark '14: main_parse_MANOPT()';
5837151497Sru  main_parse_MANOPT;
5838151497Sru  landmark '15: main_parse_args()';
5839151497Sru  main_parse_args "$@";
5840151497Sru  landmark '16: main_set_mode()';
5841151497Sru  main_set_mode;
5842151497Sru  landmark '17: main_do_fileargs()';
5843151497Sru  main_do_fileargs;
5844151497Sru  landmark '18: main_set_resources()';
5845151497Sru  main_set_resources;
5846151497Sru  landmark '19: main_display()';
5847151497Sru  main_display;
5848151497Sru  eval "${return_ok}";
5849151497Sru}
5850151497Sru
5851151497Sru
5852151497Sru########################################################################
5853151497Sru
5854151497Srumain "$@";
5855