Some changes from my thesis.
[cacao.git] / cacaoh.c
1 /* cacaoh.c - main for header generation (cacaoh)
2
3    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4    R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5    M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6    P. Tomsich, J. Wenninger
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Reinhard Grafl
28
29    Changes: Mark Probst
30             Philipp Tomsich
31             Christian Thalinger
32
33    $Id: cacaoh.c 1543 2004-11-18 12:20:19Z twisti $
34
35 */
36
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41
42 #include "config.h"
43 #include "types.h"
44 #include "global.h"
45 #include "headers.h"
46 #include "loader.h"
47 #include "options.h"
48 #include "tables.h"
49 #include "mm/boehm.h"
50 #include "threads/thread.h"
51 #include "toolbox/logging.h"
52 #include "toolbox/memory.h"
53
54
55 /* define cacaoh options ******************************************************/
56
57 #define OPT_HELP      2
58 #define OPT_VERSION   3
59 #define OPT_VERBOSE   4
60 #define OPT_DIRECTORY 5
61
62 opt_struct opts[] = {
63         { "help",             false, OPT_HELP      },
64         { "version",          false, OPT_VERSION   },
65         { "verbose",          false, OPT_VERBOSE   },
66         { "d",                true,  OPT_DIRECTORY },
67         { NULL,               false, 0 }
68 };
69
70
71 /* usage ***********************************************************************
72
73    Obviously prints usage information of cacaoh.
74
75 *******************************************************************************/
76
77 static void usage()
78 {
79         printf("Usage: cacaoh [options] <classes>\n"
80                    "\n"
81                    "Options:\n"
82                    "        -help           Print this message\n"
83                    "        -version        Print version information\n"
84                    "        -verbose        Enable verbose output\n"
85                    "        -d <dir>        Output directory\n");
86
87         /* exit with error code */
88
89         exit(1);
90 }
91
92
93 static void version()
94 {
95         printf("cacaoh "VERSION"\n");
96         exit(0);
97 }
98
99
100 /* main ************************************************************************
101
102    Main program.
103    
104 *******************************************************************************/
105
106 int main(int argc, char **argv)
107 {
108         s4 i, a;
109         char *cp;
110         classinfo *c;
111         char *opt_directory;
112
113         /********** internal (only used by main) *****************************/
114    
115         char classpath[500] = "";
116         u4 heapmaxsize = 2 * 1024 * 1024;
117         u4 heapstartsize = 100 * 1024;
118
119
120         /************ Collect some info from the environment *****************/
121
122         if (argc < 2)
123                 usage();
124
125         cp = getenv("CLASSPATH");
126         if (cp) {
127                 strcpy(classpath + strlen(classpath), ":");
128                 strcpy(classpath + strlen(classpath), cp);
129         }
130
131         /* initialize options with default values */
132
133         opt_verbose = false;
134         opt_directory = NULL;
135
136         while ((i = get_opt(argc, argv, opts)) != OPT_DONE) {
137                 switch (i) {
138                 case OPT_IGNORE:
139                         break;
140
141                 case OPT_HELP:
142                         usage();
143                         break;
144
145                 case OPT_VERSION:
146                         version();
147                         break;
148
149                 case OPT_VERBOSE:
150                         opt_verbose = true;
151                         break;
152
153                 case OPT_DIRECTORY:
154                         opt_directory = MNEW(char, strlen(opt_arg));
155                         strcpy(opt_directory, opt_arg);
156                         break;
157
158                 default:
159                         usage();
160                 }
161         }
162                         
163         /**************************** Program start **************************/
164
165         if (opt_verbose) {
166                 log_init(NULL);
167                 log_text("Java - header-generator started"); 
168         }
169         
170         /* initialize the garbage collector */
171         gc_init(heapmaxsize, heapstartsize);
172
173         tables_init();
174
175         suck_init(classpath);
176    
177 #if defined(USE_THREADS)
178 #if defined(NATIVE_THREADS)
179         initThreadsEarly();
180 #endif
181         initLocks();
182 #endif
183
184         loader_init();
185
186
187         /*********************** Load JAVA classes  **************************/
188         
189         nativemethod_chain = chain_new();
190         nativeclass_chain = chain_new();
191         
192         for (a = opt_ind; a < argc; a++) {
193                 cp = argv[a];
194
195                 /* convert classname */
196                 for (i = strlen(cp) - 1; i >= 0; i--) {
197                         switch (cp[i]) {
198                         case '.': cp[i]='/';
199                                 break;
200                         case '_': cp[i]='$';    
201                         }
202                 }
203         
204                 c = class_new(utf_new_char(cp));
205
206                 /* exceptions are catched with new_exception call */
207                 class_load(c);
208                 class_link(c);
209
210                 headerfile_generate(c, opt_directory);
211         }
212
213         /************************ Release all resources **********************/
214
215         loader_close();
216         tables_close(literalstring_free);
217
218         if (opt_verbose) {
219                 log_text("Java - header-generator stopped");
220                 log_cputime();
221                 mem_usagelog(1);
222         }
223         
224         return 0;
225 }
226
227
228 /*
229  * These are local overrides for various environment variables in Emacs.
230  * Please do not remove this and leave it at the end of the file, where
231  * Emacs will automagically detect them.
232  * ---------------------------------------------------------------------
233  * Local variables:
234  * mode: c
235  * indent-tabs-mode: t
236  * c-basic-offset: 4
237  * tab-width: 4
238  * End:
239  */