00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "config.h"
00023 #include "system.h"
00024
00025 #include "obstack.h"
00026
00027 #include "version.h"
00028
00029 #ifdef HAVE_LOCALE_H
00030 #include <locale.h>
00031 #endif
00032
00033 #ifdef HAVE_NL_LANGINFO
00034 #include <langinfo.h>
00035 #endif
00036
00037 #include <getopt.h>
00038
00039 extern void fatal_error PARAMS ((const char *s, ...))
00040 ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN;
00041 void warning PARAMS ((const char *s, ...)) ATTRIBUTE_PRINTF_1;
00042 void gcc_obstack_init PARAMS ((struct obstack *obstack));
00043 void report PARAMS ((void));
00044
00045 static void usage PARAMS ((void)) ATTRIBUTE_NORETURN;
00046 static void help PARAMS ((void)) ATTRIBUTE_NORETURN;
00047 static void version PARAMS ((void)) ATTRIBUTE_NORETURN;
00048
00049 #define JC1_LITE
00050 #include "jcf.h"
00051 #include "parse.h"
00052
00053
00054 FILE *finput, *out;
00055
00056
00057 char *input_filename;
00058
00059
00060 char *exec_name;
00061
00062
00063 int flag_find_main = 0;
00064 int flag_dump_class = 0;
00065 int flag_list_filename = 0;
00066 int flag_complexity = 0;
00067
00068 int pedantic = 0;
00069
00070
00071
00072
00073 #define LONG_OPT(Num) ((Num) + 128)
00074
00075 #define OPT_HELP LONG_OPT (0)
00076 #define OPT_VERSION LONG_OPT (1)
00077 #define OPT_ENCODING LONG_OPT (2)
00078
00079 static const struct option options[] =
00080 {
00081 { "help", no_argument, NULL, OPT_HELP },
00082 { "version", no_argument, NULL, OPT_VERSION },
00083 { "print-main", no_argument, &flag_find_main, 1 },
00084 { "list-filename", no_argument, &flag_list_filename, 1 },
00085 { "list-class", no_argument, &flag_dump_class, 1 },
00086 { "encoding", required_argument, NULL, OPT_ENCODING },
00087 { "complexity", no_argument, &flag_complexity, 1 },
00088 { NULL, no_argument, NULL, 0 }
00089 };
00090
00091 static void
00092 usage ()
00093 {
00094 fprintf (stderr, "Try `jv-scan --help' for more information.\n");
00095 exit (1);
00096 }
00097
00098 static void
00099 help ()
00100 {
00101 printf ("Usage: jv-scan [OPTION]... FILE...\n\n");
00102 printf ("Print useful information read from Java source files.\n\n");
00103 printf (" --complexity Print cyclomatic complexity of input file\n");
00104 printf (" --encoding NAME Specify encoding of input file\n");
00105 printf (" --print-main Print name of class containing `main'\n");
00106 printf (" --list-class List all classes defined in file\n");
00107 printf (" --list-filename Print input filename when listing class names\n");
00108 printf (" -o FILE Set output file name\n");
00109 printf ("\n");
00110 printf (" --help Print this help, then exit\n");
00111 printf (" --version Print version number, then exit\n");
00112 printf ("\n");
00113 printf ("For bug reporting instructions, please see:\n");
00114 printf ("%s.\n", GCCBUGURL);
00115 exit (0);
00116 }
00117
00118 static void
00119 version ()
00120 {
00121 printf ("jv-scan (GCC) %s\n\n", version_string);
00122 printf ("Copyright (C) 2002 Free Software Foundation, Inc.\n");
00123 printf ("This is free software; see the source for copying conditions. There is NO\n");
00124 printf ("warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n");
00125 exit (0);
00126 }
00127
00128
00129 int
00130 DEFUN (main, (argc, argv),
00131 int argc AND char **argv)
00132 {
00133 int i = 1;
00134 const char *output_file = NULL;
00135 const char *encoding = NULL;
00136 long ft;
00137 int opt;
00138
00139 exec_name = argv[0];
00140
00141
00142 out = stdout;
00143
00144
00145
00146 while ((opt = getopt_long (argc, argv, "o:", options, NULL)) != -1)
00147 {
00148 switch (opt)
00149 {
00150 case 0:
00151
00152 break;
00153
00154 case 'o':
00155 output_file = optarg;
00156 break;
00157
00158 case OPT_HELP:
00159 help ();
00160 break;
00161
00162 case OPT_VERSION:
00163 version ();
00164 break;
00165
00166 case OPT_ENCODING:
00167 encoding = optarg;
00168 break;
00169
00170 default:
00171 usage ();
00172 break;
00173 }
00174 }
00175
00176
00177 if (! flag_find_main && ! flag_dump_class && ! flag_complexity)
00178 return 0;
00179
00180
00181 if (flag_find_main + flag_dump_class + flag_complexity > 1)
00182 fatal_error
00183 ("only one of `--print-main', `--list-class', and `--complexity' allowed");
00184
00185 if (output_file && !(out = fopen (output_file, "w")))
00186 fatal_error ("can't open output file `%s'", output_file);
00187
00188 ft = ftell (out);
00189
00190 gcc_obstack_init (&temporary_obstack);
00191 java_push_parser_context ();
00192
00193 for ( i = optind; i < argc; i++ )
00194 if (argv [i])
00195 {
00196 input_filename = argv [i];
00197 if ( (finput = fopen (argv [i], "r")) )
00198 {
00199
00200
00201
00202 #if defined (HAVE_LOCALE_H) && defined (HAVE_ICONV) && defined (HAVE_NL_LANGINFO)
00203 setlocale (LC_CTYPE, "");
00204 if (encoding == NULL)
00205 encoding = nl_langinfo (CODESET);
00206 #endif
00207 if (encoding == NULL || *encoding == '\0')
00208 encoding = DEFAULT_ENCODING;
00209
00210 java_init_lex (finput, encoding);
00211 yyparse ();
00212 report ();
00213 if (ftell (out) != ft)
00214 fputc ('\n', out);
00215 ft = ftell (out);
00216 fclose (finput);
00217 reset_report ();
00218 }
00219 else
00220 fatal_error ("file not found `%s'", argv [i]);
00221 }
00222
00223
00224 if (ftell (out) != ft)
00225 fputc ('\n', out);
00226 if (!output_file)
00227 fclose (out);
00228
00229 return 0;
00230 }
00231
00232
00233
00234
00235
00236
00237 void
00238 fatal_error VPARAMS ((const char *s, ...))
00239 {
00240 VA_OPEN (ap, s);
00241 VA_FIXEDARG (ap, const char *, s);
00242
00243 fprintf (stderr, "%s: error: ", exec_name);
00244 vfprintf (stderr, s, ap);
00245 fputc ('\n', stderr);
00246 VA_CLOSE (ap);
00247 exit (1);
00248 }
00249
00250 void
00251 warning VPARAMS ((const char *s, ...))
00252 {
00253 VA_OPEN (ap, s);
00254 VA_FIXEDARG (ap, const char *, s);
00255
00256 fprintf (stderr, "%s: warning: ", exec_name);
00257 vfprintf (stderr, s, ap);
00258 fputc ('\n', stderr);
00259 VA_CLOSE (ap);
00260 }
00261
00262 void
00263 gcc_obstack_init (obstack)
00264 struct obstack *obstack;
00265 {
00266
00267 #ifndef OBSTACK_CHUNK_SIZE
00268 #define OBSTACK_CHUNK_SIZE 0
00269 #endif
00270
00271 #ifndef OBSTACK_CHUNK_ALLOC
00272 #define OBSTACK_CHUNK_ALLOC xmalloc
00273 #endif
00274 #ifndef OBSTACK_CHUNK_FREE
00275 #define OBSTACK_CHUNK_FREE free
00276 #endif
00277 _obstack_begin (obstack, OBSTACK_CHUNK_SIZE, 0,
00278 (void *(*) (long)) OBSTACK_CHUNK_ALLOC,
00279 (void (*) (void *)) OBSTACK_CHUNK_FREE);
00280 }