Imported Upstream version 1.22.4
[platform/upstream/groff.git] / src / roff / nroff / nroff.sh
1 #! /bin/sh
2 # Emulate nroff with groff.
3 #
4 # Copyright (C) 1992-2018 Free Software Foundation, Inc.
5 #
6 # Written by James Clark.
7
8 # This file is of 'groff'.
9
10 # 'groff' is free software; you can redistribute it and/or modify it
11 # under the terms of the GNU General Public License (GPL) as published
12 # by the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
14
15 # 'groff' is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 # General Public License for more details.
19
20 # You should have received a copy of the GNU General Public License
21 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
23 prog="$0"
24
25 # Default device.
26
27 # Check the GROFF_TYPESETTER environment variable.
28 Tenv=$GROFF_TYPESETTER
29
30 # Try the 'locale charmap' command first because it is most reliable.
31 # On systems where it doesn't exist, look at the environment variables.
32 case "`exec 2>/dev/null ; locale charmap`" in
33   UTF-8)
34     Tloc=utf8 ;;
35   ISO-8859-1 | ISO-8859-15)
36     Tloc=latin1 ;;
37   IBM-1047)
38     Tloc=cp1047 ;;
39   *)
40     case "${LC_ALL-${LC_CTYPE-${LANG}}}" in
41       *.UTF-8)
42         Tloc=utf8 ;;
43       iso_8859_1 | *.ISO-8859-1 | *.ISO8859-1 | \
44       iso_8859_15 | *.ISO-8859-15 | *.ISO8859-15)
45         Tloc=latin1 ;;
46       *.IBM-1047)
47         Tloc=cp1047 ;;
48       *)
49         case "$LESSCHARSET" in
50           utf-8)
51             Tloc=utf8 ;;
52           latin1)
53             Tloc=latin1 ;;
54           cp1047)
55             Tloc=cp1047 ;;
56           *)
57             Tloc=ascii ;;
58         esac ;;
59     esac ;;
60 esac
61
62 # 'for i; do' doesn't work with some versions of sh
63
64 Topt=
65 opts=
66 for i
67   do
68   case $1 in
69     -c)
70       opts="$opts -P-c" ;;
71     -h)
72       opts="$opts -P-h" ;;
73     -[eq] | -s*)
74       # ignore these options
75       ;;
76     -[dMmrnoTwW])
77       echo "$prog: option $1 requires an argument" >&2
78       exit 1 ;;
79     -[iptSUC] | -[dMmrnowW]*)
80       opts="$opts $1" ;;
81     -T*)
82       Topt=$1 ;;
83     -u*)
84       # Solaris 2.2 through at least Solaris 9 'man' invokes
85       # 'nroff -u0 ... | col -x'.  Ignore the -u0,
86       # since 'less' and 'more' can use the emboldening info.
87       # However, disable SGR, since Solaris 'col' mishandles it.
88       opts="$opts -P-c" ;;
89     -v | --version)
90       echo "GNU nroff (groff) version @VERSION@"
91       exit 0 ;;
92     --help)
93       echo "usage: nroff [-CchipStUv] [-dCS] [-MDIR] [-mNAME] [-nNUM] [-oLIST]"
94       echo "             [-rCN] [-Tname] [-WNAME] [-wNAME] [FILE...]"
95       exit 0 ;;
96     --)
97       shift
98       break ;;
99     -)
100       break ;;
101     -*)
102       echo "$prog: invalid option $1" >&2
103       exit 1 ;;
104     *)
105       break ;;
106   esac
107   shift
108 done
109
110 if test "x$Topt" != x ; then
111   T=$Topt
112 else
113   if test "x$Tenv" != x ; then
114     T=-T$Tenv
115   fi
116 fi
117
118 case $T in
119   -Tascii | -Tlatin1 | -Tutf8 | -Tcp1047)
120     ;;
121   *)
122     # ignore other devices and use locale fallback
123     T=-T$Tloc ;;
124 esac
125
126 # Set up the 'GROFF_BIN_PATH' variable
127 # to be exported in the current 'GROFF_RUNTIME' environment.
128
129 @GROFF_BIN_PATH_SETUP@
130 export GROFF_BIN_PATH
131
132 # Load nroff-style character definitions too.
133
134 PATH="$GROFF_RUNTIME$PATH" groff -mtty-char $T $opts ${1+"$@"}
135
136 # eof