/* BARE_OS_BIN_API 1.0.0 — bump when staged /bin script semantics change (see developer guide). */ /** Shared helpers for drive-resident /bin scripts (prepended before each command). */ function bareStdin(ctx) { return typeof ctx.shellStdin === 'string' ? ctx.shellStdin : '' } /** @param {number} mode @param {'file' | 'directory' | 'symlink'} type */ function bareFormatModeString(mode, type) { const typeChar = type === 'directory' ? 'd' : type === 'symlink' ? 'l' : '-' const perm = mode & 0o777 const r = (bit) => (perm & bit ? 'r' : '-') const w = (bit) => (perm & bit ? 'w' : '-') const x = (bit) => (perm & bit ? 'x' : '-') return ( typeChar + r(0o400) + w(0o200) + x(0o100) + r(0o040) + w(0o020) + x(0o010) + r(0o004) + w(0o002) + x(0o001) ) } /** @param {number} mtimeMs @param {number} [nowMs] */ function bareFormatLsMtime(mtimeMs, nowMs) { const now = nowMs != null ? nowMs : Date.now() const d = new Date(mtimeMs) const months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ] const mon = months[d.getMonth()] const day = String(d.getDate()).padStart(2, ' ') const sixMo = 180 * 24 * 3600 * 1000 if (Math.abs(now - mtimeMs) > sixMo) { const yr = String(d.getFullYear()).padStart(4, ' ') return mon + ' ' + day + ' ' + yr } const hh = String(d.getHours()).padStart(2, '0') const mm = String(d.getMinutes()).padStart(2, '0') return mon + ' ' + day + ' ' + hh + ':' + mm } /** @param {number} size */ function barePosixBlocks(size) { return Math.ceil(Number(size) / 512) || 0 } /** * Raw stdout for NUL/binary when **`process.stdout.write`** is missing. * If **`ctx.bareOsBinWrite(Uint8Array|string)`** is set (tests / host), use it. * @param {Record} ctx * @param {string | Uint8Array} chunk * @returns {boolean} */ function bareOsEmitRaw(ctx, chunk) { if (typeof ctx.bareOsBinWrite === 'function') { const b4 = ctx.b4a const u8 = typeof chunk === 'string' ? b4 && typeof b4.from === 'function' ? b4.from(chunk) : new TextEncoder().encode(chunk) : chunk ctx.bareOsBinWrite(u8 instanceof Uint8Array ? u8 : new Uint8Array(u8)) return true } const w = globalThis.process?.stdout?.write if (typeof w === 'function') { w.call(globalThis.process.stdout, chunk) return true } return false } /** Session env map (`vfs.env`, then `ctx.env`). Never throws. */ function bareOsEnv(ctx) { const v = ctx && ctx.vfs && ctx.vfs.env if (v && typeof v === 'object') return v const e = ctx && ctx.env if (e && typeof e === 'object') return e return {} } /** * Strict POSIX-ish decimal integer (no octal, no exponent, no empty). * @param {unknown} s * @returns {number} */ function bareOsParseDecInt(s) { const t = String(s == null ? '' : s).trim() if (!/^[+-]?(?:0|[1-9][0-9]*)$/.test(t)) return NaN const n = Number.parseInt(t, 10) return Number.isSafeInteger(n) ? n : NaN } /** @param {unknown} s */ function bareOsParseNonNegInt(s) { const n = bareOsParseDecInt(s) return n >= 0 ? n : NaN } /** * @param {Record} ctx * @param {string} name * @param {number} fallback * @param {number} [min] * @param {number} [max] */ function bareOsEnvInt(ctx, name, fallback, min, max) { const raw = bareOsEnv(ctx)[name] if (raw == null || raw === '') return fallback const n = Number.parseInt(String(raw), 10) if (!Number.isFinite(n)) return fallback let v = n if (min != null && v < min) v = min if (max != null && v > max) v = max return v } /** * @param {Record} ctx * @param {string} msg * @param {number} [code] */ function bareOsFail(ctx, msg, code) { if (msg) ctx.console.error(msg) ctx.exitCode = code == null ? 1 : code } /** @param {unknown} e */ function bareOsIsNotFoundErr(e) { const code = e && typeof e === 'object' ? e.code : '' if (code === 'ENOENT') return true const msg = String((e && e.message) || e || '') return /ENOENT|No such file|not found/i.test(msg) } /** * @param {Record} ctx * @param {unknown} buf * @returns {Uint8Array} */ function bareOsToU8(ctx, buf) { if (!buf) return new Uint8Array(0) if (buf instanceof Uint8Array) return buf if (ctx && ctx.b4a && typeof ctx.b4a.from === 'function') return ctx.b4a.from(buf) return new Uint8Array(buf) } /** @param {string} dir @param {string} name */ function bareOsJoinPath(dir, name) { const d = String(dir || '').replace(/\/+$/, '') const n = String(name || '').replace(/^\/+/, '') if (!d || d === '/') return '/' + n return d + '/' + n } /** @param {string} p */ function bareOsBaseName(p) { const t = String(p || '').replace(/\/+$/, '') if (!t || t === '/') return t === '/' ? '/' : '' const i = t.lastIndexOf('/') return i < 0 ? t : t.slice(i + 1) || t } /** @param {string} p */ function bareOsParentDir(p) { const t = String(p || '').replace(/\/+$/, '') || '/' if (t === '/') return '/' const i = t.lastIndexOf('/') return i <= 0 ? '/' : t.slice(0, i) || '/' } /** @param {string} p */ function bareOsNormPath(p) { return String(p || '').replace(/\/+$/, '') || '/' } /** * @param {Record} ctx * @param {string} p */ function bareOsResolvePath(ctx, p) { if (ctx && ctx.vfs && typeof ctx.vfs.resolveLogical === 'function') { try { return String(ctx.vfs.resolveLogical(p) || p) } catch { /* fall through */ } } return String(p || '') } /** * True when dest is src or lives under src (self-copy / self-move). * @param {Record} ctx * @param {string} src * @param {string} dest */ function bareOsDestInsideSrc(ctx, src, dest) { const s = bareOsNormPath(bareOsResolvePath(ctx, src)) const d = bareOsNormPath(bareOsResolvePath(ctx, dest)) if (s === d) return true if (s === '/') return d !== '/' return d === s || d.startsWith(s + '/') } const BARE_OS_B64_ALPH = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' /** @param {Uint8Array} u8 */ function bareOsB64Encode(u8) { let out = '' let i = 0 for (; i + 2 < u8.length; i += 3) { const n = (u8[i] << 16) | (u8[i + 1] << 8) | u8[i + 2] out += BARE_OS_B64_ALPH[(n >> 18) & 63] + BARE_OS_B64_ALPH[(n >> 12) & 63] + BARE_OS_B64_ALPH[(n >> 6) & 63] + BARE_OS_B64_ALPH[n & 63] } const rest = u8.length - i if (rest === 1) { const n = u8[i] << 16 out += BARE_OS_B64_ALPH[(n >> 18) & 63] + BARE_OS_B64_ALPH[(n >> 12) & 63] + '==' } else if (rest === 2) { const n = (u8[i] << 16) | (u8[i + 1] << 8) out += BARE_OS_B64_ALPH[(n >> 18) & 63] + BARE_OS_B64_ALPH[(n >> 12) & 63] + BARE_OS_B64_ALPH[(n >> 6) & 63] + '=' } return out } /** * RFC 4648 Base64 decode (also accepts URL-safe alphabet). Rejects junk. * @param {string} s * @returns {Uint8Array} */ function bareOsB64Decode(s) { const t = String(s).replace(/\s+/g, '') if (!t) return new Uint8Array(0) if (t.length % 4 === 1) throw new Error('invalid base64 length') let pad = 0 if (t.endsWith('==')) pad = 2 else if (t.endsWith('=')) pad = 1 const body = pad ? t.slice(0, t.length - pad) : t const bytes = [] let buf = 0 let bits = 0 for (let i = 0; i < body.length; i++) { const c = body[i] let v = BARE_OS_B64_ALPH.indexOf(c) if (v < 0) { if (c === '-') v = 62 else if (c === '_') v = 63 else throw new Error('invalid base64 character') } buf = (buf << 6) | v bits += 6 if (bits >= 8) { bits -= 8 bytes.push((buf >> bits) & 255) } } if (pad) { const want = Math.floor((body.length * 6) / 8) if (bytes.length > want) bytes.length = want } return new Uint8Array(bytes) } /** * @param {string} s * @returns {Uint8Array} */ function bareOsHexDecode(s) { const t = String(s).replace(/\s+/g, '') if (t.length % 2 !== 0) throw new Error('odd hex length') const out = new Uint8Array(t.length / 2) for (let i = 0; i < out.length; i++) { const pair = t.slice(i * 2, i * 2 + 2) if (!/^[0-9a-fA-F]{2}$/.test(pair)) throw new Error('invalid hex') out[i] = Number.parseInt(pair, 16) } return out } /** @param {Uint8Array} u8 */ function bareOsHexEncode(u8) { let s = '' for (let i = 0; i < u8.length; i++) s += u8[i].toString(16).padStart(2, '0') return s } /** * @param {string} line * @param {string | null} delim * @param {number} start1 * @param {number | null} end1 */ function sortKeySlice(line, delim, start1, end1) { /** @type {string[]} */ let fields if (delim != null) { if (delim === '') fields = line.split('') else fields = line.split(delim) } else { const t = line.trim() fields = t.length ? t.split(/\s+/) : [] } const i0 = Math.max(0, start1 - 1) const i1 = end1 != null ? end1 : fields.length const slice = fields.slice(i0, i1) if (delim != null) return slice.join(delim) return slice.join(' ') } /** * @param {object} o * @param {string} o.line * @param {boolean} o.numeric * @param {boolean} o.fold * @param {number | null} o.keyStart * @param {number | null} o.keyEnd * @param {string | null} o.dForKey */ function sortKeyObj(o) { const keyText = o.keyStart != null ? sortKeySlice(o.line, o.dForKey, o.keyStart, o.keyEnd) : o.line if (o.numeric) { const m = String(keyText).match( /^\s*(-?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?)/ ) if (m) return { n: Number(m[1]), raw: keyText } return { n: Number.POSITIVE_INFINITY, raw: keyText } } if (o.fold) return { n: 0, raw: keyText.toLowerCase() } return { n: 0, raw: keyText } } /** * @param {Record | undefined} ctx */ function sortLocaleTag(ctx) { const env = ctx && ctx.vfs && ctx.vfs.env && typeof ctx.vfs.env === 'object' ? ctx.vfs.env : ctx && ctx.env && typeof ctx.env === 'object' ? ctx.env : {} const t = String( /** @type {Record} */ (env).LC_ALL || /** @type {Record} */ (env).LC_COLLATE || 'C' ).trim() return t || 'C' } /** * @param {Record | undefined} ctx * @param {string} a * @param {string} b */ function sortLocaleCompareRaw(ctx, a, b) { const tag = sortLocaleTag(ctx) if (tag === 'C' || tag === 'POSIX') { return a < b ? -1 : a > b ? 1 : 0 } try { return String(a).localeCompare(String(b), tag, { sensitivity: 'variant' }) } catch { return a < b ? -1 : a > b ? 1 : 0 } } /** * @param {string} x * @param {string} y * @param {object} opts * @param {boolean} opts.numeric * @param {boolean} opts.reverse * @param {boolean} opts.fold * @param {number | null} opts.keyStart * @param {number | null} opts.keyEnd * @param {string | null} opts.dForKey * @param {Record | undefined} [opts.ctx] * @returns {number} */ function sortCompareLines(x, y, opts) { const base = { numeric: opts.numeric, fold: opts.fold, keyStart: opts.keyStart, keyEnd: opts.keyEnd, dForKey: opts.dForKey } const kx = sortKeyObj({ ...base, line: x }) const ky = sortKeyObj({ ...base, line: y }) if (opts.numeric) { if (kx.n !== ky.n) { const ord = kx.n < ky.n ? -1 : 1 return opts.reverse ? -ord : ord } } let cmp = sortLocaleCompareRaw(opts.ctx, kx.raw, ky.raw) if (cmp === 0) cmp = sortLocaleCompareRaw(opts.ctx, x, y) return opts.reverse ? -cmp : cmp } async function run(ctx, argv) { const vfs = ctx.vfs let numeric = false let reverse = false let uniq = false let fold = false /** @type {'off' | 'verbose' | 'quiet'} */ let checkMode = 'off' let stable = false /** @type {string | null} */ let outFile = null /** @type {string | undefined} */ let delim /** @type {number | null} */ let keyStart = null /** @type {number | null} */ let keyEnd = null let i = 1 while (i < argv.length) { const a = argv[i] if (a === '--') { i++ break } if (a === '--check' || a === '--check=diagnose-first') { checkMode = 'verbose' i++ continue } if (a === '--check=silent' || a === '--check=quiet') { checkMode = 'quiet' i++ continue } if (a === '-C') { checkMode = 'quiet' i++ continue } if (a === '-c') { checkMode = 'verbose' i++ continue } if (a === '-s' || a === '--stable') { stable = true i++ continue } if (a === '-o' || a === '--output') { const f = argv[++i] if (f === undefined) { ctx.console.error('sort: option requires an argument -- output') ctx.exitCode = 1 return } outFile = f i++ continue } if (a.startsWith('--output=')) { outFile = a.slice('--output='.length) || null if (!outFile) { ctx.console.error('sort: option requires an argument -- output') ctx.exitCode = 1 return } i++ continue } if (a === '-n' || a === '--numeric-sort' || a === '-g') { numeric = true i++ continue } if (a === '-r' || a === '--reverse') { reverse = true i++ continue } if (a === '-u' || a === '--unique') { uniq = true i++ continue } if (a === '-f' || a === '--ignore-case') { fold = true i++ continue } if (a === '-t' || a === '--field-separator') { const d = argv[++i] if (d === undefined) { ctx.console.error('sort: option requires an argument -- t') ctx.exitCode = 1 return } delim = d === '\\t' ? '\t' : d i++ continue } if (a.startsWith('-t') && a.length > 2) { delim = a.slice(2) === 't' ? '\t' : a.slice(2) i++ continue } if (a === '-k' || a === '--key') { const spec = argv[++i] if (spec === undefined) { ctx.console.error('sort: option requires an argument -- k') ctx.exitCode = 1 return } const parts = String(spec).split(',') keyStart = Number.parseInt(parts[0], 10) keyEnd = parts[1] !== undefined && parts[1] !== '' ? Number.parseInt(parts[1], 10) : null if (!Number.isFinite(keyStart) || keyStart < 1) { ctx.console.error('sort: invalid key specification') ctx.exitCode = 1 return } if ( keyEnd != null && (!Number.isFinite(keyEnd) || keyEnd < keyStart) ) { ctx.console.error('sort: invalid key specification') ctx.exitCode = 1 return } i++ continue } if (/^-k\d/.test(a)) { const spec = a.slice(2) const parts = String(spec).split(',') keyStart = Number.parseInt(parts[0], 10) keyEnd = parts[1] !== undefined && parts[1] !== '' ? Number.parseInt(parts[1], 10) : null if (!Number.isFinite(keyStart) || keyStart < 1) { ctx.console.error('sort: invalid key specification') ctx.exitCode = 1 return } i++ continue } if (a.startsWith('-') && a.length > 1) { let j = 1 let ok = true while (j < a.length) { const c = a[j++] if (c === 'n') numeric = true else if (c === 'r') reverse = true else if (c === 'u') uniq = true else if (c === 'f') fold = true else if (c === 'c') checkMode = 'verbose' else if (c === 'C') checkMode = 'quiet' else if (c === 's') stable = true else { ok = false break } } if (ok) { i++ continue } } break } const files = argv.slice(i).filter((x) => x !== '--') /** @type {string[]} */ let lines = [] if (!files.length) { const s = bareStdin(ctx) lines = s.length ? s.split('\n') : [] if (lines.length && lines[lines.length - 1] === '') lines.pop() } else { for (const f of files) { if (f === '-') { const s = bareStdin(ctx) const part = s.length ? s.split('\n') : [] if (part.length && part[part.length - 1] === '') part.pop() lines.push(...part) continue } const buf = await vfs.readFile(f) if (!buf) { ctx.console.error('sort: ' + f + ': No such file') ctx.exitCode = 1 continue } const part = ctx.b4a.toString(buf).split('\n') if (part.length && part[part.length - 1] === '') part.pop() lines.push(...part) } } const dForKey = delim === undefined ? null : delim const cmpOpts = { numeric, reverse, fold, keyStart, keyEnd, dForKey, ctx } if (checkMode !== 'off') { const checkLabel = files.length === 1 ? files[0] : '-' for (let li = 0; li < lines.length - 1; li++) { const aLine = lines[li] const bLine = lines[li + 1] const c = sortCompareLines(aLine, bLine, cmpOpts) if (uniq && c === 0) { ctx.exitCode = 1 if (checkMode === 'verbose') { ctx.console.error( 'sort: ' + checkLabel + ':' + (li + 2) + ': disorder: ' + bLine ) } return } if (c > 0) { ctx.exitCode = 1 if (checkMode === 'verbose') { ctx.console.error( 'sort: ' + checkLabel + ':' + (li + 2) + ': disorder: ' + bLine ) } return } } ctx.exitCode = 0 return } /** @param {string} x * @param {string} y */ function cmpPair(x, y) { const primary = sortCompareLines(x, y, cmpOpts) if (primary !== 0 || !stable) return primary return 0 } if (stable) { const decorated = lines.map((line, idx) => ({ line, idx })) decorated.sort((a, b) => { const p = sortCompareLines(a.line, b.line, cmpOpts) if (p !== 0) return p return a.idx - b.idx }) lines = decorated.map((d) => d.line) } else { lines.sort(cmpPair) } if (uniq) { const out = [] let prev = null for (const ln of lines) { if (prev != null && sortCompareLines(prev, ln, cmpOpts) === 0) continue out.push(ln) prev = ln } lines = out } const body = lines.length ? lines.join('\n') + '\n' : '' if (outFile != null) { try { await vfs.writeFile(outFile, ctx.b4a.from(body)) } catch (e) { ctx.console.error('sort: ' + outFile + ': ' + (e.message || e)) ctx.exitCode = 1 return } } else if (lines.length) { ctx.console.log(lines.join('\n')) } }