1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:28:10 +00:00

Shell: Skip caching PATH and history load/save when not interactive

Non-interactive shells (i.e. when running scripts) do not need this
functionality, so they are a boatload of wasted time.
This significantly reduces the script startup and shutdown times when
there are lots of executables in PATH or lots of entries in the history.
This commit is contained in:
AnotherTest 2021-03-07 09:42:24 +03:30 committed by Andreas Kling
parent 48347fb1c7
commit e1512d5968
4 changed files with 61 additions and 41 deletions

View file

@ -60,21 +60,6 @@ int main(int argc, char** argv)
s_shell->editor()->save_history(s_shell->get_history_path());
});
editor = Line::Editor::construct();
editor->initialize();
auto shell = Shell::Shell::construct(*editor);
s_shell = shell.ptr();
s_shell->setup_signals();
#ifndef __serenity__
sigset_t blocked;
sigemptyset(&blocked);
sigaddset(&blocked, SIGTTOU);
sigaddset(&blocked, SIGTTIN);
pthread_sigmask(SIG_BLOCK, &blocked, nullptr);
#endif
#ifdef __serenity__
if (pledge("stdio rpath wpath cpath proc exec tty accept sigaction unix fattr", nullptr) < 0) {
perror("pledge");
@ -82,23 +67,43 @@ int main(int argc, char** argv)
}
#endif
shell->termios = editor->termios();
shell->default_termios = editor->default_termios();
RefPtr<::Shell::Shell> shell;
bool attempt_interactive = false;
editor->on_display_refresh = [&](auto& editor) {
editor.strip_styles();
if (shell->should_format_live()) {
auto line = editor.line();
ssize_t cursor = editor.cursor();
editor.clear_line();
editor.insert(shell->format(line, cursor));
if (cursor >= 0)
editor.set_cursor(cursor);
}
shell->highlight(editor);
};
editor->on_tab_complete = [&](const Line::Editor&) {
return shell->complete();
auto initialize = [&] {
editor = Line::Editor::construct();
editor->initialize();
shell = Shell::Shell::construct(*editor, attempt_interactive);
s_shell = shell.ptr();
s_shell->setup_signals();
#ifndef __serenity__
sigset_t blocked;
sigemptyset(&blocked);
sigaddset(&blocked, SIGTTOU);
sigaddset(&blocked, SIGTTIN);
pthread_sigmask(SIG_BLOCK, &blocked, nullptr);
#endif
shell->termios = editor->termios();
shell->default_termios = editor->default_termios();
editor->on_display_refresh = [&](auto& editor) {
editor.strip_styles();
if (shell->should_format_live()) {
auto line = editor.line();
ssize_t cursor = editor.cursor();
editor.clear_line();
editor.insert(shell->format(line, cursor));
if (cursor >= 0)
editor.set_cursor(cursor);
}
shell->highlight(editor);
};
editor->on_tab_complete = [&](const Line::Editor&) {
return shell->complete();
};
};
const char* command_to_run = nullptr;
@ -118,8 +123,6 @@ int main(int argc, char** argv)
parser.parse(argc, argv);
shell->set_live_formatting(should_format_live);
if (format) {
auto file = Core::File::open(format, Core::IODevice::ReadOnly);
if (file.is_error()) {
@ -127,6 +130,8 @@ int main(int argc, char** argv)
return 1;
}
initialize();
ssize_t cursor = -1;
puts(shell->format(file.value()->read_all(), cursor).characters());
return 0;
@ -152,6 +157,12 @@ int main(int argc, char** argv)
}
}
auto execute_file = file_to_read_from && StringView { "-" } != file_to_read_from;
attempt_interactive = !execute_file;
initialize();
shell->set_live_formatting(should_format_live);
shell->current_script = argv[0];
if (!skip_rc_files) {
@ -179,7 +190,7 @@ int main(int argc, char** argv)
return shell->run_command(command_to_run);
}
if (file_to_read_from && StringView { "-" } != file_to_read_from) {
if (execute_file) {
if (shell->run_file(file_to_read_from))
return 0;
return 1;