core utils updates
This commit is contained in:
+69
-10
@@ -60,15 +60,74 @@ 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
|
||||
}
|
||||
|
||||
async function run(ctx, argv) {
|
||||
const flagArgs = argv.slice(1).filter((a) => a.startsWith('-') && a !== '--')
|
||||
const all = argv.includes('-a')
|
||||
const noFlag = flagArgs.length === 0
|
||||
const wantS = all || argv.includes('-s') || noFlag
|
||||
const wantN = all || argv.includes('-n')
|
||||
const wantR = all || argv.includes('-r')
|
||||
const wantM = all || argv.includes('-m')
|
||||
const wantV = all || argv.includes('-v')
|
||||
const e = ctx.vfs.env || {}
|
||||
const nodename = e.HOSTNAME || e.NAME || 'bare-os'
|
||||
let all = false
|
||||
let wantS = false
|
||||
let wantN = false
|
||||
let wantR = false
|
||||
let wantM = false
|
||||
let wantV = false
|
||||
for (let i = 1; i < argv.length; i++) {
|
||||
const a = argv[i]
|
||||
if (a === '--') break
|
||||
if (a === '-a' || a === '--all') {
|
||||
all = true
|
||||
continue
|
||||
}
|
||||
if (!a.startsWith('-') || a === '-') {
|
||||
ctx.console.error('uname: unexpected argument ' + a)
|
||||
ctx.exitCode = 1
|
||||
return
|
||||
}
|
||||
for (let j = 1; j < a.length; j++) {
|
||||
const c = a[j]
|
||||
if (c === 'a') all = true
|
||||
else if (c === 's') wantS = true
|
||||
else if (c === 'n') wantN = true
|
||||
else if (c === 'r') wantR = true
|
||||
else if (c === 'm') wantM = true
|
||||
else if (c === 'v') wantV = true
|
||||
else {
|
||||
ctx.console.error('uname: invalid option -- ' + c)
|
||||
ctx.exitCode = 1
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
if (all) {
|
||||
wantS = wantN = wantR = wantM = wantV = true
|
||||
} else if (!wantS && !wantN && !wantR && !wantM && !wantV) {
|
||||
wantS = true
|
||||
}
|
||||
|
||||
let name = 'BareOS'
|
||||
let version = '0.1'
|
||||
@@ -85,10 +144,10 @@ async function run(ctx, argv) {
|
||||
|
||||
const parts = []
|
||||
if (wantS) parts.push(name)
|
||||
if (wantN) parts.push('bare-os')
|
||||
if (wantN) parts.push(nodename)
|
||||
if (wantR) parts.push(version)
|
||||
if (wantV) parts.push('bare-userland')
|
||||
if (wantM) parts.push('unknown')
|
||||
if (wantV) parts.push('bare-userland')
|
||||
|
||||
ctx.console.log(parts.join(' '))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user