2009-04-14 12:50:06 +04:00
|
|
|
/* $NetBSD: wwwrite.c,v 1.9 2009/04/14 08:50:06 lukem Exp $ */
|
1995-09-28 13:33:55 +03:00
|
|
|
|
1993-04-09 16:58:42 +04:00
|
|
|
/*
|
1995-09-28 13:33:55 +03:00
|
|
|
* Copyright (c) 1983, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-04-09 16:58:42 +04:00
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Edward Wang at The University of California, Berkeley.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 15:13:06 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-04-09 16:58:42 +04:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-11-21 11:35:41 +03:00
|
|
|
#include <sys/cdefs.h>
|
1993-04-09 16:58:42 +04:00
|
|
|
#ifndef lint
|
1995-09-28 13:33:55 +03:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)wwwrite.c 8.1 (Berkeley) 6/6/93";
|
|
|
|
#else
|
2009-04-14 12:50:06 +04:00
|
|
|
__RCSID("$NetBSD: wwwrite.c,v 1.9 2009/04/14 08:50:06 lukem Exp $");
|
1995-09-28 13:33:55 +03:00
|
|
|
#endif
|
1993-04-09 16:58:42 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include "ww.h"
|
|
|
|
#include "tt.h"
|
1997-11-21 11:35:41 +03:00
|
|
|
#include "xx.h"
|
1993-04-09 16:58:42 +04:00
|
|
|
#include "char.h"
|
|
|
|
|
|
|
|
#define UPDATE() \
|
1996-02-08 23:44:57 +03:00
|
|
|
if (!ISSET(w->ww_wflags, WWW_NOUPDATE) && w->ww_cur.r >= 0 && \
|
|
|
|
w->ww_cur.r < wwnrow && wwtouched[w->ww_cur.r]) \
|
1993-04-09 16:58:42 +04:00
|
|
|
wwupdate1(w->ww_cur.r, w->ww_cur.r + 1)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* To support control character expansion, we save the old
|
|
|
|
* p and q values in r and s, and point p at the beginning
|
|
|
|
* of the expanded string, and q at some safe place beyond it
|
|
|
|
* (p + 10). At strategic points in the loops, we check
|
|
|
|
* for (r && !*p) and restore the saved values back into
|
|
|
|
* p and q. Essentially, we implement a stack of depth 2,
|
|
|
|
* to avoid recursion, which might be a better idea.
|
|
|
|
*/
|
1997-11-21 11:35:41 +03:00
|
|
|
int
|
2009-04-14 12:50:06 +04:00
|
|
|
wwwrite(struct ww *w, const char *p, int n)
|
1993-04-09 16:58:42 +04:00
|
|
|
{
|
1996-02-08 23:44:57 +03:00
|
|
|
int hascursor;
|
2009-04-14 12:50:06 +04:00
|
|
|
const char *savep = p;
|
|
|
|
const char *q = p + n;
|
|
|
|
const char *r = 0;
|
|
|
|
const char *s = 0;
|
1993-04-09 16:58:42 +04:00
|
|
|
|
|
|
|
#ifdef lint
|
|
|
|
s = 0; /* define it before possible use */
|
|
|
|
#endif
|
1996-02-08 23:44:57 +03:00
|
|
|
hascursor = ISSET(w->ww_wflags, WWW_HASCURSOR);
|
|
|
|
if (hascursor)
|
1993-04-09 16:58:42 +04:00
|
|
|
wwcursor(w, 0);
|
1996-02-08 23:44:57 +03:00
|
|
|
while (p < q && !ISSET(w->ww_pflags, WWP_STOPPED) &&
|
|
|
|
(!wwinterrupt() || ISSET(w->ww_wflags, WWW_NOINTR))) {
|
1993-04-09 16:58:42 +04:00
|
|
|
if (r && !*p) {
|
|
|
|
p = r;
|
|
|
|
q = s;
|
|
|
|
r = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (w->ww_wstate == 0 &&
|
1997-11-21 11:35:41 +03:00
|
|
|
(isprt(*p) ||
|
|
|
|
(ISSET(w->ww_wflags, WWW_UNCTRL) && isunctrl(*p)))) {
|
|
|
|
int i;
|
|
|
|
union ww_char *bp;
|
1993-04-09 16:58:42 +04:00
|
|
|
int col, col1;
|
|
|
|
|
1996-02-08 23:44:57 +03:00
|
|
|
if (ISSET(w->ww_wflags, WWW_INSERT)) {
|
|
|
|
/* this is very slow */
|
1993-04-09 16:58:42 +04:00
|
|
|
if (*p == '\t') {
|
|
|
|
p++;
|
|
|
|
w->ww_cur.c += 8 -
|
1997-11-21 11:35:41 +03:00
|
|
|
((w->ww_cur.c - w->ww_w.l) & 7);
|
1993-04-09 16:58:42 +04:00
|
|
|
goto chklf;
|
|
|
|
}
|
|
|
|
if (!isprt(*p)) {
|
|
|
|
r = p + 1;
|
|
|
|
s = q;
|
|
|
|
p = unctrl(*p);
|
|
|
|
q = p + 10;
|
|
|
|
}
|
|
|
|
wwinschar(w, w->ww_cur.r, w->ww_cur.c,
|
|
|
|
*p++, w->ww_modes);
|
|
|
|
goto right;
|
|
|
|
}
|
|
|
|
|
|
|
|
bp = &w->ww_buf[w->ww_cur.r][w->ww_cur.c];
|
|
|
|
i = w->ww_cur.c;
|
|
|
|
while (i < w->ww_w.r && p < q)
|
|
|
|
if (!*p && r) {
|
|
|
|
p = r;
|
|
|
|
q = s;
|
|
|
|
r = 0;
|
|
|
|
} else if (*p == '\t') {
|
1997-11-21 11:35:41 +03:00
|
|
|
int tmp = 8 - ((i - w->ww_w.l) & 7);
|
1993-04-09 16:58:42 +04:00
|
|
|
p++;
|
|
|
|
i += tmp;
|
|
|
|
bp += tmp;
|
|
|
|
} else if (isprt(*p)) {
|
|
|
|
bp++->c_w = *p++
|
|
|
|
| w->ww_modes << WWC_MSHIFT;
|
|
|
|
i++;
|
1996-02-08 23:44:57 +03:00
|
|
|
} else if (ISSET(w->ww_wflags, WWW_UNCTRL) &&
|
|
|
|
isunctrl(*p)) {
|
1993-04-09 16:58:42 +04:00
|
|
|
r = p + 1;
|
|
|
|
s = q;
|
|
|
|
p = unctrl(*p);
|
|
|
|
q = p + 10;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
col = MAX(w->ww_cur.c, w->ww_i.l);
|
|
|
|
col1 = MIN(i, w->ww_i.r);
|
|
|
|
w->ww_cur.c = i;
|
|
|
|
if (w->ww_cur.r >= w->ww_i.t
|
|
|
|
&& w->ww_cur.r < w->ww_i.b) {
|
1997-11-21 11:35:41 +03:00
|
|
|
union ww_char *ns = wwns[w->ww_cur.r];
|
|
|
|
unsigned char *smap =
|
1996-02-09 00:48:51 +03:00
|
|
|
&wwsmap[w->ww_cur.r][col];
|
1997-11-21 11:35:41 +03:00
|
|
|
char *win = w->ww_win[w->ww_cur.r];
|
1993-04-09 16:58:42 +04:00
|
|
|
int nchanged = 0;
|
|
|
|
|
|
|
|
bp = w->ww_buf[w->ww_cur.r];
|
|
|
|
for (i = col; i < col1; i++)
|
|
|
|
if (*smap++ == w->ww_index) {
|
|
|
|
nchanged++;
|
|
|
|
ns[i].c_w = bp[i].c_w
|
|
|
|
^ win[i] << WWC_MSHIFT;
|
|
|
|
}
|
|
|
|
if (nchanged > 0)
|
|
|
|
wwtouched[w->ww_cur.r] |= WWU_TOUCHED;
|
|
|
|
}
|
|
|
|
chklf:
|
|
|
|
if (w->ww_cur.c >= w->ww_w.r)
|
|
|
|
goto crlf;
|
|
|
|
} else switch (w->ww_wstate) {
|
|
|
|
case 0:
|
|
|
|
switch (*p++) {
|
|
|
|
case '\n':
|
1996-02-08 23:44:57 +03:00
|
|
|
if (ISSET(w->ww_wflags, WWW_MAPNL))
|
1993-04-09 16:58:42 +04:00
|
|
|
crlf:
|
|
|
|
w->ww_cur.c = w->ww_w.l;
|
|
|
|
lf:
|
|
|
|
UPDATE();
|
|
|
|
if (++w->ww_cur.r >= w->ww_w.b) {
|
|
|
|
w->ww_cur.r = w->ww_w.b - 1;
|
|
|
|
if (w->ww_w.b < w->ww_b.b) {
|
|
|
|
(void) wwscroll1(w, w->ww_i.t,
|
|
|
|
w->ww_i.b, 1, 0);
|
|
|
|
w->ww_buf++;
|
|
|
|
w->ww_b.t--;
|
|
|
|
w->ww_b.b--;
|
|
|
|
} else
|
|
|
|
wwdelline(w, w->ww_b.t);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '\b':
|
|
|
|
if (--w->ww_cur.c < w->ww_w.l) {
|
|
|
|
w->ww_cur.c = w->ww_w.r - 1;
|
|
|
|
goto up;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
w->ww_cur.c = w->ww_w.l;
|
|
|
|
break;
|
|
|
|
case ctrl('g'):
|
|
|
|
ttputc(ctrl('g'));
|
|
|
|
break;
|
|
|
|
case ctrl('['):
|
|
|
|
w->ww_wstate = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
w->ww_wstate = 0;
|
|
|
|
switch (*p++) {
|
|
|
|
case '@':
|
1996-02-08 23:44:57 +03:00
|
|
|
SET(w->ww_wflags, WWW_INSERT);
|
1993-04-09 16:58:42 +04:00
|
|
|
break;
|
|
|
|
case 'A':
|
|
|
|
up:
|
|
|
|
UPDATE();
|
|
|
|
if (--w->ww_cur.r < w->ww_w.t) {
|
|
|
|
w->ww_cur.r = w->ww_w.t;
|
|
|
|
if (w->ww_w.t > w->ww_b.t) {
|
|
|
|
(void) wwscroll1(w, w->ww_i.t,
|
|
|
|
w->ww_i.b, -1, 0);
|
|
|
|
w->ww_buf--;
|
|
|
|
w->ww_b.t++;
|
|
|
|
w->ww_b.b++;
|
|
|
|
} else
|
|
|
|
wwinsline(w, w->ww_b.t);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'B':
|
|
|
|
goto lf;
|
|
|
|
case 'C':
|
|
|
|
right:
|
|
|
|
w->ww_cur.c++;
|
|
|
|
goto chklf;
|
|
|
|
case 'E':
|
|
|
|
w->ww_buf -= w->ww_w.t - w->ww_b.t;
|
|
|
|
w->ww_b.t = w->ww_w.t;
|
|
|
|
w->ww_b.b = w->ww_b.t + w->ww_b.nr;
|
|
|
|
w->ww_cur.r = w->ww_w.t;
|
|
|
|
w->ww_cur.c = w->ww_w.l;
|
|
|
|
wwclreos(w, w->ww_w.t, w->ww_w.l);
|
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
UPDATE();
|
|
|
|
w->ww_cur.r = w->ww_w.t;
|
|
|
|
w->ww_cur.c = w->ww_w.l;
|
|
|
|
break;
|
|
|
|
case 'J':
|
|
|
|
wwclreos(w, w->ww_cur.r, w->ww_cur.c);
|
|
|
|
break;
|
|
|
|
case 'K':
|
|
|
|
wwclreol(w, w->ww_cur.r, w->ww_cur.c);
|
|
|
|
break;
|
|
|
|
case 'L':
|
|
|
|
UPDATE();
|
|
|
|
wwinsline(w, w->ww_cur.r);
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
wwdelline(w, w->ww_cur.r);
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
wwdelchar(w, w->ww_cur.r, w->ww_cur.c);
|
|
|
|
break;
|
|
|
|
case 'O':
|
1996-02-08 23:44:57 +03:00
|
|
|
CLR(w->ww_wflags, WWW_INSERT);
|
1993-04-09 16:58:42 +04:00
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
wwinschar(w, w->ww_cur.r, w->ww_cur.c, ' ', 0);
|
|
|
|
break;
|
|
|
|
case 'X':
|
|
|
|
wwupdate();
|
|
|
|
break;
|
|
|
|
case 'Y':
|
|
|
|
UPDATE();
|
|
|
|
w->ww_wstate = 2;
|
|
|
|
break;
|
|
|
|
case 'Z':
|
|
|
|
wwupdate();
|
|
|
|
xxflush(0);
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
w->ww_wstate = 4;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
w->ww_wstate = 5;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
w->ww_cur.r = w->ww_w.t +
|
|
|
|
(unsigned)(*p++ - ' ') % w->ww_w.nr;
|
|
|
|
w->ww_wstate = 3;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
w->ww_cur.c = w->ww_w.l +
|
|
|
|
(unsigned)(*p++ - ' ') % w->ww_w.nc;
|
|
|
|
w->ww_wstate = 0;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
w->ww_modes |= *p++ & wwavailmodes;
|
|
|
|
w->ww_wstate = 0;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
w->ww_modes &= ~*p++;
|
|
|
|
w->ww_wstate = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (hascursor)
|
|
|
|
wwcursor(w, 1);
|
|
|
|
wwnwwr++;
|
|
|
|
wwnwwra += n;
|
|
|
|
n = p - savep;
|
|
|
|
wwnwwrc += n;
|
|
|
|
return n;
|
|
|
|
}
|