Skip to content

Commit

Permalink
Not much of a start, but it's a start.
Browse files Browse the repository at this point in the history
  • Loading branch information
nixpulvis committed Jul 10, 2018
0 parents commit a7142d8
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
/target
**/*.rs.bk
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
@@ -0,0 +1,6 @@
[package]
name = "oursh"
version = "0.1.0"
authors = ["Nathan Lilienthal <nathan@nixpulvis.com>"]

[dependencies]
58 changes: 58 additions & 0 deletions src/main.rs
@@ -0,0 +1,58 @@
use std::io::{self, Read, Write};
use std::process::{Command, Output};
use std::str;

// Our shell, for the greater good. Ready and waiting.
fn main() {
// Standard input file descriptor (0), used for user input from the user
// of the shell.
let mut stdin = io::stdin();

// Standard output file descriptor (1), used to display program output to
// the user of the shell.
let mut stdout = io::stdout();

// STDIN, input buffer, used to `read` text into for further processing.
// TODO: Full fledged parser will be neato.
let mut input = [0; 24];

loop {
// XXX: Blindly drop the contents of input, again this will be better
// with a real parser.
input = [0; 24];

// Print a boring static prompt.
print!("oursh> ");
stdout.lock().flush();

loop {
// TODO: Enable raw access to STDIN, so we can read as the user
// types. By default the underlying file is line buffered. This
// will allow us to process history, syntax, and more!

// Read what's avalible to us.
stdin.read(&mut input).expect("error reading STDIN");

// Once we've read a complete "program" (§2.10.2) we handle it,
// until then we keep reading. Once we have a proper parser this
// wont look like a huge hack.
let vec: Vec<&[u8]> = input.splitn(2, |b| *b == '\n' as u8).collect();
match &vec[..] {
[line, rest] => {
let program = str::from_utf8(&line).expect("error reading utf8");
let mut output = handle_program(program);
stdout.write(&output.stdout).expect("error writing to STDOUT");
break
}
_ => {},
}
}
}
}

// Obviously very wrong. Most notably this blocks until the command completes.
fn handle_program(program: &str) -> Output {
Command::new(program)
.output()
.expect("error executing program")
}

0 comments on commit a7142d8

Please sign in to comment.