-# ifdef DEBUG
- fprintf(outf, "INITSCR()\n");
-# endif
- if (My_term)
- setterm(Def_term);
- else {
- if (isatty(2))
- _tty_ch = 2;
- else {
- nfd = getdtablesize();
- for (_tty_ch = 0; _tty_ch < nfd; _tty_ch++)
- if (isatty(_tty_ch))
- break;
- }
- gettmode();
- if ((sp = getenv("TERM")) == NULL)
- sp = Def_term;
- setterm(sp);
-# ifdef DEBUG
- fprintf(outf, "INITSCR: term = %s\n", sp);
-# endif
- }
- _puts(TI);
- _puts(VS);
-# ifdef SIGTSTP
- signal(SIGTSTP, tstp);
-# endif
- if (curscr != NULL) {
-# ifdef DEBUG
- fprintf(outf, "INITSCR: curscr = 0%o\n", curscr);
-# endif
+ if (gettmode() == ERR)
+ return (NULL);
+
+ /*
+ * If My_term is set, or can't find a terminal in the environment,
+ * use Def_term.
+ */
+ if (My_term || (sp = getenv("TERM")) == NULL)
+ sp = Def_term;
+ if (setterm(sp) == ERR)
+ return (NULL);
+
+ /* Need either homing or cursor motion for refreshes */
+ if (!HO && !CM)
+ return (NULL);
+
+ tputs(TI, 0, __cputchar);
+ tputs(VS, 0, __cputchar);
+
+ if (curscr != NULL)