xref: /original-bsd/usr.bin/soelim/soelim.c (revision 92d853e2)
1 /*
2  * Copyright (c) 1980 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 char copyright[] =
20 "@(#) Copyright (c) 1980 The Regents of the University of California.\n\
21  All rights reserved.\n";
22 #endif /* not lint */
23 
24 #ifndef lint
25 static char sccsid[] = "@(#)soelim.c	5.2 (Berkeley) 10/26/88";
26 #endif /* not lint */
27 
28 #include <stdio.h>
29 /*
30  * soelim - a filter to process n/troff input eliminating .so's
31  *
32  * Author: Bill Joy UCB July 8, 1977
33  *
34  * This program eliminates .so's from a n/troff input stream.
35  * It can be used to prepare safe input for submission to the
36  * phototypesetter since the software supporting the operator
37  * doesn't let him do chdir.
38  *
39  * This is a kludge and the operator should be given the
40  * ability to do chdir.
41  *
42  * This program is more generally useful, it turns out, because
43  * the program tbl doesn't understand ".so" directives.
44  */
45 #define	STDIN_NAME	"-"
46 
47 main(argc, argv)
48 	int argc;
49 	char *argv[];
50 {
51 
52 	argc--;
53 	argv++;
54 	if (argc == 0) {
55 		(void)process(STDIN_NAME);
56 		exit(0);
57 	}
58 	do {
59 		(void)process(argv[0]);
60 		argv++;
61 		argc--;
62 	} while (argc > 0);
63 	exit(0);
64 }
65 
66 int process(file)
67 	char *file;
68 {
69 	register char *cp;
70 	register int c;
71 	char fname[BUFSIZ];
72 	FILE *soee;
73 	int isfile;
74 
75 	if (!strcmp(file, STDIN_NAME)) {
76 		soee = stdin;
77 	} else {
78 		soee = fopen(file, "r");
79 		if (soee == NULL) {
80 			perror(file);
81 			return(-1);
82 		}
83 	}
84 	for (;;) {
85 		c = getc(soee);
86 		if (c == EOF)
87 			break;
88 		if (c != '.')
89 			goto simple;
90 		c = getc(soee);
91 		if (c != 's') {
92 			putchar('.');
93 			goto simple;
94 		}
95 		c = getc(soee);
96 		if (c != 'o') {
97 			printf(".s");
98 			goto simple;
99 		}
100 		do
101 			c = getc(soee);
102 		while (c == ' ' || c == '\t');
103 		cp = fname;
104 		isfile = 0;
105 		for (;;) {
106 			switch (c) {
107 
108 			case ' ':
109 			case '\t':
110 			case '\n':
111 			case EOF:
112 				goto donename;
113 
114 			default:
115 				*cp++ = c;
116 				c = getc(soee);
117 				isfile++;
118 				continue;
119 			}
120 		}
121 donename:
122 		if (cp == fname) {
123 			printf(".so");
124 			goto simple;
125 		}
126 		*cp = 0;
127 		if (process(fname) < 0)
128 			if (isfile)
129 				printf(".so %s\n", fname);
130 		continue;
131 simple:
132 		if (c == EOF)
133 			break;
134 		putchar(c);
135 		if (c != '\n') {
136 			c = getc(soee);
137 			goto simple;
138 		}
139 	}
140 	if (soee != stdin) {
141 		fclose(soee);
142 	}
143 	return(0);
144 }
145