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