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