Advertisement
retro-64K

trying to make a zsh-like shell for windows

Nov 6th, 2021
101
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma warning (disable : 4996)
  2. #include <iostream>
  3. #include <direct.h>
  4. #include <fstream>
  5. #include <stdio.h>
  6. #include <string>
  7. #include <io.h>
  8. #include <map>
  9. #define clear cout << "\033[H\033[J"
  10. using namespace std;
  11.  
  12. const string CurrentDateAndTime() {
  13.     time_t     now = time(0);
  14.     struct tm  tstruct;
  15.     char       buf[128];
  16.     tstruct = *localtime(&now);
  17.     strftime(buf, sizeof(buf), "%d/%m/%Y, %X", &tstruct);
  18.     return buf;
  19. }
  20.  
  21. void help() {
  22.     cout << "Enhanced Z shell, build 0 from November, 2021.\n"
  23.          << "Available commands:\n"
  24.          << "help - shows this message\n"
  25.          << "ls - list contents of current or specified directory\n"
  26.          << "cd - changes current directory\n"
  27.          << "exit - exit the shell\n"
  28.          << "clear - erases all contents of the screen\n";
  29. }
  30.  
  31. void add_history(string cmd) {
  32.     fstream history;
  33.     history.open("history.zsh", ios::in | ios::out | ios::app);
  34.     history << cmd;
  35. }
  36.  
  37. int main() {
  38.     ios::sync_with_stdio(false);
  39.     cout.tie(0);
  40.     cin.tie(0);
  41.     register string cmd;
  42.     register string prompt = ": ";
  43.     register char cwd[1024];
  44.     register char path[1024];
  45.     getcwd(cwd, sizeof(cwd));
  46.     register char* username = getenv("USERNAME");
  47.     map<string, int> cmds = {
  48.         {"help", 1},
  49.         {"ls", 2},
  50.         {"cd", 3},
  51.         {"exit", 4},
  52.         {"clear", 5},
  53.         {"echo", 6}
  54.     };
  55.     cout << "Welcome to the Enhanced Z shell, " << username << ".\n";
  56.     cout << CurrentDateAndTime() << "\n";
  57.     while (1) {
  58.         cout << cwd << prompt;
  59.         cin >> cmd;
  60.         add_history(cmd);
  61.         switch (cmds[cmd]) {
  62.         case 1:
  63.             help();
  64.             break;
  65.         case 2:
  66.             system("dir");
  67.             break;
  68.         case 3:
  69.             cin >> path;
  70.             chdir(path);
  71.             break;
  72.         case 4:
  73.             exit(0);
  74.             break;
  75.         case 5:
  76.             clear;
  77.             break;
  78.         case 6:
  79.  
  80.             break;
  81.         }
  82.     }
  83.     return 0;
  84. }
Advertisement
RAW Paste Data Copied
Advertisement