Skip to content

Commit 3352e98

Browse files
committed
ls -l > GGG works.
1 parent a31e37f commit 3352e98

3 files changed

Lines changed: 48 additions & 12 deletions

File tree

include/shell/execution_policy.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ struct ExecutionResult {
2929
[[nodiscard]] bool is_failure() const noexcept {
3030
return !is_success();
3131
}
32+
33+
friend std::ostream& operator<<(std::ostream& os, const ExecutionResult& result) {
34+
if (result.error_message.has_value()) {
35+
return os << result.error_code << ": " << result.error_message.value() << ", EXIT_CODE: " << result.exit_code;
36+
} else {
37+
return os << result.error_code << ": " << ", EXIT_CODE: " << result.exit_code;
38+
}
39+
}
3240
};
3341

3442
// ============================================================================
@@ -50,7 +58,8 @@ concept ExecutionPolicy = requires(T policy, const Command& cmd, const Pipeline&
5058
/// Platform-specific execution policy
5159
/// Implementation is in exec_posix.cpp (Linux/macOS) or exec_win32.cpp (Windows)
5260
struct PlatformExecutionPolicy {
53-
61+
[[nodiscard]] static std::vector<const char*> convertEnvironment(const Command& cmd);
62+
[[nodiscard]] static std::vector<const char*> convertArgv(const Command& cmd);
5463
/// Execute a single command
5564
ExecutionResult execute(const Command& cmd) const;
5665

include/shell/shell_interpreter.hpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,9 @@ class ShellInterpreter {
198198
}
199199

200200
auto result = executor_.execute(cmd);
201+
std::cout << "Executed command: " << cmd.executable << "\n";
202+
std::cout << result << "\n";
201203

202-
if (result.error_message) {
203-
return std::unexpected(*result.error_message);
204-
std::cout << "Error executing command: " << *result.error_message << "\n";
205-
} else {
206-
std::cout << "Success executing command: " << cmd.executable << "\n";
207-
}
208204
return result.exit_code;
209205
}
210206

src/lib/executor/executor_posix.cpp

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,39 @@ class EnvironmentCache {
122122
std::mutex mutex_;
123123
};
124124

125+
std::vector<const char*>
126+
PlatformExecutionPolicy::convertEnvironment(const Command& cmd) {
127+
// Set up environment variables
128+
std::unordered_map<std::string, std::string> env_map;
129+
env_map.insert(cmd.env.begin(), cmd.env.end());
130+
if (cmd.env_inherit) {
131+
// Add current environment to env
132+
auto current_env = EnvironmentCache::instance().get_all();
133+
env_map.insert(current_env.begin(), current_env.end());
134+
}
135+
std::vector<const char*> envp;
136+
for (const auto& arg : env_map) {
137+
envp.push_back((arg.first+ "=" + arg.second).c_str());
138+
}
139+
envp.push_back(nullptr); // NULL-terminated
140+
141+
return envp;
142+
}
143+
144+
std::vector<const char*>
145+
PlatformExecutionPolicy::convertArgv(const Command& cmd) {
146+
// Convert command args to C-style argv
147+
std::vector<const char*> argv;
148+
argv.push_back(cmd.executable.c_str());
149+
for (const auto& arg : cmd.args) {
150+
argv.push_back(arg.c_str());
151+
}
152+
argv.insert(argv.begin(), cmd.executable.filename().c_str());
153+
argv.push_back(nullptr); // NULL-terminated
154+
155+
return argv;
156+
}
157+
125158
ExecutionResult PlatformExecutionPolicy::execute(const Command& cmd) const {
126159
// Fork process
127160
pid_t pid = fork();
@@ -151,8 +184,9 @@ ExecutionResult PlatformExecutionPolicy::execute(const Command& cmd) const {
151184
// Open file and redirect stdin (TODO)
152185
}
153186
if (std::holds_alternative<FileTarget>(cmd.stdout_)) {
154-
std::cout << "Redirecting stdout to file\n";
187+
155188
const auto& file_target = std::get<FileTarget>(cmd.stdout_);
189+
std::cout << "Redirecting stdout to file: " << file_target.path.c_str() << std::endl;
156190
// TODO umask
157191
// Open file and redirect stdout
158192
int fd = open(file_target.path.c_str(),
@@ -253,10 +287,7 @@ ExecutionResult PlatformExecutionPolicy::execute(const Command& cmd) const {
253287
}
254288
return ExecutionResult{.exit_code = exit_code, .error_message = std::nullopt};
255289
}
256-
return ExecutionResult{.exit_code = platform::EXIT_FAILURE_STATUS,
257-
.error_message = "Fallthrough case reached, pid == 0"};
258-
return ExecutionResult{.exit_code = platform::EXIT_FAILURE_STATUS,
259-
.error_message = "Fallthrough case reached, outside of pid checks"};
290+
return ExecutionResult{.error_code = 0, .error_message = std::nullopt};
260291
}
261292
ExecutionResult PlatformExecutionPolicy::execute(const Pipeline& pipeline) const {
262293
// Phase 1: No pipeline support yet - just execute first command

0 commit comments

Comments
 (0)