/* start of special IO module */

#include <string.h>
#include <descrip.h>
#include <iodef.h>
#include <libclidef.h>

#define TRUE 1
#define FALSE 0

long sys$assign();
long sys$qiow();
long sys$dassgn();
long lib$disable_ctrl();
long lib$enable_ctrl();

short chan;
long ctrlint = FALSE;

void catcher()
{
   ctrlint = TRUE;
   return;
}

void ttopen()
{
   $DESCRIPTOR(ttdesc,"TT:");
   long mask;
   sys$assign(&ttdesc,&chan,0,0);
   mask = LIB$M_CLI_CTRLY;
   lib$disable_ctrl(&mask);
   sys$qiow(0,chan,IO$_SETMODE|IO$M_CTRLYAST,0,0,0,catcher,0,0,0,0,0);
   return;
}

void putch(char k)
{
   sys$qiow(0,chan,IO$_WRITEVBLK,0,0,0,&k,1,0,0,0,0);
   return;
}

void putstr(char *s)
{
   sys$qiow(0,chan,IO$_WRITEVBLK,0,0,0,s,strlen(s),0,0,0,0);
   return;
}

long kbhit()
{
   short charac[4];
   sys$qiow(0,chan,IO$_SENSEMODE|IO$M_TYPEAHDCNT,0,0,0,&charac,8,0,0,0,0);
   if (charac[0]>0)
      return TRUE;
   else
      return FALSE;
}

long ctrlychit()
{
   return ctrlint;
}

long getch()
{
   long k = 0;
   sys$qiow(0,chan,IO$_READVBLK|IO$M_NOECHO|IO$M_NOFILTR,0,0,0,&k,1,0,0,0,0);
   return k;
}

void ttclose()
{
   long mask;
   putch('\r');
   mask = LIB$M_CLI_CTRLY;
   lib$enable_ctrl(&mask);
   sys$dassgn(chan);
   return;
}

/* end of special IO module */

#include <stdio.h>
#include <ssdef.h>

long lib$wait();

main(long argc,char *argv[])
{
   long i;
   float t;
   char k;
   char pmt[]="Enter 1,2 or 3 ? ";
   ttopen();
   putstr(pmt);
   t = 1.0;
   do {
      putch('.');
      lib$wait(&t);
   } while (!(kbhit()||ctrlychit()));
   if (ctrlychit()) {
      ttclose();
      printf("Interrupted by user !\n");
   } else {
      k=getch();
      ttclose();
      printf("You entered : %c\n",k);
   };
   return SS$_NORMAL;
}
