提交 ba6592ff 编写于 作者: A Andre Weinand

improve quoting args; fixes #21635

上级 7afecae3
......@@ -11,6 +11,8 @@ import { ITerminalService, ITerminalInstance, ITerminalConfiguration } from 'vs/
import { ITerminalService as IExternalTerminalService } from 'vs/workbench/parts/execution/common/execution';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
const enum ShellType { cmd, powershell, bash };
export class TerminalSupport {
private static integratedTerminalInstance: ITerminalInstance;
......@@ -55,84 +57,104 @@ export class TerminalSupport {
private static prepareCommand(args: DebugProtocol.RunInTerminalRequestArguments, configurationService: IConfigurationService): string {
let shellType: ShellType;
// get the shell configuration for the current platform
let shell: string;
const shell_config = (<ITerminalConfiguration>configurationService.getConfiguration<any>().terminal.integrated).shell;
if (platform.isWindows) {
shell = shell_config.windows;
shellType = ShellType.cmd;
} else if (platform.isLinux) {
shell = shell_config.linux;
shellType = ShellType.bash;
} else if (platform.isMacintosh) {
shell = shell_config.osx;
shellType = ShellType.bash;
}
shell = shell.toLowerCase();
// try to determine the shell type
shell = shell.trim().toLowerCase();
if (shell.indexOf('powershell') >= 0) {
shellType = ShellType.powershell;
} else if (shell.indexOf('cmd.exe') >= 0) {
shellType = ShellType.cmd;
} else if (shell.indexOf('bash') >= 0) {
shellType = ShellType.bash;
} else if (shell.indexOf('c:\\program files\\git\\bin\\bash.exe') >= 0) {
shellType = ShellType.bash;
}
let quote: (s: string) => string;
let command = '';
if (shell.indexOf('powershell') >= 0) {
const quote = (s: string) => {
s = s.replace(/\'/g, '\'\'');
return s.indexOf(' ') >= 0 || s.indexOf('\'') >= 0 || s.indexOf('"') >= 0 ? `'${s}'` : s;
};
if (args.cwd) {
command += `cd '${args.cwd}'; `;
}
if (args.env) {
for (let key in args.env) {
command += `$env:${key}='${args.env[key]}'; `;
switch (shellType) {
case ShellType.powershell:
quote = (s: string) => {
s = s.replace(/\'/g, '\'\'');
return s.indexOf(' ') >= 0 || s.indexOf('\'') >= 0 || s.indexOf('"') >= 0 ? `'${s}'` : s;
};
if (args.cwd) {
command += `cd '${args.cwd}'; `;
}
if (args.env) {
for (let key in args.env) {
command += `$env:${key}='${args.env[key]}'; `;
}
}
}
for (let a of args.args) {
command += `${quote(a)} `;
}
for (let a of args.args) {
command += `${quote(a)} `;
}
break;
} else if (shell.indexOf('cmd.exe') >= 0) {
case ShellType.cmd:
quote = (s: string) => {
s = s.replace(/\"/g, '""');
return (s.indexOf(' ') >= 0 || s.indexOf('"') >= 0) ? `"${s}"` : s;
};
const quote = (s: string) => {
s = s.replace(/\"/g, '""');
return (s.indexOf(' ') >= 0 || s.indexOf('"') >= 0) ? `"${s}"` : s;
};
if (args.cwd) {
command += `cd ${quote(args.cwd)} && `;
}
if (args.env) {
command += 'cmd /C "';
for (let key in args.env) {
command += `set "${key}=${args.env[key]}" && `;
if (args.cwd) {
command += `cd ${quote(args.cwd)} && `;
}
if (args.env) {
command += 'cmd /C "';
for (let key in args.env) {
command += `set "${key}=${args.env[key]}" && `;
}
}
for (let a of args.args) {
command += `${quote(a)} `;
}
}
for (let a of args.args) {
command += `${quote(a)} `;
}
if (args.env) {
command += '"';
}
} else {
// fallback: unix shell
const quote = (s: string) => {
s = s.replace(/\"/g, '\\"');
return s.indexOf(' ') >= 0 ? `"${s}"` : s;
};
if (args.cwd) {
command += `cd ${quote(args.cwd)} ; `;
}
if (args.env) {
command += 'env';
for (let key in args.env) {
command += ` "${key}=${args.env[key]}"`;
if (args.env) {
command += '"';
}
command += ' ';
}
for (let a of args.args) {
command += `${quote(a)} `;
}
break;
case ShellType.bash:
quote = (s: string) => {
s = s.replace(/\"/g, '\\"');
return (s.indexOf(' ') >= 0 || s.indexOf('\\') >= 0) ? `"${s}"` : s;
};
if (args.cwd) {
command += `cd ${quote(args.cwd)} ; `;
}
if (args.env) {
command += 'env';
for (let key in args.env) {
command += ` "${key}=${args.env[key]}"`;
}
command += ' ';
}
for (let a of args.args) {
command += `${quote(a)} `;
}
break;
}
return command;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册