From 5e94ca9f060d2952ef08913234c500a815f734bf Mon Sep 17 00:00:00 2001 From: _ov4 Date: Tue, 1 Sep 2026 00:39:44 +0000 Subject: refactor --- src/command/builtin.cc | 23 ++++++++++ src/command/quit.cc | 13 ++++++ src/eval.cc | 21 ++++----- src/global.cc | 1 - src/main.cc | 2 +- src/util.cc | 118 ------------------------------------------------- src/util/io.cc | 99 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 147 insertions(+), 130 deletions(-) create mode 100644 src/command/builtin.cc create mode 100644 src/command/quit.cc delete mode 100644 src/util.cc create mode 100644 src/util/io.cc (limited to 'src') diff --git a/src/command/builtin.cc b/src/command/builtin.cc new file mode 100644 index 0000000..b2c3149 --- /dev/null +++ b/src/command/builtin.cc @@ -0,0 +1,23 @@ +#include "command/builtin.h" + +namespace ov4 +{ + +using namespace std; + +//array builtin_cmd_list{"quit", "fg", "bg", "jobs"}; + +/* + * builtin_cmd - If the user has typed a built-in command then execute + * it immediately. + */ +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; } + else return false; +} + + + +} diff --git a/src/command/quit.cc b/src/command/quit.cc new file mode 100644 index 0000000..917f642 --- /dev/null +++ b/src/command/quit.cc @@ -0,0 +1,13 @@ +#include "command/builtin.h" + +namespace ov4::command +{ + +using namespace ov4; + +void quit() +{ + exit(0); +} + +} diff --git a/src/eval.cc b/src/eval.cc index 4350367..1b5884f 100644 --- a/src/eval.cc +++ b/src/eval.cc @@ -27,17 +27,18 @@ void eval(char *cmdline) int ground = parseline(cmdline, argv); if (argv[0] == nullptr) return; + if (exe_bultin_command(argv)) return; - if (builtin_cmd(argv[0])) - { - LOG << "built-in" << endl; - if (strcmp(argv[0], "quit") == 0) builtin_command_quit(); - else if (strcmp(argv[0], "jobs") == 0) listjobs(jobs); - else if (strcmp(argv[0], "fg") == 0) do_bgfg(argv); - else if (strcmp(argv[0], "bg") == 0) do_bgfg(argv); - else cerr << "built-in but not supported" << endl; - return; - } + // if (builtin_cmd(argv[0])) + // { + // LOG << "built-in" << endl; + // if (strcmp(argv[0], "quit") == 0) builtin_command_quit(); + // else if (strcmp(argv[0], "jobs") == 0) listjobs(jobs); + // else if (strcmp(argv[0], "fg") == 0) do_bgfg(argv); + // else if (strcmp(argv[0], "bg") == 0) do_bgfg(argv); + // else cerr << "built-in but not supported" << endl; + // return; + // } sigset_t prev; block_all(&prev); diff --git a/src/global.cc b/src/global.cc index 5b92331..0995b4d 100644 --- a/src/global.cc +++ b/src/global.cc @@ -6,6 +6,5 @@ namespace ov4 using namespace std; int verbose = 0; -vector builtin_cmd_list = {"quit", "fg", "bg", "jobs"}; } diff --git a/src/main.cc b/src/main.cc index f75dc7d..04aca2a 100644 --- a/src/main.cc +++ b/src/main.cc @@ -11,7 +11,7 @@ #include #include -#include "util.h" +#include "util/io.h" #include "job.h" #include "parse.h" #include "handler.h" diff --git a/src/util.cc b/src/util.cc deleted file mode 100644 index 9574ce6..0000000 --- a/src/util.cc +++ /dev/null @@ -1,118 +0,0 @@ -#include "util.h" - -namespace ov4 -{ - -using namespace std; - -void atomic_print(char *s, bool ignore_wrap) -{ - if (ignore_wrap) - for (int i = 0; s[i] != 0; i++) - if (s[i] == '\n') - { - write(STDOUT_FILENO, s, i); - return; - } - write(STDOUT_FILENO, s, strlen(s)); -} - -void atomic_print(int x) -{ - char msg[LENGTH]; - itoa(x, msg, DECIMAL); - atomic_print(msg); -} - -void reverse(char str[], int length) -{ - int start = 0; - int end = length - 1; - while (start < end) { - char temp = str[start]; - str[start] = str[end]; - str[end] = temp; - end--; - start++; - } -} - -void atomic_debug(char *s, bool ignore_wrap) -{ - if (verbose && GLOBAL_DEBUG) atomic_print(s, ignore_wrap); -} - -void atomic_debug(int x) -{ - if (verbose && GLOBAL_DEBUG) atomic_print(x); -} - -bool isnum(char *s) -{ - for (int i = 0; s[i] != 0; i++) - if (!isdigit(s[i])) - return false; - return true; -} - - -char *itoa(int num, char* str, int base) -{ - int i = 0; - bool isNegative = false; - - /* Handle 0 explicitly, otherwise empty string is - * printed for 0 */ - if (num == 0) { - str[i++] = '0'; - str[i] = '\0'; - return str; - } - - // In standard itoa(), negative numbers are handled - // only with base 10. Otherwise numbers are - // considered unsigned. - if (num < 0 && base == 10) { - isNegative = true; - num = -num; - } - - // Process individual digits - while (num != 0) { - int rem = num % base; - str[i++] = (rem > 9) ? (rem - 10) + 'a' : rem + '0'; - num = num / base; - } - - // If number is negative, append '-' - if (isNegative) - str[i++] = '-'; - - str[i] = '\0'; // Append string terminator - - // Reverse the string - reverse(str, i); - - return str; -} - -/* - * builtin_cmd - If the user has typed a built-in command then execute - * it immediately. - */ -int builtin_cmd(char *c_str) -{ - string s(c_str); - for (auto c : builtin_cmd_list) - if (c == s) - return 1; - return 0; /* not a builtin command */ -} - -void builtin_command_quit() -{ - exit(0); -} - - -} diff --git a/src/util/io.cc b/src/util/io.cc new file mode 100644 index 0000000..531bd47 --- /dev/null +++ b/src/util/io.cc @@ -0,0 +1,99 @@ +#include "util/io.h" + +namespace ov4 +{ + +using namespace std; + +void atomic_print(char *s, bool ignore_wrap) +{ + if (ignore_wrap) + for (int i = 0; s[i] != 0; i++) + if (s[i] == '\n') + { + write(STDOUT_FILENO, s, i); + return; + } + write(STDOUT_FILENO, s, strlen(s)); +} + +void atomic_print(int x) +{ + char msg[LENGTH]; + itoa(x, msg, DECIMAL); + atomic_print(msg); +} + +void reverse(char str[], int length) +{ + int start = 0; + int end = length - 1; + while (start < end) { + char temp = str[start]; + str[start] = str[end]; + str[end] = temp; + end--; + start++; + } +} + +void atomic_debug(char *s, bool ignore_wrap) +{ + if (verbose && GLOBAL_DEBUG) atomic_print(s, ignore_wrap); +} + +void atomic_debug(int x) +{ + if (verbose && GLOBAL_DEBUG) atomic_print(x); +} + +bool isnum(char *s) +{ + for (int i = 0; s[i] != 0; i++) + if (!isdigit(s[i])) + return false; + return true; +} + + +char *itoa(int num, char* str, int base) +{ + int i = 0; + bool isNegative = false; + + /* Handle 0 explicitly, otherwise empty string is + * printed for 0 */ + if (num == 0) { + str[i++] = '0'; + str[i] = '\0'; + return str; + } + + // In standard itoa(), negative numbers are handled + // only with base 10. Otherwise numbers are + // considered unsigned. + if (num < 0 && base == 10) { + isNegative = true; + num = -num; + } + + // Process individual digits + while (num != 0) { + int rem = num % base; + str[i++] = (rem > 9) ? (rem - 10) + 'a' : rem + '0'; + num = num / base; + } + + // If number is negative, append '-' + if (isNegative) + str[i++] = '-'; + + str[i] = '\0'; // Append string terminator + + // Reverse the string + reverse(str, i); + + return str; +} + +} -- cgit v1.3.1-4-g156e