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