Blame SOURCES/0212-ocaml-dynamically-generate-the-content-of-Guestfs.Er.patch

ffd6ed
From 6cbba6117ae103b157c8e1b242de1868117d7601 Mon Sep 17 00:00:00 2001
ffd6ed
From: Pino Toscano <ptoscano@redhat.com>
ffd6ed
Date: Mon, 24 Aug 2015 17:57:10 +0200
ffd6ed
Subject: [PATCH] ocaml: dynamically generate the content of Guestfs.Errno
ffd6ed
ffd6ed
Put in a list the errnos to expose, filling the content of the
ffd6ed
Guestfs.Errno submodule from that.
ffd6ed
Also, generate a separate guestfs-c-errnos.c with the implementations of
ffd6ed
the functions returning the errno codes.
ffd6ed
ffd6ed
Only code motion and refactoring, no actual changes on the content of
ffd6ed
the ocaml Guestfs module.
ffd6ed
ffd6ed
(cherry picked from commit 649f439cb78f12bb04ef90d83b81e1fa23495852)
ffd6ed
---
ffd6ed
 .gitignore         |  1 +
ffd6ed
 generator/main.ml  |  1 +
ffd6ed
 generator/ocaml.ml | 71 +++++++++++++++++++++++++++++++++++++++++++++++++-----
ffd6ed
 ocaml/Makefile.am  |  2 ++
ffd6ed
 ocaml/guestfs-c.c  | 16 ------------
ffd6ed
 po/POTFILES        |  1 +
ffd6ed
 6 files changed, 70 insertions(+), 22 deletions(-)
ffd6ed
ffd6ed
diff --git a/.gitignore b/.gitignore
ffd6ed
index abb63d8..b18a123 100644
ffd6ed
--- a/.gitignore
ffd6ed
+++ b/.gitignore
ffd6ed
@@ -316,6 +316,7 @@ Makefile.in
ffd6ed
 /ocaml/examples/inspect_vm
ffd6ed
 /ocaml/examples/stamp-guestfs-ocaml.pod
ffd6ed
 /ocaml/guestfs-c-actions.c
ffd6ed
+/ocaml/guestfs-c-errnos.c
ffd6ed
 /ocaml/guestfs.ml
ffd6ed
 /ocaml/guestfs.mli
ffd6ed
 /ocamlinit-stamp
ffd6ed
diff --git a/generator/main.ml b/generator/main.ml
ffd6ed
index c0ad146..cb6e77b 100644
ffd6ed
--- a/generator/main.ml
ffd6ed
+++ b/generator/main.ml
ffd6ed
@@ -129,6 +129,7 @@ Run it from the top source directory using the command
ffd6ed
   output_to "ocaml/guestfs.mli" generate_ocaml_mli;
ffd6ed
   output_to "ocaml/guestfs.ml" generate_ocaml_ml;
ffd6ed
   output_to "ocaml/guestfs-c-actions.c" generate_ocaml_c;
ffd6ed
+  output_to "ocaml/guestfs-c-errnos.c" generate_ocaml_c_errnos;
ffd6ed
   output_to "ocaml/bindtests.ml" generate_ocaml_bindtests;
ffd6ed
   output_to "perl/Guestfs.xs" generate_perl_xs;
ffd6ed
   output_to "perl/lib/Sys/Guestfs.pm" generate_perl_pm;
ffd6ed
diff --git a/generator/ocaml.ml b/generator/ocaml.ml
ffd6ed
index a0101d5..f58cab3 100644
ffd6ed
--- a/generator/ocaml.ml
ffd6ed
+++ b/generator/ocaml.ml
ffd6ed
@@ -30,6 +30,14 @@ open Structs
ffd6ed
 open C
ffd6ed
 open Events
ffd6ed
 
ffd6ed
+(* List of errnos to expose on Guestfs.Errno. *)
ffd6ed
+let ocaml_errnos = [
ffd6ed
+  "EINVAL";
ffd6ed
+  "ENOTSUP";
ffd6ed
+  "EPERM";
ffd6ed
+  "ESRCH";
ffd6ed
+]
ffd6ed
+
ffd6ed
 (* Generate the OCaml bindings interface. *)
