xref: /src/contrib/tzcode/workman.sh (revision ff2c98b30b57b9763e2a6575f729bab676e6c025)
1#! /bin/sh
2# Convert manual page troff stdin to formatted .txt stdout.
3
4# This file is in the public domain, so clarified as of
5# 2009-05-17 by Arthur David Olson.
6
7manflags=
8while
9  case $1 in
10    -*) :;;
11    *) false;;
12  esac
13do
14  manflags="$manflags $1"
15  shift
16done
17
18groff="groff -dAD=l -rHY=0 $manflags -mtty-char -man -ww -P-bcou"
19if ($groff) </dev/null >/dev/null 2>&1; then
20  $groff "$@"
21elif (type mandoc && type col) >/dev/null 2>&1; then
22  mandoc $manflags -man "$@" | col -bx
23elif (type nroff && type perl) >/dev/null 2>&1; then
24  printf '%s\n' '.
25.\" Left-adjust and do not hyphenate.
26.am TH
27.na
28.hy 0
29..
30.\" Omit internal page headers and footers.
31.\" Unfortunately this also omits the starting header and ending footer,
32.\" but that is the best old nroff can easily do.
33.rm }H
34.rm }F
35.' | nroff -man - "$@" | perl -ne '
36	binmode STDIN, '\'':encoding(utf8)'\'';
37	binmode STDOUT, '\'':encoding(utf8)'\'';
38	chomp;
39	s/.\010//g;
40	s/\s*$//;
41	if (/^$/) {
42		$sawblank = 1;
43		next;
44	} else {
45		if ($sawblank && $didprint) {
46			print "\n";
47			$sawblank = 0;
48		}
49		print "$_\n";
50		$didprint = 1;
51	}
52  '
53else
54  printf >&2 '%s\n' "$0: please install groff, or mandoc and col"
55  exit 1
56fi
57