Skip to content

Commit

Permalink
Implement variables
Browse files Browse the repository at this point in the history
Also added exit codes for applications, bug fixes in cd command, fixed parser and much more
  • Loading branch information
robiot committed Dec 13, 2021
1 parent 8cae5f5 commit 4224f0c
Show file tree
Hide file tree
Showing 15 changed files with 387 additions and 287 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ jobs:
run: cargo run -- -c "echo test"
- name: Run tests
run: cargo test
- name: Clippy
run: cargo clippy --workspace -- -D warnings
30 changes: 2 additions & 28 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2018"
license = "GPL-3.0"
name = "zash"
repository = "https://github.com/robiot/zash"
version = "0.2.0"
version = "0.3.0"

[dependencies]
colored = "2.0.0"
Expand All @@ -15,6 +15,6 @@ rustyline-derive = "0.5.0"
signal-hook = "0.3.10"
structopt = "0.3.25"
# toml = "0.5.8"
whoami = "1.1.5"
glob = "0.3.0"
regex = "1.5.4"
whoami = "1.1.5"
libc = "0.2"
22 changes: 13 additions & 9 deletions src/builtins/cd.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
use colored::Colorize;
use crate::parser;
use crate::utils;

pub fn cd(args: parser::Parser) -> i32
fn error_cd<T: std::string::ToString>(error: T) {
utils::zash_error(format!("{}: {}", "cd".red(), error.to_string()));
}

pub fn cd(args: Vec<String>) -> i32
{
let homedir = utils::get_home_dir();
let mut peekable = args.peekable();
if let Some(new_dir) = peekable.peek().as_ref()
if args.len() > 1 {
error_cd("too many arguments");
return 1;
}

if let Some(dir) = args.get(0)
{
let dir = new_dir.to_string().replace("~", &homedir.to_string());
let root = std::path::Path::new(&dir);
if let Err(_) = std::env::set_current_dir(&root) {
println!("{}: no such file or directory: {}", "cd".red(), dir);
if let Err(err) = std::env::set_current_dir(&std::path::Path::new(&dir)) {
error_cd(utils::error_string(err.raw_os_error().unwrap()));
return 1;
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/builtins/exit.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use crate::parser;
use crate::utils;

pub fn exit(args: parser::Parser) -> i32
pub fn exit(args: Vec<String>) -> i32
{
let mut peekable = args.peekable();
if let Some(exit_code) = peekable.peek().as_ref()
if let Some(exit_code) = args.get(0)
{
utils::exit(match exit_code.to_string().parse::<i32>(){
Ok(m) => m,
Err(_) => {
utils::zash_error("exit: numeric argument required");
return 1;
2
}
});
} else {
Expand Down
7 changes: 4 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use signal_hook::{consts, iterator::Signals};
use structopt::StructOpt;

// mod builtins;
mod builtins;
mod opts;
mod parser;
mod parsers;
mod scripting;
mod shell;
mod utils;
Expand All @@ -20,7 +20,8 @@ fn main() {
let opts = opts::Opts::from_args();

if let Some(command) = opts.command {
shell::run_line(command);
let mut shell = shell::Shell::new();
shell.run_line(command);
utils::exit(0);
};

Expand Down
43 changes: 0 additions & 43 deletions src/parser/parser.rs

This file was deleted.

10 changes: 10 additions & 0 deletions src/parsers/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pub type Result<T> = std::result::Result<T, SyntaxError>;

#[derive(Debug, Clone)]
pub struct SyntaxError;

impl std::fmt::Display for SyntaxError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "SyntaxError: Unexpected end of input")
}
}
Loading

0 comments on commit 4224f0c

Please sign in to comment.