xref: /openbsd/gnu/usr.bin/gcc/gcc/java/jv-scan.c (revision c87b03e5)
1 /* Main for jv-scan
2    Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
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 
22 #include "config.h"
23 #include "system.h"
24 
25 #include "obstack.h"		/* We use obstacks in lex.c */
26 
27 #include "version.h"
28 
29 #ifdef HAVE_LOCALE_H
30 #include <locale.h>
31 #endif
32 
33 #ifdef HAVE_LANGINFO_CODESET
34 #include <langinfo.h>
35 #endif
36 
37 #include <getopt.h>
38 
39 extern void fatal_error PARAMS ((const char *s, ...))
40      ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN;
41 void warning PARAMS ((const char *s, ...)) ATTRIBUTE_PRINTF_1;
42 void gcc_obstack_init PARAMS ((struct obstack *obstack));
43 void report PARAMS ((void));
44 
45 static void usage PARAMS ((void)) ATTRIBUTE_NORETURN;
46 static void help PARAMS ((void)) ATTRIBUTE_NORETURN;
47 static void version PARAMS ((void)) ATTRIBUTE_NORETURN;
48 
49 #define JC1_LITE
50 #include "jcf.h"
51 #include "parse.h"
52 
53 /* Current input file and output file IO streams.  */
54 FILE *finput, *out;
55 
56 /* Current input filename.  */
57 char *input_filename;
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 
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
usage()95 usage ()
96 {
97   fprintf (stderr, "Try `jv-scan --help' for more information.\n");
98   exit (1);
99 }
100 
101 static void
help()102 help ()
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   printf ("%s.\n", bug_report_url);
119   exit (0);
120 }
121 
122 static void
version()123 version ()
124 {
125   printf ("jv-scan (GCC) %s\n\n", version_string);
126   printf ("Copyright (C) 2002 Free Software Foundation, Inc.\n");
127   printf ("This is free software; see the source for copying conditions.  There is NO\n");
128   printf ("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 DEFUN (main, (argc, argv),
135        int argc AND char **argv)
136 {
137   int i = 1;
138   const char *output_file = NULL;
139   const char *encoding = NULL;
140   long ft;
141   int opt;
142 
143   exec_name = argv[0];
144 
145   /* Default for output */
146   out = stdout;
147 
148   /* Process options first.  We use getopt_long and not
149      getopt_long_only because we only support `--' long options here.  */
150   while ((opt = getopt_long (argc, argv, "o:", options, NULL)) != -1)
151     {
152       switch (opt)
153 	{
154 	case 0:
155 	  /* Already handled.  */
156 	  break;
157 
158 	case 'o':
159 	  output_file = optarg;
160 	  break;
161 
162 	case OPT_HELP:
163 	  help ();
164 	  break;
165 
166 	case OPT_VERSION:
167 	  version ();
168 	  break;
169 
170 	case OPT_ENCODING:
171 	  encoding = optarg;
172 	  break;
173 
174 	default:
175 	  usage ();
176 	  break;
177 	}
178     }
179 
180   /* No flags? Do nothing */
181   if (! flag_find_main && ! flag_dump_class && ! flag_complexity)
182     return 0;
183 
184   /* Check on bad usage */
185   if (flag_find_main + flag_dump_class + flag_complexity > 1)
186     fatal_error
187       ("only one of `--print-main', `--list-class', and `--complexity' allowed");
188 
189   if (output_file && !(out = fopen (output_file, "w")))
190     fatal_error ("can't open output file `%s'", output_file);
191 
192   ft = ftell (out);
193 
194   gcc_obstack_init (&temporary_obstack);
195   java_push_parser_context ();
196 
197   for ( i = optind; i < argc; i++ )
198     if (argv [i])
199       {
200 	input_filename = argv [i];
201 	if ( (finput = fopen (argv [i], "r")) )
202 	  {
203 	    /* There's no point in trying to find the current encoding
204 	       unless we are going to do something intelligent with it
205 	       -- hence the test for iconv.  */
206 #if defined (HAVE_LOCALE_H) && defined (HAVE_ICONV) && defined (HAVE_LANGINFO_CODESET)
207 	    setlocale (LC_CTYPE, "");
208 	    if (encoding == NULL)
209 	      encoding = nl_langinfo (CODESET);
210 #endif
211 	    if (encoding == NULL || *encoding == '\0')
212 	      encoding = DEFAULT_ENCODING;
213 
214 	    java_init_lex (finput, encoding);
215 	    yyparse ();
216 	    report ();
217 	    if (ftell (out) != ft)
218 	      fputc ('\n', out);
219 	    ft = ftell (out);
220 	    fclose (finput);
221 	    reset_report ();
222 	  }
223 	else
224 	  fatal_error ("file not found `%s'", argv [i]);
225       }
226 
227   /* Flush and close */
228   if (ftell (out) != ft)
229     fputc ('\n', out);
230   if (!output_file)
231     fclose (out);
232 
233   return 0;
234 }
235 
236 
237 
238 /* Error report, memory, obstack initialization and other utility
239    functions */
240 
241 void
fatal_error(const char * s,...)242 fatal_error VPARAMS ((const char *s, ...))
243 {
244   VA_OPEN (ap, s);
245   VA_FIXEDARG (ap, const char *, s);
246 
247   fprintf (stderr, "%s: error: ", exec_name);
248   vfprintf (stderr, s, ap);
249   fputc ('\n', stderr);
250   VA_CLOSE (ap);
251   exit (1);
252 }
253 
254 void
warning(const char * s,...)255 warning VPARAMS ((const char *s, ...))
256 {
257   VA_OPEN (ap, s);
258   VA_FIXEDARG (ap, const char *, s);
259 
260   fprintf (stderr, "%s: warning: ", exec_name);
261   vfprintf (stderr, s, ap);
262   fputc ('\n', stderr);
263   VA_CLOSE (ap);
264 }
265 
266 void
gcc_obstack_init(obstack)267 gcc_obstack_init (obstack)
268      struct obstack *obstack;
269 {
270   /* Let particular systems override the size of a chunk.  */
271 #ifndef OBSTACK_CHUNK_SIZE
272 #define OBSTACK_CHUNK_SIZE 0
273 #endif
274   /* Let them override the alloc and free routines too.  */
275 #ifndef OBSTACK_CHUNK_ALLOC
276 #define OBSTACK_CHUNK_ALLOC xmalloc
277 #endif
278 #ifndef OBSTACK_CHUNK_FREE
279 #define OBSTACK_CHUNK_FREE free
280 #endif
281   _obstack_begin (obstack, OBSTACK_CHUNK_SIZE, 0,
282 		  (void *(*) (long)) OBSTACK_CHUNK_ALLOC,
283 		  (void (*) (void *)) OBSTACK_CHUNK_FREE);
284 }
285