summary refs log tree commit diff stats
path: root/lib/std/envvars.nim
blob: ab5c9f06ed557c6dcb919cb1b0fabbf63274a6f4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#
#
#            Nim's Runtime Library
#        (c) Copyright 2022 Nim contributors
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#


## The `std/envvars` module implements environment variable handling.
import std/oserrors

type
  ReadEnvEffect* = object of ReadIOEffect   ## Effect that denotes a read
                                            ## from an environment variable.
  WriteEnvEffect* = object of WriteIOEffect ## Effect that denotes a write
                                            ## to an environment variable.


when not defined(nimscript):
  when defined(nodejs):
    proc getEnv*(key: string, default = ""): string {.tags: [ReadEnvEffect].} =
      var ret = default.cstring
      let key2 = key.cstring
      {.emit: "const value = process.env[`key2`];".}
      {.emit: "if (value !== undefined) { `ret` = value };".}
      result = $ret

    proc existsEnv*(key: string): bool {.tags: [ReadEnvEffect].} =
      var key2 = key.cstring
      var ret: bool
      {.emit: "`ret` = `key2` in process.env;".}
      result = ret

    proc putEnv*(key, val: string) {.tags: [WriteEnvEffect].} =
      var key2 = key.cstring
      var val2 = val.cstring
      {.emit: "process.env[`key2`] = `val2`;".}

    proc delEnv*(key: string) {.tags: [WriteEnvEffect].} =
      var key2 = key.cstring
      {.emit: "delete process.env[`key2`];".}

    iterator envPairsImpl(): tuple[key, value: string] {.tags: [ReadEnvEffect].} =
      var num: int
      var keys: RootObj
      {.emit: "`keys` = Object.keys(process.env); `num` = `keys`.length;".}
      for i in 0..<num:
        var key, value: cstring
        {.emit: "`key` = `keys`[`i`]; `value` = process.env[`key`];".}
        yield ($key, $value)

  # commented because it must keep working with js+VM
  # elif defined(js):
  #   {.error: "requires -d:nodejs".}

  else:

    when defined(windows):
      proc c_putenv(envstring: cstring): cint {.importc: "_putenv", header: "<stdlib.h>".}
      from std/private/win_setenv import setEnvImpl
      import winlean
      when defined(nimPreviewSlimSystem):
        import std/widestrs

      type wchar_t {.importc: "wchar_t", header: "<stdlib.h>".} = int16
      proc c_wgetenv(varname: ptr wchar_t): ptr wchar_t {.importc: "_wgetenv",
          header: "<stdlib.h>".}
      proc getEnvImpl(env: cstring): WideCString =
        let r: WideCString = env.newWideCString
        cast[WideCString](c_wgetenv(cast[ptr wchar_t](r)))
    else:
      proc c_getenv(env: cstring): cstring {.
        importc: "getenv", header: "<stdlib.h>".}
      proc c_setenv(envname: cstring, envval: cstring, overwrite: cint): cint {.importc: "setenv", header: "<stdlib.h>".}
      proc c_unsetenv(env: cstring): cint {.importc: "unsetenv", header: "<stdlib.h>".}
      proc getEnvImpl(env: cstring): cstring = c_getenv(env)

    proc getEnv*(key: string, default = ""): string {.tags: [ReadEnvEffect].} =
      ## Returns the value of the `environment variable`:idx: named `key`.
      ##
      ## If the variable does not exist, `""` is returned. To distinguish
      ## whether a variable exists or it's value is just `""`, call
      ## `existsEnv(key) proc`_.
      ##
      ## See also:
      ## * `existsEnv proc`_
      ## * `putEnv proc`_
      ## * `delEnv proc`_
      ## * `envPairs iterator`_
      runnableExamples:
        assert getEnv("unknownEnv") == ""
        assert getEnv("unknownEnv", "doesn't exist") == "doesn't exist"

      let env = getEnvImpl(key)
      if env == nil:
        result = default
      else:
        result = $env

    proc existsEnv*(key: string): bool {.tags: [ReadEnvEffect].} =
      ## Checks whether the environment variable named `key` exists.
      ## Returns true if it exists, false otherwise.
      ##
      ## See also:
      ## * `getEnv proc`_
      ## * `putEnv proc`_
      ## * `delEnv proc`_
      ## * `envPairs iterator`_
      runnableExamples:
        assert not existsEnv("unknownEnv")

      result = getEnvImpl(key) != nil

    proc putEnv*(key, val: string) {.tags: [WriteEnvEffect].} =
      ## Sets the value of the `environment variable`:idx: named `key` to `val`.
      ## If an error occurs, `OSError` is raised.
      ##
      ## See also:
      ## * `getEnv proc`_
      ## * `existsEnv proc`_
      ## * `delEnv proc`_
      ## * `envPairs iterator`_
      when defined(windows):
        if key.len == 0 or '=' in key:
          raise newException(OSError, "invalid key, got: " & $(key, val))
        if setEnvImpl(key, val, 1'i32) != 0'i32:
          raiseOSError(osLastError(), $(key, val))
      else:
        if c_setenv(key, val, 1'i32) != 0'i32:
          raiseOSError(osLastError(), $(key, val))

    proc delEnv*(key: string) {.tags: [WriteEnvEffect].} =
      ## Deletes the `environment variable`:idx: named `key`.
      ## If an error occurs, `OSError` is raised.
      ##
      ## See also:ven
      ## * `getEnv proc`_
      ## * `existsEnv proc`_
      ## * `putEnv proc`_
      ## * `envPairs iterator`_
      template bail = raiseOSError(osLastError(), key)
      when defined(windows):
        #[
        # https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/putenv-s-wputenv-s?view=msvc-160
        > You can remove a variable from the environment by specifying an empty string (that is, "") for value_string
        note that nil is not legal
        ]#
        if key.len == 0 or '=' in key:
          raise newException(OSError, "invalid key, got: " & key)
        let envToDel = key & "="
        if c_putenv(cstring envToDel) != 0'i32: bail
      else:
        if c_unsetenv(key) != 0'i32: bail

    when defined(windows):
      when defined(cpp):
        proc strEnd(cstr: WideCString, c = 0'i32): WideCString {.importcpp: "(NI16*)wcschr((const wchar_t *)#, #)",
            header: "<string.h>".}
      else:
        proc strEnd(cstr: WideCString, c = 0'i32): WideCString {.importc: "wcschr",
            header: "<string.h>".}
    elif defined(macosx) and not defined(ios) and not defined(emscripten):
      # From the manual:
      # Shared libraries and bundles don't have direct access to environ,
      # which is only available to the loader ld(1) when a complete program
      # is being linked.
      # The environment routines can still be used, but if direct access to
      # environ is needed, the _NSGetEnviron() routine, defined in
      # <crt_externs.h>, can be used to retrieve the address of environ
      # at runtime.
      proc NSGetEnviron(): ptr cstringArray {.importc: "_NSGetEnviron",
          header: "<crt_externs.h>".}
    elif defined(haiku):
      var gEnv {.importc: "environ", header: "<stdlib.h>".}: cstringArray
    else:
      var gEnv {.importc: "environ".}: cstringArray

    iterator envPairsImpl(): tuple[key, value: string] {.tags: [ReadEnvEffect].} =
      when defined(windows):
        let env = getEnvironmentStringsW()
        var e = env
        if e != nil:
          while true:
            let eend = strEnd(e)
            let kv = $e
            let p = find(kv, '=')
            yield (substr(kv, 0, p-1), substr(kv, p+1))
            e = cast[WideCString](cast[ByteAddress](eend)+2)
            if int(eend[1]) == 0: break
          discard freeEnvironmentStringsW(env)
      else:
        var i = 0
        when defined(macosx) and not defined(ios) and not defined(emscripten):
          var gEnv = NSGetEnviron()[]
        while gEnv[i] != nil:
          let kv = $gEnv[i]
          inc(i)
          let p = find(kv, '=')
          yield (substr(kv, 0, p-1), substr(kv, p+1))

proc envPairsImplSeq(): seq[tuple[key, value: string]] = discard # vmops

iterator envPairs*(): tuple[key, value: string] {.tags: [ReadEnvEffect].} =
  ## Iterate over all `environments variables`:idx:.
  ##
  ## In the first component of the tuple is the name of the current variable stored,
  ## in the second its value.
  ##
  ## Works in native backends, nodejs and vm, like the following APIs:
  ## * `getEnv proc`_
  ## * `existsEnv proc`_
  ## * `putEnv proc`_
  ## * `delEnv proc`_
  when nimvm:
    for ai in envPairsImplSeq(): yield ai
  else:
    when defined(nimscript): discard
    else:
      for ai in envPairsImpl(): yield ai