[BACK]Return to wwsize.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / window

Annotation of src/usr.bin/window/wwsize.c, Revision 1.6

1.6     ! millert     1: /*     $OpenBSD: wwsize.c,v 1.5 1997/02/25 00:05:08 downsj Exp $       */
1.3       niklas      2: /*     $NetBSD: wwsize.c,v 1.5 1996/02/08 20:45:11 mycroft Exp $       */
1.1       deraadt     3:
                      4: /*
                      5:  * Copyright (c) 1983, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * This code is derived from software contributed to Berkeley by
                      9:  * Edward Wang at The University of California, Berkeley.
                     10:  *
                     11:  * Redistribution and use in source and binary forms, with or without
                     12:  * modification, are permitted provided that the following conditions
                     13:  * are met:
                     14:  * 1. Redistributions of source code must retain the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer.
                     16:  * 2. Redistributions in binary form must reproduce the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer in the
                     18:  *    documentation and/or other materials provided with the distribution.
                     19:  * 3. All advertising materials mentioning features or use of this software
                     20:  *    must display the following acknowledgement:
                     21:  *     This product includes software developed by the University of
                     22:  *     California, Berkeley and its contributors.
                     23:  * 4. Neither the name of the University nor the names of its contributors
                     24:  *    may be used to endorse or promote products derived from this software
                     25:  *    without specific prior written permission.
                     26:  *
                     27:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     28:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     29:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     30:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     31:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     32:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     33:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     34:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     35:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     36:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     37:  * SUCH DAMAGE.
                     38:  */
                     39:
                     40: #ifndef lint
                     41: #if 0
                     42: static char sccsid[] = "@(#)wwsize.c   8.1 (Berkeley) 6/6/93";
                     43: #else
1.6     ! millert    44: static char rcsid[] = "$OpenBSD: wwsize.c,v 1.5 1997/02/25 00:05:08 downsj Exp $";
1.1       deraadt    45: #endif
                     46: #endif /* not lint */
                     47:
