[BACK]Return to filter.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / usr.bin / error

File: [cvs.NetBSD.org] / src / usr.bin / error / filter.c (download)

Revision 1.15, Thu Aug 13 05:53:58 2009 UTC (14 years, 8 months ago) by dholland
Branch: MAIN
CVS Tags: yamt-pagecache-tag8, yamt-pagecache-base9, yamt-pagecache-base8, yamt-pagecache-base7, yamt-pagecache-base6, yamt-pagecache-base5, yamt-pagecache-base4, yamt-pagecache-base3, yamt-pagecache-base2, yamt-pagecache-base, yamt-pagecache, tls-maxphys-base, tls-maxphys, tls-earlyentropy-base, tls-earlyentropy, riastradh-xf86-video-intel-2-7-1-pre-2-21-15, riastradh-drm2-base3, riastradh-drm2-base2, riastradh-drm2-base1, riastradh-drm2-base, riastradh-drm2, netbsd-7-nhusb-base-20170116, netbsd-7-nhusb-base, netbsd-7-nhusb, netbsd-7-base, netbsd-7-2-RELEASE, netbsd-7-1-RELEASE, netbsd-7-1-RC2, netbsd-7-1-RC1, netbsd-7-1-2-RELEASE, netbsd-7-1-1-RELEASE, netbsd-7-1, netbsd-7-0-RELEASE, netbsd-7-0-RC3, netbsd-7-0-RC2, netbsd-7-0-RC1, netbsd-7-0-2-RELEASE, netbsd-7-0-1-RELEASE, netbsd-7-0, netbsd-7, netbsd-6-base, netbsd-6-1-RELEASE, netbsd-6-1-RC4, netbsd-6-1-RC3, netbsd-6-1-RC2, netbsd-6-1-RC1, netbsd-6-1-5-RELEASE, netbsd-6-1-4-RELEASE, netbsd-6-1-3-RELEASE, netbsd-6-1-2-RELEASE, netbsd-6-1-1-RELEASE, netbsd-6-1, netbsd-6-0-RELEASE, netbsd-6-0-RC2, netbsd-6-0-RC1, netbsd-6-0-6-RELEASE, netbsd-6-0-5-RELEASE, netbsd-6-0-4-RELEASE, netbsd-6-0-3-RELEASE, netbsd-6-0-2-RELEASE, netbsd-6-0-1-RELEASE, netbsd-6-0, netbsd-6, matt-premerge-20091211, matt-nb6-plus-nbase, matt-nb6-plus-base, matt-nb6-plus, matt-mips64-premerge-20101231, cherry-xenmp-base, cherry-xenmp, bouyer-quota2-nbase, bouyer-quota2-base, bouyer-quota2, agc-symver-base, agc-symver
Changes since 1.14: +6 -6 lines

Assorted minor cleanup:
  - use stdbool.h (partly)
  - move extern declarations of data to header files
  - use right types for calloc() wrapper
  - remove bogus casts on return values
  - remove excessive Pascal-style parentheses in conditionals
  - a couple const fixes
  - fix some typos in comments

/*	$NetBSD: filter.c,v 1.15 2009/08/13 05:53:58 dholland Exp $	*/

/*
 * Copyright (c) 1980, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)filter.c	8.1 (Berkeley) 6/6/93";
#endif
__RCSID("$NetBSD: filter.c,v 1.15 2009/08/13 05:53:58 dholland Exp $");
#endif /* not lint */

#include <sys/param.h>
#include <pwd.h>
#include <unistd.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "error.h"
#include "pathnames.h"

static const char *lint_libs[] = {
	IG_FILE1,
	IG_FILE2,
	IG_FILE3,
	IG_FILE4,
	0
};

static int lexsort(const void *, const void *);
static int search_ignore(const char *);

/*
 * Read the file ERRORNAME of the names of functions in lint
 * to ignore complaints about.
 */
