/*------------------------------------------------------------------------
 *
 *  ecurses.c - a new curses implementation (version 2)
 *
 *  Location: /usr/local/src/ecurses/ecurses.c
 *
 *  Directory:
 *    initscr ()    - initscr function using terminal specific driver
 *    endwin ()     - restore terminal
 *
 *  Author: Frank Meyer
 *  (c) Copyright 1992 textware GmbH Koeln
 *----------------------------------------------------------------------*/

#define CURSES_DEFINED

#include <ecurses.h>
#include "private.h"

extern char * getenv ();
extern char * calloc ();

/*------------------------------------------------------------------------
 *  initscr ()    - initscr function using terminal specific driver
 *----------------------------------------------------------------------*/

int
initscr ()
{
  char * term;                                  /* type of term  */
  char   fname[64];                             /* file name buf */
  FILE * fp;                                    /* file pointer  */
  int    rtc;                                   /* return code   */
  chtype ** scr_ptr;                            /* screen image  */
  int    i;

  if (! (term = getenv ("TERM")))               /* no environment*/
  {                                             /* var TERM      */
    fprintf (stderr, "ecurses: don't know your terminal name\n");
    exit (1);
  }

  sprintf (fname, "/usr/local/lib/term/iso8/%s.OUT", term);
                                                /* fill fname    */

  if (! (fp = fopen (fname, "r")))              /* open ok ?     */
  {                                             /* no !          */
    fprintf (stderr, "ecurses: cannot open database file %s\n",
             fname);
    exit (1);
  }

  fread (charset, sizeof (charset), 1, fp);     /* read charset   */
  fread (map_table, sizeof (map_table), 1, fp); /* read map_table */
  fread (key_table, sizeof (key_table), 1, fp); /* read key_table */

  (void) fclose (fp);                           /* close the file */

  stdscr = (struct _win_st *) calloc (1, sizeof (struct _win_st));

  if (! strncmp (term, "vt", 2))                /* DEC VTxxx      */
  {
    rtc = VT_initscr (term);
  }
  else if (! strcmp (term, "at386"))            /* PC Console     */
  {
    rtc = AT_initscr (term);
  }
  else if (! strncmp (term, "9780", 4))         /* Siemens 9780x  */
  {
    rtc = SI_initscr (term);
  }
  else if (! strcmp (term, "xterm"))
  {
    rtc = X11_initscr (term);
  }
  else
  {
   fprintf (stderr,
    "ecurses: don't know anything about your term. emulation!\n");
    exit (1);
  }

  scr_ptr = (unsigned int **)
             malloc ((unsigned) (LINES * sizeof (int *)));

  if (! scr_ptr)
  {
    (void) fprintf (stderr, "ecurses: Not enough memory\n");
    (void) exit (1);
  }

  for (i = 0; i < LINES; i++)
  {
    scr_ptr[i] = (unsigned int *)
                 malloc ((unsigned) (sizeof (int) * (COLS + 1)));

    if (! scr_ptr[i])
    {
      (void) fprintf (stderr, "ecurses: Not enough memory\n");
      (void) exit (1);
    }
  }

  stdscr->image = scr_ptr;

  (*clear_func) ();
  return (rtc);
} /* initscr () */


/*------------------------------------------------------------------------
 *  endwin ()     - restore terminal
 *----------------------------------------------------------------------*/

int
endwin ()
{
  return (*endwin_func) ();
} /* endwin () */

/* end of ecurses.c */
