]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/lib_setup.c
ncurses 5.9 - patch 20110625
[ncurses.git] / ncurses / tinfo / lib_setup.c
1 /****************************************************************************
2  * Copyright (c) 1998-2010,2011 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.138 2011/06/14 22:36:29 tom 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 #define ret_error(code, fmt, arg)       if (errret) {\
416                                             *errret = code;\
417                                             returnCode(ERR);\
418                                         } else {\
419                                             fprintf(stderr, fmt, arg);\
420                                             exit(EXIT_FAILURE);\
421                                         }
422
423 #define ret_error0(code, msg)           if (errret) {\
424                                             *errret = code;\
425                                             returnCode(ERR);\
426                                         } else {\
427                                             fprintf(stderr, msg);\
428                                             exit(EXIT_FAILURE);\
429                                         }
430
431 #if USE_DATABASE || USE_TERMCAP
432 /*
433  * Return 1 if entry found, 0 if not found, -1 if database not accessible,
434  * just like tgetent().
435  */
436 int
437 _nc_setup_tinfo(const char *const tn, TERMTYPE *const tp)
438 {
439     char filename[PATH_MAX];
440     int status = _nc_read_entry(tn, filename, tp);
441
442     /*
443      * If we have an entry, force all of the cancelled strings to null
444      * pointers so we don't have to test them in the rest of the library.
445      * (The terminfo compiler bypasses this logic, since it must know if
446      * a string is cancelled, for merging entries).
447      */
448     if (status == TGETENT_YES) {
449         unsigned n;
450         for_each_boolean(n, tp) {
451             if (!VALID_BOOLEAN(tp->Booleans[n]))
452                 tp->Booleans[n] = FALSE;
453         }
454         for_each_string(n, tp) {
455             if (tp->Strings[n] == CANCELLED_STRING)
456                 tp->Strings[n] = ABSENT_STRING;
457         }
458     }
459     return (status);
460 }
461 #endif
462
463 /*
464 **      Take the real command character out of the CC environment variable
465 **      and substitute it in for the prototype given in 'command_character'.
466 */
467 void
468 _nc_tinfo_cmdch(TERMINAL * termp, char proto)
469 {
470     unsigned i;
471     char CC;
472     char *tmp;
473
474     /*
475      * Only use the character if the string is a single character,
476      * since it is fairly common for developers to set the C compiler
477      * name as an environment variable - using the same symbol.
478      */
479     if ((tmp = getenv("CC")) != 0 && strlen(tmp) == 1) {
480         CC = *tmp;
481         for_each_string(i, &(termp->type)) {
482             for (tmp = termp->type.Strings[i]; *tmp; tmp++) {
483                 if (*tmp == proto)
484                     *tmp = CC;
485             }
486         }
487     }
488 }
489
490 /*
491  * Find the locale which is in effect.
492  */
493 NCURSES_EXPORT(char *)
494 _nc_get_locale(void)
495 {
496     char *env;
497 #if HAVE_LOCALE_H
498     /*
499      * This is preferable to using getenv() since it ensures that we are using
500      * the locale which was actually initialized by the application.
501      */
502     env = setlocale(LC_CTYPE, 0);
503 #else
504     if (((env = getenv("LC_ALL")) != 0 && *env != '\0')
505         || ((env = getenv("LC_CTYPE")) != 0 && *env != '\0')
506         || ((env = getenv("LANG")) != 0 && *env != '\0')) {
507         ;
508     }
509 #endif
510     T(("_nc_get_locale %s", _nc_visbuf(env)));
511     return env;
512 }
513
514 /*
515  * Check if we are running in a UTF-8 locale.
516  */
517 NCURSES_EXPORT(int)
518 _nc_unicode_locale(void)
519 {
520     int result = 0;
521 #if HAVE_LANGINFO_CODESET
522     char *env = nl_langinfo(CODESET);
523     result = !strcmp(env, "UTF-8");
524     T(("_nc_unicode_locale(%s) ->%d", env, result));
525 #else
526     char *env = _nc_get_locale();
527     if (env != 0) {
528         if (strstr(env, ".UTF-8") != 0) {
529             result = 1;
530             T(("_nc_unicode_locale(%s) ->%d", env, result));
531         }
532     }
533 #endif
534     return result;
535 }
536
537 #define CONTROL_N(s) ((s) != 0 && strstr(s, "\016") != 0)
538 #define CONTROL_O(s) ((s) != 0 && strstr(s, "\017") != 0)
539
540 /*
541  * Check for known broken cases where a UTF-8 locale breaks the alternate
542  * character set.
543  */
544 NCURSES_EXPORT(int)
545 _nc_locale_breaks_acs(TERMINAL * termp)
546 {
547     const char *env_name = "NCURSES_NO_UTF8_ACS";
548     char *env;
549     int value;
550     int result = 0;
551
552     if (getenv(env_name) != 0) {
553         result = _nc_getenv_num(env_name);
554     } else if ((value = tigetnum("U8")) >= 0) {
555         result = value;         /* use extension feature */
556     } else if ((env = getenv("TERM")) != 0) {
557         if (strstr(env, "linux")) {
558             result = 1;         /* always broken */
559         } else if (strstr(env, "screen") != 0
560                    && ((env = getenv("TERMCAP")) != 0
561                        && strstr(env, "screen") != 0)
562                    && strstr(env, "hhII00") != 0) {
563             if (CONTROL_N(enter_alt_charset_mode) ||
564                 CONTROL_O(enter_alt_charset_mode) ||
565                 CONTROL_N(set_attributes) ||
566                 CONTROL_O(set_attributes)) {
567                 result = 1;
568             }
569         }
570     }
571     return result;
572 }
573
574 NCURSES_EXPORT(int)
575 TINFO_SETUP_TERM(TERMINAL ** tp,
576                  NCURSES_CONST char *tname,
577                  int Filedes,
578                  int *errret,
579                  bool reuse)
580 {
581 #ifdef USE_TERM_DRIVER
582     TERMINAL_CONTROL_BLOCK *TCB = 0;
583 #else
584     int status;
585 #endif
586     TERMINAL *termp;
587     SCREEN *sp = 0;
588     int code = ERR;
589
590     START_TRACE();
591
592 #ifdef USE_TERM_DRIVER
593     T((T_CALLED("_nc_setupterm_ex(%p,%s,%d,%p)"),
594        (void *) tp, _nc_visbuf(tname), Filedes, (void *) errret));
595
596     if (tp == 0) {
597         ret_error0(TGETENT_ERR,
598                    "Invalid parameter, internal error.\n");
599     } else
600         termp = *tp;
601 #else
602     termp = cur_term;
603     T((T_CALLED("setupterm(%s,%d,%p)"), _nc_visbuf(tname), Filedes, (void *) errret));
604 #endif
605
606     if (tname == 0) {
607         tname = getenv("TERM");
608         if (tname == 0 || *tname == '\0') {
609             ret_error0(TGETENT_ERR, "TERM environment variable not set.\n");
610         }
611     }
612
613     if (strlen(tname) > MAX_NAME_SIZE) {
614         ret_error(TGETENT_ERR,
615                   "TERM environment must be <= %d characters.\n",
616                   MAX_NAME_SIZE);
617     }
618
619     T(("your terminal name is %s", tname));
620
621     /*
622      * Allow output redirection.  This is what SVr3 does.  If stdout is
623      * directed to a file, screen updates go to standard error.
624      */
625     if (Filedes == STDOUT_FILENO && !isatty(Filedes))
626         Filedes = STDERR_FILENO;
627
628     /*
629      * Check if we have already initialized to use this terminal.  If so, we
630      * do not need to re-read the terminfo entry, or obtain TTY settings.
631      *
632      * This is an improvement on SVr4 curses.  If an application mixes curses
633      * and termcap calls, it may call both initscr and tgetent.  This is not
634      * really a good thing to do, but can happen if someone tries using ncurses
635      * with the readline library.  The problem we are fixing is that when
636      * tgetent calls setupterm, the resulting Ottyb struct in cur_term is
637      * zeroed.  A subsequent call to endwin uses the zeroed terminal settings
638      * rather than the ones saved in initscr.  So we check if cur_term appears
639      * to contain terminal settings for the same output file as our current
640      * call - and copy those terminal settings.  (SVr4 curses does not do this,
641      * however applications that are working around the problem will still work
642      * properly with this feature).
643      */
644     if (reuse
645         && (termp != 0)
646         && termp->Filedes == Filedes
647         && termp->_termname != 0
648         && !strcmp(termp->_termname, tname)
649         && _nc_name_match(termp->type.term_names, tname, "|")) {
650         T(("reusing existing terminal information and mode-settings"));
651         code = OK;
652     } else {
653 #ifdef USE_TERM_DRIVER
654         termp = (TERMINAL *) typeCalloc(TERMINAL_CONTROL_BLOCK, 1);
655 #else
656         termp = typeCalloc(TERMINAL, 1);
657 #endif
658         if (termp == 0) {
659             ret_error0(TGETENT_ERR,
660                        "Not enough memory to create terminal structure.\n");
661         }
662 #ifdef USE_TERM_DRIVER
663         INIT_TERM_DRIVER();
664         TCB = (TERMINAL_CONTROL_BLOCK *) termp;
665         code = _nc_globals.term_driver(TCB, tname, errret);
666         if (code == OK) {
667             termp->Filedes = (short) Filedes;
668             termp->_termname = strdup(tname);
669         } else {
670             ret_error0(TGETENT_ERR,
671                        "Could not find any driver to handle this terminal.\n");
672         }
673 #else
674 #if USE_DATABASE || USE_TERMCAP
675         status = _nc_setup_tinfo(tname, &termp->type);
676 #else
677         status = TGETENT_NO;
678 #endif
679
680         /* try fallback list if entry on disk */
681         if (status != TGETENT_YES) {
682             const TERMTYPE *fallback = _nc_fallback(tname);
683
684             if (fallback) {
685                 termp->type = *fallback;
686                 status = TGETENT_YES;
687             }
688         }
689
690         if (status != TGETENT_YES) {
691             del_curterm(termp);
692             if (status == TGETENT_ERR) {
693                 ret_error0(status, "terminals database is inaccessible\n");
694             } else if (status == TGETENT_NO) {
695                 ret_error(status, "'%s': unknown terminal type.\n", tname);
696             }
697         }
698 #if !USE_REENTRANT
699         strncpy(ttytype, termp->type.term_names, NAMESIZE - 1);
700         ttytype[NAMESIZE - 1] = '\0';
701 #endif
702
703         termp->Filedes = (short) Filedes;
704         termp->_termname = strdup(tname);
705
706         set_curterm(termp);
707
708         if (command_character)
709             _nc_tinfo_cmdch(termp, *command_character);
710
711         /*
712          * If an application calls setupterm() rather than initscr() or
713          * newterm(), we will not have the def_prog_mode() call in
714          * _nc_setupscreen().  Do it now anyway, so we can initialize the
715          * baudrate.
716          */
717         if (isatty(Filedes)) {
718             def_prog_mode();
719             baudrate();
720         }
721         code = OK;
722 #endif
723     }
724
725 #ifdef USE_TERM_DRIVER
726     *tp = termp;
727     NCURSES_SP_NAME(set_curterm) (sp, termp);
728     TCB->drv->init(TCB);
729 #else
730     sp = SP;
731 #endif
732
733     /*
734      * We should always check the screensize, just in case.
735      */
736     TINFO_GET_SIZE(sp, termp, ptrLines(sp), ptrCols(sp));
737
738     if (errret)
739         *errret = TGETENT_YES;
740
741 #ifndef USE_TERM_DRIVER
742     if (generic_type) {
743         ret_error(TGETENT_NO, "'%s': I need something more specific.\n", tname);
744     }
745     if (hard_copy) {
746         ret_error(TGETENT_YES, "'%s': I can't handle hardcopy terminals.\n", tname);
747     }
748 #endif
749     returnCode(code);
750 }
751
752 #if NCURSES_SP_FUNCS
753 /*
754  * In case of handling multiple screens, we need to have a screen before
755  * initialization in setupscreen takes place.  This is to extend the substitute
756  * for some of the stuff in _nc_prescreen, especially for slk and ripoff
757  * handling which should be done per screen.
758  */
759 NCURSES_EXPORT(SCREEN *)
760 new_prescr(void)
761 {
762     static SCREEN *sp;
763
764     START_TRACE();
765     T((T_CALLED("new_prescr()")));
766
767     if (sp == 0) {
768         sp = _nc_alloc_screen_sp();
769         if (sp != 0) {
770             sp->rsp = sp->rippedoff;
771             sp->_filtered = _nc_prescreen.filter_mode;
772             sp->_use_env = _nc_prescreen.use_env;
773 #if NCURSES_NO_PADDING
774             sp->_no_padding = _nc_prescreen._no_padding;
775 #endif
776             sp->slk_format = 0;
777             sp->_slk = 0;
778             sp->_prescreen = TRUE;
779             SP_PRE_INIT(sp);
780 #if USE_REENTRANT
781             sp->_TABSIZE = _nc_prescreen._TABSIZE;
782             sp->_ESCDELAY = _nc_prescreen._ESCDELAY;
783 #endif
784         }
785     }
786     returnSP(sp);
787 }
788 #endif
789
790 #ifdef USE_TERM_DRIVER
791 /*
792  * This entrypoint is called from tgetent() to allow a special case of reusing
793  * the same TERMINAL data (see comment).
794  */
795 NCURSES_EXPORT(int)
796 _nc_setupterm(NCURSES_CONST char *tname,
797               int Filedes,
798               int *errret,
799               bool reuse)
800 {
801     int res;
802     TERMINAL *termp;
803     res = TINFO_SETUP_TERM(&termp, tname, Filedes, errret, reuse);
804     if (ERR != res)
805         NCURSES_SP_NAME(set_curterm) (CURRENT_SCREEN_PRE, termp);
806     return res;
807 }
808 #endif
809
810 /*
811  *      setupterm(termname, Filedes, errret)
812  *
813  *      Find and read the appropriate object file for the terminal
814  *      Make cur_term point to the structure.
815  */
816 NCURSES_EXPORT(int)
817 setupterm(NCURSES_CONST char *tname, int Filedes, int *errret)
818 {
819     return _nc_setupterm(tname, Filedes, errret, FALSE);
820 }