* cpplib.pot: Regenerate.
[official-gcc.git] / gcc / java / jvgenmain.c
blobd6ca8717d70c782dcf8e0f8372430d484d5629d9
1 /* Program to generate "main" a Java(TM) class containing a main method.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
3 2007, 2008, 2010, 2011 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 3, 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 COPYING3. If not see
19 <http://www.gnu.org/licenses/>.
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> */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "obstack.h"
31 #include "jcf.h"
32 #include "tree.h"
33 #include "java-tree.h"
34 #include "intl.h"
35 #include "diagnostic.h"
37 static char * do_mangle_classname (const char *string);
39 struct obstack name_obstack;
40 struct obstack *mangle_obstack = &name_obstack;
42 static void usage (const char *) ATTRIBUTE_NORETURN;
44 static void
45 usage (const char *name)
47 fprintf (stderr, _("Usage: %s [OPTIONS]... CLASSNAMEmain [OUTFILE]\n"),
48 name);
49 exit (1);
52 int
53 main (int argc, char **argv)
55 char *classname, *p;
56 FILE *stream;
57 const char *mangled_classname;
58 int i, last_arg;
59 int indirect = 0;
60 char *prog_name = argv[0];
62 p = argv[0] + strlen (argv[0]);
63 while (p != argv[0] && !IS_DIR_SEPARATOR (p[-1]))
64 --p;
65 progname = p;
67 xmalloc_set_program_name (progname);
69 /* Unlock the stdio streams. */
70 unlock_std_streams ();
72 gcc_init_libintl ();
74 diagnostic_initialize (global_dc, 0);
76 if (argc > 1 && ! strcmp (argv[1], "-findirect-dispatch"))
78 indirect = 1;
79 ++argv;
80 --argc;
83 if (argc < 2)
84 usage (prog_name);
86 for (i = 1; i < argc; ++i)
88 if (! strncmp (argv[i], "-D", 2))
90 /* Handled later. Check "-D XXX=YYY". */
91 if (argv[i][2] == '\0')
92 i++;
94 else
95 break;
98 if (i < argc - 2 || i == argc)
99 usage (prog_name);
100 last_arg = i;
102 classname = argv[i];
104 /* gcj always appends `main' to classname. We need to strip this here. */
105 p = strrchr (classname, 'm');
106 if (p == NULL || p == classname || strcmp (p, "main") != 0)
107 usage (prog_name);
108 else
109 *p = '\0';
111 gcc_obstack_init (mangle_obstack);
112 mangled_classname = do_mangle_classname (classname);
114 if (i < argc - 1 && strcmp (argv[i + 1], "-") != 0)
116 const char *outfile = argv[i + 1];
117 stream = fopen (outfile, "w");
118 if (stream == NULL)
120 fprintf (stderr, _("%s: Cannot open output file: %s\n"),
121 prog_name, outfile);
122 exit (1);
125 else
126 stream = stdout;
128 /* At this point every element of ARGV from 1 to LAST_ARG is a `-D'
129 option. Process them appropriately. */
130 fprintf (stream, "extern const char **_Jv_Compiler_Properties;\n");
131 fprintf (stream, "static const char *props[] =\n{\n");
132 for (i = 1; i < last_arg; ++i)
134 const char *p;
136 if (strcmp (argv[i], "-D") == 0)
137 continue;
139 fprintf (stream, " \"");
140 for (p = argv[i]; *p; ++p)
142 if (! ISPRINT (*p))
143 fprintf (stream, "\\%o", *p);
144 else if (*p == '\\' || *p == '"')
145 fprintf (stream, "\\%c", *p);
146 else
147 putc (*p, stream);
149 fprintf (stream, "\",\n");
151 fprintf (stream, " 0\n};\n\n");
153 fprintf (stream, "int main (int argc, const char **argv)\n");
154 fprintf (stream, "{\n");
155 fprintf (stream, " _Jv_Compiler_Properties = props;\n");
156 if (indirect)
157 fprintf (stream, " JvRunMainName (\"%s\", argc, argv);\n", classname);
158 else
160 fprintf (stream, " extern char %s;\n", mangled_classname);
161 fprintf (stream, " JvRunMain (&%s, argc, argv);\n", mangled_classname);
163 fprintf (stream, "}\n");
164 if (stream != stdout && fclose (stream) != 0)
166 fprintf (stderr, _("%s: Failed to close output file %s\n"),
167 prog_name, argv[2]);
168 exit (1);
170 return 0;
174 static char *
175 do_mangle_classname (const char *string)
177 const char *ptr;
178 int count = 0;
180 obstack_grow (&name_obstack, "_ZN", 3);
182 for (ptr = string; *ptr; ptr++ )
184 if (*ptr == '.')
186 append_gpp_mangled_name (ptr - count, count);
187 count = 0;
189 else
190 count++;
192 append_gpp_mangled_name (&ptr [-count], count);
193 obstack_grow (mangle_obstack, "6class$E", strlen ("6class$E"));
194 obstack_1grow (mangle_obstack, '\0');
195 return XOBFINISH (mangle_obstack, char *);