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