xref: /illumos-gate/usr/src/cmd/soelim/soelim.c (revision 20a7641f9918de8574b8b3b47dbe35c4bfc78df1)
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 #include <stdio.h>
18 /*
19  * soelim - a filter to process n/troff input eliminating .so's
20  *
21  * Author: Bill Joy UCB July 8, 1977
22  *
23  * This program eliminates .so's from a n/troff input stream.
24  * It can be used to prepare safe input for submission to the
25  * phototypesetter since the software supporting the operator
26  * doesn't let them do chdir.
27  *
28  * This is a kludge and the operator should be given the
29  * ability to do chdir.
30  *
31  * This program is more generally useful, it turns out, because
32  * the program tbl doesn't understand ".so" directives.
33  */
34 #define	STDIN_NAME	"-"
35 
36 int
37 main(int argc, char *argv[])
38 {
39 
40 	argc--;
41 	argv++;
42 	if (argc == 0) {
43 		(void)process(STDIN_NAME);
44 		exit(0);
45 	}
46 	do {
47 		(void)process(argv[0]);
48 		argv++;
49 		argc--;
50 	} while (argc > 0);
51 	return (0);
52 }
53 
54 int process(file)
55 	char *file;
56 {
57 	register char *cp;
58 	register int c;
59 	char fname[BUFSIZ];
60 	FILE *soee;
61 	int isfile;
62 
63 	if (!strcmp(file, STDIN_NAME)) {
64 		soee = stdin;
65 	} else {
66 		soee = fopen(file, "r");
67 		if (soee == NULL) {
68 			perror(file);
69 			return(-1);
70 		}
71 	}
72 	for (;;) {
73 		c = getc(soee);
74 		if (c == EOF)
75 			break;
76 		if (c != '.')
77 			goto simple;
78 		c = getc(soee);
79 		if (c != 's') {
80 			putchar('.');
81 			goto simple;
82 		}
83 		c = getc(soee);
84 		if (c != 'o') {
85 			printf(".s");
86 			goto simple;
87 		}
88 		do
89 			c = getc(soee);
90 		while (c == ' ' || c == '\t');
91 		cp = fname;
92 		isfile = 0;
93 		for (;;) {
94 			switch (c) {
95 
96 			case ' ':
97 			case '\t':
98 			case '\n':
99 			case EOF:
100 				goto donename;
101 
102 			default:
103 				*cp++ = c;
104 				c = getc(soee);
105 				isfile++;
106 				continue;
107 			}
108 		}
109 donename:
110 		if (cp == fname) {
111 			printf(".so");
112 			goto simple;
113 		}
114 		*cp = 0;
115 		if (process(fname) < 0)
116 			if (isfile)
117 				printf(".so %s\n", fname);
118 		continue;
119 simple:
120 		if (c == EOF)
121 			break;
122 		putchar(c);
123 		if (c != '\n') {
124 			c = getc(soee);
125 			goto simple;
126 		}
127 	}
128 	if (soee != stdin) {
129 		fclose(soee);
130 	}
131 	return(0);
132 }
133