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