xref: /onnv-gate/usr/src/cmd/soelim/soelim.c (revision 644:bca1986a1faa)
1*644Sakaplan /*
2*644Sakaplan  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3*644Sakaplan  * Use is subject to license terms.
4*644Sakaplan  */
5*644Sakaplan 
60Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
70Sstevel@tonic-gate /*	  All Rights Reserved  	*/
80Sstevel@tonic-gate 
90Sstevel@tonic-gate 
100Sstevel@tonic-gate /*
110Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
120Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
130Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
140Sstevel@tonic-gate  */
15*644Sakaplan 
16*644Sakaplan #pragma ident	"%Z%%M%	%I%	%E% SMI"
170Sstevel@tonic-gate 
180Sstevel@tonic-gate #include <stdio.h>
190Sstevel@tonic-gate /*
200Sstevel@tonic-gate  * soelim - a filter to process n/troff input eliminating .so's
210Sstevel@tonic-gate  *
220Sstevel@tonic-gate  * Author: Bill Joy UCB July 8, 1977
230Sstevel@tonic-gate  *
240Sstevel@tonic-gate  * This program eliminates .so's from a n/troff input stream.
250Sstevel@tonic-gate  * It can be used to prepare safe input for submission to the
260Sstevel@tonic-gate  * phototypesetter since the software supporting the operator
270Sstevel@tonic-gate  * doesn't let him do chdir.
280Sstevel@tonic-gate  *
290Sstevel@tonic-gate  * This is a kludge and the operator should be given the
300Sstevel@tonic-gate  * ability to do chdir.
310Sstevel@tonic-gate  *
320Sstevel@tonic-gate  * This program is more generally useful, it turns out, because
330Sstevel@tonic-gate  * the program tbl doesn't understand ".so" directives.
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate #define	STDIN_NAME	"-"
360Sstevel@tonic-gate 
37*644Sakaplan int
main(int argc,char * argv[])38*644Sakaplan main(int argc, char *argv[])
390Sstevel@tonic-gate {
400Sstevel@tonic-gate 
410Sstevel@tonic-gate 	argc--;
420Sstevel@tonic-gate 	argv++;
430Sstevel@tonic-gate 	if (argc == 0) {
440Sstevel@tonic-gate 		(void)process(STDIN_NAME);
450Sstevel@tonic-gate 		exit(0);
460Sstevel@tonic-gate 	}
470Sstevel@tonic-gate 	do {
480Sstevel@tonic-gate 		(void)process(argv[0]);
490Sstevel@tonic-gate 		argv++;
500Sstevel@tonic-gate 		argc--;
510Sstevel@tonic-gate 	} while (argc > 0);
52*644Sakaplan 	return (0);
530Sstevel@tonic-gate }
540Sstevel@tonic-gate 
process(file)550Sstevel@tonic-gate int process(file)
560Sstevel@tonic-gate 	char *file;
570Sstevel@tonic-gate {
580Sstevel@tonic-gate 	register char *cp;
590Sstevel@tonic-gate 	register int c;
600Sstevel@tonic-gate 	char fname[BUFSIZ];
610Sstevel@tonic-gate 	FILE *soee;
620Sstevel@tonic-gate 	int isfile;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	if (!strcmp(file, STDIN_NAME)) {
650Sstevel@tonic-gate 		soee = stdin;
660Sstevel@tonic-gate 	} else {
670Sstevel@tonic-gate 		soee = fopen(file, "r");
680Sstevel@tonic-gate 		if (soee == NULL) {
690Sstevel@tonic-gate 			perror(file);
700Sstevel@tonic-gate 			return(-1);
710Sstevel@tonic-gate 		}
720Sstevel@tonic-gate 	}
730Sstevel@tonic-gate 	for (;;) {
740Sstevel@tonic-gate 		c = getc(soee);
750Sstevel@tonic-gate 		if (c == EOF)
760Sstevel@tonic-gate 			break;
770Sstevel@tonic-gate 		if (c != '.')
780Sstevel@tonic-gate 			goto simple;
790Sstevel@tonic-gate 		c = getc(soee);
800Sstevel@tonic-gate 		if (c != 's') {
810Sstevel@tonic-gate 			putchar('.');
820Sstevel@tonic-gate 			goto simple;
830Sstevel@tonic-gate 		}
840Sstevel@tonic-gate 		c = getc(soee);
850Sstevel@tonic-gate 		if (c != 'o') {
860Sstevel@tonic-gate 			printf(".s");
870Sstevel@tonic-gate 			goto simple;
880Sstevel@tonic-gate 		}
890Sstevel@tonic-gate 		do
900Sstevel@tonic-gate 			c = getc(soee);
910Sstevel@tonic-gate 		while (c == ' ' || c == '\t');
920Sstevel@tonic-gate 		cp = fname;
930Sstevel@tonic-gate 		isfile = 0;
940Sstevel@tonic-gate 		for (;;) {
950Sstevel@tonic-gate 			switch (c) {
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 			case ' ':
980Sstevel@tonic-gate 			case '\t':
990Sstevel@tonic-gate 			case '\n':
1000Sstevel@tonic-gate 			case EOF:
1010Sstevel@tonic-gate 				goto donename;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 			default:
1040Sstevel@tonic-gate 				*cp++ = c;
1050Sstevel@tonic-gate 				c = getc(soee);
1060Sstevel@tonic-gate 				isfile++;
1070Sstevel@tonic-gate 				continue;
1080Sstevel@tonic-gate 			}
1090Sstevel@tonic-gate 		}
1100Sstevel@tonic-gate donename:
1110Sstevel@tonic-gate 		if (cp == fname) {
1120Sstevel@tonic-gate 			printf(".so");
1130Sstevel@tonic-gate 			goto simple;
1140Sstevel@tonic-gate 		}
1150Sstevel@tonic-gate 		*cp = 0;
1160Sstevel@tonic-gate 		if (process(fname) < 0)
1170Sstevel@tonic-gate 			if (isfile)
1180Sstevel@tonic-gate 				printf(".so %s\n", fname);
1190Sstevel@tonic-gate 		continue;
1200Sstevel@tonic-gate simple:
1210Sstevel@tonic-gate 		if (c == EOF)
1220Sstevel@tonic-gate 			break;
1230Sstevel@tonic-gate 		putchar(c);
1240Sstevel@tonic-gate 		if (c != '\n') {
1250Sstevel@tonic-gate 			c = getc(soee);
1260Sstevel@tonic-gate 			goto simple;
1270Sstevel@tonic-gate 		}
1280Sstevel@tonic-gate 	}
1290Sstevel@tonic-gate 	if (soee != stdin) {
1300Sstevel@tonic-gate 		fclose(soee);
1310Sstevel@tonic-gate 	}
1320Sstevel@tonic-gate 	return(0);
1330Sstevel@tonic-gate }
134