# include "opihi.h"

/******************/
int opihi (int argc, char **argv) {

  int Nbad, status;
  char *line, *prompt, *history;
  pid_t ppid;

  general_init (&argc, argv);
  program_init (&argc, argv);
  startup (&argc, argv);
  prompt = get_variable("PROMPT");
  history = get_variable("HISTORY");
  welcome ();

  Nbad = 0;
  while (1) {  /** must exit with command "exit" or "quit" */
    if (Nbad == 10) exit (3);

    line = readline (prompt);

    if (line == NULL) { 
      
      ppid = getppid();
      if (ppid == 1) {
	gprint (GP_ERR, "caught parent shutdown\n");
	exit (2);
      }
      if (!isatty (STDIN_FILENO)) exit (2);
      gprint (GP_LOG, "Use \"quit\" to exit\n");
      Nbad ++;
      continue;
    }

    Nbad = 0;
    ohana_memregister (line);

    stripwhite (line);
    if (*line) {
      status = multicommand (line);
      add_history (line);
      append_history (1, history);
    }
    free (line);
    line = (char *) NULL;
  }
}

/* 
   startup sequence:

   - general_init
   - program_init
   - startup (exit if non-interactive)
   - welcome

*/
