Compare commits

...

3 Commits

3 changed files with 81 additions and 7 deletions

Binary file not shown.

View File

@ -265,6 +265,8 @@ BAN::Vector<BAN::Vector<BAN::String>> parse_command(BAN::StringView command_view
int execute_command(BAN::Vector<BAN::String>& args, int fd_in, int fd_out); int execute_command(BAN::Vector<BAN::String>& args, int fd_in, int fd_out);
int source_script(const BAN::String& path);
BAN::Optional<int> execute_builtin(BAN::Vector<BAN::String>& args, int fd_in, int fd_out) BAN::Optional<int> execute_builtin(BAN::Vector<BAN::String>& args, int fd_in, int fd_out)
{ {
if (args.empty()) if (args.empty())
@ -312,6 +314,15 @@ BAN::Optional<int> execute_builtin(BAN::Vector<BAN::String>& args, int fd_in, in
ERROR_RETURN("setenv", 1); ERROR_RETURN("setenv", 1);
} }
} }
else if (args.front() == "source"sv)
{
if (args.size() != 2)
{
fprintf(fout, "usage: source FILE\n");
return 1;
}
return source_script(args[1]);
}
else if (args.front() == "env"sv) else if (args.front() == "env"sv)
{ {
char** current = environ; char** current = environ;
@ -634,6 +645,70 @@ int execute_piped_commands(BAN::Vector<BAN::Vector<BAN::String>>& commands)
return exit_codes.back(); return exit_codes.back();
} }
int parse_and_execute_command(BAN::StringView command)
{
if (command.empty())
return 0;
auto parsed_commands = parse_command(command);
if (parsed_commands.empty())
return 0;
tcsetattr(0, TCSANOW, &old_termios);
int ret = execute_piped_commands(parsed_commands);
tcsetattr(0, TCSANOW, &new_termios);
return ret;
}
int source_script(const BAN::String& path)
{
FILE* fp = fopen(path.data(), "r");
if (fp == nullptr)
ERROR_RETURN("fopen", 1);
int ret = 0;
BAN::String command;
char temp_buffer[128];
while (fgets(temp_buffer, sizeof(temp_buffer), fp))
{
MUST(command.append(temp_buffer));
if (command.back() != '\n')
continue;
command.pop_back();
if (!command.empty())
if (int temp = parse_and_execute_command(command))
ret = temp;
command.clear();
}
if (!command.empty())
if (int temp = parse_and_execute_command(command))
ret = temp;
fclose(fp);
return ret;
}
bool exists(const BAN::String& path)
{
struct stat st;
return stat(path.data(), &st) == 0;
}
int source_shellrc()
{
if (char* home = getenv("HOME"))
{
BAN::String path(home);
MUST(path.append("/.shellrc"sv));
if (exists(path))
return source_script(path);
}
return 0;
}
int character_length(BAN::StringView prompt) int character_length(BAN::StringView prompt)
{ {
int length { 0 }; int length { 0 };
@ -660,7 +735,7 @@ BAN::String get_prompt()
{ {
const char* raw_prompt = getenv("PS1"); const char* raw_prompt = getenv("PS1");
if (raw_prompt == nullptr) if (raw_prompt == nullptr)
return ""sv; return "$ "sv;
BAN::String prompt; BAN::String prompt;
for (int i = 0; raw_prompt[i]; i++) for (int i = 0; raw_prompt[i]; i++)
@ -796,7 +871,8 @@ int main(int argc, char** argv)
if (argc >= 1) if (argc >= 1)
setenv("SHELL", argv[0], true); setenv("SHELL", argv[0], true);
setenv("PS1", "\e[32m\\u@\\h\e[m:\e[34m\\~\e[m$ ", false);
source_shellrc();
tcgetattr(0, &old_termios); tcgetattr(0, &old_termios);
@ -901,10 +977,7 @@ int main(int argc, char** argv)
fputc('\n', stdout); fputc('\n', stdout);
if (!buffers[index].empty()) if (!buffers[index].empty())
{ {
tcsetattr(0, TCSANOW, &old_termios); last_return = parse_and_execute_command(buffers[index]);
auto commands = parse_command(buffers[index]);
last_return = execute_piped_commands(commands);
tcsetattr(0, TCSANOW, &new_termios);
MUST(history.push_back(buffers[index])); MUST(history.push_back(buffers[index]));
buffers = history; buffers = history;
MUST(buffers.emplace_back(""sv)); MUST(buffers.emplace_back(""sv));

View File

@ -82,8 +82,9 @@ int main()
perror("read"); perror("read");
else else
{ {
size_t percent_times_100 = 10000 * meminfo.phys_pages / meminfo.virt_pages;
printf(" vmem: %zu pages (%zu bytes)\n", meminfo.virt_pages, meminfo.page_size * meminfo.virt_pages); printf(" vmem: %zu pages (%zu bytes)\n", meminfo.virt_pages, meminfo.page_size * meminfo.virt_pages);
printf(" pmem: %zu pages (%zu bytes)\n", meminfo.phys_pages, meminfo.page_size * meminfo.phys_pages); printf(" pmem: %zu pages (%zu bytes) %zu.%02zu%%\n", meminfo.phys_pages, meminfo.page_size * meminfo.phys_pages, percent_times_100 / 100, percent_times_100 % 100);
} }
close(fd); close(fd);