1 /****************************************************************************
2 * Copyright 2018-2023,2024 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
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: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
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. *
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 *
28 ****************************************************************************/
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 ****************************************************************************/
38 * Terminal setup routines common to termcap and terminfo:
42 * setupterm(char *, int, int *)
45 #include <curses.priv.h>
46 #include <tic.h> /* for MAX_NAME_SIZE */
52 MODULE_ID("$Id: lib_setup.c,v 1.233 2024/03/16 23:39:28 tom Exp $")
54 /****************************************************************************
56 * Terminal size computation
58 ****************************************************************************/
61 # if !defined(sun) || !TERMIOS
63 # include <sys/ioctl.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).
72 # include <sys/stream.h>
73 # include <sys/ptem.h>
76 #if HAVE_LANGINFO_CODESET
81 * SCO defines TIOCGSIZE and the corresponding struct. Other systems (SunOS,
82 * Solaris, IRIX) define TIOCGWINSZ and struct winsize.
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
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
99 * Reduce explicit use of "cur_term" global variable.
102 #define CUR TerminalType(termp).
105 * Wrap global variables in this module.
109 NCURSES_EXPORT(char *)
110 NCURSES_PUBLIC_VAR(ttytype) (void)
112 static char empty[] = "";
113 char *result = empty;
116 if (CURRENT_SCREEN) {
117 TERMINAL *termp = TerminalOf(CURRENT_SCREEN);
119 result = TerminalType(termp).term_names;
124 result = TerminalType(cur_term).term_names;
130 NCURSES_EXPORT(int *)
131 _nc_ptr_Lines(SCREEN *sp)
137 NCURSES_PUBLIC_VAR(LINES) (void)
139 return *_nc_ptr_Lines(CURRENT_SCREEN);
142 NCURSES_EXPORT(int *)
143 _nc_ptr_Cols(SCREEN *sp)
149 NCURSES_PUBLIC_VAR(COLS) (void)
151 return *_nc_ptr_Cols(CURRENT_SCREEN);
154 NCURSES_EXPORT(int *)
155 _nc_ptr_Tabsize(SCREEN *sp)
157 return ptrTabsize(sp);
161 NCURSES_PUBLIC_VAR(TABSIZE) (void)
163 return *_nc_ptr_Tabsize(CURRENT_SCREEN);
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;
172 #if NCURSES_EXT_FUNCS
174 NCURSES_SP_NAME(set_tabsize) (NCURSES_SP_DCLx int value)
182 SP_PARM->_TABSIZE = value;
196 set_tabsize(int value)
198 return NCURSES_SP_NAME(set_tabsize) (CURRENT_SCREEN, value);
201 #endif /* NCURSES_EXT_FUNCS */
205 * If we have a pending SIGWINCH, set the flag in each screen.
208 _nc_handle_sigwinch(SCREEN *sp)
212 if (_nc_globals.have_sigwinch) {
213 _nc_globals.have_sigwinch = 0;
215 for (each_screen(scan)) {
216 scan->_sig_winch = TRUE;
220 return (sp ? sp->_sig_winch : 0);
226 NCURSES_SP_NAME(use_env) (NCURSES_SP_DCLx bool f)
229 T((T_CALLED("use_env(%p,%d)"), (void *) SP_PARM, (int) f));
231 if (IsPreScreen(SP_PARM)) {
232 SP_PARM->_use_env = f;
235 _nc_prescreen.use_env = f;
241 NCURSES_SP_NAME(use_tioctl) (NCURSES_SP_DCLx bool f)
244 T((T_CALLED("use_tioctl(%p,%d)"), (void *) SP_PARM, (int) f));
246 if (IsPreScreen(SP_PARM)) {
247 SP_PARM->use_tioctl = f;
250 _nc_prescreen.use_tioctl = f;
260 T((T_CALLED("use_env(%d)"), (int) f));
261 _nc_prescreen.use_env = f;
269 T((T_CALLED("use_tioctl(%d)"), (int) f));
270 _nc_prescreen.use_tioctl = f;
275 #if !(defined(USE_TERM_DRIVER) || defined(EXP_WIN32_DRIVER))
277 _nc_default_screensize(TERMINAL *termp, int *linep, int *colp)
279 /* if we can't get dynamic info about the size, use static */
281 *linep = (int) lines;
284 *colp = (int) columns;
287 /* the ultimate fallback, assume fixed 24x80 size */
296 #if defined(USE_CHECK_SIZE) && defined(user6) && defined(user7)
298 skip_csi(const char *value)
300 if (UChar(*value) == CSI_CHR) {
302 } else if (*value == ESC_CHR && value[1] == L_BLOCK) {
309 is_expected(const char *value, const char *expected)
312 if (VALID_STRING(value)) {
313 const char *skipped = skip_csi(value);
314 if (skipped != value) {
315 if (!strcmp(skipped, expected))
323 get_position(TERMINAL *termp, int fd, int *row, int *col)
326 size_t need = strlen(user7);
329 have = (int) write(fd, user7, need);
331 if (have == (int) need) {
339 s = memset(buf, '\0', sizeof(buf));
341 size_t ask = (sizeof(buf) - 1 - (size_t) (s - buf));
342 int got = (int) read(fd, s, ask);
347 } while (strchr(buf, 'R') == NULL && (size_t) (s + 1 - buf) < sizeof(buf));
348 T(("CPR response %s", _nc_visbuf(buf)));
349 skipped = skip_csi(buf);
353 && (scanned = sscanf(skip_csi(buf), "%d;%d%c", &y, &x, &cc)) == 3
360 T(("get_position %s %d,%d", result ? "OK" : "ERR", *row, *col));
365 set_position(TERMINAL *termp, int fd, int row, int col)
368 char *actual = TIPARM_2(cursor_address, row, col);
369 T(("set_position %d,%d", row, col));
370 if (actual != NULL) {
371 size_t want = strlen(actual);
372 int have = (int) write(fd, actual, want); /* FIXME - padding */
373 result = ((int) want == have);
379 * This is a little more complicated than one might expect, because we do this
380 * before setting up the terminal modes, etc., and cannot use the timeout or
381 * buffering functions.
383 * We check if the terminal description has the ECMA-48 CPR (cursor position
384 * report) in u7 and the response in u6. The two variations of is_expected()
385 * cover the termcap style and terminfo style, and are equivalent as far as we
386 * are concerned. For analyzing the response, we wait (a short time) for 'R'
387 * to be echoed, and then check if we received two integers in the response.
389 * In principle, this could run on "any" ECMA-48 terminal, but in practice,
390 * there is a scenario using GNU screen where it uses ncurses with a partially
391 * configured pseudo-terminal, and the CPR response goes to the wrong place.
392 * So we do a simple check to exclude pseudo-terminals.
395 _nc_check_screensize(TERMINAL *termp, int *linep, int *colp)
397 int fd = termp->Filedes;
402 && (name = ttyname(fd)) != NULL
403 && strncmp(name, "/dev/pts/", 9)
404 && VALID_STRING(cursor_address)
405 && is_expected(user7, "6n")
406 && (is_expected(user6, "%i%d;%dR") ||
407 is_expected(user6, "%i%p1%d;%p2%dR"))
408 && GET_TTY(fd, &saved) == OK) {
409 int current_y = -1, current_x = -1;
410 int updated_y = -1, updated_x = -1;
413 T(("checking screensize of %s", name));
414 alter.c_lflag &= (unsigned) ~(ECHO | ICANON | ISIG | IEXTEN);
415 alter.c_iflag &= (unsigned) ~(IXON | BRKINT | PARMRK);
416 alter.c_cc[VMIN] = 0;
417 alter.c_cc[VTIME] = 1;
420 if (get_position(termp, fd, ¤t_y, ¤t_x)
421 && set_position(termp, fd, 9999, 9999)
422 && get_position(termp, fd, &updated_y, &updated_x)) {
425 set_position(termp, fd, current_y, current_x);
427 /* restore tty modes */
431 _nc_default_screensize(termp, linep, colp);
433 #else /* !USE_CHECK_SIZE */
434 #define _nc_check_screensize(termp, linep, colp) /* nothing */
436 #endif /* !(defined(USE_TERM_DRIVER) || defined(EXP_WIN32_DRIVER)) */
439 _nc_get_screensize(SCREEN *sp,
440 #ifdef USE_TERM_DRIVER
443 int *linep, int *colp)
444 /* Obtain lines/columns values from the environment and/or terminfo entry */
446 #ifdef USE_TERM_DRIVER
447 TERMINAL_CONTROL_BLOCK *TCB;
450 assert(termp != 0 && linep != 0 && colp != 0);
451 TCB = (TERMINAL_CONTROL_BLOCK *) termp;
453 my_tabsize = TCB->info.tabsize;
454 TCB->drv->td_size(TCB, linep, colp);
458 sp->_TABSIZE = my_tabsize;
462 TABSIZE = my_tabsize;
464 T(("TABSIZE = %d", my_tabsize));
465 #else /* !USE_TERM_DRIVER */
466 TERMINAL *termp = cur_term;
468 bool useEnv = _nc_prescreen.use_env;
469 bool useTioctl = _nc_prescreen.use_tioctl;
471 T((T_CALLED("_nc_get_screensize (%p)"), sp));
472 #ifdef EXP_WIN32_DRIVER
473 /* If we are here, then Windows console is used in terminfo mode.
474 We need to figure out the size using the console API
476 _nc_console_size(linep, colp);
477 T(("screen size: winconsole lines = %d columns = %d", *linep, *colp));
479 /* figure out the size of the screen */
480 T(("screen size: terminfo lines = %d columns = %d", lines, columns));
482 *linep = (int) lines;
483 *colp = (int) columns;
488 useEnv = sp->_use_env;
489 useTioctl = sp->use_tioctl;
493 T(("useEnv:%d useTioctl:%d", useEnv, useTioctl));
494 if (useEnv || useTioctl) {
498 _scrsize(screendata);
499 *colp = screendata[0];
500 *linep = ((sp != 0 && sp->_filtered)
503 T(("EMX screen size: environment LINES = %d COLUMNS = %d",
508 /* try asking the OS */
509 if (NC_ISATTY(cur_term->Filedes)) {
514 if (ioctl(cur_term->Filedes, IOCTL_WINSIZE, &size) >= 0) {
515 *linep = ((sp != 0 && sp->_filtered)
517 : WINSIZE_ROWS(size));
518 *colp = WINSIZE_COLS(size);
519 T(("SYS screen size: environment LINES = %d COLUMNS = %d",
526 #endif /* HAVE_SIZECHANGE */
533 * If environment variables are used, update them.
535 if ((sp == 0 || !sp->_filtered) && _nc_getenv_num("LINES") > 0) {
536 _nc_setenv_num("LINES", *linep);
538 if (_nc_getenv_num("COLUMNS") > 0) {
539 _nc_setenv_num("COLUMNS", *colp);
544 * Finally, look for environment variables.
546 * Solaris lets users override either dimension with an environment
549 if ((value = _nc_getenv_num("LINES")) > 0) {
551 T(("screen size: environment LINES = %d", *linep));
553 if ((value = _nc_getenv_num("COLUMNS")) > 0) {
555 T(("screen size: environment COLUMNS = %d", *colp));
558 _nc_default_screensize(termp, linep, colp);
560 _nc_check_screensize(termp, linep, colp);
564 * Put the derived values back in the screen-size caps, so
565 * tigetnum() and tgetnum() will do the right thing.
567 lines = (NCURSES_INT2) (*linep);
568 columns = (NCURSES_INT2) (*colp);
569 #if NCURSES_EXT_NUMBERS
570 #define OldNumber(termp,name) \
571 (termp)->type.Numbers[(&name - (termp)->type2.Numbers)]
572 OldNumber(termp, lines) = (short) (*linep);
573 OldNumber(termp, columns) = (short) (*colp);
576 _nc_check_screensize(termp, linep, colp);
579 T(("screen size is %dx%d", *linep, *colp));
581 if (VALID_NUMERIC(init_tabs))
582 my_tabsize = (int) init_tabs;
588 sp->_TABSIZE = my_tabsize;
590 TABSIZE = my_tabsize;
592 T(("TABSIZE = %d", TABSIZE));
594 #endif /* USE_TERM_DRIVER */
599 _nc_update_screensize(SCREEN *sp)
604 #ifdef USE_TERM_DRIVER
610 CallDriver_2(sp, td_getsize, &old_lines, &old_cols);
613 TERMINAL *termp = cur_term;
614 int old_lines = lines;
615 int old_cols = columns;
619 TINFO_GET_SIZE(sp, sp->_term, &new_lines, &new_cols);
621 * See is_term_resized() and resizeterm().
622 * We're doing it this way because those functions belong to the upper
623 * ncurses library, while this resides in the lower terminfo library.
625 if (sp->_resize != 0) {
626 if ((new_lines != old_lines) || (new_cols != old_cols)) {
627 sp->_resize(NCURSES_SP_ARGx new_lines, new_cols);
628 } else if (sp->_sig_winch && (sp->_ungetch != 0)) {
629 sp->_ungetch(SP_PARM, KEY_RESIZE); /* so application can know this */
631 sp->_sig_winch = FALSE;
635 #endif /* USE_SIZECHANGE */
637 /****************************************************************************
641 ****************************************************************************/
643 #if NCURSES_USE_DATABASE || NCURSES_USE_TERMCAP
645 * Return 1 if entry found, 0 if not found, -1 if database not accessible,
646 * just like tgetent().
649 _nc_setup_tinfo(const char *const tn, TERMTYPE2 *const tp)
651 char filename[PATH_MAX];
652 int status = _nc_read_entry2(tn, filename, tp);
655 * If we have an entry, force all of the cancelled strings to null
656 * pointers so we don't have to test them in the rest of the library.
657 * (The terminfo compiler bypasses this logic, since it must know if
658 * a string is cancelled, for merging entries).
660 if (status == TGETENT_YES) {
662 T(("_nc_setup_tinfo - resetting invalid booleans/strings"));
663 for_each_boolean(n, tp) {
664 if (!VALID_BOOLEAN(tp->Booleans[n]))
665 tp->Booleans[n] = FALSE;
667 for_each_string(n, tp) {
668 if (tp->Strings[n] == CANCELLED_STRING)
669 tp->Strings[n] = ABSENT_STRING;
677 * Take the real command character out of the CC environment variable
678 * and substitute it in for the prototype given in 'command_character'.
681 _nc_tinfo_cmdch(TERMINAL *termp, int proto)
686 * Only use the character if the string is a single character,
687 * since it is fairly common for developers to set the C compiler
688 * name as an environment variable - using the same symbol.
690 if ((tmp = getenv("CC")) != 0 && strlen(tmp) == 1) {
694 for_each_string(i, &(termp->type)) {
695 tmp = termp->type.Strings[i];
696 if (VALID_STRING(tmp)) {
697 for (; *tmp; ++tmp) {
698 if (UChar(*tmp) == proto)
707 * Find the locale which is in effect.
709 NCURSES_EXPORT(char *)
715 * This is preferable to using getenv() since it ensures that we are using
716 * the locale which was actually initialized by the application.
718 env = setlocale(LC_CTYPE, 0);
720 if (((env = getenv("LANG")) != 0 && *env != '\0')
721 || ((env = getenv("LC_CTYPE")) != 0 && *env != '\0')
722 || ((env = getenv("LC_ALL")) != 0 && *env != '\0')) {
726 T(("_nc_get_locale %s", _nc_visbuf(env)));
731 * Check if we are running in a UTF-8 locale.
734 _nc_unicode_locale(void)
736 static bool initialized = FALSE;
737 static int result = 0;
740 #if defined(_NC_WINDOWS) && USE_WIDEC_SUPPORT
742 #elif HAVE_LANGINFO_CODESET
743 char *env = nl_langinfo(CODESET);
744 result = !strcmp(env, "UTF-8");
745 T(("_nc_unicode_locale(%s) ->%d", env, result));
747 char *env = _nc_get_locale();
749 if (strstr(env, ".UTF-8") != 0) {
751 T(("_nc_unicode_locale(%s) ->%d", env, result));
760 #define CONTROL_N(s) ((s) != 0 && strstr(s, "\016") != 0)
761 #define CONTROL_O(s) ((s) != 0 && strstr(s, "\017") != 0)
764 * Check for known broken cases where a UTF-8 locale breaks the alternate
768 _nc_locale_breaks_acs(TERMINAL *termp)
770 const char *env_name = "NCURSES_NO_UTF8_ACS";
775 T((T_CALLED("_nc_locale_breaks_acs:%d"), result));
776 if (getenv(env_name) != 0) {
777 result = _nc_getenv_num(env_name);
778 } else if ((value = tigetnum("U8")) >= 0) {
779 result = value; /* use extension feature */
780 } else if ((env = getenv("TERM")) != 0) {
781 if (strstr(env, "linux")) {
782 result = 1; /* always broken */
783 } else if (strstr(env, "screen") != 0
784 && ((env = getenv("TERMCAP")) != 0
785 && strstr(env, "screen") != 0)
786 && strstr(env, "hhII00") != 0) {
787 if (CONTROL_N(enter_alt_charset_mode) ||
788 CONTROL_O(enter_alt_charset_mode) ||
789 CONTROL_N(set_attributes) ||
790 CONTROL_O(set_attributes)) {
799 TINFO_SETUP_TERM(TERMINAL **tp,
805 #ifdef USE_TERM_DRIVER
806 TERMINAL_CONTROL_BLOCK *TCB = 0;
815 #ifdef USE_TERM_DRIVER
816 T((T_CALLED("_nc_setupterm_ex(%p,%s,%d,%p)"),
817 (void *) tp, _nc_visbuf(tname), Filedes, (void *) errret));
820 ret_error0(TGETENT_ERR,
821 "Invalid parameter, internal error.\n");
826 T((T_CALLED("setupterm(%s,%d,%p)"), _nc_visbuf(tname), Filedes, (void *) errret));
830 tname = getenv("TERM");
831 #if defined(EXP_WIN32_DRIVER)
832 if (!VALID_TERM_ENV(tname, NO_TERMINAL)) {
833 T(("Failure with TERM=%s", NonNull(tname)));
834 ret_error0(TGETENT_ERR, "TERM environment variable not set.\n");
836 #elif defined(USE_TERM_DRIVER)
837 if (!NonEmpty(tname))
840 if (!NonEmpty(tname)) {
841 T(("Failure with TERM=%s", NonNull(tname)));
842 ret_error0(TGETENT_ERR, "TERM environment variable not set.\n");
846 myname = strdup(tname);
847 if (myname == NULL || strlen(myname) > MAX_NAME_SIZE) {
848 ret_error(TGETENT_ERR,
849 "TERM environment must be 1..%d characters.\n",
854 T(("your terminal name is %s", myname));
857 * Allow output redirection. This is what SVr3 does. If stdout is
858 * directed to a file, screen updates go to standard error.
860 if (Filedes == STDOUT_FILENO && !NC_ISATTY(Filedes))
861 Filedes = STDERR_FILENO;
862 #if defined(EXP_WIN32_DRIVER)
863 if (Filedes != STDERR_FILENO && NC_ISATTY(Filedes))
864 _setmode(Filedes, _O_BINARY);
868 * Check if we have already initialized to use this terminal. If so, we
869 * do not need to re-read the terminfo entry, or obtain TTY settings.
871 * This is an improvement on SVr4 curses. If an application mixes curses
872 * and termcap calls, it may call both initscr and tgetent. This is not
873 * really a good thing to do, but can happen if someone tries using ncurses
874 * with the readline library. The problem we are fixing is that when
875 * tgetent calls setupterm, the resulting Ottyb struct in cur_term is
876 * zeroed. A subsequent call to endwin uses the zeroed terminal settings
877 * rather than the ones saved in initscr. So we check if cur_term appears
878 * to contain terminal settings for the same output file as our current
879 * call - and copy those terminal settings. (SVr4 curses does not do this,
880 * however applications that are working around the problem will still work
881 * properly with this feature).
885 && termp->Filedes == Filedes
886 && termp->_termname != 0
887 && !strcmp(termp->_termname, myname)
888 && _nc_name_match(TerminalType(termp).term_names, myname, "|")) {
889 T(("reusing existing terminal information and mode-settings"));
891 #ifdef USE_TERM_DRIVER
892 TCB = (TERMINAL_CONTROL_BLOCK *) termp;
895 #ifdef USE_TERM_DRIVER
896 TERMINAL_CONTROL_BLOCK *my_tcb;
898 if ((my_tcb = typeCalloc(TERMINAL_CONTROL_BLOCK, 1)) != 0)
899 termp = &(my_tcb->term);
903 termp = typeCalloc(TERMINAL, 1);
906 ret_error1(TGETENT_ERR,
907 "Not enough memory to create terminal structure.\n",
908 myname, free(myname));
910 ++_nc_globals.terminal_count;
917 limit = _nc_globals.getstr_limit;
920 if (limit < sysconf(_SC_LINE_MAX))
921 limit = sysconf(_SC_LINE_MAX);
923 if (_nc_globals.getstr_limit < (int) limit)
924 _nc_globals.getstr_limit = (int) limit;
926 #endif /* HAVE_SYSCONF */
927 T(("using %d for getstr limit", _nc_globals.getstr_limit));
929 #ifdef USE_TERM_DRIVER
932 * _nc_get_driver() will call td_CanHandle() for each driver, and win_driver
933 * needs file descriptor to do the test, so set it before calling.
935 termp->Filedes = (short) Filedes;
936 TCB = (TERMINAL_CONTROL_BLOCK *) termp;
937 code = _nc_globals.term_driver(TCB, myname, errret);
939 termp->_termname = strdup(myname);
941 ret_error1(errret ? *errret : TGETENT_ERR,
942 "Could not find any driver to handle terminal.\n",
943 myname, free(myname));
946 #if NCURSES_USE_DATABASE || NCURSES_USE_TERMCAP
947 status = _nc_setup_tinfo(myname, &TerminalType(termp));
948 T(("_nc_setup_tinfo returns %d", status));
950 T(("no database available"));
954 /* try fallback list if entry on disk */
955 if (status != TGETENT_YES) {
956 const TERMTYPE2 *fallback = _nc_fallback2(myname);
959 T(("found fallback entry"));
960 _nc_copy_termtype2(&(TerminalType(termp)), fallback);
961 status = TGETENT_YES;
965 if (status != TGETENT_YES) {
967 if (status == TGETENT_ERR) {
969 ret_error0(status, "terminals database is inaccessible\n");
970 } else if (status == TGETENT_NO) {
971 ret_error1(status, "unknown terminal type.\n",
972 myname, free(myname));
975 ret_error0(status, "unexpected return-code\n");
978 #if NCURSES_EXT_NUMBERS
979 _nc_export_termtype2(&termp->type, &TerminalType(termp));
985 termp->Filedes = (short) Filedes;
986 termp->_termname = strdup(myname);
990 if (VALID_STRING(command_character))
991 _nc_tinfo_cmdch(termp, UChar(*command_character));
994 * If an application calls setupterm() rather than initscr() or
995 * newterm(), we will not have the def_prog_mode() call in
996 * _nc_setupscreen(). Do it now anyway, so we can initialize the
997 * baudrate. Also get the shell-mode so that erasechar() works.
999 if (NC_ISATTY(Filedes)) {
1000 NCURSES_SP_NAME(def_shell_mode) (NCURSES_SP_ARG);
1001 NCURSES_SP_NAME(def_prog_mode) (NCURSES_SP_ARG);
1002 NCURSES_SP_NAME(baudrate) (NCURSES_SP_ARG);
1008 #ifdef USE_TERM_DRIVER
1010 NCURSES_SP_NAME(set_curterm) (sp, termp);
1011 TCB->drv->td_init(TCB);
1017 * We should always check the screensize, just in case.
1019 TINFO_GET_SIZE(sp, termp, ptrLines(sp), ptrCols(sp));
1022 *errret = TGETENT_YES;
1024 #ifndef USE_TERM_DRIVER
1027 * BSD 4.3's termcap contains mis-typed "gn" for wy99. Do a sanity
1028 * check before giving up.
1030 if ((VALID_STRING(cursor_address)
1031 || (VALID_STRING(cursor_down) && VALID_STRING(cursor_home)))
1032 && VALID_STRING(clear_screen)) {
1033 ret_error1(TGETENT_YES, "terminal is not really generic.\n",
1034 myname, free(myname));
1037 ret_error1(TGETENT_NO, "I need something more specific.\n",
1038 myname, free(myname));
1040 } else if (hard_copy) {
1041 ret_error1(TGETENT_YES, "I can't handle hardcopy terminals.\n",
1042 myname, free(myname));
1051 * Returns a non-null pointer unless a new screen should be allocated because
1052 * no match was found in the pre-screen cache.
1054 NCURSES_EXPORT(SCREEN *)
1055 _nc_find_prescr(void)
1059 pthread_t id = GetThreadID();
1060 for (p = _nc_prescreen.allocated; p != 0; p = p->next) {
1070 * Tells ncurses to forget that this thread was associated with the pre-screen
1071 * cache. It does not modify the pre-screen cache itself, since that is used
1072 * for creating new screens.
1074 NCURSES_EXPORT(void)
1075 _nc_forget_prescr(void)
1077 PRESCREEN_LIST *p, *q;
1078 pthread_t id = GetThreadID();
1079 _nc_lock_global(screen);
1080 for (p = _nc_prescreen.allocated, q = 0; p != 0; q = p, p = p->next) {
1085 _nc_prescreen.allocated = p->next;
1091 _nc_unlock_global(screen);
1093 #endif /* USE_PTHREADS */
1095 #if NCURSES_SP_FUNCS
1097 * In case of handling multiple screens, we need to have a screen before
1098 * initialization in _nc_setupscreen takes place. This is to extend the
1099 * substitute for some of the stuff in _nc_prescreen, especially for slk and
1100 * ripoff handling which should be done per screen.
1102 NCURSES_EXPORT(SCREEN *)
1108 T((T_CALLED("new_prescr()")));
1110 _nc_lock_global(screen);
1111 if ((sp = _nc_find_prescr()) == 0) {
1112 sp = _nc_alloc_screen_sp();
1113 T(("_nc_alloc_screen_sp %p", (void *) sp));
1116 PRESCREEN_LIST *p = typeCalloc(PRESCREEN_LIST, 1);
1118 p->id = GetThreadID();
1120 p->next = _nc_prescreen.allocated;
1121 _nc_prescreen.allocated = p;
1124 _nc_prescreen.allocated = sp;
1126 sp->rsp = sp->rippedoff;
1127 sp->_filtered = _nc_prescreen.filter_mode;
1128 sp->_use_env = _nc_prescreen.use_env;
1129 #if NCURSES_NO_PADDING
1130 sp->_no_padding = _nc_prescreen._no_padding;
1134 sp->_prescreen = TRUE;
1137 sp->_TABSIZE = _nc_prescreen._TABSIZE;
1138 sp->_ESCDELAY = _nc_prescreen._ESCDELAY;
1142 T(("_nc_alloc_screen_sp %p (reuse)", (void *) sp));
1144 _nc_unlock_global(screen);
1149 #ifdef USE_TERM_DRIVER
1151 * This entrypoint is called from tgetent() to allow a special case of reusing
1152 * the same TERMINAL data (see comment).
1155 _nc_setupterm(const char *tname,
1161 TERMINAL *termp = 0;
1163 _nc_init_pthreads();
1164 _nc_lock_global(prescreen);
1166 if (TINFO_SETUP_TERM(&termp, tname, Filedes, errret, reuse) == OK) {
1167 _nc_forget_prescr();
1168 if (NCURSES_SP_NAME(set_curterm) (CURRENT_SCREEN_PRE, termp) != 0) {
1172 _nc_unlock_global(prescreen);
1179 * setupterm(termname, Filedes, errret)
1181 * Find and read the appropriate object file for the terminal
1182 * Make cur_term point to the structure.
1185 setupterm(const char *tname, int Filedes, int *errret)
1188 return _nc_setupterm(tname, Filedes, errret, FALSE);