Add bring-your-own-buffer versions of os.lookup_env and os.get_env

And make `core:terminal` use it so that `core:log` can be imported with `-default-to-nil-allocator`,
in which the actual allocator is set up in `main()`.

Windows was tricky because of the utf-8 <> utf-16 conversion, so we use some temporary stack buffers for that purpose,
limiting the non-allocating version there to 512 utf-16 characters each for the key and environment value.

In general the value is (obviously) limited to the size of the supplied buffer, and a `.Buffer_Full` error is returned
if that buffer is insufficient. If the key is not found, the procedure returns `.Env_Var_Not_Found`.

TODO:
- Factor out buffer-backed utf8 + utf16 conversion to `core:sys/util` to more easily apply this pattern.
- Add similar `lookup_env` and `get_env` procedures to `core:os/os2`.

Fixes #5336
This commit is contained in:
Jeroen van Rijn
2025-06-16 20:12:26 +02:00
parent 1bd48df41f
commit 1a2f83f123
10 changed files with 266 additions and 30 deletions
+5 -6
View File
@@ -11,17 +11,17 @@ import "core:strings"
// - [[ https://invisible-island.net/ncurses/terminfo.src.html ]]
get_no_color :: proc() -> bool {
if no_color, ok := os.lookup_env("NO_COLOR"); ok {
defer delete(no_color)
buf: [128]u8
if no_color, err := os.lookup_env(buf[:], "NO_COLOR"); err == nil {
return no_color != ""
}
return false
}
get_environment_color :: proc() -> Color_Depth {
buf: [128]u8
// `COLORTERM` is non-standard but widespread and unambiguous.
if colorterm, ok := os.lookup_env("COLORTERM"); ok {
defer delete(colorterm)
if colorterm, err := os.lookup_env(buf[:], "COLORTERM"); err == nil {
// These are the only values that are typically advertised that have
// anything to do with color depth.
if colorterm == "truecolor" || colorterm == "24bit" {
@@ -29,8 +29,7 @@ get_environment_color :: proc() -> Color_Depth {
}
}
if term, ok := os.lookup_env("TERM"); ok {
defer delete(term)
if term, err := os.lookup_env(buf[:], "TERM"); err == nil {
if strings.contains(term, "-truecolor") {
return .True_Color
}