added PDX constant
[unix-history] / usr / src / usr.bin / error / main.c
CommitLineData
58195f21 1static char *sccsid = "@(#)main.c 1.3 (Berkeley) %G%";
ad33082a
BJ
2#include <stdio.h>
3#include <ctype.h>
4#include <signal.h>
5#include "error.h"
6
7int nerrors = 0;
58195f21
RH
8Eptr er_head;
9Eptr *errors;
ad33082a
BJ
10
11int nfiles = 0;
58195f21 12Eptr **files; /* array of pointers into errors*/
ad33082a
BJ
13int language = INCC;
14
15char *currentfilename = "????";
16char *processname;
17char *im_on; /* my tty name */
18
19boolean query = FALSE; /* query the operator if touch files */
20boolean notouch = FALSE; /* don't touch ANY files */
21boolean piflag = FALSE; /* this is not pi */
58195f21 22boolean terse = FALSE; /* Terse output */
ad33082a
BJ
23
24char *suffixlist = ".*"; /* initially, can touch any file */
25
26int errorsort();
27int onintr();
28/*
29 * error [-I ignorename] [-n] [-q] [-t suffixlist] [-s] [-v] [infile]
30 *
58195f21
RH
31 * -T: terse output
32 *
ad33082a
BJ
33 * -I: the following name, `ignorename' contains a list of
34 * function names that are not to be treated as hard errors.
35 * Default: ~/.errorsrc
36 *
37 * -n: don't touch ANY files!
38 *
39 * -q: The user is to be queried before touching each
40 * file; if not specified, all files with hard, non
41 * ignorable errors are touched (assuming they can be).
42 *
43 * -t: touch only files ending with the list of suffices, each
44 * suffix preceded by a dot.
45 * eg, -t .c.y.l
46 * will touch only files ending with .c, .y or .l
47 *
48 * -s: print a summary of the error's categories.
49 *
50 * -v: after touching all files, overlay vi(1), ex(1) or ed(1)
51 * on top of error, entered in the first file with
52 * an error in it, with the appropriate editor
53 * set up to use the "next" command to get the other
54 * files containing errors.
55 *
56 * -p: (obsolete: for older versions of pi without bug
57 * fix regarding printing out the name of the main file
58 * with an error in it)
59 * Take the following argument and use it as the name of
60 * the pascal source file, suffix .p
61 *
62 * -E: show the errors in sorted order; intended for
63 * debugging.
64 *
65 * -S: show the errors in unsorted order
66 * (as they come from the error file)
67 *
68 * infile: The error messages come from this file.
69 * Default: stdin
70 */
71main(argc, argv)
72 int argc;
73 char *argv[];
74{
75 char *cp;
76 char *ignorename = 0;
77 int ed_argc;
78 char **ed_argv; /*return from touchfiles*/
79 boolean show_errors = FALSE;
80 boolean Show_Errors = FALSE;
81 boolean pr_summary = FALSE;
82 boolean edit_files = FALSE;
83
84 processname = argv[0];
85
86 errorfile = stdin;
58195f21
RH
87 if (argc > 1) for(; (argc > 1) && (argv[1][0] == '-'); argc--, argv++){
88 for (cp = argv[1] + 1; *cp; cp++) switch(*cp){
89 default:
90 fprintf(stderr, "%s: -%c: Unknown flag\n",
91 processname, *cp);
92 break;
93
94 case 'n': notouch = TRUE; break;
95 case 'q': query = TRUE; break;
96 case 'S': Show_Errors = TRUE; break;
97 case 's': pr_summary = TRUE; break;
98 case 'v': edit_files = TRUE; break;
99 case 'T': terse = TRUE; break;
ad33082a 100#ifndef ERNIE
58195f21
RH
101 case 'p':
102 *cp-- = 0; argv++; argc--;
103 if (argc > 1){
104 currentfilename = argv[1];
105 piflag = TRUE;
106 }
107 break;
ad33082a 108#endif
58195f21
RH
109 case 't':
110 *cp-- = 0; argv++; argc--;
111 if (argc > 1){
112 suffixlist = argv[1];
113 }
114 break;
115 case 'I': /*ignore file name*/
116 *cp-- = 0; argv++; argc--;
117 if (argc > 1)
118 ignorename = argv[1];
119 break;
120 }
121 }
ad33082a
BJ
122 if (notouch)
123 suffixlist = 0;
124 if (argc > 1){
125 if (argc > 3){
126 fprintf(stderr, "%s: Only takes 0 or 1 arguments\n",
127 processname);
128 exit(3);
129 }
130 if ( (errorfile = fopen(argv[1], "r")) == NULL){
131 fprintf(stderr, "%s: %s: No such file or directory for reading errors.\n",
132 processname, argv[1]);
133 exit(4);
134 }
135 }
8f46d9dc 136 im_on = "/dev/tty";
ad33082a
BJ
137 if ( (queryfile = fopen(im_on, "r")) == NULL){
138 fprintf(stderr,"%s: Can't open \"%s\" to query the user.\n",
139 processname, im_on);
140 exit(9);
141 }
142 if (signal(SIGINT, onintr) == SIG_IGN)
143 signal(SIGINT, SIG_IGN);
144 if (signal(SIGTERM, onintr) == SIG_IGN)
145 signal(SIGTERM, SIG_IGN);
146 getignored(ignorename);
147 eaterrors(&nerrors, &errors);
148 if (Show_Errors)
149 printerrors(TRUE, nerrors, errors);
58195f21 150 qsort(errors, nerrors, sizeof(Eptr), errorsort);
ad33082a
BJ
151 if (show_errors)
152 printerrors(FALSE, nerrors, errors);
153 findfiles(nerrors, errors, &nfiles, &files);
154#define P(msg, arg) fprintf(stdout, msg, arg)
155 if (pr_summary){
156 if (nunknown)
157 P("%d Errors are unclassifiable.\n", nunknown);
158 if (nignore)
159 P("%d Errors are classifiable, but totally discarded.\n",nignore);
160 if (nsyncerrors)
161 P("%d Errors are synchronization errors.\n", nsyncerrors);
162 if (nignore)
163 P("%d Errors are discarded because they refer to sacrosinct files.\n", ndiscard);
164 if (nnulled)
165 P("%d Errors are nulled because they refer to specific functions.\n", nnulled);
166 if (nnonspec)
167 P("%d Errors are not specific to any file.\n", nnonspec);
168 if (nthisfile)
169 P("%d Errors are specific to a given file, but not to a line.\n", nthisfile);
170 if (ntrue)
171 P("%d Errors are true errors, and can be inserted into the files.\n", ntrue);
172 }
173 filenames(nfiles, files);
174 fflush(stdout);
58195f21
RH
175 if (touchfiles(nfiles, files, &ed_argc, &ed_argv) && edit_files)
176 forkvi(ed_argc, ed_argv);
177}
178
179forkvi(argc, argv)
180 int argc;
181 char **argv;
182{
183 if (query){
184 switch(inquire(terse
185 ? "Edit? "
186 : "Do you still want to edit the files you touched? ")){
187 case Q_NO:
188 case Q_no:
189 return;
190 default:
191 break;
ad33082a
BJ
192 }
193 }
58195f21
RH
194 /*
195 * ed_agument's first argument is
196 * a vi/ex compatabile search argument
197 * to find the first occurance of ###
198 */
199 try("vi", argc, argv);
200 try("ex", argc, argv);
201 try("ed", argc-1, argv+1);
202 fprintf(stdout, "Can't find any editors.\n");
ad33082a
BJ
203}
204
205try(name, argc, argv)
206 char *name;
207 int argc;
208 char **argv;
209{
210 argv[0] = name;
211 wordvprint(stdout, argc, argv);
212 fprintf(stdout, "\n");
213 fflush(stderr);
214 fflush(stdout);
215 sleep(2);
216 if (freopen(im_on, "r", stdin) == NULL)
217 return;
218 if (freopen(im_on, "w", stdout) == NULL)
219 return;
220 execvp(name, argv);
221}
222
223int errorsort(epp1, epp2)
58195f21 224 Eptr *epp1, *epp2;
ad33082a 225{
58195f21
RH
226 reg Eptr ep1, ep2;
227 int order;
ad33082a
BJ
228 /*
229 * Sort by:
230 * 1) synchronization, non specific, discarded errors first;
231 * 2) nulled and true errors last
232 * a) grouped by similar file names
233 * 1) grouped in ascending line number
234 */
235 ep1 = *epp1; ep2 = *epp2;
236 if (ep1 == 0 || ep2 == 0)
237 return(0);
238 if ( (NOTSORTABLE(ep1->error_e_class)) ^ (NOTSORTABLE(ep2->error_e_class))){
239 return(NOTSORTABLE(ep1->error_e_class) ? -1 : 1);
240 }
241 if (NOTSORTABLE(ep1->error_e_class)) /* then both are */
242 return(ep1->error_no - ep2->error_no);
243 order = strcmp(ep1->error_text[0], ep2->error_text[0]);
244 if (order == 0){
245 return(ep1->error_line - ep2->error_line);
246 }
247 return(order);
248}