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