7 ms·
Building your own shell using Rust
- jslakro 8y agoAny other recommended article in making your own shell in other language?
- homarp 8y agohttps://github.com/danistefanovic/build-your-own-x#build-your-own-shell https://github.com/danistefanovic/build-your-own-x#build-you... ?
- ejanus 8y agoI am not a professional programmer , just a guy fooling around without job. But I am surprised that stuff like this would make it to HN front page . I have checked out the link , it is indeed great but it is not what I should expect to land here. My take is that Rust is hot nowadays.
- ejanus 8y agoFeasting on me with down votes . Please why?
- zufallsheld 8y agoProbably because your comment did Not contribute to the discussion.
- ejanus 8y agoOk
- coldtea 8y agoFirst of all, because it doesn't make any point. "I am surprised that stuff like this would make it to HN front page. I have checked out the link , it is indeed great but it is not what I should expect to land here." Why? Is it too simple for HN? Too different from what's posted here? You don't clarify, and make no real contribution to the discussion. (Besides, you, yourself, call it "great". So why shouldn't it be here?) Furthermore, the surprise is misplaced. This kind of post features regularly on HN, be it for Rust, Go, etc. There have been similar posts on building a shell, a basic compiler/interpreter, a simple boot loader, and tons of other things besides. Last, but not least, your last sentence looks like a jab/snark against Rust or against HN-ers voting based on trends ("My take is that Rust is hot nowadays"). Rust is indeed hot, but you can find similar posts all the time on HN for all kinds of languages.
- deleted 8y ago[deleted]
- pmoriarty 8y agoFrom the HN Guidelines: Please don't complain that a submission is inappropriate. If a story is spam or off-topic, flag it. Don't feed egregious comments by replying; flag them instead. If you flag something, please don't also comment that you did. https://news.ycombinator.com/newsguidelines.html https://news.ycombinator.com/newsguidelines.html
- JoshMcguigan 8y agoHi 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.
- JacKTrocinskI 8y agoWhy not just write in C?
- jchw 8y agoWhy not just write it in Rust?
- deleted 8y ago[deleted]
- Cursuviam 8y agoHaving written a shell in C, shells require a lot of string and buffer manipulation, which I prefer doing in rust for both security and ergonomic reasons.
- JacKTrocinskI 8y agoSecurity in a shell, what for exactly?
- saagarjha 8y agoBecause shells generally provide you with almost unrestricted access to the system, so it’s pretty important that they are secure?
- JacKTrocinskI 8y agoIf you already have access to the shell of a system would you not say that you have already defeated many of the security points of the system you are breaking into?
- JacKTrocinskI 8y agoA shell does not grant you unrestricted access to a system, that's absurd, once an attacker gains access to a shell then they still have gain root.
- noahster11 8y agoIf anyone is interested in developing a shell even further, checkout rustyline[0]. It makes it very simple to add keyboard shortcuts (Ctrl-C), completion, and even history. There's also liner[1], which is part of the redox-os project, which also has a lot of the same features. [0]: https://github.com/kkawakam/rustyline https://github.com/kkawakam/rustyline [1]: https://github.com/redox-os/liner https://github.com/redox-os/liner
- JoshMcguigan 8y agoI'd second the recommendation for rustyline. I played around with it a bit while reviewing the rush[0] project, another shell implemented in Rust for learning purposes. [0]: https://github.com/psinghal20/rush https://github.com/psinghal20/rush
- laumars 8y agoctrl+c (as well as ctrl+\ and ctrl+z) is a little different from other shortcuts because they're captured by your TTY rather than your running application - though you can catch the interrupt that ctrl+c et al sends. Lesser known fact: you can also redefine which keys you want to raise SIGINT, SIGQUIT and SIGTSTP with.
- Cursuviam 8y agoI second this greatly! Too many people roll their own line editors and always mess up the readline (emacs) shortcuts.
- laumars 8y agoI rolled my own and didn't even bother with emacs shortcuts (got vim ones though, as that's what I'm familiar with). However I'm not adverse adding emacs shortcuts if anyone can recommend a good guide for which keys do what.
- Cursuviam 8y agoBash shortcut guides get most of the useful ones. https://github.com/fliptheweb/bash-shortcuts-cheat-sheet https://github.com/fliptheweb/bash-shortcuts-cheat-sheet
- Null-Set 8y agoAn example of a more fully featured shell written in rust is Ion. https://gitlab.redox-os.org/redox-os/ion https://gitlab.redox-os.org/redox-os/ion It is inspired by fish
- nixpulvis 8y agoI'm currently writing a shell in Rust, and have written a post about it. If you find this interesting you may also find my post interesting. https://nixpulvis.com/ramblings/2018-10-15-building-a-shell-part-2 https://nixpulvis.com/ramblings/2018-10-15-building-a-shell-...
- JoshMcguigan 8y agoThanks for sharing, your posts are very well written. I really like the idea of syntax highlighting before the user presses enter. Any reason you are writing your own posix parser and readline implementation, rather than using existing Rust crates?
- nixpulvis 8y agoPartially to learn how, partially to maintain a consistent style, and partially to see where we can improve. As for the POSIX parser specifically, I love grammars, and want to support multiple, and building one in LALRPOP is t too bad. Also I looked at conch-parser, but didn't want to use its AST, and decided to go this route. I have been using the termion crate, and may have another PR to contribute. I have somewhat ambitious goals for the readline like library though.
- JoshMcguigan 8y agoAs I was thinking about an improved user interface for the shell, it seemed like at some point you'd hit the limit of what is reasonable given the current split implementation between terminal emulator and shell. Do you think you'll get to a point where you'll need to build an all-in-one solution?
- nixpulvis 8y agoIt's interesting you mention that. I'm (slightly) involved with the Alacritty project, and generally like the layer of abstraction here. If not only to support the existing status quo, but also as it leaves a lot of the platform specific features for the terminal. ANSI, and the other standards are large and somewhat unwieldy though, and there are times it would be nice to do away with it all. As for features, there's a number of things that you could argue for being implemented in either place. One feature I feel is a good fit for the shell for example is saving and being able to recall and/or pipe previous commands, while copy paste might be implemented in both.