xref: /netbsd-src/usr.bin/error/filter.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /*	$NetBSD: filter.c,v 1.3 1995/09/02 06:15:28 jtc 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 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)filter.c	8.1 (Berkeley) 6/6/93";
39 #endif
40 static char rcsid[] = "$NetBSD: filter.c,v 1.3 1995/09/02 06:15:28 jtc Exp $";
41 #endif /* not lint */
42 
43 #include <sys/types.h>
44 #include <pwd.h>
45 #include <unistd.h>
46 #include <stdio.h>
47 #include <ctype.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include "error.h"
51 #include "pathnames.h"
52 
53 char	*lint_libs[] = {
54 	IG_FILE1,
55 	IG_FILE2,
56 	IG_FILE3,
57 	IG_FILE4,
58 	0
59 };
60 extern	char*	processname;
61 int	lexsort();
62 /*
63  *	Read the file ERRORNAME of the names of functions in lint
64  *	to ignore complaints about.
65  */
66 getignored(auxname)
67 	char	*auxname;
68 {
69 	reg	int	i;
70 		FILE	*fyle;
71 		char	inbuffer[256];
72 		int	uid;
73 		char	filename[128];
74 		char	*username;
75 		struct	passwd *passwdentry;
76 
77 	nignored = 0;
78 	if (auxname == 0){	/* use the default */
79 		if ( (username = (char *)getlogin()) == NULL){
80 			username = "Unknown";
81 			uid = getuid();
82 			if ( (passwdentry = (struct passwd *)getpwuid(uid)) == NULL){
83 				return;
84 			}
85 		} else {
86 			if ( (passwdentry = (struct passwd *)getpwnam(username)) == NULL)
87 				return;
88 		}
89 		strcpy(filename, passwdentry->pw_dir);
90 		(void)strcat(filename, ERRORNAME);
91 	} else
92 		(void)strcpy(filename, auxname);
93 #ifdef FULLDEBUG
94 	printf("Opening file \"%s\" to read names to ignore.\n",
95 		filename);
96 #endif
97 	if ( (fyle = fopen(filename, "r")) == NULL){
98 #ifdef FULLDEBUG
99 		fprintf(stderr, "%s: Can't open file \"%s\"\n",
100 			processname, filename);
101 #endif
102 		return;
103 	}
104 	/*
105 	 *	Make the first pass through the file, counting lines
106 	 */
107 	for (nignored = 0; fgets(inbuffer, 255, fyle) != NULL; nignored++)
108 		continue;
109 	names_ignored = (char **)Calloc(nignored+1, sizeof (char *));
110 	fclose(fyle);
111 	if (freopen(filename, "r", fyle) == NULL){
112 #ifdef FULLDEBUG
113 		fprintf(stderr, "%s: Failure to open \"%s\" for second read.\n",
114 			processname, filename);
115 #endif
116 		nignored = 0;
117 		return;
118 	}
119 	for (i=0; i < nignored && (fgets (inbuffer, 255, fyle) != NULL); i++){
120 		names_ignored[i] = strsave(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 lexsort(cpp1, cpp2)
133 	char	**cpp1, **cpp2;
134 {
135 	return(strcmp(*cpp1, *cpp2));
136 }
137 
138 int search_ignore(key)
139 	char	*key;
140 {
141 	reg	int	ub, lb;
142 	reg	int	halfway;
143 		int	order;
144 
145 	if (nignored == 0)
146 		return(-1);
147 	for(lb = 0, ub = nignored - 1; ub >= lb; ){
148 		halfway = (ub + lb)/2;
149 		if ( (order = strcmp(key, names_ignored[halfway])) == 0)
150 			return(halfway);
151 		if (order < 0)	/*key is less than probe, throw away above*/
152 			ub = halfway - 1;
153 		 else
154 			lb = halfway + 1;
155 	}
156 	return(-1);
157 }
158 
159 /*
160  *	Tell if the error text is to be ignored.
161  *	The error must have been canonicalized, with
162  *	the file name the zeroth entry in the errorv,
163  *	and the linenumber the second.
164  *	Return the new categorization of the error class.
165  */
166 Errorclass discardit(errorp)
167 	reg	Eptr	errorp;
168 {
169 		int	language;
170 	reg	int	i;
171 	Errorclass	errorclass = errorp->error_e_class;
172 
173 	switch(errorclass){
174 		case	C_SYNC:
175 		case	C_NONSPEC:
176 		case	C_UNKNOWN:	return(errorclass);
177 		default:	;
178 	}
179 	if(errorp->error_lgtext < 2){
180 		return(C_NONSPEC);
181 	}
182 	language = errorp->error_language;
183 	if(language == INLINT){
184 		if (errorclass != C_NONSPEC){	/* no file */
185 			for(i=0; lint_libs[i] != 0; i++){
186 				if (strcmp(errorp->error_text[0], lint_libs[i]) == 0){
187 					return(C_DISCARD);
188 				}
189 			}
190 		}
191 		/* check if the argument to the error message is to be ignored*/
192 		if (ispunct(lastchar(errorp->error_text[2])))
193 			clob_last(errorp->error_text[2], '\0');
194 		if (search_ignore(errorp->error_text[errorclass == C_NONSPEC ? 0 : 2]) >= 0){
195 			return(C_NULLED);
196 		}
197 	}
198 	return(errorclass);
199 }
200