Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.sbin / lpr / chkprintcap / skimprintcap.c
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 #ifndef lint
35 static const char rcsid[] =
36   "$FreeBSD: src/usr.sbin/lpr/chkprintcap/skimprintcap.c,v 1.1.2.2 2002/06/19 01:53:48 gad Exp $";
37 #endif /* not lint */
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 *
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         memset(skinf, 0, sizeof(struct skiminfo));
88
89         pc_file = fopen(pcap_fname, "r");
90         if (pc_file == NULL) {
91                 warn("fopen(%s)", pcap_fname);
92                 skinf->fatalerr++;
93                 return (skinf);         /* fatal error */
94         }
95
96         skim_entryname[0] = '0';
97
98         is_cont = NO_CONTINUE;
99         is_type = CMNT_LINE;
100         errno = 0;
101         curline = fgets(buff, sizeof(buff), pc_file);
102         while (curline != NULL) {
103                 skinf->lines++;
104
105                 /* Check for the expected newline char, and remove it */
106                 missing_nl = 0;
107                 lastchar = strchr(curline, '\n');
108                 if (lastchar != NULL)
109                         *lastchar = '\0';
110                 else {
111                         lastchar = strchr(curline, '\0');
112                         missing_nl = 1;
113                 }
114                 if (curline < lastchar)
115                         lastchar--;
116
117                 /*
118                  * Check for `\' (continuation-character) at end of line.
119                  * If there is none, then trim off spaces and check again.
120                  * This would be a bad line because it looks like it is
121                  * continued, but it will not be treated that way.
122                  */
123                 had_cont = is_cont;
124                 is_cont = NO_CONTINUE;
125                 if (*lastchar == '\\') {
126                         is_cont = WILL_CONTINUE;
127                         lastchar--;
128                 } else {
129                         while ((curline < lastchar) && !isgraphch(*lastchar))
130                                 lastchar--;
131                         if (*lastchar == '\\')
132                                 is_cont = BAD_CONTINUE;
133                 }
134
135                 had_type = is_type;
136                 is_type = CMNT_LINE;
137                 switch (*curline) {
138                 case '\0':      /* treat zero-length line as comment */
139                 case '#':
140                         skinf->comments++;
141                         break;
142                 case ' ':
143                 case '\t':
144                         is_type = TAB_LINE;
145                         break;
146                 default:
147                         is_type = ENTRY_LINE;
148                         skinf->entries++;
149
150                         /* pick up the queue name, to use in warning messages */
151                         ch = curline;
152                         while ((ch <= lastchar) && (*ch != ':') && (*ch != '|'))
153                                 ch++;
154                         ch--;                   /* last char of queue name */
155                         strcpy(skim_entryname, QENTRY_PREFIX);
156                         if ((ch - curline) > QENTRY_MAXLEN) {
157                                 strncat(skim_entryname, curline, QENTRY_MAXLEN
158                                     - 1);
159                                 strcat(skim_entryname, "+");
160                         } else {
161                                 strncat(skim_entryname, curline, (ch - curline
162                                     + 1));
163                         }
164                         strlcat(skim_entryname, ")", sizeof(skim_entryname));
165                         break;
166                 }
167
168                 /*
169                  * Check to see if the previous line was a bad contination
170                  * line.  The check is delayed until now so a warning message
171                  * is not printed when a "bad continuation" is on a comment
172                  * line, and it just "continues" into another comment line.
173                 */
174                 if (had_cont == BAD_CONTINUE) {
175                         if ((had_type != CMNT_LINE) || (is_type != CMNT_LINE) ||
176                             (verbosity > 1)) {
177                                 skinf->warnings++;
178                                 warnx("Warning: blanks after trailing '\\'," 
179                                     " at line %d%s", skinf->lines - 1,
180                                     skim_entryname);
181                         }
182                 }
183
184                 /* If we are no longer in an entry, then forget the name */
185                 if ((had_cont != WILL_CONTINUE) && (is_type != ENTRY_LINE)) {
186                         skim_entryname[0] = '\0';
187                 }
188
189                 /*
190                  * Print out warning for missing newline, done down here
191                  * so we are sure to have the right entry-name for it.
192                 */
193                 if (missing_nl) {
194                         skinf->warnings++;
195                         warnx("Warning: No newline at end of line %d%s",
196                             skinf->lines, skim_entryname);
197                 }
198
199                 /*
200                  * Check for start-of-entry lines which do not include a
201                  * ":" character (to indicate the end of the name field).
202                  * This can cause standard printcap processing to ignore
203                  * ALL of the following lines.
204                  * XXXXX - May need to allow for the list-of-names to
205                  *         continue on to the following line...
206                 */
207                 if (is_type == ENTRY_LINE) {
208                         endfield = strchr(curline, ':');
209                         if (endfield == NULL) {
210                                 skinf->warnings++;
211                                 warnx("Warning: No ':' to terminate name-field"
212                                     " at line %d%s", skinf->lines,
213                                     skim_entryname);
214                         }
215                 }
216
217                 /*
218                  * Now check for cases where this line is (or is-not) a
219                  * continuation of the previous line, and a person skimming
220                  * the file would assume it is not (or is) a continuation.
221                 */
222                 switch (had_cont) {
223                 case NO_CONTINUE:
224                 case BAD_CONTINUE:
225                         if (is_type == TAB_LINE) {
226                                 skinf->warnings++;
227                                 warnx("Warning: values-line after line with" 
228                                     " NO trailing '\\', at line %d%s",
229                                     skinf->lines, skim_entryname);
230                         }
231                         break;
232
233                 case WILL_CONTINUE:
234                         if (is_type == ENTRY_LINE) {
235                                 skinf->warnings++;
236                                 warnx("Warning: new entry starts after line" 
237                                     " with trailing '\\', at line %d%s",
238                                     skinf->lines, skim_entryname);
239                         }
240                         break;
241                 }
242
243                 /* get another line from printcap and repeat loop */
244                 curline = fgets(buff, sizeof(buff), pc_file);
245         }
246
247         if (errno != 0) {
248                 warn("fgets(%s)", pcap_fname);
249                 skinf->fatalerr++;              /* fatal error */
250         }
251
252         if (skinf->warnings > 0)
253                 warnx("%4d warnings from skimming %s", skinf->warnings,
254                     pcap_fname);
255
256         if (verbosity)
257                 warnx("%4d lines (%d comments), %d entries for %s",
258                     skinf->lines, skinf->comments, skinf->entries, pcap_fname);
259
260         fclose(pc_file);
261         return (skinf);
262 }