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 {
|
||||
|
||||
Reference in New Issue
Block a user