diff options
author | AnotherTest <ali.mpfard@gmail.com> | 2020-06-17 18:51:44 +0430 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-07-05 15:43:14 +0200 |
commit | 2915dcfcc314deb4b41efdf14768e527614dc3fc (patch) | |
tree | 2209513d6907cd93bd47231e740d98075b6f7bd2 /Shell/Builtin.cpp | |
parent | a4627f24390035a8a8402e019be42b03f525f034 (diff) | |
download | serenity-2915dcfcc314deb4b41efdf14768e527614dc3fc.zip |
Shell: Add the alias builtin and resolve aliases
This follows the other shells in alias resolution, and resolves the
alias only once.
Diffstat (limited to 'Shell/Builtin.cpp')
-rw-r--r-- | Shell/Builtin.cpp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/Shell/Builtin.cpp b/Shell/Builtin.cpp index 14d7aece5c..4be6e4ff4f 100644 --- a/Shell/Builtin.cpp +++ b/Shell/Builtin.cpp @@ -33,6 +33,40 @@ extern RefPtr<Line::Editor> editor; +int Shell::builtin_alias(int argc, const char** argv) +{ + Vector<const char*> arguments; + + Core::ArgsParser parser; + parser.add_positional_argument(arguments, "List of name[=values]'s", "name[=value]", Core::ArgsParser::Required::No); + + if (!parser.parse(argc, const_cast<char**>(argv), false)) + return 1; + + if (arguments.is_empty()) { + for (auto& alias : m_aliases) + printf("%s=%s\n", escape_token(alias.key).characters(), escape_token(alias.value).characters()); + return 0; + } + + bool fail = false; + for (auto& argument : arguments) { + auto parts = String { argument }.split_limit('=', 2, true); + if (parts.size() == 1) { + auto alias = m_aliases.get(parts[0]); + if (alias.has_value()) { + printf("%s=%s\n", escape_token(parts[0]).characters(), escape_token(alias.value()).characters()); + } else { + fail = true; + } + } else { + m_aliases.set(parts[0], parts[1]); + } + } + + return fail ? 1 : 0; +} + int Shell::builtin_bg(int argc, const char** argv) { int job_id = -1; |