2001-02-14 Tom Tromey <tromey@redhat.com>
[official-gcc.git] / gcc / java / jvgenmain.c
blob414ccded13ad7a92476838ce861b592e83c22c8a
1 /* Program to generate "main" a Java(TM) class containing a main method.
2 Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC 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 2, or (at your option)
9 any later version.
11 GNU CC 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 GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
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 "obstack.h"
30 #include "gansidecl.h"
31 #include "jcf.h"
32 #include "tree.h"
33 #include "java-tree.h"
35 static char * do_mangle_classname PARAMS ((const char *string));
37 struct obstack name_obstack;
38 struct obstack *mangle_obstack = &name_obstack;
40 void
41 gcc_obstack_init (obstack)
42 struct obstack *obstack;
44 /* Let particular systems override the size of a chunk. */
45 #ifndef OBSTACK_CHUNK_SIZE
46 #define OBSTACK_CHUNK_SIZE 0
47 #endif
48 /* Let them override the alloc and free routines too. */
49 #ifndef OBSTACK_CHUNK_ALLOC
50 #define OBSTACK_CHUNK_ALLOC xmalloc
51 #endif
52 #ifndef OBSTACK_CHUNK_FREE
53 #define OBSTACK_CHUNK_FREE free
54 #endif
55 _obstack_begin (obstack, OBSTACK_CHUNK_SIZE, 0,
56 (void *(*) PARAMS ((long))) OBSTACK_CHUNK_ALLOC,
57 (void (*) PARAMS ((void *))) OBSTACK_CHUNK_FREE);
60 static void usage (const char *) ATTRIBUTE_NORETURN;
62 static void
63 usage (const char *name)
65 fprintf (stderr, "Usage: %s [OPTIONS]... CLASSNAME [OUTFILE]\n", name);
66 exit (1);
69 int
70 main (int argc, const char **argv)
72 const char *classname;
73 FILE *stream;
74 const char *mangled_classname;
75 int i, last_arg;
77 if (argc < 2)
78 usage (argv[0]);
80 for (i = 1; i < argc; ++i)
82 if (! strncmp (argv[i], "-D", 2))
84 /* Handled later. */
86 else
87 break;
90 if (i < argc - 2 || i == argc)
91 usage (argv[0]);
92 last_arg = i;
94 classname = argv[i];
96 gcc_obstack_init (mangle_obstack);
97 mangled_classname = do_mangle_classname (classname);
99 if (i < argc - 1 && strcmp (argv[i + 1], "-") != 0)
101 const char *outfile = argv[i + 1];
102 stream = fopen (outfile, "w");
103 if (stream == NULL)
105 fprintf (stderr, "%s: Cannot open output file: %s\n",
106 argv[0], outfile);
107 exit (1);
110 else
111 stream = stdout;
113 /* At this point every element of ARGV from 1 to LAST_ARG is a `-D'
114 option. Process them appropriately. */
115 fprintf (stream, "extern const char **_Jv_Compiler_Properties;\n");
116 fprintf (stream, "static const char *props[] =\n{\n");
117 for (i = 1; i < last_arg; ++i)
119 const char *p;
120 fprintf (stream, " \"");
121 for (p = &argv[i][2]; *p; ++p)
123 if (! ISPRINT (*p))
124 fprintf (stream, "\\%o", *p);
125 else if (*p == '\\' || *p == '"')
126 fprintf (stream, "\\%c", *p);
127 else
128 putc (*p, stream);
130 fprintf (stream, "\",\n");
132 fprintf (stream, " 0\n};\n\n");
134 fprintf (stream, "extern int %s;\n", mangled_classname);
135 fprintf (stream, "int main (int argc, const char **argv)\n");
136 fprintf (stream, "{\n");
137 fprintf (stream, " _Jv_Compiler_Properties = props;\n");
138 fprintf (stream, " JvRunMain (&%s, argc, argv);\n", mangled_classname);
139 fprintf (stream, "}\n");
140 if (stream != stdout && fclose (stream) != 0)
142 fprintf (stderr, "%s: Failed to close output file %s\n",
143 argv[0], argv[2]);
144 exit (1);
146 return 0;
150 static char *
151 do_mangle_classname (string)
152 const char *string;
154 const char *ptr;
155 int count = 0;
157 obstack_grow (&name_obstack, "_ZN", 3);
159 for (ptr = string; *ptr; ptr++ )
161 if (ptr[0] == '.')
163 append_gpp_mangled_name (&ptr [-count], count);
164 count = 0;
166 else
167 count++;
169 append_gpp_mangled_name (&ptr [-count], count);
170 obstack_grow (mangle_obstack, "6class$E", 8);
171 return obstack_finish (mangle_obstack);