···66let strf = Printf.sprintf
77let error_to_failure = function Ok v -> v | Error e -> failwith e
8899-let quote_tool tool =
1010- (* Can be replaced by Filename.quote_command once we drop OCaml < 4.10 *)
1111- Filename.quote @@
1212- if Sys.win32 then String.map (function '/' -> '\\' | c -> c) tool else tool
1313-149let find_sub ?(start = 0) ~sub s =
1510 (* naive algorithm, worst case O(length sub * length s) *)
1611 let len_sub = String.length sub in
···8378 try let () = set_binary_mode_out stdout true in f () with
8479 | Sys_error e | Failure e -> prerr_endline e; Cmdliner.Cmd.Exit.some_error
85808686-let exec_stdout cmd =
8181+let exec_stdout tool ~args =
8282+ (* The cmd munging logic can be replaced by Filename.quote_command once we
8383+ drop OCaml < 4.10 *)
8484+ let quote_tool tool =
8585+ Filename.quote @@
8686+ if Sys.win32 then String.map (function '/' -> '\\' | c -> c) tool else tool
8787+ in
8788 try
8889 let tmp = Filename.temp_file "cmd" "stdout" in
9090+ let tool = quote_tool tool and args = List.map Filename.quote args in
9191+ let cmd = String.concat " " (tool :: args) in
8992 let exec = String.concat " > " [cmd; Filename.quote tmp] in
9393+ let exec = if Sys.win32 then strf {|"%s"|} exec else exec in
9094 match Sys.command exec with
9195 | 0 ->
9296 let ic = open_in_bin tmp in
···168172 | _ :: lines -> find_subs lines
169173 | [] -> []
170174 in
171171- let exec =
172172- strf "%s --__complete %s --__complete=" (quote_tool tool) cmd
173173- in
174174- let comps = exec_stdout exec |> error_to_failure in
175175+ let subs = if cmd = "" then [] else String.split_on_char ' ' cmd in
176176+ let args = "--__complete" :: (subs @ ["--__complete="]) in
177177+ let comps = exec_stdout tool ~args |> error_to_failure in
175178 let comps = String.split_on_char '\n' comps in
176179 match comps with
177180 | "1" :: comps -> find_subs comps
···190193 with Failure e -> Error e
191194192195let get_tool_command_man tool ~name cmd =
193193- let tool = quote_tool tool in
194194- let exec = if cmd = "" then tool else String.concat " " [tool; cmd] in
195196 let man_basename =
196197 let exec = if cmd = "" then name else String.concat " " [name; cmd] in
197198 (String.map (function ' ' -> '-' | c -> c) exec)
···204205 | exception Scanf.Scan_failure _ -> extract_section lines
205206 end
206207 | [] ->
207207- Error (strf "%s command: Could not extract section from manual" exec)
208208+ Error (strf "%s command: Could not extract section from manual"
209209+ (tool ^ " " ^ cmd))
208210 in
209211 extract_section (String.split_on_char '\n' man)
210212 in
211211- let get_groff = exec ^ " --help=groff" in
212212- match exec_stdout get_groff with
213213+ let subs = if cmd = "" then [] else String.split_on_char ' ' cmd in
214214+ let args = subs @ ["--help=groff"] in
215215+ match exec_stdout tool ~args with
213216 | Error _ as e -> e
214217 | Ok man -> add_section man
215218