[net.sources] extract -- print troff macro definitions

req@warwick.UUCP (Russell Quin) (11/20/86)

Extract finds specified definitions of macros (for the text formatters nroff,
troff, ditroff, titroff) and prints them.  For example,
     $ extract -ms I
produces
     .de I
     .nr PQ \\n(.f
     .ft 2
     .if !"\\$1"" \&\\$1\\f\\n(PQ\\$2
     ..
You can say
	extract I
because -ms is the default.  You can also say
	extract -fmymacs PP X XX pp
to find the definitions of PP, X, XX and pp in the file "mymacs".

The program ``soelim'' that is used is not supplied here.  It's standard with
BSD, and a public domain version was posted to the net.

The 4.2BSD -ms macros work out filenames to give to ".so" at runtime, so
it doesn't always work with them.

Nested macro definitions can produce surprising (but correct) results.
Source, Makefile and manual entry enclosed.

1:	Edit the Makefile: you'll need to alter the "R=/usr/local" if you
	don't want mailsplit to live in /usr/local/usr/bin.
	You may also want to rename it "fsplit".  I prefer "fsplit", but it's
	called mailsplit for historical reasons.

2:	make mailsplit

3:	have a play with it & satisfy yourself that it behaves reasonably

4:	make install

Make "install" will do a "$(MAKE) $(CLEAN)" afterwards.  If you don't want to
remove the binary, say
CLEAN="" make install
at step 4.

I shall be leaving Warwick to move to a new job at the end of the week, so
although mail will be forwarded (& I'd be glad of any changes that you needed
to make), I may take a while to respond!

Note that there are TAB characters in "extract.sh".  They're inside []:
the two lines like
	    '/^[.'"'"'][ 	]*d[es][ 	]*'"${pat}"'$/,/^\.\./p
			^      ^
                        |      |
                      tab      space
and similarly for the 2nd place in the same line, on each of the two lines.
Beware of using cheap editors.

Russell

#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
#	Makefile
#	extract.1
#	extract.sh
#	mkpat.c
# This archive created: Thu Nov 20 14:58:01 1986
export PATH; PATH=/bin:$PATH
echo shar: extracting "'Makefile'" '(760 characters)'
if test -f 'Makefile'
then
	echo shar: will not over-write existing file "'Makefile'"
else
sed 's/^X//' << \SHAR_EOF > 'Makefile'
X# PROGS is what to make, DESTDIR is where to install it
XR=/usr/local
XDESTDIR=$R/usr/bin
XDOCDIR=
XLIBDESTDIR=$R/usr/lib/troff
XMANDIR=$R/usr/man
XPROGS=mkpat extract
XMAKE=make
XCLEAN=clean
X
X# HACKS are common lint/cc/f77 flags: -D,-I,-U
X# R is an optional root (ie. prefix to DESTDIR)
X
XHACKS=
XCFLAGS=-O -s $(HACKS)
XLOADLIBES=
XLINT=lint
XLINTFLAGS=-phxabc $(HACKS)
XCHMODFLAGS=u=rwx,og=rx
XSHELL=/bin/sh
X
Xall:    $(PROGS)                                 # default
X
Xcp install:     $(PROGS)
X	-mkdir $(LIBDESTDIR) 2>/dev/null
X	cp extract $(DESTDIR)
X	cp extract.1 $(MANDIR)/man1
X	cp mkpat $(LIBDESTDIR)
X	$(MAKE) $(CLEAN)
X
Xclean:
X	rm -f $(PROGS) *.o core y.tab.c lex.yy.c a.out
X
Xlint:
X	$(LINT) $(LINTFLAGS) *.c
X
X$(DESTDIR)/extract: extract.sh
X
X$(LIBDESTDIR)/mkpat: mkpat.c
SHAR_EOF
if test 760 -ne "`wc -c < 'Makefile'`"
then
	echo shar: error transmitting "'Makefile'" '(should have been 760 characters)'
fi
fi # end of overwriting check
echo shar: extracting "'extract.1'" '(875 characters)'
if test -f 'extract.1'
then
	echo shar: will not over-write existing file "'extract.1'"
else
sed 's/^X//' << \SHAR_EOF > 'extract.1'
X.TH EXTRACT 1R
X.SH NAME
Xextract \- extract troff definitions from macro packages
X.SH SYNOPSIS
X.B extract 
X[
X.I options
X]
X.B macro_name
X.SH DESCRIPTION
X.I extract
Xis intended to be useful for writers and testers of troff macros.
XIt attempts to find the definition[s] of the macros given as arguments in
Xthe appropriate places -- /usr/lib/tmac/tmac.s is the default file,
Xcontaining the ms macros.
X.PP
XOptions are -f\c
X.I file
Xwhich looks in the named file, and -m\fIpackage\fP, which behaves as the
Xtroff -m flag.
X.SH EXAMPLE
X.PP
Xextract -man PP
X.br
X\&	.de SH
X.br
X\&	.}X 0
X.br
X\&	.nr )E 2
X.br
X\&	\e&\e\e$1 \e|\e\e$2 \e|\e\e$3 \e|\e\e$4 \e|\e\e$5 \e|\e\e$6
X.br
X\&	..
X.br
X.sp
X.SH "BUGS"
XDoesn't cope with input like
X.br
X.ft 3
X\&.de XXy
X.ft
X.br
Xwhich is legal
X.I troff
Xinput but is better written as
X.br
X.ft 3
X\&.de XX y
X.ft
X.br
X.SH "SEE ALSO"
Xtroff(1), soelim(1), man(7), ms(7)
SHAR_EOF
if test 875 -ne "`wc -c < 'extract.1'`"
then
	echo shar: error transmitting "'extract.1'" '(should have been 875 characters)'
