Following on my question at https://forums.swift.org/t/how-to-allow-process-to-receive-user-input-when-run-as-part-of-an-executable-e-g-to-enabled-sudo-commands/34357 I am trying to spawn an interactive process using a swift executable, specifically `sudo`.
I've tried a few things but nothing is working so I think I'm missing something fundemental.
I tried using `openpty` but I believe the issue is that I need to pass input to `amaster`, which I'm unsure how to do:
public func runOpenpty(_ command: [String]) throws {
var amaster: Int32 = 0
var aslave: Int32 = 0
if openpty(&amaster, &aslave, nil, nil, nil) == -1 {
print("Failed to open pty")
}
let masterHandle = FileHandle(fileDescriptor: amaster, closeOnDealloc: true)
let slaveHandle = FileHandle(fileDescriptor: aslave, closeOnDealloc: true)
let process = Process()
process.launchPath = "/usr/bin/env"
process.arguments = command
process.standardInput = FileHandle.standardInput
process.standardOutput = FileHandle.standardOutput
try process.run()
process.waitUntilExit()
}
I also came across https://github.com/eonil/PseudoTeletypewriter.Swift, which uses `forkpty`, but I've been unable to get it working from a swift executable.
How can I allow the user to provide input to a process that I spawn from my swift executable?