1 /****************************************************************************
2 * Copyright (c) 1998-2008,2009 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 */
45 #include <term_entry.h>
47 #if SVR4_TERMIO && !defined(_POSIX_SOURCE)
55 MODULE_ID("$Id: lib_setup.c,v 1.123 2009/10/31 20:39:36 tom Exp $")
57 /****************************************************************************
59 * Terminal size computation
61 ****************************************************************************/
64 # if !defined(sun) || !TERMIOS
66 # include <sys/ioctl.h>
72 /* On SCO, they neglected to define struct winsize in termios.h -- it's only
73 * in termio.h and ptem.h (the former conflicts with other definitions).
75 # include <sys/stream.h>
76 # include <sys/ptem.h>
79 #if HAVE_LANGINFO_CODESET
84 * SCO defines TIOCGSIZE and the corresponding struct. Other systems (SunOS,
85 * Solaris, IRIX) define TIOCGWINSZ and struct winsize.
88 # define IOCTL_WINSIZE TIOCGSIZE
89 # define STRUCT_WINSIZE struct ttysize
90 # define WINSIZE_ROWS(n) (int)n.ts_lines
91 # define WINSIZE_COLS(n) (int)n.ts_cols
94 # define IOCTL_WINSIZE TIOCGWINSZ
95 # define STRUCT_WINSIZE struct winsize
96 # define WINSIZE_ROWS(n) (int)n.ws_row
97 # define WINSIZE_COLS(n) (int)n.ws_col
102 * Reduce explicit use of "cur_term" global variable.
105 #define CUR termp->type.
108 * Wrap global variables in this module.
112 NCURSES_EXPORT(char *)
113 NCURSES_PUBLIC_VAR(ttytype) (void)
115 static char empty[] = "";
116 char *result = empty;
119 if (CURRENT_SCREEN) {
120 TERMINAL *termp = TerminalOf(CURRENT_SCREEN);
122 result = termp->type.term_names;
127 result = cur_term->type.term_names;
133 NCURSES_EXPORT(int *)
134 _nc_ptr_Lines(SCREEN *sp)
140 NCURSES_PUBLIC_VAR(LINES) (void)
142 return *_nc_ptr_Lines(CURRENT_SCREEN);
145 NCURSES_EXPORT(int *)
146 _nc_ptr_Cols(SCREEN *sp)
152 NCURSES_PUBLIC_VAR(COLS) (void)
154 return *_nc_ptr_Cols(CURRENT_SCREEN);
157 NCURSES_EXPORT(int *)
158 _nc_ptr_Tabsize(SCREEN *sp)
160 return ptrTabsize(sp);
164 NCURSES_PUBLIC_VAR(TABSIZE) (void)
166 return *_nc_ptr_Tabsize(CURRENT_SCREEN);
169 NCURSES_EXPORT_VAR(char) ttytype[NAMESIZE] = "";
170 NCURSES_EXPORT_VAR(int) LINES = 0;
171 NCURSES_EXPORT_VAR(int) COLS = 0;
172 NCURSES_EXPORT_VAR(int) TABSIZE = 8;
175 #if NCURSES_EXT_FUNCS
177 NCURSES_SP_NAME(set_tabsize) (NCURSES_SP_DCLx int value)
182 SP_PARM->_TABSIZE = value;
195 set_tabsize(int value)
197 return NCURSES_SP_NAME(set_tabsize) (CURRENT_SCREEN, value);
200 #endif /* NCURSES_EXT_FUNCS */
204 * If we have a pending SIGWINCH, set the flag in each screen.
207 _nc_handle_sigwinch(SCREEN *sp)
211 if (_nc_globals.have_sigwinch) {
212 _nc_globals.have_sigwinch = 0;
214 for (each_screen(scan)) {
215 scan->_sig_winch = TRUE;
219 return (sp ? sp->_sig_winch : 0);
225 NCURSES_SP_NAME(use_env) (NCURSES_SP_DCLx bool f)
227 T((T_CALLED("use_env(%p,%d)"), (void *) SP_PARM, (int) f));
229 if (IsPreScreen(SP_PARM)) {
230 SP_PARM->_use_env = f;
233 _nc_prescreen.use_env = f;
242 T((T_CALLED("use_env(%d)"), (int) f));
243 _nc_prescreen.use_env = f;
249 _nc_get_screensize(SCREEN *sp,
250 #ifdef USE_TERM_DRIVER
253 int *linep, int *colp)
254 /* Obtain lines/columns values from the environment and/or terminfo entry */
256 #ifdef USE_TERM_DRIVER
257 TERMINAL_CONTROL_BLOCK *TCB;
260 assert(termp != 0 && linep != 0 && colp != 0);
261 TCB = (TERMINAL_CONTROL_BLOCK *) termp;
263 my_tabsize = TCB->info.tabsize;
264 TCB->drv->size(TCB, linep, colp);
268 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 if (!_nc_prescreen.use_env) {
282 *linep = (int) lines;
283 *colp = (int) columns;
284 } else { /* usually want to query LINES and COLUMNS from environment */
289 /* first, look for environment variables */
290 if ((value = _nc_getenv_num("LINES")) > 0) {
293 if ((value = _nc_getenv_num("COLUMNS")) > 0) {
296 T(("screen size: environment LINES = %d COLUMNS = %d", *linep, *colp));
299 if (*linep <= 0 || *colp <= 0) {
301 _scrsize(screendata);
302 *colp = screendata[0];
303 *linep = screendata[1];
304 T(("EMX screen size: environment LINES = %d COLUMNS = %d",
309 /* if that didn't work, maybe we can try asking the OS */
310 if (*linep <= 0 || *colp <= 0) {
311 if (isatty(cur_term->Filedes)) {
316 if (ioctl(cur_term->Filedes, IOCTL_WINSIZE, &size) < 0
323 * Solaris lets users override either dimension with an
324 * environment variable.
327 *linep = (sp != 0 && sp->_filtered) ? 1 : WINSIZE_ROWS(size);
329 *colp = WINSIZE_COLS(size);
334 #endif /* HAVE_SIZECHANGE */
336 /* if we can't get dynamic info about the size, use static */
338 *linep = (int) lines;
341 *colp = (int) columns;
344 /* the ultimate fallback, assume fixed 24x80 size */
353 * Put the derived values back in the screen-size caps, so
354 * tigetnum() and tgetnum() will do the right thing.
356 lines = (short) (*linep);
357 columns = (short) (*colp);
360 T(("screen size is %dx%d", *linep, *colp));
362 if (VALID_NUMERIC(init_tabs))
363 my_tabsize = (int) init_tabs;
369 sp->_TABSIZE = my_tabsize;
371 TABSIZE = my_tabsize;
373 T(("TABSIZE = %d", TABSIZE));
374 #endif /* USE_TERM_DRIVER */
379 _nc_update_screensize(SCREEN *sp)
384 #ifdef USE_TERM_DRIVER
390 CallDriver_2(sp, getsize, &old_lines, &old_cols);
393 TERMINAL *termp = cur_term;
394 int old_lines = lines;
395 int old_cols = columns;
398 TINFO_GET_SIZE(sp, sp->_term, &new_lines, &new_cols);
401 * See is_term_resized() and resizeterm().
402 * We're doing it this way because those functions belong to the upper
403 * ncurses library, while this resides in the lower terminfo library.
406 && sp->_resize != 0) {
407 if ((new_lines != old_lines) || (new_cols != old_cols))
408 sp->_resize(NCURSES_SP_ARGx new_lines, new_cols);
409 sp->_sig_winch = FALSE;
414 /****************************************************************************
418 ****************************************************************************/
420 #define ret_error(code, fmt, arg) if (errret) {\
424 fprintf(stderr, fmt, arg);\
428 #define ret_error0(code, msg) if (errret) {\
432 fprintf(stderr, msg);\
436 #ifndef USE_TERM_DRIVER
437 #if USE_DATABASE || USE_TERMCAP
439 * Return 1 if entry found, 0 if not found, -1 if database not accessible,
440 * just like tgetent().
443 grab_entry(const char *const tn, TERMTYPE *const tp)
445 char filename[PATH_MAX];
446 int status = _nc_read_entry(tn, filename, tp);
449 * If we have an entry, force all of the cancelled strings to null
450 * pointers so we don't have to test them in the rest of the library.
451 * (The terminfo compiler bypasses this logic, since it must know if
452 * a string is cancelled, for merging entries).
454 if (status == TGETENT_YES) {
456 for_each_boolean(n, tp) {
457 if (!VALID_BOOLEAN(tp->Booleans[n]))
458 tp->Booleans[n] = FALSE;
460 for_each_string(n, tp) {
461 if (tp->Strings[n] == CANCELLED_STRING)
462 tp->Strings[n] = ABSENT_STRING;
468 #endif /* !USE_TERM_DRIVER */
471 ** Take the real command character out of the CC environment variable
472 ** and substitute it in for the prototype given in 'command_character'.
475 _nc_tinfo_cmdch(TERMINAL * termp, char proto)
482 * Only use the character if the string is a single character,
483 * since it is fairly common for developers to set the C compiler
484 * name as an environment variable - using the same symbol.
486 if ((tmp = getenv("CC")) != 0 && strlen(tmp) == 1) {
488 for_each_string(i, &(termp->type)) {
489 for (tmp = termp->type.Strings[i]; *tmp; tmp++) {
498 * Find the locale which is in effect.
500 NCURSES_EXPORT(char *)
506 * This is preferable to using getenv() since it ensures that we are using
507 * the locale which was actually initialized by the application.
509 env = setlocale(LC_CTYPE, 0);
511 if (((env = getenv("LC_ALL")) != 0 && *env != '\0')
512 || ((env = getenv("LC_CTYPE")) != 0 && *env != '\0')
513 || ((env = getenv("LANG")) != 0 && *env != '\0')) {
517 T(("_nc_get_locale %s", _nc_visbuf(env)));
522 * Check if we are running in a UTF-8 locale.
525 _nc_unicode_locale(void)
528 #if HAVE_LANGINFO_CODESET
529 char *env = nl_langinfo(CODESET);
530 result = !strcmp(env, "UTF-8");
531 T(("_nc_unicode_locale(%s) ->%d", env, result));
533 char *env = _nc_get_locale();
535 if (strstr(env, ".UTF-8") != 0) {
537 T(("_nc_unicode_locale(%s) ->%d", env, result));
544 #define CONTROL_N(s) ((s) != 0 && strstr(s, "\016") != 0)
545 #define CONTROL_O(s) ((s) != 0 && strstr(s, "\017") != 0)
548 * Check for known broken cases where a UTF-8 locale breaks the alternate
552 _nc_locale_breaks_acs(TERMINAL * termp)
556 if ((env = getenv("NCURSES_NO_UTF8_ACS")) != 0) {
558 } else if ((env = getenv("TERM")) != 0) {
559 if (strstr(env, "linux"))
560 return 1; /* always broken */
561 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))
576 TINFO_SETUP_TERM(TERMINAL ** tp,
577 NCURSES_CONST char *tname,
582 #ifdef USE_TERM_DRIVER
583 TERMINAL_CONTROL_BLOCK *TCB = 0;
593 #ifdef USE_TERM_DRIVER
594 T((T_CALLED("_nc_setupterm_ex(%p,%s,%d,%p)"),
595 tp, _nc_visbuf(tname), Filedes, errret));
598 ret_error0(TGETENT_ERR,
599 "Invalid parameter, internal error.\n");
604 T((T_CALLED("setupterm(%s,%d,%p)"), _nc_visbuf(tname), Filedes, (void *) errret));
608 tname = getenv("TERM");
609 if (tname == 0 || *tname == '\0') {
610 ret_error0(TGETENT_ERR, "TERM environment variable not set.\n");
614 if (strlen(tname) > MAX_NAME_SIZE) {
615 ret_error(TGETENT_ERR,
616 "TERM environment must be <= %d characters.\n",
620 T(("your terminal name is %s", tname));
623 * Allow output redirection. This is what SVr3 does. If stdout is
624 * directed to a file, screen updates go to standard error.
626 if (Filedes == STDOUT_FILENO && !isatty(Filedes))
627 Filedes = STDERR_FILENO;
630 * Check if we have already initialized to use this terminal. If so, we
631 * do not need to re-read the terminfo entry, or obtain TTY settings.
633 * This is an improvement on SVr4 curses. If an application mixes curses
634 * and termcap calls, it may call both initscr and tgetent. This is not
635 * really a good thing to do, but can happen if someone tries using ncurses
636 * with the readline library. The problem we are fixing is that when
637 * tgetent calls setupterm, the resulting Ottyb struct in cur_term is
638 * zeroed. A subsequent call to endwin uses the zeroed terminal settings
639 * rather than the ones saved in initscr. So we check if cur_term appears
640 * to contain terminal settings for the same output file as our current
641 * call - and copy those terminal settings. (SVr4 curses does not do this,
642 * however applications that are working around the problem will still work
643 * properly with this feature).
647 && termp->Filedes == Filedes
648 && termp->_termname != 0
649 && !strcmp(termp->_termname, tname)
650 && _nc_name_match(termp->type.term_names, tname, "|")) {
651 T(("reusing existing terminal information and mode-settings"));
654 #ifdef USE_TERM_DRIVER
655 termp = (TERMINAL *) typeCalloc(TERMINAL_CONTROL_BLOCK, 1);
657 termp = typeCalloc(TERMINAL, 1);
660 ret_error0(TGETENT_ERR,
661 "Not enough memory to create terminal structure.\n");
663 #ifdef USE_TERM_DRIVER
664 TCB = (TERMINAL_CONTROL_BLOCK *) termp;
665 code = _nc_get_driver(TCB, tname, errret);
667 termp->Filedes = Filedes;
668 termp->_termname = strdup(tname);
670 ret_error0(TGETENT_ERR,
671 "Could not find any driver to handle this terminal.\n");
674 #if USE_DATABASE || USE_TERMCAP
675 status = grab_entry(tname, &termp->type);
680 /* try fallback list if entry on disk */
681 if (status != TGETENT_YES) {
682 const TERMTYPE *fallback = _nc_fallback(tname);
685 termp->type = *fallback;
686 status = TGETENT_YES;
690 if (status != TGETENT_YES) {
692 if (status == TGETENT_ERR) {
693 ret_error0(status, "terminals database is inaccessible\n");
694 } else if (status == TGETENT_NO) {
695 ret_error(status, "'%s': unknown terminal type.\n", tname);
699 strncpy(ttytype, termp->type.term_names, NAMESIZE - 1);
700 ttytype[NAMESIZE - 1] = '\0';
703 termp->Filedes = Filedes;
704 termp->_termname = strdup(tname);
708 if (command_character)
709 _nc_tinfo_cmdch(termp, *command_character);
712 * If an application calls setupterm() rather than initscr() or
713 * newterm(), we will not have the def_prog_mode() call in
714 * _nc_setupscreen(). Do it now anyway, so we can initialize the
717 if (isatty(Filedes)) {
725 #ifdef USE_TERM_DRIVER
727 NCURSES_SP_NAME(set_curterm) (sp, termp);
734 * We should always check the screensize, just in case.
736 TINFO_GET_SIZE(sp, termp, ptrLines(sp), ptrCols(sp));
739 *errret = TGETENT_YES;
741 #ifndef USE_TERM_DRIVER
743 ret_error(TGETENT_NO, "'%s': I need something more specific.\n", tname);
746 ret_error(TGETENT_YES, "'%s': I can't handle hardcopy terminals.\n", tname);
752 #ifdef USE_TERM_DRIVER
754 * This entrypoint is called from tgetent() to allow a special case of reusing
755 * the same TERMINAL data (see comment).
758 _nc_setupterm(NCURSES_CONST char *tname,
765 res = TINFO_SETUP_TERM(&termp, tname, Filedes, errret, reuse);
767 NCURSES_SP_NAME(set_curterm) (CURRENT_SCREEN_PRE, termp);
773 * setupterm(termname, Filedes, errret)
775 * Find and read the appropriate object file for the terminal
776 * Make cur_term point to the structure.
779 setupterm(NCURSES_CONST char *tname, int Filedes, int *errret)
781 return _nc_setupterm(tname, Filedes, errret, FALSE);