OSDN Git Service

Merged gcj-eclipse branch to trunk.
[pf3gnuchains/gcc-fork.git] / gcc / java / jvgenmain.c
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    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, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, 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 #include "intl.h"
37
38 static char * do_mangle_classname (const char *string);
39
40 struct obstack  name_obstack;
41 struct obstack *mangle_obstack = &name_obstack;
42
43 static void usage (const char *) ATTRIBUTE_NORETURN;
44
45 static void
46 usage (const char *name)
47 {
48   fprintf (stderr, _("Usage: %s [OPTIONS]... CLASSNAMEmain [OUTFILE]\n"),
49            name);
50   exit (1);
51 }
52
53 int
54 main (int argc, char **argv)
55 {
56   char *classname, *p;
57   FILE *stream;
58   const char *mangled_classname;
59   int i, last_arg;
60   int indirect = 0;
61   char *prog_name = argv[0];
62
63   /* Unlock the stdio streams.  */
64   unlock_std_streams ();
65
66   gcc_init_libintl ();
67
68   if (argc > 1 && ! strcmp (argv[1], "-findirect-dispatch"))
69     {
70       indirect = 1;
71       ++argv;
72       --argc;
73     }
74
75   if (argc < 2)
76     usage (prog_name);
77
78   for (i = 1; i < argc; ++i)
79     {
80       if (! strncmp (argv[i], "-D", 2))
81         {
82           /* Handled later.  */
83         }
84       else
85         break;
86     }
87
88   if (i < argc - 2 || i == argc)
89     usage (prog_name);
90   last_arg = i;
91
92   classname = argv[i];
93
94   /* gcj always appends `main' to classname.  We need to strip this here.  */
95   p = strrchr (classname, 'm');
96   if (p == NULL || p == classname || strcmp (p, "main") != 0)
97     usage (prog_name);
98   else
99     *p = '\0';
100
101   gcc_obstack_init (mangle_obstack);
102   mangled_classname = do_mangle_classname (classname);
103
104   if (i < argc - 1 && strcmp (argv[i + 1], "-") != 0)
105     {
106       const char *outfile = argv[i + 1];
107       stream = fopen (outfile, "w");
108       if (stream == NULL)
109         {
110           fprintf (stderr, _("%s: Cannot open output file: %s\n"),
111                    prog_name, outfile);
112           exit (1);
113         }
114     }
115   else
116     stream = stdout;
117
118   /* At this point every element of ARGV from 1 to LAST_ARG is a `-D'
119      option.  Process them appropriately.  */
120   fprintf (stream, "extern const char **_Jv_Compiler_Properties;\n");
121   fprintf (stream, "static const char *props[] =\n{\n");
122   for (i = 1; i < last_arg; ++i)
123     {
124       const char *p;
125       fprintf (stream, "  \"");
126       for (p = &argv[i][2]; *p; ++p)
127         {
128           if (! ISPRINT (*p))
129             fprintf (stream, "\\%o", *p);
130           else if (*p == '\\' || *p == '"')
131             fprintf (stream, "\\%c", *p);
132           else
133             putc (*p, stream);
134         }
135       fprintf (stream, "\",\n");
136     }
137   fprintf (stream, "  0\n};\n\n");
138
139   fprintf (stream, "int main (int argc, const char **argv)\n");
140   fprintf (stream, "{\n");
141   fprintf (stream, "   _Jv_Compiler_Properties = props;\n");
142   if (indirect)
143     fprintf (stream, "   JvRunMainName (\"%s\", argc, argv);\n", classname);
144   else
145     {
146       fprintf (stream, "   extern void *%s;\n", mangled_classname);
147       fprintf (stream, "   JvRunMain (%s, argc, argv);\n", mangled_classname);
148     }
149   fprintf (stream, "}\n");
150   if (stream != stdout && fclose (stream) != 0)
151     {
152       fprintf (stderr, _("%s: Failed to close output file %s\n"),
153                prog_name, argv[2]);
154       exit (1);
155     }
156   return 0;
157 }
158
159
160 static char *
161 do_mangle_classname (const char *string)
162 {
163   const char *ptr;
164   int count = 0;
165
166   obstack_grow (&name_obstack, "_ZN", 3);
167
168   for (ptr = string; *ptr; ptr++ )
169     {
170       if (*ptr == '.')
171         {
172           append_gpp_mangled_name (ptr - count, count);
173           count = 0;
174         }
175       else
176         count++;
177     }
178   append_gpp_mangled_name (&ptr [-count], count);
179   obstack_grow (mangle_obstack, "7class$$E", strlen ("7class$$E"));
180   obstack_1grow (mangle_obstack, '\0');
181   return obstack_finish (mangle_obstack);
182 }