=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/mg/tty.c,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- src/usr.bin/mg/tty.c 2000/02/25 19:08:52 1.1 +++ src/usr.bin/mg/tty.c 2000/02/26 22:53:16 1.2 @@ -1,8 +1,8 @@ /* - * Termcap/terminfo display driver + * Terminfo display driver * - * Termcap is a terminal information database and routines to describe - * terminals on most UNIX systems. Many other systems have adopted + * Terminfo is a terminal information database and routines to describe + * terminals on most modern UNIX systems. Many other systems have adopted * this as a reasonable way to allow for widly varying and ever changing * varieties of terminal types. This should be used where practical. */ @@ -26,7 +26,8 @@ */ #include "def.h" -#define BEL 0x07 /* BEL character. */ +#include +#include extern int ttrow; extern int ttcol; @@ -34,150 +35,81 @@ extern int ttbot; extern int tthue; +extern int ttputc(); + int tceeol; /* Costs are set later */ int tcinsl; int tcdell; static int insdel; /* Do we have both insert & delete line? */ +static char * scroll_fwd; /* How to scroll forward. */ #ifdef NO_RESIZE static setttysize(); #endif -char *tgetstr(); -char *tgoto(); -int ttputc(); - -#define TCAPSLEN 1024 - -char tcapbuf[TCAPSLEN]; - -/* PC, UP, and BC are used by termlib, so must be extern and have these - * names unless you have a non-standard termlib. - */ - -int LI; /* standard # lines */ -char PC, - *CM, - *CE, - *UP, - *BC, - *IM, /* insert mode */ - *IC, /* insert a single space */ - *EI, /* end insert mode */ - *DC, - *AL, /* add line */ - *DL, /* del line */ - *pAL, /* parameterized add line */ - *pDL, /* parameterized delete line */ - *TI, /* term init -- start using cursor motion */ - *TE, /* term end --- end using cursor motion */ - *SO, - *SE, - *CD, - *CS, /* set scroll region */ - *SF, /* forw index (used with scroll region) */ - *SR; /* back index (used with scroll region) */ -#ifdef XKEYS -char *KS, *KE; /* enter keypad mode, exit keypad mode */ -#endif -int SG; /* number of glitches, 0 for invisible, -1 for none */ - /* (yes virginia, there are terminals with invisible glitches) */ - /* * Initialize the terminal when the editor * gets started up. */ -static char tcbuf[1024]; - ttinit() { char *tv_stype; - char *t, *p, *tgetstr(); + char *t, *p; #ifndef gettermtype /* (avoid declaration if #define) */ char *gettermtype(); /* system dependent function to determin terminal type */ #endif - if((tv_stype = gettermtype()) == NULL) + if ((tv_stype = gettermtype()) == NULL) panic("Could not determine terminal type"); - if((tgetent(tcbuf, tv_stype)) != 1) { - (VOID) strcpy(tcbuf, "Unknown terminal type "); - (VOID) strcat(tcbuf, tv_stype); - panic(tcbuf); + if (setupterm(tv_stype, 1, NULL) == ERR) { + (void) asprintf(&p, "Unknown terminal type: %s", tv_stype); + panic(p); } - p = tcapbuf; - t = tgetstr("pc", &p); - if(t) PC = *t; - - LI = tgetnum("li"); - CD = tgetstr("cd", &p); - CM = tgetstr("cm", &p); - CE = tgetstr("ce", &p); - UP = tgetstr("up", &p); - BC = tgetstr("bc", &p); - IM = tgetstr("im", &p); - IC = tgetstr("ic", &p); - EI = tgetstr("ei", &p); - DC = tgetstr("dc", &p); - AL = tgetstr("al", &p); - DL = tgetstr("dl", &p); - pAL= tgetstr("AL", &p); /* parameterized insert and del. line */ - pDL= tgetstr("DL", &p); - TI = tgetstr("ti", &p); - TE = tgetstr("te", &p); - SO = tgetstr("so", &p); - SE = tgetstr("se", &p); - CS = tgetstr("cs", &p); /* set scrolling region */ - SF = tgetstr("sf", &p); - if(!SF || !*SF) { /* this is what GNU Emacs does */ - SF = tgetstr("do", &p); - if(!SF || !*SF) { - SF = tgetstr("nl", &p); - if(!SF || !*SF) SF = "\n"; - } + scroll_fwd = scroll_forward; + if (!scroll_fwd || !*scroll_fwd) { + /* this is what GNU Emacs does */ + scroll_fwd = parm_down_cursor; + if (!scroll_fwd || !*scroll_fwd) + scroll_fwd = "\n"; } - SR = tgetstr("sr", &p); - SG = tgetnum("sg"); /* standout glitch */ -#ifdef XKEYS - KS = tgetstr("ks", &p); /* keypad start, keypad end */ - KE = tgetstr("ke", &p); -#endif - if(CM == NULL || UP == NULL) - panic("This terminal is to stupid to run MicroGnuEmacs\n"); + if (!cursor_address || !cursor_up) + panic("This terminal is to stupid to run mg"); ttresize(); /* set nrow & ncol */ - /* watch out for empty capabilities (sure to be wrong) */ - if (CE && !*CE) CE = NULL; - if (CS && !*CS) CS = NULL; - if (SR && !*SR) SR = NULL; - if (AL && !*AL) AL = NULL; - if (DL && !*DL) DL = NULL; - if (pAL && !*pAL) pAL = NULL; - if (pDL && !*pDL) pDL = NULL; - if (CD && !*CD) CD = NULL; + if (!clr_eol) + tceeol = ncol; + else + tceeol = charcost(clr_eol); - if(!CE) tceeol = ncol; - else tceeol = charcost(CE); - /* Estimate cost of inserting a line */ - if (CS && SR) tcinsl = charcost(CS)*2 + charcost(SR); - else if (pAL) tcinsl = charcost(pAL); - else if (AL) tcinsl = charcost(AL); - else tcinsl = NROW * NCOL; /* make this cost high enough */ + if (change_scroll_region && scroll_reverse) + tcinsl = charcost(change_scroll_region)*2 + + charcost(scroll_reverse); + else if (parm_insert_line) + tcinsl = charcost(parm_insert_line); + else if (insert_line) + tcinsl = charcost(insert_line); + else + tcinsl = NROW * NCOL; /* make this cost high enough */ /* Estimate cost of deleting a line */ - if (CS) tcdell = charcost(CS)*2 + charcost(SF); - else if (pDL) tcdell = charcost(pDL); - else if (DL) tcdell = charcost(DL); - else tcdell = NROW * NCOL; /* make this cost high enough */ + if (change_scroll_region) + tcdell = charcost(change_scroll_region)*2 + + charcost(scroll_fwd); + else if (parm_delete_line) + tcdell = charcost(parm_delete_line); + else if (delete_line) + tcdell = charcost(delete_line); + else + tcdell = NROW * NCOL; /* make this cost high enough */ /* Flag to indicate that we can both insert and delete lines */ - insdel = (AL || pAL) && (DL || pDL); + insdel = (insert_line || parm_insert_line) && (delete_line || parm_delete_line); - if (p >= &tcapbuf[TCAPSLEN]) - panic("Terminal description too big!\n"); - if (TI && *TI) putpad(TI, 1); /* init the term */ + if (enter_ca_mode) + putpad(enter_ca_mode, 1); /* init the term */ } /* @@ -189,7 +121,10 @@ * back to what it was. */ tttidy() { - if (TE && *TE) putpad(TE, 1); /* set the term back to normal mode */ + + /* set the term back to normal mode */ + if (exit_ca_mode) + putpad(exit_ca_mode, 1); #ifdef XKEYS ttykeymaptidy(); #endif @@ -205,10 +140,10 @@ ttmove(row, col) { char *tgoto(); - if (ttrow!=row || ttcol!=col) { - putpad(tgoto(CM, col, row), 1); - ttrow = row; - ttcol = col; + if (ttrow != row || ttcol !=col) { + putpad(tgoto(cursor_address, col, row), 1); + ttrow = row; + ttcol = col; } } @@ -216,38 +151,44 @@ * Erase to end of line. */ tteeol() { - if(CE) putpad(CE, 1); - else { - register int i=ncol-ttcol; - while(i--) ttputc(' '); - ttrow = ttcol = HUGE; - } + + if (clr_eol) + putpad(clr_eol, 1); + else { + int i = ncol - ttcol; + while (i--) + ttputc(' '); + ttrow = ttcol = HUGE; + } } /* * Erase to end of page. */ tteeop() { - if(CD) putpad(CD, nrow - ttrow); - else { - putpad(CE, 1); - if (insdel) ttdell(ttrow + 1, LI, LI - ttrow - 1); - else { /* do it by hand */ - register int line; - for (line = ttrow + 1; line <= LI; ++line) { - ttmove(line, 0); - tteeol(); - } + int line; + + if (clr_eos) + putpad(clr_eos, nrow - ttrow); + else { + putpad(clr_eol, 1); + if (insdel) + ttdell(ttrow + 1, lines, lines - ttrow - 1); + else { /* do it by hand */ + for (line = ttrow + 1; line <= lines; ++line) { + ttmove(line, 0); + tteeol(); + } + } + ttrow = ttcol = HUGE; } - ttrow = ttcol = HUGE; - } } /* * Make a noise. */ ttbeep() { - ttputc(BEL); + putpad(bell, 1); ttflush(); } @@ -260,34 +201,43 @@ * of insert and delete line sequences */ ttinsl(row, bot, nchunk) { - register int i, nl; + int i, nl; - if (row == bot) { /* Case of one line insert is */ - ttmove(row, 0); /* special */ - tteeol(); - return; - } - if (CS && SR) { /* Use scroll region and back index */ - nl = bot - row; - ttwindow(row,bot); - ttmove(row, 0); - while (nchunk--) putpad(SR, nl); - ttnowindow(); - return; - } else if (insdel) { - ttmove(1+bot-nchunk, 0); - nl = nrow - ttrow; - if (pDL) putpad(tgoto(pDL, 0, nchunk), nl); - else for (i=0; i LI ? nrow : LI) - 1, 0), nrow - ttrow); - ttrow = HUGE; /* Unknown. */ - ttcol = HUGE; - tttop = HUGE; /* No scroll region. */ - ttbot = HUGE; - } + if (change_scroll_region) { + putpad(tgoto(change_scroll_region, + (nrow > lines ? nrow : lines) - 1, 0), nrow - ttrow); + ttrow = HUGE; /* Unknown. */ + ttcol = HUGE; + tttop = HUGE; /* No scroll region. */ + ttbot = HUGE; + } } /* @@ -383,15 +342,17 @@ * line by line basis, so don't bother sending * out the color shift. */ -ttcolor(color) register int color; { - if (color != tthue) { - if (color == CTEXT) { /* Normal video. */ - putpad(SE, 1); - } else if (color == CMODE) { /* Reverse video. */ - putpad(SO, 1); +ttcolor(color) + int color; +{ + + if (color != tthue) { + if (color == CTEXT) /* Normal video. */ + putpad(exit_standout_mode, 1); + else if (color == CMODE) /* Reverse video. */ + putpad(enter_standout_mode, 1); + tthue = color; /* Save the color. */ } - tthue = color; /* Save the color. */ - } } /* @@ -404,6 +365,7 @@ * see how the caller deals with a change. */ ttresize() { + setttysize(); /* found in "ttyio.c", */ /* ask OS for tty size */ if (nrow < 1) /* Check limits. */ @@ -435,8 +397,8 @@ /* calculate the cost of doing string s */ charcost (s) char *s; { - cci = 0; + cci = 0; - tputs(s, nrow, fakec); - return cci; + tputs(s, nrow, fakec); + return (cci); }