diff options
author | AnotherTest <ali.mpfard@gmail.com> | 2020-06-17 19:37:44 +0430 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-07-05 15:43:14 +0200 |
commit | bc3285abb02c13918cf2653deb39dd9c9d71c202 (patch) | |
tree | 0d7571952661133f3c47e5eaeb90f0dbcdcb76ac /Shell | |
parent | 3d6a035d0f2b8022ce7026c675ab395d30d2582c (diff) | |
download | serenity-bc3285abb02c13918cf2653deb39dd9c9d71c202.zip |
Shell: Read and evaluate an init file on start
This behaviour is overridable with the `--skip-init' flag.
The default file is at '~/shell-init.sh'
Diffstat (limited to 'Shell')
-rw-r--r-- | Shell/Shell.cpp | 16 | ||||
-rw-r--r-- | Shell/Shell.h | 3 | ||||
-rw-r--r-- | Shell/main.cpp | 26 |
3 files changed, 33 insertions, 12 deletions
diff --git a/Shell/Shell.cpp b/Shell/Shell.cpp index b6ce3d8437..ac945dd893 100644 --- a/Shell/Shell.cpp +++ b/Shell/Shell.cpp @@ -460,6 +460,22 @@ RefPtr<Job> Shell::run_command(AST::Command& command) return *job; } +bool Shell::run_file(const String& filename) +{ + auto file_result = Core::File::open(filename, Core::File::ReadOnly); + if (file_result.is_error()) { + fprintf(stderr, "Failed to open %s: %s\n", filename.characters(), file_result.error().characters()); + return false; + } + auto file = file_result.value(); + for (;;) { + auto line = file->read_line(4096); + if (line.is_null()) + break; + run_command(String::copy(line, Chomp)); + } + return true; +} void Shell::take_back_stdin() { tcsetpgrp(0, m_pid); diff --git a/Shell/Shell.h b/Shell/Shell.h index e052428c15..6be974027b 100644 --- a/Shell/Shell.h +++ b/Shell/Shell.h @@ -64,8 +64,11 @@ class Shell : public Core::Object { C_OBJECT(Shell); public: + constexpr static auto init_file_path = "~/shell-init.sh"; + int run_command(const StringView&); RefPtr<Job> run_command(AST::Command&); + bool run_file(const String&); bool run_builtin(int argc, const char** argv, int& retval); void block_on_job(RefPtr<Job>); String prompt() const; diff --git a/Shell/main.cpp b/Shell/main.cpp index 20ef531e51..4ec41e2154 100644 --- a/Shell/main.cpp +++ b/Shell/main.cpp @@ -119,13 +119,24 @@ int main(int argc, char** argv) const char* command_to_run = nullptr; const char* file_to_read_from = nullptr; + bool skip_init_file = false; Core::ArgsParser parser; parser.add_option(command_to_run, "String to read commands from", "command-string", 'c', "command-string"); parser.add_positional_argument(file_to_read_from, "File to read commands from", "file", Core::ArgsParser::Required::No); + parser.add_option(skip_init_file, "Skip running ~/shell-init.sh", "skip-init", 0); parser.parse(argc, argv); + if (!skip_init_file) { + String file_path = Shell::init_file_path; + if (file_path.starts_with('~')) + file_path = shell->expand_tilde(file_path); + if (!shell->run_file(file_path)) { + fprintf(stderr, "Shell: Failed to execute init file '%s'\n", Shell::init_file_path); + } + } + if (command_to_run) { dbgprintf("sh -c '%s'\n", command_to_run); shell->run_command(command_to_run); @@ -133,18 +144,9 @@ int main(int argc, char** argv) } if (file_to_read_from && StringView { "-" } != file_to_read_from) { - auto file = Core::File::construct(file_to_read_from); - if (!file->open(Core::IODevice::ReadOnly)) { - fprintf(stderr, "Failed to open %s: %s\n", file->filename().characters(), file->error_string()); - return 1; - } - for (;;) { - auto line = file->read_line(4096); - if (line.is_null()) - break; - shell->run_command(String::copy(line, Chomp)); - } - return 0; + if (shell->run_file(file_to_read_from)) + return 0; + return 1; } editor->on_interrupt_handled = [&] { |