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