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