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