ffd6ed
 let rec generate_ocaml_mli () =
ffd6ed
   generate_header OCamlStyle LGPLv2plus;
ffd6ed
@@ -132,8 +140,12 @@ val last_errno : t -> int
ffd6ed
     which you can use to test the return value of {!Guestfs.last_errno}. *)
ffd6ed
 
ffd6ed
 module Errno : sig
ffd6ed
-  val errno_ENOTSUP : int
ffd6ed
-  val errno_ESRCH : int
ffd6ed
+";
ffd6ed
+  List.iter (
ffd6ed
+    fun e ->
ffd6ed
+      pr "  val errno_%s : int\n" e
ffd6ed
+  ) ocaml_errnos;
ffd6ed
+  pr "\
ffd6ed
 end
ffd6ed
 
ffd6ed
 ";
ffd6ed
@@ -265,10 +277,15 @@ external event_to_string : event list -> string
ffd6ed
 external last_errno : t -> int = \"ocaml_guestfs_last_errno\"
ffd6ed
 
ffd6ed
 module Errno = struct
ffd6ed
-  external enotsup : unit -> int = \"ocaml_guestfs_get_ENOTSUP\" \"noalloc\"
ffd6ed
-  let errno_ENOTSUP = enotsup ()
ffd6ed
-  external esrch : unit -> int = \"ocaml_guestfs_get_ESRCH\" \"noalloc\"
ffd6ed
-  let errno_ESRCH = esrch ()
ffd6ed
+";
ffd6ed
+  List.iter (
ffd6ed
+    fun e ->
ffd6ed
+      let le = String.lowercase e in
ffd6ed
+      pr "  external %s : unit -> int = \"ocaml_guestfs_get_%s\" \"noalloc\"\n"
ffd6ed
+        le e;
ffd6ed
+      pr "  let errno_%s = %s ()\n" e le
ffd6ed
+  ) ocaml_errnos;
ffd6ed
+  pr "\
ffd6ed
 end
ffd6ed
 
ffd6ed
 (* Give the exceptions names, so they can be raised from the C code. *)
ffd6ed
@@ -691,6 +708,48 @@ copy_table (char * const * argv)
ffd6ed
       )
ffd6ed
   ) external_functions_sorted
ffd6ed
 
ffd6ed
+(* Generate the OCaml bindings C errnos. *)
ffd6ed
+and generate_ocaml_c_errnos () =
ffd6ed
+  generate_header CStyle LGPLv2plus;
ffd6ed
+
ffd6ed
+  pr "\
ffd6ed
+#include <config.h>
ffd6ed
+
ffd6ed
+#include <stdio.h>
ffd6ed
+#include <stdlib.h>
ffd6ed
+#include <string.h>
ffd6ed
+#include <errno.h>
ffd6ed
+
ffd6ed
+#include <caml/config.h>
ffd6ed
+#include <caml/alloc.h>
ffd6ed
+#include <caml/fail.h>
ffd6ed
+#include <caml/memory.h>
ffd6ed
+#include <caml/mlvalues.h>
ffd6ed
+
ffd6ed
+#include \"guestfs.h\"
ffd6ed
+
ffd6ed
+#include \"guestfs-c.h\"
ffd6ed
+
ffd6ed
+/* These prototypes are solely to quiet gcc warnings. */
ffd6ed
+";
ffd6ed
+  List.iter (
ffd6ed
+    fun e ->
ffd6ed
+      pr "value ocaml_guestfs_get_%s (value unitv);\n" e
ffd6ed
+  ) ocaml_errnos;
ffd6ed
+
ffd6ed
+  List.iter (
ffd6ed
+    fun e ->
ffd6ed
+      pr "\
ffd6ed
+
ffd6ed
+/* NB: \"noalloc\" function. */
ffd6ed
+value
ffd6ed
+ocaml_guestfs_get_%s (value unitv)
ffd6ed
+{
ffd6ed
+  return Val_int (%s);
ffd6ed
+}
ffd6ed
+" e e
ffd6ed
+  ) ocaml_errnos
ffd6ed
+
ffd6ed
 and generate_ocaml_structure_decls () =
