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