/*
 *	testraw.c
 *
 *   This program shows how to use the functions raw() and cooked() to
 * control the way characters are read from a Level 2 file pointer.
 * These are only useful if the file pointer points at an instance of
 * the console.device, which means one of 'stdin', 'stdout', or 'stderr'
 * or a console opened with fp = fopen("CON:x/y/wid/len/Title","w+");
 * like this example does. 
 *
 * Written : 16-Jun-87 By Chuck McManis, do with it what you will.
 *
 */

#include <stdio.h>
#ifndef u_char
#define	u_char	unsigned char
#endif

void main()

{
  FILE	*win;
  char	c;
  long	i;

  printf("This program shows how to use raw mode file pointers.\n");
  printf("First we open the window...\n");
  win = fopen("CON:10/10/400/100/Test Console","w+");
/* If opening a file, use setnbf() to set it to unbuffered mode, this is
 * very importatant. Not required for stdin since Lattice defaults it to
 * unbuffered. 
 */
  setnbf(win);
  fprintf(win,"Using the default mode, type some characters ... \n");  
  fprintf(win,"Type a 'Q' to go to next phase ... \n");  
  i = 0;
  while ((c = fgetc(win)) != 'Q') {
    i = (i + 1) % 25;
    if (i == 0) printf("\n");
    printf(" %02x",(u_char) c);
  }
  printf("\n************\n");
  fprintf(win,"Now switching to 'raw' mode ...\n");
  fprintf(win,"Type a 'Q' to go to next phase ... \n");  
  if (raw(win) != 0) perror("raw");
  i = 0;
  while ((c = fgetc(win)) != 'Q') {
    i = (i + 1) % 25;
    if (i == 0) printf("\n");
    printf(" %02x",(u_char) c);
  }
  printf("\n************\n");
  fprintf(win,"Now back to 'cooked' mode ... \n");
  fprintf(win,"Type a 'Q' to quit ... \n");  
  if (cooked(win) != 0) perror("cooked");
  i = 0;
  while ((c = fgetc(win)) != 'Q') {
    i = (i + 1) % 25;
    if (i == 0) printf("\n");
    printf(" %02x",(u_char) c);
  }
  fclose (win);
}
