home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 019.lha / Aterm / FKeys.c < prev    next >
C/C++ Source or Header  |  1986-11-10  |  7KB  |  257 lines

  1. /* FKeys.c */ 
  2. #include <exec/types.h> 
  3. #include <stdio.h> 
  4. #include <fcntl.h>
  5. #include "ConsoleIO.h"
  6.   /* things for function keys */
  7. #define NUMKEYS 10            /* number of function keys */
  8. #define KEYSIZE 40            /* maximum length of any one string */
  9. #define BUFSIZE KEYSIZE * NUMKEYS   /* size of total buffer */
  10. #define HEADER  '\x70'    /* first byte of file is version number (7.0) */
  11. #define MAXOPTION 256               /* maximum length of option string */
  12. #define CSI     '\x9b'            /* Command Sequence Introducer */     
  13. #define DEL      127
  14. #define LDK       128
  15. #define LDX      129
  16. #define F10      130
  17.  
  18. extern UBYTE *KeyBuf;
  19.  
  20. /*********************************************************************
  21. **      Some routines concerned with the function keys        **
  22. *********************************************************************/
  23.  
  24. extern void SetOptions();
  25. extern int  GetOptions();
  26.            
  27. /************* Attempt to load a Function-Key file ******************/
  28.  
  29. BOOL LoadKeys(keyfile)    /* returns (BOOL) success    */
  30.  
  31.    char *keyfile;  /* the filename */
  32.  {
  33.       int fp;
  34.       int i;
  35.       short optsize;
  36.       BOOL filefound = FALSE;
  37.       BOOL goodfile  = TRUE;
  38.       UBYTE c, opt[ MAXOPTION ];
  39.  
  40.    if ( (fp = open(keyfile, O_RDONLY)) == -1)
  41.       return FALSE;
  42.    i = read( fp, &c, sizeof( c ) );
  43.    if ( i == sizeof( c ) && c == HEADER  )  /* gotta be a valid key file! */
  44.      {
  45.        filefound = TRUE;
  46.        i = read( fp, &optsize, sizeof( optsize ) ); 
  47.        if ( i != sizeof( optsize ) ||
  48.             !((0 <= optsize) && (optsize < MAXOPTION)) )
  49.       goodfile = FALSE;
  50.        else if ( read( fp, opt, optsize ) != optsize )
  51.       goodfile = FALSE;
  52.        else
  53.      {
  54.         SetOptions( opt, optsize );
  55.         if ( read( fp, KeyBuf, BUFSIZE ) != BUFSIZE )
  56.            goodfile = FALSE;
  57.      }
  58.       }
  59.         
  60.     close( fp );
  61.     if ( !goodfile )
  62.       {
  63.     PutString( "Key Buffer file has been corrupted...\n." );
  64.     PutString( "  Function Keys May be Invalid.\n" );
  65.       }
  66.     
  67.     return filefound;    /* ahh, success? */
  68.   }
  69. /**************Save Function-Key file *****************/
  70.  
  71. BOOL SaveFKeys(filename)    /* returns (BOOL) success */
  72.  char *filename;
  73.  
  74. {
  75.     int fp;
  76.     BOOL goodfile = TRUE;
  77.     short optsize;
  78.     UBYTE c = HEADER, opt[ MAXOPTION ];
  79.  
  80.     if ( ( fp = open( filename, O_WRONLY + O_CREAT )) == -1)
  81.        return FALSE;
  82.  
  83.     if ( write( fp, &c, sizeof( c)) != sizeof( c ))
  84.     goodfile = FALSE;
  85.     else if ( ( optsize = GetOptions( opt, MAXOPTION )) > MAXOPTION)
  86.       { 
  87.     PutString( "Can't Happen: Too many options to save.\n" );  
  88.     goodfile = FALSE;
  89.       }
  90.     else if ( write( fp, &optsize, sizeof( optsize )) != sizeof( optsize ))
  91.         goodfile = FALSE;
  92.     else if ( write( fp, opt, optsize ) != optsize )
  93.     goodfile = FALSE;
  94.     else if ( write( fp, KeyBuf, BUFSIZE ) != BUFSIZE )
  95.     goodfile = FALSE;
  96.  
  97.     close( fp );
  98.     if ( !goodfile )
  99.        PutString( "Error writing file.\n");
  100.  
  101.     return goodfile;
  102.  }
  103.  
  104. /************ Print the function-key number **************/
  105. /*        format example: F7:            */
  106.  
  107. static void PrintKeyNum(keynum)
  108.     int keynum;
  109. {
  110.    keynum++;
  111.    PutChar('F');
  112.    if ((keynum == 10) || (keynum == 0))
  113.       PutString("10: ");
  114.    else
  115.      {
  116.        PutChar(keynum + '0');
  117.        PutString(": ");
  118.      }
  119. }
  120.  
  121. /*********** Print a character from function-key string ************/
  122.  
  123. void keychar(key)
  124.    UBYTE key;
  125.  {
  126.    if ((key > 129) && (key < 140 ))
  127.      {
  128.        PutChar('<');
  129.        PrintKeyNum (key - 131);
  130.        PutString ("\b>");
  131.      }
  132.    else
  133.     {
  134.       if ((key < ' ') || (key > DEL))         /* control character */
  135.        {
  136.          switch (key)
  137.          { case 0:
  138.          PutChar('0');
  139.          break;
  140.        case '\b':
  141.          PutString("<BS>");
  142.          break;
  143.        case '\t':
  144.          PutString("<TAB>");
  145.          break;
  146.           case '\n':
  147.          PutString("<NL>");
  148.          break;
  149.           case '\r':
  150.          PutString("<CR>");
  151.          break;
  152.           case 27:
  153.          PutString("<ESC>");
  154.          break;
  155.           case LDK:
  156.              PutString("<LOAD>");
  157.              break;
  158.           case LDX:
  159.          PutString("<LOADEX>");
  160.          break;
  161.           default:
  162.          PutChar('^');
  163.          PutChar(key + 0x40);
  164.           }                /* end switch(key) */
  165.         }                /* end if control character */
  166.       else PutChar(key);
  167.     }                    /* end if FKey execute */
  168.   }
  169.  
  170. /************ Display Function-Key contents **********/
  171.  
  172. void DoContents()
  173.  
  174. {
  175.    int x;
  176.    UBYTE c, *offset;
  177.    
  178.  for (x=0; x < NUMKEYS; x++)
  179.   {
  180.     offset = KeyBuf + KEYSIZE * x;
  181.     PrintKeyNum( x );
  182.     while ( c = *offset++ )
  183.       keychar(c);
  184.     PutChar('\n');
  185.    }
  186.  }
  187.  
  188. /*******    Define a function-key string    *********/
  189.  
  190. void DefineString()
  191. {
  192.    UBYTE c, character;
  193.    int keynum, count, offset, i;
  194.  
  195.    PutString( "\x9b33m\nFunction-Key String Definition:\n\n");
  196.    if ( GetKey() != CSI )
  197.      { PutString( "\naborted...\n\n\x9bm" );
  198.        return;
  199.      }
  200.    else
  201.      {
  202.        character = GetKey();        /* get the meat of the sequence */
  203.        while ( GetKey() != '~' );   /* wait for sequence to end     */
  204.      }
  205.    if (!( (character >= '0') && (character <= '9') ))   /* isdigit? */
  206.      {
  207.     PutString( "aborted...\n\n\x9bm" );
  208.     return;
  209.       }
  210.    else
  211.       {
  212.      keynum = character - '0';
  213.      PrintKeyNum( keynum );
  214.      offset = KEYSIZE * keynum; /* find correct spot in buffer */
  215.      i = 0;
  216.      while ( (character = GetKey()) != CSI )    /* get the string */
  217.        { if (i<(KEYSIZE-1))
  218.          {
  219.         if(character == 127)       /* DEL key is used as a special  */
  220.           {               /* FKey 'CSI'. Takes the next    */
  221.             character = GetKey();  /* character as the function to  */
  222.             switch (character)       /* be stored in the FKey string. */
  223.               { case 'l':
  224.               character = LDK; /* <LOAD> -- loads another FKey  */
  225.               break;       /* definition file.            */
  226.             case 'L':
  227.               character = LDX; /* <LOADEX> -- takes the next    */
  228.               break;       /* character as the FKey to be   */
  229.                        /* executed AFTER loading the    */
  230.                        /* specified file. ie...         */
  231.                        /* <LOADEX>1key.file             */
  232.                        /* will load key.file, then      */
  233.                        /* execute FKey #1 as defined in */
  234.                        /* key.file                      */
  235.                        /* NOTE: 0 = FKey 10            */
  236.             default :
  237.               if ((character >= '0') && (character <= '9'))
  238.                 character += 82;
  239.               break;
  240.             }        /* end case */
  241.           }
  242.             KeyBuf[offset + (i++) ] = character;    /* store it      */
  243.             keychar(character);              /* display it, too */
  244.           }
  245.         }
  246.       while (GetKey() != '~' );    /* skip over rest of F-key    */
  247.            
  248.       while (i < KEYSIZE)
  249.         {
  250.            KeyBuf[offset+i] = '\0';    /* zero out the rest of        */
  251.            i++;            /* that string's buffer        */
  252.          }
  253.        PutString("\n\n");
  254.        PutString("\x9bm");
  255.       }    /* end else */
  256.  }    /* end DefineString();    */
  257.