]> xenbits.xensource.com Git - xcp/xen-api-libs.git/commitdiff
stdext/Os: A simple module for running stuff on the command line.
authorMatthias Goergens <Matthias.Goergens@citrix.com>
Thu, 17 Jun 2010 21:16:18 +0000 (22:16 +0100)
committerMatthias Goergens <Matthias.Goergens@citrix.com>
Thu, 17 Jun 2010 21:16:18 +0000 (22:16 +0100)
Signed-off-by: Matthias Goergens <Matthias.Goergens@citrix.com>
stdext/Makefile
stdext/os.ml [new file with mode: 0644]
stdext/os.mli [new file with mode: 0644]

index 44237308e30a51006f42dd0044b1668ef9267aa9..728d3ea5b70834c9a5f2350698fc1512727fbbd6 100644 (file)
@@ -22,7 +22,7 @@ OCAML_TEST_LIB = $(shell ocamlfind query oUnit)/oUnit.cmxa
 
 STDEXT_OBJS = fun listext filenameext stringext arrayext hashtblext pervasiveext threadext ring \
        qring fring opt bigbuffer unixext range vIO trie config date encodings fe fecomms \
-       forkhelpers gzip sha1sum zerocheck base64 backtrace tar mapext
+       forkhelpers gzip sha1sum zerocheck base64 backtrace tar mapext os
 
 INTF = $(foreach obj, $(STDEXT_OBJS),$(obj).cmi)
 LIBS = stdext.cma stdext.cmxa
diff --git a/stdext/os.ml b/stdext/os.ml
new file mode 100644 (file)
index 0000000..1210695
--- /dev/null
@@ -0,0 +1,27 @@
+let check_exit_status = function
+       | Unix.WEXITED 0 -> true
+       | Unix.WEXITED r -> Printf.eprintf "warning: the process terminated with exit code (%d)\n%!" r; false
+       | Unix.WSIGNALED n -> Printf.eprintf "warning: the process was killed by a signal (number: %d)\n%!" n; false
+       | Unix.WSTOPPED n -> Printf.eprintf "warning: the process was stopped by a signal (number: %d)\n%!" n; false
+;;
+
+let was_successful = function
+       | Unix.WEXITED 0 -> true
+       | Unix.WEXITED r -> false
+       | Unix.WSIGNALED n -> false
+       | Unix.WSTOPPED n -> false
+
+let syscall : ?env:string array -> string -> string * string * Unix.process_status = fun ?(env=[| |]) cmd ->
+       print_endline cmd;
+       let ic, oc, ec = Unix.open_process_full cmd env in
+       let buf1 = Buffer.create 96
+       and buf2 = Buffer.create 48 in
+       (try while true do Buffer.add_channel buf1 ic 1 done
+       with End_of_file -> ());
+       (try while true do Buffer.add_channel buf2 ec 1 done
+       with End_of_file -> ());
+       let exit_status = Unix.close_process_full (ic, oc, ec) in
+       check_exit_status exit_status;
+       (Buffer.contents buf1,
+       Buffer.contents buf2,
+       exit_status)
diff --git a/stdext/os.mli b/stdext/os.mli
new file mode 100644 (file)
index 0000000..7e16e58
--- /dev/null
@@ -0,0 +1,3 @@
+val check_exit_status : Unix.process_status -> bool
+val was_successful : Unix.process_status -> bool
+val syscall : ?env:string array -> string -> string * string * Unix.process_status