xref: /trunk/main/soltools/javadep/javadep.c (revision 402c6fde746b160d73dd806e2d51c38830f54a1e)
17ce20373SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
37ce20373SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
47ce20373SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
57ce20373SAndrew Rist  * distributed with this work for additional information
67ce20373SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
77ce20373SAndrew Rist  * to you under the Apache License, Version 2.0 (the
87ce20373SAndrew Rist  * "License"); you may not use this file except in compliance
97ce20373SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
117ce20373SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
137ce20373SAndrew Rist  * Unless required by applicable law or agreed to in writing,
147ce20373SAndrew Rist  * software distributed under the License is distributed on an
157ce20373SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
167ce20373SAndrew Rist  * KIND, either express or implied.  See the License for the
177ce20373SAndrew Rist  * specific language governing permissions and limitations
187ce20373SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
207ce20373SAndrew Rist  *************************************************************/
217ce20373SAndrew Rist 
227ce20373SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir /* All Java Virtual Machine Specs are from
25cdf0e10cSrcweir  * "The Java Virtual Machine Specification", T. Lindholm, F. Yellin
26cdf0e10cSrcweir  * (JVMS)
27cdf0e10cSrcweir  */
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <stdlib.h>
30cdf0e10cSrcweir #include <stdio.h>
31cdf0e10cSrcweir #include <stdarg.h>
32cdf0e10cSrcweir #include <string.h>
33cdf0e10cSrcweir #include <errno.h>
34cdf0e10cSrcweir #include <ctype.h>
35cdf0e10cSrcweir #include <limits.h>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #if defined(UNX) || defined(OS2)
38cdf0e10cSrcweir #include <unistd.h>
39cdf0e10cSrcweir #include <netinet/in.h>     /* ntohl(), ntohs() */
40cdf0e10cSrcweir #elif defined(WNT)
41cdf0e10cSrcweir #include <io.h>
42cdf0e10cSrcweir #define access      _access
43cdf0e10cSrcweir #define vsnprintf   _vsnprintf
44cdf0e10cSrcweir #define CDECL       _cdecl
45cdf0e10cSrcweir #define F_OK        00
46cdf0e10cSrcweir #define PATH_MAX    _MAX_PATH
47cdf0e10cSrcweir #define ntohl(x)    ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) | \
48cdf0e10cSrcweir                         (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))
49cdf0e10cSrcweir 
50cdf0e10cSrcweir #define ntohs(x)    ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
51cdf0e10cSrcweir #endif
52cdf0e10cSrcweir 
53cdf0e10cSrcweir #if defined(OS2)
54cdf0e10cSrcweir #define CDECL
55cdf0e10cSrcweir #endif
56cdf0e10cSrcweir 
57cdf0e10cSrcweir /* max. length of line in response file */
58cdf0e10cSrcweir #define RES_FILE_BUF    65536
59cdf0e10cSrcweir 
60cdf0e10cSrcweir struct file {
61cdf0e10cSrcweir     char *pname;
62cdf0e10cSrcweir     FILE *pfs;
63cdf0e10cSrcweir };
64cdf0e10cSrcweir 
65cdf0e10cSrcweir struct growable {
66cdf0e10cSrcweir     int     ncur;
67cdf0e10cSrcweir     int     nmax;
68cdf0e10cSrcweir     char    **parray;
69cdf0e10cSrcweir };
70cdf0e10cSrcweir 
71cdf0e10cSrcweir typedef struct file     file_t;
72cdf0e10cSrcweir typedef unsigned char   uint8;
73cdf0e10cSrcweir typedef unsigned short  uint16;
74cdf0e10cSrcweir typedef unsigned int    uint32;
75cdf0e10cSrcweir 
76cdf0e10cSrcweir struct utf8 {
77cdf0e10cSrcweir     uint16  nlen;
78cdf0e10cSrcweir     void    *pdata;
79cdf0e10cSrcweir };
80cdf0e10cSrcweir 
81cdf0e10cSrcweir typedef struct utf8     utf8_t;
82cdf0e10cSrcweir 
83cdf0e10cSrcweir /* The contents of the Constant_pool is described in JVMS p. 93
84cdf0e10cSrcweir  */
85cdf0e10cSrcweir enum {
86cdf0e10cSrcweir     CONSTANT_Class              = 7,
87cdf0e10cSrcweir     CONSTANT_Fieldref           = 9,
88cdf0e10cSrcweir     CONSTANT_Methodref          = 10,
89cdf0e10cSrcweir     CONSTANT_InterfaceMethodref = 11,
90cdf0e10cSrcweir     CONSTANT_String             = 8,
91cdf0e10cSrcweir     CONSTANT_Integer            = 3,
92cdf0e10cSrcweir     CONSTANT_Float              = 4,
93cdf0e10cSrcweir     CONSTANT_Long               = 5,
94cdf0e10cSrcweir     CONSTANT_Double             = 6,
95cdf0e10cSrcweir     CONSTANT_NameAndType        = 12,
96d32879a4SDamjan Jovanovic     CONSTANT_Utf8               = 1,
97d32879a4SDamjan Jovanovic     CONSTANT_MethodHandle       = 15,
98d32879a4SDamjan Jovanovic     CONSTANT_MethodType         = 16,
99d32879a4SDamjan Jovanovic     CONSTANT_InvokeDynamic      = 18
100cdf0e10cSrcweir };
101cdf0e10cSrcweir 
102cdf0e10cSrcweir enum { NGROW_INIT = 10, NGROW = 2 };
103cdf0e10cSrcweir 
104cdf0e10cSrcweir static char     *pprogname  = "javadep";
105cdf0e10cSrcweir static char     csep        = ';';
106cdf0e10cSrcweir #if defined (UNX) || defined(OS2)
107cdf0e10cSrcweir #define CDECL
108cdf0e10cSrcweir static char     cpathsep    = '/';
109cdf0e10cSrcweir #elif defined (WNT) || defined(OS2)
110cdf0e10cSrcweir static char     cpathsep    = '\\';
111cdf0e10cSrcweir #endif
112cdf0e10cSrcweir static FILE     *pfsout     = NULL;
113cdf0e10cSrcweir static char     *pout_file  = NULL;
114cdf0e10cSrcweir 
115cdf0e10cSrcweir 
116cdf0e10cSrcweir /* prototypes */
117cdf0e10cSrcweir uint8   read_uint8(const file_t *pfile);
118cdf0e10cSrcweir uint16  read_uint16(const file_t *pfile);
119cdf0e10cSrcweir uint32  read_uint32(const file_t *pfile);
120cdf0e10cSrcweir void    skip_bytes(const file_t *pfile, const size_t nnum);
121cdf0e10cSrcweir char    *escape_slash(const char *pstr);
122cdf0e10cSrcweir int     is_inner(const char *pstr);
123cdf0e10cSrcweir void    print_dependencies(const struct growable *pdep,
124cdf0e10cSrcweir                            const char* pclass_file);
125cdf0e10cSrcweir void    process_class_file(const char *pfilenamem,
126cdf0e10cSrcweir                            const struct growable *pfilt);
127cdf0e10cSrcweir char    *utf8tolatin1(const utf8_t a_utf8);
128cdf0e10cSrcweir void    *xmalloc(size_t size);
129cdf0e10cSrcweir void    *xcalloc(size_t nmemb, size_t size);
130cdf0e10cSrcweir void    *xrealloc(void *ptr, size_t size);
131cdf0e10cSrcweir void    grow_if_needed (struct growable *pgrow);
132cdf0e10cSrcweir int     append_to_growable(struct growable *, char *);
133cdf0e10cSrcweir struct growable *allocate_growable(void);
134cdf0e10cSrcweir void    free_growable(struct growable *pgrowvoid);
135cdf0e10cSrcweir void    create_filters(struct growable *pfilt, const struct growable *pinc);
136cdf0e10cSrcweir void    usage(void);
137cdf0e10cSrcweir void    err_quit(const char *, ...);
138cdf0e10cSrcweir void    silent_quit(void);
139cdf0e10cSrcweir 
140cdf0e10cSrcweir #ifdef WNT
141cdf0e10cSrcweir /* poor man's getopt() */
142cdf0e10cSrcweir int     simple_getopt(char *pargv[], const char *poptstring);
143cdf0e10cSrcweir char    *optarg = NULL;
144cdf0e10cSrcweir int     optind  = 1;
145cdf0e10cSrcweir int     optopt  = 0;
146cdf0e10cSrcweir int     opterr  = 0;
147cdf0e10cSrcweir #endif
148cdf0e10cSrcweir 
149cdf0e10cSrcweir uint8
150cdf0e10cSrcweir read_uint8(const file_t *pfile)
151cdf0e10cSrcweir {
152cdf0e10cSrcweir     /* read a byte from classfile */
153cdf0e10cSrcweir     int nread;
154cdf0e10cSrcweir     uint8 ndata;
155cdf0e10cSrcweir     nread = fread(&ndata, sizeof(uint8), 1, pfile->pfs);
156cdf0e10cSrcweir     if ( !nread ) {
157cdf0e10cSrcweir         fclose(pfile->pfs);
158cdf0e10cSrcweir         err_quit("%s: truncated class file", pfile->pname);
159cdf0e10cSrcweir     }
160cdf0e10cSrcweir     return ndata;
161cdf0e10cSrcweir }
162cdf0e10cSrcweir 
163cdf0e10cSrcweir uint16
164cdf0e10cSrcweir read_uint16(const file_t *pfile)
165cdf0e10cSrcweir {
166cdf0e10cSrcweir     /* read a short from classfile and convert it to host format */
167cdf0e10cSrcweir     int nread;
168cdf0e10cSrcweir     uint16 ndata;
169cdf0e10cSrcweir     nread = fread(&ndata, sizeof(uint16), 1, pfile->pfs);
170cdf0e10cSrcweir     if ( !nread ) {
171cdf0e10cSrcweir         fclose(pfile->pfs);
172cdf0e10cSrcweir         err_quit("%s: truncated class file", pfile->pname);
173cdf0e10cSrcweir     }
174cdf0e10cSrcweir     ndata = ntohs(ndata);
175cdf0e10cSrcweir     return ndata;
176cdf0e10cSrcweir }
177cdf0e10cSrcweir 
178cdf0e10cSrcweir uint32
179cdf0e10cSrcweir read_uint32(const file_t *pfile)
180cdf0e10cSrcweir {
181cdf0e10cSrcweir     /* read an int from classfile and convert it to host format */
182cdf0e10cSrcweir     int nread;
183cdf0e10cSrcweir     uint32 ndata;
184cdf0e10cSrcweir     nread = fread(&ndata, sizeof(uint32), 1, pfile->pfs);
185cdf0e10cSrcweir     if ( !nread ) {
186cdf0e10cSrcweir         fclose(pfile->pfs);
187cdf0e10cSrcweir         err_quit("%s: truncated class file", pfile->pname);
188cdf0e10cSrcweir     }
189cdf0e10cSrcweir     ndata = ntohl(ndata);
190cdf0e10cSrcweir     return ndata;
191cdf0e10cSrcweir }
192cdf0e10cSrcweir 
193cdf0e10cSrcweir utf8_t
194cdf0e10cSrcweir read_utf8(const file_t *pfile)
195cdf0e10cSrcweir {
196cdf0e10cSrcweir     /* Read a java utf-8-string with uint16 length prependend
197cdf0e10cSrcweir      * from class file. Returns utf8 struct
198cdf0e10cSrcweir      * with fresh allocated datablock,
199cdf0e10cSrcweir      * caller is responsible for freeing.
200cdf0e10cSrcweir      * Data is still in network byteorder
201cdf0e10cSrcweir      */
202cdf0e10cSrcweir 
203cdf0e10cSrcweir     utf8_t  a_utf8;
204cdf0e10cSrcweir     int     nread;
205cdf0e10cSrcweir 
206cdf0e10cSrcweir     a_utf8.pdata = NULL;
207cdf0e10cSrcweir 
208cdf0e10cSrcweir     a_utf8.nlen = read_uint16(pfile);
209cdf0e10cSrcweir     if (a_utf8.nlen > 0) {
210cdf0e10cSrcweir         a_utf8.pdata = xmalloc(a_utf8.nlen*sizeof(char));
211cdf0e10cSrcweir         nread = fread(a_utf8.pdata, a_utf8.nlen*sizeof(char), 1, pfile->pfs);
212cdf0e10cSrcweir         if ( !nread ) {
213cdf0e10cSrcweir             fclose(pfile->pfs);
214cdf0e10cSrcweir             err_quit("%s: truncated class file", pfile->pname);
215cdf0e10cSrcweir         }
216cdf0e10cSrcweir     }
217cdf0e10cSrcweir 
218cdf0e10cSrcweir     return a_utf8;
219cdf0e10cSrcweir }
220cdf0e10cSrcweir 
221cdf0e10cSrcweir char *utf8tolatin1(const utf8_t a_utf8)
222cdf0e10cSrcweir {
223cdf0e10cSrcweir     /* function returns fresh allocated zero terminated string,
224cdf0e10cSrcweir      * caller is responsible for freeing
225cdf0e10cSrcweir      */
226cdf0e10cSrcweir 
227cdf0e10cSrcweir     /* JVMS p. 101: the null byte is encoded using a two byte format,
228cdf0e10cSrcweir      * Java Virtual Machine Utf8 strings differ in this respect from
229cdf0e10cSrcweir      * standard UTF-8 strings
230cdf0e10cSrcweir      */
231cdf0e10cSrcweir 
232cdf0e10cSrcweir     /* Multibyte data is in network byte order */
233cdf0e10cSrcweir 
234cdf0e10cSrcweir     char *p;
235cdf0e10cSrcweir     char *pp;
236cdf0e10cSrcweir     char *pstr;
237cdf0e10cSrcweir 
238cdf0e10cSrcweir     pstr = pp = xmalloc((a_utf8.nlen+1) * sizeof(char));
239cdf0e10cSrcweir 
240cdf0e10cSrcweir     for ( p = (char*)a_utf8.pdata;
241cdf0e10cSrcweir           p <  (char*)a_utf8.pdata+a_utf8.nlen;
242cdf0e10cSrcweir           p++ ) {
243cdf0e10cSrcweir         if ( *p & 0x80 ) {
244cdf0e10cSrcweir             err_quit("sorry, real UTF8 decoding not yet implemented\n");
245cdf0e10cSrcweir         } else {
246cdf0e10cSrcweir             *pp++ = *p;
247cdf0e10cSrcweir         }
248cdf0e10cSrcweir     }
249cdf0e10cSrcweir     *pp = '\0';
250cdf0e10cSrcweir 
251cdf0e10cSrcweir     return pstr;
252cdf0e10cSrcweir }
253cdf0e10cSrcweir 
254cdf0e10cSrcweir 
255cdf0e10cSrcweir void
256cdf0e10cSrcweir skip_bytes(const file_t *pfile, const size_t nnumber)
257cdf0e10cSrcweir {
258cdf0e10cSrcweir     /* skip a nnumber of bytes in classfile */
259cdf0e10cSrcweir     if ( fseek(pfile->pfs, nnumber, SEEK_CUR) == -1 )
260cdf0e10cSrcweir          err_quit("%s: %s", pfile->pname, strerror(errno));
261cdf0e10cSrcweir }
262cdf0e10cSrcweir 
263cdf0e10cSrcweir void
264cdf0e10cSrcweir add_to_dependencies(struct growable *pdep,
265cdf0e10cSrcweir                     const struct growable *pfilt,
266cdf0e10cSrcweir                     char *pdepstr,
267cdf0e10cSrcweir                     const char *pclass_file)
268cdf0e10cSrcweir {
269cdf0e10cSrcweir     /* create dependencies */
270cdf0e10cSrcweir     int i;
271cdf0e10cSrcweir     int nlen_filt, nlen_str, nlen_pdepstr;
272cdf0e10cSrcweir     char *pstr, *ptrunc;
273cdf0e10cSrcweir     char path[PATH_MAX+1];
274cdf0e10cSrcweir     char cnp_class_file[PATH_MAX+1];
275cdf0e10cSrcweir     char cnp_str[PATH_MAX+1];
276cdf0e10cSrcweir 
277cdf0e10cSrcweir     nlen_pdepstr = strlen(pdepstr);
278cdf0e10cSrcweir     pstr = xmalloc((nlen_pdepstr+6+1)*sizeof(char));
279cdf0e10cSrcweir     memcpy(pstr, pdepstr, nlen_pdepstr+1);
280cdf0e10cSrcweir     strncat(pstr, ".class", 6);
281cdf0e10cSrcweir 
282cdf0e10cSrcweir     if ( pfilt->ncur == 0 ) { /* no filters */
283cdf0e10cSrcweir         if ( access(pstr, F_OK) == 0 ) {
284cdf0e10cSrcweir             append_to_growable(pdep, strdup(pstr));
285cdf0e10cSrcweir         }
286cdf0e10cSrcweir     } else {
287cdf0e10cSrcweir         nlen_str    = strlen(pstr);
288cdf0e10cSrcweir         for ( i = 0; i < pfilt->ncur; i++ ) {
289cdf0e10cSrcweir             nlen_filt   = strlen(pfilt->parray[i]);
290cdf0e10cSrcweir             if ( nlen_filt + 1 + nlen_str > PATH_MAX )
291cdf0e10cSrcweir                 err_quit("path to long");
292cdf0e10cSrcweir             memcpy(path, pfilt->parray[i], nlen_filt);
293cdf0e10cSrcweir             path[nlen_filt]     = '/';
294cdf0e10cSrcweir             memcpy( path+nlen_filt+1, pstr, nlen_str+1);
295cdf0e10cSrcweir 
296cdf0e10cSrcweir             if ( access(path, F_OK) != 0 ) {
297cdf0e10cSrcweir                 free(pstr);
298cdf0e10cSrcweir                 pstr = NULL;
299cdf0e10cSrcweir                 return; /* path doesn't represent a real file, don't bother */
300cdf0e10cSrcweir             }
301cdf0e10cSrcweir 
302cdf0e10cSrcweir             /* get the canonical path */
303cdf0e10cSrcweir #if defined (UNX) || defined(OS2)
304cdf0e10cSrcweir             if ( !(realpath(pclass_file, cnp_class_file)
305cdf0e10cSrcweir                 && realpath(path, cnp_str) ) ) {
306cdf0e10cSrcweir                 err_quit("can't get the canonical path");
307cdf0e10cSrcweir             }
308cdf0e10cSrcweir #else
309cdf0e10cSrcweir             if ( !(_fullpath(cnp_class_file, pclass_file, sizeof(cnp_class_file))
310cdf0e10cSrcweir                 && _fullpath(cnp_str, path, sizeof(cnp_str)) ) ) {
311cdf0e10cSrcweir                 err_quit("can't get the canonical path");
312cdf0e10cSrcweir             }
313cdf0e10cSrcweir #endif
314cdf0e10cSrcweir 
315cdf0e10cSrcweir             /* truncate so that only the package prefix remains */
316cdf0e10cSrcweir             ptrunc = strrchr(cnp_str, cpathsep);
317cdf0e10cSrcweir             *ptrunc = '\0';
318cdf0e10cSrcweir             ptrunc = strrchr(cnp_class_file, cpathsep);
319cdf0e10cSrcweir             *ptrunc = '\0';
320cdf0e10cSrcweir 
321cdf0e10cSrcweir             if ( !strcmp(cnp_str, cnp_class_file) ) {
322cdf0e10cSrcweir                 free(pstr);
323cdf0e10cSrcweir                 pstr = NULL;
324cdf0e10cSrcweir                 return; /* identical, don't bother with this one */
325cdf0e10cSrcweir             }
326cdf0e10cSrcweir 
327cdf0e10cSrcweir             append_to_growable(pdep, strdup(path));
328cdf0e10cSrcweir         }
329cdf0e10cSrcweir     }
330cdf0e10cSrcweir     free(pstr);
331cdf0e10cSrcweir     return;
332cdf0e10cSrcweir }
333cdf0e10cSrcweir 
334cdf0e10cSrcweir char *
335cdf0e10cSrcweir escape_slash(const char *pstr)
336cdf0e10cSrcweir {
337cdf0e10cSrcweir     /* returns a fresh allocated string with all cpathsep escaped exchanged
338cdf0e10cSrcweir      * with "$/"
339cdf0e10cSrcweir      *
340cdf0e10cSrcweir      * caller is responsible for freeing
341cdf0e10cSrcweir      */
342cdf0e10cSrcweir 
343cdf0e10cSrcweir     const char *pp = pstr;
344cdf0e10cSrcweir     char *p, *pnp;
345cdf0e10cSrcweir     char *pnew_str;
346cdf0e10cSrcweir     int nlen_pnp, nlen_pp;
347cdf0e10cSrcweir     int i = 0;
348cdf0e10cSrcweir 
349cdf0e10cSrcweir     while ( (p=strchr(pp, cpathsep)) != NULL ) {
350cdf0e10cSrcweir         ++i;
351cdf0e10cSrcweir         pp = ++p;
352cdf0e10cSrcweir     }
353cdf0e10cSrcweir 
354cdf0e10cSrcweir     nlen_pnp = strlen(pstr) + i;
355cdf0e10cSrcweir     pnp = pnew_str = xmalloc((nlen_pnp+1) * sizeof(char));
356cdf0e10cSrcweir 
357cdf0e10cSrcweir     pp = pstr;
358cdf0e10cSrcweir 
359cdf0e10cSrcweir     if ( i > 0 ) {
360cdf0e10cSrcweir         while ( (p=strchr(pp, cpathsep)) != NULL ) {
361cdf0e10cSrcweir             memcpy(pnp, pp, p-pp);
362cdf0e10cSrcweir             pnp += p-pp;
363cdf0e10cSrcweir             *pnp++ = '$';
364cdf0e10cSrcweir             *pnp++ = '/';
365cdf0e10cSrcweir             pp = ++p;
366cdf0e10cSrcweir         }
367cdf0e10cSrcweir     }
368cdf0e10cSrcweir     nlen_pp = strlen(pp);
369cdf0e10cSrcweir     memcpy(pnp, pp, nlen_pp+1);
370cdf0e10cSrcweir 
371cdf0e10cSrcweir     return pnew_str;
372cdf0e10cSrcweir }
373cdf0e10cSrcweir 
374cdf0e10cSrcweir 
375cdf0e10cSrcweir void
376cdf0e10cSrcweir print_dependencies(const struct growable *pdep, const char* pclass_file)
377cdf0e10cSrcweir {
378cdf0e10cSrcweir     char *pstr;
379cdf0e10cSrcweir     int i;
380cdf0e10cSrcweir 
381cdf0e10cSrcweir     pstr = escape_slash(pclass_file);
382cdf0e10cSrcweir     fprintf(pfsout, "%s:", pstr);
383cdf0e10cSrcweir     free(pstr);
384cdf0e10cSrcweir 
385cdf0e10cSrcweir     for( i=0; i<pdep->ncur; ++i) {
386cdf0e10cSrcweir         fprintf(pfsout, "  \\\n");
387cdf0e10cSrcweir         pstr=escape_slash(pdep->parray[i]);
388cdf0e10cSrcweir         fprintf(pfsout, "\t%s", pstr);
389cdf0e10cSrcweir         free(pstr);
390cdf0e10cSrcweir     }
391cdf0e10cSrcweir 
392cdf0e10cSrcweir     fprintf(pfsout,"\n\n");
393cdf0e10cSrcweir     return;
394cdf0e10cSrcweir }
395cdf0e10cSrcweir 
396cdf0e10cSrcweir int
397cdf0e10cSrcweir is_inner(const char *pstr)
398cdf0e10cSrcweir {
399cdf0e10cSrcweir     /* return true if character '$' is found in classname */
400cdf0e10cSrcweir 
401cdf0e10cSrcweir     /*
402cdf0e10cSrcweir      * note that a '$' in a classname is not an exact indicator
403cdf0e10cSrcweir      * for an inner class. Java identifier may legally contain
404*402c6fdeSJohn Bampton      * this character, and so may classnames. In the context
405cdf0e10cSrcweir      * of javadep this doesn't matter since the makefile system
406cdf0e10cSrcweir      * can't cope with classfiles with '$'s in the filename
407cdf0e10cSrcweir      * anyway.
408cdf0e10cSrcweir      *
409cdf0e10cSrcweir      */
410cdf0e10cSrcweir 
411cdf0e10cSrcweir     if ( strchr(pstr, '$') != NULL )
412cdf0e10cSrcweir         return 1;
413cdf0e10cSrcweir 
414cdf0e10cSrcweir     return 0;
415cdf0e10cSrcweir }
416cdf0e10cSrcweir 
417cdf0e10cSrcweir void
418cdf0e10cSrcweir process_class_file(const char *pfilename, const struct growable *pfilt)
419cdf0e10cSrcweir {
420cdf0e10cSrcweir     /* read class file and extract object information
421cdf0e10cSrcweir      * java class files are in bigendian data format
422cdf0e10cSrcweir      * (JVMS, p. 83)
423cdf0e10cSrcweir      */
424cdf0e10cSrcweir     int     i;
425cdf0e10cSrcweir     uint32  nmagic;
426cdf0e10cSrcweir     uint16  nminor, nmajor;
427cdf0e10cSrcweir     uint16  ncnt;
428cdf0e10cSrcweir     uint16  nclass_cnt;
429cdf0e10cSrcweir     utf8_t* pc_pool;
430cdf0e10cSrcweir     uint16* pc_class;
431cdf0e10cSrcweir     file_t  file;
432cdf0e10cSrcweir 
433cdf0e10cSrcweir     struct growable *pdepen;
434cdf0e10cSrcweir 
435cdf0e10cSrcweir     file.pname = (char*)pfilename;
436cdf0e10cSrcweir 
437cdf0e10cSrcweir     file.pfs = fopen(file.pname,"rb");
438cdf0e10cSrcweir     if ( !file.pfs )
439cdf0e10cSrcweir         silent_quit();
440cdf0e10cSrcweir 
441cdf0e10cSrcweir     nmagic = read_uint32(&file);
442cdf0e10cSrcweir 
443cdf0e10cSrcweir     if ( nmagic != 0xCAFEBABE ) {
444cdf0e10cSrcweir         fclose(file.pfs);
445cdf0e10cSrcweir         err_quit("%s: invalid magic", file.pname);
446cdf0e10cSrcweir     }
447cdf0e10cSrcweir 
448cdf0e10cSrcweir     nminor = read_uint16(&file);
449cdf0e10cSrcweir     nmajor = read_uint16(&file);
450cdf0e10cSrcweir 
451cdf0e10cSrcweir     /* get number of entries in constant pool */
452cdf0e10cSrcweir     ncnt = read_uint16(&file);
453cdf0e10cSrcweir 
454cdf0e10cSrcweir #ifdef DEBUG
455cdf0e10cSrcweir     printf("Magic: %p\n", (void*)nmagic);
456cdf0e10cSrcweir     printf("Major %d, Minor %d\n", nmajor, nminor);
457cdf0e10cSrcweir     printf("Const_pool_count %d\n", ncnt);
458cdf0e10cSrcweir #endif
459cdf0e10cSrcweir 
460cdf0e10cSrcweir     /* There can be ncount entries in the constant_pool table
461cdf0e10cSrcweir      * so at most ncount-1 of them can be of type CONSTANT_Class
462cdf0e10cSrcweir      * (at leat one CONSTANT_Utf8 entry must exist).
463cdf0e10cSrcweir      * Usually way less CONSTANT_Class entries exists, of course
464cdf0e10cSrcweir      */
465cdf0e10cSrcweir 
466cdf0e10cSrcweir     pc_pool = xcalloc(ncnt,sizeof(utf8_t));
467cdf0e10cSrcweir     pc_class = xmalloc((ncnt-1)*sizeof(uint16));
468cdf0e10cSrcweir 
469cdf0e10cSrcweir     /* pc_pool[0] is reserved to the java virtual machine and does
470cdf0e10cSrcweir      * not exist in the class file
471cdf0e10cSrcweir      */
472cdf0e10cSrcweir 
473cdf0e10cSrcweir     nclass_cnt = 0;
474cdf0e10cSrcweir 
475cdf0e10cSrcweir     for (i = 1; i < ncnt; i++) {
476cdf0e10cSrcweir         uint8   ntag;
477cdf0e10cSrcweir         uint16  nindex;
478cdf0e10cSrcweir         utf8_t  a_utf8;
479cdf0e10cSrcweir 
480cdf0e10cSrcweir         ntag = read_uint8(&file);
481cdf0e10cSrcweir 
482cdf0e10cSrcweir         /* we are only interested in CONSTANT_Class entries and
483cdf0e10cSrcweir          * Utf8 string entries, because they might belong to
484cdf0e10cSrcweir          * CONSTANT_Class entries
485cdf0e10cSrcweir          */
486cdf0e10cSrcweir         switch(ntag) {
487cdf0e10cSrcweir             case CONSTANT_Class:
488cdf0e10cSrcweir                 nindex = read_uint16(&file);
489cdf0e10cSrcweir                 pc_class[nclass_cnt++] = nindex;
490cdf0e10cSrcweir                 break;
491cdf0e10cSrcweir             case CONSTANT_Fieldref:
492cdf0e10cSrcweir             case CONSTANT_Methodref:
493cdf0e10cSrcweir             case CONSTANT_InterfaceMethodref:
494cdf0e10cSrcweir                 skip_bytes(&file, 4L);
495cdf0e10cSrcweir                 break;
496cdf0e10cSrcweir             case CONSTANT_String:
497cdf0e10cSrcweir                 skip_bytes(&file, 2L);
498cdf0e10cSrcweir                 break;
499cdf0e10cSrcweir             case CONSTANT_Integer:
500cdf0e10cSrcweir             case CONSTANT_Float:
501cdf0e10cSrcweir                 skip_bytes(&file, 4L);
502cdf0e10cSrcweir                 break;
503cdf0e10cSrcweir             case CONSTANT_Long:
504cdf0e10cSrcweir             case CONSTANT_Double:
505cdf0e10cSrcweir                 skip_bytes(&file, 8L);
506cdf0e10cSrcweir                 /* Long and Doubles take 2(!)
507cdf0e10cSrcweir                  * entries in constant_pool_table
508cdf0e10cSrcweir                  */
509cdf0e10cSrcweir                 i++;
510cdf0e10cSrcweir                 break;
511cdf0e10cSrcweir             case CONSTANT_NameAndType:
512cdf0e10cSrcweir                 skip_bytes(&file, 4L);
513cdf0e10cSrcweir                 break;
514cdf0e10cSrcweir             case CONSTANT_Utf8:
515cdf0e10cSrcweir                 a_utf8 = read_utf8(&file);
516cdf0e10cSrcweir                 pc_pool[i] = a_utf8;
517cdf0e10cSrcweir                 break;
518d32879a4SDamjan Jovanovic             case CONSTANT_MethodHandle:
519d32879a4SDamjan Jovanovic                 skip_bytes(&file, 3);
520d32879a4SDamjan Jovanovic                 break;
521d32879a4SDamjan Jovanovic             case CONSTANT_MethodType:
522d32879a4SDamjan Jovanovic                 skip_bytes(&file, 2);
523d32879a4SDamjan Jovanovic                 break;
524d32879a4SDamjan Jovanovic             case CONSTANT_InvokeDynamic:
525d32879a4SDamjan Jovanovic                 skip_bytes(&file, 4);
526d32879a4SDamjan Jovanovic                 break;
527cdf0e10cSrcweir             default:
528cdf0e10cSrcweir                 /* Unknown Constant_pool entry, this means we are
529cdf0e10cSrcweir                  * in trouble
530cdf0e10cSrcweir                  */
531cdf0e10cSrcweir                 err_quit("corrupted class file\n");
532cdf0e10cSrcweir                 break;
533cdf0e10cSrcweir 
534cdf0e10cSrcweir         }
535cdf0e10cSrcweir     }
536cdf0e10cSrcweir 
537cdf0e10cSrcweir     fclose(file.pfs);
538cdf0e10cSrcweir 
539cdf0e10cSrcweir     pdepen = allocate_growable();
540cdf0e10cSrcweir 
541cdf0e10cSrcweir     for (i = 0; i < nclass_cnt; i++) {
542cdf0e10cSrcweir         char *pstr, *ptmpstr;
543cdf0e10cSrcweir         pstr = ptmpstr = utf8tolatin1(pc_pool[pc_class[i]]);
544cdf0e10cSrcweir         /* we are not interested in inner classes */
545cdf0e10cSrcweir         if ( is_inner(pstr) ) {
546cdf0e10cSrcweir             free(pstr);
547cdf0e10cSrcweir             pstr = NULL;
548cdf0e10cSrcweir             continue;
549cdf0e10cSrcweir         }
550cdf0e10cSrcweir         /* strip off evt. array indicators */
551cdf0e10cSrcweir         if ( *ptmpstr == '[' ) {
552cdf0e10cSrcweir             while ( *ptmpstr == '[' )
553cdf0e10cSrcweir                 ptmpstr++;
554cdf0e10cSrcweir             /* we only interested in obj. arrays, which are marked with 'L' */
555cdf0e10cSrcweir             if ( *ptmpstr == 'L' ) {
556cdf0e10cSrcweir                 char *p = pstr;
557cdf0e10cSrcweir                 pstr = strdup(++ptmpstr);
558cdf0e10cSrcweir                 /* remove final ';' from object array name */
559cdf0e10cSrcweir                 pstr[strlen(pstr)-1] = '\0';
560cdf0e10cSrcweir                 free(p);
561cdf0e10cSrcweir             } else {
562cdf0e10cSrcweir                 free(pstr);
563cdf0e10cSrcweir                 pstr = NULL;
564cdf0e10cSrcweir             }
565cdf0e10cSrcweir         }
566cdf0e10cSrcweir 
567cdf0e10cSrcweir         if (pstr) {
568cdf0e10cSrcweir             add_to_dependencies(pdepen, pfilt, pstr, file.pname);
569cdf0e10cSrcweir             free(pstr);
570cdf0e10cSrcweir         }
571cdf0e10cSrcweir     }
572cdf0e10cSrcweir 
573cdf0e10cSrcweir     print_dependencies(pdepen, file.pname);
574cdf0e10cSrcweir     free_growable(pdepen);
575cdf0e10cSrcweir     pdepen = NULL;
576cdf0e10cSrcweir 
577cdf0e10cSrcweir     for (i = 0; i < ncnt; i++)
578cdf0e10cSrcweir         free(pc_pool[i].pdata);
579cdf0e10cSrcweir 
580cdf0e10cSrcweir     free(pc_class);
581cdf0e10cSrcweir     free(pc_pool);
582cdf0e10cSrcweir }
583cdf0e10cSrcweir 
584cdf0e10cSrcweir void *
585cdf0e10cSrcweir xmalloc(size_t size)
586cdf0e10cSrcweir {
587cdf0e10cSrcweir     void *ptr;
588cdf0e10cSrcweir 
589cdf0e10cSrcweir     ptr = malloc(size);
590cdf0e10cSrcweir 
591cdf0e10cSrcweir     if ( !ptr )
592cdf0e10cSrcweir         err_quit("out of memory");
593cdf0e10cSrcweir 
594cdf0e10cSrcweir     return ptr;
595cdf0e10cSrcweir }
596cdf0e10cSrcweir 
597cdf0e10cSrcweir 
598cdf0e10cSrcweir void *
599cdf0e10cSrcweir xcalloc(size_t nmemb, size_t size)
600cdf0e10cSrcweir {
601cdf0e10cSrcweir     void *ptr;
602cdf0e10cSrcweir 
603cdf0e10cSrcweir     ptr = calloc(nmemb, size);
604cdf0e10cSrcweir 
605cdf0e10cSrcweir     if ( !ptr )
606cdf0e10cSrcweir         err_quit("out of memory");
607cdf0e10cSrcweir 
608cdf0e10cSrcweir     return ptr;
609cdf0e10cSrcweir }
610cdf0e10cSrcweir 
611cdf0e10cSrcweir void *
612cdf0e10cSrcweir xrealloc(void *ptr, size_t size)
613cdf0e10cSrcweir {
614cdf0e10cSrcweir     ptr = realloc(ptr, size);
615cdf0e10cSrcweir 
616cdf0e10cSrcweir     if ( !ptr )
617cdf0e10cSrcweir         err_quit("out of memory");
618cdf0e10cSrcweir 
619cdf0e10cSrcweir     return ptr;
620cdf0e10cSrcweir }
621cdf0e10cSrcweir 
622cdf0e10cSrcweir void
623cdf0e10cSrcweir err_quit(const char* fmt, ...)
624cdf0e10cSrcweir {
625cdf0e10cSrcweir     /* No dependency file must be generated for any error condition,
626cdf0e10cSrcweir      * just print message and exit.
627cdf0e10cSrcweir      */
628cdf0e10cSrcweir     va_list args;
629cdf0e10cSrcweir     char buffer[PATH_MAX];
630cdf0e10cSrcweir 
631cdf0e10cSrcweir     va_start(args, fmt);
632cdf0e10cSrcweir 
633cdf0e10cSrcweir     if ( pprogname )
634cdf0e10cSrcweir         fprintf(stderr, "%s: ", pprogname);
635cdf0e10cSrcweir     vsnprintf(buffer, sizeof(buffer), fmt, args);
636cdf0e10cSrcweir     fputs(buffer, stderr);
637cdf0e10cSrcweir     fputc('\n', stderr);
638cdf0e10cSrcweir 
639cdf0e10cSrcweir     va_end(args);
640cdf0e10cSrcweir 
641cdf0e10cSrcweir     /* clean up */
642cdf0e10cSrcweir     if ( pfsout && pfsout != stdout ) {
643cdf0e10cSrcweir         fclose(pfsout);
644cdf0e10cSrcweir         unlink(pout_file);
645cdf0e10cSrcweir     }
646cdf0e10cSrcweir     exit(1);
647cdf0e10cSrcweir }
648cdf0e10cSrcweir 
649cdf0e10cSrcweir void
650cdf0e10cSrcweir silent_quit()
651cdf0e10cSrcweir {
652cdf0e10cSrcweir     /* In some cases we should just do a silent exit */
653cdf0e10cSrcweir 
654cdf0e10cSrcweir     /* clean up */
655cdf0e10cSrcweir     if ( pfsout && pfsout != stdout ) {
656cdf0e10cSrcweir         fclose(pfsout);
657cdf0e10cSrcweir         unlink(pout_file);
658cdf0e10cSrcweir     }
659cdf0e10cSrcweir     exit(0);
660cdf0e10cSrcweir }
661cdf0e10cSrcweir 
662cdf0e10cSrcweir int append_to_growable(struct growable *pgrow, char *pstr)
663cdf0e10cSrcweir {
664cdf0e10cSrcweir     /* append an element pstr to pgrow,
665cdf0e10cSrcweir      * return new number of elements
666cdf0e10cSrcweir      */
667cdf0e10cSrcweir     grow_if_needed(pgrow);
668cdf0e10cSrcweir     pgrow->parray[pgrow->ncur++] = pstr;
669cdf0e10cSrcweir     return pgrow->ncur;
670cdf0e10cSrcweir }
671cdf0e10cSrcweir 
672cdf0e10cSrcweir void
673cdf0e10cSrcweir grow_if_needed(struct growable *pgrow)
674cdf0e10cSrcweir {
675cdf0e10cSrcweir     /* grow growable arrays */
676cdf0e10cSrcweir 
677cdf0e10cSrcweir     if ( pgrow->ncur >= pgrow->nmax ) {
678cdf0e10cSrcweir         pgrow->parray = xrealloc(pgrow->parray,
679cdf0e10cSrcweir                            (NGROW*pgrow->nmax)*sizeof(char*));
680cdf0e10cSrcweir         pgrow->nmax *= NGROW;
681cdf0e10cSrcweir     }
682cdf0e10cSrcweir     return;
683cdf0e10cSrcweir }
684cdf0e10cSrcweir 
685cdf0e10cSrcweir struct growable *allocate_growable(void)
686cdf0e10cSrcweir {
687cdf0e10cSrcweir     /* allocate an growable array,
688cdf0e10cSrcweir      * initialize with NGROW_INIT elements
689cdf0e10cSrcweir      */
690cdf0e10cSrcweir 
691cdf0e10cSrcweir     struct growable *pgrow;
692cdf0e10cSrcweir 
693cdf0e10cSrcweir     pgrow = xmalloc(sizeof(struct growable));
694cdf0e10cSrcweir     pgrow->parray = xmalloc(NGROW_INIT*sizeof(char *));
695cdf0e10cSrcweir     pgrow->nmax = NGROW_INIT;
696cdf0e10cSrcweir     pgrow->ncur = 0;
697cdf0e10cSrcweir     return pgrow;
698cdf0e10cSrcweir }
699cdf0e10cSrcweir 
700cdf0e10cSrcweir void
701cdf0e10cSrcweir free_growable(struct growable *pgrow)
702cdf0e10cSrcweir {
703cdf0e10cSrcweir     int i;
704cdf0e10cSrcweir     for( i = 0; i < pgrow->ncur; i++ )
705cdf0e10cSrcweir         free(pgrow->parray[i]);
706cdf0e10cSrcweir     free(pgrow->parray);
707cdf0e10cSrcweir     free(pgrow);
708cdf0e10cSrcweir }
709cdf0e10cSrcweir 
710cdf0e10cSrcweir void
711cdf0e10cSrcweir create_filters(struct growable *pfilt, const struct growable *pinc)
712cdf0e10cSrcweir {
713cdf0e10cSrcweir     char *p, *pp, *pstr;
714cdf0e10cSrcweir     int i, nlen, nlen_pstr;
715cdf0e10cSrcweir     /* break up includes into filter list */
716cdf0e10cSrcweir     for ( i = 0; i < pinc->ncur; i++ ) {
717cdf0e10cSrcweir         pp = pinc->parray[i];
718cdf0e10cSrcweir 
719cdf0e10cSrcweir         while ( (p = strchr(pp, csep)) != NULL) {
720cdf0e10cSrcweir             nlen = p - pp;
721cdf0e10cSrcweir             pstr = xmalloc((nlen+1)*sizeof(char*));
722cdf0e10cSrcweir             memcpy(pstr, pp, nlen);
723cdf0e10cSrcweir             pstr[nlen] = '\0';
724cdf0e10cSrcweir             append_to_growable(pfilt, pstr);
725cdf0e10cSrcweir             pp = p + 1;
726cdf0e10cSrcweir         }
727cdf0e10cSrcweir         nlen_pstr = strlen(pp);
728cdf0e10cSrcweir         pstr = xmalloc((nlen_pstr+1)*sizeof(char*));
729cdf0e10cSrcweir         memcpy(pstr, pp, nlen_pstr+1);
730cdf0e10cSrcweir         append_to_growable(pfilt, pstr);
731cdf0e10cSrcweir     }
732cdf0e10cSrcweir 
733cdf0e10cSrcweir }
734cdf0e10cSrcweir 
735cdf0e10cSrcweir void
736cdf0e10cSrcweir usage()
737cdf0e10cSrcweir {
738cdf0e10cSrcweir     fprintf(stderr,
739cdf0e10cSrcweir             "usage: %s [-i|-I includepath ...  -s|-S seperator "
740cdf0e10cSrcweir             "-o|-O outpath -v|-V -h|-H] <file> ....\n",
741cdf0e10cSrcweir             pprogname);
742cdf0e10cSrcweir }
743cdf0e10cSrcweir 
744cdf0e10cSrcweir #ifdef WNT
745cdf0e10cSrcweir /* my very simple minded implementation of getopt()
746cdf0e10cSrcweir  * it's to sad that getopt() is not available everywhere
747cdf0e10cSrcweir  * note: this is not a full POSIX conforming getopt()
748cdf0e10cSrcweir  */
749cdf0e10cSrcweir int simple_getopt(char *pargv[], const char *poptstring)
750cdf0e10cSrcweir {
751cdf0e10cSrcweir     char *parg = pargv[optind];
752cdf0e10cSrcweir 
753cdf0e10cSrcweir     /* skip all response file arguments */
754cdf0e10cSrcweir     if ( parg ) {
755cdf0e10cSrcweir         while ( *parg == '@' )
756cdf0e10cSrcweir             parg = pargv[++optind];
757cdf0e10cSrcweir 
758cdf0e10cSrcweir         if ( parg[0] == '-' && parg[1] != '\0' ) {
759cdf0e10cSrcweir             char *popt;
760cdf0e10cSrcweir             int c = parg[1];
761cdf0e10cSrcweir             if ( (popt = strchr(poptstring, c)) == NULL ) {
762cdf0e10cSrcweir                optopt = c;
763cdf0e10cSrcweir                 if ( opterr )
764cdf0e10cSrcweir                     fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
765cdf0e10cSrcweir                 return '?';
766cdf0e10cSrcweir             }
767cdf0e10cSrcweir             if ( *(++popt) == ':') {
768cdf0e10cSrcweir                 if ( parg[2] != '\0' ) {
769cdf0e10cSrcweir                     optarg = ++parg;
770cdf0e10cSrcweir                 } else {
771cdf0e10cSrcweir                     optarg = pargv[++optind];
772cdf0e10cSrcweir                 }
773cdf0e10cSrcweir             } else {
774cdf0e10cSrcweir                 optarg = NULL;
775cdf0e10cSrcweir             }
776cdf0e10cSrcweir             ++optind;
777cdf0e10cSrcweir             return c;
778cdf0e10cSrcweir         }
779cdf0e10cSrcweir     }
780cdf0e10cSrcweir     return -1;
781cdf0e10cSrcweir }
782cdf0e10cSrcweir #endif
783cdf0e10cSrcweir 
784cdf0e10cSrcweir int CDECL
785cdf0e10cSrcweir main(int argc, char *argv[])
786cdf0e10cSrcweir {
787cdf0e10cSrcweir     int     bv_flag = 0;
788cdf0e10cSrcweir     struct  growable *presp, *pincs, *pfilters;
789cdf0e10cSrcweir     int     c, i, nall_argc;
790cdf0e10cSrcweir     char    **pall_argv;
791cdf0e10cSrcweir 
792cdf0e10cSrcweir     presp = allocate_growable();
793cdf0e10cSrcweir 
794cdf0e10cSrcweir     /* FIXME: cleanup the option parsing */
795cdf0e10cSrcweir     /* search for response file, read it */
796cdf0e10cSrcweir     for ( i = 1; i < argc; i++ ) {
797cdf0e10cSrcweir         char *parg = argv[i];
798cdf0e10cSrcweir         char buffer[RES_FILE_BUF];
799cdf0e10cSrcweir 
800cdf0e10cSrcweir         if ( *parg == '@' ) {
801cdf0e10cSrcweir             FILE *pfile = fopen(++parg, "r");
802cdf0e10cSrcweir             if ( !pfile )
803cdf0e10cSrcweir                 err_quit("%s: %s", parg, strerror(errno));
804cdf0e10cSrcweir             while ( !feof(pfile) ) {
805cdf0e10cSrcweir                 char *p, *token;
806cdf0e10cSrcweir 
807cdf0e10cSrcweir                 if ( fgets(buffer, RES_FILE_BUF, pfile) ) {;
808cdf0e10cSrcweir                     p = buffer;
809cdf0e10cSrcweir                     while ( (token = strtok(p, " \t\n")) != NULL ) {
810cdf0e10cSrcweir                         p = NULL;
811cdf0e10cSrcweir                         append_to_growable(presp, strdup(token));
812cdf0e10cSrcweir                     }
813cdf0e10cSrcweir                 }
814cdf0e10cSrcweir             }
815cdf0e10cSrcweir             fclose(pfile);
816cdf0e10cSrcweir         }
817cdf0e10cSrcweir     }
818cdf0e10cSrcweir 
819cdf0e10cSrcweir     /* copy all arguments incl. response file in one array
820cdf0e10cSrcweir      * for parsing with getopt
821cdf0e10cSrcweir      */
822cdf0e10cSrcweir     nall_argc = argc + presp->ncur;
823cdf0e10cSrcweir     pall_argv = xmalloc((nall_argc+1)*sizeof(char *));
824cdf0e10cSrcweir     memcpy(pall_argv, argv, argc*sizeof(char *));
825cdf0e10cSrcweir     memcpy(pall_argv+argc, presp->parray, presp->ncur*sizeof(char *));
826cdf0e10cSrcweir     *(pall_argv+argc+presp->ncur) = '\0'; /* terminate */
827cdf0e10cSrcweir 
828cdf0e10cSrcweir     opterr = 0;
829cdf0e10cSrcweir     pincs = allocate_growable();
830cdf0e10cSrcweir 
831cdf0e10cSrcweir #ifdef WNT
832cdf0e10cSrcweir     while( (c = simple_getopt(pall_argv, ":i:I:s:S:o:OhHvV")) != -1 ) {
833cdf0e10cSrcweir #else
834cdf0e10cSrcweir     while( (c = getopt(nall_argc, pall_argv, ":i:I:s:S:o:OhHvV")) != -1 ) {
835cdf0e10cSrcweir #endif
836cdf0e10cSrcweir         switch(c) {
837cdf0e10cSrcweir             case 'i':
838cdf0e10cSrcweir             case 'I':
839cdf0e10cSrcweir                 append_to_growable(pincs, strdup(optarg));
840cdf0e10cSrcweir                 break;
841cdf0e10cSrcweir             case 's':
842cdf0e10cSrcweir             case 'S':
843cdf0e10cSrcweir                 csep = optarg[0];
844cdf0e10cSrcweir                 break;
845cdf0e10cSrcweir             case 'o':
846cdf0e10cSrcweir             case 'O':
847cdf0e10cSrcweir                 pout_file = optarg;
848cdf0e10cSrcweir                 break;
849cdf0e10cSrcweir             case 'h':
850cdf0e10cSrcweir             case 'H':
851cdf0e10cSrcweir                 usage();
852cdf0e10cSrcweir                 return 0;
853cdf0e10cSrcweir                 break;
854cdf0e10cSrcweir             case 'v':
855cdf0e10cSrcweir             case 'V':
856cdf0e10cSrcweir                 bv_flag = 1;
857cdf0e10cSrcweir                 break;
858cdf0e10cSrcweir             case '?':
859cdf0e10cSrcweir                 if (isprint (optopt))
860cdf0e10cSrcweir                     fprintf (stderr,
861cdf0e10cSrcweir                              "Unknown option `-%c'.\n", optopt);
862cdf0e10cSrcweir                 else
863cdf0e10cSrcweir                     fprintf (stderr,
864cdf0e10cSrcweir                              "Unknown option character `\\x%x'.\n",
865cdf0e10cSrcweir                              optopt);
866cdf0e10cSrcweir                 usage();
867cdf0e10cSrcweir                 return 1;
868cdf0e10cSrcweir                 break;
869cdf0e10cSrcweir             case ':':
870cdf0e10cSrcweir                 fprintf(stderr, "Missing parameter.\n");
871cdf0e10cSrcweir                 usage();
872cdf0e10cSrcweir                 return 1;
873cdf0e10cSrcweir                 break;
874cdf0e10cSrcweir             default:
875cdf0e10cSrcweir                 usage();
876cdf0e10cSrcweir                 return 1;
877cdf0e10cSrcweir                 break;
878cdf0e10cSrcweir         }
879cdf0e10cSrcweir     }
880cdf0e10cSrcweir 
881cdf0e10cSrcweir     pfilters = allocate_growable();
882cdf0e10cSrcweir     create_filters(pfilters, pincs);
883cdf0e10cSrcweir     free_growable(pincs);
884cdf0e10cSrcweir     pincs = NULL;
885cdf0e10cSrcweir 
886cdf0e10cSrcweir     if ( pout_file ) {
887cdf0e10cSrcweir         pfsout = fopen(pout_file, "w");
888cdf0e10cSrcweir         if ( !pfsout )
889cdf0e10cSrcweir             err_quit("%s: %s", pout_file, strerror(errno));
890cdf0e10cSrcweir     } else {
891cdf0e10cSrcweir         pfsout = stdout;
892cdf0e10cSrcweir     }
893cdf0e10cSrcweir 
894cdf0e10cSrcweir     /* the remaining arguments are either class file
895cdf0e10cSrcweir      * names or response files, ignore response file
896cdf0e10cSrcweir      * since they have already been included
897cdf0e10cSrcweir      */
898cdf0e10cSrcweir     for ( i = optind; i < nall_argc; i++ ) {
899cdf0e10cSrcweir         char *parg = pall_argv[i];
900cdf0e10cSrcweir         if ( *parg != '@' ) {
901cdf0e10cSrcweir             process_class_file(parg, pfilters);
902cdf0e10cSrcweir             if ( pfsout != stdout ) {
903cdf0e10cSrcweir                 if ( bv_flag )
904cdf0e10cSrcweir                     printf("Processed %s ...\n", parg);
905cdf0e10cSrcweir             }
906cdf0e10cSrcweir         }
907cdf0e10cSrcweir     }
908cdf0e10cSrcweir 
909cdf0e10cSrcweir     free_growable(pfilters);
910cdf0e10cSrcweir     pfilters = NULL;
911cdf0e10cSrcweir     free(pall_argv);
912cdf0e10cSrcweir     pall_argv = NULL;
913cdf0e10cSrcweir     free_growable(presp);
914cdf0e10cSrcweir     presp = NULL;
915cdf0e10cSrcweir 
916cdf0e10cSrcweir     fclose(pfsout);
917cdf0e10cSrcweir     exit(0);
918cdf0e10cSrcweir }
919cdf0e10cSrcweir 
920