Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
217 changes: 217 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
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ edition = "2018"
[dependencies]
os_pipe = "0.9.2"
nix = "0.18.0"
regex = "1.3.9"

[dev-dependencies]
assert_cmd = "1.0.1"
escargot = "0.5.0"
predicates-core = "1.0.0"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ I know that the name is overused, but it's too good to pass up.
- [ ] Async execution `&`
- [ ] Shell builtins
- [ ] Normal built-ins
- [ ] `alias` `unalias`
- [x] `alias` `unalias`
- [X] `cd`
- [ ] etc
- [ ] Special built-ins
Expand Down
52 changes: 52 additions & 0 deletions src/builtins.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use regex::Regex;
use std::collections::BTreeMap;
use std::process::exit as exit_program;
use std::env;
use std::rc::Rc;
Expand All @@ -7,6 +9,36 @@ use crate::helpers::Shell;
// Unless specified otherwise, if provided multiple arguments while only
// accepting one, these use the first argument. Dash does this as well.

pub fn alias(
// Aliases can be added and then printed in the same command
aliases: &mut BTreeMap<String, String>,
args: Vec<String>,
) -> bool {
if args.is_empty() {
for (lhs, rhs) in aliases {
println!("alias {}='{}'", lhs, rhs);
}
true
} else {
let mut success = true;
let assignment_re = Regex::new(r"^(\w+)=(.*)").unwrap();
for arg in args {
if assignment_re.is_match(&arg) {
let caps = assignment_re.captures(&arg).unwrap();
let lhs = &caps[1];
let rhs = &caps[2];
aliases.insert(lhs.to_string(), rhs.to_string());
} else if aliases.contains_key(&arg) {
println!("alias {}='{}'", arg, aliases[&arg]);
} else {
eprintln!("rush: alias: {}: not found", arg);
success = false;
}
}
success
}
}

pub fn exit(args: Vec<String>) -> bool {
match args.get(0).map_or(Ok(0), |x| x.parse::<i32>()) {
Ok(n) => {
Expand Down Expand Up @@ -35,3 +67,23 @@ pub fn set(args: Vec<String>, shell: &Rc<RefCell<Shell>>) -> bool {
true
}

pub fn unalias(aliases: &mut BTreeMap<String, String>, args: Vec<String>) -> bool {
if args.is_empty() {
eprintln!("unalias: usage: unalias [-a] name [name ...]");
false
} else if args[0] == "-a" {
aliases.clear();
true
} else {
let mut success = true;
for arg in args {
if aliases.contains_key(&arg) {
aliases.remove(&arg);
} else {
eprintln!("rush: unalias: {}: not found", arg);
success = false;
}
}
success
}
}
7 changes: 7 additions & 0 deletions src/debug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/// Alias of println! but only present in debug builds.
macro_rules! debug_println {
($($t:tt)*) => {
#[cfg(debug_assertions)] // Only include when not built with `--release` flag
println!($($t)*);
}
}
4 changes: 3 additions & 1 deletion src/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use nix::unistd::Uid;
use os_pipe::{dup_stderr, dup_stdin, dup_stdout, PipeReader, PipeWriter};
use std::collections::HashMap;
use std::collections::{BTreeMap, HashMap};
use std::env;
use std::fs::{self, File, OpenOptions};
use std::io::{self, BufRead, BufReader, Write};
Expand Down Expand Up @@ -38,6 +38,7 @@ pub struct Shell {
positional: Vec<String>,
name: String,
pub vars: HashMap<String, String>,
pub aliases: BTreeMap<String, String>,
}

impl Shell {
Expand All @@ -62,6 +63,7 @@ impl Shell {
positional: Vec::new(),
name,
vars: HashMap::new(),
aliases: BTreeMap::new(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ impl Lexer {
}
Some(_) => match self.read_until(false, false, false, Box::new(is_token_split)) {
Ok(w) => {
println!("The words I got: {:?}", w);
debug_println!("The words I got: {:?}", w);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, this print statement did slid past me before I pushed. Thanks for catching it.

Honestly I'm not sure whether something like this makes sense as a debug_println, or if it should just be removed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heheh, ok. I've left it in for now, feel free to remove it later.

match &w[..] {
[Literal(s), ..]
if s.ends_with('=')
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[macro_use]
mod debug;

pub mod lexer;
pub mod parser;
pub mod runner;
Expand Down
6 changes: 4 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[macro_use]
mod debug;

use rush::lexer::Lexer;
use rush::parser::Parser;
use rush::runner::Runner;
Expand All @@ -20,8 +23,7 @@ fn main() {
let mut parser = Parser::new(lexer, Rc::clone(&shell));
match parser.get() {
Ok(command) => {
#[cfg(debug_assertions)] // Only include when not built with `--release` flag
println!("\u{001b}[34m{:#?}\u{001b}[0m", command);
debug_println!("\u{001b}[34m{:#?}\u{001b}[0m", command);

runner.execute(command, false);
}
Expand Down
Loading