xref: /titanic_52/usr/src/lib/libcurses/screen/newscreen.c (revision 69a119caa6570c7077699161b7c28b6ee9f8b0f4)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 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 /*LINTLIBRARY*/
41 
42 #include	<stdio.h>
43 #include	<stdlib.h>
44 #include	<string.h>
45 #include	<sys/types.h>
46 #include	"curses_inc.h"
47 
48 #ifdef	_VR2_COMPAT_CODE
49 extern	char	_endwin;
50 #endif	/* _VR2_COMPAT_CODE */
51 
52 /* 1200 is put at the 0th location since 0 is probably a mistake. */
53 static long    baud_convert[] = {
54 		    1200, 50, 75, 110, 135, 150, 200, 300, 600, 1200,
55 		    1800, 2400, 4800, 9600, 19200, 38400, 57600, 76800,
56 		    115200, 153600, 230400, 307200, 460800, 921600
57 		};
58 
59 static	char	isfilter = 0;
60 static	int	_chk_trm(void);
61 static	void	_forget(void);
62 
63 /*
64  * newscreen sets up a terminal and returns a pointer to the terminal
65  * structure or NULL in case of an error.  The parameters are:
66  *	type: terminal type
67  *	lsize, csize, tabsize: physical sizes
68  *	infptr, outfptr: input and output stdio stream file pointers
69  */
70 
71 SCREEN	*
72 newscreen(char *type, int lsize, int csize, int tabsize,
73 	FILE *outfptr, FILE *infptr)
74 {
75 	int		old_lines = LINES, old_cols = COLS, retcode;
76 #ifndef	_IOFBF
77 	char	*sobuf;
78 #endif	/* _IOBUF */
79 	WINDOW	*old_curscr = curscr;
80 	SCREEN	*old = SP;
81 	TERMINAL	*old_term = cur_term;
82 
83 #ifdef	DEBUG
84 	if (outf == NULL) {
85 		outf = fopen("trace", "w");
86 		if (outf == NULL) {
87 			perror("trace");
88 			exit(-1);
89 		}
90 		setbuf(outf, (char *)NULL);
91 	}
92 
93 	if (outf)
94 		fprintf(outf, "NEWTERM(type=%s, outfptr=%x %d, infptr=%x %d) "
95 		    "isatty(2) %d, getenv %s\n", type, outfptr,
96 		    fileno(outfptr), infptr, fileno(infptr), isatty(2),
97 		    getenv("TERM"));
98 #endif	/* DEBUG */
99 
100 
101 	/* read in terminfo file */
102 
103 	if (setupterm(type, fileno(outfptr), &retcode) != 0)
104 		goto err2;
105 
106 	/* the max length of a multi-byte character */
107 	_csmax = (cswidth[0] > cswidth[1]+1 ?
108 	    (cswidth[0] > cswidth[2]+1 ? cswidth[0] : cswidth[2]+1) :
109 	    (cswidth[1] > cswidth[2] ? cswidth[1]+1 : cswidth[2]+1));
110 	if (_csmax > CSMAX)
111 		goto err2;
112 	/* the max length of a multi-column character */
113 	_scrmax = _curs_scrwidth[0] > _curs_scrwidth[1] ?
114 	    (_curs_scrwidth[0] > _curs_scrwidth[2] ? _curs_scrwidth[0] :
115 	    _curs_scrwidth[2]) : (_curs_scrwidth[1] > _curs_scrwidth[2] ?
116 	    _curs_scrwidth[1] : _curs_scrwidth[2]);
117 	/* true multi-byte/multi-column case */
118 	_mbtrue = (_csmax > 1 || _scrmax > 1);
119 
120 	if ((curs_errno = _chk_trm()) != -1) {
121 		(void) strcpy(curs_parm_err, cur_term->_termname);
122 		goto err2;
123 	}
124 
125 	/* use calloc because almost everything needs to be zero */
126 	if ((SP = (SCREEN *) calloc(1, sizeof (SCREEN))) == NULL)
127 		goto err1;
128 
129 	SP->term_file = outfptr;
130 	SP->input_file = infptr;
131 
132 	/*
133 	 * The default is echo, for upward compatibility, but we do
134 	 * all echoing in curses to avoid problems with the tty driver
135 	 * echoing things during critical sections.
136 	 */
137 
138 	SP->fl_echoit = 1;
139 
140 	/* set some fields for cur_term structure */
141 
142 	(void) typeahead(fileno(infptr));
143 	(void) tinputfd(fileno(infptr));
144 
145 	/*
146 	 * We use LINES instead of the SP variable and a local variable because
147 	 * slk_init and rip_init update the LINES value and application code
148 	 * may look at the value of LINES in the function called by rip_init.
149 	 */
150 
151 	/* LINTED */
152 	LINES = SP->lsize = lsize > 0 ? lsize : lines;
153 
154 	/* force the output to be buffered */
155 #ifdef	_IOFBF
156 	(void) setvbuf(outfptr, (char *)NULL, _IOFBF, 0);
157 #else	/* _IOFBF */
158 	if ((sobuf = malloc(BUFSIZ)) == NULL) {
159 		curs_errno = CURS_BAD_MALLOC;
160 #ifdef	DEBUG
161 		strcpy(curs_parm_err, "newscreen");
162 #endif	/* DEBUG */
163 	}
164 	setbuf(outfptr, sobuf);
165 #endif	/* _IOFBF */
166 
167 #ifdef	SYSV
168 	SP->baud = baud_convert[_BRS(PROGTTYS)];
169 #else	/* SYSV */
170 	SP->baud = baud_convert[_BR(PROGTTY)];
171 #endif	/* SYSV */
172 
173 	/* figure out how much each terminal capability costs */
174 	_init_costs();
175 
176 	/* initialize the array of alternate characters */
177 	(void) init_acs();
178 
179 	SP->tcap = cur_term;
180 
181 	/* set tty settings to something reasonable for us */
182 #ifdef	SYSV
183 	PROGTTYS.c_lflag &= ~ECHO;
184 	PROGTTYS.c_lflag |= ISIG;
185 	PROGTTYS.c_oflag &= ~(OCRNL|ONLCR); /* why would anyone set OCRNL? */
186 #else	/* SYSV */
187 	PROGTTY.sg_flags &= ~(RAW|ECHO|CRMOD);
188 #endif	/* SYSV */
189 
190 	(void) cbreak();
191 
192 	/* LINTED */
193 	COLS = SP->csize = csize > 0 ? csize : columns;
194 	if (tabsize == 0)
195 		tabsize = (init_tabs == -1) ? 8 : init_tabs;
196 	/* LINTED */
197 	SP->tsize = (short)tabsize;
198 #ifdef	DEBUG
199 	if (outf)
200 		fprintf(outf, "LINES = %d, COLS = %d\n", LINES, COLS);
201 #endif	/* DEBUG */
202 
203 	if ((curscr = SP->cur_scr = newwin(LINES, COLS, 0, 0)) == NULL)
204 		goto err;
205 
206 	SP->fl_endwin = 2;
207 #ifdef	_VR2_COMPAT_CODE
208 	_endwin = FALSE;
209 #endif	/* _VR2_COMPAT_CODE */
210 	curscr->_sync = TRUE;
211 
212 	/*
213 	 * This will tell _quick_echo(if it's ever called), whether
214 	 * _quick_echo should let wrefresh handle everything.
215 	 */
216 
217 	if (ceol_standout_glitch || (magic_cookie_glitch >= 0) ||
218 	    tilde_glitch || (transparent_underline && erase_overstrike)) {
219 		curscr->_flags |= _CANT_BE_IMMED;
220 	}
221 	if (!(SP->virt_scr = newwin(LINES, COLS, 0, 0)))
222 		goto err;
223 	_virtscr = SP->virt_scr;
224 
225 	SP->virt_scr->_clear = FALSE;
226 
227 	/* video mark map for cookie terminals */
228 
229 	if (ceol_standout_glitch || (magic_cookie_glitch >= 0)) {
230 		int	i, nc;
231 		char	**marks;
232 
233 		if ((marks = (char **)calloc((unsigned)LINES,
234 		    sizeof (char *))) == NULL)
235 			goto err;
236 		SP->_mks = marks;
237 		nc = (COLS / BITSPERBYTE) + (COLS % BITSPERBYTE ? 1 : 0);
238 		if ((*marks = (char *)calloc((unsigned)nc * LINES,
239 		    sizeof (char))) == NULL)
240 			goto err;
241 		for (i = LINES - 1; i-- > 0; ++marks)
242 			*(marks + 1) = *marks + nc;
243 	}
244 
245 	/* hash tables for lines */
246 	if ((SP->cur_hash = (int *)calloc((unsigned)2 * LINES,
247 	    sizeof (int))) == NULL)
248 		goto err;
249 	SP->virt_hash = SP->cur_hash + LINES;
250 
251 	/* adjust the screen size if soft labels and/or ripoffline are used */
252 	if (_slk_init)
253 		(*_slk_init)();
254 	if (_rip_init)
255 		(*_rip_init)();
256 
257 	if ((SP->std_scr = newwin(LINES, COLS, 0, 0)) == NULL) {
258 		/* free all the storage allocated above and return NULL */
259 err:
260 		delscreen(SP);
261 		COLS = old_cols;
262 		curscr = old_curscr;
263 		LINES = old_lines;
264 err1:
265 		SP = old;
266 
267 		curs_errno = CURS_BAD_MALLOC;
268 #ifdef	DEBUG
269 		strcpy(curs_parm_err, "newscreen");
270 #endif	/* DEBUG */
271 
272 err2:
273 		cur_term = old_term;
274 		return (NULL);
275 	}
276 #ifdef	DEBUG
277 	if (outf)
278 		fprintf(outf, "SP %x, stdscr %x, curscr %x\n",
279 		    SP, SP->std_scr, curscr);
280 #endif	/* DEBUG */
281 
282 	if (((SP->imode = (enter_insert_mode && exit_insert_mode)) != 0) &&
283 	    ((SP->dmode = (enter_delete_mode && exit_delete_mode)) != 0)) {
284 		if (strcmp(enter_insert_mode, enter_delete_mode) == 0)
285 			SP->sid_equal = TRUE;
286 		if (strcmp(exit_insert_mode, exit_delete_mode) == 0)
287 			SP->eid_equal = TRUE;
288 	}
289 	SP->ichok = (SP->imode || insert_character || parm_ich);
290 	SP->dchok = (delete_character || parm_dch);
291 
292 	stdscr = SP->std_scr;
293 	TABSIZE = SP->tsize;
294 
295 	return (SP);
296 }
297 
298 /*
299  * check if terminal have capabilities to do basic cursor movements and
300  * screen clearing
301  */
302 static int
303 _chk_trm(void)
304 {
305 	short	error_num = -1;
306 #ifdef	DEBUG
307 	if (outf)
308 		fprintf(outf, "chk_trm().\n");
309 #endif	/* DEBUG */
310 
311 	if (generic_type)
312 		error_num = CURS_UNKNOWN;
313 	else {
314 		if (isfilter) {
315 			_forget();
316 			/* Only need to move left or right on current line */
317 			if (!(cursor_left || carriage_return ||
318 			    column_address || parm_left_cursor)) {
319 				goto out_stupid;
320 			}
321 		} else {
322 			if ((hard_copy || over_strike) ||
323 			/* some way to move up, down, left */
324 			    (!(cursor_address) &&
325 			    (!((cursor_up || cursor_home) && cursor_down &&
326 			    (cursor_left || carriage_return)))) ||
327 			    (!clear_screen)) {
328 out_stupid:
329 				error_num = CURS_STUPID;
330 			}
331 		}
332 	}
333 	return (error_num);
334 }
335 
336 int
337 filter(void)
338 {
339 	isfilter = 1;
340 	return (OK);
341 }
342 
343 /*
344  * if (for some reason) user assumes that terminal has only one line,
345  * disable all capabilities that deal with non-horizontal cursor movement
346  */
347 static void
348 _forget(void)
349 {
350 	row_address = cursor_address = clear_screen = parm_down_cursor =
351 	    cursor_up = cursor_down = NULL;
352 	cursor_home = carriage_return;
353 	lines = 1;
354 }
355