1 /*-
2  * Copyright (c) 1980 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1980 The Regents of the University of California.\n\
37  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 static char sccsid[] = "@(#)csfix.c	6.3 (Berkeley) 4/17/91";
42 #endif /* not lint */
43 
44 #include <stdio.h>
45 /*
46  * csfix - fix constant spacing for error message flags in troff
47  *
48  * Bill Joy UCB September 11, 1977
49  *
50  * This would be better written in snobol!
51  *
52  * Normally fixes error flags in a pi listing
53  * Optional - causes fixing of '---' and initial blank widthin a pxp profile.
54  */
55 
56 char	flag, dflag;
57 
main(argc,argv)58 main(argc, argv)
59 	int argc;
60 	char *argv[];
61 {
62 
63 	argc--, argv++;
64 	if (argc > 0 && argv[0][0] == '-' && argv[0][1] == 'd')
65 		dflag++, argc--, argv++;
66 	if (argc > 0 && argv[0][0] == '-')
67 		flag++, argc--, argv++;
68 	if (argc != 0) {
69 		write(2, "Usage: csfix\n", 13);
70 		exit(1);
71 	}
72 	while (getline()) {
73 		if (errline()) {
74 			flag ? fixpxp() : reformat();
75 			continue;
76 		}
77 		if (flag) {
78 			fixdigits();
79 			continue;
80 		}
81 		if (spwarn())
82 			continue;
83 		if (nontriv())
84 			save();
85 		if (dflag)
86 			fixdigits();
87 		else
88 			putline();
89 	}
90 	exit(0);
91 }
92 
93 char	line[160], flagee[160], *digitty();
94 
getline()95 getline()
96 {
97 	register char *cp, c;
98 
99 	for (cp = line, c = getchar(); c != '\n' && c != EOF; c = getchar())
100 		*cp++ = c;
101 	if (c == EOF)
102 		return (0);
103 	*cp = 0;
104 	return (1);
105 }
106 
errline()107 errline()
108 {
109 	register int i;
110 	register char *cp;
111 
112 	for (cp = line; cp[0] && cp[1] && cp[2]; cp++)
113 		if (cp[0] == '-' && cp[1] == '-' && cp[2] == '-')
114 			return (1);
115 	return (0);
116 }
117 
reformat()118 reformat()
119 {
120 	register char *cp, c, *tail;
121 
122 	printf("%2.2s", line);
123 	if (line[0] != 'w')
124 		printf("\\l'\\w`w `u-\\w`%2.2s`u '", line);
125 	for (cp = line; *cp != 0 && *cp != '^'; cp++)
126 		continue;
127 	tail = cp + 1;
128 	if (cp[-1] == '\b' && cp[-2] == '|')
129 		cp -= 2;
130 	c = flagee[cp - line];
131 	flagee[cp - line] = 0;
132 	printf("\\l'\\w`%s`u-\\w`w `u\\&\\(rh'", flagee);
133 	flagee[cp - line] = c;
134 	if (c == '\0')
135 		c = flagee[cp - line - 1];
136 	printf("\\l'(\\w`%c`u-\\w`^`u)/2 '", c);
137 	printf("\\(ua");
138 	printf("\\l'(\\w`%c`u-\\w`^`u)/2 '", c);
139 	printf("\\l'\\w`---`u\\&\\(rh'%s\n", tail+3);
140 }
141 
nontriv()142 nontriv()
143 {
144 
145 	switch (line[0]) {
146 		case 'E':
147 		case 'e':
148 		case 'w':
149 		case 's':
150 		case 0:
151 			return (0);
152 	}
153 	return (1);
154 }
155 
save()156 save()
157 {
158 
159 	strcpy(flagee, line);
160 }
161 
putline()162 putline()
163 {
164 
165 	printf("%s\n", flag ? digitty(0) : line);
166 }
167 
spwarn()168 spwarn()
169 {
170 
171 	if (line[0] != ' ' || line[1] != ' ' || line[2] != 'w')
172 		return (0);
173 	printf("  \\l'(\\w`E`u-\\w`w`u)/2 'w\\l'(\\w`E`u-\\w`w`u)/2 '");
174 	printf(&line[3]);
175 	printf("\n");
176 	return (1);
177 }
178 
fixpxp()179 fixpxp()
180 {
181 	register char *cp;
182 
183 	for (cp = line; *cp != '-'; cp++)
184 		continue;
185 	*cp = 0;
186 	printf("%s\\l'\\w`\\0\\0\\0\\0`u-\\w`.`u\\&\\(rh'%s\n", digitty(1), cp + 3);
187 }
188 
189 char *
digitty(yup)190 digitty(yup)
191 	char yup;
192 {
193 	register char *cp, *dp, *lp;
194 
195 	for (lp = line; *lp && *lp != '|'; lp++)
196 		continue;
197 	if (yup == 0 && !*lp)
198 		return (line);
199 	for (cp = line, dp = flagee; cp < lp; cp++)
200 		if (*cp == ' ')
201 			*dp++ = '\\', *dp++ = '0';
202 		else
203 			*dp++ = *cp;
204 	strcpy(dp, cp);
205 	return (flagee);
206 }
207 
fixdigits()208 fixdigits()
209 {
210 	register char *cp, c;
211 
212 	for (cp = line; *cp == ' ' || *cp >= '0' && *cp <= '9'; cp++)
213 		continue;
214 	c = *cp, *cp = 0;
215 	digitty(1);
216 	*cp = c;
217 	printf("%s%s\n", flagee, cp);
218 }
219