xref: /netbsd-src/usr.bin/error/filter.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: filter.c,v 1.6 2000/01/14 06:53:48 mjl Exp $	*/
2 
3 /*
4  * Copyright (c) 1980, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)filter.c	8.1 (Berkeley) 6/6/93";
40 #endif
41 __RCSID("$NetBSD: filter.c,v 1.6 2000/01/14 06:53:48 mjl Exp $");
42 #endif /* not lint */
43 
44 #include <sys/param.h>
45 #include <pwd.h>
46 #include <unistd.h>
47 #include <stdio.h>
48 #include <ctype.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include "error.h"
52 #include "pathnames.h"
53 
54 char	*lint_libs[] = {
55 	IG_FILE1,
56 	IG_FILE2,
57 	IG_FILE3,
58 	IG_FILE4,
59 	0
60 };
61 extern	char*	processname;
62 int	lexsort __P((const void *, const void *));
63 int	search_ignore __P((char *));
64 
65 /*
66  *	Read the file ERRORNAME of the names of functions in lint
67  *	to ignore complaints about.
68  */
69 void
70 getignored(auxname)
71 	char	*auxname;
72 {
73 	int	i;
74 	FILE	*fyle;
75 	char	inbuffer[256];
76 	uid_t	uid;
77 	char	filename[MAXPATHLEN];
78 	char	*username;
79 	struct	passwd *passwdentry;
80 
81 	nignored = 0;
82 	if (auxname == 0){	/* use the default */
83 		if ( (username = (char *)getlogin()) == NULL){
84 			username = "Unknown";
85 			uid = getuid();
86 			if ( (passwdentry = (struct passwd *)getpwuid(uid)) == NULL){
87 				return;
88 			}
89 		} else {
90 			if ( (passwdentry = (struct passwd *)getpwnam(username)) == NULL)
91 				return;
92 		}
93 		strlcpy(filename, passwdentry->pw_dir, sizeof(filename));
94 		(void)strlcat(filename, ERRORNAME, sizeof(filename));
95 	} else
96 		(void)strlcpy(filename, auxname, sizeof(filename));
97 #ifdef FULLDEBUG
98 	printf("Opening file \"%s\" to read names to ignore.\n",
99 		filename);
100 #endif
101 	if ( (fyle = fopen(filename, "r")) == NULL){
102 #ifdef FULLDEBUG
103 		fprintf(stderr, "%s: Can't open file \"%s\"\n",
104 			processname, filename);
105 #endif
106 		return;
107 	}
108 	/*
109 	 *	Make the first pass through the file, counting lines
110 	 */
111 	for (nignored = 0;
112 	     fgets(inbuffer, sizeof(inbuffer)-1, fyle) != NULL; nignored++)
113 		continue;
114 	names_ignored = (char **)Calloc(nignored+1, sizeof (char *));
115 	fclose(fyle);
116 	if (freopen(filename, "r", fyle) == NULL){
117 #ifdef FULLDEBUG
118 		fprintf(stderr, "%s: Failure to open \"%s\" for second read.\n",
119 			processname, filename);
120 #endif
121 		nignored = 0;
122 		return;
123 	}
124 	for (i=0; i < nignored &&
125 	          (fgets (inbuffer, sizeof(inbuffer)-1, fyle) != NULL); i++){
126 		names_ignored[i] = strsave(inbuffer);
127 		(void)substitute(names_ignored[i], '\n', '\0');
128 	}
129 	qsort(names_ignored, nignored, sizeof *names_ignored, lexsort);
130 #ifdef FULLDEBUG
131 	printf("Names to ignore follow.\n");
132 	for (i=0; i < nignored; i++){
133 		printf("\tIgnore: %s\n", names_ignored[i]);
134 	}
135 #endif
136 }
137 
138 int
139 lexsort(c1, c2)
140 	const void *c1, *c2;
141 {
142 	char	**cpp1, **cpp2;
143 
144 	cpp1 = (char **)c1;
145 	cpp2 = (char **)c2;
146 	return(strcmp(*cpp1, *cpp2));
147 }
148 
149 int
150 search_ignore(key)
151 	char	*key;
152 {
153 	int	ub, lb;
154 	int	halfway;
155 	int	order;
156 
157 	if (nignored == 0)
158 		return(-1);
159 	for(lb = 0, ub = nignored - 1; ub >= lb; ){
160 		halfway = (ub + lb)/2;
161 		if ( (order = strcmp(key, names_ignored[halfway])) == 0)
162 			return(halfway);
163 		if (order < 0)	/*key is less than probe, throw away above*/
164 			ub = halfway - 1;
165 		 else
166 			lb = halfway + 1;
167 	}
168 	return(-1);
169 }
170 
171 /*
172  *	Tell if the error text is to be ignored.
173  *	The error must have been canonicalized, with
174  *	the file name the zeroth entry in the errorv,
175  *	and the linenumber the second.
176  *	Return the new categorization of the error class.
177  */
178 Errorclass
179 discardit(errorp)
180 	Eptr		errorp;
181 {
182 	int		language;
183 	int		i;
184 	Errorclass	errorclass = errorp->error_e_class;
185 
186 	switch(errorclass){
187 		case	C_SYNC:
188 		case	C_NONSPEC:
189 		case	C_UNKNOWN:	return(errorclass);
190 		default:	;
191 	}
192 	if(errorp->error_lgtext < 2){
193 		return(C_NONSPEC);
194 	}
195 	language = errorp->error_language;
196 	if(language == INLINT){
197 		if (errorclass != C_NONSPEC){	/* no file */
198 			for(i=0; lint_libs[i] != 0; i++){
199 				if (strcmp(errorp->error_text[0], lint_libs[i]) == 0){
200 					return(C_DISCARD);
201 				}
202 			}
203 		}
204 		/* check if the argument to the error message is to be ignored*/
205 		if (ispunct((unsigned char)lastchar(errorp->error_text[2])))
206 			clob_last(errorp->error_text[2], '\0');
207 		if (search_ignore(errorp->error_text[errorclass == C_NONSPEC ? 0 : 2]) >= 0){
208 			return(C_NULLED);
209 		}
210 	}
211 	return(errorclass);
212 }
213