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