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