4 ms·
Hi all, author here. I'm not sure of the rules around self promotion so I want to point out that I didn't submit this, but I'm happy to see that others are enjo
by JoshMcguigan 8y ago
Hi all, author here. I'm not sure of the rules around self promotion so I want to point out that I didn't submit this, but I'm happy to see that others are enjoying it.
I wrote this blog post to document my process of building a simple shell using Rust. I learned a lot about how shells, terminal emulators, and the OS interact by going through this exercise, so I hope this is useful to others as well.
Feel free to reach out if you have any questions.
- detaro 8y agoFor future reference, it's fine to submit your own stuff as long as you don't overdo it. For things that can be tried out, there's also a special Show HN category: https://news.ycombinator.com/show https://news.ycombinator.com/show
- ejanus 8y agoIf you have open source projects, then I would like to join
- JoshMcguigan 8y agoI've linked my GitHub profile below. I really enjoy social coding, so feel free to open pull requests or issues on any of my repos. https://github.com/JoshMcguigan https://github.com/JoshMcguigan
- robert_foss 8y agoTooting your own horn is a foundational block of HN and very welcome!
- jonathankoren 8y agoI've never used Rust, but looking at the code, and reading https://doc.rust-lang.org/std/process/struct.Command.html https://doc.rust-lang.org/std/process/struct.Command.html , is std::process:Command just farming out to bash? I ask, because the examples don't specify a full path, so that implies something else is doing the PATH search, like bash. Because of this, and the semantics, it looks like it's equivalent to a system() function in other languages. If you really want job control, a path, environmental variables, and all the things that make a shell a shell, you have to be using exec() or execvp() https://doc.rust-lang.org/std/process/struct.Command.html#method.exec https://doc.rust-lang.org/std/process/struct.Command.html#me... . Otherwise all you've done is wrap an existing shell that does all the work for you, and that was an immediate fail when we had to a make a shell in my undergrad systems course. An easy check is the use the command "echo $SHELL". If that spits out anything besides literally "$SHELL", you're farming out. All that said, Rust's exec() is listed a unix specific, so if you're on Windows, I guess you're stuck.
- wtetzner 8y agoIf you look at the examples in the first link you posted, you can see to farm out to an existing shell, they're explicitly invoking "sh". There would be no reason to do that if Command always farmed out to another shell.
- JoshMcguigan 8y agoI'm not confident enough to say this for sure, but I think you are off base. I created a demo in the Rust playground [0] which shows that there is no implicit environment variable expansion. Also, the source for `std::process:Command` [1] looks to be calling into libc to perform a fork/exec. That said, this is rather new territory for me, so let me know if I'm missing something here. [0] : https://play.rust-lang.org/?version=stable&mode=debug&edition=2015&gist=54b9b60c2b2b7ea84ed1f2e433024043 https://play.rust-lang.org/?version=stable&mode=debug&editio... [1] : https://github.com/rust-lang/rust/blob/master/src/libstd/sys/unix/process/process_unix.rs#L52 https://github.com/rust-lang/rust/blob/master/src/libstd/sys...
- johncolanduoni 8y agoIt will use `execvp` (which automatically reads the PATH variable, no shell required) if you use features that require it (like `before_exec` which runs a block of code between `fork` and `exec), but it generally prefers `posix_spawnp` (Which also uses PATH). The relevant code is here: https://github.com/rust-lang/rust/blob/7d3b9b1640611c52eb949dd60e8b9565997c6cea/src/libstd/sys/unix/process/process_unix.rs https://github.com/rust-lang/rust/blob/7d3b9b1640611c52eb949.... All of this is of course Unix specific, on Windows it uses `CreateProcess`.
- adito 8y agoThe internal implementation of Command is using libc::execvp() on unix and CreateProcessW on windows. In windows case, it's searching the executable in the PATH manually first to match the semantics of unix.
- steveklabnik 8y agoIf you or anyone else would like to file a bug, that’d be great: we should make this more clear in the docs. I’m about to jump on a plane or I’d just do it; a bug will make sure I get back around to it eventually.