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