lpf.c revision 1.5 1 /* $NetBSD: lpf.c,v 1.5 1997/07/10 06:21:51 mikel 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 #ifndef lint
36 static char copyright[] =
37 "@(#) Copyright (c) 1983, 1993\n\
38 The Regents of the University of California. All rights reserved.\n";
39 #endif /* not lint */
40
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)lpf.c 8.1 (Berkeley) 6/6/93";
44 #else
45 static char rcsid[] = "$NetBSD";
46 #endif
47 #endif /* not lint */
48
49 /*
50 * filter which reads the output of nroff and converts lines
51 * with ^H's to overwritten lines. Thus this works like 'ul'
52 * but is much better: it can handle more than 2 overwrites
53 * and it is written with some style.
54 * modified by kls to use register references instead of arrays
55 * to try to gain a little speed.
56 */
57
58 #include <signal.h>
59 #include <unistd.h>
60 #include <stdlib.h>
61 #include <stdio.h>
62
63 #define MAXWIDTH 132
64 #define MAXREP 10
65
66 char buf[MAXREP][MAXWIDTH];
67 int maxcol[MAXREP] = {-1};
68 int lineno;
69 int width = 132; /* default line length */
70 int length = 66; /* page length */
71 int indent; /* indentation length */
72 int npages = 1;
73 int literal; /* print control characters */
74 char *name; /* user's login name */
75 char *host; /* user's machine name */
76 char *acctfile; /* accounting information file */
77
78 int
79 main(argc, argv)
80 int argc;
81 char *argv[];
82 {
83 register FILE *p = stdin, *o = stdout;
84 register int i, col;
85 register char *cp;
86 int done, linedone, maxrep;
87 char ch, *limit;
88
89 while (--argc) {
90 if (*(cp = *++argv) == '-') {
91 switch (cp[1]) {
92 case 'n':
93 argc--;
94 name = *++argv;
95 break;
96
97 case 'h':
98 argc--;
99 host = *++argv;
100 break;
101
102 case 'w':
103 if ((i = atoi(&cp[2])) > 0 && i <= MAXWIDTH)
104 width = i;
105 break;
106
107 case 'l':
108 length = atoi(&cp[2]);
109 break;
110
111 case 'i':
112 indent = atoi(&cp[2]);
113 break;
114
115 case 'c': /* Print control chars */
116 literal++;
117 break;
118 }
119 } else
120 acctfile = cp;
121 }
122
123 for (cp = buf[0], limit = buf[MAXREP]; cp < limit; *cp++ = ' ');
124 done = 0;
125
126 while (!done) {
127 col = indent;
128 maxrep = -1;
129 linedone = 0;
130 while (!linedone) {
131 switch (ch = getc(p)) {
132 case EOF:
133 linedone = done = 1;
134 ch = '\n';
135 break;
136
137 case '\f':
138 lineno = length;
139 case '\n':
140 if (maxrep < 0)
141 maxrep = 0;
142 linedone = 1;
143 break;
144
145 case '\b':
146 if (--col < indent)
147 col = indent;
148 break;
149
150 case '\r':
151 col = indent;
152 break;
153
154 case '\t':
155 col = ((col - indent) | 07) + indent + 1;
156 break;
157
158 case '\031':
159 /*
160 * lpd needs to use a different filter to
161 * print data so stop what we are doing and
162 * wait for lpd to restart us.
163 */
164 if ((ch = getchar()) == '\1') {
165 fflush(stdout);
166 kill(getpid(), SIGSTOP);
167 break;
168 } else {
169 ungetc(ch, stdin);
170 ch = '\031';
171 }
172
173 default:
174 if (col >= width || (!literal && ch < ' ')) {
175 col++;
176 break;
177 }
178 cp = &buf[0][col];
179 for (i = 0; i < MAXREP; i++) {
180 if (i > maxrep)
181 maxrep = i;
182 if (*cp == ' ') {
183 *cp = ch;
184 if (col > maxcol[i])
185 maxcol[i] = col;
186 break;
187 }
188 cp += MAXWIDTH;
189 }
190 col++;
191 break;
192 }
193 }
194
195 /* print out lines */
196 for (i = 0; i <= maxrep; i++) {
197 for (cp = buf[i], limit = cp+maxcol[i]; cp <= limit;) {
198 putc(*cp, o);
199 *cp++ = ' ';
200 }
201 if (i < maxrep)
202 putc('\r', o);
203 else
204 putc(ch, o);
205 if (++lineno >= length) {
206 fflush(o);
207 npages++;
208 lineno = 0;
209 }
210 maxcol[i] = -1;
211 }
212 }
213 if (lineno) { /* be sure to end on a page boundary */
214 putchar('\f');
215 npages++;
216 }
217 if (name && acctfile && access(acctfile, 02) >= 0 &&
218 freopen(acctfile, "a", stdout) != NULL) {
219 printf("%7.2f\t%s:%s\n", (float)npages, host, name);
220 }
221 exit(0);
222 }
223