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