#if M_RCSID
#ifndef lint
static char rcsID[] =
"$Header: /team/ps/sun_xcurses/archive/local_changes/xcurses/src/lib/"
"libxcurses/src/libc/xcurses/rcs/m_cc.c 1.40 1998/06/12 12:45:39 "
"cbates Exp $";
#endif
#endif
#include <private.h>
#include <limits.h>
#include <m_wio.h>
#include <string.h>
typedef struct {
int max;
int used;
char *mbs;
} t_string;
static int
write_string(int byte, t_string *sp)
{
if (sp->max <= sp->used)
return (EOF);
sp->mbs[sp->used++] = (char)byte;
return (byte);
}
int
wistombs(char *mbs, const wint_t *wis, int n)
{
int last;
t_string string = { 0 };
t_wide_io convert = { 0 };
string.max = n;
string.mbs = mbs;
convert.object = (void *) &string;
convert.put = (int (*)(int, void *)) write_string;
for (; ; ++wis) {
last = string.used;
if (m_wio_put(*wis, &convert) < 0) {
string.used = last;
break;
}
if (*wis == '\0' || *wis == WEOF)
break;
}
string.mbs[string.used] = '\0';
return (string.used);
}
int
wistowcs(wchar_t *wcs, const wint_t *wis, int n)
{
wchar_t *start;
if (n < 0)
n = INT_MAX;
for (start = wcs; *wis != '\0' && 0 < n; ++wis, ++wcs, --n) {
if (*wis == WEOF)
break;
*wcs = (wchar_t)*wis;
}
*wcs = '\0';
return ((int)(wcs - start));
}
void
__m_touch_locs(WINDOW *w, int row, int firstCol, int lastCol)
{
if (w) {
if (firstCol < w->_first[row])
w->_first[row] = (short)firstCol;
if (lastCol > w->_last[row])
w->_last[row] = (short)lastCol;
}
}
int
__m_chtype_cc(chtype ch, cchar_t *cc)
{
char mb;
cc->_f = 1;
cc->_n = 1;
mb = (char)(ch & A_CHARTEXT);
cc->_co = (short)PAIR_NUMBER((int)ch);
cc->_at = (attr_t)((ch & (A_ATTRIBUTES & ~A_COLOR)) >> 16);
if (mb == 0)
cc->_wc[0] = cc->_wc[1] = 0;
else if (mbtowc(cc->_wc, &mb, 1) < 0) {
return (ERR);
}
return (OK);
}
chtype
__m_cc_chtype(const cchar_t *cc)
{
chtype ch;
unsigned char mb[MB_LEN_MAX];
if (cc->_n != 1 || wctomb((char *)mb, cc->_wc[0]) != 1)
return ((chtype) ERR);
ch = ((chtype) cc->_at << 16) & ~A_COLOR;
ch |= COLOR_PAIR(cc->_co) | mb[0];
return (ch);
}
int
__m_cc_mbs(const cchar_t *cc, char *mbs, int n)
{
int i, bytes, count, last;
static t_string string = { 0 };
static t_wide_io convert = { 0 };
if (n < 0) {
return (string.used);
} else if (0 < n) {
string.max = n;
string.used = 0;
string.mbs = mbs;
convert._next = convert._size = 0;
convert.object = (void *) &string;
convert.put = (int (*)(int, void *)) write_string;
}
last = string.used;
if (cc == NULL) {
if ((count = m_wio_put('\0', &convert)) < 0) {
string.used = last;
return (-1);
}
if (string.used < string.max)
string.mbs[string.used++] = '\0';
} else {
for (count = i = 0; i < cc->_n; ++i, count += bytes)
if ((bytes = m_wio_put(cc->_wc[i], &convert)) < 0) {
string.used = last;
return (-1);
}
}
return (count);
}
int
__m_tty_wc(int index, wchar_t *wcp)
{
char mb;
int code;
mb = (char)PTERMIOS(_shell)->c_cc[index];
if (mb)
code = mbtowc(wcp, &mb, 1) < 0 ? ERR : OK;
else
code = ERR;
return (code);
}
int
__m_mbs_cc(const char *mbs, attr_t at, short co, cchar_t *cc)
{
wchar_t wc;
const char *start;
int i, nbytes, width, have_one;
for (start = mbs, have_one = i = 0; *mbs != '\0'; mbs += nbytes, ++i) {
if (sizeof (cc->_wc) <= i)
return (-1);
if ((nbytes = mbtowc(&wc, mbs, UINT_MAX)) < 0)
return (-1);
if (nbytes == 0)
break;
if (iscntrl(*mbs))
width = 1;
else
width = wcwidth(wc);
if (0 < width) {
if (have_one)
break;
have_one = 1;
}
cc->_wc[i] = wc;
}
cc->_f = 1;
cc->_n = (short)i;
cc->_co = co;
cc->_at = at;
(void) __m_cc_sort(cc);
return ((int)(mbs - start));
}
int
__m_wcs_cc(const wchar_t *wcs, attr_t at, short co, cchar_t *cc)
{
short i;
const wchar_t *start;
for (start = wcs, i = 0; *wcs != '\0'; ++wcs, ++i) {
if (sizeof (cc->_wc) <= i) {
return (-1);
}
if (wcwidth(*wcs) > 0) {
if (i != 0)
break;
} else if ((*wcs == L'\n') || (*wcs == L'\t') ||
(*wcs == L'\b') || (*wcs == L'\r')) {
if (i != 0)
break;
cc->_wc[i++] = *wcs++;
break;
}
cc->_wc[i] = *wcs;
}
cc->_f = 1;
cc->_n = i;
cc->_co = co;
cc->_at = at;
return ((int)(wcs - start));
}
int
__m_wc_cc(wint_t wc, cchar_t *cc)
{
wchar_t wcs[2];
if (wc == WEOF)
return (-1);
if (wc == 0) {
cc->_f = 1;
cc->_n = 1;
cc->_co = 0;
cc->_at = WA_NORMAL;
cc->_wc[0] = 0;
cc->_wc[1] = 0;
} else {
wcs[0] = (wchar_t)wc;
wcs[1] = '\0';
(void) __m_wcs_cc(wcs, WA_NORMAL, 0, cc);
}
return (0);
}
int
__m_cc_sort(cchar_t *cc)
{
wchar_t wc;
int width, i, j, spacing;
for (width = spacing = i = 0; i < cc->_n; ++i) {
j = wcwidth(cc->_wc[i]);
if (0 < j) {
if (0 < width)
return (-1);
wc = cc->_wc[0];
cc->_wc[0] = cc->_wc[i];
cc->_wc[i] = wc;
spacing = 1;
width = j;
break;
}
}
for (i = spacing; i < cc->_n; ++i) {
for (j = cc->_n - 1; i < j; --j) {
if (cc->_wc[j-1] > cc->_wc[j]) {
wc = cc->_wc[j];
cc->_wc[j] = cc->_wc[j-1];
cc->_wc[j-1] = wc;
}
}
}
return (width);
}
int
__m_cc_first(WINDOW *w, int y, int x)
{
cchar_t *lp;
for (lp = w->_line[y]; 0 < x; --x) {
if (lp[x]._f)
break;
}
return (x);
}
int
__m_cc_next(WINDOW *w, int y, int x)
{
cchar_t *lp;
for (lp = w->_line[y]; ++x < w->_maxx; ) {
if (lp[x]._f)
break;
}
return (x);
}
int
__m_cc_islast(WINDOW *w, int y, int x)
{
int first, width;
first = __m_cc_first(w, y, x);
width = __m_cc_width(&w->_line[y][x]);
return ((first + width) == (x + 1));
}
int
__m_cc_replace(WINDOW *w, int y, int x,
const cchar_t *cc, int as_is)
{
int i, width;
cchar_t *cp, *np;
width = __m_cc_width(cc);
if (width <= 0)
return (__m_cc_modify(w, y, x, cc));
if (0 < width && w->_maxx < x + width) {
(void) __m_cc_erase(w, y, x, y, w->_maxx-1);
return (-1);
}
(void) __m_cc_erase(w, y, x, y, x + width - 1);
cp = &w->_line[y][x++];
if (cc->_wc[0] == L' ') {
*cp = w->_bg;
cp->_at = cc->_at | w->_fg._at;
cp->_co = (cc->_co) ? cc->_co : w->_fg._co;
} else {
if (__m_wacs_cc(cc, cp)) {
cp->_at = (cc->_at | w->_fg._at) & ~WA_ALTCHARSET;
} else {
cp->_at = cc->_at | w->_fg._at;
}
cp->_co = (cc->_co) ? cc->_co : w->_fg._co;
}
cp->_f = 1;
for (np = cp + 1, i = 1; i < width; ++i, ++x, ++np) {
*np = *cp;
np->_f = 0;
}
return (width);
}
int
__m_do_scroll(WINDOW *w, int y, int x, int *yp, int *xp)
{
int code = OK;
if (w->_maxx <= x)
x = w->_maxx - 1;
++y;
if (y == w->_bottom) {
--y;
if (w->_flags & W_CAN_SCROLL) {
if (wscrl(w, 1) == ERR)
return (ERR);
x = 0;
w->_flags |= W_FLUSH;
} else {
#ifdef BREAKS
w->_curx = x;
w->_cury = y;
#endif
code = ERR;
}
} else if (w->_maxy <= y) {
y = w->_maxy - 1;
} else {
x = 0;
}
*yp = y;
*xp = x;
return (code);
}
int
__m_cc_add(WINDOW *w, int y, int x,
const cchar_t *cc, int as_is, int *yp, int *xp)
{
int nx, width, code = ERR;
switch (cc->_wc[0]) {
case L'\t':
nx = x + (8 - (x & 07));
if (nx >= w->_maxx) {
nx = w->_maxx;
}
if (__m_cc_erase(w, y, x, y, nx-1) == -1)
goto error;
x = nx;
if (w->_maxx <= x) {
if (__m_do_scroll(w, y, x, &y, &x) == ERR)
goto error;
}
break;
case L'\n':
if (__m_cc_erase(w, y, x, y, w->_maxx-1) == -1)
goto error;
if (__m_do_scroll(w, y, x, &y, &x) == ERR)
goto error;
break;
case L'\r':
x = 0;
break;
case L'\b':
if (0 < x)
--x;
else
(void) beep();
break;
default:
width = __m_cc_replace(w, y, x, cc, as_is);
x += width;
if (width < 0 || w->_maxx <= x) {
if (__m_do_scroll(w, y, x, &y, &x) == ERR) {
goto error;
}
if (width < 0)
x += __m_cc_replace(w, y, x, cc, as_is);
}
}
code = OK;
error:
*yp = y;
*xp = x;
return (code);
}
int
__m_cc_add_k(WINDOW *w, int y, int x,
const cchar_t *cc, int as_is, int *yp, int *xp)
{
int width, code = ERR;
switch (cc->_wc[0]) {
case L'\n':
if (__m_cc_erase(w, y, x, y, w->_maxx-1) == -1)
goto error;
if (__m_do_scroll(w, y, x, &y, &x) == ERR)
goto error;
break;
default:
width = __m_cc_replace(w, y, x, cc, as_is);
x += width;
}
code = OK;
error:
*yp = y;
*xp = x;
return (code);
}
int
__m_cc_modify(WINDOW *w, int y, int x, const cchar_t *cc)
{
cchar_t *cp, tch;
int i, j, width;
x = __m_cc_first(w, y, x);
cp = &w->_line[y][x];
if (_M_CCHAR_MAX < cp->_n + cc->_n)
return (-1);
for (i = cp->_n, j = 0; j < cc->_n; ++i, ++j)
cp->_wc[i] = cc->_wc[j];
cp->_n = (short)i;
width = __m_cc_width(cp);
__m_touch_locs(w, y, x, x + width);
(void) __m_cc_sort(cp);
while (0 < --width) {
tch = *cp;
cp[1] = tch;
cp++;
}
return (0);
}
static void
__m_cc_erase_in_line(WINDOW *w, int y, int x, int lx, int bgWidth)
{
cchar_t *cp;
int i;
if (x < w->_first[y])
w->_first[y] = (short)x;
for (cp = w->_line[y], i = 0; x <= lx; ++x, ++i) {
cp[x] = w->_bg;
cp[x]._f = (short)(i % bgWidth == 0);
}
if (w->_last[y] < x)
w->_last[y] = (short)x;
}
static void
__m_cc_erase_in_line_sub(WINDOW *w, int y, int x,
int lx, int bgWidth, int parentBGWidth)
{
cchar_t *cp;
int i;
int xi;
int wmin, wmax;
int wlx;
WINDOW *parent = w->_parent;
int parentY = w->_begy + y - parent->_begy;
int dx = w->_begx - parent->_begx;
xi = x = __m_cc_first(parent, parentY, dx + x);
wlx = lx = __m_cc_next(parent, parentY, dx + lx) - 1;
if (wlx >= dx + w->_maxx) wlx = dx + w->_maxx - 1;
for (cp = parent->_line[parentY]; x <= lx; ) {
if ((x < dx) || (x >= (dx + w->_maxx))) {
for (i = 0; x <= lx && i <= parentBGWidth; x++, i++) {
cp[x] = parent->_bg;
cp[x]._f = (i == 0);
}
} else {
for (i = 0; x <= wlx; x++, i++) {
cp[x] = w->_bg;
cp[x]._f = (short)(i % bgWidth == 0);
}
}
}
wmax = x - dx;
wmin = xi - dx;
if ((xi < dx) || (x >= dx + w->_maxx)) {
int pmin, pmax;
pmax = dx;
pmin = dx + w->_maxx;
if (xi < dx) {
wmin = 0;
pmin = xi;
}
if (x >= dx + w->_maxx) {
wmax = w->_maxx;
pmax = x;
}
if (pmin < parent->_first[parentY])
parent->_first[parentY] = (short)pmin;
if (pmax > parent->_last[parentY])
parent->_last[parentY] = (short)pmax;
}
if (wmin < w->_first[y])
w->_first[y] = (short)wmin;
if (wmax > w->_last[y])
w->_last[y] = (short)wmax;
}
int
__m_cc_erase(WINDOW *w, int y, int x, int ly, int lx)
{
int bgWidth;
if (ly < y)
return (-1);
if (w->_maxy <= ly)
ly = w->_maxy - 1;
bgWidth = __m_cc_width(&w->_bg);
if (bgWidth <= 0)
return (-1);
if (w->_parent) {
int parentBGWidth = __m_cc_width(&w->_parent->_bg);
for (; y < ly; ++y, x = 0) {
__m_cc_erase_in_line_sub(w, y, x, w->_maxx-1,
bgWidth, parentBGWidth);
}
__m_cc_erase_in_line_sub(w, y, x, lx, bgWidth, parentBGWidth);
} else {
if (w->_maxx <= lx)
lx = w->_maxx - 1;
x = __m_cc_first(w, y, x);
lx = __m_cc_next(w, ly, lx) - 1;
for (; y < ly; ++y, x = 0) {
__m_cc_erase_in_line(w, y, x, w->_maxx-1, bgWidth);
}
__m_cc_erase_in_line(w, y, x, lx, bgWidth);
}
return (0);
}
int
__m_cc_expand(WINDOW *w, int y, int x, int side)
{
cchar_t cc;
int dx, width;
width = __m_cc_width(&w->_line[y][x]);
if (side < 0)
dx = __m_cc_next(w, y, x) - width;
else if (0 < side)
dx = __m_cc_first(w, y, x);
else
return (-1);
cc = w->_line[y][x];
return (__m_cc_replace(w, y, dx, &cc, 0));
}
int
__m_cc_equal(const cchar_t *c1, const cchar_t *c2)
{
int i;
if (c1->_f != c2->_f)
return (0);
if (c1->_n != c2->_n)
return (0);
for (i = 0; i < c1->_n; ++i)
if (c1->_wc[i] != c2->_wc[i])
return (0);
return (1);
}
int
__m_cc_compare(const cchar_t *c1, const cchar_t *c2, int exact)
{
int i;
if (exact && c1->_f != c2->_f)
return (0);
if (c1->_n != c2->_n)
return (0);
if ((c1->_at & ~WA_COOKIE) != (c2->_at & ~WA_COOKIE))
return (0);
if (c1->_co != c2->_co)
return (0);
for (i = 0; i < c1->_n; ++i)
if (c1->_wc[i] != c2->_wc[i])
return (0);
return (1);
}
int
__m_cc_write(const cchar_t *cc)
{
int j;
size_t i;
char mb[MB_LEN_MAX];
int backed_up = 0;
for (i = 0; i < cc->_n; ++i) {
j = wctomb(mb, cc->_wc[i]);
if (j == -1)
return (EOF);
if (i == 1) {
if (fwrite(cursor_left, 1, strlen(cursor_left),
__m_screen->_of) == 0) {
return (EOF);
}
backed_up = 1;
}
if (fwrite(mb, sizeof (*mb), (size_t)j, __m_screen->_of) == 0) {
return (EOF);
}
}
if (backed_up) {
if (fwrite(cursor_right, 1, strlen(cursor_right),
__m_screen->_of) == 0) {
return (EOF);
}
}
__m_screen->_flags |= W_FLUSH;
return (0);
}