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