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