1 /****************************************************************************
2 * Copyright (c) 1998-2006,2007 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.99 2007/07/16 20:32:27 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 NCURSES_EXPORT(char *)
104 NCURSES_PUBLIC_VAR(ttytype) (void)
106 return cur_term ? cur_term->type.term_names : "";
109 NCURSES_PUBLIC_VAR(LINES) (void)
111 return (SP ? SP->_LINES : _nc_prescreen._LINES);
114 NCURSES_PUBLIC_VAR(COLS) (void)
116 return SP ? SP->_COLS : _nc_prescreen._COLS;
119 NCURSES_PUBLIC_VAR(TABSIZE) (void)
121 return SP ? SP->_TABSIZE : 8;
124 NCURSES_EXPORT_VAR(char) ttytype[NAMESIZE] = "";
125 NCURSES_EXPORT_VAR(int) LINES = 0;
126 NCURSES_EXPORT_VAR(int) COLS = 0;
127 NCURSES_EXPORT_VAR(int) TABSIZE = 0;
132 * If we have a pending SIGWINCH, set the flag in each screen.
135 _nc_handle_sigwinch(int update)
139 (void) update; /* no longer used */
141 if (_nc_globals.have_sigwinch) {
142 _nc_globals.have_sigwinch = 0;
144 scan = _nc_screen_chain;
146 scan->_sig_winch = TRUE;
147 scan = scan->_next_screen;
151 return (SP ? SP->_sig_winch : 0);
159 T((T_CALLED("use_env()")));
160 _nc_prescreen.use_env = f;
165 _nc_get_screensize(int *linep, int *colp)
166 /* Obtain lines/columns values from the environment and/or terminfo entry */
170 /* figure out the size of the screen */
171 T(("screen size: terminfo lines = %d columns = %d", lines, columns));
173 if (!_nc_prescreen.use_env) {
174 *linep = (int) lines;
175 *colp = (int) columns;
176 } else { /* usually want to query LINES and COLUMNS from environment */
181 /* first, look for environment variables */
182 if ((value = _nc_getenv_num("LINES")) > 0) {
185 if ((value = _nc_getenv_num("COLUMNS")) > 0) {
188 T(("screen size: environment LINES = %d COLUMNS = %d", *linep, *colp));
191 if (*linep <= 0 || *colp <= 0) {
193 _scrsize(screendata);
194 *colp = screendata[0];
195 *linep = screendata[1];
196 T(("EMX screen size: environment LINES = %d COLUMNS = %d",
201 /* if that didn't work, maybe we can try asking the OS */
202 if (*linep <= 0 || *colp <= 0) {
203 if (isatty(cur_term->Filedes)) {
208 if (ioctl(cur_term->Filedes, IOCTL_WINSIZE, &size) < 0
215 * Solaris lets users override either dimension with an
216 * environment variable.
219 *linep = (SP != 0 && SP->_filtered) ? 1 : WINSIZE_ROWS(size);
221 *colp = WINSIZE_COLS(size);
226 #endif /* HAVE_SIZECHANGE */
228 /* if we can't get dynamic info about the size, use static */
230 *linep = (int) lines;
233 *colp = (int) columns;
236 /* the ultimate fallback, assume fixed 24x80 size */
245 * Put the derived values back in the screen-size caps, so
246 * tigetnum() and tgetnum() will do the right thing.
248 lines = (short) (*linep);
249 columns = (short) (*colp);
252 T(("screen size is %dx%d", *linep, *colp));
254 if (VALID_NUMERIC(init_tabs))
255 my_tabsize = (int) init_tabs;
261 SP->_TABSIZE = my_tabsize;
263 TABSIZE = my_tabsize;
265 T(("TABSIZE = %d", TABSIZE));
270 _nc_update_screensize(void)
272 int old_lines = lines;
274 int old_cols = columns;
277 _nc_get_screensize(&new_lines, &new_cols);
280 * See is_term_resized() and resizeterm().
281 * We're doing it this way because those functions belong to the upper
282 * ncurses library, while this resides in the lower terminfo library.
285 && SP->_resize != 0) {
286 if ((new_lines != old_lines) || (new_cols != old_cols))
287 SP->_resize(new_lines, new_cols);
288 SP->_sig_winch = FALSE;
293 /****************************************************************************
297 ****************************************************************************/
299 #define ret_error(code, fmt, arg) if (errret) {\
303 fprintf(stderr, fmt, arg);\
307 #define ret_error0(code, msg) if (errret) {\
311 fprintf(stderr, msg);\
315 #if USE_DATABASE || USE_TERMCAP
317 * Return 1 if entry found, 0 if not found, -1 if database not accessible,
318 * just like tgetent().
321 grab_entry(const char *const tn, TERMTYPE *const tp)
323 char filename[PATH_MAX];
324 int status = _nc_read_entry(tn, filename, tp);
327 * If we have an entry, force all of the cancelled strings to null
328 * pointers so we don't have to test them in the rest of the library.
329 * (The terminfo compiler bypasses this logic, since it must know if
330 * a string is cancelled, for merging entries).
332 if (status == TGETENT_YES) {
334 for_each_boolean(n, tp) {
335 if (!VALID_BOOLEAN(tp->Booleans[n]))
336 tp->Booleans[n] = FALSE;
338 for_each_string(n, tp) {
339 if (tp->Strings[n] == CANCELLED_STRING)
340 tp->Strings[n] = ABSENT_STRING;
350 ** Take the real command character out of the CC environment variable
351 ** and substitute it in for the prototype given in 'command_character'.
363 proto = *command_character;
365 for_each_string(i, &(cur_term->type)) {
366 for (tmp = cur_term->type.Strings[i]; *tmp; tmp++) {
374 * Find the locale which is in effect.
376 NCURSES_EXPORT(char *)
382 * This is preferable to using getenv() since it ensures that we are using
383 * the locale which was actually initialized by the application.
385 env = setlocale(LC_CTYPE, 0);
387 if (((env = getenv("LC_ALL")) != 0 && *env != '\0')
388 || ((env = getenv("LC_CTYPE")) != 0 && *env != '\0')
389 || ((env = getenv("LANG")) != 0 && *env != '\0')) {
393 T(("_nc_get_locale %s", _nc_visbuf(env)));
398 * Check if we are running in a UTF-8 locale.
401 _nc_unicode_locale(void)
404 #if HAVE_LANGINFO_CODESET
405 char *env = nl_langinfo(CODESET);
406 result = !strcmp(env, "UTF-8");
407 T(("_nc_unicode_locale(%s) ->%d", env, result));
409 char *env = _nc_get_locale();
411 if (strstr(env, ".UTF-8") != 0) {
413 T(("_nc_unicode_locale(%s) ->%d", env, result));
420 #define CONTROL_N(s) ((s) != 0 && strstr(s, "\016") != 0)
421 #define CONTROL_O(s) ((s) != 0 && strstr(s, "\017") != 0)
424 * Check for known broken cases where a UTF-8 locale breaks the alternate
428 _nc_locale_breaks_acs(void)
432 if ((env = getenv("NCURSES_NO_UTF8_ACS")) != 0) {
434 } else if ((env = getenv("TERM")) != 0) {
435 if (strstr(env, "linux"))
436 return 1; /* always broken */
437 if (strstr(env, "screen") != 0
438 && ((env = getenv("TERMCAP")) != 0
439 && strstr(env, "screen") != 0)
440 && strstr(env, "hhII00") != 0) {
441 if (CONTROL_N(enter_alt_charset_mode) ||
442 CONTROL_O(enter_alt_charset_mode) ||
443 CONTROL_N(set_attributes) ||
444 CONTROL_O(set_attributes))
452 * This entrypoint is called from tgetent() to allow a special case of reusing
453 * the same TERMINAL data (see comment).
456 _nc_setupterm(NCURSES_CONST char *tname, int Filedes, int *errret, bool reuse)
461 T((T_CALLED("setupterm(%s,%d,%p)"), _nc_visbuf(tname), Filedes, errret));
464 tname = getenv("TERM");
465 if (tname == 0 || *tname == '\0') {
466 ret_error0(TGETENT_ERR, "TERM environment variable not set.\n");
470 if (strlen(tname) > MAX_NAME_SIZE) {
471 ret_error(TGETENT_ERR,
472 "TERM environment must be <= %d characters.\n",
476 T(("your terminal name is %s", tname));
479 * Allow output redirection. This is what SVr3 does. If stdout is
480 * directed to a file, screen updates go to standard error.
482 if (Filedes == STDOUT_FILENO && !isatty(Filedes))
483 Filedes = STDERR_FILENO;
486 * Check if we have already initialized to use this terminal. If so, we
487 * do not need to re-read the terminfo entry, or obtain TTY settings.
489 * This is an improvement on SVr4 curses. If an application mixes curses
490 * and termcap calls, it may call both initscr and tgetent. This is not
491 * really a good thing to do, but can happen if someone tries using ncurses
492 * with the readline library. The problem we are fixing is that when
493 * tgetent calls setupterm, the resulting Ottyb struct in cur_term is
494 * zeroed. A subsequent call to endwin uses the zeroed terminal settings
495 * rather than the ones saved in initscr. So we check if cur_term appears
496 * to contain terminal settings for the same output file as our current
497 * call - and copy those terminal settings. (SVr4 curses does not do this,
498 * however applications that are working around the problem will still work
499 * properly with this feature).
503 && cur_term->Filedes == Filedes
504 && cur_term->_termname != 0
505 && !strcmp(cur_term->_termname, tname)
506 && _nc_name_match(cur_term->type.term_names, tname, "|")) {
507 T(("reusing existing terminal information and mode-settings"));
511 term_ptr = typeCalloc(TERMINAL, 1);
514 ret_error0(TGETENT_ERR,
515 "Not enough memory to create terminal structure.\n");
517 #if USE_DATABASE || USE_TERMCAP
518 status = grab_entry(tname, &term_ptr->type);
523 /* try fallback list if entry on disk */
524 if (status != TGETENT_YES) {
525 const TERMTYPE *fallback = _nc_fallback(tname);
528 term_ptr->type = *fallback;
529 status = TGETENT_YES;
533 if (status != TGETENT_YES) {
534 del_curterm(term_ptr);
535 if (status == TGETENT_ERR) {
536 ret_error0(status, "terminals database is inaccessible\n");
537 } else if (status == TGETENT_NO) {
538 ret_error(status, "'%s': unknown terminal type.\n", tname);
542 set_curterm(term_ptr);
544 if (command_character && getenv("CC"))
548 strncpy(ttytype, cur_term->type.term_names, NAMESIZE - 1);
549 ttytype[NAMESIZE - 1] = '\0';
552 cur_term->Filedes = Filedes;
553 cur_term->_termname = strdup(tname);
556 * If an application calls setupterm() rather than initscr() or
557 * newterm(), we will not have the def_prog_mode() call in
558 * _nc_setupscreen(). Do it now anyway, so we can initialize the
561 if (isatty(Filedes)) {
568 * We should always check the screensize, just in case.
571 _nc_get_screensize(SP ? &(SP->_LINES) : &(_nc_prescreen._LINES),
572 SP ? &(SP->_COLS) : &(_nc_prescreen._COLS));
574 _nc_get_screensize(&LINES, &COLS);
578 *errret = TGETENT_YES;
581 ret_error(TGETENT_NO, "'%s': I need something more specific.\n", tname);
584 ret_error(TGETENT_YES, "'%s': I can't handle hardcopy terminals.\n", tname);
590 * setupterm(termname, Filedes, errret)
592 * Find and read the appropriate object file for the terminal
593 * Make cur_term point to the structure.
596 setupterm(NCURSES_CONST char *tname, int Filedes, int *errret)
598 return _nc_setupterm(tname, Filedes, errret, FALSE);