/*
 *  Deal w/ amiga console screen stuff (and other missing stuff - LJR).
 *
 *					-- luis soltero, 5/12/88 --
 *
 *  Total rework by Loren J. Rittle, 12/28/90
 *
 *  Now when ISpell is run in interactive mode, all screen
 *  I/O is directed at the CLI window ISpell is started in 
 *  if possible.  If started by WorkBench then, of course,
 *  a window is opened for screen I/O.
 *
 *  Note: None of the screen I/O routines are called if ISpell
 *  is run in ARexx server mode.
 */

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#pragma msg 148 ignore push
#pragma msg 149 ignore push
#pragma msg 61 ignore push
#include <exec/types.h>
extern struct DosLibrary *DOSBase ;
#include <dos/dos.h>
#include <dos/dosextens.h>
#include <clib/dos_protos.h>
#include <pragmas/dos_pragmas.h>
#include <intuition/intuition.h>
#include <clib/intuition_protos.h>
#include <pragmas/intuition_pragmas.h>
#pragma msg 149 pop
#pragma msg 61 pop
#include "version.h"
#include "ispell.h"

BPTR tty;

struct colortype
{
  char *style;
  char *frw;
  char *bak;
} colors[] =
{
#define WHITE_ON_BLACK	0
  {"0", "31", "42"},
#define WHITE_ON_BLUE	1
  {"0", "31", "40"},
#define BLACK_ON_WHITE	2
  {"0", "32", "41"},
#define BLUE_ON_WHITE	3
  {"0", "30", "41"},
#define ORANGE_ON_BLUE	4
  {"0", "33", "40"},
#define BLUE_ON_ORANGE  5
  {"0", "30", "43"}
};

#define SETCOLORS(x)	(setcolors(colors[x].style, colors[x].frw, colors[x].bak))

void setcolors (char *style, char *fg, char *bg)
{
  char buf[16];

  sprintf (buf, "\x9b%s\x3b%s\x3b%s\x6d", style, fg, bg);
  Write (tty, buf, strlen(buf));
}

void printcon (char *fmt,...)
{
  char buf[256];
  va_list ap;

  va_start (ap, fmt);
  vsprintf (buf, fmt, ap);
  va_end (ap);
  Write (tty, buf, strlen(buf));
}

void putccon (int ch)
{
  char c = ch;

  Write (tty, &c, 1);
}

int getccon (void)
{
  unsigned char ch;

  Read(tty, &ch,1L);
  return ((int)ch);
}

/* This subroutine does the work of turning the current console to raw  */
/* Pass a zero to turn it to cooked mode, any other value to turn it    */
/* to raw mode.                                                         */
int rawmode (int flag)
{
  return SetMode (Output (), flag);
}

/* open a console window for ISpell */
void terminit (void)
{
  tty = Open("*", MODE_OLDFILE);
  rawmode(1);
  li = 23;
  co = 77;
}

void done (void)
{
  remove (tempfile);
  Close(tty);
  rawmode(0);
  exit (0);
}

/* clear the screen */
void erase (void)
{
  Write (tty, "\x0c", 1);
}

/* move to row, col */
void move (int row, int col)
{
  char buf[16];

  sprintf (buf, "\x9b%d\x3b%d\x48", row, col);
  Write (tty, buf, strlen(buf));
}

/* do stand out mode */
void inverse (void)
{
  SETCOLORS (BLUE_ON_WHITE);
}

/* do stand end mode */
void normal (void)
{
  SETCOLORS (WHITE_ON_BLUE);
}

/* do a back space */
void backup (void)
{
  putccon ('\b');
}

/* not implemented on the amiga */
void onstop (int signo)
{
}

void stop (void)
{
}

/*
 * i really do not understand why sleep is used in Ispell. Here is a null
 * function used soley to keep ispell happy.
 */
/* i do.  fixed.  -tgr */
void sleep (int n)
{
  if (n > 0)
    Delay (50L * n);
}

/*
 * This function is broken in SAS/C v5.10 - LJR 
 * Thanks to Matt Dillon, as this was lifted from DICE. 
 */
char *tmpnam(char *buf)
{
    static char Buf[32];
    static long i;

    if (buf == NULL)
	buf = Buf;
    sprintf(buf, "T:%08lx-%ld", FindTask(NULL), i++);
    return(buf);
}
