File tree Expand file tree Collapse file tree 11 files changed +124
-0
lines changed
Expand file tree Collapse file tree 11 files changed +124
-0
lines changed Original file line number Diff line number Diff line change 1+ {
2+ "editor.tabSize" : 4 ,
3+ "editor.detectIndentation" : false
4+ }
Original file line number Diff line number Diff line change 1+ #pragma once
2+ #include < string>
3+
4+ class Command {
5+ public:
6+ virtual ~Command () = default ;
7+ virtual void execute (const std::string& args) = 0;
8+ virtual std::string type () = 0;
9+ };
Original file line number Diff line number Diff line change 1+ #include " command_factory.h"
2+
3+ CommandFactory::CommandFactory () {
4+ commands[" exit" ] = std::make_unique<ExitCommand>();
5+ commands[" echo" ] = std::make_unique<EchoCommand>();
6+ }
7+
8+ Command* CommandFactory::get (const std::string& name) {
9+ auto it = commands.find (name);
10+ return it != commands.end () ? it->second .get () : nullptr ;
11+ }
Original file line number Diff line number Diff line change 1+ #pragma once
2+ #include < map>
3+ #include < memory>
4+ #include < string>
5+ #include " command.h"
6+ #include " exit_command.h"
7+ #include " echo_command.h"
8+
9+ class CommandFactory {
10+ std::map<std::string, std::unique_ptr<Command>> commands;
11+ public:
12+ CommandFactory ();
13+ Command* get (const std::string& name);
14+ };
Original file line number Diff line number Diff line change 1+ #include " echo_command.h"
2+ #include < iostream>
3+
4+ void EchoCommand::execute (const std::string& args) {
5+ std::cout << args << std::endl;
6+ }
7+
8+ std::string EchoCommand::type () {
9+ return " builtin" ;
10+ }
Original file line number Diff line number Diff line change 1+ #pragma once
2+ #include < string>
3+ #include " command.h"
4+
5+ class EchoCommand : public Command {
6+ public:
7+ void execute (const std::string& args) override ;
8+
9+ std::string type () override ;
10+ };
Original file line number Diff line number Diff line change 1+ #include " exit_command.h"
2+ #include < iostream>
3+
4+ void ExitCommand::execute (const std::string& args) {
5+ int exit_code = std::stoi (args);
6+ exit (exit_code);
7+ }
8+
9+ std::string ExitCommand::type () {
10+ return " builtin" ;
11+ }
Original file line number Diff line number Diff line change 1+ #pragma once
2+ #include < string>
3+ #include " command.h"
4+
5+ class ExitCommand : public Command {
6+ public:
7+ void execute (const std::string& args) override ;
8+
9+ std::string type () override ;
10+ };
Original file line number Diff line number Diff line change 1+ #include " shell.h"
2+ #include < iostream>
3+ #include < sstream>
4+
5+ void Shell::run () {
6+ // Flush after every std::cout / std:cerr
7+ std::cout << std::unitbuf;
8+ std::cerr << std::unitbuf;
9+
10+ std::string input;
11+ while (true ) {
12+ std::cout << " $ " ;
13+ if (!std::getline (std::cin, input) || input.empty ()) {
14+ break ;
15+ }
16+ std::istringstream iss (input);
17+ std::string cmd, args;
18+ iss >> cmd;
19+ std::getline (iss, args);
20+
21+
22+ // trim leading whitespace
23+ size_t start = args.find_first_not_of (" " );
24+ if (start != std::string::npos) {
25+ args = args.substr (start);
26+ } else {
27+ args.clear ();
28+ }
29+
30+ if (auto * command = commandFactory.get (cmd)) {
31+ command->execute (args);
32+ } else {
33+ std::cout << cmd << " : command not found" << std::endl;
34+ }
35+ }
36+ };
You can’t perform that action at this time.
0 commit comments