groff: update vendor branch to v1.20.1
[dragonfly.git] / contrib / groff / src / roff / grog / grog.sh
CommitLineData
465b256c 1#! /bin/sh
92d0a6a6
JR
2# grog -- guess options for groff command
3# Like doctype in Kernighan & Pike, Unix Programming Environment, pp 306-8.
4
4d3e9548
JL
5# Source file position: <groff-source>/src/roff/grog/grog.sh
6# Installed position: <prefix>/bin/grog
7
8# Copyright (C) 1993, 2006, 2009 Free Software Foundation, Inc.
9# Written by James Clark, maintained by Werner Lemberg.
10# Rewritten by and put under GPL Bernd Warken.
11
12# This file is part of `grog', which is part of `groff'.
13
14# `groff' is free software; you can redistribute it and/or modify it
15# under the terms of the GNU General Public License (GPL) as published
16# by the Free Software Foundation, either version 3 of the License, or
17# (at your option) any later version.
18
19# `groff' is distributed in the hope that it will be useful, but
20# WITHOUT ANY WARRANTY; without even the implied warranty of
21# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22# General Public License for more details.
23
24# You should have received a copy of the GNU General Public License
25# along with this program. If not, see <http://www.gnu.org/licenses/>.
26
27########################################################################
28Last_Update='5 Jan 2009'
29########################################################################
30
92d0a6a6
JR
31soelim=@g@soelim
32
33opts=
4d3e9548
JL
34mopt=
35nr=0
92d0a6a6 36sp="([ ]|$)"
4d3e9548
JL
37double_minus=0
38was_minus=0
39filespec=
40had_filespec=0
92d0a6a6
JR
41
42for arg
43do
4d3e9548
JL
44 if test _"${double_minus}"_ = _1_
45 then
46 had_filespec=1
47 if test -f "${arg}" && test -r "${arg}"
48 then
49 case "$arg" in
50 *" "*)
51 eval filespec="'${filespec} '"'\"'"'${arg}'"'\"'
52 ;;
53 *)
54 eval filespec="'${filespec} ${arg}'"
55 ;;
56 esac
57 else
58 echo "grog: ${arg} is not a readable file.">&2
59 fi
60 continue
61 fi
62 case "$arg" in
63 --)
64 double_minus=1
65 ;;
66 -)
67 # omit several -
68 if test _"${was_minus}"_ = _0_
69 then
70 was_minus=1
71 filespec="${filespec} -"
72 fi
73 ;;
74 -C)
75 sp=; opts="$opts -C";
76 ;;
77 -v|--v|--ve|--ver|--vers|--versi|--versio|--version)
78 echo \
79"Shell version of GNU grog of $Last_Update in groff version @VERSION@";
80 exit 0
81 ;;
82 -h|--h|--he|--hel|--help)
83 cat <<EOF
84
85usage: grog [option]... [--] [filespec]...
86
87"filespec" is either the name of an existing, readable file or "-" for
88standard input. If no "filespec" is specified, standard input is
89assumed automatically.
90
91"option" is either a "groff" option or one of these:
92
93-C compatibility mode
94-h --help print this uasge message and exit
95-v --version print version information and exit
96
97"groff" options are appended to the output, "-m" options are checked.
98
99EOF
100 exit 0
101 ;;
102 -m*)
103 if test _"$nr"_ = _0_
104 then
105 nr=1
106 mopt="$arg"
107 else
108 echo "grog: several -m* options are not allowed." >&2
109 mopt=
110 fi
111 ;;
112 -*)
113 opts="$opts $arg"
114 ;;
115 *)
116 had_filespec=1
117 if test -f "${arg}" && test -r "${arg}"
118 then
119 case "$arg" in
120 *" "*)
121 eval filespec="'${filespec} '"'\"'"'${arg}'"'\"'
122 ;;
123 *)
124 eval filespec="'${filespec} ${arg}'"
125 ;;
126 esac
127 else
128 echo "grog: ${arg} is not a readable file.">&2
129 fi
130 ;;
131 esac
92d0a6a6 132done
4d3e9548
JL
133if test _"${filespec}"_ = __ && test _"${had_filespec}"_ = _0_
134then
135 filespec='-'
136fi
137if test _"${filespec}"_ = __
138then
139 exit 1
140fi
141if test "${double_minus}" = 1
142then
143 eval files="'-- ${filespec}'"
144else
145 eval files="'${filespec}'"
146fi
147files=`echo $files|sed s/\"/\'/g`
92d0a6a6 148
4d3e9548
JL
149
150eval sed "'s/[ ]*$//'" '--' "${filespec}" \
151| sed 's/^[ ]*begin[ ][ ]*chem$/.cstart/' \
152| sed 's/^[.'"'"'][ ]*/./' \
153| @EGREP@ -h \
154 "^\.(\[|\])|cstart|((P|PS|[PLI]P|[pnil]p|sh|Dd|Tp|Dp|De|Cx|Cl|Oo|.* Oo|Oc|.* Oc|NH|TL|TS|TE|EQ|TH|SH|so|\[|R1|GS|G1|PH|SA)$sp)" \
155| sed '/^\.so/s/^.*$/.SO_START\
92d0a6a6
JR
156&\
157.SO_END/' \
158| $soelim \
4d3e9548
JL
159| @EGREP@ \
160 '^\.(cstart|P|PS|[PLI]P|[pnil]p|sh|Dd|Tp|Dp|De|Cx|Cl|Oo|.* Oo|Oc|.* Oc|NH|TL|TS|TE|EQ|TH|TL|NH|SH|\[|\]|R1|GS|G1|PH|SA|SO_START|SO_END)' \
92d0a6a6
JR
161| awk '
162/^\.SO_START$/ { so = 1 }
163/^\.SO_END$/ { so = 0 }
4d3e9548
JL
164/^\.cstart$/ { chem++ }
165/^\.TS/ { tbl++; in_tbl = 1; if (so > 0) soelim++; }
166/^\.TE/ { in_tbl = 0 }
92d0a6a6
JR
167/^\.PS([ 0-9.<].*)?$/ { pic++; if (so > 0) soelim++ }
168/^\.EQ/ { eqn++; if (so > 0) soelim++ }
169/^\.R1/ { refer++; if (so > 0) soelim++ }
170/^\.\[/ {refer_start++; if (so > 0) soelim++ }
171/^\.\]/ {refer_end++; if (so > 0) soelim++ }
172/^\.GS/ { grn++; if (so > 0) soelim++ }
173/^\.G1/ { grap++; pic++; if (so > 0) soelim++ }
4d3e9548
JL
174/^\.TH/ { if (in_tbl != 1) TH++ }
175/^\.PP/ { PP++ }
176/^\.TL/ { TL++ }
177/^\.NH/ { NH++ }
178/^\.[IL]P/ { ILP++ }
92d0a6a6
JR
179/^\.P$/ { P++ }
180/^\.SH/ { SH++ }
181/^\.(PH|SA)/ { mm++ }
182/^\.([pnil]p|sh)/ { me++ }
183/^\.Dd/ { mdoc++ }
184/^\.(Tp|Dp|De|Cx|Cl)/ { mdoc_old++ }
185/^\.(O[oc]|.* O[oc]( |$))/ {
4d3e9548
JL
186 sub(/\\\".*/, "")
187 gsub(/\"[^\"]*\"/, "")
188 sub(/\".*/, "")
189 sub(/^\.Oo/, " Oo ")
190 sub(/^\.Oc/, " Oc ")
191 sub(/ Oo$/, " Oo ")
192 sub(/ Oc$/, " Oc ")
193 while (/ Oo /) {
194 sub(/ Oo /, " ")
195 Oo++
196 }
197 while (/ Oc /) {
198 sub(/ Oc /, " ")
199 Oo--
200 }
92d0a6a6
JR
201}
202/^\.(PRINTSTYLE|START)/ { mom++ }
203
204END {
4d3e9548
JL
205 if (chem > 0) {
206 printf "chem %s | ", files
207 pic++
208 files = ""
209 }
210 printf "groff"
211 refer = refer || (refer_start && refer_end)
212 if (pic > 0 || tbl > 0 || grn > 0 || grap > 0 || eqn > 0 || refer > 0) {
213 printf " -"
214 if (soelim > 0) printf "s"
215 if (refer > 0) printf "R"
216 if (grn > 0) printf "g"
217 if (grap > 0) printf "G"
218 if (pic > 0) printf "p"
219 if (tbl > 0) printf "t"
220 if (eqn > 0) printf "e"
221 }
222 mnr = 0
223 m = ""
224 is_man = 0
225 is_mm = 0
226 is_mom = 0
227 # me
228 if (me > 0) {
229 mnr++; m = "-me"; printf " -me"
230 }
231 # man
232 if (SH > 0 && TH > 0) {
233 mnr++; m = "-man"; printf " -man"; is_man = 1
234 }
235 # mom
236 if (mom > 0) {
237 mnr++; m = "-mom"; printf " -mom"; is_mom = 1
238 }
239 # mm
240 if ( mm > 0 || (P > 0 && is_man == 0) ) {
241 mnr++; m = "-mm"; printf " -mm"; is_mm = 1
242 }
243 # ms
244 if ( NH > 0 || (TL > 0 && is_mm == 0) || (ILP > 0 && is_man == 0) ||
245 (PP > 0 && is_mom == 0 && is_man == 0) ) {
246 # .TL also occurs in -mm, .IP and .LP also occur in -man
247 # .PP also occurs in -mom and -man
248 mnr++; m = "-ms"; printf " -ms"
249 }
250 # mdoc
251 if (mdoc > 0) {
252 mnr++
253 if (mdoc_old > 0 || Oo > 0) {
254 m = "-mdoc-old"; printf " -mdoc-old"
255 }
256 else {
257 m = "-mdoc"; printf " -mdoc"
258 }
259 }
260
261 if (mnr == 0) {
262 if (mopt != "")
263 printf "%s", mopt
264 }
265
266 if (opts != "")
267 printf "%s", opts
268 if (files != "")
269 printf " %s", files
270 print ""
271
272 if (mnr == 1) {
273 if (mopt != "" && m != mopt)
274 printf "grog: wrong option %s\n", mopt | "cat 1>&2"
275 }
276 if (mnr >= 2) {
277 err = "grog: error: there are several -m* arguments"
278 printf "%s\n", err | "cat 1>&2"
279 exit mnr
280 }
281}' "opts=$opts" "mopt=$mopt" "files=$files" -