xref: /netbsd-src/usr.bin/error/filter.c (revision d9158b13b5dfe46201430699a3f7a235ecf28df3)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 /*static char sccsid[] = "from: @(#)filter.c	5.7 (Berkeley) 2/26/91";*/
36 static char rcsid[] = "$Id: filter.c,v 1.2 1993/08/01 18:16:45 mycroft Exp $";
37 #endif /* not lint */
38 
39 #include <sys/types.h>
40 #include <pwd.h>
41 #include <unistd.h>
42 #include <stdio.h>
43 #include <ctype.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include "error.h"
47 #include "pathnames.h"
48 
49 char	*lint_libs[] = {
50 	IG_FILE1,
51 	IG_FILE2,
52 	IG_FILE3,
53 	IG_FILE4,
54 	0
55 };
56 extern	char*	processname;
57 int	lexsort();
58 /*
59  *	Read the file ERRORNAME of the names of functions in lint
60  *	to ignore complaints about.
61  */
62 getignored(auxname)
63 	char	*auxname;
64 {
65 	reg	int	i;
66 		FILE	*fyle;
67 		char	inbuffer[256];
68 		int	uid;
69 		char	filename[128];
70 		char	*username;
71 		struct	passwd *passwdentry;
72 
73 	nignored = 0;
74 	if (auxname == 0){	/* use the default */
75 		if ( (username = (char *)getlogin()) == NULL){
76 			username = "Unknown";
77 			uid = getuid();
78 			if ( (passwdentry = (struct passwd *)getpwuid(uid)) == NULL){
79 				return;
80 			}
81 		} else {
82 			if ( (passwdentry = (struct passwd *)getpwnam(username)) == NULL)
83 				return;
84 		}
85 		strcpy(filename, passwdentry->pw_dir);
86 		(void)strcat(filename, ERRORNAME);
87 	} else
88 		(void)strcpy(filename, auxname);
89 #ifdef FULLDEBUG
90 	printf("Opening file \"%s\" to read names to ignore.\n",
91 		filename);
92 #endif
93 	if ( (fyle = fopen(filename, "r")) == NULL){
94 #ifdef FULLDEBUG
95 		fprintf(stderr, "%s: Can't open file \"%s\"\n",
96 			processname, filename);
97 #endif
98 		return;
99 	}
100 	/*
101 	 *	Make the first pass through the file, counting lines
102 	 */
103 	for (nignored = 0; fgets(inbuffer, 255, fyle) != NULL; nignored++)
104 		continue;
105 	names_ignored = (char **)Calloc(nignored+1, sizeof (char *));
106 	fclose(fyle);
107 	if (freopen(filename, "r", fyle) == NULL){
108 #ifdef FULLDEBUG
109 		fprintf(stderr, "%s: Failure to open \"%s\" for second read.\n",
110 			processname, filename);
111 #endif
112 		nignored = 0;
113 		return;
114 	}
115 	for (i=0; i < nignored && (fgets (inbuffer, 255, fyle) != NULL); i++){
116 		names_ignored[i] = strsave(inbuffer);
117 		(void)substitute(names_ignored[i], '\n', '\0');
118 	}
119 	qsort(names_ignored, nignored, sizeof *names_ignored, lexsort);
120 #ifdef FULLDEBUG
121 	printf("Names to ignore follow.\n");
122 	for (i=0; i < nignored; i++){
123 		printf("\tIgnore: %s\n", names_ignored[i]);
124 	}
125 #endif
126 }
127 
128 int lexsort(cpp1, cpp2)
129 	char	**cpp1, **cpp2;
130 {
131 	return(strcmp(*cpp1, *cpp2));
132 }
133 
134 int search_ignore(key)
135 	char	*key;
136 {
137 	reg	int	ub, lb;
138 	reg	int	halfway;
139 		int	order;
140 
141 	if (nignored == 0)
142 		return(-1);
143 	for(lb = 0, ub = nignored - 1; ub >= lb; ){
144 		halfway = (ub + lb)/2;
145 		if ( (order = strcmp(key, names_ignored[halfway])) == 0)
146 			return(halfway);
147 		if (order < 0)	/*key is less than probe, throw away above*/
148 			ub = halfway - 1;
149 		 else
150 			lb = halfway + 1;
151 	}
152 	return(-1);
153 }
154 
155 /*
156  *	Tell if the error text is to be ignored.
157  *	The error must have been canonicalized, with
158  *	the file name the zeroth entry in the errorv,
159  *	and the linenumber the second.
160  *	Return the new categorization of the error class.
161  */
162 Errorclass discardit(errorp)
163 	reg	Eptr	errorp;
164 {
165 		int	language;
166 	reg	int	i;
167 	Errorclass	errorclass = errorp->error_e_class;
168 
169 	switch(errorclass){
170 		case	C_SYNC:
171 		case	C_NONSPEC:
172 		case	C_UNKNOWN:	return(errorclass);
173 		default:	;
174 	}
175 	if(errorp->error_lgtext < 2){
176 		return(C_NONSPEC);
177 	}
178 	language = errorp->error_language;
179 	if(language == INLINT){
180 		if (errorclass != C_NONSPEC){	/* no file */
181 			for(i=0; lint_libs[i] != 0; i++){
182 				if (strcmp(errorp->error_text[0], lint_libs[i]) == 0){
183 					return(C_DISCARD);
184 				}
185 			}
186 		}
187 		/* check if the argument to the error message is to be ignored*/
188 		if (ispunct(lastchar(errorp->error_text[2])))
189 			clob_last(errorp->error_text[2], '\0');
190 		if (search_ignore(errorp->error_text[errorclass == C_NONSPEC ? 0 : 2]) >= 0){
191 			return(C_NULLED);
192 		}
193 	}
194 	return(errorclass);
195 }
196