- initial changes to data shapes
- ls command needs fixing
This commit is contained in:
+1
-6
@@ -3,12 +3,7 @@ import type { Result } from "./result";
|
|||||||
import type { ShellState } from "./shell_state";
|
import type { ShellState } from "./shell_state";
|
||||||
import type { ShellError } from "./util/errors";
|
import type { ShellError } from "./util/errors";
|
||||||
|
|
||||||
export type CommandFn = (
|
export type CommandFn = (io: CommandIO, state: ShellState, args: string[]) => Promise<CommandOutput<ShellError>>;
|
||||||
io: CommandIO,
|
|
||||||
state: ShellState,
|
|
||||||
flags: string[],
|
|
||||||
args: string[],
|
|
||||||
) => Promise<CommandOutput<ShellError>>;
|
|
||||||
|
|
||||||
export interface CommandDescriptor {
|
export interface CommandDescriptor {
|
||||||
fn: CommandFn;
|
fn: CommandFn;
|
||||||
|
|||||||
@@ -7,12 +7,7 @@ import type { ShellError } from "@core/util/errors";
|
|||||||
export const COMMAND_NAME = "";
|
export const COMMAND_NAME = "";
|
||||||
|
|
||||||
//Need to implement escape sequences
|
//Need to implement escape sequences
|
||||||
async function main(
|
async function main(io: CommandIO, state: ShellState, args: string[]): Promise<CommandOutput<ShellError>> {
|
||||||
io: CommandIO,
|
|
||||||
state: ShellState,
|
|
||||||
flags: string[],
|
|
||||||
args: string[],
|
|
||||||
): Promise<CommandOutput<ShellError>> {
|
|
||||||
const result: ResultSuccess = createSuccessResult(COMMAND_NAME);
|
const result: ResultSuccess = createSuccessResult(COMMAND_NAME);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -10,12 +10,7 @@ import { resolvePath } from "@core/util/path_resolver";
|
|||||||
export const COMMAND_NAME = "cat";
|
export const COMMAND_NAME = "cat";
|
||||||
|
|
||||||
//Need to implement escape sequences
|
//Need to implement escape sequences
|
||||||
async function main(
|
async function main(io: CommandIO, state: ShellState, args: string[]): Promise<CommandOutput<ShellError>> {
|
||||||
io: CommandIO,
|
|
||||||
state: ShellState,
|
|
||||||
flags: string[],
|
|
||||||
args: string[],
|
|
||||||
): Promise<CommandOutput<ShellError>> {
|
|
||||||
for (const arg of args) {
|
for (const arg of args) {
|
||||||
const inode = resolvePath(state, arg);
|
const inode = resolvePath(state, arg);
|
||||||
if (inode === -1) {
|
if (inode === -1) {
|
||||||
|
|||||||
@@ -13,12 +13,7 @@ import { resolvePath } from "@core/util/path_resolver";
|
|||||||
|
|
||||||
export const COMMAND_NAME = "cd";
|
export const COMMAND_NAME = "cd";
|
||||||
|
|
||||||
async function main(
|
async function main(__io: CommandIO, state: ShellState, args: string[]): Promise<CommandOutput<ShellError>> {
|
||||||
__io: CommandIO,
|
|
||||||
state: ShellState,
|
|
||||||
__flags: string[],
|
|
||||||
args: string[],
|
|
||||||
): Promise<CommandOutput<ShellError>> {
|
|
||||||
if (args.length > 1) {
|
if (args.length > 1) {
|
||||||
return {
|
return {
|
||||||
state,
|
state,
|
||||||
|
|||||||
@@ -7,12 +7,7 @@ import type { ShellError } from "@core/util/errors";
|
|||||||
export const COMMAND_NAME = "echo";
|
export const COMMAND_NAME = "echo";
|
||||||
|
|
||||||
//Need to implement escape sequences
|
//Need to implement escape sequences
|
||||||
async function main(
|
async function main(io: CommandIO, state: ShellState, args: string[]): Promise<CommandOutput<ShellError>> {
|
||||||
io: CommandIO,
|
|
||||||
state: ShellState,
|
|
||||||
__flags: string[],
|
|
||||||
args: string[],
|
|
||||||
): Promise<CommandOutput<ShellError>> {
|
|
||||||
const joined = args.join(" ");
|
const joined = args.join(" ");
|
||||||
const encoded = new TextEncoder().encode(joined + "\n");
|
const encoded = new TextEncoder().encode(joined + "\n");
|
||||||
|
|
||||||
|
|||||||
+16
-6
@@ -11,14 +11,22 @@ import { convertToUnit } from "@termos/util/format_unit";
|
|||||||
|
|
||||||
export const COMMAND_NAME = "ls";
|
export const COMMAND_NAME = "ls";
|
||||||
|
|
||||||
async function main(
|
async function main(io: CommandIO, state: ShellState, args: string[]): Promise<CommandOutput<ShellError>> {
|
||||||
io: CommandIO,
|
|
||||||
state: ShellState,
|
|
||||||
flags: string[],
|
|
||||||
args: string[],
|
|
||||||
): Promise<CommandOutput<ShellError>> {
|
|
||||||
const entries: { inode: number; path: string }[] = [];
|
const entries: { inode: number; path: string }[] = [];
|
||||||
const e = new TextEncoder();
|
const e = new TextEncoder();
|
||||||
|
|
||||||
|
const flags: Set<string> = new Set();
|
||||||
|
|
||||||
|
for (const arg of args) {
|
||||||
|
if (arg.startsWith("--")) {
|
||||||
|
arg.slice(2);
|
||||||
|
flags.add(arg);
|
||||||
|
} else if (arg.startsWith("-")) {
|
||||||
|
arg.slice(1);
|
||||||
|
flags.add(arg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const vars = flags.filter(
|
const vars = flags.filter(
|
||||||
// Match at least 1 character between each side of a singular = character
|
// Match at least 1 character between each side of a singular = character
|
||||||
f =>
|
f =>
|
||||||
@@ -238,6 +246,8 @@ function longMode(io: CommandIO, state: ShellState, nodes: TreeNode[], flags: Se
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function parseFlags() {}
|
||||||
|
|
||||||
function parseMode(mode: number) {
|
function parseMode(mode: number) {
|
||||||
const owner = (mode >> 6) & 7;
|
const owner = (mode >> 6) & 7;
|
||||||
const group = (mode >> 3) & 7;
|
const group = (mode >> 3) & 7;
|
||||||
|
|||||||
@@ -8,12 +8,7 @@ import { createImmutableBuffer } from "@core/util/immutable_buffer";
|
|||||||
|
|
||||||
const COMMAND_NAME = "async";
|
const COMMAND_NAME = "async";
|
||||||
|
|
||||||
async function main(
|
async function main(io: CommandIO, state: ShellState, __args: string[]): Promise<CommandOutput<ShellError>> {
|
||||||
io: CommandIO,
|
|
||||||
state: ShellState,
|
|
||||||
__flags: string[],
|
|
||||||
__args: string[],
|
|
||||||
): Promise<CommandOutput<ShellError>> {
|
|
||||||
while (true) {
|
while (true) {
|
||||||
const out = await io.getSignal();
|
const out = await io.getSignal();
|
||||||
|
|
||||||
|
|||||||
@@ -10,12 +10,7 @@ import { resolvePath } from "@core/util/path_resolver";
|
|||||||
export const COMMAND_NAME = "touch";
|
export const COMMAND_NAME = "touch";
|
||||||
|
|
||||||
//Need to implement escape sequences
|
//Need to implement escape sequences
|
||||||
async function main(
|
async function main(io: CommandIO, state: ShellState, args: string[]): Promise<CommandOutput<ShellError>> {
|
||||||
io: CommandIO,
|
|
||||||
state: ShellState,
|
|
||||||
flags: string[],
|
|
||||||
args: string[],
|
|
||||||
): Promise<CommandOutput<ShellError>> {
|
|
||||||
for (const arg of args) {
|
for (const arg of args) {
|
||||||
const inode = resolvePath(state, arg);
|
const inode = resolvePath(state, arg);
|
||||||
const session = getCurrentSession(state);
|
const session = getCurrentSession(state);
|
||||||
|
|||||||
+1
-52
@@ -6,13 +6,7 @@ interface UnclosedQuoteError {
|
|||||||
timestamp: number;
|
timestamp: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ParsedCommand {
|
export function tokenize(input: string): string[] {
|
||||||
command: string;
|
|
||||||
flags: string[];
|
|
||||||
args: string[];
|
|
||||||
}
|
|
||||||
|
|
||||||
function tokenize(input: string): string[] {
|
|
||||||
enum State {
|
enum State {
|
||||||
Normal,
|
Normal,
|
||||||
SingleQuote,
|
SingleQuote,
|
||||||
@@ -98,48 +92,3 @@ function tokenize(input: string): string[] {
|
|||||||
flushBuffer();
|
flushBuffer();
|
||||||
return tokens;
|
return tokens;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parseCommand(input: string): ParsedCommand {
|
|
||||||
const tokens = tokenize(input);
|
|
||||||
const command = tokens[0] ?? "";
|
|
||||||
const flags: string[] = [];
|
|
||||||
const args: string[] = [];
|
|
||||||
|
|
||||||
let pastEndOfOptions = false;
|
|
||||||
|
|
||||||
for (let i = 1; i < tokens.length; i++) {
|
|
||||||
const token = tokens[i]!;
|
|
||||||
|
|
||||||
if (pastEndOfOptions) {
|
|
||||||
args.push(token);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (token === "--") {
|
|
||||||
pastEndOfOptions = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (token.startsWith("--")) {
|
|
||||||
flags.push(token.slice(2));
|
|
||||||
continue;
|
|
||||||
} else if (token.startsWith("-")) {
|
|
||||||
if (token.includes("=")) {
|
|
||||||
flags.push(token.slice(1));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
for (const char of token.slice(1)) {
|
|
||||||
flags.push(char);
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
} else {
|
|
||||||
args.push(token);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
command,
|
|
||||||
flags,
|
|
||||||
args,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -23,14 +23,12 @@ export class Process {
|
|||||||
readonly CommandDescriptor: CommandDescriptor;
|
readonly CommandDescriptor: CommandDescriptor;
|
||||||
readonly State: ShellState;
|
readonly State: ShellState;
|
||||||
readonly Args: string[];
|
readonly Args: string[];
|
||||||
readonly Flags: string[];
|
|
||||||
|
|
||||||
constructor(pid: number, state: ShellState, descriptor: CommandDescriptor, flags: string[], args: string[]) {
|
constructor(pid: number, state: ShellState, descriptor: CommandDescriptor, args: string[]) {
|
||||||
this.PID = pid;
|
this.PID = pid;
|
||||||
this.State = state;
|
this.State = state;
|
||||||
this.Name = descriptor.name;
|
this.Name = descriptor.name;
|
||||||
this.CommandDescriptor = descriptor;
|
this.CommandDescriptor = descriptor;
|
||||||
this.Flags = flags;
|
|
||||||
this.Args = args;
|
this.Args = args;
|
||||||
// Check if positive later and throw an error if so
|
// Check if positive later and throw an error if so
|
||||||
this._MaxChunkByteSize = 1024;
|
this._MaxChunkByteSize = 1024;
|
||||||
@@ -68,7 +66,7 @@ export class Process {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const promise = this.CommandDescriptor.fn(io, this.State, this.Flags, this.Args);
|
const promise = this.CommandDescriptor.fn(io, this.State, this.Args);
|
||||||
|
|
||||||
promise
|
promise
|
||||||
.then(output => {
|
.then(output => {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import type { CommandDescriptor } from "@core/command";
|
import type { CommandDescriptor } from "@core/command";
|
||||||
import type { ShellState } from "@core/shell_state";
|
import type { ShellState } from "@core/shell_state";
|
||||||
import type { Signal } from "@core/signal";
|
import { SIGNAL_MAP, type Signal } from "@core/signal";
|
||||||
import { EventEmitter } from "./event_emitter";
|
import { EventEmitter } from "./event_emitter";
|
||||||
import { Process } from "./process";
|
import { Process } from "./process";
|
||||||
import type { ShellEventMap } from "./shell";
|
import type { ShellEventMap } from "./shell";
|
||||||
@@ -11,9 +11,9 @@ export class ProcessManager {
|
|||||||
|
|
||||||
private _EventEmitter: EventEmitter<ShellEventMap> = new EventEmitter();
|
private _EventEmitter: EventEmitter<ShellEventMap> = new EventEmitter();
|
||||||
|
|
||||||
public createProcess(state: ShellState, descriptor: CommandDescriptor, flags: string[], args: string[]): number {
|
public createProcess(state: ShellState, descriptor: CommandDescriptor, args: string[]): number {
|
||||||
const pid = ++this._PIDIndex;
|
const pid = ++this._PIDIndex;
|
||||||
const process = new Process(pid, state, descriptor, flags, args);
|
const process = new Process(pid, state, descriptor, args);
|
||||||
this._Processes.set(pid, process);
|
this._Processes.set(pid, process);
|
||||||
|
|
||||||
process.on("stdout", payload => this._EventEmitter.emit("stdout", payload));
|
process.on("stdout", payload => this._EventEmitter.emit("stdout", payload));
|
||||||
@@ -51,7 +51,7 @@ export class ProcessManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Make global signals an enum later
|
//Make global signals an enum later
|
||||||
process.sendSignal({ type: "SIGKILL" } satisfies Signal);
|
process.sendSignal({ type: SIGNAL_MAP.SIGKILL } satisfies Signal);
|
||||||
}
|
}
|
||||||
|
|
||||||
public on<K extends keyof ShellEventMap>(type: K, handler: (payload: ShellEventMap[K]) => void): () => void {
|
public on<K extends keyof ShellEventMap>(type: K, handler: (payload: ShellEventMap[K]) => void): () => void {
|
||||||
|
|||||||
+12
-19
@@ -5,7 +5,7 @@ import { withExitCode, withHistory, type ShellState } from "@core/shell_state";
|
|||||||
import { SIGNAL_MAP } from "@core/signal";
|
import { SIGNAL_MAP } from "@core/signal";
|
||||||
import type { ShellError } from "@core/util/errors";
|
import type { ShellError } from "@core/util/errors";
|
||||||
import { createImmutableBuffer } from "@core/util/immutable_buffer";
|
import { createImmutableBuffer } from "@core/util/immutable_buffer";
|
||||||
import { parseCommand } from "@core/util/parser";
|
import { tokenize } from "@core/util/parser";
|
||||||
import { EventEmitter } from "./event_emitter";
|
import { EventEmitter } from "./event_emitter";
|
||||||
import { ProcessManager } from "./process_manager";
|
import { ProcessManager } from "./process_manager";
|
||||||
|
|
||||||
@@ -64,30 +64,21 @@ export class Shell {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async execute(input: string): Promise<void> {
|
public async execute(input: string): Promise<void> {
|
||||||
let parsed;
|
const parsed = tokenize(input);
|
||||||
try {
|
|
||||||
parsed = parseCommand(input);
|
|
||||||
} catch {
|
|
||||||
const result = createErrorResult("ParseError", `Parsing error - ${input}`, {});
|
|
||||||
|
|
||||||
this._State = withExitCode(this._State, result.error.exitCode);
|
if (!parsed[0]) return;
|
||||||
this._EventEmitter.emit("result", { pid: 0, result, state: this._State });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (parsed.command === "") return;
|
|
||||||
|
|
||||||
this._State = withHistory(this._State, input);
|
this._State = withHistory(this._State, input);
|
||||||
const command = COMMAND_REGISTRY.get(parsed.command);
|
const command = COMMAND_REGISTRY.get(parsed[0]);
|
||||||
|
|
||||||
if (!command) {
|
if (!command) {
|
||||||
const encoded = new TextEncoder().encode(`Command ${parsed.command} not found`);
|
const encoded = new TextEncoder().encode(`Command ${parsed[0]} not found`);
|
||||||
|
|
||||||
const result = createErrorResult(
|
const result = createErrorResult(
|
||||||
"CommandNotFound",
|
"CommandNotFound",
|
||||||
`Command ${parsed.command} not found`,
|
`Command ${parsed[0]} not found`,
|
||||||
{ command: parsed.command },
|
{ command: parsed[0] },
|
||||||
parsed.command,
|
parsed[0],
|
||||||
createImmutableBuffer(encoded),
|
createImmutableBuffer(encoded),
|
||||||
createImmutableBuffer(new Uint8Array()),
|
createImmutableBuffer(new Uint8Array()),
|
||||||
);
|
);
|
||||||
@@ -102,7 +93,7 @@ export class Shell {
|
|||||||
"PermissionDenied",
|
"PermissionDenied",
|
||||||
`Permission Denied`,
|
`Permission Denied`,
|
||||||
{ resource: `Execution of the ${command.name} command` },
|
{ resource: `Execution of the ${command.name} command` },
|
||||||
parsed.command,
|
parsed[0],
|
||||||
);
|
);
|
||||||
|
|
||||||
this._State = withExitCode(this._State, result.error.exitCode);
|
this._State = withExitCode(this._State, result.error.exitCode);
|
||||||
@@ -110,7 +101,9 @@ export class Shell {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const pid = this._ProcessManager.createProcess(this._State, command, parsed.flags, parsed.args);
|
parsed.shift();
|
||||||
|
|
||||||
|
const pid = this._ProcessManager.createProcess(this._State, command, parsed);
|
||||||
this.BoundPID = pid;
|
this.BoundPID = pid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user