2015-05-18 Steven G. Kargl <kargl@gcc.gnu.org>
[official-gcc.git] / gcc / java / jvgenmain.c
blobfa31937dd392fffa1c7a10b884bd658c99a43bf2
1 /* Program to generate "main" a Java(TM) class containing a main method.
2 Copyright (C) 1998-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC 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 3, or (at your option)
9 any later version.
11 GCC 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 GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>.
20 Java and all Java-based marks are trademarks or registered trademarks
21 of Sun Microsystems, Inc. in the United States and other countries.
22 The Free Software Foundation is independent of Sun Microsystems, Inc. */
24 /* Written by Per Bothner <bothner@cygnus.com> */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "obstack.h"
30 #include "jcf.h"
31 #include "hash-set.h"
32 #include "machmode.h"
33 #include "vec.h"
34 #include "double-int.h"
35 #include "input.h"
36 #include "alias.h"
37 #include "symtab.h"
38 #include "options.h"
39 #include "wide-int.h"
40 #include "inchash.h"
41 #include "tree.h"
42 #include "java-tree.h"
43 #include "intl.h"
44 #include "diagnostic.h"
46 static char * do_mangle_classname (const char *string);
48 struct obstack name_obstack;
49 struct obstack *mangle_obstack = &name_obstack;
51 static void usage (const char *) ATTRIBUTE_NORETURN;
53 static void
54 usage (const char *name)
56 fprintf (stderr, _("Usage: %s [OPTIONS]... CLASSNAMEmain [OUTFILE]\n"),
57 name);
58 exit (1);
61 int
62 main (int argc, char **argv)
64 char *classname, *p;
65 FILE *stream;
66 const char *mangled_classname;
67 int i, last_arg;
68 int indirect = 0;
69 char *prog_name = argv[0];
71 p = argv[0] + strlen (argv[0]);
72 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
73 --p;
74 progname = p;
76 xmalloc_set_program_name (progname);
78 /* Unlock the stdio streams. */
79 unlock_std_streams ();
81 gcc_init_libintl ();
83 diagnostic_initialize (global_dc, 0);
85 if (argc > 1 && ! strcmp (argv[1], "-findirect-dispatch"))
87 indirect = 1;
88 ++argv;
89 --argc;
92 if (argc < 2)
93 usage (prog_name);
95 for (i = 1; i < argc; ++i)
97 if (! strncmp (argv[i], "-D", 2))
99 /* Handled later. Check "-D XXX=YYY". */
100 if (argv[i][2] == '\0')
101 i++;
103 else
104 break;
107 if (i < argc - 2 || i == argc)
108 usage (prog_name);
109 last_arg = i;
111 classname = argv[i];
113 /* gcj always appends `main' to classname. We need to strip this here. */
114 p = strrchr (classname, 'm');
115 if (p == NULL || p == classname || strcmp (p, "main") != 0)
116 usage (prog_name);
117 else
118 *p = '\0';
120 gcc_obstack_init (mangle_obstack);
121 mangled_classname = do_mangle_classname (classname);
123 if (i < argc - 1 && strcmp (argv[i + 1], "-") != 0)
125 const char *outfile = argv[i + 1];
126 stream = fopen (outfile, "w");
127 if (stream == NULL)
129 fprintf (stderr, _("%s: Cannot open output file: %s\n"),
130 prog_name, outfile);
131 exit (1);
134 else
135 stream = stdout;
137 /* At this point every element of ARGV from 1 to LAST_ARG is a `-D'
138 option. Process them appropriately. */
139 fprintf (stream, "extern const char **_Jv_Compiler_Properties;\n");
140 if (indirect)
141 fprintf (stream, "extern void JvRunMainName ();\n");
142 else
143 fprintf (stream, "extern void JvRunMain ();\n");
144 fprintf (stream, "static const char *props[] =\n{\n");
145 for (i = 1; i < last_arg; ++i)
147 const char *p;
149 if (strcmp (argv[i], "-D") == 0)
150 continue;
152 fprintf (stream, " \"");
153 for (p = argv[i]; *p; ++p)
155 if (! ISPRINT (*p))
156 fprintf (stream, "\\%o", *p);
157 else if (*p == '\\' || *p == '"')
158 fprintf (stream, "\\%c", *p);
159 else
160 putc (*p, stream);
162 fprintf (stream, "\",\n");
164 fprintf (stream, " 0\n};\n\n");
166 fprintf (stream, "int main (int argc, const char **argv)\n");
167 fprintf (stream, "{\n");
168 fprintf (stream, " _Jv_Compiler_Properties = props;\n");
169 if (indirect)
170 fprintf (stream, " JvRunMainName (\"%s\", argc, argv);\n", classname);
171 else
173 fprintf (stream, " extern char %s;\n", mangled_classname);
174 fprintf (stream, " JvRunMain (&%s, argc, argv);\n", mangled_classname);
176 fprintf (stream, "}\n");
177 if (stream != stdout && fclose (stream) != 0)
179 fprintf (stderr, _("%s: Failed to close output file %s\n"),
180 prog_name, argv[2]);
181 exit (1);
183 return 0;
187 static char *
188 do_mangle_classname (const char *string)
190 const char *ptr;
191 int count = 0;
193 obstack_grow (&name_obstack, "_ZN", 3);
195 for (ptr = string; *ptr; ptr++ )
197 if (*ptr == '.')
199 append_gpp_mangled_name (ptr - count, count);
200 count = 0;
202 else
203 count++;
205 append_gpp_mangled_name (&ptr [-count], count);
206 obstack_grow (mangle_obstack, "6class$E", strlen ("6class$E"));
207 obstack_1grow (mangle_obstack, '\0');
208 return XOBFINISH (mangle_obstack, char *);