1 /****************************************************************************
2 * Copyright (c) 1998-2002,2003 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)
50 #include <term.h> /* lines, columns, cur_term */
52 MODULE_ID("$Id: lib_setup.c,v 1.79 2003/12/27 18:24:26 tom Exp $")
54 /****************************************************************************
56 * Terminal size computation
58 ****************************************************************************/
61 # if !defined(sun) || !TERMIOS
63 # include <sys/ioctl.h>
69 /* On SCO, they neglected to define struct winsize in termios.h -- it's only
70 * in termio.h and ptem.h (the former conflicts with other definitions).
72 # include <sys/stream.h>
73 # include <sys/ptem.h>
77 * SCO defines TIOCGSIZE and the corresponding struct. Other systems (SunOS,
78 * Solaris, IRIX) define TIOCGWINSZ and struct winsize.
81 # define IOCTL_WINSIZE TIOCGSIZE
82 # define STRUCT_WINSIZE struct ttysize
83 # define WINSIZE_ROWS(n) (int)n.ts_lines
84 # define WINSIZE_COLS(n) (int)n.ts_cols
87 # define IOCTL_WINSIZE TIOCGWINSZ
88 # define STRUCT_WINSIZE struct winsize
89 # define WINSIZE_ROWS(n) (int)n.ws_row
90 # define WINSIZE_COLS(n) (int)n.ws_col
94 NCURSES_EXPORT_VAR(char) ttytype[NAMESIZE] = "";
95 NCURSES_EXPORT_VAR(int) LINES = 0;
96 NCURSES_EXPORT_VAR(int) COLS = 0;
97 NCURSES_EXPORT_VAR(int) TABSIZE = 0;
99 static int _use_env = TRUE;
104 T((T_CALLED("use_env()")));
110 _nc_get_screensize(int *linep, int *colp)
111 /* Obtain lines/columns values from the environment and/or terminfo entry */
113 /* figure out the size of the screen */
114 T(("screen size: terminfo lines = %d columns = %d", lines, columns));
117 *linep = (int) lines;
118 *colp = (int) columns;
119 } else { /* usually want to query LINES and COLUMNS from environment */
124 /* first, look for environment variables */
125 if ((value = _nc_getenv_num("LINES")) > 0) {
128 if ((value = _nc_getenv_num("COLUMNS")) > 0) {
131 T(("screen size: environment LINES = %d COLUMNS = %d", *linep, *colp));
134 if (*linep <= 0 || *colp <= 0) {
136 _scrsize(screendata);
137 *colp = screendata[0];
138 *linep = screendata[1];
139 T(("EMX screen size: environment LINES = %d COLUMNS = %d",
144 /* if that didn't work, maybe we can try asking the OS */
145 if (*linep <= 0 || *colp <= 0) {
146 if (isatty(cur_term->Filedes)) {
151 if (ioctl(cur_term->Filedes, IOCTL_WINSIZE, &size) < 0
158 * Solaris lets users override either dimension with an
159 * environment variable.
162 *linep = WINSIZE_ROWS(size);
164 *colp = WINSIZE_COLS(size);
169 #endif /* HAVE_SIZECHANGE */
171 /* if we can't get dynamic info about the size, use static */
173 *linep = (int) lines;
176 *colp = (int) columns;
179 /* the ultimate fallback, assume fixed 24x80 size */
188 * Put the derived values back in the screen-size caps, so
189 * tigetnum() and tgetnum() will do the right thing.
191 lines = (short) (*linep);
192 columns = (short) (*colp);
195 T(("screen size is %dx%d", *linep, *colp));
197 if (VALID_NUMERIC(init_tabs))
198 TABSIZE = (int) init_tabs;
201 T(("TABSIZE = %d", TABSIZE));
206 _nc_update_screensize(void)
208 int my_lines, my_cols;
210 _nc_get_screensize(&my_lines, &my_cols);
211 if (SP != 0 && SP->_resize != 0)
212 SP->_resize(my_lines, my_cols);
216 /****************************************************************************
220 ****************************************************************************/
222 #define ret_error(code, fmt, arg) if (errret) {\
226 fprintf(stderr, fmt, arg);\
230 #define ret_error0(code, msg) if (errret) {\
234 fprintf(stderr, msg);\
238 #if USE_DATABASE || USE_TERMCAP
240 grab_entry(const char *const tn, TERMTYPE * const tp)
241 /* return 1 if entry found, 0 if not found, -1 if database not accessible */
244 char filename[PATH_MAX];
249 * $TERM shouldn't contain pathname delimiters.
255 if ((status = _nc_read_entry(tn, filename, tp)) != 1) {
259 * Try falling back on the termcap file.
260 * Note: allowing this call links the entire terminfo/termcap
261 * compiler into the startup code. It's preferable to build a
262 * real terminfo database and use that.
264 status = _nc_read_termcap_entry(tn, tp);
265 #endif /* PURE_TERMINFO */
269 status = _nc_read_termcap_entry(tn, tp);
273 * If we have an entry, force all of the cancelled strings to null
274 * pointers so we don't have to test them in the rest of the library.
275 * (The terminfo compiler bypasses this logic, since it must know if
276 * a string is cancelled, for merging entries).
280 for_each_boolean(n, tp) {
281 if (!VALID_BOOLEAN(tp->Booleans[n]))
282 tp->Booleans[n] = FALSE;
284 for_each_string(n, tp) {
285 if (tp->Strings[n] == CANCELLED_STRING)
286 tp->Strings[n] = ABSENT_STRING;
296 ** Take the real command character out of the CC environment variable
297 ** and substitute it in for the prototype given in 'command_character'.
310 proto = *command_character;
312 for_each_string(i, &(cur_term->type)) {
313 for (tmp = cur_term->type.Strings[i]; *tmp; tmp++) {
321 * Check if we are running in a UTF-8 locale.
323 NCURSES_EXPORT(char *)
327 if (((env = getenv("LC_ALL")) != 0 && *env != '\0')
328 || ((env = getenv("LC_CTYPE")) != 0 && *env != '\0')
329 || ((env = getenv("LANG")) != 0 && *env != '\0')) {
336 * Check if we are running in a UTF-8 locale.
339 _nc_unicode_locale(void)
341 char *env = _nc_get_locale();
343 if (strstr(env, ".UTF-8") != 0)
350 * Check for known broken cases where a UTF-8 locale breaks the alternate
354 _nc_locale_breaks_acs(void)
356 char *env = getenv("TERM");
358 if (strstr(env, "linux"))
359 return 1; /* always broken */
360 if (strstr(env, "screen") != 0
361 && ((env = getenv("TERMCAP")) != 0
362 && strstr(env, "screen") != 0)
363 && strstr(env, "hhII00") != 0) {
371 * setupterm(termname, Filedes, errret)
373 * Find and read the appropriate object file for the terminal
374 * Make cur_term point to the structure.
379 setupterm(NCURSES_CONST char *tname, int Filedes, int *errret)
384 T((T_CALLED("setupterm(%s,%d,%p)"), _nc_visbuf(tname), Filedes, errret));
387 tname = getenv("TERM");
388 if (tname == 0 || *tname == '\0') {
389 ret_error0(-1, "TERM environment variable not set.\n");
392 if (strlen(tname) > MAX_NAME_SIZE) {
393 ret_error(-1, "TERM environment must be <= %d characters.\n",
397 T(("your terminal name is %s", tname));
400 * Allow output redirection. This is what SVr3 does. If stdout is
401 * directed to a file, screen updates go to standard error.
403 if (Filedes == STDOUT_FILENO && !isatty(Filedes))
404 Filedes = STDERR_FILENO;
407 * Check if we have already initialized to use this terminal. If so, we
408 * do not need to re-read the terminfo entry, or obtain TTY settings.
410 * This is an improvement on SVr4 curses. If an application mixes curses
411 * and termcap calls, it may call both initscr and tgetent. This is not
412 * really a good thing to do, but can happen if someone tries using ncurses
413 * with the readline library. The problem we are fixing is that when
414 * tgetent calls setupterm, the resulting Ottyb struct in cur_term is
415 * zeroed. A subsequent call to endwin uses the zeroed terminal settings
416 * rather than the ones saved in initscr. So we check if cur_term appears
417 * to contain terminal settings for the same output file as our current
418 * call - and copy those terminal settings. (SVr4 curses does not do this,
419 * however applications that are working around the problem will still work
420 * properly with this feature).
423 && cur_term->Filedes == Filedes
424 && cur_term->_termname != 0
425 && !strcmp(cur_term->_termname, tname)
426 && _nc_name_match(cur_term->type.term_names, tname, "|")) {
427 T(("reusing existing terminal information and mode-settings"));
431 term_ptr = typeCalloc(TERMINAL, 1);
435 "Not enough memory to create terminal structure.\n");
437 #if USE_DATABASE || USE_TERMCAP
438 status = grab_entry(tname, &term_ptr->type);
443 /* try fallback list if entry on disk */
445 const TERMTYPE *fallback = _nc_fallback(tname);
448 term_ptr->type = *fallback;
454 del_curterm(term_ptr);
456 ret_error0(-1, "terminals database is inaccessible\n");
457 } else if (status == 0) {
458 ret_error(0, "'%s': unknown terminal type.\n", tname);
462 set_curterm(term_ptr);
464 if (command_character && getenv("CC"))
467 strncpy(ttytype, cur_term->type.term_names, NAMESIZE - 1);
468 ttytype[NAMESIZE - 1] = '\0';
470 cur_term->Filedes = Filedes;
471 cur_term->_termname = strdup(tname);
474 * If an application calls setupterm() rather than initscr() or
475 * newterm(), we will not have the def_prog_mode() call in
476 * _nc_setupscreen(). Do it now anyway, so we can initialize the
479 if (isatty(Filedes)) {
486 * We should always check the screensize, just in case.
488 _nc_get_screensize(&LINES, &COLS);
493 T((T_CREATE("screen %s %dx%d"), tname, LINES, COLS));
496 ret_error(0, "'%s': I need something more specific.\n", tname);
499 ret_error(1, "'%s': I can't handle hardcopy terminals.\n", tname);