2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License, Version 1.0 only
2N/A * (the "License"). You may not use this file except in compliance
2N/A * with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright (c) 1995-1999 by Sun Microsystems, Inc.
2N/A * All rights reserved.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/* LINTLIBRARY */
2N/A
2N/A/*
2N/A * newterm.c
2N/A *
2N/A * XCurses Library
2N/A *
2N/A * Copyright 1990, 1995 by Mortice Kern Systems Inc. All rights reserved.
2N/A *
2N/A */
2N/A
2N/A#ifdef M_RCSID
2N/A#ifndef lint
2N/Astatic char const rcsID[] =
2N/A"$Header: /team/ps/sun_xcurses/archive/local_changes/xcurses/src/lib/"
2N/A"libxcurses/src/libc/xcurses/rcs/newterm.c 1.13 1998/06/04 19:55:52 "
2N/A"cbates Exp $";
2N/A#endif
2N/A#endif
2N/A
2N/A#include <sys/isa_defs.h>
2N/A#include <private.h>
2N/A#include <m_wio.h>
2N/A#include <errno.h>
2N/A#include <signal.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A
2N/Aint LINES, COLS;
2N/Aint COLORS, COLOR_PAIRS;
2N/A
2N/AWINDOW *curscr;
2N/AWINDOW *stdscr;
2N/ASCREEN *__m_screen;
2N/A
2N/Astatic short assume_one_line = FALSE;
2N/A
2N/A/*
2N/A * Assume terminal has only one screen line by restricting those
2N/A * capabilities that assume more than one line. This function must
2N/A * be called before initscr() or newterm().
2N/A *
2N/A * This flag will reset after initscr() or newterm() so that subsequent
2N/A * calls to newterm(), without a preceding call to filter(), will load
2N/A * an unmodified terminal. THIS IS NOT HISTORICAL PRACTICE, BUT DEEMED
2N/A * USEFUL.
2N/A */
2N/Avoid
2N/Afilter(void)
2N/A{
2N/A assume_one_line = TRUE;
2N/A}
2N/A
2N/A/*
2N/A * SIGTSTP Handler.
2N/A */
2N/A/* ARGSUSED */
2N/Avoid
2N/Atstp(int signo)
2N/A{
2N/A#ifdef SIGTSTP
2N/A /*
2N/A * Only permit SIGTSTP if the curent process is the process
2N/A * group leader. If the process is not the current group
2N/A * leader, then suspending the current process will suspend
2N/A * other members of the process group, such as the parent
2N/A * process.
2N/A */
2N/A if (getpid() == getpgrp()) {
2N/A (void) endwin();
2N/A
2N/A#ifdef SIG_UNBLOCK
2N/A {
2N/A sigset_t unblock;
2N/A
2N/A (void) sigemptyset(&unblock);
2N/A (void) sigaddset(&unblock, SIGTSTP);
2N/A (void) sigprocmask(SIG_UNBLOCK, &unblock,
2N/A (sigset_t *) 0);
2N/A }
2N/A#endif /* SIG_UNBLOCK */
2N/A (void) signal(SIGTSTP, SIG_DFL);
2N/A (void) kill(0, SIGTSTP);
2N/A } else {
2N/A (void) beep();
2N/A }
2N/A
2N/A (void) signal(SIGTSTP, tstp);
2N/A (void) wrefresh(curscr);
2N/A#else /* no SIGTSTP */
2N/A (void) beep();
2N/A#endif /* SIGTSTP */
2N/A}
2N/A
2N/Aint __m_slk_format = -1;
2N/A
2N/A/*
2N/A * Do real soft label key initialisation once setupterm() have been called
2N/A * to load the current terminal. Determine whether the terminal supplies
2N/A * soft label keys, or whether we have to fake it by using the last line
2N/A * of a terminal screen.
2N/A */
2N/A/* ARGSUSED */
2N/Aint
2N/A__m_slk_init(SCREEN *sp, int style)
2N/A{
2N/A int code;
2N/A
2N/A code = ERR;
2N/A
2N/A (void) memset(&sp->_slk, 0, sizeof (sp->_slk));
2N/A
2N/A /* Does the terminal have a method to program the soft label key? */
2N/A if (plab_norm != NULL || pkey_plab != NULL) {
2N/A code = OK;
2N/A goto done;
2N/A }
2N/A
2N/A /* We have to fake it. */
2N/A if (lines < 2)
2N/A goto done;
2N/A
2N/A sp->_slk._w = subwin(sp->_newscr, 1, 0, lines-1, 0);
2N/A if (sp->_slk._w == NULL)
2N/A goto done;
2N/A
2N/A /* Test suite seems to expect this */
2N/A (void) wattrset(sp->_slk._w, A_DIM|A_REVERSE);
2N/A (void) ripoffline(-1, 0);
2N/A code = OK;
2N/Adone:
2N/A return (code);
2N/A}
2N/A
2N/A/*
2N/A * The XCurses specification is unclear how ripoffline() would
2N/A * affect newterm(). We assume that it can't be used with newterm()
2N/A * and that it only affects initscr(), which is responsible for
2N/A * creating stdscr.
2N/A */
2N/At_rip rip = { 0 };
2N/A
2N/A/*
2N/A * If line is positive (1), one line is removed from the beginning of
2N/A * stdscr; else if line is negative (-1), one line is removed from the end.
2N/A */
2N/Aint
2N/Aripoffline(int line, int (*init)(WINDOW *, int))
2N/A{
2N/A int i;
2N/A
2N/A i = rip.top - rip.bottom;
2N/A
2N/A if (line != 0 && i < M_CURSES_MAX_RIPOFFLINE) {
2N/A rip.line[i].init = init;
2N/A if (line < 0)
2N/A rip.line[i].dy = --rip.bottom;
2N/A else
2N/A rip.line[i].dy = rip.top++;
2N/A }
2N/A
2N/A return (OK);
2N/A}
2N/A
2N/A/*
2N/A * Create a new terminal screen. Used if a program is going to be sending
2N/A * output to more than one terminal. It returns a SCREEN* for the terminal.
2N/A * The parameters are a terminal name, output FILE*, and input FILE*. If
2N/A * the terminal name is null then $TERM is used. The program must also
2N/A * call endwin() for each terminal being used before exiting from curses.
2N/A * If newterm() is called more than once for the same terminal, the first
2N/A * terminal referred to must be the last one for which endwin() is called.
2N/A */
2N/ASCREEN *
2N/Anewterm(char *term, FILE *out_fp, FILE *in_fp)
2N/A{
2N/A WINDOW *w;
2N/A t_wide_io *wio;
2N/A SCREEN *sp, *osp;
2N/A int i, n, y, errret;
2N/A
2N/A /*
2N/A * Input stream should be unbuffered so that m_tfgetc() works
2N/A * correctly on BSD and SUN systems.
2N/A */
2N/A (void) setvbuf(in_fp, (char *) 0, _IONBF, BUFSIZ);
2N/A#if 0
2N/A/*
2N/A * Not sure whether we really want to concern ourselves with the output
2N/A * buffer scheme. Might be best to leave it upto the application to
2N/A * deal with buffer schemes and when to perform flushes.
2N/A *
2N/A * MKS Vi uses MKS Curses and so must support the ability to switch in
2N/A * and out of Curses mode when switching from Vi to Ex and back.
2N/A * Problem is that in Vi mode you would prefer full buffered output to
2N/A * give updates a smoother appearance and Ex mode you require line
2N/A * buffered in order to see prompts and messages.
2N/A */
2N/A (void) setvbuf(out_fp, (char *) 0, _IOLBF, BUFSIZ);
2N/A#endif
2N/A errno = 0;
2N/A
2N/A if (__m_setupterm(term, fileno(in_fp), fileno(out_fp), &errret)
2N/A == ERR) {
2N/A switch (errret) {
2N/A case -1:
2N/A errno = ENOMEM;
2N/A break;
2N/A case 2:
2N/A errno = ENAMETOOLONG;
2N/A break;
2N/A case 0:
2N/A default:
2N/A errno = ENOENT;
2N/A break;
2N/A }
2N/A goto error1;
2N/A }
2N/A
2N/A if (__m_doupdate_init())
2N/A goto error1;
2N/A
2N/A if ((sp = (SCREEN *) calloc(1, sizeof (*sp))) == NULL)
2N/A goto error1;
2N/A
2N/A sp->_kfd = -1;
2N/A sp->_if = in_fp;
2N/A sp->_of = out_fp;
2N/A sp->_term = cur_term;
2N/A
2N/A sp->_unget._size = __m_decode_init((t_decode **) &sp->_decode);
2N/A
2N/A /*
2N/A * Maximum length of a multbyte key sequence, including
2N/A * multibyte characters and terminal function keys.
2N/A */
2N/A if (sp->_unget._size < (M_TYPEAHEAD_SIZE + MB_LEN_MAX))
2N/A sp->_unget._size = M_TYPEAHEAD_SIZE + MB_LEN_MAX;
2N/A
2N/A sp->_unget._stack = calloc((size_t) sp->_unget._size,
2N/A sizeof (*sp->_unget._stack));
2N/A if (sp->_unget._stack == NULL)
2N/A goto error2;
2N/A
2N/A if ((wio = (t_wide_io *) calloc(1, sizeof (*wio))) == NULL)
2N/A goto error2;
2N/A
2N/A /* Setup wide input for XCurses. */
2N/A wio->get = (int (*)(void *)) wgetch;
2N/A wio->unget = __xc_ungetc;
2N/A wio->reset = __xc_clearerr;
2N/A wio->iserror = __xc_ferror;
2N/A wio->iseof = __xc_feof;
2N/A sp->_in = wio;
2N/A
2N/A if (assume_one_line) {
2N/A /* Assume only one line. */
2N/A lines = 1;
2N/A
2N/A /* Disable capabilities that assume more than one line. */
2N/A clear_screen = clr_eos = cursor_up = cursor_down = NULL;
2N/A cursor_home = cursor_to_ll = cursor_address = NULL;
2N/A row_address = parm_up_cursor = parm_down_cursor = NULL;
2N/A
2N/A /* Re-evaluate the cursor motion costs. */
2N/A __m_mvcur_cost();
2N/A
2N/A /* Reset flag for subsequent calls to newterm(). */
2N/A assume_one_line = FALSE;
2N/A }
2N/A
2N/A if ((sp->_curscr = newwin(lines, columns, 0, 0)) == NULL)
2N/A goto error2;
2N/A
2N/A if ((sp->_newscr = newwin(lines, columns, 0, 0)) == NULL)
2N/A goto error2;
2N/A
2N/A#if defined(_LP64)
2N/A sp->_hash = (unsigned int *) calloc(lines, sizeof (*sp->_hash));
2N/A#else
2N/A sp->_hash = (unsigned long *) calloc(lines, sizeof (*sp->_hash));
2N/A#endif
2N/A if (sp->_hash == NULL)
2N/A goto error2;
2N/A
2N/A if (0 <= __m_slk_format && __m_slk_init(sp, __m_slk_format) == ERR) {
2N/A goto error2;
2N/A }
2N/A
2N/A /*
2N/A * doupdate() will perform the final screen preparations like
2N/A * enter_ca_mode, reset_prog_mode() (to assert the termios
2N/A * changes), etc.
2N/A */
2N/A sp->_flags |= S_ENDWIN;
2N/A
2N/A#ifdef SIGTSTP
2N/A (void) signal(SIGTSTP, tstp);
2N/A#endif
2N/A /* Assert that __m_screen is set to the new terminal. */
2N/A osp = set_term(sp);
2N/A
2N/A /* Disable echo in tty driver, Curses does software echo. */
2N/A PTERMIOS(_prog)->c_lflag &= ~ECHO;
2N/A
2N/A /* Enable mappnig of cr -> nl on input and nl -> crlf on output. */
2N/A PTERMIOS(_prog)->c_iflag |= ICRNL;
2N/A PTERMIOS(_prog)->c_oflag |= OPOST;
2N/A#ifdef ONLCR
2N/A PTERMIOS(_prog)->c_oflag |= ONLCR;
2N/A#endif
2N/A cur_term->_flags |= __TERM_NL_IS_CRLF;
2N/A
2N/A#ifdef TAB0
2N/A /* Use real tabs. */
2N/A PTERMIOS(_prog)->c_oflag &= ~(TAB1|TAB2|TAB3);
2N/A#endif
2N/A
2N/A /*
2N/A * Default to 'cbreak' mode as per
2N/A * test /tset/CAPIxcurses/fcbreak/fcbreak1{4}
2N/A */
2N/A cur_term->_flags &= ~__TERM_HALF_DELAY;
2N/A
2N/A /*
2N/A * Default to 'idcok' mode as per
2N/A * test /tset/CAPIxcurses/fidcok/fidcok1{3}
2N/A */
2N/A __m_screen->_flags |= S_INS_DEL_CHAR;
2N/A
2N/A PTERMIOS(_prog)->c_cc[VMIN] = 1;
2N/A PTERMIOS(_prog)->c_cc[VTIME] = 0;
2N/A PTERMIOS(_prog)->c_lflag &= ~ICANON;
2N/A
2N/A (void) __m_tty_set_prog_mode();
2N/A (void) __m_set_echo(1);
2N/A (void) typeahead(fileno(in_fp));
2N/A
2N/A (void) __m_slk_clear(1);
2N/A
2N/A n = rip.top - rip.bottom;
2N/A if (stdscr == NULL) {
2N/A stdscr = newwin(lines - n, 0, rip.top, 0);
2N/A if (stdscr == NULL)
2N/A goto error3;
2N/A }
2N/A /*
2N/A * Create and initialise ripped off line windows.
2N/A * It is the application's responsiblity to free the
2N/A * windows when the application terminates.
2N/A */
2N/A for (i = 0; i < n; ++i) {
2N/A if (rip.line[i].created)
2N/A continue;
2N/A y = rip.line[i].dy;
2N/A if (y < 0)
2N/A y += lines;
2N/A
2N/A w = newwin(1, 0, y, 0);
2N/A if (rip.line[i].init != (int (*)(WINDOW *, int)) 0)
2N/A (void) (*rip.line[i].init)(w, columns);
2N/A rip.line[i].created = 1;
2N/A }
2N/A LINES = stdscr->_maxy = sp->_curscr->_maxy - n;
2N/A
2N/A return (sp);
2N/Aerror3:
2N/A (void) set_term(osp);
2N/Aerror2:
2N/A delscreen(sp);
2N/Aerror1:
2N/A return (NULL);
2N/A}
2N/A
2N/A/*
2N/A * Free storage associated with a screen structure.
2N/A * NOTE endwin() does not do this.
2N/A */
2N/Avoid
2N/Adelscreen(SCREEN *sp)
2N/A{
2N/A if (sp != NULL) {
2N/A if (sp->_slk._w != NULL)
2N/A (void) delwin(sp->_slk._w);
2N/A
2N/A (void) delwin(sp->_newscr);
2N/A (void) delwin(sp->_curscr);
2N/A (void) del_curterm(sp->_term);
2N/A
2N/A __m_decode_free((t_decode **) &sp->_decode);
2N/A
2N/A if (sp->_hash != NULL)
2N/A free(sp->_hash);
2N/A
2N/A if (sp->_unget._stack != NULL)
2N/A free(sp->_unget._stack);
2N/A
2N/A if (sp->_in != NULL)
2N/A free(sp->_in);
2N/A
2N/A free(sp);
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * Switch current terminal for Curses layer.
2N/A */
2N/ASCREEN *
2N/Aset_term(SCREEN *screen)
2N/A{
2N/A SCREEN *osp = __m_screen;
2N/A
2N/A if (screen != NULL) {
2N/A (void) set_curterm(screen->_term);
2N/A curscr = screen->_curscr;
2N/A __m_screen = screen;
2N/A
2N/A LINES = lines;
2N/A COLS = columns;
2N/A COLORS = max_colors;
2N/A COLOR_PAIRS = max_pairs;
2N/A }
2N/A
2N/A return (osp);
2N/A}
2N/A
2N/Aint
2N/Atypeahead(int fd)
2N/A{
2N/A __m_screen->_flags &= ~S_ISATTY;
2N/A if (fd != -1) {
2N/A if (isatty(fd)) {
2N/A __m_screen->_kfd = fd;
2N/A __m_screen->_flags |= S_ISATTY;
2N/A } else {
2N/A __m_screen->_kfd = -1;
2N/A }
2N/A }
2N/A
2N/A return (OK);
2N/A}
2N/A
2N/Aint
2N/A__m_set_echo(int bf)
2N/A{
2N/A int old;
2N/A
2N/A old = (__m_screen->_flags & S_ECHO) == S_ECHO;
2N/A
2N/A __m_screen->_flags &= ~S_ECHO;
2N/A if (bf)
2N/A __m_screen->_flags |= S_ECHO;
2N/A
2N/A return (old);
2N/A}