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