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