#ifdef M_RCSID
#ifndef lint
static char const rcsID[] = "$Header: /rd/src/libc/xcurses/rcs/newterm.c 1.15 1995/07/25 19:54:00 ant Exp $";
#endif
#endif
#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)
{
#ifdef M_CURSES_TRACE
__m_trace("filter(void)");
#endif
assume_one_line = TRUE;
__m_return_void("filter");
}
void
tstp(signo)
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;
#ifdef M_CURSES_TRACE
__m_trace("__m_slk_init(%d)", style );
#endif
code = ERR;
if (plab_norm != (char *) 0 || pkey_plab != (char *) 0) {
code = OK;
goto done;
}
if (lines < 2)
goto done;
sp->_slk._w = subwin(sp->_newscr, 1, 0, --lines, 0);
if (sp->_slk._w == (WINDOW *) 0)
goto done;
code = OK;
done:
return __m_return_code("__m_slk_init", code);
}
static t_rip rip = { 0 };
int
ripoffline(int line, int (*init)(WINDOW *, int))
{
int i;
#ifdef M_CURSES_TRACE
__m_trace("ripoffline(%d, %p)", line, init);
#endif
i = rip.top - rip.bottom;
if (line != 0 && i + 1 < 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 __m_return_code("ripoffline", OK);
}
SCREEN *
newterm(term, out_fp, in_fp)
char *term;
FILE *out_fp, *in_fp;
{
WINDOW *w;
t_wide_io *wio;
SCREEN *sp, *osp;
int i, n, y, errret;
#ifdef M_CURSES_TRACE
__m_trace(
"newterm(%s, %p, %p)",
term == (char *) 0 ? "NULL" : term, out_fp, in_fp
);
#endif
(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)) == (SCREEN *) 0)
goto error1;
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 < MB_LEN_MAX)
sp->_unget._size = MB_LEN_MAX;
sp->_unget._stack = calloc(
(size_t) sp->_unget._size, sizeof *sp->_unget._stack
);
if (sp->_unget._stack == (void *) 0)
goto error2;
if ((wio = (t_wide_io *) calloc(1, sizeof *wio)) == (t_wide_io *) 0)
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 =
cursor_home =
cursor_to_ll =
cursor_address =
row_address =
parm_up_cursor =
parm_down_cursor = (char *) 0;
__m_mvcur_cost();
assume_one_line = FALSE;
}
if ((sp->_curscr = newwin(lines, columns, 0, 0)) == (WINDOW *) 0)
goto error2;
if ((sp->_newscr = newwin(lines, columns, 0, 0)) == (WINDOW *) 0)
goto error2;
sp->_hash = (unsigned long *) calloc(lines, sizeof *sp->_hash);
if (sp->_hash == (unsigned long *) 0)
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);
cur_term->_prog.c_lflag &= ~ECHO;
cur_term->_prog.c_iflag |= ICRNL;
cur_term->_prog.c_oflag |= OPOST;
#ifdef ONLCR
cur_term->_prog.c_oflag |= ONLCR;
#endif
cur_term->_flags |= __TERM_NL_IS_CRLF;
#ifdef TAB0
cur_term->_prog.c_oflag &= ~(TAB1|TAB2|TAB3);
#endif
(void) __m_tty_set(&cur_term->_prog);
(void) __m_set_echo(1);
(void) typeahead(fileno(in_fp));
if (stdscr == (WINDOW *) 0) {
n = rip.top - rip.bottom;
stdscr = newwin(lines - n, 0, rip.top, 0);
if (stdscr == (WINDOW *) 0)
goto error3;
for (i = 0; i < n; ++i) {
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);
}
}
return __m_return_pointer("newterm", sp);
error3:
(void) set_term(osp);
error2:
delscreen(sp);
error1:
return __m_return_pointer("newterm", (SCREEN *) 0);
}
void
delscreen(sp)
SCREEN *sp;
{
#ifdef M_CURSES_TRACE
__m_trace("delscreen(%p)", sp);
#endif
if (sp != (SCREEN *) 0) {
if (sp->_slk._w != (WINDOW *) 0)
(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 != (unsigned long *) 0)
free(sp->_hash);
if (sp->_unget._stack != (int *) 0)
free(sp->_unget._stack);
if (sp->_in != (void *) 0)
free(sp->_in);
free(sp);
}
__m_return_void("delscreen");
}
SCREEN *
set_term(screen)
SCREEN *screen;
{
SCREEN *osp = __m_screen;
#ifdef M_CURSES_TRACE
__m_trace("set_term(%p)", screen);
#endif
if (screen != (SCREEN *) 0) {
(void) set_curterm(screen->_term);
curscr = screen->_curscr;
__m_screen = screen;
LINES = lines;
COLS = columns;
COLORS = max_colors;
COLOR_PAIRS = max_pairs;
}
return __m_return_pointer("set_term", osp);
}
int
typeahead(fd)
int fd;
{
#ifdef M_CURSES_TRACE
__m_trace("typeahead(%d)", fd);
#endif
__m_screen->_kfd = fd;
__m_screen->_flags &= ~S_ISATTY;
if (fd != -1 && isatty(fd))
__m_screen->_flags |= S_ISATTY;
return __m_return_code("typeahead", OK);
}
int
__m_set_echo(bf)
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;
}