Skip to content

Commit abb667a

Browse files
Update secshell.cpp
Added support for using $ to call variables saved with export.
1 parent c1355da commit abb667a

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

secshell.cpp

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include <limits.h>
1717

1818
class SecShell {
19-
std::unordered_map<pid_t, std::string> jobs;
19+
std::unordered_map<pid_t, std::string> jobs;
2020
bool running = true;
2121

2222
// Security whitelists
@@ -125,7 +125,7 @@ class SecShell {
125125
}
126126

127127
void display_history() {
128-
std::string drawbox_command = "drawbox \" Command History \" bold_white";
128+
std::string drawbox_command = "drawbox \" Command History \" bold_white";
129129
int result = system(drawbox_command.c_str());
130130

131131
if (result != 0) {
@@ -166,19 +166,31 @@ class SecShell {
166166
}
167167

168168
void list_env_variables() {
169+
std::string drawbox_command = "drawbox \" Environment Variable \" bold_white";
170+
int result = system(drawbox_command.c_str());
171+
172+
if (result != 0) {
173+
print_error("Failed to execute drawbox command.");
174+
return;
175+
}
169176
extern char** environ;
170177
for (char** env = environ; *env; env++) {
171178
std::cout << *env << "\n";
172179
}
173180
}
174181

175182
void unset_env_variable(const std::vector<std::string>& args) {
183+
std::string var_value = args[1];
184+
185+
176186
if (args.size() < 2) {
177187
print_error("Usage: unset VAR");
178188
return;
179189
}
180190
if (unsetenv(args[1].c_str()) != 0) {
181191
print_error("Failed to unset environment variable: " + std::string(strerror(errno)));
192+
} else {
193+
print_alert("Unset: " + var_value);
182194
}
183195
}
184196

@@ -670,6 +682,16 @@ class SecShell {
670682
} else {
671683
arg += c;
672684
}
685+
} else if (c == '$' && !in_quotes) {
686+
// Handle environment variable expansion
687+
std::string var_name;
688+
while (i + 1 < input.length() && (isalnum(input[i + 1]) || input[i + 1] == '_')) {
689+
var_name += input[++i];
690+
}
691+
const char* var_value = getenv(var_name.c_str());
692+
if (var_value) {
693+
arg += var_value;
694+
}
673695
} else if (c == ' ' && !in_quotes) {
674696
if (!arg.empty()) {
675697
args.push_back(arg);
@@ -688,7 +710,7 @@ class SecShell {
688710
}
689711

690712
static void signal_handler(int signum) {
691-
std::cout << "\nReceived signal " << signum << ". Use 'exit' to quit.\n";
713+
std::cout << "\nReceived signal " << signum << ". Use 'exit' to quit. Press ENTER to continue...\n";
692714
}
693715
};
694716

@@ -725,6 +747,11 @@ std::string get_executable_directory() {
725747
}
726748

727749
int main(int argc, char* argv[]) {
750+
#ifdef _WIN32
751+
system("cls");
752+
#elif defined(unix) || defined(__unix__) || defined(__unix)
753+
system("clear");
754+
#endif
728755

729756
// Get the directory of the executable
730757
std::string exe_dir = get_executable_directory();

0 commit comments

Comments
 (0)