xref: /csrg-svn/usr.bin/error/main.c (revision 16564)
1*16564Sralph static	char *sccsid = "@(#)main.c	1.5 (Berkeley) 06/05/84";
21457Sroot #include <stdio.h>
31457Sroot #include <ctype.h>
41457Sroot #include <signal.h>
51457Sroot #include "error.h"
61457Sroot 
71457Sroot int	nerrors = 0;
85592Srrh Eptr	er_head;
95592Srrh Eptr	*errors;
101457Sroot 
111457Sroot int	nfiles = 0;
125592Srrh Eptr	**files;	/* array of pointers into errors*/
131457Sroot int	language = INCC;
141457Sroot 
151457Sroot char	*currentfilename = "????";
161457Sroot char	*processname;
17*16564Sralph char	im_on[] = "/dev/tty";	/* my tty name */
181457Sroot 
191457Sroot boolean	query = FALSE;		/* query the operator if touch files */
201457Sroot boolean	notouch = FALSE;	/* don't touch ANY files */
211457Sroot boolean	piflag	= FALSE;	/* this is not pi */
225592Srrh boolean	terse	= FALSE;	/* Terse output */
231457Sroot 
241457Sroot char	*suffixlist = ".*";	/* initially, can touch any file */
251457Sroot 
261457Sroot int	errorsort();
271457Sroot int	onintr();
281457Sroot /*
291457Sroot  *	error [-I ignorename] [-n] [-q] [-t suffixlist] [-s] [-v] [infile]
301457Sroot  *
315592Srrh  *	-T:	terse output
325592Srrh  *
331457Sroot  *	-I:	the following name, `ignorename' contains a list of
341457Sroot  *		function names that are not to be treated as hard errors.
351457Sroot  *		Default: ~/.errorsrc
361457Sroot  *
371457Sroot  *	-n:	don't touch ANY files!
381457Sroot  *
391457Sroot  *	-q:	The user is to be queried before touching each
401457Sroot  *		file; if not specified, all files with hard, non
411457Sroot  *		ignorable errors are touched (assuming they can be).
421457Sroot  *
431457Sroot  *	-t:	touch only files ending with the list of suffices, each
441457Sroot  *		suffix preceded by a dot.
451457Sroot  *		eg, -t .c.y.l
461457Sroot  *		will touch only files ending with .c, .y or .l
471457Sroot  *
481457Sroot  *	-s:	print a summary of the error's categories.
491457Sroot  *
501457Sroot  *	-v:	after touching all files, overlay vi(1), ex(1) or ed(1)
511457Sroot  *		on top of error, entered in the first file with
521457Sroot  *		an error in it, with the appropriate editor
531457Sroot  *		set up to use the "next" command to get the other
541457Sroot  *		files containing errors.
551457Sroot  *
561457Sroot  *	-p:	(obsolete: for older versions of pi without bug
571457Sroot  *		fix regarding printing out the name of the main file
581457Sroot  *		with an error in it)
591457Sroot  *		Take the following argument and use it as the name of
601457Sroot  *		the pascal source file, suffix .p
611457Sroot  *
621457Sroot  *	-E:	show the errors in sorted order; intended for
631457Sroot  *		debugging.
641457Sroot  *
651457Sroot  *	-S:	show the errors in unsorted order
661457Sroot  *		(as they come from the error file)
671457Sroot  *
681457Sroot  *	infile:	The error messages come from this file.
691457Sroot  *		Default: stdin
701457Sroot  */
711457Sroot main(argc, argv)
721457Sroot 	int	argc;
731457Sroot 	char	*argv[];
741457Sroot {
751457Sroot 	char	*cp;
761457Sroot 	char	*ignorename = 0;
771457Sroot 	int	ed_argc;
781457Sroot 	char	**ed_argv;		/*return from touchfiles*/
791457Sroot 	boolean	show_errors = FALSE;
801457Sroot 	boolean	Show_Errors = FALSE;
811457Sroot 	boolean	pr_summary = FALSE;
821457Sroot 	boolean	edit_files = FALSE;
831457Sroot 
841457Sroot 	processname = argv[0];
851457Sroot 
861457Sroot 	errorfile = stdin;
875592Srrh 	if (argc > 1) for(; (argc > 1) && (argv[1][0] == '-'); argc--, argv++){
885592Srrh 		for (cp = argv[1] + 1; *cp; cp++) switch(*cp){
895592Srrh 		default:
905592Srrh 			fprintf(stderr, "%s: -%c: Unknown flag\n",
915592Srrh 				processname, *cp);
925592Srrh 			break;
935592Srrh 
945592Srrh 		case 'n':	notouch = TRUE;	break;
955592Srrh 		case 'q':	query = TRUE;	break;
965592Srrh 		case 'S':	Show_Errors = TRUE;	break;
975592Srrh 		case 's':	pr_summary = TRUE;	break;
985592Srrh 		case 'v':	edit_files = TRUE;	break;
995592Srrh 		case 'T':	terse = TRUE;	break;
1005592Srrh 		case 't':
1015592Srrh 			*cp-- = 0; argv++; argc--;
1025592Srrh 			if (argc > 1){
1035592Srrh 				suffixlist = argv[1];
1045592Srrh 			}
1055592Srrh 			break;
1065592Srrh 		case 'I':	/*ignore file name*/
1075592Srrh 			*cp-- = 0; argv++; argc--;
1085592Srrh 			if (argc > 1)
1095592Srrh 				ignorename = argv[1];
1105592Srrh 			break;
1115592Srrh 		}
1125592Srrh 	}
1131457Sroot 	if (notouch)
1141457Sroot 		suffixlist = 0;
1151457Sroot 	if (argc > 1){
1161457Sroot 		if (argc > 3){
1171457Sroot 			fprintf(stderr, "%s: Only takes 0 or 1 arguments\n",
1181457Sroot 				processname);
1191457Sroot 			exit(3);
1201457Sroot 		}
1211457Sroot 		if ( (errorfile = fopen(argv[1], "r")) == NULL){
1221457Sroot 			fprintf(stderr, "%s: %s: No such file or directory for reading errors.\n",
1231457Sroot 				processname, argv[1]);
1241457Sroot 			exit(4);
1251457Sroot 		}
1261457Sroot 	}
1271457Sroot 	if ( (queryfile = fopen(im_on, "r")) == NULL){
128*16564Sralph 		if (query){
129*16564Sralph 			fprintf(stderr,
130*16564Sralph 				"%s: Can't open \"%s\" to query the user.\n",
131*16564Sralph 				processname, im_on);
132*16564Sralph 			exit(9);
133*16564Sralph 		}
1341457Sroot 	}
1351457Sroot 	if (signal(SIGINT, onintr) == SIG_IGN)
1361457Sroot 		signal(SIGINT, SIG_IGN);
1371457Sroot 	if (signal(SIGTERM, onintr) == SIG_IGN)
1381457Sroot 		signal(SIGTERM, SIG_IGN);
1391457Sroot 	getignored(ignorename);
1401457Sroot 	eaterrors(&nerrors, &errors);
1411457Sroot 	if (Show_Errors)
1421457Sroot 		printerrors(TRUE, nerrors, errors);
1435592Srrh 	qsort(errors, nerrors, sizeof(Eptr), errorsort);
1441457Sroot 	if (show_errors)
1451457Sroot 		printerrors(FALSE, nerrors, errors);
1461457Sroot 	findfiles(nerrors, errors, &nfiles, &files);
1471457Sroot #define P(msg, arg) fprintf(stdout, msg, arg)
1481457Sroot 	if (pr_summary){
1491457Sroot 	    if (nunknown)
1501457Sroot 	      P("%d Errors are unclassifiable.\n", nunknown);
1511457Sroot 	    if (nignore)
1521457Sroot 	      P("%d Errors are classifiable, but totally discarded.\n",nignore);
1531457Sroot 	    if (nsyncerrors)
1541457Sroot 	      P("%d Errors are synchronization errors.\n", nsyncerrors);
1551457Sroot 	    if (nignore)
1561457Sroot 	      P("%d Errors are discarded because they refer to sacrosinct files.\n", ndiscard);
1571457Sroot 	    if (nnulled)
1581457Sroot 	      P("%d Errors are nulled because they refer to specific functions.\n", nnulled);
1591457Sroot 	    if (nnonspec)
1601457Sroot 	      P("%d Errors are not specific to any file.\n", nnonspec);
1611457Sroot 	    if (nthisfile)
1621457Sroot 	      P("%d Errors are specific to a given file, but not to a line.\n", nthisfile);
1631457Sroot 	    if (ntrue)
1641457Sroot 	      P("%d Errors are true errors, and can be inserted into the files.\n", ntrue);
1651457Sroot 	}
1661457Sroot 	filenames(nfiles, files);
1671457Sroot 	fflush(stdout);
1685592Srrh 	if (touchfiles(nfiles, files, &ed_argc, &ed_argv) && edit_files)
1695592Srrh 		forkvi(ed_argc, ed_argv);
1705592Srrh }
1715592Srrh 
1725592Srrh forkvi(argc, argv)
1735592Srrh 	int	argc;
1745592Srrh 	char	**argv;
1755592Srrh {
1765592Srrh 	if (query){
1775592Srrh 		switch(inquire(terse
1785592Srrh 		    ? "Edit? "
1795592Srrh 		    : "Do you still want to edit the files you touched? ")){
1805592Srrh 		case Q_NO:
1815592Srrh 		case Q_no:
1825592Srrh 			return;
1835592Srrh 		default:
1845592Srrh 			break;
1851457Sroot 		}
1861457Sroot 	}
1875592Srrh 	/*
1885592Srrh 	 *	ed_agument's first argument is
1895592Srrh 	 *	a vi/ex compatabile search argument
1905592Srrh 	 *	to find the first occurance of ###
1915592Srrh 	 */
1925592Srrh 	try("vi", argc, argv);
1935592Srrh 	try("ex", argc, argv);
1945592Srrh 	try("ed", argc-1, argv+1);
1955592Srrh 	fprintf(stdout, "Can't find any editors.\n");
1961457Sroot }
1971457Sroot 
1981457Sroot try(name, argc, argv)
1991457Sroot 	char	*name;
2001457Sroot 	int	argc;
2011457Sroot 	char	**argv;
2021457Sroot {
2031457Sroot 	argv[0] = name;
2041457Sroot 	wordvprint(stdout, argc, argv);
2051457Sroot 	fprintf(stdout, "\n");
2061457Sroot 	fflush(stderr);
2071457Sroot 	fflush(stdout);
2081457Sroot 	sleep(2);
2091457Sroot 	if (freopen(im_on, "r", stdin) == NULL)
2101457Sroot 		return;
2111457Sroot 	if (freopen(im_on, "w", stdout) == NULL)
2121457Sroot 		return;
2131457Sroot 	execvp(name, argv);
2141457Sroot }
2151457Sroot 
2161457Sroot int errorsort(epp1, epp2)
2175592Srrh 		Eptr	*epp1, *epp2;
2181457Sroot {
2195592Srrh 	reg	Eptr	ep1, ep2;
2205592Srrh 		int	order;
2211457Sroot 	/*
2221457Sroot 	 *	Sort by:
2231457Sroot 	 *	1)	synchronization, non specific, discarded errors first;
2241457Sroot 	 *	2)	nulled and true errors last
2251457Sroot 	 *		a)	grouped by similar file names
2261457Sroot 	 *			1)	grouped in ascending line number
2271457Sroot 	 */
2281457Sroot 	ep1 = *epp1; ep2 = *epp2;
2291457Sroot 	if (ep1 == 0 || ep2 == 0)
2301457Sroot 		return(0);
2311457Sroot 	if ( (NOTSORTABLE(ep1->error_e_class)) ^ (NOTSORTABLE(ep2->error_e_class))){
2321457Sroot 		return(NOTSORTABLE(ep1->error_e_class) ? -1 : 1);
2331457Sroot 	}
2341457Sroot 	if (NOTSORTABLE(ep1->error_e_class))	/* then both are */
2351457Sroot 		return(ep1->error_no - ep2->error_no);
2361457Sroot 	order = strcmp(ep1->error_text[0], ep2->error_text[0]);
2371457Sroot 	if (order == 0){
2381457Sroot 		return(ep1->error_line - ep2->error_line);
2391457Sroot 	}
2401457Sroot 	return(order);
2411457Sroot }
242