xref: /csrg-svn/usr.bin/error/main.c (revision 42683)
121636Sdist /*
221636Sdist  * Copyright (c) 1980 Regents of the University of California.
334664Sbostic  * All rights reserved.
434664Sbostic  *
5*42683Sbostic  * %sccs.include.redist.c%
621636Sdist  */
721636Sdist 
821636Sdist #ifndef lint
921636Sdist char copyright[] =
1021636Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\
1121636Sdist  All rights reserved.\n";
1234664Sbostic #endif /* not lint */
1321636Sdist 
1434664Sbostic #ifndef lint
15*42683Sbostic static char sccsid[] = "@(#)main.c	5.5 (Berkeley) 06/01/90";
1634664Sbostic #endif /* not lint */
1734664Sbostic 
181457Sroot #include <stdio.h>
191457Sroot #include <ctype.h>
201457Sroot #include <signal.h>
211457Sroot #include "error.h"
2237791Sbostic #include "pathnames.h"
231457Sroot 
241457Sroot int	nerrors = 0;
255592Srrh Eptr	er_head;
2637791Sbostic Eptr	*errors;
271457Sroot 
281457Sroot int	nfiles = 0;
295592Srrh Eptr	**files;	/* array of pointers into errors*/
301457Sroot int	language = INCC;
311457Sroot 
321457Sroot char	*currentfilename = "????";
331457Sroot char	*processname;
3437791Sbostic char	im_on[] = _PATH_TTY;	/* my tty name */
351457Sroot 
361457Sroot boolean	query = FALSE;		/* query the operator if touch files */
371457Sroot boolean	notouch = FALSE;	/* don't touch ANY files */
381457Sroot boolean	piflag	= FALSE;	/* this is not pi */
395592Srrh boolean	terse	= FALSE;	/* Terse output */
401457Sroot 
411457Sroot char	*suffixlist = ".*";	/* initially, can touch any file */
421457Sroot 
431457Sroot int	errorsort();
441457Sroot int	onintr();
451457Sroot /*
461457Sroot  *	error [-I ignorename] [-n] [-q] [-t suffixlist] [-s] [-v] [infile]
471457Sroot  *
485592Srrh  *	-T:	terse output
495592Srrh  *
501457Sroot  *	-I:	the following name, `ignorename' contains a list of
511457Sroot  *		function names that are not to be treated as hard errors.
521457Sroot  *		Default: ~/.errorsrc
531457Sroot  *
541457Sroot  *	-n:	don't touch ANY files!
551457Sroot  *
561457Sroot  *	-q:	The user is to be queried before touching each
571457Sroot  *		file; if not specified, all files with hard, non
581457Sroot  *		ignorable errors are touched (assuming they can be).
591457Sroot  *
601457Sroot  *	-t:	touch only files ending with the list of suffices, each
611457Sroot  *		suffix preceded by a dot.
621457Sroot  *		eg, -t .c.y.l
631457Sroot  *		will touch only files ending with .c, .y or .l
641457Sroot  *
651457Sroot  *	-s:	print a summary of the error's categories.
661457Sroot  *
671457Sroot  *	-v:	after touching all files, overlay vi(1), ex(1) or ed(1)
681457Sroot  *		on top of error, entered in the first file with
691457Sroot  *		an error in it, with the appropriate editor
701457Sroot  *		set up to use the "next" command to get the other
711457Sroot  *		files containing errors.
721457Sroot  *
731457Sroot  *	-p:	(obsolete: for older versions of pi without bug
741457Sroot  *		fix regarding printing out the name of the main file
751457Sroot  *		with an error in it)
761457Sroot  *		Take the following argument and use it as the name of
771457Sroot  *		the pascal source file, suffix .p
781457Sroot  *
791457Sroot  *	-E:	show the errors in sorted order; intended for
801457Sroot  *		debugging.
811457Sroot  *
821457Sroot  *	-S:	show the errors in unsorted order
831457Sroot  *		(as they come from the error file)
841457Sroot  *
851457Sroot  *	infile:	The error messages come from this file.
861457Sroot  *		Default: stdin
871457Sroot  */
881457Sroot main(argc, argv)
891457Sroot 	int	argc;
901457Sroot 	char	*argv[];
911457Sroot {
921457Sroot 	char	*cp;
931457Sroot 	char	*ignorename = 0;
941457Sroot 	int	ed_argc;
951457Sroot 	char	**ed_argv;		/*return from touchfiles*/
961457Sroot 	boolean	show_errors = FALSE;
971457Sroot 	boolean	Show_Errors = FALSE;
981457Sroot 	boolean	pr_summary = FALSE;
991457Sroot 	boolean	edit_files = FALSE;
1001457Sroot 
1011457Sroot 	processname = argv[0];
1021457Sroot 
1031457Sroot 	errorfile = stdin;
1045592Srrh 	if (argc > 1) for(; (argc > 1) && (argv[1][0] == '-'); argc--, argv++){
1055592Srrh 		for (cp = argv[1] + 1; *cp; cp++) switch(*cp){
1065592Srrh 		default:
1075592Srrh 			fprintf(stderr, "%s: -%c: Unknown flag\n",
1085592Srrh 				processname, *cp);
1095592Srrh 			break;
1105592Srrh 
1115592Srrh 		case 'n':	notouch = TRUE;	break;
1125592Srrh 		case 'q':	query = TRUE;	break;
1135592Srrh 		case 'S':	Show_Errors = TRUE;	break;
1145592Srrh 		case 's':	pr_summary = TRUE;	break;
1155592Srrh 		case 'v':	edit_files = TRUE;	break;
1165592Srrh 		case 'T':	terse = TRUE;	break;
1175592Srrh 		case 't':
1185592Srrh 			*cp-- = 0; argv++; argc--;
1195592Srrh 			if (argc > 1){
1205592Srrh 				suffixlist = argv[1];
1215592Srrh 			}
1225592Srrh 			break;
1235592Srrh 		case 'I':	/*ignore file name*/
1245592Srrh 			*cp-- = 0; argv++; argc--;
1255592Srrh 			if (argc > 1)
1265592Srrh 				ignorename = argv[1];
1275592Srrh 			break;
1285592Srrh 		}
1295592Srrh 	}
1301457Sroot 	if (notouch)
1311457Sroot 		suffixlist = 0;
1321457Sroot 	if (argc > 1){
1331457Sroot 		if (argc > 3){
1341457Sroot 			fprintf(stderr, "%s: Only takes 0 or 1 arguments\n",
1351457Sroot 				processname);
1361457Sroot 			exit(3);
1371457Sroot 		}
1381457Sroot 		if ( (errorfile = fopen(argv[1], "r")) == NULL){
1391457Sroot 			fprintf(stderr, "%s: %s: No such file or directory for reading errors.\n",
1401457Sroot 				processname, argv[1]);
1411457Sroot 			exit(4);
1421457Sroot 		}
1431457Sroot 	}
1441457Sroot 	if ( (queryfile = fopen(im_on, "r")) == NULL){
14516564Sralph 		if (query){
14616564Sralph 			fprintf(stderr,
14716564Sralph 				"%s: Can't open \"%s\" to query the user.\n",
14816564Sralph 				processname, im_on);
14916564Sralph 			exit(9);
15016564Sralph 		}
1511457Sroot 	}
1521457Sroot 	if (signal(SIGINT, onintr) == SIG_IGN)
1531457Sroot 		signal(SIGINT, SIG_IGN);
1541457Sroot 	if (signal(SIGTERM, onintr) == SIG_IGN)
1551457Sroot 		signal(SIGTERM, SIG_IGN);
1561457Sroot 	getignored(ignorename);
1571457Sroot 	eaterrors(&nerrors, &errors);
1581457Sroot 	if (Show_Errors)
1591457Sroot 		printerrors(TRUE, nerrors, errors);
1605592Srrh 	qsort(errors, nerrors, sizeof(Eptr), errorsort);
1611457Sroot 	if (show_errors)
1621457Sroot 		printerrors(FALSE, nerrors, errors);
1631457Sroot 	findfiles(nerrors, errors, &nfiles, &files);
1641457Sroot #define P(msg, arg) fprintf(stdout, msg, arg)
1651457Sroot 	if (pr_summary){
1661457Sroot 	    if (nunknown)
1671457Sroot 	      P("%d Errors are unclassifiable.\n", nunknown);
1681457Sroot 	    if (nignore)
1691457Sroot 	      P("%d Errors are classifiable, but totally discarded.\n",nignore);
1701457Sroot 	    if (nsyncerrors)
1711457Sroot 	      P("%d Errors are synchronization errors.\n", nsyncerrors);
1721457Sroot 	    if (nignore)
1731457Sroot 	      P("%d Errors are discarded because they refer to sacrosinct files.\n", ndiscard);
1741457Sroot 	    if (nnulled)
1751457Sroot 	      P("%d Errors are nulled because they refer to specific functions.\n", nnulled);
1761457Sroot 	    if (nnonspec)
1771457Sroot 	      P("%d Errors are not specific to any file.\n", nnonspec);
1781457Sroot 	    if (nthisfile)
1791457Sroot 	      P("%d Errors are specific to a given file, but not to a line.\n", nthisfile);
1801457Sroot 	    if (ntrue)
1811457Sroot 	      P("%d Errors are true errors, and can be inserted into the files.\n", ntrue);
1821457Sroot 	}
1831457Sroot 	filenames(nfiles, files);
1841457Sroot 	fflush(stdout);
1855592Srrh 	if (touchfiles(nfiles, files, &ed_argc, &ed_argv) && edit_files)
1865592Srrh 		forkvi(ed_argc, ed_argv);
1875592Srrh }
1885592Srrh 
1895592Srrh forkvi(argc, argv)
1905592Srrh 	int	argc;
1915592Srrh 	char	**argv;
1925592Srrh {
1935592Srrh 	if (query){
1945592Srrh 		switch(inquire(terse
1955592Srrh 		    ? "Edit? "
1965592Srrh 		    : "Do you still want to edit the files you touched? ")){
1975592Srrh 		case Q_NO:
1985592Srrh 		case Q_no:
1995592Srrh 			return;
2005592Srrh 		default:
2015592Srrh 			break;
2021457Sroot 		}
2031457Sroot 	}
2045592Srrh 	/*
2055592Srrh 	 *	ed_agument's first argument is
2065592Srrh 	 *	a vi/ex compatabile search argument
2075592Srrh 	 *	to find the first occurance of ###
2085592Srrh 	 */
2095592Srrh 	try("vi", argc, argv);
2105592Srrh 	try("ex", argc, argv);
2115592Srrh 	try("ed", argc-1, argv+1);
2125592Srrh 	fprintf(stdout, "Can't find any editors.\n");
2131457Sroot }
2141457Sroot 
2151457Sroot try(name, argc, argv)
2161457Sroot 	char	*name;
2171457Sroot 	int	argc;
2181457Sroot 	char	**argv;
2191457Sroot {
2201457Sroot 	argv[0] = name;
2211457Sroot 	wordvprint(stdout, argc, argv);
2221457Sroot 	fprintf(stdout, "\n");
2231457Sroot 	fflush(stderr);
2241457Sroot 	fflush(stdout);
2251457Sroot 	sleep(2);
2261457Sroot 	if (freopen(im_on, "r", stdin) == NULL)
2271457Sroot 		return;
2281457Sroot 	if (freopen(im_on, "w", stdout) == NULL)
2291457Sroot 		return;
2301457Sroot 	execvp(name, argv);
2311457Sroot }
2321457Sroot 
2331457Sroot int errorsort(epp1, epp2)
2345592Srrh 		Eptr	*epp1, *epp2;
2351457Sroot {
2365592Srrh 	reg	Eptr	ep1, ep2;
2375592Srrh 		int	order;
2381457Sroot 	/*
2391457Sroot 	 *	Sort by:
2401457Sroot 	 *	1)	synchronization, non specific, discarded errors first;
2411457Sroot 	 *	2)	nulled and true errors last
2421457Sroot 	 *		a)	grouped by similar file names
2431457Sroot 	 *			1)	grouped in ascending line number
2441457Sroot 	 */
2451457Sroot 	ep1 = *epp1; ep2 = *epp2;
2461457Sroot 	if (ep1 == 0 || ep2 == 0)
2471457Sroot 		return(0);
2481457Sroot 	if ( (NOTSORTABLE(ep1->error_e_class)) ^ (NOTSORTABLE(ep2->error_e_class))){
2491457Sroot 		return(NOTSORTABLE(ep1->error_e_class) ? -1 : 1);
2501457Sroot 	}
2511457Sroot 	if (NOTSORTABLE(ep1->error_e_class))	/* then both are */
2521457Sroot 		return(ep1->error_no - ep2->error_no);
2531457Sroot 	order = strcmp(ep1->error_text[0], ep2->error_text[0]);
2541457Sroot 	if (order == 0){
2551457Sroot 		return(ep1->error_line - ep2->error_line);
2561457Sroot 	}
2571457Sroot 	return(order);
2581457Sroot }
259