UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 43.19 Removing Leading Tabs and Other Trivia Chapter 43
Printing
Next: 43.21 Preprocessing troff Input with sed
 

43.20 Displaying a troff Macro Definition

If you're writing or debugging troff macros (43.13), you often need to review the contents of a macro. Rather than searching through the macro definition files with an editor, it is relatively easy to construct a small script that uses sed (34.24) and the shell to extract and display a single macro.

The script is easy to construct because the macro definitions have a regular structure that lends itself to easy identification with the regular expressions (26.4) used by sed. A troff macro definition always begins with the string .de, followed by an optional space and the one- or two-letter name of the macro. The definition ends with a line beginning with two dots (..).

troff macros are defined in a macro package, often a single file that's located in a directory such as /usr/lib/tmac. The most common macro packages are mm, ms and me-though it is unlikely that a given system will have all three. mm is generally found on System V-derived systems, and ms and me on BSD-derived systems. The man macros are found on just about all systems because they are used by the man command (50.1).

Here's an example:

% getmac -ms LP
.\"
.\" The following definition of LP was found in /usr/lib/tmac/tmac.s:
.\"
.de LP
.RT
.if \\n(1T .sp \\n(PDu

.ti \\n(.iu
..

The getmac script is below. For those unfamiliar with sed, there are comments in the script on the CD-ROM (and in the online archive file):

#!/bin/sh
# Usage: getmac -package macro
case $1 in
-mm) file="/usr/lib/macros/mmt";;
-ms) file="/usr/lib/tmac/tmac.s";;
-me) file="/usr/lib/tmac/tmac.e";;
-man) file="/usr/lib/tmac/tmac.an";;
*) echo "Usage: getmac -package macro" 1>&2; exit 1;;
esac
mac=$2

sed -n '
/^\.de *'$mac'/,/^\.\.$/ {
   /^\.de/i\
.\\"\
.\\" The following definition of '$mac' was found in '$file':\
.\\"
   p
   /^\.\.$/q
}' $file

- TOR, JP


Previous: 43.19 Removing Leading Tabs and Other Trivia UNIX Power ToolsNext: 43.21 Preprocessing troff Input with sed
43.19 Removing Leading Tabs and Other Trivia Book Index43.21 Preprocessing troff Input with sed

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System