OSDN Git Service

* Makefile.in (BUILD_ERRORS): Set to build-errors.
[pf3gnuchains/gcc-fork.git] / gcc / java / jv-scan.c
1 /* Main for jv-scan
2    Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004
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 #include "intl.h"
28
29 #include "obstack.h"            /* We use obstacks in lex.c */
30
31 #include "version.h"
32
33 #ifdef HAVE_LOCALE_H
34 #include <locale.h>
35 #endif
36
37 #ifdef HAVE_LANGINFO_CODESET
38 #include <langinfo.h>
39 #endif
40
41 #include <getopt.h>
42
43 extern void fatal_error (const char *msgid, ...)
44      ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN;
45 void warning (const char *msgid, ...) ATTRIBUTE_PRINTF_1;
46 void report (void);
47
48 static void usage (void) ATTRIBUTE_NORETURN;
49 static void help (void) ATTRIBUTE_NORETURN;
50 static void version (void) ATTRIBUTE_NORETURN;
51
52 #define JC1_LITE
53 #include "jcf.h"
54 #include "parse.h"
55
56 /* Current input file and output file IO streams.  */
57 FILE *finput, *out;
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 int flag_assert = 1;
68
69 int pedantic = 0;
70
71 \f
72
73 /* This is used to mark options with no short value.  */
74 #define LONG_OPT(Num)  ((Num) + 128)
75
76 #define OPT_HELP      LONG_OPT (0)
77 #define OPT_VERSION   LONG_OPT (1)
78 #define OPT_ENCODING  LONG_OPT (2)
79
80 static const struct option options[] =
81 {
82   { "help",      no_argument,       NULL, OPT_HELP },
83   { "version",   no_argument,       NULL, OPT_VERSION },
84   { "print-main", no_argument,      &flag_find_main, 1 },
85   { "list-filename", no_argument,   &flag_list_filename, 1 },
86   { "list-class", no_argument,      &flag_dump_class, 1 },
87   { "encoding",  required_argument, NULL, OPT_ENCODING },
88   { "complexity", no_argument,      &flag_complexity, 1 },
89   { "no-assert", no_argument,       &flag_assert, 0 },
90   { "assert",    no_argument,       &flag_assert, 1 },
91   { NULL,        no_argument,       NULL, 0 }
92 };
93
94 static void
95 usage (void)
96 {
97   fprintf (stderr, _("Try `jv-scan --help' for more information.\n"));
98   exit (1);
99 }
100
101 static void
102 help (void)
103 {
104   printf (_("Usage: jv-scan [OPTION]... FILE...\n\n"));
105   printf (_("Print useful information read from Java source files.\n\n"));
106   printf (_("  --no-assert             Don't recognize the assert keyword\n"));
107   printf (_("  --complexity            Print cyclomatic complexity of input file\n"));
108   printf (_("  --encoding NAME         Specify encoding of input file\n"));
109   printf (_("  --print-main            Print name of class containing `main'\n"));
110   printf (_("  --list-class            List all classes defined in file\n"));
111   printf (_("  --list-filename         Print input filename when listing class names\n"));
112   printf (_("  -o FILE                 Set output file name\n"));
113   printf ("\n");
114   printf (_("  --help                  Print this help, then exit\n"));
115   printf (_("  --version               Print version number, then exit\n"));
116   printf ("\n");
117   printf (_("For bug reporting instructions, please see:\n"
118             "%s.\n"), bug_report_url);
119   exit (0);
120 }
121
122 static void
123 version (void)
124 {
125   printf ("jv-scan (GCC) %s\n\n", version_string);
126   printf ("Copyright %s 2004 Free Software Foundation, Inc.\n", _("(C)"));
127   printf (_("This is free software; see the source for copying conditions.  There is NO\n"
128             "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"));
129   exit (0);
130 }
131
132 /* jc1-lite main entry point */
133 int
134 main (int argc, char **argv)
135 {
136   int i = 1;
137   const char *output_file = NULL;
138   const char *encoding = NULL;
139   long ft;
140   int opt;
141
142   exec_name = argv[0];
143
144   /* Default for output */
145   out = stdout;
146
147   gcc_init_libintl ();
148
149   /* Process options first.  We use getopt_long and not
150      getopt_long_only because we only support `--' long options here.  */
151   while ((opt = getopt_long (argc, argv, "o:", options, NULL)) != -1)
152     {
153       switch (opt)
154         {
155         case 0:
156           /* Already handled.  */
157           break;
158
159         case 'o':
160           output_file = optarg;
161           break;
162
163         case OPT_HELP:
164           help ();
165           break;
166
167         case OPT_VERSION:
168           version ();
169           break;
170
171         case OPT_ENCODING:
172           encoding = optarg;
173           break;
174
175         default:
176           usage ();
177           break;
178         }
179     }
180
181   /* No flags? Do nothing */
182   if (! flag_find_main && ! flag_dump_class && ! flag_complexity)
183     return 0;
184
185   /* Check on bad usage */
186   if (flag_find_main + flag_dump_class + flag_complexity > 1)
187     fatal_error
188       ("only one of `--print-main', `--list-class', and `--complexity' allowed");
189
190   if (output_file && !(out = fopen (output_file, "w")))
191     fatal_error ("can't open output file `%s'", output_file);
192
193   ft = ftell (out);
194
195   gcc_obstack_init (&temporary_obstack);
196   java_push_parser_context ();
197
198   for ( i = optind; i < argc; i++ )
199     if (argv [i])
200       {
201         input_filename = argv [i];
202         if ( (finput = fopen (argv [i], "r")) )
203           {
204             /* There's no point in trying to find the current encoding
205                unless we are going to do something intelligent with it
206                -- hence the test for iconv.  */
207 #if defined (HAVE_LOCALE_H) && defined (HAVE_ICONV) && defined (HAVE_LANGINFO_CODESET)
208             setlocale (LC_CTYPE, "");
209             if (encoding == NULL)
210               encoding = nl_langinfo (CODESET);
211 #endif  
212             if (encoding == NULL || *encoding == '\0')
213               encoding = DEFAULT_ENCODING;
214
215             java_init_lex (finput, encoding);
216             yyparse ();
217             report ();
218             if (ftell (out) != ft)
219               fputc ('\n', out);
220             ft = ftell (out);
221             fclose (finput);
222             reset_report ();
223           }
224         else
225           fatal_error ("file not found `%s'", argv [i]);
226       }
227
228   /* Flush and close */
229   if (ftell (out) != ft)
230     fputc ('\n', out);
231   if (!output_file)
232     fclose (out);
233
234   return 0;
235 }
236
237 \f
238
239 /* Error report, memory, obstack initialization and other utility
240    functions */
241
242 void
243 fatal_error (const char *msgid, ...)
244 {
245   va_list ap;
246   va_start (ap, msgid);
247   fprintf (stderr, _("%s: error: "), exec_name);
248   vfprintf (stderr, _(msgid), ap);
249   fputc ('\n', stderr);
250   va_end (ap);
251   exit (1);
252 }
253
254 void
255 warning (const char *msgid, ...)
256 {
257   va_list ap;
258   va_start (ap, msgid);
259   fprintf (stderr, _("%s: warning: "), exec_name);
260   vfprintf (stderr, _(msgid), ap);
261   fputc ('\n', stderr);
262   va_end (ap);
263 }
264
265 void
266 fancy_abort (const char *file, int line, const char *func)
267 {
268   fatal_error ("abort in %s, at %s:%d", func, file, line);
269 }