xref: /csrg-svn/usr.bin/error/filter.c (revision 61987)
121634Sdist /*
2*61987Sbostic  * Copyright (c) 1980, 1993
3*61987Sbostic  *	The Regents of the University of California.  All rights reserved.
434664Sbostic  *
542683Sbostic  * %sccs.include.redist.c%
621634Sdist  */
721634Sdist 
821634Sdist #ifndef lint
9*61987Sbostic static char sccsid[] = "@(#)filter.c	8.1 (Berkeley) 06/06/93";
1034664Sbostic #endif /* not lint */
1121634Sdist 
1237071Sbostic #include <sys/types.h>
1346694Sbostic #include <pwd.h>
1446694Sbostic #include <unistd.h>
151455Sroot #include <stdio.h>
161455Sroot #include <ctype.h>
1746694Sbostic #include <stdlib.h>
1846694Sbostic #include <string.h>
191455Sroot #include "error.h"
2037791Sbostic #include "pathnames.h"
211455Sroot 
221455Sroot char	*lint_libs[] = {
231455Sroot 	IG_FILE1,
241455Sroot 	IG_FILE2,
251455Sroot 	IG_FILE3,
261455Sroot 	IG_FILE4,
271455Sroot 	0
281455Sroot };
291455Sroot extern	char*	processname;
301455Sroot int	lexsort();
311455Sroot /*
321455Sroot  *	Read the file ERRORNAME of the names of functions in lint
331455Sroot  *	to ignore complaints about.
341455Sroot  */
getignored(auxname)351455Sroot getignored(auxname)
361455Sroot 	char	*auxname;
371455Sroot {
385592Srrh 	reg	int	i;
395592Srrh 		FILE	*fyle;
405592Srrh 		char	inbuffer[256];
415592Srrh 		int	uid;
425592Srrh 		char	filename[128];
435592Srrh 		char	*username;
445592Srrh 		struct	passwd *passwdentry;
451455Sroot 
461455Sroot 	nignored = 0;
471455Sroot 	if (auxname == 0){	/* use the default */
481455Sroot 		if ( (username = (char *)getlogin()) == NULL){
491455Sroot 			username = "Unknown";
501455Sroot 			uid = getuid();
511455Sroot 			if ( (passwdentry = (struct passwd *)getpwuid(uid)) == NULL){
521455Sroot 				return;
531455Sroot 			}
541455Sroot 		} else {
551455Sroot 			if ( (passwdentry = (struct passwd *)getpwnam(username)) == NULL)
561455Sroot 				return;
571455Sroot 		}
581455Sroot 		strcpy(filename, passwdentry->pw_dir);
595592Srrh 		(void)strcat(filename, ERRORNAME);
601455Sroot 	} else
615592Srrh 		(void)strcpy(filename, auxname);
621455Sroot #ifdef FULLDEBUG
631455Sroot 	printf("Opening file \"%s\" to read names to ignore.\n",
641455Sroot 		filename);
651455Sroot #endif
661455Sroot 	if ( (fyle = fopen(filename, "r")) == NULL){
671455Sroot #ifdef FULLDEBUG
681455Sroot 		fprintf(stderr, "%s: Can't open file \"%s\"\n",
691455Sroot 			processname, filename);
701455Sroot #endif
711455Sroot 		return;
721455Sroot 	}
731455Sroot 	/*
741455Sroot 	 *	Make the first pass through the file, counting lines
751455Sroot 	 */
761455Sroot 	for (nignored = 0; fgets(inbuffer, 255, fyle) != NULL; nignored++)
771455Sroot 		continue;
781455Sroot 	names_ignored = (char **)Calloc(nignored+1, sizeof (char *));
791455Sroot 	fclose(fyle);
801455Sroot 	if (freopen(filename, "r", fyle) == NULL){
811455Sroot #ifdef FULLDEBUG
821455Sroot 		fprintf(stderr, "%s: Failure to open \"%s\" for second read.\n",
831455Sroot 			processname, filename);
841455Sroot #endif
851455Sroot 		nignored = 0;
861455Sroot 		return;
871455Sroot 	}
881455Sroot 	for (i=0; i < nignored && (fgets (inbuffer, 255, fyle) != NULL); i++){
891455Sroot 		names_ignored[i] = strsave(inbuffer);
905592Srrh 		(void)substitute(names_ignored[i], '\n', '\0');
911455Sroot 	}
921455Sroot 	qsort(names_ignored, nignored, sizeof *names_ignored, lexsort);
931455Sroot #ifdef FULLDEBUG
941455Sroot 	printf("Names to ignore follow.\n");
951455Sroot 	for (i=0; i < nignored; i++){
961455Sroot 		printf("\tIgnore: %s\n", names_ignored[i]);
971455Sroot 	}
981455Sroot #endif
991455Sroot }
1001455Sroot 
lexsort(cpp1,cpp2)1011455Sroot int lexsort(cpp1, cpp2)
1021455Sroot 	char	**cpp1, **cpp2;
1031455Sroot {
1041455Sroot 	return(strcmp(*cpp1, *cpp2));
1051455Sroot }
1061455Sroot 
search_ignore(key)1071455Sroot int search_ignore(key)
1081455Sroot 	char	*key;
1091455Sroot {
1105592Srrh 	reg	int	ub, lb;
1115592Srrh 	reg	int	halfway;
1125592Srrh 		int	order;
1131455Sroot 
1141455Sroot 	if (nignored == 0)
1151455Sroot 		return(-1);
1161455Sroot 	for(lb = 0, ub = nignored - 1; ub >= lb; ){
1171455Sroot 		halfway = (ub + lb)/2;
1181455Sroot 		if ( (order = strcmp(key, names_ignored[halfway])) == 0)
1191455Sroot 			return(halfway);
1201455Sroot 		if (order < 0)	/*key is less than probe, throw away above*/
1211455Sroot 			ub = halfway - 1;
1221455Sroot 		 else
1231455Sroot 			lb = halfway + 1;
1241455Sroot 	}
1251455Sroot 	return(-1);
1261455Sroot }
1271455Sroot 
1281455Sroot /*
1291455Sroot  *	Tell if the error text is to be ignored.
1301455Sroot  *	The error must have been canonicalized, with
1311455Sroot  *	the file name the zeroth entry in the errorv,
1321455Sroot  *	and the linenumber the second.
1331455Sroot  *	Return the new categorization of the error class.
1341455Sroot  */
discardit(errorp)1351455Sroot Errorclass discardit(errorp)
1365592Srrh 	reg	Eptr	errorp;
1371455Sroot {
1385592Srrh 		int	language;
1395592Srrh 	reg	int	i;
1401455Sroot 	Errorclass	errorclass = errorp->error_e_class;
1411455Sroot 
1421455Sroot 	switch(errorclass){
1431455Sroot 		case	C_SYNC:
1441455Sroot 		case	C_NONSPEC:
1451455Sroot 		case	C_UNKNOWN:	return(errorclass);
1461455Sroot 		default:	;
1471455Sroot 	}
1481455Sroot 	if(errorp->error_lgtext < 2){
1491455Sroot 		return(C_NONSPEC);
1501455Sroot 	}
1511455Sroot 	language = errorp->error_language;
1521455Sroot 	if(language == INLINT){
1531455Sroot 		if (errorclass != C_NONSPEC){	/* no file */
1541455Sroot 			for(i=0; lint_libs[i] != 0; i++){
1551455Sroot 				if (strcmp(errorp->error_text[0], lint_libs[i]) == 0){
1561455Sroot 					return(C_DISCARD);
1571455Sroot 				}
1581455Sroot 			}
1591455Sroot 		}
1601455Sroot 		/* check if the argument to the error message is to be ignored*/
1611455Sroot 		if (ispunct(lastchar(errorp->error_text[2])))
1621455Sroot 			clob_last(errorp->error_text[2], '\0');
1631455Sroot 		if (search_ignore(errorp->error_text[errorclass == C_NONSPEC ? 0 : 2]) >= 0){
1641455Sroot 			return(C_NULLED);
1651455Sroot 		}
1661455Sroot 	}
1671455Sroot 	return(errorclass);
1681455Sroot }
169