1 /****************************************************************************
2 * Copyright (c) 1998-2010,2011 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:
40 * setupterm(char *, int, int *)
43 #include <curses.priv.h>
44 #include <tic.h> /* for MAX_NAME_SIZE */
46 #if SVR4_TERMIO && !defined(_POSIX_SOURCE)
54 MODULE_ID("$Id: lib_setup.c,v 1.135 2011/02/06 01:04:21 tom Exp $")
56 /****************************************************************************
58 * Terminal size computation
60 ****************************************************************************/
63 # if !defined(sun) || !TERMIOS
65 # include <sys/ioctl.h>
71 /* On SCO, they neglected to define struct winsize in termios.h -- it's only
72 * in termio.h and ptem.h (the former conflicts with other definitions).
74 # include <sys/stream.h>
75 # include <sys/ptem.h>
78 #if HAVE_LANGINFO_CODESET
83 * SCO defines TIOCGSIZE and the corresponding struct. Other systems (SunOS,
84 * Solaris, IRIX) define TIOCGWINSZ and struct winsize.
87 # define IOCTL_WINSIZE TIOCGSIZE
88 # define STRUCT_WINSIZE struct ttysize
89 # define WINSIZE_ROWS(n) (int)n.ts_lines
90 # define WINSIZE_COLS(n) (int)n.ts_cols
93 # define IOCTL_WINSIZE TIOCGWINSZ
94 # define STRUCT_WINSIZE struct winsize
95 # define WINSIZE_ROWS(n) (int)n.ws_row
96 # define WINSIZE_COLS(n) (int)n.ws_col
101 * Reduce explicit use of "cur_term" global variable.
104 #define CUR termp->type.
107 * Wrap global variables in this module.
111 NCURSES_EXPORT(char *)
112 NCURSES_PUBLIC_VAR(ttytype) (void)
114 static char empty[] = "";
115 char *result = empty;
118 if (CURRENT_SCREEN) {
119 TERMINAL *termp = TerminalOf(CURRENT_SCREEN);
121 result = termp->type.term_names;
126 result = cur_term->type.term_names;
132 NCURSES_EXPORT(int *)
133 _nc_ptr_Lines(SCREEN *sp)
139 NCURSES_PUBLIC_VAR(LINES) (void)
141 return *_nc_ptr_Lines(CURRENT_SCREEN);
144 NCURSES_EXPORT(int *)
145 _nc_ptr_Cols(SCREEN *sp)
151 NCURSES_PUBLIC_VAR(COLS) (void)
153 return *_nc_ptr_Cols(CURRENT_SCREEN);
156 NCURSES_EXPORT(int *)
157 _nc_ptr_Tabsize(SCREEN *sp)
159 return ptrTabsize(sp);
163 NCURSES_PUBLIC_VAR(TABSIZE) (void)
165 return *_nc_ptr_Tabsize(CURRENT_SCREEN);
168 NCURSES_EXPORT_VAR(char) ttytype[NAMESIZE] = "";
169 NCURSES_EXPORT_VAR(int) LINES = 0;
170 NCURSES_EXPORT_VAR(int) COLS = 0;
171 NCURSES_EXPORT_VAR(int) TABSIZE = 8;
174 #if NCURSES_EXT_FUNCS
176 NCURSES_SP_NAME(set_tabsize) (NCURSES_SP_DCLx int value)
181 SP_PARM->_TABSIZE = value;
194 set_tabsize(int value)
196 return NCURSES_SP_NAME(set_tabsize) (CURRENT_SCREEN, value);
199 #endif /* NCURSES_EXT_FUNCS */
203 * If we have a pending SIGWINCH, set the flag in each screen.
206 _nc_handle_sigwinch(SCREEN *sp)
210 if (_nc_globals.have_sigwinch) {
211 _nc_globals.have_sigwinch = 0;
213 for (each_screen(scan)) {
214 scan->_sig_winch = TRUE;
218 return (sp ? sp->_sig_winch : 0);
224 NCURSES_SP_NAME(use_env) (NCURSES_SP_DCLx bool f)
226 T((T_CALLED("use_env(%p,%d)"), (void *) SP_PARM, (int) f));
228 if (IsPreScreen(SP_PARM)) {
229 SP_PARM->_use_env = f;
232 _nc_prescreen.use_env = f;
241 T((T_CALLED("use_env(%d)"), (int) f));
242 _nc_prescreen.use_env = f;
248 _nc_get_screensize(SCREEN *sp,
249 #ifdef USE_TERM_DRIVER
252 int *linep, int *colp)
253 /* Obtain lines/columns values from the environment and/or terminfo entry */
255 #ifdef USE_TERM_DRIVER
256 TERMINAL_CONTROL_BLOCK *TCB;
259 assert(termp != 0 && linep != 0 && colp != 0);
260 TCB = (TERMINAL_CONTROL_BLOCK *) termp;
262 my_tabsize = TCB->info.tabsize;
263 TCB->drv->size(TCB, linep, colp);
267 sp->_TABSIZE = my_tabsize;
271 TABSIZE = my_tabsize;
273 T(("TABSIZE = %d", my_tabsize));
274 #else /* !USE_TERM_DRIVER */
275 TERMINAL *termp = cur_term;
278 /* figure out the size of the screen */
279 T(("screen size: terminfo lines = %d columns = %d", lines, columns));
281 *linep = (int) lines;
282 *colp = (int) columns;
284 if (_nc_prescreen.use_env) {
290 _scrsize(screendata);
291 *colp = screendata[0];
292 *linep = screendata[1];
293 T(("EMX screen size: environment LINES = %d COLUMNS = %d",
298 /* try asking the OS */
299 if (isatty(cur_term->Filedes)) {
304 if (ioctl(cur_term->Filedes, IOCTL_WINSIZE, &size) >= 0) {
305 *linep = ((sp != 0 && sp->_filtered)
307 : WINSIZE_ROWS(size));
308 *colp = WINSIZE_COLS(size);
309 T(("SYS screen size: environment LINES = %d COLUMNS = %d",
316 #endif /* HAVE_SIZECHANGE */
319 * Finally, look for environment variables.
321 * Solaris lets users override either dimension with an environment
324 if ((value = _nc_getenv_num("LINES")) > 0) {
326 T(("screen size: environment LINES = %d", *linep));
328 if ((value = _nc_getenv_num("COLUMNS")) > 0) {
330 T(("screen size: environment COLUMNS = %d", *colp));
333 /* if we can't get dynamic info about the size, use static */
335 *linep = (int) lines;
338 *colp = (int) columns;
341 /* the ultimate fallback, assume fixed 24x80 size */
350 * Put the derived values back in the screen-size caps, so
351 * tigetnum() and tgetnum() will do the right thing.
353 lines = (short) (*linep);
354 columns = (short) (*colp);
357 T(("screen size is %dx%d", *linep, *colp));
359 if (VALID_NUMERIC(init_tabs))
360 my_tabsize = (int) init_tabs;
366 sp->_TABSIZE = my_tabsize;
368 TABSIZE = my_tabsize;
370 T(("TABSIZE = %d", TABSIZE));
371 #endif /* USE_TERM_DRIVER */
376 _nc_update_screensize(SCREEN *sp)
381 #ifdef USE_TERM_DRIVER
387 CallDriver_2(sp, getsize, &old_lines, &old_cols);
390 TERMINAL *termp = cur_term;
391 int old_lines = lines;
392 int old_cols = columns;
395 TINFO_GET_SIZE(sp, sp->_term, &new_lines, &new_cols);
398 * See is_term_resized() and resizeterm().
399 * We're doing it this way because those functions belong to the upper
400 * ncurses library, while this resides in the lower terminfo library.
403 && sp->_resize != 0) {
404 if ((new_lines != old_lines) || (new_cols != old_cols))
405 sp->_resize(NCURSES_SP_ARGx new_lines, new_cols);
406 sp->_sig_winch = FALSE;
411 /****************************************************************************
415 ****************************************************************************/
417 #define ret_error(code, fmt, arg) if (errret) {\
421 fprintf(stderr, fmt, arg);\
425 #define ret_error0(code, msg) if (errret) {\
429 fprintf(stderr, msg);\
433 #if USE_DATABASE || USE_TERMCAP
435 * Return 1 if entry found, 0 if not found, -1 if database not accessible,
436 * just like tgetent().
439 _nc_setup_tinfo(const char *const tn, TERMTYPE *const tp)
441 char filename[PATH_MAX];
442 int status = _nc_read_entry(tn, filename, tp);
445 * If we have an entry, force all of the cancelled strings to null
446 * pointers so we don't have to test them in the rest of the library.
447 * (The terminfo compiler bypasses this logic, since it must know if
448 * a string is cancelled, for merging entries).
450 if (status == TGETENT_YES) {
452 for_each_boolean(n, tp) {
453 if (!VALID_BOOLEAN(tp->Booleans[n]))
454 tp->Booleans[n] = FALSE;
456 for_each_string(n, tp) {
457 if (tp->Strings[n] == CANCELLED_STRING)
458 tp->Strings[n] = ABSENT_STRING;
466 ** Take the real command character out of the CC environment variable
467 ** and substitute it in for the prototype given in 'command_character'.
470 _nc_tinfo_cmdch(TERMINAL * termp, char proto)
477 * Only use the character if the string is a single character,
478 * since it is fairly common for developers to set the C compiler
479 * name as an environment variable - using the same symbol.
481 if ((tmp = getenv("CC")) != 0 && strlen(tmp) == 1) {
483 for_each_string(i, &(termp->type)) {
484 for (tmp = termp->type.Strings[i]; *tmp; tmp++) {
493 * Find the locale which is in effect.
495 NCURSES_EXPORT(char *)
501 * This is preferable to using getenv() since it ensures that we are using
502 * the locale which was actually initialized by the application.
504 env = setlocale(LC_CTYPE, 0);
506 if (((env = getenv("LC_ALL")) != 0 && *env != '\0')
507 || ((env = getenv("LC_CTYPE")) != 0 && *env != '\0')
508 || ((env = getenv("LANG")) != 0 && *env != '\0')) {
512 T(("_nc_get_locale %s", _nc_visbuf(env)));
517 * Check if we are running in a UTF-8 locale.
520 _nc_unicode_locale(void)
523 #if HAVE_LANGINFO_CODESET
524 char *env = nl_langinfo(CODESET);
525 result = !strcmp(env, "UTF-8");
526 T(("_nc_unicode_locale(%s) ->%d", env, result));
528 char *env = _nc_get_locale();
530 if (strstr(env, ".UTF-8") != 0) {
532 T(("_nc_unicode_locale(%s) ->%d", env, result));
539 #define CONTROL_N(s) ((s) != 0 && strstr(s, "\016") != 0)
540 #define CONTROL_O(s) ((s) != 0 && strstr(s, "\017") != 0)
543 * Check for known broken cases where a UTF-8 locale breaks the alternate
547 _nc_locale_breaks_acs(TERMINAL * termp)
549 const char *env_name = "NCURSES_NO_UTF8_ACS";
554 if ((env = getenv(env_name)) != 0) {
555 result = _nc_getenv_num(env_name);
556 } else if ((value = tigetnum("U8")) >= 0) {
557 result = value; /* use extension feature */
558 } else if ((env = getenv("TERM")) != 0) {
559 if (strstr(env, "linux")) {
560 result = 1; /* always broken */
561 } else if (strstr(env, "screen") != 0
562 && ((env = getenv("TERMCAP")) != 0
563 && strstr(env, "screen") != 0)
564 && strstr(env, "hhII00") != 0) {
565 if (CONTROL_N(enter_alt_charset_mode) ||
566 CONTROL_O(enter_alt_charset_mode) ||
567 CONTROL_N(set_attributes) ||
568 CONTROL_O(set_attributes)) {
577 TINFO_SETUP_TERM(TERMINAL ** tp,
578 NCURSES_CONST char *tname,
583 #ifdef USE_TERM_DRIVER
584 TERMINAL_CONTROL_BLOCK *TCB = 0;
594 #ifdef USE_TERM_DRIVER
595 T((T_CALLED("_nc_setupterm_ex(%p,%s,%d,%p)"),
596 (void *) tp, _nc_visbuf(tname), Filedes, (void *) errret));
599 ret_error0(TGETENT_ERR,
600 "Invalid parameter, internal error.\n");
605 T((T_CALLED("setupterm(%s,%d,%p)"), _nc_visbuf(tname), Filedes, (void *) errret));
609 tname = getenv("TERM");
610 if (tname == 0 || *tname == '\0') {
611 ret_error0(TGETENT_ERR, "TERM environment variable not set.\n");
615 if (strlen(tname) > MAX_NAME_SIZE) {
616 ret_error(TGETENT_ERR,
617 "TERM environment must be <= %d characters.\n",
621 T(("your terminal name is %s", tname));
624 * Allow output redirection. This is what SVr3 does. If stdout is
625 * directed to a file, screen updates go to standard error.
627 if (Filedes == STDOUT_FILENO && !isatty(Filedes))
628 Filedes = STDERR_FILENO;
631 * Check if we have already initialized to use this terminal. If so, we
632 * do not need to re-read the terminfo entry, or obtain TTY settings.
634 * This is an improvement on SVr4 curses. If an application mixes curses
635 * and termcap calls, it may call both initscr and tgetent. This is not
636 * really a good thing to do, but can happen if someone tries using ncurses
637 * with the readline library. The problem we are fixing is that when
638 * tgetent calls setupterm, the resulting Ottyb struct in cur_term is
639 * zeroed. A subsequent call to endwin uses the zeroed terminal settings
640 * rather than the ones saved in initscr. So we check if cur_term appears
641 * to contain terminal settings for the same output file as our current
642 * call - and copy those terminal settings. (SVr4 curses does not do this,
643 * however applications that are working around the problem will still work
644 * properly with this feature).
648 && termp->Filedes == Filedes
649 && termp->_termname != 0
650 && !strcmp(termp->_termname, tname)
651 && _nc_name_match(termp->type.term_names, tname, "|")) {
652 T(("reusing existing terminal information and mode-settings"));
655 #ifdef USE_TERM_DRIVER
656 termp = (TERMINAL *) typeCalloc(TERMINAL_CONTROL_BLOCK, 1);
658 termp = typeCalloc(TERMINAL, 1);
661 ret_error0(TGETENT_ERR,
662 "Not enough memory to create terminal structure.\n");
664 #ifdef USE_TERM_DRIVER
666 TCB = (TERMINAL_CONTROL_BLOCK *) termp;
667 code = _nc_globals.term_driver(TCB, tname, errret);
669 termp->Filedes = (short) Filedes;
670 termp->_termname = strdup(tname);
672 ret_error0(TGETENT_ERR,
673 "Could not find any driver to handle this terminal.\n");
676 #if USE_DATABASE || USE_TERMCAP
677 status = _nc_setup_tinfo(tname, &termp->type);
682 /* try fallback list if entry on disk */
683 if (status != TGETENT_YES) {
684 const TERMTYPE *fallback = _nc_fallback(tname);
687 termp->type = *fallback;
688 status = TGETENT_YES;
692 if (status != TGETENT_YES) {
694 if (status == TGETENT_ERR) {
695 ret_error0(status, "terminals database is inaccessible\n");
696 } else if (status == TGETENT_NO) {
697 ret_error(status, "'%s': unknown terminal type.\n", tname);
701 strncpy(ttytype, termp->type.term_names, NAMESIZE - 1);
702 ttytype[NAMESIZE - 1] = '\0';
705 termp->Filedes = (short) Filedes;
706 termp->_termname = strdup(tname);
710 if (command_character)
711 _nc_tinfo_cmdch(termp, *command_character);
714 * If an application calls setupterm() rather than initscr() or
715 * newterm(), we will not have the def_prog_mode() call in
716 * _nc_setupscreen(). Do it now anyway, so we can initialize the
719 if (isatty(Filedes)) {
727 #ifdef USE_TERM_DRIVER
729 NCURSES_SP_NAME(set_curterm) (sp, termp);
736 * We should always check the screensize, just in case.
738 TINFO_GET_SIZE(sp, termp, ptrLines(sp), ptrCols(sp));
741 *errret = TGETENT_YES;
743 #ifndef USE_TERM_DRIVER
745 ret_error(TGETENT_NO, "'%s': I need something more specific.\n", tname);
748 ret_error(TGETENT_YES, "'%s': I can't handle hardcopy terminals.\n", tname);
756 * In case of handling multiple screens, we need to have a screen before
757 * initialization in setupscreen takes place. This is to extend the substitute
758 * for some of the stuff in _nc_prescreen, especially for slk and ripoff
759 * handling which should be done per screen.
761 NCURSES_EXPORT(SCREEN *)
767 T((T_CALLED("new_prescr()")));
770 sp = _nc_alloc_screen_sp();
772 sp->rsp = sp->rippedoff;
773 sp->_filtered = _nc_prescreen.filter_mode;
774 sp->_use_env = _nc_prescreen.use_env;
775 #if NCURSES_NO_PADDING
776 sp->_no_padding = _nc_prescreen._no_padding;
780 sp->_prescreen = TRUE;
783 sp->_TABSIZE = _nc_prescreen._TABSIZE;
784 sp->_ESCDELAY = _nc_prescreen._ESCDELAY;
792 #ifdef USE_TERM_DRIVER
794 * This entrypoint is called from tgetent() to allow a special case of reusing
795 * the same TERMINAL data (see comment).
798 _nc_setupterm(NCURSES_CONST char *tname,
805 res = TINFO_SETUP_TERM(&termp, tname, Filedes, errret, reuse);
807 NCURSES_SP_NAME(set_curterm) (CURRENT_SCREEN_PRE, termp);
813 * setupterm(termname, Filedes, errret)
815 * Find and read the appropriate object file for the terminal
816 * Make cur_term point to the structure.
819 setupterm(NCURSES_CONST char *tname, int Filedes, int *errret)
821 return _nc_setupterm(tname, Filedes, errret, FALSE);