xref: /illumos-gate/usr/src/cmd/soelim/soelim.c (revision 22028508fd28d36ff74dc02c5774a8ba1f0db045)
1 /*
2  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  * Copyright (c) 2016 by Delphix. All rights reserved.
5  */
6 
7 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
8 /*	  All Rights Reserved  	*/
9 
10 
11 /*
12  * Copyright (c) 1980 Regents of the University of California.
13  * All rights reserved. The Berkeley software License Agreement
14  * specifies the terms and conditions for redistribution.
15  */
16 
17 #pragma ident	"%Z%%M%	%I%	%E% SMI"
18 
19 #include <stdio.h>
20 /*
21  * soelim - a filter to process n/troff input eliminating .so's
22  *
23  * Author: Bill Joy UCB July 8, 1977
24  *
25  * This program eliminates .so's from a n/troff input stream.
26  * It can be used to prepare safe input for submission to the
27  * phototypesetter since the software supporting the operator
28  * doesn't let them do chdir.
29  *
30  * This is a kludge and the operator should be given the
31  * ability to do chdir.
32  *
33  * This program is more generally useful, it turns out, because
34  * the program tbl doesn't understand ".so" directives.
35  */
36 #define	STDIN_NAME	"-"
37 
38 int
39 main(int argc, 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 	return (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