xref: /titanic_51/usr/src/lib/libxcurses/src/libc/xcurses/initscr.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * initscr.c
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  * XCurses Library
33*7c478bd9Sstevel@tonic-gate  *
34*7c478bd9Sstevel@tonic-gate  * Copyright 1986, 1994 by Mortice Kern Systems Inc.  All rights reserved.
35*7c478bd9Sstevel@tonic-gate  *
36*7c478bd9Sstevel@tonic-gate  */
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #ifdef M_RCSID
39*7c478bd9Sstevel@tonic-gate #ifndef lint
40*7c478bd9Sstevel@tonic-gate static char rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/initscr.c 1.5 1995/09/28 16:47:03 ant Exp $";
41*7c478bd9Sstevel@tonic-gate #endif
42*7c478bd9Sstevel@tonic-gate #endif
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #include <private.h>
45*7c478bd9Sstevel@tonic-gate #include <errno.h>
46*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate static char nomem_msg[] = m_textstr(
49*7c478bd9Sstevel@tonic-gate 	3139, "Failed to allocate required memory.\n", "E"
50*7c478bd9Sstevel@tonic-gate );
51*7c478bd9Sstevel@tonic-gate static char noterm_msg[] = m_textstr(
52*7c478bd9Sstevel@tonic-gate 	202, "Unknown terminal \"%s\".\n", "E term"
53*7c478bd9Sstevel@tonic-gate );
54*7c478bd9Sstevel@tonic-gate static char dumb_msg[] = m_textstr(
55*7c478bd9Sstevel@tonic-gate 	3140, "Terminal \"%s\" has insufficent capabilities for Curses.\n",
56*7c478bd9Sstevel@tonic-gate 	"E term"
57*7c478bd9Sstevel@tonic-gate );
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate /*f
60*7c478bd9Sstevel@tonic-gate  * Initialize XCurses for use with a single terminal.  stdin and stdout
61*7c478bd9Sstevel@tonic-gate  * are used.  If a program needs an indication of error conditions,
62*7c478bd9Sstevel@tonic-gate  * so that it can continue to run in a line-oriented mode, use newterm()
63*7c478bd9Sstevel@tonic-gate  * instead.
64*7c478bd9Sstevel@tonic-gate  */
65*7c478bd9Sstevel@tonic-gate WINDOW *
66*7c478bd9Sstevel@tonic-gate initscr()
67*7c478bd9Sstevel@tonic-gate {
68*7c478bd9Sstevel@tonic-gate 	WINDOW *w;
69*7c478bd9Sstevel@tonic-gate 	SCREEN *sp;
70*7c478bd9Sstevel@tonic-gate 	int i, n, begy;
71*7c478bd9Sstevel@tonic-gate 	char *term, *err;
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate #ifdef M_CURSES_TRACE
74*7c478bd9Sstevel@tonic-gate 	__m_trace("initscr(void)");
75*7c478bd9Sstevel@tonic-gate #endif
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	errno = 0;
78*7c478bd9Sstevel@tonic-gate  	sp = newterm((char *) 0, stdout, stdin);
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	if (sp == (SCREEN *) 0) {
81*7c478bd9Sstevel@tonic-gate 		err = errno == ENOMEM ? nomem_msg : noterm_msg;
82*7c478bd9Sstevel@tonic-gate 		goto error_1;
83*7c478bd9Sstevel@tonic-gate 	}
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 	(void) set_term(sp);
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 	/* We require some form of cursor positioning and the ability to
88*7c478bd9Sstevel@tonic-gate 	 * clear the end of a line.  These abilities should be sufficient
89*7c478bd9Sstevel@tonic-gate 	 * to provide minimum full screen support.
90*7c478bd9Sstevel@tonic-gate 	 */
91*7c478bd9Sstevel@tonic-gate 	if (1 < lines
92*7c478bd9Sstevel@tonic-gate 	&& cursor_address == (char *) 0
93*7c478bd9Sstevel@tonic-gate 	&& row_address == (char *) 0
94*7c478bd9Sstevel@tonic-gate 	&& (cursor_up == (char *) 0 || cursor_down == (char *) 0)
95*7c478bd9Sstevel@tonic-gate 	&& (parm_up_cursor == (char *) 0 || parm_down_cursor == (char *) 0)) {
96*7c478bd9Sstevel@tonic-gate 		err = dumb_msg;
97*7c478bd9Sstevel@tonic-gate 		goto error_3;
98*7c478bd9Sstevel@tonic-gate 	}
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 	if ((1 < lines && cursor_address == (char *) 0)
101*7c478bd9Sstevel@tonic-gate 	&& column_address == (char *) 0
102*7c478bd9Sstevel@tonic-gate 	&& (cursor_left == (char *) 0 || cursor_right == (char *) 0)
103*7c478bd9Sstevel@tonic-gate 	&& (parm_left_cursor == (char *) 0 || parm_right_cursor == (char *)0)) {
104*7c478bd9Sstevel@tonic-gate 		err = dumb_msg;
105*7c478bd9Sstevel@tonic-gate 		goto error_3;
106*7c478bd9Sstevel@tonic-gate 	}
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	if (clr_eol == (char *) 0) {
109*7c478bd9Sstevel@tonic-gate 		err = dumb_msg;
110*7c478bd9Sstevel@tonic-gate 		goto error_3;
111*7c478bd9Sstevel@tonic-gate 	}
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 	return __m_return_pointer("initscr", stdscr);
114*7c478bd9Sstevel@tonic-gate error_3:
115*7c478bd9Sstevel@tonic-gate 	(void) delwin(stdscr);
116*7c478bd9Sstevel@tonic-gate error_2:
117*7c478bd9Sstevel@tonic-gate 	(void) endwin();
118*7c478bd9Sstevel@tonic-gate 	(void) delscreen(sp);
119*7c478bd9Sstevel@tonic-gate error_1:
120*7c478bd9Sstevel@tonic-gate 	/* newterm()/setupterm() attempts to load $TERM, else if
121*7c478bd9Sstevel@tonic-gate 	 * $TERM is not defined, the vendor's default terminal type.
122*7c478bd9Sstevel@tonic-gate 	 */
123*7c478bd9Sstevel@tonic-gate 	if ((term = getenv("TERM")) == (char *) 0)
124*7c478bd9Sstevel@tonic-gate 		term = M_TERM_NAME;
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, m_strmsg(err), term);
127*7c478bd9Sstevel@tonic-gate error_0:
128*7c478bd9Sstevel@tonic-gate 	exit(1);
129*7c478bd9Sstevel@tonic-gate 	return (0);	/* keep gcc happy */
130*7c478bd9Sstevel@tonic-gate }
131