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 #include <term.h> /* lines, columns, cur_term */
57 MODULE_ID("$Id: lib_setup.c,v 1.113 2009/02/14 22:21:40 tom Exp $")
59 /****************************************************************************
61 * Terminal size computation
63 ****************************************************************************/
66 # if !defined(sun) || !TERMIOS
68 # include <sys/ioctl.h>
74 /* On SCO, they neglected to define struct winsize in termios.h -- it's only
75 * in termio.h and ptem.h (the former conflicts with other definitions).
77 # include <sys/stream.h>
78 # include <sys/ptem.h>
81 #if HAVE_LANGINFO_CODESET
86 * SCO defines TIOCGSIZE and the corresponding struct. Other systems (SunOS,
87 * Solaris, IRIX) define TIOCGWINSZ and struct winsize.
90 # define IOCTL_WINSIZE TIOCGSIZE
91 # define STRUCT_WINSIZE struct ttysize
92 # define WINSIZE_ROWS(n) (int)n.ts_lines
93 # define WINSIZE_COLS(n) (int)n.ts_cols
96 # define IOCTL_WINSIZE TIOCGWINSZ
97 # define STRUCT_WINSIZE struct winsize
98 # define WINSIZE_ROWS(n) (int)n.ws_row
99 # define WINSIZE_COLS(n) (int)n.ws_col
104 * Reduce explicit use of "cur_term" global variable.
107 #define CUR termp->type.
110 * Wrap global variables in this module.
113 NCURSES_EXPORT(char *)
114 NCURSES_PUBLIC_VAR(ttytype) (void)
116 static char empty[] = "";
117 return cur_term ? cur_term->type.term_names : empty;
119 NCURSES_EXPORT(int *)
125 NCURSES_PUBLIC_VAR(LINES) (void)
127 return *_nc_ptr_Lines();
129 NCURSES_EXPORT(int *)
135 NCURSES_PUBLIC_VAR(COLS) (void)
137 return *_nc_ptr_Cols();
140 NCURSES_PUBLIC_VAR(TABSIZE) (void)
142 return SP ? SP->_TABSIZE : 8;
145 NCURSES_EXPORT_VAR(char) ttytype[NAMESIZE] = "";
146 NCURSES_EXPORT_VAR(int) LINES = 0;
147 NCURSES_EXPORT_VAR(int) COLS = 0;
148 NCURSES_EXPORT_VAR(int) TABSIZE = 0;
151 #if NCURSES_EXT_FUNCS
153 NCURSES_SP_NAME(set_tabsize) (NCURSES_SP_DCLx int value)
158 SP_PARM->_TABSIZE = value;
171 set_tabsize(int value)
173 return NCURSES_SP_NAME(set_tabsize) (CURRENT_SCREEN, value);
176 #endif /* NCURSES_EXT_FUNCS */
180 * If we have a pending SIGWINCH, set the flag in each screen.
183 _nc_handle_sigwinch(SCREEN *sp)
187 if (_nc_globals.have_sigwinch) {
188 _nc_globals.have_sigwinch = 0;
190 for (each_screen(scan)) {
191 scan->_sig_winch = TRUE;
195 return (sp ? sp->_sig_winch : 0);
201 NCURSES_SP_NAME(use_env) (NCURSES_SP_DCLx bool f)
203 T((T_CALLED("use_env()")));
204 if (IsPreScreen(SP_PARM)) {
205 SP_PARM->_use_env = f;
207 _nc_prescreen.use_env = f;
216 NCURSES_SP_NAME(use_env) (CURRENT_SCREEN, f);
221 _nc_get_screensize(SCREEN *sp, int *linep, int *colp)
222 /* Obtain lines/columns values from the environment and/or terminfo entry */
224 TERMINAL *termp = cur_term;
227 /* figure out the size of the screen */
228 T(("screen size: terminfo lines = %d columns = %d", lines, columns));
230 if (!_nc_prescreen.use_env) {
231 *linep = (int) lines;
232 *colp = (int) columns;
233 } else { /* usually want to query LINES and COLUMNS from environment */
238 /* first, look for environment variables */
239 if ((value = _nc_getenv_num("LINES")) > 0) {
242 if ((value = _nc_getenv_num("COLUMNS")) > 0) {
245 T(("screen size: environment LINES = %d COLUMNS = %d", *linep, *colp));
248 if (*linep <= 0 || *colp <= 0) {
250 _scrsize(screendata);
251 *colp = screendata[0];
252 *linep = screendata[1];
253 T(("EMX screen size: environment LINES = %d COLUMNS = %d",
258 /* if that didn't work, maybe we can try asking the OS */
259 if (*linep <= 0 || *colp <= 0) {
260 if (isatty(cur_term->Filedes)) {
265 if (ioctl(cur_term->Filedes, IOCTL_WINSIZE, &size) < 0
272 * Solaris lets users override either dimension with an
273 * environment variable.
276 *linep = (sp != 0 && sp->_filtered) ? 1 : WINSIZE_ROWS(size);
278 *colp = WINSIZE_COLS(size);
283 #endif /* HAVE_SIZECHANGE */
285 /* if we can't get dynamic info about the size, use static */
287 *linep = (int) lines;
290 *colp = (int) columns;
293 /* the ultimate fallback, assume fixed 24x80 size */
302 * Put the derived values back in the screen-size caps, so
303 * tigetnum() and tgetnum() will do the right thing.
305 lines = (short) (*linep);
306 columns = (short) (*colp);
309 T(("screen size is %dx%d", *linep, *colp));
311 if (VALID_NUMERIC(init_tabs))
312 my_tabsize = (int) init_tabs;
318 sp->_TABSIZE = my_tabsize;
320 TABSIZE = my_tabsize;
322 T(("TABSIZE = %d", TABSIZE));
327 _nc_update_screensize(SCREEN *sp)
329 TERMINAL *termp = cur_term;
330 int old_lines = lines;
332 int old_cols = columns;
335 _nc_get_screensize(sp, &new_lines, &new_cols);
338 * See is_term_resized() and resizeterm().
339 * We're doing it this way because those functions belong to the upper
340 * ncurses library, while this resides in the lower terminfo library.
343 && sp->_resize != 0) {
344 if ((new_lines != old_lines) || (new_cols != old_cols))
345 sp->_resize(new_lines, new_cols);
346 sp->_sig_winch = FALSE;
351 /****************************************************************************
355 ****************************************************************************/
357 #define ret_error(code, fmt, arg) if (errret) {\
361 fprintf(stderr, fmt, arg);\
365 #define ret_error0(code, msg) if (errret) {\
369 fprintf(stderr, msg);\
373 #if USE_DATABASE || USE_TERMCAP
375 * Return 1 if entry found, 0 if not found, -1 if database not accessible,
376 * just like tgetent().
379 grab_entry(const char *const tn, TERMTYPE *const tp)
381 char filename[PATH_MAX];
382 int status = _nc_read_entry(tn, filename, tp);
385 * If we have an entry, force all of the cancelled strings to null
386 * pointers so we don't have to test them in the rest of the library.
387 * (The terminfo compiler bypasses this logic, since it must know if
388 * a string is cancelled, for merging entries).
390 if (status == TGETENT_YES) {
392 for_each_boolean(n, tp) {
393 if (!VALID_BOOLEAN(tp->Booleans[n]))
394 tp->Booleans[n] = FALSE;
396 for_each_string(n, tp) {
397 if (tp->Strings[n] == CANCELLED_STRING)
398 tp->Strings[n] = ABSENT_STRING;
408 ** Take the real command character out of the CC environment variable
409 ** and substitute it in for the prototype given in 'command_character'.
412 do_prototype(TERMINAL * termp)
419 if ((tmp = getenv("CC")) != 0) {
420 if ((CC = *tmp) != 0) {
421 proto = *command_character;
423 for_each_string(i, &(termp->type)) {
424 for (tmp = termp->type.Strings[i]; *tmp; tmp++) {
434 * Find the locale which is in effect.
436 NCURSES_EXPORT(char *)
442 * This is preferable to using getenv() since it ensures that we are using
443 * the locale which was actually initialized by the application.
445 env = setlocale(LC_CTYPE, 0);
447 if (((env = getenv("LC_ALL")) != 0 && *env != '\0')
448 || ((env = getenv("LC_CTYPE")) != 0 && *env != '\0')
449 || ((env = getenv("LANG")) != 0 && *env != '\0')) {
453 T(("_nc_get_locale %s", _nc_visbuf(env)));
458 * Check if we are running in a UTF-8 locale.
461 _nc_unicode_locale(void)
464 #if HAVE_LANGINFO_CODESET
465 char *env = nl_langinfo(CODESET);
466 result = !strcmp(env, "UTF-8");
467 T(("_nc_unicode_locale(%s) ->%d", env, result));
469 char *env = _nc_get_locale();
471 if (strstr(env, ".UTF-8") != 0) {
473 T(("_nc_unicode_locale(%s) ->%d", env, result));
480 #define CONTROL_N(s) ((s) != 0 && strstr(s, "\016") != 0)
481 #define CONTROL_O(s) ((s) != 0 && strstr(s, "\017") != 0)
484 * Check for known broken cases where a UTF-8 locale breaks the alternate
488 _nc_locale_breaks_acs(TERMINAL * termp)
492 if ((env = getenv("NCURSES_NO_UTF8_ACS")) != 0) {
494 } else if ((env = getenv("TERM")) != 0) {
495 if (strstr(env, "linux"))
496 return 1; /* always broken */
497 if (strstr(env, "screen") != 0
498 && ((env = getenv("TERMCAP")) != 0
499 && strstr(env, "screen") != 0)
500 && strstr(env, "hhII00") != 0) {
501 if (CONTROL_N(enter_alt_charset_mode) ||
502 CONTROL_O(enter_alt_charset_mode) ||
503 CONTROL_N(set_attributes) ||
504 CONTROL_O(set_attributes))
512 * This entrypoint is called from tgetent() to allow a special case of reusing
513 * the same TERMINAL data (see comment).
516 _nc_setupterm(NCURSES_CONST char *tname, int Filedes, int *errret, bool reuse)
522 T((T_CALLED("setupterm(%s,%d,%p)"), _nc_visbuf(tname), Filedes, errret));
525 tname = getenv("TERM");
526 if (tname == 0 || *tname == '\0') {
527 ret_error0(TGETENT_ERR, "TERM environment variable not set.\n");
531 if (strlen(tname) > MAX_NAME_SIZE) {
532 ret_error(TGETENT_ERR,
533 "TERM environment must be <= %d characters.\n",
537 T(("your terminal name is %s", tname));
540 * Allow output redirection. This is what SVr3 does. If stdout is
541 * directed to a file, screen updates go to standard error.
543 if (Filedes == STDOUT_FILENO && !isatty(Filedes))
544 Filedes = STDERR_FILENO;
547 * Check if we have already initialized to use this terminal. If so, we
548 * do not need to re-read the terminfo entry, or obtain TTY settings.
550 * This is an improvement on SVr4 curses. If an application mixes curses
551 * and termcap calls, it may call both initscr and tgetent. This is not
552 * really a good thing to do, but can happen if someone tries using ncurses
553 * with the readline library. The problem we are fixing is that when
554 * tgetent calls setupterm, the resulting Ottyb struct in cur_term is
555 * zeroed. A subsequent call to endwin uses the zeroed terminal settings
556 * rather than the ones saved in initscr. So we check if cur_term appears
557 * to contain terminal settings for the same output file as our current
558 * call - and copy those terminal settings. (SVr4 curses does not do this,
559 * however applications that are working around the problem will still work
560 * properly with this feature).
563 && (termp = cur_term) != 0
564 && termp->Filedes == Filedes
565 && termp->_termname != 0
566 && !strcmp(termp->_termname, tname)
567 && _nc_name_match(termp->type.term_names, tname, "|")) {
568 T(("reusing existing terminal information and mode-settings"));
571 termp = typeCalloc(TERMINAL, 1);
574 ret_error0(TGETENT_ERR,
575 "Not enough memory to create terminal structure.\n");
577 #if USE_DATABASE || USE_TERMCAP
578 status = grab_entry(tname, &termp->type);
583 /* try fallback list if entry on disk */
584 if (status != TGETENT_YES) {
585 const TERMTYPE *fallback = _nc_fallback(tname);
588 termp->type = *fallback;
589 status = TGETENT_YES;
593 if (status != TGETENT_YES) {
595 if (status == TGETENT_ERR) {
596 ret_error0(status, "terminals database is inaccessible\n");
597 } else if (status == TGETENT_NO) {
598 ret_error(status, "'%s': unknown terminal type.\n", tname);
602 strncpy(ttytype, termp->type.term_names, NAMESIZE - 1);
603 ttytype[NAMESIZE - 1] = '\0';
606 termp->Filedes = Filedes;
607 termp->_termname = strdup(tname);
611 if (command_character && getenv("CC"))
615 * If an application calls setupterm() rather than initscr() or
616 * newterm(), we will not have the def_prog_mode() call in
617 * _nc_setupscreen(). Do it now anyway, so we can initialize the
620 if (isatty(Filedes)) {
627 * We should always check the screensize, just in case.
629 _nc_get_screensize(SP, ptrLines(), ptrCols());
632 *errret = TGETENT_YES;
635 ret_error(TGETENT_NO, "'%s': I need something more specific.\n", tname);
638 ret_error(TGETENT_YES, "'%s': I can't handle hardcopy terminals.\n", tname);
644 * setupterm(termname, Filedes, errret)
646 * Find and read the appropriate object file for the terminal
647 * Make cur_term point to the structure.
650 setupterm(NCURSES_CONST char *tname, int Filedes, int *errret)
652 return _nc_setupterm(tname, Filedes, errret, FALSE);