diff options
| author | _ov4 <ov4@ov4.org> | 2026-09-01 05:54:21 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-09-01 05:54:21 +0000 |
| commit | f206526c58e7c116f182c4b4dc2ce4d825fd6659 (patch) | |
| tree | fc5fce24565b431c4a33696bdcff589fe4dc5864 | |
| parent | 30726f56c8c522e81e65055ada52f56d95f68575 (diff) | |
refactor builtin commands
| -rw-r--r-- | include/command/builtin.h | 9 | ||||
| -rw-r--r-- | src/command/builtin.cc | 22 | ||||
| -rw-r--r-- | src/command/exit.cc | 2 | ||||
| -rw-r--r-- | src/command/jobs.cc | 2 | ||||
| -rw-r--r-- | src/command/quit.cc | 2 |
5 files changed, 25 insertions, 12 deletions
diff --git a/include/command/builtin.h b/include/command/builtin.h index f90c9bf..49a8e13 100644 --- a/include/command/builtin.h +++ b/include/command/builtin.h @@ -5,6 +5,9 @@ #include <array> #include <cstring> #include <iostream> +#include <map> +#include <unordered_map> +#include <functional> namespace ov4 { @@ -18,10 +21,10 @@ bool exe_bultin_command(char **argv); namespace ov4::command { -void quit(); -void jobs(); +void quit(char **argv); +void jobs(char **argv); void bg(char **argv); void fg(char **argv); -void exit(); +void exit(char **argv); } diff --git a/src/command/builtin.cc b/src/command/builtin.cc index 3e87850..a7ba56d 100644 --- a/src/command/builtin.cc +++ b/src/command/builtin.cc @@ -7,6 +7,15 @@ using namespace std; //array<string> builtin_cmd_list{"quit", "fg", "bg", "jobs"}; +unordered_map<string, function<void(char**)>> cmd = +{ + { "quit", ov4::command::quit }, + { "exit", ov4::command::exit }, + { "jobs", ov4::command::jobs }, + { "bg", ov4::command::bg }, + { "fg", ov4::command::fg } +}; + /* * builtin_cmd - If the user has typed a built-in command then execute * it immediately. @@ -14,12 +23,13 @@ using namespace std; bool exe_bultin_command(char **argv) { if (argv == nullptr || argv[0] == nullptr) exit(1); // WTF??? - if (strcmp(argv[0], "quit") == 0) { ov4::command::quit(); return true; } - if (strcmp(argv[0], "exit") == 0) { ov4::command::exit(); return true; } - if (strcmp(argv[0], "jobs") == 0) { ov4::command::jobs(); return true; } - if (strcmp(argv[0], "bg") == 0) { ov4::command::bg(argv); return true; } - if (strcmp(argv[0], "fg") == 0) { ov4::command::fg(argv); return true; } - else return false; + string s = argv[0]; + if (cmd.find(s) != cmd.end()) + { + cmd[s](argv); + return true; + } + return false; } diff --git a/src/command/exit.cc b/src/command/exit.cc index 5d817a6..e00a0f2 100644 --- a/src/command/exit.cc +++ b/src/command/exit.cc @@ -3,7 +3,7 @@ namespace ov4::command { -void exit() +void exit(char **argv) { std::exit(0); } diff --git a/src/command/jobs.cc b/src/command/jobs.cc index f618d55..ccdda75 100644 --- a/src/command/jobs.cc +++ b/src/command/jobs.cc @@ -7,7 +7,7 @@ namespace ov4::command using namespace ov4; -void jobs() +void jobs(char **argv) { listjobs(ov4::jobs); } diff --git a/src/command/quit.cc b/src/command/quit.cc index 8aee14d..0ac918b 100644 --- a/src/command/quit.cc +++ b/src/command/quit.cc @@ -5,7 +5,7 @@ namespace ov4::command using namespace ov4; -void quit() +void quit(char **argv) { std::exit(0); } |
