buninu-linux 0.8.56 → 0.9.56
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/initramfs/bin/tar +34 -46
- package/initramfs/buninu/apps/bunmsh/CHANGELOG.md +103 -0
- package/initramfs/buninu/apps/bunmsh/PORTING.md +133 -15
- package/initramfs/buninu/apps/bunmsh/help/awk.md +38 -0
- package/initramfs/buninu/apps/bunmsh/help/break.md +21 -0
- package/initramfs/buninu/apps/bunmsh/help/chmod.md +8 -2
- package/initramfs/buninu/apps/bunmsh/help/cmp.md +29 -0
- package/initramfs/buninu/apps/bunmsh/help/continue.md +22 -0
- package/initramfs/buninu/apps/bunmsh/help/cp.md +4 -0
- package/initramfs/buninu/apps/bunmsh/help/diff.md +30 -0
- package/initramfs/buninu/apps/bunmsh/help/egrep.md +26 -0
- package/initramfs/buninu/apps/bunmsh/help/export.md +2 -2
- package/initramfs/buninu/apps/bunmsh/help/expr.md +30 -0
- package/initramfs/buninu/apps/bunmsh/help/fgrep.md +25 -0
- package/initramfs/buninu/apps/bunmsh/help/grep.md +4 -0
- package/initramfs/buninu/apps/bunmsh/help/ls.md +4 -2
- package/initramfs/buninu/apps/bunmsh/help/mkdir.md +1 -1
- package/initramfs/buninu/apps/bunmsh/help/print.md +3 -0
- package/initramfs/buninu/apps/bunmsh/help/return.md +23 -0
- package/initramfs/buninu/apps/bunmsh/help/sed.md +8 -3
- package/initramfs/buninu/apps/bunmsh/help/set.md +12 -3
- package/initramfs/buninu/apps/bunmsh/help/tr.md +4 -0
- package/initramfs/buninu/apps/bunmsh/help/trap.md +22 -0
- package/initramfs/buninu/apps/bunmsh/help/uniq.md +27 -0
- package/initramfs/buninu/apps/bunmsh/package.json +1 -1
- package/initramfs/buninu/apps/bunmsh/src/awk.js +203 -0
- package/initramfs/buninu/apps/bunmsh/src/diff.js +222 -0
- package/initramfs/buninu/apps/bunmsh/src/fancy-ls.js +15 -4
- package/initramfs/buninu/apps/bunmsh/src/main.js +21 -5
- package/initramfs/buninu/apps/bunmsh/src/sed.js +159 -0
- package/initramfs/buninu/apps/bunmsh/src/shell.js +1283 -540
- package/initramfs/buninu/apps/bunmsh/test/awk-reference.test.js +35 -0
- package/initramfs/buninu/apps/bunmsh/test/awk.test.js +62 -0
- package/initramfs/buninu/apps/bunmsh/test/posix-bootstrap.test.js +595 -0
- package/initramfs/buninu/apps/bunmsh/test/utility-reference-cases.mjs +98 -0
- package/initramfs/buninu/apps/bunmsh/test/utility-reference.test.js +23 -0
- package/initramfs/buninu/apps/jsgotty/client-kitty.mjs +61 -0
- package/initramfs/buninu/apps/jsgotty/client.mjs +16 -0
- package/initramfs/buninu/apps/jsgotty/package-lock.json +2 -2
- package/initramfs/buninu/apps/jsgotty/package.json +1 -1
- package/initramfs/buninu/apps/jsgotty/tests/client-kitty.test.mjs +43 -0
- package/initramfs/etc/resolv.conf +2 -1
- package/initramfs/usr/share/doc/buninu-linux/tar.md +11 -0
- package/package.json +1 -1
- package/scripts/fetch.js +2 -0
- package/test/tar-strip.test.js +36 -0
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Adapted from jsmdcui/src/index.js diffgutter (Myers bisect and merge cleanup).
|
|
3
|
+
|
|
4
|
+
MIT License
|
|
5
|
+
|
|
6
|
+
Copyright (c) 2016-2020: Zachary Yedidia, et al.
|
|
7
|
+
Copyright (c) 2026 Dr. John (醫者小智)
|
|
8
|
+
|
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
in the Software without restriction, including without limitation the rights
|
|
12
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
furnished to do so, subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
SOFTWARE.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
const _DIFF_DELETE = -1, _DIFF_INSERT = 1, _DIFF_EQUAL = 0;
|
|
29
|
+
|
|
30
|
+
function _arrEq(a, b) { if (a.length !== b.length) return false; for (let i = 0; i < a.length; i++) if (a[i] !== b[i]) return false; return true; }
|
|
31
|
+
function _commonPrefix(a, b) { const n = Math.min(a.length, b.length); let i = 0; while (i < n && a[i] === b[i]) i++; return i; }
|
|
32
|
+
function _commonSuffix(a, b) { const n = Math.min(a.length, b.length); let i = 0; while (i < n && a[a.length-1-i] === b[b.length-1-i]) i++; return i; }
|
|
33
|
+
function _indexOfSub(longArr, shortArr) { const L = longArr.length, S = shortArr.length; if (S === 0) return 0; outer: for (let i = 0; i + S <= L; i++) { for (let j = 0; j < S; j++) if (longArr[i+j] !== shortArr[j]) continue outer; return i; } return -1; }
|
|
34
|
+
function _hasSuffix(seq, suf) { if (suf.length > seq.length) return false; for (let i = 0; i < suf.length; i++) if (seq[seq.length-suf.length+i] !== suf[i]) return false; return true; }
|
|
35
|
+
function _hasPrefix(seq, pre) { if (pre.length > seq.length) return false; for (let i = 0; i < pre.length; i++) if (seq[i] !== pre[i]) return false; return true; }
|
|
36
|
+
|
|
37
|
+
function _diffMain(a, b) {
|
|
38
|
+
if (_arrEq(a, b)) return a.length ? [{ op: _DIFF_EQUAL, seq: a.slice() }] : [];
|
|
39
|
+
const pre = _commonPrefix(a, b);
|
|
40
|
+
const commonprefix = a.slice(0, pre);
|
|
41
|
+
a = a.slice(pre); b = b.slice(pre);
|
|
42
|
+
const suf = _commonSuffix(a, b);
|
|
43
|
+
const commonsuffix = a.slice(a.length - suf);
|
|
44
|
+
a = a.slice(0, a.length - suf); b = b.slice(0, b.length - suf);
|
|
45
|
+
const diffs = _diffCompute(a, b);
|
|
46
|
+
if (commonprefix.length) diffs.unshift({ op: _DIFF_EQUAL, seq: commonprefix });
|
|
47
|
+
if (commonsuffix.length) diffs.push({ op: _DIFF_EQUAL, seq: commonsuffix });
|
|
48
|
+
return _diffCleanupMerge(diffs);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function _diffCompute(a, b) {
|
|
52
|
+
if (a.length === 0) return [{ op: _DIFF_INSERT, seq: b.slice() }];
|
|
53
|
+
if (b.length === 0) return [{ op: _DIFF_DELETE, seq: a.slice() }];
|
|
54
|
+
const aLonger = a.length > b.length;
|
|
55
|
+
const long = aLonger ? a : b, short = aLonger ? b : a;
|
|
56
|
+
const idx = _indexOfSub(long, short);
|
|
57
|
+
if (idx !== -1) {
|
|
58
|
+
const op = aLonger ? _DIFF_DELETE : _DIFF_INSERT;
|
|
59
|
+
return [
|
|
60
|
+
{ op, seq: long.slice(0, idx) },
|
|
61
|
+
{ op: _DIFF_EQUAL, seq: short.slice() },
|
|
62
|
+
{ op, seq: long.slice(idx + short.length) },
|
|
63
|
+
];
|
|
64
|
+
}
|
|
65
|
+
if (short.length === 1) return [{ op: _DIFF_DELETE, seq: a.slice() }, { op: _DIFF_INSERT, seq: b.slice() }];
|
|
66
|
+
return _diffBisect(a, b);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function _diffBisect(a, b) {
|
|
70
|
+
const n = a.length, m = b.length;
|
|
71
|
+
const maxD = Math.ceil((n + m) / 2);
|
|
72
|
+
const vOffset = maxD, vLength = 2 * maxD;
|
|
73
|
+
const v1 = new Int32Array(vLength).fill(-1);
|
|
74
|
+
const v2 = new Int32Array(vLength).fill(-1);
|
|
75
|
+
v1[vOffset + 1] = 0; v2[vOffset + 1] = 0;
|
|
76
|
+
const delta = n - m;
|
|
77
|
+
const front = (delta % 2 !== 0);
|
|
78
|
+
let k1start = 0, k1end = 0, k2start = 0, k2end = 0;
|
|
79
|
+
for (let d = 0; d < maxD; d++) {
|
|
80
|
+
for (let k1 = -d + k1start; k1 <= d - k1end; k1 += 2) {
|
|
81
|
+
const k1Offset = vOffset + k1;
|
|
82
|
+
let x1;
|
|
83
|
+
if (k1 === -d || (k1 !== d && v1[k1Offset-1] < v1[k1Offset+1])) x1 = v1[k1Offset+1];
|
|
84
|
+
else x1 = v1[k1Offset-1] + 1;
|
|
85
|
+
let y1 = x1 - k1;
|
|
86
|
+
while (x1 < n && y1 < m && a[x1] === b[y1]) { x1++; y1++; }
|
|
87
|
+
v1[k1Offset] = x1;
|
|
88
|
+
if (x1 > n) k1end += 2;
|
|
89
|
+
else if (y1 > m) k1start += 2;
|
|
90
|
+
else if (front) {
|
|
91
|
+
const k2Offset = vOffset + delta - k1;
|
|
92
|
+
if (k2Offset >= 0 && k2Offset < vLength && v2[k2Offset] !== -1) {
|
|
93
|
+
const x2 = n - v2[k2Offset];
|
|
94
|
+
if (x1 >= x2) return _diffBisectSplit(a, b, x1, y1);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
for (let k2 = -d + k2start; k2 <= d - k2end; k2 += 2) {
|
|
99
|
+
const k2Offset = vOffset + k2;
|
|
100
|
+
let x2;
|
|
101
|
+
if (k2 === -d || (k2 !== d && v2[k2Offset-1] < v2[k2Offset+1])) x2 = v2[k2Offset+1];
|
|
102
|
+
else x2 = v2[k2Offset-1] + 1;
|
|
103
|
+
let y2 = x2 - k2;
|
|
104
|
+
while (x2 < n && y2 < m && a[n-x2-1] === b[m-y2-1]) { x2++; y2++; }
|
|
105
|
+
v2[k2Offset] = x2;
|
|
106
|
+
if (x2 > n) k2end += 2;
|
|
107
|
+
else if (y2 > m) k2start += 2;
|
|
108
|
+
else if (!front) {
|
|
109
|
+
const k1Offset = vOffset + delta - k2;
|
|
110
|
+
if (k1Offset >= 0 && k1Offset < vLength && v1[k1Offset] !== -1) {
|
|
111
|
+
const x1 = v1[k1Offset];
|
|
112
|
+
const y1 = vOffset + x1 - k1Offset;
|
|
113
|
+
const x2real = n - x2;
|
|
114
|
+
if (x1 >= x2real) return _diffBisectSplit(a, b, x1, y1);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return [{ op: _DIFF_DELETE, seq: a.slice() }, { op: _DIFF_INSERT, seq: b.slice() }];
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function _diffBisectSplit(a, b, x, y) {
|
|
123
|
+
return _diffMain(a.slice(0, x), b.slice(0, y)).concat(_diffMain(a.slice(x), b.slice(y)));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function _diffCleanupMerge(diffs) {
|
|
127
|
+
diffs.push({ op: _DIFF_EQUAL, seq: [] });
|
|
128
|
+
let pointer = 0, countDelete = 0, countInsert = 0, textDelete = [], textInsert = [];
|
|
129
|
+
while (pointer < diffs.length) {
|
|
130
|
+
const d = diffs[pointer];
|
|
131
|
+
if (d.op === _DIFF_INSERT) { countInsert++; textInsert = textInsert.concat(d.seq); pointer++; }
|
|
132
|
+
else if (d.op === _DIFF_DELETE) { countDelete++; textDelete = textDelete.concat(d.seq); pointer++; }
|
|
133
|
+
else {
|
|
134
|
+
if (countDelete + countInsert > 1) {
|
|
135
|
+
if (countDelete !== 0 && countInsert !== 0) {
|
|
136
|
+
let cl = _commonPrefix(textInsert, textDelete);
|
|
137
|
+
if (cl !== 0) {
|
|
138
|
+
const x = pointer - countDelete - countInsert;
|
|
139
|
+
if (x > 0 && diffs[x-1].op === _DIFF_EQUAL) diffs[x-1].seq = diffs[x-1].seq.concat(textInsert.slice(0, cl));
|
|
140
|
+
else { diffs.unshift({ op: _DIFF_EQUAL, seq: textInsert.slice(0, cl) }); pointer++; }
|
|
141
|
+
textInsert = textInsert.slice(cl); textDelete = textDelete.slice(cl);
|
|
142
|
+
}
|
|
143
|
+
cl = _commonSuffix(textInsert, textDelete);
|
|
144
|
+
if (cl !== 0) {
|
|
145
|
+
const insIdx = textInsert.length - cl;
|
|
146
|
+
diffs[pointer].seq = textInsert.slice(insIdx).concat(diffs[pointer].seq);
|
|
147
|
+
textInsert = textInsert.slice(0, insIdx); textDelete = textDelete.slice(0, textDelete.length - cl);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
const repl = [];
|
|
151
|
+
if (textDelete.length) repl.push({ op: _DIFF_DELETE, seq: textDelete });
|
|
152
|
+
if (textInsert.length) repl.push({ op: _DIFF_INSERT, seq: textInsert });
|
|
153
|
+
const start = pointer - countDelete - countInsert;
|
|
154
|
+
diffs.splice(start, countDelete + countInsert, ...repl);
|
|
155
|
+
pointer = start + repl.length + 1;
|
|
156
|
+
} else if (pointer !== 0 && diffs[pointer-1].op === _DIFF_EQUAL) {
|
|
157
|
+
diffs[pointer-1].seq = diffs[pointer-1].seq.concat(d.seq);
|
|
158
|
+
diffs.splice(pointer, 1);
|
|
159
|
+
} else pointer++;
|
|
160
|
+
countInsert = 0; countDelete = 0; textDelete = []; textInsert = [];
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (diffs[diffs.length-1].seq.length === 0) diffs.pop();
|
|
164
|
+
|
|
165
|
+
// Second pass: shift a single edit sandwiched between two equalities sideways.
|
|
166
|
+
let changes = false;
|
|
167
|
+
pointer = 1;
|
|
168
|
+
while (pointer < diffs.length - 1) {
|
|
169
|
+
const prev = diffs[pointer-1], cur = diffs[pointer], next = diffs[pointer+1];
|
|
170
|
+
if (prev.op === _DIFF_EQUAL && next.op === _DIFF_EQUAL) {
|
|
171
|
+
if (_hasSuffix(cur.seq, prev.seq)) {
|
|
172
|
+
if (prev.seq.length) {
|
|
173
|
+
cur.seq = prev.seq.concat(cur.seq.slice(0, cur.seq.length - prev.seq.length));
|
|
174
|
+
next.seq = prev.seq.concat(next.seq);
|
|
175
|
+
}
|
|
176
|
+
diffs.splice(pointer-1, 1); changes = true;
|
|
177
|
+
} else if (_hasPrefix(cur.seq, next.seq)) {
|
|
178
|
+
prev.seq = prev.seq.concat(next.seq);
|
|
179
|
+
cur.seq = cur.seq.slice(next.seq.length).concat(next.seq);
|
|
180
|
+
diffs.splice(pointer+1, 1); changes = true;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
pointer++;
|
|
184
|
+
}
|
|
185
|
+
if (changes) return _diffCleanupMerge(diffs);
|
|
186
|
+
return diffs;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export function normalFileDiff(left, right) {
|
|
190
|
+
// Keep the terminator as part of line identity, including a missing final LF.
|
|
191
|
+
const lines = text => text.match(/[^\n]*\n|[^\n]+$/g) ?? [];
|
|
192
|
+
const a = lines(left), b = lines(right);
|
|
193
|
+
const operations = _diffMain(a, b);
|
|
194
|
+
const range = (first, last) => first === last ? String(first) : `${first},${last}`;
|
|
195
|
+
const render = (values, prefix) => values.map(line =>
|
|
196
|
+
`${prefix} ${line}${line.endsWith("\n") ? "" : "\n\\n"}`
|
|
197
|
+
).join("");
|
|
198
|
+
let oldLine = 0, newLine = 0, output = "";
|
|
199
|
+
for (let i = 0; i < operations.length;) {
|
|
200
|
+
if (operations[i].op === _DIFF_EQUAL) {
|
|
201
|
+
oldLine += operations[i].seq.length;
|
|
202
|
+
newLine += operations[i++].seq.length;
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
205
|
+
const oldStart = oldLine, newStart = newLine;
|
|
206
|
+
let removed = [], added = [];
|
|
207
|
+
while (i < operations.length && operations[i].op !== _DIFF_EQUAL) {
|
|
208
|
+
const change = operations[i++];
|
|
209
|
+
if (change.op === _DIFF_DELETE) {
|
|
210
|
+
removed = removed.concat(change.seq);
|
|
211
|
+
oldLine += change.seq.length;
|
|
212
|
+
} else {
|
|
213
|
+
added = added.concat(change.seq);
|
|
214
|
+
newLine += change.seq.length;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
if (!removed.length) output += `${oldStart}a${range(newStart + 1, newLine)}\n${render(added, ">")}`;
|
|
218
|
+
else if (!added.length) output += `${range(oldStart + 1, oldLine)}d${newStart}\n${render(removed, "<")}`;
|
|
219
|
+
else output += `${range(oldStart + 1, oldLine)}c${range(newStart + 1, newLine)}\n${render(removed, "<")}---\n${render(added, ">")}`;
|
|
220
|
+
}
|
|
221
|
+
return output;
|
|
222
|
+
}
|
|
@@ -43,8 +43,8 @@ function classifySuffix(stats) {
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
function displayEntry(entry, options) {
|
|
46
|
-
const suffix = options.classify ? classifySuffix(entry.stats) : entry.stats.isDirectory() ? "/" : "";
|
|
47
|
-
return `${iconFor(entry.name, entry.stats, entry.path)} ${entry.name}${suffix}`;
|
|
46
|
+
const suffix = options.classify ? classifySuffix(entry.stats) : !options.plain && entry.stats.isDirectory() ? "/" : "";
|
|
47
|
+
return `${options.inode ? `${entry.stats.ino} ` : ""}${options.plain ? "" : `${iconFor(entry.name, entry.stats, entry.path)} `}${entry.name}${suffix}`;
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
function columns(items, width) {
|
|
@@ -98,7 +98,7 @@ function listDirectory(path, options) {
|
|
|
98
98
|
.filter((entry) => options.all || options.almostAll || !entry.name.startsWith("."))
|
|
99
99
|
.map((entry) => {
|
|
100
100
|
const entryPath = resolve(path, entry.name);
|
|
101
|
-
return { name: entry.name, stats: lstatSync(entryPath), path: entryPath };
|
|
101
|
+
return { name: entry.name, stats: options.dereference ? statSync(entryPath) : lstatSync(entryPath), path: entryPath };
|
|
102
102
|
});
|
|
103
103
|
if (options.all) entries = [
|
|
104
104
|
{ name: ".", stats: lstatSync(path), path },
|
|
@@ -116,6 +116,7 @@ function listDirectory(path, options) {
|
|
|
116
116
|
|
|
117
117
|
export function fancyLs(argv, state, terminal = Boolean(process.stdout.isTTY)) {
|
|
118
118
|
const options = {
|
|
119
|
+
plain: argv[0] === "ls" && Boolean(state.shellOptions?.posix),
|
|
119
120
|
all: false, almostAll: false, directory: false, long: false, human: false,
|
|
120
121
|
recursive: false, time: false, reverse: false, size: false, one: false, classify: false,
|
|
121
122
|
};
|
|
@@ -128,6 +129,8 @@ export function fancyLs(argv, state, terminal = Boolean(process.stdout.isTTY)) {
|
|
|
128
129
|
for (const flag of argument.slice(1)) {
|
|
129
130
|
if (flag === "a") options.all = true;
|
|
130
131
|
else if (flag === "A") options.almostAll = true;
|
|
132
|
+
else if (flag === "L") options.dereference = true;
|
|
133
|
+
else if (flag === "i") options.inode = true;
|
|
131
134
|
else if (flag === "d") options.directory = true;
|
|
132
135
|
else if (flag === "l") options.long = true;
|
|
133
136
|
else if (flag === "h") options.human = true;
|
|
@@ -148,7 +151,7 @@ export function fancyLs(argv, state, terminal = Boolean(process.stdout.isTTY)) {
|
|
|
148
151
|
const show = (operand, heading = operands.length > 1, separate = true) => {
|
|
149
152
|
const path = isAbsolute(operand) ? operand : resolve(state.cwd, operand);
|
|
150
153
|
try {
|
|
151
|
-
const stats = lstatSync(path);
|
|
154
|
+
const stats = options.dereference ? statSync(path) : lstatSync(path);
|
|
152
155
|
let listingDirectory = stats.isDirectory() && !options.directory;
|
|
153
156
|
// GNU/POSIX-style ls follows a command-line symlink to a directory for
|
|
154
157
|
// an ordinary listing and for -R. -d, -l, and -F request information
|
|
@@ -196,6 +199,14 @@ export function fancyLs(argv, state, terminal = Boolean(process.stdout.isTTY)) {
|
|
|
196
199
|
stderr += `bunmsh: ${argv[0]}: ${operand}: ${error.message}\n`;
|
|
197
200
|
}
|
|
198
201
|
};
|
|
202
|
+
if (options.time || options.size) operands.sort((a,b) => {
|
|
203
|
+
try {
|
|
204
|
+
const stat = options.dereference ? statSync : lstatSync;
|
|
205
|
+
const left=stat(resolve(state.cwd,a)), right=stat(resolve(state.cwd,b));
|
|
206
|
+
const order=(options.size ? right.size-left.size : right.mtimeMs-left.mtimeMs) || a.localeCompare(b);
|
|
207
|
+
return options.reverse ? -order : order;
|
|
208
|
+
} catch { return 0; }
|
|
209
|
+
});
|
|
199
210
|
for (const operand of operands) show(operand);
|
|
200
211
|
return { status, stdout, stderr };
|
|
201
212
|
}
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
createState,
|
|
7
7
|
execute,
|
|
8
8
|
executeArgv,
|
|
9
|
+
finishShell,
|
|
9
10
|
executeBunShellFallback,
|
|
10
11
|
isJavaScriptMode,
|
|
11
12
|
needsMoreInput,
|
|
@@ -440,6 +441,10 @@ async function interactive(state) {
|
|
|
440
441
|
}
|
|
441
442
|
};
|
|
442
443
|
const interrupt = () => {
|
|
444
|
+
if (Object.hasOwn(state.traps,"SIGINT")) {
|
|
445
|
+
if (state.traps.SIGINT && !state.pendingTraps.includes("SIGINT")) state.pendingTraps.push("SIGINT");
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
443
448
|
state.lastStatus = 130;
|
|
444
449
|
if (foregroundCommand || promptAbort?.signal.aborted) return;
|
|
445
450
|
process.stdout.write("\n");
|
|
@@ -456,6 +461,7 @@ async function interactive(state) {
|
|
|
456
461
|
// periodic autosave.
|
|
457
462
|
const SIGNAL_EXIT_STATUS = { SIGHUP: 129, SIGTERM: 143 };
|
|
458
463
|
const terminate = (signal) => {
|
|
464
|
+
if (Object.hasOwn(state.traps,signal)) return;
|
|
459
465
|
state.exitRequested = true;
|
|
460
466
|
state.exitStatus = SIGNAL_EXIT_STATUS[signal] ?? state.exitStatus;
|
|
461
467
|
if (foregroundCommand) {
|
|
@@ -725,7 +731,7 @@ async function interactive(state) {
|
|
|
725
731
|
process.off("SIGHUP", terminate);
|
|
726
732
|
readline.close();
|
|
727
733
|
}
|
|
728
|
-
return state.exitRequested ? state.exitStatus : state.lastStatus;
|
|
734
|
+
return (await finishShell(state,state.exitRequested ? state.exitStatus : state.lastStatus)).status;
|
|
729
735
|
}
|
|
730
736
|
|
|
731
737
|
async function main(argv) {
|
|
@@ -736,6 +742,7 @@ async function main(argv) {
|
|
|
736
742
|
let forceInteractive = false;
|
|
737
743
|
let forceMouse = false;
|
|
738
744
|
let builtinOnly = false;
|
|
745
|
+
const shellOptions = {};
|
|
739
746
|
let i = 0;
|
|
740
747
|
for (; i < argv.length; i++) {
|
|
741
748
|
const arg = argv[i];
|
|
@@ -785,6 +792,10 @@ async function main(argv) {
|
|
|
785
792
|
await printChangelog();
|
|
786
793
|
return 0;
|
|
787
794
|
}
|
|
795
|
+
if (/^-[eufxv]+$/.test(arg)) {
|
|
796
|
+
for (const key of arg.slice(1)) shellOptions[key]=true;
|
|
797
|
+
continue;
|
|
798
|
+
}
|
|
788
799
|
if (arg.startsWith("-")) {
|
|
789
800
|
console.error(`bunmsh: ${arg}: unknown option`);
|
|
790
801
|
return 2;
|
|
@@ -801,7 +812,7 @@ async function main(argv) {
|
|
|
801
812
|
if (forceMouse) state.mouseTracking = true;
|
|
802
813
|
if (builtinOnly) state.pathSearch = false;
|
|
803
814
|
const result = await executeArgv(forwardedArgv, state);
|
|
804
|
-
return state.exitRequested ? state.exitStatus : result.status;
|
|
815
|
+
return (await finishShell(state,state.exitRequested ? state.exitStatus : result.status)).status;
|
|
805
816
|
}
|
|
806
817
|
|
|
807
818
|
if (command !== null) {
|
|
@@ -813,8 +824,10 @@ async function main(argv) {
|
|
|
813
824
|
mouseTracking: forceMouse || undefined,
|
|
814
825
|
pathSearch: builtinOnly ? false : undefined,
|
|
815
826
|
});
|
|
827
|
+
Object.assign(state.shellOptions,shellOptions);
|
|
828
|
+
if (state.shellOptions.v) console.error(command);
|
|
816
829
|
const result = await execute(command, state);
|
|
817
|
-
return state.exitRequested ? state.exitStatus : result.status;
|
|
830
|
+
return (await finishShell(state,state.exitRequested ? state.exitStatus : result.status)).status;
|
|
818
831
|
}
|
|
819
832
|
|
|
820
833
|
if (i < argv.length) {
|
|
@@ -826,9 +839,11 @@ async function main(argv) {
|
|
|
826
839
|
mouseTracking: forceMouse || undefined,
|
|
827
840
|
pathSearch: builtinOnly ? false : undefined,
|
|
828
841
|
});
|
|
842
|
+
Object.assign(state.shellOptions,shellOptions);
|
|
843
|
+
if (state.shellOptions.v) console.error(source);
|
|
829
844
|
const result = await execute(source, state);
|
|
830
845
|
if (forceInteractive && !state.exitRequested) return interactive(state);
|
|
831
|
-
return state.exitRequested ? state.exitStatus : result.status;
|
|
846
|
+
return (await finishShell(state,state.exitRequested ? state.exitStatus : result.status)).status;
|
|
832
847
|
} catch (error) {
|
|
833
848
|
console.error(`bunmsh: ${script}: ${error.message}`);
|
|
834
849
|
return 1;
|
|
@@ -839,10 +854,11 @@ async function main(argv) {
|
|
|
839
854
|
mouseTracking: forceMouse || undefined,
|
|
840
855
|
pathSearch: builtinOnly ? false : undefined,
|
|
841
856
|
});
|
|
857
|
+
Object.assign(state.shellOptions,shellOptions);
|
|
842
858
|
if (forceInteractive || process.stdin.isTTY) return interactive(state);
|
|
843
859
|
const source = await Bun.stdin.text();
|
|
844
860
|
const result = await execute(source, state);
|
|
845
|
-
return state.exitRequested ? state.exitStatus : result.status;
|
|
861
|
+
return (await finishShell(state,state.exitRequested ? state.exitStatus : result.status)).status;
|
|
846
862
|
}
|
|
847
863
|
|
|
848
864
|
// Lets --define process.env.SERVE_AUTO_OPEN=/index.html, =off, or =yes
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
// The fallback operates on pattern/hold spaces, with a program counter for
|
|
2
|
+
// branches. Parsing delimiters before command separators preserves s/;/x/.
|
|
3
|
+
export function compileSed(script, regexSource) {
|
|
4
|
+
let i = 0, lastPattern = null;
|
|
5
|
+
const commands = [], groups = [], labels = new Map();
|
|
6
|
+
const space = () => { while (/[ \t\r]/.test(script[i] ?? "") && i < script.length) i++; };
|
|
7
|
+
const field = delimiter => {
|
|
8
|
+
let value = "";
|
|
9
|
+
for (; i < script.length; i++) {
|
|
10
|
+
const ch = script[i];
|
|
11
|
+
if (ch === delimiter) { i++; return value; }
|
|
12
|
+
if (ch === "\\" && i + 1 < script.length) {
|
|
13
|
+
const next = script[++i];
|
|
14
|
+
value += next === delimiter && !'.*[]^$\\'.includes(next) ? next : `\\${next}`;
|
|
15
|
+
} else value += ch;
|
|
16
|
+
}
|
|
17
|
+
throw new Error("unterminated sed field");
|
|
18
|
+
};
|
|
19
|
+
const regex = pattern => {
|
|
20
|
+
if (pattern) lastPattern = pattern;
|
|
21
|
+
if (lastPattern === null) throw new Error("no previous regular expression");
|
|
22
|
+
return regexSource(lastPattern);
|
|
23
|
+
};
|
|
24
|
+
const address = () => {
|
|
25
|
+
if (script[i] === "$") { i++; return {last:true}; }
|
|
26
|
+
if (/\d/.test(script[i] ?? "")) { const match = /^\d+/.exec(script.slice(i))[0]; i += match.length; return {line:Number(match)}; }
|
|
27
|
+
if (script[i] === "/") { i++; return {regex:new RegExp(regex(field("/")))}; }
|
|
28
|
+
return null;
|
|
29
|
+
};
|
|
30
|
+
while (i < script.length) {
|
|
31
|
+
space();
|
|
32
|
+
if (";\n".includes(script[i])) { i++; continue; }
|
|
33
|
+
if (script[i] === "#") { while (i < script.length && script[i] !== "\n") i++; continue; }
|
|
34
|
+
if (i >= script.length) break;
|
|
35
|
+
const first = address(); space();
|
|
36
|
+
let second = null;
|
|
37
|
+
if (script[i] === ",") { i++; space(); second = address(); if (!second) throw new Error("missing range address"); }
|
|
38
|
+
space(); const invert = script[i] === "!"; if (invert) { i++; space(); }
|
|
39
|
+
const op = script[i++], command = {op, first, second, invert};
|
|
40
|
+
const index = commands.length;
|
|
41
|
+
if (op === "{") groups.push(index);
|
|
42
|
+
else if (op === "}") { const open = groups.pop(); if (open === undefined) throw new Error("unexpected }"); commands[open].end = index; }
|
|
43
|
+
else if (op === "s" || op === "y") {
|
|
44
|
+
const delimiter = script[i++];
|
|
45
|
+
const pattern = field(delimiter), replacement = field(delimiter);
|
|
46
|
+
if (op === "y") {
|
|
47
|
+
const unescape = s => s.replace(/\\n/g,"\n").replace(/\\(.)/g,"$1");
|
|
48
|
+
command.from = unescape(pattern); command.to = unescape(replacement);
|
|
49
|
+
if (command.from.length !== command.to.length) throw new Error("unequal y strings");
|
|
50
|
+
} else {
|
|
51
|
+
const flags = /^[gp0-9]*/.exec(script.slice(i))[0]; i += flags.length;
|
|
52
|
+
command.regex = new RegExp(regex(pattern), "g");
|
|
53
|
+
command.replacement = replacement;
|
|
54
|
+
command.global = flags.includes("g"); command.print = flags.includes("p");
|
|
55
|
+
command.occurrence = Number(/\d+/.exec(flags)?.[0] ?? 1);
|
|
56
|
+
}
|
|
57
|
+
} else if ([":","b","t","T"].includes(op)) {
|
|
58
|
+
space(); const start = i; while (i < script.length && !";\n}".includes(script[i])) i++;
|
|
59
|
+
command.label = script.slice(start,i).trim();
|
|
60
|
+
if (op === ":") labels.set(command.label,index);
|
|
61
|
+
} else if (op === "r") {
|
|
62
|
+
space(); const start=i; while (i<script.length && script[i]!=="\n") i++;
|
|
63
|
+
command.file=script.slice(start,i);
|
|
64
|
+
if (!command.file) throw new Error("r requires a filename");
|
|
65
|
+
} else if (["a","i","c"].includes(op)) {
|
|
66
|
+
space(); if (script[i] === "\\") { i++; if (script[i] === "\n") i++; }
|
|
67
|
+
let text = "";
|
|
68
|
+
while (i < script.length && script[i] !== "\n") {
|
|
69
|
+
if (script[i] === "\\" && script[i+1] === "\n") { text += "\n"; i+=2; }
|
|
70
|
+
else text += script[i++];
|
|
71
|
+
}
|
|
72
|
+
command.text = text;
|
|
73
|
+
} else if (!"pPdDqnhHgGxN=".includes(op ?? "")) throw new Error(`unsupported sed command: ${op}`);
|
|
74
|
+
commands.push(command);
|
|
75
|
+
}
|
|
76
|
+
if (groups.length) throw new Error("unclosed sed group");
|
|
77
|
+
for (const command of commands) if (["b","t","T"].includes(command.op)) {
|
|
78
|
+
command.target = command.label ? labels.get(command.label) : commands.length;
|
|
79
|
+
if (command.target === undefined) throw new Error(`undefined sed label: ${command.label}`);
|
|
80
|
+
}
|
|
81
|
+
return commands;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function replacementText(template, args) {
|
|
85
|
+
let output = "";
|
|
86
|
+
for (let i = 0; i < template.length; i++) {
|
|
87
|
+
if (template[i] === "&") output += args[0];
|
|
88
|
+
else if (template[i] === "\\" && i + 1 < template.length) {
|
|
89
|
+
const ch = template[++i];
|
|
90
|
+
output += /[1-9]/.test(ch) ? (args[Number(ch)] ?? "") : ch === "n" ? "\n" : ch;
|
|
91
|
+
} else output += template[i];
|
|
92
|
+
}
|
|
93
|
+
return output;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function applySed(text, commands, quiet, readFile = () => "") {
|
|
97
|
+
if (!text) return "";
|
|
98
|
+
const trailing = text.endsWith("\n"), lines = text.split("\n");
|
|
99
|
+
if (trailing) lines.pop();
|
|
100
|
+
const active = new Set(), output = [];
|
|
101
|
+
let hold = "";
|
|
102
|
+
for (let index = 0; index < lines.length; index++) {
|
|
103
|
+
let pattern = lines[index], deleted = false, changed = false, quit = false;
|
|
104
|
+
const appended = [];
|
|
105
|
+
const emit = value => output.push(value + (index < lines.length-1 || trailing ? "\n" : ""));
|
|
106
|
+
const matches = a => a.last ? index === lines.length-1 : a.line !== undefined ? index+1 === a.line : a.regex.test(pattern);
|
|
107
|
+
for (let pc = 0; pc < commands.length; pc++) {
|
|
108
|
+
const c = commands[pc];
|
|
109
|
+
let selected = !c.first || matches(c.first);
|
|
110
|
+
if (c.second) {
|
|
111
|
+
const wasActive = active.has(pc);
|
|
112
|
+
selected ||= wasActive;
|
|
113
|
+
if (selected) {
|
|
114
|
+
active.add(pc);
|
|
115
|
+
if ((wasActive || !c.second.regex) && (c.second.line !== undefined ? index+1 >= c.second.line : matches(c.second))) active.delete(pc);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (c.invert) selected = !selected;
|
|
119
|
+
if (!selected) { if (c.op === "{") pc = c.end; continue; }
|
|
120
|
+
if (c.op === "s") {
|
|
121
|
+
let count = 0, replaced = false;
|
|
122
|
+
pattern = pattern.replace(c.regex,(...args) => {
|
|
123
|
+
count++;
|
|
124
|
+
if (count !== c.occurrence && !(c.global && count >= c.occurrence)) return args[0];
|
|
125
|
+
replaced = true; return replacementText(c.replacement,args);
|
|
126
|
+
});
|
|
127
|
+
changed ||= replaced;
|
|
128
|
+
if (replaced && c.print) emit(pattern);
|
|
129
|
+
} else if (c.op === "y") pattern = [...pattern].map(ch => { const at=c.from.indexOf(ch); return at<0 ? ch : c.to[at]; }).join("");
|
|
130
|
+
else if (c.op === "p") emit(pattern);
|
|
131
|
+
else if (c.op === "P") emit(pattern.split("\n")[0]);
|
|
132
|
+
else if (c.op === "=") emit(String(index+1));
|
|
133
|
+
else if (c.op === "h") hold = pattern;
|
|
134
|
+
else if (c.op === "H") hold += "\n" + pattern;
|
|
135
|
+
else if (c.op === "g") pattern = hold;
|
|
136
|
+
else if (c.op === "G") pattern += "\n" + hold;
|
|
137
|
+
else if (c.op === "x") [pattern,hold] = [hold,pattern];
|
|
138
|
+
else if (c.op === "d") { deleted=true; break; }
|
|
139
|
+
else if (c.op === "D") { const nl=pattern.indexOf("\n"); if (nl<0) { deleted=true; break; } pattern=pattern.slice(nl+1); pc=-1; }
|
|
140
|
+
else if (c.op === "q") { quit=true; break; }
|
|
141
|
+
else if (c.op === "n" || c.op === "N") {
|
|
142
|
+
if (c.op === "n" && !quiet) emit(pattern);
|
|
143
|
+
if (index+1 >= lines.length) { deleted = c.op === "n"; quit=true; break; }
|
|
144
|
+
pattern = (c.op === "N" ? pattern + "\n" : "") + lines[++index]; changed=false;
|
|
145
|
+
} else if (["b","t","T"].includes(c.op)) {
|
|
146
|
+
const take = c.op === "b" || (c.op === "t" ? changed : !changed);
|
|
147
|
+
if (c.op !== "b") changed=false;
|
|
148
|
+
if (take) pc=c.target-1;
|
|
149
|
+
} else if (c.op === "r") appended.push(readFile(c.file));
|
|
150
|
+
else if (c.op === "a") appended.push(c.text+"\n");
|
|
151
|
+
else if (c.op === "i") output.push(c.text+"\n");
|
|
152
|
+
else if (c.op === "c") { if (!c.second || !active.has(pc)) output.push(c.text+"\n"); deleted=true; break; }
|
|
153
|
+
}
|
|
154
|
+
if (!quiet && !deleted) emit(pattern);
|
|
155
|
+
output.push(...appended);
|
|
156
|
+
if (quit) break;
|
|
157
|
+
}
|
|
158
|
+
return output.join("");
|
|
159
|
+
}
|