2024-10-13 21:56:59 +03:00
|
|
|
#include "Builtin.h"
|
|
|
|
#include "Execute.h"
|
|
|
|
#include "Input.h"
|
|
|
|
#include "TokenParser.h"
|
2023-06-19 01:39:24 +03:00
|
|
|
|
2024-10-13 21:56:59 +03:00
|
|
|
#include <signal.h>
|
2023-06-05 01:42:57 +03:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2023-05-16 19:22:46 +03:00
|
|
|
|
2024-10-13 21:56:59 +03:00
|
|
|
int g_pid;
|
|
|
|
int g_argc;
|
|
|
|
char** g_argv;
|
2023-07-06 00:39:04 +03:00
|
|
|
|
2024-10-13 21:56:59 +03:00
|
|
|
int main(int argc, char** argv)
|
2023-10-03 10:24:10 +03:00
|
|
|
{
|
2024-10-13 21:56:59 +03:00
|
|
|
g_pid = getpid();
|
|
|
|
g_argc = argc;
|
|
|
|
g_argv = argv;
|
2023-10-03 10:24:10 +03:00
|
|
|
|
|
|
|
{
|
2024-10-13 21:56:59 +03:00
|
|
|
struct sigaction sa;
|
|
|
|
sa.sa_flags = 0;
|
2024-01-24 14:43:46 +02:00
|
|
|
|
2024-10-13 21:56:59 +03:00
|
|
|
sa.sa_handler = [](int) {};
|
|
|
|
sigaction(SIGINT, &sa, nullptr);
|
2023-10-03 10:24:36 +03:00
|
|
|
|
2024-10-13 21:56:59 +03:00
|
|
|
sa.sa_handler = SIG_IGN;
|
|
|
|
sigaction(SIGTTOU, &sa, nullptr);
|
2023-10-03 10:24:36 +03:00
|
|
|
}
|
2024-10-06 18:18:56 +03:00
|
|
|
|
2024-10-13 21:56:59 +03:00
|
|
|
Builtin::get().initialize();
|
2024-10-06 18:18:56 +03:00
|
|
|
|
2024-10-13 21:56:59 +03:00
|
|
|
for (int i = 1; i < argc; i++)
|
2024-10-06 18:18:56 +03:00
|
|
|
{
|
2024-10-13 21:56:59 +03:00
|
|
|
if (argv[i][0] != '-')
|
2024-10-06 18:18:56 +03:00
|
|
|
{
|
2024-10-13 21:56:59 +03:00
|
|
|
g_argc = g_argc - i;
|
|
|
|
g_argv = g_argv + i;
|
2024-10-06 18:18:56 +03:00
|
|
|
|
2024-10-13 21:56:59 +03:00
|
|
|
Execute execute;
|
|
|
|
(void)execute.source_script(argv[i]);
|
|
|
|
return execute.last_return_value();
|
2024-10-06 18:18:56 +03:00
|
|
|
}
|
|
|
|
|
2024-10-13 21:56:59 +03:00
|
|
|
if (strcmp(argv[i], "-c") == 0)
|
2024-10-06 18:18:56 +03:00
|
|
|
{
|
2024-10-13 21:56:59 +03:00
|
|
|
if (i + 1 >= argc)
|
2024-10-06 18:18:56 +03:00
|
|
|
{
|
2024-10-13 21:56:59 +03:00
|
|
|
printf("-c requires an argument\n");
|
|
|
|
return 1;
|
2024-10-06 18:18:56 +03:00
|
|
|
}
|
|
|
|
|
2024-10-13 21:56:59 +03:00
|
|
|
g_argc = g_argc - (i + 2);
|
|
|
|
g_argv = g_argv + (i + 2);
|
2023-06-12 00:45:47 +03:00
|
|
|
|
2024-10-13 21:56:59 +03:00
|
|
|
bool got_input = false;
|
2024-01-24 14:43:46 +02:00
|
|
|
|
2024-10-13 21:56:59 +03:00
|
|
|
TokenParser parser(
|
|
|
|
[&](BAN::Optional<BAN::StringView>) -> BAN::Optional<BAN::String>
|
2023-06-12 01:31:33 +03:00
|
|
|
{
|
2024-10-13 21:56:59 +03:00
|
|
|
if (got_input)
|
|
|
|
return {};
|
|
|
|
got_input = true;
|
2023-06-12 01:31:33 +03:00
|
|
|
|
2024-10-13 21:56:59 +03:00
|
|
|
BAN::String input;
|
|
|
|
MUST(input.append(argv[i + 1]));
|
|
|
|
return input;
|
2023-09-29 19:20:48 +03:00
|
|
|
}
|
2024-10-13 21:56:59 +03:00
|
|
|
);
|
|
|
|
if (!parser.main_loop(true))
|
|
|
|
return 126;
|
|
|
|
return parser.execute().last_return_value();
|
2023-07-06 23:18:37 +03:00
|
|
|
}
|
2024-10-04 17:54:01 +03:00
|
|
|
else if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2023-07-06 23:18:37 +03:00
|
|
|
}
|
|
|
|
|
2024-10-13 21:56:59 +03:00
|
|
|
Input input;
|
|
|
|
TokenParser parser(
|
|
|
|
[&](BAN::Optional<BAN::StringView> prompt) -> BAN::Optional<BAN::String>
|
2023-06-05 18:53:52 +03:00
|
|
|
{
|
2024-10-13 21:56:59 +03:00
|
|
|
return input.get_input(prompt);
|
2023-06-05 18:53:52 +03:00
|
|
|
}
|
2024-10-13 21:56:59 +03:00
|
|
|
);
|
2023-06-05 18:53:52 +03:00
|
|
|
|
2024-10-13 21:56:59 +03:00
|
|
|
if (const char* home_env = getenv("HOME"))
|
|
|
|
{
|
|
|
|
BAN::String config_file_path;
|
|
|
|
MUST(config_file_path.append(home_env));
|
|
|
|
MUST(config_file_path.append("/.shellrc"_sv));
|
2024-10-07 16:09:36 +03:00
|
|
|
|
2024-10-13 21:56:59 +03:00
|
|
|
struct stat st;
|
|
|
|
if (stat(config_file_path.data(), &st) == 0)
|
2024-10-06 18:18:56 +03:00
|
|
|
{
|
2024-10-13 21:56:59 +03:00
|
|
|
if (auto ret = parser.execute().source_script(config_file_path.sv()); ret.is_error())
|
|
|
|
fprintf(stderr, "could not source config file at '%s': %s\n", config_file_path.data(), ret.error().get_message());
|
2023-06-05 01:42:57 +03:00
|
|
|
}
|
2023-05-16 19:22:46 +03:00
|
|
|
}
|
2024-10-13 21:56:59 +03:00
|
|
|
|
|
|
|
if (!parser.main_loop(false))
|
|
|
|
return 126;
|
|
|
|
return 0;
|
2023-05-16 19:22:46 +03:00
|
|
|
}
|