xref: /illumos-gate/usr/src/lib/libxcurses/src/libc/xcurses/initscr.c (revision da7fc762b82ced1a0ec19a51e04cdf823187ec77)
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 /*
28  * initscr.c
29  *
30  * XCurses Library
31  *
32  * Copyright 1986, 1994 by Mortice Kern Systems Inc.  All rights reserved.
33  *
34  */
35 
36 #ifdef M_RCSID
37 #ifndef lint
38 static char rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/initscr.c 1.5 1995/09/28 16:47:03 ant Exp $";
39 #endif
40 #endif
41 
42 #include <private.h>
43 #include <errno.h>
44 #include <stdlib.h>
45 
46 static char nomem_msg[] = m_textstr(
47 	3139, "Failed to allocate required memory.\n", "E"
48 );
49 static char noterm_msg[] = m_textstr(
50 	202, "Unknown terminal \"%s\".\n", "E term"
51 );
52 static char dumb_msg[] = m_textstr(
53 	3140, "Terminal \"%s\" has insufficent capabilities for Curses.\n",
54 	"E term"
55 );
56 
57 /*f
58  * Initialize XCurses for use with a single terminal.  stdin and stdout
59  * are used.  If a program needs an indication of error conditions,
60  * so that it can continue to run in a line-oriented mode, use newterm()
61  * instead.
62  */
63 WINDOW *
64 initscr()
65 {
66 	WINDOW *w;
67 	SCREEN *sp;
68 	int i, n, begy;
69 	char *term, *err;
70 
71 #ifdef M_CURSES_TRACE
72 	__m_trace("initscr(void)");
73 #endif
74 
75 	errno = 0;
76  	sp = newterm((char *) 0, stdout, stdin);
77 
78 	if (sp == (SCREEN *) 0) {
79 		err = errno == ENOMEM ? nomem_msg : noterm_msg;
80 		goto error_1;
81 	}
82 
83 	(void) set_term(sp);
84 
85 	/* We require some form of cursor positioning and the ability to
86 	 * clear the end of a line.  These abilities should be sufficient
87 	 * to provide minimum full screen support.
88 	 */
89 	if (1 < lines
90 	&& cursor_address == (char *) 0
91 	&& row_address == (char *) 0
92 	&& (cursor_up == (char *) 0 || cursor_down == (char *) 0)
93 	&& (parm_up_cursor == (char *) 0 || parm_down_cursor == (char *) 0)) {
94 		err = dumb_msg;
95 		goto error_3;
96 	}
97 
98 	if ((1 < lines && cursor_address == (char *) 0)
99 	&& column_address == (char *) 0
100 	&& (cursor_left == (char *) 0 || cursor_right == (char *) 0)
101 	&& (parm_left_cursor == (char *) 0 || parm_right_cursor == (char *)0)) {
102 		err = dumb_msg;
103 		goto error_3;
104 	}
105 
106 	if (clr_eol == (char *) 0) {
107 		err = dumb_msg;
108 		goto error_3;
109 	}
110 
111 	return __m_return_pointer("initscr", stdscr);
112 error_3:
113 	(void) delwin(stdscr);
114 error_2:
115 	(void) endwin();
116 	(void) delscreen(sp);
117 error_1:
118 	/* newterm()/setupterm() attempts to load $TERM, else if
119 	 * $TERM is not defined, the vendor's default terminal type.
120 	 */
121 	if ((term = getenv("TERM")) == (char *) 0)
122 		term = M_TERM_NAME;
123 
124 	(void) fprintf(stderr, m_strmsg(err), term);
125 error_0:
126 	exit(1);
127 	return (0);	/* keep gcc happy */
128 }
129