main.c revision 1.25 1 1.25 rillig /* $NetBSD: main.c,v 1.25 2023/08/26 14:50:53 rillig 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.25 rillig __RCSID("$NetBSD: main.c,v 1.25 2023/08/26 14:50:53 rillig 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.25 rillig bool *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.25 rillig bool query = false; /* query the operator if touch files */
75 1.25 rillig bool terse = false; /* Terse output */
76 1.1 cgd
77 1.12 dholland static char im_on[] = _PATH_TTY; /* my tty name */
78 1.25 rillig static bool 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.4 lukem int
88 1.7 wiz main(int argc, char **argv)
89 1.1 cgd {
90 1.18 christos int c;
91 1.13 dholland char *ignorename = 0;
92 1.13 dholland int ed_argc;
93 1.13 dholland char **ed_argv; /* return from touchfiles */
94 1.25 rillig bool show_errors = false;
95 1.25 rillig bool Show_Errors = false;
96 1.25 rillig bool pr_summary = false;
97 1.25 rillig bool edit_files = false;
98 1.1 cgd
99 1.18 christos setprogname(argv[0]);
100 1.1 cgd
101 1.1 cgd errorfile = stdin;
102 1.18 christos while ((c = getopt(argc, argv, "I:np:qSsTt:v")) != -1)
103 1.18 christos switch (c) {
104 1.18 christos case 'I': /*ignore file name*/
105 1.18 christos ignorename = optarg;
106 1.18 christos break;
107 1.18 christos case 'n':
108 1.18 christos notouch = true;
109 1.18 christos break;
110 1.18 christos case 'p':
111 1.18 christos filelevel = (size_t)strtol(optarg, NULL, 0);
112 1.18 christos break;
113 1.18 christos case 'q':
114 1.18 christos query = true;
115 1.18 christos break;
116 1.18 christos case 'S':
117 1.18 christos Show_Errors = true;
118 1.18 christos break;
119 1.18 christos case 's':
120 1.18 christos pr_summary = true;
121 1.18 christos break;
122 1.18 christos case 'T':
123 1.18 christos terse = true;
124 1.18 christos break;
125 1.18 christos case 't':
126 1.18 christos suffixlist = optarg;
127 1.18 christos break;
128 1.18 christos case 'v':
129 1.18 christos edit_files = true;
130 1.18 christos break;
131 1.18 christos default:
132 1.18 christos usage();
133 1.1 cgd }
134 1.18 christos
135 1.18 christos argv += optind;
136 1.18 christos argc -= optind;
137 1.20 christos
138 1.20 christos switch (argc) {
139 1.20 christos case 0:
140 1.20 christos break;
141 1.20 christos case 1:
142 1.20 christos if ((errorfile = fopen(argv[0], "r")) == NULL)
143 1.20 christos err(1, "Cannot open `%s' to read errors", argv[0]);
144 1.20 christos break;
145 1.20 christos default:
146 1.20 christos usage();
147 1.20 christos }
148 1.20 christos
149 1.1 cgd if (notouch)
150 1.1 cgd suffixlist = 0;
151 1.20 christos
152 1.20 christos
153 1.13 dholland if ((queryfile = fopen(im_on, "r")) == NULL) {
154 1.18 christos if (query)
155 1.18 christos err(1, "Cannot open `%s' to query the user", im_on);
156 1.1 cgd }
157 1.1 cgd if (signal(SIGINT, onintr) == SIG_IGN)
158 1.1 cgd signal(SIGINT, SIG_IGN);
159 1.1 cgd if (signal(SIGTERM, onintr) == SIG_IGN)
160 1.1 cgd signal(SIGTERM, SIG_IGN);
161 1.1 cgd getignored(ignorename);
162 1.1 cgd eaterrors(&nerrors, &errors);
163 1.1 cgd if (Show_Errors)
164 1.16 dholland printerrors(true, nerrors, errors);
165 1.1 cgd qsort(errors, nerrors, sizeof(Eptr), errorsort);
166 1.1 cgd if (show_errors)
167 1.16 dholland printerrors(false, nerrors, errors);
168 1.1 cgd findfiles(nerrors, errors, &nfiles, &files);
169 1.1 cgd #define P(msg, arg) fprintf(stdout, msg, arg)
170 1.13 dholland if (pr_summary) {
171 1.25 rillig if (nunknown > 0)
172 1.20 christos P("%d Errors are unclassifiable.\n", nunknown);
173 1.25 rillig if (nignore > 0)
174 1.20 christos P("%d Errors are classifiable, but totally "
175 1.20 christos "discarded.\n", nignore);
176 1.25 rillig if (nsyncerrors > 0)
177 1.20 christos P("%d Errors are synchronization errors.\n",
178 1.20 christos nsyncerrors);
179 1.25 rillig if (nignore > 0)
180 1.20 christos P("%d Errors are discarded because they refer to "
181 1.20 christos "sacrosinct files.\n", ndiscard);
182 1.25 rillig if (nnulled > 0)
183 1.20 christos P("%d Errors are nulled because they refer to specific "
184 1.20 christos "functions.\n", nnulled);
185 1.25 rillig if (nnonspec > 0)
186 1.20 christos P("%d Errors are not specific to any file.\n",
187 1.20 christos nnonspec);
188 1.25 rillig if (nthisfile > 0)
189 1.20 christos P("%d Errors are specific to a given file, but not "
190 1.20 christos "to a line.\n", nthisfile);
191 1.25 rillig if (ntrue > 0)
192 1.20 christos P("%d Errors are true errors, and can be inserted "
193 1.20 christos "into the files.\n", ntrue);
194 1.1 cgd }
195 1.1 cgd filenames(nfiles, files);
196 1.1 cgd fflush(stdout);
197 1.1 cgd if (touchfiles(nfiles, files, &ed_argc, &ed_argv) && edit_files)
198 1.1 cgd forkvi(ed_argc, ed_argv);
199 1.20 christos return 0;
200 1.1 cgd }
201 1.1 cgd
202 1.12 dholland static void
203 1.7 wiz forkvi(int argc, char **argv)
204 1.1 cgd {
205 1.13 dholland if (query) {
206 1.13 dholland switch (inquire(terse
207 1.1 cgd ? "Edit? "
208 1.13 dholland : "Do you still want to edit the files you touched? ")) {
209 1.10 lukem case Q_error:
210 1.1 cgd case Q_NO:
211 1.1 cgd case Q_no:
212 1.1 cgd return;
213 1.1 cgd default:
214 1.1 cgd break;
215 1.1 cgd }
216 1.1 cgd }
217 1.1 cgd /*
218 1.22 andvar * ed_argument's first argument is
219 1.6 wiz * a vi/ex compatible search argument
220 1.16 dholland * to find the first occurrence of ###
221 1.1 cgd */
222 1.1 cgd try("vi", argc, argv);
223 1.1 cgd try("ex", argc, argv);
224 1.1 cgd try("ed", argc-1, argv+1);
225 1.1 cgd fprintf(stdout, "Can't find any editors.\n");
226 1.1 cgd }
227 1.1 cgd
228 1.12 dholland static void
229 1.16 dholland try(const char *name, int argc, char **argv)
230 1.1 cgd {
231 1.16 dholland argv[0] = __UNCONST(name);
232 1.1 cgd wordvprint(stdout, argc, argv);
233 1.1 cgd fprintf(stdout, "\n");
234 1.1 cgd fflush(stderr);
235 1.1 cgd fflush(stdout);
236 1.1 cgd sleep(2);
237 1.1 cgd if (freopen(im_on, "r", stdin) == NULL)
238 1.1 cgd return;
239 1.1 cgd if (freopen(im_on, "w", stdout) == NULL)
240 1.1 cgd return;
241 1.1 cgd execvp(name, argv);
242 1.1 cgd }
243 1.1 cgd
244 1.12 dholland static int
245 1.12 dholland errorsort(const void *x1, const void *x2)
246 1.1 cgd {
247 1.15 dholland const Eptr *epp1 = x1;
248 1.15 dholland const Eptr *epp2 = x2;
249 1.13 dholland Eptr ep1, ep2;
250 1.13 dholland int order;
251 1.13 dholland
252 1.1 cgd /*
253 1.13 dholland * Sort by:
254 1.13 dholland * 1) synchronization, non specific, discarded errors first;
255 1.13 dholland * 2) nulled and true errors last
256 1.13 dholland * a) grouped by similar file names
257 1.13 dholland * 1) grouped in ascending line number
258 1.1 cgd */
259 1.1 cgd ep1 = *epp1; ep2 = *epp2;
260 1.1 cgd if (ep1 == 0 || ep2 == 0)
261 1.20 christos return 0;
262 1.20 christos if (NOTSORTABLE(ep1->error_e_class) ^ NOTSORTABLE(ep2->error_e_class)) {
263 1.20 christos return NOTSORTABLE(ep1->error_e_class) ? -1 : 1;
264 1.1 cgd }
265 1.1 cgd if (NOTSORTABLE(ep1->error_e_class)) /* then both are */
266 1.20 christos return ep1->error_no - ep2->error_no;
267 1.1 cgd order = strcmp(ep1->error_text[0], ep2->error_text[0]);
268 1.13 dholland if (order == 0) {
269 1.20 christos return ep1->error_line - ep2->error_line;
270 1.1 cgd }
271 1.20 christos return order;
272 1.1 cgd }
273 1.18 christos
274 1.18 christos static void
275 1.18 christos usage(void)
276 1.18 christos {
277 1.19 wiz fprintf(stderr, "Usage: %s [-nqSsTv] [-I ignorefile] "
278 1.19 wiz "[-p filelevel] [-t suffixlist] [name]\n", getprogname());
279 1.18 christos exit(1);
280 1.18 christos }
281