mesalock-linux / mesabox

A collection of core system utilities written in Rust for Unix-like systems (and now Windows)
BSD 3-Clause "New" or "Revised" License
137 stars 19 forks source link

Implement command substitution #13

Open Arcterus opened 6 years ago

Arcterus commented 6 years ago

The only issue at the moment is getting the output. Running the program and waiting for it should work fine.

Arcterus commented 6 years ago

I've got this working for the most part. I need to separate it from my piping-related changes though. Backtick-style substitution still doesn't work at the moment (due to parsing). There are also some issues with setting variables and whatnot in a substitution as I want to avoid forking (and thus creating a real subshell) unless absolutely necessary.

Arcterus commented 6 years ago

Basically what needs to happen to avoid forking is we need to have locality for variables (in addition to file descriptors, which already have locality). There are two easy ways to do this:

  1. Use the Locality type currently used for file descriptors and, like file descriptors, loop through every variable and increment the count for Locality::Local variables. This is pretty easy to do as we already have the necessary types and such, but could become slower than forking given enough variables.
  2. Give the current Environment a parent Environment and check for variables in the current Environment all the way back up to the global Environment. Disadvantage of this is that if there are many scopes finding variables can be very slow (not updating though, as any time we set a variable we'd just do env.set_local_fd() as the scopes are only used for subshells). If we do this we'd probably want to write a custom HashMap that allows us to provide the hashed key (so that we don't need to keep hashing the key over and over again when going up the chain of scopes).