OSDN Git Service

7c8d2d3e58e2355e07681ede7ae3fb73126bdaec
[pf3gnuchains/gcc-fork.git] / gcc / java / jv-scan.c
1 /* Main for jv-scan
2    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003
3    Free Software Foundation, Inc.
4    Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27
28 #include "obstack.h"            /* We use obstacks in lex.c */
29
30 #include "version.h"
31
32 #ifdef HAVE_LOCALE_H
33 #include <locale.h>
34 #endif
35
36 #ifdef HAVE_LANGINFO_CODESET
37 #include <langinfo.h>
38 #endif
39
40 #include <getopt.h>
41
42 extern void fatal_error (const char *s, ...)
43      ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN;
44 void warning (const char *s, ...) ATTRIBUTE_PRINTF_1;
45 void report (void);
46
47 static void usage (void) ATTRIBUTE_NORETURN;
48 static void help (void) ATTRIBUTE_NORETURN;
49 static void version (void) ATTRIBUTE_NORETURN;
50
51 #define JC1_LITE
52 #include "jcf.h"
53 #include "parse.h"
54
55 /* Current input file and output file IO streams.  */
56 FILE *finput, *out;
57
58 /* Executable name.  */
59 char *exec_name;
60
61 /* Flags matching command line options.  */
62 int flag_find_main = 0;
63 int flag_dump_class = 0;
64 int flag_list_filename = 0;
65 int flag_complexity = 0;
66 int flag_assert = 1;
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   { "no-assert", no_argument,       &flag_assert, 0 },
89   { "assert",    no_argument,       &flag_assert, 1 },
90   { NULL,        no_argument,       NULL, 0 }
91 };
92
93 static void
94 usage (void)
95 {
96   fprintf (stderr, "Try `jv-scan --help' for more information.\n");
97   exit (1);
98 }
99
100 static void
101 help (void)
102 {
103   printf ("Usage: jv-scan [OPTION]... FILE...\n\n");
104   printf ("Print useful information read from Java source files.\n\n");
105   printf ("  --no-assert             Don't recognize the assert keyword\n");
106   printf ("  --complexity            Print cyclomatic complexity of input file\n");
107   printf ("  --encoding NAME         Specify encoding of input file\n");
108   printf ("  --print-main            Print name of class containing `main'\n");
109   printf ("  --list-class            List all classes defined in file\n");
110   printf ("  --list-filename         Print input filename when listing class names\n");
111   printf ("  -o FILE                 Set output file name\n");
112   printf ("\n");
113   printf ("  --help                  Print this help, then exit\n");
114   printf ("  --version               Print version number, then exit\n");
115   printf ("\n");
116   printf ("For bug reporting instructions, please see:\n");
117   printf ("%s.\n", bug_report_url);
118   exit (0);
119 }
120
121 static void
122 version (void)
123 {
124   printf ("jv-scan (GCC) %s\n\n", version_string);
125   printf ("Copyright (C) 2002 Free Software Foundation, Inc.\n");
126   printf ("This is free software; see the source for copying conditions.  There is NO\n");
127   printf ("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
128   exit (0);
129 }
130
131 /* jc1-lite main entry point */
132 int
133 main (int argc, char **argv)
134 {
135   int i = 1;
136   const char *output_file = NULL;
137   const char *encoding = NULL;
138   long ft;
139   int opt;
140
141   exec_name = argv[0];
142
143   /* Default for output */
144   out = stdout;
145
146   /* Process options first.  We use getopt_long and not
147      getopt_long_only because we only support `--' long options here.  */
148   while ((opt = getopt_long (argc, argv, "o:", options, NULL)) != -1)
149     {
150       switch (opt)
151         {
152         case 0:
153           /* Already handled.  */
154           break;
155
156         case 'o':
157           output_file = optarg;
158           break;
159
160         case OPT_HELP:
161           help ();
162           break;
163
164         case OPT_VERSION:
165           version ();
166           break;
167
168         case OPT_ENCODING:
169           encoding = optarg;
170           break;
171
172         default:
173           usage ();
174           break;
175         }
176     }
177
178   /* No flags? Do nothing */
179   if (! flag_find_main && ! flag_dump_class && ! flag_complexity)
180     return 0;
181
182   /* Check on bad usage */
183   if (flag_find_main + flag_dump_class + flag_complexity > 1)
184     fatal_error
185       ("only one of `--print-main', `--list-class', and `--complexity' allowed");
186
187   if (output_file && !(out = fopen (output_file, "w")))
188     fatal_error ("can't open output file `%s'", output_file);
189
190   ft = ftell (out);
191
192   gcc_obstack_init (&temporary_obstack);
193   java_push_parser_context ();
194
195   for ( i = optind; i < argc; i++ )
196     if (argv [i])
197       {
198         input_filename = argv [i];
199         if ( (finput = fopen (argv [i], "r")) )
200           {
201             /* There's no point in trying to find the current encoding
202                unless we are going to do something intelligent with it
203                -- hence the test for iconv.  */
204 #if defined (HAVE_LOCALE_H) && defined (HAVE_ICONV) && defined (HAVE_LANGINFO_CODESET)
205             setlocale (LC_CTYPE, "");
206             if (encoding == NULL)
207               encoding = nl_langinfo (CODESET);
208 #endif  
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 (const char *s, ...)
241 {
242   va_list ap;
243   va_start (ap, s);
244   fprintf (stderr, "%s: error: ", exec_name);
245   vfprintf (stderr, s, ap);
246   fputc ('\n', stderr);
247   va_end (ap);
248   exit (1);
249 }
250
251 void
252 warning (const char *s, ...)
253 {
254   va_list ap;
255   va_start (ap, s);
256   fprintf (stderr, "%s: warning: ", exec_name);
257   vfprintf (stderr, s, ap);
258   fputc ('\n', stderr);
259   va_end (ap);
260 }