xref: /onnv-gate/usr/src/cmd/sgs/error/common/errormain.c (revision 291:0ac955735323)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22*291Smike_s 
23*291Smike_s /*
24*291Smike_s  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25*291Smike_s  * Use is subject to license terms.
26*291Smike_s  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <ctype.h>
320Sstevel@tonic-gate #include <signal.h>
33*291Smike_s #include <stdlib.h>
34*291Smike_s #include <unistd.h>
35*291Smike_s #include <string.h>
360Sstevel@tonic-gate #include "error.h"
370Sstevel@tonic-gate 
380Sstevel@tonic-gate int	nerrors = 0;
390Sstevel@tonic-gate Eptr	er_head;
40*291Smike_s Eptr	*errors;
410Sstevel@tonic-gate 
420Sstevel@tonic-gate int	nfiles = 0;
43*291Smike_s Eptr	**files;	/* array of pointers into errors */
440Sstevel@tonic-gate int	language = INCC;
450Sstevel@tonic-gate 
460Sstevel@tonic-gate char	*currentfilename = "????";
470Sstevel@tonic-gate char	*processname;
480Sstevel@tonic-gate char	im_on[] = "/dev/tty";	/* my tty name */
490Sstevel@tonic-gate 
500Sstevel@tonic-gate boolean	query = FALSE;		/* query the operator if touch files */
510Sstevel@tonic-gate boolean	notouch = FALSE;	/* don't touch ANY files */
520Sstevel@tonic-gate boolean	terse	= FALSE;	/* Terse output */
530Sstevel@tonic-gate 
540Sstevel@tonic-gate char	*suffixlist = ".*";	/* initially, can touch any file */
550Sstevel@tonic-gate 
56*291Smike_s static void try(char *name, int argc, char **argv);
57*291Smike_s static void forkvi(int argc, char **argv);
58*291Smike_s static int errorsort(const void *arg1, const void *arg2);
590Sstevel@tonic-gate 
60*291Smike_s 
610Sstevel@tonic-gate /*
620Sstevel@tonic-gate  *	error [-I ignorename] [-n] [-q] [-t suffixlist] [-s] [-v] [infile]
63*291Smike_s  *
640Sstevel@tonic-gate  *	-T:	terse output
650Sstevel@tonic-gate  *
660Sstevel@tonic-gate  *	-I:	the following name, `ignorename' contains a list of
670Sstevel@tonic-gate  *		function names that are not to be treated as hard errors.
680Sstevel@tonic-gate  *		Default: ~/.errorsrc
690Sstevel@tonic-gate  *
700Sstevel@tonic-gate  *	-n:	don't touch ANY files!
710Sstevel@tonic-gate  *
720Sstevel@tonic-gate  *	-q:	The user is to be queried before touching each
730Sstevel@tonic-gate  *		file; if not specified, all files with hard, non
740Sstevel@tonic-gate  *		ignorable errors are touched (assuming they can be).
750Sstevel@tonic-gate  *
760Sstevel@tonic-gate  *	-t:	touch only files ending with the list of suffices, each
770Sstevel@tonic-gate  *		suffix preceded by a dot.
780Sstevel@tonic-gate  *		eg, -t .c.y.l
790Sstevel@tonic-gate  *		will touch only files ending with .c, .y or .l
800Sstevel@tonic-gate  *
810Sstevel@tonic-gate  *	-s:	print a summary of the error's categories.
820Sstevel@tonic-gate  *
830Sstevel@tonic-gate  *	-v:	after touching all files, overlay vi(1), ex(1) or ed(1)
840Sstevel@tonic-gate  *		on top of error, entered in the first file with
850Sstevel@tonic-gate  *		an error in it, with the appropriate editor
860Sstevel@tonic-gate  *		set up to use the "next" command to get the other
870Sstevel@tonic-gate  *		files containing errors.
880Sstevel@tonic-gate  *
890Sstevel@tonic-gate  *	-p:	(obsolete: for older versions of pi without bug
900Sstevel@tonic-gate  *		fix regarding printing out the name of the main file
910Sstevel@tonic-gate  *		with an error in it)
920Sstevel@tonic-gate  *		Take the following argument and use it as the name of
930Sstevel@tonic-gate  *		the pascal source file, suffix .p
940Sstevel@tonic-gate  *
950Sstevel@tonic-gate  *	-E:	show the errors in sorted order; intended for
960Sstevel@tonic-gate  *		debugging.
970Sstevel@tonic-gate  *
980Sstevel@tonic-gate  *	-S:	show the errors in unsorted order
990Sstevel@tonic-gate  *		(as they come from the error file)
1000Sstevel@tonic-gate  *
1010Sstevel@tonic-gate  *	infile:	The error messages come from this file.
1020Sstevel@tonic-gate  *		Default: stdin
1030Sstevel@tonic-gate  */
104*291Smike_s int
main(int argc,char * argv[])105*291Smike_s main(int argc, char *argv[])
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate 	char	*cp;
1080Sstevel@tonic-gate 	char	*ignorename = 0;
1090Sstevel@tonic-gate 	int	ed_argc;
110*291Smike_s 	char	**ed_argv;		/* return from touchfiles */
1110Sstevel@tonic-gate 	boolean	show_errors = FALSE;
1120Sstevel@tonic-gate 	boolean	Show_Errors = FALSE;
1130Sstevel@tonic-gate 	boolean	pr_summary = FALSE;
1140Sstevel@tonic-gate 	boolean	edit_files = FALSE;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	processname = argv[0];
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	errorfile = stdin;
119*291Smike_s 	if (argc > 1) {
120*291Smike_s 		for (; (argc > 1) && (argv[1][0] == '-'); argc--, argv++) {
121*291Smike_s 			for (cp = argv[1] + 1; *cp; cp++) {
122*291Smike_s 				switch (*cp) {
123*291Smike_s 				default:
124*291Smike_s 					(void) fprintf(stderr,
125*291Smike_s 					    "%s: -%c: Unknown flag\n",
126*291Smike_s 					    processname, *cp);
127*291Smike_s 					break;
128*291Smike_s 				case 'n':
129*291Smike_s 					notouch = TRUE;
130*291Smike_s 					break;
131*291Smike_s 				case 'q':
132*291Smike_s 					query = TRUE;
133*291Smike_s 					break;
134*291Smike_s 				case 'S':
135*291Smike_s 					Show_Errors = TRUE;
136*291Smike_s 					break;
137*291Smike_s 				case 's':
138*291Smike_s 					pr_summary = TRUE;
139*291Smike_s 					break;
140*291Smike_s 				case 'v':
141*291Smike_s 					edit_files = TRUE;
142*291Smike_s 					break;
143*291Smike_s 				case 'T':
144*291Smike_s 					terse = TRUE;
145*291Smike_s 					break;
146*291Smike_s 				case 't':
147*291Smike_s 					*cp-- = 0;
148*291Smike_s 					argv++;
149*291Smike_s 					argc--;
150*291Smike_s 					if (argc > 1) {
151*291Smike_s 						suffixlist = argv[1];
152*291Smike_s 					}
153*291Smike_s 					break;
154*291Smike_s 				case 'I':	/* ignore file name */
155*291Smike_s 					*cp-- = 0;
156*291Smike_s 					argv++;
157*291Smike_s 					argc--;
158*291Smike_s 					if (argc > 1)
159*291Smike_s 						ignorename = argv[1];
160*291Smike_s 					break;
161*291Smike_s 				}
1620Sstevel@tonic-gate 			}
1630Sstevel@tonic-gate 		}
164*291Smike_s 	}
1650Sstevel@tonic-gate 	if (notouch)
1660Sstevel@tonic-gate 		suffixlist = 0;
167*291Smike_s 	if (argc > 1) {
168*291Smike_s 		if (argc > 3) {
169*291Smike_s 			(void) fprintf(stderr,
170*291Smike_s 			    "%s: Only takes 0 or 1 arguments\n",
171*291Smike_s 			    processname);
1720Sstevel@tonic-gate 			exit(3);
1730Sstevel@tonic-gate 		}
174*291Smike_s 		if ((errorfile = fopen(argv[1], "r")) == NULL) {
175*291Smike_s 			(void) fprintf(stderr,
176*291Smike_s 			    "%s: %s: No such file or directory for "
177*291Smike_s 			    "reading errors.\n", processname, argv[1]);
1780Sstevel@tonic-gate 			exit(4);
1790Sstevel@tonic-gate 		}
1800Sstevel@tonic-gate 	}
181*291Smike_s 	if ((queryfile = fopen(im_on, "r")) == NULL) {
182*291Smike_s 		if (query) {
183*291Smike_s 			(void) fprintf(stderr,
1840Sstevel@tonic-gate 				"%s: Can't open \"%s\" to query the user.\n",
1850Sstevel@tonic-gate 				processname, im_on);
1860Sstevel@tonic-gate 			exit(9);
1870Sstevel@tonic-gate 		}
1880Sstevel@tonic-gate 	}
1890Sstevel@tonic-gate 	if (signal(SIGINT, onintr) == SIG_IGN)
190*291Smike_s 		(void) signal(SIGINT, SIG_IGN);
1910Sstevel@tonic-gate 	if (signal(SIGTERM, onintr) == SIG_IGN)
192*291Smike_s 		(void) signal(SIGTERM, SIG_IGN);
1930Sstevel@tonic-gate 	getignored(ignorename);
1940Sstevel@tonic-gate 	eaterrors(&nerrors, &errors);
1950Sstevel@tonic-gate 	if (Show_Errors)
1960Sstevel@tonic-gate 		printerrors(TRUE, nerrors, errors);
197*291Smike_s 	qsort(errors, nerrors, sizeof (Eptr), errorsort);
1980Sstevel@tonic-gate 	if (show_errors)
1990Sstevel@tonic-gate 		printerrors(FALSE, nerrors, errors);
2000Sstevel@tonic-gate 	findfiles(nerrors, errors, &nfiles, &files);
201*291Smike_s 	if (pr_summary) {
202*291Smike_s 		if (nunknown)
203*291Smike_s 			(void) fprintf(stdout,
204*291Smike_s 			    "%d Errors are unclassifiable.\n",
205*291Smike_s 			    nunknown);
206*291Smike_s 		if (nignore)
207*291Smike_s 			(void) fprintf(stdout,
208*291Smike_s 			    "%d Errors are classifiable, but totally "
209*291Smike_s 			    "discarded.\n", nignore);
210*291Smike_s 		if (nsyncerrors)
211*291Smike_s 			(void) fprintf(stdout,
212*291Smike_s 			    "%d Errors are synchronization errors.\n",
213*291Smike_s 			    nsyncerrors);
214*291Smike_s 		if (nignore)
215*291Smike_s 			(void) fprintf(stdout,
216*291Smike_s 			    "%d Errors are discarded because they "
217*291Smike_s 			    "refer to sacrosanct files.\n", ndiscard);
218*291Smike_s 		if (nnulled)
219*291Smike_s 			(void) fprintf(stdout,
220*291Smike_s 			    "%d Errors are nulled because they refer "
221*291Smike_s 			    "to specific functions.\n", nnulled);
222*291Smike_s 		if (nnonspec)
223*291Smike_s 			(void) fprintf(stdout,
224*291Smike_s 			    "%d Errors are not specific to any file.\n",
225*291Smike_s 			    nnonspec);
226*291Smike_s 		if (nthisfile)
227*291Smike_s 			(void) fprintf(stdout,
228*291Smike_s 			    "%d Errors are specific to a given file, "
229*291Smike_s 			    "but not to a line.\n", nthisfile);
230*291Smike_s 		if (ntrue)
231*291Smike_s 			(void) fprintf(stdout,
232*291Smike_s 			    "%d Errors are true errors, and can be "
233*291Smike_s 			    "inserted into the files.\n", ntrue);
2340Sstevel@tonic-gate 	}
2350Sstevel@tonic-gate 	filenames(nfiles, files);
236*291Smike_s 	(void) fflush(stdout);
2370Sstevel@tonic-gate 	if (touchfiles(nfiles, files, &ed_argc, &ed_argv) && edit_files)
2380Sstevel@tonic-gate 		forkvi(ed_argc, ed_argv);
239*291Smike_s 	return (0);
2400Sstevel@tonic-gate }
2410Sstevel@tonic-gate 
242*291Smike_s static void
forkvi(int argc,char ** argv)243*291Smike_s forkvi(int argc, char **argv)
2440Sstevel@tonic-gate {
245*291Smike_s 	if (query) {
246*291Smike_s 		switch (inquire(terse
2470Sstevel@tonic-gate 		    ? "Edit? "
248*291Smike_s 		    : "Do you still want to edit the files you touched? ")) {
2490Sstevel@tonic-gate 		case Q_NO:
2500Sstevel@tonic-gate 		case Q_no:
2510Sstevel@tonic-gate 			return;
2520Sstevel@tonic-gate 		default:
2530Sstevel@tonic-gate 			break;
2540Sstevel@tonic-gate 		}
2550Sstevel@tonic-gate 	}
2560Sstevel@tonic-gate 	/*
2570Sstevel@tonic-gate 	 *	ed_agument's first argument is
2580Sstevel@tonic-gate 	 *	a vi/ex compatabile search argument
2590Sstevel@tonic-gate 	 *	to find the first occurance of ###
2600Sstevel@tonic-gate 	 */
2610Sstevel@tonic-gate 	try("vi", argc, argv);
2620Sstevel@tonic-gate 	try("ex", argc, argv);
2630Sstevel@tonic-gate 	try("ed", argc-1, argv+1);
264*291Smike_s 	(void) fprintf(stdout, "Can't find any editors.\n");
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate 
267*291Smike_s static void
try(char * name,int argc,char ** argv)268*291Smike_s try(char *name, int argc, char **argv)
2690Sstevel@tonic-gate {
2700Sstevel@tonic-gate 	argv[0] = name;
2710Sstevel@tonic-gate 	wordvprint(stdout, argc, argv);
272*291Smike_s 	(void) fprintf(stdout, "\n");
273*291Smike_s 	(void) fflush(stderr);
274*291Smike_s 	(void) fflush(stdout);
275*291Smike_s 	(void) sleep(2);
2760Sstevel@tonic-gate 	if (freopen(im_on, "r", stdin) == NULL)
2770Sstevel@tonic-gate 		return;
2780Sstevel@tonic-gate 	if (freopen(im_on, "w", stdout) == NULL)
2790Sstevel@tonic-gate 		return;
280*291Smike_s 	(void) execvp(name, argv);
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate 
283*291Smike_s static int
errorsort(const void * arg1,const void * arg2)284*291Smike_s errorsort(const void *arg1, const void *arg2)
2850Sstevel@tonic-gate {
286*291Smike_s 	Eptr	*epp1 = (Eptr *)arg1;
287*291Smike_s 	Eptr	*epp2 = (Eptr *)arg2;
288*291Smike_s 	Eptr	ep1, ep2;
289*291Smike_s 	int	order;
290*291Smike_s 
2910Sstevel@tonic-gate 	/*
2920Sstevel@tonic-gate 	 *	Sort by:
2930Sstevel@tonic-gate 	 *	1)	synchronization, non specific, discarded errors first;
2940Sstevel@tonic-gate 	 *	2)	nulled and true errors last
2950Sstevel@tonic-gate 	 *		a)	grouped by similar file names
2960Sstevel@tonic-gate 	 *			1)	grouped in ascending line number
2970Sstevel@tonic-gate 	 */
2980Sstevel@tonic-gate 	ep1 = *epp1; ep2 = *epp2;
2990Sstevel@tonic-gate 	if (ep1 == 0 || ep2 == 0)
300*291Smike_s 		return (0);
301*291Smike_s 	if ((NOTSORTABLE(ep1->error_e_class)) ^
302*291Smike_s 	    (NOTSORTABLE(ep2->error_e_class))) {
303*291Smike_s 		return (NOTSORTABLE(ep1->error_e_class) ? -1 : 1);
3040Sstevel@tonic-gate 	}
3050Sstevel@tonic-gate 	if (NOTSORTABLE(ep1->error_e_class))	/* then both are */
306*291Smike_s 		return (ep1->error_no - ep2->error_no);
3070Sstevel@tonic-gate 	order = strcmp(ep1->error_text[0], ep2->error_text[0]);
308*291Smike_s 	if (order == 0) {
309*291Smike_s 		return (ep1->error_line - ep2->error_line);
3100Sstevel@tonic-gate 	}
311*291Smike_s 	return (order);
3120Sstevel@tonic-gate }
313