OSDN Git Service

* collect2.c (is_ctor_dtor): Const-ify.
[pf3gnuchains/gcc-fork.git] / gcc / java / jv-scan.c
1 /* Main for jv-scan
2    Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3    Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24
25 #include "obstack.h"            /* We use obstacks in lex.c */
26
27 #include "version.h"
28
29 #ifdef HAVE_LOCALE_H
30 #include <locale.h>
31 #endif
32
33 #ifdef HAVE_NL_LANGINFO
34 #include <langinfo.h>
35 #endif
36
37 #include <getopt.h>
38
39 extern void fatal_error PARAMS ((const char *s, ...))
40      ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN;
41 void warning PARAMS ((const char *s, ...)) ATTRIBUTE_PRINTF_1;
42 void gcc_obstack_init PARAMS ((struct obstack *obstack));
43 void report PARAMS ((void));
44
45 static void usage PARAMS ((void)) ATTRIBUTE_NORETURN;
46 static void help PARAMS ((void)) ATTRIBUTE_NORETURN;
47 static void version PARAMS ((void)) ATTRIBUTE_NORETURN;
48
49 #define JC1_LITE
50 #include "jcf.h"
51 #include "parse.h"
52
53 /* Current input file and output file IO streams.  */
54 FILE *finput, *out;
55
56 /* Current input filename.  */
57 char *input_filename;
58
59 /* Executable name.  */
60 char *exec_name;
61
62 /* Flags matching command line options.  */
63 int flag_find_main = 0;
64 int flag_dump_class = 0;
65 int flag_list_filename = 0;
66 int flag_complexity = 0;
67
68 int pedantic = 0;
69
70 \f
71
72 /* This is used to mark options with no short value.  */
73 #define LONG_OPT(Num)  ((Num) + 128)
74
75 #define OPT_HELP      LONG_OPT (0)
76 #define OPT_VERSION   LONG_OPT (1)
77 #define OPT_ENCODING  LONG_OPT (2)
78
79 static const struct option options[] =
80 {
81   { "help",      no_argument,       NULL, OPT_HELP },
82   { "version",   no_argument,       NULL, OPT_VERSION },
83   { "print-main", no_argument,      &flag_find_main, 1 },
84   { "list-filename", no_argument,   &flag_list_filename, 1 },
85   { "list-class", no_argument,      &flag_dump_class, 1 },
86   { "encoding",  required_argument, NULL, OPT_ENCODING },
87   { "complexity", no_argument,      &flag_complexity, 1 },
88   { NULL,        no_argument,       NULL, 0 }
89 };
90
91 static void
92 usage ()
93 {
94   fprintf (stderr, "Try `jv-scan --help' for more information.\n");
95   exit (1);
96 }
97
98 static void
99 help ()
100 {
101   printf ("Usage: jv-scan [OPTION]... FILE...\n\n");
102   printf ("Print useful information read from Java source files.\n\n");
103   printf ("  --complexity            Print cyclomatic complexity of input file\n");
104   printf ("  --encoding NAME         Specify encoding of input file\n");
105   printf ("  --print-main            Print name of class containing `main'\n");
106   printf ("  --list-class            List all classes defined in file\n");
107   printf ("  --list-filename         Print input filename when listing class names\n");
108   printf ("  -o FILE                 Set output file name\n");
109   printf ("\n");
110   printf ("  --help                  Print this help, then exit\n");
111   printf ("  --version               Print version number, then exit\n");
112   printf ("\n");
113   printf ("For bug reporting instructions, please see:\n");
114   printf ("%s.\n", GCCBUGURL);
115   exit (0);
116 }
117
118 static void
119 version ()
120 {
121   printf ("jv-scan (%s)\n\n", version_string);
122   printf ("Copyright (C) 2001 Free Software Foundation, Inc.\n");
123   printf ("This is free software; see the source for copying conditions.  There is NO\n");
124   printf ("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
125   exit (0);
126 }
127
128 /* jc1-lite main entry point */
129 int
130 DEFUN (main, (argc, argv),
131        int argc AND char **argv)
132 {
133   int i = 1;
134   const char *output_file = NULL;
135   const char *encoding = NULL;
136   long ft;
137   int opt;
138
139   exec_name = argv[0];
140
141   /* Default for output */
142   out = stdout;
143
144   /* Process options first.  We use getopt_long and not
145      getopt_long_only because we only support `--' long options here.  */
146   while ((opt = getopt_long (argc, argv, "o:", options, NULL)) != -1)
147     {
148       switch (opt)
149         {
150         case 0:
151           /* Already handled.  */
152           break;
153
154         case 'o':
155           output_file = optarg;
156           break;
157
158         case OPT_HELP:
159           help ();
160           break;
161
162         case OPT_VERSION:
163           version ();
164           break;
165
166         case OPT_ENCODING:
167           encoding = optarg;
168           break;
169
170         default:
171           usage ();
172           break;
173         }
174     }
175
176   /* No flags? Do nothing */
177   if (! flag_find_main && ! flag_dump_class && ! flag_complexity)
178     return 0;
179
180   /* Check on bad usage */
181   if (flag_find_main + flag_dump_class + flag_complexity > 1)
182     fatal_error
183       ("only one of `--print-main', `--list-class', and `--complexity' allowed");
184
185   if (output_file && !(out = fopen (output_file, "w")))
186     fatal_error ("can't open output file `%s'", output_file);
187
188   ft = ftell (out);
189
190   gcc_obstack_init (&temporary_obstack);
191   java_push_parser_context ();
192
193   for ( i = optind; i < argc; i++ )
194     if (argv [i])
195       {
196         input_filename = argv [i];
197         if ( (finput = fopen (argv [i], "r")) )
198           {
199             /* There's no point in trying to find the current encoding
200                unless we are going to do something intelligent with it
201                -- hence the test for iconv.  */
202 #ifdef HAVE_ICONV
203 #ifdef HAVE_NL_LANGINFO
204             setlocale (LC_CTYPE, "");
205             if (encoding == NULL)
206               encoding = nl_langinfo (CODESET);
207 #endif /* HAVE_NL_LANGINFO */
208 #endif /* HAVE_ICONV */
209             if (encoding == NULL || *encoding == '\0')
210               encoding = DEFAULT_ENCODING;
211
212             java_init_lex (finput, encoding);
213             yyparse ();
214             report ();
215             if (ftell (out) != ft)
216               fputc ('\n', out);
217             ft = ftell (out);
218             fclose (finput);
219             reset_report ();
220           }
221         else
222           fatal_error ("file not found `%s'", argv [i]);
223       }
224
225   /* Flush and close */
226   if (ftell (out) != ft)
227     fputc ('\n', out);
228   if (!output_file)
229     fclose (out);
230
231   return 0;
232 }
233
234 \f
235
236 /* Error report, memory, obstack initialization and other utility
237    functions */
238
239 void
240 fatal_error VPARAMS ((const char *s, ...))
241 {
242   VA_OPEN (ap, s);
243   VA_FIXEDARG (ap, const char *, s);
244
245   fprintf (stderr, "%s: error: ", exec_name);
246   vfprintf (stderr, s, ap);
247   fputc ('\n', stderr);
248   VA_CLOSE (ap);
249   exit (1);
250 }
251
252 void
253 warning VPARAMS ((const char *s, ...))
254 {
255   VA_OPEN (ap, s);
256   VA_FIXEDARG (ap, const char *, s);
257
258   fprintf (stderr, "%s: warning: ", exec_name);
259   vfprintf (stderr, s, ap);
260   fputc ('\n', stderr);
261   VA_CLOSE (ap);
262 }
263
264 void
265 gcc_obstack_init (obstack)
266      struct obstack *obstack;
267 {
268   /* Let particular systems override the size of a chunk.  */
269 #ifndef OBSTACK_CHUNK_SIZE
270 #define OBSTACK_CHUNK_SIZE 0
271 #endif
272   /* Let them override the alloc and free routines too.  */
273 #ifndef OBSTACK_CHUNK_ALLOC
274 #define OBSTACK_CHUNK_ALLOC xmalloc
275 #endif
276 #ifndef OBSTACK_CHUNK_FREE
277 #define OBSTACK_CHUNK_FREE free
278 #endif
279   _obstack_begin (obstack, OBSTACK_CHUNK_SIZE, 0,
280                   (void *(*) (long)) OBSTACK_CHUNK_ALLOC,
281                   (void (*) (void *)) OBSTACK_CHUNK_FREE);
282 }