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