xref: /netbsd-src/usr.bin/soelim/soelim.c (revision 4d7e773266e3c3f48566c86c0ad52d51c6454fd1)
1 /*	$NetBSD: soelim.c,v 1.4 1997/10/19 23:25:51 lukem Exp $	*/
2 
3 /*
4  * Copyright (c) 1980, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
39 	The Regents of the University of California.  All rights reserved.\n");
40 #endif /* not lint */
41 
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)soelim.c	8.1 (Berkeley) 6/6/93";
45 #endif
46 __RCSID("$NetBSD: soelim.c,v 1.4 1997/10/19 23:25:51 lukem Exp $");
47 #endif /* not lint */
48 
49 #include <stdio.h>
50 /*
51  * soelim - a filter to process n/troff input eliminating .so's
52  *
53  * Author: Bill Joy UCB July 8, 1977
54  *
55  * This program eliminates .so's from a n/troff input stream.
56  * It can be used to prepare safe input for submission to the
57  * phototypesetter since the software supporting the operator
58  * doesn't let him do chdir.
59  *
60  * This is a kludge and the operator should be given the
61  * ability to do chdir.
62  *
63  * This program is more generally useful, it turns out, because
64  * the program tbl doesn't understand ".so" directives.
65  */
66 #define	STDIN_NAME	"-"
67 
68 int	main __P((int, char **));
69 int	process __P((char *));
70 
71 int
72 main(argc, argv)
73 	int argc;
74 	char *argv[];
75 {
76 
77 	argc--;
78 	argv++;
79 	if (argc == 0) {
80 		(void)process(STDIN_NAME);
81 		exit(0);
82 	}
83 	do {
84 		(void)process(argv[0]);
85 		argv++;
86 		argc--;
87 	} while (argc > 0);
88 	exit(0);
89 }
90 
91 int
92 process(file)
93 	char *file;
94 {
95 	char *cp;
96 	int c;
97 	char fname[BUFSIZ];
98 	FILE *soee;
99 	int isfile;
100 
101 	if (!strcmp(file, STDIN_NAME)) {
102 		soee = stdin;
103 	} else {
104 		soee = fopen(file, "r");
105 		if (soee == NULL) {
106 			perror(file);
107 			return(-1);
108 		}
109 	}
110 	for (;;) {
111 		c = getc(soee);
112 		if (c == EOF)
113 			break;
114 		if (c != '.')
115 			goto simple;
116 		c = getc(soee);
117 		if (c != 's') {
118 			putchar('.');
119 			goto simple;
120 		}
121 		c = getc(soee);
122 		if (c != 'o') {
123 			printf(".s");
124 			goto simple;
125 		}
126 		do
127 			c = getc(soee);
128 		while (c == ' ' || c == '\t');
129 		cp = fname;
130 		isfile = 0;
131 		for (;;) {
132 			switch (c) {
133 
134 			case ' ':
135 			case '\t':
136 			case '\n':
137 			case EOF:
138 				goto donename;
139 
140 			default:
141 				*cp++ = c;
142 				c = getc(soee);
143 				isfile++;
144 				continue;
145 			}
146 		}
147 donename:
148 		if (cp == fname) {
149 			printf(".so");
150 			goto simple;
151 		}
152 		*cp = 0;
153 		if (process(fname) < 0)
154 			if (isfile)
155 				printf(".so %s\n", fname);
156 		continue;
157 simple:
158 		if (c == EOF)
159 			break;
160 		putchar(c);
161 		if (c != '\n') {
162 			c = getc(soee);
163 			goto simple;
164 		}
165 	}
166 	if (soee != stdin) {
167 		fclose(soee);
168 	}
169 	return(0);
170 }
171