OSDN Git Service

Revert Rainer Orth's Java and gcc.c patch.
[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 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
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.
10
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.
15
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. 
20
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.  */
24
25 /* Written by Per Bothner <bothner@cygnus.com> */
26
27 #include "config.h"
28 #include "system.h"
29 #include "obstack.h"
30 #include "jcf.h"
31 #include "tree.h"
32 #include "java-tree.h"
33
34 static char * do_mangle_classname PARAMS ((const char *string));
35
36 struct obstack  name_obstack;
37 struct obstack *mangle_obstack = &name_obstack;
38
39 void
40 gcc_obstack_init (obstack)
41      struct obstack *obstack;
42 {
43   /* Let particular systems override the size of a chunk.  */
44 #ifndef OBSTACK_CHUNK_SIZE
45 #define OBSTACK_CHUNK_SIZE 0
46 #endif
47   /* Let them override the alloc and free routines too.  */
48 #ifndef OBSTACK_CHUNK_ALLOC
49 #define OBSTACK_CHUNK_ALLOC xmalloc
50 #endif
51 #ifndef OBSTACK_CHUNK_FREE
52 #define OBSTACK_CHUNK_FREE free
53 #endif
54   _obstack_begin (obstack, OBSTACK_CHUNK_SIZE, 0,
55                   (void *(*) PARAMS ((long))) OBSTACK_CHUNK_ALLOC,
56                   (void (*) PARAMS ((void *))) OBSTACK_CHUNK_FREE);
57 }
58
59 static void usage (const char *) ATTRIBUTE_NORETURN;
60
61 static void
62 usage (const char *name)
63 {
64   fprintf (stderr, "Usage: %s [OPTIONS]... CLASSNAME [OUTFILE]\n", name);
65   exit (1);
66 }
67
68 int
69 main (int argc, const char **argv)
70 {
71   const char *classname;
72   FILE *stream;
73   const char *mangled_classname;
74   int i, last_arg;
75
76   if (argc < 2)
77     usage (argv[0]);
78
79   for (i = 1; i < argc; ++i)
80     {
81       if (! strncmp (argv[i], "-D", 2))
82         {
83           /* Handled later.  */
84         }
85       else
86         break;
87     }
88
89   if (i < argc - 2 || i == argc)
90     usage (argv[0]);
91   last_arg = i;
92
93   classname = argv[i];
94
95   gcc_obstack_init (mangle_obstack);
96   mangled_classname = do_mangle_classname (classname);
97
98   if (i < argc - 1 && strcmp (argv[i + 1], "-") != 0)
99     {
100       const char *outfile = argv[i + 1];
101       stream = fopen (outfile, "w");
102       if (stream == NULL)
103         {
104           fprintf (stderr, "%s: Cannot open output file: %s\n",
105                    argv[0], outfile);
106           exit (1);
107         }
108     }
109   else
110     stream = stdout;
111
112   /* At this point every element of ARGV from 1 to LAST_ARG is a `-D'
113      option.  Process them appropriately.  */
114   fprintf (stream, "extern const char **_Jv_Compiler_Properties;\n");
115   fprintf (stream, "static const char *props[] =\n{\n");
116   for (i = 1; i < last_arg; ++i)
117     {
118       const char *p;
119       fprintf (stream, "  \"");
120       for (p = &argv[i][2]; *p; ++p)
121         {
122           if (! ISPRINT (*p))
123             fprintf (stream, "\\%o", *p);
124           else if (*p == '\\' || *p == '"')
125             fprintf (stream, "\\%c", *p);
126           else
127             putc (*p, stream);
128         }
129       fprintf (stream, "\",\n");
130     }
131   fprintf (stream, "  0\n};\n\n");
132
133   fprintf (stream, "extern int %s;\n", mangled_classname);
134   fprintf (stream, "int main (int argc, const char **argv)\n");
135   fprintf (stream, "{\n");
136   fprintf (stream, "   _Jv_Compiler_Properties = props;\n");
137   fprintf (stream, "   JvRunMain (&%s, argc, argv);\n", mangled_classname);
138   fprintf (stream, "}\n");
139   if (stream != stdout && fclose (stream) != 0)
140     {
141       fprintf (stderr, "%s: Failed to close output file %s\n",
142                argv[0], argv[2]);
143       exit (1);
144     }
145   return 0;
146 }
147
148
149 static char *
150 do_mangle_classname (string)
151      const char *string;
152 {
153   const char *ptr;
154   int count = 0;
155
156   obstack_grow (&name_obstack, "_ZN", 3);
157
158   for (ptr = string; *ptr; ptr++ )
159     {
160       if (ptr[0] == '.')
161         {
162           append_gpp_mangled_name (&ptr [-count], count);
163           count = 0;
164         }
165       else
166         count++;
167     }
168   append_gpp_mangled_name (&ptr [-count], count);
169   obstack_grow (mangle_obstack, "6class$E", 8);
170   obstack_1grow (mangle_obstack, '\0');
171   return obstack_finish (mangle_obstack);
172 }