i'm trying to make a simple shell in c++ but i'm having some trouble to make it work:
#pragma warning (disable : 4996)
#include <iostream>
#include <string>
#include <chrono>
#include <ctime>
#include <filesystem>
#include <fstream>
#ifdef _WIN64
#include <direct.h>
#else
#include <unistd.h>
#endif
#define clear cout << "\033[H\033[J"
#define MAXLETTER 1000
#define MAXCOMMAND 1000
using namespace std;
const string currentDateTime() {
time_t now = time(0);
struct tm tstruct;
char buf[128];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), "%d/%m/%Y, %X", &tstruct);
return buf;
}
void help() {
cout << "core's shell, build 0 from September, 2021." << endl;
cout << "Available commands:" << endl;
cout << "exit - exit shell" << endl;
cout << "cd - changes current working directory" << endl;
cout << "help - shows this message" << endl;
cout << "chprompt - changes shell's prompt" << endl;
}
void start() {
string username = getenv("USERNAME");
cout << "Welcome to core's shell, " << username << "." << endl;
cout << currentDateTime() << endl;
}
int main() {
string prompt = ": ";
string command;
const char* command_list[8];
command_list[0] = "exit";
command_list[1] = "cd";
command_list[2] = "help";
command_list[3] = "chprompt";
command_list[4] = "start";
int switch_arg = 0;
const char* dir = getenv("CD");
string history("history.csh");
fstream history_file;
clear;
start();
command:
history_file.open(history, ios::out);
cout << prompt;
getline(cin, command);
if (history_file.is_open()) {
history_file << command;
}
switch (switch_arg) {
case 1:
history_file.close();
exit(0);
case 2:
_chdir(dir);
return 0;
break;
case 3:
help();
return 0;
break;
case 4:
cout << "Type in your prompt: ";
cin >> prompt;
break;
case 5:
start();
break;
default:
cout << "Command not found" << endl;
goto command;
}
return 0;
}
how do i use case
on strings to make my commands work?
is there a better way to make a command history?
i'm making this code based on this code right here: https://www.geeksforgeeks.org/making-linux-shell-c/
sorry if the question is stupid, i'm quite rusty on c++.