xref: /illumos-gate/usr/src/lib/libcurses/screen/newwin.c (revision 201ceb75ab95f9bf1f42ea1dc9ab363b43ba47cf)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 /*LINTLIBRARY*/
41 
42 /* allocate space for and set up defaults for a new _window */
43 
44 #include	<stdlib.h>
45 #include	<sys/types.h>
46 #include	"curses_inc.h"
47 
48 WINDOW	*
49 newwin(int nlines, int ncols, int by, int bx)
50 {
51 	WINDOW	*win;
52 	int		counter = 0;
53 
54 	if (nlines <= 0)
55 		nlines = LINES - by;
56 	if (ncols <= 0)
57 		ncols = COLS - bx;
58 
59 	if ((by < 0) || (bx < 0) || ((win = _makenew(nlines, ncols, by,
60 	    bx)) == (WINDOW *) NULL) || (_image(win) == ERR)) {
61 		return ((WINDOW *) NULL);
62 	}
63 
64 	while (counter < nlines) {
65 		memSset(&win->_y[counter][0], (chtype) ' ', ncols);
66 #ifdef	_VR3_COMPAT_CODE
67 		if (_y16update) {
68 			int	i = ncols;
69 
70 			while (i--)
71 				win->_y16[counter][i] = (_ochtype) ' ';
72 		}
73 #endif	/* _VR3_COMPAT_CODE */
74 		counter++;
75 	}
76 
77 	win->_yoffset = SP->Yabove;
78 	return (win);
79 }
80 
81 int
82 _image(WINDOW *win)
83 {
84 			int	i, nlines = win->_maxy;
85 #ifdef	_VR3_COMPAT_CODE
86 			size_t oscols = win->_maxx * sizeof (_ochtype);
87 #endif	/* _VR3_COMPAT_CODE */
88 			size_t scols = win->_maxx * sizeof (chtype);
89 			chtype	**_y = win->_y;
90 #ifdef	_VR3_COMPAT_CODE
91 			_ochtype	**_y16 = win->_y16;
92 #endif	/* _VR3_COMPAT_CODE */
93 
94 	for (i = 0; i < nlines; i++) {
95 #ifdef	_VR3_COMPAT_CODE
96 		if (((_y[i] = (chtype *) malloc(scols)) == NULL) ||
97 		    ((_y16update) && ((_y16[i] = (_ochtype *)
98 		    malloc(oscols)) == NULL)))
99 #else	/* _VR3_COMPAT_CODE */
100 			if ((_y[i] = (chtype *) malloc(scols)) ==
101 			    NULL)
102 #endif	/* _VR3_COMPAT_CODE */
103 				{
104 				int	j;
105 
106 				curs_errno = CURS_BAD_MALLOC;
107 #ifdef	DEBUG
108 				strcpy(curs_parm_err, "_image");
109 #endif	/* DEBUG */
110 #ifdef	_VR3_COMPAT_CODE
111 				for (j = 0; j <= i; j++) {
112 					if (_y[j] != NULL)
113 					free((char *) _y[j]);
114 					if ((_y16update) && (_y16[j] != NULL))
115 						free((char *) _y16[j]);
116 				}
117 #else	/* _VR3_COMPAT_CODE */
118 				for (j = 0; j < i; j++)
119 					free((char *) _y[j]);
120 #endif	/* _VR3_COMPAT_CODE */
121 
122 				free((char *) win->_firstch);
123 				free((char *) win->_y);
124 #ifdef	_VR3_COMPAT_CODE
125 				if ((_y16update) && (win->_y16 != NULL))
126 					free((char *) win->_y16);
127 #endif	/* _VR3_COMPAT_CODE */
128 				free((char *) win);
129 				return (ERR);
130 			}
131 	}
132 	return (OK);
133 }
134