main.c revision 1.21 1 1.21 christos /* $NetBSD: main.c,v 1.21 2011/08/17 13:11:22 christos Exp $ */
2 1.3 jtc
3 1.1 cgd /*
4 1.3 jtc * Copyright (c) 1980, 1993
5 1.3 jtc * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.8 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.4 lukem #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.11 lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1993\
35 1.11 lukem The Regents of the University of California. All rights reserved.");
36 1.1 cgd #endif /* not lint */
37 1.1 cgd
38 1.1 cgd #ifndef lint
39 1.3 jtc #if 0
40 1.3 jtc static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
41 1.3 jtc #endif
42 1.21 christos __RCSID("$NetBSD: main.c,v 1.21 2011/08/17 13:11:22 christos Exp $");
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #include <signal.h>
46 1.1 cgd #include <unistd.h>
47 1.1 cgd #include <stdio.h>
48 1.1 cgd #include <ctype.h>
49 1.1 cgd #include <stdlib.h>
50 1.1 cgd #include <string.h>
51 1.18 christos #include <err.h>
52 1.1 cgd #include "error.h"
53 1.1 cgd #include "pathnames.h"
54 1.1 cgd
55 1.13 dholland FILE *errorfile; /* where error file comes from */
56 1.20 christos FILE *queryfile; /* input for the query responses from the user */
57 1.5 wsanchez
58 1.13 dholland int nignored;
59 1.13 dholland char **names_ignored;
60 1.5 wsanchez
61 1.18 christos size_t filelevel = 0;
62 1.14 dholland int nerrors = 0;
63 1.13 dholland Eptr er_head;
64 1.12 dholland static Eptr *errors;
65 1.1 cgd
66 1.13 dholland int nfiles = 0;
67 1.13 dholland Eptr **files; /* array of pointers into errors*/
68 1.5 wsanchez boolean *touchedfiles; /* which files we touched */
69 1.13 dholland int language = INCC;
70 1.1 cgd
71 1.21 christos char default_currentfilename[] = "????";
72 1.17 dholland char *currentfilename = default_currentfilename;
73 1.1 cgd
74 1.16 dholland boolean query = false; /* query the operator if touch files */
75 1.16 dholland boolean terse = false; /* Terse output */
76 1.1 cgd
77 1.12 dholland static char im_on[] = _PATH_TTY; /* my tty name */
78 1.16 dholland static boolean notouch = false; /* don't touch ANY files */
79 1.12 dholland
80 1.17 dholland const char *suffixlist = ".*"; /* initially, can touch any file */
81 1.1 cgd
82 1.12 dholland static int errorsort(const void *, const void *);
83 1.12 dholland static void forkvi(int, char **);
84 1.16 dholland static void try(const char *, int, char **);
85 1.18 christos static void usage(void) __attribute__((__noreturn__));
86 1.4 lukem
87 1.1 cgd /*
88 1.18 christos * error [-nqSsTv] [-I <ignorename>] [-t <suffixlist>] [-p <level>] <infile>
89 1.1 cgd *
90 1.1 cgd * -I: the following name, `ignorename' contains a list of
91 1.1 cgd * function names that are not to be treated as hard errors.
92 1.1 cgd * Default: ~/.errorsrc
93 1.1 cgd *
94 1.1 cgd * -n: don't touch ANY files!
95 1.1 cgd *
96 1.18 christos * -p: take the next argument as the number of levels to skip
97 1.18 christos * from the filename, like perl.
98 1.18 christos *
99 1.1 cgd * -q: The user is to be queried before touching each
100 1.1 cgd * file; if not specified, all files with hard, non
101 1.1 cgd * ignorable errors are touched (assuming they can be).
102 1.1 cgd *
103 1.18 christos * -S: show the errors in unsorted order
104 1.18 christos * (as they come from the error file)
105 1.18 christos *
106 1.18 christos * -s: print a summary of the error's categories.
107 1.18 christos *
108 1.18 christos * -T: terse output
109 1.18 christos *
110 1.9 wiz * -t: touch only files ending with the list of suffixes, each
111 1.1 cgd * suffix preceded by a dot.
112 1.1 cgd * eg, -t .c.y.l
113 1.1 cgd * will touch only files ending with .c, .y or .l
114 1.1 cgd *
115 1.1 cgd * -v: after touching all files, overlay vi(1), ex(1) or ed(1)
116 1.1 cgd * on top of error, entered in the first file with
117 1.1 cgd * an error in it, with the appropriate editor
118 1.1 cgd * set up to use the "next" command to get the other
119 1.1 cgd * files containing errors.
120 1.1 cgd *
121 1.1 cgd * infile: The error messages come from this file.
122 1.1 cgd * Default: stdin
123 1.1 cgd */
124 1.4 lukem int
125 1.7 wiz main(int argc, char **argv)
126 1.1 cgd {
127 1.18 christos int c;
128 1.13 dholland char *ignorename = 0;
129 1.13 dholland int ed_argc;
130 1.13 dholland char **ed_argv; /* return from touchfiles */
131 1.16 dholland boolean show_errors = false;
132 1.16 dholland boolean Show_Errors = false;
133 1.16 dholland boolean pr_summary = false;
134 1.16 dholland boolean edit_files = false;
135 1.1 cgd
136 1.18 christos setprogname(argv[0]);
137 1.1 cgd
138 1.1 cgd errorfile = stdin;
139 1.18 christos while ((c = getopt(argc, argv, "I:np:qSsTt:v")) != -1)
140 1.18 christos switch (c) {
141 1.18 christos case 'I': /*ignore file name*/
142 1.18 christos ignorename = optarg;
143 1.18 christos break;
144 1.18 christos case 'n':
145 1.18 christos notouch = true;
146 1.18 christos break;
147 1.18 christos case 'p':
148 1.18 christos filelevel = (size_t)strtol(optarg, NULL, 0);
149 1.18 christos break;
150 1.18 christos case 'q':
151 1.18 christos query = true;
152 1.18 christos break;
153 1.18 christos case 'S':
154 1.18 christos Show_Errors = true;
155 1.18 christos break;
156 1.18 christos case 's':
157 1.18 christos pr_summary = true;
158 1.18 christos break;
159 1.18 christos case 'T':
160 1.18 christos terse = true;
161 1.18 christos break;
162 1.18 christos case 't':
163 1.18 christos suffixlist = optarg;
164 1.18 christos break;
165 1.18 christos case 'v':
166 1.18 christos edit_files = true;
167 1.18 christos break;
168 1.18 christos default:
169 1.18 christos usage();
170 1.1 cgd }
171 1.18 christos
172 1.18 christos argv += optind;
173 1.18 christos argc -= optind;
174 1.20 christos
175 1.20 christos switch (argc) {
176 1.20 christos case 0:
177 1.20 christos break;
178 1.20 christos case 1:
179 1.20 christos if ((errorfile = fopen(argv[0], "r")) == NULL)
180 1.20 christos err(1, "Cannot open `%s' to read errors", argv[0]);
181 1.20 christos break;
182 1.20 christos default:
183 1.20 christos usage();
184 1.20 christos }
185 1.20 christos
186 1.1 cgd if (notouch)
187 1.1 cgd suffixlist = 0;
188 1.20 christos
189 1.20 christos
190 1.13 dholland if ((queryfile = fopen(im_on, "r")) == NULL) {
191 1.18 christos if (query)
192 1.18 christos err(1, "Cannot open `%s' to query the user", im_on);
193 1.1 cgd }
194 1.1 cgd if (signal(SIGINT, onintr) == SIG_IGN)
195 1.1 cgd signal(SIGINT, SIG_IGN);
196 1.1 cgd if (signal(SIGTERM, onintr) == SIG_IGN)
197 1.1 cgd signal(SIGTERM, SIG_IGN);
198 1.1 cgd getignored(ignorename);
199 1.1 cgd eaterrors(&nerrors, &errors);
200 1.1 cgd if (Show_Errors)
201 1.16 dholland printerrors(true, nerrors, errors);
202 1.1 cgd qsort(errors, nerrors, sizeof(Eptr), errorsort);
203 1.1 cgd if (show_errors)
204 1.16 dholland printerrors(false, nerrors, errors);
205 1.1 cgd findfiles(nerrors, errors, &nfiles, &files);
206 1.1 cgd #define P(msg, arg) fprintf(stdout, msg, arg)
207 1.13 dholland if (pr_summary) {
208 1.20 christos if (nunknown)
209 1.20 christos P("%d Errors are unclassifiable.\n", nunknown);
210 1.20 christos if (nignore)
211 1.20 christos P("%d Errors are classifiable, but totally "
212 1.20 christos "discarded.\n", nignore);
213 1.20 christos if (nsyncerrors)
214 1.20 christos P("%d Errors are synchronization errors.\n",
215 1.20 christos nsyncerrors);
216 1.20 christos if (nignore)
217 1.20 christos P("%d Errors are discarded because they refer to "
218 1.20 christos "sacrosinct files.\n", ndiscard);
219 1.20 christos if (nnulled)
220 1.20 christos P("%d Errors are nulled because they refer to specific "
221 1.20 christos "functions.\n", nnulled);
222 1.20 christos if (nnonspec)
223 1.20 christos P("%d Errors are not specific to any file.\n",
224 1.20 christos nnonspec);
225 1.20 christos if (nthisfile)
226 1.20 christos P("%d Errors are specific to a given file, but not "
227 1.20 christos "to a line.\n", nthisfile);
228 1.20 christos if (ntrue)
229 1.20 christos P("%d Errors are true errors, and can be inserted "
230 1.20 christos "into the files.\n", ntrue);
231 1.1 cgd }
232 1.1 cgd filenames(nfiles, files);
233 1.1 cgd fflush(stdout);
234 1.1 cgd if (touchfiles(nfiles, files, &ed_argc, &ed_argv) && edit_files)
235 1.1 cgd forkvi(ed_argc, ed_argv);
236 1.20 christos return 0;
237 1.1 cgd }
238 1.1 cgd
239 1.12 dholland static void
240 1.7 wiz forkvi(int argc, char **argv)
241 1.1 cgd {
242 1.13 dholland if (query) {
243 1.13 dholland switch (inquire(terse
244 1.1 cgd ? "Edit? "
245 1.13 dholland : "Do you still want to edit the files you touched? ")) {
246 1.10 lukem case Q_error:
247 1.1 cgd case Q_NO:
248 1.1 cgd case Q_no:
249 1.1 cgd return;
250 1.1 cgd default:
251 1.1 cgd break;
252 1.1 cgd }
253 1.1 cgd }
254 1.1 cgd /*
255 1.1 cgd * ed_agument's first argument is
256 1.6 wiz * a vi/ex compatible search argument
257 1.16 dholland * to find the first occurrence of ###
258 1.1 cgd */
259 1.1 cgd try("vi", argc, argv);
260 1.1 cgd try("ex", argc, argv);
261 1.1 cgd try("ed", argc-1, argv+1);
262 1.1 cgd fprintf(stdout, "Can't find any editors.\n");
263 1.1 cgd }
264 1.1 cgd
265 1.12 dholland static void
266 1.16 dholland try(const char *name, int argc, char **argv)
267 1.1 cgd {
268 1.16 dholland argv[0] = __UNCONST(name);
269 1.1 cgd wordvprint(stdout, argc, argv);
270 1.1 cgd fprintf(stdout, "\n");
271 1.1 cgd fflush(stderr);
272 1.1 cgd fflush(stdout);
273 1.1 cgd sleep(2);
274 1.1 cgd if (freopen(im_on, "r", stdin) == NULL)
275 1.1 cgd return;
276 1.1 cgd if (freopen(im_on, "w", stdout) == NULL)
277 1.1 cgd return;
278 1.1 cgd execvp(name, argv);
279 1.1 cgd }
280 1.1 cgd
281 1.12 dholland static int
282 1.12 dholland errorsort(const void *x1, const void *x2)
283 1.1 cgd {
284 1.15 dholland const Eptr *epp1 = x1;
285 1.15 dholland const Eptr *epp2 = x2;
286 1.13 dholland Eptr ep1, ep2;
287 1.13 dholland int order;
288 1.13 dholland
289 1.1 cgd /*
290 1.13 dholland * Sort by:
291 1.13 dholland * 1) synchronization, non specific, discarded errors first;
292 1.13 dholland * 2) nulled and true errors last
293 1.13 dholland * a) grouped by similar file names
294 1.13 dholland * 1) grouped in ascending line number
295 1.1 cgd */
296 1.1 cgd ep1 = *epp1; ep2 = *epp2;
297 1.1 cgd if (ep1 == 0 || ep2 == 0)
298 1.20 christos return 0;
299 1.20 christos if (NOTSORTABLE(ep1->error_e_class) ^ NOTSORTABLE(ep2->error_e_class)) {
300 1.20 christos return NOTSORTABLE(ep1->error_e_class) ? -1 : 1;
301 1.1 cgd }
302 1.1 cgd if (NOTSORTABLE(ep1->error_e_class)) /* then both are */
303 1.20 christos return ep1->error_no - ep2->error_no;
304 1.1 cgd order = strcmp(ep1->error_text[0], ep2->error_text[0]);
305 1.13 dholland if (order == 0) {
306 1.20 christos return ep1->error_line - ep2->error_line;
307 1.1 cgd }
308 1.20 christos return order;
309 1.1 cgd }
310 1.18 christos
311 1.18 christos static void
312 1.18 christos usage(void)
313 1.18 christos {
314 1.19 wiz fprintf(stderr, "Usage: %s [-nqSsTv] [-I ignorefile] "
315 1.19 wiz "[-p filelevel] [-t suffixlist] [name]\n", getprogname());
316 1.18 christos exit(1);
317 1.18 christos }
318