xref: /netbsd-src/usr.bin/error/filter.c (revision dc306354b0b29af51801a7632f1e95265a68cd81)
1 /*	$NetBSD: filter.c,v 1.5 1998/11/06 23:10:08 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.5 1998/11/06 23:10:08 christos Exp $");
42 #endif /* not lint */
43 
44 #include <sys/types.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 	int	uid;
77 	char	filename[128];
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 		strcpy(filename, passwdentry->pw_dir);
94 		(void)strcat(filename, ERRORNAME);
95 	} else
96 		(void)strcpy(filename, auxname);
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; fgets(inbuffer, 255, 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 && (fgets (inbuffer, 255, 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(c1, c2)
138 	const void *c1, *c2;
139 {
140 	char	**cpp1, **cpp2;
141 
142 	cpp1 = (char **)c1;
143 	cpp2 = (char **)c2;
144 	return(strcmp(*cpp1, *cpp2));
145 }
146 
147 int
148 search_ignore(key)
149 	char	*key;
150 {
151 	int	ub, lb;
152 	int	halfway;
153 	int	order;
154 
155 	if (nignored == 0)
156 		return(-1);
157 	for(lb = 0, ub = nignored - 1; ub >= lb; ){
158 		halfway = (ub + lb)/2;
159 		if ( (order = strcmp(key, names_ignored[halfway])) == 0)
160 			return(halfway);
161 		if (order < 0)	/*key is less than probe, throw away above*/
162 			ub = halfway - 1;
163 		 else
164 			lb = halfway + 1;
165 	}
166 	return(-1);
167 }
168 
169 /*
170  *	Tell if the error text is to be ignored.
171  *	The error must have been canonicalized, with
172  *	the file name the zeroth entry in the errorv,
173  *	and the linenumber the second.
174  *	Return the new categorization of the error class.
175  */
176 Errorclass
177 discardit(errorp)
178 	Eptr		errorp;
179 {
180 	int		language;
181 	int		i;
182 	Errorclass	errorclass = errorp->error_e_class;
183 
184 	switch(errorclass){
185 		case	C_SYNC:
186 		case	C_NONSPEC:
187 		case	C_UNKNOWN:	return(errorclass);
188 		default:	;
189 	}
190 	if(errorp->error_lgtext < 2){
191 		return(C_NONSPEC);
192 	}
193 	language = errorp->error_language;
194 	if(language == INLINT){
195 		if (errorclass != C_NONSPEC){	/* no file */
196 			for(i=0; lint_libs[i] != 0; i++){
197 				if (strcmp(errorp->error_text[0], lint_libs[i]) == 0){
198 					return(C_DISCARD);
199 				}
200 			}
201 		}
202 		/* check if the argument to the error message is to be ignored*/
203 		if (ispunct((unsigned char)lastchar(errorp->error_text[2])))
204 			clob_last(errorp->error_text[2], '\0');
205 		if (search_ignore(errorp->error_text[errorclass == C_NONSPEC ? 0 : 2]) >= 0){
206 			return(C_NULLED);
207 		}
208 	}
209 	return(errorclass);
210 }
211