Starting point
This commit is contained in:
commit
2fa6237406
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
target/*
|
7
Cargo.lock
generated
Normal file
7
Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "Rshell"
|
||||||
|
version = "0.1.0"
|
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "Rshell"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
14
README.md
Normal file
14
README.md
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
# RShell
|
||||||
|
Rust Shell. This is an attempt to create a simple shell in Rust, because why not.
|
||||||
|
|
||||||
|
My starting point is [this article](https://www.joshmcguigan.com/blog/build-your-own-shell-rust/) which is excellent. I will start with the final form of a shell given in the article and try to reformat it:
|
||||||
|
* Make it more easy to extend
|
||||||
|
* Make it a lib
|
||||||
|
|
||||||
|
I can also add basic features:
|
||||||
|
* History
|
||||||
|
* customizable prompt
|
||||||
|
* Colours ?
|
||||||
|
|
||||||
|
Later, I can even make something useful out of it. For example, I could make it so that it is configurable via a config file. I could make it the default terminal via ssh for some user and make it so the terminal opens directly in a docker container, or chrooted or smthing, as a basic form of honeypot.
|
||||||
|
|
80
src/main.rs
Normal file
80
src/main.rs
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
use std::process::Command;
|
||||||
|
use std::io::stdin;
|
||||||
|
use std::io::stdout;
|
||||||
|
use std::io::Write;
|
||||||
|
use std::path::Path;
|
||||||
|
use std::env;
|
||||||
|
use std::process::Child;
|
||||||
|
use std::process::Stdio;
|
||||||
|
|
||||||
|
fn main(){
|
||||||
|
loop {
|
||||||
|
print!("> ");
|
||||||
|
stdout().flush();
|
||||||
|
|
||||||
|
let mut input = String::new();
|
||||||
|
stdin().read_line(&mut input).unwrap();
|
||||||
|
|
||||||
|
// must be peekable so we know when we are on the last command
|
||||||
|
let mut commands = input.trim().split(" | ").peekable();
|
||||||
|
let mut previous_command = None;
|
||||||
|
|
||||||
|
while let Some(command) = commands.next() {
|
||||||
|
|
||||||
|
let mut parts = command.trim().split_whitespace();
|
||||||
|
let command = parts.next().unwrap();
|
||||||
|
let args = parts;
|
||||||
|
|
||||||
|
match command {
|
||||||
|
"cd" => {
|
||||||
|
let new_dir = args.peekable().peek()
|
||||||
|
.map_or("/", |x| *x);
|
||||||
|
let root = Path::new(new_dir);
|
||||||
|
if let Err(e) = env::set_current_dir(&root) {
|
||||||
|
eprintln!("{}", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
previous_command = None;
|
||||||
|
},
|
||||||
|
"exit" => return,
|
||||||
|
command => {
|
||||||
|
let stdin = previous_command
|
||||||
|
.map_or(
|
||||||
|
Stdio::inherit(),
|
||||||
|
|output: Child| Stdio::from(output.stdout.unwrap())
|
||||||
|
);
|
||||||
|
|
||||||
|
let stdout = if commands.peek().is_some() {
|
||||||
|
// there is another command piped behind this one
|
||||||
|
// prepare to send output to the next command
|
||||||
|
Stdio::piped()
|
||||||
|
} else {
|
||||||
|
// there are no more commands piped behind this one
|
||||||
|
// send output to shell stdout
|
||||||
|
Stdio::inherit()
|
||||||
|
};
|
||||||
|
|
||||||
|
let output = Command::new(command)
|
||||||
|
.args(args)
|
||||||
|
.stdin(stdin)
|
||||||
|
.stdout(stdout)
|
||||||
|
.spawn();
|
||||||
|
|
||||||
|
match output {
|
||||||
|
Ok(output) => { previous_command = Some(output); },
|
||||||
|
Err(e) => {
|
||||||
|
previous_command = None;
|
||||||
|
eprintln!("{}", e);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(mut final_command) = previous_command {
|
||||||
|
// block until the final command has finished
|
||||||
|
final_command.wait();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user