From 5309f16159e4decd81330622dcdd6eb4b25819a1 Mon Sep 17 00:00:00 2001 From: Bernhard Schommer Date: Tue, 16 Aug 2016 10:35:17 +0200 Subject: Moved quoting functions in Responsefile Also corrected some typos and corrected exception handling for expandargv. Bug 18308 --- lib/Responsefile.mll | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'lib/Responsefile.mll') diff --git a/lib/Responsefile.mll b/lib/Responsefile.mll index bb29fd75..35a2dbdb 100644 --- a/lib/Responsefile.mll +++ b/lib/Responsefile.mll @@ -15,8 +15,6 @@ (* *********************************************************************) -(* Parsing response files with various quoting styles *) - { (* To accumulate the characters in a word or quoted string *) let buf = Buffer.create 32 @@ -94,4 +92,29 @@ let expandargv argv = let args = Array.to_list argv in Array.of_list (List.rev (expand_args [] args [])) +(* This function reimplements quoting of writeargv from libiberty *) +let gnu_quote arg = + let len = String.length arg in + let buf = Buffer.create len in + String.iter (fun c -> begin match c with + | ' ' | '\t' | '\r' | '\n' | '\\' | '\'' | '"' -> + Buffer.add_char buf '\\' + | _ -> () end; + Buffer.add_char buf c) arg; + Buffer.contents buf + +let re_quote = Str.regexp ".*[ \t\n\r\"]" + +let diab_quote arg = + let buf = Buffer.create ((String.length arg) + 8) in + let doublequote = Str.string_match re_quote arg 0 in + if doublequote then begin + Buffer.add_char buf '"'; + String.iter (fun c -> + if c = '"' then Buffer.add_char buf '\\'; + Buffer.add_char buf c) arg; + if doublequote then Buffer.add_char buf '"'; + Buffer.contents buf + end else + arg } -- cgit