xref: /freebsd/usr.sbin/manctl/manctl.sh (revision 38069501)
1#!/bin/sh
2#
3# Copyright (c) 1994 Geoffrey M. Rehmet, Rhodes University
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14# 3. All advertising materials mentioning features or use of this software
15#    must display the following acknowledgement:
16#	This product includes software developed by Geoffrey M. Rehmet
17# 4. Neither the name of Geoffrey M. Rehmet nor that of Rhodes University
18#    may be used to endorse or promote products derived from this software
19#    without specific prior written permission.
20#
21# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24# IN NO EVENT SHALL GEOFFREY M. REHMET OR RHODES UNIVERSITY BE LIABLE
25# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31# SUCH DAMAGE.
32#
33# $FreeBSD$
34#
35# manctl:
36#	a utility for manipulating manual pages
37# functions:
38#	compress uncompressed man pages (elliminating .so's)
39#		this is now two-pass.  If possible, .so's
40#		are replaced with hard links
41#	uncompress compressed man pages
42# Things to watch out for:
43#	Hard links - careful with g(un)zipping!
44#	.so's - throw everything through soelim before gzip!
45#	symlinks - ignore these - eg: expn is its own man page:
46#			don't want to compress this!
47#
48PATH=/bin:/sbin:/usr/bin:/usr/sbin; export PATH
49
50#
51# Uncompress one page
52#
53uncompress_page()
54{
55	local	pname
56	local	fname
57	local	sect
58	local	ext
59
60	# break up file name
61	pname=$1
62	IFS='.' ; set $pname
63	# less than 3 fields - don't know what to do with this
64	if [ $# -lt 3 ] ; then
65		IFS=" 	" ; echo ignoring $pname 1>&2 ; return 0 ;
66	fi
67	# construct name and section
68	fname=$1 ; shift
69	while [ $# -gt 2 ] ; do
70		fname=$fname.$1
71		shift
72	done
73	sect=$1
74	ext=$2
75
76	IFS=" 	"
77	case "$ext" in
78	gz|Z) 	{
79		IFS=" 	" ; set `file $pname`
80		if [ $2 != "gzip" ] ; then
81			echo moving hard link $pname 1>&2
82			mv $pname $fname.$ext	# link
83		else
84			if [ $2 != "symbolic" ] ; then
85				echo gunzipping page $pname 1>&2
86				temp=`mktemp -t manager` || exit 1
87				gunzip -c $pname > $temp
88				chmod u+w $pname
89				cp $temp $pname
90				chmod 444 $pname
91				mv $pname $fname.$sect
92				rm -f $temp
93			else
94				# skip symlinks - this can be
95				# a program like expn, which is
96				# its own man page !
97				echo skipping symlink $pname 1>&2
98			fi
99		fi };;
100	*)	{
101		IFS=" 	"
102		echo skipping file $pname 1>&2
103		} ;;
104	esac
105	# reset IFS - this is important!
106	IFS=" 	"
107}
108
109
110#
111# Uncompress manpages in paths
112#
113do_uncompress()
114{
115	local	i
116	local	dir
117	local	workdir
118
119	workdir=`pwd`
120	while [ $# != 0 ] ; do
121		if [ -d $1 ] ; then
122			dir=$1
123			cd $dir
124			for i in * ; do
125				case $i in
126				*cat?)	;; # ignore cat directories
127				*)	{
128					if [ -d $i ] ; then
129						do_uncompress $i
130					else
131						if [ -e $i ] ; then
132							uncompress_page $i
133						fi
134					fi } ;;
135				esac
136			done
137			cd $workdir
138		else
139			echo "directory $1 not found" 1>&2
140		fi
141		shift
142	done
143}
144
145#
146# Remove .so's from one file
147#
148so_purge_page()
149{
150 	local	so_entries
151	local	lines
152	local	fname
153
154	so_entries=`grep "^\.so" $1 | wc -l`
155	if [ $so_entries -eq 0 ] ; then return 0 ; fi
156
157	# we have a page with a .so in it
158	echo $1 contains a .so entry 2>&1
159
160	# now check how many lines in the file
161	lines=`wc -l < $1`
162
163	# if the file is only one line long, we can replace it
164	# with a hard link!
165	if [ $lines -eq 1 ] ; then
166		fname=$1;
167		echo replacing $fname with a hard link
168		set `cat $fname`;
169		rm -f $fname
170		ln ../$2 $fname
171	else
172		echo inlining page $fname 1>&2
173		temp=`mktemp -t manager` || exit 1
174		cat $fname | \
175		(cd .. ; soelim ) > $temp
176		chmod u+w $fname
177		cp $temp $fname
178		chmod 444 $fname
179		rm -f $temp
180	fi
181}
182
183#
184# Remove .so entries from man pages
185#	If a page consists of just one line with a .so,
186#	replace it with a hard link
187#
188remove_so()
189{
190	local	pname
191	local	fname
192	local	sect
193
194	# break up file name
195	pname=$1
196	IFS='.' ; set $pname
197	if [ $# -lt 2 ] ; then
198		IFS=" 	" ; echo ignoring $pname 1>&2 ; return 0 ;
199	fi
200	# construct name and section
201	fname=$1 ; shift
202	while [ $# -gt 1 ] ; do
203		fname=$fname.$1
204		shift
205	done
206	sect=$1
207
208	IFS=" 	"
209	case "$sect" in
210	gz) 	{ echo file $pname already gzipped 1>&2 ; } ;;
211	Z)	{ echo file $pname already compressed 1>&2 ; } ;;
212	[12345678ln]*){
213		IFS=" 	" ; set `file $pname`
214		if [ $2 = "gzip" ] ; then
215			echo moving hard link $pname 1>&2
216			mv $pname $pname.gz	# link
217		else
218			if [ $2 != "symbolic" ] ; then
219				echo "removing .so's in  page $pname" 1>&2
220				so_purge_page $pname
221			else
222				# skip symlink - this can be
223				# a program like expn, which is
224				# its own man page !
225				echo skipping symlink $pname 1>&2
226			fi
227		fi };;
228	*)	{
229		IFS=" 	"
230		echo skipping file $pname 1>&2
231		} ;;
232	esac
233	# reset IFS - this is important!
234	IFS=" 	"
235}
236
237
238#
239# compress one page
240#	We need to watch out for hard links here.
241#
242compress_page()
243{
244	local	pname
245	local	fname
246	local	sect
247
248	# break up file name
249	pname=$1
250	IFS='.' ; set $pname
251	if [ $# -lt 2 ] ; then
252		IFS=" 	" ; echo ignoring $pname 1>&2 ; return 0 ;
253	fi
254	# construct name and section
255	fname=$1 ; shift
256	while [ $# -gt 1 ] ; do
257		fname=$fname.$1
258		shift
259	done
260	sect=$1
261
262	IFS=" 	"
263	case "$sect" in
264	gz) 	{ echo file $pname already gzipped 1>&2 ; } ;;
265	Z)	{ echo file $pname already compressed 1>&2 ; } ;;
266	[12345678ln]*){
267		IFS=" 	" ; set `file $pname`
268		if [ $2 = "gzip" ] ; then
269			echo moving hard link $pname 1>&2
270			mv $pname $pname.gz	# link
271		else
272			if [ $2 != "symbolic" ] ; then
273				echo gzipping page $pname 1>&2
274				temp=`mktemp -t manager` || exit 1
275				cat $pname | \
276				(cd .. ; soelim )| gzip -c -- > $temp
277				chmod u+w $pname
278				cp $temp $pname
279				chmod 444 $pname
280				mv $pname $pname.gz
281				rm -f $temp
282			else
283				# skip symlink - this can be
284				# a program like expn, which is
285				# its own man page !
286				echo skipping symlink $pname 1>&2
287			fi
288		fi };;
289	*)	{
290		IFS=" 	"
291		echo skipping file $pname 1>&2
292		} ;;
293	esac
294	# reset IFS - this is important!
295	IFS=" 	"
296}
297
298#
299# Compress man pages in paths
300#
301do_compress_so()
302{
303	local	i
304	local	dir
305	local	workdir
306	local	what
307
308	what=$1
309	shift
310	workdir=`pwd`
311	while [ $# != 0 ] ; do
312		if [ -d $1 ] ; then
313			dir=$1
314			cd $dir
315			for i in * ; do
316				case $i in
317				*cat?)	;; # ignore cat directories
318				*)	{
319					if [ -d $i ] ; then
320						do_compress_so $what $i
321					else
322						if [ -e $i ] ; then
323							$what $i
324						fi
325					fi } ;;
326				esac
327			done
328			cd $workdir
329		else
330			echo "directory $1 not found" 1>&2
331		fi
332		shift
333	done
334}
335
336#
337# Display a usage message
338#
339ctl_usage()
340{
341	echo "usage: $1 -compress <path> ... " 1>&2
342	echo "       $1 -uncompress <path> ... " 1>&2
343	exit 1
344}
345
346#
347# remove .so's and do compress
348#
349do_compress()
350{
351	# First remove all so's from the pages to be compressed
352	do_compress_so remove_so "$@"
353	# now do ahead and compress the pages
354	do_compress_so compress_page "$@"
355}
356
357#
358# dispatch options
359#
360if [ $# -lt 2 ] ; then ctl_usage $0 ; fi ;
361
362case "$1" in
363	-compress)	shift ; do_compress "$@" ;;
364	-uncompress)	shift ; do_uncompress "$@" ;;
365	*)		ctl_usage $0 ;;
366esac
367