Files
bare-operating-system/packages/bare-os-seeder/kernel/bin/sort
T
2026-04-03 22:34:08 -04:00

308 lines
7.8 KiB
Plaintext

/* 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<string, unknown>} 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
}
/**
* @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(' ')
}
async function run(ctx, argv) {
const vfs = ctx.vfs
let numeric = false
let reverse = false
let uniq = false
let fold = false
/** @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 === '-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 {
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
/** @param {string} s */
function sortKey(s) {
const keyText =
keyStart != null
? sortKeySlice(s, dForKey, keyStart, keyEnd)
: s
if (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 (fold) return { n: 0, raw: keyText.toLowerCase() }
return { n: 0, raw: keyText }
}
lines.sort((x, y) => {
const kx = sortKey(x)
const ky = sortKey(y)
if (numeric) {
if (kx.n !== ky.n) {
const ord = kx.n < ky.n ? -1 : 1
return reverse ? -ord : ord
}
}
const cmp =
kx.raw < ky.raw ? -1 : kx.raw > ky.raw ? 1 : x < y ? -1 : x > y ? 1 : 0
return reverse ? -cmp : cmp
})
if (uniq) {
const out = []
let prev = null
for (const ln of lines) {
if (ln !== prev) out.push(ln)
prev = ln
}
lines = out
}
if (lines.length) ctx.console.log(lines.join('\n'))
}