+ }
+
+ if (strlen(tname) > MAX_NAME_SIZE) {
+ ret_error(TGETENT_ERR,
+ "TERM environment must be <= %d characters.\n",
+ MAX_NAME_SIZE);
+ }
+
+ T(("your terminal name is %s", tname));
+
+ /*
+ * Allow output redirection. This is what SVr3 does. If stdout is
+ * directed to a file, screen updates go to standard error.
+ */
+ if (Filedes == STDOUT_FILENO && !NC_ISATTY(Filedes))
+ Filedes = STDERR_FILENO;
+
+ /*
+ * Check if we have already initialized to use this terminal. If so, we
+ * do not need to re-read the terminfo entry, or obtain TTY settings.
+ *
+ * This is an improvement on SVr4 curses. If an application mixes curses
+ * and termcap calls, it may call both initscr and tgetent. This is not
+ * really a good thing to do, but can happen if someone tries using ncurses
+ * with the readline library. The problem we are fixing is that when
+ * tgetent calls setupterm, the resulting Ottyb struct in cur_term is
+ * zeroed. A subsequent call to endwin uses the zeroed terminal settings
+ * rather than the ones saved in initscr. So we check if cur_term appears
+ * to contain terminal settings for the same output file as our current
+ * call - and copy those terminal settings. (SVr4 curses does not do this,
+ * however applications that are working around the problem will still work
+ * properly with this feature).
+ */
+ if (reuse
+ && (termp != 0)
+ && termp->Filedes == Filedes
+ && termp->_termname != 0
+ && !strcmp(termp->_termname, tname)
+ && _nc_name_match(TerminalType(termp).term_names, tname, "|")) {
+ T(("reusing existing terminal information and mode-settings"));
+ code = OK;
+#ifdef USE_TERM_DRIVER
+ TCB = (TERMINAL_CONTROL_BLOCK *) termp;
+#endif
+ } else {
+#ifdef USE_TERM_DRIVER
+ TERMINAL_CONTROL_BLOCK *my_tcb;
+ termp = 0;
+ if ((my_tcb = typeCalloc(TERMINAL_CONTROL_BLOCK, 1)) != 0)
+ termp = &(my_tcb->term);
+#else
+ int status;