#include <windows.h>
#include <stdio.h>
#include <readline/readline.h>

/* A simple Windows shell. 
   This program uses readline (and needs the readline library!)
   to read lines of text from the user.
   Each line is given to CreateProcess to start up a new process.
   
   NOTE: You either have to give it a progam that is in the PATH
   (try echo $PATH at the cygwin prompt), or specify the entire
   path to the program. The following are some examples:

   notepad
   mspaint
   winver
   calc
   c:\program files\windows NT\Accessories\wordpad.exe
   c:\program files\microsoft office\office10\winword.exe

   To compile in cygwin, use gcc -o minishell minishell.c -lreadline

   To get out (in a cygwin window), press ^d on a line by itself

*/

int main(int argc, char **argv) {

  PROCESS_INFORMATION pi;    /* holds info about child processes */
  STARTUPINFO si; 

  LPTSTR cmd;                /* LPTSTR is like char * (with support for unicode) */

  GetStartupInfo(&si);
  /* You can mess with the startup info in si to change
     things like the window size of the new process, etc */

  while (cmd  = readline("Enter command: ")) {
    /* create new process (or try to...) */
    CreateProcess(NULL,          /* lpApplicationName */
		  cmd,           /* lpCommandLine */
		  NULL,          /* lpsaProcess */
		  NULL,          /* lpsaThread */
		  FALSE,         /* bInheritHandles */
		  DETACHED_PROCESS, /* dwCreationFlags */
		  NULL,          /* lpEnvironment */
		  NULL,          /* lpCurDir */
		  &si,           /* lpStartupInfo */
		  &pi            /* lpProcInfo */
		  );

    printf("New Process Info:\n");
    printf("Process ID: %d\n",pi.dwProcessId);
    printf("Waiting for completion\n");
    /* wait for the process to terminate */
    if (WaitForSingleObject(pi.hProcess,INFINITE)==WAIT_FAILED) {
      printf("Wait failed ?!?!\n");
    } else {
      printf("Process seems to have terminated normally\n");
    }
  }
  return(0);
}



