1 /****************************************************************************
2 * Copyright (c) 1998-2011,2012 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
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 ****************************************************************************/
37 * Terminal setup routines common to termcap and terminfo:
41 * setupterm(char *, int, int *)
44 #include <curses.priv.h>
45 #include <tic.h> /* for MAX_NAME_SIZE */
51 MODULE_ID("$Id: lib_setup.c,v 1.147 2012/07/14 23:59:26 tom Exp $")
53 /****************************************************************************
55 * Terminal size computation
57 ****************************************************************************/
60 # if !defined(sun) || !TERMIOS
62 # include <sys/ioctl.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).
71 # include <sys/stream.h>
72 # include <sys/ptem.h>
75 #if HAVE_LANGINFO_CODESET
80 * SCO defines TIOCGSIZE and the corresponding struct. Other systems (SunOS,
81 * Solaris, IRIX) define TIOCGWINSZ and struct winsize.
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
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
98 * Reduce explicit use of "cur_term" global variable.
101 #define CUR termp->type.
104 * Wrap global variables in this module.
108 NCURSES_EXPORT(char *)
109 NCURSES_PUBLIC_VAR(ttytype) (void)
111 static char empty[] = "";
112 char *result = empty;
115 if (CURRENT_SCREEN) {
116 TERMINAL *termp = TerminalOf(CURRENT_SCREEN);
118 result = termp->type.term_names;
123 result = cur_term->type.term_names;
129 NCURSES_EXPORT(int *)
130 _nc_ptr_Lines(SCREEN *sp)
136 NCURSES_PUBLIC_VAR(LINES) (void)
138 return *_nc_ptr_Lines(CURRENT_SCREEN);
141 NCURSES_EXPORT(int *)
142 _nc_ptr_Cols(SCREEN *sp)
148 NCURSES_PUBLIC_VAR(COLS) (void)
150 return *_nc_ptr_Cols(CURRENT_SCREEN);
153 NCURSES_EXPORT(int *)
154 _nc_ptr_Tabsize(SCREEN *sp)
156 return ptrTabsize(sp);
160 NCURSES_PUBLIC_VAR(TABSIZE) (void)
162 return *_nc_ptr_Tabsize(CURRENT_SCREEN);
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;
171 #if NCURSES_EXT_FUNCS
173 NCURSES_SP_NAME(set_tabsize) (NCURSES_SP_DCLx int value)
178 SP_PARM->_TABSIZE = value;
191 set_tabsize(int value)
193 return NCURSES_SP_NAME(set_tabsize) (CURRENT_SCREEN, value);
196 #endif /* NCURSES_EXT_FUNCS */
200 * If we have a pending SIGWINCH, set the flag in each screen.
203 _nc_handle_sigwinch(SCREEN *sp)
207 if (_nc_globals.have_sigwinch) {
208 _nc_globals.have_sigwinch = 0;
210 for (each_screen(scan)) {
211 scan->_sig_winch = TRUE;
215 return (sp ? sp->_sig_winch : 0);
221 NCURSES_SP_NAME(use_env) (NCURSES_SP_DCLx bool f)
223 T((T_CALLED("use_env(%p,%d)"), (void *) SP_PARM, (int) f));
226 if (IsPreScreen(SP_PARM)) {
227 SP_PARM->_use_env = f;
230 _nc_prescreen.use_env = f;
236 NCURSES_SP_NAME(use_tioctl) (NCURSES_SP_DCLx bool f)
238 T((T_CALLED("use_tioctl(%p,%d)"), (void *) SP_PARM, (int) f));
241 if (IsPreScreen(SP_PARM)) {
242 SP_PARM->_use_tioctl = f;
245 _nc_prescreen.use_tioctl = f;
254 T((T_CALLED("use_env(%d)"), (int) f));
256 _nc_prescreen.use_env = f;
263 T((T_CALLED("use_tioctl(%d)"), (int) f));
265 _nc_prescreen.use_tioctl = f;
271 _nc_get_screensize(SCREEN *sp,
272 #ifdef USE_TERM_DRIVER
275 int *linep, int *colp)
276 /* Obtain lines/columns values from the environment and/or terminfo entry */
278 #ifdef USE_TERM_DRIVER
279 TERMINAL_CONTROL_BLOCK *TCB;
282 assert(termp != 0 && linep != 0 && colp != 0);
283 TCB = (TERMINAL_CONTROL_BLOCK *) termp;
285 my_tabsize = TCB->info.tabsize;
286 TCB->drv->size(TCB, linep, colp);
290 sp->_TABSIZE = my_tabsize;
294 TABSIZE = my_tabsize;
296 T(("TABSIZE = %d", my_tabsize));
297 #else /* !USE_TERM_DRIVER */
298 TERMINAL *termp = cur_term;
301 /* figure out the size of the screen */
302 T(("screen size: terminfo lines = %d columns = %d", lines, columns));
304 *linep = (int) lines;
305 *colp = (int) columns;
307 if (_nc_prescreen.use_env) {
313 _scrsize(screendata);
314 *colp = screendata[0];
315 *linep = ((sp != 0 && sp->_filtered)
318 T(("EMX screen size: environment LINES = %d COLUMNS = %d",
323 /* try asking the OS */
324 if (isatty(cur_term->Filedes)) {
329 if (ioctl(cur_term->Filedes, IOCTL_WINSIZE, &size) >= 0) {
330 *linep = ((sp != 0 && sp->_filtered)
332 : WINSIZE_ROWS(size));
333 *colp = WINSIZE_COLS(size);
334 T(("SYS screen size: environment LINES = %d COLUMNS = %d",
341 #endif /* HAVE_SIZECHANGE */
343 if (_nc_prescreen.use_tioctl) {
345 * If environment variables are used, update them.
347 if ((sp == 0 || !sp->_filtered) && _nc_getenv_num("LINES") > 0) {
348 _nc_setenv_num("LINES", *linep);
350 if (_nc_getenv_num("COLUMNS") > 0) {
351 _nc_setenv_num("COLUMNS", *colp);
356 * Finally, look for environment variables.
358 * Solaris lets users override either dimension with an environment
361 if ((value = _nc_getenv_num("LINES")) > 0) {
363 T(("screen size: environment LINES = %d", *linep));
365 if ((value = _nc_getenv_num("COLUMNS")) > 0) {
367 T(("screen size: environment COLUMNS = %d", *colp));
370 /* if we can't get dynamic info about the size, use static */
372 *linep = (int) lines;
375 *colp = (int) columns;
378 /* the ultimate fallback, assume fixed 24x80 size */
387 * Put the derived values back in the screen-size caps, so
388 * tigetnum() and tgetnum() will do the right thing.
390 lines = (short) (*linep);
391 columns = (short) (*colp);
394 T(("screen size is %dx%d", *linep, *colp));
396 if (VALID_NUMERIC(init_tabs))
397 my_tabsize = (int) init_tabs;
403 sp->_TABSIZE = my_tabsize;
405 TABSIZE = my_tabsize;
407 T(("TABSIZE = %d", TABSIZE));
408 #endif /* USE_TERM_DRIVER */
413 _nc_update_screensize(SCREEN *sp)
418 #ifdef USE_TERM_DRIVER
424 CallDriver_2(sp, getsize, &old_lines, &old_cols);
427 TERMINAL *termp = cur_term;
428 int old_lines = lines;
429 int old_cols = columns;
432 TINFO_GET_SIZE(sp, sp->_term, &new_lines, &new_cols);
435 * See is_term_resized() and resizeterm().
436 * We're doing it this way because those functions belong to the upper
437 * ncurses library, while this resides in the lower terminfo library.
440 && sp->_resize != 0) {
441 if ((new_lines != old_lines) || (new_cols != old_cols)) {
442 sp->_resize(NCURSES_SP_ARGx new_lines, new_cols);
443 } else if (sp->_sig_winch && (sp->_ungetch != 0)) {
444 sp->_ungetch(SP_PARM, KEY_RESIZE); /* so application can know this */
446 sp->_sig_winch = FALSE;
451 /****************************************************************************
455 ****************************************************************************/
457 #if USE_DATABASE || USE_TERMCAP
459 * Return 1 if entry found, 0 if not found, -1 if database not accessible,
460 * just like tgetent().
463 _nc_setup_tinfo(const char *const tn, TERMTYPE *const tp)
465 char filename[PATH_MAX];
466 int status = _nc_read_entry(tn, filename, tp);
469 * If we have an entry, force all of the cancelled strings to null
470 * pointers so we don't have to test them in the rest of the library.
471 * (The terminfo compiler bypasses this logic, since it must know if
472 * a string is cancelled, for merging entries).
474 if (status == TGETENT_YES) {
476 for_each_boolean(n, tp) {
477 if (!VALID_BOOLEAN(tp->Booleans[n]))
478 tp->Booleans[n] = FALSE;
480 for_each_string(n, tp) {
481 if (tp->Strings[n] == CANCELLED_STRING)
482 tp->Strings[n] = ABSENT_STRING;
490 ** Take the real command character out of the CC environment variable
491 ** and substitute it in for the prototype given in 'command_character'.
494 _nc_tinfo_cmdch(TERMINAL * termp, int proto)
501 * Only use the character if the string is a single character,
502 * since it is fairly common for developers to set the C compiler
503 * name as an environment variable - using the same symbol.
505 if ((tmp = getenv("CC")) != 0 && strlen(tmp) == 1) {
507 for_each_string(i, &(termp->type)) {
508 for (tmp = termp->type.Strings[i]; tmp && *tmp; tmp++) {
509 if (UChar(*tmp) == proto)
517 * Find the locale which is in effect.
519 NCURSES_EXPORT(char *)
525 * This is preferable to using getenv() since it ensures that we are using
526 * the locale which was actually initialized by the application.
528 env = setlocale(LC_CTYPE, 0);
530 if (((env = getenv("LC_ALL")) != 0 && *env != '\0')
531 || ((env = getenv("LC_CTYPE")) != 0 && *env != '\0')
532 || ((env = getenv("LANG")) != 0 && *env != '\0')) {
536 T(("_nc_get_locale %s", _nc_visbuf(env)));
541 * Check if we are running in a UTF-8 locale.
544 _nc_unicode_locale(void)
547 #if HAVE_LANGINFO_CODESET
548 char *env = nl_langinfo(CODESET);
549 result = !strcmp(env, "UTF-8");
550 T(("_nc_unicode_locale(%s) ->%d", env, result));
552 char *env = _nc_get_locale();
554 if (strstr(env, ".UTF-8") != 0) {
556 T(("_nc_unicode_locale(%s) ->%d", env, result));
563 #define CONTROL_N(s) ((s) != 0 && strstr(s, "\016") != 0)
564 #define CONTROL_O(s) ((s) != 0 && strstr(s, "\017") != 0)
567 * Check for known broken cases where a UTF-8 locale breaks the alternate
571 _nc_locale_breaks_acs(TERMINAL * termp)
573 const char *env_name = "NCURSES_NO_UTF8_ACS";
578 if (getenv(env_name) != 0) {
579 result = _nc_getenv_num(env_name);
580 } else if ((value = tigetnum("U8")) >= 0) {
581 result = value; /* use extension feature */
582 } else if ((env = getenv("TERM")) != 0) {
583 if (strstr(env, "linux")) {
584 result = 1; /* always broken */
585 } else if (strstr(env, "screen") != 0
586 && ((env = getenv("TERMCAP")) != 0
587 && strstr(env, "screen") != 0)
588 && strstr(env, "hhII00") != 0) {
589 if (CONTROL_N(enter_alt_charset_mode) ||
590 CONTROL_O(enter_alt_charset_mode) ||
591 CONTROL_N(set_attributes) ||
592 CONTROL_O(set_attributes)) {
601 TINFO_SETUP_TERM(TERMINAL ** tp,
602 NCURSES_CONST char *tname,
607 #ifdef USE_TERM_DRIVER
608 TERMINAL_CONTROL_BLOCK *TCB = 0;
618 #ifdef USE_TERM_DRIVER
619 T((T_CALLED("_nc_setupterm_ex(%p,%s,%d,%p)"),
620 (void *) tp, _nc_visbuf(tname), Filedes, (void *) errret));
623 ret_error0(TGETENT_ERR,
624 "Invalid parameter, internal error.\n");
629 T((T_CALLED("setupterm(%s,%d,%p)"), _nc_visbuf(tname), Filedes, (void *) errret));
633 tname = getenv("TERM");
634 if (tname == 0 || *tname == '\0') {
635 ret_error0(TGETENT_ERR, "TERM environment variable not set.\n");
639 if (strlen(tname) > MAX_NAME_SIZE) {
640 ret_error(TGETENT_ERR,
641 "TERM environment must be <= %d characters.\n",
645 T(("your terminal name is %s", tname));
648 * Allow output redirection. This is what SVr3 does. If stdout is
649 * directed to a file, screen updates go to standard error.
651 if (Filedes == STDOUT_FILENO && !isatty(Filedes))
652 Filedes = STDERR_FILENO;
655 * Check if we have already initialized to use this terminal. If so, we
656 * do not need to re-read the terminfo entry, or obtain TTY settings.
658 * This is an improvement on SVr4 curses. If an application mixes curses
659 * and termcap calls, it may call both initscr and tgetent. This is not
660 * really a good thing to do, but can happen if someone tries using ncurses
661 * with the readline library. The problem we are fixing is that when
662 * tgetent calls setupterm, the resulting Ottyb struct in cur_term is
663 * zeroed. A subsequent call to endwin uses the zeroed terminal settings
664 * rather than the ones saved in initscr. So we check if cur_term appears
665 * to contain terminal settings for the same output file as our current
666 * call - and copy those terminal settings. (SVr4 curses does not do this,
667 * however applications that are working around the problem will still work
668 * properly with this feature).
672 && termp->Filedes == Filedes
673 && termp->_termname != 0
674 && !strcmp(termp->_termname, tname)
675 && _nc_name_match(termp->type.term_names, tname, "|")) {
676 T(("reusing existing terminal information and mode-settings"));
679 #ifdef USE_TERM_DRIVER
680 termp = (TERMINAL *) typeCalloc(TERMINAL_CONTROL_BLOCK, 1);
682 termp = typeCalloc(TERMINAL, 1);
685 ret_error0(TGETENT_ERR,
686 "Not enough memory to create terminal structure.\n");
688 #ifdef USE_TERM_DRIVER
690 TCB = (TERMINAL_CONTROL_BLOCK *) termp;
691 code = _nc_globals.term_driver(TCB, tname, errret);
693 termp->Filedes = (short) Filedes;
694 termp->_termname = strdup(tname);
696 ret_error0(TGETENT_ERR,
697 "Could not find any driver to handle this terminal.\n");
700 #if USE_DATABASE || USE_TERMCAP
701 status = _nc_setup_tinfo(tname, &termp->type);
706 /* try fallback list if entry on disk */
707 if (status != TGETENT_YES) {
708 const TERMTYPE *fallback = _nc_fallback(tname);
711 _nc_copy_termtype(&(termp->type), fallback);
712 status = TGETENT_YES;
716 if (status != TGETENT_YES) {
718 if (status == TGETENT_ERR) {
719 ret_error0(status, "terminals database is inaccessible\n");
720 } else if (status == TGETENT_NO) {
721 ret_error1(status, "unknown terminal type.\n", tname);
725 strncpy(ttytype, termp->type.term_names, (size_t) (NAMESIZE - 1));
726 ttytype[NAMESIZE - 1] = '\0';
729 termp->Filedes = (short) Filedes;
730 termp->_termname = strdup(tname);
734 if (command_character)
735 _nc_tinfo_cmdch(termp, UChar(*command_character));
738 * If an application calls setupterm() rather than initscr() or
739 * newterm(), we will not have the def_prog_mode() call in
740 * _nc_setupscreen(). Do it now anyway, so we can initialize the
743 if (isatty(Filedes)) {
751 #ifdef USE_TERM_DRIVER
753 NCURSES_SP_NAME(set_curterm) (sp, termp);
760 * We should always check the screensize, just in case.
762 TINFO_GET_SIZE(sp, termp, ptrLines(sp), ptrCols(sp));
765 *errret = TGETENT_YES;
767 #ifndef USE_TERM_DRIVER
770 * BSD 4.3's termcap contains mis-typed "gn" for wy99. Do a sanity
771 * check before giving up.
773 if ((VALID_STRING(cursor_address)
774 || (VALID_STRING(cursor_down) && VALID_STRING(cursor_home)))
775 && VALID_STRING(clear_screen)) {
776 ret_error1(TGETENT_YES, "terminal is not really generic.\n", tname);
778 ret_error1(TGETENT_NO, "I need something more specific.\n", tname);
780 } else if (hard_copy) {
781 ret_error1(TGETENT_YES, "I can't handle hardcopy terminals.\n", tname);
789 * In case of handling multiple screens, we need to have a screen before
790 * initialization in setupscreen takes place. This is to extend the substitute
791 * for some of the stuff in _nc_prescreen, especially for slk and ripoff
792 * handling which should be done per screen.
794 NCURSES_EXPORT(SCREEN *)
800 T((T_CALLED("new_prescr()")));
803 sp = _nc_alloc_screen_sp();
805 sp->rsp = sp->rippedoff;
806 sp->_filtered = _nc_prescreen.filter_mode;
807 sp->_use_env = _nc_prescreen.use_env;
808 #if NCURSES_NO_PADDING
809 sp->_no_padding = _nc_prescreen._no_padding;
813 sp->_prescreen = TRUE;
816 sp->_TABSIZE = _nc_prescreen._TABSIZE;
817 sp->_ESCDELAY = _nc_prescreen._ESCDELAY;
825 #ifdef USE_TERM_DRIVER
827 * This entrypoint is called from tgetent() to allow a special case of reusing
828 * the same TERMINAL data (see comment).
831 _nc_setupterm(NCURSES_CONST char *tname,
838 res = TINFO_SETUP_TERM(&termp, tname, Filedes, errret, reuse);
840 NCURSES_SP_NAME(set_curterm) (CURRENT_SCREEN_PRE, termp);
846 * setupterm(termname, Filedes, errret)
848 * Find and read the appropriate object file for the terminal
849 * Make cur_term point to the structure.
852 setupterm(NCURSES_CONST char *tname, int Filedes, int *errret)
854 return _nc_setupterm(tname, Filedes, errret, FALSE);