xref: /titanic_44/usr/src/cmd/soelim/soelim.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2 /*	  All Rights Reserved  	*/
3 
4 
5 /*
6  * Copyright (c) 1980 Regents of the University of California.
7  * All rights reserved. The Berkeley software License Agreement
8  * specifies the terms and conditions for redistribution.
9  */
10 
11 /*
12  * Copyright (c) 1983, 1984 1985, 1986, 1987, 1988, Sun Microsystems, Inc.
13  * All Rights Reserved.
14  */
15 
16 #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.1	*/
17 
18 #include <stdio.h>
19 /*
20  * soelim - a filter to process n/troff input eliminating .so's
21  *
22  * Author: Bill Joy UCB July 8, 1977
23  *
24  * This program eliminates .so's from a n/troff input stream.
25  * It can be used to prepare safe input for submission to the
26  * phototypesetter since the software supporting the operator
27  * doesn't let him do chdir.
28  *
29  * This is a kludge and the operator should be given the
30  * ability to do chdir.
31  *
32  * This program is more generally useful, it turns out, because
33  * the program tbl doesn't understand ".so" directives.
34  */
35 #define	STDIN_NAME	"-"
36 
37 main(argc, argv)
38 	int argc;
39 	char *argv[];
40 {
41 
42 	argc--;
43 	argv++;
44 	if (argc == 0) {
45 		(void)process(STDIN_NAME);
46 		exit(0);
47 	}
48 	do {
49 		(void)process(argv[0]);
50 		argv++;
51 		argc--;
52 	} while (argc > 0);
53 	exit(0);
54 }
55 
56 int process(file)
57 	char *file;
58 {
59 	register char *cp;
60 	register int c;
61 	char fname[BUFSIZ];
62 	FILE *soee;
63 	int isfile;
64 
65 	if (!strcmp(file, STDIN_NAME)) {
66 		soee = stdin;
67 	} else {
68 		soee = fopen(file, "r");
69 		if (soee == NULL) {
70 			perror(file);
71 			return(-1);
72 		}
73 	}
74 	for (;;) {
75 		c = getc(soee);
76 		if (c == EOF)
77 			break;
78 		if (c != '.')
79 			goto simple;
80 		c = getc(soee);
81 		if (c != 's') {
82 			putchar('.');
83 			goto simple;
84 		}
85 		c = getc(soee);
86 		if (c != 'o') {
87 			printf(".s");
88 			goto simple;
89 		}
90 		do
91 			c = getc(soee);
92 		while (c == ' ' || c == '\t');
93 		cp = fname;
94 		isfile = 0;
95 		for (;;) {
96 			switch (c) {
97 
98 			case ' ':
99 			case '\t':
100 			case '\n':
101 			case EOF:
102 				goto donename;
103 
104 			default:
105 				*cp++ = c;
106 				c = getc(soee);
107 				isfile++;
108 				continue;
109 			}
110 		}
111 donename:
112 		if (cp == fname) {
113 			printf(".so");
114 			goto simple;
115 		}
116 		*cp = 0;
117 		if (process(fname) < 0)
118 			if (isfile)
119 				printf(".so %s\n", fname);
120 		continue;
121 simple:
122 		if (c == EOF)
123 			break;
124 		putchar(c);
125 		if (c != '\n') {
126 			c = getc(soee);
127 			goto simple;
128 		}
129 	}
130 	if (soee != stdin) {
131 		fclose(soee);
132 	}
133 	return(0);
134 }
135