Skip to content

Commit

Permalink
implement 'source' builtin command (#18)
Browse files Browse the repository at this point in the history
* implement 'source' builtin command

* source to throw error if not cicadarc file

* add source command to completer, update changelong

* 'scripting is a WIP' error if source called on non RC file

* forgot to return 1 from source if not cicadarc file
  • Loading branch information
Nicholas Klingler authored and Hugo Wang committed Mar 20, 2019
1 parent 7da18e5 commit 3165323
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

- Made brace expansion behavior align with bash.
- Fixed an issue of path completion in middle of input.
- Implemented builtin command `source`

## 0.9.2

Expand Down
1 change: 1 addition & 0 deletions src/builtins/mod.rs
Expand Up @@ -8,3 +8,4 @@ pub mod fg;
pub mod history;
pub mod jobs;
pub mod vox;
pub mod source;
27 changes: 27 additions & 0 deletions src/builtins/source.rs
@@ -0,0 +1,27 @@
use std::io::Write;

use crate::shell;
use crate::parsers;
use crate::rcfile;
use crate::types::Tokens;


pub fn run(sh: &mut shell::Shell, tokens: &Tokens) -> i32 {
let args = parsers::parser_line::tokens_to_args(&tokens);
if args.len() > 2 {
println_stderr!("cicada: source: too many arguments");
return 1;
}
if args.len() < 2 {
println_stderr!("cicada: source: no file specified");
return 1;
}
if !args[1].ends_with("cicadarc") {
println_stderr!("cicada: source command only supports cicadarc files");
println_stderr!("scripting in cicada is still a work in progress");
return 1;
}

rcfile::load_file(sh, &args[1], 1);
0
}
2 changes: 1 addition & 1 deletion src/completers/path.rs
Expand Up @@ -165,7 +165,7 @@ fn complete_bin(sh: &shell::Shell, path: &str) -> Vec<Completion> {
});
}
let builtins = vec![
"cd", "cinfo", "exec", "exit", "export", "fg", "history", "jobs", "fg", "vox",
"cd", "cinfo", "exec", "exit", "export", "fg", "history", "jobs", "fg", "vox", "source"
];
for item in &builtins {
if !item.starts_with(fname) {
Expand Down
3 changes: 3 additions & 0 deletions src/execute.rs
Expand Up @@ -191,6 +191,9 @@ pub fn run_proc(sh: &mut shell::Shell, line: &str, tty: bool) -> i32 {
if cmd == "vox" && tokens.len() > 1 && (tokens[1].1 == "enter" || tokens[1].1 == "exit") {
return builtins::vox::run(sh, &tokens);
}
if cmd == "source" {
return builtins::source::run(sh, &tokens);
}

// for any other situations
let mut background = false;
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Expand Up @@ -72,6 +72,7 @@ mod jobc;
mod libs;
mod parsers;
mod shell;
mod rcfile;

/// Represents an error calling `exec`.
pub use crate::types::CommandResult;
Expand Down
2 changes: 1 addition & 1 deletion src/rcfile.rs
Expand Up @@ -10,7 +10,7 @@ use crate::shell;
use crate::tools;
use crate::types;

fn load_file(sh: &mut shell::Shell, file_path: &str, count: i32) {
pub fn load_file(sh: &mut shell::Shell, file_path: &str, count: i32) {
if count > 99 {
// to prevent dead include loop
println_stderr!("loaded too many rc files");
Expand Down

0 comments on commit 3165323

Please sign in to comment.