insert mode bug fix and cleanup
[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
d9375810 19static char sccsid[] = "@(#)xx.c 3.2 (Berkeley) %G%";
a8577ffd
EW
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);
a8577ffd
EW
56 if (tt.tt_modes)
57 (*tt.tt_setmodes)(0);
58 if (tt.tt_scroll_down)
59 (*tt.tt_scroll_down)(1);
60 (*tt.tt_move)(tt.tt_nrow - 1, 0);
61 (*tt.tt_end)();
62 ttflush();
63}
64
65struct xx *
66xxalloc()
67{
68 register struct xx *xp;
69
70 if (xxbufp > xxbufe)
71 abort();
72 if ((xp = xx_freelist) == 0)
73 /* XXX can't deal with failure */
74 xp = (struct xx *) malloc((unsigned) sizeof *xp);
75 else
76 xx_freelist = xp->link;
77 if (xx_head == 0)
78 xx_head = xp;
79 else
80 xx_tail->link = xp;
81 xx_tail = xp;
82 xp->link = 0;
83 return xp;
84}
85
86xxfree(xp)
87 register struct xx *xp;
88{
89 xp->link = xx_freelist;
90 xx_freelist = xp;
91}
92
93xxmove(row, col)
94{
95 register struct xx *xp = xx_tail;
96
97 if (xp == 0 || xp->cmd != xc_move) {
98 xp = xxalloc();
99 xp->cmd = xc_move;
100 }
101 xp->arg0 = row;
102 xp->arg1 = col;
103}
104
105xxscroll(dir, top, bot)
106{
107 register struct xx *xp = xx_tail;
108
109 if (xp != 0 && xp->cmd == xc_scroll &&
110 xp->arg1 == top && xp->arg2 == bot &&
111 (xp->arg0 < 0 && dir < 0 || xp->arg0 > 0 && dir > 0)) {
112 xp->arg0 += dir;
113 return;
114 }
115 xp = xxalloc();
116 xp->cmd = xc_scroll;
117 xp->arg0 = dir;
118 xp->arg1 = top;
119 xp->arg2 = bot;
120}
121
d9375810
EW
122xxinschar(row, col, c, m)
123{
124 register struct xx *xp;
125
126 xp = xxalloc();
127 xp->cmd = xc_inschar;
128 xp->arg0 = row;
129 xp->arg1 = col;
130 xp->arg2 = c;
131 xp->arg3 = m;
132}
133
134xxinsspace(row, col)
a8577ffd
EW
135{
136 register struct xx *xp = xx_tail;
d9375810
EW
137
138 if (xp != 0 && xp->cmd == xc_insspace && xp->arg0 == row &&
139 col >= xp->arg1 && col <= xp->arg1 + xp->arg2) {
140 xp->arg2++;
a8577ffd
EW
141 return;
142 }
143 xp = xxalloc();
d9375810 144 xp->cmd = xc_insspace;
a8577ffd
EW
145 xp->arg0 = row;
146 xp->arg1 = col;
147 xp->arg2 = 1;
a8577ffd
EW
148}
149
150xxdelchar(row, col)
151{
152 register struct xx *xp = xx_tail;
153
154 if (xp != 0 && xp->cmd == xc_delchar &&
155 xp->arg0 == row && xp->arg1 == col) {
156 xp->arg2++;
157 return;
158 }
159 xp = xxalloc();
160 xp->cmd = xc_delchar;
161 xp->arg0 = row;
162 xp->arg1 = col;
163 xp->arg2 = 1;
164}
165
166xxclear()
167{
168 register struct xx *xp;
169
170 xxreset();
171 xp = xxalloc();
172 xp->cmd = xc_clear;
173}
174
175xxclreos(row, col)
176{
177 register struct xx *xp = xxalloc();
178
179 xp->cmd = xc_clreos;
180 xp->arg0 = row;
181 xp->arg1 = col;
182}
183
184xxclreol(row, col)
185{
186 register struct xx *xp = xxalloc();
187
188 xp->cmd = xc_clreol;
189 xp->arg0 = row;
190 xp->arg1 = col;
191}
192
193xxwrite(row, col, p, n, m)
194 char *p;
195{
196 register struct xx *xp;
197
198 if (xxbufp + n > xxbufe)
199 xxflush(0);
200 xp = xxalloc();
201 xp->cmd = xc_write;
202 xp->arg0 = row;
203 xp->arg1 = col;
204 xp->arg2 = n;
205 xp->arg3 = m;
206 xp->buf = xxbufp;
207 bcopy(p, xxbufp, n);
208 xxbufp += n;
209 if (tt.tt_ntoken > 0)
210 xcscan(xp->buf, n, xp->buf - xxbuf);
211}
212
213xxreset()
214{
215 register struct xx *xp, *xq;
216
217 for (xp = xx_head; xp != 0; xp = xq) {
218 xq = xp->link;
219 xxfree(xp);
220 }
221 xx_tail = xx_head = 0;
222 xxbufp = xxbuf;
223 if (tt.tt_ntoken > 0)
224 xcreset();
225}