/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/* Copyright (c) 1981 Regents of the University of California */
#pragma ident "%Z%%M% %I% %E% SMI"
#include "ex.h"
#include "ex_tty.h"
#include "ex_vis.h"
#ifndef PRESUNEUC
#include <wctype.h>
#ifdef putchar
#endif
#ifdef getchar
#endif
#endif /* PRESUNEUC */
/*
* Terminal driving and line formatting routines.
* Basic motion optimizations are done here as well
* as formatting of lines (printing of control characters,
* line numbering and the like).
*/
/*
* The routines outchar, putchar and pline are actually
* variables, and these variables point at the current definitions
* of the routines. See the routine setflav.
* We sometimes make outchar be routines which catch the characters
* to be printed, e.g. if we want to see how long a line is.
* routines in the file ex_vput.c (vputchar, vinschar, etc.).
*/
* structure */
int (*
setlist(t))()
bool t;
{
int (*P)();
listf = t;
P = Putchar;
return (P);
}
int (*
setnumb(t))()
bool t;
{
int (*P)();
numberf = t;
P = Pline;
return (P);
}
/*
* Format c for list mode; leave things in common
* with normal print mode to be done by normchar.
*/
int
{
switch (c) {
case '\t':
case '\b':
outchar('^');
c = ctlof(c);
break;
case '\n':
break;
case (int)('\n' | QUOTE):
outchar('$');
break;
default:
if((int)(c & QUOTE))
break;
}
(void) normchar(c);
return (0);
}
/*
* Format c for printing. Handle funnies of upper case terminals
* and hazeltines which don't have ~.
*/
int
{
char *colp;
if (c == '~' && tilde_glitch) {
(void) normchar('\\');
c = '^';
}
if ((int)(c & QUOTE))
switch (c) {
case (int)(' ' | QUOTE):
case (int)('\b' | QUOTE):
break;
case (int)QUOTE:
return (0);
default:
c &= (int)TRIM;
}
outchar('\\');
return (0);
} else if (UPPERCASE)
if (isupper(c)) {
outchar('\\');
c = tolower(c);
} else {
colp = "({)}!|^~'`";
while (*colp++)
if (c == *colp++) {
outchar('\\');
c = colp[-2];
break;
}
}
outchar(c);
return (0);
}
/*
* Print a line with a number.
*/
int
numbline(int i)
{
if (shudclob)
slobber(' ');
viprintf("%6d ", i);
(void) normline();
return (0);
}
/*
* Normal line output, no numbering.
*/
int
normline(void)
{
unsigned char *cp;
int n;
if (shudclob)
/* pdp-11 doprnt is not reentrant so can't use "printf" here
in case we are tracing */
putoctal = 1;
putoctal = 0;
} else {
cp += n;
}
if (!inopen)
return (0);
}
/*
* Given c at the beginning of a line, determine whether
* the printing of the line will erase or otherwise obliterate
* the prompt which was printed before. If it won't, do it now.
*/
void
slobber(int c)
{
shudclob = 0;
switch (c) {
case '\t':
return;
break;
default:
return;
case ' ':
case 0:
break;
}
if (over_strike)
return;
flush();
(void) putch(' ');
}
/*
* The output buffer is initialized with a useful error
* message so we don't have to keep it in data space.
*/
/*
* Phadnl records when we have already had a complete line ending with \n.
* If another line starts without a flush, and the terminal suggests it,
* we switch into -nl mode so that we can send linefeeds to avoid
* a lot of spacing.
*/
static bool phadnl;
/*
* Indirect to current definition of putchar.
*/
int
putchar(int c)
{
}
/*
* Termchar routine for command mode.
* Watch for possible switching to -nl mode.
* Otherwise flush into next level of buffering when
* small buffer fills or at a newline.
*/
int
{
pstart();
if (c == '\n')
phadnl = 1;
flush1();
*linp++ = c;
fgoto();
flush1();
}
return (0);
}
void
flush(void)
{
flush1();
flush2();
}
/*
* Flush from small line buffer into output buffer.
* Work here is destroying motion into positions, and then
* letting fgoto do the optimized motion.
*/
void
flush1(void)
{
wchar_t c;
#ifdef PRESUNEUC
/* used for multibyte characters split between lines */
int splitcnt = 0;
#else
/* used for multicolumn character substitution and padding */
int fillercnt = 0;
#endif /* PRESUNEUC */
*linp = 0;
while (*lp)
switch (c = *lp++) {
case '\r':
destcol = 0;
continue;
case '\b':
if (destcol)
destcol--;
continue;
case ' ':
destcol++;
continue;
case '\t':
continue;
case '\n':
destline--;
destcol = 0;
continue;
default:
fgoto();
for (;;) {
unsigned char *p;
c &= TRIM;
length = 0;
fgoto();
#ifdef PRESUNEUC
/* represent split chars by '>' */
c = '>';
#else
/* substitute/wrap multicolumn char */
if(mc_wrap) {
while(fillercnt) {
outcol++;
destcol++;
fillercnt--;
}
} else {
c = mc_filler;
}
#endif /* PRESUNEUC */
continue;
}
p = multic;
while(length2--)
(void) putch(*p++);
if (c == '\b') {
outcol--;
destcol--;
} else if (c >= ' ' && c != DELETE) {
(void) putch('\r'),
(void) putch('\n');
}
#ifdef PRESUNEUC
if(splitcnt) {
splitcnt--;
c = '>';
} else
c = *lp++;
#else
if(fillercnt) {
fillercnt--;
c = mc_filler;
if(c == ' ')
continue;
} else
c = *lp++;
#endif /* PRESUNEUC */
if (c <= ' ')
break;
}
--lp;
continue;
}
}
void
flush2(void)
{
fgoto();
flusho();
pstop();
}
/*
* Sync the position of the output cursor.
* Most work here is rounding for terminal boundaries getting the
* column position implied by wraparound or the lack thereof and
* rolling up the screen to get destline on the screen.
*/
void
fgoto(void)
{
int l, c;
}
outline += l;
if (auto_right_margin == 0) {
while (l > 0) {
if (pfast)
l--;
}
outcol = 0;
}
}
}
l = destline;
c = destcol;
destcol = 0;
fgoto();
destcol = c;
}
while (l > lines - 1) {
/*
* The following linefeed (or simulation thereof)
* is supposed to scroll up the screen, since we
* are on the bottom line.
*
* Superbee glitch: in the middle of the screen we
* have to use esc B (down) because linefeed messes up
* in "Efficient Paging" mode (which is essential in
* some SB's because CRLF mode puts garbage
* in at end of memory), but you must use linefeed to
* scroll since down arrow won't go past memory end.
* I turned this off after receiving Paul Eggert's
* Superbee description which wins better.
*/
else
(void) putch('\n');
l--;
if (pfast == 0)
outcol = 0;
}
}
if (cursor_address && !holdcm)
plod(0);
else
else
plod(0);
}
/*
* Tab to column col by flushing and then setting destcol.
* Used by "set all".
*/
void
{
flush1();
}
/*
* Move (slowly) to destination.
* Hard thing here is using home cursor on really deficient terminals.
* Otherwise just use cursor motions, hacking use of tabs and overtabbing
* and backspace.
*/
int
#ifdef __STDC__
plodput(char c)
#else
plodput(c)
char c;
#endif
{
if (plodflg)
plodcnt--;
else
(void) putch(c);
return (0);
}
int
{
int i, j, k;
/*
* directly with local motions to the right spot.
*/
if (cursor_home) {
/*
* get to the proper column. This assumes cursor_right costs
* 1 char. So i+destcol is cost of motion with home.
*/
else
i = destcol;
/*
* j is cost to move locally without homing
*/
if (value(vi_HARDTABS)) {
if (tab && j)
else
} else
} else
/* leftward motion only works if we can backspace. */
else
j = i + 1; /* impossibly expensive */
/* k is the absolute value of vertical distance */
if (k < 0)
k = -k;
j += k;
/*
* Decision. We may not have a choice if no cursor_up.
*/
/*
* Cheaper to home. Do it now and pretend it's a
* regular local motion.
*/
} else if (cursor_to_ll) {
/*
* Quickly consider homing down and moving from there.
* Assume cost of cursor_to_ll is 2.
*/
if (i + k + 2 < j && (k<=0 || cursor_up)) {
outcol = 0;
}
}
} else
/*
* No home and no up means it's impossible, so we return an
* incredibly big number to make cursor motion win out.
*/
return (500);
else
i = destcol;
/*
if (back_tab && outcol > destcol && (j = (((outcol+7) & ~7) - destcol - 1) >> 3)) {
j *= (k = strlen(back_tab));
if ((k += (destcol&7)) > 4)
j += 8 - (destcol&7);
else
j += k;
} else
*/
/*
* If we will later need a \n which will turn into a \r\n by
* the system or the terminal, then don't bother to try to \r.
*/
goto dontcr;
/*
* If the terminal will do a \r\n and there isn't room for it,
* then we can't afford a \r.
*/
goto dontcr;
/*
* If it will be cheaper, or if we can't back up, then send
* a return preliminarily.
*/
/*
* BUG: this doesn't take the (possibly long) length
* of carriage_return into account.
*/
if (carriage_return) {
outcol = 0;
} else if (newline) {
outline++;
outcol = 0;
}
}
/* Move down, if necessary, until we are at the desired line */
if (j > costDP && parm_down_cursor) {
/* Win big on Tek 4025 */
outline += j;
}
else {
outline++;
if (cursor_down && pfast)
else
(void) plodput('\n');
}
if (plodcnt < 0)
goto out;
outcol = 0;
}
if (back_tab)
/* Move left, if necessary, to desired column */
if (plodcnt < 0)
goto out;
outcol--;
if (value(vi_HARDTABS))
continue;
}
if (j > costLP && parm_left_cursor) {
outcol -= j;
}
else {
outcol--;
}
}
/* Move up, if necessary, to desired row */
if (parm_up_cursor && j > 1) {
/* Win big on Tek 4025 */
outline -= j;
}
else {
outline--;
}
if (plodcnt < 0)
goto out;
}
/*
* Now move to the right, if necessary. We first tab to
* as close as we can get.
*/
/* tab to right as far as possible without passing col */
for (;;) {
if (i > destcol)
break;
if (tab)
else
(void) plodput('\t');
outcol = i;
}
/* consider another tab and then some backspaces */
outcol = i;
/*
* Back up. Don't worry about parm_left_cursor because
* it's never more than 4 spaces anyway.
*/
outcol--;
}
}
}
/*
* We've tabbed as much as possible. If we still need to go
* further (not exact or can't tab) space over. This is a
* very common case when moving to the right with space.
*/
if (j > costRP && parm_right_cursor) {
/*
* This probably happens rarely, if at all.
* It seems mainly useful for ANSI terminals
* with no hardware tabs, and I don't know
* of any such terminal at the moment.
*/
outcol += j;
}
else {
/*
* move one char to the right. We don't use right
* because it's better to just print the char we are
* moving over. There are various exceptions, however.
* If !inopen, vtube contains garbage. If the char is
* a null or a tab we want to print a space. Other
* random chars we use space for instead, too.
*/
wchar = ' ';
wchar = ' ';
scrlength = 0;
/* assume multibyte terminals have cursor_right */
while(j--) {
outcol++;
}
} else {
outcol++;
}
}
if (plodcnt < 0)
goto out;
}
out:
if(plodflg) {
}
return(plodcnt);
}
/*
* An input line arrived.
* Calculate new (approximate) screen line position.
* Approximate because kill character echoes newline with
* no feedback and also because of long input lines.
*/
void
noteinp(void)
{
outline++;
}
/*
* Something weird just happened and we
* lost track of what's happening out there.
* Since we can't, in general, read where we are
* we just reset to some known state.
* On cursor addressable terminals setting to unknown
* will force a cursor address soon.
*/
void
termreset(void)
{
endim();
if (enter_ca_mode)
putpad((unsigned char *)enter_ca_mode);
destcol = 0;
if (cursor_address) {
} else {
}
}
/*
* Low level buffering, with the ability to drain
* buffered output without printing it.
*/
void
draino(void)
{
}
void
flusho(void)
{
#ifdef TRACE
if (trace)
#endif
}
}
void
putnl(void)
{
putchar('\n');
}
void
{
return;
while (*cp)
}
int
putch(char c)
{
#ifdef OLD3BTTY
if(c == '\n') /* Fake "\n\r" for '\n' til fix in 3B firmware */
#endif
*obp++ = c;
flusho();
return (0);
}
/*
* Miscellaneous routines related to output.
*/
/*
* Put with padding
*/
void
{
flush();
}
/*
* Set output through normal command mode routine.
*/
void
setoutt(void)
{
}
/*
* Printf (temporarily) in list mode.
*/
/*VARARGS2*/
void
{
int (*P)();
P = setlist(1);
#ifdef PRESUNEUC
#else
#endif /* PRESUNEUC */
Putchar = P;
}
/*
* Newline + flush.
*/
void
putNFL()
{
putnl();
flush();
}
/*
* Try to start -nl mode.
*/
void
pstart(void)
{
if (NONL)
return;
if (!value(vi_OPTIMIZE))
return;
return;
fgoto();
flusho();
pfast = 1;
normtty++;
saveterm();
sTTY(2);
}
/*
* Stop -nl mode.
*/
void
pstop(void)
{
if (inopen)
return;
phadnl = 0;
draino();
pfast &= ~1;
}
/*
* Prep tty for open mode.
*/
ostart()
{
ttymode f;
/*
if (!intty)
error("Open and visual must be used interactively");
*/
(void) gTTY(2);
normtty++;
f = tty;
ttcharoff();
sTTY(2);
tostart();
pfast |= 2;
saveterm();
return (f);
}
/* actions associated with putting the terminal in open mode */
void
tostart(void)
{
putpad((unsigned char *)cursor_visible);
putpad((unsigned char *)keypad_xmit);
if (ttynbuf[0] == 0) {
char *tn;
ttynbuf[0] = 1;
else
}
if (ttynbuf[0] != 1) {
}
}
}
/*
* We always turn off quit since datamedias send ^\ for their
* right arrow key.
*/
void
ttcharoff(void)
{
/*
* use 200 instead of 377 because 377 is y-umlaut
* in ISO 8859/1
*/
/* We will read ^z and suspend ourselves via kill */
}
/*
* Stop open, restoring tty modes.
*/
void
{
normal(f);
tostop();
}
/* Actions associated with putting the terminal in the right mode. */
void
tostop(void)
{
putpad((unsigned char *)cursor_normal);
putpad((unsigned char *)keypad_local);
}
#ifndef CBREAK
/*
* Into cooked mode for interruptibility.
*/
vcook()
{
sTTY(2);
}
/*
* Back into raw mode.
*/
vraw()
{
sTTY(2);
}
#endif
/*
* Restore flags to normal state f.
*/
void
{
if (normtty > 0) {
setty(f);
normtty--;
}
}
/*
* Straight set of flags to state f.
*/
setty(f)
ttymode f;
{
int isnorm = 0;
ttcharoff();
else
isnorm = 1;
tty = f;
sTTY(2);
if (!isnorm)
saveterm();
return (ot);
}
int
gTTY(int i)
{
if(termiosflag < 0) {
termiosflag = 1;
else {
termiosflag = 0;
return (-1);
for(i = 0; i < NCC; i++)
}
return (0);
}
if(termiosflag)
return (-1);
for(i = 0; i < NCC; i++)
return (0);
}
/*
* sTTY: set the tty modes on file descriptor i to be what's
* currently in global "tty". (Also use nttyc if needed.)
*/
void
sTTY(int i)
{
int j;
if(termiosflag)
else {
for(j = 0; j < NCC; j++)
}
}
/*
*/
void
noonl(void)
{
}