skimprintcap.c revision 88004
1/*
2 * ------+---------+---------+---------+---------+---------+---------+---------*
3 * Copyright (c) 2001  - Garance Alistair Drosehn <gad@FreeBSD.org>.
4 * 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 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * The views and conclusions contained in the software and documentation
28 * are those of the authors and should not be interpreted as representing
29 * official policies, either expressed or implied, of the FreeBSD Project.
30 *
31 * ------+---------+---------+---------+---------+---------+---------+---------*
32 */
33
34#include <sys/cdefs.h>
35
36__FBSDID("$FreeBSD: head/usr.sbin/lpr/chkprintcap/skimprintcap.c 88004 2001-12-15 23:35:55Z gad $");
37
38#include <sys/types.h>
39
40#include <ctype.h>
41#include <err.h>
42#include <errno.h>
43#include <grp.h>
44#include <stdio.h>
45#include <string.h>
46#include <stdlib.h>
47#include <unistd.h>
48
49#include <sys/param.h>		/* needed for lp.h but not used here */
50#include <dirent.h>		/* ditto */
51#include "lp.h"
52#include "lp.local.h"
53#include "skimprintcap.h"
54
55/*
56 * Save the canonical queue name of the entry that is currently being
57 * scanned, in case a warning message is printed for the current queue.
58 * Only the first 'QENTRY_MAXLEN' characters will be saved, since this
59 * is only for warning messages.   The variable includes space for the
60 * string " (entry " and a trailing ")", when the scanner is in the
61 * middle of an entry.  When the scanner is not in a specific entry,
62 * the variable will be the a null string.
63 */
64#define QENTRY_MAXLEN	30
65#define QENTRY_PREFIX	" (entry "
66static char	 skim_entryname[sizeof(QENTRY_PREFIX) + QENTRY_MAXLEN + 2];
67
68/*
69 * isgraph is defined to work on an 'int', in the range 0 to 255, plus EOF.
70 * Define a wrapper which can take 'char', either signed or unsigned.
71 */
72#define isgraphch(Anychar)    isgraph(((int) Anychar) & 255)
73
74struct skiminfo *
75skim_printcap(const char *pcap_fname, int verbosity)
76{
77	struct skiminfo *skinf;
78	char buff[BUFSIZ];
79	char *ch, *curline, *endfield, *lastchar;
80	FILE *pc_file;
81	int missing_nl;
82	enum {NO_CONTINUE, WILL_CONTINUE, BAD_CONTINUE} is_cont, had_cont;
83	enum {CMNT_LINE, ENTRY_LINE, TAB_LINE, TABERR_LINE} is_type, had_type;
84
85	skinf = malloc(sizeof(struct skiminfo));
86	memset(skinf, 0, sizeof(struct skiminfo));
87
88	pc_file = fopen(pcap_fname, "r");
89	if (pc_file == NULL) {
90		warn("fopen(%s)", pcap_fname);
91		skinf->fatalerr++;
92		return (skinf);		/* fatal error */
93	}
94
95	skim_entryname[0] = '0';
96
97	is_cont = NO_CONTINUE;
98	is_type = CMNT_LINE;
99	errno = 0;
100	curline = fgets(buff, sizeof(buff), pc_file);
101	while (curline != NULL) {
102		skinf->lines++;
103
104		/* Check for the expected newline char, and remove it */
105		missing_nl = 0;
106		lastchar = strchr(curline, '\n');
107		if (lastchar != NULL)
108			*lastchar = '\0';
109		else {
110			lastchar = strchr(curline, '\0');
111			missing_nl = 1;
112		}
113		if (curline < lastchar)
114			lastchar--;
115
116		/*
117		 * Check for `\' (continuation-character) at end of line.
118		 * If there is none, then trim off spaces and check again.
119		 * This would be a bad line because it looks like it is
120		 * continued, but it will not be treated that way.
121		 */
122		had_cont = is_cont;
123		is_cont = NO_CONTINUE;
124		if (*lastchar == '\\') {
125			is_cont = WILL_CONTINUE;
126			lastchar--;
127		} else {
128			while ((curline < lastchar) && !isgraphch(*lastchar))
129				lastchar--;
130			if (*lastchar == '\\')
131				is_cont = BAD_CONTINUE;
132		}
133
134		had_type = is_type;
135		is_type = CMNT_LINE;
136		switch (*curline) {
137		case '\0':	/* treat zero-length line as comment */
138		case '#':
139			skinf->comments++;
140			break;
141		case ' ':
142		case '\t':
143			is_type = TAB_LINE;
144			break;
145		default:
146			is_type = ENTRY_LINE;
147			skinf->entries++;
148
149			/* pick up the queue name, to use in warning messages */
150			ch = curline;
151			while ((ch <= lastchar) && (*ch != ':') && (*ch != '|'))
152				ch++;
153			ch--;			/* last char of queue name */
154			strcpy(skim_entryname, QENTRY_PREFIX);
155			if ((ch - curline) > QENTRY_MAXLEN) {
156				strncat(skim_entryname, curline, QENTRY_MAXLEN
157				    - 1);
158				strcat(skim_entryname, "+");
159			} else {
160				strncat(skim_entryname, curline, (ch - curline
161				    + 1));
162			}
163			strlcat(skim_entryname, ")", sizeof(skim_entryname));
164			break;
165		}
166
167		/*
168		 * Check to see if the previous line was a bad contination
169		 * line.  The check is delayed until now so a warning message
170		 * is not printed when a "bad continuation" is on a comment
171		 * line, and it just "continues" into another comment line.
172		*/
173		if (had_cont == BAD_CONTINUE) {
174			if ((had_type != CMNT_LINE) || (is_type != CMNT_LINE) ||
175			    (verbosity > 1)) {
176				skinf->warnings++;
177				warnx("Warning: blanks after trailing '\\',"
178				    " at line %d%s", skinf->lines - 1,
179				    skim_entryname);
180			}
181		}
182
183		/* If we are no longer in an entry, then forget the name */
184		if ((had_cont != WILL_CONTINUE) && (is_type != ENTRY_LINE)) {
185			skim_entryname[0] = '\0';
186		}
187
188		/*
189		 * Print out warning for missing newline, done down here
190		 * so we are sure to have the right entry-name for it.
191		*/
192		if (missing_nl) {
193			skinf->warnings++;
194			warnx("Warning: No newline at end of line %d%s",
195			    skinf->lines, skim_entryname);
196		}
197
198		/*
199		 * Check for start-of-entry lines which do not include a
200		 * ":" character (to indicate the end of the name field).
201		 * This can cause standard printcap processing to ignore
202		 * ALL of the following lines.
203		 * XXXXX - May need to allow for the list-of-names to
204		 *         continue on to the following line...
205		*/
206		if (is_type == ENTRY_LINE) {
207			endfield = strchr(curline, ':');
208			if (endfield == NULL) {
209				skinf->warnings++;
210				warnx("Warning: No ':' to terminate name-field"
211				    " at line %d%s", skinf->lines,
212				    skim_entryname);
213			}
214		}
215
216		/*
217		 * Now check for cases where this line is (or is-not) a
218		 * continuation of the previous line, and a person skimming
219		 * the file would assume it is not (or is) a continuation.
220		*/
221		switch (had_cont) {
222		case NO_CONTINUE:
223		case BAD_CONTINUE:
224			if (is_type == TAB_LINE) {
225				skinf->warnings++;
226				warnx("Warning: values-line after line with"
227				    " NO trailing '\\', at line %d%s",
228				    skinf->lines, skim_entryname);
229			}
230			break;
231
232		case WILL_CONTINUE:
233			if (is_type == ENTRY_LINE) {
234				skinf->warnings++;
235				warnx("Warning: new entry starts after line"
236				    " with trailing '\\', at line %d%s",
237				    skinf->lines, skim_entryname);
238			}
239			break;
240		}
241
242		/* get another line from printcap and repeat loop */
243		curline = fgets(buff, sizeof(buff), pc_file);
244	}
245
246	if (errno != 0) {
247		warn("fgets(%s)", pcap_fname);
248		skinf->fatalerr++;		/* fatal error */
249	}
250
251	if (skinf->warnings > 0)
252		warnx("%4d warnings from skimming %s", skinf->warnings,
253		    pcap_fname);
254
255	if (verbosity)
256		warnx("%4d lines (%d comments), %d entries for %s",
257		    skinf->lines, skinf->comments, skinf->entries, pcap_fname);
258
259	fclose(pc_file);
260	return (skinf);
261}
262