Implement system env and app resolver
This commit is contained in:
@@ -5,22 +5,41 @@ import { uiarrtostr } from "../../../lib/libts/uint_arr";
|
|||||||
import {
|
import {
|
||||||
kmod_filesystem_listDir,
|
kmod_filesystem_listDir,
|
||||||
kmod_filesystem_readFile,
|
kmod_filesystem_readFile,
|
||||||
|
kmod_filesystem_stat,
|
||||||
} from "../filesystem/filesystem.kmod";
|
} from "../filesystem/filesystem.kmod";
|
||||||
import { oskrnl_register } from "../../../oskrnl";
|
import { oskrnl_register } from "../../../oskrnl";
|
||||||
|
import { getPathEnv } from "../../../lib/sys/env";
|
||||||
|
|
||||||
export function kmod_app_init() {
|
export function kmod_app_init() {
|
||||||
oskrnl_register();
|
oskrnl_register();
|
||||||
}
|
}
|
||||||
|
|
||||||
export function kmod_app_run(path: string) {
|
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(
|
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");
|
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)!);
|
const code = uiarrtostr(kmod_filesystem_readFile(entrypoint)!);
|
||||||
|
|
||||||
iexec(code);
|
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
22
src/os/src/lib/sys/env.ts
Normal 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(":");
|
||||||
|
}
|
||||||
@@ -4,7 +4,7 @@ import { oskrnl_input_onKeyPress } from "./input/input";
|
|||||||
|
|
||||||
export function oskrnl_register() {
|
export function oskrnl_register() {
|
||||||
(globalThis as any).__oskrnl = {
|
(globalThis as any).__oskrnl = {
|
||||||
app_args: "why",
|
app_args: null,
|
||||||
console_log: oskrnl_console_log,
|
console_log: oskrnl_console_log,
|
||||||
console_update: oskrnl_console_update,
|
console_update: oskrnl_console_update,
|
||||||
input_onKeyPress: oskrnl_input_onKeyPress,
|
input_onKeyPress: oskrnl_input_onKeyPress,
|
||||||
|
|||||||
1
test-hdd/uenv
Normal file
1
test-hdd/uenv
Normal file
@@ -0,0 +1 @@
|
|||||||
|
PATH=/disk/apps
|
||||||
Reference in New Issue
Block a user