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
33 (* Generate ruby bindings. *)
34 let rec generate_ruby_c () =
35 generate_header CStyle LGPLv2plus;
44 #include \"guestfs.h\"
46 #include \"extconf.h\"
50 #define RARRAY_LEN(r) (RARRAY((r))->len)
55 #define RSTRING_LEN(r) (RSTRING((r))->len)
59 #define RSTRING_PTR(r) (RSTRING((r))->ptr)
62 static VALUE m_guestfs; /* guestfs module */
63 static VALUE c_guestfs; /* guestfs_h handle */
64 static VALUE e_Error; /* used for all errors */
66 static void ruby_event_callback_wrapper (guestfs_h *g, void *data, uint64_t event, int event_handle, int flags, const char *buf, size_t buf_len, const uint64_t *array, size_t array_len);
67 static VALUE ruby_event_callback_wrapper_wrapper (VALUE argv);
68 static VALUE ruby_event_callback_handle_exception (VALUE not_used, VALUE exn);
69 static VALUE **get_all_event_callbacks (guestfs_h *g, size_t *len_rtn);
72 ruby_guestfs_free (void *gvp)
77 /* As in the OCaml binding, there is a nasty, difficult to
78 * solve case here where the user deletes events in one of
79 * the callbacks that we are about to invoke, resulting in
83 VALUE **roots = get_all_event_callbacks (g, &len);
85 /* Close the handle: this could invoke callbacks from the list
86 * above, which is why we don't want to delete them before
91 /* Now unregister the global roots. */
92 for (i = 0; i < len; ++i) {
93 rb_gc_unregister_address (roots[i]);
102 * Guestfs::Guestfs.new() -> Guestfs::Guestfs
105 * +guestfs_create+[http://libguestfs.org/guestfs.3.html#guestfs_create]
106 * to create a new libguestfs handle. The handle is represented in
107 * Ruby as an instance of the Guestfs::Guestfs class.
110 ruby_guestfs_create (VALUE m)
114 g = guestfs_create ();
116 rb_raise (e_Error, \"failed to create guestfs handle\");
118 /* Don't print error messages to stderr by default. */
119 guestfs_set_error_handler (g, NULL, NULL);
121 /* Wrap it, and make sure the close function is called when the
124 return Data_Wrap_Struct (c_guestfs, NULL, ruby_guestfs_free, g);
132 * +guestfs_close+[http://libguestfs.org/guestfs.3.html#guestfs_close]
133 * to close the libguestfs handle.
136 ruby_guestfs_close (VALUE gv)
139 Data_Get_Struct (gv, guestfs_h, g);
141 ruby_guestfs_free (g);
142 DATA_PTR (gv) = NULL;
149 * g.set_event_callback(cb, event_bitmask) -> event_handle
152 * +guestfs_set_event_callback+[http://libguestfs.org/guestfs.3.html#guestfs_set_event_callback]
153 * to register an event callback. This returns an event handle.
156 ruby_set_event_callback (VALUE gv, VALUE cbv, VALUE event_bitmaskv)
159 uint64_t event_bitmask;
164 Data_Get_Struct (gv, guestfs_h, g);
166 event_bitmask = NUM2ULL (event_bitmaskv);
168 root = guestfs_safe_malloc (g, sizeof *root);
171 eh = guestfs_set_event_callback (g, ruby_event_callback_wrapper,
172 event_bitmask, 0, root);
175 rb_raise (e_Error, \"%%s\", guestfs_last_error (g));
178 rb_gc_register_address (root);
180 snprintf (key, sizeof key, \"_ruby_event_%%d\", eh);
181 guestfs_set_private (g, key, root);
188 * g.delete_event_callback(event_handle) -> nil
191 * +guestfs_delete_event_callback+[http://libguestfs.org/guestfs.3.html#guestfs_delete_event_callback]
192 * to delete an event callback.
195 ruby_delete_event_callback (VALUE gv, VALUE event_handlev)
199 int eh = NUM2INT (event_handlev);
202 Data_Get_Struct (gv, guestfs_h, g);
204 snprintf (key, sizeof key, \"_ruby_event_%%d\", eh);
206 root = guestfs_get_private (g, key);
208 rb_gc_unregister_address (root);
210 guestfs_set_private (g, key, NULL);
211 guestfs_delete_event_callback (g, eh);
218 ruby_event_callback_wrapper (guestfs_h *g,
223 const char *buf, size_t buf_len,
224 const uint64_t *array, size_t array_len)
227 VALUE eventv, event_handlev, bufv, arrayv;
230 eventv = ULL2NUM (event);
231 event_handlev = INT2NUM (event_handle);
233 bufv = rb_str_new (buf, buf_len);
235 arrayv = rb_ary_new2 (array_len);
236 for (i = 0; i < array_len; ++i)
237 rb_ary_push (arrayv, ULL2NUM (array[i]));
239 /* This is a crap limitation of rb_rescue.
240 * http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/~poffice/mail/ruby-talk/65698
242 argv[0] = * (VALUE *) data; /* function */
244 argv[2] = event_handlev;
248 rb_rescue (ruby_event_callback_wrapper_wrapper, (VALUE) argv,
249 ruby_event_callback_handle_exception, Qnil);
253 ruby_event_callback_wrapper_wrapper (VALUE argvv)
255 VALUE *argv = (VALUE *) argvv;
256 VALUE fn, eventv, event_handlev, bufv, arrayv;
260 /* Check the Ruby callback still exists. For reasons which are not
261 * fully understood, even though we registered this as a global root,
262 * it is still possible for the callback to go away (fn value remains
263 * but its type changes from T_DATA to T_NONE). (RHBZ#733297)
265 if (rb_type (fn) != T_NONE) {
267 event_handlev = argv[2];
271 rb_funcall (fn, rb_intern (\"call\"), 4,
272 eventv, event_handlev, bufv, arrayv);
279 ruby_event_callback_handle_exception (VALUE not_used, VALUE exn)
281 /* Callbacks aren't supposed to throw exceptions. The best we
282 * can do is to print the error.
284 fprintf (stderr, \"libguestfs: exception in callback: %%s\\n\",
285 StringValueCStr (exn));
291 get_all_event_callbacks (guestfs_h *g, size_t *len_rtn)
298 /* Count the length of the array that will be needed. */
300 root = guestfs_first_private (g, &key);
301 while (root != NULL) {
302 if (strncmp (key, \"_ruby_event_\", strlen (\"_ruby_event_\")) == 0)
304 root = guestfs_next_private (g, &key);
307 /* Copy them into the return array. */
308 r = guestfs_safe_malloc (g, sizeof (VALUE *) * (*len_rtn));
311 root = guestfs_first_private (g, &key);
312 while (root != NULL) {
313 if (strncmp (key, \"_ruby_event_\", strlen (\"_ruby_event_\")) == 0) {
317 root = guestfs_next_private (g, &key);
325 * g.user_cancel() -> nil
328 * +guestfs_user_cancel+[http://libguestfs.org/guestfs.3.html#guestfs_user_cancel]
329 * to cancel the current transfer. This is safe to call from Ruby
330 * signal handlers and threads.
333 ruby_user_cancel (VALUE gv)
337 Data_Get_Struct (gv, guestfs_h, g);
339 guestfs_user_cancel (g);
346 fun (name, (ret, args, optargs as style), _, flags, _, shortdesc, longdesc) ->
348 if not (List.mem NotInDocs flags); then (
349 let doc = replace_str longdesc "C<guestfs_" "C<g." in
351 if optargs <> [] then
352 doc ^ "\n\nOptional arguments are supplied in the final hash parameter, which is a hash of the argument name to its value. Pass an empty {} for no optional arguments."
355 if List.mem ProtocolLimitWarning flags then
356 doc ^ "\n\n" ^ protocol_limit_warning
359 match deprecation_notice flags with
361 | Some txt -> doc ^ "\n\n" ^ txt in
362 let doc = pod2text ~width:60 name doc in
363 let doc = String.concat "\n * " doc in
364 let doc = trim doc in
366 let args = List.map name_of_argt args in
367 let args = if optargs <> [] then args @ ["{optargs...}"] else args in
368 let args = String.concat ", " args in
373 | RBool _ -> "[True|False]"
375 | RInt64 _ -> "fixnum"
376 | RConstString _ -> "string"
377 | RConstOptString _ -> "string"
378 | RString _ -> "string"
379 | RBufferOut _ -> "string"
381 | RHashtable _ -> "hash"
383 | RStructList _ -> "list" in
394 * (For the C API documentation for this function, see
395 * +guestfs_%s+[http://libguestfs.org/guestfs.3.html#guestfs_%s]).
397 " name args ret shortdesc doc name name
400 (* Generate the function. *)
402 pr "ruby_guestfs_%s (VALUE gv" name;
403 List.iter (fun arg -> pr ", VALUE %sv" (name_of_argt arg)) args;
404 (* XXX This makes the hash mandatory, meaning that you have
405 * to specify {} for no arguments. We could make it so this
406 * can be omitted. However that is a load of hassle because
407 * you have to completely change the way that arguments are
409 * http://www.redhat.com/archives/libvir-list/2008-April/msg00004.html
411 if optargs <> [] then
412 pr ", VALUE optargsv";
415 pr " guestfs_h *g;\n";
416 pr " Data_Get_Struct (gv, guestfs_h, g);\n";
418 pr " rb_raise (rb_eArgError, \"%%s: used handle after closing it\", \"%s\");\n"
424 | Pathname n | Device n | Dev_or_Path n | String n | Key n
425 | FileIn n | FileOut n ->
426 pr " const char *%s = StringValueCStr (%sv);\n" n n;
428 pr " Check_Type (%sv, T_STRING);\n" n;
429 pr " const char *%s = RSTRING_PTR (%sv);\n" n n;
431 pr " rb_raise (rb_eTypeError, \"expected string for parameter %%s of %%s\",\n";
432 pr " \"%s\", \"%s\");\n" n name;
433 pr " size_t %s_size = RSTRING_LEN (%sv);\n" n n
435 pr " const char *%s = !NIL_P (%sv) ? StringValueCStr (%sv) : NULL;\n" n n n
436 | StringList n | DeviceList n ->
437 pr " char **%s;\n" n;
438 pr " Check_Type (%sv, T_ARRAY);\n" n;
440 pr " size_t i, len;\n";
441 pr " len = RARRAY_LEN (%sv);\n" n;
442 pr " %s = ALLOC_N (char *, len+1);\n"
444 pr " for (i = 0; i < len; ++i) {\n";
445 pr " VALUE v = rb_ary_entry (%sv, i);\n" n;
446 pr " %s[i] = StringValueCStr (v);\n" n;
448 pr " %s[len] = NULL;\n" n;
451 pr " int %s = RTEST (%sv);\n" n n
453 pr " int %s = NUM2INT (%sv);\n" n n
455 pr " long long %s = NUM2LL (%sv);\n" n n
457 pr " %s %s = (%s) (intptr_t) NUM2LL (%sv);\n" t n t n
461 (* Optional arguments are passed in a final hash parameter. *)
462 if optargs <> [] then (
463 let uc_name = String.uppercase name in
464 pr " Check_Type (optargsv, T_HASH);\n";
465 pr " struct guestfs_%s_argv optargs_s = { .bitmask = 0 };\n" name;
466 pr " struct guestfs_%s_argv *optargs = &optargs_s;\n" name;
470 let n = name_of_argt argt in
471 let uc_n = String.uppercase n in
472 pr " v = rb_hash_lookup (optargsv, ID2SYM (rb_intern (\"%s\")));\n" n;
473 pr " if (v != Qnil) {\n";
476 pr " optargs_s.%s = RTEST (v);\n" n;
478 pr " optargs_s.%s = NUM2INT (v);\n" n;
480 pr " optargs_s.%s = NUM2LL (v);\n" n;
482 pr " optargs_s.%s = StringValueCStr (v);\n" n
485 pr " optargs_s.bitmask |= GUESTFS_%s_%s_BITMASK;\n" uc_name uc_n;
492 | RErr | RInt _ | RBool _ -> pr " int r;\n"
493 | RInt64 _ -> pr " int64_t r;\n"
494 | RConstString _ | RConstOptString _ ->
495 pr " const char *r;\n"
496 | RString _ -> pr " char *r;\n"
497 | RStringList _ | RHashtable _ -> pr " char **r;\n"
498 | RStruct (_, typ) -> pr " struct guestfs_%s *r;\n" typ
499 | RStructList (_, typ) ->
500 pr " struct guestfs_%s_list *r;\n" typ
508 pr " r = guestfs_%s " name
510 pr " r = guestfs_%s_argv " name;
511 generate_c_call_args ~handle:"g" style;
516 | Pathname _ | Device _ | Dev_or_Path _ | String _ | Key _
517 | FileIn _ | FileOut _ | OptString _ | Bool _ | Int _ | Int64 _
518 | BufferIn _ | Pointer _ -> ()
519 | StringList n | DeviceList n ->
523 (match errcode_of_ret ret with
524 | `CannotReturnError -> ()
525 | `ErrorIsMinusOne ->
526 pr " if (r == -1)\n";
527 pr " rb_raise (e_Error, \"%%s\", guestfs_last_error (g));\n"
529 pr " if (r == NULL)\n";
530 pr " rb_raise (e_Error, \"%%s\", guestfs_last_error (g));\n"
537 | RInt _ | RBool _ ->
538 pr " return INT2NUM (r);\n"
540 pr " return ULL2NUM (r);\n"
542 pr " return rb_str_new2 (r);\n";
543 | RConstOptString _ ->
545 pr " return rb_str_new2 (r);\n";
547 pr " return Qnil;\n";
549 pr " VALUE rv = rb_str_new2 (r);\n";
553 pr " size_t i, len = 0;\n";
554 pr " for (i = 0; r[i] != NULL; ++i) len++;\n";
555 pr " VALUE rv = rb_ary_new2 (len);\n";
556 pr " for (i = 0; r[i] != NULL; ++i) {\n";
557 pr " rb_ary_push (rv, rb_str_new2 (r[i]));\n";
558 pr " free (r[i]);\n";
562 | RStruct (_, typ) ->
563 let cols = cols_of_struct typ in
564 generate_ruby_struct_code typ cols
565 | RStructList (_, typ) ->
566 let cols = cols_of_struct typ in
567 generate_ruby_struct_list_code typ cols
569 pr " VALUE rv = rb_hash_new ();\n";
571 pr " for (i = 0; r[i] != NULL; i+=2) {\n";
572 pr " rb_hash_aset (rv, rb_str_new2 (r[i]), rb_str_new2 (r[i+1]));\n";
573 pr " free (r[i]);\n";
574 pr " free (r[i+1]);\n";
579 pr " VALUE rv = rb_str_new (r, size);\n";
589 /* Initialize the module. */
590 void Init__guestfs ()
592 m_guestfs = rb_define_module (\"Guestfs\");
593 c_guestfs = rb_define_class_under (m_guestfs, \"Guestfs\", rb_cObject);
594 e_Error = rb_define_class_under (m_guestfs, \"Error\", rb_eStandardError);
596 #ifdef HAVE_RB_DEFINE_ALLOC_FUNC
597 rb_define_alloc_func (c_guestfs, ruby_guestfs_create);
600 rb_define_module_function (m_guestfs, \"create\", ruby_guestfs_create, 0);
601 rb_define_method (c_guestfs, \"close\", ruby_guestfs_close, 0);
602 rb_define_method (c_guestfs, \"set_event_callback\",
603 ruby_set_event_callback, 2);
604 rb_define_method (c_guestfs, \"delete_event_callback\",
605 ruby_delete_event_callback, 1);
606 rb_define_method (c_guestfs, \"user_cancel\",
607 ruby_user_cancel, 0);
613 fun (name, bitmask) ->
614 pr " rb_define_const (m_guestfs, \"EVENT_%s\",\n"
615 (String.uppercase name);
616 pr " ULL2NUM (UINT64_C (0x%x)));\n" bitmask;
622 fun (name, (_, args, optargs), _, _, _, _, _) ->
623 let nr_args = List.length args + if optargs <> [] then 1 else 0 in
624 pr " rb_define_method (c_guestfs, \"%s\",\n" name;
625 pr " ruby_guestfs_%s, %d);\n" name nr_args
630 (* Ruby code to return a struct. *)
631 and generate_ruby_struct_code typ cols =
632 pr " VALUE rv = rb_hash_new ();\n";
636 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), rb_str_new2 (r->%s));\n" name name
638 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), rb_str_new (r->%s, r->%s_len));\n" name name name
640 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), rb_str_new (r->%s, 32));\n" name name
641 | name, (FBytes|FUInt64) ->
642 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), ULL2NUM (r->%s));\n" name name
644 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), LL2NUM (r->%s));\n" name name
646 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), UINT2NUM (r->%s));\n" name name
648 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), INT2NUM (r->%s));\n" name name
649 | name, FOptPercent ->
650 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), rb_dbl2big (r->%s));\n" name name
651 | name, FChar -> (* XXX wrong? *)
652 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), ULL2NUM (r->%s));\n" name name
654 pr " guestfs_free_%s (r);\n" typ;
657 (* Ruby code to return a struct list. *)
658 and generate_ruby_struct_list_code typ cols =
659 pr " VALUE rv = rb_ary_new2 (r->len);\n";
661 pr " for (i = 0; i < r->len; ++i) {\n";
662 pr " VALUE hv = rb_hash_new ();\n";
666 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), rb_str_new2 (r->val[i].%s));\n" name name
668 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), rb_str_new (r->val[i].%s, r->val[i].%s_len));\n" name name name
670 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), rb_str_new (r->val[i].%s, 32));\n" name name
671 | name, (FBytes|FUInt64) ->
672 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), ULL2NUM (r->val[i].%s));\n" name name
674 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), LL2NUM (r->val[i].%s));\n" name name
676 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), UINT2NUM (r->val[i].%s));\n" name name
678 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), INT2NUM (r->val[i].%s));\n" name name
679 | name, FOptPercent ->
680 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), rb_dbl2big (r->val[i].%s));\n" name name
681 | name, FChar -> (* XXX wrong? *)
682 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), ULL2NUM (r->val[i].%s));\n" name name
684 pr " rb_ary_push (rv, hv);\n";
686 pr " guestfs_free_%s_list (r);\n" typ;