xref: /original-bsd/usr.bin/soelim/soelim.c (revision 8ac030d2)
1 static char *sccsid = "@(#)soelim.c	4.2 (Berkeley) 09/16/82";
2 
3 #include <stdio.h>
4 /*
5  * soelim - a filter to process n/troff input eliminating .so's
6  *
7  * Author: Bill Joy UCB July 8, 1977
8  *
9  * This program eliminates .so's from a n/troff input stream.
10  * It can be used to prepare safe input for submission to the
11  * phototypesetter since the software supporting the operator
12  * doesn't let him do chdir.
13  *
14  * This is a kludge and the operator should be given the
15  * ability to do chdir.
16  *
17  * This program is more generally useful, it turns out, because
18  * the program tbl doesn't understand ".so" directives.
19  */
20 #define	STDIN_NAME	"-"
21 
22 main(argc, argv)
23 	int argc;
24 	char *argv[];
25 {
26 
27 	argc--;
28 	argv++;
29 	if (argc == 0) {
30 		process(STDIN_NAME);
31 		exit(0);
32 	}
33 	do {
34 		process(argv[0]);
35 		argv++;
36 		argc--;
37 	} while (argc > 0);
38 	exit(0);
39 }
40 
41 process(file)
42 	char *file;
43 {
44 	register char *cp;
45 	register int c;
46 	char fname[BUFSIZ];
47 	FILE *soee;
48 
49 	if (!strcmp(file, STDIN_NAME)) {
50 		soee = stdin;
51 	} else {
52 		soee = fopen(file, "r");
53 		if (soee == NULL) {
54 			perror(file);
55 			return;
56 		}
57 	}
58 	for (;;) {
59 		c = getc(soee);
60 		if (c < 0)
61 			break;
62 		if (c != '.')
63 			goto simple;
64 		c = getc(soee);
65 		if (c != 's') {
66 			putchar('.');
67 			goto simple;
68 		}
69 		c = getc(soee);
70 		if (c != 'o') {
71 			printf(".s");
72 			goto simple;
73 		}
74 		do
75 			c = getc(soee);
76 		while (c == ' ' || c == '\t');
77 		cp = fname;
78 		for (;;) {
79 			switch (c) {
80 
81 			case ' ':
82 			case '\t':
83 			case '\n':
84 			case EOF:
85 				goto donename;
86 
87 			default:
88 				*cp++ = c;
89 				c = getc(soee);
90 				continue;
91 			}
92 		}
93 donename:
94 		if (cp == fname) {
95 			printf(".so");
96 			goto simple;
97 		}
98 		*cp++ = 0;
99 		process(fname);
100 		continue;
101 simple:
102 		if (c == EOF)
103 			break;
104 		putchar(c);
105 	}
106 	if (soee != stdin) {
107 		fclose(soee);
108 	}
109 }
110