OSDN Git Service

* buffer.h: PROTO -> PARAMS.
[pf3gnuchains/gcc-fork.git] / gcc / java / jv-scan.c
1 /* Main for jv-scan
2    Copyright (C) 1998, 1999, 2000 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 void fatal PARAMS ((const char *s, ...)) ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN;
28 void warning PARAMS ((const char *s, ...)) ATTRIBUTE_PRINTF_1;
29 void gcc_obstack_init PARAMS ((struct obstack *obstack));
30
31 #define JC1_LITE
32 #include "jcf.h"
33 #include "parse.h"
34
35 /* Current input file and output file IO streams.  */
36 FILE *finput, *out;
37
38 /* Current input filename.  */
39 char *input_filename;
40
41 /* Executable name.  */
42 char *exec_name;
43
44 /* Flags matching command line options.  */
45 int flag_find_main = 0;
46 int flag_dump_class = 0;
47 int flag_list_filename = 0;
48
49 /* jc1-lite main entry point */
50 int
51 DEFUN (main, (argc, argv),
52        int argc AND char **argv)
53 {
54   int i = 1;
55   const 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     return 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 at the same time");
103
104   if (output_file && !(out = fopen (output_file, "w")))
105     fatal ("Can't open output file `%s'", output_file);
106
107   ft = ftell (out);
108
109   gcc_obstack_init (&temporary_obstack);
110   java_push_parser_context ();
111
112   for ( i = 1; i < argc; i++ )
113     if (argv [i])
114       {
115         input_filename = argv [i];
116         if ( (finput = fopen (argv [i], "r")) )
117           {
118             java_init_lex ();
119             yyparse ();
120             if (ftell (out) != ft)
121               fputc ('\n', out);
122             ft = ftell (out);
123             fclose (finput);
124             reset_report ();
125           }
126         else
127           fatal ("File not found `%s'", argv [i]);
128       }
129
130   /* Flush and close */
131   if (ftell (out) != ft)
132     fputc ('\n', out);
133   if (!output_file)
134     fclose (out);
135
136   return 0;
137 }
138
139 /* Error report, memory, obstack initialization and other utility
140    functions */
141
142 void
143 fatal VPARAMS ((const char *s, ...))
144 {
145 #ifndef ANSI_PROTOTYPES
146   const char *s;
147 #endif
148   va_list ap;
149
150   VA_START (ap, s);
151
152 #ifndef ANSI_PROTOTYPES
153   s = va_arg (ap, const char *);
154 #endif
155
156   fprintf (stderr, "%s: error: ", exec_name);
157   vfprintf (stderr, s, ap);
158   fputc ('\n', stderr);
159   va_end (ap);
160   exit (1);
161 }
162
163 void
164 warning VPARAMS ((const char *s, ...))
165 {
166 #ifndef ANSI_PROTOTYPES
167   const char *s;
168 #endif
169   va_list ap;
170
171   VA_START (ap, s);
172
173 #ifndef ANSI_PROTOTYPES
174   s = va_arg (ap, const char *);
175 #endif
176
177   fprintf (stderr, "%s: warning: ", exec_name);
178   vfprintf (stderr, s, ap);
179   fputc ('\n', stderr);
180   va_end (ap);
181 }
182
183 void
184 gcc_obstack_init (obstack)
185      struct obstack *obstack;
186 {
187   /* Let particular systems override the size of a chunk.  */
188 #ifndef OBSTACK_CHUNK_SIZE
189 #define OBSTACK_CHUNK_SIZE 0
190 #endif
191   /* Let them override the alloc and free routines too.  */
192 #ifndef OBSTACK_CHUNK_ALLOC
193 #define OBSTACK_CHUNK_ALLOC xmalloc
194 #endif
195 #ifndef OBSTACK_CHUNK_FREE
196 #define OBSTACK_CHUNK_FREE free
197 #endif
198   _obstack_begin (obstack, OBSTACK_CHUNK_SIZE, 0,
199                   (void *(*) (long)) OBSTACK_CHUNK_ALLOC,
200                   (void (*) (void *)) OBSTACK_CHUNK_FREE);
201 }