1 /* Program to generate "main" a Java(TM) class containing a main method.
2 Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
21 Java and all Java-based marks are trademarks or registered trademarks
22 of Sun Microsystems, Inc. in the United States and other countries.
23 The Free Software Foundation is independent of Sun Microsystems, Inc. */
25 /* Written by Per Bothner <bothner@cygnus.com> */
30 #include "gansidecl.h"
33 #include "java-tree.h"
35 static char * do_mangle_classname
PARAMS ((const char *string
));
37 struct obstack name_obstack
;
38 struct obstack
*mangle_obstack
= &name_obstack
;
41 gcc_obstack_init (obstack
)
42 struct obstack
*obstack
;
44 /* Let particular systems override the size of a chunk. */
45 #ifndef OBSTACK_CHUNK_SIZE
46 #define OBSTACK_CHUNK_SIZE 0
48 /* Let them override the alloc and free routines too. */
49 #ifndef OBSTACK_CHUNK_ALLOC
50 #define OBSTACK_CHUNK_ALLOC xmalloc
52 #ifndef OBSTACK_CHUNK_FREE
53 #define OBSTACK_CHUNK_FREE free
55 _obstack_begin (obstack
, OBSTACK_CHUNK_SIZE
, 0,
56 (void *(*) PARAMS ((long))) OBSTACK_CHUNK_ALLOC
,
57 (void (*) PARAMS ((void *))) OBSTACK_CHUNK_FREE
);
60 static void usage (const char *) ATTRIBUTE_NORETURN
;
63 usage (const char *name
)
65 fprintf (stderr
, "Usage: %s [OPTIONS]... CLASSNAME [OUTFILE]\n", name
);
70 main (int argc
, const char **argv
)
72 const char *classname
;
74 const char *mangled_classname
;
80 for (i
= 1; i
< argc
; ++i
)
82 if (! strncmp (argv
[i
], "-D", 2))
90 if (i
< argc
- 2 || i
== argc
)
96 gcc_obstack_init (mangle_obstack
);
97 mangled_classname
= do_mangle_classname (classname
);
99 if (i
< argc
- 1 && strcmp (argv
[i
+ 1], "-") != 0)
101 const char *outfile
= argv
[i
+ 1];
102 stream
= fopen (outfile
, "w");
105 fprintf (stderr
, "%s: Cannot open output file: %s\n",
113 /* At this point every element of ARGV from 1 to LAST_ARG is a `-D'
114 option. Process them appropriately. */
115 fprintf (stream
, "extern const char **_Jv_Compiler_Properties;\n");
116 fprintf (stream
, "static const char *props[] =\n{\n");
117 for (i
= 1; i
< last_arg
; ++i
)
120 fprintf (stream
, " \"");
121 for (p
= &argv
[i
][2]; *p
; ++p
)
124 fprintf (stream
, "\\%o", *p
);
125 else if (*p
== '\\' || *p
== '"')
126 fprintf (stream
, "\\%c", *p
);
130 fprintf (stream
, "\",\n");
132 fprintf (stream
, " 0\n};\n\n");
134 fprintf (stream
, "extern int %s;\n", mangled_classname
);
135 fprintf (stream
, "int main (int argc, const char **argv)\n");
136 fprintf (stream
, "{\n");
137 fprintf (stream
, " _Jv_Compiler_Properties = props;\n");
138 fprintf (stream
, " JvRunMain (&%s, argc, argv);\n", mangled_classname
);
139 fprintf (stream
, "}\n");
140 if (stream
!= stdout
&& fclose (stream
) != 0)
142 fprintf (stderr
, "%s: Failed to close output file %s\n",
151 do_mangle_classname (string
)
157 obstack_grow (&name_obstack
, "_ZN", 3);
159 for (ptr
= string
; *ptr
; ptr
++ )
163 append_gpp_mangled_name (&ptr
[-count
], count
);
169 append_gpp_mangled_name (&ptr
[-count
], count
);
170 obstack_grow (mangle_obstack
, "6class$E", 8);
171 return obstack_finish (mangle_obstack
);