summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnotherTest <ali.mpfard@gmail.com>2021-03-07 09:42:24 +0330
committerAndreas Kling <kling@serenityos.org>2021-03-07 10:58:42 +0100
commite1512d59688be4155a6b378de06b9a076063a640 (patch)
treedc2c8e64c77bcd137cf3cd0fdd1ae20f3c3f5301
parent48347fb1c7df6f9645e8b48f7bacf7e82ba33f3d (diff)
downloadserenity-e1512d59688be4155a6b378de06b9a076063a640.zip
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.
-rw-r--r--Userland/Shell/Builtin.cpp5
-rw-r--r--Userland/Shell/Shell.cpp16
-rw-r--r--Userland/Shell/Shell.h2
-rw-r--r--Userland/Shell/main.cpp81
4 files changed, 62 insertions, 42 deletions
diff --git a/Userland/Shell/Builtin.cpp b/Userland/Shell/Builtin.cpp
index 46322d7cb6..3b1cb9db33 100644
--- a/Userland/Shell/Builtin.cpp
+++ b/Userland/Shell/Builtin.cpp
@@ -299,9 +299,10 @@ int Shell::builtin_exit(int argc, const char** argv)
}
}
stop_all_jobs();
- m_editor->save_history(get_history_path());
- if (m_is_interactive)
+ if (m_is_interactive) {
+ m_editor->save_history(get_history_path());
printf("Good-bye!\n");
+ }
exit(exit_code);
return 0;
}
diff --git a/Userland/Shell/Shell.cpp b/Userland/Shell/Shell.cpp
index 840f24356c..6d7c423332 100644
--- a/Userland/Shell/Shell.cpp
+++ b/Userland/Shell/Shell.cpp
@@ -1222,6 +1222,9 @@ String Shell::unescape_token(const String& token)
void Shell::cache_path()
{
+ if (!m_is_interactive)
+ return;
+
if (!cached_path.is_empty())
cached_path.clear_with_capacity();
@@ -1691,7 +1694,7 @@ Shell::Shell()
cache_path();
}
-Shell::Shell(Line::Editor& editor)
+Shell::Shell(Line::Editor& editor, bool attempt_interactive)
: m_editor(editor)
{
uid = getuid();
@@ -1705,7 +1708,7 @@ Shell::Shell(Line::Editor& editor)
perror("gethostname");
auto istty = isatty(STDIN_FILENO);
- m_is_interactive = istty;
+ m_is_interactive = attempt_interactive && istty;
if (istty) {
rc = ttyname_r(0, ttyname, Shell::TTYNameSize);
@@ -1733,8 +1736,10 @@ Shell::Shell(Line::Editor& editor)
}
directory_stack.append(cwd);
- m_editor->load_history(get_history_path());
- cache_path();
+ if (m_is_interactive) {
+ m_editor->load_history(get_history_path());
+ cache_path();
+ }
m_editor->register_key_input_callback('\n', [](Line::Editor& editor) {
auto ast = Parser(editor.line()).parse();
@@ -1751,6 +1756,9 @@ Shell::~Shell()
return;
stop_all_jobs();
+ if (!m_is_interactive)
+ return;
+
m_editor->save_history(get_history_path());
}
diff --git a/Userland/Shell/Shell.h b/Userland/Shell/Shell.h
index 8429e57190..08f8f89c04 100644
--- a/Userland/Shell/Shell.h
+++ b/Userland/Shell/Shell.h
@@ -264,7 +264,7 @@ public:
#undef __ENUMERATE_SHELL_OPTION
private:
- Shell(Line::Editor&);
+ Shell(Line::Editor&, bool attempt_interactive);
Shell();
virtual ~Shell() override;
diff --git a/Userland/Shell/main.cpp b/Userland/Shell/main.cpp
index 2d43167842..581edd93c1 100644
--- a/Userland/Shell/main.cpp
+++ b/Userland/Shell/main.cpp
@@ -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();
-
- 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();
+ RefPtr<::Shell::Shell> shell;
+ bool attempt_interactive = false;
+
+ 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;