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