/* include files */
/* compile: g++ -Wall -o simpleshell simpleshell.cc */
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stream.h>

/* a simple define for convenience */
#define TRUE 1

int main()
{
  int pid,i=0;
  char tab[1024];

  /* the main loop of our shell */
  while (TRUE) {
    /* print strings and numbers */
    cout<<"prompt "<<i<<"> ";

    /* read the command line */
    cin>>tab;
	     
    if (tab[0]==0) { /* <Control+D> pressed */
      cerr<<"Bye bye\n";
      exit(0);
    }

    /* one process running */
    pid = fork();
    /* 2 processes running */
    cout<<"I'm running\n";

    switch (pid) /* where are we ? */
      {
      case 0: /* in the child! */
	cout<<"In the child\n";
	/* exec command */
	execlp(tab,tab,NULL);
	/* executed only if the exec command fails */
	perror("exec");
	exit(0);
	break;
      case -1: /* in... the parent: fork has failed no wait!*/
	perror("fork");
	break;
      default: /* in the parent, the fork worked! */
	cout<<"In the parent.. wait\n";
	cout<<wait(0)<<" done\n";
	i++; /* for the next command */
      }
  }
  /* end of the loop */

  return 0;
}

