date and time created 89/08/02 19:14:19 by edward
[unix-history] / usr / src / usr.bin / window / xx.c
CommitLineData
a8577ffd
EW
1/*
2 * Copyright (c) 1989 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 */
17
18#ifndef lint
19static char sccsid[] = "@(#)xx.c 3.1 (Berkeley) %G%";
20#endif /* not lint */
21
22#include "ww.h"
23#include "xx.h"
24#include "tt.h"
25
26xxinit()
27{
28 if (ttinit() < 0)
29 return -1;
30 xxbufsize = tt.tt_nrow * tt.tt_ncol * 2;
31 xxbuf = malloc((unsigned) xxbufsize * sizeof *xxbuf);
32 if (xxbuf == 0) {
33 wwerrno = WWE_NOMEM;
34 return -1;
35 }
36 xxbufp = xxbuf;
37 xxbufe = xxbuf + xxbufsize;
38 if (tt.tt_ntoken > 0 && xcinit() < 0)
39 return -1;
40 return 0;
41}
42
43xxstart()
44{
45 (*tt.tt_start)();
46 if (tt.tt_ntoken > 0)
47 xcstart();
48 xxreset(); /* might be a restart */
49}
50
51xxend()
52{
53 if (tt.tt_scroll_top != 0 || tt.tt_scroll_bot != tt.tt_nrow - 1)
54 /* tt.tt_setscroll is known to be defined */
55 (*tt.tt_setscroll)(0, tt.tt_nrow - 1);
56 if (tt.tt_insert)
57 (*tt.tt_setinsert)(0);
58 if (tt.tt_modes)
59 (*tt.tt_setmodes)(0);
60 if (tt.tt_scroll_down)
61 (*tt.tt_scroll_down)(1);
62 (*tt.tt_move)(tt.tt_nrow - 1, 0);
63 (*tt.tt_end)();
64 ttflush();
65}
66
67struct xx *
68xxalloc()
69{
70 register struct xx *xp;
71
72 if (xxbufp > xxbufe)
73 abort();
74 if ((xp = xx_freelist) == 0)
75 /* XXX can't deal with failure */
76 xp = (struct xx *) malloc((unsigned) sizeof *xp);
77 else
78 xx_freelist = xp->link;
79 if (xx_head == 0)
80 xx_head = xp;
81 else
82 xx_tail->link = xp;
83 xx_tail = xp;
84 xp->link = 0;
85 return xp;
86}
87
88xxfree(xp)
89 register struct xx *xp;
90{
91 xp->link = xx_freelist;
92 xx_freelist = xp;
93}
94
95xxmove(row, col)
96{
97 register struct xx *xp = xx_tail;
98
99 if (xp == 0 || xp->cmd != xc_move) {
100 xp = xxalloc();
101 xp->cmd = xc_move;
102 }
103 xp->arg0 = row;
104 xp->arg1 = col;
105}
106
107xxscroll(dir, top, bot)
108{
109 register struct xx *xp = xx_tail;
110
111 if (xp != 0 && xp->cmd == xc_scroll &&
112 xp->arg1 == top && xp->arg2 == bot &&
113 (xp->arg0 < 0 && dir < 0 || xp->arg0 > 0 && dir > 0)) {
114 xp->arg0 += dir;
115 return;
116 }
117 xp = xxalloc();
118 xp->cmd = xc_scroll;
119 xp->arg0 = dir;
120 xp->arg1 = top;
121 xp->arg2 = bot;
122}
123
124xxinschar(row, col, c)
125{
126 register struct xx *xp = xx_tail;
127 int m = c >> WWC_MSHIFT;
128
129 if (xxbufp >= xxbufe)
130 xxflush(0);
131 c &= WWC_CMASK;
132 if (xp != 0 && xp->cmd == xc_inschar &&
133 xp->arg0 == row && xp->arg1 + xp->arg2 == col && xp->arg3 == m) {
134 xp->buf[xp->arg2++] = c;
135 xxbufp++;
136 return;
137 }
138 xp = xxalloc();
139 xp->cmd = xc_inschar;
140 xp->arg0 = row;
141 xp->arg1 = col;
142 xp->arg2 = 1;
143 xp->arg3 = m;
144 xp->buf = xxbufp++;
145 *xp->buf = c;
146}
147
148xxdelchar(row, col)
149{
150 register struct xx *xp = xx_tail;
151
152 if (xp != 0 && xp->cmd == xc_delchar &&
153 xp->arg0 == row && xp->arg1 == col) {
154 xp->arg2++;
155 return;
156 }
157 xp = xxalloc();
158 xp->cmd = xc_delchar;
159 xp->arg0 = row;
160 xp->arg1 = col;
161 xp->arg2 = 1;
162}
163
164xxclear()
165{
166 register struct xx *xp;
167
168 xxreset();
169 xp = xxalloc();
170 xp->cmd = xc_clear;
171}
172
173xxclreos(row, col)
174{
175 register struct xx *xp = xxalloc();
176
177 xp->cmd = xc_clreos;
178 xp->arg0 = row;
179 xp->arg1 = col;
180}
181
182xxclreol(row, col)
183{
184 register struct xx *xp = xxalloc();
185
186 xp->cmd = xc_clreol;
187 xp->arg0 = row;
188 xp->arg1 = col;
189}
190
191xxwrite(row, col, p, n, m)
192 char *p;
193{
194 register struct xx *xp;
195
196 if (xxbufp + n > xxbufe)
197 xxflush(0);
198 xp = xxalloc();
199 xp->cmd = xc_write;
200 xp->arg0 = row;
201 xp->arg1 = col;
202 xp->arg2 = n;
203 xp->arg3 = m;
204 xp->buf = xxbufp;
205 bcopy(p, xxbufp, n);
206 xxbufp += n;
207 if (tt.tt_ntoken > 0)
208 xcscan(xp->buf, n, xp->buf - xxbuf);
209}
210
211xxreset()
212{
213 register struct xx *xp, *xq;
214
215 for (xp = xx_head; xp != 0; xp = xq) {
216 xq = xp->link;
217 xxfree(xp);
218 }
219 xx_tail = xx_head = 0;
220 xxbufp = xxbuf;
221 if (tt.tt_ntoken > 0)
222 xcreset();
223}