@jalaxy33/pi-permission 0.4.0 → 0.5.1
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/README.md +37 -62
- package/docs/configuration.md +24 -10
- package/index.ts +2 -0
- package/package.json +1 -1
- package/rules/index.json +11 -6
- package/rules/packs/dynamic-exec.json +111 -0
- package/src/argv.ts +11 -1
- package/src/config.ts +5 -2
- package/src/decide.ts +177 -10
- package/src/gate.ts +44 -19
- package/src/match.ts +2 -1
- package/src/ops.ts +47 -10
- package/src/paths.ts +24 -6
- package/src/prefixes.ts +87 -0
- package/src/program.ts +19 -0
- package/src/rulesets.ts +17 -2
- package/src/scripts.ts +121 -0
- package/src/tokenize.ts +410 -34
- package/src/unlayer.ts +48 -0
package/src/tokenize.ts
CHANGED
|
@@ -2,9 +2,14 @@
|
|
|
2
2
|
* Quote-aware command splitter.
|
|
3
3
|
*
|
|
4
4
|
* Turns a raw bash command string into simple commands (argv lists) and
|
|
5
|
-
* throws away text that is not a command:
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* throws away text that is not a command: heredoc bodies and comments are
|
|
6
|
+
* skipped, and command and process substitutions are tokenized as their own
|
|
7
|
+
* commands. Text the splitter cannot resolve (an unterminated quote, `$(` or
|
|
8
|
+
* heredoc) is reported as a hazard, so the gate can refuse to guess.
|
|
9
|
+
*
|
|
10
|
+
* Every word carries what it lost on the way here: an expansion, a quote, an
|
|
11
|
+
* escape or a glob. A program name that came from one of those is not a name,
|
|
12
|
+
* and the gate treats it as unreadable rather than matching on a guess.
|
|
8
13
|
*/
|
|
9
14
|
|
|
10
15
|
export interface Redirect {
|
|
@@ -12,9 +17,33 @@ export interface Redirect {
|
|
|
12
17
|
target: string;
|
|
13
18
|
}
|
|
14
19
|
|
|
20
|
+
/** What a word went through before it became text. */
|
|
21
|
+
export interface WordMeta {
|
|
22
|
+
/** `$(...)`, backticks, process substitution, `$VAR`, `$((...))`, `$'...'`. */
|
|
23
|
+
expansion: boolean;
|
|
24
|
+
quote: boolean;
|
|
25
|
+
escape: boolean;
|
|
26
|
+
/** Unquoted `*`, `?` or `[`. */
|
|
27
|
+
glob: boolean;
|
|
28
|
+
}
|
|
29
|
+
|
|
15
30
|
export interface SimpleCommand {
|
|
16
31
|
argv: string[];
|
|
32
|
+
/** Parallel to `argv`. */
|
|
33
|
+
words: WordMeta[];
|
|
17
34
|
redirects: Redirect[];
|
|
35
|
+
/** Bodies of the heredocs this command reads (`bash <<EOF`). */
|
|
36
|
+
heredocs?: string[];
|
|
37
|
+
/** Text fed on stdin by a here string (`bash <<< "..."`). */
|
|
38
|
+
stdinText?: string;
|
|
39
|
+
/** Stdin comes from a pipe, so it is not visible in the command text. */
|
|
40
|
+
stdinPiped?: boolean;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface Tokenized {
|
|
44
|
+
commands: SimpleCommand[];
|
|
45
|
+
/** Parts of the command text the splitter could not resolve. */
|
|
46
|
+
hazards: string[];
|
|
18
47
|
}
|
|
19
48
|
|
|
20
49
|
const BLANK = /\s/;
|
|
@@ -23,78 +52,296 @@ function isSeparator(ch: string): boolean {
|
|
|
23
52
|
return ch === ";" || ch === "&" || ch === "|" || ch === "\n";
|
|
24
53
|
}
|
|
25
54
|
|
|
26
|
-
/**
|
|
27
|
-
function
|
|
28
|
-
|
|
55
|
+
/** Subshell and group operators end a command without starting a new one. */
|
|
56
|
+
function isGrouping(ch: string): boolean {
|
|
57
|
+
return ch === "(" || ch === ")";
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function blankMeta(): WordMeta {
|
|
61
|
+
return { expansion: false, quote: false, escape: false, glob: false };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
interface Scanned {
|
|
65
|
+
inner: string;
|
|
66
|
+
next: number;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Reads `(...)` open at `open`; nested parentheses are balanced. */
|
|
70
|
+
function scanParen(input: string, open: number): Scanned | undefined {
|
|
71
|
+
let depth = 1;
|
|
72
|
+
let i = open + 1;
|
|
73
|
+
while (i < input.length) {
|
|
74
|
+
const ch = input[i] as string;
|
|
75
|
+
if (ch === "\\") {
|
|
76
|
+
i += 2;
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
if (ch === "'" || ch === '"') {
|
|
80
|
+
const quoted = readQuoted(input, i);
|
|
81
|
+
i = quoted.next;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
if (ch === "(") depth++;
|
|
85
|
+
else if (ch === ")") {
|
|
86
|
+
depth--;
|
|
87
|
+
if (depth === 0) return { inner: input.slice(open + 1, i), next: i + 1 };
|
|
88
|
+
}
|
|
89
|
+
i++;
|
|
90
|
+
}
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Reads a backtick pair open at `open`. */
|
|
95
|
+
function scanBacktick(input: string, open: number): Scanned | undefined {
|
|
96
|
+
let i = open + 1;
|
|
97
|
+
while (i < input.length) {
|
|
98
|
+
const ch = input[i] as string;
|
|
99
|
+
if (ch === "\\") {
|
|
100
|
+
i += 2;
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (ch === "`") return { inner: input.slice(open + 1, i), next: i + 1 };
|
|
104
|
+
i++;
|
|
105
|
+
}
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
interface Quoted {
|
|
110
|
+
text: string;
|
|
111
|
+
next: number;
|
|
112
|
+
/** Substitutions the shell still runs inside the quotes. */
|
|
113
|
+
subs: string[];
|
|
114
|
+
/** The quoted text holds an expansion, so its value is not literal. */
|
|
115
|
+
expansion: boolean;
|
|
116
|
+
unterminated: boolean;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Reads a quoted string starting at `start`. Double quotes honour backslash
|
|
121
|
+
* escapes and keep running `$(...)` and `` `...` ``; single quotes keep
|
|
122
|
+
* everything literally, the way the shell does.
|
|
123
|
+
*/
|
|
124
|
+
function readQuoted(input: string, start: number): Quoted {
|
|
125
|
+
const quote = input[start] as string;
|
|
126
|
+
const subs: string[] = [];
|
|
127
|
+
let expansion = false;
|
|
29
128
|
let text = "";
|
|
30
129
|
let i = start + 1;
|
|
130
|
+
|
|
31
131
|
while (i < input.length && input[i] !== quote) {
|
|
32
|
-
|
|
33
|
-
|
|
132
|
+
const ch = input[i] as string;
|
|
133
|
+
if (quote === '"') {
|
|
134
|
+
if (ch === "\\") {
|
|
135
|
+
// Inside double quotes bash only removes the backslash before these.
|
|
136
|
+
// Everywhere else it stays, which is what keeps `"C:\\repo"` intact.
|
|
137
|
+
const next = input[i + 1];
|
|
138
|
+
if (next !== undefined && "$`\"\\\n".includes(next)) {
|
|
139
|
+
text += next;
|
|
140
|
+
i += 2;
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
text += ch;
|
|
144
|
+
i++;
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
if (ch === "`") {
|
|
148
|
+
const found = scanBacktick(input, i);
|
|
149
|
+
if (!found) return { text, next: input.length, subs, expansion: true, unterminated: true };
|
|
150
|
+
subs.push(found.inner);
|
|
151
|
+
expansion = true;
|
|
152
|
+
i = found.next;
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
if (ch === "$" && input[i + 1] === "(") {
|
|
156
|
+
// `$((...))` is arithmetic: a value, not a command.
|
|
157
|
+
if (input[i + 2] === "(") {
|
|
158
|
+
const arithmetic = scanArithmetic(input, i + 1);
|
|
159
|
+
if (arithmetic === undefined) {
|
|
160
|
+
return { text, next: input.length, subs, expansion: true, unterminated: true };
|
|
161
|
+
}
|
|
162
|
+
expansion = true;
|
|
163
|
+
i = arithmetic;
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
const found = scanParen(input, i + 1);
|
|
167
|
+
if (!found) return { text, next: input.length, subs, expansion: true, unterminated: true };
|
|
168
|
+
subs.push(found.inner);
|
|
169
|
+
expansion = true;
|
|
170
|
+
i = found.next;
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
if (ch === "$") expansion = true;
|
|
174
|
+
}
|
|
175
|
+
text += ch;
|
|
176
|
+
i++;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (i >= input.length) return { text, next: input.length, subs, expansion: true, unterminated: true };
|
|
180
|
+
return { text, next: i + 1, subs, expansion, unterminated: false };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const ANSI_C: Record<string, string> = {
|
|
184
|
+
n: "\n",
|
|
185
|
+
t: "\t",
|
|
186
|
+
r: "\r",
|
|
187
|
+
a: "\u0007",
|
|
188
|
+
b: "\b",
|
|
189
|
+
f: "\f",
|
|
190
|
+
v: "\v",
|
|
191
|
+
e: "\u001b",
|
|
192
|
+
E: "\u001b",
|
|
193
|
+
"\\": "\\",
|
|
194
|
+
"'": "'",
|
|
195
|
+
'"': '"',
|
|
196
|
+
"?": "?",
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
/** Reads `$'...'` opened at the quote; the value is what bash would produce. */
|
|
200
|
+
function readAnsiC(input: string, open: number): { text: string; next: number } | undefined {
|
|
201
|
+
let text = "";
|
|
202
|
+
let i = open + 1;
|
|
203
|
+
while (i < input.length) {
|
|
204
|
+
const ch = input[i] as string;
|
|
205
|
+
if (ch === "'") return { text, next: i + 1 };
|
|
206
|
+
if (ch !== "\\") {
|
|
207
|
+
text += ch;
|
|
208
|
+
i++;
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
const code = input[i + 1] ?? "";
|
|
212
|
+
const simple = ANSI_C[code];
|
|
213
|
+
if (simple !== undefined) {
|
|
214
|
+
text += simple;
|
|
34
215
|
i += 2;
|
|
35
216
|
continue;
|
|
36
217
|
}
|
|
37
|
-
|
|
218
|
+
const hex = /^x([0-9A-Fa-f]{1,2})/.exec(input.slice(i + 1));
|
|
219
|
+
if (hex) {
|
|
220
|
+
text += String.fromCharCode(Number.parseInt(hex[1] as string, 16));
|
|
221
|
+
i += 1 + hex[0].length;
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
const octal = /^([0-7]{1,3})/.exec(input.slice(i + 1));
|
|
225
|
+
if (octal) {
|
|
226
|
+
text += String.fromCharCode(Number.parseInt(octal[1] as string, 8));
|
|
227
|
+
i += 1 + octal[0].length;
|
|
228
|
+
continue;
|
|
229
|
+
}
|
|
230
|
+
text += code;
|
|
231
|
+
i += 2;
|
|
232
|
+
}
|
|
233
|
+
return undefined;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Index after the closing `))` of an arithmetic expansion. `open` points at the
|
|
238
|
+
* first of the two opening parentheses.
|
|
239
|
+
*/
|
|
240
|
+
function scanArithmetic(input: string, open: number): number | undefined {
|
|
241
|
+
let depth = 2;
|
|
242
|
+
let i = open + 2;
|
|
243
|
+
while (i < input.length) {
|
|
244
|
+
const ch = input[i] as string;
|
|
245
|
+
if (ch === "(") depth++;
|
|
246
|
+
else if (ch === ")") {
|
|
247
|
+
depth--;
|
|
248
|
+
if (depth === 0) return i + 1;
|
|
249
|
+
}
|
|
38
250
|
i++;
|
|
39
251
|
}
|
|
40
|
-
return
|
|
252
|
+
return undefined;
|
|
41
253
|
}
|
|
42
254
|
|
|
43
|
-
export function
|
|
255
|
+
export function tokenizeDetailed(input: string): Tokenized {
|
|
44
256
|
const commands: SimpleCommand[] = [];
|
|
257
|
+
const hazards: string[] = [];
|
|
45
258
|
const subs: string[] = [];
|
|
46
259
|
let argv: string[] = [];
|
|
260
|
+
let words: WordMeta[] = [];
|
|
47
261
|
let redirects: Redirect[] = [];
|
|
48
262
|
let word = "";
|
|
263
|
+
let meta = blankMeta();
|
|
49
264
|
let inWord = false;
|
|
50
|
-
let
|
|
265
|
+
let piped = false;
|
|
266
|
+
let pendingStdin: string | undefined;
|
|
267
|
+
let heredocs: Array<{ delimiter: string; owner?: SimpleCommand }> = [];
|
|
51
268
|
|
|
52
269
|
const flushWord = () => {
|
|
53
270
|
if (inWord) {
|
|
271
|
+
// `[ -f x ]` starts with the test builtin, not a brace expression.
|
|
272
|
+
if (word === "[" || word === "[[") meta.glob = false;
|
|
54
273
|
argv.push(word);
|
|
274
|
+
words.push(meta);
|
|
275
|
+
word = "";
|
|
276
|
+
meta = blankMeta();
|
|
277
|
+
inWord = false;
|
|
278
|
+
}
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
// `2>file` names a descriptor; that digit is not an argument.
|
|
282
|
+
const dropFileDescriptor = () => {
|
|
283
|
+
if (inWord && /^\d+$/.test(word)) {
|
|
55
284
|
word = "";
|
|
285
|
+
meta = blankMeta();
|
|
56
286
|
inWord = false;
|
|
57
287
|
}
|
|
58
288
|
};
|
|
59
289
|
|
|
60
290
|
const endCommand = () => {
|
|
61
291
|
flushWord();
|
|
62
|
-
if (argv.length > 0)
|
|
292
|
+
if (argv.length > 0) {
|
|
293
|
+
const command: SimpleCommand = { argv, words, redirects };
|
|
294
|
+
if (piped) command.stdinPiped = true;
|
|
295
|
+
if (pendingStdin !== undefined) command.stdinText = pendingStdin;
|
|
296
|
+
pendingStdin = undefined;
|
|
297
|
+
commands.push(command);
|
|
298
|
+
piped = false;
|
|
299
|
+
// A heredoc body belongs to the command that declared it, which is the
|
|
300
|
+
// one just pushed — not to whatever runs later on the same line.
|
|
301
|
+
for (const heredoc of heredocs) heredoc.owner ??= command;
|
|
302
|
+
}
|
|
63
303
|
argv = [];
|
|
304
|
+
words = [];
|
|
64
305
|
redirects = [];
|
|
65
306
|
};
|
|
66
307
|
|
|
67
|
-
// Skip to the end of a pending heredoc body
|
|
68
|
-
const skipHeredoc = (from: number, delimiter: string): number => {
|
|
308
|
+
// Skip to the end of a pending heredoc body, keeping the body text.
|
|
309
|
+
const skipHeredoc = (from: number, delimiter: string): { next: number; found: boolean; body: string } => {
|
|
69
310
|
let i = from;
|
|
70
311
|
while (i < input.length) {
|
|
71
312
|
const nl = input.indexOf("\n", i);
|
|
72
313
|
const lineEnd = nl === -1 ? input.length : nl;
|
|
73
314
|
if (input.slice(i, lineEnd).replace(/^\t+/, "").trimEnd() === delimiter) {
|
|
74
|
-
return
|
|
315
|
+
return {
|
|
316
|
+
next: nl === -1 ? input.length : nl + 1,
|
|
317
|
+
found: true,
|
|
318
|
+
body: input.slice(from, i),
|
|
319
|
+
};
|
|
75
320
|
}
|
|
76
|
-
if (nl === -1) return input.length;
|
|
321
|
+
if (nl === -1) return { next: input.length, found: false, body: input.slice(from) };
|
|
77
322
|
i = nl + 1;
|
|
78
323
|
}
|
|
79
|
-
return input.length;
|
|
324
|
+
return { next: input.length, found: false, body: input.slice(from) };
|
|
80
325
|
};
|
|
81
326
|
|
|
82
|
-
const readRedirTarget = (from: number): { target: string; next: number } => {
|
|
327
|
+
const readRedirTarget = (from: number): { target: string; next: number; subs: string[] } => {
|
|
83
328
|
let i = from;
|
|
84
329
|
while (i < input.length && (input[i] === " " || input[i] === "\t")) i++;
|
|
85
330
|
let target = "";
|
|
331
|
+
const found: string[] = [];
|
|
86
332
|
while (i < input.length && !BLANK.test(input[i] as string) && !isSeparator(input[i] as string)) {
|
|
87
333
|
const ch = input[i] as string;
|
|
88
334
|
if (ch === "'" || ch === '"') {
|
|
89
335
|
const quoted = readQuoted(input, i);
|
|
90
336
|
target += quoted.text;
|
|
337
|
+
found.push(...quoted.subs);
|
|
91
338
|
i = quoted.next;
|
|
92
339
|
continue;
|
|
93
340
|
}
|
|
94
341
|
target += ch;
|
|
95
342
|
i++;
|
|
96
343
|
}
|
|
97
|
-
return { target, next: i };
|
|
344
|
+
return { target, next: i, subs: found };
|
|
98
345
|
};
|
|
99
346
|
|
|
100
347
|
let i = 0;
|
|
@@ -102,7 +349,13 @@ export function tokenize(input: string): SimpleCommand[] {
|
|
|
102
349
|
const ch = input[i] as string;
|
|
103
350
|
|
|
104
351
|
if (ch === "\\") {
|
|
352
|
+
// `\` at end of line joins the lines and contributes no character.
|
|
353
|
+
if (input[i + 1] === "\n") {
|
|
354
|
+
i += 2;
|
|
355
|
+
continue;
|
|
356
|
+
}
|
|
105
357
|
word += input[i + 1] ?? "";
|
|
358
|
+
meta.escape = true;
|
|
106
359
|
inWord = true;
|
|
107
360
|
i += 2;
|
|
108
361
|
continue;
|
|
@@ -110,36 +363,122 @@ export function tokenize(input: string): SimpleCommand[] {
|
|
|
110
363
|
|
|
111
364
|
if (ch === "'" || ch === '"') {
|
|
112
365
|
const quoted = readQuoted(input, i);
|
|
366
|
+
if (quoted.unterminated) hazards.push("unterminated quote");
|
|
367
|
+
subs.push(...quoted.subs);
|
|
113
368
|
word += quoted.text;
|
|
369
|
+
meta.quote = true;
|
|
370
|
+
if (quoted.expansion) meta.expansion = true;
|
|
114
371
|
inWord = true;
|
|
115
372
|
i = quoted.next;
|
|
116
373
|
continue;
|
|
117
374
|
}
|
|
118
375
|
|
|
119
376
|
if (ch === "`") {
|
|
120
|
-
const
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
377
|
+
const found = scanBacktick(input, i);
|
|
378
|
+
if (!found) {
|
|
379
|
+
hazards.push("unterminated backtick");
|
|
380
|
+
i = input.length;
|
|
381
|
+
continue;
|
|
382
|
+
}
|
|
383
|
+
subs.push(found.inner);
|
|
384
|
+
meta.expansion = true;
|
|
385
|
+
i = found.next;
|
|
386
|
+
continue;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
if (ch === "$" && input[i + 1] === "'") {
|
|
390
|
+
const found = readAnsiC(input, i + 1);
|
|
391
|
+
if (!found) {
|
|
392
|
+
hazards.push("unterminated quote");
|
|
393
|
+
i = input.length;
|
|
394
|
+
continue;
|
|
395
|
+
}
|
|
396
|
+
word += found.text;
|
|
397
|
+
meta.quote = true;
|
|
398
|
+
inWord = true;
|
|
399
|
+
i = found.next;
|
|
124
400
|
continue;
|
|
125
401
|
}
|
|
126
402
|
|
|
127
403
|
if (ch === "$" && input[i + 1] === "(") {
|
|
404
|
+
// `$((...))` is arithmetic: a value, not a command.
|
|
405
|
+
if (input[i + 2] === "(") {
|
|
406
|
+
const next = scanArithmetic(input, i + 1);
|
|
407
|
+
if (next === undefined) {
|
|
408
|
+
hazards.push("unterminated arithmetic expansion");
|
|
409
|
+
i = input.length;
|
|
410
|
+
continue;
|
|
411
|
+
}
|
|
412
|
+
meta.expansion = true;
|
|
413
|
+
i = next;
|
|
414
|
+
continue;
|
|
415
|
+
}
|
|
416
|
+
const found = scanParen(input, i + 1);
|
|
417
|
+
if (!found) {
|
|
418
|
+
hazards.push("unterminated command substitution");
|
|
419
|
+
i = input.length;
|
|
420
|
+
continue;
|
|
421
|
+
}
|
|
422
|
+
subs.push(found.inner);
|
|
423
|
+
meta.expansion = true;
|
|
424
|
+
i = found.next;
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
// `<(cmd)` and `>(cmd)` run a command, so their text is checked too.
|
|
429
|
+
if ((ch === "<" || ch === ">") && input[i + 1] === "(") {
|
|
430
|
+
const found = scanParen(input, i + 1);
|
|
431
|
+
if (!found) {
|
|
432
|
+
hazards.push("unterminated process substitution");
|
|
433
|
+
i = input.length;
|
|
434
|
+
continue;
|
|
435
|
+
}
|
|
436
|
+
subs.push(found.inner);
|
|
437
|
+
meta.expansion = true;
|
|
438
|
+
i = found.next;
|
|
439
|
+
continue;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
if (ch === "$" && input[i + 1] === "{") {
|
|
443
|
+
// `${x:-a)b}` is one word; `)` and `}` inside it are data.
|
|
128
444
|
let depth = 1;
|
|
129
445
|
let j = i + 2;
|
|
130
446
|
while (j < input.length && depth > 0) {
|
|
131
|
-
const
|
|
132
|
-
if (
|
|
133
|
-
|
|
134
|
-
|
|
447
|
+
const inner = input[j] as string;
|
|
448
|
+
if (inner === "\\") {
|
|
449
|
+
j += 2;
|
|
450
|
+
continue;
|
|
451
|
+
}
|
|
452
|
+
if (inner === "{") depth++;
|
|
453
|
+
else if (inner === "}") depth--;
|
|
135
454
|
j++;
|
|
136
455
|
}
|
|
137
|
-
|
|
138
|
-
i
|
|
456
|
+
meta.expansion = true;
|
|
457
|
+
word += input.slice(i, j);
|
|
458
|
+
inWord = true;
|
|
459
|
+
i = j;
|
|
460
|
+
continue;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
if (ch === "$") {
|
|
464
|
+
// `$VAR` and `$'...'`: the word's value is decided at runtime.
|
|
465
|
+
if (/[A-Za-z_@*#?$!'-]/.test(input[i + 1] ?? "")) meta.expansion = true;
|
|
466
|
+
word += ch;
|
|
467
|
+
inWord = true;
|
|
468
|
+
i++;
|
|
139
469
|
continue;
|
|
140
470
|
}
|
|
141
471
|
|
|
142
472
|
if (ch === "<" && input[i + 1] === "<") {
|
|
473
|
+
// `<<<word` feeds `word` on stdin. It is not an operand, so it is read
|
|
474
|
+
// here and attached to the command as data.
|
|
475
|
+
if (input[i + 2] === "<") {
|
|
476
|
+
const redir = readRedirTarget(i + 3);
|
|
477
|
+
subs.push(...redir.subs);
|
|
478
|
+
pendingStdin = redir.target;
|
|
479
|
+
i = redir.next;
|
|
480
|
+
continue;
|
|
481
|
+
}
|
|
143
482
|
let j = i + 2;
|
|
144
483
|
if (input[j] === "-") j++;
|
|
145
484
|
while (input[j] === " " || input[j] === "\t") j++;
|
|
@@ -154,7 +493,7 @@ export function tokenize(input: string): SimpleCommand[] {
|
|
|
154
493
|
j++;
|
|
155
494
|
}
|
|
156
495
|
}
|
|
157
|
-
if (delimiter) heredocs.push(delimiter);
|
|
496
|
+
if (delimiter) heredocs.push({ delimiter });
|
|
158
497
|
i = j;
|
|
159
498
|
continue;
|
|
160
499
|
}
|
|
@@ -164,6 +503,7 @@ export function tokenize(input: string): SimpleCommand[] {
|
|
|
164
503
|
const append = input[i + 2] === ">";
|
|
165
504
|
flushWord();
|
|
166
505
|
const redir = readRedirTarget(i + (append ? 3 : 2));
|
|
506
|
+
subs.push(...redir.subs);
|
|
167
507
|
redirects.push({ op: append ? ">>" : ">", target: redir.target });
|
|
168
508
|
i = redir.next;
|
|
169
509
|
continue;
|
|
@@ -173,26 +513,51 @@ export function tokenize(input: string): SimpleCommand[] {
|
|
|
173
513
|
const append = input[i + 1] === ">";
|
|
174
514
|
// `>|` overrides noclobber and is the same write.
|
|
175
515
|
const clobber = input[i + 1] === "|";
|
|
516
|
+
dropFileDescriptor();
|
|
176
517
|
flushWord();
|
|
177
518
|
const redir = readRedirTarget(i + (append || clobber ? 2 : 1));
|
|
519
|
+
subs.push(...redir.subs);
|
|
178
520
|
redirects.push({ op: append ? ">>" : ">", target: redir.target });
|
|
179
521
|
i = redir.next;
|
|
180
522
|
continue;
|
|
181
523
|
}
|
|
182
524
|
|
|
183
525
|
if (ch === "<") {
|
|
526
|
+
dropFileDescriptor();
|
|
184
527
|
flushWord();
|
|
185
528
|
const redir = readRedirTarget(i + 1);
|
|
529
|
+
subs.push(...redir.subs);
|
|
186
530
|
redirects.push({ op: "<", target: redir.target });
|
|
187
531
|
i = redir.next;
|
|
188
532
|
continue;
|
|
189
533
|
}
|
|
190
534
|
|
|
535
|
+
if (isGrouping(ch)) {
|
|
536
|
+
endCommand();
|
|
537
|
+
i++;
|
|
538
|
+
continue;
|
|
539
|
+
}
|
|
540
|
+
|
|
191
541
|
if (isSeparator(ch)) {
|
|
542
|
+
const pushedTo = commands.length;
|
|
192
543
|
endCommand();
|
|
544
|
+
if (ch === "|") {
|
|
545
|
+
// `||` is logical or; only a single `|` feeds the next command.
|
|
546
|
+
if (input[i + 1] === "|") i += 1;
|
|
547
|
+
else if (commands.length >= pushedTo) piped = true;
|
|
548
|
+
}
|
|
193
549
|
i++;
|
|
194
550
|
if (ch === "\n") {
|
|
195
|
-
for (const
|
|
551
|
+
for (const heredoc of heredocs) {
|
|
552
|
+
const skipped = skipHeredoc(i, heredoc.delimiter);
|
|
553
|
+
if (!skipped.found) hazards.push("unterminated heredoc");
|
|
554
|
+
const owner = heredoc.owner;
|
|
555
|
+
if (owner) {
|
|
556
|
+
owner.heredocs ??= [];
|
|
557
|
+
owner.heredocs.push(skipped.body);
|
|
558
|
+
}
|
|
559
|
+
i = skipped.next;
|
|
560
|
+
}
|
|
196
561
|
heredocs = [];
|
|
197
562
|
}
|
|
198
563
|
continue;
|
|
@@ -210,13 +575,24 @@ export function tokenize(input: string): SimpleCommand[] {
|
|
|
210
575
|
continue;
|
|
211
576
|
}
|
|
212
577
|
|
|
578
|
+
if (ch === "*" || ch === "?" || ch === "[") meta.glob = true;
|
|
213
579
|
word += ch;
|
|
214
580
|
inWord = true;
|
|
215
581
|
i++;
|
|
216
582
|
}
|
|
217
583
|
|
|
218
584
|
endCommand();
|
|
585
|
+
if (heredocs.length > 0) hazards.push("unterminated heredoc");
|
|
586
|
+
|
|
587
|
+
for (const sub of subs) {
|
|
588
|
+
const inner = tokenizeDetailed(sub);
|
|
589
|
+
commands.push(...inner.commands);
|
|
590
|
+
hazards.push(...inner.hazards);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
return { commands, hazards };
|
|
594
|
+
}
|
|
219
595
|
|
|
220
|
-
|
|
221
|
-
return commands;
|
|
596
|
+
export function tokenize(input: string): SimpleCommand[] {
|
|
597
|
+
return tokenizeDetailed(input).commands;
|
|
222
598
|
}
|
package/src/unlayer.ts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Peels the layers between a command string and the program that runs.
|
|
3
|
+
*
|
|
4
|
+
* Two things hide a program from the rule matcher:
|
|
5
|
+
*
|
|
6
|
+
* - shell keywords, which the splitter leaves in front of the command
|
|
7
|
+
* (`if true; then rm -rf x; fi` puts `then` where the program should be);
|
|
8
|
+
* - wrapper programs whose own argv carries the real command.
|
|
9
|
+
*
|
|
10
|
+
* Only wrappers with a closed option table are peeled here. `env -S`, `xargs`,
|
|
11
|
+
* `find -exec`, `busybox`, `eval` and `source` build or hide their command at
|
|
12
|
+
* runtime, so they stay whole and remain a reason to ask.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { stripPrefixes } from "./prefixes";
|
|
16
|
+
|
|
17
|
+
/** Segments that never carry a command of their own. */
|
|
18
|
+
const SKIP_WORDS = new Set(["for", "in", "case", "fi", "done", "esac", "}"]);
|
|
19
|
+
|
|
20
|
+
/** Keywords that precede a command; the command follows them. */
|
|
21
|
+
const PREFIX_WORDS = new Set(["then", "do", "else", "elif", "if", "while", "until", "!", "{", "coproc"]);
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The innermost command of one simple command, or undefined when the segment
|
|
25
|
+
* holds no command at all. Word-exact, position zero only: `cp a in` keeps `in`
|
|
26
|
+
* as an operand.
|
|
27
|
+
*/
|
|
28
|
+
export function unlayer(argv: string[]): string[] | undefined {
|
|
29
|
+
let current = argv;
|
|
30
|
+
|
|
31
|
+
for (let step = 0; step < 4; step += 1) {
|
|
32
|
+
const head = current[0];
|
|
33
|
+
if (head === undefined) return current;
|
|
34
|
+
if (SKIP_WORDS.has(head)) return undefined;
|
|
35
|
+
if (PREFIX_WORDS.has(head)) {
|
|
36
|
+
current = current.slice(1);
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
const stripped = stripPrefixes(current);
|
|
40
|
+
if (stripped !== current && stripped.length > 0) {
|
|
41
|
+
current = stripped;
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
return current;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return current;
|
|
48
|
+
}
|