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