No empty .Rs/.Re
[netbsd-mini2440.git] / usr.bin / error / filter.c
blobae72a70d069c9633399e6d2f2522d4f421ae4236
1 /* $NetBSD: filter.c,v 1.14 2009/08/13 04:09:53 dholland Exp $ */
3 /*
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)filter.c 8.1 (Berkeley) 6/6/93";
36 #endif
37 __RCSID("$NetBSD: filter.c,v 1.14 2009/08/13 04:09:53 dholland Exp $");
38 #endif /* not lint */
40 #include <sys/param.h>
41 #include <pwd.h>
42 #include <unistd.h>
43 #include <stdio.h>
44 #include <ctype.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include "error.h"
48 #include "pathnames.h"
50 static const char *lint_libs[] = {
51 IG_FILE1,
52 IG_FILE2,
53 IG_FILE3,
54 IG_FILE4,
58 static int lexsort(const void *, const void *);
59 static int search_ignore(const char *);
62 * Read the file ERRORNAME of the names of functions in lint
63 * to ignore complaints about.
65 void
66 getignored(const char *auxname)
68 int i;
69 FILE *fyle;
70 char inbuffer[256];
71 uid_t uid;
72 char filename[MAXPATHLEN];
73 const char *username;
74 struct passwd *passwdentry;
76 nignored = 0;
77 if (auxname == 0) { /* use the default */
78 if ((username = getlogin()) == NULL) {
79 username = "Unknown";
80 uid = getuid();
81 if ((passwdentry = getpwuid(uid)) == NULL) {
82 return;
84 } else {
85 if ((passwdentry = getpwnam(username)) == NULL)
86 return;
88 strlcpy(filename, passwdentry->pw_dir, sizeof(filename));
89 (void)strlcat(filename, ERRORNAME, sizeof(filename));
90 } else
91 (void)strlcpy(filename, auxname, sizeof(filename));
92 #ifdef FULLDEBUG
93 printf("Opening file \"%s\" to read names to ignore.\n",
94 filename);
95 #endif
96 if ((fyle = fopen(filename, "r")) == NULL) {
97 #ifdef FULLDEBUG
98 fprintf(stderr, "%s: Can't open file \"%s\"\n",
99 processname, filename);
100 #endif
101 return;
105 * Make the first pass through the file, counting lines
107 for (nignored = 0;
108 fgets(inbuffer, sizeof(inbuffer)-1, fyle) != NULL; nignored++)
109 continue;
110 names_ignored = Calloc(nignored+1, sizeof (char *));
111 fclose(fyle);
112 if (freopen(filename, "r", fyle) == NULL) {
113 #ifdef FULLDEBUG
114 fprintf(stderr, "%s: Failure to open \"%s\" for second read.\n",
115 processname, filename);
116 #endif
117 nignored = 0;
118 return;
120 for (i=0; i < nignored &&
121 (fgets (inbuffer, sizeof(inbuffer)-1, fyle) != NULL); i++) {
122 names_ignored[i] = strdup(inbuffer);
123 (void)substitute(names_ignored[i], '\n', '\0');
125 qsort(names_ignored, nignored, sizeof *names_ignored, lexsort);
126 #ifdef FULLDEBUG
127 printf("Names to ignore follow.\n");
128 for (i=0; i < nignored; i++) {
129 printf("\tIgnore: %s\n", names_ignored[i]);
131 #endif
134 static int
135 lexsort(const void *c1, const void *c2)
137 const char *const *cpp1;
138 const char *const *cpp2;
140 cpp1 = c1;
141 cpp2 = c2;
142 return (strcmp(*cpp1, *cpp2));
145 static int
146 search_ignore(const char *key)
148 int ub, lb;
149 int halfway;
150 int order;
152 if (nignored == 0)
153 return (-1);
154 for (lb = 0, ub = nignored - 1; ub >= lb; ) {
155 halfway = (ub + lb)/2;
156 if ( (order = strcmp(key, names_ignored[halfway])) == 0)
157 return (halfway);
158 if (order < 0) /* key is less than probe, throw away above */
159 ub = halfway - 1;
160 else
161 lb = halfway + 1;
163 return (-1);
167 * Tell if the error text is to be ignored.
168 * The error must have been canonicalized, with
169 * the file name the zeroth entry in the errorv,
170 * and the linenumber the second.
171 * Return the new categorization of the error class.
173 Errorclass
174 discardit(Eptr errorp)
176 int i;
177 Errorclass errorclass = errorp->error_e_class;
179 switch (errorclass) {
180 case C_SYNC:
181 case C_NONSPEC:
182 case C_UNKNOWN: return (errorclass);
183 default: ;
185 if (errorp->error_lgtext < 2) {
186 return (C_NONSPEC);
188 if (errorp->error_language == INLINT) {
189 if (errorclass != C_NONSPEC) { /* no file */
190 for (i=0; lint_libs[i] != 0; i++) {
191 if (strcmp(errorp->error_text[0], lint_libs[i]) == 0) {
192 return (C_DISCARD);
196 /* check if the argument to the error message is to be ignored */
197 if (ispunct((unsigned char)lastchar(errorp->error_text[2])))
198 clob_last(errorp->error_text[2], '\0');
199 if (search_ignore(errorp->error_text[errorclass == C_NONSPEC ? 0 : 2]) >= 0) {
200 return (C_NULLED);
203 return (errorclass);