From 2edfe9ff1b9c5569539a6cb8f0a8c4793ee84c44 Mon Sep 17 00:00:00 2001 From: John Whitington Date: Mon, 24 Nov 2014 12:02:36 +0000 Subject: [PATCH] Simplifying linearizer finding --- cpdf.ml | 54 ++++++++++++-------------------------------------- cpdf.mli | 2 +- cpdfcommand.ml | 6 +----- 3 files changed, 15 insertions(+), 47 deletions(-) diff --git a/cpdf.ml b/cpdf.ml index 60adebe..15addb6 100755 --- a/cpdf.ml +++ b/cpdf.ml @@ -2,49 +2,18 @@ open Pdfutil open Pdfio -let system_path_separator = - match Sys.os_type with "Win32" -> ';' | _ -> ':' - -(* Find the location of the cpdflin binary, either in a provided place (with --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))) - | pathsep::t when pathsep = system_path_separator -> - 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 - Printf.printf "Looking for %s\n%!" full; - if Sys.file_exists full then Some full else None - +(* Prefer a) the one given with -cpdflin b) a local cpdflin, c) otherwise assume +installed at a system place *) let find_cpdflin provided = match provided with - Some x -> Some x + Some x -> x | None -> let dotslash = match Sys.os_type with "Win32" -> "" | _ -> "./" in - if Sys.file_exists "cpdflin" then Some (dotslash ^ "cpdflin") else - if Sys.file_exists "cpdflin.exe" then Some (dotslash ^ "cpdflin.exe") else - if Sys.file_exists "qpdf" then Some (dotslash ^ "qpdf") else - if Sys.file_exists "qpdf.exe" then Some (dotslash ^ "qpdf.exe") else - match option_map (is_at_path "cpdflin") (paths ()) with - h::_ -> Some (Filename.quote h) - | _ -> - match option_map (is_at_path "cpdflin.exe") (paths ()) with - h::_ -> Some (Filename.quote h) - | _ -> - match option_map (is_at_path "qpdf") (paths ()) with - h::_ -> Some (Filename.quote h) - | _ -> - match option_map (is_at_path "qpdf") (paths ()) with - h::_ -> Some (Filename.quote h) - | _ -> None + if Sys.file_exists "cpdflin" then (dotslash ^ "cpdflin") else + if Sys.file_exists "cpdflin.exe" then (dotslash ^ "cpdflin.exe") else + match Sys.os_type with + "Win32" -> "cpdflin.exe" + | _ -> "cpdflin" (* Call cpdflin, given the (temp) input name, the output name, and the location of the cpdflin binary. Returns the exit code. *) @@ -59,8 +28,11 @@ let call_cpdflin cpdflin temp output best_password = (* On windows, don't use LD_LIBRARY_PATH - it will happen automatically *) Sys.command command | _ -> - (* On linux, set LD_LIBRARY_PATH to the stem of cpdflin *) - Sys.command ("LD_LIBRARY_PATH=" ^ Filename.dirname cpdflin ^ " " ^ command) + (* On other platforms, if -cpdflin was provided, or cpdflin was in the + current folder, set up LD_LIBRARY_PATH: *) + match cpdflin with + "cpdflin" -> Sys.command (cpdflin ^ " " ^ command) + | _ -> Sys.command ("LD_LIBRARY_PATH=" ^ Filename.dirname cpdflin ^ " " ^ command) (* Recompress anything which isn't compressed, unless it's metadata. *) let recompress_stream pdf = function diff --git a/cpdf.mli b/cpdf.mli index 149adae..ea329f8 100644 --- a/cpdf.mli +++ b/cpdf.mli @@ -472,7 +472,7 @@ val custom_csp2 : float -> Pdf.t -> Pdf.t (** Nobble a page, given pdf, pagenumber and page *) val nobble_page : Pdf.t -> 'a -> Pdfpage.t -> Pdfpage.t -val find_cpdflin : string option -> string option +val find_cpdflin : string option -> string val call_cpdflin : string -> string -> string -> string -> int diff --git a/cpdfcommand.ml b/cpdfcommand.ml index d09c4b1..3c8db9e 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -1963,11 +1963,7 @@ let really_write_pdf ?(encryption = None) ?(is_decompress=false) mk_id pdf outna end; begin if args.linearize then - let cpdflin = - match Cpdf.find_cpdflin args.cpdflin with - Some x -> x - | None -> raise (Pdf.PDFError "Could not find cpdflin") - in + let cpdflin = Cpdf.find_cpdflin args.cpdflin in match args.inputs with [] -> raise (Pdf.PDFError "no input in recryption") | (_, _, user_pw, owner_pw, _)::_ ->