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