open /dev/tty instead of ttyname(2) for input
[unix-history] / usr / src / usr.bin / error / filter.c
CommitLineData
3a5a3cc6
BJ
1static char *sccsid = "@(#)filter.c 1.1 (Berkeley) %G%";
2#include <stdio.h>
3#include <ctype.h>
4#include <pwd.h>
5#include "error.h"
6
7char *lint_libs[] = {
8 IG_FILE1,
9 IG_FILE2,
10 IG_FILE3,
11 IG_FILE4,
12 0
13};
14extern char* processname;
15int lexsort();
16/*
17 * Read the file ERRORNAME of the names of functions in lint
18 * to ignore complaints about.
19 */
20getignored(auxname)
21 char *auxname;
22{
23 register int i;
24 FILE *fyle;
25 char inbuffer[256];
26 int uid;
27 char filename[128];
28 char *username;
29 struct passwd *passwdentry;
30
31 nignored = 0;
32 if (auxname == 0){ /* use the default */
33 if ( (username = (char *)getlogin()) == NULL){
34 username = "Unknown";
35 uid = getuid();
36 if ( (passwdentry = (struct passwd *)getpwuid(uid)) == NULL){
37 return;
38 }
39 } else {
40 if ( (passwdentry = (struct passwd *)getpwnam(username)) == NULL)
41 return;
42 }
43 strcpy(filename, passwdentry->pw_dir);
44 strcat(filename, ERRORNAME);
45 } else
46 strcpy(filename, auxname);
47#ifdef FULLDEBUG
48 printf("Opening file \"%s\" to read names to ignore.\n",
49 filename);
50#endif
51 if ( (fyle = fopen(filename, "r")) == NULL){
52#ifdef FULLDEBUG
53 fprintf(stderr, "%s: Can't open file \"%s\"\n",
54 processname, filename);
55#endif
56 return;
57 }
58 /*
59 * Make the first pass through the file, counting lines
60 */
61 for (nignored = 0; fgets(inbuffer, 255, fyle) != NULL; nignored++)
62 continue;
63 names_ignored = (char **)Calloc(nignored+1, sizeof (char *));
64 fclose(fyle);
65 if (freopen(filename, "r", fyle) == NULL){
66#ifdef FULLDEBUG
67 fprintf(stderr, "%s: Failure to open \"%s\" for second read.\n",
68 processname, filename);
69#endif
70 nignored = 0;
71 return;
72 }
73 for (i=0; i < nignored && (fgets (inbuffer, 255, fyle) != NULL); i++){
74 names_ignored[i] = strsave(inbuffer);
75 substitute(names_ignored[i], '\n', '\0');
76 }
77 qsort(names_ignored, nignored, sizeof *names_ignored, lexsort);
78#ifdef FULLDEBUG
79 printf("Names to ignore follow.\n");
80 for (i=0; i < nignored; i++){
81 printf("\tIgnore: %s\n", names_ignored[i]);
82 }
83#endif
84}
85
86int lexsort(cpp1, cpp2)
87 char **cpp1, **cpp2;
88{
89 return(strcmp(*cpp1, *cpp2));
90}
91
92int search_ignore(key)
93 char *key;
94{
95 register int ub, lb;
96 register int halfway;
97 int order;
98
99 if (nignored == 0)
100 return(-1);
101 for(lb = 0, ub = nignored - 1; ub >= lb; ){
102 halfway = (ub + lb)/2;
103 if ( (order = strcmp(key, names_ignored[halfway])) == 0)
104 return(halfway);
105 if (order < 0) /*key is less than probe, throw away above*/
106 ub = halfway - 1;
107 else
108 lb = halfway + 1;
109 }
110 return(-1);
111}
112
113/*
114 * Tell if the error text is to be ignored.
115 * The error must have been canonicalized, with
116 * the file name the zeroth entry in the errorv,
117 * and the linenumber the second.
118 * Return the new categorization of the error class.
119 */
120Errorclass discardit(errorp)
121 register struct error_desc *errorp;
122{
123 int language;
124 register int i;
125 Errorclass errorclass = errorp->error_e_class;
126
127 switch(errorclass){
128 case C_SYNC:
129 case C_NONSPEC:
130 case C_UNKNOWN: return(errorclass);
131 default: ;
132 }
133 if(errorp->error_lgtext < 2){
134 return(C_NONSPEC);
135 }
136 language = errorp->error_language;
137 if(language == INLINT){
138 if (errorclass != C_NONSPEC){ /* no file */
139 for(i=0; lint_libs[i] != 0; i++){
140 if (strcmp(errorp->error_text[0], lint_libs[i]) == 0){
141 return(C_DISCARD);
142 }
143 }
144 }
145 /* check if the argument to the error message is to be ignored*/
146 if (ispunct(lastchar(errorp->error_text[2])))
147 clob_last(errorp->error_text[2], '\0');
148 if (search_ignore(errorp->error_text[errorclass == C_NONSPEC ? 0 : 2]) >= 0){
149 return(C_NULLED);
150 }
151 }
152 return(errorclass);
153}