summary refs log tree commit diff stats
path: root/lib/std/private/win_setenv.nim
blob: 067e656a317f79c8541a2a9d22583403c4630c7b (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
#[
Copyright (c) Facebook, Inc. and its affiliates.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Adapted `setenv` from https://github.com/facebook/folly/blob/master/folly/portability/Stdlib.cpp
translated from C to nim.
]#

#[
Introduced in https://github.com/facebook/folly/commit/5d8ca09a3f96afefb44e35808f03651a096ab9c7

TODO:
check errno_t vs cint
]#

when not defined(windows): discard
else:
  proc setEnvironmentVariableA*(lpName, lpValue: cstring): int32 {.
    stdcall, dynlib: "kernel32", importc: "SetEnvironmentVariableA", sideEffect.}
    # same as winlean.setEnvironmentVariableA

  proc c_getenv(env: cstring): cstring {.importc: "getenv", header: "<stdlib.h>".}
  proc c_putenv_s(envname: cstring, envval: cstring): cint {.importc: "_putenv_s", header: "<stdlib.h>".}
  proc c_wgetenv(varname: WideCString): WideCString {.importc: "_wgetenv", header: "<stdlib.h>".}

  var errno {.importc, header: "<errno.h>".}: cint
  type wchar_t  {.importc: "wchar_t".} = int16
  var gWenviron {.importc:"_wenviron".}: ptr ptr wchar_t
    # xxx `ptr UncheckedArray[WideCString]` did not work

  proc mbstowcs_s(pReturnValue: ptr csize_t, wcstr: WideCString, sizeInWords: csize_t, mbstr: cstring, count: csize_t): cint {.importc: "mbstowcs_s", header: "<stdlib.h>".}
    # xxx cint vs errno_t?

  proc setEnvImpl*(name: cstring, value: cstring, overwrite: cint): cint =
    const EINVAL = cint(22)
    const MAX_ENV = 32767
      # xxx get it from: `var MAX_ENV {.importc: "_MAX_ENV", header:"<stdlib.h>".}: cint`
    if overwrite == 0 and c_getenv(name) != nil: return 0
    if value[0] != '\0':
      let e = c_putenv_s(name, value)
      if e != 0:
        errno = e
        return -1
      return 0
    #[
    We are trying to set the value to an empty string, but `_putenv_s` deletes
    entries if the value is an empty string, and just calling
    SetEnvironmentVariableA doesn't update `_environ`,
    so we have to do these terrible things.
    ]#
    if c_putenv_s(name, "  ") != 0:
      errno = EINVAL
      return -1
    # Here lies the documentation we blatently ignore to make this work.
    var s = c_getenv(name)
    s[0] = '\0'
    #[
    This would result in a double null termination, which normally signifies the
    end of the environment variable list, so we stick a completely empty
    environment variable into the list instead.
    ]#
    s[1] = '='
    #[
    If gWenviron is null, the wide environment has not been initialized
    yet, and we don't need to try to update it. We have to do this otherwise
    we'd be forcing the initialization and maintenance of the wide environment
    even though it's never actually used in most programs.
    ]#
    if gWenviron != nil:
      # var buf: array[MAX_ENV + 1, WideCString]
      var buf: array[MAX_ENV + 1, Utf16Char]
      let buf2 = cast[WideCString](buf[0].addr)
      var len: csize_t
      if mbstowcs_s(len.addr, buf2, buf.len.csize_t, name, MAX_ENV) != 0:
        errno = EINVAL
        return -1
      c_wgetenv(buf2)[0] = '\0'.Utf16Char
      c_wgetenv(buf2)[1] = '='.Utf16Char

    # And now, we have to update the outer environment to have a proper empty value.
    if setEnvironmentVariableA(name, value) == 0:
      errno = EINVAL
      return -1
    return 0
end >end - __teliva_timestamp: >Sat Feb 26 21:54:53 2022 filter_task: >function filter_task(p, cin, cout) > while true do > local i = cin:recv() > if i%p ~= 0 then > cout:send(i) > end > end >end - __teliva_timestamp: >Sat Feb 26 21:55:46 2022 main_task: >function main_task() > local primes = task.Channel:new() > task.spawn(sieve, primes) > for i=1,10 do > print(primes:recv()) > end >end - __teliva_timestamp: >Sat Feb 26 21:59:37 2022 __teliva_note: >filter out multiples of a single number sieve: >function sieve(ch) > local iota = task.Channel:new() > task.spawn(counter, iota) > task.spawn(filter_task, 2, iota, ch) >end - __teliva_timestamp: >Sat Feb 26 22:08:07 2022 __teliva_note: >implement the complete sieve algorithm sieve: >-- Set up a Sieve of Eratosthenes (https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes) >-- for computing prime numbers by chaining tasks, one per prime. >-- Each task is responsible for filtering out all multiples of its prime. >function sieve(primes_ch) > local c = task.Channel:new() > task.spawn(counter, c) > while true do > local p, newc = c:recv(), task.Channel:new() > primes_ch:send(p) > task.spawn(filter_task, p, c, newc) > c = newc > end >end - __teliva_timestamp: >Sat Feb 26 22:09:47 2022 main_task: >function main_task(window) > local primes = task.Channel:new() > task.spawn(sieve, primes) > while true do > window:addstr(primes:recv()) > window:addstr(' ') > window:refresh() > end >end - __teliva_timestamp: >Sat Feb 26 22:08:52 2022 __teliva_note: >infinite primes main: >function main() > Window:nodelay(true) > Window:clear() > task.spawn(main_task, Window) > task.scheduler() > print('key pressed; done') > Window:nodelay(false) > Window:getch() >end - __teliva_timestamp: >Sat Feb 26 22:09:47 2022 __teliva_note: >clear screen when it fills up; pause on keypress > >In Teliva getch() implicitly refreshes the screen. main_task: >function main_task(window) > local primes = task.Channel:new() > task.spawn(sieve, primes) > local h, w = window:getmaxyx() > while true do > window:addstr(primes:recv()) > window:addstr(' ') > local c = window:getch() > if c then break end -- key pressed > local y, x = window:getyx() > if y > h-1 then > window:clear() > end > end >end - __teliva_timestamp: >Sat Feb 26 22:27:25 2022 doc:blurb: >Sieve of Eratosthenes >https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes > >A demonstration of tasks and channels, the primitives for (cooperative) concurrency in Teliva. > >We string together a cascade of tasks connected by channels. Every prime number gets a new task that prints the first incoming number, and then filters out multiples of it from the incoming channel. > >This approach has the advantage that we don't need to create an array of n numbers to compute primes less than n. > >However, we still need to create p tasks and p channels if there are p primes less than n. Probably not worth it, given tasks and channels are much larger than numbers. This is just a demo. > >The noticeable periodic pauses are perhaps due to garbage collection.