Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/trunk/src/smlnj-lib/Unix/unix-env.sml
ViewVC logotype

Annotation of /sml/trunk/src/smlnj-lib/Unix/unix-env.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 967 - (view) (download)

1 : monnier 2 (* unix-env.sml
2 :     *
3 :     * COPYRIGHT (c) 1993 by AT&T Bell Laboratories. See COPYRIGHT file for details.
4 :     *
5 :     * A UNIX environment is a list of strings of the form "name=value", where
6 :     * the "=" character does not appear in name.
7 :     * NOTE: binding the user's environment as an ML value and then exporting the
8 :     * ML image can result in incorrect behavior, since the environment bound in the
9 :     * heap image may differ from the user's environment when the exported image
10 :     * is used.
11 :     *)
12 :    
13 :     structure UnixEnv : UNIX_ENV =
14 :     struct
15 :    
16 : jhr 967 structure SS = Substring
17 :    
18 : monnier 2 local
19 : jhr 967 fun notEqual #"=" = false | notEqual _ = true
20 :     val split = SS.splitl notEqual
21 : monnier 2 in
22 : jhr 967 fun splitBinding s = let
23 :     val (a, b) = split(SS.all s)
24 :     in
25 :     if SS.isEmpty b
26 :     then (s, "")
27 :     else (SS.string a, SS.string(SS.triml 1 b))
28 :     end
29 : monnier 2 end
30 :    
31 :     (* return the value, if any, bound to the name. *)
32 :     fun getFromEnv (name, env) = let
33 :     fun look [] = NONE
34 :     | look (s::r) = let
35 :     val (n, v) = splitBinding s
36 :     in
37 :     if (n = name) then (SOME v) else look r
38 :     end
39 :     in
40 :     look env
41 :     end
42 :    
43 :     (* return the value bound to the name, or a default value *)
44 :     fun getValue {name, default, env} = (case getFromEnv(name, env)
45 :     of (SOME s) => s
46 :     | NONE => default
47 :     (* end case *))
48 :    
49 :     (* remove a binding from an environment *)
50 :     fun removeFromEnv (name, env) = let
51 :     fun look [] = []
52 :     | look (s::r) = let
53 :     val (n, v) = splitBinding s
54 :     in
55 :     if (n = name) then r else (s :: look r)
56 :     end
57 :     in
58 :     look env
59 :     end
60 :    
61 :     (* add a binding to an environment, replacing an existing binding
62 :     * if necessary.
63 :     *)
64 :     fun addToEnv (nameValue, env) = let
65 :     val (name, _) = splitBinding nameValue
66 :     fun look [] = [nameValue]
67 :     | look (s::r) = let
68 :     val (n, v) = splitBinding s
69 :     in
70 :     if (n = name) then r else (s :: look r)
71 :     end
72 :     in
73 :     look env
74 :     end
75 :    
76 :     (* return the user's environment *)
77 :     val environ = Posix.ProcEnv.environ
78 :    
79 :     (* return the binding of an environment variable in the
80 :     * user's environment.
81 :     *)
82 :     fun getEnv name = getFromEnv(name, environ())
83 :    
84 :     end; (* UnixEnv *)
85 :    

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0