xref: /freebsd/usr.sbin/lpr/chkprintcap/skimprintcap.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
11de7b4b8SPedro F. Giffuni /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
443323a1dSGarance A Drosehn  * ------+---------+---------+---------+---------+---------+---------+---------*
543323a1dSGarance A Drosehn  * Copyright (c) 2001  - Garance Alistair Drosehn <gad@FreeBSD.org>.
643323a1dSGarance A Drosehn  * All rights reserved.
743323a1dSGarance A Drosehn  *
843323a1dSGarance A Drosehn  * Redistribution and use in source and binary forms, with or without
943323a1dSGarance A Drosehn  * modification, are permitted provided that the following conditions
1043323a1dSGarance A Drosehn  * are met:
1143323a1dSGarance A Drosehn  *   1. Redistributions of source code must retain the above copyright
1243323a1dSGarance A Drosehn  *      notice, this list of conditions and the following disclaimer.
1343323a1dSGarance A Drosehn  *   2. Redistributions in binary form must reproduce the above copyright
1443323a1dSGarance A Drosehn  *      notice, this list of conditions and the following disclaimer in the
1543323a1dSGarance A Drosehn  *      documentation and/or other materials provided with the distribution.
1643323a1dSGarance A Drosehn  *
1743323a1dSGarance A Drosehn  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1843323a1dSGarance A Drosehn  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1943323a1dSGarance A Drosehn  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2043323a1dSGarance A Drosehn  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2143323a1dSGarance A Drosehn  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2243323a1dSGarance A Drosehn  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2343323a1dSGarance A Drosehn  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2443323a1dSGarance A Drosehn  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2543323a1dSGarance A Drosehn  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2643323a1dSGarance A Drosehn  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2743323a1dSGarance A Drosehn  * SUCH DAMAGE.
2843323a1dSGarance A Drosehn  *
2943323a1dSGarance A Drosehn  * The views and conclusions contained in the software and documentation
3043323a1dSGarance A Drosehn  * are those of the authors and should not be interpreted as representing
3143323a1dSGarance A Drosehn  * official policies, either expressed or implied, of the FreeBSD Project.
3243323a1dSGarance A Drosehn  *
3343323a1dSGarance A Drosehn  * ------+---------+---------+---------+---------+---------+---------+---------*
3443323a1dSGarance A Drosehn  */
3543323a1dSGarance A Drosehn 
368e36ed92SGarance A Drosehn #include "lp.cdefs.h"		/* A cross-platform version of <sys/cdefs.h> */
3743323a1dSGarance A Drosehn #include <sys/types.h>
3843323a1dSGarance A Drosehn 
3943323a1dSGarance A Drosehn #include <ctype.h>
4043323a1dSGarance A Drosehn #include <err.h>
4143323a1dSGarance A Drosehn #include <errno.h>
4243323a1dSGarance A Drosehn #include <grp.h>
4343323a1dSGarance A Drosehn #include <stdio.h>
4443323a1dSGarance A Drosehn #include <string.h>
4543323a1dSGarance A Drosehn #include <stdlib.h>
4643323a1dSGarance A Drosehn #include <unistd.h>
4743323a1dSGarance A Drosehn 
4843323a1dSGarance A Drosehn #include <sys/param.h>		/* needed for lp.h but not used here */
4943323a1dSGarance A Drosehn #include <dirent.h>		/* ditto */
5043323a1dSGarance A Drosehn #include "lp.h"
5143323a1dSGarance A Drosehn #include "lp.local.h"
5243323a1dSGarance A Drosehn #include "skimprintcap.h"
5343323a1dSGarance A Drosehn 
5443323a1dSGarance A Drosehn /*
5543323a1dSGarance A Drosehn  * Save the canonical queue name of the entry that is currently being
5643323a1dSGarance A Drosehn  * scanned, in case a warning message is printed for the current queue.
5743323a1dSGarance A Drosehn  * Only the first 'QENTRY_MAXLEN' characters will be saved, since this
5843323a1dSGarance A Drosehn  * is only for warning messages.   The variable includes space for the
5943323a1dSGarance A Drosehn  * string " (entry " and a trailing ")", when the scanner is in the
6043323a1dSGarance A Drosehn  * middle of an entry.  When the scanner is not in a specific entry,
6143323a1dSGarance A Drosehn  * the variable will be the a null string.
6243323a1dSGarance A Drosehn  */
6343323a1dSGarance A Drosehn #define QENTRY_MAXLEN	30
6443323a1dSGarance A Drosehn #define QENTRY_PREFIX	" (entry "
6543323a1dSGarance A Drosehn static char	 skim_entryname[sizeof(QENTRY_PREFIX) + QENTRY_MAXLEN + 2];
6643323a1dSGarance A Drosehn 
6743323a1dSGarance A Drosehn /*
6843323a1dSGarance A Drosehn  * isgraph is defined to work on an 'int', in the range 0 to 255, plus EOF.
6943323a1dSGarance A Drosehn  * Define a wrapper which can take 'char', either signed or unsigned.
7043323a1dSGarance A Drosehn  */
7143323a1dSGarance A Drosehn #define isgraphch(Anychar)    isgraph(((int) Anychar) & 255)
7243323a1dSGarance A Drosehn 
7343323a1dSGarance A Drosehn struct skiminfo *
skim_printcap(const char * pcap_fname,int verbosity)7443323a1dSGarance A Drosehn skim_printcap(const char *pcap_fname, int verbosity)
7543323a1dSGarance A Drosehn {
7643323a1dSGarance A Drosehn 	struct skiminfo *skinf;
7743323a1dSGarance A Drosehn 	char buff[BUFSIZ];
7843323a1dSGarance A Drosehn 	char *ch, *curline, *endfield, *lastchar;
7943323a1dSGarance A Drosehn 	FILE *pc_file;
8043323a1dSGarance A Drosehn 	int missing_nl;
8143323a1dSGarance A Drosehn 	enum {NO_CONTINUE, WILL_CONTINUE, BAD_CONTINUE} is_cont, had_cont;
8243323a1dSGarance A Drosehn 	enum {CMNT_LINE, ENTRY_LINE, TAB_LINE, TABERR_LINE} is_type, had_type;
8343323a1dSGarance A Drosehn 
8443323a1dSGarance A Drosehn 	skinf = malloc(sizeof(struct skiminfo));
85e550d65cSGarance A Drosehn 	if (skinf == NULL)
86e550d65cSGarance A Drosehn 		return (NULL);
8743323a1dSGarance A Drosehn 	memset(skinf, 0, sizeof(struct skiminfo));
8843323a1dSGarance A Drosehn 
8943323a1dSGarance A Drosehn 	pc_file = fopen(pcap_fname, "r");
9043323a1dSGarance A Drosehn 	if (pc_file == NULL) {
9143323a1dSGarance A Drosehn 		warn("fopen(%s)", pcap_fname);
9243323a1dSGarance A Drosehn 		skinf->fatalerr++;
9343323a1dSGarance A Drosehn 		return (skinf);		/* fatal error */
9443323a1dSGarance A Drosehn 	}
9543323a1dSGarance A Drosehn 
9643323a1dSGarance A Drosehn 	skim_entryname[0] = '0';
9743323a1dSGarance A Drosehn 
9843323a1dSGarance A Drosehn 	is_cont = NO_CONTINUE;
9943323a1dSGarance A Drosehn 	is_type = CMNT_LINE;
10043323a1dSGarance A Drosehn 	errno = 0;
10143323a1dSGarance A Drosehn 	curline = fgets(buff, sizeof(buff), pc_file);
10243323a1dSGarance A Drosehn 	while (curline != NULL) {
10343323a1dSGarance A Drosehn 		skinf->lines++;
10443323a1dSGarance A Drosehn 
10543323a1dSGarance A Drosehn 		/* Check for the expected newline char, and remove it */
10643323a1dSGarance A Drosehn 		missing_nl = 0;
10743323a1dSGarance A Drosehn 		lastchar = strchr(curline, '\n');
10843323a1dSGarance A Drosehn 		if (lastchar != NULL)
10943323a1dSGarance A Drosehn 			*lastchar = '\0';
11043323a1dSGarance A Drosehn 		else {
11143323a1dSGarance A Drosehn 			lastchar = strchr(curline, '\0');
11243323a1dSGarance A Drosehn 			missing_nl = 1;
11343323a1dSGarance A Drosehn 		}
11443323a1dSGarance A Drosehn 		if (curline < lastchar)
11543323a1dSGarance A Drosehn 			lastchar--;
11643323a1dSGarance A Drosehn 
11743323a1dSGarance A Drosehn 		/*
11843323a1dSGarance A Drosehn 		 * Check for `\' (continuation-character) at end of line.
11943323a1dSGarance A Drosehn 		 * If there is none, then trim off spaces and check again.
12043323a1dSGarance A Drosehn 		 * This would be a bad line because it looks like it is
12143323a1dSGarance A Drosehn 		 * continued, but it will not be treated that way.
12243323a1dSGarance A Drosehn 		 */
12343323a1dSGarance A Drosehn 		had_cont = is_cont;
12443323a1dSGarance A Drosehn 		is_cont = NO_CONTINUE;
12543323a1dSGarance A Drosehn 		if (*lastchar == '\\') {
12643323a1dSGarance A Drosehn 			is_cont = WILL_CONTINUE;
12743323a1dSGarance A Drosehn 			lastchar--;
12843323a1dSGarance A Drosehn 		} else {
12943323a1dSGarance A Drosehn 			while ((curline < lastchar) && !isgraphch(*lastchar))
13043323a1dSGarance A Drosehn 				lastchar--;
13143323a1dSGarance A Drosehn 			if (*lastchar == '\\')
13243323a1dSGarance A Drosehn 				is_cont = BAD_CONTINUE;
13343323a1dSGarance A Drosehn 		}
13443323a1dSGarance A Drosehn 
13543323a1dSGarance A Drosehn 		had_type = is_type;
13643323a1dSGarance A Drosehn 		is_type = CMNT_LINE;
13743323a1dSGarance A Drosehn 		switch (*curline) {
13843323a1dSGarance A Drosehn 		case '\0':	/* treat zero-length line as comment */
13943323a1dSGarance A Drosehn 		case '#':
14043323a1dSGarance A Drosehn 			skinf->comments++;
14143323a1dSGarance A Drosehn 			break;
14243323a1dSGarance A Drosehn 		case ' ':
14343323a1dSGarance A Drosehn 		case '\t':
14443323a1dSGarance A Drosehn 			is_type = TAB_LINE;
14543323a1dSGarance A Drosehn 			break;
14643323a1dSGarance A Drosehn 		default:
14743323a1dSGarance A Drosehn 			is_type = ENTRY_LINE;
14843323a1dSGarance A Drosehn 			skinf->entries++;
14943323a1dSGarance A Drosehn 
15043323a1dSGarance A Drosehn 			/* pick up the queue name, to use in warning messages */
15143323a1dSGarance A Drosehn 			ch = curline;
15243323a1dSGarance A Drosehn 			while ((ch <= lastchar) && (*ch != ':') && (*ch != '|'))
15343323a1dSGarance A Drosehn 				ch++;
15443323a1dSGarance A Drosehn 			ch--;			/* last char of queue name */
15543323a1dSGarance A Drosehn 			strcpy(skim_entryname, QENTRY_PREFIX);
15643323a1dSGarance A Drosehn 			if ((ch - curline) > QENTRY_MAXLEN) {
15743323a1dSGarance A Drosehn 				strncat(skim_entryname, curline, QENTRY_MAXLEN
15843323a1dSGarance A Drosehn 				    - 1);
15943323a1dSGarance A Drosehn 				strcat(skim_entryname, "+");
16043323a1dSGarance A Drosehn 			} else {
16143323a1dSGarance A Drosehn 				strncat(skim_entryname, curline, (ch - curline
16243323a1dSGarance A Drosehn 				    + 1));
16343323a1dSGarance A Drosehn 			}
16443323a1dSGarance A Drosehn 			strlcat(skim_entryname, ")", sizeof(skim_entryname));
16543323a1dSGarance A Drosehn 			break;
16643323a1dSGarance A Drosehn 		}
16743323a1dSGarance A Drosehn 
16843323a1dSGarance A Drosehn 		/*
16943323a1dSGarance A Drosehn 		 * Check to see if the previous line was a bad contination
17043323a1dSGarance A Drosehn 		 * line.  The check is delayed until now so a warning message
17143323a1dSGarance A Drosehn 		 * is not printed when a "bad continuation" is on a comment
17243323a1dSGarance A Drosehn 		 * line, and it just "continues" into another comment line.
17343323a1dSGarance A Drosehn 		*/
17443323a1dSGarance A Drosehn 		if (had_cont == BAD_CONTINUE) {
17543323a1dSGarance A Drosehn 			if ((had_type != CMNT_LINE) || (is_type != CMNT_LINE) ||
17643323a1dSGarance A Drosehn 			    (verbosity > 1)) {
17743323a1dSGarance A Drosehn 				skinf->warnings++;
17843323a1dSGarance A Drosehn 				warnx("Warning: blanks after trailing '\\',"
17943323a1dSGarance A Drosehn 				    " at line %d%s", skinf->lines - 1,
18043323a1dSGarance A Drosehn 				    skim_entryname);
18143323a1dSGarance A Drosehn 			}
18243323a1dSGarance A Drosehn 		}
18343323a1dSGarance A Drosehn 
18443323a1dSGarance A Drosehn 		/* If we are no longer in an entry, then forget the name */
18543323a1dSGarance A Drosehn 		if ((had_cont != WILL_CONTINUE) && (is_type != ENTRY_LINE)) {
18643323a1dSGarance A Drosehn 			skim_entryname[0] = '\0';
18743323a1dSGarance A Drosehn 		}
18843323a1dSGarance A Drosehn 
18943323a1dSGarance A Drosehn 		/*
19043323a1dSGarance A Drosehn 		 * Print out warning for missing newline, done down here
19143323a1dSGarance A Drosehn 		 * so we are sure to have the right entry-name for it.
19243323a1dSGarance A Drosehn 		*/
19343323a1dSGarance A Drosehn 		if (missing_nl) {
19443323a1dSGarance A Drosehn 			skinf->warnings++;
19543323a1dSGarance A Drosehn 			warnx("Warning: No newline at end of line %d%s",
19643323a1dSGarance A Drosehn 			    skinf->lines, skim_entryname);
19743323a1dSGarance A Drosehn 		}
19843323a1dSGarance A Drosehn 
19943323a1dSGarance A Drosehn 		/*
20043323a1dSGarance A Drosehn 		 * Check for start-of-entry lines which do not include a
20143323a1dSGarance A Drosehn 		 * ":" character (to indicate the end of the name field).
20243323a1dSGarance A Drosehn 		 * This can cause standard printcap processing to ignore
20343323a1dSGarance A Drosehn 		 * ALL of the following lines.
20443323a1dSGarance A Drosehn 		 * XXXXX - May need to allow for the list-of-names to
20543323a1dSGarance A Drosehn 		 *         continue on to the following line...
20643323a1dSGarance A Drosehn 		*/
20743323a1dSGarance A Drosehn 		if (is_type == ENTRY_LINE) {
20843323a1dSGarance A Drosehn 			endfield = strchr(curline, ':');
20943323a1dSGarance A Drosehn 			if (endfield == NULL) {
21043323a1dSGarance A Drosehn 				skinf->warnings++;
21143323a1dSGarance A Drosehn 				warnx("Warning: No ':' to terminate name-field"
21243323a1dSGarance A Drosehn 				    " at line %d%s", skinf->lines,
21343323a1dSGarance A Drosehn 				    skim_entryname);
21443323a1dSGarance A Drosehn 			}
21543323a1dSGarance A Drosehn 		}
21643323a1dSGarance A Drosehn 
21743323a1dSGarance A Drosehn 		/*
21843323a1dSGarance A Drosehn 		 * Now check for cases where this line is (or is-not) a
21943323a1dSGarance A Drosehn 		 * continuation of the previous line, and a person skimming
22043323a1dSGarance A Drosehn 		 * the file would assume it is not (or is) a continuation.
22143323a1dSGarance A Drosehn 		*/
22243323a1dSGarance A Drosehn 		switch (had_cont) {
22343323a1dSGarance A Drosehn 		case NO_CONTINUE:
22443323a1dSGarance A Drosehn 		case BAD_CONTINUE:
22543323a1dSGarance A Drosehn 			if (is_type == TAB_LINE) {
22643323a1dSGarance A Drosehn 				skinf->warnings++;
22743323a1dSGarance A Drosehn 				warnx("Warning: values-line after line with"
22843323a1dSGarance A Drosehn 				    " NO trailing '\\', at line %d%s",
22943323a1dSGarance A Drosehn 				    skinf->lines, skim_entryname);
23043323a1dSGarance A Drosehn 			}
23143323a1dSGarance A Drosehn 			break;
23243323a1dSGarance A Drosehn 
23343323a1dSGarance A Drosehn 		case WILL_CONTINUE:
23443323a1dSGarance A Drosehn 			if (is_type == ENTRY_LINE) {
23543323a1dSGarance A Drosehn 				skinf->warnings++;
23643323a1dSGarance A Drosehn 				warnx("Warning: new entry starts after line"
23743323a1dSGarance A Drosehn 				    " with trailing '\\', at line %d%s",
23843323a1dSGarance A Drosehn 				    skinf->lines, skim_entryname);
23943323a1dSGarance A Drosehn 			}
24043323a1dSGarance A Drosehn 			break;
24143323a1dSGarance A Drosehn 		}
24243323a1dSGarance A Drosehn 
24343323a1dSGarance A Drosehn 		/* get another line from printcap and repeat loop */
24443323a1dSGarance A Drosehn 		curline = fgets(buff, sizeof(buff), pc_file);
24543323a1dSGarance A Drosehn 	}
24643323a1dSGarance A Drosehn 
24743323a1dSGarance A Drosehn 	if (errno != 0) {
24843323a1dSGarance A Drosehn 		warn("fgets(%s)", pcap_fname);
24943323a1dSGarance A Drosehn 		skinf->fatalerr++;		/* fatal error */
25043323a1dSGarance A Drosehn 	}
25143323a1dSGarance A Drosehn 
25243323a1dSGarance A Drosehn 	if (skinf->warnings > 0)
25343323a1dSGarance A Drosehn 		warnx("%4d warnings from skimming %s", skinf->warnings,
25443323a1dSGarance A Drosehn 		    pcap_fname);
25543323a1dSGarance A Drosehn 
25643323a1dSGarance A Drosehn 	if (verbosity)
25743323a1dSGarance A Drosehn 		warnx("%4d lines (%d comments), %d entries for %s",
25843323a1dSGarance A Drosehn 		    skinf->lines, skinf->comments, skinf->entries, pcap_fname);
25943323a1dSGarance A Drosehn 
26043323a1dSGarance A Drosehn 	fclose(pc_file);
26143323a1dSGarance A Drosehn 	return (skinf);
26243323a1dSGarance A Drosehn }
263