home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / lib / libcurses / addbytes.c next >
Encoding:
C/C++ Source or Header  |  1992-08-28  |  3.8 KB  |  134 lines

  1. /*
  2.  * Copyright (c) 1987 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)addbytes.c    5.8 (Berkeley) 8/28/92";
  36. #endif    /* not lint */
  37.  
  38. #include <curses.h>
  39. #include <termios.h>
  40.  
  41. #define    SYNCH_IN    {y = win->_cury; x = win->_curx;}
  42. #define    SYNCH_OUT    {win->_cury = y; win->_curx = x;}
  43.  
  44. /*
  45.  * waddbytes --
  46.  *    Add the character to the current position in the given window.
  47.  */
  48. int
  49. waddbytes(win, bytes, count)
  50.     register WINDOW *win;
  51.     register char *bytes;
  52.     register int count;
  53. {
  54.     static char blanks[] = "        ";
  55.     register int c, newx, x, y;
  56.  
  57.     SYNCH_IN;
  58.  
  59. #ifdef DEBUG
  60.     __TRACE("ADDBYTES('%c') at (%d, %d)\n", c, y, x);
  61. #endif
  62.     while (count--) {
  63.         c = *bytes++;
  64.         switch (c) {
  65.         case '\t':
  66.             SYNCH_OUT;
  67.             if (waddbytes(win, blanks, 8 - (x % 8)) == ERR)
  68.                 return (ERR);
  69.             SYNCH_IN;
  70.             break;
  71.  
  72.         default:
  73. #ifdef DEBUG
  74.     __TRACE("ADDBYTES: 1: y = %d, x = %d, firstch = %d, lastch = %d\n",
  75.         y, x, win->_firstch[y], win->_lastch[y]);
  76. #endif
  77.             if (win->_flags & _STANDOUT)
  78.                 c |= _STANDOUT;
  79. #ifdef DEBUG
  80.     __TRACE("ADDBYTES(%0.2o, %d, %d)\n", win, y, x);
  81. #endif
  82.             if (win->_y[y][x] != c) {
  83.                 newx = x + win->_ch_off;
  84.                 if (win->_firstch[y] == _NOCHANGE)
  85.                     win->_firstch[y] =
  86.                         win->_lastch[y] = newx;
  87.                 else if (newx < win->_firstch[y])
  88.                     win->_firstch[y] = newx;
  89.                 else if (newx > win->_lastch[y])
  90.                     win->_lastch[y] = newx;
  91. #ifdef DEBUG
  92.     __TRACE("ADDBYTES: change gives f/l: %d/%d [%d/%d]\n",
  93.         win->_firstch[y], win->_lastch[y],
  94.         win->_firstch[y] - win->_ch_off,
  95.         win->_lastch[y] - win->_ch_off);
  96. #endif
  97.             }
  98.             win->_y[y][x] = c;
  99.             if (++x >= win->_maxx) {
  100.                 x = 0;
  101. newline:            if (++y >= win->_maxy)
  102.                     if (win->_scroll) {
  103.                         SYNCH_OUT;
  104.                         scroll(win);
  105.                         SYNCH_IN;
  106.                         --y;
  107.                     } else
  108.                         return (ERR);
  109.             }
  110. #ifdef DEBUG
  111.     __TRACE("ADDBYTES: 2: y = %d, x = %d, firstch = %d, lastch = %d\n",
  112.         y, x, win->_firstch[y], win->_lastch[y]);
  113. #endif
  114.             break;
  115.         case '\n':
  116.             SYNCH_OUT;
  117.             wclrtoeol(win);
  118.             SYNCH_IN;
  119.             if (origtermio.c_oflag & ONLCR)
  120.                 x = 0;
  121.             goto newline;
  122.         case '\r':
  123.             x = 0;
  124.             break;
  125.         case '\b':
  126.             if (--x < 0)
  127.                 x = 0;
  128.             break;
  129.         }
  130.     }
  131.     SYNCH_OUT;
  132.     return (OK);
  133. }
  134.