Skip to content

Commit f250f86

Browse files
committed
feat(phoenix): Add --dump and --file options to sed
1 parent 4067c82 commit f250f86

File tree

1 file changed

+38
-7
lines changed
  • packages/phoenix/src/puter-shell/coreutils

1 file changed

+38
-7
lines changed

packages/phoenix/src/puter-shell/coreutils/sed.js

+38-7
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,27 @@ export default {
3232
args: {
3333
$: 'simple-parser',
3434
allowPositionals: true,
35+
tokens: true,
3536
options: {
37+
dump: {
38+
description: 'Dump a representation of the parsed script, for debugging.',
39+
type: 'boolean',
40+
default: false,
41+
},
3642
expression: {
3743
description: 'Specify an additional script to execute. May be specified multiple times.',
3844
type: 'string',
3945
short: 'e',
4046
multiple: true,
4147
default: [],
4248
},
49+
file: {
50+
description: 'Specify a script file to execute. May be specified multiple times.',
51+
type: 'string',
52+
short: 'f',
53+
multiple: true,
54+
default: [],
55+
},
4356
quiet: {
4457
description: 'Suppress default printing of selected lines.',
4558
type: 'boolean',
@@ -50,7 +63,7 @@ export default {
5063
},
5164
execute: async ctx => {
5265
const { out, err } = ctx.externs;
53-
const { positionals, values } = ctx.locals;
66+
const { positionals, values, tokens } = ctx.locals;
5467

5568
if (positionals.length < 1) {
5669
await err.write('sed: No inputs given\n');
@@ -62,16 +75,34 @@ export default {
6275
// made, if the previous addition (if any) was from a -e option, a <newline> shall be inserted before the new
6376
// addition. The resulting script shall have the same properties as the script operand, described in the
6477
// OPERANDS section."
65-
// TODO: -f loads scripts from a file
6678
let scriptString = '';
67-
if (values.expression.length > 0) {
68-
scriptString = values.expression.join('\n');
79+
if (values.expression.length + values.file.length > 0) {
80+
// These have to be in order, and -e and -f could be intermixed, so iterate the tokens
81+
for (let token of tokens) {
82+
if (token.kind !== 'option') continue;
83+
if (token.name === 'expression') {
84+
scriptString += token.value + '\n';
85+
continue;
86+
}
87+
if (token.name === 'file') {
88+
for await (const line of fileLines(ctx, token.value)) {
89+
scriptString += line;
90+
}
91+
continue;
92+
}
93+
}
6994
} else {
7095
scriptString = positionals.shift();
7196
}
7297

73-
const script = parseScript(scriptString);
74-
await out.write(script.dump());
75-
await script.run(ctx);
98+
try {
99+
const script = parseScript(scriptString, values);
100+
if (values.dump)
101+
await out.write(script.dump());
102+
await script.run(ctx);
103+
} catch (e) {
104+
console.error(e);
105+
await err.write(`sed: ${e.message}\n`);
106+
}
76107
}
77108
};

0 commit comments

Comments
 (0)