ffd6ed
   List.iter (
ffd6ed
     fun { s_name = typ; s_cols = cols } ->
ffd6ed
diff --git a/ocaml/Makefile.am b/ocaml/Makefile.am
ffd6ed
index 94f11ef..d2444dc 100644
ffd6ed
--- a/ocaml/Makefile.am
ffd6ed
+++ b/ocaml/Makefile.am
ffd6ed
@@ -21,6 +21,7 @@ generator_built = \
ffd6ed
 	guestfs.mli \
ffd6ed
 	guestfs.ml \
ffd6ed
 	guestfs-c-actions.c \
ffd6ed
+	guestfs-c-errnos.c \
ffd6ed
 	$(srcdir)/bindtests.ml
ffd6ed
 
ffd6ed
 EXTRA_DIST = \
ffd6ed
@@ -87,6 +88,7 @@ libguestfsocaml_a_CFLAGS = \
ffd6ed
 libguestfsocaml_a_SOURCES = \
ffd6ed
 	guestfs-c.c \
ffd6ed
 	guestfs-c-actions.c \
ffd6ed
+	guestfs-c-errnos.c \
ffd6ed
 	../src/utils.c
ffd6ed
 
ffd6ed
 if HAVE_OCAMLDOC
ffd6ed
diff --git a/ocaml/guestfs-c.c b/ocaml/guestfs-c.c
ffd6ed
index 0ebb84c..1bcf5b8 100644
ffd6ed
--- a/ocaml/guestfs-c.c
ffd6ed
+++ b/ocaml/guestfs-c.c
ffd6ed
@@ -63,8 +63,6 @@ value ocaml_guestfs_set_event_callback (value gv, value closure, value events);
ffd6ed
 value ocaml_guestfs_delete_event_callback (value gv, value eh);
ffd6ed
 value ocaml_guestfs_event_to_string (value events);
ffd6ed
 value ocaml_guestfs_last_errno (value gv);
ffd6ed
-value ocaml_guestfs_get_ENOTSUP (value unitv);
ffd6ed
-value ocaml_guestfs_get_ESRCH (value unitv);
ffd6ed
 
ffd6ed
 /* Allocate handles and deal with finalization. */
ffd6ed
 static void
ffd6ed
@@ -440,17 +438,3 @@ ocaml_guestfs_last_errno (value gv)
ffd6ed
   rv = Val_int (r);
ffd6ed
   CAMLreturn (rv);
ffd6ed
 }
ffd6ed
-
ffd6ed
-/* NB: "noalloc" function. */
ffd6ed
-value
ffd6ed
-ocaml_guestfs_get_ENOTSUP (value unitv)
ffd6ed
-{
ffd6ed
-  return Val_int (ENOTSUP);
ffd6ed
-}
ffd6ed
-
ffd6ed
-/* NB: "noalloc" function. */
ffd6ed
-value
ffd6ed
-ocaml_guestfs_get_ESRCH (value unitv)
ffd6ed
-{
ffd6ed
-  return Val_int (ESRCH);
ffd6ed
-}
ffd6ed
diff --git a/po/POTFILES b/po/POTFILES
ffd6ed
index 7c99fd0..b359bf6 100644
ffd6ed
--- a/po/POTFILES
ffd6ed
+++ b/po/POTFILES
ffd6ed
@@ -252,6 +252,7 @@ mllib/progress-c.c
ffd6ed
 mllib/tty-c.c
ffd6ed
 mllib/uri-c.c
ffd6ed
 ocaml/guestfs-c-actions.c
ffd6ed
+ocaml/guestfs-c-errnos.c
ffd6ed
 ocaml/guestfs-c.c
ffd6ed
 p2v/authors.c
ffd6ed
 p2v/config.c
ffd6ed
-- 
ffd6ed
1.8.3.1
ffd6ed