Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement 'source' builtin command #18

Merged
merged 5 commits into from Mar 20, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
26 changes: 26 additions & 0 deletions src/builtins/source.rs
@@ -0,0 +1,26 @@
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");
Copy link
Owner

Choose a reason for hiding this comment

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

I think here we should have a return 1; :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed 23ec537

Wouldn't be coding if I didn't introduce an error :)

}

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