1996-11-29 08:55:09 -08:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
1996-11-29 08:55:09 -08:00
|
|
|
(* *)
|
|
|
|
(* Jerome Vouillon, projet Cristal, INRIA Rocquencourt *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml port by John Malecki and Xavier Leroy *)
|
1996-11-29 08:55:09 -08:00
|
|
|
(* *)
|
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the Q Public License version 1.0. *)
|
1996-11-29 08:55:09 -08:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(****************** Tools for Unix *************************************)
|
|
|
|
|
|
|
|
open Misc
|
|
|
|
open Unix
|
|
|
|
open Primitives
|
|
|
|
|
|
|
|
(*** Convert a socket name into a socket address. ***)
|
|
|
|
let convert_address address =
|
|
|
|
try
|
2009-05-20 04:52:42 -07:00
|
|
|
let n = String.index address ':' in
|
2000-08-10 02:58:08 -07:00
|
|
|
let host = String.sub address 0 n
|
|
|
|
and port = String.sub address (n + 1) (String.length address - n - 1)
|
1996-11-29 08:55:09 -08:00
|
|
|
in
|
|
|
|
(PF_INET,
|
1997-05-19 08:42:21 -07:00
|
|
|
ADDR_INET
|
|
|
|
((try inet_addr_of_string host with Failure _ ->
|
|
|
|
try (gethostbyname host).h_addr_list.(0) with Not_found ->
|
2012-10-17 05:26:42 -07:00
|
|
|
prerr_endline ("Unknown host: " ^ host);
|
1997-05-19 08:42:21 -07:00
|
|
|
failwith "Can't convert address"),
|
|
|
|
(try int_of_string port with Failure _ ->
|
|
|
|
prerr_endline "The port number should be an integer";
|
|
|
|
failwith "Can't convert address")))
|
1996-11-29 08:55:09 -08:00
|
|
|
with Not_found ->
|
2008-07-29 01:31:41 -07:00
|
|
|
match Sys.os_type with
|
|
|
|
"Win32" -> failwith "Unix sockets not supported"
|
|
|
|
| _ -> (PF_UNIX, ADDR_UNIX address)
|
1996-11-29 08:55:09 -08:00
|
|
|
|
2002-11-02 14:36:46 -08:00
|
|
|
(*** Report a unix error. ***)
|
1996-11-29 08:55:09 -08:00
|
|
|
let report_error = function
|
2000-03-06 14:12:09 -08:00
|
|
|
| Unix_error (err, fun_name, arg) ->
|
2012-10-17 05:26:42 -07:00
|
|
|
prerr_string "Unix error: '";
|
1996-11-29 08:55:09 -08:00
|
|
|
prerr_string fun_name;
|
|
|
|
prerr_string "' failed";
|
|
|
|
if String.length arg > 0 then
|
|
|
|
(prerr_string " on '";
|
|
|
|
prerr_string arg;
|
|
|
|
prerr_string "'");
|
2012-10-17 05:26:42 -07:00
|
|
|
prerr_string ": ";
|
1996-11-29 08:55:09 -08:00
|
|
|
prerr_endline (error_message err)
|
2002-11-02 14:36:46 -08:00
|
|
|
| _ -> fatal_error "report_error: not a Unix error"
|
1996-11-29 08:55:09 -08:00
|
|
|
|
|
|
|
(* Find program `name' in `PATH'. *)
|
|
|
|
(* Return the full path if found. *)
|
|
|
|
(* Raise `Not_found' otherwise. *)
|
|
|
|
let search_in_path name =
|
2012-05-30 07:52:37 -07:00
|
|
|
Printf.fprintf Pervasives.stderr "search_in_path [%s]\n%!" name;
|
1996-11-29 08:55:09 -08:00
|
|
|
let check name =
|
|
|
|
try access name [X_OK]; name with Unix_error _ -> raise Not_found
|
|
|
|
in
|
2002-11-02 14:36:46 -08:00
|
|
|
if not (Filename.is_implicit name) then
|
1996-11-29 08:55:09 -08:00
|
|
|
check name
|
|
|
|
else
|
|
|
|
let path = Sys.getenv "PATH" in
|
|
|
|
let length = String.length path in
|
|
|
|
let rec traverse pointer =
|
2000-03-07 10:22:19 -08:00
|
|
|
if (pointer >= length) || (path.[pointer] = ':') then
|
1996-11-29 08:55:09 -08:00
|
|
|
pointer
|
|
|
|
else
|
|
|
|
traverse (pointer + 1)
|
|
|
|
in
|
|
|
|
let rec find pos =
|
|
|
|
let pos2 = traverse pos in
|
|
|
|
let directory = (String.sub path pos (pos2 - pos)) in
|
1997-05-19 08:42:21 -07:00
|
|
|
let fullname =
|
2000-03-07 10:22:19 -08:00
|
|
|
if directory = "" then name else directory ^ "/" ^ name
|
1996-11-29 08:55:09 -08:00
|
|
|
in
|
|
|
|
try check fullname with
|
2000-03-07 10:22:19 -08:00
|
|
|
| Not_found ->
|
|
|
|
if pos2 < length then find (pos2 + 1)
|
|
|
|
else raise Not_found
|
1996-11-29 08:55:09 -08:00
|
|
|
in
|
|
|
|
find 0
|
|
|
|
|
|
|
|
(* Expand a path. *)
|
|
|
|
(* ### path -> path' *)
|
|
|
|
let rec expand_path ch =
|
|
|
|
let rec subst_variable ch =
|
|
|
|
try
|
2009-05-20 04:52:42 -07:00
|
|
|
let pos = String.index ch '$' in
|
2000-03-07 10:22:19 -08:00
|
|
|
if (pos + 1 < String.length ch) && (ch.[pos + 1] = '$') then
|
1997-05-19 08:42:21 -07:00
|
|
|
(String.sub ch 0 (pos + 1))
|
|
|
|
^ (subst_variable
|
|
|
|
(String.sub ch (pos + 2) (String.length ch - pos - 2)))
|
1996-11-29 08:55:09 -08:00
|
|
|
else
|
|
|
|
(String.sub ch 0 pos)
|
|
|
|
^ (subst2 (String.sub ch (pos + 1) (String.length ch - pos - 1)))
|
|
|
|
with Not_found ->
|
|
|
|
ch
|
|
|
|
and subst2 ch =
|
|
|
|
let suiv =
|
|
|
|
let i = ref 0 in
|
|
|
|
while !i < String.length ch &&
|
|
|
|
(let c = ch.[!i] in (c >= 'a' && c <= 'z')
|
|
|
|
|| (c >= 'A' && c <= 'Z')
|
|
|
|
|| (c >= '0' && c <= '9')
|
|
|
|
|| c = '_')
|
|
|
|
do incr i done;
|
|
|
|
!i
|
|
|
|
in (Sys.getenv (String.sub ch 0 suiv))
|
|
|
|
^ (subst_variable (String.sub ch suiv (String.length ch - suiv)))
|
|
|
|
in
|
|
|
|
let ch = subst_variable ch in
|
|
|
|
let concat_root nom ch2 =
|
|
|
|
try Filename.concat (getpwnam nom).pw_dir ch2
|
|
|
|
with Not_found ->
|
|
|
|
"~" ^ nom
|
|
|
|
in
|
|
|
|
if ch.[0] = '~' then
|
1997-05-19 08:42:21 -07:00
|
|
|
try
|
2009-05-20 04:52:42 -07:00
|
|
|
match String.index ch '/' with
|
1996-11-29 08:55:09 -08:00
|
|
|
1 ->
|
|
|
|
(let tail = String.sub ch 2 (String.length ch - 2)
|
|
|
|
in
|
|
|
|
try Filename.concat (Sys.getenv "HOME") tail
|
|
|
|
with Not_found ->
|
|
|
|
concat_root (Sys.getenv "LOGNAME") tail)
|
|
|
|
| n -> concat_root
|
|
|
|
(String.sub ch 1 (n - 1))
|
|
|
|
(String.sub ch (n + 1) (String.length ch - n - 1))
|
|
|
|
with
|
1997-05-19 08:42:21 -07:00
|
|
|
Not_found ->
|
1996-11-29 08:55:09 -08:00
|
|
|
expand_path (ch ^ "/")
|
|
|
|
else ch
|
2002-11-02 14:36:46 -08:00
|
|
|
|
|
|
|
let make_absolute name =
|
|
|
|
if Filename.is_relative name
|
|
|
|
then Filename.concat (getcwd ()) name
|
|
|
|
else name
|
|
|
|
;;
|