Skip to content

Commit

Permalink
Fix ocaml#32
Browse files Browse the repository at this point in the history
Now the environment variables can't have more than one ~/.opam/... path
  • Loading branch information
samoht committed Jul 5, 2012
1 parent 3a0af3c commit 8a65023
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/client.ml
Expand Up @@ -701,10 +701,11 @@ type env = {
let expand_env t env =
List.map (fun (ident, symbol, string) ->
let string = substitute_string t string in
let clean_env () = try Utils.reset_env_value (Sys.getenv ident) with _ -> [] in
match symbol with
| "=" -> (ident, string)
| "+=" -> (ident, try string ^ ":" ^ Sys.getenv ident with _ -> string)
| "=+" -> (ident, try Sys.getenv ident ^ ":" ^ string with _ -> string)
| "+=" -> (ident, String.concat ":" (string :: clean_env ()))
| "=+" -> (ident, String.concat ":" (clean_env () @ [string]))
| _ -> failwith (Printf.sprintf "expand_env: %s is an unknown symbol" symbol)
) env

Expand Down
10 changes: 5 additions & 5 deletions src/run.ml
Expand Up @@ -180,17 +180,17 @@ let real_path p =
else
dir / base

let add_path bins =
let replace_path bins =
let path = ref "<not set>" in
let env = Unix.environment () in
for i = 0 to Array.length env - 1 do
let k,v = match Utils.cut_at env.(i) '=' with
| Some (k,v) -> k,v
| None -> assert false in
if k = "PATH" then
let new_path = match List.filter Sys.file_exists bins with
| [] -> v
| l -> String.concat ":" l ^ ":" ^ v in
let v = Utils.reset_env_value v in
let bins = List.filter Sys.file_exists bins in
let new_path = String.concat ":" (bins @ v) in
env.(i) <- "PATH=" ^ new_path;
path := new_path;
done;
Expand All @@ -201,7 +201,7 @@ type command = string list
let run_process ?(add_to_env=[]) ?(add_to_path=[]) = function
| [] -> invalid_arg "run_process"
| cmd :: args ->
let env, path = add_path add_to_path in
let env, path = replace_path add_to_path in
let add_to_env = List.map (fun (k,v) -> k^"="^v) add_to_env in
let env = Array.concat [ env; Array.of_list add_to_env ] in
let name = log_file () in
Expand Down
6 changes: 6 additions & 0 deletions src/utils.ml
Expand Up @@ -77,4 +77,10 @@ let cut_at = cut_at_aux String.index

let rcut_at = cut_at_aux String.rindex

let split s c =
Pcre.split (Pcre.regexp (String.make 1 c)) s

(* Remove from a ':' separated list of string the one which are prefix of Globals.root_path *)
let reset_env_value v =
let v = split v ':' in
List.filter (fun v -> not (starts_with ~prefix:!Globals.root_path v)) v

0 comments on commit 8a65023

Please sign in to comment.