+ if (tname == 0) {
+ tname = getenv("TERM");
+ if (tname == 0 || *tname == '\0') {
+ ret_error0(TGETENT_ERR, "TERM environment variable not set.\n");
+ }
+ }
+
+ 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 && !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 = cur_term) != 0
+ && termp->Filedes == Filedes
+ && termp->_termname != 0
+ && !strcmp(termp->_termname, tname)
+ && _nc_name_match(termp->type.term_names, tname, "|")) {
+ T(("reusing existing terminal information and mode-settings"));
+ } else {
+
+ termp = typeCalloc(TERMINAL, 1);
+
+ if (termp == 0) {
+ ret_error0(TGETENT_ERR,
+ "Not enough memory to create terminal structure.\n");
+ }
+#if USE_DATABASE || USE_TERMCAP
+ status = grab_entry(tname, &termp->type);