@machina.at/xsh 19.81.0
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/main.cjs +4875 -0
- package/package.json +15 -0
package/main.cjs
ADDED
|
@@ -0,0 +1,4875 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __esm = (fn, res) => function __init() {
|
|
8
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
9
|
+
};
|
|
10
|
+
var __commonJS = (cb, mod) => function __require() {
|
|
11
|
+
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
12
|
+
};
|
|
13
|
+
var __export = (target, all) => {
|
|
14
|
+
for (var name in all)
|
|
15
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
16
|
+
};
|
|
17
|
+
var __copyProps = (to, from, except, desc) => {
|
|
18
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
19
|
+
for (let key of __getOwnPropNames(from))
|
|
20
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
21
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
22
|
+
}
|
|
23
|
+
return to;
|
|
24
|
+
};
|
|
25
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
26
|
+
|
|
27
|
+
// projects/@machina.at/shell/src/commands/ls.js
|
|
28
|
+
var require_ls = __commonJS({
|
|
29
|
+
"projects/@machina.at/shell/src/commands/ls.js"(exports2, module2) {
|
|
30
|
+
"use strict";
|
|
31
|
+
function formatMode(mode, isDir, isLink) {
|
|
32
|
+
var type = isDir ? "d" : isLink ? "l" : "-";
|
|
33
|
+
var perms = "";
|
|
34
|
+
for (var i = 2; i >= 0; i--) {
|
|
35
|
+
var shift = i * 3;
|
|
36
|
+
perms += mode & 4 << shift ? "r" : "-";
|
|
37
|
+
perms += mode & 2 << shift ? "w" : "-";
|
|
38
|
+
perms += mode & 1 << shift ? "x" : "-";
|
|
39
|
+
}
|
|
40
|
+
return type + perms;
|
|
41
|
+
}
|
|
42
|
+
function pad(s, n, alignLeft) {
|
|
43
|
+
s = String(s);
|
|
44
|
+
while (s.length < n) s = alignLeft ? s + " " : " " + s;
|
|
45
|
+
return s;
|
|
46
|
+
}
|
|
47
|
+
function pad2(n) {
|
|
48
|
+
return n < 10 ? "0" + n : String(n);
|
|
49
|
+
}
|
|
50
|
+
module2.exports = function ls(args, ctx) {
|
|
51
|
+
var showAll = false;
|
|
52
|
+
var longFormat = false;
|
|
53
|
+
var targets = [];
|
|
54
|
+
for (var i = 0; i < args.length; i++) {
|
|
55
|
+
var a = args[i];
|
|
56
|
+
if (a === "-a" || a === "--all" || a === "-A") {
|
|
57
|
+
showAll = true;
|
|
58
|
+
} else if (a === "-l") {
|
|
59
|
+
longFormat = true;
|
|
60
|
+
} else if (a === "-la" || a === "-al" || a === "-lA" || a === "-Al") {
|
|
61
|
+
showAll = true;
|
|
62
|
+
longFormat = true;
|
|
63
|
+
} else if (!a.startsWith("-")) {
|
|
64
|
+
targets.push(a);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (targets.length === 0) targets.push(".");
|
|
68
|
+
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
|
|
69
|
+
function fmtEntry(name, stat2) {
|
|
70
|
+
var isDir = stat2.isDirectory && stat2.isDirectory();
|
|
71
|
+
var isLink = stat2.isSymbolicLink && stat2.isSymbolicLink();
|
|
72
|
+
if (!longFormat) {
|
|
73
|
+
var suffix = isDir ? "/" : "";
|
|
74
|
+
return name + suffix + "\n";
|
|
75
|
+
}
|
|
76
|
+
var mode = typeof stat2.mode === "number" ? stat2.mode : isDir ? 493 : 420;
|
|
77
|
+
var perms = formatMode(mode, isDir, isLink);
|
|
78
|
+
var nlink = stat2.nlink || 1;
|
|
79
|
+
var uid = stat2.uid || 0;
|
|
80
|
+
var gid = stat2.gid || 0;
|
|
81
|
+
var size = typeof stat2.size === "number" ? stat2.size : 0;
|
|
82
|
+
var mtime;
|
|
83
|
+
if (stat2.mtime) mtime = new Date(stat2.mtime);
|
|
84
|
+
else if (typeof stat2.mtimeMs === "number") mtime = new Date(stat2.mtimeMs);
|
|
85
|
+
else mtime = /* @__PURE__ */ new Date();
|
|
86
|
+
var dateStr = months[mtime.getMonth()] + " " + pad(mtime.getDate(), 2) + " " + pad2(mtime.getHours()) + ":" + pad2(mtime.getMinutes());
|
|
87
|
+
var suffix2 = isDir ? "/" : "";
|
|
88
|
+
return perms + " " + pad(nlink, 3) + " " + pad(uid, 6, true) + " " + pad(gid, 6, true) + " " + pad(size, 8) + " " + dateStr + " " + name + suffix2 + "\n";
|
|
89
|
+
}
|
|
90
|
+
for (var t = 0; t < targets.length; t++) {
|
|
91
|
+
var target = targets[t];
|
|
92
|
+
var fullPath = target.startsWith("/") ? target : ctx.path.join(ctx.cwd, target);
|
|
93
|
+
try {
|
|
94
|
+
var fstat = ctx.fs.statSync(fullPath);
|
|
95
|
+
if (!fstat.isDirectory()) {
|
|
96
|
+
process.stdout.write(fmtEntry(target, fstat));
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
if (showAll && longFormat) {
|
|
100
|
+
try {
|
|
101
|
+
var dotStat = ctx.fs.statSync(fullPath);
|
|
102
|
+
process.stdout.write(fmtEntry(".", dotStat));
|
|
103
|
+
} catch (e) {
|
|
104
|
+
}
|
|
105
|
+
try {
|
|
106
|
+
var parentStat = ctx.fs.statSync(ctx.path.join(fullPath, ".."));
|
|
107
|
+
process.stdout.write(fmtEntry("..", parentStat));
|
|
108
|
+
} catch (e) {
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
var entries = ctx.fs.readdirSync(fullPath);
|
|
112
|
+
for (var j = 0; j < entries.length; j++) {
|
|
113
|
+
var entry = entries[j];
|
|
114
|
+
if (!showAll && entry.startsWith(".")) continue;
|
|
115
|
+
try {
|
|
116
|
+
var stat = ctx.fs.statSync(ctx.path.join(fullPath, entry));
|
|
117
|
+
process.stdout.write(fmtEntry(entry, stat));
|
|
118
|
+
} catch (e) {
|
|
119
|
+
process.stdout.write(entry + "\n");
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
} catch (err2) {
|
|
123
|
+
process.stderr.write("ls: " + target + ": " + err2.message + "\n");
|
|
124
|
+
return 1;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return 0;
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// projects/@machina.at/shell/src/commands/cat.js
|
|
133
|
+
var require_cat = __commonJS({
|
|
134
|
+
"projects/@machina.at/shell/src/commands/cat.js"(exports2, module2) {
|
|
135
|
+
"use strict";
|
|
136
|
+
module2.exports = function cat2(args, ctx) {
|
|
137
|
+
if (args.length === 0) {
|
|
138
|
+
if (ctx.stdin) {
|
|
139
|
+
process.stdout.write(ctx.stdin);
|
|
140
|
+
return 0;
|
|
141
|
+
}
|
|
142
|
+
process.stderr.write("cat: missing file operand\n");
|
|
143
|
+
return 1;
|
|
144
|
+
}
|
|
145
|
+
for (var i = 0; i < args.length; i++) {
|
|
146
|
+
var filePath = args[i].startsWith("/") ? args[i] : ctx.path.join(ctx.cwd, args[i]);
|
|
147
|
+
try {
|
|
148
|
+
var content = ctx.fs.readFileSync(filePath);
|
|
149
|
+
if (typeof content === "string") {
|
|
150
|
+
process.stdout.write(content);
|
|
151
|
+
} else {
|
|
152
|
+
process.stdout.write(new TextDecoder().decode(content));
|
|
153
|
+
}
|
|
154
|
+
} catch (err2) {
|
|
155
|
+
process.stderr.write("cat: " + args[i] + ": " + err2.message + "\n");
|
|
156
|
+
return 1;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return 0;
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
// projects/@machina.at/shell/src/commands/echo.js
|
|
165
|
+
var require_echo = __commonJS({
|
|
166
|
+
"projects/@machina.at/shell/src/commands/echo.js"(exports2, module2) {
|
|
167
|
+
"use strict";
|
|
168
|
+
module2.exports = function echo(args) {
|
|
169
|
+
var noNewline = false;
|
|
170
|
+
var escapes = false;
|
|
171
|
+
var start = 0;
|
|
172
|
+
for (var i = 0; i < args.length; i++) {
|
|
173
|
+
if (args[i] === "-n") {
|
|
174
|
+
noNewline = true;
|
|
175
|
+
start = i + 1;
|
|
176
|
+
} else if (args[i] === "-e") {
|
|
177
|
+
escapes = true;
|
|
178
|
+
start = i + 1;
|
|
179
|
+
} else if (args[i] === "-ne" || args[i] === "-en") {
|
|
180
|
+
noNewline = true;
|
|
181
|
+
escapes = true;
|
|
182
|
+
start = i + 1;
|
|
183
|
+
} else {
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
var output = args.slice(start).join(" ");
|
|
188
|
+
if (escapes) {
|
|
189
|
+
output = output.replace(/\\n/g, "\n").replace(/\\t/g, " ").replace(/\\r/g, "\r").replace(/\\\\/g, "\\");
|
|
190
|
+
}
|
|
191
|
+
process.stdout.write(output + (noNewline ? "" : "\n"));
|
|
192
|
+
return 0;
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
// projects/@machina.at/shell/src/commands/pwd.js
|
|
198
|
+
var require_pwd = __commonJS({
|
|
199
|
+
"projects/@machina.at/shell/src/commands/pwd.js"(exports2, module2) {
|
|
200
|
+
"use strict";
|
|
201
|
+
module2.exports = function pwd(args, ctx) {
|
|
202
|
+
process.stdout.write(ctx.cwd + "\n");
|
|
203
|
+
return 0;
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
// projects/@machina.at/shell/src/commands/mkdir.js
|
|
209
|
+
var require_mkdir = __commonJS({
|
|
210
|
+
"projects/@machina.at/shell/src/commands/mkdir.js"(exports2, module2) {
|
|
211
|
+
"use strict";
|
|
212
|
+
module2.exports = function mkdir(args, ctx) {
|
|
213
|
+
if (args.length === 0) {
|
|
214
|
+
process.stderr.write("mkdir: missing operand\n");
|
|
215
|
+
return 1;
|
|
216
|
+
}
|
|
217
|
+
var recursive = false;
|
|
218
|
+
var dirs = [];
|
|
219
|
+
for (var i = 0; i < args.length; i++) {
|
|
220
|
+
if (args[i] === "-p") {
|
|
221
|
+
recursive = true;
|
|
222
|
+
} else if (!args[i].startsWith("-")) {
|
|
223
|
+
dirs.push(args[i]);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
for (var j = 0; j < dirs.length; j++) {
|
|
227
|
+
var dirPath = dirs[j].startsWith("/") ? dirs[j] : ctx.path.join(ctx.cwd, dirs[j]);
|
|
228
|
+
try {
|
|
229
|
+
ctx.fs.mkdirSync(dirPath, { recursive });
|
|
230
|
+
} catch (err2) {
|
|
231
|
+
process.stderr.write("mkdir: " + dirs[j] + ": " + err2.message + "\n");
|
|
232
|
+
return 1;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return 0;
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
// projects/@machina.at/shell/src/commands/head.js
|
|
241
|
+
var require_head = __commonJS({
|
|
242
|
+
"projects/@machina.at/shell/src/commands/head.js"(exports2, module2) {
|
|
243
|
+
"use strict";
|
|
244
|
+
module2.exports = function head2(args, ctx) {
|
|
245
|
+
var lines = 10;
|
|
246
|
+
var files = [];
|
|
247
|
+
for (var i = 0; i < args.length; i++) {
|
|
248
|
+
if (args[i] === "-n" && args[i + 1]) {
|
|
249
|
+
lines = parseInt(args[++i], 10);
|
|
250
|
+
} else if (args[i].startsWith("-n")) {
|
|
251
|
+
lines = parseInt(args[i].substring(2), 10);
|
|
252
|
+
} else if (/^-\d+$/.test(args[i])) {
|
|
253
|
+
lines = parseInt(args[i].substring(1), 10);
|
|
254
|
+
} else if (!args[i].startsWith("-")) {
|
|
255
|
+
files.push(args[i]);
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
if (files.length === 0) {
|
|
259
|
+
if (ctx.stdin) {
|
|
260
|
+
var stdinResult = ctx.stdin.split("\n").slice(0, lines).join("\n");
|
|
261
|
+
if (!stdinResult.endsWith("\n")) stdinResult += "\n";
|
|
262
|
+
process.stdout.write(stdinResult);
|
|
263
|
+
return 0;
|
|
264
|
+
}
|
|
265
|
+
process.stderr.write("head: missing file operand\n");
|
|
266
|
+
return 1;
|
|
267
|
+
}
|
|
268
|
+
for (var j = 0; j < files.length; j++) {
|
|
269
|
+
var filePath = files[j].startsWith("/") ? files[j] : ctx.path.join(ctx.cwd, files[j]);
|
|
270
|
+
try {
|
|
271
|
+
var content = ctx.fs.readFileSync(filePath);
|
|
272
|
+
var text = typeof content === "string" ? content : new TextDecoder().decode(content);
|
|
273
|
+
var result = text.split("\n").slice(0, lines).join("\n");
|
|
274
|
+
process.stdout.write(result + "\n");
|
|
275
|
+
} catch (err2) {
|
|
276
|
+
process.stderr.write("head: " + files[j] + ": " + err2.message + "\n");
|
|
277
|
+
return 1;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
return 0;
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
// projects/@machina.at/shell/src/commands/tail.js
|
|
286
|
+
var require_tail = __commonJS({
|
|
287
|
+
"projects/@machina.at/shell/src/commands/tail.js"(exports2, module2) {
|
|
288
|
+
"use strict";
|
|
289
|
+
module2.exports = function tail2(args, ctx) {
|
|
290
|
+
var lines = 10;
|
|
291
|
+
var files = [];
|
|
292
|
+
for (var i = 0; i < args.length; i++) {
|
|
293
|
+
if (args[i] === "-n" && args[i + 1]) {
|
|
294
|
+
lines = parseInt(args[++i], 10);
|
|
295
|
+
} else if (args[i].startsWith("-n")) {
|
|
296
|
+
lines = parseInt(args[i].substring(2), 10);
|
|
297
|
+
} else if (!args[i].startsWith("-")) {
|
|
298
|
+
files.push(args[i]);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
if (files.length === 0) {
|
|
302
|
+
if (ctx.stdin) {
|
|
303
|
+
var stdinAll = ctx.stdin.split("\n");
|
|
304
|
+
if (stdinAll[stdinAll.length - 1] === "") stdinAll.pop();
|
|
305
|
+
var stdinResult = stdinAll.slice(-lines).join("\n");
|
|
306
|
+
if (!stdinResult.endsWith("\n")) stdinResult += "\n";
|
|
307
|
+
process.stdout.write(stdinResult);
|
|
308
|
+
return 0;
|
|
309
|
+
}
|
|
310
|
+
process.stderr.write("tail: missing file operand\n");
|
|
311
|
+
return 1;
|
|
312
|
+
}
|
|
313
|
+
for (var j = 0; j < files.length; j++) {
|
|
314
|
+
var filePath = files[j].startsWith("/") ? files[j] : ctx.path.join(ctx.cwd, files[j]);
|
|
315
|
+
try {
|
|
316
|
+
var content = ctx.fs.readFileSync(filePath);
|
|
317
|
+
var text = typeof content === "string" ? content : new TextDecoder().decode(content);
|
|
318
|
+
var result = text.split("\n").slice(-lines).join("\n");
|
|
319
|
+
process.stdout.write(result + "\n");
|
|
320
|
+
} catch (err2) {
|
|
321
|
+
process.stderr.write("tail: " + files[j] + ": " + err2.message + "\n");
|
|
322
|
+
return 1;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
return 0;
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
// projects/@machina.at/shell/src/commands/env.js
|
|
331
|
+
var require_env = __commonJS({
|
|
332
|
+
"projects/@machina.at/shell/src/commands/env.js"(exports2, module2) {
|
|
333
|
+
"use strict";
|
|
334
|
+
module2.exports = function env(args, ctx) {
|
|
335
|
+
var keys = Object.keys(ctx.env);
|
|
336
|
+
for (var i = 0; i < keys.length; i++) {
|
|
337
|
+
process.stdout.write(keys[i] + "=" + ctx.env[keys[i]] + "\n");
|
|
338
|
+
}
|
|
339
|
+
return 0;
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
// projects/@machina.at/shell/src/commands/which.js
|
|
345
|
+
var require_which = __commonJS({
|
|
346
|
+
"projects/@machina.at/shell/src/commands/which.js"(exports2, module2) {
|
|
347
|
+
"use strict";
|
|
348
|
+
var commandNames = require_commands();
|
|
349
|
+
var shellBuiltins = [
|
|
350
|
+
"cd",
|
|
351
|
+
"export",
|
|
352
|
+
"exit",
|
|
353
|
+
"set",
|
|
354
|
+
"unset",
|
|
355
|
+
"read",
|
|
356
|
+
"source",
|
|
357
|
+
"shift",
|
|
358
|
+
"type",
|
|
359
|
+
"local",
|
|
360
|
+
"command",
|
|
361
|
+
"getopts"
|
|
362
|
+
];
|
|
363
|
+
module2.exports = function which(args) {
|
|
364
|
+
if (args.length === 0) return 0;
|
|
365
|
+
var exitCode = 0;
|
|
366
|
+
for (var i = 0; i < args.length; i++) {
|
|
367
|
+
var name = args[i];
|
|
368
|
+
if (commandNames[name]) {
|
|
369
|
+
process.stdout.write(name + ": /bin/commands/" + name + ".js\n");
|
|
370
|
+
} else if (shellBuiltins.indexOf(name) !== -1) {
|
|
371
|
+
process.stdout.write(name + ": shell built-in\n");
|
|
372
|
+
} else {
|
|
373
|
+
process.stderr.write("which: " + name + ": not found\n");
|
|
374
|
+
exitCode = 1;
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
return exitCode;
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
// projects/@machina.at/shell/src/commands/help.js
|
|
383
|
+
var require_help = __commonJS({
|
|
384
|
+
"projects/@machina.at/shell/src/commands/help.js"(exports2, module2) {
|
|
385
|
+
"use strict";
|
|
386
|
+
var DESCRIPTIONS = {
|
|
387
|
+
ls: "List directory contents",
|
|
388
|
+
cat: "Concatenate and print files",
|
|
389
|
+
echo: "Display a line of text",
|
|
390
|
+
pwd: "Print working directory",
|
|
391
|
+
cd: "Change directory (built-in)",
|
|
392
|
+
mkdir: "Make directories",
|
|
393
|
+
head: "Output first part of files",
|
|
394
|
+
tail: "Output last part of files",
|
|
395
|
+
env: "Print environment",
|
|
396
|
+
export: "Set environment variable (built-in)",
|
|
397
|
+
which: "Locate a command",
|
|
398
|
+
help: "Display this help",
|
|
399
|
+
clear: "Clear the terminal screen",
|
|
400
|
+
exit: "Exit the shell (built-in)"
|
|
401
|
+
};
|
|
402
|
+
module2.exports = function help(args, ctx) {
|
|
403
|
+
process.stdout.write("Available commands:\n");
|
|
404
|
+
var names = Object.keys(DESCRIPTIONS);
|
|
405
|
+
for (var i = 0; i < names.length; i++) {
|
|
406
|
+
var name = names[i];
|
|
407
|
+
var pad = " ".substring(0, 10 - name.length);
|
|
408
|
+
process.stdout.write(" " + name + pad + " - " + DESCRIPTIONS[name] + "\n");
|
|
409
|
+
}
|
|
410
|
+
return 0;
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
// projects/@machina.at/shell/src/commands/clear.js
|
|
416
|
+
var require_clear = __commonJS({
|
|
417
|
+
"projects/@machina.at/shell/src/commands/clear.js"(exports2, module2) {
|
|
418
|
+
"use strict";
|
|
419
|
+
module2.exports = function clear(args, ctx) {
|
|
420
|
+
process.stdout.write("\x1B[2J\x1B[H");
|
|
421
|
+
return 0;
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
// projects/@machina.at/shell/src/commands/test.js
|
|
427
|
+
var require_test = __commonJS({
|
|
428
|
+
"projects/@machina.at/shell/src/commands/test.js"(exports2, module2) {
|
|
429
|
+
"use strict";
|
|
430
|
+
module2.exports = function test(args, ctx) {
|
|
431
|
+
if (args.length > 0 && args[args.length - 1] === "]") {
|
|
432
|
+
args = args.slice(0, -1);
|
|
433
|
+
} else if (module2.exports._bracketMode) {
|
|
434
|
+
ctx.stderr && ctx.stderr.write("test: missing ]\n");
|
|
435
|
+
return 2;
|
|
436
|
+
}
|
|
437
|
+
if (args.length === 0) return 1;
|
|
438
|
+
if (args[0] === "!") {
|
|
439
|
+
var inner = evaluate(args.slice(1), ctx);
|
|
440
|
+
return inner === 0 ? 1 : 0;
|
|
441
|
+
}
|
|
442
|
+
return evaluate(args, ctx);
|
|
443
|
+
};
|
|
444
|
+
function evaluate(args, ctx) {
|
|
445
|
+
if (args.length === 0) return 1;
|
|
446
|
+
if (args.length === 1) {
|
|
447
|
+
return args[0] ? 0 : 1;
|
|
448
|
+
}
|
|
449
|
+
if (args.length === 2) {
|
|
450
|
+
return evalUnary(args[0], args[1], ctx);
|
|
451
|
+
}
|
|
452
|
+
if (args.length === 3) {
|
|
453
|
+
return evalBinary(args[0], args[1], args[2], ctx);
|
|
454
|
+
}
|
|
455
|
+
return 1;
|
|
456
|
+
}
|
|
457
|
+
function resolvePath(p, ctx) {
|
|
458
|
+
if (p.startsWith("/")) return p;
|
|
459
|
+
return ctx.path.join(ctx.cwd, p);
|
|
460
|
+
}
|
|
461
|
+
function evalUnary(op, operand, ctx) {
|
|
462
|
+
var filePath = resolvePath(operand, ctx);
|
|
463
|
+
switch (op) {
|
|
464
|
+
case "-e":
|
|
465
|
+
return ctx.fs.existsSync(filePath) ? 0 : 1;
|
|
466
|
+
case "-f":
|
|
467
|
+
try {
|
|
468
|
+
return ctx.fs.statSync(filePath).isFile() ? 0 : 1;
|
|
469
|
+
} catch (_) {
|
|
470
|
+
return 1;
|
|
471
|
+
}
|
|
472
|
+
case "-d":
|
|
473
|
+
try {
|
|
474
|
+
return ctx.fs.statSync(filePath).isDirectory() ? 0 : 1;
|
|
475
|
+
} catch (_) {
|
|
476
|
+
return 1;
|
|
477
|
+
}
|
|
478
|
+
case "-s":
|
|
479
|
+
try {
|
|
480
|
+
return ctx.fs.statSync(filePath).size > 0 ? 0 : 1;
|
|
481
|
+
} catch (_) {
|
|
482
|
+
return 1;
|
|
483
|
+
}
|
|
484
|
+
case "-r":
|
|
485
|
+
try {
|
|
486
|
+
return ctx.fs.statSync(filePath).mode & 292 ? 0 : 1;
|
|
487
|
+
} catch (_) {
|
|
488
|
+
return 1;
|
|
489
|
+
}
|
|
490
|
+
case "-w":
|
|
491
|
+
try {
|
|
492
|
+
return ctx.fs.statSync(filePath).mode & 146 ? 0 : 1;
|
|
493
|
+
} catch (_) {
|
|
494
|
+
return 1;
|
|
495
|
+
}
|
|
496
|
+
case "-x":
|
|
497
|
+
try {
|
|
498
|
+
return ctx.fs.statSync(filePath).mode & 73 ? 0 : 1;
|
|
499
|
+
} catch (_) {
|
|
500
|
+
return 1;
|
|
501
|
+
}
|
|
502
|
+
case "-z":
|
|
503
|
+
return operand.length === 0 ? 0 : 1;
|
|
504
|
+
case "-n":
|
|
505
|
+
return operand.length > 0 ? 0 : 1;
|
|
506
|
+
default:
|
|
507
|
+
return 1;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
function evalBinary(left, op, right) {
|
|
511
|
+
switch (op) {
|
|
512
|
+
case "=":
|
|
513
|
+
return left === right ? 0 : 1;
|
|
514
|
+
case "!=":
|
|
515
|
+
return left !== right ? 0 : 1;
|
|
516
|
+
case "-eq":
|
|
517
|
+
return parseInt(left, 10) === parseInt(right, 10) ? 0 : 1;
|
|
518
|
+
case "-ne":
|
|
519
|
+
return parseInt(left, 10) !== parseInt(right, 10) ? 0 : 1;
|
|
520
|
+
case "-lt":
|
|
521
|
+
return parseInt(left, 10) < parseInt(right, 10) ? 0 : 1;
|
|
522
|
+
case "-le":
|
|
523
|
+
return parseInt(left, 10) <= parseInt(right, 10) ? 0 : 1;
|
|
524
|
+
case "-gt":
|
|
525
|
+
return parseInt(left, 10) > parseInt(right, 10) ? 0 : 1;
|
|
526
|
+
case "-ge":
|
|
527
|
+
return parseInt(left, 10) >= parseInt(right, 10) ? 0 : 1;
|
|
528
|
+
default:
|
|
529
|
+
return 1;
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
});
|
|
534
|
+
|
|
535
|
+
// projects/@machina.at/shell/src/commands/true.js
|
|
536
|
+
var require_true = __commonJS({
|
|
537
|
+
"projects/@machina.at/shell/src/commands/true.js"(exports2, module2) {
|
|
538
|
+
"use strict";
|
|
539
|
+
module2.exports = function _true() {
|
|
540
|
+
return 0;
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
});
|
|
544
|
+
|
|
545
|
+
// projects/@machina.at/shell/src/commands/false.js
|
|
546
|
+
var require_false = __commonJS({
|
|
547
|
+
"projects/@machina.at/shell/src/commands/false.js"(exports2, module2) {
|
|
548
|
+
"use strict";
|
|
549
|
+
module2.exports = function _false() {
|
|
550
|
+
return 1;
|
|
551
|
+
};
|
|
552
|
+
}
|
|
553
|
+
});
|
|
554
|
+
|
|
555
|
+
// projects/@machina.at/shell/src/commands/cp.js
|
|
556
|
+
var require_cp = __commonJS({
|
|
557
|
+
"projects/@machina.at/shell/src/commands/cp.js"(exports2, module2) {
|
|
558
|
+
"use strict";
|
|
559
|
+
module2.exports = function cp2(args, ctx) {
|
|
560
|
+
var recursive = false;
|
|
561
|
+
var paths = [];
|
|
562
|
+
for (var i = 0; i < args.length; i++) {
|
|
563
|
+
if (args[i] === "-r" || args[i] === "-R" || args[i] === "-rf" || args[i] === "-fr") recursive = true;
|
|
564
|
+
else if (args[i] === "-f") {
|
|
565
|
+
} else if (!args[i].startsWith("-")) paths.push(args[i]);
|
|
566
|
+
}
|
|
567
|
+
if (paths.length < 2) {
|
|
568
|
+
process.stderr.write("cp: missing file operand\n");
|
|
569
|
+
return 1;
|
|
570
|
+
}
|
|
571
|
+
var src = paths[0].startsWith("/") ? paths[0] : ctx.path.join(ctx.cwd, paths[0]);
|
|
572
|
+
var dst = paths[1].startsWith("/") ? paths[1] : ctx.path.join(ctx.cwd, paths[1]);
|
|
573
|
+
try {
|
|
574
|
+
var stat = ctx.fs.statSync(src);
|
|
575
|
+
if (stat.isDirectory()) {
|
|
576
|
+
if (!recursive) {
|
|
577
|
+
process.stderr.write("cp: -r not specified; omitting directory '" + paths[0] + "'\n");
|
|
578
|
+
return 1;
|
|
579
|
+
}
|
|
580
|
+
cpDir(src, dst, ctx);
|
|
581
|
+
} else {
|
|
582
|
+
ctx.fs.writeFileSync(dst, ctx.fs.readFileSync(src));
|
|
583
|
+
}
|
|
584
|
+
return 0;
|
|
585
|
+
} catch (err2) {
|
|
586
|
+
process.stderr.write("cp: " + paths[0] + ": " + err2.message + "\n");
|
|
587
|
+
return 1;
|
|
588
|
+
}
|
|
589
|
+
};
|
|
590
|
+
function cpDir(src, dst, ctx) {
|
|
591
|
+
try {
|
|
592
|
+
ctx.fs.mkdirSync(dst);
|
|
593
|
+
} catch (_) {
|
|
594
|
+
}
|
|
595
|
+
var entries = ctx.fs.readdirSync(src);
|
|
596
|
+
for (var i = 0; i < entries.length; i++) {
|
|
597
|
+
var s = ctx.path.join(src, entries[i]), d = ctx.path.join(dst, entries[i]);
|
|
598
|
+
if (ctx.fs.statSync(s).isDirectory()) cpDir(s, d, ctx);
|
|
599
|
+
else ctx.fs.writeFileSync(d, ctx.fs.readFileSync(s));
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
});
|
|
604
|
+
|
|
605
|
+
// projects/@machina.at/shell/src/commands/mv.js
|
|
606
|
+
var require_mv = __commonJS({
|
|
607
|
+
"projects/@machina.at/shell/src/commands/mv.js"(exports2, module2) {
|
|
608
|
+
"use strict";
|
|
609
|
+
module2.exports = function mv2(args, ctx) {
|
|
610
|
+
var paths = [];
|
|
611
|
+
for (var i = 0; i < args.length; i++) {
|
|
612
|
+
if (!args[i].startsWith("-")) paths.push(args[i]);
|
|
613
|
+
}
|
|
614
|
+
if (paths.length < 2) {
|
|
615
|
+
process.stderr.write("mv: missing file operand\n");
|
|
616
|
+
return 1;
|
|
617
|
+
}
|
|
618
|
+
var src = paths[0].startsWith("/") ? paths[0] : ctx.path.join(ctx.cwd, paths[0]);
|
|
619
|
+
var dst = paths[1].startsWith("/") ? paths[1] : ctx.path.join(ctx.cwd, paths[1]);
|
|
620
|
+
try {
|
|
621
|
+
ctx.fs.renameSync(src, dst);
|
|
622
|
+
return 0;
|
|
623
|
+
} catch (err2) {
|
|
624
|
+
process.stderr.write("mv: " + paths[0] + ": " + err2.message + "\n");
|
|
625
|
+
return 1;
|
|
626
|
+
}
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
});
|
|
630
|
+
|
|
631
|
+
// projects/@machina.at/shell/src/commands/rm.js
|
|
632
|
+
var require_rm = __commonJS({
|
|
633
|
+
"projects/@machina.at/shell/src/commands/rm.js"(exports2, module2) {
|
|
634
|
+
"use strict";
|
|
635
|
+
module2.exports = function rm2(args, ctx) {
|
|
636
|
+
var recursive = false, force = false, targets = [];
|
|
637
|
+
for (var i = 0; i < args.length; i++) {
|
|
638
|
+
if (args[i] === "-r" || args[i] === "-R") recursive = true;
|
|
639
|
+
else if (args[i] === "-f") force = true;
|
|
640
|
+
else if (args[i] === "-rf" || args[i] === "-fr") {
|
|
641
|
+
recursive = true;
|
|
642
|
+
force = true;
|
|
643
|
+
} else if (!args[i].startsWith("-")) targets.push(args[i]);
|
|
644
|
+
}
|
|
645
|
+
if (targets.length === 0) {
|
|
646
|
+
if (!force) {
|
|
647
|
+
process.stderr.write("rm: missing operand\n");
|
|
648
|
+
return 1;
|
|
649
|
+
}
|
|
650
|
+
return 0;
|
|
651
|
+
}
|
|
652
|
+
for (var j = 0; j < targets.length; j++) {
|
|
653
|
+
var p = targets[j].startsWith("/") ? targets[j] : ctx.path.join(ctx.cwd, targets[j]);
|
|
654
|
+
try {
|
|
655
|
+
var stat = ctx.fs.statSync(p);
|
|
656
|
+
if (stat.isDirectory()) {
|
|
657
|
+
if (!recursive) {
|
|
658
|
+
process.stderr.write("rm: cannot remove '" + targets[j] + "': Is a directory\n");
|
|
659
|
+
return 1;
|
|
660
|
+
}
|
|
661
|
+
rmDir2(p, ctx);
|
|
662
|
+
} else {
|
|
663
|
+
ctx.fs.unlinkSync(p);
|
|
664
|
+
}
|
|
665
|
+
} catch (err2) {
|
|
666
|
+
if (!force) {
|
|
667
|
+
process.stderr.write("rm: " + targets[j] + ": " + err2.message + "\n");
|
|
668
|
+
return 1;
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
return 0;
|
|
673
|
+
};
|
|
674
|
+
function rmDir2(p, ctx) {
|
|
675
|
+
var entries = ctx.fs.readdirSync(p);
|
|
676
|
+
for (var i = 0; i < entries.length; i++) {
|
|
677
|
+
var full = ctx.path.join(p, entries[i]);
|
|
678
|
+
if (ctx.fs.statSync(full).isDirectory()) rmDir2(full, ctx);
|
|
679
|
+
else ctx.fs.unlinkSync(full);
|
|
680
|
+
}
|
|
681
|
+
ctx.fs.rmdirSync(p);
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
});
|
|
685
|
+
|
|
686
|
+
// projects/@machina.at/shell/src/commands/rmdir.js
|
|
687
|
+
var require_rmdir = __commonJS({
|
|
688
|
+
"projects/@machina.at/shell/src/commands/rmdir.js"(exports2, module2) {
|
|
689
|
+
"use strict";
|
|
690
|
+
module2.exports = function rmdir2(args, ctx) {
|
|
691
|
+
if (args.length === 0) {
|
|
692
|
+
process.stderr.write("rmdir: missing operand\n");
|
|
693
|
+
return 1;
|
|
694
|
+
}
|
|
695
|
+
for (var i = 0; i < args.length; i++) {
|
|
696
|
+
if (args[i].startsWith("-")) continue;
|
|
697
|
+
var p = args[i].startsWith("/") ? args[i] : ctx.path.join(ctx.cwd, args[i]);
|
|
698
|
+
try {
|
|
699
|
+
var entries = ctx.fs.readdirSync(p);
|
|
700
|
+
if (entries.length > 0) {
|
|
701
|
+
process.stderr.write("rmdir: " + args[i] + ": Directory not empty\n");
|
|
702
|
+
return 1;
|
|
703
|
+
}
|
|
704
|
+
ctx.fs.rmdirSync(p);
|
|
705
|
+
} catch (err2) {
|
|
706
|
+
process.stderr.write("rmdir: " + args[i] + ": " + err2.message + "\n");
|
|
707
|
+
return 1;
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
return 0;
|
|
711
|
+
};
|
|
712
|
+
}
|
|
713
|
+
});
|
|
714
|
+
|
|
715
|
+
// projects/@machina.at/shell/src/commands/touch.js
|
|
716
|
+
var require_touch = __commonJS({
|
|
717
|
+
"projects/@machina.at/shell/src/commands/touch.js"(exports2, module2) {
|
|
718
|
+
"use strict";
|
|
719
|
+
module2.exports = function touch2(args, ctx) {
|
|
720
|
+
if (args.length === 0) {
|
|
721
|
+
process.stderr.write("touch: missing file operand\n");
|
|
722
|
+
return 1;
|
|
723
|
+
}
|
|
724
|
+
for (var i = 0; i < args.length; i++) {
|
|
725
|
+
if (args[i].startsWith("-")) continue;
|
|
726
|
+
var p = args[i].startsWith("/") ? args[i] : ctx.path.join(ctx.cwd, args[i]);
|
|
727
|
+
try {
|
|
728
|
+
if (ctx.fs.existsSync(p)) {
|
|
729
|
+
ctx.fs.utimesSync(p, /* @__PURE__ */ new Date(), /* @__PURE__ */ new Date());
|
|
730
|
+
} else {
|
|
731
|
+
ctx.fs.writeFileSync(p, "");
|
|
732
|
+
}
|
|
733
|
+
} catch (err2) {
|
|
734
|
+
process.stderr.write("touch: " + args[i] + ": " + err2.message + "\n");
|
|
735
|
+
return 1;
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
return 0;
|
|
739
|
+
};
|
|
740
|
+
}
|
|
741
|
+
});
|
|
742
|
+
|
|
743
|
+
// projects/@machina.at/shell/src/commands/ln.js
|
|
744
|
+
var require_ln = __commonJS({
|
|
745
|
+
"projects/@machina.at/shell/src/commands/ln.js"(exports2, module2) {
|
|
746
|
+
"use strict";
|
|
747
|
+
module2.exports = function ln2(args, ctx) {
|
|
748
|
+
var symbolic = false, paths = [];
|
|
749
|
+
for (var i = 0; i < args.length; i++) {
|
|
750
|
+
if (args[i] === "-s") symbolic = true;
|
|
751
|
+
else if (!args[i].startsWith("-")) paths.push(args[i]);
|
|
752
|
+
}
|
|
753
|
+
if (paths.length < 2) {
|
|
754
|
+
process.stderr.write("ln: missing file operand\n");
|
|
755
|
+
return 1;
|
|
756
|
+
}
|
|
757
|
+
var target = paths[0].startsWith("/") ? paths[0] : ctx.path.join(ctx.cwd, paths[0]);
|
|
758
|
+
var linkName = paths[1].startsWith("/") ? paths[1] : ctx.path.join(ctx.cwd, paths[1]);
|
|
759
|
+
try {
|
|
760
|
+
if (symbolic) ctx.fs.symlinkSync(target, linkName);
|
|
761
|
+
else ctx.fs.linkSync(target, linkName);
|
|
762
|
+
return 0;
|
|
763
|
+
} catch (err2) {
|
|
764
|
+
process.stderr.write("ln: " + paths[1] + ": " + err2.message + "\n");
|
|
765
|
+
return 1;
|
|
766
|
+
}
|
|
767
|
+
};
|
|
768
|
+
}
|
|
769
|
+
});
|
|
770
|
+
|
|
771
|
+
// projects/@machina.at/shell/src/commands/grep.js
|
|
772
|
+
var require_grep = __commonJS({
|
|
773
|
+
"projects/@machina.at/shell/src/commands/grep.js"(exports2, module2) {
|
|
774
|
+
"use strict";
|
|
775
|
+
module2.exports = function grep2(args, ctx) {
|
|
776
|
+
var invert = false, ignoreCase = false, countOnly = false, lineNum = false;
|
|
777
|
+
var pattern = "", patternSet = false, files = [];
|
|
778
|
+
for (var i = 0; i < args.length; i++) {
|
|
779
|
+
if (args[i] === "-v") invert = true;
|
|
780
|
+
else if (args[i] === "-i") ignoreCase = true;
|
|
781
|
+
else if (args[i] === "-c") countOnly = true;
|
|
782
|
+
else if (args[i] === "-n") lineNum = true;
|
|
783
|
+
else if (args[i].startsWith("-")) {
|
|
784
|
+
} else if (!patternSet) {
|
|
785
|
+
pattern = args[i];
|
|
786
|
+
patternSet = true;
|
|
787
|
+
} else files.push(args[i]);
|
|
788
|
+
}
|
|
789
|
+
if (!patternSet) {
|
|
790
|
+
process.stderr.write("grep: missing pattern\n");
|
|
791
|
+
return 2;
|
|
792
|
+
}
|
|
793
|
+
var input = files.length === 0 ? ctx.stdin || "" : "";
|
|
794
|
+
for (var j = 0; j < files.length; j++) {
|
|
795
|
+
var p = files[j].startsWith("/") ? files[j] : ctx.path.join(ctx.cwd, files[j]);
|
|
796
|
+
try {
|
|
797
|
+
input += ctx.fs.readFileSync(p);
|
|
798
|
+
} catch (e) {
|
|
799
|
+
process.stderr.write(files[j] + ": " + e.message + "\n");
|
|
800
|
+
return 1;
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
var re = new RegExp(pattern, ignoreCase ? "i" : "");
|
|
804
|
+
var lines = input.split("\n");
|
|
805
|
+
if (lines[lines.length - 1] === "") lines.pop();
|
|
806
|
+
var count = 0, matched = [];
|
|
807
|
+
for (var k = 0; k < lines.length; k++) {
|
|
808
|
+
var m = re.test(lines[k]);
|
|
809
|
+
if (m !== invert) {
|
|
810
|
+
count++;
|
|
811
|
+
matched.push(lineNum ? k + 1 + ":" + lines[k] : lines[k]);
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
if (countOnly) process.stdout.write(count + "\n");
|
|
815
|
+
else for (var l = 0; l < matched.length; l++) process.stdout.write(matched[l] + "\n");
|
|
816
|
+
return count > 0 ? 0 : 1;
|
|
817
|
+
};
|
|
818
|
+
}
|
|
819
|
+
});
|
|
820
|
+
|
|
821
|
+
// projects/@machina.at/shell/src/commands/sed.js
|
|
822
|
+
var require_sed = __commonJS({
|
|
823
|
+
"projects/@machina.at/shell/src/commands/sed.js"(exports2, module2) {
|
|
824
|
+
"use strict";
|
|
825
|
+
module2.exports = function sed2(args, ctx) {
|
|
826
|
+
var expression = "", files = [];
|
|
827
|
+
for (var i = 0; i < args.length; i++) {
|
|
828
|
+
if (!args[i].startsWith("-") && !expression) expression = args[i];
|
|
829
|
+
else if (!args[i].startsWith("-")) files.push(args[i]);
|
|
830
|
+
}
|
|
831
|
+
if (!expression) {
|
|
832
|
+
process.stderr.write("sed: missing expression\n");
|
|
833
|
+
return 1;
|
|
834
|
+
}
|
|
835
|
+
var input = files.length === 0 ? ctx.stdin || "" : "";
|
|
836
|
+
for (var j = 0; j < files.length; j++) {
|
|
837
|
+
var p = files[j].startsWith("/") ? files[j] : ctx.path.join(ctx.cwd, files[j]);
|
|
838
|
+
try {
|
|
839
|
+
input += ctx.fs.readFileSync(p);
|
|
840
|
+
} catch (e) {
|
|
841
|
+
process.stderr.write(files[j] + ": " + e.message + "\n");
|
|
842
|
+
return 1;
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
var m = expression.match(/^s(.)(.+?)\1(.*?)\1([g]?)$/);
|
|
846
|
+
if (!m) {
|
|
847
|
+
process.stderr.write("sed: invalid expression: " + expression + "\n");
|
|
848
|
+
return 1;
|
|
849
|
+
}
|
|
850
|
+
var re = new RegExp(m[2], m[4] === "g" ? "g" : "");
|
|
851
|
+
var lines = input.split("\n");
|
|
852
|
+
for (var k = 0; k < lines.length; k++) {
|
|
853
|
+
process.stdout.write(lines[k].replace(re, m[3]) + (k < lines.length - 1 ? "\n" : ""));
|
|
854
|
+
}
|
|
855
|
+
return 0;
|
|
856
|
+
};
|
|
857
|
+
}
|
|
858
|
+
});
|
|
859
|
+
|
|
860
|
+
// projects/@machina.at/shell/src/commands/wc.js
|
|
861
|
+
var require_wc = __commonJS({
|
|
862
|
+
"projects/@machina.at/shell/src/commands/wc.js"(exports2, module2) {
|
|
863
|
+
"use strict";
|
|
864
|
+
module2.exports = function wc2(args, ctx) {
|
|
865
|
+
var lo = false, wo = false, co = false, files = [];
|
|
866
|
+
for (var i = 0; i < args.length; i++) {
|
|
867
|
+
if (args[i] === "-l") lo = true;
|
|
868
|
+
else if (args[i] === "-w") wo = true;
|
|
869
|
+
else if (args[i] === "-c" || args[i] === "-m") co = true;
|
|
870
|
+
else if (!args[i].startsWith("-")) files.push(args[i]);
|
|
871
|
+
}
|
|
872
|
+
var input = files.length === 0 ? ctx.stdin || "" : "";
|
|
873
|
+
for (var j = 0; j < files.length; j++) {
|
|
874
|
+
var p = files[j].startsWith("/") ? files[j] : ctx.path.join(ctx.cwd, files[j]);
|
|
875
|
+
try {
|
|
876
|
+
input += ctx.fs.readFileSync(p);
|
|
877
|
+
} catch (e) {
|
|
878
|
+
process.stderr.write(files[j] + ": " + e.message + "\n");
|
|
879
|
+
return 1;
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
var lc = input.split("\n").length - (input.endsWith("\n") ? 1 : 0);
|
|
883
|
+
var wt = input.trim() ? input.trim().split(/\s+/).length : 0;
|
|
884
|
+
var cc = input.length;
|
|
885
|
+
if (lo) process.stdout.write(lc + "\n");
|
|
886
|
+
else if (wo) process.stdout.write(wt + "\n");
|
|
887
|
+
else if (co) process.stdout.write(cc + "\n");
|
|
888
|
+
else process.stdout.write(lc + " " + wt + " " + cc + "\n");
|
|
889
|
+
return 0;
|
|
890
|
+
};
|
|
891
|
+
}
|
|
892
|
+
});
|
|
893
|
+
|
|
894
|
+
// projects/@machina.at/shell/src/commands/sort.js
|
|
895
|
+
var require_sort = __commonJS({
|
|
896
|
+
"projects/@machina.at/shell/src/commands/sort.js"(exports2, module2) {
|
|
897
|
+
"use strict";
|
|
898
|
+
module2.exports = function sort2(args, ctx) {
|
|
899
|
+
var rev3 = false, num = false, uniq2 = false, files = [];
|
|
900
|
+
for (var i = 0; i < args.length; i++) {
|
|
901
|
+
if (args[i] === "-r") rev3 = true;
|
|
902
|
+
else if (args[i] === "-n") num = true;
|
|
903
|
+
else if (args[i] === "-u") uniq2 = true;
|
|
904
|
+
else if (!args[i].startsWith("-")) files.push(args[i]);
|
|
905
|
+
}
|
|
906
|
+
var input = files.length === 0 ? ctx.stdin || "" : "";
|
|
907
|
+
for (var j = 0; j < files.length; j++) {
|
|
908
|
+
var p = files[j].startsWith("/") ? files[j] : ctx.path.join(ctx.cwd, files[j]);
|
|
909
|
+
try {
|
|
910
|
+
input += ctx.fs.readFileSync(p);
|
|
911
|
+
} catch (e) {
|
|
912
|
+
process.stderr.write(files[j] + ": " + e.message + "\n");
|
|
913
|
+
return 1;
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
var lines = input.split("\n");
|
|
917
|
+
if (lines[lines.length - 1] === "") lines.pop();
|
|
918
|
+
if (num) lines.sort(function(a, b) {
|
|
919
|
+
return parseFloat(a) - parseFloat(b);
|
|
920
|
+
});
|
|
921
|
+
else lines.sort();
|
|
922
|
+
if (rev3) lines.reverse();
|
|
923
|
+
if (uniq2) {
|
|
924
|
+
var s = {};
|
|
925
|
+
lines = lines.filter(function(l) {
|
|
926
|
+
if (s[l]) return false;
|
|
927
|
+
s[l] = 1;
|
|
928
|
+
return true;
|
|
929
|
+
});
|
|
930
|
+
}
|
|
931
|
+
for (var k = 0; k < lines.length; k++) process.stdout.write(lines[k] + "\n");
|
|
932
|
+
return 0;
|
|
933
|
+
};
|
|
934
|
+
}
|
|
935
|
+
});
|
|
936
|
+
|
|
937
|
+
// projects/@machina.at/shell/src/commands/uniq.js
|
|
938
|
+
var require_uniq = __commonJS({
|
|
939
|
+
"projects/@machina.at/shell/src/commands/uniq.js"(exports2, module2) {
|
|
940
|
+
"use strict";
|
|
941
|
+
module2.exports = function uniq2(args, ctx) {
|
|
942
|
+
var countP = false, dupsOnly = false, files = [];
|
|
943
|
+
for (var i = 0; i < args.length; i++) {
|
|
944
|
+
if (args[i] === "-c") countP = true;
|
|
945
|
+
else if (args[i] === "-d") dupsOnly = true;
|
|
946
|
+
else if (!args[i].startsWith("-")) files.push(args[i]);
|
|
947
|
+
}
|
|
948
|
+
var input = files.length === 0 ? ctx.stdin || "" : "";
|
|
949
|
+
for (var j = 0; j < files.length; j++) {
|
|
950
|
+
var p = files[j].startsWith("/") ? files[j] : ctx.path.join(ctx.cwd, files[j]);
|
|
951
|
+
try {
|
|
952
|
+
input += ctx.fs.readFileSync(p);
|
|
953
|
+
} catch (e) {
|
|
954
|
+
process.stderr.write(files[j] + ": " + e.message + "\n");
|
|
955
|
+
return 1;
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
var lines = input.split("\n");
|
|
959
|
+
if (lines[lines.length - 1] === "") lines.pop();
|
|
960
|
+
var groups = [];
|
|
961
|
+
for (var k = 0; k < lines.length; k++) {
|
|
962
|
+
if (groups.length > 0 && groups[groups.length - 1].l === lines[k]) groups[groups.length - 1].c++;
|
|
963
|
+
else groups.push({ l: lines[k], c: 1 });
|
|
964
|
+
}
|
|
965
|
+
for (var m = 0; m < groups.length; m++) {
|
|
966
|
+
if (dupsOnly && groups[m].c < 2) continue;
|
|
967
|
+
if (countP) process.stdout.write(groups[m].c + " " + groups[m].l + "\n");
|
|
968
|
+
else process.stdout.write(groups[m].l + "\n");
|
|
969
|
+
}
|
|
970
|
+
return 0;
|
|
971
|
+
};
|
|
972
|
+
}
|
|
973
|
+
});
|
|
974
|
+
|
|
975
|
+
// projects/@machina.at/shell/src/commands/cut.js
|
|
976
|
+
var require_cut = __commonJS({
|
|
977
|
+
"projects/@machina.at/shell/src/commands/cut.js"(exports2, module2) {
|
|
978
|
+
"use strict";
|
|
979
|
+
module2.exports = function cut2(args, ctx) {
|
|
980
|
+
var delim = " ", fields = "", files = [];
|
|
981
|
+
for (var i = 0; i < args.length; i++) {
|
|
982
|
+
if (args[i] === "-d" && args[i + 1]) delim = args[++i];
|
|
983
|
+
else if (args[i] === "-f" && args[i + 1]) fields = args[++i];
|
|
984
|
+
else if (args[i].startsWith("-d")) delim = args[i].slice(2);
|
|
985
|
+
else if (args[i].startsWith("-f")) fields = args[i].slice(2);
|
|
986
|
+
else if (!args[i].startsWith("-")) files.push(args[i]);
|
|
987
|
+
}
|
|
988
|
+
if (!fields) {
|
|
989
|
+
process.stderr.write("cut: missing field list\n");
|
|
990
|
+
return 1;
|
|
991
|
+
}
|
|
992
|
+
var input = files.length === 0 ? ctx.stdin || "" : "";
|
|
993
|
+
for (var j = 0; j < files.length; j++) {
|
|
994
|
+
var p = files[j].startsWith("/") ? files[j] : ctx.path.join(ctx.cwd, files[j]);
|
|
995
|
+
try {
|
|
996
|
+
input += ctx.fs.readFileSync(p);
|
|
997
|
+
} catch (e) {
|
|
998
|
+
process.stderr.write(files[j] + ": " + e.message + "\n");
|
|
999
|
+
return 1;
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
var fns = fields.split(",").map(function(f) {
|
|
1003
|
+
return parseInt(f, 10) - 1;
|
|
1004
|
+
});
|
|
1005
|
+
var lines = input.split("\n");
|
|
1006
|
+
if (lines[lines.length - 1] === "") lines.pop();
|
|
1007
|
+
for (var k = 0; k < lines.length; k++) {
|
|
1008
|
+
var parts = lines[k].split(delim);
|
|
1009
|
+
process.stdout.write(fns.map(function(f) {
|
|
1010
|
+
return parts[f] || "";
|
|
1011
|
+
}).join(delim) + "\n");
|
|
1012
|
+
}
|
|
1013
|
+
return 0;
|
|
1014
|
+
};
|
|
1015
|
+
}
|
|
1016
|
+
});
|
|
1017
|
+
|
|
1018
|
+
// projects/@machina.at/shell/src/commands/tr.js
|
|
1019
|
+
var require_tr = __commonJS({
|
|
1020
|
+
"projects/@machina.at/shell/src/commands/tr.js"(exports2, module2) {
|
|
1021
|
+
"use strict";
|
|
1022
|
+
module2.exports = function tr2(args, ctx) {
|
|
1023
|
+
var del = false, sets = [];
|
|
1024
|
+
for (var i = 0; i < args.length; i++) {
|
|
1025
|
+
if (args[i] === "-d") del = true;
|
|
1026
|
+
else sets.push(args[i]);
|
|
1027
|
+
}
|
|
1028
|
+
if (sets.length < 1) {
|
|
1029
|
+
process.stderr.write("tr: missing operand\n");
|
|
1030
|
+
return 1;
|
|
1031
|
+
}
|
|
1032
|
+
var input = ctx.stdin || "";
|
|
1033
|
+
if (del) {
|
|
1034
|
+
var result = input;
|
|
1035
|
+
for (var j = 0; j < sets[0].length; j++) result = result.split(sets[0][j]).join("");
|
|
1036
|
+
process.stdout.write(result);
|
|
1037
|
+
return 0;
|
|
1038
|
+
}
|
|
1039
|
+
if (sets.length < 2) {
|
|
1040
|
+
process.stderr.write("tr: missing operand after '" + sets[0] + "'\n");
|
|
1041
|
+
return 1;
|
|
1042
|
+
}
|
|
1043
|
+
var out = "";
|
|
1044
|
+
for (var k = 0; k < input.length; k++) {
|
|
1045
|
+
var idx = sets[0].indexOf(input[k]);
|
|
1046
|
+
out += idx >= 0 ? sets[1][Math.min(idx, sets[1].length - 1)] || input[k] : input[k];
|
|
1047
|
+
}
|
|
1048
|
+
process.stdout.write(out);
|
|
1049
|
+
return 0;
|
|
1050
|
+
};
|
|
1051
|
+
}
|
|
1052
|
+
});
|
|
1053
|
+
|
|
1054
|
+
// projects/@machina.at/shell/src/commands/tee.js
|
|
1055
|
+
var require_tee = __commonJS({
|
|
1056
|
+
"projects/@machina.at/shell/src/commands/tee.js"(exports2, module2) {
|
|
1057
|
+
"use strict";
|
|
1058
|
+
module2.exports = function tee2(args, ctx) {
|
|
1059
|
+
var append = false, files = [];
|
|
1060
|
+
for (var i = 0; i < args.length; i++) {
|
|
1061
|
+
if (args[i] === "-a") append = true;
|
|
1062
|
+
else if (!args[i].startsWith("-")) files.push(args[i]);
|
|
1063
|
+
}
|
|
1064
|
+
var input = ctx.stdin || "";
|
|
1065
|
+
process.stdout.write(input);
|
|
1066
|
+
for (var j = 0; j < files.length; j++) {
|
|
1067
|
+
var p = files[j].startsWith("/") ? files[j] : ctx.path.join(ctx.cwd, files[j]);
|
|
1068
|
+
try {
|
|
1069
|
+
if (append) ctx.fs.appendFileSync(p, input);
|
|
1070
|
+
else ctx.fs.writeFileSync(p, input);
|
|
1071
|
+
} catch (e) {
|
|
1072
|
+
process.stderr.write("tee: " + files[j] + ": " + e.message + "\n");
|
|
1073
|
+
return 1;
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
return 0;
|
|
1077
|
+
};
|
|
1078
|
+
}
|
|
1079
|
+
});
|
|
1080
|
+
|
|
1081
|
+
// projects/@machina.at/shell/src/commands/printf.js
|
|
1082
|
+
var require_printf = __commonJS({
|
|
1083
|
+
"projects/@machina.at/shell/src/commands/printf.js"(exports2, module2) {
|
|
1084
|
+
"use strict";
|
|
1085
|
+
module2.exports = function printf(args, ctx) {
|
|
1086
|
+
if (args.length === 0) return 0;
|
|
1087
|
+
var format = args[0], params = args.slice(1), pi = 0;
|
|
1088
|
+
var result = "", i = 0;
|
|
1089
|
+
while (i < format.length) {
|
|
1090
|
+
if (format[i] === "\\") {
|
|
1091
|
+
i++;
|
|
1092
|
+
if (i >= format.length) break;
|
|
1093
|
+
if (format[i] === "n") result += "\n";
|
|
1094
|
+
else if (format[i] === "t") result += " ";
|
|
1095
|
+
else if (format[i] === "r") result += "\r";
|
|
1096
|
+
else if (format[i] === "\\") result += "\\";
|
|
1097
|
+
else if (format[i] === '"') result += '"';
|
|
1098
|
+
else if (format[i] === "'") result += "'";
|
|
1099
|
+
else if (format[i] === "0") result += "\0";
|
|
1100
|
+
else if (format[i] === "a") result += "\x07";
|
|
1101
|
+
else if (format[i] === "b") result += "\b";
|
|
1102
|
+
else if (format[i] === "e") result += "\x1B";
|
|
1103
|
+
else if (format[i] === "f") result += "\f";
|
|
1104
|
+
else if (format[i] === "v") result += "\v";
|
|
1105
|
+
else if (format[i] === "x") {
|
|
1106
|
+
if (i + 1 < format.length && /[0-9a-fA-F]/.test(format[i + 1])) {
|
|
1107
|
+
var hexEnd = i + 2;
|
|
1108
|
+
if (hexEnd < format.length && /[0-9a-fA-F]/.test(format[hexEnd])) hexEnd++;
|
|
1109
|
+
var hex = format.substring(i + 1, hexEnd);
|
|
1110
|
+
result += String.fromCharCode(parseInt(hex, 16));
|
|
1111
|
+
i = hexEnd - 1;
|
|
1112
|
+
} else result += "x";
|
|
1113
|
+
} else result += "\\" + format[i];
|
|
1114
|
+
} else if (format[i] === "%") {
|
|
1115
|
+
i++;
|
|
1116
|
+
if (i >= format.length) break;
|
|
1117
|
+
if (format[i] === "s") result += params[pi++] || "";
|
|
1118
|
+
else if (format[i] === "d") result += parseInt(params[pi++] || "0", 10).toString();
|
|
1119
|
+
else if (format[i] === "%") result += "%";
|
|
1120
|
+
else result += "%" + format[i];
|
|
1121
|
+
} else {
|
|
1122
|
+
result += format[i];
|
|
1123
|
+
}
|
|
1124
|
+
i++;
|
|
1125
|
+
}
|
|
1126
|
+
process.stdout.write(result);
|
|
1127
|
+
return 0;
|
|
1128
|
+
};
|
|
1129
|
+
}
|
|
1130
|
+
});
|
|
1131
|
+
|
|
1132
|
+
// projects/@machina.at/shell/src/commands/find.js
|
|
1133
|
+
var require_find = __commonJS({
|
|
1134
|
+
"projects/@machina.at/shell/src/commands/find.js"(exports2, module2) {
|
|
1135
|
+
"use strict";
|
|
1136
|
+
module2.exports = function find2(args, ctx) {
|
|
1137
|
+
var searchPath = ".", namePattern = "", typeFilter = "";
|
|
1138
|
+
for (var i = 0; i < args.length; i++) {
|
|
1139
|
+
if (args[i] === "-name" && args[i + 1]) namePattern = args[++i];
|
|
1140
|
+
else if (args[i] === "-type" && args[i + 1]) typeFilter = args[++i];
|
|
1141
|
+
else if (!args[i].startsWith("-")) searchPath = args[i];
|
|
1142
|
+
}
|
|
1143
|
+
var root = searchPath.startsWith("/") ? searchPath : ctx.path.join(ctx.cwd, searchPath);
|
|
1144
|
+
try {
|
|
1145
|
+
walk(root, namePattern, typeFilter, ctx);
|
|
1146
|
+
return 0;
|
|
1147
|
+
} catch (err2) {
|
|
1148
|
+
process.stderr.write("find: " + searchPath + ": " + err2.message + "\n");
|
|
1149
|
+
return 1;
|
|
1150
|
+
}
|
|
1151
|
+
};
|
|
1152
|
+
function walk(dir, namePattern, typeFilter, ctx) {
|
|
1153
|
+
var entries = ctx.fs.readdirSync(dir);
|
|
1154
|
+
for (var i = 0; i < entries.length; i++) {
|
|
1155
|
+
var full = ctx.path.join(dir, entries[i]);
|
|
1156
|
+
var stat;
|
|
1157
|
+
try {
|
|
1158
|
+
stat = ctx.fs.statSync(full);
|
|
1159
|
+
} catch (_) {
|
|
1160
|
+
continue;
|
|
1161
|
+
}
|
|
1162
|
+
var isDir = stat.isDirectory();
|
|
1163
|
+
if (typeFilter === "f" && isDir) {
|
|
1164
|
+
} else if (typeFilter === "d" && !isDir) {
|
|
1165
|
+
} else if (!namePattern || matchGlob2(entries[i], namePattern)) {
|
|
1166
|
+
process.stdout.write(full + "\n");
|
|
1167
|
+
}
|
|
1168
|
+
if (isDir) walk(full, namePattern, typeFilter, ctx);
|
|
1169
|
+
}
|
|
1170
|
+
}
|
|
1171
|
+
function matchGlob2(name, pattern) {
|
|
1172
|
+
var re = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*").replace(/\?/g, ".");
|
|
1173
|
+
return new RegExp("^" + re + "$").test(name);
|
|
1174
|
+
}
|
|
1175
|
+
}
|
|
1176
|
+
});
|
|
1177
|
+
|
|
1178
|
+
// projects/@machina.at/shell/src/commands/xargs.js
|
|
1179
|
+
var require_xargs = __commonJS({
|
|
1180
|
+
"projects/@machina.at/shell/src/commands/xargs.js"(exports2, module2) {
|
|
1181
|
+
"use strict";
|
|
1182
|
+
module2.exports = function xargs2(args, ctx) {
|
|
1183
|
+
var cmdParts = [];
|
|
1184
|
+
for (var i = 0; i < args.length; i++) {
|
|
1185
|
+
if (!args[i].startsWith("-")) cmdParts.push(args[i]);
|
|
1186
|
+
}
|
|
1187
|
+
var cmdName = cmdParts.length > 0 ? cmdParts.join(" ") : "echo";
|
|
1188
|
+
var input = ctx.stdin || "";
|
|
1189
|
+
var items = input.trim().split(/\s+/).filter(Boolean);
|
|
1190
|
+
if (items.length === 0) return 0;
|
|
1191
|
+
process.stdout.write(cmdName + " " + items.join(" ") + "\n");
|
|
1192
|
+
return 0;
|
|
1193
|
+
};
|
|
1194
|
+
}
|
|
1195
|
+
});
|
|
1196
|
+
|
|
1197
|
+
// projects/@machina.at/shell/src/commands/sleep.js
|
|
1198
|
+
var require_sleep = __commonJS({
|
|
1199
|
+
"projects/@machina.at/shell/src/commands/sleep.js"(exports2, module2) {
|
|
1200
|
+
"use strict";
|
|
1201
|
+
module2.exports = function sleep2(args) {
|
|
1202
|
+
if (args.length === 0) {
|
|
1203
|
+
process.stderr.write("sleep: missing operand\n");
|
|
1204
|
+
return 1;
|
|
1205
|
+
}
|
|
1206
|
+
var seconds = parseFloat(args[0]);
|
|
1207
|
+
if (isNaN(seconds) || seconds < 0) {
|
|
1208
|
+
process.stderr.write("sleep: invalid time interval '" + args[0] + "'\n");
|
|
1209
|
+
return 1;
|
|
1210
|
+
}
|
|
1211
|
+
return 0;
|
|
1212
|
+
};
|
|
1213
|
+
}
|
|
1214
|
+
});
|
|
1215
|
+
|
|
1216
|
+
// projects/@machina.at/shell/src/commands/date.js
|
|
1217
|
+
var require_date = __commonJS({
|
|
1218
|
+
"projects/@machina.at/shell/src/commands/date.js"(exports2, module2) {
|
|
1219
|
+
"use strict";
|
|
1220
|
+
module2.exports = function date2(args) {
|
|
1221
|
+
var now = /* @__PURE__ */ new Date();
|
|
1222
|
+
if (args.length === 0 || !args[0].startsWith("+")) {
|
|
1223
|
+
process.stdout.write(now.toString() + "\n");
|
|
1224
|
+
return 0;
|
|
1225
|
+
}
|
|
1226
|
+
var fmt = args[0].substring(1);
|
|
1227
|
+
var result = fmt.replace(/%Y/g, now.getFullYear().toString()).replace(/%m/g, String(now.getMonth() + 1).padStart(2, "0")).replace(/%d/g, String(now.getDate()).padStart(2, "0")).replace(/%H/g, String(now.getHours()).padStart(2, "0")).replace(/%M/g, String(now.getMinutes()).padStart(2, "0")).replace(/%S/g, String(now.getSeconds()).padStart(2, "0")).replace(/%%/g, "%");
|
|
1228
|
+
process.stdout.write(result + "\n");
|
|
1229
|
+
return 0;
|
|
1230
|
+
};
|
|
1231
|
+
}
|
|
1232
|
+
});
|
|
1233
|
+
|
|
1234
|
+
// projects/@machina.at/shell/src/commands/basename.js
|
|
1235
|
+
var require_basename = __commonJS({
|
|
1236
|
+
"projects/@machina.at/shell/src/commands/basename.js"(exports2, module2) {
|
|
1237
|
+
"use strict";
|
|
1238
|
+
module2.exports = function basename2(args, ctx) {
|
|
1239
|
+
if (args.length === 0) {
|
|
1240
|
+
process.stderr.write("basename: missing operand\n");
|
|
1241
|
+
return 1;
|
|
1242
|
+
}
|
|
1243
|
+
var name = ctx.path.basename(args[0]);
|
|
1244
|
+
if (args[1] && name.endsWith(args[1])) name = name.slice(0, -args[1].length);
|
|
1245
|
+
process.stdout.write(name + "\n");
|
|
1246
|
+
return 0;
|
|
1247
|
+
};
|
|
1248
|
+
}
|
|
1249
|
+
});
|
|
1250
|
+
|
|
1251
|
+
// projects/@machina.at/shell/src/commands/dirname.js
|
|
1252
|
+
var require_dirname = __commonJS({
|
|
1253
|
+
"projects/@machina.at/shell/src/commands/dirname.js"(exports2, module2) {
|
|
1254
|
+
"use strict";
|
|
1255
|
+
module2.exports = function dirname2(args, ctx) {
|
|
1256
|
+
if (args.length === 0) {
|
|
1257
|
+
process.stderr.write("dirname: missing operand\n");
|
|
1258
|
+
return 1;
|
|
1259
|
+
}
|
|
1260
|
+
process.stdout.write(ctx.path.dirname(args[0]) + "\n");
|
|
1261
|
+
return 0;
|
|
1262
|
+
};
|
|
1263
|
+
}
|
|
1264
|
+
});
|
|
1265
|
+
|
|
1266
|
+
// projects/@machina.at/shell/src/commands/bit.js
|
|
1267
|
+
var require_bit = __commonJS({
|
|
1268
|
+
"projects/@machina.at/shell/src/commands/bit.js"(exports2, module2) {
|
|
1269
|
+
"use strict";
|
|
1270
|
+
var _bit = null;
|
|
1271
|
+
function getBit() {
|
|
1272
|
+
if (!_bit) {
|
|
1273
|
+
_bit = require("@mizchi/bit");
|
|
1274
|
+
}
|
|
1275
|
+
return _bit;
|
|
1276
|
+
}
|
|
1277
|
+
function createVfsBackend(fs2, pathMod) {
|
|
1278
|
+
return {
|
|
1279
|
+
readFile: function(p) {
|
|
1280
|
+
var data = fs2.readFileSync(p);
|
|
1281
|
+
if (data instanceof Uint8Array) return data;
|
|
1282
|
+
if (data && data.buffer) return new Uint8Array(data.buffer);
|
|
1283
|
+
return new Uint8Array(data);
|
|
1284
|
+
},
|
|
1285
|
+
writeFile: function(p, data) {
|
|
1286
|
+
var dir = pathMod.dirname(p);
|
|
1287
|
+
try {
|
|
1288
|
+
fs2.mkdirSync(dir, { recursive: true });
|
|
1289
|
+
} catch (_e) {
|
|
1290
|
+
}
|
|
1291
|
+
fs2.writeFileSync(p, Buffer.from(data));
|
|
1292
|
+
},
|
|
1293
|
+
readdir: function(p) {
|
|
1294
|
+
try {
|
|
1295
|
+
return fs2.readdirSync(p);
|
|
1296
|
+
} catch (e) {
|
|
1297
|
+
return [];
|
|
1298
|
+
}
|
|
1299
|
+
},
|
|
1300
|
+
isDir: function(p) {
|
|
1301
|
+
try {
|
|
1302
|
+
return fs2.statSync(p).isDirectory();
|
|
1303
|
+
} catch (e) {
|
|
1304
|
+
return false;
|
|
1305
|
+
}
|
|
1306
|
+
},
|
|
1307
|
+
isFile: function(p) {
|
|
1308
|
+
try {
|
|
1309
|
+
return fs2.statSync(p).isFile();
|
|
1310
|
+
} catch (e) {
|
|
1311
|
+
return false;
|
|
1312
|
+
}
|
|
1313
|
+
},
|
|
1314
|
+
mkdirP: function(p) {
|
|
1315
|
+
fs2.mkdirSync(p, { recursive: true });
|
|
1316
|
+
},
|
|
1317
|
+
removeFile: function(p) {
|
|
1318
|
+
try {
|
|
1319
|
+
fs2.unlinkSync(p);
|
|
1320
|
+
} catch (_e) {
|
|
1321
|
+
}
|
|
1322
|
+
},
|
|
1323
|
+
removeDir: function(p) {
|
|
1324
|
+
try {
|
|
1325
|
+
fs2.rmdirSync(p);
|
|
1326
|
+
} catch (_e) {
|
|
1327
|
+
}
|
|
1328
|
+
}
|
|
1329
|
+
};
|
|
1330
|
+
}
|
|
1331
|
+
function handleInit(bit, backend, args, ctx2) {
|
|
1332
|
+
var branchName = "main";
|
|
1333
|
+
for (var i = 0; i < args.length; i++) {
|
|
1334
|
+
if (args[i] === "-b" && args[i + 1]) {
|
|
1335
|
+
branchName = args[++i];
|
|
1336
|
+
}
|
|
1337
|
+
}
|
|
1338
|
+
bit.init(backend, ctx2.cwd, branchName);
|
|
1339
|
+
process.stdout.write("Initialized empty Git repository in " + ctx2.cwd + "/.git/\n");
|
|
1340
|
+
return 0;
|
|
1341
|
+
}
|
|
1342
|
+
function handleAdd(bit, backend, args, ctx2) {
|
|
1343
|
+
if (args.length === 0) {
|
|
1344
|
+
process.stderr.write("usage: bit add <pathspec>...\n");
|
|
1345
|
+
return 1;
|
|
1346
|
+
}
|
|
1347
|
+
var paths = [];
|
|
1348
|
+
for (var i = 0; i < args.length; i++) {
|
|
1349
|
+
if (args[i] === ".") {
|
|
1350
|
+
paths.push(".");
|
|
1351
|
+
} else {
|
|
1352
|
+
paths.push(args[i]);
|
|
1353
|
+
}
|
|
1354
|
+
}
|
|
1355
|
+
bit.add(backend, ctx2.cwd, paths);
|
|
1356
|
+
return 0;
|
|
1357
|
+
}
|
|
1358
|
+
function handleCommit(bit, backend, args, ctx2) {
|
|
1359
|
+
var message = "";
|
|
1360
|
+
for (var i = 0; i < args.length; i++) {
|
|
1361
|
+
if ((args[i] === "-m" || args[i] === "--message") && args[i + 1]) {
|
|
1362
|
+
message = args[++i];
|
|
1363
|
+
}
|
|
1364
|
+
}
|
|
1365
|
+
if (!message) {
|
|
1366
|
+
process.stderr.write("error: empty commit message\n");
|
|
1367
|
+
return 1;
|
|
1368
|
+
}
|
|
1369
|
+
var author = ctx2.env && ctx2.env.GIT_AUTHOR_EMAIL || "user@web-kernel";
|
|
1370
|
+
var ts = Math.floor(Date.now() / 1e3);
|
|
1371
|
+
bit.commit(backend, ctx2.cwd, message, author, ts);
|
|
1372
|
+
process.stdout.write("[main] " + message + "\n");
|
|
1373
|
+
return 0;
|
|
1374
|
+
}
|
|
1375
|
+
function handleStatus(bit, backend, args, ctx2) {
|
|
1376
|
+
var result = bit.statusText(backend, ctx2.cwd);
|
|
1377
|
+
if (result) {
|
|
1378
|
+
process.stdout.write(result + "\n");
|
|
1379
|
+
} else {
|
|
1380
|
+
process.stdout.write("nothing to commit, working tree clean\n");
|
|
1381
|
+
}
|
|
1382
|
+
return 0;
|
|
1383
|
+
}
|
|
1384
|
+
function handleLog(bit, backend, args, ctx2) {
|
|
1385
|
+
var maxCount = 20;
|
|
1386
|
+
for (var i = 0; i < args.length; i++) {
|
|
1387
|
+
if (args[i] === "-n" && args[i + 1]) {
|
|
1388
|
+
maxCount = parseInt(args[++i], 10);
|
|
1389
|
+
}
|
|
1390
|
+
if (args[i].match(/^-\d+$/)) {
|
|
1391
|
+
maxCount = parseInt(args[i].slice(1), 10);
|
|
1392
|
+
}
|
|
1393
|
+
}
|
|
1394
|
+
var entries = bit.log(backend, ctx2.cwd, maxCount);
|
|
1395
|
+
if (!entries || entries.length === 0) {
|
|
1396
|
+
process.stderr.write("fatal: no commits yet\n");
|
|
1397
|
+
return 1;
|
|
1398
|
+
}
|
|
1399
|
+
for (var j = 0; j < entries.length; j++) {
|
|
1400
|
+
var e = entries[j];
|
|
1401
|
+
var hash = (e.id || "").substring(0, 7);
|
|
1402
|
+
process.stdout.write(hash + " " + (e.message || "") + "\n");
|
|
1403
|
+
}
|
|
1404
|
+
return 0;
|
|
1405
|
+
}
|
|
1406
|
+
function handleBranch(bit, backend, args, ctx2) {
|
|
1407
|
+
if (args.length === 0) {
|
|
1408
|
+
var branches = bit.branchList(backend, ctx2.cwd);
|
|
1409
|
+
if (branches && branches.length > 0) {
|
|
1410
|
+
for (var i = 0; i < branches.length; i++) {
|
|
1411
|
+
process.stdout.write(" " + branches[i] + "\n");
|
|
1412
|
+
}
|
|
1413
|
+
}
|
|
1414
|
+
return 0;
|
|
1415
|
+
}
|
|
1416
|
+
bit.branchCreate(backend, ctx2.cwd, args[0]);
|
|
1417
|
+
return 0;
|
|
1418
|
+
}
|
|
1419
|
+
function handleCheckout(bit, backend, args, ctx2) {
|
|
1420
|
+
if (args.length === 0) {
|
|
1421
|
+
process.stderr.write("usage: bit checkout <branch>\n");
|
|
1422
|
+
return 1;
|
|
1423
|
+
}
|
|
1424
|
+
bit.checkout(backend, ctx2.cwd, args[0]);
|
|
1425
|
+
process.stdout.write("Switched to branch '" + args[0] + "'\n");
|
|
1426
|
+
return 0;
|
|
1427
|
+
}
|
|
1428
|
+
function handleDiff(bit, backend, args, ctx2) {
|
|
1429
|
+
var result = bit.diffWorktree(backend, ctx2.cwd);
|
|
1430
|
+
if (result) {
|
|
1431
|
+
process.stdout.write(result + "\n");
|
|
1432
|
+
}
|
|
1433
|
+
return 0;
|
|
1434
|
+
}
|
|
1435
|
+
function showHelp() {
|
|
1436
|
+
process.stdout.write(
|
|
1437
|
+
"usage: bit <command> [<args>]\n\ncommands:\n init Create an empty Git repository\n add Add file contents to the index\n commit Record changes to the repository\n status Show the working tree status\n log Show commit logs\n branch List or create branches\n checkout Switch branches\n diff Show changes in working tree\n"
|
|
1438
|
+
);
|
|
1439
|
+
return 0;
|
|
1440
|
+
}
|
|
1441
|
+
function runBit(args, ctx2) {
|
|
1442
|
+
if (args.length === 0 || args[0] === "--help" || args[0] === "-h") {
|
|
1443
|
+
return showHelp();
|
|
1444
|
+
}
|
|
1445
|
+
if (args[0] === "--version" || args[0] === "-v") {
|
|
1446
|
+
process.stdout.write("bit (web-kernel) using @mizchi/bit JS API\n");
|
|
1447
|
+
return 0;
|
|
1448
|
+
}
|
|
1449
|
+
try {
|
|
1450
|
+
var lib = getBit();
|
|
1451
|
+
var backend = createVfsBackend(ctx2.fs, ctx2.path);
|
|
1452
|
+
var subArgs = args.slice(1);
|
|
1453
|
+
switch (args[0]) {
|
|
1454
|
+
case "init":
|
|
1455
|
+
return handleInit(lib, backend, subArgs, ctx2);
|
|
1456
|
+
case "add":
|
|
1457
|
+
return handleAdd(lib, backend, subArgs, ctx2);
|
|
1458
|
+
case "commit":
|
|
1459
|
+
return handleCommit(lib, backend, subArgs, ctx2);
|
|
1460
|
+
case "status":
|
|
1461
|
+
return handleStatus(lib, backend, subArgs, ctx2);
|
|
1462
|
+
case "log":
|
|
1463
|
+
return handleLog(lib, backend, subArgs, ctx2);
|
|
1464
|
+
case "branch":
|
|
1465
|
+
return handleBranch(lib, backend, subArgs, ctx2);
|
|
1466
|
+
case "checkout":
|
|
1467
|
+
return handleCheckout(lib, backend, subArgs, ctx2);
|
|
1468
|
+
case "diff":
|
|
1469
|
+
return handleDiff(lib, backend, subArgs, ctx2);
|
|
1470
|
+
default:
|
|
1471
|
+
process.stderr.write("bit: '" + args[0] + "' is not a bit command\n");
|
|
1472
|
+
return 1;
|
|
1473
|
+
}
|
|
1474
|
+
} catch (err2) {
|
|
1475
|
+
process.stderr.write("bit: " + (err2.message || err2) + "\n");
|
|
1476
|
+
return 1;
|
|
1477
|
+
}
|
|
1478
|
+
}
|
|
1479
|
+
module2.exports = runBit;
|
|
1480
|
+
if (typeof require !== "undefined" && require.main === module2) {
|
|
1481
|
+
fs = require("fs");
|
|
1482
|
+
path = require("path");
|
|
1483
|
+
cliArgs = process.argv.slice(2);
|
|
1484
|
+
ctx = { cwd: process.cwd(), env: process.env, fs, path };
|
|
1485
|
+
code = runBit(cliArgs, ctx);
|
|
1486
|
+
process.exit(code);
|
|
1487
|
+
}
|
|
1488
|
+
var fs;
|
|
1489
|
+
var path;
|
|
1490
|
+
var cliArgs;
|
|
1491
|
+
var ctx;
|
|
1492
|
+
var code;
|
|
1493
|
+
}
|
|
1494
|
+
});
|
|
1495
|
+
|
|
1496
|
+
// projects/@machina.at/shell/src/lib/util-commands.ts
|
|
1497
|
+
var util_commands_exports = {};
|
|
1498
|
+
__export(util_commands_exports, {
|
|
1499
|
+
basename: () => basename,
|
|
1500
|
+
date: () => date,
|
|
1501
|
+
dirname: () => dirname,
|
|
1502
|
+
envCmd: () => envCmd,
|
|
1503
|
+
expr: () => expr,
|
|
1504
|
+
find: () => find,
|
|
1505
|
+
mktemp: () => mktemp,
|
|
1506
|
+
readlink: () => readlink,
|
|
1507
|
+
realpath: () => realpath,
|
|
1508
|
+
seq: () => seq,
|
|
1509
|
+
sleep: () => sleep,
|
|
1510
|
+
uname: () => uname,
|
|
1511
|
+
whoami: () => whoami,
|
|
1512
|
+
xargs: () => xargs,
|
|
1513
|
+
yes: () => yes
|
|
1514
|
+
});
|
|
1515
|
+
function resolve(p, ctx) {
|
|
1516
|
+
return p.startsWith("/") ? p : ctx.path.join(ctx.cwd, p);
|
|
1517
|
+
}
|
|
1518
|
+
function find(args, ctx) {
|
|
1519
|
+
let searchPath = ".";
|
|
1520
|
+
let namePattern = "";
|
|
1521
|
+
let typeFilter = "";
|
|
1522
|
+
for (let i = 0; i < args.length; i++) {
|
|
1523
|
+
if (args[i] === "-name" && args[i + 1]) namePattern = args[++i];
|
|
1524
|
+
else if (args[i] === "-type" && args[i + 1]) typeFilter = args[++i];
|
|
1525
|
+
else if (!args[i].startsWith("-")) searchPath = args[i];
|
|
1526
|
+
}
|
|
1527
|
+
const root = resolve(searchPath, ctx);
|
|
1528
|
+
try {
|
|
1529
|
+
walkDir(root, root, namePattern, typeFilter, ctx);
|
|
1530
|
+
return 0;
|
|
1531
|
+
} catch (err2) {
|
|
1532
|
+
ctx.stderr.write("find: " + searchPath + ": " + err2.message + "\n");
|
|
1533
|
+
return 1;
|
|
1534
|
+
}
|
|
1535
|
+
}
|
|
1536
|
+
function walkDir(root, dir, namePattern, typeFilter, ctx) {
|
|
1537
|
+
const entries = ctx.fs.readdirSync(dir);
|
|
1538
|
+
for (const entry of entries) {
|
|
1539
|
+
const full = ctx.path.join(dir, entry);
|
|
1540
|
+
let stat;
|
|
1541
|
+
try {
|
|
1542
|
+
stat = ctx.fs.statSync(full);
|
|
1543
|
+
} catch {
|
|
1544
|
+
continue;
|
|
1545
|
+
}
|
|
1546
|
+
const isDir = stat.isDirectory();
|
|
1547
|
+
if (typeFilter === "f" && isDir) {
|
|
1548
|
+
} else if (typeFilter === "d" && !isDir) {
|
|
1549
|
+
} else {
|
|
1550
|
+
if (!namePattern || matchGlob(entry, namePattern)) {
|
|
1551
|
+
ctx.stdout.write(full + "\n");
|
|
1552
|
+
}
|
|
1553
|
+
}
|
|
1554
|
+
if (isDir) walkDir(root, full, namePattern, typeFilter, ctx);
|
|
1555
|
+
}
|
|
1556
|
+
}
|
|
1557
|
+
function matchGlob(name, pattern) {
|
|
1558
|
+
const re = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*").replace(/\?/g, ".");
|
|
1559
|
+
return new RegExp("^" + re + "$").test(name);
|
|
1560
|
+
}
|
|
1561
|
+
function xargs(args, ctx, executeCommand) {
|
|
1562
|
+
const cmdParts = [];
|
|
1563
|
+
for (const arg of args) {
|
|
1564
|
+
if (!arg.startsWith("-")) cmdParts.push(arg);
|
|
1565
|
+
}
|
|
1566
|
+
const cmdName = cmdParts.length > 0 ? cmdParts.join(" ") : "echo";
|
|
1567
|
+
const input = ctx.stdin ?? "";
|
|
1568
|
+
const items = input.trim().split(/\s+/).filter(Boolean);
|
|
1569
|
+
if (items.length === 0) return 0;
|
|
1570
|
+
if (executeCommand) {
|
|
1571
|
+
return executeCommand(cmdName + " " + items.join(" "));
|
|
1572
|
+
}
|
|
1573
|
+
ctx.stdout.write(cmdName + " " + items.join(" ") + "\n");
|
|
1574
|
+
return 0;
|
|
1575
|
+
}
|
|
1576
|
+
function sleep(args, ctx) {
|
|
1577
|
+
if (args.length === 0) {
|
|
1578
|
+
ctx.stderr.write("sleep: missing operand\n");
|
|
1579
|
+
return 1;
|
|
1580
|
+
}
|
|
1581
|
+
const seconds = parseFloat(args[0]);
|
|
1582
|
+
if (isNaN(seconds) || seconds < 0) {
|
|
1583
|
+
ctx.stderr.write("sleep: invalid time interval '" + args[0] + "'\n");
|
|
1584
|
+
return 1;
|
|
1585
|
+
}
|
|
1586
|
+
return 0;
|
|
1587
|
+
}
|
|
1588
|
+
function date(args, ctx) {
|
|
1589
|
+
const now = /* @__PURE__ */ new Date();
|
|
1590
|
+
if (args.length === 0) {
|
|
1591
|
+
ctx.stdout.write(now.toString() + "\n");
|
|
1592
|
+
return 0;
|
|
1593
|
+
}
|
|
1594
|
+
if (args[0].startsWith("+")) {
|
|
1595
|
+
const fmt = args[0].substring(1);
|
|
1596
|
+
const result = formatDate(now, fmt);
|
|
1597
|
+
ctx.stdout.write(result + "\n");
|
|
1598
|
+
return 0;
|
|
1599
|
+
}
|
|
1600
|
+
ctx.stdout.write(now.toString() + "\n");
|
|
1601
|
+
return 0;
|
|
1602
|
+
}
|
|
1603
|
+
function formatDate(d, fmt) {
|
|
1604
|
+
return fmt.replace(/%Y/g, d.getFullYear().toString()).replace(/%m/g, String(d.getMonth() + 1).padStart(2, "0")).replace(/%d/g, String(d.getDate()).padStart(2, "0")).replace(/%H/g, String(d.getHours()).padStart(2, "0")).replace(/%M/g, String(d.getMinutes()).padStart(2, "0")).replace(/%S/g, String(d.getSeconds()).padStart(2, "0")).replace(/%%/g, "%");
|
|
1605
|
+
}
|
|
1606
|
+
function basename(args, ctx) {
|
|
1607
|
+
if (args.length === 0) {
|
|
1608
|
+
ctx.stderr.write("basename: missing operand\n");
|
|
1609
|
+
return 1;
|
|
1610
|
+
}
|
|
1611
|
+
let name = ctx.path.basename(args[0]);
|
|
1612
|
+
if (args[1] && name.endsWith(args[1])) {
|
|
1613
|
+
name = name.slice(0, -args[1].length);
|
|
1614
|
+
}
|
|
1615
|
+
ctx.stdout.write(name + "\n");
|
|
1616
|
+
return 0;
|
|
1617
|
+
}
|
|
1618
|
+
function dirname(args, ctx) {
|
|
1619
|
+
if (args.length === 0) {
|
|
1620
|
+
ctx.stderr.write("dirname: missing operand\n");
|
|
1621
|
+
return 1;
|
|
1622
|
+
}
|
|
1623
|
+
ctx.stdout.write(ctx.path.dirname(args[0]) + "\n");
|
|
1624
|
+
return 0;
|
|
1625
|
+
}
|
|
1626
|
+
function seq(args, ctx) {
|
|
1627
|
+
if (args.length === 0) {
|
|
1628
|
+
ctx.stderr.write("seq: missing operand\n");
|
|
1629
|
+
return 1;
|
|
1630
|
+
}
|
|
1631
|
+
let first = 1, incr = 1, last;
|
|
1632
|
+
if (args.length === 1) {
|
|
1633
|
+
last = parseFloat(args[0]);
|
|
1634
|
+
} else if (args.length === 2) {
|
|
1635
|
+
first = parseFloat(args[0]);
|
|
1636
|
+
last = parseFloat(args[1]);
|
|
1637
|
+
} else {
|
|
1638
|
+
first = parseFloat(args[0]);
|
|
1639
|
+
incr = parseFloat(args[1]);
|
|
1640
|
+
last = parseFloat(args[2]);
|
|
1641
|
+
}
|
|
1642
|
+
if (incr === 0) {
|
|
1643
|
+
ctx.stderr.write("seq: zero increment\n");
|
|
1644
|
+
return 1;
|
|
1645
|
+
}
|
|
1646
|
+
if (incr > 0) {
|
|
1647
|
+
for (let i = first; i <= last; i += incr) ctx.stdout.write(i + "\n");
|
|
1648
|
+
} else {
|
|
1649
|
+
for (let i = first; i >= last; i += incr) ctx.stdout.write(i + "\n");
|
|
1650
|
+
}
|
|
1651
|
+
return 0;
|
|
1652
|
+
}
|
|
1653
|
+
function readlink(args, ctx) {
|
|
1654
|
+
let followAll = false;
|
|
1655
|
+
const targets = [];
|
|
1656
|
+
for (const arg of args) {
|
|
1657
|
+
if (arg === "-f") followAll = true;
|
|
1658
|
+
else if (!arg.startsWith("-")) targets.push(arg);
|
|
1659
|
+
}
|
|
1660
|
+
if (targets.length === 0) {
|
|
1661
|
+
ctx.stderr.write("readlink: missing operand\n");
|
|
1662
|
+
return 1;
|
|
1663
|
+
}
|
|
1664
|
+
for (const target of targets) {
|
|
1665
|
+
const p = resolve(target, ctx);
|
|
1666
|
+
try {
|
|
1667
|
+
if (followAll && ctx.fs.realpathSync) {
|
|
1668
|
+
ctx.stdout.write(ctx.fs.realpathSync(p) + "\n");
|
|
1669
|
+
} else if (ctx.fs.readlinkSync) {
|
|
1670
|
+
ctx.stdout.write(ctx.fs.readlinkSync(p) + "\n");
|
|
1671
|
+
} else {
|
|
1672
|
+
ctx.stderr.write("readlink: not supported\n");
|
|
1673
|
+
return 1;
|
|
1674
|
+
}
|
|
1675
|
+
} catch (err2) {
|
|
1676
|
+
ctx.stderr.write("readlink: " + target + ": " + err2.message + "\n");
|
|
1677
|
+
return 1;
|
|
1678
|
+
}
|
|
1679
|
+
}
|
|
1680
|
+
return 0;
|
|
1681
|
+
}
|
|
1682
|
+
function realpath(args, ctx) {
|
|
1683
|
+
const targets = args.filter((a) => !a.startsWith("-"));
|
|
1684
|
+
if (targets.length === 0) {
|
|
1685
|
+
ctx.stderr.write("realpath: missing operand\n");
|
|
1686
|
+
return 1;
|
|
1687
|
+
}
|
|
1688
|
+
for (const target of targets) {
|
|
1689
|
+
const p = resolve(target, ctx);
|
|
1690
|
+
if (ctx.fs.realpathSync) {
|
|
1691
|
+
try {
|
|
1692
|
+
ctx.stdout.write(ctx.fs.realpathSync(p) + "\n");
|
|
1693
|
+
} catch (err2) {
|
|
1694
|
+
ctx.stderr.write("realpath: " + target + ": " + err2.message + "\n");
|
|
1695
|
+
return 1;
|
|
1696
|
+
}
|
|
1697
|
+
} else {
|
|
1698
|
+
ctx.stdout.write(ctx.path.resolve(p) + "\n");
|
|
1699
|
+
}
|
|
1700
|
+
}
|
|
1701
|
+
return 0;
|
|
1702
|
+
}
|
|
1703
|
+
function mktemp(args, ctx) {
|
|
1704
|
+
let isDir = false;
|
|
1705
|
+
let template = "/tmp/tmp.XXXXXXXXXX";
|
|
1706
|
+
for (const arg of args) {
|
|
1707
|
+
if (arg === "-d") isDir = true;
|
|
1708
|
+
else if (!arg.startsWith("-")) template = arg;
|
|
1709
|
+
}
|
|
1710
|
+
const chars = "abcdefghijklmnopqrstuvwxyz0123456789";
|
|
1711
|
+
const name = template.replace(/X+/g, (match) => {
|
|
1712
|
+
let result = "";
|
|
1713
|
+
for (let i = 0; i < match.length; i++) {
|
|
1714
|
+
result += chars[Math.floor(Math.random() * chars.length)];
|
|
1715
|
+
}
|
|
1716
|
+
return result;
|
|
1717
|
+
});
|
|
1718
|
+
const p = resolve(name, ctx);
|
|
1719
|
+
try {
|
|
1720
|
+
if (isDir) {
|
|
1721
|
+
ctx.fs.mkdirSync(p);
|
|
1722
|
+
} else {
|
|
1723
|
+
ctx.fs.writeFileSync(p, "");
|
|
1724
|
+
}
|
|
1725
|
+
ctx.stdout.write(p + "\n");
|
|
1726
|
+
return 0;
|
|
1727
|
+
} catch (err2) {
|
|
1728
|
+
ctx.stderr.write("mktemp: " + err2.message + "\n");
|
|
1729
|
+
return 1;
|
|
1730
|
+
}
|
|
1731
|
+
}
|
|
1732
|
+
function expr(args, ctx) {
|
|
1733
|
+
if (args.length === 0) {
|
|
1734
|
+
ctx.stderr.write("expr: missing operand\n");
|
|
1735
|
+
return 1;
|
|
1736
|
+
}
|
|
1737
|
+
if (args[0] === "length" && args[1]) {
|
|
1738
|
+
ctx.stdout.write(args[1].length + "\n");
|
|
1739
|
+
return args[1].length === 0 ? 1 : 0;
|
|
1740
|
+
}
|
|
1741
|
+
if (args[0] === "substr" && args.length >= 4) {
|
|
1742
|
+
const str = args[1];
|
|
1743
|
+
const pos = parseInt(args[2], 10) - 1;
|
|
1744
|
+
const len = parseInt(args[3], 10);
|
|
1745
|
+
ctx.stdout.write(str.substring(pos, pos + len) + "\n");
|
|
1746
|
+
return 0;
|
|
1747
|
+
}
|
|
1748
|
+
if (args.length === 3) {
|
|
1749
|
+
const [left, op, right] = args;
|
|
1750
|
+
const a = parseInt(left, 10);
|
|
1751
|
+
const b = parseInt(right, 10);
|
|
1752
|
+
if (!isNaN(a) && !isNaN(b)) {
|
|
1753
|
+
let result;
|
|
1754
|
+
switch (op) {
|
|
1755
|
+
case "+":
|
|
1756
|
+
result = a + b;
|
|
1757
|
+
break;
|
|
1758
|
+
case "-":
|
|
1759
|
+
result = a - b;
|
|
1760
|
+
break;
|
|
1761
|
+
case "*":
|
|
1762
|
+
result = a * b;
|
|
1763
|
+
break;
|
|
1764
|
+
case "/":
|
|
1765
|
+
result = b === 0 ? 0 : Math.trunc(a / b);
|
|
1766
|
+
break;
|
|
1767
|
+
case "%":
|
|
1768
|
+
result = b === 0 ? 0 : a % b;
|
|
1769
|
+
break;
|
|
1770
|
+
default:
|
|
1771
|
+
ctx.stderr.write("expr: unknown operator: " + op + "\n");
|
|
1772
|
+
return 2;
|
|
1773
|
+
}
|
|
1774
|
+
ctx.stdout.write(result + "\n");
|
|
1775
|
+
return result === 0 ? 1 : 0;
|
|
1776
|
+
}
|
|
1777
|
+
if (op === ":") {
|
|
1778
|
+
const re = new RegExp("^" + right);
|
|
1779
|
+
const match = left.match(re);
|
|
1780
|
+
if (match) {
|
|
1781
|
+
ctx.stdout.write((match[1] ?? match[0].length) + "\n");
|
|
1782
|
+
return 0;
|
|
1783
|
+
}
|
|
1784
|
+
ctx.stdout.write("0\n");
|
|
1785
|
+
return 1;
|
|
1786
|
+
}
|
|
1787
|
+
}
|
|
1788
|
+
ctx.stdout.write(args.join(" ") + "\n");
|
|
1789
|
+
return 0;
|
|
1790
|
+
}
|
|
1791
|
+
function yes(args, ctx) {
|
|
1792
|
+
const text = args.length > 0 ? args.join(" ") : "y";
|
|
1793
|
+
const max2 = 1e3;
|
|
1794
|
+
for (let i = 0; i < max2; i++) {
|
|
1795
|
+
ctx.stdout.write(text + "\n");
|
|
1796
|
+
}
|
|
1797
|
+
return 0;
|
|
1798
|
+
}
|
|
1799
|
+
function whoami(args, ctx) {
|
|
1800
|
+
ctx.stdout.write((ctx.env.USER || ctx.env.LOGNAME || "unknown") + "\n");
|
|
1801
|
+
return 0;
|
|
1802
|
+
}
|
|
1803
|
+
function uname(args, ctx) {
|
|
1804
|
+
const sysname = ctx.env._UNAME_S || "VFS";
|
|
1805
|
+
const nodename = ctx.env.HOSTNAME || "localhost";
|
|
1806
|
+
const release = ctx.env._UNAME_R || "1.0.0";
|
|
1807
|
+
const machine = ctx.env._UNAME_M || "wasm32";
|
|
1808
|
+
if (args.length === 0 || args.includes("-s")) {
|
|
1809
|
+
ctx.stdout.write(sysname + "\n");
|
|
1810
|
+
return 0;
|
|
1811
|
+
}
|
|
1812
|
+
if (args.includes("-a")) {
|
|
1813
|
+
ctx.stdout.write([sysname, nodename, release, machine].join(" ") + "\n");
|
|
1814
|
+
return 0;
|
|
1815
|
+
}
|
|
1816
|
+
if (args.includes("-n")) {
|
|
1817
|
+
ctx.stdout.write(nodename + "\n");
|
|
1818
|
+
return 0;
|
|
1819
|
+
}
|
|
1820
|
+
if (args.includes("-r")) {
|
|
1821
|
+
ctx.stdout.write(release + "\n");
|
|
1822
|
+
return 0;
|
|
1823
|
+
}
|
|
1824
|
+
if (args.includes("-m")) {
|
|
1825
|
+
ctx.stdout.write(machine + "\n");
|
|
1826
|
+
return 0;
|
|
1827
|
+
}
|
|
1828
|
+
ctx.stdout.write(sysname + "\n");
|
|
1829
|
+
return 0;
|
|
1830
|
+
}
|
|
1831
|
+
function envCmd(args, ctx) {
|
|
1832
|
+
if (args.length === 0) {
|
|
1833
|
+
for (const [k, v] of Object.entries(ctx.env)) {
|
|
1834
|
+
ctx.stdout.write(k + "=" + v + "\n");
|
|
1835
|
+
}
|
|
1836
|
+
return 0;
|
|
1837
|
+
}
|
|
1838
|
+
for (const arg of args) {
|
|
1839
|
+
const eq = arg.indexOf("=");
|
|
1840
|
+
if (eq !== -1) {
|
|
1841
|
+
ctx.env[arg.substring(0, eq)] = arg.substring(eq + 1);
|
|
1842
|
+
}
|
|
1843
|
+
}
|
|
1844
|
+
return 0;
|
|
1845
|
+
}
|
|
1846
|
+
var init_util_commands = __esm({
|
|
1847
|
+
"projects/@machina.at/shell/src/lib/util-commands.ts"() {
|
|
1848
|
+
"use strict";
|
|
1849
|
+
}
|
|
1850
|
+
});
|
|
1851
|
+
|
|
1852
|
+
// projects/@machina.at/shell/src/commands/seq.js
|
|
1853
|
+
var require_seq = __commonJS({
|
|
1854
|
+
"projects/@machina.at/shell/src/commands/seq.js"(exports2, module2) {
|
|
1855
|
+
"use strict";
|
|
1856
|
+
var lib = (init_util_commands(), __toCommonJS(util_commands_exports));
|
|
1857
|
+
module2.exports = function seq2(args, ctx) {
|
|
1858
|
+
return lib.seq(args, { ...ctx, stdout: process.stdout, stderr: process.stderr });
|
|
1859
|
+
};
|
|
1860
|
+
}
|
|
1861
|
+
});
|
|
1862
|
+
|
|
1863
|
+
// projects/@machina.at/shell/src/commands/readlink.js
|
|
1864
|
+
var require_readlink = __commonJS({
|
|
1865
|
+
"projects/@machina.at/shell/src/commands/readlink.js"(exports2, module2) {
|
|
1866
|
+
"use strict";
|
|
1867
|
+
var lib = (init_util_commands(), __toCommonJS(util_commands_exports));
|
|
1868
|
+
module2.exports = function readlink2(args, ctx) {
|
|
1869
|
+
return lib.readlink(args, { ...ctx, stdout: process.stdout, stderr: process.stderr });
|
|
1870
|
+
};
|
|
1871
|
+
}
|
|
1872
|
+
});
|
|
1873
|
+
|
|
1874
|
+
// projects/@machina.at/shell/src/commands/realpath.js
|
|
1875
|
+
var require_realpath = __commonJS({
|
|
1876
|
+
"projects/@machina.at/shell/src/commands/realpath.js"(exports2, module2) {
|
|
1877
|
+
"use strict";
|
|
1878
|
+
var lib = (init_util_commands(), __toCommonJS(util_commands_exports));
|
|
1879
|
+
module2.exports = function realpath2(args, ctx) {
|
|
1880
|
+
return lib.realpath(args, { ...ctx, stdout: process.stdout, stderr: process.stderr });
|
|
1881
|
+
};
|
|
1882
|
+
}
|
|
1883
|
+
});
|
|
1884
|
+
|
|
1885
|
+
// projects/@machina.at/shell/src/commands/mktemp.js
|
|
1886
|
+
var require_mktemp = __commonJS({
|
|
1887
|
+
"projects/@machina.at/shell/src/commands/mktemp.js"(exports2, module2) {
|
|
1888
|
+
"use strict";
|
|
1889
|
+
var lib = (init_util_commands(), __toCommonJS(util_commands_exports));
|
|
1890
|
+
module2.exports = function mktemp2(args, ctx) {
|
|
1891
|
+
return lib.mktemp(args, { ...ctx, stdout: process.stdout, stderr: process.stderr });
|
|
1892
|
+
};
|
|
1893
|
+
}
|
|
1894
|
+
});
|
|
1895
|
+
|
|
1896
|
+
// projects/@machina.at/shell/src/commands/expr.js
|
|
1897
|
+
var require_expr = __commonJS({
|
|
1898
|
+
"projects/@machina.at/shell/src/commands/expr.js"(exports2, module2) {
|
|
1899
|
+
"use strict";
|
|
1900
|
+
var lib = (init_util_commands(), __toCommonJS(util_commands_exports));
|
|
1901
|
+
module2.exports = function expr2(args, ctx) {
|
|
1902
|
+
return lib.expr(args, { ...ctx, stdout: process.stdout, stderr: process.stderr });
|
|
1903
|
+
};
|
|
1904
|
+
}
|
|
1905
|
+
});
|
|
1906
|
+
|
|
1907
|
+
// projects/@machina.at/shell/src/commands/yes.js
|
|
1908
|
+
var require_yes = __commonJS({
|
|
1909
|
+
"projects/@machina.at/shell/src/commands/yes.js"(exports2, module2) {
|
|
1910
|
+
"use strict";
|
|
1911
|
+
var lib = (init_util_commands(), __toCommonJS(util_commands_exports));
|
|
1912
|
+
module2.exports = function yes2(args, ctx) {
|
|
1913
|
+
return lib.yes(args, { ...ctx, stdout: process.stdout, stderr: process.stderr });
|
|
1914
|
+
};
|
|
1915
|
+
}
|
|
1916
|
+
});
|
|
1917
|
+
|
|
1918
|
+
// projects/@machina.at/shell/src/commands/whoami.js
|
|
1919
|
+
var require_whoami = __commonJS({
|
|
1920
|
+
"projects/@machina.at/shell/src/commands/whoami.js"(exports2, module2) {
|
|
1921
|
+
"use strict";
|
|
1922
|
+
var lib = (init_util_commands(), __toCommonJS(util_commands_exports));
|
|
1923
|
+
module2.exports = function whoami2(args, ctx) {
|
|
1924
|
+
return lib.whoami(args, { ...ctx, stdout: process.stdout, stderr: process.stderr });
|
|
1925
|
+
};
|
|
1926
|
+
}
|
|
1927
|
+
});
|
|
1928
|
+
|
|
1929
|
+
// projects/@machina.at/shell/src/commands/uname.js
|
|
1930
|
+
var require_uname = __commonJS({
|
|
1931
|
+
"projects/@machina.at/shell/src/commands/uname.js"(exports2, module2) {
|
|
1932
|
+
"use strict";
|
|
1933
|
+
var lib = (init_util_commands(), __toCommonJS(util_commands_exports));
|
|
1934
|
+
module2.exports = function uname2(args, ctx) {
|
|
1935
|
+
return lib.uname(args, { ...ctx, stdout: process.stdout, stderr: process.stderr });
|
|
1936
|
+
};
|
|
1937
|
+
}
|
|
1938
|
+
});
|
|
1939
|
+
|
|
1940
|
+
// projects/@machina.at/shell/src/lib/file-commands.ts
|
|
1941
|
+
var file_commands_exports = {};
|
|
1942
|
+
__export(file_commands_exports, {
|
|
1943
|
+
chmod: () => chmod,
|
|
1944
|
+
cp: () => cp,
|
|
1945
|
+
ln: () => ln,
|
|
1946
|
+
mv: () => mv,
|
|
1947
|
+
rm: () => rm,
|
|
1948
|
+
rmdir: () => rmdir,
|
|
1949
|
+
touch: () => touch
|
|
1950
|
+
});
|
|
1951
|
+
function resolve2(p, ctx) {
|
|
1952
|
+
return p.startsWith("/") ? p : ctx.path.join(ctx.cwd, p);
|
|
1953
|
+
}
|
|
1954
|
+
function cp(args, ctx) {
|
|
1955
|
+
let recursive = false;
|
|
1956
|
+
const paths = [];
|
|
1957
|
+
for (const arg of args) {
|
|
1958
|
+
if (arg === "-r" || arg === "-R" || arg === "-rf" || arg === "-fr") {
|
|
1959
|
+
recursive = true;
|
|
1960
|
+
} else if (arg === "-f") {
|
|
1961
|
+
} else if (!arg.startsWith("-")) {
|
|
1962
|
+
paths.push(arg);
|
|
1963
|
+
}
|
|
1964
|
+
}
|
|
1965
|
+
if (paths.length < 2) {
|
|
1966
|
+
ctx.stderr.write("cp: missing file operand\n");
|
|
1967
|
+
return 1;
|
|
1968
|
+
}
|
|
1969
|
+
const src = resolve2(paths[0], ctx);
|
|
1970
|
+
const dst = resolve2(paths[1], ctx);
|
|
1971
|
+
try {
|
|
1972
|
+
const stat = ctx.fs.statSync(src);
|
|
1973
|
+
if (stat.isDirectory()) {
|
|
1974
|
+
if (!recursive) {
|
|
1975
|
+
ctx.stderr.write("cp: -r not specified; omitting directory '" + paths[0] + "'\n");
|
|
1976
|
+
return 1;
|
|
1977
|
+
}
|
|
1978
|
+
copyDir(src, dst, ctx);
|
|
1979
|
+
} else {
|
|
1980
|
+
const content = ctx.fs.readFileSync(src);
|
|
1981
|
+
ctx.fs.writeFileSync(dst, content);
|
|
1982
|
+
}
|
|
1983
|
+
return 0;
|
|
1984
|
+
} catch (err2) {
|
|
1985
|
+
ctx.stderr.write("cp: " + paths[0] + ": " + err2.message + "\n");
|
|
1986
|
+
return 1;
|
|
1987
|
+
}
|
|
1988
|
+
}
|
|
1989
|
+
function copyDir(src, dst, ctx) {
|
|
1990
|
+
try {
|
|
1991
|
+
ctx.fs.mkdirSync(dst);
|
|
1992
|
+
} catch {
|
|
1993
|
+
}
|
|
1994
|
+
const entries = ctx.fs.readdirSync(src);
|
|
1995
|
+
for (const entry of entries) {
|
|
1996
|
+
const srcPath = ctx.path.join(src, entry);
|
|
1997
|
+
const dstPath = ctx.path.join(dst, entry);
|
|
1998
|
+
const stat = ctx.fs.statSync(srcPath);
|
|
1999
|
+
if (stat.isDirectory()) {
|
|
2000
|
+
copyDir(srcPath, dstPath, ctx);
|
|
2001
|
+
} else {
|
|
2002
|
+
ctx.fs.writeFileSync(dstPath, ctx.fs.readFileSync(srcPath));
|
|
2003
|
+
}
|
|
2004
|
+
}
|
|
2005
|
+
}
|
|
2006
|
+
function mv(args, ctx) {
|
|
2007
|
+
const paths = [];
|
|
2008
|
+
for (const arg of args) {
|
|
2009
|
+
if (!arg.startsWith("-")) paths.push(arg);
|
|
2010
|
+
}
|
|
2011
|
+
if (paths.length < 2) {
|
|
2012
|
+
ctx.stderr.write("mv: missing file operand\n");
|
|
2013
|
+
return 1;
|
|
2014
|
+
}
|
|
2015
|
+
const src = resolve2(paths[0], ctx);
|
|
2016
|
+
const dst = resolve2(paths[1], ctx);
|
|
2017
|
+
try {
|
|
2018
|
+
ctx.fs.renameSync(src, dst);
|
|
2019
|
+
return 0;
|
|
2020
|
+
} catch (err2) {
|
|
2021
|
+
ctx.stderr.write("mv: " + paths[0] + ": " + err2.message + "\n");
|
|
2022
|
+
return 1;
|
|
2023
|
+
}
|
|
2024
|
+
}
|
|
2025
|
+
function rm(args, ctx) {
|
|
2026
|
+
let recursive = false;
|
|
2027
|
+
let force = false;
|
|
2028
|
+
const targets = [];
|
|
2029
|
+
for (const arg of args) {
|
|
2030
|
+
if (arg === "-r" || arg === "-R") recursive = true;
|
|
2031
|
+
else if (arg === "-f") force = true;
|
|
2032
|
+
else if (arg === "-rf" || arg === "-fr") {
|
|
2033
|
+
recursive = true;
|
|
2034
|
+
force = true;
|
|
2035
|
+
} else if (!arg.startsWith("-")) targets.push(arg);
|
|
2036
|
+
}
|
|
2037
|
+
if (targets.length === 0) {
|
|
2038
|
+
if (!force) {
|
|
2039
|
+
ctx.stderr.write("rm: missing operand\n");
|
|
2040
|
+
return 1;
|
|
2041
|
+
}
|
|
2042
|
+
return 0;
|
|
2043
|
+
}
|
|
2044
|
+
for (const target of targets) {
|
|
2045
|
+
const p = resolve2(target, ctx);
|
|
2046
|
+
try {
|
|
2047
|
+
const stat = ctx.fs.statSync(p);
|
|
2048
|
+
if (stat.isDirectory()) {
|
|
2049
|
+
if (!recursive) {
|
|
2050
|
+
ctx.stderr.write("rm: cannot remove '" + target + "': Is a directory\n");
|
|
2051
|
+
return 1;
|
|
2052
|
+
}
|
|
2053
|
+
rmDir(p, ctx);
|
|
2054
|
+
} else {
|
|
2055
|
+
ctx.fs.unlinkSync(p);
|
|
2056
|
+
}
|
|
2057
|
+
} catch (err2) {
|
|
2058
|
+
if (!force) {
|
|
2059
|
+
ctx.stderr.write("rm: " + target + ": " + err2.message + "\n");
|
|
2060
|
+
return 1;
|
|
2061
|
+
}
|
|
2062
|
+
}
|
|
2063
|
+
}
|
|
2064
|
+
return 0;
|
|
2065
|
+
}
|
|
2066
|
+
function rmDir(p, ctx) {
|
|
2067
|
+
const entries = ctx.fs.readdirSync(p);
|
|
2068
|
+
for (const entry of entries) {
|
|
2069
|
+
const full = ctx.path.join(p, entry);
|
|
2070
|
+
const stat = ctx.fs.statSync(full);
|
|
2071
|
+
if (stat.isDirectory()) {
|
|
2072
|
+
rmDir(full, ctx);
|
|
2073
|
+
} else {
|
|
2074
|
+
ctx.fs.unlinkSync(full);
|
|
2075
|
+
}
|
|
2076
|
+
}
|
|
2077
|
+
ctx.fs.rmdirSync(p);
|
|
2078
|
+
}
|
|
2079
|
+
function rmdir(args, ctx) {
|
|
2080
|
+
if (args.length === 0) {
|
|
2081
|
+
ctx.stderr.write("rmdir: missing operand\n");
|
|
2082
|
+
return 1;
|
|
2083
|
+
}
|
|
2084
|
+
for (const arg of args) {
|
|
2085
|
+
if (arg.startsWith("-")) continue;
|
|
2086
|
+
const p = resolve2(arg, ctx);
|
|
2087
|
+
try {
|
|
2088
|
+
const entries = ctx.fs.readdirSync(p);
|
|
2089
|
+
if (entries.length > 0) {
|
|
2090
|
+
ctx.stderr.write("rmdir: " + arg + ": Directory not empty\n");
|
|
2091
|
+
return 1;
|
|
2092
|
+
}
|
|
2093
|
+
ctx.fs.rmdirSync(p);
|
|
2094
|
+
} catch (err2) {
|
|
2095
|
+
ctx.stderr.write("rmdir: " + arg + ": " + err2.message + "\n");
|
|
2096
|
+
return 1;
|
|
2097
|
+
}
|
|
2098
|
+
}
|
|
2099
|
+
return 0;
|
|
2100
|
+
}
|
|
2101
|
+
function touch(args, ctx) {
|
|
2102
|
+
if (args.length === 0) {
|
|
2103
|
+
ctx.stderr.write("touch: missing file operand\n");
|
|
2104
|
+
return 1;
|
|
2105
|
+
}
|
|
2106
|
+
for (const arg of args) {
|
|
2107
|
+
if (arg.startsWith("-")) continue;
|
|
2108
|
+
const p = resolve2(arg, ctx);
|
|
2109
|
+
try {
|
|
2110
|
+
if (ctx.fs.existsSync(p)) {
|
|
2111
|
+
ctx.fs.utimesSync(p, /* @__PURE__ */ new Date(), /* @__PURE__ */ new Date());
|
|
2112
|
+
} else {
|
|
2113
|
+
ctx.fs.writeFileSync(p, "");
|
|
2114
|
+
}
|
|
2115
|
+
} catch (err2) {
|
|
2116
|
+
ctx.stderr.write("touch: " + arg + ": " + err2.message + "\n");
|
|
2117
|
+
return 1;
|
|
2118
|
+
}
|
|
2119
|
+
}
|
|
2120
|
+
return 0;
|
|
2121
|
+
}
|
|
2122
|
+
function ln(args, ctx) {
|
|
2123
|
+
let symbolic = false;
|
|
2124
|
+
const paths = [];
|
|
2125
|
+
for (const arg of args) {
|
|
2126
|
+
if (arg === "-s") symbolic = true;
|
|
2127
|
+
else if (!arg.startsWith("-")) paths.push(arg);
|
|
2128
|
+
}
|
|
2129
|
+
if (paths.length < 2) {
|
|
2130
|
+
ctx.stderr.write("ln: missing file operand\n");
|
|
2131
|
+
return 1;
|
|
2132
|
+
}
|
|
2133
|
+
const target = resolve2(paths[0], ctx);
|
|
2134
|
+
const linkName = resolve2(paths[1], ctx);
|
|
2135
|
+
try {
|
|
2136
|
+
if (symbolic) {
|
|
2137
|
+
ctx.fs.symlinkSync(target, linkName);
|
|
2138
|
+
} else {
|
|
2139
|
+
ctx.fs.linkSync(target, linkName);
|
|
2140
|
+
}
|
|
2141
|
+
return 0;
|
|
2142
|
+
} catch (err2) {
|
|
2143
|
+
ctx.stderr.write("ln: " + paths[1] + ": " + err2.message + "\n");
|
|
2144
|
+
return 1;
|
|
2145
|
+
}
|
|
2146
|
+
}
|
|
2147
|
+
function chmod(args, ctx) {
|
|
2148
|
+
let recursive = false;
|
|
2149
|
+
const rest = [];
|
|
2150
|
+
for (const arg of args) {
|
|
2151
|
+
if (arg === "-R" || arg === "-r") recursive = true;
|
|
2152
|
+
else if (!arg.startsWith("-")) rest.push(arg);
|
|
2153
|
+
}
|
|
2154
|
+
if (rest.length < 2) {
|
|
2155
|
+
ctx.stderr.write("chmod: missing operand\n");
|
|
2156
|
+
return 1;
|
|
2157
|
+
}
|
|
2158
|
+
const modeStr = rest[0];
|
|
2159
|
+
const targets = rest.slice(1);
|
|
2160
|
+
let mode;
|
|
2161
|
+
if (/^\d+$/.test(modeStr)) {
|
|
2162
|
+
mode = parseInt(modeStr, 8);
|
|
2163
|
+
} else {
|
|
2164
|
+
mode = parseSymbolicMode(modeStr);
|
|
2165
|
+
if (mode < 0) {
|
|
2166
|
+
ctx.stderr.write("chmod: invalid mode: " + modeStr + "\n");
|
|
2167
|
+
return 1;
|
|
2168
|
+
}
|
|
2169
|
+
}
|
|
2170
|
+
for (const target of targets) {
|
|
2171
|
+
const p = resolve2(target, ctx);
|
|
2172
|
+
try {
|
|
2173
|
+
if (ctx.fs.chmodSync) {
|
|
2174
|
+
ctx.fs.chmodSync(p, mode);
|
|
2175
|
+
}
|
|
2176
|
+
if (recursive) {
|
|
2177
|
+
try {
|
|
2178
|
+
const stat = ctx.fs.statSync(p);
|
|
2179
|
+
if (stat.isDirectory()) chmodRecursive(p, mode, ctx);
|
|
2180
|
+
} catch {
|
|
2181
|
+
}
|
|
2182
|
+
}
|
|
2183
|
+
} catch (err2) {
|
|
2184
|
+
ctx.stderr.write("chmod: " + target + ": " + err2.message + "\n");
|
|
2185
|
+
return 1;
|
|
2186
|
+
}
|
|
2187
|
+
}
|
|
2188
|
+
return 0;
|
|
2189
|
+
}
|
|
2190
|
+
function parseSymbolicMode(s) {
|
|
2191
|
+
const match = s.match(/^([+-])([rwx]+)$/);
|
|
2192
|
+
if (!match) return -1;
|
|
2193
|
+
const [, op, perms] = match;
|
|
2194
|
+
let bits2 = 0;
|
|
2195
|
+
if (perms.includes("r")) bits2 |= 292;
|
|
2196
|
+
if (perms.includes("w")) bits2 |= 146;
|
|
2197
|
+
if (perms.includes("x")) bits2 |= 73;
|
|
2198
|
+
return op === "+" ? bits2 : bits2;
|
|
2199
|
+
}
|
|
2200
|
+
function chmodRecursive(dir, mode, ctx) {
|
|
2201
|
+
const entries = ctx.fs.readdirSync(dir);
|
|
2202
|
+
for (const entry of entries) {
|
|
2203
|
+
const full = ctx.path.join(dir, entry);
|
|
2204
|
+
if (ctx.fs.chmodSync) ctx.fs.chmodSync(full, mode);
|
|
2205
|
+
try {
|
|
2206
|
+
const stat = ctx.fs.statSync(full);
|
|
2207
|
+
if (stat.isDirectory()) chmodRecursive(full, mode, ctx);
|
|
2208
|
+
} catch {
|
|
2209
|
+
}
|
|
2210
|
+
}
|
|
2211
|
+
}
|
|
2212
|
+
var init_file_commands = __esm({
|
|
2213
|
+
"projects/@machina.at/shell/src/lib/file-commands.ts"() {
|
|
2214
|
+
"use strict";
|
|
2215
|
+
}
|
|
2216
|
+
});
|
|
2217
|
+
|
|
2218
|
+
// projects/@machina.at/shell/src/commands/chmod.js
|
|
2219
|
+
var require_chmod = __commonJS({
|
|
2220
|
+
"projects/@machina.at/shell/src/commands/chmod.js"(exports2, module2) {
|
|
2221
|
+
"use strict";
|
|
2222
|
+
var lib = (init_file_commands(), __toCommonJS(file_commands_exports));
|
|
2223
|
+
module2.exports = function chmod2(args, ctx) {
|
|
2224
|
+
return lib.chmod(args, { ...ctx, stdout: process.stdout, stderr: process.stderr });
|
|
2225
|
+
};
|
|
2226
|
+
}
|
|
2227
|
+
});
|
|
2228
|
+
|
|
2229
|
+
// projects/@machina.at/shell/src/lib/text-commands.ts
|
|
2230
|
+
var text_commands_exports = {};
|
|
2231
|
+
__export(text_commands_exports, {
|
|
2232
|
+
cat: () => cat,
|
|
2233
|
+
cut: () => cut,
|
|
2234
|
+
grep: () => grep,
|
|
2235
|
+
head: () => head,
|
|
2236
|
+
nl: () => nl,
|
|
2237
|
+
paste: () => paste,
|
|
2238
|
+
rev: () => rev,
|
|
2239
|
+
sed: () => sed,
|
|
2240
|
+
sort: () => sort,
|
|
2241
|
+
tail: () => tail,
|
|
2242
|
+
tee: () => tee,
|
|
2243
|
+
tr: () => tr,
|
|
2244
|
+
uniq: () => uniq,
|
|
2245
|
+
wc: () => wc
|
|
2246
|
+
});
|
|
2247
|
+
function resolve3(p, ctx) {
|
|
2248
|
+
return p.startsWith("/") ? p : ctx.path.join(ctx.cwd, p);
|
|
2249
|
+
}
|
|
2250
|
+
function parseRangeList(spec) {
|
|
2251
|
+
const indices = [];
|
|
2252
|
+
for (const part of spec.split(",")) {
|
|
2253
|
+
if (part.includes("-")) {
|
|
2254
|
+
const [s, e] = part.split("-").map((n) => parseInt(n, 10) - 1);
|
|
2255
|
+
for (let i = s; i <= (isNaN(e) ? s : e); i++) indices.push(i);
|
|
2256
|
+
} else {
|
|
2257
|
+
indices.push(parseInt(part, 10) - 1);
|
|
2258
|
+
}
|
|
2259
|
+
}
|
|
2260
|
+
return indices;
|
|
2261
|
+
}
|
|
2262
|
+
function expandTrClass(s) {
|
|
2263
|
+
return s.replace(/\[:upper:]/g, "ABCDEFGHIJKLMNOPQRSTUVWXYZ").replace(/\[:lower:]/g, "abcdefghijklmnopqrstuvwxyz").replace(/\[:digit:]/g, "0123456789").replace(/\[:alpha:]/g, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").replace(/\[:space:]/g, " \n\r\v\f");
|
|
2264
|
+
}
|
|
2265
|
+
function readInput(files, ctx) {
|
|
2266
|
+
if (files.length === 0) return ctx.stdin ?? null;
|
|
2267
|
+
let result = "";
|
|
2268
|
+
for (const f of files) {
|
|
2269
|
+
const p = resolve3(f, ctx);
|
|
2270
|
+
try {
|
|
2271
|
+
const c = ctx.fs.readFileSync(p);
|
|
2272
|
+
result += typeof c === "string" ? c : new TextDecoder().decode(c);
|
|
2273
|
+
} catch (err2) {
|
|
2274
|
+
ctx.stderr.write(f + ": " + err2.message + "\n");
|
|
2275
|
+
return null;
|
|
2276
|
+
}
|
|
2277
|
+
}
|
|
2278
|
+
return result;
|
|
2279
|
+
}
|
|
2280
|
+
function grep(args, ctx) {
|
|
2281
|
+
let invert = false, ignoreCase = false, countOnly = false, lineNum = false;
|
|
2282
|
+
let extended = false, fileNameOnly = false, wordMatch = false;
|
|
2283
|
+
let pattern = "";
|
|
2284
|
+
const files = [];
|
|
2285
|
+
let patternSet = false;
|
|
2286
|
+
for (const arg of args) {
|
|
2287
|
+
if (arg === "-v") invert = true;
|
|
2288
|
+
else if (arg === "-i") ignoreCase = true;
|
|
2289
|
+
else if (arg === "-c") countOnly = true;
|
|
2290
|
+
else if (arg === "-n") lineNum = true;
|
|
2291
|
+
else if (arg === "-E") extended = true;
|
|
2292
|
+
else if (arg === "-l") fileNameOnly = true;
|
|
2293
|
+
else if (arg === "-w") wordMatch = true;
|
|
2294
|
+
else if (arg.startsWith("-")) {
|
|
2295
|
+
} else if (!patternSet) {
|
|
2296
|
+
pattern = arg;
|
|
2297
|
+
patternSet = true;
|
|
2298
|
+
} else files.push(arg);
|
|
2299
|
+
}
|
|
2300
|
+
if (!patternSet) {
|
|
2301
|
+
ctx.stderr.write("grep: missing pattern\n");
|
|
2302
|
+
return 2;
|
|
2303
|
+
}
|
|
2304
|
+
const input = readInput(files, ctx);
|
|
2305
|
+
if (input === null) return 1;
|
|
2306
|
+
let flags = ignoreCase ? "i" : "";
|
|
2307
|
+
if (extended) flags += "";
|
|
2308
|
+
const pat = wordMatch ? "\\b" + pattern + "\\b" : pattern;
|
|
2309
|
+
const re = new RegExp(pat, flags);
|
|
2310
|
+
const lines = input.split("\n");
|
|
2311
|
+
if (lines[lines.length - 1] === "") lines.pop();
|
|
2312
|
+
let count = 0;
|
|
2313
|
+
const matched = [];
|
|
2314
|
+
for (let i = 0; i < lines.length; i++) {
|
|
2315
|
+
const match = re.test(lines[i]);
|
|
2316
|
+
if (match !== invert) {
|
|
2317
|
+
count++;
|
|
2318
|
+
if (lineNum) matched.push(i + 1 + ":" + lines[i]);
|
|
2319
|
+
else matched.push(lines[i]);
|
|
2320
|
+
}
|
|
2321
|
+
}
|
|
2322
|
+
if (fileNameOnly) {
|
|
2323
|
+
if (count > 0 && files.length > 0) ctx.stdout.write(files.join("\n") + "\n");
|
|
2324
|
+
return count > 0 ? 0 : 1;
|
|
2325
|
+
}
|
|
2326
|
+
if (countOnly) {
|
|
2327
|
+
ctx.stdout.write(count + "\n");
|
|
2328
|
+
} else {
|
|
2329
|
+
for (const m of matched) ctx.stdout.write(m + "\n");
|
|
2330
|
+
}
|
|
2331
|
+
return count > 0 ? 0 : 1;
|
|
2332
|
+
}
|
|
2333
|
+
function sed(args, ctx) {
|
|
2334
|
+
const files = [];
|
|
2335
|
+
let expression = "";
|
|
2336
|
+
let silent = false;
|
|
2337
|
+
for (let i = 0; i < args.length; i++) {
|
|
2338
|
+
if (args[i] === "-n") silent = true;
|
|
2339
|
+
else if (args[i] === "-e" && args[i + 1]) expression = args[++i];
|
|
2340
|
+
else if (!args[i].startsWith("-") && !expression) expression = args[i];
|
|
2341
|
+
else if (!args[i].startsWith("-")) files.push(args[i]);
|
|
2342
|
+
}
|
|
2343
|
+
if (!expression) {
|
|
2344
|
+
ctx.stderr.write("sed: missing expression\n");
|
|
2345
|
+
return 1;
|
|
2346
|
+
}
|
|
2347
|
+
const input = readInput(files, ctx);
|
|
2348
|
+
if (input === null) return 1;
|
|
2349
|
+
const lines = input.split("\n");
|
|
2350
|
+
if (lines[lines.length - 1] === "") lines.pop();
|
|
2351
|
+
const addrMatch = expression.match(/^(\d+)?(?:,(\d+))?(.*)/);
|
|
2352
|
+
if (!addrMatch) {
|
|
2353
|
+
ctx.stderr.write("sed: invalid expression\n");
|
|
2354
|
+
return 1;
|
|
2355
|
+
}
|
|
2356
|
+
const addrStart = addrMatch[1] ? parseInt(addrMatch[1], 10) : 0;
|
|
2357
|
+
const addrEnd = addrMatch[2] ? parseInt(addrMatch[2], 10) : 0;
|
|
2358
|
+
const cmd = addrMatch[3];
|
|
2359
|
+
const patCmd = expression.match(/^\/(.+?)\/([dp])$/);
|
|
2360
|
+
if (patCmd) {
|
|
2361
|
+
const re2 = new RegExp(patCmd[1]);
|
|
2362
|
+
const action = patCmd[2];
|
|
2363
|
+
for (let i = 0; i < lines.length; i++) {
|
|
2364
|
+
const matches = re2.test(lines[i]);
|
|
2365
|
+
if (action === "d") {
|
|
2366
|
+
if (!matches && !silent) ctx.stdout.write(lines[i] + "\n");
|
|
2367
|
+
} else if (action === "p") {
|
|
2368
|
+
if (!silent && !matches) ctx.stdout.write(lines[i] + "\n");
|
|
2369
|
+
if (matches) ctx.stdout.write(lines[i] + "\n");
|
|
2370
|
+
}
|
|
2371
|
+
}
|
|
2372
|
+
return 0;
|
|
2373
|
+
}
|
|
2374
|
+
if (cmd === "d") {
|
|
2375
|
+
for (let i = 0; i < lines.length; i++) {
|
|
2376
|
+
const lineNum = i + 1;
|
|
2377
|
+
const inRange = addrEnd ? lineNum >= addrStart && lineNum <= addrEnd : lineNum === addrStart;
|
|
2378
|
+
if (!inRange) ctx.stdout.write(lines[i] + "\n");
|
|
2379
|
+
}
|
|
2380
|
+
return 0;
|
|
2381
|
+
}
|
|
2382
|
+
if (cmd === "p") {
|
|
2383
|
+
for (let i = 0; i < lines.length; i++) {
|
|
2384
|
+
const lineNum = i + 1;
|
|
2385
|
+
const inRange = addrEnd ? lineNum >= addrStart && lineNum <= addrEnd : lineNum === addrStart;
|
|
2386
|
+
if (!silent) ctx.stdout.write(lines[i] + "\n");
|
|
2387
|
+
if (inRange) ctx.stdout.write(lines[i] + "\n");
|
|
2388
|
+
}
|
|
2389
|
+
return 0;
|
|
2390
|
+
}
|
|
2391
|
+
const sMatch = cmd.match(/^s(.)(.+?)\1(.*?)\1([g]?)$/);
|
|
2392
|
+
if (!sMatch) {
|
|
2393
|
+
ctx.stderr.write("sed: invalid expression: " + expression + "\n");
|
|
2394
|
+
return 1;
|
|
2395
|
+
}
|
|
2396
|
+
const [, , pat, repl, flag] = sMatch;
|
|
2397
|
+
const re = new RegExp(pat, flag === "g" ? "g" : "");
|
|
2398
|
+
for (let i = 0; i < lines.length; i++) {
|
|
2399
|
+
const lineNum = i + 1;
|
|
2400
|
+
const inRange = addrStart ? addrEnd ? lineNum >= addrStart && lineNum <= addrEnd : lineNum === addrStart : true;
|
|
2401
|
+
const replaced = inRange ? lines[i].replace(re, repl) : lines[i];
|
|
2402
|
+
if (!silent) ctx.stdout.write(replaced + "\n");
|
|
2403
|
+
}
|
|
2404
|
+
return 0;
|
|
2405
|
+
}
|
|
2406
|
+
function wc(args, ctx) {
|
|
2407
|
+
let linesOnly = false, wordsOnly = false, charsOnly = false;
|
|
2408
|
+
const files = [];
|
|
2409
|
+
for (const arg of args) {
|
|
2410
|
+
if (arg === "-l") linesOnly = true;
|
|
2411
|
+
else if (arg === "-w") wordsOnly = true;
|
|
2412
|
+
else if (arg === "-c" || arg === "-m") charsOnly = true;
|
|
2413
|
+
else if (!arg.startsWith("-")) files.push(arg);
|
|
2414
|
+
}
|
|
2415
|
+
const input = readInput(files, ctx);
|
|
2416
|
+
if (input === null) return 1;
|
|
2417
|
+
const lineCount = input.split("\n").length - (input.endsWith("\n") ? 1 : 0);
|
|
2418
|
+
const wordCount = input.trim() ? input.trim().split(/\s+/).length : 0;
|
|
2419
|
+
const charCount = input.length;
|
|
2420
|
+
if (linesOnly) ctx.stdout.write(lineCount + "\n");
|
|
2421
|
+
else if (wordsOnly) ctx.stdout.write(wordCount + "\n");
|
|
2422
|
+
else if (charsOnly) ctx.stdout.write(charCount + "\n");
|
|
2423
|
+
else ctx.stdout.write(lineCount + " " + wordCount + " " + charCount + "\n");
|
|
2424
|
+
return 0;
|
|
2425
|
+
}
|
|
2426
|
+
function sort(args, ctx) {
|
|
2427
|
+
let reverse = false, numeric = false, unique = false;
|
|
2428
|
+
let keyField = 0, separator = "";
|
|
2429
|
+
const files = [];
|
|
2430
|
+
for (let i = 0; i < args.length; i++) {
|
|
2431
|
+
if (args[i] === "-r") reverse = true;
|
|
2432
|
+
else if (args[i] === "-n") numeric = true;
|
|
2433
|
+
else if (args[i] === "-u") unique = true;
|
|
2434
|
+
else if (args[i] === "-k" && args[i + 1]) keyField = parseInt(args[++i], 10);
|
|
2435
|
+
else if (args[i] === "-t" && args[i + 1]) separator = args[++i];
|
|
2436
|
+
else if (!args[i].startsWith("-")) files.push(args[i]);
|
|
2437
|
+
}
|
|
2438
|
+
const input = readInput(files, ctx);
|
|
2439
|
+
if (input === null) return 1;
|
|
2440
|
+
let lines = input.split("\n");
|
|
2441
|
+
if (lines[lines.length - 1] === "") lines.pop();
|
|
2442
|
+
const getKey = (line) => {
|
|
2443
|
+
if (keyField > 0) {
|
|
2444
|
+
const sep = separator || /\s+/;
|
|
2445
|
+
const parts = typeof sep === "string" ? line.split(sep) : line.split(sep);
|
|
2446
|
+
return parts[keyField - 1] ?? "";
|
|
2447
|
+
}
|
|
2448
|
+
return line;
|
|
2449
|
+
};
|
|
2450
|
+
if (numeric) {
|
|
2451
|
+
lines.sort((a, b) => parseFloat(getKey(a)) - parseFloat(getKey(b)));
|
|
2452
|
+
} else {
|
|
2453
|
+
lines.sort((a, b) => getKey(a).localeCompare(getKey(b)));
|
|
2454
|
+
}
|
|
2455
|
+
if (reverse) lines.reverse();
|
|
2456
|
+
if (unique) lines = [...new Set(lines)];
|
|
2457
|
+
for (const l of lines) ctx.stdout.write(l + "\n");
|
|
2458
|
+
return 0;
|
|
2459
|
+
}
|
|
2460
|
+
function uniq(args, ctx) {
|
|
2461
|
+
let countPrefix = false, dupsOnly = false;
|
|
2462
|
+
const files = [];
|
|
2463
|
+
for (const arg of args) {
|
|
2464
|
+
if (arg === "-c") countPrefix = true;
|
|
2465
|
+
else if (arg === "-d") dupsOnly = true;
|
|
2466
|
+
else if (!arg.startsWith("-")) files.push(arg);
|
|
2467
|
+
}
|
|
2468
|
+
const input = readInput(files, ctx);
|
|
2469
|
+
if (input === null) return 1;
|
|
2470
|
+
const lines = input.split("\n");
|
|
2471
|
+
if (lines[lines.length - 1] === "") lines.pop();
|
|
2472
|
+
const groups = [];
|
|
2473
|
+
for (const line of lines) {
|
|
2474
|
+
if (groups.length > 0 && groups[groups.length - 1].line === line) {
|
|
2475
|
+
groups[groups.length - 1].count++;
|
|
2476
|
+
} else {
|
|
2477
|
+
groups.push({ line, count: 1 });
|
|
2478
|
+
}
|
|
2479
|
+
}
|
|
2480
|
+
for (const g of groups) {
|
|
2481
|
+
if (dupsOnly && g.count < 2) continue;
|
|
2482
|
+
if (countPrefix) ctx.stdout.write(g.count + " " + g.line + "\n");
|
|
2483
|
+
else ctx.stdout.write(g.line + "\n");
|
|
2484
|
+
}
|
|
2485
|
+
return 0;
|
|
2486
|
+
}
|
|
2487
|
+
function cut(args, ctx) {
|
|
2488
|
+
let delimiter = " ";
|
|
2489
|
+
let fields = "";
|
|
2490
|
+
let charMode = "";
|
|
2491
|
+
const files = [];
|
|
2492
|
+
for (let i = 0; i < args.length; i++) {
|
|
2493
|
+
if (args[i] === "-d" && args[i + 1]) delimiter = args[++i];
|
|
2494
|
+
else if (args[i] === "-f" && args[i + 1]) fields = args[++i];
|
|
2495
|
+
else if (args[i] === "-c" && args[i + 1]) charMode = args[++i];
|
|
2496
|
+
else if (args[i].startsWith("-d")) delimiter = args[i].slice(2);
|
|
2497
|
+
else if (args[i].startsWith("-f")) fields = args[i].slice(2);
|
|
2498
|
+
else if (args[i].startsWith("-c")) charMode = args[i].slice(2);
|
|
2499
|
+
else if (!args[i].startsWith("-")) files.push(args[i]);
|
|
2500
|
+
}
|
|
2501
|
+
if (!fields && !charMode) {
|
|
2502
|
+
ctx.stderr.write("cut: missing field or character list\n");
|
|
2503
|
+
return 1;
|
|
2504
|
+
}
|
|
2505
|
+
const input = readInput(files, ctx);
|
|
2506
|
+
if (input === null) return 1;
|
|
2507
|
+
const lines = input.split("\n");
|
|
2508
|
+
if (lines[lines.length - 1] === "") lines.pop();
|
|
2509
|
+
if (charMode) {
|
|
2510
|
+
const indices = parseRangeList(charMode);
|
|
2511
|
+
for (const line of lines) {
|
|
2512
|
+
ctx.stdout.write(indices.map((i) => line[i] ?? "").join("") + "\n");
|
|
2513
|
+
}
|
|
2514
|
+
return 0;
|
|
2515
|
+
}
|
|
2516
|
+
const fieldNums = parseRangeList(fields);
|
|
2517
|
+
for (const line of lines) {
|
|
2518
|
+
const parts = line.split(delimiter);
|
|
2519
|
+
const selected = fieldNums.map((f) => parts[f] ?? "").join(delimiter);
|
|
2520
|
+
ctx.stdout.write(selected + "\n");
|
|
2521
|
+
}
|
|
2522
|
+
return 0;
|
|
2523
|
+
}
|
|
2524
|
+
function tr(args, ctx) {
|
|
2525
|
+
let deleteMode = false;
|
|
2526
|
+
const sets = [];
|
|
2527
|
+
for (const arg of args) {
|
|
2528
|
+
if (arg === "-d") deleteMode = true;
|
|
2529
|
+
else sets.push(arg);
|
|
2530
|
+
}
|
|
2531
|
+
if (sets.length < 1) {
|
|
2532
|
+
ctx.stderr.write("tr: missing operand\n");
|
|
2533
|
+
return 1;
|
|
2534
|
+
}
|
|
2535
|
+
const input = ctx.stdin ?? "";
|
|
2536
|
+
const set1 = expandTrClass(sets[0]);
|
|
2537
|
+
if (deleteMode) {
|
|
2538
|
+
let result2 = input;
|
|
2539
|
+
for (const ch of set1) result2 = result2.split(ch).join("");
|
|
2540
|
+
ctx.stdout.write(result2);
|
|
2541
|
+
return 0;
|
|
2542
|
+
}
|
|
2543
|
+
if (sets.length < 2) {
|
|
2544
|
+
ctx.stderr.write("tr: missing operand after '" + sets[0] + "'\n");
|
|
2545
|
+
return 1;
|
|
2546
|
+
}
|
|
2547
|
+
const set2 = expandTrClass(sets[1]);
|
|
2548
|
+
let result = "";
|
|
2549
|
+
for (const ch of input) {
|
|
2550
|
+
const idx = set1.indexOf(ch);
|
|
2551
|
+
result += idx >= 0 ? set2[Math.min(idx, set2.length - 1)] ?? ch : ch;
|
|
2552
|
+
}
|
|
2553
|
+
ctx.stdout.write(result);
|
|
2554
|
+
return 0;
|
|
2555
|
+
}
|
|
2556
|
+
function head(args, ctx) {
|
|
2557
|
+
let count = 10;
|
|
2558
|
+
const files = [];
|
|
2559
|
+
for (let i = 0; i < args.length; i++) {
|
|
2560
|
+
if (args[i] === "-n" && args[i + 1]) {
|
|
2561
|
+
count = parseInt(args[++i], 10);
|
|
2562
|
+
} else if (/^-\d+$/.test(args[i])) {
|
|
2563
|
+
count = parseInt(args[i].slice(1), 10);
|
|
2564
|
+
} else if (!args[i].startsWith("-")) files.push(args[i]);
|
|
2565
|
+
}
|
|
2566
|
+
const input = readInput(files, ctx);
|
|
2567
|
+
if (input === null) return 1;
|
|
2568
|
+
const lines = input.split("\n");
|
|
2569
|
+
if (lines[lines.length - 1] === "") lines.pop();
|
|
2570
|
+
const selected = lines.slice(0, count);
|
|
2571
|
+
for (const l of selected) ctx.stdout.write(l + "\n");
|
|
2572
|
+
return 0;
|
|
2573
|
+
}
|
|
2574
|
+
function tail(args, ctx) {
|
|
2575
|
+
let count = 10;
|
|
2576
|
+
const files = [];
|
|
2577
|
+
for (let i = 0; i < args.length; i++) {
|
|
2578
|
+
if (args[i] === "-n" && args[i + 1]) {
|
|
2579
|
+
count = parseInt(args[++i], 10);
|
|
2580
|
+
} else if (/^-\d+$/.test(args[i])) {
|
|
2581
|
+
count = parseInt(args[i].slice(1), 10);
|
|
2582
|
+
} else if (!args[i].startsWith("-")) files.push(args[i]);
|
|
2583
|
+
}
|
|
2584
|
+
const input = readInput(files, ctx);
|
|
2585
|
+
if (input === null) return 1;
|
|
2586
|
+
const lines = input.split("\n");
|
|
2587
|
+
if (lines[lines.length - 1] === "") lines.pop();
|
|
2588
|
+
const selected = lines.slice(-count);
|
|
2589
|
+
for (const l of selected) ctx.stdout.write(l + "\n");
|
|
2590
|
+
return 0;
|
|
2591
|
+
}
|
|
2592
|
+
function tee(args, ctx) {
|
|
2593
|
+
let append = false;
|
|
2594
|
+
const files = [];
|
|
2595
|
+
for (const arg of args) {
|
|
2596
|
+
if (arg === "-a") append = true;
|
|
2597
|
+
else if (!arg.startsWith("-")) files.push(arg);
|
|
2598
|
+
}
|
|
2599
|
+
const input = ctx.stdin ?? "";
|
|
2600
|
+
ctx.stdout.write(input);
|
|
2601
|
+
for (const f of files) {
|
|
2602
|
+
const p = resolve3(f, ctx);
|
|
2603
|
+
try {
|
|
2604
|
+
if (append) ctx.fs.appendFileSync(p, input);
|
|
2605
|
+
else ctx.fs.writeFileSync(p, input);
|
|
2606
|
+
} catch (err2) {
|
|
2607
|
+
ctx.stderr.write("tee: " + f + ": " + err2.message + "\n");
|
|
2608
|
+
return 1;
|
|
2609
|
+
}
|
|
2610
|
+
}
|
|
2611
|
+
return 0;
|
|
2612
|
+
}
|
|
2613
|
+
function rev(args, ctx) {
|
|
2614
|
+
const files = args.filter((a) => !a.startsWith("-"));
|
|
2615
|
+
const input = readInput(files, ctx);
|
|
2616
|
+
if (input === null) return 1;
|
|
2617
|
+
const lines = input.split("\n");
|
|
2618
|
+
if (lines[lines.length - 1] === "") lines.pop();
|
|
2619
|
+
for (const l of lines) ctx.stdout.write([...l].reverse().join("") + "\n");
|
|
2620
|
+
return 0;
|
|
2621
|
+
}
|
|
2622
|
+
function paste(args, ctx) {
|
|
2623
|
+
let delimiter = " ";
|
|
2624
|
+
const files = [];
|
|
2625
|
+
for (let i = 0; i < args.length; i++) {
|
|
2626
|
+
if (args[i] === "-d" && args[i + 1]) delimiter = args[++i];
|
|
2627
|
+
else if (!args[i].startsWith("-")) files.push(args[i]);
|
|
2628
|
+
}
|
|
2629
|
+
if (files.length === 0) {
|
|
2630
|
+
const input = ctx.stdin ?? "";
|
|
2631
|
+
const lines = input.split("\n");
|
|
2632
|
+
if (lines[lines.length - 1] === "") lines.pop();
|
|
2633
|
+
ctx.stdout.write(lines.join(delimiter) + "\n");
|
|
2634
|
+
return 0;
|
|
2635
|
+
}
|
|
2636
|
+
const allLines = [];
|
|
2637
|
+
let maxLines = 0;
|
|
2638
|
+
for (const f of files) {
|
|
2639
|
+
const p = resolve3(f, ctx);
|
|
2640
|
+
try {
|
|
2641
|
+
const c = ctx.fs.readFileSync(p);
|
|
2642
|
+
const content = typeof c === "string" ? c : new TextDecoder().decode(c);
|
|
2643
|
+
const lines = content.split("\n");
|
|
2644
|
+
if (lines[lines.length - 1] === "") lines.pop();
|
|
2645
|
+
allLines.push(lines);
|
|
2646
|
+
maxLines = Math.max(maxLines, lines.length);
|
|
2647
|
+
} catch (err2) {
|
|
2648
|
+
ctx.stderr.write("paste: " + f + ": " + err2.message + "\n");
|
|
2649
|
+
return 1;
|
|
2650
|
+
}
|
|
2651
|
+
}
|
|
2652
|
+
for (let i = 0; i < maxLines; i++) {
|
|
2653
|
+
const parts = allLines.map((lines) => lines[i] ?? "");
|
|
2654
|
+
ctx.stdout.write(parts.join(delimiter) + "\n");
|
|
2655
|
+
}
|
|
2656
|
+
return 0;
|
|
2657
|
+
}
|
|
2658
|
+
function nl(args, ctx) {
|
|
2659
|
+
let numberAll = false;
|
|
2660
|
+
const files = [];
|
|
2661
|
+
for (const arg of args) {
|
|
2662
|
+
if (arg === "-ba") numberAll = true;
|
|
2663
|
+
else if (!arg.startsWith("-")) files.push(arg);
|
|
2664
|
+
}
|
|
2665
|
+
const input = readInput(files, ctx);
|
|
2666
|
+
if (input === null) return 1;
|
|
2667
|
+
const lines = input.split("\n");
|
|
2668
|
+
if (lines[lines.length - 1] === "") lines.pop();
|
|
2669
|
+
let num = 1;
|
|
2670
|
+
for (const l of lines) {
|
|
2671
|
+
if (!numberAll && l.trim() === "") {
|
|
2672
|
+
ctx.stdout.write(" " + l + "\n");
|
|
2673
|
+
} else {
|
|
2674
|
+
ctx.stdout.write(String(num).padStart(6, " ") + " " + l + "\n");
|
|
2675
|
+
num++;
|
|
2676
|
+
}
|
|
2677
|
+
}
|
|
2678
|
+
return 0;
|
|
2679
|
+
}
|
|
2680
|
+
function cat(args, ctx) {
|
|
2681
|
+
let showLineNums = false;
|
|
2682
|
+
const files = [];
|
|
2683
|
+
for (const arg of args) {
|
|
2684
|
+
if (arg === "-n") showLineNums = true;
|
|
2685
|
+
else if (!arg.startsWith("-")) files.push(arg);
|
|
2686
|
+
}
|
|
2687
|
+
if (files.length === 0) {
|
|
2688
|
+
const input = ctx.stdin ?? "";
|
|
2689
|
+
if (showLineNums) {
|
|
2690
|
+
const lines = input.split("\n");
|
|
2691
|
+
if (lines[lines.length - 1] === "") lines.pop();
|
|
2692
|
+
for (let i = 0; i < lines.length; i++) {
|
|
2693
|
+
ctx.stdout.write(String(i + 1).padStart(6, " ") + " " + lines[i] + "\n");
|
|
2694
|
+
}
|
|
2695
|
+
} else {
|
|
2696
|
+
ctx.stdout.write(input);
|
|
2697
|
+
}
|
|
2698
|
+
return 0;
|
|
2699
|
+
}
|
|
2700
|
+
let lineNum = 1;
|
|
2701
|
+
for (const f of files) {
|
|
2702
|
+
const p = resolve3(f, ctx);
|
|
2703
|
+
try {
|
|
2704
|
+
const c = ctx.fs.readFileSync(p);
|
|
2705
|
+
const content = typeof c === "string" ? c : new TextDecoder().decode(c);
|
|
2706
|
+
if (showLineNums) {
|
|
2707
|
+
const lines = content.split("\n");
|
|
2708
|
+
if (lines[lines.length - 1] === "") lines.pop();
|
|
2709
|
+
for (const l of lines) {
|
|
2710
|
+
ctx.stdout.write(String(lineNum++).padStart(6, " ") + " " + l + "\n");
|
|
2711
|
+
}
|
|
2712
|
+
} else {
|
|
2713
|
+
ctx.stdout.write(content);
|
|
2714
|
+
}
|
|
2715
|
+
} catch (err2) {
|
|
2716
|
+
ctx.stderr.write("cat: " + f + ": " + err2.message + "\n");
|
|
2717
|
+
return 1;
|
|
2718
|
+
}
|
|
2719
|
+
}
|
|
2720
|
+
return 0;
|
|
2721
|
+
}
|
|
2722
|
+
var init_text_commands = __esm({
|
|
2723
|
+
"projects/@machina.at/shell/src/lib/text-commands.ts"() {
|
|
2724
|
+
"use strict";
|
|
2725
|
+
}
|
|
2726
|
+
});
|
|
2727
|
+
|
|
2728
|
+
// projects/@machina.at/shell/src/commands/nl.js
|
|
2729
|
+
var require_nl = __commonJS({
|
|
2730
|
+
"projects/@machina.at/shell/src/commands/nl.js"(exports2, module2) {
|
|
2731
|
+
"use strict";
|
|
2732
|
+
var lib = (init_text_commands(), __toCommonJS(text_commands_exports));
|
|
2733
|
+
module2.exports = function nl2(args, ctx) {
|
|
2734
|
+
return lib.nl(args, { ...ctx, stdout: process.stdout, stderr: process.stderr });
|
|
2735
|
+
};
|
|
2736
|
+
}
|
|
2737
|
+
});
|
|
2738
|
+
|
|
2739
|
+
// projects/@machina.at/shell/src/commands/paste.js
|
|
2740
|
+
var require_paste = __commonJS({
|
|
2741
|
+
"projects/@machina.at/shell/src/commands/paste.js"(exports2, module2) {
|
|
2742
|
+
"use strict";
|
|
2743
|
+
var lib = (init_text_commands(), __toCommonJS(text_commands_exports));
|
|
2744
|
+
module2.exports = function paste2(args, ctx) {
|
|
2745
|
+
return lib.paste(args, { ...ctx, stdout: process.stdout, stderr: process.stderr });
|
|
2746
|
+
};
|
|
2747
|
+
}
|
|
2748
|
+
});
|
|
2749
|
+
|
|
2750
|
+
// projects/@machina.at/shell/src/commands/rev.js
|
|
2751
|
+
var require_rev = __commonJS({
|
|
2752
|
+
"projects/@machina.at/shell/src/commands/rev.js"(exports2, module2) {
|
|
2753
|
+
"use strict";
|
|
2754
|
+
var lib = (init_text_commands(), __toCommonJS(text_commands_exports));
|
|
2755
|
+
module2.exports = function rev3(args, ctx) {
|
|
2756
|
+
return lib.rev(args, { ...ctx, stdout: process.stdout, stderr: process.stderr });
|
|
2757
|
+
};
|
|
2758
|
+
}
|
|
2759
|
+
});
|
|
2760
|
+
|
|
2761
|
+
// projects/@machina.at/shell/src/lib/net-commands.ts
|
|
2762
|
+
var net_commands_exports = {};
|
|
2763
|
+
__export(net_commands_exports, {
|
|
2764
|
+
curl: () => curl
|
|
2765
|
+
});
|
|
2766
|
+
function base64ToBytes(b64) {
|
|
2767
|
+
if (typeof atob === "function") {
|
|
2768
|
+
const bin = atob(b64);
|
|
2769
|
+
const out = new Uint8Array(bin.length);
|
|
2770
|
+
for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
|
|
2771
|
+
return out;
|
|
2772
|
+
}
|
|
2773
|
+
return new Uint8Array(globalThis.Buffer.from(b64, "base64"));
|
|
2774
|
+
}
|
|
2775
|
+
function parseArgs(args) {
|
|
2776
|
+
const opts = {
|
|
2777
|
+
url: null,
|
|
2778
|
+
method: null,
|
|
2779
|
+
headers: {},
|
|
2780
|
+
body: null,
|
|
2781
|
+
outputFile: null,
|
|
2782
|
+
includeHeaders: false,
|
|
2783
|
+
followRedirects: false,
|
|
2784
|
+
silent: false,
|
|
2785
|
+
failOnError: false
|
|
2786
|
+
};
|
|
2787
|
+
let i = 0;
|
|
2788
|
+
let endOfOptions = false;
|
|
2789
|
+
while (i < args.length) {
|
|
2790
|
+
const a = args[i];
|
|
2791
|
+
if (!endOfOptions && a === "--") {
|
|
2792
|
+
endOfOptions = true;
|
|
2793
|
+
i++;
|
|
2794
|
+
continue;
|
|
2795
|
+
}
|
|
2796
|
+
if (!endOfOptions && a.startsWith("-") && a.length > 1) {
|
|
2797
|
+
switch (a) {
|
|
2798
|
+
case "-X":
|
|
2799
|
+
if (!args[i + 1]) return { opts, error: `option '${a}' requires an argument` };
|
|
2800
|
+
opts.method = args[++i];
|
|
2801
|
+
break;
|
|
2802
|
+
case "-H": {
|
|
2803
|
+
if (!args[i + 1]) return { opts, error: `option '${a}' requires an argument` };
|
|
2804
|
+
const h = args[++i];
|
|
2805
|
+
const idx = h.indexOf(":");
|
|
2806
|
+
if (idx < 0) return { opts, error: `invalid header '${h}'` };
|
|
2807
|
+
const k = h.slice(0, idx).trim();
|
|
2808
|
+
const v = h.slice(idx + 1).trim();
|
|
2809
|
+
opts.headers[k] = v;
|
|
2810
|
+
break;
|
|
2811
|
+
}
|
|
2812
|
+
case "-d":
|
|
2813
|
+
case "--data":
|
|
2814
|
+
if (args[i + 1] === void 0) return { opts, error: `option '${a}' requires an argument` };
|
|
2815
|
+
opts.body = args[++i];
|
|
2816
|
+
break;
|
|
2817
|
+
case "-o":
|
|
2818
|
+
if (!args[i + 1]) return { opts, error: `option '${a}' requires an argument` };
|
|
2819
|
+
opts.outputFile = args[++i];
|
|
2820
|
+
break;
|
|
2821
|
+
case "-i":
|
|
2822
|
+
opts.includeHeaders = true;
|
|
2823
|
+
break;
|
|
2824
|
+
case "-L":
|
|
2825
|
+
opts.followRedirects = true;
|
|
2826
|
+
break;
|
|
2827
|
+
case "-s":
|
|
2828
|
+
case "--silent":
|
|
2829
|
+
opts.silent = true;
|
|
2830
|
+
break;
|
|
2831
|
+
case "-f":
|
|
2832
|
+
case "--fail":
|
|
2833
|
+
opts.failOnError = true;
|
|
2834
|
+
break;
|
|
2835
|
+
default:
|
|
2836
|
+
return { opts, error: `option '${a}' unknown` };
|
|
2837
|
+
}
|
|
2838
|
+
i++;
|
|
2839
|
+
} else {
|
|
2840
|
+
if (opts.url !== null) return { opts, error: `unexpected argument '${a}'` };
|
|
2841
|
+
opts.url = a;
|
|
2842
|
+
i++;
|
|
2843
|
+
}
|
|
2844
|
+
}
|
|
2845
|
+
return { opts };
|
|
2846
|
+
}
|
|
2847
|
+
function resolveSyncRequest(ctx) {
|
|
2848
|
+
if (ctx.syncRequest) return ctx.syncRequest;
|
|
2849
|
+
const g = globalThis.syncFetch;
|
|
2850
|
+
if (typeof g === "function") return g;
|
|
2851
|
+
return childProcessSyncRequest;
|
|
2852
|
+
}
|
|
2853
|
+
function resolveOutputPath(p, ctx) {
|
|
2854
|
+
return p.startsWith("/") ? p : ctx.path.join(ctx.cwd, p);
|
|
2855
|
+
}
|
|
2856
|
+
function formatHeaderBlock(res) {
|
|
2857
|
+
let out = `HTTP/1.1 ${res.status} ${res.statusText}\r
|
|
2858
|
+
`;
|
|
2859
|
+
for (const k of Object.keys(res.headers)) {
|
|
2860
|
+
out += `${k}: ${res.headers[k]}\r
|
|
2861
|
+
`;
|
|
2862
|
+
}
|
|
2863
|
+
out += "\r\n";
|
|
2864
|
+
return out;
|
|
2865
|
+
}
|
|
2866
|
+
function curl(args, ctx) {
|
|
2867
|
+
const stdout = ctx.stdout ?? process.stdout;
|
|
2868
|
+
const stderr = ctx.stderr ?? process.stderr;
|
|
2869
|
+
const parsed = parseArgs(args);
|
|
2870
|
+
if (parsed.error) {
|
|
2871
|
+
if (!parsed.opts.silent) stderr.write(`curl: ${parsed.error}
|
|
2872
|
+
`);
|
|
2873
|
+
return 2;
|
|
2874
|
+
}
|
|
2875
|
+
const opts = parsed.opts;
|
|
2876
|
+
if (!opts.url) {
|
|
2877
|
+
if (!opts.silent) stderr.write("curl: no URL specified\n");
|
|
2878
|
+
return 2;
|
|
2879
|
+
}
|
|
2880
|
+
const sr = resolveSyncRequest(ctx);
|
|
2881
|
+
if (!sr) {
|
|
2882
|
+
if (!opts.silent) stderr.write("curl: syncRequest transport unavailable\n");
|
|
2883
|
+
return 2;
|
|
2884
|
+
}
|
|
2885
|
+
const method = opts.method ?? (opts.body !== null ? "POST" : "GET");
|
|
2886
|
+
let currentUrl = opts.url;
|
|
2887
|
+
let res = { status: 0, statusText: "", headers: {}, body: "" };
|
|
2888
|
+
let redirects = 0;
|
|
2889
|
+
let done = false;
|
|
2890
|
+
try {
|
|
2891
|
+
while (!done) {
|
|
2892
|
+
res = sr(currentUrl, {
|
|
2893
|
+
method,
|
|
2894
|
+
headers: opts.headers,
|
|
2895
|
+
body: opts.body ?? void 0
|
|
2896
|
+
});
|
|
2897
|
+
if (opts.followRedirects && res.status >= 300 && res.status < 400) {
|
|
2898
|
+
const loc = res.headers["location"] ?? res.headers["Location"];
|
|
2899
|
+
if (!loc) {
|
|
2900
|
+
done = true;
|
|
2901
|
+
break;
|
|
2902
|
+
}
|
|
2903
|
+
redirects++;
|
|
2904
|
+
if (redirects > MAX_REDIRECTS) {
|
|
2905
|
+
if (!opts.silent) {
|
|
2906
|
+
stderr.write(`curl: maximum redirect (${MAX_REDIRECTS}) reached
|
|
2907
|
+
`);
|
|
2908
|
+
}
|
|
2909
|
+
return 47;
|
|
2910
|
+
}
|
|
2911
|
+
currentUrl = loc;
|
|
2912
|
+
continue;
|
|
2913
|
+
}
|
|
2914
|
+
done = true;
|
|
2915
|
+
}
|
|
2916
|
+
} catch (err2) {
|
|
2917
|
+
if (!opts.silent) stderr.write(`curl: ${err2.message ?? String(err2)}
|
|
2918
|
+
`);
|
|
2919
|
+
return 7;
|
|
2920
|
+
}
|
|
2921
|
+
if (opts.failOnError && res.status >= 400) {
|
|
2922
|
+
if (!opts.silent) {
|
|
2923
|
+
stderr.write(`curl: HTTP ${res.status} ${res.statusText}
|
|
2924
|
+
`);
|
|
2925
|
+
}
|
|
2926
|
+
return 22;
|
|
2927
|
+
}
|
|
2928
|
+
const headerBlock = opts.includeHeaders ? formatHeaderBlock(res) : "";
|
|
2929
|
+
if (opts.outputFile) {
|
|
2930
|
+
const filePath = resolveOutputPath(opts.outputFile, ctx);
|
|
2931
|
+
try {
|
|
2932
|
+
if (res.isBinary) {
|
|
2933
|
+
ctx.fs.writeFileSync(filePath, base64ToBytes(res.body));
|
|
2934
|
+
} else {
|
|
2935
|
+
ctx.fs.writeFileSync(filePath, headerBlock + res.body);
|
|
2936
|
+
}
|
|
2937
|
+
} catch (err2) {
|
|
2938
|
+
if (!opts.silent) stderr.write(`curl: ${opts.outputFile}: ${err2.message}
|
|
2939
|
+
`);
|
|
2940
|
+
return 23;
|
|
2941
|
+
}
|
|
2942
|
+
} else {
|
|
2943
|
+
if (headerBlock) stdout.write(headerBlock);
|
|
2944
|
+
stdout.write(res.body);
|
|
2945
|
+
}
|
|
2946
|
+
return 0;
|
|
2947
|
+
}
|
|
2948
|
+
var MAX_REDIRECTS, childProcessSyncRequest;
|
|
2949
|
+
var init_net_commands = __esm({
|
|
2950
|
+
"projects/@machina.at/shell/src/lib/net-commands.ts"() {
|
|
2951
|
+
"use strict";
|
|
2952
|
+
MAX_REDIRECTS = 10;
|
|
2953
|
+
childProcessSyncRequest = (url, init) => {
|
|
2954
|
+
let cp2;
|
|
2955
|
+
try {
|
|
2956
|
+
cp2 = typeof require !== "undefined" ? require("child_process") : null;
|
|
2957
|
+
} catch {
|
|
2958
|
+
throw new Error("syncRequest transport unavailable (child_process not loadable)");
|
|
2959
|
+
}
|
|
2960
|
+
if (!cp2) throw new Error("syncRequest transport unavailable (require not available)");
|
|
2961
|
+
const method = init.method ?? "GET";
|
|
2962
|
+
const headers = init.headers ?? {};
|
|
2963
|
+
const body = init.body ?? "";
|
|
2964
|
+
const script = `
|
|
2965
|
+
(async () => {
|
|
2966
|
+
try {
|
|
2967
|
+
const res = await fetch(${JSON.stringify(url)}, {
|
|
2968
|
+
method: ${JSON.stringify(method)},
|
|
2969
|
+
headers: ${JSON.stringify(headers)},
|
|
2970
|
+
${body ? `body: ${JSON.stringify(body)},` : ""}
|
|
2971
|
+
});
|
|
2972
|
+
const h = {};
|
|
2973
|
+
res.headers.forEach((v, k) => { h[k] = v; });
|
|
2974
|
+
// binary content-type \u306F base64 \u5316\u3057\u3066 isBinary:true \u3067\u8FD4\u3059 (curl -o \u304C\u5FA9\u5143\u3059\u308B)
|
|
2975
|
+
var ct = (h['content-type'] || '');
|
|
2976
|
+
var isBinary = /(application\\/octet-stream|application\\/gzip|application\\/x-gzip|application\\/zip|application\\/wasm|application\\/x-tar)/.test(ct);
|
|
2977
|
+
var bodyOut;
|
|
2978
|
+
if (isBinary) {
|
|
2979
|
+
var buf = Buffer.from(await res.arrayBuffer());
|
|
2980
|
+
bodyOut = buf.toString('base64');
|
|
2981
|
+
} else {
|
|
2982
|
+
bodyOut = await res.text();
|
|
2983
|
+
}
|
|
2984
|
+
process.stdout.write(JSON.stringify({ status: res.status, statusText: res.statusText, headers: h, body: bodyOut, isBinary: isBinary }));
|
|
2985
|
+
} catch (e) {
|
|
2986
|
+
process.stderr.write(String(e && e.message ? e.message : e));
|
|
2987
|
+
process.exit(1);
|
|
2988
|
+
}
|
|
2989
|
+
})();
|
|
2990
|
+
`;
|
|
2991
|
+
const execPath = globalThis.process?.execPath ?? "node";
|
|
2992
|
+
const r = cp2.spawnSync(execPath, ["-e", script], { timeout: 6e4, maxBuffer: 64 * 1024 * 1024 });
|
|
2993
|
+
if (r.status !== 0) {
|
|
2994
|
+
const err2 = typeof r.stderr === "string" ? r.stderr : r.stderr?.toString() ?? "";
|
|
2995
|
+
throw new Error(err2 || `child node exited with status ${r.status}`);
|
|
2996
|
+
}
|
|
2997
|
+
const out = typeof r.stdout === "string" ? r.stdout : r.stdout?.toString() ?? "";
|
|
2998
|
+
return JSON.parse(out);
|
|
2999
|
+
};
|
|
3000
|
+
}
|
|
3001
|
+
});
|
|
3002
|
+
|
|
3003
|
+
// projects/@machina.at/shell/src/commands/curl.js
|
|
3004
|
+
var require_curl = __commonJS({
|
|
3005
|
+
"projects/@machina.at/shell/src/commands/curl.js"(exports2, module2) {
|
|
3006
|
+
"use strict";
|
|
3007
|
+
var netCommands = (init_net_commands(), __toCommonJS(net_commands_exports));
|
|
3008
|
+
module2.exports = function curl2(args, ctx) {
|
|
3009
|
+
return netCommands.curl(args, ctx);
|
|
3010
|
+
};
|
|
3011
|
+
}
|
|
3012
|
+
});
|
|
3013
|
+
|
|
3014
|
+
// node_modules/fflate/esm/index.mjs
|
|
3015
|
+
function gunzipSync(data, opts) {
|
|
3016
|
+
var st = gzs(data);
|
|
3017
|
+
if (st + 8 > data.length)
|
|
3018
|
+
err(6, "invalid gzip data");
|
|
3019
|
+
return inflt(data.subarray(st, -8), { i: 2 }, opts && opts.out || new u8(gzl(data)), opts && opts.dictionary);
|
|
3020
|
+
}
|
|
3021
|
+
var import_module, require2, Worker, u8, u16, i32, fleb, fdeb, clim, freb, _a, fl, revfl, _b, fd, revfd, rev2, x, i, hMap, flt, i, i, i, i, fdt, i, flrm, fdrm, max, bits, bits16, shft, slc, ec, err, inflt, et, gzs, gzl, td, tds;
|
|
3022
|
+
var init_esm = __esm({
|
|
3023
|
+
"node_modules/fflate/esm/index.mjs"() {
|
|
3024
|
+
"use strict";
|
|
3025
|
+
import_module = require("module");
|
|
3026
|
+
require2 = (0, import_module.createRequire)("/");
|
|
3027
|
+
try {
|
|
3028
|
+
Worker = require2("worker_threads").Worker;
|
|
3029
|
+
} catch (e) {
|
|
3030
|
+
}
|
|
3031
|
+
u8 = Uint8Array;
|
|
3032
|
+
u16 = Uint16Array;
|
|
3033
|
+
i32 = Int32Array;
|
|
3034
|
+
fleb = new u8([
|
|
3035
|
+
0,
|
|
3036
|
+
0,
|
|
3037
|
+
0,
|
|
3038
|
+
0,
|
|
3039
|
+
0,
|
|
3040
|
+
0,
|
|
3041
|
+
0,
|
|
3042
|
+
0,
|
|
3043
|
+
1,
|
|
3044
|
+
1,
|
|
3045
|
+
1,
|
|
3046
|
+
1,
|
|
3047
|
+
2,
|
|
3048
|
+
2,
|
|
3049
|
+
2,
|
|
3050
|
+
2,
|
|
3051
|
+
3,
|
|
3052
|
+
3,
|
|
3053
|
+
3,
|
|
3054
|
+
3,
|
|
3055
|
+
4,
|
|
3056
|
+
4,
|
|
3057
|
+
4,
|
|
3058
|
+
4,
|
|
3059
|
+
5,
|
|
3060
|
+
5,
|
|
3061
|
+
5,
|
|
3062
|
+
5,
|
|
3063
|
+
0,
|
|
3064
|
+
/* unused */
|
|
3065
|
+
0,
|
|
3066
|
+
0,
|
|
3067
|
+
/* impossible */
|
|
3068
|
+
0
|
|
3069
|
+
]);
|
|
3070
|
+
fdeb = new u8([
|
|
3071
|
+
0,
|
|
3072
|
+
0,
|
|
3073
|
+
0,
|
|
3074
|
+
0,
|
|
3075
|
+
1,
|
|
3076
|
+
1,
|
|
3077
|
+
2,
|
|
3078
|
+
2,
|
|
3079
|
+
3,
|
|
3080
|
+
3,
|
|
3081
|
+
4,
|
|
3082
|
+
4,
|
|
3083
|
+
5,
|
|
3084
|
+
5,
|
|
3085
|
+
6,
|
|
3086
|
+
6,
|
|
3087
|
+
7,
|
|
3088
|
+
7,
|
|
3089
|
+
8,
|
|
3090
|
+
8,
|
|
3091
|
+
9,
|
|
3092
|
+
9,
|
|
3093
|
+
10,
|
|
3094
|
+
10,
|
|
3095
|
+
11,
|
|
3096
|
+
11,
|
|
3097
|
+
12,
|
|
3098
|
+
12,
|
|
3099
|
+
13,
|
|
3100
|
+
13,
|
|
3101
|
+
/* unused */
|
|
3102
|
+
0,
|
|
3103
|
+
0
|
|
3104
|
+
]);
|
|
3105
|
+
clim = new u8([16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15]);
|
|
3106
|
+
freb = function(eb, start) {
|
|
3107
|
+
var b = new u16(31);
|
|
3108
|
+
for (var i = 0; i < 31; ++i) {
|
|
3109
|
+
b[i] = start += 1 << eb[i - 1];
|
|
3110
|
+
}
|
|
3111
|
+
var r = new i32(b[30]);
|
|
3112
|
+
for (var i = 1; i < 30; ++i) {
|
|
3113
|
+
for (var j = b[i]; j < b[i + 1]; ++j) {
|
|
3114
|
+
r[j] = j - b[i] << 5 | i;
|
|
3115
|
+
}
|
|
3116
|
+
}
|
|
3117
|
+
return { b, r };
|
|
3118
|
+
};
|
|
3119
|
+
_a = freb(fleb, 2);
|
|
3120
|
+
fl = _a.b;
|
|
3121
|
+
revfl = _a.r;
|
|
3122
|
+
fl[28] = 258, revfl[258] = 28;
|
|
3123
|
+
_b = freb(fdeb, 0);
|
|
3124
|
+
fd = _b.b;
|
|
3125
|
+
revfd = _b.r;
|
|
3126
|
+
rev2 = new u16(32768);
|
|
3127
|
+
for (i = 0; i < 32768; ++i) {
|
|
3128
|
+
x = (i & 43690) >> 1 | (i & 21845) << 1;
|
|
3129
|
+
x = (x & 52428) >> 2 | (x & 13107) << 2;
|
|
3130
|
+
x = (x & 61680) >> 4 | (x & 3855) << 4;
|
|
3131
|
+
rev2[i] = ((x & 65280) >> 8 | (x & 255) << 8) >> 1;
|
|
3132
|
+
}
|
|
3133
|
+
hMap = (function(cd, mb, r) {
|
|
3134
|
+
var s = cd.length;
|
|
3135
|
+
var i = 0;
|
|
3136
|
+
var l = new u16(mb);
|
|
3137
|
+
for (; i < s; ++i) {
|
|
3138
|
+
if (cd[i])
|
|
3139
|
+
++l[cd[i] - 1];
|
|
3140
|
+
}
|
|
3141
|
+
var le = new u16(mb);
|
|
3142
|
+
for (i = 1; i < mb; ++i) {
|
|
3143
|
+
le[i] = le[i - 1] + l[i - 1] << 1;
|
|
3144
|
+
}
|
|
3145
|
+
var co;
|
|
3146
|
+
if (r) {
|
|
3147
|
+
co = new u16(1 << mb);
|
|
3148
|
+
var rvb = 15 - mb;
|
|
3149
|
+
for (i = 0; i < s; ++i) {
|
|
3150
|
+
if (cd[i]) {
|
|
3151
|
+
var sv = i << 4 | cd[i];
|
|
3152
|
+
var r_1 = mb - cd[i];
|
|
3153
|
+
var v = le[cd[i] - 1]++ << r_1;
|
|
3154
|
+
for (var m = v | (1 << r_1) - 1; v <= m; ++v) {
|
|
3155
|
+
co[rev2[v] >> rvb] = sv;
|
|
3156
|
+
}
|
|
3157
|
+
}
|
|
3158
|
+
}
|
|
3159
|
+
} else {
|
|
3160
|
+
co = new u16(s);
|
|
3161
|
+
for (i = 0; i < s; ++i) {
|
|
3162
|
+
if (cd[i]) {
|
|
3163
|
+
co[i] = rev2[le[cd[i] - 1]++] >> 15 - cd[i];
|
|
3164
|
+
}
|
|
3165
|
+
}
|
|
3166
|
+
}
|
|
3167
|
+
return co;
|
|
3168
|
+
});
|
|
3169
|
+
flt = new u8(288);
|
|
3170
|
+
for (i = 0; i < 144; ++i)
|
|
3171
|
+
flt[i] = 8;
|
|
3172
|
+
for (i = 144; i < 256; ++i)
|
|
3173
|
+
flt[i] = 9;
|
|
3174
|
+
for (i = 256; i < 280; ++i)
|
|
3175
|
+
flt[i] = 7;
|
|
3176
|
+
for (i = 280; i < 288; ++i)
|
|
3177
|
+
flt[i] = 8;
|
|
3178
|
+
fdt = new u8(32);
|
|
3179
|
+
for (i = 0; i < 32; ++i)
|
|
3180
|
+
fdt[i] = 5;
|
|
3181
|
+
flrm = /* @__PURE__ */ hMap(flt, 9, 1);
|
|
3182
|
+
fdrm = /* @__PURE__ */ hMap(fdt, 5, 1);
|
|
3183
|
+
max = function(a) {
|
|
3184
|
+
var m = a[0];
|
|
3185
|
+
for (var i = 1; i < a.length; ++i) {
|
|
3186
|
+
if (a[i] > m)
|
|
3187
|
+
m = a[i];
|
|
3188
|
+
}
|
|
3189
|
+
return m;
|
|
3190
|
+
};
|
|
3191
|
+
bits = function(d, p, m) {
|
|
3192
|
+
var o = p / 8 | 0;
|
|
3193
|
+
return (d[o] | d[o + 1] << 8) >> (p & 7) & m;
|
|
3194
|
+
};
|
|
3195
|
+
bits16 = function(d, p) {
|
|
3196
|
+
var o = p / 8 | 0;
|
|
3197
|
+
return (d[o] | d[o + 1] << 8 | d[o + 2] << 16) >> (p & 7);
|
|
3198
|
+
};
|
|
3199
|
+
shft = function(p) {
|
|
3200
|
+
return (p + 7) / 8 | 0;
|
|
3201
|
+
};
|
|
3202
|
+
slc = function(v, s, e) {
|
|
3203
|
+
if (s == null || s < 0)
|
|
3204
|
+
s = 0;
|
|
3205
|
+
if (e == null || e > v.length)
|
|
3206
|
+
e = v.length;
|
|
3207
|
+
return new u8(v.subarray(s, e));
|
|
3208
|
+
};
|
|
3209
|
+
ec = [
|
|
3210
|
+
"unexpected EOF",
|
|
3211
|
+
"invalid block type",
|
|
3212
|
+
"invalid length/literal",
|
|
3213
|
+
"invalid distance",
|
|
3214
|
+
"stream finished",
|
|
3215
|
+
"no stream handler",
|
|
3216
|
+
,
|
|
3217
|
+
"no callback",
|
|
3218
|
+
"invalid UTF-8 data",
|
|
3219
|
+
"extra field too long",
|
|
3220
|
+
"date not in range 1980-2099",
|
|
3221
|
+
"filename too long",
|
|
3222
|
+
"stream finishing",
|
|
3223
|
+
"invalid zip data"
|
|
3224
|
+
// determined by unknown compression method
|
|
3225
|
+
];
|
|
3226
|
+
err = function(ind, msg, nt) {
|
|
3227
|
+
var e = new Error(msg || ec[ind]);
|
|
3228
|
+
e.code = ind;
|
|
3229
|
+
if (Error.captureStackTrace)
|
|
3230
|
+
Error.captureStackTrace(e, err);
|
|
3231
|
+
if (!nt)
|
|
3232
|
+
throw e;
|
|
3233
|
+
return e;
|
|
3234
|
+
};
|
|
3235
|
+
inflt = function(dat, st, buf, dict) {
|
|
3236
|
+
var sl = dat.length, dl = dict ? dict.length : 0;
|
|
3237
|
+
if (!sl || st.f && !st.l)
|
|
3238
|
+
return buf || new u8(0);
|
|
3239
|
+
var noBuf = !buf;
|
|
3240
|
+
var resize = noBuf || st.i != 2;
|
|
3241
|
+
var noSt = st.i;
|
|
3242
|
+
if (noBuf)
|
|
3243
|
+
buf = new u8(sl * 3);
|
|
3244
|
+
var cbuf = function(l2) {
|
|
3245
|
+
var bl = buf.length;
|
|
3246
|
+
if (l2 > bl) {
|
|
3247
|
+
var nbuf = new u8(Math.max(bl * 2, l2));
|
|
3248
|
+
nbuf.set(buf);
|
|
3249
|
+
buf = nbuf;
|
|
3250
|
+
}
|
|
3251
|
+
};
|
|
3252
|
+
var final = st.f || 0, pos = st.p || 0, bt = st.b || 0, lm = st.l, dm = st.d, lbt = st.m, dbt = st.n;
|
|
3253
|
+
var tbts = sl * 8;
|
|
3254
|
+
do {
|
|
3255
|
+
if (!lm) {
|
|
3256
|
+
final = bits(dat, pos, 1);
|
|
3257
|
+
var type = bits(dat, pos + 1, 3);
|
|
3258
|
+
pos += 3;
|
|
3259
|
+
if (!type) {
|
|
3260
|
+
var s = shft(pos) + 4, l = dat[s - 4] | dat[s - 3] << 8, t = s + l;
|
|
3261
|
+
if (t > sl) {
|
|
3262
|
+
if (noSt)
|
|
3263
|
+
err(0);
|
|
3264
|
+
break;
|
|
3265
|
+
}
|
|
3266
|
+
if (resize)
|
|
3267
|
+
cbuf(bt + l);
|
|
3268
|
+
buf.set(dat.subarray(s, t), bt);
|
|
3269
|
+
st.b = bt += l, st.p = pos = t * 8, st.f = final;
|
|
3270
|
+
continue;
|
|
3271
|
+
} else if (type == 1)
|
|
3272
|
+
lm = flrm, dm = fdrm, lbt = 9, dbt = 5;
|
|
3273
|
+
else if (type == 2) {
|
|
3274
|
+
var hLit = bits(dat, pos, 31) + 257, hcLen = bits(dat, pos + 10, 15) + 4;
|
|
3275
|
+
var tl = hLit + bits(dat, pos + 5, 31) + 1;
|
|
3276
|
+
pos += 14;
|
|
3277
|
+
var ldt = new u8(tl);
|
|
3278
|
+
var clt = new u8(19);
|
|
3279
|
+
for (var i = 0; i < hcLen; ++i) {
|
|
3280
|
+
clt[clim[i]] = bits(dat, pos + i * 3, 7);
|
|
3281
|
+
}
|
|
3282
|
+
pos += hcLen * 3;
|
|
3283
|
+
var clb = max(clt), clbmsk = (1 << clb) - 1;
|
|
3284
|
+
var clm = hMap(clt, clb, 1);
|
|
3285
|
+
for (var i = 0; i < tl; ) {
|
|
3286
|
+
var r = clm[bits(dat, pos, clbmsk)];
|
|
3287
|
+
pos += r & 15;
|
|
3288
|
+
var s = r >> 4;
|
|
3289
|
+
if (s < 16) {
|
|
3290
|
+
ldt[i++] = s;
|
|
3291
|
+
} else {
|
|
3292
|
+
var c = 0, n = 0;
|
|
3293
|
+
if (s == 16)
|
|
3294
|
+
n = 3 + bits(dat, pos, 3), pos += 2, c = ldt[i - 1];
|
|
3295
|
+
else if (s == 17)
|
|
3296
|
+
n = 3 + bits(dat, pos, 7), pos += 3;
|
|
3297
|
+
else if (s == 18)
|
|
3298
|
+
n = 11 + bits(dat, pos, 127), pos += 7;
|
|
3299
|
+
while (n--)
|
|
3300
|
+
ldt[i++] = c;
|
|
3301
|
+
}
|
|
3302
|
+
}
|
|
3303
|
+
var lt = ldt.subarray(0, hLit), dt = ldt.subarray(hLit);
|
|
3304
|
+
lbt = max(lt);
|
|
3305
|
+
dbt = max(dt);
|
|
3306
|
+
lm = hMap(lt, lbt, 1);
|
|
3307
|
+
dm = hMap(dt, dbt, 1);
|
|
3308
|
+
} else
|
|
3309
|
+
err(1);
|
|
3310
|
+
if (pos > tbts) {
|
|
3311
|
+
if (noSt)
|
|
3312
|
+
err(0);
|
|
3313
|
+
break;
|
|
3314
|
+
}
|
|
3315
|
+
}
|
|
3316
|
+
if (resize)
|
|
3317
|
+
cbuf(bt + 131072);
|
|
3318
|
+
var lms = (1 << lbt) - 1, dms = (1 << dbt) - 1;
|
|
3319
|
+
var lpos = pos;
|
|
3320
|
+
for (; ; lpos = pos) {
|
|
3321
|
+
var c = lm[bits16(dat, pos) & lms], sym = c >> 4;
|
|
3322
|
+
pos += c & 15;
|
|
3323
|
+
if (pos > tbts) {
|
|
3324
|
+
if (noSt)
|
|
3325
|
+
err(0);
|
|
3326
|
+
break;
|
|
3327
|
+
}
|
|
3328
|
+
if (!c)
|
|
3329
|
+
err(2);
|
|
3330
|
+
if (sym < 256)
|
|
3331
|
+
buf[bt++] = sym;
|
|
3332
|
+
else if (sym == 256) {
|
|
3333
|
+
lpos = pos, lm = null;
|
|
3334
|
+
break;
|
|
3335
|
+
} else {
|
|
3336
|
+
var add = sym - 254;
|
|
3337
|
+
if (sym > 264) {
|
|
3338
|
+
var i = sym - 257, b = fleb[i];
|
|
3339
|
+
add = bits(dat, pos, (1 << b) - 1) + fl[i];
|
|
3340
|
+
pos += b;
|
|
3341
|
+
}
|
|
3342
|
+
var d = dm[bits16(dat, pos) & dms], dsym = d >> 4;
|
|
3343
|
+
if (!d)
|
|
3344
|
+
err(3);
|
|
3345
|
+
pos += d & 15;
|
|
3346
|
+
var dt = fd[dsym];
|
|
3347
|
+
if (dsym > 3) {
|
|
3348
|
+
var b = fdeb[dsym];
|
|
3349
|
+
dt += bits16(dat, pos) & (1 << b) - 1, pos += b;
|
|
3350
|
+
}
|
|
3351
|
+
if (pos > tbts) {
|
|
3352
|
+
if (noSt)
|
|
3353
|
+
err(0);
|
|
3354
|
+
break;
|
|
3355
|
+
}
|
|
3356
|
+
if (resize)
|
|
3357
|
+
cbuf(bt + 131072);
|
|
3358
|
+
var end = bt + add;
|
|
3359
|
+
if (bt < dt) {
|
|
3360
|
+
var shift = dl - dt, dend = Math.min(dt, end);
|
|
3361
|
+
if (shift + bt < 0)
|
|
3362
|
+
err(3);
|
|
3363
|
+
for (; bt < dend; ++bt)
|
|
3364
|
+
buf[bt] = dict[shift + bt];
|
|
3365
|
+
}
|
|
3366
|
+
for (; bt < end; ++bt)
|
|
3367
|
+
buf[bt] = buf[bt - dt];
|
|
3368
|
+
}
|
|
3369
|
+
}
|
|
3370
|
+
st.l = lm, st.p = lpos, st.b = bt, st.f = final;
|
|
3371
|
+
if (lm)
|
|
3372
|
+
final = 1, st.m = lbt, st.d = dm, st.n = dbt;
|
|
3373
|
+
} while (!final);
|
|
3374
|
+
return bt != buf.length && noBuf ? slc(buf, 0, bt) : buf.subarray(0, bt);
|
|
3375
|
+
};
|
|
3376
|
+
et = /* @__PURE__ */ new u8(0);
|
|
3377
|
+
gzs = function(d) {
|
|
3378
|
+
if (d[0] != 31 || d[1] != 139 || d[2] != 8)
|
|
3379
|
+
err(6, "invalid gzip data");
|
|
3380
|
+
var flg = d[3];
|
|
3381
|
+
var st = 10;
|
|
3382
|
+
if (flg & 4)
|
|
3383
|
+
st += (d[10] | d[11] << 8) + 2;
|
|
3384
|
+
for (var zs = (flg >> 3 & 1) + (flg >> 4 & 1); zs > 0; zs -= !d[st++])
|
|
3385
|
+
;
|
|
3386
|
+
return st + (flg & 2);
|
|
3387
|
+
};
|
|
3388
|
+
gzl = function(d) {
|
|
3389
|
+
var l = d.length;
|
|
3390
|
+
return (d[l - 4] | d[l - 3] << 8 | d[l - 2] << 16 | d[l - 1] << 24) >>> 0;
|
|
3391
|
+
};
|
|
3392
|
+
td = typeof TextDecoder != "undefined" && /* @__PURE__ */ new TextDecoder();
|
|
3393
|
+
tds = 0;
|
|
3394
|
+
try {
|
|
3395
|
+
td.decode(et, { stream: true });
|
|
3396
|
+
tds = 1;
|
|
3397
|
+
} catch (e) {
|
|
3398
|
+
}
|
|
3399
|
+
}
|
|
3400
|
+
});
|
|
3401
|
+
|
|
3402
|
+
// projects/@machina.at/shell/src/lib/tar-commands.ts
|
|
3403
|
+
var tar_commands_exports = {};
|
|
3404
|
+
__export(tar_commands_exports, {
|
|
3405
|
+
tar: () => tar
|
|
3406
|
+
});
|
|
3407
|
+
function parseArgs2(args) {
|
|
3408
|
+
const opts = { extract: false, list: false, gzip: false, file: null, dir: null, stripComponents: 0 };
|
|
3409
|
+
const wants = [];
|
|
3410
|
+
for (let i = 0; i < args.length; i++) {
|
|
3411
|
+
const a = args[i];
|
|
3412
|
+
if (a.startsWith("--strip-components")) {
|
|
3413
|
+
const eq = a.indexOf("=");
|
|
3414
|
+
if (eq >= 0) {
|
|
3415
|
+
const n = parseInt(a.slice(eq + 1), 10);
|
|
3416
|
+
if (!Number.isFinite(n) || n < 0) return { opts, error: `invalid --strip-components value '${a.slice(eq + 1)}'` };
|
|
3417
|
+
opts.stripComponents = n;
|
|
3418
|
+
} else {
|
|
3419
|
+
wants.push("strip");
|
|
3420
|
+
}
|
|
3421
|
+
continue;
|
|
3422
|
+
}
|
|
3423
|
+
if (a.startsWith("-") && a.length > 1 && !a.startsWith("--")) {
|
|
3424
|
+
for (const ch of a.slice(1)) {
|
|
3425
|
+
if (ch === "x") opts.extract = true;
|
|
3426
|
+
else if (ch === "t") opts.list = true;
|
|
3427
|
+
else if (ch === "z") opts.gzip = true;
|
|
3428
|
+
else if (ch === "f") wants.push("file");
|
|
3429
|
+
else if (ch === "C") wants.push("dir");
|
|
3430
|
+
else return { opts, error: `unknown option '-${ch}'` };
|
|
3431
|
+
}
|
|
3432
|
+
} else {
|
|
3433
|
+
const target = wants.shift();
|
|
3434
|
+
if (target === "dir") opts.dir = a;
|
|
3435
|
+
else if (target === "strip") {
|
|
3436
|
+
const n = parseInt(a, 10);
|
|
3437
|
+
if (!Number.isFinite(n) || n < 0) return { opts, error: `invalid --strip-components value '${a}'` };
|
|
3438
|
+
opts.stripComponents = n;
|
|
3439
|
+
} else opts.file = a;
|
|
3440
|
+
}
|
|
3441
|
+
}
|
|
3442
|
+
if (wants.includes("file")) return { opts, error: `option requires an argument -- 'f'` };
|
|
3443
|
+
if (wants.includes("strip")) return { opts, error: `option requires an argument -- '--strip-components'` };
|
|
3444
|
+
return { opts };
|
|
3445
|
+
}
|
|
3446
|
+
function stripPath(name, n) {
|
|
3447
|
+
if (n <= 0) return name;
|
|
3448
|
+
const parts = name.split("/").filter((p) => p.length > 0);
|
|
3449
|
+
if (parts.length <= n) return null;
|
|
3450
|
+
return parts.slice(n).join("/");
|
|
3451
|
+
}
|
|
3452
|
+
function parseTar(buf) {
|
|
3453
|
+
const dec = new TextDecoder();
|
|
3454
|
+
const str = (off, len) => dec.decode(buf.subarray(off, off + len)).replace(/\0.*$/, "").trim();
|
|
3455
|
+
const entries = [];
|
|
3456
|
+
let pos = 0;
|
|
3457
|
+
while (pos + 512 <= buf.length) {
|
|
3458
|
+
let allZero = true;
|
|
3459
|
+
for (let i = 0; i < 512; i++) {
|
|
3460
|
+
if (buf[pos + i] !== 0) {
|
|
3461
|
+
allZero = false;
|
|
3462
|
+
break;
|
|
3463
|
+
}
|
|
3464
|
+
}
|
|
3465
|
+
if (allZero) break;
|
|
3466
|
+
const name = str(pos, 100);
|
|
3467
|
+
const sizeOct = str(pos + 124, 12);
|
|
3468
|
+
const size = parseInt(sizeOct || "0", 8) || 0;
|
|
3469
|
+
const typeflag = String.fromCharCode(buf[pos + 156]);
|
|
3470
|
+
const dataStart = pos + 512;
|
|
3471
|
+
pos = dataStart + Math.ceil(size / 512) * 512;
|
|
3472
|
+
const isFile = typeflag === "0" || typeflag === "\0";
|
|
3473
|
+
const isDir = typeflag === "5" || name.endsWith("/") && !isFile;
|
|
3474
|
+
if (!name || !isFile && !isDir) continue;
|
|
3475
|
+
const data = isDir ? new Uint8Array(0) : buf.subarray(dataStart, dataStart + size);
|
|
3476
|
+
entries.push({ name: name.replace(/\/$/, ""), isDir, data });
|
|
3477
|
+
}
|
|
3478
|
+
return entries;
|
|
3479
|
+
}
|
|
3480
|
+
function tar(args, ctx) {
|
|
3481
|
+
const stderr = ctx.stderr ?? process.stderr;
|
|
3482
|
+
const stdout = ctx.stdout ?? process.stdout;
|
|
3483
|
+
const parsed = parseArgs2(args);
|
|
3484
|
+
if (parsed.error) {
|
|
3485
|
+
stderr.write(`tar: ${parsed.error}
|
|
3486
|
+
`);
|
|
3487
|
+
return 2;
|
|
3488
|
+
}
|
|
3489
|
+
const opts = parsed.opts;
|
|
3490
|
+
if (!opts.extract && !opts.list) {
|
|
3491
|
+
stderr.write("tar: you must specify one of -x or -t\n");
|
|
3492
|
+
return 2;
|
|
3493
|
+
}
|
|
3494
|
+
if (!opts.file) {
|
|
3495
|
+
stderr.write("tar: no archive file specified (-f)\n");
|
|
3496
|
+
return 2;
|
|
3497
|
+
}
|
|
3498
|
+
let raw;
|
|
3499
|
+
try {
|
|
3500
|
+
const read = ctx.fs.readFileSync(opts.file);
|
|
3501
|
+
raw = typeof read === "string" ? new TextEncoder().encode(read) : read;
|
|
3502
|
+
} catch (err2) {
|
|
3503
|
+
stderr.write(`tar: ${opts.file}: ${err2.message ?? "cannot open"}
|
|
3504
|
+
`);
|
|
3505
|
+
return 2;
|
|
3506
|
+
}
|
|
3507
|
+
let tarBuf;
|
|
3508
|
+
try {
|
|
3509
|
+
tarBuf = opts.gzip ? gunzipSync(raw) : raw;
|
|
3510
|
+
} catch (err2) {
|
|
3511
|
+
stderr.write(`tar: gunzip failed: ${err2.message ?? String(err2)}
|
|
3512
|
+
`);
|
|
3513
|
+
return 2;
|
|
3514
|
+
}
|
|
3515
|
+
let entries;
|
|
3516
|
+
try {
|
|
3517
|
+
entries = parseTar(tarBuf);
|
|
3518
|
+
} catch (err2) {
|
|
3519
|
+
stderr.write(`tar: parse failed: ${err2.message ?? String(err2)}
|
|
3520
|
+
`);
|
|
3521
|
+
return 2;
|
|
3522
|
+
}
|
|
3523
|
+
const baseDir = opts.dir ?? ctx.cwd;
|
|
3524
|
+
for (const e of entries) {
|
|
3525
|
+
if (opts.list) {
|
|
3526
|
+
stdout.write(e.name + "\n");
|
|
3527
|
+
continue;
|
|
3528
|
+
}
|
|
3529
|
+
const stripped = stripPath(e.name, opts.stripComponents);
|
|
3530
|
+
if (stripped === null) continue;
|
|
3531
|
+
const dest = ctx.path.join(baseDir, stripped);
|
|
3532
|
+
try {
|
|
3533
|
+
if (e.isDir) {
|
|
3534
|
+
ctx.fs.mkdirSync(dest, { recursive: true });
|
|
3535
|
+
} else {
|
|
3536
|
+
const parent = ctx.path.dirname(dest);
|
|
3537
|
+
ctx.fs.mkdirSync(parent, { recursive: true });
|
|
3538
|
+
ctx.fs.writeFileSync(dest, e.data);
|
|
3539
|
+
}
|
|
3540
|
+
} catch (err2) {
|
|
3541
|
+
stderr.write(`tar: ${e.name}: ${err2.message ?? String(err2)}
|
|
3542
|
+
`);
|
|
3543
|
+
return 2;
|
|
3544
|
+
}
|
|
3545
|
+
}
|
|
3546
|
+
return 0;
|
|
3547
|
+
}
|
|
3548
|
+
var init_tar_commands = __esm({
|
|
3549
|
+
"projects/@machina.at/shell/src/lib/tar-commands.ts"() {
|
|
3550
|
+
"use strict";
|
|
3551
|
+
init_esm();
|
|
3552
|
+
}
|
|
3553
|
+
});
|
|
3554
|
+
|
|
3555
|
+
// projects/@machina.at/shell/src/commands/tar.js
|
|
3556
|
+
var require_tar = __commonJS({
|
|
3557
|
+
"projects/@machina.at/shell/src/commands/tar.js"(exports2, module2) {
|
|
3558
|
+
"use strict";
|
|
3559
|
+
var tarCommands = (init_tar_commands(), __toCommonJS(tar_commands_exports));
|
|
3560
|
+
module2.exports = function tar2(args, ctx) {
|
|
3561
|
+
return tarCommands.tar(args, ctx);
|
|
3562
|
+
};
|
|
3563
|
+
}
|
|
3564
|
+
});
|
|
3565
|
+
|
|
3566
|
+
// projects/@machina.at/shell/src/commands/index.js
|
|
3567
|
+
var require_commands = __commonJS({
|
|
3568
|
+
"projects/@machina.at/shell/src/commands/index.js"(exports2, module2) {
|
|
3569
|
+
"use strict";
|
|
3570
|
+
module2.exports.ls = require_ls();
|
|
3571
|
+
module2.exports.cat = require_cat();
|
|
3572
|
+
module2.exports.echo = require_echo();
|
|
3573
|
+
module2.exports.pwd = require_pwd();
|
|
3574
|
+
module2.exports.mkdir = require_mkdir();
|
|
3575
|
+
module2.exports.head = require_head();
|
|
3576
|
+
module2.exports.tail = require_tail();
|
|
3577
|
+
module2.exports.env = require_env();
|
|
3578
|
+
module2.exports.which = require_which();
|
|
3579
|
+
module2.exports.help = require_help();
|
|
3580
|
+
module2.exports.clear = require_clear();
|
|
3581
|
+
module2.exports.test = require_test();
|
|
3582
|
+
module2.exports["["] = require_test();
|
|
3583
|
+
module2.exports["true"] = require_true();
|
|
3584
|
+
module2.exports["false"] = require_false();
|
|
3585
|
+
module2.exports.cp = require_cp();
|
|
3586
|
+
module2.exports.mv = require_mv();
|
|
3587
|
+
module2.exports.rm = require_rm();
|
|
3588
|
+
module2.exports.rmdir = require_rmdir();
|
|
3589
|
+
module2.exports.touch = require_touch();
|
|
3590
|
+
module2.exports.ln = require_ln();
|
|
3591
|
+
module2.exports.grep = require_grep();
|
|
3592
|
+
module2.exports.sed = require_sed();
|
|
3593
|
+
module2.exports.wc = require_wc();
|
|
3594
|
+
module2.exports.sort = require_sort();
|
|
3595
|
+
module2.exports.uniq = require_uniq();
|
|
3596
|
+
module2.exports.cut = require_cut();
|
|
3597
|
+
module2.exports.tr = require_tr();
|
|
3598
|
+
module2.exports.tee = require_tee();
|
|
3599
|
+
module2.exports.printf = require_printf();
|
|
3600
|
+
module2.exports.find = require_find();
|
|
3601
|
+
module2.exports.xargs = require_xargs();
|
|
3602
|
+
module2.exports.sleep = require_sleep();
|
|
3603
|
+
module2.exports.date = require_date();
|
|
3604
|
+
module2.exports.basename = require_basename();
|
|
3605
|
+
module2.exports.dirname = require_dirname();
|
|
3606
|
+
module2.exports.bit = require_bit();
|
|
3607
|
+
module2.exports.seq = require_seq();
|
|
3608
|
+
module2.exports.readlink = require_readlink();
|
|
3609
|
+
module2.exports.realpath = require_realpath();
|
|
3610
|
+
module2.exports.mktemp = require_mktemp();
|
|
3611
|
+
module2.exports.expr = require_expr();
|
|
3612
|
+
module2.exports.yes = require_yes();
|
|
3613
|
+
module2.exports.whoami = require_whoami();
|
|
3614
|
+
module2.exports.uname = require_uname();
|
|
3615
|
+
module2.exports.chmod = require_chmod();
|
|
3616
|
+
module2.exports.nl = require_nl();
|
|
3617
|
+
module2.exports.paste = require_paste();
|
|
3618
|
+
module2.exports.rev = require_rev();
|
|
3619
|
+
module2.exports.curl = require_curl();
|
|
3620
|
+
module2.exports.tar = require_tar();
|
|
3621
|
+
}
|
|
3622
|
+
});
|
|
3623
|
+
|
|
3624
|
+
// projects/@machina.at/shell/src/lib/parse-bg.ts
|
|
3625
|
+
var parse_bg_exports = {};
|
|
3626
|
+
__export(parse_bg_exports, {
|
|
3627
|
+
parseTrailingBg: () => parseTrailingBg
|
|
3628
|
+
});
|
|
3629
|
+
function parseTrailingBg(line) {
|
|
3630
|
+
if (typeof line !== "string") return { command: line, bgMode: false };
|
|
3631
|
+
const trimmed = line.replace(/\s+$/, "");
|
|
3632
|
+
if (trimmed.length === 0) return { command: line, bgMode: false };
|
|
3633
|
+
const lastIdx = trimmed.length - 1;
|
|
3634
|
+
if (trimmed.charAt(lastIdx) !== "&") return { command: line, bgMode: false };
|
|
3635
|
+
let inQuote = false;
|
|
3636
|
+
let quoteChar = "";
|
|
3637
|
+
for (let i = 0; i < trimmed.length; i++) {
|
|
3638
|
+
const ch = trimmed.charAt(i);
|
|
3639
|
+
if (inQuote) {
|
|
3640
|
+
if (ch === quoteChar && trimmed.charAt(i - 1) !== "\\") {
|
|
3641
|
+
inQuote = false;
|
|
3642
|
+
}
|
|
3643
|
+
} else if (ch === '"' || ch === "'") {
|
|
3644
|
+
inQuote = true;
|
|
3645
|
+
quoteChar = ch;
|
|
3646
|
+
}
|
|
3647
|
+
}
|
|
3648
|
+
if (inQuote) return { command: line, bgMode: false };
|
|
3649
|
+
if (lastIdx >= 1 && trimmed.charAt(lastIdx - 1) === "&") {
|
|
3650
|
+
return { command: line, bgMode: false };
|
|
3651
|
+
}
|
|
3652
|
+
const withoutBg = trimmed.slice(0, lastIdx).replace(/\s+$/, "");
|
|
3653
|
+
return { command: withoutBg, bgMode: true };
|
|
3654
|
+
}
|
|
3655
|
+
var init_parse_bg = __esm({
|
|
3656
|
+
"projects/@machina.at/shell/src/lib/parse-bg.ts"() {
|
|
3657
|
+
"use strict";
|
|
3658
|
+
}
|
|
3659
|
+
});
|
|
3660
|
+
|
|
3661
|
+
// projects/@machina.at/shell/src/lib/job-table.ts
|
|
3662
|
+
var job_table_exports = {};
|
|
3663
|
+
__export(job_table_exports, {
|
|
3664
|
+
JobTable: () => JobTable
|
|
3665
|
+
});
|
|
3666
|
+
function formatStatus(status) {
|
|
3667
|
+
switch (status.kind) {
|
|
3668
|
+
case "running":
|
|
3669
|
+
return "Running";
|
|
3670
|
+
case "done":
|
|
3671
|
+
return "Done";
|
|
3672
|
+
case "exit":
|
|
3673
|
+
return `Exit ${status.code}`;
|
|
3674
|
+
}
|
|
3675
|
+
}
|
|
3676
|
+
var JobTable;
|
|
3677
|
+
var init_job_table = __esm({
|
|
3678
|
+
"projects/@machina.at/shell/src/lib/job-table.ts"() {
|
|
3679
|
+
"use strict";
|
|
3680
|
+
JobTable = class {
|
|
3681
|
+
jobs = /* @__PURE__ */ new Map();
|
|
3682
|
+
nextId = 0;
|
|
3683
|
+
add(cmd, child) {
|
|
3684
|
+
const id = ++this.nextId;
|
|
3685
|
+
this.jobs.set(id, { id, cmd, status: { kind: "running" }, child });
|
|
3686
|
+
return id;
|
|
3687
|
+
}
|
|
3688
|
+
attachChild(id, child) {
|
|
3689
|
+
const job = this.jobs.get(id);
|
|
3690
|
+
if (job) job.child = child;
|
|
3691
|
+
}
|
|
3692
|
+
getChildByRef(ref) {
|
|
3693
|
+
return this.findByRef(ref)?.child;
|
|
3694
|
+
}
|
|
3695
|
+
finish(id, exitCode) {
|
|
3696
|
+
const job = this.jobs.get(id);
|
|
3697
|
+
if (!job) return;
|
|
3698
|
+
job.status = exitCode === 0 ? { kind: "done" } : { kind: "exit", code: exitCode };
|
|
3699
|
+
}
|
|
3700
|
+
listRunning() {
|
|
3701
|
+
return [...this.jobs.values()].filter((j) => j.status.kind === "running");
|
|
3702
|
+
}
|
|
3703
|
+
listAll() {
|
|
3704
|
+
return [...this.jobs.values()];
|
|
3705
|
+
}
|
|
3706
|
+
pruneFinished() {
|
|
3707
|
+
for (const [id, job] of this.jobs) {
|
|
3708
|
+
if (job.status.kind !== "running") {
|
|
3709
|
+
this.jobs.delete(id);
|
|
3710
|
+
}
|
|
3711
|
+
}
|
|
3712
|
+
}
|
|
3713
|
+
findByRef(ref) {
|
|
3714
|
+
if (!ref || !ref.startsWith("%")) return void 0;
|
|
3715
|
+
const tail2 = ref.slice(1);
|
|
3716
|
+
const list = this.listAll().sort((a, b) => a.id - b.id);
|
|
3717
|
+
if (list.length === 0) return void 0;
|
|
3718
|
+
if (tail2 === "+" || tail2 === "%" || tail2 === "") {
|
|
3719
|
+
return list[list.length - 1];
|
|
3720
|
+
}
|
|
3721
|
+
if (tail2 === "-") {
|
|
3722
|
+
return list.length >= 2 ? list[list.length - 2] : list[list.length - 1];
|
|
3723
|
+
}
|
|
3724
|
+
if (/^\d+$/.test(tail2)) {
|
|
3725
|
+
const id = parseInt(tail2, 10);
|
|
3726
|
+
return this.jobs.get(id);
|
|
3727
|
+
}
|
|
3728
|
+
return void 0;
|
|
3729
|
+
}
|
|
3730
|
+
format() {
|
|
3731
|
+
const list = this.listAll();
|
|
3732
|
+
if (list.length === 0) return "";
|
|
3733
|
+
list.sort((a, b) => a.id - b.id);
|
|
3734
|
+
const last = list[list.length - 1]?.id;
|
|
3735
|
+
const prev = list[list.length - 2]?.id;
|
|
3736
|
+
return list.map((j) => {
|
|
3737
|
+
const marker = j.id === last ? "+" : j.id === prev ? "-" : " ";
|
|
3738
|
+
const status = formatStatus(j.status);
|
|
3739
|
+
return `[${j.id}]${marker} ${status.padEnd(11)}${j.cmd}`;
|
|
3740
|
+
}).join("\n");
|
|
3741
|
+
}
|
|
3742
|
+
};
|
|
3743
|
+
}
|
|
3744
|
+
});
|
|
3745
|
+
|
|
3746
|
+
// projects/@machina.at/shell/src/lib/path-resolve.ts
|
|
3747
|
+
var path_resolve_exports = {};
|
|
3748
|
+
__export(path_resolve_exports, {
|
|
3749
|
+
resolveInPath: () => resolveInPath
|
|
3750
|
+
});
|
|
3751
|
+
function resolveInPath(cmdName, ctx) {
|
|
3752
|
+
if (cmdName.includes("/")) return null;
|
|
3753
|
+
const PATH = ctx.env.PATH;
|
|
3754
|
+
if (!PATH) return null;
|
|
3755
|
+
for (const dir of PATH.split(":")) {
|
|
3756
|
+
if (!dir) continue;
|
|
3757
|
+
const candidate = ctx.path.join(dir, cmdName);
|
|
3758
|
+
try {
|
|
3759
|
+
if (ctx.fs.existsSync(candidate) && ctx.fs.statSync(candidate).isFile()) {
|
|
3760
|
+
return candidate;
|
|
3761
|
+
}
|
|
3762
|
+
} catch (_e) {
|
|
3763
|
+
}
|
|
3764
|
+
}
|
|
3765
|
+
return null;
|
|
3766
|
+
}
|
|
3767
|
+
var init_path_resolve = __esm({
|
|
3768
|
+
"projects/@machina.at/shell/src/lib/path-resolve.ts"() {
|
|
3769
|
+
"use strict";
|
|
3770
|
+
}
|
|
3771
|
+
});
|
|
3772
|
+
|
|
3773
|
+
// projects/@machina.at/shell/src/lib/rc-loader.ts
|
|
3774
|
+
var rc_loader_exports = {};
|
|
3775
|
+
__export(rc_loader_exports, {
|
|
3776
|
+
listRcFiles: () => listRcFiles
|
|
3777
|
+
});
|
|
3778
|
+
function listRcFiles(ctx) {
|
|
3779
|
+
const candidates = [];
|
|
3780
|
+
if (ctx.env.HOME) candidates.push(ctx.path.join(ctx.env.HOME, ".xshrc"));
|
|
3781
|
+
const result = [];
|
|
3782
|
+
for (const file of candidates) {
|
|
3783
|
+
try {
|
|
3784
|
+
if (ctx.fs.statSync(file).isFile()) result.push(file);
|
|
3785
|
+
} catch (_e) {
|
|
3786
|
+
}
|
|
3787
|
+
}
|
|
3788
|
+
return result;
|
|
3789
|
+
}
|
|
3790
|
+
var init_rc_loader = __esm({
|
|
3791
|
+
"projects/@machina.at/shell/src/lib/rc-loader.ts"() {
|
|
3792
|
+
"use strict";
|
|
3793
|
+
}
|
|
3794
|
+
});
|
|
3795
|
+
|
|
3796
|
+
// projects/@machina.at/shell/src/commands/sh.js
|
|
3797
|
+
var require_sh = __commonJS({
|
|
3798
|
+
"projects/@machina.at/shell/src/commands/sh.js"() {
|
|
3799
|
+
"use strict";
|
|
3800
|
+
var fs = require("fs");
|
|
3801
|
+
var path = require("path");
|
|
3802
|
+
var commands = require_commands();
|
|
3803
|
+
var parseBg = (init_parse_bg(), __toCommonJS(parse_bg_exports));
|
|
3804
|
+
var JobTableModule = (init_job_table(), __toCommonJS(job_table_exports));
|
|
3805
|
+
var pathResolveModule = (init_path_resolve(), __toCommonJS(path_resolve_exports));
|
|
3806
|
+
var rcLoaderModule = (init_rc_loader(), __toCommonJS(rc_loader_exports));
|
|
3807
|
+
var jobTable = new JobTableModule.JobTable();
|
|
3808
|
+
function charWidth(ch) {
|
|
3809
|
+
var code = ch.charCodeAt(0);
|
|
3810
|
+
if (code >= 4352 && code <= 4447 || // Hangul Jamo
|
|
3811
|
+
code >= 11904 && code <= 12350 || // CJK Radicals
|
|
3812
|
+
code >= 12352 && code <= 13247 || // Hiragana, Katakana, CJK
|
|
3813
|
+
code >= 13312 && code <= 19903 || // CJK Extension A
|
|
3814
|
+
code >= 19968 && code <= 40959 || // CJK Unified
|
|
3815
|
+
code >= 44032 && code <= 55215 || // Hangul Syllables
|
|
3816
|
+
code >= 63744 && code <= 64255 || // CJK Compatibility
|
|
3817
|
+
code >= 65072 && code <= 65135 || // CJK Compatibility Forms
|
|
3818
|
+
code >= 65281 && code <= 65376 || // Fullwidth Forms
|
|
3819
|
+
code >= 65504 && code <= 65510 || // Fullwidth Signs
|
|
3820
|
+
code >= 131072 && code <= 195103) {
|
|
3821
|
+
return 2;
|
|
3822
|
+
}
|
|
3823
|
+
return 1;
|
|
3824
|
+
}
|
|
3825
|
+
function strWidth(s) {
|
|
3826
|
+
var w = 0;
|
|
3827
|
+
for (var i = 0; i < s.length; i++) w += charWidth(s[i]);
|
|
3828
|
+
return w;
|
|
3829
|
+
}
|
|
3830
|
+
var cwd = process.cwd() || "/";
|
|
3831
|
+
var env = Object.assign({}, process.env);
|
|
3832
|
+
var exitCode = 0;
|
|
3833
|
+
var asyncRunning = false;
|
|
3834
|
+
var pendingChain = null;
|
|
3835
|
+
function prompt() {
|
|
3836
|
+
process.stdout.write("\x1B[1;32m$ \x1B[0m");
|
|
3837
|
+
}
|
|
3838
|
+
function parseCommandLine(line) {
|
|
3839
|
+
var args = [];
|
|
3840
|
+
var current = "";
|
|
3841
|
+
var inQuote = false;
|
|
3842
|
+
var quoteChar = "";
|
|
3843
|
+
for (var i = 0; i < line.length; i++) {
|
|
3844
|
+
var ch = line[i];
|
|
3845
|
+
if (inQuote) {
|
|
3846
|
+
if (ch === quoteChar) {
|
|
3847
|
+
inQuote = false;
|
|
3848
|
+
} else {
|
|
3849
|
+
current += ch;
|
|
3850
|
+
}
|
|
3851
|
+
} else if (ch === '"' || ch === "'") {
|
|
3852
|
+
inQuote = true;
|
|
3853
|
+
quoteChar = ch;
|
|
3854
|
+
} else if (ch === " " || ch === " ") {
|
|
3855
|
+
if (current) {
|
|
3856
|
+
args.push(current);
|
|
3857
|
+
current = "";
|
|
3858
|
+
}
|
|
3859
|
+
} else {
|
|
3860
|
+
current += ch;
|
|
3861
|
+
}
|
|
3862
|
+
}
|
|
3863
|
+
if (current) args.push(current);
|
|
3864
|
+
return args;
|
|
3865
|
+
}
|
|
3866
|
+
function executeCommand(line, bgMode) {
|
|
3867
|
+
var trimmed = line.trim();
|
|
3868
|
+
if (!trimmed) return 0;
|
|
3869
|
+
bgMode = !!bgMode;
|
|
3870
|
+
var chain = splitChainLocal(trimmed);
|
|
3871
|
+
return runChainFrom(chain, 0, bgMode, false);
|
|
3872
|
+
}
|
|
3873
|
+
function runChainFrom(chain, startIdx, bgMode, skip) {
|
|
3874
|
+
for (var idx = startIdx; idx < chain.length; idx++) {
|
|
3875
|
+
var item = chain[idx];
|
|
3876
|
+
if (!item.cmd) continue;
|
|
3877
|
+
if (skip) {
|
|
3878
|
+
skip = false;
|
|
3879
|
+
if (item.operator === "||" && exitCode !== 0) {
|
|
3880
|
+
skip = false;
|
|
3881
|
+
} else if (item.operator === "&&" && exitCode === 0) {
|
|
3882
|
+
skip = false;
|
|
3883
|
+
} else {
|
|
3884
|
+
continue;
|
|
3885
|
+
}
|
|
3886
|
+
}
|
|
3887
|
+
var isLast = idx === chain.length - 1;
|
|
3888
|
+
exitCode = executeSingle(expandVars(item.cmd), bgMode && isLast);
|
|
3889
|
+
if (exitCode === -1 && asyncRunning && !isLast) {
|
|
3890
|
+
pendingChain = { chain, nextIdx: idx + 1, bgMode, lastOp: item.operator };
|
|
3891
|
+
return -1;
|
|
3892
|
+
}
|
|
3893
|
+
if (item.operator === "&&" && exitCode !== 0) skip = true;
|
|
3894
|
+
else if (item.operator === "||" && exitCode === 0) skip = true;
|
|
3895
|
+
}
|
|
3896
|
+
return exitCode;
|
|
3897
|
+
}
|
|
3898
|
+
function executeSingle(line, bgMode) {
|
|
3899
|
+
bgMode = !!bgMode;
|
|
3900
|
+
var parsed = parseRedirects(line);
|
|
3901
|
+
var command = parsed.command;
|
|
3902
|
+
var redir = parsed.redirects;
|
|
3903
|
+
var stdinData = null;
|
|
3904
|
+
if (redir.stdin) {
|
|
3905
|
+
var stdinPath = redir.stdin.startsWith("/") ? redir.stdin : path.join(cwd, redir.stdin);
|
|
3906
|
+
try {
|
|
3907
|
+
var content = fs.readFileSync(stdinPath);
|
|
3908
|
+
stdinData = typeof content === "string" ? content : new TextDecoder().decode(content);
|
|
3909
|
+
} catch (err2) {
|
|
3910
|
+
process.stderr.write("sh: " + redir.stdin + ": no such file or directory\n");
|
|
3911
|
+
return 1;
|
|
3912
|
+
}
|
|
3913
|
+
}
|
|
3914
|
+
var hasPipe = command.indexOf("|") !== -1;
|
|
3915
|
+
var capturedStdout = "";
|
|
3916
|
+
var capturedStderr = "";
|
|
3917
|
+
var captureStdout = !!redir.stdout;
|
|
3918
|
+
var captureStderr = !!redir.stderr;
|
|
3919
|
+
var result;
|
|
3920
|
+
if (hasPipe) {
|
|
3921
|
+
result = executePipeline(command, stdinData, captureStdout, captureStderr);
|
|
3922
|
+
} else {
|
|
3923
|
+
result = executeSimple(command, stdinData, captureStdout, captureStderr, bgMode);
|
|
3924
|
+
}
|
|
3925
|
+
if (redir.stdout && result.stdout) {
|
|
3926
|
+
var outPath = redir.stdout.startsWith("/") ? redir.stdout : path.join(cwd, redir.stdout);
|
|
3927
|
+
if (redir.stdoutAppend) fs.appendFileSync(outPath, result.stdout);
|
|
3928
|
+
else fs.writeFileSync(outPath, result.stdout);
|
|
3929
|
+
}
|
|
3930
|
+
if (redir.stderr && result.stderr) {
|
|
3931
|
+
var errPath = redir.stderr.startsWith("/") ? redir.stderr : path.join(cwd, redir.stderr);
|
|
3932
|
+
fs.writeFileSync(errPath, result.stderr);
|
|
3933
|
+
}
|
|
3934
|
+
return result.exitCode;
|
|
3935
|
+
}
|
|
3936
|
+
function parseRedirects(line) {
|
|
3937
|
+
var redirects = { stdout: null, stdoutAppend: false, stdin: null, stderr: null };
|
|
3938
|
+
var command = line;
|
|
3939
|
+
var m;
|
|
3940
|
+
m = command.match(/\s+2>\s+(\S+)/);
|
|
3941
|
+
if (m) {
|
|
3942
|
+
redirects.stderr = m[1];
|
|
3943
|
+
command = command.replace(m[0], " ");
|
|
3944
|
+
}
|
|
3945
|
+
m = command.match(/\s*>>\s+(\S+)/);
|
|
3946
|
+
if (m) {
|
|
3947
|
+
redirects.stdout = m[1];
|
|
3948
|
+
redirects.stdoutAppend = true;
|
|
3949
|
+
command = command.replace(m[0], " ");
|
|
3950
|
+
}
|
|
3951
|
+
if (!redirects.stdout) {
|
|
3952
|
+
m = command.match(/\s*>\s+(\S+)/);
|
|
3953
|
+
if (m) {
|
|
3954
|
+
redirects.stdout = m[1];
|
|
3955
|
+
command = command.replace(m[0], " ");
|
|
3956
|
+
}
|
|
3957
|
+
}
|
|
3958
|
+
m = command.match(/\s*<\s+(\S+)/);
|
|
3959
|
+
if (m) {
|
|
3960
|
+
redirects.stdin = m[1];
|
|
3961
|
+
command = command.replace(m[0], " ");
|
|
3962
|
+
}
|
|
3963
|
+
return { command: command.trim(), redirects };
|
|
3964
|
+
}
|
|
3965
|
+
function executePipeline(line, stdinData, captureStdout, captureStderr) {
|
|
3966
|
+
var stages = line.split("|");
|
|
3967
|
+
var data = stdinData;
|
|
3968
|
+
var lastExitCode = 0;
|
|
3969
|
+
var lastStderr = "";
|
|
3970
|
+
for (var i = 0; i < stages.length; i++) {
|
|
3971
|
+
var stage = stages[i].trim();
|
|
3972
|
+
if (!stage) continue;
|
|
3973
|
+
var isLast = true;
|
|
3974
|
+
for (var j = i + 1; j < stages.length; j++) {
|
|
3975
|
+
if (stages[j].trim()) {
|
|
3976
|
+
isLast = false;
|
|
3977
|
+
break;
|
|
3978
|
+
}
|
|
3979
|
+
}
|
|
3980
|
+
var result = executeSimple(stage, data, !isLast || captureStdout, captureStderr);
|
|
3981
|
+
lastExitCode = result.exitCode;
|
|
3982
|
+
lastStderr += result.stderr || "";
|
|
3983
|
+
data = result.stdout || "";
|
|
3984
|
+
}
|
|
3985
|
+
return { exitCode: lastExitCode, stdout: captureStdout ? data : "", stderr: lastStderr };
|
|
3986
|
+
}
|
|
3987
|
+
function executeSimple(line, stdinData, captureStdout, captureStderr, bgMode) {
|
|
3988
|
+
bgMode = !!bgMode;
|
|
3989
|
+
var args = parseCommandLine(line);
|
|
3990
|
+
if (args.length === 0) return { exitCode: 0, stdout: "", stderr: "" };
|
|
3991
|
+
args = expandGlobsLocal(args);
|
|
3992
|
+
var cmdName = args[0];
|
|
3993
|
+
var cmdArgs = args.slice(1);
|
|
3994
|
+
if (cmdName === "cd") return { exitCode: builtinCd(cmdArgs), stdout: "", stderr: "" };
|
|
3995
|
+
if (cmdName === "jobs") {
|
|
3996
|
+
var out = jobTable.format();
|
|
3997
|
+
if (out) process.stdout.write(out + "\n");
|
|
3998
|
+
jobTable.pruneFinished();
|
|
3999
|
+
return { exitCode: 0, stdout: "", stderr: "" };
|
|
4000
|
+
}
|
|
4001
|
+
if (cmdName === "export") return { exitCode: builtinExport(cmdArgs), stdout: "", stderr: "" };
|
|
4002
|
+
if (cmdName === "exit") {
|
|
4003
|
+
process.exit(cmdArgs[0] ? parseInt(cmdArgs[0], 10) : 0);
|
|
4004
|
+
return { exitCode: 0, stdout: "", stderr: "" };
|
|
4005
|
+
}
|
|
4006
|
+
if (cmdName === "set") return { exitCode: builtinSet(cmdArgs), stdout: "", stderr: "" };
|
|
4007
|
+
if (cmdName === "unset") return { exitCode: builtinUnset(cmdArgs), stdout: "", stderr: "" };
|
|
4008
|
+
if (cmdName === "read") return { exitCode: builtinRead(cmdArgs, stdinData), stdout: "", stderr: "" };
|
|
4009
|
+
if (cmdName === "source" || cmdName === ".") return { exitCode: builtinSource(cmdArgs), stdout: "", stderr: "" };
|
|
4010
|
+
var resolved = pathResolveModule.resolveInPath(cmdName, { env, fs, path });
|
|
4011
|
+
if (resolved) {
|
|
4012
|
+
var head2 = null;
|
|
4013
|
+
try {
|
|
4014
|
+
var fd2 = fs.openSync(resolved, "r");
|
|
4015
|
+
head2 = Buffer.alloc(4);
|
|
4016
|
+
fs.readSync(fd2, head2, 0, 4, 0);
|
|
4017
|
+
fs.closeSync(fd2);
|
|
4018
|
+
} catch (_e) {
|
|
4019
|
+
head2 = null;
|
|
4020
|
+
}
|
|
4021
|
+
var isShebang = head2 && head2[0] === 35 && head2[1] === 33;
|
|
4022
|
+
var isBinary = head2 && (head2[0] === 127 && head2[1] === 69 && head2[2] === 76 && head2[3] === 70 || // ELF
|
|
4023
|
+
(head2[0] === 207 || head2[0] === 206) && head2[1] === 250 && head2[2] === 237 && head2[3] === 254 || // Mach-O LE
|
|
4024
|
+
head2[0] === 254 && head2[1] === 237 && head2[2] === 250 && (head2[3] === 207 || head2[3] === 206) || // Mach-O BE
|
|
4025
|
+
head2[0] === 202 && head2[1] === 254 && head2[2] === 186 && head2[3] === 190 || // Mach-O Fat
|
|
4026
|
+
head2[0] === 77 && head2[1] === 90);
|
|
4027
|
+
if (!isShebang && !isBinary) {
|
|
4028
|
+
return { exitCode: builtinSource([resolved].concat(cmdArgs)), stdout: "", stderr: "" };
|
|
4029
|
+
}
|
|
4030
|
+
cmdName = resolved;
|
|
4031
|
+
} else {
|
|
4032
|
+
var cmd = commands[cmdName];
|
|
4033
|
+
if (cmd) {
|
|
4034
|
+
var capturedOut = "";
|
|
4035
|
+
var capturedErr = "";
|
|
4036
|
+
var origOut = process.stdout.write;
|
|
4037
|
+
var origErr = process.stderr.write;
|
|
4038
|
+
if (captureStdout) {
|
|
4039
|
+
process.stdout.write = function(d) {
|
|
4040
|
+
capturedOut += d;
|
|
4041
|
+
return true;
|
|
4042
|
+
};
|
|
4043
|
+
}
|
|
4044
|
+
if (captureStderr) {
|
|
4045
|
+
process.stderr.write = function(d) {
|
|
4046
|
+
capturedErr += d;
|
|
4047
|
+
return true;
|
|
4048
|
+
};
|
|
4049
|
+
}
|
|
4050
|
+
var ctx = { cwd, env, fs, path, stdin: stdinData };
|
|
4051
|
+
var code;
|
|
4052
|
+
try {
|
|
4053
|
+
code = cmd(cmdArgs, ctx);
|
|
4054
|
+
} catch (err2) {
|
|
4055
|
+
process.stderr.write("sh: " + cmdName + ": " + (err2.message || err2) + "\n");
|
|
4056
|
+
code = 1;
|
|
4057
|
+
} finally {
|
|
4058
|
+
if (captureStdout) process.stdout.write = origOut;
|
|
4059
|
+
if (captureStderr) process.stderr.write = origErr;
|
|
4060
|
+
}
|
|
4061
|
+
return { exitCode: code, stdout: capturedOut, stderr: capturedErr };
|
|
4062
|
+
}
|
|
4063
|
+
}
|
|
4064
|
+
var isWebkernel = env && env.PLANX_WEBKERNEL === "1";
|
|
4065
|
+
var cp2 = require("child_process");
|
|
4066
|
+
var spawnOpts = { cwd, env };
|
|
4067
|
+
if (!bgMode && !isWebkernel) {
|
|
4068
|
+
spawnOpts.stdio = "inherit";
|
|
4069
|
+
if (process.stdin.setRawMode) process.stdin.setRawMode(false);
|
|
4070
|
+
process.stdin.pause();
|
|
4071
|
+
}
|
|
4072
|
+
var child = cp2.spawn(cmdName, cmdArgs, spawnOpts);
|
|
4073
|
+
if (bgMode || isWebkernel) {
|
|
4074
|
+
child.stdout && child.stdout.on("data", function(d) {
|
|
4075
|
+
process.stdout.write(typeof d === "string" ? d : d.toString());
|
|
4076
|
+
});
|
|
4077
|
+
child.stderr && child.stderr.on("data", function(d) {
|
|
4078
|
+
process.stderr.write(typeof d === "string" ? d : d.toString());
|
|
4079
|
+
});
|
|
4080
|
+
}
|
|
4081
|
+
child.on("error", function(e) {
|
|
4082
|
+
process.stderr.write("sh: " + cmdName + ": " + (e.message || e) + "\n");
|
|
4083
|
+
});
|
|
4084
|
+
if (bgMode) {
|
|
4085
|
+
var bgCmdLine = [cmdName].concat(cmdArgs).join(" ");
|
|
4086
|
+
var jobId = jobTable.add(bgCmdLine);
|
|
4087
|
+
process.stderr.write("[" + jobId + "] " + cmdName + "\n");
|
|
4088
|
+
child.on("close", function(code2) {
|
|
4089
|
+
var c = typeof code2 === "number" ? code2 : 0;
|
|
4090
|
+
exitCode = c;
|
|
4091
|
+
jobTable.finish(jobId, c);
|
|
4092
|
+
var statusText = c === 0 ? "Done" : "Exit " + c;
|
|
4093
|
+
process.stderr.write("[" + jobId + "]+ " + statusText + " " + cmdName + "\n");
|
|
4094
|
+
});
|
|
4095
|
+
return { exitCode: 0, stdout: "", stderr: "" };
|
|
4096
|
+
}
|
|
4097
|
+
child.on("close", function(code2) {
|
|
4098
|
+
exitCode = typeof code2 === "number" ? code2 : 0;
|
|
4099
|
+
asyncRunning = false;
|
|
4100
|
+
if (pendingChain) {
|
|
4101
|
+
var pc = pendingChain;
|
|
4102
|
+
pendingChain = null;
|
|
4103
|
+
var initialSkip = pc.lastOp === "&&" && exitCode !== 0 || pc.lastOp === "||" && exitCode === 0;
|
|
4104
|
+
asyncRunning = true;
|
|
4105
|
+
var rc = runChainFrom(pc.chain, pc.nextIdx, pc.bgMode, initialSkip);
|
|
4106
|
+
if (rc === -1) return;
|
|
4107
|
+
asyncRunning = false;
|
|
4108
|
+
}
|
|
4109
|
+
process.stdin.resume();
|
|
4110
|
+
if (process.stdin.setRawMode) process.stdin.setRawMode(true);
|
|
4111
|
+
prompt();
|
|
4112
|
+
processInputBuffer();
|
|
4113
|
+
});
|
|
4114
|
+
return { exitCode: -1, stdout: "", stderr: "" };
|
|
4115
|
+
}
|
|
4116
|
+
function builtinSet(args) {
|
|
4117
|
+
if (args.length === 0) {
|
|
4118
|
+
for (var key in env) {
|
|
4119
|
+
process.stdout.write(key + "=" + env[key] + "\n");
|
|
4120
|
+
}
|
|
4121
|
+
return 0;
|
|
4122
|
+
}
|
|
4123
|
+
for (var i = 0; i < args.length; i++) {
|
|
4124
|
+
if (args[i].startsWith("-")) continue;
|
|
4125
|
+
var eq = args[i].indexOf("=");
|
|
4126
|
+
if (eq !== -1) {
|
|
4127
|
+
env[args[i].substring(0, eq)] = args[i].substring(eq + 1);
|
|
4128
|
+
}
|
|
4129
|
+
}
|
|
4130
|
+
return 0;
|
|
4131
|
+
}
|
|
4132
|
+
function builtinUnset(args) {
|
|
4133
|
+
for (var i = 0; i < args.length; i++) {
|
|
4134
|
+
if (!args[i].startsWith("-")) delete env[args[i]];
|
|
4135
|
+
}
|
|
4136
|
+
return 0;
|
|
4137
|
+
}
|
|
4138
|
+
function builtinRead(args, stdinData) {
|
|
4139
|
+
var varNames = [];
|
|
4140
|
+
for (var i = 0; i < args.length; i++) {
|
|
4141
|
+
if (args[i] === "-p" && args[i + 1]) {
|
|
4142
|
+
i++;
|
|
4143
|
+
continue;
|
|
4144
|
+
}
|
|
4145
|
+
if (!args[i].startsWith("-")) varNames.push(args[i]);
|
|
4146
|
+
}
|
|
4147
|
+
var input = stdinData || "";
|
|
4148
|
+
var firstLine = input.split("\n")[0] || "";
|
|
4149
|
+
var parts = firstLine.split(/\s+/);
|
|
4150
|
+
if (varNames.length === 0) {
|
|
4151
|
+
env["REPLY"] = firstLine;
|
|
4152
|
+
} else {
|
|
4153
|
+
for (var j = 0; j < varNames.length; j++) {
|
|
4154
|
+
env[varNames[j]] = j === varNames.length - 1 ? parts.slice(j).join(" ") : parts[j] || "";
|
|
4155
|
+
}
|
|
4156
|
+
}
|
|
4157
|
+
return input ? 0 : 1;
|
|
4158
|
+
}
|
|
4159
|
+
function builtinSource(args) {
|
|
4160
|
+
if (args.length === 0) {
|
|
4161
|
+
process.stderr.write("source: missing file operand\n");
|
|
4162
|
+
return 1;
|
|
4163
|
+
}
|
|
4164
|
+
var filePath = args[0].startsWith("/") ? args[0] : path.join(cwd, args[0]);
|
|
4165
|
+
var rawContent;
|
|
4166
|
+
try {
|
|
4167
|
+
var raw = fs.readFileSync(filePath);
|
|
4168
|
+
rawContent = typeof raw === "string" ? raw : new TextDecoder().decode(raw);
|
|
4169
|
+
} catch (err2) {
|
|
4170
|
+
process.stderr.write("source: " + args[0] + ": " + err2.message + "\n");
|
|
4171
|
+
return 1;
|
|
4172
|
+
}
|
|
4173
|
+
var lastCode = 0;
|
|
4174
|
+
var lines = rawContent.split("\n");
|
|
4175
|
+
for (var i = 0; i < lines.length; i++) {
|
|
4176
|
+
var trimmed = lines[i].trim();
|
|
4177
|
+
if (!trimmed || trimmed.startsWith("#")) continue;
|
|
4178
|
+
lastCode = executeCommand(trimmed);
|
|
4179
|
+
}
|
|
4180
|
+
return lastCode;
|
|
4181
|
+
}
|
|
4182
|
+
function builtinCd(args) {
|
|
4183
|
+
var target = args[0] || env.HOME || "/";
|
|
4184
|
+
if (target === "-") {
|
|
4185
|
+
target = env.OLDPWD || cwd;
|
|
4186
|
+
}
|
|
4187
|
+
var newCwd;
|
|
4188
|
+
if (target.startsWith("/")) {
|
|
4189
|
+
newCwd = target;
|
|
4190
|
+
} else if (target === "..") {
|
|
4191
|
+
newCwd = path.dirname(cwd);
|
|
4192
|
+
} else {
|
|
4193
|
+
newCwd = path.join(cwd, target);
|
|
4194
|
+
}
|
|
4195
|
+
newCwd = path.normalize(newCwd);
|
|
4196
|
+
try {
|
|
4197
|
+
var stat = fs.statSync(newCwd);
|
|
4198
|
+
if (!stat.isDirectory()) {
|
|
4199
|
+
process.stderr.write("cd: not a directory: " + target + "\n");
|
|
4200
|
+
return 1;
|
|
4201
|
+
}
|
|
4202
|
+
env.OLDPWD = cwd;
|
|
4203
|
+
cwd = newCwd;
|
|
4204
|
+
process.chdir(newCwd);
|
|
4205
|
+
return 0;
|
|
4206
|
+
} catch (e) {
|
|
4207
|
+
process.stderr.write("cd: no such file or directory: " + target + "\n");
|
|
4208
|
+
return 1;
|
|
4209
|
+
}
|
|
4210
|
+
}
|
|
4211
|
+
function builtinExport(args) {
|
|
4212
|
+
for (var i = 0; i < args.length; i++) {
|
|
4213
|
+
var eq = args[i].indexOf("=");
|
|
4214
|
+
if (eq === -1) {
|
|
4215
|
+
process.stderr.write("export: invalid identifier: " + args[i] + "\n");
|
|
4216
|
+
return 1;
|
|
4217
|
+
}
|
|
4218
|
+
var key = args[i].substring(0, eq);
|
|
4219
|
+
var value = args[i].substring(eq + 1);
|
|
4220
|
+
env[key] = value;
|
|
4221
|
+
process.env[key] = value;
|
|
4222
|
+
}
|
|
4223
|
+
return 0;
|
|
4224
|
+
}
|
|
4225
|
+
function expandVars(input) {
|
|
4226
|
+
var result = "";
|
|
4227
|
+
var i = 0;
|
|
4228
|
+
while (i < input.length) {
|
|
4229
|
+
if (input[i] === "'") {
|
|
4230
|
+
result += "'";
|
|
4231
|
+
i++;
|
|
4232
|
+
while (i < input.length && input[i] !== "'") {
|
|
4233
|
+
result += input[i];
|
|
4234
|
+
i++;
|
|
4235
|
+
}
|
|
4236
|
+
if (i < input.length) {
|
|
4237
|
+
result += "'";
|
|
4238
|
+
i++;
|
|
4239
|
+
}
|
|
4240
|
+
continue;
|
|
4241
|
+
}
|
|
4242
|
+
if (input[i] === "\\" && i + 1 < input.length) {
|
|
4243
|
+
result += input[i] + input[i + 1];
|
|
4244
|
+
i += 2;
|
|
4245
|
+
continue;
|
|
4246
|
+
}
|
|
4247
|
+
if (input[i] === "$" && input[i + 1] === "(" && input[i + 2] === "(") {
|
|
4248
|
+
i += 3;
|
|
4249
|
+
var expr2 = "";
|
|
4250
|
+
while (i < input.length && !(input[i] === ")" && input[i + 1] === ")")) {
|
|
4251
|
+
expr2 += input[i];
|
|
4252
|
+
i++;
|
|
4253
|
+
}
|
|
4254
|
+
i += 2;
|
|
4255
|
+
try {
|
|
4256
|
+
result += evalSimpleArith(expr2);
|
|
4257
|
+
} catch (_e) {
|
|
4258
|
+
result += "0";
|
|
4259
|
+
}
|
|
4260
|
+
continue;
|
|
4261
|
+
}
|
|
4262
|
+
if (input[i] === "$" && input[i + 1] === "?") {
|
|
4263
|
+
result += exitCode.toString();
|
|
4264
|
+
i += 2;
|
|
4265
|
+
continue;
|
|
4266
|
+
}
|
|
4267
|
+
if (input[i] === "$" && input[i + 1] === "$") {
|
|
4268
|
+
result += process.pid.toString();
|
|
4269
|
+
i += 2;
|
|
4270
|
+
continue;
|
|
4271
|
+
}
|
|
4272
|
+
if (input[i] === "$" && input[i + 1] === "{") {
|
|
4273
|
+
i += 2;
|
|
4274
|
+
var braceContent = "";
|
|
4275
|
+
while (i < input.length && input[i] !== "}") {
|
|
4276
|
+
braceContent += input[i];
|
|
4277
|
+
i++;
|
|
4278
|
+
}
|
|
4279
|
+
if (i < input.length) i++;
|
|
4280
|
+
if (braceContent[0] === "#") {
|
|
4281
|
+
var vn = braceContent.slice(1);
|
|
4282
|
+
result += (env[vn] || "").length.toString();
|
|
4283
|
+
} else if (braceContent.indexOf(":-") !== -1) {
|
|
4284
|
+
var parts = braceContent.split(":-");
|
|
4285
|
+
result += env[parts[0]] || parts[1] || "";
|
|
4286
|
+
} else if (braceContent.indexOf(":+") !== -1) {
|
|
4287
|
+
var parts2 = braceContent.split(":+");
|
|
4288
|
+
result += env[parts2[0]] ? parts2[1] : "";
|
|
4289
|
+
} else {
|
|
4290
|
+
result += env[braceContent] || "";
|
|
4291
|
+
}
|
|
4292
|
+
continue;
|
|
4293
|
+
}
|
|
4294
|
+
if (input[i] === "$" && i + 1 < input.length && /[a-zA-Z_]/.test(input[i + 1])) {
|
|
4295
|
+
i++;
|
|
4296
|
+
var name = "";
|
|
4297
|
+
while (i < input.length && /[a-zA-Z0-9_]/.test(input[i])) {
|
|
4298
|
+
name += input[i];
|
|
4299
|
+
i++;
|
|
4300
|
+
}
|
|
4301
|
+
result += env[name] || "";
|
|
4302
|
+
continue;
|
|
4303
|
+
}
|
|
4304
|
+
if (input[i] === "~" && (i === 0 || input[i - 1] === " " || input[i - 1] === "=")) {
|
|
4305
|
+
if (i + 1 >= input.length || input[i + 1] === "/" || input[i + 1] === " ") {
|
|
4306
|
+
result += env.HOME || "/";
|
|
4307
|
+
i++;
|
|
4308
|
+
continue;
|
|
4309
|
+
}
|
|
4310
|
+
}
|
|
4311
|
+
if (input[i] === '"') {
|
|
4312
|
+
result += '"';
|
|
4313
|
+
i++;
|
|
4314
|
+
while (i < input.length && input[i] !== '"') {
|
|
4315
|
+
if (input[i] === "$") {
|
|
4316
|
+
if (input[i + 1] === "?") {
|
|
4317
|
+
result += exitCode.toString();
|
|
4318
|
+
i += 2;
|
|
4319
|
+
continue;
|
|
4320
|
+
}
|
|
4321
|
+
if (input[i + 1] === "{") {
|
|
4322
|
+
i += 2;
|
|
4323
|
+
var bc = "";
|
|
4324
|
+
while (i < input.length && input[i] !== "}") {
|
|
4325
|
+
bc += input[i];
|
|
4326
|
+
i++;
|
|
4327
|
+
}
|
|
4328
|
+
if (i < input.length) i++;
|
|
4329
|
+
if (bc.indexOf(":-") !== -1) {
|
|
4330
|
+
var p = bc.split(":-");
|
|
4331
|
+
result += env[p[0]] || p[1] || "";
|
|
4332
|
+
} else {
|
|
4333
|
+
result += env[bc] || "";
|
|
4334
|
+
}
|
|
4335
|
+
continue;
|
|
4336
|
+
}
|
|
4337
|
+
if (/[a-zA-Z_]/.test(input[i + 1] || "")) {
|
|
4338
|
+
i++;
|
|
4339
|
+
var n2 = "";
|
|
4340
|
+
while (i < input.length && /[a-zA-Z0-9_]/.test(input[i])) {
|
|
4341
|
+
n2 += input[i];
|
|
4342
|
+
i++;
|
|
4343
|
+
}
|
|
4344
|
+
result += env[n2] || "";
|
|
4345
|
+
continue;
|
|
4346
|
+
}
|
|
4347
|
+
}
|
|
4348
|
+
if (input[i] === "\\" && i + 1 < input.length) {
|
|
4349
|
+
var next = input[i + 1];
|
|
4350
|
+
if (next === "$" || next === '"' || next === "\\") {
|
|
4351
|
+
result += next;
|
|
4352
|
+
i += 2;
|
|
4353
|
+
continue;
|
|
4354
|
+
}
|
|
4355
|
+
}
|
|
4356
|
+
result += input[i];
|
|
4357
|
+
i++;
|
|
4358
|
+
}
|
|
4359
|
+
if (i < input.length) {
|
|
4360
|
+
result += '"';
|
|
4361
|
+
i++;
|
|
4362
|
+
}
|
|
4363
|
+
continue;
|
|
4364
|
+
}
|
|
4365
|
+
result += input[i];
|
|
4366
|
+
i++;
|
|
4367
|
+
}
|
|
4368
|
+
return result;
|
|
4369
|
+
}
|
|
4370
|
+
function evalSimpleArith(expr2) {
|
|
4371
|
+
var resolved = expr2.replace(/[a-zA-Z_]\w*/g, function(name) {
|
|
4372
|
+
return env[name] || "0";
|
|
4373
|
+
});
|
|
4374
|
+
var val = 0;
|
|
4375
|
+
try {
|
|
4376
|
+
if (/^[\d+\-*/%() <>=!&|]+$/.test(resolved.trim())) {
|
|
4377
|
+
val = new Function("return (" + resolved + ")")();
|
|
4378
|
+
}
|
|
4379
|
+
} catch (_e) {
|
|
4380
|
+
}
|
|
4381
|
+
return (val | 0).toString();
|
|
4382
|
+
}
|
|
4383
|
+
function splitChainLocal(line) {
|
|
4384
|
+
var result = [];
|
|
4385
|
+
var current = "";
|
|
4386
|
+
var inSingle = false, inDouble = false;
|
|
4387
|
+
for (var i = 0; i < line.length; i++) {
|
|
4388
|
+
var ch = line[i];
|
|
4389
|
+
if (ch === "'" && !inDouble) {
|
|
4390
|
+
inSingle = !inSingle;
|
|
4391
|
+
current += ch;
|
|
4392
|
+
continue;
|
|
4393
|
+
}
|
|
4394
|
+
if (ch === '"' && !inSingle) {
|
|
4395
|
+
inDouble = !inDouble;
|
|
4396
|
+
current += ch;
|
|
4397
|
+
continue;
|
|
4398
|
+
}
|
|
4399
|
+
if (inSingle || inDouble) {
|
|
4400
|
+
current += ch;
|
|
4401
|
+
continue;
|
|
4402
|
+
}
|
|
4403
|
+
if (ch === "&" && line[i + 1] === "&") {
|
|
4404
|
+
result.push({ cmd: current.trim(), operator: "&&" });
|
|
4405
|
+
current = "";
|
|
4406
|
+
i++;
|
|
4407
|
+
continue;
|
|
4408
|
+
}
|
|
4409
|
+
if (ch === "|" && line[i + 1] === "|") {
|
|
4410
|
+
result.push({ cmd: current.trim(), operator: "||" });
|
|
4411
|
+
current = "";
|
|
4412
|
+
i++;
|
|
4413
|
+
continue;
|
|
4414
|
+
}
|
|
4415
|
+
if (ch === ";") {
|
|
4416
|
+
result.push({ cmd: current.trim(), operator: ";" });
|
|
4417
|
+
current = "";
|
|
4418
|
+
continue;
|
|
4419
|
+
}
|
|
4420
|
+
current += ch;
|
|
4421
|
+
}
|
|
4422
|
+
if (current.trim()) result.push({ cmd: current.trim(), operator: null });
|
|
4423
|
+
return result;
|
|
4424
|
+
}
|
|
4425
|
+
function expandGlobsLocal(args) {
|
|
4426
|
+
var result = [];
|
|
4427
|
+
for (var a = 0; a < args.length; a++) {
|
|
4428
|
+
var arg = args[a];
|
|
4429
|
+
if (arg.indexOf("*") !== -1 || arg.indexOf("?") !== -1) {
|
|
4430
|
+
var dir = path.dirname(arg);
|
|
4431
|
+
var base = path.basename(arg);
|
|
4432
|
+
var absDir = dir.startsWith("/") ? dir : path.join(cwd, dir);
|
|
4433
|
+
try {
|
|
4434
|
+
var entries = fs.readdirSync(absDir);
|
|
4435
|
+
var re = globToRe(base);
|
|
4436
|
+
var matched = entries.filter(function(e) {
|
|
4437
|
+
return re.test(e);
|
|
4438
|
+
}).sort();
|
|
4439
|
+
if (matched.length > 0) {
|
|
4440
|
+
for (var m = 0; m < matched.length; m++) {
|
|
4441
|
+
result.push(dir === "." ? matched[m] : path.join(dir, matched[m]));
|
|
4442
|
+
}
|
|
4443
|
+
continue;
|
|
4444
|
+
}
|
|
4445
|
+
} catch (_e) {
|
|
4446
|
+
}
|
|
4447
|
+
}
|
|
4448
|
+
result.push(arg);
|
|
4449
|
+
}
|
|
4450
|
+
return result;
|
|
4451
|
+
}
|
|
4452
|
+
function globToRe(pattern) {
|
|
4453
|
+
var re = "";
|
|
4454
|
+
for (var i = 0; i < pattern.length; i++) {
|
|
4455
|
+
var ch = pattern[i];
|
|
4456
|
+
if (ch === "*") re += ".*";
|
|
4457
|
+
else if (ch === "?") re += ".";
|
|
4458
|
+
else if (".+^${}()|[]\\".indexOf(ch) !== -1) re += "\\" + ch;
|
|
4459
|
+
else re += ch;
|
|
4460
|
+
}
|
|
4461
|
+
return new RegExp("^" + re + "$");
|
|
4462
|
+
}
|
|
4463
|
+
var inputBuffer = "";
|
|
4464
|
+
var cursorPos = 0;
|
|
4465
|
+
var commandHistory = [];
|
|
4466
|
+
var historyIndex = -1;
|
|
4467
|
+
var savedInput = "";
|
|
4468
|
+
var completionMenu = null;
|
|
4469
|
+
var menuVisible = false;
|
|
4470
|
+
function wordLeft() {
|
|
4471
|
+
if (cursorPos === 0) return;
|
|
4472
|
+
var pos = cursorPos;
|
|
4473
|
+
while (pos > 0 && inputBuffer[pos - 1] === " ") pos--;
|
|
4474
|
+
while (pos > 0 && inputBuffer[pos - 1] !== " ") pos--;
|
|
4475
|
+
var moved = strWidth(inputBuffer.slice(pos, cursorPos));
|
|
4476
|
+
if (moved > 0) {
|
|
4477
|
+
process.stdout.write("\x1B[" + moved + "D");
|
|
4478
|
+
cursorPos = pos;
|
|
4479
|
+
}
|
|
4480
|
+
}
|
|
4481
|
+
function wordRight() {
|
|
4482
|
+
var len = inputBuffer.length;
|
|
4483
|
+
if (cursorPos >= len) return;
|
|
4484
|
+
var pos = cursorPos;
|
|
4485
|
+
while (pos < len && inputBuffer[pos] !== " ") pos++;
|
|
4486
|
+
while (pos < len && inputBuffer[pos] === " ") pos++;
|
|
4487
|
+
var moved = strWidth(inputBuffer.slice(cursorPos, pos));
|
|
4488
|
+
if (moved > 0) {
|
|
4489
|
+
process.stdout.write("\x1B[" + moved + "C");
|
|
4490
|
+
cursorPos = pos;
|
|
4491
|
+
}
|
|
4492
|
+
}
|
|
4493
|
+
function deleteWordBack() {
|
|
4494
|
+
if (cursorPos === 0) return;
|
|
4495
|
+
var pos = cursorPos;
|
|
4496
|
+
while (pos > 0 && inputBuffer[pos - 1] === " ") pos--;
|
|
4497
|
+
while (pos > 0 && inputBuffer[pos - 1] !== " ") pos--;
|
|
4498
|
+
var delCount = cursorPos - pos;
|
|
4499
|
+
if (delCount === 0) return;
|
|
4500
|
+
var delWidth = strWidth(inputBuffer.slice(pos, cursorPos));
|
|
4501
|
+
var tail2 = inputBuffer.slice(cursorPos);
|
|
4502
|
+
var tailWidth = strWidth(tail2);
|
|
4503
|
+
inputBuffer = inputBuffer.slice(0, pos) + tail2;
|
|
4504
|
+
cursorPos = pos;
|
|
4505
|
+
process.stdout.write("\x1B[" + delWidth + "D" + tail2 + " ".repeat(delWidth) + "\x1B[" + (tailWidth + delWidth) + "D");
|
|
4506
|
+
}
|
|
4507
|
+
function replaceLine(newLine) {
|
|
4508
|
+
var oldWidth = strWidth(inputBuffer.slice(0, cursorPos));
|
|
4509
|
+
if (oldWidth > 0) {
|
|
4510
|
+
process.stdout.write("\x1B[" + oldWidth + "D");
|
|
4511
|
+
}
|
|
4512
|
+
process.stdout.write("\x1B[K");
|
|
4513
|
+
inputBuffer = newLine;
|
|
4514
|
+
cursorPos = newLine.length;
|
|
4515
|
+
process.stdout.write(newLine);
|
|
4516
|
+
}
|
|
4517
|
+
function historyUp() {
|
|
4518
|
+
if (commandHistory.length === 0) return;
|
|
4519
|
+
var prefix = historyIndex === -1 ? inputBuffer : savedInput;
|
|
4520
|
+
if (historyIndex === -1) {
|
|
4521
|
+
savedInput = inputBuffer;
|
|
4522
|
+
}
|
|
4523
|
+
var start = historyIndex === -1 ? commandHistory.length - 1 : historyIndex - 1;
|
|
4524
|
+
for (var i = start; i >= 0; i--) {
|
|
4525
|
+
if (!prefix || commandHistory[i].indexOf(prefix) === 0) {
|
|
4526
|
+
historyIndex = i;
|
|
4527
|
+
replaceLine(commandHistory[i]);
|
|
4528
|
+
return;
|
|
4529
|
+
}
|
|
4530
|
+
}
|
|
4531
|
+
}
|
|
4532
|
+
function historyDown() {
|
|
4533
|
+
if (historyIndex === -1) return;
|
|
4534
|
+
var prefix = savedInput;
|
|
4535
|
+
for (var i = historyIndex + 1; i < commandHistory.length; i++) {
|
|
4536
|
+
if (!prefix || commandHistory[i].indexOf(prefix) === 0) {
|
|
4537
|
+
historyIndex = i;
|
|
4538
|
+
replaceLine(commandHistory[i]);
|
|
4539
|
+
return;
|
|
4540
|
+
}
|
|
4541
|
+
}
|
|
4542
|
+
historyIndex = -1;
|
|
4543
|
+
replaceLine(savedInput);
|
|
4544
|
+
}
|
|
4545
|
+
function tabComplete() {
|
|
4546
|
+
var input = inputBuffer.slice(0, cursorPos);
|
|
4547
|
+
var parts = input.split(/\s+/);
|
|
4548
|
+
var partial = parts[parts.length - 1] || "";
|
|
4549
|
+
var candidates;
|
|
4550
|
+
if (parts.length === 1 && !partial) {
|
|
4551
|
+
try {
|
|
4552
|
+
var cwdEntries = fs.readdirSync(cwd);
|
|
4553
|
+
candidates = [];
|
|
4554
|
+
for (var ce = 0; ce < cwdEntries.length; ce++) {
|
|
4555
|
+
var ent = cwdEntries[ce];
|
|
4556
|
+
if (typeof ent !== "string") continue;
|
|
4557
|
+
var ep = path.join(cwd, ent);
|
|
4558
|
+
try {
|
|
4559
|
+
var est = fs.statSync(ep);
|
|
4560
|
+
candidates.push(est.isDirectory() ? ent + "/" : ent);
|
|
4561
|
+
} catch (e4) {
|
|
4562
|
+
candidates.push(ent);
|
|
4563
|
+
}
|
|
4564
|
+
}
|
|
4565
|
+
} catch (e5) {
|
|
4566
|
+
candidates = [];
|
|
4567
|
+
}
|
|
4568
|
+
} else if (parts.length === 1) {
|
|
4569
|
+
var cmdNames = Object.keys(commands).concat(["cd", "export", "exit", "set", "unset", "read", "source"]);
|
|
4570
|
+
candidates = cmdNames.filter(function(c2) {
|
|
4571
|
+
return c2.indexOf(partial) === 0;
|
|
4572
|
+
});
|
|
4573
|
+
} else {
|
|
4574
|
+
var lastSlash = partial.lastIndexOf("/");
|
|
4575
|
+
var dir = lastSlash >= 0 ? partial.slice(0, lastSlash + 1) : ".";
|
|
4576
|
+
var prefix = lastSlash >= 0 ? partial.slice(lastSlash + 1) : partial;
|
|
4577
|
+
var dirPath = dir.startsWith("/") ? dir : path.join(cwd, dir);
|
|
4578
|
+
try {
|
|
4579
|
+
var entries = fs.readdirSync(dirPath);
|
|
4580
|
+
candidates = [];
|
|
4581
|
+
for (var ei = 0; ei < entries.length; ei++) {
|
|
4582
|
+
if (typeof entries[ei] === "string" && entries[ei].indexOf(prefix) === 0) {
|
|
4583
|
+
candidates.push((lastSlash >= 0 ? partial.slice(0, lastSlash + 1) : "") + entries[ei]);
|
|
4584
|
+
}
|
|
4585
|
+
}
|
|
4586
|
+
} catch (e) {
|
|
4587
|
+
candidates = [];
|
|
4588
|
+
}
|
|
4589
|
+
}
|
|
4590
|
+
if (candidates.length === 1) {
|
|
4591
|
+
var completed = candidates[0];
|
|
4592
|
+
if (parts.length > 1 && !completed.endsWith("/")) {
|
|
4593
|
+
var checkPath = completed.startsWith("/") ? completed : path.join(cwd, completed);
|
|
4594
|
+
try {
|
|
4595
|
+
var st = fs.statSync(checkPath);
|
|
4596
|
+
if (st.isDirectory()) completed += "/";
|
|
4597
|
+
} catch (e2) {
|
|
4598
|
+
}
|
|
4599
|
+
}
|
|
4600
|
+
var completion = completed.slice(partial.length);
|
|
4601
|
+
inputBuffer = inputBuffer.slice(0, cursorPos) + completion + inputBuffer.slice(cursorPos);
|
|
4602
|
+
cursorPos += completion.length;
|
|
4603
|
+
process.stdout.write(completion);
|
|
4604
|
+
} else if (candidates.length > 1) {
|
|
4605
|
+
var displayCandidates = [];
|
|
4606
|
+
for (var di = 0; di < candidates.length; di++) {
|
|
4607
|
+
var c = candidates[di];
|
|
4608
|
+
if (parts.length > 1 && !c.endsWith("/")) {
|
|
4609
|
+
var cp2 = c.startsWith("/") ? c : path.join(cwd, c);
|
|
4610
|
+
try {
|
|
4611
|
+
var dst = fs.statSync(cp2);
|
|
4612
|
+
if (dst.isDirectory()) c += "/";
|
|
4613
|
+
} catch (e3) {
|
|
4614
|
+
}
|
|
4615
|
+
}
|
|
4616
|
+
displayCandidates.push(c);
|
|
4617
|
+
}
|
|
4618
|
+
displayCandidates.sort();
|
|
4619
|
+
completionMenu = {
|
|
4620
|
+
candidates: displayCandidates,
|
|
4621
|
+
selectedIndex: 0,
|
|
4622
|
+
originalInput: inputBuffer,
|
|
4623
|
+
originalCursorPos: cursorPos,
|
|
4624
|
+
partial
|
|
4625
|
+
};
|
|
4626
|
+
menuSelect(0);
|
|
4627
|
+
renderMenu();
|
|
4628
|
+
}
|
|
4629
|
+
}
|
|
4630
|
+
function menuSelect(index) {
|
|
4631
|
+
if (!completionMenu) return;
|
|
4632
|
+
completionMenu.selectedIndex = index;
|
|
4633
|
+
var candidate = completionMenu.candidates[index];
|
|
4634
|
+
var partial = completionMenu.partial;
|
|
4635
|
+
var origInput = completionMenu.originalInput;
|
|
4636
|
+
var origCursor = completionMenu.originalCursorPos;
|
|
4637
|
+
var before = origInput.slice(0, origCursor - partial.length);
|
|
4638
|
+
var after = origInput.slice(origCursor);
|
|
4639
|
+
replaceLine(before + candidate + after);
|
|
4640
|
+
}
|
|
4641
|
+
function renderMenu() {
|
|
4642
|
+
if (!completionMenu) return;
|
|
4643
|
+
var cands = completionMenu.candidates;
|
|
4644
|
+
var sel = completionMenu.selectedIndex;
|
|
4645
|
+
var maxShow = 10;
|
|
4646
|
+
var start = Math.max(0, Math.min(sel - Math.floor(maxShow / 2), cands.length - maxShow));
|
|
4647
|
+
var end = Math.min(cands.length, start + maxShow);
|
|
4648
|
+
var line = "";
|
|
4649
|
+
for (var ri = start; ri < end; ri++) {
|
|
4650
|
+
if (ri > start) line += " ";
|
|
4651
|
+
if (ri === sel) {
|
|
4652
|
+
line += "\x1B[7m" + cands[ri] + "\x1B[0m";
|
|
4653
|
+
} else {
|
|
4654
|
+
line += cands[ri];
|
|
4655
|
+
}
|
|
4656
|
+
}
|
|
4657
|
+
if (start > 0) line = "... " + line;
|
|
4658
|
+
if (end < cands.length) line += " ... +" + (cands.length - end) + " more";
|
|
4659
|
+
process.stdout.write("\x1B[s");
|
|
4660
|
+
process.stdout.write("\n" + line + "\x1B[K");
|
|
4661
|
+
process.stdout.write("\x1B[u");
|
|
4662
|
+
menuVisible = true;
|
|
4663
|
+
}
|
|
4664
|
+
function exitMenu() {
|
|
4665
|
+
if (!completionMenu) return;
|
|
4666
|
+
if (menuVisible) {
|
|
4667
|
+
process.stdout.write("\x1B[s\n\x1B[J\x1B[u");
|
|
4668
|
+
menuVisible = false;
|
|
4669
|
+
}
|
|
4670
|
+
completionMenu = null;
|
|
4671
|
+
}
|
|
4672
|
+
function cancelMenu() {
|
|
4673
|
+
if (!completionMenu) return;
|
|
4674
|
+
replaceLine(completionMenu.originalInput);
|
|
4675
|
+
cursorPos = completionMenu.originalCursorPos;
|
|
4676
|
+
exitMenu();
|
|
4677
|
+
}
|
|
4678
|
+
if (process.stdin.setRawMode) process.stdin.setRawMode(true);
|
|
4679
|
+
process.stdin._keepAlive = true;
|
|
4680
|
+
process.stdin.resume();
|
|
4681
|
+
process.stdin.setEncoding("utf8");
|
|
4682
|
+
var _keepAlive = setInterval(function() {
|
|
4683
|
+
}, 500);
|
|
4684
|
+
var SILENT_PREFIX = "\0@machina/silent\0";
|
|
4685
|
+
process.stdin.on("data", function(chunk) {
|
|
4686
|
+
if (typeof chunk === "string" && chunk.length >= SILENT_PREFIX.length && chunk.indexOf(SILENT_PREFIX) === 0) {
|
|
4687
|
+
var savedAsyncRunning = asyncRunning;
|
|
4688
|
+
var savedExitCode = exitCode;
|
|
4689
|
+
try {
|
|
4690
|
+
var silentBody = chunk.slice(SILENT_PREFIX.length);
|
|
4691
|
+
var silentLines = silentBody.split("\n");
|
|
4692
|
+
for (var sli = 0; sli < silentLines.length; sli++) {
|
|
4693
|
+
var sl = silentLines[sli];
|
|
4694
|
+
if (sl.length > 0) {
|
|
4695
|
+
try {
|
|
4696
|
+
executeCommand(sl, false);
|
|
4697
|
+
} catch (e) {
|
|
4698
|
+
}
|
|
4699
|
+
}
|
|
4700
|
+
}
|
|
4701
|
+
} finally {
|
|
4702
|
+
asyncRunning = savedAsyncRunning;
|
|
4703
|
+
exitCode = savedExitCode;
|
|
4704
|
+
}
|
|
4705
|
+
return;
|
|
4706
|
+
}
|
|
4707
|
+
for (var ci = 0; ci < chunk.length; ci++) {
|
|
4708
|
+
var ch = chunk[ci];
|
|
4709
|
+
if (completionMenu) {
|
|
4710
|
+
if (ch === "\x1B" && ci + 1 < chunk.length && chunk[ci + 1] === "[") {
|
|
4711
|
+
ci += 2;
|
|
4712
|
+
while (ci < chunk.length && !/[A-Za-z~]/.test(chunk[ci])) {
|
|
4713
|
+
ci++;
|
|
4714
|
+
}
|
|
4715
|
+
var mseq = chunk[ci];
|
|
4716
|
+
if (mseq === "B" || mseq === "C") {
|
|
4717
|
+
menuSelect((completionMenu.selectedIndex + 1) % completionMenu.candidates.length);
|
|
4718
|
+
renderMenu();
|
|
4719
|
+
} else if (mseq === "A" || mseq === "D") {
|
|
4720
|
+
var mlen = completionMenu.candidates.length;
|
|
4721
|
+
menuSelect((completionMenu.selectedIndex - 1 + mlen) % mlen);
|
|
4722
|
+
renderMenu();
|
|
4723
|
+
}
|
|
4724
|
+
continue;
|
|
4725
|
+
}
|
|
4726
|
+
if (ch === " ") {
|
|
4727
|
+
menuSelect((completionMenu.selectedIndex + 1) % completionMenu.candidates.length);
|
|
4728
|
+
renderMenu();
|
|
4729
|
+
continue;
|
|
4730
|
+
}
|
|
4731
|
+
if (ch === "\r" || ch === "\n") {
|
|
4732
|
+
exitMenu();
|
|
4733
|
+
continue;
|
|
4734
|
+
}
|
|
4735
|
+
if (ch === "\x1B") {
|
|
4736
|
+
cancelMenu();
|
|
4737
|
+
continue;
|
|
4738
|
+
}
|
|
4739
|
+
if (ch === "") {
|
|
4740
|
+
cancelMenu();
|
|
4741
|
+
} else {
|
|
4742
|
+
exitMenu();
|
|
4743
|
+
}
|
|
4744
|
+
}
|
|
4745
|
+
if (ch === "\x1B" && ci + 1 < chunk.length && chunk[ci + 1] === "[") {
|
|
4746
|
+
ci += 2;
|
|
4747
|
+
var param = "";
|
|
4748
|
+
while (ci < chunk.length && !/[A-Za-z~]/.test(chunk[ci])) {
|
|
4749
|
+
param += chunk[ci];
|
|
4750
|
+
ci++;
|
|
4751
|
+
}
|
|
4752
|
+
var seq2 = chunk[ci];
|
|
4753
|
+
var hasAlt = param === "1;3";
|
|
4754
|
+
if (seq2 === "D" && hasAlt) {
|
|
4755
|
+
wordLeft();
|
|
4756
|
+
} else if (seq2 === "C" && hasAlt) {
|
|
4757
|
+
wordRight();
|
|
4758
|
+
} else if (seq2 === "D" && cursorPos > 0) {
|
|
4759
|
+
var lw = charWidth(inputBuffer[cursorPos - 1]);
|
|
4760
|
+
cursorPos--;
|
|
4761
|
+
process.stdout.write("\x1B[" + lw + "D");
|
|
4762
|
+
} else if (seq2 === "C" && cursorPos < inputBuffer.length) {
|
|
4763
|
+
var rw = charWidth(inputBuffer[cursorPos]);
|
|
4764
|
+
cursorPos++;
|
|
4765
|
+
process.stdout.write("\x1B[" + rw + "C");
|
|
4766
|
+
} else if (seq2 === "A") {
|
|
4767
|
+
historyUp();
|
|
4768
|
+
} else if (seq2 === "B") {
|
|
4769
|
+
historyDown();
|
|
4770
|
+
}
|
|
4771
|
+
continue;
|
|
4772
|
+
}
|
|
4773
|
+
if (ch === "\x1B" && ci + 1 < chunk.length && chunk[ci + 1] === "\x7F") {
|
|
4774
|
+
ci++;
|
|
4775
|
+
deleteWordBack();
|
|
4776
|
+
continue;
|
|
4777
|
+
}
|
|
4778
|
+
if (ch === "\r" || ch === "\n") {
|
|
4779
|
+
process.stdout.write("\n");
|
|
4780
|
+
if (inputBuffer.trim()) {
|
|
4781
|
+
commandHistory.push(inputBuffer);
|
|
4782
|
+
}
|
|
4783
|
+
historyIndex = -1;
|
|
4784
|
+
savedInput = "";
|
|
4785
|
+
inputBuffer += "\n";
|
|
4786
|
+
cursorPos = 0;
|
|
4787
|
+
} else if (ch === "\x7F" || ch === "\b") {
|
|
4788
|
+
if (cursorPos > 0) {
|
|
4789
|
+
var deleted = inputBuffer[cursorPos - 1];
|
|
4790
|
+
var dw = charWidth(deleted);
|
|
4791
|
+
inputBuffer = inputBuffer.slice(0, cursorPos - 1) + inputBuffer.slice(cursorPos);
|
|
4792
|
+
cursorPos--;
|
|
4793
|
+
var t = inputBuffer.slice(cursorPos);
|
|
4794
|
+
var tw = strWidth(t);
|
|
4795
|
+
process.stdout.write("\x1B[" + dw + "D" + t + " ".repeat(dw) + "\x1B[" + (tw + dw) + "D");
|
|
4796
|
+
}
|
|
4797
|
+
} else if (ch === "") {
|
|
4798
|
+
if (inputBuffer.length === 0) {
|
|
4799
|
+
process.stdout.write("\n");
|
|
4800
|
+
process.exit(0);
|
|
4801
|
+
}
|
|
4802
|
+
} else if (ch === "") {
|
|
4803
|
+
inputBuffer = "";
|
|
4804
|
+
cursorPos = 0;
|
|
4805
|
+
process.stdout.write("^C\n");
|
|
4806
|
+
prompt();
|
|
4807
|
+
} else if (ch === "") {
|
|
4808
|
+
if (cursorPos > 0) {
|
|
4809
|
+
process.stdout.write("\x1B[" + strWidth(inputBuffer.slice(0, cursorPos)) + "D");
|
|
4810
|
+
cursorPos = 0;
|
|
4811
|
+
}
|
|
4812
|
+
} else if (ch === "") {
|
|
4813
|
+
if (cursorPos < inputBuffer.length) {
|
|
4814
|
+
process.stdout.write("\x1B[" + strWidth(inputBuffer.slice(cursorPos)) + "C");
|
|
4815
|
+
cursorPos = inputBuffer.length;
|
|
4816
|
+
}
|
|
4817
|
+
} else if (ch === "") {
|
|
4818
|
+
var uDel = inputBuffer.slice(0, cursorPos);
|
|
4819
|
+
if (uDel.length > 0) {
|
|
4820
|
+
var uDelW = strWidth(uDel);
|
|
4821
|
+
inputBuffer = inputBuffer.slice(cursorPos);
|
|
4822
|
+
process.stdout.write("\x1B[" + uDelW + "D" + inputBuffer + " ".repeat(uDelW) + "\x1B[" + (strWidth(inputBuffer) + uDelW) + "D");
|
|
4823
|
+
cursorPos = 0;
|
|
4824
|
+
}
|
|
4825
|
+
} else if (ch === "\v") {
|
|
4826
|
+
var kDel = inputBuffer.slice(cursorPos);
|
|
4827
|
+
if (kDel.length > 0) {
|
|
4828
|
+
inputBuffer = inputBuffer.slice(0, cursorPos);
|
|
4829
|
+
process.stdout.write(" ".repeat(strWidth(kDel)) + "\x1B[" + strWidth(kDel) + "D");
|
|
4830
|
+
}
|
|
4831
|
+
} else if (ch === "") {
|
|
4832
|
+
deleteWordBack();
|
|
4833
|
+
} else if (ch === " ") {
|
|
4834
|
+
tabComplete();
|
|
4835
|
+
} else if (ch === "\x1B") {
|
|
4836
|
+
} else {
|
|
4837
|
+
inputBuffer = inputBuffer.slice(0, cursorPos) + ch + inputBuffer.slice(cursorPos);
|
|
4838
|
+
cursorPos++;
|
|
4839
|
+
var t2 = inputBuffer.slice(cursorPos);
|
|
4840
|
+
process.stdout.write(ch + t2);
|
|
4841
|
+
var tw2 = strWidth(t2);
|
|
4842
|
+
if (tw2 > 0) {
|
|
4843
|
+
process.stdout.write("\x1B[" + tw2 + "D");
|
|
4844
|
+
}
|
|
4845
|
+
}
|
|
4846
|
+
}
|
|
4847
|
+
processInputBuffer();
|
|
4848
|
+
});
|
|
4849
|
+
function processInputBuffer() {
|
|
4850
|
+
if (asyncRunning) return;
|
|
4851
|
+
var newlineIdx;
|
|
4852
|
+
while (!asyncRunning && (newlineIdx = inputBuffer.indexOf("\n")) !== -1) {
|
|
4853
|
+
var line = inputBuffer.substring(0, newlineIdx);
|
|
4854
|
+
inputBuffer = inputBuffer.substring(newlineIdx + 1);
|
|
4855
|
+
var bg = parseBg.parseTrailingBg(line);
|
|
4856
|
+
asyncRunning = true;
|
|
4857
|
+
var result = executeCommand(bg.command, bg.bgMode);
|
|
4858
|
+
if (result === -1) {
|
|
4859
|
+
} else {
|
|
4860
|
+
asyncRunning = false;
|
|
4861
|
+
prompt();
|
|
4862
|
+
}
|
|
4863
|
+
}
|
|
4864
|
+
}
|
|
4865
|
+
var rcFiles = rcLoaderModule.listRcFiles({ env, fs, path });
|
|
4866
|
+
for (rcIdx = 0; rcIdx < rcFiles.length; rcIdx++) {
|
|
4867
|
+
builtinSource([rcFiles[rcIdx]]);
|
|
4868
|
+
}
|
|
4869
|
+
var rcIdx;
|
|
4870
|
+
prompt();
|
|
4871
|
+
}
|
|
4872
|
+
});
|
|
4873
|
+
|
|
4874
|
+
// packages/@machina.at/xsh/src/main.ts
|
|
4875
|
+
require_sh();
|