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