lpf.c revision 1.8 1 /* $NetBSD: lpf.c,v 1.8 2000/04/29 00:12:32 abs 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.8 2000/04/29 00:12:32 abs 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 <unistd.h>
57 #include <stdlib.h>
58 #include <stdio.h>
59
60 #define MAXWIDTH 132
61 #define MAXREP 10
62
63 char buf[MAXREP][MAXWIDTH];
64 int maxcol[MAXREP] = {-1};
65 int lineno;
66 int width = 132; /* default line length */
67 int length = 66; /* page length */
68 int indent; /* indentation length */
69 int npages = 1;
70 int literal; /* print control characters */
71 char *name; /* user's login name */
72 char *host; /* user's machine name */
73 char *acctfile; /* accounting information file */
74 int crnl; /* \n -> \r\n */
75 int need_cr;
76
77 int main __P((int, char *[]));
78 void usage __P((void));
79
80 int
81 main(argc, argv)
82 int argc;
83 char *argv[];
84 {
85 FILE *p = stdin, *o = stdout;
86 int i, col;
87 char *cp;
88 int done, linedone, maxrep, ch, prch;
89 char *limit;
90
91 while ((ch = getopt(argc, argv, "cfh:i:l:n:w:")) != -1)
92 switch (ch) {
93 case 'n':
94 name = optarg;
95 break;
96 case 'h':
97 host = optarg;
98 break;
99 case 'w':
100 if ((i = atoi(optarg)) > 0 && i <= MAXWIDTH)
101 width = i;
102 break;
103 case 'l':
104 length = atoi(optarg);
105 break;
106 case 'i':
107 indent = atoi(optarg);
108 break;
109 case 'c': /* Print control chars */
110 literal++;
111 break;
112 case 'f': /* Fix missing carriage returns */
113 crnl++;
114 break;
115 default:
116 usage();
117 }
118 argc -= optind;
119 argv += optind;
120 if (argc)
121 acctfile = *argv;
122
123 memset(buf, ' ', sizeof(buf));
124 done = 0;
125
126 while (!done) {
127 col = indent;
128 maxrep = -1;
129 linedone = 0;
130 prch = ch = 0;
131 need_cr = 0;
132 while (!linedone) {
133 prch = ch;
134 switch (ch = getc(p)) {
135 case EOF:
136 linedone = done = 1;
137 ch = '\n';
138 break;
139
140 case '\f':
141 lineno = length;
142 case '\n':
143 if (crnl && prch != '\r')
144 need_cr = 1;
145 if (maxrep < 0)
146 maxrep = 0;
147 linedone = 1;
148 break;
149
150 case '\b':
151 if (--col < indent)
152 col = indent;
153 break;
154
155 case '\r':
156 col = indent;
157 break;
158
159 case '\t':
160 col = ((col - indent) | 07) + indent + 1;
161 break;
162
163 case '\031':
164 /*
165 * lpd needs to use a different filter to
166 * print data so stop what we are doing and
167 * wait for lpd to restart us.
168 */
169 if ((ch = getchar()) == '\1') {
170 fflush(stdout);
171 kill(getpid(), SIGSTOP);
172 break;
173 } else {
174 ungetc(ch, stdin);
175 ch = '\031';
176 }
177
178 default:
179 if (col >= width || (!literal && ch < ' ')) {
180 col++;
181 break;
182 }
183 cp = &buf[0][col];
184 for (i = 0; i < MAXREP; i++) {
185 if (i > maxrep)
186 maxrep = i;
187 if (*cp == ' ') {
188 *cp = ch;
189 if (col > maxcol[i])
190 maxcol[i] = col;
191 break;
192 }
193 cp += MAXWIDTH;
194 }
195 col++;
196 break;
197 }
198 }
199
200 /* print out lines */
201 for (i = 0; i <= maxrep; i++) {
202 for (cp = buf[i], limit = cp+maxcol[i]; cp <= limit;) {
203 putc(*cp, o);
204 *cp++ = ' ';
205 }
206 if (i < maxrep)
207 putc('\r', o);
208 else {
209 if (need_cr)
210 putc('\r', o);
211 putc(ch, o);
212 }
213 if (++lineno >= length) {
214 fflush(o);
215 npages++;
216 lineno = 0;
217 }
218 maxcol[i] = -1;
219 }
220 }
221 if (lineno) { /* be sure to end on a page boundary */
222 putchar('\f');
223 npages++;
224 }
225 if (name && acctfile && access(acctfile, 02) >= 0 &&
226 freopen(acctfile, "a", stdout) != NULL) {
227 printf("%7.2f\t%s:%s\n", (float)npages, host, name);
228 }
229 exit(0);
230 }
231
232 void
233 usage()
234 {
235 fprintf(stderr,
236 "usage: lpf [-c] [-f] [-h host] [-i indent] [-l length] [-n name] [-w width] [acctfile]\n");
237 exit(1);
238
239 }
240
241