ncursesw-morphos/ncurses/tinfo/lib_setup.c

438 lines
12 KiB
C
Raw Normal View History

1998-03-01 12:21:12 +08:00
/****************************************************************************
2002-10-13 11:35:53 +08:00
* Copyright (c) 1998-2001,2002 Free Software Foundation, Inc. *
1998-03-01 12:21:12 +08:00
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the *
* "Software"), to deal in the Software without restriction, including *
* without limitation the rights to use, copy, modify, merge, publish, *
* distribute, distribute with modifications, sublicense, and/or sell *
* copies of the Software, and to permit persons to whom the Software is *
* furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included *
* in all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
* IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
* THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
* *
* Except as contained in this notice, the name(s) of the above copyright *
* holders shall not be used in advertising or otherwise to promote the *
* sale, use or other dealings in this Software without prior written *
* authorization. *
****************************************************************************/
1997-05-15 12:00:00 +08:00
1998-03-01 12:21:12 +08:00
/****************************************************************************
* Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
* and: Eric S. Raymond <esr@snark.thyrsus.com> *
****************************************************************************/
1997-05-15 12:00:00 +08:00
/*
* Terminal setup routines common to termcap and terminfo:
*
* use_env(bool)
* setupterm(char *, int, int *)
*/
#include <curses.priv.h>
2000-07-09 10:46:08 +08:00
#include <tic.h> /* for MAX_NAME_SIZE */
1999-10-24 12:32:42 +08:00
#include <term_entry.h>
1997-05-15 12:00:00 +08:00
2000-10-21 12:42:11 +08:00
#if SVR4_TERMIO && !defined(_POSIX_SOURCE)
1997-05-15 12:00:00 +08:00
#define _POSIX_SOURCE
#endif
2000-07-09 10:46:08 +08:00
#include <term.h> /* lines, columns, cur_term */
1997-05-15 12:00:00 +08:00
2002-10-13 11:35:53 +08:00
MODULE_ID("$Id: lib_setup.c,v 1.70 2002/10/12 21:50:18 tom Exp $")
1997-05-15 12:00:00 +08:00
/****************************************************************************
*
* Terminal size computation
*
****************************************************************************/
1998-03-01 12:21:12 +08:00
#if HAVE_SIZECHANGE
1999-10-24 12:32:42 +08:00
# if !defined(sun) || !TERMIOS
1998-03-01 12:21:12 +08:00
# if HAVE_SYS_IOCTL_H
# include <sys/ioctl.h>
# endif
# endif
#endif
#if NEED_PTEM_H
/* On SCO, they neglected to define struct winsize in termios.h -- it's only
* in termio.h and ptem.h (the former conflicts with other definitions).
*/
# include <sys/stream.h>
# include <sys/ptem.h>
#endif
/*
* SCO defines TIOCGSIZE and the corresponding struct. Other systems (SunOS,
* Solaris, IRIX) define TIOCGWINSZ and struct winsize.
*/
#ifdef TIOCGSIZE
# define IOCTL_WINSIZE TIOCGSIZE
# define STRUCT_WINSIZE struct ttysize
# define WINSIZE_ROWS(n) (int)n.ts_lines
# define WINSIZE_COLS(n) (int)n.ts_cols
#else
# ifdef TIOCGWINSZ
# define IOCTL_WINSIZE TIOCGWINSZ
# define STRUCT_WINSIZE struct winsize
# define WINSIZE_ROWS(n) (int)n.ws_row
# define WINSIZE_COLS(n) (int)n.ws_col
# endif
1997-05-15 12:00:00 +08:00
#endif
static int _use_env = TRUE;
static void do_prototype(void);
2002-10-13 11:35:53 +08:00
NCURSES_EXPORT(void)
2000-07-09 10:46:08 +08:00
use_env(bool f)
1997-05-15 12:00:00 +08:00
{
2002-10-13 11:35:53 +08:00
T((T_CALLED("use_env()")));
2000-07-09 10:46:08 +08:00
_use_env = f;
2002-10-13 11:35:53 +08:00
returnVoid;
1997-05-15 12:00:00 +08:00
}
2002-10-13 11:35:53 +08:00
NCURSES_EXPORT_VAR(int) LINES = 0;
NCURSES_EXPORT_VAR(int) COLS = 0;
NCURSES_EXPORT_VAR(int) TABSIZE = 0;
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
static void
_nc_get_screensize(int *linep, int *colp)
1998-03-01 12:21:12 +08:00
/* Obtain lines/columns values from the environment and/or terminfo entry */
1997-05-15 12:00:00 +08:00
{
2000-07-09 10:46:08 +08:00
/* figure out the size of the screen */
T(("screen size: terminfo lines = %d columns = %d", lines, columns));
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
if (!_use_env) {
*linep = (int) lines;
*colp = (int) columns;
} else { /* usually want to query LINES and COLUMNS from environment */
int value;
1999-10-24 12:32:42 +08:00
2000-07-09 10:46:08 +08:00
*linep = *colp = 0;
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
/* first, look for environment variables */
if ((value = _nc_getenv_num("LINES")) > 0) {
*linep = value;
}
if ((value = _nc_getenv_num("COLUMNS")) > 0) {
*colp = value;
}
T(("screen size: environment LINES = %d COLUMNS = %d", *linep, *colp));
1997-05-15 12:00:00 +08:00
1999-10-24 12:32:42 +08:00
#ifdef __EMX__
2000-07-09 10:46:08 +08:00
if (*linep <= 0 || *colp <= 0) {
int screendata[2];
_scrsize(screendata);
*colp = screendata[0];
*linep = screendata[1];
T(("EMX screen size: environment LINES = %d COLUMNS = %d",
2002-10-13 11:35:53 +08:00
*linep, *colp));
2000-07-09 10:46:08 +08:00
}
1999-10-24 12:32:42 +08:00
#endif
1998-03-01 12:21:12 +08:00
#if HAVE_SIZECHANGE
2000-07-09 10:46:08 +08:00
/* if that didn't work, maybe we can try asking the OS */
if (*linep <= 0 || *colp <= 0) {
if (isatty(cur_term->Filedes)) {
STRUCT_WINSIZE size;
errno = 0;
do {
if (ioctl(cur_term->Filedes, IOCTL_WINSIZE, &size) < 0
&& errno != EINTR)
goto failure;
} while
(errno == EINTR);
/*
* Solaris lets users override either dimension with an
* environment variable.
*/
if (*linep <= 0)
*linep = WINSIZE_ROWS(size);
if (*colp <= 0)
*colp = WINSIZE_COLS(size);
1997-05-15 12:00:00 +08:00
}
2000-07-09 10:46:08 +08:00
/* FALLTHRU */
failure:;
}
1998-03-01 12:21:12 +08:00
#endif /* HAVE_SIZECHANGE */
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
/* if we can't get dynamic info about the size, use static */
2002-10-13 11:35:53 +08:00
if (*linep <= 0) {
*linep = (int) lines;
}
if (*colp <= 0) {
*colp = (int) columns;
}
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
/* the ultimate fallback, assume fixed 24x80 size */
2002-10-13 11:35:53 +08:00
if (*linep <= 0) {
2000-07-09 10:46:08 +08:00
*linep = 24;
2002-10-13 11:35:53 +08:00
}
if (*colp <= 0) {
2000-07-09 10:46:08 +08:00
*colp = 80;
1997-05-15 12:00:00 +08:00
}
2000-07-09 10:46:08 +08:00
/*
* Put the derived values back in the screen-size caps, so
* tigetnum() and tgetnum() will do the right thing.
*/
lines = (short) (*linep);
columns = (short) (*colp);
}
T(("screen size is %dx%d", *linep, *colp));
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
if (VALID_NUMERIC(init_tabs))
TABSIZE = (int) init_tabs;
else
TABSIZE = 8;
T(("TABSIZE = %d", TABSIZE));
1997-05-15 12:00:00 +08:00
}
1998-03-01 12:21:12 +08:00
#if USE_SIZECHANGE
2002-10-13 11:35:53 +08:00
NCURSES_EXPORT(void)
2000-07-09 10:46:08 +08:00
_nc_update_screensize(void)
1998-03-01 12:21:12 +08:00
{
2000-07-09 10:46:08 +08:00
int my_lines, my_cols;
1998-03-01 12:21:12 +08:00
2000-07-09 10:46:08 +08:00
_nc_get_screensize(&my_lines, &my_cols);
if (SP != 0 && SP->_resize != 0)
SP->_resize(my_lines, my_cols);
1998-03-01 12:21:12 +08:00
}
#endif
1997-05-15 12:00:00 +08:00
/****************************************************************************
*
* Terminal setup
*
****************************************************************************/
#define ret_error(code, fmt, arg) if (errret) {\
*errret = code;\
returnCode(ERR);\
} else {\
fprintf(stderr, fmt, arg);\
exit(EXIT_FAILURE);\
}
#define ret_error0(code, msg) if (errret) {\
*errret = code;\
returnCode(ERR);\
} else {\
fprintf(stderr, msg);\
exit(EXIT_FAILURE);\
}
2002-10-13 11:35:53 +08:00
#if USE_DATABASE || USE_TERMCAP
2000-07-09 10:46:08 +08:00
static int
grab_entry(const char *const tn, TERMTYPE * const tp)
1997-05-15 12:00:00 +08:00
/* return 1 if entry found, 0 if not found, -1 if database not accessible */
{
2000-07-09 10:46:08 +08:00
char filename[PATH_MAX];
int status;
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
/*
* $TERM shouldn't contain pathname delimiters.
*/
if (strchr(tn, '/'))
return 0;
1999-10-24 12:32:42 +08:00
2002-10-13 11:35:53 +08:00
#if USE_DATABASE
2000-07-09 10:46:08 +08:00
if ((status = _nc_read_entry(tn, filename, tp)) != 1) {
1999-10-24 12:32:42 +08:00
2000-10-21 12:42:11 +08:00
#if !PURE_TERMINFO
1999-10-24 12:32:42 +08:00
/*
2000-07-09 10:46:08 +08:00
* Try falling back on the termcap file.
* Note: allowing this call links the entire terminfo/termcap
* compiler into the startup code. It's preferable to build a
* real terminfo database and use that.
1999-10-24 12:32:42 +08:00
*/
2000-07-09 10:46:08 +08:00
status = _nc_read_termcap_entry(tn, tp);
#endif /* PURE_TERMINFO */
}
2002-10-13 11:35:53 +08:00
#else
status = _nc_read_termcap_entry(tn, tp);
#endif
2000-07-09 10:46:08 +08:00
/*
* If we have an entry, force all of the cancelled strings to null
* pointers so we don't have to test them in the rest of the library.
* (The terminfo compiler bypasses this logic, since it must know if
* a string is cancelled, for merging entries).
*/
if (status == 1) {
int n;
2002-10-13 11:35:53 +08:00
for_each_boolean(n, tp) {
2000-07-09 10:46:08 +08:00
if (!VALID_BOOLEAN(tp->Booleans[n]))
2002-10-13 11:35:53 +08:00
tp->Booleans[n] = FALSE;
}
for_each_string(n, tp) {
2000-07-09 10:46:08 +08:00
if (tp->Strings[n] == CANCELLED_STRING)
2002-10-13 11:35:53 +08:00
tp->Strings[n] = ABSENT_STRING;
}
2000-07-09 10:46:08 +08:00
}
return (status);
1997-05-15 12:00:00 +08:00
}
1998-03-01 12:21:12 +08:00
#endif
1997-05-15 12:00:00 +08:00
2002-10-13 11:35:53 +08:00
NCURSES_EXPORT_VAR(char) ttytype[NAMESIZE] = "";
1997-05-15 12:00:00 +08:00
/*
* setupterm(termname, Filedes, errret)
*
* Find and read the appropriate object file for the terminal
* Make cur_term point to the structure.
*
*/
2002-10-13 11:35:53 +08:00
NCURSES_EXPORT(int)
2000-07-09 10:46:08 +08:00
setupterm(NCURSES_CONST char *tname, int Filedes, int *errret)
1997-05-15 12:00:00 +08:00
{
2000-07-09 10:46:08 +08:00
struct term *term_ptr;
int status;
1997-05-15 12:00:00 +08:00
2002-10-13 11:35:53 +08:00
START_TRACE();
2000-07-09 10:46:08 +08:00
T((T_CALLED("setupterm(%s,%d,%p)"), _nc_visbuf(tname), Filedes, errret));
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
if (tname == 0) {
tname = getenv("TERM");
if (tname == 0 || *tname == '\0') {
ret_error0(-1, "TERM environment variable not set.\n");
1997-05-15 12:00:00 +08:00
}
2000-07-09 10:46:08 +08:00
}
if (strlen(tname) > MAX_NAME_SIZE) {
ret_error(-1, "TERM environment must be <= %d characters.\n",
2002-10-13 11:35:53 +08:00
MAX_NAME_SIZE);
2000-07-09 10:46:08 +08:00
}
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
T(("your terminal name is %s", tname));
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
term_ptr = typeCalloc(TERMINAL, 1);
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
if (term_ptr == 0) {
ret_error0(-1, "Not enough memory to create terminal structure.\n");
}
2002-10-13 11:35:53 +08:00
#if USE_DATABASE || USE_TERMCAP
2000-07-09 10:46:08 +08:00
status = grab_entry(tname, &term_ptr->type);
1998-03-01 12:21:12 +08:00
#else
2000-07-09 10:46:08 +08:00
status = 0;
1998-03-01 12:21:12 +08:00
#endif
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
/* try fallback list if entry on disk */
if (status != 1) {
const TERMTYPE *fallback = _nc_fallback(tname);
1997-05-15 12:00:00 +08:00
2000-07-09 10:46:08 +08:00
if (fallback) {
term_ptr->type = *fallback;
status = 1;
1999-10-24 12:32:42 +08:00
}
2000-07-09 10:46:08 +08:00
}
if (status == -1) {
ret_error0(-1, "terminals database is inaccessible\n");
} else if (status == 0) {
ret_error(0, "'%s': unknown terminal type.\n", tname);
}
/*
* Improve on SVr4 curses. If an application mixes curses and termcap
* calls, it may call both initscr and tgetent. This is not really a
* good thing to do, but can happen if someone tries using ncurses with
* the readline library. The problem we are fixing is that when
* tgetent calls setupterm, the resulting Ottyb struct in cur_term is
* zeroed. A subsequent call to endwin uses the zeroed terminal
* settings rather than the ones saved in initscr. So we check if
* cur_term appears to contain terminal settings for the same output
* file as our current call - and copy those terminal settings. (SVr4
* curses does not do this, however applications that are working
* around the problem will still work properly with this feature).
*/
if (cur_term != 0) {
if (cur_term->Filedes == Filedes)
term_ptr->Ottyb = cur_term->Ottyb;
}
set_curterm(term_ptr);
if (command_character && getenv("CC"))
do_prototype();
strncpy(ttytype, cur_term->type.term_names, NAMESIZE - 1);
ttytype[NAMESIZE - 1] = '\0';
/*
2002-10-13 11:35:53 +08:00
* Allow output redirection. This is what SVr3 does. If stdout is
* directed to a file, screen updates go to standard error.
2000-07-09 10:46:08 +08:00
*/
if (Filedes == STDOUT_FILENO && !isatty(Filedes))
Filedes = STDERR_FILENO;
cur_term->Filedes = Filedes;
2002-10-13 11:35:53 +08:00
/*
* If an application calls setupterm() rather than initscr() or newterm(),
* we will not have the def_prog_mode() call in _nc_setupscreen(). Do it
* now anyway, so we can initialize the baudrate.
*/
if (isatty(Filedes)) {
def_prog_mode();
baudrate();
}
2000-07-09 10:46:08 +08:00
_nc_get_screensize(&LINES, &COLS);
if (errret)
*errret = 1;
T((T_CREATE("screen %s %dx%d"), tname, LINES, COLS));
if (generic_type) {
ret_error(0, "'%s': I need something more specific.\n", tname);
}
if (hard_copy) {
ret_error(1, "'%s': I can't handle hardcopy terminals.\n", tname);
}
returnCode(OK);
1997-05-15 12:00:00 +08:00
}
/*
** do_prototype()
**
** Take the real command character out of the CC environment variable
** and substitute it in for the prototype given in 'command_character'.
**
*/
static void
do_prototype(void)
{
2000-07-09 10:46:08 +08:00
int i;
char CC;
char proto;
char *tmp;
tmp = getenv("CC");
CC = *tmp;
proto = *command_character;
for_each_string(i, &(cur_term->type)) {
for (tmp = cur_term->type.Strings[i]; *tmp; tmp++) {
if (*tmp == proto)
*tmp = CC;
1997-05-15 12:00:00 +08:00
}
2000-07-09 10:46:08 +08:00
}
1997-05-15 12:00:00 +08:00
}