Skip to content

Commit

Permalink
Shell: Add support for special parameter that expands to return-code …
Browse files Browse the repository at this point in the history
…of last program executed
  • Loading branch information
MinusGix authored and awesomekling committed Sep 10, 2019
1 parent 2bd181a commit 91a609d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
1 change: 1 addition & 0 deletions Shell/GlobalState.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct GlobalState {
struct termios termios;
bool was_interrupted { false };
bool was_resized { false };
int last_return_code { 0 };
};

extern GlobalState g;
4 changes: 4 additions & 0 deletions Shell/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ static Vector<String> expand_parameters(const StringView& param)
return { param };

String variable_name = String(param.substring_view(1, param.length() - 1));
if (variable_name == "?")
return { String::number(g.last_return_code) };

char* env_value = getenv(variable_name.characters());
if (env_value == nullptr)
Expand Down Expand Up @@ -546,6 +548,8 @@ static int run_command(const String& cmd)
}
}

g.last_return_code = return_value;

// FIXME: Should I really have to tcsetpgrp() after my child has exited?
// Is the terminal controlling pgrp really still the PGID of the dead process?
tcsetpgrp(0, getpid());
Expand Down

0 comments on commit 91a609d

Please sign in to comment.