From c11e17573334dfb487a8e1cc25fa84fa9e138f5c Mon Sep 17 00:00:00 2001 From: Lily <129828376+Soccera1@users.noreply.github.com> Date: Tue, 28 Jul 2026 16:12:21 +1000 Subject: Refactor coreutils into single-call command modules (#1) * Refactor to single call --- src/commands/echo.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/commands/echo.js (limited to 'src/commands/echo.js') diff --git a/src/commands/echo.js b/src/commands/echo.js new file mode 100644 index 0000000..7f8ad5f --- /dev/null +++ b/src/commands/echo.js @@ -0,0 +1,61 @@ +#!/usr/bin/env bun + +import { VERSION, stdout } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export async function echo(args) { + if (!process.env.POSIXLY_CORRECT && args.length === 1 && args[0] === "--help") { + stdout("Usage: echo [SHORT-OPTION]... [STRING]...\n or: echo LONG-OPTION\nEcho the STRING(s) to standard output.\n\n -n do not output the trailing newline\n -e enable interpretation of backslash escapes\n -E disable interpretation of backslash escapes (default)\n"); + return 0; + } + if (!process.env.POSIXLY_CORRECT && args.length === 1 && args[0] === "--version") { + stdout(`${VERSION}\n`); + return 0; + } + let interpret = false; + let newline = true; + let i = 0; + if (process.env.POSIXLY_CORRECT) { + interpret = true; + if (args[0] === "-n") { + newline = false; + i = 1; + while (i < args.length && /^-[eE]+$/.test(args[i])) i++; + } + } else { + while (i < args.length && /^-[neE]+$/.test(args[i])) { + for (const ch of args[i].slice(1)) { + if (ch === "n") newline = false; + if (ch === "e") interpret = true; + if (ch === "E") interpret = false; + } + i++; + } + } + let text = args.slice(i).join(" "); + if (interpret) { + const stop = text.indexOf("\\c"); + if (stop !== -1) { + text = text.slice(0, stop); + newline = false; + } + text = decodeEchoEscapes(text); + } + stdout(text + (newline ? "\n" : "")); + return 0; +} + +export function decodeEchoEscapes(text) { + return text.replace(/\\(0[0-7]{0,3}|[0-7]{1,3}|x[0-9a-fA-F]{1,2}|[\\abefnrtv])/g, (_, escape) => { + const simple = { "\\": "\\", a: "\x07", b: "\b", e: "\x1b", f: "\f", n: "\n", r: "\r", t: "\t", v: "\v" }; + if (simple[escape] != null) return simple[escape]; + if (escape.startsWith("x")) return String.fromCharCode(Number.parseInt(escape.slice(1), 16)); + const digits = escape.startsWith("0") ? escape.slice(1) : escape; + return String.fromCharCode(Number.parseInt(digits || "0", 8)); + }); +} + +const singleCall = defineCommand("echo", echo, (args) => !process.env.POSIXLY_CORRECT && args.length === 1 && (args[0] === "--help" || args[0] === "--version") ? args[0] : null); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); -- cgit v1.3