Initial import from FreeBSD RELENG_4:
[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
34 #ifndef lint
35 static char rcsid[] = "$NetBSD: main2.c,v 1.2 1995/07/03 21:24:53 cgd Exp $";
36 #endif
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42
43 #include "lint2.h"
44
45 /* warnings for symbols which are declared but not defined or used */
46 int     xflag;
47
48 /*
49  * warnings for symbols which are used and not defined or defined
50  * and not used
51  */
52 int     uflag = 1;
53
54 /* Create a lint library in the current directory with name libname. */
55 int     Cflag;
56 const   char *libname;
57
58 int     pflag;
59
60 /*
61  * warnings for (tentative) definitions of the same name in more then
62  * one translation unit
63  */
64 int     sflag;
65
66 int     tflag;
67
68 /*
69  * If a complaint stems from a included file, print the name of the included
70  * file instead of the name spezified at the command line followed by '?'
71  */
72 int     Hflag;
73
74 int     hflag;
75
76 /* Print full path names, not only the last component */
77 int     Fflag;
78
79 /*
80  * List of libraries (from -l flag). These libraries are read after all
81  * other input files has been read and, for Cflag, after the new lint library
82  * has been written.
83  */
84 const   char    **libs;
85
86 static  void    usage __P((void));
87
88
89 int
90 main(argc, argv)
91         int     argc;
92         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                         libs = xrealloc(libs, (i + 2) * sizeof (char *)); 
138                         libs[i] = xstrdup(optarg);
139                         libs[i + 1] = NULL;
140                         break;
141                 case '?':
142                         usage();
143                 }
144         }
145         
146         argc -= optind;
147         argv += optind;
148
149         if (argc == 0)
150                 usage();
151
152         initmem();
153
154         /* initialize hash table */
155         inithash();
156
157         inittyp();
158
159         for (i = 0; i < argc; i++)
160                 readfile(argv[i]);
161
162         /* write the lint library */
163         if (Cflag) {
164                 forall(mkstatic);
165                 outlib(libname);
166         }
167
168         /* read additional libraries */
169         for (i = 0; libs[i] != NULL; i++)
170                 readfile(libs[i]);
171
172         forall(mkstatic);
173
174         mainused();
175
176         /* perform all tests */
177         forall(chkname);
178
179         exit(0);
180         /* NOTREACHED */
181 }
182
183 static void
184 usage()
185 {
186         (void)fprintf(stderr,
187                       "usage: lint2 -hpstxuHF -Clib -l lib ... src1 ...\n");
188         exit(1);
189 }
190