diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2025-11-27 12:35:52 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2025-11-27 12:35:52 -0500 |
| commit | 8b897851facaa177e2346e0d97fcba7411dfc0aa (patch) | |
| tree | f2f3ef96a5628ec4d76c4097ef7c26a85f1d855a | |
| parent | 31c47caa0282cbdf43361a3c0f45fff9699a8814 (diff) | |
Update `setenv()` to take an optional value, also bugfix for `setenv()`
returning a value.
| -rw-r--r-- | CHANGES.md | 2 | ||||
| -rw-r--r-- | Makefile | 1 | ||||
| -rw-r--r-- | api/api.md | 4 | ||||
| -rw-r--r-- | api/builtins.md | 4 | ||||
| -rw-r--r-- | api/builtins.yaml | 5 | ||||
| -rw-r--r-- | man/man3/tomo-setenv.3 | 6 | ||||
| -rw-r--r-- | src/environment.c | 2 | ||||
| -rw-r--r-- | src/stdlib/stdlib.c | 5 |
8 files changed, 17 insertions, 12 deletions
@@ -24,6 +24,8 @@ - Added `at_cleanup()` to register cleanup functions. - Added `recursive` argument to `Path.create_directory()` to create parent directories if needed. +- `setenv()` now takes an optional parameter for value, which allows for + unsetting environment values. - Deprecated: - Sets are no longer a separate type with separate methods. - Instead of sets, use tables with a value type of `{KeyType:Empty}`. @@ -206,7 +206,6 @@ api-docs: $(API_MD) api/api.md .PHONY: manpages manpages: $(API_YAML) man/man1/tomo.1 - rm -f man/man3/* ./scripts/mandoc_gen.py $(API_YAML) man/man1/tomo.1: docs/tomo.1.md @@ -166,7 +166,7 @@ say("world!") ## setenv ```tomo -setenv : func(name: Text, value: Text -> Void) +setenv : func(name: Text, value: Text? -> Void) ``` Sets an environment variable. @@ -174,7 +174,7 @@ Sets an environment variable. Argument | Type | Description | Default ---------|------|-------------|--------- name | `Text` | The name of the environment variable to set. | - -value | `Text` | The new value of the environment variable. | - +value | `Text?` | The new value of the environment variable. If `none`, then the environment variable will be unset. | - **Return:** Nothing. diff --git a/api/builtins.md b/api/builtins.md index 6d042741..2ef14275 100644 --- a/api/builtins.md +++ b/api/builtins.md @@ -166,7 +166,7 @@ say("world!") ## setenv ```tomo -setenv : func(name: Text, value: Text -> Void) +setenv : func(name: Text, value: Text? -> Void) ``` Sets an environment variable. @@ -174,7 +174,7 @@ Sets an environment variable. Argument | Type | Description | Default ---------|------|-------------|--------- name | `Text` | The name of the environment variable to set. | - -value | `Text` | The new value of the environment variable. | - +value | `Text?` | The new value of the environment variable. If `none`, then the environment variable will be unset. | - **Return:** Nothing. diff --git a/api/builtins.yaml b/api/builtins.yaml index 764a1bd4..af7c9319 100644 --- a/api/builtins.yaml +++ b/api/builtins.yaml @@ -157,9 +157,10 @@ setenv: description: > The name of the environment variable to set. value: - type: 'Text' + type: 'Text?' description: > - The new value of the environment variable. + The new value of the environment variable. If `none`, then the + environment variable will be unset. example: | setenv("FOOBAR", "xyz") diff --git a/man/man3/tomo-setenv.3 b/man/man3/tomo-setenv.3 index a9ed528d..faa6a662 100644 --- a/man/man3/tomo-setenv.3 +++ b/man/man3/tomo-setenv.3 @@ -2,14 +2,14 @@ .\" Copyright (c) 2025 Bruce Hill .\" All rights reserved. .\" -.TH setenv 3 2025-05-17 "Tomo man-pages" +.TH setenv 3 2025-11-27 "Tomo man-pages" .SH NAME setenv \- set an environment variable .SH LIBRARY Tomo Standard Library .SH SYNOPSIS .nf -.BI setenv\ :\ func(name:\ Text,\ value:\ Text\ ->\ Void) +.BI setenv\ :\ func(name:\ Text,\ value:\ Text?\ ->\ Void) .fi .SH DESCRIPTION Sets an environment variable. @@ -23,7 +23,7 @@ lb lb lbx lb l l l l. Name Type Description Default name Text The name of the environment variable to set. - -value Text The new value of the environment variable. - +value Text? The new value of the environment variable. If `none`, then the environment variable will be unset. - .TE .SH RETURN Nothing. diff --git a/src/environment.c b/src/environment.c index 88a15bb5..43d22c3d 100644 --- a/src/environment.c +++ b/src/environment.c @@ -540,7 +540,7 @@ env_t *global_env(bool source_mapping) { {"getenv", "getenv_text", "func(name:Text -> Text?)"}, {"print", "say", "func(text:Text, newline=yes)"}, {"say", "say", "func(text:Text, newline=yes)"}, - {"setenv", "setenv_text", "func(name:Text, value:Text -> Text?)"}, + {"setenv", "setenv_text", "func(name:Text, value:Text?)"}, {"sleep", "sleep_num", "func(seconds:Num)"}, }; diff --git a/src/stdlib/stdlib.c b/src/stdlib/stdlib.c index 21547efe..8ec9e90b 100644 --- a/src/stdlib/stdlib.c +++ b/src/stdlib/stdlib.c @@ -219,7 +219,10 @@ OptionalText_t getenv_text(Text_t name) { } public -void setenv_text(Text_t name, Text_t value) { setenv(Text$as_c_string(name), Text$as_c_string(value), 1); } +void setenv_text(Text_t name, OptionalText_t value) { + if (value.tag == TEXT_NONE) unsetenv(Text$as_c_string(name)); + else setenv(Text$as_c_string(name), Text$as_c_string(value), 1); +} typedef struct cleanup_s { Closure_t cleanup_fn; |
