aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-11-27 12:35:52 -0500
committerBruce Hill <bruce@bruce-hill.com>2025-11-27 12:35:52 -0500
commit8b897851facaa177e2346e0d97fcba7411dfc0aa (patch)
treef2f3ef96a5628ec4d76c4097ef7c26a85f1d855a
parent31c47caa0282cbdf43361a3c0f45fff9699a8814 (diff)
Update `setenv()` to take an optional value, also bugfix for `setenv()`
returning a value.
-rw-r--r--CHANGES.md2
-rw-r--r--Makefile1
-rw-r--r--api/api.md4
-rw-r--r--api/builtins.md4
-rw-r--r--api/builtins.yaml5
-rw-r--r--man/man3/tomo-setenv.36
-rw-r--r--src/environment.c2
-rw-r--r--src/stdlib/stdlib.c5
8 files changed, 17 insertions, 12 deletions
diff --git a/CHANGES.md b/CHANGES.md
index f54e4d05..0bbe387f 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -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}`.
diff --git a/Makefile b/Makefile
index b8220a95..ab9096cd 100644
--- a/Makefile
+++ b/Makefile
@@ -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
diff --git a/api/api.md b/api/api.md
index ec6ffb49..f52691d3 100644
--- a/api/api.md
+++ b/api/api.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;