Search code examples
macosterminalspawnptyjob-control

How to switch terminal to new child process of process launched with NSTask?


I made a pseudo terminal with method described here: http://lists.apple.com/archives/student-dev/2005/Mar/msg00019.html

The terminal itself worked well. Anyway the problem is terminal cannot being switched to child process. For an example, I launched bash with NSTask, and if I execute ftp within the bash, it stops automatically.

ftp
ftp
ftp> 
[1]+  Stopped                 ftp
bash-3.2$ 

And if I try to continue the ftp with fg, it terminates quietly. (I checked this with Activity Monitor)

fg
fg
ftp

bash-3.2$ 

fg
fg
bash: fg: current: no such job
bash-3.2$ 

I think it needs some more infrastructure (which completes pseudo terminal) to switch control to child process. What's required to do this?


Solution

  • I could finally do this by creating a pty device. To make a program behave like "Terminal", it must be executed in an interactive terminal, and that needs pseudo-terminal device.

    Unfortunately, AFAIK, NSTask does not support any pty ability, so I had to get down to BSD layer.

    Here's my current implementation: https://github.com/eonil/PseudoTeletypewriter.Swift

    sudo is working well, and I believe ssh should also work.