1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 1997 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */ 28*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 29*7c478bd9Sstevel@tonic-gate 30*7c478bd9Sstevel@tonic-gate /* 31*7c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 32*7c478bd9Sstevel@tonic-gate * The Regents of the University of California 33*7c478bd9Sstevel@tonic-gate * All Rights Reserved 34*7c478bd9Sstevel@tonic-gate * 35*7c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 36*7c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 37*7c478bd9Sstevel@tonic-gate * contributors. 38*7c478bd9Sstevel@tonic-gate */ 39*7c478bd9Sstevel@tonic-gate 40*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 41*7c478bd9Sstevel@tonic-gate 42*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/ 43*7c478bd9Sstevel@tonic-gate 44*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 45*7c478bd9Sstevel@tonic-gate #include "curses_inc.h" 46*7c478bd9Sstevel@tonic-gate 47*7c478bd9Sstevel@tonic-gate /* 48*7c478bd9Sstevel@tonic-gate * Insert to 'win' at most n chars of a string 49*7c478bd9Sstevel@tonic-gate * starting at (cury, curx). However, if n <= 0, 50*7c478bd9Sstevel@tonic-gate * insert the entire string. 51*7c478bd9Sstevel@tonic-gate * \n, \t, \r, \b are treated in the same way 52*7c478bd9Sstevel@tonic-gate * as other control chars. 53*7c478bd9Sstevel@tonic-gate */ 54*7c478bd9Sstevel@tonic-gate 55*7c478bd9Sstevel@tonic-gate int 56*7c478bd9Sstevel@tonic-gate winsnstr(WINDOW *win, char *tsp, int n) 57*7c478bd9Sstevel@tonic-gate { 58*7c478bd9Sstevel@tonic-gate chtype *wcp; 59*7c478bd9Sstevel@tonic-gate int x, cury, endx, maxx, len; 60*7c478bd9Sstevel@tonic-gate bool savscrl, savsync, savimmed; 61*7c478bd9Sstevel@tonic-gate short savx, savy; 62*7c478bd9Sstevel@tonic-gate unsigned char *sp = (unsigned char *)tsp; 63*7c478bd9Sstevel@tonic-gate 64*7c478bd9Sstevel@tonic-gate /* only insert at the start of a character */ 65*7c478bd9Sstevel@tonic-gate win->_nbyte = -1; 66*7c478bd9Sstevel@tonic-gate win->_insmode = TRUE; 67*7c478bd9Sstevel@tonic-gate if (_scrmax > 1 && _mbvalid(win) == ERR) 68*7c478bd9Sstevel@tonic-gate return (ERR); 69*7c478bd9Sstevel@tonic-gate 70*7c478bd9Sstevel@tonic-gate if (n < 0) 71*7c478bd9Sstevel@tonic-gate n = MAXINT; 72*7c478bd9Sstevel@tonic-gate 73*7c478bd9Sstevel@tonic-gate /* compute total length of the insertion */ 74*7c478bd9Sstevel@tonic-gate endx = win->_curx; 75*7c478bd9Sstevel@tonic-gate maxx = win->_maxx; 76*7c478bd9Sstevel@tonic-gate for (x = 0; sp[x] != '\0' && x < n && endx < maxx; ++x) { 77*7c478bd9Sstevel@tonic-gate len = (sp[x] < ' '|| sp[x] == _CTRL('?')) ? 2 : 1; 78*7c478bd9Sstevel@tonic-gate 79*7c478bd9Sstevel@tonic-gate if (ISMBIT(sp[x])) { 80*7c478bd9Sstevel@tonic-gate int m, k, ty; 81*7c478bd9Sstevel@tonic-gate chtype c; 82*7c478bd9Sstevel@tonic-gate 83*7c478bd9Sstevel@tonic-gate /* see if the entire character is defined */ 84*7c478bd9Sstevel@tonic-gate c = RBYTE(sp[x]); 85*7c478bd9Sstevel@tonic-gate ty = TYPE(c); 86*7c478bd9Sstevel@tonic-gate m = x + cswidth[ty] - (ty == 0 ? 1 : 0); 87*7c478bd9Sstevel@tonic-gate for (k = x + 1; sp[k] != '\0' && k <= m; ++k) { 88*7c478bd9Sstevel@tonic-gate c = RBYTE(sp[k]); 89*7c478bd9Sstevel@tonic-gate if (TYPE(c) != 0) 90*7c478bd9Sstevel@tonic-gate break; 91*7c478bd9Sstevel@tonic-gate } 92*7c478bd9Sstevel@tonic-gate if (k <= m) 93*7c478bd9Sstevel@tonic-gate break; 94*7c478bd9Sstevel@tonic-gate /* recompute # of columns used */ 95*7c478bd9Sstevel@tonic-gate len = _curs_scrwidth[ty]; 96*7c478bd9Sstevel@tonic-gate /* skip an appropriate number of bytes */ 97*7c478bd9Sstevel@tonic-gate x = m; 98*7c478bd9Sstevel@tonic-gate } 99*7c478bd9Sstevel@tonic-gate 100*7c478bd9Sstevel@tonic-gate if ((endx += len) > maxx) { 101*7c478bd9Sstevel@tonic-gate endx -= len; 102*7c478bd9Sstevel@tonic-gate break; 103*7c478bd9Sstevel@tonic-gate } 104*7c478bd9Sstevel@tonic-gate } 105*7c478bd9Sstevel@tonic-gate 106*7c478bd9Sstevel@tonic-gate /* length of chars to be shifted */ 107*7c478bd9Sstevel@tonic-gate if ((len = endx - win->_curx) <= 0) 108*7c478bd9Sstevel@tonic-gate return (ERR); 109*7c478bd9Sstevel@tonic-gate 110*7c478bd9Sstevel@tonic-gate /* number of chars insertible */ 111*7c478bd9Sstevel@tonic-gate n = x; 112*7c478bd9Sstevel@tonic-gate 113*7c478bd9Sstevel@tonic-gate /* shift data */ 114*7c478bd9Sstevel@tonic-gate cury = win->_cury; 115*7c478bd9Sstevel@tonic-gate 116*7c478bd9Sstevel@tonic-gate if (_mbinsshift(win, len) == ERR) 117*7c478bd9Sstevel@tonic-gate return (ERR); 118*7c478bd9Sstevel@tonic-gate 119*7c478bd9Sstevel@tonic-gate /* insert new data */ 120*7c478bd9Sstevel@tonic-gate wcp = win->_y[cury] + win->_curx; 121*7c478bd9Sstevel@tonic-gate 122*7c478bd9Sstevel@tonic-gate /* act as if we are adding characters */ 123*7c478bd9Sstevel@tonic-gate savx = win->_curx; 124*7c478bd9Sstevel@tonic-gate savy = win->_cury; 125*7c478bd9Sstevel@tonic-gate win->_insmode = FALSE; 126*7c478bd9Sstevel@tonic-gate savscrl = win->_scroll; 127*7c478bd9Sstevel@tonic-gate savimmed = win->_immed; 128*7c478bd9Sstevel@tonic-gate savsync = win->_sync; 129*7c478bd9Sstevel@tonic-gate win->_scroll = win->_sync; 130*7c478bd9Sstevel@tonic-gate 131*7c478bd9Sstevel@tonic-gate for (; n > 0; --n, ++sp) { 132*7c478bd9Sstevel@tonic-gate /* multi-byte characters */ 133*7c478bd9Sstevel@tonic-gate if (_mbtrue && ISMBIT(*sp)) { 134*7c478bd9Sstevel@tonic-gate (void) _mbaddch(win, A_NORMAL, RBYTE(*sp)); 135*7c478bd9Sstevel@tonic-gate wcp = win->_y[cury] + win->_curx; 136*7c478bd9Sstevel@tonic-gate continue; 137*7c478bd9Sstevel@tonic-gate } 138*7c478bd9Sstevel@tonic-gate if (_scrmax > 1 && ISMBIT(*wcp)) 139*7c478bd9Sstevel@tonic-gate (void) _mbclrch(win, cury, win->_curx); 140*7c478bd9Sstevel@tonic-gate /* single byte character */ 141*7c478bd9Sstevel@tonic-gate win->_nbyte = -1; 142*7c478bd9Sstevel@tonic-gate 143*7c478bd9Sstevel@tonic-gate if (*sp < ' ' || *sp == _CTRL('?')) { 144*7c478bd9Sstevel@tonic-gate *wcp++ = _CHAR('^') | win->_attrs; 145*7c478bd9Sstevel@tonic-gate *wcp = _CHAR(_UNCTRL(*sp)) | win->_attrs; 146*7c478bd9Sstevel@tonic-gate } else 147*7c478bd9Sstevel@tonic-gate *wcp = _CHAR(*sp) | win->_attrs; 148*7c478bd9Sstevel@tonic-gate win->_curx += (*sp < ' ' || *sp == _CTRL('?')) ? 2 : 1; 149*7c478bd9Sstevel@tonic-gate ++wcp; 150*7c478bd9Sstevel@tonic-gate } 151*7c478bd9Sstevel@tonic-gate win->_curx = savx; 152*7c478bd9Sstevel@tonic-gate win->_cury = savy; 153*7c478bd9Sstevel@tonic-gate 154*7c478bd9Sstevel@tonic-gate /* update the change structure */ 155*7c478bd9Sstevel@tonic-gate if (win->_firstch[cury] > win->_curx) 156*7c478bd9Sstevel@tonic-gate win->_firstch[cury] = win->_curx; 157*7c478bd9Sstevel@tonic-gate win->_lastch[cury] = maxx - 1; 158*7c478bd9Sstevel@tonic-gate 159*7c478bd9Sstevel@tonic-gate win->_flags |= _WINCHANGED; 160*7c478bd9Sstevel@tonic-gate 161*7c478bd9Sstevel@tonic-gate win->_scroll = savscrl; 162*7c478bd9Sstevel@tonic-gate win->_sync = savsync; 163*7c478bd9Sstevel@tonic-gate win->_immed = savimmed; 164*7c478bd9Sstevel@tonic-gate 165*7c478bd9Sstevel@tonic-gate if (win->_sync) 166*7c478bd9Sstevel@tonic-gate wsyncup(win); 167*7c478bd9Sstevel@tonic-gate return (win->_immed ? wrefresh(win) : OK); 168*7c478bd9Sstevel@tonic-gate } 169