Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 97296843df |
@@ -1,60 +1,68 @@
|
|||||||
import { VirtualFS, type TreeNode } from './fs';
|
import { COMMANDS, GROUP, PASSWD, type CommandArgs, type ICommand, type Result } from './static';
|
||||||
|
import { VirtualFS } from './fs';
|
||||||
import { Terminal, type PrintData } from '../terminal/terminal';
|
import { Terminal, type PrintData } from '../terminal/terminal';
|
||||||
import { Stack } from '../stack';
|
import { Stack } from '../stack';
|
||||||
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';
|
|
||||||
|
|
||||||
export type BashInitArgs = {
|
export type Permission = {
|
||||||
io?: Terminal;
|
r: boolean;
|
||||||
instanceId: number;
|
w: boolean;
|
||||||
user: {username: string, password: string};
|
x: boolean;
|
||||||
fs?: VirtualFS;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export type Result = {
|
export type BashInitArgs = {
|
||||||
exitCode: ExitCode;
|
stdio?: Terminal;
|
||||||
path: number; //the inode of the place that the command was executed in
|
user: User;
|
||||||
resultData?: CommandResultData;
|
fs: any;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type TimeStamps = {
|
||||||
|
modified: Date;
|
||||||
|
changed: Date;
|
||||||
|
accessed: Date;
|
||||||
|
};
|
||||||
|
|
||||||
|
// TODO: Finish this
|
||||||
|
// TODO: Change into a type instead of an enum for performance (low priority)
|
||||||
|
export enum ExitCode {
|
||||||
|
SUCCESS = 0,
|
||||||
|
ERROR = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
export type User = {
|
||||||
|
username: string;
|
||||||
|
passwd: string; //HASHED PASSWORD //TODO: Make a formated type
|
||||||
|
readonly uid: number; // Normal user 1000+ System user 1-999 root - 0 //TODO: Make a formated type
|
||||||
|
readonly gid: number; // Primary group | 'Users' 1000 - Others - 1000+ root - 0 //TODO: Make a formated type
|
||||||
|
home: string; //TODO: Make a formated type
|
||||||
|
history: string[];
|
||||||
|
cwd?: number; //TODO: Make a formated type
|
||||||
|
pwd?: number; //TODO: Make a formated type
|
||||||
|
};
|
||||||
|
|
||||||
|
export type Group = {
|
||||||
|
groupname: string;
|
||||||
|
gid: number; // Primary group 'Users' 1000 - Others - 1000+ root - 0
|
||||||
|
members: number[]; //TODO: Make a formated type UID
|
||||||
};
|
};
|
||||||
|
|
||||||
export class Bash {
|
export class Bash {
|
||||||
private readonly _instanceId: number;
|
|
||||||
private vfs: VirtualFS;
|
private vfs: VirtualFS;
|
||||||
private _passwd: User[];
|
private _passwd: User[];
|
||||||
private _userInstances: Stack<User>;
|
private _instances: Stack<User>;
|
||||||
private _group: Group[];
|
private _group: Group[];
|
||||||
private _terminal!: Terminal;
|
private _terminal!: Terminal;
|
||||||
private user: User;
|
private user: User;
|
||||||
private readonly _commands: Record<string, ICommand>;
|
private readonly _commands: Record<string, ICommand>;
|
||||||
|
|
||||||
constructor(args: BashInitArgs) {
|
constructor(args: BashInitArgs) {
|
||||||
this._instanceId = args.instanceId
|
this.user = args.user;
|
||||||
this._commands = COMMANDS;
|
this._commands = COMMANDS;
|
||||||
this._passwd = PASSWD;
|
this._passwd = PASSWD;
|
||||||
this._group = GROUP;
|
this._group = GROUP;
|
||||||
this._userInstances = new Stack<User>();
|
this._terminal = args.stdio!;
|
||||||
this._terminal = args.io!;
|
this._instances = new Stack<User>();
|
||||||
|
|
||||||
const loginResult = this.userLogin(args.user.username, args.user.password);
|
this.vfs = new VirtualFS({ fs: args.fs, user: args.user });
|
||||||
if(loginResult == ExitCode.ERROR)
|
|
||||||
this._terminal.throwExeption(
|
|
||||||
`Failed to initialize bash instance - access denied for user ${args.user.username}`,
|
|
||||||
ExitCode.ERROR
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
this.user = this._userInstances.peek()!
|
|
||||||
this.vfs = this._terminal.fileSystem;
|
|
||||||
}
|
|
||||||
|
|
||||||
private _initNewUserSession(user: User): User {
|
|
||||||
if(!this._passwd.includes(user))
|
|
||||||
this._terminal.throwExeption(`user not found under the name ${user.username}`, ExitCode.ERROR)
|
|
||||||
|
|
||||||
this._userInstances.push(user);
|
|
||||||
return user
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private _appendNewResult(inode: number, output: any, cmd: string) {
|
private _appendNewResult(inode: number, output: any, cmd: string) {
|
||||||
@@ -77,10 +85,6 @@ export class Bash {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
clearTerminal(): void {
|
|
||||||
this._terminal.clearTerminal();
|
|
||||||
}
|
|
||||||
|
|
||||||
getCwd(): number {
|
getCwd(): number {
|
||||||
return this.vfs.cwd;
|
return this.vfs.cwd;
|
||||||
}
|
}
|
||||||
@@ -109,7 +113,7 @@ export class Bash {
|
|||||||
return this._group[1].members.includes(uid);
|
return this._group[1].members.includes(uid);
|
||||||
}
|
}
|
||||||
|
|
||||||
async executeCommand(commandName: string, args: CommandArgs) {
|
executeCommand(commandName: string, args: CommandArgs): void {
|
||||||
let result: Result = { exitCode: ExitCode.ERROR, path: this.getCwd() };
|
let result: Result = { exitCode: ExitCode.ERROR, path: this.getCwd() };
|
||||||
const command = this._commands[commandName];
|
const command = this._commands[commandName];
|
||||||
if (!command) this.throwError(result);
|
if (!command) this.throwError(result);
|
||||||
@@ -124,7 +128,7 @@ export class Bash {
|
|||||||
|
|
||||||
let out: Result = command.method.call(this, args);
|
let out: Result = command.method.call(this, args);
|
||||||
console.log(out);
|
console.log(out);
|
||||||
this._appendNewResult(out.path, out.resultData?.data, this.user.history[0]);
|
this._appendNewResult(out.path, out.data?.data, this.user.history[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
throwError(result: Result): void {
|
throwError(result: Result): void {
|
||||||
@@ -136,25 +140,14 @@ export class Bash {
|
|||||||
}
|
}
|
||||||
|
|
||||||
userLogout() {
|
userLogout() {
|
||||||
this._userInstances.pop();
|
this._instances.pop();
|
||||||
if (this._userInstances.size() === 0) {
|
if (this._instances.size() === 0) {
|
||||||
//TODO: Implement system logout
|
//TODO: Implement system logout
|
||||||
} else {
|
} else {
|
||||||
//this.changeUser(this._instances.peek()!);
|
//this.changeUser(this._instances.peek()!);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
userLogin(username: string, passwd: string): ExitCode {
|
|
||||||
const user: User | undefined =
|
|
||||||
this._passwd.find((user) => user.username === username);
|
|
||||||
|
|
||||||
if(user && user.passwd === passwd) {
|
|
||||||
this._initNewUserSession(user);
|
|
||||||
return ExitCode.SUCCESS;
|
|
||||||
}
|
|
||||||
return ExitCode.ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
formatBytes(bytes: number, dPoint?: number, pow: 1024 | 1000 = 1024): string {
|
formatBytes(bytes: number, dPoint?: number, pow: 1024 | 1000 = 1024): string {
|
||||||
if (!+bytes) return '0';
|
if (!+bytes) return '0';
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import type { Bash, Result } from '../bash';
|
import { ExitCode, type Bash } from '../bash';
|
||||||
import type { CommandArgs, ICommand } from '../commandRegistry';
|
|
||||||
import { Type, type TreeNode } from '../fs';
|
import { Type, type TreeNode } from '../fs';
|
||||||
import { ExitCode } from '../metadata';
|
import type { CommandArgs, ICommand, Result } from '../static';
|
||||||
|
|
||||||
export const cmd_cd = function (this: Bash, args: CommandArgs): Result {
|
export const cmd_cd = function (this: Bash, args: CommandArgs): Result {
|
||||||
let result: Result = { exitCode: ExitCode.ERROR, path: this.getCwd() };
|
let result: Result = { exitCode: ExitCode.ERROR, path: this.getCwd() };
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import type { CommandArgs, ICommand, Result } from "../static";
|
|||||||
|
|
||||||
export const cmd_clear = function(this: Bash, args: CommandArgs): Result {
|
export const cmd_clear = function(this: Bash, args: CommandArgs): Result {
|
||||||
let result: Result = { exitCode: ExitCode.ERROR, path: this.getCwd() };
|
let result: Result = { exitCode: ExitCode.ERROR, path: this.getCwd() };
|
||||||
this.clearTerminal();
|
|
||||||
result.exitCode = ExitCode.SUCCESS;
|
result.exitCode = ExitCode.SUCCESS;
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
import type { Bash, Result } from '../bash';
|
import { Bash, ExitCode, type Permission, type TimeStamps } from '../bash';
|
||||||
import type { CommandArgs, CommandResultData, ICommand } from '../commandRegistry';
|
|
||||||
import { Type, VirtualFS, type NodePerms, type TreeNode } from '../fs';
|
import { Type, VirtualFS, type NodePerms, type TreeNode } from '../fs';
|
||||||
import { ExitCode, type Permission } from '../metadata';
|
|
||||||
import { Sort, SortNodeBy } from '../sort';
|
import { Sort, SortNodeBy } from '../sort';
|
||||||
|
import type { CommandArgs, ICommand, Result, resultData } from '../static';
|
||||||
|
|
||||||
type LsEntry = {
|
type LsEntry = {
|
||||||
inode: number | null;
|
inode: number | null;
|
||||||
@@ -40,8 +39,8 @@ const months: readonly string[] = [
|
|||||||
|
|
||||||
export const cmd_ls = function (this: Bash, args: CommandArgs): Result {
|
export const cmd_ls = function (this: Bash, args: CommandArgs): Result {
|
||||||
const Fs = this.getFs();
|
const Fs = this.getFs();
|
||||||
const resultData: CommandResultData = { cmd: 'ls', data: null, args: args };
|
const resultData: resultData = { cmd: 'ls', data: null, args: args };
|
||||||
const result: Result = { exitCode: ExitCode.ERROR, path: this.getCwd(), resultData: resultData };
|
const result: Result = { exitCode: ExitCode.ERROR, path: this.getCwd(), data: resultData };
|
||||||
const nodes: TreeNode[] = [];
|
const nodes: TreeNode[] = [];
|
||||||
|
|
||||||
//Check if any args contain the long flags with value and are valid flags inside the ls const
|
//Check if any args contain the long flags with value and are valid flags inside the ls const
|
||||||
@@ -226,7 +225,7 @@ function result_ls(this: Bash, data: any, args: CommandArgs): HTMLElement {
|
|||||||
colWidths.push(Math.max(...columns[i].map((name) => name.length)));
|
colWidths.push(Math.max(...columns[i].map((name) => name.length)));
|
||||||
}
|
}
|
||||||
|
|
||||||
const calcWidth: number = colWidths.reduce((prev, curr) => prev + curr) + ((c-1) * 2);
|
const calcWidth: number = colWidths.reduce((prev, curr) => prev + curr) + ((c-1) * 3);
|
||||||
|
|
||||||
if(calcWidth < maxWidth) lowBound = c + 1;
|
if(calcWidth < maxWidth) lowBound = c + 1;
|
||||||
else if(calcWidth > maxWidth) highBound = c -1
|
else if(calcWidth > maxWidth) highBound = c -1
|
||||||
@@ -235,7 +234,6 @@ function result_ls(this: Bash, data: any, args: CommandArgs): HTMLElement {
|
|||||||
const wrapper: HTMLElement = document.createElement('div');
|
const wrapper: HTMLElement = document.createElement('div');
|
||||||
|
|
||||||
wrapper.style.display = 'flex';
|
wrapper.style.display = 'flex';
|
||||||
wrapper.style.columnGap = `${this.getTerminalFontSize() * 2}px`;
|
|
||||||
wrapper.style.marginBottom = `${this.getTerminalFontSize() * 2}px`;
|
wrapper.style.marginBottom = `${this.getTerminalFontSize() * 2}px`;
|
||||||
|
|
||||||
let fileIndex = 0;
|
let fileIndex = 0;
|
||||||
@@ -324,6 +322,7 @@ function formatChildren(node: TreeNode): string {
|
|||||||
if (!node.children) throw new Error('children array is null on this node');
|
if (!node.children) throw new Error('children array is null on this node');
|
||||||
|
|
||||||
const c = node.children.length.toString();
|
const c = node.children.length.toString();
|
||||||
|
console.log(c, "TEST TEST");
|
||||||
return c.length > 1 ? c : ` ${c}`;
|
return c.length > 1 ? c : ` ${c}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -363,35 +362,28 @@ function formatName(node: TreeNode, flag: any, shouldShift: boolean) {
|
|||||||
let name: string = node.name;
|
let name: string = node.name;
|
||||||
const char: string = flag.has('Q') ? '"' : "'";
|
const char: string = flag.has('Q') ? '"' : "'";
|
||||||
|
|
||||||
if (/\s/.test(node.name)) {
|
if (/\s/.test(node.name)) name = `${char}${name}${char}`;
|
||||||
name = `${char}${name}${char}`
|
|
||||||
} else {
|
if(flag.has('p') && node.type === Type.Directory) name = `${name}/`
|
||||||
//Shift non quoted names 1 char right to align if any names in group have a quote
|
|
||||||
name = `${shouldShift ? ' ' : ''}${node.name}`;
|
if((flag.has('l') || flag.has('g') || flag.has('o'))) {
|
||||||
|
// Is the ls in long format
|
||||||
|
if (!(/\s/.test(node.name))) {
|
||||||
|
//Shift non quoted names 1 char right to align if any names in group have a quote
|
||||||
|
name = `${shouldShift ? ' ' : ''}${name}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (!(/\s/.test(node.name))) {
|
||||||
|
name = ` ${name}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
name = `${name} `;
|
||||||
}
|
}
|
||||||
|
|
||||||
return flag.has('p') && node.type === Type.Directory ? `${name}/` : name;
|
return name
|
||||||
}
|
}
|
||||||
|
|
||||||
/* function formatOutputShort(node: TreeNode, flag: any) {
|
|
||||||
let output: string = node.name;
|
|
||||||
const char: string = flag.has('Q') ? '"' : "'";
|
|
||||||
|
|
||||||
if (/\s/.test(node.name)) {
|
|
||||||
if(flag.has('i')) {
|
|
||||||
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
output = `${char}${node.name}${char}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(flag.has('i')) {
|
|
||||||
output = `${node.inode} ${output}`
|
|
||||||
}
|
|
||||||
|
|
||||||
return flag.has('p') && node.type === Type.Directory ? ` ${output}/ ` : output;
|
|
||||||
} */
|
|
||||||
|
|
||||||
const checkFlags = (pFlags: string[]) => {
|
const checkFlags = (pFlags: string[]) => {
|
||||||
const flagSet = new Set(pFlags);
|
const flagSet = new Set(pFlags);
|
||||||
|
|
||||||
|
|||||||
13
src/lib/stores/bash/commands/ls2.ts
Normal file
13
src/lib/stores/bash/commands/ls2.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import type { ICommand } from "../static"
|
||||||
|
import { cmd_ls } from "./ls"
|
||||||
|
|
||||||
|
type LsEntry
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
export class ls {
|
||||||
|
public const ls: ICommand = {
|
||||||
|
method: ls.cmd_ls
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
export type Group = {
|
|
||||||
groupname: string;
|
|
||||||
gid: number; // Primary group 'Users' 1000 - Others - 1000+ root - 0
|
|
||||||
members: number[]; //TODO: Make a formated type UID
|
|
||||||
};
|
|
||||||
|
|
||||||
export const GROUP: Group[] = [
|
|
||||||
{
|
|
||||||
groupname: 'sudo',
|
|
||||||
gid: 69,
|
|
||||||
members: [0, 1001]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
groupname: 'users',
|
|
||||||
gid: 984,
|
|
||||||
members: [1001, 1002]
|
|
||||||
}
|
|
||||||
] as const;
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
export type User = {
|
|
||||||
username: string;
|
|
||||||
passwd: string; //HASHED PASSWORD //TODO: Make a formated type
|
|
||||||
readonly uid: number; // Normal user 1000+ System user 1-999 root - 0 //TODO: Make a formated type
|
|
||||||
readonly gid: number; // Primary group | 'Users' 1000 - Others - 1000+ root - 0 //TODO: Make a formated type
|
|
||||||
home: string; //TODO: Make a formated type
|
|
||||||
history: string[];
|
|
||||||
cwd?: number; //TODO: Make a formated type
|
|
||||||
pwd?: number; //TODO: Make a formated type
|
|
||||||
};
|
|
||||||
|
|
||||||
export const PASSWD: User[] = [
|
|
||||||
{
|
|
||||||
username: 'root',
|
|
||||||
passwd: '123',
|
|
||||||
uid: 0,
|
|
||||||
gid: 0,
|
|
||||||
home: '/',
|
|
||||||
history: []
|
|
||||||
},
|
|
||||||
{
|
|
||||||
username: 'admin',
|
|
||||||
passwd: '456',
|
|
||||||
uid: 1000,
|
|
||||||
gid: 1000,
|
|
||||||
home: '/home/admin',
|
|
||||||
history: []
|
|
||||||
},
|
|
||||||
{
|
|
||||||
username: 'user',
|
|
||||||
passwd: '789',
|
|
||||||
uid: 1001,
|
|
||||||
gid: 1000,
|
|
||||||
home: '/home/user',
|
|
||||||
history: []
|
|
||||||
},
|
|
||||||
{
|
|
||||||
username: 'kamil',
|
|
||||||
passwd: '000',
|
|
||||||
uid: 1002,
|
|
||||||
gid: 1000,
|
|
||||||
home: '/home/kamil',
|
|
||||||
history: []
|
|
||||||
}
|
|
||||||
];
|
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
import type { Bash } from "./bash";
|
import type { Permission, TimeStamps, User } from './bash';
|
||||||
import type { Permission, TimeStamps } from "./metadata";
|
|
||||||
|
|
||||||
export enum Type {
|
export enum Type {
|
||||||
Directory = 16384,
|
Directory = 16384,
|
||||||
@@ -15,7 +14,7 @@ export type NodePerms = {
|
|||||||
|
|
||||||
export type FsInitArgs = {
|
export type FsInitArgs = {
|
||||||
fs: any;
|
fs: any;
|
||||||
bash: Bash;
|
user: User;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type TreeNode = {
|
export type TreeNode = {
|
||||||
@@ -38,7 +37,6 @@ export type TreeNode = {
|
|||||||
export class VirtualFS {
|
export class VirtualFS {
|
||||||
private FsTable: Map<number, TreeNode>;
|
private FsTable: Map<number, TreeNode>;
|
||||||
private rootINode: number;
|
private rootINode: number;
|
||||||
private _Bash: Bash;
|
|
||||||
|
|
||||||
home: number;
|
home: number;
|
||||||
cwd: number;
|
cwd: number;
|
||||||
@@ -47,10 +45,9 @@ export class VirtualFS {
|
|||||||
constructor(args: FsInitArgs) {
|
constructor(args: FsInitArgs) {
|
||||||
this.FsTable = args.fs;
|
this.FsTable = args.fs;
|
||||||
this.rootINode = 1;
|
this.rootINode = 1;
|
||||||
this._Bash = args.bash;
|
this.home = this._pathStringToINode(args.user.home);
|
||||||
this.home = this._pathStringToINode(args.bash.getUser().home);
|
this.cwd = args.user.cwd ? args.user.cwd : this.home;
|
||||||
this.cwd = args.bash.getUser().cwd ? args.bash.getUser().cwd! : this.home;
|
this.pwd = args.user.pwd ? args.user.pwd : this.cwd;
|
||||||
this.pwd = args.bash.getUser().pwd ? args.bash.getUser().pwd! : this.cwd;
|
|
||||||
|
|
||||||
console.log(this.home);
|
console.log(this.home);
|
||||||
console.log(this.cwd);
|
console.log(this.cwd);
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
export type TimeStamps = {
|
|
||||||
modified: Date;
|
|
||||||
changed: Date;
|
|
||||||
accessed: Date;
|
|
||||||
};
|
|
||||||
|
|
||||||
// TODO: Finish this
|
|
||||||
export enum ExitCode {
|
|
||||||
SUCCESS = 0,
|
|
||||||
ERROR = 1
|
|
||||||
}
|
|
||||||
|
|
||||||
export type Permission = {
|
|
||||||
r: boolean;
|
|
||||||
w: boolean;
|
|
||||||
x: boolean;
|
|
||||||
};
|
|
||||||
82
src/lib/stores/bash/commandRegistry.ts → src/lib/stores/bash/static.ts
Normal file → Executable file
82
src/lib/stores/bash/commandRegistry.ts → src/lib/stores/bash/static.ts
Normal file → Executable file
@@ -1,26 +1,80 @@
|
|||||||
import type { Bash, Result } from "./bash";
|
import { Bash, ExitCode, type Group, type User } from './bash';
|
||||||
import { cd } from "./commands/cd";
|
import { ls } from './commands/ls';
|
||||||
import { clear } from "./commands/clear";
|
import { cd } from './commands/cd';
|
||||||
import { ls } from "./commands/ls";
|
import { clear } from './commands/clear';
|
||||||
|
|
||||||
export type ICommand = {
|
export type ICommand = {
|
||||||
method: (this: Bash, args: CommandArgs) => Result;
|
method: (this: Bash, args: CommandArgs) => Result;
|
||||||
flags: string[];
|
flags: string[];
|
||||||
help: string;
|
help: string;
|
||||||
root: boolean;
|
root: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type CommandArgs = {
|
export type CommandArgs = {
|
||||||
flags: string[];
|
flags: string[];
|
||||||
args: string[];
|
args: string[];
|
||||||
};
|
};
|
||||||
|
|
||||||
export type CommandResultData = {
|
export type resultData = {
|
||||||
cmd: string; //the string that contains the shorthand for the command that was executed - used in a switch statement in parseResult
|
cmd: string; //the string that contains the shorthand for the command that was executed - used in a switch statement in parseResult
|
||||||
data: any; //the data that the commmand may have returned like TreeNodes[] from ls
|
data: any; //the data that the commmand may have returned like TreeNodes[] from ls
|
||||||
args?: CommandArgs;
|
args?: CommandArgs;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type Result = {
|
||||||
|
exitCode: ExitCode;
|
||||||
|
path: number; //the inode of the place that the command was executed in
|
||||||
|
data?: resultData;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const GROUP: Group[] = [
|
||||||
|
{
|
||||||
|
groupname: 'sudo',
|
||||||
|
gid: 69,
|
||||||
|
members: [0, 1001]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
groupname: 'users',
|
||||||
|
gid: 984,
|
||||||
|
members: [1001, 1002]
|
||||||
|
}
|
||||||
|
] as const;
|
||||||
|
|
||||||
|
export const PASSWD: User[] = [
|
||||||
|
{
|
||||||
|
username: 'root',
|
||||||
|
passwd: '123',
|
||||||
|
uid: 0,
|
||||||
|
gid: 0,
|
||||||
|
home: '/',
|
||||||
|
history: [] //TODO: Delete this and declare a new history array when logging the user in.
|
||||||
|
},
|
||||||
|
{
|
||||||
|
username: 'admin',
|
||||||
|
passwd: '456',
|
||||||
|
uid: 1000,
|
||||||
|
gid: 1000,
|
||||||
|
home: '/home/admin',
|
||||||
|
history: [] //TODO: Delete this and declare a new history array when logging the user in.
|
||||||
|
},
|
||||||
|
{
|
||||||
|
username: 'user',
|
||||||
|
passwd: '789',
|
||||||
|
uid: 1001,
|
||||||
|
gid: 1000,
|
||||||
|
home: '/home/user',
|
||||||
|
history: [] //TODO: Delete this and declare a new history array when logging the user in.
|
||||||
|
},
|
||||||
|
{
|
||||||
|
username: 'kamil',
|
||||||
|
passwd: '000',
|
||||||
|
uid: 1002,
|
||||||
|
gid: 1000,
|
||||||
|
home: '/home/kamil',
|
||||||
|
history: [] //TODO: Delete this and declare a new history array when logging the user in.
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
export const COMMANDS = {
|
export const COMMANDS = {
|
||||||
cd,
|
cd,
|
||||||
ls,
|
ls,
|
||||||
@@ -4,13 +4,10 @@ import deFlag from '$lib/assets/deFlag.svg';
|
|||||||
import frFlag from '$lib/assets/frFlag.svg';
|
import frFlag from '$lib/assets/frFlag.svg';
|
||||||
import jaFlag from '$lib/assets/jaFlag.svg';
|
import jaFlag from '$lib/assets/jaFlag.svg';
|
||||||
|
|
||||||
import { writable } from 'svelte/store';
|
export const langs = {
|
||||||
|
pl: {},
|
||||||
function getInitalLocale(): string {
|
en: {},
|
||||||
if (typeof navigator === 'undefined') return 'en-US';
|
de: {},
|
||||||
|
ja: {},
|
||||||
const sysPrefLocale = navigator.language
|
fr: {}
|
||||||
return sysPrefLocale
|
};
|
||||||
}
|
|
||||||
|
|
||||||
export const locale = writable(getInitalLocale());
|
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import type { User } from '../bash/etc/userData';
|
import type { User } from '../bash/bash';
|
||||||
import type { TreeNode } from '../bash/fs';
|
import type { TreeNode } from '../bash/fs';
|
||||||
import { type ILocale } from './localeRegistry';
|
import { Terminal, type TermInitArgs } from './terminal';
|
||||||
import { Terminal, type PageCallbacks, type TermInitArgs } from './terminal';
|
|
||||||
|
|
||||||
let initializing = $state(true);
|
let initializing = $state(true);
|
||||||
|
|
||||||
@@ -100,34 +99,16 @@ async function fetchFileSystem(path: string): Promise<any> {
|
|||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchLocale(id: string): Promise<any> {
|
export async function initTerminal(user: User, callbackInit: any): Promise<Terminal> {
|
||||||
const response = await fetch(`/src/lib/assets/locales/terminal/${id}.json`);
|
|
||||||
if(!response.ok) throw new Error('Failed to fetch the chosen locale');
|
|
||||||
|
|
||||||
const data = await response.json();
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function initTerminal(
|
|
||||||
user: {username: string, password: string},
|
|
||||||
callbackInit: PageCallbacks,
|
|
||||||
localeId: string
|
|
||||||
): Promise<Terminal> {
|
|
||||||
try {
|
try {
|
||||||
const sig = await fetchFsSignature('/src/lib/assets/fs/signature');
|
const sig = await fetchFsSignature('/src/lib/assets/fs/signature');
|
||||||
const fsJson = await fetchFsJson(sig);
|
const fsJson = await fetchFsJson(sig);
|
||||||
const fs: Map<number, TreeNode> = jsonToNodeTable(fsJson);
|
const fs: Map<number, TreeNode> = jsonToNodeTable(fsJson);
|
||||||
const locale: ILocale = {
|
|
||||||
id: localeId,
|
|
||||||
keys: fetchLocale(localeId)
|
|
||||||
};
|
|
||||||
|
|
||||||
const args: TermInitArgs = {
|
const args: TermInitArgs = {
|
||||||
fs,
|
|
||||||
locale,
|
|
||||||
bash: {
|
bash: {
|
||||||
user,
|
user,
|
||||||
instanceId: 0
|
fs
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +0,0 @@
|
|||||||
export interface ILocale {
|
|
||||||
id: string;
|
|
||||||
keys?: {};
|
|
||||||
}
|
|
||||||
|
|
||||||
const en: ILocale = {
|
|
||||||
id: 'en_US',
|
|
||||||
keys: {}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const LOCALES = {
|
|
||||||
en
|
|
||||||
} as const satisfies Record<string, ILocale>;
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
import { mount, unmount } from 'svelte';
|
import { mount, unmount } from 'svelte';
|
||||||
import Output from '../../../../modules/terminal/Output.svelte';
|
import Output from '../../../modules/terminal/Output.svelte';
|
||||||
import { isInitializing } from '../init.svelte';
|
import { isInitializing } from './init.svelte';
|
||||||
import type { PrintData } from '../terminal';
|
import type { PrintData } from './terminal';
|
||||||
|
|
||||||
interface OutputProps {
|
interface OutputProps {
|
||||||
path: string;
|
path: string;
|
||||||
@@ -23,8 +23,6 @@ function appendOutput(container: HTMLElement, props: OutputProps): Output | unde
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function print(e: HTMLElement, data: PrintData): void {
|
export function print(e: HTMLElement, data: PrintData): void {
|
||||||
if(data.cmd == 'clear') return;
|
|
||||||
|
|
||||||
if (isInitializing()) {
|
if (isInitializing()) {
|
||||||
console.error('Terminal is initializing! Skipping Print');
|
console.error('Terminal is initializing! Skipping Print');
|
||||||
return;
|
return;
|
||||||
@@ -37,7 +35,6 @@ export function print(e: HTMLElement, data: PrintData): void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function clear(): void {
|
export function clear(): void {
|
||||||
for(const instance of outputInstances) {
|
console.log("outInstances", outputInstances);
|
||||||
unmount(instance, {});
|
unmount(outputInstances);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
export interface KeyStroke {
|
|
||||||
keys: KeyboardEvent[];
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,16 +1,12 @@
|
|||||||
import { Bash, type BashInitArgs } from '../bash/bash';
|
import { Bash, ExitCode, type BashInitArgs, type User } from '../bash/bash';
|
||||||
import type { CommandArgs } from '../bash/commandRegistry';
|
import type { VirtualFS } from '../bash/fs';
|
||||||
import type { User } from '../bash/etc/userData';
|
import type { CommandArgs } from '../bash/static';
|
||||||
import { VirtualFS, type TreeNode } from '../bash/fs';
|
import { Char } from '../char';
|
||||||
import type { ExitCode } from '../bash/metadata';
|
|
||||||
import type { ILocale } from './localeRegistry';
|
|
||||||
|
|
||||||
export type TerminalMode = {};
|
export type TerminalMode = {};
|
||||||
|
|
||||||
export type TermInitArgs = {
|
export type TermInitArgs = {
|
||||||
fs: Map<number, TreeNode>;
|
|
||||||
bash: BashInitArgs;
|
bash: BashInitArgs;
|
||||||
locale: ILocale
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export type ParsedInput = {
|
export type ParsedInput = {
|
||||||
@@ -32,28 +28,12 @@ export type PageCallbacks = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export class Terminal {
|
export class Terminal {
|
||||||
private _bashInstanceId: number;
|
private bash: Bash;
|
||||||
private _bashInstances: Bash[];
|
private callbacks: Partial<PageCallbacks> = {};
|
||||||
private _callbacks: Partial<PageCallbacks> = {};
|
|
||||||
public fileSystem: VirtualFS;
|
|
||||||
public locale: ILocale;
|
|
||||||
|
|
||||||
constructor(args: TermInitArgs) {
|
constructor(args: TermInitArgs) {
|
||||||
args.bash.io = this;
|
args.bash.stdio = this;
|
||||||
this._bashInstanceId = 0;
|
this.bash = new Bash(args.bash);
|
||||||
this._bashInstances = [];
|
|
||||||
|
|
||||||
this._initializeBashInstance(args.bash);
|
|
||||||
|
|
||||||
this.fileSystem = new VirtualFS({ fs: args.fs, bash: this._bashInstances[this._bashInstanceId] });
|
|
||||||
this.locale = args.locale;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private _initializeBashInstance(init: BashInitArgs) {
|
|
||||||
const instance: Bash = new Bash(init);
|
|
||||||
this._bashInstances.push(instance);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private _parseInput(input: string): ParsedInput {
|
private _parseInput(input: string): ParsedInput {
|
||||||
@@ -109,50 +89,47 @@ export class Terminal {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
throwExeption(message: string, exitCode: ExitCode) {
|
|
||||||
console.error(message, exitCode);
|
|
||||||
}
|
|
||||||
|
|
||||||
executeCommand(input: string): void {
|
executeCommand(input: string): void {
|
||||||
this._bashInstances[this._bashInstanceId].updateHistory(input);
|
this.bash.updateHistory(input);
|
||||||
const parsed: ParsedInput = this._parseInput(input);
|
const parsed: ParsedInput = this._parseInput(input);
|
||||||
console.log(parsed, 'executeCommand output');
|
console.log(parsed, 'executeCommand output');
|
||||||
this._bashInstances[this._bashInstanceId].executeCommand(parsed.command, parsed.args);
|
this.bash.executeCommand(parsed.command, parsed.args);
|
||||||
}
|
}
|
||||||
|
|
||||||
registerCallbacks(callbacks: PageCallbacks): void {
|
registerCallbacks(callbacks: PageCallbacks): void {
|
||||||
this._callbacks = callbacks;
|
this.callbacks = callbacks;
|
||||||
}
|
|
||||||
|
|
||||||
clearTerminal() {
|
|
||||||
this._callbacks.clear?.();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getUser(): User {
|
getUser(): User {
|
||||||
return this._bashInstances[this._bashInstanceId].getUser();
|
return this.bash.getUser();
|
||||||
}
|
}
|
||||||
|
|
||||||
getTerminalWidth(): number {
|
getTerminalWidth(): number {
|
||||||
const width = this._callbacks.getWidth?.();
|
const width = this.callbacks.getWidth?.();
|
||||||
if(!width) { throw new Error('somehow width is undefined still after all the checks'); }
|
if(!width) { throw new Error('somehow width is undefined still after all the checks'); }
|
||||||
|
|
||||||
return width;
|
return width;
|
||||||
}
|
}
|
||||||
|
|
||||||
getFontSize(): number {
|
getFontSize(): number {
|
||||||
const size = this._callbacks.getFontSize?.();
|
const size = this.callbacks.getFontSize?.();
|
||||||
if(!size) { throw new Error('somehow font size is undefined still after all the checks'); }
|
if(!size) { throw new Error('somehow font size is undefined still after all the checks'); }
|
||||||
|
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
getCwd(): string {
|
getCwd(): string {
|
||||||
const fs: VirtualFS = this._bashInstances[this._bashInstanceId].getFs();
|
const fs: VirtualFS = this.bash.getFs();
|
||||||
console.log(fs.getPathByInode(this._bashInstances[this._bashInstanceId].getCwd()));
|
console.log(fs.getPathByInode(this.bash.getCwd()));
|
||||||
return fs.formatPath(fs.getPathByInode(this._bashInstances[this._bashInstanceId].getCwd()));
|
return fs.formatPath(fs.getPathByInode(this.bash.getCwd()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO: Later reimplement the backend helper methods
|
||||||
|
/* userLogin(username: string, passwd: string): ExitCode {
|
||||||
|
return this.bash.userLogin(username, passwd);
|
||||||
|
} */
|
||||||
|
|
||||||
PrintOutput(data: PrintData) {
|
PrintOutput(data: PrintData) {
|
||||||
this._callbacks.print?.(data);
|
this.callbacks.print?.(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { Languages, Settings2, SunMoon } from '@lucide/svelte';
|
import { Languages, Settings2, SunMoon } from '@lucide/svelte';
|
||||||
import { theme } from '$lib/stores/theme';
|
import { theme } from '$lib/stores/theme';
|
||||||
import { locale } from '$lib/stores/lang';
|
|
||||||
import plFlag from '$lib/assets/plFlag.svg';
|
import plFlag from '$lib/assets/plFlag.svg';
|
||||||
import enFlag from '$lib/assets/enFlag.svg';
|
import enFlag from '$lib/assets/enFlag.svg';
|
||||||
import deFlag from '$lib/assets/deFlag.svg';
|
import deFlag from '$lib/assets/deFlag.svg';
|
||||||
@@ -19,10 +18,6 @@
|
|||||||
const langsMenu = window.document.getElementById('langs-menu');
|
const langsMenu = window.document.getElementById('langs-menu');
|
||||||
langsMenu?.classList.toggle('hide');
|
langsMenu?.classList.toggle('hide');
|
||||||
}
|
}
|
||||||
|
|
||||||
function setLang(id: string) {
|
|
||||||
|
|
||||||
}
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
@@ -55,21 +50,19 @@
|
|||||||
<label
|
<label
|
||||||
class="border-primary-dark duration-100 hover:ml-1 hover:pl-1 has-checked:border-l-3 has-checked:hover:m-0 has-checked:hover:p-0 light:border-primary-light"
|
class="border-primary-dark duration-100 hover:ml-1 hover:pl-1 has-checked:border-l-3 has-checked:hover:m-0 has-checked:hover:p-0 light:border-primary-light"
|
||||||
>
|
>
|
||||||
<input name="langs" type="radio" class="lang hidden" id="pl-PL" autocomplete="off" onclick={() => {console.log($locale)}}/>
|
<input name="langs" type="radio" class="lang hidden" id="pl" autocomplete="off" />
|
||||||
<img class="flags mx-2" src={plFlag} alt="PL" height="26" width="26" />
|
<img class="flags mx-2" src={plFlag} alt="PL" height="26" width="26" />
|
||||||
</label>
|
</label>
|
||||||
<label
|
<label
|
||||||
class="border-primary-dark duration-100 hover:ml-1 hover:pl-1 has-checked:border-l-3 has-checked:hover:m-0 has-checked:hover:p-0 light:border-primary-light"
|
class="border-primary-dark duration-100 hover:ml-1 hover:pl-1 has-checked:border-l-3 has-checked:hover:m-0 has-checked:hover:p-0 light:border-primary-light"
|
||||||
>
|
>
|
||||||
<input name="langs" type="radio" class="lang hidden" id="en-US" autocomplete="off" onclick={(id) => {
|
<input name="langs" type="radio" class="lang hidden" id="en" autocomplete="off" />
|
||||||
console.log(id);
|
|
||||||
}}/>
|
|
||||||
<img class="flags mx-2" src={enFlag} alt="EN" height="26" width="26" />
|
<img class="flags mx-2" src={enFlag} alt="EN" height="26" width="26" />
|
||||||
</label>
|
</label>
|
||||||
<label
|
<label
|
||||||
class="border-primary-dark duration-100 hover:ml-1 hover:pl-1 has-checked:border-l-3 has-checked:hover:m-0 has-checked:hover:p-0 light:border-primary-light"
|
class="border-primary-dark duration-100 hover:ml-1 hover:pl-1 has-checked:border-l-3 has-checked:hover:m-0 has-checked:hover:p-0 light:border-primary-light"
|
||||||
>
|
>
|
||||||
<input name="langs" type="radio" class="lang hidden" id="fr-FR" autocomplete="off" />
|
<input name="langs" type="radio" class="lang hidden" id="en" autocomplete="off" />
|
||||||
<img class="flags mx-2" src={deFlag} alt="DE" height="26" width="26" />
|
<img class="flags mx-2" src={deFlag} alt="DE" height="26" width="26" />
|
||||||
</label>
|
</label>
|
||||||
<label
|
<label
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
import type { User } from '$lib/stores/bash/bash';
|
||||||
import { Terminal, type PrintData } from '$lib/stores/terminal/terminal';
|
import { Terminal, type PrintData } from '$lib/stores/terminal/terminal';
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
import Input from './terminal/Input.svelte';
|
import Input from './terminal/Input.svelte';
|
||||||
import { initTerminal, isInitializing } from '$lib/stores/terminal/init.svelte';
|
import { initTerminal, isInitializing } from '$lib/stores/terminal/init.svelte';
|
||||||
import { clear, print } from '$lib/stores/terminal/stdio/io';
|
import { clear, print } from '$lib/stores/terminal/stdio';
|
||||||
import type { User } from '$lib/stores/bash/etc/userData';
|
|
||||||
|
|
||||||
const clearTerminal = (): void => clear();
|
const clearTerminal = (): void => clear();
|
||||||
|
|
||||||
@@ -26,7 +26,8 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getFontSize() {
|
function getFontSize() {
|
||||||
const e = document.getElementById('cout');
|
return 10;
|
||||||
|
/* const e = document.getElementById('cout');
|
||||||
if(!e) {
|
if(!e) {
|
||||||
throw new Error('cant get font size of the terminal element. its null');
|
throw new Error('cant get font size of the terminal element. its null');
|
||||||
}
|
}
|
||||||
@@ -34,10 +35,10 @@
|
|||||||
const canvas = document.createElement('canvas');
|
const canvas = document.createElement('canvas');
|
||||||
const ctx = canvas.getContext('2d')!;
|
const ctx = canvas.getContext('2d')!;
|
||||||
ctx.font = `${window.getComputedStyle(e, null).getPropertyValue('font-size')} 'JetBrains Mono', monospace;`;
|
ctx.font = `${window.getComputedStyle(e, null).getPropertyValue('font-size')} 'JetBrains Mono', monospace;`;
|
||||||
return ctx.measureText('M').width;
|
return ctx.measureText(' ').width; */
|
||||||
}
|
}
|
||||||
|
|
||||||
function inputHandler(event: KeyboardEvent) {
|
function handleInput(event: KeyboardEvent) {
|
||||||
switch (event.key) {
|
switch (event.key) {
|
||||||
case 'Enter': {
|
case 'Enter': {
|
||||||
terminal.executeCommand(inputValue);
|
terminal.executeCommand(inputValue);
|
||||||
@@ -73,7 +74,7 @@
|
|||||||
if (!e) return;
|
if (!e) return;
|
||||||
printOutput(e, data);
|
printOutput(e, data);
|
||||||
},
|
},
|
||||||
clear: clearTerminal,
|
clear: clear,
|
||||||
getWidth: getWidth,
|
getWidth: getWidth,
|
||||||
getFontSize: getFontSize
|
getFontSize: getFontSize
|
||||||
};
|
};
|
||||||
@@ -97,7 +98,7 @@
|
|||||||
|
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
try {
|
try {
|
||||||
terminal = await initTerminal({ username: 'root', password: '123'}, callbackInit, 'en_US');
|
terminal = await initTerminal(testUser, callbackInit);
|
||||||
updateTerminal();
|
updateTerminal();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('onMount trycatch failed', error);
|
console.error('onMount trycatch failed', error);
|
||||||
@@ -105,7 +106,7 @@
|
|||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<label for="input" onkeydowncapture={(event) => inputHandler(event)} class="w-11/12">
|
<label for="input" onkeydowncapture={(e) => handleInput(e)} class="w-11/12">
|
||||||
<div id="terminal" class="terminal-window shadow-() h-full w-full rounded-md shadow-bg">
|
<div id="terminal" class="terminal-window shadow-() h-full w-full rounded-md shadow-bg">
|
||||||
<div
|
<div
|
||||||
class="terminal-bar flex h-9 w-full flex-row items-center rounded-t-md bg-bg-dark text-center font-terminal text-sm font-bold text-primary-dark light:bg-bg-dark-light light:text-primary-light"
|
class="terminal-bar flex h-9 w-full flex-row items-center rounded-t-md bg-bg-dark text-center font-terminal text-sm font-bold text-primary-dark light:bg-bg-dark-light light:text-primary-light"
|
||||||
|
|||||||
Reference in New Issue
Block a user