X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=blobdiff_plain;f=generator%2Fgenerator_ruby.ml;h=7c8788d04895c679fbde910144914a288f4c04d3;hp=4ee34752a642e4626b2ac9689fe6ab867313187d;hb=e751293e10d5ecbb2ef43a61b9c153a1fc4f0304;hpb=04d8209077d2227eb1d42695ba71147f78987050 diff --git a/generator/generator_ruby.ml b/generator/generator_ruby.ml index 4ee3475..7c8788d 100644 --- a/generator/generator_ruby.ml +++ b/generator/generator_ruby.ml @@ -1,5 +1,5 @@ (* libguestfs - * Copyright (C) 2009-2010 Red Hat Inc. + * Copyright (C) 2009-2011 Red Hat Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -28,6 +28,7 @@ open Generator_optgroups open Generator_actions open Generator_structs open Generator_c +open Generator_events (* Generate ruby bindings. *) let rec generate_ruby_c () = @@ -36,6 +37,7 @@ let rec generate_ruby_c () = pr "\ #include #include +#include #include @@ -52,13 +54,50 @@ static VALUE m_guestfs; /* guestfs module */ static VALUE c_guestfs; /* guestfs_h handle */ static VALUE e_Error; /* used for all errors */ -static void ruby_guestfs_free (void *p) +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); +static VALUE ruby_event_callback_wrapper_wrapper (VALUE argv); +static VALUE ruby_event_callback_handle_exception (VALUE not_used, VALUE exn); +static VALUE **get_all_event_callbacks (guestfs_h *g, size_t *len_rtn); + +static void +ruby_guestfs_free (void *gvp) { - if (!p) return; - guestfs_close ((guestfs_h *) p); + guestfs_h *g = gvp; + + if (g) { + /* As in the OCaml binding, there is a nasty, difficult to + * solve case here where the user deletes events in one of + * the callbacks that we are about to invoke, resulting in + * a double-free. XXX + */ + size_t len, i; + VALUE **roots = get_all_event_callbacks (g, &len); + + /* Close the handle: this could invoke callbacks from the list + * above, which is why we don't want to delete them before + * closing the handle. + */ + guestfs_close (g); + + /* Now unregister the global roots. */ + for (i = 0; i < len; ++i) { + rb_gc_unregister_address (roots[i]); + free (roots[i]); + } + } } -static VALUE ruby_guestfs_create (VALUE m) +/* + * call-seq: + * Guestfs::Guestfs.new() -> Guestfs::Guestfs + * + * Call + * +guestfs_create+[http://libguestfs.org/guestfs.3.html#guestfs_create] + * to create a new libguestfs handle. The handle is represented in + * Ruby as an instance of the Guestfs::Guestfs class. + */ +static VALUE +ruby_guestfs_create (VALUE m) { guestfs_h *g; @@ -75,7 +114,16 @@ static VALUE ruby_guestfs_create (VALUE m) return Data_Wrap_Struct (c_guestfs, NULL, ruby_guestfs_free, g); } -static VALUE ruby_guestfs_close (VALUE gv) +/* + * call-seq: + * g.close() -> nil + * + * Call + * +guestfs_close+[http://libguestfs.org/guestfs.3.html#guestfs_close] + * to close the libguestfs handle. + */ +static VALUE +ruby_guestfs_close (VALUE gv) { guestfs_h *g; Data_Get_Struct (gv, guestfs_h, g); @@ -86,12 +134,249 @@ static VALUE ruby_guestfs_close (VALUE gv) return Qnil; } +/* + * call-seq: + * g.set_event_callback(cb, event_bitmask) -> event_handle + * + * Call + * +guestfs_set_event_callback+[http://libguestfs.org/guestfs.3.html#guestfs_set_event_callback] + * to register an event callback. This returns an event handle. + */ +static VALUE +ruby_set_event_callback (VALUE gv, VALUE cbv, VALUE event_bitmaskv) +{ + guestfs_h *g; + uint64_t event_bitmask; + int eh; + VALUE *root; + char key[64]; + + Data_Get_Struct (gv, guestfs_h, g); + + event_bitmask = NUM2ULL (event_bitmaskv); + + root = guestfs_safe_malloc (g, sizeof *root); + *root = cbv; + + eh = guestfs_set_event_callback (g, ruby_event_callback_wrapper, + event_bitmask, 0, root); + if (eh == -1) { + free (root); + rb_raise (e_Error, \"%%s\", guestfs_last_error (g)); + } + + rb_gc_register_address (root); + + snprintf (key, sizeof key, \"_ruby_event_%%d\", eh); + guestfs_set_private (g, key, root); + + return INT2NUM (eh); +} + +/* + * call-seq: + * g.delete_event_callback(event_handle) -> nil + * + * Call + * +guestfs_delete_event_callback+[http://libguestfs.org/guestfs.3.html#guestfs_delete_event_callback] + * to delete an event callback. + */ +static VALUE +ruby_delete_event_callback (VALUE gv, VALUE event_handlev) +{ + guestfs_h *g; + char key[64]; + int eh = NUM2INT (event_handlev); + VALUE *root; + + Data_Get_Struct (gv, guestfs_h, g); + + snprintf (key, sizeof key, \"_ruby_event_%%d\", eh); + + root = guestfs_get_private (g, key); + if (root) { + rb_gc_unregister_address (root); + free (root); + guestfs_set_private (g, key, NULL); + guestfs_delete_event_callback (g, eh); + } + + return Qnil; +} + +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) +{ + size_t i; + VALUE eventv, event_handlev, bufv, arrayv, argv; + + eventv = ULL2NUM (event); + event_handlev = INT2NUM (event_handle); + + bufv = rb_str_new (buf, buf_len); + + arrayv = rb_ary_new2 (array_len); + for (i = 0; i < array_len; ++i) + rb_ary_push (arrayv, ULL2NUM (array[i])); + + /* Wrap up the arguments in an array which will be unpacked + * and passed as multiple arguments. This is a crap limitation + * of rb_rescue. + * http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/~poffice/mail/ruby-talk/65698 + */ + argv = rb_ary_new2 (5); + rb_ary_store (argv, 0, * (VALUE *) data /* function */); + rb_ary_store (argv, 1, eventv); + rb_ary_store (argv, 2, event_handlev); + rb_ary_store (argv, 3, bufv); + rb_ary_store (argv, 4, arrayv); + + rb_rescue (ruby_event_callback_wrapper_wrapper, argv, + ruby_event_callback_handle_exception, Qnil); +} + +static VALUE +ruby_event_callback_wrapper_wrapper (VALUE argv) +{ + VALUE fn, eventv, event_handlev, bufv, arrayv; + + fn = rb_ary_entry (argv, 0); + eventv = rb_ary_entry (argv, 1); + event_handlev = rb_ary_entry (argv, 2); + bufv = rb_ary_entry (argv, 3); + arrayv = rb_ary_entry (argv, 4); + + rb_funcall (fn, rb_intern (\"call\"), 4, + eventv, event_handlev, bufv, arrayv); + + return Qnil; +} + +static VALUE +ruby_event_callback_handle_exception (VALUE not_used, VALUE exn) +{ + /* Callbacks aren't supposed to throw exceptions. The best we + * can do is to print the error. + */ + fprintf (stderr, \"libguestfs: exception in callback: %%s\", + StringValueCStr (exn)); + + return Qnil; +} + +static VALUE ** +get_all_event_callbacks (guestfs_h *g, size_t *len_rtn) +{ + VALUE **r; + size_t i; + const char *key; + VALUE *root; + + /* Count the length of the array that will be needed. */ + *len_rtn = 0; + root = guestfs_first_private (g, &key); + while (root != NULL) { + if (strncmp (key, \"_ruby_event_\", strlen (\"_ruby_event_\")) == 0) + (*len_rtn)++; + root = guestfs_next_private (g, &key); + } + + /* Copy them into the return array. */ + r = guestfs_safe_malloc (g, sizeof (VALUE *) * (*len_rtn)); + + i = 0; + root = guestfs_first_private (g, &key); + while (root != NULL) { + if (strncmp (key, \"_ruby_event_\", strlen (\"_ruby_event_\")) == 0) { + r[i] = root; + i++; + } + root = guestfs_next_private (g, &key); + } + + return r; +} + "; List.iter ( - fun (name, style, _, _, _, _, _) -> - pr "static VALUE ruby_guestfs_%s (VALUE gv" name; - List.iter (fun arg -> pr ", VALUE %sv" (name_of_argt arg)) (snd style); + fun (name, (ret, args, optargs as style), _, flags, _, shortdesc, longdesc) -> + (* Generate rdoc. *) + if not (List.mem NotInDocs flags); then ( + let doc = replace_str longdesc "C [] then + 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." + else doc in + let doc = + if List.mem ProtocolLimitWarning flags then + doc ^ "\n\n" ^ protocol_limit_warning + else doc in + let doc = + if List.mem DangerWillRobinson flags then + doc ^ "\n\n" ^ danger_will_robinson + else doc in + let doc = + match deprecation_notice flags with + | None -> doc + | Some txt -> doc ^ "\n\n" ^ txt in + let doc = pod2text ~width:60 name doc in + let doc = String.concat "\n * " doc in + let doc = trim doc in + + let args = List.map name_of_argt args in + let args = if optargs <> [] then args @ ["{optargs...}"] else args in + let args = String.concat ", " args in + + let ret = + match ret with + | RErr -> "nil" + | RBool _ -> "[True|False]" + | RInt _ -> "fixnum" + | RInt64 _ -> "fixnum" + | RConstString _ -> "string" + | RConstOptString _ -> "string" + | RString _ -> "string" + | RBufferOut _ -> "string" + | RStruct _ + | RHashtable _ -> "hash" + | RStringList _ + | RStructList _ -> "list" in + + pr "\ +/* + * call-seq: + * g.%s(%s) -> %s + * + * %s + * + * %s + * + * (For the C API documentation for this function, see + * +guestfs_%s+[http://libguestfs.org/guestfs.3.html#guestfs_%s]). + */ +" name args ret shortdesc doc name name + ); + + (* Generate the function. *) + pr "static VALUE\n"; + pr "ruby_guestfs_%s (VALUE gv" name; + List.iter (fun arg -> pr ", VALUE %sv" (name_of_argt arg)) args; + (* XXX This makes the hash mandatory, meaning that you have + * to specify {} for no arguments. We could make it so this + * can be omitted. However that is a load of hassle because + * you have to completely change the way that arguments are + * passed in. See: + * http://www.redhat.com/archives/libvir-list/2008-April/msg00004.html + *) + if optargs <> [] then + pr ", VALUE optargsv"; pr ")\n"; pr "{\n"; pr " guestfs_h *g;\n"; @@ -105,11 +390,7 @@ static VALUE ruby_guestfs_close (VALUE gv) function | Pathname n | Device n | Dev_or_Path n | String n | Key n | FileIn n | FileOut n -> - pr " Check_Type (%sv, T_STRING);\n" n; pr " const char *%s = StringValueCStr (%sv);\n" n n; - pr " if (!%s)\n" n; - pr " rb_raise (rb_eTypeError, \"expected string for parameter %%s of %%s\",\n"; - pr " \"%s\", \"%s\");\n" n name | BufferIn n -> pr " Check_Type (%sv, T_STRING);\n" n; pr " const char *%s = RSTRING (%sv)->ptr;\n" n n; @@ -125,7 +406,7 @@ static VALUE ruby_guestfs_close (VALUE gv) pr " {\n"; pr " size_t i, len;\n"; pr " len = RARRAY_LEN (%sv);\n" n; - pr " %s = guestfs_safe_malloc (g, sizeof (char *) * (len+1));\n" + pr " %s = ALLOC_N (char *, len+1);\n" n; pr " for (i = 0; i < len; ++i) {\n"; pr " VALUE v = rb_ary_entry (%sv, i);\n" n; @@ -139,27 +420,61 @@ static VALUE ruby_guestfs_close (VALUE gv) pr " int %s = NUM2INT (%sv);\n" n n | Int64 n -> pr " long long %s = NUM2LL (%sv);\n" n n - ) (snd style); + | Pointer (t, n) -> + pr " %s %s = (%s) (intptr_t) NUM2LL (%sv);\n" t n t n + ) args; pr "\n"; - let error_code = - match fst style with - | RErr | RInt _ | RBool _ -> pr " int r;\n"; "-1" - | RInt64 _ -> pr " int64_t r;\n"; "-1" - | RConstString _ | RConstOptString _ -> - pr " const char *r;\n"; "NULL" - | RString _ -> pr " char *r;\n"; "NULL" - | RStringList _ | RHashtable _ -> pr " char **r;\n"; "NULL" - | RStruct (_, typ) -> pr " struct guestfs_%s *r;\n" typ; "NULL" - | RStructList (_, typ) -> - pr " struct guestfs_%s_list *r;\n" typ; "NULL" - | RBufferOut _ -> - pr " char *r;\n"; - pr " size_t size;\n"; - "NULL" in + (* Optional arguments are passed in a final hash parameter. *) + if optargs <> [] then ( + let uc_name = String.uppercase name in + pr " Check_Type (optargsv, T_HASH);\n"; + pr " struct guestfs_%s_argv optargs_s = { .bitmask = 0 };\n" name; + pr " struct guestfs_%s_argv *optargs = &optargs_s;\n" name; + pr " VALUE v;\n"; + List.iter ( + fun argt -> + let n = name_of_argt argt in + let uc_n = String.uppercase n in + pr " v = rb_hash_lookup (optargsv, ID2SYM (rb_intern (\"%s\")));\n" n; + pr " if (v != Qnil) {\n"; + (match argt with + | Bool n -> + pr " optargs_s.%s = RTEST (v);\n" n; + | Int n -> + pr " optargs_s.%s = NUM2INT (v);\n" n; + | Int64 n -> + pr " optargs_s.%s = NUM2LL (v);\n" n; + | String _ -> + pr " optargs_s.%s = StringValueCStr (v);\n" n + | _ -> assert false + ); + pr " optargs_s.bitmask |= GUESTFS_%s_%s_BITMASK;\n" uc_name uc_n; + pr " }\n"; + ) optargs; + pr "\n"; + ); + + (match ret with + | RErr | RInt _ | RBool _ -> pr " int r;\n" + | RInt64 _ -> pr " int64_t r;\n" + | RConstString _ | RConstOptString _ -> + pr " const char *r;\n" + | RString _ -> pr " char *r;\n" + | RStringList _ | RHashtable _ -> pr " char **r;\n" + | RStruct (_, typ) -> pr " struct guestfs_%s *r;\n" typ + | RStructList (_, typ) -> + pr " struct guestfs_%s_list *r;\n" typ + | RBufferOut _ -> + pr " char *r;\n"; + pr " size_t size;\n" + ); pr "\n"; - pr " r = guestfs_%s " name; + if optargs = [] then + pr " r = guestfs_%s " name + else + pr " r = guestfs_%s_argv " name; generate_c_call_args ~handle:"g" style; pr ";\n"; @@ -167,16 +482,23 @@ static VALUE ruby_guestfs_close (VALUE gv) function | Pathname _ | Device _ | Dev_or_Path _ | String _ | Key _ | FileIn _ | FileOut _ | OptString _ | Bool _ | Int _ | Int64 _ - | BufferIn _ -> () + | BufferIn _ | Pointer _ -> () | StringList n | DeviceList n -> pr " free (%s);\n" n - ) (snd style); - - pr " if (r == %s)\n" error_code; - pr " rb_raise (e_Error, \"%%s\", guestfs_last_error (g));\n"; + ) args; + + (match errcode_of_ret ret with + | `CannotReturnError -> () + | `ErrorIsMinusOne -> + pr " if (r == -1)\n"; + pr " rb_raise (e_Error, \"%%s\", guestfs_last_error (g));\n" + | `ErrorIsNULL -> + pr " if (r == NULL)\n"; + pr " rb_raise (e_Error, \"%%s\", guestfs_last_error (g));\n" + ); pr "\n"; - (match fst style with + (match ret with | RErr -> pr " return Qnil;\n" | RInt _ | RBool _ -> @@ -244,13 +566,28 @@ void Init__guestfs () rb_define_module_function (m_guestfs, \"create\", ruby_guestfs_create, 0); rb_define_method (c_guestfs, \"close\", ruby_guestfs_close, 0); + rb_define_method (c_guestfs, \"set_event_callback\", + ruby_set_event_callback, 2); + rb_define_method (c_guestfs, \"delete_event_callback\", + ruby_delete_event_callback, 1); "; - (* Define the rest of the methods. *) + + (* Constants. *) + List.iter ( + fun (name, bitmask) -> + pr " rb_define_const (m_guestfs, \"EVENT_%s\",\n" + (String.uppercase name); + pr " ULL2NUM (UINT64_C (0x%x)));\n" bitmask; + ) events; + pr "\n"; + + (* Methods. *) List.iter ( - fun (name, style, _, _, _, _, _) -> + fun (name, (_, args, optargs), _, _, _, _, _) -> + let nr_args = List.length args + if optargs <> [] then 1 else 0 in pr " rb_define_method (c_guestfs, \"%s\",\n" name; - pr " ruby_guestfs_%s, %d);\n" name (List.length (snd style)) + pr " ruby_guestfs_%s, %d);\n" name nr_args ) all_functions; pr "}\n"