Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

commandLabel.ts

169 lines5.1 KB

1type CommandWrapper = "env" | "sudo";
2​
3const COMMAND_WRAPPER_OPTIONS_WITH_VALUE: Record<CommandWrapper, ReadonlySet<string>> = {
4 env: new Set(["-C", "--chdir", "-S", "--split-string", "-u", "--unset"]),
5 sudo: new Set(["-C", "--close-from", "-D", "--chdir", "-g", "--group", "-u", "--user"]),
6};
7​
8const COMMAND_WRAPPER_FLAGS: Record<CommandWrapper, ReadonlySet<string>> = {
9 env: new Set(["-0", "--null", "-i", "--ignore-environment", "--debug", "-v"]),
10 sudo: new Set(["-A", "--askpass", "-b", "--background", "-E", "-H", "-i", "-n", "-S"]),
11};
12​
13function tokenizeShellCommand(command: string): string[] | null {
14 const input = command.trim();
15 const tokens: string[] = [];
16 let current = "";
17 let quote: '"' | "'" | null = null;
18 let escaping = false;
19 let substitutionDepth = 0;
20 let tokenStarted = false;
21​
22 for (let index = 0; index < input.length; index += 1) {
23 const character = input[index]!;
24 if (escaping) {
25 current += character;
26 escaping = false;
27 tokenStarted = true;
28 continue;
29 }
30 if (character === "\\" && quote !== "'") {
31 const nextCharacter = input[index + 1];
32 const isWindowsDrivePath = quote === null && /^[A-Za-z]:/.test(current);
33 if (
34 (quote === '"' || isWindowsDrivePath) &&
35 nextCharacter !== undefined &&
36 nextCharacter !== '"' &&
37 nextCharacter !== "\\" &&
38 nextCharacter !== "$" &&
39 nextCharacter !== "`" &&
40 nextCharacter !== "\n"
41 ) {
42 current += character;
43 tokenStarted = true;
44 continue;
45 }
46 escaping = true;
47 tokenStarted = true;
48 continue;
49 }
50 if (quote !== null) {
51 if (character === quote) {
52 quote = null;
53 } else {
54 current += character;
55 }
56 tokenStarted = true;
57 continue;
58 }
59 if (character === "$" && input[index + 1] === "(") {
60 current += "$(";
61 substitutionDepth += 1;
62 tokenStarted = true;
63 index += 1;
64 continue;
65 }
66 if (character === ")" && substitutionDepth > 0) {
67 current += character;
68 substitutionDepth -= 1;
69 tokenStarted = true;
70 continue;
71 }
72 if (character === '"' || character === "'") {
73 quote = character;
74 tokenStarted = true;
75 continue;
76 }
77 if (/\s/u.test(character)) {
78 if (substitutionDepth > 0) {
79 current += character;
80 tokenStarted = true;
81 continue;
82 }
83 if (tokenStarted) {
84 tokens.push(current);
85 current = "";
86 tokenStarted = false;
87 }
88 continue;
89 }
90 current += character;
91 tokenStarted = true;
92 }
93​
94 if (quote !== null || escaping || substitutionDepth > 0) return null;
95 if (tokenStarted) tokens.push(current);
96 return tokens;
98​
99export function commandProgramName(command: string, depth = 0): string | null {
100 if (depth >= 8) return null;
101 const tokens = tokenizeShellCommand(command);
102 if (tokens === null) return null;
103 let index = 0;
104 let wrapper: CommandWrapper | null = null;
105​
106 while (index < tokens.length) {
107 const token = tokens[index];
108 if (!token) return null;
109 if (/^[A-Za-z_][A-Za-z0-9_]*=/.test(token)) {
110 index += 1;
111 continue;
112 }
113 const tokenProgram = token.split(/[\\/]/).at(-1);
114 if (tokenProgram === "env" || tokenProgram === "sudo") {
115 wrapper = tokenProgram;
116 index += 1;
117 continue;
118 }
119 if (wrapper !== null && token === "--") {
120 wrapper = null;
121 index += 1;
122 continue;
123 }
124 if (wrapper !== null && token.startsWith("-")) {
125 if (wrapper === "env" && (token === "-S" || token === "--split-string")) {
126 const splitCommand = tokens[index + 1];
127 return splitCommand ? commandProgramName(splitCommand, depth + 1) : null;
128 }
129 if (wrapper === "env" && token.startsWith("--split-string=")) {
130 return commandProgramName(token.slice("--split-string=".length), depth + 1);
131 }
132 if (COMMAND_WRAPPER_OPTIONS_WITH_VALUE[wrapper].has(token)) {
133 if (tokens[index + 1] === undefined) return null;
134 index += 2;
135 continue;
136 }
137 if (COMMAND_WRAPPER_FLAGS[wrapper].has(token)) {
138 index += 1;
139 continue;
140 }
141 const equalsIndex = token.indexOf("=");
142 if (token.startsWith("--") && equalsIndex > 2) {
143 if (!COMMAND_WRAPPER_OPTIONS_WITH_VALUE[wrapper].has(token.slice(0, equalsIndex))) {
144 return null;
145 }
146 index += 1;
147 continue;
148 }
149 if (/^-[A-Za-z].+/.test(token) && !token.startsWith("--")) {
150 let consumesNextToken = false;
151 for (const [optionIndex, option] of token.slice(1).split("").entries()) {
152 const shortOption = `-${option}`;
153 if (COMMAND_WRAPPER_OPTIONS_WITH_VALUE[wrapper].has(shortOption)) {
154 consumesNextToken = optionIndex === token.length - 2;
155 break;
156 }
157 if (!COMMAND_WRAPPER_FLAGS[wrapper].has(shortOption)) return null;
158 }
159 if (consumesNextToken && tokens[index + 1] === undefined) return null;
160 index += consumesNextToken ? 2 : 1;
161 continue;
162 }
163 return null;
164 }
165 return token.split(/[\\/]/).at(-1) || null;
166 }
167​
168 return null;