Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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  * $FreeBSD: src/usr.sbin/lpr/chkprintcap/skimprintcap.c,v 1.1.2.2 2002/06/19 01:53:48 gad Exp $
34  * $DragonFly: src/usr.sbin/lpr/chkprintcap/skimprintcap.c,v 1.2 2003/06/17 04:29:55 dillon Exp $
35  */
36
37 #include <sys/types.h>
38
39 #include <ctype.h>
40 #include <err.h>
41 #include <errno.h>
42 #include <grp.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47
48 #include <sys/param.h>          /* needed for lp.h but not used here */
49 #include <dirent.h>             /* ditto */
50 #include "lp.h"
51 #include "lp.local.h"
52 #include "skimprintcap.h"
53
54 /*
55  * Save the canonical queue name of the entry that is currently being
56  * scanned, in case a warning message is printed for the current queue.
57  * Only the first 'QENTRY_MAXLEN' characters will be saved, since this
58  * is only for warning messages.   The variable includes space for the
59  * string " (entry " and a trailing ")", when the scanner is in the
60  * middle of an entry.  When the scanner is not in a specific entry,
61  * the variable will be the a null string.
62  */
63 #define QENTRY_MAXLEN   30
64 #define QENTRY_PREFIX   " (entry "
65 static char      skim_entryname[sizeof(QENTRY_PREFIX) + QENTRY_MAXLEN + 2];
66
67 /*
68  * isgraph is defined to work on an 'int', in the range 0 to 255, plus EOF.
69  * Define a wrapper which can take 'char', either signed or unsigned.
70  */
71 #define isgraphch(Anychar)    isgraph(((int) Anychar) & 255)
72
73 struct skiminfo *
74 skim_printcap(const char *pcap_fname, int verbosity)
75 {
76         struct skiminfo *skinf;
77         char buff[BUFSIZ];
78         char *ch, *curline, *endfield, *lastchar;
79         FILE *pc_file;
80         int missing_nl;
81         enum {NO_CONTINUE, WILL_CONTINUE, BAD_CONTINUE} is_cont, had_cont;
82         enum {CMNT_LINE, ENTRY_LINE, TAB_LINE, TABERR_LINE} is_type, had_type;
83
84         skinf = malloc(sizeof(struct skiminfo));
85         memset(skinf, 0, sizeof(struct skiminfo));
86
87         pc_file = fopen(pcap_fname, "r");
88         if (pc_file == NULL) {
89                 warn("fopen(%s)", pcap_fname);
90                 skinf->fatalerr++;
91                 return (skinf);         /* fatal error */
92         }
93
94         skim_entryname[0] = '0';
95
96         is_cont = NO_CONTINUE;
97         is_type = CMNT_LINE;
98         errno = 0;
99         curline = fgets(buff, sizeof(buff), pc_file);
100         while (curline != NULL) {
101                 skinf->lines++;
102
103                 /* Check for the expected newline char, and remove it */
104                 missing_nl = 0;
105                 lastchar = strchr(curline, '\n');
106                 if (lastchar != NULL)
107                         *lastchar = '\0';
108                 else {
109                         lastchar = strchr(curline, '\0');
110                         missing_nl = 1;
111                 }
112                 if (curline < lastchar)
113                         lastchar--;
114
115                 /*
116                  * Check for `\' (continuation-character) at end of line.
117                  * If there is none, then trim off spaces and check again.
118                  * This would be a bad line because it looks like it is
119                  * continued, but it will not be treated that way.
120                  */
121                 had_cont = is_cont;
122                 is_cont = NO_CONTINUE;
123                 if (*lastchar == '\\') {
124                         is_cont = WILL_CONTINUE;
125                         lastchar--;
126                 } else {
127                         while ((curline < lastchar) && !isgraphch(*lastchar))
128                                 lastchar--;
129                         if (*lastchar == '\\')
130                                 is_cont = BAD_CONTINUE;
131                 }
132
133                 had_type = is_type;
134                 is_type = CMNT_LINE;
135                 switch (*curline) {
136                 case '\0':      /* treat zero-length line as comment */
137                 case '#':
138                         skinf->comments++;
139                         break;
140                 case ' ':
141                 case '\t':
142                         is_type = TAB_LINE;
143                         break;
144                 default:
145                         is_type = ENTRY_LINE;
146                         skinf->entries++;
147
148                         /* pick up the queue name, to use in warning messages */
149                         ch = curline;
150                         while ((ch <= lastchar) && (*ch != ':') && (*ch != '|'))
151                                 ch++;
152                         ch--;                   /* last char of queue name */
153                         strcpy(skim_entryname, QENTRY_PREFIX);
154                         if ((ch - curline) > QENTRY_MAXLEN) {
155                                 strncat(skim_entryname, curline, QENTRY_MAXLEN
156                                     - 1);
157                                 strcat(skim_entryname, "+");
158                         } else {
159                                 strncat(skim_entryname, curline, (ch - curline
160                                     + 1));
161                         }
162                         strlcat(skim_entryname, ")", sizeof(skim_entryname));
163                         break;
164                 }
165
166                 /*
167                  * Check to see if the previous line was a bad contination
168                  * line.  The check is delayed until now so a warning message
169                  * is not printed when a "bad continuation" is on a comment
170                  * line, and it just "continues" into another comment line.
171                 */
172                 if (had_cont == BAD_CONTINUE) {
173                         if ((had_type != CMNT_LINE) || (is_type != CMNT_LINE) ||
174                             (verbosity > 1)) {
175                                 skinf->warnings++;
176                                 warnx("Warning: blanks after trailing '\\'," 
177                                     " at line %d%s", skinf->lines - 1,
178                                     skim_entryname);
179                         }
180                 }
181
182                 /* If we are no longer in an entry, then forget the name */
183                 if ((had_cont != WILL_CONTINUE) && (is_type != ENTRY_LINE)) {
184                         skim_entryname[0] = '\0';
185                 }
186
187                 /*
188                  * Print out warning for missing newline, done down here
189                  * so we are sure to have the right entry-name for it.
190                 */
191                 if (missing_nl) {
192                         skinf->warnings++;
193                         warnx("Warning: No newline at end of line %d%s",
194                             skinf->lines, skim_entryname);
195                 }
196
197                 /*
198                  * Check for start-of-entry lines which do not include a
199                  * ":" character (to indicate the end of the name field).
200                  * This can cause standard printcap processing to ignore
201                  * ALL of the following lines.
202                  * XXXXX - May need to allow for the list-of-names to
203                  *         continue on to the following line...
204                 */
205                 if (is_type == ENTRY_LINE) {
206                         endfield = strchr(curline, ':');
207                         if (endfield == NULL) {
208                                 skinf->warnings++;
209                                 warnx("Warning: No ':' to terminate name-field"
210                                     " at line %d%s", skinf->lines,
211                                     skim_entryname);
212                         }
213                 }
214
215                 /*
216                  * Now check for cases where this line is (or is-not) a
217                  * continuation of the previous line, and a person skimming
218                  * the file would assume it is not (or is) a continuation.
219                 */
220                 switch (had_cont) {
221                 case NO_CONTINUE:
222                 case BAD_CONTINUE:
223                         if (is_type == TAB_LINE) {
224                                 skinf->warnings++;
225                                 warnx("Warning: values-line after line with" 
226                                     " NO trailing '\\', at line %d%s",
227                                     skinf->lines, skim_entryname);
228                         }
229                         break;
230
231                 case WILL_CONTINUE:
232                         if (is_type == ENTRY_LINE) {
233                                 skinf->warnings++;
234                                 warnx("Warning: new entry starts after line" 
235                                     " with trailing '\\', at line %d%s",
236                                     skinf->lines, skim_entryname);
237                         }
238                         break;
239                 }
240
241                 /* get another line from printcap and repeat loop */
242                 curline = fgets(buff, sizeof(buff), pc_file);
243         }
244
245         if (errno != 0) {
246                 warn("fgets(%s)", pcap_fname);
247                 skinf->fatalerr++;              /* fatal error */
248         }
249
250         if (skinf->warnings > 0)
251                 warnx("%4d warnings from skimming %s", skinf->warnings,
252                     pcap_fname);
253
254         if (verbosity)
255                 warnx("%4d lines (%d comments), %d entries for %s",
256                     skinf->lines, skinf->comments, skinf->entries, pcap_fname);
257
258         fclose(pc_file);
259         return (skinf);
260 }