Skip to main content

strat9_kernel/shell/commands/util/
env.rs

1/*
2 * This file is part of strat9-os.
3 *
4 * strat9-os is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * strat9-os is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with strat9-os. If not, see <https://www.gnu.org/licenses/>.
16 *
17 * Website: https://www.strat9-os.org
18 * Repository: https://github.com/strat9-os/strat9-os
19 *
20 * Copyright (C) 2023-26 Guillaume Gielly
21 */
22
23use super::*;
24use alloc::{collections::BTreeMap, string::String};
25
26static SHELL_ENV: crate::sync::SpinLock<Option<BTreeMap<String, String>>> =
27    crate::sync::SpinLock::new(None);
28
29/// Initialize kernel shell environment with default values.
30pub fn init_shell_env() {
31    let mut map = BTreeMap::new();
32    map.insert(String::from("KERNEL"), String::from("strat9"));
33    map.insert(String::from("ARCH"), String::from("x86_64"));
34    map.insert(String::from("SHELL"), String::from("chevron"));
35    map.insert(String::from("HOME"), String::from("/"));
36    map.insert(String::from("PATH"), String::from("/initfs/bin"));
37    *SHELL_ENV.lock() = Some(map);
38}
39
40/// Get a shell environment variable by key.
41pub fn shell_getenv(key: &str) -> Option<String> {
42    SHELL_ENV.lock().as_ref().and_then(|m| m.get(key).cloned())
43}
44
45/// Set a shell environment variable.
46pub fn shell_setenv(key: &str, val: &str) {
47    let mut guard = SHELL_ENV.lock();
48    let map = guard.get_or_insert_with(BTreeMap::new);
49    map.insert(String::from(key), String::from(val));
50}
51
52/// Remove a shell environment variable.
53pub fn shell_unsetenv(key: &str) {
54    if let Some(map) = SHELL_ENV.lock().as_mut() {
55        map.remove(key);
56    }
57}
58
59/// Display all shell environment variables.
60pub fn cmd_env(_args: &[String]) -> Result<(), ShellError> {
61    if let Some(map) = SHELL_ENV.lock().as_ref() {
62        for (k, v) in map.iter() {
63            shell_println!("{}={}", k, v);
64        }
65    }
66
67    let ticks = crate::process::scheduler::ticks();
68    let hz = crate::arch::timer::TIMER_HZ;
69    shell_println!("UPTIME_SECS={}", ticks / hz);
70    shell_println!("SILO_COUNT={}", crate::silo::list_silos_snapshot().len());
71    shell_println!("MOUNT_COUNT={}", vfs::list_mounts().len());
72    Ok(())
73}
74
75/// Set a shell environment variable: `setenv KEY=VALUE`.
76pub fn cmd_setenv(args: &[String]) -> Result<(), ShellError> {
77    if args.is_empty() {
78        shell_println!("Usage: setenv KEY=VALUE");
79        return Err(ShellError::InvalidArguments);
80    }
81    let arg = &args[0];
82    if let Some(eq_pos) = arg.find('=') {
83        let key = &arg[..eq_pos];
84        let val = &arg[eq_pos + 1..];
85        shell_setenv(key, val);
86    } else {
87        shell_setenv(arg, "");
88    }
89    Ok(())
90}
91
92/// Remove a shell environment variable: `unsetenv KEY`.
93pub fn cmd_unsetenv(args: &[String]) -> Result<(), ShellError> {
94    if args.is_empty() {
95        shell_println!("Usage: unsetenv KEY");
96        return Err(ShellError::InvalidArguments);
97    }
98    shell_unsetenv(&args[0]);
99    Ok(())
100}