]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/lib_setup.c
ncurses 5.9 - patch 20120707
[ncurses.git] / ncurses / tinfo / lib_setup.c
1 /****************************************************************************
2  * Copyright (c) 1998-2011,2012 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  *     and: Thomas E. Dickey                        1996-on                 *
33  *     and: Juergen Pfeifer                         2009                    *
34  ****************************************************************************/
35
36 /*
37  * Terminal setup routines common to termcap and terminfo:
38  *
39  *              use_env(bool)
40  *              setupterm(char *, int, int *)
41  */
42
43 #include <curses.priv.h>
44 #include <tic.h>                /* for MAX_NAME_SIZE */
45
46 #if HAVE_LOCALE_H
47 #include <locale.h>
48 #endif
49
50 MODULE_ID("$Id: lib_setup.c,v 1.145 2012/07/07 20:35:27 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         } else if (sp->_sig_winch && (sp->_ungetch != 0)) {
405             sp->_ungetch(SP_PARM, KEY_RESIZE);  /* so application can know this */
406         }
407         sp->_sig_winch = FALSE;
408     }
409 }
410 #endif
411
412 /****************************************************************************
413  *
414  * Terminal setup
415  *
416  ****************************************************************************/
417
418 #if USE_DATABASE || USE_TERMCAP
419 /*
420  * Return 1 if entry found, 0 if not found, -1 if database not accessible,
421  * just like tgetent().
422  */
423 int
424 _nc_setup_tinfo(const char *const tn, TERMTYPE *const tp)
425 {
426     char filename[PATH_MAX];
427     int status = _nc_read_entry(tn, filename, tp);
428
429     /*
430      * If we have an entry, force all of the cancelled strings to null
431      * pointers so we don't have to test them in the rest of the library.
432      * (The terminfo compiler bypasses this logic, since it must know if
433      * a string is cancelled, for merging entries).
434      */
435     if (status == TGETENT_YES) {
436         unsigned n;
437         for_each_boolean(n, tp) {
438             if (!VALID_BOOLEAN(tp->Booleans[n]))
439                 tp->Booleans[n] = FALSE;
440         }
441         for_each_string(n, tp) {
442             if (tp->Strings[n] == CANCELLED_STRING)
443                 tp->Strings[n] = ABSENT_STRING;
444         }
445     }
446     return (status);
447 }
448 #endif
449
450 /*
451 **      Take the real command character out of the CC environment variable
452 **      and substitute it in for the prototype given in 'command_character'.
453 */
454 void
455 _nc_tinfo_cmdch(TERMINAL * termp, int proto)
456 {
457     unsigned i;
458     char CC;
459     char *tmp;
460
461     /*
462      * Only use the character if the string is a single character,
463      * since it is fairly common for developers to set the C compiler
464      * name as an environment variable - using the same symbol.
465      */
466     if ((tmp = getenv("CC")) != 0 && strlen(tmp) == 1) {
467         CC = *tmp;
468         for_each_string(i, &(termp->type)) {
469             for (tmp = termp->type.Strings[i]; tmp && *tmp; tmp++) {
470                 if (UChar(*tmp) == proto)
471                     *tmp = CC;
472             }
473         }
474     }
475 }
476
477 /*
478  * Find the locale which is in effect.
479  */
480 NCURSES_EXPORT(char *)
481 _nc_get_locale(void)
482 {
483     char *env;
484 #if HAVE_LOCALE_H
485     /*
486      * This is preferable to using getenv() since it ensures that we are using
487      * the locale which was actually initialized by the application.
488      */
489     env = setlocale(LC_CTYPE, 0);
490 #else
491     if (((env = getenv("LC_ALL")) != 0 && *env != '\0')
492         || ((env = getenv("LC_CTYPE")) != 0 && *env != '\0')
493         || ((env = getenv("LANG")) != 0 && *env != '\0')) {
494         ;
495     }
496 #endif
497     T(("_nc_get_locale %s", _nc_visbuf(env)));
498     return env;
499 }
500
501 /*
502  * Check if we are running in a UTF-8 locale.
503  */
504 NCURSES_EXPORT(int)
505 _nc_unicode_locale(void)
506 {
507     int result = 0;
508 #if HAVE_LANGINFO_CODESET
509     char *env = nl_langinfo(CODESET);
510     result = !strcmp(env, "UTF-8");
511     T(("_nc_unicode_locale(%s) ->%d", env, result));
512 #else
513     char *env = _nc_get_locale();
514     if (env != 0) {
515         if (strstr(env, ".UTF-8") != 0) {
516             result = 1;
517             T(("_nc_unicode_locale(%s) ->%d", env, result));
518         }
519     }
520 #endif
521     return result;
522 }
523
524 #define CONTROL_N(s) ((s) != 0 && strstr(s, "\016") != 0)
525 #define CONTROL_O(s) ((s) != 0 && strstr(s, "\017") != 0)
526
527 /*
528  * Check for known broken cases where a UTF-8 locale breaks the alternate
529  * character set.
530  */
531 NCURSES_EXPORT(int)
532 _nc_locale_breaks_acs(TERMINAL * termp)
533 {
534     const char *env_name = "NCURSES_NO_UTF8_ACS";
535     char *env;
536     int value;
537     int result = 0;
538
539     if (getenv(env_name) != 0) {
540         result = _nc_getenv_num(env_name);
541     } else if ((value = tigetnum("U8")) >= 0) {
542         result = value;         /* use extension feature */
543     } else if ((env = getenv("TERM")) != 0) {
544         if (strstr(env, "linux")) {
545             result = 1;         /* always broken */
546         } else if (strstr(env, "screen") != 0
547                    && ((env = getenv("TERMCAP")) != 0
548                        && strstr(env, "screen") != 0)
549                    && strstr(env, "hhII00") != 0) {
550             if (CONTROL_N(enter_alt_charset_mode) ||
551                 CONTROL_O(enter_alt_charset_mode) ||
552                 CONTROL_N(set_attributes) ||
553                 CONTROL_O(set_attributes)) {
554                 result = 1;
555             }
556         }
557     }
558     return result;
559 }
560
561 NCURSES_EXPORT(int)
562 TINFO_SETUP_TERM(TERMINAL ** tp,
563                  NCURSES_CONST char *tname,
564                  int Filedes,
565                  int *errret,
566                  int reuse)
567 {
568 #ifdef USE_TERM_DRIVER
569     TERMINAL_CONTROL_BLOCK *TCB = 0;
570 #else
571     int status;
572 #endif
573     TERMINAL *termp;
574     SCREEN *sp = 0;
575     int code = ERR;
576
577     START_TRACE();
578
579 #ifdef USE_TERM_DRIVER
580     T((T_CALLED("_nc_setupterm_ex(%p,%s,%d,%p)"),
581        (void *) tp, _nc_visbuf(tname), Filedes, (void *) errret));
582
583     if (tp == 0) {
584         ret_error0(TGETENT_ERR,
585                    "Invalid parameter, internal error.\n");
586     } else
587         termp = *tp;
588 #else
589     termp = cur_term;
590     T((T_CALLED("setupterm(%s,%d,%p)"), _nc_visbuf(tname), Filedes, (void *) errret));
591 #endif
592
593     if (tname == 0) {
594         tname = getenv("TERM");
595         if (tname == 0 || *tname == '\0') {
596             ret_error0(TGETENT_ERR, "TERM environment variable not set.\n");
597         }
598     }
599
600     if (strlen(tname) > MAX_NAME_SIZE) {
601         ret_error(TGETENT_ERR,
602                   "TERM environment must be <= %d characters.\n",
603                   MAX_NAME_SIZE);
604     }
605
606     T(("your terminal name is %s", tname));
607
608     /*
609      * Allow output redirection.  This is what SVr3 does.  If stdout is
610      * directed to a file, screen updates go to standard error.
611      */
612     if (Filedes == STDOUT_FILENO && !isatty(Filedes))
613         Filedes = STDERR_FILENO;
614
615     /*
616      * Check if we have already initialized to use this terminal.  If so, we
617      * do not need to re-read the terminfo entry, or obtain TTY settings.
618      *
619      * This is an improvement on SVr4 curses.  If an application mixes curses
620      * and termcap calls, it may call both initscr and tgetent.  This is not
621      * really a good thing to do, but can happen if someone tries using ncurses
622      * with the readline library.  The problem we are fixing is that when
623      * tgetent calls setupterm, the resulting Ottyb struct in cur_term is
624      * zeroed.  A subsequent call to endwin uses the zeroed terminal settings
625      * rather than the ones saved in initscr.  So we check if cur_term appears
626      * to contain terminal settings for the same output file as our current
627      * call - and copy those terminal settings.  (SVr4 curses does not do this,
628      * however applications that are working around the problem will still work
629      * properly with this feature).
630      */
631     if (reuse
632         && (termp != 0)
633         && termp->Filedes == Filedes
634         && termp->_termname != 0
635         && !strcmp(termp->_termname, tname)
636         && _nc_name_match(termp->type.term_names, tname, "|")) {
637         T(("reusing existing terminal information and mode-settings"));
638         code = OK;
639     } else {
640 #ifdef USE_TERM_DRIVER
641         termp = (TERMINAL *) typeCalloc(TERMINAL_CONTROL_BLOCK, 1);
642 #else
643         termp = typeCalloc(TERMINAL, 1);
644 #endif
645         if (termp == 0) {
646             ret_error0(TGETENT_ERR,
647                        "Not enough memory to create terminal structure.\n");
648         }
649 #ifdef USE_TERM_DRIVER
650         INIT_TERM_DRIVER();
651         TCB = (TERMINAL_CONTROL_BLOCK *) termp;
652         code = _nc_globals.term_driver(TCB, tname, errret);
653         if (code == OK) {
654             termp->Filedes = (short) Filedes;
655             termp->_termname = strdup(tname);
656         } else {
657             ret_error0(TGETENT_ERR,
658                        "Could not find any driver to handle this terminal.\n");
659         }
660 #else
661 #if USE_DATABASE || USE_TERMCAP
662         status = _nc_setup_tinfo(tname, &termp->type);
663 #else
664         status = TGETENT_NO;
665 #endif
666
667         /* try fallback list if entry on disk */
668         if (status != TGETENT_YES) {
669             const TERMTYPE *fallback = _nc_fallback(tname);
670
671             if (fallback) {
672                 _nc_copy_termtype(&(termp->type), fallback);
673                 status = TGETENT_YES;
674             }
675         }
676
677         if (status != TGETENT_YES) {
678             del_curterm(termp);
679             if (status == TGETENT_ERR) {
680                 ret_error0(status, "terminals database is inaccessible\n");
681             } else if (status == TGETENT_NO) {
682                 ret_error1(status, "unknown terminal type.\n", tname);
683             }
684         }
685 #if !USE_REENTRANT
686         strncpy(ttytype, termp->type.term_names, (size_t) (NAMESIZE - 1));
687         ttytype[NAMESIZE - 1] = '\0';
688 #endif
689
690         termp->Filedes = (short) Filedes;
691         termp->_termname = strdup(tname);
692
693         set_curterm(termp);
694
695         if (command_character)
696             _nc_tinfo_cmdch(termp, UChar(*command_character));
697
698         /*
699          * If an application calls setupterm() rather than initscr() or
700          * newterm(), we will not have the def_prog_mode() call in
701          * _nc_setupscreen().  Do it now anyway, so we can initialize the
702          * baudrate.
703          */
704         if (isatty(Filedes)) {
705             def_prog_mode();
706             baudrate();
707         }
708         code = OK;
709 #endif
710     }
711
712 #ifdef USE_TERM_DRIVER
713     *tp = termp;
714     NCURSES_SP_NAME(set_curterm) (sp, termp);
715     TCB->drv->init(TCB);
716 #else
717     sp = SP;
718 #endif
719
720     /*
721      * We should always check the screensize, just in case.
722      */
723     TINFO_GET_SIZE(sp, termp, ptrLines(sp), ptrCols(sp));
724
725     if (errret)
726         *errret = TGETENT_YES;
727
728 #ifndef USE_TERM_DRIVER
729     if (generic_type) {
730         /*
731          * BSD 4.3's termcap contains mis-typed "gn" for wy99.  Do a sanity
732          * check before giving up.
733          */
734         if ((VALID_STRING(cursor_address)
735              || (VALID_STRING(cursor_down) && VALID_STRING(cursor_home)))
736             && VALID_STRING(clear_screen)) {
737             ret_error1(TGETENT_YES, "terminal is not really generic.\n", tname);
738         } else {
739             ret_error1(TGETENT_NO, "I need something more specific.\n", tname);
740         }
741     } else if (hard_copy) {
742         ret_error1(TGETENT_YES, "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               int 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 }