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 struct UnwrapResult { let command: [String] let usesModifiers: Bool } 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]? { self.unwrapWithMetadata(command)?.command } static func unwrapWithMetadata(_ command: [String]) -> UnwrapResult? { var idx = 1 var expectsOptionValue = false var usesModifiers = false while idx < command.count { let token = command[idx].trimmingCharacters(in: .whitespacesAndNewlines) if token.isEmpty { idx += 1 continue } if expectsOptionValue { expectsOptionValue = false usesModifiers = true idx += 1 continue } if token == "--" || token == "-" { idx += 1 break } if self.isEnvAssignment(token) { usesModifiers = true idx += 1 continue } if token.hasPrefix("-"), token != "-" { let lower = token.lowercased() let flag = lower.split(separator: "=", maxSplits: 1).first.map(String.init) ?? lower if ExecEnvOptions.flagOnly.contains(flag) { usesModifiers = true idx += 1 continue } if ExecEnvOptions.withValue.contains(flag) { usesModifiers = true 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=") { usesModifiers = true idx += 1 continue } return nil } break } guard !expectsOptionValue, idx < command.count else { return nil } return UnwrapResult(command: Array(command[idx...]), usesModifiers: usesModifiers) } }