xref: /original-bsd/usr.bin/error/main.c (revision 9a96b58b)
1 static	char *sccsid = "@(#)main.c	1.4 (Berkeley) 05/04/82";
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;			/* 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 	im_on = "/dev/tty";
128 	if ( (queryfile = fopen(im_on, "r")) == NULL){
129 		fprintf(stderr,"%s: Can't open \"%s\" to query the user.\n",
130 			processname, im_on);
131 		exit(9);
132 	}
133 	if (signal(SIGINT, onintr) == SIG_IGN)
134 		signal(SIGINT, SIG_IGN);
135 	if (signal(SIGTERM, onintr) == SIG_IGN)
136 		signal(SIGTERM, SIG_IGN);
137 	getignored(ignorename);
138 	eaterrors(&nerrors, &errors);
139 	if (Show_Errors)
140 		printerrors(TRUE, nerrors, errors);
141 	qsort(errors, nerrors, sizeof(Eptr), errorsort);
142 	if (show_errors)
143 		printerrors(FALSE, nerrors, errors);
144 	findfiles(nerrors, errors, &nfiles, &files);
145 #define P(msg, arg) fprintf(stdout, msg, arg)
146 	if (pr_summary){
147 	    if (nunknown)
148 	      P("%d Errors are unclassifiable.\n", nunknown);
149 	    if (nignore)
150 	      P("%d Errors are classifiable, but totally discarded.\n",nignore);
151 	    if (nsyncerrors)
152 	      P("%d Errors are synchronization errors.\n", nsyncerrors);
153 	    if (nignore)
154 	      P("%d Errors are discarded because they refer to sacrosinct files.\n", ndiscard);
155 	    if (nnulled)
156 	      P("%d Errors are nulled because they refer to specific functions.\n", nnulled);
157 	    if (nnonspec)
158 	      P("%d Errors are not specific to any file.\n", nnonspec);
159 	    if (nthisfile)
160 	      P("%d Errors are specific to a given file, but not to a line.\n", nthisfile);
161 	    if (ntrue)
162 	      P("%d Errors are true errors, and can be inserted into the files.\n", ntrue);
163 	}
164 	filenames(nfiles, files);
165 	fflush(stdout);
166 	if (touchfiles(nfiles, files, &ed_argc, &ed_argv) && edit_files)
167 		forkvi(ed_argc, ed_argv);
168 }
169 
170 forkvi(argc, argv)
171 	int	argc;
172 	char	**argv;
173 {
174 	if (query){
175 		switch(inquire(terse
176 		    ? "Edit? "
177 		    : "Do you still want to edit the files you touched? ")){
178 		case Q_NO:
179 		case Q_no:
180 			return;
181 		default:
182 			break;
183 		}
184 	}
185 	/*
186 	 *	ed_agument's first argument is
187 	 *	a vi/ex compatabile search argument
188 	 *	to find the first occurance of ###
189 	 */
190 	try("vi", argc, argv);
191 	try("ex", argc, argv);
192 	try("ed", argc-1, argv+1);
193 	fprintf(stdout, "Can't find any editors.\n");
194 }
195 
196 try(name, argc, argv)
197 	char	*name;
198 	int	argc;
199 	char	**argv;
200 {
201 	argv[0] = name;
202 	wordvprint(stdout, argc, argv);
203 	fprintf(stdout, "\n");
204 	fflush(stderr);
205 	fflush(stdout);
206 	sleep(2);
207 	if (freopen(im_on, "r", stdin) == NULL)
208 		return;
209 	if (freopen(im_on, "w", stdout) == NULL)
210 		return;
211 	execvp(name, argv);
212 }
213 
214 int errorsort(epp1, epp2)
215 		Eptr	*epp1, *epp2;
216 {
217 	reg	Eptr	ep1, ep2;
218 		int	order;
219 	/*
220 	 *	Sort by:
221 	 *	1)	synchronization, non specific, discarded errors first;
222 	 *	2)	nulled and true errors last
223 	 *		a)	grouped by similar file names
224 	 *			1)	grouped in ascending line number
225 	 */
226 	ep1 = *epp1; ep2 = *epp2;
227 	if (ep1 == 0 || ep2 == 0)
228 		return(0);
229 	if ( (NOTSORTABLE(ep1->error_e_class)) ^ (NOTSORTABLE(ep2->error_e_class))){
230 		return(NOTSORTABLE(ep1->error_e_class) ? -1 : 1);
231 	}
232 	if (NOTSORTABLE(ep1->error_e_class))	/* then both are */
233 		return(ep1->error_no - ep2->error_no);
234 	order = strcmp(ep1->error_text[0], ep2->error_text[0]);
235 	if (order == 0){
236 		return(ep1->error_line - ep2->error_line);
237 	}
238 	return(order);
239 }
240