1 /****************************************************************************
2 * Copyright 2018-2020,2021 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
4 * *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
12 * *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
15 * *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
27 * authorization. *
28 ****************************************************************************/
29
30 /****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 * and: Thomas E. Dickey 1996-on *
34 * and: Juergen Pfeifer 2009 *
35 ****************************************************************************/
36
37 /*
38 * Terminal setup routines common to termcap and terminfo:
39 *
40 * use_env(bool)
41 * use_tioctl(bool)
42 * setupterm(char *, int, int *)
43 */
44
45 #include <curses.priv.h>
46 #include <tic.h> /* for MAX_NAME_SIZE */
47
48 #if HAVE_LOCALE_H
49 #include <locale.h>
50 #endif
51
52 MODULE_ID("$Id: lib_setup.c,v 1.214 2021/09/01 23:38:12 tom Exp $")
53
54 /****************************************************************************
55 *
56 * Terminal size computation
57 *
58 ****************************************************************************/
59
60 #if HAVE_SIZECHANGE
61 # if !defined(sun) || !TERMIOS
62 # if HAVE_SYS_IOCTL_H
63 # include <sys/ioctl.h>
64 # endif
65 # endif
66 #endif
67
68 #if NEED_PTEM_H
69 /* On SCO, they neglected to define struct winsize in termios.h -- it is only
70 * in termio.h and ptem.h (the former conflicts with other definitions).
71 */
72 # include <sys/stream.h>
73 # include <sys/ptem.h>
74 #endif
75
76 #if HAVE_LANGINFO_CODESET
77 #include <langinfo.h>
78 #endif
79
80 /*
81 * SCO defines TIOCGSIZE and the corresponding struct. Other systems (SunOS,
82 * Solaris, IRIX) define TIOCGWINSZ and struct winsize.
83 */
84 #ifdef TIOCGSIZE
85 # define IOCTL_WINSIZE TIOCGSIZE
86 # define STRUCT_WINSIZE struct ttysize
87 # define WINSIZE_ROWS(n) (int)n.ts_lines
88 # define WINSIZE_COLS(n) (int)n.ts_cols
89 #else
90 # ifdef TIOCGWINSZ
91 # define IOCTL_WINSIZE TIOCGWINSZ
92 # define STRUCT_WINSIZE struct winsize
93 # define WINSIZE_ROWS(n) (int)n.ws_row
94 # define WINSIZE_COLS(n) (int)n.ws_col
95 # endif
96 #endif
97
98 /*
99 * Reduce explicit use of "cur_term" global variable.
100 */
101 #undef CUR
102 #define CUR TerminalType(termp).
103
104 /*
105 * Wrap global variables in this module.
106 */
107 #if USE_REENTRANT
108
NCURSES_EXPORT(char *)109 NCURSES_EXPORT(char *)
110 NCURSES_PUBLIC_VAR(ttytype) (void)
111 {
112 static char empty[] = "";
113 char *result = empty;
114
115 #if NCURSES_SP_FUNCS
116 if (CURRENT_SCREEN) {
117 TERMINAL *termp = TerminalOf(CURRENT_SCREEN);
118 if (termp != 0) {
119 result = TerminalType(termp).term_names;
120 }
121 }
122 #else
123 if (cur_term != 0) {
124 result = TerminalType(cur_term).term_names;
125 }
126 #endif
127 return result;
128 }
129
130 NCURSES_EXPORT(int *)
_nc_ptr_Lines(SCREEN * sp)131 _nc_ptr_Lines(SCREEN *sp)
132 {
133 return ptrLines(sp);
134 }
135
136 NCURSES_EXPORT(int)
NCURSES_PUBLIC_VAR(LINES)137 NCURSES_PUBLIC_VAR(LINES) (void)
138 {
139 return *_nc_ptr_Lines(CURRENT_SCREEN);
140 }
141
142 NCURSES_EXPORT(int *)
_nc_ptr_Cols(SCREEN * sp)143 _nc_ptr_Cols(SCREEN *sp)
144 {
145 return ptrCols(sp);
146 }
147
148 NCURSES_EXPORT(int)
NCURSES_PUBLIC_VAR(COLS)149 NCURSES_PUBLIC_VAR(COLS) (void)
150 {
151 return *_nc_ptr_Cols(CURRENT_SCREEN);
152 }
153
154 NCURSES_EXPORT(int *)
_nc_ptr_Tabsize(SCREEN * sp)155 _nc_ptr_Tabsize(SCREEN *sp)
156 {
157 return ptrTabsize(sp);
158 }
159
160 NCURSES_EXPORT(int)
NCURSES_PUBLIC_VAR(TABSIZE)161 NCURSES_PUBLIC_VAR(TABSIZE) (void)
162 {
163 return *_nc_ptr_Tabsize(CURRENT_SCREEN);
164 }
165 #else
166 NCURSES_EXPORT_VAR(char) ttytype[NAMESIZE] = "";
167 NCURSES_EXPORT_VAR(int) LINES = 0;
168 NCURSES_EXPORT_VAR(int) COLS = 0;
169 NCURSES_EXPORT_VAR(int) TABSIZE = 8;
170 #endif
171
172 #if NCURSES_EXT_FUNCS
173 NCURSES_EXPORT(int)
NCURSES_SP_NAME(set_tabsize)174 NCURSES_SP_NAME(set_tabsize) (NCURSES_SP_DCLx int value)
175 {
176 int code = OK;
177 if (value <= 0) {
178 code = ERR;
179 } else {
180 #if USE_REENTRANT
181 if (SP_PARM) {
182 SP_PARM->_TABSIZE = value;
183 } else {
184 code = ERR;
185 }
186 #else
187 (void) SP_PARM;
188 TABSIZE = value;
189 #endif
190 }
191 return code;
192 }
193
194 #if NCURSES_SP_FUNCS
195 NCURSES_EXPORT(int)
set_tabsize(int value)196 set_tabsize(int value)
197 {
198 return NCURSES_SP_NAME(set_tabsize) (CURRENT_SCREEN, value);
199 }
200 #endif
201 #endif /* NCURSES_EXT_FUNCS */
202
203 #if USE_SIGWINCH
204 /*
205 * If we have a pending SIGWINCH, set the flag in each screen.
206 */
207 NCURSES_EXPORT(int)
_nc_handle_sigwinch(SCREEN * sp)208 _nc_handle_sigwinch(SCREEN *sp)
209 {
210 SCREEN *scan;
211
212 if (_nc_globals.have_sigwinch) {
213 _nc_globals.have_sigwinch = 0;
214
215 for (each_screen(scan)) {
216 scan->_sig_winch = TRUE;
217 }
218 }
219
220 return (sp ? sp->_sig_winch : 0);
221 }
222
223 #endif
224
225 NCURSES_EXPORT(void)
NCURSES_SP_NAME(use_env)226 NCURSES_SP_NAME(use_env) (NCURSES_SP_DCLx bool f)
227 {
228 START_TRACE();
229 T((T_CALLED("use_env(%p,%d)"), (void *) SP_PARM, (int) f));
230 #if NCURSES_SP_FUNCS
231 if (IsPreScreen(SP_PARM)) {
232 SP_PARM->_use_env = f;
233 }
234 #else
235 _nc_prescreen.use_env = f;
236 #endif
237 returnVoid;
238 }
239
240 NCURSES_EXPORT(void)
NCURSES_SP_NAME(use_tioctl)241 NCURSES_SP_NAME(use_tioctl) (NCURSES_SP_DCLx bool f)
242 {
243 START_TRACE();
244 T((T_CALLED("use_tioctl(%p,%d)"), (void *) SP_PARM, (int) f));
245 #if NCURSES_SP_FUNCS
246 if (IsPreScreen(SP_PARM)) {
247 SP_PARM->use_tioctl = f;
248 }
249 #else
250 _nc_prescreen.use_tioctl = f;
251 #endif
252 returnVoid;
253 }
254
255 #if NCURSES_SP_FUNCS
256 NCURSES_EXPORT(void)
use_env(bool f)257 use_env(bool f)
258 {
259 START_TRACE();
260 T((T_CALLED("use_env(%d)"), (int) f));
261 _nc_prescreen.use_env = f;
262 returnVoid;
263 }
264
265 NCURSES_EXPORT(void)
use_tioctl(bool f)266 use_tioctl(bool f)
267 {
268 START_TRACE();
269 T((T_CALLED("use_tioctl(%d)"), (int) f));
270 _nc_prescreen.use_tioctl = f;
271 returnVoid;
272 }
273 #endif
274
275 NCURSES_EXPORT(void)
_nc_get_screensize(SCREEN * sp,TERMINAL * termp,int * linep,int * colp)276 _nc_get_screensize(SCREEN *sp,
277 #ifdef USE_TERM_DRIVER
278 TERMINAL *termp,
279 #endif
280 int *linep, int *colp)
281 /* Obtain lines/columns values from the environment and/or terminfo entry */
282 {
283 #ifdef USE_TERM_DRIVER
284 TERMINAL_CONTROL_BLOCK *TCB;
285 int my_tabsize;
286
287 assert(termp != 0 && linep != 0 && colp != 0);
288 TCB = (TERMINAL_CONTROL_BLOCK *) termp;
289
290 my_tabsize = TCB->info.tabsize;
291 TCB->drv->td_size(TCB, linep, colp);
292
293 #if USE_REENTRANT
294 if (sp != 0) {
295 sp->_TABSIZE = my_tabsize;
296 }
297 #else
298 (void) sp;
299 TABSIZE = my_tabsize;
300 #endif
301 T(("TABSIZE = %d", my_tabsize));
302 #else /* !USE_TERM_DRIVER */
303 TERMINAL *termp = cur_term;
304 int my_tabsize;
305 bool useEnv = _nc_prescreen.use_env;
306 bool useTioctl = _nc_prescreen.use_tioctl;
307
308 #ifdef EXP_WIN32_DRIVER
309 /* If we are here, then Windows console is used in terminfo mode.
310 We need to figure out the size using the console API
311 */
312 _nc_console_size(linep, colp);
313 T(("screen size: winconsole lines = %d columns = %d", *linep, *colp));
314 #else
315 /* figure out the size of the screen */
316 T(("screen size: terminfo lines = %d columns = %d", lines, columns));
317
318 *linep = (int) lines;
319 *colp = (int) columns;
320 #endif
321
322 #if NCURSES_SP_FUNCS
323 if (sp) {
324 useEnv = sp->_use_env;
325 useTioctl = sp->use_tioctl;
326 }
327 #endif
328
329 if (useEnv || useTioctl) {
330 #ifdef __EMX__
331 {
332 int screendata[2];
333 _scrsize(screendata);
334 *colp = screendata[0];
335 *linep = ((sp != 0 && sp->_filtered)
336 ? 1
337 : screendata[1]);
338 T(("EMX screen size: environment LINES = %d COLUMNS = %d",
339 *linep, *colp));
340 }
341 #endif
342 #if HAVE_SIZECHANGE
343 /* try asking the OS */
344 if (NC_ISATTY(cur_term->Filedes)) {
345 STRUCT_WINSIZE size;
346
347 errno = 0;
348 do {
349 if (ioctl(cur_term->Filedes, IOCTL_WINSIZE, &size) >= 0) {
350 *linep = ((sp != 0 && sp->_filtered)
351 ? 1
352 : WINSIZE_ROWS(size));
353 *colp = WINSIZE_COLS(size);
354 T(("SYS screen size: environment LINES = %d COLUMNS = %d",
355 *linep, *colp));
356 break;
357 }
358 } while
359 (errno == EINTR);
360 }
361 #endif /* HAVE_SIZECHANGE */
362
363 if (useEnv) {
364 int value;
365
366 if (useTioctl) {
367 /*
368 * If environment variables are used, update them.
369 */
370 if ((sp == 0 || !sp->_filtered) && _nc_getenv_num("LINES") > 0) {
371 _nc_setenv_num("LINES", *linep);
372 }
373 if (_nc_getenv_num("COLUMNS") > 0) {
374 _nc_setenv_num("COLUMNS", *colp);
375 }
376 }
377
378 /*
379 * Finally, look for environment variables.
380 *
381 * Solaris lets users override either dimension with an environment
382 * variable.
383 */
384 if ((value = _nc_getenv_num("LINES")) > 0) {
385 *linep = value;
386 T(("screen size: environment LINES = %d", *linep));
387 }
388 if ((value = _nc_getenv_num("COLUMNS")) > 0) {
389 *colp = value;
390 T(("screen size: environment COLUMNS = %d", *colp));
391 }
392 }
393
394 /* if we can't get dynamic info about the size, use static */
395 if (*linep <= 0) {
396 *linep = (int) lines;
397 }
398 if (*colp <= 0) {
399 *colp = (int) columns;
400 }
401
402 /* the ultimate fallback, assume fixed 24x80 size */
403 if (*linep <= 0) {
404 *linep = 24;
405 }
406 if (*colp <= 0) {
407 *colp = 80;
408 }
409
410 /*
411 * Put the derived values back in the screen-size caps, so
412 * tigetnum() and tgetnum() will do the right thing.
413 */
414 lines = (NCURSES_INT2) (*linep);
415 columns = (NCURSES_INT2) (*colp);
416 #if NCURSES_EXT_NUMBERS
417 #define OldNumber(termp,name) \
418 (termp)->type.Numbers[(&name - (termp)->type2.Numbers)]
419 OldNumber(termp, lines) = (short) (*linep);
420 OldNumber(termp, columns) = (short) (*colp);
421 #endif
422 }
423
424 T(("screen size is %dx%d", *linep, *colp));
425
426 if (VALID_NUMERIC(init_tabs))
427 my_tabsize = (int) init_tabs;
428 else
429 my_tabsize = 8;
430
431 #if USE_REENTRANT
432 if (sp != 0)
433 sp->_TABSIZE = my_tabsize;
434 #else
435 TABSIZE = my_tabsize;
436 #endif
437 T(("TABSIZE = %d", TABSIZE));
438 #endif /* USE_TERM_DRIVER */
439 }
440
441 #if USE_SIZECHANGE
442 NCURSES_EXPORT(void)
_nc_update_screensize(SCREEN * sp)443 _nc_update_screensize(SCREEN *sp)
444 {
445 int new_lines;
446 int new_cols;
447
448 #ifdef USE_TERM_DRIVER
449 int old_lines;
450 int old_cols;
451
452 assert(sp != 0);
453
454 CallDriver_2(sp, td_getsize, &old_lines, &old_cols);
455
456 #else
457 TERMINAL *termp = cur_term;
458 int old_lines = lines;
459 int old_cols = columns;
460 #endif
461
462 if (sp != 0) {
463 TINFO_GET_SIZE(sp, sp->_term, &new_lines, &new_cols);
464 /*
465 * See is_term_resized() and resizeterm().
466 * We're doing it this way because those functions belong to the upper
467 * ncurses library, while this resides in the lower terminfo library.
468 */
469 if (sp->_resize != 0) {
470 if ((new_lines != old_lines) || (new_cols != old_cols)) {
471 sp->_resize(NCURSES_SP_ARGx new_lines, new_cols);
472 } else if (sp->_sig_winch && (sp->_ungetch != 0)) {
473 sp->_ungetch(SP_PARM, KEY_RESIZE); /* so application can know this */
474 }
475 sp->_sig_winch = FALSE;
476 }
477 }
478 }
479 #endif /* USE_SIZECHANGE */
480
481 /****************************************************************************
482 *
483 * Terminal setup
484 *
485 ****************************************************************************/
486
487 #if NCURSES_USE_DATABASE || NCURSES_USE_TERMCAP
488 /*
489 * Return 1 if entry found, 0 if not found, -1 if database not accessible,
490 * just like tgetent().
491 */
492 int
_nc_setup_tinfo(const char * const tn,TERMTYPE2 * const tp)493 _nc_setup_tinfo(const char *const tn, TERMTYPE2 *const tp)
494 {
495 char filename[PATH_MAX];
496 int status = _nc_read_entry2(tn, filename, tp);
497
498 /*
499 * If we have an entry, force all of the cancelled strings to null
500 * pointers so we don't have to test them in the rest of the library.
501 * (The terminfo compiler bypasses this logic, since it must know if
502 * a string is cancelled, for merging entries).
503 */
504 if (status == TGETENT_YES) {
505 unsigned n;
506 for_each_boolean(n, tp) {
507 if (!VALID_BOOLEAN(tp->Booleans[n]))
508 tp->Booleans[n] = FALSE;
509 }
510 for_each_string(n, tp) {
511 if (tp->Strings[n] == CANCELLED_STRING)
512 tp->Strings[n] = ABSENT_STRING;
513 }
514 }
515 return (status);
516 }
517 #endif
518
519 /*
520 ** Take the real command character out of the CC environment variable
521 ** and substitute it in for the prototype given in 'command_character'.
522 */
523 void
_nc_tinfo_cmdch(TERMINAL * termp,int proto)524 _nc_tinfo_cmdch(TERMINAL *termp, int proto)
525 {
526 char *tmp;
527
528 /*
529 * Only use the character if the string is a single character,
530 * since it is fairly common for developers to set the C compiler
531 * name as an environment variable - using the same symbol.
532 */
533 if ((tmp = getenv("CC")) != 0 && strlen(tmp) == 1) {
534 unsigned i;
535 char CC = *tmp;
536
537 for_each_string(i, &(termp->type)) {
538 for (tmp = termp->type.Strings[i]; tmp && *tmp; tmp++) {
539 if (UChar(*tmp) == proto)
540 *tmp = CC;
541 }
542 }
543 }
544 }
545
546 /*
547 * Find the locale which is in effect.
548 */
549 NCURSES_EXPORT(char *)
_nc_get_locale(void)550 _nc_get_locale(void)
551 {
552 char *env;
553 #if HAVE_LOCALE_H
554 /*
555 * This is preferable to using getenv() since it ensures that we are using
556 * the locale which was actually initialized by the application.
557 */
558 env = setlocale(LC_CTYPE, 0);
559 #else
560 if (((env = getenv("LANG")) != 0 && *env != '\0')
561 || ((env = getenv("LC_CTYPE")) != 0 && *env != '\0')
562 || ((env = getenv("LC_ALL")) != 0 && *env != '\0')) {
563 ;
564 }
565 #endif
566 T(("_nc_get_locale %s", _nc_visbuf(env)));
567 return env;
568 }
569
570 /*
571 * Check if we are running in a UTF-8 locale.
572 */
573 NCURSES_EXPORT(int)
_nc_unicode_locale(void)574 _nc_unicode_locale(void)
575 {
576 int result = 0;
577 #if defined(_NC_WINDOWS) && USE_WIDEC_SUPPORT
578 result = 1;
579 #elif HAVE_LANGINFO_CODESET
580 char *env = nl_langinfo(CODESET);
581 result = !strcmp(env, "UTF-8");
582 T(("_nc_unicode_locale(%s) ->%d", env, result));
583 #else
584 char *env = _nc_get_locale();
585 if (env != 0) {
586 if (strstr(env, ".UTF-8") != 0) {
587 result = 1;
588 T(("_nc_unicode_locale(%s) ->%d", env, result));
589 }
590 }
591 #endif
592 return result;
593 }
594
595 #define CONTROL_N(s) ((s) != 0 && strstr(s, "\016") != 0)
596 #define CONTROL_O(s) ((s) != 0 && strstr(s, "\017") != 0)
597
598 /*
599 * Check for known broken cases where a UTF-8 locale breaks the alternate
600 * character set.
601 */
602 NCURSES_EXPORT(int)
_nc_locale_breaks_acs(TERMINAL * termp)603 _nc_locale_breaks_acs(TERMINAL *termp)
604 {
605 const char *env_name = "NCURSES_NO_UTF8_ACS";
606 const char *env;
607 int value;
608 int result = 0;
609
610 T((T_CALLED("_nc_locale_breaks_acs:%d"), result));
611 if (getenv(env_name) != 0) {
612 result = _nc_getenv_num(env_name);
613 } else if ((value = tigetnum("U8")) >= 0) {
614 result = value; /* use extension feature */
615 } else if ((env = getenv("TERM")) != 0) {
616 if (strstr(env, "linux")) {
617 result = 1; /* always broken */
618 } else if (strstr(env, "screen") != 0
619 && ((env = getenv("TERMCAP")) != 0
620 && strstr(env, "screen") != 0)
621 && strstr(env, "hhII00") != 0) {
622 if (CONTROL_N(enter_alt_charset_mode) ||
623 CONTROL_O(enter_alt_charset_mode) ||
624 CONTROL_N(set_attributes) ||
625 CONTROL_O(set_attributes)) {
626 result = 1;
627 }
628 }
629 }
630 returnCode(result);
631 }
632
633 NCURSES_EXPORT(int)
TINFO_SETUP_TERM(TERMINAL ** tp,const char * tname,int Filedes,int * errret,int reuse)634 TINFO_SETUP_TERM(TERMINAL **tp,
635 const char *tname,
636 int Filedes,
637 int *errret,
638 int reuse)
639 {
640 #ifdef USE_TERM_DRIVER
641 TERMINAL_CONTROL_BLOCK *TCB = 0;
642 #endif
643 TERMINAL *termp;
644 SCREEN *sp = 0;
645 char *myname;
646 int code = ERR;
647
648 START_TRACE();
649
650 #ifdef USE_TERM_DRIVER
651 T((T_CALLED("_nc_setupterm_ex(%p,%s,%d,%p)"),
652 (void *) tp, _nc_visbuf(tname), Filedes, (void *) errret));
653
654 if (tp == 0) {
655 ret_error0(TGETENT_ERR,
656 "Invalid parameter, internal error.\n");
657 } else
658 termp = *tp;
659 #else
660 termp = cur_term;
661 T((T_CALLED("setupterm(%s,%d,%p)"), _nc_visbuf(tname), Filedes, (void *) errret));
662 #endif
663
664 if (tname == 0) {
665 tname = getenv("TERM");
666 #if defined(EXP_WIN32_DRIVER)
667 if (!VALID_TERM_ENV(tname, NO_TERMINAL)) {
668 T(("Failure with TERM=%s", NonNull(tname)));
669 ret_error0(TGETENT_ERR, "TERM environment variable not set.\n");
670 }
671 #elif defined(USE_TERM_DRIVER)
672 if (!NonEmpty(tname))
673 tname = "unknown";
674 #else
675 if (!NonEmpty(tname)) {
676 T(("Failure with TERM=%s", NonNull(tname)));
677 ret_error0(TGETENT_ERR, "TERM environment variable not set.\n");
678 }
679 #endif
680 }
681 myname = strdup(tname);
682
683 if (strlen(myname) > MAX_NAME_SIZE) {
684 ret_error(TGETENT_ERR,
685 "TERM environment must be <= %d characters.\n",
686 MAX_NAME_SIZE,
687 free(myname));
688 }
689
690 T(("your terminal name is %s", myname));
691
692 /*
693 * Allow output redirection. This is what SVr3 does. If stdout is
694 * directed to a file, screen updates go to standard error.
695 */
696 if (Filedes == STDOUT_FILENO && !NC_ISATTY(Filedes))
697 Filedes = STDERR_FILENO;
698 #if defined(EXP_WIN32_DRIVER)
699 if (Filedes != STDERR_FILENO && NC_ISATTY(Filedes))
700 _setmode(Filedes, _O_BINARY);
701 #endif
702
703 /*
704 * Check if we have already initialized to use this terminal. If so, we
705 * do not need to re-read the terminfo entry, or obtain TTY settings.
706 *
707 * This is an improvement on SVr4 curses. If an application mixes curses
708 * and termcap calls, it may call both initscr and tgetent. This is not
709 * really a good thing to do, but can happen if someone tries using ncurses
710 * with the readline library. The problem we are fixing is that when
711 * tgetent calls setupterm, the resulting Ottyb struct in cur_term is
712 * zeroed. A subsequent call to endwin uses the zeroed terminal settings
713 * rather than the ones saved in initscr. So we check if cur_term appears
714 * to contain terminal settings for the same output file as our current
715 * call - and copy those terminal settings. (SVr4 curses does not do this,
716 * however applications that are working around the problem will still work
717 * properly with this feature).
718 */
719 if (reuse
720 && (termp != 0)
721 && termp->Filedes == Filedes
722 && termp->_termname != 0
723 && !strcmp(termp->_termname, myname)
724 && _nc_name_match(TerminalType(termp).term_names, myname, "|")) {
725 T(("reusing existing terminal information and mode-settings"));
726 code = OK;
727 #ifdef USE_TERM_DRIVER
728 TCB = (TERMINAL_CONTROL_BLOCK *) termp;
729 #endif
730 } else {
731 #ifdef USE_TERM_DRIVER
732 TERMINAL_CONTROL_BLOCK *my_tcb;
733 termp = 0;
734 if ((my_tcb = typeCalloc(TERMINAL_CONTROL_BLOCK, 1)) != 0)
735 termp = &(my_tcb->term);
736 #else
737 int status;
738
739 termp = typeCalloc(TERMINAL, 1);
740 #endif
741 if (termp == 0) {
742 ret_error1(TGETENT_ERR,
743 "Not enough memory to create terminal structure.\n",
744 myname, free(myname));
745 }
746 #if HAVE_SYSCONF
747 {
748 long limit;
749 #ifdef LINE_MAX
750 limit = LINE_MAX;
751 #else
752 limit = _nc_globals.getstr_limit;
753 #endif
754 #ifdef _SC_LINE_MAX
755 if (limit < sysconf(_SC_LINE_MAX))
756 limit = sysconf(_SC_LINE_MAX);
757 #endif
758 if (_nc_globals.getstr_limit < (int) limit)
759 _nc_globals.getstr_limit = (int) limit;
760 }
761 #endif /* HAVE_SYSCONF */
762 T(("using %d for getstr limit", _nc_globals.getstr_limit));
763
764 #ifdef USE_TERM_DRIVER
765 INIT_TERM_DRIVER();
766 TCB = (TERMINAL_CONTROL_BLOCK *) termp;
767 code = _nc_globals.term_driver(TCB, myname, errret);
768 if (code == OK) {
769 termp->Filedes = (short) Filedes;
770 termp->_termname = strdup(myname);
771 } else {
772 ret_error1(errret ? *errret : TGETENT_ERR,
773 "Could not find any driver to handle terminal.\n",
774 myname, free(myname));
775 }
776 #else
777 #if NCURSES_USE_DATABASE || NCURSES_USE_TERMCAP
778 status = _nc_setup_tinfo(myname, &TerminalType(termp));
779 T(("_nc_setup_tinfo returns %d", status));
780 #else
781 T(("no database available"));
782 status = TGETENT_NO;
783 #endif
784
785 /* try fallback list if entry on disk */
786 if (status != TGETENT_YES) {
787 const TERMTYPE2 *fallback = _nc_fallback2(myname);
788
789 if (fallback) {
790 T(("found fallback entry"));
791 _nc_copy_termtype2(&(TerminalType(termp)), fallback);
792 status = TGETENT_YES;
793 }
794 }
795
796 if (status != TGETENT_YES) {
797 del_curterm(termp);
798 if (status == TGETENT_ERR) {
799 free(myname);
800 ret_error0(status, "terminals database is inaccessible\n");
801 } else if (status == TGETENT_NO) {
802 ret_error1(status, "unknown terminal type.\n",
803 myname, free(myname));
804 } else {
805 free(myname);
806 ret_error0(status, "unexpected return-code\n");
807 }
808 }
809 #if NCURSES_EXT_NUMBERS
810 _nc_export_termtype2(&termp->type, &TerminalType(termp));
811 #endif
812 #if !USE_REENTRANT
813 save_ttytype(termp);
814 #endif
815
816 termp->Filedes = (short) Filedes;
817 termp->_termname = strdup(myname);
818
819 set_curterm(termp);
820
821 if (command_character)
822 _nc_tinfo_cmdch(termp, UChar(*command_character));
823
824 /*
825 * If an application calls setupterm() rather than initscr() or
826 * newterm(), we will not have the def_prog_mode() call in
827 * _nc_setupscreen(). Do it now anyway, so we can initialize the
828 * baudrate. Also get the shell-mode so that erasechar() works.
829 */
830 if (NC_ISATTY(Filedes)) {
831 NCURSES_SP_NAME(def_shell_mode) (NCURSES_SP_ARG);
832 NCURSES_SP_NAME(def_prog_mode) (NCURSES_SP_ARG);
833 baudrate();
834 }
835 code = OK;
836 #endif
837 }
838
839 #ifdef USE_TERM_DRIVER
840 *tp = termp;
841 NCURSES_SP_NAME(set_curterm) (sp, termp);
842 TCB->drv->td_init(TCB);
843 #else
844 sp = SP;
845 #endif
846
847 /*
848 * We should always check the screensize, just in case.
849 */
850 TINFO_GET_SIZE(sp, termp, ptrLines(sp), ptrCols(sp));
851
852 if (errret)
853 *errret = TGETENT_YES;
854
855 #ifndef USE_TERM_DRIVER
856 if (generic_type) {
857 /*
858 * BSD 4.3's termcap contains mis-typed "gn" for wy99. Do a sanity
859 * check before giving up.
860 */
861 if ((VALID_STRING(cursor_address)
862 || (VALID_STRING(cursor_down) && VALID_STRING(cursor_home)))
863 && VALID_STRING(clear_screen)) {
864 ret_error1(TGETENT_YES, "terminal is not really generic.\n",
865 myname, free(myname));
866 } else {
867 del_curterm(termp);
868 ret_error1(TGETENT_NO, "I need something more specific.\n",
869 myname, free(myname));
870 }
871 } else if (hard_copy) {
872 ret_error1(TGETENT_YES, "I can't handle hardcopy terminals.\n",
873 myname, free(myname));
874 }
875 #endif
876 free(myname);
877 returnCode(code);
878 }
879
880 #ifdef USE_PTHREADS
881 /*
882 * Returns a non-null pointer unless a new screen should be allocated because
883 * no match was found in the pre-screen cache.
884 */
885 NCURSES_EXPORT(SCREEN *)
_nc_find_prescr(void)886 _nc_find_prescr(void)
887 {
888 SCREEN *result = 0;
889 PRESCREEN_LIST *p;
890 pthread_t id = GetThreadID();
891 for (p = _nc_prescreen.allocated; p != 0; p = p->next) {
892 if (p->id == id) {
893 result = p->sp;
894 break;
895 }
896 }
897 return result;
898 }
899
900 /*
901 * Tells ncurses to forget that this thread was associated with the pre-screen
902 * cache. It does not modify the pre-screen cache itself, since that is used
903 * for creating new screens.
904 */
905 NCURSES_EXPORT(void)
_nc_forget_prescr(void)906 _nc_forget_prescr(void)
907 {
908 PRESCREEN_LIST *p, *q;
909 pthread_t id = GetThreadID();
910 for (p = _nc_prescreen.allocated, q = 0; p != 0; q = p, p = p->next) {
911 if (p->id == id) {
912 if (q) {
913 q->next = p->next;
914 } else {
915 _nc_prescreen.allocated = p->next;
916 }
917 free(p);
918 break;
919 }
920 }
921 }
922 #endif /* USE_PTHREADS */
923
924 #if NCURSES_SP_FUNCS
925 /*
926 * In case of handling multiple screens, we need to have a screen before
927 * initialization in _nc_setupscreen takes place. This is to extend the
928 * substitute for some of the stuff in _nc_prescreen, especially for slk and
929 * ripoff handling which should be done per screen.
930 */
931 NCURSES_EXPORT(SCREEN *)
new_prescr(void)932 new_prescr(void)
933 {
934 SCREEN *sp;
935
936 START_TRACE();
937 T((T_CALLED("new_prescr()")));
938
939 _nc_lock_global(screen);
940 if ((sp = _nc_find_prescr()) == 0) {
941 sp = _nc_alloc_screen_sp();
942 T(("_nc_alloc_screen_sp %p", (void *) sp));
943 if (sp != 0) {
944 #ifdef USE_PTHREADS
945 PRESCREEN_LIST *p = typeCalloc(PRESCREEN_LIST, 1);
946 if (p != 0) {
947 p->id = GetThreadID();
948 p->sp = sp;
949 p->next = _nc_prescreen.allocated;
950 _nc_prescreen.allocated = p;
951 }
952 #else
953 _nc_prescreen.allocated = sp;
954 #endif
955 sp->rsp = sp->rippedoff;
956 sp->_filtered = _nc_prescreen.filter_mode;
957 sp->_use_env = _nc_prescreen.use_env;
958 #if NCURSES_NO_PADDING
959 sp->_no_padding = _nc_prescreen._no_padding;
960 #endif
961 sp->slk_format = 0;
962 sp->_slk = 0;
963 sp->_prescreen = TRUE;
964 SP_PRE_INIT(sp);
965 #if USE_REENTRANT
966 sp->_TABSIZE = _nc_prescreen._TABSIZE;
967 sp->_ESCDELAY = _nc_prescreen._ESCDELAY;
968 #endif
969 }
970 } else {
971 T(("_nc_alloc_screen_sp %p (reuse)", (void *) sp));
972 }
973 _nc_unlock_global(screen);
974 returnSP(sp);
975 }
976 #endif
977
978 #ifdef USE_TERM_DRIVER
979 /*
980 * This entrypoint is called from tgetent() to allow a special case of reusing
981 * the same TERMINAL data (see comment).
982 */
983 NCURSES_EXPORT(int)
_nc_setupterm(const char * tname,int Filedes,int * errret,int reuse)984 _nc_setupterm(const char *tname,
985 int Filedes,
986 int *errret,
987 int reuse)
988 {
989 int rc = ERR;
990 TERMINAL *termp = 0;
991
992 _nc_lock_global(prescreen);
993 START_TRACE();
994 if (TINFO_SETUP_TERM(&termp, tname, Filedes, errret, reuse) == OK) {
995 _nc_forget_prescr();
996 if (NCURSES_SP_NAME(set_curterm) (CURRENT_SCREEN_PRE, termp) != 0) {
997 rc = OK;
998 }
999 }
1000 _nc_unlock_global(prescreen);
1001 return rc;
1002 }
1003 #endif
1004
1005 /*
1006 * setupterm(termname, Filedes, errret)
1007 *
1008 * Find and read the appropriate object file for the terminal
1009 * Make cur_term point to the structure.
1010 */
1011 NCURSES_EXPORT(int)
setupterm(const char * tname,int Filedes,int * errret)1012 setupterm(const char *tname, int Filedes, int *errret)
1013 {
1014 START_TRACE();
1015 return _nc_setupterm(tname, Filedes, errret, FALSE);
1016 }
1017