home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD2.bin / bbs / dev / curses-2.10.lha / curses / src / _ansi.c next >
C/C++ Source or Header  |  1994-02-21  |  5KB  |  219 lines

  1. /* -*-C-*-
  2.  *
  3.  *
  4.  * Filename : _ansi.c
  5.  *
  6.  * Author   : Simon J Raybould.    (sie@fulcrum.bt.co.uk).
  7.  *
  8.  * Date     : Thursday 28th May 1992.
  9.  *
  10.  * Desc     : ANSI support for curses.
  11.  *            Implemented using termcap support.
  12.  *
  13.  *
  14.  * THIS CODE IS NOT PUBLIC DOMAIN
  15.  * ==============================
  16.  * 
  17.  * This code is copyright Simon J Raybould 1992, all rights are reserved.
  18.  * All code, ideas, data structures and algorithms remain the property of the
  19.  * author. Neither the whole nor sections of this code may be used as part
  20.  * of other code without the authors consent. If you wish to use some of this
  21.  * code then please email me at (sie@fulcrum.bt.co.uk).
  22.  *
  23.  * This source is not public domain, so you do not have any right to alter it
  24.  * or sell it for personal gain. The source is provided purely for reference
  25.  * purposes. You may re-compile the source with any compiler you choose.
  26.  * You must not distribute altered copies without the authors consent. My
  27.  * intention is that the source will help people isolate any bugs much more
  28.  * effectively.
  29.  *
  30.  * Disclaimer
  31.  * ==========
  32.  *
  33.  * No implication is made as to this code being fit for any purpose at all.
  34.  * I (the author) shall not be held responsible for any loss of data or damage 
  35.  * to property that may result from its use or misuse.
  36.  *
  37.  *
  38.  * Revision History
  39.  * ================
  40.  *
  41.  * $Log: _ansi.c,v $
  42.  * Revision 1.4  1994/02/21  22:20:22  sie
  43.  * removed _ANSIInit() as it is now replaced by setupterm().
  44.  *
  45.  * Revision 1.3  1993/05/17  23:33:10  sie
  46.  * Underscores added to names.
  47.  * Changes for version 2.10
  48.  *
  49.  * Revision 1.2  1992/12/25  23:43:43  sie
  50.  * GNU C port
  51.  *
  52.  * Revision 1.1  92/06/10  23:46:04  sie
  53.  * Initial revision
  54.  * 
  55.  *
  56.  */
  57.  
  58. #include <libraries/dosextens.h>
  59. #include <errno.h>
  60. #include <exec/memory.h>
  61. #include <fcntl.h>
  62. #include "acurses.h"
  63.  
  64. static char *rcsid = "$Header: /SRC/lib/curses/src/RCS/_ansi.c,v 1.4 1994/02/21 22:20:22 sie Exp $";
  65.  
  66.  
  67. static int
  68. _myputc(char c)
  69. {
  70.   fputc(c, stdout);
  71.   fflush(stdout);
  72.   return 0;
  73. }
  74.  
  75. void
  76. _ANSIClear()
  77. {
  78.   tputs(_clstr, 1, _myputc);
  79. }
  80.  
  81. void
  82. _ANSIMove(int line, int col)
  83. {
  84.   tputs(tgoto(_cmstr, col, line), 1, _myputc);
  85. }
  86.  
  87. void
  88. _ANSIFlash()
  89. {
  90.   fputc(BELL, stdout);
  91. }
  92.  
  93. void
  94. _ANSIClearRect(int line, int col, int height, int width)
  95. {
  96.   char buf[BUFSIZ], *ptr;
  97.   int i;
  98.   
  99.   if(!line && !col && height>=LINES-1 && width>=COLS-1) {
  100.     _ANSIClear();
  101.     return;
  102.   }
  103.   if(!(ptr = malloc(width+1)))    /* 1 for the NULL */
  104.     return;
  105.   memset(ptr, ' ', width);
  106.   ptr[width] = '\0';        /* terminate  */
  107.   for(i=line; i<line+height; i++) {
  108.     sprintf(buf, "\033[%d;%dH%s", i+1, col+1, ptr);
  109.     write(1, buf, strlen(buf));
  110.   }
  111.   free(ptr);
  112. }
  113.  
  114.  
  115. /*
  116.  *
  117.  * Author   : Chuck McManis.
  118.  *
  119.  * Date     : 18th June 1987.
  120.  *
  121.  * Desc     : These are routines for setting a given stream to RawMode or
  122.  *            CanonMode mode on the Amiga.
  123.  *
  124.  */
  125.  
  126.  
  127. /*
  128.  * send_packet() - written by Phil Lindsay, Carolyn Scheppner, and Andy
  129.  * Finkel. This function will send a packet of the given type to the Message
  130.  * Port supplied. 
  131.  */
  132.  
  133. static long
  134. _send_packet(struct MsgPort *pid, long action, long args[], long nargs)
  135. {
  136.     struct MsgPort *replyport;
  137.     struct StandardPacket *packet;
  138.  
  139.     long            count, *pargs, res1;
  140.  
  141.     replyport = (struct MsgPort *) CreatePort(NULL, 0);
  142.     if (!replyport)
  143.     return (0);
  144.  
  145.     /* Allocate space for a packet, make it public and clear it */
  146.     packet = (struct StandardPacket *)
  147.     AllocMem((long) sizeof(struct StandardPacket),
  148.          MEMF_PUBLIC | MEMF_CLEAR);
  149.     if (!packet) {
  150.     DeletePort(replyport);
  151.     return (0);
  152.     }
  153.     packet->sp_Msg.mn_Node.ln_Name = (char *) &(packet->sp_Pkt);
  154.     packet->sp_Pkt.dp_Link = &(packet->sp_Msg);
  155.     packet->sp_Pkt.dp_Port = replyport;
  156.     packet->sp_Pkt.dp_Type = action;
  157.  
  158.     /* copy the args into the packet */
  159.     pargs = &(packet->sp_Pkt.dp_Arg1);    /* address of first argument */
  160.     for (count = 0; count < nargs; count++)
  161.     pargs[count] = args[count];
  162.  
  163.     PutMsg(pid, (struct Message *)packet);    /* send packet */
  164.  
  165.     WaitPort(replyport);
  166.     GetMsg(replyport);
  167.  
  168.     res1 = packet->sp_Pkt.dp_Res1;
  169.  
  170.     FreeMem(packet, (long) sizeof(struct StandardPacket));
  171.     DeletePort(replyport);
  172.  
  173.     return (res1);
  174. }
  175.  
  176. /*
  177.  * Function RawMode() - Convert the specified File Handle to 'RawMode' mode. This
  178.  * only works on TTY's and essentially keeps DOS from translating keys for
  179.  * you.
  180.  */
  181.  
  182. long
  183. _RawMode(BPTR afh)
  184. {
  185.     struct MsgPort *mp;        /* The File Handle message port */
  186.     long            Arg[1], res;
  187.  
  188.     mp = ((struct FileHandle *) (BADDR(afh)))->fh_Type;
  189.     Arg[0] = -1L;
  190.     res = _send_packet(mp, ACTION_SCREEN_MODE, Arg, 1);
  191.     if (res == 0) {
  192.     errno = ENXIO;
  193.     return (-1);
  194.     }
  195.     return (0);
  196. }
  197.  
  198. /*
  199.  * Function - CanonMode() this function returns the designate file pointer to
  200.  * it's normal, wait for a <CR> mode. This is exactly like RawMode() except that
  201.  * it sends a 0 to the console to make it back into a CON: from a RAWMODE: 
  202.  */
  203.  
  204. long
  205. _CanonMode(BPTR afh)
  206. {
  207.     struct MsgPort *mp;        /* The File Handle message port */
  208.     long            Arg[1], res;
  209.  
  210.     mp = ((struct FileHandle *) (BADDR(afh)))->fh_Type;
  211.     Arg[0] = 0L;
  212.     res = _send_packet(mp, ACTION_SCREEN_MODE, Arg, 1);
  213.     if (res == 0) {
  214.     errno = ENXIO;
  215.     return (-1);
  216.     }
  217.     return (0);
  218. }
  219.