home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / Editors / elvis1_5.zoo / src / mint.c < prev    next >
C/C++ Source or Header  |  1992-08-08  |  3KB  |  165 lines

  1. /* mint.c */
  2.  
  3. /* Author:
  4.  *    Warwick Allison
  5.  *    warwick@cs.uq.oz.au
  6.  *
  7.  * (Based on unix.c and atari.c)
  8.  */
  9.  
  10.  
  11. /* This file contains the mint-specific version the ttyread() functions.
  12.  */
  13.  
  14. #include "config.h"
  15.  
  16. #if MINT
  17. # include "vi.h"
  18. # include <osbind.h>
  19.  
  20. /* For MINT, we copy V7 UNIX we set an alarm() before doing a blocking
  21.  * read(), and assume that the SIGALRM signal will cause the read() function
  22.  * to give up.
  23.  */
  24.  
  25. #include <setjmp.h>
  26.  
  27. static jmp_buf env;
  28.  
  29. /*ARGSUSED*/
  30. int dummy(signo)
  31.     int    signo;
  32. {
  33.     longjmp(env, 1);
  34. }
  35. int ttyread(buf, len, time)
  36.     char    *buf;    /* where to store the gotten characters */
  37.     int    len;    /* maximum number of characters to read */
  38.     int    time;    /* maximum time to allow for reading */
  39. {
  40.     /* arrange for timeout */
  41. #if __GNUC__
  42.     signal(SIGALRM, (void (*)()) dummy);
  43. #else
  44.     signal(SIGALRM, dummy);
  45. #endif
  46.     alarm(time);
  47.  
  48.     /* perform the blocking read */
  49.     if (setjmp(env) == 0)
  50.     {
  51.         const int SHF=3;
  52.         const int CNT=4;
  53.         const int ALT=8;
  54.  
  55.         int pos=0;
  56.         long l = Bconin(2);
  57.         int meta = Kbshift(-1);
  58.  
  59.         if (len>1) {
  60.             unsigned char sc=l>>16;
  61.             unsigned char ch=l&0xff;
  62.  
  63.             char format[10]="#%s%c";
  64.  
  65.             if (sc >= 0x63 && sc <= 0x72 || sc == 0x4a || sc == 0x4e) {
  66.                 /* Keypad */
  67.                 strcpy(format,"#%sK%c");
  68.                 ch=0;
  69.             } else if (sc >= 0x3b && sc <= 0x44) {
  70.                 /* F keys */
  71.                 if (sc == 0x44) sc-=10; /* Map F10 to F0 */
  72.                 sprintf(format,"#%%sF%d",sc-0x3b+1);
  73.                 ch=0;
  74.             } else if (sc >= 0x54 && sc <= 0x5d) {
  75.                 /* Shifted F keys - convert back to unshifted */
  76.                 sc-=(0x54-0x3b);
  77.                 if (sc == 0x44) sc-=10;
  78.                 sprintf(format,"#%%sF%d",sc-0x3b+1);
  79.                 meta|=SHF; /* Will be set anyway */
  80.                 ch=0;
  81.             } else if (sc >= 0x78 && sc <= 0x83) {
  82.                 /* Alt-numberkeys */
  83.                 sc-=(0x78-0x02);
  84.                 meta|=ALT;
  85.             } else if (sc == 0x62) {
  86.                 /* Help */
  87.                 strcpy(format,"#%s?");
  88.                 ch=0;
  89.             } else if (sc == 0x61) {
  90.                 /* Undo */
  91.                 strcpy(format,"#%s!");
  92.                 ch=0;
  93.             } else if (sc == 0x52) {
  94.                 /* Ins */
  95.                 strcpy(format,"#%sI");
  96.                 ch=0;
  97.             } else if (sc == 0x47) {
  98.                 /* Home */
  99.                 strcpy(format,"#%sH");
  100.                 ch=0;
  101.             } else if (sc == 0x48) {
  102.                 /* Up */
  103.                 strcpy(format,"#%sU");
  104.                 ch=0;
  105.             } else if (sc == 0x4b) {
  106.                 /* Left */
  107.                 strcpy(format,"#%sL");
  108.                 ch=0;
  109.             } else if (sc == 0x50) {
  110.                 /* Down */
  111.                 strcpy(format,"#%sD");
  112.                 ch=0;
  113.             } else if (sc == 0x4d) {
  114.                 /* Right */
  115.                 strcpy(format,"#%sR");
  116.                 ch=0;
  117.             }
  118.             if (!ch) {
  119.                 static char* keytable=0;
  120.                 char metastr[8];
  121.                 int m=0;
  122.  
  123.                 if (meta) {
  124.                     metastr[m++]='<';
  125.                     if (meta&CNT) metastr[m++] = 'c';
  126.                     if (meta&SHF) metastr[m++] = 's';
  127.                     if (meta&ALT) metastr[m++] = 'a';
  128.                     metastr[m++]='>';
  129.                 }
  130.                 metastr[m]='\0';
  131.  
  132.                 if (!keytable) {
  133.                     keytable=((char**)Keytbl(-1,-1,-1))[0];
  134.                 }
  135.  
  136.                 ch=keytable[sc];
  137.                 if (!ch) ch='*';
  138.  
  139.                 pos=sprintf(buf,format,metastr,ch);
  140.             } else {
  141.                 buf[pos++]=ch;
  142.             }
  143.         } else {
  144.             buf[pos++] = l;
  145.         }
  146.  
  147.         len=pos;
  148.     }
  149.     else /* I guess we timed out */
  150.     {
  151.         len = 0;
  152.     }
  153.  
  154.     /* cancel the alarm */
  155.     signal(SIGALRM, dummy); /* <-- to work around a bug in Minix */
  156.     alarm(0);
  157.  
  158.     /* return the number of bytes read */
  159.     if (len < 0)
  160.         len = 0;
  161.     return len;
  162. }
  163.  
  164. #endif /* MINT */
  165.