xref: /netbsd-src/usr.bin/xlint/lint1/main1.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: main1.c,v 1.16 2005/09/24 15:30:35 perry 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.16 2005/09/24 15:30:35 perry 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 
51 #include "lint1.h"
52 
53 /* set yydebug to 1*/
54 int	yflag;
55 
56 /*
57  * Print warnings if an assignment of an integertype to another integertype
58  * causes an implicit narrowing conversion. If aflag is 1, these warnings
59  * are printed only if the source type is at least as wide as long. If aflag
60  * is greater than 1, they are always printed.
61  */
62 int	aflag;
63 
64 /* Print a warning if a break statement cannot be reached. */
65 int	bflag;
66 
67 /* Print warnings for pointer casts. */
68 int	cflag;
69 
70 /* Print various debug information. */
71 int	dflag;
72 
73 /* Perform stricter checking of enum types and operations on enum types. */
74 int	eflag;
75 
76 /* Print complete pathnames, not only the basename. */
77 int	Fflag;
78 
79 /* Enable some extensions of gcc */
80 int	gflag;
81 
82 /* Treat warnings as errors */
83 int	wflag;
84 
85 /*
86  * Apply a number of heuristic tests to attempt to intuit bugs, improve
87  * style, and reduce waste.
88  */
89 int	hflag;
90 
91 /* Attempt to check portability to other dialects of C. */
92 int	pflag;
93 
94 /*
95  * In case of redeclarations/redefinitions print the location of the
96  * previous declaration/definition.
97  */
98 int	rflag;
99 
100 /* Strict ANSI C mode. */
101 int	sflag;
102 
103 /* Traditional C mode. */
104 int	tflag;
105 
106 /* Enable C9X extensions */
107 int	Sflag;
108 /*
109  * Complain about functions and external variables used and not defined,
110  * or defined and not used.
111  */
112 int	uflag = 1;
113 
114 /* Complain about unused function arguments. */
115 int	vflag = 1;
116 
117 /* Complain about structures which are never defined. */
118 int	zflag = 1;
119 
120 err_set	msgset;
121 
122 static	void	usage(void);
123 
124 int main(int, char *[]);
125 
126 int
127 main(int argc, char *argv[])
128 {
129 	int	c;
130 	char	*ptr;
131 
132 	setprogname(argv[0]);
133 
134 	ERR_ZERO(&msgset);
135 	while ((c = getopt(argc, argv, "abcdeghmprstuvwyzFSX:")) != -1) {
136 		switch (c) {
137 		case 'a':	aflag++;	break;
138 		case 'b':	bflag = 1;	break;
139 		case 'c':	cflag = 1;	break;
140 		case 'd':	dflag = 1;	break;
141 		case 'e':	eflag = 1;	break;
142 		case 'F':	Fflag = 1;	break;
143 		case 'g':	gflag = 1;	break;
144 		case 'h':	hflag = 1;	break;
145 		case 'p':	pflag = 1;	break;
146 		case 'r':	rflag = 1;	break;
147 		case 's':	sflag = 1;	break;
148 		case 'S':	Sflag = 1;	break;
149 		case 't':	tflag = 1;	break;
150 		case 'u':	uflag = 0;	break;
151 		case 'w':	wflag = 1;	break;
152 		case 'v':	vflag = 0;	break;
153 		case 'y':	yflag = 1;	break;
154 		case 'z':	zflag = 0;	break;
155 
156 		case 'm':
157 			msglist();
158 			return(0);
159 
160 		case 'X':
161 			for (ptr = strtok(optarg, ","); ptr;
162 			    ptr = strtok(NULL, ",")) {
163 				char *eptr;
164 				long msg;
165 
166 				errno = 0;
167 				msg = strtol(ptr, &eptr, 0);
168 				if ((msg == LONG_MIN || msg == LONG_MAX) &&
169 				    errno == ERANGE)
170 				    err(1, "invalid error message id '%s'",
171 					ptr);
172 				if (*eptr || ptr == eptr || msg < 0 ||
173 				    msg >= ERR_SETSIZE)
174 					errx(1, "invalid error message id '%s'",
175 					    ptr);
176 				ERR_SET(msg, &msgset);
177 			}
178 			break;
179 		case '?':
180 		default:
181 			usage();
182 			break;
183 		}
184 	}
185 	argc -= optind;
186 	argv += optind;
187 
188 	if (argc != 2)
189 		usage();
190 
191 	/* open the input file */
192 	if ((yyin = fopen(argv[0], "r")) == NULL)
193 		err(1, "cannot open '%s'", argv[0]);
194 
195 	/* initialize output */
196 	outopen(argv[1]);
197 
198 	if (yflag)
199 		yydebug = 1;
200 
201 	initmem();
202 	initdecl();
203 	initscan();
204 	initmtab();
205 
206 	yyparse();
207 
208 	/* Following warnings cannot be suppressed by LINTED */
209 	nowarn = 0;
210 
211 	chkglsyms();
212 
213 	outclose();
214 
215 	return (nerr != 0);
216 }
217 
218 static void
219 usage(void)
220 {
221 	(void)fprintf(stderr,
222 	    "Usage: %s [-abcdeghmprstuvwyzFS] [-X <id>[,<id>]... src dest\n",
223 	    getprogname());
224 	exit(1);
225 }
226 
227 void
228 norecover(void)
229 {
230 	/* cannot recover from previous errors */
231 	error(224);
232 	exit(1);
233 }
234