jvgenmain.c 4.6 KB
Newer Older
Anthony Green committed
1
/* Program to generate "main" a Java(TM) class containing a main method.
2
   Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
Anthony Green committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27

This file is part of GNU CC.

GNU CC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU CC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU CC; see the file COPYING.  If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. 

Java and all Java-based marks are trademarks or registered trademarks
of Sun Microsystems, Inc. in the United States and other countries.
The Free Software Foundation is independent of Sun Microsystems, Inc.  */

/* Written by Per Bothner <bothner@cygnus.com> */

#include "config.h"
28
#include "system.h"
Anthony Green committed
29
#include "obstack.h"
30 31 32
#include "jcf.h"
#include "tree.h"
#include "java-tree.h"
Anthony Green committed
33

34 35
static char * do_mangle_classname PARAMS ((const char *string));

36 37
struct obstack  name_obstack;
struct obstack *mangle_obstack = &name_obstack;
Anthony Green committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

void
gcc_obstack_init (obstack)
     struct obstack *obstack;
{
  /* Let particular systems override the size of a chunk.  */
#ifndef OBSTACK_CHUNK_SIZE
#define OBSTACK_CHUNK_SIZE 0
#endif
  /* Let them override the alloc and free routines too.  */
#ifndef OBSTACK_CHUNK_ALLOC
#define OBSTACK_CHUNK_ALLOC xmalloc
#endif
#ifndef OBSTACK_CHUNK_FREE
#define OBSTACK_CHUNK_FREE free
#endif
  _obstack_begin (obstack, OBSTACK_CHUNK_SIZE, 0,
55 56
		  (void *(*) PARAMS ((long))) OBSTACK_CHUNK_ALLOC,
		  (void (*) PARAMS ((void *))) OBSTACK_CHUNK_FREE);
Anthony Green committed
57 58
}

59 60
static void usage (const char *) ATTRIBUTE_NORETURN;

61 62 63
static void
usage (const char *name)
{
64
  fprintf (stderr, "Usage: %s [OPTIONS]... CLASSNAMEmain [OUTFILE]\n", name);
65 66 67
  exit (1);
}

Anthony Green committed
68
int
69
main (int argc, char **argv)
Anthony Green committed
70
{
71
  char *classname, *p;
Anthony Green committed
72
  FILE *stream;
73
  const char *mangled_classname;
74 75 76 77
  int i, last_arg;

  if (argc < 2)
    usage (argv[0]);
Anthony Green committed
78

79
  for (i = 1; i < argc; ++i)
Anthony Green committed
80
    {
81 82 83 84 85 86
      if (! strncmp (argv[i], "-D", 2))
	{
	  /* Handled later.  */
	}
      else
	break;
Anthony Green committed
87 88
    }

89 90 91 92 93
  if (i < argc - 2 || i == argc)
    usage (argv[0]);
  last_arg = i;

  classname = argv[i];
Anthony Green committed
94

95 96 97 98 99 100 101
  /* gcj always appends `main' to classname.  We need to strip this here.  */
  p = strrchr (classname, 'm');
  if (p == NULL || p == classname || strcmp (p, "main") != 0)
    usage (argv[0]);
  else
    *p = '\0';

102
  gcc_obstack_init (mangle_obstack);
103
  mangled_classname = do_mangle_classname (classname);
Anthony Green committed
104

105
  if (i < argc - 1 && strcmp (argv[i + 1], "-") != 0)
Anthony Green committed
106
    {
107
      const char *outfile = argv[i + 1];
Anthony Green committed
108 109 110 111 112
      stream = fopen (outfile, "w");
      if (stream == NULL)
	{
	  fprintf (stderr, "%s: Cannot open output file: %s\n",
		   argv[0], outfile);
113
	  exit (1);
Anthony Green committed
114 115 116 117
	}
    }
  else
    stream = stdout;
118 119 120

  /* At this point every element of ARGV from 1 to LAST_ARG is a `-D'
     option.  Process them appropriately.  */
121 122
  fprintf (stream, "extern const char **_Jv_Compiler_Properties;\n");
  fprintf (stream, "static const char *props[] =\n{\n");
123 124 125 126 127 128
  for (i = 1; i < last_arg; ++i)
    {
      const char *p;
      fprintf (stream, "  \"");
      for (p = &argv[i][2]; *p; ++p)
	{
Zack Weinberg committed
129
	  if (! ISPRINT (*p))
130 131 132 133 134 135 136 137 138 139
	    fprintf (stream, "\\%o", *p);
	  else if (*p == '\\' || *p == '"')
	    fprintf (stream, "\\%c", *p);
	  else
	    putc (*p, stream);
	}
      fprintf (stream, "\",\n");
    }
  fprintf (stream, "  0\n};\n\n");

140
  fprintf (stream, "extern int %s;\n", mangled_classname);
Anthony Green committed
141 142
  fprintf (stream, "int main (int argc, const char **argv)\n");
  fprintf (stream, "{\n");
143
  fprintf (stream, "   _Jv_Compiler_Properties = props;\n");
144
  fprintf (stream, "   JvRunMain (&%s, argc, argv);\n", mangled_classname);
Anthony Green committed
145 146 147 148 149
  fprintf (stream, "}\n");
  if (stream != stdout && fclose (stream) != 0)
    {
      fprintf (stderr, "%s: Failed to close output file %s\n",
	       argv[0], argv[2]);
150
      exit (1);
Anthony Green committed
151 152 153
    }
  return 0;
}
154 155 156 157 158 159


static char *
do_mangle_classname (string)
     const char *string;
{
160
  const char *ptr;
161 162 163 164
  int count = 0;

  obstack_grow (&name_obstack, "_ZN", 3);

165
  for (ptr = string; *ptr; ptr++ )
166 167 168
    {
      if (ptr[0] == '.')
	{
169 170
	  append_gpp_mangled_name (&ptr [-count], count);
	  count = 0;
171 172 173 174
	}
      else
	count++;
    }
175 176
  append_gpp_mangled_name (&ptr [-count], count);
  obstack_grow (mangle_obstack, "6class$E", 8);
177
  obstack_1grow (mangle_obstack, '\0');
178
  return obstack_finish (mangle_obstack);
179
}