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
7if (type nroff && type perl) >/dev/null 2>&1; then
8
9  # Tell groff not to emit SGR escape sequences (ANSI color escapes).
10  GROFF_NO_SGR=1
11  export GROFF_NO_SGR
12
13  echo ".am TH
14.hy 0
15.na
16..
17.rm }H
18.rm }F" | nroff -man - ${1+"$@"} | perl -ne '
19	binmode STDIN, '\'':encoding(utf8)'\'';
20	binmode STDOUT, '\'':encoding(utf8)'\'';
21	chomp;
22	s/.\010//g;
23	s/\s*$//;
24	if (/^$/) {
25		$sawblank = 1;
26		next;
27	} else {
28		if ($sawblank && $didprint) {
29			print "\n";
30			$sawblank = 0;
31		}
32		print "$_\n";
33		$didprint = 1;
34	}
35  '
36elif (type mandoc && type col) >/dev/null 2>&1; then
37  mandoc -man -T ascii "$@" | col -bx
38else
39  echo >&2 "$0: please install nroff and perl, or mandoc and col"
40  exit 1
41fi
42