]> git.saurik.com Git - apple/shell_cmds.git/blame - window/wwwrite.c
shell_cmds-17.1.tar.gz
[apple/shell_cmds.git] / window / wwwrite.c
CommitLineData
44bd5ea7
A
1/* $NetBSD: wwwrite.c,v 1.6 1997/11/21 08:38:00 lukem Exp $ */
2
3/*
4 * Copyright (c) 1983, 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[] = "@(#)wwwrite.c 8.1 (Berkeley) 6/6/93";
43#else
44__RCSID("$NetBSD: wwwrite.c,v 1.6 1997/11/21 08:38:00 lukem Exp $");
45#endif
46#endif /* not lint */
47
48#include "ww.h"
49#include "tt.h"
50#include "xx.h"
51#include "char.h"
52
53#define UPDATE() \
54 if (!ISSET(w->ww_wflags, WWW_NOUPDATE) && w->ww_cur.r >= 0 && \
55 w->ww_cur.r < wwnrow && wwtouched[w->ww_cur.r]) \
56 wwupdate1(w->ww_cur.r, w->ww_cur.r + 1)
57
58/*
59 * To support control character expansion, we save the old
60 * p and q values in r and s, and point p at the beginning
61 * of the expanded string, and q at some safe place beyond it
62 * (p + 10). At strategic points in the loops, we check
63 * for (r && !*p) and restore the saved values back into
64 * p and q. Essentially, we implement a stack of depth 2,
65 * to avoid recursion, which might be a better idea.
66 */
67int
68wwwrite(w, p, n)
69 struct ww *w;
70 char *p;
71 int n;
72{
73 int hascursor;
74 char *savep = p;
75 char *q = p + n;
76 char *r = 0;
77 char *s = 0;
78
79#ifdef lint
80 s = 0; /* define it before possible use */
81#endif
82 hascursor = ISSET(w->ww_wflags, WWW_HASCURSOR);
83 if (hascursor)
84 wwcursor(w, 0);
85 while (p < q && !ISSET(w->ww_pflags, WWP_STOPPED) &&
86 (!wwinterrupt() || ISSET(w->ww_wflags, WWW_NOINTR))) {
87 if (r && !*p) {
88 p = r;
89 q = s;
90 r = 0;
91 continue;
92 }
93 if (w->ww_wstate == 0 &&
94 (isprt(*p) ||
95 (ISSET(w->ww_wflags, WWW_UNCTRL) && isunctrl(*p)))) {
96 int i;
97 union ww_char *bp;
98 int col, col1;
99
100 if (ISSET(w->ww_wflags, WWW_INSERT)) {
101 /* this is very slow */
102 if (*p == '\t') {
103 p++;
104 w->ww_cur.c += 8 -
105 ((w->ww_cur.c - w->ww_w.l) & 7);
106 goto chklf;
107 }
108 if (!isprt(*p)) {
109 r = p + 1;
110 s = q;
111 p = unctrl(*p);
112 q = p + 10;
113 }
114 wwinschar(w, w->ww_cur.r, w->ww_cur.c,
115 *p++, w->ww_modes);
116 goto right;
117 }
118
119 bp = &w->ww_buf[w->ww_cur.r][w->ww_cur.c];
120 i = w->ww_cur.c;
121 while (i < w->ww_w.r && p < q)
122 if (!*p && r) {
123 p = r;
124 q = s;
125 r = 0;
126 } else if (*p == '\t') {
127 int tmp = 8 - ((i - w->ww_w.l) & 7);
128 p++;
129 i += tmp;
130 bp += tmp;
131 } else if (isprt(*p)) {
132 bp++->c_w = *p++
133 | w->ww_modes << WWC_MSHIFT;
134 i++;
135 } else if (ISSET(w->ww_wflags, WWW_UNCTRL) &&
136 isunctrl(*p)) {
137 r = p + 1;
138 s = q;
139 p = unctrl(*p);
140 q = p + 10;
141 } else
142 break;
143 col = MAX(w->ww_cur.c, w->ww_i.l);
144 col1 = MIN(i, w->ww_i.r);
145 w->ww_cur.c = i;
146 if (w->ww_cur.r >= w->ww_i.t
147 && w->ww_cur.r < w->ww_i.b) {
148 union ww_char *ns = wwns[w->ww_cur.r];
149 unsigned char *smap =
150 &wwsmap[w->ww_cur.r][col];
151 char *win = w->ww_win[w->ww_cur.r];
152 int nchanged = 0;
153
154 bp = w->ww_buf[w->ww_cur.r];
155 for (i = col; i < col1; i++)
156 if (*smap++ == w->ww_index) {
157 nchanged++;
158 ns[i].c_w = bp[i].c_w
159 ^ win[i] << WWC_MSHIFT;
160 }
161 if (nchanged > 0)
162 wwtouched[w->ww_cur.r] |= WWU_TOUCHED;
163 }
164 chklf:
165 if (w->ww_cur.c >= w->ww_w.r)
166 goto crlf;
167 } else switch (w->ww_wstate) {
168 case 0:
169 switch (*p++) {
170 case '\n':
171 if (ISSET(w->ww_wflags, WWW_MAPNL))
172 crlf:
173 w->ww_cur.c = w->ww_w.l;
174 lf:
175 UPDATE();
176 if (++w->ww_cur.r >= w->ww_w.b) {
177 w->ww_cur.r = w->ww_w.b - 1;
178 if (w->ww_w.b < w->ww_b.b) {
179 (void) wwscroll1(w, w->ww_i.t,
180 w->ww_i.b, 1, 0);
181 w->ww_buf++;
182 w->ww_b.t--;
183 w->ww_b.b--;
184 } else
185 wwdelline(w, w->ww_b.t);
186 }
187 break;
188 case '\b':
189 if (--w->ww_cur.c < w->ww_w.l) {
190 w->ww_cur.c = w->ww_w.r - 1;
191 goto up;
192 }
193 break;
194 case '\r':
195 w->ww_cur.c = w->ww_w.l;
196 break;
197 case ctrl('g'):
198 ttputc(ctrl('g'));
199 break;
200 case ctrl('['):
201 w->ww_wstate = 1;
202 break;
203 }
204 break;
205 case 1:
206 w->ww_wstate = 0;
207 switch (*p++) {
208 case '@':
209 SET(w->ww_wflags, WWW_INSERT);
210 break;
211 case 'A':
212 up:
213 UPDATE();
214 if (--w->ww_cur.r < w->ww_w.t) {
215 w->ww_cur.r = w->ww_w.t;
216 if (w->ww_w.t > w->ww_b.t) {
217 (void) wwscroll1(w, w->ww_i.t,
218 w->ww_i.b, -1, 0);
219 w->ww_buf--;
220 w->ww_b.t++;
221 w->ww_b.b++;
222 } else
223 wwinsline(w, w->ww_b.t);
224 }
225 break;
226 case 'B':
227 goto lf;
228 case 'C':
229 right:
230 w->ww_cur.c++;
231 goto chklf;
232 case 'E':
233 w->ww_buf -= w->ww_w.t - w->ww_b.t;
234 w->ww_b.t = w->ww_w.t;
235 w->ww_b.b = w->ww_b.t + w->ww_b.nr;
236 w->ww_cur.r = w->ww_w.t;
237 w->ww_cur.c = w->ww_w.l;
238 wwclreos(w, w->ww_w.t, w->ww_w.l);
239 break;
240 case 'H':
241 UPDATE();
242 w->ww_cur.r = w->ww_w.t;
243 w->ww_cur.c = w->ww_w.l;
244 break;
245 case 'J':
246 wwclreos(w, w->ww_cur.r, w->ww_cur.c);
247 break;
248 case 'K':
249 wwclreol(w, w->ww_cur.r, w->ww_cur.c);
250 break;
251 case 'L':
252 UPDATE();
253 wwinsline(w, w->ww_cur.r);
254 break;
255 case 'M':
256 wwdelline(w, w->ww_cur.r);
257 break;
258 case 'N':
259 wwdelchar(w, w->ww_cur.r, w->ww_cur.c);
260 break;
261 case 'O':
262 CLR(w->ww_wflags, WWW_INSERT);
263 break;
264 case 'P':
265 wwinschar(w, w->ww_cur.r, w->ww_cur.c, ' ', 0);
266 break;
267 case 'X':
268 wwupdate();
269 break;
270 case 'Y':
271 UPDATE();
272 w->ww_wstate = 2;
273 break;
274 case 'Z':
275 wwupdate();
276 xxflush(0);
277 break;
278 case 's':
279 w->ww_wstate = 4;
280 break;
281 case 'r':
282 w->ww_wstate = 5;
283 break;
284 }
285 break;
286 case 2:
287 w->ww_cur.r = w->ww_w.t +
288 (unsigned)(*p++ - ' ') % w->ww_w.nr;
289 w->ww_wstate = 3;
290 break;
291 case 3:
292 w->ww_cur.c = w->ww_w.l +
293 (unsigned)(*p++ - ' ') % w->ww_w.nc;
294 w->ww_wstate = 0;
295 break;
296 case 4:
297 w->ww_modes |= *p++ & wwavailmodes;
298 w->ww_wstate = 0;
299 break;
300 case 5:
301 w->ww_modes &= ~*p++;
302 w->ww_wstate = 0;
303 break;
304 }
305 }
306 if (hascursor)
307 wwcursor(w, 1);
308 wwnwwr++;
309 wwnwwra += n;
310 n = p - savep;
311 wwnwwrc += n;
312 return n;
313}