fi
fi # end of overwriting check
echo shar: extracting "'extract.sh'" '(1334 characters)'
if test -f 'extract.sh'
then
	echo shar: will not over-write existing file "'extract.sh'"
else
sed 's/^X//' << \SHAR_EOF > 'extract.sh'
X#! /bin/sh
X# extract -- print out troff macro definitions
X# author -- R E Quin, Warwick University (UK) 1983
X# (version 2 November 1985)
X# see endnotes
Xsoelim=/usr/ucb/soelim
X# mkpat puts a \ in front of sed/grep meta-chars in its args
Xmkpat=/usr/local/usr/lib/troff/mkpat
X
XPATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin
Xmac_file=/usr/lib/tmac/tmac.s    # default macro package is -ms
X
Xfor i in "$@"
Xdo
X	case x"$i" in
X
X	x-m*)
X		mac_file=/usr/lib/tmac/tmac.`echo $i |sed -e 's/-m//'`
X		;;
X	x-f*)
X		mac_file=`echo $i | sed -e 's/-f//'`
X		;;
X	x*)
X		pat=`${mkpat} "$i"`
X		${soelim} ${mac_file} | sed -n -e \
X		    '/^[.'"'"'][ 	]*d[es][ 	]*'"${pat}"'$/,/^\.\./p
X		     /^[.'"'"'][ 	]*d[es][ 	]*'"${pat}"'[ 	]/,/^\.\./p'
X		;;
X	esac
Xdone
Xexit
X
X# bugs:
X# gets nested macro definitions wrong (will print out from the start of the
X# definition of the inner macro right up to the end of the enclosing one. It
X# would be hard to deal with that one.
X#
X# can't cope with .cc, so if you .de doesn't start with a ".", tough.
X#
X# won't get .de xx yy right -- always looks for .. at end.
X# 
X# most of the problems involve getting too much text out though.
X#
X# soelim can't cope with .ds xx file .... .so \*(xx, so it fails on the
X# 4.2 ms macros.  There is no way to deal with this, since some of the macros
X# in the .so files overwrite ones elsewhere!
X#
SHAR_EOF
if test 1334 -ne "`wc -c < 'extract.sh'`"
then
	echo shar: error transmitting "'extract.sh'" '(should have been 1334 characters)'
fi
fi # end of overwriting check
echo shar: extracting "'mkpat.c'" '(715 characters)'
if test -f 'mkpat.c'
then
	echo shar: will not over-write existing file "'mkpat.c'"
else
sed 's/^X//' << \SHAR_EOF > 'mkpat.c'
X/* mkpat -- make a pattern for sed to use to match argument */
X#include <stdio.h>
X
Xmain(argc, argv)
X     char *argv[];
X{
X     char *rindex();
X
X     char *progname = rindex(argv[0], '/');
X     char * fullprogname = argv[0];
X     char *ap;
X
X     if (!progname)
X	  progname = argv[0];
X     
X     if (argc != 2) {
X	  fprintf(stderr, "%s:usage - %s name\n", fullprogname, progname);
X	  exit(1);
X     }
X
X     for (ap = argv[1]; *ap; ap++ ) {
X	  switch(*ap) {
X
X	  case '*':
X	  case '[':
X	  case '.':
X	  case '&':
X	  case '~':	/* ~ is special to ex and ucbgrep */
X	  case '$':
X	  case '^':
X	  case '\\':
X	  case '/':
X	       printf("\\%c", *ap);
X	       break;
X	  default:
X	       putchar(*ap);
X	  }
X     }
X     exit(0);
X}
SHAR_EOF
if test 715 -ne "`wc -c < 'mkpat.c'`"
then
	echo shar: error transmitting "'mkpat.c'" '(should have been 715 characters)'
fi
fi # end of overwriting check
#	End of shell archive
exit 0
-- 
ARPA		req%uu.warwick.ac.uk@ucl-cs.arpa
EARN/BITNET	req%UK.AC.WARWICK.UU@AC.UK
JANET		req@uk.ac.warwick.uu
UUCP		seismo!mcvax!ukc!warwick!req  (req@warwick.UUCP)
PHONE		+44 203 523193 Until 20th November.
No new FRPList requests, please.  Details in net.games.frp.  Thanks!
The BITNET path only works from sites that have AC.UK in their tables.  Sorry.