diff --git a/src/os/src/kernel/modules/app/app.kmod.ts b/src/os/src/kernel/modules/app/app.kmod.ts index 1c36fd4..1e1a61a 100644 --- a/src/os/src/kernel/modules/app/app.kmod.ts +++ b/src/os/src/kernel/modules/app/app.kmod.ts @@ -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; +} diff --git a/src/os/src/lib/sys/env.ts b/src/os/src/lib/sys/env.ts new file mode 100644 index 0000000..686948c --- /dev/null +++ b/src/os/src/lib/sys/env.ts @@ -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(":"); +} diff --git a/src/os/src/oskrnl/index.ts b/src/os/src/oskrnl/index.ts index 3cd74bd..9280abe 100644 --- a/src/os/src/oskrnl/index.ts +++ b/src/os/src/oskrnl/index.ts @@ -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, diff --git a/test-hdd/uenv b/test-hdd/uenv new file mode 100644 index 0000000..e006bf2 --- /dev/null +++ b/test-hdd/uenv @@ -0,0 +1 @@ +PATH=/disk/apps \ No newline at end of file