Sync usage with man page.
[netbsd-mini2440.git] / usr.bin / window / wwwrite.c
blob40d01115251a97224db8db55e2236f16d167a3cb
1 /* $NetBSD: wwwrite.c,v 1.8 2003/08/07 11:17:46 agc Exp $ */
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Edward Wang at The University of California, Berkeley.
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)wwwrite.c 8.1 (Berkeley) 6/6/93";
39 #else
40 __RCSID("$NetBSD: wwwrite.c,v 1.8 2003/08/07 11:17:46 agc Exp $");
41 #endif
42 #endif /* not lint */
44 #include "ww.h"
45 #include "tt.h"
46 #include "xx.h"
47 #include "char.h"
49 #define UPDATE() \
50 if (!ISSET(w->ww_wflags, WWW_NOUPDATE) && w->ww_cur.r >= 0 && \
51 w->ww_cur.r < wwnrow && wwtouched[w->ww_cur.r]) \
52 wwupdate1(w->ww_cur.r, w->ww_cur.r + 1)
55 * To support control character expansion, we save the old
56 * p and q values in r and s, and point p at the beginning
57 * of the expanded string, and q at some safe place beyond it
58 * (p + 10). At strategic points in the loops, we check
59 * for (r && !*p) and restore the saved values back into
60 * p and q. Essentially, we implement a stack of depth 2,
61 * to avoid recursion, which might be a better idea.
63 int
64 wwwrite(struct ww *w, const char *p, int n)
66 int hascursor;
67 const char *savep = p;
68 const char *q = p + n;
69 const char *r = 0;
70 const char *s = 0;
72 #ifdef lint
73 s = 0; /* define it before possible use */
74 #endif
75 hascursor = ISSET(w->ww_wflags, WWW_HASCURSOR);
76 if (hascursor)
77 wwcursor(w, 0);
78 while (p < q && !ISSET(w->ww_pflags, WWP_STOPPED) &&
79 (!wwinterrupt() || ISSET(w->ww_wflags, WWW_NOINTR))) {
80 if (r && !*p) {
81 p = r;
82 q = s;
83 r = 0;
84 continue;
86 if (w->ww_wstate == 0 &&
87 (isprt(*p) ||
88 (ISSET(w->ww_wflags, WWW_UNCTRL) && isunctrl(*p)))) {
89 int i;
90 union ww_char *bp;
91 int col, col1;
93 if (ISSET(w->ww_wflags, WWW_INSERT)) {
94 /* this is very slow */
95 if (*p == '\t') {
96 p++;
97 w->ww_cur.c += 8 -
98 ((w->ww_cur.c - w->ww_w.l) & 7);
99 goto chklf;
101 if (!isprt(*p)) {
102 r = p + 1;
103 s = q;
104 p = unctrl(*p);
105 q = p + 10;
107 wwinschar(w, w->ww_cur.r, w->ww_cur.c,
108 *p++, w->ww_modes);
109 goto right;
112 bp = &w->ww_buf[w->ww_cur.r][w->ww_cur.c];
113 i = w->ww_cur.c;
114 while (i < w->ww_w.r && p < q)
115 if (!*p && r) {
116 p = r;
117 q = s;
118 r = 0;
119 } else if (*p == '\t') {
120 int tmp = 8 - ((i - w->ww_w.l) & 7);
121 p++;
122 i += tmp;
123 bp += tmp;
124 } else if (isprt(*p)) {
125 bp++->c_w = *p++
126 | w->ww_modes << WWC_MSHIFT;
127 i++;
128 } else if (ISSET(w->ww_wflags, WWW_UNCTRL) &&
129 isunctrl(*p)) {
130 r = p + 1;
131 s = q;
132 p = unctrl(*p);
133 q = p + 10;
134 } else
135 break;
136 col = MAX(w->ww_cur.c, w->ww_i.l);
137 col1 = MIN(i, w->ww_i.r);
138 w->ww_cur.c = i;
139 if (w->ww_cur.r >= w->ww_i.t
140 && w->ww_cur.r < w->ww_i.b) {
141 union ww_char *ns = wwns[w->ww_cur.r];
142 unsigned char *smap =
143 &wwsmap[w->ww_cur.r][col];
144 char *win = w->ww_win[w->ww_cur.r];
145 int nchanged = 0;
147 bp = w->ww_buf[w->ww_cur.r];
148 for (i = col; i < col1; i++)
149 if (*smap++ == w->ww_index) {
150 nchanged++;
151 ns[i].c_w = bp[i].c_w
152 ^ win[i] << WWC_MSHIFT;
154 if (nchanged > 0)
155 wwtouched[w->ww_cur.r] |= WWU_TOUCHED;
157 chklf:
158 if (w->ww_cur.c >= w->ww_w.r)
159 goto crlf;
160 } else switch (w->ww_wstate) {
161 case 0:
162 switch (*p++) {
163 case '\n':
164 if (ISSET(w->ww_wflags, WWW_MAPNL))
165 crlf:
166 w->ww_cur.c = w->ww_w.l;
168 UPDATE();
169 if (++w->ww_cur.r >= w->ww_w.b) {
170 w->ww_cur.r = w->ww_w.b - 1;
171 if (w->ww_w.b < w->ww_b.b) {
172 (void) wwscroll1(w, w->ww_i.t,
173 w->ww_i.b, 1, 0);
174 w->ww_buf++;
175 w->ww_b.t--;
176 w->ww_b.b--;
177 } else
178 wwdelline(w, w->ww_b.t);
180 break;
181 case '\b':
182 if (--w->ww_cur.c < w->ww_w.l) {
183 w->ww_cur.c = w->ww_w.r - 1;
184 goto up;
186 break;
187 case '\r':
188 w->ww_cur.c = w->ww_w.l;
189 break;
190 case ctrl('g'):
191 ttputc(ctrl('g'));
192 break;
193 case ctrl('['):
194 w->ww_wstate = 1;
195 break;
197 break;
198 case 1:
199 w->ww_wstate = 0;
200 switch (*p++) {
201 case '@':
202 SET(w->ww_wflags, WWW_INSERT);
203 break;
204 case 'A':
206 UPDATE();
207 if (--w->ww_cur.r < w->ww_w.t) {
208 w->ww_cur.r = w->ww_w.t;
209 if (w->ww_w.t > w->ww_b.t) {
210 (void) wwscroll1(w, w->ww_i.t,
211 w->ww_i.b, -1, 0);
212 w->ww_buf--;
213 w->ww_b.t++;
214 w->ww_b.b++;
215 } else
216 wwinsline(w, w->ww_b.t);
218 break;
219 case 'B':
220 goto lf;
221 case 'C':
222 right:
223 w->ww_cur.c++;
224 goto chklf;
225 case 'E':
226 w->ww_buf -= w->ww_w.t - w->ww_b.t;
227 w->ww_b.t = w->ww_w.t;
228 w->ww_b.b = w->ww_b.t + w->ww_b.nr;
229 w->ww_cur.r = w->ww_w.t;
230 w->ww_cur.c = w->ww_w.l;
231 wwclreos(w, w->ww_w.t, w->ww_w.l);
232 break;
233 case 'H':
234 UPDATE();
235 w->ww_cur.r = w->ww_w.t;
236 w->ww_cur.c = w->ww_w.l;
237 break;
238 case 'J':
239 wwclreos(w, w->ww_cur.r, w->ww_cur.c);
240 break;
241 case 'K':
242 wwclreol(w, w->ww_cur.r, w->ww_cur.c);
243 break;
244 case 'L':
245 UPDATE();
246 wwinsline(w, w->ww_cur.r);
247 break;
248 case 'M':
249 wwdelline(w, w->ww_cur.r);
250 break;
251 case 'N':
252 wwdelchar(w, w->ww_cur.r, w->ww_cur.c);
253 break;
254 case 'O':
255 CLR(w->ww_wflags, WWW_INSERT);
256 break;
257 case 'P':
258 wwinschar(w, w->ww_cur.r, w->ww_cur.c, ' ', 0);
259 break;
260 case 'X':
261 wwupdate();
262 break;
263 case 'Y':
264 UPDATE();
265 w->ww_wstate = 2;
266 break;
267 case 'Z':
268 wwupdate();
269 xxflush(0);
270 break;
271 case 's':
272 w->ww_wstate = 4;
273 break;
274 case 'r':
275 w->ww_wstate = 5;
276 break;
278 break;
279 case 2:
280 w->ww_cur.r = w->ww_w.t +
281 (unsigned)(*p++ - ' ') % w->ww_w.nr;
282 w->ww_wstate = 3;
283 break;
284 case 3:
285 w->ww_cur.c = w->ww_w.l +
286 (unsigned)(*p++ - ' ') % w->ww_w.nc;
287 w->ww_wstate = 0;
288 break;
289 case 4:
290 w->ww_modes |= *p++ & wwavailmodes;
291 w->ww_wstate = 0;
292 break;
293 case 5:
294 w->ww_modes &= ~*p++;
295 w->ww_wstate = 0;
296 break;
299 if (hascursor)
300 wwcursor(w, 1);
301 wwnwwr++;
302 wwnwwra += n;
303 n = p - savep;
304 wwnwwrc += n;
305 return n;