1 /****************************************************************************
2 * Copyright (c) 1998-2005,2006 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-2003 *
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.95 2006/07/28 22:58:13 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
102 NCURSES_EXPORT_VAR(char) ttytype[NAMESIZE] = "";
103 NCURSES_EXPORT_VAR(int) LINES = 0;
104 NCURSES_EXPORT_VAR(int) COLS = 0;
105 NCURSES_EXPORT_VAR(int) TABSIZE = 0;
107 static int _use_env = TRUE;
111 _nc_handle_sigwinch(int enable)
113 static int have_sigwinch = 0; /* initially no SIGWINCH's */
114 static int can_resizeall = 1; /* initially enabled */
120 /* record a SIGWINCH */
124 /* temporarily disable the next block */
128 /* temporarily enable the next block */
134 * If we have a pending SIGWINCH, set the flag in each screen.
135 * But do this only if the block is enabled.
137 if (can_resizeall-- >= 0) { /* test and disable */
139 scan = _nc_screen_chain;
141 scan->_sig_winch = TRUE;
142 scan = scan->_next_screen;
147 result = can_resizeall + 1; /* reenable (unless disables are nested) */
148 can_resizeall = result;
158 T((T_CALLED("use_env()")));
164 _nc_get_screensize(int *linep, int *colp)
165 /* Obtain lines/columns values from the environment and/or terminfo entry */
167 /* figure out the size of the screen */
168 T(("screen size: terminfo lines = %d columns = %d", lines, columns));
170 _nc_handle_sigwinch(0);
172 *linep = (int) lines;
173 *colp = (int) columns;
174 } else { /* usually want to query LINES and COLUMNS from environment */
179 /* first, look for environment variables */
180 if ((value = _nc_getenv_num("LINES")) > 0) {
183 if ((value = _nc_getenv_num("COLUMNS")) > 0) {
186 T(("screen size: environment LINES = %d COLUMNS = %d", *linep, *colp));
189 if (*linep <= 0 || *colp <= 0) {
191 _scrsize(screendata);
192 *colp = screendata[0];
193 *linep = screendata[1];
194 T(("EMX screen size: environment LINES = %d COLUMNS = %d",
199 /* if that didn't work, maybe we can try asking the OS */
200 if (*linep <= 0 || *colp <= 0) {
201 if (isatty(cur_term->Filedes)) {
206 if (ioctl(cur_term->Filedes, IOCTL_WINSIZE, &size) < 0
213 * Solaris lets users override either dimension with an
214 * environment variable.
217 *linep = (SP != 0 && SP->_filtered) ? 1 : WINSIZE_ROWS(size);
219 *colp = WINSIZE_COLS(size);
224 #endif /* HAVE_SIZECHANGE */
226 /* if we can't get dynamic info about the size, use static */
228 *linep = (int) lines;
231 *colp = (int) columns;
234 /* the ultimate fallback, assume fixed 24x80 size */
243 * Put the derived values back in the screen-size caps, so
244 * tigetnum() and tgetnum() will do the right thing.
246 lines = (short) (*linep);
247 columns = (short) (*colp);
249 _nc_handle_sigwinch(1);
251 T(("screen size is %dx%d", *linep, *colp));
253 if (VALID_NUMERIC(init_tabs))
254 TABSIZE = (int) init_tabs;
257 T(("TABSIZE = %d", TABSIZE));
262 _nc_update_screensize(void)
264 int old_lines = lines;
266 int old_cols = columns;
269 _nc_get_screensize(&new_lines, &new_cols);
272 * See is_term_resized() and resizeterm().
273 * We're doing it this way because those functions belong to the upper
274 * ncurses library, while this resides in the lower terminfo library.
277 && SP->_resize != 0) {
278 if ((new_lines != old_lines) || (new_cols != old_cols))
279 SP->_resize(new_lines, new_cols);
280 SP->_sig_winch = FALSE;
285 /****************************************************************************
289 ****************************************************************************/
291 #define ret_error(code, fmt, arg) if (errret) {\
295 fprintf(stderr, fmt, arg);\
299 #define ret_error0(code, msg) if (errret) {\
303 fprintf(stderr, msg);\
307 #if USE_DATABASE || USE_TERMCAP
309 * Return 1 if entry found, 0 if not found, -1 if database not accessible,
310 * just like tgetent().
313 grab_entry(const char *const tn, TERMTYPE *const tp)
315 char filename[PATH_MAX];
316 int status = _nc_read_entry(tn, filename, tp);
319 * If we have an entry, force all of the cancelled strings to null
320 * pointers so we don't have to test them in the rest of the library.
321 * (The terminfo compiler bypasses this logic, since it must know if
322 * a string is cancelled, for merging entries).
324 if (status == TGETENT_YES) {
326 for_each_boolean(n, tp) {
327 if (!VALID_BOOLEAN(tp->Booleans[n]))
328 tp->Booleans[n] = FALSE;
330 for_each_string(n, tp) {
331 if (tp->Strings[n] == CANCELLED_STRING)
332 tp->Strings[n] = ABSENT_STRING;
342 ** Take the real command character out of the CC environment variable
343 ** and substitute it in for the prototype given in 'command_character'.
355 proto = *command_character;
357 for_each_string(i, &(cur_term->type)) {
358 for (tmp = cur_term->type.Strings[i]; *tmp; tmp++) {
366 * Find the locale which is in effect.
368 NCURSES_EXPORT(char *)
374 * This is preferable to using getenv() since it ensures that we are using
375 * the locale which was actually initialized by the application.
377 env = setlocale(LC_CTYPE, 0);
379 if (((env = getenv("LC_ALL")) != 0 && *env != '\0')
380 || ((env = getenv("LC_CTYPE")) != 0 && *env != '\0')
381 || ((env = getenv("LANG")) != 0 && *env != '\0')) {
385 T(("_nc_get_locale %s", _nc_visbuf(env)));
390 * Check if we are running in a UTF-8 locale.
393 _nc_unicode_locale(void)
396 #if HAVE_LANGINFO_CODESET
397 char *env = nl_langinfo(CODESET);
398 result = !strcmp(env, "UTF-8");
399 T(("_nc_unicode_locale(%s) ->%d", env, result));
401 char *env = _nc_get_locale();
403 if (strstr(env, ".UTF-8") != 0) {
405 T(("_nc_unicode_locale(%s) ->%d", env, result));
412 #define CONTROL_N(s) ((s) != 0 && strstr(s, "\016") != 0)
413 #define CONTROL_O(s) ((s) != 0 && strstr(s, "\017") != 0)
416 * Check for known broken cases where a UTF-8 locale breaks the alternate
420 _nc_locale_breaks_acs(void)
424 if ((env = getenv("NCURSES_NO_UTF8_ACS")) != 0) {
426 } else if ((env = getenv("TERM")) != 0) {
427 if (strstr(env, "linux"))
428 return 1; /* always broken */
429 if (strstr(env, "screen") != 0
430 && ((env = getenv("TERMCAP")) != 0
431 && strstr(env, "screen") != 0)
432 && strstr(env, "hhII00") != 0) {
433 if (CONTROL_N(enter_alt_charset_mode) ||
434 CONTROL_O(enter_alt_charset_mode) ||
435 CONTROL_N(set_attributes) ||
436 CONTROL_O(set_attributes))
444 * This entrypoint is called from tgetent() to allow a special case of reusing
445 * the same TERMINAL data (see comment).
448 _nc_setupterm(NCURSES_CONST char *tname, int Filedes, int *errret, bool reuse)
453 T((T_CALLED("setupterm(%s,%d,%p)"), _nc_visbuf(tname), Filedes, errret));
456 tname = getenv("TERM");
457 if (tname == 0 || *tname == '\0') {
458 ret_error0(TGETENT_ERR, "TERM environment variable not set.\n");
462 if (strlen(tname) > MAX_NAME_SIZE) {
463 ret_error(TGETENT_ERR,
464 "TERM environment must be <= %d characters.\n",
468 T(("your terminal name is %s", tname));
471 * Allow output redirection. This is what SVr3 does. If stdout is
472 * directed to a file, screen updates go to standard error.
474 if (Filedes == STDOUT_FILENO && !isatty(Filedes))
475 Filedes = STDERR_FILENO;
478 * Check if we have already initialized to use this terminal. If so, we
479 * do not need to re-read the terminfo entry, or obtain TTY settings.
481 * This is an improvement on SVr4 curses. If an application mixes curses
482 * and termcap calls, it may call both initscr and tgetent. This is not
483 * really a good thing to do, but can happen if someone tries using ncurses
484 * with the readline library. The problem we are fixing is that when
485 * tgetent calls setupterm, the resulting Ottyb struct in cur_term is
486 * zeroed. A subsequent call to endwin uses the zeroed terminal settings
487 * rather than the ones saved in initscr. So we check if cur_term appears
488 * to contain terminal settings for the same output file as our current
489 * call - and copy those terminal settings. (SVr4 curses does not do this,
490 * however applications that are working around the problem will still work
491 * properly with this feature).
495 && cur_term->Filedes == Filedes
496 && cur_term->_termname != 0
497 && !strcmp(cur_term->_termname, tname)
498 && _nc_name_match(cur_term->type.term_names, tname, "|")) {
499 T(("reusing existing terminal information and mode-settings"));
503 term_ptr = typeCalloc(TERMINAL, 1);
506 ret_error0(TGETENT_ERR,
507 "Not enough memory to create terminal structure.\n");
509 #if USE_DATABASE || USE_TERMCAP
510 status = grab_entry(tname, &term_ptr->type);
515 /* try fallback list if entry on disk */
516 if (status != TGETENT_YES) {
517 const TERMTYPE *fallback = _nc_fallback(tname);
520 term_ptr->type = *fallback;
521 status = TGETENT_YES;
525 if (status != TGETENT_YES) {
526 del_curterm(term_ptr);
527 if (status == TGETENT_ERR) {
528 ret_error0(status, "terminals database is inaccessible\n");
529 } else if (status == TGETENT_NO) {
530 ret_error(status, "'%s': unknown terminal type.\n", tname);
534 set_curterm(term_ptr);
536 if (command_character && getenv("CC"))
539 strncpy(ttytype, cur_term->type.term_names, NAMESIZE - 1);
540 ttytype[NAMESIZE - 1] = '\0';
542 cur_term->Filedes = Filedes;
543 cur_term->_termname = strdup(tname);
546 * If an application calls setupterm() rather than initscr() or
547 * newterm(), we will not have the def_prog_mode() call in
548 * _nc_setupscreen(). Do it now anyway, so we can initialize the
551 if (isatty(Filedes)) {
558 * We should always check the screensize, just in case.
560 _nc_get_screensize(&LINES, &COLS);
563 *errret = TGETENT_YES;
565 T((T_CREATE("screen %s %dx%d"), tname, LINES, COLS));
568 ret_error(TGETENT_NO, "'%s': I need something more specific.\n", tname);
571 ret_error(TGETENT_YES, "'%s': I can't handle hardcopy terminals.\n", tname);
577 * setupterm(termname, Filedes, errret)
579 * Find and read the appropriate object file for the terminal
580 * Make cur_term point to the structure.
583 setupterm(NCURSES_CONST char *tname, int Filedes, int *errret)
585 return _nc_setupterm(tname, Filedes, errret, FALSE);