GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libcurses/base/wresize.c Lines: 0 80 0.0 %
Date: 2016-12-06 Branches: 0 78 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: wresize.c,v 1.5 2010/01/12 23:22:06 nicm Exp $ */
2
3
/****************************************************************************
4
 * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc.              *
5
 *                                                                          *
6
 * Permission is hereby granted, free of charge, to any person obtaining a  *
7
 * copy of this software and associated documentation files (the            *
8
 * "Software"), to deal in the Software without restriction, including      *
9
 * without limitation the rights to use, copy, modify, merge, publish,      *
10
 * distribute, distribute with modifications, sublicense, and/or sell       *
11
 * copies of the Software, and to permit persons to whom the Software is    *
12
 * furnished to do so, subject to the following conditions:                 *
13
 *                                                                          *
14
 * The above copyright notice and this permission notice shall be included  *
15
 * in all copies or substantial portions of the Software.                   *
16
 *                                                                          *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20
 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24
 *                                                                          *
25
 * Except as contained in this notice, the name(s) of the above copyright   *
26
 * holders shall not be used in advertising or otherwise to promote the     *
27
 * sale, use or other dealings in this Software without prior written       *
28
 * authorization.                                                           *
29
 ****************************************************************************/
30
31
/****************************************************************************
32
 *  Author: Thomas E. Dickey 1996-2002                                      *
33
 ****************************************************************************/
34
35
#include <curses.priv.h>
36
37
MODULE_ID("$Id: wresize.c,v 1.5 2010/01/12 23:22:06 nicm Exp $")
38
39
static int
40
cleanup_lines(struct ldat *data, int length)
41
{
42
    while (--length >= 0)
43
	free(data[length].text);
44
    free(data);
45
    return ERR;
46
}
47
48
/*
49
 * If we have reallocated the ldat structs, we will have to repair pointers
50
 * used in subwindows.
51
 */
52
static void
53
repair_subwindows(WINDOW *cmp)
54
{
55
    WINDOWLIST *wp;
56
    struct ldat *pline = cmp->_line;
57
    int row;
58
59
    _nc_lock_global(curses);
60
61
    for (each_window(wp)) {
62
	WINDOW *tst = &(wp->win);
63
64
	if (tst->_parent == cmp) {
65
66
	    if (tst->_pary > cmp->_maxy)
67
		tst->_pary = cmp->_maxy;
68
	    if (tst->_parx > cmp->_maxx)
69
		tst->_parx = cmp->_maxx;
70
71
	    if (tst->_maxy + tst->_pary > cmp->_maxy)
72
		tst->_maxy = cmp->_maxy - tst->_pary;
73
	    if (tst->_maxx + tst->_parx > cmp->_maxx)
74
		tst->_maxx = cmp->_maxx - tst->_parx;
75
76
	    for (row = 0; row <= tst->_maxy; ++row) {
77
		tst->_line[row].text = &pline[tst->_pary + row].text[tst->_parx];
78
	    }
79
	    repair_subwindows(tst);
80
	}
81
    }
82
    _nc_unlock_global(curses);
83
}
84
85
/*
86
 * Reallocate a curses WINDOW struct to either shrink or grow to the specified
87
 * new lines/columns.  If it grows, the new character cells are filled with
88
 * blanks.  The application is responsible for repainting the blank area.
89
 */
