]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/window/xx.c
This commit was generated by cvs2svn to compensate for changes in r76371,
[FreeBSD/FreeBSD.git] / usr.bin / window / xx.c
1 /*
2  * Copyright (c) 1989, 1993
3  *      The Regents of the University of California.  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        8.1 (Berkeley) 6/6/93";
39 static char rcsid[] = "@(#)$FreeBSD$";
40 #endif /* not lint */
41
42 #include "ww.h"
43 #include "xx.h"
44 #include "tt.h"
45 #include <stdlib.h>
46
47 xxinit()
48 {
49         if (ttinit() < 0)
50                 return -1;
51         xxbufsize = tt.tt_nrow * tt.tt_ncol * 2;
52         /* ccinit may choose to change xxbufsize */
53         if (tt.tt_ntoken > 0 && ccinit() < 0)
54                 return -1;
55         xxbuf = malloc((unsigned) xxbufsize * sizeof *xxbuf);
56         if (xxbuf == 0) {
57                 wwerrno = WWE_NOMEM;
58                 return -1;
59         }
60         xxbufp = xxbuf;
61         xxbufe = xxbuf + xxbufsize;
62         return 0;
63 }
64
65 xxstart()
66 {
67         (*tt.tt_start)();
68         if (tt.tt_ntoken > 0)
69                 ccstart();
70         xxreset1();                     /* might be a restart */
71 }
72
73 xxreset()
74 {
75         if (tt.tt_ntoken > 0)
76                 ccreset();
77         xxreset1();
78         (*tt.tt_reset)();
79 }
80
81 xxreset1()
82 {
83         register struct xx *xp, *xq;
84
85         for (xp = xx_head; xp != 0; xp = xq) {
86                 xq = xp->link;
87                 xxfree(xp);
88         }
89         xx_tail = xx_head = 0;
90         xxbufp = xxbuf;
91 }
92
93 xxend()
94 {
95         if (tt.tt_scroll_top != 0 || tt.tt_scroll_bot != tt.tt_nrow - 1)
96                 /* tt.tt_setscroll is known to be defined */
97                 (*tt.tt_setscroll)(0, tt.tt_nrow - 1);
98         if (tt.tt_modes)
99                 (*tt.tt_setmodes)(0);
100         if (tt.tt_scroll_down)
101                 (*tt.tt_scroll_down)(1);
102         (*tt.tt_move)(tt.tt_nrow - 1, 0);
103         if (tt.tt_ntoken > 0)
104                 ccend();
105         (*tt.tt_end)();
106         ttflush();
107 }
108
109 struct xx *
110 xxalloc()
111 {
112         register struct xx *xp;
113
114         if (xxbufp > xxbufe)
115                 abort();
116         if ((xp = xx_freelist) == 0)
117                 /* XXX can't deal with failure */
118                 xp = (struct xx *) malloc((unsigned) sizeof *xp);
119         else
120                 xx_freelist = xp->link;
121         if (xx_head == 0)
122                 xx_head = xp;
123         else
124                 xx_tail->link = xp;
125         xx_tail = xp;
126         xp->link = 0;
127         return xp;
128 }
129
130 xxfree(xp)
131         register struct xx *xp;
132 {
133         xp->link = xx_freelist;
134         xx_freelist = xp;
135 }
136
137 xxmove(row, col)
138 {
139         register struct xx *xp = xx_tail;
140
141         if (xp == 0 || xp->cmd != xc_move) {
142                 xp = xxalloc();
143                 xp->cmd = xc_move;
144         }
145         xp->arg0 = row;
146         xp->arg1 = col;
147 }
148
149 xxscroll(dir, top, bot)
150 {
151         register struct xx *xp = xx_tail;
152
153         if (xp != 0 && xp->cmd == xc_scroll &&
154             xp->arg1 == top && xp->arg2 == bot &&
155             (xp->arg0 < 0 && dir < 0 || xp->arg0 > 0 && dir > 0)) {
156                 xp->arg0 += dir;
157                 return;
158         }
159         xp = xxalloc();
160         xp->cmd = xc_scroll;
161         xp->arg0 = dir;
162         xp->arg1 = top;
163         xp->arg2 = bot;
164 }
165
166 xxinschar(row, col, c, m)
167 {
168         register struct xx *xp;
169
170         xp = xxalloc();
171         xp->cmd = xc_inschar;
172         xp->arg0 = row;
173         xp->arg1 = col;
174         xp->arg2 = c;
175         xp->arg3 = m;
176 }
177
178 xxinsspace(row, col)
179 {
180         register struct xx *xp = xx_tail;
181
182         if (xp != 0 && xp->cmd == xc_insspace && xp->arg0 == row &&
183             col >= xp->arg1 && col <= xp->arg1 + xp->arg2) {
184                 xp->arg2++;
185                 return;
186         }
187         xp = xxalloc();
188         xp->cmd = xc_insspace;
189         xp->arg0 = row;
190         xp->arg1 = col;
191         xp->arg2 = 1;
192 }
193
194 xxdelchar(row, col)
195 {
196         register struct xx *xp = xx_tail;
197
198         if (xp != 0 && xp->cmd == xc_delchar &&
199             xp->arg0 == row && xp->arg1 == col) {
200                 xp->arg2++;
201                 return;
202         }
203         xp = xxalloc();
204         xp->cmd = xc_delchar;
205         xp->arg0 = row;
206         xp->arg1 = col;
207         xp->arg2 = 1;
208 }
209
210 xxclear()
211 {
212         register struct xx *xp;
213
214         xxreset1();
215         xp = xxalloc();
216         xp->cmd = xc_clear;
217 }
218
219 xxclreos(row, col)
220 {
221         register struct xx *xp = xxalloc();
222
223         xp->cmd = xc_clreos;
224         xp->arg0 = row;
225         xp->arg1 = col;
226 }
227
228 xxclreol(row, col)
229 {
230         register struct xx *xp = xxalloc();
231
232         xp->cmd = xc_clreol;
233         xp->arg0 = row;
234         xp->arg1 = col;
235 }
236
237 xxwrite(row, col, p, n, m)
238         char *p;
239 {
240         register struct xx *xp;
241
242         if (xxbufp + n + 1 > xxbufe)
243                 xxflush(0);
244         xp = xxalloc();
245         xp->cmd = xc_write;
246         xp->arg0 = row;
247         xp->arg1 = col;
248         xp->arg2 = n;
249         xp->arg3 = m;
250         xp->buf = xxbufp;
251         bcopy(p, xxbufp, n);
252         xxbufp += n;
253         *xxbufp++ = char_sep;
254 }