FSF GCC merge 02/23/03
[official-gcc.git] / gcc / java / jvgenmain.c
blobba3ac8882046a27448b5c7efca6a084b204b6a55
1 /* Program to generate "main" a Java(TM) class containing a main method.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC 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.
12 GCC 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.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 Java and all Java-based marks are trademarks or registered trademarks
23 of Sun Microsystems, Inc. in the United States and other countries.
24 The Free Software Foundation is independent of Sun Microsystems, Inc. */
26 /* Written by Per Bothner <bothner@cygnus.com> */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "tm.h"
32 #include "obstack.h"
33 #include "jcf.h"
34 #include "tree.h"
35 #include "java-tree.h"
37 static char * do_mangle_classname (const char *string);
39 struct obstack name_obstack;
40 struct obstack *mangle_obstack = &name_obstack;
42 void
43 gcc_obstack_init (struct obstack *obstack)
45 /* Let particular systems override the size of a chunk. */
46 #ifndef OBSTACK_CHUNK_SIZE
47 #define OBSTACK_CHUNK_SIZE 0
48 #endif
49 /* Let them override the alloc and free routines too. */
50 #ifndef OBSTACK_CHUNK_ALLOC
51 #define OBSTACK_CHUNK_ALLOC xmalloc
52 #endif
53 #ifndef OBSTACK_CHUNK_FREE
54 #define OBSTACK_CHUNK_FREE free
55 #endif
56 _obstack_begin (obstack, OBSTACK_CHUNK_SIZE, 0,
57 (void *(*) (long)) OBSTACK_CHUNK_ALLOC,
58 (void (*) (void *)) OBSTACK_CHUNK_FREE);
61 static void usage (const char *) ATTRIBUTE_NORETURN;
63 static void
64 usage (const char *name)
66 fprintf (stderr, "Usage: %s [OPTIONS]... CLASSNAMEmain [OUTFILE]\n", name);
67 exit (1);
70 int
71 main (int argc, char **argv)
73 char *classname, *p;
74 FILE *stream;
75 const char *mangled_classname;
76 int i, last_arg;
78 if (argc < 2)
79 usage (argv[0]);
81 for (i = 1; i < argc; ++i)
83 if (! strncmp (argv[i], "-D", 2))
85 /* Handled later. */
87 else
88 break;
91 if (i < argc - 2 || i == argc)
92 usage (argv[0]);
93 last_arg = i;
95 classname = argv[i];
97 /* gcj always appends `main' to classname. We need to strip this here. */
98 p = strrchr (classname, 'm');
99 if (p == NULL || p == classname || strcmp (p, "main") != 0)
100 usage (argv[0]);
101 else
102 *p = '\0';
104 gcc_obstack_init (mangle_obstack);
105 mangled_classname = do_mangle_classname (classname);
107 if (i < argc - 1 && strcmp (argv[i + 1], "-") != 0)
109 const char *outfile = argv[i + 1];
110 stream = fopen (outfile, "w");
111 if (stream == NULL)
113 fprintf (stderr, "%s: Cannot open output file: %s\n",
114 argv[0], outfile);
115 exit (1);
118 else
119 stream = stdout;
121 /* At this point every element of ARGV from 1 to LAST_ARG is a `-D'
122 option. Process them appropriately. */
123 fprintf (stream, "extern const char **_Jv_Compiler_Properties;\n");
124 fprintf (stream, "static const char *props[] =\n{\n");
125 for (i = 1; i < last_arg; ++i)
127 const char *p;
128 fprintf (stream, " \"");
129 for (p = &argv[i][2]; *p; ++p)
131 if (! ISPRINT (*p))
132 fprintf (stream, "\\%o", *p);
133 else if (*p == '\\' || *p == '"')
134 fprintf (stream, "\\%c", *p);
135 else
136 putc (*p, stream);
138 fprintf (stream, "\",\n");
140 fprintf (stream, " 0\n};\n\n");
142 fprintf (stream, "extern int %s;\n", mangled_classname);
143 fprintf (stream, "int main (int argc, const char **argv)\n");
144 fprintf (stream, "{\n");
145 fprintf (stream, " _Jv_Compiler_Properties = props;\n");
146 fprintf (stream, " JvRunMain (&%s, argc, argv);\n", mangled_classname);
147 fprintf (stream, "}\n");
148 if (stream != stdout && fclose (stream) != 0)
150 fprintf (stderr, "%s: Failed to close output file %s\n",
151 argv[0], argv[2]);
152 exit (1);
154 return 0;
158 static char *
159 do_mangle_classname (const char *string)
161 const char *ptr;
162 int count = 0;
164 obstack_grow (&name_obstack, "_ZN", 3);
166 for (ptr = string; *ptr; ptr++ )
168 if (ptr[0] == '.')
170 append_gpp_mangled_name (&ptr [-count], count);
171 count = 0;
173 else
174 count++;
176 append_gpp_mangled_name (&ptr [-count], count);
177 obstack_grow (mangle_obstack, "6class$E", 8);
178 obstack_1grow (mangle_obstack, '\0');
179 return obstack_finish (mangle_obstack);