2026-02-22 10:26:06 +01:00
|
|
|
import Foundation
|
|
|
|
|
|
|
|
|
|
enum ExecCommandToken {
|
|
|
|
|
static func basenameLower(_ token: String) -> String {
|
|
|
|
|
let trimmed = token.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
|
|
|
guard !trimmed.isEmpty else { return "" }
|
|
|
|
|
let normalized = trimmed.replacingOccurrences(of: "\\", with: "/")
|
|
|
|
|
return normalized.split(separator: "/").last.map { String($0).lowercased() } ?? normalized.lowercased()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum ExecEnvInvocationUnwrapper {
|
|
|
|
|
static let maxWrapperDepth = 4
|
|
|
|
|
|
2026-03-19 13:51:17 +02:00
|
|
|
struct UnwrapResult {
|
|
|
|
|
let command: [String]
|
|
|
|
|
let usesModifiers: Bool
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-22 10:26:06 +01:00
|
|
|
private static func isEnvAssignment(_ token: String) -> Bool {
|
|
|
|
|
let pattern = #"^[A-Za-z_][A-Za-z0-9_]*=.*"#
|
|
|
|
|
return token.range(of: pattern, options: .regularExpression) != nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static func unwrap(_ command: [String]) -> [String]? {
|
2026-03-19 13:51:17 +02:00
|
|
|
self.unwrapWithMetadata(command)?.command
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static func unwrapWithMetadata(_ command: [String]) -> UnwrapResult? {
|
2026-02-22 10:26:06 +01:00
|
|
|
var idx = 1
|
|
|
|
|
var expectsOptionValue = false
|
2026-03-19 13:51:17 +02:00
|
|
|
var usesModifiers = false
|
2026-02-22 10:26:06 +01:00
|
|
|
while idx < command.count {
|
|
|
|
|
let token = command[idx].trimmingCharacters(in: .whitespacesAndNewlines)
|
|
|
|
|
if token.isEmpty {
|
|
|
|
|
idx += 1
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if expectsOptionValue {
|
|
|
|
|
expectsOptionValue = false
|
2026-03-19 13:51:17 +02:00
|
|
|
usesModifiers = true
|
2026-02-22 10:26:06 +01:00
|
|
|
idx += 1
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if token == "--" || token == "-" {
|
|
|
|
|
idx += 1
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
if self.isEnvAssignment(token) {
|
2026-03-19 13:51:17 +02:00
|
|
|
usesModifiers = true
|
2026-02-22 10:26:06 +01:00
|
|
|
idx += 1
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if token.hasPrefix("-"), token != "-" {
|
|
|
|
|
let lower = token.lowercased()
|
|
|
|
|
let flag = lower.split(separator: "=", maxSplits: 1).first.map(String.init) ?? lower
|
2026-03-02 11:32:04 +00:00
|
|
|
if ExecEnvOptions.flagOnly.contains(flag) {
|
2026-03-19 13:51:17 +02:00
|
|
|
usesModifiers = true
|
2026-02-22 10:26:06 +01:00
|
|
|
idx += 1
|
|
|
|
|
continue
|
|
|
|
|
}
|
2026-03-02 11:32:04 +00:00
|
|
|
if ExecEnvOptions.withValue.contains(flag) {
|
2026-03-19 13:51:17 +02:00
|
|
|
usesModifiers = true
|
2026-02-22 10:26:06 +01:00
|
|
|
if !lower.contains("=") {
|
|
|
|
|
expectsOptionValue = true
|
|
|
|
|
}
|
|
|
|
|
idx += 1
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if lower.hasPrefix("-u") ||
|
|
|
|
|
lower.hasPrefix("-c") ||
|
|
|
|
|
lower.hasPrefix("-s") ||
|
|
|
|
|
lower.hasPrefix("--unset=") ||
|
|
|
|
|
lower.hasPrefix("--chdir=") ||
|
|
|
|
|
lower.hasPrefix("--split-string=") ||
|
|
|
|
|
lower.hasPrefix("--default-signal=") ||
|
|
|
|
|
lower.hasPrefix("--ignore-signal=") ||
|
|
|
|
|
lower.hasPrefix("--block-signal=")
|
|
|
|
|
{
|
2026-03-19 13:51:17 +02:00
|
|
|
usesModifiers = true
|
2026-02-22 10:26:06 +01:00
|
|
|
idx += 1
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
break
|
|
|
|
|
}
|
2026-03-19 13:51:17 +02:00
|
|
|
guard !expectsOptionValue, idx < command.count else { return nil }
|
|
|
|
|
return UnwrapResult(command: Array(command[idx...]), usesModifiers: usesModifiers)
|
2026-02-22 10:26:06 +01:00
|
|
|
}
|
|
|
|
|
}
|