Shell: $? returns last return value

This commit is contained in:
Bananymous 2023-08-15 09:17:46 +03:00
parent b30f4cbfb5
commit 0fa5401800
1 changed files with 8 additions and 2 deletions

View File

@ -19,6 +19,7 @@ struct termios old_termios, new_termios;
extern char** environ; extern char** environ;
static const char* argv0 = nullptr; static const char* argv0 = nullptr;
static int last_return = 0;
BAN::Vector<BAN::String> parse_command(BAN::StringView); BAN::Vector<BAN::String> parse_command(BAN::StringView);
@ -29,6 +30,11 @@ BAN::Optional<BAN::String> parse_dollar(BAN::StringView command, size_t& i)
if (++i >= command.size()) if (++i >= command.size())
return "$"sv; return "$"sv;
if (command[i] == '?')
{
i++;
return BAN::String::formatted("{}", last_return);
}
if (isalnum(command[i])) if (isalnum(command[i]))
{ {
size_t len = 1; size_t len = 1;
@ -643,7 +649,7 @@ int main(int argc, char** argv)
{ {
tcsetattr(0, TCSANOW, &old_termios); tcsetattr(0, TCSANOW, &old_termios);
auto parsed_arguments = parse_command(buffers[index]); auto parsed_arguments = parse_command(buffers[index]);
execute_command(parsed_arguments); last_return = execute_command(parsed_arguments);
tcsetattr(0, TCSANOW, &new_termios); tcsetattr(0, TCSANOW, &new_termios);
MUST(history.push_back(buffers[index])); MUST(history.push_back(buffers[index]));
buffers = history; buffers = history;