diff options
| author | _ov4 <ov4@ov4.org> | 2026-09-01 07:58:49 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-09-01 07:58:49 +0000 |
| commit | 9a09c0bfc5956ed57fa4c26f84babb6778fa8a34 (patch) | |
| tree | 9f11e72fa682dbaad744a4b617500d924e8436e4 | |
| parent | c1733952f1bbb567b335109f51fb66a73abfedd3 (diff) | |
path
| -rw-r--r-- | include/all.h | 1 | ||||
| -rw-r--r-- | include/command/builtin.h | 3 | ||||
| -rw-r--r-- | include/global.h | 2 | ||||
| -rw-r--r-- | include/path.h | 9 | ||||
| -rw-r--r-- | include/util/prompt.h | 13 | ||||
| -rw-r--r-- | src/command/builtin.cc | 3 | ||||
| -rw-r--r-- | src/command/cd.cc | 19 | ||||
| -rw-r--r-- | src/handler.cc | 4 | ||||
| -rw-r--r-- | src/init.cc | 1 | ||||
| -rw-r--r-- | src/main.cc | 10 | ||||
| -rw-r--r-- | src/path.cc | 20 | ||||
| -rw-r--r-- | src/util/prompt.cc | 16 |
12 files changed, 76 insertions, 25 deletions
diff --git a/include/all.h b/include/all.h index 266735b..5141e37 100644 --- a/include/all.h +++ b/include/all.h @@ -2,6 +2,7 @@ #include "command/builtin.h" #include "util/io.h" +#include "util/prompt.h" #include "util/string.h" #include "eval.h" #include "global.h" diff --git a/include/command/builtin.h b/include/command/builtin.h index 49a8e13..cd58b14 100644 --- a/include/command/builtin.h +++ b/include/command/builtin.h @@ -9,6 +9,8 @@ #include <unordered_map> #include <functional> +#include "all.h" + namespace ov4 { @@ -26,5 +28,6 @@ void jobs(char **argv); void bg(char **argv); void fg(char **argv); void exit(char **argv); +void cd(char **argv); } diff --git a/include/global.h b/include/global.h index 5ff04f3..36fe3be 100644 --- a/include/global.h +++ b/include/global.h @@ -14,4 +14,6 @@ constexpr int MAXARGS = 128; constexpr bool GLOBAL_DEBUG = true; +constexpr std::string_view name = "ccshell"; + } diff --git a/include/path.h b/include/path.h index c4ea85c..06e5459 100644 --- a/include/path.h +++ b/include/path.h @@ -11,11 +11,10 @@ namespace ov4 { -extern std::string current_dir; - -std::string find_cmd(char* s); -std::string find_cmd(std::string s); -std::string set_current_dir(std::string s); +std::string find_cmd(const char* s); +std::string find_cmd(const std::string &s); std::string path_unifier(const std::string &s); +std::string get_current_dir(); +int set_current_dir(const std::string &s); } diff --git a/include/util/prompt.h b/include/util/prompt.h new file mode 100644 index 0000000..464156a --- /dev/null +++ b/include/util/prompt.h @@ -0,0 +1,13 @@ +#pragma once + +#include <iostream> + +#include "all.h" + +namespace ov4 +{ + constexpr std::string_view prompt = "$"; + extern bool emit_prompt; + + void print_prompt(); +} diff --git a/src/command/builtin.cc b/src/command/builtin.cc index 917dee4..4c5c1e2 100644 --- a/src/command/builtin.cc +++ b/src/command/builtin.cc @@ -13,7 +13,8 @@ unordered_map<string, function<void(char**)>> cmd = { "exit", ov4::command::exit }, { "jobs", ov4::command::jobs }, { "bg", ov4::command::bg }, - { "fg", ov4::command::fg } + { "fg", ov4::command::fg }, + { "cd", ov4::command::cd }, }; /* diff --git a/src/command/cd.cc b/src/command/cd.cc new file mode 100644 index 0000000..49b1899 --- /dev/null +++ b/src/command/cd.cc @@ -0,0 +1,19 @@ +#include "command/builtin.h" + +namespace ov4::command +{ + +using namespace std; + +void cd(char **argv) +{ + if (argv == nullptr) return; + if (argv[1] == nullptr) + { + cout << "cd: requires path" << endl; + return; + } + ov4::set_current_dir(argv[1]); +} + +} diff --git a/src/handler.cc b/src/handler.cc index 5356d47..b6399c2 100644 --- a/src/handler.cc +++ b/src/handler.cc @@ -8,11 +8,13 @@ sigset_t BLOCK_HANDLER; void signal_init() { + /* Install the signal handlers */ + sigemptyset(&BLOCK_HANDLER); sigaddset(&BLOCK_HANDLER, SIGINT); sigaddset(&BLOCK_HANDLER, SIGTSTP); sigaddset(&BLOCK_HANDLER, SIGCHLD); - + Signal(SIGINT, sigint_handler); /* ctrl-c */ Signal(SIGTSTP, sigtstp_handler); /* ctrl-z */ Signal(SIGCHLD, sigchld_handler); /* Terminated or stopped child */ diff --git a/src/init.cc b/src/init.cc index 9fdd5b7..e4d0b8f 100644 --- a/src/init.cc +++ b/src/init.cc @@ -14,7 +14,6 @@ There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR P Published under GNU AFFERO GENERAL PUBLIC LICENSE (AGPL) Version 3." << endl; signal_init(); - set_current_dir(get_current_dir_name()); } diff --git a/src/main.cc b/src/main.cc index f983590..2132ccb 100644 --- a/src/main.cc +++ b/src/main.cc @@ -18,7 +18,6 @@ using namespace std; using namespace ov4; /* Global variables */ -char prompt[] = "$ "; /* command line prompt (DO NOT CHANGE) */ char sbuf[MAXLINE]; /* for composing sprintf messages */ /* @@ -31,7 +30,6 @@ int main(int argc, char **argv) char c; char cmdline[MAXLINE]; - int emit_prompt = 1; /* emit prompt (default) */ /* Redirect stderr to stdout (so that driver will get all output * on the pipe connected to stdout) */ @@ -54,8 +52,6 @@ int main(int argc, char **argv) } } - /* Install the signal handlers */ - /* This one provides a clean way to kill the shell */ @@ -68,10 +64,8 @@ int main(int argc, char **argv) while (1) { /* Read command line */ - if (emit_prompt) { - printf("%s", prompt); - fflush(stdout); - } + print_prompt(); + if ((fgets(cmdline, MAXLINE, stdin) == NULL) && ferror(stdin)) app_error("fgets error"); if (feof(stdin)) { /* End of file (ctrl-d) */ diff --git a/src/path.cc b/src/path.cc index e9d1892..4d2ea1b 100644 --- a/src/path.cc +++ b/src/path.cc @@ -5,19 +5,16 @@ namespace ov4 using namespace std; -string current_dir; - -string find_cmd(char* s) +string find_cmd(const char * s) { - string tmp = s; + const string tmp = s; return find_cmd(tmp); } -string find_cmd(string s) +string find_cmd(const string &s) { if (s.size() == 0) return ""; string unified_s = path_unifier(s); - cout << unified_s << endl; if (access(unified_s.c_str(), F_OK) == 0) return unified_s; @@ -48,13 +45,18 @@ string path_unifier(const string &s) if (s[0] == '/') return s; if (s[0] == '.') { - return current_dir + s.substr(1); + return get_current_dir() + s.substr(1); } } -string set_current_dir(string s) +int set_current_dir(const string &s) +{ + return chdir(s.c_str()); +} + +string get_current_dir() { - return current_dir = s; + return (string)(get_current_dir_name()); } diff --git a/src/util/prompt.cc b/src/util/prompt.cc new file mode 100644 index 0000000..8f4e5e1 --- /dev/null +++ b/src/util/prompt.cc @@ -0,0 +1,16 @@ +#include "util/prompt.h" + +namespace ov4 +{ + using namespace std; + + bool emit_prompt = true; /* emit prompt (default) */ + + void print_prompt() + { + if (emit_prompt) + { + cout << name << " : " << get_current_dir() << " " << prompt << " " << flush; + } + } +} |
