SCM Repository
Annotation of /sml/trunk/src/smlnj-lib/Unix/unix-env.sml
Parent Directory
|
Revision Log
Revision 651 - (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 : | local | ||
17 : | fun isEqual #"=" = true | isEqual _ = false | ||
18 : | in | ||
19 : | fun splitBinding s = (case (String.fields isEqual s) | ||
20 : | of [a, b] => (a, b) | ||
21 : | | _ => (s, "") | ||
22 : | (* end case *)) | ||
23 : | end | ||
24 : | |||
25 : | (* return the value, if any, bound to the name. *) | ||
26 : | fun getFromEnv (name, env) = let | ||
27 : | fun look [] = NONE | ||
28 : | | look (s::r) = let | ||
29 : | val (n, v) = splitBinding s | ||
30 : | in | ||
31 : | if (n = name) then (SOME v) else look r | ||
32 : | end | ||
33 : | in | ||
34 : | look env | ||
35 : | end | ||
36 : | |||
37 : | (* return the value bound to the name, or a default value *) | ||
38 : | fun getValue {name, default, env} = (case getFromEnv(name, env) | ||
39 : | of (SOME s) => s | ||
40 : | | NONE => default | ||
41 : | (* end case *)) | ||
42 : | |||
43 : | (* remove a binding from an environment *) | ||
44 : | fun removeFromEnv (name, env) = let | ||
45 : | fun look [] = [] | ||
46 : | | look (s::r) = let | ||
47 : | val (n, v) = splitBinding s | ||
48 : | in | ||
49 : | if (n = name) then r else (s :: look r) | ||
50 : | end | ||
51 : | in | ||
52 : | look env | ||
53 : | end | ||
54 : | |||
55 : | (* add a binding to an environment, replacing an existing binding | ||
56 : | * if necessary. | ||
57 : | *) | ||
58 : | fun addToEnv (nameValue, env) = let | ||
59 : | val (name, _) = splitBinding nameValue | ||
60 : | fun look [] = [nameValue] | ||
61 : | | look (s::r) = let | ||
62 : | val (n, v) = splitBinding s | ||
63 : | in | ||
64 : | if (n = name) then r else (s :: look r) | ||
65 : | end | ||
66 : | in | ||
67 : | look env | ||
68 : | end | ||
69 : | |||
70 : | (* return the user's environment *) | ||
71 : | val environ = Posix.ProcEnv.environ | ||
72 : | |||
73 : | (* return the binding of an environment variable in the | ||
74 : | * user's environment. | ||
75 : | *) | ||
76 : | fun getEnv name = getFromEnv(name, environ()) | ||
77 : | |||
78 : | end; (* UnixEnv *) | ||
79 : |
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |