[Libguestfs] [PATCH 11/67] resize: Add bindings for the guestfish -a URI parsing mini-library.

Richard W.M. Jones rjones at redhat.com
Sat Aug 24 11:04:11 UTC 2013


From: "Richard W.M. Jones" <rjones at redhat.com>

This allows -a options to be parsed from OCaml programs, reusing
the same code that is being used by C.

(cherry picked from commit 406522d1d2c9108c52f1356cbbb9bb4039d9ce84)
---
 po/POTFILES        |  1 +
 po/POTFILES-ml     |  1 +
 resize/Makefile.am | 15 ++++++++--
 resize/uRI.ml      | 26 +++++++++++++++++
 resize/uRI.mli     | 29 +++++++++++++++++++
 resize/uri-c.c     | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 154 insertions(+), 2 deletions(-)
 create mode 100644 resize/uRI.ml
 create mode 100644 resize/uRI.mli
 create mode 100644 resize/uri-c.c

diff --git a/po/POTFILES b/po/POTFILES
index bcefe16..92b9a18 100644
--- a/po/POTFILES
+++ b/po/POTFILES
@@ -233,6 +233,7 @@ rescue/test-virt-rescue.pl
 rescue/virt-rescue.c
 resize/progress-c.c
 resize/tty-c.c
+resize/uri-c.c
 ruby/ext/guestfs/_guestfs.c
 src/actions-0.c
 src/actions-1.c
diff --git a/po/POTFILES-ml b/po/POTFILES-ml
index 34533ea..ba64fa3 100644
--- a/po/POTFILES-ml
+++ b/po/POTFILES-ml
@@ -4,6 +4,7 @@ resize/common_utils_tests.ml
 resize/progress.ml
 resize/resize.ml
 resize/tTY.ml
+resize/uRI.ml
 sparsify/sparsify.ml
 sysprep/firstboot.ml
 sysprep/main.ml
diff --git a/resize/Makefile.am b/resize/Makefile.am
index a9c95ff..62e7659 100644
--- a/resize/Makefile.am
+++ b/resize/Makefile.am
@@ -35,19 +35,25 @@ SOURCES = \
 	resize.ml \
 	tty-c.c \
 	tTY.mli \
-	tTY.ml
+	tTY.ml \
+	uri-c.c \
+	uRI.mli \
+	uRI.ml
 
 if HAVE_OCAML
 
 # Note this list must be in dependency order.
 OBJECTS = \
 	$(top_builddir)/fish/guestfish-progress.o \
+	$(top_builddir)/fish/guestfish-uri.o \
 	tty-c.o \
 	progress-c.o \
+	uri-c.o \
 	common_gettext.cmx \
 	common_utils.cmx \
 	tTY.cmx \
 	progress.cmx \
+	uRI.cmx \
 	resize.cmx
 
 bin_SCRIPTS = virt-resize
@@ -63,10 +69,15 @@ endif
 OCAMLCFLAGS = -g -warn-error CDEFLMPSUVYZX $(OCAMLPACKAGES)
 OCAMLOPTFLAGS = $(OCAMLCFLAGS)
 
+OCAMLCLIBS  = \
+	$(LIBXML2_LIBS) -lncurses \
+	-L../src/.libs -lutils \
+	-L../gnulib/lib/.libs -lgnu
+
 virt-resize: $(OBJECTS)
 	$(OCAMLFIND) ocamlopt $(OCAMLOPTFLAGS) \
 	  mlguestfs.cmxa -linkpkg $^ \
-	  -cclib -lncurses \
+	  -cclib '$(OCAMLCLIBS)' \
 	  $(OCAML_GCOV_LDFLAGS) \
 	  -o $@
 
diff --git a/resize/uRI.ml b/resize/uRI.ml
new file mode 100644
index 0000000..272f339
--- /dev/null
+++ b/resize/uRI.ml
@@ -0,0 +1,26 @@
+(* virt-resize - interface to -a URI option parsing mini library
+ * Copyright (C) 2013 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *)
+
+type uri = {
+  path : string;
+  protocol : string;
+  server : string array option;
+  username : string option;
+}
+
+external parse_uri : string -> uri = "virt_resize_parse_uri"
diff --git a/resize/uRI.mli b/resize/uRI.mli
new file mode 100644
index 0000000..efd39dd
--- /dev/null
+++ b/resize/uRI.mli
@@ -0,0 +1,29 @@
+(* virt-resize - interface to -a URI option parsing mini library
+ * Copyright (C) 2013 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *)
+
+(** Interface to [-a] URI option parsing mini library. *)
+
+type uri = {
+  path : string;                        (** path *)
+  protocol : string;                    (** protocol, eg. [file], [nbd] *)
+  server : string array option;         (** list of servers *)
+  username : string option;             (** username *)
+}
+
+val parse_uri : string -> uri
+(** See [fish/uri.h]. *)
diff --git a/resize/uri-c.c b/resize/uri-c.c
new file mode 100644
index 0000000..8c9c385
--- /dev/null
+++ b/resize/uri-c.c
@@ -0,0 +1,84 @@
+/* virt-resize - interface to -a URI option parsing mini library
+ * Copyright (C) 2013 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <unistd.h>
+#include <locale.h>
+
+#include <caml/alloc.h>
+#include <caml/fail.h>
+#include <caml/memory.h>
+#include <caml/mlvalues.h>
+
+#include "uri.h"
+
+value
+virt_resize_parse_uri (value argv /* arg value, not an array! */)
+{
+  CAMLparam1 (argv);
+  CAMLlocal4 (rv, sv, ssv, ov);
+  struct uri uri;
+  int r;
+  size_t len;
+
+  r = parse_uri (String_val (argv), &uri);
+  if (r == -1)
+    caml_invalid_argument ("URI.parse_uri");
+
+  /* Convert the struct into an OCaml tuple. */
+  rv = caml_alloc_tuple (4);
+
+  /* path : string */
+  sv = caml_copy_string (uri.path);
+  free (uri.path);
+  Store_field (rv, 0, sv);
+
+  /* protocol : string */
+  sv = caml_copy_string (uri.protocol);
+  free (uri.protocol);
+  Store_field (rv, 1, sv);
+
+  /* server : string array option */
+  if (uri.server) {
+    ssv = caml_copy_string_array (uri.server);
+    guestfs___free_string_list (uri.server);
+    ov = caml_alloc (1, 0);
+    Store_field (ov, 0, ssv);
+  }
+  else
+    ov = Val_int (0);
+  Store_field (rv, 2, ov);
+
+  /* username : string option */
+  if (uri.username) {
+    sv = caml_copy_string (uri.username);
+    free (uri.username);
+    ov = caml_alloc (1, 0);
+    Store_field (ov, 0, sv);
+  }
+  else
+    ov = Val_int (0);
+  Store_field (rv, 3, ov);
+
+  CAMLreturn (rv);
+}
-- 
1.8.3.1




More information about the Libguestfs mailing list