xref: /titanic_51/usr/src/cmd/soelim/soelim.c (revision 5a7763bf3e9db4cfe6cb523b096cb74af71e3793)
1 /*
2  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
7 /*	  All Rights Reserved  	*/
8 
9 
10 /*
11  * Copyright (c) 1980 Regents of the University of California.
12  * All rights reserved. The Berkeley software License Agreement
13  * specifies the terms and conditions for redistribution.
14  */
15 
16 #pragma ident	"%Z%%M%	%I%	%E% SMI"
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 int
38 main(int argc, char *argv[])
39 {
40 
41 	argc--;
42 	argv++;
43 	if (argc == 0) {
44 		(void)process(STDIN_NAME);
45 		exit(0);
46 	}
47 	do {
48 		(void)process(argv[0]);
49 		argv++;
50 		argc--;
51 	} while (argc > 0);
52 	return (0);
53 }
54 
55 int process(file)
56 	char *file;
57 {
58 	register char *cp;
59 	register int c;
60 	char fname[BUFSIZ];
61 	FILE *soee;
62 	int isfile;
63 
64 	if (!strcmp(file, STDIN_NAME)) {
65 		soee = stdin;
66 	} else {
67 		soee = fopen(file, "r");
68 		if (soee == NULL) {
69 			perror(file);
70 			return(-1);
71 		}
72 	}
73 	for (;;) {
74 		c = getc(soee);
75 		if (c == EOF)
76 			break;
77 		if (c != '.')
78 			goto simple;
79 		c = getc(soee);
80 		if (c != 's') {
81 			putchar('.');
82 			goto simple;
83 		}
84 		c = getc(soee);
85 		if (c != 'o') {
86 			printf(".s");
87 			goto simple;
88 		}
89 		do
90 			c = getc(soee);
91 		while (c == ' ' || c == '\t');
92 		cp = fname;
93 		isfile = 0;
94 		for (;;) {
95 			switch (c) {
96 
97 			case ' ':
98 			case '\t':
99 			case '\n':
100 			case EOF:
101 				goto donename;
102 
103 			default:
104 				*cp++ = c;
105 				c = getc(soee);
106 				isfile++;
107 				continue;
108 			}
109 		}
110 donename:
111 		if (cp == fname) {
112 			printf(".so");
113 			goto simple;
114 		}
115 		*cp = 0;
116 		if (process(fname) < 0)
117 			if (isfile)
118 				printf(".so %s\n", fname);
119 		continue;
120 simple:
121 		if (c == EOF)
122 			break;
123 		putchar(c);
124 		if (c != '\n') {
125 			c = getc(soee);
126 			goto simple;
127 		}
128 	}
129 	if (soee != stdin) {
130 		fclose(soee);
131 	}
132 	return(0);
133 }
134