+and generate_ruby_c () =
+ generate_header CStyle LGPLv2plus;
+
+ pr "\
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+
+#include <ruby.h>
+
+#include \"hivex.h\"
+
+#include \"extconf.h\"
+
+/* For Ruby < 1.9 */
+#ifndef RARRAY_LEN
+#define RARRAY_LEN(r) (RARRAY((r))->len)
+#endif
+
+static VALUE m_hivex; /* hivex module */
+static VALUE c_hivex; /* hive_h handle */
+static VALUE e_Error; /* used for all errors */
+
+static void
+ruby_hivex_free (void *hvp)
+{
+ hive_h *h = hvp;
+
+ if (h)
+ hivex_close (h);
+}
+
+static void
+get_value (VALUE valv, hive_set_value *val)
+{
+ VALUE key = rb_hash_lookup (valv, ID2SYM (rb_intern (\"key\")));
+ VALUE type = rb_hash_lookup (valv, ID2SYM (rb_intern (\"type\")));
+ VALUE value = rb_hash_lookup (valv, ID2SYM (rb_intern (\"value\")));
+
+ val->key = StringValueCStr (key);
+ val->t = NUM2ULL (type);
+ val->len = RSTRING (value)->len;
+ val->value = RSTRING (value)->ptr;
+}
+
+static hive_set_value *
+get_values (VALUE valuesv, size_t *nr_values)
+{
+ size_t i;
+ hive_set_value *ret;
+
+ *nr_values = RARRAY_LEN (valuesv);
+ ret = malloc (sizeof (*ret) * *nr_values);
+ if (ret == NULL)
+ abort ();
+
+ for (i = 0; i < *nr_values; ++i) {
+ VALUE v = rb_ary_entry (valuesv, i);
+ get_value (v, &ret[i]);
+ }
+
+ return ret;
+}
+
+";
+
+ List.iter (
+ fun (name, (ret, args), shortdesc, longdesc) ->
+ let () =
+ (* Generate rdoc. *)
+ let doc = replace_str longdesc "C<hivex_" "C<h." in
+ let doc = pod2text ~width:60 name doc in
+ let doc = String.concat "\n * " doc in
+ let doc = trim doc in
+
+ let call, args =
+ match args with
+ | AHive :: args -> "h." ^ name, args
+ | args -> "Hivex::" ^ name, args in
+ let args = filter_map (
+ function
+ | AUnusedFlags -> None
+ | args -> Some (name_of_argt args)
+ ) args in
+ let args = String.concat ", " args in
+
+ let ret =
+ match ret with
+ | RErr | RErrDispose -> "nil"
+ | RHive -> "Hivex::Hivex"
+ | RNode | RNodeNotFound -> "integer"
+ | RNodeList -> "list"
+ | RValue -> "integer"
+ | RValueList -> "list"
+ | RString -> "string"
+ | RStringList -> "list"
+ | RLenType -> "hash"
+ | RLenTypeVal -> "hash"
+ | RInt32 -> "integer"
+ | RInt64 -> "integer" in
+
+ pr "\
+/*
+ * call-seq:
+ * %s(%s) -> %s
+ *
+ * %s
+ *
+ * %s
+ *
+ * (For the C API documentation for this function, see
+ * +hivex_%s+[http://libguestfs.org/hivex.3.html#hivex_%s]).
+ */
+" call args ret shortdesc doc name name in
+
+ (* Generate the function. *)
+ pr "static VALUE\n";
+ pr "ruby_hivex_%s (" name;
+
+ let () =
+ (* If the first argument is not AHive, then this is a module-level
+ * function, and Ruby passes an implicit module argument which we
+ * must ignore. Otherwise the first argument is the hive handle.
+ *)
+ let args =
+ match args with
+ | AHive :: args -> pr "VALUE hv"; args
+ | args -> pr "VALUE modulev"; args in
+ List.iter (
+ function
+ | AUnusedFlags -> ()
+ | arg ->
+ pr ", VALUE %sv" (name_of_argt arg)
+ ) args;
+ pr ")\n" in
+
+ pr "{\n";
+
+ List.iter (
+ function
+ | AHive ->
+ pr " hive_h *h;\n";
+ pr " Data_Get_Struct (hv, hive_h, h);\n";
+ pr " if (!h)\n";
+ pr " rb_raise (rb_eArgError, \"%%s: used handle after closing it\",\n";
+ pr " \"%s\");\n" name;
+ | ANode n ->
+ pr " hive_node_h %s = NUM2ULL (%sv);\n" n n
+ | AValue n ->
+ pr " hive_value_h %s = NUM2ULL (%sv);\n" n n
+ | AString n ->
+ pr " const char *%s = StringValueCStr (%sv);\n" n n;
+ | AStringNullable n ->
+ pr " const char *%s =\n" n;
+ pr " !NIL_P (%sv) ? StringValueCStr (%sv) : NULL;\n" n n
+ | AOpenFlags ->
+ pr " int flags = 0;\n";
+ List.iter (
+ fun (n, flag, _) ->
+ pr " if (RTEST (rb_hash_lookup (flagsv, ID2SYM (rb_intern (\"%s\")))))\n"
+ (String.lowercase flag);
+ pr " flags += %d;\n" n
+ ) open_flags
+ | AUnusedFlags -> ()
+ | ASetValues ->
+ pr " size_t nr_values;\n";
+ pr " hive_set_value *values;\n";
+ pr " values = get_values (valuesv, &nr_values);\n"
+ | ASetValue ->
+ pr " hive_set_value val;\n";
+ pr " get_value (valv, &val);\n"
+ ) args;
+ pr "\n";
+
+ let error_code =
+ match ret with
+ | RErr -> pr " int r;\n"; "-1"
+ | RErrDispose -> pr " int r;\n"; "-1"
+ | RHive -> pr " hive_h *r;\n"; "NULL"
+ | RNode -> pr " hive_node_h r;\n"; "0"
+ | RNodeNotFound ->
+ pr " errno = 0;\n";
+ pr " hive_node_h r;\n";
+ "0 && errno != 0"
+ | RNodeList -> pr " hive_node_h *r;\n"; "NULL"
+ | RValue -> pr " hive_value_h r;\n"; "0"
+ | RValueList -> pr " hive_value_h *r;\n"; "NULL"
+ | RString -> pr " char *r;\n"; "NULL"
+ | RStringList -> pr " char **r;\n"; "NULL"
+ | RLenType ->
+ pr " int r;\n";
+ pr " size_t len;\n";
+ pr " hive_type t;\n";
+ "-1"
+ | RLenTypeVal ->
+ pr " char *r;\n";
+ pr " size_t len;\n";
+ pr " hive_type t;\n";
+ "NULL"
+ | RInt32 ->
+ pr " errno = 0;\n";
+ pr " int32_t r;\n";
+ "-1 && errno != 0"
+ | RInt64 ->
+ pr " errno = 0;\n";
+ pr " int64_t r;\n";
+ "-1 && errno != 0" in
+ pr "\n";
+
+ let c_params =
+ List.map (function
+ | ASetValues -> ["nr_values"; "values"]
+ | ASetValue -> ["&val"]
+ | AUnusedFlags -> ["0"]
+ | arg -> [name_of_argt arg]) args in
+ let c_params =
+ match ret with
+ | RLenType | RLenTypeVal -> c_params @ [["&t"; "&len"]]
+ | _ -> c_params in
+ let c_params = List.concat c_params in
+
+ pr " r = hivex_%s (%s" name (List.hd c_params);
+ List.iter (pr ", %s") (List.tl c_params);
+ pr ");\n";
+ pr "\n";
+
+ (* Dispose of the hive handle (even if hivex_close returns error). *)
+ (match ret with
+ | RErrDispose ->
+ pr " /* So we don't double-free in the finalizer. */\n";
+ pr " DATA_PTR (hv) = NULL;\n";
+ pr "\n";
+ | _ -> ()
+ );
+
+ List.iter (
+ function
+ | AHive
+ | ANode _
+ | AValue _
+ | AString _
+ | AStringNullable _
+ | AOpenFlags
+ | AUnusedFlags -> ()
+ | ASetValues ->
+ pr " free (values);\n"
+ | ASetValue -> ()
+ ) args;
+
+ (* Check for errors from C library. *)
+ pr " if (r == %s)\n" error_code;
+ pr " rb_raise (e_Error, \"%%s\", strerror (errno));\n";
+ pr "\n";
+
+ (match ret with
+ | RErr | RErrDispose ->
+ pr " return Qnil;\n"
+ | RHive ->
+ pr " return Data_Wrap_Struct (c_hivex, NULL, ruby_hivex_free, r);\n"
+ | RNode
+ | RValue
+ | RInt64 ->
+ pr " return ULL2NUM (r);\n"
+ | RInt32 ->
+ pr " return INT2NUM (r);\n"
+ | RNodeNotFound ->
+ pr " if (r)\n";
+ pr " return ULL2NUM (r);\n";
+ pr " else\n";
+ pr " return Qnil;\n"
+ | RNodeList
+ | RValueList ->
+ pr " size_t i, len = 0;\n";
+ pr " for (i = 0; r[i] != 0; ++i) len++;\n";
+ pr " VALUE rv = rb_ary_new2 (len);\n";
+ pr " for (i = 0; r[i] != 0; ++i)\n";
+ pr " rb_ary_push (rv, ULL2NUM (r[i]));\n";
+ pr " free (r);\n";
+ pr " return rv;\n"
+ | RString ->
+ pr " VALUE rv = rb_str_new2 (r);\n";
+ pr " free (r);\n";
+ pr " return rv;\n"
+ | RStringList ->
+ pr " size_t i, len = 0;\n";
+ pr " for (i = 0; r[i] != NULL; ++i) len++;\n";
+ pr " VALUE rv = rb_ary_new2 (len);\n";
+ pr " for (i = 0; r[i] != NULL; ++i) {\n";
+ pr " rb_ary_push (rv, rb_str_new2 (r[i]));\n";
+ pr " free (r[i]);\n";
+ pr " }\n";
+ pr " free (r);\n";
+ pr " return rv;\n"
+ | RLenType ->
+ pr " VALUE rv = rb_hash_new ();\n";
+ pr " rb_hash_aset (rv, ID2SYM (rb_intern (\"len\")), INT2NUM (len));\n";
+ pr " rb_hash_aset (rv, ID2SYM (rb_intern (\"type\")), INT2NUM (t));\n";
+ pr " return rv;\n"
+ | RLenTypeVal ->
+ pr " VALUE rv = rb_hash_new ();\n";
+ pr " rb_hash_aset (rv, ID2SYM (rb_intern (\"len\")), INT2NUM (len));\n";
+ pr " rb_hash_aset (rv, ID2SYM (rb_intern (\"type\")), INT2NUM (t));\n";
+ pr " rb_hash_aset (rv, ID2SYM (rb_intern (\"value\")), rb_str_new (r, len));\n";
+ pr " free (r);\n";
+ pr " return rv;\n"
+ );
+
+ pr "}\n";
+ pr "\n"
+ ) functions;
+
+ pr "\
+/* Initialize the module. */
+void Init__hivex ()
+{
+ m_hivex = rb_define_module (\"Hivex\");
+ c_hivex = rb_define_class_under (m_hivex, \"Hivex\", rb_cObject);
+ e_Error = rb_define_class_under (m_hivex, \"Error\", rb_eStandardError);
+
+ /* XXX How to pass arguments? */
+#if 0
+#ifdef HAVE_RB_DEFINE_ALLOC_FUNC
+ rb_define_alloc_func (c_hivex, ruby_hivex_open);
+#endif
+#endif
+
+";
+
+ (* Methods. *)
+ List.iter (
+ fun (name, (_, args), _, _) ->
+ let args = List.filter (
+ function
+ | AUnusedFlags -> false
+ | _ -> true
+ ) args in
+ let nr_args = List.length args in
+ match args with
+ | AHive :: _ ->
+ pr " rb_define_method (c_hivex, \"%s\",\n" name;
+ pr " ruby_hivex_%s, %d);\n" name (nr_args-1)
+ | args -> (* class function *)
+ pr " rb_define_module_function (m_hivex, \"%s\",\n" name;
+ pr " ruby_hivex_%s, %d);\n" name nr_args
+ ) functions;
+
+ pr "}\n"
+