void
getignored(const char *auxname)
{
	int i;
	FILE *fyle;
	char inbuffer[256];
	uid_t uid;
	char filename[MAXPATHLEN];
	const char *username;
	struct passwd *passwdentry;

	nignored = 0;
	if (auxname == 0) {	/* use the default */
		if ((username = getlogin()) == NULL) {
			username = "Unknown";
			uid = getuid();
			if ((passwdentry = getpwuid(uid)) == NULL) {
				return;
			}
		} else {
			if ((passwdentry = getpwnam(username)) == NULL)
				return;
		}
		strlcpy(filename, passwdentry->pw_dir, sizeof(filename));
		(void)strlcat(filename, ERRORNAME, sizeof(filename));
	} else
		(void)strlcpy(filename, auxname, sizeof(filename));
#ifdef FULLDEBUG
	printf("Opening file \"%s\" to read names to ignore.\n",
		filename);
#endif
	if ((fyle = fopen(filename, "r")) == NULL) {
#ifdef FULLDEBUG
		fprintf(stderr, "%s: Can't open file \"%s\"\n",
			processname, filename);
#endif
		return;
	}

	/*
	 * Make the first pass through the file, counting lines
	 */
	for (nignored = 0;
	     fgets(inbuffer, sizeof(inbuffer)-1, fyle) != NULL; nignored++)
		continue;
	names_ignored = Calloc(nignored+1, sizeof (char *));
	fclose(fyle);
	if (freopen(filename, "r", fyle) == NULL) {
#ifdef FULLDEBUG
		fprintf(stderr, "%s: Failure to open \"%s\" for second read.\n",
			processname, filename);
#endif
		nignored = 0;
		return;
	}
	for (i=0; i < nignored &&
	          (fgets (inbuffer, sizeof(inbuffer)-1, fyle) != NULL); i++) {
		names_ignored[i] = strdup(inbuffer);
		(void)substitute(names_ignored[i], '\n', '\0');
	}
	qsort(names_ignored, nignored, sizeof *names_ignored, lexsort);
#ifdef FULLDEBUG
	printf("Names to ignore follow.\n");
	for (i=0; i < nignored; i++) {
		printf("\tIgnore: %s\n", names_ignored[i]);
	}
#endif
}

static int
lexsort(const void *c1, const void *c2)
{
	const char *const *cpp1;
	const char *const *cpp2;

	cpp1 = c1;
	cpp2 = c2;
	return (strcmp(*cpp1, *cpp2));
}

static int
search_ignore(const char *key)
{
	int ub, lb;
	int halfway;
	int order;

	if (nignored == 0)
		return (-1);
	for (lb = 0, ub = nignored - 1; ub >= lb; ) {
		halfway = (ub + lb)/2;
		if ( (order = strcmp(key, names_ignored[halfway])) == 0)
			return (halfway);
		if (order < 0)	/* key is less than probe, throw away above */
			ub = halfway - 1;
		 else
			lb = halfway + 1;
	}
	return (-1);
}

/*
 * Tell if the error text is to be ignored.
 * The error must have been canonicalized, with
 * the file name the zeroth entry in the errorv,
 * and the linenumber the second.
 * Return the new categorization of the error class.
 */
Errorclass
discardit(Eptr errorp)
{
	int i;
	Errorclass errorclass = errorp->error_e_class;

	switch (errorclass) {
		case C_SYNC:
		case C_NONSPEC:
		case C_UNKNOWN: return (errorclass);
		default:	;
	}
	if (errorp->error_lgtext < 2) {
		return (C_NONSPEC);
	}
	if (errorp->error_language == INLINT) {
		if (errorclass != C_NONSPEC) {	/* no file */
			for (i=0; lint_libs[i] != 0; i++) {
				if (strcmp(errorp->error_text[0], lint_libs[i]) == 0) {
					return (C_DISCARD);
				}
			}
		}
		/* check if the argument to the error message is to be ignored */
		if (ispunct((unsigned char)lastchar(errorp->error_text[2])))
			clob_last(errorp->error_text[2], '\0');
		if (search_ignore(errorp->error_text[errorclass == C_NONSPEC ? 0 : 2]) >= 0) {
			return (C_NULLED);
		}
	}
	return (errorclass);
}