home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / window / xx.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-18  |  5.2 KB  |  245 lines

  1. /*
  2.  * Copyright (c) 1989 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Edward Wang at The University of California, Berkeley.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #ifndef lint
  38. static char sccsid[] = "@(#)xx.c    3.6 (Berkeley) 6/6/90";
  39. #endif /* not lint */
  40.  
  41. #include "ww.h"
  42. #include "xx.h"
  43. #include "tt.h"
  44.  
  45. xxinit()
  46. {
  47.     if (ttinit() < 0)
  48.         return -1;
  49.     xxbufsize = tt.tt_nrow * tt.tt_ncol * 2;
  50.     /* ccinit may choose to change xxbufsize */
  51.     if (tt.tt_ntoken > 0 && ccinit() < 0)
  52.         return -1;
  53.     xxbuf = malloc((unsigned) xxbufsize * sizeof *xxbuf);
  54.     if (xxbuf == 0) {
  55.         wwerrno = WWE_NOMEM;
  56.         return -1;
  57.     }
  58.     xxbufp = xxbuf;
  59.     xxbufe = xxbuf + xxbufsize;
  60.     return 0;
  61. }
  62.  
  63. xxstart()
  64. {
  65.     (*tt.tt_start)();
  66.     if (tt.tt_ntoken > 0)
  67.         ccstart();
  68.     xxreset();            /* might be a restart */
  69. }
  70.  
  71. xxend()
  72. {
  73.     if (tt.tt_scroll_top != 0 || tt.tt_scroll_bot != tt.tt_nrow - 1)
  74.         /* tt.tt_setscroll is known to be defined */
  75.         (*tt.tt_setscroll)(0, tt.tt_nrow - 1);
  76.     if (tt.tt_modes)
  77.         (*tt.tt_setmodes)(0);
  78.     if (tt.tt_scroll_down)
  79.         (*tt.tt_scroll_down)(1);
  80.     (*tt.tt_move)(tt.tt_nrow - 1, 0);
  81.     if (tt.tt_ntoken > 0)
  82.         ccend();
  83.     (*tt.tt_end)();
  84.     (*tt.tt_flush)();
  85. }
  86.  
  87. struct xx *
  88. xxalloc()
  89. {
  90.     register struct xx *xp;
  91.  
  92.     if (xxbufp > xxbufe)
  93.         abort();
  94.     if ((xp = xx_freelist) == 0)
  95.         /* XXX can't deal with failure */
  96.         xp = (struct xx *) malloc((unsigned) sizeof *xp);
  97.     else
  98.         xx_freelist = xp->link;
  99.     if (xx_head == 0)
  100.         xx_head = xp;
  101.     else
  102.         xx_tail->link = xp;
  103.     xx_tail = xp;
  104.     xp->link = 0;
  105.     return xp;
  106. }
  107.  
  108. xxfree(xp)
  109.     register struct xx *xp;
  110. {
  111.     xp->link = xx_freelist;
  112.     xx_freelist = xp;
  113. }
  114.  
  115. xxmove(row, col)
  116. {
  117.     register struct xx *xp = xx_tail;
  118.  
  119.     if (xp == 0 || xp->cmd != xc_move) {
  120.         xp = xxalloc();
  121.         xp->cmd = xc_move;
  122.     }
  123.     xp->arg0 = row;
  124.     xp->arg1 = col;
  125. }
  126.  
  127. xxscroll(dir, top, bot)
  128. {
  129.     register struct xx *xp = xx_tail;
  130.  
  131.     if (xp != 0 && xp->cmd == xc_scroll &&
  132.         xp->arg1 == top && xp->arg2 == bot &&
  133.         (xp->arg0 < 0 && dir < 0 || xp->arg0 > 0 && dir > 0)) {
  134.         xp->arg0 += dir;
  135.         return;
  136.     }
  137.     xp = xxalloc();
  138.     xp->cmd = xc_scroll;
  139.     xp->arg0 = dir;
  140.     xp->arg1 = top;
  141.     xp->arg2 = bot;
  142. }
  143.  
  144. xxinschar(row, col, c, m)
  145. {
  146.     register struct xx *xp;
  147.  
  148.     xp = xxalloc();
  149.     xp->cmd = xc_inschar;
  150.     xp->arg0 = row;
  151.     xp->arg1 = col;
  152.     xp->arg2 = c;
  153.     xp->arg3 = m;
  154. }
  155.  
  156. xxinsspace(row, col)
  157. {
  158.     register struct xx *xp = xx_tail;
  159.  
  160.     if (xp != 0 && xp->cmd == xc_insspace && xp->arg0 == row &&
  161.         col >= xp->arg1 && col <= xp->arg1 + xp->arg2) {
  162.         xp->arg2++;
  163.         return;
  164.     }
  165.     xp = xxalloc();
  166.     xp->cmd = xc_insspace;
  167.     xp->arg0 = row;
  168.     xp->arg1 = col;
  169.     xp->arg2 = 1;
  170. }
  171.  
  172. xxdelchar(row, col)
  173. {
  174.     register struct xx *xp = xx_tail;
  175.  
  176.     if (xp != 0 && xp->cmd == xc_delchar &&
  177.         xp->arg0 == row && xp->arg1 == col) {
  178.         xp->arg2++;
  179.         return;
  180.     }
  181.     xp = xxalloc();
  182.     xp->cmd = xc_delchar;
  183.     xp->arg0 = row;
  184.     xp->arg1 = col;
  185.     xp->arg2 = 1;
  186. }
  187.  
  188. xxclear()
  189. {
  190.     register struct xx *xp;
  191.  
  192.     xxreset();
  193.     xp = xxalloc();
  194.     xp->cmd = xc_clear;
  195. }
  196.  
  197. xxclreos(row, col)
  198. {
  199.     register struct xx *xp = xxalloc();
  200.  
  201.     xp->cmd = xc_clreos;
  202.     xp->arg0 = row;
  203.     xp->arg1 = col;
  204. }
  205.  
  206. xxclreol(row, col)
  207. {
  208.     register struct xx *xp = xxalloc();
  209.  
  210.     xp->cmd = xc_clreol;
  211.     xp->arg0 = row;
  212.     xp->arg1 = col;
  213. }
  214.  
  215. xxwrite(row, col, p, n, m)
  216.     char *p;
  217. {
  218.     register struct xx *xp;
  219.  
  220.     if (xxbufp + n + 1 > xxbufe)
  221.         xxflush(0);
  222.     xp = xxalloc();
  223.     xp->cmd = xc_write;
  224.     xp->arg0 = row;
  225.     xp->arg1 = col;
  226.     xp->arg2 = n;
  227.     xp->arg3 = m;
  228.     xp->buf = xxbufp;
  229.     bcopy(p, xxbufp, n);
  230.     xxbufp += n;
  231.     *xxbufp++ = char_sep;
  232. }
  233.  
  234. xxreset()
  235. {
  236.     register struct xx *xp, *xq;
  237.  
  238.     for (xp = xx_head; xp != 0; xp = xq) {
  239.         xq = xp->link;
  240.         xxfree(xp);
  241.     }
  242.     xx_tail = xx_head = 0;
  243.     xxbufp = xxbuf;
  244. }
  245.