Building a Shell in Swift

Hi,

So I am interested in building a shell in Swift. I have built a toy shell in a Computer Systems class a few months ago (current CS undergrad). My question is what is the recommend Swift way of doing this?

I don't think Process has anyway of getting or setting the groupIDs which I was taught to use for example pass along SIGINT to child processes.

Will I be able to use the up coming rewrite of Process to build something like this?

Thanks in advanced for any help/advice you may have,
Zane

Hey Zane,

Process doesn't have a wrapper for setpgid, but you can just fish out the PID and call setpgid yourself

2 Likes

Thanks for the fast reply. So I only get a child id after calling process.run() but when I try setpgid(childId, parrentID) after calling run() errno is set to EACCES which makes sense but I don't think this will work for changing the child group id. :confused:

I know this is wrong but I also don't have a way of passing any variables into singal as the code below gets the following error a C function pointer cannot be formed from a closure that captures context

var id: Int32 = 0
signal(SIGINT,{ sig in 
      kill(id, SIGINT)
      exit(1)
})

You'll probably need to drop down to the posix_spawn function and use posix_spawnattr_setpgroup.

I don't think you can use traditional fork and execve because I don't think you're supposed to call into the Swift runtime after a fork, and it may be difficult to avoid such calls.

1 Like

Thanks I will give that a try next.

There is the linenoise-swift project which seems to provide a quite sophisticated command line but without the ability of actually starting something (if I haven't overlooked anything). It might be interesting to see how this could be combined.