#ifdef M_RCSID
#ifndef lint
static char const rcsID[] =
"$Header: /team/ps/sun_xcurses/archive/local_changes/xcurses/src/lib/"
"libxcurses/src/libc/xcurses/rcs/newterm.c 1.13 1998/06/04 19:55:52 "
"cbates Exp $";
#endif
#endif
#include <sys/isa_defs.h>
#include <private.h>
#include <m_wio.h>
#include <errno.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
int LINES, COLS;
int COLORS, COLOR_PAIRS;
WINDOW *curscr;
WINDOW *stdscr;
SCREEN *__m_screen;
static short assume_one_line = FALSE;
void
filter(void)
{
assume_one_line = TRUE;
}
void
tstp(int signo)
{
#ifdef SIGTSTP
if (getpid() == getpgrp()) {
(void) endwin();
#ifdef SIG_UNBLOCK
{
sigset_t unblock;
(void) sigemptyset(&unblock);
(void) sigaddset(&unblock, SIGTSTP);
(void) sigprocmask(SIG_UNBLOCK, &unblock,
(sigset_t *) 0);
}
#endif
(void) signal(SIGTSTP, SIG_DFL);
(void) kill(0, SIGTSTP);
} else {
(void) beep();
}
(void) signal(SIGTSTP, tstp);
(void) wrefresh(curscr);
#else
(void) beep();
#endif
}
int __m_slk_format = -1;
int
__m_slk_init(SCREEN *sp, int style)
{
int code;
code = ERR;
(void) memset(&sp->_slk, 0, sizeof (sp->_slk));
if (plab_norm != NULL || pkey_plab != NULL) {
code = OK;
goto done;
}
if (lines < 2)
goto done;
sp->_slk._w = subwin(sp->_newscr, 1, 0, lines-1, 0);
if (sp->_slk._w == NULL)
goto done;
(void) wattrset(sp->_slk._w, A_DIM|A_REVERSE);
(void) ripoffline(-1, 0);
code = OK;
done:
return (code);
}
t_rip rip = { 0 };
int
ripoffline(int line, int (*init)(WINDOW *, int))
{
int i;
i = rip.top - rip.bottom;
if (line != 0 && i < M_CURSES_MAX_RIPOFFLINE) {
rip.line[i].init = init;
if (line < 0)
rip.line[i].dy = --rip.bottom;
else
rip.line[i].dy = rip.top++;
}
return (OK);
}
SCREEN *
newterm(char *term, FILE *out_fp, FILE *in_fp)
{
WINDOW *w;
t_wide_io *wio;
SCREEN *sp, *osp;
int i, n, y, errret;
(void) setvbuf(in_fp, (char *) 0, _IONBF, BUFSIZ);
#if 0
(void) setvbuf(out_fp, (char *) 0, _IOLBF, BUFSIZ);
#endif
errno = 0;
if (__m_setupterm(term, fileno(in_fp), fileno(out_fp), &errret)
== ERR) {
switch (errret) {
case -1:
errno = ENOMEM;
break;
case 2:
errno = ENAMETOOLONG;
break;
case 0:
default:
errno = ENOENT;
break;
}
goto error1;
}
if (__m_doupdate_init())
goto error1;
if ((sp = (SCREEN *) calloc(1, sizeof (*sp))) == NULL)
goto error1;
sp->_kfd = -1;
sp->_if = in_fp;
sp->_of = out_fp;
sp->_term = cur_term;
sp->_unget._size = __m_decode_init((t_decode **) &sp->_decode);
if (sp->_unget._size < (M_TYPEAHEAD_SIZE + MB_LEN_MAX))
sp->_unget._size = M_TYPEAHEAD_SIZE + MB_LEN_MAX;
sp->_unget._stack = calloc((size_t) sp->_unget._size,
sizeof (*sp->_unget._stack));
if (sp->_unget._stack == NULL)
goto error2;
if ((wio = (t_wide_io *) calloc(1, sizeof (*wio))) == NULL)
goto error2;
wio->get = (int (*)(void *)) wgetch;
wio->unget = __xc_ungetc;
wio->reset = __xc_clearerr;
wio->iserror = __xc_ferror;
wio->iseof = __xc_feof;
sp->_in = wio;
if (assume_one_line) {
lines = 1;
clear_screen = clr_eos = cursor_up = cursor_down = NULL;
cursor_home = cursor_to_ll = cursor_address = NULL;
row_address = parm_up_cursor = parm_down_cursor = NULL;
__m_mvcur_cost();
assume_one_line = FALSE;
}
if ((sp->_curscr = newwin(lines, columns, 0, 0)) == NULL)
goto error2;
if ((sp->_newscr = newwin(lines, columns, 0, 0)) == NULL)
goto error2;
#if defined(_LP64)
sp->_hash = (unsigned int *) calloc(lines, sizeof (*sp->_hash));
#else
sp->_hash = (unsigned long *) calloc(lines, sizeof (*sp->_hash));
#endif
if (sp->_hash == NULL)
goto error2;
if (0 <= __m_slk_format && __m_slk_init(sp, __m_slk_format) == ERR) {
goto error2;
}
sp->_flags |= S_ENDWIN;
#ifdef SIGTSTP
(void) signal(SIGTSTP, tstp);
#endif
osp = set_term(sp);
PTERMIOS(_prog)->c_lflag &= ~ECHO;
PTERMIOS(_prog)->c_iflag |= ICRNL;
PTERMIOS(_prog)->c_oflag |= OPOST;
#ifdef ONLCR
PTERMIOS(_prog)->c_oflag |= ONLCR;
#endif
cur_term->_flags |= __TERM_NL_IS_CRLF;
#ifdef TAB0
PTERMIOS(_prog)->c_oflag &= ~(TAB1|TAB2|TAB3);
#endif
cur_term->_flags &= ~__TERM_HALF_DELAY;
__m_screen->_flags |= S_INS_DEL_CHAR;
PTERMIOS(_prog)->c_cc[VMIN] = 1;
PTERMIOS(_prog)->c_cc[VTIME] = 0;
PTERMIOS(_prog)->c_lflag &= ~ICANON;
(void) __m_tty_set_prog_mode();
(void) __m_set_echo(1);
(void) typeahead(fileno(in_fp));
(void) __m_slk_clear(1);
n = rip.top - rip.bottom;
if (stdscr == NULL) {
stdscr = newwin(lines - n, 0, rip.top, 0);
if (stdscr == NULL)
goto error3;
}
for (i = 0; i < n; ++i) {
if (rip.line[i].created)
continue;
y = rip.line[i].dy;
if (y < 0)
y += lines;
w = newwin(1, 0, y, 0);
if (rip.line[i].init != (int (*)(WINDOW *, int)) 0)
(void) (*rip.line[i].init)(w, columns);
rip.line[i].created = 1;
}
LINES = stdscr->_maxy = sp->_curscr->_maxy - n;
return (sp);
error3:
(void) set_term(osp);
error2:
delscreen(sp);
error1:
return (NULL);
}
void
delscreen(SCREEN *sp)
{
if (sp != NULL) {
if (sp->_slk._w != NULL)
(void) delwin(sp->_slk._w);
(void) delwin(sp->_newscr);
(void) delwin(sp->_curscr);
(void) del_curterm(sp->_term);
__m_decode_free((t_decode **) &sp->_decode);
if (sp->_hash != NULL)
free(sp->_hash);
if (sp->_unget._stack != NULL)
free(sp->_unget._stack);
if (sp->_in != NULL)
free(sp->_in);
free(sp);
}
}
SCREEN *
set_term(SCREEN *screen)
{
SCREEN *osp = __m_screen;
if (screen != NULL) {
(void) set_curterm(screen->_term);
curscr = screen->_curscr;
__m_screen = screen;
LINES = lines;
COLS = columns;
COLORS = max_colors;
COLOR_PAIRS = max_pairs;
}
return (osp);
}
int
typeahead(int fd)
{
__m_screen->_flags &= ~S_ISATTY;
if (fd != -1) {
if (isatty(fd)) {
__m_screen->_kfd = fd;
__m_screen->_flags |= S_ISATTY;
} else {
__m_screen->_kfd = -1;
}
}
return (OK);
}
int
__m_set_echo(int bf)
{
int old;
old = (__m_screen->_flags & S_ECHO) == S_ECHO;
__m_screen->_flags &= ~S_ECHO;
if (bf)
__m_screen->_flags |= S_ECHO;
return (old);
}