Is there a way to make the exec system call work like a function call that returns instead of totally replacing the current process image? My motivation is trying to integrate two shells with incompatible languages. The idea is that shell A would be able to execute scripts written in the shell B language and still keep any changes to the environment variables, working directory, etc that the shell B script makes.
So far all the solutions I could think of have some big limitations.
The first solution is to have shell A exec into shell B and then have shell B exec back into shell A, like I do in this question I asked earlier. The problem is that this throws away any state from the initial shell A process, such as the position in the currently running script, local variables, aliases, etc.
The second solution is to have shell A run shell B as a traditional subprocess using fork-exec and have shell B serialize its environment just before it exits and send it back to the shell A process. Two upsides of this are that it preserves the state of the parent process and that we can also serialize application-level data such as functions and aliases if we want to. The problem is that there is a a lot of stuff that a subprocess inherits from the parent and its hard to make sure that we didn't forget to serialize one of those.