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