tbl preprocessor for expanding .so files

R A Levenberg ral at pyuxqq.UUCP
Thu Jun 6 12:45:13 AEST 1985


This filter can be used to expand tables specified in files that
are dragged in by .so nroff commands.  It's needed in front of
pipelines like
	tbl ... | nroff ...
if the input contains lines like
	.so filename
when filename contains a table definition such as
	.TS
		...
	.TE
because nroff, and not tbl, expands the .so line to the contents
of the named file.  The program is actually very trivial, but the
concept is useful.  The following was built with shar.
#	This is a shell archive.
#	Remove everything above and including the cut line.
#	Then run the rest of the file through sh.
-----cut here-----cut here-----cut here-----cut here-----
#!/bin/sh
# shar:	Shell Archiver
#	Run the following text with /bin/sh to create:
#	catso.1
#	catso.c
# This archive created: Wed Jun  5 22:39:44 1985
echo shar: extracting catso.1 '(1469 characters)'
sed 's/^XX//' << \SHAR_EOF > catso.1
XX.ad b
XX.de In		\" Space and indent
XX.sp
XX.in +4
XX..
XX.de Un		\" Space and undent
XX.sp
XX.in -4
XX..
XX.TH CATSO 1 "LOCAL"
XX.SH NAME
XXcatso - Cats files, interpreting nroff-style file inclusion
XX.SH SYNOPSIS
XX\fBcatso\fR [ file or \fB-\fR ] ...
XX.SH DESCRIPTION
XX\fICatso\fR concatenates the files together, recursively inserting
XXfiles referenced by the nroff commands '.so' and '.nx' in their
XXproper positions.
XXWhen interpreting a '.so' command, the named file is itself \fIcatso\fRd
XXat that point, then processing resumes in the file that contained the '.so'
XXcommand.
XXFiles referenced by '.nx' are treated the same, except that processing
XXstops after the file referenced by the '.nx' command.
XX.P
XX\fICatso\fR is useful for providing input to an \fInroff\fR(1)
XXpreprocessor, such as \fInrpp\fR(1), since normally the files referenced by
XXthe .so and .nx commands will not be seen by the preprocessor.
XX.P
XXThe file name '-' means standard input.
XXStandard input will also be used if no arguments are given, unless
XXthe input is connected to the terminal.
XXIn the latter case, \fIcatso\fR will print its usage instructions.
XX.SH FILES
XX.SH AUTHOR
XXJ. Leth, IH 55414 6B-326, x6133.
XX(Based on an earlier version by D. A. Spicer).
XX.SH SEE ALSO
XXnroff(1), mm(1)
XX.SH DIAGNOSTICS
XX"Can't open [ .nx | .so ] file '<filename>'." -- the argument file, .so
XXfile, or .nx file can't be read or does not exist.
XX.SH BUGS
XXWill not interpret commands imbedded in macros or conditionals, or commands
XXfollowing a ';'.
SHAR_EOF
if test 1469 -ne "`wc -c catso.1`"
then
echo shar: error transmitting catso.1 '(should have been 1469 characters)'
fi
echo shar: extracting catso.c '(1898 characters)'
sed 's/^XX//' << \SHAR_EOF > catso.c
XX#include <stdio.h>
XX
XX/* Catso will cat the standard input to the standard output, inserting
XX * all files referenced by the nroff commands '.so' or '.nx'.
XX *	J. Leth, IH 55414 6B-326, x6133.
XX *	(original program from: D. A. Spicer)
XX */
XX
XX#define SPACE " \t\n"
XX#define MAXLINE 500
XX
XXchar buff[MAXLINE];
XXchar line[MAXLINE];
XX
XX/* Functions: */
XXextern char *strtok();
XX/* * * * * * */
XX
XX
XXmain(argc, argv)
XX	int argc;
XX	char *argv[];
XX{
XX	FILE *inp;
XX	int i;
XX
XX	if (isatty(0)  &&  argc == 1) {
XX		/* If input is from the terminal, and there are no args,
XX		 * print usage instructions and exit.
XX		 */
XX
XX		fprintf(stderr, "Usage:\tcatso [ file or '-' ] ...\n\
XX	Cats the files (standard input, default) together, inserting\n\
XX	files referenced by the nroff commands '.so' and '.nx' in their\n\
XX	proper positions.  File name '-' means standard input\n");
XX		exit(1);
XX	};
XX
XX	if (argc == 1) {
XX		fetch(stdin);
XX	} else {
XX		for (i=1; i < argc; ++i) {
XX			if (strcmp(argv[i], "-") == 0) {
XX				fetch(stdin);
XX			} else {
XX				inp = fopen(argv[i], "r");
XX				if (inp == NULL) {
XX					fprintf(stderr,
XX						"catso: can't open file '%s'.\n",
XX							argv[i]);
XX				} else {
XX					fetch(inp);
XX				}
XX			}
XX		}
XX	}
XX}
XX
XX
XXfetch(fdes)
XX	FILE *fdes;
XX{
XX	FILE *newdes;
XX	char *cmd;
XX	char *fname;
XX
XX	while(fgets(buff,sizeof(buff),fdes) != NULL ) {
XX		strncpy(line, buff, sizeof(line));
XX		cmd = strtok(buff, SPACE);
XX		fname = strtok((char *) 0, SPACE);
XX
XX		if (strcmp(cmd, ".so") == 0) {
XX			if((newdes = fopen(fname,"r")) != NULL) {
XX				fetch(newdes);
XX				fclose(newdes);
XX			} else {
XX				fprintf(stderr, "catso: can't open .so file '%s'.\n",
XX						fname);
XX			}
XX		} else if (strcmp(cmd, ".nx") == 0) {
XX			if((newdes = fopen(fname,"r")) != NULL) {
XX				fclose(fdes);
XX				fetch(newdes);
XX				fclose(newdes);
XX			} else {
XX				fprintf(stderr, "catso: can't open .nx file '%s'.\n",
XX						fname);
XX				exit(1);
XX			}
XX			exit(0);
XX		} else {
XX			fputs(line, stdout);
XX		}
XX	}
XX}
SHAR_EOF
if test 1898 -ne "`wc -c catso.c`"
then
echo shar: error transmitting catso.c '(should have been 1898 characters)'
fi
#	End of shell archive
exit 0



More information about the Comp.unix.wizards mailing list