#include "system.h"
#include <conio.h>

using namespace sys;
void* ReadLoop(shell_stream* ptr) {
   int ch = ptr->get();
   while(*ptr) {
      cout << (char)ch << flush;
      ch = ptr->get();
   }
   return NULL;
}

int
main(int argc, char** argv)
{
   string cmd;
   bool running = true;
   int rc = 0;
   DWORD wait_id;
   char ch;
   shell_stream cmd_out("cmd.exe");
   HANDLE hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ReadLoop, (LPVOID)&cmd_out, 0, &wait_id);
   while(cmd_out) {
      ch = getch();
      cout << ch;
      cmd_out << ch;
   }
   CloseHandle(hThread);
   return rc;
}

/*
int
main2(int argc, char** argv)
{
   string cmd;
   bool running = true;
   int rc = 0;
   int ch;
   DWORD wait_id;
   while(running) {
      cout << "shell> " << flush;
      cmd = read_line();
      if(cmd == "exit") {
         running = false;
         continue;
      }
      Task* hRead = popen(cmd);
      if(hRead != NULL) {
         HANDLE hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ReadLoop, (LPVOID)hRead->get_read(), 0, &wait_id);
         if(hThread != NULL) {
            ch = cin.get();
            while(cin && !program_done()) {
               if(pputc(hRead->get_write(), ch) == -1) break;
               ch = cin.get();
            }
            WaitForSingleObject(hThread, INFINITE);
            CloseHandle(hThread);
         }
         CloseHandle(hRead);
         delete hRead;
      }
   }
   return 0;
}
*/
