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