xref: /netbsd/usr.sbin/lpr/filters/lpf.c (revision c4a72b64)
1 /*	$NetBSD: lpf.c,v 1.10 2002/07/14 15:27:59 wiz Exp $	*/
2 /*
3  * Copyright (c) 1983, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by the University of
17  *	California, Berkeley and its contributors.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 #ifndef lint
37 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
38 	The Regents of the University of California.  All rights reserved.\n");
39 #if 0
40 static char sccsid[] = "@(#)lpf.c	8.1 (Berkeley) 6/6/93";
41 #else
42 __RCSID("$NetBSD: lpf.c,v 1.10 2002/07/14 15:27:59 wiz Exp $");
43 #endif
44 #endif /* not lint */
45 
46 /*
47  * 	filter which reads the output of nroff and converts lines
48  *	with ^H's to overwritten lines.  Thus this works like 'ul'
49  *	but is much better: it can handle more than 2 overwrites
50  *	and it is written with some style.
51  *	modified by kls to use register references instead of arrays
52  *	to try to gain a little speed.
53  */
54 
55 #include <signal.h>
56 #include <string.h>
57 #include <unistd.h>
58 #include <stdlib.h>
59 #include <stdio.h>
60 
61 #define MAXWIDTH  132
62 #define MAXREP    10
63 
64 char	buf[MAXREP][MAXWIDTH];
65 int	maxcol[MAXREP] = {-1};
66 int	lineno;
67 int	width = 132;	/* default line length */
68 int	length = 66;	/* page length */
69 int	indent;		/* indentation length */
70 int	npages = 1;
71 int	literal;	/* print control characters */
72 char	*name;		/* user's login name */
73 char	*host;		/* user's machine name */
74 char	*acctfile;	/* accounting information file */
75 int	crnl;		/* \n -> \r\n */
76 int	need_cr;
77 
78 int main(int, char *[]);
79 void usage(void);
80 
81 int
82 main(int argc, char *argv[])
83 {
84 	FILE *p = stdin, *o = stdout;
85 	int i, col;
86 	char *cp;
87 	int done, linedone, maxrep, ch, prch;
88 	char *limit;
89 
90         while ((ch = getopt(argc, argv, "cfh:i:l:n:w:")) != -1)
91 		switch (ch) {
92 		case 'n':
93 			name = optarg;
94 			break;
95 		case 'h':
96 			host = optarg;
97 			break;
98 		case 'w':
99 			if ((i = atoi(optarg)) > 0 && i <= MAXWIDTH)
100 				width = i;
101 			break;
102 		case 'l':
103 			length = atoi(optarg);
104 			break;
105 		case 'i':
106 			indent = atoi(optarg);
107 			break;
108 		case 'c':	/* Print control chars */
109 			literal++;
110 			break;
111 		case 'f':	/* Fix missing carriage returns */
112 			crnl++;
113 			break;
114 		default:
115 			usage();
116 		}
117 	argc -= optind;
118 	argv += optind;
119 	if (argc)
120 		acctfile = *argv;
121 
122 	memset(buf, ' ',  sizeof(buf));
123 	done = 0;
124 
125 	while (!done) {
126 		col = indent;
127 		maxrep = -1;
128 		linedone = 0;
129 		prch = ch = 0;
130 		need_cr = 0;
131 		while (!linedone) {
132 			prch = ch;
133 			switch (ch = getc(p)) {
134 			case EOF:
135 				linedone = done = 1;
136 				ch = '\n';
137 				break;
138 
139 			case '\f':
140 				lineno = length;
141 			case '\n':
142 				if (crnl && prch != '\r')
143 					need_cr = 1;
144 				if (maxrep < 0)
145 					maxrep = 0;
146 				linedone = 1;
147 				break;
148 
149 			case '\b':
150 				if (--col < indent)
151 					col = indent;
152 				break;
153 
154 			case '\r':
155 				col = indent;
156 				break;
157 
158 			case '\t':
159 				col = ((col - indent) | 07) + indent + 1;
160 				break;
161 
162 			case '\031':
163 				/*
164 				 * lpd needs to use a different filter to
165 				 * print data so stop what we are doing and
166 				 * wait for lpd to restart us.
167 				 */
168 				if ((ch = getchar()) == '\1') {
169 					fflush(stdout);
170 					kill(getpid(), SIGSTOP);
171 					break;
172 				} else {
173 					ungetc(ch, stdin);
174 					ch = '\031';
175 				}
176 
177 			default:
178 				if (col >= width || (!literal && ch < ' ')) {
179 					col++;
180 					break;
181 				}
182 				cp = &buf[0][col];
183 				for (i = 0; i < MAXREP; i++) {
184 					if (i > maxrep)
185 						maxrep = i;
186 					if (*cp == ' ') {
187 						*cp = ch;
188 						if (col > maxcol[i])
189 							maxcol[i] = col;
190 						break;
191 					}
192 					cp += MAXWIDTH;
193 				}
194 				col++;
195 				break;
196 			}
197 		}
198 
199 		/* print out lines */
200 		for (i = 0; i <= maxrep; i++) {
201 			for (cp = buf[i], limit = cp+maxcol[i]; cp <= limit;) {
202 				putc(*cp, o);
203 				*cp++ = ' ';
204 			}
205 			if (i < maxrep)
206 				putc('\r', o);
207 			else {
208 				if (need_cr)
209 					putc('\r', o);
210 				putc(ch, o);
211 			}
212 			if (++lineno >= length) {
213 				fflush(o);
214 				npages++;
215 				lineno = 0;
216 			}
217 			maxcol[i] = -1;
218 		}
219 	}
220 	if (lineno) {		/* be sure to end on a page boundary */
221 		putchar('\f');
222 		npages++;
223 	}
224 	if (name && acctfile && access(acctfile, 02) >= 0 &&
225 	    freopen(acctfile, "a", stdout) != NULL) {
226 		printf("%7.2f\t%s:%s\n", (float)npages, host, name);
227 	}
228 	exit(0);
229 }
230 
231 void
232 usage(void)
233 {
234         fprintf(stderr,
235 	  "usage: lpf [-c] [-f] [-h host] [-i indent] [-l length] [-n name] [-w width] [acctfile]\n");
236 	exit(1);
237 
238 }
239 
240