/* 
 * ==================================================================
 * GetFileInput - read and process chars from stdin until EOF
 *
 * Updates:
 *
 * 03/93 George Haddad, Michigan State U.<georgeh@ibm.cl.msu.edu>:
 *	written
 *
 * ==================================================================
 */

/* #define DEBUG */

#include <stdio.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>

extern Bool kybdlock;
extern Bool screen_dirty;
extern Bool formatted;
extern Widget toplevel;
extern unsigned char asc2cg[256];
extern void screen_disp(); 

/* ARGUSED */
void
GetFileInput(client_data, fid, id)
caddr_t		client_data;	/* unused */
int		*fid;
XtInputId 	*id;
{
	char	buf[BUFSIZ];
	int	nbytes;
	int	i;

#ifdef DEBUG
printf("Entering GetFileInput\n");
#endif /* DEBUG */

	if (kybdlock)  {

#ifdef DEBUG
printf("GetFileInput - keybd locked (1)\n");
#endif /* DEBUG */

		return;
	}

        if ((nbytes = read(*fid, buf, BUFSIZ)) == -1) {

#ifdef DEBUG
printf("GetFileInput - error reading file\n");
#endif /* DEBUG */

                return;
        }



#ifdef DEBUG
printf("GetFileInput - nbytes read: %d\n",nbytes);
#endif /* DEBUG */

	if (nbytes > 0) {
		for (i = 0; i < nbytes; i++) {

			/* 03/93 GJH:                           */
			/* End loop if kybd locks since kybd.c  */
			/* will stop processing chars anyway.   */
	
#ifdef DEBUG
printf("GetFileInput - Buf[%d]: %c\n",i,buf[i]);
#endif /* DEBUG */
			if (kybdlock)  {
#ifdef DEBUG
printf("GetFileInput -  keybd locked (2)\n");
#endif /* DEBUG */
                       		return;
			}

			switch (buf[i]) {
		      	case '\b': key_Left();		break;
		      	case '\f': key_Clear(); 	break;
		      	case '\n': key_Enter();		break;
		      	case '\r': key_Enter();		break;
		      	case '\t': key_FTab();		break;
		      	default:
				(void) key_Character(asc2cg[buf[i]]);

			}
		}
	}
	else   {

#ifdef DEBUG
printf("GetFileInput - Removing Input Handler/n");
#endif /* DEBUG */

		XtRemoveInput(*id);	
	}

}
