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.
4
5 This file is part of GCC.
6
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.
11
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.
16
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.
21
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. */
25
26 /* Written by Per Bothner <bothner@cygnus.com> */
27
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"
36
37 static char * do_mangle_classname (const char *string);
38
39 struct obstack name_obstack;
40 struct obstack *mangle_obstack = &name_obstack;
41
42 static void usage (const char *) ATTRIBUTE_NORETURN;
43
44 static void
usage(const char * name)45 usage (const char *name)
46 {
47 fprintf (stderr, "Usage: %s [OPTIONS]... CLASSNAMEmain [OUTFILE]\n", name);
48 exit (1);
49 }
50
51 int
main(int argc,char ** argv)52 main (int argc, char **argv)
53 {
54 char *classname, *p;
55 FILE *stream;
56 const char *mangled_classname;
57 int i, last_arg;
58
59 if (argc < 2)
60 usage (argv[0]);
61
62 for (i = 1; i < argc; ++i)
63 {
64 if (! strncmp (argv[i], "-D", 2))
65 {
66 /* Handled later. */
67 }
68 else
69 break;
70 }
71
72 if (i < argc - 2 || i == argc)
73 usage (argv[0]);
74 last_arg = i;
75
76 classname = argv[i];
77
78 /* gcj always appends `main' to classname. We need to strip this here. */
79 p = strrchr (classname, 'm');
80 if (p == NULL || p == classname || strcmp (p, "main") != 0)
81 usage (argv[0]);
82 else
83 *p = '\0';
84
85 gcc_obstack_init (mangle_obstack);
86 mangled_classname = do_mangle_classname (classname);
87
88 if (i < argc - 1 && strcmp (argv[i + 1], "-") != 0)
89 {
90 const char *outfile = argv[i + 1];
91 stream = fopen (outfile, "w");
92 if (stream == NULL)
93 {
94 fprintf (stderr, "%s: Cannot open output file: %s\n",
95 argv[0], outfile);
96 exit (1);
97 }
98 }
99 else
100 stream = stdout;
101
102 /* At this point every element of ARGV from 1 to LAST_ARG is a `-D'
103 option. Process them appropriately. */
104 fprintf (stream, "extern const char **_Jv_Compiler_Properties;\n");
105 fprintf (stream, "static const char *props[] =\n{\n");
106 for (i = 1; i < last_arg; ++i)
107 {
108 const char *p;
109 fprintf (stream, " \"");
110 for (p = &argv[i][2]; *p; ++p)
111 {
112 if (! ISPRINT (*p))
113 fprintf (stream, "\\%o", *p);
114 else if (*p == '\\' || *p == '"')
115 fprintf (stream, "\\%c", *p);
116 else
117 putc (*p, stream);
118 }
119 fprintf (stream, "\",\n");
120 }
121 fprintf (stream, " 0\n};\n\n");
122
123 fprintf (stream, "extern int %s;\n", mangled_classname);
124 fprintf (stream, "int main (int argc, const char **argv)\n");
125 fprintf (stream, "{\n");
126 fprintf (stream, " _Jv_Compiler_Properties = props;\n");
127 fprintf (stream, " JvRunMain (&%s, argc, argv);\n", mangled_classname);
128 fprintf (stream, "}\n");
129 if (stream != stdout && fclose (stream) != 0)
130 {
131 fprintf (stderr, "%s: Failed to close output file %s\n",
132 argv[0], argv[2]);
133 exit (1);
134 }
135 return 0;
136 }
137
138
139 static char *
do_mangle_classname(const char * string)140 do_mangle_classname (const char *string)
141 {
142 const char *ptr;
143 int count = 0;
144
145 obstack_grow (&name_obstack, "_ZN", 3);
146
147 for (ptr = string; *ptr; ptr++ )
148 {
149 if (ptr[0] == '.')
150 {
151 append_gpp_mangled_name (&ptr [-count], count);
152 count = 0;
153 }
154 else
155 count++;
156 }
157 append_gpp_mangled_name (&ptr [-count], count);
158 obstack_grow (mangle_obstack, "6class$E", 8);
159 obstack_1grow (mangle_obstack, '\0');
160 return obstack_finish (mangle_obstack);
161 }
162