xref: /original-bsd/usr.bin/error/filter.c (revision 9a77813a)
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 are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)filter.c	5.4 (Berkeley) 03/06/89";
20 #endif /* not lint */
21 
22 #include <sys/types.h>
23 #include <stdio.h>
24 #include <ctype.h>
25 #include <pwd.h>
26 #include "error.h"
27 
28 char	*lint_libs[] = {
29 	IG_FILE1,
30 	IG_FILE2,
31 	IG_FILE3,
32 	IG_FILE4,
33 	0
34 };
35 extern	char*	processname;
36 int	lexsort();
37 /*
38  *	Read the file ERRORNAME of the names of functions in lint
39  *	to ignore complaints about.
40  */
41 getignored(auxname)
42 	char	*auxname;
43 {
44 	reg	int	i;
45 		FILE	*fyle;
46 		char	inbuffer[256];
47 		int	uid;
48 		char	filename[128];
49 		char	*username;
50 		struct	passwd *passwdentry;
51 
52 	nignored = 0;
53 	if (auxname == 0){	/* use the default */
54 		if ( (username = (char *)getlogin()) == NULL){
55 			username = "Unknown";
56 			uid = getuid();
57 			if ( (passwdentry = (struct passwd *)getpwuid(uid)) == NULL){
58 				return;
59 			}
60 		} else {
61 			if ( (passwdentry = (struct passwd *)getpwnam(username)) == NULL)
62 				return;
63 		}
64 		strcpy(filename, passwdentry->pw_dir);
65 		(void)strcat(filename, ERRORNAME);
66 	} else
67 		(void)strcpy(filename, auxname);
68 #ifdef FULLDEBUG
69 	printf("Opening file \"%s\" to read names to ignore.\n",
70 		filename);
71 #endif
72 	if ( (fyle = fopen(filename, "r")) == NULL){
73 #ifdef FULLDEBUG
74 		fprintf(stderr, "%s: Can't open file \"%s\"\n",
75 			processname, filename);
76 #endif
77 		return;
78 	}
79 	/*
80 	 *	Make the first pass through the file, counting lines
81 	 */
82 	for (nignored = 0; fgets(inbuffer, 255, fyle) != NULL; nignored++)
83 		continue;
84 	names_ignored = (char **)Calloc(nignored+1, sizeof (char *));
85 	fclose(fyle);
86 	if (freopen(filename, "r", fyle) == NULL){
87 #ifdef FULLDEBUG
88 		fprintf(stderr, "%s: Failure to open \"%s\" for second read.\n",
89 			processname, filename);
90 #endif
91 		nignored = 0;
92 		return;
93 	}
94 	for (i=0; i < nignored && (fgets (inbuffer, 255, fyle) != NULL); i++){
95 		names_ignored[i] = strsave(inbuffer);
96 		(void)substitute(names_ignored[i], '\n', '\0');
97 	}
98 	qsort(names_ignored, nignored, sizeof *names_ignored, lexsort);
99 #ifdef FULLDEBUG
100 	printf("Names to ignore follow.\n");
101 	for (i=0; i < nignored; i++){
102 		printf("\tIgnore: %s\n", names_ignored[i]);
103 	}
104 #endif
105 }
106 
107 int lexsort(cpp1, cpp2)
108 	char	**cpp1, **cpp2;
109 {
110 	return(strcmp(*cpp1, *cpp2));
111 }
112 
113 int search_ignore(key)
114 	char	*key;
115 {
116 	reg	int	ub, lb;
117 	reg	int	halfway;
118 		int	order;
119 
120 	if (nignored == 0)
121 		return(-1);
122 	for(lb = 0, ub = nignored - 1; ub >= lb; ){
123 		halfway = (ub + lb)/2;
124 		if ( (order = strcmp(key, names_ignored[halfway])) == 0)
125 			return(halfway);
126 		if (order < 0)	/*key is less than probe, throw away above*/
127 			ub = halfway - 1;
128 		 else
129 			lb = halfway + 1;
130 	}
131 	return(-1);
132 }
133 
134 /*
135  *	Tell if the error text is to be ignored.
136  *	The error must have been canonicalized, with
137  *	the file name the zeroth entry in the errorv,
138  *	and the linenumber the second.
139  *	Return the new categorization of the error class.
140  */
141 Errorclass discardit(errorp)
142 	reg	Eptr	errorp;
143 {
144 		int	language;
145 	reg	int	i;
146 	Errorclass	errorclass = errorp->error_e_class;
147 
148 	switch(errorclass){
149 		case	C_SYNC:
150 		case	C_NONSPEC:
151 		case	C_UNKNOWN:	return(errorclass);
152 		default:	;
153 	}
154 	if(errorp->error_lgtext < 2){
155 		return(C_NONSPEC);
156 	}
157 	language = errorp->error_language;
158 	if(language == INLINT){
159 		if (errorclass != C_NONSPEC){	/* no file */
160 			for(i=0; lint_libs[i] != 0; i++){
161 				if (strcmp(errorp->error_text[0], lint_libs[i]) == 0){
162 					return(C_DISCARD);
163 				}
164 			}
165 		}
166 		/* check if the argument to the error message is to be ignored*/
167 		if (ispunct(lastchar(errorp->error_text[2])))
168 			clob_last(errorp->error_text[2], '\0');
169 		if (search_ignore(errorp->error_text[errorclass == C_NONSPEC ? 0 : 2]) >= 0){
170 			return(C_NULLED);
171 		}
172 	}
173 	return(errorclass);
174 }
175