Home | History | Annotate | Line # | Download | only in lint1
main1.c revision 1.18
      1 /*	$NetBSD: main1.c,v 1.18 2008/05/02 15:10:05 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994, 1995 Jochen Pohl
      5  * 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 Jochen Pohl for
     18  *	The NetBSD Project.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #if HAVE_NBTOOL_CONFIG_H
     35 #include "nbtool_config.h"
     36 #endif
     37 
     38 #include <sys/cdefs.h>
     39 #if defined(__RCSID) && !defined(lint)
     40 __RCSID("$NetBSD: main1.c,v 1.18 2008/05/02 15:10:05 christos Exp $");
     41 #endif
     42 
     43 #include <sys/types.h>
     44 #include <stdio.h>
     45 #include <string.h>
     46 #include <stdlib.h>
     47 #include <unistd.h>
     48 #include <errno.h>
     49 #include <limits.h>
     50 #include <signal.h>
     51 
     52 #include "lint1.h"
     53 
     54 /* set yydebug to 1*/
     55 int	yflag;
     56 
     57 /*
     58  * Print warnings if an assignment of an integertype to another integertype
     59  * causes an implicit narrowing conversion. If aflag is 1, these warnings
     60  * are printed only if the source type is at least as wide as long. If aflag
     61  * is greater than 1, they are always printed.
     62  */
     63 int	aflag;
     64 
     65 /* Print a warning if a break statement cannot be reached. */
     66 int	bflag;
     67 
     68 /* Print warnings for pointer casts. */
     69 int	cflag;
     70 
     71 /* Print various debug information. */
     72 int	dflag;
     73 
     74 /* Perform stricter checking of enum types and operations on enum types. */
     75 int	eflag;
     76 
     77 /* Print complete pathnames, not only the basename. */
     78 int	Fflag;
     79 
     80 /* Enable some extensions of gcc */
     81 int	gflag;
     82 
     83 /* Treat warnings as errors */
     84 int	wflag;
     85 
     86 /*
     87  * Apply a number of heuristic tests to attempt to intuit bugs, improve
     88  * style, and reduce waste.
     89  */
     90 int	hflag;
     91 
     92 /* Attempt to check portability to other dialects of C. */
     93 int	pflag;
     94 
     95 /*
     96  * In case of redeclarations/redefinitions print the location of the
     97  * previous declaration/definition.
     98  */
     99 int	rflag;
    100 
    101 /* Strict ANSI C mode. */
    102 int	sflag;
    103 
    104 /* Traditional C mode. */
    105 int	tflag;
    106 
    107 /* Enable C9X extensions */
    108 int	Sflag;
    109 /*
    110  * Complain about functions and external variables used and not defined,
    111  * or defined and not used.
    112  */
    113 int	uflag = 1;
    114 
    115 /* Complain about unused function arguments. */
    116 int	vflag = 1;
    117 
    118 /* Complain about structures which are never defined. */
    119 int	zflag = 1;
    120 
    121 err_set	msgset;
    122 
    123 sig_atomic_t fpe;
    124 
    125 static	void	usage(void);
    126 
    127 int main(int, char *[]);
    128 
    129 /*ARGSUSED*/
    130 static void
    131 sigfpe(int s)
    132 {
    133 	fpe = 1;
    134 }
    135 
    136 int
    137 main(int argc, char *argv[])
    138 {
    139 	int	c;
    140 	char	*ptr;
    141 
    142 	setprogname(argv[0]);
    143 
    144 	ERR_ZERO(&msgset);
    145 	while ((c = getopt(argc, argv, "abcdeghmprstuvwyzFSX:")) != -1) {
    146 		switch (c) {
    147 		case 'a':	aflag++;	break;
    148 		case 'b':	bflag = 1;	break;
    149 		case 'c':	cflag = 1;	break;
    150 		case 'd':	dflag = 1;	break;
    151 		case 'e':	eflag = 1;	break;
    152 		case 'F':	Fflag = 1;	break;
    153 		case 'g':	gflag = 1;	break;
    154 		case 'h':	hflag = 1;	break;
    155 		case 'p':	pflag = 1;	break;
    156 		case 'r':	rflag = 1;	break;
    157 		case 's':	sflag = 1;	break;
    158 		case 'S':	Sflag = 1;	break;
    159 		case 't':	tflag = 1;	break;
    160 		case 'u':	uflag = 0;	break;
    161 		case 'w':	wflag = 1;	break;
    162 		case 'v':	vflag = 0;	break;
    163 		case 'y':	yflag = 1;	break;
    164 		case 'z':	zflag = 0;	break;
    165 
    166 		case 'm':
    167 			msglist();
    168 			return(0);
    169 
    170 		case 'X':
    171 			for (ptr = strtok(optarg, ","); ptr;
    172 			    ptr = strtok(NULL, ",")) {
    173 				char *eptr;
    174 				long msg;
    175 
    176 				errno = 0;
    177 				msg = strtol(ptr, &eptr, 0);
    178 				if ((msg == LONG_MIN || msg == LONG_MAX) &&
    179 				    errno == ERANGE)
    180 				    err(1, "invalid error message id '%s'",
    181 					ptr);
    182 				if (*eptr || ptr == eptr || msg < 0 ||
    183 				    msg >= ERR_SETSIZE)
    184 					errx(1, "invalid error message id '%s'",
    185 					    ptr);
    186 				ERR_SET(msg, &msgset);
    187 			}
    188 			break;
    189 		case '?':
    190 		default:
    191 			usage();
    192 			break;
    193 		}
    194 	}
    195 	argc -= optind;
    196 	argv += optind;
    197 
    198 	if (argc != 2)
    199 		usage();
    200 
    201 	/* open the input file */
    202 	if ((yyin = fopen(argv[0], "r")) == NULL)
    203 		err(1, "cannot open '%s'", argv[0]);
    204 
    205 	/* initialize output */
    206 	outopen(argv[1]);
    207 
    208 	if (yflag)
    209 		yydebug = 1;
    210 
    211 	(void)signal(SIGFPE, sigfpe);
    212 	initmem();
    213 	initdecl();
    214 	initscan();
    215 	initmtab();
    216 
    217 	yyparse();
    218 
    219 	/* Following warnings cannot be suppressed by LINTED */
    220 	nowarn = 0;
    221 #ifdef DEBUG
    222 	printf("%s, %d: nowarn = 0\n", curr_pos.p_file, curr_pos.p_line);
    223 #endif
    224 
    225 	chkglsyms();
    226 
    227 	outclose();
    228 
    229 	return (nerr != 0);
    230 }
    231 
    232 static void
    233 usage(void)
    234 {
    235 	(void)fprintf(stderr,
    236 	    "Usage: %s [-abcdeghmprstuvwyzFS] [-X <id>[,<id>]... src dest\n",
    237 	    getprogname());
    238 	exit(1);
    239 }
    240 
    241 void
    242 norecover(void)
    243 {
    244 	/* cannot recover from previous errors */
    245 	error(224);
    246 	exit(1);
    247 }
    248