1 /****************************************************************************
2 * Copyright (c) 1998-2007,2008 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 ****************************************************************************/
36 * Terminal setup routines common to termcap and terminfo:
39 * setupterm(char *, int, int *)
42 #include <curses.priv.h>
43 #include <tic.h> /* for MAX_NAME_SIZE */
44 #include <term_entry.h>
46 #if SVR4_TERMIO && !defined(_POSIX_SOURCE)
54 #include <term.h> /* lines, columns, cur_term */
56 MODULE_ID("$Id: lib_setup.c,v 1.111 2008/08/03 22:42:33 tom Exp $")
58 /****************************************************************************
60 * Terminal size computation
62 ****************************************************************************/
65 # if !defined(sun) || !TERMIOS
67 # include <sys/ioctl.h>
73 /* On SCO, they neglected to define struct winsize in termios.h -- it's only
74 * in termio.h and ptem.h (the former conflicts with other definitions).
76 # include <sys/stream.h>
77 # include <sys/ptem.h>
80 #if HAVE_LANGINFO_CODESET
85 * SCO defines TIOCGSIZE and the corresponding struct. Other systems (SunOS,
86 * Solaris, IRIX) define TIOCGWINSZ and struct winsize.
89 # define IOCTL_WINSIZE TIOCGSIZE
90 # define STRUCT_WINSIZE struct ttysize
91 # define WINSIZE_ROWS(n) (int)n.ts_lines
92 # define WINSIZE_COLS(n) (int)n.ts_cols
95 # define IOCTL_WINSIZE TIOCGWINSZ
96 # define STRUCT_WINSIZE struct winsize
97 # define WINSIZE_ROWS(n) (int)n.ws_row
98 # define WINSIZE_COLS(n) (int)n.ws_col
103 * Reduce explicit use of "cur_term" global variable.
106 #define CUR termp->type.
109 * Wrap global variables in this module.
112 NCURSES_EXPORT(char *)
113 NCURSES_PUBLIC_VAR(ttytype) (void)
115 static char empty[] = "";
116 return cur_term ? cur_term->type.term_names : empty;
118 NCURSES_EXPORT(int *)
124 NCURSES_PUBLIC_VAR(LINES) (void)
126 return *_nc_ptr_Lines();
128 NCURSES_EXPORT(int *)
134 NCURSES_PUBLIC_VAR(COLS) (void)
136 return *_nc_ptr_Cols();
139 NCURSES_PUBLIC_VAR(TABSIZE) (void)
141 return SP ? SP->_TABSIZE : 8;
144 NCURSES_EXPORT_VAR(char) ttytype[NAMESIZE] = "";
145 NCURSES_EXPORT_VAR(int) LINES = 0;
146 NCURSES_EXPORT_VAR(int) COLS = 0;
147 NCURSES_EXPORT_VAR(int) TABSIZE = 0;
150 #if NCURSES_EXT_FUNCS
152 set_tabsize(int value)
157 SP->_TABSIZE = value;
170 * If we have a pending SIGWINCH, set the flag in each screen.
173 _nc_handle_sigwinch(SCREEN *sp)
177 if (_nc_globals.have_sigwinch) {
178 _nc_globals.have_sigwinch = 0;
180 for (each_screen(scan)) {
181 scan->_sig_winch = TRUE;
185 return (sp ? sp->_sig_winch : 0);
193 T((T_CALLED("use_env()")));
194 _nc_prescreen.use_env = f;
199 _nc_get_screensize(SCREEN *sp, int *linep, int *colp)
200 /* Obtain lines/columns values from the environment and/or terminfo entry */
202 TERMINAL *termp = cur_term;
205 /* figure out the size of the screen */
206 T(("screen size: terminfo lines = %d columns = %d", lines, columns));
208 if (!_nc_prescreen.use_env) {
209 *linep = (int) lines;
210 *colp = (int) columns;
211 } else { /* usually want to query LINES and COLUMNS from environment */
216 /* first, look for environment variables */
217 if ((value = _nc_getenv_num("LINES")) > 0) {
220 if ((value = _nc_getenv_num("COLUMNS")) > 0) {
223 T(("screen size: environment LINES = %d COLUMNS = %d", *linep, *colp));
226 if (*linep <= 0 || *colp <= 0) {
228 _scrsize(screendata);
229 *colp = screendata[0];
230 *linep = screendata[1];
231 T(("EMX screen size: environment LINES = %d COLUMNS = %d",
236 /* if that didn't work, maybe we can try asking the OS */
237 if (*linep <= 0 || *colp <= 0) {
238 if (isatty(cur_term->Filedes)) {
243 if (ioctl(cur_term->Filedes, IOCTL_WINSIZE, &size) < 0
250 * Solaris lets users override either dimension with an
251 * environment variable.
254 *linep = (sp != 0 && sp->_filtered) ? 1 : WINSIZE_ROWS(size);
256 *colp = WINSIZE_COLS(size);
261 #endif /* HAVE_SIZECHANGE */
263 /* if we can't get dynamic info about the size, use static */
265 *linep = (int) lines;
268 *colp = (int) columns;
271 /* the ultimate fallback, assume fixed 24x80 size */
280 * Put the derived values back in the screen-size caps, so
281 * tigetnum() and tgetnum() will do the right thing.
283 lines = (short) (*linep);
284 columns = (short) (*colp);
287 T(("screen size is %dx%d", *linep, *colp));
289 if (VALID_NUMERIC(init_tabs))
290 my_tabsize = (int) init_tabs;
296 sp->_TABSIZE = my_tabsize;
298 TABSIZE = my_tabsize;
300 T(("TABSIZE = %d", TABSIZE));
305 _nc_update_screensize(SCREEN *sp)
307 TERMINAL *termp = cur_term;
308 int old_lines = lines;
310 int old_cols = columns;
313 _nc_get_screensize(sp, &new_lines, &new_cols);
316 * See is_term_resized() and resizeterm().
317 * We're doing it this way because those functions belong to the upper
318 * ncurses library, while this resides in the lower terminfo library.
321 && sp->_resize != 0) {
322 if ((new_lines != old_lines) || (new_cols != old_cols))
323 sp->_resize(new_lines, new_cols);
324 sp->_sig_winch = FALSE;
329 /****************************************************************************
333 ****************************************************************************/
335 #define ret_error(code, fmt, arg) if (errret) {\
339 fprintf(stderr, fmt, arg);\
343 #define ret_error0(code, msg) if (errret) {\
347 fprintf(stderr, msg);\
351 #if USE_DATABASE || USE_TERMCAP
353 * Return 1 if entry found, 0 if not found, -1 if database not accessible,
354 * just like tgetent().
357 grab_entry(const char *const tn, TERMTYPE *const tp)
359 char filename[PATH_MAX];
360 int status = _nc_read_entry(tn, filename, tp);
363 * If we have an entry, force all of the cancelled strings to null
364 * pointers so we don't have to test them in the rest of the library.
365 * (The terminfo compiler bypasses this logic, since it must know if
366 * a string is cancelled, for merging entries).
368 if (status == TGETENT_YES) {
370 for_each_boolean(n, tp) {
371 if (!VALID_BOOLEAN(tp->Booleans[n]))
372 tp->Booleans[n] = FALSE;
374 for_each_string(n, tp) {
375 if (tp->Strings[n] == CANCELLED_STRING)
376 tp->Strings[n] = ABSENT_STRING;
386 ** Take the real command character out of the CC environment variable
387 ** and substitute it in for the prototype given in 'command_character'.
390 do_prototype(TERMINAL * termp)
397 if ((tmp = getenv("CC")) != 0) {
398 if ((CC = *tmp) != 0) {
399 proto = *command_character;
401 for_each_string(i, &(termp->type)) {
402 for (tmp = termp->type.Strings[i]; *tmp; tmp++) {
412 * Find the locale which is in effect.
414 NCURSES_EXPORT(char *)
420 * This is preferable to using getenv() since it ensures that we are using
421 * the locale which was actually initialized by the application.
423 env = setlocale(LC_CTYPE, 0);
425 if (((env = getenv("LC_ALL")) != 0 && *env != '\0')
426 || ((env = getenv("LC_CTYPE")) != 0 && *env != '\0')
427 || ((env = getenv("LANG")) != 0 && *env != '\0')) {
431 T(("_nc_get_locale %s", _nc_visbuf(env)));
436 * Check if we are running in a UTF-8 locale.
439 _nc_unicode_locale(void)
442 #if HAVE_LANGINFO_CODESET
443 char *env = nl_langinfo(CODESET);
444 result = !strcmp(env, "UTF-8");
445 T(("_nc_unicode_locale(%s) ->%d", env, result));
447 char *env = _nc_get_locale();
449 if (strstr(env, ".UTF-8") != 0) {
451 T(("_nc_unicode_locale(%s) ->%d", env, result));
458 #define CONTROL_N(s) ((s) != 0 && strstr(s, "\016") != 0)
459 #define CONTROL_O(s) ((s) != 0 && strstr(s, "\017") != 0)
462 * Check for known broken cases where a UTF-8 locale breaks the alternate
466 _nc_locale_breaks_acs(TERMINAL * termp)
470 if ((env = getenv("NCURSES_NO_UTF8_ACS")) != 0) {
472 } else if ((env = getenv("TERM")) != 0) {
473 if (strstr(env, "linux"))
474 return 1; /* always broken */
475 if (strstr(env, "screen") != 0
476 && ((env = getenv("TERMCAP")) != 0
477 && strstr(env, "screen") != 0)
478 && strstr(env, "hhII00") != 0) {
479 if (CONTROL_N(enter_alt_charset_mode) ||
480 CONTROL_O(enter_alt_charset_mode) ||
481 CONTROL_N(set_attributes) ||
482 CONTROL_O(set_attributes))
490 * This entrypoint is called from tgetent() to allow a special case of reusing
491 * the same TERMINAL data (see comment).
494 _nc_setupterm(NCURSES_CONST char *tname, int Filedes, int *errret, bool reuse)
500 T((T_CALLED("setupterm(%s,%d,%p)"), _nc_visbuf(tname), Filedes, errret));
503 tname = getenv("TERM");
504 if (tname == 0 || *tname == '\0') {
505 ret_error0(TGETENT_ERR, "TERM environment variable not set.\n");
509 if (strlen(tname) > MAX_NAME_SIZE) {
510 ret_error(TGETENT_ERR,
511 "TERM environment must be <= %d characters.\n",
515 T(("your terminal name is %s", tname));
518 * Allow output redirection. This is what SVr3 does. If stdout is
519 * directed to a file, screen updates go to standard error.
521 if (Filedes == STDOUT_FILENO && !isatty(Filedes))
522 Filedes = STDERR_FILENO;
525 * Check if we have already initialized to use this terminal. If so, we
526 * do not need to re-read the terminfo entry, or obtain TTY settings.
528 * This is an improvement on SVr4 curses. If an application mixes curses
529 * and termcap calls, it may call both initscr and tgetent. This is not
530 * really a good thing to do, but can happen if someone tries using ncurses
531 * with the readline library. The problem we are fixing is that when
532 * tgetent calls setupterm, the resulting Ottyb struct in cur_term is
533 * zeroed. A subsequent call to endwin uses the zeroed terminal settings
534 * rather than the ones saved in initscr. So we check if cur_term appears
535 * to contain terminal settings for the same output file as our current
536 * call - and copy those terminal settings. (SVr4 curses does not do this,
537 * however applications that are working around the problem will still work
538 * properly with this feature).
541 && (termp = cur_term) != 0
542 && termp->Filedes == Filedes
543 && termp->_termname != 0
544 && !strcmp(termp->_termname, tname)
545 && _nc_name_match(termp->type.term_names, tname, "|")) {
546 T(("reusing existing terminal information and mode-settings"));
549 termp = typeCalloc(TERMINAL, 1);
552 ret_error0(TGETENT_ERR,
553 "Not enough memory to create terminal structure.\n");
555 #if USE_DATABASE || USE_TERMCAP
556 status = grab_entry(tname, &termp->type);
561 /* try fallback list if entry on disk */
562 if (status != TGETENT_YES) {
563 const TERMTYPE *fallback = _nc_fallback(tname);
566 termp->type = *fallback;
567 status = TGETENT_YES;
571 if (status != TGETENT_YES) {
573 if (status == TGETENT_ERR) {
574 ret_error0(status, "terminals database is inaccessible\n");
575 } else if (status == TGETENT_NO) {
576 ret_error(status, "'%s': unknown terminal type.\n", tname);
580 strncpy(ttytype, termp->type.term_names, NAMESIZE - 1);
581 ttytype[NAMESIZE - 1] = '\0';
584 termp->Filedes = Filedes;
585 termp->_termname = strdup(tname);
589 if (command_character && getenv("CC"))
593 * If an application calls setupterm() rather than initscr() or
594 * newterm(), we will not have the def_prog_mode() call in
595 * _nc_setupscreen(). Do it now anyway, so we can initialize the
598 if (isatty(Filedes)) {
605 * We should always check the screensize, just in case.
607 _nc_get_screensize(SP, ptrLines(), ptrCols());
610 *errret = TGETENT_YES;
613 ret_error(TGETENT_NO, "'%s': I need something more specific.\n", tname);
616 ret_error(TGETENT_YES, "'%s': I can't handle hardcopy terminals.\n", tname);
622 * setupterm(termname, Filedes, errret)
624 * Find and read the appropriate object file for the terminal
625 * Make cur_term point to the structure.
628 setupterm(NCURSES_CONST char *tname, int Filedes, int *errret)
630 return _nc_setupterm(tname, Filedes, errret, FALSE);