/* vim: set filetype=c:
 *
 * io.c86  -  Terminal I/O routines
 *
 * Copyright (C) 1998-2007 Gero Kuhlmann   <gero@gkminix.han.de>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Id: io.c86,v 1.5 2007/01/06 18:31:21 gkminix Exp $
 */

#include <common.h86>
#include <i386/conio.h86>
#include <i386/ctype.h86>
#include "romcheck.h86"



/*
 * Wait for a character to be typed into the keyboard
 */
char waitchar(void)
{
  char ret;

  /* This construct is necessary because tolower() is a CPP macro */
  ret = getkey(0) & 0x00ff;
  return(tolower(ret));
}



/*
 * Read a decimal integer number.
 */
int readint(const char *prompt, int minval, int maxval, int defval)
{
  int i, num;

  while (TRUE) {
	cflush();
	if (prompt != NULL)
		printf("%s [%d to %d]: ", prompt, minval, maxval);
	if ((i = scanf("%d\n", &num)) == EOF) {
		printf("%d\n", defval);
		return(defval);
	} else if (i != 1)
		printf("You have to enter a decimal number\n");
	else if (num < minval || num > maxval)
		printf("Entry out of range\n");
	else
		return(num);
  }
}



/*
 * Read an unsigned hex integer number.
 */
unsigned short readhex(const char *prompt,
			unsigned short minval,
			unsigned short maxval,
			unsigned short defval)
{
  unsigned int num;
  int i;

  while (TRUE) {
	cflush();
	if (prompt != NULL)
		printf("%s [%04X to %04X hex]: ", prompt, minval, maxval);
	if ((i = scanf("%x\n", &num)) == EOF) {
		printf("%04X\n", defval);
		return(defval);
	} else if (i != 1)
		printf("You have to enter a hex number\n");
	else if (num < minval || num > maxval)
		printf("Entry out of range\n");
	else
		return(num);
  }
}

