a lot of "drunk code", processes need rethinking, terminal initializes but commands dont go through to IO
This commit is contained in:
+64
-11
@@ -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<User>;
|
||||
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<string, ICommand>;
|
||||
|
||||
|
||||
private _userInstances: Stack<User>;
|
||||
|
||||
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<Result> {
|
||||
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 {
|
||||
|
||||
@@ -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<Result>;
|
||||
keybinds?: Map<KeyBind, () => 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<string, ICommand>;
|
||||
|
||||
/* //export const commands {
|
||||
|
||||
@@ -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<Result> {
|
||||
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;
|
||||
|
||||
@@ -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<Result> {
|
||||
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<KeyBind, () => 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
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
import type { KeyBind, Modifier } from "../terminal/keybindRegistry";
|
||||
|
||||
|
||||
export class KeybindInhibitor{
|
||||
private _keybinds: Map<KeyBind, () => any>;
|
||||
|
||||
constructor(keybinds: Map<KeyBind, () => 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();
|
||||
}
|
||||
}
|
||||
@@ -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<Result> {
|
||||
return this._result;
|
||||
}
|
||||
|
||||
private async _Run(command: ICommand, bashInstance: Bash, args: CommandArgs) {
|
||||
this._result = await command.method.call(
|
||||
bashInstance, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -123,7 +123,7 @@ export async function initTerminal(
|
||||
}; */
|
||||
|
||||
const args: TermInitArgs = {
|
||||
//locale,
|
||||
//locale,
|
||||
bash: {
|
||||
fs,
|
||||
user
|
||||
|
||||
@@ -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<string, KeyBind>;
|
||||
@@ -1,4 +0,0 @@
|
||||
export interface KeyStroke {
|
||||
keys: KeyboardEvent[];
|
||||
}
|
||||
|
||||
@@ -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<PageCallbacks> = {};
|
||||
private _keybindInhibitor: KeybindInhibitor;
|
||||
//public locale: ILocale;
|
||||
|
||||
constructor(args: TermInitArgs) {
|
||||
args.bash.io = this;
|
||||
this._bash = new Bash(args.bash);
|
||||
|
||||
const keybinds: Map<KeyStroke, () => 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<Result> {
|
||||
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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user