OSDN Git Service

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