10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*9354STim.Marsland@Sun.COM * Common Development and Distribution License (the "License").
6*9354STim.Marsland@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21*9354STim.Marsland@Sun.COM
220Sstevel@tonic-gate /*
23*9354STim.Marsland@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate * The Regents of the University of California
330Sstevel@tonic-gate * All Rights Reserved
340Sstevel@tonic-gate *
350Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
360Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate * contributors.
380Sstevel@tonic-gate */
390Sstevel@tonic-gate
400Sstevel@tonic-gate /*LINTLIBRARY*/
410Sstevel@tonic-gate
420Sstevel@tonic-gate #include <stdio.h>
430Sstevel@tonic-gate #include <stdlib.h>
440Sstevel@tonic-gate #include <string.h>
450Sstevel@tonic-gate #include <sys/types.h>
460Sstevel@tonic-gate #include "curses_inc.h"
470Sstevel@tonic-gate
480Sstevel@tonic-gate #ifdef _VR2_COMPAT_CODE
490Sstevel@tonic-gate extern char _endwin;
500Sstevel@tonic-gate #endif /* _VR2_COMPAT_CODE */
510Sstevel@tonic-gate
520Sstevel@tonic-gate /* 1200 is put at the 0th location since 0 is probably a mistake. */
530Sstevel@tonic-gate static long baud_convert[] = {
540Sstevel@tonic-gate 1200, 50, 75, 110, 135, 150, 200, 300, 600, 1200,
550Sstevel@tonic-gate 1800, 2400, 4800, 9600, 19200, 38400, 57600, 76800,
56*9354STim.Marsland@Sun.COM 115200, 153600, 230400, 307200, 460800, 921600
570Sstevel@tonic-gate };
580Sstevel@tonic-gate
590Sstevel@tonic-gate static char isfilter = 0;
600Sstevel@tonic-gate static int _chk_trm(void);
610Sstevel@tonic-gate static void _forget(void);
620Sstevel@tonic-gate
630Sstevel@tonic-gate /*
640Sstevel@tonic-gate * newscreen sets up a terminal and returns a pointer to the terminal
650Sstevel@tonic-gate * structure or NULL in case of an error. The parameters are:
660Sstevel@tonic-gate * type: terminal type
670Sstevel@tonic-gate * lsize, csize, tabsize: physical sizes
680Sstevel@tonic-gate * infptr, outfptr: input and output stdio stream file pointers
690Sstevel@tonic-gate */
700Sstevel@tonic-gate
710Sstevel@tonic-gate SCREEN *
newscreen(char * type,int lsize,int csize,int tabsize,FILE * outfptr,FILE * infptr)720Sstevel@tonic-gate newscreen(char *type, int lsize, int csize, int tabsize,
730Sstevel@tonic-gate FILE *outfptr, FILE *infptr)
740Sstevel@tonic-gate {
750Sstevel@tonic-gate int old_lines = LINES, old_cols = COLS, retcode;
760Sstevel@tonic-gate #ifndef _IOFBF
770Sstevel@tonic-gate char *sobuf;
780Sstevel@tonic-gate #endif /* _IOBUF */
790Sstevel@tonic-gate WINDOW *old_curscr = curscr;
800Sstevel@tonic-gate SCREEN *old = SP;
810Sstevel@tonic-gate TERMINAL *old_term = cur_term;
820Sstevel@tonic-gate
830Sstevel@tonic-gate #ifdef DEBUG
840Sstevel@tonic-gate if (outf == NULL) {
850Sstevel@tonic-gate outf = fopen("trace", "w");
860Sstevel@tonic-gate if (outf == NULL) {
870Sstevel@tonic-gate perror("trace");
880Sstevel@tonic-gate exit(-1);
890Sstevel@tonic-gate }
900Sstevel@tonic-gate setbuf(outf, (char *)NULL);
910Sstevel@tonic-gate }
920Sstevel@tonic-gate
930Sstevel@tonic-gate if (outf)
940Sstevel@tonic-gate fprintf(outf, "NEWTERM(type=%s, outfptr=%x %d, infptr=%x %d) "
950Sstevel@tonic-gate "isatty(2) %d, getenv %s\n", type, outfptr,
960Sstevel@tonic-gate fileno(outfptr), infptr, fileno(infptr), isatty(2),
970Sstevel@tonic-gate getenv("TERM"));
980Sstevel@tonic-gate #endif /* DEBUG */
990Sstevel@tonic-gate
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate /* read in terminfo file */
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate if (setupterm(type, fileno(outfptr), &retcode) != 0)
1040Sstevel@tonic-gate goto err2;
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate /* the max length of a multi-byte character */
1070Sstevel@tonic-gate _csmax = (cswidth[0] > cswidth[1]+1 ?
1080Sstevel@tonic-gate (cswidth[0] > cswidth[2]+1 ? cswidth[0] : cswidth[2]+1) :
1090Sstevel@tonic-gate (cswidth[1] > cswidth[2] ? cswidth[1]+1 : cswidth[2]+1));
1100Sstevel@tonic-gate if (_csmax > CSMAX)
1110Sstevel@tonic-gate goto err2;
1120Sstevel@tonic-gate /* the max length of a multi-column character */
1130Sstevel@tonic-gate _scrmax = _curs_scrwidth[0] > _curs_scrwidth[1] ?
1140Sstevel@tonic-gate (_curs_scrwidth[0] > _curs_scrwidth[2] ? _curs_scrwidth[0] :
1150Sstevel@tonic-gate _curs_scrwidth[2]) : (_curs_scrwidth[1] > _curs_scrwidth[2] ?
1160Sstevel@tonic-gate _curs_scrwidth[1] : _curs_scrwidth[2]);
1170Sstevel@tonic-gate /* true multi-byte/multi-column case */
1180Sstevel@tonic-gate _mbtrue = (_csmax > 1 || _scrmax > 1);
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate if ((curs_errno = _chk_trm()) != -1) {
1210Sstevel@tonic-gate (void) strcpy(curs_parm_err, cur_term->_termname);
1220Sstevel@tonic-gate goto err2;
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate /* use calloc because almost everything needs to be zero */
1260Sstevel@tonic-gate if ((SP = (SCREEN *) calloc(1, sizeof (SCREEN))) == NULL)
1270Sstevel@tonic-gate goto err1;
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate SP->term_file = outfptr;
1300Sstevel@tonic-gate SP->input_file = infptr;
1310Sstevel@tonic-gate
1320Sstevel@tonic-gate /*
1330Sstevel@tonic-gate * The default is echo, for upward compatibility, but we do
1340Sstevel@tonic-gate * all echoing in curses to avoid problems with the tty driver
1350Sstevel@tonic-gate * echoing things during critical sections.
1360Sstevel@tonic-gate */
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate SP->fl_echoit = 1;
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate /* set some fields for cur_term structure */
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate (void) typeahead(fileno(infptr));
1430Sstevel@tonic-gate (void) tinputfd(fileno(infptr));
1440Sstevel@tonic-gate
1450Sstevel@tonic-gate /*
1460Sstevel@tonic-gate * We use LINES instead of the SP variable and a local variable because
1470Sstevel@tonic-gate * slk_init and rip_init update the LINES value and application code
1480Sstevel@tonic-gate * may look at the value of LINES in the function called by rip_init.
1490Sstevel@tonic-gate */
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate /* LINTED */
1520Sstevel@tonic-gate LINES = SP->lsize = lsize > 0 ? lsize : lines;
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate /* force the output to be buffered */
1550Sstevel@tonic-gate #ifdef _IOFBF
1560Sstevel@tonic-gate (void) setvbuf(outfptr, (char *)NULL, _IOFBF, 0);
1570Sstevel@tonic-gate #else /* _IOFBF */
1580Sstevel@tonic-gate if ((sobuf = malloc(BUFSIZ)) == NULL) {
1590Sstevel@tonic-gate curs_errno = CURS_BAD_MALLOC;
1600Sstevel@tonic-gate #ifdef DEBUG
1610Sstevel@tonic-gate strcpy(curs_parm_err, "newscreen");
1620Sstevel@tonic-gate #endif /* DEBUG */
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate setbuf(outfptr, sobuf);
1650Sstevel@tonic-gate #endif /* _IOFBF */
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate #ifdef SYSV
1680Sstevel@tonic-gate SP->baud = baud_convert[_BRS(PROGTTYS)];
1690Sstevel@tonic-gate #else /* SYSV */
1700Sstevel@tonic-gate SP->baud = baud_convert[_BR(PROGTTY)];
1710Sstevel@tonic-gate #endif /* SYSV */
1720Sstevel@tonic-gate
1730Sstevel@tonic-gate /* figure out how much each terminal capability costs */
1740Sstevel@tonic-gate _init_costs();
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate /* initialize the array of alternate characters */
1770Sstevel@tonic-gate (void) init_acs();
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate SP->tcap = cur_term;
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate /* set tty settings to something reasonable for us */
1820Sstevel@tonic-gate #ifdef SYSV
1830Sstevel@tonic-gate PROGTTYS.c_lflag &= ~ECHO;
1840Sstevel@tonic-gate PROGTTYS.c_lflag |= ISIG;
1850Sstevel@tonic-gate PROGTTYS.c_oflag &= ~(OCRNL|ONLCR); /* why would anyone set OCRNL? */
1860Sstevel@tonic-gate #else /* SYSV */
1870Sstevel@tonic-gate PROGTTY.sg_flags &= ~(RAW|ECHO|CRMOD);
1880Sstevel@tonic-gate #endif /* SYSV */
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate (void) cbreak();
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate /* LINTED */
1930Sstevel@tonic-gate COLS = SP->csize = csize > 0 ? csize : columns;
1940Sstevel@tonic-gate if (tabsize == 0)
1950Sstevel@tonic-gate tabsize = (init_tabs == -1) ? 8 : init_tabs;
1960Sstevel@tonic-gate /* LINTED */
1970Sstevel@tonic-gate SP->tsize = (short)tabsize;
1980Sstevel@tonic-gate #ifdef DEBUG
1990Sstevel@tonic-gate if (outf)
2000Sstevel@tonic-gate fprintf(outf, "LINES = %d, COLS = %d\n", LINES, COLS);
2010Sstevel@tonic-gate #endif /* DEBUG */
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate if ((curscr = SP->cur_scr = newwin(LINES, COLS, 0, 0)) == NULL)
2040Sstevel@tonic-gate goto err;
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate SP->fl_endwin = 2;
2070Sstevel@tonic-gate #ifdef _VR2_COMPAT_CODE
2080Sstevel@tonic-gate _endwin = FALSE;
2090Sstevel@tonic-gate #endif /* _VR2_COMPAT_CODE */
2100Sstevel@tonic-gate curscr->_sync = TRUE;
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate /*
2130Sstevel@tonic-gate * This will tell _quick_echo(if it's ever called), whether
2140Sstevel@tonic-gate * _quick_echo should let wrefresh handle everything.
2150Sstevel@tonic-gate */
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate if (ceol_standout_glitch || (magic_cookie_glitch >= 0) ||
2180Sstevel@tonic-gate tilde_glitch || (transparent_underline && erase_overstrike)) {
2190Sstevel@tonic-gate curscr->_flags |= _CANT_BE_IMMED;
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate if (!(SP->virt_scr = newwin(LINES, COLS, 0, 0)))
2220Sstevel@tonic-gate goto err;
2230Sstevel@tonic-gate _virtscr = SP->virt_scr;
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate SP->virt_scr->_clear = FALSE;
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate /* video mark map for cookie terminals */
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate if (ceol_standout_glitch || (magic_cookie_glitch >= 0)) {
2300Sstevel@tonic-gate int i, nc;
2310Sstevel@tonic-gate char **marks;
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate if ((marks = (char **)calloc((unsigned)LINES,
2340Sstevel@tonic-gate sizeof (char *))) == NULL)
2350Sstevel@tonic-gate goto err;
2360Sstevel@tonic-gate SP->_mks = marks;
2370Sstevel@tonic-gate nc = (COLS / BITSPERBYTE) + (COLS % BITSPERBYTE ? 1 : 0);
2380Sstevel@tonic-gate if ((*marks = (char *)calloc((unsigned)nc * LINES,
2390Sstevel@tonic-gate sizeof (char))) == NULL)
2400Sstevel@tonic-gate goto err;
2410Sstevel@tonic-gate for (i = LINES - 1; i-- > 0; ++marks)
2420Sstevel@tonic-gate *(marks + 1) = *marks + nc;
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate /* hash tables for lines */
2460Sstevel@tonic-gate if ((SP->cur_hash = (int *)calloc((unsigned)2 * LINES,
2470Sstevel@tonic-gate sizeof (int))) == NULL)
2480Sstevel@tonic-gate goto err;
2490Sstevel@tonic-gate SP->virt_hash = SP->cur_hash + LINES;
2500Sstevel@tonic-gate
2510Sstevel@tonic-gate /* adjust the screen size if soft labels and/or ripoffline are used */
2520Sstevel@tonic-gate if (_slk_init)
2530Sstevel@tonic-gate (*_slk_init)();
2540Sstevel@tonic-gate if (_rip_init)
2550Sstevel@tonic-gate (*_rip_init)();
2560Sstevel@tonic-gate
2570Sstevel@tonic-gate if ((SP->std_scr = newwin(LINES, COLS, 0, 0)) == NULL) {
2580Sstevel@tonic-gate /* free all the storage allocated above and return NULL */
2590Sstevel@tonic-gate err:
2600Sstevel@tonic-gate delscreen(SP);
2610Sstevel@tonic-gate COLS = old_cols;
2620Sstevel@tonic-gate curscr = old_curscr;
2630Sstevel@tonic-gate LINES = old_lines;
2640Sstevel@tonic-gate err1:
2650Sstevel@tonic-gate SP = old;
2660Sstevel@tonic-gate
2670Sstevel@tonic-gate curs_errno = CURS_BAD_MALLOC;
2680Sstevel@tonic-gate #ifdef DEBUG
2690Sstevel@tonic-gate strcpy(curs_parm_err, "newscreen");
2700Sstevel@tonic-gate #endif /* DEBUG */
2710Sstevel@tonic-gate
2720Sstevel@tonic-gate err2:
2730Sstevel@tonic-gate cur_term = old_term;
2740Sstevel@tonic-gate return (NULL);
2750Sstevel@tonic-gate }
2760Sstevel@tonic-gate #ifdef DEBUG
2770Sstevel@tonic-gate if (outf)
2780Sstevel@tonic-gate fprintf(outf, "SP %x, stdscr %x, curscr %x\n",
2790Sstevel@tonic-gate SP, SP->std_scr, curscr);
2800Sstevel@tonic-gate #endif /* DEBUG */
2810Sstevel@tonic-gate
2820Sstevel@tonic-gate if (((SP->imode = (enter_insert_mode && exit_insert_mode)) != 0) &&
2830Sstevel@tonic-gate ((SP->dmode = (enter_delete_mode && exit_delete_mode)) != 0)) {
2840Sstevel@tonic-gate if (strcmp(enter_insert_mode, enter_delete_mode) == 0)
2850Sstevel@tonic-gate SP->sid_equal = TRUE;
2860Sstevel@tonic-gate if (strcmp(exit_insert_mode, exit_delete_mode) == 0)
2870Sstevel@tonic-gate SP->eid_equal = TRUE;
2880Sstevel@tonic-gate }
2890Sstevel@tonic-gate SP->ichok = (SP->imode || insert_character || parm_ich);
2900Sstevel@tonic-gate SP->dchok = (delete_character || parm_dch);
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate stdscr = SP->std_scr;
2930Sstevel@tonic-gate TABSIZE = SP->tsize;
2940Sstevel@tonic-gate
2950Sstevel@tonic-gate return (SP);
2960Sstevel@tonic-gate }
2970Sstevel@tonic-gate
2980Sstevel@tonic-gate /*
2990Sstevel@tonic-gate * check if terminal have capabilities to do basic cursor movements and
3000Sstevel@tonic-gate * screen clearing
3010Sstevel@tonic-gate */
3020Sstevel@tonic-gate static int
_chk_trm(void)3030Sstevel@tonic-gate _chk_trm(void)
3040Sstevel@tonic-gate {
3050Sstevel@tonic-gate short error_num = -1;
3060Sstevel@tonic-gate #ifdef DEBUG
3070Sstevel@tonic-gate if (outf)
3080Sstevel@tonic-gate fprintf(outf, "chk_trm().\n");
3090Sstevel@tonic-gate #endif /* DEBUG */
3100Sstevel@tonic-gate
3110Sstevel@tonic-gate if (generic_type)
3120Sstevel@tonic-gate error_num = CURS_UNKNOWN;
3130Sstevel@tonic-gate else {
3140Sstevel@tonic-gate if (isfilter) {
3150Sstevel@tonic-gate _forget();
3160Sstevel@tonic-gate /* Only need to move left or right on current line */
3170Sstevel@tonic-gate if (!(cursor_left || carriage_return ||
3180Sstevel@tonic-gate column_address || parm_left_cursor)) {
3190Sstevel@tonic-gate goto out_stupid;
3200Sstevel@tonic-gate }
3210Sstevel@tonic-gate } else {
3220Sstevel@tonic-gate if ((hard_copy || over_strike) ||
3230Sstevel@tonic-gate /* some way to move up, down, left */
3240Sstevel@tonic-gate (!(cursor_address) &&
3250Sstevel@tonic-gate (!((cursor_up || cursor_home) && cursor_down &&
3260Sstevel@tonic-gate (cursor_left || carriage_return)))) ||
3270Sstevel@tonic-gate (!clear_screen)) {
3280Sstevel@tonic-gate out_stupid:
3290Sstevel@tonic-gate error_num = CURS_STUPID;
3300Sstevel@tonic-gate }
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate }
3330Sstevel@tonic-gate return (error_num);
3340Sstevel@tonic-gate }
3350Sstevel@tonic-gate
3360Sstevel@tonic-gate int
filter(void)3370Sstevel@tonic-gate filter(void)
3380Sstevel@tonic-gate {
3390Sstevel@tonic-gate isfilter = 1;
3400Sstevel@tonic-gate return (OK);
3410Sstevel@tonic-gate }
3420Sstevel@tonic-gate
3430Sstevel@tonic-gate /*
3440Sstevel@tonic-gate * if (for some reason) user assumes that terminal has only one line,
3450Sstevel@tonic-gate * disable all capabilities that deal with non-horizontal cursor movement
3460Sstevel@tonic-gate */
3470Sstevel@tonic-gate static void
_forget(void)3480Sstevel@tonic-gate _forget(void)
3490Sstevel@tonic-gate {
3500Sstevel@tonic-gate row_address = cursor_address = clear_screen = parm_down_cursor =
3510Sstevel@tonic-gate cursor_up = cursor_down = NULL;
3520Sstevel@tonic-gate cursor_home = carriage_return;
3530Sstevel@tonic-gate lines = 1;
3540Sstevel@tonic-gate }
355