GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/lpr/filters/lpf.c Lines: 0 80 0.0 %
Date: 2017-11-13 Branches: 0 81 0.0 %

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