xref: /netbsd-src/usr.bin/error/main.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /*	$NetBSD: main.c,v 1.3 1995/09/02 06:15:37 jtc Exp $	*/
2 
3 /*
4  * Copyright (c) 1980, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 static char copyright[] =
38 "@(#) Copyright (c) 1980, 1993\n\
39 	The Regents of the University of California.  All rights reserved.\n";
40 #endif /* not lint */
41 
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)main.c	8.1 (Berkeley) 6/6/93";
45 #endif
46 static char rcsid[] = "$NetBSD: main.c,v 1.3 1995/09/02 06:15:37 jtc Exp $";
47 #endif /* not lint */
48 
49 #include <signal.h>
50 #include <unistd.h>
51 #include <stdio.h>
52 #include <ctype.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include "error.h"
56 #include "pathnames.h"
57 
58 int	nerrors = 0;
59 Eptr	er_head;
60 Eptr	*errors;
61 
62 int	nfiles = 0;
63 Eptr	**files;	/* array of pointers into errors*/
64 int	language = INCC;
65 
66 char	*currentfilename = "????";
67 char	*processname;
68 char	im_on[] = _PATH_TTY;	/* my tty name */
69 
70 boolean	query = FALSE;		/* query the operator if touch files */
71 boolean	notouch = FALSE;	/* don't touch ANY files */
72 boolean	piflag	= FALSE;	/* this is not pi */
73 boolean	terse	= FALSE;	/* Terse output */
74 
75 char	*suffixlist = ".*";	/* initially, can touch any file */
76 
77 int	errorsort();
78 void	onintr();
79 /*
80  *	error [-I ignorename] [-n] [-q] [-t suffixlist] [-s] [-v] [infile]
81  *
82  *	-T:	terse output
83  *
84  *	-I:	the following name, `ignorename' contains a list of
85  *		function names that are not to be treated as hard errors.
86  *		Default: ~/.errorsrc
87  *
88  *	-n:	don't touch ANY files!
89  *
90  *	-q:	The user is to be queried before touching each
91  *		file; if not specified, all files with hard, non
92  *		ignorable errors are touched (assuming they can be).
93  *
94  *	-t:	touch only files ending with the list of suffices, each
95  *		suffix preceded by a dot.
96  *		eg, -t .c.y.l
97  *		will touch only files ending with .c, .y or .l
98  *
99  *	-s:	print a summary of the error's categories.
100  *
101  *	-v:	after touching all files, overlay vi(1), ex(1) or ed(1)
102  *		on top of error, entered in the first file with
103  *		an error in it, with the appropriate editor
104  *		set up to use the "next" command to get the other
105  *		files containing errors.
106  *
107  *	-p:	(obsolete: for older versions of pi without bug
108  *		fix regarding printing out the name of the main file
109  *		with an error in it)
110  *		Take the following argument and use it as the name of
111  *		the pascal source file, suffix .p
112  *
113  *	-E:	show the errors in sorted order; intended for
114  *		debugging.
115  *
116  *	-S:	show the errors in unsorted order
117  *		(as they come from the error file)
118  *
119  *	infile:	The error messages come from this file.
120  *		Default: stdin
121  */
122 main(argc, argv)
123 	int	argc;
124 	char	*argv[];
125 {
126 	char	*cp;
127 	char	*ignorename = 0;
128 	int	ed_argc;
129 	char	**ed_argv;		/*return from touchfiles*/
130 	boolean	show_errors = FALSE;
131 	boolean	Show_Errors = FALSE;
132 	boolean	pr_summary = FALSE;
133 	boolean	edit_files = FALSE;
134 
135 	processname = argv[0];
136 
137 	errorfile = stdin;
138 	if (argc > 1) for(; (argc > 1) && (argv[1][0] == '-'); argc--, argv++){
139 		for (cp = argv[1] + 1; *cp; cp++) switch(*cp){
140 		default:
141 			fprintf(stderr, "%s: -%c: Unknown flag\n",
142 				processname, *cp);
143 			break;
144 
145 		case 'n':	notouch = TRUE;	break;
146 		case 'q':	query = TRUE;	break;
147 		case 'S':	Show_Errors = TRUE;	break;
148 		case 's':	pr_summary = TRUE;	break;
149 		case 'v':	edit_files = TRUE;	break;
150 		case 'T':	terse = TRUE;	break;
151 		case 't':
152 			*cp-- = 0; argv++; argc--;
153 			if (argc > 1){
154 				suffixlist = argv[1];
155 			}
156 			break;
157 		case 'I':	/*ignore file name*/
158 			*cp-- = 0; argv++; argc--;
159 			if (argc > 1)
160 				ignorename = argv[1];
161 			break;
162 		}
163 	}
164 	if (notouch)
165 		suffixlist = 0;
166 	if (argc > 1){
167 		if (argc > 3){
168 			fprintf(stderr, "%s: Only takes 0 or 1 arguments\n",
169 				processname);
170 			exit(3);
171 		}
172 		if ( (errorfile = fopen(argv[1], "r")) == NULL){
173 			fprintf(stderr, "%s: %s: No such file or directory for reading errors.\n",
174 				processname, argv[1]);
175 			exit(4);
176 		}
177 	}
178 	if ( (queryfile = fopen(im_on, "r")) == NULL){
179 		if (query){
180 			fprintf(stderr,
181 				"%s: Can't open \"%s\" to query the user.\n",
182 				processname, im_on);
183 			exit(9);
184 		}
185 	}
186 	if (signal(SIGINT, onintr) == SIG_IGN)
187 		signal(SIGINT, SIG_IGN);
188 	if (signal(SIGTERM, onintr) == SIG_IGN)
189 		signal(SIGTERM, SIG_IGN);
190 	getignored(ignorename);
191 	eaterrors(&nerrors, &errors);
192 	if (Show_Errors)
193 		printerrors(TRUE, nerrors, errors);
194 	qsort(errors, nerrors, sizeof(Eptr), errorsort);
195 	if (show_errors)
196 		printerrors(FALSE, nerrors, errors);
197 	findfiles(nerrors, errors, &nfiles, &files);
198 #define P(msg, arg) fprintf(stdout, msg, arg)
199 	if (pr_summary){
200 	    if (nunknown)
201 	      P("%d Errors are unclassifiable.\n", nunknown);
202 	    if (nignore)
203 	      P("%d Errors are classifiable, but totally discarded.\n",nignore);
204 	    if (nsyncerrors)
205 	      P("%d Errors are synchronization errors.\n", nsyncerrors);
206 	    if (nignore)
207 	      P("%d Errors are discarded because they refer to sacrosinct files.\n", ndiscard);
208 	    if (nnulled)
209 	      P("%d Errors are nulled because they refer to specific functions.\n", nnulled);
210 	    if (nnonspec)
211 	      P("%d Errors are not specific to any file.\n", nnonspec);
212 	    if (nthisfile)
213 	      P("%d Errors are specific to a given file, but not to a line.\n", nthisfile);
214 	    if (ntrue)
215 	      P("%d Errors are true errors, and can be inserted into the files.\n", ntrue);
216 	}
217 	filenames(nfiles, files);
218 	fflush(stdout);
219 	if (touchfiles(nfiles, files, &ed_argc, &ed_argv) && edit_files)
220 		forkvi(ed_argc, ed_argv);
221 }
222 
223 forkvi(argc, argv)
224 	int	argc;
225 	char	**argv;
226 {
227 	if (query){
228 		switch(inquire(terse
229 		    ? "Edit? "
230 		    : "Do you still want to edit the files you touched? ")){
231 		case Q_NO:
232 		case Q_no:
233 			return;
234 		default:
235 			break;
236 		}
237 	}
238 	/*
239 	 *	ed_agument's first argument is
240 	 *	a vi/ex compatabile search argument
241 	 *	to find the first occurance of ###
242 	 */
243 	try("vi", argc, argv);
244 	try("ex", argc, argv);
245 	try("ed", argc-1, argv+1);
246 	fprintf(stdout, "Can't find any editors.\n");
247 }
248 
249 try(name, argc, argv)
250 	char	*name;
251 	int	argc;
252 	char	**argv;
253 {
254 	argv[0] = name;
255 	wordvprint(stdout, argc, argv);
256 	fprintf(stdout, "\n");
257 	fflush(stderr);
258 	fflush(stdout);
259 	sleep(2);
260 	if (freopen(im_on, "r", stdin) == NULL)
261 		return;
262 	if (freopen(im_on, "w", stdout) == NULL)
263 		return;
264 	execvp(name, argv);
265 }
266 
267 int errorsort(epp1, epp2)
268 		Eptr	*epp1, *epp2;
269 {
270 	reg	Eptr	ep1, ep2;
271 		int	order;
272 	/*
273 	 *	Sort by:
274 	 *	1)	synchronization, non specific, discarded errors first;
275 	 *	2)	nulled and true errors last
276 	 *		a)	grouped by similar file names
277 	 *			1)	grouped in ascending line number
278 	 */
279 	ep1 = *epp1; ep2 = *epp2;
280 	if (ep1 == 0 || ep2 == 0)
281 		return(0);
282 	if ( (NOTSORTABLE(ep1->error_e_class)) ^ (NOTSORTABLE(ep2->error_e_class))){
283 		return(NOTSORTABLE(ep1->error_e_class) ? -1 : 1);
284 	}
285 	if (NOTSORTABLE(ep1->error_e_class))	/* then both are */
286 		return(ep1->error_no - ep2->error_no);
287 	order = strcmp(ep1->error_text[0], ep2->error_text[0]);
288 	if (order == 0){
289 		return(ep1->error_line - ep2->error_line);
290 	}
291 	return(order);
292 }
293