diff --git a/package-lock.json b/package-lock.json index bc0826c..fe2ce01 100755 --- a/package-lock.json +++ b/package-lock.json @@ -1723,9 +1723,9 @@ } }, "node_modules/@sveltejs/kit": { - "version": "2.53.4", - "resolved": "https://registry.npmjs.org/@sveltejs/kit/-/kit-2.53.4.tgz", - "integrity": "sha512-iAIPEahFgDJJyvz8g0jP08KvqnM6JvdW8YfsygZ+pMeMvyM2zssWMltcsotETvjSZ82G3VlitgDtBIvpQSZrTA==", + "version": "2.55.0", + "resolved": "https://registry.npmjs.org/@sveltejs/kit/-/kit-2.55.0.tgz", + "integrity": "sha512-MdFRjevVxmAknf2NbaUkDF16jSIzXMWd4Nfah0Qp8TtQVoSp3bV4jKt8mX7z7qTUTWvgSaxtR0EG5WJf53gcuA==", "dev": true, "license": "MIT", "dependencies": { @@ -1734,7 +1734,7 @@ "@types/cookie": "^0.6.0", "acorn": "^8.14.1", "cookie": "^0.6.0", - "devalue": "^5.6.3", + "devalue": "^5.6.4", "esm-env": "^1.2.2", "kleur": "^4.1.5", "magic-string": "^0.30.5", @@ -2971,9 +2971,9 @@ } }, "node_modules/devalue": { - "version": "5.6.3", - "resolved": "https://registry.npmjs.org/devalue/-/devalue-5.6.3.tgz", - "integrity": "sha512-nc7XjUU/2Lb+SvEFVGcWLiKkzfw8+qHI7zn8WYXKkLMgfGSHbgCEaR6bJpev8Cm6Rmrb19Gfd/tZvGqx9is3wg==", + "version": "5.6.4", + "resolved": "https://registry.npmjs.org/devalue/-/devalue-5.6.4.tgz", + "integrity": "sha512-Gp6rDldRsFh/7XuouDbxMH3Mx8GMCcgzIb1pDTvNyn8pZGQ22u+Wa+lGV9dQCltFQ7uVw0MhRyb8XDskNFOReA==", "license": "MIT" }, "node_modules/dom-accessibility-api": { @@ -3590,9 +3590,9 @@ } }, "node_modules/flatted": { - "version": "3.3.3", - "resolved": "https://registry.npmjs.org/flatted/-/flatted-3.3.3.tgz", - "integrity": "sha512-GX+ysw4PBCz0PzosHDepZGANEuFCMLrnRTiEy9McGjmkCQYwRq4A/X786G/fjM/+OjsWSU1ZrY5qyARZmO/uwg==", + "version": "3.4.1", + "resolved": "https://registry.npmjs.org/flatted/-/flatted-3.4.1.tgz", + "integrity": "sha512-IxfVbRFVlV8V/yRaGzk0UVIcsKKHMSfYw66T/u4nTwlWteQePsxe//LjudR1AMX4tZW3WFCh3Zqa/sjlqpbURQ==", "dev": true, "license": "ISC" }, diff --git a/src/lib/stores/bash/bash.ts b/src/lib/stores/bash/bash.ts index f2147ff..2c6743c 100755 --- a/src/lib/stores/bash/bash.ts +++ b/src/lib/stores/bash/bash.ts @@ -5,6 +5,13 @@ import { PASSWD, type User } from './etc/userData'; import { ExitCode } from './metadata'; import { COMMANDS, type CommandArgs, type CommandResultData, type ICommand } from './commandRegistry'; import { GROUP, type Group } from './etc/groupData'; +import { Process } from './process'; +import type { KeybindInhibitor } from './keybindInhibitor'; + +export enum Signal { + SIGINT, + SIGKILL +} export type BashInitArgs = { io?: Terminal; @@ -19,14 +26,18 @@ export type Result = { }; export class Bash { - private vfs: VirtualFS; - private _passwd: User[]; - private _userInstances: Stack; - private _group: Group[]; - private _terminal!: Terminal; - private user: User; + private readonly vfs: VirtualFS; + private readonly _passwd: User[]; + private readonly _group: Group[]; + private readonly _terminal!: Terminal; private readonly _commands: Record; - + + private _userInstances: Stack; + + private user: User; + private _process?: Process; + + constructor(args: BashInitArgs) { this._commands = COMMANDS; this._passwd = PASSWD; @@ -54,6 +65,16 @@ export class Bash { return user } + private async _initNewProcess(command: ICommand, args: CommandArgs) { + this._process = new Process( + this, + this.user.uid, + 'TEMP PROCESS NAME', + command, + args + ); + } + private _appendNewResult(inode: number, output: any, cmd: string) { const data: PrintData = { path: this.vfs.formatPath(this.vfs.getPathByInode(inode)), @@ -94,6 +115,20 @@ export class Bash { return this.vfs; } + getProcess(): Process | ExitCode.ERROR { + if(this._process) { + return this._process; + } + return ExitCode.ERROR; + } + + getProcessKeybindInhibitor(): KeybindInhibitor | null { + if(this._process) { + return this._process.getKeybindInhibitor(); + } + else return null; + } + getTerminalWidth(): number { return this._terminal.getTerminalWidth(); } @@ -106,22 +141,40 @@ export class Bash { return this._group[1].members.includes(uid); } - async executeCommand(commandName: string, args: CommandArgs) { + async executeCommand(commandName: string, args: CommandArgs): Promise { let result: Result = { exitCode: ExitCode.ERROR, path: this.getCwd() }; - const command = this._commands[commandName]; + const command: ICommand = this._commands[commandName]; + if (!command) this.throwError(result); if (command.root) { if (this.hasSudoPerms(this.user.uid)) { - let out: Result = command.method.call(this, args); + await this._initNewProcess(command, args); + if(!this._process) throw new Error('failed to init new process'); + + const out: Result = await this._process.getResult(); this._appendNewResult(this.getCwd(), out, this.user.history[0]); } this.throwError(result); } - let out: Result = command.method.call(this, args); + await this._initNewProcess(command, args); + if(!this._process) throw new Error('failed to init new process'); + + const out: Result = await this._process.getResult(); console.log(out); this._appendNewResult(out.path, out.resultData?.data, this.user.history[0]); + + return out; + } + + processKill(): ExitCode { + if(this._process) { + return this._process.sigKill(() => {console.log("process Kill successfull")}); + } + else { + return ExitCode.SUCCESS; //Temp need to make custom enum value + } } throwError(result: Result): void { diff --git a/src/lib/stores/bash/commandRegistry.ts b/src/lib/stores/bash/commandRegistry.ts index d2fc4cc..8097c27 100644 --- a/src/lib/stores/bash/commandRegistry.ts +++ b/src/lib/stores/bash/commandRegistry.ts @@ -1,10 +1,13 @@ +import type { KeyBind } from "../terminal/keybindRegistry"; import type { Bash, Result } from "./bash"; import { cd } from "./commands/cd"; import { clear } from "./commands/clear"; import { ls } from "./commands/ls"; +import { aSync } from "./commands/testAsync"; export type ICommand = { - method: (this: Bash, args: CommandArgs) => Result; + method: (this: Bash, args: CommandArgs) => Promise; + keybinds?: Map any>; flags: string[]; help: string; root: boolean; @@ -24,7 +27,8 @@ export type CommandResultData = { export const COMMANDS = { cd, ls, - clear + clear, + aSync } as const satisfies Record; /* //export const commands { diff --git a/src/lib/stores/bash/commands/ls.ts b/src/lib/stores/bash/commands/ls.ts index 4e967d8..bef13ea 100755 --- a/src/lib/stores/bash/commands/ls.ts +++ b/src/lib/stores/bash/commands/ls.ts @@ -38,7 +38,7 @@ const months: readonly string[] = [ 'Dec' ]; -export const cmd_ls = function (this: Bash, args: CommandArgs): Result { +export const cmd_ls = async function (this: Bash, args: CommandArgs): Promise { const Fs = this.getFs(); const resultData: CommandResultData = { cmd: 'ls', data: null, args: args }; const result: Result = { exitCode: ExitCode.ERROR, path: this.getCwd(), resultData: resultData }; @@ -72,9 +72,7 @@ export const cmd_ls = function (this: Bash, args: CommandArgs): Result { }; function result_ls(this: Bash, data: any, args: CommandArgs): HTMLElement { - const dummysonoerror: HTMLElement = document.createElement('div'); const Fs: VirtualFS = this.getFs(); - const flagInfo = checkFlags(args.flags); const nodes: TreeNode[] = data; diff --git a/src/lib/stores/bash/commands/testAsync.ts b/src/lib/stores/bash/commands/testAsync.ts new file mode 100644 index 0000000..ea2d06a --- /dev/null +++ b/src/lib/stores/bash/commands/testAsync.ts @@ -0,0 +1,43 @@ +import type { KeyBind } from "$lib/stores/terminal/keybindRegistry"; +import { type Bash, type Result } from "../bash"; +import type { CommandArgs, ICommand } from "../commandRegistry"; +import { ExitCode } from "../metadata"; + +export const cmd_async = async function(this: Bash, args: CommandArgs): Promise { + let result: Result = { exitCode: ExitCode.ERROR, path: this.getCwd() }; + + + result.exitCode = ExitCode.SUCCESS; + return result; +} + +const test_keybind: KeyBind = [ + { + key: 'a', + modifiers: [ + 'Shift' + ] + } +]; + +function test(): void { + console.log("Keybind works"); +} + +const keybinds: Map any> = new Map([ + [test_keybind,test] +]); + +export const aSync: ICommand = { + method: cmd_async, + keybinds: keybinds, + flags: [] as string[], + help: 'PATH TO HELP.md', + root: false +}; + + + + + + diff --git a/src/lib/stores/bash/keybindInhibitor.ts b/src/lib/stores/bash/keybindInhibitor.ts new file mode 100644 index 0000000..303705a --- /dev/null +++ b/src/lib/stores/bash/keybindInhibitor.ts @@ -0,0 +1,34 @@ +import type { KeyBind, Modifier } from "../terminal/keybindRegistry"; + + +export class KeybindInhibitor{ + private _keybinds: Map any>; + + constructor(keybinds: Map any>) { + this._keybinds = keybinds; + } + + private parseModifierStates(event: KeyboardEvent): Modifier[] | undefined { + const mods: Modifier[] = []; + + if(event.altKey) mods.push('Alt'); + if(event.ctrlKey) mods.push('Ctrl'); + if(event.shiftKey) mods.push('Shift'); + + if(mods.length == 0) return undefined; + + return mods; + } + + processKeystroke(event: KeyboardEvent) { + const keybind: KeyBind = [ + { + key: event.key, + modifiers: this.parseModifierStates(event) + } + ]; + + const out = this._keybinds.get(keybind); + if(out) out(); + } +} \ No newline at end of file diff --git a/src/lib/stores/bash/process.ts b/src/lib/stores/bash/process.ts new file mode 100644 index 0000000..7d17baa --- /dev/null +++ b/src/lib/stores/bash/process.ts @@ -0,0 +1,74 @@ +import type { Bash, Result } from "./bash"; +import type { CommandArgs, ICommand } from "./commandRegistry"; +import { KeybindInhibitor } from "./keybindInhibitor"; +import { ExitCode } from "./metadata"; + +export class Process { + + private _bash: Bash; + private _result: Result; + private _uid: number; + private _name: string; + private _initTimestamp: Date; + private _keybindInhibitor?: KeybindInhibitor; + private _abortController: AbortController; + + constructor( + _bashInstance: Bash, + uid: number, + name: string, + command: ICommand, + args: CommandArgs + ) { + this._bash = _bashInstance; + this._result = { exitCode: ExitCode.ERROR, path: this._bash.getCwd() } + this._uid = uid; + this._name = name; + this._initTimestamp = new Date(); + this._abortController = new AbortController(); + + if(command.keybinds) { + this._keybindInhibitor = new KeybindInhibitor(command.keybinds); + } + + this._Run(command, _bashInstance, args); + } + + public getKeybindInhibitor(): KeybindInhibitor | null { + if(this._keybindInhibitor) { + return this._keybindInhibitor; + } + else { + return null; + } + } + + public sigKill(callback: () => void): ExitCode { + this._abortController.abort(); + callback(); + + return ExitCode.SUCCESS //Only temp, need to add ExitCode.INT + } + + public getUid(): number { + return this._uid; + } + + public getInitTime(): Date { + return this._initTimestamp; + } + + public getName(): string { + return this._name; + } + + public async getResult(): Promise { + return this._result; + } + + private async _Run(command: ICommand, bashInstance: Bash, args: CommandArgs) { + this._result = await command.method.call( + bashInstance, args); + } + +} \ No newline at end of file diff --git a/src/lib/stores/terminal/init.svelte.ts b/src/lib/stores/terminal/init.svelte.ts index 1ab61e5..cfb4437 100755 --- a/src/lib/stores/terminal/init.svelte.ts +++ b/src/lib/stores/terminal/init.svelte.ts @@ -123,7 +123,7 @@ export async function initTerminal( }; */ const args: TermInitArgs = { - //locale, + //locale, bash: { fs, user diff --git a/src/lib/stores/terminal/keybindRegistry.ts b/src/lib/stores/terminal/keybindRegistry.ts new file mode 100644 index 0000000..fdea218 --- /dev/null +++ b/src/lib/stores/terminal/keybindRegistry.ts @@ -0,0 +1,17 @@ +export type Modifier = 'Ctrl' | 'Alt' | 'Shift'; + +type KeyStroke = { + key: string; + modifiers?: Modifier[]; +} + +export type KeyBind = [KeyStroke] | [KeyStroke, KeyStroke]; + +const sigint: KeyBind = [{ + key: 'c', + modifiers: ['Ctrl'] +}]; + +export const TERMINAL_KEYBINDS = { + sigint +} as const satisfies Record; diff --git a/src/lib/stores/terminal/stdio/keystrokeRegistry.ts b/src/lib/stores/terminal/stdio/keystrokeRegistry.ts deleted file mode 100644 index a225bf9..0000000 --- a/src/lib/stores/terminal/stdio/keystrokeRegistry.ts +++ /dev/null @@ -1,4 +0,0 @@ -export interface KeyStroke { - keys: KeyboardEvent[]; -} - diff --git a/src/lib/stores/terminal/terminal.ts b/src/lib/stores/terminal/terminal.ts index ab6ef3b..a702cb3 100755 --- a/src/lib/stores/terminal/terminal.ts +++ b/src/lib/stores/terminal/terminal.ts @@ -1,8 +1,11 @@ -import { Bash, type BashInitArgs } from '../bash/bash'; +import { Bash, Signal, type BashInitArgs, type Result } from '../bash/bash'; import type { CommandArgs } from '../bash/commandRegistry'; import type { User } from '../bash/etc/userData'; import { VirtualFS, type TreeNode } from '../bash/fs'; -import type { ExitCode } from '../bash/metadata'; +import { KeybindInhibitor } from '../bash/keybindInhibitor'; +import { ExitCode } from '../bash/metadata'; +import type { Process } from '../bash/process'; +import { TERMINAL_KEYBINDS, type KeyStroke } from './keybindRegistry'; //import type { ILocale } from './localeRegistry'; export type TerminalMode = {}; @@ -33,16 +36,35 @@ export type PageCallbacks = { export class Terminal { private _bash: Bash; private _callbacks: Partial = {}; + private _keybindInhibitor: KeybindInhibitor; //public locale: ILocale; constructor(args: TermInitArgs) { args.bash.io = this; this._bash = new Bash(args.bash); + + const keybinds: Map any> = new Map([ + [TERMINAL_KEYBINDS.sigint,this._processKill] + ]); + + this._keybindInhibitor = new KeybindInhibitor(keybinds); //this.locale = args.locale; } + private _processKill(): ExitCode { + const get: Process | ExitCode = this._bash.getProcess(); + if(get == ExitCode.ERROR) return ExitCode.ERROR; + + const process: Process = get; + const result = process.sigKill(() => { + console.log("process kill successful"); + }); + + return result; + } + private _parseInput(input: string): ParsedInput { let args: string[] = []; const result: ParsedInput = { command: '', args: { flags: [], args: [] } }; @@ -100,11 +122,12 @@ export class Terminal { console.error(message, exitCode); } - executeCommand(input: string): void { + executeCommand(input: string): Promise { this._bash.updateHistory(input); const parsed: ParsedInput = this._parseInput(input); - console.log(parsed, 'executeCommand output'); - this._bash.executeCommand(parsed.command, parsed.args); + //console.log(parsed, 'executeCommand output'); + + return this._bash.executeCommand(parsed.command, parsed.args); } registerCallbacks(callbacks: PageCallbacks): void { @@ -115,6 +138,27 @@ export class Terminal { this._callbacks.clear?.(); } + getPrimaryKeybindInhibitor(): KeybindInhibitor { + const processKeybinds: KeybindInhibitor | null = + this._bash.getProcessKeybindInhibitor(); + if(processKeybinds) { + return processKeybinds; + } + else { + return this._keybindInhibitor; + } + } + + getProcessKeyInhibitor(): KeybindInhibitor | ExitCode.ERROR { + const out = this._bash.getProcessKeybindInhibitor(); + if(out) return out; + else return ExitCode.ERROR; + } + + getKeybindInhibitor(): KeybindInhibitor { + return this._keybindInhibitor; + } + getUser(): User { return this._bash.getUser(); } diff --git a/src/modules/Login.svelte b/src/modules/Login.svelte index 1f5aad2..a281777 100644 --- a/src/modules/Login.svelte +++ b/src/modules/Login.svelte @@ -6,7 +6,7 @@ let {init, form}: { init: (formData: FormData) => void, form: any} = $props(); -
+
gornodox @@ -35,10 +35,6 @@ animation: fadeIn 0.2s forwards cubic-bezier(.4,.7,0,.99); } -#wrapper { - animation: fadeIn 0.5s forwards cubic-bezier(.4,.7,0,.99) 1s; -} - @keyframes fadeIn { to { opacity: 1; diff --git a/src/modules/Terminal.svelte b/src/modules/Terminal.svelte index 7d12858..c909808 100755 --- a/src/modules/Terminal.svelte +++ b/src/modules/Terminal.svelte @@ -5,6 +5,7 @@ import { initTerminal, isInitializing } from '$lib/stores/terminal/init.svelte'; import { clear, print } from '$lib/stores/terminal/stdio/io'; import type { User } from '$lib/stores/bash/etc/userData'; + import { ExitCode } from '$lib/stores/bash/metadata'; const clearTerminal = (): void => clear(); @@ -49,6 +50,15 @@ } function inputHandler(event: KeyboardEvent) { + const terminalKeyInhibitor = terminal.getKeybindInhibitor(); + terminalKeyInhibitor.processKeystroke(event); + + const processKeyInhibitor = terminal.getProcessKeyInhibitor(); + if(processKeyInhibitor != ExitCode.ERROR) { + processKeyInhibitor.processKeystroke(event); + } + + console.log(processKeyInhibitor, terminalKeyInhibitor); switch (event.key) { case 'Enter': { terminal.executeCommand(inputValue); diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index d6fcd8d..0979627 100755 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -39,17 +39,28 @@
-
+
{/if}