#include <curses.priv.h>
#include <tic.h>
#include <term_entry.h>
#if SVR4_TERMIO && !defined(_POSIX_SOURCE)
#define _POSIX_SOURCE
#endif
#include <term.h>
MODULE_ID("$Id: lib_setup.c,v 1.1.1.2 2002/01/03 23:53:44 jevans Exp $")
#if HAVE_SIZECHANGE
# if !defined(sun) || !TERMIOS
# if HAVE_SYS_IOCTL_H
# include <sys/ioctl.h>
# endif
# endif
#endif
#if NEED_PTEM_H
# include <sys/stream.h>
# include <sys/ptem.h>
#endif
#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
#endif
static int _use_env = TRUE;
static void do_prototype(void);
NCURSES_EXPORT(void)
use_env(bool f)
{
T((T_CALLED("use_env()")));
_use_env = f;
returnVoid;
}
NCURSES_EXPORT_VAR(int) LINES = 0;
NCURSES_EXPORT_VAR(int) COLS = 0;
NCURSES_EXPORT_VAR(int) TABSIZE = 0;
static void
_nc_get_screensize(int *linep, int *colp)
{
T(("screen size: terminfo lines = %d columns = %d", lines, columns));
if (!_use_env) {
*linep = (int) lines;
*colp = (int) columns;
} else {
int value;
*linep = *colp = 0;
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));
#ifdef __EMX__
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",
*linep, *colp));
}
#endif
#if HAVE_SIZECHANGE
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);
if (*linep <= 0)
*linep = WINSIZE_ROWS(size);
if (*colp <= 0)
*colp = WINSIZE_COLS(size);
}
failure:;
}
#endif
if (*linep <= 0) {
*linep = (int) lines;
}
if (*colp <= 0) {
*colp = (int) columns;
}
if (*linep <= 0) {
*linep = 24;
}
if (*colp <= 0) {
*colp = 80;
}
lines = (short) (*linep);
columns = (short) (*colp);
}
T(("screen size is %dx%d", *linep, *colp));
if (VALID_NUMERIC(init_tabs))
TABSIZE = (int) init_tabs;
else
TABSIZE = 8;
T(("TABSIZE = %d", TABSIZE));
}
#if USE_SIZECHANGE
NCURSES_EXPORT(void)
_nc_update_screensize(void)
{
int my_lines, my_cols;
_nc_get_screensize(&my_lines, &my_cols);
if (SP != 0 && SP->_resize != 0)
SP->_resize(my_lines, my_cols);
}
#endif
#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);\
}
#if USE_DATABASE || USE_TERMCAP
static int
grab_entry(const char *const tn, TERMTYPE * const tp)
{
char filename[PATH_MAX];
int status;
if (strchr(tn, '/'))
return 0;
#if USE_DATABASE
if ((status = _nc_read_entry(tn, filename, tp)) != 1) {
#if !PURE_TERMINFO
status = _nc_read_termcap_entry(tn, tp);
#endif
}
#else
status = _nc_read_termcap_entry(tn, tp);
#endif
if (status == 1) {
int n;
for_each_boolean(n, tp) {
if (!VALID_BOOLEAN(tp->Booleans[n]))
tp->Booleans[n] = FALSE;
}
for_each_string(n, tp) {
if (tp->Strings[n] == CANCELLED_STRING)
tp->Strings[n] = ABSENT_STRING;
}
}
return (status);
}
#endif
NCURSES_EXPORT_VAR(char) ttytype[NAMESIZE] = "";
NCURSES_EXPORT(int)
setupterm(NCURSES_CONST char *tname, int Filedes, int *errret)
{
struct term *term_ptr;
int status;
T((T_CALLED("setupterm(%s,%d,%p)"), _nc_visbuf(tname), Filedes, errret));
if (tname == 0) {
tname = getenv("TERM");
if (tname == 0 || *tname == '\0') {
ret_error0(-1, "TERM environment variable not set.\n");
}
}
if (strlen(tname) > MAX_NAME_SIZE) {
ret_error(-1, "TERM environment must be <= %d characters.\n",
MAX_NAME_SIZE);
}
T(("your terminal name is %s", tname));
term_ptr = typeCalloc(TERMINAL, 1);
if (term_ptr == 0) {
ret_error0(-1, "Not enough memory to create terminal structure.\n");
}
#if USE_DATABASE || USE_TERMCAP
status = grab_entry(tname, &term_ptr->type);
#else
status = 0;
#endif
if (status != 1) {
const TERMTYPE *fallback = _nc_fallback(tname);
if (fallback) {
term_ptr->type = *fallback;
status = 1;
}
}
if (status == -1) {
ret_error0(-1, "terminals database is inaccessible\n");
} else if (status == 0) {
ret_error(0, "'%s': unknown terminal type.\n", tname);
}
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';
if (Filedes == STDOUT_FILENO && !isatty(Filedes))
Filedes = STDERR_FILENO;
cur_term->Filedes = Filedes;
_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);
}
static void
do_prototype(void)
{
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;
}
}
}