Annotation of src/usr.bin/error/filter.c, Revision 1.5
1.5 ! deraadt 1: /* $OpenBSD: filter.c,v 1.4 1999/08/06 20:41:07 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.5 ! deraadt 41: static char rcsid[] = "$OpenBSD: filter.c,v 1.4 1999/08/06 20:41:07 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: */
66: getignored(auxname)
67: char *auxname;
68: {
69: reg int i;
1.4 deraadt 70: FILE *fyle;
71: char inbuffer[256];
72: uid_t uid;
1.5 ! deraadt 73: char filename[MAXPATHLEN];
1.4 deraadt 74: char *username;
75: struct passwd *passwdentry;
1.1 deraadt 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: }
1.5 ! deraadt 89: strlcpy(filename, passwdentry->pw_dir, sizeof(filename));
! 90: (void)strlcat(filename, ERRORNAME, sizeof(filename));
1.1 deraadt 91: } else
1.5 ! deraadt 92: (void)strlcpy(filename, auxname, sizeof filename);
1.1 deraadt 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
1.3 mickey 99: warn("Can't open file \"%s\"", filename);
1.1 deraadt 100: #endif
101: return;
102: }
103: /*
104: * Make the first pass through the file, counting lines
105: */
1.5 ! deraadt 106: for (nignored = 0;
! 107: fgets(inbuffer, sizeof(inbuffer)-1, fyle) != NULL; nignored++)
1.1 deraadt 108: continue;
109: names_ignored = (char **)Calloc(nignored+1, sizeof (char *));
110: fclose(fyle);
111: if (freopen(filename, "r", fyle) == NULL){
112: #ifdef FULLDEBUG
1.3 mickey 113: warn("Failure to open \"%s\" for second read.", filename);
1.1 deraadt 114: #endif
115: nignored = 0;
116: return;
117: }
1.5 ! deraadt 118: for (i=0; i < nignored &&
! 119: (fgets (inbuffer, sizeof(inbuffer)-1, fyle) != NULL); i++){
1.1 deraadt 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: }