1.6     ! millert    48: #include <stdlib.h>
1.1       deraadt    49: #include "ww.h"
                     50:
                     51: /*
                     52:  * Resize a window.  Should be unattached.
                     53:  */
                     54: wwsize(w, nrow, ncol)
                     55: register struct ww *w;
                     56: {
                     57:        register i, j;
                     58:        int nline;
                     59:        union ww_char **buf = 0;
                     60:        char **win = 0;
                     61:        short *nvis = 0;
                     62:        char **fmap = 0;
                     63:        char m;
                     64:
                     65:        /*
                     66:         * First allocate new buffers.
                     67:         */
                     68:        win = wwalloc(w->ww_w.t, w->ww_w.l, nrow, ncol, sizeof (char));
                     69:        if (win == 0)
                     70:                goto bad;
                     71:        if (w->ww_fmap != 0) {
                     72:                fmap = wwalloc(w->ww_w.t, w->ww_w.l, nrow, ncol, sizeof (char));
                     73:                if (fmap == 0)
                     74:                        goto bad;
                     75:        }
                     76:        if (nrow > w->ww_b.nr || ncol > w->ww_b.nc) {
                     77:                nline = MAX(w->ww_b.nr, nrow);
                     78:                buf = (union ww_char **) wwalloc(w->ww_b.t, w->ww_b.l,
                     79:                        nline, ncol, sizeof (union ww_char));
                     80:                if (buf == 0)
                     81:                        goto bad;
                     82:        }
1.6     ! millert    83:        nvis = (short *)malloc(nrow * sizeof (short));
1.1       deraadt    84:        if (nvis == 0) {
                     85:                wwerrno = WWE_NOMEM;
                     86:                goto bad;
                     87:        }
                     88:        nvis -= w->ww_w.t;
                     89:        /*
                     90:         * Copy text buffer.
                     91:         */
                     92:        if (buf != 0) {
                     93:                int b, r;
                     94:
                     95:                b = w->ww_b.t + nline;
                     96:                r = w->ww_b.l + ncol;
                     97:                if (ncol < w->ww_b.nc)
                     98:                        for (i = w->ww_b.t; i < w->ww_b.b; i++)
                     99:                                for (j = w->ww_b.l; j < r; j++)
                    100:                                        buf[i][j] = w->ww_buf[i][j];
                    101:                else
                    102:                        for (i = w->ww_b.t; i < w->ww_b.b; i++) {
                    103:                                for (j = w->ww_b.l; j < w->ww_b.r; j++)
                    104:                                        buf[i][j] = w->ww_buf[i][j];
                    105:                                for (; j < r; j++)
                    106:                                        buf[i][j].c_w = ' ';
                    107:                        }
                    108:                for (; i < b; i++)
                    109:                        for (j = w->ww_b.l; j < r; j++)
                    110:                                buf[i][j].c_w = ' ';
                    111:        }
                    112:        /*
                    113:         * Now free the old stuff.
                    114:         */
                    115:        wwfree((char **)w->ww_win, w->ww_w.t);
                    116:        w->ww_win = win;
                    117:        if (buf != 0) {
                    118:                wwfree((char **)w->ww_buf, w->ww_b.t);
                    119:                w->ww_buf = buf;
                    120:        }
                    121:        if (w->ww_fmap != 0) {
                    122:                wwfree((char **)w->ww_fmap, w->ww_w.t);
                    123:                w->ww_fmap = fmap;
                    124:        }
                    125:        free((char *)(w->ww_nvis + w->ww_w.t));
                    126:        w->ww_nvis = nvis;
                    127:        /*
                    128:         * Set new sizes.
                    129:         */
                    130:                /* window */
                    131:        w->ww_w.b = w->ww_w.t + nrow;
                    132:        w->ww_w.r = w->ww_w.l + ncol;
                    133:        w->ww_w.nr = nrow;
                    134:        w->ww_w.nc = ncol;
                    135:                /* text buffer */
                    136:        if (buf != 0) {
                    137:                w->ww_b.b = w->ww_b.t + nline;
                    138:                w->ww_b.r = w->ww_b.l + ncol;
                    139:                w->ww_b.nr = nline;
                    140:                w->ww_b.nc = ncol;
                    141:        }
                    142:                /* scroll */
                    143:        if ((i = w->ww_b.b - w->ww_w.b) < 0 ||
                    144:            (i = w->ww_cur.r - w->ww_w.b + 1) > 0) {
                    145:                w->ww_buf += i;
                    146:                w->ww_b.t -= i;
                    147:                w->ww_b.b -= i;
                    148:                w->ww_cur.r -= i;
                    149:        }
                    150:                /* interior */
                    151:        w->ww_i.b = MIN(w->ww_w.b, wwnrow);
                    152:        w->ww_i.r = MIN(w->ww_w.r, wwncol);
                    153:        w->ww_i.nr = w->ww_i.b - w->ww_i.t;
                    154:        w->ww_i.nc = w->ww_i.r - w->ww_i.l;
                    155:        /*
                    156:         * Initialize new buffers.
                    157:         */
                    158:                /* window */
                    159:        m = 0;
                    160:        if (w->ww_oflags & WWO_GLASS)
                    161:                m |= WWM_GLS;
                    162:        if (w->ww_oflags & WWO_REVERSE)
                    163:                m |= WWM_REV;
                    164:        for (i = w->ww_w.t; i < w->ww_w.b; i++)
                    165:                for (j = w->ww_w.l; j < w->ww_w.r; j++)
                    166:                        w->ww_win[i][j] = m;
                    167:                /* frame map */
                    168:        if (fmap != 0)
                    169:                for (i = w->ww_w.t; i < w->ww_w.b; i++)
                    170:                        for (j = w->ww_w.l; j < w->ww_w.r; j++)
                    171:                                w->ww_fmap[i][j] = 0;
                    172:                /* visibility */
                    173:        j = m ? 0 : w->ww_w.nc;
                    174:        for (i = w->ww_w.t; i < w->ww_w.b; i++)
                    175:                w->ww_nvis[i] = j;
                    176:        /*
                    177:         * Put cursor back.
                    178:         */
1.3       niklas    179:        if (ISSET(w->ww_wflags, WWW_HASCURSOR)) {
                    180:                CLR(w->ww_wflags, WWW_HASCURSOR);
1.1       deraadt   181:                wwcursor(w, 1);
                    182:        }
                    183:        /*
                    184:         * Fool with pty.
                    185:         */
1.2       deraadt   186:        if (w->ww_type == WWT_PTY && w->ww_pty >= 0)
1.1       deraadt   187:                (void) wwsetttysize(w->ww_pty, nrow, ncol);
                    188:        return 0;
                    189: bad:
                    190:        if (win != 0)
                    191:                wwfree(win, w->ww_w.t);
                    192:        if (fmap != 0)
                    193:                wwfree(fmap, w->ww_w.t);
                    194:        if (buf != 0)
                    195:                wwfree((char **)buf, w->ww_b.t);
                    196:        if (nvis != 0)
                    197:                free((char *)(nvis + w->ww_w.t));
                    198:        return -1;
                    199: }