xref: /csrg-svn/usr.bin/error/main.c (revision 37791)
121636Sdist /*
221636Sdist  * Copyright (c) 1980 Regents of the University of California.
334664Sbostic  * All rights reserved.
434664Sbostic  *
534664Sbostic  * Redistribution and use in source and binary forms are permitted
634874Sbostic  * provided that the above copyright notice and this paragraph are
734874Sbostic  * duplicated in all such forms and that any documentation,
834874Sbostic  * advertising materials, and other materials related to such
934874Sbostic  * distribution and use acknowledge that the software was developed
1034874Sbostic  * by the University of California, Berkeley.  The name of the
1134874Sbostic  * University may not be used to endorse or promote products derived
1234874Sbostic  * from this software without specific prior written permission.
1334874Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434874Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534874Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621636Sdist  */
1721636Sdist 
1821636Sdist #ifndef lint
1921636Sdist char copyright[] =
2021636Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\
2121636Sdist  All rights reserved.\n";
2234664Sbostic #endif /* not lint */
2321636Sdist 
2434664Sbostic #ifndef lint
25*37791Sbostic static char sccsid[] = "@(#)main.c	5.4 (Berkeley) 05/10/89";
2634664Sbostic #endif /* not lint */
2734664Sbostic 
281457Sroot #include <stdio.h>
291457Sroot #include <ctype.h>
301457Sroot #include <signal.h>
311457Sroot #include "error.h"
32*37791Sbostic #include "pathnames.h"
331457Sroot 
341457Sroot int	nerrors = 0;
355592Srrh Eptr	er_head;
36*37791Sbostic Eptr	*errors;
371457Sroot 
381457Sroot int	nfiles = 0;
395592Srrh Eptr	**files;	/* array of pointers into errors*/
401457Sroot int	language = INCC;
411457Sroot 
421457Sroot char	*currentfilename = "????";
431457Sroot char	*processname;
44*37791Sbostic char	im_on[] = _PATH_TTY;	/* my tty name */
451457Sroot 
461457Sroot boolean	query = FALSE;		/* query the operator if touch files */
471457Sroot boolean	notouch = FALSE;	/* don't touch ANY files */
481457Sroot boolean	piflag	= FALSE;	/* this is not pi */
495592Srrh boolean	terse	= FALSE;	/* Terse output */
501457Sroot 
511457Sroot char	*suffixlist = ".*";	/* initially, can touch any file */
521457Sroot 
531457Sroot int	errorsort();
541457Sroot int	onintr();
551457Sroot /*
561457Sroot  *	error [-I ignorename] [-n] [-q] [-t suffixlist] [-s] [-v] [infile]
571457Sroot  *
585592Srrh  *	-T:	terse output
595592Srrh  *
601457Sroot  *	-I:	the following name, `ignorename' contains a list of
611457Sroot  *		function names that are not to be treated as hard errors.
621457Sroot  *		Default: ~/.errorsrc
631457Sroot  *
641457Sroot  *	-n:	don't touch ANY files!
651457Sroot  *
661457Sroot  *	-q:	The user is to be queried before touching each
671457Sroot  *		file; if not specified, all files with hard, non
681457Sroot  *		ignorable errors are touched (assuming they can be).
691457Sroot  *
701457Sroot  *	-t:	touch only files ending with the list of suffices, each
711457Sroot  *		suffix preceded by a dot.
721457Sroot  *		eg, -t .c.y.l
731457Sroot  *		will touch only files ending with .c, .y or .l
741457Sroot  *
751457Sroot  *	-s:	print a summary of the error's categories.
761457Sroot  *
771457Sroot  *	-v:	after touching all files, overlay vi(1), ex(1) or ed(1)
781457Sroot  *		on top of error, entered in the first file with
791457Sroot  *		an error in it, with the appropriate editor
801457Sroot  *		set up to use the "next" command to get the other
811457Sroot  *		files containing errors.
821457Sroot  *
831457Sroot  *	-p:	(obsolete: for older versions of pi without bug
841457Sroot  *		fix regarding printing out the name of the main file
851457Sroot  *		with an error in it)
861457Sroot  *		Take the following argument and use it as the name of
871457Sroot  *		the pascal source file, suffix .p
881457Sroot  *
891457Sroot  *	-E:	show the errors in sorted order; intended for
901457Sroot  *		debugging.
911457Sroot  *
921457Sroot  *	-S:	show the errors in unsorted order
931457Sroot  *		(as they come from the error file)
941457Sroot  *
951457Sroot  *	infile:	The error messages come from this file.
961457Sroot  *		Default: stdin
971457Sroot  */
981457Sroot main(argc, argv)
991457Sroot 	int	argc;
1001457Sroot 	char	*argv[];
1011457Sroot {
1021457Sroot 	char	*cp;
1031457Sroot 	char	*ignorename = 0;
1041457Sroot 	int	ed_argc;
1051457Sroot 	char	**ed_argv;		/*return from touchfiles*/
1061457Sroot 	boolean	show_errors = FALSE;
1071457Sroot 	boolean	Show_Errors = FALSE;
1081457Sroot 	boolean	pr_summary = FALSE;
1091457Sroot 	boolean	edit_files = FALSE;
1101457Sroot 
1111457Sroot 	processname = argv[0];
1121457Sroot 
1131457Sroot 	errorfile = stdin;
1145592Srrh 	if (argc > 1) for(; (argc > 1) && (argv[1][0] == '-'); argc--, argv++){
1155592Srrh 		for (cp = argv[1] + 1; *cp; cp++) switch(*cp){
1165592Srrh 		default:
1175592Srrh 			fprintf(stderr, "%s: -%c: Unknown flag\n",
1185592Srrh 				processname, *cp);
1195592Srrh 			break;
1205592Srrh 
1215592Srrh 		case 'n':	notouch = TRUE;	break;
1225592Srrh 		case 'q':	query = TRUE;	break;
1235592Srrh 		case 'S':	Show_Errors = TRUE;	break;
1245592Srrh 		case 's':	pr_summary = TRUE;	break;
1255592Srrh 		case 'v':	edit_files = TRUE;	break;
1265592Srrh 		case 'T':	terse = TRUE;	break;
1275592Srrh 		case 't':
1285592Srrh 			*cp-- = 0; argv++; argc--;
1295592Srrh 			if (argc > 1){
1305592Srrh 				suffixlist = argv[1];
1315592Srrh 			}
1325592Srrh 			break;
1335592Srrh 		case 'I':	/*ignore file name*/
1345592Srrh 			*cp-- = 0; argv++; argc--;
1355592Srrh 			if (argc > 1)
1365592Srrh 				ignorename = argv[1];
1375592Srrh 			break;
1385592Srrh 		}
1395592Srrh 	}
1401457Sroot 	if (notouch)
1411457Sroot 		suffixlist = 0;
1421457Sroot 	if (argc > 1){
1431457Sroot 		if (argc > 3){
1441457Sroot 			fprintf(stderr, "%s: Only takes 0 or 1 arguments\n",
1451457Sroot 				processname);
1461457Sroot 			exit(3);
1471457Sroot 		}
1481457Sroot 		if ( (errorfile = fopen(argv[1], "r")) == NULL){
1491457Sroot 			fprintf(stderr, "%s: %s: No such file or directory for reading errors.\n",
1501457Sroot 				processname, argv[1]);
1511457Sroot 			exit(4);
1521457Sroot 		}
1531457Sroot 	}
1541457Sroot 	if ( (queryfile = fopen(im_on, "r")) == NULL){
15516564Sralph 		if (query){
15616564Sralph 			fprintf(stderr,
15716564Sralph 				"%s: Can't open \"%s\" to query the user.\n",
15816564Sralph 				processname, im_on);
15916564Sralph 			exit(9);
16016564Sralph 		}
1611457Sroot 	}
1621457Sroot 	if (signal(SIGINT, onintr) == SIG_IGN)
1631457Sroot 		signal(SIGINT, SIG_IGN);
1641457Sroot 	if (signal(SIGTERM, onintr) == SIG_IGN)
1651457Sroot 		signal(SIGTERM, SIG_IGN);
1661457Sroot 	getignored(ignorename);
1671457Sroot 	eaterrors(&nerrors, &errors);
1681457Sroot 	if (Show_Errors)
1691457Sroot 		printerrors(TRUE, nerrors, errors);
1705592Srrh 	qsort(errors, nerrors, sizeof(Eptr), errorsort);
1711457Sroot 	if (show_errors)
1721457Sroot 		printerrors(FALSE, nerrors, errors);
1731457Sroot 	findfiles(nerrors, errors, &nfiles, &files);
1741457Sroot #define P(msg, arg) fprintf(stdout, msg, arg)
1751457Sroot 	if (pr_summary){
1761457Sroot 	    if (nunknown)
1771457Sroot 	      P("%d Errors are unclassifiable.\n", nunknown);
1781457Sroot 	    if (nignore)
1791457Sroot 	      P("%d Errors are classifiable, but totally discarded.\n",nignore);
1801457Sroot 	    if (nsyncerrors)
1811457Sroot 	      P("%d Errors are synchronization errors.\n", nsyncerrors);
1821457Sroot 	    if (nignore)
1831457Sroot 	      P("%d Errors are discarded because they refer to sacrosinct files.\n", ndiscard);
1841457Sroot 	    if (nnulled)
1851457Sroot 	      P("%d Errors are nulled because they refer to specific functions.\n", nnulled);
1861457Sroot 	    if (nnonspec)
1871457Sroot 	      P("%d Errors are not specific to any file.\n", nnonspec);
1881457Sroot 	    if (nthisfile)
1891457Sroot 	      P("%d Errors are specific to a given file, but not to a line.\n", nthisfile);
1901457Sroot 	    if (ntrue)
1911457Sroot 	      P("%d Errors are true errors, and can be inserted into the files.\n", ntrue);
1921457Sroot 	}
1931457Sroot 	filenames(nfiles, files);
1941457Sroot 	fflush(stdout);
1955592Srrh 	if (touchfiles(nfiles, files, &ed_argc, &ed_argv) && edit_files)
1965592Srrh 		forkvi(ed_argc, ed_argv);
1975592Srrh }
1985592Srrh 
1995592Srrh forkvi(argc, argv)
2005592Srrh 	int	argc;
2015592Srrh 	char	**argv;
2025592Srrh {
2035592Srrh 	if (query){
2045592Srrh 		switch(inquire(terse
2055592Srrh 		    ? "Edit? "
2065592Srrh 		    : "Do you still want to edit the files you touched? ")){
2075592Srrh 		case Q_NO:
2085592Srrh 		case Q_no:
2095592Srrh 			return;
2105592Srrh 		default:
2115592Srrh 			break;
2121457Sroot 		}
2131457Sroot 	}
2145592Srrh 	/*
2155592Srrh 	 *	ed_agument's first argument is
2165592Srrh 	 *	a vi/ex compatabile search argument
2175592Srrh 	 *	to find the first occurance of ###
2185592Srrh 	 */
2195592Srrh 	try("vi", argc, argv);
2205592Srrh 	try("ex", argc, argv);
2215592Srrh 	try("ed", argc-1, argv+1);
2225592Srrh 	fprintf(stdout, "Can't find any editors.\n");
2231457Sroot }
2241457Sroot 
2251457Sroot try(name, argc, argv)
2261457Sroot 	char	*name;
2271457Sroot 	int	argc;
2281457Sroot 	char	**argv;
2291457Sroot {
2301457Sroot 	argv[0] = name;
2311457Sroot 	wordvprint(stdout, argc, argv);
2321457Sroot 	fprintf(stdout, "\n");
2331457Sroot 	fflush(stderr);
2341457Sroot 	fflush(stdout);
2351457Sroot 	sleep(2);
2361457Sroot 	if (freopen(im_on, "r", stdin) == NULL)
2371457Sroot 		return;
2381457Sroot 	if (freopen(im_on, "w", stdout) == NULL)
2391457Sroot 		return;
2401457Sroot 	execvp(name, argv);
2411457Sroot }
2421457Sroot 
2431457Sroot int errorsort(epp1, epp2)
2445592Srrh 		Eptr	*epp1, *epp2;
2451457Sroot {
2465592Srrh 	reg	Eptr	ep1, ep2;
2475592Srrh 		int	order;
2481457Sroot 	/*
2491457Sroot 	 *	Sort by:
2501457Sroot 	 *	1)	synchronization, non specific, discarded errors first;
2511457Sroot 	 *	2)	nulled and true errors last
2521457Sroot 	 *		a)	grouped by similar file names
2531457Sroot 	 *			1)	grouped in ascending line number
2541457Sroot 	 */
2551457Sroot 	ep1 = *epp1; ep2 = *epp2;
2561457Sroot 	if (ep1 == 0 || ep2 == 0)
2571457Sroot 		return(0);
2581457Sroot 	if ( (NOTSORTABLE(ep1->error_e_class)) ^ (NOTSORTABLE(ep2->error_e_class))){
2591457Sroot 		return(NOTSORTABLE(ep1->error_e_class) ? -1 : 1);
2601457Sroot 	}
2611457Sroot 	if (NOTSORTABLE(ep1->error_e_class))	/* then both are */
2621457Sroot 		return(ep1->error_no - ep2->error_no);
2631457Sroot 	order = strcmp(ep1->error_text[0], ep2->error_text[0]);
2641457Sroot 	if (order == 0){
2651457Sroot 		return(ep1->error_line - ep2->error_line);
2661457Sroot 	}
2671457Sroot 	return(order);
2681457Sroot }
269