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