2 * Copyright (C) 2009-2011 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 (* Please read generator/README first. *)
26 open Generator_docstrings
27 open Generator_optgroups
28 open Generator_actions
29 open Generator_structs
32 (* Generate Java bindings GuestFS.java file. *)
33 let rec generate_java_java () =
34 generate_header CStyle LGPLv2plus;
37 package com.redhat.et.libguestfs;
39 import java.util.HashMap;
41 import com.redhat.et.libguestfs.LibGuestFSException;
42 import com.redhat.et.libguestfs.PV;
43 import com.redhat.et.libguestfs.VG;
44 import com.redhat.et.libguestfs.LV;
45 import com.redhat.et.libguestfs.Stat;
46 import com.redhat.et.libguestfs.StatVFS;
47 import com.redhat.et.libguestfs.IntBool;
48 import com.redhat.et.libguestfs.Dirent;
51 * The GuestFS object is a libguestfs handle.
55 public class GuestFS {
56 // Load the native code.
58 System.loadLibrary (\"guestfs_jni\");
62 * The native guestfs_h pointer.
67 * Create a libguestfs handle.
69 * @throws LibGuestFSException
71 public GuestFS () throws LibGuestFSException
75 private native long _create () throws LibGuestFSException;
78 * Close a libguestfs handle.
80 * You can also leave handles to be collected by the garbage
81 * collector, but this method ensures that the resources used
82 * by the handle are freed up immediately. If you call any
83 * other methods after closing the handle, you will get an
86 * @throws LibGuestFSException
88 public void close () throws LibGuestFSException
94 private native void _close (long g) throws LibGuestFSException;
96 public void finalize () throws LibGuestFSException
104 fun (name, (ret, args, optargs as style), _, flags, _, shortdesc, longdesc) ->
105 if not (List.mem NotInDocs flags); then (
106 let doc = replace_str longdesc "C<guestfs_" "C<g." in
108 if optargs <> [] then
109 doc ^ "\n\nOptional arguments are supplied in the final Map<String,Object> parameter, which is a hash of the argument name to its value (cast to Object). Pass an empty Map for no optional arguments."
112 if List.mem ProtocolLimitWarning flags then
113 doc ^ "\n\n" ^ protocol_limit_warning
116 if List.mem DangerWillRobinson flags then
117 doc ^ "\n\n" ^ danger_will_robinson
120 match deprecation_notice flags with
122 | Some txt -> doc ^ "\n\n" ^ txt in
123 let doc = pod2text ~width:60 name doc in
124 let doc = List.map ( (* RHBZ#501883 *)
127 | nonempty -> nonempty
129 let doc = String.concat "\n * " doc in
132 pr " * %s\n" shortdesc;
135 pr " * @throws LibGuestFSException\n";
139 generate_java_prototype ~public:true ~semicolon:false name style;
143 pr " throw new LibGuestFSException (\"%s: handle is closed\");\n"
148 generate_java_call_args ~handle:"g" style;
151 pr " String[] r = _%s " name;
152 generate_java_call_args ~handle:"g" style;
155 pr " HashMap rhash = new HashMap ();\n";
156 pr " for (int i = 0; i < r.length; i += 2)\n";
157 pr " rhash.put (r[i], r[i+1]);\n";
158 pr " return rhash;\n"
160 pr " return _%s " name;
161 generate_java_call_args ~handle:"g" style;
166 generate_java_prototype ~privat:true ~native:true name style;
173 (* Generate Java call arguments, eg "(handle, foo, bar)" *)
174 and generate_java_call_args ~handle (_, args, optargs) =
176 List.iter (fun arg -> pr ", %s" (name_of_argt arg)) args;
177 if optargs <> [] then pr ", optargs";
180 and generate_java_prototype ?(public=false) ?(privat=false) ?(native=false)
181 ?(semicolon=true) name (ret, args, optargs) =
182 if privat then pr "private ";
183 if public then pr "public ";
184 if native then pr "native ";
188 | RErr -> pr "void ";
189 | RInt _ -> pr "int ";
190 | RInt64 _ -> pr "long ";
191 | RBool _ -> pr "boolean ";
192 | RConstString _ | RConstOptString _ | RString _
193 | RBufferOut _ -> pr "String ";
194 | RStringList _ -> pr "String[] ";
195 | RStruct (_, typ) ->
196 let name = java_name_of_struct typ in
198 | RStructList (_, typ) ->
199 let name = java_name_of_struct typ in
203 pr "Map<String,String> "
208 if native then pr "_%s " name else pr "%s " name;
210 let needs_comma = ref false in
219 if !needs_comma then pr ", ";
224 | Device n | Dev_or_Path n
233 | StringList n | DeviceList n ->
239 | Int64 n | Pointer (_, n) ->
243 if optargs <> [] then (
244 if !needs_comma then pr ", ";
250 pr " throws LibGuestFSException";
251 if semicolon then pr ";"
253 and generate_java_struct jtyp cols () =
254 generate_header CStyle LGPLv2plus;
257 package com.redhat.et.libguestfs;
260 * Libguestfs %s structure.
272 | name, FBuffer -> pr " public String %s;\n" name
273 | name, (FBytes|FUInt64|FInt64) -> pr " public long %s;\n" name
274 | name, (FUInt32|FInt32) -> pr " public int %s;\n" name
275 | name, FChar -> pr " public char %s;\n" name
276 | name, FOptPercent ->
277 pr " /* The next field is [0..100] or -1 meaning 'not present': */\n";
278 pr " public float %s;\n" name
283 and generate_java_c () =
284 generate_header CStyle LGPLv2plus;
291 #include \"com_redhat_et_libguestfs_GuestFS.h\"
292 #include \"guestfs.h\"
294 /* Note that this function returns. The exception is not thrown
295 * until after the wrapper function returns.
298 throw_exception (JNIEnv *env, const char *msg)
301 cl = (*env)->FindClass (env,
302 \"com/redhat/et/libguestfs/LibGuestFSException\");
303 (*env)->ThrowNew (env, cl, msg);
306 JNIEXPORT jlong JNICALL
307 Java_com_redhat_et_libguestfs_GuestFS__1create
308 (JNIEnv *env, jobject obj)
312 g = guestfs_create ();
314 throw_exception (env, \"GuestFS.create: failed to allocate handle\");
317 guestfs_set_error_handler (g, NULL, NULL);
318 return (jlong) (long) g;
321 JNIEXPORT void JNICALL
322 Java_com_redhat_et_libguestfs_GuestFS__1close
323 (JNIEnv *env, jobject obj, jlong jg)
325 guestfs_h *g = (guestfs_h *) (long) jg;
332 fun (name, (ret, args, optargs as style), _, _, _, _, _) ->
335 | RErr -> pr "void ";
336 | RInt _ -> pr "jint ";
337 | RInt64 _ -> pr "jlong ";
338 | RBool _ -> pr "jboolean ";
339 | RConstString _ | RConstOptString _ | RString _
340 | RBufferOut _ -> pr "jstring ";
341 | RStruct _ | RHashtable _ ->
343 | RStringList _ | RStructList _ ->
347 pr "Java_com_redhat_et_libguestfs_GuestFS_";
348 pr "%s" (replace_str ("_" ^ name) "_" "_1");
350 pr " (JNIEnv *env, jobject obj, jlong jg";
354 | Device n | Dev_or_Path n
362 pr ", jbyteArray j%s" n
363 | StringList n | DeviceList n ->
364 pr ", jobjectArray j%s" n
366 pr ", jboolean j%s" n
369 | Int64 n | Pointer (_, n) ->
372 if optargs <> [] then
373 pr ", jobject joptargs";
376 pr " guestfs_h *g = (guestfs_h *) (long) jg;\n";
378 | RErr -> pr " int r;\n"
380 | RInt _ -> pr " int r;\n"
381 | RInt64 _ -> pr " int64_t r;\n"
382 | RConstString _ -> pr " const char *r;\n"
383 | RConstOptString _ -> pr " const char *r;\n"
389 pr " jobjectArray jr;\n";
392 pr " jstring jstr;\n";
394 | RStruct (_, typ) ->
397 pr " jfieldID fl;\n";
398 pr " struct guestfs_%s *r;\n" typ
399 | RStructList (_, typ) ->
400 pr " jobjectArray jr;\n";
402 pr " jfieldID fl;\n";
403 pr " jobject jfl;\n";
404 pr " struct guestfs_%s_list *r;\n" typ
414 | Device n | Dev_or_Path n
420 pr " const char *%s;\n" n
422 pr " jbyte *%s;\n" n;
423 pr " size_t %s_size;\n" n
424 | StringList n | DeviceList n ->
425 pr " int %s_len;\n" n;
426 pr " const char **%s;\n" n
431 pr " int64_t %s;\n" n
438 | RStringList _ | RStructList _ | RHashtable _ -> true
439 | RErr | RBool _ | RInt _ | RInt64 _ | RConstString _
441 | RString _ | RBufferOut _ | RStruct _ -> false) ||
442 List.exists (function
443 | StringList _ -> true
444 | DeviceList _ -> true
445 | _ -> false) args in
451 (* Get the parameters. *)
455 | Device n | Dev_or_Path n
460 pr " %s = (*env)->GetStringUTFChars (env, j%s, NULL);\n" n n
462 (* This is completely undocumented, but Java null becomes
465 pr " %s = j%s ? (*env)->GetStringUTFChars (env, j%s, NULL) : NULL;\n" n n n
467 pr " %s = (*env)->GetByteArrayElements (env, j%s, NULL);\n" n n;
468 pr " %s_size = (*env)->GetArrayLength (env, j%s);\n" n n
469 | StringList n | DeviceList n ->
470 pr " %s_len = (*env)->GetArrayLength (env, j%s);\n" n n;
471 pr " %s = guestfs_safe_malloc (g, sizeof (char *) * (%s_len+1));\n" n n;
472 pr " for (i = 0; i < %s_len; ++i) {\n" n;
473 pr " jobject o = (*env)->GetObjectArrayElement (env, j%s, i);\n"
475 pr " %s[i] = (*env)->GetStringUTFChars (env, o, NULL);\n" n;
477 pr " %s[%s_len] = NULL;\n" n n;
481 pr " %s = j%s;\n" n n
483 pr " %s = (%s) j%s;\n" n t n
486 if optargs <> [] then (
488 pr " throw_exception (env, \"%s: internal error: please let us know how to read a Java HashMap parameter from JNI bindings!\");\n" name;
489 pr " return NULL;\n";
495 pr " r = guestfs_%s " name
497 pr " r = guestfs_%s_argv " name;
498 generate_c_call_args ~handle:"g" style;
501 (* Release the parameters. *)
505 | Device n | Dev_or_Path n
510 pr " (*env)->ReleaseStringUTFChars (env, j%s, %s);\n" n n
513 pr " (*env)->ReleaseStringUTFChars (env, j%s, %s);\n" n n
515 pr " (*env)->ReleaseByteArrayElements (env, j%s, %s, 0);\n" n n
516 | StringList n | DeviceList n ->
517 pr " for (i = 0; i < %s_len; ++i) {\n" n;
518 pr " jobject o = (*env)->GetObjectArrayElement (env, j%s, i);\n"
520 pr " (*env)->ReleaseStringUTFChars (env, o, %s[i]);\n" n;
529 (* Check for errors. *)
530 (match errcode_of_ret ret with
531 | `CannotReturnError -> ()
532 | `ErrorIsMinusOne ->
533 pr " if (r == -1) {\n";
534 pr " throw_exception (env, guestfs_last_error (g));\n";
538 pr " if (r == NULL) {\n";
539 pr " throw_exception (env, guestfs_last_error (g));\n";
540 pr " return NULL;\n";
547 | RInt _ -> pr " return (jint) r;\n"
548 | RBool _ -> pr " return (jboolean) r;\n"
549 | RInt64 _ -> pr " return (jlong) r;\n"
550 | RConstString _ -> pr " return (*env)->NewStringUTF (env, r);\n"
551 | RConstOptString _ ->
552 pr " return (*env)->NewStringUTF (env, r); /* XXX r NULL? */\n"
554 pr " jr = (*env)->NewStringUTF (env, r);\n";
559 pr " for (r_len = 0; r[r_len] != NULL; ++r_len) ;\n";
560 pr " cl = (*env)->FindClass (env, \"java/lang/String\");\n";
561 pr " jstr = (*env)->NewStringUTF (env, \"\");\n";
562 pr " jr = (*env)->NewObjectArray (env, r_len, cl, jstr);\n";
563 pr " for (i = 0; i < r_len; ++i) {\n";
564 pr " jstr = (*env)->NewStringUTF (env, r[i]);\n";
565 pr " (*env)->SetObjectArrayElement (env, jr, i, jstr);\n";
566 pr " free (r[i]);\n";
570 | RStruct (_, typ) ->
571 let jtyp = java_name_of_struct typ in
572 let cols = cols_of_struct typ in
573 generate_java_struct_return typ jtyp cols
574 | RStructList (_, typ) ->
575 let jtyp = java_name_of_struct typ in
576 let cols = cols_of_struct typ in
577 generate_java_struct_list_return typ jtyp cols
579 pr " jr = (*env)->NewStringUTF (env, r); // XXX size\n";
584 if optargs <> [] then
591 and generate_java_struct_return typ jtyp cols =
592 pr " cl = (*env)->FindClass (env, \"com/redhat/et/libguestfs/%s\");\n" jtyp;
593 pr " jr = (*env)->AllocObject (env, cl);\n";
597 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
598 pr " (*env)->SetObjectField (env, jr, fl, (*env)->NewStringUTF (env, r->%s));\n" name;
602 pr " memcpy (s, r->%s, 32);\n" name;
604 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
605 pr " (*env)->SetObjectField (env, jr, fl, (*env)->NewStringUTF (env, s));\n";
609 pr " int len = r->%s_len;\n" name;
610 pr " char s[len+1];\n";
611 pr " memcpy (s, r->%s, len);\n" name;
613 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
614 pr " (*env)->SetObjectField (env, jr, fl, (*env)->NewStringUTF (env, s));\n";
616 | name, (FBytes|FUInt64|FInt64) ->
617 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"J\");\n" name;
618 pr " (*env)->SetLongField (env, jr, fl, r->%s);\n" name;
619 | name, (FUInt32|FInt32) ->
620 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"I\");\n" name;
621 pr " (*env)->SetLongField (env, jr, fl, r->%s);\n" name;
622 | name, FOptPercent ->
623 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"F\");\n" name;
624 pr " (*env)->SetFloatField (env, jr, fl, r->%s);\n" name;
626 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"C\");\n" name;
627 pr " (*env)->SetLongField (env, jr, fl, r->%s);\n" name;
632 and generate_java_struct_list_return typ jtyp cols =
633 pr " cl = (*env)->FindClass (env, \"com/redhat/et/libguestfs/%s\");\n" jtyp;
634 pr " jr = (*env)->NewObjectArray (env, r->len, cl, NULL);\n";
635 pr " for (i = 0; i < r->len; ++i) {\n";
636 pr " jfl = (*env)->AllocObject (env, cl);\n";
640 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
641 pr " (*env)->SetObjectField (env, jfl, fl, (*env)->NewStringUTF (env, r->val[i].%s));\n" name;
645 pr " memcpy (s, r->val[i].%s, 32);\n" name;
647 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
648 pr " (*env)->SetObjectField (env, jfl, fl, (*env)->NewStringUTF (env, s));\n";
652 pr " int len = r->val[i].%s_len;\n" name;
653 pr " char s[len+1];\n";
654 pr " memcpy (s, r->val[i].%s, len);\n" name;
656 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"Ljava/lang/String;\");\n" name;
657 pr " (*env)->SetObjectField (env, jfl, fl, (*env)->NewStringUTF (env, s));\n";
659 | name, (FBytes|FUInt64|FInt64) ->
660 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"J\");\n" name;
661 pr " (*env)->SetLongField (env, jfl, fl, r->val[i].%s);\n" name;
662 | name, (FUInt32|FInt32) ->
663 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"I\");\n" name;
664 pr " (*env)->SetLongField (env, jfl, fl, r->val[i].%s);\n" name;
665 | name, FOptPercent ->
666 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"F\");\n" name;
667 pr " (*env)->SetFloatField (env, jfl, fl, r->val[i].%s);\n" name;
669 pr " fl = (*env)->GetFieldID (env, cl, \"%s\", \"C\");\n" name;
670 pr " (*env)->SetLongField (env, jfl, fl, r->val[i].%s);\n" name;
672 pr " (*env)->SetObjectArrayElement (env, jfl, i, jfl);\n";
674 pr " guestfs_free_%s_list (r);\n" typ;
677 and generate_java_makefile_inc () =
678 generate_header HashStyle GPLv2plus;
680 pr "java_built_sources = \\\n";
683 pr "\tcom/redhat/et/libguestfs/%s.java \\\n" jtyp;
685 pr "\tcom/redhat/et/libguestfs/GuestFS.java\n"