From 3184d3482c7b95c0fd1fc0745555ff82fc9a8c99 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 23 May 2024 15:27:15 +0100 Subject: [PATCH] feat(phoenix): Implement an `exit` builtin --- packages/phoenix/src/main_cli.js | 2 + packages/phoenix/src/main_puter.js | 2 + packages/phoenix/src/platform/node/system.js | 9 ++++ packages/phoenix/src/platform/puter/system.js | 7 +++ .../src/puter-shell/coreutils/__exports__.js | 2 + .../phoenix/src/puter-shell/coreutils/exit.js | 48 +++++++++++++++++++ 6 files changed, 70 insertions(+) create mode 100644 packages/phoenix/src/platform/node/system.js create mode 100644 packages/phoenix/src/platform/puter/system.js create mode 100644 packages/phoenix/src/puter-shell/coreutils/exit.js diff --git a/packages/phoenix/src/main_cli.js b/packages/phoenix/src/main_cli.js index 9f3106c3..1f1808ee 100644 --- a/packages/phoenix/src/main_cli.js +++ b/packages/phoenix/src/main_cli.js @@ -21,6 +21,7 @@ import { launchPuterShell } from './puter-shell/main.js'; import { NodeStdioPTT } from './pty/NodeStdioPTT.js'; import { CreateFilesystemProvider } from './platform/node/filesystem.js'; import { CreateEnvProvider } from './platform/node/env.js'; +import { CreateSystemProvider } from './platform/node/system.js'; import { parseArgs } from '@pkgjs/parseargs'; import capcon from 'capture-console'; import fs from 'fs'; @@ -64,6 +65,7 @@ const ctx = new Context({ name: 'node', filesystem: CreateFilesystemProvider(), env: CreateEnvProvider(), + system: CreateSystemProvider(), }), }); diff --git a/packages/phoenix/src/main_puter.js b/packages/phoenix/src/main_puter.js index 2102fc07..73d03085 100644 --- a/packages/phoenix/src/main_puter.js +++ b/packages/phoenix/src/main_puter.js @@ -22,6 +22,7 @@ import { CreateFilesystemProvider } from './platform/puter/filesystem.js'; import { CreateDriversProvider } from './platform/puter/drivers.js'; import { XDocumentPTT } from './pty/XDocumentPTT.js'; import { CreateEnvProvider } from './platform/puter/env.js'; +import { CreateSystemProvider } from './platform/puter/system.js'; window.main_shell = async () => { const config = {}; @@ -73,6 +74,7 @@ window.main_shell = async () => { filesystem: CreateFilesystemProvider({ puterSDK }), drivers: CreateDriversProvider({ puterSDK }), env: CreateEnvProvider({ config }), + system: CreateSystemProvider({ puterSDK }) }), })); }; diff --git a/packages/phoenix/src/platform/node/system.js b/packages/phoenix/src/platform/node/system.js new file mode 100644 index 00000000..ec83ebfe --- /dev/null +++ b/packages/phoenix/src/platform/node/system.js @@ -0,0 +1,9 @@ +import process from 'node:process'; + +export const CreateSystemProvider = () => { + return { + exit: (code) => { + process.exit(code); + }, + } +} diff --git a/packages/phoenix/src/platform/puter/system.js b/packages/phoenix/src/platform/puter/system.js new file mode 100644 index 00000000..320ec73a --- /dev/null +++ b/packages/phoenix/src/platform/puter/system.js @@ -0,0 +1,7 @@ +export const CreateSystemProvider = ({ puterSDK }) => { + return { + exit: (code) => { + puterSDK.exit(code); + }, + } +} diff --git a/packages/phoenix/src/puter-shell/coreutils/__exports__.js b/packages/phoenix/src/puter-shell/coreutils/__exports__.js index 2ff3c9d2..7d35eb96 100644 --- a/packages/phoenix/src/puter-shell/coreutils/__exports__.js +++ b/packages/phoenix/src/puter-shell/coreutils/__exports__.js @@ -31,6 +31,7 @@ import module_dirname from './dirname.js' import module_echo from './echo.js' import module_env from './env.js' import module_errno from './errno.js' +import module_exit from './exit.js' import module_false from './false.js' import module_grep from './grep.js' import module_head from './head.js' @@ -75,6 +76,7 @@ export default { "echo": module_echo, "env": module_env, "errno": module_errno, + "exit": module_exit, "false": module_false, "grep": module_grep, "head": module_head, diff --git a/packages/phoenix/src/puter-shell/coreutils/exit.js b/packages/phoenix/src/puter-shell/coreutils/exit.js new file mode 100644 index 00000000..a3164984 --- /dev/null +++ b/packages/phoenix/src/puter-shell/coreutils/exit.js @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2024 Puter Technologies Inc. + * + * This file is part of Phoenix Shell. + * + * Phoenix Shell is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +import { Exit } from './coreutil_lib/exit.js'; + +export default { + name: 'exit', + usage: 'exit [CODE]', + description: 'Exit the shell and return the given CODE. If no argument is given, uses the most recent return code.', + args: { + $: 'simple-parser', + allowPositionals: true + }, + execute: async ctx => { + const { positionals, exit } = ctx.locals; + + let status_code = 0; + + if (positionals.length === 0) { + status_code = exit; + } else if (positionals.length === 1) { + const maybe_number = Number(positionals[0]); + if (Number.isInteger(maybe_number)) { + status_code = maybe_number; + } + } else { + await ctx.externs.err.write('exit: Too many arguments'); + throw new Exit(1); + } + + ctx.platform.system.exit(status_code); + } +};