From 617cfde741d6b9de77d619d675a3277217e549f0 Mon Sep 17 00:00:00 2001 From: ModStart Date: Mon, 16 Dec 2024 11:16:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=EF=BC=9A=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E8=A1=8C=E8=A7=A3=E6=9E=90=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- electron/lib/util.ts | 46 ++++++++++++++++++++++++++++++++++++++ electron/mapi/app/index.ts | 31 ++----------------------- 2 files changed, 48 insertions(+), 29 deletions(-) diff --git a/electron/lib/util.ts b/electron/lib/util.ts index 52b9c5f9..4c101b85 100644 --- a/electron/lib/util.ts +++ b/electron/lib/util.ts @@ -265,6 +265,52 @@ export const MemoryCacheUtil = { export const ShellUtil = { quotaPath(p: string) { return `"${p}"` + }, + parseCommandArgs(command: string) { + let args = [] + let arg = '' + let quote = '' + let escape = false + for (let i = 0; i < command.length; i++) { + const c = command[i] + if (escape) { + arg += c + escape = false + continue + } + if ('\\' === c) { + escape = true + arg += c + continue + } + if ('' === quote && (' ' === c || '\t' === c)) { + if (arg) { + args.push(arg) + arg = '' + } + continue + } + if ('' === quote && ('"' === c || "'" === c)) { + quote = c + arg += c + continue + } + if ('"' === quote && '"' === c) { + quote = '' + arg += c + continue + } + if ("'" === quote && "'" === c) { + quote = '' + arg += c + continue + } + arg += c + } + if (arg) { + args.push(arg) + } + return args } } diff --git a/electron/mapi/app/index.ts b/electron/mapi/app/index.ts index 88bbc956..61a5b278 100644 --- a/electron/mapi/app/index.ts +++ b/electron/mapi/app/index.ts @@ -4,38 +4,11 @@ import {exec as _exec, spawn} from "node:child_process"; import {isLinux, isMac, isWin, platformArch, platformName, platformUUID, platformVersion} from "../../lib/env"; import {Log} from "../log/index"; import iconv from "iconv-lite"; -import {StrUtil} from "../../lib/util"; +import {ShellUtil, StrUtil} from "../../lib/util"; import {AppConfig} from "../../../src/config"; const exec = util.promisify(_exec) -const commandSplit = (command: string) => { - // split command with space, but ignore space in quotes - const args = [] - let arg = '' - let quote = '' - for (let i = 0; i < command.length; i++) { - const c = command[i] - if (c === ' ' && !quote) { - if (arg) { - args.push(arg) - arg = '' - } - continue - } - if (c === '"' || c === "'") { - if (quote && quote === c) { - quote = '' - continue - } - quote = c - continue - } - arg += c - } - return args -} - const outputStringConvert = (outputEncoding: 'utf8' | 'cp936', data: any) => { if (!data) { return '' @@ -90,7 +63,7 @@ const spawnShell = async (command: string | string[], option: { commandEntry = command[0] args = command.slice(1) } else { - args = commandSplit(command) + args = ShellUtil.parseCommandArgs(command) commandEntry = args.shift() as string } Log.info('App.spawnShell', {