BSD 4_3_Net_2 release
[unix-history] / usr / src / usr.bin / window / wwsize.c
CommitLineData
60de5df9 1/*
46e9ea25
KB
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved.
4 *
3dd3a9e5
KB
5 * This code is derived from software contributed to Berkeley by
6 * Edward Wang at The University of California, Berkeley.
7 *
af359dea
C
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.
60de5df9
EW
35 */
36
46e9ea25 37#ifndef lint
af359dea 38static char sccsid[] = "@(#)wwsize.c 3.10 (Berkeley) 8/12/90";
46e9ea25
KB
39#endif /* not lint */
40
0b8d556f
EW
41#include "ww.h"
42
43/*
44 * Resize a window. Should be unattached.
45 */
46wwsize(w, nrow, ncol)
47register struct ww *w;
48{
49 register i, j;
50 int nline;
51 union ww_char **buf = 0;
52 char **win = 0;
53 short *nvis = 0;
54 char **fmap = 0;
55 char m;
56
0b8d556f
EW
57 /*
58 * First allocate new buffers.
59 */
60 win = wwalloc(w->ww_w.t, w->ww_w.l, nrow, ncol, sizeof (char));
61 if (win == 0)
62 goto bad;
63 if (w->ww_fmap != 0) {
64 fmap = wwalloc(w->ww_w.t, w->ww_w.l, nrow, ncol, sizeof (char));
65 if (fmap == 0)
66 goto bad;
67 }
68 if (nrow > w->ww_b.nr || ncol > w->ww_b.nc) {
69 nline = MAX(w->ww_b.nr, nrow);
70 buf = (union ww_char **) wwalloc(w->ww_b.t, w->ww_b.l,
71 nline, ncol, sizeof (union ww_char));
72 if (buf == 0)
73 goto bad;
74 }
75 nvis = (short *)malloc((unsigned) nrow * sizeof (short));
76 if (nvis == 0) {
77 wwerrno = WWE_NOMEM;
78 goto bad;
79 }
80 nvis -= w->ww_w.t;
81 /*
82 * Copy text buffer.
83 */
84 if (buf != 0) {
85 int b, r;
86
87 b = w->ww_b.t + nline;
88 r = w->ww_b.l + ncol;
89 if (ncol < w->ww_b.nc)
90 for (i = w->ww_b.t; i < w->ww_b.b; i++)
91 for (j = w->ww_b.l; j < r; j++)
92 buf[i][j] = w->ww_buf[i][j];
93 else
94 for (i = w->ww_b.t; i < w->ww_b.b; i++) {
95 for (j = w->ww_b.l; j < w->ww_b.r; j++)
96 buf[i][j] = w->ww_buf[i][j];
97 for (; j < r; j++)
98 buf[i][j].c_w = ' ';
99 }
100 for (; i < b; i++)
101 for (j = w->ww_b.l; j < r; j++)
102 buf[i][j].c_w = ' ';
103 }
104 /*
105 * Now free the old stuff.
106 */
107 wwfree((char **)w->ww_win, w->ww_w.t);
108 w->ww_win = win;
109 if (buf != 0) {
110 wwfree((char **)w->ww_buf, w->ww_b.t);
111 w->ww_buf = buf;
112 }
113 if (w->ww_fmap != 0) {
114 wwfree((char **)w->ww_fmap, w->ww_w.t);
115 w->ww_fmap = fmap;
116 }
117 free((char *)(w->ww_nvis + w->ww_w.t));
118 w->ww_nvis = nvis;
119 /*
120 * Set new sizes.
121 */
122 /* window */
123 w->ww_w.b = w->ww_w.t + nrow;
124 w->ww_w.r = w->ww_w.l + ncol;
125 w->ww_w.nr = nrow;
126 w->ww_w.nc = ncol;
127 /* text buffer */
128 if (buf != 0) {
129 w->ww_b.b = w->ww_b.t + nline;
130 w->ww_b.r = w->ww_b.l + ncol;
131 w->ww_b.nr = nline;
132 w->ww_b.nc = ncol;
133 }
134 /* scroll */
135 if ((i = w->ww_b.b - w->ww_w.b) < 0 ||
136 (i = w->ww_cur.r - w->ww_w.b + 1) > 0) {
137 w->ww_buf += i;
138 w->ww_b.t -= i;
139 w->ww_b.b -= i;
140 w->ww_cur.r -= i;
141 }
142 /* interior */
143 w->ww_i.b = MIN(w->ww_w.b, wwnrow);
144 w->ww_i.r = MIN(w->ww_w.r, wwncol);
145 w->ww_i.nr = w->ww_i.b - w->ww_i.t;
146 w->ww_i.nc = w->ww_i.r - w->ww_i.l;
147 /*
148 * Initialize new buffers.
149 */
150 /* window */
151 m = 0;
152 if (w->ww_oflags & WWO_GLASS)
153 m |= WWM_GLS;
154 if (w->ww_oflags & WWO_REVERSE)
155 m |= WWM_REV;
156 for (i = w->ww_w.t; i < w->ww_w.b; i++)
157 for (j = w->ww_w.l; j < w->ww_w.r; j++)
158 w->ww_win[i][j] = m;
159 /* frame map */
160 if (fmap != 0)
161 for (i = w->ww_w.t; i < w->ww_w.b; i++)
162 for (j = w->ww_w.l; j < w->ww_w.r; j++)
163 w->ww_fmap[i][j] = 0;
164 /* visibility */
165 j = m ? 0 : w->ww_w.nc;
166 for (i = w->ww_w.t; i < w->ww_w.b; i++)
167 w->ww_nvis[i] = j;
168 /*
169 * Put cursor back.
170 */
171 if (w->ww_hascursor) {
172 w->ww_hascursor = 0;
173 wwcursor(w, 1);
174 }
175 /*
176 * Fool with pty.
177 */
9de86788
EW
178 if (w->ww_ispty && w->ww_pty >= 0)
179 (void) wwsetttysize(w->ww_pty, nrow, ncol);
0b8d556f
EW
180 return 0;
181bad:
182 if (win != 0)
183 wwfree(win, w->ww_w.t);
184 if (fmap != 0)
185 wwfree(fmap, w->ww_w.t);
186 if (buf != 0)
187 wwfree((char **)buf, w->ww_b.t);
188 if (nvis != 0)
189 free((char *)(nvis + w->ww_w.t));
190 return -1;
191}