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