FSF GCC merge 02/23/03
[official-gcc.git] / gcc / java / jv-scan.c
blobe1bc5729345a08f1566e0a08303acb2b5b0d2518
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)
6 This file is part of GCC.
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.
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.
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. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
28 #include "obstack.h" /* We use obstacks in lex.c */
30 #include "version.h"
32 #ifdef HAVE_LOCALE_H
33 #include <locale.h>
34 #endif
36 #ifdef HAVE_LANGINFO_CODESET
37 #include <langinfo.h>
38 #endif
40 #include <getopt.h>
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 gcc_obstack_init (struct obstack *obstack);
46 void report (void);
48 static void usage (void) ATTRIBUTE_NORETURN;
49 static void help (void) ATTRIBUTE_NORETURN;
50 static void version (void) ATTRIBUTE_NORETURN;
52 #define JC1_LITE
53 #include "jcf.h"
54 #include "parse.h"
56 /* Current input file and output file IO streams. */
57 FILE *finput, *out;
59 /* Current input filename. */
60 char *input_filename;
62 /* Executable name. */
63 char *exec_name;
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;
72 int pedantic = 0;
76 /* This is used to mark options with no short value. */
77 #define LONG_OPT(Num) ((Num) + 128)
79 #define OPT_HELP LONG_OPT (0)
80 #define OPT_VERSION LONG_OPT (1)
81 #define OPT_ENCODING LONG_OPT (2)
83 static const struct option options[] =
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 }
97 static void
98 usage (void)
100 fprintf (stderr, "Try `jv-scan --help' for more information.\n");
101 exit (1);
104 static void
105 help (void)
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 printf ("%s.\n", bug_report_url);
122 exit (0);
125 static void
126 version (void)
128 printf ("jv-scan (GCC) %s\n\n", version_string);
129 printf ("Copyright (C) 2002 Free Software Foundation, Inc.\n");
130 printf ("This is free software; see the source for copying conditions. There is NO\n");
131 printf ("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
132 exit (0);
135 /* jc1-lite main entry point */
137 main (int argc, char **argv)
139 int i = 1;
140 const char *output_file = NULL;
141 const char *encoding = NULL;
142 long ft;
143 int opt;
145 exec_name = argv[0];
147 /* Default for output */
148 out = stdout;
150 /* Process options first. We use getopt_long and not
151 getopt_long_only because we only support `--' long options here. */
152 while ((opt = getopt_long (argc, argv, "o:", options, NULL)) != -1)
154 switch (opt)
156 case 0:
157 /* Already handled. */
158 break;
160 case 'o':
161 output_file = optarg;
162 break;
164 case OPT_HELP:
165 help ();
166 break;
168 case OPT_VERSION:
169 version ();
170 break;
172 case OPT_ENCODING:
173 encoding = optarg;
174 break;
176 default:
177 usage ();
178 break;
182 /* No flags? Do nothing */
183 if (! flag_find_main && ! flag_dump_class && ! flag_complexity)
184 return 0;
186 /* Check on bad usage */
187 if (flag_find_main + flag_dump_class + flag_complexity > 1)
188 fatal_error
189 ("only one of `--print-main', `--list-class', and `--complexity' allowed");
191 if (output_file && !(out = fopen (output_file, "w")))
192 fatal_error ("can't open output file `%s'", output_file);
194 ft = ftell (out);
196 gcc_obstack_init (&temporary_obstack);
197 java_push_parser_context ();
199 for ( i = optind; i < argc; i++ )
200 if (argv [i])
202 input_filename = argv [i];
203 if ( (finput = fopen (argv [i], "r")) )
205 /* There's no point in trying to find the current encoding
206 unless we are going to do something intelligent with it
207 -- hence the test for iconv. */
208 #if defined (HAVE_LOCALE_H) && defined (HAVE_ICONV) && defined (HAVE_LANGINFO_CODESET)
209 setlocale (LC_CTYPE, "");
210 if (encoding == NULL)
211 encoding = nl_langinfo (CODESET);
212 #endif
213 if (encoding == NULL || *encoding == '\0')
214 encoding = DEFAULT_ENCODING;
216 java_init_lex (finput, encoding);
217 yyparse ();
218 report ();
219 if (ftell (out) != ft)
220 fputc ('\n', out);
221 ft = ftell (out);
222 fclose (finput);
223 reset_report ();
225 else
226 fatal_error ("file not found `%s'", argv [i]);
229 /* Flush and close */
230 if (ftell (out) != ft)
231 fputc ('\n', out);
232 if (!output_file)
233 fclose (out);
235 return 0;
240 /* Error report, memory, obstack initialization and other utility
241 functions */
243 void
244 fatal_error (const char *s, ...)
246 va_list ap;
247 va_start (ap, s);
248 fprintf (stderr, "%s: error: ", exec_name);
249 vfprintf (stderr, s, ap);
250 fputc ('\n', stderr);
251 va_end (ap);
252 exit (1);
255 void
256 warning (const char *s, ...)
258 va_list ap;
259 va_start (ap, s);
260 fprintf (stderr, "%s: warning: ", exec_name);
261 vfprintf (stderr, s, ap);
262 fputc ('\n', stderr);
263 va_end (ap);
266 void
267 gcc_obstack_init (struct obstack *obstack)
269 /* Let particular systems override the size of a chunk. */
270 #ifndef OBSTACK_CHUNK_SIZE
271 #define OBSTACK_CHUNK_SIZE 0
272 #endif
273 /* Let them override the alloc and free routines too. */
274 #ifndef OBSTACK_CHUNK_ALLOC
275 #define OBSTACK_CHUNK_ALLOC xmalloc
276 #endif
277 #ifndef OBSTACK_CHUNK_FREE
278 #define OBSTACK_CHUNK_FREE free
279 #endif
280 _obstack_begin (obstack, OBSTACK_CHUNK_SIZE, 0,
281 (void *(*) (long)) OBSTACK_CHUNK_ALLOC,
282 (void (*) (void *)) OBSTACK_CHUNK_FREE);