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