Shell: Cleanup argument handling and exiting

This commit is contained in:
Bananymous 2024-10-04 17:54:01 +03:00
parent ab9a6d583b
commit f709e88994
1 changed files with 37 additions and 29 deletions

View File

@ -20,17 +20,11 @@ struct termios old_termios, new_termios;
extern char** environ; extern char** environ;
static const char* argv0 = nullptr; static char s_shell_path[PATH_MAX];
static int last_return = 0; static int last_return = 0;
static BAN::String hostname; static BAN::String hostname;
static void clean_exit(int exit_code)
{
tcsetattr(0, TCSANOW, &old_termios);
exit(exit_code);
}
BAN::Vector<BAN::Vector<BAN::String>> parse_command(BAN::StringView); BAN::Vector<BAN::Vector<BAN::String>> parse_command(BAN::StringView);
BAN::Optional<BAN::String> parse_dollar(BAN::StringView command, size_t& i) BAN::Optional<BAN::String> parse_dollar(BAN::StringView command, size_t& i)
@ -104,7 +98,7 @@ BAN::Optional<BAN::String> parse_dollar(BAN::StringView command, size_t& i)
char temp[3] { '-', 'c', '\0' }; char temp[3] { '-', 'c', '\0' };
BAN::Vector<char*> argv; BAN::Vector<char*> argv;
MUST(argv.push_back((char*)argv0)); MUST(argv.push_back(s_shell_path));
MUST(argv.push_back(temp)); MUST(argv.push_back(temp));
MUST(argv.push_back((char*)subcommand.data())); MUST(argv.push_back((char*)subcommand.data()));
MUST(argv.push_back(nullptr)); MUST(argv.push_back(nullptr));
@ -309,7 +303,7 @@ BAN::Optional<int> execute_builtin(BAN::Vector<BAN::String>& args, int fd_in, in
} }
else if (args.front() == "exit"_sv) else if (args.front() == "exit"_sv)
{ {
clean_exit(0); exit(0);
} }
else if (args.front() == "export"_sv) else if (args.front() == "export"_sv)
{ {
@ -784,7 +778,7 @@ void print_prompt()
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
argv0 = argv[0]; realpath(argv[0], s_shell_path);
if (signal(SIGINT, [](int) {}) == SIG_ERR) if (signal(SIGINT, [](int) {}) == SIG_ERR)
perror("signal"); perror("signal");
@ -798,33 +792,50 @@ int main(int argc, char** argv)
MUST(hostname.append(hostname_buffer)); MUST(hostname.append(hostname_buffer));
} }
if (argc >= 2) new_termios = old_termios;
new_termios.c_lflag &= ~(ECHO | ICANON);
tcsetattr(0, TCSANOW, &new_termios);
atexit([]() { tcsetattr(0, TCSANOW, &old_termios); });
for (int i = 1; i < argc; i++)
{ {
if (strcmp(argv[1], "-c") == 0) if (argv[i][0] != '-')
return source_script(BAN::String(argv[i]));
if (strcmp(argv[i], "-c") == 0)
{ {
if (argc == 2) if (i + 1 >= argc)
{ {
printf("-c requires an argument\n"); printf("-c requires an argument\n");
return 1; return 1;
} }
BAN::String command; auto commands = parse_command(BAN::String(argv[i + 1]));
MUST(command.append(argv[2]));
auto commands = parse_command(command);
return execute_piped_commands(commands); return execute_piped_commands(commands);
} }
else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0)
printf("unknown argument '%s'\n", argv[1]); {
return 1; printf("banan-sh 1.0\n");
return 0;
}
else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0)
{
printf("usage: %s [options...]\n", argv[0]);
printf(" -c run following argument as an argument\n");
printf(" -v, --version print version information and exit\n");
printf(" -h, --help print this message and exit\n");
return 0;
}
else
{
printf("unknown argument '%s'\n", argv[i]);
return 1;
}
} }
source_shellrc(); source_shellrc();
new_termios = old_termios;
new_termios.c_lflag &= ~(ECHO | ICANON);
tcsetattr(0, TCSANOW, &new_termios);
BAN::Vector<BAN::String> buffers, history; BAN::Vector<BAN::String> buffers, history;
MUST(buffers.emplace_back(""_sv)); MUST(buffers.emplace_back(""_sv));
size_t index = 0; size_t index = 0;
@ -842,7 +853,7 @@ int main(int argc, char** argv)
if (errno != EINTR) if (errno != EINTR)
{ {
perror("getchar"); perror("getchar");
clean_exit(1); return 1;
} }
clearerr(stdin); clearerr(stdin);
@ -956,8 +967,7 @@ int main(int argc, char** argv)
break; break;
case '\x04': // ^D case '\x04': // ^D
putchar('\n'); putchar('\n');
clean_exit(0); return 0;
break;
case '\x7F': // backspace case '\x7F': // backspace
if (col <= 0) if (col <= 0)
break; break;
@ -993,6 +1003,4 @@ int main(int argc, char** argv)
break; break;
} }
} }
clean_exit(0);
} }