aboutsummaryrefslogtreecommitdiff
path: root/src/commands/sum.js
diff options
context:
space:
mode:
authorLily <[email protected]>2026-07-28 16:12:21 +1000
committerGitHub <[email protected]>2026-07-28 16:12:21 +1000
commitc11e17573334dfb487a8e1cc25fa84fa9e138f5c (patch)
treee91dbd19b524f9432da6bcb86b1fc193e0d02e26 /src/commands/sum.js
parent6a2f098e72e71437154ec00506e8e736aa8e1026 (diff)
downloadbnu-c11e17573334dfb487a8e1cc25fa84fa9e138f5c.tar.gz
bnu-c11e17573334dfb487a8e1cc25fa84fa9e138f5c.zip
Refactor coreutils into single-call command modules (#1)
* Refactor to single call
Diffstat (limited to 'src/commands/sum.js')
-rw-r--r--src/commands/sum.js109
1 files changed, 109 insertions, 0 deletions
diff --git a/src/commands/sum.js b/src/commands/sum.js
new file mode 100644
index 0000000..09e13b6
--- /dev/null
+++ b/src/commands/sum.js
@@ -0,0 +1,109 @@
+#!/usr/bin/env bun
+
+import { sumForFile } from "../shared/checksum.js";
+import { systemErrorMessage, textInputDiagnosticName } from "../shared/common.js";
+import { UsageError, stderr, stdout } from "../shared/diagnostics.js";
+import { defineCommand, runAsMain } from "../shared/command.js";
+
+export const SUM_LONG_OPTIONS = ["sysv", "help", "version"];
+
+export function sumMetaOption(args) {
+ for (const arg of args) {
+ if (arg === "--") return null;
+ if (!arg.startsWith("-") || arg === "-") continue;
+ if (arg.startsWith("--")) {
+ const option = normalizeSumLongOption(arg);
+ const name = option.slice(2).split("=", 1)[0];
+ if (!SUM_LONG_OPTIONS.includes(name) || option.includes("=")) return null;
+ if (option === "--help" || option === "--version") return option;
+ continue;
+ }
+ for (let j = 1; j < arg.length; j++) if (arg[j] !== "r" && arg[j] !== "s") return null;
+ }
+ return null;
+}
+
+export async function sum(args) {
+ args = normalizeSumLongOptions(args);
+ const { operands, sysv } = parseSumArgs(args);
+ const implicitStdin = operands.length === 0;
+ const files = implicitStdin ? ["-"] : operands;
+ let failed = false;
+ for (const file of files) {
+ let result;
+ try {
+ result = await sumForFile(file, sysv);
+ } catch (error) {
+ stderr(`sum: ${textInputDiagnosticName(file)}: ${systemErrorMessage(error)}\n`);
+ failed = true;
+ continue;
+ }
+ const { sum, blocks } = result;
+ const label = implicitStdin ? "" : ` ${file}`;
+ if (sysv) {
+ stdout(`${sum} ${blocks}${label}\n`);
+ } else {
+ stdout(`${String(sum).padStart(5, "0")} ${String(blocks).padStart(5)}${label}\n`);
+ }
+ }
+ return failed ? 1 : 0;
+}
+
+export function parseSumArgs(args) {
+ const operands = [];
+ let sysv = false;
+ let end = false;
+ for (const arg of args) {
+ if (end || arg === "-" || !arg.startsWith("-")) {
+ operands.push(arg);
+ if (process.env.POSIXLY_CORRECT) end = true;
+ continue;
+ }
+ if (arg === "--") {
+ end = true;
+ continue;
+ }
+ if (arg.startsWith("--")) {
+ const [name, inlineValue] = arg.slice(2).split(/=(.*)/s, 2);
+ if (!SUM_LONG_OPTIONS.includes(name)) throw new UsageError(`unrecognized option '${arg}'`, true);
+ if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true);
+ if (name === "sysv") sysv = true;
+ continue;
+ }
+ for (const ch of arg.slice(1)) {
+ if (ch === "s") sysv = true;
+ else if (ch === "r") sysv = false;
+ else throw new UsageError(`invalid option -- '${ch}'`, true);
+ }
+ }
+ return { operands, sysv };
+}
+
+export function normalizeSumLongOptions(args) {
+ const out = [];
+ let end = false;
+ for (const arg of args) {
+ if (end || arg === "--" || !arg.startsWith("--")) {
+ out.push(arg);
+ if (arg === "--") end = true;
+ continue;
+ }
+ out.push(normalizeSumLongOption(arg));
+ }
+ return out;
+}
+
+export function normalizeSumLongOption(arg) {
+ const body = arg.slice(2);
+ const eq = body.indexOf("=");
+ const name = eq === -1 ? body : body.slice(0, eq);
+ if (!name) return arg;
+ const match = SUM_LONG_OPTIONS.find((option) => option.startsWith(name));
+ if (!match) return arg;
+ return eq === -1 ? `--${match}` : `--${match}=${body.slice(eq + 1)}`;
+}
+
+const singleCall = defineCommand("sum", sum, sumMetaOption);
+export default singleCall;
+
+if (import.meta.main) await runAsMain(singleCall);