xref: /netbsd-src/usr.bin/xlint/lint2/main2.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: main2.c,v 1.8 2010/01/14 16:58:27 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: main2.c,v 1.8 2010/01/14 16:58:27 christos Exp $");
41 #endif
42 
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 
48 #include "lint2.h"
49 
50 /* warnings for symbols which are declared but not defined or used */
51 int	xflag;
52 
53 /*
54  * warnings for symbols which are used and not defined or defined
55  * and not used
56  */
57 int	uflag = 1;
58 
59 /* Create a lint library in the current directory with name libname. */
60 int	Cflag;
61 const	char *libname;
62 
63 int	pflag;
64 
65 /*
66  * warnings for (tentative) definitions of the same name in more than
67  * one translation unit
68  */
69 int	sflag;
70 
71 int	tflag;
72 
73 /*
74  * If a complaint stems from a included file, print the name of the included
75  * file instead of the name spezified at the command line followed by '?'
76  */
77 int	Hflag;
78 
79 int	hflag;
80 
81 /* Print full path names, not only the last component */
82 int	Fflag;
83 
84 /*
85  * List of libraries (from -l flag). These libraries are read after all
86  * other input files has been read and, for Cflag, after the new lint library
87  * has been written.
88  */
89 const	char	**libs;
90 
91 static	void	usage(void);
92 
93 int main(int, char *[]);
94 
95 int
96 main(int argc, char *argv[])
97 {
98 	int	c, i;
99 	size_t	len;
100 	char	*lname;
101 
102 	libs = xcalloc(1, sizeof (char *));
103 
104 	opterr = 0;
105 	while ((c = getopt(argc, argv, "hpstxuC:HFl:")) != -1) {
106 		switch (c) {
107 		case 's':
108 			sflag = 1;
109 			break;
110 		case 't':
111 			tflag = 1;
112 			break;
113 		case 'u':
114 			uflag = 0;
115 			break;
116 		case 'x':
117 			xflag = 1;
118 			break;
119 		case 'p':
120 			pflag = 1;
121 			break;
122 		case 'C':
123 			len = strlen(optarg);
124 			lname = xmalloc(len + 10);
125 			(void)sprintf(lname, "llib-l%s.ln", optarg);
126 			libname = lname;
127 			Cflag = 1;
128 			uflag = 0;
129 			break;
130 		case 'H':
131 			Hflag = 1;
132 			break;
133 		case 'h':
134 			hflag++;
135 			break;
136 		case 'F':
137 			Fflag = 1;
138 			break;
139 		case 'l':
140 			for (i = 0; libs[i] != NULL; i++)
141 				continue;
142 			libs = xrealloc(libs, (i + 2) * sizeof (char *));
143 			libs[i] = xstrdup(optarg);
144 			libs[i + 1] = NULL;
145 			break;
146 		case '?':
147 			usage();
148 		}
149 	}
150 
151 	argc -= optind;
152 	argv += optind;
153 
154 	if (argc == 0)
155 		usage();
156 
157 	initmem();
158 
159 	/* initialize hash table */
160 	inithash();
161 
162 	inittyp();
163 
164 	for (i = 0; i < argc; i++)
165 		readfile(argv[i]);
166 
167 	/* write the lint library */
168 	if (Cflag) {
169 		forall(mkstatic);
170 		outlib(libname);
171 	}
172 
173 	/* read additional libraries */
174 	for (i = 0; libs[i] != NULL; i++)
175 		readfile(libs[i]);
176 
177 	forall(mkstatic);
178 
179 	mainused();
180 
181 	/* perform all tests */
182 	forall(chkname);
183 
184 	exit(0);
185 	/* NOTREACHED */
186 }
187 
188 static void
189 usage(void)
190 {
191 	(void)fprintf(stderr,
192 		      "usage: lint2 -hpstxuHF -Clib -l lib ... src1 ...\n");
193 	exit(1);
194 }
195