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