xref: /illumos-gate/usr/src/lib/libcurses/screen/makenew.c (revision a61ed2ce7a86a4d6428f2a83eb4739fae945447e)
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 1997 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
41 
42 /*LINTLIBRARY*/
43 
44 #include	<stdlib.h>
45 #include	<string.h>
46 #include	<sys/types.h>
47 #include	"curses_inc.h"
48 
49 /* This routine sets up a window buffer and returns a pointer to it. */
50 
51 WINDOW	*
52 _makenew(int nlines, int ncols, int begy, int begx)
53 {
54 	/* order the register allocations against highest usage */
55 	WINDOW	*win;
56 
57 #ifdef	DEBUG
58 	if (outf)
59 		fprintf(outf, "MAKENEW(%d, %d, %d, %d)\n",
60 		    nlines, ncols, begy, begx);
61 #endif	/* DEBUG */
62 
63 	if ((win = (WINDOW *) malloc(sizeof (WINDOW))) == NULL)
64 		goto out_no_win;
65 	if ((win->_y = (chtype **) malloc(nlines * sizeof (chtype *))) == NULL)
66 		goto out_win;
67 #ifdef	_VR3_COMPAT_CODE
68 	if ((_y16update) && ((win->_y16 = (_ochtype **)
69 	    calloc(1, nlines * sizeof (_ochtype *))) == NULL)) {
70 		goto out_y16;
71 	}
72 #endif	/* _VR3_COMPAT_CODE */
73 	if ((win->_firstch = (short *) malloc(2 * nlines * sizeof (short)))
74 	    == NULL) {
75 #ifdef	_VR3_COMPAT_CODE
76 		if ((_y16update) && (win->_y16 != NULL))
77 			free((char *) win->_y16);
78 out_y16:
79 #endif	/* _VR3_COMPAT_CODE */
80 		free((char *) win->_y);
81 out_win:
82 		free((char *) win);
83 out_no_win:
84 		curs_errno = CURS_BAD_MALLOC;
85 #ifdef	DEBUG
86 		strcpy(curs_parm_err, "_makenew");
87 #endif	/* DEBUG */
88 		return ((WINDOW *) NULL);
89 	} else
90 		win->_lastch = win->_firstch + nlines;
91 
92 	win->_cury = win->_curx = 0;
93 	/*LINTED*/
94 	win->_maxy = (short) nlines;
95 	/*LINTED*/
96 	win->_maxx = (short) ncols;
97 	/*LINTED*/
98 	win->_begy = (short) begy;
99 	/*LINTED*/
100 	win->_begx = (short) begx;
101 	win->_clear = (((begy + SP->Yabove + begx) == 0) &&
102 	    (nlines >= (LINES + SP->Yabove)) && (ncols >= COLS));
103 	win->_leave = win->_scroll = win->_use_idl = win->_use_keypad =
104 	    win->_notimeout = win->_immed = win->_sync = FALSE;
105 	win->_use_idc = TRUE;
106 	win->_ndescs = win->_tmarg = 0;
107 	win->_bmarg = nlines - 1;
108 	win->_bkgd = _BLNKCHAR;
109 	win->_delay = win->_parx = win->_pary = -1;
110 	win->_attrs = A_NORMAL;
111 	win->_flags = _WINCHANGED;
112 	win->_parent = win->_padwin = (WINDOW *) NULL;
113 	(void) memset((char *) win->_firstch, 0, (nlines * sizeof (short)));
114 	{
115 		short	*lastch = win->_lastch,
116 			*elastch = lastch + nlines;
117 
118 		ncols--;
119 		while (lastch < elastch)
120 		    /*LINTED*/
121 		    *lastch++ = (short) ncols;
122 	}
123 
124 	win->_insmode = FALSE;
125 	win->_index = 0;
126 	win->_nbyte = -1;
127 
128 #ifdef	DEBUG
129 	if (outf) {
130 		fprintf(outf, "MAKENEW: win->_clear = %d\n", win->_clear);
131 		fprintf(outf, "MAKENEW: win->_flags = %0.2o\n", win->_flags);
132 		fprintf(outf, "MAKENEW: win->_maxy = %d\n", win->_maxy);
133 		fprintf(outf, "MAKENEW: win->_maxx = %d\n", win->_maxx);
134 		fprintf(outf, "MAKENEW: win->_begy = %d\n", win->_begy);
135 		fprintf(outf, "MAKENEW: win->_begx = %d\n", win->_begx);
136 	}
137 #endif	/* DEBUG */
138 	return (win);
139 }
140