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 */
50 MODULE_ID("$Id: lib_setup.c,v 1.142 2011/10/22 16:13:06 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 sp->_sig_winch = FALSE;
409 /****************************************************************************
413 ****************************************************************************/
415 #if USE_DATABASE || USE_TERMCAP
417 * Return 1 if entry found, 0 if not found, -1 if database not accessible,
418 * just like tgetent().
421 _nc_setup_tinfo(const char *const tn, TERMTYPE *const tp)
423 char filename[PATH_MAX];
424 int status = _nc_read_entry(tn, filename, tp);
427 * If we have an entry, force all of the cancelled strings to null
428 * pointers so we don't have to test them in the rest of the library.
429 * (The terminfo compiler bypasses this logic, since it must know if
430 * a string is cancelled, for merging entries).
432 if (status == TGETENT_YES) {
434 for_each_boolean(n, tp) {
435 if (!VALID_BOOLEAN(tp->Booleans[n]))
436 tp->Booleans[n] = FALSE;
438 for_each_string(n, tp) {
439 if (tp->Strings[n] == CANCELLED_STRING)
440 tp->Strings[n] = ABSENT_STRING;
448 ** Take the real command character out of the CC environment variable
449 ** and substitute it in for the prototype given in 'command_character'.
452 _nc_tinfo_cmdch(TERMINAL * termp, int proto)
459 * Only use the character if the string is a single character,
460 * since it is fairly common for developers to set the C compiler
461 * name as an environment variable - using the same symbol.
463 if ((tmp = getenv("CC")) != 0 && strlen(tmp) == 1) {
465 for_each_string(i, &(termp->type)) {
466 for (tmp = termp->type.Strings[i]; tmp && *tmp; tmp++) {
467 if (UChar(*tmp) == proto)
475 * Find the locale which is in effect.
477 NCURSES_EXPORT(char *)
483 * This is preferable to using getenv() since it ensures that we are using
484 * the locale which was actually initialized by the application.
486 env = setlocale(LC_CTYPE, 0);
488 if (((env = getenv("LC_ALL")) != 0 && *env != '\0')
489 || ((env = getenv("LC_CTYPE")) != 0 && *env != '\0')
490 || ((env = getenv("LANG")) != 0 && *env != '\0')) {
494 T(("_nc_get_locale %s", _nc_visbuf(env)));
499 * Check if we are running in a UTF-8 locale.
502 _nc_unicode_locale(void)
505 #if HAVE_LANGINFO_CODESET
506 char *env = nl_langinfo(CODESET);
507 result = !strcmp(env, "UTF-8");
508 T(("_nc_unicode_locale(%s) ->%d", env, result));
510 char *env = _nc_get_locale();
512 if (strstr(env, ".UTF-8") != 0) {
514 T(("_nc_unicode_locale(%s) ->%d", env, result));
521 #define CONTROL_N(s) ((s) != 0 && strstr(s, "\016") != 0)
522 #define CONTROL_O(s) ((s) != 0 && strstr(s, "\017") != 0)
525 * Check for known broken cases where a UTF-8 locale breaks the alternate
529 _nc_locale_breaks_acs(TERMINAL * termp)
531 const char *env_name = "NCURSES_NO_UTF8_ACS";
536 if (getenv(env_name) != 0) {
537 result = _nc_getenv_num(env_name);
538 } else if ((value = tigetnum("U8")) >= 0) {
539 result = value; /* use extension feature */
540 } else if ((env = getenv("TERM")) != 0) {
541 if (strstr(env, "linux")) {
542 result = 1; /* always broken */
543 } else if (strstr(env, "screen") != 0
544 && ((env = getenv("TERMCAP")) != 0
545 && strstr(env, "screen") != 0)
546 && strstr(env, "hhII00") != 0) {
547 if (CONTROL_N(enter_alt_charset_mode) ||
548 CONTROL_O(enter_alt_charset_mode) ||
549 CONTROL_N(set_attributes) ||
550 CONTROL_O(set_attributes)) {
559 TINFO_SETUP_TERM(TERMINAL ** tp,
560 NCURSES_CONST char *tname,
565 #ifdef USE_TERM_DRIVER
566 TERMINAL_CONTROL_BLOCK *TCB = 0;
576 #ifdef USE_TERM_DRIVER
577 T((T_CALLED("_nc_setupterm_ex(%p,%s,%d,%p)"),
578 (void *) tp, _nc_visbuf(tname), Filedes, (void *) errret));
581 ret_error0(TGETENT_ERR,
582 "Invalid parameter, internal error.\n");
587 T((T_CALLED("setupterm(%s,%d,%p)"), _nc_visbuf(tname), Filedes, (void *) errret));
591 tname = getenv("TERM");
592 if (tname == 0 || *tname == '\0') {
593 ret_error0(TGETENT_ERR, "TERM environment variable not set.\n");
597 if (strlen(tname) > MAX_NAME_SIZE) {
598 ret_error(TGETENT_ERR,
599 "TERM environment must be <= %d characters.\n",
603 T(("your terminal name is %s", tname));
606 * Allow output redirection. This is what SVr3 does. If stdout is
607 * directed to a file, screen updates go to standard error.
609 if (Filedes == STDOUT_FILENO && !isatty(Filedes))
610 Filedes = STDERR_FILENO;
613 * Check if we have already initialized to use this terminal. If so, we
614 * do not need to re-read the terminfo entry, or obtain TTY settings.
616 * This is an improvement on SVr4 curses. If an application mixes curses
617 * and termcap calls, it may call both initscr and tgetent. This is not
618 * really a good thing to do, but can happen if someone tries using ncurses
619 * with the readline library. The problem we are fixing is that when
620 * tgetent calls setupterm, the resulting Ottyb struct in cur_term is
621 * zeroed. A subsequent call to endwin uses the zeroed terminal settings
622 * rather than the ones saved in initscr. So we check if cur_term appears
623 * to contain terminal settings for the same output file as our current
624 * call - and copy those terminal settings. (SVr4 curses does not do this,
625 * however applications that are working around the problem will still work
626 * properly with this feature).
630 && termp->Filedes == Filedes
631 && termp->_termname != 0
632 && !strcmp(termp->_termname, tname)
633 && _nc_name_match(termp->type.term_names, tname, "|")) {
634 T(("reusing existing terminal information and mode-settings"));
637 #ifdef USE_TERM_DRIVER
638 termp = (TERMINAL *) typeCalloc(TERMINAL_CONTROL_BLOCK, 1);
640 termp = typeCalloc(TERMINAL, 1);
643 ret_error0(TGETENT_ERR,
644 "Not enough memory to create terminal structure.\n");
646 #ifdef USE_TERM_DRIVER
648 TCB = (TERMINAL_CONTROL_BLOCK *) termp;
649 code = _nc_globals.term_driver(TCB, tname, errret);
651 termp->Filedes = (short) Filedes;
652 termp->_termname = strdup(tname);
654 ret_error0(TGETENT_ERR,
655 "Could not find any driver to handle this terminal.\n");
658 #if USE_DATABASE || USE_TERMCAP
659 status = _nc_setup_tinfo(tname, &termp->type);
664 /* try fallback list if entry on disk */
665 if (status != TGETENT_YES) {
666 const TERMTYPE *fallback = _nc_fallback(tname);
669 termp->type = *fallback;
670 status = TGETENT_YES;
674 if (status != TGETENT_YES) {
676 if (status == TGETENT_ERR) {
677 ret_error0(status, "terminals database is inaccessible\n");
678 } else if (status == TGETENT_NO) {
679 ret_error1(status, "unknown terminal type.\n", tname);
683 strncpy(ttytype, termp->type.term_names, (size_t) (NAMESIZE - 1));
684 ttytype[NAMESIZE - 1] = '\0';
687 termp->Filedes = (short) Filedes;
688 termp->_termname = strdup(tname);
692 if (command_character)
693 _nc_tinfo_cmdch(termp, UChar(*command_character));
696 * If an application calls setupterm() rather than initscr() or
697 * newterm(), we will not have the def_prog_mode() call in
698 * _nc_setupscreen(). Do it now anyway, so we can initialize the
701 if (isatty(Filedes)) {
709 #ifdef USE_TERM_DRIVER
711 NCURSES_SP_NAME(set_curterm) (sp, termp);
718 * We should always check the screensize, just in case.
720 TINFO_GET_SIZE(sp, termp, ptrLines(sp), ptrCols(sp));
723 *errret = TGETENT_YES;
725 #ifndef USE_TERM_DRIVER
728 * BSD 4.3's termcap contains mis-typed "gn" for wy99. Do a sanity
729 * check before giving up.
731 if ((VALID_STRING(cursor_address)
732 || (VALID_STRING(cursor_down) && VALID_STRING(cursor_home)))
733 && VALID_STRING(clear_screen)) {
734 ret_error1(TGETENT_YES, "terminal is not really generic.\n", tname);
736 ret_error1(TGETENT_NO, "I need something more specific.\n", tname);
738 } else if (hard_copy) {
739 ret_error1(TGETENT_YES, "I can't handle hardcopy terminals.\n", tname);
747 * In case of handling multiple screens, we need to have a screen before
748 * initialization in setupscreen takes place. This is to extend the substitute
749 * for some of the stuff in _nc_prescreen, especially for slk and ripoff
750 * handling which should be done per screen.
752 NCURSES_EXPORT(SCREEN *)
758 T((T_CALLED("new_prescr()")));
761 sp = _nc_alloc_screen_sp();
763 sp->rsp = sp->rippedoff;
764 sp->_filtered = _nc_prescreen.filter_mode;
765 sp->_use_env = _nc_prescreen.use_env;
766 #if NCURSES_NO_PADDING
767 sp->_no_padding = _nc_prescreen._no_padding;
771 sp->_prescreen = TRUE;
774 sp->_TABSIZE = _nc_prescreen._TABSIZE;
775 sp->_ESCDELAY = _nc_prescreen._ESCDELAY;
783 #ifdef USE_TERM_DRIVER
785 * This entrypoint is called from tgetent() to allow a special case of reusing
786 * the same TERMINAL data (see comment).
789 _nc_setupterm(NCURSES_CONST char *tname,
796 res = TINFO_SETUP_TERM(&termp, tname, Filedes, errret, reuse);
798 NCURSES_SP_NAME(set_curterm) (CURRENT_SCREEN_PRE, termp);
804 * setupterm(termname, Filedes, errret)
806 * Find and read the appropriate object file for the terminal
807 * Make cur_term point to the structure.
810 setupterm(NCURSES_CONST char *tname, int Filedes, int *errret)
812 return _nc_setupterm(tname, Filedes, errret, FALSE);