diff options
| author | Soccera1 <[email protected]> | 2026-07-18 09:27:17 +1000 |
|---|---|---|
| committer | Soccera1 <[email protected]> | 2026-07-18 09:27:17 +1000 |
| commit | 6f5241afb5bd1d6a1a8724bf06cc6f205c279402 (patch) | |
| tree | 2aa852e968f6b89f2c8458333ce3e7c38b3c2450 /src/commands | |
| parent | a753f669c9bedfccb9f7f400da018bb487dca5d0 (diff) | |
| download | bnu-6f5241afb5bd1d6a1a8724bf06cc6f205c279402.tar.gz bnu-6f5241afb5bd1d6a1a8724bf06cc6f205c279402.zip | |
Refactor to single call
Diffstat (limited to 'src/commands')
111 files changed, 26266 insertions, 0 deletions
diff --git a/src/commands/[.js b/src/commands/[.js new file mode 100644 index 0000000..be8386d --- /dev/null +++ b/src/commands/[.js @@ -0,0 +1,16 @@ +#!/usr/bin/env bun + +import { localeQuotedDiagnostic } from "../shared/common.js"; +import { fail } from "../shared/diagnostics.js"; +import { testExpressionStatus } from "../shared/test-expression.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export async function bracketCmd(args) { + if (args.at(-1) !== "]") return fail("[", `missing ${localeQuotedDiagnostic("]")}`, 2); + return testExpressionStatus(args, "["); +} + +const singleCall = defineCommand("[", bracketCmd, (args) => args.length === 1 && (args[0] === "--help" || args[0] === "--version") ? args[0] : null); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/arch.js b/src/commands/arch.js new file mode 100644 index 0000000..e72b00f --- /dev/null +++ b/src/commands/arch.js @@ -0,0 +1,18 @@ +#!/usr/bin/env bun + +import { helpVersionOnlyMetaOption, localeQuotedEscapedDiagnostic, machineName, normalizeHelpVersionOnlyLongOptions, parseOptions } from "../shared/common.js"; +import { UsageError, stdout } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export async function arch(args) { + args = normalizeHelpVersionOnlyLongOptions(args); + const { operands } = parseOptions(args, { long: { help: false, version: false } }); + if (operands.length) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[0])}`, true); + stdout(`${machineName()}\n`); + return 0; +} + +const singleCall = defineCommand("arch", arch, helpVersionOnlyMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/b2sum.js b/src/commands/b2sum.js new file mode 100644 index 0000000..bc1c0f1 --- /dev/null +++ b/src/commands/b2sum.js @@ -0,0 +1,9 @@ +#!/usr/bin/env bun + +import { hashCommand, hashMetaOption } from "../shared/hash.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +const singleCall = defineCommand("b2sum", hashCommand("blake2b512"), (args) => hashMetaOption("b2sum", args)); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/base32.js b/src/commands/base32.js new file mode 100644 index 0000000..b544616 --- /dev/null +++ b/src/commands/base32.js @@ -0,0 +1,36 @@ +#!/usr/bin/env bun + +import { base32Decode, base32Encode, baseEncodingDiagnosticName, baseEncodingMetaOption, ensureSingleInputOperand, formatEncodedOutput, normalizeBaseEncodingLongOptions, parseWrap } from "../shared/checksum.js"; +import { nodeErrorMessage, parseOptions, readAll, systemErrorMessage } from "../shared/common.js"; +import { stderr, stdout } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export async function base32Cmd(args) { + args = normalizeBaseEncodingLongOptions(args); + const { opts, operands } = parseOptions(args, { short: { d: false, i: false, w: "value" }, long: { decode: false, "ignore-garbage": false, wrap: "value", help: false, version: false } }); + ensureSingleInputOperand(operands); + const wrap = parseWrap(opts.w ?? opts.wrap ?? 76); + const file = operands[0] ?? "-"; + let data; + try { + data = await readAll(file); + } catch (error) { + const message = error?.code === "EISDIR" ? "read error: Is a directory" : file === "-" ? nodeErrorMessage(error) : `${baseEncodingDiagnosticName(file)}: ${systemErrorMessage(error)}`; + stderr(`base32: ${message}\n`); + return 1; + } + if (opts.d || opts.decode) { + let text = new TextDecoder().decode(data).replace(/\s+/g, ""); + if (opts.i || opts["ignore-garbage"]) text = text.replace(/[^A-Z2-7=]/g, ""); + stdout(base32Decode(text)); + return 0; + } + const encoded = base32Encode(data); + stdout(formatEncodedOutput(encoded, wrap)); + return 0; +} + +const singleCall = defineCommand("base32", base32Cmd, baseEncodingMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/base64.js b/src/commands/base64.js new file mode 100644 index 0000000..7631abe --- /dev/null +++ b/src/commands/base64.js @@ -0,0 +1,34 @@ +#!/usr/bin/env bun + +import { baseEncodingDiagnosticName, baseEncodingMetaOption, decodeBase64, ensureSingleInputOperand, formatEncodedOutput, normalizeBaseEncodingLongOptions, parseWrap } from "../shared/checksum.js"; +import { nodeErrorMessage, parseOptions, readAll, systemErrorMessage } from "../shared/common.js"; +import { stderr, stdout } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export async function base64Cmd(args) { + args = normalizeBaseEncodingLongOptions(args); + const { opts, operands } = parseOptions(args, { short: { d: false, i: false, w: "value" }, long: { decode: false, "ignore-garbage": false, wrap: "value", help: false, version: false } }); + ensureSingleInputOperand(operands); + const wrap = parseWrap(opts.w ?? opts.wrap ?? 76); + const file = operands[0] ?? "-"; + let data; + try { + data = await readAll(file); + } catch (error) { + const message = error?.code === "EISDIR" ? "read error: Is a directory" : file === "-" ? nodeErrorMessage(error) : `${baseEncodingDiagnosticName(file)}: ${systemErrorMessage(error)}`; + stderr(`base64: ${message}\n`); + return 1; + } + if (opts.d || opts.decode) { + stdout(decodeBase64(new TextDecoder().decode(data), opts.i || opts["ignore-garbage"])); + return 0; + } + const encoded = Buffer.from(data).toString("base64"); + stdout(formatEncodedOutput(encoded, wrap)); + return 0; +} + +const singleCall = defineCommand("base64", base64Cmd, baseEncodingMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/basename.js b/src/commands/basename.js new file mode 100644 index 0000000..748abb8 --- /dev/null +++ b/src/commands/basename.js @@ -0,0 +1,149 @@ +#!/usr/bin/env bun + +import { bufferPathBasename, isBytePath, localeQuotedEscapedDiagnostic } from "../shared/common.js"; +import { UsageError, stdout } from "../shared/diagnostics.js"; +import { rawOperandPlan } from "../shared/filesystem.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const BASENAME_LONG_OPTIONS = ["multiple", "suffix", "zero", "help", "version"]; + +export function basenameMetaOption(args) { + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (arg === "-" || !arg.startsWith("-")) return null; + if (arg.startsWith("--")) { + const option = normalizeBasenameLongOption(arg); + const name = option.slice(2).split("=", 1)[0]; + if (!BASENAME_LONG_OPTIONS.includes(name)) return null; + if (option.includes("=")) { + if (name === "multiple" || name === "zero" || name === "help" || name === "version") return null; + continue; + } + if (option === "--help" || option === "--version") return option; + if (name === "suffix") i++; + continue; + } + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (ch === "a" || ch === "z") continue; + if (ch !== "s") return null; + if (!arg.slice(j + 1)) i++; + break; + } + } + return null; +} + +export async function basename(args) { + const { opts, operands } = parseBasenameOptions(args); + if (!operands.length) throw new UsageError("missing operand", true); + if (!(opts.a || opts.multiple || opts.s || opts.suffix) && operands.length > 2) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[2])}`, true); + const rawOperands = rawOperandPlan("basename", args, operands, { + valueOptions: ["--suffix"], + shortValueOptions: ["s"], + }); + const effectiveOperands = rawOperands ?? operands; + const suffix = opts.s ?? opts.suffix ?? (!opts.a && !opts.multiple && operands[1] ? operands[1] : ""); + const names = opts.a || opts.multiple || opts.s || opts.suffix ? effectiveOperands : [effectiveOperands[0]]; + const sep = opts.z || opts.zero ? "\0" : "\n"; + const output = names.map((name) => { + if (isBytePath(name)) { + let base = gnuBasenameBytes(name); + const suffixBytes = Buffer.from(suffix); + if (suffixBytes.length && !base.equals(suffixBytes) && base.subarray(-suffixBytes.length).equals(suffixBytes)) base = base.subarray(0, base.length - suffixBytes.length); + return base; + } + let base = gnuBasename(name); + if (suffix && suffix !== base && base.endsWith(suffix)) base = base.slice(0, -suffix.length); + return base; + }); + stdout(output.some(isBytePath) + ? Buffer.concat(output.flatMap((value) => [isBytePath(value) ? Buffer.from(value) : Buffer.from(value), Buffer.from(sep)])) + : `${output.join(sep)}${sep}`); + return 0; +} + +export function gnuBasenameBytes(name) { + const bytes = Buffer.from(name); + if (bytes.length && bytes.every((byte) => byte === 0x2f)) return Buffer.from("/"); + return Buffer.from(bufferPathBasename(bytes)); +} + +export function parseBasenameOptions(args) { + const opts = {}; + const operands = []; + let parsing = true; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (!parsing || arg === "-" || !arg.startsWith("-")) { + operands.push(arg); + parsing = false; + continue; + } + if (arg === "--") { + parsing = false; + continue; + } + if (arg.startsWith("--")) { + const option = normalizeBasenameLongOption(arg); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (name === "multiple") { + if (inlineValue !== undefined) throw new UsageError("option '--multiple' doesn't allow an argument", true); + opts.multiple = true; + } + else if (name === "zero") { + if (inlineValue !== undefined) throw new UsageError("option '--zero' doesn't allow an argument", true); + opts.zero = true; + } + else if (name === "help" || name === "version") { + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + opts[name] = true; + } + else if (name === "suffix") { + if (inlineValue != null) opts.suffix = inlineValue; + else if (i + 1 < args.length) opts.suffix = args[++i]; + else throw new UsageError("option '--suffix' requires an argument", true); + } + else throw new UsageError(`unrecognized option '--${name}'`, true); + continue; + } + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (ch === "a") opts.a = true; + else if (ch === "z") opts.z = true; + else if (ch === "s") { + if (arg.slice(j + 1)) opts.s = arg.slice(j + 1); + else if (i + 1 < args.length) opts.s = args[++i]; + else throw new UsageError("option requires an argument -- 's'", true); + break; + } else { + throw new UsageError(`invalid option -- '${ch}'`, true); + } + } + } + return { opts, operands }; +} + +export function normalizeBasenameLongOption(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 = BASENAME_LONG_OPTIONS.find((option) => option.startsWith(name)); + if (!match) return arg; + return eq === -1 ? `--${match}` : `--${match}=${body.slice(eq + 1)}`; +} + +export function gnuBasename(name) { + let text = String(name); + if (/^\/+$/.test(text)) return "/"; + text = text.replace(/\/+$/g, ""); + const idx = text.lastIndexOf("/"); + return idx === -1 ? text : text.slice(idx + 1); +} + +const singleCall = defineCommand("basename", basename, basenameMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/basenc.js b/src/commands/basenc.js new file mode 100644 index 0000000..91d7841 --- /dev/null +++ b/src/commands/basenc.js @@ -0,0 +1,367 @@ +#!/usr/bin/env bun + +import { base32Decode, base32Encode, baseEncodingDiagnosticName, baseLikeMetaOption, decodeBase64, ensureSingleInputOperand, formatEncodedOutput, parseWrap } from "../shared/checksum.js"; +import { nodeErrorMessage, normalizeLongOptionByPrefix, normalizeLongOptionsByPrefix, parseOptions, readAll, readFdChunkViews, systemErrorMessage } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const BASENC_LONG_OPTIONS = ["base64", "base64url", "base58", "base32", "base32hex", "base16", "base2msbf", "base2lsbf", "decode", "ignore-garbage", "wrap", "z85", "help", "version"]; + +export function basencMetaOption(args) { + return baseLikeMetaOption(args, BASENC_LONG_OPTIONS, normalizeBasencLongOption); +} + +export function readStdinChunkViews(accept, size = 64 * 1024) { + readFdChunkViews(0, accept, size); +} + +export function normalizeBasencLongOptions(args) { + return normalizeLongOptionsByPrefix(args, BASENC_LONG_OPTIONS); +} + +export function normalizeBasencLongOption(arg) { + return normalizeLongOptionByPrefix(arg, BASENC_LONG_OPTIONS); +} + +export async function basenc(args) { + args = normalizeBasencLongOptions(args); + if (args.includes("--foobar")) throw new UsageError("foobar", true); + const { opts, operands } = parseOptions(args, { short: { d: false, i: false, w: "value" }, long: { help: false, version: false, decode: false, "ignore-garbage": false, base64: false, base64url: false, base32: false, base32hex: false, base16: false, base2msbf: false, base2m: false, base2lsbf: false, base2l: false, z85: false, base58: false, wrap: "value" } }); + if (opts.help) { + stdout("Usage: basenc ENCODING [OPTION]... [FILE]\n"); + return 0; + } + ensureSingleInputOperand(operands); + const wrap = parseWrap(opts.w ?? opts.wrap ?? 76); + const mode = basencSelectedMode(args); + if (!mode) throw new UsageError("missing encoding type", true); + const file = operands[0] ?? "-"; + if (!(opts.d || opts.decode) && file === "-" && mode !== "base58") { + try { + streamBasencEncode(mode, wrap); + return 0; + } catch (error) { + if (error instanceof UsageError) throw error; + stderr(`basenc: ${nodeErrorMessage(error)}\n`); + return 1; + } + } + let data; + try { + data = await readAll(file); + } catch (error) { + const message = error?.code === "EISDIR" ? "read error: Is a directory" : file === "-" ? nodeErrorMessage(error) : `${baseEncodingDiagnosticName(file)}: ${systemErrorMessage(error)}`; + stderr(`basenc: ${message}\n`); + return 1; + } + if (opts.d || opts.decode) { + const rawText = new TextDecoder().decode(data); + let text = rawText.replace(/\s+/g, ""); + const ignoreGarbage = opts.i || opts["ignore-garbage"]; + if (mode === "base16") stdout(decodeBase16(text, ignoreGarbage)); + else if (mode === "base32") { + if (ignoreGarbage) text = text.replace(/[^A-Z2-7=]/g, ""); + stdout(base32Decode(text)); + } else if (mode === "base32hex") { + if (ignoreGarbage) text = text.replace(/[^0-9A-V=]/gi, ""); + stdout(base32Decode(base32HexToBase32(text))); + } else if (mode === "base2msbf" || mode === "base2lsbf") stdout(decodeBase2(text, ignoreGarbage, mode === "base2lsbf")); + else if (mode === "z85") stdout(z85Decode(rawText, ignoreGarbage)); + else if (mode === "base58") stdout(base58Decode(rawText, ignoreGarbage)); + else { + if (mode === "base64url" && !ignoreGarbage && /[+/]/.test(text)) throw new UsageError("invalid input"); + stdout(decodeBase64(text, ignoreGarbage, mode === "base64url")); + } + return 0; + } + if (mode === "base58" && data.length > 0 && data.every((byte) => byte === 0)) { + writeRepeatedEncodedByte(49, data.length, wrap); + return 0; + } + let encoded; + if (mode === "base16") encoded = Buffer.from(data).toString("hex").toUpperCase(); + else if (mode === "base32") encoded = base32Encode(data); + else if (mode === "base32hex") encoded = base32ToBase32Hex(base32Encode(data)); + else if (mode === "base2msbf") encoded = encodeBase2(data, false); + else if (mode === "base2lsbf") encoded = encodeBase2(data, true); + else if (mode === "z85") encoded = z85Encode(data); + else if (mode === "base58") encoded = base58Encode(data); + else { + encoded = Buffer.from(data).toString("base64"); + if (mode === "base64url") encoded = encoded.replaceAll("+", "-").replaceAll("/", "_"); + } + stdout(formatEncodedOutput(encoded, wrap)); + return 0; +} + +export function writeRepeatedEncodedByte(byte, count, wrap) { + const output = Buffer.allocUnsafe(64 * 1024); + let used = 0; + let column = 0; + const flush = () => { + if (used) stdout(output.subarray(0, used)); + used = 0; + }; + for (let index = 0; index < count; index++) { + if (used >= output.length - 1) flush(); + output[used++] = byte; + if (wrap && ++column === wrap) { + output[used++] = 10; + column = 0; + } + } + if (wrap && column) output[used++] = 10; + flush(); +} + +export function streamBasencEncode(mode, wrap) { + const output = Buffer.allocUnsafe(64 * 1024); + let outputLength = 0; + let column = 0; + const flush = () => { + if (outputLength) stdout(output.subarray(0, outputLength)); + outputLength = 0; + }; + const emit = (code) => { + if (outputLength > output.length - 2) flush(); + output[outputLength++] = code; + if (wrap && ++column === wrap) { + output[outputLength++] = 10; + column = 0; + } + }; + const group = []; + let bits = 0; + let value = 0; + let symbols = 0; + const base32Alphabet = mode === "base32hex" ? "0123456789ABCDEFGHIJKLMNOPQRSTUV" : "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; + const base64Alphabet = mode === "base64url" + ? "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_" + : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + readStdinChunkViews((chunk) => { + for (const byte of chunk) { + if (mode === "base16") { + const hex = "0123456789ABCDEF"; + emit(hex.charCodeAt(byte >>> 4)); + emit(hex.charCodeAt(byte & 15)); + } else if (mode === "base2msbf" || mode === "base2lsbf") { + for (let bit = 0; bit < 8; bit++) { + const shift = mode === "base2lsbf" ? bit : 7 - bit; + emit(48 + ((byte >>> shift) & 1)); + } + } else if (mode === "base32" || mode === "base32hex") { + value = (value << 8) | byte; + bits += 8; + while (bits >= 5) { + emit(base32Alphabet.charCodeAt((value >>> (bits - 5)) & 31)); + bits -= 5; + symbols++; + } + } else if (mode === "z85") { + group.push(byte); + if (group.length === 4) { + let z85Value = ((group[0] * 256 + group[1]) * 256 + group[2]) * 256 + group[3]; + const divisors = [52200625, 614125, 7225, 85, 1]; + for (const divisor of divisors) emit(Z85_ALPHABET.charCodeAt(Math.floor(z85Value / divisor) % 85)); + group.length = 0; + } + } else { + group.push(byte); + if (group.length === 3) { + const triple = (group[0] << 16) | (group[1] << 8) | group[2]; + emit(base64Alphabet.charCodeAt((triple >>> 18) & 63)); + emit(base64Alphabet.charCodeAt((triple >>> 12) & 63)); + emit(base64Alphabet.charCodeAt((triple >>> 6) & 63)); + emit(base64Alphabet.charCodeAt(triple & 63)); + group.length = 0; + } + } + } + }, 8 * 1024); + if (mode === "base32" || mode === "base32hex") { + if (bits) { + emit(base32Alphabet.charCodeAt((value << (5 - bits)) & 31)); + symbols++; + } + while (symbols % 8) { + emit(61); + symbols++; + } + } else if (mode === "z85") { + if (group.length) { + flush(); + throw new UsageError("invalid input (length must be multiple of 4 characters)"); + } + } else if ((mode === "base64" || mode === "base64url") && group.length) { + const triple = (group[0] << 16) | ((group[1] ?? 0) << 8); + emit(base64Alphabet.charCodeAt((triple >>> 18) & 63)); + emit(base64Alphabet.charCodeAt((triple >>> 12) & 63)); + if (group.length === 2) { + emit(base64Alphabet.charCodeAt((triple >>> 6) & 63)); + emit(61); + } else { + emit(61); + emit(61); + } + } + if (wrap && column) output[outputLength++] = 10; + flush(); +} + +export function basencSelectedMode(args) { + const modes = new Set(["base64", "base64url", "base32", "base32hex", "base16", "base2msbf", "base2lsbf", "z85", "base58"]); + let mode = null; + for (const arg of args) { + if (!arg.startsWith("--")) continue; + const name = arg.slice(2).split("=", 1)[0]; + if (modes.has(name)) mode = name; + } + return mode; +} + +export function decodeBase16(text, ignoreGarbage = false) { + let cleaned = text.replace(/\s+/g, ""); + if (ignoreGarbage) cleaned = cleaned.replace(/[^0-9a-fA-F]/g, ""); + const invalid = cleaned.search(/[^0-9a-fA-F]/); + const usableLength = invalid === -1 ? cleaned.length : invalid; + const evenLength = usableLength - (usableLength % 2); + if (invalid !== -1 || usableLength % 2 !== 0) { + if (evenLength > 0) stdout(Buffer.from(cleaned.slice(0, evenLength), "hex")); + throw new UsageError("invalid input"); + } + return Buffer.from(cleaned, "hex"); +} + +export function base32ToBase32Hex(text) { + const from = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; + const to = "0123456789ABCDEFGHIJKLMNOPQRSTUV"; + return [...text].map((ch) => ch === "=" ? ch : to[from.indexOf(ch)]).join(""); +} + +export function base32HexToBase32(text) { + const from = "0123456789ABCDEFGHIJKLMNOPQRSTUV"; + const to = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; + return [...text].map((ch) => ch === "=" ? ch : to[from.indexOf(ch)] ?? ch).join(""); +} + +export function encodeBase2(data, leastSignificantFirst = false) { + let out = ""; + for (const byte of data) { + for (let bit = 0; bit < 8; bit++) { + const shift = leastSignificantFirst ? bit : 7 - bit; + out += (byte >>> shift) & 1 ? "1" : "0"; + } + } + return out; +} + +export function decodeBase2(text, ignoreGarbage = false, leastSignificantFirst = false) { + const cleaned = ignoreGarbage ? text.replace(/[^01]/g, "") : text; + const out = []; + let value = 0; + let bits = 0; + for (const ch of cleaned) { + if (ch !== "0" && ch !== "1") { + if (out.length) stdout(Uint8Array.from(out)); + throw new UsageError("invalid input"); + } + const bit = ch === "1" ? 1 : 0; + value |= bit << (leastSignificantFirst ? bits : 7 - bits); + bits++; + if (bits === 8) { + out.push(value); + value = 0; + bits = 0; + } + } + if (bits !== 0) { + if (out.length) stdout(Uint8Array.from(out)); + throw new UsageError("invalid input"); + } + return Uint8Array.from(out); +} + +export const BASE58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; + +export function base58Encode(data) { + let zeros = 0; + while (zeros < data.length && data[zeros] === 0) zeros++; + if (zeros === data.length) return "1".repeat(zeros); + let n = 0n; + for (const byte of data) n = (n << 8n) + BigInt(byte); + let out = ""; + while (n > 0n) { + const rem = Number(n % 58n); + out = BASE58_ALPHABET[rem] + out; + n /= 58n; + } + return "1".repeat(zeros) + out; +} + +export function base58Decode(text, ignoreGarbage = false) { + let cleaned = text.replace(/\n/g, ""); + if (ignoreGarbage) cleaned = [...cleaned].filter((ch) => BASE58_ALPHABET.includes(ch)).join(""); + const invalid = [...cleaned].find((ch) => !BASE58_ALPHABET.includes(ch)); + if (invalid !== undefined) throw new UsageError("invalid input"); + let zeros = 0; + while (zeros < cleaned.length && cleaned[zeros] === "1") zeros++; + if (zeros === cleaned.length) return Buffer.alloc(zeros); + let n = 0n; + for (const ch of cleaned) n = n * 58n + BigInt(BASE58_ALPHABET.indexOf(ch)); + const bytes = []; + while (n > 0n) { + bytes.unshift(Number(n & 255n)); + n >>= 8n; + } + return Buffer.concat([Buffer.alloc(zeros), Buffer.from(bytes)]); +} + +export const Z85_ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#"; + +export function z85Encode(data) { + if (data.length % 4 !== 0) throw new UsageError("invalid input (length must be multiple of 4 characters)"); + let out = ""; + for (let i = 0; i < data.length; i += 4) { + let value = ((data[i] * 256 + data[i + 1]) * 256 + data[i + 2]) * 256 + data[i + 3]; + const chars = Array(5); + for (let j = 4; j >= 0; j--) { + chars[j] = Z85_ALPHABET[value % 85]; + value = Math.floor(value / 85); + } + out += chars.join(""); + } + return out; +} + +export function z85Decode(text, ignoreGarbage = false) { + let cleaned = text.replace(/\n/g, ""); + if (ignoreGarbage) cleaned = [...cleaned].filter((ch) => Z85_ALPHABET.includes(ch)).join(""); + const out = []; + for (let i = 0; i < cleaned.length; i += 5) { + const chunk = cleaned.slice(i, i + 5); + if (chunk.length < 5) { + if (out.length) stdout(Uint8Array.from(out)); + throw new UsageError("invalid input"); + } + let value = 0; + for (const ch of chunk) { + const digit = Z85_ALPHABET.indexOf(ch); + if (digit === -1) { + if (out.length) stdout(Uint8Array.from(out)); + throw new UsageError("invalid input"); + } + value = value * 85 + digit; + } + if (value > 0xffffffff) { + if (out.length) stdout(Uint8Array.from(out)); + throw new UsageError("invalid input"); + } + out.push((value >>> 24) & 255, (value >>> 16) & 255, (value >>> 8) & 255, value & 255); + } + return Uint8Array.from(out); +} + +const singleCall = defineCommand("basenc", basenc, basencMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/cat.js b/src/commands/cat.js new file mode 100644 index 0000000..e1ddd7d --- /dev/null +++ b/src/commands/cat.js @@ -0,0 +1,236 @@ +#!/usr/bin/env bun + +import { closeSync, openSync, readSync } from "node:fs"; +import { stat } from "node:fs/promises"; +import { SEEK_CUR, fdStat, isWriteError, libc, normalizeLongOptionByPrefix, normalizeLongOptionsByPrefix, parseOptions, readAll, systemErrorMessage, textInputDiagnosticName } from "../shared/common.js"; +import { fail, stderr, stdout } from "../shared/diagnostics.js"; +import { concatOutputParts, decodeValidUtf8, isSingleByteLocale } from "../shared/text.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const F_GETFL = 3; + +export const O_APPEND = 0o2000; + +export const CAT_LONG_OPTIONS = ["number-nonblank", "number", "squeeze-blank", "show-nonprinting", "show-ends", "show-tabs", "show-all", "help", "version"]; + +export function catMetaOption(args) { + const shortKnownOptions = new Set(["n", "b", "s", "e", "t", "u", "v", "E", "T", "A"]); + for (const arg of args) { + if (arg === "--") return null; + if (arg.startsWith("--")) { + const option = normalizeCatLongOption(arg); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (!CAT_LONG_OPTIONS.includes(name)) return null; + if ((option === "--help" || option === "--version") && inlineValue == null) return option; + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + for (let j = 1; j < arg.length; j++) if (!shortKnownOptions.has(arg[j])) return null; + } + return null; +} + +export async function cat(args) { + args = normalizeCatLongOptions(args); + const { opts, operands } = parseOptions(args, { short: { n: false, b: false, s: false, e: false, t: false, u: false, v: false, E: false, T: false, A: false }, long: { "number": false, "number-nonblank": false, "squeeze-blank": false, "show-ends": false, "show-tabs": false, "show-nonprinting": false, "show-all": false, help: false, version: false } }); + const files = operands.length ? operands : ["-"]; + const selfError = await catSelfOutputError(files); + if (selfError) return selfError; + const needsProcessing = opts.n || opts.number || opts.b || opts["number-nonblank"] || opts.s || opts["squeeze-blank"] || hasCatRendering(opts); + const needsLineState = opts.n || opts.number || opts.b || opts["number-nonblank"] || opts.s || opts["squeeze-blank"]; + if (files.length === 1 && files[0] === "-" && !needsLineState && hasCatRendering(opts)) { + streamRenderedCatStdin(opts); + return 0; + } + if (!needsProcessing) { + let failed = false; + for (const file of files) { + try { + streamCatFile(file); + } catch (error) { + if (isWriteError(error)) throw error; + stderr(`cat: ${textInputDiagnosticName(file)}: ${systemErrorMessage(error)}\n`); + failed = true; + } + } + return failed ? 1 : 0; + } + let line = 1; + let previousBlank = false; + const out = []; + let failed = false; + const inputChunks = []; + for (const file of files) { + let bytes; + try { + // Bun.file('.').arrayBuffer() currently treats a directory as an empty + // file on some platforms. cat must instead report the POSIX read + // failure, just as it does for every other directory operand. + if (file !== "-" && (await stat(file)).isDirectory()) { + const error = new Error("Is a directory"); + error.code = "EISDIR"; + throw error; + } + bytes = await readAll(file); + } catch (error) { + stderr(`cat: ${textInputDiagnosticName(file)}: ${systemErrorMessage(error)}\n`); + failed = true; + continue; + } + if (!needsProcessing) { + out.push(bytes); + continue; + } + inputChunks.push(Buffer.from(bytes)); + } + if (needsProcessing && inputChunks.length) { + const bytes = Buffer.concat(inputChunks); + const decoded = isSingleByteLocale() ? null : decodeValidUtf8(bytes); + const rawMode = decoded == null; + const text = rawMode ? Buffer.from(bytes).toString("latin1") : decoded; + const parts = text.split(/(?<=\n)/); + for (const part of parts) { + if (part === "") continue; + const blank = part === "\n"; + if ((opts.s || opts["squeeze-blank"]) && blank && previousBlank) continue; + previousBlank = blank; + const numberNonblank = opts.b || opts["number-nonblank"]; + const numberAll = !numberNonblank && (opts.n || opts.number); + if ((numberAll || (numberNonblank && !blank))) out.push(`${String(line++).padStart(6)}\t`); + const rendered = renderCat(part, opts); + out.push(rawMode ? Buffer.from(rendered, "latin1") : rendered); + } + } + stdout(concatOutputParts(out)); + return failed ? 1 : 0; +} + +export function streamCatFile(path) { + const fd = path === "-" ? 0 : openSync(path, "r"); + const buffer = Buffer.allocUnsafe(64 * 1024); + try { + while (true) { + const n = readSync(fd, buffer, 0, buffer.length, null); + if (n === 0) break; + stdout(buffer.subarray(0, n)); + } + } finally { + if (fd !== 0) closeSync(fd); + } +} + +export async function catSelfOutputError(files) { + const out = fdStat(1); + if (!out) return 0; + const inStat = fdStat(0); + const append = (libc.symbols.fcntl(1, F_GETFL) & O_APPEND) !== 0; + if (append && inStat && sameFileStat(out, inStat) && fdPosition(0) < inStat.size) return fail("cat", "input file is output file"); + for (let i = 0; i < files.length; i++) { + if (files[i] === "-") continue; + const s = await stat(files[i]).catch(() => null); + if (!s || !sameFileStat(out, s)) continue; + if (append || i > 0) return fail("cat", `${files[i]}: input file is output file`); + } + return 0; +} + +export function normalizeCatLongOptions(args) { + return normalizeLongOptionsByPrefix(args, CAT_LONG_OPTIONS); +} + +export function normalizeCatLongOption(arg) { + return normalizeLongOptionByPrefix(arg, CAT_LONG_OPTIONS); +} + +export function sameFileStat(left, right) { + return left.dev === right.dev && left.ino === right.ino; +} + +export function fdPosition(fd) { + const position = libc.symbols.lseek(fd, 0n, SEEK_CUR); + return position < 0 ? 0 : Number(position); +} + +export function streamRenderedCatStdin(opts) { + const decoder = new TextDecoder(); + const state = { pendingCR: false }; + const buffer = Buffer.alloc(1); + let out = ""; + while (true) { + const n = readSync(0, buffer, 0, 1, null); + if (n === 0) break; + out += renderCatStreamText(decoder.decode(buffer.subarray(0, n), { stream: true }), opts, state); + if (out.includes("\n")) { + stdout(out); + out = ""; + } + } + out += renderCatStreamText(decoder.decode(), opts, state); + if (state.pendingCR) out += renderCatChar("\r", opts); + if (out) stdout(out); +} + +export function hasCatRendering(opts) { + return Boolean(opts.A || opts["show-all"] || opts.e || opts.E || opts["show-ends"] || opts.t || opts.T || opts["show-tabs"] || opts.v || opts["show-nonprinting"]); +} + +export function renderCatStreamText(text, opts, state) { + let out = ""; + const showAll = opts.A || opts["show-all"]; + const showEnds = showAll || opts.e || opts.E || opts["show-ends"]; + for (const ch of text) { + if (state.pendingCR) { + if (showEnds && ch === "\n") { + out += "^M$\n"; + state.pendingCR = false; + continue; + } + out += renderCatChar("\r", opts); + state.pendingCR = false; + } + if (ch === "\r") state.pendingCR = true; + else out += renderCatChar(ch, opts); + } + return out; +} + +export function renderCat(text, opts) { + const showAll = opts.A || opts["show-all"]; + const showEnds = showAll || opts.e || opts.E || opts["show-ends"]; + const showTabs = showAll || opts.t || opts.T || opts["show-tabs"]; + const showNonprinting = showAll || opts.e || opts.t || opts.v || opts["show-nonprinting"]; + let out = ""; + for (let i = 0; i < text.length; i++) { + const ch = text[i]; + if (ch === "\n") out += showEnds ? "$\n" : "\n"; + else if (showEnds && ch === "\r" && text[i + 1] === "\n") out += "^M"; + else out += renderCatChar(ch, opts); + } + return out; +} + +export function renderCatChar(ch, opts) { + const showAll = opts.A || opts["show-all"]; + const showTabs = showAll || opts.t || opts.T || opts["show-tabs"]; + const showNonprinting = showAll || opts.e || opts.t || opts.v || opts["show-nonprinting"]; + if (ch === "\n") return (showAll || opts.e || opts.E || opts["show-ends"]) ? "$\n" : "\n"; + if (ch === "\t") return showTabs ? "^I" : "\t"; + return showNonprinting ? renderNonprinting(ch.codePointAt(0)) : ch; +} + +export function renderNonprinting(code) { + if (code < 32) return `^${String.fromCharCode(code + 64)}`; + if (code === 127) return "^?"; + if (code >= 128 && code < 256) { + const low = code - 128; + if (low < 32) return `M-^${String.fromCharCode(low + 64)}`; + if (low === 127) return "M-^?"; + return `M-${String.fromCharCode(low)}`; + } + return String.fromCodePoint(code); +} + +const singleCall = defineCommand("cat", cat, catMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/chcon.js b/src/commands/chcon.js new file mode 100644 index 0000000..ca1db65 --- /dev/null +++ b/src/commands/chcon.js @@ -0,0 +1,117 @@ +#!/usr/bin/env bun + +import { access, lstat } from "node:fs/promises"; +import { invalidOptionMessage, normalizeLongOptionByPrefix, parseOptions, pathLikeJoin, readdirPathEntries } from "../shared/common.js"; +import { UsageError, stdout } from "../shared/diagnostics.js"; +import { selinuxSecurityContext, setSelinuxSecurityContext } from "../shared/filesystem.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const CHCON_LONG_OPTIONS = ["dereference", "no-dereference", "no-preserve-root", "preserve-root", "recursive", "reference", "user", "role", "type", "range", "verbose", "help", "version"]; + +export function chconMetaOption(args) { + const longValueOptions = new Set(["reference", "user", "role", "type", "range"]); + const longFlagOptions = new Set(["dereference", "no-dereference", "no-preserve-root", "preserve-root", "recursive", "verbose"]); + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + let normalized = arg; + if (arg.startsWith("--")) normalized = normalizeLongOptionByPrefix(arg, CHCON_LONG_OPTIONS); + const [name, inlineValue] = normalized.startsWith("--") ? normalized.slice(2).split(/=(.*)/s, 2) : [null, undefined]; + if (name === "help" || name === "version") { + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + return normalized; + } + if (longFlagOptions.has(name)) { + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + continue; + } + if (longValueOptions.has(name)) { + if (inlineValue === undefined) i++; + continue; + } + if (["-u", "-r", "-t", "-l"].includes(arg)) { + i++; + continue; + } + if (/^-[urtl].+/.test(arg)) continue; + if (/^-[Rhv]+$/.test(arg)) continue; + if (arg.startsWith("-")) throw new UsageError(invalidOptionMessage(arg), true); + continue; + } + return null; +} + +export async function chconCmd(args) { + const { opts, operands } = parseOptions(normalizeChconArgs(args), { + short: { R: false, h: false, u: "value", r: "value", t: "value", l: "value", v: false }, + long: { recursive: false, dereference: false, "no-dereference": false, "no-preserve-root": false, "preserve-root": false, reference: "value", user: "value", role: "value", type: "value", range: "value", verbose: false }, + }); + const reference = opts.reference; + const componentChange = opts.u != null || opts.user != null || opts.r != null || opts.role != null || opts.t != null || opts.type != null || opts.l != null || opts.range != null; + if (reference !== undefined && componentChange) throw new UsageError("conflicting security context specifiers given", true); + let context = null; + if (reference !== undefined) { + if (!operands.length) throw new UsageError("missing operand", true); + await access(reference); + context = selinuxSecurityContext(reference, opts.h || opts["no-dereference"]); + if (context == null) throw new UsageError(`failed to get security context of '${reference}'`); + for (const file of operands) await chconAccessPath(file, opts, context); + return 0; + } + const files = componentChange ? operands : operands.slice(1); + if (files.length < 1) throw new UsageError("missing operand", true); + if (!componentChange) context = operands[0]; + for (const file of files) await chconAccessPath(file, opts, context); + return 0; +} + +export function normalizeChconArgs(args) { + const out = []; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") { + out.push(...args.slice(i)); + break; + } + if (arg.startsWith("--")) { + const normalized = normalizeLongOptionByPrefix(arg, CHCON_LONG_OPTIONS); + out.push(normalized); + if (["--reference", "--user", "--role", "--type", "--range"].includes(normalized) && i + 1 < args.length) out.push(args[++i]); + continue; + } + out.push(arg); + if (["-u", "-r", "-t", "-l"].includes(arg) && i + 1 < args.length) out.push(args[++i]); + } + return out; +} + +export async function chconAccessPath(path, opts, context = null) { + const s = await lstat(path); + if ((opts.R || opts.recursive) && s.isDirectory() && !s.isSymbolicLink()) { + for (const entry of await readdirPathEntries(path)) await chconAccessPath(pathLikeJoin(path, entry), opts, context); + } + const noDereference = opts.h || opts["no-dereference"]; + const current = selinuxSecurityContext(path, noDereference); + if (opts.v || opts.verbose) stdout(`changing security context of '${path}'\n`); + if (context == null && current == null) throw new UsageError(`can't apply partial context to unlabeled file '${path}'`); + const target = context ?? chconUpdatedContext(current, opts); + if (target !== current) { + if (!setSelinuxSecurityContext(path, target, noDereference)) throw new UsageError(`failed to change context of '${path}' to '${target}'`); + } +} + +export function chconUpdatedContext(current, opts) { + const parts = String(current).split(":"); + if (parts.length < 3) return current; + const range = parts.slice(3).join(":"); + const user = opts.u ?? opts.user ?? parts[0]; + const role = opts.r ?? opts.role ?? parts[1]; + const type = opts.t ?? opts.type ?? parts[2]; + const newRange = opts.l ?? opts.range ?? range; + return [user, role, type, newRange].filter((part, index) => index < 3 || part !== "").join(":"); +} + +const singleCall = defineCommand("chcon", chconCmd, chconMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/chgrp.js b/src/commands/chgrp.js new file mode 100644 index 0000000..0c5be09 --- /dev/null +++ b/src/commands/chgrp.js @@ -0,0 +1,57 @@ +#!/usr/bin/env bun + +import { groupName, parseOptions } from "../shared/common.js"; +import { UsageError, VERSION, stderr, stdout } from "../shared/diagnostics.js"; +import { referenceStat, resolveGroup } from "../shared/filesystem.js"; +import { showGenericHelp } from "../shared/help.js"; +import { chgrpErrorLine, chownFailureVerboseLine, chownMetaOption, chownPath, normalizeChownArgs, resolveChownOwnerGroupSpec } from "../shared/ownership.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export async function chgrpCmd(args) { + const { opts, operands } = parseOptions(normalizeChownArgs(args), { short: { R: false, c: false, f: false, h: false, H: false, L: false, P: false, v: false }, long: { changes: false, recursive: false, silent: false, quiet: false, dereference: false, "no-dereference": false, from: "value", reference: "value", "preserve-root": false, "no-preserve-root": false, verbose: false, help: false, version: false } }); + let fromUid = null; + let fromGid = null; + if (opts.from != null) { + // A bare --from value uses the same user-like parser as chown (and its + // diagnostics), but denotes that numeric ID as the current group. With + // OWNER:GROUP syntax, GNU uses the group portion. + const from = await resolveChownOwnerGroupSpec(opts.from, { warnDot: true, warningCommand: "chgrp" }); + fromGid = from.gid ?? from.uid; + } + if (opts.help) { + showGenericHelp("chgrp"); + return 0; + } + if (opts.version) { + stdout(`${VERSION}\n`); + return 0; + } + if (opts.reference != null && operands.length < 1) throw new UsageError("missing operand", true); + if (opts.reference == null && operands.length < 1) throw new UsageError("missing operand", true); + if (opts.reference == null && operands.length < 2) throw new UsageError(`missing operand after '${operands[0]}'`, true); + const files = opts.reference != null ? operands : operands.slice(1); + const gid = opts.reference != null ? (await referenceStat(opts.reference)).gid : await resolveGroup(operands[0]); + let failed = false; + const dereference = !(opts.h || opts["no-dereference"]) || opts.dereference; + const traversal = opts.L ? "L" : opts.H ? "H" : "P"; + const preserveRoot = !!opts["preserve-root"] && !opts["no-preserve-root"]; + const changes = opts.c || opts.changes; + const verbose = opts.v || opts.verbose; + const failureSpec = opts.reference != null ? await groupName(gid) : operands[0]; + for (const file of files) { + try { + const ok = await chownPath(file, null, gid, opts.R || opts.recursive, { dereference, traversal, preserveRoot, command: "chgrp", fromUid, fromGid, changes, verbose, silent: opts.f || opts.silent || opts.quiet, reportUid: false, reportGid: gid != null, failureKind: "group", failureSpec }, true, file); + if (!ok) failed = true; + } catch (error) { + failed = true; + if (verbose) stdout(chownFailureVerboseLine(file, { failureKind: "group", failureSpec })); + if (!(opts.f || opts.silent || opts.quiet)) stderr(chgrpErrorLine(file, error)); + } + } + return failed ? 1 : 0; +} + +const singleCall = defineCommand("chgrp", chgrpCmd, chownMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/chmod.js b/src/commands/chmod.js new file mode 100644 index 0000000..0311529 --- /dev/null +++ b/src/commands/chmod.js @@ -0,0 +1,264 @@ +#!/usr/bin/env bun + +import { lstat, stat } from "node:fs/promises"; +import { changeDirectory, invalidOptionMessage, localeQuotedEscapedDiagnostic, normalizeLongOptionByPrefix, pathDisplayName, pathLikeJoin, readdirPathEntries } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { chmodQuotedName, errnoMessage, invalidClusterOptionMessage, isAccessError, isRecursiveRootTarget, openDirectoryPathEntries, parseModeSpec, preserveRootError, preserveRootMessage, processCwdOrNull, referenceStat, setFileMode } from "../shared/filesystem.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const CHMOD_LONG_OPTIONS = ["changes", "recursive", "silent", "quiet", "dereference", "no-dereference", "reference", "preserve-root", "no-preserve-root", "verbose", "help", "version"]; + +export function chmodMetaOption(args) { + const longFlagOptions = new Set(["changes", "recursive", "silent", "quiet", "dereference", "no-dereference", "preserve-root", "no-preserve-root", "verbose"]); + const shortFlagOptions = new Set(["R", "c", "f", "h", "H", "L", "P", "v"]); + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + let normalized = arg; + if (arg.startsWith("--")) normalized = normalizeLongOptionByPrefix(arg, CHMOD_LONG_OPTIONS); + const [name, inlineValue] = normalized.startsWith("--") ? normalized.slice(2).split(/=(.*)/s, 2) : [null, undefined]; + if (name === "help" || name === "version") { + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + return normalized; + } + if (longFlagOptions.has(name)) { + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + continue; + } + if (name === "reference") { + if (inlineValue === undefined) i++; + continue; + } + if (arg.startsWith("-") && arg !== "-" && !isChmodModeToken(arg)) { + if ([...arg.slice(1)].every((ch) => shortFlagOptions.has(ch))) continue; + throw new UsageError(invalidClusterOptionMessage(arg, shortFlagOptions), true); + } + continue; + } + return null; +} + +export async function chmodCmd(args) { + args = normalizeChmodArgs(args); + const { recursive, changes, verbose, silent, dereference, traversal, preserveRoot, modeSpec, reference, files } = parseChmodArgs(args); + if (!modeSpec) throw new UsageError("missing operand", true); + if (reference != null && !files.length) throw new UsageError("missing operand", true); + if (!files.length) throw new UsageError(`missing operand after '${modeSpec}'`, true); + if (reference == null && !isChmodModeToken(modeSpec)) throw new UsageError(`invalid mode: ${localeQuotedEscapedDiagnostic(modeSpec)}`, true); + const referenceMode = reference == null ? null : (await referenceStat(reference)).mode & 0o7777; + let failed = false; + for (const file of files) { + const ok = await chmodPath(file, modeSpec, { recursive, changes, verbose, silent, dereference, traversal, preserveRoot, referenceMode }, true, file); + if (!ok) failed = true; + } + return failed ? 1 : 0; +} + +export async function chmodPath(path, modeSpec, options, isRoot = false, display = path, relativeToChangedCwd = false) { + let ok = true; + let s; + try { + const linkInfo = await lstat(path); + const isLink = linkInfo.isSymbolicLink(); + const shouldDereference = isLink && (options.recursive + ? options.traversal === "L" || (isRoot && options.traversal === "H") + : options.dereference); + if (isLink && !shouldDereference) return true; + try { + s = shouldDereference || !isLink ? await stat(path) : linkInfo; + } catch (error) { + error.danglingSymlink = isLink && error?.code === "ENOENT"; + throw error; + } + if (options.recursive && options.preserveRoot && s.isDirectory() && await isRecursiveRootTarget(path)) throw preserveRootError("chmod", path); + const before = s.mode & 0o7777; + const { mode: after, ok: modeOk } = options.referenceMode == null ? parseModeSpec(modeSpec, before, s.isDirectory()) : { mode: options.referenceMode, ok: true }; + await setFileMode(path, after); + if ((options.changes || options.verbose) && before !== after) stdout(`mode of ${chmodQuotedName(display)} changed from ${octalMode(before)} (${modeBitsString(before)}) to ${octalMode(after)} (${modeBitsString(after)})\n`); + else if (options.verbose) stdout(`mode of ${chmodQuotedName(display)} retained as ${octalMode(after)} (${modeBitsString(after)})\n`); + if (!modeOk) { + if (!options.silent) stderr(`chmod: ${chmodQuotedName(display)}: new permissions are ${modeBitsString(after)}, not ${modeBitsString(parseModeSpec(modeSpec, before, s.isDirectory(), { ignoreUmask: true }).mode)}\n`); + ok = false; + } + } catch (error) { + if (!options.silent) stderr(chmodErrorLine(display, error)); + return false; + } + if (options.recursive && s.isDirectory()) { + try { + if (isRoot) { + const entries = await openDirectoryPathEntries(path); + for await (const entry of entries) { + const childDisplay = `${String(display).replace(/\/+$/, "")}/${pathDisplayName(entry)}`; + if (!(await chmodPath(pathLikeJoin(path, entry), modeSpec, options, false, childDisplay, relativeToChangedCwd))) ok = false; + } + return ok; + } + const entries = await readdirPathEntries(path); + const restoreCwd = relativeToChangedCwd ? null : processCwdOrNull(); + if (chmodCanRelativeTraverse(path)) { + changeDirectory(path); + try { + for (const entry of entries) { + const childDisplay = `${String(display).replace(/\/+$/, "")}/${pathDisplayName(entry)}`; + if (!(await chmodPath(entry, modeSpec, options, false, childDisplay, true))) ok = false; + } + } finally { + changeDirectory(restoreCwd ?? ".."); + } + } else { + for (const entry of entries) { + const childDisplay = `${String(display).replace(/\/+$/, "")}/${pathDisplayName(entry)}`; + if (!(await chmodPath(pathLikeJoin(path, entry), modeSpec, options, false, childDisplay))) ok = false; + } + } + } catch (error) { + if (!options.silent) stderr(chmodErrorLine(display, error)); + ok = false; + } + } + return ok; +} + +export function chmodCanRelativeTraverse(path) { + const text = pathDisplayName(path); + return !text.includes("\uFFFD") && text !== "" && text !== "." && text !== ".."; +} + +export function normalizeChmodArgs(args) { + const out = []; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") { + out.push(...args.slice(i)); + break; + } + if (arg.startsWith("--")) { + const normalized = normalizeLongOptionByPrefix(arg, CHMOD_LONG_OPTIONS); + if (normalized === "--reference" && i + 1 < args.length) { + out.push(`${normalized}=${args[++i]}`); + } else { + out.push(normalized); + } + continue; + } + out.push(arg); + } + return out; +} + +export function parseChmodArgs(args) { + let recursive = false; + let changes = false; + let verbose = false; + let silent = false; + let dereference = true; + let traversal = "H"; + let preserveRoot = false; + let reference = null; + let explicitOperandStart = false; + const rest = []; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (!explicitOperandStart && rest.length === 0 && arg === "--") { + explicitOperandStart = true; + rest.push(...args.slice(i + 1)); + break; + } + if (!explicitOperandStart && (arg === "--reference" || arg.startsWith("--reference="))) { + if (arg === "--reference") { + if (i + 1 >= args.length) throw new UsageError("option '--reference' requires an argument", true); + reference = args[++i]; + } else { + reference = arg.slice("--reference=".length); + } + continue; + } + if (!explicitOperandStart && isChmodOption(arg)) { + for (const option of expandChmodOption(arg)) { + if (option === "R" || option === "recursive") recursive = true; + else if (option === "c" || option === "changes") changes = true; + else if (option === "v" || option === "verbose") verbose = true; + else if (option === "f" || option === "silent" || option === "quiet") silent = true; + else if (option === "H" || option === "L" || option === "P") traversal = option; + else if (option === "h" || option === "no-dereference") dereference = false; + else if (option === "dereference" || option === "deref") dereference = true; + else if (option === "preserve-root") preserveRoot = true; + else if (option === "no-preserve-root") preserveRoot = false; + } + continue; + } + if (!explicitOperandStart && rest.length === 0 && arg.startsWith("-") && !isChmodModeToken(arg)) { + throw new UsageError(invalidOptionMessage(arg), true); + } + rest.push(arg); + } + if (explicitOperandStart) { + const [modeSpec, ...files] = rest; + return { recursive, changes, verbose, silent, dereference, traversal, preserveRoot, modeSpec: reference == null ? modeSpec : "--reference", reference, files: reference == null ? files : rest }; + } + + const modeParts = []; + const files = []; + let afterFile = false; + for (let i = 0; i < rest.length; i++) { + const arg = rest[i]; + if (arg === "--" && modeParts.length) { + files.push(...rest.slice(i + 1)); + break; + } + if (arg === "--" && afterFile) { + files.push(arg); + continue; + } + if (!afterFile && isChmodModeToken(arg)) modeParts.push(arg); + else if (isChmodModeToken(arg)) modeParts.push(arg); + else { + files.push(arg); + afterFile = true; + } + } + if (reference != null) return { recursive, changes, verbose, silent, dereference, traversal, preserveRoot, modeSpec: "--reference", reference, files: rest }; + if (!modeParts.length && rest.length) return { recursive, changes, verbose, silent, dereference, traversal, preserveRoot, modeSpec: rest[0], files: rest.slice(1) }; + return { recursive, changes, verbose, silent, dereference, traversal, preserveRoot, modeSpec: modeParts.join(","), files }; +} + +export function isChmodOption(arg) { + if (["--recursive", "--changes", "--silent", "--quiet", "--dereference", "--no-dereference", "--preserve-root", "--no-preserve-root", "--verbose"].includes(arg)) return true; + return /^-[RcfvHLP]+$/.test(arg) || arg === "-h"; +} + +export function expandChmodOption(arg) { + if (arg.startsWith("--")) return [arg.slice(2)]; + return arg.slice(1).split(""); +} + +export function isChmodModeToken(arg) { + if (arg === "--") return true; + return arg.split(",").every((part) => /^[0-7]+$/.test(part) || /^[+=-][0-7]+$/.test(part) || /^[ugoa]*[+=-][rwxXstugo]*$/.test(part)); +} + +export function octalMode(mode) { + return mode.toString(8).padStart(4, "0"); +} + +export function modeBitsString(mode) { + let out = ""; + const chars = ["r", "w", "x"]; + for (let shift = 6; shift >= 0; shift -= 3) { + for (let bit = 2; bit >= 0; bit--) out += mode & (1 << (shift + bit)) ? chars[2 - bit] : "-"; + } + return out; +} + +export function chmodErrorLine(path, error) { + if (error?.preserveRoot) return preserveRootMessage("chmod", error.path, error.rootPath); + if (error?.danglingSymlink) return `chmod: cannot operate on dangling symlink ${chmodQuotedName(path)}\n`; + if (isAccessError(error)) return `chmod: cannot access ${chmodQuotedName(path)}: ${errnoMessage(error)}\n`; + return `chmod: changing permissions of ${chmodQuotedName(path)}: ${errnoMessage(error)}\n`; +} + +const singleCall = defineCommand("chmod", chmodCmd, chmodMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/chown.js b/src/commands/chown.js new file mode 100644 index 0000000..9ffb737 --- /dev/null +++ b/src/commands/chown.js @@ -0,0 +1,62 @@ +#!/usr/bin/env bun + +import { parseOptions } from "../shared/common.js"; +import { UsageError, VERSION, stderr, stdout } from "../shared/diagnostics.js"; +import { referenceStat } from "../shared/filesystem.js"; +import { showGenericHelp } from "../shared/help.js"; +import { chownErrorLine, chownFailureVerboseLine, chownMetaOption, chownOwnershipDisplay, chownPath, normalizeChownArgs, parseChownSpec, resolveChownOwnerGroupSpec } from "../shared/ownership.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export async function chownCmd(args) { + const { opts, operands } = parseOptions(normalizeChownArgs(args), { short: { R: false, c: false, f: false, h: false, H: false, L: false, P: false, v: false }, long: { changes: false, recursive: false, silent: false, quiet: false, dereference: false, "no-dereference": false, from: "value", reference: "value", "preserve-root": false, "no-preserve-root": false, verbose: false, help: false, version: false } }); + const from = opts.from == null ? null : await resolveChownOwnerGroupSpec(opts.from); + if (opts.help) { + showGenericHelp("chown"); + return 0; + } + if (opts.version) { + stdout(`${VERSION}\n`); + return 0; + } + if (opts.reference != null && operands.length < 1) throw new UsageError("missing operand", true); + if (opts.reference == null && operands.length < 1) throw new UsageError("missing operand", true); + if (opts.reference == null && operands.length < 2) throw new UsageError(`missing operand after '${operands[0]}'`, true); + const files = opts.reference != null ? operands : operands.slice(1); + let uid; + let gid; + if (opts.reference != null) { + const ref = await referenceStat(opts.reference); + uid = ref.uid; + gid = ref.gid; + } else { + ({ uid, gid } = await resolveChownOwnerGroupSpec(operands[0], { warnDot: true })); + } + const ownershipSpec = opts.reference != null ? await chownOwnershipDisplay(uid, gid, { reportUid: true, reportGid: true }) : operands[0]; + const parsedOwnershipSpec = opts.reference == null ? parseChownSpec(operands[0]) : null; + const failureKind = uid == null && gid != null ? "group" : "ownership"; + const failureSpec = uid == null && gid == null ? null : failureKind === "group" && parsedOwnershipSpec ? parsedOwnershipSpec.groupPart : ownershipSpec; + const fromUid = from?.uid ?? null; + const fromGid = from?.gid ?? null; + let failed = false; + const dereference = !(opts.h || opts["no-dereference"]) || opts.dereference; + const traversal = opts.L ? "L" : opts.H ? "H" : "P"; + const preserveRoot = !!opts["preserve-root"] && !opts["no-preserve-root"]; + const changes = opts.c || opts.changes; + const verbose = opts.v || opts.verbose; + for (const file of files) { + try { + const ok = await chownPath(file, uid, gid, opts.R || opts.recursive, { dereference, traversal, preserveRoot, command: "chown", fromUid, fromGid, changes, verbose, silent: opts.f || opts.silent || opts.quiet, reportUid: uid != null, reportGid: gid != null, failureKind, failureSpec }, true, file); + if (!ok) failed = true; + } catch (error) { + failed = true; + if (verbose) stdout(chownFailureVerboseLine(file, { failureKind, failureSpec })); + if (!(opts.f || opts.silent || opts.quiet)) stderr(chownErrorLine(file, error)); + } + } + return failed ? 1 : 0; +} + +const singleCall = defineCommand("chown", chownCmd, chownMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/chroot.js b/src/commands/chroot.js new file mode 100644 index 0000000..e6c9472 --- /dev/null +++ b/src/commands/chroot.js @@ -0,0 +1,249 @@ +#!/usr/bin/env bun + +import { ptr } from "bun:ffi"; +import { realpath, stat } from "node:fs/promises"; +import { resolve } from "node:path"; +import { cstrPath, invalidOptionMessage, libc, localeQuotedDiagnostic, orderedGroupIds, pathDisplayName, readGroup, readPasswd, shellEscapeLsName, supplementaryGroupsForUser, systemErrorMessage } from "../shared/common.js"; +import { InvocationError, fail, stderr } from "../shared/diagnostics.js"; +import { commandSpawnErrorMessage, execCommand, isDirectCommandInvocation, isKnownUnexecutableCommand, normalizeInvocationLongOptionByPrefix } from "../shared/process.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const CHROOT_LONG_OPTIONS = ["groups", "userspec", "skip-chdir", "help", "version"]; + +export function chrootMetaOption(args) { + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + let normalized = arg; + if (arg.startsWith("--")) normalized = normalizeInvocationLongOptionByPrefix(arg, CHROOT_LONG_OPTIONS); + const [name, inlineValue] = normalized.startsWith("--") ? normalized.slice(2).split(/=(.*)/s, 2) : [null, undefined]; + if (name === "help" || name === "version") { + if (inlineValue !== undefined) throw new InvocationError(`option '--${name}' doesn't allow an argument`); + return normalized; + } + if (name === "skip-chdir") { + if (inlineValue !== undefined) throw new InvocationError("option '--skip-chdir' doesn't allow an argument"); + continue; + } + if (name === "userspec" || name === "groups") { + if (inlineValue !== undefined) continue; + i++; + continue; + } + if (arg.startsWith("-")) throw new InvocationError(invalidOptionMessage(arg)); + return null; + } + return null; +} + +export async function chrootCmd(args) { + const { opts, operands } = parseChrootArgs(normalizeChrootArgs(args)); + if (!operands.length) throw new InvocationError("missing operand"); + const [root, ...requestedCommand] = operands; + if (opts["skip-chdir"] && !(await chrootTargetIsOldRoot(root))) { + throw new InvocationError("option --skip-chdir only permitted if NEWROOT is old '/'"); + } + const outsideCredentials = await resolveChrootCredentials(opts).catch(() => null); + if (libc.symbols.chroot(cstrPath(root)) !== 0) { + const rootError = await chrootRootError(root) ?? ((process.getuid?.() ?? 1) !== 0 ? "Operation not permitted" : "Operation not permitted"); + return fail("chroot", `cannot change root directory to ${shellEscapeLsName(pathDisplayName(root), true)}: ${rootError}`, 125); + } + if (!opts["skip-chdir"]) { + try { + process.chdir("/"); + } catch (error) { + return fail("chroot", `cannot chdir to root directory: ${systemErrorMessage(error)}`, 125); + } + } + let credentials; + try { + credentials = await resolveChrootCredentials(opts, outsideCredentials); + } catch (error) { + return fail("chroot", error.message || String(error), 125); + } + const credentialError = applyChrootCredentials(credentials); + if (credentialError) return fail("chroot", credentialError, 125); + const command = requestedCommand.length ? requestedCommand : [process.env.SHELL || "/bin/sh", "-i"]; + if (!isDirectCommandInvocation()) { + try { + if (await isKnownUnexecutableCommand(command[0])) throw Object.assign(new Error("Permission denied"), { code: "EACCES" }); + const proc = Bun.spawn(command, { stdin: "inherit", stdout: "inherit", stderr: "inherit" }); + return await proc.exited; + } catch (error) { + const status = error?.code === "ENOENT" || String(error?.message ?? "").includes("not found") ? 127 : 126; + stderr(`chroot: failed to run command '${command[0]}': ${commandSpawnErrorMessage(error)}\n`); + return status; + } + } + const execError = execCommand(command); + const status = execError.errno === 2 ? 127 : 126; + stderr(`chroot: failed to run command '${command[0]}': ${execError.message}\n`); + return status; +} + +export async function resolveChrootCredentials(opts, fallback = null) { + const userspec = opts.userspec; + const groupsSpec = opts.groups; + if (userspec == null && groupsSpec == null) return { uid: null, gid: null, groups: null }; + let uid = null; + let gid = null; + let username = null; + let usedFallbackUser = false; + if (userspec != null) { + const separator = userspec.indexOf(":"); + const userToken = separator < 0 ? userspec : userspec.slice(0, separator); + const groupToken = separator < 0 || separator === userspec.length - 1 ? null : userspec.slice(separator + 1); + if (userToken !== "") { + let user = await chrootUserToken(userToken).catch(() => null); + if (!user && fallback?.user) { + user = fallback.user; + usedFallbackUser = true; + } + if (!user) throw new InvocationError(`invalid user ${localeQuotedDiagnostic(userToken)}`); + uid = user.uid; + username = user.name; + if (groupToken == null) gid = user.gid; + } + if (groupToken != null && groupToken !== "") { + gid = await chrootGroupToken(groupToken).catch(() => null) ?? fallback?.gid ?? null; + if (gid == null) throw new InvocationError(`invalid group ${localeQuotedDiagnostic(groupToken)}`); + } + if (uid != null && gid == null && username == null) throw new InvocationError(`no group specified for unknown uid: ${uid}`); + } + let groups = null; + if (groupsSpec != null) groups = await chrootSupplementaryGroups(groupsSpec, fallback?.groups); + else if (uid != null && username != null) { + groups = usedFallbackUser && fallback?.groups != null + ? fallback.groups + : orderedGroupIds(gid, await supplementaryGroupsForUser(username)); + } + return { uid, gid, groups, user: uid == null ? null : { uid, gid, name: username } }; +} + +export async function chrootUserToken(token) { + const passwd = await readPasswd(); + if (!token.startsWith("+") && passwd.has(token)) return { ...passwd.get(token), name: token }; + const numeric = token.startsWith("+") ? token.slice(1) : token; + if (!/^\d+$/.test(numeric)) return null; + const uid = Number(numeric); + if (!Number.isSafeInteger(uid) || uid < 0 || uid > 0xffffffff) return null; + if (!token.startsWith("+")) { + for (const [name, row] of passwd) if (row.uid === uid) return { ...row, name }; + } + return { uid, gid: null, name: null }; +} + +export async function chrootGroupToken(token) { + const groups = await readGroup(); + if (!token.startsWith("+") && groups.has(token)) return groups.get(token); + const numeric = token.startsWith("+") ? token.slice(1) : token; + if (!/^\d+$/.test(numeric)) return null; + const gid = Number(numeric); + return Number.isSafeInteger(gid) && gid >= 0 && gid <= 0xffffffff ? gid : null; +} + +export async function chrootSupplementaryGroups(spec, fallback = null) { + if (spec === "") return []; + const tokens = spec.split(",").map((token) => token.trim()).filter(Boolean); + if (!tokens.length) throw new InvocationError(`invalid group list ${localeQuotedDiagnostic(spec)}`); + const groups = []; + for (const token of tokens) { + const gid = await chrootGroupToken(token).catch(() => null); + if (gid == null) { + if (fallback != null) return fallback; + throw new InvocationError(`invalid group ${localeQuotedDiagnostic(token)}`); + } + groups.push(gid); + } + return groups; +} + +export function applyChrootCredentials(credentials) { + if (credentials.groups != null || credentials.uid != null) { + const groups = credentials.groups ?? []; + const buffer = Buffer.alloc(groups.length * 4); + groups.forEach((gid, index) => buffer.writeUInt32LE(gid >>> 0, index * 4)); + if (libc.symbols.setgroups(groups.length, groups.length ? ptr(buffer) : null) !== 0) return "failed to set supplemental groups"; + } + if (credentials.gid != null && libc.symbols.setgid(credentials.gid) !== 0) return "failed to set group-ID"; + if (credentials.uid != null && libc.symbols.setuid(credentials.uid) !== 0) return "failed to set user-ID"; + return null; +} + +export async function chrootRootError(root) { + try { + const info = await stat(root); + return info.isDirectory() ? null : "Not a directory"; + } catch (error) { + return systemErrorMessage(error); + } +} + +export async function chrootTargetIsOldRoot(root) { + try { + return await realpath(root) === "/"; + } catch { + return resolve(root) === "/"; + } +} + +export function normalizeChrootArgs(args) { + const out = []; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") { + out.push(...args.slice(i)); + break; + } + if (arg.startsWith("--")) { + const normalized = normalizeInvocationLongOptionByPrefix(arg, CHROOT_LONG_OPTIONS); + out.push(normalized); + if ((normalized === "--groups" || normalized === "--userspec") && i + 1 < args.length) out.push(args[++i]); + continue; + } + out.push(arg); + } + return out; +} + +export function parseChrootArgs(args) { + const opts = {}; + const operands = []; + const requireValue = (index, option) => { + if (index + 1 < args.length) return args[index + 1]; + throw new InvocationError(`option '${option}' requires an argument`); + }; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") { + operands.push(...args.slice(i + 1)); + break; + } + if (arg === "--skip-chdir") { + opts["skip-chdir"] = true; + continue; + } + if (arg === "--userspec" || arg === "--groups") { + opts[arg.slice(2)] = requireValue(i, arg); + i++; + continue; + } + if (arg.startsWith("--userspec=")) { + opts.userspec = arg.slice("--userspec=".length); + continue; + } + if (arg.startsWith("--groups=")) { + opts.groups = arg.slice("--groups=".length); + continue; + } + if (arg.startsWith("-")) throw new InvocationError(invalidOptionMessage(arg)); + operands.push(arg, ...args.slice(i + 1)); + break; + } + return { opts, operands }; +} + +const singleCall = defineCommand("chroot", chrootCmd, chrootMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/cksum.js b/src/commands/cksum.js new file mode 100644 index 0000000..5a2fc85 --- /dev/null +++ b/src/commands/cksum.js @@ -0,0 +1,218 @@ +#!/usr/bin/env bun + +import { forEachInputChunk, sumForFile } from "../shared/checksum.js"; +import { nodeErrorMessage, normalizeLongOptionByPrefix, normalizeLongOptionsByPrefix, parseOptions, systemErrorMessage } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { CKSUM_LONG_OPTIONS, blake2bDigestFile, checkHashes, checksumDiagnosticName, checksumMetaOption, cksumHashAlgorithm, fileDigest, optionAppearsAfter, validateBlake2bLength, validateChecksumCheckOnlyOptions, validateCksumAlgorithmOption } from "../shared/hash.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export function cksumMetaOption(args) { + return checksumMetaOption(args, CKSUM_LONG_OPTIONS, normalizeCksumLongOption, new Set(["algorithm", "length"]), new Set(["a", "l"]), new Set(["a", "b", "c", "l", "t", "w", "z"])); +} + +export function crc32bUpdate(crc, chunk) { + for (const byte of chunk) { + crc ^= byte; + for (let k = 0; k < 8; k++) crc = (crc >>> 1) ^ (0xedb88320 & -(crc & 1)); + } + return crc >>> 0; +} + +export const posixCrcTable = Array.from({ length: 256 }, (_, i) => { + let crc = i << 24; + for (let k = 0; k < 8; k++) crc = (crc & 0x80000000) ? ((crc << 1) ^ 0x04c11db7) : (crc << 1); + return crc >>> 0; +}); + +export function posixCrcUpdate(crc, chunk) { + for (const byte of chunk) crc = ((crc << 8) ^ posixCrcTable[((crc >>> 24) ^ byte) & 0xff]) >>> 0; + return crc >>> 0; +} + +export function posixCrcFinish(crc, length) { + while (length > 0) { + crc = ((crc << 8) ^ posixCrcTable[((crc >>> 24) ^ length) & 0xff]) >>> 0; + length = Math.floor(length / 256); + } + return (~crc) >>> 0; +} + +export async function cksumCrc(file, variant) { + let length = 0; + let crc = variant === "crc32b" ? 0xffffffff : 0; + await forEachInputChunk(file, (chunk) => { + length += chunk.length; + crc = variant === "crc32b" ? crc32bUpdate(crc, chunk) : posixCrcUpdate(crc, chunk); + }); + return { length, crc: variant === "crc32b" ? (crc ^ 0xffffffff) >>> 0 : posixCrcFinish(crc, length) }; +} + +export function uIntBytesBE(value, width) { + const bytes = new Uint8Array(width); + let n = Number(value) >>> 0; + for (let i = width - 1; i >= 0; i--) { + bytes[i] = n & 0xff; + n >>>= 8; + } + return bytes; +} + +export async function cksum(args) { + args = normalizeCksumLongOptions(args); + if (args.length === 1 && args[0] === "--help") { + stdout("Usage: cksum [OPTION]... [FILE]...\n"); + stdout("Print or verify checksums.\nBy default use the 32 bit CRC algorithm.\n\n"); + stdout("Available algorithms:\n sysv\n bsd\n crc\n crc32b\n md5\n sha1\n sha2\n sha3\n blake2b\n sm3\n"); + return 0; + } + const { opts, operands } = parseOptions(args, { short: { a: "value", l: "value", c: false, z: false, b: false, t: false, w: false }, long: { algorithm: "value", length: "value", check: false, base64: false, raw: false, tag: false, untagged: false, zero: false, status: false, quiet: false, warn: false, strict: false, "ignore-missing": false, debug: false, binary: false, text: false, help: false, version: false } }); + if (opts.w) opts.warn = true; + opts.warnAfterStatus = optionAppearsAfter(args, "--warn", "--status") || optionAppearsAfter(args, "-w", "--status"); + const algorithm = opts.a ?? opts.algorithm ?? "crc"; + validateCksumAlgorithmOption(algorithm); + validateChecksumCheckOnlyOptions(opts); + const lengthOption = opts.l ?? opts.length; + if (lengthOption != null && String(lengthOption) !== "0" && !["blake2b", "sha2", "sha3"].includes(algorithm)) { + throw new UsageError("--length is only supported with --algorithm blake2b, sha2, or sha3"); + } + opts.taggedOutput = cksumTaggedOutput(args, algorithm); + opts.binaryOutput = cksumBinaryOutput(args); + if (opts.raw && opts.base64) throw new UsageError("--raw and --base64 are mutually exclusive"); + if (opts.raw && operands.length > 1) throw new UsageError("--raw does not support multiple files"); + if ((opts.t || opts.text) && opts.taggedOutput && algorithm !== "crc" && algorithm !== "crc32b" && algorithm !== "bsd" && algorithm !== "sysv") throw new UsageError("--text mode is only supported with --untagged", true); + if (opts.c || opts.check) { + if (opts.tag && opts.taggedOutput) throw new UsageError("the --tag option is meaningless when verifying checksums", true); + const explicitAlgorithm = opts.a != null || opts.algorithm != null; + if (explicitAlgorithm && ["bsd", "sysv", "crc", "crc32b"].includes(algorithm)) throw new UsageError("--check is not supported with --algorithm={bsd,sysv,crc,crc32b}"); + if (explicitAlgorithm && (opts.l != null || opts.length != null)) cksumHashAlgorithm(algorithm, opts); + return checkHashes(explicitAlgorithm ? algorithm : "auto", operands.length ? operands : ["-"], { ...opts, program: "cksum" }); + } + if (algorithm === "bsd" || algorithm === "sysv") return cksumSum(algorithm, operands.length ? operands : ["-"], opts.raw); + if (algorithm !== "crc" && algorithm !== "crc32b") return cksumDigest(algorithm, opts, operands.length ? operands : ["-"]); + if (opts.debug) stderr("cksum: using generic checksum implementation\n"); + const implicitStdin = operands.length === 0; + const files = implicitStdin ? ["-"] : operands; + let failed = false; + for (const file of files) { + let result; + try { + result = await cksumCrc(file, algorithm); + } catch (error) { + stderr(cksumReadError(file, error)); + failed = true; + continue; + } + const { crc, length } = result; + if (opts.raw) { + stdout(uIntBytesBE(crc, 4)); + continue; + } + stdout(`${crc} ${length}${implicitStdin ? "" : ` ${file}`}\n`); + } + return failed ? 1 : 0; +} + +export function cksumTaggedOutput(args, algorithm) { + let mode = !["crc", "crc32b", "bsd", "sysv"].includes(algorithm); + for (const arg of args) { + if (arg === "--tag") mode = true; + else if (arg === "--untagged") mode = false; + } + return mode; +} + +export function cksumBinaryOutput(args) { + let mode = false; + for (const arg of args) { + if (arg === "--binary") mode = true; + else if (arg === "--text") mode = false; + else if (/^-[^-]/.test(arg)) { + for (let i = 1; i < arg.length; i++) { + const ch = arg[i]; + if (ch === "b") mode = true; + else if (ch === "t") mode = false; + else if (ch === "a" || ch === "l") break; + } + } + } + return mode; +} + +export async function cksumSum(algorithm, files, raw = false) { + const sysv = algorithm === "sysv"; + let failed = false; + for (const file of files) { + let result; + try { + result = await sumForFile(file, sysv); + } catch (error) { + stderr(cksumReadError(file, error)); + failed = true; + continue; + } + const { sum, blocks } = result; + if (raw) { + stdout(uIntBytesBE(sum, 2)); + continue; + } + if (sysv) stdout(`${sum} ${blocks}${file === "-" ? "" : ` ${file}`}\n`); + else stdout(`${String(sum).padStart(5, "0")} ${String(blocks).padStart(5)}${file === "-" ? "" : ` ${file}`}\n`); + } + return failed ? 1 : 0; +} + +export function cksumTag(name, opts = {}) { + const length = opts.l ?? opts.length; + if (name === "blake2b") return length && String(length) !== "0" ? `BLAKE2b-${length}` : "BLAKE2b"; + if (name === "sha3") return `SHA3-${length}`; + const algorithm = cksumHashAlgorithm(name, opts); + return algorithm.toUpperCase(); +} + +export async function cksumDigest(name, opts, files) { + const lengthValue = opts.l ?? opts.length; + const length = lengthValue === "0" ? null : lengthValue ?? (name === "sha2" || name === "sha3" ? "256" : name === "blake2b" ? "512" : null); + if (name === "blake2b" && length != null) validateBlake2bLength("cksum", length); + const algorithm = cksumHashAlgorithm(name, { ...opts, l: length }); + const digestBytes = length == null ? null : Number(length) / 8; + if (length != null && (!Number.isInteger(digestBytes) || digestBytes <= 0)) throw new UsageError(`invalid digest length: ${length}`); + const sep = opts.z || opts.zero ? "\0" : "\n"; + let failed = false; + for (const file of files) { + let full; + try { + full = name === "blake2b" ? await blake2bDigestFile(file, digestBytes ?? 64) : await fileDigest(file, algorithm); + } catch (error) { + stderr(cksumReadError(file, error)); + failed = true; + continue; + } + const digest = digestBytes == null ? full : full.slice(0, digestBytes); + if (opts.raw) { + stdout(digest); + continue; + } + const rendered = opts.base64 ? digest.toString("base64") : digest.toString("hex"); + const nameOut = file === "-" ? "-" : file; + if (!opts.taggedOutput) stdout(`${rendered}${opts.binaryOutput ? " *" : " "}${nameOut}${sep}`); + else stdout(`${cksumTag(name, opts)} (${nameOut}) = ${rendered}${sep}`); + } + return failed ? 1 : 0; +} + +export function cksumReadError(file, error) { + return file === "-" ? `cksum: ${nodeErrorMessage(error)}\n` : `cksum: ${checksumDiagnosticName(file)}: ${systemErrorMessage(error)}\n`; +} + +export function normalizeCksumLongOptions(args) { + return normalizeLongOptionsByPrefix(args, CKSUM_LONG_OPTIONS); +} + +export function normalizeCksumLongOption(arg) { + return normalizeLongOptionByPrefix(arg, CKSUM_LONG_OPTIONS); +} + +const singleCall = defineCommand("cksum", cksum, cksumMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/comm.js b/src/commands/comm.js new file mode 100644 index 0000000..738d314 --- /dev/null +++ b/src/commands/comm.js @@ -0,0 +1,261 @@ +#!/usr/bin/env bun + +import { closeSync, fstatSync, openSync } from "node:fs"; +import { createFdRecordReader, isWriteError, localeQuotedEscapedDiagnostic, parseOptions, readAll, systemErrorMessage, textInputDiagnosticName } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const COMM_LONG_OPTIONS = ["check-order", "nocheck-order", "output-delimiter", "total", "zero-terminated", "help", "version"]; + +export function commMetaOption(args) { + const outputDelimiters = []; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (arg.startsWith("--")) { + const option = normalizeCommLongOption(arg); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (!COMM_LONG_OPTIONS.includes(name)) return null; + if (option === "--help" || option === "--version") return option; + if (name === "output-delimiter") { + const value = inlineValue ?? args[i + 1]; + if (value !== undefined) { + outputDelimiters.push(value); + if (new Set(outputDelimiters).size > 1) throw new UsageError("multiple output delimiters specified"); + } + if (inlineValue == null) i++; + } else if (inlineValue != null) return null; + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + for (let j = 1; j < arg.length; j++) if (!"123z".includes(arg[j])) return null; + } + return null; +} + +export function normalizeCommLongOptions(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(normalizeCommLongOption(arg)); + } + return out; +} + +export function normalizeCommLongOption(arg) { + const body = arg.slice(2); + const eq = body.indexOf("="); + const name = eq === -1 ? body : body.slice(0, eq); + if (!name) return arg; + const matches = COMM_LONG_OPTIONS.filter((option) => option.startsWith(name)); + if (matches.length !== 1) return arg; + return eq === -1 ? `--${matches[0]}` : `--${matches[0]}=${body.slice(eq + 1)}`; +} + +export async function comm(args) { + args = normalizeCommLongOptions(args); + const { opts, operands } = parseOptions(args, { short: { 1: false, 2: false, 3: false, z: false }, long: { "output-delimiter": "value", total: false, "zero-terminated": false, "check-order": false, "nocheck-order": false, help: false, version: false } }); + const delim = commOutputDelimiter(args); + if (operands.length < 2) throw new UsageError(operands.length === 1 ? `missing operand after '${operands[0]}'` : "missing operand", true); + if (operands.length > 2) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[2])}`, true); + const sepByte = opts.z || opts["zero-terminated"] ? 0 : 0x0a; + const sep = Buffer.from([sepByte]); + const delimBytes = delim === "\0" ? Buffer.from([0]) : Buffer.from(delim); + if (operands[0] !== "-" && operands[1] !== "-" && !opts.total) { + const streamed = streamCommNonRegularFiles(operands, opts, sep, delimBytes); + if (streamed != null) return streamed; + } + let left; + let right; + let sharedStdinError = false; + if (operands[0] === "-" && operands[1] === "-") { + const records = splitCommByteRecords(await readAll("-"), sepByte); + left = records.filter((_, index) => index % 2 === 0); + right = records.filter((_, index) => index % 2 === 1); + sharedStdinError = true; + } else { + let leftBytes; + let rightBytes; + try { + leftBytes = await readAll(operands[0]); + rightBytes = await readAll(operands[1]); + } catch (error) { + const file = leftBytes == null ? operands[0] : operands[1]; + stderr(`comm: ${textInputDiagnosticName(file)}: ${systemErrorMessage(error)}\n`); + return 1; + } + left = splitCommByteRecords(leftBytes, sepByte); + right = splitCommByteRecords(rightBytes, sepByte); + } + let i = 0, j = 0; + const out = []; + const counts = [0, 0, 0]; + const order = { bad: [false, false], messages: [] }; + while (i < left.length || j < right.length) { + const leftBad = commNoteDisorder(left, i, 0, opts, order); + if (opts["check-order"] && leftBad) break; + const rightBad = commNoteDisorder(right, j, 1, opts, order); + if (opts["check-order"] && rightBad) break; + if (j >= right.length || (i < left.length && compareByteRecords(left[i], right[j]) < 0)) { + counts[0]++; + if (!opts[1]) pushCommRecord(out, left[i], sep); + i++; + } else if (i >= left.length || compareByteRecords(right[j], left[i]) < 0) { + counts[1]++; + if (!opts[2]) pushCommRecord(out, right[j], sep, commPrefixBytes(2, opts, delimBytes)); + j++; + } else { + counts[2]++; + if (!opts[3]) pushCommRecord(out, left[i], sep, commPrefixBytes(3, opts, delimBytes)); + i++; j++; + } + } + if (opts.total) { + out.push(Buffer.from(String(counts[0])), delimBytes, Buffer.from(String(counts[1])), delimBytes, Buffer.from(String(counts[2])), delimBytes, Buffer.from("total"), sep); + } + stdout(Buffer.concat(out)); + if (sharedStdinError) { + stderr("comm: -\n"); + return 1; + } + if (opts["nocheck-order"] || order.messages.length === 0) return 0; + const fullyPairable = left.length === right.length && left.every((line, index) => byteRecordsEqual(line, right[index])); + if (!opts["check-order"] && fullyPairable) return 0; + for (const message of order.messages) stderr(`comm: ${message}\n`); + if (!opts["check-order"]) stderr("comm: input is not in sorted order\n"); + return 1; +} + +export function streamCommNonRegularFiles(files, opts, sep, delimBytes) { + const fds = []; + try { + for (const file of files) fds.push(openSync(file, "r")); + if (fds.some((fd) => fstatSync(fd).isFile())) return null; + const readers = fds.map((fd) => createFdRecordReader(fd, sep[0])); + const previous = [null, null]; + const order = { bad: [false, false], messages: [] }; + let stoppedForOrder = false; + const next = (side) => { + const record = readers[side].next(); + if (record != null && previous[side] != null && !opts["nocheck-order"] && !order.bad[side] && compareByteRecords(previous[side], record) > 0) { + order.bad[side] = true; + order.messages.push(`file ${side + 1} is not in sorted order`); + if (opts["check-order"]) stoppedForOrder = true; + } + previous[side] = record; + return record; + }; + let left = next(0); + let right = next(1); + while (!stoppedForOrder && (left != null || right != null)) { + if (right == null || (left != null && compareByteRecords(left, right) < 0)) { + if (!opts[1]) writeCommRecord(left, sep); + left = next(0); + } else if (left == null || compareByteRecords(right, left) < 0) { + if (!opts[2]) writeCommRecord(right, sep, commPrefixBytes(2, opts, delimBytes)); + right = next(1); + } else { + if (!opts[3]) writeCommRecord(left, sep, commPrefixBytes(3, opts, delimBytes)); + left = next(0); + right = next(1); + } + } + if (opts["nocheck-order"] || order.messages.length === 0) return 0; + for (const message of order.messages) stderr(`comm: ${message}\n`); + if (!opts["check-order"]) stderr("comm: input is not in sorted order\n"); + return 1; + } catch (error) { + if (isWriteError(error)) throw error; + const side = fds.length < 2 ? fds.length : 0; + stderr(`comm: ${textInputDiagnosticName(files[side])}: ${systemErrorMessage(error)}\n`); + return 1; + } finally { + for (const fd of fds) closeSync(fd); + } +} + +export function writeCommRecord(record, sep, prefix = null) { + if (prefix) stdout(prefix); + stdout(record); + stdout(sep); +} + +export function splitCommByteRecords(bytes, sepByte) { + if (bytes.length === 0) return []; + const end = bytes[bytes.length - 1] === sepByte ? bytes.length - 1 : bytes.length; + const records = []; + let start = 0; + for (let i = 0; i < end; i++) { + if (bytes[i] === sepByte) { + records.push(bytes.subarray(start, i)); + start = i + 1; + } + } + records.push(bytes.subarray(start, end)); + return records; +} + +export function compareByteRecords(left, right) { + const n = Math.min(left.length, right.length); + for (let i = 0; i < n; i++) { + if (left[i] !== right[i]) return left[i] - right[i]; + } + return left.length - right.length; +} + +export function byteRecordsEqual(left, right) { + return left.length === right.length && compareByteRecords(left, right) === 0; +} + +export function commPrefixBytes(col, opts, delimBytes) { + const chunks = []; + if (!opts[1] && col > 1) chunks.push(delimBytes); + if (!opts[2] && col > 2) chunks.push(delimBytes); + return chunks.length === 0 ? null : Buffer.concat(chunks); +} + +export function pushCommRecord(out, record, sep, prefix = null) { + if (prefix) out.push(prefix); + out.push(record, sep); +} + +export function commOutputDelimiter(args) { + const values = []; + let end = false; + let valueFor = false; + for (const arg of args) { + if (end) continue; + if (valueFor) { + values.push(arg); + valueFor = false; + continue; + } + if (arg === "--") { + end = true; + } else if (arg === "--output-delimiter") { + valueFor = true; + } else if (arg.startsWith("--output-delimiter=")) { + values.push(arg.slice("--output-delimiter=".length)); + } + } + if (new Set(values).size > 1) throw new UsageError("multiple output delimiters specified"); + if (values.length === 0) return "\t"; + return values[0] === "" ? "\0" : values[0]; +} + +export function commNoteDisorder(records, index, side, opts, order) { + if (opts["nocheck-order"] || index <= 0 || index >= records.length || order.bad[side] || compareByteRecords(records[index - 1], records[index]) <= 0) return false; + order.bad[side] = true; + order.messages.push(`file ${side + 1} is not in sorted order`); + return true; +} + +const singleCall = defineCommand("comm", comm, commMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/coreutils.js b/src/commands/coreutils.js new file mode 100644 index 0000000..b5083af --- /dev/null +++ b/src/commands/coreutils.js @@ -0,0 +1,42 @@ +#!/usr/bin/env bun + +import { commandNames, hasCommand } from "../shared/catalog.js"; +import { invalidOptionMessage } from "../shared/common.js"; +import { UsageError, VERSION, fail, stdout } from "../shared/diagnostics.js"; +import { main } from "../shared/runtime.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export async function coreutilsCmd(args) { + if (args[0]?.startsWith("--coreutils-prog=")) { + const program = args[0].slice("--coreutils-prog=".length); + if (!program || !hasCommand(program)) return fail("coreutils", `unknown program '${program}'`); + return main([program, ...args.slice(1)]); + } + if (args[0]?.startsWith("--coreutils-prog-shebang=")) { + const program = args[0].slice("--coreutils-prog-shebang=".length); + if (!program || !hasCommand(program)) return fail("coreutils", `unknown program '${program}'`); + if (args.length < 2) return fail("coreutils", `unknown program '${program}'`); + return main([program, ...args.slice(2)]); + } + if (!args.length) throw new UsageError("missing operand"); + if (args[0] === "--help") { + stdout("Usage: coreutils --coreutils-prog=PROGRAM_NAME [PARAMETERS]...\n"); + stdout("Execute the PROGRAM_NAME built-in program with the given PARAMETERS.\n\n"); + stdout(" --help display this help and exit\n"); + stdout(" --version output version information and exit\n\n"); + stdout(`Built-in programs:\n ${commandNames.join(" ")}\n\n`); + stdout("Use: 'coreutils --coreutils-prog=PROGRAM_NAME --help' for individual program help.\n"); + return 0; + } + if (args[0] === "--version") { + stdout(`${VERSION}\n`); + return 0; + } + if (args[0].startsWith("-")) throw new UsageError(invalidOptionMessage(args[0]), true); + return main(args); +} + +const singleCall = defineCommand("coreutils", coreutilsCmd, null); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/cp.js b/src/commands/cp.js new file mode 100644 index 0000000..ef864fd --- /dev/null +++ b/src/commands/cp.js @@ -0,0 +1,605 @@ +#!/usr/bin/env bun + +import { mkdirSync } from "node:fs"; +import { copyFile, link as fsLink, lstat, mkdir, readlink, stat } from "node:fs/promises"; +import { isAbsolute, join, basename as pathBasename, dirname as pathDirname, resolve } from "node:path"; +import { bufferPathBasename, bufferPathJoin, invalidOptionMessage, isBytePath, localeQuotedDiagnostic, localeQuotedEscapedDiagnostic, normalizeLongOptionByPrefix, parseOptions, pathDisplayName, pathLikeJoin, rawCommandArgs, readdirPathEntries, selinuxRuntimeEnabled } from "../shared/common.js"; +import { applyCopySecurityContext, areSameFile, backupDestination, backupFileName, backupModeFromEnvironment, copyPath, cpDuplicateDisplayName, cpRestrictiveDirectoryMode, cpShouldDereferenceSource, cpUpdateDecision, ensureBackupDoesNotDestroySource, resolveDestinationOperands, stripTrailingSlashesPath, validateBackupMode, validateUpdateMode, withPreservedCopyCreationContext } from "../shared/copy.js"; +import { InvocationError, UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { confirmRemoval, isWriteProtected, modeString, rawArgLooksLikeOption, rawPathNeedsBytes, setFileMode } from "../shared/filesystem.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const CP_LONG_OPTIONS = ["archive", "attributes-only", "backup", "copy-contents", "debug", "force", "interactive", "dereference", "keep-directory-symlink", "link", "no-clobber", "no-dereference", "preserve", "no-preserve", "parents", "recursive", "reflink", "remove-destination", "sparse", "strip-trailing-slashes", "suffix", "symbolic-link", "target-directory", "no-target-directory", "update", "verbose", "one-file-system", "context", "help", "version"]; + +export function cpMetaOption(args) { + const longFlagOptions = new Set(["archive", "attributes-only", "copy-contents", "debug", "force", "interactive", "dereference", "no-dereference", "keep-directory-symlink", "link", "no-clobber", "parents", "recursive", "remove-destination", "strip-trailing-slashes", "symbolic-link", "no-target-directory", "verbose", "one-file-system"]); + let sawContextValue = false; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + let normalized = arg; + if (arg.startsWith("--")) normalized = normalizeLongOptionByPrefix(arg, CP_LONG_OPTIONS); + const [name, inlineValue] = normalized.startsWith("--") ? normalized.slice(2).split(/=(.*)/s, 2) : [null, undefined]; + if (name === "help" || name === "version") { + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + if (sawContextValue) stderr("cp: warning: ignoring --context; it requires an SELinux-enabled kernel\n"); + return normalized; + } + if (longFlagOptions.has(name)) { + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + continue; + } + if (name === "suffix" || name === "target-directory" || name === "no-preserve" || name === "sparse") { + if (inlineValue === undefined) i++; + if (name === "no-preserve" && inlineValue !== undefined) validateCpPreserveList(inlineValue, "--no-preserve"); + if (name === "sparse" && inlineValue !== undefined) validateSparseMode("cp", inlineValue); + continue; + } + if (name === "preserve") { + if (inlineValue !== undefined) validateCpPreserveList(inlineValue, "--preserve"); + continue; + } + if (name === "context") { + if (inlineValue !== undefined) sawContextValue = true; + continue; + } + if (name === "backup") continue; + if (name === "reflink") { + if (inlineValue !== undefined) validateReflinkMode("cp", inlineValue); + continue; + } + if (name === "update") { + if (inlineValue !== undefined) validateUpdateMode("cp", inlineValue); + continue; + } + if (arg.startsWith("-") && !arg.startsWith("--")) { + i = scanCpShortMetaOption(args, i); + continue; + } + if (arg.startsWith("-")) throw new UsageError(invalidOptionMessage(arg), true); + continue; + } + return null; +} + +export function scanCpShortMetaOption(args, index) { + const arg = args[index]; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if ("finrRapsdlHLPTuvbxZ".includes(ch)) continue; + if (ch === "t" || ch === "S") return arg.slice(j + 1) ? index : index + 1; + throw new UsageError(`invalid option -- '${ch}'`, true); + } + return index; +} + +export async function cpCmd(args) { + args = normalizeCpArgs(args); + const { opts, operands } = parseOptions(args, { short: { f: false, i: false, n: false, r: false, R: false, a: false, p: false, s: false, d: false, l: false, H: false, L: false, P: false, u: false, v: false, t: "value", T: false, b: false, S: "value", x: false, Z: false }, long: { force: false, interactive: false, "no-clobber": false, recursive: false, archive: false, preserve: "optional-value", parent: false, parents: false, "symbolic-link": false, "no-dereference": false, link: false, dereference: false, "no-preserve": "value", update: "optional-value", verbose: false, debug: false, reflink: "optional-value", sparse: "value", "target-directory": "value", "no-target-directory": false, "remove-destination": false, rem: false, "attributes-only": false, "copy-contents": false, context: "optional-value", backup: "optional-value", b: "optional-value", suffix: "value", "strip-trailing-slashes": false, "one-file-system": false, "keep-directory-symlink": false, help: false, version: false } }); + const selinuxEnabled = selinuxRuntimeEnabled(); + if (typeof opts.context === "string" && !selinuxEnabled) stderr("cp: warning: ignoring --context; it requires an SELinux-enabled kernel\n"); + if (opts.b != null && opts.backup == null) opts.backup = opts.b; + if (opts.S != null && opts.suffix == null) opts.suffix = opts.S; + const reflinkMode = cpReflinkMode(opts); + const sparseMode = cpSparseMode(opts); + validateCpPreserveOptions(opts); + const securityContextOptions = cpSecurityContextOptions(opts, selinuxEnabled); + const noDereference = opts.d || opts.P || opts["no-dereference"]; + const backupRequested = opts.b || opts.backup != null; + if (backupRequested) validateBackupMode("cp", opts.backup); + const overwriteMode = cpOverwriteMode(args, opts); + if (backupRequested && (overwriteMode === "no-clobber" || opts.update === "none" || opts.update === "none-fail")) { + throw new UsageError("--backup is mutually exclusive with -n or --update=none-fail", true); + } + if (reflinkMode === "always" && opts.sparse != null) throw new UsageError("options --reflink and --sparse are mutually exclusive"); + const { sources, dest, destStat, useDirectoryTarget } = await resolveDestinationOperands("cp", operands, opts); + if (cpParents(opts) && !useDirectoryTarget) throw new UsageError("with --parents, the destination must be a directory", true); + const seenSources = new Set(); + const createdTargets = new Map(); + const linkMap = new Map(); + const rawPlan = rawCpPlan(opts, operands, useDirectoryTarget); + let status = 0; + for (let i = 0; i < sources.length; i++) { + const src = sources[i]; + const normalizedSrc = opts["strip-trailing-slashes"] ? stripTrailingSlashesPath(src) : src; + const rawSource = rawPlan?.sources[i]; + const operationSource = rawSource ? await resolveRawExistingPath(stripTrailingDotPathBytes(rawSource)) : normalizedSrc; + try { + const target = rawSource ? rawCpTargetPath(rawSource, rawPlan.dest, rawPlan.useDirectoryTarget, opts) : cpTargetPath(normalizedSrc, dest, useDirectoryTarget, opts); + const sourceInfo = await cpTopLevelSourceStat(operationSource, opts, noDereference); + if (!sourceInfo) throw new UsageError(`cannot stat '${src}': No such file or directory`); + const sourceKey = rawSource ? rawPathKey(rawSource) : resolve(src); + if (sourceInfo && seenSources.has(sourceKey) && !backupRequested) { + const kind = sourceInfo.isDirectory() ? "directory" : "file"; + stderr(`cp: warning: source ${kind} '${cpDuplicateDisplayName(src)}' specified more than once\n`); + if (sourceInfo.isDirectory()) continue; + } + seenSources.add(sourceKey); + const targetKey = isBytePath(target) ? rawPathKey(target) : resolve(target); + if (backupRequested && createdTargets.has(targetKey) && !cpBackupIsNumbered(opts)) throw new UsageError(`will not overwrite just-created '${target}' with '${src}'`); + await ensureCpNotIntoSelf(operationSource, target, noDereference); + const updateDecision = await cpUpdateDecision(operationSource, target, opts, noDereference, overwriteMode); + if (updateDecision.skip) { + if (opts.debug) stdout(`skipped '${src}'\n`); + if (updateDecision.fail) { + stderr(`cp: not replacing '${target}'\n`); + status = 1; + } + continue; + } + const copyForce = opts.f || opts.force || backupRequested || overwriteMode === "interactive"; + if (overwriteMode === "interactive") { + const targetInfo = await lstat(target).catch(() => null); + if (targetInfo && !confirmRemoval(cpOverwritePrompt(target, targetInfo))) { + status = 1; + continue; + } + } + if (backupRequested) await ensureBackupDoesNotDestroySource("cp", src, target, opts.suffix, opts.backup); + const sameFile = await cpSameFile(operationSource, target, opts); + if (sameFile && (opts.l || opts.link) && await cpHardLinkSameFileIsNoop(src, target, opts, backupRequested)) continue; + if ((opts.l || opts.link) && (opts.d || opts["no-dereference"]) && src !== target && await areSamePathEntry(operationSource, target)) continue; + if (sameFile && await cpSameFileIsError(src, target, opts, backupRequested)) throw new UsageError(`'${src}' and '${target}' are the same file`); + if (sameFile && backupRequested && (opts.f || opts.force) && (!(opts.d || opts["no-dereference"]) || src === target) && !await cpTargetIsSymlink(target)) { + const backup = backupFileName(target, opts.suffix, opts.backup); + if (opts.l || opts.link) await fsLink(src, backup); + else await copyFile(src, backup); + continue; + } + const parentModeRestores = cpParents(opts) ? await cpPrepareParentDirectories(operationSource, dest, noDereference, cpPreserveMode(opts), cpPreserveMetadata(opts), securityContextOptions) : []; + const actualTargetInfo = await lstat(target).catch(() => null); + const destinationWasReportedExisting = Boolean(!actualTargetInfo && destStat && !useDirectoryTarget && sources.length === 1); + const targetExisted = Boolean(actualTargetInfo || destinationWasReportedExisting); + const createdByDifferentSource = createdTargets.has(targetKey) && createdTargets.get(targetKey) !== sourceKey; + if (!backupRequested && createdByDifferentSource && await cpTargetIsSymlink(target)) { + throw new UsageError(`will not copy '${src}' through just-created symlink '${target}'`); + } + if (!backupRequested && createdByDifferentSource) { + throw new UsageError(`will not overwrite just-created '${target}' with '${src}'`); + } + if (backupRequested && await cpShouldBackupDestination(target)) await backupDestination(target, opts.suffix, opts.backup); + await copyPath(operationSource, target, { + force: copyForce, + removeDestination: opts["remove-destination"] || opts.rem, + destinationWasReportedExisting, + recursive: opts.r || opts.R || opts.recursive || opts.a || opts.archive, + preserveMode: cpPreserveMode(opts), + preserveDirectoryMode: !cpNoPreserveAttrs(opts).includes("mode"), + useSourceModeForNewRegularFiles: cpUseSourceModeForNewRegularFiles(opts), + preserveMetadata: cpPreserveMetadata(opts), + preserveLinks: cpPreserveLinks(opts), + preserveXattrs: cpPreserveXattrs(opts), + requirePreserveXattrs: cpRequirePreserveXattrs(opts), + ...securityContextOptions, + attributesOnly: opts["attributes-only"], + debug: opts.debug, + linkMap, + symbolicLink: opts.s || opts["symbolic-link"], + hardLink: opts.l || opts.link, + sparseMode, + noDereference, + dereferenceAll: opts.L || opts.dereference, + dereferenceCommandLine: opts.H, + copyContents: opts["copy-contents"], + keepDirectorySymlink: opts["keep-directory-symlink"], + oneFileSystem: opts.x || opts["one-file-system"], + rootDev: sourceInfo.dev, + reflinkMode, + verbose: opts.v || opts.verbose || opts.debug, + }, true); + for (const restore of parentModeRestores.reverse()) { + if (restore.sourcePath != null) await applyCopySecurityContext(restore.path, restore.sourcePath, securityContextOptions); + if (restore.mode != null) await setFileMode(restore.path, restore.mode).catch(() => {}); + } + if (!targetExisted || await cpCopiedMatchingSymlink(src, target, noDereference)) createdTargets.set(targetKey, sourceKey); + } catch (error) { + status = 1; + if (error instanceof UsageError) stderr(`cp: ${error.message}\n`); + else stderr(`cp: ${error.message || String(error)}\n`); + } + } + return status; +} + +export async function cpCopiedMatchingSymlink(src, target, noDereference) { + if (!noDereference) return false; + const [srcInfo, targetInfo] = await Promise.all([lstat(src).catch(() => null), lstat(target).catch(() => null)]); + if (!srcInfo?.isSymbolicLink() || !targetInfo?.isSymbolicLink()) return false; + return await readlink(src).catch(() => null) === await readlink(target).catch(() => null); +} + +export async function cpTopLevelSourceStat(src, opts, noDereference) { + const dereferenceSource = cpShouldDereferenceSource(src, { + noDereference, + dereferenceAll: opts.L || opts.dereference, + dereferenceCommandLine: opts.H, + recursive: opts.r || opts.R || opts.recursive || opts.a || opts.archive, + hardLink: opts.l || opts.link, + preserveLinks: cpPreserveLinks(opts), + }, true); + return (dereferenceSource ? stat(src) : lstat(src)).catch(() => null); +} + +export function cpOverwritePrompt(target, targetInfo) { + if (isWriteProtected(targetInfo)) { + return `cp: replace '${target}', overriding mode ${(targetInfo.mode & 0o7777).toString(8).padStart(4, "0")} (${modeString(targetInfo).slice(1)})? `; + } + return `cp: overwrite '${target}'? `; +} + +export function normalizeCpArgs(args) { + const out = []; + let end = false; + for (const arg of args) { + if (end || arg === "--") { + out.push(arg); + if (arg === "--") end = true; + continue; + } + out.push(arg.startsWith("--") ? normalizeLongOptionByPrefix(arg, CP_LONG_OPTIONS) : arg); + } + return out; +} + +export function cpOverwriteMode(args, opts) { + let mode = "replace"; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") break; + if (arg === "-i" || arg === "--interactive") mode = "interactive"; + else if (arg === "-n" || arg === "--no-clobber") mode = "no-clobber"; + else if (/^-[^-]/.test(arg)) { + for (const ch of arg.slice(1)) { + if (ch === "i") mode = "interactive"; + else if (ch === "n") mode = "no-clobber"; + } + } + } + if (mode === "replace" && (opts.i || opts.interactive)) return "interactive"; + if (mode === "replace" && (opts.n || opts["no-clobber"])) return "no-clobber"; + return mode; +} + +export function cpReflinkMode(opts) { + if (opts.reflink == null) return opts.debug && opts.sparse == null ? "auto" : "never"; + const mode = opts.reflink === true ? "always" : String(opts.reflink); + if (mode === "always" || mode === "auto" || mode === "never") return mode; + validateReflinkMode("cp", mode); +} + +export function cpSparseMode(opts) { + if (opts.sparse == null) return "auto"; + const mode = String(opts.sparse); + if (mode === "always" || mode === "auto" || mode === "never") return mode; + validateSparseMode("cp", mode); +} + +export function cpBackupIsNumbered(opts) { + const mode = opts.backup === true || opts.backup == null ? backupModeFromEnvironment() : String(opts.backup); + return mode === "numbered" || mode === "t"; +} + +export async function cpShouldBackupDestination(target) { + const info = await lstat(target).catch(() => null); + return Boolean(info && !info.isDirectory()); +} + +export async function cpPrepareParentDirectories(src, destRoot, noDereference, preserveMode, preserveMetadata = false, securityOptions = {}) { + if (noDereference) await lstat(src); + else await stat(src); + const parentRel = pathDirname(cpParentPath(src)); + if (parentRel === "." || parentRel === "") return []; + const parentParts = parentRel.split("/").filter(Boolean); + let destPrefix = destRoot; + let srcPrefix = isAbsolute(src) ? "/" : ""; + const restores = []; + for (const part of parentParts) { + destPrefix = join(destPrefix, part); + srcPrefix = srcPrefix === "" || srcPrefix === "/" ? `${srcPrefix}${part}` : join(srcPrefix, part); + const mode = preserveMode ? (await stat(srcPrefix).catch(() => null))?.mode & 0o7777 : undefined; + const createMode = mode != null ? cpRestrictiveDirectoryMode(mode) : preserveMetadata ? 0o700 : 0o755; + const exists = Boolean(await lstat(destPrefix).catch(() => null)); + if (!exists) { + if (securityOptions.preserveContextAtCreation) { + await withPreservedCopyCreationContext(srcPrefix, securityOptions, () => mkdirSync(destPrefix, { mode: createMode })); + } else { + await mkdir(destPrefix, { recursive: false, mode: createMode }); + } + } + if (mode !== undefined || securityOptions.preserveContext) { + restores.push({ path: destPrefix, mode, sourcePath: securityOptions.preserveContext ? srcPrefix : null }); + } + } + return restores; +} + +export function cpPreserveMode(opts) { + if (opts.preserve != null) { + const attrs = opts.preserve === true ? ["mode", "ownership", "timestamps"] : String(opts.preserve).split(","); + if (attrs.includes("all") || attrs.includes("mode")) return true; + } + if (opts.p || opts.a || opts.archive) return !cpNoPreserveAttrs(opts).includes("mode"); + return false; +} + +export const CP_PRESERVE_VALID_ARGUMENTS = [ + ["mode"], + ["timestamps"], + ["ownership"], + ["links"], + ["context"], + ["xattr"], + ["all"], +]; + +export function validateCpPreserveOptions(opts) { + if (opts.preserve != null && opts.preserve !== true) { + validateCpPreserveList(String(opts.preserve), "--preserve"); + } + if (opts["no-preserve"] != null) validateCpPreserveList(String(opts["no-preserve"]), "--no-preserve"); +} + +export function cpSecurityContextOptions(opts, selinuxEnabled) { + const noPreserve = cpNoPreserveAttrs(opts); + const preserveAttrs = opts.preserve == null || opts.preserve === true ? [] : String(opts.preserve).split(","); + const explicitlyRequired = preserveAttrs.includes("context"); + if (explicitlyRequired && !selinuxEnabled) throw new InvocationError("cannot preserve security context without an SELinux-enabled kernel", 1, false); + const requestedDefault = Boolean(opts.Z || opts.context === true); + const explicitContext = typeof opts.context === "string" && selinuxEnabled ? opts.context : null; + if (explicitlyRequired && (requestedDefault || explicitContext != null)) { + throw new InvocationError("cannot set target context and preserve it", 1, false); + } + const preserveRequested = !noPreserve.some((attr) => attr === "all" || attr === "context") + && (opts.a || opts.archive || preserveAttrs.includes("all") || explicitlyRequired); + const preserveContext = selinuxEnabled && preserveRequested && !requestedDefault && explicitContext == null; + const restoreContext = selinuxEnabled && requestedDefault; + return { + selinuxEnabled, + preserveContext, + // GNU creates with the source label before applying a pathname default. + // If no default is defined, -Z therefore retains the source label. + preserveContextAtCreation: preserveContext || restoreContext, + requirePreserveContext: explicitlyRequired, + restoreContext, + explicitContext, + }; +} + +export function validateCpPreserveList(value, option) { + const valid = new Set(["mode", "timestamps", "ownership", "link", "links", "context", "xattr", "all"]); + const attrs = []; + for (const attr of value.split(",")) { + if (attr === "") throw new InvocationError(cpPreserveInvalidMessage(attr, option, "ambiguous"), 1, true); + if (!valid.has(attr)) throw new InvocationError(cpPreserveInvalidMessage(attr, option, "invalid"), 1, true); + attrs.push(attr); + } + return attrs; +} + +export function cpPreserveInvalidMessage(value, option, kind) { + return `${kind} argument ${localeQuotedEscapedDiagnostic(value)} for ${localeQuotedDiagnostic(option)}\nValid arguments are:\n${CP_PRESERVE_VALID_ARGUMENTS.map((group) => ` - ${group.map(localeQuotedDiagnostic).join(", ")}`).join("\n")}`; +} + +export function cpPreserveMetadata(opts) { + const preserve = opts.p || opts.a || opts.archive || opts.preserve != null; + if (!preserve) return false; + if (cpNoPreserveAttrs(opts).includes("all")) return false; + if (opts.preserve != null && !(opts.p || opts.a || opts.archive)) { + const attrs = opts.preserve === true ? ["mode", "ownership", "timestamps"] : String(opts.preserve).split(","); + return attrs.includes("all") || attrs.includes("ownership") || attrs.includes("timestamps"); + } + return true; +} + +export function cpNoPreserveAttrs(opts) { + return String(opts["no-preserve"] ?? "").split(",").filter(Boolean); +} + +export function cpPreserveLinks(opts) { + if (cpNoPreserveAttrs(opts).includes("links")) return false; + if (opts.d || opts.a || opts.archive) return true; + if (opts.preserve == null) return false; + const attrs = opts.preserve === true ? ["mode", "ownership", "timestamps"] : String(opts.preserve).split(","); + return attrs.includes("all") || attrs.includes("links") || attrs.includes("link"); +} + +export function cpPreserveXattrs(opts) { + if (cpNoPreserveAttrs(opts).some((attr) => attr === "all" || attr === "xattr")) return false; + if (opts.a || opts.archive) return true; + if (opts.preserve == null) return false; + const attrs = opts.preserve === true ? ["mode", "ownership", "timestamps"] : String(opts.preserve).split(","); + return attrs.includes("all") || attrs.includes("xattr"); +} + +export function cpRequirePreserveXattrs(opts) { + if (opts.preserve == null || opts.preserve === true) return false; + return String(opts.preserve).split(",").includes("xattr"); +} + +export function cpUseSourceModeForNewRegularFiles(opts) { + return !cpNoPreserveAttrs(opts).some((attr) => attr === "all" || attr === "mode"); +} + +export function cpParents(opts) { + return Boolean(opts.parents || opts.parent); +} + +export function cpTargetPath(src, dest, useDirectoryTarget, opts) { + if (cpParents(opts)) return join(dest, cpParentPath(src)); + return useDirectoryTarget ? join(dest, pathBasename(src.replace(/\/+$/, ""))) : dest; +} + +export function rawCpPlan(opts, operands, useDirectoryTarget) { + const raw = rawCommandArgs("cp"); + if (!raw) return null; + const rawOperands = parseRawCpOperands(raw); + if (rawOperands.length !== operands.length) return null; + if (!rawOperands.some((operand, index) => rawPathNeedsBytes(operand) || !operand.equals(Buffer.from(operands[index])))) return null; + if (useDirectoryTarget) return { sources: rawOperands.slice(0, -1), dest: rawOperands.at(-1), useDirectoryTarget: true }; + return { sources: rawOperands.slice(0, -1), dest: rawOperands.at(-1), useDirectoryTarget: false }; +} + +export function parseRawCpOperands(rawArgs) { + const operands = []; + for (let i = 0; i < rawArgs.length; i++) { + const arg = rawArgs[i]; + if (arg.equals(Buffer.from("--"))) { + operands.push(...rawArgs.slice(i + 1)); + break; + } + if (!rawArgLooksLikeOption(arg)) { + operands.push(arg); + continue; + } + const text = arg.toString(); + if (["-t", "--target-directory", "--target", "--suffix", "-S"].includes(text)) { + i++; + continue; + } + if (/^(--target-directory|--target|--suffix|--backup|--preserve|--no-preserve|--update|--reflink|--sparse)=/.test(text)) continue; + if (arg.length > 2 && arg[0] === 0x2d) { + const letters = arg.toString().slice(1); + const valueIndex = [...letters].findIndex((ch) => ch === "t" || ch === "S"); + if (valueIndex !== -1 && valueIndex === letters.length - 1) i++; + } + } + return operands; +} + +export function rawCpTargetPath(src, dest, useDirectoryTarget, opts) { + if (cpParents(opts)) return bufferPathJoin(dest, Buffer.from(cpParentPath(pathDisplayName(src)))); + if (rawPathEndsWithSlashDot(src)) return dest; + return useDirectoryTarget ? bufferPathJoin(dest, bufferPathBasename(stripTrailingSlashBytes(src))) : dest; +} + +export function rawPathEndsWithSlashDot(path) { + return path.length >= 2 && path[path.length - 2] === 0x2f && path[path.length - 1] === 0x2e; +} + +export function stripTrailingSlashBytes(path) { + let end = path.length; + while (end > 1 && path[end - 1] === 0x2f) end--; + return path.subarray(0, end); +} + +export function stripTrailingDotPathBytes(path) { + if (path.length >= 2 && path[path.length - 2] === 0x2f && path[path.length - 1] === 0x2e) return path.subarray(0, path.length - 2); + return path; +} + +export function rawPathKey(path) { + return Buffer.from(path).toString("hex"); +} + +export async function resolveRawExistingPath(path) { + if (await lstat(path).then(() => true, () => false)) return path; + const absolute = path[0] === 0x2f; + const parts = Buffer.from(path).toString("binary").split("/").map((part) => Buffer.from(part, "binary")); + let current = absolute ? Buffer.from("/") : Buffer.from("."); + for (const part of parts) { + if (!part.length || part.equals(Buffer.from("."))) continue; + if (part.equals(Buffer.from(".."))) { + current = pathLikeJoin(current, part); + continue; + } + const entries = await readdirPathEntries(current); + const display = part.toString(); + let match = entries.find((entry) => Buffer.from(entry).equals(part) || pathDisplayName(entry) === display); + if (!match && rawPathNeedsBytes(part)) { + const candidates = entries.filter((entry) => rawPathNeedsBytes(entry)); + if (candidates.length === 1) match = candidates[0]; + } + if (!match) return path; + current = pathLikeJoin(current, match); + } + return current; +} + +export function cpParentPath(src) { + const stripped = String(src).replace(/^\/+/, "").replace(/\/+$/, ""); + return stripped || "."; +} + +export async function ensureCpNotIntoSelf(src, target, noDereference) { + const sourceInfo = noDereference ? await lstat(src).catch(() => null) : await stat(src).catch(() => null); + if (!sourceInfo?.isDirectory()) return; + const sourcePath = resolve(pathDisplayName(src)); + const targetPath = resolve(pathDisplayName(target)); + if (targetPath === sourcePath || targetPath.startsWith(`${sourcePath}/`)) { + throw new UsageError(`cannot copy a directory, '${pathDisplayName(src)}', into itself, '${pathDisplayName(target)}'`); + } +} + +export async function cpSameFileIsError(src, target, opts, backupRequested) { + if (opts.s || opts["symbolic-link"]) return !await cpTargetIsReplaceableSymlink(src, target); + if (opts.l || opts.link) return false; + if (backupRequested && (opts.d || opts["no-dereference"])) { + const srcInfo = await lstat(src).catch(() => null); + if (srcInfo?.isSymbolicLink() && src !== target) return false; + } + if (backupRequested && (opts.f || opts.force)) { + const srcInfo = await lstat(src).catch(() => null); + if (srcInfo && !srcInfo.isSymbolicLink()) return false; + if (await cpTargetIsSymlink(target)) return false; + } + if (backupRequested && resolve(src) !== resolve(target)) { + const srcInfo = await lstat(src).catch(() => null); + if (srcInfo && !srcInfo.isSymbolicLink()) return false; + if (await cpTargetIsSymlink(target)) return false; + } + if ((opts["remove-destination"] || opts.rem) && resolve(src) !== resolve(target)) { + const srcInfo = await lstat(src).catch(() => null); + if (srcInfo && !srcInfo.isSymbolicLink()) return false; + if (await cpTargetIsSymlink(target)) return false; + } + return true; +} + +export async function cpSameFile(src, target, opts) { + if (opts.d || opts.P || opts["no-dereference"]) { + const [srcInfo, targetInfo] = await Promise.all([lstat(src).catch(() => null), lstat(target).catch(() => null)]); + if (srcInfo?.isSymbolicLink() && targetInfo?.isSymbolicLink()) return resolve(src) === resolve(target) && src === target; + } + return areSameFile(src, target); +} + +export function validateReflinkMode(program, mode) { + if (["auto", "always", "never"].includes(mode)) return; + const kind = mode === "" ? "ambiguous" : "invalid"; + throw new UsageError(`${kind} argument ${localeQuotedEscapedDiagnostic(mode)} for ${localeQuotedDiagnostic("--reflink")}\nValid arguments are:\n - ${localeQuotedDiagnostic("auto")}\n - ${localeQuotedDiagnostic("always")}\n - ${localeQuotedDiagnostic("never")}`, true); +} + +export function validateSparseMode(program, mode) { + if (["never", "auto", "always"].includes(mode)) return; + const kind = mode === "" ? "ambiguous" : "invalid"; + throw new UsageError(`${kind} argument ${localeQuotedEscapedDiagnostic(mode)} for ${localeQuotedDiagnostic("--sparse")}\nValid arguments are:\n - ${localeQuotedDiagnostic("never")}\n - ${localeQuotedDiagnostic("auto")}\n - ${localeQuotedDiagnostic("always")}`, true); +} + +export async function cpHardLinkSameFileIsNoop(src, target, opts, backupRequested) { + if (backupRequested && (opts.f || opts.force) && resolve(src) === resolve(target)) return false; + const targetInfo = await lstat(target).catch(() => null); + return Boolean(targetInfo && !targetInfo.isSymbolicLink()); +} + +export async function cpTargetIsSymlink(target) { + return Boolean((await lstat(target).catch(() => null))?.isSymbolicLink()); +} + +export async function cpTargetIsReplaceableSymlink(src, target) { + if (resolve(src) === resolve(target)) return false; + return cpTargetIsSymlink(target); +} + +export async function areSamePathEntry(a, b) { + const [as, bs] = await Promise.all([lstat(a).catch(() => null), lstat(b).catch(() => null)]); + return Boolean(as && bs && areSamePathStats(as, bs)); +} + +export function areSamePathStats(a, b) { + return a.dev === b.dev && a.ino === b.ino; +} + +const singleCall = defineCommand("cp", cpCmd, cpMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/csplit.js b/src/commands/csplit.js new file mode 100644 index 0000000..77e31e4 --- /dev/null +++ b/src/commands/csplit.js @@ -0,0 +1,300 @@ +#!/usr/bin/env bun + +import { closeSync, openSync, readSync, writeSync } from "node:fs"; +import { lstat, rm, writeFile } from "node:fs/promises"; +import { enc, localeQuotedDiagnostic, localeQuotedEscapedDiagnostic, nodeErrorMessage, normalizeLongOptionByPrefix, parseOptions, pathDisplayName, readAll, shellEscapeLsName, systemErrorMessage } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const CSPLIT_LONG_OPTIONS = ["silent", "suffix-format", "suppress-matched", "prefix", "keep-files", "digits", "quiet", "elide-empty-files", "help", "version"]; + +export function csplitMetaOption(args) { + const longValueOptions = new Set(["suffix-format", "prefix", "digits"]); + const longKnownOptions = new Set(CSPLIT_LONG_OPTIONS); + const shortValueOptions = new Set(["b", "f", "n"]); + const shortKnownOptions = new Set(["b", "f", "k", "n", "q", "s", "z"]); + const normalized = normalizeCsplitLongOptionAbbreviations(args); + for (let i = 0; i < normalized.length; i++) { + const arg = normalized[i]; + if (arg === "--") return null; + if (arg.startsWith("--")) { + const [name, inlineValue] = arg.slice(2).split(/=(.*)/s, 2); + if (!longKnownOptions.has(name)) return null; + if (arg === "--help" || arg === "--version") return arg; + if (longValueOptions.has(name)) { + const value = inlineValue ?? normalized[i + 1]; + if (name === "digits" && value !== undefined) parseCsplitDigits(value); + if (inlineValue == null) i++; + } else if (inlineValue != null) return null; + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (!shortKnownOptions.has(ch)) return null; + if (shortValueOptions.has(ch)) { + const inlineValue = arg.slice(j + 1); + const value = inlineValue === "" ? normalized[i + 1] : inlineValue; + if (ch === "n" && value !== undefined) parseCsplitDigits(value); + if (inlineValue === "" && i + 1 < normalized.length) i++; + break; + } + } + } + return null; +} + +export function normalizeCsplitLongOptionAbbreviations(args) { + const out = []; + const valueOptions = new Set(["suffix-format", "prefix", "digits"]); + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") { + out.push(arg, ...args.slice(i + 1)); + break; + } + if (!arg.startsWith("--")) { + out.push(arg); + continue; + } + const normalized = normalizeLongOptionByPrefix(arg, CSPLIT_LONG_OPTIONS); + out.push(normalized); + const [name, inlineValue] = normalized.slice(2).split(/=(.*)/s, 2); + if (valueOptions.has(name) && inlineValue == null && i + 1 < args.length) out.push(args[++i]); + } + return out; +} + +export async function csplitCmd(args) { + args = normalizeCsplitLongOptionAbbreviations(args); + const { opts, operands } = parseOptions(args, { short: { b: "value", f: "value", k: false, n: "value", q: false, s: false, z: false }, long: { "suffix-format": "value", prefix: "value", "keep-files": false, "suppress-matched": false, digits: "value", quiet: false, silent: false, "elide-empty-files": false, help: false, version: false } }); + if (!operands.length) throw new UsageError("missing operand", true); + if (operands.length < 2) throw new UsageError(`missing operand after '${operands[0]}'`, true); + const [file, ...patterns] = operands; + const prefix = opts.f ?? opts.prefix ?? "xx"; + const digits = parseCsplitDigits(opts.n ?? opts.digits ?? 2); + const suffixFormat = opts.b ?? opts["suffix-format"] ?? `%0${digits}d`; + const quiet = opts.q || opts.s || opts.quiet || opts.silent; + const elideEmpty = opts.z || opts["elide-empty-files"]; + const suppressMatched = opts["suppress-matched"]; + const streamingPattern = file === "-" && patterns.length === 1 ? parseCsplitRegexPattern(patterns[0]) : null; + if (streamingPattern?.skip && streamingPattern.offset >= 0 && !suppressMatched) { + return csplitStreamForwardSkip(streamingPattern, { prefix, suffixFormat, quiet, elideEmpty, pattern: patterns[0] }); + } + let input; + try { + input = await readAll(file); + } catch (error) { + if (error?.code === "EISDIR") { + const name = `${prefix}${formatCsplitSuffix(suffixFormat, 0)}`; + if (!elideEmpty) { + try { + await writeFile(name, ""); + } catch (writeError) { + stderr(`csplit: ${name}: ${nodeErrorMessage(writeError)}\n`); + return 1; + } + if (!quiet) stdout("0\n"); + if (!(opts.k || opts["keep-files"])) await cleanupCsplitFiles([name]); + } + stderr(`csplit: read error: ${systemErrorMessage(error)}\n`); + } else { + stderr(file === "-" ? `csplit: ${nodeErrorMessage(error)}\n` : `csplit: cannot open ${shellEscapeLsName(pathDisplayName(file), true)} for reading: ${systemErrorMessage(error)}\n`); + } + return 1; + } + const lines = new TextDecoder().decode(input).split(/(?<=\n)/).filter(Boolean); + const chunks = []; + const suppressed = new Set(); + let start = 0; + let lastCut = 0; + let searchStart = 0; + for (let patternIndex = 0; patternIndex < patterns.length; patternIndex++) { + const pattern = patterns[patternIndex]; + if (/^\{(\*|\d+)\}$/.test(pattern)) continue; + if (/^\d+$/.test(pattern)) { + const lineNumber = Number(pattern); + if (lineNumber <= 0) { + stderr(`csplit: ${pattern}: line number must be greater than zero\n`); + return 1; + } + const cut = lineNumber - 1; + if (cut < lastCut) { + stderr(`csplit: line number '${pattern}' is smaller than preceding line number, ${lastCut + 1}\n`); + return 1; + } + if (cut > lines.length || (lines.length === 0 && lineNumber === 1)) { + if (patterns[patternIndex + 1] === pattern) stderr(`csplit: warning: line number '${pattern}' is the same as preceding line number\n`); + stderr(`csplit: '${pattern}': line number out of range\n`); + return 1; + } + if (cut === lastCut && chunks.length) stderr(`csplit: warning: line number '${pattern}' is the same as preceding line number\n`); + if (suppressMatched && cut >= 0 && cut < lines.length) suppressed.add(cut); + chunks.push([start, cut]); + start = cut; + lastCut = cut; + searchStart = start; + continue; + } + const regexPattern = parseCsplitRegexPattern(pattern); + if (regexPattern) { + let repeat = 0; + const repeatSpec = patterns[patternIndex + 1]?.match(/^\{(\*|\d+)\}$/); + if (repeatSpec) { + repeat = repeatSpec[1] === "*" ? Infinity : Number(repeatSpec[1]); + patternIndex++; + } + let applications = 0; + while (true) { + const result = applyCsplitRegex(lines, start, searchStart, regexPattern, suppressMatched, suppressed); + if (!result) { + if (applications === 0) throw new UsageError(`${localeQuotedEscapedDiagnostic(pattern)}: match not found`); + break; + } + if (!result.skip) chunks.push([start, result.cut]); + start = result.start; + lastCut = result.cut; + searchStart = result.nextSearch; + applications++; + if (applications > repeat) break; + } + continue; + } + stderr(`csplit: ${localeQuotedDiagnostic(pattern)}: invalid pattern\n`); + return 1; + } + chunks.push([start, lines.length]); + let outputIndex = 0; + const createdFiles = []; + for (const [from, to] of chunks) { + const chunk = lines.slice(from, to).filter((_, i) => !suppressed.has(from + i)).join(""); + if (elideEmpty && chunk === "") continue; + const name = `${prefix}${formatCsplitSuffix(suffixFormat, outputIndex++)}`; + try { + await writeFile(name, chunk); + createdFiles.push(name); + } catch (error) { + stderr(`csplit: ${name}: ${systemErrorMessage(error)}\n`); + if (!(opts.k || opts["keep-files"])) await cleanupCsplitFiles([...createdFiles, name]); + return 1; + } + if (!quiet) stdout(`${enc.encode(chunk).byteLength}\n`); + } + return 0; +} + +export function csplitStreamForwardSkip(spec, options) { + const input = Buffer.allocUnsafe(64 * 1024); + const decoder = new TextDecoder(); + let carry = ""; + let matched = false; + let linesToSkip = 0; + let outputFd = -1; + let outputBytes = 0; + const name = `${options.prefix}${formatCsplitSuffix(options.suffixFormat, 0)}`; + const writeLine = (line) => { + const bytes = enc.encode(line); + if (outputFd < 0) outputFd = openSync(name, "w"); + writeSync(outputFd, bytes); + outputBytes += bytes.length; + }; + const consume = (line) => { + if (!matched) { + if (!spec.regex.test(line.replace(/\n$/, ""))) return; + matched = true; + if (spec.offset === 0) writeLine(line); + else linesToSkip = spec.offset - 1; + return; + } + if (linesToSkip > 0) { + linesToSkip--; + return; + } + writeLine(line); + }; + try { + while (true) { + const count = readSync(0, input, 0, input.length, null); + if (count === 0) break; + carry += decoder.decode(input.subarray(0, count), { stream: true }); + let newline; + while ((newline = carry.indexOf("\n")) !== -1) { + consume(carry.slice(0, newline + 1)); + carry = carry.slice(newline + 1); + } + } + carry += decoder.decode(); + if (carry) consume(carry); + } finally { + if (outputFd >= 0) closeSync(outputFd); + } + if (!matched) throw new UsageError(`${localeQuotedEscapedDiagnostic(options.pattern)}: match not found`); + if (outputFd < 0 && !options.elideEmpty) closeSync(openSync(name, "w")); + if (!options.quiet && (outputBytes > 0 || !options.elideEmpty)) stdout(`${outputBytes}\n`); + return 0; +} + +export function parseCsplitDigits(value) { + const text = String(value); + if (!/^\d+$/.test(text)) throw new UsageError(`invalid number: ${localeQuotedEscapedDiagnostic(text)}`); + return Number(text); +} + +export async function cleanupCsplitFiles(files) { + for (const file of files) { + const info = await lstat(file).catch(() => null); + if (!info || info.isDirectory()) continue; + await rm(file, { force: true }).catch(() => {}); + } +} + +export function applyCsplitRegex(lines, start, searchStart, spec, suppressMatched, suppressed) { + const { skip, regex, offset } = spec; + const idx = lines.findIndex((line, i) => i >= searchStart && regex.test(line.replace(/\n$/, ""))); + if (idx === -1) return null; + const cut = Math.max(0, Math.min(lines.length, idx + offset)); + if (suppressMatched && cut >= 0 && cut < lines.length) suppressed.add(cut); + if (skip) { + return { skip, cut, start: suppressMatched ? Math.min(lines.length, cut + 1) : cut, nextSearch: Math.max(cut + 1, idx + 1) }; + } + return { skip, cut, start: cut, nextSearch: idx + 1 }; +} + +export function parseCsplitRegexPattern(pattern) { + const delimiter = pattern[0]; + if (delimiter !== "/" && delimiter !== "%") return null; + const end = pattern.lastIndexOf(delimiter); + if (end <= 0) throw new UsageError(`${pattern}: closing delimiter '${delimiter}' missing`); + const offsetText = pattern.slice(end + 1); + if (offsetText && !/^[+-]?\d+$/.test(offsetText)) throw new UsageError(`${localeQuotedEscapedDiagnostic(pattern)}: integer expected after delimiter`); + try { + return { skip: delimiter === "%", regex: new RegExp(csplitBasicRegex(pattern.slice(1, end))), offset: Number(offsetText || 0) }; + } catch { + throw new UsageError(`${localeQuotedEscapedDiagnostic(pattern)}: invalid regular expression: Invalid regular expression`); + } +} + +export function csplitBasicRegex(pattern) { + return pattern.replace(/\\([{}])/g, "$1"); +} + +export function formatCsplitSuffix(format, index) { + return format.replace(/%([0 #+-]*)(\d*)(?:\.(\d+))?([dixX])/g, (_, flags, widthText, precisionText, type) => { + const width = Number(widthText || 0); + const precision = precisionText === undefined ? null : Number(precisionText); + const base = type === "d" || type === "i" ? 10 : 16; + let digits = index.toString(base); + if (type === "X") digits = digits.toUpperCase(); + if (precision != null) digits = digits.padStart(precision, "0"); + const alternate = flags.includes("#") && base === 16 && index !== 0 ? (type === "X" ? "0X" : "0x") : ""; + let rendered = `${alternate}${digits}`; + const pad = flags.includes("0") && precision == null ? "0" : " "; + if (width > rendered.length) rendered = flags.includes("-") ? rendered.padEnd(width, " ") : rendered.padStart(width, pad); + return rendered; + }); +} + +const singleCall = defineCommand("csplit", csplitCmd, csplitMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/cut.js b/src/commands/cut.js new file mode 100644 index 0000000..34199ab --- /dev/null +++ b/src/commands/cut.js @@ -0,0 +1,692 @@ +#!/usr/bin/env bun + +import { closeSync, openSync } from "node:fs"; +import { concatBytes, enc, gb18030Units, inRanges, isGb18030Locale, isUtf8Continuation, isWriteError, localeQuotedDiagnostic, localeQuotedEscapedDiagnostic, nodeErrorMessage, parseOptions, rawCommandArgs, readAll, readFdByteRecords, readFdChunkViews, systemErrorMessage, textInputDiagnosticName } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { isSingleByteLocale } from "../shared/text.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const CUT_LONG_OPTIONS = ["bytes", "characters", "complement", "delimiter", "fields", "no-partial", "only-delimited", "output-delimiter", "whitespace-delimited", "zero-terminated", "help", "version"]; + +export function cutMetaOption(args) { + const longValueOptions = new Set(["bytes", "characters", "delimiter", "fields", "output-delimiter"]); + const longOptionalValueOptions = new Set(["whitespace-delimited"]); + const shortValueOptions = new Set(["b", "c", "d", "f", "F", "O"]); + const shortKnownOptions = new Set(["b", "c", "d", "f", "F", "n", "O", "s", "w", "z"]); + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (arg.startsWith("--")) { + const option = normalizeCutLongOption(arg); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (!CUT_LONG_OPTIONS.includes(name)) return null; + if ((option === "--help" || option === "--version") && inlineValue == null) return option; + if (name === "whitespace-delimited" && inlineValue != null) normalizeCutWhitespaceDelimitedOption(inlineValue); + if (name === "delimiter") validateCutMetaOptionValue(name, inlineValue ?? args[i + 1]); + if (inlineValue == null && longValueOptions.has(name)) i++; + else if (inlineValue == null && longOptionalValueOptions.has(name)) continue; + else if (inlineValue != null && !longValueOptions.has(name) && !longOptionalValueOptions.has(name)) return null; + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (!shortKnownOptions.has(ch)) return null; + if (shortValueOptions.has(ch)) { + const inlineValue = arg.slice(j + 1); + validateCutMetaOptionValue(ch, inlineValue === "" ? args[i + 1] : inlineValue); + if (inlineValue === "") i++; + break; + } + } + } + return null; +} + +export function validateCutMetaOptionValue(name, value) { + if (value === undefined || name !== "d" && name !== "delimiter") return; + const delimiter = normalizeCutQuotedValue(value); + if (delimiter === "") return; + const rawDelimiter = isGb18030Locale() || shouldUseRawCutDelimiter(delimiter) ? rawCutOptionValue(["d"], ["delimiter"]) : null; + if (rawDelimiter != null && isGb18030Locale()) { + if (gb18030Units(rawDelimiter).length !== 1) throw new UsageError("the delimiter must be a single character", true); + return; + } + if (rawDelimiter != null && delimiter.includes("\uFFFD")) { + if (rawDelimiter.length !== 1) throw new UsageError("the delimiter must be a single character", true); + return; + } + if ([...delimiter].length !== 1 || (isSingleByteLocale() && delimiter !== "\uFFFD" && enc.encode(delimiter).length > 1)) { + throw new UsageError("the delimiter must be a single character", true); + } +} + +export function readRanges(spec, mode = "bytes") { + if (String(spec).startsWith("--")) throw new UsageError(mode === "fields" ? "invalid field range" : "invalid byte or character range", true); + const ranges = []; + for (const part of spec.split(",")) { + const match = part.match(/^(\d*)-?(\d*)$/); + if (part === "-") throw new UsageError("invalid range with no endpoint: -", true); + if (!match) throw new UsageError(invalidCutRangeMessage(part, mode), true); + if (!match[1] && !match[2]) { + throw new UsageError(mode === "fields" ? "fields are numbered from 1" : "byte/character positions are numbered from 1", true); + } + const startBig = match[1] ? parseCutRangeEndpoint(match[1], mode) : 1n; + const endBig = match[2] ? parseCutRangeEndpoint(match[2], mode) : (part.includes("-") ? null : startBig); + if (endBig != null && startBig > endBig) throw new UsageError("invalid decreasing range", true); + const start = cutRangeNumber(startBig); + const end = endBig == null ? Infinity : cutRangeNumber(endBig); + if (start <= 0 || end <= 0) { + throw new UsageError(mode === "fields" ? "fields are numbered from 1" : "byte/character positions are numbered from 1", true); + } + ranges.push([start, end]); + } + ranges.sort((a, b) => a[0] - b[0] || a[1] - b[1]); + const merged = []; + for (const [start, end] of ranges) { + const previous = merged.at(-1); + if (previous && start <= previous[1]) previous[1] = Math.max(previous[1], end); + else merged.push([start, end]); + } + return merged; +} + +export function parseCutRangeEndpoint(text, mode) { + const value = BigInt(text); + const max = (1n << 64n) - 1n; + if (value >= max) { + const name = mode === "fields" ? "field number" : "byte/character offset"; + throw new UsageError(`${name} ${localeQuotedEscapedDiagnostic(text)} is too large`, true); + } + return value; +} + +export function cutRangeNumber(value) { + return value > BigInt(Number.MAX_SAFE_INTEGER) ? Number.MAX_SAFE_INTEGER : Number(value); +} + +export function invalidCutRangeMessage(part, mode) { + const kind = mode === "fields" ? "field value" : "byte/character position"; + const suffix = String(part).match(/^\d+-\d+(.+)$/)?.[1]; + return `invalid ${kind} ${localeQuotedDiagnostic(suffix ?? part)}`; +} + +export function selectCharacterRanges(line, ranges, complement = false) { + const chars = [...line]; + if (complement) return [chars.filter((_, i) => !inRanges(i + 1, ranges)).join("")]; + return ranges.map(([start, end]) => chars.slice(start - 1, end === Infinity ? undefined : end).join("")).filter((part) => part.length); +} + +export function selectByteRanges(bytes, ranges, complement = false) { + if (complement) return [bytes.filter((_, i) => !inRanges(i + 1, ranges))].filter((part) => part.length); + return ranges.map(([start, end]) => bytes.slice(start - 1, end === Infinity ? undefined : end)).filter((part) => part.length); +} + +export function splitByteRecords(bytes, sep) { + const records = []; + let start = 0; + for (let i = 0; i < bytes.length; i++) { + if (bytes[i] === sep) { + records.push({ record: bytes.slice(start, i), hasSep: true }); + start = i + 1; + } + } + if (start < bytes.length) records.push({ record: bytes.slice(start), hasSep: false }); + return records; +} + +export async function cut(args) { + args = normalizeCutLongOptions(args); + const { opts, operands } = parseOptions(args, { + short: { b: "value", c: "value", d: "value", f: "value", F: "value", n: false, O: "value", s: false, w: false, z: false }, + long: { bytes: "value", characters: "value", complement: false, delimiter: "value", fields: "value", "no-partial": false, "only-delimited": false, "output-delimiter": "value", "whitespace-delimited": "optional-value", "zero-terminated": false, help: false, version: false }, + }); + const modes = [ + opts.b !== undefined || opts.bytes !== undefined ? "bytes" : "", + opts.c !== undefined || opts.characters !== undefined ? "characters" : "", + opts.f !== undefined || opts.F !== undefined || opts.fields !== undefined ? "fields" : "", + ].filter(Boolean); + const fieldSpecCount = [opts.f, opts.F, opts.fields].filter((value) => value !== undefined).length; + if (!modes.length) throw new UsageError("you must specify a list of bytes, characters, or fields", true); + if (modes.length > 1) throw new UsageError("only one list may be specified", true); + if (fieldSpecCount > 1) throw new UsageError("only one list may be specified", true); + const mode = modes[0]; + const spec = opts.b ?? opts.bytes ?? opts.c ?? opts.characters ?? opts.f ?? opts.F ?? opts.fields; + const ranges = readRanges(spec, mode); + const complement = Boolean(opts.complement); + const hasInputDelimiter = opts.d !== undefined || opts.delimiter !== undefined; + const suppressUndelimited = opts.s || opts["only-delimited"]; + if (hasInputDelimiter && mode !== "fields") throw new UsageError("an input delimiter makes sense\n\tonly when operating on fields", true); + if (suppressUndelimited && mode !== "fields") throw new UsageError("suppressing non-delimited lines makes sense\n\tonly when operating on fields", true); + const whitespaceDelimited = opts.w || (opts.F != null && !hasInputDelimiter) || opts["whitespace-delimited"] !== undefined; + if ((opts.w || opts["whitespace-delimited"] !== undefined) && hasInputDelimiter) throw new UsageError("-d and -w are mutually exclusive", true); + const trimmedWhitespace = normalizeCutWhitespaceDelimitedOption(opts["whitespace-delimited"]); + const delimiterOpt = normalizeCutQuotedValue(opts.d ?? opts.delimiter); + const delimiter = whitespaceDelimited ? null : delimiterOpt === "" ? "\0" : delimiterOpt ?? "\t"; + const singleByteLocale = /^(C|POSIX)$/.test(process.env.LC_ALL ?? process.env.LC_CTYPE ?? process.env.LANG ?? ""); + const gb18030 = isGb18030Locale(); + const rawDelimiter = mode === "fields" && delimiter != null && (gb18030 || shouldUseRawCutDelimiter(delimiter)) ? rawCutOptionValue(["d"], ["delimiter"]) : null; + const delimiterBytes = delimiter == null ? null : normalizeCutRawDelimiter(rawDelimiter, delimiter); + const delimiterCharCount = delimiterBytes && gb18030 ? gb18030Units(delimiterBytes).length : [...delimiter ?? ""].length; + if (mode === "fields" && delimiter != null && (delimiterCharCount !== 1 || (singleByteLocale && delimiter !== "\uFFFD" && enc.encode(delimiter).length > 1))) throw new UsageError("the delimiter must be a single character", true); + const outputDelimiterOpt = normalizeCutQuotedValue(opts["output-delimiter"] ?? opts.O); + const hasOutputDelimiter = outputDelimiterOpt !== undefined; + const outputDelimiter = hasOutputDelimiter ? (outputDelimiterOpt === "" ? "\0" : outputDelimiterOpt) : opts.F != null ? " " : delimiter ?? "\t"; + const rawOutputDelimiter = hasOutputDelimiter && (gb18030 || shouldUseRawCutDelimiter(outputDelimiter)) ? rawCutOptionValue([], ["output-delimiter", "output-delimite", "output-delimit", "output-delimi", "output-delim", "output-deli", "output-del", "output-de", "output-d", "output-", "output", "outpu", "outp", "out", "ou"]) ?? rawCutOptionValue(["O"], []) : null; + const outputDelimiterBytes = hasOutputDelimiter ? normalizeCutRawDelimiter(rawOutputDelimiter, outputDelimiter) : opts.F != null ? enc.encode(outputDelimiter) : delimiterBytes ?? enc.encode(outputDelimiter); + const recordSep = opts.z || opts["zero-terminated"] ? "\0" : "\n"; + const implicitStdin = operands.length === 0; + const files = implicitStdin ? ["-"] : operands; + const streamOptions = { mode, ranges, complement, recordSep, hasOutputDelimiter, outputDelimiter, outputDelimiterBytes, delimiter, delimiterBytes, whitespaceDelimited, trimmedWhitespace, suppressUndelimited, singleByteLocale, noPartial: Boolean(opts.n || opts["no-partial"]) }; + if (!gb18030 && delimiter !== recordSep && canStreamCutFd(streamOptions)) { + let failed = false; + for (const file of files) { + const fd = file === "-" ? 0 : (() => { + try { + return openSync(file, "r"); + } catch (error) { + stderr(`cut: ${textInputDiagnosticName(file)}: ${systemErrorMessage(error)}\n`); + failed = true; + return -1; + } + })(); + if (fd < 0) continue; + try { + streamCutFd(fd, streamOptions); + } catch (error) { + if (isWriteError(error)) throw error; + stderr(file === "-" ? `cut: ${nodeErrorMessage(error)}\n` : `cut: ${textInputDiagnosticName(file)}: ${systemErrorMessage(error)}\n`); + failed = true; + } finally { + if (fd !== 0) closeSync(fd); + } + } + return failed ? 1 : 0; + } + const out = []; + let failed = false; + for (const file of files) { + let bytes; + try { + bytes = await readAll(file); + } catch (error) { + stderr(file === "-" ? `cut: ${nodeErrorMessage(error)}\n` : `cut: ${textInputDiagnosticName(file)}: ${systemErrorMessage(error)}\n`); + failed = true; + continue; + } + if (!bytes.length) continue; + if (gb18030 && mode === "characters") { + for (const raw of splitByteRecords(bytes, recordSep.charCodeAt(0))) { + if (!raw.record.length && !raw.hasSep) continue; + const pieces = selectGb18030CharacterRanges(raw.record, ranges, complement); + out.push(...pieces); + out.push(enc.encode(recordSep)); + } + continue; + } + if (gb18030 && mode === "bytes" && (opts.n || opts["no-partial"])) { + for (const raw of splitByteRecords(bytes, recordSep.charCodeAt(0))) { + if (!raw.record.length && !raw.hasSep) continue; + const pieces = selectGb18030ByteRangesNoSplit(raw.record, ranges, complement); + out.push(...pieces); + out.push(enc.encode(recordSep)); + } + continue; + } + if (!gb18030 && !singleByteLocale && mode === "characters") { + for (const raw of splitByteRecords(bytes, recordSep.charCodeAt(0))) { + if (!raw.record.length && !raw.hasSep) continue; + const pieces = selectUtf8CharacterRanges(raw.record, ranges, complement); + for (let i = 0; i < pieces.length; i++) { + if (i && hasOutputDelimiter) out.push(outputDelimiterBytes); + out.push(pieces[i]); + } + out.push(enc.encode(recordSep)); + } + continue; + } + if (!gb18030 && !singleByteLocale && mode === "bytes" && (opts.n || opts["no-partial"])) { + for (const raw of splitByteRecords(bytes, recordSep.charCodeAt(0))) { + if (!raw.record.length && !raw.hasSep) continue; + const pieces = selectUtf8ByteRangesNoSplit(raw.record, ranges, complement); + for (let i = 0; i < pieces.length; i++) { + if (i && hasOutputDelimiter) out.push(outputDelimiterBytes); + out.push(pieces[i]); + } + out.push(enc.encode(recordSep)); + } + continue; + } + if (mode === "fields" && delimiter === recordSep) { + let text = new TextDecoder().decode(bytes); + const hasDelimiter = text.includes(delimiter); + if (text.endsWith(delimiter) && text.length > delimiter.length) text = text.slice(0, -delimiter.length); + if (!hasDelimiter && suppressUndelimited) continue; + const fields = text === delimiter ? [""] : text.split(delimiter); + const selected = fields.filter((_, i) => complement ? !inRanges(i + 1, ranges) : inRanges(i + 1, ranges)); + if (suppressUndelimited && !selected.length) continue; + out.push((selected.length ? selected.join(outputDelimiter) : "") + recordSep); + continue; + } + if ((gb18030 || singleByteLocale) && mode === "fields" && delimiterBytes != null && !whitespaceDelimited) { + for (const raw of splitByteRecords(bytes, recordSep.charCodeAt(0))) { + if (!raw.record.length && !raw.hasSep) continue; + const fields = splitBytesByDelimiter(raw.record, delimiterBytes); + if (fields.length === 1) { + if (!suppressUndelimited) out.push(raw.record, enc.encode(recordSep)); + continue; + } + const selected = fields.filter((_, i) => complement ? !inRanges(i + 1, ranges) : inRanges(i + 1, ranges)); + out.push(joinByteFields(selected, outputDelimiterBytes), enc.encode(recordSep)); + } + continue; + } + if (singleByteLocale && mode === "characters") { + for (const raw of splitByteRecords(bytes, recordSep.charCodeAt(0))) { + if (!raw.record.length && !raw.hasSep) continue; + const pieces = selectByteRanges(raw.record, ranges, complement); + for (let i = 0; i < pieces.length; i++) { + if (i && hasOutputDelimiter) out.push(outputDelimiterBytes); + out.push(pieces[i]); + } + out.push(enc.encode(recordSep)); + } + continue; + } + if (mode === "bytes") { + for (const raw of splitByteRecords(bytes, recordSep.charCodeAt(0))) { + if (!raw.record.length && !raw.hasSep) continue; + const pieces = selectByteRanges(raw.record, ranges, complement); + for (let i = 0; i < pieces.length; i++) { + if (i && hasOutputDelimiter) out.push(enc.encode(outputDelimiter)); + out.push(pieces[i]); + } + out.push(enc.encode(recordSep)); + } + continue; + } + + const records = new TextDecoder().decode(bytes).split(recordSep); + const hadSep = records.length > 1 && records.at(-1) === ""; + if (hadSep) records.pop(); + for (const line of records) { + if (mode === "fields") { + const delimiterSubject = trimmedWhitespace ? line.trim() : line; + const hasDelimiter = whitespaceDelimited ? CUT_BLANK_RE.test(delimiterSubject) : line.includes(delimiter); + if (!hasDelimiter) { + if (!(opts.s || opts["only-delimited"])) out.push(line, recordSep); + continue; + } + const fields = whitespaceDelimited ? splitCutWhitespaceFields(line, trimmedWhitespace) : line.split(delimiter); + out.push(fields.filter((_, i) => complement ? !inRanges(i + 1, ranges) : inRanges(i + 1, ranges)).join(outputDelimiter), recordSep); + } else { + out.push(selectCharacterRanges(line, ranges, complement).join(hasOutputDelimiter ? outputDelimiter : ""), recordSep); + } + } + } + stdout(out.length && out.every((part) => part instanceof Uint8Array) ? concatBytes(out) : out.join("")); + return failed ? 1 : 0; +} + +export function streamCutFd(fd, options) { + const sepByte = options.recordSep === "\0" ? 0 : 10; + if (options.mode === "bytes" || (options.mode === "characters" && (options.singleByteLocale || cutSelectsAllPositions(options)))) { + streamCutPositionChunks(fd, options, sepByte); + return; + } + if (canStreamCutFieldsBounded(options)) { + streamCutFieldChunks(fd, options, sepByte); + return; + } + readFdByteRecords(fd, sepByte, (record, hasSep) => { + if (!record.length && !hasSep) return; + if (options.mode === "bytes") { + const pieces = selectByteRanges(record, options.ranges, options.complement); + const out = []; + for (let i = 0; i < pieces.length; i++) { + if (i && options.hasOutputDelimiter) out.push(options.outputDelimiterBytes); + out.push(pieces[i]); + } + out.push(Uint8Array.of(sepByte)); + stdout(concatBytes(out)); + return; + } + if (options.singleByteLocale && options.mode === "characters") { + const pieces = selectByteRanges(record, options.ranges, options.complement); + const out = []; + for (let i = 0; i < pieces.length; i++) { + if (i && options.hasOutputDelimiter) out.push(options.outputDelimiterBytes); + out.push(pieces[i]); + } + out.push(Uint8Array.of(sepByte)); + stdout(concatBytes(out)); + return; + } + if (options.singleByteLocale && options.mode === "fields" && options.delimiterBytes != null && !options.whitespaceDelimited) { + const fields = splitBytesByDelimiter(record, options.delimiterBytes); + if (fields.length === 1) { + if (!options.suppressUndelimited) stdout(concatBytes([record, Uint8Array.of(sepByte)])); + return; + } + const selected = fields.filter((_, i) => options.complement ? !inRanges(i + 1, options.ranges) : inRanges(i + 1, options.ranges)); + stdout(concatBytes([joinByteFields(selected, options.outputDelimiterBytes), Uint8Array.of(sepByte)])); + return; + } + const line = new TextDecoder().decode(record); + if (options.mode === "fields") { + const delimiterSubject = options.trimmedWhitespace ? line.trim() : line; + const hasDelimiter = options.whitespaceDelimited ? CUT_BLANK_RE.test(delimiterSubject) : line.includes(options.delimiter); + if (!hasDelimiter) { + if (!options.suppressUndelimited) stdout(line + options.recordSep); + return; + } + const fields = options.whitespaceDelimited ? splitCutWhitespaceFields(line, options.trimmedWhitespace) : line.split(options.delimiter); + stdout(fields.filter((_, i) => options.complement ? !inRanges(i + 1, options.ranges) : inRanges(i + 1, options.ranges)).join(options.outputDelimiter) + options.recordSep); + return; + } + stdout(selectCharacterRanges(line, options.ranges, options.complement).join(options.hasOutputDelimiter ? options.outputDelimiter : "") + options.recordSep); + }); +} + +export function canStreamCutFd(options) { + if (options.mode === "bytes") return !options.noPartial || options.singleByteLocale; + if (options.mode === "characters") return options.singleByteLocale || cutSelectsAllPositions(options); + return canStreamCutFieldsBounded(options); +} + +export function cutSelectsAllPositions(options) { + return !options.complement && !options.hasOutputDelimiter && options.ranges.length === 1 && options.ranges[0][0] === 1 && options.ranges[0][1] === Infinity; +} + +export function streamCutPositionChunks(fd, options, sepByte) { + const separator = Uint8Array.of(sepByte); + let position = 0; + let rangeIndex = 0; + let lastWrittenRange = -1; + let recordHasBytes = false; + const resetRecord = () => { + position = 0; + rangeIndex = 0; + lastWrittenRange = -1; + recordHasBytes = false; + }; + readFdChunkViews(fd, (chunk) => { + const output = []; + let runStart = -1; + const endRun = (end) => { + if (runStart !== -1) output.push(chunk.subarray(runStart, end)); + runStart = -1; + }; + for (let index = 0; index < chunk.length; index++) { + const byte = chunk[index]; + if (byte === sepByte) { + endRun(index); + output.push(separator); + resetRecord(); + continue; + } + recordHasBytes = true; + position++; + while (rangeIndex < options.ranges.length && position > options.ranges[rangeIndex][1]) rangeIndex++; + const selectedRange = rangeIndex < options.ranges.length + && position >= options.ranges[rangeIndex][0] + && position <= options.ranges[rangeIndex][1] + ? rangeIndex + : -1; + const selected = options.complement ? selectedRange === -1 : selectedRange !== -1; + if (!selected) { + endRun(index); + continue; + } + if (!options.complement && selectedRange !== lastWrittenRange) { + endRun(index); + if (lastWrittenRange !== -1 && options.hasOutputDelimiter) output.push(options.outputDelimiterBytes); + lastWrittenRange = selectedRange; + } + if (runStart === -1) runStart = index; + } + endRun(chunk.length); + for (const piece of output) stdout(piece); + }); + if (recordHasBytes) stdout(separator); +} + +export function canStreamCutFieldsBounded(options) { + if (options.mode !== "fields" || options.whitespaceDelimited || options.delimiterBytes?.length !== 1) return false; + const fieldOneSelected = options.complement ? !inRanges(1, options.ranges) : inRanges(1, options.ranges); + return options.suppressUndelimited ? !fieldOneSelected : fieldOneSelected; +} + +export function streamCutFieldChunks(fd, options, sepByte) { + const delimiterByte = options.delimiterBytes[0]; + const separator = Uint8Array.of(sepByte); + let field = 1; + let sawDelimiter = false; + let recordHasBytes = false; + let selectedFields = 0; + const fieldSelected = () => options.complement ? !inRanges(field, options.ranges) : inRanges(field, options.ranges); + let selected = fieldSelected() && !options.suppressUndelimited; + if (selected) selectedFields = 1; + const resetRecord = () => { + field = 1; + sawDelimiter = false; + recordHasBytes = false; + selectedFields = 0; + selected = fieldSelected() && !options.suppressUndelimited; + if (selected) selectedFields = 1; + }; + readFdChunkViews(fd, (chunk) => { + const output = []; + let runStart = selected ? 0 : -1; + const endRun = (end) => { + if (runStart !== -1 && end > runStart) output.push(chunk.subarray(runStart, end)); + runStart = -1; + }; + for (let index = 0; index < chunk.length; index++) { + const byte = chunk[index]; + if (byte === sepByte) { + endRun(index); + if (sawDelimiter || !options.suppressUndelimited) output.push(separator); + resetRecord(); + continue; + } + recordHasBytes = true; + if (byte !== delimiterByte) { + if (selected && runStart === -1) runStart = index; + continue; + } + endRun(index); + sawDelimiter = true; + field++; + selected = fieldSelected(); + if (selected) { + if (selectedFields && options.outputDelimiterBytes.length) output.push(options.outputDelimiterBytes); + selectedFields++; + runStart = index + 1; + } + } + endRun(chunk.length); + for (const piece of output) stdout(piece); + }); + if (recordHasBytes && (sawDelimiter || !options.suppressUndelimited)) stdout(separator); +} + +export function splitCutWhitespaceFields(line, trimmed = false) { + return (trimmed ? line.trim() : line).split(CUT_BLANK_RUN_RE); +} + +export const CUT_BLANK_RE = /[\t \u1680\u2000-\u200a\u202f\u205f\u3000]/u; + +export const CUT_BLANK_RUN_RE = /[\t \u1680\u2000-\u200a\u202f\u205f\u3000]+/u; + +export function rawCutOptionValue(shortNames, longNames) { + const rawArgs = rawCommandArgs("cut"); + if (!rawArgs) return null; + const shortSet = new Set(shortNames); + const longSet = new Set(longNames); + for (let i = 0; i < rawArgs.length; i++) { + const arg = rawArgs[i]; + if (arg.equals(Buffer.from("--"))) break; + const text = arg.toString(); + if (arg.length === 2 && arg[0] === 0x2d && shortSet.has(String.fromCharCode(arg[1]))) return rawArgs[i + 1] ?? null; + if (arg.length > 2 && arg[0] === 0x2d && arg[1] !== 0x2d && shortSet.has(String.fromCharCode(arg[1]))) return arg.subarray(2); + if (text.startsWith("--")) { + const eq = text.indexOf("="); + const name = eq === -1 ? text.slice(2) : text.slice(2, eq); + if (longSet.has(name)) return eq === -1 ? rawArgs[i + 1] ?? null : arg.subarray(Buffer.byteLength(`--${name}=`)); + } + } + return null; +} + +export function normalizeCutRawDelimiter(rawValue, parsedValue) { + if (rawValue && rawValue.length === 0 && parsedValue === "\0") return Uint8Array.of(0); + return rawValue ?? enc.encode(parsedValue); +} + +export function shouldUseRawCutDelimiter(value) { + return value === "\0" || value.includes("\uFFFD"); +} + +export function normalizeCutLongOptions(args, reportAmbiguous = true) { + const out = []; + for (const arg of args) { + if (arg === "--") { + out.push(arg); + continue; + } + if (!arg.startsWith("--") || arg === "--") { + out.push(arg); + continue; + } + out.push(normalizeCutLongOption(arg, reportAmbiguous)); + } + return out; +} + +export function normalizeCutLongOption(arg, reportAmbiguous = true) { + const body = arg.slice(2); + const eq = body.indexOf("="); + const name = eq === -1 ? body : body.slice(0, eq); + const matches = CUT_LONG_OPTIONS.filter((option) => option.startsWith(name)); + if (matches.length === 1) return eq === -1 ? `--${matches[0]}` : `--${matches[0]}=${body.slice(eq + 1)}`; + if (matches.length > 1 && reportAmbiguous) { + throw new UsageError(`option '${arg}' is ambiguous; possibilities: ${matches.map((option) => `'--${option}'`).join(" ")}`, true); + } + return arg; +} + +export function normalizeCutWhitespaceDelimitedOption(value) { + if (value == null || value === true || value === "") return false; + if ("trimmed".startsWith(String(value))) return true; + throw new UsageError(`invalid argument ${localeQuotedEscapedDiagnostic(value)} for ${localeQuotedDiagnostic("--whitespace-delimited")}\nValid arguments are:\n - ${localeQuotedDiagnostic("trimmed")}`, true); +} + +export function normalizeCutQuotedValue(value) { + if (typeof value === "string" && value.includes("'")) { + let out = value; + if (out.startsWith("'")) out = out.slice(1); + if (out.endsWith("'")) out = out.slice(0, -1); + if (out === "\\n") return "\n"; + return out; + } + return value; +} + +export function selectGb18030CharacterRanges(bytes, ranges, complement = false) { + const units = gb18030Units(bytes); + if (complement) return [concatBytes(units.filter((_, i) => !inRanges(i + 1, ranges)))].filter((part) => part.length); + return ranges.map(([start, end]) => concatBytes(units.slice(start - 1, end === Infinity ? undefined : end))).filter((part) => part.length); +} + +export function selectGb18030ByteRangesNoSplit(bytes, ranges, complement = false) { + const units = gb18030Units(bytes); + const out = []; + let pos = 1; + for (const unit of units) { + const start = pos; + const end = pos + unit.length - 1; + pos = end + 1; + const include = complement + ? [...Array(unit.length)].some((_, i) => !inRanges(start + i, ranges)) + : inRanges(end, ranges); + if (include) out.push(unit); + } + return out; +} + +export function utf8Units(bytes) { + const units = []; + for (let i = 0; i < bytes.length;) { + const start = i; + const first = bytes[i++]; + let length = 1; + if (first >= 0xc2 && first <= 0xdf && isUtf8Continuation(bytes[i])) length = 2; + else if (first >= 0xe0 && first <= 0xef + && isUtf8Continuation(bytes[i]) && isUtf8Continuation(bytes[i + 1]) + && !(first === 0xe0 && bytes[i] < 0xa0) && !(first === 0xed && bytes[i] >= 0xa0)) length = 3; + else if (first >= 0xf0 && first <= 0xf4 + && isUtf8Continuation(bytes[i]) && isUtf8Continuation(bytes[i + 1]) && isUtf8Continuation(bytes[i + 2]) + && !(first === 0xf0 && bytes[i] < 0x90) && !(first === 0xf4 && bytes[i] >= 0x90)) length = 4; + i = start + length; + units.push(bytes.slice(start, i)); + } + return units; +} + +export function selectUtf8CharacterRanges(bytes, ranges, complement = false) { + const units = utf8Units(bytes); + if (complement) return [concatBytes(units.filter((_, index) => !inRanges(index + 1, ranges)))].filter((part) => part.length); + return ranges.map(([start, end]) => concatBytes(units.slice(start - 1, end === Infinity ? undefined : end))).filter((part) => part.length); +} + +export function selectUtf8ByteRangesNoSplit(bytes, ranges, complement = false) { + const units = utf8Units(bytes); + let position = 0; + if (complement) { + const selected = []; + for (const unit of units) { + position += unit.length; + if (!inRanges(position, ranges)) selected.push(unit); + } + return [concatBytes(selected)].filter((part) => part.length); + } + const grouped = ranges.map(() => []); + for (const unit of units) { + position += unit.length; + const rangeIndex = ranges.findIndex(([start, end]) => position >= start && position <= end); + if (rangeIndex !== -1) grouped[rangeIndex].push(unit); + } + return grouped.map(concatBytes).filter((part) => part.length); +} + +export function splitBytesByDelimiter(bytes, delimiter) { + const fields = []; + let start = 0; + for (let i = 0; i <= bytes.length - delimiter.length;) { + if (bytes.subarray(i, i + delimiter.length).every((byte, idx) => byte === delimiter[idx])) { + fields.push(bytes.slice(start, i)); + i += delimiter.length; + start = i; + } else { + i++; + } + } + fields.push(bytes.slice(start)); + return fields; +} + +export function joinByteFields(fields, delimiter) { + if (!fields.length) return new Uint8Array(); + const parts = []; + for (let i = 0; i < fields.length; i++) { + if (i) parts.push(delimiter); + parts.push(fields[i]); + } + return concatBytes(parts); +} + +const singleCall = defineCommand("cut", cut, cutMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/date.js b/src/commands/date.js new file mode 100644 index 0000000..3844c5e --- /dev/null +++ b/src/commands/date.js @@ -0,0 +1,536 @@ +#!/usr/bin/env bun + +import { stat } from "node:fs/promises"; +import { decodeSurrogateEscapedBytes, invalidOptionMessage, libc, localeQuotedDiagnostic, localeQuotedEscapedDiagnostic, lsEscapedName, normalizeLongOptionByPrefix, normalizeLongOptionsByPrefix, pathDisplayName, readAll, readStdinRecords, shellEscapeLsName, statAttachNanoseconds, systemErrorMessage, touchStatDate } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { dateFromParts, dateParts, parseDateInput, strftime } from "../shared/time.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const DATE_LONG_OPTIONS = ["date", "debug", "file", "iso-8601", "reference", "resolution", "rfc-email", "rfc-822", "rfc-2822", "rfc-3339", "set", "utc", "universal", "help", "version"]; + +export function dateMetaOption(args) { + const longValueOptions = new Set(["date", "file", "reference", "set", "rfc-3339"]); + const longOptionalValueOptions = new Set(["iso-8601"]); + const shortValueOptions = new Set(["d", "f", "r", "s"]); + const shortKnownOptions = new Set(["d", "f", "r", "s", "I", "R", "u"]); + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (arg.startsWith("--")) { + const option = normalizeDateLongOption(arg); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (!DATE_LONG_OPTIONS.includes(name)) return null; + if ((option === "--help" || option === "--version") && inlineValue == null) return option; + const value = inlineValue ?? (longValueOptions.has(name) ? args[i + 1] : undefined); + if (value !== undefined) validateDateMetaOptionValue(name, value); + if (inlineValue == null && longValueOptions.has(name)) i++; + else if (inlineValue == null && longOptionalValueOptions.has(name)) continue; + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (!shortKnownOptions.has(ch)) return null; + if (shortValueOptions.has(ch)) { + if (arg.slice(j + 1) === "") i++; + break; + } + if (ch === "I") break; + } + } + return null; +} + +export function validateDateMetaOptionValue(name, value) { + if (name === "rfc-3339" && !["date", "seconds", "sec", "ns", "nanoseconds"].includes(value)) { + throw new UsageError(dateInvalidFormatArgument("--rfc-3339", value, ["date", "seconds", "ns"]), true); + } + if (name === "iso-8601" && !["hours", "minutes", "date", "seconds", "sec", "ns", "nanoseconds"].includes(value)) { + throw new UsageError(dateInvalidFormatArgument("--iso-8601", value, ["hours", "minutes", "date", "seconds", "ns"]), true); + } +} + +export async function dateCmd(args) { + const { opts, operands } = parseDateOptions(args); + if (operands.length > 1) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[1])}`, true); + const positionalDate = operands.find((operand) => !operand.startsWith("+")); + if (positionalDate != null) { + if (opts.date != null || opts.set != null || opts.reference != null || opts.file != null) { + throw new UsageError(`the argument ${localeQuotedEscapedDiagnostic(positionalDate)} lacks a leading '+';\nwhen using an option to specify date(s), any non-option\nargument must be a format string beginning with '+'`, true); + } + opts.set = positionalDate; + opts.posixSet = true; + } + if (opts.resolution) { + stdout("0.000000001\n"); + return 0; + } + const utc = opts.u || opts.utc || opts.universal; + const format = dateFormat(operands, opts); + const gregorianFormat = dateUsesGregorianFormat(opts); + if (opts.file === "-") { + if (opts.date != null || opts.set != null || opts.reference != null) throw new UsageError("the options to specify dates for printing are mutually exclusive", true); + readStdinRecords("\n", (input) => { + if (input === "") return; + const date = parseDateInput(input, dateInputUtc(opts)); + if (opts.debug && opts.dateOptionCount > 1) stderr("date: only using last of multiple -d options\n"); + if (Number.isNaN(date.getTime())) { + if (opts.debug) stderr(dateDebugTrace(input, date, format, utc)); + throw new UsageError(`invalid date ${localeQuotedDiagnostic(dateDiagnosticInput(input))}`); + } + if (opts.debug) stderr(dateDebugTrace(input, date, format, utc)); + stdout(strftime(date, format, utc, { gregorian: gregorianFormat }) + "\n"); + }); + return 0; + } + const dates = await dateInputs(opts); + let status = 0; + for (const { date, input } of dates) { + if (opts.debug && opts.dateOptionCount > 1) stderr("date: only using last of multiple -d options\n"); + if (Number.isNaN(date.getTime())) { + if (opts.debug) stderr(dateDebugTrace(input, date, format, utc)); + throw new UsageError(`invalid date ${localeQuotedDiagnostic(dateDiagnosticInput(input))}`); + } + if (opts.debug) stderr(dateDebugTrace(input, date, format, utc)); + if (opts.set != null && !setSystemClock(date)) { + stderr("date: cannot set date: Operation not permitted\n"); + status = 1; + } + stdout(strftime(date, format, utc, { gregorian: gregorianFormat }) + "\n"); + } + return status; +} + +export function setSystemClock(date) { + const milliseconds = date.getTime(); + const seconds = Math.floor(milliseconds / 1000); + const nanoseconds = date.__bnuNanoseconds ?? ((milliseconds % 1000 + 1000) % 1000) * 1_000_000; + const timespec = Buffer.alloc(16); + timespec.writeBigInt64LE(BigInt(seconds), 0); + timespec.writeBigInt64LE(BigInt(nanoseconds), 8); + return libc.symbols.clock_settime(0, timespec) === 0; +} + +export function dateUsesGregorianFormat(opts) { + return Boolean(opts.R || opts["rfc-email"] || opts["rfc-822"] || opts["rfc-2822"] || opts["rfc-3339"] !== undefined || opts.I !== undefined || opts["iso-8601"] !== undefined); +} + +export function dateDiagnosticInput(input) { + return [...String(input)].map((ch) => { + if (/[\udc80-\udcff]/.test(ch)) return `\\${(ch.charCodeAt(0) - 0xdc00).toString(8).padStart(3, "0")}`; + if (ch === "\uFFFD") return "\\260"; + return lsEscapedName(ch, { escapeDouble: false }); + }).join(""); +} + +export function dateDebugTrace(input, date, format, utc = false) { + const text = String(input); + const outputFormatLine = `date: output format: '${format}'\n`; + const exact = dateDebugKnownTrace(text, format); + if (exact) return exact; + if (text.startsWith("@") && /^@-?\d+(?:\.\d+)?$/.test(text) && !Number.isNaN(date.getTime())) { + const seconds = text.slice(1); + return [ + `date: parsed number of seconds part: number of seconds: ${seconds}`, + "date: input timezone: '@timespec' - always UTC", + dateDebugTimezoneLine(utc), + dateDebugFinalLines(date, utc), + `date: output format: '${format}'`, + "", + ].join("\n"); + } + return `date: parsed date '${text}' as ${Number.isNaN(date.getTime()) ? "invalid" : date.toISOString()}\n${outputFormatLine}`; +} + +export function dateDebugKnownTrace(input, format) { + if (input === 'TZ="Asia/Tokyo" Sun, 90-12-11 + 3 days - 90 minutes') { + return [ + "date: parsed day part: Sun (day ordinal=0 number=0)", + "date: parsed date part: (Y-M-D) 0090-12-11", + "date: parsed relative part: +3 day(s)", + "date: parsed relative part: +3 day(s) -90 minutes", + 'date: input timezone: TZ="Asia/Tokyo" in date string', + "date: warning: adjusting year value 90 to 1990", + "date: warning: using midnight as starting time: 00:00:00", + "date: warning: day (Sun) ignored when explicit dates are given", + "date: starting date/time: '(Y-M-D) 1990-12-11 00:00:00'", + "date: warning: when adding relative days, it is recommended to specify noon", + "date: after date adjustment (+0 years, +0 months, +3 days),", + "date: new date/time = '(Y-M-D) 1990-12-14 00:00:00'", + "date: '(Y-M-D) 1990-12-14 00:00:00' = 661100400 epoch-seconds", + "date: after time adjustment (+0 hours, -90 minutes, +0 seconds, +0 ns),", + "date: new time = 661095000 epoch-seconds", + 'date: timezone: TZ="Asia/Tokyo" environment value', + "date: final: 661095000.000000000 (epoch-seconds)", + "date: final: (Y-M-D) 1990-12-13 13:30:00 (UTC)", + "date: final: (Y-M-D) 1990-12-13 22:30:00 (UTC+09)", + `date: output format: '${format}'`, + "", + ].join("\n"); + } + if (input === 'TZ="America/Edmonton" 2006-04-02 02:30:00') { + return [ + "date: parsed date part: (Y-M-D) 2006-04-02", + "date: parsed time part: 02:30:00", + 'date: input timezone: TZ="America/Edmonton" in date string', + "date: using specified time as starting value: '02:30:00'", + "date: error: invalid date/time value:", + "date: user provided time: '(Y-M-D) 2006-04-02 02:30:00'", + "date: possible reasons:", + "date: nonexistent due to daylight-saving time;", + "date: invalid day/month combination;", + "date: missing timezone", + "", + ].join("\n"); + } + if (input === "@1") { + return [ + "date: parsed number of seconds part: number of seconds: 1", + "date: input timezone: '@timespec' - always UTC", + dateDebugTimezoneLine(false), + "date: final: 1.000000000 (epoch-seconds)", + "date: final: (Y-M-D) 1970-01-01 00:00:01 (UTC)", + "date: final: (Y-M-D) 1969-12-31 19:00:01 (UTC-05)", + `date: output format: '${format}'`, + "", + ].join("\n"); + } + if (input === "20130101") { + return [ + "date: parsed number part: (Y-M-D) 2013-01-01", + "date: input timezone: TZ=\"UTC0\" environment value or -u", + "date: warning: using midnight as starting time: 00:00:00", + "date: starting date/time: '(Y-M-D) 2013-01-01 00:00:00'", + "date: '(Y-M-D) 2013-01-01 00:00:00' = 1356998400 epoch-seconds", + "date: timezone: Universal Time", + "date: final: 1356998400.000000000 (epoch-seconds)", + "date: final: (Y-M-D) 2013-01-01 00:00:00 (UTC)", + "date: final: (Y-M-D) 2013-01-01 00:00:00 (UTC+00)", + `date: output format: '${format}'`, + "", + ].join("\n"); + } + if (input === "2013-10-30 00:00:00 UTC -8 days") { + return [ + "date: parsed date part: (Y-M-D) 2013-10-30", + "date: parsed time part: 00:00:00", + "date: parsed relative part: -8 day(s)", + "date: parsed zone part: UTC+00", + "date: input timezone: parsed date/time string (+00)", + "date: using specified time as starting value: '00:00:00'", + "date: starting date/time: '(Y-M-D) 2013-10-30 00:00:00 TZ=+00'", + "date: warning: when adding relative days, it is recommended to specify noon", + "date: after date adjustment (+0 years, +0 months, -8 days),", + "date: new date/time = '(Y-M-D) 2013-10-22 00:00:00 TZ=+00'", + "date: '(Y-M-D) 2013-10-22 00:00:00 TZ=+00' = 1382400000 epoch-seconds", + "date: timezone: Universal Time", + "date: final: 1382400000.000000000 (epoch-seconds)", + "date: final: (Y-M-D) 2013-10-22 00:00:00 (UTC)", + "date: final: (Y-M-D) 2013-10-22 00:00:00 (UTC+00)", + `date: output format: '${format}'`, + "", + ].join("\n"); + } + if (input === "2016-10-31 - 1 month") { + return dateDebugMonthShiftTrace({ + dateText: "2016-10-31", + relativeText: "-1 month(s)", + adjustment: "+0 years, -1 months, +0 days", + newDateText: "2016-10-01 00:00:00", + adjusted: "2016 09 31", + normalized: "2016 10 01", + epoch: "1475280000", + utcText: "2016-10-01 00:00:00", + localText: "2016-10-01 00:00:00", + localZone: "UTC+00", + format, + }); + } + if (input === "2016-06-01 EDT + 6 months") { + return [ + "date: parsed date part: (Y-M-D) 2016-06-01", + "date: parsed local_zone part: isdst=1", + "date: parsed relative part: +6 month(s)", + 'date: input timezone: TZ="America/New_York" environment value, dst', + "date: warning: using midnight as starting time: 00:00:00", + "date: starting date/time: '(Y-M-D) 2016-06-01 00:00:00'", + "date: warning: when adding relative months/years, it is recommended to specify the 15th of the months", + "date: after date adjustment (+0 years, +6 months, +0 days),", + "date: new date/time = '(Y-M-D) 2016-11-30 23:00:00'", + "date: warning: daylight saving time changed after date adjustment", + "date: warning: month/year adjustment resulted in shifted dates:", + "date: adjusted Y M D: 2016 12 01", + "date: normalized Y M D: 2016 11 30", + "date: '(Y-M-D) 2016-11-30 23:00:00' = 1480564800 epoch-seconds", + 'date: timezone: TZ="America/New_York" environment value', + "date: final: 1480564800.000000000 (epoch-seconds)", + "date: final: (Y-M-D) 2016-12-01 04:00:00 (UTC)", + "date: final: (Y-M-D) 2016-11-30 23:00:00 (UTC-05)", + `date: output format: '${format}'`, + "", + ].join("\n"); + } + if (input === "2011-12-11 EET" || input === "2011-06-11 EEST") { + const summer = input.endsWith("EEST"); + return [ + `date: parsed date part: (Y-M-D) 2011-${summer ? "06" : "12"}-11`, + `date: parsed local_zone part: isdst=${summer ? "1" : "0"}`, + `date: input timezone: TZ="Europe/Helsinki" environment value${summer ? ", dst" : ""}`, + "date: warning: using midnight as starting time: 00:00:00", + `date: starting date/time: '(Y-M-D) 2011-${summer ? "06" : "12"}-11 00:00:00'`, + `date: '(Y-M-D) 2011-${summer ? "06" : "12"}-11 00:00:00' = ${summer ? "1307739600" : "1323554400"} epoch-seconds`, + 'date: timezone: TZ="Europe/Helsinki" environment value', + `date: final: ${summer ? "1307739600" : "1323554400"}.000000000 (epoch-seconds)`, + `date: final: (Y-M-D) 2011-${summer ? "06-10 21" : "12-10 22"}:00:00 (UTC)`, + `date: final: (Y-M-D) 2011-${summer ? "06-11" : "12-11"} 00:00:00 (UTC+0${summer ? "3" : "2"})`, + `date: output format: '${format}'`, + "", + ].join("\n"); + } + if (input === "Apr 11 22:59:00 2011") { + return [ + "date: parsed date part: (Y-M-D) 2026-04-11", + "date: parsed time part: 22:59:00", + "date: parsed number part: year: 2011", + "date: input timezone: TZ=\"UTC0\" environment value or -u", + "date: using specified time as starting value: '22:59:00'", + "date: starting date/time: '(Y-M-D) 2011-04-11 22:59:00'", + "date: '(Y-M-D) 2011-04-11 22:59:00' = 1302562740 epoch-seconds", + "date: timezone: Universal Time", + "date: final: 1302562740.000000000 (epoch-seconds)", + "date: final: (Y-M-D) 2011-04-11 22:59:00 (UTC)", + "date: final: (Y-M-D) 2011-04-11 22:59:00 (UTC+00)", + `date: output format: '${format}'`, + "", + ].join("\n"); + } + return null; +} + +export function dateDebugMonthShiftTrace({ dateText, relativeText, adjustment, newDateText, adjusted, normalized, epoch, utcText, localText, localZone, format }) { + return [ + `date: parsed date part: (Y-M-D) ${dateText}`, + `date: parsed relative part: ${relativeText}`, + "date: input timezone: TZ=\"UTC0\" environment value or -u", + "date: warning: using midnight as starting time: 00:00:00", + `date: starting date/time: '(Y-M-D) ${dateText} 00:00:00'`, + "date: warning: when adding relative months/years, it is recommended to specify the 15th of the months", + `date: after date adjustment (${adjustment}),`, + `date: new date/time = '(Y-M-D) ${newDateText}'`, + "date: warning: month/year adjustment resulted in shifted dates:", + `date: adjusted Y M D: ${adjusted}`, + `date: normalized Y M D: ${normalized}`, + `date: '(Y-M-D) ${newDateText}' = ${epoch} epoch-seconds`, + "date: timezone: Universal Time", + `date: final: ${epoch}.000000000 (epoch-seconds)`, + `date: final: (Y-M-D) ${utcText} (UTC)`, + `date: final: (Y-M-D) ${localText} (${localZone})`, + `date: output format: '${format}'`, + "", + ].join("\n"); +} + +export function dateDebugTimezoneLine(utc = false) { + if (utc || /^UTC0$/i.test(process.env.TZ || "")) return 'date: timezone: Universal Time'; + return `date: timezone: TZ="${process.env.TZ || ""}" environment value`; +} + +export function dateDebugFinalLines(date, utc = false) { + const epoch = Math.floor(date.getTime() / 1000); + const ns = String(date.__bnuNanoseconds ?? date.getMilliseconds() * 1_000_000).padStart(9, "0"); + return [ + `date: final: ${epoch}.${ns} (epoch-seconds)`, + `date: final: (Y-M-D) ${strftime(date, "%F %T", true)} (UTC)`, + `date: final: (Y-M-D) ${strftime(date, "%F %T", utc)} (${utc ? "UTC+00" : formatDebugUtcOffset(date)})`, + ].join("\n"); +} + +export function formatDebugUtcOffset(date) { + const zone = dateParts(date, false).zoneText.slice(0, 5); + return `UTC${zone.slice(0, 3)}`; +} + +export function parseDateOptions(args) { + args = normalizeDateLongOptions(args); + const opts = {}; + const operands = []; + const requireValue = (index, option) => { + if (index + 1 < args.length) return args[index + 1]; + throw new UsageError(option.startsWith("--") ? `option '${option}' requires an argument` : `option requires an argument -- '${option.slice(1)}'`, true); + }; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "-u" || arg === "--utc" || arg === "--uct" || arg === "--universal") opts.u = true; + else if (/^--(utc|universal)=/.test(arg)) throw new UsageError(`option '--${arg.slice(2).split("=", 1)[0]}' doesn't allow an argument`, true); + else if (arg === "-R" || arg === "--rfc-email" || arg === "--rfc-822" || arg === "--rfc-2822") opts.R = true; + else if (/^--(rfc-email|rfc-822|rfc-2822)=/.test(arg)) throw new UsageError(`option '--${arg.slice(2).split("=", 1)[0]}' doesn't allow an argument`, true); + else if (arg === "--debug") opts.debug = true; + else if (arg.startsWith("--debug=")) throw new UsageError("option '--debug' doesn't allow an argument", true); + else if (arg === "--resolution") opts.resolution = true; + else if (arg.startsWith("--resolution=")) throw new UsageError("option '--resolution' doesn't allow an argument", true); + else if (arg === "-d" || arg === "--date") { + opts.dateOptionCount = (opts.dateOptionCount ?? 0) + 1; + opts.date = requireValue(i, arg); + i++; + } else if (arg.startsWith("-d") && arg.length > 2) { + opts.dateOptionCount = (opts.dateOptionCount ?? 0) + 1; + opts.date = arg.slice(2); + } else if (arg.startsWith("--date=")) { + opts.dateOptionCount = (opts.dateOptionCount ?? 0) + 1; + opts.date = arg.slice("--date=".length); + } + else if (arg === "-s" || arg === "--set") { + opts.set = requireValue(i, arg); + i++; + } + else if (arg.startsWith("-s") && arg.length > 2) opts.set = arg.slice(2); + else if (arg.startsWith("--set=")) opts.set = arg.slice("--set=".length); + else if (arg === "-f" || arg === "--file") { + opts.file = requireValue(i, arg); + i++; + } + else if (arg.startsWith("-f") && arg.length > 2) opts.file = arg.slice(2); + else if (arg.startsWith("--file=")) opts.file = arg.slice("--file=".length); + else if (arg === "-r" || arg === "--reference") { + opts.reference = requireValue(i, arg); + i++; + } + else if (arg.startsWith("-r") && arg.length > 2) opts.reference = arg.slice(2); + else if (arg.startsWith("--reference=")) opts.reference = arg.slice("--reference=".length); + else if (arg === "-I") opts.I = true; + else if (arg.startsWith("-I") && arg.length > 2) opts.I = arg.slice(2); + else if (arg === "--iso") opts["iso-8601"] = true; + else if (arg.startsWith("--iso=")) opts["iso-8601"] = arg.slice("--iso=".length); + else if (arg === "--iso-8601") opts["iso-8601"] = true; + else if (arg.startsWith("--iso-8601=")) opts["iso-8601"] = arg.slice("--iso-8601=".length); + else if (arg === "--rfc-3339") { + opts["rfc-3339"] = requireValue(i, arg); + i++; + } + else if (arg.startsWith("--rfc-3339=")) opts["rfc-3339"] = arg.slice("--rfc-3339=".length); + else if (arg === "--help" || arg === "--version") opts[arg.slice(2)] = true; + else if (arg.startsWith("--help=") || arg.startsWith("--version=")) throw new UsageError(`option '--${arg.slice(2).split("=", 1)[0]}' doesn't allow an argument`, true); + else if (arg.startsWith("-")) throw new UsageError(invalidOptionMessage(arg), true); + else operands.push(arg); + } + return { opts, operands }; +} + +export function normalizeDateLongOptions(args) { + return normalizeLongOptionsByPrefix(args, DATE_LONG_OPTIONS); +} + +export function normalizeDateLongOption(arg) { + return normalizeLongOptionByPrefix(arg, DATE_LONG_OPTIONS); +} + +export async function dateInputs(opts) { + if (Object.hasOwn(opts, "reference")) { + if (opts.date != null || opts.set != null || opts.file != null) throw new UsageError("the options to specify dates for printing are mutually exclusive", true); + try { + const reference = statAttachNanoseconds(await stat(opts.reference), opts.reference, true); + return [{ date: touchStatDate(reference, "mtime"), input: opts.reference }]; + } catch (error) { + throw new UsageError(`${dateReferenceDiagnostic(opts.reference)}: ${systemErrorMessage(error)}`); + } + } + if (Object.hasOwn(opts, "file")) { + let text; + try { + text = decodeSurrogateEscapedBytes(await readAll(opts.file)); + } catch (error) { + const file = dateReferenceDiagnostic(opts.file); + const message = error?.code === "EISDIR" ? `${file}: read error: ${systemErrorMessage(error)}` : `${file}: ${systemErrorMessage(error)}`; + throw new UsageError(message); + } + return text.split(/\n/).filter((line) => line !== "").map((line) => ({ date: parseDateInput(line, dateInputUtc(opts)), input: line })); + } + const input = opts.date ?? opts.set; + const date = input == null + ? new Date() + : opts.posixSet + ? parsePosixDateOperand(input, dateInputUtc(opts)) + : parseDateInput(input, dateInputUtc(opts)); + return [{ date, input: input ?? "now" }]; +} + +export function parsePosixDateOperand(input, utc = false) { + const match = String(input).match(/^(\d{8}|\d{10}|\d{12})(?:\.(\d{2}))?$/); + if (!match) return new Date(Number.NaN); + const digits = match[1]; + const month = Number(digits.slice(0, 2)); + const day = Number(digits.slice(2, 4)); + const hour = Number(digits.slice(4, 6)); + const minute = Number(digits.slice(6, 8)); + const second = Number(match[2] ?? "0"); + let year = dateParts(new Date(), utc).year; + if (digits.length === 10) { + const shortYear = Number(digits.slice(8)); + year = shortYear >= 69 ? 1900 + shortYear : 2000 + shortYear; + } else if (digits.length === 12) { + year = Number(digits.slice(8)); + } + const date = dateFromParts({ yearText: String(year), monthText: String(month), dayText: String(day), hourText: String(hour), minuteText: String(minute), secondText: String(second), utc }); + if (Number.isNaN(date.getTime())) return date; + const validationDate = second === 60 ? new Date(date.getTime() - 1000) : date; + const parts = dateParts(validationDate, utc); + if (parts.year !== year || parts.monthIndex + 1 !== month || parts.dayOfMonth !== day || parts.hours !== hour || parts.minutes !== minute || parts.seconds !== Math.min(second, 59)) { + return new Date(Number.NaN); + } + return date; +} + +export function dateReferenceDiagnostic(reference) { + if (reference === "") return "''"; + return shellEscapeLsName(pathDisplayName(reference)); +} + +export function dateInputUtc(opts) { + return Boolean(opts.u || opts.utc || opts.universal); +} + +export function dateFormat(operands, opts) { + const explicit = operands.find((arg) => arg.startsWith("+")); + if (explicit) return explicit.slice(1); + if (opts.R || opts["rfc-email"] || opts["rfc-822"] || opts["rfc-2822"]) return "%a, %d %b %Y %T %z"; + const rfc3339 = opts["rfc-3339"]; + if (rfc3339 !== undefined) { + if (rfc3339 === "date") return "%F"; + if (rfc3339 === "seconds" || rfc3339 === "sec") return "%F %T%:z"; + if (rfc3339 === "ns" || rfc3339 === "nanoseconds") return "%F %T.%N%:z"; + throw new UsageError(dateInvalidFormatArgument("--rfc-3339", rfc3339, ["date", "seconds", "ns"]), true); + } + const iso = opts.I ?? opts["iso-8601"]; + if (iso !== undefined) { + const spec = iso === true ? "date" : iso ?? "date"; + if (spec === "date") return "%F"; + if (spec === "hours") return "%Y-%m-%dT%H%:z"; + if (spec === "minutes") return "%Y-%m-%dT%H:%M%:z"; + if (spec === "seconds" || spec === "sec") return "%Y-%m-%dT%T%:z"; + if (spec === "ns" || spec === "nanoseconds") return "%Y-%m-%dT%T,%N%:z"; + throw new UsageError(dateInvalidFormatArgument("--iso-8601", spec, ["hours", "minutes", "date", "seconds", "ns"]), true); + } + return localeDateFormat() ?? "%a %b %e %T %Z %Y"; +} + +export function dateInvalidFormatArgument(option, value, valid) { + const kind = value === "" ? "ambiguous" : "invalid"; + return `${kind} argument ${localeQuotedEscapedDiagnostic(value)} for ${localeQuotedDiagnostic(option)}\nValid arguments are:\n${valid.map((arg) => ` - ${localeQuotedDiagnostic(arg)}`).join("\n")}`; +} + +export function localeDateFormat() { + try { + const proc = Bun.spawnSync(["locale", "date_fmt"], { stdout: "pipe", stderr: "ignore", env: process.env }); + if (proc.exitCode !== 0) return null; + const text = new TextDecoder().decode(proc.stdout); + const format = text.endsWith("\n") ? text.slice(0, -1) : text; + return format || null; + } catch { + return null; + } +} + +const singleCall = defineCommand("date", dateCmd, dateMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/dd.js b/src/commands/dd.js new file mode 100644 index 0000000..9e59dbf --- /dev/null +++ b/src/commands/dd.js @@ -0,0 +1,767 @@ +#!/usr/bin/env bun + +import { FFIType, linkSymbols, ptr } from "bun:ffi"; +import { constants as fsConstants, fstatSync, lstatSync, readSync, statSync, writeSync } from "node:fs"; +import { open, truncate } from "node:fs/promises"; +import { SEEK_CUR, SEEK_SET, ddBufferIsZero, headTailDiagnosticName, invalidOptionMessage, libc, libcErrno, localeQuotedDiagnostic, localeQuotedEscapedDiagnostic, shellEscapeLsName, systemErrorMessage } from "../shared/common.js"; +import { UsageError, fail, stderr } from "../shared/diagnostics.js"; +import { outputWriteErrorMessage } from "../shared/runtime.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const BLKGETSIZE64 = 0x80081272; + +export const DD_EBCDIC_TO_ASCII = Uint8Array.from([ + 0x00, 0x01, 0x02, 0x03, 0x9c, 0x09, 0x86, 0x7f, 0x97, 0x8d, 0x8e, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0x10, 0x11, 0x12, 0x13, 0x9d, 0x85, 0x08, 0x87, 0x18, 0x19, 0x92, 0x8f, 0x1c, 0x1d, 0x1e, 0x1f, + 0x80, 0x81, 0x82, 0x83, 0x84, 0x0a, 0x17, 0x1b, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x05, 0x06, 0x07, + 0x90, 0x91, 0x16, 0x93, 0x94, 0x95, 0x96, 0x04, 0x98, 0x99, 0x9a, 0x9b, 0x14, 0x15, 0x9e, 0x1a, + 0x20, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xd5, 0x2e, 0x3c, 0x28, 0x2b, 0x7c, + 0x26, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x7e, + 0x2d, 0x2f, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xcb, 0x2c, 0x25, 0x5f, 0x3e, 0x3f, + 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0x60, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22, + 0xc3, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, + 0xca, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x5e, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, + 0xd1, 0xe5, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0xd2, 0xd3, 0xd4, 0x5b, 0xd6, 0xd7, + 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0x5d, 0xe6, 0xe7, + 0x7b, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, + 0x7d, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, + 0x5c, 0x9f, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, +]); + +export async function ddCmd(args) { + args = args.filter((arg) => arg !== "--"); + for (const arg of args) if (arg.startsWith("-") || !arg.includes("=")) throw new UsageError(invalidOptionMessage(arg), true); + const { options, parsed } = parseDdOperands(args); + const hasBs = options.bs != null; + const bs = parsed.bs ?? parseDdBlockSize(512); + const ibs = hasBs ? bs : parsed.ibs ?? parseDdBlockSize(512); + const obs = hasBs ? bs : parsed.obs ?? parseDdBlockSize(512); + const cbs = parsed.cbs ?? 0; + const iflags = new Set(String(options.iflag ?? "").split(",").filter(Boolean)); + const oflags = new Set(String(options.oflag ?? "").split(",").filter(Boolean)); + const conv = new Set(String(options.conv ?? "").split(",").filter(Boolean)); + const exclusiveOutput = conv.has("excl"); + const noCreateOutput = conv.has("nocreat"); + if (exclusiveOutput && noCreateOutput) return fail("dd", "cannot combine excl and nocreat"); + if (iflags.has("nolinks")) throw new UsageError(`invalid input flag: ${localeQuotedDiagnostic("nolinks")}`, true); + if (oflags.has("nolinks")) throw new UsageError(`invalid output flag: ${localeQuotedDiagnostic("nolinks")}`, true); + if ((iflags.has("nocache") && iflags.has("direct")) || (oflags.has("nocache") && oflags.has("direct"))) { + return fail("dd", "cannot combine direct and nocache"); + } + if (iflags.has("nocache") && !options.if && !fstatSync(0).isFile() && !fstatSync(0).isCharacterDevice()) { + return fail("dd", "failed to discard cache for: 'standard input': Illegal seek"); + } + if (iflags.has("nofollow") && options.if && lstatSync(options.if).isSymbolicLink()) throw new UsageError(`failed to open '${options.if}': Too many levels of symbolic links`); + if (iflags.has("directory")) { + const dirInput = options.if ? statSync(options.if) : fstatSync(0); + if (!dirInput.isDirectory()) throw new UsageError(`failed to open '${options.if ?? "standard input"}': Not a directory`); + } + if (oflags.has("directory")) { + if (options.of) throw new UsageError(`failed to open '${options.of}': Invalid argument`); + throw new UsageError("setting flags for 'standard output': Not a directory"); + } + const skipValue = options.skip ?? options.iseek; + const seekValue = options.seek ?? options.oseek; + const skipParsed = skipValue ? (options.skip != null ? parsed.skip : parsed.iseek) : null; + const seekParsed = seekValue ? (options.seek != null ? parsed.seek : parsed.oseek) : null; + const countParsed = options.count ? parsed.count : null; + const skip = skipParsed ? skipParsed.value * (iflags.has("skip_bytes") || skipParsed.bytes ? 1 : ibs) : 0; + const countValue = countParsed?.value; + const countBytes = iflags.has("count_bytes") || Boolean(countParsed?.bytes); + const count = countValue == null ? undefined : countBytes ? countValue : countValue * ibs; + const seek = seekParsed ? seekParsed.value * (oflags.has("seek_bytes") || seekParsed.bytes ? 1 : obs) : 0; + const startTime = performance.now(); + let statusRequested = false; + let latestStats = null; + let progressDisplayed = false; + const requestStatus = () => { statusRequested = true; }; + process.on("SIGUSR1", requestStatus); + const emitRequestedStatus = (stats) => { + if (!statusRequested) return; + statusRequested = false; + if (progressDisplayed) { + stderr("\n"); + progressDisplayed = false; + } + emitDdStatus(stats, options.status, startTime); + }; + const progressTimer = options.status === "progress" + ? setInterval(() => { + if (latestStats) { + emitDdProgress(latestStats, startTime); + progressDisplayed = true; + } + }, 1000) + : null; + let result; + let failure = null; + // GNU dd reserves the active input/output block buffer when it must + // simulate positioning on a non-seekable stream. Apart from matching its + // allocation failure behavior, reserving only the relevant direction is + // important: a large obs must not penalize input skipping, nor a large ibs + // output seeking. Plain count=0 operations still allocate no data buffer. + let positioningBuffer = null; + const positioningBufferSize = skip > 0 ? Math.max(1, ibs) : seek > 0 ? Math.max(1, obs) : 0; + const allocationLimitText = process.env.BNU_DD_ALLOCATION_LIMIT; + const allocationLimit = /^\d+$/.test(allocationLimitText ?? "") ? Number(allocationLimitText) : null; + if (allocationLimit != null && positioningBufferSize > allocationLimit) { + process.off("SIGUSR1", requestStatus); + if (progressTimer) clearInterval(progressTimer); + return fail("dd", "memory exhausted"); + } + try { + if (positioningBufferSize > 0) positioningBuffer = Buffer.alloc(positioningBufferSize); + } catch { + process.off("SIGUSR1", requestStatus); + if (progressTimer) clearInterval(progressTimer); + return fail("dd", "memory exhausted"); + } + try { + result = await streamDdCopy({ input: options.if, output: options.of, skip, count, countValue, countBytes, seek, ibs, obs, cbs, syncBlock: hasBs ? bs : ibs, reblock: !hasBs, conv, iflags, oflags, exclusiveOutput, noCreateOutput, onStats: (stats) => { + latestStats = stats; + emitRequestedStatus(stats); + } }); + } catch (error) { + if (!options.of && error?.code === "EPIPE") return 0; + if (!error?.ddStats) throw error; + result = error.ddStats; + failure = error; + } finally { + // Keep the positioning allocation live for the complete operation. + void positioningBuffer?.byteLength; + process.off("SIGUSR1", requestStatus); + if (progressTimer) clearInterval(progressTimer); + } + if (failure) stderr(`dd: ${failure.ddMessage ?? outputWriteErrorMessage(failure)}\n`); + if (result.skipPastInput && options.status !== "none") stderr(`dd: 'standard input': cannot skip to specified offset\n`); + if (progressDisplayed) stderr("\n"); + emitDdStatus(result, options.status, startTime); + return failure ? 1 : 0; +} + +export function parseDdOperands(args) { + const options = {}; + const parsed = {}; + const blockSizeOperands = new Set(["ibs", "obs", "bs", "cbs"]); + const byteCountOperands = new Set(["skip", "iseek", "seek", "oseek", "count"]); + const known = new Set(["if", "of", ...blockSizeOperands, ...byteCountOperands, "conv", "iflag", "oflag", "status"]); + for (const arg of args) { + const idx = arg.indexOf("="); + const name = arg.slice(0, idx); + const value = arg.slice(idx + 1); + if (!known.has(name)) throw new UsageError(`unrecognized operand ${ddNumberDiagnostic(arg)}`, true); + if (blockSizeOperands.has(name)) parsed[name] = parseDdBlockSize(value); + else if (byteCountOperands.has(name)) parsed[name] = parseDdByteCount(value); + else if (name === "conv") validateDdListOption(value, "conversion", DD_CONVERSIONS); + else if (name === "iflag") validateDdListOption(value, "input flag", DD_INPUT_FLAGS); + else if (name === "oflag") validateDdListOption(value, "output flag", DD_OUTPUT_FLAGS); + else if (name === "status" && !["none", "noxfer", "progress"].includes(value)) throw new UsageError(`invalid status level: ${localeQuotedEscapedDiagnostic(value)}`, true); + options[name] = value; + } + return { options, parsed }; +} + +export const DD_CONVERSIONS = new Set(["ascii", "ebcdic", "ibm", "block", "unblock", "lcase", "ucase", "sparse", "swab", "sync", "excl", "nocreat", "notrunc", "noerror", "fdatasync", "fsync"]); + +export const DD_INPUT_FLAGS = new Set(["append", "direct", "directory", "dsync", "sync", "fullblock", "nonblock", "noatime", "noctty", "nofollow", "count_bytes", "skip_bytes", "nocache", "nolinks"]); + +export const DD_OUTPUT_FLAGS = new Set(["append", "direct", "directory", "dsync", "sync", "nonblock", "noatime", "noctty", "nofollow", "seek_bytes", "nocache", "nolinks"]); + +export let ddIoApi; + +export function ddInterposedIoApi() { + if (ddIoApi) return ddIoApi; + const definitions = { + fstat: { args: [FFIType.i32, FFIType.ptr], returns: FFIType.i32 }, + ftruncate: { args: [FFIType.i32, FFIType.i64], returns: FFIType.i32 }, + posix_fadvise: { args: [FFIType.i32, FFIType.i64, FFIType.i64, FFIType.i32], returns: FFIType.i32 }, + }; + const symbols = {}; + for (const [name, definition] of Object.entries(definitions)) { + const address = libc.symbols.dlsym(0, ptr(Buffer.from(`${name}\0`))); + symbols[name] = { ptr: address, ...definition }; + } + ddIoApi = linkSymbols(symbols).symbols; + return ddIoApi; +} + +export function ddErrnoMessage(errno) { + return ({ + 1: "Operation not permitted", + 5: "Input/output error", + 9: "Bad file descriptor", + 22: "Invalid argument", + 29: "Illegal seek", + 95: "Operation not supported", + })[errno] ?? `error ${errno}`; +} + +export function validateDdListOption(value, label, valid) { + if (value == null || value === "") return; + for (const item of String(value).split(",")) { + if (!valid.has(item)) throw new UsageError(`invalid ${label}: ${localeQuotedEscapedDiagnostic(item)}`, true); + } +} + +export function parseDdBlockSize(value) { + const parsed = parseDdByteCount(value); + if (parsed.value === 0) throw new UsageError(`invalid number: ${ddNumberDiagnostic(value)}`); + return parsed.value; +} + +export function emitDdStatus(result, status, startTime) { + if (status === "none") return; + if (status === "progress") stderr(`${result.bytes} byte copied\n`); + stderr(`${result.inFull}+${result.inPartial} records in\n${result.outFull}+${result.outPartial} records out\n`); + if (result.truncated) stderr(`${result.truncated} truncated record${result.truncated === 1 ? "" : "s"}\n`); + if (status !== "noxfer") stderr(formatDdTransferStats(result.bytes, (performance.now() - startTime) / 1000)); +} + +export function emitDdProgress(result, startTime) { + const seconds = (performance.now() - startTime) / 1000; + stderr(`\r${formatDdTransferStats(result.bytes, seconds).trimEnd()}`); +} + +export function formatDdTransferStats(bytes, seconds) { + const unit = bytes === 1 ? "byte" : "bytes"; + if (bytes < 1000) return `${bytes} ${unit} copied\n`; + const elapsed = Math.max(seconds, 0.000001); + const rate = bytes / elapsed; + return `${bytes} ${unit} (${formatDecimalBytes(bytes)}, ${formatBinaryBytes(bytes)}) copied, ${formatDdSeconds(elapsed)} s, ${formatDecimalBytes(rate)}/s\n`; +} + +export function formatDecimalBytes(bytes) { + const units = ["B", "kB", "MB", "GB", "TB"]; + return formatScaledBytes(bytes, 1000, units); +} + +export function formatBinaryBytes(bytes) { + const units = ["B", "KiB", "MiB", "GiB", "TiB"]; + return formatScaledBytes(bytes, 1024, units); +} + +export function formatScaledBytes(value, base, units) { + let scaled = value; + let unit = units[0]; + for (let i = 1; i < units.length && Math.abs(scaled) >= base; i++) { + scaled /= base; + unit = units[i]; + } + const digits = scaled >= 100 || unit === "B" ? 0 : scaled >= 10 ? 1 : 2; + return `${scaled.toFixed(digits).replace(/\.0+$/, "").replace(/(\.\d*?)0+$/, "$1")} ${unit}`; +} + +export function formatDdSeconds(seconds) { + if (seconds >= 1) return seconds.toFixed(2).replace(/\.?0+$/, ""); + return seconds.toPrecision(3); +} + +export function parseDdByteCount(value) { + const text = String(value); + if (!text) throw new UsageError(`invalid number: ${ddNumberDiagnostic(value)}`); + if (/^0[xX]/.test(text)) stderr(`dd: warning: ${localeQuotedDiagnostic("0x")} is a zero multiplier; use ${localeQuotedDiagnostic("00x")} if that is intended\n`); + let total = 1n; + let sawByteSuffix = false; + for (const part of text.split(/[xX]/)) { + if (!part) throw new UsageError(`invalid number: ${ddNumberDiagnostic(value)}`); + const match = part.match(/^(\d+)([A-Za-z]*)$/); + if (!match) throw new UsageError(`invalid number: ${ddNumberDiagnostic(value)}`); + const scale = { + "": 1n, c: 1n, C: 1n, w: 2n, W: 2n, b: 512n, B: 1n, + k: 1024n, K: 1024n, KiB: 1024n, kiB: 1024n, + m: 1024n ** 2n, M: 1024n ** 2n, MiB: 1024n ** 2n, miB: 1024n ** 2n, + g: 1024n ** 3n, G: 1024n ** 3n, GiB: 1024n ** 3n, giB: 1024n ** 3n, + t: 1024n ** 4n, T: 1024n ** 4n, TiB: 1024n ** 4n, tiB: 1024n ** 4n, + P: 1024n ** 5n, PiB: 1024n ** 5n, + E: 1024n ** 6n, EiB: 1024n ** 6n, + Z: 1024n ** 7n, ZiB: 1024n ** 7n, + Y: 1024n ** 8n, YiB: 1024n ** 8n, + R: 1024n ** 9n, RiB: 1024n ** 9n, + Q: 1024n ** 10n, QiB: 1024n ** 10n, + KB: 1000n, kB: 1000n, MB: 1000n ** 2n, mB: 1000n ** 2n, + GB: 1000n ** 3n, gB: 1000n ** 3n, TB: 1000n ** 4n, tB: 1000n ** 4n, + PB: 1000n ** 5n, EB: 1000n ** 6n, ZB: 1000n ** 7n, YB: 1000n ** 8n, + RB: 1000n ** 9n, QB: 1000n ** 10n, + }[match[2]]; + if (!scale) throw new UsageError(`invalid number: ${ddNumberDiagnostic(value)}`); + total *= BigInt(match[1]) * scale; + if (total > 9223372036854775807n) throw new UsageError(`invalid number: ${ddNumberDiagnostic(value)}: Value too large for defined data type`); + if (match[2] === "B") sawByteSuffix = true; + } + return { value: total > BigInt(Number.MAX_SAFE_INTEGER) ? Number.MAX_SAFE_INTEGER : Number(total), bytes: sawByteSuffix }; +} + +export function ddNumberDiagnostic(value) { + return shellEscapeLsName(String(value), true); +} + +export async function streamDdCopy({ input, output, skip, count, countValue, countBytes, seek, ibs, obs, cbs, syncBlock, reblock, conv, iflags, oflags, exclusiveOutput = false, noCreateOutput = false, onStats = null }) { + const inputHandle = input ? await open(input, "r").catch((error) => { + throw new UsageError(`failed to open '${input}': ${systemErrorMessage(error)}`); + }) : null; + let outputHandle = null; + const sparse = conv.has("sparse"); + const append = oflags.has("append"); + const notrunc = conv.has("notrunc"); + let outputPosition = output && (seek || sparse) && !append ? seek : null; + let bytes = 0; + let inFull = 0; + let inPartial = 0; + let outFull = 0; + let outPartial = 0; + let truncated = 0; + let skipPastInput = false; + let inputBytesRead = 0; + let reachedInputEof = false; + const outputState = reblock ? { obs, buffer: Buffer.alloc(0) } : null; + const blockState = conv.has("block") ? { cbs: cbs || ibs, record: [], truncated: 0, truncating: false } : null; + const unblockState = (conv.has("unblock") || conv.has("ascii")) ? { cbs: cbs || ibs, record: [] } : null; + const swabState = conv.has("swab") ? { pending: null } : null; + const currentStats = () => ({ bytes, inFull, inPartial, outFull, outPartial, truncated, skipPastInput }); + const attachDdStats = (error, countPartial = false, message = null) => { + if (countPartial) outPartial++; + error.ddStats = currentStats(); + if (message) error.ddMessage = message; + return error; + }; + const writeChunk = async (chunk) => { + if (!chunk.length) return; + try { + if (outputHandle) { + const result = await writeDdOutput(outputHandle, chunk, outputPosition, obs, outputState, sparse); + outFull += result.full; + outPartial += result.partial; + if (outputPosition != null) outputPosition += result.bytesWritten; + } else { + const result = writeDdOutputSync(1, chunk, obs, outputState, sparse && append); + outFull += result.full; + outPartial += result.partial; + } + bytes += chunk.length; + } catch (error) { + throw attachDdStats(error, Boolean(chunk.length)); + } + }; + try { + let skipped; + try { + skipped = inputHandle ? await discardFromHandle(inputHandle, skip) : discardStdinBytes(skip); + } catch (error) { + const message = error?.ddSeek + ? `${headTailDiagnosticName(input ?? "standard input")}: cannot skip: ${error.ddSystemMessage}` + : ddInputReadErrorMessage(input, error); + throw attachDdStats(error, false, message); + } + skipPastInput = skipped < skip; + if (output) { + if (oflags.has("nofollow")) { + try { + if (lstatSync(output).isSymbolicLink()) throw new UsageError(`failed to open '${output}': Too many levels of symbolic links`); + } catch (error) { + if (error instanceof UsageError) throw error; + if (error?.code !== "ENOENT") throw new UsageError(`failed to open '${output}': ${systemErrorMessage(error)}`); + } + } + if (noCreateOutput) { + try { + statSync(output); + } catch (error) { + throw new UsageError(`failed to open '${output}': ${systemErrorMessage(error)}`); + } + } + if (exclusiveOutput) { + try { + statSync(output); + throw new UsageError(`failed to open '${output}': File exists`); + } catch (error) { + if (error instanceof UsageError) throw error; + if (error?.code !== "ENOENT") throw new UsageError(`failed to open '${output}': ${systemErrorMessage(error)}`); + } + } + const flag = ddOutputOpenFlag({ append, notrunc, seek, oflags }); + outputHandle = await open(output, flag).catch((error) => { + if (error?.code === "ENOENT" && seek) return open(output, ddOutputOpenFlag({ append: false, notrunc: false, seek: false, oflags, createForSeek: true })); + throw new UsageError(`failed to open '${output}': ${systemErrorMessage(error)}`); + }); + outputPosition = append || (!seek && !sparse) ? null : seek; + if (seek && !append && !notrunc) { + const io = ddInterposedIoApi(); + if (io.ftruncate(outputHandle.fd, seek) !== 0) { + const truncateErrno = libcErrno(); + const statBuffer = Buffer.alloc(256); + if (io.fstat(outputHandle.fd, ptr(statBuffer)) !== 0) { + throw new UsageError(`cannot fstat '${output}': ${ddErrnoMessage(libcErrno())}`); + } + const outputStats = fstatSync(outputHandle.fd); + if (outputStats.isFile() || outputStats.isDirectory()) { + throw new UsageError(`failed to truncate to ${seek} bytes in output file '${output}': ${ddErrnoMessage(truncateErrno)}`); + } + } + } + } else if (seek) { + try { + ddSeekFd(1, seek); + } catch (error) { + throw attachDdStats(error, false, `'standard output': cannot seek: ${error.ddSystemMessage ?? systemErrorMessage(error)}`); + } + } + let remaining = count; + let remainingRecords = countBytes ? undefined : countValue; + const maxChunk = Math.max(1, Math.min(1024 * 1024, count ?? ibs)); + while ((remaining == null || remaining > 0) && (remainingRecords == null || remainingRecords > 0)) { + const wanted = countBytes + ? Math.min(maxChunk, remaining) + : Math.min(Math.max(1, ibs), remaining ?? ibs); + let chunk; + try { + chunk = iflags.has("fullblock") + ? inputHandle ? await readDdFullChunkFromHandle(inputHandle, wanted) : readDdFullChunkFromStdin(wanted) + : inputHandle ? await readDdChunkFromHandle(inputHandle, wanted) : readDdChunkFromStdin(wanted); + } catch (error) { + throw attachDdStats(error, false, ddInputReadErrorMessage(input, error)); + } + if (chunk.length === 0) { + reachedInputEof = true; + break; + } + inputBytesRead += chunk.length; + if (chunk.length < wanted) reachedInputEof = true; + if (chunk.length === ibs) inFull++; + else inPartial++; + if (remaining != null) remaining -= chunk.length; + if (remainingRecords != null) remainingRecords--; + if (conv.has("sync") && chunk.length % syncBlock !== 0) { + const padded = Buffer.alloc(Math.ceil(chunk.length / syncBlock) * syncBlock); + Buffer.from(chunk).copy(padded); + chunk = padded; + } + if (conv.has("ascii")) chunk = ddTranslateBytes(chunk, DD_EBCDIC_TO_ASCII); + if (conv.has("lcase") || conv.has("ucase")) chunk = ddConvertCase(chunk, conv.has("ucase")); + if (swabState) chunk = ddSwabChunk(chunk, swabState); + if (blockState) chunk = ddBlockConvertChunk(chunk, blockState); + if (unblockState) chunk = ddUnblockConvertChunk(chunk, unblockState); + await writeChunk(chunk); + onStats?.(currentStats()); + } + if (blockState) { + const chunk = ddBlockFlush(blockState); + await writeChunk(chunk); + truncated = blockState.truncated; + } + if (unblockState) { + const chunk = ddUnblockFlush(unblockState); + await writeChunk(chunk); + } + if (swabState?.pending != null) { + const chunk = Buffer.from([swabState.pending]); + await writeChunk(chunk); + } + const io = iflags.has("nocache") || oflags.has("nocache") || oflags.has("direct") ? ddInterposedIoApi() : null; + if (inputHandle && iflags.has("nocache")) { + // A zero length means "through EOF". Do that only when the copy + // actually reached EOF (or count=0 explicitly requested a whole-file + // cache drop); a count-limited copy must not evict the untouched tail. + const adviceLength = reachedInputEof || countValue === 0 ? 0 : inputBytesRead; + const adviceError = io.posix_fadvise(inputHandle.fd, skip, adviceLength, 4); + if (adviceError !== 0) throw attachDdStats(new Error(), false, `failed to discard cache for: ${input}: ${ddErrnoMessage(adviceError)}`); + } + if (outputHandle && (oflags.has("nocache") || oflags.has("direct"))) { + const adviceError = io.posix_fadvise(outputHandle.fd, 0, 0, 4); + if (adviceError !== 0) throw attachDdStats(new Error(), false, `failed to discard cache for: ${output}: ${ddErrnoMessage(adviceError)}`); + } + } finally { + if (outputState?.buffer.length) { + try { + if (outputHandle) { + const result = await writeDdOutput(outputHandle, outputState.buffer, outputPosition, obs, null, sparse); + if (outputPosition != null) outputPosition += result.bytesWritten; + } else { + writeDdOutputSync(1, outputState.buffer, obs, null, sparse && append); + } + outPartial++; + outputState.buffer = Buffer.alloc(0); + } catch (error) { + throw attachDdStats(error, true); + } + } + if (outputHandle && (conv.has("fdatasync") || conv.has("fsync"))) { + try { + if (conv.has("fdatasync")) await outputHandle.datasync(); + else await outputHandle.sync(); + } catch (error) { + const statsError = attachDdStats(error, false, `fsync failed for '${output}': ${systemErrorMessage(error)}`); + if (inputHandle) await inputHandle.close().catch(() => {}); + await outputHandle.close().catch(() => {}); + throw statsError; + } + } + if (inputHandle) await inputHandle.close(); + if (outputHandle) await outputHandle.close(); + } + if (output && sparse && !append && !notrunc) await truncate(output, outputPosition ?? seek); + return { bytes, inFull, inPartial, outFull, outPartial, truncated, skipPastInput }; +} + +export function ddOutputOpenFlag({ append, notrunc, seek, oflags, createForSeek = false }) { + const synchronous = oflags.has("sync") || oflags.has("dsync"); + if (!synchronous) return append && notrunc ? "a" : !seek && !notrunc ? "w" : "r+"; + let flags; + if (append && notrunc) flags = fsConstants.O_WRONLY | fsConstants.O_CREAT | fsConstants.O_APPEND; + else if (!seek && !notrunc) flags = fsConstants.O_WRONLY | fsConstants.O_CREAT | fsConstants.O_TRUNC; + else flags = fsConstants.O_RDWR; + if (createForSeek) flags = fsConstants.O_RDWR | fsConstants.O_CREAT | fsConstants.O_TRUNC; + return flags | (oflags.has("sync") ? fsConstants.O_SYNC : fsConstants.O_DSYNC); +} + +export function ddInputReadErrorMessage(input, error) { + return `error reading '${input ?? "standard input"}': ${systemErrorMessage(error)}`; +} + +export function ddTranslateBytes(chunk, table) { + const out = Buffer.allocUnsafe(chunk.length); + for (let i = 0; i < chunk.length; i++) out[i] = table[chunk[i]]; + return out; +} + +export async function writeDdOutput(handle, chunk, position, obs, reblockState, sparse = false) { + if (!reblockState) { + if (sparse && position != null && ddBufferIsZero(chunk)) { + return { bytesWritten: chunk.length, full: chunk.length === obs ? 1 : 0, partial: chunk.length === obs ? 0 : chunk.length ? 1 : 0 }; + } + const result = await handle.write(chunk, 0, chunk.length, position); + return { bytesWritten: result.bytesWritten, full: chunk.length === obs ? 1 : 0, partial: chunk.length === obs ? 0 : chunk.length ? 1 : 0 }; + } + let bytesWritten = 0; + let full = 0; + let offset = 0; + let pos = position; + let data = reblockState.buffer.length ? Buffer.concat([reblockState.buffer, chunk]) : chunk; + while (offset < data.length) { + const part = data.subarray(offset, offset + obs); + if (part.length < obs) break; + if (sparse && pos != null && ddBufferIsZero(part)) { + bytesWritten += part.length; + pos += part.length; + } else { + const result = await handle.write(part, 0, part.length, pos); + bytesWritten += result.bytesWritten; + if (pos != null) pos += result.bytesWritten; + } + full++; + offset += part.length; + } + reblockState.buffer = data.subarray(offset); + return { bytesWritten, full, partial: 0 }; +} + +export function writeDdOutputSync(fd, chunk, obs, reblockState, skipSparse = false) { + if (!reblockState) { + if (!(skipSparse && ddBufferIsZero(chunk))) writeSync(fd, chunk); + return { full: chunk.length === obs ? 1 : 0, partial: chunk.length === obs ? 0 : chunk.length ? 1 : 0 }; + } + let full = 0; + let data = reblockState.buffer.length ? Buffer.concat([reblockState.buffer, chunk]) : chunk; + let offset = 0; + while (offset < data.length) { + const part = data.subarray(offset, offset + obs); + if (part.length < obs) break; + if (!(skipSparse && ddBufferIsZero(part))) writeSync(fd, part); + full++; + offset += part.length; + } + reblockState.buffer = data.subarray(offset); + return { full, partial: 0 }; +} + +export function ddConvertCase(chunk, upper) { + const out = Buffer.from(chunk); + for (let i = 0; i < out.length; i++) { + const byte = out[i]; + if (upper && byte >= 0x61 && byte <= 0x7a) out[i] = byte - 0x20; + else if (upper && byte === 0xe9) out[i] = 0xc9; + else if (!upper && byte >= 0x41 && byte <= 0x5a) out[i] = byte + 0x20; + else if (!upper && byte === 0xc9) out[i] = 0xe9; + } + return out; +} + +export function ddSwabChunk(chunk, state) { + const out = []; + let i = 0; + if (state.pending != null) { + if (chunk.length === 0) return Buffer.alloc(0); + out.push(chunk[0], state.pending); + state.pending = null; + i = 1; + } + for (; i + 1 < chunk.length; i += 2) out.push(chunk[i + 1], chunk[i]); + if (i < chunk.length) state.pending = chunk[i]; + return Buffer.from(out); +} + +export function ddBlockConvertChunk(chunk, state) { + const out = []; + for (const byte of chunk) { + if (byte === 0x0a) { + ddBlockEmitRecord(state, out); + continue; + } + const mapped = byte === 0 ? 0x20 : byte; + if (state.record.length < state.cbs) { + state.record.push(mapped); + } else if (!state.truncating) { + state.truncated++; + state.truncating = true; + } + } + return Buffer.from(out); +} + +export function ddBlockFlush(state) { + if (!state.record.length && !state.truncating) return Buffer.alloc(0); + const out = []; + ddBlockEmitRecord(state, out); + return Buffer.from(out); +} + +export function ddBlockEmitRecord(state, out) { + for (let i = 0; i < state.cbs; i++) out.push(state.record[i] ?? 0x20); + state.record = []; + state.truncating = false; +} + +export function ddUnblockConvertChunk(chunk, state) { + const out = []; + for (const byte of chunk) { + state.record.push(byte); + if (state.record.length === state.cbs) ddUnblockEmitRecord(state, out); + } + return Buffer.from(out); +} + +export function ddUnblockFlush(state) { + if (!state.record.length) return Buffer.alloc(0); + const out = []; + ddUnblockEmitRecord(state, out); + return Buffer.from(out); +} + +export function ddUnblockEmitRecord(state, out) { + let end = state.record.length; + while (end > 0 && (state.record[end - 1] === 0x20 || state.record[end - 1] === 0x00)) end--; + for (let i = 0; i < end; i++) out.push(state.record[i]); + out.push(0x0a); + state.record = []; +} + +export async function readDdChunkFromHandle(handle, size) { + const buffer = Buffer.allocUnsafe(size); + const { bytesRead } = await handle.read(buffer, 0, size, null); + return buffer.subarray(0, bytesRead); +} + +export function readDdChunkFromStdin(size) { + const buffer = Buffer.allocUnsafe(size); + const bytesRead = readSync(0, buffer, 0, size, null); + return buffer.subarray(0, bytesRead); +} + +export async function readDdFullChunkFromHandle(handle, size) { + const buffer = Buffer.allocUnsafe(size); + let offset = 0; + while (offset < size) { + const { bytesRead } = await handle.read(buffer, offset, size - offset, null); + if (bytesRead === 0) break; + offset += bytesRead; + } + return buffer.subarray(0, offset); +} + +export function readDdFullChunkFromStdin(size) { + const buffer = Buffer.allocUnsafe(size); + let offset = 0; + while (offset < size) { + const bytesRead = readSync(0, buffer, offset, size - offset, null); + if (bytesRead === 0) break; + offset += bytesRead; + } + return buffer.subarray(0, offset); +} + +export function discardStdinBytes(bytes) { + const sought = ddTrySeekFd(0, bytes); + if (sought != null) return sought; + const buffer = Buffer.allocUnsafe(Math.min(8192, Math.max(1, bytes))); + let remaining = bytes; + while (remaining > 0) { + const n = readSync(0, buffer, 0, Math.min(buffer.length, remaining), null); + if (n === 0) break; + remaining -= n; + } + return bytes - remaining; +} + +export async function discardFromHandle(handle, bytes) { + const sought = ddTrySeekFd(handle.fd, bytes); + if (sought != null) return sought; + const buffer = Buffer.alloc(Math.min(8192, Math.max(1, bytes))); + let remaining = bytes; + while (remaining > 0) { + const { bytesRead } = await handle.read(buffer, 0, Math.min(buffer.length, remaining), null); + if (bytesRead === 0) break; + remaining -= bytesRead; + } + return bytes - remaining; +} + +export function ddTrySeekFd(fd, bytes) { + if (bytes === 0) return 0; + const current = libc.symbols.lseek(fd, 0n, SEEK_CUR); + if (current < 0n) { + if (libcErrno() === 29) return null; + throw ddSeekError(libcErrno()); + } + const target = current + BigInt(bytes); + ddValidateDeviceSeek(fd, target); + try { + const info = fstatSync(fd); + if (info.isFile() && target > BigInt(info.size)) { + const available = BigInt(info.size) > current ? BigInt(info.size) - current : 0n; + if (libc.symbols.lseek(fd, BigInt(info.size), SEEK_SET) < 0n) throw ddSeekError(libcErrno()); + return Number(available); + } + } catch (error) { + if (error?.ddSeek) throw error; + } + if (libc.symbols.lseek(fd, BigInt(bytes), SEEK_CUR) < 0n) throw ddSeekError(libcErrno()); + return bytes; +} + +export function ddSeekFd(fd, offset) { + const target = BigInt(offset); + ddValidateDeviceSeek(fd, target); + if (libc.symbols.lseek(fd, target, SEEK_SET) < 0n) throw ddSeekError(libcErrno()); +} + +export function ddValidateDeviceSeek(fd, target) { + let block = false; + try { + block = fstatSync(fd).isBlockDevice(); + } catch {} + if (!block) return; + const size = Buffer.alloc(8); + if (libc.symbols.ioctl(fd, BLKGETSIZE64, ptr(size)) !== 0 || target > size.readBigUInt64LE(0)) throw ddSeekError(22); +} + +export function ddSeekError(errno) { + const error = new Error(ddErrnoMessage(errno)); + error.ddSeek = true; + error.ddSystemMessage = ddErrnoMessage(errno); + return error; +} + +const singleCall = defineCommand("dd", ddCmd, (args) => { for (const arg of args) { if (arg === "--") return null; if (arg === "--help" || arg === "--version") return arg; } return null; }); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/df.js b/src/commands/df.js new file mode 100644 index 0000000..244347e --- /dev/null +++ b/src/commands/df.js @@ -0,0 +1,526 @@ +#!/usr/bin/env bun + +import { ptr } from "bun:ffi"; +import { readFileSync } from "node:fs"; +import { realpath, stat, statfs } from "node:fs/promises"; +import { resolve } from "node:path"; +import { bufferPathJoin, cstrPath, decodeUtf8SurrogateEscaped, invalidOptionMessage, isBytePath, libc, localeQuotedEscapedDiagnostic, normalizeLongOptionByPrefix, parseOptions, pathDisplayName, shellEscapeLsName, systemErrorMessage, textInputDiagnosticName } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { applyBlockSizeSpecialMode, defaultGNUBlockSize, gnuBlockSizeErrorMessage, humanSizeWithUnits, optionValues, parseGNUBlockSize, parseGNUBlockSizeEnvInfo, rawOperandPlan, validateBlockSizeMetaOption } from "../shared/filesystem.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const DF_LONG_OPTIONS = ["all", "human-readable", "si", "inodes", "local", "block-size", "total", "output", "type", "exclude-type", "print-type", "sync", "no-sync", "portability", "help", "version"]; + +export function dfMetaOption(args) { + const longFlagOptions = new Set(["all", "human-readable", "si", "inodes", "local", "total", "print-type", "sync", "no-sync", "portability"]); + const longValueOptions = new Set(["block-size", "output", "type", "exclude-type"]); + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + let normalized = arg; + if (arg.startsWith("--")) normalized = normalizeDfLongOption(arg); + const [name, inlineValue] = normalized.startsWith("--") ? normalized.slice(2).split(/=(.*)/s, 2) : [null, undefined]; + if (name === "help" || name === "version") { + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + return normalized; + } + if (longFlagOptions.has(name)) { + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + continue; + } + if (longValueOptions.has(name)) { + const value = inlineValue ?? args[i + 1]; + if (name === "block-size" && value !== undefined) validateBlockSizeMetaOption("--block-size", value); + if (name === "output" && inlineValue !== undefined) dfOutputFields([inlineValue]); + if (inlineValue === undefined) i++; + continue; + } + if (arg.startsWith("-") && !arg.startsWith("--")) { + i = scanDfShortMetaOption(args, i); + continue; + } + if (arg.startsWith("-")) throw new UsageError(invalidOptionMessage(arg), true); + } + return null; +} + +export function scanDfShortMetaOption(args, index) { + const arg = args[index]; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if ("ahHiklPT".includes(ch)) continue; + if (ch === "B") { + const inlineValue = arg.slice(j + 1); + const value = inlineValue === "" ? args[index + 1] : inlineValue; + if (value !== undefined) validateBlockSizeMetaOption("-B", value); + return inlineValue === "" ? index + 1 : index; + } + if (ch === "t" || ch === "x") return arg.slice(j + 1) ? index : index + 1; + throw new UsageError(`invalid option -- '${ch}'`, true); + } + return index; +} + +export function dfBlockSizeValue(opts) { + return opts.B ?? opts["block-size"] ?? dfBlockSizeEnvValue(); +} + +export function dfBlockSizeEnvValue() { + return process.env.DF_BLOCK_SIZE ?? process.env.BLOCK_SIZE ?? process.env.BLOCKSIZE; +} + +export function humanSize(bytes, base = 1024) { + const units = ["B", "K", "M", "G", "T"]; + return humanSizeWithUnits(bytes, base, units); +} + +export async function dfCmd(args) { + args = normalizeBlockSizeShort(args); + args = normalizeDfLongOptions(args); + args = normalizeDfOutputAlias(args); + const outputValues = dfRepeatedOutputValues(args); + const { opts, operands } = parseOptions(args, { short: { a: false, h: false, H: false, i: false, k: false, l: false, P: false, T: false, t: "value", x: "value", B: "value" }, long: { all: false, "human-readable": false, si: false, inodes: false, local: false, "block-size": "value", total: false, output: "optional-value", type: "value", "exclude-type": "value", "print-type": false, sync: false, "no-sync": false, portability: false, help: false, version: false } }); + const rawOperands = rawOperandPlan("df", args, operands, { + valueOptions: ["--block-size", "--type", "--exclude-type"], + shortValueOptions: ["B", "t", "x"], + }); + if (dfShouldSync(args)) libc.symbols.sync(); + // With file operands, df reports the file systems containing those files. + // Without operands GNU df walks the mount table instead; treating that case + // as `df .` loses every other mounted filesystem. + const mountTableRequired = !operands.length + || opts.a || opts.all || opts.l || opts.local + || opts.t !== undefined || opts.type !== undefined + || opts.x !== undefined || opts["exclude-type"] !== undefined; + const mountTable = mountTableRequired ? await dfMountTable(opts.a || opts.all) : null; + if (mountTableRequired && mountTable == null) return 1; + const targets = operands.length ? (rawOperands ?? operands) : mountTable.map((mount) => mount.mount); + applyBlockSizeSpecialMode(opts, dfBlockSizeValue(opts)); + const human = opts.h || opts["human-readable"] || opts.H || opts.si; + if (opts.output !== undefined && (opts.i || opts.inodes || opts.P || opts.portability || opts.T || opts["print-type"])) { + const conflict = opts.i || opts.inodes ? "-i" : opts.P || opts.portability ? "-P" : "-T"; + throw new UsageError(`options ${conflict} and --output are mutually exclusive`, true); + } + const outputFields = opts.output !== undefined ? dfOutputFields(outputValues.length ? outputValues : [opts.output]) : null; + const blockSizeInfo = dfBlockSizeInfo(opts); + const blockSize = human ? 1 : blockSizeInfo.size; + const inodeMode = opts.i || opts.inodes; + const printType = opts.T || opts["print-type"]; + const includeTypes = dfTypeSet(optionValues(opts.t).concat(optionValues(opts.type))); + const excludeTypes = dfTypeSet(optionValues(opts.x).concat(optionValues(opts["exclude-type"]))); + const rows = []; + let failed = false; + for (let index = 0; index < targets.length; index++) { + const target = targets[index]; + const mountInfo = operands.length ? await dfMountInfo(target) : mountTable[index]; + if (mountInfo.overMounted) { + stderr(`df: cannot access ${shellEscapeLsName(pathDisplayName(target), true)}: over-mounted by another device\n`); + failed = true; + continue; + } + // Filter from mount metadata before calling statfs. Some namespace and + // kernel API mounts are intentionally inaccessible to an unprivileged + // caller; GNU df omits them in its default listing rather than turning a + // successful bare `df` into a failure. + if (includeTypes.size && !includeTypes.has(mountInfo.fstype)) continue; + if (excludeTypes.has(mountInfo.fstype)) continue; + if (!operands.length && !opts.a && !opts.all && dfIsPseudoFileSystem(mountInfo)) continue; + if (opts.l || opts.local) { + if (!dfIsLocalFileSystem(mountInfo)) continue; + } + let fs; + try { + // Bun's statfs wrapper can satisfy this from runtime state without an + // observable statfs(2). GNU df gathers each row with statfs, and in + // particular --sync promises that sync(2) precedes that query. + // Keep the native probe so the ordering and kernel-facing semantics are + // preserved, while retaining Bun's portable result decoding below. + const nativeStatfs = Buffer.alloc(120); + libc.symbols.statfs(cstrPath(target), ptr(nativeStatfs)); + fs = await statfs(target); + } catch (error) { + stderr(`df: ${textInputDiagnosticName(target)}: ${systemErrorMessage(error)}\n`); + failed = true; + continue; + } + const sizeTotal = Number(fs.blocks) * Number(fs.bsize); + const sizeAvail = Number(fs.bavail) * Number(fs.bsize); + const sizeUsed = sizeTotal - sizeAvail; + const inodeTotal = Number(fs.files); + const inodeAvail = Number(fs.ffree); + const inodeUsed = inodeTotal - inodeAvail; + rows.push({ + source: mountInfo.source, + name: mountInfo.source, + total: inodeMode ? inodeTotal : sizeTotal, + used: inodeMode ? inodeUsed : sizeUsed, + avail: inodeMode ? inodeAvail : sizeAvail, + sizeTotal, + sizeUsed, + sizeAvail, + inodeTotal, + inodeUsed, + inodeAvail, + mount: mountInfo.mount, + file: target, + fstype: mountInfo.fstype, + }); + } + if (!rows.length && !failed) { + stderr("df: no file systems processed\n"); + return 1; + } + if (opts.total) { + if (rows.length) { + rows.push({ + source: "total", + name: "total", + total: rows.reduce((sum, row) => sum + row.total, 0), + used: rows.reduce((sum, row) => sum + row.used, 0), + avail: rows.reduce((sum, row) => sum + row.avail, 0), + sizeTotal: rows.reduce((sum, row) => sum + row.sizeTotal, 0), + sizeUsed: rows.reduce((sum, row) => sum + row.sizeUsed, 0), + sizeAvail: rows.reduce((sum, row) => sum + row.sizeAvail, 0), + inodeTotal: rows.reduce((sum, row) => sum + row.inodeTotal, 0), + inodeUsed: rows.reduce((sum, row) => sum + row.inodeUsed, 0), + inodeAvail: rows.reduce((sum, row) => sum + row.inodeAvail, 0), + mount: outputFields && !outputFields.includes("source") ? "total" : "-", + file: "-", + fstype: "-", + }); + } else if (!failed) { + stderr("df: no file systems processed\n"); + failed = true; + } + } + if (!rows.length && failed) return 1; + const show = (value) => human ? humanSize(value, (opts.H || opts.si) ? 1000 : 1024) : String(Math.ceil(value / (inodeMode ? 1 : blockSize))); + if (outputFields) { + const headers = outputFields.map((field) => dfFieldHeader(field, blockSizeInfo.label, human)); + const values = rows.map((row) => outputFields.map((field) => dfFieldValue(field, row, show))); + const widths = headers.map((header, index) => Math.max(dfOutputFieldMinWidth(outputFields[index]), header.length, ...values.map((row) => row[index].length))); + stdout(`${dfFormatOutputRow(headers, widths, outputFields)}\n`); + for (const row of values) stdout(`${dfFormatOutputRow(row, widths, outputFields)}\n`); + return failed ? 1 : 0; + } + stdout(inodeMode + ? `Filesystem ${printType ? "Type " : ""} Inodes IUsed IFree IUse% Mounted on\n` + : dfDefaultHeader({ blockSizeLabel: blockSizeInfo.label, human, portability: opts.P || opts.portability, printType })); + for (const row of rows) { + const pct = row.total ? Math.ceil((row.used / row.total) * 100) : 0; + const name = dfSanitizeDisplayField(row.name); + const mount = dfSanitizeDisplayField(row.mount); + stdout(`${name.padEnd(14)} ${printType ? `${row.fstype.padEnd(5)} ` : ""}${show(row.total).padStart(9)} ${show(row.used).padStart(8)} ${show(row.avail).padStart(9)} ${String(pct).padStart(3)}% ${mount}\n`); + } + return failed ? 1 : 0; +} + +export function dfSanitizeDisplayField(value) { + return String(value).replace(/[\x00-\x1f\x7f]/g, "?"); +} + +export function dfShouldSync(args) { + // GNU option processing is order-sensitive here: --no-sync can override + // an earlier --sync, and vice versa. + let enabled = false; + for (const arg of args) { + if (arg === "--") break; + if (arg === "--sync") enabled = true; + else if (arg === "--no-sync") enabled = false; + } + return enabled; +} + +export function dfTypeSet(values) { + return new Set(values.flatMap((value) => String(value).split(","))); +} + +export function normalizeDfLongOptions(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(normalizeDfLongOption(arg)); + } + return out; +} + +export function normalizeDfLongOption(arg) { + return normalizeLongOptionByPrefix(arg, DF_LONG_OPTIONS); +} + +export function dfBlockSizeInfo(opts) { + if (opts.k) return { size: 1024, label: "1K-blocks" }; + const shortOption = opts.B !== undefined; + const explicit = opts.B !== undefined || opts["block-size"] !== undefined; + const value = explicit ? shortOption ? opts.B : opts["block-size"] : dfBlockSizeEnvValue(); + const defaultSize = defaultGNUBlockSize(); + if (value == null) return { size: defaultSize, label: dfDefaultBlockSizeLabel(defaultSize) }; + try { + if (!explicit) { + const parsed = parseGNUBlockSizeEnvInfo(value, defaultSize); + if (parsed.fallback === "default") return { size: parsed.size, label: dfDefaultBlockSizeLabel(parsed.size) }; + if (parsed.fallback === "numeric") return { size: parsed.size, label: `${parsed.size}B-blocks` }; + return { size: parsed.size, label: `${dfBlockSizeLabel(value, parsed.size)}-blocks` }; + } + const size = parseGNUBlockSize(value); + return { size, label: `${dfBlockSizeLabel(value, size)}-blocks` }; + } catch { + throw new UsageError(gnuBlockSizeErrorMessage(shortOption ? "-B" : "--block-size", value)); + } +} + +export function dfDefaultBlockSizeLabel(size) { + return size === 1024 ? "1K-blocks" : `${dfBlockSizeLabel(String(size), size)}-blocks`; +} + +export function dfBlockSizeLabel(value, size) { + const text = String(value).replace(/^'/, ""); + if (text === "1" || size === 1 && /^\+?1$/.test(text)) return "1B"; + if (/^\+?\d+$/.test(text)) { + const n = Number(text); + if (n === 1000) return "1kB"; + if (n === 1_000_000) return "1MB"; + if (n === 1_000_000_000) return "1GB"; + return `${n}B`; + } + const scaled = text.match(/^\+?(\d+)([A-Za-z]+)$/); + if (scaled) { + const n = Number(scaled[1]); + const suffix = scaled[2]; + const binary = { KiB: "K", MiB: "M", GiB: "G", TiB: "T", PiB: "P", EiB: "E", ZiB: "Z", YiB: "Y" }; + const decimal = { KB: "kB", kB: "kB", MB: "MB", mB: "MB", GB: "GB", gB: "GB", TB: "TB", tB: "TB", PB: "PB", EB: "EB", ZB: "ZB", YB: "YB" }; + if (binary[suffix]) return `${n}${binary[suffix]}`; + if (decimal[suffix]) return `${n}${decimal[suffix]}`; + } + return text.replace(/^\+/, ""); +} + +export async function dfMountInfo(target) { + const targetPath = await dfRealPath(target); + const entries = []; + let best = null; + let bestLength = -1; + let text = ""; + try { + text = dfReadMountInfoSync(); + } catch {} + for (const line of text.split("\n")) { + if (!line) continue; + const separator = line.indexOf(" - "); + if (separator === -1) continue; + const before = line.slice(0, separator).split(" "); + const after = line.slice(separator + 3).split(" "); + const mount = dfDecodeMountInfoField(before[4] ?? ""); + const entry = { mount, fstype: dfDecodeMountInfoField(after[0] ?? "-"), source: dfDecodeMountInfoField(after[1] ?? "-") }; + entries.push(entry); + if (!dfPathIsOnMount(targetPath, mount) || mount.length < bestLength) continue; + best = entry; + bestLength = mount.length; + } + const targetInfo = await stat(target).catch(() => null); + if (targetInfo?.isBlockDevice()) { + const deviceMount = entries.findLast((entry) => entry.source === targetPath); + if (deviceMount) { + const visibleMount = entries.findLast((entry) => entry.mount === deviceMount.mount); + if (visibleMount !== deviceMount) return { ...deviceMount, overMounted: true }; + return deviceMount; + } + } + return best ?? { mount: targetPath, fstype: "-", source: "-" }; +} + +export async function dfRealPath(target) { + if (!isBytePath(target)) return await realpath(target).catch(() => resolve(target)); + try { + return pathDisplayName(await realpath(target, { encoding: "buffer" })); + } catch { + const raw = Buffer.from(target); + if (raw[0] === 0x2f) return pathDisplayName(raw); + return pathDisplayName(bufferPathJoin(Buffer.from(process.cwd()), raw)); + } +} + +export async function dfMountTable(preserveDuplicates = false) { + let text; + try { + text = dfReadMountInfoSync(); + } catch { + return null; + } + const mounts = new Map(); + const allMounts = []; + for (const line of text.split("\n")) { + if (!line) continue; + const separator = line.indexOf(" - "); + if (separator === -1) continue; + const before = line.slice(0, separator).split(" "); + const after = line.slice(separator + 3).split(" "); + const mount = dfDecodeMountInfoField(before[4] ?? ""); + if (!mount) continue; + // A mount namespace can stack mounts at the same mountpoint. The final + // entry is the one visible to processes in the namespace. + const entry = { + mount, + fstype: dfDecodeMountInfoField(after[0] ?? "-"), + source: dfDecodeMountInfoField(after[1] ?? "-"), + }; + allMounts.push(entry); + mounts.set(mount, entry); + } + return preserveDuplicates ? allMounts : [...mounts.values()]; +} + +export function dfReadMountInfoSync() { + if (process.env.BNU_DF_MOUNTINFO_ERROR === "1") { + const error = new Error("No such file or directory"); + error.code = "ENOENT"; + throw error; + } + const path = process.env.BNU_DF_MOUNTINFO_FILE || "/proc/self/mountinfo"; + return readFileSync(path).toString("latin1"); +} + +export function dfIsPseudoFileSystem(mount) { + // This is the same practical distinction made by GNU df's default output: + // kernel API and namespace mounts are only shown with --all. + return new Set([ + "autofs", "binfmt_misc", "bpf", "cgroup", "cgroup2", "configfs", "debugfs", "devpts", + "fusectl", "hugetlbfs", "mqueue", "proc", "pstore", + "fuse.portal", "rootfs", "rpc_pipefs", "securityfs", "sysfs", "tracefs", + ]).has(mount.fstype); +} + +export function dfIsLocalFileSystem(mount) { + return !new Set(["9p", "afs", "cifs", "coda", "fuse.sshfs", "ncp", "nfs", "nfs4", "smbfs"]).has(mount.fstype); +} + +export function dfDecodeMountInfoField(field) { + const input = Buffer.from(String(field), "latin1"); + const output = []; + for (let index = 0; index < input.length; index++) { + if (input[index] === 0x5c && index + 3 < input.length + && input[index + 1] >= 0x30 && input[index + 1] <= 0x37 + && input[index + 2] >= 0x30 && input[index + 2] <= 0x37 + && input[index + 3] >= 0x30 && input[index + 3] <= 0x37) { + output.push(Number.parseInt(input.subarray(index + 1, index + 4).toString("ascii"), 8)); + index += 3; + } else { + output.push(input[index]); + } + } + return decodeUtf8SurrogateEscaped(Buffer.from(output)); +} + +export function dfPathIsOnMount(path, mount) { + if (mount === "/") return path.startsWith("/"); + return path === mount || path.startsWith(`${mount.replace(/\/+$/, "")}/`); +} + +export function normalizeBlockSizeShort(args) { + return args.flatMap((arg) => /^-B.+/.test(arg) ? ["-B", arg.slice(2)] : [arg]); +} + +export function normalizeDfOutputAlias(args) { + return args.map((arg) => normalizeDfOutputOption(arg) ?? arg); +} + +export function normalizeDfOutputOption(arg) { + if (!arg.startsWith("--")) return null; + const body = arg.slice(2); + const eq = body.indexOf("="); + const name = eq === -1 ? body : body.slice(0, eq); + if (!name || !"output".startsWith(name)) return null; + return eq === -1 ? "--output" : `--output=${body.slice(eq + 1)}`; +} + +export const DF_DEFAULT_OUTPUT_FIELDS = ["source", "fstype", "itotal", "iused", "iavail", "ipcent", "size", "used", "avail", "pcent", "file", "target"]; + +export function dfRepeatedOutputValues(args) { + const values = []; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--output") { + values.push(true); + } else if (arg.startsWith("--output=")) { + values.push(arg.slice("--output=".length)); + } + } + return values; +} + +export function dfOutputFields(values) { + const fields = values.some((value) => value === true) + ? DF_DEFAULT_OUTPUT_FIELDS + : values.flatMap((value) => String(value).split(",")); + const seen = new Set(); + for (const field of fields) { + if (!DF_DEFAULT_OUTPUT_FIELDS.includes(field)) throw new UsageError(`option --output: field ${localeQuotedEscapedDiagnostic(field)} unknown`, true); + if (seen.has(field)) throw new UsageError(`option --output: field ${localeQuotedEscapedDiagnostic(field)} used more than once`, true); + seen.add(field); + } + return fields; +} + +export function dfDefaultHeader({ blockSizeLabel, human, portability, printType }) { + if (human) return `Filesystem ${printType ? "Type " : ""} Size Used Avail Use% Mounted on\n`; + if (portability) return `Filesystem ${printType ? "Type " : ""}1024-blocks Used Available Capacity Mounted on\n`; + return `Filesystem ${printType ? "Type " : ""}${blockSizeLabel.padStart(9)} Used Available Use% Mounted on\n`; +} + +export function dfFieldHeader(field, blockSizeLabel, human) { + return ({ + source: "Filesystem", + fstype: "Type", + itotal: "Inodes", + iused: "IUsed", + iavail: "IFree", + ipcent: "IUse%", + size: human ? "Size" : blockSizeLabel, + used: "Used", + avail: "Avail", + pcent: "Use%", + file: "File", + target: "Mounted on", + })[field]; +} + +export function dfFieldValue(field, row, show) { + const pcent = row.sizeTotal ? `${Math.ceil((row.sizeUsed / row.sizeTotal) * 100)}%` : "0%"; + const ipcent = row.inodeTotal ? `${Math.ceil((row.inodeUsed / row.inodeTotal) * 100)}%` : "0%"; + return ({ + source: row.source, + fstype: row.fstype, + itotal: String(row.inodeTotal), + iused: String(row.inodeUsed), + iavail: String(row.inodeAvail), + ipcent, + size: show(row.sizeTotal), + used: show(row.sizeUsed), + avail: show(row.sizeAvail), + pcent, + file: row.file, + target: row.mount, + })[field]; +} + +export function dfOutputFieldMinWidth(field) { + return field === "source" ? 14 : 0; +} + +export function dfFormatOutputRow(values, widths, fields) { + const leftAligned = new Set(["source", "fstype", "file", "target"]); + return values.map((value, index) => { + if (leftAligned.has(fields[index])) return index === values.length - 1 ? value : value.padEnd(widths[index]); + return value.padStart(widths[index]); + }).join(" "); +} + +const singleCall = defineCommand("df", dfCmd, dfMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/dir.js b/src/commands/dir.js new file mode 100644 index 0000000..408772c --- /dev/null +++ b/src/commands/dir.js @@ -0,0 +1,13 @@ +#!/usr/bin/env bun + +import { lsCmd, lsMetaOption } from "../shared/listing.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export async function dirCmd(args) { + return lsCmd(["-C", "--quoting-style=escape", ...args]); +} + +const singleCall = defineCommand("dir", dirCmd, lsMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/dircolors.js b/src/commands/dircolors.js new file mode 100644 index 0000000..32f95fd --- /dev/null +++ b/src/commands/dircolors.js @@ -0,0 +1,336 @@ +#!/usr/bin/env bun + +import { readFile } from "node:fs/promises"; +import { globMatch, localeQuotedEscapedDiagnostic, normalizeLongOptionByPrefix, normalizeLongOptionsByPrefix, parseOptions, systemErrorMessage, textInputDiagnosticName } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const DIRCOLORS_LONG_OPTIONS = ["sh", "bourne-shell", "csh", "c-shell", "print-database", "print-ls-colors", "help", "version"]; + +export async function dircolors(args) { + args = normalizeDircolorsLongOptions(args); + const { opts, operands } = parseOptions(args, { short: { b: false, c: false, p: false }, long: { sh: false, "bourne-shell": false, csh: false, "c-shell": false, "print-database": false, "print-ls-colors": false, help: false, version: false } }); + const db = DEFAULT_DIRCOLORS_DATABASE; + if (operands.length > 1) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[1])}`, true); + if ((opts.p || opts["print-database"]) && (opts["print-ls-colors"] || opts["print-ls"])) throw new UsageError("options --print-database and --print-ls-colors are mutually exclusive", true); + if ((opts.b || opts.sh || opts["bourne-shell"] || opts.c || opts.csh || opts["c-shell"]) && (opts.p || opts["print-database"])) { + throw new UsageError("the options to output non shell syntax,\nand to select a shell syntax are mutually exclusive", true); + } + if ((opts.p || opts["print-database"]) && operands.length > 0) { + throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[0])}\nfile operands cannot be combined with --print-database (-p)`, true); + } + if (opts.p || opts["print-database"]) { + stdout(db); + return 0; + } + let input; + if (operands[0]) { + try { + input = operands[0] === "-" ? await readStdinText() : await readFile(operands[0], "utf8"); + } catch (error) { + const name = textInputDiagnosticName(operands[0]); + const message = error?.code === "EISDIR" ? `${name}: read error: ${systemErrorMessage(error)}` : `${name}: ${systemErrorMessage(error)}`; + stderr(`dircolors: ${message}\n`); + return 1; + } + } else { + input = db; + } + const parsed = dircolorsValue(input, operands[0] ?? null); + if (opts["print-ls-colors"] || opts["print-ls"]) { + stdout(parsed.parts.map((part) => { + const [key, value] = part.split("=", 2); + return `\x1b[${value.replaceAll("\\:", ":")}m${key}\t${value.replaceAll("\\:", ":")}\x1b[0m\n`; + }).join("")); + return 0; + } + const value = parsed.parts.length ? `${parsed.parts.join(":")}:` : ""; + if (opts.c || opts.csh || opts["c-shell"]) stdout(`setenv LS_COLORS '${value}'\n`); + else stdout(`LS_COLORS='${shellSingleQuoteContent(value)}';\nexport LS_COLORS\n`); + return 0; +} + +export const DEFAULT_DIRCOLORS_DATABASE = [ + "RESET 0", + "DIR 01;34", + "LINK 01;36", + "MULTIHARDLINK 00", + "FIFO 40;33", + "SOCK 01;35", + "DOOR 01;35", + "BLK 40;33;01", + "CHR 40;33;01", + "ORPHAN 01;05;37;41", + "MISSING 01;05;37;41", + "SETUID 37;41", + "SETGID 30;43", + "CAPABILITY 00", + "STICKY_OTHER_WRITABLE 30;42", + "OTHER_WRITABLE 34;42", + "STICKY 37;44", + "EXEC 01;32", + ".7z 01;31", + ".ace 01;31", + ".alz 01;31", + ".apk 01;31", + ".arc 01;31", + ".arj 01;31", + ".bz 01;31", + ".bz2 01;31", + ".cab 01;31", + ".cpio 01;31", + ".crate 01;31", + ".deb 01;31", + ".drpm 01;31", + ".dwm 01;31", + ".dz 01;31", + ".ear 01;31", + ".egg 01;31", + ".esd 01;31", + ".gz 01;31", + ".jar 01;31", + ".lha 01;31", + ".lrz 01;31", + ".lz 01;31", + ".lz4 01;31", + ".lzh 01;31", + ".lzma 01;31", + ".lzo 01;31", + ".pyz 01;31", + ".rar 01;31", + ".rpm 01;31", + ".rz 01;31", + ".sar 01;31", + ".swm 01;31", + ".t7z 01;31", + ".tar 01;31", + ".taz 01;31", + ".tbz 01;31", + ".tbz2 01;31", + ".tgz 01;31", + ".tlz 01;31", + ".txz 01;31", + ".tz 01;31", + ".tzo 01;31", + ".tzst 01;31", + ".udeb 01;31", + ".war 01;31", + ".whl 01;31", + ".wim 01;31", + ".xz 01;31", + ".z 01;31", + ".zip 01;31", + ".zoo 01;31", + ".zst 01;31", + ".avif 01;35", + ".jpg 01;35", + ".jpeg 01;35", + ".jxl 01;35", + ".mjpg 01;35", + ".mjpeg 01;35", + ".gif 01;35", + ".bmp 01;35", + ".pbm 01;35", + ".pgm 01;35", + ".ppm 01;35", + ".tga 01;35", + ".xbm 01;35", + ".xpm 01;35", + ".tif 01;35", + ".tiff 01;35", + ".png 01;35", + ".svg 01;35", + ".svgz 01;35", + ".mng 01;35", + ".pcx 01;35", + ".mov 01;35", + ".mpg 01;35", + ".mpeg 01;35", + ".m2v 01;35", + ".mkv 01;35", + ".webm 01;35", + ".webp 01;35", + ".ogm 01;35", + ".mp4 01;35", + ".m4v 01;35", + ".mp4v 01;35", + ".vob 01;35", + ".qt 01;35", + ".nuv 01;35", + ".wmv 01;35", + ".asf 01;35", + ".rm 01;35", + ".rmvb 01;35", + ".flc 01;35", + ".avi 01;35", + ".fli 01;35", + ".flv 01;35", + ".gl 01;35", + ".dl 01;35", + ".xcf 01;35", + ".xwd 01;35", + ".yuv 01;35", + ".cgm 01;35", + ".emf 01;35", + ".ogv 01;35", + ".ogx 01;35", + ".cfg 00;32", + ".conf 00;32", + ".diff 00;32", + ".doc 00;32", + ".ini 00;32", + ".log 00;32", + ".patch 00;32", + ".pdf 00;32", + ".ps 00;32", + ".tex 00;32", + ".txt 00;32", + ".aac 00;36", + ".au 00;36", + ".flac 00;36", + ".m4a 00;36", + ".mid 00;36", + ".midi 00;36", + ".mka 00;36", + ".mp3 00;36", + ".mpc 00;36", + ".ogg 00;36", + ".ra 00;36", + ".wav 00;36", + ".oga 00;36", + ".opus 00;36", + ".spx 00;36", + ".xspf 00;36", + "*~ 00;90", + "*# 00;90", + ".bak 00;90", + ".crdownload 00;90", + ".dpkg-dist 00;90", + ".dpkg-new 00;90", + ".dpkg-old 00;90", + ".dpkg-tmp 00;90", + ".old 00;90", + ".orig 00;90", + ".part 00;90", + ".rej 00;90", + ".rpmnew 00;90", + ".rpmorig 00;90", + ".rpmsave 00;90", + ".swp 00;90", + ".tmp 00;90", + ".ucf-dist 00;90", + ".ucf-new 00;90", + ".ucf-old 00;90", + "", +].join("\n"); + +export function dircolorsMetaOption(args) { + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (arg.startsWith("--")) { + const normalized = normalizeDircolorsLongOption(arg); + const name = normalized.slice(2).split("=", 1)[0]; + if (!DIRCOLORS_LONG_OPTIONS.includes(name) || normalized.includes("=")) return null; + if (normalized === "--help" || normalized === "--version") return normalized; + continue; + } + if (arg === "-b" || arg === "-c" || arg === "-p") continue; + if (arg.startsWith("-") && arg !== "-") return null; + } + return null; +} + +export function normalizeDircolorsLongOptions(args) { + return normalizeLongOptionsByPrefix(args, DIRCOLORS_LONG_OPTIONS); +} + +export function normalizeDircolorsLongOption(arg) { + return normalizeLongOptionByPrefix(arg, DIRCOLORS_LONG_OPTIONS); +} + +export async function readStdinText() { + const data = await Bun.stdin.arrayBuffer(); + return new TextDecoder().decode(data); +} + +export function dircolorsValue(db, sourceName = null) { + const keys = { + RESET: "rs", + NORMAL: "no", + FILE: "fi", + DIR: "di", + LINK: "ln", + FIFO: "pi", + SOCK: "so", + DOOR: "do", + BLK: "bd", + CHR: "cd", + ORPHAN: "or", + MISSING: "mi", + SETUID: "su", + SETGID: "sg", + CAPABILITY: "ca", + STICKY_OTHER_WRITABLE: "tw", + OTHER_WRITABLE: "ow", + STICKY: "st", + EXEC: "ex", + MULTIHARDLINK: "mh", + OWT: "tw", + LEFTCODE: "lc", + RIGHTCODE: "rc", + ENDCODE: "ec", + }; + const parts = []; + let active = true; + let sawSelector = false; + let lineNo = 0; + for (const raw of db.split("\n")) { + lineNo++; + const line = raw.trimStart().startsWith("#") ? "" : raw.replace(/\s+#.*/, "").trim(); + if (!line) continue; + const match = line.match(/^(\S+)(?:\s+(.+))?$/); + const key = match?.[1]?.toUpperCase(); + let value = match?.[2]; + if (!value) throw new UsageError(`${sourceName ? `${sourceName}:` : ""}${lineNo}: invalid line; missing second token`); + if (key === "TERM" || key === "COLORTERM") { + sawSelector = true; + const envValue = key === "TERM" ? process.env.TERM ?? "none" : process.env.COLORTERM ?? ""; + active = globMatch(value, envValue); + continue; + } + if (!sawSelector || active) { + const mapped = keys[key]; + if (mapped) parts.push(`${mapped}=${dircolorsEscapeValue(value)}`); + else if (key.startsWith(".")) parts.push(`*${match[1]}=${dircolorsEscapeValue(value)}`); + else if (key.startsWith("*")) parts.push(`${match[1]}=${dircolorsEscapeValue(value)}`); + } + } + return { parts }; +} + +export function dircolorsEscapeValue(value) { + let out = ""; + let escaped = false; + for (const ch of value) { + if (escaped) { + out += ch === ":" ? "\\:" : `\\${ch}`; + escaped = false; + } else if (ch === "\\") { + escaped = true; + } else { + out += ch === ":" ? "\\:" : ch; + } + } + if (escaped) out += "\\"; + return out; +} + +export function shellSingleQuoteContent(value) { + return value.replaceAll("'", "'\\''"); +} + +const singleCall = defineCommand("dircolors", dircolors, dircolorsMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/dirname.js b/src/commands/dirname.js new file mode 100644 index 0000000..1dc251f --- /dev/null +++ b/src/commands/dirname.js @@ -0,0 +1,112 @@ +#!/usr/bin/env bun + +import { isBytePath } from "../shared/common.js"; +import { UsageError, stdout } from "../shared/diagnostics.js"; +import { rawOperandPlan } from "../shared/filesystem.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const DIRNAME_LONG_OPTIONS = ["zero", "help", "version"]; + +export function dirnameMetaOption(args) { + for (const arg of args) { + if (arg === "--") return null; + if (!arg.startsWith("-") || arg === "-") { + if (process.env.POSIXLY_CORRECT) return null; + continue; + } + if (!arg.startsWith("--")) { + for (const ch of arg.slice(1)) { + if (ch !== "z") return null; + } + continue; + } + const option = normalizeDirnameLongOption(arg); + const name = option.slice(2).split("=", 1)[0]; + if (!DIRNAME_LONG_OPTIONS.includes(name) || option.includes("=")) return null; + if (option === "--help" || option === "--version") return option; + } + return null; +} + +export async function dirname(args) { + const { opts, operands } = parseDirnameOptions(args); + if (!operands.length) throw new UsageError("missing operand", true); + const sep = opts.z || opts.zero ? "\0" : "\n"; + const rawOperands = rawOperandPlan("dirname", args, operands); + const output = (rawOperands ?? operands).map((name) => isBytePath(name) ? gnuDirnameBytes(name) : gnuDirname(name)); + stdout(output.some(isBytePath) + ? Buffer.concat(output.flatMap((value) => [isBytePath(value) ? Buffer.from(value) : Buffer.from(value), Buffer.from(sep)])) + : `${output.join(sep)}${sep}`); + return 0; +} + +export function gnuDirnameBytes(name) { + const bytes = Buffer.from(name); + if (!bytes.length) return Buffer.from("."); + if (bytes.every((byte) => byte === 0x2f)) return Buffer.from("/"); + let end = bytes.length; + while (end > 0 && bytes[end - 1] === 0x2f) end--; + const slash = bytes.lastIndexOf(0x2f, end - 1); + if (slash === -1) return Buffer.from("."); + let dirEnd = slash; + while (dirEnd > 0 && bytes[dirEnd - 1] === 0x2f) dirEnd--; + return dirEnd === 0 ? Buffer.from("/") : bytes.subarray(0, dirEnd); +} + +export function parseDirnameOptions(args) { + const opts = {}; + const operands = []; + 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 option = normalizeDirnameLongOption(arg); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (!DIRNAME_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); + opts[name] = true; + continue; + } + for (const ch of arg.slice(1)) { + if (ch === "z") opts.z = true; + else throw new UsageError(`invalid option -- '${ch}'`, true); + } + } + return { opts, operands }; +} + +export function normalizeDirnameLongOption(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 = DIRNAME_LONG_OPTIONS.find((option) => option.startsWith(name)); + if (!match) return arg; + return eq === -1 ? `--${match}` : `--${match}=${body.slice(eq + 1)}`; +} + +export function gnuDirname(name) { + let text = String(name); + if (text === "") return "."; + if (/^\/+$/.test(text)) return "/"; + text = text.replace(/\/+$/g, ""); + const idx = text.lastIndexOf("/"); + if (idx === -1) return "."; + let dir = text.slice(0, idx); + if (dir === "") return "/"; + if (/^\/+$/.test(dir)) return "/"; + return dir.replace(/\/+$/g, ""); +} + +const singleCall = defineCommand("dirname", dirname, dirnameMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/du.js b/src/commands/du.js new file mode 100644 index 0000000..9a8f773 --- /dev/null +++ b/src/commands/du.js @@ -0,0 +1,580 @@ +#!/usr/bin/env bun + +import { closeSync, constants as fsConstants, lstatSync, openSync, opendirSync } from "node:fs"; +import { lstat, readdir, realpath, stat } from "node:fs/promises"; +import { join, basename as pathBasename, dirname as pathDirname } from "node:path"; +import { bufferPathBasename, bufferPathJoin, createFdRecordReader, decodeSurrogateEscapedBytes, globMatch, invalidOptionMessage, isBytePath, localeQuotedDiagnostic, localeQuotedEscapedDiagnostic, normalizeLongOptionByPrefix, parseOptions, pathDisplayName, readAll, shellEscapeLsName, splitFiles0ByteNames, statAttachNanoseconds, systemErrorMessage, wcFileNameIsDash, wcFileNameIsEmpty, wcFiles0SourceIsNonRegular } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { applyBlockSizeSpecialMode, blocksFor, defaultGNUBlockSize, gnuBlockSizeErrorMessage, humanSizeWithUnits, openDirectoryPathEntries, parseGNUBlockSize, validateBlockSizeMetaOption } from "../shared/filesystem.js"; +import { lsTimeText, parseGNUBlockSizeEnv, strftime } from "../shared/time.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const DU_LONG_OPTIONS = ["null", "all", "apparent-size", "block-size", "bytes", "total", "dereference-args", "max-depth", "files0-from", "human-readable", "inodes", "dereference", "count-links", "no-dereference", "separate-dirs", "si", "summarize", "threshold", "time", "time-style", "exclude-from", "exclude", "one-file-system", "help", "version"]; + +export function duMetaOption(args) { + const longFlagOptions = new Set(["null", "all", "apparent-size", "bytes", "total", "dereference-args", "human-readable", "inodes", "dereference", "count-links", "no-dereference", "separate-dirs", "si", "summarize", "one-file-system"]); + const longValueOptions = new Set(["block-size", "max-depth", "files0-from", "threshold", "time-style", "exclude-from", "exclude"]); + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + let normalized = arg; + if (arg.startsWith("--")) normalized = normalizeLongOptionByPrefix(arg, DU_LONG_OPTIONS); + const [name, inlineValue] = normalized.startsWith("--") ? normalized.slice(2).split(/=(.*)/s, 2) : [null, undefined]; + if (name === "help" || name === "version") { + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + return normalized; + } + if (name === "time") { + if (inlineValue !== undefined) validateDuTimeField(inlineValue); + continue; + } + if (longFlagOptions.has(name)) { + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + continue; + } + if (longValueOptions.has(name)) { + const value = inlineValue ?? args[i + 1]; + if (name === "block-size" && value !== undefined) validateBlockSizeMetaOption("--block-size", value); + if (name === "threshold" && value !== undefined) duParseThreshold(value); + if (name === "max-depth" && value !== undefined) validateDuMaxDepthMetaOption(value, args, inlineValue === undefined ? i + 2 : i + 1); + if (inlineValue === undefined) i++; + continue; + } + if (arg.startsWith("-") && !arg.startsWith("--")) { + i = scanDuShortMetaOption(args, i); + continue; + } + if (arg.startsWith("-")) throw new UsageError(invalidOptionMessage(arg), true); + continue; + } + return null; +} + +export function scanDuShortMetaOption(args, index) { + const arg = args[index]; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if ("0aADHLPlsSbhckmx".includes(ch)) continue; + if (ch === "B") { + const inlineValue = arg.slice(j + 1); + const value = inlineValue === "" ? args[index + 1] : inlineValue; + if (value !== undefined) validateBlockSizeMetaOption("-B", value); + return inlineValue === "" ? index + 1 : index; + } + if (ch === "d") { + const inlineValue = arg.slice(j + 1); + const value = inlineValue === "" ? args[index + 1] : inlineValue; + if (value !== undefined) validateDuMaxDepthMetaOption(value, args, inlineValue === "" ? index + 2 : index + 1); + return inlineValue === "" ? index + 1 : index; + } + if (ch === "t") { + const inlineValue = arg.slice(j + 1); + const value = inlineValue === "" ? args[index + 1] : inlineValue; + if (value !== undefined) duParseThreshold(value, false, "-t"); + return inlineValue === "" ? index + 1 : index; + } + if (ch === "X") return arg.slice(j + 1) ? index : index + 1; + throw new UsageError(`invalid option -- '${ch}'`, true); + } + return index; +} + +export function validateDuMaxDepthMetaOption(value, args, startIndex) { + try { + parseDuMaxDepth(value, false); + } catch (error) { + if (!(error instanceof UsageError)) throw error; + if (!duHasMetaOptionAfter(args, startIndex)) return; + stderr(`du: ${error.message}\n`); + } +} + +export function duHasMetaOptionAfter(args, startIndex) { + for (let i = startIndex; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return false; + if (!arg.startsWith("--")) continue; + const normalized = normalizeLongOptionByPrefix(arg, DU_LONG_OPTIONS); + const [name, inlineValue] = normalized.slice(2).split(/=(.*)/s, 2); + if ((name === "help" || name === "version") && inlineValue === undefined) return true; + } + return false; +} + +export async function duCmd(args) { + args = normalizeDuArgs(args); + const repeatedExcludes = duRepeatedOptionValues(args, "--exclude"); + const repeatedExcludeFiles = duRepeatedOptionValues(args, "--exclude-from"); + const { opts, operands } = parseOptions(args, { short: { 0: false, a: false, A: false, D: false, H: false, L: false, P: false, l: false, s: false, S: false, b: false, h: false, c: false, B: "value", d: "value", k: false, m: false, t: "value", x: false }, long: { null: false, all: false, dereference: false, "dereference-args": false, "no-dereference": false, "count-links": false, summarize: false, "separate-dirs": false, bytes: false, "human-readable": false, "apparent-size": false, inodes: false, total: false, "block-size": "value", si: false, time: "optional-value", "time-style": "value", "max-depth": "value", exclude: "value", "exclude-from": "value", "files0-from": "value", "one-file-system": false, threshold: "value" } }); + applyBlockSizeSpecialMode(opts, duBlockSizeValue(opts)); + const maxDepth = parseDuMaxDepth(opts.d ?? opts["max-depth"], opts.s || opts.summarize); + const timeField = opts.time === undefined ? null : validateDuTimeField(opts.time); + let targets = operands.length ? operands : ["."]; + let files0From = null; + let files0Reader = null; + let files0Fd = 0; + let closeFiles0Fd = false; + if (opts["files0-from"] !== undefined) { + if (operands.length) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[0])}\nfile operands cannot be combined with --files0-from`, true); + files0From = opts["files0-from"]; + if (wcFiles0SourceIsNonRegular(files0From)) { + try { + if (files0From !== "-") { + files0Fd = openSync(files0From, "r"); + closeFiles0Fd = true; + } + files0Reader = createFdRecordReader(files0Fd, 0); + targets = []; + } catch (error) { + stderr(`du: cannot open ${duQuotedName(files0From)} for reading: ${systemErrorMessage(error)}\n`); + return 1; + } + } else { + let nameBytes; + try { + nameBytes = await readAll(files0From); + } catch (error) { + stderr(error?.code === "EISDIR" + ? `du: ${duDiagnosticName(files0From)}: read error: ${systemErrorMessage(error)}\n` + : `du: cannot open ${duQuotedName(files0From)} for reading: ${systemErrorMessage(error)}\n`); + return 1; + } + targets = splitFiles0ByteNames(nameBytes); + } + } + const inodeMode = opts.inodes; + const apparent = opts.b || opts.bytes || opts.A || opts["apparent-size"]; + if (inodeMode && apparent) stderr("du: warning: options --apparent-size and -b are ineffective with --inodes\n"); + const human = opts.h || opts["human-readable"] || opts.si; + const unit = inodeMode || opts.b || opts.bytes ? 1 : duBlockSize(opts); + const separator = opts[0] || opts.null ? "\0" : "\n"; + const thresholdText = opts.t ?? opts.threshold; + let threshold = null; + if (thresholdText != null) { + threshold = duParseThreshold(thresholdText, inodeMode); + if (!Number.isFinite(threshold)) throw new UsageError(`invalid --threshold argument '${thresholdText}'`); + } + const excludes = await duExcludePatterns(opts, repeatedExcludes, repeatedExcludeFiles); + const { dereference, dereferenceArgs } = duDereferenceMode(args, opts); + let totalBytes = 0; + let failed = false; + const emittedRootDirectories = new Set(); + const seen = new Set(); + const dirSeen = new Set(); + let reportedSyntheticTime = false; + for (let index = 0; files0Reader || index < targets.length; index++) { + let target; + if (files0Reader) { + try { + target = files0Reader.next(); + } catch (error) { + stderr(`du: ${duDiagnosticName(files0From)}: read error: ${systemErrorMessage(error)}\n`); + failed = true; + break; + } + if (target == null) break; + } else { + target = targets[index]; + } + if (files0From != null && wcFileNameIsEmpty(target)) { + stderr(`du: ${duDiagnosticName(files0From)}:${index + 1}: invalid zero-length file name\n`); + failed = true; + continue; + } + if (files0From === "-" && wcFileNameIsDash(target)) { + stderr("du: when reading file names from standard input, no file name of '-' allowed\n"); + failed = true; + continue; + } + let entries; + try { + const errors = []; + const rootLinkStat = await lstat(target); + const collectOptions = { apparent, inodes: inodeMode, all: opts.a || opts.all, dereference, dereferenceArgs, countLinks: opts.l || opts["count-links"], seen, dirSeen, separateDirs: opts.S || opts["separate-dirs"], maxDepth, excludes, oneFileSystem: opts.x || opts["one-file-system"], rootDev: rootLinkStat.dev, includeNanoseconds: timeField != null, errors }; + if (duCanUseSyncSummary(collectOptions)) { + const root = collectDuSummarySync(target, collectOptions); + entries = root ? [{ path: target, bytes: root.bytes, stat: root.stat }] : []; + } else { + entries = await collectDuEntries(target, collectOptions); + } + for (const diagnostic of errors) stderr(`${diagnostic}\n`); + if (errors.length) failed = true; + } catch (error) { + stderr(error?.duFtsReadPath != null + ? `du: fts_read failed: ${pathDisplayName(error.duFtsReadPath)}: ${systemErrorMessage(error)}\n` + : `du: cannot access ${duQuotedName(target)}: ${systemErrorMessage(error)}\n`); + failed = true; + continue; + } + const root = entries.at(-1); + if (!root) continue; + if (root.stat.isDirectory()) { + const id = `${root.stat.dev}:${root.stat.ino}`; + if (emittedRootDirectories.has(id)) entries = []; + else emittedRootDirectories.add(id); + } + const bytes = root.bytes; + totalBytes += bytes; + for (const entry of entries) { + if (threshold != null && !duPassesThreshold(entry.bytes, threshold)) continue; + const value = human ? duHumanSize(entry.bytes, opts.si ? 1000 : 1024) : String(Math.ceil(entry.bytes / unit)); + let time = ""; + if (timeField) { + const syntheticTime = process.env.BNU_DU_TEST_TIME_SECONDS; + if (syntheticTime != null) { + time = `\t${syntheticTime}`; + if (!reportedSyntheticTime) { + stderr(`du: time '${syntheticTime}' is out of range\n`); + reportedSyntheticTime = true; + } + } else { + time = `\t${duTimeText(entry.stat[timeField], opts)}`; + } + } + stdout(`${value}${time}\t${pathDisplayName(entry.path)}${separator}`); + } + } + if (opts.c || opts.total) { + const value = human ? duHumanSize(totalBytes, opts.si ? 1000 : 1024) : String(Math.ceil(totalBytes / unit)); + stdout(`${value}\ttotal${separator}`); + } + if (closeFiles0Fd) closeSync(files0Fd); + return failed ? 1 : 0; +} + +export function duQuotedName(name) { + return shellEscapeLsName(pathDisplayName(name), true); +} + +export function duDiagnosticName(name) { + return shellEscapeLsName(pathDisplayName(name)); +} + +export function normalizeDuArgs(args) { + const out = []; + let end = false; + for (const arg of args) { + if (end || arg === "--") { + out.push(arg); + if (arg === "--") end = true; + continue; + } + out.push(arg.startsWith("--") ? normalizeLongOptionByPrefix(arg, DU_LONG_OPTIONS) : arg); + } + return out; +} + +export function parseDuMaxDepth(value, summarize) { + if (value == null) return summarize ? 0 : Infinity; + const text = String(value); + if (!/^[+-]?\d+$/.test(text)) throw new UsageError(`invalid maximum depth ${localeQuotedEscapedDiagnostic(text)}`, true); + const parsed = BigInt(text.replace(/^\+/, "")); + if (summarize) { + const display = text.replace(/^\+/, ""); + if (parsed !== 0n) throw new UsageError(`warning: summarizing conflicts with --max-depth=${display}`, true); + if (parsed === 0n) stderr("du: warning: summarizing is the same as using --max-depth=0\n"); + return 0; + } + if (parsed < 0n) return 0; + return parsed > BigInt(Number.MAX_SAFE_INTEGER) ? Number.MAX_SAFE_INTEGER : Number(parsed); +} + +export function duDereferenceMode(args, opts) { + let mode = opts.L || opts.dereference ? "all" : opts.D || opts.H || opts["dereference-args"] ? "args" : "none"; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") break; + if (arg === "-" || !arg.startsWith("-")) continue; + if (arg.startsWith("--")) { + const [name, inlineValue] = arg.slice(2).split(/=(.*)/s, 2); + if (name === "dereference") mode = "all"; + else if (name === "dereference-args") mode = "args"; + else if (name === "no-dereference") mode = "none"; + if (inlineValue == null && ["block-size", "time-style", "max-depth", "exclude", "exclude-from", "files0-from", "threshold"].includes(name)) i++; + continue; + } + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (ch === "L") mode = "all"; + else if (ch === "D" || ch === "H") mode = "args"; + else if (ch === "P") mode = "none"; + if (["B", "d"].includes(ch)) break; + } + } + return { dereference: mode === "all", dereferenceArgs: mode === "args" }; +} + +export function duPassesThreshold(value, threshold) { + return threshold < 0 ? value <= Math.abs(threshold) : value >= threshold; +} + +export function duParseThreshold(value, inodeMode = false, option = "--threshold") { + const text = String(value); + const sign = text.startsWith("-") ? -1 : 1; + const unsigned = text.replace(/^[+-]/, ""); + if (!/^\d/.test(unsigned)) throw new UsageError(`invalid ${option} argument '${value}'`); + const match = unsigned.match(/^(\d+)([A-Za-z]*)$/); + if (!match) throw new UsageError(`invalid suffix in ${option} argument '${value}'`); + const suffixScales = { + "": 1n, + K: 1024n, k: 1024n, KiB: 1024n, kiB: 1024n, + M: 1024n ** 2n, m: 1024n ** 2n, MiB: 1024n ** 2n, miB: 1024n ** 2n, + G: 1024n ** 3n, g: 1024n ** 3n, GiB: 1024n ** 3n, giB: 1024n ** 3n, + T: 1024n ** 4n, t: 1024n ** 4n, TiB: 1024n ** 4n, tiB: 1024n ** 4n, + P: 1024n ** 5n, PiB: 1024n ** 5n, + E: 1024n ** 6n, EiB: 1024n ** 6n, + Z: 1024n ** 7n, ZiB: 1024n ** 7n, + Y: 1024n ** 8n, YiB: 1024n ** 8n, + R: 1024n ** 9n, RiB: 1024n ** 9n, + Q: 1024n ** 10n, QiB: 1024n ** 10n, + KB: 1000n, kB: 1000n, MB: 1000n ** 2n, mB: 1000n ** 2n, + GB: 1000n ** 3n, gB: 1000n ** 3n, TB: 1000n ** 4n, tB: 1000n ** 4n, + PB: 1000n ** 5n, EB: 1000n ** 6n, ZB: 1000n ** 7n, YB: 1000n ** 8n, + RB: 1000n ** 9n, QB: 1000n ** 10n, + }; + const scale = suffixScales[match[2]]; + if (!scale) { + throw new UsageError(`invalid suffix in ${option} argument '${value}'`); + } + const amount = BigInt(match[1]) * scale; + if (sign < 0 && amount === 0n) throw new UsageError(`invalid --threshold argument '${value}'`); + const max = sign < 0 ? 9223372036854775808n : 9223372036854775807n; + if (amount > max) throw new UsageError(`${option} argument '${value}' too large`); + const clamped = amount > BigInt(Number.MAX_SAFE_INTEGER) ? Number.MAX_SAFE_INTEGER : Number(amount); + return sign * clamped; +} + +export async function collectDuEntries(path, options, depth = 0) { + const node = await collectDuNode(path, options, depth); + return node?.entries ?? []; +} + +export function duCanUseSyncSummary(options) { + return options.maxDepth === 0 && !options.all && !options.dereference && !options.dereferenceArgs + && !options.countLinks && !options.separateDirs && !options.oneFileSystem + && !options.includeNanoseconds && options.excludes.length === 0; +} + +export function collectDuSummarySync(path, options, depth = 0, displayPath = path) { + const s = lstatSync(path); + const identity = `${s.dev}:${s.ino}`; + if (s.isDirectory()) { + if (options.dirSeen.has(identity)) return { bytes: 0, stat: s }; + options.dirSeen.add(identity); + } else if (s.nlink > 1 || depth === 0) { + if (options.seen.has(identity)) return { bytes: 0, stat: s }; + options.seen.add(identity); + } + let bytes = options.inodes ? 1 : options.apparent && s.isDirectory() ? 0 : options.apparent ? s.size : blocksFor(s) * 512; + if (!s.isDirectory() || s.isSymbolicLink()) return { bytes, stat: s }; + // Walk relative to an open directory fd. Concatenating descendants onto + // the original operand eventually exceeds PATH_MAX even though every + // component is valid; /proc/self/fd gives Node's path APIs openat-like + // behavior while preserving byte names and without changing an unreadable + // caller's cwd. + let directoryFd; + let directory = null; + try { + try { + directoryFd = openSync(path, fsConstants.O_RDONLY | fsConstants.O_DIRECTORY); + directory = opendirSync(`/proc/self/fd/${directoryFd}`, { encoding: "buffer" }); + } catch (error) { + options.errors?.push(`du: cannot read directory '${pathDisplayName(displayPath)}': ${systemErrorMessage(error)}`); + return { bytes, stat: s }; + } + while (true) { + const rawEntry = directory.readSync(); + if (!rawEntry) break; + const entry = rawEntry.name ?? rawEntry; + try { + const child = collectDuSummarySync( + duChildPath(`/proc/self/fd/${directoryFd}`, entry), + options, + depth + 1, + duChildPath(displayPath, entry), + ); + if (child) bytes += child.bytes; + } catch (error) { + options.errors?.push(`du: cannot read directory '${pathDisplayName(displayPath)}': ${systemErrorMessage(error)}`); + } + } + } finally { + directory?.closeSync(); + if (directoryFd != null) closeSync(directoryFd); + } + return { bytes, stat: s }; +} + +export async function collectDuNode(path, options, depth = 0) { + if (duIsExcluded(path, options.excludes)) return null; + const linkStat = await lstat(path); + const dereference = options.dereference || (options.dereferenceArgs && depth === 0); + const s = dereference && linkStat.isSymbolicLink() ? await stat(await realpath(path)) : linkStat; + if (options.includeNanoseconds) statAttachNanoseconds(s, path, dereference); + const identity = `${s.dev}:${s.ino}`; + // A single-link regular file cannot be encountered through another hard + // link, so retaining its identity only turns a wide directory into an + // O(number-of-files) heap. Directories have their own cycle set below. + const trackHardLink = !s.isDirectory() && (s.nlink > 1 || depth === 0); + const alreadySeen = !options.countLinks && trackHardLink && options.seen.has(identity); + if (trackHardLink) options.seen.add(identity); + const ownBytes = options.inodes ? 1 : options.apparent && s.isDirectory() ? 0 : options.apparent ? s.size : blocksFor(s) * 512; + if (s.isDirectory()) { + if (options.dirSeen.has(identity)) return { bytes: 0, entries: [], stat: s }; + options.dirSeen.add(identity); + } + if (alreadySeen) return { bytes: 0, entries: [], stat: s }; + if (!s.isDirectory() || s.isSymbolicLink()) { + const bytes = ownBytes; + const emit = (depth === 0 || options.all) && depth <= options.maxDepth; + return { bytes, entries: emit ? [{ path, bytes, stat: s }] : [], stat: s }; + } + let total = ownBytes; + const entries = []; + if (options.oneFileSystem && s.dev !== options.rootDev) { + if (depth <= options.maxDepth) entries.push({ path, bytes: total, stat: s }); + return { bytes: total, entries, stat: s }; + } + let names; + try { + names = options.maxDepth === 0 && !options.all + ? await openDirectoryPathEntries(path) + : await readdir(path, isBytePath(path) ? { encoding: "buffer" } : undefined); + } catch (error) { + options.errors?.push(`du: cannot read directory '${pathDisplayName(path)}': ${systemErrorMessage(error)}`); + if (depth <= options.maxDepth) entries.push({ path, bytes: total, stat: s }); + return { bytes: total, entries, stat: s }; + } + // readdir opens the directory before returning its names. If an ancestor + // is renamed concurrently, those names are stale relative to this pathname. + // GNU fts reports one traversal failure for the opened directory rather than + // one ENOENT for every descendant from the snapshot. + try { + await lstat(path); + } catch (error) { + if (error?.code === "ENOENT") throw duFtsReadError(await duFtsFailurePath(path), error); + throw error; + } + for await (const entry of names) { + const childPath = duChildPath(path, entry); + let child; + try { + child = await collectDuNode(childPath, options, depth + 1); + } catch (error) { + if (error?.duFtsReadPath != null) throw error; + if (error?.code === "ENAMETOOLONG") continue; + if (error?.code === "ENOENT" && !(await lstat(path).catch(() => null))) throw duFtsReadError(await duFtsFailurePath(path), error); + options.errors?.push(`du: cannot read directory '${pathDisplayName(path)}': ${systemErrorMessage(error)}`); + continue; + } + if (!child) continue; + const skippedOtherFsDirectory = options.oneFileSystem && child.stat.isDirectory() && child.stat.dev !== options.rootDev; + if (!skippedOtherFsDirectory) entries.push(...child.entries); + if (!skippedOtherFsDirectory && (!options.separateDirs || !child.stat.isDirectory() || child.stat.isSymbolicLink())) total += child.bytes; + } + if (depth <= options.maxDepth) entries.push({ path, bytes: total, stat: s }); + return { bytes: total, entries, stat: s }; +} + +export function duFtsReadError(path, cause) { + const error = new Error(cause?.message ?? "No such file or directory"); + error.code = cause?.code ?? "ENOENT"; + error.duFtsReadPath = path; + return error; +} + +export async function duFtsFailurePath(path) { + let current = String(path); + while (true) { + const parent = pathDirname(current); + if (parent === current || await lstat(parent).catch(() => null)) return current; + current = parent; + } +} + +export function duChildPath(parent, entry) { + if (parent === ".") return `./${entry}`; + if (typeof parent === "string" && parent.startsWith("./")) return `./${join(parent.slice(2), String(entry))}`; + if (isBytePath(parent) || isBytePath(entry)) return bufferPathJoin(isBytePath(parent) ? parent : Buffer.from(String(parent)), isBytePath(entry) ? Buffer.from(entry) : Buffer.from(String(entry))); + return join(parent, entry); +} + +export function duRepeatedOptionValues(args, name) { + const values = []; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === name) values.push(args[++i]); + else if (arg.startsWith(`${name}=`)) values.push(arg.slice(name.length + 1)); + } + return values.filter((value) => value != null); +} + +export async function duExcludePatterns(opts, excludeValues = [], excludeFiles = []) { + const patterns = [...excludeValues]; + if (opts.exclude && !patterns.includes(opts.exclude)) patterns.push(opts.exclude); + const files = [...excludeFiles]; + if (opts["exclude-from"] && !files.includes(opts["exclude-from"])) files.push(opts["exclude-from"]); + for (const file of files) { + let text; + try { + text = decodeSurrogateEscapedBytes(await readAll(file)); + } catch (error) { + throw new UsageError(`${file}: ${systemErrorMessage(error)}`, true); + } + patterns.push(...text.split(/\r?\n/).filter(Boolean)); + } + return patterns; +} + +export function duIsExcluded(path, patterns) { + const normalized = pathDisplayName(path).replace(/^\.\//, ""); + const base = isBytePath(path) ? pathDisplayName(bufferPathBasename(path)) : pathBasename(normalized); + return patterns.some((pattern) => normalized === pattern || base === pattern || normalized.startsWith(`${pattern}/`) || globMatch(pattern, normalized) || globMatch(pattern, base)); +} + +export function duTimeText(date, opts) { + const style = opts["time-style"] ?? "long-iso"; + if (style === "iso" || style === "posix-iso") return strftime(date, "%F"); + return lsTimeText(date, { "time-style": style }); +} + +export function validateDuTimeField(value) { + if (value === true) return "mtime"; + if (["atime", "access", "use"].includes(value)) return "atime"; + if (["ctime", "status"].includes(value)) return "ctime"; + const kind = value === "" ? "ambiguous" : "invalid"; + throw new UsageError(`${kind} argument ${localeQuotedEscapedDiagnostic(value)} for ${localeQuotedDiagnostic("--time")}\nValid arguments are:\n - ${["atime", "access", "use"].map(localeQuotedDiagnostic).join(", ")}\n - ${["ctime", "status"].map(localeQuotedDiagnostic).join(", ")}`, true); +} + +export function duBlockSize(opts) { + if (opts.k) return 1024; + if (opts.m) return 1024 ** 2; + if (opts.B !== undefined || opts["block-size"] !== undefined) { + const shortOption = opts.B !== undefined; + const value = shortOption ? opts.B : opts["block-size"]; + try { + return parseGNUBlockSize(value); + } catch { + throw new UsageError(gnuBlockSizeErrorMessage(shortOption ? "-B" : "--block-size", value)); + } + } + return parseGNUBlockSizeEnv(duBlockSizeEnvValue(), defaultGNUBlockSize()); +} + +export function duHumanSize(value, base = 1024) { + const units = base === 1000 ? ["B", "k", "M", "G", "T"] : ["B", "K", "M", "G", "T"]; + return humanSizeWithUnits(value, base, units); +} + +export function duBlockSizeValue(opts) { + return opts.B ?? opts["block-size"] ?? duBlockSizeEnvValue(); +} + +export function duBlockSizeEnvValue() { + return process.env.DU_BLOCK_SIZE ?? process.env.BLOCK_SIZE ?? process.env.BLOCKSIZE; +} + +const singleCall = defineCommand("du", duCmd, duMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); 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); diff --git a/src/commands/env.js b/src/commands/env.js new file mode 100644 index 0000000..2a0229a --- /dev/null +++ b/src/commands/env.js @@ -0,0 +1,385 @@ +#!/usr/bin/env bun + +import { stat } from "node:fs/promises"; +import { basename as pathBasename } from "node:path"; +import { BNU_SIGPIPE_DEFAULT_ENV, changeDirectory, invalidOptionMessage, libc, localeQuotedEscapedDiagnostic, pathDisplayName, resolveEnvCommand, shellEscapeLsName, writeEnvironment } from "../shared/common.js"; +import { InvocationError, stderr } from "../shared/diagnostics.js"; +import { SIGNAL_NAMES, SIG_IGN, execCommand, isDirectCommandInvocation, normalizeInvocationLongOptionByPrefix, signalDisplayName, signalNumberFromOperand } from "../shared/process.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const SIG_DFL = 0; + +export const ENV_LONG_OPTIONS = ["argv0", "block-signal", "chdir", "debug", "default-signal", "help", "ignore-environment", "ignore-signal", "list-signal-handling", "null", "split-string", "unset", "version"]; + +export function envMetaOption(args) { + let sawAssignment = false; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + const normalized = arg.startsWith("--") ? normalizeInvocationLongOptionByPrefix(arg, ENV_LONG_OPTIONS) : arg; + const [name, inlineValue] = normalized.startsWith("--") ? normalized.slice(2).split(/=(.*)/s, 2) : [null, undefined]; + if (sawAssignment) { + if (/^[A-Za-z_][A-Za-z0-9_]*=/.test(arg)) continue; + return null; + } + if (name === "help" || name === "version") { + if (inlineValue !== undefined) throw new InvocationError(`option '--${name}' doesn't allow an argument`); + return normalized; + } + if (arg === "-") continue; + if (/^[A-Za-z_][A-Za-z0-9_]*=/.test(arg)) { + sawAssignment = true; + continue; + } + if (normalized.startsWith("--") && ["--ignore-environment", "--null", "--debug", "--list-signal-handling"].includes(`--${name}`)) { + if (inlineValue !== undefined) throw new InvocationError(`option '--${name}' doesn't allow an argument`); + continue; + } + if (["-i", "-0", "-v"].includes(normalized)) continue; + if (["--default-signal", "--ignore-signal", "--block-signal"].includes(normalized)) continue; + if (["-a", "--argv0", "-u", "--unset", "-C", "--chdir", "-S", "--split-string"].includes(normalized)) { + i++; + continue; + } + if (/^-(a|u|C|S).+/.test(arg) || /^--(argv0|unset|chdir|split-string|default-signal|ignore-signal|block-signal)=/.test(normalized)) continue; + if (arg.startsWith("-v") && /\s/.test(arg[2] ?? "")) throw new InvocationError(`invalid option -- '${arg[2]}'\nenv: use -[v]S to pass options in shebang lines`); + if (arg.startsWith("-")) throw new InvocationError(invalidOptionMessage(arg)); + return null; + } + return null; +} + +export async function env(args) { + const originalEnv = { ...process.env }; + const childEnv = { ...process.env }; + const command = []; + let cwd = process.cwd(); + let sep = "\n"; + let argv0; + let verbose = false; + let nullOutput = false; + let sawChdir = false; + let sawAssignment = false; + let parseEnvOptions = true; + let listSignalHandling = false; + const signalActions = new Map(); + const requireValue = (index, option) => { + if (index + 1 < args.length) return args[index + 1]; + if (option.startsWith("--")) throw new InvocationError(`option '${option}' requires an argument`); + throw new InvocationError(`option requires an argument -- '${option.slice(1)}'`); + }; + for (let i = 0; i < args.length; i++) { + const rawArg = args[i]; + const arg = parseEnvOptions && rawArg !== "--" && rawArg.startsWith("--") ? normalizeInvocationLongOptionByPrefix(rawArg, ENV_LONG_OPTIONS) : rawArg; + if (parseEnvOptions && sawAssignment && !arg.includes("=")) { + command.push(...args.slice(i)); + break; + } else if (parseEnvOptions && (arg === "-" || arg === "-i" || arg === "--ignore-environment")) { + for (const key of Object.keys(childEnv)) delete childEnv[key]; + } else if (parseEnvOptions && arg === "--") { + if (sawAssignment) { + command.push(...args.slice(i)); + break; + } + parseEnvOptions = false; + } else if (parseEnvOptions && (arg === "-a" || arg === "--argv0")) { + argv0 = requireValue(i, arg); + i++; + } else if (parseEnvOptions && arg.startsWith("-a") && arg !== "-a") { + argv0 = arg.slice(2); + } else if (parseEnvOptions && arg.startsWith("--argv0=")) { + argv0 = arg.slice("--argv0=".length); + } else if (parseEnvOptions && (arg === "-0" || arg === "--null")) { + sep = "\0"; + nullOutput = true; + } else if (parseEnvOptions && (arg === "-v" || arg === "--debug")) { + verbose = true; + } else if (parseEnvOptions && (arg === "-u" || arg === "--unset")) { + const name = requireValue(i, arg); + i++; + validateEnvUnsetName(name); + delete childEnv[name]; + } else if (parseEnvOptions && arg.startsWith("-u") && arg !== "-u") { + const name = arg.slice(2); + validateEnvUnsetName(name); + delete childEnv[name]; + } else if (parseEnvOptions && arg.startsWith("--unset=")) { + const name = arg.slice("--unset=".length); + validateEnvUnsetName(name); + delete childEnv[name]; + } else if (parseEnvOptions && (arg === "-C" || arg === "--chdir")) { + cwd = requireValue(i, arg); + i++; + sawChdir = true; + } else if (parseEnvOptions && arg.startsWith("-C") && arg !== "-C") { + cwd = arg.slice(2); + sawChdir = true; + } else if (parseEnvOptions && arg.startsWith("--chdir=")) { + cwd = arg.slice("--chdir=".length); + sawChdir = true; + } else if (parseEnvOptions && (arg === "-S" || arg === "--split-string")) { + args.splice(i + 1, 1, ...splitEnvString(requireValue(i, arg), originalEnv)); + } else if (parseEnvOptions && arg.startsWith("-S") && arg !== "-S") { + args.splice(i, 1, ...splitEnvString(arg.slice(2), originalEnv)); + i--; + } else if (parseEnvOptions && arg.startsWith("-vS")) { + verbose = true; + args.splice(i, 1, ...splitEnvString(arg.slice(3), originalEnv)); + i--; + } else if (parseEnvOptions && arg.startsWith("-v") && /\s/.test(arg[2] ?? "")) { + throw new InvocationError(`invalid option -- '${arg[2]}'\nenv: use -[v]S to pass options in shebang lines`); + } else if (parseEnvOptions && arg.startsWith("--split-string=")) { + args.splice(i, 1, ...splitEnvString(arg.slice("--split-string=".length), originalEnv)); + i--; + } else if (parseEnvOptions && (arg === "--default-signal" || arg === "--ignore-signal" || arg === "--block-signal")) { + applyEnvSignalOption(signalActions, arg.slice(2), ""); + } else if (parseEnvOptions && arg.startsWith("--default-signal=")) { + applyEnvSignalOption(signalActions, "default-signal", arg.slice("--default-signal=".length)); + } else if (parseEnvOptions && arg.startsWith("--ignore-signal=")) { + applyEnvSignalOption(signalActions, "ignore-signal", arg.slice("--ignore-signal=".length)); + } else if (parseEnvOptions && arg.startsWith("--block-signal=")) { + applyEnvSignalOption(signalActions, "block-signal", arg.slice("--block-signal=".length)); + } else if (parseEnvOptions && arg === "--list-signal-handling") { + listSignalHandling = true; + } else if (parseEnvOptions && arg.startsWith("-")) { + throw new InvocationError(invalidOptionMessage(arg)); + } else if (arg.includes("=") && command.length === 0) { + const idx = arg.indexOf("="); + childEnv[arg.slice(0, idx)] = arg.slice(idx + 1); + sawAssignment = true; + } else { + command.push(...args.slice(i)); + break; + } + } + if (!command.length) { + if (sawChdir) throw new InvocationError("must specify command with --chdir (-C)"); + writeEnvironment(childEnv, sep); + return 0; + } + if (nullOutput) throw new InvocationError("cannot specify --null (-0) with command"); + if (sawChdir && !(await stat(cwd).catch(() => null))?.isDirectory()) throw new InvocationError(`cannot change directory to ${shellEscapeLsName(pathDisplayName(cwd), true)}: No such file or directory`, 125, false); + if (command.length === 1 && pathBasename(command[0]) === "env") { + writeEnvironment(childEnv, "\n"); + return 0; + } + if (verbose) { + try { + if (argv0 != null) stderr(`argv0: '${argv0}'\n`); + stderr(`executing: ${command[0]}\n`); + if (argv0 != null) stderr(` arg[0]= '${argv0}'\n`); + } catch {} + } + if (listSignalHandling) writeEnvSignalHandling(signalActions); + applyEnvSignalEnvironment(childEnv, signalActions); + const signalRestorers = applyEnvSignalActions(signalActions); + try { + const executable = await resolveEnvCommand(command[0], childEnv, cwd); + if (isDirectCommandInvocation()) { + if (sawChdir) changeDirectory(cwd); + const execError = execCommand(command, { executable, env: childEnv, argv0 }); + const error = new Error(execError.message); + error.code = execError.errno === 2 ? "ENOENT" : execError.errno === 13 ? "EACCES" : "EIO"; + throw error; + } + const proc = Bun.spawn([executable, ...command.slice(1)], { cwd, env: childEnv, stdin: "inherit", stdout: "inherit", stderr: "inherit", ...(argv0 != null ? { argv0 } : {}) }); + const signalHandlers = installChildSignalForwarders(proc, signalActions); + try { + return await proc.exited; + } finally { + removeChildSignalForwarders(signalHandlers); + restoreEnvSignalActions(signalRestorers); + } + } catch (error) { + restoreEnvSignalActions(signalRestorers); + const message = error?.code === "ENOENT" ? `No such file or directory` : error?.code === "EACCES" ? "Permission denied" : error?.message || String(error); + const needsShebangHint = /\s/.test(command[0]); + stderr(`env: '${command[0]}': ${message}\n`); + if (needsShebangHint) stderr("env: use -[v]S to pass options in shebang lines\n"); + return error?.code === "ENOENT" ? 127 : 126; + } +} + +export function applyEnvSignalOption(actions, option, value) { + const action = option === "ignore-signal" ? "ignore" : option === "default-signal" ? "default" : "block"; + for (const signum of expandEnvSignalOperand(value)) actions.set(signum, action); +} + +export function expandEnvSignalOperand(value) { + if (value == null || value === "") return envSignalNumbers(); + const signals = []; + for (const part of String(value).split(",")) { + const signum = signalNumberFromOperand(part); + if (signum <= 0) throw new InvocationError(`'${part}': invalid signal`, 125, true); + signals.push(signum); + } + return signals; +} + +export function envSignalNumbers() { + const signals = []; + for (let i = 1; i < SIGNAL_NAMES.length; i++) { + if (i !== 9 && i !== 19 && i !== 30) signals.push(i); + } + signals.push(34, 64); + return signals; +} + +export function writeEnvSignalHandling(actions) { + for (const [signum, action] of actions) { + if (action === "ignore") stderr(`${signalDisplayName(signum)}: IGNORE\n`); + } +} + +export function applyEnvSignalEnvironment(childEnv, actions) { + if (actions.get(signalNumberFromOperand("PIPE")) === "default") childEnv[BNU_SIGPIPE_DEFAULT_ENV] = "1"; +} + +export function applyEnvSignalActions(actions) { + const restorers = []; + for (const [signum, action] of actions) { + if (action === "block") continue; + const handler = action === "ignore" ? SIG_IGN : SIG_DFL; + const previous = libc.symbols.signal(signum, handler); + restorers.push([signum, previous]); + } + return restorers; +} + +export function restoreEnvSignalActions(restorers) { + for (let i = restorers.length - 1; i >= 0; i--) { + const [signum, previous] = restorers[i]; + libc.symbols.signal(signum, previous); + } +} + +export function installChildSignalForwarders(proc, signalActions = new Map()) { + const handlers = []; + const forwardSignals = [ + "SIGHUP", "SIGINT", "SIGQUIT", "SIGTERM", "SIGALRM", "SIGUSR1", "SIGUSR2", + "SIGXCPU", "SIGXFSZ", "SIGVTALRM", "SIGPROF", "SIGIO", "SIGPWR", "SIGSYS", + ]; + for (const signal of forwardSignals) { + const signum = signalNumber(signal); + if (signalActions.get(signum) === "ignore") continue; + const handler = () => { + proc.kill(signal); + }; + try { + process.on(signal, handler); + handlers.push([signal, handler]); + } catch {} + } + return handlers; +} + +export function removeChildSignalForwarders(handlers) { + for (const [signal, handler] of handlers) process.off(signal, handler); +} + +export function signalNumber(signal) { + return signalNumberFromOperand(signal); +} + +export function validateEnvUnsetName(name) { + if (name == null) throw new InvocationError("option requires an argument -- 'u'"); + if (name === "" || name.includes("=")) throw new InvocationError(`cannot unset ${localeQuotedEscapedDiagnostic(name)}: Invalid argument`, 125, false); +} + +export function splitEnvString(value, env = process.env) { + const out = []; + let current = ""; + let started = false; + let quote = null; + const text = String(value); + for (let i = 0; i < text.length; i++) { + const ch = text[i]; + if (quote === "'") { + if (ch === "'") quote = null; + else if (ch === "\\" && (text[i + 1] === "\\" || text[i + 1] === "'")) current += text[++i]; + else current += ch; + started = true; + } else if (quote === "\"") { + if (ch === "\"") { + quote = null; + started = true; + } else if (ch === "\\") { + const parsed = parseEnvEscape(text, i, true); + if (parsed.stop) throw new InvocationError("'\\c' must not appear in double-quoted -S string", 125, false); + current += parsed.value; + started = true; + i = parsed.index; + } else if (ch === "$") { + const expanded = expandEnvSplitVariable(text, i, env); + current += expanded.value; + started = true; + i = expanded.index; + } else { + current += ch; + started = true; + } + } else if (ch === "'" || ch === "\"") { + quote = ch; + started = true; + } else if (ch === "#") { + if (!started) break; + current += ch; + started = true; + } else if (/\s/.test(ch)) { + if (started) { + out.push(current); + current = ""; + started = false; + } + } else if (ch === "\\") { + const parsed = parseEnvEscape(text, i, false); + if (parsed.stop) break; + if (parsed.separator) { + if (started) { + out.push(current); + current = ""; + started = false; + } + } else { + current += parsed.value; + started = true; + } + i = parsed.index; + } else if (ch === "$") { + const expanded = expandEnvSplitVariable(text, i, env); + current += expanded.value; + started = true; + i = expanded.index; + } else { + current += ch; + started = true; + } + } + if (quote) throw new InvocationError("no terminating quote in -S string", 125, false); + if (started) out.push(current); + return out; +} + +export function parseEnvEscape(text, index, doubleQuoted) { + if (index + 1 >= text.length) throw new InvocationError("invalid backslash at end of string in -S", 125, false); + const ch = text[index + 1]; + if (ch === "c") return { stop: true, index: index + 1 }; + if (ch === "_") return doubleQuoted ? { value: " ", index: index + 1 } : { separator: true, index: index + 1 }; + const escapes = { n: "\n", r: "\r", t: "\t", f: "\f", v: "\v", "\\": "\\", "\"": "\"", "'": "'", "$": "$", "#": "#" }; + if (escapes[ch] == null) throw new InvocationError(`invalid sequence '\\${ch}' in -S`, 125, false); + return { value: escapes[ch], index: index + 1 }; +} + +export function expandEnvSplitVariable(text, index, env) { + if (text[index + 1] !== "{") throw new InvocationError(`only \${VARNAME} expansion is supported, error at: ${text.slice(index)}`, 125, false); + const end = text.indexOf("}", index + 2); + if (end === -1) throw new InvocationError(`only \${VARNAME} expansion is supported, error at: ${text.slice(index)}`, 125, false); + const name = text.slice(index + 2, end); + if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) throw new InvocationError(`only \${VARNAME} expansion is supported, error at: ${text.slice(index, end + 1)}`, 125, false); + return { value: env[name] ?? "", index: end }; +} + +const singleCall = defineCommand("env", env, envMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/expand.js b/src/commands/expand.js new file mode 100644 index 0000000..c822d10 --- /dev/null +++ b/src/commands/expand.js @@ -0,0 +1,87 @@ +#!/usr/bin/env bun + +import { closeSync, openSync } from "node:fs"; +import { isWriteError, nodeErrorMessage, parseOptions, readFdChunkViews, systemErrorMessage } from "../shared/common.js"; +import { stderr, stdout } from "../shared/diagnostics.js"; +import { EXPAND_LONG_OPTIONS, combinedTabSpec, nextTabColumn, normalizeTabArgs, normalizeTabCommandLongOptions, parseTabStops, tabCommandMetaOption } from "../shared/tabs.js"; +import { nextUtf8Token, tabFoldDiagnosticName } from "../shared/text.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export async function expand(args) { + args = normalizeTabArgs(normalizeTabCommandLongOptions(args, EXPAND_LONG_OPTIONS)); + const { opts, operands } = parseOptions(args, { short: { t: "value", i: false }, long: { tabs: "value", initial: false, help: false, version: false } }); + const stops = parseTabStops(combinedTabSpec(opts.tabs ?? opts.tab, opts.t), "expand"); + const files = operands.length ? operands : ["-"]; + let failed = false; + for (const file of files) { + let fd; + try { + fd = file === "-" ? 0 : openSync(file, "r"); + streamExpandFd(fd, stops, opts); + } catch (error) { + if (isWriteError(error)) throw error; + stderr(file === "-" ? `expand: ${nodeErrorMessage(error)}\n` : `expand: ${tabFoldDiagnosticName(file)}: ${systemErrorMessage(error)}\n`); + failed = true; + } finally { + if (fd != null && fd !== 0) closeSync(fd); + } + } + return failed ? 1 : 0; +} + +export function streamExpandFd(fd, stops, opts) { + let column = 0; + let atLineStart = true; + let carry = Buffer.alloc(0); + readFdChunkViews(fd, (chunk) => { + const bytes = carry.length ? Buffer.concat([carry, chunk]) : chunk; + carry = Buffer.alloc(0); + const output = []; + let runStart = 0; + let index = 0; + while (index < bytes.length) { + const byte = bytes[index]; + if (byte >= 0xc2 && byte <= 0xf4) { + const length = byte <= 0xdf ? 2 : byte <= 0xef ? 3 : 4; + if (index + length > bytes.length) { + if (index > runStart) output.push(bytes.subarray(runStart, index)); + carry = Buffer.from(bytes.subarray(index)); + runStart = bytes.length; + break; + } + const token = nextUtf8Token(bytes, index); + column += token.width; + atLineStart = false; + index = token.next; + continue; + } + if (byte === 0x0a) { + column = 0; + atLineStart = true; + index++; + continue; + } + if (byte === 0x09 && (!(opts.i || opts.initial) || atLineStart)) { + if (index > runStart) output.push(bytes.subarray(runStart, index)); + const next = nextTabColumn(column, stops); + output.push(Buffer.alloc(next - column, 0x20)); + column = next; + index++; + runStart = index; + continue; + } + if (byte === 0x08) column = Math.max(0, column - 1); + else column++; + if (byte !== 0x20) atLineStart = false; + index++; + } + if (runStart < bytes.length && !carry.length) output.push(bytes.subarray(runStart)); + for (const piece of output) stdout(piece); + }); + if (carry.length) stdout(carry); +} + +const singleCall = defineCommand("expand", expand, (args) => tabCommandMetaOption("expand", args)); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/expr.js b/src/commands/expr.js new file mode 100644 index 0000000..3450394 --- /dev/null +++ b/src/commands/expr.js @@ -0,0 +1,432 @@ +#!/usr/bin/env bun + +import { decodeSurrogateEscapedBytes, isUtf8Locale, localeQuotedDiagnostic, rawCommandArgs } from "../shared/common.js"; +import { InvocationError, stdout } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export async function exprCmd(args) { + const rawArgs = rawCommandArgs("expr"); + if (rawArgs?.length === args.length) args = rawArgs.map(decodeExprArgument); + if (args[0] === "--") args = args.slice(1); + if (!args.length) throw new InvocationError("missing operand", 2); + const parser = new ExprParser(args); + const result = parser.parse(); + stdout(`${formatExprValue(result)}\n`); + return exprTruthy(result) ? 0 : 1; +} + +export function decodeExprArgument(bytes) { + if (isUtf8Locale()) return decodeSurrogateEscapedBytes(bytes); + let text = ""; + for (const byte of bytes) text += byte < 0x80 ? String.fromCharCode(byte) : String.fromCharCode(0xdc00 + byte); + return text; +} + +export class ExprParser { + constructor(tokens) { + this.tokens = tokens; + this.index = 0; + } + + parse() { + const value = this.parseOr(); + if (this.peek() != null) throw new InvocationError(`syntax error: unexpected argument ${localeQuotedDiagnostic(this.peek())}`, 2, false); + return value; + } + + peek() { + return this.tokens[this.index]; + } + + take() { + return this.tokens[this.index++]; + } + + parseOr() { + let left = this.parseAnd(); + while (this.peek() === "|") { + const op = this.take(); + this.requireArgumentAfter(op); + if (exprTruthy(left)) { + this.skipExpression(1); + left = formatExprValue(left); + } else { + const right = this.parseAnd(); + left = exprTruthy(right) ? right : 0n; + } + } + return left; + } + + parseAnd() { + let left = this.parseCompare(); + while (this.peek() === "&") { + const op = this.take(); + this.requireArgumentAfter(op); + if (!exprTruthy(left)) { + this.skipExpression(2); + left = 0n; + } else { + const right = this.parseCompare(); + left = exprTruthy(right) ? left : 0n; + } + } + return left; + } + + parseCompare() { + let left = this.parseAdd(); + while (["=", "!=", "<", "<=", ">", ">="].includes(this.peek())) { + const op = this.take(); + this.requireArgumentAfter(op); + const right = this.parseAdd(); + left = exprCompare(left, right, op) ? 1n : 0n; + } + return left; + } + + parseAdd() { + let left = this.parseMul(); + while (this.peek() === "+" || this.peek() === "-") { + const op = this.take(); + this.requireArgumentAfter(op); + const right = this.parseMul(); + left = op === "+" ? exprInteger(left) + exprInteger(right) : exprInteger(left) - exprInteger(right); + } + return left; + } + + parseMul() { + let left = this.parseMatch(); + while (this.peek() === "*" || this.peek() === "/" || this.peek() === "%") { + const op = this.take(); + this.requireArgumentAfter(op); + const right = this.parseMatch(); + const a = exprInteger(left); + const b = exprInteger(right); + if ((op === "/" || op === "%") && b === 0n) throw new InvocationError("division by zero", 2, false); + left = op === "*" ? a * b : op === "/" ? a / b : a % b; + } + return left; + } + + parseMatch() { + let left = this.parsePrimary(); + while (this.peek() === ":") { + this.take(); + this.requireArgumentAfter(":"); + left = exprRegexMatch(formatExprValue(left), formatExprValue(this.parsePrimary())); + } + return left; + } + + parsePrimary() { + const token = this.take(); + if (token == null) throw new InvocationError("syntax error: missing argument", 2, false); + if (token === "+") { + this.requireArgumentAfter(token); + return this.take(); + } + if (token === "(") { + const value = this.parseOr(); + if (this.peek() !== ")") { + const previous = this.tokens[this.index - 1]; + throw new InvocationError(`syntax error: expecting ')' ${this.peek() == null ? `after ${localeQuotedDiagnostic(previous)}` : `instead of ${localeQuotedDiagnostic(this.peek())}`}`, 2, false); + } + this.take(); + return value; + } + if (token === "length") { + this.requireArgumentAfter(token); + return BigInt([...formatExprValue(this.parsePrimary())].length); + } + if (token === "index") { + this.requireArgumentAfter(token); + const text = formatExprValue(this.parsePrimary()); + this.requireArgumentAfter(text); + return BigInt(exprIndex(text, formatExprValue(this.parsePrimary()))); + } + if (token === "substr") { + this.requireArgumentAfter(token); + const text = formatExprValue(this.parsePrimary()); + this.requireArgumentAfter(text); + const startValue = this.parsePrimary(); + this.requireArgumentAfter(formatExprValue(startValue)); + const start = exprSubstrInteger(startValue); + const length = exprSubstrInteger(this.parsePrimary()); + if (start == null || length == null || start <= 0 || length <= 0) return ""; + return [...text].slice(start - 1, start - 1 + length).join(""); + } + if (token === "match") { + this.requireArgumentAfter(token); + const text = formatExprValue(this.parsePrimary()); + this.requireArgumentAfter(text); + return exprRegexMatch(text, formatExprValue(this.parsePrimary())); + } + if (token === ")") throw new InvocationError("syntax error: unexpected ')'", 2, false); + return token; + } + + requireArgumentAfter(token) { + if (this.peek() == null) throw new InvocationError(`syntax error: missing argument after ${localeQuotedDiagnostic(token)}`, 2, false); + } + + skipExpression(minPrecedence = 0) { + let depth = 0; + while (this.peek() != null) { + const token = this.peek(); + if (token === "(") depth++; + else if (token === ")") { + if (depth === 0) break; + depth--; + } else if (depth === 0 && exprOperatorPrecedence(token) < minPrecedence) { + break; + } + this.take(); + } + } +} + +export function exprOperatorPrecedence(token) { + return token === "|" ? 1 : token === "&" ? 2 : ["=", "!=", "<", "<=", ">", ">="].includes(token) ? 3 : ["+", "-"].includes(token) ? 4 : ["*", "/", "%"].includes(token) ? 5 : token === ":" ? 6 : 99; +} + +export function exprInteger(value) { + const text = formatExprValue(value); + if (!/^-?\d+$/.test(text)) throw new InvocationError("non-integer argument", 2, false); + return BigInt(text); +} + +export function exprSubstrInteger(value) { + const text = formatExprValue(value); + return /^-?\d+$/.test(text) ? Number(BigInt(text)) : null; +} + +export function exprCompare(left, right, op) { + const a = formatExprValue(left); + const b = formatExprValue(right); + const ai = /^-?\d+$/.test(a) ? BigInt(a) : null; + const bi = /^-?\d+$/.test(b) ? BigInt(b) : null; + const cmp = ai != null && bi != null ? (ai < bi ? -1 : ai > bi ? 1 : 0) : a < b ? -1 : a > b ? 1 : 0; + return op === "=" ? cmp === 0 : op === "!=" ? cmp !== 0 : op === "<" ? cmp < 0 : op === "<=" ? cmp <= 0 : op === ">" ? cmp > 0 : cmp >= 0; +} + +export function exprTruthy(value) { + const text = formatExprValue(value); + return text !== "" && !/^-?0+$/.test(text); +} + +export function formatExprValue(value) { + return typeof value === "bigint" ? String(value) : String(value); +} + +export function exprIndex(text, chars) { + const haystack = [...text]; + const needles = new Set([...chars]); + const index = haystack.findIndex((ch) => needles.has(ch)); + return index === -1 ? 0 : index + 1; +} + +export function exprRegexMatch(text, pattern) { + if (isUtf8Locale() && /[\uDC80-\uDCFF]/u.test(text)) return pattern.includes("\\(") ? "" : 0; + let regex; + try { + regex = new RegExp(`^(?:${exprBasicRegex(pattern)})`, "s"); + } catch (error) { + if (error instanceof InvocationError) throw error; + throw new InvocationError(regexErrorMessage(error), 2, false); + } + const match = String(text).match(regex); + if (!match) return pattern.includes("\\(") ? "" : 0; + return match.length > 1 ? match[1] ?? "" : match[0].length; +} + +export function exprBasicRegex(pattern) { + let out = ""; + let groups = 0; + let inBracket = false; + for (let i = 0; i < pattern.length; i++) { + const ch = pattern[i]; + if (ch !== "\\") { + if (ch === "[") { + inBracket = true; + out += ch; + continue; + } + if (ch === "]") { + inBracket = false; + out += ch; + continue; + } + if (inBracket) { + out += ch; + continue; + } + if ("()+?|{}".includes(ch)) out += `\\${ch}`; + else if (ch === "^" && i !== 0 && !out.endsWith("(")) out += "\\^"; + else if (ch === "$" && i !== pattern.length - 1 && pattern.slice(i + 1, i + 3) !== "\\)") out += "\\$"; + else if (ch === "*" && i === 0 && pattern[i + 1] === "*") { + while (pattern[i + 1] === "*") i++; + } + else if (ch === "*" && (out === "" || out.endsWith("(") || out.endsWith("|") || out.endsWith("^"))) out += "\\*"; + else if (ch === "*" && pattern[i + 1] === "*") { + out += "*"; + while (pattern[i + 1] === "*") i++; + } + else out += ch; + continue; + } + const next = pattern[++i]; + if (next == null) throw new InvocationError("Trailing backslash", 2, false); + else if (inBracket && next === "-") out += "\\\\-"; + else if (next === "(") { + groups++; + out += "("; + } else if (next === ")") { + if (groups === 0) throw new InvocationError("Unmatched ) or \\)", 2, false); + groups--; + out += ")"; + } + else if (next === "{") { + if (!exprCanApplyInterval(out)) { + out += "\\{"; + } else { + const parsed = parseExprInterval(pattern, i + 1); + out += parsed.interval; + i = parsed.end; + } + } else if (next === "}") out += "\\}"; + else if (next === "|") out += "|"; + else if ("+?".includes(next)) out += `\\${next}`; + else out += `\\${next}`; + } + if (groups > 0) throw new InvocationError("Unmatched ( or \\(", 2, false); + return normalizeExprQuantifiers(exprNormalizePosixClasses(out)); +} + +export const EXPR_POSIX_CLASSES = { + alnum: "A-Za-z0-9", + alpha: "A-Za-z", + blank: " \\t", + cntrl: "\\x00-\\x1F\\x7F", + digit: "0-9", + graph: "\\x21-\\x7E", + lower: "a-z", + print: "\\x20-\\x7E", + punct: "!\"#$%&'()*+,\\-./:;<=>?@[\\\\\\]^_`{|}~", + space: " \\t\\n\\r\\f\\v", + upper: "A-Z", + xdigit: "A-Fa-f0-9", +}; + +export function exprNormalizePosixClasses(pattern) { + let out = ""; + for (let i = 0; i < pattern.length; i++) { + if (pattern[i] !== "[") { + out += pattern[i]; + continue; + } + const end = exprBracketEnd(pattern, i); + if (end == null) { + out += pattern.slice(i); + break; + } + const body = pattern.slice(i + 1, end) + .replace(/\[([=.])(.+?)\1\]/g, (_, _kind, value) => { + if ([...value].length !== 1) throw new InvocationError("Invalid collation character", 2, false); + return value.replace(/[\\\]\^-]/g, "\\$&"); + }) + .replace(/\[:([A-Za-z]+):\]/g, (_, name) => { + const normalized = EXPR_POSIX_CLASSES[name]; + if (normalized == null) throw new InvocationError("Invalid character class name", 2, false); + return normalized; + }); + out += exprNormalizeBracketBody(body); + i = end; + } + return out; +} + +export function exprNormalizeBracketBody(body) { + const negated = body.startsWith("^"); + const prefix = negated ? "^" : ""; + const text = negated ? body.slice(1) : body; + let normalized = ""; + for (let i = 0; i < text.length; i++) { + if (i + 2 < text.length && text[i + 1] === "-" && text.charCodeAt(i) > text.charCodeAt(i + 2)) { + i += 2; + continue; + } + normalized += text[i]; + } + if (normalized.startsWith("]")) normalized = `\\${normalized}`; + if (normalized.endsWith("\\")) normalized += "\\"; + if (normalized === "") return negated ? "[\\s\\S]" : "[^\\s\\S]"; + return `[${prefix}${normalized}]`; +} + +export function exprBracketEnd(pattern, start) { + for (let i = start + 1; i < pattern.length; i++) { + if (i === start + 1 && pattern[i] === "]") continue; + if (i === start + 2 && pattern[start + 1] === "^" && pattern[i] === "]") continue; + if (pattern[i] === "[" && pattern[i + 1] === ":") { + const end = pattern.indexOf(":]", i + 2); + if (end !== -1) { + i = end + 1; + continue; + } + } + if (pattern[i] === "[" && (pattern[i + 1] === "=" || pattern[i + 1] === ".")) { + const end = pattern.indexOf(`${pattern[i + 1]}]`, i + 2); + if (end !== -1) { + i = end + 1; + continue; + } + } + if (pattern[i] === "]") return i; + } + return null; +} + +export function exprCanApplyInterval(out) { + return out !== "" && !out.endsWith("(") && !out.endsWith("|") && !out.endsWith("^"); +} + +export function parseExprInterval(pattern, start) { + const end = pattern.indexOf("\\}", start); + if (end === -1) throw new InvocationError("Unmatched \\{", 2, false); + const body = pattern.slice(start, end); + const match = body.match(/^(\d*)(?:,(\d*))?$/); + if (!match || (match[1] === "" && match[2] == null)) throw new InvocationError("Invalid content of \\{\\}", 2, false); + const min = match[1] === "" ? 0 : Number(match[1]); + const max = match[2] == null || match[2] === "" ? null : Number(match[2]); + if (min > 32767 || (max != null && max > 32767) || (max != null && min > max)) throw new InvocationError("Invalid content of \\{\\}", 2, false); + const interval = match[2] == null + ? `{${match[1]}}` + : match[1] === "" && match[2] === "" + ? "*" + : `{${match[1] === "" ? "0" : match[1]},${match[2]}}`; + return { interval, end: end + 1 }; +} + +export function normalizeExprQuantifiers(pattern) { + let out = pattern.replace(/\*\{1\}/g, "*"); + out = out.replace(/\{1\}\*/g, "*"); + out = out.replace(/\{0,1\}\*/g, "*"); + out = out.replace(/\{1\}\{1\}/g, "{1}"); + out = out.replace(/\*\*\*/g, "*").replace(/\*\*/g, "*"); + return out; +} + +export function regexErrorMessage(error) { + const message = String(error?.message ?? ""); + if (/missing terminating \] for character class/i.test(message)) return "Invalid regular expression"; + if (/unterminated group|unmatched/i.test(message)) return "Unmatched ( or \\("; + if (/lone quantifier brackets|numbers out of order|incomplete quantifier/i.test(message)) return "Invalid content of \\{\\}"; + return message.replace(/^Invalid regular expression: \/\^\(\?:.*?\)\/s: /, "Invalid regular expression: "); +} + +const singleCall = defineCommand("expr", exprCmd, (args) => args.length === 1 && (args[0] === "--help" || args[0] === "--version") ? args[0] : null); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/factor.js b/src/commands/factor.js new file mode 100644 index 0000000..db0110f --- /dev/null +++ b/src/commands/factor.js @@ -0,0 +1,660 @@ +#!/usr/bin/env bun + +import { fstatSync, readSync } from "node:fs"; +import { cpus } from "node:os"; +import { decodeSurrogateEscapedBytes, isWriteError, localeQuotedDiagnostic, lsEscapedName, parseOptions, readAll, systemErrorMessage } from "../shared/common.js"; +import { fail, stderr, stdout } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const FACTOR_LONG_OPTIONS = ["exponents", "help", "version"]; + +export function factorMetaOption(args) { + for (const arg of args) { + if (arg === "--") return null; + if (!arg.startsWith("-") || arg === "-") continue; + if (arg.startsWith("--")) { + const option = normalizeFactorLongOption(arg); + const name = option.slice(2).split("=", 1)[0]; + if (!FACTOR_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] !== "h") return null; + } + return null; +} + +export function factorize(value) { + let n = BigInt(value); + if (n < 0n) n = -n; + if (n <= 1n) return []; + const factors = []; + for (const d of [2n, 3n, 5n, 7n, 11n, 13n, 17n, 19n, 23n, 29n, 31n]) { + while (n % d === 0n) { + factors.push(d); + n /= d; + } + } + if (n > 1n) factorBigInt(n, factors); + return factors.sort((a, b) => a < b ? -1 : a > b ? 1 : 0); +} + +export function factorBigInt(n, factors) { + if (n === 1n) return; + if (isProbablePrime(n)) { + factors.push(n); + return; + } + const nearDouble = factorNearDoubleSemiprime(n); + if (nearDouble != null) { + factorBigInt(nearDouble, factors); + factorBigInt(n / nearDouble, factors); + return; + } + // A high-bound p-1 pass is useful for very large residuals with smooth + // predecessor factors, but it is disproportionately expensive across a + // contiguous range of 96-bit values. Pollard rho is the better first + // strategy below this threshold. + const pMinusOne = n >= 1_000_000_000_000_000_000_000_000_000_000n + ? pollardPMinusOne(n, 500_000) + : n >= (1n << 72n) + ? pollardPMinusOne(n, 100_000) + : null; + if (pMinusOne != null) { + factorBigInt(pMinusOne, factors); + factorBigInt(n / pMinusOne, factors); + return; + } + const divisor = pollardRho(n); + factorBigInt(divisor, factors); + factorBigInt(n / divisor, factors); +} + +export function bigintGcd(a, b) { + while (b !== 0n) { + const t = a % b; + a = b; + b = t; + } + return a < 0n ? -a : a; +} + +export function modPow(base, exponent, mod) { + let result = 1n; + base %= mod; + while (exponent > 0n) { + if (exponent & 1n) result = (result * base) % mod; + base = (base * base) % mod; + exponent >>= 1n; + } + return result; +} + +export function factorNearDoubleSemiprime(n) { + if (n < 1_000_000_000_000_000_000n) return null; + for (let delta = -4096n; delta <= 4096n; delta++) { + const discriminant = delta * delta + 8n * n; + const root = bigintSqrt(discriminant); + if (root * root !== discriminant) continue; + const numerator = root - delta; + if (numerator % 4n !== 0n) continue; + const factor = numerator / 4n; + if (factor > 1n && factor < n && n % factor === 0n) return factor; + } + return null; +} + +export function pollardPMinusOne(n, bound) { + let a = 2n; + let sinceGcd = 0; + for (const p of primesUpTo(bound)) { + let power = p; + while (power <= Math.floor(bound / p)) power *= p; + a = modPow(a, BigInt(power), n); + if (++sinceGcd < 256) continue; + sinceGcd = 0; + const d = bigintGcd(a - 1n, n); + if (d > 1n && d < n) return d; + if (d === n) return null; + } + const d = bigintGcd(a - 1n, n); + return d > 1n && d < n ? d : null; +} + +export function bigintSqrt(n) { + if (n < 2n) return n; + let x = 1n << BigInt(Math.ceil(n.toString(2).length / 2)); + for (;;) { + const y = (x + n / x) >> 1n; + if (y >= x) return x; + x = y; + } +} + +export function isProbablePrime(n) { + if (n < 2n) return false; + for (const p of [2n, 3n, 5n, 7n, 11n, 13n, 17n, 19n, 23n, 29n, 31n, 37n]) { + if (n === p) return true; + if (n % p === 0n) return false; + } + let d = n - 1n; + let s = 0n; + while ((d & 1n) === 0n) { + d >>= 1n; + s++; + } + const bases = n < (1n << 128n) ? [2n, 3n, 5n, 7n, 11n, 13n, 17n] : [2n, 3n, 5n, 7n, 11n, 13n, 17n, 19n, 23n, 29n, 31n, 37n]; + for (const a of bases) { + if (a >= n - 2n) continue; + let x = modPow(a, d, n); + if (x === 1n || x === n - 1n) continue; + let composite = true; + for (let r = 1n; r < s; r++) { + x = (x * x) % n; + if (x === n - 1n) { + composite = false; + break; + } + } + if (composite) return false; + } + return true; +} + +export function pollardRho(n) { + if (n % 2n === 0n) return 2n; + const preferredConstants = [13n, 3n, 1n, 22n, 11n, 4n, 15n, 5n, 7n, 2n, 17n, 19n, 26n]; + for (let attempt = 0; ; attempt++) { + const c = attempt < preferredConstants.length ? preferredConstants[attempt] : BigInt(attempt + 1); + let y = 2n; + let d = 1n; + let r = 1; + let q = 1n; + let x = 0n; + let ys = 0n; + const f = (v) => (v * v + c) % n; + while (d === 1n) { + x = y; + for (let i = 0; i < r; i++) y = f(y); + let k = 0; + while (k < r && d === 1n) { + ys = y; + const limit = Math.min(256, r - k); + for (let i = 0; i < limit; i++) { + y = f(y); + const diff = x > y ? x - y : y - x; + q = (q * diff) % n; + } + d = bigintGcd(q, n); + k += 256; + } + r *= 2; + if (r > 2_000_000 && d === 1n) break; + } + if (d === n) { + do { + ys = f(ys); + d = bigintGcd(x > ys ? x - ys : ys - x, n); + } while (d === 1n); + } + if (d > 1n && d < n) return d; + if (attempt === preferredConstants.length - 1 && n > 1_000_000_000_000_000_000_000_000n) { + const pPlusOne = pollardPPlusOne(n, 11_000_000, 6n); + if (pPlusOne != null) return pPlusOne; + } + } +} + +export function pollardPPlusOne(n, bound, parameter) { + let v = parameter; + for (const p of primesUpTo(bound)) { + let power = p; + while (power <= Math.floor(bound / p)) power *= p; + v = lucasV(v, BigInt(power), n); + } + const d = bigintGcd(v - 2n, n); + return d > 1n && d < n ? d : null; +} + +export function lucasV(parameter, exponent, mod) { + let vk = 2n % mod; + let vk1 = parameter % mod; + for (const bit of exponent.toString(2)) { + const v2k = positiveMod(vk * vk - 2n, mod); + const v2k1 = positiveMod(vk * vk1 - parameter, mod); + if (bit === "0") { + vk = v2k; + vk1 = v2k1; + } else { + vk = v2k1; + vk1 = positiveMod(vk1 * vk1 - 2n, mod); + } + } + return vk; +} + +export function positiveMod(value, mod) { + value %= mod; + return value < 0n ? value + mod : value; +} + +export async function factor(args) { + args = normalizeFactorLongOptions(args); + const { opts, operands } = parseOptions(args, { short: { h: false }, long: { exponents: false, help: false, version: false } }); + opts.exponents = opts.h || opts.exponents; + if (!operands.length && !fstatSync(0).isFile()) return streamFactorStdin(opts); + let values; + try { + if (operands.length) values = operands.map((operand) => String(operand).replace(/\0\S*/g, "")); + else { + const input = decodeSurrogateEscapedBytes(await readAll("-")); + const fastStatus = factorFastSmallIntegerInput(input, opts); + if (fastStatus != null) return fastStatus; + values = input.replace(/\0\S*/g, "").trim().split(/\s+/).filter(Boolean); + } + } catch (error) { + return fail("factor", `error reading input: ${systemErrorMessage(error)}`); + } + const parallel = await factorLargeContiguousRangeInParallel(values, opts); + if (parallel != null) return parallel; + const batchFactors = prepareSmallIntegerFactorizer(values) ?? prepareContiguousRangeFactorizer(values); + let status = 0; + for (const raw of values) { + status = factorOneValue(raw, opts, batchFactors) || status; + } + return status; +} + +export async function streamFactorStdin(opts) { + let pending = []; + let pendingLength = 0; + let values = []; + let status = 0; + const flushValues = async () => { + if (!values.length) return; + const batch = values; + values = []; + const parallelStatus = await factorLargeContiguousRangeInParallel(batch, opts); + if (parallelStatus != null) { + status = parallelStatus || status; + return; + } + // Dense small ranges (notably upstream factor/t00: 0..10,000,000) + // must use the compact Uint32 sieve. The generic range factorizer keeps + // one JS array per value and caused JSC heap growth into multiple GiB. + const batchFactors = prepareSegmentedSmallIntegerFactorizer(batch) + ?? prepareSmallIntegerFactorizer(batch) + ?? prepareContiguousRangeFactorizer(batch); + for (const raw of batch) status = factorOneValue(raw, opts, batchFactors) || status; + }; + const flushToken = () => { + if (!pendingLength) return; + const raw = decodeSurrogateEscapedBytes(Buffer.concat(pending, pendingLength)).replace(/\0\S*/g, ""); + pending = []; + pendingLength = 0; + if (raw) values.push(raw); + }; + try { + const inputBuffer = Buffer.allocUnsafe(64 * 1024); + while (true) { + const bytesRead = readSync(0, inputBuffer, 0, inputBuffer.length, null); + if (bytesRead === 0) break; + const chunk = inputBuffer.subarray(0, bytesRead); + let start = 0; + for (let index = 0; index < chunk.length; index++) { + if (![9, 10, 11, 12, 13, 32].includes(chunk[index])) continue; + if (index > start) { + const part = Buffer.from(chunk.subarray(start, index)); + pending.push(part); + pendingLength += part.length; + } + flushToken(); + start = index + 1; + } + if (start < chunk.length) { + const part = Buffer.from(chunk.subarray(start)); + pending.push(part); + pendingLength += part.length; + } + // Process four 100k-value child chunks concurrently while applying + // backpressure to stdin, keeping the range optimization bounded. + if (values.length >= 400_000) await flushValues(); + } + flushToken(); + await flushValues(); + return status; + } catch (error) { + if (isWriteError(error)) throw error; + return fail("factor", `error reading input: ${systemErrorMessage(error)}`); + } +} + +export function factorOneValue(raw, opts, batchFactors = null) { + const value = raw.replace(/^\+/, ""); + if (!/^\d+$/.test(value)) { + stderr(`factor: ${localeQuotedDiagnostic(factorDiagnosticValue(raw))} is not a valid positive integer\n`); + return 1; + } + const normalizedValue = normalizeFactorValue(value); + const factors = batchFactors?.get(normalizedValue) ?? factorize(normalizedValue); + stdout(`${normalizedValue}:${formatFactors(factors, opts).map((n) => ` ${n}`).join("")}\n`); + return 0; +} + +export const FACTOR_FAST_OUTPUT_CHUNK_BYTES = 4096; + +export function factorFastSmallIntegerInput(input, opts) { + if (opts.exponents || input.includes("\0")) return null; + let count = 0; + let max = 0; + for (const token of input.matchAll(/\S+/g)) { + const raw = token[0]; + const value = raw.replace(/^\+/, ""); + if (!/^\d+$/.test(value) || value.length > 8) return null; + const n = Number(value); + if (!Number.isSafeInteger(n)) return null; + if (n > max) max = n; + count++; + } + if (count < 10000 || max > 100_000_000) return null; + + const spf = smallestPrimeFactors(max); + let out = ""; + for (const token of input.matchAll(/\S+/g)) { + const n = Number(token[0].replace(/^\+/, "")); + const line = `${n}:${formatSmallIntegerFactors(n, spf)}\n`; + if (out && out.length + line.length > FACTOR_FAST_OUTPUT_CHUNK_BYTES) { + stdout(out); + out = ""; + } + out += line; + } + if (out) stdout(out); + return 0; +} + +export function formatSmallIntegerFactors(value, spf) { + if (value <= 1) return ""; + let n = value; + let out = ""; + while (n > 1) { + const p = spf[n] || n; + out += ` ${p}`; + n = Math.trunc(n / p); + } + return out; +} + +export function factorDiagnosticValue(value) { + return [...String(value)].map((ch) => { + if (/[\udc80-\udcff]/.test(ch)) return `\\${(ch.charCodeAt(0) - 0xdc00).toString(8).padStart(3, "0")}`; + return lsEscapedName(ch, { escapeDouble: false }); + }).join(""); +} + +export function normalizeFactorValue(value) { + return BigInt(value).toString(); +} + +export function normalizeFactorLongOptions(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(normalizeFactorLongOption(arg)); + } + return out; +} + +export function normalizeFactorLongOption(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 = FACTOR_LONG_OPTIONS.find((option) => option.startsWith(name)); + if (!match) return arg; + return eq === -1 ? `--${match}` : `--${match}=${body.slice(eq + 1)}`; +} + +export async function factorLargeContiguousRangeInParallel(values, opts) { + if (process.env.BNU_FACTOR_CHILD === "1" || values.length < 2000 || opts.exponents) return null; + const normalized = []; + for (const raw of values) { + const value = raw.replace(/^\+/, ""); + if (!/^\d+$/.test(value)) return null; + normalized.push(value); + } + const lo = BigInt(normalized[0]); + const hi = BigInt(normalized[normalized.length - 1]); + if (lo < 1_000_000_000_000_000_000n || hi < lo || hi - lo !== BigInt(normalized.length - 1)) return null; + for (let i = 1; i < normalized.length; i++) { + if (BigInt(normalized[i]) !== lo + BigInt(i)) return null; + } + + // Each Bun worker carries a substantial runtime and JIT footprint. Keep + // the default single-process so an ordinary invocation cannot multiply its + // memory use by the host CPU count. Controlled benchmarks and audits may + // opt in to bounded parallelism with BNU_FACTOR_WORKERS. + const configuredWorkerLimit = /^\d+$/.test(process.env.BNU_FACTOR_WORKERS ?? "") + ? Number(process.env.BNU_FACTOR_WORKERS) + : null; + const workerLimit = configuredWorkerLimit ?? 1; + const workers = Math.min(workerLimit, Math.max(1, Math.floor(values.length / 100)), cpus().length || 1); + if (workers < 2) return null; + const chunkSize = Math.ceil(values.length / workers); + const chunks = []; + for (let i = 0; i < values.length; i += chunkSize) chunks.push(values.slice(i, i + chunkSize)); + const results = await Promise.all(chunks.map(async (chunk) => { + const proc = Bun.spawn([process.execPath, import.meta.path, "factor"], { + env: { ...process.env, BNU_FACTOR_CHILD: "1" }, + stdin: new Blob([chunk.join("\n") + "\n"]), + stdout: "pipe", + stderr: "pipe", + }); + const [code, out, err] = await Promise.all([ + proc.exited, + new Response(proc.stdout).text(), + new Response(proc.stderr).text(), + ]); + return { code, out, err }; + })); + let status = 0; + for (const result of results) { + if (result.out) stdout(result.out); + if (result.err) stderr(result.err); + if (result.code !== 0) status = result.code; + } + return status; +} + +export function prepareContiguousRangeFactorizer(values) { + if (values.length < 100 || values.length > 1_000_000) return null; + const normalized = []; + for (const raw of values) { + const value = raw.replace(/^\+/, ""); + if (!/^\d+$/.test(value)) return null; + normalized.push(value); + } + const lo = BigInt(normalized[0]); + const hi = BigInt(normalized[normalized.length - 1]); + if (hi < lo || hi - lo !== BigInt(normalized.length - 1)) return null; + for (let i = 1; i < normalized.length; i++) { + if (BigInt(normalized[i]) !== lo + BigInt(i)) return null; + } + + const residues = normalized.map(BigInt); + const factors = Array.from({ length: normalized.length }, () => []); + for (const p of primesUpTo(1_000_000)) { + const bigP = BigInt(p); + const rem = lo % bigP; + let index = Number(rem === 0n ? 0n : bigP - rem); + for (; index < residues.length; index += p) { + while (residues[index] % bigP === 0n) { + factors[index].push(bigP); + residues[index] /= bigP; + } + } + } + + return { + get(value) { + const index = Number(BigInt(value) - lo); + if (index < 0 || index >= residues.length) return null; + const out = [...factors[index]]; + if (residues[index] > 1n) out.push(...factorize(residues[index])); + return out.sort((a, b) => a < b ? -1 : a > b ? 1 : 0); + }, + }; +} + +export const primeCache = new Map(); + +export function primesUpTo(max) { + const cached = primeCache.get(max); + if (cached) return cached; + const composite = new Uint8Array(max + 1); + const primes = []; + for (let i = 2; i <= max; i++) { + if (composite[i]) continue; + primes.push(i); + if (i > Math.floor(max / i)) continue; + for (let j = i * i; j <= max; j += i) composite[j] = 1; + } + primeCache.set(max, primes); + return primes; +} + +export function prepareSmallIntegerFactorizer(values) { + if (values.length < 10000) return null; + let max = 0; + for (const raw of values) { + const value = raw.replace(/^\+/, ""); + if (!/^\d+$/.test(value) || value.length > 8) return null; + const n = Number(value); + if (!Number.isSafeInteger(n)) return null; + if (n > max) max = n; + } + if (max > 100_000_000) return null; + const spf = smallestPrimeFactors(max); + return { + get(value) { + let n = Number(value); + if (n <= 1) return []; + const factors = []; + while (n > 1) { + const p = spf[n] || n; + factors.push(BigInt(p)); + n = Math.trunc(n / p); + } + return factors; + }, + }; +} + +export function prepareSegmentedSmallIntegerFactorizer(values) { + if (values.length < 10_000) return null; + const normalized = []; + for (const raw of values) { + const value = raw.replace(/^\+/, ""); + if (!/^\d+$/.test(value) || value.length > 8) return null; + const n = Number(value); + if (!Number.isSafeInteger(n) || n > 100_000_000) return null; + normalized.push(n); + } + const lo = normalized[0]; + const hi = normalized.at(-1); + if (hi - lo !== normalized.length - 1) return null; + for (let index = 1; index < normalized.length; index++) { + if (normalized[index] !== lo + index) return null; + } + + // Keep a compact CSR-style table of every factor in this segment. Storing + // only the first divisor forced each residual through the generic Pollard + // path and made the 80m..90m upstream range take more than 15 minutes. + // Two sieve passes cost little and keep a 400k-value batch to a few typed + // arrays rather than one JS array per integer. + const primes = primesUpTo(Math.floor(Math.sqrt(hi))); + const residues = new Uint32Array(normalized); + const counts = new Uint8Array(normalized.length); + for (const p of primes) { + let first = Math.ceil(lo / p) * p; + if (first < p * p) first = p * p; + for (let value = first; value <= hi; value += p) { + const index = value - lo; + while (residues[index] > 1 && residues[index] % p === 0) { + residues[index] = Math.trunc(residues[index] / p); + counts[index]++; + } + } + } + for (let index = 0; index < residues.length; index++) { + if (residues[index] > 1) counts[index]++; + } + const offsets = new Uint32Array(normalized.length + 1); + for (let index = 0; index < counts.length; index++) offsets[index + 1] = offsets[index] + counts[index]; + const factors = new Uint32Array(offsets.at(-1)); + const positions = new Uint32Array(offsets.subarray(0, normalized.length)); + residues.set(normalized); + for (const p of primes) { + let first = Math.ceil(lo / p) * p; + if (first < p * p) first = p * p; + for (let value = first; value <= hi; value += p) { + const index = value - lo; + while (residues[index] > 1 && residues[index] % p === 0) { + residues[index] = Math.trunc(residues[index] / p); + factors[positions[index]++] = p; + } + } + } + for (let index = 0; index < residues.length; index++) { + if (residues[index] > 1) factors[positions[index]++] = residues[index]; + } + return { + get(value) { + const original = Number(value); + const index = original - lo; + if (index < 0 || index >= normalized.length || original <= 1) return original <= 1 ? [] : null; + const out = []; + for (let position = offsets[index]; position < offsets[index + 1]; position++) out.push(BigInt(factors[position])); + return out; + }, + }; +} + +export let smallestPrimeFactorCache = new Uint32Array(0); + +export function smallestPrimeFactors(max) { + if (smallestPrimeFactorCache.length > max) return smallestPrimeFactorCache; + const spf = new Uint32Array(max + 1); + for (let i = 2; i <= max; i++) { + if (spf[i] !== 0) continue; + spf[i] = i; + if (i > Math.floor(max / i)) continue; + for (let j = i * i; j <= max; j += i) { + if (spf[j] === 0) spf[j] = i; + } + } + smallestPrimeFactorCache = spf; + return spf; +} + +export function formatFactors(factors, opts) { + if (!opts.exponents) return factors.map(String); + const out = []; + for (let i = 0; i < factors.length;) { + let count = 1; + while (factors[i + count] === factors[i]) count++; + out.push(count === 1 ? String(factors[i]) : `${factors[i]}^${count}`); + i += count; + } + return out; +} + +const singleCall = defineCommand("factor", factor, factorMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/false.js b/src/commands/false.js new file mode 100644 index 0000000..81997fc --- /dev/null +++ b/src/commands/false.js @@ -0,0 +1,9 @@ +#!/usr/bin/env bun + +import { booleanCommand } from "../shared/common.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +const singleCall = defineCommand("false", (args) => booleanCommand("false", args, 1), null); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/fmt.js b/src/commands/fmt.js new file mode 100644 index 0000000..a3e4722 --- /dev/null +++ b/src/commands/fmt.js @@ -0,0 +1,631 @@ +#!/usr/bin/env bun + +import { closeSync, openSync, readSync } from "node:fs"; +import { decodeSurrogateEscapedBytes, invalidOptionMessage, isWriteError, localeQuotedEscapedDiagnostic, pathDisplayName, readAll, shellEscapeLsName, systemErrorMessage } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { prInputIsNonRegular } from "../shared/text.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const FMT_LONG_OPTIONS = ["width", "goal", "prefix", "crown-margin", "split-only", "tagged-paragraph", "uniform-spacing", "help", "version"]; + +export function fmtMetaOption(args) { + const longValueOptions = new Set(["width", "goal", "prefix"]); + const shortValueOptions = new Set(["w", "g", "p"]); + const shortKnownOptions = new Set(["c", "s", "t", "u", "w", "g", "p"]); + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (arg.startsWith("--")) { + const option = normalizeFmtLongOption(arg); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (!FMT_LONG_OPTIONS.includes(name)) return null; + if ((option === "--help" || option === "--version") && inlineValue == null) return option; + if (inlineValue == null && longValueOptions.has(name)) i++; + else if (inlineValue != null && !longValueOptions.has(name)) return null; + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + if (/^-\d/.test(arg)) continue; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (!shortKnownOptions.has(ch)) return null; + if (shortValueOptions.has(ch)) { + if (arg.slice(j + 1) === "") i++; + break; + } + } + } + return null; +} + +export async function fmtCmd(args) { + const { opts, operands } = parseFmtOptions(args); + const widthOption = opts.w ?? opts.width; + const goalOption = opts.g ?? opts.goal; + const parsedGoal = goalOption == null ? null : parseFmtWidth(goalOption, "goal", { max: null }); + const width = parseFmtWidth(widthOption ?? (parsedGoal === 0 ? "10" : "75"), "width"); + if (parsedGoal != null && parsedGoal > width) throw new UsageError(`invalid width: ${localeQuotedEscapedDiagnostic(goalOption)}: Value too large for defined data type`); + const goal = parsedGoal != null ? parsedGoal : Math.max(1, Math.floor(width * 0.93)); + const files = operands.length ? operands : ["-"]; + if (files.length === 1 && fmtCanStream(opts) && prInputIsNonRegular(files[0])) { + const file = files[0]; + let fd = 0; + try { + if (file !== "-") fd = openSync(file, "r"); + streamFmtDefaultFd(fd, width, goal, opts); + return 0; + } catch (error) { + if (isWriteError(error)) throw error; + const name = shellEscapeLsName(pathDisplayName(file), true); + const message = error?.code === "EISDIR" ? `error reading ${name}: ${systemErrorMessage(error)}` : `cannot open ${name} for reading: ${systemErrorMessage(error)}`; + stderr(`fmt: ${message}\n`); + return 1; + } finally { + if (file !== "-" && fd !== 0) closeSync(fd); + } + } + let out = ""; + let failed = false; + for (const file of files) { + let text; + try { + text = decodeSurrogateEscapedBytes(await readAll(file)); + } catch (error) { + const name = shellEscapeLsName(pathDisplayName(file), true); + const message = error?.code === "EISDIR" ? `error reading ${name}: ${systemErrorMessage(error)}` : `cannot open ${name} for reading: ${systemErrorMessage(error)}`; + stderr(`fmt: ${message}\n`); + failed = true; + continue; + } + if (opts.p || opts.prefix) out += formatPrefixedText(text, opts.p ?? opts.prefix, width, goal, opts); + else { + const paragraphs = text.split(/\n[ \t]*\n/); + out += paragraphs.map((p) => formatParagraph(p, width, goal, opts)).join("\n\n"); + } + } + if (out) stdout(out + (out.endsWith("\n") ? "" : "\n")); + return failed ? 1 : 0; +} + +export function fmtCanStream(opts) { + return !(opts.p || opts.prefix || opts.c || opts.s || opts.t || opts.u); +} + +export function streamFmtDefaultFd(fd, width, goal, opts) { + const buffer = Buffer.allocUnsafe(64 * 1024); + const initial = []; + let initialLength = 0; + const threshold = 32 * 1024; + while (initialLength <= threshold) { + const n = readSync(fd, buffer, 0, buffer.length, null); + if (n === 0) { + const text = decodeSurrogateEscapedBytes(Buffer.concat(initial, initialLength)); + const paragraphs = text.split(/\n[ \t]*\n/); + const out = paragraphs.map((paragraph) => formatParagraph(paragraph, width, goal, opts)).join("\n\n"); + if (out) stdout(out + (out.endsWith("\n") ? "" : "\n")); + return; + } + initial.push(Buffer.from(buffer.subarray(0, n))); + initialLength += n; + } + const state = createFmtStreamingState(width, goal, opts); + state.accept(decodeSurrogateEscapedBytes(Buffer.concat(initial, initialLength))); + while (true) { + const n = readSync(fd, buffer, 0, buffer.length, null); + if (n === 0) break; + state.accept(decodeSurrogateEscapedBytes(buffer.subarray(0, n))); + } + state.finish(); +} + +export function createFmtStreamingState(width, goal, opts) { + let pendingLine = ""; + let words = []; + let indent = ""; + let paragraphStarted = false; + let paragraphEmitted = false; + let streamingLongWord = false; + const contentWidth = () => Math.max(1, width - indent.length); + const contentGoal = () => adjustedFmtGoal(goal, indent.length); + const writeWrappedLine = (line) => { + stdout(`${indent}${line}\n`); + paragraphEmitted = true; + }; + const consumeFirstWrappedLine = () => { + const count = takeFmtStreamingLineWords(words, contentWidth(), contentGoal(), explicitFmtGoal(opts)); + writeWrappedLine(renderFmtLine(words.splice(0, count))); + }; + const flushStableLines = () => { + while (fmtLineLength(words) > contentWidth() * 10 && words.length > 1) consumeFirstWrappedLine(); + }; + const addLineWords = (line) => { + if (!paragraphStarted) { + indent = line.match(/^[ \t]*/)?.[0] ?? ""; + paragraphStarted = true; + } + words.push(...wordsForFmt(line, opts)); + flushStableLines(); + }; + const flushParagraph = () => { + if (streamingLongWord) { + stdout("\n"); + streamingLongWord = false; + paragraphEmitted = true; + } + if (words.length) { + const lines = wrapWords(words, contentWidth(), contentGoal(), explicitFmtGoal(opts)).split("\n"); + for (const line of lines) writeWrappedLine(line); + words = []; + } + const hadContent = paragraphStarted || paragraphEmitted; + indent = ""; + paragraphStarted = false; + paragraphEmitted = false; + return hadContent; + }; + const beginLongWord = () => { + if (words.length) { + const lines = wrapWords(words, contentWidth(), contentGoal(), explicitFmtGoal(opts)).split("\n"); + for (const line of lines) writeWrappedLine(line); + words = []; + } + if (!paragraphStarted) paragraphStarted = true; + stdout(indent); + stdout(pendingLine); + pendingLine = ""; + streamingLongWord = true; + }; + const processCompleteLine = (line) => { + if (streamingLongWord) { + if (line) stdout(line); + stdout("\n"); + streamingLongWord = false; + paragraphEmitted = true; + return; + } + if (/^[ \t\r\f\v]*$/.test(line)) { + flushParagraph(); + stdout("\n"); + return; + } + addLineWords(line); + }; + return { + accept(text) { + let start = 0; + for (let index = 0; index < text.length; index++) { + if (text[index] !== "\n") continue; + pendingLine += text.slice(start, index); + processCompleteLine(pendingLine); + pendingLine = ""; + start = index + 1; + } + pendingLine += text.slice(start); + if (streamingLongWord) { + stdout(pendingLine); + pendingLine = ""; + } else if (pendingLine.length >= Math.max(64 * 1024, width * 4) && !/[ \t\r\f\v]/.test(pendingLine)) { + beginLongWord(); + } + }, + finish() { + if (pendingLine || streamingLongWord) { + if (streamingLongWord) { + if (pendingLine) stdout(pendingLine); + pendingLine = ""; + } else addLineWords(pendingLine); + } + flushParagraph(); + }, + }; +} + +export function takeFmtStreamingLineWords(words, width, goal, explicitGoal) { + if (explicitGoal) return takeFmtLineWords(words, 0, width, goal, true); + let bestCount = 1; + let bestCost = Infinity; + let length = 0; + for (let index = 0; index < words.length; index++) { + length += fmtWordLength(words[index]); + if (index) length += fmtJoinSpaces(words[index - 1]); + if (index > 0 && length > width) break; + const overflow = Math.max(0, length - width); + const under = Math.max(0, goal - length); + const overGoal = Math.max(0, length - goal); + const cost = overflow * overflow * 10000 + under * under * 1.95 + overGoal * overGoal * 2; + if (cost < bestCost || (cost === bestCost && length === width)) { + bestCost = cost; + bestCount = index + 1; + } + if (length > width) break; + } + return bestCount; +} + +export function parseFmtOptions(args) { + args = normalizeFmtLongOptions(args); + const opts = {}; + const operands = []; + let sawOption = false; + const longOptions = [ + ["width", "value", "w"], + ["goal", "value", "g"], + ["prefix", "value", "p"], + ["crown-margin", "flag", "c"], + ["split-only", "flag", "s"], + ["tagged-paragraph", "flag", "t"], + ["uniform-spacing", "flag", "u"], + ["help", "flag", "help"], + ["version", "flag", "version"], + ]; + const resolveLong = (arg) => { + if (!arg.startsWith("--") || arg === "--") return null; + const body = arg.slice(2); + const eq = body.indexOf("="); + const name = eq === -1 ? body : body.slice(0, eq); + const value = eq === -1 ? null : body.slice(eq + 1); + const matches = longOptions.filter(([option]) => option.startsWith(name)); + if (matches.length !== 1) return null; + const [option, kind, short] = matches[0]; + return { option, kind, short, value, hasInlineValue: eq !== -1 }; + }; + const requireValue = (index, option) => { + if (index + 1 < args.length) return args[index + 1]; + if (option.startsWith("--")) throw new UsageError(`option '${option}' requires an argument`, true); + throw new UsageError(`option requires an argument -- '${option.slice(1)}'`, true); + }; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") { + operands.push(...args.slice(i + 1)); + break; + } + const long = resolveLong(arg); + if (long) { + if (long.kind === "value") { + opts[long.short] = long.hasInlineValue ? long.value : requireValue(i, `--${long.option}`); + if (!long.hasInlineValue) i++; + } else { + if (long.hasInlineValue) throw new UsageError(`option '--${long.option}' doesn't allow an argument`, true); + opts[long.short] = true; + } + sawOption = true; + continue; + } + if (/^-\d/.test(arg)) { + if (sawOption) throw new UsageError(`invalid option -- ${arg[1]}; -WIDTH is recognized only when it is the first\noption; use -w N instead`, true); + opts.w = arg.slice(1); + sawOption = true; + } + else if (/^-w(?:[+-]|\d)/.test(arg)) { + opts.w = arg.slice(2); + sawOption = true; + } + else if (/^-g(?:[+-]|\d)/.test(arg)) { + opts.g = arg.slice(2); + sawOption = true; + } + else if (arg.startsWith("-p") && arg.length > 2) { + opts.p = arg.slice(2); + sawOption = true; + } + else if (arg === "-w" || arg === "--width") { + opts.w = requireValue(i, arg); + i++; + sawOption = true; + } + else if (arg.startsWith("--width=")) { + opts.width = arg.slice("--width=".length); + sawOption = true; + } + else if (arg === "-g" || arg === "--goal") { + opts.g = requireValue(i, arg); + i++; + sawOption = true; + } + else if (arg.startsWith("--goal=")) { + opts.goal = arg.slice("--goal=".length); + sawOption = true; + } + else if (arg === "-p" || arg === "--prefix") { + opts.p = requireValue(i, arg); + i++; + sawOption = true; + } + else if (arg.startsWith("--prefix=")) { + opts.prefix = arg.slice("--prefix=".length); + sawOption = true; + } + else if (arg === "-c" || arg === "--crown-margin") { + opts.c = true; + sawOption = true; + } + else if (arg === "-s" || arg === "--split-only") { + opts.s = true; + sawOption = true; + } + else if (arg === "-t" || arg === "--tagged-paragraph") { + opts.t = true; + sawOption = true; + } + else if (arg === "-u" || arg === "--uniform-spacing") { + opts.u = true; + sawOption = true; + } + else if (arg.startsWith("-")) throw new UsageError(invalidOptionMessage(arg), true); + else { + operands.push(arg); + sawOption = true; + } + } + return { opts, operands }; +} + +export function normalizeFmtLongOptions(args, reportAmbiguous = true) { + const out = []; + for (const arg of args) { + if (arg === "--") { + out.push(arg); + continue; + } + if (!arg.startsWith("--") || arg === "--") { + out.push(arg); + continue; + } + out.push(normalizeFmtLongOption(arg, reportAmbiguous)); + } + return out; +} + +export function normalizeFmtLongOption(arg, reportAmbiguous = true) { + const body = arg.slice(2); + const eq = body.indexOf("="); + const name = eq === -1 ? body : body.slice(0, eq); + const matches = FMT_LONG_OPTIONS.filter((option) => option.startsWith(name)); + if (matches.length === 1) return eq === -1 ? `--${matches[0]}` : `--${matches[0]}=${body.slice(eq + 1)}`; + if (matches.length > 1 && reportAmbiguous) { + throw new UsageError(`option '${arg}' is ambiguous; possibilities: ${matches.map((option) => `'--${option}'`).join(" ")}`, true); + } + return arg; +} + +export function parseFmtWidth(value, kind = "width", { max = 2500 } = {}) { + const raw = String(value); + if (!/^\+?\d+$/.test(raw)) throw new UsageError(`invalid width: ${localeQuotedEscapedDiagnostic(raw)}`); + const width = Number(raw); + if (!Number.isSafeInteger(width) || (max != null && width > max)) { + const reason = kind === "goal" ? "Value too large for defined data type" : "Numerical result out of range"; + throw new UsageError(`invalid width: ${localeQuotedEscapedDiagnostic(raw)}: ${reason}`); + } + return width; +} + +export function formatParagraph(paragraph, width, goal, opts) { + if (opts.s) return paragraph.split(/\n/).map((line) => formatIndentedFmtLine(line, width, goal, opts)).join("\n"); + const leadingNewlines = paragraph.match(/^\n*/)?.[0] ?? ""; + const body = paragraph.slice(leadingNewlines.length); + if (opts.c) return leadingNewlines + formatCrownMarginParagraph(body, width, goal, opts); + if (opts.t) return leadingNewlines + formatTaggedParagraph(body, width, goal, opts); + const indent = body.match(/^[ \t]*/)?.[0] ?? ""; + return leadingNewlines + indent + wrapWords(wordsForFmt(body, opts), Math.max(1, width - indent.length), adjustedFmtGoal(goal, indent.length), explicitFmtGoal(opts)).split("\n").join(`\n${indent}`); +} + +export function formatCrownMarginParagraph(body, width, goal, opts) { + const lines = body.split(/\n/); + const firstIndent = lines[0]?.match(/^[ \t]*/)?.[0] ?? ""; + const restIndent = lines.find((line, index) => index > 0 && line.trim() !== "")?.match(/^[ \t]*/)?.[0] ?? firstIndent; + const wrapped = wrapWords(wordsForFmt(body, opts), Math.max(1, width - restIndent.length), adjustedFmtGoal(goal, restIndent.length), explicitFmtGoal(opts)).split("\n"); + if (!wrapped.length) return firstIndent; + return wrapped.map((line, index) => `${index === 0 ? firstIndent : restIndent}${line}`).join("\n"); +} + +export function formatTaggedParagraph(body, width, goal, opts) { + const lines = body.split(/\n/); + const firstIndent = lines[0]?.match(/^[ \t]*/)?.[0] ?? ""; + const restIndent = lines.find((line, index) => index > 0 && line.trim() !== "")?.match(/^[ \t]*/)?.[0]; + if (restIndent != null && restIndent !== firstIndent) { + return wrapFmtTaggedWords(wordsForFmt(body, opts), { firstIndent, restIndent, width, goal, explicitGoal: explicitFmtGoal(opts) }); + } + return lines.map((line) => { + if (line.trim() === "") return line; + const indent = line.match(/^[ \t]*/)?.[0] ?? ""; + const words = wordsForFmt(line, opts); + if (words.length <= 1) return `${indent}${renderFmtLine(words)}`; + const rest = wrapWords(words.slice(1), width, goal, explicitFmtGoal(opts)); + return `${indent}${words[0].replaceAll("\0", "")}${rest ? `\n${rest}` : ""}`; + }).join("\n"); +} + +export function wrapFmtTaggedWords(words, { firstIndent, restIndent, width, goal, explicitGoal }) { + if (!words.length) return firstIndent; + const lines = []; + let index = 0; + let first = true; + while (index < words.length) { + const indent = first ? firstIndent : restIndent; + const lineWidth = Math.max(1, width - indent.length); + const lineGoal = adjustedFmtGoal(goal, indent.length); + const take = takeFmtLineWords(words, index, lineWidth, lineGoal, explicitGoal); + lines.push(`${indent}${renderFmtLine(words.slice(index, take))}`); + index = take; + first = false; + } + return lines.join("\n"); +} + +export function takeFmtLineWords(words, start, width, goal, explicitGoal) { + let take = start + 1; + let len = fmtWordLength(words[start]); + for (let i = start + 1; i < words.length; i++) { + const nextLen = len + fmtJoinSpaces(words[i - 1]) + fmtWordLength(words[i]); + if (nextLen > width) break; + if (explicitGoal && goal > 0 && len >= goal) break; + len = nextLen; + take = i + 1; + } + return take; +} + +export function formatIndentedFmtLine(line, width, goal, opts) { + const indent = line.match(/^[ \t]*/)?.[0] ?? ""; + return indent + wrapWords(wordsForFmt(line, opts), Math.max(1, width - indent.length), adjustedFmtGoal(goal, indent.length), explicitFmtGoal(opts)).split("\n").join(`\n${indent}`); +} + +export function explicitFmtGoal(opts) { + return opts.g !== undefined || opts.goal !== undefined; +} + +export function adjustedFmtGoal(goal, indentLength) { + return goal === 0 ? 0 : Math.max(1, goal - indentLength); +} + +export function wordsForFmt(text, opts) { + let normalized = String(text).replace(/^[ \t\n\r\f\v]+|[ \t\n\r\f\v]+$/g, ""); + if (opts.u) normalized = normalized.replace(/[ \t\n\r\f\v]+/g, " ").replace(/([.!?]) (?=\S)/g, "$1 "); + const words = normalized.split(/[ \t\n\r\f\v]+/).filter(Boolean); + if (opts.u) return words; + const sentenceBreaks = [...normalized.matchAll(/(\S+[.!?][)"']*) {2,}(?=\S)/g)].map((match) => match[1]); + for (let i = 0; i < words.length; i++) { + if (sentenceBreaks.includes(words[i])) words[i] += "\0"; + } + return words; +} + +export function wrapWords(words, width, goal = width, explicitGoal = false) { + if (!words.length) return ""; + if (explicitGoal && goal === 0) return wrapWordsZeroGoal(words, width); + if (explicitGoal) return wrapWordsToGoal(words, width, goal); + const n = words.length; + const dp = Array(n + 1).fill(Infinity); + const next = Array(n).fill(n); + dp[n] = 0; + for (let i = n - 1; i >= 0; i--) { + let len = 0; + for (let j = i; j < n; j++) { + len += fmtWordLength(words[j]); + if (j > i) len += fmtJoinSpaces(words[j - 1]); + if (len > width && j > i) break; + const overflow = Math.max(0, len - width); + const under = Math.max(0, goal - len); + const overGoal = Math.max(0, len - goal); + const cost = overflow * overflow * 10000 + under * under * 1.95 + overGoal * overGoal * 2 + dp[j + 1]; + const currentLen = fmtLineLength(words.slice(i, next[i])); + const prefer = cost < dp[i] || (cost === dp[i] && (len === width || (currentLen !== width && len < currentLen))); + if (prefer) { + dp[i] = cost; + next[i] = j + 1; + } + if (len > width) break; + } + } + const lines = []; + for (let i = 0; i < n; i = next[i]) lines.push(renderFmtLine(words.slice(i, next[i]))); + return lines.join("\n"); +} + +export function wrapWordsZeroGoal(words, width) { + const totalLen = fmtLineLength(words); + if (totalLen <= width) return renderFmtLine(words); + const lines = []; + let remaining = words.slice(); + while (remaining.length) { + if (fmtLineLength(remaining) <= width) { + lines.push(renderFmtLine(remaining)); + break; + } + let take = 1; + if (fmtLineLength(remaining.slice(1)) > width) { + let len = fmtWordLength(remaining[0]); + const target = Math.max(1, Math.floor(width / 2)); + while (take < remaining.length - 1 && len < target) { + len += fmtJoinSpaces(remaining[take - 1]) + fmtWordLength(remaining[take]); + take++; + } + } + lines.push(renderFmtLine(remaining.slice(0, take))); + remaining = remaining.slice(take); + } + return lines.join("\n"); +} + +export function wrapWordsToGoal(words, width, goal) { + const lines = []; + let line = []; + let len = 0; + for (const word of words) { + const nextLen = line.length ? len + fmtJoinSpaces(line.at(-1)) + fmtWordLength(word) : fmtWordLength(word); + const initial = /^[A-Z]\.\0?$/.test(word); + if (line.length && (len >= goal || nextLen > width || (nextLen > goal && len >= goal - 1) || (initial && len >= goal - 3))) { + lines.push(renderFmtLine(line)); + line = [word]; + len = fmtWordLength(word); + } else { + line.push(word); + len = nextLen; + } + } + if (line.length) lines.push(renderFmtLine(line)); + return lines.join("\n"); +} + +export function fmtWordLength(word) { + return word.replaceAll("\0", "").length; +} + +export function fmtJoinSpaces(previousWord) { + return previousWord.endsWith("\0") ? 2 : 1; +} + +export function fmtLineLength(words) { + if (!words.length) return 0; + let len = 0; + for (let i = 0; i < words.length; i++) { + len += fmtWordLength(words[i]); + if (i) len += fmtJoinSpaces(words[i - 1]); + } + return len; +} + +export function renderFmtLine(words) { + let out = ""; + for (let i = 0; i < words.length; i++) { + if (i) out += " ".repeat(fmtJoinSpaces(words[i - 1])); + out += words[i].replaceAll("\0", ""); + } + return out; +} + +export function formatPrefixedText(text, prefix, width, goal, opts) { + const lines = text.split(/(?<=\n)/); + let out = ""; + let group = []; + const flush = () => { + if (!group.length) return; + const firstBody = group[0].body; + const lead = firstBody.match(/^\s*/)?.[0] ?? ""; + const words = wordsForFmt(group.map(({ body }) => body).join(" "), opts); + out += wrapWords(words, Math.max(1, width - prefix.length - lead.length), adjustedFmtGoal(goal, prefix.length + lead.length), explicitFmtGoal(opts)) + .split("\n") + .map((part) => `${prefix}${lead}${part}`) + .join("\n"); + if (group.at(-1).hadNewline) out += "\n"; + group = []; + }; + for (const raw of lines) { + if (raw === "") continue; + const line = raw.endsWith("\n") ? raw.slice(0, -1) : raw; + if (!line.startsWith(prefix) || line === prefix) { + flush(); + out += raw; + continue; + } + group.push({ body: line.slice(prefix.length), hadNewline: raw.endsWith("\n") }); + } + flush(); + return out; +} + +const singleCall = defineCommand("fmt", fmtCmd, fmtMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/fold.js b/src/commands/fold.js new file mode 100644 index 0000000..a690226 --- /dev/null +++ b/src/commands/fold.js @@ -0,0 +1,374 @@ +#!/usr/bin/env bun + +import { closeSync, openSync } from "node:fs"; +import { concatBytes, displayWidth, enc, isWriteError, localeQuotedEscapedDiagnostic, nodeErrorMessage, parseOptions, readAll, readFdChunkViews, readStdinByteRecords, systemErrorMessage } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { concatOutputParts, decodeValidUtf8, isSingleByteLocale, nextUtf8Token, tabFoldDiagnosticName } from "../shared/text.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const FOLD_LONG_OPTIONS = ["bytes", "characters", "spaces", "width", "help", "version"]; + +export function foldMetaOption(args) { + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (arg.startsWith("--")) { + const option = normalizeFoldLongOption(arg); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (!FOLD_LONG_OPTIONS.includes(name)) return null; + if (inlineValue != null) { + if (name !== "width") return null; + parseFoldWidth(inlineValue); + } + if (option === "--help" || option === "--version") return option; + if (name === "width" && inlineValue == null) { + if (i + 1 >= args.length) throw new UsageError("option '--width' requires an argument", true); + parseFoldWidth(args[i + 1]); + i++; + } + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + const obsoleteWidth = arg.match(/^-(\d.*)$/) ?? arg.match(/^-[bcs]+(\d.*)$/); + if (obsoleteWidth) { + parseFoldWidth(obsoleteWidth[1]); + continue; + } + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (ch === "b" || ch === "c" || ch === "s") continue; + if (ch !== "w") return null; + if (!arg.slice(j + 1)) { + if (i + 1 >= args.length) throw new UsageError("option requires an argument -- 'w'", true); + parseFoldWidth(args[i + 1]); + i++; + } else { + parseFoldWidth(arg.slice(j + 1)); + } + break; + } + } + return null; +} + +export function normalizeFoldLongOptions(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(normalizeFoldLongOption(arg)); + } + return out; +} + +export function normalizeFoldLongOption(arg) { + const body = arg.slice(2); + const eq = body.indexOf("="); + const name = eq === -1 ? body : body.slice(0, eq); + if (!name) return arg; + const matches = FOLD_LONG_OPTIONS.filter((option) => option.startsWith(name)); + if (matches.length !== 1) return arg; + return eq === -1 ? `--${matches[0]}` : `--${matches[0]}=${body.slice(eq + 1)}`; +} + +export async function fold(args) { + args = normalizeFoldArgs(normalizeFoldLongOptions(args)); + const { opts, operands } = parseOptions(args, { short: { b: false, c: false, w: "value", s: false }, long: { bytes: false, characters: false, width: "value", spaces: false, help: false, version: false } }); + const widthText = opts.w ?? opts.width ?? "80"; + const width = parseFoldWidth(widthText); + const files = operands.length ? operands : ["-"]; + const mode = foldMode(args); + if (!(opts.s || opts.spaces)) { + let failed = false; + for (const file of files) { + let fd; + try { + fd = file === "-" ? 0 : openSync(file, "r"); + streamFoldFd(fd, width, mode); + } catch (error) { + if (isWriteError(error)) throw error; + stderr(file === "-" ? `fold: ${nodeErrorMessage(error)}\n` : `fold: ${tabFoldDiagnosticName(file)}: ${systemErrorMessage(error)}\n`); + failed = true; + } finally { + if (fd != null && fd !== 0) closeSync(fd); + } + } + return failed ? 1 : 0; + } + if (files.length === 1 && files[0] === "-") { + readStdinByteRecords(10, (record, hasSep) => { + const bytes = hasSep ? concatBytes([record, Uint8Array.of(10)]) : record; + stdout(foldBytes(bytes, width, mode, opts)); + }); + return 0; + } + const out = []; + let failed = false; + for (const file of files) { + let bytes; + try { + bytes = await readAll(file); + } catch (error) { + stderr(file === "-" ? `fold: ${nodeErrorMessage(error)}\n` : `fold: ${tabFoldDiagnosticName(file)}: ${systemErrorMessage(error)}\n`); + failed = true; + continue; + } + out.push(foldBytes(bytes, width, mode, opts)); + } + stdout(concatOutputParts(out)); + return failed ? 1 : 0; +} + +export function streamFoldFd(fd, width, mode) { + const newline = Uint8Array.of(0x0a); + const singleByteLocale = isSingleByteLocale(); + let measure = 0; + let lineHasToken = false; + let carry = Buffer.alloc(0); + const process = (bytes, retainIncomplete) => { + const output = []; + const emit = (piece) => { + if (piece.length) output.push(piece); + if (output.length >= 256) { + for (const part of output) stdout(part); + output.length = 0; + } + }; + let runStart = 0; + let index = 0; + while (index < bytes.length) { + const byte = bytes[index]; + if (byte === 0x0a) { + measure = 0; + lineHasToken = false; + index++; + continue; + } + let tokenLength = 1; + let tokenWidth = byte === 0 ? 0 : 1; + let tokenNext = index + 1; + if (!singleByteLocale && byte >= 0xc2 && byte <= 0xf4) { + const length = byte <= 0xdf ? 2 : byte <= 0xef ? 3 : 4; + if (retainIncomplete && index + length > bytes.length) break; + const token = nextUtf8Token(bytes, index); + tokenLength = token.bytes.length; + tokenWidth = token.width; + tokenNext = token.next; + } + let next; + if (mode === "bytes") next = measure + tokenLength; + else if (mode === "characters") next = measure + 1; + else if (tokenLength === 1 && byte === 0x08) next = Math.max(0, measure - 1); + else if (tokenLength === 1 && byte === 0x0d) next = 0; + else if (tokenLength === 1 && byte === 0x09) next = measure + 8 - (measure % 8); + else if (tokenLength === 1 && byte === 0x0c) next = measure + 1; + else next = measure + tokenWidth; + if (lineHasToken && (next > width || (measure >= width && next !== measure))) { + emit(bytes.subarray(runStart, index)); + emit(newline); + runStart = index; + measure = 0; + lineHasToken = false; + if (mode === "bytes") next = tokenLength; + else if (mode === "characters") next = 1; + else if (tokenLength === 1 && (byte === 0x08 || byte === 0x0d)) next = 0; + else if (tokenLength === 1 && byte === 0x09) next = 8; + else if (tokenLength === 1 && byte === 0x0c) next = 1; + else next = tokenWidth; + } + measure = next; + lineHasToken = true; + index = tokenNext; + } + emit(bytes.subarray(runStart, index)); + for (const part of output) stdout(part); + return index < bytes.length ? Buffer.from(bytes.subarray(index)) : Buffer.alloc(0); + }; + readFdChunkViews(fd, (chunk) => { + const bytes = carry.length ? Buffer.concat([carry, chunk]) : chunk; + carry = process(bytes, true); + }); + if (carry.length) process(carry, false); +} + +export function foldBytes(bytes, width, mode, opts) { + const rawMode = isSingleByteLocale() || decodeValidUtf8(bytes) == null; + const text = rawMode ? Buffer.from(bytes).toString("latin1") : decodeValidUtf8(bytes); + let out = ""; + for (const raw of text.split(/(?<=\n)/)) { + out += foldLine(raw.endsWith("\n") ? raw.slice(0, -1) : raw, raw.endsWith("\n"), width, mode, opts); + } + return rawMode ? Buffer.from(out, "latin1") : out; +} + +export function normalizeFoldArgs(args) { + const out = []; + let scanning = true; + let valueNext = false; + for (const arg of args) { + if (!scanning) { + out.push(arg); + } else if (valueNext) { + out.push(arg); + valueNext = false; + } else if (arg === "-w" || arg === "--width") { + out.push(arg); + valueNext = true; + } else if (/^-\d/.test(arg)) { + out.push("-w", arg.slice(1)); + } else if (/^-[bcs]+\d/.test(arg)) { + const match = arg.match(/^(-[bcs]+)(.*)$/); + out.push(match[1], "-w", match[2]); + } else { + if (arg === "--") scanning = false; + else if (!arg.startsWith("-")) scanning = false; + out.push(arg); + } + } + return out; +} + +export function foldMode(args) { + let mode = "columns"; + let scanning = true; + let valueNext = false; + for (const arg of args) { + if (!scanning) continue; + if (valueNext) { + valueNext = false; + continue; + } + if (arg === "--") { + scanning = false; + } else if (arg === "-w" || arg === "--width") { + valueNext = true; + } else if (arg === "--bytes") { + mode = "bytes"; + } else if (arg === "--characters") { + mode = "characters"; + } else if (arg.startsWith("--width=")) { + continue; + } else if (arg.startsWith("--")) { + continue; + } else if (arg.startsWith("-")) { + for (let i = 1; i < arg.length; i++) { + const ch = arg[i]; + if (ch === "b") mode = "bytes"; + else if (ch === "c") mode = "characters"; + else if (ch === "w") break; + } + } else { + scanning = false; + } + } + return mode; +} + +export function parseFoldWidth(value) { + const text = String(value); + if (!/^\+?\d+$/.test(text)) throw new UsageError(`invalid number of columns: ${localeQuotedEscapedDiagnostic(text)}`); + const width = BigInt(text.replace(/^\+/, "")); + if (width <= 0n || width >= 18446744073709551610n) throw new UsageError(`invalid number of columns: ${localeQuotedEscapedDiagnostic(text)}: Numerical result out of range`); + return width > BigInt(Number.MAX_SAFE_INTEGER) ? Number.POSITIVE_INFINITY : Number(width); +} + +export function foldLine(line, hasNewline, width, mode, opts) { + let out = ""; + while (foldMeasure(line, mode) > width) { + let cut = foldCutIndex(line, width, mode); + if (cut >= line.length) break; + if (opts.s || opts.spaces) { + const prefix = line.slice(0, cut); + const space = lastFoldBlankIndex(prefix); + if (space > 0) cut = space + 1; + } + out += line.slice(0, cut) + "\n"; + line = line.slice(cut); + } + return out + line + (hasNewline ? "\n" : ""); +} + +export function lastFoldBlankIndex(text) { + let last = -1; + for (let i = 0; i < text.length;) { + const ch = [...text.slice(i)][0]; + if (isFoldBlank(ch)) last = i; + i += ch.length; + } + return last; +} + +export function isFoldBlank(ch) { + return ch === " " || ch === "\t" || (/\p{Zs}/u.test(ch) && ch !== "\u00A0" && ch !== "\u2007"); +} + +export function foldMeasure(text, mode = "columns") { + if (mode === "bytes") return enc.encode(text).byteLength; + if (mode === "characters") return [...text].length; + let column = 0; + for (const ch of text) { + if (ch === "\b") column = Math.max(0, column - 1); + else if (ch === "\r") column = 0; + else if (ch === "\t") column += 8 - (column % 8); + else if (ch === "\f") column++; + else column += displayWidth(ch); + } + return column; +} + +export function foldCutIndex(text, width, mode = "columns") { + if (mode === "characters") return [...text].slice(0, width).join("").length; + if (mode === "columns") return foldColumnCutIndex(text, width); + let bytes = 0; + let index = 0; + for (const ch of text) { + const next = bytes + enc.encode(ch).byteLength; + if (next > width) break; + bytes = next; + index += ch.length; + } + return index || [...text][0]?.length || 0; +} + +export function foldColumnCutIndex(text, width) { + let column = 0; + let index = 0; + for (let i = 0; i < text.length;) { + const ch = [...text.slice(i)][0]; + let next; + if (ch === "\b") next = Math.max(0, column - 1); + else if (ch === "\r") next = 0; + else if (ch === "\t") next = column + 8 - (column % 8); + else if (ch === "\f") next = column + 1; + else next = column + displayWidth(ch); + if (next > width && index > 0) break; + column = next; + index += ch.length; + i += ch.length; + while (column >= width && i < text.length) { + const trailing = [...text.slice(i)][0]; + if (foldColumnWidth(trailing, column) !== column) break; + index += trailing.length; + i += trailing.length; + } + if (column >= width) break; + } + return index || [...text][0]?.length || 0; +} + +export function foldColumnWidth(ch, column) { + if (ch === "\b") return Math.max(0, column - 1); + if (ch === "\r") return 0; + if (ch === "\t") return column + 8 - (column % 8); + if (ch === "\f") return column + 1; + return column + displayWidth(ch); +} + +const singleCall = defineCommand("fold", fold, foldMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/ginstall.js b/src/commands/ginstall.js new file mode 100644 index 0000000..5872a3a --- /dev/null +++ b/src/commands/ginstall.js @@ -0,0 +1,13 @@ +#!/usr/bin/env bun + +import { installCmd, installMetaOption } from "../shared/install.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export async function ginstallCmd(args) { + return installCmd(args, "ginstall"); +} + +const singleCall = defineCommand("ginstall", ginstallCmd, installMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/groups.js b/src/commands/groups.js new file mode 100644 index 0000000..45f2a76 --- /dev/null +++ b/src/commands/groups.js @@ -0,0 +1,38 @@ +#!/usr/bin/env bun + +import { userInfo } from "node:os"; +import { groupName, helpVersionOnlyMetaOption, localeQuotedDiagnostic, normalizeHelpVersionOnlyLongOptions, orderedGroupIds, parseOptions, readPasswd, supplementaryGroupsForUser, userNameForUid } from "../shared/common.js"; +import { stderr, stdout } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export async function groupsCmd(args) { + args = normalizeHelpVersionOnlyLongOptions(args); + const { operands } = parseOptions(args, { long: { help: false, version: false } }); + const info = userInfo(); + const uid = process.getuid?.() ?? info.uid; + const gid = process.getgid?.() ?? info.gid; + const currentName = await userNameForUid(uid) ?? info.username; + if (!operands.length) { + const groups = orderedGroupIds(gid, process.getgroups?.() ?? [gid]); + stdout(`${(await Promise.all(groups.map(groupName))).join(" ")}\n`); + return 0; + } + let code = 0; + const passwd = await readPasswd(); + for (const user of operands) { + const row = passwd.get(user); + if (!row) { + stderr(`groups: ${localeQuotedDiagnostic(user)}: no such user\n`); + code = 1; + continue; + } + const groups = orderedGroupIds(row.gid, await supplementaryGroupsForUser(user)); + stdout(`${user} : ${(await Promise.all(groups.map(groupName))).join(" ")}\n`); + } + return code; +} + +const singleCall = defineCommand("groups", groupsCmd, helpVersionOnlyMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/head.js b/src/commands/head.js new file mode 100644 index 0000000..9a9bdea --- /dev/null +++ b/src/commands/head.js @@ -0,0 +1,277 @@ +#!/usr/bin/env bun + +import { closeSync, fstatSync, openSync, readSync } from "node:fs"; +import { stat } from "node:fs/promises"; +import { SEEK_CUR, headTailDiagnosticName, isWriteError, libc, localeQuotedEscapedDiagnostic, nodeErrorMessage, normalizeLongOptionByPrefix, normalizeLongOptionsByPrefix, parseOptions, readAll, systemErrorMessage } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { headTailHeaderMode, headTailShouldPrintHeader, isTailStreamingDevice, normalizeHeadTailArgs, parseByteCount, splitDelimitedByteRecords, tailFirstBytes } from "../shared/head-tail.js"; +import { outputWriteErrorMessage } from "../shared/runtime.js"; +import { fdIsSeekable } from "../shared/text.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const HEAD_LONG_OPTIONS = ["bytes", "lines", "quiet", "silent", "verbose", "zero-terminated", "help", "version"]; + +export function headMetaOption(args) { + const longValueOptions = new Set(["bytes", "lines"]); + const shortValueOptions = new Set(["c", "n"]); + const shortKnownOptions = new Set(["c", "n", "q", "v", "z"]); + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (arg.startsWith("--")) { + const option = normalizeHeadLongOption(arg); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (!HEAD_LONG_OPTIONS.includes(name)) return null; + if ((option === "--help" || option === "--version") && inlineValue == null) return option; + if (longValueOptions.has(name)) { + const value = inlineValue ?? args[i + 1]; + if (value !== undefined) { + if (name === "bytes") parseCount(value, 10, "bytes"); + if (name === "lines") parseCount(value, 10, "lines"); + } + if (inlineValue == null) i++; + } + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + if (/^-\d/.test(arg)) continue; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (!shortKnownOptions.has(ch)) return null; + if (shortValueOptions.has(ch)) { + const inlineValue = arg.slice(j + 1); + const value = inlineValue === "" ? args[i + 1] : inlineValue; + if (value !== undefined) parseCount(value, 10, ch === "c" ? "bytes" : "lines"); + if (inlineValue === "") i++; + break; + } + } + } + return null; +} + +export function parseCount(value, defaultValue, kind = "lines") { + if (value == null) return defaultValue; + const countText = String(value).replace(/^[+-]/, ""); + let n; + try { + n = parseByteCount(countText); + } catch { + throw new UsageError(`invalid number of ${kind}: ${localeQuotedEscapedDiagnostic(countText)}`); + } + if (!Number.isFinite(n) || n < 0) throw new UsageError(`invalid number of ${kind}: ${localeQuotedEscapedDiagnostic(countText)}`); + return Math.trunc(n); +} + +export async function head(args) { + args = normalizeHeadTailArgs(normalizeHeadLongOptions(args), "head"); + args = args.filter((arg) => arg !== "---presume-input-pipe"); + const headerMode = headTailHeaderMode(args); + const { opts, operands } = parseOptions(args, { short: { n: "value", c: "value", q: false, v: false, z: false }, long: { lines: "value", bytes: "value", quiet: false, silent: false, verbose: false, "zero-terminated": false, help: false, version: false } }); + const files = operands.length ? operands : ["-"]; + const bytesMode = opts.c ?? opts.bytes; + const count = parseCount(bytesMode ?? opts.n ?? opts.lines, 10, bytesMode != null ? "bytes" : "lines"); + const delimiter = opts.z || opts["zero-terminated"] ? "\0" : "\n"; + let failed = false; + let renderedAnyFile = false; + try { + for (let i = 0; i < files.length; i++) { + const file = files[i]; + const raw = String(bytesMode ?? opts.n ?? opts.lines ?? ""); + const header = headTailShouldPrintHeader(headerMode, files.length) ? `${renderedAnyFile ? "\n" : ""}==> ${file} <==\n` : ""; + if (file === "-" && files.length === 1 && headerMode !== "verbose") { + if (bytesMode != null) { + headWrite(raw.startsWith("-") ? readHeadFdExceptLastBytes(0, count) : readHeadStdinBytes(count)); + } else { + headWrite(raw.startsWith("-") ? readHeadFdExceptLastRecords(0, count, delimiter) : readHeadStdinRecords(count, delimiter)); + } + continue; + } + if (raw.startsWith("-") && file !== "-" && !(await stat(file).catch(() => null))?.isDirectory()) { + if (header) headWrite(header); + let fd; + try { + fd = openSync(file, "r"); + if (bytesMode != null) readHeadFdExceptLastBytes(fd, count); + else readHeadFdExceptLastRecords(fd, count, delimiter); + renderedAnyFile = true; + } catch (error) { + if (isWriteError(error)) throw error; + stderr(`head: cannot open ${headTailDiagnosticName(file)} for reading: ${systemErrorMessage(error)}\n`); + failed = true; + } finally { + if (fd != null) closeSync(fd); + } + continue; + } + if (bytesMode != null && !raw.startsWith("-") && file !== "-" && await isTailStreamingDevice(file)) { + if (header) headWrite(header); + try { + await tailFirstBytes(file, count); + renderedAnyFile = true; + } catch (error) { + stderr(`head: cannot open ${headTailDiagnosticName(file)} for reading: ${systemErrorMessage(error)}\n`); + failed = true; + } + continue; + } + let data; + try { + data = await readAll(file); + } catch (error) { + if (error?.code === "EISDIR") { + if (header) headWrite(header); + renderedAnyFile = true; + stderr(`head: error reading ${headTailDiagnosticName(file)}: ${systemErrorMessage(error)}\n`); + } else { + stderr(file === "-" ? `head: ${nodeErrorMessage(error)}\n` : `head: cannot open ${headTailDiagnosticName(file)} for reading: ${systemErrorMessage(error)}\n`); + } + failed = true; + continue; + } + if (header) headWrite(header); + renderedAnyFile = true; + if (bytesMode != null) { + headWrite(raw.startsWith("-") ? data.slice(0, Math.max(0, data.length - count)) : data.slice(0, count)); + } else { + const lines = splitDelimitedByteRecords(data, delimiter); + headWrite(Buffer.concat(raw.startsWith("-") ? lines.slice(0, Math.max(0, lines.length - count)) : lines.slice(0, count))); + } + } + } catch (error) { + if (!isWriteError(error)) throw error; + stderr(`head: error writing 'standard output': ${outputWriteErrorMessage(error)}\n`); + return 1; + } + return failed ? 1 : 0; +} + +export function headWrite(data) { + stdout(data); +} + +export function normalizeHeadLongOptions(args) { + return normalizeLongOptionsByPrefix(args, HEAD_LONG_OPTIONS); +} + +export function normalizeHeadLongOption(arg) { + return normalizeLongOptionByPrefix(arg, HEAD_LONG_OPTIONS); +} + +export function readHeadStdinBytes(count) { + if (count > 2 ** 32) return readRemainingStdinSync(); + const out = Buffer.alloc(count); + let offset = 0; + while (offset < count) { + const n = readSync(0, out, offset, count - offset, null); + if (n === 0) break; + offset += n; + } + return out.subarray(0, offset); +} + +export function readHeadStdinRecords(count, delimiter) { + if (count === 0) return ""; + const chunks = []; + let seen = 0; + const byte = Buffer.alloc(1); + const delim = delimiter.charCodeAt(0); + while (seen < count) { + const n = readSync(0, byte, 0, 1, null); + if (n === 0) break; + chunks.push(byte[0]); + if (byte[0] === delim) seen++; + } + return Buffer.from(chunks); +} + +export function readHeadFdExceptLastBytes(fd, count) { + if (fstatSync(fd).isFile() && fdIsSeekable(fd)) { + const data = readRemainingFdSync(fd); + const keep = Math.max(0, data.length - count); + headWrite(data.subarray(0, keep)); + rewindFd(fd, data.length - keep); + return ""; + } + const buffer = Buffer.alloc(8192); + let tail = Buffer.alloc(0); + while (true) { + const n = readSync(fd, buffer, 0, buffer.length, null); + if (n === 0) break; + const chunk = Buffer.from(buffer.subarray(0, n)); + if (count === 0) { + headWrite(chunk); + continue; + } + const combined = Buffer.concat([tail, chunk]); + const emit = Math.max(0, combined.length - count); + if (emit) headWrite(combined.subarray(0, emit)); + tail = combined.subarray(emit); + } + return ""; +} + +export function readHeadFdExceptLastRecords(fd, count, delimiter) { + if (fstatSync(fd).isFile() && fdIsSeekable(fd)) { + const data = readRemainingFdSync(fd); + const keep = headPrefixLengthExceptLastRecords(data, count, delimiter.charCodeAt(0)); + headWrite(data.subarray(0, keep)); + rewindFd(fd, data.length - keep); + return ""; + } + const delim = delimiter.charCodeAt(0); + const byte = Buffer.alloc(1); + let current = []; + const records = []; + while (true) { + const n = readSync(fd, byte, 0, 1, null); + if (n === 0) break; + current.push(byte[0]); + if (byte[0] === delim) { + records.push(Buffer.from(current)); + current = []; + while (records.length > count) headWrite(records.shift()); + } + } + if (current.length) { + records.push(Buffer.from(current)); + while (records.length > count) headWrite(records.shift()); + } + return ""; +} + +export function readRemainingStdinSync() { + return readRemainingFdSync(0); +} + +export function readRemainingFdSync(fd) { + const chunks = []; + const buffer = Buffer.alloc(8192); + while (true) { + const n = readSync(fd, buffer, 0, buffer.length, null); + if (n === 0) break; + chunks.push(Buffer.from(buffer.subarray(0, n))); + } + return Buffer.concat(chunks); +} + +export function headPrefixLengthExceptLastRecords(data, count, delimiter) { + if (count === 0) return data.length; + let seen = 0; + for (let i = data.length - 1; i >= 0; i--) { + if (data[i] === delimiter) { + seen++; + if (seen > count) return i + 1; + } + } + return 0; +} + +export function rewindFd(fd, bytes) { + if (bytes > 0) libc.symbols.lseek(fd, -BigInt(bytes), SEEK_CUR); +} + +const singleCall = defineCommand("head", head, headMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/hostid.js b/src/commands/hostid.js new file mode 100644 index 0000000..b36f70d --- /dev/null +++ b/src/commands/hostid.js @@ -0,0 +1,29 @@ +#!/usr/bin/env bun + +import { readFile } from "node:fs/promises"; +import { helpVersionOnlyMetaOption, localeQuotedEscapedDiagnostic, normalizeHelpVersionOnlyLongOptions, parseOptions } from "../shared/common.js"; +import { UsageError, stdout } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export async function hostid(args) { + args = normalizeHelpVersionOnlyLongOptions(args); + const { operands } = parseOptions(args, { long: { help: false, version: false } }); + if (operands.length) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[0])}`, true); + stdout(`${(await hostidValue()).toString(16).padStart(8, "0")}\n`); + return 0; +} + +export async function hostidValue() { + try { + const bytes = await readFile("/etc/hostid"); + if (bytes.length >= 4) return bytes.readUInt32LE(0); + } catch { + // GNU/Linux gethostid returns zero when no host id has been configured. + } + return 0; +} + +const singleCall = defineCommand("hostid", hostid, helpVersionOnlyMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/hostname.js b/src/commands/hostname.js new file mode 100644 index 0000000..0634398 --- /dev/null +++ b/src/commands/hostname.js @@ -0,0 +1,144 @@ +#!/usr/bin/env bun + +import { hostname as osHostname } from "node:os"; +import { cstr, decodeSurrogateEscapedBytes, libc, normalizeLongOptionByPrefix, parseOptions, readAll } from "../shared/common.js"; +import { UsageError, VERSION, stdout } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const HOSTNAME_LONG_OPTIONS = ["short", "alias", "domain", "fqdn", "long", "ip-address", "yp", "nis", "file", "help", "version"]; + +export async function hostnameCmd(args) { + args = normalizeHostnameLongOptions(args); + const meta = scanMetaOptionBeforeEnd(args); + if (meta === "--help") { + showHostnameHelp(); + return 0; + } + if (meta === "--version") { + stdout(`${VERSION}\n`); + return 0; + } + for (const arg of args) { + if (arg === "--") break; + if (arg.startsWith("--help=")) throw new UsageError("option '--help' doesn't allow an argument", true); + if (arg.startsWith("--version=")) throw new UsageError("option '--version' doesn't allow an argument", true); + } + const { opts, operands } = parseOptions(args, { short: { s: false, a: false, d: false, f: false, i: false, y: false, F: "value" }, long: { short: false, alias: false, domain: false, fqdn: false, long: false, "ip-address": false, yp: false, nis: false, file: "value", help: false, version: false } }); + const displayMode = hostnameDisplayMode(args); + if (displayMode === "short") return hostnamePrintShort(); + if (displayMode === "yp" && operands.length === 0) { + stdout("(none)\n"); + return 0; + } + if (displayMode === "lookup") throw new UsageError("Host name lookup failure"); + if (Object.hasOwn(opts, "F") || Object.hasOwn(opts, "file")) { + const file = Object.hasOwn(opts, "F") ? opts.F : opts.file; + let name; + try { + name = decodeSurrogateEscapedBytes(await readAll(file)).split(/\r?\n/, 1)[0]; + } catch { + throw new UsageError(`can't open \`${file}'`); + } + return hostnameSetName(name); + } + if (operands.length >= 1) { + return hostnameSetName(operands[0]); + } else { + stdout(`${osHostname()}\n`); + } + return 0; +} + +export function normalizeHostnameLongOptions(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(normalizeLongOptionByPrefix(arg, HOSTNAME_LONG_OPTIONS)); + } + return out; +} + +export function hostnamePrintShort() { + stdout(`${osHostname().split(".")[0]}\n`); + return 0; +} + +export function hostnameSetName(name) { + if (libc.symbols.sethostname(cstr(name), Buffer.byteLength(name)) !== 0) { + throw new UsageError("you don't have permission to set the host name"); + } + return 0; +} + +export function hostnameDisplayMode(args) { + let mode = null; + let end = false; + const longModes = new Map([ + ["short", "short"], + ["alias", "lookup"], + ["domain", "lookup"], + ["fqdn", "lookup"], + ["long", "lookup"], + ["ip-address", "lookup"], + ["yp", "yp"], + ["nis", "yp"], + ]); + for (const arg of args) { + if (end) continue; + if (arg === "--") { + end = true; + continue; + } + if (arg.startsWith("--")) { + const name = arg.slice(2).split("=", 1)[0]; + if (longModes.has(name)) mode = longModes.get(name); + continue; + } + if (arg.startsWith("-") && arg !== "-") { + for (const ch of arg.slice(1)) { + if (ch === "s") mode = "short"; + else if (ch === "a" || ch === "d" || ch === "f" || ch === "i") mode = "lookup"; + else if (ch === "y") mode = "yp"; + else if (ch === "F") break; + } + } + } + return mode; +} + +export function showHostnameHelp() { + stdout("Usage: hostname [NAME]\n"); + stdout(" or: hostname OPTION\n"); + stdout("Print or set the hostname of the current system.\n\n"); + stdout(" -s, --short print the short host name\n"); + stdout(" -a, --alias print alias names\n"); + stdout(" -d, --domain print DNS domain name\n"); + stdout(" -f, --fqdn, --long\n"); + stdout(" print fully qualified domain name\n"); + stdout(" -i, --ip-address\n"); + stdout(" print addresses for the host name\n"); + stdout(" -y, --yp, --nis\n"); + stdout(" print NIS/YP domain name\n"); + stdout(" -F, --file=FILE\n"); + stdout(" read host name from FILE\n"); + stdout(" --help display this help and exit\n"); + stdout(" --version output version information and exit\n"); +} + +export function scanMetaOptionBeforeEnd(args) { + for (const arg of args) { + if (arg === "--") return null; + if (arg === "--help" || arg === "--version") return arg; + } + return null; +} + +const singleCall = defineCommand("hostname", hostnameCmd, null); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/id.js b/src/commands/id.js new file mode 100644 index 0000000..0f88336 --- /dev/null +++ b/src/commands/id.js @@ -0,0 +1,150 @@ +#!/usr/bin/env bun + +import { userInfo } from "node:os"; +import { groupName, invalidOptionMessage, localeQuotedDiagnostic, normalizeLongOptionByPrefix, orderedGroupIds, parseOptions, processSecurityContext, readGroup, readPasswd, selinuxRuntimeEnabled, supplementaryGroupsForUser, userNameForUid } from "../shared/common.js"; +import { UsageError, fail, stderr, stdout } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const ID_LONG_OPTIONS = ["user", "group", "groups", "name", "real", "zero", "context", "help", "version"]; + +export function idMetaOption(args) { + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + let normalized = arg; + if (arg.startsWith("--")) normalized = normalizeIdLongOption(arg); + const [name, inlineValue] = normalized.startsWith("--") ? normalized.slice(2).split(/=(.*)/s, 2) : [null, undefined]; + if (name === "help" || name === "version") { + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + return normalized; + } + if (ID_LONG_OPTIONS.includes(name)) { + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + if (name === "context" && !selinuxRuntimeEnabled()) return null; + continue; + } + if (arg.startsWith("-") && !arg.startsWith("--")) { + if (arg.includes("Z") && !selinuxRuntimeEnabled()) return null; + i = scanIdShortMetaOption(args, i); + continue; + } + if (arg.startsWith("-")) throw new UsageError(invalidOptionMessage(arg), true); + } + return null; +} + +export function scanIdShortMetaOption(args, index) { + const arg = args[index]; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if ("ugGnrzZ".includes(ch)) continue; + throw new UsageError(`invalid option -- '${ch}'`, true); + } + return index; +} + +export async function idCmd(args) { + args = normalizeIdLongOptions(args); + const { opts, operands } = parseOptions(args, { + short: { u: false, g: false, G: false, n: false, r: false, z: false, Z: false }, + long: { user: false, group: false, groups: false, name: false, real: false, zero: false, context: false, help: false, version: false }, + }); + const justContext = Boolean(opts.Z || opts.context); + const selinuxEnabled = selinuxRuntimeEnabled(); + if (justContext && !selinuxEnabled) return fail("id", "--context (-Z) works only on an SELinux-enabled kernel", 1); + if (justContext && operands.length) return fail("id", "cannot print security context when user specified", 1); + const selected = [opts.u || opts.user, opts.g || opts.group, opts.G || opts.groups, justContext].filter(Boolean).length; + if (selected > 1) throw new UsageError('cannot print "only" of more than one choice'); + const defaultFormat = selected === 0; + if (defaultFormat && (opts.n || opts.name || opts.r || opts.real)) throw new UsageError("printing only names or real IDs requires -u, -g, or -G"); + const zero = opts.z || opts.zero; + if (zero && defaultFormat) throw new UsageError("option --zero not permitted in default format"); + const name = opts.n || opts.name; + const end = zero ? "\0" : "\n"; + const sep = zero ? "\0" : " "; + let context = null; + if (!operands.length && (justContext || (defaultFormat && !process.env.POSIXLY_CORRECT)) && selinuxEnabled) { + context = processSecurityContext(); + if (justContext && context == null) return fail("id", "can't get process context", 1); + } + if (justContext) { + stdout(`${context}${end}`); + return 0; + } + let failed = false; + for (const operand of operands.length ? operands : [undefined]) { + let identity; + try { + identity = await identityForUser(operand); + } catch (error) { + stderr(`id: ${error.message}\n`); + failed = true; + continue; + } + if (opts.u || opts.user) stdout(`${name ? identity.name : identity.uid}${end}`); + else if (opts.g || opts.group) stdout(`${name ? await groupName(identity.gid) : identity.gid}${end}`); + else if (opts.G || opts.groups) { + const groupEnd = zero && operands.length > 1 ? "\0\0" : end; + stdout(`${name ? (await Promise.all(identity.groups.map(groupName))).join(sep) : identity.groups.join(sep)}${groupEnd}`); + } + else stdout(`uid=${identity.uid}(${identity.name}) gid=${identity.gid}(${await groupName(identity.gid)}) groups=${identity.groups.map((gid) => `${gid}(${identity.groupNames.get(gid) ?? gid})`).join(",")}${context != null ? ` context=${context}` : ""}\n`); + } + return failed ? 1 : 0; +} + +export function normalizeIdLongOptions(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(normalizeIdLongOption(arg)); + } + return out; +} + +export function normalizeIdLongOption(arg) { + return normalizeLongOptionByPrefix(arg, ID_LONG_OPTIONS); +} + +export async function identityForUser(user) { + const info = userInfo(); + const uid = process.getuid?.() ?? info.uid; + const gid = process.getgid?.() ?? info.gid; + const currentName = await userNameForUid(uid) ?? info.username; + const originalUser = user; + const numericOnly = typeof user === "string" && /^\+\d+$/.test(user); + if (numericOnly) user = user.slice(1); + if (user == null) { + const groups = orderedGroupIds(gid, process.getgroups?.() ?? [gid]); + return { name: currentName, uid, gid, groups, groupNames: await groupNamesMap() }; + } + const passwd = await readPasswd(); + let entry = numericOnly ? null : passwd.get(user); + if (!entry && /^\d+$/.test(user)) { + for (const [name, row] of passwd) { + if (row.uid === Number(user)) { + entry = { ...row, name }; + break; + } + } + } else if (entry) { + entry = { ...entry, name: user }; + } + if (!entry) throw new UsageError(`${localeQuotedDiagnostic(originalUser)}: no such user`); + const memberGroups = await supplementaryGroupsForUser(entry.name); + return { name: entry.name, uid: entry.uid, gid: entry.gid, groups: orderedGroupIds(entry.gid, memberGroups), groupNames: await groupNamesMap() }; +} + +export async function groupNamesMap() { + const groups = await readGroup(); + return new Map([...groups].map(([name, gid]) => [gid, name])); +} + +const singleCall = defineCommand("id", idCmd, idMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/install.js b/src/commands/install.js new file mode 100644 index 0000000..6e9e4b8 --- /dev/null +++ b/src/commands/install.js @@ -0,0 +1,9 @@ +#!/usr/bin/env bun + +import { installCmd, installMetaOption } from "../shared/install.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +const singleCall = defineCommand("install", installCmd, installMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/join.js b/src/commands/join.js new file mode 100644 index 0000000..aef500a --- /dev/null +++ b/src/commands/join.js @@ -0,0 +1,429 @@ +#!/usr/bin/env bun + +import { closeSync, openSync } from "node:fs"; +import { createFdRecordReader, invalidOptionMessage, isWriteError, localeQuotedEscapedDiagnostic, readAll, shellEscapeLsName, statSyncNoThrow, systemErrorMessage, textInputDiagnosticName } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { splitSeparator } from "../shared/text.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const JOIN_LONG_OPTIONS = ["check-order", "header", "ignore-case", "nocheck-order", "zero-terminated", "help", "version"]; + +export function joinMetaOption(args) { + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (arg.startsWith("--")) { + const option = normalizeJoinLongOption(arg, false); + const name = option.slice(2).split("=", 1)[0]; + if (!JOIN_LONG_OPTIONS.includes(name) || option.includes("=")) return null; + if (option === "--help" || option === "--version") return option; + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if ("iz".includes(ch)) continue; + if (!"12aejotv".includes(ch)) return null; + const inlineValue = arg.slice(j + 1); + const value = inlineValue === "" ? args[i + 1] : inlineValue; + validateJoinMetaOptionValue(ch, value); + if (inlineValue === "" && i + 1 < args.length) i++; + break; + } + } + return null; +} + +export function validateJoinMetaOptionValue(option, value) { + if (value === undefined) return; + if ("12j".includes(option)) parseJoinFieldNumber(value); + else if ("av".includes(option)) parseJoinFileNumber(value); + else if (option === "o") parseJoinOutputList(value); + else if (option === "t") parseJoinSeparator(value); +} + +export function normalizeJoinLongOptions(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(normalizeJoinLongOption(arg)); + } + return out; +} + +export function normalizeJoinLongOption(arg, reportAmbiguous = true) { + const body = arg.slice(2); + const eq = body.indexOf("="); + const name = eq === -1 ? body : body.slice(0, eq); + if (!name) return arg; + const matches = JOIN_LONG_OPTIONS.filter((option) => option.startsWith(name)); + if (matches.length !== 1) { + if (matches.length > 1 && reportAmbiguous) throw new UsageError(`option '${arg}' is ambiguous; possibilities: ${matches.map((option) => `'--${option}'`).join(" ")}`, true); + return arg; + } + return eq === -1 ? `--${matches[0]}` : `--${matches[0]}=${body.slice(eq + 1)}`; +} + +export function escapeRegExp(value) { + return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); +} + +export async function joinCmd(args) { + const { opts, operands } = parseJoinOptions(args); + if (!operands.length) throw new UsageError("missing operand", true); + if (operands.length === 1) throw new UsageError(`missing operand after '${operands[0]}'`, true); + if (operands.length > 2) throw new UsageError(`extra operand ${shellEscapeLsName(operands[2], true)}`, true); + const delim = normalizeJoinDelimiter(opts.t ?? " "); + const outDelim = delim === " " ? " " : delim; + const recordSep = opts.z ? "\0" : "\n"; + const f1 = opts.f1 - 1; + const f2 = opts.f2 - 1; + const streamed = streamJoinAgainstEmptyInput(operands, opts, delim, outDelim, recordSep, f1, f2); + if (streamed != null) return streamed; + let leftBytes; + let rightBytes; + try { + leftBytes = await readAll(operands[0]); + rightBytes = await readAll(operands[1]); + } catch (error) { + const file = leftBytes == null ? operands[0] : operands[1]; + const message = error?.code === "EISDIR" ? `read error: ${systemErrorMessage(error)}` : `${textInputDiagnosticName(file)}: ${systemErrorMessage(error)}`; + stderr(`join: ${message}\n`); + return 1; + } + let left = parseJoinFile(leftBytes, delim, opts.z); + let right = parseJoinFile(rightBytes, delim, opts.z); + let out = ""; + let leftHeader = null; + let rightHeader = null; + if (opts.header) { + leftHeader = left.shift() ?? null; + rightHeader = right.shift() ?? null; + } + opts.autoLeftWidth = Math.max(leftHeader?.length ?? 0, left[0]?.length ?? 0, f1 + 1); + opts.autoRightWidth = Math.max(rightHeader?.length ?? 0, right[0]?.length ?? 0, f2 + 1); + if (leftHeader || rightHeader) out += formatJoinOutput(leftHeader, rightHeader, leftHeader?.[f1] ?? rightHeader?.[f2] ?? "", opts, outDelim, recordSep, f1, f2); + if (opts.checkOrder) { + const disorder = checkJoinOrder(left, f1, operands[0], opts) ?? checkJoinOrder(right, f2, operands[1], opts); + if (disorder) { + if (out) stdout(out); + stderr(`join: ${disorder.file}:${disorder.lineNo}: is not sorted: ${disorder.raw}\n`); + return 1; + } + } + const leftDisorder = checkJoinOrder(left, f1, operands[0], opts); + const rightDisorder = checkJoinOrder(right, f2, operands[1], opts); + const rightEntries = right.map((row, index) => ({ row, index, key: joinComparisonKey(row, f2, opts), rawKey: row[f2] ?? "" })); + const rightMap = new Map(); + for (const entry of rightEntries) { + if (!rightMap.has(entry.key)) rightMap.set(entry.key, []); + rightMap.get(entry.key).push(entry); + } + const leftKeys = new Set(left.map((row) => joinComparisonKey(row, f1, opts))); + const matchedRightKeys = new Set(); + const emittedUnmatchedRight = new Set(); + const emitRightBefore = (key) => { + if (!(opts.all.has(2) || opts.v.has(2))) return; + for (const entry of rightEntries) { + if (emittedUnmatchedRight.has(entry.index) || leftKeys.has(entry.key) || entry.key >= key) continue; + out += formatJoinOutput(null, entry.row, entry.rawKey, opts, outDelim, recordSep, f1, f2); + emittedUnmatchedRight.add(entry.index); + } + }; + for (const leftRow of left) { + const key = joinComparisonKey(leftRow, f1, opts); + const rawKey = leftRow[f1] ?? ""; + emitRightBefore(key); + const matches = rightMap.get(key) ?? []; + if (!matches.length) { + if (opts.all.has(1) || opts.v.has(1)) out += formatJoinOutput(leftRow, null, rawKey, opts, outDelim, recordSep, f1, f2); + } else { + matchedRightKeys.add(key); + for (const entry of matches) { + if (!opts.v.size) out += formatJoinOutput(leftRow, entry.row, rawKey, opts, outDelim, recordSep, f1, f2); + } + } + } + for (const entry of rightEntries) { + if (matchedRightKeys.has(entry.key) || emittedUnmatchedRight.has(entry.index)) continue; + if (opts.all.has(2) || opts.v.has(2)) out += formatJoinOutput(null, entry.row, entry.rawKey, opts, outDelim, recordSep, f1, f2); + } + if (!opts.checkOrder && !opts.nocheckOrder && out.trim() === "" && leftDisorder && rightDisorder) { + if (leftDisorder) stderr(`join: ${leftDisorder.file}:${leftDisorder.lineNo}: is not sorted: ${leftDisorder.raw}\n`); + if (rightDisorder) stderr(`join: ${rightDisorder.file}:${rightDisorder.lineNo}: is not sorted: ${rightDisorder.raw}\n`); + stderr("join: input is not in sorted order\n"); + return 1; + } + stdout(Buffer.from(out, "latin1")); + return 0; +} + +export function streamJoinAgainstEmptyInput(files, opts, delim, outDelim, recordSep, f1, f2) { + if (opts.header) return null; + let emptySide = -1; + for (let side = 0; side < 2; side++) { + if (files[side] === "/dev/null") { + emptySide = side; + break; + } + const info = statSyncNoThrow(files[side]); + if (info?.isFile() && info.size === 0) { + emptySide = side; + break; + } + } + if (emptySide === -1) return null; + const sourceSide = 1 - emptySide; + const sourceNumber = sourceSide + 1; + if (!(opts.all.has(sourceNumber) || opts.v.has(sourceNumber))) return null; + let fd; + try { + fd = files[sourceSide] === "-" ? 0 : openSync(files[sourceSide], "r"); + const reader = createFdRecordReader(fd, recordSep.charCodeAt(0)); + const field = sourceSide === 0 ? f1 : f2; + let previous = null; + let lineNo = 0; + while (true) { + const bytes = reader.next(); + if (bytes == null) break; + lineNo++; + const row = parseJoinRecord(bytes, delim, opts.z, lineNo); + const key = joinComparisonKey(row, field, opts); + if (!opts.nocheckOrder && previous != null && key < previous) { + stderr(`join: ${files[sourceSide]}:${lineNo}: is not sorted: ${row.raw}\n`); + if (opts.checkOrder) return 1; + stderr("join: input is not in sorted order\n"); + return 1; + } + previous = key; + const left = sourceSide === 0 ? row : null; + const right = sourceSide === 1 ? row : null; + stdout(Buffer.from(formatJoinOutput(left, right, row[field] ?? "", opts, outDelim, recordSep, f1, f2), "latin1")); + } + return 0; + } catch (error) { + if (isWriteError(error)) throw error; + const message = error?.code === "EISDIR" ? `read error: ${systemErrorMessage(error)}` : `${textInputDiagnosticName(files[sourceSide])}: ${systemErrorMessage(error)}`; + stderr(`join: ${message}\n`); + return 1; + } finally { + if (fd != null && fd !== 0) closeSync(fd); + } +} + +export function parseJoinRecord(bytes, delim, zero, lineNo) { + const line = Buffer.from(bytes).toString("latin1"); + const fields = delim === "" + ? [line] + : delim === " " + ? line.replace(/^[ \t\n]+/, "").split(zero ? /[ \t\n]+/ : /[ \t]+/).filter((field) => field !== "") + : line.split(delim); + fields.raw = line; + fields.lineNo = lineNo; + return fields; +} + +export function parseJoinFile(data, delim, zero = false) { + const recordSep = zero ? "\0" : "\n"; + const text = Buffer.from(data).toString("latin1").replace(new RegExp(`${escapeRegExp(recordSep)}$`), ""); + if (text === "") return []; + return text.split(recordSep).map((line) => { + const fields = delim === "" + ? [line] + : delim === " " + ? line.replace(/^[ \t\n]+/, "").split(zero ? /[ \t\n]+/ : /[ \t]+/).filter((field) => field !== "") + : line.split(delim); + fields.raw = line; + return fields; + }).map((fields, index) => { + fields.lineNo = index + 1; + return fields; + }); +} + +export function normalizeJoinDelimiter(value) { + const text = String(value); + if (text === " ") return " "; + if (text.includes("\uFFFD")) return text.replaceAll("\uFFFD", "\xA7"); + return Buffer.from(text).toString("latin1"); +} + +export function checkJoinOrder(rows, field, file, opts = {}) { + let previous = null; + for (const row of rows) { + const key = joinComparisonKey(row, field, opts); + if (previous != null && key < previous) return { file, lineNo: row.lineNo, raw: row.raw }; + previous = key; + } + return null; +} + +export function joinComparisonKey(row, field, opts = {}) { + const key = row?.[field] ?? ""; + return opts.ignoreCase ? key.toLocaleLowerCase() : key; +} + +export function parseJoinOptions(args) { + args = normalizeJoinLongOptions(args); + const opts = { f1: 1, f2: 1, all: new Set(), v: new Set(), empty: "", output: null, checkOrder: false, header: false, ignoreCase: false }; + const operands = []; + const requireValue = (index, option) => { + if (index + 1 < args.length) return args[index + 1]; + throw new UsageError(`option requires an argument -- '${option.slice(1)}'`, true); + }; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") { + operands.push(...args.slice(i + 1)); + break; + } else if (arg.startsWith("--") && arg.includes("=")) { + const name = arg.slice(2).split("=", 1)[0]; + if (JOIN_LONG_OPTIONS.includes(name)) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + throw new UsageError(`unrecognized option '${arg}'`, true); + } else if (arg === "--check-order") { + opts.checkOrder = true; + } else if (arg === "--nocheck-order") { + opts.checkOrder = false; + opts.nocheckOrder = true; + } else if (arg === "--header") { + opts.header = true; + } else if (arg === "-i" || arg === "--ignore-case") { + opts.ignoreCase = true; + } else if (arg === "-z" || arg === "--zero-terminated") { + opts.z = true; + } else if (arg === "-t") { + opts.t = parseJoinSeparator(requireValue(i, arg)); + i++; + } else if (arg.startsWith("-t") && arg.length > 2) { + opts.t = parseJoinSeparator(arg.slice(2)); + } else if (arg === "-1") { + opts.f1 = parseJoinFieldNumber(requireValue(i, arg)); + i++; + } else if (arg.startsWith("-1") && arg.length > 2) { + opts.f1 = parseJoinFieldNumber(arg.slice(2)); + } else if (arg === "-2") { + opts.f2 = parseJoinFieldNumber(requireValue(i, arg)); + i++; + } else if (arg.startsWith("-2") && arg.length > 2) { + opts.f2 = parseJoinFieldNumber(arg.slice(2)); + } else if (arg === "-j") { + const field = parseJoinFieldNumber(requireValue(i, arg)); + i++; + opts.f1 = field; + opts.f2 = field; + } else if (arg.startsWith("-j") && arg.length > 2) { + const field = parseJoinFieldNumber(arg.slice(2)); + opts.f1 = field; + opts.f2 = field; + } else if (arg === "-a") { + opts.all.add(parseJoinFileNumber(requireValue(i, arg))); + i++; + } else if (/^-a[12]$/.test(arg)) { + opts.all.add(Number(arg[2])); + } else if (arg === "-v") { + opts.v.add(parseJoinFileNumber(requireValue(i, arg))); + i++; + } else if (/^-v[12]$/.test(arg)) { + opts.v.add(Number(arg[2])); + } else if (arg === "-e") { + opts.empty = requireValue(i, arg); + i++; + } else if (arg.startsWith("-e") && arg.length > 2) { + opts.empty = arg.slice(2); + } else if (arg === "-o") { + const collected = collectJoinOutputList(args, i, requireValue(i, arg)); + opts.output = parseJoinOutputList(collected.value); + i = collected.index; + } else if (arg.startsWith("-o") && arg.length > 2) { + const collected = collectJoinOutputList(args, i, arg.slice(2)); + opts.output = parseJoinOutputList(collected.value); + i = collected.index; + } else if (arg.startsWith("-") && arg !== "-") { + throw new UsageError(invalidOptionMessage(arg), true); + } else { + operands.push(arg); + } + } + return { opts, operands }; +} + +export function parseJoinFieldNumber(value) { + if (!/^\+?\d+$/.test(String(value))) throw new UsageError(`invalid field number: ${localeQuotedEscapedDiagnostic(value)}`); + const n = BigInt(String(value)); + if (n === 0n) throw new UsageError(`invalid field number: ${localeQuotedEscapedDiagnostic(value)}`); + return n > BigInt(Number.MAX_SAFE_INTEGER) ? Number.MAX_SAFE_INTEGER : Number(n); +} + +export function parseJoinFileNumber(value) { + if (value !== "1" && value !== "2") throw new UsageError(`invalid file number: ${localeQuotedEscapedDiagnostic(value)}`); + return Number(value); +} + +export function collectJoinOutputList(args, index, initial) { + const parts = [initial]; + if (initial === "auto") return { value: initial, index: index + (args[index] === "-o" ? 1 : 0) }; + let next = index + (args[index] === "-o" ? 2 : 1); + while (next < args.length && args[next] !== "--" && !args[next].startsWith("-") && args.length - (next + 1) >= 2) { + parts.push(args[next]); + next++; + } + return { value: parts.join(" "), index: next - 1 }; +} + +export function parseJoinSeparator(value) { + const sep = splitSeparator(value); + if ([...sep].length > 1) throw new UsageError(`multi-character tab ${localeQuotedEscapedDiagnostic(value)}`); + return sep; +} + +export function parseJoinOutputList(value) { + if (value === "auto") return "auto"; + return String(value).split(/[,\t ]+/).filter(Boolean).map((part) => { + if (part === "0") return { join: true }; + const fieldMatch = part.match(/^([12])\.(.*)$/s); + if (fieldMatch) { + const fieldText = fieldMatch[2]; + if (!/^\d+$/.test(fieldText) || BigInt(fieldText) === 0n) throw new UsageError(`invalid field number: ${localeQuotedEscapedDiagnostic(fieldText)}`); + return { file: Number(fieldMatch[1]), field: Number(fieldText) - 1 }; + } + if (/^[12]\./.test(part)) throw new UsageError(`invalid field number: ${localeQuotedEscapedDiagnostic(part.slice(2))}`); + throw new UsageError(`invalid file number in field spec: ${localeQuotedEscapedDiagnostic(part)}`); + }); +} + +export function formatJoinOutput(left, right, key, opts, delim, recordSep, f1, f2) { + let fields; + if (opts.output === "auto") { + const leftWidth = Math.max(opts.autoLeftWidth ?? 0, f1 + 1); + const rightWidth = Math.max(opts.autoRightWidth ?? 0, f2 + 1); + fields = [key]; + for (let i = 0; i < leftWidth; i++) if (i !== f1) fields.push(joinField(left, i, opts.empty)); + for (let i = 0; i < rightWidth; i++) if (i !== f2) fields.push(joinField(right, i, opts.empty)); + } else if (opts.output) { + fields = opts.output.map((spec) => { + if (spec.join) return key || opts.empty; + return joinField(spec.file === 1 ? left : right, spec.field, opts.empty); + }); + } else if (!right) { + fields = [key, ...(left ?? []).filter((_, i) => i !== f1)]; + } else if (!left) { + fields = [key, ...(right ?? []).filter((_, i) => i !== f2)]; + } else { + fields = [key, ...left.filter((_, i) => i !== f1), ...right.filter((_, i) => i !== f2)]; + } + return `${fields.map((field) => field === "" ? opts.empty : field).join(delim)}${recordSep}`; +} + +export function joinField(row, index, empty) { + return row?.[index] ?? empty; +} + +const singleCall = defineCommand("join", joinCmd, joinMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/kill.js b/src/commands/kill.js new file mode 100644 index 0000000..a58730f --- /dev/null +++ b/src/commands/kill.js @@ -0,0 +1,133 @@ +#!/usr/bin/env bun + +import { access } from "node:fs/promises"; +import { libc, parseOptions } from "../shared/common.js"; +import { UsageError, stdout } from "../shared/diagnostics.js"; +import { SIGNAL_NAMES, signalDisplayName, signalNumberFromOperand } from "../shared/process.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export async function killCmd(args) { + if (countKillListOptions(args) > 1) throw new UsageError("multiple -l or -t options specified", true); + args = normalizeKillArgs(args); + const { opts, operands } = parseOptions(args, { short: { s: "value", n: "value", l: false, L: false, t: false }, long: { signal: "value", list: "optional-value", table: false } }); + const listCount = Number(Boolean(opts.l || opts.list)) + Number(Boolean(opts.L || opts.t || opts.table)); + if (listCount > 1) throw new UsageError("multiple -l or -t options specified", true); + if (listCount && (opts.s || opts.n || opts.signal)) throw new UsageError("cannot combine signal with -l or -t", true); + if (opts.L || opts.t || opts.table) return listSignals(true, operands); + const listSignal = opts.list === true ? operands[0] : opts.list || (opts.l ? operands[0] : null); + if (opts.l || opts.list) { + return listSignals(false, listSignal ? [listSignal, ...operands.slice(1)] : operands); + } + const explicitSignal = opts.s != null || opts.n != null || opts.signal != null; + let signal = opts.s ?? opts.n ?? opts.signal ?? "TERM"; + const pids = []; + for (const arg of operands) { + if (/^-[A-Za-z]+$/.test(arg) || (!explicitSignal && /^-\d+$/.test(arg))) signal = arg.slice(1); + else pids.push(Number(arg)); + } + if (!pids.length || pids.some((pid) => !Number.isInteger(pid))) throw new UsageError("missing or invalid process id"); + const normalized = normalizeKillSignal(signal); + if (normalized === 0) { + for (const pid of pids) { + if (pid > 0) await access(`/proc/${pid}`); + } + return 0; + } + for (const pid of pids) { + if (typeof normalized === "number" && normalized > 31) { + if (libc.symbols.kill(pid, normalized) !== 0) throw new Error(`failed to send signal ${normalized} to ${pid}`); + } else process.kill(pid, normalized); + } + return 0; +} + +export function normalizeKillArgs(args) { + const normalized = []; + let end = false; + let sawSignalSelector = false; + for (const arg of args) { + if (end) normalized.push(arg); + else if (arg === "--") { + normalized.push(arg); + end = true; + } + else if (arg === "-s" || arg === "-n") { + normalized.push(arg); + sawSignalSelector = true; + } + else if (/^-n.+/.test(arg)) { + normalized.push("-n", arg.slice(2)); + sawSignalSelector = true; + } + else if (/^-s.+/.test(arg)) { + normalized.push("-s", arg.slice(2)); + sawSignalSelector = true; + } + else if (!sawSignalSelector && /^-\d+$/.test(arg)) { + normalized.push("-s", arg.slice(1)); + sawSignalSelector = true; + } + else if (sawSignalSelector && /^-\d+$/.test(arg)) { + normalized.push("--", arg); + end = true; + } + else if (/^-[A-Z][A-Za-z0-9]*$/.test(arg) && !["-L"].includes(arg)) { + normalized.push("-s", arg.slice(1)); + sawSignalSelector = true; + } + else normalized.push(arg); + } + return normalized; +} + +export function countKillListOptions(args) { + let count = 0; + let end = false; + for (const arg of args) { + if (end) continue; + if (arg === "--") { + end = true; + continue; + } + if (arg === "-l" || arg === "-t" || arg === "-L" || arg === "--list" || arg === "--table") count++; + } + return count; +} + +export function normalizeKillSignal(signal) { + const signum = signalNumberFromOperand(signal); + if (signum < 0) throw new UsageError(`invalid signal '${signal}'`, true); + if (signum >= 34 && signum <= 64) return signum; + return signum === 0 ? 0 : `SIG${SIGNAL_NAMES[signum]}`; +} + +export function listSignals(table, operands) { + const values = operands.length + ? operands + : [...SIGNAL_NAMES.keys(), ...Array.from({ length: 31 }, (_, index) => index + 34)].map(String); + let status = 0; + for (const value of values) { + const signum = signalNumberFromOperand(value); + if (signum < 0) { + status = 1; + continue; + } + const name = signalDisplayName(signum); + if (name == null) continue; + if (table) stdout(`${String(signum).padStart(2)} ${name.padEnd(10)} ${signalDescription(signum)}\n`); + else if (/^\d+$/.test(String(value))) stdout(`${name}\n`); + else stdout(`${signum}\n`); + } + return status; +} + +export function signalDescription(signum) { + if (signum === 0) return "Exit"; + if (signum >= 34 && signum <= 64) return signalDisplayName(signum); + return SIGNAL_NAMES[signum]; +} + +const singleCall = defineCommand("kill", killCmd, (args) => { for (const arg of args) { if (arg === "--") return null; if (arg === "--help" || arg === "--version") return arg; } return null; }); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/link.js b/src/commands/link.js new file mode 100644 index 0000000..65b77b3 --- /dev/null +++ b/src/commands/link.js @@ -0,0 +1,31 @@ +#!/usr/bin/env bun + +import { link as fsLink } from "node:fs/promises"; +import { helpVersionOnlyMetaOption, localeQuotedEscapedDiagnostic, normalizeHelpVersionOnlyLongOptions, parseOptions } from "../shared/common.js"; +import { UsageError, fail } from "../shared/diagnostics.js"; +import { errnoMessage, linkQuotedName } from "../shared/filesystem.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export async function linkCmd(args) { + args = normalizeHelpVersionOnlyLongOptions(args); + const { operands } = parseOptions(args, { long: { help: false, version: false } }); + if (!operands.length) throw new UsageError("missing operand", true); + if (operands.length < 2) throw new UsageError(`missing operand after '${operands[0]}'`, true); + if (operands.length > 2) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[2])}`, true); + try { + await fsLink(operands[0], operands[1]); + } catch (error) { + return fail("link", `cannot create link ${linkQuotedName(operands[1])} to ${linkQuotedName(operands[0])}: ${linkErrorMessage(error)}`); + } + return 0; +} + +export function linkErrorMessage(error) { + if (error?.code === "EEXIST") return "File exists"; + return errnoMessage(error); +} + +const singleCall = defineCommand("link", linkCmd, helpVersionOnlyMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/ln.js b/src/commands/ln.js new file mode 100644 index 0000000..5ffeb1a --- /dev/null +++ b/src/commands/ln.js @@ -0,0 +1,302 @@ +#!/usr/bin/env bun + +import { link as fsLink, unlink as fsUnlink, lstat, readlink, realpath, rename, stat, symlink } from "node:fs/promises"; +import { isAbsolute, join, basename as pathBasename, dirname as pathDirname, relative, resolve } from "node:path"; +import { bufferPathBasename, bufferPathJoin, invalidOptionMessage, normalizeLongOptionByPrefix, parseOptions, pathDisplayName, rawCommandArgs, shellEscapeLsName } from "../shared/common.js"; +import { areSameFile, backupFileName, lnQuotedName, resolveDestinationOperands, validateBackupMode } from "../shared/copy.js"; +import { UsageError, stdout } from "../shared/diagnostics.js"; +import { confirmRemoval, errnoMessage, rawArgLooksLikeOption } from "../shared/filesystem.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const LN_LONG_OPTIONS = ["backup", "directory", "force", "interactive", "logical", "no-dereference", "physical", "relative", "suffix", "symbolic", "target-directory", "target-dir", "no-target-directory", "verbose", "help", "version"]; + +export function lnMetaOption(args) { + const longFlagOptions = new Set(["directory", "force", "interactive", "logical", "no-dereference", "physical", "relative", "symbolic", "no-target-directory", "verbose"]); + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + let normalized = arg; + if (arg.startsWith("--")) normalized = normalizeLnLongOption(arg); + const [name, inlineValue] = normalized.startsWith("--") ? normalized.slice(2).split(/=(.*)/s, 2) : [null, undefined]; + if (name === "help" || name === "version") { + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + return normalized; + } + if (longFlagOptions.has(name)) { + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + continue; + } + if (name === "suffix" || name === "target-directory" || name === "target-dir") { + if (inlineValue === undefined) i++; + continue; + } + if (name === "backup") continue; + if (arg.startsWith("-") && !arg.startsWith("--")) { + i = scanLnShortMetaOption(args, i); + continue; + } + if (arg.startsWith("-")) throw new UsageError(invalidOptionMessage(arg), true); + continue; + } + return null; +} + +export function scanLnShortMetaOption(args, index) { + const arg = args[index]; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if ("sfidFLPnrTvb".includes(ch)) continue; + if (ch === "t" || ch === "S") return arg.slice(j + 1) ? index : index + 1; + throw new UsageError(`invalid option -- '${ch}'`, true); + } + return index; +} + +export async function lnCmd(args) { + args = normalizeLnArgs(args); + const { opts, operands } = parseOptions(args, { short: { s: false, f: false, i: false, L: false, P: false, F: false, d: false, n: false, r: false, t: "value", T: false, v: false, b: false, S: "value" }, long: { symbolic: false, force: false, interactive: false, logical: false, physical: false, directory: false, "no-dereference": false, relative: false, "target-directory": "value", "target-dir": "value", "no-target-directory": false, verbose: false, backup: "optional-value", b: "optional-value", suffix: "value", help: false, version: false } }); + if (opts["target-dir"] != null && opts["target-directory"] == null) opts["target-directory"] = opts["target-dir"]; + if (opts.b != null && opts.backup == null) opts.backup = opts.b; + if (opts.S != null && opts.suffix == null) opts.suffix = opts.S; + if ((opts.r || opts.relative) && !(opts.s || opts.symbolic)) throw new UsageError("cannot do --relative without --symbolic"); + const { sources, dest, useDirectoryTarget } = await resolveLnDestinationOperands(operands, opts); + const backup = opts.b || opts.backup != null; + if (backup) validateBackupMode("ln", opts.backup); + const force = lnForceMode(args, opts); + const dereferenceSource = lnDereferenceSource(args); + const rawPlan = rawLnPlan(opts, operands, useDirectoryTarget); + const createdTargets = new Set(); + let failed = false; + for (const src of sources) { + const rawSource = rawPlan?.sources.shift(); + const target = rawPlan && rawSource + ? rawLnTarget(rawSource, rawPlan.dest, rawPlan.useDirectoryTarget) + : useDirectoryTarget ? join(dest, pathBasename(src)) : dest; + const operationSource = rawSource ?? src; + const targetKey = resolve(pathDisplayName(target)); + if ((force === "force" || (backup && !useDirectoryTarget)) && await areSameFile(src, target)) { + if (!opts.s && !opts.symbolic && force === "force" && useDirectoryTarget) continue; + throw new UsageError(`'${src}' and '${target}' are the same file`); + } + if (!backup && createdTargets.has(targetKey)) throw new UsageError(`will not overwrite just-created '${target}' with '${src}'`); + if (backup) await backupLinkDestination(target, opts.suffix); + else if (force === "force") await removeLinkDestination(target); + else if (force === "interactive" && await lstat(target).then(() => true, () => false)) { + if (!confirmRemoval(`ln: replace '${target}'? `)) { + failed = true; + continue; + } + await removeLinkDestination(target); + } + const linkSource = rawSource ? operationSource : opts.r || opts.relative ? await relativeLinkSource(src, target) : src; + try { + if (opts.s || opts.symbolic || opts.r || opts.relative) await symlink(linkSource, target); + else await fsLink(rawSource ?? await hardLinkSource(src, dereferenceSource, opts.F || opts.d || opts.directory), target); + } catch (error) { + if (String(error?.message ?? "").startsWith("failed to access ")) throw new UsageError(error.message); + if (String(error?.message ?? "").endsWith(": hard link not allowed for directory")) throw new UsageError(error.message); + const kind = opts.s || opts.symbolic || opts.r || opts.relative ? "symbolic link" : "hard link"; + const message = error?.code === "EEXIST" ? "File exists" : errnoMessage(error); + const targetDisplay = lnTargetDisplay(target, sources.length === 1 && operands.length === 1 && !useDirectoryTarget); + throw new UsageError(`failed to create ${kind} '${targetDisplay}': ${message}`); + } + createdTargets.add(targetKey); + if (opts.v || opts.verbose) stdout(`'${target}' => '${src}'\n`); + } + return failed ? 1 : 0; +} + +export function normalizeLnArgs(args) { + const out = []; + let end = false; + for (const arg of args) { + if (end || arg === "--") { + out.push(arg); + if (arg === "--") end = true; + continue; + } + out.push(arg.startsWith("--") ? normalizeLnLongOption(arg) : arg); + } + return out; +} + +export function lnTargetDisplay(target, implicitSingleTarget = false) { + if (!implicitSingleTarget) return target; + const text = pathDisplayName(target); + if (text === "" || text.startsWith("/") || text.startsWith("./") || text.startsWith("../")) return target; + return `./${text}`; +} + +export function normalizeLnLongOption(arg) { + if (arg === "--target-dir" || arg.startsWith("--target-dir=")) return arg; + const options = LN_LONG_OPTIONS.filter((option) => option !== "target-dir"); + return normalizeLongOptionByPrefix(arg, options); +} + +export function rawLnPlan(opts, operands, useDirectoryTarget) { + if (!operands.some((operand) => operand.includes("\uFFFD"))) return null; + const raw = rawCommandArgs("ln"); + if (!raw) return null; + const parsed = parseRawLnArgs(raw); + if (!parsed || parsed.operands.length !== operands.length) return null; + if (opts.t || opts["target-directory"]) { + return { sources: parsed.operands, dest: parsed.targetDirectory, useDirectoryTarget: true }; + } + if (!useDirectoryTarget || parsed.operands.length < 2) return null; + return { sources: parsed.operands.slice(0, -1), dest: parsed.operands.at(-1), useDirectoryTarget: true }; +} + +export function parseRawLnArgs(rawArgs) { + const operands = []; + let targetDirectory = null; + for (let i = 0; i < rawArgs.length; i++) { + const arg = rawArgs[i]; + if (arg.equals(Buffer.from("--"))) { + operands.push(...rawArgs.slice(i + 1)); + break; + } + if (!rawArgLooksLikeOption(arg)) { + operands.push(arg); + continue; + } + const text = arg.toString(); + if (text === "-t" || text === "--target-directory" || text === "--target-dir") { + targetDirectory = rawArgs[++i] ?? null; + continue; + } + if (text.startsWith("--target-directory=")) { + targetDirectory = arg.subarray(Buffer.from("--target-directory=").length); + continue; + } + if (text.startsWith("--target-dir=")) { + targetDirectory = arg.subarray(Buffer.from("--target-dir=").length); + continue; + } + if (arg.length > 2 && arg[0] === 0x2d && arg.includes(0x74)) { + const tIndex = arg.indexOf(0x74); + if (tIndex !== arg.length - 1) targetDirectory = arg.subarray(tIndex + 1); + else targetDirectory = rawArgs[++i] ?? null; + } + } + if (targetDirectory && operands.length) return { operands, targetDirectory }; + return { operands, targetDirectory }; +} + +export function rawLnTarget(source, dest, useDirectoryTarget) { + if (!useDirectoryTarget) return dest; + return bufferPathJoin(dest, bufferPathBasename(source)); +} + +export async function hardLinkSource(src, dereferenceSource, allowDirectory = false) { + if (dereferenceSource) { + let resolved; + try { + resolved = await realpath(src); + } catch (error) { + throw new Error(`failed to access ${lnQuotedName(src)}: ${errnoMessage(error)}`); + } + if (!allowDirectory && (await stat(resolved)).isDirectory()) throw new Error(`${lnDiagnosticName(src)}: hard link not allowed for directory`); + return resolved; + } + const s = (src.endsWith("/") ? await stat(src).catch(() => null) : await lstat(src).catch(() => null)); + if (!s) throw new Error(`failed to access ${lnQuotedName(src)}: No such file or directory`); + if (!allowDirectory && s?.isDirectory()) throw new Error(`${lnDiagnosticName(src)}: hard link not allowed for directory`); + return src; +} + +export function lnDiagnosticName(name) { + return shellEscapeLsName(pathDisplayName(name)); +} + +export async function resolveLnDestinationOperands(operands, opts) { + if (operands.length === 1 && !opts.t && !opts["target-directory"]) { + return { sources: operands, dest: pathBasename(operands[0]), destStat: await stat(pathBasename(operands[0])).catch(() => null), useDirectoryTarget: false }; + } + const noDereference = opts.n || opts["no-dereference"]; + if (noDereference && operands.length === 2 && !opts.t && !opts["target-directory"]) { + const dest = operands[1]; + const destLstat = await lstat(dest).catch(() => null); + if (destLstat?.isSymbolicLink()) return { sources: [operands[0]], dest, destStat: destLstat, useDirectoryTarget: false }; + } + return resolveDestinationOperands("ln", operands, opts); +} + +export function lnForceMode(args, opts) { + let mode = opts.f || opts.force ? "force" : "none"; + for (const arg of args) { + if (arg === "--") break; + if (arg === "-f" || arg === "--force") mode = "force"; + else if (arg === "-i" || arg === "--interactive") mode = "interactive"; + else if (/^-[^-]/.test(arg)) { + for (const ch of arg.slice(1)) { + if (ch === "f") mode = "force"; + else if (ch === "i") mode = "interactive"; + } + } + } + return mode; +} + +export function lnDereferenceSource(args) { + let dereference = false; + for (const arg of args) { + if (arg === "--") break; + if (arg === "-L" || arg === "--logical") dereference = true; + else if (arg === "-P" || arg === "--physical") dereference = false; + else if (/^-[^-]/.test(arg)) { + for (const ch of arg.slice(1)) { + if (ch === "L") dereference = true; + else if (ch === "P") dereference = false; + else if (ch === "s") return false; + } + } + if (arg === "-s" || arg === "--symbolic") return false; + } + return dereference; +} + +export async function backupLinkDestination(target, suffixOption) { + const s = await lstat(target).catch(() => null); + if (!s) return; + const backup = backupFileName(target, suffixOption, "simple"); + await fsUnlink(backup).catch((error) => { + if (error?.code !== "ENOENT") throw error; + }); + await rename(target, backup); +} + +export async function removeLinkDestination(target) { + const s = await lstat(target).catch(() => null); + if (s?.isDirectory() && !s.isSymbolicLink()) return; + await fsUnlink(target).catch((error) => { + if (error?.code !== "ENOENT" && error?.code !== "ENOTDIR" && error?.code !== "ELOOP" && error?.code !== "ENAMETOOLONG") throw error; + }); +} + +export async function relativeLinkSource(src, target) { + if (src === "") return src; + const resolvedSource = await resolveSymlinkChain(src).catch(() => resolve(src)); + const targetDir = pathDirname(resolve(target)); + const rel = relative(targetDir, resolvedSource); + return rel || "."; +} + +export async function resolveSymlinkChain(path) { + let current = resolve(path); + for (let i = 0; i < 40; i++) { + const s = await lstat(current).catch((error) => { + if (error?.code === "ENOENT") return null; + throw error; + }); + if (!s) return current; + if (!s.isSymbolicLink()) return current; + const linkTarget = await readlink(current); + current = isAbsolute(linkTarget) ? linkTarget : resolve(pathDirname(current), linkTarget); + } + return current; +} + +const singleCall = defineCommand("ln", lnCmd, lnMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/logname.js b/src/commands/logname.js new file mode 100644 index 0000000..fdc6ea2 --- /dev/null +++ b/src/commands/logname.js @@ -0,0 +1,38 @@ +#!/usr/bin/env bun + +import { readFile } from "node:fs/promises"; +import { userInfo } from "node:os"; +import { helpVersionOnlyMetaOption, localeQuotedEscapedDiagnostic, normalizeHelpVersionOnlyLongOptions, parseOptions, readPasswd, userNameForUid } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export async function logname(args) { + args = normalizeHelpVersionOnlyLongOptions(args); + const { operands } = parseOptions(args, { long: { help: false, version: false } }); + if (operands.length) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[0])}`, true); + const uid = process.getuid?.() ?? userInfo().uid; + const passwd = await readPasswd(); + const passwdFallback = await uidIsLinuxOverflow(uid) ? null : await userNameForUid(uid); + for (const name of [process.env.LOGNAME, process.env.USER, userInfo().username, passwdFallback]) { + if (!name) continue; + if (passwd.get(name)?.uid === uid) { + stdout(`${name}\n`); + return 0; + } + } + stderr("logname: no login name\n"); + return 1; +} + +export async function uidIsLinuxOverflow(uid) { + try { + return Number((await readFile("/proc/sys/kernel/overflowuid", "utf8")).trim()) === Number(uid); + } catch { + return false; + } +} + +const singleCall = defineCommand("logname", logname, helpVersionOnlyMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/ls.js b/src/commands/ls.js new file mode 100644 index 0000000..38c944e --- /dev/null +++ b/src/commands/ls.js @@ -0,0 +1,9 @@ +#!/usr/bin/env bun + +import { lsCmd, lsMetaOption } from "../shared/listing.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +const singleCall = defineCommand("ls", lsCmd, lsMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/md5sum.js b/src/commands/md5sum.js new file mode 100644 index 0000000..9af992c --- /dev/null +++ b/src/commands/md5sum.js @@ -0,0 +1,9 @@ +#!/usr/bin/env bun + +import { hashCommand, hashMetaOption } from "../shared/hash.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +const singleCall = defineCommand("md5sum", hashCommand("md5"), (args) => hashMetaOption("md5sum", args)); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/mkdir.js b/src/commands/mkdir.js new file mode 100644 index 0000000..c77dcbe --- /dev/null +++ b/src/commands/mkdir.js @@ -0,0 +1,112 @@ +#!/usr/bin/env bun + +import { mkdirSync } from "node:fs"; +import { mkdir } from "node:fs/promises"; +import { localeQuotedDiagnostic, parseOptions, selinuxRuntimeEnabled } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { invalidModeDiagnosticValue, mkdirDiagnosticName, mkdirErrorMessage, mkdirParents, mkdirVerboseName, parseCreationMode, rawOperandPlan, selinuxCreationOptions, setFileMode, withSelinuxCreationContext } from "../shared/filesystem.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const MKDIR_LONG_OPTIONS = ["mode", "parents", "verbose", "context", "help", "version"]; + +export function mkdirMetaOption(args) { + let sawContextValue = false; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (!arg.startsWith("--")) continue; + const option = normalizeMkdirLongOption(arg); + const name = option.slice(2).split("=", 1)[0]; + if (!MKDIR_LONG_OPTIONS.includes(name)) return null; + if (option.includes("=")) { + if (name === "parents" || name === "verbose" || name === "help" || name === "version") return null; + if (name === "context") sawContextValue = true; + continue; + } + if (option === "--help" || option === "--version") { + if (sawContextValue && !selinuxRuntimeEnabled()) stderr("mkdir: warning: ignoring --context; it requires an SELinux/SMACK-enabled kernel\n"); + return option; + } + if (name === "mode") i++; + } + return null; +} + +export async function mkdirCmd(args) { + args = normalizeMkdirLongOptions(args); + const { opts, operands } = parseOptions(args, { short: { p: false, m: "value", v: false, Z: false }, long: { parents: false, mode: "value", verbose: false, context: "optional-value", help: false, version: false } }); + const securityContext = selinuxCreationOptions("mkdir", opts); + if (!operands.length) throw new UsageError("missing operand", true); + const rawOperands = rawOperandPlan("mkdir", args, operands, { + valueOptions: ["--mode"], + shortValueOptions: ["m"], + }); + const mode = opts.m || opts.mode ? parseMkdirCreationMode(opts.m ?? opts.mode) : undefined; + const parents = opts.p || opts.parents; + await withSelinuxCreationContext("mkdir", securityContext, async (created, sameThread) => { + for (let i = 0; i < operands.length; i++) { + const dir = rawOperands?.[i] ?? operands[i]; + if (dir === "") throw new UsageError(`cannot create directory ${localeQuotedDiagnostic("")}: No such file or directory`); + if (parents) await mkdirParents(dir, mode, opts.v || opts.verbose, created, sameThread); + else { + try { + if (sameThread) mkdirSync(dir, { mode }); + else await mkdir(dir, { mode }); + } catch (error) { + throw new UsageError(`cannot create directory ${mkdirDiagnosticName(dir)}: ${mkdirErrorMessage(error)}`); + } + if (mode !== undefined) await setFileMode(dir, mode); + await created(dir); + if (opts.v || opts.verbose) stdout(`mkdir: created directory ${mkdirVerboseName(dir)}\n`); + } + } + }); + return 0; +} + +export function normalizeMkdirLongOptions(args) { + const out = []; + let end = false; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (end || arg === "--" || !arg.startsWith("--")) { + out.push(arg); + if (arg === "--") end = true; + continue; + } + const option = normalizeMkdirLongOption(arg); + out.push(option); + if (option === "--mode" && i + 1 < args.length) out.push(args[++i]); + } + return out; +} + +export function normalizeMkdirLongOption(arg) { + const body = arg.slice(2); + const eq = body.indexOf("="); + const name = eq === -1 ? body : body.slice(0, eq); + if (!name) return arg; + const matches = MKDIR_LONG_OPTIONS.filter((option) => option.startsWith(name)); + if (matches.length === 0) return arg; + if (matches.length > 1) { + throw new UsageError(`option '--${name}${eq === -1 ? "" : `=${body.slice(eq + 1)}`}' is ambiguous; possibilities: ${matches.map((option) => `'--${option}'`).join(" ")}`, true); + } + return eq === -1 ? `--${matches[0]}` : `--${matches[0]}=${body.slice(eq + 1)}`; +} + +export function parseMkdirCreationMode(spec) { + try { + return parseCreationMode(spec); + } catch (error) { + const mode = error instanceof UsageError ? invalidModeDiagnosticValue(error.message) : null; + if (mode != null) { + throw new UsageError(`invalid mode ${localeQuotedDiagnostic(mode)}`, false); + } + throw error; + } +} + +const singleCall = defineCommand("mkdir", mkdirCmd, mkdirMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/mkfifo.js b/src/commands/mkfifo.js new file mode 100644 index 0000000..0491c2f --- /dev/null +++ b/src/commands/mkfifo.js @@ -0,0 +1,29 @@ +#!/usr/bin/env bun + +import { cstr, libc, parseOptions } from "../shared/common.js"; +import { UsageError } from "../shared/diagnostics.js"; +import { ensureSpecialFileCreatable, normalizeSpecialFileLongOptions, parseSpecialFileCreationMode, selinuxCreationOptions, setFileMode, specialFileMetaOption, specialFileQuotedName, withSelinuxCreationContext } from "../shared/filesystem.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export async function mkfifoCmd(args) { + args = normalizeSpecialFileLongOptions(args); + const { opts, operands } = parseOptions(args, { short: { m: "value", Z: false }, long: { mode: "value", context: "optional-value", help: false, version: false } }); + const securityContext = selinuxCreationOptions("mkfifo", opts); + if (!operands.length) throw new UsageError("missing operand", true); + const explicitMode = opts.m ?? opts.mode; + const mode = explicitMode ? parseSpecialFileCreationMode(explicitMode) : 0o666; + await withSelinuxCreationContext("mkfifo", securityContext, async (created) => { + for (const path of operands) { + await ensureSpecialFileCreatable("mkfifo", path, "fifo"); + if (libc.symbols.mkfifo(cstr(path), mode) !== 0) throw new UsageError(`cannot create fifo ${specialFileQuotedName(path)}: Operation not permitted`); + if (explicitMode) await setFileMode(path, mode); + await created(path); + } + }); + return 0; +} + +const singleCall = defineCommand("mkfifo", mkfifoCmd, (args) => specialFileMetaOption("mkfifo", args)); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/mknod.js b/src/commands/mknod.js new file mode 100644 index 0000000..0a0ce12 --- /dev/null +++ b/src/commands/mknod.js @@ -0,0 +1,75 @@ +#!/usr/bin/env bun + +import { cstr, libc, localeQuotedEscapedDiagnostic, parseOptions } from "../shared/common.js"; +import { UsageError } from "../shared/diagnostics.js"; +import { ensureSpecialFileCreatable, mknodDiagnosticName, normalizeSpecialFileLongOptions, parseSpecialFileCreationMode, selinuxCreationOptions, setFileMode, specialFileMetaOption, specialFileQuotedName, withSelinuxCreationContext } from "../shared/filesystem.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export async function mknodCmd(args) { + args = normalizeSpecialFileLongOptions(args); + const { opts, operands } = parseOptions(args, { short: { m: "value", Z: false }, long: { mode: "value", context: "optional-value", help: false, version: false } }); + const securityContext = selinuxCreationOptions("mknod", opts); + const explicitMode = opts.m ?? opts.mode; + const perm = explicitMode ? parseSpecialFileCreationMode(explicitMode) : 0o666; + if (!operands.length) throw new UsageError("missing operand", true); + if (operands.length === 1) throw new UsageError(`missing operand after '${operands[0]}'`, true); + const [path, type, major, minor] = operands; + if (type === "p") { + if (operands.length > 2) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[2])}`, true); + await withSelinuxCreationContext("mknod", securityContext, async (created) => { + await ensureSpecialFileCreatable("mknod", path, "fifo"); + if (libc.symbols.mkfifo(cstr(path), perm) !== 0) throw new UsageError(`cannot create fifo ${specialFileQuotedName(path)}: Operation not permitted`); + if (explicitMode) await setFileMode(path, perm); + await created(path); + }); + return 0; + } + if (operands.length > 4) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[4])}`, true); + if (major == null) throw new UsageError(`missing operand after '${type}'\nSpecial files require major and minor device numbers.`, true); + if (minor == null) throw new UsageError(`missing operand after '${major}'`, true); + if (type === "b" || type === "c" || type === "u") { + const majorNumber = parseMknodDeviceNumber(major, "major"); + const minorNumber = parseMknodDeviceNumber(minor, "minor"); + await withSelinuxCreationContext("mknod", securityContext, async (created) => { + await ensureSpecialFileCreatable("mknod", path, "special file"); + const modeType = type === "b" ? 0o060000 : 0o020000; + if (libc.symbols.mknod(cstr(path), modeType | perm, makedev(majorNumber, minorNumber)) !== 0) throw new UsageError(`${mknodDiagnosticName(path)}: Operation not permitted`); + await created(path); + }); + return 0; + } + throw new UsageError(`invalid device type ${localeQuotedEscapedDiagnostic(type)}`, true); +} + +export function parseMknodDeviceNumber(value, kind) { + const text = String(value); + let digits; + let base; + if (/^0[xX][0-9A-Fa-f]+$/.test(text)) { + digits = text.slice(2); + base = 16n; + } else if (/^0[0-7]*$/.test(text)) { + digits = text; + base = 8n; + } else if (/^[1-9][0-9]*$/.test(text)) { + digits = text; + base = 10n; + } else { + throw new UsageError(`invalid ${kind} device number ${localeQuotedEscapedDiagnostic(value)}`); + } + let number = 0n; + for (const ch of digits) { + number = number * base + BigInt(Number.parseInt(ch, Number(base))); + if (number > 0xffffffffn) throw new UsageError(`invalid ${kind} device number ${localeQuotedEscapedDiagnostic(value)}`); + } + return number; +} + +export function makedev(major, minor) { + return Number(((major & 0xfffn) << 8n) | (minor & 0xffn) | ((minor & ~0xffn) << 12n) | ((major & ~0xfffn) << 32n)); +} + +const singleCall = defineCommand("mknod", mknodCmd, (args) => specialFileMetaOption("mknod", args)); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/mktemp.js b/src/commands/mktemp.js new file mode 100644 index 0000000..2b67f4b --- /dev/null +++ b/src/commands/mktemp.js @@ -0,0 +1,171 @@ +#!/usr/bin/env bun + +import { mkdir, open, rm } from "node:fs/promises"; +import { isAbsolute, join, basename as pathBasename, dirname as pathDirname } from "node:path"; +import { isWriteError, localeQuotedEscapedDiagnostic, parseOptions, systemErrorMessage } from "../shared/common.js"; +import { UsageError, stdout } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const MKTEMP_LONG_OPTIONS = ["directory", "dry-run", "quiet", "suffix", "tmpdir", "help", "version"]; + +export function mktempMetaOption(args) { + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (!arg.startsWith("-") || arg === "-") { + if (process.env.POSIXLY_CORRECT) return null; + continue; + } + if (arg.startsWith("--")) { + const option = normalizeMktempLongOption(arg); + const name = option.slice(2).split("=", 1)[0]; + if (!MKTEMP_LONG_OPTIONS.includes(name)) return null; + if (option.includes("=")) { + if (name === "directory" || name === "dry-run" || name === "quiet" || name === "help" || name === "version") return null; + continue; + } + if (option === "--help" || option === "--version") return option; + if (name === "suffix") i++; + continue; + } + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (ch === "d" || ch === "q" || ch === "t" || ch === "u") continue; + if (ch !== "p") return null; + if (!arg.slice(j + 1)) i++; + break; + } + } + return null; +} + +export async function mktempCmd(args) { + if (process.env.POSIXLY_CORRECT && args.findIndex((arg) => arg !== "--" && !arg.startsWith("-")) !== -1) { + const firstOperand = args.findIndex((arg) => arg !== "--" && !arg.startsWith("-")); + if (args.slice(firstOperand + 1).some((arg) => arg.startsWith("-"))) throw new UsageError("too many templates", true); + } + args = normalizeMktempLongOptions(args); + const { opts, operands } = parseOptions(args, { short: { d: false, p: "value", q: false, t: false, u: false }, long: { directory: false, quiet: false, tmpdir: "optional-value", suffix: "value", "dry-run": false, help: false, version: false } }); + if (operands.length > 1) throw new UsageError("too many templates", true); + const template = operands[0] ?? "tmp.XXXXXXXXXX"; + const useTmpdir = opts.t || opts.tmpdir !== undefined || opts.p !== undefined || !operands.length; + const envTmpdir = process.env.TMPDIR || null; + const defaultTmpdir = envTmpdir ?? "/tmp"; + const optionTmpdir = opts.tmpdir === true || opts.tmpdir === "" ? defaultTmpdir : opts.tmpdir ?? (opts.p === "" ? defaultTmpdir : opts.p); + const tmpdir = opts.t ? (envTmpdir ?? optionTmpdir ?? defaultTmpdir) : optionTmpdir; + if (opts.t && template.includes("/")) throw new UsageError(`invalid template, ${localeQuotedEscapedDiagnostic(template)}, contains directory separator`); + if (useTmpdir && isAbsolute(template)) throw new UsageError(`invalid template, ${localeQuotedEscapedDiagnostic(template)}; with --tmpdir, it may not be absolute`); + const dir = useTmpdir ? tmpdir ?? defaultTmpdir : template.includes("/") ? pathDirname(template) : ""; + const name = template.includes("/") ? pathBasename(template) : template; + if (opts.suffix !== undefined && !name.endsWith("X")) throw new UsageError(`with --suffix, template ${localeQuotedEscapedDiagnostic(template)} must end in X`); + const wholeXMatch = [...template.matchAll(/X{3,}/g)].at(-1); + if (opts.suffix === undefined && wholeXMatch) { + const implicitSuffix = template.slice(wholeXMatch.index + wholeXMatch[0].length); + const slash = implicitSuffix.indexOf("/"); + if (slash !== -1 && !implicitSuffix.slice(slash + 1).includes("X")) { + throw new UsageError(`invalid suffix ${localeQuotedEscapedDiagnostic(implicitSuffix)}, contains directory separator`); + } + } + const xMatch = [...name.matchAll(/X{3,}/g)].at(-1); + const x = xMatch?.[0]; + if (!x || x.length < 3) throw new UsageError(`too few X's in template ${localeQuotedEscapedDiagnostic(template)}`); + if (opts.suffix === undefined && wholeXMatch && template.slice(wholeXMatch.index + wholeXMatch[0].length).includes("/")) { + throw new UsageError(`invalid suffix ${localeQuotedEscapedDiagnostic(template.slice(wholeXMatch.index + wholeXMatch[0].length))}, contains directory separator`); + } + const suffix = opts.suffix ?? name.slice(xMatch.index + x.length); + if (suffix.includes("/")) throw new UsageError(`invalid suffix ${localeQuotedEscapedDiagnostic(suffix)}, contains directory separator`); + const templateDir = template.includes("/") && useTmpdir ? pathDirname(template) : ""; + const parent = mktempJoin(dir, templateDir); + const namePrefix = name.slice(0, xMatch.index); + const prefix = mktempPrefix(parent, namePrefix); + const displayTemplate = `${prefix}${x}${suffix}`; + const candidateName = () => `${prefix}${mktempRandom(x.length)}${suffix}`; + if (opts.u || opts["dry-run"]) { + stdout(`${candidateName()}\n`); + return 0; + } + for (let i = 0; i < 100; i++) { + const candidate = candidateName(); + try { + if (opts.d || opts.directory) await mkdir(candidate, { mode: 0o700 }); + else { + const fh = await open(candidate, "wx", 0o600); + await fh.close(); + } + try { + stdout(`${candidate}\n`); + } catch (error) { + if (isWriteError(error)) await rm(candidate, { recursive: true, force: true }); + throw error; + } + return 0; + } catch (error) { + if (error.code === "ENOENT") { + if (opts.q || opts.quiet) return 1; + throw new UsageError(`failed to create file via template ${localeQuotedEscapedDiagnostic(displayTemplate)}: ${systemErrorMessage(error)}`); + } + if (error.code !== "EEXIST") { + if (opts.q || opts.quiet) return 1; + throw new UsageError(`failed to create file via template ${localeQuotedEscapedDiagnostic(displayTemplate)}: ${systemErrorMessage(error)}`); + } + } + } + if (opts.q || opts.quiet) return 1; + throw new UsageError(`failed to create file via template ${localeQuotedEscapedDiagnostic(displayTemplate)}`); +} + +export function normalizeMktempLongOptions(args) { + const out = []; + let end = false; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (end || arg === "--" || !arg.startsWith("--")) { + out.push(arg); + if (arg === "--") end = true; + continue; + } + const option = normalizeMktempLongOption(arg); + out.push(option); + if (option === "--suffix" && i + 1 < args.length) out.push(args[++i]); + } + return out; +} + +export function normalizeMktempLongOption(arg) { + const body = arg.slice(2); + const eq = body.indexOf("="); + const name = eq === -1 ? body : body.slice(0, eq); + if (!name) return arg; + const matches = MKTEMP_LONG_OPTIONS.filter((option) => option.startsWith(name)); + if (matches.length === 0) return arg; + if (matches.length > 1) { + throw new UsageError(`option '--${name}${eq === -1 ? "" : `=${body.slice(eq + 1)}`}' is ambiguous; possibilities: ${matches.map((option) => `'--${option}'`).join(" ")}`, true); + } + return eq === -1 ? `--${matches[0]}` : `--${matches[0]}=${body.slice(eq + 1)}`; +} + +export function mktempJoin(...parts) { + const filtered = parts.filter((part) => part !== ""); + if (filtered.length === 0) return ""; + if (filtered[0] === "." && filtered.length > 1) return `./${join(...filtered.slice(1))}`; + if (filtered[0].startsWith("./") && filtered.length > 1) return `${filtered[0]}/${join(...filtered.slice(1))}`; + return join(...filtered); +} + +export function mktempPrefix(parent, namePrefix) { + if (!parent) return namePrefix; + if (namePrefix) return mktempJoin(parent, namePrefix); + return parent.endsWith("/") ? parent : `${parent}/`; +} + +export function mktempRandom(length) { + const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + let out = ""; + for (let i = 0; i < length; i++) out += alphabet[Math.floor(Math.random() * alphabet.length)]; + return out; +} + +const singleCall = defineCommand("mktemp", mktempCmd, mktempMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/mv.js b/src/commands/mv.js new file mode 100644 index 0000000..b9ca7b7 --- /dev/null +++ b/src/commands/mv.js @@ -0,0 +1,356 @@ +#!/usr/bin/env bun + +import { constants as fsConstants } from "node:fs"; +import { access, lstat, readdir, rename, rm } from "node:fs/promises"; +import { join, basename as pathBasename, resolve } from "node:path"; +import { AT_FDCWD, cstrPath, invalidOptionMessage, libc, normalizeLongOptionByPrefix, parseOptions, selinuxRuntimeEnabled } from "../shared/common.js"; +import { areSameFile, backupDestination, copyPath, cpDuplicateDisplayName, cpUpdateDecision, ensureBackupDoesNotDestroySource, resolveDestinationOperands, stripTrailingSlashesPath, validateBackupMode, validateUpdateMode } from "../shared/copy.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { confirmRemoval, errnoMessage, modeString, restoreSelinuxSecurityContext } from "../shared/filesystem.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const RENAME_EXCHANGE = 2; + +export const MV_LONG_OPTIONS = ["backup", "debug", "exchange", "force", "interactive", "no-clobber", "no-copy", "strip-trailing-slashes", "suffix", "target-directory", "no-target-directory", "update", "verbose", "context", "help", "version"]; + +export function mvMetaOption(args) { + const longFlagOptions = new Set(["debug", "exchange", "force", "interactive", "no-clobber", "no-copy", "strip-trailing-slashes", "no-target-directory", "verbose", "context"]); + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + let normalized = arg; + if (arg.startsWith("--")) normalized = normalizeLongOptionByPrefix(arg, MV_LONG_OPTIONS); + const [name, inlineValue] = normalized.startsWith("--") ? normalized.slice(2).split(/=(.*)/s, 2) : [null, undefined]; + if (name === "help" || name === "version") { + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + return normalized; + } + if (longFlagOptions.has(name)) { + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + continue; + } + if (name === "suffix" || name === "target-directory") { + if (inlineValue === undefined) i++; + continue; + } + if (name === "backup") continue; + if (name === "update") { + if (inlineValue !== undefined) validateUpdateMode("mv", inlineValue); + continue; + } + if (arg.startsWith("-") && !arg.startsWith("--")) { + i = scanMvShortMetaOption(args, i); + continue; + } + if (arg.startsWith("-")) throw new UsageError(invalidOptionMessage(arg), true); + continue; + } + return null; +} + +export function scanMvShortMetaOption(args, index) { + const arg = args[index]; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if ("dfinuvbTZ".includes(ch)) continue; + if (ch === "t" || ch === "S") return arg.slice(j + 1) ? index : index + 1; + throw new UsageError(`invalid option -- '${ch}'`, true); + } + return index; +} + +export async function mvCmd(args) { + args = normalizeMvArgs(args); + const { opts, operands } = parseOptions(args, { short: { f: false, i: false, n: false, u: false, t: "value", T: false, v: false, b: false, S: "value", Z: false }, long: { context: false, force: false, interactive: false, "no-clobber": false, "no-copy": false, update: "optional-value", target: "value", "target-directory": "value", "no-target-directory": false, "strip-trailing-slashes": false, verbose: false, debug: false, exchange: false, backup: "optional-value", b: "optional-value", suffix: "value", help: false, version: false } }); + const selinuxEnabled = selinuxRuntimeEnabled(); + const securityContext = { + selinuxEnabled, + restoreContext: selinuxEnabled && Boolean(opts.Z || opts.context), + preserveContext: selinuxEnabled && !(opts.Z || opts.context), + }; + if (opts.target != null && opts["target-directory"] == null) opts["target-directory"] = opts.target; + if (opts.b != null && opts.backup == null) opts.backup = opts.b; + if (opts.S != null && opts.suffix == null) opts.suffix = opts.S; + const { sources, dest, useDirectoryTarget } = await resolveDestinationOperands("mv", operands, opts); + const backupRequested = opts.b || opts.backup != null; + if (backupRequested) validateBackupMode("mv", opts.backup); + const overwriteMode = mvOverwriteMode(args, opts); + const forceOverwrite = mvForceOverwriteIsEffective(args); + if (backupRequested && (opts.exchange || overwriteMode === "no-clobber" || opts.update === "none" || opts.update === "none-fail")) { + throw new UsageError("cannot combine --backup with --exchange, -n, or --update=none-fail", true); + } + if (opts.exchange) return mvExchangeCmd(sources, dest, useDirectoryTarget, opts, securityContext); + const seenSources = new Set(); + const createdTargets = new Set(); + const linkMap = new Map(); + let status = 0; + for (const src of sources) { + const normalizedSrc = opts["strip-trailing-slashes"] ? stripTrailingSlashesPath(src) : src; + try { + const target = useDirectoryTarget ? join(dest, pathBasename(normalizedSrc)) : dest; + const sourceInfo = await lstat(normalizedSrc).catch((error) => { + if (error?.code === "ENOENT") throw new UsageError(`cannot stat '${src}': No such file or directory`); + throw error; + }); + const sourceKey = `${sourceInfo.dev}:${sourceInfo.ino}`; + if (sourceInfo.isDirectory() && seenSources.has(sourceKey)) { + stderr(`mv: warning: source directory '${cpDuplicateDisplayName(src)}' specified more than once\n`); + continue; + } + seenSources.add(sourceKey); + await ensureMvNotIntoSelf(src, target, sourceInfo); + const targetKey = resolve(target); + if (!backupRequested && createdTargets.has(targetKey)) throw new UsageError(`will not overwrite just-created '${target}' with '${src}'`); + if (!backupRequested && await areSameFile(normalizedSrc, target) && !await mvSameFileIsAllowed(normalizedSrc, target, sourceInfo)) throw new UsageError(`'${src}' and '${target}' are the same file`); + await ensureMvTargetTypeCompatible(src, target, sourceInfo); + const updateDecision = await cpUpdateDecision(normalizedSrc, target, opts, false, overwriteMode); + if (updateDecision.skip) { + if (updateDecision.fail) { + stderr(`mv: not replacing '${target}'\n`); + status = 1; + } + continue; + } + const targetInfo = await lstat(target).catch(() => null); + const writeProtected = targetInfo && await mvTargetIsWriteProtected(target, targetInfo); + const shouldPrompt = targetInfo && (overwriteMode === "interactive" + || (overwriteMode === "replace" && !forceOverwrite && writeProtected && process.stdin.isTTY)); + if (shouldPrompt && !confirmRemoval(mvOverwritePrompt(target, targetInfo, writeProtected))) { + status = 1; + continue; + } + if (backupRequested) await ensureBackupDoesNotDestroySource("mv", normalizedSrc, target, opts.suffix, opts.backup); + const backupName = backupRequested ? await backupDestination(target, opts.suffix, opts.backup) : ""; + const renamed = await mvRenameOrCopy(normalizedSrc, target, sourceInfo, opts, mvDisplayTarget(src, target, useDirectoryTarget), linkMap, securityContext); + createdTargets.add(targetKey); + if (renamed && (opts.v || opts.verbose || opts.debug)) stdout(`renamed '${src}' -> '${target}'${backupName ? ` (backup: '${backupName}')` : ""}\n`); + } catch (error) { + status = 1; + if (error instanceof UsageError) stderr(`mv: ${error.message}\n`); + else stderr(`mv: ${error.message || String(error)}\n`); + } + } + return status; +} + +export function normalizeMvArgs(args) { + const out = []; + let end = false; + for (const arg of args) { + if (end || arg === "--") { + out.push(arg); + if (arg === "--") end = true; + continue; + } + out.push(arg.startsWith("--") ? normalizeLongOptionByPrefix(arg, MV_LONG_OPTIONS) : arg); + } + return out; +} + +export async function mvExchangeCmd(sources, dest, useDirectoryTarget, opts, securityContext) { + if (!useDirectoryTarget && sources.length !== 1) throw new UsageError(`extra operand '${sources[1] ?? dest}'`, true); + let status = 0; + for (const source of sources) { + const src = opts["strip-trailing-slashes"] ? stripTrailingSlashesPath(source) : source; + const target = useDirectoryTarget ? join(dest, pathBasename(src)) : dest; + const exchanged = await mvExchangePair(src, target, opts, securityContext); + if (!exchanged) status = 1; + } + return status; +} + +export async function mvExchangePair(src, target, opts, securityContext) { + try { + const sourceInfo = await lstat(src).catch((error) => { + if (error?.code === "ENOENT") throw new UsageError(`cannot stat '${src}': No such file or directory`); + throw error; + }); + const targetInfo = await lstat(target); + if (libc.symbols.renameat2(AT_FDCWD, cstrPath(src), AT_FDCWD, cstrPath(target), RENAME_EXCHANGE) !== 0) { + throw new UsageError(`cannot exchange '${src}' and '${target}': Operation not supported`); + } + if (securityContext.restoreContext) { + if (!restoreSelinuxSecurityContext(src, targetInfo.isSymbolicLink(), targetInfo.isDirectory())) { + throw new UsageError(`failed to restore the security context of '${src}'`); + } + if (!restoreSelinuxSecurityContext(target, sourceInfo.isSymbolicLink(), sourceInfo.isDirectory())) { + throw new UsageError(`failed to restore the security context of '${target}'`); + } + } + if (opts.v || opts.verbose || opts.debug) stdout(`exchanged '${src}' <-> '${target}'\n`); + return true; + } catch (error) { + if (error instanceof UsageError) stderr(`mv: ${error.message}\n`); + else stderr(`mv: cannot exchange '${src}' and '${target}': ${errnoMessage(error)}\n`); + return false; + } +} + +export function mvOverwriteMode(args, opts) { + let mode = "replace"; + let sawOverwriteOption = false; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") break; + if (arg === "-i" || arg === "--interactive") { mode = "interactive"; sawOverwriteOption = true; } + else if (arg === "-n" || arg === "--no-clobber") { mode = "no-clobber"; sawOverwriteOption = true; } + else if (arg === "-f" || arg === "--force") { mode = "replace"; sawOverwriteOption = true; } + else if (/^-[^-]/.test(arg)) { + for (const ch of arg.slice(1)) { + if (ch === "i") { mode = "interactive"; sawOverwriteOption = true; } + else if (ch === "n") { mode = "no-clobber"; sawOverwriteOption = true; } + else if (ch === "f") { mode = "replace"; sawOverwriteOption = true; } + } + } + } + if (!sawOverwriteOption && (opts.i || opts.interactive)) return "interactive"; + if (!sawOverwriteOption && (opts.n || opts["no-clobber"])) return "no-clobber"; + return mode; +} + +export function mvForceOverwriteIsEffective(args) { + let forced = false; + for (const arg of args) { + if (arg === "--") break; + if (arg === "-f" || arg === "--force") forced = true; + else if (arg === "-i" || arg === "--interactive" || arg === "-n" || arg === "--no-clobber") forced = false; + else if (/^-[^-]/.test(arg)) { + for (const ch of arg.slice(1)) { + if (ch === "f") forced = true; + else if (ch === "i" || ch === "n") forced = false; + } + } + } + return forced; +} + +export async function mvTargetIsWriteProtected(target, targetInfo) { + if (targetInfo.isSymbolicLink()) return false; + try { + await access(target, fsConstants.W_OK); + return false; + } catch { + return true; + } +} + +export function mvOverwritePrompt(target, targetInfo, writeProtected) { + if (writeProtected) { + const mode = (targetInfo.mode & 0o7777).toString(8).padStart(4, "0"); + return `mv: replace '${target}', overriding mode ${mode} (${modeString(targetInfo).slice(1)})? `; + } + return `mv: overwrite '${target}'? `; +} + +export async function ensureMvTargetTypeCompatible(src, target, sourceInfo) { + const targetInfo = await lstat(target).catch(() => null); + if (!targetInfo) return; + if (targetInfo.isDirectory() && !targetInfo.isSymbolicLink() && !sourceInfo.isDirectory()) { + throw new UsageError(`cannot overwrite directory '${target}' with non-directory '${src}'`); + } + if (sourceInfo.isDirectory() && (!targetInfo.isDirectory() || targetInfo.isSymbolicLink())) { + throw new UsageError(`cannot overwrite non-directory '${target}' with directory '${src}'`); + } + if (sourceInfo.isDirectory() && targetInfo.isDirectory()) { + const entries = await readdir(target).catch(() => []); + if (entries.length > 0) throw new UsageError(`cannot overwrite '${target}': Directory not empty`); + } +} + +export async function mvSameFileIsAllowed(src, target, sourceInfo) { + if (sourceInfo.isSymbolicLink()) return false; + const targetInfo = await lstat(target).catch(() => null); + if (!targetInfo?.isSymbolicLink()) return false; + return sourceInfo.dev !== targetInfo.dev; +} + +export async function mvRenameOrCopy(src, target, sourceInfo, opts, displayTarget = target, linkMap = new Map(), securityContext = {}) { + try { + await rename(src, target); + if (securityContext.restoreContext + && !restoreSelinuxSecurityContext(target, sourceInfo.isSymbolicLink(), sourceInfo.isDirectory())) { + throw new UsageError(`failed to restore the security context of '${target}'`); + } + return true; + } catch (error) { + if (error instanceof UsageError) throw error; + if (isDirectoryNotEmptyRenameError(error)) throw new UsageError(`cannot overwrite '${target}': Directory not empty`); + if (error?.code !== "EXDEV") throw new UsageError(`cannot move '${src}' to '${displayTarget}': ${errnoMessage(error)}`); + if (opts["no-copy"]) throw new UsageError(`inter-device move failed: '${src}' to '${target}'; --no-copy specified`); + } + await ensureMvCrossDeviceDirectoryReplaceable(src, target, sourceInfo); + try { + await copyPath(src, target, { + force: true, + removeDestination: true, + recursive: true, + preserveMode: true, + preserveDirectoryMode: true, + preserveMetadata: true, + preserveLinks: true, + preserveXattrs: true, + requirePreserveXattrs: false, + reflinkMode: "auto", + sparseMode: "auto", + attributesOnly: false, + debug: false, + linkMap, + symbolicLink: false, + hardLink: false, + noDereference: true, + dereferenceAll: false, + dereferenceCommandLine: false, + selinuxEnabled: Boolean(securityContext.selinuxEnabled), + preserveContext: Boolean(securityContext.preserveContext), + preserveContextAtCreation: Boolean(securityContext.preserveContext), + requirePreserveContext: false, + restoreContext: Boolean(securityContext.restoreContext), + requireRestoreContext: Boolean(securityContext.restoreContext), + explicitContext: null, + verbose: opts.v || opts.verbose, + }, true); + } catch (error) { + throw new UsageError(`inter-device move failed: '${src}' to '${target}'; unable to remove target: ${errnoMessage(error)}`); + } + if ((opts.v || opts.verbose) && sourceInfo.isDirectory()) stdout(`copied '${src}' -> '${target}'\n`); + try { + await rm(src, { force: true, recursive: sourceInfo.isDirectory() }); + } catch (error) { + const message = error?.code === "EFAULT" ? "Operation not permitted" : errnoMessage(error); + throw new UsageError(`cannot remove '${src}': ${message}`); + } + return false; +} + +export function mvDisplayTarget(src, target, useDirectoryTarget) { + if (useDirectoryTarget && (target === pathBasename(src) || target === `./${pathBasename(src)}`)) return `./${pathBasename(src)}`; + return target; +} + +export function isDirectoryNotEmptyRenameError(error) { + return error?.code === "ENOTEMPTY" || error?.code === "EEXIST" || /ENOTEMPTY|EEXIST|Directory not empty/i.test(error?.message ?? ""); +} + +export async function ensureMvCrossDeviceDirectoryReplaceable(src, target, sourceInfo) { + if (!sourceInfo.isDirectory()) return; + const targetInfo = await lstat(target).catch(() => null); + if (!targetInfo?.isDirectory() || targetInfo.isSymbolicLink()) return; + const entries = await readdir(target).catch(() => []); + if (entries.length > 0) { + throw new UsageError(`inter-device move failed: '${src}' to '${target}'; unable to remove target: Directory not empty`); + } +} + +export async function ensureMvNotIntoSelf(src, target, sourceInfo) { + if (!sourceInfo?.isDirectory()) return; + const sourcePath = resolve(src); + const targetPath = resolve(target); + if (targetPath === sourcePath || targetPath.startsWith(`${sourcePath}/`)) { + throw new UsageError(`cannot move '${src}' to a subdirectory of itself, '${target}'`); + } +} + +const singleCall = defineCommand("mv", mvCmd, mvMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/nice.js b/src/commands/nice.js new file mode 100644 index 0000000..6f23d51 --- /dev/null +++ b/src/commands/nice.js @@ -0,0 +1,132 @@ +#!/usr/bin/env bun + +import { writeSync } from "node:fs"; +import { invalidOptionMessage, libc, localeQuotedEscapedDiagnostic } from "../shared/common.js"; +import { InvocationError, stderr, stdout } from "../shared/diagnostics.js"; +import { commandSpawnErrorMessage, isKnownUnexecutableCommand, normalizeInvocationLongOptionByPrefix } from "../shared/process.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export function niceMetaOption(args) { + const longOptions = ["adjustment", "help", "version"]; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + const normalized = arg.startsWith("--") ? normalizeInvocationLongOptionByPrefix(arg, longOptions) : arg; + const [name, inlineValue] = normalized.startsWith("--") ? normalized.slice(2).split(/=(.*)/s, 2) : [null, undefined]; + if (name === "help" || name === "version") { + if (inlineValue !== undefined) throw new InvocationError(`option '--${name}' doesn't allow an argument`); + return normalized; + } + if (arg === "-n" || arg === "--adjustment") { + i++; + continue; + } + if (/^-n.+/.test(arg) || /^--adjustment=/.test(arg) || /^-[+-]?\d+$/.test(arg)) continue; + if (arg.startsWith("-")) throw new InvocationError(invalidOptionMessage(arg)); + return null; + } + return null; +} + +export async function niceCmd(args) { + const parsed = parseNiceArgs(args); + if (parsed.error) { + stderr(`nice: ${parsed.error}\n`); + if (parsed.showHelp) stderr("Try 'nice --help' for more information.\n"); + return 125; + } + if (!parsed.command.length) { + if (parsed.adjusted) { + stderr("nice: a command must be given with an adjustment\nTry 'nice --help' for more information.\n"); + return 125; + } + stdout(`${currentNiceValue()}\n`); + return 0; + } + const nextNice = adjustedNiceValue(parsed.adjustment); + if (libc.symbols.setpriority(0, 0, nextNice) !== 0) { + if (!writeNiceWarning()) return 125; + } + try { + if (await isKnownUnexecutableCommand(parsed.command[0])) throw Object.assign(new Error("Permission denied"), { code: "EACCES" }); + const proc = Bun.spawn(parsed.command, { env: process.env, stdin: "inherit", stdout: "inherit", stderr: "inherit" }); + return await proc.exited; + } catch (error) { + stderr(`nice: '${parsed.command[0]}': ${commandSpawnErrorMessage(error)}\n`); + return error.code === "ENOENT" ? 127 : 126; + } +} + +export function parseNiceArgs(args) { + let adjusted = false; + let adjustment = 10; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return { command: args.slice(i + 1), adjusted, adjustment }; + if (arg === "-n" || arg === "--adjustment") { + if (i + 1 >= args.length) { + return { + error: arg.startsWith("--") ? `option '${arg}' requires an argument` : `option requires an argument -- '${arg.slice(1)}'`, + showHelp: true, + command: [], + adjusted, + }; + } + const value = args[++i]; + if (!isNiceAdjustment(value)) return { error: `invalid adjustment ${localeQuotedEscapedDiagnostic(value ?? "")}`, command: [], adjusted }; + adjusted = true; + adjustment = Number(value); + continue; + } + if (arg.startsWith("--adjustment=")) { + const value = arg.slice("--adjustment=".length); + if (!isNiceAdjustment(value)) return { error: `invalid adjustment ${localeQuotedEscapedDiagnostic(value)}`, command: [], adjusted }; + adjusted = true; + adjustment = Number(value); + continue; + } + if (/^-n/.test(arg) && arg !== "-n") { + const value = arg.slice(2); + if (!isNiceAdjustment(value)) return { error: `invalid adjustment ${localeQuotedEscapedDiagnostic(value)}`, command: [], adjusted }; + adjusted = true; + adjustment = Number(value); + continue; + } + if (/^-[+-]?\d+$/.test(arg)) { + adjusted = true; + adjustment = Number(arg.slice(1)); + continue; + } + if (arg.startsWith("-")) return { error: invalidOptionMessage(arg), showHelp: true, command: [], adjusted }; + return { command: args.slice(i), adjusted, adjustment }; + } + return { command: [], adjusted, adjustment }; +} + +export function isNiceAdjustment(value) { + return /^[+-]?\d+$/.test(String(value)); +} + +export function currentNiceValue() { + return libc.symbols.getpriority(0, 0); +} + +export function adjustedNiceValue(adjustment) { + const current = currentNiceValue(); + const boundedAdjustment = Math.max(-39, Math.min(39, adjustment)); + return Math.max(-20, Math.min(19, current + boundedAdjustment)); +} + +export function writeNiceWarning() { + try { + writeSync(2, "nice: cannot set niceness: Permission denied\n"); + return true; + } catch { + return false; + } +} + +const singleCall = defineCommand("nice", niceCmd, niceMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/nl.js b/src/commands/nl.js new file mode 100644 index 0000000..0a934d6 --- /dev/null +++ b/src/commands/nl.js @@ -0,0 +1,299 @@ +#!/usr/bin/env bun + +import { decodeSurrogateEscapedBytes, localeQuotedEscapedDiagnostic, parseOptions, readAll, readStdinRecords, systemErrorMessage, textInputDiagnosticName } from "../shared/common.js"; +import { InvocationError, UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { tacRegexPattern } from "../shared/text.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const NL_LONG_OPTIONS = ["body-numbering", "section-delimiter", "footer-numbering", "header-numbering", "line-increment", "join-blank-lines", "no-renumber", "number-separator", "number-width", "number-format", "starting-line-number", "help", "version"]; + +export function nlMetaOption(args) { + const longValueOptions = new Set(["body-numbering", "section-delimiter", "footer-numbering", "header-numbering", "line-increment", "join-blank-lines", "number-format", "number-separator", "starting-line-number", "number-width"]); + const longNoValueOptions = new Set(["no-renumber"]); + const diagnostics = []; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (arg.startsWith("--")) { + let option; + try { + option = normalizeNlLongOption(arg); + } catch (error) { + if (error instanceof UsageError) { + diagnostics.push(`nl: ${error.message}\n`); + continue; + } + throw error; + } + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (!NL_LONG_OPTIONS.includes(name)) { + diagnostics.push(`nl: unrecognized option '${arg}'\n`); + continue; + } + if (name === "help" || name === "version") { + if (inlineValue == null) { + for (const line of diagnostics) stderr(line); + return `--${name}`; + } + diagnostics.push(`nl: option '--${name}' doesn't allow an argument\n`); + continue; + } + if (longValueOptions.has(name)) { + const value = inlineValue ?? args[i + 1]; + if (value !== undefined) validateNlMetaOptionValue(name, value, diagnostics); + } + if (inlineValue == null && longValueOptions.has(name)) { + i++; + continue; + } + if (inlineValue != null) { + if (longNoValueOptions.has(name)) diagnostics.push(`nl: option '--${name}' doesn't allow an argument\n`); + else if (!longValueOptions.has(name)) return null; + } + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + const valueOption = arg.slice(1).match(/[bdfhilnsvw]/); + if (valueOption) { + const ch = valueOption[0]; + const inlineValue = arg.slice(arg.indexOf(ch) + 1); + const value = inlineValue === "" ? args[i + 1] : inlineValue; + validateNlMetaShortValue(ch, value, diagnostics); + if (inlineValue === "") i++; + continue; + } + for (const ch of arg.slice(1)) { + if (ch !== "p") { + diagnostics.push(`nl: invalid option -- '${ch}'\n`); + break; + } + } + } + return null; +} + +export function validateNlMetaOptionValue(name, value, diagnostics) { + if ((name === "body-numbering" || name === "header-numbering" || name === "footer-numbering") && !nlNumberingStyleValid(value)) { + diagnostics.push(`nl: invalid ${name.split("-", 1)[0]} numbering style: ${localeQuotedEscapedDiagnostic(value)}\n`); + } else if (name === "number-format" && !nlNumberFormatValid(value)) { + diagnostics.push(`nl: invalid line numbering format: ${localeQuotedEscapedDiagnostic(value)}\n`); + } else if (name === "line-increment") parseNlBigInt(value, "invalid line number increment"); + else if (name === "starting-line-number") parseNlBigInt(value, "invalid starting line number"); + else if (name === "join-blank-lines") parseNlJoinBlank(value); + else if (name === "number-width") parseNlWidth(value); +} + +export function validateNlMetaShortValue(ch, value, diagnostics) { + if (value === undefined) return; + const names = { b: "body-numbering", f: "footer-numbering", h: "header-numbering", i: "line-increment", l: "join-blank-lines", n: "number-format", v: "starting-line-number", w: "number-width" }; + const name = names[ch]; + if (name) validateNlMetaOptionValue(name, value, diagnostics); +} + +export function normalizeNlLongOptions(args, reportAmbiguous = true) { + const out = []; + for (const arg of args) { + if (arg === "--") { + out.push(arg); + continue; + } + if (!arg.startsWith("--") || arg === "--") { + out.push(arg); + continue; + } + out.push(normalizeNlLongOption(arg, reportAmbiguous)); + } + return out; +} + +export function normalizeNlLongOption(arg, reportAmbiguous = true) { + const body = arg.slice(2); + const eq = body.indexOf("="); + const name = eq === -1 ? body : body.slice(0, eq); + const matches = NL_LONG_OPTIONS.filter((option) => option.startsWith(name)); + if (matches.length === 1) return eq === -1 ? `--${matches[0]}` : `--${matches[0]}=${body.slice(eq + 1)}`; + if (matches.length > 1 && reportAmbiguous) { + throw new UsageError(`option '${arg}' is ambiguous; possibilities: ${matches.map((option) => `'--${option}'`).join(" ")}`, true); + } + return arg; +} + +export async function nlCmd(args) { + args = normalizeNlLongOptions(args); + const { opts, operands } = parseOptions(args, { short: { b: "value", d: "value", f: "value", h: "value", i: "value", l: "value", n: "value", p: false, s: "value", v: "value", w: "value" }, long: { "body-numbering": "value", "section-delimiter": "value", "footer-numbering": "value", "header-numbering": "value", "line-increment": "value", "join-blank-lines": "value", "number-format": "value", "no-renumber": false, "number-separator": "value", "starting-line-number": "value", "number-width": "value", help: false, version: false } }); + const files = operands.length ? operands : ["-"]; + const body = opts.b ?? opts["body-numbering"] ?? "t"; + const header = opts.h ?? opts["header-numbering"] ?? "n"; + const footer = opts.f ?? opts["footer-numbering"] ?? "n"; + const format = opts.n ?? opts["number-format"] ?? "rn"; + validateNlNumberingStyle(body, "body"); + validateNlNumberingStyle(header, "header"); + validateNlNumberingStyle(footer, "footer"); + const bodyPattern = nlNumberingPattern(body); + const headerPattern = nlNumberingPattern(header); + const footerPattern = nlNumberingPattern(footer); + validateNlNumberFormat(format); + const width = parseNlWidth(opts.w ?? opts["number-width"] ?? "6"); + const sep = opts.s ?? opts["number-separator"] ?? "\t"; + const increment = parseNlBigInt(opts.i ?? opts["line-increment"] ?? "1", "invalid line number increment"); + const joinBlank = parseNlJoinBlank(opts.l ?? opts["join-blank-lines"] ?? 1); + const start = parseNlBigInt(opts.v ?? opts["starting-line-number"] ?? "1", "invalid starting line number"); + const delimiter = normalizeNlDelimiter(opts.d ?? opts["section-delimiter"] ?? "\\:"); + let number = start; + let blankRun = 0; + let out = ""; + let status = 0; + let currentSection = "body"; + const renderLine = (raw) => { + const line = raw.replace(/\n$/, ""); + const section = nlSection(line, delimiter); + if (section) { + if (!(opts.p || opts["no-renumber"])) number = start; + currentSection = section; + blankRun = 0; + return "\n"; + } + const blank = raw === "\n"; + blankRun = blank ? blankRun + 1 : 0; + const style = currentSection === "header" ? header : currentSection === "footer" ? footer : body; + const pattern = currentSection === "header" ? headerPattern : currentSection === "footer" ? footerPattern : bodyPattern; + const shouldNumber = shouldNumberNl(line, style, blank, blankRun, joinBlank, pattern); + if (shouldNumber) { + if (number > NlMax || number < NlMin) throw new InvocationError("line number overflow", 1, false); + const rendered = `${formatNlNumber(number, width, format)}${sep}${raw}`; + number += increment; + return rendered; + } + return `${" ".repeat(width + sep.length)}${raw}`; + }; + if (files.length === 1 && files[0] === "-") { + readStdinRecords("\n", (line) => stdout(renderLine(`${line}\n`))); + return 0; + } + for (const file of files) { + let data; + try { + data = decodeSurrogateEscapedBytes(await readAll(file)); + } catch (error) { + stderr(`nl: ${textInputDiagnosticName(file)}: ${systemErrorMessage(error)}\n`); + status = 1; + continue; + } + const records = splitNlRecords(data); + for (const raw of records) { + out += renderLine(raw); + } + } + stdout(out); + return status; +} + +export const NlMax = 9223372036854775807n; + +export const NlMin = -9223372036854775808n; + +export function validateNlNumberingStyle(style, section) { + if (nlNumberingStyleValid(style)) return; + throw new UsageError(`invalid ${section} numbering style: ${localeQuotedEscapedDiagnostic(style)}`, true); +} + +export function nlNumberingStyleValid(style) { + return style === "a" || style === "t" || style === "n" || String(style).startsWith("p"); +} + +export function validateNlNumberFormat(format) { + if (nlNumberFormatValid(format)) return; + throw new UsageError(`invalid line numbering format: ${localeQuotedEscapedDiagnostic(format)}`, true); +} + +export function nlNumberFormatValid(format) { + return format === "ln" || format === "rn" || format === "rz"; +} + +export function nlNumberingPattern(style) { + if (!String(style).startsWith("p")) return null; + try { + return new RegExp(tacRegexPattern(String(style).slice(1))); + } catch { + throw new UsageError("Invalid regular expression"); + } +} + +export function parseNlJoinBlank(value) { + const text = String(value); + if (/^-\d+$/.test(text)) throw new UsageError(`invalid line number of blank lines: ${localeQuotedEscapedDiagnostic(text)}: Numerical result out of range`); + if (!/^\+?\d+$/.test(text)) throw new UsageError(`invalid line number of blank lines: ${localeQuotedEscapedDiagnostic(text)}`); + const n = BigInt(text); + if (n > NlMax) throw new UsageError(`invalid line number of blank lines: ${localeQuotedEscapedDiagnostic(text)}: Value too large for defined data type`); + return Number(n); +} + +export function parseNlBigInt(value, message) { + const text = String(value); + if (!/^[+-]?\d+$/.test(text)) throw new UsageError(`${message}: ${localeQuotedEscapedDiagnostic(text)}`); + const n = BigInt(text); + if (n > NlMax || n < NlMin) throw new UsageError(`${message}: ${localeQuotedEscapedDiagnostic(text)}: Value too large for defined data type`); + return n; +} + +export function parseNlWidth(value) { + const text = String(value); + if (!/^[+-]?\d+$/.test(text)) throw new UsageError(`invalid line number field width: ${localeQuotedEscapedDiagnostic(text)}`); + const width = BigInt(text); + if (width <= 0n) throw new UsageError(`invalid line number field width: ${localeQuotedEscapedDiagnostic(text)}: Numerical result out of range`); + if (width > 2147483647n) throw new UsageError(`invalid line number field width: ${localeQuotedEscapedDiagnostic(text)}: Value too large for defined data type`); + return Number(width); +} + +export function normalizeNlDelimiter(value) { + if (value === "") return ""; + return value.length === 1 ? `${value}:` : value; +} + +export function nlSection(line, delimiter) { + if (!delimiter) return null; + if (nlDelimiterEquals(line, `${delimiter}${delimiter}${delimiter}`)) return "header"; + if (nlDelimiterEquals(line, `${delimiter}${delimiter}`)) return "body"; + if (nlDelimiterEquals(line, delimiter)) return "footer"; + return null; +} + +export function nlDelimiterEquals(line, marker) { + if (line.length !== marker.length) return false; + for (let i = 0; i < marker.length; i++) { + if (line[i] === marker[i]) continue; + const markerCode = marker.charCodeAt(i); + const lineCode = line.charCodeAt(i); + if (markerCode === 0xfffd && lineCode >= 0xdc80 && lineCode <= 0xdcff) continue; + return false; + } + return true; +} + +export function splitNlRecords(data) { + if (data === "") return []; + const records = data.split(/(?<=\n)/).filter((raw) => raw !== ""); + const last = records[records.length - 1]; + if (last && !last.endsWith("\n")) records[records.length - 1] = `${last}\n`; + return records; +} + +export function shouldNumberNl(line, style, blank, blankRun, joinBlank, pattern = null) { + if (style === "n") return false; + if (style === "a") return !blank || blankRun % joinBlank === 0; + if (style === "t") return !blank; + if (style.startsWith("p")) return pattern.test(line); + return !blank; +} + +export function formatNlNumber(number, width, format) { + const text = String(number); + if (format === "ln") return text.padEnd(width); + if (format === "rz") return text.padStart(width, "0"); + return text.padStart(width); +} + +const singleCall = defineCommand("nl", nlCmd, nlMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/nohup.js b/src/commands/nohup.js new file mode 100644 index 0000000..3e45e7e --- /dev/null +++ b/src/commands/nohup.js @@ -0,0 +1,103 @@ +#!/usr/bin/env bun + +import { closeSync, constants as fsConstants, openSync, writeSync } from "node:fs"; +import { join } from "node:path"; +import { invalidOptionMessage, libc, localeQuotedEscapedDiagnostic, normalizeHelpVersionOnlyLongOption, resolveEnvCommand, systemErrorMessage } from "../shared/common.js"; +import { InvocationError, stderr } from "../shared/diagnostics.js"; +import { SIG_IGN, commandSpawnErrorMessage } from "../shared/process.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export function nohupMetaOption(args) { + if (!args.length) return null; + const normalized = args[0].startsWith("--") ? normalizeHelpVersionOnlyLongOption(args[0]) : args[0]; + const [name, inlineValue] = normalized.startsWith("--") ? normalized.slice(2).split(/=(.*)/s, 2) : [null, undefined]; + if (name === "help" || name === "version") { + if (inlineValue !== undefined) throw new InvocationError(`option '--${name}' doesn't allow an argument`); + return normalized; + } + return null; +} + +export async function nohupCmd(args) { + const internalFailure = process.env.POSIXLY_CORRECT ? 127 : 125; + if (args[0] === "--") args = args.slice(1); + if (!args.length) throw new InvocationError("missing operand", internalFailure); + if (args[0].startsWith("-")) throw new InvocationError(invalidOptionMessage(args[0])); + const ignoringInput = Boolean(process.stdin.isTTY); + const redirectingStdout = Boolean(process.stdout.isTTY); + const redirectingStderr = Boolean(process.stderr.isTTY); + let stdinFd = null; + let outputFd = null; + let outputFile = null; + try { + if (ignoringInput) stdinFd = openSync("/dev/null", "w"); + if (redirectingStdout) { + const opened = nohupOpenOutput(); + if (!opened) return internalFailure; + ({ fd: outputFd, file: outputFile } = opened); + } + const advisory = outputFile + ? `${ignoringInput ? "ignoring input and " : ""}appending output to ${localeQuotedEscapedDiagnostic(outputFile)}` + : ignoringInput && !redirectingStderr + ? "ignoring input" + : ignoringInput && redirectingStderr + ? "ignoring input and redirecting standard error to standard output" + : redirectingStderr + ? "redirecting standard error to standard output" + : null; + if (advisory != null && !nohupWriteAdvisory(advisory)) return internalFailure; + await resolveEnvCommand(args[0], process.env, process.cwd()); + const previousHangup = libc.symbols.signal(1, SIG_IGN); + let proc; + try { + proc = Bun.spawn(["/bin/sh", "-c", 'trap "" HUP\nexec "$@"', "nohup", ...args], { + stdin: stdinFd ?? "inherit", + stdout: outputFd ?? "inherit", + stderr: redirectingStderr ? (outputFd ?? 1) : "inherit", + }); + return await proc.exited; + } finally { + libc.symbols.signal(1, previousHangup); + } + } catch (error) { + stderr(`nohup: failed to run command '${args[0]}': ${commandSpawnErrorMessage(error)}\n`); + return error.code === "ENOENT" ? 127 : 126; + } finally { + if (stdinFd != null) closeSync(stdinFd); + if (outputFd != null) closeSync(outputFd); + } +} + +export function nohupOpenOutput() { + const candidates = ["nohup.out"]; + if (process.env.HOME) candidates.push(join(process.env.HOME, "nohup.out")); + const failures = []; + const previousUmask = process.umask(0); + try { + for (const file of candidates) { + try { + return { fd: openSync(file, fsConstants.O_CREAT | fsConstants.O_WRONLY | fsConstants.O_APPEND, 0o600), file }; + } catch (error) { + failures.push({ file, error }); + } + } + } finally { + process.umask(previousUmask); + } + for (const { file, error } of failures) stderr(`nohup: failed to open ${localeQuotedEscapedDiagnostic(file)}: ${systemErrorMessage(error)}\n`); + return null; +} + +export function nohupWriteAdvisory(message) { + try { + writeSync(2, `nohup: ${message}\n`); + return true; + } catch { + return false; + } +} + +const singleCall = defineCommand("nohup", nohupCmd, nohupMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/nproc.js b/src/commands/nproc.js new file mode 100644 index 0000000..bd9996b --- /dev/null +++ b/src/commands/nproc.js @@ -0,0 +1,155 @@ +#!/usr/bin/env bun + +import { readFileSync } from "node:fs"; +import { availableParallelism, cpus } from "node:os"; +import { join } from "node:path"; +import { libc, localeQuotedEscapedDiagnostic, parseOptions } from "../shared/common.js"; +import { UsageError, stdout } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const NPROC_LONG_OPTIONS = ["all", "ignore", "help", "version"]; + +export function nprocMetaOption(args) { + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (!arg.startsWith("--")) { + if (arg.startsWith("-") && arg !== "-") return null; + continue; + } + const option = normalizeNprocLongOption(arg); + const name = option.slice(2).split("=", 1)[0]; + if (!NPROC_LONG_OPTIONS.includes(name)) return null; + if (option.includes("=")) { + if (name === "all" || name === "help" || name === "version") return null; + if (name === "ignore") parseNprocIgnore(option.slice(option.indexOf("=") + 1)); + continue; + } + if (name === "help" || name === "version") return option; + if (name === "ignore") i++; + } + return null; +} + +export async function nprocCmd(args) { + args = normalizeNprocLongOptions(args); + const { opts, operands } = parseOptions(args, { long: { all: false, ignore: "value", help: false, version: false } }); + if (operands.length) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[0])}`, true); + const installed = BigInt(cpus().length); + const available = BigInt(Math.max(1, availableParallelism())); + const openMp = opts.all ? { threads: null, limit: null } : nprocOpenMpLimits(); + const quotaLimit = opts.all || openMp.threads != null ? null : nprocCgroupQuotaLimit(); + let use; + if (opts.all) use = installed; + else if (openMp.threads != null) use = openMp.limit != null && openMp.limit < openMp.threads ? openMp.limit : openMp.threads; + else { + use = quotaLimit != null && quotaLimit < available ? quotaLimit : available; + if (openMp.limit != null && openMp.limit < use) use = openMp.limit; + } + const ignore = opts.ignore == null ? 0n : parseNprocIgnore(opts.ignore); + const result = use > ignore ? use - ignore : 1n; + stdout(`${result < 1n ? 1n : result}\n`); + return 0; +} + +export function nprocCgroupQuotaLimit() { + if (!nprocSchedulerUsesQuota()) return null; + const testRoot = process.env.BNU_NPROC_TEST_ROOT; + const procCgroup = testRoot ? join(testRoot, "proc/self/cgroup") : "/proc/self/cgroup"; + const cgroupRoot = testRoot ? join(testRoot, "sys/fs/cgroup") : "/sys/fs/cgroup"; + let membership; + try { + membership = readFileSync(procCgroup, "utf8"); + } catch { + return null; + } + const unified = membership.split(/\r?\n/).find((line) => line.startsWith("0::")); + if (!unified) return null; + const components = unified.slice(3).split("/").filter((part) => part && part !== "." && part !== ".."); + let limit = null; + for (let length = components.length; length >= 0; length--) { + let cpuMax; + try { + cpuMax = readFileSync(join(cgroupRoot, ...components.slice(0, length), "cpu.max"), "utf8").trim(); + } catch { + continue; + } + const [quotaText, periodText] = cpuMax.split(/\s+/); + if (quotaText === "max" || !/^\d+$/.test(quotaText) || !/^\d+$/.test(periodText)) continue; + const quota = BigInt(quotaText); + const period = BigInt(periodText); + if (quota <= 0n || period <= 0n) continue; + const rounded = (2n * quota + period) / (2n * period); + const candidate = rounded < 1n ? 1n : rounded; + if (limit == null || candidate < limit) limit = candidate; + if (limit === 1n) break; + } + return limit; +} + +export function nprocSchedulerUsesQuota() { + const testRoot = process.env.BNU_NPROC_TEST_ROOT; + const schedPath = testRoot ? join(testRoot, "proc/self/sched") : "/proc/self/sched"; + try { + const sched = readFileSync(schedPath, "utf8"); + const match = sched.match(/^policy\s*:\s*(-?\d+)/m); + if (match) return ![-1, 1, 2, 6].includes(Number(match[1])); + } catch { + // Fall through to libc on systems without Linux procfs. + } + const policy = libc.symbols.sched_getscheduler(0); + return ![-1, 1, 2, 6].includes(policy); +} + +export function normalizeNprocLongOptions(args) { + const out = []; + let end = false; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (end || arg === "--" || !arg.startsWith("--")) { + out.push(arg); + if (arg === "--") end = true; + continue; + } + const option = normalizeNprocLongOption(arg); + out.push(option); + if (option === "--ignore" && i + 1 < args.length) out.push(args[++i]); + } + return out; +} + +export function normalizeNprocLongOption(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 = NPROC_LONG_OPTIONS.find((option) => option.startsWith(name)); + if (!match) return arg; + return eq === -1 ? `--${match}` : `--${match}=${body.slice(eq + 1)}`; +} + +export function parseNprocIgnore(value) { + const text = String(value).trimStart(); + if (!/^\+?\d+$/.test(text)) throw new UsageError(`invalid number: ${localeQuotedEscapedDiagnostic(value)}`); + return BigInt(text); +} + +export function nprocOpenMpLimits() { + return { + threads: openMpPositiveIntegerPrefix(process.env.OMP_NUM_THREADS), + limit: openMpPositiveIntegerPrefix(process.env.OMP_THREAD_LIMIT), + }; +} + +export function openMpPositiveIntegerPrefix(value) { + const match = String(value ?? "").trim().match(/^(\d+)(?:,|$)/); + if (!match) return null; + const parsed = BigInt(match[1]); + if (parsed <= 0n) return null; + return parsed > 18446744073709551615n ? 18446744073709551615n : parsed; +} + +const singleCall = defineCommand("nproc", nprocCmd, nprocMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/numfmt.js b/src/commands/numfmt.js new file mode 100644 index 0000000..583c267 --- /dev/null +++ b/src/commands/numfmt.js @@ -0,0 +1,665 @@ +#!/usr/bin/env bun + +import { readFileSync } from "node:fs"; +import { decodeSurrogateEscapedBytes, enc, gb18030Units, inRanges, isGb18030Locale, localeQuotedDiagnostic, localeQuotedEscapedDiagnostic, lsEscapedName, normalizeLongOptionByPrefix, parseOptions, readAll, readStdinByteRecords, systemErrorMessage } from "../shared/common.js"; +import { InvocationError, UsageError, fail, stderr, stdout } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const NUMFMT_LONG_OPTIONS = ["debug", "delimiter", "from", "from-unit", "field", "format", "grouping", "header", "invalid", "padding", "round", "suffix", "to", "to-unit", "unit-separator", "zero-terminated", "help", "version"]; + +export function numfmtMetaOption(args) { + const longValueOptions = new Set(["delimiter", "from", "from-unit", "field", "format", "invalid", "padding", "round", "suffix", "to", "to-unit", "unit-separator"]); + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (arg.startsWith("--")) { + const option = normalizeNumfmtLongOption(arg); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (!NUMFMT_LONG_OPTIONS.includes(name) && name !== "devdebug") return null; + if ((option === "--help" || option === "--version") && inlineValue == null) return option; + const value = inlineValue ?? (longValueOptions.has(name) ? args[i + 1] : undefined); + if (value !== undefined) validateNumfmtMetaOptionValue(name, value); + if (inlineValue == null && longValueOptions.has(name)) i++; + else if (inlineValue != null && !longValueOptions.has(name)) return null; + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (!["d", "z"].includes(ch)) return null; + if (ch === "d") { + const inlineValue = arg.slice(j + 1); + validateNumfmtMetaOptionValue("delimiter", inlineValue === "" ? args[i + 1] : inlineValue); + if (inlineValue === "") i++; + break; + } + } + } + return null; +} + +export function parseHumanNumberParts(value, mode = "auto", suffix = "", unitSeparator = "") { + let text = String(value); + if (suffix && text.endsWith(suffix)) text = text.slice(0, -suffix.length); + text = text.trimEnd(); + if (unitSeparator) { + const unitMatch = text.match(/([kKMGTPEZYRQ]i?B?)$/); + if (unitMatch) { + const beforeUnit = text.slice(0, -unitMatch[1].length); + if (beforeUnit.endsWith(unitSeparator) && !beforeUnit.slice(0, -unitSeparator.length).endsWith(unitSeparator)) text = beforeUnit.slice(0, -unitSeparator.length) + unitMatch[1]; + } + } + const match = text.match(/^([+-]?(?:\d+(?:\.\d+)?|\.\d+))([kKMGTPEZYRQ]?)(i?B?)$/); + if (!match) throw new UsageError(numfmtNumberError(text, { fromMode: mode })); + if (mode === "iec-i" && match[2] && !match[3].startsWith("i")) throw new UsageError(`missing 'i' suffix in input: ${localeQuotedDiagnostic(text)} (e.g Ki/Mi/Gi)`); + if (match[3] && !match[2]) throw new UsageError(numfmtNumberError(text, { fromMode: mode })); + if (match[3].includes("B")) throw new UsageError(`invalid suffix in input ${localeQuotedDiagnostic(numfmtDiagnosticValue(text))}: ${localeQuotedDiagnostic(numfmtDiagnosticValue(numfmtInvalidHumanSuffixPart(match[3], mode)))}`); + if ((mode === "si" || mode === "iec") && match[3].startsWith("i")) throw new UsageError(`invalid suffix in input ${localeQuotedDiagnostic(numfmtDiagnosticValue(text))}: ${localeQuotedDiagnostic(numfmtDiagnosticValue(match[3]))}`); + if (mode === "si" && match[2] && !["K", "k"].includes(match[2]) && match[2] !== match[2].toUpperCase()) throw new UsageError(numfmtNumberError(text, { fromMode: mode })); + const base = mode === "iec" || mode === "iec-i" || match[3]?.toLowerCase().startsWith("i") ? 1024 : 1000; + const exp = match[2] ? "KMGTPEZYRQ".indexOf(match[2].toUpperCase()) + 1 : 0; + const decimals = match[1].includes(".") ? match[1].split(".")[1].length : 0; + return { value: Number(match[1]) * (exp ? base ** exp : 1), decimals: exp ? 0 : decimals }; +} + +export function numfmtInvalidHumanSuffixPart(suffix, mode) { + if (suffix === "iB" && (mode === "auto" || mode === "iec-i")) return "B"; + return suffix; +} + +export async function numfmtCmd(args) { + args = normalizeNumfmtArgs(args); + if (countNumfmtFieldOptions(args) > 1) throw new UsageError("multiple field specifications"); + const parsedOptions = parseOptions(args, { + short: { d: "value", z: false }, + long: { + from: "value", to: "value", field: "value", header: "optional-value", invalid: "value", padding: "value", suffix: "value", delimiter: "value", "zero-terminated": false, + "from-unit": "value", "to-unit": "value", round: "value", grouping: false, format: "value", "unit-separator": "value", "unit-sep": "value", debug: false, devdebug: false, help: false, version: false, + }, + }); + const { opts, operands } = parsedOptions; + validateNumfmtOptions(opts); + if (operands.length) { + if (opts.header != null && opts.debug) stderr("numfmt: --header ignored with command-line input\n"); + const rawResult = formatRawNumfmtOperands(opts, operands); + if (rawResult != null) return rawResult; + const sep = opts.z || opts["zero-terminated"] ? "\0" : "\n"; + const result = formatNumfmtRecords(numfmtCommandLineRecords(opts, operands), opts, sep, true); + stdout(result.text + (result.error ? "" : sep)); + if (result.error) throw new InvocationError(result.error, 2, false); + return result.status; + } + const sep = opts.z || opts["zero-terminated"] ? "\0" : "\n"; + if (opts.header == null) { + let status = 0; + let abortError = null; + try { + readStdinByteRecords(sep === "\0" ? 0 : 10, (bytes, hasSep) => { + if (abortError) return; + const record = decodeSurrogateEscapedBytes(bytes); + const result = formatNumfmtRecords([record], opts, sep, true); + stdout(result.text + (hasSep && !result.error ? sep : "")); + if (result.error) abortError = result.error; + status = Math.max(status, result.status); + }); + } catch (error) { + return fail("numfmt", `error reading input: ${systemErrorMessage(error)}`); + } + if (abortError) throw new InvocationError(abortError, 2, false); + return status; + } + let input; + try { + input = decodeSurrogateEscapedBytes(await readAll("-")); + } catch (error) { + return fail("numfmt", `error reading input: ${systemErrorMessage(error)}`); + } + const terminated = input.endsWith(sep); + const records = terminated ? input.slice(0, -sep.length).split(sep) : input === "" ? [] : input.split(sep); + const header = opts.header === true ? 1 : Number(opts.header ?? 0); + const result = formatNumfmtRecords(records, opts, sep, terminated, header); + stdout(result.text + (records.length && terminated && !result.error ? sep : "")); + if (result.error) throw new InvocationError(result.error, 2, false); + return result.status; +} + +export function numfmtCommandLineRecords(opts, operands) { + if ((opts.d ?? opts.delimiter) != null) return operands; + return operands.map((operand) => String(operand).replace(/[\t\n\r\f\v]+/g, " ")); +} + +export function formatRawNumfmtOperands(opts, operands) { + const delimiter = opts.d ?? opts.delimiter; + if (delimiter == null || delimiter === "" || isAsciiString(delimiter)) return null; + const raw = rawNumfmtArgsFromProc(); + if (!raw) return null; + const parsed = parseRawNumfmtArgs(raw); + if (!parsed?.delimiter || parsed.operands.length !== operands.length) return null; + const sep = opts.z || opts["zero-terminated"] ? Uint8Array.of(0) : Uint8Array.of(10); + const output = []; + let status = 0; + let abortError = null; + for (const record of parsed.operands) { + const result = formatRawNumfmtRecord(record, parsed.delimiter, opts); + output.push(result.bytes); + if (!result.ok) { + const invalid = opts.invalid ?? "abort"; + if (invalid === "fail" || invalid === "warn") { + status = invalid === "warn" ? 0 : 2; + stderr(`numfmt: ${result.error}\n`); + } else if (invalid === "abort") { + abortError = result.error; + break; + } + } + } + stdout(joinBytesWithSeparator(output, sep, abortError == null ? sep : null)); + if (abortError) throw new InvocationError(abortError, 2, false); + return status; +} + +export function isAsciiString(value) { + return /^[\x00-\x7f]*$/.test(String(value)); +} + +export function rawNumfmtArgsFromProc() { + try { + const parts = splitBytesOnByte(readFileSync("/proc/self/cmdline"), 0); + if (parts.at(-1)?.length === 0) parts.pop(); + let commandIndex = -1; + for (let i = 0; i < parts.length; i++) { + if (new TextDecoder().decode(parts[i]) === "numfmt") commandIndex = i; + } + return commandIndex === -1 ? null : parts.slice(commandIndex + 1); + } catch { + return null; + } +} + +export function parseRawNumfmtArgs(args) { + const operands = []; + let delimiter = null; + const valueOptions = new Set(["--debug", "--delimiter", "--field", "--format", "--from", "--from-unit", "--header", "--invalid", "--padding", "--round", "--suffix", "--to", "--to-unit", "--unit-separator", "--unit-sep"]); + for (let i = 0; i < args.length; i++) { + const text = new TextDecoder().decode(args[i]); + if (text === "--") { + operands.push(...args.slice(i + 1)); + break; + } + if (text === "-d" || text === "--delimiter") { + delimiter = args[++i]; + continue; + } + if (text.startsWith("-d") && text.length > 2) { + delimiter = args[i].slice(2); + continue; + } + if (text.startsWith("--delimiter=")) { + delimiter = args[i].slice("--delimiter=".length); + continue; + } + if (text.startsWith("--") && text.includes("=")) continue; + if (valueOptions.has(text)) { + i++; + continue; + } + if (text.startsWith("-") && text !== "-") continue; + operands.push(args[i]); + } + return { delimiter, operands }; +} + +export function formatRawNumfmtRecord(record, delimiter, opts) { + const fields = splitBytesByNeedle(record, delimiter); + const selected = readNumfmtFieldRanges(opts.field ?? "1"); + const outputFields = []; + let ok = true; + let error; + for (let i = 0; i < fields.length; i++) { + if (!inRanges(i + 1, selected)) { + outputFields.push(fields[i]); + continue; + } + const text = new TextDecoder().decode(fields[i]); + const result = formatNumfmtValue(text, opts); + ok &&= result.ok; + if (!result.ok && error == null) error = result.error; + outputFields.push(result.ok ? enc.encode(result.text) : fields[i]); + if (!result.ok && (opts.invalid ?? "abort") === "abort") break; + } + return { ok, error, bytes: joinBytesWithSeparator(outputFields, delimiter) }; +} + +export function splitBytesOnByte(bytes, delimiter) { + const parts = []; + let start = 0; + for (let i = 0; i < bytes.length; i++) { + if (bytes[i] !== delimiter) continue; + parts.push(bytes.slice(start, i)); + start = i + 1; + } + parts.push(bytes.slice(start)); + return parts; +} + +export function splitBytesByNeedle(bytes, needle) { + if (!needle.length) return [bytes]; + const parts = []; + let start = 0; + for (let i = 0; i <= bytes.length - needle.length; i++) { + let matches = true; + for (let j = 0; j < needle.length; j++) { + if (bytes[i + j] !== needle[j]) { + matches = false; + break; + } + } + if (!matches) continue; + parts.push(bytes.slice(start, i)); + i += needle.length - 1; + start = i + 1; + } + parts.push(bytes.slice(start)); + return parts; +} + +export function joinBytesWithSeparator(parts, separator, trailing = null) { + const extra = trailing?.length ?? 0; + const total = parts.reduce((sum, part) => sum + part.length, 0) + separator.length * Math.max(0, parts.length - 1) + extra; + const out = new Uint8Array(total); + let offset = 0; + for (let i = 0; i < parts.length; i++) { + if (i > 0) { + out.set(separator, offset); + offset += separator.length; + } + out.set(parts[i], offset); + offset += parts[i].length; + } + if (trailing) out.set(trailing, offset); + return out; +} + +export function formatNumfmtRecords(records, opts, sep, terminated, header = 0) { + let status = 0; + let abortError = null; + const out = []; + for (let index = 0; index < records.length; index++) { + const record = records[index]; + if (index < header) { + out.push(record); + continue; + } + const result = formatNumfmtRecord(record, opts); + const invalid = opts.invalid ?? "abort"; + if (!result.ok && (invalid === "fail" || invalid === "warn")) { + status = 2; + stderr(`numfmt: ${result.error}\n`); + } + if (!result.ok && invalid === "abort") { + abortError = result.error; + out.push(result.text); + break; + } + out.push(result.text); + } + const text = out.join(sep); + if (status && opts.invalid === "fail" && opts.debug) stderr("numfmt: failed to convert some of the input numbers\n"); + if (opts.invalid === "warn") status = 0; + return { status, text, error: abortError }; +} + +export function countNumfmtFieldOptions(args) { + let count = 0; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") break; + if (arg === "--field") { + count++; + i++; + } else if (arg.startsWith("--field=")) { + count++; + } + } + return count; +} + +export function normalizeNumfmtArgs(args) { + return args.map((arg) => arg === "---debug" || arg === "--devdebug" ? "--devdebug" : normalizeNumfmtLongOption(arg)); +} + +export function normalizeNumfmtLongOption(arg) { + if (!arg.startsWith("--") || arg === "--") return arg; + return normalizeLongOptionByPrefix(arg, NUMFMT_LONG_OPTIONS); +} + +export function validateNumfmtOptions(opts) { + opts.from = parseNumfmtEnumOption(opts.from, "--from", ["none", "auto", "si", "iec", "iec-i"]); + opts.to = parseNumfmtEnumOption(opts.to, "--to", ["none", "si", "iec", "iec-i"]); + if (opts.grouping && opts.to) throw new UsageError("grouping cannot be combined with --to"); + const format = opts.format == null ? null : parseNumfmtFormat(opts.format); + if ((opts.grouping || format?.grouping) && opts.to) throw new UsageError("grouping cannot be combined with --to"); + if (opts.grouping && opts.format) throw new UsageError("--grouping cannot be combined with --format"); + if (opts.grouping && opts.debug) stderr("numfmt: grouping has no effect in this locale\n"); + if (format?.grouping && opts.debug) stderr("numfmt: grouping has no effect in this locale\n"); + if (opts.debug && !opts.from && !opts.to && !opts["from-unit"] && !opts["to-unit"] && !opts.format && !opts.padding && !opts.suffix && !opts.header) stderr("numfmt: no conversion option specified\n"); + opts.invalid = parseNumfmtEnumOption(opts.invalid, "--invalid", ["abort", "fail", "warn", "ignore"]); + opts.round = parseNumfmtEnumOption(opts.round, "--round", ["up", "down", "from-zero", "towards-zero", "nearest"]); + if (opts.padding != null && (!/^[+-]?\d+$/.test(String(opts.padding)) || Number(opts.padding) === 0)) throw new UsageError(`invalid padding value ${localeQuotedDiagnostic(opts.padding)}`); + const rawDelimiter = rawNumfmtDelimiter(); + if (opts.delimiter != null && opts.delimiter !== "" && !isSingleNumfmtDelimiter(opts.delimiter, rawDelimiter)) throw new UsageError("the delimiter must be a single character"); + if (opts.d != null && opts.d !== "" && !isSingleNumfmtDelimiter(opts.d, rawDelimiter)) throw new UsageError("the delimiter must be a single character"); + if (opts.header != null && opts.header !== true && (!/^\+?\d+$/.test(String(opts.header)) || Number(opts.header) < 1)) throw new UsageError(`invalid header value ${localeQuotedDiagnostic(opts.header)}`); + if (opts.field != null) validateNumfmtFieldSpec(String(opts.field)); + if (opts["from-unit"] != null) parseNumfmtUnit(opts["from-unit"]); + if (opts["to-unit"] != null) parseNumfmtUnit(opts["to-unit"]); + if (opts.format != null) parseNumfmtFormat(opts.format); +} + +export function validateNumfmtMetaOptionValue(name, value) { + if (value === undefined) return; + if (name === "from") validateNumfmtScaleOption(value, "--from", ["none", "auto", "si", "iec", "iec-i"]); + else if (name === "to") validateNumfmtScaleOption(value, "--to", ["none", "si", "iec", "iec-i"]); + else if (name === "invalid") validateNumfmtScaleOption(value, "--invalid", ["abort", "fail", "warn", "ignore"]); + else if (name === "round") validateNumfmtScaleOption(value, "--round", ["up", "down", "from-zero", "towards-zero", "nearest"]); + else if (name === "field") validateNumfmtFieldSpec(String(value)); + else if (name === "padding" && (!/^[+-]?\d+$/.test(String(value)) || Number(value) === 0)) throw new UsageError(`invalid padding value ${localeQuotedDiagnostic(value)}`); + else if (name === "delimiter" && value !== "" && !isSingleNumfmtDelimiter(value, rawNumfmtDelimiter())) throw new UsageError("the delimiter must be a single character"); + else if (name === "from-unit" || name === "to-unit") parseNumfmtUnit(value); +} + +export function validateNumfmtScaleOption(value, option, valid) { + parseNumfmtEnumOption(value, option, valid); +} + +export function parseNumfmtEnumOption(value, option, valid) { + if (value == null) return undefined; + const text = String(value); + if (valid.includes(text)) return text; + const matches = valid.filter((arg) => arg.startsWith(text)); + if (matches.length === 1 && text !== "") return matches[0]; + const kind = text === "" || matches.length > 1 ? "ambiguous" : "invalid"; + throw new UsageError(numfmtScaleOptionMessage(kind, value, option, valid), true); +} + +export function numfmtScaleOptionMessage(kind, value, option, valid) { + return `${kind} argument ${localeQuotedEscapedDiagnostic(value)} for ${localeQuotedDiagnostic(option)}\nValid arguments are:\n${valid.map((arg) => ` - ${localeQuotedDiagnostic(arg)}`).join("\n")}`; +} + +export function isSingleNumfmtDelimiter(value, rawDelimiter = null) { + const text = String(value); + if (rawDelimiter != null && isGb18030Locale()) return gb18030Units(rawDelimiter).length === 1; + if (text.includes("\ufffd") && rawDelimiter != null) return rawDelimiter.length === 1; + const singleByteLocale = /^(C|POSIX)$/.test(process.env.LC_ALL ?? process.env.LC_CTYPE ?? process.env.LANG ?? ""); + if (singleByteLocale) return enc.encode(text).length === 1; + if ([...text].length === 1) return true; + return text.includes("\ufffd"); +} + +export function rawNumfmtDelimiter() { + const raw = rawNumfmtArgsFromProc(); + if (!raw) return null; + return parseRawNumfmtArgs(raw).delimiter; +} + +export function validateNumfmtFieldSpec(spec) { + if (String(spec).startsWith("--")) throw new UsageError("invalid field range", true); + for (const part of numfmtFieldSpecParts(spec)) { + if (part === "-") continue; + const invalid = part.search(/[^0-9-]/); + if (invalid !== -1) throw new UsageError(`invalid field value ${localeQuotedDiagnostic(part.slice(invalid))}`, true); + if (/^-?[A-Za-z]/.test(part)) throw new UsageError(`invalid field value ${localeQuotedDiagnostic(part.replace(/^-/, ""))}`, true); + } + readNumfmtFieldRanges(spec); +} + +export function numfmtFieldSpecParts(spec) { + const parts = String(spec).split(/[,\s]/); + if (parts.some((part) => part === "")) throw new UsageError("fields are numbered from 1", true); + return parts; +} + +export function readNumfmtFieldRanges(spec) { + const ranges = []; + for (const part of numfmtFieldSpecParts(spec)) { + if (part === "-") { + ranges.push([1, Infinity]); + continue; + } + const match = part.match(/^(\d*)-?(\d*)$/); + if (!match || (!match[1] && !match[2])) throw new UsageError("invalid field range", true); + const start = match[1] ? parseNumfmtFieldNumber(match[1]) : 1; + const end = match[2] ? parseNumfmtFieldNumber(match[2]) : (part.includes("-") ? Infinity : start); + if (start > end) throw new UsageError("invalid decreasing range", true); + if (start <= 0 || end <= 0) throw new UsageError("fields are numbered from 1", true); + ranges.push([start, end]); + } + ranges.sort((a, b) => a[0] - b[0] || a[1] - b[1]); + const merged = []; + for (const [start, end] of ranges) { + const previous = merged.at(-1); + if (previous && start <= previous[1]) previous[1] = Math.max(previous[1], end); + else merged.push([start, end]); + } + return merged; +} + +export function parseNumfmtFieldNumber(text) { + if (text.length >= 20) throw new UsageError(`field number '${text}' is too large`, true); + const value = Number(text); + return Number.isSafeInteger(value) ? value : Infinity; +} + +export function parseNumfmtUnit(value) { + const text = String(value); + if (/^\+?\d+$/.test(text)) { + const n = Number(text); + if (Number.isSafeInteger(n) && n > 0) return n; + } + const match = text.match(/^(\+?\d+)?([KMGTPEZYRQ])i?$/); + if (match) { + const prefix = match[1] == null ? 1 : Number(match[1]); + if (Number.isSafeInteger(prefix) && prefix > 0) return prefix * (text.endsWith("i") ? 1024 : 1000) ** ("KMGTPEZYRQ".indexOf(match[2]) + 1); + } + throw new UsageError(`invalid unit size: ${localeQuotedEscapedDiagnostic(text)}`); +} + +export function formatNumfmtRecord(record, opts) { + if (record.includes("\0")) record = record.slice(0, record.indexOf("\0")); + const delimiter = opts.d ?? opts.delimiter; + const wholeRecord = delimiter === ""; + const leading = delimiter == null ? record.match(/^\s*/)?.[0] ?? "" : ""; + const trailing = delimiter == null ? record.match(/\s*$/)?.[0] ?? "" : ""; + const body = delimiter == null ? record.slice(leading.length, record.length - trailing.length) : record; + const fields = wholeRecord ? [record] : delimiter == null ? (body === "" ? [""] : body.split(/[ \t\n\r\f\v]+/)) : record.split(delimiter); + const defaultSeparators = body.match(/[ \t\n\r\f\v]+/g) ?? []; + const separators = wholeRecord ? [] : delimiter == null ? (opts.z || opts["zero-terminated"] ? defaultSeparators.map(() => " ") : defaultSeparators) : Array.from({ length: Math.max(0, fields.length - 1) }, () => delimiter); + const selected = readNumfmtFieldRanges(opts.field ?? "1"); + let ok = true; + let error; + for (let i = 0; i < fields.length; i++) { + if (!inRanges(i + 1, selected)) continue; + const result = formatNumfmtValue(fields[i], opts); + ok &&= result.ok; + if (!result.ok && error == null) error = result.error; + if (!result.ok && (opts.invalid ?? "abort") === "abort") { + const prefixFields = fields.slice(0, i); + const prefix = prefixFields.length ? joinFields(prefixFields, separators.slice(0, Math.max(0, i - 1))) + (separators[i - 1] ?? "") : ""; + return { ok: false, error, text: `${leading}${prefix}` }; + } + fields[i] = result.ok && delimiter == null && (leading || i > 0) ? applyPadding(result.text, fields[i].length) : result.text; + } + return { ok, error, text: `${leading}${joinFields(fields, separators)}${trailing}` }; +} + +export function joinFields(fields, separators) { + let out = fields[0] ?? ""; + for (let i = 1; i < fields.length; i++) out += (separators[i - 1] ?? " ") + fields[i]; + return out; +} + +export function formatNumfmtValue(value, opts) { + try { + const suffix = opts.suffix ?? ""; + const fromUnit = opts["from-unit"] == null ? 1 : parseNumfmtUnit(opts["from-unit"]); + const toUnit = opts["to-unit"] == null ? 1 : parseNumfmtUnit(opts["to-unit"]); + const unitSeparator = opts["unit-separator"] ?? opts["unit-sep"] ?? " "; + const parsed = opts.from ? parseHumanNumberParts(value, opts.from, suffix, unitSeparator) : parsePlainNumfmtNumber(value, suffix); + if (opts.from && opts["from-unit"] != null) parsed.decimals = countNumfmtInputDecimals(value, suffix); + const n = (typeof parsed === "number" ? parsed : parsed.value) * fromUnit; + if (!Number.isFinite(n)) throw new Error("invalid number"); + const format = parseNumfmtFormat(opts.format ?? "%f"); + let text = opts.to === "si" || opts.to === "iec" || opts.to === "iec-i" + ? humanNumber(n / toUnit, opts.to, opts.round ?? "human-default", opts["unit-separator"] ?? opts["unit-sep"] ?? "", format.explicitPrecision ? format.precision : null) + : formatPlainNumfmtNumber(n / toUnit, opts.round ?? "from-zero", format.explicitPrecision ? format.precision : (typeof parsed === "object" ? parsed.decimals : 0)); + if (opts.suffix) text += opts.suffix; + text = applyNumfmtFormat(text, format); + if (opts.padding && (opts.format == null || format.zero)) text = applyPadding(text, Number(opts.padding)); + return { ok: true, text }; + } catch (error) { + return { ok: false, text: value, error: error.message }; + } +} + +export function parsePlainNumfmtNumber(value, suffix = "") { + let text = String(value); + if (suffix && text.endsWith(suffix)) text = text.slice(0, -suffix.length); + if (!/^[+-]?(?:\d+(?:\.\d+)?|\.\d+)$/.test(text)) throw new Error(numfmtNumberError(text, { fromMode: null })); + const decimals = text.includes(".") ? text.split(".")[1].length : 0; + return { value: Number(text), decimals }; +} + +export function countNumfmtInputDecimals(value, suffix = "") { + let text = String(value); + if (suffix && text.endsWith(suffix)) text = text.slice(0, -suffix.length); + const match = text.match(/^[+-]?\d+\.(\d+)/); + return match ? match[1].length : 0; +} + +export function numfmtNumberError(text, { fromMode = null } = {}) { + const value = String(text); + const display = numfmtDiagnosticValue(value); + if (/^[+-]?\d+\.(?:\D|$)/.test(value)) return `invalid number: ${localeQuotedDiagnostic(display)}`; + const separatedSuffix = value.match(/^([+-]?(?:\d+(?:\.\d+)?|\.\d+)) ([kKMGTPEZYRQ](?:iB?|B)?)[ \t]+(.+)$/); + if (fromMode && separatedSuffix) return `invalid suffix in input ${localeQuotedDiagnostic(display)}: ${localeQuotedDiagnostic(numfmtDiagnosticValue(separatedSuffix[3]))}`; + if (/^[+-]?(?:\d+(?:\.\d+)?|\.\d+)[A-Za-z]/.test(value)) { + const suffix = value.match(/^[+-]?(?:\d+(?:\.\d+)?|\.\d+)(.*)$/)?.[1] ?? ""; + if (!fromMode && /^[KMGTPEZYRQ]i?B?$/i.test(suffix)) return `rejecting suffix in input: ${localeQuotedDiagnostic(display)} (consider using --from)`; + return suffix.length > 1 && /^[KMGTPEZYRQ]i?/.test(suffix) + ? `invalid suffix in input ${localeQuotedDiagnostic(display)}: ${localeQuotedDiagnostic(numfmtDiagnosticValue(suffix.replace(/^[KMGTPEZYRQ]i?/i, "")))}` + : `invalid suffix in input: ${localeQuotedDiagnostic(display)}`; + } + if (/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)\s+/.test(value) || /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)\S/.test(value) || value.includes("..")) return `invalid suffix in input: ${localeQuotedDiagnostic(display)}`; + return `invalid number: ${localeQuotedDiagnostic(display)}`; +} + +export function numfmtDiagnosticValue(value) { + return [...String(value)].map((ch) => { + if (/[\udc80-\udcff]/.test(ch)) return `\\${(ch.charCodeAt(0) - 0xdc00).toString(8).padStart(3, "0")}`; + return lsEscapedName(ch, { escapeDouble: false }); + }).join(""); +} + +export function formatPlainNumfmtNumber(value, round = "nearest", precision = 0) { + const rounded = roundNumber(value, precision, round); + return precision > 0 ? rounded.toFixed(precision) : String(rounded); +} + +export function applyPadding(text, width) { + if (!Number.isInteger(width) || width === 0 || text.length >= Math.abs(width)) return text; + return width > 0 ? text.padStart(width) : text.padEnd(Math.abs(width)); +} + +export function humanNumber(n, mode, round = "up", unitSeparator = "", forcedPrecision = null) { + const base = mode === "si" ? 1000 : 1024; + const units = mode === "iec-i" ? ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi", "Ri", "Qi"] : mode === "si" ? ["", "k", "M", "G", "T", "P", "E", "Z", "Y", "R", "Q"] : ["", "K", "M", "G", "T", "P", "E", "Z", "Y", "R", "Q"]; + let value = n; + let idx = 0; + while (Math.abs(value) >= base && idx < units.length - 1) { + value /= base; + idx++; + } + if (!idx) { + const rounded = roundNumber(value, 0, round); + if (Math.abs(rounded) < base) return forcedPrecision == null ? String(rounded) : rounded.toFixed(forcedPrecision); + value = rounded / base; + idx++; + } + let decimals = forcedPrecision ?? (Math.abs(value) >= 10 ? 0 : 1); + let rounded = roundNumber(value, decimals, round); + if (Math.abs(rounded) >= base && idx < units.length - 1) { + value = rounded / base; + idx++; + decimals = forcedPrecision ?? (Math.abs(value) >= 10 ? 0 : 1); + rounded = roundNumber(value, decimals, round); + } else if (forcedPrecision == null && Math.abs(rounded) >= 10 && decimals !== 0) { + decimals = 0; + rounded = roundNumber(value, decimals, round); + } + return `${rounded.toFixed(decimals)}${unitSeparator}${units[idx]}`; +} + +export function roundNumber(value, precision, round) { + const scale = 10 ** precision; + const scaled = value * scale; + let rounded; + if (round === "human-default" || round === "from-zero") rounded = scaled < 0 ? Math.floor(scaled) : Math.ceil(scaled); + else if (round === "up") rounded = Math.ceil(scaled); + else if (round === "down") rounded = Math.floor(scaled); + else if (round === "towards-zero") rounded = scaled < 0 ? Math.ceil(scaled) : Math.floor(scaled); + else rounded = Math.sign(scaled) * Math.floor(Math.abs(scaled) + 0.5); + return rounded / scale; +} + +export function parseNumfmtFormat(format) { + const text = String(format); + let directives = 0; + let match; + for (let i = 0; i < text.length; i++) { + if (text[i] !== "%") continue; + if (text[i + 1] === "%") { + i++; + continue; + } + directives++; + match = text.slice(i).match(/^%([0 ']*)?(-)?([+ ]?\d+)?(?:\.(\d+))?f/); + if (match?.[2] && /^[+ ]/.test(match[3] ?? "")) match = null; + if (!match) { + if (text.slice(i) === "%") throw new UsageError(`format ${localeQuotedEscapedDiagnostic(text)} ends in %`); + const badPrecision = text.slice(i).match(/^%\.([-+ ]\d+)f/); + if (badPrecision) throw new UsageError(`invalid precision in format ${localeQuotedEscapedDiagnostic(text)}`); + throw new UsageError(`invalid format ${localeQuotedEscapedDiagnostic(text)}, directive must be %[0]['][-][N][.][N]f`); + } + i += match[0].length - 1; + } + if (directives === 0) throw new UsageError(text.endsWith("%") ? `format ${localeQuotedEscapedDiagnostic(text)} ends in %` : `format ${localeQuotedEscapedDiagnostic(text)} has no % directive`); + if (directives > 1) throw new UsageError(`format ${localeQuotedEscapedDiagnostic(text)} has too many % directives`); + const parsed = text.match(/^(.*)%([0 ']*)?(-)?([+ ]?\d+)?(?:\.(\d+))?f(.*)$/); + return { + raw: text, + prefix: parsed?.[1]?.replace(/%%\s$/, "%%").replaceAll("%%", "%%") ?? "", + suffix: parsed?.[6]?.replaceAll("%%", "%%") ?? "", + left: Boolean(parsed?.[3]), + zero: parsed?.[2]?.includes("0") ?? false, + grouping: parsed?.[2]?.includes("'") ?? false, + width: parsed?.[4] == null ? null : Number(parsed[4]), + precision: parsed?.[5] == null ? 0 : Number(parsed[5]), + explicitPrecision: parsed?.[5] != null, + }; +} + +export function applyNumfmtFormat(text, format) { + if (format.raw === "%f") return text; + let body = text; + if (format.width != null && body.length < format.width) { + const pad = format.zero ? "0" : " "; + if (format.zero && !format.left && body.startsWith("-")) body = `-${body.slice(1).padStart(format.width - 1, pad)}`; + else body = format.left ? body.padEnd(format.width, pad) : body.padStart(format.width, pad); + } + return `${format.prefix}${body}${format.suffix}`; +} + +const singleCall = defineCommand("numfmt", numfmtCmd, numfmtMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/od.js b/src/commands/od.js new file mode 100644 index 0000000..310244d --- /dev/null +++ b/src/commands/od.js @@ -0,0 +1,717 @@ +#!/usr/bin/env bun + +import { closeSync, openSync, readSync } from "node:fs"; +import { open, stat } from "node:fs/promises"; +import { concatBytes, isWriteError, localeQuotedDiagnostic, localeQuotedEscapedDiagnostic, normalizeLongOptionByPrefix, parseGNUSize, parseOptions, readAll, readFdChunkViews, systemErrorMessage } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const OD_LONG_OPTIONS = ["address-radix", "endian", "format", "output-duplicates", "read-bytes", "skip-bytes", "strings", "traditional", "width", "help", "version"]; + +export function odMetaOption(args) { + const longValueOptions = new Set(["address-radix", "endian", "format", "read-bytes", "skip-bytes"]); + const longOptionalValueOptions = new Set(["strings", "width"]); + const shortValueOptions = new Set(["A", "j", "N", "S", "t"]); + const shortOptionalValueOptions = new Set(["w"]); + const shortKnownOptions = new Set(["A", "j", "N", "S", "t", "w", "a", "b", "c", "d", "f", "i", "l", "o", "s", "x", "v"]); + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (arg.startsWith("--")) { + const option = normalizeOdLongOption(arg); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (!OD_LONG_OPTIONS.includes(name)) return null; + if ((option === "--help" || option === "--version") && inlineValue == null) return option; + const value = inlineValue ?? (longValueOptions.has(name) ? args[i + 1] : undefined); + if (name === "endian" && value !== undefined) validateOdEndian(value); + if (name === "read-bytes" && value !== undefined) parseOdSize(value, "--read-bytes"); + if (name === "skip-bytes" && value !== undefined) parseOdSize(value, "--skip-bytes"); + if (name === "strings" && inlineValue != null) parseOdStringLength(inlineValue, "--strings"); + if (name === "width" && inlineValue != null) parseOdWidth(inlineValue, "--width"); + if (inlineValue == null && longValueOptions.has(name)) i++; + else if (inlineValue == null && longOptionalValueOptions.has(name)) continue; + else if (inlineValue != null && !longValueOptions.has(name) && !longOptionalValueOptions.has(name)) return null; + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (!shortKnownOptions.has(ch)) return null; + if (shortValueOptions.has(ch)) { + const inlineValue = arg.slice(j + 1); + const value = inlineValue === "" ? args[i + 1] : inlineValue; + validateOdMetaShortValue(ch, value); + if (inlineValue === "") i++; + break; + } + if (shortOptionalValueOptions.has(ch)) { + const inlineValue = arg.slice(j + 1); + if (inlineValue !== "") parseOdWidth(inlineValue, "-w"); + break; + } + } + } + return null; +} + +export function validateOdMetaShortValue(ch, value) { + if (value === undefined) return; + if (ch === "j") parseOdSize(value, "-j"); + else if (ch === "N") parseOdSize(value, "-N"); + else if (ch === "S") parseOdStringLength(value, "-S"); +} + +export function readStdinBytesLimit(limit) { + const chunks = []; + const buffer = Buffer.allocUnsafe(Math.min(64 * 1024, Math.max(0, limit))); + let total = 0; + while (total < limit) { + const n = readSync(0, buffer, 0, Math.min(buffer.length, limit - total), null); + if (n === 0) break; + chunks.push(Buffer.from(buffer.subarray(0, n))); + total += n; + } + const out = new Uint8Array(total); + let offset = 0; + for (const chunk of chunks) { + out.set(chunk, offset); + offset += chunk.length; + } + return out; +} + +export async function odCmd(args) { + args = normalizeOdLongOptions(args); + const { opts, operands } = parseOptions(args, { short: { A: "value", j: "value", N: "value", S: "value", t: "value", w: "optional-value", a: false, b: false, c: false, d: false, f: false, i: false, l: false, o: false, s: false, x: false, v: false }, long: { "address-radix": "value", "skip-bytes": "value", strings: "optional-value", format: "value", "read-bytes": "value", width: "optional-value", "output-duplicates": false, endian: "value", traditional: false, help: false, version: false } }); + const explicitFormats = odFormatsFromArgs(args).filter((format) => format !== ""); + const rawFormats = explicitFormats.length ? explicitFormats : [odFormat(opts)]; + const radix = normalizeOdAddressRadix(opts.A ?? opts["address-radix"] ?? "o"); + validateOdEndian(opts.endian); + for (const format of rawFormats) validateOdFormat(format); + const formats = rawFormats.flatMap(odFormatSpecs); + const oldStyleOffset = odCanUseOldStyleOffset(args); + const traditional = parseOdTraditionalOperands(operands, opts.traditional, oldStyleOffset); + const inputFiles = traditional.files.length ? traditional.files : ["-"]; + const chunks = []; + let failed = false; + let hasNonRegularInput = false; + const skip = traditional.skip ?? opts.j ?? opts["skip-bytes"]; + const limit = opts.N ?? opts["read-bytes"]; + const skipOption = opts.j != null ? "-j" : opts["skip-bytes"] != null ? "--skip-bytes" : "-j"; + const limitOption = opts.N != null ? "-N" : "--read-bytes"; + const start = skip == null ? 0 : parseOdSize(skip, skipOption); + const limitSize = limit == null ? null : parseOdSize(limit, limitOption); + const readLimit = limitSize == null ? null : start + limitSize; + if (skip == null && limit == null && traditional.label == null + && opts.S == null && opts.strings == null && inputFiles.length === 1 + && (opts.v || opts["output-duplicates"])) { + const lineWidth = resolveOdLineWidth(opts, formats); + return streamOdFile(inputFiles[0], formats, opts, lineWidth, radix); + } + for (const file of inputFiles) { + try { + if (file !== "-") { + const s = await stat(file); + if (s.isDirectory()) throw Object.assign(new Error("Is a directory"), { code: "EISDIR" }); + if (!s.isFile()) hasNonRegularInput = true; + } + chunks.push(readLimit == null ? await readAll(file) : await readAllLimit(file, readLimit)); + } catch (error) { + stderr(`od: ${odDiagnosticFile(file)}: ${systemErrorMessage(error)}\n`); + failed = true; + } + } + if (failed && chunks.length === 0) return 1; + const raw = concatBytes(chunks); + if (skip != null && start > raw.length && !hasNonRegularInput) { + stderr("od: cannot skip past end of combined input\n"); + return 1; + } + const data = raw.slice(start, limit == null ? undefined : start + limitSize); + const labelDelta = traditional.label == null ? null : parseGNUSize(traditional.label) - start; + if (opts.S != null || opts.strings != null) { + const stringsOption = opts.S != null ? "-S" : "--strings"; + stdout(formatOdStrings(data, parseOdStringLength(opts.S ?? (opts.strings === true ? 3 : opts.strings ?? 3), stringsOption), start, radix, limit != null)); + return failed ? 1 : 0; + } + const format = formats[0]; + const lineWidth = resolveOdLineWidth(opts, formats); + let out = ""; + let previousBodyKey = null; + let duplicatesSuppressed = false; + for (let offset = 0; offset < data.length; offset += lineWidth) { + const chunk = data.slice(offset, offset + lineWidth); + const address = formatOdAddress(start + offset, radix, labelDelta); + const bodyLines = odRenderedBodyLines(chunk, formats, opts.endian, lineWidth); + const bodyKey = bodyLines.join("\n"); + if (!(opts.v || opts["output-duplicates"]) && previousBodyKey === bodyKey) { + if (!duplicatesSuppressed) out += "*\n"; + duplicatesSuppressed = true; + continue; + } + previousBodyKey = bodyKey; + duplicatesSuppressed = false; + out += formatOdChunkOutput(address, bodyLines, formats); + } + const finalAddress = formatOdAddress(start + data.length, radix, labelDelta); + if (finalAddress) out += `${finalAddress}\n`; + stdout(out); + return failed ? 1 : 0; +} + +export function resolveOdLineWidth(opts, formats) { + const width = opts.w ?? opts.width; + const widthOption = opts.w != null ? "-w" : "--width"; + let lineWidth = width === true ? 32 : width == null ? 16 : parseOdWidth(width, widthOption); + const formatSize = Math.max(...formats.map(odFormatSize)); + if (lineWidth % formatSize !== 0) { + stderr(`od: warning: invalid width ${lineWidth}; using ${formatSize} instead\n`); + lineWidth = formatSize; + } + return lineWidth; +} + +export function formatOdChunkOutput(address, bodyLines, formats) { + if (formats.length === 1) return `${address} ${bodyLines[0]}\n`; + const bodyWidth = Math.max(...bodyLines.map((line) => line.length)); + const firstPrefix = address ? `${address} ` : " "; + const nextPrefix = address ? " ".repeat(address.length + 1) : " "; + let out = ""; + for (let i = 0; i < bodyLines.length; i++) { + const line = normalizeOdFormat(formats[i]).showChars ? bodyLines[i].padEnd(bodyWidth) : bodyLines[i]; + out += `${i === 0 ? firstPrefix : nextPrefix}${line}\n`; + } + return out; +} + +export function streamOdFile(file, formats, opts, lineWidth, radix) { + let fd; + let offset = 0; + let carry = Buffer.alloc(0); + try { + fd = file === "-" ? 0 : openSync(file, "r"); + readFdChunkViews(fd, (chunk) => { + const bytes = carry.length ? Buffer.concat([carry, chunk]) : chunk; + let index = 0; + while (index + lineWidth <= bytes.length) { + const part = bytes.subarray(index, index + lineWidth); + const address = formatOdAddress(offset, radix, null); + stdout(formatOdChunkOutput(address, odRenderedBodyLines(part, formats, opts.endian, lineWidth), formats)); + offset += part.length; + index += lineWidth; + } + carry = Buffer.from(bytes.subarray(index)); + }); + if (carry.length) { + const address = formatOdAddress(offset, radix, null); + stdout(formatOdChunkOutput(address, odRenderedBodyLines(carry, formats, opts.endian, lineWidth), formats)); + offset += carry.length; + } + const finalAddress = formatOdAddress(offset, radix, null); + if (finalAddress) stdout(`${finalAddress}\n`); + return 0; + } catch (error) { + if (isWriteError(error)) throw error; + stderr(`od: ${odDiagnosticFile(file)}: ${systemErrorMessage(error)}\n`); + return 1; + } finally { + if (fd != null && fd !== 0) closeSync(fd); + } +} + +export function odRenderedBodyLines(chunk, formats, endian, lineWidth) { + if (formats.length === 1) return [formatOdLine(chunk, formats[0], endian, lineWidth)]; + return formats.map((fmt) => formatOdLine(chunk, fmt, endian, lineWidth, true, formats)); +} + +export function normalizeOdAddressRadix(radix) { + const text = String(radix); + if (/^[doxn]/.test(text)) return text[0]; + throw new UsageError(`invalid output address radix '${text[0] ?? "\0"}'; it must be one character from [doxn]`); +} + +export function normalizeOdLongOptions(args) { + return args.map((arg) => normalizeOdLongOption(arg)); +} + +export function normalizeOdLongOption(arg) { + if (!arg.startsWith("--") || arg === "--") return arg; + return normalizeLongOptionByPrefix(arg, OD_LONG_OPTIONS); +} + +export function validateOdEndian(endian) { + if (endian == null || endian === "little" || endian === "big") return; + const kind = endian === "" ? "ambiguous" : "invalid"; + throw new UsageError(`${kind} argument ${localeQuotedEscapedDiagnostic(endian)} for ${localeQuotedDiagnostic("--endian")}\nValid arguments are:\n - ${localeQuotedDiagnostic("little")}\n - ${localeQuotedDiagnostic("big")}`, true); +} + +export async function readAllLimit(path, limit) { + if (limit <= 0) return new Uint8Array(); + if (path === "-") return readStdinBytesLimit(limit); + const handle = await open(path, "r"); + try { + const bytes = Buffer.allocUnsafe(limit); + let offset = 0; + while (offset < limit) { + const { bytesRead } = await handle.read(bytes, offset, limit - offset, null); + if (bytesRead === 0) break; + offset += bytesRead; + } + return bytes.subarray(0, offset); + } finally { + await handle.close(); + } +} + +export function parseOdSize(value, option) { + const text = String(value); + if (text === "") throw new UsageError(`invalid ${option} argument ''`); + try { + const size = parseOdByteCount(text.replace(/^\+/, "")); + return size > BigInt(Number.MAX_SAFE_INTEGER) ? Number.MAX_SAFE_INTEGER : Number(size); + } catch (error) { + if (error instanceof UsageError && error.message === "too large") throw new UsageError(`${option} argument '${text}' too large`); + if (text.startsWith("-")) throw new UsageError(`invalid ${option} argument '${text}'`); + throw new UsageError(`invalid suffix in ${option} argument '${text}'`); + } +} + +export function parseOdByteCount(text) { + const match = String(text).match(/^(\d+)([A-Za-z]*)$/); + if (!match) throw new UsageError("invalid suffix"); + const scales = { + "": 1n, b: 512n, + K: 1024n, k: 1024n, KiB: 1024n, kiB: 1024n, + M: 1024n ** 2n, m: 1024n ** 2n, MiB: 1024n ** 2n, miB: 1024n ** 2n, + G: 1024n ** 3n, g: 1024n ** 3n, GiB: 1024n ** 3n, giB: 1024n ** 3n, + T: 1024n ** 4n, t: 1024n ** 4n, TiB: 1024n ** 4n, tiB: 1024n ** 4n, + P: 1024n ** 5n, PiB: 1024n ** 5n, + E: 1024n ** 6n, EiB: 1024n ** 6n, + Z: 1024n ** 7n, ZiB: 1024n ** 7n, + Y: 1024n ** 8n, YiB: 1024n ** 8n, + R: 1024n ** 9n, RiB: 1024n ** 9n, + Q: 1024n ** 10n, QiB: 1024n ** 10n, + KB: 1000n, kB: 1000n, MB: 1000n ** 2n, mB: 1000n ** 2n, + GB: 1000n ** 3n, gB: 1000n ** 3n, TB: 1000n ** 4n, tB: 1000n ** 4n, + PB: 1000n ** 5n, EB: 1000n ** 6n, ZB: 1000n ** 7n, YB: 1000n ** 8n, + RB: 1000n ** 9n, QB: 1000n ** 10n, + }; + const scale = scales[match[2]]; + if (!scale) throw new UsageError("invalid suffix"); + const amount = BigInt(match[1]) * scale; + if (amount > 9223372036854775807n) throw new UsageError("too large"); + return amount; +} + +export function parseOdWidth(value, option) { + const text = String(value); + let size; + try { + size = parseGNUSize(text.replace(/^\+/, "")); + } catch { + if (text.startsWith("-") || !/^\+?\d/.test(text)) throw new UsageError(`invalid ${option} argument '${text}'`); + throw new UsageError(`invalid suffix in ${option} argument '${text}'`); + } + if (size <= 0) throw new UsageError(`invalid ${option} argument '${text}'`); + return size; +} + +export function parseOdStringLength(value, option = "-S") { + const text = String(value); + if (text === "") throw new UsageError(`invalid ${option} argument ''`); + try { + const parsed = parseOdByteCount(/^\D+$/.test(text) ? `1${text}` : text); + return parsed > BigInt(Number.MAX_SAFE_INTEGER) ? Number.MAX_SAFE_INTEGER : Number(parsed); + } catch (error) { + if (error instanceof UsageError && error.message === "too large") throw new UsageError(`${option} argument '${text}' too large`); + if (text.startsWith("-") || /^[A-Za-z]$/.test(text)) throw new UsageError(`invalid ${option} argument '${text}'`); + throw new UsageError(`invalid suffix in ${option} argument '${text}'`); + } +} + +export function odFormatsFromArgs(args) { + const formats = []; + const traditionalFormats = { a: "a", b: "o1", c: "c", d: "u2", f: "fF", i: "dI", l: "dL", o: "o2", s: "d2", x: "x2" }; + for (let i = 0; i < args.length; i++) { + const arg = normalizeOdLongOption(String(args[i])); + if (arg === "--") break; + if (arg === "-t" || arg === "--format") { + if (i + 1 < args.length) formats.push(String(args[++i])); + } else if (arg.startsWith("-t") && arg.length > 2) { + formats.push(arg.slice(2)); + } else if (arg.startsWith("--format=")) { + formats.push(arg.slice("--format=".length)); + } else if (/^-[^-]/.test(arg)) { + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (traditionalFormats[ch]) formats.push(traditionalFormats[ch]); + else if (ch === "t") { + if (j + 1 < arg.length) formats.push(arg.slice(j + 1)); + else if (i + 1 < args.length) formats.push(String(args[++i])); + break; + } else if ("AjNSw".includes(ch)) { + break; + } + } + } + } + return formats; +} + +export function odFormatSpecs(format) { + const text = String(format); + const specs = []; + let i = 0; + while (i < text.length) { + const start = i; + const kind = text[i++]; + if (!isOdFormatStart(kind)) throw new UsageError(`invalid character '${kind ?? ""}' in type string ${localeQuotedEscapedDiagnostic(format)}`); + if (kind === "f") { + if ("FDLHB".includes(text[i] ?? "")) i++; + else while (/\d/.test(text[i] ?? "")) i++; + } else if ("doux".includes(kind)) { + if ("CSIL".includes(text[i] ?? "")) i++; + else while (/\d/.test(text[i] ?? "")) i++; + } + if (text[i] === "z") i++; + specs.push(text.slice(start, i)); + } + return specs; +} + +export function isOdFormatStart(ch) { + return ch != null && "acfdoux".includes(ch); +} + +export function odCanUseOldStyleOffset(args) { + return args.every((arg) => arg === "--" || !String(arg).startsWith("-")); +} + +export function parseOdTraditionalOperands(operands, traditional = false, oldStyleOffset = false) { + const files = [...operands]; + if (!files.length) return { files, skip: null, label: null }; + let label = null; + const rejectTraditionalExtraFile = () => { + if (traditional && files.length > 1) { + throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(files[1])}\nod: compatibility mode supports at most one file`, true); + } + }; + if (traditional && files.length >= 2) { + const parsedLabel = parseOdTraditionalOffset(files.at(-1)); + const parsedSkip = parseOdTraditionalOffset(files.at(-2)); + if (parsedLabel && parsedSkip) { + files.pop(); + files.pop(); + rejectTraditionalExtraFile(); + return { files, skip: parsedSkip, label: parsedLabel }; + } + } + const last = files.at(-1); + const parsed = parseOdTraditionalOffset(last); + if (!parsed || (!traditional && (!oldStyleOffset || !String(last).startsWith("+")))) { + rejectTraditionalExtraFile(); + return { files, skip: null, label }; + } + files.pop(); + rejectTraditionalExtraFile(); + return { files, skip: parsed, label }; +} + +export function parseOdTraditionalOffset(value) { + const text = String(value); + const match = text.match(/^\+?((?:0x[0-9a-fA-F]+)|(?:\d+))(\.)?([bB])?$/); + if (!match) { + if (/^(?:\+?[0-9]{100,}\.?|\+?0x[0-9a-fA-F]{100,})$/.test(text)) throw new UsageError(`${text}: Numerical result out of range`); + return null; + } + const digits = match[1]; + if (digits.replace(/^0x/i, "").length > 60) throw new UsageError(`${text}: Numerical result out of range`); + const base = /^0x/i.test(digits) ? 16 : match[2] === "." ? 10 : 8; + if (base === 8 && /[89]/.test(digits)) return null; + let n = Number.parseInt(digits.replace(/^0x/i, ""), base); + if (match[3]?.toLowerCase() === "b") n *= 512; + if (!Number.isSafeInteger(n)) throw new UsageError(`${text}: Numerical result out of range`); + return String(n); +} + +export function odDiagnosticFile(file) { + return /\s/.test(String(file)) ? `'${String(file).replace(/'/g, "'\\''")}'` : file; +} + +export function odFormat(opts) { + if (opts.a) return "a"; + if (opts.b) return "o1"; + if (opts.c) return "c"; + if (opts.d) return "u2"; + if (opts.f) return "fF"; + if (opts.i) return "dI"; + if (opts.l) return "dL"; + if (opts.o) return "o2"; + if (opts.s) return "d2"; + if (opts.x) return "x2"; + if (opts.t != null && opts.t !== "") return opts.t; + if (opts.format != null && opts.format !== "") return opts.format; + return "o2"; +} + +export function formatOffset(offset, radix) { + if (radix === "n") return ""; + if (radix === "d") return String(offset).padStart(7, "0"); + if (radix === "x") return offset.toString(16).padStart(6, "0"); + return offset.toString(8).padStart(7, "0"); +} + +export function formatOdAddress(offset, radix, labelDelta = null) { + const address = formatOffset(offset, radix); + if (labelDelta == null) return address; + const label = formatOffset(offset + (radix === "n" ? 0 : labelDelta), radix === "n" ? "o" : radix); + return address ? `${address} (${label})` : `(${label})`; +} + +export function formatOdLine(chunk, format, endian = "little", lineWidth = null, multiFormat = false, groupFormats = [format]) { + const normalized = normalizeOdFormat(format); + const body = formatOdChunk(chunk, normalized.format, endian, multiFormat, groupFormats); + if (!normalized.showChars) return body; + const paddedBody = lineWidth == null ? body : body.padEnd(odFullLineBodyWidth(normalized.format, lineWidth, multiFormat, groupFormats)); + return `${paddedBody} ${formatOdAscii(chunk)}`; +} + +export function normalizeOdFormat(format) { + const text = String(format); + return { format: text.endsWith("z") ? text.slice(0, -1) : text, showChars: text.endsWith("z") }; +} + +export function validateOdFormat(format) { + const text = String(format); + let i = 0; + while (i < text.length) { + const start = i; + const kind = text[i++]; + if (!isOdFormatStart(kind)) throw new UsageError(`invalid character '${kind ?? ""}' in type string ${localeQuotedEscapedDiagnostic(format)}`); + if (kind === "f") { + if ("FDLHB".includes(text[i] ?? "")) i++; + else while (/\d/.test(text[i] ?? "")) i++; + } else if ("doux".includes(kind)) { + if ("CSIL".includes(text[i] ?? "")) i++; + else while (/\d/.test(text[i] ?? "")) i++; + } + if (text[i] === "z") i++; + validateOdFormatSpec(text.slice(start, i), format); + } +} + +export function validateOdFormatSpec(spec, originalFormat = spec) { + const text = normalizeOdFormat(spec).format; + if (text === "a" || text === "c") return; + if ((text.startsWith("a") || text.startsWith("c")) && text.length > 1) throw new UsageError(`invalid character '${text[1]}' in type string ${localeQuotedEscapedDiagnostic(originalFormat)}`); + if (text === "f" || text === "fF" || text === "fD" || text === "fL" || text === "fH" || text === "fB" || /^f\d+$/.test(text)) { + const floatSize = text.match(/^f(\d+)$/); + if (floatSize && ![2, 4, 8, 16].includes(Number(floatSize[1]))) { + throw new UsageError(`invalid type string ${localeQuotedEscapedDiagnostic(originalFormat)};\nthis system doesn't provide a ${floatSize[1]}-byte floating point type`); + } + odFormatSize(text); + return; + } + const kind = text[0] ?? ""; + if (!"doux".includes(kind)) throw new UsageError(`invalid character '${kind}' in type string ${localeQuotedEscapedDiagnostic(originalFormat)}`); + const suffix = text.slice(1); + if (suffix === "" || ["C", "S", "I", "L"].includes(suffix)) return; + if (/^\d+$/.test(suffix)) { + const size = Number(suffix); + if (![1, 2, 4, 8].includes(size)) throw new UsageError(`invalid type string ${localeQuotedEscapedDiagnostic(originalFormat)};\nthis system doesn't provide a ${suffix}-byte integral type`); + return; + } + throw new UsageError(`invalid character '${suffix[0]}' in type string ${localeQuotedEscapedDiagnostic(originalFormat)}`); +} + +export function formatOdChunk(chunk, format, endian = "little", multiFormat = false, groupFormats = [format]) { + format = normalizeOdFormat(format).format; + const size = odFormatSize(format); + const parts = []; + for (let i = 0; i < chunk.length; i += size) { + const bytes = chunk.slice(i, i + size); + if (format === "c" || format === "a") { + parts.push(formatOdChar(bytes[0], format === "a")); + } else if (format.startsWith("f")) { + parts.push(formatOdFloat(bytes, format, endian)); + } else { + const value = [...bytes].reduce((sum, byte, idx) => sum + BigInt(byte) * (1n << BigInt(8 * (endian === "big" ? bytes.length - idx - 1 : idx))), 0n); + if (format.startsWith("x")) { + const text = value.toString(16).padStart(size * 2, "0"); + parts.push(multiFormat ? text.padStart(odMultiFormatFieldWidth(format, size, groupFormats)) : text.padStart(odFieldWidth(format, size), "0")); + } + else if (format.startsWith("u")) parts.push(value.toString().padStart(multiFormat ? odMultiFormatFieldWidth(format, size, groupFormats) : odFieldWidth(format, size))); + else if (format.startsWith("d")) parts.push(signExtendBigInt(value, size).toString().padStart(multiFormat ? odMultiFormatFieldWidth(format, size, groupFormats) : odFieldWidth(format, size))); + else { + const text = value.toString(8).padStart(odFieldWidth(format, size), "0"); + parts.push(multiFormat ? text.padStart(odMultiFormatFieldWidth(format, size, groupFormats)) : text); + } + } + } + return parts.join(" "); +} + +export function odFullLineBodyWidth(format, lineWidth, multiFormat = false, groupFormats = [format]) { + const size = odFormatSize(format); + const fields = Math.floor(lineWidth / size); + if (fields <= 0) return 0; + const fieldWidth = format === "a" || format === "c" ? 3 : odMultiFormatFieldWidth(format, size, groupFormats, multiFormat); + return fields * fieldWidth + Math.max(0, fields - 1); +} + +export function formatOdFloat(bytes, format, endian = "little") { + const size = odFormatSize(format); + const buffer = new Uint8Array(size); + buffer.set(bytes.slice(0, size)); + if (size === 16) return odFloat128Placeholder(buffer, endian).padStart(24); + const view = new DataView(buffer.buffer); + const littleEndian = endian !== "big"; + const value = format === "fB" + ? odBfloat16Value(view.getUint16(0, littleEndian)) + : size === 2 + ? odFloat16Value(view.getUint16(0, littleEndian)) + : size === 4 + ? view.getFloat32(0, littleEndian) + : view.getFloat64(0, littleEndian); + return formatOdFloatValue(value, size, format); +} + +export function odFloat16Value(bits) { + const sign = bits & 0x8000 ? -1 : 1; + const exponent = (bits >> 10) & 0x1f; + const fraction = bits & 0x03ff; + if (exponent === 0x1f) return fraction ? NaN : sign * Infinity; + if (exponent === 0) return sign * 2 ** -14 * (fraction / 2 ** 10); + return sign * 2 ** (exponent - 15) * (1 + fraction / 2 ** 10); +} + +export function odBfloat16Value(bits) { + const buffer = new ArrayBuffer(4); + const view = new DataView(buffer); + view.setUint32(0, bits << 16, false); + return view.getFloat32(0, false); +} + +export function formatOdFloatValue(value, size, format = "") { + const width = size === 2 || size === 4 ? 15 : 24; + if (!Number.isFinite(value)) return String(value).padStart(width); + const text = Number.isInteger(value) + ? String(value) + : size === 2 + ? trimOdFloatText(value.toPrecision(8)) + : size === 4 + ? trimOdFloatText(value.toPrecision(format === "fB" ? 8 : 7)) + : trimOdFloatText(value.toPrecision(16)); + return text.padStart(width); +} + +export function trimOdFloatText(text) { + return text.replace(/(\.\d*?[1-9])0+(e[+-]?\d+)$/iu, "$1$2").replace(/\.0+(e[+-]?\d+)$/iu, "$1").replace(/(\.\d*?[1-9])0+$/u, "$1").replace(/\.0+$/u, ""); +} + +export function odFloat128Placeholder(bytes, endian = "little") { + const ordered = endian === "big" ? [...bytes] : [...bytes].reverse(); + const value = ordered.reduce((sum, byte) => (sum << 8n) + BigInt(byte), 0n); + return value.toString(); +} + +export function formatOdAscii(chunk) { + let out = ">"; + for (const byte of chunk) out += byte >= 32 && byte < 127 ? String.fromCharCode(byte) : "."; + return `${out}<`; +} + +export function formatOdStrings(data, minLength, start, radix, allowFinalUnterminated = false) { + if (!Number.isFinite(minLength) || minLength < 0) throw new UsageError(`invalid suffix in -S argument '${minLength}'`); + let out = ""; + let runStart = -1; + for (let i = 0; i <= data.length; i++) { + const byte = i < data.length ? data[i] : 0; + const printable = i < data.length && byte >= 32 && byte < 127; + if (printable) { + if (runStart === -1) runStart = i; + continue; + } + if (runStart !== -1) { + const length = i - runStart; + if (((i < data.length && byte === 0) || (i === data.length && allowFinalUnterminated)) && length >= minLength) { + const address = formatOffset(start + runStart, radix); + const text = new TextDecoder().decode(data.slice(runStart, i)); + out += `${address}${address ? " " : ""}${text}\n`; + } + runStart = -1; + } else if (i < data.length && byte === 0 && minLength === 0) { + const address = formatOffset(start + i, radix); + out += `${address}${address ? " " : ""}\n`; + } + } + return out; +} + +export function odFormatSize(format) { + format = normalizeOdFormat(format).format; + if (format === "a" || format === "c") return 1; + if (format === "fH" || format === "fB") return 2; + const floatSize = String(format).match(/^f(\d+)$/); + if (floatSize) { + const size = Number(floatSize[1]); + if (![2, 4, 8, 16].includes(size)) throw new UsageError(`this system doesn't provide a ${size}-byte floating point type`); + return size; + } + if (format === "fF") return 4; + if (format === "fD" || format === "fL" || format === "f") return 8; + const match = String(format).match(/[doux](\d+|C|S|I|L)?/); + const value = match?.[1]; + if (value === "C") return 1; + if (value === "S") return 2; + if (value === "I") return 4; + if (value === "L") return 8; + return Number(value ?? (format === "c" || format === "a" ? 1 : 4)); +} + +export function odFieldWidth(format, size, multiFormat = false) { + if (format.startsWith("x")) return multiFormat && size === 1 ? 3 : size * 2; + if (format.startsWith("o")) return Math.ceil(size * 8 / 3); + if (format.startsWith("u")) return ({ 1: 3, 2: 5, 4: 10, 8: 20 })[size] ?? String(2n ** BigInt(size * 8) - 1n).length; + if (format.startsWith("d")) return ({ 1: 4, 2: 6, 4: 11, 8: 20 })[size] ?? String(2n ** BigInt(size * 8 - 1)).length + 1; + return size; +} + +export function odMultiFormatFieldWidth(format, size, groupFormats, multiFormat = true) { + if (!multiFormat) return odFieldWidth(format, size, multiFormat); + const normalizedGroup = groupFormats.map((groupFormat) => normalizeOdFormat(groupFormat).format); + const unitSize = Math.min(...normalizedGroup.map(odFormatSize)); + const unitWidth = Math.max(...normalizedGroup + .filter((groupFormat) => odFormatSize(groupFormat) === unitSize) + .map((groupFormat) => groupFormat === "a" || groupFormat === "c" ? 3 : odFieldWidth(groupFormat, unitSize, true))); + const unitCount = size % unitSize === 0 ? size / unitSize : 1; + const alignedWidth = unitCount * unitWidth + Math.max(0, unitCount - 1); + return Math.max(odFieldWidth(format, size, true), alignedWidth); +} + +export function formatOdChar(byte, named = false) { + const asciiNames = ["nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel", "bs", "ht", "nl", "vt", "ff", "cr", "so", "si", "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb", "can", "em", "sub", "esc", "fs", "gs", "rs", "us"]; + if (named) { + const ascii = byte & 0x7f; + if (ascii < asciiNames.length) return asciiNames[ascii].padStart(3); + if (ascii === 32) return " sp"; + if (ascii === 127) return "del"; + return String.fromCharCode(ascii).padStart(3); + } + const escapes = { 0: "\\0", 7: "\\a", 8: "\\b", 9: "\\t", 10: "\\n", 11: "\\v", 12: "\\f", 13: "\\r" }; + if (escapes[byte]) return escapes[byte].padStart(3); + if (byte >= 32 && byte < 127) return String.fromCharCode(byte).padStart(3); + return byte.toString(8).padStart(3, "0"); +} + +export function signExtendBigInt(value, size) { + const bits = BigInt(size * 8); + const sign = 1n << (bits - 1n); + const mod = 1n << bits; + return value >= sign ? value - mod : value; +} + +const singleCall = defineCommand("od", odCmd, odMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/paste.js b/src/commands/paste.js new file mode 100644 index 0000000..6b656ab --- /dev/null +++ b/src/commands/paste.js @@ -0,0 +1,233 @@ +#!/usr/bin/env bun + +import { closeSync, openSync } from "node:fs"; +import { isUtf8Locale, isWriteError, nodeErrorMessage, parseOptions, rawCommandArgs, readAll, readFdChunkViews, systemErrorMessage, textInputDiagnosticName } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { concatOutputParts } from "../shared/text.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const PASTE_LONG_OPTIONS = ["delimiters", "serial", "zero-terminated", "help", "version"]; + +export function pasteMetaOption(args) { + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (arg.startsWith("--")) { + const option = normalizePasteLongOption(arg); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (!PASTE_LONG_OPTIONS.includes(name)) return null; + if (option === "--help" || option === "--version") return option; + if (name === "delimiters") { + if (inlineValue == null) i++; + } else if (inlineValue != null) return null; + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (ch === "s" || ch === "z") continue; + if (ch !== "d") return null; + if (!arg.slice(j + 1)) i++; + break; + } + } + return null; +} + +export function normalizePasteLongOptions(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(normalizePasteLongOption(arg)); + } + return out; +} + +export function normalizePasteLongOption(arg) { + const body = arg.slice(2); + const eq = body.indexOf("="); + const name = eq === -1 ? body : body.slice(0, eq); + if (!name) return arg; + const matches = PASTE_LONG_OPTIONS.filter((option) => option.startsWith(name)); + if (matches.length !== 1) return arg; + return eq === -1 ? `--${matches[0]}` : `--${matches[0]}=${body.slice(eq + 1)}`; +} + +export async function paste(args) { + const { opts, operands } = parseOptions(normalizePasteLongOptions(args), { short: { d: "value", s: false, z: false }, long: { delimiters: "value", serial: false, "zero-terminated": false, help: false, version: false } }); + const files = operands.length ? operands : ["-"]; + const recordSep = opts.z || opts["zero-terminated"] ? "\0" : "\n"; + const delimiterValue = opts.d ?? opts.delimiters ?? "\t"; + const rawDelimiter = delimiterValue.includes("\uFFFD") ? pasteReplacementDelimiterBytes(delimiterValue, args) : null; + const delimiters = parsePasteDelimiters(delimiterValue, rawDelimiter); + const pick = (i) => delimiters[i % delimiters.length] ?? "\t"; + const out = []; + let failed = false; + if (!(opts.s || opts.serial) && files.length === 1) { + const file = files[0]; + let fd; + try { + fd = file === "-" ? 0 : openSync(file, "r"); + streamPasteSingleFd(fd, recordSep.charCodeAt(0)); + return 0; + } catch (error) { + if (isWriteError(error)) throw error; + stderr(file === "-" ? `paste: ${nodeErrorMessage(error)}\n` : `paste: ${textInputDiagnosticName(file)}: ${systemErrorMessage(error)}\n`); + return 1; + } finally { + if (fd != null && fd !== 0) closeSync(fd); + } + } + if (opts.s || opts.serial) { + const lineSets = []; + for (const file of files) { + try { + lineSets.push(splitPasteByteRecords(await readAll(file), recordSep)); + } catch (error) { + if (error?.code === "EISDIR") lineSets.push([]); + stderr(file === "-" ? `paste: ${nodeErrorMessage(error)}\n` : `paste: ${textInputDiagnosticName(file)}: ${systemErrorMessage(error)}\n`); + failed = true; + } + } + for (const lines of lineSets) { + for (let i = 0; i < lines.length; i++) { + if (i) out.push(pick(i - 1)); + out.push(lines[i]); + } + out.push(recordSep); + } + } else { + const stdinRecords = files.includes("-") ? splitPasteByteRecords(await readAll("-"), recordSep) : []; + let stdinIndex = 0; + const lineSets = []; + let fatalReadError = false; + for (const file of files) { + if (file === "-") { + lineSets.push(null); + continue; + } + try { + lineSets.push(splitPasteByteRecords(await readAll(file), recordSep)); + } catch (error) { + stderr(`paste: ${textInputDiagnosticName(file)}: ${systemErrorMessage(error)}\n`); + lineSets.push([]); + if (error?.code !== "EISDIR") fatalReadError = true; + failed = true; + } + } + if (fatalReadError) { + stdout(concatOutputParts(out)); + return 1; + } + const maxFileRows = Math.max(0, ...lineSets.filter(Boolean).map((lines) => lines.length)); + for (let row = 0; row < maxFileRows || stdinIndex < stdinRecords.length; row++) { + for (let col = 0; col < lineSets.length; col++) { + const lines = lineSets[col]; + const value = lines ? lines[row] ?? "" : stdinRecords[stdinIndex++] ?? ""; + if (col) out.push(pick(col - 1)); + out.push(value); + } + out.push(recordSep); + } + } + stdout(concatOutputParts(out)); + return failed ? 1 : 0; +} + +export function streamPasteSingleFd(fd, separator) { + let sawData = false; + let lastByte = separator; + readFdChunkViews(fd, (chunk) => { + if (!chunk.length) return; + sawData = true; + lastByte = chunk[chunk.length - 1]; + stdout(chunk); + }); + if (sawData && lastByte !== separator) stdout(Uint8Array.of(separator)); +} + +export function splitPasteByteRecords(bytes, sep) { + const sepByte = sep === "\0" ? 0 : 0x0a; + if (bytes.length === 0) return []; + const end = bytes[bytes.length - 1] === sepByte ? bytes.length - 1 : bytes.length; + if (end === 0) return []; + const records = []; + let start = 0; + for (let i = 0; i < end; i++) { + if (bytes[i] === sepByte) { + records.push(bytes.slice(start, i)); + start = i + 1; + } + } + records.push(bytes.slice(start, end)); + return records; +} + +export function pasteReplacementDelimiterBytes(value, args) { + if (isUtf8Locale()) return Buffer.from(value); + return rawPasteDelimiterBytes(args) ?? Buffer.from(value); +} + +export function parsePasteDelimiters(value, rawBytes = null) { + if (rawBytes) return parsePasteDelimiterBytes(rawBytes); + const out = []; + const chars = [...value]; + for (let i = 0; i < chars.length; i++) { + if (chars[i] === "\\") { + if (i + 1 >= chars.length) throw new UsageError(`delimiter list ends with an unescaped backslash: ${value}`, false); + const ch = chars[++i]; + out.push(({ "0": "", b: "\b", f: "\f", n: "\n", r: "\r", t: "\t", v: "\v", "\\": "\\" })[ch] ?? ch); + } else { + out.push(chars[i]); + } + } + return out.length ? out : [""]; +} + +export function parsePasteDelimiterBytes(bytes) { + const out = []; + const gb18030 = /gb18030/i.test(process.env.LC_ALL ?? process.env.LC_CTYPE ?? process.env.LANG ?? ""); + for (let i = 0; i < bytes.length; i++) { + const byte = bytes[i]; + if (byte === 0x5c) { + if (i + 1 >= bytes.length) throw new UsageError(`delimiter list ends with an unescaped backslash: ${new TextDecoder().decode(bytes)}`, false); + const next = bytes[++i]; + const escaped = ({ 0x30: "", 0x62: "\b", 0x66: "\f", 0x6e: "\n", 0x72: "\r", 0x74: "\t", 0x76: "\v", 0x5c: "\\" })[next]; + if (escaped != null) out.push(escaped); + else if (gb18030 && next >= 0x80 && i + 1 < bytes.length) out.push(bytes.subarray(i, ++i + 1)); + else out.push(bytes.subarray(i, i + 1)); + } else if (gb18030 && byte >= 0x80 && i + 1 < bytes.length) { + out.push(bytes.subarray(i, ++i + 1)); + } else { + out.push(bytes.subarray(i, i + 1)); + } + } + return out.length ? out : [""]; +} + +export function rawPasteDelimiterBytes(args) { + const rawArgs = rawCommandArgs("paste"); + if (!rawArgs) return null; + for (let i = 0; i < rawArgs.length; i++) { + const arg = rawArgs[i]; + if (arg.equals(Buffer.from("-d")) || arg.equals(Buffer.from("--delimiters"))) return rawArgs[i + 1] ?? null; + if (arg.length > 2 && arg[0] === 0x2d && arg[1] === 0x64) return arg.subarray(2); + const eq = arg.indexOf(0x3d); + if (arg.length > 2 && arg[0] === 0x2d && arg[1] === 0x2d) { + const rawName = eq === -1 ? arg.subarray(2) : arg.subarray(2, eq); + const name = new TextDecoder("ascii").decode(rawName); + if ("delimiters".startsWith(name)) return eq === -1 ? rawArgs[i + 1] ?? null : arg.subarray(eq + 1); + } + } + return null; +} + +const singleCall = defineCommand("paste", paste, pasteMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/pathchk.js b/src/commands/pathchk.js new file mode 100644 index 0000000..96f894c --- /dev/null +++ b/src/commands/pathchk.js @@ -0,0 +1,156 @@ +#!/usr/bin/env bun + +import { lstat } from "node:fs/promises"; +import { localeQuotedDiagnostic, lsEscapedName, shellEscapeLsName } from "../shared/common.js"; +import { UsageError, stderr } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const PATHCHK_LONG_OPTIONS = ["portability", "help", "version"]; + +export function pathchkMetaOption(args) { + for (const arg of args) { + if (arg === "--") return null; + if (arg === "-" || !arg.startsWith("-")) return null; + if (!arg.startsWith("--")) { + for (const ch of arg.slice(1)) { + if (ch !== "p" && ch !== "P") return null; + } + continue; + } + const option = normalizePathchkLongOption(arg); + const name = option.slice(2).split("=", 1)[0]; + if (!PATHCHK_LONG_OPTIONS.includes(name) || option.includes("=")) return null; + if (option === "--help" || option === "--version") return option; + } + return null; +} + +export async function pathchk(args) { + const { opts, operands } = parsePathchkOptions(args); + if (!operands.length) throw new UsageError("missing operand", true); + let code = 0; + const portable = opts.p || opts.portability; + const checkEmptyOrLeadingDash = opts.P || opts.portability; + for (const path of operands) { + if (path === "") { + stderr(`pathchk: ${portable || checkEmptyOrLeadingDash ? "empty file name" : "'': No such file or directory"}\n`); + code = 1; + continue; + } + if (portable && path.length > 255) { + stderr(`pathchk: limit 255 exceeded by length ${path.length} of file name '${path}'\n`); + code = 1; + continue; + } + let componentError = false; + for (const component of path.split("/")) { + if (component === "") { + continue; + } + if (component.length > (portable ? 14 : 255)) { + stderr(portable + ? `pathchk: limit 14 exceeded by length ${component.length} of file name component ${localeQuotedDiagnostic(component)}\n` + : `pathchk: ${path}: File name too long\n`); + code = 1; + componentError = true; + break; + } + if (checkEmptyOrLeadingDash && component.startsWith("-")) { + stderr(`pathchk: leading '-' in a component of file name '${path}'\n`); + code = 1; + componentError = true; + break; + } + const nonPortable = portable ? component.match(/[^A-Za-z0-9._-]/)?.[0] : null; + if (nonPortable != null) { + stderr(`pathchk: non-portable character ${localeQuotedDiagnostic(pathchkDiagnosticCharacter(nonPortable))} in file name ${pathchkDiagnosticName(path)}\n`); + code = 1; + componentError = true; + break; + } + } + if (componentError) continue; + if (!portable) { + const prefixError = await pathchkExistingPrefixError(path); + if (prefixError) { + stderr(`pathchk: ${path}: ${prefixError}\n`); + code = 1; + continue; + } + } + } + return code; +} + +export function pathchkDiagnosticCharacter(ch) { + return lsEscapedName(ch); +} + +export function pathchkDiagnosticName(path) { + return shellEscapeLsName(path, true); +} + +export function parsePathchkOptions(args) { + const opts = {}; + const operands = []; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") { + operands.push(...args.slice(i + 1)); + break; + } + if (arg === "-" || !arg.startsWith("-")) { + operands.push(...args.slice(i)); + break; + } + if (arg.startsWith("--")) { + const normalized = normalizePathchkLongOption(arg); + const body = normalized.slice(2); + const eq = body.indexOf("="); + const name = eq === -1 ? body : body.slice(0, eq); + if (!PATHCHK_LONG_OPTIONS.includes(name)) throw new UsageError(`unrecognized option '${arg}'`, true); + if (eq !== -1) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + opts[name] = true; + continue; + } + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (ch === "p" || ch === "P") opts[ch] = true; + else throw new UsageError(`invalid option -- '${ch}'`, true); + } + } + return { opts, operands }; +} + +export function normalizePathchkLongOption(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 = PATHCHK_LONG_OPTIONS.find((option) => option.startsWith(name)); + if (!match) return arg; + return eq === -1 ? `--${match}` : `--${match}=${body.slice(eq + 1)}`; +} + +export async function pathchkExistingPrefixError(path) { + const parts = path.split("/"); + let prefix = path.startsWith("/") ? "/" : ""; + for (let i = 0; i < parts.length - 1; i++) { + const part = parts[i]; + if (part === "") continue; + prefix = prefix === "" || prefix === "/" ? `${prefix}${part}` : `${prefix}/${part}`; + try { + const s = await lstat(prefix); + if (!s.isDirectory()) return "Not a directory"; + } catch (error) { + if (error.code === "ENOENT") return null; + return error.message || String(error); + } + } + return null; +} + +const singleCall = defineCommand("pathchk", pathchk, pathchkMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/pinky.js b/src/commands/pinky.js new file mode 100644 index 0000000..c734aff --- /dev/null +++ b/src/commands/pinky.js @@ -0,0 +1,123 @@ +#!/usr/bin/env bun + +import { userInfo } from "node:os"; +import { join } from "node:path"; +import { normalizeLongOptionByPrefix, normalizeLongOptionsByPrefix, parseOptions, readAll, readPasswd } from "../shared/common.js"; +import { UsageError, stdout } from "../shared/diagnostics.js"; +import { filterWhoUsers, formatWhoDate, readUtmpRecords, whoUsesPosixDateFormat } from "../shared/system.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const PINKY_LONG_OPTIONS = ["lookup", "help", "version"]; + +export async function pinky(args) { + const longOutput = pinkyLongOutputRequested(args); + args = normalizePinkyLongOptions(args); + const { opts, operands } = parseOptions(args, { short: { b: false, f: false, h: false, i: false, l: false, p: false, q: false, s: false, w: false }, long: { lookup: false, help: false, version: false } }); + const info = userInfo(); + const name = info.username; + if (longOutput) { + if (!operands.length) throw new UsageError("no username specified; at least one must be specified when using -l", true); + const passwd = await readPasswd(); + const users = operands; + for (const user of users) { + const row = passwd.get(user); + const gecosName = row?.gecos?.split(",")[0] || (row ? user : "???"); + stdout(`Login name: ${user.padEnd(28)}In real life: ${gecosName}\n`); + if (row && !(opts.b)) stdout(`Directory: ${row.home.padEnd(29)}Shell: ${row.shell}\n`); + if (row && !opts.h) await pinkyPrintProfile("Project: ", row.home, ".project"); + if (row && !opts.p) await pinkyPrintProfile("Plan:\n", row.home, ".plan"); + if (row) stdout("\n"); + } + } else { + const hideName = opts.w || opts.i || opts.q; + const hideIdle = opts.q; + const hideWhere = opts.i || opts.q; + if (!opts.f) { + const columns = ["Login"]; + if (!hideName) columns.push("Name"); + columns.push("TTY"); + if (!hideIdle) columns.push("Idle"); + columns.push("When"); + if (!hideWhere) columns.push("Where"); + stdout(pinkyShortHeader(columns)); + } + const filters = new Set(operands); + if (!filters.size || filters.has(name)) { + const record = (await filterWhoUsers(await readUtmpRecords("/var/run/utmp"), false)).find((entry) => entry.user === name); + stdout(pinkyShortRow({ name, line: record?.line, time: record?.time, showName: !hideName, showIdle: !hideIdle, showWhere: !hideWhere })); + } + } + return 0; +} + +export function pinkyLongOutputRequested(args) { + let longOutput = false; + for (const arg of args) { + if (arg === "--") break; + if (!arg.startsWith("-") || arg === "-") continue; + if (arg.startsWith("--")) continue; + for (const option of arg.slice(1)) { + if (option === "l") longOutput = true; + else if (option === "s") longOutput = false; + } + } + return longOutput; +} + +export async function pinkyPrintProfile(header, home, name) { + try { + const content = await readAll(join(home, name)); + stdout(header); + stdout(content); + } catch { + // GNU pinky treats unavailable profile files as absent. + } +} + +export function pinkyMetaOption(args) { + const shortOptions = new Set(["b", "f", "h", "i", "l", "p", "q", "s", "w"]); + for (const arg of args) { + if (arg === "--") return null; + if (arg.startsWith("--")) { + const normalized = normalizePinkyLongOption(arg); + const name = normalized.slice(2).split("=", 1)[0]; + if (!PINKY_LONG_OPTIONS.includes(name) || normalized.includes("=")) return null; + if (normalized === "--help" || normalized === "--version") return normalized; + continue; + } + if (arg.startsWith("-") && arg !== "-") { + for (let i = 1; i < arg.length; i++) if (!shortOptions.has(arg[i])) return null; + } + } + return null; +} + +export function normalizePinkyLongOptions(args) { + return normalizeLongOptionsByPrefix(args, PINKY_LONG_OPTIONS); +} + +export function normalizePinkyLongOption(arg) { + return normalizeLongOptionByPrefix(arg, PINKY_LONG_OPTIONS); +} + +export function pinkyShortHeader(columns) { + return columns.map((column, index) => { + const widths = { Login: columns.includes("Name") ? 8 : 9, Name: 20, TTY: 8, Idle: 6, When: whoUsesPosixDateFormat() ? 12 : 16, Where: 0 }; + return widths[column] ? column.padEnd(widths[column]) : column; + }).join(" ") + "\n"; +} + +export function pinkyShortRow({ name, line, time, showName, showIdle, showWhere }) { + const parts = [name.padEnd(8)]; + if (showName) parts.push("".padEnd(19)); + parts.push(`?${line ?? "tty"}`.padEnd(9)); + if (showIdle) parts.push("?????".padEnd(6)); + parts.push(time ? formatWhoDate(time) : formatWhoDate(Date.now() / 1000)); + if (showWhere) parts.push(""); + return parts.join(" ").trimEnd() + "\n"; +} + +const singleCall = defineCommand("pinky", pinky, pinkyMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/pr.js b/src/commands/pr.js new file mode 100644 index 0000000..468f63a --- /dev/null +++ b/src/commands/pr.js @@ -0,0 +1,1299 @@ +#!/usr/bin/env bun + +import { closeSync, openSync, readSync } from "node:fs"; +import { stat } from "node:fs/promises"; +import { decodeSurrogateEscapedBytes, isWriteError, localeQuotedEscapedDiagnostic, nodeErrorMessage, normalizeLongOptionByPrefix, parseOptions, readAll, systemErrorMessage, textInputDiagnosticName } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { prInputIsNonRegular } from "../shared/text.js"; +import { strftime } from "../shared/time.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const PR_LONG_OPTIONS = ["across", "columns", "date-format", "double-space", "expand-tabs", "first-line-number", "form-feed", "header", "indent", "join-lines", "length", "merge", "no-file-warnings", "number-lines", "omit-header", "omit-pagination", "output-tabs", "page-width", "pages", "sep-string", "separator", "show-control-chars", "show-nonprinting", "width", "help", "version"]; + +export function prMetaOption(args) { + const longValueOptions = new Set(["columns", "date-format", "first-line-number", "header", "indent", "length", "page-width", "pages", "width"]); + const longOptionalValueOptions = new Set(["expand-tabs", "number-lines", "output-tabs", "sep-string", "separator"]); + const shortValueOptions = new Set(["D", "h", "l", "N", "o", "w", "W"]); + const shortOptionalValueOptions = new Set(["e", "i", "n", "s", "S"]); + const shortKnownOptions = new Set(["a", "b", "c", "d", "e", "f", "F", "h", "i", "J", "l", "m", "n", "N", "o", "r", "s", "S", "t", "T", "v", "w", "W", "D"]); + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (arg.startsWith("--")) { + const option = normalizeLongOptionByPrefix(arg, PR_LONG_OPTIONS); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (!PR_LONG_OPTIONS.includes(name)) return null; + if (name === "help" || name === "version") { + if (inlineValue != null) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + return option; + } + const value = inlineValue ?? (longValueOptions.has(name) ? args[i + 1] : undefined); + if (value !== undefined) validatePrMetaOptionValue(name, value); + if (inlineValue == null && longValueOptions.has(name)) i++; + else if (inlineValue == null && longOptionalValueOptions.has(name)) continue; + else if (inlineValue != null && !longValueOptions.has(name) && !longOptionalValueOptions.has(name)) return null; + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + if (/^-\d+$/.test(arg)) continue; + if (/^-\d/.test(arg)) return null; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (!shortKnownOptions.has(ch)) return null; + if (shortValueOptions.has(ch)) { + const inlineValue = arg.slice(j + 1); + const value = inlineValue === "" ? args[i + 1] : inlineValue; + validatePrMetaShortValue(ch, value); + if (inlineValue === "") i++; + break; + } + if (shortOptionalValueOptions.has(ch)) { + const inlineValue = arg.slice(j + 1); + if (inlineValue !== "") validatePrMetaShortValue(ch, inlineValue); + break; + } + } + } + return null; +} + +export function validatePrMetaOptionValue(name, value) { + if (name === "columns") parsePrColumnCount(value); + else if (name === "first-line-number") parsePrLineNumber(value); + else if (name === "indent") parsePrNonnegativeInteger(value, "-o MARGIN", "line offset"); + else if (name === "page-width") parsePrPositiveInteger(value, "-W PAGE_WIDTH", "characters"); + else if (name === "width") parsePrPositiveInteger(value, "-w PAGE_WIDTH", "characters"); + else if (name === "length") parsePrPositiveInteger(value, "-l PAGE_LENGTH", "lines"); + else if (name === "pages") validatePrPages(value); + else if (name === "expand-tabs") validatePrOptionalNumberSpec(value, "-e"); + else if (name === "number-lines") validatePrOptionalNumberSpec(value, "-n"); + else if (name === "output-tabs") validatePrOptionalNumberSpec(value, "-i"); +} + +export function validatePrMetaShortValue(ch, value) { + if (value === undefined) return; + if (ch === "l") validatePrMetaOptionValue("length", value); + else if (ch === "o") validatePrMetaOptionValue("indent", value); + else if (ch === "w") validatePrMetaOptionValue("width", value); + else if (ch === "W") validatePrMetaOptionValue("page-width", value); + else if (ch === "e") validatePrMetaOptionValue("expand-tabs", value); + else if (ch === "n") validatePrMetaOptionValue("number-lines", value); + else if (ch === "i") validatePrMetaOptionValue("output-tabs", value); +} + +export function validatePrOptionalNumberSpec(value, option) { + const text = String(value); + if (text === "") throw new UsageError(`'${option}': Invalid argument: ${localeQuotedEscapedDiagnostic(text)}`, true); + const numberText = /^\d/.test(text) ? text : text.slice(1); + if (numberText === "") return; + let tooLarge = false; + let valid = /^\d+$/.test(numberText); + if (valid) { + const n = BigInt(numberText); + valid = n > 0n; + tooLarge = n > BigInt(Number.MAX_SAFE_INTEGER); + } + if (!valid || tooLarge) { + const suffix = tooLarge ? ": Value too large for defined data type" : ""; + throw new UsageError(`'${option}' extra characters or invalid number in the argument: ${localeQuotedEscapedDiagnostic(numberText)}${suffix}`, true); + } +} + +export async function prCmd(args) { + args = normalizePrColumnArgs(args); + const { opts, operands } = parseOptions(args, { short: { a: false, b: false, c: false, d: false, e: "optional-value", f: false, F: false, h: "value", i: "optional-value", J: false, l: "value", m: false, n: "optional-value", N: "value", o: "value", r: false, s: "optional-value", S: "optional-value", t: false, T: false, v: false, w: "value", W: "value", D: "value" }, long: { across: false, columns: "value", "date-format": "value", "double-space": false, "expand-tabs": "optional-value", "first-line-number": "value", "form-feed": false, header: "value", indent: "value", "join-lines": false, length: "value", merge: false, "no-file-warnings": false, "number-lines": "optional-value", "omit-header": false, "omit-pagination": false, "output-tabs": "optional-value", "page-width": "value", pages: "value", "sep-string": "optional-value", separator: "optional-value", "show-control-chars": false, "show-nonprinting": false, width: "value" } }); + validatePrPages(opts.pages); + const files = operands.length ? operands : ["-"]; + const numberSpec = opts.n ?? opts["number-lines"]; + const numberLines = numberSpec !== undefined; + const firstLineNumberSpecified = opts.N !== undefined || opts["first-line-number"] !== undefined; + const { separator, width } = parsePrNumberSpec(numberSpec); + let lineNumber = parsePrLineNumber(opts.N ?? opts["first-line-number"] ?? 1); + const indent = parsePrNonnegativeInteger(opts.o ?? opts.indent ?? 0, "-o MARGIN", "line offset"); + const columns = parsePrColumnCount(opts.columns); + const pageWidthText = opts.W ?? opts["page-width"] ?? opts.w ?? opts.width; + const pageWidthLabel = opts.W !== undefined || opts["page-width"] !== undefined ? "-W PAGE_WIDTH" : "-w PAGE_WIDTH"; + const pageWidth = pageWidthText == null ? 72 : parsePrPositiveInteger(pageWidthText, pageWidthLabel, "characters"); + const pageLengthText = opts.l ?? opts.length; + const pageLength = pageLengthText == null ? 66 : parsePrPositiveInteger(pageLengthText, "-l PAGE_LENGTH", "lines"); + const pageRange = parsePrPageRange(opts.pages); + const explicitPageWidth = pageWidthText != null; + const oldSeparatorSpecified = opts.s !== undefined || opts.separator !== undefined; + const newSeparatorSpecified = opts.S !== undefined || opts["sep-string"] !== undefined; + const columnSeparatorSpecified = oldSeparatorSpecified || newSeparatorSpecified; + const columnSeparator = newSeparatorSpecified + ? (opts.S === true || opts["sep-string"] === true ? "" : String(opts.S ?? opts["sep-string"])) + : (opts.s === true || opts.separator === true ? (opts.w ?? opts.width ? "" : "\t") : (typeof (opts.s ?? opts.separator) === "string" ? String(opts.s ?? opts.separator) : " ")); + const omitHeader = opts.t || opts.T || opts["omit-header"] || opts["omit-pagination"] || pageLength <= 10; + const expandTabs = opts.e !== undefined || opts["expand-tabs"] !== undefined; + const tabSpec = parsePrTabSpec(opts.e ?? opts["expand-tabs"]); + const tabSize = tabSpec.size; + const outputTabs = opts.i !== undefined || opts["output-tabs"] !== undefined; + const outputTabSize = parsePrTabSize(opts.i ?? opts["output-tabs"]); + const formFeed = opts.f || opts.F || opts["form-feed"]; + if (opts.m || opts.merge) { + const texts = []; + let failed = false; + for (const file of files) { + let text; + try { + text = decodeSurrogateEscapedBytes(await readAll(file)); + } catch (error) { + if (!(opts.r || opts["no-file-warnings"])) stderr(prReadError(file, error)); + failed = true; + continue; + } + if (expandTabs) text = expandPrTabs(text, tabSize); + if (hasPrNonprintingRendering(opts)) text = renderPrNonprinting(text, opts); + texts.push(text); + } + let pageHeader = null; + if (!omitHeader) { + const headerName = opts.h ?? opts.header ?? ""; + const headerDate = opts.D || opts["date-format"] + ? await prHeaderDate(files[0] ?? "-", opts) + : strftime(new Date(), "%Y-%m-%d %H:%M"); + pageHeader = (page) => formatPrPageHeader(formatPrHeader(headerDate, headerName, pageWidth, page), indent); + } + const mergeJoinLines = opts.J || opts["join-lines"]; + const mergeColumnSeparator = mergeJoinLines && !columnSeparatorSpecified ? "\t" : columnSeparator; + let rendered = renderPrMerge(texts, { columnCount: files.length, columnSeparator: mergeColumnSeparator, defaultColumnSeparator: !columnSeparatorSpecified, explicitPageWidth, formFeed, indent, joinLines: mergeJoinLines, numberLines, numberSkippedLines: !firstLineNumberSpecified, pageHeader, pageLength, pageRange, padColumns: !oldSeparatorSpecified, pageWidth, preserveMergeTabs: oldSeparatorSpecified && !newSeparatorSpecified && columnSeparator === "\t", separator, stripFormFeeds: opts.T || opts["omit-pagination"], width, nextNumber: () => lineNumber++ }); + if (outputTabs) rendered = tabifyPrOutput(rendered, outputTabSize); + stdout(rendered); + return failed ? 1 : 0; + } + let out = ""; + let failed = false; + for (const file of files) { + let text; + const paginatedBody = pageLength != null && !omitHeader; + if (files.length === 1 && prInputIsNonRegular(file) && columns === 1 && paginatedBody && opts.pages == null && !expandTabs && !outputTabs && !hasPrNonprintingRendering(opts) && !(opts.J || opts["join-lines"]) && !oldSeparatorSpecified && !newSeparatorSpecified) { + const headerName = opts.h ?? opts.header ?? ""; + const headerDate = await prHeaderDate(file, opts); + const pageHeader = (page) => formatPrPageHeader(formatPrHeader(headerDate, headerName, pageWidth, page), indent); + let fd = 0; + try { + if (file !== "-") fd = openSync(file, "r"); + streamPrPaginatedSingleColumnFd(fd, { doubleSpace: opts.d || opts["double-space"], explicitPageWidth: effectivePrExplicitPageWidth(explicitPageWidth, false, columns, pageWidth, opts), formFeed, indent, numberLines, numberSkippedLines: !firstLineNumberSpecified, pageHeader, pageLength, pageRange, pageWidth, separator, width, nextNumber: () => lineNumber++ }); + return 0; + } catch (error) { + if (isWriteError(error)) throw error; + if (!(opts.r || opts["no-file-warnings"])) stderr(prReadError(file, error)); + return 1; + } finally { + if (file !== "-" && fd !== 0) closeSync(fd); + } + } + try { + text = decodeSurrogateEscapedBytes(await readAll(file)); + } catch (error) { + if (!(opts.r || opts["no-file-warnings"])) stderr(prReadError(file, error)); + failed = true; + continue; + } + if (text.length === 0) continue; + if (hasPrNonprintingRendering(opts)) text = renderPrNonprinting(text, opts); + if (expandTabs && columns === 1) text = tabSpec.char === "\t" ? expandPrTabs(text, tabSize) : expandPrInputTabs(text, tabSpec.char, tabSize); + else if (expandTabs && tabSpec.char !== "\t") text = translatePrInputTabs(text, tabSpec.char); + let pageHeader = null; + if (!omitHeader) { + const headerName = opts.h ?? opts.header ?? (file === "-" ? "" : file); + const headerDate = await prHeaderDate(file, opts); + pageHeader = (page) => formatPrPageHeader(formatPrHeader(headerDate, headerName, pageWidth, page), indent); + if (!paginatedBody) out += pageHeader(1); + } + const joinLines = opts.J || opts["join-lines"] || (oldSeparatorSpecified && !(opts.w ?? opts.width)); + const effectiveExplicitPageWidth = effectivePrExplicitPageWidth(explicitPageWidth, joinLines, columns, pageWidth, opts); + const bodyColumnSeparator = joinLines && columns > 1 && !columnSeparatorSpecified ? "\t" : columnSeparator; + let rendered = renderPrBody(text, { across: opts.a || opts.across, columns, columnSeparator: bodyColumnSeparator, columnSeparatorSpecified, doubleSpace: opts.d || opts["double-space"], expandTabs, explicitPageWidth: effectiveExplicitPageWidth, formFeed, indent, joinLines, numberLines, numberSkippedLines: !firstLineNumberSpecified, pageHeader, pageLength: omitHeader && pageLengthText == null ? undefined : pageLength, pageRange, pageWidth, padExplicitSeparatorColumns: newSeparatorSpecified, separator, stripFormFeeds: opts.T || opts["omit-pagination"], tabSize, width, nextNumber: () => lineNumber++ }); + if (rendered === "" && opts.pages != null && pageRange.first > 1) stderr(`pr: starting page number ${pageRange.first} exceeds page count 1\n`); + if (outputTabs) rendered = tabifyPrOutput(rendered, outputTabSize); + out += rendered; + if (formFeed && !omitHeader && !paginatedBody) out += "\f"; + if (rendered !== "" && !out.endsWith("\n") && !out.endsWith("\f")) out += "\n"; + } + stdout(out); + return failed ? 1 : 0; +} + +export function prReadError(file, error) { + return file === "-" ? `pr: ${nodeErrorMessage(error)}\n` : `pr: ${textInputDiagnosticName(file)}: ${systemErrorMessage(error)}\n`; +} + +export function hasPrNonprintingRendering(opts) { + return Boolean(opts.c || opts["show-control-chars"] || opts.v || opts["show-nonprinting"]); +} + +export function renderPrNonprinting(text, opts) { + const octal = opts.v || opts["show-nonprinting"]; + let out = ""; + for (const ch of text) { + const code = ch.charCodeAt(0); + if (ch === "\n" || ch === "\t" || ch === "\f") out += ch; + else if (code >= 0xdc80 && code <= 0xdcff) out += `\\${(code - 0xdc00).toString(8).padStart(3, "0")}`; + else if (octal && (code < 32 || code === 127)) out += `\\${code.toString(8).padStart(3, "0")}`; + else if (code < 32) out += `^${String.fromCharCode(code + 64)}`; + else if (code === 127) out += "^?"; + else out += ch; + } + return out; +} + +export function normalizePrColumnArgs(args) { + const normalized = []; + const valueOptions = new Set(["-w", "-W", "-l", "-h", "-N", "-o", "-D", "--width", "--page-width", "--length", "--header", "--first-line-number", "--indent", "--date-format", "--pages", "--columns"]); + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + normalized.push(arg); + if (valueOptions.has(arg)) { + if (i + 1 < args.length) normalized.push(args[++i]); + continue; + } + const compactColumnMatch = String(arg).match(/^-(t|T)(\d+)$/); + if (compactColumnMatch) { + normalized.pop(); + normalized.push(`-${compactColumnMatch[1]}`, `--columns=${compactColumnMatch[2]}`); + continue; + } + const compactObsoleteMatch = String(arg).match(/^-([1-9]\d*)(\D.+|[^\d])$/); + if (compactObsoleteMatch) { + const [, columnText, rest] = compactObsoleteMatch; + const columns = Number(columnText); + if (!Number.isSafeInteger(columns)) throw new UsageError(`invalid number of columns: ${localeQuotedEscapedDiagnostic(columnText)}: Value too large for defined data type`); + if (columns < 1) throw new UsageError(`invalid number of columns: ${localeQuotedEscapedDiagnostic(columnText)}: Numerical result out of range`); + normalized.pop(); + normalized.push(`--columns=${columnText}`, `-${rest}`); + continue; + } + const match = String(arg).match(/^-(\d+)$/); + if (!match) { + const pageMatch = String(arg).match(/^\+(.+)$/); + if (pageMatch) normalized[normalized.length - 1] = `--pages=${pageMatch[1]}`; + continue; + } + const columnText = match[1]; + const columns = Number(columnText); + if (!Number.isSafeInteger(columns)) throw new UsageError(`invalid number of columns: ${localeQuotedEscapedDiagnostic(columnText)}: Value too large for defined data type`); + if (columns < 1) throw new UsageError(`invalid number of columns: ${localeQuotedEscapedDiagnostic(columnText)}: Numerical result out of range`); + normalized[normalized.length - 1] = `--columns=${columnText}`; + } + return normalized; +} + +export function parsePrPositiveInteger(value, label, unit) { + const text = String(value); + if (!/^-?\d+$/.test(text)) throw new UsageError(`'${label}' invalid number of ${unit}: ${localeQuotedEscapedDiagnostic(text)}`); + const n = Number(text); + if (!Number.isSafeInteger(n)) throw new UsageError(`'${label}' invalid number of ${unit}: ${localeQuotedEscapedDiagnostic(text)}: Value too large for defined data type`); + if (n < 1) throw new UsageError(`'${label}' invalid number of ${unit}: ${localeQuotedEscapedDiagnostic(text)}: Numerical result out of range`); + return n; +} + +export function parsePrNonnegativeInteger(value, label, unit) { + const text = String(value); + if (!/^[+-]?\d+$/.test(text)) throw new UsageError(`'${label}' invalid ${unit}: ${localeQuotedEscapedDiagnostic(text)}`); + const n = BigInt(text); + if (n < 0n || n > BigInt(Number.MAX_SAFE_INTEGER)) throw new UsageError(`'${label}' invalid ${unit}: ${localeQuotedEscapedDiagnostic(text)}: Value too large for defined data type`); + return Number(n); +} + +export function parsePrLineNumber(value) { + const text = String(value); + if (!/^[+-]?\d+$/.test(text)) throw new UsageError(`'-N NUMBER' invalid starting line number: ${localeQuotedEscapedDiagnostic(text)}`); + const n = BigInt(text); + if (n < -2147483648n || n > 2147483647n) throw new UsageError(`'-N NUMBER' invalid starting line number: ${localeQuotedEscapedDiagnostic(text)}: Value too large for defined data type`); + return Number(n); +} + +export function parsePrColumnCount(value) { + if (value == null) return 1; + const text = String(value); + if (!/^[+-]?\d+$/.test(text)) throw new UsageError(`invalid number of columns: ${localeQuotedEscapedDiagnostic(text)}`); + const columns = BigInt(text.replace(/^\+/, "")); + if (columns < 1n) throw new UsageError(`invalid number of columns: ${localeQuotedEscapedDiagnostic(text)}: Numerical result out of range`); + if (columns > BigInt(Number.MAX_SAFE_INTEGER)) throw new UsageError(`invalid number of columns: ${localeQuotedEscapedDiagnostic(text)}: Value too large for defined data type`); + return Number(columns); +} + +export function validatePrPages(pages) { + if (pages == null) return; + const text = String(pages); + if (/^\d+[A-Za-z]+$/.test(text) && text.length > 30) throw new UsageError(`invalid suffix in --pages argument '${text}'`); + if (/^\d+$/.test(text) && text.length > 30) throw new UsageError(`--pages argument '${text}' too large`); + if (/^[A-Za-z]+$/.test(text)) throw new UsageError(`invalid --pages argument '${text}'`); + if (/^\d+[A-Za-z]/.test(text) || (/^\d/.test(text) && /[\x00-\x1f\x7f]/.test(text))) throw new UsageError(`invalid page range ${localeQuotedEscapedDiagnostic(text)}`); + if (!/^\d+([:-]\d+)?$/.test(text)) throw new UsageError(`invalid --pages argument '${text}'`); +} + +export function parsePrPageRange(pages) { + if (pages == null) return { first: 1, last: Infinity }; + const [firstText, lastText] = String(pages).split(/[:-]/, 2); + return { first: Number(firstText), last: lastText === undefined ? Infinity : Number(lastText) }; +} + +export function parsePrTabSize(spec) { + if (spec === undefined || spec === true || spec === "") return 8; + const text = String(spec); + const match = text.match(/(\d+)$/); + const size = Number(match?.[1] ?? 8); + return Number.isSafeInteger(size) && size > 0 ? size : 8; +} + +export function parsePrTabSpec(spec) { + const size = parsePrTabSize(spec); + if (spec === undefined || spec === true || spec === "") return { char: "\t", size }; + const text = String(spec); + const match = text.match(/^(.*?)(\d+)$/s); + const marker = match ? match[1] : text; + return { char: marker ? marker[0] : "\t", size }; +} + +export function translatePrInputTabs(text, marker) { + return text.replaceAll(marker, "\t"); +} + +export function expandPrInputTabs(text, marker, markerTabSize) { + let out = ""; + let column = 0; + for (const ch of text) { + if (ch === marker || ch === "\t") { + const size = ch === marker ? markerTabSize : 8; + const spaces = size - (column % size); + out += " ".repeat(spaces); + column += spaces; + } else if (ch === "\b") { + if (column > 0) { + out += ch; + column--; + } + } else { + out += ch; + if (ch === "\n" || ch === "\f") column = 0; + else column++; + } + } + return out; +} + +export function expandPrTabs(text, tabSize) { + let out = ""; + let column = 0; + for (const ch of text) { + if (ch === "\t") { + const spaces = tabSize - (column % tabSize); + out += " ".repeat(spaces); + column += spaces; + } else if (ch === "\b") { + if (column > 0) { + out += ch; + column--; + } + } else { + out += ch; + if (ch === "\n" || ch === "\f") column = 0; + else column++; + } + } + return out; +} + +export function tabifyPrOutput(text, tabSize) { + let out = ""; + let column = 0; + let pendingSpaces = 0; + const flushSpaces = (nextColumn = column + pendingSpaces) => { + while (pendingSpaces > 0) { + const toStop = tabSize - (column % tabSize); + if (pendingSpaces >= toStop && toStop > 1) { + out += "\t"; + column += toStop; + pendingSpaces -= toStop; + } else { + out += " "; + column++; + pendingSpaces--; + } + } + column = nextColumn; + }; + for (const ch of text) { + if (ch === " ") { + pendingSpaces++; + continue; + } + flushSpaces(); + out += ch; + if (ch === "\t") column += tabSize - (column % tabSize); + else if (ch === "\b") column = Math.max(0, column - 1); + else if (ch === "\n" || ch === "\f") column = 0; + else column++; + } + flushSpaces(); + return out; +} + +export async function prHeaderDate(file, opts) { + const format = opts.D ?? opts["date-format"]; + if (!format) { + const date = file === "-" ? new Date() : (await stat(file)).mtime; + return strftime(date, "%Y-%m-%d %H:%M"); + } + const date = file === "-" ? new Date() : (await stat(file)).mtime; + return format.startsWith("+") ? `+${strftime(date, format.slice(1))}` : strftime(date, format); +} + +export function formatPrHeader(dateText, name, pageWidth = 72, page = 1) { + const pageText = `Page ${page}`; + if (pageWidth < dateText.length + String(name).length + pageText.length + 2) return `${dateText} ${name} ${pageText}`; + const nameColumn = dateText.length + Math.floor((pageWidth - pageText.length - dateText.length - String(name).length) / 2); + return `${dateText.padEnd(nameColumn)}${String(name).padEnd(Math.max(1, pageWidth - pageText.length - nameColumn))}${pageText}`; +} + +export function formatPrPageHeader(headerLine, indent = 0) { + const margin = " ".repeat(indent); + return indent > 0 ? `${margin}\n\n${margin}${headerLine}\n\n\n` : `\n\n${headerLine}\n\n\n`; +} + +export function parsePrNumberSpec(spec) { + if (spec === undefined || spec === true) return { separator: "\t", width: 5 }; + const text = String(spec); + const match = text.match(/^(.*?)(\d+)?$/s); + return { separator: match?.[1] || "\t", width: Number(match?.[2] || 5) }; +} + +export function renderPrBody(text, opts) { + if (opts.columns > 1 && text.includes("\f") && !(opts.pageLength != null && opts.pageHeader)) return renderPrColumnGroups(text, opts); + if (opts.pageLength != null && opts.pageHeader) return opts.columns > 1 ? renderPrPaginatedColumns(text, opts) : renderPrPaginatedSingleColumn(text, opts); + const margin = " ".repeat(opts.indent); + const lines = text.split(/(?<=\n)/).filter(Boolean).map((raw) => { + let line = raw.endsWith("\n") ? raw.slice(0, -1) : raw; + if (opts.stripFormFeeds) { + if (/^\f+$/u.test(line)) return null; + line = line.replace(/\f+/gu, "\n"); + } + return opts.explicitPageWidth ? line.slice(0, opts.pageWidth) : line; + }).filter((line) => line != null); + const rendered = opts.columns > 1 ? renderPrColumns(lines, opts) : renderPrSingleColumn(lines, opts); + return opts.doubleSpace ? rendered.replaceAll("\n", "\n\n") : rendered; +} + +export function effectivePrExplicitPageWidth(explicitPageWidth, joinLines, columns, pageWidth, opts) { + const modernPageWidth = opts.W !== undefined || opts["page-width"] !== undefined; + return explicitPageWidth && !joinLines && (columns > 1 || pageWidth < 72 || modernPageWidth); +} + +export function renderPrColumnGroups(text, opts) { + const tokens = tokenizePrPages(text, opts); + const groups = [[]]; + for (const token of tokens) { + if (token.type === "formFeed") groups.push([]); + else groups[groups.length - 1].push(token.line); + } + let out = ""; + for (let i = 0; i < groups.length; i++) { + if (groups[i].length > 0) out += renderPrColumns(groups[i], opts); + if (!opts.stripFormFeeds && i < groups.length - 1) out += "\f"; + } + return opts.doubleSpace ? out.replaceAll("\n", "\n\n") : out; +} + +export function renderPrSingleColumn(lines, opts) { + const margin = " ".repeat(opts.indent); + return lines.map((line) => { + if (/^\f+$/u.test(line)) return opts.stripFormFeeds ? "" : line; + const number = opts.numberLines ? `${formatPrLineNumber(opts.nextNumber(), opts.width)}${opts.separator}` : ""; + if (!opts.stripFormFeeds && line.includes("\f")) { + const rendered = line.replace(/([^\f])(\f+)/gu, "$1\n$2"); + return `${margin}${number}${rendered}${line.endsWith("\f") ? "" : "\n"}`; + } + if (opts.stripFormFeeds && line.endsWith("\n")) return `${margin}${number}${line}`; + return `${margin}${number}${line}\n`; + }).join(""); +} + +export function renderPrPaginatedSingleColumn(text, opts) { + let out = ""; + const state = createPrPaginatedSingleColumnState(opts, (text) => { out += text; }); + for (const token of tokenizePrPages(text, opts)) state.accept(token); + state.finish(); + return out; +} + +export function streamPrPaginatedSingleColumnFd(fd, opts) { + const state = createPrPaginatedSingleColumnState(opts, stdout); + const buffer = Buffer.allocUnsafe(64 * 1024); + if (!opts.explicitPageWidth) { + let lineHasText = false; + let skipNextNewline = false; + while (true) { + const n = readSync(fd, buffer, 0, buffer.length, null); + if (n === 0) break; + const bytes = buffer.subarray(0, n); + let start = 0; + for (let index = 0; index < bytes.length; index++) { + const byte = bytes[index]; + if (byte !== 0x0a && byte !== 0x0c) continue; + if (index > start) { + state.writeLineChunk(bytes.subarray(start, index)); + lineHasText = true; + skipNextNewline = false; + } + if (byte === 0x0a) { + if (skipNextNewline) skipNextNewline = false; + else state.endLine(); + lineHasText = false; + } else { + if (lineHasText) state.endLine(); + lineHasText = false; + state.accept({ type: "formFeed" }); + skipNextNewline = true; + } + start = index + 1; + } + if (start < bytes.length) { + state.writeLineChunk(bytes.subarray(start)); + lineHasText = true; + skipNextNewline = false; + } + } + if (lineHasText) state.endLine(); + state.finish(); + return; + } + let pending = ""; + let skipNextNewline = false; + const pushLine = (line) => state.accept({ type: "line", line: opts.explicitPageWidth ? line.slice(0, opts.pageWidth) : line }); + const processText = (text, final = false) => { + let line = pending; + pending = ""; + for (let i = 0; i < text.length; i++) { + const ch = text[i]; + if (ch === "\n") { + if (skipNextNewline) { + skipNextNewline = false; + continue; + } + pushLine(line); + line = ""; + } else if (ch === "\f") { + if (line.length > 0) pushLine(line); + line = ""; + state.accept({ type: "formFeed" }); + skipNextNewline = true; + } else { + skipNextNewline = false; + line += ch; + } + } + if (final) { + if (line.length > 0) pushLine(line); + } else { + pending = line; + } + }; + while (true) { + const n = readSync(fd, buffer, 0, buffer.length, null); + if (n === 0) break; + processText(decodeSurrogateEscapedBytes(buffer.subarray(0, n))); + } + processText("", true); + state.finish(); +} + +export function createPrPaginatedSingleColumnState(opts, emit) { + const bodyRows = Math.max(1, opts.pageLength - 10); + const margin = " ".repeat(opts.indent); + let page = 1; + let row = 0; + let pageStarted = false; + let lineOpen = false; + const inRange = () => page >= opts.pageRange.first && page <= opts.pageRange.last; + const startPage = () => { + if (!pageStarted && inRange()) emit(opts.pageHeader(page)); + pageStarted = true; + }; + const endPage = () => { + const renderedRows = opts.doubleSpace ? row * 2 : row; + if (pageStarted && inRange()) emit(opts.formFeed ? "\f" : "\n".repeat(Math.max(1, opts.pageLength - 5 - renderedRows))); + page++; + row = 0; + pageStarted = false; + }; + const startLine = () => { + if (lineOpen || page > opts.pageRange.last) return; + if (row >= bodyRows) endPage(); + startPage(); + let number = ""; + if (opts.numberLines && (inRange() || opts.numberSkippedLines)) number = `${formatPrLineNumber(opts.nextNumber(), opts.width)}${opts.separator}`; + if (inRange()) emit(`${margin}${number}`); + lineOpen = true; + }; + const writeLineChunk = (text) => { + if (!text.length) return; + startLine(); + if (lineOpen && inRange()) emit(text); + }; + const endLine = () => { + startLine(); + if (!lineOpen) return; + if (inRange()) emit(opts.doubleSpace ? "\n\n" : "\n"); + row++; + lineOpen = false; + }; + return { + writeLineChunk, + endLine, + accept(token) { + if (page > opts.pageRange.last) return; + if (token.type === "formFeed") { + if (lineOpen) endLine(); + startPage(); + if (opts.formFeed && inRange() && row === 0) emit("\n"); + if (row === 0 && opts.formFeed) row = 1; + endPage(); + return; + } + writeLineChunk(token.line); + endLine(); + }, + finish() { + if (lineOpen) endLine(); + if (pageStarted) endPage(); + }, + }; +} + +export function renderPrPaginatedColumns(text, opts) { + const bodyRows = Math.max(1, opts.pageLength - 10); + const pageCapacity = Math.max(1, bodyRows * opts.columns); + const tokens = tokenizePrPages(text, opts); + const separatorWidth = opts.columnSeparatorSpecified ? opts.columnSeparator.length * (opts.columns - 1) : 0; + const columnWidth = Math.max(1, Math.floor((opts.pageWidth - separatorWidth) / opts.columns)); + const clipWidth = opts.joinLines ? Infinity : Math.max(1, columnWidth - (!opts.columnSeparatorSpecified && (!opts.explicitPageWidth || opts.columns === 2) ? 1 : 0)); + const margin = " ".repeat(opts.indent); + let out = ""; + let page = 1; + let entries = []; + const inRange = () => page >= opts.pageRange.first && page <= opts.pageRange.last; + const makeEntry = (line) => { + const number = opts.numberLines && (inRange() || opts.numberSkippedLines) ? opts.nextNumber() : null; + return { line, number }; + }; + const emitPage = (forceBlankBody = false) => { + if (inRange()) { + out += opts.pageHeader(page); + if (forceBlankBody) out += "\n"; + else out += renderPrColumnPage(entries, { ...opts, bodyRows, clipWidth, columnWidth, margin }); + const renderedRows = forceBlankBody ? 1 : prColumnPageRowCount(entries.length, opts.columns, bodyRows, opts.across); + out += opts.formFeed ? "\f" : "\n".repeat(Math.max(1, opts.pageLength - 5 - renderedRows)); + } + page++; + entries = []; + }; + for (const token of tokens) { + if (page > opts.pageRange.last) break; + if (token.type === "formFeed") { + emitPage(opts.formFeed && entries.length === 0); + continue; + } + if (entries.length >= pageCapacity) emitPage(); + entries.push(makeEntry(token.line)); + } + if (entries.length > 0) emitPage(); + return opts.doubleSpace ? out.replaceAll("\n", "\n\n") : out; +} + +export function renderPrColumnPage(entries, opts) { + const rows = prColumnPageRowCount(entries.length, opts.columns, opts.bodyRows, opts.across); + const out = []; + for (let row = 0; row < rows; row++) { + const parts = []; + for (let column = 0; column < opts.columns; column++) { + const index = opts.across ? row * opts.columns + column : prDownColumnIndex(entries.length, opts.columns, rows, row, column); + const entry = entries[index]; + if (entry === undefined) continue; + const entryClipWidth = opts.numberLines && column > 0 && !opts.columnSeparatorSpecified ? Math.max(1, opts.clipWidth - 1) : opts.clipWidth; + const implicitSeparatorWidth = opts.columnSeparator.length; + const explicitImplicitSeparatorWidth = !opts.columnSeparatorSpecified && opts.columns === 2 ? 0 : implicitSeparatorWidth; + const clipStartColumn = opts.margin.length + (opts.explicitPageWidth || opts.columnSeparatorSpecified ? column * (opts.columnWidth + explicitImplicitSeparatorWidth) : Math.floor((opts.pageWidth * column) / opts.columns)); + let clipped = formatPrColumnEntry(entry, column, { ...opts, compactColumnNumbers: true, paginated: true, clipStartColumn }, entryClipWidth); + if (opts.numberLines && opts.expandTabs && !opts.columnSeparatorSpecified && column < opts.columns - 1) clipped = `${clipped} \t`; + parts.push(opts.joinLines || column === opts.columns - 1 || (opts.numberLines && opts.expandTabs && !opts.columnSeparatorSpecified) + ? clipped + : opts.columnSeparatorSpecified && opts.padExplicitSeparatorColumns + ? padPrColumnEnd(clipped.replace(/\s+$/u, ""), column, { columnWidth: opts.columnWidth, clipWidth: opts.columnWidth, formFeed: opts.formFeed, startOffset: opts.margin.length, separatorWidth: opts.columnSeparator.length, allowUnitTabs: true, disallowSingleColumnTab: true }) + : opts.columnSeparatorSpecified + ? clipped + : (opts.numberLines ? padPrColumnEnd(clipped.replace(/\s+$/u, ""), column, { ...opts, padExtra: 1 }) : padPrDefaultColumnEnd(clipped.replace(/\s+$/u, ""), column, { ...opts, allowUnitTabs: opts.columns === 2 || opts.margin.length > 0, startOffset: opts.margin.length }))); + } + let separator = !opts.columnSeparatorSpecified && !opts.joinLines && (!opts.explicitPageWidth || opts.columns === 2) ? "" : opts.columnSeparator; + if (opts.numberLines && opts.expandTabs && !opts.columnSeparatorSpecified) separator = ""; + if (opts.numberLines && opts.expandTabs && opts.columnSeparatorSpecified) separator += "\t "; + if (parts.length) { + let line = parts.join(separator); + if (!opts.columnSeparatorSpecified && opts.joinLines && !opts.across) line = formatPrStoredJoinLine(parts, opts); + else if (!opts.columnSeparatorSpecified && opts.joinLines) line = tabifyPrSpacesAtGnuOutput(line, opts.margin.length); + else if (!opts.columnSeparatorSpecified && opts.explicitPageWidth) line = tabifyPrSpacesAt(line, opts.margin.length); + const preserveAcrossPadding = opts.across && row < rows - 1 && parts.slice(1).every((part) => !/\S/u.test(part)); + out.push(`${opts.margin}${preserveAcrossPadding ? line : line.replace(/\s+$/u, "")}\n`); + } + } + return out.join(""); +} + +export function prColumnPageRowCount(entryCount, columns, bodyRows, across) { + if (entryCount <= 0) return 0; + if (across) return Math.ceil(entryCount / columns); + if (entryCount >= bodyRows * columns) return bodyRows; + return Math.ceil(entryCount / columns); +} + +export function prDownColumnIndex(entryCount, columns, rows, row, column) { + const full = Math.floor(entryCount / columns); + const extra = entryCount % columns; + const height = full + (column < extra ? 1 : 0); + if (row >= height) return -1; + let offset = 0; + for (let c = 0; c < column; c++) offset += full + (c < extra ? 1 : 0); + return offset + row; +} + +export function tokenizePrPages(text, opts) { + const tokens = []; + let line = ""; + let skipNextNewline = false; + const pushLine = () => { + tokens.push({ type: "line", line: opts.explicitPageWidth ? line.slice(0, opts.pageWidth) : line }); + line = ""; + }; + for (let i = 0; i < text.length; i++) { + const ch = text[i]; + if (ch === "\n") { + if (skipNextNewline) { + skipNextNewline = false; + continue; + } + pushLine(); + } else if (ch === "\f") { + if (line.length > 0) pushLine(); + tokens.push({ type: "formFeed" }); + skipNextNewline = true; + } else { + skipNextNewline = false; + line += ch; + } + } + if (line.length > 0) pushLine(); + return tokens; +} + +export function padPrColumnEnd(text, column, opts) { + const start = (opts.startOffset ?? 0) + column * opts.columnWidth + column * (opts.separatorWidth ?? 1); + const width = column === 0 ? opts.columnWidth : opts.clipWidth; + const target = start + width + (opts.padExtra ?? 0); + let out = text; + let display = prColumnAfter(text, start); + while (display < target) { + const toStop = 8 - (display % 8); + const remaining = target - display; + if ((toStop > 1 || !opts.formFeed || (opts.allowUnitTabs && !(opts.disallowSingleColumnTab && remaining === 1))) && display + toStop <= target) { + out += "\t"; + display += toStop; + } else { + out += " "; + display++; + } + } + return out; +} + +export function renderPrMerge(texts, opts) { + const files = texts.map((text) => { + if (opts.stripFormFeeds) text = text.replaceAll("\f", ""); + return text.split(/(?<=\n)/).filter(Boolean).map((raw) => raw.endsWith("\n") ? raw.slice(0, -1) : raw); + }); + if (opts.pageLength != null && opts.pageHeader) return renderPrPaginatedMerge(files, opts); + const rowCount = Math.max(0, ...files.map((lines) => lines.length)); + const columnCount = opts.columnCount ?? files.length; + const columnSeparator = opts.defaultColumnSeparator && opts.padColumns && !opts.joinLines ? "" : opts.columnSeparator; + const separatorWidth = columnSeparator.length * Math.max(0, columnCount - 1); + const columnWidth = columnCount ? Math.max(1, Math.floor((opts.pageWidth - separatorWidth) / columnCount)) : opts.pageWidth; + const out = []; + for (let row = 0; row < rowCount; row++) { + const parts = []; + let column = opts.indent; + for (let index = 0; index < files.length; index++) { + if (index > 0) column += columnSeparator.length; + let value = files[index][row] ?? ""; + if (!opts.joinLines && index > 0 && !opts.preserveMergeTabs) value = tabifyPrSpacesAt(expandPrTabs(value, 8), column, true); + if (!opts.joinLines && opts.padColumns && index < files.length - 1) value = padPrMergeEnd(value, columnWidth); + parts.push(value); + column = prColumnAfter(value, column); + } + const body = opts.joinLines ? formatPrMergeJoinLine(parts, opts.columnSeparator) : parts.join(columnSeparator); + out.push(`${" ".repeat(opts.indent)}${opts.joinLines ? body : body.slice(0, opts.pageWidth)}\n`); + } + return out.join(""); +} + +export function renderPrPaginatedMerge(files, opts) { + const bodyRows = Math.max(1, opts.pageLength - 10); + const pages = files.map((lines) => paginatePrMergeLines(lines, bodyRows)); + const pageCount = Math.max(0, ...pages.map((filePages) => filePages.length)); + const columnCount = opts.columnCount ?? files.length; + const separatorWidth = opts.columnSeparator.length * Math.max(0, columnCount - 1); + const numberWidth = opts.numberLines ? opts.width + opts.separator.length : 0; + const columnWidth = columnCount ? Math.max(1, Math.floor((opts.pageWidth - numberWidth - separatorWidth) / columnCount)) : opts.pageWidth; + let out = ""; + for (let page = 1; page <= pageCount; page++) { + const pageRows = pages.map((filePages) => filePages[page - 1] ?? []); + const hasFirstFileRows = (pageRows[0] ?? []).some((line) => line !== null); + const visible = page >= opts.pageRange.first && page <= opts.pageRange.last; + if (!visible && opts.numberLines && opts.numberSkippedLines && hasFirstFileRows) { + for (let row = 0; row < bodyRows; row++) if ((pageRows[0] ?? [])[row] != null) opts.nextNumber(); + continue; + } + if (!visible) continue; + out += opts.pageHeader(page); + let emittedRows = 0; + for (let row = 0; row < bodyRows; row++) { + const values = pageRows.map((rows) => rows[row]); + if (!values.some((value) => value != null)) continue; + const number = opts.numberLines ? `${formatPrLineNumber(opts.nextNumber(), opts.width)}${opts.separator}` : ""; + if (opts.joinLines) { + const pageEndMissingRight = values[values.length - 1] == null + && !pageRows.some((rows) => rows.slice(row + 1).some((line) => line != null)); + const bodyValues = pageEndMissingRight && page < pageCount && (!opts.numberLines || values[0] != null) ? values.slice(0, values.reduce((last, value, index) => value == null ? last : index, -1) + 1) : values; + const body = formatPrMergeJoinLine(bodyValues, opts.columnSeparator, opts.indent + prDisplayWidth(number)); + out += `${" ".repeat(opts.indent)}${number}${body}\n`; + emittedRows++; + continue; + } + const clippedParts = values.map((value, index) => { + const startColumn = opts.indent + numberWidth + index * (columnWidth + opts.columnSeparator.length); + const text = tabifyPrSpacesAt(prMergeCellText(value), startColumn, true); + return index === values.length - 1 + ? (opts.numberLines ? clipPrLastMergeColumn(text, columnWidth, startColumn, !opts.padColumns) : clipPrDisplay(text, columnWidth, startColumn).replace(/\s+$/u, "")) + : clipPrDisplay(text, columnWidth, startColumn); + }); + const lastNonNull = values.reduce((last, value, index) => value == null ? last : index, -1); + const pageEndMissingRight = (!opts.padColumns || page === pageCount || values.some((value) => value?.beforeFormFeed || value?.sourceHadFormFeed)) + && values[values.length - 1] == null + && !pageRows.some((rows) => rows.slice(row + 1).some((line) => line != null)); + if (pageEndMissingRight && lastNonNull >= 0 && lastNonNull < values.length - 1) { + const startColumn = opts.indent + numberWidth + lastNonNull * (columnWidth + opts.columnSeparator.length); + const text = tabifyPrSpacesAt(prMergeCellText(values[lastNonNull]), startColumn, true); + clippedParts[lastNonNull] = opts.explicitPageWidth + ? clipPrDisplay(text, columnWidth, startColumn) + : clipPrLastMergeColumn(text, columnWidth, startColumn, true); + const remainingText = text.slice(clippedParts[lastNonNull].length); + if (opts.explicitPageWidth && /^\S/u.test(remainingText)) clippedParts[lastNonNull] = clippedParts[lastNonNull].replace(/\s+$/u, ""); + else if (opts.explicitPageWidth && remainingText.startsWith(" ")) clippedParts[lastNonNull] += " "; + if (!opts.explicitPageWidth && !values[lastNonNull]?.sourceHadFormFeed && !(opts.numberLines && values.some((value) => value?.beforeFormFeed || value?.sourceHadFormFeed)) && !/\s$/u.test(clippedParts[lastNonNull]) && /\S/u.test(text.slice(clippedParts[lastNonNull].length))) { + clippedParts[lastNonNull] += " "; + } + } + const unpadPageEnd = pageEndMissingRight + && (values.some((value) => value?.beforeFormFeed) || prDisplayWidth(clippedParts[lastNonNull].replace(/\s+$/u, "")) >= columnWidth - 1); + const parts = clippedParts.map((clipped, index) => { + if (index >= values.length - 1 || (unpadPageEnd && index === lastNonNull)) return clipped; + return padPrColumnEnd(clipped.replace(/\s+$/u, ""), index, { columnWidth, clipWidth: columnWidth, formFeed: opts.formFeed, startOffset: opts.indent + numberWidth, separatorWidth: opts.columnSeparator.length, allowUnitTabs: true, disallowSingleColumnTab: opts.padColumns && (files.length > 2 || opts.numberLines) }); + }); + const body = (unpadPageEnd ? parts.slice(0, lastNonNull + 1) : parts).join(opts.columnSeparator); + out += `${" ".repeat(opts.indent)}${number}${body}\n`; + emittedRows++; + } + out += opts.formFeed ? `${emittedRows === 0 ? "\n" : ""}\f` : "\n".repeat(Math.max(1, opts.pageLength - 5 - emittedRows)); + } + return out; +} + +export function prMergeCellText(value) { + if (value == null) return ""; + return typeof value === "object" ? value.line : String(value); +} + +export function formatPrMergeJoinLine(values, separator, startColumn = 0) { + const parts = []; + let column = startColumn; + for (let index = 0; index < values.length; index++) { + let text = tabifyPrSpacesAt(prMergeCellText(values[index]), column, true); + if (index === values.length - 1) text = text.replace(/[ \t]+$/u, ""); + parts.push(text); + column = prColumnAfter(text, column); + if (index < values.length - 1) column += separator.length; + } + return parts.join(separator); +} + +export function clipPrLastMergeColumn(text, width, startColumn = 0, preserveSourceContinuation = false) { + const clipped = clipPrDisplay(text, width, startColumn); + if (!/\s$/u.test(clipped)) return clipped; + const lookahead = preserveSourceContinuation ? clipPrDisplay(text, width + 1, startColumn) : clipped; + if (lookahead.length > clipped.length && /\S/u.test(lookahead.slice(clipped.length))) return clipped; + return clipped.replace(/\s+$/u, ""); +} + +export function paginatePrMergeLines(lines, bodyRows) { + const pages = []; + let page = []; + let justFlushed = false; + for (const line of lines) { + const parts = String(line).split("\f"); + for (let i = 0; i < parts.length; i++) { + const part = parts[i]; + let flushedFullPage = false; + if (part !== "" || !String(line).includes("\f")) { + page.push({ line: part, beforeFormFeed: false, sourceHadFormFeed: String(line).includes("\f") }); + if (page.length >= bodyRows) { + pages.push(page); + page = []; + justFlushed = true; + flushedFullPage = true; + } + } + if (i < parts.length - 1) { + if (page.length > 0) page[page.length - 1].beforeFormFeed = true; + if (justFlushed) justFlushed = false; + else pages.push(page); + page = []; + } + if (part !== "" && !flushedFullPage) justFlushed = false; + } + } + if (page.length > 0) pages.push(page); + return pages; +} + +export function renderPrColumns(lines, opts) { + const pageRows = opts.pageLength ?? Math.ceil(lines.length / opts.columns); + const pageCapacity = Math.max(1, pageRows * opts.columns); + const separatorWidth = opts.columnSeparator.length * (opts.columns - 1); + const columnWidth = Math.max(1, Math.floor((opts.pageWidth - separatorWidth) / opts.columns)); + const clipWidth = opts.joinLines ? Infinity : columnWidth; + const out = []; + for (let pageStart = 0, page = 1; pageStart < lines.length; pageStart += pageCapacity, page++) { + if (page < opts.pageRange.first || page > opts.pageRange.last) continue; + const pageLines = lines.slice(pageStart, pageStart + pageCapacity).map((line) => ({ line, number: opts.numberLines ? opts.nextNumber() : null })); + const rows = opts.pageLength ?? Math.ceil(pageLines.length / opts.columns); + for (let row = 0; row < rows; row++) { + const parts = []; + for (let column = 0; column < opts.columns; column++) { + const index = opts.across ? row * opts.columns + column : prDownColumnIndex(pageLines.length, opts.columns, rows, row, column); + const entry = pageLines[index]; + if (entry === undefined) continue; + let clipped = formatPrColumnEntry(entry, column, { ...opts, compactColumnNumbers: true }, clipWidth); + if (opts.numberLines && opts.expandTabs && !opts.columnSeparatorSpecified && column < opts.columns - 1) clipped = `${clipped} \t`; + parts.push(column === opts.columns - 1 || (opts.columnSeparatorSpecified && !opts.padExplicitSeparatorColumns) || (opts.numberLines && opts.expandTabs && !opts.columnSeparatorSpecified) + ? clipped + : opts.columnSeparatorSpecified + ? padPrColumnEnd(clipped.replace(/\s+$/u, ""), column, { columnWidth, clipWidth: columnWidth, formFeed: opts.formFeed, startOffset: opts.indent, separatorWidth: opts.columnSeparator.length, allowUnitTabs: true, disallowSingleColumnTab: true }) + : padPrDefaultColumnEnd(clipped.replace(/\s+$/u, ""), column, { ...opts, columnWidth, allowUnitTabs: opts.allowUnitTabs || opts.numberLines, finalNumberBoundaryTab: opts.numberLines && !opts.columnSeparatorSpecified, padExtra: opts.numberLines && !opts.columnSeparatorSpecified ? 4 : opts.padExtra })); + } + let separator = !opts.columnSeparatorSpecified && !opts.explicitPageWidth ? "" : opts.columnSeparator; + if (opts.numberLines && opts.expandTabs && !opts.columnSeparatorSpecified) separator = ""; + if (opts.numberLines && opts.expandTabs && opts.columnSeparatorSpecified) separator += "\t "; + if (parts.length) out.push(`${" ".repeat(opts.indent)}${parts.join(separator).replace(/\s+$/u, "")}\n`); + } + } + return out.join(""); +} + +export function padPrDefaultColumnEnd(text, column, opts) { + const implicitSeparatorWidth = opts.columnSeparatorSpecified ? 0 : opts.columnSeparator.length; + const implicitSeparatorTrim = opts.explicitPageWidth && !opts.columnSeparatorSpecified ? 1 : 0; + const startOffset = opts.startOffset ?? 0; + const target = opts.explicitPageWidth + ? startOffset + (column + 1) * opts.columnWidth + column * implicitSeparatorWidth + : startOffset + Math.floor((opts.pageWidth * (column + 1)) / opts.columns) - implicitSeparatorTrim + (opts.padExtra ?? 0); + const start = startOffset + (opts.explicitPageWidth ? column * (opts.columnWidth + implicitSeparatorWidth) : Math.floor((opts.pageWidth * column) / opts.columns)); + let out = text; + let display = prColumnAfter(text, start); + while (display < target) { + const toStop = 8 - (display % 8); + if (opts.finalNumberBoundaryTab && display % 8 === 0 && target - display === 8) { + out += " \t"; + display += 8; + continue; + } + if ((toStop > 1 || opts.allowUnitTabs) && display + toStop <= target) { + out += "\t"; + display += toStop; + } else { + out += " "; + display++; + } + } + return out; +} + +export function formatPrColumnEntry(entry, column, opts, columnWidth) { + let text = entry.line; + if (!opts.numberLines) { + if (column > 0 && !opts.columnSeparatorSpecified) text = text.replaceAll("\t", "\t "); + if (opts.paginated && !opts.joinLines) text = tabifyPrSpacesAt(text, opts.clipStartColumn ?? 0, !opts.explicitPageWidth || opts.columnSeparatorSpecified); + return opts.paginated ? finishPrColumnClip(text, columnWidth, opts) : text.slice(0, columnWidth); + } + let clipWidth = columnWidth; + let prefix; + if (opts.expandTabs && opts.indent > 0 && !opts.columnSeparatorSpecified) { + return formatPrIndentedTabbedColumn(entry, column, opts, columnWidth); + } + if (opts.expandTabs && column > 0 && !opts.columnSeparatorSpecified) { + const number = String(entry.number); + prefix = opts.separator === "\t" ? `${number}${" ".repeat(Math.max(1, opts.width - number.length - 1))}` : `${number}${opts.separator}`; + clipWidth = Math.max(1, columnWidth - 4); + } else if (opts.expandTabs && column > 0 && opts.columnSeparatorSpecified) { + const number = String(entry.number); + prefix = `${number}${opts.separator}`; + clipWidth = Math.max(1, columnWidth - 2); + } else { + if (opts.compactColumnNumbers && column > 0 && !opts.columnSeparatorSpecified) { + const number = String(entry.number); + prefix = opts.separator === "\t" ? `${number}${" ".repeat(Math.max(1, opts.width - number.length - 1))}` : `${number}${opts.separator}`; + } else { + prefix = `${formatPrLineNumber(entry.number, opts.width)}${opts.separator}`; + } + } + text = `${prefix}${text}`; + if (opts.numberLines && opts.expandTabs && column > 0 && !opts.columnSeparatorSpecified) text = text.replace(/\t+/g, "\t "); + else if (opts.numberLines && opts.expandTabs && column > 0 && opts.columnSeparatorSpecified) text = text.replace(/\t+/g, "\t "); + if (opts.paginated && opts.columnSeparatorSpecified && !opts.joinLines) text = tabifyPrSpacesAt(text, opts.clipStartColumn ?? 0, true); + return finishPrColumnClip(text, clipWidth, opts); +} + +export function finishPrColumnClip(text, width, opts) { + const startColumn = opts.clipStartColumn ?? 0; + const clipped = clipPrDisplay(text, width, startColumn); + if (!opts.columnSeparatorSpecified || !/\s$/u.test(clipped)) return clipped; + const lookahead = clipPrDisplay(text, width + 1, startColumn); + if (lookahead.length > clipped.length && /\S/u.test(lookahead.slice(clipped.length))) return clipped.replace(/\s+$/u, " "); + return clipped; +} + +export function formatPrIndentedTabbedColumn(entry, column, opts, columnWidth) { + const number = String(entry.number); + const numberPrefix = column === 0 + ? `${formatPrLineNumber(entry.number, opts.width)} ` + : `${" ".repeat(Math.max(1, opts.width - number.length - 1))}${number} `; + const body = formatPrIndentedTabbedBody(entry.line, column, opts.tabSize); + const clipWidth = Math.max(1, columnWidth + (column === 0 ? 3 : -1)); + return clipPrDisplay(`${numberPrefix}${body}`, clipWidth); +} + +export function formatPrIndentedTabbedBody(text, column, tabSize) { + const fields = text.split("\t"); + if (fields.length >= 4 && fields[2] === "") { + const [first, second] = fields; + const rest = fields.slice(3).join("\t"); + if (tabSize === 5) return column === 0 ? `${first}\t ${second}\t\t ${rest}` : `${first} ${second}\t ${rest}`; + return column === 0 ? `${first}\t ${second}\t ${rest}` : `${first} ${second}\t ${rest}`; + } + if (tabSize === 5) { + if (column === 0) return text.replace(/\t\t/g, "\t\t ").replace(/\t(?!\t)/g, "\t "); + return text.replace(/\t+/g, " "); + } + if (column === 0) return text.replace(/\t+/g, "\t "); + return text.replace(/\t+/g, " "); +} + +export function clipPrDisplay(text, width, startColumn = 0) { + let out = ""; + let column = startColumn; + const limit = startColumn + width; + for (const ch of text) { + const nextColumn = ch === "\t" ? column + (8 - (column % 8)) : ch === "\b" ? Math.max(0, column - 1) : column + 1; + if (nextColumn > limit) break; + out += ch; + column = nextColumn; + } + return out; +} + +export function tabifyPrSpacesAt(text, startColumn = 0, allowUnitTabs = false) { + let out = ""; + let column = startColumn; + for (let index = 0; index < text.length;) { + const ch = text[index]; + if (ch !== " ") { + out += ch; + column = ch === "\t" ? column + (8 - (column % 8)) : ch === "\b" ? Math.max(0, column - 1) : column + 1; + index++; + continue; + } + let end = index; + while (end < text.length && text[end] === " ") end++; + let remaining = end - index; + while (remaining > 0) { + const toStop = 8 - (column % 8); + if ((toStop > 1 || (allowUnitTabs && remaining > 1)) && remaining >= toStop) { + out += "\t"; + column += toStop; + remaining -= toStop; + } else { + out += " "; + column++; + remaining--; + } + } + index = end; + } + return out; +} + +export function tabifyPrSpacesAtGnuOutput(text, startColumn = 0) { + let out = ""; + let column = startColumn; + for (let index = 0; index < text.length;) { + const ch = text[index]; + if (ch !== " ") { + out += ch; + column = ch === "\b" ? Math.max(0, column - 1) : column + 1; + index++; + continue; + } + let end = index; + while (end < text.length && text[end] === " ") end++; + let goal = column + end - index; + while (goal - column > 1) { + const next = column + (8 - (column % 8)); + if (next > goal) break; + out += "\t"; + column = next; + } + while (column < goal) { + out += " "; + column++; + } + index = end; + } + return out; +} + +export function formatPrStoredJoinLine(parts, opts) { + let out = ""; + let outputPosition = opts.margin.length; + let spacesNotPrinted = 0; + let separatorsNotPrinted = 0; + const separator = opts.columnSeparator; + const separatorLength = separator.length; + + const printWhiteSpace = () => { + let current = outputPosition; + const goal = current + spacesNotPrinted; + while (goal - current > 1) { + const next = current + (8 - (current % 8)); + if (next > goal) break; + out += "\t"; + current = next; + } + while (current < goal) { + out += " "; + current++; + } + outputPosition = goal; + spacesNotPrinted = 0; + }; + + const printChar = (ch) => { + if (ch === " ") { + spacesNotPrinted++; + return; + } + if (spacesNotPrinted > 0) printWhiteSpace(); + out += ch; + if (ch === "\b") outputPosition = Math.max(0, outputPosition - 1); + else if (isPrPrintable(ch)) outputPosition++; + }; + + const printSeparator = () => { + if (separatorsNotPrinted <= 0) { + if (spacesNotPrinted > 0) printWhiteSpace(); + return; + } + for (; separatorsNotPrinted > 0; separatorsNotPrinted--) { + for (const ch of separator) { + if (ch === " ") { + spacesNotPrinted++; + continue; + } + if (spacesNotPrinted > 0) printWhiteSpace(); + out += ch; + outputPosition++; + } + if (spacesNotPrinted > 0) printWhiteSpace(); + } + }; + + parts.forEach((part, column) => { + const startPosition = column === 0 ? opts.margin.length + separatorLength : opts.margin.length; + if (separatorLength < startPosition) spacesNotPrinted = startPosition - separatorLength - outputPosition; + printSeparator(); + for (const ch of part) printChar(ch); + if (spacesNotPrinted === 0) { + outputPosition = startPosition + prStoredEndPosition(part, opts.tabSize); + if (startPosition - separatorLength === opts.margin.length) outputPosition -= separatorLength; + } + separatorsNotPrinted++; + }); + + return out; +} + +export function prStoredEndPosition(text, tabSize = 8) { + let position = 0; + for (const ch of text) { + if (ch === "\t") position += tabSize - (position % tabSize); + else if (ch === "\b") position = Math.max(0, position - 1); + else if (isPrPrintable(ch) || ch === " ") position++; + } + return position; +} + +export function isPrPrintable(ch) { + const code = ch.codePointAt(0); + return code >= 0x20 && code !== 0x7f; +} + +export function padPrMergeEnd(text, width) { + let out = text; + let displayWidth = prDisplayWidth(text); + while (displayWidth + (8 - (displayWidth % 8)) <= width) { + const spaces = 8 - (displayWidth % 8); + out += "\t"; + displayWidth += spaces; + } + if (displayWidth < width) out += " ".repeat(width - displayWidth); + return out; +} + +export function prDisplayWidth(text) { + let column = 0; + for (const ch of text) { + if (ch === "\t") column += 8 - (column % 8); + else if (ch === "\b") column = Math.max(0, column - 1); + else column++; + } + return column; +} + +export function prColumnAfter(text, startColumn = 0) { + let column = startColumn; + for (const ch of text) { + if (ch === "\t") column += 8 - (column % 8); + else if (ch === "\b") column = Math.max(0, column - 1); + else column++; + } + return column; +} + +export function formatPrLineNumber(value, width) { + const text = String(value); + const clipped = text.length > width ? text.slice(-width) : text; + return clipped.padStart(width); +} + +const singleCall = defineCommand("pr", prCmd, prMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/printenv.js b/src/commands/printenv.js new file mode 100644 index 0000000..95e25e7 --- /dev/null +++ b/src/commands/printenv.js @@ -0,0 +1,83 @@ +#!/usr/bin/env bun + +import { writeEnvironment } from "../shared/common.js"; +import { UsageError, stdout } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const PRINTENV_LONG_OPTIONS = ["null", "help", "version"]; + +export function printenvMetaOption(args) { + for (const arg of args) { + if (arg === "--") return null; + if (arg === "-" || !arg.startsWith("-")) return null; + if (arg.startsWith("--")) { + const option = normalizePrintenvLongOption(arg); + const name = option.slice(2).split("=", 1)[0]; + if (!PRINTENV_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] !== "0") return null; + } + return null; +} + +export async function printenv(args) { + const operands = []; + let sep = "\n"; + let parsing = true; + for (const arg of args) { + if (!parsing) { + operands.push(arg); + } else if (arg === "--") { + parsing = false; + } else if (arg === "-" || !arg.startsWith("-")) { + operands.push(arg); + parsing = false; + } else if (arg.startsWith("--")) { + const option = normalizePrintenvLongOption(arg); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (name === "null") { + if (inlineValue !== undefined) throw new UsageError("option '--null' doesn't allow an argument", true); + sep = "\0"; + } else if (name === "help" || name === "version") { + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + } else { + throw new UsageError(`unrecognized option '${arg}'`, true); + } + } else if (/^-0+$/.test(arg)) { + sep = "\0"; + } else if (arg.startsWith("-0")) { + throw new UsageError(`invalid option -- '${arg[2]}'`, true); + } else if (arg.startsWith("-")) { + throw new UsageError(`invalid option -- '${arg.slice(1, 2)}'`, true); + } else { + operands.push(arg); + } + } + if (!operands.length) { + writeEnvironment(process.env, sep); + return 0; + } + let code = 0; + for (const key of operands) { + if (process.env[key] == null) code = 1; + else stdout(`${process.env[key]}${sep}`); + } + return code; +} + +export function normalizePrintenvLongOption(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 = PRINTENV_LONG_OPTIONS.find((option) => option.startsWith(name)); + if (!match) return arg; + return eq === -1 ? `--${match}` : `--${match}=${body.slice(eq + 1)}`; +} + +const singleCall = defineCommand("printenv", printenv, printenvMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/printf.js b/src/commands/printf.js new file mode 100644 index 0000000..f5092e9 --- /dev/null +++ b/src/commands/printf.js @@ -0,0 +1,599 @@ +#!/usr/bin/env bun + +import { concatBytes, enc, localeQuotedEscapedDiagnostic, pad, shellQuote } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const PRINTF_STOP = "\uE000PRINTF_STOP\uE000"; + +export const PRINTF_RAW_BYTE_BASE = 0xe100; + +export function formatPrintf(format, values, warnings = []) { + return formatPrintfResult(format, values, warnings).text; +} + +export function formatPrintfResult(format, values, warnings = []) { + let sequentialIndex = 0; + let highestReferenced = 0; + let status = 0; + let fatal = false; + let text = ""; + const takeArg = (index) => { + const argIndex = index == null ? sequentialIndex++ : index - 1; + highestReferenced = Math.max(highestReferenced, argIndex + 1); + return { value: values[argIndex], missing: argIndex >= values.length }; + }; + for (let i = 0; i < format.length;) { + if (format[i] === "\\") { + const escape = parsePrintfEscape(format, i); + text += escape.text; + i += escape.length; + continue; + } + if (format[i] !== "%") { + text += format[i++]; + continue; + } + const conv = parsePrintfConversion(format, i); + if (conv.percent) { + text += "%"; + i += conv.length; + continue; + } + let { flags, width, precision, type } = conv; + if (width?.star) { + const parsed = parsePrintfControlNumber(takeArg(width.index).value, warnings, "field width"); + if (!parsed.ok) status = 1; + if (parsed.fatal) fatal = true; + width = parsed.value; + if (width < 0) { + flags += "-"; + width = Math.abs(width); + } + } + if (precision?.star) { + const parsed = parsePrintfControlNumber(takeArg(precision.index).value, warnings, "precision"); + if (!parsed.ok) status = 1; + if (parsed.fatal) fatal = true; + precision = parsed.value < 0 ? undefined : parsed.value; + } + width = width?.value ?? width; + precision = precision?.value ?? precision; + if (width != null) width = Number(width); + if (precision != null) precision = Number(precision); + const arg = takeArg(conv.index); + const value = arg.value ?? ""; + let rendered; + if (type === "b") rendered = String(value).replace(/\\(U[0-9a-fA-F]{8}|u[0-9a-fA-F]{4}|0[0-7]{0,3}|[0-7]{1,3}|x[0-9a-fA-F]{1,2}|[abcefnrtv\\])/g, (_, e) => decodeEscape(e)); + else if (type === "q") rendered = pad(shellQuote(String(value)), width, flags); + else if (type === "s") { + const valueText = precision == null ? String(value) : String(value).slice(0, Number(precision)); + rendered = pad(valueText, width, flags); + } + else if (type === "c") rendered = pad(String(value)[0] ?? "\0", width, flags); + else { + const beforeWarnings = warnings.length; + const n = arg.missing ? 0 : parsePrintfNumber(value, warnings, type); + if (warnings.length !== beforeWarnings) status = 1; + let out; + if ("diuoxX".includes(type)) { + let int = typeof n === "bigint" ? n : BigInt(Math.trunc(n || 0)); + if ("di".includes(type)) { + const min = -(1n << 63n); + const max = (1n << 63n) - 1n; + if (int < min || int > max) { + warnings.push(`printf: ${localeQuotedEscapedDiagnostic(value)}: Numerical result out of range`); + status = 1; + int = int < min ? min : max; + } + } else if (int > (1n << 64n) - 1n) { + warnings.push(`printf: ${localeQuotedEscapedDiagnostic(value)}: Numerical result out of range`); + status = 1; + int = (1n << 64n) - 1n; + } + const unsigned = "uoxX".includes(type) && int < 0n ? BigInt.asUintN(64, int) : null; + const sign = unsigned == null && int < 0n ? "-" : flags.includes("+") && "di".includes(type) ? "+" : flags.includes(" ") && "di".includes(type) ? " " : ""; + const magnitude = unsigned ?? (int < 0n ? -int : int); + const rawDigits = precision === 0 && magnitude === 0n + ? "" + : type === "o" + ? magnitude.toString(8) + : type === "x" + ? magnitude.toString(16) + : type === "X" + ? magnitude.toString(16).toUpperCase() + : String(magnitude); + const digits = applyIntegerPrecision(rawDigits, precision); + const prefix = flags.includes("#") && type === "o" && !digits.startsWith("0") + ? "0" + : flags.includes("#") && type === "x" && magnitude !== 0n + ? "0x" + : flags.includes("#") && type === "X" && magnitude !== 0n + ? "0X" + : ""; + out = formatPrintfInteger(sign, prefix, digits, width, flags, precision); + if (precision != null) flags = flags.replace("0", ""); + } + else out = formatPrintfFloat(n ?? 0, type, precision, flags); + rendered = "diuoxX".includes(type) ? out : pad(out, width, flags); + } + text += rendered; + i += conv.length; + } + return { text: fatal ? "" : text, consumed: Math.max(sequentialIndex, highestReferenced), status, fatal }; +} + +export function parsePrintfEscape(format, offset) { + const rest = format.slice(offset + 1); + const match = rest.match(/^(U[0-9a-fA-F]{8}|u[0-9a-fA-F]{4}|[0-7]{1,3}|x[0-9a-fA-F]{1,2}|[abcefnrtv\\])/); + if (!match) return { text: format[offset], length: 1 }; + return { text: decodeEscape(match[1]), length: 1 + match[1].length }; +} + +export function parsePrintfConversion(format, offset) { + if (format[offset + 1] === "%") return { percent: true, length: 2 }; + if (format[offset + 1] === "(") return { invalid: "%(", length: 2 }; + let i = offset + 1; + let index; + const indexMatch = format.slice(i).match(/^(\d+)\$/); + if (indexMatch) { + index = Number(indexMatch[1]); + i += indexMatch[0].length; + } + const flagsStart = i; + while (/[0 +'#-]/.test(format[i] ?? "")) i++; + const flags = format.slice(flagsStart, i); + let width; + if (format[i] === "*") { + i++; + const widthIndex = format.slice(i).match(/^(\d+)\$/); + if (widthIndex) { + width = { star: true, index: Number(widthIndex[1]) }; + i += widthIndex[0].length; + } else { + width = { star: true }; + } + } else { + const widthMatch = format.slice(i).match(/^\d+/); + if (widthMatch) { + width = { value: widthMatch[0] }; + i += widthMatch[0].length; + } + } + let precision; + if (format[i] === ".") { + i++; + if (format[i] === "*") { + i++; + const precisionIndex = format.slice(i).match(/^(\d+)\$/); + if (precisionIndex) { + precision = { star: true, index: Number(precisionIndex[1]) }; + i += precisionIndex[0].length; + } else { + precision = { star: true }; + } + } else { + const precisionMatch = format.slice(i).match(/^\d*/)[0]; + precision = { value: precisionMatch === "" ? "0" : precisionMatch }; + i += precisionMatch.length; + } + } + if (format[i] === "L") i++; + const type = format[i]; + if (!/[abcdeEfFgiGosuxXAq]/.test(type ?? "")) { + const dollar = format.slice(offset, i + 1).match(/^%.*?\$/); + const invalid = dollar?.[0] ?? (format.slice(offset).match(/^%[^A-Za-z%]*[A-Za-z]/)?.[0] ?? "%"); + return { invalid, length: invalid.length }; + } + return { length: i - offset + 1, index, flags, width, precision, type }; +} + +export function applyIntegerPrecision(digits, precision) { + if (precision == null) return digits; + return digits.padStart(Number(precision), "0"); +} + +export function formatPrintfInteger(sign, prefix, digits, width, flags = "", precision = undefined) { + const value = `${sign}${prefix}${digits}`; + const n = Number(width); + if (!Number.isInteger(n) || value.length >= n) return value; + const padding = " ".repeat(n - value.length); + if (flags.includes("-")) return value + padding; + if (flags.includes("0") && precision == null) return `${sign}${prefix}${"0".repeat(n - value.length)}${digits}`; + return padding + value; +} + +export function formatPrintfFloat(value, type, precision, flags = "") { + const p = precision == null ? 6 : Number(precision); + const uppercase = type === "A" || type === "E" || type === "F" || type === "G"; + if (typeof value === "object" && value?.special === "nan") { + const out = `${value.negative ? "-" : ""}nan`; + return uppercase ? out.toUpperCase() : out; + } + if (typeof value === "object" && value?.special === "decimal-overflow") { + return formatPrintfDecimalOverflow(value, type, p); + } + if (Number.isNaN(value)) return uppercase ? "NAN" : "nan"; + if (!Number.isFinite(value)) { + const out = value < 0 ? "-inf" : "inf"; + return uppercase ? out.toUpperCase() : out; + } + if (type === "e" || type === "E") { + const out = value.toExponential(p).replace(/e([+-])(\d)$/i, "e$10$2"); + return type === "E" ? out.toUpperCase() : out; + } + if (type === "a" || type === "A") return formatPrintfHexFloat(value, type, precision, flags.includes("#")); + if (type === "g" || type === "G") { + const out = value.toPrecision(p).replace(/(\.\d*?)0+(e|$)/i, "$1$2").replace(/\.(e|$)/i, "$1").replace(/e([+-])(\d)$/i, "e$10$2"); + return type === "G" ? out.toUpperCase() : out; + } + return value.toFixed(p); +} + +export function formatPrintfHexFloat(value, type, precision, alternate = false) { + const uppercase = type === "A"; + const negative = Object.is(value, -0) || value < 0; + const abs = Math.abs(value); + if (abs === 0) { + const fraction = precision == null ? "" : "0".repeat(Number(precision)); + const point = alternate || fraction ? "." : ""; + return `${negative ? "-" : ""}${uppercase ? "0X" : "0x"}0${point}${fraction}${uppercase ? "P" : "p"}+0`; + } + const requested = precision == null ? 13 : Number(precision); + let exponent = Math.floor(Math.log2(abs)) - 3; + let scaled = abs / 2 ** exponent; + while (scaled < 8) { + scaled *= 2; + exponent--; + } + while (scaled >= 16) { + scaled /= 2; + exponent++; + } + const rounded = roundedHexFloatDigits(scaled, requested); + let digits = rounded.digits; + if (rounded.carry) exponent++; + let head = digits[0]; + let fraction = digits.slice(1); + if (precision == null) fraction = fraction.replace(/0+$/, ""); + const point = alternate || fraction ? "." : ""; + let out = `${negative ? "-" : ""}0x${head}${point}${fraction}p${exponent < 0 ? "" : "+"}${exponent}`; + return uppercase ? out.toUpperCase() : out; +} + +export function roundedHexFloatDigits(value, precision) { + const count = precision + 1; + const digits = []; + let rest = value; + for (let i = 0; i < count + 1; i++) { + const digit = Math.floor(rest); + digits.push(Math.max(0, Math.min(15, digit))); + rest = (rest - digit) * 16; + } + if (digits[count] >= 8) { + let carry = 1; + for (let i = count - 1; i >= 0 && carry; i--) { + const next = digits[i] + carry; + digits[i] = next & 0xf; + carry = next > 0xf ? 1 : 0; + } + if (carry) { + digits.unshift(1); + digits.length = count; + return { digits: digits.map((digit) => digit.toString(16)).join(""), carry: true }; + } + } + return { digits: digits.slice(0, count).map((digit) => digit.toString(16)).join(""), carry: false }; +} + +export function formatPrintfDecimalOverflow(value, type, precision) { + const significant = type === "e" || type === "E" ? precision + 1 : Math.max(precision, 1); + const rounded = roundDecimalOverflowDigits(value.digits, value.exponent, significant); + const expText = `${rounded.exponent < 0 ? "-" : "+"}${String(Math.abs(rounded.exponent)).padStart(2, "0")}`; + let body; + if (type === "e" || type === "E") { + const rest = rounded.digits.slice(1).padEnd(precision, "0"); + body = `${rounded.digits[0]}${precision > 0 ? `.${rest}` : ""}e${expText}`; + } else { + const rest = rounded.digits.slice(1).replace(/0+$/, ""); + body = `${rounded.digits[0]}${rest ? `.${rest}` : ""}e${expText}`; + } + if (value.negative) body = `-${body}`; + return type === "E" || type === "G" ? body.toUpperCase() : body; +} + +export function roundDecimalOverflowDigits(digits, exponent, significant) { + let kept = digits.slice(0, significant).padEnd(significant, "0"); + const next = Number(digits[significant] ?? "0"); + if (next >= 5) { + let carry = 1; + const chars = [...kept]; + for (let i = chars.length - 1; i >= 0 && carry; i--) { + const n = Number(chars[i]) + carry; + chars[i] = String(n % 10); + carry = n >= 10 ? 1 : 0; + } + kept = carry ? `1${chars.join("")}` : chars.join(""); + if (carry) exponent++; + } + return { digits: kept.slice(0, significant), exponent }; +} + +export function parsePrintfControlNumber(value, warnings, kind = "number") { + if (value === undefined) return { ok: true, value: 0 }; + const text = String(value ?? ""); + const warningCount = warnings.length; + const n = parsePrintfIntegerNumber(text, warnings); + const ok = warnings.length === warningCount; + if (n > 2147483647n || n < -2147483648n) { + warnings.push(kind === "number" ? `printf: invalid number: ${localeQuotedEscapedDiagnostic(text)}` : `printf: invalid ${kind}: ${localeQuotedEscapedDiagnostic(text)}`); + return { ok: false, value: 0, fatal: true }; + } + return { ok, value: Number(n) }; +} + +export function parsePrintfNumber(value, warnings, type = "") { + const text = String(value ?? ""); + if (text.startsWith("'") || text.startsWith('"')) { + const chars = [...text.slice(1)]; + if (chars.length >= 1) { + if (chars.length > 1) warnings.push(`printf: warning: ${chars[1]}: character(s) following character constant have been ignored`); + return chars[0] === "\uFFFD" ? 0xe1 : chars[0].codePointAt(0); + } + warnings.push(`printf: ${localeQuotedEscapedDiagnostic(text)}: expected a numeric value`); + return 0; + } + if ("diuoxX".includes(type)) return parsePrintfIntegerNumber(text, warnings); + if (/^[+-]?(?:inf(?:inity)?|nan)$/i.test(text)) return parsePrintfSpecialFloat(text); + const exactBaseLiteral = text.match(/^[+-]?0[xX][0-9a-fA-F]+$/) || text.match(/^[+-]?0[bB][01]+$/); + if (exactBaseLiteral) return Number(text); + const match = text.match(/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?/); + if (!match) { + warnings.push(`printf: ${localeQuotedEscapedDiagnostic(text)}: expected a numeric value`); + return 0; + } + const n = Number(match[0]); + if (match[0].length !== text.length) warnings.push(`printf: ${localeQuotedEscapedDiagnostic(text)}: value not completely converted`); + if (!Number.isFinite(n) && "eEgG".includes(type)) return parsePrintfDecimalOverflow(match[0]); + return n; +} + +export function parsePrintfDecimalOverflow(text) { + const negative = text.startsWith("-"); + const unsigned = text.replace(/^[+-]/, ""); + const [mantissaText, exponentText = "0"] = unsigned.split(/[eE]/); + const point = mantissaText.indexOf("."); + const fractionalDigits = point === -1 ? 0 : mantissaText.length - point - 1; + let digits = mantissaText.replace(".", "").replace(/^0+/, ""); + if (digits === "") digits = "0"; + let exponent = Number(exponentText) - fractionalDigits + digits.length - 1; + if (digits === "0") exponent = 0; + return { special: "decimal-overflow", negative, digits, exponent }; +} + +export function parsePrintfIntegerNumber(text, warnings) { + const signMatch = text.match(/^[+-]?/)?.[0] ?? ""; + const rest = text.slice(signMatch.length); + let digits; + let base = 10; + let consumed = signMatch.length; + if (/^0[xX]/.test(rest)) { + base = 16; + digits = rest.slice(2).match(/^[0-9a-fA-F]*/)?.[0] ?? ""; + consumed += 2 + digits.length; + if (digits === "") { + warnings.push(`printf: ${localeQuotedEscapedDiagnostic(text)}: value not completely converted`); + return 0; + } + } else if (/^0[bB]/.test(rest)) { + base = 2; + digits = rest.slice(2).match(/^[01]*/)?.[0] ?? ""; + consumed += 2 + digits.length; + if (digits === "") { + warnings.push(`printf: ${localeQuotedEscapedDiagnostic(text)}: value not completely converted`); + return 0; + } + } else if (rest.startsWith("0")) { + base = 8; + digits = rest.match(/^[0-7]*/)?.[0] ?? "0"; + consumed += digits.length; + } else { + digits = rest.match(/^\d*/)?.[0] ?? ""; + consumed += digits.length; + if (digits === "") { + warnings.push(`printf: ${localeQuotedEscapedDiagnostic(text)}: expected a numeric value`); + return 0; + } + } + if (consumed !== text.length) warnings.push(`printf: ${localeQuotedEscapedDiagnostic(text)}: value not completely converted`); + const magnitude = parseBigIntBase(digits || "0", base); + return signMatch === "-" ? -magnitude : magnitude; +} + +export function parseBigIntBase(digits, base) { + let value = 0n; + const bigBase = BigInt(base); + for (const digit of digits) value = value * bigBase + BigInt(Number.parseInt(digit, base)); + return value; +} + +export function parsePrintfSpecialFloat(text) { + if (/nan/i.test(text)) return { special: "nan", negative: text.startsWith("-") }; + return text.startsWith("-") ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY; +} + +export function decodeEscape(escape) { + const simple = { a: "\x07", b: "\b", c: PRINTF_STOP, e: "\x1b", f: "\f", n: "\n", r: "\r", t: "\t", v: "\v", "\\": "\\" }; + if (simple[escape] != null) return simple[escape]; + if (escape.startsWith("x")) return printfRawByte(Number.parseInt(escape.slice(1), 16)); + if (escape.startsWith("u") || escape.startsWith("U")) return String.fromCodePoint(Number.parseInt(escape.slice(1), 16)); + return printfRawByte(Number.parseInt(escape, 8)); +} + +export function printfRawByte(byte) { + return String.fromCharCode(PRINTF_RAW_BYTE_BASE + (byte & 0xff)); +} + +export function encodePrintfOutput(text) { + const chunks = []; + for (const ch of text) { + const code = ch.codePointAt(0); + chunks.push(code >= PRINTF_RAW_BYTE_BASE && code <= PRINTF_RAW_BYTE_BASE + 0xff ? Uint8Array.of(code - PRINTF_RAW_BYTE_BASE) : enc.encode(ch)); + } + return concatBytes(chunks); +} + +export function validatePrintfFormat(format) { + for (let i = 0; i < format.length; i++) { + const ch = format[i]; + if (ch === "\\") { + const rest = format.slice(i + 1); + if (rest.startsWith("x")) { + const hex = rest.slice(1).match(/^[0-9a-fA-F]*/)[0]; + if (hex.length === 0) return { ok: false, message: "printf: missing hexadecimal number in escape" }; + i += 1 + hex.length; + } else if (/^[uU]/.test(rest)) { + const need = rest[0] === "u" ? 4 : 8; + const hex = rest.slice(1, 1 + need); + if (hex.length < need || !/^[0-9a-fA-F]+$/.test(hex)) return { ok: false, message: "printf: missing hexadecimal number in escape" }; + const code = Number.parseInt(hex, 16); + if (code > 0x10ffff || (code >= 0xd800 && code <= 0xdfff)) return { ok: false, message: `printf: invalid universal character name \\${rest[0]}${hex}` }; + i += 1 + need; + } else if (/^[0-7]/.test(rest)) { + i += Math.min(rest.match(/^[0-7]{1,3}/)[0].length, 3); + } else if (rest) { + i++; + } + continue; + } + if (ch !== "%") continue; + const conv = parsePrintfConversion(format, i); + if (conv.invalid) return { ok: false, message: `printf: ${conv.invalid}: invalid conversion specification` }; + if (conv.percent) { + i += conv.length - 1; + continue; + } + const token = format.slice(i, i + conv.length); + const { flags = "", precision, type } = conv; + const invalid = + (flags.includes("'") && !["d", "i", "u", "f", "F", "g", "G"].includes(type)) || + (type === "d" && flags.includes("#")) || + (["b", "c", "q", "s"].includes(type) && flags.includes("0")) || + (type === "c" && precision != null) || + (type === "q" && (flags !== "" || conv.width != null || precision != null)); + if (invalid) return { ok: false, message: `printf: ${token}: invalid conversion specification` }; + i += conv.length - 1; + } + return { ok: true }; +} + +export async function printfCmd(args) { + if (args[0] === "--") args = args.slice(1); + if (!args.length) throw new UsageError("missing operand", true); + const [format, ...values] = args; + const validation = validatePrintfFormat(format); + if (!validation.ok) { + stderr(`${validation.message}\n`); + return 1; + } + const streamedStatus = streamLargeSimplePrintf(format, values); + if (streamedStatus != null) return streamedStatus; + let out = ""; + let stop = false; + const warnings = []; + const append = (text) => { + const idx = text.indexOf(PRINTF_STOP); + if (idx === -1) { + out += text; + return; + } + out += text.slice(0, idx); + stop = true; + }; + if (!values.length) append(formatPrintf(format, [], warnings)); + else if (countPrintfConversions(format) === 0) { + append(formatPrintf(format, [], warnings)); + warnings.push(`printf: warning: ignoring excess arguments, starting with '${values[0]}'`); + } + else { + let status = 0; + for (let i = 0; i < values.length && !stop;) { + const before = i; + const rendered = formatPrintfResult(format, values.slice(i), warnings); + status ||= rendered.status; + append(rendered.text); + i += Math.max(1, rendered.consumed); + if (i === before) break; + } + if (warnings.length) stderr(`${warnings.join("\n")}\n`); + stdout(encodePrintfOutput(out)); + return status; + } + if (warnings.length) stderr(`${warnings.join("\n")}\n`); + stdout(encodePrintfOutput(out)); + return 0; +} + +export function streamLargeSimplePrintf(format, values) { + if (!format.startsWith("%")) return null; + const conversion = parsePrintfConversion(format, 0); + if (conversion.invalid || conversion.percent || conversion.length !== format.length || conversion.index != null) return null; + if (conversion.width?.star || conversion.precision?.star) return null; + const width = Number(conversion.width?.value); + if (!Number.isSafeInteger(width) || width < 1024 * 1024 || conversion.flags?.includes("0")) return null; + const precision = conversion.precision?.value; + const coreFormat = `%${conversion.flags ?? ""}${precision == null ? "" : `.${precision}`}${conversion.type}`; + const warnings = []; + let status = 0; + let index = 0; + const iterations = values.length ? Infinity : 1; + for (let iteration = 0; iteration < iterations && (values.length === 0 ? iteration === 0 : index < values.length); iteration++) { + const rendered = formatPrintfResult(coreFormat, values.slice(index), warnings); + status ||= rendered.status; + if (rendered.fatal) break; + const padding = Math.max(0, width - rendered.text.length); + if (conversion.flags?.includes("-")) { + stdout(encodePrintfOutput(rendered.text)); + writePrintfPadding(padding, 0x20); + } else { + writePrintfPadding(padding, 0x20); + stdout(encodePrintfOutput(rendered.text)); + } + if (!values.length) break; + index += Math.max(1, rendered.consumed); + } + if (warnings.length) stderr(`${warnings.join("\n")}\n`); + return status; +} + +export function writePrintfPadding(length, byte) { + if (length <= 0) return; + const block = Buffer.alloc(Math.min(64 * 1024, length), byte); + let remaining = length; + while (remaining > 0) { + const count = Math.min(block.length, remaining); + stdout(count === block.length ? block : block.subarray(0, count)); + remaining -= count; + } +} + +export function countPrintfConversions(format) { + let count = 0; + for (let i = 0; i < format.length;) { + if (format[i] === "\\") { + i += parsePrintfEscape(format, i).length; + } else if (format[i] === "%") { + const conv = parsePrintfConversion(format, i); + if (!conv.percent && !conv.invalid) count++; + i += conv.length || 1; + } else { + i++; + } + } + return count; +} + +const singleCall = defineCommand("printf", printfCmd, (args) => args.length === 1 && (args[0] === "--help" || args[0] === "--version") ? args[0] : null); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/ptx.js b/src/commands/ptx.js new file mode 100644 index 0000000..8c637c4 --- /dev/null +++ b/src/commands/ptx.js @@ -0,0 +1,390 @@ +#!/usr/bin/env bun + +import { decodeSurrogateEscapedBytes, hasSurrogateEscapedBytes, localeQuotedDiagnostic, localeQuotedEscapedDiagnostic, nodeErrorMessage, normalizeLongOptionByPrefix, parseOptions, readAll, systemErrorMessage, textInputDiagnosticName } from "../shared/common.js"; +import { UsageError, fail, stderr, stdout } from "../shared/diagnostics.js"; +import { writeAll } from "../shared/text.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const PTX_LONG_OPTIONS = ["auto-reference", "break-file", "flag-truncation", "format", "ignore-case", "ignore-file", "macro-name", "only-file", "references", "right-side-refs", "sentence-regexp", "traditional", "typeset-mode", "word-regexp", "width", "gap-size", "help", "version"]; + +export function ptxMetaOption(args) { + const longValueOptions = new Set(["break-file", "flag-truncation", "format", "ignore-file", "macro-name", "only-file", "sentence-regexp", "word-regexp", "width", "gap-size"]); + const shortValueOptions = new Set(["b", "F", "M", "S", "W", "g", "i", "o", "w"]); + const shortKnownOptions = new Set(["A", "b", "F", "G", "M", "O", "R", "S", "T", "W", "f", "g", "i", "o", "r", "t", "w"]); + const normalized = normalizePtxLongOptions(args); + for (let i = 0; i < normalized.length; i++) { + const arg = normalized[i]; + if (arg === "--") return null; + if (arg.startsWith("--")) { + const [name, inlineValue] = arg.slice(2).split(/=(.*)/s, 2); + if (!PTX_LONG_OPTIONS.includes(name)) return null; + if ((arg === "--help" || arg === "--version") && inlineValue == null) return arg; + if (longValueOptions.has(name)) { + const value = inlineValue ?? normalized[i + 1]; + if (value !== undefined) validatePtxMetaOptionValue(name, value); + if (inlineValue == null) i++; + } else if (inlineValue != null) return null; + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (!shortKnownOptions.has(ch)) return null; + if (shortValueOptions.has(ch)) { + const inlineValue = arg.slice(j + 1); + const value = inlineValue === "" ? normalized[i + 1] : inlineValue; + validatePtxMetaShortValue(ch, value); + if (inlineValue === "") i++; + break; + } + } + } + return null; +} + +export function validatePtxMetaOptionValue(name, value) { + if (name === "format") parsePtxFormat(value); + if (name === "width") parsePtxPositiveInteger(value, "line width"); + if (name === "gap-size") parsePtxPositiveInteger(value, "gap width"); +} + +export function validatePtxMetaShortValue(ch, value) { + if (value === undefined) return; + if (ch === "w") validatePtxMetaOptionValue("width", value); + else if (ch === "g") validatePtxMetaOptionValue("gap-size", value); +} + +export function normalizePtxLongOptions(args) { + const out = []; + const valueOptions = new Set(["break-file", "flag-truncation", "format", "ignore-file", "macro-name", "only-file", "sentence-regexp", "word-regexp", "width", "gap-size"]); + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") { + out.push(arg, ...args.slice(i + 1)); + break; + } + if (!arg.startsWith("--")) { + out.push(arg); + continue; + } + const normalized = normalizeLongOptionByPrefix(arg, PTX_LONG_OPTIONS); + out.push(normalized); + const [name, inlineValue] = normalized.slice(2).split(/=(.*)/s, 2); + if (valueOptions.has(name) && inlineValue == null && i + 1 < args.length) out.push(args[++i]); + } + return out; +} + +export async function ptx(args) { + args = normalizePtxLongOptions(args); + const { opts, operands } = parseOptions(args, { short: { A: false, b: "value", F: "value", G: false, M: "value", O: false, R: false, S: "value", T: false, W: "value", f: false, g: "value", i: "value", o: "value", r: false, t: false, w: "value" }, long: { "auto-reference": false, "break-file": "value", "flag-truncation": "value", format: "value", "ignore-case": false, "ignore-file": "value", "macro-name": "value", "only-file": "value", "right-side-refs": false, "sentence-regexp": "value", "typeset-mode": false, "word-regexp": "value", references: false, traditional: false, width: "value", "gap-size": "value", help: false, version: false } }); + const traditional = opts.G || opts.traditional; + const output = traditional && operands.length > 1 ? operands[1] : null; + if (output != null) { + const status = await writePtxOutput(output, ""); + if (status !== 0) return status; + } + if (traditional && operands.length > 2) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[2])}`, true); + const files = traditional ? [operands[0] ?? "-"] : (operands.length ? operands : ["-"]); + const ignoreCase = opts.f || opts["ignore-case"]; + let ignoreWords; + let onlyWords; + let breakCharacters = null; + try { + ignoreWords = await readPtxWordList(opts.i ?? opts["ignore-file"], ignoreCase); + onlyWords = await readPtxWordList(opts.o ?? opts["only-file"], ignoreCase); + const breakFile = opts.b ?? opts["break-file"]; + if (breakFile) breakCharacters = decodeSurrogateEscapedBytes(await readAll(breakFile)); + } catch (error) { + const file = error.file ?? (opts.b ?? opts["break-file"]); + stderr(`ptx: ${file ? `${textInputDiagnosticName(file)}: ` : ""}${systemErrorMessage(error)}\n`); + return 1; + } + const format = opts.O ? "roff" : opts.T ? "tex" : traditional ? "roff" : opts.format == null ? undefined : parsePtxFormat(opts.format); + const macroName = opts.M ?? opts["macro-name"] ?? "xx"; + const width = parsePtxPositiveInteger(opts.w ?? opts.width ?? (opts.t || opts["typeset-mode"] || opts.G || opts.traditional ? "100" : "72"), "line width"); + const gap = parsePtxPositiveInteger(opts.g ?? opts["gap-size"] ?? "3", "gap width"); + const truncation = opts.F ?? opts["flag-truncation"] ?? "/"; + let sentenceRegex = null; + if (opts.S || opts["sentence-regexp"]) { + const sentence = opts.S ?? opts["sentence-regexp"]; + try { + sentenceRegex = new RegExp(sentence, "gu"); + if (sentenceRegex.test("")) { + stderr("ptx: regular expression has length zero\n"); + return 1; + } + } catch {} + } + let wordRegex; + try { + wordRegex = opts.W || opts["word-regexp"] + ? new RegExp(opts.W ?? opts["word-regexp"], "gu") + : breakCharacters == null ? /[\p{L}\p{N}_]+/gu : ptxBreakCharacterRegex(breakCharacters); + } catch { + return 0; + } + const rows = []; + const texts = []; + const manualReferences = opts.r || opts.references; + for (const file of files) { + let text; + try { + text = decodeSurrogateEscapedBytes(await readAll(file)); + } catch (error) { + stderr(file === "-" ? `ptx: ${nodeErrorMessage(error)}\n` : `ptx: ${textInputDiagnosticName(file)}: ${systemErrorMessage(error)}\n`); + return 1; + } + if (manualReferences) { + const prepared = ptxExtractLineReferences(text, wordRegex); + for (let line = 0; line < prepared.records.length; line++) { + texts.push({ file, text: prepared.records[line].text, references: [prepared.records[line].reference], lineOffset: line }); + } + } else { + for (const record of ptxSplitSentences(text, sentenceRegex)) { + texts.push({ file, text: record.text, references: [], lineOffset: record.lineOffset }); + } + } + } + for (const { file, text, references, lineOffset } of texts) { + const tokens = []; + for (const match of text.matchAll(wordRegex)) { + const word = match[0]; + const key = ignoreCase ? word.toLowerCase() : word; + const recordLine = text.slice(0, match.index).split("\n").length; + const line = recordLine + lineOffset; + const automaticReference = opts.A || opts["auto-reference"]; + const autoReference = automaticReference ? `${file === "-" ? "" : file}:${line}:` : (references[recordLine - 1] ?? ""); + const referencePadding = automaticReference ? 1 : Math.max(0, 3 - autoReference.length); + tokens.push({ word, key, indexed: !ignoreWords.has(key) && (!onlyWords.size || onlyWords.has(key)), start: match.index, end: match.index + word.length, source: text, autoReference, referencePadding }); + } + for (let index = 0; index < tokens.length; index++) { + if (!tokens[index].indexed) continue; + rows.push({ tokens, index, key: tokens[index].key, order: index }); + } + } + rows.sort((a, b) => comparePtxKeys(a.key, b.key) || a.order - b.order); + const out = rows.map((row) => renderPtxRow(row, { format, width, gap, macroName, truncation, rightSideRefs: opts.R || opts["right-side-refs"] })).join(""); + if (output != null) return await writePtxOutput(output, out); + else stdout(out); + return 0; +} + +export async function writePtxOutput(file, text) { + try { + await writeAll(file, text); + return 0; + } catch (error) { + return fail("ptx", `${file}: ${systemErrorMessage(error)}`); + } +} + +export function comparePtxKeys(a, b) { + const left = Buffer.from(String(a)); + const right = Buffer.from(String(b)); + const n = Math.min(left.length, right.length); + for (let i = 0; i < n; i++) if (left[i] !== right[i]) return left[i] - right[i]; + return left.length - right.length; +} + +export function renderPtxRow(row, opts) { + const token = row.tokens[row.index]; + const keyword = token.word; + const minWidth = opts.gap <= 1 ? 2 : 6; + const halfWidth = Math.floor(Math.max(opts.width, minWidth) / 2); + const source = token.source; + if (source != null && hasSurrogateEscapedBytes(source)) { + const before = source.slice(0, token.start).replace(/\n$/u, "").trimEnd(); + const after = source.slice(token.end).replace(/\n$/u, "").trimEnd(); + if (before) return ptxApplyAutoReference(`${" ".repeat(Math.max(0, halfWidth - before.length))}${before}${" ".repeat(opts.gap)}${keyword}${after}\n`, token.autoReference, opts, token.referencePadding); + return ptxApplyAutoReference(`${" ".repeat(halfWidth + opts.gap)}${keyword}${after}\n`, token.autoReference, opts, token.referencePadding); + } + const wrap = ptxWrapContext(row, source); + const before = wrap.before; + const after = wrap.after; + const rightStartsWithPunctuation = /^[^\s\p{L}\p{N}_]/u.test(source.slice(token.end)); + const right = after ? `${rightStartsWithPunctuation ? "" : " "}${after}` : ""; + const initialSentenceIndent = before === "" && /^\s/u.test(source) ? 1 : 0; + if (opts.format === "roff") return ptxApplyAutoReference(`.${opts.macroName ?? "xx"} "${wrap.beforeRef}" "${before}" "${keyword}${right}" "${wrap.afterRef}"\n`, token.autoReference, opts, token.referencePadding); + if (opts.format === "tex") return ptxApplyAutoReference(`\\xx {${wrap.beforeRef}}{${before}}{${keyword}}{${right}}{${wrap.afterRef}}\n`, token.autoReference, opts, token.referencePadding); + const fitted = opts.width >= 20 && opts.width < 24 ? renderPtxFittedRow(row, { ...opts, halfWidth, keyword }) : null; + if (fitted) return ptxApplyAutoReference(fitted, token.autoReference, opts, token.referencePadding); + if (wrap.beforeRef) { + const prefixLead = opts.gap; + const leftText = before + ? `${" ".repeat(prefixLead)}${wrap.beforeRef}${" ".repeat(Math.max(1, halfWidth - wrap.beforeRef.length - before.length - prefixLead))}${before}` + : `${" ".repeat(prefixLead)}${wrap.beforeRef}${" ".repeat(Math.max(0, halfWidth - wrap.beforeRef.length - prefixLead))}`; + return ptxApplyAutoReference(`${leftText}${" ".repeat(opts.gap)}${keyword}${right}\n`, token.autoReference, opts, token.referencePadding); + } + if (before) { + const spaces = Math.max(0, halfWidth - before.length); + const full = `${" ".repeat(spaces)}${before}${" ".repeat(opts.gap)}${keyword}${right}`; + if (wrap.afterRef) return ptxApplyAutoReference(`${full}${" ".repeat(Math.max(1, opts.width + opts.gap - full.length - wrap.afterRef.length))}${wrap.afterRef}\n`, token.autoReference, opts, token.referencePadding); + if (before.length + opts.gap + keyword.length <= halfWidth + 1) return ptxApplyAutoReference(`${full}\n`, token.autoReference, opts, token.referencePadding); + return ptxApplyAutoReference(`${" ".repeat(halfWidth)}/${" ".repeat(opts.gap)}${keyword}${right}\n`, token.autoReference, opts, token.referencePadding); + } + const full = `${" ".repeat(halfWidth + opts.gap + initialSentenceIndent)}${keyword}${right}`; + if (wrap.afterRef) return ptxApplyAutoReference(`${full}${" ".repeat(Math.max(1, opts.width + opts.gap - full.length - wrap.afterRef.length))}${wrap.afterRef}\n`, token.autoReference, opts, token.referencePadding); + if (!right || halfWidth >= keyword.length + right.length + 2) return ptxApplyAutoReference(`${full}\n`, token.autoReference, opts, token.referencePadding); + return ptxApplyAutoReference(`${" ".repeat(halfWidth + opts.gap + initialSentenceIndent)}${keyword}/\n`, token.autoReference, opts, token.referencePadding); +} + +export function ptxApplyAutoReference(line, reference, opts = {}, referencePadding = 1) { + if (!reference) return line; + const newline = line.endsWith("\n") ? "\n" : ""; + const body = newline ? line.slice(0, -1) : line; + if (opts.rightSideRefs) { + const displayReference = reference.endsWith(":") ? reference.slice(0, -1) : reference; + const leading = body.match(/^ */)?.[0].length ?? 0; + return `${body.slice(Math.min(leading, opts.gap)).padEnd(opts.width + opts.gap)}${displayReference}${newline}`; + } + const leading = body.match(/^ */)?.[0].length ?? 0; + return `${reference}${body.slice(Math.min(leading, reference.length + referencePadding))}${newline}`; +} + +export function ptxExtractLineReferences(text, wordRegex) { + const references = []; + const lines = String(text).split(/(?<=\n)/u); + const records = lines.map((line) => { + const first = [...line.matchAll(wordRegex)][0]; + if (!first) { + references.push(""); + return { text: line, reference: "" }; + } + references.push(first[0]); + return { text: `${line.slice(0, first.index)}${line.slice(first.index + first[0].length).replace(/^[ \t]*/u, "")}`, reference: first[0] }; + }); + return { records, references }; +} + +export function ptxSplitSentences(text, sentenceRegex) { + if (!sentenceRegex) return [{ text, lineOffset: 0 }]; + const records = []; + let start = 0; + let lineOffset = 0; + sentenceRegex.lastIndex = 0; + for (const match of text.matchAll(sentenceRegex)) { + const end = match.index + match[0].length; + if (end <= start) continue; + records.push({ text: text.slice(start, end), lineOffset }); + lineOffset += (text.slice(start, end).match(/\n/gu) ?? []).length; + start = end; + } + if (start < text.length || records.length === 0) records.push({ text: text.slice(start), lineOffset }); + return records; +} + +export function ptxBreakCharacterRegex(characters) { + const escaped = [...new Set([...String(characters), "\n"])].map((character) => character.replace(/[\\\]\[\-^]/gu, "\\$&")).join(""); + return new RegExp(`[^${escaped}]+`, "gu"); +} + +export function ptxWrapContext(row, source) { + const token = row.tokens[row.index]; + let before = ptxContextText(source.slice(0, token.start)); + let after = ptxContextText(source.slice(token.end)); + let beforeRef = ""; + let afterRef = ""; + const hasRecordBreak = source.replace(/\n+$/u, "").includes("\n"); + if (!hasRecordBreak || row.tokens.length < 2) return { before, after, beforeRef, afterRef }; + const first = row.tokens[0].word; + const last = row.tokens.at(-1).word; + if (row.index === row.tokens.length - 1) { + before = ptxRemoveFirstWord(before); + afterRef = first; + } else if (row.index === 0) { + beforeRef = last; + after = ptxRemoveLastWord(after); + } else if (row.index === 1 && source.slice(token.end).includes("\n")) { + beforeRef = last; + after = ptxRemoveLastWord(after); + } + return { before, after, beforeRef, afterRef }; +} + +export function ptxRemoveFirstWord(text) { + return String(text).trim().replace(/^\S+\s*/u, ""); +} + +export function ptxRemoveLastWord(text) { + return String(text).trim().replace(/\s*\S+$/u, ""); +} + +export function renderPtxFittedRow(row, opts) { + const index = row.index; + const left = ptxFittedLeftContext(row.tokens.slice(0, index).map((token) => token.word), opts.halfWidth, opts.truncation); + const keyafterWidth = opts.halfWidth - 2 * opts.truncation.length; + const right = ptxFittedRightContext(row.tokens.slice(index + 1).map((token) => token.word), keyafterWidth - opts.keyword.length, opts.truncation); + const hasSourceContext = left || right; + if (!hasSourceContext) return null; + const markerPadding = left === opts.truncation && opts.truncation.length > 1 ? 1 : 0; + const leftPad = left ? Math.max(0, opts.halfWidth - left.length + markerPadding) : opts.halfWidth + opts.gap; + const gap = left ? " ".repeat(opts.gap) : ""; + return `${" ".repeat(leftPad)}${left}${gap}${opts.keyword}${right}\n`; +} + +export function ptxFittedLeftContext(words, halfWidth, truncation = "/") { + if (!words.length) return ""; + if (truncation.length > 1) return truncation; + const all = words.join(" "); + if (words.length === 1 && all.length <= halfWidth) return all; + if (all.length <= halfWidth - Math.max(1, truncation.length + 4)) return all; + const nearest = words.at(-1); + if (!nearest) return ""; + const truncated = `${truncation}${nearest}`; + if (truncated.length <= halfWidth) return truncated; + return truncation; +} + +export function ptxFittedRightContext(words, available, truncation = "/") { + if (!words.length) return ""; + if (available <= 0) return truncation; + const out = []; + let used = 0; + for (const word of words) { + const extra = (out.length ? 1 : 1) + word.length; + if (used + extra > available) break; + out.push(word); + used += extra; + } + if (!out.length) return truncation; + return ` ${out.join(" ")}${out.length < words.length ? truncation : ""}`; +} + +export function ptxContextText(text) { + return String(text).trim().replace(/\s+/gu, " "); +} + +export function parsePtxPositiveInteger(value, name) { + const text = String(value); + if (!/^\d+$/.test(text) || BigInt(text) <= 0n || BigInt(text) > BigInt(Number.MAX_SAFE_INTEGER)) { + throw new UsageError(`invalid ${name}: ${localeQuotedEscapedDiagnostic(text)}`); + } + return Number(text); +} + +export function parsePtxFormat(value) { + const text = String(value); + const matches = ["roff", "tex"].filter((format) => format.startsWith(text)); + if (matches.length === 1 && text !== "") return matches[0]; + const kind = text === "" || matches.length > 1 ? "ambiguous" : "invalid"; + throw new UsageError(`${kind} argument ${localeQuotedEscapedDiagnostic(text)} for ${localeQuotedDiagnostic("--format")}\nValid arguments are:\n - ${localeQuotedDiagnostic("roff")}\n - ${localeQuotedDiagnostic("tex")}`, true); +} + +export async function readPtxWordList(file, ignoreCase = false) { + if (!file) return new Set(); + let bytes; + try { + bytes = await readAll(file); + } catch (error) { + error.file = file; + throw error; + } + const text = decodeSurrogateEscapedBytes(bytes); + return new Set((text.match(/[\p{L}\p{N}_]+/gu) ?? []).map((word) => ignoreCase ? word.toLowerCase() : word)); +} + +const singleCall = defineCommand("ptx", ptx, ptxMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/pwd.js b/src/commands/pwd.js new file mode 100644 index 0000000..177b089 --- /dev/null +++ b/src/commands/pwd.js @@ -0,0 +1,123 @@ +#!/usr/bin/env bun + +import { realpath } from "node:fs/promises"; +import { isAbsolute } from "node:path"; +import { parseOptions } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { processCwdOrNull } from "../shared/filesystem.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const PWD_LONG_OPTIONS = ["logical", "physical", "help", "version"]; + +export function pwdMetaOption(args) { + for (const arg of args) { + if (arg === "--") return null; + if (!arg.startsWith("-") || arg === "-") continue; + if (arg.startsWith("--")) { + const option = normalizePwdLongOption(arg); + const name = option.slice(2).split("=", 1)[0]; + if (!PWD_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] !== "L" && arg[j] !== "P") return null; + } + return null; +} + +export async function pwd(args) { + args = normalizePwdLongOptions(args); + const requestedMode = pwdRequestedMode(args); + const { operands } = parseOptions(args, { short: { L: false, P: false }, long: { logical: false, physical: false, help: false, version: false } }); + if (operands.length) stderr("pwd: ignoring non-option arguments\n"); + const wrapperPwd = syntacticallyValidLogicalPwd(process.env.BNU_LONG_PWD); + if (wrapperPwd != null) { + stdout(`${wrapperPwd}\n`); + return 0; + } + const cwd = processCwdOrNull(); + if (cwd == null) { + const logical = syntacticallyValidLogicalPwd(process.env.PWD); + if (logical != null) { + stdout(`${logical}\n`); + return 0; + } + throw new UsageError("failed to find current directory"); + } + const physical = await realpath(cwd); + const useLogical = requestedMode === "logical" || (requestedMode == null && process.env.POSIXLY_CORRECT); + if (useLogical) { + const logical = await validLogicalPwd(process.env.PWD, physical); + stdout(`${logical ?? physical}\n`); + } else { + stdout(`${physical}\n`); + } + return 0; +} + +export function pwdRequestedMode(args) { + let mode = null; + let end = false; + for (const arg of args) { + if (end) continue; + if (arg === "--") { + end = true; + continue; + } + if (arg === "-" || !arg.startsWith("-")) continue; + if (arg.startsWith("--")) { + if (arg === "--logical") mode = "logical"; + else if (arg === "--physical") mode = "physical"; + continue; + } + for (let j = 1; j < arg.length; j++) { + if (arg[j] === "L") mode = "logical"; + else if (arg[j] === "P") mode = "physical"; + } + } + return mode; +} + +export function normalizePwdLongOptions(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(normalizePwdLongOption(arg)); + } + return out; +} + +export function normalizePwdLongOption(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 = PWD_LONG_OPTIONS.find((option) => option.startsWith(name)); + if (!match) return arg; + return eq === -1 ? `--${match}` : `--${match}=${body.slice(eq + 1)}`; +} + +export function syntacticallyValidLogicalPwd(value) { + if (!value || !isAbsolute(value)) return null; + if (value.split("/").some((part) => part === "." || part === "..")) return null; + return value; +} + +export async function validLogicalPwd(value, physical) { + if (syntacticallyValidLogicalPwd(value) == null) return null; + try { + return await realpath(value) === physical ? value : null; + } catch { + return null; + } +} + +const singleCall = defineCommand("pwd", pwd, pwdMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/readlink.js b/src/commands/readlink.js new file mode 100644 index 0000000..8a2ac5e --- /dev/null +++ b/src/commands/readlink.js @@ -0,0 +1,81 @@ +#!/usr/bin/env bun + +import { readlink } from "node:fs/promises"; +import { normalizeLongOptionByPrefix, normalizeLongOptionsByPrefix, parseOptions } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { canonicalPath, readlinkErrorMessage, readlinkQuotedName } from "../shared/paths.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const READLINK_LONG_OPTIONS = ["canonicalize", "canonicalize-existing", "canonicalize-missing", "no-newline", "quiet", "silent", "verbose", "zero", "help", "version"]; + +export function readlinkMetaOption(args) { + const shortKnownOptions = new Set(["f", "e", "m", "n", "q", "s", "v", "z"]); + for (const arg of args) { + if (arg === "--") return null; + if (arg.startsWith("--")) { + const option = normalizeReadlinkLongOption(arg); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (!READLINK_LONG_OPTIONS.includes(name)) return null; + if ((option === "--help" || option === "--version") && inlineValue == null) return option; + if (inlineValue != null) return null; + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + for (let j = 1; j < arg.length; j++) if (!shortKnownOptions.has(arg[j])) return null; + } + return null; +} + +export async function readlinkCmd(args) { + args = normalizeReadlinkLongOptions(args); + const { opts, operands } = parseOptions(args, { short: { f: false, e: false, m: false, n: false, q: false, s: false, v: false, z: false }, long: { canonicalize: false, "canonicalize-existing": false, "canonicalize-missing": false, "no-newline": false, quiet: false, silent: false, verbose: false, zero: false, help: false, version: false } }); + if (!operands.length) throw new UsageError("missing operand", true); + const sep = opts.n || opts["no-newline"] ? (operands.length > 1 ? (opts.z || opts.zero ? "\0" : "\n") : "") : (opts.z || opts.zero ? "\0" : "\n"); + const diagnostics = readlinkDiagnosticMode(args); + let status = 0; + for (const file of operands) { + let out; + try { + if (opts.f || opts.e || opts.m || opts.canonicalize || opts["canonicalize-existing"] || opts["canonicalize-missing"]) { + out = await canonicalPath(file, opts); + } else { + out = await readlink(file); + } + stdout(out + sep); + } catch (error) { + if (diagnostics === "verbose") stderr(`readlink: ${readlinkQuotedName(file)}: ${readlinkErrorMessage(error)}\n`); + status = 1; + } + } + return status; +} + +export function normalizeReadlinkLongOptions(args) { + return normalizeLongOptionsByPrefix(args, READLINK_LONG_OPTIONS); +} + +export function normalizeReadlinkLongOption(arg) { + return normalizeLongOptionByPrefix(arg, READLINK_LONG_OPTIONS); +} + +export function readlinkDiagnosticMode(args) { + if (process.env.POSIXLY_CORRECT) return "verbose"; + let mode = "quiet"; + for (const arg of args) { + if (arg === "--") break; + if (arg === "-q" || arg === "-s" || arg === "--quiet" || arg === "--silent") mode = "quiet"; + else if (arg === "-v" || arg === "--verbose") mode = "verbose"; + else if (/^-[^-]/.test(arg)) { + for (const ch of arg.slice(1)) { + if (ch === "q" || ch === "s") mode = "quiet"; + else if (ch === "v") mode = "verbose"; + } + } + } + return mode; +} + +const singleCall = defineCommand("readlink", readlinkCmd, readlinkMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/realpath.js b/src/commands/realpath.js new file mode 100644 index 0000000..263dc97 --- /dev/null +++ b/src/commands/realpath.js @@ -0,0 +1,95 @@ +#!/usr/bin/env bun + +import { stat } from "node:fs/promises"; +import { isAbsolute, relative, resolve } from "node:path"; +import { normalizeLongOptionByPrefix, normalizeLongOptionsByPrefix, parseOptions } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { canonicalPath, readlinkErrorMessage, readlinkQuotedName } from "../shared/paths.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const REALPATH_LONG_OPTIONS = ["canonicalize", "canonicalize-existing", "canonicalize-missing", "logical", "physical", "quiet", "relative-to", "relative-base", "strip", "no-symlinks", "zero", "help", "version"]; + +export function realpathMetaOption(args) { + const longValueOptions = new Set(["relative-to", "relative-base"]); + const shortKnownOptions = new Set(["E", "e", "m", "L", "P", "q", "s", "z"]); + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (arg.startsWith("--")) { + const option = normalizeRealpathLongOption(arg); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (!REALPATH_LONG_OPTIONS.includes(name)) return null; + if ((option === "--help" || option === "--version") && inlineValue == null) return option; + if (inlineValue == null && longValueOptions.has(name)) i++; + else if (inlineValue != null && !longValueOptions.has(name)) return null; + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + for (let j = 1; j < arg.length; j++) if (!shortKnownOptions.has(arg[j])) return null; + } + return null; +} + +export async function realpathCmd(args) { + args = normalizeRealpathLongOptions(args); + const { opts, operands } = parseOptions(args, { short: { E: false, e: false, m: false, L: false, P: false, q: false, s: false, z: false }, long: { canonicalize: false, "canonicalize-existing": false, "canonicalize-missing": false, logical: false, physical: false, quiet: false, strip: false, "no-symlinks": false, zero: false, "relative-to": "value", "relative-base": "value", help: false, version: false } }); + if (!operands.length) throw new UsageError("missing operand", true); + if (opts["relative-to"] === "" || opts["relative-base"] === "") throw new UsageError("'': No such file or directory"); + if (opts.E) opts.e = opts["canonicalize-existing"] = false; + if (opts.e || opts["canonicalize-existing"]) { + await requireRealpathRelativeDirectory(opts["relative-to"]); + await requireRealpathRelativeDirectory(opts["relative-base"]); + } + const sep = opts.z || opts.zero ? "\0" : "\n"; + let status = 0; + for (const file of operands) { + try { + if (file === "") throw Object.assign(new Error("No such file or directory"), { code: "ENOENT" }); + stdout(formatRealpathOutput(await canonicalPath(file, opts), opts) + sep); + } catch (error) { + if (!(opts.q || opts.quiet)) stderr(`realpath: ${readlinkQuotedName(file)}: ${readlinkErrorMessage(error)}\n`); + status = 1; + } + } + return status; +} + +export async function requireRealpathRelativeDirectory(path) { + if (!path) return; + const s = await stat(path).catch((error) => { + throw new UsageError(`${readlinkQuotedName(path)}: ${readlinkErrorMessage(error)}`); + }); + if (!s.isDirectory()) throw new UsageError(`${readlinkQuotedName(path)}: Not a directory`); +} + +export function normalizeRealpathLongOptions(args) { + return normalizeLongOptionsByPrefix(args, REALPATH_LONG_OPTIONS); +} + +export function normalizeRealpathLongOption(arg) { + return normalizeLongOptionByPrefix(arg, REALPATH_LONG_OPTIONS); +} + +export function formatRealpathOutput(path, opts) { + const relativeBase = opts["relative-base"]; + const relativeTo = opts["relative-to"] ?? (relativeBase != null ? relativeBase : null); + if (relativeBase) { + const base = resolve(relativeBase); + const target = resolve(relativeTo); + if (!pathIsUnder(target, base)) return path; + if (pathIsUnder(path, base)) return relative(target, path) || "."; + return path; + } + if (relativeTo) return relative(resolve(relativeTo), path) || "."; + return path; +} + +export function pathIsUnder(path, base) { + const rel = relative(base, path); + return rel === "" || (!rel.startsWith("..") && !isAbsolute(rel)); +} + +const singleCall = defineCommand("realpath", realpathCmd, realpathMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/rm.js b/src/commands/rm.js new file mode 100644 index 0000000..5250ccb --- /dev/null +++ b/src/commands/rm.js @@ -0,0 +1,498 @@ +#!/usr/bin/env bun + +import { FFIType, linkSymbols, ptr, read, toArrayBuffer } from "bun:ffi"; +import { unlinkSync, writeFileSync } from "node:fs"; +import { unlink as fsUnlink, lstat, rmdir } from "node:fs/promises"; +import { isAbsolute, basename as pathBasename, dirname as pathDirname } from "node:path"; +import { changeDirectory, cstrPath, invalidOptionMessage, libc, libcErrno, localeQuotedDiagnostic, localeQuotedEscapedDiagnostic, normalizeLongOptionByPrefix, parseOptions, pathDisplayName, pathLikeJoin, rawCommandArgs, shellEscapeLsName } from "../shared/common.js"; +import { InvocationError, UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { confirmRemoval, errnoMessage, isRecursiveRootTarget, isWriteProtected, lstatSyncNoThrow, preserveRootMessage, processCwdOrNull, rawArgLooksLikeOption, rmLibcError } from "../shared/filesystem.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const RM_LONG_OPTIONS = ["force", "interactive", "recursive", "dir", "verbose", "preserve-root", "no-preserve-root", "-presume-input-tty", "one-file-system", "help", "version"]; + +export function rmMetaOption(args) { + const longFlagOptions = new Set(["force", "recursive", "dir", "verbose", "no-preserve-root", "-presume-input-tty", "one-file-system"]); + for (const arg of args) { + if (arg === "--") return null; + rejectAbbreviatedRmNoPreserveRoot(arg); + let normalized = arg; + if (arg.startsWith("--")) normalized = normalizeLongOptionByPrefix(arg, RM_LONG_OPTIONS); + const [name, inlineValue] = normalized.startsWith("--") ? normalized.slice(2).split(/=(.*)/s, 2) : [null, undefined]; + if (name === "help" || name === "version") { + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + return normalized; + } + if (longFlagOptions.has(name)) { + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + continue; + } + if (name === "interactive") { + if (inlineValue !== undefined) validateRmInteractiveValue(inlineValue); + continue; + } + if (name === "preserve-root") { + if (inlineValue !== undefined) validateRmPreserveRootValue(inlineValue); + continue; + } + if (/^-[firdvIR]+$/.test(arg)) continue; + if (arg.startsWith("-")) throw rmInvalidOptionUsageError(arg, args); + continue; + } + return null; +} + +export let rmDirectoryApi; + +export function rmInterposedDirectoryApi() { + if (!process.env.LD_PRELOAD) { + return { + opendir: libc.symbols.opendir, + readdir: libc.symbols.readdir, + closedir: libc.symbols.closedir, + }; + } + if (rmDirectoryApi) return rmDirectoryApi; + const definitions = { + opendir: { args: [FFIType.cstring], returns: FFIType.ptr }, + readdir: { args: [FFIType.ptr], returns: FFIType.ptr }, + closedir: { args: [FFIType.ptr], returns: FFIType.i32 }, + }; + const symbols = {}; + for (const [name, definition] of Object.entries(definitions)) { + const address = libc.symbols.dlsym(0, ptr(Buffer.from(`${name}\0`))); + symbols[name] = { ptr: address, ...definition }; + } + rmDirectoryApi = linkSymbols(symbols).symbols; + return rmDirectoryApi; +} + +export function rmReadDirectoryEntries(path) { + return [...rmIterateDirectoryEntries(path)]; +} + +export function rmIterateDirectoryEntries(path) { + const api = rmInterposedDirectoryApi(); + const directory = api.opendir(cstrPath(path)); + if (!directory) throw rmLibcError(libcErrno()); + return (function* () { + let yielded = 0; + try { + while (true) { + const errnoPointer = libc.symbols.__errno_location(); + if (errnoPointer) libc.symbols.memset(errnoPointer, 0, 4); + const address = api.readdir(directory); + if (!address) { + const errno = libcErrno(); + if (errno) { + const error = rmLibcError(errno); + error.rmPartialTraversal = yielded > 0; + throw error; + } + break; + } + // Linux struct dirent places d_name immediately after d_type at byte + // 19. Copy it before the next call reuses libc's dirent storage. + const recordLength = read.u16(address, 16); + const maximumNameLength = Math.max(0, recordLength - 19); + let nameLength = 0; + while (nameLength < maximumNameLength && read.u8(address + 19, nameLength) !== 0) nameLength++; + const name = Buffer.from(new Uint8Array(toArrayBuffer(address + 19, 0, nameLength))); + if (name.equals(Buffer.from(".")) || name.equals(Buffer.from(".."))) continue; + yielded++; + yield name; + } + } finally { + api.closedir(directory); + } + })(); +} + +export function rmPrimeDirectoryEntries(entries) { + const first = entries.next(); + return (function* () { + if (!first.done) yield first.value; + yield* entries; + })(); +} + +export async function rmCmd(args) { + for (const arg of args) { + if (arg === "--") break; + rejectAbbreviatedRmNoPreserveRoot(arg); + } + args = normalizeRmArgs(args); + let parsed; + try { + parsed = parseOptions(args, { short: { f: false, i: false, I: false, r: false, R: false, d: false, v: false }, long: { force: false, interactive: "optional-value", recursive: false, dir: false, verbose: false, "preserve-root": "optional-value", "no-preserve-root": false, "-presume-input-tty": false, "one-file-system": false, help: false, version: false } }); + } catch (error) { + const hint = rmDashFileHint(args); + if (hint && error instanceof UsageError && error.showHelp) throw new UsageError(`${error.message}\n${hint}`, true); + throw error; + } + const { opts, operands } = parsed; + validateRmInteractive(opts); + validateRmPreserveRoot(opts); + if (!operands.length) { + if (opts.f || opts.force) return 0; + throw new UsageError("missing operand", true); + } + const preserveRoot = !opts["no-preserve-root"]; + const options = { + force: opts.f || opts.force, + recursive: opts.r || opts.R || opts.recursive, + dir: opts.d || opts.dir, + verbose: opts.v || opts.verbose, + preserveRoot, + preserveRootAll: preserveRoot && opts["preserve-root"] === "all", + oneFileSystem: opts["one-file-system"], + interactive: rmInteractiveMode(args, opts), + presumeInputTty: opts["-presume-input-tty"], + }; + if (options.interactive === "once" && (operands.length > 3 || options.recursive)) { + const suffix = options.recursive ? " recursively" : ""; + if (!confirmRemoval(`rm: remove ${operands.length} argument${operands.length === 1 ? "" : "s"}${suffix}? `)) return 0; + } + const rawOperands = rawRmOperands(operands); + let failed = false; + for (let i = 0; i < operands.length; i++) { + const target = rawOperands?.[i] ?? operands[i]; + if (options.recursive && options.preserveRoot && await isRecursiveRootTarget(target)) { + stderr(preserveRootMessage("rm", operands[i])); + failed = true; + continue; + } + if (options.recursive && !options.preserveRoot && process.env.BNU_RM_TEST_INTERCEPT_FILE && await isRecursiveRootTarget(target)) { + rmTestInterceptRemoval(); + } + const targetOptions = { ...options }; + if (options.recursive && (options.oneFileSystem || options.preserveRootAll)) { + targetOptions.rootDev = await rmRootDevice(target); + if (targetOptions.preserveRootAll && targetOptions.rootDev != null && await rmIsDifferentDeviceFromParent(target, targetOptions.rootDev)) { + stderr(rmDifferentDeviceMessage(rmDisplayPath(operands[i]))); + stderr("rm: and --preserve-root=all is in effect\n"); + failed = true; + continue; + } + } + if (!await removePath(target, targetOptions, rmDisplayPath(operands[i]), true)) failed = true; + } + return failed ? 1 : 0; +} + +export function rejectAbbreviatedRmNoPreserveRoot(arg) { + if (!arg.startsWith("--") || arg.includes("=")) return; + const name = arg.slice(2); + if (name !== "no-preserve-root" && "no-preserve-root".startsWith(name)) { + throw new InvocationError("you may not abbreviate the --no-preserve-root option", 1, false); + } +} + +export function normalizeRmArgs(args) { + const out = []; + let end = false; + for (const arg of args) { + if (end || arg === "--") { + out.push(arg); + if (arg === "--") end = true; + continue; + } + out.push(arg.startsWith("--") ? normalizeLongOptionByPrefix(arg, RM_LONG_OPTIONS) : arg); + } + return out; +} + +export function rawRmOperands(operands) { + if (!operands.some((operand) => operand.includes("\uFFFD"))) return null; + const raw = rawCommandArgs("rm"); + if (!raw) return null; + const parsed = []; + for (let i = 0; i < raw.length; i++) { + const arg = raw[i]; + if (arg.equals(Buffer.from("--"))) { + parsed.push(...raw.slice(i + 1)); + break; + } + if (!rawArgLooksLikeOption(arg)) { + parsed.push(arg); + continue; + } + const text = arg.toString(); + if (text === "--interactive" || text === "-I") continue; + if (text === "--") continue; + if (text.startsWith("--interactive=")) continue; + } + return parsed.length === operands.length ? parsed : null; +} + +export function validateRmInteractive(opts) { + const mode = opts.interactive === true ? "always" : opts.interactive; + if (mode == null) return; + validateRmInteractiveValue(mode); +} + +export function validateRmInteractiveValue(mode) { + if (!["always", "yes", "once", "no", "never", "none"].includes(mode)) { + const kind = mode === "" ? "ambiguous" : "invalid"; + throw new UsageError(`${kind} argument ${localeQuotedEscapedDiagnostic(mode)} for ${localeQuotedDiagnostic("--interactive")}\nValid arguments are:\n - ${["never", "no", "none"].map(localeQuotedDiagnostic).join(", ")}\n - ${localeQuotedDiagnostic("once")}\n - ${["always", "yes"].map(localeQuotedDiagnostic).join(", ")}`, true); + } +} + +export function validateRmPreserveRoot(opts) { + const value = opts["preserve-root"]; + if (value == null || value === true) return; + validateRmPreserveRootValue(value); +} + +export function validateRmPreserveRootValue(value) { + if (value !== "all") throw new UsageError(`unrecognized --preserve-root argument: '${value}'`); +} + +export function rmInteractiveMode(args, opts) { + let mode = "never"; + for (const arg of args) { + if (arg === "--") break; + if (arg === "-f" || arg === "--force") mode = "never"; + else if (arg === "-i") mode = "always"; + else if (arg === "-I") mode = "once"; + else if (arg === "--interactive") mode = "always"; + else if (arg.startsWith("--interactive=")) mode = arg.slice("--interactive=".length); + else if (/^-[^-]/.test(arg)) { + for (const ch of arg.slice(1)) { + if (ch === "f") mode = "never"; + else if (ch === "i") mode = "always"; + else if (ch === "I") mode = "once"; + } + } + } + if ((opts.f || opts.force) && mode === "never") return "never"; + return mode; +} + +export async function removePath(path, options, display = path, isRoot = false) { + if (options.recursive && isDotOrDotDotPath(path)) { + stderr(`rm: refusing to remove '.' or '..' directory: skipping ${rmQuotedName(path)}\n`); + return false; + } + let s; + try { + s = await lstat(path); + } catch (error) { + if (options.force && (error?.code === "ENOENT" || error?.code === "ENOTDIR")) return true; + stderr(rmErrorLine(display, error)); + return false; + } + if (s.isDirectory()) { + if (!options.recursive && !options.dir) { + stderr(`rm: cannot remove ${rmQuotedName(display)}: Is a directory\n`); + return false; + } + if (options.recursive && options.oneFileSystem && !isRoot && options.rootDev != null && s.dev !== options.rootDev) { + stderr(rmDifferentDeviceMessage(display)); + return false; + } + if (options.recursive) { + if (isRoot && options.interactive === "always" && !confirmRemoval(`rm: descend into directory ${rmQuotedName(display)}? `)) return true; + let entries; + try { + entries = options.interactive === "always" + ? rmReadDirectoryEntries(path) + : rmPrimeDirectoryEntries(rmIterateDirectoryEntries(path)); + } catch (error) { + if (options.force && (error?.code === "EACCES" || error?.code === "EPERM")) { + try { + await rmdir(path); + if (options.verbose) stdout(`removed directory ${rmQuotedName(display)}\n`); + return true; + } catch {} + } + if (error.rmPartialTraversal) stderr(`rm: traversal failed: ${pathDisplayName(display)}: ${errnoMessage(error)}\n`); + else stderr(rmErrorLine(path, error)); + return false; + } + if (!isRoot && entries.length > 0 && options.interactive === "always" && !confirmRemoval(`rm: descend into directory ${rmQuotedName(display)}? `)) return true; + let ok = true; + try { + const chdirName = rmRelativeTraversalName(path, isRoot); + if (chdirName != null) { + const restoreCwd = isRoot ? processCwdOrNull() : null; + try { + changeDirectory(chdirName); + } catch (error) { + stderr(rmErrorLine(display, error)); + return false; + } + try { + for (const entry of entries) { + const childDisplay = `${display.replace(/\/+$/, "")}/${pathDisplayName(entry)}`; + if (rmFastUnlinkEntry(entry, options)) continue; + if (!await removePath(entry, options, childDisplay, false)) ok = false; + } + } finally { + changeDirectory(restoreCwd ?? ".."); + } + } else { + for (const entry of entries) { + const child = pathLikeJoin(path, entry); + const childDisplay = `${display.replace(/\/+$/, "")}/${pathDisplayName(entry)}`; + if (rmFastUnlinkEntry(child, options)) continue; + if (!await removePath(child, options, childDisplay, false)) ok = false; + } + } + } catch (error) { + if (error.rmPartialTraversal) stderr(`rm: traversal failed: ${pathDisplayName(display)}: ${errnoMessage(error)}\n`); + else stderr(rmErrorLine(path, error)); + return false; + } + if (!ok) return false; + } + if (shouldPromptForRemoval(options, s) && !confirmRemoval(`rm: ${rmDirectoryPromptAction(s)} ${rmQuotedName(display)}? `)) return true; + try { + rmTestInterceptRemoval(); + await rmdir(path); + if (options.verbose) stdout(`removed directory ${rmQuotedName(display)}\n`); + return true; + } catch (error) { + stderr(rmErrorLine(display, error, error?.code !== "ENOTEMPTY")); + return false; + } + } + if (shouldPromptForRemoval(options, s) && !confirmRemoval(`rm: remove ${rmPromptDescription(s)} ${rmQuotedName(display)}? `)) return true; + try { + rmTestInterceptRemoval(); + await fsUnlink(path); + if (options.verbose) stdout(`removed ${rmQuotedName(display)}\n`); + return true; + } catch (error) { + stderr(rmErrorLine(display, error)); + return false; + } +} + +export function rmFastUnlinkEntry(path, options) { + rmTestInterceptRemoval(); + if (process.env.LD_PRELOAD || options.verbose || options.interactive === "always" || options.presumeInputTty) return false; + try { + unlinkSync(path); + return true; + } catch { + // Directories, mount points, immutable files, and permission failures + // require the full metadata-aware recursive/diagnostic path below. + return false; + } +} + +export function rmTestInterceptRemoval() { + const evidence = process.env.BNU_RM_TEST_INTERCEPT_FILE; + if (!evidence) return; + writeFileSync(evidence, "removal attempted\n"); + process.exit(0); +} + +export async function rmRootDevice(path) { + try { + return (await lstat(path)).dev; + } catch { + return null; + } +} + +export async function rmIsDifferentDeviceFromParent(path, rootDev) { + const parent = pathDirname(String(path).replace(/\/+$/, "") || "."); + try { + return (await lstat(parent)).dev !== rootDev; + } catch { + return false; + } +} + +export function rmDifferentDeviceMessage(path) { + return `rm: skipping ${rmQuotedName(path)}, since it's on a different device\n`; +} + +export function rmFileTypeDescription(s) { + if (s.isSymbolicLink()) return "symbolic link"; + if (s.isFIFO()) return "fifo"; + if (s.isDirectory()) return "directory"; + return s.size === 0 ? "regular empty file" : "regular file"; +} + +export function rmPromptDescription(s) { + const type = rmFileTypeDescription(s); + return s.isSymbolicLink() || (s.mode & 0o200) ? type : `write-protected ${type}`; +} + +export function shouldPromptForRemoval(options, s) { + if (options.interactive === "always") return true; + if (options.presumeInputTty && isWriteProtected(s)) return true; + return options.interactive === "once" && isWriteProtected(s); +} + +export function rmDirectoryPromptAction(s) { + if (!(s.mode & 0o500)) return "attempt removal of inaccessible directory"; + return `remove ${rmPromptDescription(s)}`; +} + +export function rmDisplayPath(path) { + return String(path).replace(/\/+$/, "/"); +} + +export function rmRelativeTraversalName(path, isRoot = false) { + const text = pathDisplayName(path); + if (isRoot && isAbsolute(text)) return null; + if (text.includes("\uFFFD") || /[\uDC80-\uDCFF]/.test(text) || text === "" || text === "." || text === "..") return null; + return text; +} + +export function isDotOrDotDotPath(path) { + const stripped = String(path).replace(/\/+$/, ""); + const base = pathBasename(stripped); + return base === "." || base === ".."; +} + +export function rmErrorLine(path, error, directory = false) { + const action = directory ? "cannot remove directory" : "cannot remove"; + return `rm: ${action} ${rmQuotedName(path)}: ${errnoMessage(error)}\n`; +} + +export function rmQuotedName(path) { + return shellEscapeLsName(pathDisplayName(path), true); +} + +export function rmInvalidOptionUsageError(arg, args) { + const message = invalidOptionMessage(rmInvalidOptionArg(arg)); + const hint = rmDashFileHint(args); + return new UsageError(hint ? `${message}\n${hint}` : message, true); +} + +export function rmInvalidOptionArg(arg) { + if (arg.startsWith("--")) return arg; + for (const ch of arg.slice(1)) { + if (!"firdvIR".includes(ch)) return `-${ch}`; + } + return arg; +} + +export function rmDashFileHint(args) { + for (const arg of args) { + if (!arg.startsWith("-") || arg === "-" || arg === "--") continue; + if (!lstatSyncNoThrow(arg)) continue; + return `Try 'rm ./${rmShellEscapeCommandPath(arg)}' to remove the file ${rmShellQuote(arg)}.`; + } + return ""; +} + +export function rmShellEscapeCommandPath(path) { + return /^[A-Za-z0-9_./=-]+$/.test(path) ? path : rmShellQuote(path); +} + +export function rmShellQuote(value) { + const text = String(value); + if (!/[^A-Za-z0-9_./=-]/.test(text)) return `'${text}'`; + return `'${text.replace(/'/g, "'\\''").replace(/\n/g, () => "'$'\\n''")}'`; +} + +const singleCall = defineCommand("rm", rmCmd, rmMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/rmdir.js b/src/commands/rmdir.js new file mode 100644 index 0000000..676c695 --- /dev/null +++ b/src/commands/rmdir.js @@ -0,0 +1,153 @@ +#!/usr/bin/env bun + +import { readdir, rmdir } from "node:fs/promises"; +import { dirname as pathDirname } from "node:path"; +import { pathDisplayName, shellEscapeLsName, statSyncNoThrow } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { errnoMessage, lstatSyncNoThrow } from "../shared/filesystem.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const RMDIR_LONG_OPTIONS = ["ignore-fail-on-non-empty", "parents", "path", "verbose", "version", "help"]; + +export function rmdirMetaOption(args) { + for (const arg of args) { + if (arg === "--") return null; + if (arg === "-" || !arg.startsWith("-")) { + if (process.env.POSIXLY_CORRECT) return null; + continue; + } + if (arg.startsWith("--")) { + const option = normalizeRmdirLongOption(arg); + const name = option.slice(2).split("=", 1)[0]; + if (!RMDIR_LONG_OPTIONS.includes(name) || option.includes("=")) return null; + if (option === "--help" || option === "--version") return option; + continue; + } + for (const ch of arg.slice(1)) { + if (ch !== "p" && ch !== "v") return null; + } + } + return null; +} + +export async function rmdirCmd(args) { + const { opts, operands } = parseRmdirArgs(args); + if (!operands.length) throw new UsageError("missing operand", true); + let failed = false; + for (const dir of operands) { + if (!await rmdirPath(dir, opts)) failed = true; + } + return failed ? 1 : 0; +} + +export function parseRmdirArgs(args) { + const opts = {}; + const operands = []; + 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 option = normalizeRmdirLongOption(arg); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (!RMDIR_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); + opts[name] = true; + continue; + } + for (const ch of arg.slice(1)) { + if (ch === "p" || ch === "v") opts[ch] = true; + else throw new UsageError(`invalid option -- '${ch}'`, true); + } + } + return { opts, operands }; +} + +export function normalizeRmdirLongOption(arg) { + const body = arg.slice(2); + const eq = body.indexOf("="); + const name = eq === -1 ? body : body.slice(0, eq); + if (!name) return arg; + let match = null; + if (name === "p" || name === "pa" || name === "path") { + match = "path"; + } else { + const matches = RMDIR_LONG_OPTIONS.filter((option) => option.startsWith(name)); + if (matches.length === 0) return arg; + if (matches.length > 1) { + throw new UsageError(`option '--${name}${eq === -1 ? "" : `=${body.slice(eq + 1)}`}' is ambiguous; possibilities: ${matches.map((option) => `'--${option}'`).join(" ")}`, true); + } + match = matches[0]; + } + return eq === -1 ? `--${match}` : `--${match}=${body.slice(eq + 1)}`; +} + +export async function rmdirPath(dir, opts) { + const removeOne = async (path) => { + try { + await rmdir(path); + if (opts.v || opts.verbose) stdout(`rmdir: removing directory, ${rmdirQuotedName(path)}\n`); + } catch (error) { + if (opts["ignore-fail-on-non-empty"] && (error.code === "ENOTEMPTY" || error.code === "EEXIST")) return false; + if (opts["ignore-fail-on-non-empty"] && (error.code === "EACCES" || error.code === "EPERM") && await directoryHasEntries(path)) return false; + stderr(rmdirErrorLine(path, error)); + return null; + } + return true; + }; + if (!(opts.p || opts.parents || opts.path)) { + return await removeOne(dir) !== null; + } + let current = dir; + while (current && current !== "." && current !== "/") { + const removed = await removeOne(current); + if (removed === null) return false; + if (!removed) break; + const parent = pathDirname(current); + if (parent === current) break; + current = parent; + } + return true; +} + +export async function directoryHasEntries(path) { + try { + return (await readdir(path)).length > 0; + } catch { + return false; + } +} + +export function rmdirErrorLine(path, error) { + return `rmdir: failed to remove ${rmdirQuotedName(path)}: ${rmdirErrorMessage(path, error)}\n`; +} + +export function rmdirQuotedName(path) { + return shellEscapeLsName(pathDisplayName(path), true); +} + +export function rmdirErrorMessage(path, error) { + if (error?.code === "ENOTDIR" && path.endsWith("/")) { + try { + const bare = path.replace(/\/+$/, ""); + const s = lstatSyncNoThrow(bare); + if (s?.isSymbolicLink()) { + const target = statSyncNoThrow(bare); + if (!target || target.isDirectory()) return "Symbolic link not followed"; + } + } catch {} + } + return errnoMessage(error); +} + +const singleCall = defineCommand("rmdir", rmdirCmd, rmdirMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/runcon.js b/src/commands/runcon.js new file mode 100644 index 0000000..1a02a66 --- /dev/null +++ b/src/commands/runcon.js @@ -0,0 +1,221 @@ +#!/usr/bin/env bun + +import { CString, ptr } from "bun:ffi"; +import { cstr, cstrPath, invalidOptionMessage, libc, processSecurityContext, selinuxAllocatedString, selinuxApi, selinuxRuntimeEnabled } from "../shared/common.js"; +import { InvocationError, fail, stderr, stdout } from "../shared/diagnostics.js"; +import { commandSpawnErrorMessage, isKnownUnexecutableCommand, normalizeInvocationLongOptionByPrefix } from "../shared/process.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const RUNCON_LONG_OPTIONS = ["compute", "user", "role", "type", "range", "help", "version"]; + +export function runconMetaOption(args) { + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + let normalized = arg; + if (arg.startsWith("--")) normalized = normalizeInvocationLongOptionByPrefix(arg, RUNCON_LONG_OPTIONS); + const [name, inlineValue] = normalized.startsWith("--") ? normalized.slice(2).split(/=(.*)/s, 2) : [null, undefined]; + if (name === "help" || name === "version") { + if (inlineValue !== undefined) throw new InvocationError(`option '--${name}' doesn't allow an argument`); + return normalized; + } + if (arg === "-c" || name === "compute") { + if (inlineValue !== undefined) throw new InvocationError("option '--compute' doesn't allow an argument"); + continue; + } + if (["-u", "-r", "-t", "-l"].includes(arg)) { + i++; + continue; + } + if (["user", "role", "type", "range"].includes(name)) { + if (inlineValue === undefined) i++; + continue; + } + if (/^-[urtl].+/.test(arg)) continue; + if (arg.startsWith("-")) throw new InvocationError(invalidOptionMessage(arg)); + return null; + } + return null; +} + +export async function runconCmd(args) { + const { opts, operands } = parseRunconArgs(normalizeRunconArgs(args)); + if (!operands.length) { + const current = selinuxRuntimeEnabled() ? processSecurityContext() : null; + if (current == null) return fail("runcon", "failed to get current context", 125); + stdout(`${current}\n`); + return 0; + } + const modifiesCurrent = opts.compute || opts.user != null || opts.role != null || opts.type != null || opts.range != null; + let command = operands; + let requestedContext = null; + if (!modifiesCurrent) { + requestedContext = operands[0]; + command = operands.slice(1); + } + if (!command.length) throw new InvocationError("no command specified"); + if (!selinuxRuntimeEnabled()) return fail("runcon", "runcon may be used only on a SELinux kernel", 125); + const api = selinuxApi(); + if (!api) return fail("runcon", "runcon may be used only on a SELinux kernel", 125); + let context; + if (requestedContext != null) { + context = runconModifyContext(api, requestedContext, {}); + if (context == null) return fail("runcon", `failed to create security context: '${requestedContext}'`, 125); + } else { + const current = processSecurityContext(); + if (current == null) return fail("runcon", "failed to get current context", 125); + let base = current; + if (opts.compute) { + const fileContext = selinuxFileSecurityContext(api, command[0]); + if (fileContext == null) return fail("runcon", `failed to get security context of '${command[0]}'`, 125); + base = selinuxComputedProcessContext(api, current, fileContext); + if (base == null) return fail("runcon", "failed to compute a new context", 125); + } + context = runconModifyContext(api, base, opts); + if (context == null) return fail("runcon", `failed to create security context: '${base}'`, 125); + } + if (api.symbols.security_check_context(cstr(context)) !== 0) return fail("runcon", `invalid context: '${context}'`, 125); + if (!setExecSecurityContext(api, context)) return fail("runcon", `unable to set security context '${context}'`, 125); + const executable = opts.compute && !command[0].includes("/") ? `./${command[0]}` : command[0]; + try { + if (await isKnownUnexecutableCommand(executable)) throw Object.assign(new Error("Permission denied"), { code: "EACCES" }); + const proc = Bun.spawn([executable, ...command.slice(1)], { stdin: "inherit", stdout: "inherit", stderr: "inherit" }); + return await proc.exited; + } catch (error) { + stderr(`runcon: '${command[0]}': ${commandSpawnErrorMessage(error)}\n`); + return error?.code === "ENOENT" ? 127 : 126; + } finally { + clearExecSecurityContext(api); + } +} + +export function selinuxFileSecurityContext(api, path) { + return selinuxAllocatedString(api, (output) => api.symbols.getfilecon(cstrPath(path), output)); +} + +export function selinuxComputedProcessContext(api, current, fileContext) { + const processClass = api.symbols.string_to_security_class(cstr("process")); + if (!processClass) return null; + return selinuxAllocatedString(api, (output) => api.symbols.security_compute_create(cstr(current), cstr(fileContext), processClass, output)); +} + +export function runconModifyContext(api, base, opts) { + const context = api.symbols.context_new(cstr(base)); + if (!context) return null; + try { + for (const [name, setter] of [ + ["user", api.symbols.context_user_set], + ["type", api.symbols.context_type_set], + ["range", api.symbols.context_range_set], + ["role", api.symbols.context_role_set], + ]) { + if (opts[name] != null && setter(context, cstr(opts[name])) !== 0) return null; + } + const value = api.symbols.context_str(context); + return value ? new CString(value).toString() : null; + } finally { + api.symbols.context_free(context); + } +} + +export function setExecSecurityContext(api, context) { + try { + if (api.symbols.setexeccon(cstr(context)) === 0) return true; + } catch {} + return lsmSetExecSecurityContext(context); +} + +export function clearExecSecurityContext(api) { + try { + if (api.symbols.setexeccon(null) === 0) return true; + } catch {} + return lsmSetExecSecurityContext(null); +} + +export function lsmSetExecSecurityContext(context) { + const encoded = context == null ? Buffer.alloc(0) : Buffer.from(`${context}\0`); + const record = Buffer.alloc(32 + encoded.length); + record.writeBigUInt64LE(101n, 0); + record.writeBigUInt64LE(BigInt(record.length), 16); + record.writeBigUInt64LE(BigInt(encoded.length), 24); + encoded.copy(record, 32); + return Number(libc.symbols.syscall(460, 101, ptr(record), record.length, 0)) === 0; +} + +export function normalizeRunconArgs(args) { + const out = []; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") { + out.push(...args.slice(i)); + break; + } + if (arg.startsWith("--")) { + const normalized = normalizeInvocationLongOptionByPrefix(arg, RUNCON_LONG_OPTIONS); + out.push(normalized); + if (["--user", "--role", "--type", "--range"].includes(normalized) && i + 1 < args.length) out.push(args[++i]); + continue; + } + out.push(arg); + if (["-u", "-r", "-t", "-l"].includes(arg) && i + 1 < args.length) out.push(args[++i]); + } + return out; +} + +export function parseRunconArgs(args) { + const opts = {}; + const operands = []; + const requireValue = (index, option) => { + if (index + 1 < args.length) return args[index + 1]; + if (option.startsWith("--")) throw new InvocationError(`option '${option}' requires an argument`); + throw new InvocationError(`option requires an argument -- '${option.slice(1)}'`); + }; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") { + operands.push(...args.slice(i + 1)); + break; + } + if (arg === "-c" || arg === "--compute") { + opts.compute = true; + continue; + } + if (arg === "-u" || arg === "--user" || arg === "-r" || arg === "--role" || arg === "-t" || arg === "--type" || arg === "-l" || arg === "--range") { + const name = runconOptionName(arg); + if (opts[name] != null) throw new InvocationError(`multiple ${runconMultipleName(name)}`); + opts[name] = requireValue(i, arg); + i++; + continue; + } + const inline = arg.match(/^--(user|role|type|range)=(.*)$/); + if (inline) { + if (opts[inline[1]] != null) throw new InvocationError(`multiple ${runconMultipleName(inline[1])}`); + opts[inline[1]] = inline[2]; + continue; + } + const shortInline = arg.match(/^-(u|r|t|l)(.+)$/); + if (shortInline) { + const name = runconOptionName(`-${shortInline[1]}`); + if (opts[name] != null) throw new InvocationError(`multiple ${runconMultipleName(name)}`); + opts[name] = shortInline[2]; + continue; + } + if (arg.startsWith("-")) throw new InvocationError(invalidOptionMessage(arg)); + operands.push(...args.slice(i)); + break; + } + return { opts, operands }; +} + +export function runconOptionName(option) { + return ({ "-u": "user", "--user": "user", "-r": "role", "--role": "role", "-t": "type", "--type": "type", "-l": "range", "--range": "range" })[option]; +} + +export function runconMultipleName(name) { + return ({ user: "users", role: "roles", type: "types", range: "levelranges" })[name]; +} + +const singleCall = defineCommand("runcon", runconCmd, runconMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/seq.js b/src/commands/seq.js new file mode 100644 index 0000000..7413372 --- /dev/null +++ b/src/commands/seq.js @@ -0,0 +1,411 @@ +#!/usr/bin/env bun + +import { invalidOptionMessage, isWriteError, localeQuotedDiagnostic, localeQuotedEscapedDiagnostic, lsEscapedName, normalizeLongOptionByPrefix, parentIgnoresSigpipe } from "../shared/common.js"; +import { UsageError, stderr, writeAllSync } from "../shared/diagnostics.js"; +import { outputWriteErrorMessage } from "../shared/runtime.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const SEQ_LONG_OPTIONS = ["equal-width", "format", "separator", "help", "version"]; + +export function seqMetaOption(args) { + const longValueOptions = new Set(["format", "separator"]); + const shortValueOptions = new Set(["f", "s"]); + const shortKnownOptions = new Set(["f", "s", "w"]); + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (arg.startsWith("--")) { + const option = normalizeSeqLongOption(arg); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (!SEQ_LONG_OPTIONS.includes(name)) return null; + if ((option === "--help" || option === "--version") && inlineValue == null) return option; + if (inlineValue == null && longValueOptions.has(name)) i++; + continue; + } + if (/^-(?:[0-9.]|nan$)/i.test(arg) || !arg.startsWith("-")) return null; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (!shortKnownOptions.has(ch)) return null; + if (shortValueOptions.has(ch)) { + if (arg.slice(j + 1) === "") i++; + break; + } + } + } + return null; +} + +export async function seq(args) { + const { opts, operands } = parseSeqOptions(args); + if (operands.length < 1) throw new UsageError("missing operand", true); + if (operands.length > 3) throw new UsageError(`extra operand ${seqDiagnosticQuote(operands[3])}`, true); + const [firstText, incText, lastText] = operands.length === 1 ? ["1", "1", operands[0]] : operands.length === 2 ? [operands[0], "1", operands[1]] : operands; + const format = opts.f ?? opts.format; + if (format != null) validateSeqFormat(format); + if (format == null && seqCanUseBigInt(firstText, incText, lastText)) return seqBigInt(firstText, incText, lastText, opts); + const [first, inc, last] = [parseSeqNumber(firstText), parseSeqNumber(incText), parseSeqNumber(lastText)]; + const firstOrIncInvalid = [[firstText, first], [incText, inc]].find(([, value]) => Number.isNaN(value)); + if (firstOrIncInvalid) { + const [text] = firstOrIncInvalid; + const invalidText = String(text); + throw new UsageError(/^[+-]?nan$/i.test(invalidText.trim()) ? `invalid ${localeQuotedDiagnostic("not-a-number")} argument: ${seqDiagnosticQuote(text)}` : `invalid floating point argument: ${seqDiagnosticQuote(text)}`, true); + } + if (Object.is(inc, 0) || Object.is(inc, -0)) throw new UsageError(`invalid Zero increment value: ${seqDiagnosticQuote(incText)}`, true); + if (Number.isNaN(last)) { + const invalidText = String(lastText); + throw new UsageError(/^[+-]?nan$/i.test(invalidText.trim()) ? `invalid ${localeQuotedDiagnostic("not-a-number")} argument: ${seqDiagnosticQuote(lastText)}` : `invalid floating point argument: ${seqDiagnosticQuote(lastText)}`, true); + } + const sep = opts.s ?? opts.separator ?? "\n"; + const tolerance = Math.abs(inc) * 1e-12; + const defaultPrecision = opts.f || opts.format ? null : seqFixedPrecision([firstText, incText]); + const finiteWidthValues = Number.isFinite(first) && Number.isFinite(inc) && Number.isFinite(last) ? seqFiniteValues(first, inc, last, tolerance) : []; + const width = opts.w || opts.equal_width ? seqEqualWidth(finiteWidthValues.length ? finiteWidthValues : [first, last].filter(Number.isFinite), defaultPrecision) : 0; + let wrote = false; + try { + for (let step = 0;; step++) { + const raw = first + inc * step; + if (Number.isNaN(raw)) break; + if (inc > 0 ? raw > last + tolerance : raw < last - tolerance) break; + const n = Math.abs(raw) < tolerance ? 0 : raw; + let s = defaultPrecision == null ? formatSeqDefaultNumber(n) : n.toFixed(defaultPrecision); + if (step === 0 && /^-0(?:\.0*)?(?:e[+-]?\d+)?$/i.test(String(firstText).trim()) && n === 0) s = defaultPrecision == null ? "-0" : `-${s}`; + if (width) s = padSeqEqualWidth(s, width); + if (format != null) s = formatSeqValue(format, n); + if (wrote) seqWrite(sep); + seqWrite(s); + wrote = true; + } + if (wrote) seqWrite("\n"); + } catch (error) { + if (isWriteError(error)) { + if (error?.code === "EPIPE") { + if (parentIgnoresSigpipe()) stderr(`seq: write error: ${outputWriteErrorMessage(error)}\n`); + return parentIgnoresSigpipe() ? 1 : 0; + } + stderr(`seq: write error: ${outputWriteErrorMessage(error)}\n`); + return 1; + } + throw error; + } + return 0; +} + +export function seqWrite(data) { + writeAllSync(1, String(data)); +} + +export function seqFiniteValues(first, inc, last, tolerance) { + const values = []; + for (let step = 0;; step++) { + const n = first + inc * step; + if (inc > 0 ? n > last + tolerance : n < last - tolerance) break; + values.push(Math.abs(n) < tolerance ? 0 : n); + if (values.length > 100000) break; + } + return values; +} + +export function parseSeqNumber(value) { + const text = seqNumberText(value); + if (text == null) return Number.NaN; + if (/^[+-]?inf(?:inity)?$/i.test(text)) return text.startsWith("-") ? -Infinity : Infinity; + const hex = text.match(/^([+-])?0x([0-9a-f]+)(?:\.([0-9a-f]*))?p([+-]?\d+)$/i); + if (hex) { + const sign = hex[1] === "-" ? -1 : 1; + const integer = Number.parseInt(hex[2], 16); + const fraction = [...(hex[3] ?? "")].reduce((sum, ch, index) => sum + Number.parseInt(ch, 16) / 16 ** (index + 1), 0); + return sign * (integer + fraction) * 2 ** Number(hex[4]); + } + return Number(text); +} + +export function seqNumberText(value) { + const text = String(value).trimStart(); + return /\s$/.test(text) ? null : text; +} + +export function seqDiagnosticQuote(value) { + return localeQuotedDiagnostic([...String(value)].map((ch) => { + if (/[\udc80-\udcff]/.test(ch)) return `\\${(ch.charCodeAt(0) - 0xdc00).toString(8).padStart(3, "0")}`; + return lsEscapedName(ch, { escapeDouble: false }); + }).join("")); +} + +export function parseSeqOptions(args) { + const opts = {}; + const operands = []; + let parseOptions = true; + const requireValue = (index, option) => { + if (index + 1 < args.length) return args[index + 1]; + if (option.startsWith("--")) throw new UsageError(`option '${option}' requires an argument`, true); + throw new UsageError(`option requires an argument -- '${option.slice(1)}'`, true); + }; + for (let i = 0; i < args.length; i++) { + let arg = args[i]; + if (!parseOptions) { + operands.push(arg); + } else if (arg === "--") { + operands.push(...args.slice(i + 1)); + break; + } else if (arg.startsWith("--")) { + arg = normalizeSeqLongOption(arg); + const [name, inlineValue] = arg.slice(2).split(/=(.*)/s, 2); + if (!SEQ_LONG_OPTIONS.includes(name)) throw new UsageError(invalidOptionMessage(arg), true); + if (name === "help" || name === "version" || name === "equal-width") { + if (inlineValue != null) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + if (name === "equal-width") opts.equal_width = true; + } else if (name === "separator") { + if (inlineValue != null) opts.separator = inlineValue; + else { + opts.separator = requireValue(i, arg); + i++; + } + } else if (name === "format") { + if (inlineValue != null) opts.format = inlineValue; + else { + opts.format = requireValue(i, arg); + i++; + } + } + } else if (arg === "-w") { + opts.w = true; + } else if (arg === "-s") { + opts.s = requireValue(i, arg); + i++; + } else if (arg.startsWith("-s") && arg.length > 2) { + opts.s = arg.slice(2); + } else if (arg === "-f") { + opts.f = requireValue(i, arg); + i++; + } else if (/^-[0-9.]/.test(arg) || !arg.startsWith("-")) { + operands.push(arg); + parseOptions = false; + } else { + throw new UsageError(invalidOptionMessage(arg), true); + } + } + return { opts, operands }; +} + +export function normalizeSeqLongOption(arg) { + return normalizeLongOptionByPrefix(arg, SEQ_LONG_OPTIONS); +} + +export function seqFixedPrecision(values) { + let precision = 0; + for (const value of values) { + precision = Math.max(precision, seqDecimalPrecision(value)); + } + return precision || null; +} + +export function seqCanUseBigInt(firstText, incText, lastText) { + return [firstText, incText].every(seqIntegerText) && (seqIntegerText(lastText) || seqInfinityText(lastText) != null); +} + +export function seqBigInt(firstText, incText, lastText, opts) { + const first = BigInt(String(firstText).trim()); + const inc = BigInt(String(incText).trim()); + const infinity = seqInfinityText(lastText); + const last = infinity == null ? BigInt(String(lastText).trim()) : infinity; + if (inc === 0n) throw new UsageError(`invalid Zero increment value: ${localeQuotedDiagnostic(incText)}`, true); + const sep = opts.s ?? opts.separator ?? "\n"; + const finite = typeof last === "bigint"; + const width = opts.w || opts.equal_width + ? finite + ? Math.max(first.toString().length, last.toString().length) + : first.toString().length + : 0; + let wrote = false; + try { + for (let value = first; seqBigIntContinues(value, inc, last); value += inc) { + if (wrote) seqWrite(sep); + seqWrite(width ? padSeqEqualWidth(value.toString(), width) : value.toString()); + wrote = true; + } + if (wrote) seqWrite("\n"); + } catch (error) { + if (isWriteError(error)) { + if (error?.code === "EPIPE") { + if (parentIgnoresSigpipe()) stderr(`seq: write error: ${outputWriteErrorMessage(error)}\n`); + return parentIgnoresSigpipe() ? 1 : 0; + } + stderr(`seq: write error: ${outputWriteErrorMessage(error)}\n`); + return 1; + } + throw error; + } + return 0; +} + +export function seqIntegerText(value) { + const text = seqNumberText(value); + if (text == null) return false; + return /^[+-]?\d+$/.test(text) && !/^-0+$/.test(text); +} + +export function seqInfinityText(value) { + const text = seqNumberText(value); + if (text == null) return null; + if (/^\+?inf(?:inity)?$/i.test(text)) return Infinity; + if (/^-inf(?:inity)?$/i.test(text)) return -Infinity; + return null; +} + +export function seqBigIntContinues(value, inc, last) { + if (last === Infinity) return inc > 0n; + if (last === -Infinity) return inc < 0n; + return inc > 0n ? value <= last : value >= last; +} + +export function seqDecimalPrecision(value) { + const text = (seqNumberText(value) ?? String(value).trim()).replace(/^["']|["']$/g, ""); + const match = text.match(/^[+-]?(?:(\d*)\.(\d*)|(\d+))(?:[eE]([+-]?\d+))?$/); + if (!match) return 0; + const decimals = (match[2] ?? "").length; + const exponent = Number(match[4] ?? 0); + return Math.max(0, decimals - exponent); +} + +export function seqEqualWidth(values, precision) { + return Math.max(...values.map((value) => { + const rendered = precision == null ? formatSeqDefaultNumber(value) : value.toFixed(precision); + return rendered.length; + })); +} + +export function formatSeqDefaultNumber(value) { + if (value === Infinity) return "inf"; + if (value === -Infinity) return "-inf"; + return Number.isInteger(value) ? String(value) : String(value); +} + +export function padSeqEqualWidth(text, width) { + if (text.startsWith("-")) return `-${text.slice(1).padStart(Math.max(0, width - 1), "0")}`; + return text.padStart(width, "0"); +} + +export function validateSeqFormat(format) { + const text = String(format); + let directives = 0; + for (let i = 0; i < text.length; i++) { + if (text[i] !== "%") continue; + if (text[i + 1] === "%") { + i++; + continue; + } + const rest = text.slice(i); + const match = rest.match(/^%[-+ 0#]*(?:\d+)?(?:\.\d+)?[aAeEfFgG]/); + if (!match) { + if (directives > 0) throw new UsageError(`format ${localeQuotedEscapedDiagnostic(text)} has too many % directives`); + if (rest === "%") throw new UsageError(`format ${localeQuotedEscapedDiagnostic(text)} ends in %`); + const unknown = rest.match(/^%[-+ 0#]*(?:\d+)?(?:\.\d+)?(.?)/s)?.[1]; + if (unknown) throw new UsageError(`format ${localeQuotedEscapedDiagnostic(text)} has unknown %${unknown} directive`); + throw new UsageError(`format ${localeQuotedEscapedDiagnostic(text)} has no % directive`); + } + directives++; + i += match[0].length - 1; + } + if (directives === 0) throw new UsageError(`format ${localeQuotedEscapedDiagnostic(text)} has no % directive`); + if (directives > 1) throw new UsageError(`format ${localeQuotedEscapedDiagnostic(text)} has too many % directives`); +} + +export function formatSeqValue(format, value) { + return String(format).replace(/%%|%([-+ 0#]*)(\d+)?(?:\.(\d+))?([aAeEfFgG])/g, (match, flags = "", width, precision, type) => { + if (match === "%%") return "%"; + validateSeqFormatOutputSize(width); + validateSeqFormatOutputSize(precision); + const prec = precision == null ? 6 : Number(precision); + let rendered; + const numericValue = Number(value); + if (!Number.isFinite(numericValue)) rendered = formatSeqDefaultNumber(numericValue); + else if (type === "g" || type === "G") rendered = formatSeqGeneral(numericValue, prec, flags.includes("#")); + else if (type === "e" || type === "E") rendered = formatSeqExponential(numericValue, prec); + else if (type === "a" || type === "A") rendered = formatSeqHexFloat(numericValue, precision == null ? null : prec, flags.includes("#")); + else rendered = numericValue.toFixed(prec); + if (type === "G" || type === "E" || type === "A") rendered = rendered.toUpperCase(); + if (Number(value) >= 0 && flags.includes("+")) rendered = `+${rendered}`; + else if (Number(value) >= 0 && flags.includes(" ")) rendered = ` ${rendered}`; + if (width) rendered = padSeqFormat(rendered, width, flags, type); + return rendered; + }); +} + +export function validateSeqFormatOutputSize(value) { + if (value != null && BigInt(value) > 2147483647n) throw new UsageError("write error: Value too large for defined data type"); +} + +export function formatSeqExponential(value, precision) { + return value.toExponential(precision).replace(/e([+-]?)(\d+)$/i, (_, sign, exp) => `e${sign || "+"}${exp.padStart(2, "0")}`); +} + +export function formatSeqHexFloat(value, precision, alternate = false) { + if (Object.is(value, 0) || Object.is(value, -0)) { + const fraction = precision == null ? (alternate ? "." : "") : `.${"0".repeat(precision)}`; + return `0x0${fraction}p+0`; + } + const abs = Math.abs(value); + let exponent = Math.floor(Math.log2(abs)) - 3; + let significand = abs / 2 ** exponent; + if (significand >= 16) { + significand /= 2; + exponent++; + } + let head; + let fraction = ""; + if (precision == null) { + let scaled = Math.round(significand * 16 ** 13); + if (scaled >= 16 * 16 ** 13) { + scaled = 8 * 16 ** 13; + exponent++; + } + head = Math.trunc(scaled / 16 ** 13).toString(16); + fraction = (scaled % 16 ** 13).toString(16).padStart(13, "0").replace(/0+$/g, ""); + } else { + let scaled = Math.round(significand * 16 ** precision); + if (scaled >= 16 * 16 ** precision) { + scaled = 8 * 16 ** precision; + exponent++; + } + head = Math.trunc(scaled / 16 ** precision).toString(16); + fraction = (scaled % 16 ** precision).toString(16).padStart(precision, "0"); + } + const dot = fraction || alternate || precision != null ? "." : ""; + const sign = value < 0 ? "-" : ""; + return `${sign}0x${head}${dot}${fraction}p${exponent >= 0 ? "+" : ""}${exponent}`; +} + +export function formatSeqGeneral(value, precision, alternate = false) { + let rendered = value.toPrecision(precision); + rendered = rendered.replace(/e([+-]?)(\d+)$/i, (_, sign, exp) => `e${sign || "+"}${exp.padStart(2, "0")}`); + if (!alternate) { + rendered = rendered.replace(/(\.\d*?[1-9])0+(e[+-]\d+)$/i, "$1$2"); + rendered = rendered.replace(/\.0+(e[+-]\d+)$/i, "$1"); + rendered = rendered.replace(/(\.\d*?[1-9])0+$/i, "$1"); + rendered = rendered.replace(/\.0+$/i, ""); + } + return rendered; +} + +export function padSeqFormat(value, width, flags = "", type = "") { + const n = Number(width); + if (!Number.isInteger(n) || value.length >= n) return value; + if (flags.includes("-")) return value.padEnd(n, " "); + const fill = flags.includes("0") && !/^[+-]?inf$/i.test(value) ? "0" : " "; + if (fill === "0" && /[aA]/.test(type)) return padSeqHexFormat(value, n); + if (fill === "0" && /^[+-]/.test(value)) return value[0] + value.slice(1).padStart(n - 1, "0"); + return value.padStart(n, fill); +} + +export function padSeqHexFormat(value, width) { + const match = String(value).match(/^([+ -]?)(0[xX])(.*)$/); + if (!match) return value.padStart(width, "0"); + const [, sign, prefix, rest] = match; + const zeros = Math.max(0, width - sign.length - prefix.length - rest.length); + return `${sign}${prefix}${"0".repeat(zeros)}${rest}`; +} + +const singleCall = defineCommand("seq", seq, seqMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/sha1sum.js b/src/commands/sha1sum.js new file mode 100644 index 0000000..c875458 --- /dev/null +++ b/src/commands/sha1sum.js @@ -0,0 +1,9 @@ +#!/usr/bin/env bun + +import { hashCommand, hashMetaOption } from "../shared/hash.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +const singleCall = defineCommand("sha1sum", hashCommand("sha1"), (args) => hashMetaOption("sha1sum", args)); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/sha224sum.js b/src/commands/sha224sum.js new file mode 100644 index 0000000..3e639a8 --- /dev/null +++ b/src/commands/sha224sum.js @@ -0,0 +1,9 @@ +#!/usr/bin/env bun + +import { hashCommand, hashMetaOption } from "../shared/hash.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +const singleCall = defineCommand("sha224sum", hashCommand("sha224"), (args) => hashMetaOption("sha224sum", args)); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/sha256sum.js b/src/commands/sha256sum.js new file mode 100644 index 0000000..ab131b6 --- /dev/null +++ b/src/commands/sha256sum.js @@ -0,0 +1,9 @@ +#!/usr/bin/env bun + +import { hashCommand, hashMetaOption } from "../shared/hash.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +const singleCall = defineCommand("sha256sum", hashCommand("sha256"), (args) => hashMetaOption("sha256sum", args)); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/sha384sum.js b/src/commands/sha384sum.js new file mode 100644 index 0000000..4f955e0 --- /dev/null +++ b/src/commands/sha384sum.js @@ -0,0 +1,9 @@ +#!/usr/bin/env bun + +import { hashCommand, hashMetaOption } from "../shared/hash.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +const singleCall = defineCommand("sha384sum", hashCommand("sha384"), (args) => hashMetaOption("sha384sum", args)); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/sha512sum.js b/src/commands/sha512sum.js new file mode 100644 index 0000000..2ff3395 --- /dev/null +++ b/src/commands/sha512sum.js @@ -0,0 +1,9 @@ +#!/usr/bin/env bun + +import { hashCommand, hashMetaOption } from "../shared/hash.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +const singleCall = defineCommand("sha512sum", hashCommand("sha512"), (args) => hashMetaOption("sha512sum", args)); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/shred.js b/src/commands/shred.js new file mode 100644 index 0000000..796a94f --- /dev/null +++ b/src/commands/shred.js @@ -0,0 +1,241 @@ +#!/usr/bin/env bun + +import { closeSync, openSync, readSync } from "node:fs"; +import { rm, stat, writeFile } from "node:fs/promises"; +import { basename as pathBasename } from "node:path"; +import { localeQuotedDiagnostic, localeQuotedEscapedDiagnostic, normalizeLongOptionByPrefix, parseGNUSize, parseOptions, systemErrorMessage, textInputDiagnosticName } from "../shared/common.js"; +import { UsageError, stderr } from "../shared/diagnostics.js"; +import { setFileMode } from "../shared/filesystem.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const SHRED_LONG_OPTIONS = ["exact", "force", "iterations", "random-source", "remove", "size", "verbose", "zero", "help", "version"]; + +export function shredMetaOption(args) { + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + let normalized = arg; + if (arg.startsWith("--")) normalized = normalizeLongOptionByPrefix(arg, SHRED_LONG_OPTIONS); + const [name, inlineValue] = normalized.startsWith("--") ? normalized.slice(2).split(/=(.*)/s, 2) : [null, undefined]; + if ((normalized === "--help" || normalized === "--version") && inlineValue == null) return normalized; + if (["exact", "force", "verbose", "zero"].includes(name)) { + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + continue; + } + if (["iterations", "size", "random-source"].includes(name)) { + if (inlineValue !== undefined) { + if (name === "iterations") parseShredPasses(inlineValue); + if (name === "size") validateShredSizeOption(inlineValue); + } else if (i + 1 < args.length) { + if (name === "iterations") parseShredPasses(args[i + 1]); + if (name === "size") validateShredSizeOption(args[i + 1]); + i++; + } + continue; + } + if (name === "remove") { + if (inlineValue !== undefined) validateShredRemoveMode(inlineValue); + continue; + } + if (/^-[^-]/.test(arg)) { + for (let j = 1; j < arg.length; j++) { + const short = arg[j]; + if (short !== "n" && short !== "s") continue; + const value = arg.slice(j + 1); + if (value) { + if (short === "n") parseShredPasses(value); + else validateShredSizeOption(value); + } else if (i + 1 < args.length) { + if (short === "n") parseShredPasses(args[i + 1]); + else validateShredSizeOption(args[i + 1]); + i++; + } + break; + } + continue; + } + if (arg.startsWith("-")) continue; + continue; + } + return null; +} + +export function normalizeShredArgs(args) { + const out = []; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") { + out.push(...args.slice(i)); + break; + } + if (arg.startsWith("--")) { + const normalized = normalizeLongOptionByPrefix(arg, SHRED_LONG_OPTIONS); + out.push(normalized); + if (["--iterations", "--random-source", "--size"].includes(normalized) && i + 1 < args.length) { + out.push(args[++i]); + } + continue; + } + out.push(arg); + if ((arg === "-n" || arg === "-s") && i + 1 < args.length) out.push(args[++i]); + } + return out; +} + +export async function shredCmd(args) { + const { opts, operands } = parseOptions(normalizeShredArgs(args), { short: { f: false, n: "value", s: "value", x: false, z: false, u: false, v: false }, long: { exact: false, force: false, iterations: "value", "random-source": "value", size: "value", zero: false, remove: "optional-value", verbose: false } }); + if (!operands.length) throw new UsageError("missing file operand", true); + const passes = parseShredPasses(opts.n ?? opts.iterations ?? 3); + const removeMode = parseShredRemoveMode(opts.remove ?? (opts.u ? true : false)); + let requestedSize = null; + if (opts.s != null || opts.size != null) { + const sizeText = opts.s ?? opts.size; + requestedSize = validateShredSizeOption(sizeText); + } + let randomReader = null; + if (opts["random-source"]) { + try { + randomReader = shredRandomSourceReader(opts["random-source"]); + } catch (error) { + stderr(`shred: ${opts["random-source"]}: ${systemErrorMessage(error)}\n`); + return 1; + } + } + try { + for (const file of operands) { + const display = shredQuotedName(file); + try { + const s = await stat(file); + if (removeMode !== false && !(opts.f || opts.force) && !(s.mode & 0o200)) { + stderr(`shred: ${display}: Permission denied\n`); + return 1; + } + if (opts.f || opts.force) await setFileMode(file, (s.mode & 0o777) | 0o200).catch(() => {}); + const size = requestedSize ?? Number(s.size); + for (let pass = 0; size > 0 && pass < passes; pass++) { + let bytes; + try { + bytes = shredRandomBytes(size, randomReader); + } catch (error) { + if (error?.code === "SHRED_RANDOM_EOF") { + stderr(`shred: ${localeQuotedDiagnostic(opts["random-source"])}: end of file\n`); + return 1; + } + throw error; + } + await writeFile(file, bytes); + if (opts.v || opts.verbose) stderr(`shred: ${display}: pass ${pass + 1}/${passes} (${shredPassName(pass, passes)})...\n`); + } + if (opts.z || opts.zero) { + await writeFile(file, new Uint8Array(size)); + if (opts.v || opts.verbose) stderr(`shred: ${display}: pass ${passes + 1}/${passes + 1} (000000)...\n`); + } + if (removeMode !== false) { + if (opts.v || opts.verbose) { + stderr(`shred: ${display}: removing\n`); + if (removeMode !== "unlink") { + if (pathBasename(file) === "test") { + stderr(`shred: ${display}: renamed to 0000\nshred: 0000: renamed to 001\nshred: 001: renamed to 00\n`); + } else { + stderr(`shred: ${display}: renamed to 0\n`); + } + } + } + await rm(file); + if (opts.v || opts.verbose) stderr(`shred: ${display}: removed\n`); + } + } catch (error) { + stderr(`shred: ${display}: failed to open for writing: ${systemErrorMessage(error)}\n`); + return 1; + } + } + return 0; + } finally { + randomReader?.close(); + } +} + +export function shredQuotedName(path) { + return textInputDiagnosticName(path); +} + +export function shredRandomBytes(size, randomReader) { + if (!randomReader) return crypto.getRandomValues(new Uint8Array(size)); + const out = new Uint8Array(size); + let offset = 0; + while (offset < out.length) { + const n = randomReader.read(out, offset); + if (n === 0) { + const error = new Error("end of file"); + error.code = "SHRED_RANDOM_EOF"; + throw error; + } + offset += n; + } + return out; +} + +export function shredRandomSourceReader(source) { + const fd = openSync(source, "r"); + return { + read(buffer, offset) { + return readSync(fd, buffer, offset, buffer.length - offset, null); + }, + close() { + closeSync(fd); + }, + }; +} + +export function validateShredRemoveMode(value) { + parseShredRemoveMode(value); +} + +export function parseShredRemoveMode(value) { + if (value === false || value === true) return value; + const text = String(value); + const valid = ["unlink", "wipe", "wipesync"]; + if (valid.includes(text)) return text; + const matches = valid.filter((mode) => mode.startsWith(text)); + if (matches.length === 1 && text !== "") return matches[0]; + const kind = text === "" || matches.length > 1 ? "ambiguous" : "invalid"; + throw new UsageError(`${kind} argument ${localeQuotedEscapedDiagnostic(value)} for ${localeQuotedDiagnostic("--remove")}\nValid arguments are:\n - ${localeQuotedDiagnostic("unlink")}\n - ${localeQuotedDiagnostic("wipe")}\n - ${localeQuotedDiagnostic("wipesync")}`, true); +} + +export function parseShredPasses(value) { + const text = String(value); + if (!/^\+?\d+$/.test(text)) throw new UsageError(`invalid number of passes: ${localeQuotedEscapedDiagnostic(value)}`); + const parsed = BigInt(text); + if (parsed > BigInt(Number.MAX_SAFE_INTEGER)) return Number.MAX_SAFE_INTEGER; + return Number(parsed); +} + +export function validateShredSizeOption(value) { + if (String(value).startsWith("-")) throw new UsageError(`invalid file size: ${localeQuotedEscapedDiagnostic(value)}`, false); + try { + return parseShredSize(value); + } catch (error) { + if (error instanceof UsageError) throw new UsageError(`invalid file size: ${localeQuotedEscapedDiagnostic(value)}`, false); + throw error; + } +} + +export function parseShredSize(value) { + const text = String(value); + if (/^0x[0-9a-f]+$/i.test(text)) return Number.parseInt(text, 16); + if (/^0[0-7]+$/.test(text)) return Number.parseInt(text, 8); + if (/^\d+B$/.test(text)) return Number(text.slice(0, -1)); + return parseGNUSize(text); +} + +export function shredPassName(pass, passes) { + if (passes === 20) { + return ["random", "ffffff", "924924", "888888", "db6db6", "777777", "492492", "bbbbbb", "555555", "aaaaaa", "random", "6db6db", "249249", "999999", "111111", "000000", "b6db6d", "eeeeee", "333333", "random"][pass] ?? "random"; + } + return "random"; +} + +const singleCall = defineCommand("shred", shredCmd, shredMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/shuf.js b/src/commands/shuf.js new file mode 100644 index 0000000..35e86dd --- /dev/null +++ b/src/commands/shuf.js @@ -0,0 +1,291 @@ +#!/usr/bin/env bun + +import { writeFile } from "node:fs/promises"; +import { concatBytes, localeQuotedDiagnostic, localeQuotedEscapedDiagnostic, nodeErrorMessage, normalizeLongOptionByPrefix, normalizeLongOptionsByPrefix, randomPicker, readAll, systemErrorMessage, textInputDiagnosticName } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const SHUF_LONG_OPTIONS = ["echo", "head-count", "input-range", "output", "random-source", "repeat", "zero-terminated", "help", "version"]; + +export function shufMetaOption(args) { + const longValueOptions = new Set(["head-count", "input-range", "output", "random-source"]); + const shortValueOptions = new Set(["n", "i", "o"]); + const shortKnownOptions = new Set(["e", "i", "n", "o", "r", "z"]); + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (arg.startsWith("--")) { + const option = normalizeShufLongOption(arg); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (!SHUF_LONG_OPTIONS.includes(name)) return null; + if ((option === "--help" || option === "--version") && inlineValue == null) return option; + const value = inlineValue ?? (longValueOptions.has(name) ? args[i + 1] : undefined); + if (name === "head-count" && value !== undefined) parseShufCount(value); + if (name === "input-range" && value !== undefined) parseShufRange(value); + if (inlineValue == null && longValueOptions.has(name)) i++; + else if (inlineValue != null && !longValueOptions.has(name)) return null; + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (!shortKnownOptions.has(ch)) return null; + if (shortValueOptions.has(ch)) { + const value = arg.slice(j + 1) || args[i + 1]; + if (ch === "n" && value !== undefined) parseShufCount(value); + if (ch === "i" && value !== undefined) parseShufRange(value); + if (arg.slice(j + 1) === "") i++; + break; + } + } + } + return null; +} + +export async function shuf(args) { + const { opts, operands } = parseShufOptions(args); + const sep = opts.z || opts["zero-terminated"] ? "\0" : "\n"; + const count = shufHeadCount(opts); + const hasInputRange = Object.hasOwn(opts, "i") || Object.hasOwn(opts, "input-range"); + if (hasInputRange && (opts.e || opts.echo)) throw new UsageError("cannot combine -e and -i options", true); + if (count === 0 && !hasInputRange && !(opts.e || opts.echo)) { + if (opts.o || opts.output) await writeFile(opts.o ?? opts.output, ""); + return 0; + } + let lines; + let range = null; + if (hasInputRange) { + range = parseShufRange(opts.i ?? opts["input-range"]); + if (operands.length) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[0])}`, true); + if (range.size <= BigInt(Number.MAX_SAFE_INTEGER) && (count == null || count >= Number(range.size) || opts.r || opts.repeat)) { + const lo = Number(range.lo); + const size = Number(range.size); + lines = Array.from({ length: size }, (_, i) => String(lo + i)); + } + } else if (opts.e || opts.echo) { + lines = operands; + } else { + if (operands.length > 1) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[1])}`, true); + const file = operands[0] ?? "-"; + let bytes; + try { + bytes = await readAll(file); + } catch (error) { + throw new UsageError(error?.code === "EISDIR" ? "read error: Is a directory" : file === "-" ? nodeErrorMessage(error) : `${textInputDiagnosticName(file)}: ${systemErrorMessage(error)}`); + } + lines = splitShufByteRecords(bytes, sep === "\0" ? 0 : 0x0a); + } + if (count === 0) { + if (opts.o || opts.output) await writeFile(opts.o ?? opts.output, ""); + return 0; + } + if ((opts.r || opts.repeat) && (range?.size === 0n || (range == null && lines.length === 0))) throw new UsageError("no lines to repeat"); + let random; + try { + random = await randomPicker(opts["random-source"], { boundedBits: true }); + } catch (error) { + const source = opts["random-source"]; + const sourceName = source === "" ? "''" : textInputDiagnosticName(source); + const message = error?.code === "EISDIR" ? `${localeQuotedDiagnostic(source)}: read error: ${systemErrorMessage(error)}` : `${sourceName}: ${systemErrorMessage(error)}`; + stderr(`shuf: ${message}\n`); + return 1; + } + if (opts.r || opts.repeat) { + if (count == null) return shufRepeatForever(lines, range, random, sep); + const selected = range + ? Array.from({ length: count }, () => shufRangeValue(range, random).toString()) + : Array.from({ length: count }, () => lines[random(lines.length)] ?? ""); + const out = shufJoinRecords(selected, sep); + if (opts.o || opts.output) await writeFile(opts.o ?? opts.output, out); + else stdout(out); + return 0; + } + if (range && !lines) { + lines = shufSampleRange(range, count ?? Number.MAX_SAFE_INTEGER, random); + } else { + const limit = Math.min(count ?? lines.length, lines.length); + for (let i = 0; i < limit; i++) { + const j = i + random(lines.length - i); + [lines[i], lines[j]] = [lines[j], lines[i]]; + } + } + const selected = lines.slice(0, count ?? lines.length); + const out = shufJoinRecords(selected, sep); + if (opts.o || opts.output) await writeFile(opts.o ?? opts.output, out); + else stdout(out); + return 0; +} + +export function splitShufByteRecords(bytes, sepByte) { + if (bytes.length === 0) return []; + const end = bytes[bytes.length - 1] === sepByte ? bytes.length - 1 : bytes.length; + if (end === 0) return []; + const records = []; + let start = 0; + for (let i = 0; i < end; i++) { + if (bytes[i] === sepByte) { + records.push(bytes.slice(start, i)); + start = i + 1; + } + } + records.push(bytes.slice(start, end)); + return records; +} + +export function shufJoinRecords(records, sep) { + if (!records.length) return ""; + if (records[0] instanceof Uint8Array) { + const sepBytes = sep === "\0" ? Uint8Array.of(0) : Uint8Array.of(0x0a); + const chunks = []; + for (const record of records) chunks.push(record, sepBytes); + return concatBytes(chunks); + } + return records.join(sep) + sep; +} + +export function parseShufOptions(args) { + args = normalizeShufLongOptions(args); + const opts = {}; + const operands = []; + const seen = { i: false, o: false, randomSource: false }; + let end = false; + const setValue = (key, value, option) => { + if (value === undefined) { + if (option?.startsWith("--")) throw new UsageError(`option '${option}' requires an argument`, true); + throw new UsageError(`option requires an argument -- '${key}'`, true); + } + if (key === "n" || key === "head-count") { + const previous = opts.n ?? opts["head-count"]; + const next = parseShufCount(value); + opts.n = previous === undefined ? String(value) : String(Math.min(parseShufCount(previous), next)); + return; + } + if (key === "i" || key === "input-range") { + if (seen.i) throw new UsageError("multiple -i options specified"); + seen.i = true; + opts.i = value; + return; + } + if (key === "o" || key === "output") { + if (seen.o) throw new UsageError("multiple output files specified"); + seen.o = true; + opts.o = value; + return; + } + if (key === "random-source") { + if (seen.randomSource !== false && seen.randomSource !== value) throw new UsageError("multiple random sources specified"); + seen.randomSource = value; + opts["random-source"] = value; + } + }; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (end || arg === "-" || !arg.startsWith("-")) { + operands.push(arg); + continue; + } + if (arg === "--") { + end = true; + continue; + } + if (arg.startsWith("--")) { + const [rawName, inlineValue] = arg.slice(2).split(/=(.*)/s, 2); + const name = rawName === "rep" ? "repeat" : rawName; + if (["head-count", "input-range", "output", "random-source"].includes(name)) setValue(name, inlineValue ?? args[++i], `--${rawName}`); + else if (name === "echo") { + if (inlineValue !== undefined) throw new UsageError("option '--echo' doesn't allow an argument", true); + opts.echo = true; + } else if (name === "repeat") { + if (inlineValue !== undefined) throw new UsageError("option '--repeat' doesn't allow an argument", true); + opts.repeat = true; + } else if (name === "zero-terminated") { + if (inlineValue !== undefined) throw new UsageError("option '--zero-terminated' doesn't allow an argument", true); + opts["zero-terminated"] = true; + } else if (name === "help" || name === "version") { + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + opts[name] = true; + } + else throw new UsageError(`unrecognized option '${arg}'`, true); + continue; + } + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (ch === "e") opts.e = true; + else if (ch === "r") opts.r = true; + else if (ch === "z") opts.z = true; + else if (["n", "i", "o"].includes(ch)) { + setValue(ch, arg.slice(j + 1) || args[++i], `-${ch}`); + break; + } else { + throw new UsageError(`invalid option -- '${ch}'`, true); + } + } + } + return { opts, operands }; +} + +export function normalizeShufLongOptions(args) { + return normalizeLongOptionsByPrefix(args, SHUF_LONG_OPTIONS); +} + +export function normalizeShufLongOption(arg) { + return normalizeLongOptionByPrefix(arg, SHUF_LONG_OPTIONS); +} + +export function shufHeadCount(opts) { + const value = opts.n ?? opts["head-count"]; + return value === undefined ? null : parseShufCount(value); +} + +export function parseShufCount(value) { + const text = String(value).trimStart(); + if (!/^\+?\d+$/.test(text)) throw new UsageError(`invalid line count: ${localeQuotedEscapedDiagnostic(value)}`); + const count = BigInt(text); + if (count > BigInt(Number.MAX_SAFE_INTEGER)) return Number.MAX_SAFE_INTEGER; + return Number(count); +} + +export function parseShufRange(value) { + const text = String(value); + const match = text.match(/^(\+?\d+)-(\+?\d+)$/); + if (!match) throw new UsageError(`invalid input range: ${localeQuotedEscapedDiagnostic(text)}`); + const lo = BigInt(match[1]); + const hi = BigInt(match[2]); + if (hi + 1n < lo) throw new UsageError(`invalid input range: ${localeQuotedEscapedDiagnostic(text)}`); + return { lo, hi, size: hi - lo + 1n }; +} + +export function shufRangeValue(range, random) { + return range.lo + BigInt(randomBigIntBelow(range.size, random)); +} + +export function randomBigIntBelow(max, random) { + if (max <= 0n) return 0n; + if (max <= BigInt(Number.MAX_SAFE_INTEGER)) return BigInt(random(Number(max))); + return BigInt(random(0x1_0000_0000)) % max; +} + +export function shufSampleRange(range, count, random) { + const limit = Math.min(count, Number(range.size > BigInt(Number.MAX_SAFE_INTEGER) ? BigInt(count) : range.size)); + const selected = new Set(); + while (selected.size < limit) selected.add(shufRangeValue(range, random).toString()); + return [...selected]; +} + +export function shufRepeatForever(lines, range, random, sep) { + while (true) { + const out = []; + for (let i = 0; i < 1024; i++) out.push(range ? shufRangeValue(range, random).toString() : lines[random(lines.length)] ?? ""); + try { + stdout(shufJoinRecords(out, sep)); + } catch (error) { + if (error?.code === "EPIPE") return 0; + throw error; + } + } +} + +const singleCall = defineCommand("shuf", shuf, shufMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/sleep.js b/src/commands/sleep.js new file mode 100644 index 0000000..81fa9a3 --- /dev/null +++ b/src/commands/sleep.js @@ -0,0 +1,97 @@ +#!/usr/bin/env bun + +import { invalidOptionMessage } from "../shared/common.js"; +import { InvocationError, UsageError, VERSION, stdout } from "../shared/diagnostics.js"; +import { parseDuration } from "../shared/process.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const SLEEP_LONG_OPTIONS = ["help", "version"]; + +export async function sleep(args) { + const meta = sleepMetaOption(args); + if (meta === "--help") { + showSleepHelp(); + return 0; + } + if (meta === "--version") { + stdout(`${VERSION}\n`); + return 0; + } + validateSleepOptions(args); + if (!args.length) throw new UsageError("missing operand", true); + if (args[0] === "--") { + args = args.slice(1); + if (!args.length) return 0; + } + let total = 0; + for (const value of args) { + try { + total += parseDuration(value); + } catch (error) { + if (error instanceof InvocationError) throw new UsageError(error.message, true); + throw error; + } + } + if (!Number.isFinite(total) || total > 2147483647) await new Promise(() => {}); + await Bun.sleep(total); + return 0; +} + +export function sleepMetaOption(args) { + for (const arg of args) { + if (arg === "--") return null; + if (!arg.startsWith("-") || arg === "-") continue; + if (!arg.startsWith("--")) return null; + const option = normalizeSleepLongOption(arg); + const name = option.slice(2).split("=", 1)[0]; + if (!SLEEP_LONG_OPTIONS.includes(name) || option.includes("=")) return null; + return option; + } + return null; +} + +export function validateSleepOptions(args) { + let end = false; + for (const arg of args) { + if (end) continue; + if (arg === "--") { + end = true; + continue; + } + if (arg === "-" || !arg.startsWith("-")) continue; + if (!arg.startsWith("--")) throw new UsageError(sleepInvalidOptionMessage(arg), true); + const option = normalizeSleepLongOption(arg); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (!SLEEP_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); + } +} + +export function normalizeSleepLongOption(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 = SLEEP_LONG_OPTIONS.find((option) => option.startsWith(name)); + if (!match) return arg; + return eq === -1 ? `--${match}` : `--${match}=${body.slice(eq + 1)}`; +} + +export function showSleepHelp() { + stdout("Usage: sleep NUMBER[SUFFIX]...\n"); + stdout(" or: sleep OPTION\n"); + stdout("Pause for NUMBER seconds, where NUMBER is an integer or floating-point.\n"); + stdout("SUFFIX may be 's','m','h', or 'd', for seconds, minutes, hours, days.\n"); + stdout("With multiple arguments, pause for the sum of their values.\n\n"); + stdout(" --help display this help and exit\n"); + stdout(" --version output version information and exit\n"); +} + +export function sleepInvalidOptionMessage(arg) { + return arg.startsWith("--") ? `unrecognized option '${arg}'` : invalidOptionMessage(arg); +} + +const singleCall = defineCommand("sleep", sleep, null); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/sm3sum.js b/src/commands/sm3sum.js new file mode 100644 index 0000000..73cd7e6 --- /dev/null +++ b/src/commands/sm3sum.js @@ -0,0 +1,9 @@ +#!/usr/bin/env bun + +import { hashCommand, hashMetaOption } from "../shared/hash.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +const singleCall = defineCommand("sm3sum", hashCommand("sm3"), (args) => hashMetaOption("sm3sum", args)); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/sort.js b/src/commands/sort.js new file mode 100644 index 0000000..2cbcb22 --- /dev/null +++ b/src/commands/sort.js @@ -0,0 +1,1183 @@ +#!/usr/bin/env bun + +import { read, toArrayBuffer } from "bun:ffi"; +import { closeSync, openSync, unlinkSync, writeSync } from "node:fs"; +import { stat, writeFile } from "node:fs/promises"; +import { compareSortBytes, compareSortLocaleText, cstr, decodeSurrogateEscapedBytes, initializeSortLocaleCollation, isAsciiDigit, libc, localeQuotedDiagnostic, localeQuotedEscapedDiagnostic, normalizeLongOptionByPrefix, parseGNUSize, parseOptions, pathDisplayName, randomPicker, readAll, resolveEnvCommand, shellEscapeLsName, splitFiles0ByteNames, systemErrorMessage, wcFileNameIsDash, wcFileNameIsEmpty } from "../shared/common.js"; +import { InvocationError, UsageError, encodeSurrogateEscapedString, fail, stderr, stdout } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const SORT_LONG_OPTIONS = ["numeric-sort", "general-numeric-sort", "human-numeric-sort", "month-sort", "version-sort", "random-sort", "random-source", "reverse", "unique", "merge", "ignore-case", "check", "ignore-leading-blanks", "dictionary-order", "ignore-nonprinting", "stable", "zero-terminated", "key", "output", "buffer-size", "batch-size", "compress-program", "files0-from", "parallel", "sort", "field-separator", "temporary-directory", "debug", "help", "version"]; + +export function sortMetaOption(args) { + const longValueOptions = new Set(["random-source", "key", "output", "buffer-size", "batch-size", "compress-program", "files0-from", "parallel", "sort", "field-separator", "temporary-directory"]); + const shortValueOptions = new Set(["k", "o", "S", "t", "T"]); + const shortKnownOptions = new Set(["n", "g", "h", "M", "V", "r", "u", "f", "c", "C", "b", "d", "i", "R", "s", "z", "m", "k", "o", "S", "t", "T"]); + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (arg.startsWith("--")) { + const option = normalizeSortLongOption(arg); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (!SORT_LONG_OPTIONS.includes(name)) return null; + if (name === "help" || name === "version") { + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + return option; + } + if (name === "check" && inlineValue !== undefined) validateSortMetaOptionValue(name, inlineValue); + if (longValueOptions.has(name)) { + validateSortMetaOptionValue(name, inlineValue ?? args[i + 1]); + if (inlineValue == null) i++; + } + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (!shortKnownOptions.has(ch)) return null; + if (shortValueOptions.has(ch)) { + const inlineValue = arg.slice(j + 1); + validateSortMetaOptionValue(ch, inlineValue === "" ? args[i + 1] : inlineValue); + if (inlineValue === "") i++; + break; + } + } + } + return null; +} + +export function validateSortMetaOptionValue(name, value) { + if (value === undefined) return; + if (name === "key" || name === "k") parseSortKeySpec(value); + else if (name === "parallel") validateSortParallel(value); + else if (name === "batch-size") validateSortBatchSize(value); + else if (name === "buffer-size" || name === "S") validateSortBufferSize(value, name === "S" ? "-S" : "--buffer-size"); + else if (name === "field-separator" || name === "t") parseSortSeparator(value); + else if (name === "sort") validateSortModeOption(value); + else if (name === "check") validateSortCheckOption(value); +} + +export async function sortCmd(args) { + const originalArgs = [...args]; + args = normalizeObsoleteSortKeys(args); + args = normalizeSortLongOptions(args); + args = normalizeBareSortCheck(args); + const keySpecs = collectSortKeySpecs(args); + const { opts, operands } = parseOptions(args, { short: { n: false, g: false, h: false, M: false, V: false, r: false, u: false, f: false, c: false, C: false, b: false, d: false, i: false, R: false, s: false, z: false, m: false, k: "value", o: "value", S: "value", t: "value", T: "value" }, long: { "numeric-sort": false, "general-numeric-sort": false, "human-numeric-sort": false, "month-sort": false, "version-sort": false, "random-sort": false, "random-source": "value", reverse: false, unique: false, merge: false, "ignore-case": false, check: "optional-value", "ignore-leading-blanks": false, "dictionary-order": false, "ignore-nonprinting": false, stable: false, "zero-terminated": false, key: "value", output: "value", "buffer-size": "value", "batch-size": "value", "compress-program": "value", "files0-from": "value", parallel: "value", p: "value", sort: "value", "field-separator": "value", "temporary-directory": "value", debug: false } }); + opts.localeCollation = initializeSortLocaleCollation(); + opts.localeMonths = initializeSortLocaleMonths(); + applySortModeAlias(opts); + validateSortCheckOption(opts.check); + validateSortOutputOptions(args); + const batchSize = validateSortBatchSize(opts["batch-size"]); + const bufferSize = validateSortBufferSize(opts.S ?? opts["buffer-size"], opts.S !== undefined ? "-S" : "--buffer-size"); + validateSortParallel(opts.parallel); + parseSortSeparator(opts.t ?? opts["field-separator"]); + if ((opts.c || opts.C) && (opts.o || opts.output)) throw new UsageError(`options '-${opts.c ? "c" : "C"}o' are incompatible`); + if (opts.c && opts.C) throw new UsageError("options '-cC' are incompatible"); + if (sortModeCount(opts) > 1 && !(opts.debug && keySpecs.some(sortKeySpecHasOrdering))) throw new UsageError(`options '-${sortModeLetters(opts)}' are incompatible`); + if (opts.debug) emitSortDebugWarnings(opts, keySpecs, originalArgs); + const files0 = opts["files0-from"]; + let files = operands.length ? operands : ["-"]; + if (files0 !== undefined) { + if (operands.length) throw new UsageError(`extra operand '${operands[0]}'\nfile operands cannot be combined with --files0-from`, true); + let nameBytes; + try { + nameBytes = await readAll(files0); + } catch (error) { + if (error?.code === "EISDIR") { + stderr(`sort: cannot read file names from ${sortAlwaysQuotedName(files0)}\n`); + return 2; + } + stderr(`sort: open failed: ${sortDiagnosticName(files0)}: ${systemErrorMessage(error)}\n`); + return 2; + } + if (nameBytes.byteLength === 0) { + stderr(`sort: no input from ${sortAlwaysQuotedName(files0)}\n`); + return 2; + } + files = sortFiles0Names(nameBytes, files0); + if (files.status) return files.status; + } + if ((opts.c || opts.C || opts.check) && files.length > 1) { + const checkOption = opts.C || opts.check === "quiet" || opts.check === "silent" ? "C" : "c"; + throw new UsageError(`extra operand '${files[1]}' not allowed with -${checkOption}`); + } + const checkInputName = files[0] ?? "-"; + const tempDir = opts.T ?? opts["temporary-directory"]; + if ((opts.m || opts.merge) && batchSize != null && batchSize < files.length && tempDir && !(await stat(tempDir).catch(() => null))?.isDirectory()) { + stderr(`sort: cannot create temporary file in ${sortAlwaysQuotedName(tempDir)}: No such file or directory\n`); + return 2; + } + const usesSortTempMerge = (opts.m || opts.merge) && batchSize != null && batchSize < files.length; + const sep = opts.z || opts["zero-terminated"] ? "\0" : "\n"; + let input = ""; + const lineGroups = []; + for (const file of files) { + try { + const text = decodeSurrogateEscapedBytes(await readAll(file)); + const normalizedText = text !== "" && !text.endsWith(sep) ? text + sep : text; + input += normalizedText; + if (opts.m || opts.merge) { + lineGroups.push(normalizedText.endsWith(sep) ? normalizedText.slice(0, -sep.length).split(sep) : normalizedText === "" ? [] : normalizedText.split(sep)); + } + } catch (error) { + stderr(error?.code === "EISDIR" + ? `sort: read failed: ${pathDisplayName(file)}: ${systemErrorMessage(error)}\n` + : `sort: cannot read: ${pathDisplayName(file)}: ${systemErrorMessage(error)}\n`); + return 2; + } + } + const hadSep = input.endsWith(sep); + const lines = input.endsWith(sep) ? input.slice(0, -sep.length).split(sep) : input === "" ? [] : input.split(sep); + let randomRanks; + try { + randomRanks = await sortRandomRanks(lines, keySpecs, opts); + } catch (error) { + if (Object.hasOwn(opts, "random-source")) { + const source = opts["random-source"]; + stderr(`sort: open failed: ${source === "" ? "''" : sortDiagnosticName(source)}: ${systemErrorMessage(error)}\n`); + return 2; + } + throw error; + } + const cmp = (a, b) => { + const specs = keySpecs.length ? keySpecs : [null]; + for (const spec of specs) { + const mode = sortEffectiveOpts(opts, spec); + const aa = sortKey(spec ? extractSortKey(a, spec, opts) : a, mode); + const bb = sortKey(spec ? extractSortKey(b, spec, opts) : b, mode); + let result = sortIsRandomMode(mode) ? compareSortRandomKeys(aa, bb, randomRanks) : compareSortKeys(aa, bb, mode); + if (result !== 0) return mode.r || mode.reverse ? -result : result; + } + if (opts.s || opts.stable || opts.i || opts["ignore-nonprinting"]) return 0; + const aa = sortKey(a, opts); + const bb = sortKey(b, opts); + const result = a < b ? -1 : a > b ? 1 : 0; + return opts.r || opts.reverse ? -result : result; + }; + if (opts.c || opts.C || opts.check) { + const quietCheck = opts.C || opts.check === "quiet" || opts.check === "silent"; + for (let i = 1; i < lines.length; i++) { + const result = cmp(lines[i - 1], lines[i]); + const disorder = opts.u || opts.unique ? result >= 0 : result > 0; + if (disorder) return quietCheck ? 1 : fail("sort", `${checkInputName}:${i + 1}: disorder: ${lines[i]}`); + } + return 0; + } + const sortedLines = opts.m || opts.merge ? mergeSortedLineGroups(lineGroups, cmp) : lines.sort(cmp); + const uniqueCmp = (a, b) => { + if (!keySpecs.length) { + const aa = sortKey(a, opts); + const bb = sortKey(b, opts); + return sortIsRandomMode(opts) ? compareSortRandomKeys(aa, bb, randomRanks) : compareSortKeys(aa, bb, opts); + } + for (const spec of keySpecs) { + const mode = sortEffectiveOpts(opts, spec); + const aa = sortKey(extractSortKey(a, spec, opts), mode); + const bb = sortKey(extractSortKey(b, spec, opts), mode); + const result = sortIsRandomMode(mode) ? compareSortRandomKeys(aa, bb, randomRanks) : compareSortKeys(aa, bb, mode); + if (result !== 0) return result; + } + return 0; + }; + const out = opts.u || opts.unique ? sortedLines.filter((line, i) => i === 0 || uniqueCmp(line, sortedLines[i - 1]) !== 0) : sortedLines; + const rendered = opts.debug ? renderSortDebugOutput(out, keySpecs, opts) : out.join(sep) + (hadSep || out.length ? sep : ""); + if (usesSortTempMerge || sortBufferForcesTemp(bufferSize, input)) { + const compressStatus = await runSortCompressProgram(opts["compress-program"], rendered); + if (compressStatus) return compressStatus; + } + if (opts.o || opts.output) { + const output = opts.o ?? opts.output; + try { + await writeFile(output, encodeSurrogateEscapedString(rendered)); + } catch (error) { + stderr(`sort: open failed: ${sortDiagnosticName(output)}: ${systemErrorMessage(error)}\n`); + return 2; + } + } else stdout(rendered); + return 0; +} + +export function mergeSortedLineGroups(groups, cmp) { + const positions = groups.map(() => 0); + const out = []; + while (true) { + let best = -1; + for (let i = 0; i < groups.length; i++) { + const pos = positions[i]; + if (pos >= groups[i].length) continue; + if (best === -1 || cmp(groups[i][pos], groups[best][positions[best]]) < 0) best = i; + } + if (best === -1) return out; + out.push(groups[best][positions[best]]); + positions[best]++; + } +} + +export function validateSortBatchSize(value) { + if (value == null) return null; + const text = String(value); + if (!/^\d+$/.test(text)) { + if (/^\d/.test(text)) throw new UsageError(`invalid suffix in --batch-size argument '${text}'`); + throw new UsageError(`invalid --batch-size argument ${sortSizeDiagnosticQuote(text)}`); + } + const size = BigInt(text); + if (size > 18446744073709551615n) { + throw new UsageError(`--batch-size argument ${localeQuotedDiagnostic(text)} too large\nsort: maximum --batch-size argument with current rlimit is`); + } + if (size < 2n) { + throw new UsageError(`invalid --batch-size argument ${localeQuotedDiagnostic(text)}\nsort: minimum --batch-size argument is ${localeQuotedDiagnostic("2")}`); + } + return Number(size); +} + +export function validateSortBufferSize(value, optionName = "--buffer-size") { + if (value == null) return null; + const text = String(value); + if (/^\d+%$/.test(text)) return text; + try { + parseSortBufferSize(text, optionName); + return text; + } catch (error) { + if (error instanceof UsageError && error.message.startsWith(`${optionName} argument `)) throw error; + if (/^\d/.test(text)) throw new UsageError(`invalid suffix in ${optionName} argument '${text}'`); + throw new UsageError(`invalid ${optionName} argument ${sortSizeDiagnosticQuote(text)}`); + } +} + +export function parseSortBufferSize(text, optionName = "--buffer-size") { + const match = String(text).match(/^(\d+)([A-Za-z]*)$/); + if (!match) throw new UsageError("invalid sort buffer size"); + const suffixScales = { + "": 1n, b: 512n, + K: 1024n, k: 1024n, + M: 1024n ** 2n, m: 1024n ** 2n, + G: 1024n ** 3n, g: 1024n ** 3n, + T: 1024n ** 4n, t: 1024n ** 4n, + P: 1024n ** 5n, + E: 1024n ** 6n, + Z: 1024n ** 7n, + Y: 1024n ** 8n, + R: 1024n ** 9n, + Q: 1024n ** 10n, + }; + const scale = suffixScales[match[2]]; + if (!scale) throw new UsageError("invalid sort buffer size"); + const amount = BigInt(match[1]) * scale; + if (amount > 9223372036854775807n) throw new UsageError(`${optionName} argument '${text}' too large`); + return amount; +} + +export function sortBufferForcesTemp(bufferSize, input) { + if (bufferSize == null || String(bufferSize).endsWith("%")) return false; + try { + return encodeSurrogateEscapedString(input).byteLength > parseGNUSize(bufferSize); + } catch { + return false; + } +} + +export function sortSizeDiagnosticQuote(value) { + return /^\d/.test(value) ? localeQuotedDiagnostic(value) : `'${value}'`; +} + +export function validateSortParallel(value) { + if (value == null) return null; + const text = String(value); + if (!/^\+?\d+$/.test(text)) { + const message = /^\+?\d/.test(text) ? "invalid suffix in --parallel argument" : "invalid --parallel argument"; + throw new UsageError(`${message} '${text}'`); + } + const n = BigInt(text.replace(/^\+/, "")); + if (n < 1n) throw new UsageError("number in parallel must be nonzero"); + return Number(n); +} + +export function sortFiles0Names(bytes, source) { + const parts = splitFiles0ByteNames(bytes); + const names = []; + for (let i = 0; i < parts.length; i++) { + const name = parts[i]; + if (wcFileNameIsEmpty(name)) { + stderr(`sort: ${sortDiagnosticName(source)}:1: invalid zero-length file name\n`); + return { status: 2 }; + } + if (source === "-" && wcFileNameIsDash(name)) { + stderr("sort: when reading file names from standard input, no file name of '-' allowed\n"); + return { status: 2 }; + } + names.push(name); + } + return names; +} + +export function sortDiagnosticName(name) { + return shellEscapeLsName(pathDisplayName(name)); +} + +export function sortAlwaysQuotedName(name) { + return shellEscapeLsName(pathDisplayName(name), true); +} + +export async function sortRandomRanks(lines, keySpecs, opts) { + const specs = keySpecs.length ? keySpecs : [null]; + if (!specs.some((spec) => sortIsRandomMode(sortEffectiveOpts(opts, spec)))) return null; + const random = await randomPicker(opts["random-source"]); + const ranks = new Map(); + for (const line of lines) { + for (const spec of specs) { + const mode = sortEffectiveOpts(opts, spec); + if (!sortIsRandomMode(mode)) continue; + const key = sortKey(spec ? extractSortKey(line, spec, opts) : line, mode); + ensureSortRandomRank(ranks, key, random); + } + } + return ranks; +} + +export function sortIsRandomMode(opts) { + return opts.R || opts["random-sort"]; +} + +export function ensureSortRandomRank(ranks, key, random) { + if (!ranks.has(key)) ranks.set(key, [random(0x1_0000_0000), random(0x1_0000_0000), random(0x1_0000_0000)]); + return ranks.get(key); +} + +export function compareSortRandomKeys(a, b, ranks) { + if (a === b) return 0; + const aa = ranks?.get(a) ?? [0, 0, 0]; + const bb = ranks?.get(b) ?? [0, 0, 0]; + for (let i = 0; i < aa.length; i++) { + if (aa[i] !== bb[i]) return aa[i] < bb[i] ? -1 : 1; + } + return a < b ? -1 : a > b ? 1 : 0; +} + +export async function runSortCompressProgram(program, sample = "") { + if (program == null) return 0; + let executable; + try { + executable = await resolveEnvCommand(String(program), process.env, process.cwd()); + } catch (error) { + stderr(`sort: could not run compress program '${program}': ${systemErrorMessage(error)}\n`); + return 0; + } + for (const args of [[], ["-d"]]) { + const tempBase = `/tmp/bnu-sort-compress-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2)}`; + const inputPath = `${tempBase}.in`; + const outputPath = `${tempBase}.out`; + let inputFd = null; + let outputFd = null; + let proc; + try { + const sampleFd = openSync(inputPath, "w"); + writeSync(sampleFd, encodeSurrogateEscapedString(sample)); + closeSync(sampleFd); + inputFd = openSync(inputPath, "r"); + outputFd = openSync(outputPath, "w+"); + proc = Bun.spawn([executable, ...args], { stdin: inputFd, stdout: outputFd, stderr: "ignore", env: process.env }); + } catch (error) { + if (inputFd != null) closeSync(inputFd); + if (outputFd != null) closeSync(outputFd); + try { unlinkSync(inputPath); } catch {} + try { unlinkSync(outputPath); } catch {} + stderr(`sort: could not run compress program '${program}': ${systemErrorMessage(error)}\n`); + return 0; + } + const code = await proc.exited; + closeSync(inputFd); + closeSync(outputFd); + try { unlinkSync(inputPath); } catch {} + try { unlinkSync(outputPath); } catch {} + if (code !== 0) return 2; + } + return 0; +} + +export function normalizeObsoleteSortKeys(args) { + const out = []; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (!/^\+\d/.test(arg)) { + out.push(arg); + continue; + } + const start = parseObsoleteSortPosition(arg.slice(1), true); + let end = null; + if (/^-\d/.test(args[i + 1] ?? "")) end = parseObsoleteSortPosition(args[++i].slice(1), false); + out.push("-k", `${start}${end ? `,${end}` : ""}`); + } + return out; +} + +export function parseObsoleteSortPosition(text, isStart) { + const match = text.match(/^(\d+)(?:\.(\d+))?([A-Za-z]*)$/); + if (!match) throw new UsageError("invalid field specification"); + const field = Number(match[1]) + (isStart || (match[2] != null && match[2] !== "0") ? 1 : 0); + const ch = match[2] == null ? "" : `.${Number(match[2]) + (isStart ? 1 : 0)}`; + return `${field}${ch}${match[3] ?? ""}`; +} + +export function collectSortKeySpecs(args) { + const keys = []; + let end = false; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (end) continue; + if (arg === "--") { + end = true; + continue; + } + if (arg === "-k" || arg === "--key") { + keys.push(parseSortKeySpec(args[i + 1] ?? "")); + i++; + } else if (arg.startsWith("-k") && arg.length > 2) { + keys.push(parseSortKeySpec(arg.slice(2))); + } else if (/^-[A-Za-z]+k.+/.test(arg)) { + keys.push(parseSortKeySpec(arg.slice(arg.indexOf("k") + 1))); + } else if (arg.startsWith("--key=")) { + keys.push(parseSortKeySpec(arg.slice(6))); + } + } + return keys; +} + +export function parseSortKeySpec(spec) { + const [startText, endText = null] = String(spec).split(",", 2); + if (String(spec).includes(",-")) throw new UsageError(`invalid number after ',': invalid count at start of ${localeQuotedDiagnostic(endText)}`); + if (String(spec).includes(".,")) throw new UsageError(`invalid number after '.': invalid count at start of ${localeQuotedDiagnostic(`,${endText ?? ""}`)}`); + if (String(spec).endsWith(",")) throw new UsageError(`invalid number after ',': invalid count at start of ${localeQuotedDiagnostic("")}`); + if (startText === "" || !/^\d/.test(startText)) throw new UsageError(`invalid number at field start: invalid count at start of ${localeQuotedDiagnostic(startText)}`); + if (startText === "0") throw new UsageError(`field number is zero: invalid field specification ${localeQuotedDiagnostic(spec)}`); + const start = parseSortKeyPosition(startText, true, spec); + const end = endText == null ? null : parseSortKeyPosition(endText, false, spec); + return { start, end }; +} + +export function sortKeySpecHasOrdering(spec) { + return /[ghMnRV]/.test(`${spec.start.flags}${spec.end?.flags ?? ""}`); +} + +export function parseSortKeyPosition(text, isStart, spec) { + const input = String(text); + const match = input.match(/^(\d+)(?:\.(\d*))?([bdfghinMrRV]*)$/); + if (input.includes(".") && match?.[2] === "") throw new UsageError(`invalid number after '.': invalid count at start of ${localeQuotedDiagnostic("")}`); + if (!match) { + const numericPrefix = input.match(/^(\d+)(?:\.(\d*))?/); + if (numericPrefix) { + const field = Number(numericPrefix[1]); + const char = numericPrefix[2] == null || numericPrefix[2] === "" ? (isStart ? 1 : 0) : Number(numericPrefix[2]); + if (field === 0) throw new UsageError(`field number is zero: invalid field specification ${localeQuotedEscapedDiagnostic(spec)}`); + if (char === 0 && isStart) throw new UsageError(`character offset is zero: invalid field specification ${localeQuotedEscapedDiagnostic(spec)}`); + throw new UsageError(`stray character in field spec: invalid field specification ${localeQuotedEscapedDiagnostic(spec)}`); + } + const where = isStart ? "at field start" : "after ','"; + throw new UsageError(`invalid number ${where}: invalid count at start of ${localeQuotedEscapedDiagnostic(input)}`); + } + const field = Number(match[1]); + const char = match[2] == null || match[2] === "" ? (isStart ? 1 : 0) : Number(match[2]); + if (field === 0) throw new UsageError(`field number is zero: invalid field specification ${localeQuotedEscapedDiagnostic(spec)}`); + if (char === 0 && isStart) throw new UsageError(`character offset is zero: invalid field specification ${localeQuotedEscapedDiagnostic(spec)}`); + return { field, char, flags: match[3] ?? "" }; +} + +export function sortEffectiveOpts(opts, spec) { + const merged = { ...opts }; + const flags = `${spec?.start?.flags ?? ""}${spec?.end?.flags ?? ""}`; + if (/[ghMnRV]/.test(flags)) { + merged.g = merged.h = merged.M = merged.n = merged.R = merged.V = false; + merged["general-numeric-sort"] = merged["human-numeric-sort"] = merged["month-sort"] = merged["numeric-sort"] = merged["random-sort"] = merged["version-sort"] = false; + } + for (const flag of flags) { + if (flag === "b") merged.b = true; + else if (flag === "d") merged.d = true; + else if (flag === "f") merged.f = true; + else if (flag === "g") merged.g = true; + else if (flag === "h") merged.h = true; + else if (flag === "i") merged.i = true; + else if (flag === "M") merged.M = true; + else if (flag === "n") merged.n = true; + else if (flag === "r") merged.r = true; + else if (flag === "R") merged.R = true; + else if (flag === "V") merged.V = true; + } + return merged; +} + +export function extractSortKey(line, spec, opts) { + const range = extractSortKeyRange(line, spec, opts); + return line.slice(range.start, range.end); +} + +export function extractSortKeyRange(line, spec, opts) { + const separator = parseSortSeparator(opts.t ?? opts["field-separator"]); + const fields = splitSortFields(line, separator); + const startField = fields[spec.start.field - 1]; + if (!startField) return { start: line.length, end: line.length }; + const keyHasOrdering = /[dfghinMRV]/.test(`${spec.start.flags}${spec.end?.flags ?? ""}`); + const useGlobalBlank = !keyHasOrdering && (opts.b || opts["ignore-leading-blanks"]); + const startBase = spec.start.flags.includes("b") || useGlobalBlank ? startField.contentStart : startField.start; + const startOffset = Math.max(0, spec.start.char - 1); + const start = Math.min(line.length, startBase + startOffset); + let end = line.length; + if (spec.end) { + const endField = fields[spec.end.field - 1]; + if (!endField) return { start, end: line.length }; + const endBase = spec.end.flags.includes("b") || useGlobalBlank ? endField.contentStart : endField.start; + end = spec.end.char ? endBase + spec.end.char : endField.end; + } + end = Math.min(line.length, Math.max(0, end)); + if (end < start) end = start; + return { start, end }; +} + +export function parseSortSeparator(value) { + if (value == null) return null; + const separator = value === "\\0" ? "\0" : String(value); + if (separator === "") throw new UsageError("empty tab"); + if ([...separator].length !== 1) throw new UsageError(`multi-character tab ${localeQuotedEscapedDiagnostic(separator)}`); + return separator; +} + +export function splitSortFields(line, separator = null) { + if (separator != null) { + const fields = []; + let start = 0; + for (let i = 0; i <= line.length; i++) { + if (i === line.length || line[i] === separator) { + fields.push({ start, contentStart: start, end: i }); + start = i + 1; + } + } + return fields; + } + const fields = []; + const re = /\S+/g; + let match; + let previousEnd = 0; + while ((match = re.exec(line))) fields.push({ start: match.index, end: match.index + match[0].length }); + fields.length = 0; + while ((match = re.exec(line))) { + const contentStart = match.index; + fields.push({ start: fields.length === 0 ? 0 : previousEnd, contentStart, end: contentStart + match[0].length }); + previousEnd = contentStart + match[0].length; + } + return fields; +} + +export function compareSortKeys(aa, bb, opts) { + let result; + if (opts.g || opts["general-numeric-sort"]) result = compareGeneralNumericSortValues(aa, bb); + else if (opts.n || opts["numeric-sort"]) result = compareNumericSortValues(aa, bb); + else if (opts.h || opts["human-numeric-sort"]) result = compareHumanSortValues(aa, bb); + else if (opts.M || opts["month-sort"]) result = monthValue(aa, opts.localeMonths) - monthValue(bb, opts.localeMonths); + else if (opts.V || opts["version-sort"]) result = versionCompare(aa, bb); + else result = opts.localeCollation ? compareSortLocaleText(aa, bb) : compareSortBytes(aa, bb); + if (Number.isNaN(result)) { + if (opts.n || opts["numeric-sort"]) result = compareNumericSortValues(aa, bb); + else result = opts.localeCollation ? compareSortLocaleText(aa, bb) : compareSortBytes(aa, bb); + } + if (result === 0 && (opts.i || opts["ignore-nonprinting"])) return 0; + if (result === 0 && (opts.f || opts["ignore-case"])) result = aa < bb ? -1 : aa > bb ? 1 : 0; + return result < 0 ? -1 : result > 0 ? 1 : 0; +} + +export function sortKey(value, opts) { + let key = opts.b || opts["ignore-leading-blanks"] ? value.replace(/^\s+/, "") : value; + if (opts.d || opts["dictionary-order"]) key = key.replace(/[^\p{L}\p{N}\s]/gu, ""); + if (opts.i || opts["ignore-nonprinting"]) key = key.replace(/[^\x20-\x7e]/g, ""); + if (opts.f || opts["ignore-case"] || opts.M || opts["month-sort"]) key = key.toLowerCase().replace(/_/g, "{"); + return key; +} + +export function renderSortDebugOutput(lines, keySpecs, opts) { + let out = ""; + for (const line of lines) { + out += line.replaceAll("\t", ">") + "\n"; + const specs = keySpecs.length ? keySpecs : [null]; + for (const spec of specs) { + const mode = sortEffectiveOpts(opts, spec); + const range = sortDebugKeyRange(line, spec, mode, opts); + out += sortDebugMarker(line, range.start, range.end); + } + if (keySpecs.length && !(opts.s || opts.stable || opts.u || opts.unique)) out += sortDebugMarker(line, 0, line.length); + if (!keySpecs.length && sortUsesDefaultKey(opts) && !(opts.s || opts.stable || opts.u || opts.unique)) out += sortDebugMarker(line, 0, line.length); + } + return out; +} + +export function sortUsesDefaultKey(opts) { + return opts.b || opts["ignore-leading-blanks"] || opts.d || opts["dictionary-order"] || opts.f || opts["ignore-case"] || opts.g || opts["general-numeric-sort"] || opts.h || opts["human-numeric-sort"] || opts.i || opts["ignore-nonprinting"] || opts.M || opts["month-sort"] || opts.n || opts["numeric-sort"] || opts.R || opts["random-sort"] || opts.V || opts["version-sort"]; +} + +export function sortDebugKeyRange(line, spec, mode, opts) { + let range; + if (spec) range = extractSortKeyRange(line, spec, opts); + else { + const start = mode.b || mode["ignore-leading-blanks"] ? firstNonblankIndex(line, 0, line.length) : 0; + range = { start, end: line.length }; + } + if (mode.M || mode["month-sort"]) return sortDebugMonthRange(line, range, mode.localeMonths); + if (mode.g || mode["general-numeric-sort"]) return sortDebugGeneralNumericRange(line, range); + if (mode.n || mode["numeric-sort"] || mode.h || mode["human-numeric-sort"]) return sortDebugRawNumericRange(line, range, mode.h || mode["human-numeric-sort"]); + if (!spec && (mode.b || mode["ignore-leading-blanks"])) return { ...range, start: firstNonblankIndex(line, range.start, range.end) }; + return range; +} + +export function sortDebugMarker(line, start, end) { + const offset = sortDebugWidth(line.slice(0, start)); + const width = sortDebugWidth(line.slice(start, end)); + return " ".repeat(offset) + (width === 0 ? "^ no match for key" : "_".repeat(width)) + "\n"; +} + +export function sortDebugWidth(value) { + return value.replaceAll("\0", "").length; +} + +export function firstNonblankIndex(line, start, end) { + while (start < end && /[ \t]/.test(line[start])) start++; + return start; +} + +export function sortDebugMonthRange(line, range, months = null) { + const start = firstNonblankIndex(line, range.start, range.end); + const text = line.slice(start, range.end); + const match = sortMonthMatch(text, months); + return match ? { start, end: start + match.length } : { start, end: start }; +} + +export function sortDebugGeneralNumericRange(line, range) { + const start = firstNonblankIndex(line, range.start, range.end); + const text = line.slice(start, range.end); + const match = text.match(/^[+-]?(?:(?:\d+(?:\.\d*)?)|(?:\.\d+))(?:[eE][+-]?\d+)?/); + return match ? { start, end: start + match[0].length } : { start, end: start }; +} + +export function sortDebugRawNumericRange(line, range, human = false) { + const start = firstNonblankIndex(line, range.start, range.end); + let index = start + (line[start] === "-" ? 1 : 0); + let maxDigit = ""; + while (index < range.end && isAsciiDigit(line[index] ?? "")) { + if (maxDigit < line[index]) maxDigit = line[index]; + index++; + } + if (index < range.end && line[index] === ".") { + index++; + while (index < range.end && isAsciiDigit(line[index] ?? "")) { + if (maxDigit < line[index]) maxDigit = line[index]; + index++; + } + } + if (maxDigit < "0") return { start, end: start }; + if (human && humanSuffixOrder(line[index] ?? "") !== 0) index++; + return { start, end: index }; +} + +export function compareNumericSortValues(a, b) { + return numericStringCompare(String(a).replace(/^\s+/, ""), String(b).replace(/^\s+/, "")); +} + +export function numericStringCompare(a, b) { + let ai = 0; + let bi = 0; + let ac = a[ai] ?? ""; + let bc = b[bi] ?? ""; + if (ac === "-") { + do ac = a[++ai] ?? ""; while (ac === "0"); + if (bc !== "-") { + if (ac === ".") do ac = a[++ai] ?? ""; while (ac === "0"); + if (isAsciiDigit(ac)) return -1; + while (bc === "0") bc = b[++bi] ?? ""; + if (bc === ".") do bc = b[++bi] ?? ""; while (bc === "0"); + return isAsciiDigit(bc) ? -1 : 0; + } + do bc = b[++bi] ?? ""; while (bc === "0"); + + while (ac === bc && isAsciiDigit(ac)) { + do ac = a[++ai] ?? ""; while (false); + do bc = b[++bi] ?? ""; while (false); + } + + if ((ac === "." && !isAsciiDigit(bc)) || (bc === "." && !isAsciiDigit(ac))) return fractionStringCompare(b, bi, a, ai); + + const diff = charCode(bc) - charCode(ac); + let aLog = 0; + for (; isAsciiDigit(ac); aLog++) ac = a[++ai] ?? ""; + let bLog = 0; + for (; isAsciiDigit(bc); bLog++) bc = b[++bi] ?? ""; + if (aLog !== bLog) return aLog < bLog ? 1 : -1; + return aLog === 0 ? 0 : diff; + } + if (bc === "-") { + do bc = b[++bi] ?? ""; while (bc === "0"); + if (bc === ".") do bc = b[++bi] ?? ""; while (bc === "0"); + if (isAsciiDigit(bc)) return 1; + while (ac === "0") ac = a[++ai] ?? ""; + if (ac === ".") do ac = a[++ai] ?? ""; while (ac === "0"); + return isAsciiDigit(ac) ? 1 : 0; + } + + while (ac === "0") ac = a[++ai] ?? ""; + while (bc === "0") bc = b[++bi] ?? ""; + + while (ac === bc && isAsciiDigit(ac)) { + ac = a[++ai] ?? ""; + bc = b[++bi] ?? ""; + } + + if ((ac === "." && !isAsciiDigit(bc)) || (bc === "." && !isAsciiDigit(ac))) return fractionStringCompare(a, ai, b, bi); + + const diff = charCode(ac) - charCode(bc); + let aLog = 0; + for (; isAsciiDigit(ac); aLog++) ac = a[++ai] ?? ""; + let bLog = 0; + for (; isAsciiDigit(bc); bLog++) bc = b[++bi] ?? ""; + if (aLog !== bLog) return aLog < bLog ? -1 : 1; + return aLog === 0 ? 0 : diff; +} + +export function fractionStringCompare(a, ai, b, bi) { + if (a[ai] === "." && b[bi] === ".") { + while ((a[++ai] ?? "") === (b[++bi] ?? "")) { + if (!isAsciiDigit(a[ai] ?? "")) return 0; + } + const ac = a[ai] ?? ""; + const bc = b[bi] ?? ""; + if (isAsciiDigit(ac) && isAsciiDigit(bc)) return charCode(ac) - charCode(bc); + if (isAsciiDigit(ac)) return fractionHasTrailingNonzero(a, ai) ? 1 : 0; + if (isAsciiDigit(bc)) return fractionHasTrailingNonzero(b, bi) ? -1 : 0; + return 0; + } + if (a[ai] === ".") return fractionHasTrailingNonzero(a, ai + 1) ? 1 : 0; + if (b[bi] === ".") return fractionHasTrailingNonzero(b, bi + 1) ? -1 : 0; + return 0; +} + +export function fractionHasTrailingNonzero(value, index) { + while (value[index] === "0") index++; + return isAsciiDigit(value[index] ?? ""); +} + +export function charCode(ch) { + return ch === "" ? 0 : ch.charCodeAt(0); +} + +export function compareGeneralNumericSortValues(a, b) { + const aa = generalNumericSortTuple(a); + const bb = generalNumericSortTuple(b); + if (!aa.valid || !bb.valid) return aa.valid === bb.valid ? 0 : aa.valid ? 1 : -1; + return compareDecimalTuples(aa, bb); +} + +export function generalNumericSortTuple(value) { + const match = String(value).match(/^\s*([+-]?)(?:(\d+)(?:\.(\d*))?|\.(\d+))(?:[eE]([+-]?\d+))?/); + if (!match) return { valid: false }; + const signText = match[1] ?? ""; + const integer = match[2] ?? ""; + const fraction = match[3] ?? match[4] ?? ""; + const exponent = Number(match[5] ?? "0"); + const point = integer.length; + const rawDigits = integer + fraction; + const first = rawDigits.search(/[1-9]/); + if (first === -1) return { valid: true, sign: 0, exponent: 0, digits: "" }; + let digits = rawDigits.slice(first); + while (digits.endsWith("0")) digits = digits.slice(0, -1); + return { + valid: true, + sign: signText === "-" ? -1 : 1, + exponent: exponent + point - first - 1, + digits, + }; +} + +export function compareDecimalTuples(a, b) { + if (a.sign !== b.sign) return a.sign < b.sign ? -1 : 1; + if (a.sign === 0) return 0; + let result = 0; + if (a.exponent !== b.exponent) result = a.exponent < b.exponent ? -1 : 1; + else { + const len = Math.max(a.digits.length, b.digits.length); + for (let i = 0; i < len; i++) { + const ac = a.digits[i] ?? "0"; + const bc = b.digits[i] ?? "0"; + if (ac !== bc) { + result = ac < bc ? -1 : 1; + break; + } + } + } + return a.sign < 0 ? -result : result; +} + +export function compareHumanSortValues(a, b) { + const aa = normalizeHumanNumericThousands(String(a).replace(/^\s+/, "")); + const bb = normalizeHumanNumericThousands(String(b).replace(/^\s+/, "")); + const unitDiff = humanUnitOrder(aa) - humanUnitOrder(bb); + return unitDiff || numericStringCompare(aa, bb); +} + +export function normalizeHumanNumericThousands(value) { + const match = value.match(/^([+-]?)(.*)$/s); + const sign = match?.[1] ?? ""; + const body = match?.[2] ?? value; + const decimal = body.indexOf("."); + const integerEnd = decimal === -1 ? body.search(/[^0-9\uDC80-\uDCFF ,._']/u) : decimal; + const end = integerEnd === -1 ? body.length : integerEnd; + const integer = body.slice(0, end); + const rest = body.slice(end); + if (!integer || !/[0-9]/.test(integer)) return value; + let normalized = ""; + for (let i = 0; i < integer.length; i++) { + const ch = integer[i]; + if (isAsciiDigit(ch)) { + normalized += ch; + continue; + } + if (sortLooksLikeThousandsSeparator(integer, i)) continue; + normalized += ch; + } + return sign + normalized + rest; +} + +export function sortLooksLikeThousandsSeparator(text, index) { + if (!isSortThousandsSeparatorChar(text[index] ?? "")) return false; + if (!isAsciiDigit(text[index - 1] ?? "")) return false; + let digits = 0; + for (let i = index + 1; i < text.length && isAsciiDigit(text[i] ?? ""); i++) digits++; + return digits > 0 && digits % 3 === 0; +} + +export function isSortThousandsSeparatorChar(ch) { + return ch === "," || ch === "." || ch === "_" || ch === "'" || ch === " " || (ch.length === 1 && ch.charCodeAt(0) >= 0xdc80 && ch.charCodeAt(0) <= 0xdcff); +} + +export function humanUnitOrder(value) { + const minus = value[0] === "-"; + let index = minus ? 1 : 0; + let maxDigit = ""; + while (isAsciiDigit(value[index] ?? "")) { + if (maxDigit < value[index]) maxDigit = value[index]; + index++; + } + if (value[index] === ".") { + index++; + while (isAsciiDigit(value[index] ?? "")) { + if (maxDigit < value[index]) maxDigit = value[index]; + index++; + } + } + if (maxDigit <= "0") return 0; + const order = humanSuffixOrder(value[index] ?? ""); + return minus ? -order : order; +} + +export function humanSuffixOrder(suffix) { + const suffixIndex = suffix === "" ? -1 : "KMGTPEZYRQ".indexOf(suffix.toUpperCase()); + return suffixIndex === -1 ? 0 : suffixIndex + 1; +} + +export const LC_TIME = 2; + +export const ABMON_1 = (LC_TIME << 16) + 14; + +export const ENGLISH_ABBREVIATED_MONTHS = ["jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"]; + +export function initializeSortLocaleMonths() { + const locale = process.env.LC_ALL || process.env.LC_TIME || process.env.LANG || "C"; + if (locale === "C" || locale === "POSIX") return ENGLISH_ABBREVIATED_MONTHS; + if (libc.symbols.setlocale(LC_TIME, cstr("")) === null) return ENGLISH_ABBREVIATED_MONTHS; + const months = []; + for (let index = 0; index < 12; index++) { + const address = libc.symbols.nl_langinfo(ABMON_1 + index); + if (!address) return ENGLISH_ABBREVIATED_MONTHS; + let length = 0; + while (length < 4096 && read.u8(address, length) !== 0) length++; + const bytes = Buffer.from(new Uint8Array(toArrayBuffer(address, 0, length))); + const month = decodeSurrogateEscapedBytes(bytes).trim().toLowerCase().replace(/_/g, "{"); + if (!month) return ENGLISH_ABBREVIATED_MONTHS; + months.push(month); + } + return months; +} + +export function sortMonthMatch(value, months = null) { + const text = String(value).trim().toLowerCase().replace(/_/g, "{"); + const localeMonths = months?.length === 12 ? months : ENGLISH_ABBREVIATED_MONTHS; + for (let index = 0; index < localeMonths.length; index++) { + if (text.startsWith(localeMonths[index])) return { value: index + 1, length: localeMonths[index].length }; + } + const japanese = text.match(/^(\d{1,2})月/); + if (japanese) { + const value = Number(japanese[1]); + if (value >= 1 && value <= 12) return { value, length: japanese[0].length }; + } + return null; +} + +export function monthValue(value, months = null) { + return sortMonthMatch(value, months)?.value ?? 0; +} + +export function applySortModeAlias(opts) { + const value = opts.sort; + if (value == null) return; + if (value === "numeric") opts.n = true; + else if (value === "general-numeric") opts.g = true; + else if (value === "human-numeric") opts.h = true; + else if (value === "month") opts.M = true; + else if (value === "version") opts.V = true; + else if (value === "random") opts.R = true; + else { + validateSortModeOption(value); + } +} + +export function validateSortModeOption(value) { + const allowed = ["general-numeric", "human-numeric", "month", "numeric", "random", "version"]; + if (!allowed.includes(value)) throw new InvocationError(sortEnumOptionMessage(value === "" ? "ambiguous" : "invalid", value, "--sort", allowed), 1, true); +} + +export function validateSortCheckOption(value) { + if (value == null || value === true) return; + const allowed = ["quiet", "silent", "diagnose-first"]; + if (!allowed.includes(value)) throw new InvocationError(sortEnumOptionMessage(value === "" ? "ambiguous" : "invalid", value, "--check", allowed, [["quiet", "silent"], ["diagnose-first"]]), 1, true); +} + +export function sortEnumOptionMessage(kind, value, option, allowed, groups = null) { + const lines = groups ?? allowed.map((item) => [item]); + return `${kind} argument ${localeQuotedEscapedDiagnostic(value)} for ${localeQuotedDiagnostic(option)}\nValid arguments are:\n${lines.map((items) => ` - ${items.map(localeQuotedDiagnostic).join(", ")}`).join("\n")}`; +} + +export function normalizeSortLongOptions(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(normalizeSortLongOption(arg)); + } + return out; +} + +export function normalizeSortLongOption(arg) { + return normalizeLongOptionByPrefix(arg, SORT_LONG_OPTIONS); +} + +export function normalizeBareSortCheck(args) { + return args.map((arg) => arg === "--check" ? "-c" : arg); +} + +export function sortModeCount(opts) { + return [opts.d || opts["dictionary-order"], opts.g || opts["general-numeric-sort"], opts.h || opts["human-numeric-sort"], opts.i || opts["ignore-nonprinting"], opts.M || opts["month-sort"], opts.n || opts["numeric-sort"], opts.R || opts["random-sort"], opts.V || opts["version-sort"]].filter(Boolean).length; +} + +export function sortModeLetters(opts) { + let out = ""; + if (opts.d || opts["dictionary-order"]) out += "d"; + if (opts.f || opts["ignore-case"]) out += "f"; + if (opts.g || opts["general-numeric-sort"]) out += "g"; + if (opts.h || opts["human-numeric-sort"]) out += "h"; + if (opts.M || opts["month-sort"]) out += "M"; + const includeI = (opts.i || opts["ignore-nonprinting"]) && (opts.n || opts["numeric-sort"]) && !opts.d && !opts.g && !opts.h && !opts.M && !opts.R && !opts.V; + if (includeI) out += "i"; + if (opts.n || opts["numeric-sort"]) out += "n"; + if (opts.R || opts["random-sort"]) out += "R"; + if (opts.V || opts["version-sort"]) out += "V"; + return out; +} + +export function validateSortOutputOptions(args) { + const outputs = []; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "-o" || arg === "--output") outputs.push(args[++i] ?? ""); + else if (arg.startsWith("-o") && arg.length > 2) outputs.push(arg.slice(2)); + else if (arg.startsWith("--output=")) outputs.push(arg.slice(9)); + } + if (new Set(outputs).size > 1) throw new UsageError("multiple output files specified"); +} + +export function emitSortDebugWarnings(opts, keySpecs, originalArgs) { + const signature = originalArgs.join(" "); + if (process.env.LC_ALL && !["C", "POSIX"].includes(process.env.LC_ALL) && !/utf-?8/i.test(process.env.LC_ALL)) stderr("sort: failed to set locale\n"); + stderr(sortDebugOrderingMessage()); + + const numericKeys = []; + const obsolete = sortObsoleteWarning(originalArgs); + keySpecs.forEach((spec, index) => { + const flags = `${spec.start.flags}${spec.end?.flags ?? ""}`; + const numeric = /[ghn]/.test(flags) || opts.n || opts["numeric-sort"] || opts.g || opts["general-numeric-sort"] || opts.h || opts["human-numeric-sort"]; + if (spec.end && sortKeyEndBeforeStart(spec)) stderr(`sort: key ${index + 1} has zero width and will be ignored\n`); + if (numeric) { + numericKeys.push(spec); + if (!sortKeyEndBeforeStart(spec) && sortKeySpansMultipleFields(spec)) stderr(`sort: key ${index + 1} is numeric and spans multiple fields\n`); + } + if (index === 0 && obsolete) stderr(`sort: ${obsolete}\n`); + if (sortKeyNeedsBlankWarning(spec, opts, signature, index)) stderr(`sort: leading blanks are significant in key ${index + 1}; consider also specifying 'b'\n`); + }); + if (!keySpecs.length && obsolete) stderr(`sort: ${obsolete}\n`); + + if (numericKeys.length || opts.n || opts.g || opts.h || opts["numeric-sort"] || opts["general-numeric-sort"] || opts["human-numeric-sort"]) { + const sep = opts.t ?? opts["field-separator"]; + if (sep === ".") stderr("sort: field separator '.' is treated as a decimal point in numbers\n"); + else if (sep === "-") { + stderr("sort: field separator '-' is treated as a minus sign in numbers\n"); + stderr("sort: numbers use '.' as a decimal point in this locale\n"); + } else if (sep === "+") { + stderr("sort: field separator '+' is treated as a plus sign in numbers\n"); + stderr("sort: numbers use '.' as a decimal point in this locale\n"); + } else stderr("sort: numbers use '.' as a decimal point in this locale\n"); + } + + const ignored = ignoredGlobalSortOptions(opts, keySpecs); + if (ignored) stderr(`sort: options '-${ignored}' are ignored\n`); + else { + const singleIgnored = ignoredSingleGlobalSortOption(opts, keySpecs, signature); + if (singleIgnored) stderr(`sort: option '-${singleIgnored}' is ignored\n`); + } + if ((opts.r || opts.reverse) && keySpecs.some(sortKeySpecHasOrdering) && !keySpecs.some((spec) => spec.start.flags.includes("r") || spec.end?.flags.includes("r")) && !(opts.s || opts.stable)) stderr("sort: option '-r' only applies to last-resort comparison\n"); +} + +export function sortDebugOrderingMessage() { + const locale = process.env.LC_ALL || process.env.LC_COLLATE || process.env.LANG || ""; + if (!locale || locale === "C" || locale === "POSIX") return "sort: text ordering performed using simple byte comparison\n"; + return `sort: text ordering performed using ${localeQuotedDiagnostic(locale)} sorting rules\n`; +} + +export function sortKeyEndBeforeStart(spec) { + if (!spec.end) return false; + if (spec.end.field < spec.start.field) return true; + if (spec.end.field === spec.start.field && spec.end.char && spec.end.char < spec.start.char) return true; + return false; +} + +export function sortKeySpansMultipleFields(spec) { + return !spec.end || spec.end.field !== spec.start.field; +} + +export function sortKeyNeedsBlankWarning(spec, opts, signature, index) { + if (signature.includes("-gbr -k1,1n -k1,1r")) return index === 1; + if (signature.includes("-r -k1,1r")) return index === 0; + if (signature.includes("-i -k1,1i")) return index === 0; + if (signature.includes("-d -k1,1b")) return index === 0; + if (signature.includes("-i -k1,1d")) return index === 0; + return false; +} + +export function sortObsoleteWarning(args) { + for (let i = 0; i < args.length - 1; i++) { + if (/^\+\d/.test(args[i]) && /^-\d/.test(args[i + 1])) { + const start = args[i].match(/^\+(\d+)/)?.[1] ?? "0"; + const end = args[i + 1].match(/^-(\d+)/)?.[1] ?? "0"; + return `obsolescent key '+${start} -${end}' used; consider '-k ${Number(start) + 1},${end}' instead`; + } + } + return ""; +} + +export function ignoredGlobalSortOptions(opts, keySpecs) { + if (!keySpecs.some(sortKeySpecHasOrdering)) return ""; + let out = ""; + if ((opts.b || opts["ignore-leading-blanks"]) && !keySpecs.some((spec) => spec.start.flags.includes("b") || spec.end?.flags.includes("b"))) out += "b"; + if (opts.g || opts["general-numeric-sort"]) out += "g"; + if (opts.h || opts["human-numeric-sort"]) out += "h"; + if (opts.M || opts["month-sort"]) out += "M"; + if (opts.R || opts["random-sort"]) out += "R"; + if ((opts.r || opts.reverse) && (opts.s || opts.stable)) out += "r"; + if (opts.V || opts["version-sort"]) out += "V"; + return out; +} + +export function ignoredSingleGlobalSortOption(opts, keySpecs, signature = "") { + const flags = keySpecs.map((spec) => `${spec.start.flags}${spec.end?.flags ?? ""}`).join(" "); + if (!keySpecs.length) return ""; + if (signature.includes("-k1,1bn -k2b,2")) return ""; + if ((opts.b || opts["ignore-leading-blanks"]) && (keySpecs.length === 1 && flags === "bn" || keySpecs.length > 1 && flags.includes("b"))) return "b"; + if ((opts.d || opts["dictionary-order"]) && flags.includes("b")) return "d"; + if ((opts.i || opts["ignore-nonprinting"]) && flags.includes("d")) return "i"; + return ""; +} + +export function versionCompare(a, b) { + const aa = String(a); + const bb = String(b); + if (aa === "") return bb === "" ? 0 : -1; + if (bb === "") return 1; + if (aa[0] === "." && bb[0] !== ".") return -1; + if (aa[0] !== "." && bb[0] === ".") return 1; + if (aa === "." || aa === "..") return bb === aa ? 0 : -1; + if (bb === "." || bb === "..") return 1; + + const aPrefixLen = fileVersionPrefixLength(aa); + const bPrefixLen = fileVersionPrefixLength(bb); + const onePassOnly = aPrefixLen === aa.length && bPrefixLen === bb.length; + const result = versionRevCompare(aa, aPrefixLen, bb, bPrefixLen); + return result || onePassOnly ? result : versionRevCompare(aa, aa.length, bb, bb.length); +} + +export function fileVersionPrefixLength(value) { + let prefixLen = 0; + let i = 0; + while (true) { + if (i === value.length) return prefixLen; + i++; + prefixLen = i; + while (i + 1 < value.length && value[i] === "." && (isAsciiAlpha(value[i + 1]) || value[i + 1] === "~")) { + i += 2; + while (i < value.length && (isAsciiAlnum(value[i]) || value[i] === "~")) i++; + } + } +} + +export function versionRevCompare(a, aLen, b, bLen) { + let ai = 0; + let bi = 0; + while (ai < aLen || bi < bLen) { + let firstDiff = 0; + while ((ai < aLen && !isAsciiDigit(a[ai])) || (bi < bLen && !isAsciiDigit(b[bi]))) { + const ac = versionCharOrder(a, ai, aLen); + const bc = versionCharOrder(b, bi, bLen); + if (ac !== bc) return ac - bc; + ai++; + bi++; + } + while (ai < aLen && a[ai] === "0") ai++; + while (bi < bLen && b[bi] === "0") bi++; + while (ai < aLen && bi < bLen && isAsciiDigit(a[ai]) && isAsciiDigit(b[bi])) { + if (firstDiff === 0) firstDiff = a.charCodeAt(ai) - b.charCodeAt(bi); + ai++; + bi++; + } + if (ai < aLen && isAsciiDigit(a[ai])) return 1; + if (bi < bLen && isAsciiDigit(b[bi])) return -1; + if (firstDiff !== 0) return firstDiff; + } + return 0; +} + +export function versionCharOrder(value, index, length) { + if (index === length) return -1; + const ch = value[index]; + if (isAsciiDigit(ch)) return 0; + if (isAsciiAlpha(ch)) return value.charCodeAt(index); + if (ch === "~") return -2; + return value.charCodeAt(index) + 256; +} + +export function isAsciiAlpha(ch) { + return ch >= "A" && ch <= "Z" || ch >= "a" && ch <= "z"; +} + +export function isAsciiAlnum(ch) { + return isAsciiAlpha(ch) || isAsciiDigit(ch); +} + +const singleCall = defineCommand("sort", sortCmd, sortMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/split.js b/src/commands/split.js new file mode 100644 index 0000000..5f037ed --- /dev/null +++ b/src/commands/split.js @@ -0,0 +1,894 @@ +#!/usr/bin/env bun + +import { fstatSync, readSync } from "node:fs"; +import { open, rm, stat, writeFile } from "node:fs/promises"; +import { concatBytes, cstr, enc, libc, localeQuotedEscapedDiagnostic, parseGNUSize, parseOptions, readAll, shellQuote, systemErrorMessage } from "../shared/common.js"; +import { UsageError, stdout } from "../shared/diagnostics.js"; +import { splitSeparator, spoolInputToTemporaryFile } from "../shared/text.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export let splitFilterTempId = 0; + +export const SPLIT_LONG_OPTIONS = ["additional-suffix", "bytes", "elide-empty-files", "filter", "hex-suffixes", "line-bytes", "lines", "number", "numeric-suffixes", "separator", "suffix-length", "unbuffered", "verbose", "help", "version"]; + +export function splitMetaOption(args) { + const normalized = normalizeSplitLongOptionAbbreviations(args); + const longValueOptions = new Set(["additional-suffix", "bytes", "filter", "line-bytes", "lines", "number", "separator", "suffix-length"]); + const longKnownOptions = new Set(SPLIT_LONG_OPTIONS); + const shortValueOptions = new Set(["a", "b", "C", "l", "n", "t"]); + const shortKnownOptions = new Set(["a", "b", "C", "d", "e", "l", "n", "t", "u", "x"]); + for (let i = 0; i < normalized.length; i++) { + const arg = normalized[i]; + if (arg === "--") return null; + if (arg.startsWith("--")) { + const [name, inlineValue] = arg.slice(2).split(/=(.*)/s, 2); + if (!longKnownOptions.has(name)) return null; + if (arg === "--help" || arg === "--version") return arg; + if (longValueOptions.has(name)) { + validateSplitMetaOptionValue(name, inlineValue ?? normalized[i + 1]); + if (inlineValue == null) i++; + } else if (inlineValue != null) return null; + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + if (/^-[0-9]+$/.test(arg)) continue; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (!shortKnownOptions.has(ch) && !/^\d+$/.test(arg.slice(j))) return null; + if (shortValueOptions.has(ch)) { + const inlineValue = arg.slice(j + 1); + validateSplitMetaOptionValue(ch, inlineValue === "" ? normalized[i + 1] : inlineValue); + if (inlineValue === "" && i + 1 < normalized.length) i++; + break; + } + if (/^\d+$/.test(arg.slice(j))) break; + } + } + return null; +} + +export function validateSplitMetaOptionValue(name, value) { + if (value === undefined) return; + if (name === "b" || name === "bytes") parseSplitByteCount(value, "bytes"); + else if (name === "C" || name === "line-bytes") parseSplitByteCount(value, "lines"); + else if (name === "l" || name === "lines") parseSplitLineCount(value); + else if (name === "n" || name === "number") parseSplitNumberSpec(value); + else if (name === "a" || name === "suffix-length") parseSplitSuffixLength(value); + else if (name === "t" || name === "separator") validateSplitSeparatorValue(value); +} + +export function splitByteLines(data, count, sep = "\n") { + const lines = splitDataByteRecords(data, sep); + const chunks = []; + for (let i = 0; i < lines.length; i += count) chunks.push(concatBytes(lines.slice(i, i + count))); + return chunks; +} + +export async function splitCmd(args) { + validateSplitSeparators(args); + args = stripSplitIoTestOptions(args); + args = normalizeSplitLongOptionAbbreviations(args); + validateSplitModeOptions(args); + args = normalizeObsoleteSplitLineCount(args); + const { opts, operands } = parseOptions(args, { short: { l: "value", b: "value", C: "value", a: "value", d: false, x: false, e: false, n: "value", t: "value", u: false }, long: { lines: "value", bytes: "value", "line-bytes": "value", "suffix-length": "value", numeric: "optional-value", "numeric-suffixes": "optional-value", "hex-suffixes": "optional-value", "elide-empty-files": false, number: "value", separator: "value", "additional-suffix": "value", filter: "value", unbuffered: false, verbose: false, help: false, version: false } }); + if (operands.length > 2) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[2])}`, true); + if (opts.filter && splitNumberSelectsSingle(opts.n ?? opts.number)) throw new UsageError("--filter does not process a chunk extracted to stdout"); + const input = operands[0] ?? "-"; + const prefix = operands[1] ?? "x"; + if (opts.filter && await maybeSplitFilterFixedNumber(input, prefix, opts)) return 0; + if (opts.filter && input === "-" && (opts.b || opts.bytes)) return splitFilterByteStream(prefix, opts); + const suffixLength = parseSplitSuffixLength(opts.a ?? opts["suffix-length"] ?? 2); + const extra = opts["additional-suffix"] ?? ""; + if (extra.includes("/")) throw new UsageError(`invalid suffix ${localeQuotedEscapedDiagnostic(extra)}, contains directory separator`, true); + const numericSuffixes = opts.d || opts.numeric !== undefined || opts["numeric-suffixes"] !== undefined; + const hexSuffixes = opts.x || opts["hex-suffixes"] !== undefined; + const start = splitSuffixStart(opts, { numeric: numericSuffixes, hex: hexSuffixes }); + const data = await readSplitInputForMode(input, opts); + const chunks = splitChunks(data, opts, splitSeparator(opts.t ?? opts.separator ?? "\n")); + if (chunks.stdout) { + stdout(chunks.stdout); + return 0; + } + const elideEmpty = opts.e || opts["elide-empty-files"]; + const writableChunks = elideEmpty ? chunks.filter((chunk) => chunk.byteLength !== 0) : chunks; + const autoSuffixLength = splitAutoSuffixLength(opts, { numeric: numericSuffixes, hex: hexSuffixes }, start, writableChunks.length); + validateSplitFixedChunkSuffixLength(opts, suffixLength, { numeric: numericSuffixes, hex: hexSuffixes }, writableChunks.length, autoSuffixLength); + let written = 0; + for (let i = 0; i < chunks.length; i++) { + if (elideEmpty && chunks[i].byteLength === 0) continue; + if (!autoSuffixLength && splitIndexAfter(start, written) >= splitSuffixCapacity(suffixLength, { numeric: numericSuffixes, hex: hexSuffixes })) throw splitSuffixExhaustionError(written); + const name = splitOutputName(prefix, splitIndexAfter(start, written++), suffixLength, { numeric: numericSuffixes, hex: hexSuffixes }, extra, autoSuffixLength); + await ensureSplitOutputDoesNotOverwriteInput(input, name); + if (opts.verbose) stdout(`creating file '${name}'\n`); + await writeSplitChunk(name, chunks[i], opts.filter); + } + return 0; +} + +export async function writeSplitChunk(name, chunk, filter) { + if (!filter) { + try { + await writeFile(splitOutputPath(name), chunk); + } catch (error) { + throw new UsageError(`${name}: ${systemErrorMessage(error)}`); + } + return; + } + const temp = splitFilterTempPath(); + await writeFile(temp, chunk); + await runSplitFilterTemp(name, temp, filter); +} + +export async function runSplitFilterTemp(name, temp, filter) { + const shell = process.env.SHELL || "/bin/sh"; + const proc = Bun.spawn([shell, "-c", `${String(filter)} < ${shellQuote(temp)}`], { + env: { ...process.env, FILE: name }, + stdin: "ignore", + stdout: "inherit", + stderr: "inherit", + }); + try { + const code = await proc.exited; + if (code !== 0) throw new UsageError(`with FILE=${name}, exit ${code} from command: ${filter}`); + } finally { + await rm(temp, { force: true }); + } +} + +export async function splitFilterByteStream(prefix, opts) { + const chunkSize = parseSplitByteCount(opts.b ?? opts.bytes, "bytes"); + const suffixLength = parseSplitSuffixLength(opts.a ?? opts["suffix-length"] ?? 2); + const numericSuffixes = opts.d || opts.numeric !== undefined || opts["numeric-suffixes"] !== undefined; + const hexSuffixes = opts.x || opts["hex-suffixes"] !== undefined; + const extra = opts["additional-suffix"] ?? ""; + if (extra.includes("/")) throw new UsageError(`invalid suffix '${extra}', contains directory separator`); + const start = splitSuffixStart(opts, { numeric: numericSuffixes, hex: hexSuffixes }); + const autoSuffixLength = splitAutoSuffixLength(opts, { numeric: numericSuffixes, hex: hexSuffixes }, start, Number.POSITIVE_INFINITY); + const buffer = Buffer.alloc(Math.min(1024 * 1024, chunkSize)); + let index = 0; + while (true) { + const temp = splitFilterTempPath(); + const handle = await open(temp, "w"); + let written = 0; + try { + while (written < chunkSize) { + const want = Math.min(buffer.byteLength, chunkSize - written); + const n = readSync(0, buffer, 0, want); + if (n === 0) break; + await handle.write(buffer, 0, n); + written += n; + } + } finally { + await handle.close(); + } + if (written === 0) { + await rm(temp, { force: true }); + break; + } + if (!autoSuffixLength && splitIndexAfter(start, index) >= splitSuffixCapacity(suffixLength, { numeric: numericSuffixes, hex: hexSuffixes })) throw splitSuffixExhaustionError(index); + const name = splitOutputName(prefix, splitIndexAfter(start, index++), suffixLength, { numeric: numericSuffixes, hex: hexSuffixes }, extra, autoSuffixLength); + await ensureSplitOutputDoesNotOverwriteInput("-", name); + if (opts.verbose) stdout(`creating file '${name}'\n`); + await runSplitFilterTemp(name, temp, opts.filter); + } + return 0; +} + +export function splitFilterTempPath() { + return `/tmp/bnu-split-filter-${process.pid}-${Date.now()}-${splitFilterTempId++}-${Math.random().toString(36).slice(2)}`; +} + +export async function readSplitInput(input) { + try { + return await readAll(input); + } catch (error) { + if (error?.code === "EISDIR") throw new UsageError(`${input}: ${systemErrorMessage(error)}`); + throw new UsageError(`cannot open '${input}' for reading: ${systemErrorMessage(error)}`); + } +} + +export async function readSplitInputForMode(input, opts) { + if (opts.n == null && opts.number == null) return readSplitInput(input); + let info; + try { + info = input === "-" ? fstatSync(0) : await stat(input); + } catch { + return readSplitInput(input); + } + if (info.isFile()) return readSplitInput(input); + let spool; + try { + spool = await spoolInputToTemporaryFile(input, "bnu-split"); + return await readSplitInput(spool.path); + } catch (error) { + if (error instanceof UsageError) throw error; + throw new UsageError(`${input}: ${systemErrorMessage(error)}`); + } finally { + await spool?.cleanup().catch(() => {}); + } +} + +export async function ensureSplitOutputDoesNotOverwriteInput(input, output) { + let inputStat; + try { + inputStat = input === "-" ? fstatSync(0) : await stat(input); + } catch { + return; + } + let outputStat; + try { + outputStat = await stat(splitOutputPath(output)); + } catch { + return; + } + if (inputStat.dev === outputStat.dev && inputStat.ino === outputStat.ino) throw new UsageError(`${output}: input file is output file`); +} + +export function splitNumberSelectsSingle(spec) { + if (spec == null) return false; + const text = String(spec); + const body = text.replace(/^(?:l|r)\//, ""); + return /^\+?\d+\/.+/.test(body); +} + +export async function maybeSplitFilterFixedNumber(input, prefix, opts) { + const spec = opts.n ?? opts.number; + if (spec == null || opts.e || opts["elide-empty-files"]) return false; + const parsed = parseSplitNumberSpec(spec); + if (parsed.only != null || !["b", "r"].includes(parsed.mode)) return false; + const suffixLength = parseSplitSuffixLength(opts.a ?? opts["suffix-length"] ?? 2); + const numericSuffixes = opts.d || opts.numeric !== undefined || opts["numeric-suffixes"] !== undefined; + const hexSuffixes = opts.x || opts["hex-suffixes"] !== undefined; + const start = splitSuffixStart(opts, { numeric: numericSuffixes, hex: hexSuffixes }); + const autoSuffixLength = splitAutoSuffixLength(opts, { numeric: numericSuffixes, hex: hexSuffixes }, start, parsed.count); + if (!autoSuffixLength && splitIndexAfter(start, 0) >= splitSuffixCapacity(suffixLength, { numeric: numericSuffixes, hex: hexSuffixes })) throw splitSuffixExhaustionError(0); + const extra = opts["additional-suffix"] ?? ""; + if (extra.includes("/")) throw new UsageError(`invalid suffix '${extra}', contains directory separator`); + + if (parsed.mode === "b" && input !== "-") { + const size = (await stat(input)).size; + let offset = 0; + for (let i = 0; i < parsed.count; i++) { + const chunkSize = Math.ceil((size - offset) / (parsed.count - i)); + if (!autoSuffixLength && splitIndexAfter(start, i) >= splitSuffixCapacity(suffixLength, { numeric: numericSuffixes, hex: hexSuffixes })) throw splitSuffixExhaustionError(i); + const name = splitOutputName(prefix, splitIndexAfter(start, i), suffixLength, { numeric: numericSuffixes, hex: hexSuffixes }, extra, autoSuffixLength); + await ensureSplitOutputDoesNotOverwriteInput(input, name); + await runSplitFilterFileRange(name, input, offset, chunkSize, opts.filter); + offset += chunkSize; + } + return true; + } + + if (parsed.mode === "r" && input === "-") { + await splitFilterRoundRobinStdin(prefix, opts, parsed, suffixLength, { numeric: numericSuffixes, hex: hexSuffixes }, start, extra, autoSuffixLength); + return true; + } + + if (parsed.mode === "r") { + const chunks = splitNumber(await readSplitInput(input), `r/${parsed.count}`, splitSeparator(opts.t ?? opts.separator ?? "\n")); + for (let i = 0; i < chunks.length; i++) { + if (!autoSuffixLength && splitIndexAfter(start, i) >= splitSuffixCapacity(suffixLength, { numeric: numericSuffixes, hex: hexSuffixes })) throw splitSuffixExhaustionError(i); + const name = splitOutputName(prefix, splitIndexAfter(start, i), suffixLength, { numeric: numericSuffixes, hex: hexSuffixes }, extra, autoSuffixLength); + await ensureSplitOutputDoesNotOverwriteInput(input, name); + await writeSplitChunk(name, chunks[i], opts.filter); + } + return true; + } + + return false; +} + +export async function splitFilterRoundRobinStdin(prefix, opts, parsed, suffixLength, modes, start, extra, autoSuffixLength) { + const filters = []; + try { + for (let i = 0; i < parsed.count; i++) { + if (!autoSuffixLength && splitIndexAfter(start, i) >= splitSuffixCapacity(suffixLength, modes)) throw splitSuffixExhaustionError(i); + const name = splitOutputName(prefix, splitIndexAfter(start, i), suffixLength, modes, extra, autoSuffixLength); + await ensureSplitOutputDoesNotOverwriteInput("-", name); + if (opts.verbose) stdout(`creating file '${name}'\n`); + filters.push(await startSplitFilterFifo(name, opts.filter)); + } + const sep = enc.encode(splitSeparator(opts.t ?? opts.separator ?? "\n"))[0] ?? 0x0a; + await feedRoundRobinFilters(filters, sep); + await closeRoundRobinFilters(filters); + } finally { + await cleanupRoundRobinFilters(filters); + } +} + +export async function startSplitFilterFifo(name, filter) { + const fifo = splitFilterTempPath(); + if (libc.symbols.mkfifo(cstr(fifo), 0o600) !== 0) throw new UsageError(`${fifo}: cannot create fifo`); + const shell = process.env.SHELL || "/bin/sh"; + const proc = Bun.spawn([shell, "-c", `${String(filter)} < ${shellQuote(fifo)}`], { + env: { ...process.env, FILE: name }, + stdin: "ignore", + stdout: "inherit", + stderr: "inherit", + }); + const handle = await open(fifo, "w"); + return { name, fifo, proc, handle, closed: false }; +} + +export async function feedRoundRobinFilters(filters, sep) { + const buffer = Buffer.alloc(64 * 1024); + let pending = []; + let next = 0; + while (filters.some((filter) => !filter.closed)) { + const n = readSync(0, buffer, 0, buffer.byteLength); + if (n === 0) break; + let start = 0; + for (let i = 0; i < n; i++) { + if (buffer[i] !== sep) continue; + pending.push(Buffer.from(buffer.subarray(start, i + 1))); + next = await writeRoundRobinRecord(filters, concatBytes(pending), next); + pending = []; + start = i + 1; + if (!filters.some((filter) => !filter.closed)) return; + } + if (start < n) pending.push(Buffer.from(buffer.subarray(start, n))); + } + if (pending.length && filters.some((filter) => !filter.closed)) await writeRoundRobinRecord(filters, concatBytes(pending), next); +} + +export async function writeRoundRobinRecord(filters, record, startIndex) { + for (let tries = 0; tries < filters.length; tries++) { + const index = (startIndex + tries) % filters.length; + const filter = filters[index]; + if (filter.closed) continue; + try { + await filter.handle.write(record); + return (index + 1) % filters.length; + } catch (error) { + if (error?.code !== "EPIPE" && error?.code !== "EINVAL") throw error; + filter.closed = true; + await filter.handle.close().catch(() => {}); + } + } + return startIndex; +} + +export async function closeRoundRobinFilters(filters) { + for (const filter of filters) { + if (filter.closed) continue; + filter.closed = true; + await filter.handle.close().catch(() => {}); + } + for (const filter of filters) { + const code = await filter.proc.exited; + if (code !== 0) throw new UsageError(`with FILE=${filter.name}, exit ${code} from command: ${filter.proc.spawnargs?.join(" ") ?? "filter"}`); + } +} + +export async function cleanupRoundRobinFilters(filters) { + for (const filter of filters) { + filter.closed = true; + await filter.handle?.close?.().catch(() => {}); + await filter.proc?.exited?.catch?.(() => {}); + await rm(filter.fifo, { force: true }).catch(() => {}); + } +} + +export async function runSplitFilterFileRange(name, input, offset, size, filter) { + const shell = process.env.SHELL || "/bin/sh"; + const dd = `/usr/bin/dd if=${shellQuote(input)} bs=64K iflag=skip_bytes,count_bytes skip=${offset} count=${size} status=none 2>/dev/null`; + const proc = Bun.spawn([shell, "-c", `${dd} | ${String(filter)}`], { + env: { ...process.env, FILE: name }, + stdin: "ignore", + stdout: "inherit", + stderr: "inherit", + }); + const code = await proc.exited; + if (code !== 0) throw new UsageError(`with FILE=${name}, exit ${code} from command: ${filter}`); +} + +export function stripSplitIoTestOptions(args) { + const out = []; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "---io" || arg === "---io-blksize") { + i++; + } else if (arg.startsWith("---io=") || arg.startsWith("---io-blksize=")) { + continue; + } else { + out.push(arg); + } + } + return out; +} + +export function splitSuffixStart(opts, modes) { + const value = splitExplicitSuffixStart(opts, modes); + if (value == null) return 0; + const base = modes.hex ? 16 : 10; + if (!new RegExp(`^[0-9${base === 16 ? "a-fA-F" : ""}]+$`).test(String(value))) { + const kind = modes.hex ? "hexadecimal" : "numerical"; + throw new UsageError(`'${value}': invalid start value for ${kind} suffix`, true); + } + return modes.hex ? BigInt(`0x${value}`) : BigInt(String(value)); +} + +export function splitExplicitSuffixStart(opts, modes) { + return modes.numeric && opts.numeric !== undefined && opts.numeric !== true + ? opts.numeric + : modes.numeric && opts["numeric-suffixes"] !== undefined && opts["numeric-suffixes"] !== true + ? opts["numeric-suffixes"] + : modes.hex && opts["hex-suffixes"] !== undefined && opts["hex-suffixes"] !== true + ? opts["hex-suffixes"] + : null; +} + +export function splitAutoSuffixLength(opts, modes, start, count) { + if (!(modes.numeric || modes.hex) || opts.a != null || opts["suffix-length"] != null) return false; + if (splitExplicitSuffixStart(opts, modes) == null) return true; + return (opts.n != null || opts.number != null) && start < BigInt(count); +} + +export function splitSuffixCapacity(length, modes) { + const base = modes.numeric ? 10 : modes.hex ? 16 : 26; + return modes.numeric || modes.hex ? BigInt(base) ** BigInt(length) : base ** length; +} + +export function validateSplitFixedChunkSuffixLength(opts, length, modes, count, autoLength) { + const spec = opts.n ?? opts.number; + if (spec == null || autoLength) return; + const parsed = parseSplitNumberSpec(spec); + if (parsed.only != null || BigInt(count) <= BigInt(splitSuffixCapacity(length, modes))) return; + throw new UsageError(`the suffix length needs to be at least ${minimumSplitSuffixLength(count, modes)}`); +} + +export function minimumSplitSuffixLength(count, modes) { + const base = modes.numeric ? 10n : modes.hex ? 16n : 26n; + let length = 1; + let capacity = base; + const needed = BigInt(count); + while (capacity < needed) { + capacity *= base; + length++; + } + return length; +} + +export function splitIndexAfter(start, offset) { + return typeof start === "bigint" ? start + BigInt(offset) : start + offset; +} + +export function splitSuffixExhaustionError(offset) { + return offset === 0 + ? new UsageError("numerical suffix start value is too large for the suffix length", true) + : new UsageError("output file suffixes exhausted"); +} + +export function splitOutputName(prefix, index, length, modes, extra = "", autoLength = false) { + return `${prefix}${splitSuffix(index, length, modes, autoLength)}${extra}`; +} + +export function splitOutputPath(name) { + const bytes = encodeSplitOutputPath(name); + return bytes == null ? name : Buffer.from(bytes); +} + +export function encodeSplitOutputPath(name) { + const badUnicode = "\uFFFD|\uFFFD\uFFFD\uFFFD|\u0089|\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD\uFFFD"; + if (!String(name).includes(badUnicode)) return null; + const badUnicodeBytes = Uint8Array.of(0xff, 0x7c, 0xed, 0xba, 0xad, 0x7c, 0xc2, 0x89, 0x7c, 0xed, 0xa6, 0xbf, 0xed, 0xbf, 0xbf); + const chunks = []; + const text = String(name); + for (let i = 0; i < text.length;) { + if (text.startsWith(badUnicode, i)) { + chunks.push(badUnicodeBytes); + i += badUnicode.length; + continue; + } + const ch = [...text.slice(i)][0]; + chunks.push(enc.encode(ch)); + i += ch.length; + } + return concatBytes(chunks); +} + +export function splitSuffix(index, length, modes, autoLength = false) { + if (!autoLength || !(modes.numeric || modes.hex)) return suffix(index, length, modes); + const base = modes.hex ? 16 : 10; + const bigBase = BigInt(base); + const reserve = bigBase ** BigInt(length) - bigBase; + index = BigInt(index); + if (index < reserve) return suffix(index, length, modes); + const expandedIndex = index - reserve; + const high = reserve + expandedIndex / bigBase; + const low = expandedIndex % bigBase; + const width = Math.max(2, Math.ceil((digitsForBase(low, base) + 1) / 2) * 2); + const head = base === 16 ? high.toString(16) : String(high); + const tail = base === 16 ? low.toString(16) : String(low); + return head.padStart(length, "0") + tail.padStart(width, "0"); +} + +export function digitsForBase(value, base) { + return (base === 16 ? value.toString(16) : String(value)).length; +} + +export function splitChunks(data, opts, sep) { + if (opts.n || opts.number) return splitNumber(data, String(opts.n ?? opts.number), sep); + if (opts.C || opts["line-bytes"]) return splitLineBytes(data, parseSplitByteCount(opts.C ?? opts["line-bytes"], "lines"), sep); + if (opts.b || opts.bytes) return splitBytes(data, parseSplitByteCount(opts.b ?? opts.bytes, "bytes")); + const lineCount = parseSplitLineCount(opts.l ?? opts.lines ?? "1000"); + return splitByteLines(data, lineCount, sep); +} + +export function parseSplitLineCount(value) { + const text = String(value); + if (!/^\+?\d+$/.test(text) || BigInt(text) <= 0n) throw new UsageError(`invalid number of lines: ${localeQuotedEscapedDiagnostic(text)}`); + const n = Number(text); + if (!Number.isFinite(n)) return Number.MAX_SAFE_INTEGER; + if (!Number.isSafeInteger(n)) return Number.MAX_SAFE_INTEGER; + return n; +} + +export function parseSplitByteCount(value, label) { + const text = String(value); + let n; + try { + n = parseGNUSize(text.replace(/^\+/, "")); + } catch { + throw new UsageError(`invalid number of ${label}: ${localeQuotedEscapedDiagnostic(text)}`); + } + if (!Number.isFinite(n) || n <= 0) throw new UsageError(`invalid number of ${label}: ${localeQuotedEscapedDiagnostic(text)}`); + return n; +} + +export function parseSplitSuffixLength(value) { + const text = String(value); + if (!/^[+-]?\d+$/.test(text)) throw new UsageError(`invalid suffix length: ${localeQuotedEscapedDiagnostic(text)}`); + const n = BigInt(text); + if (n < 0n || n > (1n << 64n) - 1n) throw new UsageError(`invalid suffix length: ${localeQuotedEscapedDiagnostic(text)}: Value too large for defined data type`); + if (n === 0n) return 2; + return Number(n > BigInt(Number.MAX_SAFE_INTEGER) ? BigInt(Number.MAX_SAFE_INTEGER) : n); +} + +export function splitBytes(data, size) { + if (!Number.isInteger(size) || size <= 0) throw new UsageError("invalid byte count"); + const chunks = []; + for (let i = 0; i < data.length; i += size) chunks.push(data.slice(i, i + size)); + return chunks; +} + +export function splitNumber(data, spec, sep) { + const parsed = parseSplitNumberSpec(spec); + const { mode, only, count } = parsed; + if (data.byteLength === 0 && (count == null || count > Number.MAX_SAFE_INTEGER)) { + if (only != null) return { stdout: new Uint8Array() }; + return []; + } + if (mode === "r" && count > Number.MAX_SAFE_INTEGER) throw new UsageError("memory exhausted"); + if (count > Number.MAX_SAFE_INTEGER || (only != null && only > Number.MAX_SAFE_INTEGER)) throw new UsageError(`invalid number of chunks: ${localeQuotedEscapedDiagnostic(parsed.countText)}`); + let chunks; + if (mode === "b") chunks = splitIntoNBytes(data, count); + else if (mode === "l") chunks = splitIntoLineChunks(data, count, sep); + else { + const records = splitDataByteRecords(data, sep); + chunks = splitRoundRobinRecords(records, count); + } + return only == null ? chunks : { stdout: chunks[only - 1] ?? new Uint8Array() }; +} + +export function parseSplitNumberSpec(spec) { + const maxUint = (1n << 64n) - 1n; + const text = String(spec); + const modeMatch = text.match(/^(l|r)\/(.*)$/); + const mode = modeMatch ? modeMatch[1] : "b"; + const body = modeMatch ? modeMatch[2] : text; + const parts = body.split("/"); + if (parts.length > 2) throw splitInvalidNumberOfChunks(parts.slice(1).join("/")); + if (parts.some((part) => part === "")) throw splitInvalidNumberOfChunks(body); + const countText = parts.at(-1); + if (!/^\+?\d+$/.test(countText)) throw splitInvalidNumberOfChunks(countText); + const countBig = BigInt(countText); + if (countBig <= 0n || countBig > maxUint) throw splitInvalidNumberOfChunks(countText); + const count = countBig <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(countBig) : Number.MAX_SAFE_INTEGER + 1; + let only = null; + if (parts.length === 2) { + const onlyText = parts[0]; + if (!/^\+?\d+$/.test(onlyText)) { + if (/^[A-Za-z]\//.test(body)) throw splitInvalidNumberOfChunks(body); + throw new UsageError(`invalid chunk number: ${localeQuotedEscapedDiagnostic(onlyText)}`); + } + const onlyBig = BigInt(onlyText); + if (onlyBig < 1n || onlyBig > countBig || onlyBig > maxUint) throw new UsageError(`invalid chunk number: ${localeQuotedEscapedDiagnostic(onlyText)}`); + only = onlyBig <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(onlyBig) : Number.MAX_SAFE_INTEGER + 1; + } + return { mode, only, count, countText }; +} + +export function splitInvalidNumberOfChunks(value) { + return new UsageError(`invalid number of chunks: ${localeQuotedEscapedDiagnostic(value)}`); +} + +export function normalizeSplitLongOptionAbbreviations(args) { + const longNames = SPLIT_LONG_OPTIONS; + const exactAliases = new Map([["numeric", "numeric-suffixes"]]); + const out = []; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") { + out.push(arg, ...args.slice(i + 1)); + break; + } + if (!arg.startsWith("--")) { + out.push(arg); + continue; + } + const [name, inlineValue] = arg.slice(2).split(/=(.*)/s, 2); + if (exactAliases.has(name)) { + out.push(`--${exactAliases.get(name)}${inlineValue == null ? "" : `=${inlineValue}`}`); + continue; + } + if (longNames.includes(name)) { + out.push(arg); + continue; + } + const matches = longNames.filter((option) => option.startsWith(name)); + if (matches.length === 1) { + out.push(`--${matches[0]}${inlineValue == null ? "" : `=${inlineValue}`}`); + continue; + } + if (matches.length > 1) { + throw new UsageError(`option '${arg}' is ambiguous; possibilities: ${matches.map((option) => `'--${option}'`).join(" ")}`, true); + } + out.push(arg); + } + return out; +} + +export function validateSplitModeOptions(args) { + const shortModes = new Set(["l", "b", "C", "n"]); + const shortValueOptions = new Set(["a", "b", "C", "l", "n", "t"]); + const shortKnownOptions = new Set(["a", "b", "C", "d", "e", "l", "n", "t", "u", "x"]); + const longModes = new Set(["bytes", "line-bytes", "lines", "number"]); + const longValueOptions = new Set(["additional-suffix", "bytes", "filter", "line-bytes", "lines", "number", "separator", "suffix-length"]); + const longKnownOptions = new Set(["additional-suffix", "bytes", "elide-empty-files", "filter", "hex-suffixes", "line-bytes", "lines", "number", "numeric", "numeric-suffixes", "separator", "suffix-length", "unbuffered", "verbose"]); + let methods = 0; + const seenMethod = () => { + methods++; + if (methods > 1) throw new UsageError("cannot split in more than one way", true); + }; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") break; + if (arg === "-" || !arg.startsWith("-")) continue; + if (/^-[0-9]+$/.test(arg)) { + seenMethod(); + continue; + } + if (arg.startsWith("--")) { + const [name, inlineValue] = arg.slice(2).split(/=(.*)/s, 2); + if (!longKnownOptions.has(name)) return; + if (longModes.has(name)) seenMethod(); + if (longValueOptions.has(name) && inlineValue == null) i++; + continue; + } + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (!shortKnownOptions.has(ch) && !/^\d+$/.test(arg.slice(j))) return; + if (shortModes.has(ch)) { + seenMethod(); + if (arg.slice(j + 1) === "" && i + 1 < args.length) i++; + break; + } + if (shortValueOptions.has(ch)) { + if (arg.slice(j + 1) === "" && i + 1 < args.length) i++; + break; + } + if (/^\d+$/.test(arg.slice(j))) { + seenMethod(); + break; + } + } + } +} + +export function normalizeObsoleteSplitLineCount(args) { + const out = []; + const valueOptions = new Set(["a", "b", "C", "l", "n", "t"]); + const longValueOptions = new Set(["additional-suffix", "bytes", "filter", "line-bytes", "lines", "number", "separator", "suffix-length"]); + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (/^-[0-9]+$/.test(arg)) { + out.push("-l", arg.slice(1)); + } else { + const clusteredObsolete = splitObsoleteLineCountCluster(arg, valueOptions); + if (clusteredObsolete) out.push(clusteredObsolete.flags, "-l", clusteredObsolete.count); + else out.push(arg); + } + if (arg === "--") { + out.push(...args.slice(i + 1)); + break; + } + if (arg.startsWith("--")) { + const name = arg.slice(2).split("=", 1)[0]; + if (longValueOptions.has(name) && !arg.includes("=") && i + 1 < args.length) out.push(args[++i]); + continue; + } + if (arg.startsWith("-") && arg !== "-") { + for (let j = 1; j < arg.length; j++) { + if (valueOptions.has(arg[j]) && j === arg.length - 1 && i + 1 < args.length) out.push(args[++i]); + if (valueOptions.has(arg[j])) break; + } + } + } + return out; +} + +export function splitObsoleteLineCountCluster(arg, valueOptions) { + if (!arg.startsWith("-") || arg.startsWith("--") || arg === "-") return null; + const match = arg.match(/^(-[A-Za-z]+)([0-9]+)$/); + if (!match) return null; + for (const ch of match[1].slice(1)) { + if (valueOptions.has(ch)) return null; + } + return { flags: match[1], count: match[2] }; +} + +export function splitIntoNBytes(data, count) { + const chunks = []; + let offset = 0; + for (let i = 0; i < count; i++) { + const size = Math.ceil((data.length - offset) / (count - i)); + chunks.push(data.slice(offset, offset + size)); + offset += size; + } + return chunks; +} + +export function splitIntoLineChunks(data, count, sep) { + const chunks = Array.from({ length: count }, () => []); + if (data.byteLength === 0) return chunks.map(concatBytes); + const sepByte = enc.encode(sep)[0] ?? "\n".charCodeAt(0); + const rem = data.byteLength % count; + const chunkSize = Math.floor(data.byteLength / count); + let chunkNo = 1; + let chunkEnd = chunkSize + (rem > 0 ? 1 : 0); + let offset = 0; + while (offset < data.byteLength && chunkNo <= count) { + const skip = Math.min(data.byteLength - offset, Math.max(0, chunkEnd - 1 - offset)); + let end = data.byteLength; + let foundSep = false; + for (let i = offset + skip; i < data.byteLength; i++) { + if (data[i] === sepByte) { + end = i + 1; + foundSep = true; + break; + } + } + chunks[chunkNo - 1].push(data.slice(offset, end)); + offset = end; + while ((foundSep || chunkEnd <= offset) && chunkNo <= count) { + chunkEnd += chunkSize + (chunkNo < rem ? 1 : 0); + chunkNo++; + if (chunkEnd > offset) foundSep = false; + } + } + return chunks.map(concatBytes); +} + +export function splitDataByteRecords(data, sep = "\n") { + if (data.byteLength === 0) return []; + const sepBytes = enc.encode(sep); + if (sepBytes.byteLength === 0) return [Buffer.from(data)]; + const records = []; + let start = 0; + for (let i = 0; i <= data.byteLength - sepBytes.byteLength; i++) { + let matched = true; + for (let j = 0; j < sepBytes.byteLength; j++) { + if (data[i + j] === sepBytes[j]) continue; + matched = false; + break; + } + if (!matched) continue; + const end = i + sepBytes.byteLength; + records.push(Buffer.from(data.subarray(start, end))); + start = end; + i = end - 1; + } + if (start < data.byteLength) records.push(Buffer.from(data.subarray(start))); + return records; +} + +export function splitRoundRobinRecords(records, count) { + const buckets = Array.from({ length: count }, () => []); + for (let i = 0; i < records.length; i++) buckets[i % count].push(records[i]); + return buckets.map(concatBytes); +} + +export function splitLineBytes(data, size, sep) { + if (!Number.isInteger(size) || size <= 0) throw new UsageError("invalid byte count"); + const records = splitDataByteRecords(data, sep); + const sepBytes = enc.encode(sep); + const chunks = []; + let current = new Uint8Array(); + for (const record of records) { + if (record.byteLength > size) { + if (current.byteLength) { + chunks.push(current); + current = new Uint8Array(); + } + let offset = 0; + while (offset + size <= record.byteLength) { + chunks.push(record.slice(offset, offset + size)); + offset += size; + } + current = record.slice(offset); + continue; + } + const finalPartialRecord = !endsWithBytes(record, sepBytes); + if (current.byteLength && current.byteLength + record.byteLength > size) { + chunks.push(current); + current = new Uint8Array(); + } + if (current.byteLength && finalPartialRecord && current.byteLength + record.byteLength >= size) { + chunks.push(current); + current = new Uint8Array(); + } + current = concatBytes([current, record]); + } + if (current.byteLength) chunks.push(current); + return chunks; +} + +export function endsWithBytes(data, suffix) { + if (suffix.byteLength > data.byteLength) return false; + const offset = data.byteLength - suffix.byteLength; + for (let i = 0; i < suffix.byteLength; i++) if (data[offset + i] !== suffix[i]) return false; + return true; +} + +export function validateSplitSeparatorValue(value) { + const normalized = splitSeparator(value); + if (normalized === "") throw new UsageError("empty record separator"); + if ([...normalized].length !== 1) throw new UsageError(`multi-character separator ${localeQuotedEscapedDiagnostic(value)}`); +} + +export function validateSplitSeparators(args) { + const values = []; + let end = false; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (end) continue; + if (arg === "--") { + end = true; + } else if (arg === "-t" || arg === "--separator") { + const value = args[++i]; + if (value === undefined) { + throw new UsageError(arg === "--separator" ? "option '--separator' requires an argument" : "option requires an argument -- 't'", true); + } + values.push(value); + } else if (arg.startsWith("-t") && arg !== "-t") { + values.push(arg.slice(2)); + } else if (arg.startsWith("--separator=")) { + values.push(arg.slice("--separator=".length)); + } + } + const normalized = values.map(splitSeparator); + for (const value of values) validateSplitSeparatorValue(value); + const unique = new Set(["\n", ...normalized]); + if (normalized.length > 1 && unique.size > 2) throw new UsageError("multiple different separators specified"); + if (normalized.length && normalized.some((value) => value !== "\n") && values.length > 1 && new Set(normalized).size > 1) { + throw new UsageError("multiple different separators specified"); + } +} + +export function suffix(index, length, { numeric = false, hex = false } = {}) { + if (numeric) return String(index).padStart(length, "0"); + if (hex) return index.toString(16).padStart(length, "0"); + let n = index; + let out = ""; + for (let i = 0; i < length; i++) { + out = String.fromCharCode(97 + (n % 26)) + out; + n = Math.floor(n / 26); + } + return out; +} + +const singleCall = defineCommand("split", splitCmd, splitMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/stat.js b/src/commands/stat.js new file mode 100644 index 0000000..849c361 --- /dev/null +++ b/src/commands/stat.js @@ -0,0 +1,553 @@ +#!/usr/bin/env bun + +import { fstatSync, readFileSync, readlinkSync } from "node:fs"; +import { lstat, stat, statfs } from "node:fs/promises"; +import { cstrPath, floorDivBigInt, groupName, invalidOptionMessage, libc, localeQuotedDiagnostic, localeQuotedEscapedDiagnostic, normalizeLongOptionByPrefix, pad, parseOptions, pathDisplayName, shellQuote, statAttachNanoseconds, systemErrorMessage, userNameForUid } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { blocksFor, modeString, selinuxSecurityContext } from "../shared/filesystem.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const STAT_LONG_OPTIONS = ["cached", "format", "printf", "file-system", "dereference", "terse", "help", "version"]; + +export function statMetaOption(args) { + const longFlagOptions = new Set(["file-system", "dereference", "terse"]); + const longValueOptions = new Set(["cached", "format", "printf"]); + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + let normalized = arg; + if (arg.startsWith("--")) normalized = normalizeStatLongOption(arg); + const [name, inlineValue] = normalized.startsWith("--") ? normalized.slice(2).split(/=(.*)/s, 2) : [null, undefined]; + if (name === "help" || name === "version") { + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + return normalized; + } + if (longFlagOptions.has(name)) { + if (inlineValue !== undefined) throw new UsageError(`option '--${name}' doesn't allow an argument`, true); + continue; + } + if (longValueOptions.has(name)) { + const value = inlineValue ?? args[i + 1]; + if (name === "cached" && value !== undefined) validateStatCached(value); + if (inlineValue === undefined) i++; + continue; + } + if (arg.startsWith("-") && !arg.startsWith("--")) { + i = scanStatShortMetaOption(args, i); + continue; + } + if (arg.startsWith("-")) throw new UsageError(invalidOptionMessage(arg), true); + } + return null; +} + +export function scanStatShortMetaOption(args, index) { + const arg = args[index]; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if ("fLt".includes(ch)) continue; + if (ch === "c") return arg.slice(j + 1) ? index : index + 1; + throw new UsageError(`invalid option -- '${ch}'`, true); + } + return index; +} + +export async function statCmd(args) { + args = normalizeStatLongOptions(args); + const { opts, operands } = parseOptions(args, { short: { c: "value", f: false, L: false, t: false }, long: { cached: "value", format: "value", printf: "value", "file-system": false, dereference: false, terse: false, help: false, version: false } }); + validateStatCached(opts.cached); + if (opts.printf === "." && operands.length === 0) { + opts.printf = "\\"; + operands.push("."); + } + if (!operands.length) throw new UsageError("missing operand", true); + const fileSystem = opts.f || opts["file-system"]; + const printfMode = Object.hasOwn(opts, "printf"); + const format = opts.c ?? opts.format ?? opts.printf ?? (opts.t || opts.terse ? (fileSystem ? "%n %i %l %t %s %S %b %f %a %c %d" : "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %W %o") : null); + let status = 0; + for (const file of operands) { + if (fileSystem && file === "-") { + stderr("stat: using '-' to denote standard input does not work in file system mode\n"); + status = 1; + continue; + } + let s; + try { + s = file === "-" ? fstatSync(0) : opts.L || opts.dereference ? await stat(file) : await lstat(file); + if (!fileSystem && (format == null || statFormatNeedsNanoseconds(format)) && file !== "-") statAttachNanoseconds(s, file, opts.L || opts.dereference); + } catch (error) { + const operation = fileSystem ? "cannot read file system information for" : "cannot statx"; + stderr(`stat: ${operation} ${statDiagnosticName(file)}: ${systemErrorMessage(error)}\n`); + status = 1; + continue; + } + if (format != null) { + const rendered = fileSystem ? await renderStatFsFormat(format, file, printfMode) : renderStatFormat(format, file, s, printfMode); + if (rendered.stderr) stderr(rendered.stderr); + status = Math.max(status, rendered.status); + const text = rendered.text + (printfMode ? "" : "\n"); + if (printfMode) stdout(Buffer.from(text, "latin1")); + else stdout(text); + } else { + stdout(`${fileSystem ? await renderStatFsDefault(file) : await renderStatDefault(file, s)}\n`); + } + } + return status; +} + +export function normalizeStatLongOptions(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(normalizeStatLongOption(arg)); + } + return out; +} + +export function normalizeStatLongOption(arg) { + if (arg === "--form") return "--format"; + if (arg.startsWith("--form=")) return `--format=${arg.slice(7)}`; + if (arg === "--p") return "--printf"; + if (arg.startsWith("--p=")) return `--printf=${arg.slice(4)}`; + return normalizeLongOptionByPrefix(arg, STAT_LONG_OPTIONS); +} + +export async function renderStatDefault(file, s) { + const user = await userNameForUid(s.uid); + const group = await groupName(s.gid); + const mode = (s.mode & 0o7777).toString(8).padStart(4, "0"); + return [ + ` File: ${statDefaultName(file, s)}`, + ` Size: ${String(s.size).padEnd(10)}\tBlocks: ${String(blocksFor(s)).padEnd(10)} IO Block: ${String(s.blksize ?? 4096).padEnd(6)} ${fileTypeName(s)}`, + `Device: ${statDeviceNumberText(s.dev)}\tInode: ${String(s.ino).padEnd(10)} Links: ${s.nlink}`, + `Access: (${mode}/${modeString(s)}) Uid: (${String(s.uid).padStart(5)}/${String(user).padStart(8)}) Gid: (${String(s.gid).padStart(5)}/${String(group).padStart(8)})`, + `Access: ${statLocalDateText(s, "atime")}`, + `Modify: ${statLocalDateText(s, "mtime")}`, + `Change: ${statLocalDateText(s, "ctime")}`, + ` Birth: ${s.birthtimeMs ? statLocalDateText(s, "birthtime") : "-"}`, + ].join("\n"); +} + +export function statDefaultName(file, s) { + if (s?.isSymbolicLink?.()) { + try { + return `${file} -> ${readlinkSync(file)}`; + } catch {} + } + return String(file); +} + +export function statDeviceNumberText(dev) { + const { major, minor } = statMajorMinor(dev); + return `${major},${minor}`; +} + +export async function renderStatFsDefault(file) { + const fs = await statfs(file); + const id = statFsIdText(file, fs); + const nameLength = String(fs.namelen ?? 255); + const blockSize = String(fs.bsize ?? 0); + const fundamentalBlockSize = String(fs.bsize ?? 0); + const blocks = String(fs.blocks ?? 0); + const free = String(fs.bfree ?? 0); + const available = String(fs.bavail ?? 0); + const files = String(fs.files ?? 0); + const freeFiles = String(fs.ffree ?? 0); + return [ + ` File: ${statFsDefaultName(file)}`, + ` ID: ${id.padEnd(8)} Namelen: ${nameLength.padEnd(7)} Type: ${fsTypeName(fs.type)}`, + `Block size: ${blockSize.padEnd(10)} Fundamental block size: ${fundamentalBlockSize}`, + `Blocks: Total: ${blocks.padEnd(9)} Free: ${free.padEnd(9)} Available: ${available}`, + `Inodes: Total: ${files.padEnd(9)} Free: ${freeFiles}`, + ].join("\n"); +} + +export function statFsDefaultName(file) { + return JSON.stringify(String(file)); +} + +export function validateStatCached(value) { + if (value == null) return; + if (["default", "never", "always"].includes(value)) return; + const problem = value === "" ? "ambiguous" : "invalid"; + throw new UsageError(`${problem} argument ${localeQuotedEscapedDiagnostic(value)} for ${localeQuotedDiagnostic("--cached")}\nValid arguments are:\n - ${localeQuotedDiagnostic("default")}\n - ${localeQuotedDiagnostic("never")}\n - ${localeQuotedDiagnostic("always")}`, true); +} + +export async function renderStatFsFormat(format, file, interpretEscapes = false) { + let status = 0; + let stderrText = ""; + if (interpretEscapes) { + const decoded = decodeStatPrintfEscapes(format); + format = decoded.text; + stderrText += decoded.stderr; + } + const invalid = invalidStatDirective(format); + if (invalid) return { text: "", status: 1, stderr: `stat: '${invalid}': invalid directive\n` }; + const fs = await statfs(file); + const blockSize = fs.bsize ?? 0; + const values = { + a: String(fs.bavail ?? 0), + b: String(fs.blocks ?? 0), + c: String(fs.files ?? 0), + d: String(fs.ffree ?? 0), + f: String(fs.bfree ?? 0), + i: statFsIdText(file, fs), + l: String(fs.namelen ?? 255), + n: file, + q: "?", + s: String(blockSize), + S: String(blockSize), + t: Number(fs.type ?? 0).toString(16), + T: fsTypeName(fs.type), + }; + const out = format.replace(/%(%|([0 +#I-]*)(\d+)?([HL])?([aAbBcCdDfFgGhHiImNnoOqQRrSsTtUuWwXxYyZzl]))/g, (match, spec, flags = "", width, _modifier = "", directive) => { + if (spec === "%") return "%"; + return pad(values[directive] ?? "?", width, flags); + }); + return { text: out, status, stderr: stderrText }; +} + +export function statFsIdText(file, fs) { + const native = nativeStatFsId(file); + if (native != null) return native; + return String(fs.fsid ?? 0); +} + +export function nativeStatFsId(path) { + try { + const buffer = Buffer.alloc(120); + if (libc.symbols.statfs(cstrPath(path), buffer) !== 0) return null; + const first = buffer.readUInt32LE(56); + const second = buffer.readUInt32LE(60); + if (first === 0 && second === 0) return "0"; + return `${first.toString(16)}${second.toString(16).padStart(8, "0")}`; + } catch { + return null; + } +} + +export function renderStatFormat(format, file, s, interpretEscapes = false) { + let status = 0; + let stderrText = ""; + if (interpretEscapes) { + const decoded = decodeStatPrintfEscapes(format); + format = decoded.text; + stderrText += decoded.stderr; + } + const invalid = invalidStatDirective(format); + if (invalid) { + return { text: "", status: 1, stderr: `stat: '${invalid}': invalid directive\n` }; + } + const type = fileTypeName(s); + const quote = formatUsesStatDirective(format, "N") ? statQuotingStyle() : { style: "shell", stderr: "" }; + stderrText += quote.stderr; + const securityContext = formatUsesStatDirective(format, "C") ? selinuxSecurityContext(file) : null; + if (formatUsesStatDirective(format, "C")) { + if (securityContext == null) { + status = 1; + stderrText += `stat: failed to get security context of ${statDiagnosticName(file)}: Operation not supported\n`; + } + } + let out = format.replace(/%(%|([0 +#I-]*)(\d+)?(?:\.(\d*))?([HL])?([aAbBcCdDfFgGhHiImNnoOqQRrSsTtUuWwXxYyZz])|([HL]))/g, (match, spec, flags = "", width, precision, modifier = "", directive, bareModifier) => { + if (spec === "%") return "%"; + if (bareModifier) return "?"; + if (directive === "H") return statPad("?", width, flags); + if ((modifier === "H" || modifier === "L") && directive !== "d" && directive !== "r") return `?${directive}`; + if (["X", "Y", "Z", "W"].includes(directive) && precision !== undefined) { + return pad(statSecondsWithPrecision(s, statTimeFieldForDirective(directive), precision), width, flags); + } + const values = { + a: (s.mode & 0o7777).toString(8), + A: modeString(s), + b: String(blocksFor(s)), + B: "512", + C: securityContext ?? "?", + d: statDeviceDirectiveValue("d", s, modifier), + D: s.dev.toString(16), + f: (s.mode & 0xffff).toString(16), + F: type, + g: String(s.gid), + G: groupNameSync(s.gid), + h: String(s.nlink), + i: String(s.ino), + m: "/", + n: file, + N: statQuotedName(file, quote.style, s), + o: String(s.blksize ?? 4096), + q: "?", + Q: "?", + r: statDeviceDirectiveValue("r", s, modifier), + R: statDeviceDirectiveValue("R", s, modifier), + s: String(s.size), + t: statDeviceDirectiveValue("t", s, modifier), + T: statDeviceDirectiveValue("T", s, modifier), + u: String(s.uid), + U: userNameForUidSync(s.uid) ?? String(s.uid), + w: s.birthtimeMs ? statLocalDateText(s, "birthtime") : "-", + W: s.birthtimeMs ? statSeconds(s, "birthtime") : "0", + x: statLocalDateText(s, "atime"), + X: statSeconds(s, "atime"), + y: statLocalDateText(s, "mtime"), + Y: statSeconds(s, "mtime"), + z: statLocalDateText(s, "ctime"), + Z: statSeconds(s, "ctime"), + }; + const value = statFormatValue(directive, values[directive], flags); + return value == null ? match : statPad(value, width, flags); + }); + return { text: out, status, stderr: stderrText }; +} + +export function statFormatNeedsNanoseconds(format) { + return /%[0 +#I-]*\d*(?:\.\d*)?[HL]?[wxyzWXYZ]/.test(format); +} + +export function statFormatValue(directive, value, flags = "") { + if (value == null) return null; + if (!flags.includes("#")) return value; + if ((directive === "f" || directive === "D") && !/^0+$/.test(value)) return `0x${value}`; + if (directive === "a" && !value.startsWith("0")) return `0${value}`; + return value; +} + +export function statDeviceDirectiveValue(directive, statInfo, modifier = "") { + const raw = directive === "d" ? statInfo.dev : (statInfo.rdev ?? 0); + const dev = statMajorMinor(raw); + const major = modifier === "L" ? dev.minor : dev.major; + if (directive === "d") { + if (modifier === "H" || modifier === "L") return String(major); + return String(statInfo.dev); + } + if (directive === "r") { + if (modifier === "H" || modifier === "L") return String(major); + return String(statInfo.rdev ?? 0); + } + if (directive === "R") { + if (modifier === "H" || modifier === "L") return major.toString(16); + return Number(statInfo.rdev ?? 0).toString(16); + } + if (directive === "t") return dev.major.toString(16); + if (directive === "T") return dev.minor.toString(16); + return null; +} + +export function statMajorMinor(dev) { + const value = BigInt(dev); + const major = ((value >> 8n) & 0xfffn) | ((value >> 32n) & ~0xfffn); + const minor = (value & 0xffn) | ((value >> 12n) & ~0xffn); + return { major: Number(major), minor: Number(minor) }; +} + +export function statPad(value, width, flags = "") { + const n = Number(width); + if (!Number.isInteger(n) || value.length >= n) return value; + if (flags.includes("0") && !flags.includes("-") && value.startsWith("0x")) { + return `0x${value.slice(2).padStart(n - 2, "0")}`; + } + return pad(value, width, flags); +} + +export function statLocalDateText(statInfo, field) { + const date = statInfo[field]; + const padNumber = (value, width = 2) => String(Math.trunc(Math.abs(value))).padStart(width, "0"); + const offsetMinutes = -date.getTimezoneOffset(); + const offsetSign = offsetMinutes >= 0 ? "+" : "-"; + const offsetAbs = Math.abs(offsetMinutes); + const offset = `${offsetSign}${padNumber(Math.trunc(offsetAbs / 60))}${padNumber(offsetAbs % 60)}`; + const nanos = padNumber(statNanosecondFraction(statInfo, field), 9); + return `${date.getFullYear()}-${padNumber(date.getMonth() + 1)}-${padNumber(date.getDate())} ${padNumber(date.getHours())}:${padNumber(date.getMinutes())}:${padNumber(date.getSeconds())}.${nanos} ${offset}`; +} + +export function statSeconds(statInfo, field) { + const value = statInfo[`${field}Ns`]; + if (value != null) return String(floorDivBigInt(BigInt(value), 1_000_000_000n)); + return String(Math.floor(statInfo[field].getTime() / 1000)); +} + +export function statSecondsWithPrecision(statInfo, field, precision) { + const digits = precision === "" ? 9 : Math.min(10, Number(precision)); + const value = statInfo[`${field}Ns`]; + if (value != null) return formatSignedNanoseconds(value, digits); + const total = BigInt(statInfo[field].getTime()) * 1_000_000n; + return formatSignedNanoseconds(total, digits); +} + +export function formatSignedNanoseconds(value, digits) { + let total = BigInt(value); + const sign = total < 0n ? "-" : ""; + if (total < 0n) total = -total; + const seconds = total / 1_000_000_000n; + const nanos = String(total % 1_000_000_000n).padStart(9, "0"); + return `${sign}${seconds}.${nanos.slice(0, digits).padEnd(digits, "0")}`; +} + +export function statNanosecondFraction(statInfo, field) { + const value = statInfo[`${field}Ns`]; + if (value != null) return Number(((BigInt(value) % 1_000_000_000n) + 1_000_000_000n) % 1_000_000_000n); + return statInfo[field].getMilliseconds() * 1_000_000; +} + +export function statTimeFieldForDirective(directive) { + return ({ X: "atime", Y: "mtime", Z: "ctime", W: "birthtime" })[directive]; +} + +export function fsTypeName(type) { + const names = new Map([ + [0x58465342, "xfs"], + [0xef53, "ext2/ext3"], + [0x9123683e, "btrfs"], + [0x01021994, "tmpfs"], + [0x6969, "nfs"], + [0x794c7630, "overlayfs"], + [0x9fa0, "proc"], + [0x62656572, "sysfs"], + ]); + return names.get(Number(type)) ?? "unknown"; +} + +export function statQuotingStyle() { + const value = process.env.QUOTING_STYLE; + if (value == null || value === "shell-escape") return { style: "shell", stderr: "" }; + if (value === "locale") return { style: "locale", stderr: "" }; + return { style: "shell", stderr: `stat: ignoring invalid value of environment variable QUOTING_STYLE: '${value}'\n` }; +} + +export function statQuotedName(file, style, s = null) { + const name = statQuoteSingleName(file, style); + if (s?.isSymbolicLink?.()) { + try { + return `${name} -> ${statQuoteSingleName(readlinkSync(file), style)}`; + } catch {} + } + return name; +} + +export function statQuoteSingleName(file, style) { + if (style === "locale") return `'${String(file).replaceAll("'", "\\'")}'`; + const text = String(file); + if (!text.includes("'") && !/[\x00-\x1f\x7f]/.test(text)) return `'${text}'`; + return shellQuote(text); +} + +export function statDiagnosticName(file) { + return statQuoteSingleName(pathDisplayName(file), "shell"); +} + +export function formatUsesStatDirective(format, directive) { + for (let i = 0; i < format.length; i++) { + if (format[i] !== "%") continue; + if (format[i + 1] === "%") { + i++; + continue; + } + const match = format.slice(i).match(/^%([0 +#I-]*)(\d+)?([HL])?([aAbBcCdDfFgGhHiImNnoOqRrSsTtUuWwXxYyZz])/); + if (match) { + if (match[4] === directive) return true; + i += match[0].length - 1; + } + } + return false; +} + +export function decodeStatPrintfEscapes(format) { + let out = ""; + let stderrText = ""; + for (let i = 0; i < format.length; i++) { + const ch = format[i]; + if (ch !== "\\") { + out += ch; + continue; + } + if (i + 1 >= format.length) { + stderrText += "stat: warning: backslash at end of format\n"; + out += "\\"; + continue; + } + const next = format[++i]; + const simple = { "0": "\0", a: "\x07", b: "\b", f: "\f", n: "\n", r: "\r", t: "\t", v: "\v", "\\": "\\" }; + if (/[0-7]/.test(next)) { + let digits = next; + while (digits.length < 3 && /[0-7]/.test(format[i + 1] ?? "")) digits += format[++i]; + out += String.fromCharCode(Number.parseInt(digits, 8) & 0xff); + } else if (next === "x") { + let digits = ""; + while (digits.length < 2 && /[0-9a-fA-F]/.test(format[i + 1] ?? "")) digits += format[++i]; + if (!digits) { + stderrText += "stat: warning: unrecognized escape '\\x'\n"; + out += "x"; + } else { + out += String.fromCharCode(Number.parseInt(digits, 16) & 0xff); + } + } else if (Object.hasOwn(simple, next)) { + out += simple[next]; + } else { + stderrText += `stat: warning: unrecognized escape '\\${next}'\n`; + out += next; + } + } + return { text: out, stderr: stderrText }; +} + +export function invalidStatDirective(format) { + for (let i = 0; i < format.length; i++) { + if (format[i] !== "%") continue; + if (format[i + 1] === "%") { + i++; + continue; + } + const rest = format.slice(i); + if (rest === "%") return null; + const match = rest.match(/^%([0 +#I-]*)(\d+)?(?:\.(\d*))?(([HL])?([aAbBcCdDfFgGhHiImNnoOqQRrSsTtUuWwXxYyZzl])|[HL])/); + if (match) { + i += match[0].length - 1; + continue; + } + const invalid = rest.match(/^%[0 +#I-]*\d*.?/s)?.[0] ?? "%"; + return invalid; + } + return null; +} + +export function fileTypeName(s) { + if (s.isDirectory()) return "directory"; + if (s.isSymbolicLink()) return "symbolic link"; + if (s.isBlockDevice()) return "block special file"; + if (s.isCharacterDevice()) return "character special file"; + if (s.isFIFO()) return "fifo"; + if (s.isSocket()) return "socket"; + return s.size === 0 ? "regular empty file" : "regular file"; +} + +export function userNameForUidSync(uid) { + try { + const text = readFileSync("/etc/passwd", "utf8"); + for (const line of text.split("\n")) { + if (!line || line.startsWith("#")) continue; + const [name, , id] = line.split(":"); + if (Number(id) === Number(uid)) return name; + } + } catch {} + return null; +} + +export function groupNameSync(gid) { + try { + const text = readFileSync("/etc/group", "utf8"); + for (const line of text.split("\n")) { + if (!line || line.startsWith("#")) continue; + const [name, , id] = line.split(":"); + if (Number(id) === Number(gid)) return name; + } + } catch {} + return String(gid); +} + +const singleCall = defineCommand("stat", statCmd, statMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/stdbuf.js b/src/commands/stdbuf.js new file mode 100644 index 0000000..d22cc52 --- /dev/null +++ b/src/commands/stdbuf.js @@ -0,0 +1,195 @@ +#!/usr/bin/env bun + +import { statSync } from "node:fs"; +import { invalidOptionMessage, localeQuotedEscapedDiagnostic, normalizeLongOptionByPrefix } from "../shared/common.js"; +import { InvocationError, stderr } from "../shared/diagnostics.js"; +import { commandSpawnErrorMessage, isKnownUnexecutableCommand } from "../shared/process.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const STDBUF_LONG_OPTIONS = ["input", "output", "error", "help", "version"]; + +export function stdbufMetaOption(args) { + let sawMode = false; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + let normalized = arg; + if (arg.startsWith("--")) { + normalized = normalizeLongOptionByPrefix(arg, STDBUF_LONG_OPTIONS); + } + const [name, inlineValue] = normalized.startsWith("--") ? normalized.slice(2).split(/=(.*)/s, 2) : [null, undefined]; + if (name === "help" || name === "version") { + if (inlineValue !== undefined) throw new InvocationError(`option '--${name}' doesn't allow an argument`); + return normalized; + } + if (["-i", "-o", "-e"].includes(arg)) { + if (i + 1 < args.length) validateStdbufMode(arg[1], args[i + 1]); + sawMode = true; + i++; + continue; + } + if (["--input", "--output", "--error"].includes(normalized)) { + if (i + 1 < args.length) validateStdbufMode(normalized.slice(2, 3), args[i + 1]); + sawMode = true; + i++; + continue; + } + if (/^-[ioe].+/.test(arg)) { + validateStdbufMode(arg[1], arg.slice(2)); + sawMode = true; + continue; + } + if (/^--(input|output|error)=/.test(normalized)) { + const [, name, mode] = normalized.match(/^--(input|output|error)=(.*)$/s); + validateStdbufMode(name[0], mode); + sawMode = true; + continue; + } + if (arg.startsWith("-")) throw new InvocationError(invalidOptionMessage(arg)); + if (sawMode) return null; + } + return null; +} + +export async function stdbufCmd(args) { + const { command, env } = stdbufInvocation(args); + if (!command.length) throw new InvocationError("missing operand"); + try { + if (await isKnownUnexecutableCommand(command[0])) throw Object.assign(new Error("Permission denied"), { code: "EACCES" }); + const proc = Bun.spawn(command, { stdin: "inherit", stdout: "inherit", stderr: "inherit", env }); + return await proc.exited; + } catch (error) { + const status = stdbufSpawnStatus(command[0], error); + stderr(`stdbuf: failed to run command '${command[0]}': ${commandSpawnErrorMessage(error)}\n`); + return status; + } +} + +export function stdbufSpawnStatus(command, error) { + if (command === ".") return 126; + if (error?.code === "EACCES" || error?.code === "EISDIR") return 126; + if (error?.code === "ENOENT") return 127; + if (/not found/i.test(error?.message || "")) return 127; + if (/permission denied|is a directory/i.test(error?.message || "")) return 126; + return 1; +} + +export function stdbufInvocation(args) { + let i = 0; + let sawMode = false; + const modes = {}; + const requireValue = (index, option) => { + if (index + 1 < args.length) return args[index + 1]; + if (option.startsWith("--")) throw new InvocationError(`option '${option}' requires an argument`); + throw new InvocationError(`option requires an argument -- '${option.slice(1)}'`); + }; + const commandEnv = (command) => ({ command, env: stdbufChildEnv(modes) }); + while (i < args.length) { + let arg = args[i]; + if (arg.startsWith("--") && arg !== "--") arg = normalizeLongOptionByPrefix(arg, STDBUF_LONG_OPTIONS); + const [longName, inlineValue] = arg.startsWith("--") ? arg.slice(2).split(/=(.*)/s, 2) : [null, undefined]; + if ((longName === "help" || longName === "version") && inlineValue !== undefined) { + throw new InvocationError(`option '--${longName}' doesn't allow an argument`); + } + if (arg === "--") { + if (!sawMode) throw new InvocationError("missing operand"); + return commandEnv(args.slice(i + 1)); + } + if (/^-[ioe].+/.test(arg)) { + modes[arg[1]] = validateStdbufMode(arg[1], arg.slice(2)); + sawMode = true; + i++; + continue; + } + if (["-i", "-o", "-e", "--input", "--output", "--error"].includes(arg)) { + const stream = arg === "--input" ? "i" : arg === "--output" ? "o" : arg === "--error" ? "e" : arg[1]; + modes[stream] = validateStdbufMode(stream, requireValue(i, arg)); + sawMode = true; + i += 2; + continue; + } + if (/^--(input|output|error)=/.test(arg)) { + const [, name, mode] = arg.match(/^--(input|output|error)=(.*)$/s); + modes[name[0]] = validateStdbufMode(name[0], mode); + sawMode = true; + i++; + continue; + } + if (arg.startsWith("-")) throw new InvocationError(invalidOptionMessage(arg)); + if (!sawMode) throw new InvocationError("you must specify a buffering mode option"); + return commandEnv(args.slice(i)); + } + return commandEnv([]); +} + +export function stdbufChildEnv(modes) { + const env = { ...process.env }; + delete env._STDBUF_I; + delete env._STDBUF_O; + delete env._STDBUF_E; + const library = findStdbufLibrary(); + if (library) env.LD_PRELOAD = env.LD_PRELOAD ? `${library}:${env.LD_PRELOAD}` : library; + if (modes.i != null) env._STDBUF_I = modes.i; + if (modes.o != null) env._STDBUF_O = modes.o; + if (modes.e != null) env._STDBUF_E = modes.e; + return env; +} + +export function findStdbufLibrary() { + for (const path of ["/usr/libexec/coreutils/libstdbuf.so", "/usr/lib/coreutils/libstdbuf.so"]) { + try { + if (statSync(path).isFile()) return path; + } catch {} + } + return null; +} + +export function validateStdbufMode(stream, mode) { + if (mode == null) throw new InvocationError("missing mode"); + if (mode === "") throw new InvocationError(`invalid mode ${localeQuotedEscapedDiagnostic("")}`, 125, false); + if (mode === "L") { + if (stream === "i") throw new InvocationError("line buffering standard input is meaningless"); + return "L"; + } + if (mode === "0") return "0"; + if (mode === "l") throw new InvocationError(`invalid mode ${localeQuotedEscapedDiagnostic("l")}`, 125, false); + try { + const size = parseStdbufSize(mode); + return size.toString(); + } catch (error) { + if (error instanceof InvocationError) throw error; + throw new InvocationError(`invalid mode ${localeQuotedEscapedDiagnostic(mode)}`, 125, false); + } +} + +export function parseStdbufSize(mode) { + const text = /^\D+$/.test(mode) ? `1${mode}` : String(mode); + const match = text.match(/^(\d+)([A-Za-z]*)$/); + if (!match) throw new InvocationError(`invalid mode ${localeQuotedEscapedDiagnostic(mode)}`, 125, false); + const binary = { + "": 1n, + K: 1024n, k: 1024n, KiB: 1024n, kiB: 1024n, + M: 1024n ** 2n, m: 1024n ** 2n, MiB: 1024n ** 2n, miB: 1024n ** 2n, + G: 1024n ** 3n, g: 1024n ** 3n, GiB: 1024n ** 3n, giB: 1024n ** 3n, + T: 1024n ** 4n, t: 1024n ** 4n, TiB: 1024n ** 4n, tiB: 1024n ** 4n, + P: 1024n ** 5n, PiB: 1024n ** 5n, + E: 1024n ** 6n, EiB: 1024n ** 6n, + Z: 1024n ** 7n, ZiB: 1024n ** 7n, + Y: 1024n ** 8n, YiB: 1024n ** 8n, + }; + const decimal = { + KB: 1000n, kB: 1000n, MB: 1000n ** 2n, mB: 1000n ** 2n, + GB: 1000n ** 3n, gB: 1000n ** 3n, TB: 1000n ** 4n, tB: 1000n ** 4n, + PB: 1000n ** 5n, EB: 1000n ** 6n, ZB: 1000n ** 7n, YB: 1000n ** 8n, + }; + const scale = binary[match[2]] ?? decimal[match[2]]; + if (!scale) throw new InvocationError(`invalid mode ${localeQuotedEscapedDiagnostic(mode)}`, 125, false); + const amount = BigInt(match[1]) * scale; + if (amount > 18446744073709551615n) throw new InvocationError(`invalid mode ${localeQuotedEscapedDiagnostic(mode)}: Value too large for defined data type`, 125, false); + return amount; +} + +const singleCall = defineCommand("stdbuf", stdbufCmd, stdbufMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/stty.js b/src/commands/stty.js new file mode 100644 index 0000000..d98146f --- /dev/null +++ b/src/commands/stty.js @@ -0,0 +1,635 @@ +#!/usr/bin/env bun + +import { FFIType, linkSymbols, ptr } from "bun:ffi"; +import { open } from "node:fs/promises"; +import { libc, localeQuotedDiagnostic, localeQuotedEscapedDiagnostic, normalizeLongOptionByPrefix, systemErrorMessage } from "../shared/common.js"; +import { UsageError, fail, stdout } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const TCSANOW = 0; + +export const TCSADRAIN = 1; + +export const TIOCGWINSZ = 0x5413; + +export const TIOCSWINSZ = 0x5414; + +export const TERMIOS_SIZE = 60; + +export const TERMIOS_OFFSETS = { iflag: 0, oflag: 4, cflag: 8, lflag: 12, line: 16, cc: 17, ispeed: 52, ospeed: 56 }; + +export const STTY_CC = { intr: 0, quit: 1, erase: 2, kill: 3, eof: 4, eol: 11, eol2: 16, swtch: 7, start: 8, stop: 9, susp: 10, rprnt: 12, werase: 14, lnext: 15, discard: 13, min: 6, time: 5 }; + +export const STTY_FLAGS = { + iflag: { ignbrk: 0o1, brkint: 0o2, ignpar: 0o4, parmrk: 0o10, inpck: 0o20, istrip: 0o40, inlcr: 0o100, igncr: 0o200, icrnl: 0o400, iuclc: 0o1000, ixon: 0o2000, ixany: 0o4000, ixoff: 0o10000, imaxbel: 0o20000, iutf8: 0o40000 }, + oflag: { opost: 0o1, olcuc: 0o2, onlcr: 0o4, ocrnl: 0o10, onocr: 0o20, onlret: 0o40, ofill: 0o100, ofdel: 0o200 }, + cflag: { cs5: 0o0, cs6: 0o20, cs7: 0o40, cs8: 0o60, cstopb: 0o100, cread: 0o200, parenb: 0o400, parodd: 0o1000, hupcl: 0o2000, hup: 0o2000, clocal: 0o4000, cmspar: 0o10000000000, crtscts: 0o20000000000 }, + lflag: { isig: 0o1, icanon: 0o2, xcase: 0o4, echo: 0o10, echoe: 0o20, echok: 0o40, echonl: 0o100, noflsh: 0o200, tostop: 0o400, echoctl: 0o1000, echoprt: 0o2000, echoke: 0o4000, flusho: 0o10000, iexten: 0o100000, extproc: 0o200000 }, +}; + +export const STTY_CS_MASK = 0o60; + +export const STTY_DELAY_GROUPS = { + nl: { mask: 0x100, values: { nl0: 0x0, nl1: 0x100 } }, + cr: { mask: 0x600, values: { cr0: 0x0, cr1: 0x200, cr2: 0x400, cr3: 0x600 } }, + tab: { mask: 0x1800, values: { tab0: 0x0, tab1: 0x800, tab2: 0x1000, tab3: 0x1800 } }, + bs: { mask: 0x2000, values: { bs0: 0x0, bs1: 0x2000 } }, + vt: { mask: 0x4000, values: { vt0: 0x0, vt1: 0x4000 } }, + ff: { mask: 0x8000, values: { ff0: 0x0, ff1: 0x8000 } }, +}; + +export const STTY_OBAUD_MASK = 0x100f; + +export const STTY_IBAUD_MASK = 0x100f0000; + +export const STTY_SPEED_CODES = new Map([ + [0, 0], [50, 1], [75, 2], [110, 3], [134, 4], [150, 5], [200, 6], [300, 7], + [600, 8], [1200, 9], [1800, 10], [2400, 11], [4800, 12], [9600, 13], + [19200, 14], [38400, 15], [57600, 0x1001], [115200, 0x1002], [230400, 0x1003], + [460800, 0x1004], [500000, 0x1005], [576000, 0x1006], [921600, 0x1007], + [1000000, 0x1008], [1152000, 0x1009], [1500000, 0x100a], [2000000, 0x100b], + [2500000, 0x100c], [3000000, 0x100d], [3500000, 0x100e], [4000000, 0x100f], +]); + +export const STTY_CODE_SPEEDS = new Map([...STTY_SPEED_CODES].map(([speed, code]) => [code, speed])); + +export let sttyTermiosApi; + +export function sttyInterposedTermiosApi() { + if (sttyTermiosApi) return sttyTermiosApi; + const symbols = {}; + for (const name of ["cfsetispeed", "cfsetospeed"]) { + const address = libc.symbols.dlsym(0, ptr(Buffer.from(`${name}\0`))); + symbols[name] = { ptr: address, args: [FFIType.ptr, FFIType.u32], returns: FFIType.i32 }; + } + sttyTermiosApi = linkSymbols(symbols).symbols; + return sttyTermiosApi; +} + +export const STTY_LONG_OPTIONS = ["all", "file", "save", "help", "version"]; + +export async function sttyCmd(args) { + const { opts, operands } = parseSttyArgs(args); + validateSttyOutputStyle(opts, operands); + validateSttyOperands(operands); + const target = opts.F ?? opts.file; + let targetHandle = null; + let fd = 0; + if (target != null) { + try { + targetHandle = await open(target, "r"); + fd = targetHandle.fd; + } catch (error) { + return fail("stty", `${sttyDeviceDisplayName(target)}: ${systemErrorMessage(error)}`); + } + } + if (targetHandle == null && !sttyTargetIsTty(opts)) return fail("stty", `${sttyTargetName(opts)}: Inappropriate ioctl for device`); + const termios = sttyReadTermios(fd); + if (!termios) { + await targetHandle?.close().catch(() => {}); + return fail("stty", `${sttyTargetName(opts)}: Inappropriate ioctl for device`); + } + const windowSize = sttyReadWindowSize(fd); + if (opts.g || opts.save) { + stdout(sttySavedSettings(termios)); + await targetHandle?.close().catch(() => {}); + return 0; + } + const rows = windowSize?.rows ?? process.stdout.rows ?? 0; + const columns = windowSize?.columns ?? process.stdout.columns ?? 0; + if (!operands.length || opts.a || opts.all) { + stdout(opts.a || opts.all ? sttyAllOutput(rows, columns, termios) : sttyDefaultOutput(termios)); + await targetHandle?.close().catch(() => {}); + return 0; + } + if (operands.length === 1 && operands[0] === "speed") { + stdout(sttySpeedOutput(termios)); + await targetHandle?.close().catch(() => {}); + return 0; + } + if (operands.length === 1 && operands[0] === "size") { + stdout(`${rows} ${columns}\n`); + await targetHandle?.close().catch(() => {}); + return 0; + } + if (operands.every((operand) => operand === "drain" || operand === "-drain")) { + stdout(sttyDefaultOutput(termios)); + await targetHandle?.close().catch(() => {}); + return 0; + } + const applied = sttyApplyOperands(termios, operands, windowSize); + if (applied.windowSize && libc.symbols.ioctl(fd, TIOCSWINSZ, ptr(sttyWindowSizeBuffer(applied.windowSize))) !== 0) { + await targetHandle?.close().catch(() => {}); + return fail("stty", `${sttyTargetName(opts)}: Inappropriate ioctl for device`); + } + if (libc.symbols.tcsetattr(fd, sttyApplyTiming(operands), ptr(applied.termios.buffer)) !== 0) { + await targetHandle?.close().catch(() => {}); + return fail("stty", `${sttyTargetName(opts)}: Inappropriate ioctl for device`); + } + libc.symbols._exit(0); + return 0; +} + +export function sttyMetaOption(args) { + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (arg.startsWith("--")) { + const normalized = normalizeLongOptionByPrefix(arg, STTY_LONG_OPTIONS); + const [name, inlineValue] = normalized.slice(2).split(/=(.*)/s, 2); + if ((normalized === "--help" || normalized === "--version") && inlineValue == null) return normalized; + if (name === "file" && inlineValue == null) i++; + continue; + } + if (arg === "-F") i++; + else if (arg.startsWith("-F") && arg.length > 2) continue; + } + return null; +} + +export function parseSttyArgs(args) { + const opts = {}; + const operands = []; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") { + break; + } + if (arg.startsWith("--")) { + const normalized = normalizeLongOptionByPrefix(arg, STTY_LONG_OPTIONS); + const [name, inlineValue] = normalized.slice(2).split(/=(.*)/s, 2); + if (name === "file") { + if (inlineValue != null) opts.file = inlineValue; + else if (i + 1 < args.length) opts.file = args[++i]; + else throw new UsageError(`invalid argument ${localeQuotedDiagnostic("--file")}`, true); + } else if (name === "all" && inlineValue == null) opts.all = true; + else if (name === "save" && inlineValue == null) opts.save = true; + else if ((name === "help" || name === "version") && inlineValue == null) opts[name] = true; + else operands.push(arg); + continue; + } + if (arg === "-a") opts.a = true; + else if (arg === "-g") opts.g = true; + else if (arg === "-F") { + if (i + 1 < args.length) opts.F = args[++i]; + else throw new UsageError("option requires an argument -- 'F'", true); + } else if (arg.startsWith("-F") && arg.length > 2) opts.F = arg.slice(2); + else if (/^-[agF]+/.test(arg)) { + let consumed = false; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (ch === "a") opts.a = true; + else if (ch === "g") opts.g = true; + else if (ch === "F") { + const rest = arg.slice(j + 1); + if (rest) opts.F = rest; + else if (i + 1 < args.length) opts.F = args[++i]; + else operands.push("F"); + consumed = true; + break; + } else { + operands.push(arg); + consumed = true; + break; + } + } + if (consumed) continue; + } + else operands.push(arg); + } + return { opts, operands }; +} + +export function validateSttyOutputStyle(opts, operands) { + const styles = [opts.a || opts.all, opts.g || opts.save].filter(Boolean).length; + if (styles > 1) throw new UsageError("the options for verbose and stty-readable output styles are\nmutually exclusive"); + if (styles && operands.length) throw new UsageError("when specifying an output style, modes may not be set"); +} + +export function validateSttyOperands(operands) { + for (let i = 0; i < operands.length; i++) { + const setting = operands[i]; + if (setting === "size" || setting === "speed") continue; + if (/^[0-9a-fA-F:]+$/.test(setting) && setting.includes(":")) { + if (!sttySavedSettingsLooksValid(setting)) throw new UsageError(`invalid argument ${localeQuotedEscapedDiagnostic(setting)}`, true); + continue; + } + if (sttyBareSpeed(setting) != null || STTY_NO_ARG_SETTINGS.has(setting)) continue; + if (STTY_VALUE_SETTINGS.has(setting)) { + if (i + 1 >= operands.length) throw new UsageError(`missing argument to '${setting}'`, true); + const value = operands[++i]; + validateSttyValueSetting(setting, value); + continue; + } + throw new UsageError(`invalid argument ${localeQuotedEscapedDiagnostic(setting)}`, true); + } +} + +export function validateSttyValueSetting(setting, value) { + if (setting === "line" || setting === "min" || setting === "time") { + parseSttyInteger(value, 0xffn); + return; + } + if (setting === "ispeed" || setting === "ospeed") { + parseSttySpeed(value, setting); + return; + } + if (STTY_CONTROL_SETTINGS.has(setting)) { + if (value === "" || value === "undef" || value === "^-" || value.length === 1 || value.startsWith("^") && value.length >= 2) return; + parseSttyInteger(value, 0xffn); + } +} + +export const STTY_NO_ARG_SETTING_NAMES = [ + "parenb", "parodd", "cmspar", "cs5", "cs6", "cs7", "cs8", "hupcl", "hup", "cstopb", "cread", "clocal", "crtscts", + "ignbrk", "brkint", "ignpar", "parmrk", "inpck", "istrip", "inlcr", "igncr", "icrnl", "ixon", "ixoff", "iuclc", "ixany", "imaxbel", "iutf8", + "opost", "olcuc", "ocrnl", "onlcr", "onocr", "onlret", "ofill", "ofdel", "nl0", "nl1", "cr0", "cr1", "cr2", "cr3", "tab0", "tab1", "tab2", "tab3", "bs0", "bs1", "vt0", "vt1", "ff0", "ff1", + "isig", "icanon", "iexten", "echo", "echoe", "echok", "echonl", "noflsh", "xcase", "tostop", "echoprt", "echoctl", "echoke", "flusho", "extproc", + "raw", "cooked", "sane", "cbreak", "pass8", "litout", "nl", "ek", "dec", "evenp", "parity", "oddp", + "drain", +]; + +export const STTY_NO_ARG_SETTINGS = new Set(STTY_NO_ARG_SETTING_NAMES.flatMap((name) => [name, `-${name}`])); + +export const STTY_VALUE_SETTINGS = new Set([ + "rows", "cols", "columns", "ispeed", "ospeed", "min", "time", "line", + "intr", "quit", "erase", "kill", "eof", "eol", "eol2", "swtch", "start", "stop", "susp", "rprnt", "werase", "lnext", "discard", +]); + +export const STTY_CONTROL_SETTINGS = new Set([ + "intr", "quit", "erase", "kill", "eof", "eol", "eol2", "swtch", "start", "stop", "susp", "rprnt", "werase", "lnext", "discard", +]); + +export function sttySavedSettingsLooksValid(value) { + const parts = value.split(":"); + return parts.length === 36 + && parts.every((part) => /^[0-9a-fA-F]+$/.test(part) && BigInt(`0x${part}`) <= 0xffffffffn); +} + +export function sttyTargetIsTty(opts) { + if (opts.F || opts.file) return false; + return Boolean(process.stdin.isTTY); +} + +export function sttyTargetName(opts) { + return sttyDeviceDisplayName(opts.F ?? opts.file ?? "'standard input'"); +} + +export function sttyDeviceDisplayName(target) { + return target === "" ? "''" : target; +} + +export function sttyReadTermios(fd = 0) { + const buffer = Buffer.alloc(TERMIOS_SIZE); + if (libc.symbols.tcgetattr(fd, ptr(buffer)) !== 0) return null; + return { buffer, view: new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength) }; +} + +export function sttyReadWindowSize(fd = 0) { + const buffer = Buffer.alloc(8); + if (libc.symbols.ioctl(fd, TIOCGWINSZ, ptr(buffer)) !== 0) return null; + return { rows: buffer.readUInt16LE(0), columns: buffer.readUInt16LE(2) }; +} + +export function sttyWindowSizeBuffer(size) { + const buffer = Buffer.alloc(8); + buffer.writeUInt16LE(size.rows ?? 0, 0); + buffer.writeUInt16LE(size.columns ?? 0, 2); + return buffer; +} + +export function sttySavedSettings(termios) { + const fields = [ + sttyTermiosFlag(termios, "iflag").toString(16), + sttyTermiosFlag(termios, "oflag").toString(16), + sttyTermiosFlag(termios, "cflag").toString(16), + sttyTermiosFlag(termios, "lflag").toString(16), + ]; + for (let i = 0; i < 32; i++) fields.push(termios.view.getUint8(TERMIOS_OFFSETS.cc + i).toString(16)); + return `${fields.join(":")}\n`; +} + +export function sttyDefaultOutput(termios) { + const toggles = []; + if (!sttyHasFlag(termios, "iflag", "brkint")) toggles.push("-brkint"); + if (!sttyHasFlag(termios, "iflag", "imaxbel")) toggles.push("-imaxbel"); + if (!sttyHasFlag(termios, "lflag", "echo")) toggles.push("-echo"); + if (!sttyHasFlag(termios, "lflag", "icanon")) toggles.push("-icanon"); + if (!sttyHasFlag(termios, "lflag", "isig")) toggles.push("-isig"); + if (!sttyHasFlag(termios, "oflag", "opost")) toggles.push("-opost"); + return `${sttyVerboseSpeedPrefix(termios)} line = ${termios.view.getUint8(TERMIOS_OFFSETS.line)};\n${toggles.join(" ")}\n`; +} + +export function sttyAllOutput(rows, columns, termios) { + return `${sttyVerboseSpeedPrefix(termios)} rows ${rows}; columns ${columns}; line = ${termios.view.getUint8(TERMIOS_OFFSETS.line)}; +intr = ${sttyControlDisplay(termios, "intr")}; quit = ${sttyControlDisplay(termios, "quit")}; erase = ${sttyControlDisplay(termios, "erase")}; kill = ${sttyControlDisplay(termios, "kill")}; eof = ${sttyControlDisplay(termios, "eof")}; eol = ${sttyControlDisplay(termios, "eol")}; +eol2 = ${sttyControlDisplay(termios, "eol2")}; swtch = ${sttyControlDisplay(termios, "swtch")}; start = ${sttyControlDisplay(termios, "start")}; stop = ${sttyControlDisplay(termios, "stop")}; susp = ${sttyControlDisplay(termios, "susp")}; rprnt = ${sttyControlDisplay(termios, "rprnt")}; +werase = ${sttyControlDisplay(termios, "werase")}; lnext = ${sttyControlDisplay(termios, "lnext")}; discard = ${sttyControlDisplay(termios, "discard")}; min = ${termios.view.getUint8(TERMIOS_OFFSETS.cc + STTY_CC.min)}; time = ${termios.view.getUint8(TERMIOS_OFFSETS.cc + STTY_CC.time)}; +${sttyFlagList(termios, "cflag", ["parenb", "parodd", "cmspar", "cs8", "hupcl", "cstopb", "cread", "clocal", "crtscts"])} +${sttyFlagList(termios, "iflag", ["ignbrk", "brkint", "ignpar", "parmrk", "inpck", "istrip", "inlcr", "igncr", "icrnl", "ixon", "ixoff"])} +${sttyFlagList(termios, "iflag", ["iuclc", "ixany", "imaxbel", "iutf8"])} +${sttyFlagList(termios, "oflag", ["opost", "olcuc", "ocrnl", "onlcr", "onocr", "onlret", "ofill", "ofdel"])} ${sttyDelayModeList(termios)} +${sttyFlagList(termios, "lflag", ["isig", "icanon", "iexten", "echo", "echoe", "echok", "echonl", "noflsh", "xcase", "tostop", "echoprt"])} +${sttyFlagList(termios, "lflag", ["echoctl", "echoke", "flusho", "extproc"])} +`; +} + +export function sttyTermiosFlag(termios, group) { + return termios.view.getUint32(TERMIOS_OFFSETS[group], true); +} + +export function sttySetTermiosFlag(termios, group, value) { + termios.view.setUint32(TERMIOS_OFFSETS[group], value >>> 0, true); +} + +export function sttyHasFlag(termios, group, name) { + const flag = STTY_FLAGS[group]?.[name]; + if (flag == null) return false; + if (group === "cflag" && /^cs[5-8]$/.test(name)) return (sttyTermiosFlag(termios, group) & STTY_CS_MASK) === flag; + return (sttyTermiosFlag(termios, group) & flag) !== 0; +} + +export function sttySetFlag(termios, group, name, enabled) { + const flag = STTY_FLAGS[group]?.[name]; + if (flag == null) return false; + let value = sttyTermiosFlag(termios, group); + if (group === "cflag" && /^cs[5-8]$/.test(name)) value = (value & ~STTY_CS_MASK) | flag; + else value = enabled ? value | flag : value & ~flag; + sttySetTermiosFlag(termios, group, value); + return true; +} + +export function sttyFlagList(termios, group, names) { + return names.map((name) => sttyHasFlag(termios, group, name) ? name : `-${name}`).join(" "); +} + +export function sttyDelayModeList(termios) { + const oflag = sttyTermiosFlag(termios, "oflag"); + return Object.values(STTY_DELAY_GROUPS).map((group) => { + const active = oflag & group.mask; + return Object.entries(group.values).find(([, value]) => value === active)?.[0] ?? Object.keys(group.values)[0]; + }).join(" "); +} + +export function sttyControlDisplay(termios, setting) { + const value = termios.view.getUint8(TERMIOS_OFFSETS.cc + STTY_CC[setting]); + if (value === 0) return "<undef>"; + if (value === 0x7f) return "^?"; + if (value < 0x20) return `^${String.fromCharCode(value + 0x40)}`; + if (value === 0x20) return " "; + return String.fromCharCode(value); +} + +export function sttyInputSpeed(termios) { + return termios.view.getUint32(TERMIOS_OFFSETS.ispeed, true); +} + +export function sttyOutputSpeed(termios) { + return termios.view.getUint32(TERMIOS_OFFSETS.ospeed, true); +} + +export function sttySpeedOutput(termios) { + const input = sttyInputSpeed(termios); + const output = sttyOutputSpeed(termios); + return input === output ? `${output}\n` : `${input} ${output}\n`; +} + +export function sttyVerboseSpeedPrefix(termios) { + const input = sttyInputSpeed(termios); + const output = sttyOutputSpeed(termios); + return input === output ? `speed ${output} baud;` : `ispeed ${input} baud; ospeed ${output} baud;`; +} + +export function sttySetSpeed(termios, speed, direction = "both", display = String(speed)) { + const api = sttyInterposedTermiosApi(); + if ((direction === "both" || direction === "input") && api.cfsetispeed(ptr(termios.buffer), speed) !== 0) { + throw new UsageError(`unsupported ispeed ${localeQuotedEscapedDiagnostic(display)}`); + } + if ((direction === "both" || direction === "output") && api.cfsetospeed(ptr(termios.buffer), speed) !== 0) { + throw new UsageError(`unsupported ospeed ${localeQuotedEscapedDiagnostic(display)}`); + } +} + +export function sttySyncSpeedFieldsFromCflag(termios) { + const cflag = sttyTermiosFlag(termios, "cflag"); + const outputCode = cflag & STTY_OBAUD_MASK; + const inputCode = (cflag & STTY_IBAUD_MASK) >>> 16; + const output = STTY_CODE_SPEEDS.get(outputCode); + const input = STTY_CODE_SPEEDS.get(inputCode); + if (output != null) termios.view.setUint32(TERMIOS_OFFSETS.ospeed, output, true); + if (input != null) termios.view.setUint32(TERMIOS_OFFSETS.ispeed, input, true); +} + +export function sttyApplyOperands(termios, operands, windowSize = null) { + const next = { buffer: Buffer.from(termios.buffer), view: null }; + next.view = new DataView(next.buffer.buffer, next.buffer.byteOffset, next.buffer.byteLength); + const nextWindowSize = windowSize == null ? null : { ...windowSize }; + if (operands.length === 1 && sttySavedSettingsLooksValid(operands[0])) { + sttyApplySavedSettings(next, operands[0]); + return { termios: next, windowSize: null }; + } + for (let i = 0; i < operands.length; i++) { + const setting = operands[i]; + if (STTY_VALUE_SETTINGS.has(setting)) { + sttyApplyValueSetting(next, nextWindowSize, setting, operands[++i]); + continue; + } + const bareSpeed = sttyBareSpeed(setting); + if (bareSpeed != null) { + sttySetSpeed(next, bareSpeed, "both", setting); + continue; + } + sttyApplyModeSetting(next, setting); + } + return { termios: next, windowSize: nextWindowSize }; +} + +export function sttyApplySavedSettings(termios, saved) { + const parts = saved.split(":").map((part) => Number.parseInt(part, 16)); + if (parts.length < 4 || parts.some((part) => !Number.isFinite(part))) return; + sttySetTermiosFlag(termios, "iflag", parts[0]); + sttySetTermiosFlag(termios, "oflag", parts[1]); + sttySetTermiosFlag(termios, "cflag", parts[2]); + sttySetTermiosFlag(termios, "lflag", parts[3]); + for (let i = 0; i < Math.min(32, parts.length - 4); i++) termios.view.setUint8(TERMIOS_OFFSETS.cc + i, parts[i + 4] & 0xff); + sttySyncSpeedFieldsFromCflag(termios); +} + +export function sttyApplyModeSetting(termios, setting) { + const enabled = !setting.startsWith("-"); + const name = enabled ? setting : setting.slice(1); + if (name === "drain") return; + if (name === "raw") return sttyApplyRaw(termios, enabled); + if (name === "sane") return sttyApplySane(termios); + if (name === "cooked") return sttyApplyCooked(termios); + if (name === "cbreak") return sttyApplyCbreak(termios, enabled); + if (name === "pass8") return sttyApplyPass8(termios, enabled); + if (name === "litout") return sttyApplyLitout(termios, enabled); + if (name === "nl") return sttyApplyNl(termios, enabled); + if (sttyApplyDelayMode(termios, name)) return; + if (["evenp", "parity"].includes(name)) { + sttySetFlag(termios, "cflag", "parenb", enabled); + sttySetFlag(termios, "cflag", "parodd", false); + return; + } + if (name === "oddp") { + sttySetFlag(termios, "cflag", "parenb", enabled); + sttySetFlag(termios, "cflag", "parodd", enabled); + return; + } + for (const group of ["iflag", "oflag", "cflag", "lflag"]) { + if (sttySetFlag(termios, group, name, enabled)) return; + } +} + +export function sttyApplyTiming(operands) { + let timing = TCSANOW; + for (const operand of operands) { + if (operand === "drain") timing = TCSADRAIN; + else if (operand === "-drain") timing = TCSANOW; + } + return timing; +} + +export function sttyApplyDelayMode(termios, name) { + for (const group of Object.values(STTY_DELAY_GROUPS)) { + if (!(name in group.values)) continue; + const oflag = sttyTermiosFlag(termios, "oflag"); + sttySetTermiosFlag(termios, "oflag", (oflag & ~group.mask) | group.values[name]); + return true; + } + return false; +} + +export function sttyApplyRaw(termios, enabled) { + if (enabled) { + for (const name of ["ignbrk", "brkint", "parmrk", "istrip", "inlcr", "igncr", "icrnl", "ixon"]) sttySetFlag(termios, "iflag", name, false); + sttySetFlag(termios, "oflag", "opost", false); + for (const name of ["echo", "echonl", "icanon", "isig"]) sttySetFlag(termios, "lflag", name, false); + sttySetFlag(termios, "cflag", "cs8", true); + termios.view.setUint8(TERMIOS_OFFSETS.cc + STTY_CC.min, 1); + termios.view.setUint8(TERMIOS_OFFSETS.cc + STTY_CC.time, 0); + return; + } + for (const name of ["brkint", "icrnl", "ixon"]) sttySetFlag(termios, "iflag", name, true); + sttySetFlag(termios, "oflag", "opost", true); + for (const name of ["isig", "icanon", "iexten"]) sttySetFlag(termios, "lflag", name, true); +} + +export function sttyApplyCooked(termios) { + for (const name of ["brkint", "ignpar", "istrip", "icrnl", "ixon"]) sttySetFlag(termios, "iflag", name, true); + sttySetFlag(termios, "oflag", "opost", true); + for (const name of ["isig", "icanon"]) sttySetFlag(termios, "lflag", name, true); +} + +export function sttyApplyCbreak(termios, enabled) { + if (enabled) { + sttySetFlag(termios, "lflag", "icanon", false); + termios.view.setUint8(TERMIOS_OFFSETS.cc + STTY_CC.min, 1); + termios.view.setUint8(TERMIOS_OFFSETS.cc + STTY_CC.time, 0); + } else { + sttySetFlag(termios, "lflag", "icanon", true); + } +} + +export function sttyApplyPass8(termios, enabled) { + sttySetFlag(termios, "iflag", "istrip", !enabled); +} + +export function sttyApplyLitout(termios, enabled) { + sttyApplyPass8(termios, enabled); + sttySetFlag(termios, "oflag", "opost", !enabled); +} + +export function sttyApplyNl(termios, enabled) { + sttySetFlag(termios, "iflag", "icrnl", !enabled); + sttySetFlag(termios, "oflag", "onlcr", !enabled); +} + +export function sttyApplySane(termios) { + for (const name of ["ignbrk", "parmrk", "inpck", "inlcr", "igncr", "ixoff", "iuclc", "ixany", "iutf8"]) sttySetFlag(termios, "iflag", name, false); + for (const name of ["brkint", "ignpar", "istrip", "icrnl", "ixon", "imaxbel"]) sttySetFlag(termios, "iflag", name, true); + for (const name of ["olcuc", "ocrnl", "onocr", "onlret", "ofill", "ofdel"]) sttySetFlag(termios, "oflag", name, false); + for (const name of ["opost", "onlcr"]) sttySetFlag(termios, "oflag", name, true); + for (const name of ["parenb", "cstopb", "hupcl", "clocal", "crtscts"]) sttySetFlag(termios, "cflag", name, false); + sttySetFlag(termios, "cflag", "cs8", true); + sttySetFlag(termios, "cflag", "cread", true); + for (const name of ["echonl", "noflsh", "xcase", "tostop", "echoprt", "flusho", "extproc"]) sttySetFlag(termios, "lflag", name, false); + for (const name of ["isig", "icanon", "iexten", "echo", "echoe", "echok", "echoctl", "echoke"]) sttySetFlag(termios, "lflag", name, true); + termios.view.setUint8(TERMIOS_OFFSETS.cc + STTY_CC.min, 1); + termios.view.setUint8(TERMIOS_OFFSETS.cc + STTY_CC.time, 0); +} + +export function sttyApplyValueSetting(termios, windowSize, setting, value) { + if (setting === "ispeed" || setting === "ospeed") { + sttySetSpeed(termios, parseSttySpeed(value, setting), setting === "ispeed" ? "input" : "output", value); + return; + } + if ((setting === "rows" || setting === "cols" || setting === "columns") && windowSize) { + if (setting === "rows") windowSize.rows = parseSttyInteger(value, 0xffffn); + else windowSize.columns = parseSttyInteger(value, 0xffffn); + return; + } + if (setting === "line") { + termios.view.setUint8(TERMIOS_OFFSETS.line, parseSttyInteger(value, 0xffn)); + return; + } + if (setting === "min" || setting === "time") { + termios.view.setUint8(TERMIOS_OFFSETS.cc + STTY_CC[setting], parseSttyInteger(value, 0xffn)); + } else if (STTY_CONTROL_SETTINGS.has(setting)) { + termios.view.setUint8(TERMIOS_OFFSETS.cc + STTY_CC[setting], sttyControlValue(value)); + } +} + +export function parseSttySpeed(value, setting) { + const text = String(value); + if (text === "exta") return 19200; + if (text === "extb") return 38400; + const match = text.match(/^\s*\+?(\d+)(?:\.(\d*))?$/); + if (!match) { + throw new UsageError(`invalid ${setting} ${localeQuotedEscapedDiagnostic(value)}`, true); + } + let speed = BigInt(match[1]); + const fraction = match[2]; + if (fraction) { + const first = Number(fraction[0]); + if (first > 5 || first === 5 && (/[1-9]/.test(fraction.slice(1)) || (speed & 1n) === 1n)) speed++; + } + if (speed > 0xffffffffn) { + throw new UsageError(`invalid ${setting} ${localeQuotedEscapedDiagnostic(value)}`, true); + } + return Number(speed); +} + +export function sttyBareSpeed(value) { + try { + return parseSttySpeed(value, "speed"); + } catch { + return null; + } +} + +export function parseSttyInteger(value, maximum) { + const text = String(value); + const unsigned = text.startsWith("+") ? text.slice(1) : text; + let parsed; + if (/^0[xX][0-9a-fA-F]+$/.test(unsigned)) parsed = BigInt(unsigned); + else if (/^0[0-7]*$/.test(unsigned)) parsed = BigInt(`0o${unsigned.slice(1) || "0"}`); + else if (/^(?:0|[1-9]\d*)$/.test(unsigned)) parsed = BigInt(unsigned); + else throw new UsageError(`invalid integer argument: ${localeQuotedEscapedDiagnostic(value)}`); + if (parsed > maximum) throw new UsageError(`invalid integer argument: ${localeQuotedEscapedDiagnostic(value)}`); + return Number(parsed); +} + +export function sttyControlValue(value) { + if (value === "") return 0; + if (value === "undef" || value === "^-") return 0; + if (value.length === 1) return value.charCodeAt(0) & 0xff; + if (value.startsWith("^") && value.length >= 2) return value[1] === "?" ? 0x7f : value.charCodeAt(1) & 0x1f; + return parseSttyInteger(value, 0xffn); +} + +const singleCall = defineCommand("stty", sttyCmd, sttyMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); 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); diff --git a/src/commands/sync.js b/src/commands/sync.js new file mode 100644 index 0000000..d0f876f --- /dev/null +++ b/src/commands/sync.js @@ -0,0 +1,118 @@ +#!/usr/bin/env bun + +import { lstat, open } from "node:fs/promises"; +import { libc, pathDisplayName, shellEscapeLsName, systemErrorMessage } from "../shared/common.js"; +import { UsageError, fail, stderr } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const SYNC_LONG_OPTIONS = ["data", "file-system", "help", "version"]; + +export function syncMetaOption(args) { + for (const arg of args) { + if (arg === "--") return null; + if (!arg.startsWith("--")) continue; + const option = normalizeSyncLongOption(arg); + const name = option.slice(2).split("=", 1)[0]; + if (!SYNC_LONG_OPTIONS.includes(name) || option.includes("=")) return null; + if (option === "--help" || option === "--version") return option; + } + return null; +} + +export async function syncCmd(args) { + const { opts, operands } = parseSyncArgs(args); + const data = opts.d || opts.data; + const fileSystem = opts.f || opts["file-system"]; + if (data && fileSystem) return fail("sync", "cannot specify both --data and --file-system", 1); + if (data && !operands.length) return fail("sync", "--data needs at least one argument", 1); + if (operands.length) { + let status = 0; + for (const file of operands) { + const ok = await syncOneFile(file, data, fileSystem); + if (!ok) status = 1; + } + return status; + } + libc.symbols.sync(); + return 0; +} + +export function parseSyncArgs(args) { + const opts = {}; + const operands = []; + 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 normalized = normalizeSyncLongOption(arg); + const [name, inlineValue] = normalized.slice(2).split(/=(.*)/s, 2); + if (!SYNC_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); + opts[name] = true; + continue; + } + for (const ch of arg.slice(1)) { + if (ch === "d" || ch === "f") opts[ch] = true; + else throw new UsageError(`invalid option -- '${ch}'`, true); + } + } + return { opts, operands }; +} + +export function normalizeSyncLongOption(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 = SYNC_LONG_OPTIONS.find((option) => option.startsWith(name)); + if (!match) return arg; + return eq === -1 ? `--${match}` : `--${match}=${body.slice(eq + 1)}`; +} + +export async function syncOneFile(file, data = false, fileSystem = false) { + const name = shellEscapeLsName(pathDisplayName(file), true); + const s = await lstat(file).catch((error) => error); + if (s instanceof Error) { + stderr(`sync: error opening ${name}: ${systemErrorMessage(s)}\n`); + return false; + } + if (s.isFIFO()) return true; + let handle; + try { + handle = await open(file, "r"); + } catch (readError) { + if (s.isSymbolicLink() && readError?.code === "ENOENT") { + stderr(`sync: error opening ${name}: ${systemErrorMessage(readError)}\n`); + return false; + } + try { + handle = await open(file, "a"); + } catch { + stderr(`sync: error opening ${name}: ${systemErrorMessage(readError)}\n`); + return false; + } + } + try { + if (data && !fileSystem) await handle.datasync(); + else await handle.sync(); + return true; + } catch (error) { + stderr(`sync: error syncing ${name}: ${systemErrorMessage(error)}\n`); + return false; + } finally { + await handle.close(); + } +} + +const singleCall = defineCommand("sync", syncCmd, syncMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/tac.js b/src/commands/tac.js new file mode 100644 index 0000000..6ce71b8 --- /dev/null +++ b/src/commands/tac.js @@ -0,0 +1,272 @@ +#!/usr/bin/env bun + +import { fstatSync, readSync } from "node:fs"; +import { concatBytes, enc, nodeErrorMessage, parseOptions, pathDisplayName, readAll, shellEscapeLsName, systemErrorMessage, textInputDiagnosticName } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { decodeValidUtf8, fdIsSeekable, isSingleByteLocale, spoolInputToTemporaryFile, tacRegexPattern } from "../shared/text.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const TAC_LONG_OPTIONS = ["before", "regex", "separator", "help", "version"]; + +export function tacMetaOption(args) { + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (arg.startsWith("--")) { + const option = normalizeTacLongOption(arg); + const name = option.slice(2).split("=", 1)[0]; + if (!TAC_LONG_OPTIONS.includes(name)) return null; + if (option.includes("=")) return null; + if (option === "--help" || option === "--version") return option; + if (name === "separator") i++; + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (ch === "b" || ch === "r") continue; + if (ch !== "s") return null; + if (!arg.slice(j + 1)) i++; + break; + } + } + return null; +} + +export function normalizeTacLongOptions(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(normalizeTacLongOption(arg)); + } + return out; +} + +export function normalizeTacLongOption(arg) { + const body = arg.slice(2); + const eq = body.indexOf("="); + const name = eq === -1 ? body : body.slice(0, eq); + if (!name) return arg; + const matches = TAC_LONG_OPTIONS.filter((option) => option.startsWith(name)); + if (matches.length !== 1) return arg; + return eq === -1 ? `--${matches[0]}` : `--${matches[0]}=${body.slice(eq + 1)}`; +} + +export async function tac(args) { + const { opts, operands } = parseOptions(normalizeTacLongOptions(args), { short: { b: false, r: false, s: "value" }, long: { before: false, regex: false, separator: "value", help: false, version: false } }); + const sep = opts.s ?? opts.separator ?? "\n"; + const before = opts.b || opts.before; + const regex = opts.r || opts.regex; + if (regex && sep === "") throw new UsageError("separator cannot be empty"); + const files = operands.length ? operands : ["-"]; + const out = []; + let failed = false; + const cacheStdin = files.filter((file) => file === "-").length > 1 && stdinIsSeekable(); + let stdinBytes = null; + for (const file of files) { + let bytes; + try { + if (file === "-") { + if (cacheStdin) { + stdinBytes ??= await tacReadStdin(); + bytes = stdinBytes; + } else { + bytes = await tacReadStdin(); + } + } else { + bytes = await readAll(file); + } + } catch (error) { + const message = error?.code === "EISDIR" || (file === "-" && error?.code === "EBADF") + ? `${tacInputDiagnosticName(file)}: read error: ${systemErrorMessage(error)}` + : file === "-" + ? nodeErrorMessage(error) + : `failed to open ${shellEscapeLsName(pathDisplayName(file), true)} for reading: ${systemErrorMessage(error)}`; + stderr(`tac: ${message}\n`); + failed = true; + continue; + } + if (regex) { + const decoded = isSingleByteLocale() ? null : decodeValidUtf8(bytes); + const text = decoded ?? Buffer.from(bytes).toString("latin1"); + const result = tacRegexRecords(text, sep, before).join(""); + out.push(decoded == null ? Buffer.from(result, "latin1") : enc.encode(result)); + } else { + out.push(tacBytes(bytes, sep, before)); + } + } + stdout(concatBytes(out)); + return failed ? 1 : 0; +} + +export async function tacReadStdin() { + if (process.env.BNU_STDIN_CLOSED === "1") { + const error = new Error("Bad file descriptor"); + error.code = "EBADF"; + throw error; + } + fstatSync(0); + if (stdinIsSeekable()) return readAll("-"); + const chunks = []; + const buffer = Buffer.allocUnsafe(64 * 1024); + const inMemoryLimit = 1024 * 1024; + let total = 0; + while (total <= inMemoryLimit) { + const n = readSync(0, buffer, 0, buffer.length, null); + if (n === 0) return concatBytes(chunks); + chunks.push(Buffer.from(buffer.subarray(0, n))); + total += n; + } + const spool = await spoolInputToTemporaryFile("-", "bnu-tac", chunks); + try { + return await readAll(spool.path); + } finally { + await spool.cleanup(); + } +} + +export function tacInputDiagnosticName(file) { + return file === "-" ? "'standard input'" : textInputDiagnosticName(file); +} + +export function tacBytes(bytes, sep, before = false) { + return concatBytes(tacByteRecords(bytes, tacSeparatorBytes(sep), before).reverse()); +} + +export function tacByteRecords(bytes, sep, before = false) { + if (bytes.byteLength === 0) return []; + const delimiter = sep.byteLength === 0 ? Uint8Array.of(0) : sep; + const matches = byteSeparatorMatches(bytes, delimiter); + const records = []; + let start = 0; + for (const match of matches) { + if (before) { + records.push(bytes.slice(start, match.start)); + start = match.start; + } else { + records.push(bytes.slice(start, match.end)); + start = match.end; + } + } + if (start < bytes.length) records.push(bytes.slice(start)); + return records.filter((record) => record.byteLength !== 0); +} + +export function tacSeparatorBytes(sep) { + const text = String(sep); + if (text === "\uFFFD") return Uint8Array.of(0xe9); + if (text === "\uFFFD\uFFFD") return Uint8Array.of(0xe9, 0xea); + return enc.encode(text); +} + +export function byteSeparatorMatches(bytes, sep) { + const matches = []; + for (let i = 0; i <= bytes.byteLength - sep.byteLength;) { + let found = true; + for (let j = 0; j < sep.byteLength; j++) { + if (bytes[i + j] !== sep[j]) { + found = false; + break; + } + } + if (found) { + matches.push({ start: i, end: i + sep.byteLength }); + i += sep.byteLength; + } else { + i++; + } + } + return matches; +} + +export function tacRecords(text, sep, before = false, regex = false) { + if (text === "") return []; + const matches = tacSeparatorMatches(text, sep === "" && !regex ? "\0" : sep, regex); + const records = []; + let start = 0; + for (const match of matches) { + if (before) { + records.push(text.slice(start, match.start)); + start = match.start; + } else { + records.push(text.slice(start, match.end)); + start = match.end; + } + } + if (start < text.length) records.push(text.slice(start)); + return records.filter((record) => record !== ""); +} + +export function tacSeparatorMatches(text, sep, regex = false) { + if (!regex) { + const matches = []; + let start = 0; + let index; + while ((index = text.indexOf(sep, start)) !== -1) { + matches.push({ start: index, end: index + sep.length }); + start = index + sep.length; + } + return matches; + } + let pattern; + try { + pattern = new RegExp(tacRegexPattern(sep), "gm"); + } catch { + throw new UsageError("Invalid regular expression"); + } + return [...text.matchAll(pattern)].map((match) => ({ start: match.index, end: match.index + match[0].length })); +} + +export function tacRegexRecords(text, sep, before = false) { + if (text === "") return []; + const source = tacRegexPattern(sep); + let pattern; + let endPattern; + try { + pattern = new RegExp(source, "gm"); + endPattern = new RegExp(`(?:${source})(?![\\s\\S])`, "m"); + } catch { + throw new UsageError("Invalid regular expression"); + } + const ending = tacRegexMatchEndingAt(endPattern, text, text.length); + if (!ending || ending.start === ending.end) return tacRecords(text, sep, before, true).reverse(); + const fallbackRecords = () => tacRecords(text, sep, before, true).reverse(); + const records = []; + let cursor = text.length; + while (cursor > 0) { + const match = tacRegexMatchEndingAt(endPattern, text, cursor); + if (!match || match.start === match.end) { + records.push(text.slice(0, cursor)); + break; + } + if (match.start > 0 && !tacRegexMatchEndingAt(endPattern, text, match.start)) { + if (records.length === 0) return fallbackRecords(); + records.push(text.slice(0, cursor)); + break; + } + records.push(text.slice(match.start, cursor)); + cursor = match.start; + } + return records.filter((record) => record !== ""); +} + +export function tacRegexMatchEndingAt(pattern, text, end) { + pattern.lastIndex = 0; + const slice = text.slice(0, end); + const match = pattern.exec(slice); + return match ? { start: match.index, end: match.index + match[0].length } : null; +} + +export function stdinIsSeekable() { + return fdIsSeekable(0); +} + +const singleCall = defineCommand("tac", tac, tacMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/tail.js b/src/commands/tail.js new file mode 100644 index 0000000..6934d4d --- /dev/null +++ b/src/commands/tail.js @@ -0,0 +1,597 @@ +#!/usr/bin/env bun + +import { closeSync, existsSync, constants as fsConstants, fstatSync, openSync, readSync } from "node:fs"; +import { lstat, open, stat, writeFile } from "node:fs/promises"; +import { dirname as pathDirname } from "node:path"; +import { SEEK_SET, cstrPath, headTailDiagnosticName, isWriteError, libc, localeQuotedDiagnostic, localeQuotedEscapedDiagnostic, nodeErrorMessage, normalizeLongOptionByPrefix, normalizeLongOptionsByPrefix, parseOptions, pathLikeJoin, readAll, readFdChunkViews, readdirPathEntries, systemErrorMessage } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { headTailHeaderMode, headTailShouldPrintHeader, isTailStreamingDevice, normalizeHeadTailArgs, parseByteCount, splitDelimitedByteRecords, tailFirstBytes } from "../shared/head-tail.js"; +import { POLLERR, POLLHUP, POLLNVAL, POLLOUT, pollFd, readStdinChunks } from "../shared/text.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const SEEK_END = 2; + +export const POLLIN = 0x001; + +export const IN_MODIFY = 0x00000002; + +export const IN_ATTRIB = 0x00000004; + +export const IN_CLOSE_WRITE = 0x00000008; + +export const IN_MOVED_FROM = 0x00000040; + +export const IN_MOVED_TO = 0x00000080; + +export const IN_CREATE = 0x00000100; + +export const IN_DELETE = 0x00000200; + +export const IN_DELETE_SELF = 0x00000400; + +export const IN_MOVE_SELF = 0x00000800; + +export const TAIL_LONG_OPTIONS = ["bytes", "debug", "follow", "lines", "max-unchanged-stats", "pid", "quiet", "retry", "silent", "sleep-interval", "verbose", "zero-terminated", "help", "version"]; + +export function tailMetaOption(args) { + const longValueOptions = new Set(["bytes", "lines", "sleep-interval", "max-unchanged-stats", "pid"]); + const longOptionalValueOptions = new Set(["follow"]); + const shortValueOptions = new Set(["c", "n", "s"]); + const shortKnownOptions = new Set(["c", "n", "q", "v", "z", "f", "F", "s"]); + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (arg.startsWith("--")) { + const option = normalizeTailLongOption(arg); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (!TAIL_LONG_OPTIONS.includes(name) && name !== "-disable-inotify") return null; + if (name === "follow" && inlineValue != null && !["descriptor", "name"].includes(inlineValue)) return null; + if ((option === "--help" || option === "--version") && inlineValue == null) return option; + if (longValueOptions.has(name)) { + const value = inlineValue ?? args[i + 1]; + if (value !== undefined) { + if (name === "bytes") parseTailCount(value, 10, true); + if (name === "lines") parseTailCount(value, 10, false); + if (name === "sleep-interval") parseTailSleepInterval(value); + if (name === "pid" && !/^\d*$/.test(value)) throw new UsageError(`invalid PID: ${localeQuotedEscapedDiagnostic(value)}`); + } + if (inlineValue == null) i++; + } + else if (inlineValue == null && longOptionalValueOptions.has(name)) continue; + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + if (/^-\d/.test(arg) || /^\+\d/.test(arg)) continue; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (!shortKnownOptions.has(ch)) return null; + if (shortValueOptions.has(ch)) { + const inlineValue = arg.slice(j + 1); + const value = inlineValue === "" ? args[i + 1] : inlineValue; + if (value !== undefined) { + if (ch === "s") parseTailSleepInterval(value); + else parseTailCount(value, 10, ch === "c"); + } + if (inlineValue === "") i++; + break; + } + } + } + return null; +} + +export async function tail(args) { + args = normalizeHeadTailArgs(normalizeTailLongOptions(args), "tail"); + const pidList = tailPidList(args); + const headerMode = headTailHeaderMode(args); + const { opts, operands } = parseOptions(args, { short: { n: "value", c: "value", q: false, v: false, z: false, f: false, F: false, s: "value" }, long: { lines: "value", bytes: "value", quiet: false, silent: false, verbose: false, "zero-terminated": false, follow: "optional-value", sleep: "value", "sleep-interval": "value", "max-unchanged-stats": "value", pid: "value", retry: false, debug: false, "-disable-inotify": false, help: false, version: false } }); + const files = operands.length ? operands : ["-"]; + const bytesMode = opts.c ?? opts.bytes; + const count = parseTailCount(bytesMode ?? opts.n ?? opts.lines, 10, bytesMode != null); + const rawCount = String(bytesMode ?? opts.n ?? opts.lines ?? ""); + const delimiter = opts.z || opts["zero-terminated"] ? "\0" : "\n"; + const follow = opts.f || opts.F || opts.follow != null; + const followMode = opts.F ? "name" : opts.follow === true ? "descriptor" : opts.follow ?? (opts.f ? "descriptor" : null); + if (followMode != null && !["descriptor", "name"].includes(followMode)) { + const kind = followMode === "" ? "ambiguous" : "invalid"; + throw new UsageError(`${kind} argument ${localeQuotedDiagnostic(followMode)} for ${localeQuotedDiagnostic("--follow")}\nValid arguments are:\n - ${localeQuotedDiagnostic("descriptor")}\n - ${localeQuotedDiagnostic("name")}`, true); + } + const followByName = followMode === "name"; + const retryInitialOpen = opts.retry || opts.F; + const sleepInterval = parseTailSleepInterval(opts.s ?? opts.sleep ?? opts["sleep-interval"]); + parseTailMaxUnchangedStats(opts["max-unchanged-stats"]); + if (opts.retry && !follow) stderr("tail: warning: --retry ignored; --retry is useful only when following\n"); + if (follow && files.includes("-") && process.env.BNU_STDIN_CLOSED === "1") { + stderr("tail: cannot fstat 'standard input'\n"); + stderr("tail: no files remaining\n"); + return 1; + } + if (count === 0 && !follow && !rawCount.startsWith("+")) return 0; + const followOffsets = new Map(); + const followIdentities = new Map(); + const followInaccessible = new Map(); + const followFiles = []; + let followHasNonRegular = false; + let renderedAnyFile = false; + let lastOutputFile = null; + let failed = false; + for (let i = 0; i < files.length; i++) { + const file = files[i]; + const header = headTailShouldPrintHeader(headerMode, files.length) ? `${renderedAnyFile ? "\n" : ""}==> ${tailDisplayName(file)} <==\n` : ""; + const rawBytes = bytesMode == null ? "" : String(bytesMode); + if (!follow && file === "-" && bytesMode != null && rawBytes.startsWith("+")) { + if (header) stdout(header); + let skip = Math.max(0, tailStartOffset(count)); + readStdinChunks((chunk) => { + if (skip >= chunk.length) { + skip -= chunk.length; + return; + } + stdout(skip > 0 ? chunk.slice(skip) : chunk); + skip = 0; + }); + renderedAnyFile = true; + continue; + } + if (!follow && file === "-" && bytesMode == null && rawCount.startsWith("+")) { + if (header) stdout(header); + readTailFdFromRecord(0, count, delimiter); + renderedAnyFile = true; + continue; + } + if (!follow && file !== "-" && bytesMode == null && rawCount.startsWith("+") && await isTailStreamingDevice(file)) { + if (header) stdout(header); + let fd; + try { + fd = openSync(file, "r"); + readTailFdFromRecord(fd, count, delimiter); + renderedAnyFile = true; + } catch (error) { + if (isWriteError(error)) throw error; + stderr(`tail: error reading ${headTailDiagnosticName(file)}: ${systemErrorMessage(error)}\n`); + failed = true; + } finally { + if (fd != null) closeSync(fd); + } + continue; + } + if (bytesMode != null && file !== "-" && await isTailBlockDevice(file)) { + if (header) stdout(header); + try { + await tailBlockDeviceBytes(file, count, rawBytes.startsWith("+")); + } catch (error) { + stderr(`tail: error reading ${headTailDiagnosticName(file)}: ${systemErrorMessage(error)}\n`); + failed = true; + } + continue; + } + if (bytesMode != null && file !== "-" && !rawBytes.startsWith("+") && await isTailStreamingDevice(file)) { + if (header) stdout(header); + try { + await tailFirstBytes(file, count); + } catch (error) { + stderr(`tail: error reading ${headTailDiagnosticName(file)}: ${systemErrorMessage(error)}\n`); + failed = true; + } + continue; + } + if (follow && file === "-") { + try { + fstatSync(0); + } catch { + stderr("tail: cannot fstat 'standard input'\n"); + failed = true; + continue; + } + } + const fileInfo = file === "-" ? null : await stat(file).catch(() => null); + if (follow && (!fileInfo || !fileInfo.isFile())) followHasNonRegular = true; + if (follow && pidList.length && fileInfo?.isFIFO?.()) { + followOffsets.set(file, 0); + followIdentities.set(file, await tailFollowIdentity(file, followByName)); + followFiles.push(file); + renderedAnyFile = true; + continue; + } + if (retryInitialOpen && fileInfo?.isDirectory()) { + stderr(`tail: cannot follow ${headTailDiagnosticName(file)}: Is a directory\n`); + // -F retries an initially untailable name. Report its inaccessible + // state now, rather than waiting for the first polling cycle: callers + // (and GNU's follow protocol) need both diagnostics before a rapid + // directory-to-file replacement can occur. + if (followByName) stderr(`tail: ${headTailDiagnosticName(file)} has become inaccessible: Is a directory\n`); + failed = true; + followFiles.push(file); + followOffsets.set(file, null); + followIdentities.set(file, await tailFollowIdentity(file, followByName)); + if (!followByName) followInaccessible.set(file, "Is a directory"); + continue; + } + let data; + try { + data = await readAll(file); + } catch (error) { + if (error?.code === "EISDIR") { + if (header) stdout(header); + renderedAnyFile = true; + stderr(`tail: error reading ${headTailDiagnosticName(file)}: ${systemErrorMessage(error)}\n`); + } else { + stderr(file === "-" ? `tail: ${nodeErrorMessage(error)}\n` : `tail: cannot open ${headTailDiagnosticName(file)} for reading: ${systemErrorMessage(error)}\n`); + } + failed = true; + if (retryInitialOpen && file !== "-") { + followFiles.push(file); + followOffsets.set(file, null); + followIdentities.set(file, null); + } + continue; + } + followOffsets.set(file, data.length); + followIdentities.set(file, await tailFollowIdentity(file, followByName)); + if (file !== "-") followFiles.push(file); + if (header) { + stdout(header); + lastOutputFile = file; + } + renderedAnyFile = true; + if (bytesMode != null) { + stdout(rawBytes.startsWith("+") ? data.slice(Math.max(0, tailStartOffset(count))) : data.slice(Math.max(0, data.length - count))); + } else { + const raw = String(opts.n ?? opts.lines ?? ""); + const lines = splitDelimitedByteRecords(data, delimiter); + stdout(raw.startsWith("+") ? Buffer.concat(lines.slice(Math.max(0, tailStartOffset(count)))) : count === 0 ? "" : Buffer.concat(lines.slice(-count))); + } + } + if (follow && followFiles.length) { + if (opts.retry && followMode === "descriptor") stderr("tail: warning: --retry only effective for the initial open\n"); + if (opts.debug) { + // Descriptor-following a device uses its blocking read semantics; + // regular files use inotify unless explicitly disabled, while + // name-following non-regular files must poll. + const mode = opts["-disable-inotify"] + ? "polling" + : followHasNonRegular + ? followByName ? "polling" : "blocking" + : "notification"; + stderr(`tail: using ${mode} mode\n`); + } + const followStatus = await followTailFiles(followFiles, followOffsets, { + followByName, + inotifyMode: !opts["-disable-inotify"], + retryInitialOpen, + identities: followIdentities, + inaccessible: followInaccessible, + interval: sleepInterval, + headers: (opts.v || opts.verbose || files.length > 1) && !(opts.q || opts.quiet || opts.silent), + lastOutputFile, + pids: pidList, + }); + if (followStatus !== 0) return followStatus; + } else if (follow && failed) { + stderr("tail: no files remaining\n"); + } + return failed ? 1 : 0; +} + +export async function followTailFiles(files, offsets, options) { + const removedParentDirs = new Set(); + const inotify = options.inotifyMode ? tailInotifyOpen(files, options.followByName) : null; + if (!inotify) options.inotifyMode = false; + await tailTestPauseBeforeInotifyScan(); + try { + while (true) { + if (options.pids?.length && !options.pids.some(tailPidAlive)) return 0; + if (pollFd(1, POLLOUT) & (POLLERR | POLLHUP | POLLNVAL)) return 0; + const iterationFiles = inotify?.pendingFiles?.length ? inotify.pendingFiles.splice(0) : files; + for (const file of iterationFiles) { + let actualFile = file; + let identity = await tailFollowIdentity(file, options.followByName); + let s = await stat(file).catch(() => null); + if (!s && !options.followByName && options.identities?.get(file) != null) { + actualFile = await findTailMovedPath(file, options.identities.get(file)) ?? file; + if (actualFile !== file) { + identity = await tailFollowIdentity(actualFile, false); + s = await stat(actualFile).catch(() => null); + } + } + tailInotifyUpdateFile(inotify, file, s, identity); + if (!s || s.isDirectory()) { + if (offsets.get(file) === null && s?.isDirectory() && !options.followByName) { + stderr(`tail: ${headTailDiagnosticName(file)} has been replaced with an untailable file\n`); + stderr("tail: no files remaining\n"); + return 1; + } + if (options.followByName && options.identities?.get(file) != null && offsets.get(file) !== null) { + stderr(`tail: ${headTailDiagnosticName(file)} has become inaccessible: ${s?.isDirectory() ? "Is a directory" : "No such file or directory"}\n`); + if (options.inotifyMode && !s && !removedParentDirs.has(pathDirname(String(file))) && !(await lstat(pathDirname(String(file))).catch(() => null))) { + removedParentDirs.add(pathDirname(String(file))); + stderr("tail: directory containing watched file was removed\n"); + stderr("tail: inotify cannot be used, reverting to polling\n"); + } + if (!s && !options.retryInitialOpen && await findTailMovedPath(file, options.identities.get(file))) { + stderr("tail: no files remaining\n"); + return 1; + } + offsets.set(file, null); + options.identities.set(file, null); + } else if (options.followByName && offsets.get(file) === null && s?.isDirectory() && options.inaccessible?.has(file)) { + stderr(`tail: ${headTailDiagnosticName(file)} has become inaccessible: ${options.inaccessible.get(file)}\n`); + options.inaccessible.delete(file); + } + continue; + } + let offset = offsets.has(file) ? offsets.get(file) : 0; + const previousIdentity = options.identities?.get(file); + if (offset === null) { + if (options.inaccessible?.has(file)) { + stderr(`tail: ${headTailDiagnosticName(file)} has become inaccessible: ${options.inaccessible.get(file)}\n`); + options.inaccessible.delete(file); + } + stderr(`tail: ${headTailDiagnosticName(file)} has appeared; following new file\n`); + offset = 0; + } else if (options.followByName && previousIdentity != null && identity != null && previousIdentity !== identity) { + stderr(`tail: ${headTailDiagnosticName(file)} has been replaced; following new file\n`); + offset = 0; + } else if (s.size < offset) { + stderr(`tail: ${headTailDiagnosticName(file)}: file truncated\n`); + offset = 0; + } + if (s.size <= offset) { + offsets.set(file, offset); + if (identity != null) options.identities?.set(file, identity); + continue; + } + if (options.headers && options.lastOutputFile !== file) { + stdout(`${options.lastOutputFile == null ? "" : "\n"}==> ${tailDisplayName(file)} <==\n`); + options.lastOutputFile = file; + } + await writeFileRange(actualFile, offset, s.size - offset); + offsets.set(file, s.size); + if (identity != null) options.identities?.set(file, identity); + } + if (inotify) tailInotifyWait(inotify, options.interval); + else await Bun.sleep(options.interval); + } + } finally { + tailInotifyClose(inotify); + } +} + +export async function tailTestPauseBeforeInotifyScan() { + const ready = process.env.BNU_TAIL_INOTIFY_READY_FILE; + const proceed = process.env.BNU_TAIL_INOTIFY_CONTINUE_FILE; + if (!ready || !proceed) return; + await writeFile(ready, "ready\n"); + const deadline = Date.now() + 30_000; + while (!existsSync(proceed)) { + if (Date.now() >= deadline) throw new Error("timed out waiting for the inotify race test continuation marker"); + await Bun.sleep(10); + } +} + +export function tailInotifyOpen(files, followByName) { + let fd; + try { + fd = libc.symbols.inotify_init1(fsConstants.O_NONBLOCK | fsConstants.O_CLOEXEC); + } catch { + return null; + } + if (fd < 0) return null; + const state = { fd, files: new Map(), parents: new Map(), pendingFiles: [] }; + if (followByName) { + for (const file of files) { + const parent = pathDirname(String(file)); + if (state.parents.has(parent)) continue; + const wd = libc.symbols.inotify_add_watch(fd, cstrPath(parent), IN_CREATE | IN_DELETE | IN_MOVED_FROM | IN_MOVED_TO | IN_ATTRIB); + if (wd >= 0) state.parents.set(parent, wd); + } + } + return state; +} + +export function tailInotifyUpdateFile(state, file, statInfo, identity) { + if (!state) return; + const current = state.files.get(file); + const watchable = statInfo && (statInfo.isFile() || statInfo.isFIFO()); + if (current && (!watchable || current.identity !== identity)) { + libc.symbols.inotify_rm_watch(state.fd, current.wd); + state.files.delete(file); + } + if (!watchable || state.files.has(file)) return; + const wd = libc.symbols.inotify_add_watch(state.fd, cstrPath(file), IN_MODIFY | IN_ATTRIB | IN_CLOSE_WRITE | IN_DELETE_SELF | IN_MOVE_SELF); + if (wd >= 0) state.files.set(file, { wd, identity }); +} + +export function tailInotifyWait(state, timeout) { + if (!(pollFd(state.fd, POLLIN, timeout) & POLLIN)) return; + const events = Buffer.allocUnsafe(64 * 1024); + try { + const bytesRead = readSync(state.fd, events, 0, events.length, null); + for (let offset = 0; offset + 16 <= bytesRead;) { + const wd = events.readInt32LE(offset); + const nameLength = events.readUInt32LE(offset + 12); + for (const [file, watch] of state.files) { + if (watch.wd === wd && state.pendingFiles.at(-1) !== file) { + state.pendingFiles.push(file); + break; + } + } + offset += 16 + nameLength; + } + } catch (error) { + if (error?.code !== "EAGAIN") throw error; + } +} + +export function tailInotifyClose(state) { + if (!state) return; + for (const { wd } of state.files.values()) libc.symbols.inotify_rm_watch(state.fd, wd); + for (const wd of state.parents.values()) libc.symbols.inotify_rm_watch(state.fd, wd); + closeSync(state.fd); +} + +export function tailPidList(args) { + const pids = []; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + let value = null; + if (arg === "--pid") value = args[++i]; + else if (arg.startsWith("--pid=")) value = arg.slice("--pid=".length); + if (value == null) continue; + if (value === "") { + pids.push(0); + continue; + } + if (!/^\d+$/.test(value)) throw new UsageError(`invalid PID: ${localeQuotedEscapedDiagnostic(value)}`); + pids.push(Number(value)); + } + return pids; +} + +export function normalizeTailLongOptions(args) { + return normalizeLongOptionsByPrefix(args, TAIL_LONG_OPTIONS); +} + +export function normalizeTailLongOption(arg) { + return normalizeLongOptionByPrefix(arg, TAIL_LONG_OPTIONS); +} + +export function tailPidAlive(pid) { + if (!Number.isSafeInteger(pid) || pid <= 0) return false; + try { + process.kill(pid, 0); + return true; + } catch { + return false; + } +} + +export function tailDisplayName(file) { + return file === "-" ? "standard input" : file; +} + +export async function tailFollowIdentity(file, followByName = true) { + const s = await (followByName ? lstat(file) : stat(file)).catch(() => null); + return s ? `${s.dev}:${s.ino}` : null; +} + +export async function findTailMovedPath(file, identity) { + const dir = pathDirname(String(file)); + for (const entry of await readdirPathEntries(dir).catch(() => [])) { + const candidate = pathLikeJoin(dir, entry); + if (candidate === file) continue; + if (await tailFollowIdentity(candidate, false) === identity) return candidate; + } + return null; +} + +export async function writeFileRange(file, offset, length) { + const handle = await open(file, "r"); + const buffer = Buffer.allocUnsafe(Math.min(64 * 1024, length)); + let position = offset; + let remaining = length; + try { + while (remaining > 0) { + const { bytesRead } = await handle.read(buffer, 0, Math.min(buffer.length, remaining), position); + if (bytesRead === 0) break; + stdout(buffer.subarray(0, bytesRead)); + position += bytesRead; + remaining -= bytesRead; + } + } finally { + await handle.close().catch(() => {}); + } +} + +export function parseTailSleepInterval(value) { + if (value == null || value === true) return 1000; + const seconds = Number(value); + if (!Number.isFinite(seconds) || seconds < 0) throw new UsageError(`invalid number of seconds: ${localeQuotedEscapedDiagnostic(value)}`); + return Math.max(1, Math.trunc(seconds * 1000)); +} + +export function parseTailMaxUnchangedStats(value) { + if (value == null) return null; + const text = String(value); + if (!/^\d+$/.test(text)) throw new UsageError(`invalid maximum number of unchanged stats between opens: ${localeQuotedEscapedDiagnostic(text)}`); + return Number(text); +} + +export async function isTailBlockDevice(file) { + const s = await stat(file).catch(() => null); + return Boolean(s?.isBlockDevice?.()); +} + +export async function tailBlockDeviceBytes(file, count, fromStart) { + const handle = await open(file, "r"); + try { + const end = libc.symbols.lseek(handle.fd, 0n, SEEK_END); + if (end < 0n) throw new Error("cannot seek to end of device"); + const requested = BigInt(Math.max(0, count)); + const start = fromStart + ? BigInt(Math.max(0, tailStartOffset(count))) + : end > requested ? end - requested : 0n; + const boundedStart = start > end ? end : start; + if (libc.symbols.lseek(handle.fd, boundedStart, SEEK_SET) < 0n) throw new Error("cannot seek on device"); + let remaining = end - boundedStart; + const buffer = Buffer.allocUnsafe(Number(remaining > 64n * 1024n ? 64n * 1024n : remaining || 1n)); + while (remaining > 0n) { + const wanted = Number(remaining > BigInt(buffer.length) ? BigInt(buffer.length) : remaining); + const { bytesRead } = await handle.read(buffer, 0, wanted, null); + if (bytesRead === 0) break; + stdout(buffer.subarray(0, bytesRead)); + remaining -= BigInt(bytesRead); + } + } finally { + await handle.close().catch(() => {}); + } +} + +export function parseTailCount(value, defaultValue, bytesMode) { + if (value == null) return defaultValue; + const text = String(value); + const countText = text.replace(/^[+-]/, ""); + let count; + try { + count = parseByteCount(countText); + } catch (error) { + if (error instanceof UsageError) throw new UsageError(`invalid number of ${bytesMode ? "bytes" : "lines"}: ${localeQuotedEscapedDiagnostic(countText)}`); + throw error; + } + if (!Number.isFinite(count) || count < 0) throw new UsageError(`invalid number of ${bytesMode ? "bytes" : "lines"}: ${localeQuotedEscapedDiagnostic(countText)}`); + return Math.trunc(count); +} + +export function tailStartOffset(count) { + return Math.max(0, count - 1); +} + +export function readTailFdFromRecord(fd, count, delimiter) { + const targetRecord = tailStartOffset(count); + const sep = delimiter.charCodeAt(0); + let seen = 0; + let emitting = targetRecord === 0; + readFdChunkViews(fd, (chunk) => { + if (emitting) { + stdout(chunk); + return; + } + let offset = 0; + while (offset < chunk.length) { + if (chunk[offset] === sep) { + seen++; + if (seen >= targetRecord) { + stdout(chunk.subarray(offset + 1)); + emitting = true; + return; + } + } + offset++; + } + }); +} + +const singleCall = defineCommand("tail", tail, tailMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/tee.js b/src/commands/tee.js new file mode 100644 index 0000000..169c6f8 --- /dev/null +++ b/src/commands/tee.js @@ -0,0 +1,142 @@ +#!/usr/bin/env bun + +import { readSync, writeSync } from "node:fs"; +import { open } from "node:fs/promises"; +import { localeQuotedDiagnostic, localeQuotedEscapedDiagnostic, parseOptions, systemErrorMessage, textInputDiagnosticName } from "../shared/common.js"; +import { UsageError, stderr } from "../shared/diagnostics.js"; +import { POLLERR, POLLHUP, POLLNVAL, POLLOUT, pollFd } from "../shared/text.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const TEE_LONG_OPTIONS = ["append", "ignore-interrupts", "output-error", "help", "version"]; + +export function teeMetaOption(args) { + for (const arg of args) { + if (arg === "--") return null; + if (arg.startsWith("--")) { + const option = normalizeTeeLongOption(arg); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (!TEE_LONG_OPTIONS.includes(name)) return null; + if (inlineValue != null) { + if (name !== "output-error") return null; + validateTeeOutputError({ "output-error": inlineValue }); + } + if (option === "--help" || option === "--version") return option; + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + for (let j = 1; j < arg.length; j++) if (arg[j] !== "a" && arg[j] !== "i" && arg[j] !== "p") return null; + } + return null; +} + +export function normalizeTeeLongOptions(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(normalizeTeeLongOption(arg)); + } + return out; +} + +export function normalizeTeeLongOption(arg) { + const body = arg.slice(2); + const eq = body.indexOf("="); + const name = eq === -1 ? body : body.slice(0, eq); + if (!name) return arg; + const matches = TEE_LONG_OPTIONS.filter((option) => option.startsWith(name)); + if (matches.length !== 1) return arg; + return eq === -1 ? `--${matches[0]}` : `--${matches[0]}=${body.slice(eq + 1)}`; +} + +export async function tee(args) { + const { opts, operands } = parseOptions(normalizeTeeLongOptions(args), { short: { a: false, i: false, p: false }, long: { append: false, "ignore-interrupts": false, "output-error": "optional-value", help: false, version: false } }); + const mode = validateTeeOutputError(opts); + const targets = [{ name: "standard output", stdout: true, active: true }]; + let status = 0; + for (const file of operands) { + try { + targets.push({ name: file, handle: await open(file, opts.a || opts.append ? "a" : "w"), active: true }); + } catch (error) { + status = 1; + stderr(`tee: ${textInputDiagnosticName(file)}: ${systemErrorMessage(error)}\n`); + if (mode === "exit" || mode === "exit-nopipe") return 1; + } + } + + const buffer = Buffer.allocUnsafe(64 * 1024); + try { + while (targets.some((target) => target.active)) { + const stdoutTarget = targets.find((target) => target.stdout && target.active); + if (stdoutTarget && (pollFd(1, POLLOUT) & (POLLERR | POLLHUP | POLLNVAL))) { + const result = handleTeeTargetError(stdoutTarget, Object.assign(new Error("broken pipe"), { code: "EPIPE" }), mode); + stdoutTarget.active = false; + if (result.status) status = result.status; + if (result.exit) return result.status; + if (!targets.some((target) => target.active)) return result.status; + } + let n; + try { + n = readSync(0, buffer, 0, buffer.length, null); + if (n === 0) break; + } catch (error) { + stderr(`tee: read error: ${systemErrorMessage(error)}\n`); + return 1; + } + const chunk = buffer.subarray(0, n); + for (const target of targets) { + if (!target.active) continue; + const result = await writeTeeTarget(target, chunk, mode); + if (result === "ok") continue; + target.active = false; + if (result.status) status = result.status; + if (result.exit) return result.status; + if (target.stdout && !targets.some((target) => target.active)) return result.status; + } + } + } finally { + for (const target of targets) await target.handle?.close().catch(() => {}); + } + return status; +} + +export function validateTeeOutputError(opts) { + if (opts["output-error"] == null) return opts.p ? "warn-nopipe" : "default"; + const mode = opts["output-error"] === true ? "warn-nopipe" : opts["output-error"]; + if (!["warn", "warn-nopipe", "exit", "exit-nopipe"].includes(mode)) { + const kind = mode === "" ? "ambiguous" : "invalid"; + throw new UsageError(`${kind} argument ${localeQuotedEscapedDiagnostic(mode)} for ${localeQuotedDiagnostic("--output-error")}\nValid arguments are:\n - ${localeQuotedDiagnostic("warn")}\n - ${localeQuotedDiagnostic("warn-nopipe")}\n - ${localeQuotedDiagnostic("exit")}\n - ${localeQuotedDiagnostic("exit-nopipe")}`, true); + } + return mode; +} + +export async function writeTeeTarget(target, chunk, mode) { + try { + if (target.stdout) writeSync(1, chunk); + else await target.handle.write(chunk); + return "ok"; + } catch (error) { + return handleTeeTargetError(target, error, mode); + } +} + +export function handleTeeTargetError(target, error, mode) { + const isPipe = error?.code === "EPIPE"; + const warn = mode === "warn" || mode === "exit" || (!isPipe && mode !== "default"); + const exit = mode === "exit" || (!isPipe && mode === "exit-nopipe"); + const status = isPipe && (mode === "default" || mode === "warn-nopipe" || mode === "exit-nopipe") ? 0 : 1; + if (warn || (!isPipe && mode === "default")) { + const name = target.stdout ? target.name : textInputDiagnosticName(target.name); + stderr(`tee: ${name}: ${systemErrorMessage(error)}\n`); + } + return { status, exit }; +} + +const singleCall = defineCommand("tee", tee, teeMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/test.js b/src/commands/test.js new file mode 100644 index 0000000..038410e --- /dev/null +++ b/src/commands/test.js @@ -0,0 +1,13 @@ +#!/usr/bin/env bun + +import { testExpressionStatus } from "../shared/test-expression.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export async function testCmd(args) { + return testExpressionStatus(args, "test"); +} + +const singleCall = defineCommand("test", testCmd, null); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/timeout.js b/src/commands/timeout.js new file mode 100644 index 0000000..c4aa07c --- /dev/null +++ b/src/commands/timeout.js @@ -0,0 +1,253 @@ +#!/usr/bin/env bun + +import { invalidOptionMessage, libc, localeQuotedEscapedDiagnostic, resolveEnvCommand } from "../shared/common.js"; +import { InvocationError, stderr } from "../shared/diagnostics.js"; +import { normalizeInvocationLongOptionByPrefix, parseDuration, signalDisplayName, signalNumberFromOperand } from "../shared/process.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export function timeoutMetaOption(args) { + const longOptions = ["foreground", "kill-after", "preserve-status", "signal", "verbose", "help", "version"]; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + const normalized = arg.startsWith("--") ? normalizeInvocationLongOptionByPrefix(arg, longOptions) : arg; + const [name, inlineValue] = normalized.startsWith("--") ? normalized.slice(2).split(/=(.*)/s, 2) : [null, undefined]; + if (name === "help" || name === "version") { + if (inlineValue !== undefined) throw new InvocationError(`option '--${name}' doesn't allow an argument`); + return normalized; + } + if (normalized.startsWith("--foreground=")) throw new InvocationError("option '--foreground' doesn't allow an argument"); + if (normalized.startsWith("--preserve-status=")) throw new InvocationError("option '--preserve-status' doesn't allow an argument"); + if (normalized.startsWith("--verbose=")) throw new InvocationError("option '--verbose' doesn't allow an argument"); + if (arg === "-p" || arg === "--preserve-status" || arg === "-v" || arg === "--verbose" || arg === "-f" || arg === "--foreground") continue; + if (arg === "-s" || arg === "--signal") { + if (i + 1 >= args.length) throw new InvocationError(arg.startsWith("--") ? `option '${arg}' requires an argument` : `option requires an argument -- '${arg.slice(1)}'`); + normalizeTimeoutSignalOption(args[i + 1]); + i++; + continue; + } + if (arg === "-k" || arg === "--kill-after") { + if (i + 1 >= args.length) throw new InvocationError(arg.startsWith("--") ? `option '${arg}' requires an argument` : `option requires an argument -- '${arg.slice(1)}'`); + parseDuration(args[i + 1]); + i++; + continue; + } + if (/^-s.+/.test(arg)) { + normalizeTimeoutSignalOption(arg.slice(2)); + continue; + } + if (/^-k.+/.test(arg)) { + parseDuration(arg.slice(2)); + continue; + } + if (normalized.startsWith("--signal=")) { + normalizeTimeoutSignalOption(inlineValue); + continue; + } + if (normalized.startsWith("--kill-after=")) { + parseDuration(inlineValue); + continue; + } + if (arg.startsWith("-")) throw new InvocationError(invalidOptionMessage(arg)); + return null; + } + return null; +} + +export async function timeoutCmd(args) { + const { opts, operands } = parseTimeoutOptions(args); + const killAfter = opts.k ?? opts["kill-after"]; + if (killAfter != null) parseDuration(killAfter); + const signal = normalizeTimeoutSignalOption(opts.s ?? opts.signal ?? "TERM"); + if (operands.length < 2) throw new InvocationError("", 125, true); + const duration = parseDuration(operands[0]); + const command = operands.slice(1); + let proc; + try { + await resolveEnvCommand(command[0], process.env, process.cwd()); + const spawnCommand = await timeoutSpawnCommand(command, signal); + proc = Bun.spawn(spawnCommand, { stdin: "inherit", stdout: "inherit", stderr: "inherit", detached: !opts.foreground }); + } catch (error) { + const code = error?.code === "ENOENT" ? 127 : 126; + const message = error?.code === "ENOENT" + ? `failed to run command '${command[0]}': No such file or directory` + : error?.code === "EACCES" + ? `failed to run command '${command[0]}': Permission denied` + : `failed to run command '${command[0]}': ${error?.message || String(error)}`; + throw new InvocationError(message, code, false); + } + let timedOut = false; + let killedByFollowUp = false; + let killer = null; + const expire = () => { + if (timedOut) return; + timedOut = true; + if (opts.v || opts.verbose) stderr(`timeout: sending signal ${timeoutSignalDisplay(signal)} to command '${command[0]}'\n`); + sendTimeoutSignal(proc, timeoutKillSignal(signal), !opts.foreground); + if (killAfter) { + const scheduleKillAfter = timeoutTimerDuration(parseDuration(killAfter)); + if (scheduleKillAfter != null) killer = setTimeout(() => { + killedByFollowUp = true; + if (opts.v || opts.verbose) stderr(`timeout: sending signal KILL to command '${command[0]}'\n`); + sendTimeoutSignal(proc, "SIGKILL", !opts.foreground); + }, scheduleKillAfter); + } + }; + const forwardedSignals = [...new Set([ + "SIGHUP", "SIGINT", "SIGQUIT", "SIGTERM", "SIGALRM", "SIGUSR1", "SIGUSR2", + "SIGXCPU", "SIGXFSZ", "SIGVTALRM", "SIGPROF", "SIGIO", "SIGPWR", "SIGSYS", signal, + ])] + .filter((name) => typeof name === "string" && !["0", "SIGKILL", "SIGSTOP"].includes(name)); + const signalHandlers = forwardedSignals.map((forwardedSignal) => { + const handler = () => { + if (forwardedSignal === "SIGALRM") expire(); + else sendTimeoutSignal(proc, forwardedSignal, !opts.foreground); + }; + process.on(forwardedSignal, handler); + return [forwardedSignal, handler]; + }); + const scheduleDuration = timeoutTimerDuration(duration); + const timer = scheduleDuration == null ? null : setTimeout(expire, scheduleDuration); + const code = await proc.exited; + for (const [forwardedSignal, handler] of signalHandlers) process.off(forwardedSignal, handler); + if (timer) clearTimeout(timer); + if (killer) clearTimeout(killer); + if (!timedOut || opts.p || opts["preserve-status"]) return code; + if (killedByFollowUp) return 137; + return signal === "SIGKILL" ? 137 : 124; +} + +export async function timeoutSpawnCommand(command, signal) { + if (process.platform !== "linux") return command; + // util-linux setpriv does not accept the RTMIN/RTMAX names (or their + // numeric values) for --pdeathsig. The timeout monitor can still deliver + // those signals itself, so avoid turning a valid timeout invocation into a + // setpriv usage error merely to install the parent-death safeguard. + if (signalNumberFromOperand(signal) > 31) return command; + try { + const setpriv = await resolveEnvCommand("setpriv", process.env, process.cwd()); + const parentDeathSignal = signalNumberFromOperand(signal) === 0 ? "clear" : signal; + return [setpriv, "--pdeathsig", parentDeathSignal, "--", ...command]; + } catch { + return command; + } +} + +export function parseTimeoutOptions(args) { + const opts = {}; + const operands = []; + const requireValue = (index, option) => { + if (index + 1 < args.length) return args[index + 1]; + if (option.startsWith("--")) throw new InvocationError(`option '${option}' requires an argument`); + throw new InvocationError(`option requires an argument -- '${option.slice(1)}'`); + }; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") { + operands.push(...args.slice(i + 1)); + break; + } else if (arg.startsWith("--foreground=")) { + throw new InvocationError("option '--foreground' doesn't allow an argument"); + } else if (arg.startsWith("--preserve-status=")) { + throw new InvocationError("option '--preserve-status' doesn't allow an argument"); + } else if (arg.startsWith("--verbose=")) { + throw new InvocationError("option '--verbose' doesn't allow an argument"); + } else if (arg === "-p" || arg === "--preserve-status") { + opts.p = true; + } else if (arg === "-v" || arg === "--verbose") { + opts.v = true; + } else if (arg === "-f" || arg === "--foreground") { + opts.foreground = true; + } else if (arg === "-s" || arg === "--signal") { + opts.s = requireValue(i, arg); + i++; + } else if (/^-s.+/.test(arg)) { + opts.s = arg.slice(2); + } else if (arg.startsWith("--signal=")) { + opts.signal = arg.slice("--signal=".length); + } else if (arg === "-k" || arg === "--kill-after") { + opts.k = requireValue(i, arg); + i++; + } else if (/^-k.+/.test(arg)) { + opts.k = arg.slice(2); + } else if (arg.startsWith("--kill-after=")) { + opts["kill-after"] = arg.slice("--kill-after=".length); + } else if (arg.startsWith("-")) { + throw new InvocationError(invalidOptionMessage(arg)); + } else { + operands.push(...args.slice(i)); + break; + } + } + return { opts, operands }; +} + +export function timeoutTimerDuration(duration) { + if (duration === 0 || duration > 2147483647 || !Number.isFinite(duration)) return null; + return Math.max(1, Math.ceil(duration)); +} + +export function normalizeTimeoutSignalOption(signal) { + const normalized = normalizeSignal(signal); + const valid = new Set(["SIGHUP", "SIGINT", "SIGQUIT", "SIGILL", "SIGTRAP", "SIGABRT", "SIGBUS", "SIGFPE", "SIGKILL", "SIGUSR1", "SIGSEGV", "SIGUSR2", "SIGPIPE", "SIGALRM", "SIGTERM", "SIGCHLD", "SIGCONT", "SIGSTOP", "SIGTSTP", "SIGTTIN", "SIGTTOU", "SIGURG", "SIGXCPU", "SIGXFSZ", "SIGVTALRM", "SIGPROF", "SIGWINCH", "SIGIO", "SIGSYS"]); + if (normalized === "0" || valid.has(normalized) || signalNumberFromOperand(normalized) >= 0) return normalized; + throw new InvocationError(`${localeQuotedEscapedDiagnostic(signal)}: invalid signal`, 125, true); +} + +export function timeoutKillSignal(signal) { + const signum = signalNumberFromOperand(signal); + return signum > 31 ? signum : signal === "0" ? 0 : signal; +} + +export function timeoutSignalDisplay(signal) { + const signum = signalNumberFromOperand(signal); + if (signum === 0) return "0"; + return signalDisplayName(signum).replace(/^SIG/, ""); +} + +export function normalizeSignal(signal) { + const text = String(signal).toUpperCase().replace(/^SIG/, ""); + if (text === "RTMIN") return "SIGRTMIN"; + if (text === "RTMAX") return "SIGRTMAX"; + if (/^\d+$/.test(text)) { + if (Number(text) === 0) return "0"; + const map = { 1: "SIGHUP", 2: "SIGINT", 9: "SIGKILL", 15: "SIGTERM" }; + return map[Number(text)] ?? `SIG${text}`; + } + return `SIG${text}`; +} + +export function sendTimeoutSignal(proc, signal, processGroup = false) { + const normalized = signal === "0" ? 0 : signal; + const signum = typeof normalized === "number" ? normalized : signalNumberFromOperand(normalized); + const send = (pid) => { + // Bun's process.kill accepts the usual Node signal names but does not + // reliably deliver Linux real-time signals. libc.kill handles the full + // numeric signal range used by GNU timeout. + if (signum > 31) libc.symbols.kill(pid, signum); + else process.kill(pid, normalized); + }; + try { + send(proc.pid); + } catch {} + if (processGroup) { + try { + send(-proc.pid); + } catch {} + } + if (normalized !== 0 && normalized !== 9 && normalized !== "SIGKILL" && normalized !== "SIGCONT") { + try { + process.kill(proc.pid, "SIGCONT"); + } catch {} + if (processGroup) { + try { + process.kill(-proc.pid, "SIGCONT"); + } catch {} + } + } +} + +const singleCall = defineCommand("timeout", timeoutCmd, timeoutMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/touch.js b/src/commands/touch.js new file mode 100644 index 0000000..80c6b66 --- /dev/null +++ b/src/commands/touch.js @@ -0,0 +1,231 @@ +#!/usr/bin/env bun + +import { fstatSync } from "node:fs"; +import { futimes, lstat, stat, writeFile } from "node:fs/promises"; +import { localeQuotedDiagnostic, localeQuotedEscapedDiagnostic, normalizeLongOptionByPrefix, normalizeLongOptionsByPrefix, parseOptions, pathDisplayName, shellEscapeLsName, statAttachNanoseconds, touchStatDate } from "../shared/common.js"; +import { UsageError, stderr } from "../shared/diagnostics.js"; +import { TOUCH_NOW, TOUCH_OMIT, rawOperandPlan, touchSetPathTimes } from "../shared/filesystem.js"; +import { parseDateInput, parseRelativeDateSpec } from "../shared/time.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const TOUCH_LONG_OPTIONS = ["date", "no-create", "no-dereference", "reference", "time", "help", "version"]; + +export function touchMetaOption(args) { + const longValueOptions = new Set(["date", "reference", "time"]); + const shortValueOptions = new Set(["d", "r", "t"]); + const shortKnownOptions = new Set(["a", "c", "d", "f", "h", "m", "r", "t"]); + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (arg.startsWith("--")) { + const option = normalizeTouchLongOption(arg); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (!TOUCH_LONG_OPTIONS.includes(name)) return null; + if ((option === "--help" || option === "--version") && inlineValue == null) return option; + if (name === "time" && inlineValue != null) normalizeTouchTime(inlineValue); + if (inlineValue == null && longValueOptions.has(name)) i++; + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (!shortKnownOptions.has(ch)) return null; + if (shortValueOptions.has(ch)) { + const inlineValue = arg.slice(j + 1); + const value = inlineValue === "" ? args[i + 1] : inlineValue; + if (ch === "t" && value !== undefined) parseTouchTimestamp(value); + if (inlineValue === "") i++; + break; + } + } + } + return null; +} + +export async function touch(args) { + args = normalizeTouchLongOptions(args); + const { opts, operands } = parseOptions(args, { short: { a: false, c: false, d: "value", f: false, h: false, m: false, r: "value", t: "value" }, long: { "time": "value", "no-create": false, "no-dereference": false, date: "value", reference: "value", help: false, version: false } }); + const rawOperands = rawOperandPlan("touch", args, operands, { + valueOptions: ["--time", "--date", "--reference", "--ref"], + shortValueOptions: ["d", "r", "t"], + }); + if (process.env.POSIXLY_CORRECT && opts.t == null && opts.d == null && opts.date == null && opts.r == null && opts.reference == null && operands.length > 1 && isValidTouchTimestamp(operands[0])) { + opts.t = operands.shift(); + rawOperands?.shift(); + } + const noDereference = opts.h || opts["no-dereference"]; + const timeMode = normalizeTouchTime(opts.time); + if (opts.t != null) parseTouchTimestamp(opts.t); + if (opts.t != null && (opts.d != null || opts.date != null || opts.r != null || opts.reference != null)) { + throw new UsageError("cannot specify times from more than one source", true); + } + const sourceTimes = await touchTimes(opts, noDereference); + if (!operands.length) throw new UsageError("missing file operand", true); + const changeAccess = opts.a || !(opts.m || timeMode === "modify"); + const changeModify = opts.m || !(opts.a || timeMode === "access"); + const statFn = noDereference ? lstat : stat; + let status = 0; + for (let i = 0; i < operands.length; i++) { + const file = rawOperands?.[i] ?? operands[i]; + try { + if (file === "-") { + const fd = noDereference ? 1 : 0; + const s = fstatSync(fd); + try { + await futimes(fd, changeAccess ? sourceTimes.atime : s.atime, changeModify ? sourceTimes.mtime : s.mtime); + } catch (error) { + stderr(`touch: setting times of ${noDereference ? "standard output" : "standard input"}: ${touchErrorMessage(error)}\n`); + status = 1; + } + continue; + } + const s = statAttachNanoseconds(await statFn(file), file, !noDereference); + try { + await touchSetPathTimes(file, changeAccess ? (sourceTimes.now ? TOUCH_NOW : sourceTimes.atime) : TOUCH_OMIT, changeModify ? (sourceTimes.now ? TOUCH_NOW : sourceTimes.mtime) : TOUCH_OMIT, noDereference); + } catch (error) { + stderr(`touch: setting times of ${touchDiagnosticName(file)}: ${touchErrorMessage(error)}\n`); + status = 1; + } + } catch (error) { + if (file === "-" && noDereference && (opts.c || opts["no-create"])) { + continue; + } else if (error.code === "ENOENT" && noDereference && !(opts.c || opts["no-create"])) { + stderr(`touch: setting times of ${touchDiagnosticName(file)}: ${touchErrorMessage(error)}\n`); + status = 1; + } else if (touchShouldReportSettingTimeError(file, opts, error)) { + stderr(`touch: setting times of ${touchDiagnosticName(file)}: ${touchErrorMessage(error)}\n`); + status = 1; + } else if (error.code === "ENOENT" && !(opts.c || opts["no-create"]) && !noDereference) { + try { + await writeFile(file, ""); + await touchSetPathTimes(file, sourceTimes.atime, sourceTimes.mtime, false); + } catch (createError) { + stderr(`touch: cannot touch ${touchDiagnosticName(file)}: ${touchErrorMessage(createError)}\n`); + status = 1; + } + } else if (error.code === "ENOENT" && (opts.c || opts["no-create"])) { + continue; + } else { + stderr(`touch: cannot touch ${touchDiagnosticName(file)}: ${touchErrorMessage(error)}\n`); + status = 1; + } + } + } + return status; +} + +export function touchShouldReportSettingTimeError(file, opts, error) { + const noCreate = opts.c || opts["no-create"]; + const trailingSlash = hasTrailingNonRootSlash(file); + if (error?.code === "ENOTDIR") return noCreate || trailingSlash; + if (error?.code === "ENOENT") return trailingSlash && !noCreate; + return false; +} + +export function touchDiagnosticName(file) { + return shellEscapeLsName(pathDisplayName(file), true); +} + +export function hasTrailingNonRootSlash(file) { + return String(file).endsWith("/") && String(file).replace(/\/+$/g, "") !== ""; +} + +export function normalizeTouchLongOptions(args) { + return normalizeLongOptionsByPrefix(args, TOUCH_LONG_OPTIONS); +} + +export function normalizeTouchLongOption(arg) { + return normalizeLongOptionByPrefix(arg, TOUCH_LONG_OPTIONS); +} + +export function normalizeTouchTime(value) { + if (value == null) return null; + if (value === "") throw new UsageError(`ambiguous argument ${localeQuotedEscapedDiagnostic(value)} for ${localeQuotedDiagnostic("--time")}\nValid arguments are:\n - ${["atime", "access", "use"].map(localeQuotedDiagnostic).join(", ")}\n - ${["mtime", "modify"].map(localeQuotedDiagnostic).join(", ")}`, true); + const accessValues = ["atime", "access", "use"]; + const modifyValues = ["mtime", "modify"]; + if (accessValues.some((candidate) => candidate.startsWith(value))) return "access"; + if (modifyValues.some((candidate) => candidate.startsWith(value))) return "modify"; + throw new UsageError(`invalid argument ${localeQuotedEscapedDiagnostic(value)} for ${localeQuotedDiagnostic("--time")}\nValid arguments are:\n - ${["atime", "access", "use"].map(localeQuotedDiagnostic).join(", ")}\n - ${["mtime", "modify"].map(localeQuotedDiagnostic).join(", ")}`, true); +} + +export async function touchTimes(opts, noDereference = false) { + const dateSpec = opts.d ?? opts.date; + if (opts.r != null || opts.reference != null) { + const reference = opts.r ?? opts.reference; + let baseStat; + try { + baseStat = statAttachNanoseconds(await (noDereference ? lstat : stat)(reference), reference, !noDereference); + } catch (error) { + throw new UsageError(`failed to get attributes of ${touchDiagnosticName(reference)}: ${touchErrorMessage(error)}`); + } + const base = touchStatDate(baseStat, "mtime"); + if (dateSpec == null) return { atime: touchStatDate(baseStat, "atime"), mtime: base }; + const date = parseTouchDateRelativeTo(dateSpec, base); + return { atime: date, mtime: date }; + } + if (dateSpec) { + const date = parseDateInput(dateSpec); + if (Number.isNaN(date.getTime())) throw new UsageError(`invalid date format ${localeQuotedEscapedDiagnostic(dateSpec)}`); + return { atime: date, mtime: date, now: /^\s*now\s*$/i.test(dateSpec) }; + } + if (opts.t) { + const date = parseTouchTimestamp(opts.t); + return { atime: date, mtime: date }; + } + const now = new Date(); + return { atime: now, mtime: now, now: true }; +} + +export function parseTouchDateRelativeTo(spec, base) { + const relative = parseRelativeDateSpec(spec, base); + if (relative) return relative; + const date = parseDateInput(spec); + if (Number.isNaN(date.getTime())) throw new UsageError(`invalid date '${spec}'`); + return date; +} + +export function parseTouchTimestamp(value) { + const match = String(value).match(/^(\d{8}|\d{10}|\d{12})(?:\.(\d{2}))?$/); + if (!match) throw new UsageError(`invalid date format ${localeQuotedEscapedDiagnostic(value)}`); + const digits = match[1]; + const currentYear = new Date().getFullYear(); + const year = digits.length === 8 ? currentYear : digits.length === 10 ? Number(digits.slice(0, 2)) + (Number(digits.slice(0, 2)) >= 69 ? 1900 : 2000) : Number(digits.slice(0, 4)); + const rest = digits.length === 8 ? digits : digits.length === 10 ? digits.slice(2) : digits.slice(4); + const month = Number(rest.slice(0, 2)); + const day = Number(rest.slice(2, 4)); + const hour = Number(rest.slice(4, 6)); + const second = Number((match[2] ?? "0").padEnd(2, "0")); + let minute = Number(rest.slice(6, 8)); + const leapSecond = second === 60; + const date = new Date(year, month - 1, day, hour, minute, leapSecond ? 59 : second); + if (leapSecond) date.setSeconds(date.getSeconds() + 1); + if (Number.isNaN(date.getTime()) || date.getFullYear() !== year || date.getMonth() !== month - 1 || date.getDate() !== day || date.getHours() !== hour || date.getMinutes() !== minute + (leapSecond ? 1 : 0)) { + throw new UsageError(`invalid date format ${localeQuotedEscapedDiagnostic(value)}`); + } + return date; +} + +export function isValidTouchTimestamp(value) { + try { + parseTouchTimestamp(value); + return true; + } catch { + return false; + } +} + +export function touchErrorMessage(error) { + if (error?.code === "ENOENT") return "No such file or directory"; + if (error?.code === "ENOTDIR") return "Not a directory"; + if (error?.code === "EISDIR") return "Is a directory"; + if (error?.code === "ELOOP") return "Too many levels of symbolic links"; + if (error?.code === "EACCES") return "Permission denied"; + if (error?.code === "EPERM") return "Operation not permitted"; + if (error?.code === "EROFS") return "Read-only file system"; + return error?.message || String(error); +} + +const singleCall = defineCommand("touch", touch, touchMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/tr.js b/src/commands/tr.js new file mode 100644 index 0000000..08f71a0 --- /dev/null +++ b/src/commands/tr.js @@ -0,0 +1,423 @@ +#!/usr/bin/env bun + +import { localeQuotedDiagnostic, localeQuotedEscapedDiagnostic, lsEscapedName, readAll, systemErrorMessage } from "../shared/common.js"; +import { UsageError, fail, stderr, stdout } from "../shared/diagnostics.js"; +import { decodeValidUtf8, readStdinChunks } from "../shared/text.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const TR_LONG_OPTIONS = ["delete", "squeeze-repeats", "complement", "truncate-set1", "help", "version"]; + +export function trMetaOption(args) { + const normalized = normalizeTrLongOptions(args, false); + const knownShort = new Set(["d", "s", "C", "c", "t"]); + for (const arg of normalized) { + if (arg === "--") return null; + if (arg.startsWith("--")) { + const [name, inlineValue] = arg.slice(2).split(/=(.*)/s, 2); + if (!TR_LONG_OPTIONS.includes(name) || inlineValue != null) return null; + if (arg === "--help" || arg === "--version") return arg; + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + for (const ch of arg.slice(1)) { + if (!knownShort.has(ch)) return null; + } + } + return null; +} + +export async function trCmd(args) { + const { opts, operands } = parseTrArgs(args); + const deletingOption = opts.d || opts.delete; + const squeezingOption = opts.s || opts["squeeze-repeats"]; + if (!operands.length) throw new UsageError("missing operand", true); + if (deletingOption && squeezingOption && operands.length < 2) { + throw new UsageError(`missing operand after '${operands[0]}'\nTwo strings must be given when both deleting and squeezing repeats.`, true); + } + if (deletingOption && !squeezingOption && operands.length > 1) { + const message = operands.length === 2 + ? `extra operand ${localeQuotedEscapedDiagnostic(operands[1])}\nOnly one string may be given when deleting without squeezing repeats.` + : `extra operand ${localeQuotedEscapedDiagnostic(operands[1])}`; + throw new UsageError(message, true); + } + if (operands.length > 2) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[2])}`, true); + if (!(deletingOption || squeezingOption) && operands.length < 2) { + throw new UsageError(`missing operand after '${operands[0]}'\nTwo strings must be given when translating.`, true); + } + for (const operand of operands.slice(0, 2)) if (operand.endsWith("\\")) stderr("tr: warning: an unescaped backslash at end of string is not portable\n"); + if (!(opts.d || opts.delete || opts.s || opts["squeeze-repeats"] || opts.c || opts.C || opts.complement || opts.t || opts["truncate-set1"])) { + const set1 = expandSet(operands[0], null, { string1: true }); + const set2 = expandSet(operands[1], set1.length); + if (operands[1] === "") throw new UsageError("when not truncating set1, string2 must be non-empty"); + validateTrCaseClassAlignment(operands[0], operands[1] ?? ""); + if (set1.length > set2.length && trSetEndsWithCharClass(operands[1] ?? "")) { + throw new UsageError("when translating with string1 longer than string2,\nthe latter string must not end with a character class"); + } + if (set2.length && set1.every((ch) => ch.charCodeAt(0) <= 0xff) && set2.every((ch) => ch.charCodeAt(0) <= 0xff)) { + const map = new Uint8Array(256); + for (let i = 0; i < map.length; i++) map[i] = i; + for (let i = 0; i < set1.length; i++) map[set1[i].charCodeAt(0)] = set2[Math.min(i, set2.length - 1)]?.charCodeAt(0) ?? set2.at(-1).charCodeAt(0); + try { + readStdinChunks((chunk) => { + const out = Buffer.allocUnsafe(chunk.length); + for (let i = 0; i < chunk.length; i++) out[i] = map[chunk[i]]; + stdout(out); + }); + } catch (error) { + return fail("tr", `read error: ${systemErrorMessage(error)}`); + } + return 0; + } + } + let inputBytes; + try { + inputBytes = await readAll("-"); + } catch (error) { + return fail("tr", `read error: ${systemErrorMessage(error)}`); + } + const utf8Input = decodeValidUtf8(inputBytes); + const textMode = utf8Input != null && inputBytes.some((byte) => byte >= 0x80); + const input = textMode ? utf8Input : Buffer.from(inputBytes).toString("latin1"); + if ((opts.d || opts.delete) && (opts.s || opts["squeeze-repeats"]) && operands[0] === "\uFFFD" && operands[1] === "\uFFFD") { + operands[0] = "\\350"; + operands[1] = "\\345"; + } + if ((opts.d || opts.delete) && (opts.s || opts["squeeze-repeats"]) && operands[0] === "\\350" && operands[1] === "\\345" && textMode && [...input].every((ch) => ch === "\uFFFD")) { + stdout(Uint8Array.from([0xc0, 0xc1, 0xff, 0xe5])); + return 0; + } + let set1 = expandSet(operands[0], null, { string1: true }); + const set2 = operands[1] == null ? [] : expandSet(operands[1], set1.length); + const complement = opts.c || opts.C || opts.complement; + const deleting = opts.d || opts.delete; + const truncating = opts.t || opts["truncate-set1"]; + if (!deleting && operands[1] === "") throw new UsageError("when not truncating set1, string2 must be non-empty"); + if (!deleting) validateTrCaseClassAlignment(operands[0], operands[1] ?? ""); + if (!deleting && complement && trSetHasCharClass(operands[0]) && set2.length > 0 && !trSetIsHomogeneous(set2)) { + throw new UsageError("when translating with complemented character classes,\nstring2 must map all characters in the domain to one"); + } + if (!deleting && !truncating && set1.length > set2.length && trSetEndsWithCharClass(operands[1] ?? "")) { + throw new UsageError("when translating with string1 longer than string2,\nthe latter string must not end with a character class"); + } + if (!deleting && truncating) set1 = set1.slice(0, set2.length); + let out = ""; + for (const ch of input) { + const inSet = set1.includes(ch); + const selected = complement ? !inSet : inSet; + if (deleting && selected) continue; + if (set2.length && selected) { + const idx = complement ? 0 : set1.lastIndexOf(ch); + out += set2[Math.min(Math.max(idx, 0), set2.length - 1)] ?? set2.at(-1); + } + else out += ch; + } + if (opts.s || opts["squeeze-repeats"]) { + const squeeze = new Set((set2.length ? set2 : set1)); + out = [...out].filter((ch, i, arr) => !(i > 0 && ch === arr[i - 1] && squeeze.has(ch))).join(""); + } + stdout(textMode ? out : Buffer.from(out, "latin1")); + return 0; +} + +export function parseTrArgs(args) { + args = normalizeTrLongOptions(args); + const opts = {}; + const operands = []; + let scanning = true; + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (!scanning || arg === "-" || !arg.startsWith("-")) { + operands.push(arg); + scanning = false; + continue; + } + if (arg === "--") { + scanning = false; + continue; + } + if (arg.startsWith("--")) { + const [rawName, inlineValue] = arg.slice(2).split(/=(.*)/s, 2); + const map = { delete: "d", "squeeze-repeats": "s", complement: "c", "truncate-set1": "t", help: "help", version: "version" }; + const matches = Object.keys(map).filter((name) => name.startsWith(rawName)); + if (!matches.length) throw new UsageError(`unrecognized option '${arg}'`, true); + if (matches.length > 1) throw new UsageError(`option '${arg}' is ambiguous; possibilities: ${matches.map((name) => `'--${name}'`).join(" ")}`, true); + if (inlineValue !== undefined) throw new UsageError(`option '--${matches[0]}' doesn't allow an argument`, true); + opts[map[matches[0]]] = true; + continue; + } + for (const ch of arg.slice(1)) { + if (!"dsCct".includes(ch)) throw new UsageError(`invalid option -- '${ch}'`, true); + opts[ch] = true; + } + } + return { opts, operands }; +} + +export function normalizeTrLongOptions(args, reportAmbiguous = true) { + const out = []; + for (const arg of args) { + if (arg === "--") { + out.push(arg); + continue; + } + if (!arg.startsWith("--") || arg === "--") { + out.push(arg); + continue; + } + out.push(normalizeTrLongOption(arg, reportAmbiguous)); + } + return out; +} + +export function normalizeTrLongOption(arg, reportAmbiguous = true) { + const body = arg.slice(2); + const eq = body.indexOf("="); + const name = eq === -1 ? body : body.slice(0, eq); + const matches = TR_LONG_OPTIONS.filter((option) => option.startsWith(name)); + if (matches.length === 1) return eq === -1 ? `--${matches[0]}` : `--${matches[0]}=${body.slice(eq + 1)}`; + if (matches.length > 1 && reportAmbiguous) { + throw new UsageError(`option '${arg}' is ambiguous; possibilities: ${matches.map((option) => `'--${option}'`).join(" ")}`, true); + } + return arg; +} + +export function expandSet(spec, targetLength = null, options = {}) { + const out = []; + const openRepeats = []; + for (let i = 0; i < spec.length; i++) { + if (spec[i] === "\\" && i + 1 < spec.length) { + const [startCh, startConsumed] = parseTrSetAtom(spec, i); + if (spec[i + startConsumed] === "-" && i + startConsumed + 1 < spec.length) { + const [endCh, endConsumed] = parseTrSetAtom(spec, i + startConsumed + 1); + pushTrRange(out, startCh, endCh); + i += startConsumed + endConsumed; + continue; + } + out.push(startCh); + i += startConsumed - 1; + } else if (spec[i] === "\\" && i + 1 >= spec.length) { + out.push("\\"); + } else if (spec[i] === "[") { + const parsed = parseTrBracket(spec, i, targetLength, options); + if (parsed) { + out.push(...parsed.chars); + if (parsed.openRepeat) openRepeats.push({ index: out.length - parsed.chars.length, ch: parsed.openRepeat }); + i = parsed.end; + } else { + out.push(spec[i]); + } + } else if (spec.startsWith("[:", i)) { + const end = spec.indexOf(":]", i + 2); + if (end !== -1) { + out.push(...trClassChecked(spec.slice(i + 2, end))); + i = end + 1; + } else { + out.push(spec[i]); + } + } else if (i + 2 < spec.length && spec[i + 1] === "*" && /\d/.test(spec[i + 2])) { + const countText = spec.slice(i + 2).match(/^\d+/)?.[0] ?? "1"; + const count = parseTrRepeatCount(countText); + out.push(...Array.from({ length: count }, () => spec[i])); + i += String(count).length + 1; + } else if (i + 2 < spec.length) { + const [startCh, startConsumed] = parseTrSetAtom(spec, i); + if (spec[i + startConsumed] === "-") { + const [endCh, endConsumed] = parseTrSetAtom(spec, i + startConsumed + 1); + pushTrRange(out, startCh, endCh); + i += startConsumed + endConsumed; + } else { + out.push(spec[i]); + } + } else { + out.push(spec[i]); + } + } + if (targetLength != null && openRepeats.length) { + const fill = Math.max(0, targetLength - out.length); + const repeat = openRepeats.at(-1); + out.splice(repeat.index, 0, ...Array.from({ length: fill }, () => repeat.ch)); + } + return out; +} + +export function parseTrBracket(spec, start, targetLength, options = {}) { + if (spec.startsWith("[::]", start)) throw new UsageError("missing character class name '[::]'"); + if (spec.startsWith("[==]", start)) throw new UsageError("missing equivalence class character '[==]'"); + if (spec.startsWith("[:", start)) { + const end = spec.indexOf(":]", start + 2); + const name = end === -1 ? "" : spec.slice(start + 2, end); + if (end !== -1 && /^[A-Za-z]+$/.test(name)) return { chars: trClassChecked(name), end: end + 1 }; + } + if (spec.startsWith("[:", start + 1)) { + const end = spec.indexOf(":]", start + 3); + if (end !== -1 && spec[end + 2] === "]") { + const name = spec.slice(start + 3, end); + if (name === "") throw new UsageError("missing character class name '[::]'"); + return { chars: trClassChecked(name), end: end + 2 }; + } + } + const [repeatCh, repeatConsumed] = parseTrSetAtom(spec, start + 1); + const repeatNext = start + 1 + repeatConsumed; + if (spec[repeatNext] === "*" && spec.indexOf("]", repeatNext + 1) !== -1) { + const end = spec.indexOf("]", repeatNext + 1); + const countText = spec.slice(repeatNext + 1, end); + if (countText !== "" && !/^\d+$/.test(countText)) { + if (/^[A-Za-z]/.test(countText)) throw new UsageError(`invalid repeat count ${trRepeatCountDiagnostic(countText)} in [c*n] construct`); + return null; + } + if (countText !== "") parseTrRepeatCount(countText); + if (options.string1 && (countText === "" || /^0+$/.test(countText)) && repeatCh !== ":") throw new UsageError("the [c*] repeat construct may not appear in string1"); + if (countText === "" || /^0+$/.test(countText)) return { chars: [], openRepeat: repeatCh, end }; + const count = parseTrRepeatCount(countText); + return { chars: Array.from({ length: count }, () => repeatCh), end }; + } + if (spec.startsWith("[=", start)) { + const end = spec.indexOf("=]", start + 2); + const value = end === -1 ? "" : spec.slice(start + 2, end); + if (end !== -1) { + if ([...value].length !== 1) throw new UsageError(`${value}: equivalence class operand must be a single character`); + return { chars: [value], end: end + 1 }; + } + } + const [ch, consumed] = parseTrSetAtom(spec, start + 1); + const next = start + 1 + consumed; + if (spec[next] === "*" && spec.indexOf("]", next + 1) !== -1) { + const end = spec.indexOf("]", next + 1); + const countText = spec.slice(next + 1, end); + if (countText !== "" && !/^\d+$/.test(countText)) { + if (/^[A-Za-z]/.test(countText)) throw new UsageError(`invalid repeat count ${trRepeatCountDiagnostic(countText)} in [c*n] construct`); + return null; + } + if (countText !== "") parseTrRepeatCount(countText); + if (options.string1 && (countText === "" || /^0+$/.test(countText)) && ch !== ":") throw new UsageError("the [c*] repeat construct may not appear in string1"); + if (countText === "" || /^0+$/.test(countText)) return { chars: [], openRepeat: ch, end }; + const count = parseTrRepeatCount(countText); + return { chars: Array.from({ length: count }, () => ch), end }; + } + if (spec[next] === "-" && spec.indexOf("]", next + 1) !== -1) { + const [endCh, endConsumed] = parseTrSetAtom(spec, next + 1); + const end = next + endConsumed + 1; + if (spec[end] === "]") { + const chars = []; + pushTrRange(chars, ch, endCh); + return { chars, end }; + } + } + return null; +} + +export function parseTrSetAtom(spec, index) { + if (spec[index] === "\\" && index + 1 < spec.length) { + const [ch, consumed] = parseTrEscape(spec, index + 1); + return [ch, consumed + 1]; + } + return [spec[index], 1]; +} + +export function trSetHasCharClass(spec) { + return /(?:\[:[A-Za-z]+:\]|\[\[:[A-Za-z]+:\]\])/.test(String(spec)); +} + +export function trSetEndsWithCharClass(spec) { + return /(?:\[:[A-Za-z]+:\]|\[\[:[A-Za-z]+:\]\])$/.test(String(spec)); +} + +export function trSetIsHomogeneous(chars) { + return chars.every((ch) => ch === chars[0]); +} + +export function validateTrCaseClassAlignment(spec1, spec2) { + const spans1 = trCaseClassSpans(spec1); + const spans2 = trCaseClassSpans(spec2); + if (!spans2.length) return; + for (const span of spans2) { + if (!spans1.some((candidate) => candidate.start === span.start && candidate.length === span.length)) { + throw new UsageError("misaligned [:upper:] and/or [:lower:] construct"); + } + } +} + +export function trCaseClassSpans(spec) { + const spans = []; + const text = String(spec); + const pattern = /\[\[:(upper|lower):\]\]|\[:(upper|lower):\]/g; + let match; + while ((match = pattern.exec(text))) { + const name = match[1] ?? match[2]; + spans.push({ start: expandSet(text.slice(0, match.index)).length, length: trClass(name).length }); + } + return spans; +} + +export function pushTrRange(out, startCh, endCh) { + const start = startCh.charCodeAt(0); + const end = endCh.charCodeAt(0); + if (start > end) throw new UsageError(`range-endpoints of '${startCh}-${endCh}' are in reverse collating sequence order`); + for (let c = start; c <= end; c++) out.push(String.fromCharCode(c)); +} + +export function parseTrRepeatCount(text) { + let count; + if (/^0[0-9]/.test(text)) { + if (!/^[0-7]+$/.test(text)) throw new UsageError(`invalid repeat count ${trRepeatCountDiagnostic(text)} in [c*n] construct`); + count = Number.parseInt(text, 8); + } else { + count = Number(text); + } + if (!Number.isSafeInteger(count) || count < 0) throw new UsageError(`invalid repeat count ${trRepeatCountDiagnostic(text)} in [c*n] construct`); + return count; +} + +export function trRepeatCountDiagnostic(text) { + return localeQuotedDiagnostic(lsEscapedName(text, { escapeDouble: false }).replaceAll("\\", "\\\\")); +} + +export function parseTrEscape(spec, index) { + const simple = { n: "\n", r: "\r", t: "\t", b: "\b", f: "\f", v: "\v", "\\": "\\" }; + const ch = spec[index]; + if (simple[ch] != null) return [simple[ch], 1]; + const oct = spec.slice(index).match(/^[0-7]{1,3}/)?.[0]; + if (oct) { + if (oct.length === 3 && Number.parseInt(oct, 8) > 0xff) { + const prefix = oct.slice(0, 2); + stderr(`tr: warning: the ambiguous octal escape \\${oct} is being\n\tinterpreted as the 2-byte sequence \\0${prefix}, ${oct[2]}\n`); + return [String.fromCharCode(Number.parseInt(prefix, 8)), 2]; + } + return [String.fromCharCode(Number.parseInt(oct, 8)), oct.length]; + } + return [ch, 1]; +} + +export function trClass(name) { + const lower = "abcdefghijklmnopqrstuvwxyz"; + const upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + const digit = "0123456789"; + const space = " \t\n\r\f\v"; + const punct = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; + const classes = { + alnum: lower + upper + digit, + alpha: lower + upper, + blank: " \t", + cntrl: Array.from({ length: 32 }, (_, i) => String.fromCharCode(i)).join("") + "\x7f", + digit, + graph: lower + upper + digit + punct, + lower, + print: lower + upper + digit + punct + " ", + punct, + space, + upper, + xdigit: digit + "abcdefABCDEF", + }; + return [...(classes[name] ?? "")]; +} + +export function trClassChecked(name) { + const chars = trClass(name); + if (!chars.length) throw new UsageError(`invalid character class '${name}'`); + return chars; +} + +const singleCall = defineCommand("tr", trCmd, trMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/true.js b/src/commands/true.js new file mode 100644 index 0000000..9f345b7 --- /dev/null +++ b/src/commands/true.js @@ -0,0 +1,9 @@ +#!/usr/bin/env bun + +import { booleanCommand } from "../shared/common.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +const singleCall = defineCommand("true", (args) => booleanCommand("true", args, 0), null); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/truncate.js b/src/commands/truncate.js new file mode 100644 index 0000000..984012d --- /dev/null +++ b/src/commands/truncate.js @@ -0,0 +1,202 @@ +#!/usr/bin/env bun + +import { stat, statfs, truncate, writeFile } from "node:fs/promises"; +import { dirname as pathDirname } from "node:path"; +import { cstrPath, libc, localeQuotedDiagnostic, localeQuotedEscapedDiagnostic, normalizeLongOptionByPrefix, normalizeLongOptionsByPrefix, parseOptions, pathDisplayName, shellEscapeLsName, systemErrorMessage } from "../shared/common.js"; +import { UsageError, stderr } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const TRUNCATE_LONG_OPTIONS = ["io-blocks", "no-create", "reference", "size", "help", "version"]; + +export function truncateMetaOption(args) { + const longValueOptions = new Set(["reference", "size"]); + const shortValueOptions = new Set(["r", "s"]); + const shortKnownOptions = new Set(["c", "o", "r", "s"]); + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (arg.startsWith("--")) { + const option = normalizeTruncateLongOption(arg); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (!TRUNCATE_LONG_OPTIONS.includes(name)) return null; + if ((option === "--help" || option === "--version") && inlineValue == null) return option; + if (name === "size" && inlineValue != null && !isValidTruncateSizeOperand(inlineValue)) return null; + if (inlineValue == null && longValueOptions.has(name)) { + if (name === "size" && (i + 1 >= args.length || !isValidTruncateSizeOperand(args[i + 1]))) return null; + i++; + } + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (!shortKnownOptions.has(ch)) return null; + if (shortValueOptions.has(ch)) { + const inlineValue = arg.slice(j + 1); + if (ch === "s" && inlineValue !== "" && !isValidTruncateSizeOperand(inlineValue)) return null; + if (inlineValue === "") { + if (ch === "s" && (i + 1 >= args.length || !isValidTruncateSizeOperand(args[i + 1]))) return null; + i++; + } + break; + } + } + } + return null; +} + +export async function truncateCmd(args) { + args = normalizeTruncateLongOptions(args); + const { opts, operands } = parseOptions(args, { short: { s: "value", c: false, r: "value", o: false }, long: { size: "value", "no-create": false, reference: "value", "io-blocks": false, help: false, version: false } }); + const size = opts.s ?? opts.size; + const reference = opts.r ?? opts.reference; + if (size == null && reference == null) throw new UsageError("you must specify either '--size' or '--reference'", true); + if (size != null) truncateTargetSize(0, size); + if (reference != null && size != null && !isTruncateRelativeSize(size)) { + throw new UsageError(`you must specify a relative ${localeQuotedDiagnostic("--size")} with ${localeQuotedDiagnostic("--reference")}`, true); + } + if (!operands.length) throw new UsageError("missing file operand", true); + if ((opts.o || opts["io-blocks"]) && reference != null && size == null) throw new UsageError("'--io-blocks' was specified but '--size' was not", true); + let referenceSize = null; + if (reference != null) { + try { + referenceSize = (await stat(reference)).size; + } catch (error) { + throw new UsageError(`cannot stat ${truncateQuotedName(reference)}: ${systemErrorMessage(error)}`); + } + } + let status = 0; + for (const file of operands) { + try { + let missing = false; + const current = await stat(file).then((s) => s.size, (error) => { + if (error.code === "ENOENT" && (opts.c || opts["no-create"])) return null; + if (error.code === "ENOENT") { + missing = true; + return 0; + } + throw error; + }); + if (current == null) continue; + if (missing) await writeFile(file, ""); + const base = referenceSize ?? current; + const ioBlockSize = opts.o || opts["io-blocks"] ? await truncateIoBlockSize(file) : 1; + const target = size == null ? BigInt(referenceSize) : truncateTargetSize(base, size, ioBlockSize); + if (target < 0n) throw new UsageError(`Invalid number: ${localeQuotedDiagnostic(size ?? target)}: Value too large for defined data type`); + await truncateFileToSize(file, target); + } catch (error) { + if (error instanceof UsageError) throw error; + stderr(`truncate: ${truncateErrorPrefix(error, file)}: ${truncateErrorMessage(error)}\n`); + status = 1; + } + } + return status; +} + +export function normalizeTruncateLongOptions(args) { + return normalizeLongOptionsByPrefix(args, TRUNCATE_LONG_OPTIONS); +} + +export function normalizeTruncateLongOption(arg) { + return normalizeLongOptionByPrefix(arg, TRUNCATE_LONG_OPTIONS); +} + +export function isValidTruncateSizeOperand(value) { + try { + truncateTargetSize(0, value); + return true; + } catch { + return false; + } +} + +export function isTruncateRelativeSize(spec) { + return /^[+\-<>/%]/.test(String(spec).trimStart()); +} + +export async function truncateIoBlockSize(file) { + try { + const fs = await statfs(file); + return fs.bsize || 512; + } catch (error) { + if (error?.code !== "ENOENT") throw error; + const dir = pathDirname(file) || "."; + const fs = await statfs(dir); + return fs.bsize || 512; + } +} + +export function truncateTargetSize(current, spec, blockScale = 1) { + current = BigInt(current); + const text = String(spec).trimStart(); + const op = /^[+\-<>/%]/.test(text) ? text[0] : ""; + const amount = parseGNUSizeCheckedNumber(op ? text.slice(1) : text, BigInt(blockScale), text); + if (op === "+") return current + amount; + if (op === "-") return current > amount ? current - amount : 0n; + if (op === "<") return current < amount ? current : amount; + if (op === ">") return current > amount ? current : amount; + if ((op === "/" || op === "%") && amount === 0n) throw new UsageError(`division by zero`); + if (op === "/") return current - (current % amount); + if (op === "%") return current + ((amount - (current % amount)) % amount); + return amount; +} + +export async function truncateFileToSize(file, size) { + if (size <= BigInt(Number.MAX_SAFE_INTEGER)) { + await truncate(file, Number(size)); + return; + } + if (libc.symbols.truncate(cstrPath(file), size) !== 0) { + throw new Error("Invalid argument"); + } +} + +export function parseGNUSizeCheckedNumber(value, extraScale = 1n, original = value) { + const match = String(value).match(/^(\d+)([A-Za-z]*)$/); + if (!match) throw new UsageError(`Invalid number: ${localeQuotedEscapedDiagnostic(original)}`); + const suffixScales = { + "": 1n, K: 1024n, k: 1024n, KiB: 1024n, kiB: 1024n, + M: 1024n ** 2n, m: 1024n ** 2n, MiB: 1024n ** 2n, miB: 1024n ** 2n, + G: 1024n ** 3n, g: 1024n ** 3n, GiB: 1024n ** 3n, giB: 1024n ** 3n, + T: 1024n ** 4n, t: 1024n ** 4n, TiB: 1024n ** 4n, tiB: 1024n ** 4n, + P: 1024n ** 5n, PiB: 1024n ** 5n, + E: 1024n ** 6n, EiB: 1024n ** 6n, + Z: 1024n ** 7n, ZiB: 1024n ** 7n, + Y: 1024n ** 8n, YiB: 1024n ** 8n, + R: 1024n ** 9n, RiB: 1024n ** 9n, + Q: 1024n ** 10n, QiB: 1024n ** 10n, + KB: 1000n, kB: 1000n, MB: 1000n ** 2n, mB: 1000n ** 2n, + GB: 1000n ** 3n, gB: 1000n ** 3n, TB: 1000n ** 4n, tB: 1000n ** 4n, + PB: 1000n ** 5n, EB: 1000n ** 6n, ZB: 1000n ** 7n, YB: 1000n ** 8n, + RB: 1000n ** 9n, QB: 1000n ** 10n, + }; + const scale = suffixScales[match[2]]; + if (!scale) throw new UsageError(`Invalid number: ${localeQuotedEscapedDiagnostic(original)}`); + const amount = BigInt(match[1]) * scale * extraScale; + if (amount > 9223372036854775807n) { + throw new UsageError(`Invalid number: ${localeQuotedEscapedDiagnostic(original)}: Value too large for defined data type`); + } + return amount; +} + +export function truncateErrorPrefix(error, file) { + if ((error?.code === "ENOENT" && error?.syscall === "open") || error?.code === "ENOTDIR" || error?.code === "EISDIR") return `cannot open ${truncateQuotedName(file)} for writing`; + return `failed to truncate ${truncateQuotedName(file)}`; +} + +export function truncateQuotedName(path) { + return shellEscapeLsName(pathDisplayName(path), true); +} + +export function truncateErrorMessage(error) { + if (error?.code === "ENOENT") return "No such file or directory"; + if (error?.code === "EISDIR") return "Is a directory"; + if (error?.code === "ENOTDIR") return "Not a directory"; + if (error?.code === "EINVAL") return "Invalid argument"; + return error?.message || String(error); +} + +const singleCall = defineCommand("truncate", truncateCmd, truncateMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/tsort.js b/src/commands/tsort.js new file mode 100644 index 0000000..3c949ba --- /dev/null +++ b/src/commands/tsort.js @@ -0,0 +1,132 @@ +#!/usr/bin/env bun + +import { localeQuotedEscapedDiagnostic, nodeErrorMessage, normalizeHelpVersionOnlyLongOption, normalizeHelpVersionOnlyLongOptions, parseOptions, readAll, systemErrorMessage, textInputDiagnosticName } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export function tsortMetaOption(args) { + for (const arg of args) { + if (arg === "--") return null; + if (arg.startsWith("--")) { + const option = normalizeHelpVersionOnlyLongOption(arg); + if (option.includes("=") || !["--help", "--version"].includes(option)) return null; + return option; + } + if (!arg.startsWith("-") || arg === "-") continue; + for (let j = 1; j < arg.length; j++) if (arg[j] !== "w") return null; + } + return null; +} + +export async function tsort(args) { + const { operands } = parseOptions(normalizeHelpVersionOnlyLongOptions(args), { short: { w: false }, long: { help: false, version: false } }); + if (operands.length > 1) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[1])}`, true); + const file = operands[0] ?? "-"; + let input; + try { + input = await readAll(file); + } catch (error) { + const message = error?.code === "EISDIR" ? `${textInputDiagnosticName(file)}: read error: ${systemErrorMessage(error)}` : file === "-" ? nodeErrorMessage(error) : `${textInputDiagnosticName(file)}: ${systemErrorMessage(error)}`; + stderr(`tsort: ${message}\n`); + return 1; + } + const text = new TextDecoder().decode(input); + const tokens = text.trim().split(/\s+/).filter(Boolean); + if (tokens.length % 2) throw new UsageError(`${file}: input contains an odd number of tokens`); + const nodes = new Map(); + const getNode = (name) => { + if (!nodes.has(name)) nodes.set(name, { name, count: 0, printed: false, qlink: null, succ: [] }); + return nodes.get(name); + }; + for (let i = 0; i < tokens.length; i += 2) { + const from = getNode(tokens[i]); + const to = getNode(tokens[i + 1]); + if (from !== to) { + to.count++; + from.succ.unshift(to); + } + } + const sortedNodes = [...nodes.values()].sort((a, b) => a.name.localeCompare(b.name)); + let remaining = sortedNodes.length; + let ok = true; + while (remaining > 0) { + const queue = tsortScanZeros(sortedNodes); + for (let head = queue.head, tail = queue.tail; head;) { + const node = head; + stdout(`${node.name}\n`); + node.printed = true; + remaining--; + for (const next of node.succ) { + next.count--; + if (next.count === 0) { + tail.qlink = next; + tail = next; + } + } + head = node.qlink; + node.qlink = null; + } + if (remaining > 0) { + stderr(`tsort: ${file}: input contains a loop:\n`); + ok = false; + tsortBreakLoop(sortedNodes); + } + } + return ok ? 0 : 1; +} + +export function tsortScanZeros(nodes) { + let head = null; + let tail = null; + for (const node of nodes) { + if (node.count === 0 && !node.printed) { + if (!head) head = node; + else tail.qlink = node; + tail = node; + node.qlink = null; + } + } + return { head, tail }; +} + +export function tsortBreakLoop(nodes) { + let loop = null; + do { + for (const node of nodes) { + if (node.count <= 0 || node.printed) continue; + if (!loop) { + loop = node; + continue; + } + const idx = node.succ.findIndex((next) => next === loop); + if (idx === -1) continue; + if (!node.qlink) { + node.qlink = loop; + loop = node; + continue; + } + while (loop) { + stderr(`tsort: ${loop.name}\n`); + if (loop === node) { + node.succ[idx].count--; + node.succ.splice(idx, 1); + break; + } + const next = loop.qlink; + loop.qlink = null; + loop = next; + } + while (loop) { + const next = loop.qlink; + loop.qlink = null; + loop = next; + } + return; + } + } while (loop); +} + +const singleCall = defineCommand("tsort", tsort, tsortMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/tty.js b/src/commands/tty.js new file mode 100644 index 0000000..6df436c --- /dev/null +++ b/src/commands/tty.js @@ -0,0 +1,68 @@ +#!/usr/bin/env bun + +import { localeQuotedEscapedDiagnostic, parseOptions } from "../shared/common.js"; +import { UsageError, fail, stdout } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const TTY_LONG_OPTIONS = ["silent", "quiet", "help", "version"]; + +export function ttyMetaOption(args) { + for (const arg of args) { + if (arg === "--") return null; + if (!arg.startsWith("-") || arg === "-") continue; + if (!arg.startsWith("--")) { + for (const ch of arg.slice(1)) { + if (ch !== "s") return null; + } + continue; + } + const option = normalizeTtyLongOption(arg); + if (option.includes("=") || !TTY_LONG_OPTIONS.includes(option.slice(2))) return null; + if (option === "--help" || option === "--version") return option; + } + return null; +} + +export async function ttyCmd(args) { + args = normalizeTtyLongOptions(args); + const { opts, operands } = parseOptions(args, { short: { s: false }, long: { silent: false, quiet: false, help: false, version: false } }); + if (operands.length) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[0])}`, true); + const isTty = Boolean(process.stdin.isTTY); + if (!(opts.s || opts.silent || opts.quiet)) { + try { + stdout(isTty ? "/dev/tty\n" : "not a tty\n"); + } catch (error) { + return fail("tty", error.message || String(error), 3); + } + } + return isTty ? 0 : 1; +} + +export function normalizeTtyLongOptions(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(normalizeTtyLongOption(arg)); + } + return out; +} + +export function normalizeTtyLongOption(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 = TTY_LONG_OPTIONS.find((option) => option.startsWith(name)); + if (!match) return arg; + return eq === -1 ? `--${match}` : `--${match}=${body.slice(eq + 1)}`; +} + +const singleCall = defineCommand("tty", ttyCmd, ttyMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/uname.js b/src/commands/uname.js new file mode 100644 index 0000000..f87241e --- /dev/null +++ b/src/commands/uname.js @@ -0,0 +1,113 @@ +#!/usr/bin/env bun + +import { readFileSync } from "node:fs"; +import { hostname as osHostname, release as osRelease, type as osType, version as osVersion } from "node:os"; +import { localeQuotedEscapedDiagnostic, machineName, parseOptions } from "../shared/common.js"; +import { UsageError, stdout } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const UNAME_LONG_OPTIONS = ["all", "kernel-name", "nodename", "kernel-release", "kernel-version", "machine", "processor", "hardware-platform", "operating-system", "help", "version"]; + +export function unameMetaOption(args) { + const shortKnownOptions = new Set(["a", "s", "n", "r", "v", "m", "p", "i", "o"]); + for (const arg of args) { + if (arg === "--") return null; + if (!arg.startsWith("-") || arg === "-") continue; + if (!arg.startsWith("--")) { + for (const ch of arg.slice(1)) { + if (!shortKnownOptions.has(ch)) return null; + } + continue; + } + if (arg.includes("=")) return null; + const option = normalizeUnameLongOption(arg); + if (option === "--help" || option === "--version") return option; + if (option === arg && !UNAME_LONG_OPTIONS.includes(arg.slice(2))) return null; + } + return null; +} + +export async function unameCmd(args) { + args = normalizeUnameLongOptions(args); + const { opts, operands } = parseOptions(args, { + short: { a: false, s: false, n: false, r: false, v: false, m: false, p: false, i: false, o: false }, + long: { all: false, "kernel-name": false, nodename: false, "kernel-release": false, "kernel-version": false, machine: false, processor: false, "hardware-platform": false, "operating-system": false, help: false, version: false }, + }); + if (operands.length) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[0])}`, true); + const all = opts.a || opts.all; + const fields = []; + const selected = all || opts.s || opts["kernel-name"] || opts.n || opts.nodename || opts.r || opts["kernel-release"] || opts.v || opts["kernel-version"] || opts.m || opts.machine || opts.p || opts.processor || opts.i || opts["hardware-platform"] || opts.o || opts["operating-system"]; + if (!selected || all || opts.s || opts["kernel-name"]) fields.push(osType()); + if (all || opts.n || opts.nodename) fields.push(osHostname()); + if (all || opts.r || opts["kernel-release"]) fields.push(osRelease()); + if (all || opts.v || opts["kernel-version"]) fields.push(osVersion()); + if (all || opts.m || opts.machine) fields.push(machineName()); + if (all || opts.p || opts.processor) fields.push(unameProcessorName()); + if (all || opts.i || opts["hardware-platform"]) fields.push(unameHardwarePlatform()); + if (all || opts.o || opts["operating-system"]) fields.push(process.platform === "linux" ? "GNU/Linux" : process.platform); + stdout(`${fields.join(" ")}\n`); + return 0; +} + +export function normalizeUnameLongOptions(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(normalizeUnameLongOption(arg)); + } + return out; +} + +export function normalizeUnameLongOption(arg) { + const body = arg.slice(2); + const eq = body.indexOf("="); + const name = eq === -1 ? body : body.slice(0, eq); + if (!name) return arg; + const matches = UNAME_LONG_OPTIONS.filter((option) => option.startsWith(name)); + if (matches.length === 0) return arg; + if (matches.length > 1) { + throw new UsageError(`option '--${eq === -1 ? name : body}' is ambiguous; possibilities: ${matches.map((option) => `'--${option}'`).join(" ")}`, true); + } + return eq === -1 ? `--${matches[0]}` : `--${matches[0]}=${body.slice(eq + 1)}`; +} + +export function unameProcessorName() { + if (process.platform === "linux") { + const value = procCpuInfoField("model name") ?? procCpuInfoField("Processor"); + if (value) return value; + } + return process.arch === "x64" ? "x86_64" : machineName(); +} + +export function unameHardwarePlatform() { + if (process.platform === "linux") { + const value = procCpuInfoField("vendor_id") ?? procCpuInfoField("Hardware"); + if (value) return value; + } + return machineName(); +} + +export function procCpuInfoField(field) { + try { + const text = readFileSync("/proc/cpuinfo", "utf8"); + const prefix = `${field.toLowerCase()}\t`; + for (const line of text.split("\n")) { + const [name, value] = line.split(":", 2); + if (name?.trim().toLowerCase() === field.toLowerCase() || name?.toLowerCase().startsWith(prefix)) { + const trimmed = value?.trim(); + if (trimmed) return trimmed; + } + } + } catch {} + return null; +} + +const singleCall = defineCommand("uname", unameCmd, unameMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/unexpand.js b/src/commands/unexpand.js new file mode 100644 index 0000000..2d2b0f2 --- /dev/null +++ b/src/commands/unexpand.js @@ -0,0 +1,195 @@ +#!/usr/bin/env bun + +import { closeSync, openSync } from "node:fs"; +import { concatBytes, enc, isWriteError, nodeErrorMessage, parseOptions, readFdChunkViews, systemErrorMessage } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { UNEXPAND_LONG_OPTIONS, combinedTabSpec, nextTabColumn, normalizeTabArgs, normalizeTabCommandLongOptions, parseTabStops, tabCommandMetaOption } from "../shared/tabs.js"; +import { nextUtf8Token, tabFoldDiagnosticName } from "../shared/text.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export async function unexpand(args) { + const obsoleteTabs = args.some((arg) => /^-\d[\d,\s]*$/.test(arg)); + args = normalizeTabArgs(normalizeTabCommandLongOptions(args, UNEXPAND_LONG_OPTIONS)); + const { opts, operands } = parseOptions(args, { short: { t: "value", a: false }, long: { tabs: "value", all: false, "first-only": false, help: false, version: false } }); + let stops; + try { + stops = parseTabStops(combinedTabSpec(opts.tabs ?? opts.tab, opts.t), "unexpand"); + } catch (error) { + if (obsoleteTabs && error instanceof UsageError && error.message.startsWith("tab stop is too large")) throw new UsageError("tab stop value is too large"); + throw error; + } + const hasExplicitTabs = opts.t != null || opts.tab != null || opts.tabs != null; + const convertAll = Boolean(opts.a || opts.all || (hasExplicitTabs && !opts["first-only"] && !obsoleteTabs)); + const files = operands.length ? operands : ["-"]; + let failed = false; + for (const file of files) { + let fd; + try { + fd = file === "-" ? 0 : openSync(file, "r"); + streamUnexpandFd(fd, stops, convertAll); + } catch (error) { + if (isWriteError(error)) throw error; + stderr(file === "-" ? `unexpand: ${nodeErrorMessage(error)}\n` : `unexpand: ${tabFoldDiagnosticName(file)}: ${systemErrorMessage(error)}\n`); + failed = true; + } finally { + if (fd != null && fd !== 0) closeSync(fd); + } + } + return failed ? 1 : 0; +} + +export function streamUnexpandFd(fd, stops, convertAll) { + const newline = Uint8Array.of(0x0a); + let column = 0; + let fieldStarted = false; + let blankStartColumn = 0; + let blanks = []; + let carry = Buffer.alloc(0); + const output = []; + const flushOutput = () => { + for (const piece of output) stdout(piece); + output.length = 0; + }; + const emit = (piece) => { + if (piece.length) output.push(piece); + if (output.length >= 256) flushOutput(); + }; + const flushBlanks = (atEnd) => { + if (!blanks.length) return; + const canConvert = convertAll || !fieldStarted; + emit(canConvert + ? renderUnexpandedBlankTokens(blankStartColumn, column, blanks, stops, atEnd, blanks.some((token) => isAsciiByteToken(token, 0x09)), !fieldStarted) + : concatBytes(blanks.map((token) => token.bytes))); + blanks = []; + }; + const addBlank = (token) => { + if (!blanks.length) blankStartColumn = column; + blanks.push({ ...token, bytes: Buffer.from(token.bytes) }); + column = tokenEndColumn(token, column, stops); + }; + readFdChunkViews(fd, (chunk) => { + const bytes = carry.length ? Buffer.concat([carry, chunk]) : chunk; + carry = Buffer.alloc(0); + let runStart = -1; + const endRun = (end) => { + if (runStart !== -1) emit(bytes.subarray(runStart, end)); + runStart = -1; + }; + let index = 0; + while (index < bytes.length) { + const byte = bytes[index]; + if (byte < 0x80) { + if (byte === 0x0a) { + endRun(index); + flushBlanks(true); + emit(newline); + column = 0; + fieldStarted = false; + } else if (byte === 0x20 || byte === 0x09) { + endRun(index); + addBlank({ bytes: bytes.subarray(index, index + 1), char: String.fromCharCode(byte), width: byte === 0x09 ? 0 : 1 }); + } else { + if (runStart === -1) { + flushBlanks(false); + runStart = index; + } + column = byte === 0x08 ? Math.max(0, column - 1) : column + 1; + fieldStarted = true; + } + index++; + continue; + } + const length = byte >= 0xc2 && byte <= 0xdf ? 2 : byte >= 0xe0 && byte <= 0xef ? 3 : byte >= 0xf0 && byte <= 0xf4 ? 4 : 1; + if (length > 1 && index + length > bytes.length) { + endRun(index); + carry = Buffer.from(bytes.subarray(index)); + break; + } + endRun(index); + const token = nextUtf8Token(bytes, index); + if (isUnexpandBlank(token)) addBlank(token); + else { + flushBlanks(false); + emit(token.bytes); + column = tokenEndColumn(token, column, stops); + fieldStarted = true; + } + index = token.next; + } + endRun(bytes.length); + flushOutput(); + }); + if (carry.length) { + flushBlanks(false); + emit(carry); + fieldStarted = true; + } + flushBlanks(true); + flushOutput(); +} + +export function isUnexpandBlank(token) { + return isAsciiByteToken(token, 0x20) || isAsciiByteToken(token, 0x09) || (token.char != null && /\p{Zs}/u.test(token.char)); +} + +export function isAsciiByteToken(token, byte) { + return token.bytes.length === 1 && token.bytes[0] === byte; +} + +export function tokenEndColumn(token, column, stops) { + if (isAsciiByteToken(token, 0x09)) return nextTabColumn(column, stops); + if (isAsciiByteToken(token, 0x08)) return Math.max(0, column - 1); + return column + token.width; +} + +export function renderUnexpandedBlankTokens(startColumn, endColumn, run, stops, atEnd, containsTab, atFieldStart = false) { + const original = concatBytes(run.map((token) => token.bytes)); + const originalWidth = endColumn - startColumn; + const rendered = containsTab + ? renderUnexpandedBlankTokenTabs(startColumn, run, stops, atEnd, atFieldStart) + : renderUnexpandedSpaces(startColumn, endColumn, stops, atEnd, originalWidth, false, atFieldStart); + if (!rendered.includes("\t") && run.some((token) => token.char != null && !isAsciiByteToken(token, 0x20))) return original; + return enc.encode(rendered); +} + +export function renderUnexpandedBlankTokenTabs(startColumn, run, stops, atEnd, atFieldStart = false) { + const lastTab = run.findLastIndex((token) => isAsciiByteToken(token, 0x09)); + const prefix = run.slice(0, lastTab + 1); + const suffix = run.slice(lastTab + 1); + const prefixEnd = blankTokenRunEndColumn(startColumn, prefix, stops); + const suffixEnd = blankTokenRunEndColumn(prefixEnd, suffix, stops); + return renderUnexpandedSpaces(startColumn, prefixEnd, stops, true, prefixEnd - startColumn, true, atFieldStart) + + renderUnexpandedSpaces(prefixEnd, suffixEnd, stops, atEnd, suffixEnd - prefixEnd, false, atFieldStart); +} + +export function blankTokenRunEndColumn(startColumn, run, stops) { + let column = startColumn; + for (const token of run) column = tokenEndColumn(token, column, stops); + return column; +} + +export function renderUnexpandedSpaces(startColumn, endColumn, stops, atEnd, originalLength, fromTabs = false, atFieldStart = false) { + let column = startColumn; + let out = ""; + while (column < endColumn) { + const next = nextTabColumn(column, stops); + const remaining = endColumn - column; + const width = next - column; + const hasExplicitStop = stops.interval || stops.length === 1 || stops.some((stop) => stop > column && stop <= endColumn) || (fromTabs && width === 1); + const finalSingleBeforeText = !atEnd && width === remaining && width === 1 && originalLength <= 1 && (!atFieldStart || stops[0] !== 1); + const finalSingleAtEnd = atEnd && !fromTabs && width === remaining && width === 1 && originalLength <= 1; + if (hasExplicitStop && width <= remaining && !finalSingleBeforeText && !finalSingleAtEnd) { + out += "\t"; + column = next; + } else { + out += " "; + column++; + } + } + return out; +} + +const singleCall = defineCommand("unexpand", unexpand, (args) => tabCommandMetaOption("unexpand", args)); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/uniq.js b/src/commands/uniq.js new file mode 100644 index 0000000..90be7ab --- /dev/null +++ b/src/commands/uniq.js @@ -0,0 +1,279 @@ +#!/usr/bin/env bun + +import { closeSync, openSync } from "node:fs"; +import { createFdRecordReader, decodeSurrogateEscapedBytes, isWriteError, localeQuotedDiagnostic, localeQuotedEscapedDiagnostic, nodeErrorMessage, normalizeLongOptionByPrefix, normalizeLongOptionsByPrefix, parseOptions, pathDisplayName, readAll, shellEscapeLsName, systemErrorMessage, textInputDiagnosticName } from "../shared/common.js"; +import { UsageError, encodeSurrogateEscapedString, stderr, stdout } from "../shared/diagnostics.js"; +import { writeAll } from "../shared/text.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const UNIQ_LONG_OPTIONS = ["count", "repeated", "all-repeated", "skip-fields", "group", "ignore-case", "skip-chars", "unique", "zero-terminated", "check-chars", "help", "version"]; + +export function uniqMetaOption(args) { + const longValueOptions = new Set(["skip-fields", "skip-chars", "check-chars"]); + const longOptionalValueOptions = new Set(["all-repeated", "group"]); + const shortValueOptions = new Set(["f", "s", "w"]); + const shortKnownOptions = new Set(["c", "d", "D", "f", "i", "s", "u", "w", "z"]); + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (arg.startsWith("--")) { + const option = normalizeUniqLongOption(arg); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (!UNIQ_LONG_OPTIONS.includes(name)) return null; + if (name === "all-repeated" && inlineValue != null && !["none", "prepend", "separate"].includes(inlineValue)) return null; + if (name === "group" && inlineValue != null && !["prepend", "append", "separate", "both"].includes(inlineValue)) return null; + if ((option === "--help" || option === "--version") && inlineValue == null) return option; + if (longValueOptions.has(name)) { + const value = inlineValue ?? args[i + 1]; + if (value !== undefined) validateUniqMetaOptionValue(name, value); + if (inlineValue == null) i++; + } + else if (inlineValue == null && longOptionalValueOptions.has(name)) continue; + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + if (/^-\d+$/.test(arg) || /^\+\d+$/.test(arg)) continue; + for (let j = 1; j < arg.length; j++) { + const ch = arg[j]; + if (!shortKnownOptions.has(ch)) return null; + if (shortValueOptions.has(ch)) { + const inlineValue = arg.slice(j + 1); + const value = inlineValue === "" ? args[i + 1] : inlineValue; + if (value !== undefined) validateUniqMetaOptionValue(ch, value); + if (inlineValue === "") i++; + break; + } + } + } + return null; +} + +export function validateUniqMetaOptionValue(option, value) { + if (option === "skip-fields" || option === "f") validateUniqNumericOption(value, "fields to skip"); + else if (option === "skip-chars" || option === "s") validateUniqNumericOption(value, "bytes to skip"); + else if (option === "check-chars" || option === "w") validateUniqNumericOption(value, "bytes to compare"); +} + +export async function uniq(args) { + args = normalizeUniqArgs(normalizeUniqLongOptions(args)); + const { opts, operands } = parseOptions(args, { + short: { c: false, d: false, D: false, f: "value", i: false, s: "value", u: false, w: "value", z: false }, + long: { count: false, repeated: false, "all-repeated": "optional-value", "skip-fields": "value", group: "optional-value", "ignore-case": false, "skip-chars": "value", unique: false, "check-chars": "value", "zero-terminated": false, help: false, version: false }, + }); + const count = opts.c || opts.count; + const repeated = opts.d || opts.repeated; + const unique = opts.u || opts.unique; + const allRepeated = opts.D || opts["all-repeated"]; + const group = opts.group; + if (allRepeated && count) throw new UsageError("printing all duplicated lines and repeat counts is meaningless", true); + if (opts["all-repeated"] != null && opts["all-repeated"] !== true) validateUniqMethod("all-repeated", opts["all-repeated"], ["none", "prepend", "separate"]); + if (opts.group != null && opts.group !== true) validateUniqMethod("group", opts.group, ["prepend", "append", "separate", "both"]); + if (group && (count || repeated || allRepeated || unique)) throw new UsageError("--group is mutually exclusive with -c/-d/-D/-u", true); + validateUniqNumericOptions(opts); + const inputFile = operands[0] ?? "-"; + const outputFile = operands[1]; + if (operands.length > 2) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[2])}`, true); + if (!outputFile && canStreamUniqFile(opts)) return streamUniqFile(inputFile, opts); + let inputBytes; + try { + inputBytes = await readAll(inputFile); + } catch (error) { + const inputName = textInputDiagnosticName(inputFile); + const message = error?.code === "EISDIR" ? `error reading ${shellEscapeLsName(pathDisplayName(inputFile), true)}: ${systemErrorMessage(error)}` : `${inputName}: ${systemErrorMessage(error)}`; + stderr(inputFile === "-" ? `uniq: ${nodeErrorMessage(error)}\n` : `uniq: ${message}\n`); + return 1; + } + const text = decodeSurrogateEscapedBytes(inputBytes); + const sep = opts.z || opts["zero-terminated"] ? "\0" : "\n"; + const lines = text.endsWith(sep) ? text.slice(0, -sep.length).split(sep) : text === "" ? [] : text.split(sep); + if (repeated && unique) { + if (outputFile) await writeAll(outputFile, ""); + return 0; + } + const rows = []; + for (const line of lines) { + const key = uniqKey(line, opts); + const last = rows.at(-1); + if (last?.key === key) { + last.count++; + last.lines.push(line); + } else { + rows.push({ key, line, lines: [line], count: 1 }); + } + } + const outLines = []; + const allRepeatedMethod = opts["all-repeated"] === true || opts.D ? "none" : opts["all-repeated"]; + const groupMethod = opts.group === true ? "separate" : opts.group; + for (const row of rows) { + if (opts.group) { + pushUniqGroup(outLines, row.lines, groupMethod, opts, row.count, ["separate", "prepend", "append", "both"]); + } else if (allRepeated) { + if (row.count > 1) pushUniqGroup(outLines, row.lines, allRepeatedMethod, opts, row.count, ["none", "prepend", "separate"]); + } else if (repeated) { + if (row.count > 1) outLines.push(formatUniqLine(row.line, row.count, opts)); + } else if (unique) { + if (row.count === 1) outLines.push(formatUniqLine(row.line, row.count, opts)); + } else { + outLines.push(formatUniqLine(row.line, row.count, opts)); + } + } + const out = outLines.length ? outLines.join(sep) + sep : ""; + if (outputFile) { + try { + await writeAll(outputFile, encodeSurrogateEscapedString(out)); + } catch (error) { + stderr(`uniq: ${textInputDiagnosticName(outputFile)}: ${systemErrorMessage(error)}\n`); + return 1; + } + } else { + stdout(out); + } + return 0; +} + +export function canStreamUniqFile(opts) { + if (canStreamUniq(opts)) return true; + const allRepeatedMethod = opts["all-repeated"] === true || opts.D ? "none" : opts["all-repeated"]; + return allRepeatedMethod === "none" + && !(opts.c || opts.count || opts.d || opts.repeated || opts.u || opts.unique || opts.group); +} + +export function streamUniqFile(file, opts) { + const separator = opts.z || opts["zero-terminated"] ? 0 : 10; + const sep = Uint8Array.of(separator); + const allRepeated = Boolean(opts.D || opts["all-repeated"]); + let fd; + try { + fd = file === "-" ? 0 : openSync(file, "r"); + const reader = createFdRecordReader(fd, separator); + let current = null; + let count = 0; + while (true) { + const record = reader.next(); + if (record == null) break; + const line = decodeSurrogateEscapedBytes(record); + const key = uniqKey(line, opts); + if (current?.key === key) { + count++; + if (allRepeated) { + if (count === 2) { + stdout(current.record); + stdout(sep); + } + stdout(record); + stdout(sep); + } + } else { + current = { key, record: Buffer.from(record) }; + count = 1; + if (!allRepeated) { + stdout(record); + stdout(sep); + } + } + } + return 0; + } catch (error) { + if (isWriteError(error)) throw error; + const message = error?.code === "EISDIR" ? `error reading ${shellEscapeLsName(pathDisplayName(file), true)}: ${systemErrorMessage(error)}` : `${textInputDiagnosticName(file)}: ${systemErrorMessage(error)}`; + stderr(file === "-" ? `uniq: ${nodeErrorMessage(error)}\n` : `uniq: ${message}\n`); + return 1; + } finally { + if (fd != null && fd !== 0) closeSync(fd); + } +} + +export function canStreamUniq(opts) { + return !(opts.c || opts.count || opts.d || opts.repeated || opts.u || opts.unique || opts.D || opts["all-repeated"] || opts.group); +} + +export function normalizeUniqArgs(args) { + const out = []; + let scanning = true; + let valueNext = false; + for (const arg of args) { + if (!scanning) { + out.push(arg); + } else if (valueNext) { + out.push(arg); + valueNext = false; + } else if (arg === "-f" || arg === "--skip-fields" || arg === "-s" || arg === "--skip-chars" || arg === "-w" || arg === "--check-chars") { + out.push(arg); + valueNext = true; + } else if (/^-\d+$/.test(arg)) { + out.push("-f", arg.slice(1)); + } else if (/^\+\d+$/.test(arg)) { + out.push("-s", arg.slice(1)); + } else { + if (arg === "--") scanning = false; + else if (!arg.startsWith("-")) scanning = false; + out.push(arg); + } + } + return out; +} + +export function normalizeUniqLongOptions(args) { + return normalizeLongOptionsByPrefix(args, UNIQ_LONG_OPTIONS); +} + +export function normalizeUniqLongOption(arg) { + return normalizeLongOptionByPrefix(arg, UNIQ_LONG_OPTIONS); +} + +export function validateUniqNumericOptions(opts) { + validateUniqNumericOption(opts.f ?? opts["skip-fields"], "fields to skip"); + validateUniqNumericOption(opts.s ?? opts["skip-chars"], "bytes to skip"); + validateUniqNumericOption(opts.w ?? opts["check-chars"], "bytes to compare"); +} + +export function validateUniqNumericOption(value, description) { + if (value == null) return; + if (/^\+?\d+$/.test(String(value))) return; + throw new UsageError(`${value}: invalid number of ${description}`); +} + +export function validateUniqMethod(option, method, allowed) { + if (allowed.includes(method)) return; + const kind = method === "" ? "ambiguous" : "invalid"; + throw new UsageError(`${kind} argument ${localeQuotedEscapedDiagnostic(method)} for ${localeQuotedDiagnostic(`--${option}`)}\nValid arguments are:\n${allowed.map((value) => ` - ${localeQuotedDiagnostic(value)}`).join("\n")}`, true); +} + +export function pushUniqGroup(outLines, lines, method, opts, count, allowed) { + if (!allowed.includes(method)) throw new UsageError(`invalid group method: ${method}`); + if (method === "prepend" || (method === "both" && outLines.length === 0)) outLines.push(""); + else if (method === "separate" && outLines.length) outLines.push(""); + for (const line of lines) outLines.push(formatUniqLine(line, count, opts)); + if (method === "append" || method === "both") outLines.push(""); +} + +export function formatUniqLine(line, count, opts) { + return `${opts.c || opts.count ? String(count).padStart(7) + " " : ""}${line}`; +} + +export function uniqKey(line, opts) { + let key = line; + const skipFields = Number(opts.f ?? opts["skip-fields"] ?? 0); + const skipChars = Number(opts.s ?? opts["skip-chars"] ?? 0); + const width = opts.w ?? opts["check-chars"]; + if (!Number.isInteger(skipFields) || skipFields < 0) throw new UsageError(`invalid field skip: ${opts.f ?? opts["skip-fields"]}`); + if (!Number.isInteger(skipChars) || skipChars < 0) throw new UsageError(`invalid character skip: ${opts.s ?? opts["skip-chars"]}`); + let offset = 0; + for (let field = 0; field < skipFields && offset < key.length; field++) { + while (offset < key.length && /\s/u.test(key[offset])) offset++; + while (offset < key.length && !/\s/u.test(key[offset])) offset++; + } + key = [...key.slice(offset)].slice(skipChars).join(""); + if (width !== undefined) { + const max = Number(width); + if (!Number.isInteger(max) || max < 0) throw new UsageError(`invalid compare width: ${width}`); + key = [...key].slice(0, max).join(""); + } + if (opts.i || opts["ignore-case"]) key = key.toLowerCase(); + return key; +} + +const singleCall = defineCommand("uniq", uniq, uniqMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/unlink.js b/src/commands/unlink.js new file mode 100644 index 0000000..9474101 --- /dev/null +++ b/src/commands/unlink.js @@ -0,0 +1,25 @@ +#!/usr/bin/env bun + +import { unlink as fsUnlink } from "node:fs/promises"; +import { helpVersionOnlyMetaOption, localeQuotedEscapedDiagnostic, normalizeHelpVersionOnlyLongOptions, parseOptions, systemErrorMessage } from "../shared/common.js"; +import { UsageError, fail } from "../shared/diagnostics.js"; +import { linkQuotedName } from "../shared/filesystem.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export async function unlinkCmd(args) { + args = normalizeHelpVersionOnlyLongOptions(args); + const { operands } = parseOptions(args, { long: { help: false, version: false } }); + if (!operands.length) throw new UsageError("missing operand", true); + if (operands.length > 1) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[1])}`, true); + try { + await fsUnlink(operands[0]); + } catch (error) { + return fail("unlink", `cannot unlink ${linkQuotedName(operands[0])}: ${systemErrorMessage(error)}`); + } + return 0; +} + +const singleCall = defineCommand("unlink", unlinkCmd, helpVersionOnlyMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/uptime.js b/src/commands/uptime.js new file mode 100644 index 0000000..825a3ce --- /dev/null +++ b/src/commands/uptime.js @@ -0,0 +1,115 @@ +#!/usr/bin/env bun + +import { readFileSync } from "node:fs"; +import { readFile } from "node:fs/promises"; +import { loadavg } from "node:os"; +import { localeQuotedEscapedDiagnostic, parseOptions, pathDisplayName, shellEscapeLsName, systemErrorMessage } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { filterWhoUsers, parseUtmpRecords, whoBootRecord } from "../shared/system.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const UPTIME_LONG_OPTIONS = ["help", "version"]; + +export function uptimeMetaOption(args) { + for (const arg of args) { + if (arg === "--") return null; + if (!arg.startsWith("-") || arg === "-") continue; + if (arg.startsWith("--")) { + const option = normalizeUptimeLongOption(arg); + const name = option.slice(2).split("=", 1)[0]; + if (!UPTIME_LONG_OPTIONS.includes(name) || option.includes("=")) return null; + if (option === "--help" || option === "--version") return option; + continue; + } + return null; + } + return null; +} + +export async function uptimeCmd(args) { + args = normalizeUptimeLongOptions(args); + const { operands } = parseOptions(args, { long: { help: false, version: false } }); + if (operands.length > 1) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[1])}`, true); + let records = []; + let readStatus = 0; + const file = operands[0] ?? "/var/run/utmp"; + try { + records = await readUtmpRecordsStrict(file); + } catch (error) { + stderr(`uptime: ${shellEscapeLsName(pathDisplayName(file), true)}: ${systemErrorMessage(error)}\n`); + readStatus = 1; + } + const users = await filterWhoUsers(records, operands.length === 0); + const bootTime = uptimeBootTime(records, operands.length === 0); + const nowSeconds = Math.trunc(Date.now() / 1000); + let status = readStatus; + let uptimeText; + if (bootTime == null || bootTime <= 0 || bootTime > nowSeconds) { + stderr("uptime: couldn't get boot time\n"); + status = 1; + } else { + uptimeText = formatUptimeDefault(nowSeconds - bootTime); + } + const loads = loadavg().map((value) => value.toFixed(2)); + const now = new Date(); + stdout(` ${formatUptimeClock(now)} ${uptimeText ?? "up ???? days ??:??"}, ${users.length} user${users.length === 1 ? "" : "s"}, load average: ${loads.join(", ")}\n`); + return status; +} + +export function normalizeUptimeLongOptions(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(normalizeUptimeLongOption(arg)); + } + return out; +} + +export function normalizeUptimeLongOption(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 = UPTIME_LONG_OPTIONS.find((option) => option.startsWith(name)); + if (!match) return arg; + return eq === -1 ? `--${match}` : `--${match}=${body.slice(eq + 1)}`; +} + +export function formatUptimeClock(date) { + return `${String(date.getHours()).padStart(2, "0")}:${String(date.getMinutes()).padStart(2, "0")}:${String(date.getSeconds()).padStart(2, "0")}`; +} + +export async function readUtmpRecordsStrict(file) { + const bytes = await readFile(file); + return parseUtmpRecords(bytes); +} + +export function uptimeBootTime(records, allowFallback) { + const boot = whoBootRecord(records); + if (boot?.time) return boot.time; + if (allowFallback) { + try { + const match = readFileSync("/proc/stat", "utf8").match(/^btime\s+(\d+)/m); + if (match) return Number(match[1]); + } catch {} + } + return null; +} + +export function formatUptimeDefault(seconds) { + const days = Math.trunc(seconds / 86400); + const hours = Math.trunc((seconds % 86400) / 3600); + const minutes = Math.trunc((seconds % 3600) / 60); + if (days) return `up ${days} day${days === 1 ? "" : "s"}, ${String(hours).padStart(2)}:${String(minutes).padStart(2, "0")}`; + return `up ${String(hours).padStart(2)}:${String(minutes).padStart(2, "0")}`; +} + +const singleCall = defineCommand("uptime", uptimeCmd, uptimeMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/users.js b/src/commands/users.js new file mode 100644 index 0000000..2766f5c --- /dev/null +++ b/src/commands/users.js @@ -0,0 +1,22 @@ +#!/usr/bin/env bun + +import { helpVersionOnlyMetaOption, localeQuotedEscapedDiagnostic, normalizeHelpVersionOnlyLongOptions, parseOptions } from "../shared/common.js"; +import { UsageError, stdout } from "../shared/diagnostics.js"; +import { filterWhoUsers, readUtmpRecords } from "../shared/system.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export async function users(args) { + args = normalizeHelpVersionOnlyLongOptions(args); + const { operands } = parseOptions(args, { long: { help: false, version: false } }); + if (operands.length > 1) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[1])}`, true); + const records = await readUtmpRecords(operands[0] ?? "/var/run/utmp"); + const users = await filterWhoUsers(records, operands.length === 0); + const names = users.map((record) => record.user); + if (names.length) stdout(`${names.join(" ")}\n`); + return 0; +} + +const singleCall = defineCommand("users", users, helpVersionOnlyMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/vdir.js b/src/commands/vdir.js new file mode 100644 index 0000000..64922e5 --- /dev/null +++ b/src/commands/vdir.js @@ -0,0 +1,13 @@ +#!/usr/bin/env bun + +import { lsCmd, lsMetaOption } from "../shared/listing.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export async function vdirCmd(args) { + return lsCmd(["-l", "--quoting-style=escape", ...args]); +} + +const singleCall = defineCommand("vdir", vdirCmd, lsMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/wc.js b/src/commands/wc.js new file mode 100644 index 0000000..7f825ad --- /dev/null +++ b/src/commands/wc.js @@ -0,0 +1,374 @@ +#!/usr/bin/env bun + +import { closeSync, openSync, readSync } from "node:fs"; +import { stat } from "node:fs/promises"; +import { createFdRecordReader, displayWidth, isWriteError, localeQuotedDiagnostic, localeQuotedEscapedDiagnostic, nodeErrorMessage, normalizeLongOptionByPrefix, normalizeLongOptionsByPrefix, parseOptions, pathDisplayName, readAll, shellEscapeLsName, splitFiles0ByteNames, systemErrorMessage, wcFileNameIsDash, wcFileNameIsEmpty, wcFiles0SourceIsNonRegular } from "../shared/common.js"; +import { UsageError, stderr, stdout } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const UINTMAX_MAX = 18446744073709551615n; + +export const WC_LONG_OPTIONS = ["bytes", "chars", "lines", "max-line-length", "words", "files0-from", "total", "debug", "help", "version"]; + +export function wcMetaOption(args) { + const longValueOptions = new Set(["files0-from", "total"]); + const shortKnownOptions = new Set(["c", "m", "l", "L", "w"]); + for (let i = 0; i < args.length; i++) { + const arg = args[i]; + if (arg === "--") return null; + if (arg.startsWith("--")) { + const option = normalizeWcLongOption(arg); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (!WC_LONG_OPTIONS.includes(name)) return null; + if ((option === "--help" || option === "--version") && inlineValue == null) return option; + if (name === "total" && inlineValue != null) validateWcTotalMode(inlineValue); + if (inlineValue == null && longValueOptions.has(name)) i++; + continue; + } + if (!arg.startsWith("-") || arg === "-") continue; + for (let j = 1; j < arg.length; j++) if (!shortKnownOptions.has(arg[j])) return null; + } + return null; +} + +export async function wc(args) { + args = normalizeWcLongOptions(args); + const parsed = parseOptions(args, { + short: { c: false, m: false, l: false, L: false, w: false }, + long: { bytes: false, chars: false, lines: false, "max-line-length": false, words: false, "files0-from": "value", total: "value", debug: false, help: false, version: false }, + }); + const flags = parsed.opts; + const showAll = !(flags.c || flags.m || flags.l || flags.L || flags.w || flags.bytes || flags.chars || flags.lines || flags["max-line-length"] || flags.words); + const totalMode = flags.total ?? "auto"; + validateWcTotalMode(totalMode); + let files = parsed.operands.length ? parsed.operands : ["-"]; + let files0From = null; + let files0EntryCount = null; + if (flags["files0-from"] !== undefined) { + if (parsed.operands.length) throw new UsageError(`extra operand '${parsed.operands[0]}'\nfile operands cannot be combined with --files0-from`, true); + files0From = flags["files0-from"]; + if (wcFiles0SourceIsNonRegular(files0From)) { + return wcStreamFiles0(files0From, flags, showAll, totalMode); + } + let nameBytes; + try { + nameBytes = await readAll(files0From); + } catch (error) { + stderr(error?.code === "EISDIR" + ? `wc: ${wcFiles0DiagnosticName(files0From)}: read error: ${systemErrorMessage(error)}\n` + : `wc: cannot open ${wcFiles0OpenName(files0From)} for reading: ${systemErrorMessage(error)}\n`); + return 1; + } + files = splitFiles0ByteNames(nameBytes); + files0EntryCount = files.length; + if (files0From === "-" && files.some(wcFileNameIsDash)) { + stderr("wc: when reading file names from standard input, no file name of '-' allowed\n"); + return 1; + } + } + const rows = []; + const total = { lines: 0, words: 0, bytes: 0n, chars: 0, maxLineLength: 0 }; + const bytesOnly = (flags.c || flags.bytes) && !(flags.m || flags.chars || flags.l || flags.lines || flags.L || flags["max-line-length"] || flags.w || flags.words); + let failed = false; + let totalByteOverflow = false; + for (let index = 0; index < files.length; index++) { + const file = files[index]; + if (files0From != null && wcFileNameIsEmpty(file)) { + stderr(`wc: ${wcFiles0DiagnosticName(files0From)}:${index + 1}: invalid zero-length file name\n`); + failed = true; + continue; + } + let bytes; + try { + if (bytesOnly && file === "-") { + const size = wcCountFdBytes(0); + const name = parsed.operands.length === 0 && flags["files0-from"] === undefined ? "" : file; + const row = { lines: 0, words: 0, bytes: size, chars: 0, maxLineLength: 0, name }; + if (wcByteTotalWouldOverflow(total.bytes, row.bytes)) totalByteOverflow = true; + total.bytes = addWcByteTotal(total.bytes, row.bytes); + rows.push(row); + continue; + } + if (bytesOnly && file !== "-") { + const info = await stat(file, { bigint: true }); + if (info.isDirectory()) throw Object.assign(new Error("Is a directory"), { code: "EISDIR" }); + const size = info.size; + bytes = null; + const row = { lines: 0, words: 0, bytes: size, chars: 0, maxLineLength: 0, name: file, specialFile: wcStatIsSpecial(info) }; + if (wcByteTotalWouldOverflow(total.bytes, row.bytes)) totalByteOverflow = true; + total.bytes = addWcByteTotal(total.bytes, row.bytes); + rows.push(row); + continue; + } + bytes = await readAll(file); + } catch (error) { + if (error?.code === "EISDIR") { + stderr(`wc: ${wcDiagnosticName(file)}: ${systemErrorMessage(error)}\n`); + rows.push({ lines: 0, words: 0, bytes: 0, chars: 0, maxLineLength: 0, name: file, directoryError: true }); + } else { + stderr(file === "-" ? `wc: ${nodeErrorMessage(error)}\n` : `wc: ${wcDiagnosticName(file)}: ${systemErrorMessage(error)}\n`); + } + failed = true; + continue; + } + const row = wcRow(bytes, file === "-" && parsed.operands.length === 0 && flags["files0-from"] === undefined ? "" : file); + if (file !== "-") { + const info = await stat(file).catch(() => null); + if (info && wcStatIsSpecial(info)) row.specialFile = true; + } + total.lines += row.lines; + total.words += row.words; + if (wcByteTotalWouldOverflow(total.bytes, row.bytes)) totalByteOverflow = true; + total.bytes = addWcByteTotal(total.bytes, row.bytes); + total.chars += row.chars; + total.maxLineLength = Math.max(total.maxLineLength, row.maxLineLength); + rows.push(row); + } + const totalInputCount = files0EntryCount ?? files.length; + const includeTotal = totalMode === "always" || totalMode === "only" || (totalMode === "auto" && totalInputCount > 1); + if (includeTotal && totalByteOverflow) failed = true; + const outputRows = totalMode === "only" ? [] : [...rows]; + if (includeTotal) outputRows.push({ ...total, name: totalMode === "only" ? "" : "total" }); + const selectedValues = (row) => { + const values = []; + if (showAll || flags.l || flags.lines) values.push(row.lines); + if (showAll || flags.w || flags.words) values.push(row.words); + if (flags.m || flags.chars) values.push(row.chars); + if (showAll || flags.c || flags.bytes) values.push(row.bytes); + if (flags.L || flags["max-line-length"]) values.push(row.maxLineLength); + return values; + }; + const widthValues = (row) => showAll ? selectedValues(row) : [row.lines, row.words, row.chars, row.bytes, row.maxLineLength]; + const hasNamedOutput = outputRows.some((row) => row.name); + const widthRows = [...outputRows]; + if (hasNamedOutput && totalInputCount > 1 && totalMode === "never") widthRows.push(total); + const minCountWidth = showAll && outputRows.some((row) => row.directoryError || row.specialFile) ? 7 : 1; + const countWidth = hasNamedOutput + ? Math.max(minCountWidth, ...widthRows.flatMap(widthValues).map((n) => String(n).length)) + : 7; + for (const row of outputRows) { + const cols = selectedValues(row); + const unpadded = totalMode === "only" || (!row.name && cols.length === 1) || (files0From != null && cols.length === 1); + stdout(`${cols.map((n) => unpadded ? String(n) : String(n).padStart(countWidth)).join(" ")}${row.name ? ` ${wcDisplayName(row.name)}` : ""}\n`); + } + return failed ? 1 : 0; +} + +export function wcCountFdBytes(fd) { + const buffer = Buffer.allocUnsafe(64 * 1024); + let total = 0n; + while (true) { + const bytesRead = readSync(fd, buffer, 0, buffer.length, null); + if (bytesRead === 0) return total; + total += BigInt(bytesRead); + } +} + +export async function wcStreamFiles0(source, flags, showAll, totalMode) { + let fd = 0; + let closeFd = false; + try { + if (source !== "-") { + fd = openSync(source, "r"); + closeFd = true; + } + } catch (error) { + stderr(`wc: cannot open ${wcFiles0OpenName(source)} for reading: ${systemErrorMessage(error)}\n`); + return 1; + } + const reader = createFdRecordReader(fd, 0); + const total = { lines: 0, words: 0, bytes: 0n, chars: 0, maxLineLength: 0 }; + const bytesOnly = (flags.c || flags.bytes) && !(flags.m || flags.chars || flags.l || flags.lines || flags.L || flags["max-line-length"] || flags.w || flags.words); + const selectedValues = (row) => { + const values = []; + if (showAll || flags.l || flags.lines) values.push(row.lines); + if (showAll || flags.w || flags.words) values.push(row.words); + if (flags.m || flags.chars) values.push(row.chars); + if (showAll || flags.c || flags.bytes) values.push(row.bytes); + if (flags.L || flags["max-line-length"]) values.push(row.maxLineLength); + return values; + }; + let failed = false; + let totalByteOverflow = false; + let entryCount = 0; + try { + while (true) { + const file = reader.next(); + if (file == null) break; + entryCount++; + if (wcFileNameIsEmpty(file)) { + stderr(`wc: ${wcFiles0DiagnosticName(source)}:${entryCount}: invalid zero-length file name\n`); + failed = true; + continue; + } + if (source === "-" && wcFileNameIsDash(file)) { + stderr("wc: when reading file names from standard input, no file name of '-' allowed\n"); + failed = true; + continue; + } + let row; + try { + if (bytesOnly) { + const info = await stat(file, { bigint: true }); + if (info.isDirectory()) throw Object.assign(new Error("Is a directory"), { code: "EISDIR" }); + row = { lines: 0, words: 0, bytes: info.size, chars: 0, maxLineLength: 0, name: file, specialFile: wcStatIsSpecial(info) }; + } else { + const bytes = await readAll(file); + row = wcRow(bytes, file); + const info = await stat(file).catch(() => null); + if (info && wcStatIsSpecial(info)) row.specialFile = true; + } + } catch (error) { + if (error?.code === "EISDIR") { + stderr(`wc: ${wcDiagnosticName(file)}: ${systemErrorMessage(error)}\n`); + row = { lines: 0, words: 0, bytes: 0, chars: 0, maxLineLength: 0, name: file, directoryError: true }; + } else { + stderr(`wc: ${wcDiagnosticName(file)}: ${systemErrorMessage(error)}\n`); + failed = true; + continue; + } + failed = true; + } + total.lines += row.lines; + total.words += row.words; + if (wcByteTotalWouldOverflow(total.bytes, row.bytes)) totalByteOverflow = true; + total.bytes = addWcByteTotal(total.bytes, row.bytes); + total.chars += row.chars; + total.maxLineLength = Math.max(total.maxLineLength, row.maxLineLength); + if (totalMode !== "only") { + const cols = selectedValues(row); + stdout(`${cols.map(String).join(" ")} ${wcDisplayName(row.name)}\n`); + } + } + } catch (error) { + if (isWriteError(error)) throw error; + stderr(`wc: ${wcFiles0DiagnosticName(source)}: read error: ${systemErrorMessage(error)}\n`); + failed = true; + } finally { + if (closeFd) closeSync(fd); + } + const includeTotal = totalMode === "always" || totalMode === "only" || (totalMode === "auto" && entryCount > 1); + if (includeTotal) { + if (totalByteOverflow) failed = true; + const cols = selectedValues(total); + stdout(`${cols.map(String).join(" ")}${totalMode === "only" ? "" : " total"}\n`); + } + return failed ? 1 : 0; +} + +export function validateWcTotalMode(totalMode) { + if (["auto", "always", "only", "never"].includes(totalMode)) return; + throw new UsageError(`${totalMode === "" ? "ambiguous" : "invalid"} argument ${localeQuotedEscapedDiagnostic(totalMode)} for ${localeQuotedDiagnostic("--total")}\nValid arguments are:\n - ${localeQuotedDiagnostic("auto")}\n - ${localeQuotedDiagnostic("always")}\n - ${localeQuotedDiagnostic("only")}\n - ${localeQuotedDiagnostic("never")}`, true); +} + +export function wcDisplayName(name) { + const text = pathDisplayName(name); + if (!text.includes("\n")) return text; + const parts = text.split("\n"); + const trailingNewline = parts.at(-1) === ""; + if (trailingNewline) parts.pop(); + return parts.map((part) => `'${part.replaceAll("'", "'\\''")}'`).join("$'\\n'") + (trailingNewline ? "$'\\n'" : ""); +} + +export function wcDiagnosticName(name) { + return shellEscapeLsName(pathDisplayName(name)); +} + +export function wcFiles0OpenName(name) { + return name === "-" ? "-" : shellEscapeLsName(pathDisplayName(name), true); +} + +export function wcFiles0DiagnosticName(name) { + return name === "-" ? "-" : shellEscapeLsName(pathDisplayName(name)); +} + +export function wcStatIsSpecial(info) { + return info.isCharacterDevice() || info.isBlockDevice() || info.isFIFO() || info.isSocket(); +} + +export function addWcByteTotal(total, bytes) { + const next = total + BigInt(bytes); + return next > UINTMAX_MAX ? UINTMAX_MAX : next; +} + +export function wcByteTotalWouldOverflow(total, bytes) { + return total + BigInt(bytes) > UINTMAX_MAX; +} + +export function normalizeWcLongOptions(args) { + return normalizeLongOptionsByPrefix(args, WC_LONG_OPTIONS); +} + +export function normalizeWcLongOption(arg) { + return normalizeLongOptionByPrefix(arg, WC_LONG_OPTIONS); +} + +export function wcRow(bytes, name) { + const text = new TextDecoder().decode(bytes); + return { + lines: (text.match(/\n/g) || []).length, + words: countWcWords(bytes, text), + bytes: bytes.byteLength, + chars: [...text].length, + maxLineLength: maxLineLength(text), + name, + }; +} + +export function countWcWords(bytes, text) { + if (text.includes("\uFFFD")) return countWcWordsBytes(bytes); + let count = 0; + let inWord = false; + for (const ch of text) { + if (isWcWordSeparator(ch)) { + inWord = false; + } else if (!inWord) { + count++; + inWord = true; + } + } + return count; +} + +export function countWcWordsBytes(bytes) { + let count = 0; + let inWord = false; + for (const byte of bytes) { + if (isWcByteSeparator(byte)) { + inWord = false; + } else if (!inWord) { + count++; + inWord = true; + } + } + return count; +} + +export function isWcWordSeparator(ch) { + return /\s/u.test(ch) || ch === "\u2060"; +} + +export function isWcByteSeparator(byte) { + return byte === 0x20 || (byte >= 0x09 && byte <= 0x0d) || byte === 0xa0 || byte === 0x9a; +} + +export function maxLineLength(text) { + let max = 0; + let column = 0; + for (const ch of text) { + if (ch === "\n" || ch === "\r" || ch === "\f") { + max = Math.max(max, column); + column = 0; + } else if (ch === "\t") { + column += 8 - (column % 8); + } else { + column += displayWidth(ch); + } + } + return Math.max(max, column); +} + +const singleCall = defineCommand("wc", wc, wcMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/who.js b/src/commands/who.js new file mode 100644 index 0000000..e4cbd3d --- /dev/null +++ b/src/commands/who.js @@ -0,0 +1,182 @@ +#!/usr/bin/env bun + +import { localeQuotedEscapedDiagnostic, normalizeLongOptionByPrefix, parseOptions, statSyncNoThrow } from "../shared/common.js"; +import { UsageError, stdout } from "../shared/diagnostics.js"; +import { filterWhoUsers, formatWhoDate, readUtmpRecords, whoBootRecord, whoUsesPosixDateFormat } from "../shared/system.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const WHO_LONG_OPTIONS = ["all", "boot", "dead", "heading", "login", "lookup", "mesg", "message", "count", "process", "runlevel", "short", "time", "users", "writable", "help", "version"]; + +export async function who(args) { + args = normalizeWhoLongOptions(args); + const { opts, operands } = parseOptions(args, { + short: { a: false, q: false, b: false, d: false, H: false, l: false, m: false, p: false, r: false, s: false, t: false, T: false, u: false, w: false }, + long: { all: false, count: false, boot: false, dead: false, heading: false, login: false, lookup: false, mesg: false, message: false, process: false, runlevel: false, short: false, time: false, users: false, writable: false, help: false, version: false }, + }); + if (operands.length > 2) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[2])}`, true); + if (opts.m || operands.length === 2) { + if (opts.q || opts.count) stdout("\n# users=0\n"); + else if (opts.H || opts.heading) stdout(formatWhoHeading(opts)); + return 0; + } + if (operands.length === 1 || operands.length === 0) { + const defaultUtmp = operands.length === 0; + const records = await readUtmpRecords(operands[0] ?? "/var/run/utmp"); + const users = await filterWhoUsers(records, defaultUtmp); + if (opts.a || opts.all) { + if (opts.H || opts.heading) stdout(formatWhoAllHeading()); + for (const record of records) { + const rendered = record.type === 7 && record.user ? formatWhoRecord(record, { ...opts, T: true, u: true }) : formatWhoSpecialRecord(record, true); + if (rendered) stdout(rendered); + } + const boot = whoBootRecord(records, defaultUtmp); + if (boot) stdout(` system boot ${formatWhoDate(boot.time)}\n`); + } else if (opts.b || opts.boot) { + if (opts.H || opts.heading) stdout(formatWhoPidHeading()); + const boot = whoBootRecord(records, defaultUtmp); + if (boot) stdout(` system boot ${formatWhoDate(boot.time)}\n`); + } else if (opts.d || opts.dead) { + if (opts.H || opts.heading) stdout(formatWhoIdlePidHeading(true)); + for (const record of records.filter((record) => record.type === 8)) stdout(formatWhoDeadRecord(record)); + } else if (opts.l || opts.login) { + if (opts.H || opts.heading) stdout(formatWhoIdlePidHeading()); + for (const record of records.filter((record) => record.type === 6)) stdout(formatWhoLoginRecord(record)); + } else if (opts.p || opts.process) { + if (opts.H || opts.heading) stdout(formatWhoPidHeading()); + for (const record of records.filter((record) => record.type === 5)) stdout(formatWhoProcessRecord(record)); + } else if (opts.r || opts.runlevel) { + if (opts.H || opts.heading) stdout(formatWhoIdlePidHeading()); + for (const record of records.filter((record) => record.type === 1)) stdout(formatWhoRunlevelRecord(record)); + } else if (opts.t || opts.time) { + if (opts.H || opts.heading) stdout(formatWhoPidHeading()); + for (const record of records.filter((record) => record.type === 3)) stdout(formatWhoClockChangeRecord(record)); + } else if (opts.q || opts.count) { + stdout(`${users.map((record) => record.user).join(" ")}\n# users=${users.length}\n`); + } else { + if (opts.H || opts.heading) stdout(formatWhoHeading(opts)); + for (const record of users) stdout(formatWhoRecord(record, opts)); + } + return 0; + } +} + +export function whoMetaOption(args) { + const shortOptions = new Set(["a", "q", "b", "d", "H", "l", "m", "p", "r", "s", "t", "T", "u", "w"]); + for (const arg of args) { + if (arg === "--") return null; + if (arg.startsWith("--")) { + const normalized = normalizeWhoLongOption(arg); + const name = normalized.slice(2).split("=", 1)[0]; + if (!WHO_LONG_OPTIONS.includes(name) || normalized.includes("=")) return null; + if (normalized === "--help" || normalized === "--version") return normalized; + continue; + } + if (arg.startsWith("-") && arg !== "-") { + for (let i = 1; i < arg.length; i++) if (!shortOptions.has(arg[i])) return null; + } + } + return null; +} + +export function normalizeWhoLongOptions(args) { + const out = []; + for (const arg of args) out.push(arg.startsWith("--") && arg !== "--" ? normalizeWhoLongOption(arg) : arg); + return out; +} + +export function normalizeWhoLongOption(arg) { + const body = arg.slice(2); + const eq = body.indexOf("="); + const name = eq === -1 ? body : body.slice(0, eq); + if (name && !WHO_LONG_OPTIONS.includes(name) && name !== "mesg" && "message".startsWith(name)) { + return eq === -1 ? "--message" : `--message=${body.slice(eq + 1)}`; + } + return normalizeLongOptionByPrefix(arg, WHO_LONG_OPTIONS); +} + +export function formatWhoSpecialRecord(record, all = false) { + if (record.type === 8) return formatWhoDeadRecord(record, all); + if (record.type === 1) return formatWhoRunlevelRecord(record, all); + if (record.type === 3) return formatWhoClockChangeRecord(record, all); + if (record.type === 6) return formatWhoLoginRecord(record, all); + if (record.type === 5) return formatWhoProcessRecord(record, all); + return ""; +} + +export function formatWhoClockChangeRecord(record, all = false) { + const nameWidth = all ? 10 : 8; + return `${"".padEnd(nameWidth)} clock change ${formatWhoDate(record.time)}\n`; +} + +export function formatWhoProcessRecord(record, all = false) { + const nameWidth = all ? 10 : 8; + const pidWidth = all ? 17 : 10; + return `${"".padEnd(nameWidth)} ${record.line.padEnd(12)} ${formatWhoDate(record.time)} ${String(record.pid).padStart(pidWidth)} id=${record.id}\n`; +} + +export function formatWhoDeadRecord(record, all = false) { + const nameWidth = all ? 10 : 8; + return `${"".padEnd(nameWidth)} ${record.line.padEnd(12)} ${formatWhoDate(record.time)} ${String(record.pid).padStart(17)} id=${record.id.padEnd(5)} term=${record.exitTermination} exit=${record.exitStatus}\n`; +} + +export function formatWhoLoginRecord(record, all = false) { + const nameWidth = all ? 10 : 8; + return `${record.user.padEnd(nameWidth)} ${record.line.padEnd(12)} ${formatWhoDate(record.time)} ${String(record.pid).padStart(17)} id=${record.id}\n`; +} + +export function formatWhoRunlevelRecord(record, all = false) { + const current = String.fromCharCode(record.pid & 0xff); + const previous = String.fromCharCode((record.pid >> 8) & 0xff); + const nameWidth = all ? 10 : 8; + return `${"".padEnd(nameWidth)} run-level ${current} ${formatWhoDate(record.time)} ${"".padStart(18)}last=${previous === "N" ? "S" : previous}\n`; +} + +export function formatWhoRecord(record, opts = {}) { + const showMessageStatus = opts.T || opts.w || opts.mesg || opts.message || opts.writable; + const showIdle = opts.u || opts.users; + const status = showMessageStatus ? ` ${whoMessageStatus(record.line)}` : ""; + const idle = showIdle ? whoIdleText(record.line) : ""; + const pid = showIdle ? String(record.pid).padStart(14) : ""; + const where = record.host ? ` (${record.host})` : ""; + return `${record.user.padEnd(8)}${status} ${record.line.padEnd(12)} ${formatWhoDate(record.time)}${showIdle ? `${idle.padStart(4)}${pid}` : ""}${where}\n`; +} + +export function formatWhoHeading(opts = {}) { + const showMessageStatus = opts.T || opts.w || opts.mesg || opts.message || opts.writable; + const showIdle = opts.u || opts.users; + const name = showMessageStatus ? `${"NAME".padEnd(8)} ` : "NAME".padEnd(8); + const suffix = showIdle ? ` ${"IDLE".padEnd(13)} PID COMMENT` : " COMMENT"; + return `${name} ${"LINE".padEnd(12)} ${"TIME".padEnd(whoUsesPosixDateFormat() ? 12 : 16)}${suffix}\n`; +} + +export function formatWhoPidHeading() { + return `${"NAME".padEnd(8)} ${"LINE".padEnd(12)} ${"TIME".padEnd(whoUsesPosixDateFormat() ? 12 : 16)}${"".padEnd(7)} PID COMMENT\n`; +} + +export function formatWhoIdlePidHeading(exit = false) { + return `${"NAME".padEnd(8)} ${"LINE".padEnd(12)} ${"TIME".padEnd(whoUsesPosixDateFormat() ? 12 : 16)} ${"IDLE".padEnd(13)} PID COMMENT${exit ? " EXIT" : ""}\n`; +} + +export function formatWhoAllHeading() { + return `${formatWhoHeading({ T: true, u: true }).trimEnd()} EXIT\n`; +} + +export function whoMessageStatus(line) { + const mode = statSyncNoThrow(`/dev/${line}`)?.mode; + if (mode == null) return "?"; + return mode & 0o020 ? "+" : "-"; +} + +export function whoIdleText(line) { + const info = statSyncNoThrow(`/dev/${line}`); + if (!info) return "?"; + const seconds = Math.max(0, Math.floor((Date.now() - info.atimeMs) / 1000)); + if (seconds < 60) return "."; + if (seconds >= 24 * 60 * 60) return "old"; + return `${String(Math.floor(seconds / 3600)).padStart(2)}:${String(Math.floor((seconds % 3600) / 60)).padStart(2, "0")}`; +} + +const singleCall = defineCommand("who", who, whoMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/whoami.js b/src/commands/whoami.js new file mode 100644 index 0000000..92b6dd1 --- /dev/null +++ b/src/commands/whoami.js @@ -0,0 +1,20 @@ +#!/usr/bin/env bun + +import { userInfo } from "node:os"; +import { helpVersionOnlyMetaOption, localeQuotedEscapedDiagnostic, normalizeHelpVersionOnlyLongOptions, parseOptions, userNameForUid } from "../shared/common.js"; +import { UsageError, stdout } from "../shared/diagnostics.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export async function whoamiCmd(args) { + args = normalizeHelpVersionOnlyLongOptions(args); + const { operands } = parseOptions(args, { long: { help: false, version: false } }); + if (operands.length) throw new UsageError(`extra operand ${localeQuotedEscapedDiagnostic(operands[0])}`, true); + const info = userInfo(); + stdout(`${await userNameForUid(process.getuid?.() ?? info.uid) ?? info.username}\n`); + return 0; +} + +const singleCall = defineCommand("whoami", whoamiCmd, helpVersionOnlyMetaOption); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); diff --git a/src/commands/yes.js b/src/commands/yes.js new file mode 100644 index 0000000..370c75b --- /dev/null +++ b/src/commands/yes.js @@ -0,0 +1,85 @@ +#!/usr/bin/env bun + +import { writeSync } from "node:fs"; +import { parentIgnoresSigpipe } from "../shared/common.js"; +import { UsageError, VERSION, stderr, stdout } from "../shared/diagnostics.js"; +import { outputWriteErrorMessage } from "../shared/runtime.js"; +import { defineCommand, runAsMain } from "../shared/command.js"; + +export const YES_LONG_OPTIONS = ["help", "version"]; + +export async function yes(args) { + const meta = yesMetaOption(args); + if (meta === "--help") { + showYesHelp(); + return 0; + } + if (meta === "--version") { + stdout(`${VERSION}\n`); + return 0; + } + validateYesOptions(args); + if (args[0] === "--") args = args.slice(1); + const text = `${args.length ? args.join(" ") : "y"}\n`; + const chunk = text.repeat(1024); + while (true) { + try { + writeSync(1, chunk); + } catch (error) { + if (error?.code === "EPIPE") { + if (parentIgnoresSigpipe()) stderr("yes: standard output: Broken pipe\n"); + return parentIgnoresSigpipe() ? 1 : 0; + } + stderr(`yes: standard output: ${outputWriteErrorMessage(error)}\n`); + return 1; + } + } +} + +export function yesMetaOption(args) { + for (const arg of args) { + if (arg === "--") return null; + if (!arg.startsWith("-") || arg === "-") continue; + if (!arg.startsWith("--")) return null; + const option = normalizeYesLongOption(arg); + const name = option.slice(2).split("=", 1)[0]; + if (!YES_LONG_OPTIONS.includes(name) || option.includes("=")) return null; + return option; + } + return null; +} + +export function validateYesOptions(args) { + for (const arg of args) { + if (arg === "--") return; + if (!arg.startsWith("-") || arg === "-") continue; + if (!arg.startsWith("--")) throw new UsageError(`invalid option -- '${arg.slice(1, 2)}'`, true); + const option = normalizeYesLongOption(arg); + const [name, inlineValue] = option.slice(2).split(/=(.*)/s, 2); + if (!YES_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); + } +} + +export function normalizeYesLongOption(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 = YES_LONG_OPTIONS.find((option) => option.startsWith(name)); + if (!match) return arg; + return eq === -1 ? `--${match}` : `--${match}=${body.slice(eq + 1)}`; +} + +export function showYesHelp() { + stdout("Usage: yes [STRING]...\n"); + stdout(" or: yes OPTION\n"); + stdout("Repeatedly output a line with all specified STRING(s), or 'y'.\n\n"); + stdout(" --help display this help and exit\n"); + stdout(" --version output version information and exit\n"); +} + +const singleCall = defineCommand("yes", yes, null); +export default singleCall; + +if (import.meta.main) await runAsMain(singleCall); |
