xref: /netbsd-src/usr.bin/error/main.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: main.c,v 1.4 1997/10/18 14:44:35 lukem 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 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) 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 __RCSID("$NetBSD: main.c,v 1.4 1997/10/18 14:44:35 lukem 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 __P((const void *, const void *));
78 void	forkvi __P((int, char **));
79 int	main __P((int, char **));
80 void	try __P((char *, int, char **));
81 
82 /*
83  *	error [-I ignorename] [-n] [-q] [-t suffixlist] [-s] [-v] [infile]
84  *
85  *	-T:	terse output
86  *
87  *	-I:	the following name, `ignorename' contains a list of
88  *		function names that are not to be treated as hard errors.
89  *		Default: ~/.errorsrc
90  *
91  *	-n:	don't touch ANY files!
92  *
93  *	-q:	The user is to be queried before touching each
94  *		file; if not specified, all files with hard, non
95  *		ignorable errors are touched (assuming they can be).
96  *
97  *	-t:	touch only files ending with the list of suffices, each
98  *		suffix preceded by a dot.
99  *		eg, -t .c.y.l
100  *		will touch only files ending with .c, .y or .l
101  *
102  *	-s:	print a summary of the error's categories.
103  *
104  *	-v:	after touching all files, overlay vi(1), ex(1) or ed(1)
105  *		on top of error, entered in the first file with
106  *		an error in it, with the appropriate editor
107  *		set up to use the "next" command to get the other
108  *		files containing errors.
109  *
110  *	-p:	(obsolete: for older versions of pi without bug
111  *		fix regarding printing out the name of the main file
112  *		with an error in it)
113  *		Take the following argument and use it as the name of
114  *		the pascal source file, suffix .p
115  *
116  *	-E:	show the errors in sorted order; intended for
117  *		debugging.
118  *
119  *	-S:	show the errors in unsorted order
120  *		(as they come from the error file)
121  *
122  *	infile:	The error messages come from this file.
123  *		Default: stdin
124  */
125 int
126 main(argc, argv)
127 	int	argc;
128 	char	*argv[];
129 {
130 	char	*cp;
131 	char	*ignorename = 0;
132 	int	ed_argc;
133 	char	**ed_argv;		/*return from touchfiles*/
134 	boolean	show_errors = FALSE;
135 	boolean	Show_Errors = FALSE;
136 	boolean	pr_summary = FALSE;
137 	boolean	edit_files = FALSE;
138 
139 	processname = argv[0];
140 
141 	errorfile = stdin;
142 	if (argc > 1) for(; (argc > 1) && (argv[1][0] == '-'); argc--, argv++){
143 		for (cp = argv[1] + 1; *cp; cp++) switch(*cp){
144 		default:
145 			fprintf(stderr, "%s: -%c: Unknown flag\n",
146 				processname, *cp);
147 			break;
148 
149 		case 'n':	notouch = TRUE;	break;
150 		case 'q':	query = TRUE;	break;
151 		case 'S':	Show_Errors = TRUE;	break;
152 		case 's':	pr_summary = TRUE;	break;
153 		case 'v':	edit_files = TRUE;	break;
154 		case 'T':	terse = TRUE;	break;
155 		case 't':
156 			*cp-- = 0; argv++; argc--;
157 			if (argc > 1){
158 				suffixlist = argv[1];
159 			}
160 			break;
161 		case 'I':	/*ignore file name*/
162 			*cp-- = 0; argv++; argc--;
163 			if (argc > 1)
164 				ignorename = argv[1];
165 			break;
166 		}
167 	}
168 	if (notouch)
169 		suffixlist = 0;
170 	if (argc > 1){
171 		if (argc > 3){
172 			fprintf(stderr, "%s: Only takes 0 or 1 arguments\n",
173 				processname);
174 			exit(3);
175 		}
176 		if ( (errorfile = fopen(argv[1], "r")) == NULL){
177 			fprintf(stderr, "%s: %s: No such file or directory for reading errors.\n",
178 				processname, argv[1]);
179 			exit(4);
180 		}
181 	}
182 	if ( (queryfile = fopen(im_on, "r")) == NULL){
183 		if (query){
184 			fprintf(stderr,
185 				"%s: Can't open \"%s\" to query the user.\n",
186 				processname, im_on);
187 			exit(9);
188 		}
189 	}
190 	if (signal(SIGINT, onintr) == SIG_IGN)
191 		signal(SIGINT, SIG_IGN);
192 	if (signal(SIGTERM, onintr) == SIG_IGN)
193 		signal(SIGTERM, SIG_IGN);
194 	getignored(ignorename);
195 	eaterrors(&nerrors, &errors);
196 	if (Show_Errors)
197 		printerrors(TRUE, nerrors, errors);
198 	qsort(errors, nerrors, sizeof(Eptr), errorsort);
199 	if (show_errors)
200 		printerrors(FALSE, nerrors, errors);
201 	findfiles(nerrors, errors, &nfiles, &files);
202 #define P(msg, arg) fprintf(stdout, msg, arg)
203 	if (pr_summary){
204 	    if (nunknown)
205 	      P("%d Errors are unclassifiable.\n", nunknown);
206 	    if (nignore)
207 	      P("%d Errors are classifiable, but totally discarded.\n",nignore);
208 	    if (nsyncerrors)
209 	      P("%d Errors are synchronization errors.\n", nsyncerrors);
210 	    if (nignore)
211 	      P("%d Errors are discarded because they refer to sacrosinct files.\n", ndiscard);
212 	    if (nnulled)
213 	      P("%d Errors are nulled because they refer to specific functions.\n", nnulled);
214 	    if (nnonspec)
215 	      P("%d Errors are not specific to any file.\n", nnonspec);
216 	    if (nthisfile)
217 	      P("%d Errors are specific to a given file, but not to a line.\n", nthisfile);
218 	    if (ntrue)
219 	      P("%d Errors are true errors, and can be inserted into the files.\n", ntrue);
220 	}
221 	filenames(nfiles, files);
222 	fflush(stdout);
223 	if (touchfiles(nfiles, files, &ed_argc, &ed_argv) && edit_files)
224 		forkvi(ed_argc, ed_argv);
225 	return (0);
226 }
227 
228 void
229 forkvi(argc, argv)
230 	int	argc;
231 	char	**argv;
232 {
233 	if (query){
234 		switch(inquire(terse
235 		    ? "Edit? "
236 		    : "Do you still want to edit the files you touched? ")){
237 		case Q_NO:
238 		case Q_no:
239 			return;
240 		default:
241 			break;
242 		}
243 	}
244 	/*
245 	 *	ed_agument's first argument is
246 	 *	a vi/ex compatabile search argument
247 	 *	to find the first occurance of ###
248 	 */
249 	try("vi", argc, argv);
250 	try("ex", argc, argv);
251 	try("ed", argc-1, argv+1);
252 	fprintf(stdout, "Can't find any editors.\n");
253 }
254 
255 void
256 try(name, argc, argv)
257 	char	*name;
258 	int	argc;
259 	char	**argv;
260 {
261 	argv[0] = name;
262 	wordvprint(stdout, argc, argv);
263 	fprintf(stdout, "\n");
264 	fflush(stderr);
265 	fflush(stdout);
266 	sleep(2);
267 	if (freopen(im_on, "r", stdin) == NULL)
268 		return;
269 	if (freopen(im_on, "w", stdout) == NULL)
270 		return;
271 	execvp(name, argv);
272 }
273 
274 int errorsort(x1, x2)
275 	const void *x1, *x2;
276 {
277 	Eptr	*epp1 = (Eptr *)x1, *epp2 = (Eptr *)x2;
278 	Eptr	ep1, ep2;
279 	int	order;
280 	/*
281 	 *	Sort by:
282 	 *	1)	synchronization, non specific, discarded errors first;
283 	 *	2)	nulled and true errors last
284 	 *		a)	grouped by similar file names
285 	 *			1)	grouped in ascending line number
286 	 */
287 	ep1 = *epp1; ep2 = *epp2;
288 	if (ep1 == 0 || ep2 == 0)
289 		return(0);
290 	if ( (NOTSORTABLE(ep1->error_e_class)) ^ (NOTSORTABLE(ep2->error_e_class))){
291 		return(NOTSORTABLE(ep1->error_e_class) ? -1 : 1);
292 	}
293 	if (NOTSORTABLE(ep1->error_e_class))	/* then both are */
294 		return(ep1->error_no - ep2->error_no);
295 	order = strcmp(ep1->error_text[0], ep2->error_text[0]);
296 	if (order == 0){
297 		return(ep1->error_line - ep2->error_line);
298 	}
299 	return(order);
300 }
301