xref: /netbsd-src/usr.bin/xlint/lint2/main2.c (revision aef5eb5f59cdfe8314f1b5f78ac04eb144e44010)
1 /*	$NetBSD: main2.c,v 1.24 2022/05/20 21:18:55 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)
40 __RCSID("$NetBSD: main2.c,v 1.24 2022/05/20 21:18:55 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 /*
64  * warnings for (tentative) definitions of the same name in more than
65  * one translation unit
66  */
67 bool	sflag;
68 
69 bool	tflag;
70 
71 /*
72  * If a complaint stems from a included file, print the name of the included
73  * file instead of the name specified at the command line followed by '?'
74  */
75 bool	Hflag;
76 
77 bool	hflag;
78 
79 /* Print full path names, not only the last component */
80 bool	Fflag;
81 
82 /*
83  * List of libraries (from -l flag). These libraries are read after all
84  * other input files has been read and, for Cflag, after the new lint library
85  * has been written.
86  */
87 const char **libs;
88 
89 static	void	usage(void) __attribute__((noreturn));
90 
91 static void
92 check_name(hte_t *hte)
93 {
94 	chkname(hte);
95 }
96 
97 int
98 main(int argc, char *argv[])
99 {
100 	int	c, i;
101 	size_t	len;
102 	char	*lname;
103 
104 	libs = xcalloc(1, sizeof(*libs));
105 
106 	opterr = 0;
107 	while ((c = getopt(argc, argv, "hstxuC:HFl:")) != -1) {
108 		switch (c) {
109 		case 's':
110 			sflag = 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 'C':
122 			len = strlen(optarg);
123 			lname = xmalloc(len + 10);
124 			(void)sprintf(lname, "llib-l%s.ln", optarg);
125 			libname = lname;
126 			Cflag = true;
127 			uflag = false;
128 			break;
129 		case 'H':
130 			Hflag = true;
131 			break;
132 		case 'h':
133 			hflag = true;
134 			break;
135 		case 'F':
136 			Fflag = true;
137 			break;
138 		case 'l':
139 			for (i = 0; libs[i] != NULL; i++)
140 				continue;
141 			libs = xrealloc(libs, (i + 2) * sizeof(*libs));
142 			libs[i] = xstrdup(optarg);
143 			libs[i + 1] = NULL;
144 			break;
145 		default:
146 			usage();
147 		}
148 	}
149 
150 	argc -= optind;
151 	argv += optind;
152 
153 	if (argc == 0)
154 		usage();
155 
156 	initmem();
157 
158 	symtab_init();
159 
160 	for (i = 0; i < argc; i++)
161 		readfile(argv[i]);
162 
163 	/* write the lint library */
164 	if (Cflag) {
165 		symtab_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 	symtab_forall(mkstatic);
174 
175 	mainused();
176 
177 	/* perform all tests */
178 	symtab_forall_sorted(check_name);
179 
180 	exit(0);
181 	/* NOTREACHED */
182 }
183 
184 static void
185 usage(void)
186 {
187 	(void)fprintf(stderr,
188 		      "usage: lint2 -hpstxuHFT -Clib -l lib ... src1 ...\n");
189 	exit(1);
190 }
191