main.c revision 1.17 1 1.17 dholland /* $NetBSD: main.c,v 1.17 2009/08/13 06:59:37 dholland 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.17 dholland __RCSID("$NetBSD: main.c,v 1.17 2009/08/13 06:59:37 dholland 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.1 cgd #include "error.h"
52 1.1 cgd #include "pathnames.h"
53 1.1 cgd
54 1.13 dholland FILE *errorfile; /* where error file comes from */
55 1.13 dholland FILE *queryfile; /* where the query responses from the user come from*/
56 1.5 wsanchez
57 1.13 dholland int nignored;
58 1.13 dholland char **names_ignored;
59 1.5 wsanchez
60 1.14 dholland int nerrors = 0;
61 1.13 dholland Eptr er_head;
62 1.12 dholland static Eptr *errors;
63 1.1 cgd
64 1.13 dholland int nfiles = 0;
65 1.13 dholland Eptr **files; /* array of pointers into errors*/
66 1.5 wsanchez boolean *touchedfiles; /* which files we touched */
67 1.13 dholland int language = INCC;
68 1.1 cgd
69 1.17 dholland static char default_currentfilename[] = "????";
70 1.17 dholland char *currentfilename = default_currentfilename;
71 1.13 dholland char *processname;
72 1.1 cgd
73 1.16 dholland boolean query = false; /* query the operator if touch files */
74 1.16 dholland boolean terse = false; /* Terse output */
75 1.1 cgd
76 1.12 dholland static char im_on[] = _PATH_TTY; /* my tty name */
77 1.16 dholland static boolean notouch = false; /* don't touch ANY files */
78 1.12 dholland
79 1.17 dholland const char *suffixlist = ".*"; /* initially, can touch any file */
80 1.1 cgd
81 1.12 dholland static int errorsort(const void *, const void *);
82 1.12 dholland static void forkvi(int, char **);
83 1.16 dholland static void try(const char *, int, char **);
84 1.4 lukem
85 1.1 cgd /*
86 1.13 dholland * error [-I ignorename] [-n] [-q] [-t suffixlist] [-s] [-v] [infile]
87 1.13 dholland *
88 1.1 cgd * -T: terse output
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.1 cgd * -q: The user is to be queried before touching each
97 1.1 cgd * file; if not specified, all files with hard, non
98 1.1 cgd * ignorable errors are touched (assuming they can be).
99 1.1 cgd *
100 1.9 wiz * -t: touch only files ending with the list of suffixes, each
101 1.1 cgd * suffix preceded by a dot.
102 1.1 cgd * eg, -t .c.y.l
103 1.1 cgd * will touch only files ending with .c, .y or .l
104 1.1 cgd *
105 1.1 cgd * -s: print a summary of the error's categories.
106 1.1 cgd *
107 1.1 cgd * -v: after touching all files, overlay vi(1), ex(1) or ed(1)
108 1.1 cgd * on top of error, entered in the first file with
109 1.1 cgd * an error in it, with the appropriate editor
110 1.1 cgd * set up to use the "next" command to get the other
111 1.1 cgd * files containing errors.
112 1.1 cgd *
113 1.1 cgd * -p: (obsolete: for older versions of pi without bug
114 1.1 cgd * fix regarding printing out the name of the main file
115 1.1 cgd * with an error in it)
116 1.1 cgd * Take the following argument and use it as the name of
117 1.1 cgd * the pascal source file, suffix .p
118 1.1 cgd *
119 1.1 cgd * -E: show the errors in sorted order; intended for
120 1.1 cgd * debugging.
121 1.1 cgd *
122 1.1 cgd * -S: show the errors in unsorted order
123 1.1 cgd * (as they come from the error file)
124 1.1 cgd *
125 1.1 cgd * infile: The error messages come from this file.
126 1.1 cgd * Default: stdin
127 1.1 cgd */
128 1.4 lukem int
129 1.7 wiz main(int argc, char **argv)
130 1.1 cgd {
131 1.13 dholland char *cp;
132 1.13 dholland char *ignorename = 0;
133 1.13 dholland int ed_argc;
134 1.13 dholland char **ed_argv; /* return from touchfiles */
135 1.16 dholland boolean show_errors = false;
136 1.16 dholland boolean Show_Errors = false;
137 1.16 dholland boolean pr_summary = false;
138 1.16 dholland boolean edit_files = false;
139 1.1 cgd
140 1.1 cgd processname = argv[0];
141 1.1 cgd
142 1.1 cgd errorfile = stdin;
143 1.13 dholland if (argc > 1)
144 1.16 dholland for (; argc > 1 && argv[1][0] == '-'; argc--, argv++) {
145 1.13 dholland for (cp = argv[1] + 1; *cp; cp++)
146 1.13 dholland switch (*cp) {
147 1.13 dholland default:
148 1.13 dholland fprintf(stderr, "%s: -%c: Unknown flag\n",
149 1.13 dholland processname, *cp);
150 1.13 dholland break;
151 1.13 dholland
152 1.16 dholland case 'n': notouch = true; break;
153 1.16 dholland case 'q': query = true; break;
154 1.16 dholland case 'S': Show_Errors = true; break;
155 1.16 dholland case 's': pr_summary = true; break;
156 1.16 dholland case 'v': edit_files = true; break;
157 1.16 dholland case 'T': terse = true; break;
158 1.13 dholland case 't':
159 1.13 dholland *cp-- = 0; argv++; argc--;
160 1.13 dholland if (argc > 1) {
161 1.13 dholland suffixlist = argv[1];
162 1.13 dholland }
163 1.13 dholland break;
164 1.13 dholland case 'I': /*ignore file name*/
165 1.13 dholland *cp-- = 0;
166 1.13 dholland argv++;
167 1.13 dholland argc--;
168 1.13 dholland if (argc > 1)
169 1.13 dholland ignorename = argv[1];
170 1.13 dholland break;
171 1.13 dholland }
172 1.1 cgd }
173 1.1 cgd if (notouch)
174 1.1 cgd suffixlist = 0;
175 1.13 dholland if (argc > 1) {
176 1.13 dholland if (argc > 3) {
177 1.1 cgd fprintf(stderr, "%s: Only takes 0 or 1 arguments\n",
178 1.1 cgd processname);
179 1.1 cgd exit(3);
180 1.1 cgd }
181 1.13 dholland if ((errorfile = fopen(argv[1], "r")) == NULL) {
182 1.1 cgd fprintf(stderr, "%s: %s: No such file or directory for reading errors.\n",
183 1.1 cgd processname, argv[1]);
184 1.1 cgd exit(4);
185 1.1 cgd }
186 1.1 cgd }
187 1.13 dholland if ((queryfile = fopen(im_on, "r")) == NULL) {
188 1.13 dholland if (query) {
189 1.1 cgd fprintf(stderr,
190 1.1 cgd "%s: Can't open \"%s\" to query the user.\n",
191 1.1 cgd processname, im_on);
192 1.1 cgd exit(9);
193 1.1 cgd }
194 1.1 cgd }
195 1.1 cgd if (signal(SIGINT, onintr) == SIG_IGN)
196 1.1 cgd signal(SIGINT, SIG_IGN);
197 1.1 cgd if (signal(SIGTERM, onintr) == SIG_IGN)
198 1.1 cgd signal(SIGTERM, SIG_IGN);
199 1.1 cgd getignored(ignorename);
200 1.1 cgd eaterrors(&nerrors, &errors);
201 1.1 cgd if (Show_Errors)
202 1.16 dholland printerrors(true, nerrors, errors);
203 1.1 cgd qsort(errors, nerrors, sizeof(Eptr), errorsort);
204 1.1 cgd if (show_errors)
205 1.16 dholland printerrors(false, nerrors, errors);
206 1.1 cgd findfiles(nerrors, errors, &nfiles, &files);
207 1.1 cgd #define P(msg, arg) fprintf(stdout, msg, arg)
208 1.13 dholland if (pr_summary) {
209 1.1 cgd if (nunknown)
210 1.1 cgd P("%d Errors are unclassifiable.\n", nunknown);
211 1.1 cgd if (nignore)
212 1.1 cgd P("%d Errors are classifiable, but totally discarded.\n",nignore);
213 1.1 cgd if (nsyncerrors)
214 1.1 cgd P("%d Errors are synchronization errors.\n", nsyncerrors);
215 1.1 cgd if (nignore)
216 1.1 cgd P("%d Errors are discarded because they refer to sacrosinct files.\n", ndiscard);
217 1.1 cgd if (nnulled)
218 1.1 cgd P("%d Errors are nulled because they refer to specific functions.\n", nnulled);
219 1.1 cgd if (nnonspec)
220 1.1 cgd P("%d Errors are not specific to any file.\n", nnonspec);
221 1.1 cgd if (nthisfile)
222 1.1 cgd P("%d Errors are specific to a given file, but not to a line.\n", nthisfile);
223 1.1 cgd if (ntrue)
224 1.1 cgd P("%d Errors are true errors, and can be inserted into the files.\n", ntrue);
225 1.1 cgd }
226 1.1 cgd filenames(nfiles, files);
227 1.1 cgd fflush(stdout);
228 1.1 cgd if (touchfiles(nfiles, files, &ed_argc, &ed_argv) && edit_files)
229 1.1 cgd forkvi(ed_argc, ed_argv);
230 1.4 lukem return (0);
231 1.1 cgd }
232 1.1 cgd
233 1.12 dholland static void
234 1.7 wiz forkvi(int argc, char **argv)
235 1.1 cgd {
236 1.13 dholland if (query) {
237 1.13 dholland switch (inquire(terse
238 1.1 cgd ? "Edit? "
239 1.13 dholland : "Do you still want to edit the files you touched? ")) {
240 1.10 lukem case Q_error:
241 1.1 cgd case Q_NO:
242 1.1 cgd case Q_no:
243 1.1 cgd return;
244 1.1 cgd default:
245 1.1 cgd break;
246 1.1 cgd }
247 1.1 cgd }
248 1.1 cgd /*
249 1.1 cgd * ed_agument's first argument is
250 1.6 wiz * a vi/ex compatible search argument
251 1.16 dholland * to find the first occurrence of ###
252 1.1 cgd */
253 1.1 cgd try("vi", argc, argv);
254 1.1 cgd try("ex", argc, argv);
255 1.1 cgd try("ed", argc-1, argv+1);
256 1.1 cgd fprintf(stdout, "Can't find any editors.\n");
257 1.1 cgd }
258 1.1 cgd
259 1.12 dholland static void
260 1.16 dholland try(const char *name, int argc, char **argv)
261 1.1 cgd {
262 1.16 dholland argv[0] = __UNCONST(name);
263 1.1 cgd wordvprint(stdout, argc, argv);
264 1.1 cgd fprintf(stdout, "\n");
265 1.1 cgd fflush(stderr);
266 1.1 cgd fflush(stdout);
267 1.1 cgd sleep(2);
268 1.1 cgd if (freopen(im_on, "r", stdin) == NULL)
269 1.1 cgd return;
270 1.1 cgd if (freopen(im_on, "w", stdout) == NULL)
271 1.1 cgd return;
272 1.1 cgd execvp(name, argv);
273 1.1 cgd }
274 1.1 cgd
275 1.12 dholland static int
276 1.12 dholland errorsort(const void *x1, const void *x2)
277 1.1 cgd {
278 1.15 dholland const Eptr *epp1 = x1;
279 1.15 dholland const Eptr *epp2 = x2;
280 1.13 dholland Eptr ep1, ep2;
281 1.13 dholland int order;
282 1.13 dholland
283 1.1 cgd /*
284 1.13 dholland * Sort by:
285 1.13 dholland * 1) synchronization, non specific, discarded errors first;
286 1.13 dholland * 2) nulled and true errors last
287 1.13 dholland * a) grouped by similar file names
288 1.13 dholland * 1) grouped in ascending line number
289 1.1 cgd */
290 1.1 cgd ep1 = *epp1; ep2 = *epp2;
291 1.1 cgd if (ep1 == 0 || ep2 == 0)
292 1.13 dholland return (0);
293 1.13 dholland if ((NOTSORTABLE(ep1->error_e_class)) ^ (NOTSORTABLE(ep2->error_e_class))) {
294 1.13 dholland return (NOTSORTABLE(ep1->error_e_class) ? -1 : 1);
295 1.1 cgd }
296 1.1 cgd if (NOTSORTABLE(ep1->error_e_class)) /* then both are */
297 1.13 dholland return (ep1->error_no - ep2->error_no);
298 1.1 cgd order = strcmp(ep1->error_text[0], ep2->error_text[0]);
299 1.13 dholland if (order == 0) {
300 1.13 dholland return (ep1->error_line - ep2->error_line);
301 1.1 cgd }
302 1.13 dholland return (order);
303 1.1 cgd }
304