[alt.sources] software tools question

jkrueger@dgis.dtic.dla.mil (Jon) (12/09/89)

Original-posting-by: jkrueger@dgis.dtic.dla.mil (Jon)
Reposted-by: emv@math.lsa.umich.edu (Edward Vielmetti)
Posting-id: 891208.2040
Posting-number: Volume TEST, Number TEST
Archive-name: m4include, lex, mimics m4 include & passes everything else thru

[This is an experimental alt.sources re-posting from the
newsgroup(s) comp.unix.questions.
No attempt has been made to edit, clean, modify, or otherwise
change the contents of the original posting, or to contact the
author.  Please consider cross-posting all sources postings to
alt.sources as a matter of course.]

[Comments on this service to emv@math.lsa.umich.edu (Edward Vielmetti)]

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@dtic.dla.mil   uunet!dgis!jkrueger
The Philip Morris Companies, Inc: without question the strongest
and best argument for an anti-flag-waving amendment.

shapiro@osiris.cso.uiuc.edu (12/09/89)

The program "soelim" idoes what you need:

	soelim files | tbl | ...

wrp@PRC.Unisys.COM (William R. Pringle) (12/14/89)

Of course, soelim is not included in System V.  If you don't have it on
your system, let me know I will mail you a PD version of soelim that I wrote.

Bill Pringle