Now looks in $PATH for cpdflin

This commit is contained in:
John Whitington 2014-10-09 15:34:24 +01:00
parent 6bb8715f23
commit e2770ee225
1 changed files with 19 additions and 8 deletions

27
cpdf.ml
View File

@ -3,7 +3,22 @@ open Pdfutil
open Pdfio open Pdfio
(* Find the location of the cpdflin binary, either in a provided place (with (* Find the location of the cpdflin binary, either in a provided place (with
-cpdflin), or using argv[0] or in the current directory. *) -cpdflin), or in the current directory or in $PATH. *)
let rec paths_of_chars prev = function
[] -> keep (fun x -> x <> "") (map implode (rev (map rev prev)))
| ':'::t -> paths_of_chars ([] :: prev) t
| h::t ->
match prev with
[] -> paths_of_chars [[h]] t
| ph::pt -> paths_of_chars ((h :: ph) :: pt) t
let paths () =
paths_of_chars [] (explode (Sys.getenv "PATH"))
let is_at_path file path =
let full = path ^ Filename.dir_sep ^ file in
if Sys.file_exists full then Some full else None
let find_cpdflin provided = let find_cpdflin provided =
match provided with match provided with
Some x -> Some x Some x -> Some x
@ -12,13 +27,9 @@ let find_cpdflin provided =
"cpdf" -> "cpdf" ->
if Sys.file_exists "cpdflin" then Some "cpdflin" else None if Sys.file_exists "cpdflin" then Some "cpdflin" else None
| s -> | s ->
try match option_map (is_at_path "cpdflin") (paths ()) with
let fullname = h::_ -> Some h
Filename.dirname s ^ Filename.dir_sep ^ "cpdflin" | _ -> None
in
if Sys.file_exists fullname then Some fullname else None
with
_ -> None
(* Call cpdflin, given the (temp) input name, the output name, and the location (* Call cpdflin, given the (temp) input name, the output name, and the location
of the cpdflin binary. Returns the exit code. *) of the cpdflin binary. Returns the exit code. *)