xref: /illumos-gate/usr/src/cmd/soelim/soelim.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
1113f4232Sakaplan /*
2113f4232Sakaplan  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3113f4232Sakaplan  * Use is subject to license terms.
4*48bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
5113f4232Sakaplan  */
6113f4232Sakaplan 
77c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
87c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
97c478bd9Sstevel@tonic-gate 
107c478bd9Sstevel@tonic-gate 
117c478bd9Sstevel@tonic-gate /*
127c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
137c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
147c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
157c478bd9Sstevel@tonic-gate  */
167c478bd9Sstevel@tonic-gate 
177c478bd9Sstevel@tonic-gate #include <stdio.h>
187c478bd9Sstevel@tonic-gate /*
197c478bd9Sstevel@tonic-gate  * soelim - a filter to process n/troff input eliminating .so's
207c478bd9Sstevel@tonic-gate  *
217c478bd9Sstevel@tonic-gate  * Author: Bill Joy UCB July 8, 1977
227c478bd9Sstevel@tonic-gate  *
237c478bd9Sstevel@tonic-gate  * This program eliminates .so's from a n/troff input stream.
247c478bd9Sstevel@tonic-gate  * It can be used to prepare safe input for submission to the
257c478bd9Sstevel@tonic-gate  * phototypesetter since the software supporting the operator
26*48bbca81SDaniel Hoffman  * doesn't let them do chdir.
277c478bd9Sstevel@tonic-gate  *
287c478bd9Sstevel@tonic-gate  * This is a kludge and the operator should be given the
297c478bd9Sstevel@tonic-gate  * ability to do chdir.
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * This program is more generally useful, it turns out, because
327c478bd9Sstevel@tonic-gate  * the program tbl doesn't understand ".so" directives.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate #define	STDIN_NAME	"-"
357c478bd9Sstevel@tonic-gate 
36113f4232Sakaplan int
main(int argc,char * argv[])37113f4232Sakaplan main(int argc, char *argv[])
387c478bd9Sstevel@tonic-gate {
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate 	argc--;
417c478bd9Sstevel@tonic-gate 	argv++;
427c478bd9Sstevel@tonic-gate 	if (argc == 0) {
437c478bd9Sstevel@tonic-gate 		(void)process(STDIN_NAME);
447c478bd9Sstevel@tonic-gate 		exit(0);
457c478bd9Sstevel@tonic-gate 	}
467c478bd9Sstevel@tonic-gate 	do {
477c478bd9Sstevel@tonic-gate 		(void)process(argv[0]);
487c478bd9Sstevel@tonic-gate 		argv++;
497c478bd9Sstevel@tonic-gate 		argc--;
507c478bd9Sstevel@tonic-gate 	} while (argc > 0);
51113f4232Sakaplan 	return (0);
527c478bd9Sstevel@tonic-gate }
537c478bd9Sstevel@tonic-gate 
process(file)547c478bd9Sstevel@tonic-gate int process(file)
557c478bd9Sstevel@tonic-gate 	char *file;
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate 	register char *cp;
587c478bd9Sstevel@tonic-gate 	register int c;
597c478bd9Sstevel@tonic-gate 	char fname[BUFSIZ];
607c478bd9Sstevel@tonic-gate 	FILE *soee;
617c478bd9Sstevel@tonic-gate 	int isfile;
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 	if (!strcmp(file, STDIN_NAME)) {
647c478bd9Sstevel@tonic-gate 		soee = stdin;
657c478bd9Sstevel@tonic-gate 	} else {
667c478bd9Sstevel@tonic-gate 		soee = fopen(file, "r");
677c478bd9Sstevel@tonic-gate 		if (soee == NULL) {
687c478bd9Sstevel@tonic-gate 			perror(file);
697c478bd9Sstevel@tonic-gate 			return(-1);
707c478bd9Sstevel@tonic-gate 		}
717c478bd9Sstevel@tonic-gate 	}
727c478bd9Sstevel@tonic-gate 	for (;;) {
737c478bd9Sstevel@tonic-gate 		c = getc(soee);
747c478bd9Sstevel@tonic-gate 		if (c == EOF)
757c478bd9Sstevel@tonic-gate 			break;
767c478bd9Sstevel@tonic-gate 		if (c != '.')
777c478bd9Sstevel@tonic-gate 			goto simple;
787c478bd9Sstevel@tonic-gate 		c = getc(soee);
797c478bd9Sstevel@tonic-gate 		if (c != 's') {
807c478bd9Sstevel@tonic-gate 			putchar('.');
817c478bd9Sstevel@tonic-gate 			goto simple;
827c478bd9Sstevel@tonic-gate 		}
837c478bd9Sstevel@tonic-gate 		c = getc(soee);
847c478bd9Sstevel@tonic-gate 		if (c != 'o') {
857c478bd9Sstevel@tonic-gate 			printf(".s");
867c478bd9Sstevel@tonic-gate 			goto simple;
877c478bd9Sstevel@tonic-gate 		}
887c478bd9Sstevel@tonic-gate 		do
897c478bd9Sstevel@tonic-gate 			c = getc(soee);
907c478bd9Sstevel@tonic-gate 		while (c == ' ' || c == '\t');
917c478bd9Sstevel@tonic-gate 		cp = fname;
927c478bd9Sstevel@tonic-gate 		isfile = 0;
937c478bd9Sstevel@tonic-gate 		for (;;) {
947c478bd9Sstevel@tonic-gate 			switch (c) {
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 			case ' ':
977c478bd9Sstevel@tonic-gate 			case '\t':
987c478bd9Sstevel@tonic-gate 			case '\n':
997c478bd9Sstevel@tonic-gate 			case EOF:
1007c478bd9Sstevel@tonic-gate 				goto donename;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 			default:
1037c478bd9Sstevel@tonic-gate 				*cp++ = c;
1047c478bd9Sstevel@tonic-gate 				c = getc(soee);
1057c478bd9Sstevel@tonic-gate 				isfile++;
1067c478bd9Sstevel@tonic-gate 				continue;
1077c478bd9Sstevel@tonic-gate 			}
1087c478bd9Sstevel@tonic-gate 		}
1097c478bd9Sstevel@tonic-gate donename:
1107c478bd9Sstevel@tonic-gate 		if (cp == fname) {
1117c478bd9Sstevel@tonic-gate 			printf(".so");
1127c478bd9Sstevel@tonic-gate 			goto simple;
1137c478bd9Sstevel@tonic-gate 		}
1147c478bd9Sstevel@tonic-gate 		*cp = 0;
1157c478bd9Sstevel@tonic-gate 		if (process(fname) < 0)
1167c478bd9Sstevel@tonic-gate 			if (isfile)
1177c478bd9Sstevel@tonic-gate 				printf(".so %s\n", fname);
1187c478bd9Sstevel@tonic-gate 		continue;
1197c478bd9Sstevel@tonic-gate simple:
1207c478bd9Sstevel@tonic-gate 		if (c == EOF)
1217c478bd9Sstevel@tonic-gate 			break;
1227c478bd9Sstevel@tonic-gate 		putchar(c);
1237c478bd9Sstevel@tonic-gate 		if (c != '\n') {
1247c478bd9Sstevel@tonic-gate 			c = getc(soee);
1257c478bd9Sstevel@tonic-gate 			goto simple;
1267c478bd9Sstevel@tonic-gate 		}
1277c478bd9Sstevel@tonic-gate 	}
1287c478bd9Sstevel@tonic-gate 	if (soee != stdin) {
1297c478bd9Sstevel@tonic-gate 		fclose(soee);
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate 	return(0);
1327c478bd9Sstevel@tonic-gate }
133