xref: /csrg-svn/usr.bin/error/main.c (revision 16564)
1 static	char *sccsid = "@(#)main.c	1.5 (Berkeley) 06/05/84";
2 #include <stdio.h>
3 #include <ctype.h>
4 #include <signal.h>
5 #include "error.h"
6 
7 int	nerrors = 0;
8 Eptr	er_head;
9 Eptr	*errors;
10 
11 int	nfiles = 0;
12 Eptr	**files;	/* array of pointers into errors*/
13 int	language = INCC;
14 
15 char	*currentfilename = "????";
16 char	*processname;
17 char	im_on[] = "/dev/tty";	/* my tty name */
18 
19 boolean	query = FALSE;		/* query the operator if touch files */
20 boolean	notouch = FALSE;	/* don't touch ANY files */
21 boolean	piflag	= FALSE;	/* this is not pi */
22 boolean	terse	= FALSE;	/* Terse output */
23 
24 char	*suffixlist = ".*";	/* initially, can touch any file */
25 
26 int	errorsort();
27 int	onintr();
28 /*
29  *	error [-I ignorename] [-n] [-q] [-t suffixlist] [-s] [-v] [infile]
30  *
31  *	-T:	terse output
32  *
33  *	-I:	the following name, `ignorename' contains a list of
34  *		function names that are not to be treated as hard errors.
35  *		Default: ~/.errorsrc
36  *
37  *	-n:	don't touch ANY files!
38  *
39  *	-q:	The user is to be queried before touching each
40  *		file; if not specified, all files with hard, non
41  *		ignorable errors are touched (assuming they can be).
42  *
43  *	-t:	touch only files ending with the list of suffices, each
44  *		suffix preceded by a dot.
45  *		eg, -t .c.y.l
46  *		will touch only files ending with .c, .y or .l
47  *
48  *	-s:	print a summary of the error's categories.
49  *
50  *	-v:	after touching all files, overlay vi(1), ex(1) or ed(1)
51  *		on top of error, entered in the first file with
52  *		an error in it, with the appropriate editor
53  *		set up to use the "next" command to get the other
54  *		files containing errors.
55  *
56  *	-p:	(obsolete: for older versions of pi without bug
57  *		fix regarding printing out the name of the main file
58  *		with an error in it)
59  *		Take the following argument and use it as the name of
60  *		the pascal source file, suffix .p
61  *
62  *	-E:	show the errors in sorted order; intended for
63  *		debugging.
64  *
65  *	-S:	show the errors in unsorted order
66  *		(as they come from the error file)
67  *
68  *	infile:	The error messages come from this file.
69  *		Default: stdin
70  */
71 main(argc, argv)
72 	int	argc;
73 	char	*argv[];
74 {
75 	char	*cp;
76 	char	*ignorename = 0;
77 	int	ed_argc;
78 	char	**ed_argv;		/*return from touchfiles*/
79 	boolean	show_errors = FALSE;
80 	boolean	Show_Errors = FALSE;
81 	boolean	pr_summary = FALSE;
82 	boolean	edit_files = FALSE;
83 
84 	processname = argv[0];
85 
86 	errorfile = stdin;
87 	if (argc > 1) for(; (argc > 1) && (argv[1][0] == '-'); argc--, argv++){
88 		for (cp = argv[1] + 1; *cp; cp++) switch(*cp){
89 		default:
90 			fprintf(stderr, "%s: -%c: Unknown flag\n",
91 				processname, *cp);
92 			break;
93 
94 		case 'n':	notouch = TRUE;	break;
95 		case 'q':	query = TRUE;	break;
96 		case 'S':	Show_Errors = TRUE;	break;
97 		case 's':	pr_summary = TRUE;	break;
98 		case 'v':	edit_files = TRUE;	break;
99 		case 'T':	terse = TRUE;	break;
100 		case 't':
101 			*cp-- = 0; argv++; argc--;
102 			if (argc > 1){
103 				suffixlist = argv[1];
104 			}
105 			break;
106 		case 'I':	/*ignore file name*/
107 			*cp-- = 0; argv++; argc--;
108 			if (argc > 1)
109 				ignorename = argv[1];
110 			break;
111 		}
112 	}
113 	if (notouch)
114 		suffixlist = 0;
115 	if (argc > 1){
116 		if (argc > 3){
117 			fprintf(stderr, "%s: Only takes 0 or 1 arguments\n",
118 				processname);
119 			exit(3);
120 		}
121 		if ( (errorfile = fopen(argv[1], "r")) == NULL){
122 			fprintf(stderr, "%s: %s: No such file or directory for reading errors.\n",
123 				processname, argv[1]);
124 			exit(4);
125 		}
126 	}
127 	if ( (queryfile = fopen(im_on, "r")) == NULL){
128 		if (query){
129 			fprintf(stderr,
130 				"%s: Can't open \"%s\" to query the user.\n",
131 				processname, im_on);
132 			exit(9);
133 		}
134 	}
135 	if (signal(SIGINT, onintr) == SIG_IGN)
136 		signal(SIGINT, SIG_IGN);
137 	if (signal(SIGTERM, onintr) == SIG_IGN)
138 		signal(SIGTERM, SIG_IGN);
139 	getignored(ignorename);
140 	eaterrors(&nerrors, &errors);
141 	if (Show_Errors)
142 		printerrors(TRUE, nerrors, errors);
143 	qsort(errors, nerrors, sizeof(Eptr), errorsort);
144 	if (show_errors)
145 		printerrors(FALSE, nerrors, errors);
146 	findfiles(nerrors, errors, &nfiles, &files);
147 #define P(msg, arg) fprintf(stdout, msg, arg)
148 	if (pr_summary){
149 	    if (nunknown)
150 	      P("%d Errors are unclassifiable.\n", nunknown);
151 	    if (nignore)
152 	      P("%d Errors are classifiable, but totally discarded.\n",nignore);
153 	    if (nsyncerrors)
154 	      P("%d Errors are synchronization errors.\n", nsyncerrors);
155 	    if (nignore)
156 	      P("%d Errors are discarded because they refer to sacrosinct files.\n", ndiscard);
157 	    if (nnulled)
158 	      P("%d Errors are nulled because they refer to specific functions.\n", nnulled);
159 	    if (nnonspec)
160 	      P("%d Errors are not specific to any file.\n", nnonspec);
161 	    if (nthisfile)
162 	      P("%d Errors are specific to a given file, but not to a line.\n", nthisfile);
163 	    if (ntrue)
164 	      P("%d Errors are true errors, and can be inserted into the files.\n", ntrue);
165 	}
166 	filenames(nfiles, files);
167 	fflush(stdout);
168 	if (touchfiles(nfiles, files, &ed_argc, &ed_argv) && edit_files)
169 		forkvi(ed_argc, ed_argv);
170 }
171 
172 forkvi(argc, argv)
173 	int	argc;
174 	char	**argv;
175 {
176 	if (query){
177 		switch(inquire(terse
178 		    ? "Edit? "
179 		    : "Do you still want to edit the files you touched? ")){
180 		case Q_NO:
181 		case Q_no:
182 			return;
183 		default:
184 			break;
185 		}
186 	}
187 	/*
188 	 *	ed_agument's first argument is
189 	 *	a vi/ex compatabile search argument
190 	 *	to find the first occurance of ###
191 	 */
192 	try("vi", argc, argv);
193 	try("ex", argc, argv);
194 	try("ed", argc-1, argv+1);
195 	fprintf(stdout, "Can't find any editors.\n");
196 }
197 
198 try(name, argc, argv)
199 	char	*name;
200 	int	argc;
201 	char	**argv;
202 {
203 	argv[0] = name;
204 	wordvprint(stdout, argc, argv);
205 	fprintf(stdout, "\n");
206 	fflush(stderr);
207 	fflush(stdout);
208 	sleep(2);
209 	if (freopen(im_on, "r", stdin) == NULL)
210 		return;
211 	if (freopen(im_on, "w", stdout) == NULL)
212 		return;
213 	execvp(name, argv);
214 }
215 
216 int errorsort(epp1, epp2)
217 		Eptr	*epp1, *epp2;
218 {
219 	reg	Eptr	ep1, ep2;
220 		int	order;
221 	/*
222 	 *	Sort by:
223 	 *	1)	synchronization, non specific, discarded errors first;
224 	 *	2)	nulled and true errors last
225 	 *		a)	grouped by similar file names
226 	 *			1)	grouped in ascending line number
227 	 */
228 	ep1 = *epp1; ep2 = *epp2;
229 	if (ep1 == 0 || ep2 == 0)
230 		return(0);
231 	if ( (NOTSORTABLE(ep1->error_e_class)) ^ (NOTSORTABLE(ep2->error_e_class))){
232 		return(NOTSORTABLE(ep1->error_e_class) ? -1 : 1);
233 	}
234 	if (NOTSORTABLE(ep1->error_e_class))	/* then both are */
235 		return(ep1->error_no - ep2->error_no);
236 	order = strcmp(ep1->error_text[0], ep2->error_text[0]);
237 	if (order == 0){
238 		return(ep1->error_line - ep2->error_line);
239 	}
240 	return(order);
241 }
242