|
1 | 1 | # This file is a part of Julia. License is MIT: https://julialang.org/license |
2 | 2 |
|
3 | | -if Sys.iswindows() |
4 | | - const ERROR_ENVVAR_NOT_FOUND = UInt32(203) |
5 | | - |
6 | | - _getenvlen(var::Vector{UInt16}) = ccall(:GetEnvironmentVariableW,stdcall,UInt32,(Ptr{UInt16},Ptr{UInt16},UInt32),var,C_NULL,0) |
7 | | - _hasenv(s::Vector{UInt16}) = _getenvlen(s) != 0 || Libc.GetLastError() != ERROR_ENVVAR_NOT_FOUND |
8 | | - _hasenv(s::AbstractString) = _hasenv(cwstring(s)) |
9 | | - |
10 | | - function access_env(onError::Function, str::AbstractString) |
11 | | - var = cwstring(str) |
12 | | - len = _getenvlen(var) |
13 | | - if len == 0 |
14 | | - return Libc.GetLastError() != ERROR_ENVVAR_NOT_FOUND ? "" : onError(str) |
15 | | - end |
16 | | - val = zeros(UInt16,len) |
17 | | - ret = ccall(:GetEnvironmentVariableW,stdcall,UInt32,(Ptr{UInt16},Ptr{UInt16},UInt32),var,val,len) |
18 | | - windowserror(:getenv, (ret == 0 && len != 1) || ret != len-1 || val[end] != 0) |
19 | | - pop!(val) # NUL |
20 | | - return transcode(String, val) |
21 | | - end |
22 | | - |
23 | | - function _setenv(svar::AbstractString, sval::AbstractString, overwrite::Bool=true) |
24 | | - var = cwstring(svar) |
25 | | - val = cwstring(sval) |
26 | | - if overwrite || !_hasenv(var) |
27 | | - ret = ccall(:SetEnvironmentVariableW,stdcall,Int32,(Ptr{UInt16},Ptr{UInt16}),var,val) |
28 | | - windowserror(:setenv, ret == 0) |
29 | | - end |
30 | | - end |
31 | | - |
32 | | - function _unsetenv(svar::AbstractString) |
33 | | - var = cwstring(svar) |
34 | | - ret = ccall(:SetEnvironmentVariableW,stdcall,Int32,(Ptr{UInt16},Ptr{UInt16}),var,C_NULL) |
35 | | - windowserror(:setenv, ret == 0) |
36 | | - end |
37 | | -else # !windows |
38 | | - _getenv(var::AbstractString) = ccall(:getenv, Cstring, (Cstring,), var) |
39 | | - _hasenv(s::AbstractString) = _getenv(s) != C_NULL |
40 | | - |
41 | | - function access_env(onError::Function, var::AbstractString) |
42 | | - val = _getenv(var) |
43 | | - val == C_NULL ? onError(var) : unsafe_string(val) |
44 | | - end |
45 | | - |
46 | | - function _setenv(var::AbstractString, val::AbstractString, overwrite::Bool=true) |
47 | | - ret = ccall(:setenv, Int32, (Cstring,Cstring,Int32), var, val, overwrite) |
48 | | - systemerror(:setenv, ret != 0) |
49 | | - end |
50 | | - |
51 | | - function _unsetenv(var::AbstractString) |
52 | | - ret = ccall(:unsetenv, Int32, (Cstring,), var) |
53 | | - systemerror(:unsetenv, ret != 0) |
54 | | - end |
55 | | -end # os test |
56 | | - |
57 | | -## ENV: hash interface ## |
58 | | - |
59 | | -""" |
60 | | - EnvDict() -> EnvDict |
61 | | -
|
62 | | -A singleton of this type provides a hash table interface to environment variables. |
63 | | -""" |
64 | | -struct EnvDict <: AbstractDict{String,String}; end |
65 | | - |
66 | | -""" |
67 | | - ENV |
68 | | -
|
69 | | -Reference to the singleton `EnvDict`, providing a dictionary interface to system environment |
70 | | -variables. |
71 | | -
|
72 | | -(On Windows, system environment variables are case-insensitive, and `ENV` correspondingly converts |
73 | | -all keys to uppercase for display, iteration, and copying. Portable code should not rely on the |
74 | | -ability to distinguish variables by case, and should beware that setting an ostensibly lowercase |
75 | | -variable may result in an uppercase `ENV` key.) |
76 | | -""" |
77 | | -const ENV = EnvDict() |
78 | | - |
79 | | -getindex(::EnvDict, k::AbstractString) = access_env(k->throw(KeyError(k)), k) |
80 | | -get(::EnvDict, k::AbstractString, def) = access_env(k->def, k) |
81 | | -get(f::Callable, ::EnvDict, k::AbstractString) = access_env(k->f(), k) |
82 | | -in(k::AbstractString, ::KeySet{String, EnvDict}) = _hasenv(k) |
83 | | -pop!(::EnvDict, k::AbstractString) = (v = ENV[k]; _unsetenv(k); v) |
84 | | -pop!(::EnvDict, k::AbstractString, def) = haskey(ENV,k) ? pop!(ENV,k) : def |
85 | | -delete!(::EnvDict, k::AbstractString) = (_unsetenv(k); ENV) |
86 | | -setindex!(::EnvDict, v, k::AbstractString) = _setenv(k,string(v)) |
87 | | -push!(::EnvDict, kv::Pair{<:AbstractString}) = setindex!(ENV, kv.second, kv.first) |
88 | | - |
89 | | -if Sys.iswindows() |
90 | | - GESW() = (pos = ccall(:GetEnvironmentStringsW,stdcall,Ptr{UInt16},()); (pos,pos)) |
91 | | - function winuppercase(s::AbstractString) |
92 | | - isempty(s) && return s |
93 | | - LOCALE_INVARIANT = 0x0000007f |
94 | | - LCMAP_UPPERCASE = 0x00000200 |
95 | | - ws = transcode(UInt16, String(s)) |
96 | | - result = ccall(:LCMapStringW, stdcall, Cint, (UInt32, UInt32, Ptr{UInt16}, Cint, Ptr{UInt16}, Cint), |
97 | | - LOCALE_INVARIANT, LCMAP_UPPERCASE, ws, length(ws), ws, length(ws)) |
98 | | - systemerror(:LCMapStringW, iszero(result)) |
99 | | - return transcode(String, ws) |
100 | | - end |
101 | | - function iterate(hash::EnvDict, block::Tuple{Ptr{UInt16},Ptr{UInt16}} = GESW()) |
102 | | - if unsafe_load(block[1]) == 0 |
103 | | - ccall(:FreeEnvironmentStringsW, stdcall, Int32, (Ptr{UInt16},), block[2]) |
104 | | - return nothing |
105 | | - end |
106 | | - pos = block[1] |
107 | | - blk = block[2] |
108 | | - len = ccall(:wcslen, UInt, (Ptr{UInt16},), pos) |
109 | | - buf = Vector{UInt16}(undef, len) |
110 | | - GC.@preserve buf unsafe_copyto!(pointer(buf), pos, len) |
111 | | - env = transcode(String, buf) |
112 | | - m = match(r"^(=?[^=]+)=(.*)$"s, env) |
113 | | - if m === nothing |
114 | | - error("malformed environment entry: $env") |
115 | | - end |
116 | | - return (Pair{String,String}(winuppercase(m.captures[1]), m.captures[2]), (pos+(len+1)*2, blk)) |
117 | | - end |
118 | | -else # !windows |
119 | | - function iterate(::EnvDict, i=0) |
120 | | - env = ccall(:jl_environ, Any, (Int32,), i) |
121 | | - env === nothing && return nothing |
122 | | - env = env::String |
123 | | - m = match(r"^(.*?)=(.*)$"s, env) |
124 | | - if m === nothing |
125 | | - error("malformed environment entry: $env") |
126 | | - end |
127 | | - return (Pair{String,String}(m.captures[1], m.captures[2]), i+1) |
128 | | - end |
129 | | -end # os-test |
130 | | - |
131 | | -#TODO: Make these more efficient |
132 | | -function length(::EnvDict) |
133 | | - i = 0 |
134 | | - for (k,v) in ENV |
135 | | - i += 1 |
136 | | - end |
137 | | - return i |
138 | | -end |
139 | | - |
140 | | -function show(io::IO, ::EnvDict) |
141 | | - for (k,v) = ENV |
142 | | - println(io, "$k=$v") |
143 | | - end |
144 | | -end |
145 | | - |
146 | 3 | """ |
147 | 4 | withenv(f::Function, kv::Pair...) |
148 | 5 |
|
149 | | -Execute `f` in an environment that is temporarily modified (not replaced as in `setenv`) |
| 6 | +Execute `f` in an environment that is temporarily modified (not replaced as in`setenv`) |
150 | 7 | by zero or more `"var"=>val` arguments `kv`. `withenv` is generally used via the |
151 | | -`withenv(kv...) do ... end` syntax. A value of `nothing` can be used to temporarily unset an |
152 | | -environment variable (if it is set). When `withenv` returns, the original environment has |
153 | | -been restored. |
| 8 | +`withenv(kv...) do ... end` syntax. A value of `nothing` can be used to temporarily |
| 9 | +unset an environment variable (if it is set). When `withenv` returns, the original |
| 10 | +environment has been restored. |
154 | 11 | """ |
155 | 12 | function withenv(f::Function, keyvals::Pair{T}...) where T<:AbstractString |
156 | 13 | old = Dict{T,Any}() |
157 | | - for (key,val) in keyvals |
158 | | - old[key] = get(ENV,key,nothing) |
159 | | - val !== nothing ? (ENV[key]=val) : delete!(ENV, key) |
| 14 | + for (key, val) in keyvals |
| 15 | + old[key] = get(ENV, key, nothing) |
| 16 | + if val === nothing |
| 17 | + delete!(ENV, key) |
| 18 | + else |
| 19 | + ENV[key] = val |
| 20 | + end |
160 | 21 | end |
161 | 22 | try f() |
162 | 23 | finally |
163 | | - for (key,val) in old |
164 | | - val !== nothing ? (ENV[key]=val) : delete!(ENV, key) |
| 24 | + for (key, val) in old |
| 25 | + if val === nothing |
| 26 | + delete!(ENV, key) |
| 27 | + else |
| 28 | + ENV[key] = val |
| 29 | + end |
165 | 30 | end |
166 | 31 | end |
167 | 32 | end |
|
0 commit comments