Implement system env and app resolver

This commit is contained in:
2025-11-23 11:54:05 +01:00
parent 4bf79e358a
commit 6f303bf656
4 changed files with 45 additions and 3 deletions

View File

@@ -5,22 +5,41 @@ import { uiarrtostr } from "../../../lib/libts/uint_arr";
import {
kmod_filesystem_listDir,
kmod_filesystem_readFile,
kmod_filesystem_stat,
} from "../filesystem/filesystem.kmod";
import { oskrnl_register } from "../../../oskrnl";
import { getPathEnv } from "../../../lib/sys/env";
export function kmod_app_init() {
oskrnl_register();
}
export function kmod_app_run(path: string) {
const appPath = kmod_app_resolve(path);
if (!appPath) throw new Error("App not found");
const meta = JSON.parse(
uiarrtostr(kmod_filesystem_readFile(Path.join(path, "meta.lam"))!)
uiarrtostr(kmod_filesystem_readFile(Path.join(appPath, "meta.lam"))!)
);
if (!meta) throw new Error("App metadata not found");
const entrypoint = Path.join(path, meta["app:entrypoint"]);
const entrypoint = Path.join(appPath, meta["app:entrypoint"]);
const code = uiarrtostr(kmod_filesystem_readFile(entrypoint)!);
iexec(code);
}
export function kmod_app_resolve(path: string): string | null {
if (kmod_filesystem_stat(path)) return path;
const pathEnv = getPathEnv();
if (!pathEnv) return null;
for (let i = 0; i < pathEnv.length; i++) {
const p = Path.join(pathEnv[i]!, path);
if (kmod_filesystem_stat(p)) return p;
}
return null;
}

22
src/os/src/lib/sys/env.ts Normal file
View File

@@ -0,0 +1,22 @@
import { kmod_filesystem_readFile } from "../../kernel/modules/filesystem/filesystem.kmod";
import { uiarrtostr } from "../libts/uint_arr";
export function getEnv(key: string): string | null {
const data = uiarrtostr(kmod_filesystem_readFile("/disk/uenv")!);
const lines = data.split("\n");
for (let i = 0; i < lines.length; i++) {
const kv = lines[i]!.split("=");
if (kv[0] == key) return kv[1]!;
}
return null;
}
export function getPathEnv(): string[] | null {
const path = getEnv("PATH");
if (!path) return null;
return path.split(":");
}

View File

@@ -4,7 +4,7 @@ import { oskrnl_input_onKeyPress } from "./input/input";
export function oskrnl_register() {
(globalThis as any).__oskrnl = {
app_args: "why",
app_args: null,
console_log: oskrnl_console_log,
console_update: oskrnl_console_update,
input_onKeyPress: oskrnl_input_onKeyPress,

1
test-hdd/uenv Normal file
View File

@@ -0,0 +1 @@
PATH=/disk/apps