]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/java/jvgenmain.c
Merge basic-improvements-branch to trunk
[thirdparty/gcc.git] / gcc / java / jvgenmain.c
CommitLineData
377029eb 1/* Program to generate "main" a Java(TM) class containing a main method.
6599bf3a 2 Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
377029eb 3
4This file is part of GNU CC.
5
6GNU CC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU CC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU CC; see the file COPYING. If not, write to
18the Free Software Foundation, 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA.
20
21Java and all Java-based marks are trademarks or registered trademarks
22of Sun Microsystems, Inc. in the United States and other countries.
23The Free Software Foundation is independent of Sun Microsystems, Inc. */
24
25/* Written by Per Bothner <bothner@cygnus.com> */
26
377029eb 27#include "config.h"
014e6e0c 28#include "system.h"
805e22b2 29#include "coretypes.h"
30#include "tm.h"
377029eb 31#include "obstack.h"
003019ba 32#include "jcf.h"
33#include "tree.h"
34#include "java-tree.h"
377029eb 35
6599bf3a 36static char * do_mangle_classname PARAMS ((const char *string));
37
bca8957b 38struct obstack name_obstack;
39struct obstack *mangle_obstack = &name_obstack;
377029eb 40
377029eb 41void
42gcc_obstack_init (obstack)
43 struct obstack *obstack;
44{
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,
fa334f7d 57 (void *(*) PARAMS ((long))) OBSTACK_CHUNK_ALLOC,
58 (void (*) PARAMS ((void *))) OBSTACK_CHUNK_FREE);
377029eb 59}
60
4e46d728 61static void usage (const char *) ATTRIBUTE_NORETURN;
62
d25efa9c 63static void
64usage (const char *name)
65{
32d661fc 66 fprintf (stderr, "Usage: %s [OPTIONS]... CLASSNAMEmain [OUTFILE]\n", name);
d25efa9c 67 exit (1);
68}
69
377029eb 70int
32d661fc 71main (int argc, char **argv)
377029eb 72{
32d661fc 73 char *classname, *p;
377029eb 74 FILE *stream;
68ddcc2a 75 const char *mangled_classname;
d25efa9c 76 int i, last_arg;
77
78 if (argc < 2)
79 usage (argv[0]);
377029eb 80
d25efa9c 81 for (i = 1; i < argc; ++i)
377029eb 82 {
d25efa9c 83 if (! strncmp (argv[i], "-D", 2))
84 {
85 /* Handled later. */
86 }
87 else
88 break;
377029eb 89 }
90
d25efa9c 91 if (i < argc - 2 || i == argc)
92 usage (argv[0]);
93 last_arg = i;
94
95 classname = argv[i];
377029eb 96
32d661fc 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';
103
bca8957b 104 gcc_obstack_init (mangle_obstack);
6599bf3a 105 mangled_classname = do_mangle_classname (classname);
377029eb 106
d25efa9c 107 if (i < argc - 1 && strcmp (argv[i + 1], "-") != 0)
377029eb 108 {
d25efa9c 109 const char *outfile = argv[i + 1];
377029eb 110 stream = fopen (outfile, "w");
111 if (stream == NULL)
112 {
113 fprintf (stderr, "%s: Cannot open output file: %s\n",
114 argv[0], outfile);
d25efa9c 115 exit (1);
377029eb 116 }
117 }
118 else
119 stream = stdout;
d25efa9c 120
121 /* At this point every element of ARGV from 1 to LAST_ARG is a `-D'
122 option. Process them appropriately. */
cb4952eb 123 fprintf (stream, "extern const char **_Jv_Compiler_Properties;\n");
124 fprintf (stream, "static const char *props[] =\n{\n");
d25efa9c 125 for (i = 1; i < last_arg; ++i)
126 {
127 const char *p;
128 fprintf (stream, " \"");
129 for (p = &argv[i][2]; *p; ++p)
130 {
93ce8ce4 131 if (! ISPRINT (*p))
d25efa9c 132 fprintf (stream, "\\%o", *p);
133 else if (*p == '\\' || *p == '"')
134 fprintf (stream, "\\%c", *p);
135 else
136 putc (*p, stream);
137 }
138 fprintf (stream, "\",\n");
139 }
140 fprintf (stream, " 0\n};\n\n");
141
e104bac6 142 fprintf (stream, "extern int %s;\n", mangled_classname);
377029eb 143 fprintf (stream, "int main (int argc, const char **argv)\n");
144 fprintf (stream, "{\n");
cb4952eb 145 fprintf (stream, " _Jv_Compiler_Properties = props;\n");
e104bac6 146 fprintf (stream, " JvRunMain (&%s, argc, argv);\n", mangled_classname);
377029eb 147 fprintf (stream, "}\n");
148 if (stream != stdout && fclose (stream) != 0)
149 {
150 fprintf (stderr, "%s: Failed to close output file %s\n",
151 argv[0], argv[2]);
d25efa9c 152 exit (1);
377029eb 153 }
154 return 0;
155}
6599bf3a 156
157
158static char *
159do_mangle_classname (string)
160 const char *string;
161{
bca8957b 162 const char *ptr;
6599bf3a 163 int count = 0;
164
6599bf3a 165 obstack_grow (&name_obstack, "_ZN", 3);
166
bca8957b 167 for (ptr = string; *ptr; ptr++ )
6599bf3a 168 {
169 if (ptr[0] == '.')
170 {
bca8957b 171 append_gpp_mangled_name (&ptr [-count], count);
172 count = 0;
6599bf3a 173 }
174 else
175 count++;
176 }
bca8957b 177 append_gpp_mangled_name (&ptr [-count], count);
178 obstack_grow (mangle_obstack, "6class$E", 8);
7fd2eec7 179 obstack_1grow (mangle_obstack, '\0');
bca8957b 180 return obstack_finish (mangle_obstack);
6599bf3a 181}