]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - usr.bin/window/wwsize.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / usr.bin / window / wwsize.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[] = "@(#)wwsize.c    8.1 (Berkeley) 6/6/93";
39 static char rcsid[] =
40   "$FreeBSD$";
41 #endif /* not lint */
42
43 #include <stdlib.h>
44 #include "ww.h"
45
46 /*
47  * Resize a window.  Should be unattached.
48  */
49 wwsize(w, nrow, ncol)
50 register struct ww *w;
51 {
52         register i, j;
53         int nline;
54         union ww_char **buf = 0;
55         char **win = 0;
56         short *nvis = 0;
57         char **fmap = 0;
58         char m;
59
60         /*
61          * First allocate new buffers.
62          */
63         win = wwalloc(w->ww_w.t, w->ww_w.l, nrow, ncol, sizeof (char));
64         if (win == 0)
65                 goto bad;
66         if (w->ww_fmap != 0) {
67                 fmap = wwalloc(w->ww_w.t, w->ww_w.l, nrow, ncol, sizeof (char));
68                 if (fmap == 0)
69                         goto bad;
70         }
71         if (nrow > w->ww_b.nr || ncol > w->ww_b.nc) {
72                 nline = MAX(w->ww_b.nr, nrow);
73                 buf = (union ww_char **) wwalloc(w->ww_b.t, w->ww_b.l,
74                         nline, ncol, sizeof (union ww_char));
75                 if (buf == 0)
76                         goto bad;
77         }
78         nvis = (short *)malloc((unsigned) nrow * sizeof (short));
79         if (nvis == 0) {
80                 wwerrno = WWE_NOMEM;
81                 goto bad;
82         }
83         nvis -= w->ww_w.t;
84         /*
85          * Copy text buffer.
86          */
87         if (buf != 0) {
88                 int b, r;
89
90                 b = w->ww_b.t + nline;
91                 r = w->ww_b.l + ncol;
92                 if (ncol < w->ww_b.nc)
93                         for (i = w->ww_b.t; i < w->ww_b.b; i++)
94                                 for (j = w->ww_b.l; j < r; j++)
95                                         buf[i][j] = w->ww_buf[i][j];
96                 else
97                         for (i = w->ww_b.t; i < w->ww_b.b; i++) {
98                                 for (j = w->ww_b.l; j < w->ww_b.r; j++)
99                                         buf[i][j] = w->ww_buf[i][j];
100                                 for (; j < r; j++)
101                                         buf[i][j].c_w = ' ';
102                         }
103                 for (; i < b; i++)
104                         for (j = w->ww_b.l; j < r; j++)
105                                 buf[i][j].c_w = ' ';
106         }
107         /*
108          * Now free the old stuff.
109          */
110         wwfree((char **)w->ww_win, w->ww_w.t);
111         w->ww_win = win;
112         if (buf != 0) {
113                 wwfree((char **)w->ww_buf, w->ww_b.t);
114                 w->ww_buf = buf;
115         }
116         if (w->ww_fmap != 0) {
117                 wwfree((char **)w->ww_fmap, w->ww_w.t);
118                 w->ww_fmap = fmap;
119         }
120         free((char *)(w->ww_nvis + w->ww_w.t));
121         w->ww_nvis = nvis;
122         /*
123          * Set new sizes.
124          */
125                 /* window */
126         w->ww_w.b = w->ww_w.t + nrow;
127         w->ww_w.r = w->ww_w.l + ncol;
128         w->ww_w.nr = nrow;
129         w->ww_w.nc = ncol;
130                 /* text buffer */
131         if (buf != 0) {
132                 w->ww_b.b = w->ww_b.t + nline;
133                 w->ww_b.r = w->ww_b.l + ncol;
134                 w->ww_b.nr = nline;
135                 w->ww_b.nc = ncol;
136         }
137                 /* scroll */
138         if ((i = w->ww_b.b - w->ww_w.b) < 0 ||
139             (i = w->ww_cur.r - w->ww_w.b + 1) > 0) {
140                 w->ww_buf += i;
141                 w->ww_b.t -= i;
142                 w->ww_b.b -= i;
143                 w->ww_cur.r -= i;
144         }
145                 /* interior */
146         w->ww_i.b = MIN(w->ww_w.b, wwnrow);
147         w->ww_i.r = MIN(w->ww_w.r, wwncol);
148         w->ww_i.nr = w->ww_i.b - w->ww_i.t;
149         w->ww_i.nc = w->ww_i.r - w->ww_i.l;
150         /*
151          * Initialize new buffers.
152          */
153                 /* window */
154         m = 0;
155         if (w->ww_oflags & WWO_GLASS)
156                 m |= WWM_GLS;
157         if (w->ww_oflags & WWO_REVERSE)
158                 m |= WWM_REV;
159         for (i = w->ww_w.t; i < w->ww_w.b; i++)
160                 for (j = w->ww_w.l; j < w->ww_w.r; j++)
161                         w->ww_win[i][j] = m;
162                 /* frame map */
163         if (fmap != 0)
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_fmap[i][j] = 0;
167                 /* visibility */
168         j = m ? 0 : w->ww_w.nc;
169         for (i = w->ww_w.t; i < w->ww_w.b; i++)
170                 w->ww_nvis[i] = j;
171         /*
172          * Put cursor back.
173          */
174         if (w->ww_hascursor) {
175                 w->ww_hascursor = 0;
176                 wwcursor(w, 1);
177         }
178         /*
179          * Fool with pty.
180          */
181         if (w->ww_ispty && w->ww_pty >= 0)
182                 (void) wwsetttysize(w->ww_pty, nrow, ncol);
183         return 0;
184 bad:
185         if (win != 0)
186                 wwfree(win, w->ww_w.t);
187         if (fmap != 0)
188                 wwfree(fmap, w->ww_w.t);
189         if (buf != 0)
190                 wwfree((char **)buf, w->ww_b.t);
191         if (nvis != 0)
192                 free((char *)(nvis + w->ww_w.t));
193         return -1;
194 }