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