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