Conversation
|
This you might need to revise/force push to fix the merge conflicts too. Other than that (and my other comment) looks good! |
cc40b84 to
25e41b6
Compare
siliconlad
left a comment
There was a problem hiding this comment.
I rebased this branch so fix the diffs.
|
I've merged most of the other PRs so we should be able to revert the temp fix. |
e74a9ce to
fb25ae4
Compare
fc0a41c to
3ad19ea
Compare
siliconlad
left a comment
There was a problem hiding this comment.
Sorry, just realized that we probably want to support alias substitution in slightly more complicated scenarios haha 😅
63ffbf0 to
2f702b3
Compare
|
Should the "Hello world" have double quotes when greet is run? |
2f702b3 to
ac3902b
Compare
It should not hahah, will have a look at that |
1efd75c to
31c6bbd
Compare
f9e5d05 to
fe785f4
Compare
|
Completely changed my approach so now expanding the aliases before they are tokenized. This seems much simpler and means the string is still tokenized properly and we don't need to retokenize in the alias expansion if we did it after the tokenization. Seems to work for all the tests I've done - using piping and recursive aliases etc. What do you think? |
363e6f7 to
4c48cc0
Compare
|
Found a couple more things to fix |
|
So many edge cases 😂 |
bdfbe22 to
3a37646
Compare
|
I think we should merge #82 so we can add all the edge-cases as tests. |
Merged, will add some tests now for most of the edge cases I can think of. |
fcf49f4 to
d4a320f
Compare
|
@siliconlad can you think of a way I can get around this problem I'm having... Currently if we do the last line doesn't work because |
d4a320f to
f8ebaa5
Compare
|
What I might try (in
Would that work? Maybe you also need to deal with aliases using aliases. Perhaps that's just repeated application of the steps above until you detect no more aliases (might have to detect loops / set maximum alias depth). |
f8ebaa5 to
99cfa47
Compare
2d3280c to
e0c9dd8
Compare
|
It's not the prettiest code in the world but functionally I think it finally works. Going to try make it a bit nicer to read, especially the |
8ffd384 to
4ac43fe
Compare
|
@siliconlad think we're good to go on this PR. Ready for review, hopefully for the final time lol |
siliconlad
left a comment
There was a problem hiding this comment.
I guess the main thing is it works.
I left some comments mainly about what bash does and whether we want to behave the same way or not. I think some of it will allow us to cut down on the complexity that alias adds (e.g. in pipe). What do you think?
| } | ||
| } | ||
| } else { | ||
| Err("Usage: alias [name[=value] ...]".into()) |
| fn pipe(&self, stdin: Option<ChildStdout>) -> Result<Option<ChildStdout>, Box<dyn Error>> { | ||
| fn pipe( | ||
| &self, | ||
| stdin: Option<ChildStdout>, | ||
| aliases: &mut HashMap<String, String>, | ||
| ) -> Result<Option<ChildStdout>, Box<dyn Error>> { | ||
| let (pipe_out_r, pipe_out_w) = pipe()?; | ||
| let (pipe_err_r, pipe_err_w) = pipe()?; | ||
| let (alias_r, alias_w) = pipe()?; | ||
|
|
||
| match unsafe { fork() }? { | ||
| ForkResult::Parent { child: _ } => { | ||
| drop(pipe_out_w); | ||
| drop(pipe_err_w); | ||
| drop(alias_w); | ||
|
|
||
| // Read updated aliases from child | ||
| let mut alias_reader = unsafe { File::from_raw_fd(alias_r.into_raw_fd()) }; | ||
| let mut serialized_aliases = String::new(); | ||
| alias_reader.read_to_string(&mut serialized_aliases)?; | ||
| if !serialized_aliases.is_empty() { | ||
| *aliases = serde_json::from_str(&serialized_aliases)?; | ||
| } | ||
|
|
||
| Ok(Some(ChildStdout::from(pipe_out_r))) | ||
| } | ||
| ForkResult::Child => { | ||
| drop(pipe_out_r); | ||
| drop(pipe_err_r); | ||
| drop(alias_r); | ||
|
|
||
| dup2(pipe_out_w.as_raw_fd(), 1)?; | ||
| dup2(pipe_err_w.as_raw_fd(), 2)?; | ||
| self.run_builtin(stdin)?; | ||
| self.run_builtin(stdin, aliases)?; | ||
|
|
||
| // Send updated aliases to parent | ||
| let serialized_aliases = serde_json::to_string(aliases)?; | ||
| let mut alias_writer = unsafe { File::from_raw_fd(alias_w.into_raw_fd()) }; | ||
| write!(alias_writer, "{}", serialized_aliases)?; | ||
|
|
There was a problem hiding this comment.
This is really cool haha. I'm assuming that this is for this particular scenario:
> alias r='echo' | r Hello
HelloBut this doesn't work in Bash, so we don't necessarily need to support it? Might cut down on the complexity.
| fn handle_alias_definition(temp_aliases: &mut HashMap<String, String>, args: &[String]) { | ||
| let full_command = args.join(" "); | ||
| if let Some(equals_pos) = full_command.find('=') { | ||
| let (alias_name, alias_value) = full_command.split_at(equals_pos); | ||
| temp_aliases.insert( | ||
| alias_name.to_string(), | ||
| alias_value[1..].trim_matches('\'').to_string(), | ||
| ); | ||
| } | ||
| } |
There was a problem hiding this comment.
It's solving the problem where we have something like alias greet='echo \"Hello, World!\"' && greet because expand tokens gets called before we add greet to the aliases, the second greet after the && doesn't get expanded so this checks if we have an alias on the left of the command and if we do then it expands the right hand side. So basically all handle_alias_definition is doing is getting the alias greet and the expansion of this which is echo "Hello, World!", this is then returned and used in expand_command_aliases to turn the whole command into alias greet='echo \"Hello, World!\"' && alias greet='echo \"Hello, World!\"
There was a problem hiding this comment.
This could probably be made into one function as we are doing the same thing in the alias buitlin command.
There was a problem hiding this comment.
I guess it doesn't hurt to have all these tests, but probably worth having tests that execute aliases (you should be able to use ;).
| while let Some(expansion) = temp_aliases.get(&words[0]) { | ||
| let expanded_words: Vec<String> = expansion.split_whitespace().map(String::from).collect(); | ||
| if expanded_words.is_empty() || expansion_count >= MAX_EXPANSIONS { | ||
| break; | ||
| } | ||
| words.splice(0..1, expanded_words); | ||
| expansion_count += 1; | ||
| } |
There was a problem hiding this comment.
Reading the reference manual, they actually don't do recursive expansions.
| if parts.len() == 2 { | ||
| let alias = parts[0]; | ||
| let command = parts[1]; | ||
| aliases.insert(alias.to_string(), command.to_string()); |
There was a problem hiding this comment.
The reference manual does not allow special characters in the name. Not sure if we want to enforce that or not :)
4ac43fe to
9552fac
Compare




No description provided.