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:
40 * setupterm(char *, int, int *)
43 #include <curses.priv.h>
44 #include <tic.h> /* for MAX_NAME_SIZE */
50 MODULE_ID("$Id: lib_setup.c,v 1.145 2012/07/07 20:35:27 tom Exp $")
52 /****************************************************************************
54 * Terminal size computation
56 ****************************************************************************/
59 # if !defined(sun) || !TERMIOS
61 # include <sys/ioctl.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).
70 # include <sys/stream.h>
71 # include <sys/ptem.h>
74 #if HAVE_LANGINFO_CODESET
79 * SCO defines TIOCGSIZE and the corresponding struct. Other systems (SunOS,
80 * Solaris, IRIX) define TIOCGWINSZ and struct winsize.
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
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
97 * Reduce explicit use of "cur_term" global variable.
100 #define CUR termp->type.
103 * Wrap global variables in this module.
107 NCURSES_EXPORT(char *)
108 NCURSES_PUBLIC_VAR(ttytype) (void)
110 static char empty[] = "";
111 char *result = empty;
114 if (CURRENT_SCREEN) {
115 TERMINAL *termp = TerminalOf(CURRENT_SCREEN);
117 result = termp->type.term_names;
122 result = cur_term->type.term_names;
128 NCURSES_EXPORT(int *)
129 _nc_ptr_Lines(SCREEN *sp)
135 NCURSES_PUBLIC_VAR(LINES) (void)
137 return *_nc_ptr_Lines(CURRENT_SCREEN);
140 NCURSES_EXPORT(int *)
141 _nc_ptr_Cols(SCREEN *sp)
147 NCURSES_PUBLIC_VAR(COLS) (void)
149 return *_nc_ptr_Cols(CURRENT_SCREEN);
152 NCURSES_EXPORT(int *)
153 _nc_ptr_Tabsize(SCREEN *sp)
155 return ptrTabsize(sp);
159 NCURSES_PUBLIC_VAR(TABSIZE) (void)
161 return *_nc_ptr_Tabsize(CURRENT_SCREEN);
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;
170 #if NCURSES_EXT_FUNCS
172 NCURSES_SP_NAME(set_tabsize) (NCURSES_SP_DCLx int value)
177 SP_PARM->_TABSIZE = value;
190 set_tabsize(int value)
192 return NCURSES_SP_NAME(set_tabsize) (CURRENT_SCREEN, value);
195 #endif /* NCURSES_EXT_FUNCS */
199 * If we have a pending SIGWINCH, set the flag in each screen.
202 _nc_handle_sigwinch(SCREEN *sp)
206 if (_nc_globals.have_sigwinch) {
207 _nc_globals.have_sigwinch = 0;
209 for (each_screen(scan)) {
210 scan->_sig_winch = TRUE;
214 return (sp ? sp->_sig_winch : 0);
220 NCURSES_SP_NAME(use_env) (NCURSES_SP_DCLx bool f)
222 T((T_CALLED("use_env(%p,%d)"), (void *) SP_PARM, (int) f));
225 if (IsPreScreen(SP_PARM)) {
226 SP_PARM->_use_env = f;
229 _nc_prescreen.use_env = f;
238 T((T_CALLED("use_env(%d)"), (int) f));
240 _nc_prescreen.use_env = f;
246 _nc_get_screensize(SCREEN *sp,
247 #ifdef USE_TERM_DRIVER
250 int *linep, int *colp)
251 /* Obtain lines/columns values from the environment and/or terminfo entry */
253 #ifdef USE_TERM_DRIVER
254 TERMINAL_CONTROL_BLOCK *TCB;
257 assert(termp != 0 && linep != 0 && colp != 0);
258 TCB = (TERMINAL_CONTROL_BLOCK *) termp;
260 my_tabsize = TCB->info.tabsize;
261 TCB->drv->size(TCB, linep, colp);
265 sp->_TABSIZE = my_tabsize;
269 TABSIZE = my_tabsize;
271 T(("TABSIZE = %d", my_tabsize));
272 #else /* !USE_TERM_DRIVER */
273 TERMINAL *termp = cur_term;
276 /* figure out the size of the screen */
277 T(("screen size: terminfo lines = %d columns = %d", lines, columns));
279 *linep = (int) lines;
280 *colp = (int) columns;
282 if (_nc_prescreen.use_env) {
288 _scrsize(screendata);
289 *colp = screendata[0];
290 *linep = screendata[1];
291 T(("EMX screen size: environment LINES = %d COLUMNS = %d",
296 /* try asking the OS */
297 if (isatty(cur_term->Filedes)) {
302 if (ioctl(cur_term->Filedes, IOCTL_WINSIZE, &size) >= 0) {
303 *linep = ((sp != 0 && sp->_filtered)
305 : WINSIZE_ROWS(size));
306 *colp = WINSIZE_COLS(size);
307 T(("SYS screen size: environment LINES = %d COLUMNS = %d",
314 #endif /* HAVE_SIZECHANGE */
317 * Finally, look for environment variables.
319 * Solaris lets users override either dimension with an environment
322 if ((value = _nc_getenv_num("LINES")) > 0) {
324 T(("screen size: environment LINES = %d", *linep));
326 if ((value = _nc_getenv_num("COLUMNS")) > 0) {
328 T(("screen size: environment COLUMNS = %d", *colp));
331 /* if we can't get dynamic info about the size, use static */
333 *linep = (int) lines;
336 *colp = (int) columns;
339 /* the ultimate fallback, assume fixed 24x80 size */
348 * Put the derived values back in the screen-size caps, so
349 * tigetnum() and tgetnum() will do the right thing.
351 lines = (short) (*linep);
352 columns = (short) (*colp);
355 T(("screen size is %dx%d", *linep, *colp));
357 if (VALID_NUMERIC(init_tabs))
358 my_tabsize = (int) init_tabs;
364 sp->_TABSIZE = my_tabsize;
366 TABSIZE = my_tabsize;
368 T(("TABSIZE = %d", TABSIZE));
369 #endif /* USE_TERM_DRIVER */
374 _nc_update_screensize(SCREEN *sp)
379 #ifdef USE_TERM_DRIVER
385 CallDriver_2(sp, getsize, &old_lines, &old_cols);
388 TERMINAL *termp = cur_term;
389 int old_lines = lines;
390 int old_cols = columns;
393 TINFO_GET_SIZE(sp, sp->_term, &new_lines, &new_cols);
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.
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 */
407 sp->_sig_winch = FALSE;
412 /****************************************************************************
416 ****************************************************************************/
418 #if USE_DATABASE || USE_TERMCAP
420 * Return 1 if entry found, 0 if not found, -1 if database not accessible,
421 * just like tgetent().
424 _nc_setup_tinfo(const char *const tn, TERMTYPE *const tp)
426 char filename[PATH_MAX];
427 int status = _nc_read_entry(tn, filename, tp);
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).
435 if (status == TGETENT_YES) {
437 for_each_boolean(n, tp) {
438 if (!VALID_BOOLEAN(tp->Booleans[n]))
439 tp->Booleans[n] = FALSE;
441 for_each_string(n, tp) {
442 if (tp->Strings[n] == CANCELLED_STRING)
443 tp->Strings[n] = ABSENT_STRING;
451 ** Take the real command character out of the CC environment variable
452 ** and substitute it in for the prototype given in 'command_character'.
455 _nc_tinfo_cmdch(TERMINAL * termp, int proto)
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.
466 if ((tmp = getenv("CC")) != 0 && strlen(tmp) == 1) {
468 for_each_string(i, &(termp->type)) {
469 for (tmp = termp->type.Strings[i]; tmp && *tmp; tmp++) {
470 if (UChar(*tmp) == proto)
478 * Find the locale which is in effect.
480 NCURSES_EXPORT(char *)
486 * This is preferable to using getenv() since it ensures that we are using
487 * the locale which was actually initialized by the application.
489 env = setlocale(LC_CTYPE, 0);
491 if (((env = getenv("LC_ALL")) != 0 && *env != '\0')
492 || ((env = getenv("LC_CTYPE")) != 0 && *env != '\0')
493 || ((env = getenv("LANG")) != 0 && *env != '\0')) {
497 T(("_nc_get_locale %s", _nc_visbuf(env)));
502 * Check if we are running in a UTF-8 locale.
505 _nc_unicode_locale(void)
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));
513 char *env = _nc_get_locale();
515 if (strstr(env, ".UTF-8") != 0) {
517 T(("_nc_unicode_locale(%s) ->%d", env, result));
524 #define CONTROL_N(s) ((s) != 0 && strstr(s, "\016") != 0)
525 #define CONTROL_O(s) ((s) != 0 && strstr(s, "\017") != 0)
528 * Check for known broken cases where a UTF-8 locale breaks the alternate
532 _nc_locale_breaks_acs(TERMINAL * termp)
534 const char *env_name = "NCURSES_NO_UTF8_ACS";
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)) {
562 TINFO_SETUP_TERM(TERMINAL ** tp,
563 NCURSES_CONST char *tname,
568 #ifdef USE_TERM_DRIVER
569 TERMINAL_CONTROL_BLOCK *TCB = 0;
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));
584 ret_error0(TGETENT_ERR,
585 "Invalid parameter, internal error.\n");
590 T((T_CALLED("setupterm(%s,%d,%p)"), _nc_visbuf(tname), Filedes, (void *) errret));
594 tname = getenv("TERM");
595 if (tname == 0 || *tname == '\0') {
596 ret_error0(TGETENT_ERR, "TERM environment variable not set.\n");
600 if (strlen(tname) > MAX_NAME_SIZE) {
601 ret_error(TGETENT_ERR,
602 "TERM environment must be <= %d characters.\n",
606 T(("your terminal name is %s", tname));
609 * Allow output redirection. This is what SVr3 does. If stdout is
610 * directed to a file, screen updates go to standard error.
612 if (Filedes == STDOUT_FILENO && !isatty(Filedes))
613 Filedes = STDERR_FILENO;
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.
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).
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"));
640 #ifdef USE_TERM_DRIVER
641 termp = (TERMINAL *) typeCalloc(TERMINAL_CONTROL_BLOCK, 1);
643 termp = typeCalloc(TERMINAL, 1);
646 ret_error0(TGETENT_ERR,
647 "Not enough memory to create terminal structure.\n");
649 #ifdef USE_TERM_DRIVER
651 TCB = (TERMINAL_CONTROL_BLOCK *) termp;
652 code = _nc_globals.term_driver(TCB, tname, errret);
654 termp->Filedes = (short) Filedes;
655 termp->_termname = strdup(tname);
657 ret_error0(TGETENT_ERR,
658 "Could not find any driver to handle this terminal.\n");
661 #if USE_DATABASE || USE_TERMCAP
662 status = _nc_setup_tinfo(tname, &termp->type);
667 /* try fallback list if entry on disk */
668 if (status != TGETENT_YES) {
669 const TERMTYPE *fallback = _nc_fallback(tname);
672 _nc_copy_termtype(&(termp->type), fallback);
673 status = TGETENT_YES;
677 if (status != TGETENT_YES) {
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);
686 strncpy(ttytype, termp->type.term_names, (size_t) (NAMESIZE - 1));
687 ttytype[NAMESIZE - 1] = '\0';
690 termp->Filedes = (short) Filedes;
691 termp->_termname = strdup(tname);
695 if (command_character)
696 _nc_tinfo_cmdch(termp, UChar(*command_character));
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
704 if (isatty(Filedes)) {
712 #ifdef USE_TERM_DRIVER
714 NCURSES_SP_NAME(set_curterm) (sp, termp);
721 * We should always check the screensize, just in case.
723 TINFO_GET_SIZE(sp, termp, ptrLines(sp), ptrCols(sp));
726 *errret = TGETENT_YES;
728 #ifndef USE_TERM_DRIVER
731 * BSD 4.3's termcap contains mis-typed "gn" for wy99. Do a sanity
732 * check before giving up.
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);
739 ret_error1(TGETENT_NO, "I need something more specific.\n", tname);
741 } else if (hard_copy) {
742 ret_error1(TGETENT_YES, "I can't handle hardcopy terminals.\n", tname);
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.
755 NCURSES_EXPORT(SCREEN *)
761 T((T_CALLED("new_prescr()")));
764 sp = _nc_alloc_screen_sp();
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;
774 sp->_prescreen = TRUE;
777 sp->_TABSIZE = _nc_prescreen._TABSIZE;
778 sp->_ESCDELAY = _nc_prescreen._ESCDELAY;
786 #ifdef USE_TERM_DRIVER
788 * This entrypoint is called from tgetent() to allow a special case of reusing
789 * the same TERMINAL data (see comment).
792 _nc_setupterm(NCURSES_CONST char *tname,
799 res = TINFO_SETUP_TERM(&termp, tname, Filedes, errret, reuse);
801 NCURSES_SP_NAME(set_curterm) (CURRENT_SCREEN_PRE, termp);
807 * setupterm(termname, Filedes, errret)
809 * Find and read the appropriate object file for the terminal
810 * Make cur_term point to the structure.
813 setupterm(NCURSES_CONST char *tname, int Filedes, int *errret)
815 return _nc_setupterm(tname, Filedes, errret, FALSE);