software tools question

Jon jkrueger at dgis.dtic.dla.mil
Fri Dec 8 18:14:30 AEST 1989


I can't be the only person who needs to do this...
so maybe this is appropriate for the net.

I have a table that is common to many different documents.  I decided
to keep it in a separate file, so I can maintain a single copy, and
include it into my documents as needed.  I'm using text processing
tools commonly found on the UNIX (registered trademark of AT&T)
timesharing system:  troff and tbl.  So I placed my tbl definitions
into a file we'll call mytable.  I figured I'd just use the the .so
command in troff: .so mytable.  Right?

Wrong: the .so command includes mytable *after* tbl is done.  It needs
to be included before.  So I went looking for an interpolation tool.
The right tool on UNIX seemed to be simple macro preprocessor: m4.  I
changed the .so mytable to include(mytable), and generated my document
with m4 mydoc | tbl | troff.  This worked pretty well.

One day, the word "shift" appeared at the beginning of one of the lines
in my document.  This generated the message "m4: shift not yet
implemented", and that line was dropped from output.  More careful
examination of the m4 man page revealed that m4 keywords like
"include", "define", and "divert" would occur in my documents.  M4 will
drop them, and if they're at the beginning of the line, the rest of the
line with them.

Well, I gave up.  The following lex program mimics the m4 include
behavior, and passes through all else:

	%{
		FILE	*fp;
		char	*index();
		int	gotc;
	%}
	%%
	^include\([^)]*\)$	{
				*(index(yytext, ')')) = '\0';
				fp = fopen(index(yytext, '(') + 1, "r");
				if (fp != NULL) {
					while ((gotc = getc(fp)) != EOF)
						putchar((char) gotc);
					fclose(fp);
				}
			}
	%%

It solves my immediate problem, and certainly wasn't that terrible to
write.  But the software tools question keeps coming back to me:  is
there some tool already on UNIX that I'm missing here?  Sed can't do
this at all, as far as I can tell.  Awk can, but doesn't seem better
suited than lex; it would be even more, uh, awkward.  Cpp has fewer
keywords to bang into but isn't really a general purpose tool, will
want to introduce stray text like line numbers.  What is the right
tool for this?

-- Jon
-- 
Jonathan Krueger    jkrueger at dtic.dla.mil   uunet!dgis!jkrueger
The Philip Morris Companies, Inc: without question the strongest
and best argument for an anti-flag-waving amendment.



More information about the Comp.unix.questions mailing list