90
NCURSES_EXPORT(int)
91
wresize(WINDOW *win, int ToLines, int ToCols)
92
{
93
    int col, row, size_x, size_y;
94
    struct ldat *pline;
95
    struct ldat *new_lines = 0;
96
97
#ifdef TRACE
98
    T((T_CALLED("wresize(%p,%d,%d)"), win, ToLines, ToCols));
99
    if (win) {
100
	TR(TRACE_UPDATE, ("...beg (%ld, %ld), max(%ld,%ld), reg(%ld,%ld)",
101
			  (long) win->_begy, (long) win->_begx,
102
			  (long) win->_maxy, (long) win->_maxx,
103
			  (long) win->_regtop, (long) win->_regbottom));
104
	if (USE_TRACEF(TRACE_UPDATE)) {
105
	    _tracedump("...before", win);
106
	    _nc_unlock_global(tracef);
107
	}
108
    }
109
#endif
110
111
    if (!win || --ToLines < 0 || --ToCols < 0)
112
	returnCode(ERR);
113
114
    size_x = win->_maxx;
115
    size_y = win->_maxy;
116
117
    if (ToLines == size_y
118
	&& ToCols == size_x)
119
	returnCode(OK);
120
121
    if ((win->_flags & _SUBWIN)) {
122
	/*
123
	 * Check if the new limits will fit into the parent window's size.  If
124
	 * not, do not resize.  We could adjust the location of the subwindow,
125
	 * but the application may not like that.
126
	 */
127
	if (win->_pary + ToLines > win->_parent->_maxy
128
	    || win->_parx + ToCols > win->_parent->_maxx) {
129
	    returnCode(ERR);
130
	}
131
	pline = win->_parent->_line;
132
    } else {
133
	pline = 0;
134
    }
135
136
    /*
137
     * Allocate new memory as needed.  Do the allocations without modifying
138
     * the original window, in case an allocation fails.  Always allocate
139
     * (at least temporarily) the array pointing to the individual lines.
140
     */
141
    new_lines = typeCalloc(struct ldat, (unsigned) (ToLines + 1));
142
    if (new_lines == 0)
143
	returnCode(ERR);
144
145
    /*
146
     * For each line in the target, allocate or adjust pointers for the
147
     * corresponding text, depending on whether this is a window or a
148
     * subwindow.
149
     */
150
    for (row = 0; row <= ToLines; ++row) {
151
	int begin = (row > size_y) ? 0 : (size_x + 1);
152
	int end = ToCols;
153
	NCURSES_CH_T *s;
154
155
	if (!(win->_flags & _SUBWIN)) {
156
	    if (row <= size_y) {
157
		if (ToCols != size_x) {
158
		    if ((s = typeMalloc(NCURSES_CH_T, ToCols + 1)) == 0)
159
			returnCode(cleanup_lines(new_lines, row));
160
		    for (col = 0; col <= ToCols; ++col) {
161
			s[col] = (col <= size_x
162
				  ? win->_line[row].text[col]
163
				  : win->_nc_bkgd);
164
		    }
165
		} else {
166
		    s = win->_line[row].text;
167
		}
168
	    } else {
169
		if ((s = typeMalloc(NCURSES_CH_T, ToCols + 1)) == 0)
170
		    returnCode(cleanup_lines(new_lines, row));
171
		for (col = 0; col <= ToCols; ++col)
172
		    s[col] = win->_nc_bkgd;
173
	    }
174
	} else {
175
	    s = &pline[win->_pary + row].text[win->_parx];
176
	}
177
178
	if_USE_SCROLL_HINTS(new_lines[row].oldindex = row);
179
	if (row <= size_y) {
180
	    new_lines[row].firstchar = win->_line[row].firstchar;
181
	    new_lines[row].lastchar = win->_line[row].lastchar;
182
	}
183
	if ((ToCols != size_x) || (row > size_y)) {
184
	    if (end >= begin) {	/* growing */
185
		if (new_lines[row].firstchar < begin)
186
		    new_lines[row].firstchar = begin;
187
	    } else {		/* shrinking */
188
		new_lines[row].firstchar = 0;
189
	    }
190
	    new_lines[row].lastchar = ToCols;
191
	}
192
	new_lines[row].text = s;
193
    }
194
195
    /*
196
     * Dispose of unwanted memory.
197
     */
198
    if (!(win->_flags & _SUBWIN)) {
199
	if (ToCols == size_x) {
200
	    for (row = ToLines + 1; row <= size_y; row++) {
201
		free(win->_line[row].text);
202
	    }
203
	} else {
204
	    for (row = 0; row <= size_y; row++) {
205
		free(win->_line[row].text);
206
	    }
207
	}
208
    }
209
210
    free(win->_line);
211
    win->_line = new_lines;
212
213
    /*
214
     * Finally, adjust the parameters showing screen size and cursor
215
     * position:
216
     */
217
    win->_maxx = ToCols;
218
    win->_maxy = ToLines;
219
220
    if (win->_regtop > win->_maxy)
221
	win->_regtop = win->_maxy;
222
    if (win->_regbottom > win->_maxy
223
	|| win->_regbottom == size_y)
224
	win->_regbottom = win->_maxy;
225
226
    if (win->_curx > win->_maxx)
227
	win->_curx = win->_maxx;
228
    if (win->_cury > win->_maxy)
229
	win->_cury = win->_maxy;
230
231
    /*
232
     * Check for subwindows of this one, and readjust pointers to our text,
233
     * if needed.
234
     */
235
    repair_subwindows(win);
236
237
#ifdef TRACE
238
    TR(TRACE_UPDATE, ("...beg (%ld, %ld), max(%ld,%ld), reg(%ld,%ld)",
239
		      (long) win->_begy, (long) win->_begx,
240
		      (long) win->_maxy, (long) win->_maxx,
241
		      (long) win->_regtop, (long) win->_regbottom));
242
    if (USE_TRACEF(TRACE_UPDATE)) {
243
	_tracedump("...after:", win);
244
	_nc_unlock_global(tracef);
245
    }
246
#endif
247
    returnCode(OK);
248
}