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