xref: /netbsd-src/usr.bin/error/filter.c (revision 1ca5c1b28139779176bd5c13ad7c5f25c0bcd5f8)
1 /*	$NetBSD: filter.c,v 1.7 2001/02/05 01:44:07 christos 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.7 2001/02/05 01:44:07 christos 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 int	lexsort __P((const void *, const void *));
62 int	search_ignore __P((char *));
63 
64 /*
65  *	Read the file ERRORNAME of the names of functions in lint
66  *	to ignore complaints about.
67  */
68 void
69 getignored(auxname)
70 	char	*auxname;
71 {
72 	int	i;
73 	FILE	*fyle;
74 	char	inbuffer[256];
75 	uid_t	uid;
76 	char	filename[MAXPATHLEN];
77 	char	*username;
78 	struct	passwd *passwdentry;
79 
80 	nignored = 0;
81 	if (auxname == 0){	/* use the default */
82 		if ( (username = (char *)getlogin()) == NULL){
83 			username = "Unknown";
84 			uid = getuid();
85 			if ( (passwdentry = (struct passwd *)getpwuid(uid)) == NULL){
86 				return;
87 			}
88 		} else {
89 			if ( (passwdentry = (struct passwd *)getpwnam(username)) == NULL)
90 				return;
91 		}
92 		strlcpy(filename, passwdentry->pw_dir, sizeof(filename));
93 		(void)strlcat(filename, ERRORNAME, sizeof(filename));
94 	} else
95 		(void)strlcpy(filename, auxname, sizeof(filename));
96 #ifdef FULLDEBUG
97 	printf("Opening file \"%s\" to read names to ignore.\n",
98 		filename);
99 #endif
100 	if ( (fyle = fopen(filename, "r")) == NULL){
101 #ifdef FULLDEBUG
102 		fprintf(stderr, "%s: Can't open file \"%s\"\n",
103 			processname, filename);
104 #endif
105 		return;
106 	}
107 	/*
108 	 *	Make the first pass through the file, counting lines
109 	 */
110 	for (nignored = 0;
111 	     fgets(inbuffer, sizeof(inbuffer)-1, fyle) != NULL; nignored++)
112 		continue;
113 	names_ignored = (char **)Calloc(nignored+1, sizeof (char *));
114 	fclose(fyle);
115 	if (freopen(filename, "r", fyle) == NULL){
116 #ifdef FULLDEBUG
117 		fprintf(stderr, "%s: Failure to open \"%s\" for second read.\n",
118 			processname, filename);
119 #endif
120 		nignored = 0;
121 		return;
122 	}
123 	for (i=0; i < nignored &&
124 	          (fgets (inbuffer, sizeof(inbuffer)-1, fyle) != NULL); i++){
125 		names_ignored[i] = strsave(inbuffer);
126 		(void)substitute(names_ignored[i], '\n', '\0');
127 	}
128 	qsort(names_ignored, nignored, sizeof *names_ignored, lexsort);
129 #ifdef FULLDEBUG
130 	printf("Names to ignore follow.\n");
131 	for (i=0; i < nignored; i++){
132 		printf("\tIgnore: %s\n", names_ignored[i]);
133 	}
134 #endif
135 }
136 
137 int
138 lexsort(c1, c2)
139 	const void *c1, *c2;
140 {
141 	char	**cpp1, **cpp2;
142 
143 	cpp1 = (char **)c1;
144 	cpp2 = (char **)c2;
145 	return(strcmp(*cpp1, *cpp2));
146 }
147 
148 int
149 search_ignore(key)
150 	char	*key;
151 {
152 	int	ub, lb;
153 	int	halfway;
154 	int	order;
155 
156 	if (nignored == 0)
157 		return(-1);
158 	for(lb = 0, ub = nignored - 1; ub >= lb; ){
159 		halfway = (ub + lb)/2;
160 		if ( (order = strcmp(key, names_ignored[halfway])) == 0)
161 			return(halfway);
162 		if (order < 0)	/*key is less than probe, throw away above*/
163 			ub = halfway - 1;
164 		 else
165 			lb = halfway + 1;
166 	}
167 	return(-1);
168 }
169 
170 /*
171  *	Tell if the error text is to be ignored.
172  *	The error must have been canonicalized, with
173  *	the file name the zeroth entry in the errorv,
174  *	and the linenumber the second.
175  *	Return the new categorization of the error class.
176  */
177 Errorclass
178 discardit(errorp)
179 	Eptr		errorp;
180 {
181 	int		language;
182 	int		i;
183 	Errorclass	errorclass = errorp->error_e_class;
184 
185 	switch(errorclass){
186 		case	C_SYNC:
187 		case	C_NONSPEC:
188 		case	C_UNKNOWN:	return(errorclass);
189 		default:	;
190 	}
191 	if(errorp->error_lgtext < 2){
192 		return(C_NONSPEC);
193 	}
194 	language = errorp->error_language;
195 	if(language == INLINT){
196 		if (errorclass != C_NONSPEC){	/* no file */
197 			for(i=0; lint_libs[i] != 0; i++){
198 				if (strcmp(errorp->error_text[0], lint_libs[i]) == 0){
199 					return(C_DISCARD);
200 				}
201 			}
202 		}
203 		/* check if the argument to the error message is to be ignored*/
204 		if (ispunct((unsigned char)lastchar(errorp->error_text[2])))
205 			clob_last(errorp->error_text[2], '\0');
206 		if (search_ignore(errorp->error_text[errorclass == C_NONSPEC ? 0 : 2]) >= 0){
207 			return(C_NULLED);
208 		}
209 	}
210 	return(errorclass);
211 }
212