1/* Main for jv-scan
2   Copyright (C) 1998, 1999 Free Software Foundation, Inc.
3   Contributed by Alexandre Petit-Bianco (apbianco@cygnus.com)
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU CC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU CC; see the file COPYING.  If not, write to
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, 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
27void fatal VPROTO((const char *s, ...)) ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN;
28void warning VPROTO((const char *s, ...)) ATTRIBUTE_PRINTF_1;
29void gcc_obstack_init PROTO ((struct obstack *obstack));
30
31#define JC1_LITE
32#include "parse.h"
33
34/* Current input file and output file IO streams.  */
35FILE *finput, *out;
36
37/* Current input filename.  */
38char *input_filename;
39
40/* Executable name.  */
41char *exec_name;
42
43/* Flags matching command line options.  */
44int flag_find_main = 0;
45int flag_dump_class = 0;
46int flag_list_filename = 0;
47
48/* jc1-lite main entry point */
49int
50main (argc, argv)
51  int argc;
52  char **argv;
53{
54  int i = 1;
55  char *output_file = NULL;
56  long ft;
57
58  exec_name = argv[0];
59
60  /* Default for output */
61  out = stdout;
62
63  /* Process options first */
64  while (argv [i])
65    {
66      if (argv [i][0] == '-')
67	{
68	  /* Dump result into a file */
69	  if (!strcmp (argv [i], "-o") && i+1 < argc)
70	    {
71	      argv [i] = NULL;
72	      output_file = argv [++i];
73	      argv [i] = NULL;
74	    }
75
76	  /* Print the name of the class that contains main */
77	  else if (!strcmp (argv [i], "--print-main"))
78	    flag_find_main = 1;
79
80	  else if (!strcmp (argv [i], "--list-filename"))
81	    flag_list_filename = 1;
82
83	  /* List all the classes found in a source file */
84	  else if (!strcmp (argv [i], "--list-class"))
85	    flag_dump_class = 1;
86
87	  else
88	    warning ("Unrecognized argument `%s'", argv[i]);
89
90	  /* non recognized argument ignored silently */
91	  argv [i] = NULL;	/* Nullify so it's not considered a file */
92	}
93      i++;
94    }
95
96  /* No flags? Do nothing */
97  if (!flag_find_main && !flag_dump_class)
98    exit (0);
99
100  /* Check on bad usage */
101  if (flag_find_main && flag_dump_class)
102    fatal ("Options `--print-main' and `--list-class' can't be turned on "
103	   "at the same time");
104
105  if (output_file && !(out = fopen (output_file, "w")))
106    fatal ("Can't open output file `%s'", output_file);
107
108  ft = ftell (out);
109
110  gcc_obstack_init (&temporary_obstack);
111  java_push_parser_context ();
112
113  for ( i = 1; i < argc; i++ )
114    if (argv [i])
115      {
116	input_filename = argv [i];
117	if ( (finput = fopen (argv [i], "r")) )
118	  {
119	    java_init_lex ();
120	    yyparse ();
121	    if (ftell (out) != ft)
122	      fputc ('\n', out);
123	    ft = ftell (out);
124	    fclose (finput);
125	    reset_report ();
126	  }
127	else
128	  fatal ("File not found `%s'", argv [i]);
129      }
130
131  /* Flush and close */
132  if (ftell (out) != ft)
133    fputc ('\n', out);
134  if (!output_file)
135    fclose (out);
136
137  exit (0);
138}
139
140/* Error report, memory, obstack initialization and other utility
141   functions */
142
143void
144fatal VPROTO((const char *s, ...))
145{
146#ifndef ANSI_PROTOTYPES
147  const char *s;
148#endif
149  va_list ap;
150
151  VA_START (ap, s);
152
153#ifndef ANSI_PROTOTYPES
154  s = va_arg (ap, const char *);
155#endif
156
157  fprintf (stderr, "%s: error: ", exec_name);
158  vfprintf (stderr, s, ap);
159  fputc ('\n', stderr);
160  va_end (ap);
161  exit (1);
162}
163
164void
165warning VPROTO((const char *s, ...))
166{
167#ifndef ANSI_PROTOTYPES
168  const char *s;
169#endif
170  va_list ap;
171
172  VA_START (ap, s);
173
174#ifndef ANSI_PROTOTYPES
175  s = va_arg (ap, const char *);
176#endif
177
178  fprintf (stderr, "%s: warning: ", exec_name);
179  vfprintf (stderr, s, ap);
180  fputc ('\n', stderr);
181  va_end (ap);
182}
183
184void
185gcc_obstack_init (obstack)
186     struct obstack *obstack;
187{
188  /* Let particular systems override the size of a chunk.  */
189#ifndef OBSTACK_CHUNK_SIZE
190#define OBSTACK_CHUNK_SIZE 0
191#endif
192  /* Let them override the alloc and free routines too.  */
193#ifndef OBSTACK_CHUNK_ALLOC
194#define OBSTACK_CHUNK_ALLOC xmalloc
195#endif
196#ifndef OBSTACK_CHUNK_FREE
197#define OBSTACK_CHUNK_FREE free
198#endif
199  _obstack_begin (obstack, OBSTACK_CHUNK_SIZE, 0,
200		  (void *(*) ()) OBSTACK_CHUNK_ALLOC,
201		  (void (*) ()) OBSTACK_CHUNK_FREE);
202}
203
204PTR
205xmalloc (size)
206  size_t size;
207{
208  register PTR val = (PTR) malloc (size);
209
210  if (val == 0)
211    fatal ("virtual memory exhausted");
212  return val;
213}
214