File: wrapper.c

package info (click to toggle)
icepick 0.01-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 19,608 kB
  • ctags: 28,706
  • sloc: java: 134,567; sh: 7,395; xml: 1,565; makefile: 530; ansic: 217
file content (167 lines) | stat: -rw-r--r-- 3,934 bytes parent folder | download
1
2
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* wrapper.c - IcePick tools wrapper
   Copyright (C) 2008 Andrew John Hughes

This file is part of IcePick.

IcePick 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.
 
IcePick 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 IcePick; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.
*/

#include "wrapper.h"

#ifdef USE_JNI
int invoke_via_jni(char** vm_options, int vm_count,
		   char** main_options, int main_count)
{
  JNIEnv *env;
  JavaVM *vm;
  JavaVMInitArgs vm_args;
  JavaVMOption options[vm_count];
  jint res;
  jclass cls;
  jmethodID main_method;
  jclass string_class;
  jobjectArray args;
  int a;

  for (a = 0; a < vm_count; ++a)
    options[a].optionString = *vm_options++;
#ifdef DEBUG
  for (a = 0; a < vm_count; ++a)
    printf("vm option %d: %s\n", a, options[a].optionString);
#endif

  vm_args.version = JNI_VERSION_1_2;
  vm_args.ignoreUnrecognized = JNI_TRUE;
  vm_args.options = options;
  vm_args.nOptions = vm_count;

  res = JNI_CreateJavaVM(&vm, (void**)&env, &vm_args);
  if (res < 0)
    {
      fprintf(stderr, "Couldn't create Java VM\n");
      exit(-1);
    }
  cls = (*env)->FindClass(env, "CLASS_NAME");
  if (cls == NULL) {
    goto destroy;
  }
 
  main_method = (*env)->GetStaticMethodID(env, cls, "main",
					  "([Ljava/lang/String;)V");
  if (main_method == NULL) {
    goto destroy;
  }

  string_class = (*env)->FindClass(env, "java/lang/String");
  args = (*env)->NewObjectArray(env, main_count, string_class, NULL);
  if (args == NULL) {
    goto destroy;
  }

  for (a = 0; a < main_count; ++a)
    {
      jstring jstr;

      jstr = (*env)->NewStringUTF(env, *argv);
      if (jstr == NULL) 
	goto destroy;
      ++argv;
      (*env)->SetObjectArrayElement(env, args, a, jstr);
    }

  (*env)->CallStaticVoidMethod(env, cls, main_method, args);
 
 destroy:
  if ((*env)->ExceptionOccurred(env)) 
    (*env)->ExceptionDescribe(env);
  return (*vm)->DestroyJavaVM(vm);
}
#endif

int invoke_via_exec(char** vm_options, int vm_count,
		    char** main_options, int main_count)
{
  char* argv[vm_count + main_count + 5];
  char** argv_pos;
  int a;

  argv_pos = argv;
  *argv_pos++ = (char*) VM_BINARY;
  *argv_pos++ = (char*) "-classpath";
  *argv_pos++ = (char*) TOOLS_CP;
  
  for (a = 0; a < vm_count; ++a)
    *argv_pos++ = *vm_options++;

  *argv_pos++ = (char*) MAIN_METHOD;

  for (a = 0; a < main_count; ++a)
    *argv_pos++ = *main_options++;
  
#ifdef DEBUG
  for (a = 0; a < vm_count + main_count + 4; ++a)
    printf("cmd line option %d: %s\n", a, argv[a]);
#endif

  *argv_pos++ = NULL;

  int res = execvp(VM_BINARY, argv);
  if (res)
    perror("Error invoking VM");
  return res;
}

int main(int argc, char** argv)
{
  char** read_pos;
  char** write_pos;
  int a;
  int vm_count = 0;
  int main_count = 0;
  char* vm_opt[argc-1];

  read_pos = argv + 1;
  write_pos = argv;
  for (a = 0; a < argc - 1; ++a)
    {
      if (strncmp(*read_pos, "-J", (size_t)2) == 0)
	{
	  vm_opt[vm_count] = *read_pos + 2;
	  ++vm_count;
	}
      else
	{
	  *write_pos = *read_pos;
	  ++write_pos;
	  ++main_count;
	}
      ++read_pos;
    }
#ifdef DEBUG
  for (a = 0; a < main_count; ++a)
    printf("main option %d: %s\n", a, *argv++);
  for (a = 0; a < vm_count; ++a)
    printf("vm option %d: %s\n", a, vm_opt[a]);
#endif

#ifdef USE_JNI
  return invoke_via_jni(vm_opt, vm_count, argv, main_count);
#else
  return invoke_via_exec(vm_opt, vm_count, argv, main_count);
#endif

}