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

  1. /*
  2. ConsoleIO.c
  3. Contains info and code necessary for opening the console device for
  4. input and output.
  5. Functions in the file include InitConsoleIO(), CloseConsoleIO(),
  6. PutChar(), PutString() PutFormat(),
  7. CheckKey(),GetKey(),
  8. CursorXY(),PutSequence(),GetLine();
  9. */
  10.  
  11. #include <exec/types.h>
  12. #include <stdio.h>
  13. #include <exec/io.h>
  14. #include <intuition/intuition.h>
  15. #include <functions.h>
  16.  
  17. #define CSI            '\x9b'  /* Command Sequence Introducer */
  18. #define PRINTBUFSIZE    130
  19. #define SQUIRTCHAR    '\xff'
  20.  
  21. static UBYTE buffer[PRINTBUFSIZE];
  22. static int count;
  23.  
  24. static UBYTE readbuf[8];
  25.  
  26. static struct MsgPort *ConPort  = NULL;
  27. static struct MsgPort *ReadPort = NULL;
  28.  
  29. static struct IOStdReq *WriteMsg = NULL;
  30. static struct IOStdReq *ReadMsg  = NULL;
  31.  
  32. /*    Set Up the IO Paths:
  33.       Returns pointer to input (keyboard) IOStdReq structure or zero.
  34.       The IOStdReq structure may be used in a wait() command.
  35.      You must have the address of your window structure to open the
  36.      console.
  37. */
  38.  
  39.  
  40. /******************** Close Up the Console *************************/
  41.  
  42. void CloseConsoleIO()
  43.  
  44. {
  45.    register struct IOStdReq *r = ReadMsg;
  46.    register struct IOStdReq *w = WriteMsg;
  47.  
  48.    if ( w != NULL )
  49.      {
  50.         CloseDevice( w );
  51.         DeleteStdIO( w );
  52.         WriteMsg = NULL;
  53.      }
  54.    if ( r != NULL )
  55.      {
  56.         DeleteStdIO( r );
  57.         ReadMsg = NULL;
  58.      }
  59.    if ( ReadPort != NULL )
  60.      {
  61.         RemPort(ReadPort);
  62.         ReadPort = NULL;
  63.      }
  64.    if ( ConPort != NULL )
  65.      {
  66.         RemPort( ConPort);
  67.         ConPort = NULL;
  68.      }
  69.  }
  70.  
  71. /*--------------------------------------------------------*/
  72. /*    GetConsoleSigBit: Return console signal bit      */
  73. /*--------------------------------------------------------*/
  74.  
  75. int GetConsoleSigBit()
  76. {
  77.    return ReadMsg->io_Message.mn_ReplyPort->mp_SigBit; 
  78. }
  79.  
  80. /*--------------------------------------------------------*/
  81. /*    InitConsole: return read request pointer      */
  82. /*--------------------------------------------------------*/
  83.  
  84. struct IOStdReq *InitConsoleIO(userwindow)
  85. struct Window *userwindow;
  86. {
  87.  
  88.    register struct IOStdReq *r;
  89.    register struct IOStdReq *w;
  90.  
  91.    if ( (ConPort = CreatePort("writeport",NULL)) == NULL)
  92.        return(NULL);
  93.  
  94.    if ( (ReadPort = CreatePort("readport",NULL)) == NULL)
  95.        goto badopen;
  96.  
  97.    if ( (WriteMsg = CreateStdIO(ConPort)) == NULL)
  98.        goto badopen;
  99.  
  100.    if ( (ReadMsg = CreateStdIO(ReadPort)) == NULL)
  101.        goto badopen;
  102.  
  103.    r = ReadMsg;
  104.    w = WriteMsg;
  105.  
  106.    w->io_Data= (APTR) userwindow;
  107.    w->io_Length = sizeof(*userwindow);
  108.    if (OpenDevice("console.device", NULL, w, NULL) )
  109.        goto badopen;
  110.  
  111.    w->io_Command = CMD_WRITE;
  112.  
  113.    r->io_Device = w->io_Device;
  114.    r->io_Unit = w->io_Unit;
  115.    r->io_Command = CMD_READ;
  116.    r->io_Data = (APTR)readbuf;
  117.    r->io_Length = 1;
  118.  
  119.    SendIO(r);  /* ask for keypresses */
  120.    count = 0;
  121.    return( r );/* Ports successfully opened */
  122.  
  123. badopen:
  124.    CloseConsoleIO();
  125.    return NULL;   
  126.  
  127. }    /* End of InitConsole(userwindow) */
  128.  
  129.  
  130. /************* develop a string, then print it all at once **************/
  131.  
  132. /*  This function is designed to be used with Manx's format() function  */
  133. /*  Usage: format(PutFormat,format_string...);                */
  134. /*  Your calls must have one added character at the end            */
  135. /*  of the format_string-- 255 (-1). Use \xff.                */
  136. /*  This tells PutFormat to go print the string it has been building.    */
  137. /*  Make sure your format_string ends with '\xff', otherwise        */
  138. /*  the string won't print properly.                    */
  139.  
  140. void PutFormat(character)
  141. register UBYTE character;
  142. {  
  143.   register struct IOStdReq *w = WriteMsg;
  144.  
  145.   if ( (character == SQUIRTCHAR) || (count >= PRINTBUFSIZE - 3) )
  146.     {
  147.       w->io_Data = (APTR)buffer;
  148.       w->io_Length = count;    /* subtract \xff count    */
  149.       DoIO( w );
  150.       count = 0;
  151.      }
  152.   if ( character != SQUIRTCHAR )
  153.      {    
  154.     buffer[count++] = character;
  155.     if (character == '\b')        /* if backspace */
  156.       {
  157.         buffer[count++] = ' ';    /* put <BS> <SP> <BS> */
  158.         buffer[count++] = character;
  159.       }
  160.      }
  161.  }
  162.  
  163. /***************** Put a character to screen *********************/
  164.  
  165. void PutChar(character)
  166.   UBYTE character;
  167.  {
  168.    register struct IOStdReq *w = WriteMsg;
  169.                                 /* io_Command is always WRITE */
  170.    if ( count )
  171.       PutFormat( SQUIRTCHAR );
  172.  
  173.    if (character == '\b')
  174.     {
  175.       w->io_Data = (APTR) "\b \b";
  176.       w->io_Length = 3;
  177.       DoIO( w );
  178.      }
  179.    else
  180.     {
  181.       w->io_Data = (APTR) &character;
  182.       w->io_Length = 1;
  183.       DoIO( w );
  184.      }
  185.   }
  186.  
  187. /*********** Put a null-terminated string to screen ****************/
  188.  
  189. void PutString(string)
  190.   UBYTE *string;
  191.  {
  192.   register struct IOStdReq *w = WriteMsg;
  193.                              /* io_Command is always WRITE */
  194.    if ( count )
  195.       PutFormat( SQUIRTCHAR );
  196.  
  197.    w->io_Data = (APTR) string;
  198.    w->io_Length = -1;
  199.    DoIO( w );
  200.   }
  201.  
  202. /********************* Move cursor to column,row ************************/
  203.  
  204. void CursorXY(x, y)     /* note: x,y is column, row */
  205. int x, y;
  206. {
  207.    struct parm 
  208.      {
  209.     int row;
  210.     int col;
  211.      } parms;
  212.    parms.row = y;
  213.    parms.col = x;
  214.    if ( !( (1 <= y && y <= 22)
  215.          &&(1 <= x && x <= 79) ))
  216.      {
  217.         format( PutFormat, "Bad Cursor x=%d, y= %d\n\xFF", &parms );
  218.     parms.row = 1;
  219.     parms.col = 1;
  220.      }
  221.       
  222.    format(PutFormat,"\x9B%d;%dH\xFF", &parms );
  223. }
  224.  
  225.  
  226. /**************** Print an escape sequence *********************/
  227.  
  228. void PutSequence(string)  /* this routine appends your string onto */
  229. UBYTE *string;                /* a CSI, and prints it to console */
  230.  {
  231.    PutChar(CSI);
  232.    PutString(string);
  233.   }
  234.  
  235.  
  236. /***************** If key pressed, get it: else return **************/
  237.  
  238. UBYTE CheckKey()
  239.  
  240.  {
  241.     UBYTE temp;
  242.    if (GetMsg(ReadPort) == NULL)
  243.        return 0;
  244.    temp = readbuf[0];
  245.    SendIO( ReadMsg );
  246.    return(temp);
  247.   }
  248.  
  249. /************** Wait as long as necessary for keypress **************/
  250.  
  251. UBYTE GetKey()
  252.  
  253.  {
  254.    UBYTE temp;
  255.  
  256.    while (GetMsg(ReadPort) == NULL)
  257.       WaitPort(ReadPort);
  258.    temp = readbuf[0];
  259.    SendIO( ReadMsg );
  260.    return temp;
  261.   }
  262.  
  263. /*************** Get a line of input from keyboard ******************/
  264.  
  265.  
  266. BOOL GetLine(prompt,inputbuf)
  267.  
  268.  UBYTE *prompt;
  269.  register UBYTE *inputbuf;
  270.  
  271.  {
  272.    register struct IOStdReq *r = ReadMsg;
  273.    UBYTE c;
  274.    int i=0;
  275.  
  276.    if (prompt)
  277.       PutString(prompt);
  278.  
  279.    inputbuf[ i ] = '\0';             
  280.    while ( (c=GetKey()) != '\r')   /* until c/r */
  281.      switch (c)
  282.        {
  283.          case 3:           /* ^C aborts */
  284.          case 24:          /* ^X aborts */
  285.              return FALSE;
  286.          case '\b':           /* backspace */
  287.              if (i>0)
  288.                {
  289.                  inputbuf[--i] = '\0';
  290.                  PutString( "\b \b" );
  291.                }
  292.              break;
  293.          default:
  294.              if ((c >= ' ') && (c < '\x7f'))
  295.                {
  296.           inputbuf[i++] = c;
  297.          inputbuf[i] = '\0';
  298.          PutChar(c);
  299.            }
  300.         }
  301.    PutChar('\n');
  302.    return ( inputbuf[0] != '\0' );
  303.   }
  304.  
  305.