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)
53 static VALUE m_guestfs; /* guestfs module */
54 static VALUE c_guestfs; /* guestfs_h handle */
55 static VALUE e_Error; /* used for all errors */
57 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);
58 static VALUE ruby_event_callback_wrapper_wrapper (VALUE argv);
59 static VALUE ruby_event_callback_handle_exception (VALUE not_used, VALUE exn);
60 static VALUE **get_all_event_callbacks (guestfs_h *g, size_t *len_rtn);
63 ruby_guestfs_free (void *gvp)
68 /* As in the OCaml binding, there is a nasty, difficult to
69 * solve case here where the user deletes events in one of
70 * the callbacks that we are about to invoke, resulting in
74 VALUE **roots = get_all_event_callbacks (g, &len);
76 /* Close the handle: this could invoke callbacks from the list
77 * above, which is why we don't want to delete them before
82 /* Now unregister the global roots. */
83 for (i = 0; i < len; ++i) {
84 rb_gc_unregister_address (roots[i]);
92 * Guestfs::Guestfs.new() -> Guestfs::Guestfs
95 * +guestfs_create+[http://libguestfs.org/guestfs.3.html#guestfs_create]
96 * to create a new libguestfs handle. The handle is represented in
97 * Ruby as an instance of the Guestfs::Guestfs class.
100 ruby_guestfs_create (VALUE m)
104 g = guestfs_create ();
106 rb_raise (e_Error, \"failed to create guestfs handle\");
108 /* Don't print error messages to stderr by default. */
109 guestfs_set_error_handler (g, NULL, NULL);
111 /* Wrap it, and make sure the close function is called when the
114 return Data_Wrap_Struct (c_guestfs, NULL, ruby_guestfs_free, g);
122 * +guestfs_close+[http://libguestfs.org/guestfs.3.html#guestfs_close]
123 * to close the libguestfs handle.
126 ruby_guestfs_close (VALUE gv)
129 Data_Get_Struct (gv, guestfs_h, g);
131 ruby_guestfs_free (g);
132 DATA_PTR (gv) = NULL;
139 * g.set_event_callback(cb, event_bitmask) -> event_handle
142 * +guestfs_set_event_callback+[http://libguestfs.org/guestfs.3.html#guestfs_set_event_callback]
143 * to register an event callback. This returns an event handle.
146 ruby_set_event_callback (VALUE gv, VALUE cbv, VALUE event_bitmaskv)
149 uint64_t event_bitmask;
154 Data_Get_Struct (gv, guestfs_h, g);
156 event_bitmask = NUM2ULL (event_bitmaskv);
158 root = guestfs_safe_malloc (g, sizeof *root);
161 eh = guestfs_set_event_callback (g, ruby_event_callback_wrapper,
162 event_bitmask, 0, root);
165 rb_raise (e_Error, \"%%s\", guestfs_last_error (g));
168 rb_gc_register_address (root);
170 snprintf (key, sizeof key, \"_ruby_event_%%d\", eh);
171 guestfs_set_private (g, key, root);
178 * g.delete_event_callback(event_handle) -> nil
181 * +guestfs_delete_event_callback+[http://libguestfs.org/guestfs.3.html#guestfs_delete_event_callback]
182 * to delete an event callback.
185 ruby_delete_event_callback (VALUE gv, VALUE event_handlev)
189 int eh = NUM2INT (event_handlev);
192 Data_Get_Struct (gv, guestfs_h, g);
194 snprintf (key, sizeof key, \"_ruby_event_%%d\", eh);
196 root = guestfs_get_private (g, key);
198 rb_gc_unregister_address (root);
200 guestfs_set_private (g, key, NULL);
201 guestfs_delete_event_callback (g, eh);
208 ruby_event_callback_wrapper (guestfs_h *g,
213 const char *buf, size_t buf_len,
214 const uint64_t *array, size_t array_len)
217 VALUE eventv, event_handlev, bufv, arrayv, argv;
219 eventv = ULL2NUM (event);
220 event_handlev = INT2NUM (event_handle);
222 bufv = rb_str_new (buf, buf_len);
224 arrayv = rb_ary_new2 (array_len);
225 for (i = 0; i < array_len; ++i)
226 rb_ary_push (arrayv, ULL2NUM (array[i]));
228 /* Wrap up the arguments in an array which will be unpacked
229 * and passed as multiple arguments. This is a crap limitation
231 * http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/~poffice/mail/ruby-talk/65698
233 argv = rb_ary_new2 (5);
234 rb_ary_store (argv, 0, * (VALUE *) data /* function */);
235 rb_ary_store (argv, 1, eventv);
236 rb_ary_store (argv, 2, event_handlev);
237 rb_ary_store (argv, 3, bufv);
238 rb_ary_store (argv, 4, arrayv);
240 rb_rescue (ruby_event_callback_wrapper_wrapper, argv,
241 ruby_event_callback_handle_exception, Qnil);
245 ruby_event_callback_wrapper_wrapper (VALUE argv)
247 VALUE fn, eventv, event_handlev, bufv, arrayv;
249 fn = rb_ary_entry (argv, 0);
250 eventv = rb_ary_entry (argv, 1);
251 event_handlev = rb_ary_entry (argv, 2);
252 bufv = rb_ary_entry (argv, 3);
253 arrayv = rb_ary_entry (argv, 4);
255 rb_funcall (fn, rb_intern (\"call\"), 4,
256 eventv, event_handlev, bufv, arrayv);
262 ruby_event_callback_handle_exception (VALUE not_used, VALUE exn)
264 /* Callbacks aren't supposed to throw exceptions. The best we
265 * can do is to print the error.
267 fprintf (stderr, \"libguestfs: exception in callback: %%s\",
268 StringValueCStr (exn));
274 get_all_event_callbacks (guestfs_h *g, size_t *len_rtn)
281 /* Count the length of the array that will be needed. */
283 root = guestfs_first_private (g, &key);
284 while (root != NULL) {
285 if (strncmp (key, \"_ruby_event_\", strlen (\"_ruby_event_\")) == 0)
287 root = guestfs_next_private (g, &key);
290 /* Copy them into the return array. */
291 r = guestfs_safe_malloc (g, sizeof (VALUE *) * (*len_rtn));
294 root = guestfs_first_private (g, &key);
295 while (root != NULL) {
296 if (strncmp (key, \"_ruby_event_\", strlen (\"_ruby_event_\")) == 0) {
300 root = guestfs_next_private (g, &key);
309 fun (name, (ret, args, optargs as style), _, flags, _, shortdesc, longdesc) ->
311 if not (List.mem NotInDocs flags); then (
312 let doc = replace_str longdesc "C<guestfs_" "C<g." in
314 if optargs <> [] then
315 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."
318 if List.mem ProtocolLimitWarning flags then
319 doc ^ "\n\n" ^ protocol_limit_warning
322 if List.mem DangerWillRobinson flags then
323 doc ^ "\n\n" ^ danger_will_robinson
326 match deprecation_notice flags with
328 | Some txt -> doc ^ "\n\n" ^ txt in
329 let doc = pod2text ~width:60 name doc in
330 let doc = String.concat "\n * " doc in
331 let doc = trim doc in
333 let args = List.map name_of_argt args in
334 let args = if optargs <> [] then args @ ["{optargs...}"] else args in
335 let args = String.concat ", " args in
340 | RBool _ -> "[True|False]"
342 | RInt64 _ -> "fixnum"
343 | RConstString _ -> "string"
344 | RConstOptString _ -> "string"
345 | RString _ -> "string"
346 | RBufferOut _ -> "string"
348 | RHashtable _ -> "hash"
350 | RStructList _ -> "list" in
361 * (For the C API documentation for this function, see
362 * +guestfs_%s+[http://libguestfs.org/guestfs.3.html#guestfs_%s]).
364 " name args ret shortdesc doc name name
367 (* Generate the function. *)
369 pr "ruby_guestfs_%s (VALUE gv" name;
370 List.iter (fun arg -> pr ", VALUE %sv" (name_of_argt arg)) args;
371 (* XXX This makes the hash mandatory, meaning that you have
372 * to specify {} for no arguments. We could make it so this
373 * can be omitted. However that is a load of hassle because
374 * you have to completely change the way that arguments are
376 * http://www.redhat.com/archives/libvir-list/2008-April/msg00004.html
378 if optargs <> [] then
379 pr ", VALUE optargsv";
382 pr " guestfs_h *g;\n";
383 pr " Data_Get_Struct (gv, guestfs_h, g);\n";
385 pr " rb_raise (rb_eArgError, \"%%s: used handle after closing it\", \"%s\");\n"
391 | Pathname n | Device n | Dev_or_Path n | String n | Key n
392 | FileIn n | FileOut n ->
393 pr " const char *%s = StringValueCStr (%sv);\n" n n;
395 pr " Check_Type (%sv, T_STRING);\n" n;
396 pr " const char *%s = RSTRING (%sv)->ptr;\n" n n;
398 pr " rb_raise (rb_eTypeError, \"expected string for parameter %%s of %%s\",\n";
399 pr " \"%s\", \"%s\");\n" n name;
400 pr " size_t %s_size = RSTRING (%sv)->len;\n" n n
402 pr " const char *%s = !NIL_P (%sv) ? StringValueCStr (%sv) : NULL;\n" n n n
403 | StringList n | DeviceList n ->
404 pr " char **%s;\n" n;
405 pr " Check_Type (%sv, T_ARRAY);\n" n;
407 pr " size_t i, len;\n";
408 pr " len = RARRAY_LEN (%sv);\n" n;
409 pr " %s = ALLOC_N (char *, len+1);\n"
411 pr " for (i = 0; i < len; ++i) {\n";
412 pr " VALUE v = rb_ary_entry (%sv, i);\n" n;
413 pr " %s[i] = StringValueCStr (v);\n" n;
415 pr " %s[len] = NULL;\n" n;
418 pr " int %s = RTEST (%sv);\n" n n
420 pr " int %s = NUM2INT (%sv);\n" n n
422 pr " long long %s = NUM2LL (%sv);\n" n n
424 pr " %s %s = (%s) (intptr_t) NUM2LL (%sv);\n" t n t n
428 (* Optional arguments are passed in a final hash parameter. *)
429 if optargs <> [] then (
430 let uc_name = String.uppercase name in
431 pr " Check_Type (optargsv, T_HASH);\n";
432 pr " struct guestfs_%s_argv optargs_s = { .bitmask = 0 };\n" name;
433 pr " struct guestfs_%s_argv *optargs = &optargs_s;\n" name;
437 let n = name_of_argt argt in
438 let uc_n = String.uppercase n in
439 pr " v = rb_hash_lookup (optargsv, ID2SYM (rb_intern (\"%s\")));\n" n;
440 pr " if (v != Qnil) {\n";
443 pr " optargs_s.%s = RTEST (v);\n" n;
445 pr " optargs_s.%s = NUM2INT (v);\n" n;
447 pr " optargs_s.%s = NUM2LL (v);\n" n;
449 pr " optargs_s.%s = StringValueCStr (v);\n" n
452 pr " optargs_s.bitmask |= GUESTFS_%s_%s_BITMASK;\n" uc_name uc_n;
459 | RErr | RInt _ | RBool _ -> pr " int r;\n"
460 | RInt64 _ -> pr " int64_t r;\n"
461 | RConstString _ | RConstOptString _ ->
462 pr " const char *r;\n"
463 | RString _ -> pr " char *r;\n"
464 | RStringList _ | RHashtable _ -> pr " char **r;\n"
465 | RStruct (_, typ) -> pr " struct guestfs_%s *r;\n" typ
466 | RStructList (_, typ) ->
467 pr " struct guestfs_%s_list *r;\n" typ
475 pr " r = guestfs_%s " name
477 pr " r = guestfs_%s_argv " name;
478 generate_c_call_args ~handle:"g" style;
483 | Pathname _ | Device _ | Dev_or_Path _ | String _ | Key _
484 | FileIn _ | FileOut _ | OptString _ | Bool _ | Int _ | Int64 _
485 | BufferIn _ | Pointer _ -> ()
486 | StringList n | DeviceList n ->
490 (match errcode_of_ret ret with
491 | `CannotReturnError -> ()
492 | `ErrorIsMinusOne ->
493 pr " if (r == -1)\n";
494 pr " rb_raise (e_Error, \"%%s\", guestfs_last_error (g));\n"
496 pr " if (r == NULL)\n";
497 pr " rb_raise (e_Error, \"%%s\", guestfs_last_error (g));\n"
504 | RInt _ | RBool _ ->
505 pr " return INT2NUM (r);\n"
507 pr " return ULL2NUM (r);\n"
509 pr " return rb_str_new2 (r);\n";
510 | RConstOptString _ ->
512 pr " return rb_str_new2 (r);\n";
514 pr " return Qnil;\n";
516 pr " VALUE rv = rb_str_new2 (r);\n";
520 pr " size_t i, len = 0;\n";
521 pr " for (i = 0; r[i] != NULL; ++i) len++;\n";
522 pr " VALUE rv = rb_ary_new2 (len);\n";
523 pr " for (i = 0; r[i] != NULL; ++i) {\n";
524 pr " rb_ary_push (rv, rb_str_new2 (r[i]));\n";
525 pr " free (r[i]);\n";
529 | RStruct (_, typ) ->
530 let cols = cols_of_struct typ in
531 generate_ruby_struct_code typ cols
532 | RStructList (_, typ) ->
533 let cols = cols_of_struct typ in
534 generate_ruby_struct_list_code typ cols
536 pr " VALUE rv = rb_hash_new ();\n";
538 pr " for (i = 0; r[i] != NULL; i+=2) {\n";
539 pr " rb_hash_aset (rv, rb_str_new2 (r[i]), rb_str_new2 (r[i+1]));\n";
540 pr " free (r[i]);\n";
541 pr " free (r[i+1]);\n";
546 pr " VALUE rv = rb_str_new (r, size);\n";
556 /* Initialize the module. */
557 void Init__guestfs ()
559 m_guestfs = rb_define_module (\"Guestfs\");
560 c_guestfs = rb_define_class_under (m_guestfs, \"Guestfs\", rb_cObject);
561 e_Error = rb_define_class_under (m_guestfs, \"Error\", rb_eStandardError);
563 #ifdef HAVE_RB_DEFINE_ALLOC_FUNC
564 rb_define_alloc_func (c_guestfs, ruby_guestfs_create);
567 rb_define_module_function (m_guestfs, \"create\", ruby_guestfs_create, 0);
568 rb_define_method (c_guestfs, \"close\", ruby_guestfs_close, 0);
569 rb_define_method (c_guestfs, \"set_event_callback\",
570 ruby_set_event_callback, 2);
571 rb_define_method (c_guestfs, \"delete_event_callback\",
572 ruby_delete_event_callback, 1);
578 fun (name, bitmask) ->
579 pr " rb_define_const (m_guestfs, \"EVENT_%s\",\n"
580 (String.uppercase name);
581 pr " ULL2NUM (UINT64_C (0x%x)));\n" bitmask;
587 fun (name, (_, args, optargs), _, _, _, _, _) ->
588 let nr_args = List.length args + if optargs <> [] then 1 else 0 in
589 pr " rb_define_method (c_guestfs, \"%s\",\n" name;
590 pr " ruby_guestfs_%s, %d);\n" name nr_args
595 (* Ruby code to return a struct. *)
596 and generate_ruby_struct_code typ cols =
597 pr " VALUE rv = rb_hash_new ();\n";
601 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), rb_str_new2 (r->%s));\n" name name
603 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), rb_str_new (r->%s, r->%s_len));\n" name name name
605 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), rb_str_new (r->%s, 32));\n" name name
606 | name, (FBytes|FUInt64) ->
607 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), ULL2NUM (r->%s));\n" name name
609 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), LL2NUM (r->%s));\n" name name
611 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), UINT2NUM (r->%s));\n" name name
613 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), INT2NUM (r->%s));\n" name name
614 | name, FOptPercent ->
615 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), rb_dbl2big (r->%s));\n" name name
616 | name, FChar -> (* XXX wrong? *)
617 pr " rb_hash_aset (rv, rb_str_new2 (\"%s\"), ULL2NUM (r->%s));\n" name name
619 pr " guestfs_free_%s (r);\n" typ;
622 (* Ruby code to return a struct list. *)
623 and generate_ruby_struct_list_code typ cols =
624 pr " VALUE rv = rb_ary_new2 (r->len);\n";
626 pr " for (i = 0; i < r->len; ++i) {\n";
627 pr " VALUE hv = rb_hash_new ();\n";
631 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), rb_str_new2 (r->val[i].%s));\n" name name
633 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
635 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), rb_str_new (r->val[i].%s, 32));\n" name name
636 | name, (FBytes|FUInt64) ->
637 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), ULL2NUM (r->val[i].%s));\n" name name
639 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), LL2NUM (r->val[i].%s));\n" name name
641 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), UINT2NUM (r->val[i].%s));\n" name name
643 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), INT2NUM (r->val[i].%s));\n" name name
644 | name, FOptPercent ->
645 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), rb_dbl2big (r->val[i].%s));\n" name name
646 | name, FChar -> (* XXX wrong? *)
647 pr " rb_hash_aset (hv, rb_str_new2 (\"%s\"), ULL2NUM (r->val[i].%s));\n" name name
649 pr " rb_ary_push (rv, hv);\n";
651 pr " guestfs_free_%s_list (r);\n" typ;