xref: /netbsd-src/usr.bin/xlint/lint2/main2.c (revision 5a2e7833b234db34b627d04318995236487b7352)
1 /*	$NetBSD: main2.c,v 1.35 2023/12/03 18:17:41 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.35 2023/12/03 18:17:41 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 bool Cflag;
51 bool Fflag;
52 bool Hflag;
53 bool hflag;
54 bool sflag;
55 bool tflag;
56 bool uflag;
57 bool xflag;
58 const char *libname;
59 
60 /*
61  * List of libraries (from -l flag). These libraries are read after all
62  * other input files has been read and, for Cflag, after the new lint library
63  * has been written.
64  */
65 static const char **libs;
66 
67 static void usage(void) __attribute__((noreturn));
68 
69 static void
check_name_non_const(hte_t * hte)70 check_name_non_const(hte_t *hte)
71 {
72 	check_name(hte);
73 }
74 
75 int
main(int argc,char * argv[])76 main(int argc, char *argv[])
77 {
78 	int c, i;
79 	size_t len;
80 	char *lname;
81 
82 	libs = xcalloc(1, sizeof(*libs));
83 
84 	opterr = 0;
85 	while ((c = getopt(argc, argv, "hl:stuxC:HF")) != -1) {
86 		switch (c) {
87 		case 's':
88 			sflag = true;
89 			break;
90 		case 't':
91 			tflag = true;
92 			break;
93 		case 'u':
94 			uflag = true;
95 			break;
96 		case 'x':
97 			xflag = true;
98 			break;
99 		case 'C':
100 			len = strlen(optarg);
101 			lname = xmalloc(len + 10);
102 			(void)snprintf(lname, len + 10, "llib-l%s.ln", optarg);
103 			libname = lname;
104 			Cflag = true;
105 			uflag = true;
106 			break;
107 		case 'H':
108 			Hflag = true;
109 			break;
110 		case 'h':
111 			hflag = true;
112 			break;
113 		case 'F':
114 			Fflag = true;
115 			break;
116 		case 'l':
117 			for (i = 0; libs[i] != NULL; i++)
118 				continue;
119 			libs = xrealloc(libs, (i + 2) * sizeof(*libs));
120 			libs[i] = xstrdup(optarg);
121 			libs[i + 1] = NULL;
122 			break;
123 		default:
124 			usage();
125 		}
126 	}
127 
128 	argc -= optind;
129 	argv += optind;
130 
131 	if (argc == 0)
132 		usage();
133 
134 	symtab_init();
135 
136 	for (i = 0; i < argc; i++)
137 		readfile(argv[i]);
138 
139 	/* write the lint library */
140 	if (Cflag) {
141 		symtab_forall(mkstatic);
142 		outlib(libname);
143 	}
144 
145 	/* read additional libraries */
146 	for (i = 0; libs[i] != NULL; i++)
147 		readfile(libs[i]);
148 
149 	symtab_forall(mkstatic);
150 
151 	mark_main_as_used();
152 
153 	/* perform all tests */
154 	symtab_forall_sorted(check_name_non_const);
155 
156 	return 0;
157 }
158 
159 static void
usage(void)160 usage(void)
161 {
162 	(void)fprintf(stderr,
163 	    "usage: %s [-hstuxHF] -Clib -l lib ... src1 ...\n", getprogname());
164 	exit(1);
165 }
166