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