change type of CCUnix.escape_str

This commit is contained in:
Simon Cruanes 2016-11-03 17:56:05 +01:00
parent e9c20b9b21
commit af6b1dd6e0
2 changed files with 11 additions and 8 deletions

View file

@ -34,10 +34,12 @@ let finally_ f x ~h =
raise e raise e
(* print a string, but escaped if required *) (* print a string, but escaped if required *)
let escape_str buf s = let escape_str s =
if str_exists s if
str_exists s
(function ' ' | '"' | '\'' | '\n' | '\t'-> true | _ -> false) (function ' ' | '"' | '\'' | '\n' | '\t'-> true | _ -> false)
then ( then (
let buf = Buffer.create (String.length s) in
Buffer.add_char buf '\''; Buffer.add_char buf '\'';
String.iter String.iter
(function (function
@ -45,7 +47,8 @@ let escape_str buf s =
| c -> Buffer.add_char buf c | c -> Buffer.add_char buf c
) s; ) s;
Buffer.add_char buf '\''; Buffer.add_char buf '\'';
) else Buffer.add_string buf s Buffer.contents buf
) else s
let read_all ?(size=1024) ic = let read_all ?(size=1024) ic =
let buf = ref (Bytes.create size) in let buf = ref (Bytes.create size) in

View file

@ -13,13 +13,13 @@ type 'a gen = unit -> 'a option
(** {2 Calling Commands} *) (** {2 Calling Commands} *)
val escape_str : Buffer.t -> string -> unit val escape_str : string -> string
(** Escape a string so it can be a shell argument. *) (** Escape a string so it can be a shell argument. *)
(*$T (*$T
CCPrint.sprintf "%a" escape_str "foo" = "foo" escape_str "foo" = "foo"
CCPrint.sprintf "%a" escape_str "foo bar" = "'foo bar'" escape_str "foo bar" = "'foo bar'"
CCPrint.sprintf "%a" escape_str "fo'o b'ar" = "'fo'\\''o b'\\''ar'" escape_str "fo'o b'ar" = "'fo'\\''o b'\\''ar'"
*) *)
type call_result = type call_result =
@ -45,7 +45,7 @@ val call : ?bufsize:int ->
(*$T (*$T
(call ~stdin:(`Str "abc") "cat")#stdout = "abc" (call ~stdin:(`Str "abc") "cat")#stdout = "abc"
(call "echo %a" escape_str "a'b'c")#stdout = "a'b'c\n" (call "echo %s" (escape_str "a'b'c"))#stdout = "a'b'c\n"
(call "echo %s" "a'b'c")#stdout = "abc\n" (call "echo %s" "a'b'c")#stdout = "abc\n"
*) *)