Skip to content

Commit f95aa27

Browse files
committed
Redirect stdout using > sign to be able to execute a second command
1 parent aa0a44e commit f95aa27

File tree

11 files changed

+398
-257
lines changed

11 files changed

+398
-257
lines changed

.vscode/launch.json

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
{
2-
"version": "0.2.0",
32
"configurations": [
43
{
5-
"name": "DEBUG",
6-
"type": "cppdbg",
7-
"request": "launch",
8-
"program": "${workspaceFolder}/build/shell_cpp",
94
"args": [],
10-
"stopAtEntry": false,
11-
"cwd": "${fileDirname}",
5+
"cwd": "${workspaceFolder}/build/",
126
"environment": [],
137
"externalConsole": false,
148
"MIMode": "gdb",
9+
"name": "DEBUG",
10+
"preLaunchTask": "BUILD",
11+
"program": "${workspaceFolder}/build/shell_cpp",
12+
"request": "launch",
1513
"setupCommands": [
1614
{
1715
"description": "Enable pretty-printing for gdb",
18-
"text": "-enable-pretty-printing",
19-
"ignoreFailures": true
16+
"ignoreFailures": true,
17+
"text": "-enable-pretty-printing"
2018
},
2119
{
2220
"description": "Set Disassembly Flavor to Intel",
23-
"text": "-gdb-set disassembly-flavor intel",
24-
"ignoreFailures": true
21+
"ignoreFailures": true,
22+
"text": "-gdb-set disassembly-flavor intel"
2523
}
26-
]
24+
],
25+
"stopAtEntry": false,
26+
"type": "cppdbg"
2727
}
28-
29-
]
28+
],
29+
"version": "0.2.0"
3030
}

.vscode/tasks.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"tasks": [
3+
{
4+
"args": [],
5+
"command": "./BUILD.sh",
6+
"group": {
7+
"isDefault": true,
8+
"kind": "build"
9+
},
10+
"label": "BUILD",
11+
"options": {
12+
"cwd": "${workspaceFolder}"
13+
},
14+
"problemMatcher": [
15+
"$gcc"
16+
],
17+
"type": "cppbuild"
18+
}
19+
],
20+
"version": "2.0.0"
21+
}

compile.sh renamed to BUILD.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/usr/bin/env bash
22
set -e
33

44
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE=${VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake

CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ project(shell-cpp)
44
set(CMAKE_CXX_STANDARD 23)
55
set(CMAKE_CXX_STANDARD_REQUIRED ON)
66

7+
if(NOT CMAKE_BUILD_TYPE)
8+
set(CMAKE_BUILD_TYPE Debug CACHE STRING "Build type" FORCE)
9+
endif()
10+
711
file(GLOB_RECURSE SOURCE_FILES src/*.cc include/*.hh)
812
add_executable(shell_cpp ${SOURCE_FILES})
913

README.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Shell
22

3-
Shell capable of interpreting shell commands, running external programs and builtin commands like echo, type, pwd, cd
3+
Shell capable of interpreting shell commands, running external programs and builtin commands like echo, type, pwd, cd, redirect stdout (>)
4+
5+
---
6+
7+
### Prerequisites
8+
9+
- GCC
10+
- CMake
11+
12+
### Instructions
413

514
`$ exit` to leave
15+
16+
TODO...

run.sh renamed to RUN.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/usr/bin/env bash
22
set -e
33

44
exec ./build/shell_cpp "$@"

test.sh renamed to TEST.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/usr/bin/env bash
22
set -e
33

44
ctest --test-dir build

include/shell.hh

+16-4
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,29 @@
44

55
#include <filesystem>
66
#include <iostream>
7-
#include <vector>
8-
#include <print>
7+
#include <numeric>
98
#include <ranges>
9+
#include <string>
10+
#include <vector>
1011

1112
namespace fs = std::filesystem;
1213

13-
struct Command
14+
struct Input
1415
{
1516
std::string_view original_input;
16-
std::string executable;
17+
std::string command;
1718
std::string arguments;
19+
std::vector<std::string> args;
1820
};
1921

2022
bool processInput(std::string input);
23+
24+
Input _parseInput(const std::string_view &input);
25+
std::string _searchExecutable(const std::string &exec_name, const std::string &env_p);
26+
std::string _echoResult(const std::string &params);
27+
std::string _typeResult(const std::string &args, const std::string &env_p);
28+
std::string _changeDirectoryResult(const std::string &exec, const std::string &args);
29+
char _nextCharacter(const char &ch);
30+
bool _isExcapableCharacter(const char &ch);
31+
std::string join_args(const std::vector<std::string> &vec, const std::string &delimiter);
32+
std::vector<std::string> sliceVector(const std::vector<std::string> &vec);

src/main.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ int main()
1010
std::cerr << std::unitbuf;
1111

1212
std::string input;
13-
std::print("$ ");
13+
std::cout << "$ ";
1414
std::getline(std::cin, input);
1515

1616
running = processInput(input);

0 commit comments

Comments
 (0)