[BACK]Return to screen.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / less

Annotation of src/usr.bin/less/screen.c, Revision 1.23

1.1       etheisen    1: /*
1.17      shadchin    2:  * Copyright (C) 1984-2012  Mark Nudelman
1.19      nicm        3:  * Modified for use with illumos by Garrett D'Amore.
                      4:  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
1.1       etheisen    5:  *
1.11      millert     6:  * You may distribute under the terms of either the GNU General Public
                      7:  * License or the Less License, as specified in the README file.
1.1       etheisen    8:  *
1.17      shadchin    9:  * For more information, see the README file.
1.18      nicm       10:  */
1.1       etheisen   11:
                     12: /*
                     13:  * Routines which deal with the characteristics of the terminal.
                     14:  * Uses termcap to be as terminal-independent as possible.
                     15:  */
                     16:
1.23    ! mmcc       17: #include <sys/ioctl.h>
1.1       etheisen   18:
1.23    ! mmcc       19: #include <term.h>
1.15      shadchin   20: #include <termios.h>
1.23    ! mmcc       21:
        !            22: #include "cmd.h"
        !            23: #include "less.h"
1.11      millert    24:
1.1       etheisen   25: #define        DEFAULT_TERM            "unknown"
1.11      millert    26:
1.1       etheisen   27: /*
                     28:  * Strings passed to tputs() to do various terminal functions.
                     29:  */
                     30: static char
                     31:        *sc_home,               /* Cursor home */
                     32:        *sc_addline,            /* Add line, scroll down following lines */
                     33:        *sc_lower_left,         /* Cursor to last line, first column */
1.15      shadchin   34:        *sc_return,             /* Cursor to beginning of current line */
1.1       etheisen   35:        *sc_move,               /* General cursor positioning */
                     36:        *sc_clear,              /* Clear screen */
                     37:        *sc_eol_clear,          /* Clear to end of line */
                     38:        *sc_eos_clear,          /* Clear to end of screen */
                     39:        *sc_s_in,               /* Enter standout (highlighted) mode */
                     40:        *sc_s_out,              /* Exit standout mode */
                     41:        *sc_u_in,               /* Enter underline mode */
                     42:        *sc_u_out,              /* Exit underline mode */
                     43:        *sc_b_in,               /* Enter bold mode */
                     44:        *sc_b_out,              /* Exit bold mode */
                     45:        *sc_bl_in,              /* Enter blink mode */
                     46:        *sc_bl_out,             /* Exit blink mode */
                     47:        *sc_visual_bell,        /* Visual bell (flash screen) sequence */
                     48:        *sc_backspace,          /* Backspace cursor */
                     49:        *sc_s_keypad,           /* Start keypad mode */
                     50:        *sc_e_keypad,           /* End keypad mode */
                     51:        *sc_init,               /* Startup terminal initialization */
                     52:        *sc_deinit;             /* Exit terminal de-initialization */
                     53:
                     54: static int init_done = 0;
                     55:
1.18      nicm       56: int auto_wrap;                 /* Terminal does \r\n when write past margin */
                     57: int ignaw;                     /* Terminal ignores \n immediately after wrap */
                     58: int erase_char;                        /* The user's erase char */
                     59: int erase2_char;               /* The user's other erase char */
                     60: int kill_char;                 /* The user's line-kill char */
                     61: int werase_char;               /* The user's word-erase char */
                     62: int sc_width, sc_height;       /* Height & width of screen */
                     63: int bo_s_width, bo_e_width;    /* Printing width of boldface seq */
                     64: int ul_s_width, ul_e_width;    /* Printing width of underline seq */
                     65: int so_s_width, so_e_width;    /* Printing width of standout seq */
                     66: int bl_s_width, bl_e_width;    /* Printing width of blink seq */
                     67: int can_goto_line;             /* Can move cursor to any line */
                     68: int missing_cap = 0;           /* Some capability is missing */
                     69: static int above_mem;          /* Memory retained above screen */
                     70: static int below_mem;          /* Memory retained below screen */
1.11      millert    71:
                     72: static int attrmode = AT_NORMAL;
1.15      shadchin   73: extern int binattr;
1.1       etheisen   74:
1.18      nicm       75: static char *cheaper(char *, char *, char *);
                     76: static void tmodes(char *, char *, char **, char **, char *, char *);
1.1       etheisen   77:
                     78: extern int quiet;              /* If VERY_QUIET, use visual bell for bell */
1.11      millert    79: extern int no_back_scroll;
1.1       etheisen   80: extern int swindow;
                     81: extern int no_init;
1.11      millert    82: extern int no_keypad;
1.16      millert    83: extern volatile sig_atomic_t sigs;
1.11      millert    84: extern int wscroll;
                     85: extern int screen_trashed;
                     86: extern int tty;
1.15      shadchin   87: extern int top_scroll;
                     88: extern int oldbot;
1.1       etheisen   89: extern int hilite_search;
                     90:
                     91: /*
                     92:  * Change terminal to "raw mode", or restore to "normal" mode.
1.18      nicm       93:  * "Raw mode" means
1.1       etheisen   94:  *     1. An outstanding read will complete on receipt of a single keystroke.
1.18      nicm       95:  *     2. Input is not echoed.
1.1       etheisen   96:  *     3. On output, \n is mapped to \r\n.
                     97:  *     4. \t is NOT expanded into spaces.
                     98:  *     5. Signal-causing characters such as ctrl-C (interrupt),
                     99:  *        etc. are NOT disabled.
                    100:  * It doesn't matter whether an input \n is mapped to \r, or vice versa.
                    101:  */
1.18      nicm      102: void
                    103: raw_mode(int on)
1.1       etheisen  104: {
                    105:        static int curr_on = 0;
1.18      nicm      106:        struct termios s;
                    107:        static struct termios save_term;
                    108:        static int saved_term = 0;
1.1       etheisen  109:
                    110:        if (on == curr_on)
                    111:                return;
1.15      shadchin  112:        erase2_char = '\b'; /* in case OS doesn't know about erase2 */
1.1       etheisen  113:
1.18      nicm      114:        if (on) {
1.1       etheisen  115:                /*
                    116:                 * Get terminal modes.
                    117:                 */
1.18      nicm      118:                (void) tcgetattr(tty, &s);
1.1       etheisen  119:
                    120:                /*
                    121:                 * Save modes and set certain variables dependent on modes.
                    122:                 */
1.18      nicm      123:                if (!saved_term) {
1.11      millert   124:                        save_term = s;
                    125:                        saved_term = 1;
                    126:                }
1.18      nicm      127:
1.1       etheisen  128:                erase_char = s.c_cc[VERASE];
1.15      shadchin  129: #ifdef VERASE2
                    130:                erase2_char = s.c_cc[VERASE2];
                    131: #endif
1.1       etheisen  132:                kill_char = s.c_cc[VKILL];
                    133: #ifdef VWERASE
                    134:                werase_char = s.c_cc[VWERASE];
                    135: #endif
                    136:
                    137:                /*
                    138:                 * Set the modes to the way we want them.
                    139:                 */
1.18      nicm      140:                s.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL);
1.1       etheisen  141:
1.18      nicm      142: #ifndef        TAB3
                    143: #define        TAB3    0       /* Its a lie, but TAB3 isn't defined by POSIX. */
1.1       etheisen  144: #endif
1.18      nicm      145:                s.c_oflag |= (TAB3 | OPOST | ONLCR);
                    146:                s.c_oflag &= ~(OCRNL | ONOCR | ONLRET);
1.1       etheisen  147:                s.c_cc[VMIN] = 1;
                    148:                s.c_cc[VTIME] = 0;
1.11      millert   149: #ifdef VLNEXT
                    150:                s.c_cc[VLNEXT] = 0;
                    151: #endif
                    152: #ifdef VDSUSP
                    153:                s.c_cc[VDSUSP] = 0;
                    154: #endif
1.18      nicm      155:        } else {
1.11      millert   156:                /*
                    157:                 * Restore saved modes.
                    158:                 */
                    159:                s = save_term;
                    160:        }
1.18      nicm      161:        (void) tcsetattr(tty, TCSASOFT | TCSADRAIN, &s);
1.1       etheisen  162:        curr_on = on;
                    163: }
                    164:
1.11      millert   165: /*
                    166:  * Some glue to prevent calling termcap functions if tgetent() failed.
                    167:  */
                    168: static int hardcopy;
                    169:
1.1       etheisen  170: /*
1.11      millert   171:  * Get size of the output screen.
1.1       etheisen  172:  */
1.18      nicm      173: static void
                    174: scrsize(void)
1.1       etheisen  175: {
1.21      deraadt   176:        int sys_height = 0, sys_width = 0, n;
                    177:        struct winsize w;
1.18      nicm      178:        char *s;
1.1       etheisen  179:
1.11      millert   180: #define        DEF_SC_WIDTH    80
                    181: #define        DEF_SC_HEIGHT   24
1.1       etheisen  182:
1.18      nicm      183:        if (ioctl(2, TIOCGWINSZ, &w) == 0) {
                    184:                if (w.ws_row > 0)
                    185:                        sys_height = w.ws_row;
                    186:                if (w.ws_col > 0)
                    187:                        sys_width = w.ws_col;
1.1       etheisen  188:        }
                    189:
1.11      millert   190:        if (sys_height > 0)
                    191:                sc_height = sys_height;
                    192:        else if ((s = lgetenv("LINES")) != NULL)
                    193:                sc_height = atoi(s);
1.18      nicm      194:        else if (!hardcopy && (n = lines) > 0)
                    195:                sc_height = n;
1.17      shadchin  196:        if (sc_height <= 0)
1.11      millert   197:                sc_height = DEF_SC_HEIGHT;
1.1       etheisen  198:
1.11      millert   199:        if (sys_width > 0)
                    200:                sc_width = sys_width;
                    201:        else if ((s = lgetenv("COLUMNS")) != NULL)
                    202:                sc_width = atoi(s);
1.18      nicm      203:        else if (!hardcopy && (n = columns) > 0)
                    204:                sc_width = n;
1.17      shadchin  205:        if (sc_width <= 0)
1.11      millert   206:                sc_width = DEF_SC_WIDTH;
                    207: }
1.1       etheisen  208:
                    209: /*
1.11      millert   210:  * Return the characters actually input by a "special" key.
1.1       etheisen  211:  */
1.18      nicm      212: char *
                    213: special_key_str(int key)
1.1       etheisen  214: {
1.11      millert   215:        char *s;
1.18      nicm      216:        static char ctrlk[] = { CONTROL('K'), 0 };
                    217:
                    218:        if (hardcopy)
                    219:                return (NULL);
1.1       etheisen  220:
1.18      nicm      221:        switch (key) {
1.11      millert   222:        case SK_RIGHT_ARROW:
1.18      nicm      223:                s = key_right;
1.11      millert   224:                break;
                    225:        case SK_LEFT_ARROW:
1.18      nicm      226:                s = key_left;
1.11      millert   227:                break;
                    228:        case SK_UP_ARROW:
1.18      nicm      229:                s = key_up;
1.11      millert   230:                break;
                    231:        case SK_DOWN_ARROW:
1.18      nicm      232:                s = key_down;
1.11      millert   233:                break;
                    234:        case SK_PAGE_UP:
1.18      nicm      235:                s = key_ppage;
1.11      millert   236:                break;
                    237:        case SK_PAGE_DOWN:
1.18      nicm      238:                s = key_npage;
1.11      millert   239:                break;
                    240:        case SK_HOME:
1.18      nicm      241:                s = key_home;
1.11      millert   242:                break;
                    243:        case SK_END:
1.18      nicm      244:                s = key_end;
1.11      millert   245:                break;
                    246:        case SK_DELETE:
1.18      nicm      247:                s = key_dc;
                    248:                if (s == NULL) {
                    249:                        s = "\177\0";
1.11      millert   250:                }
                    251:                break;
                    252:        case SK_CONTROL_K:
1.18      nicm      253:                s = ctrlk;
1.11      millert   254:                break;
                    255:        default:
                    256:                return (NULL);
                    257:        }
                    258:        return (s);
                    259: }
                    260:
                    261: /*
                    262:  * Get terminal capabilities via termcap.
                    263:  */
1.18      nicm      264: void
                    265: get_term(void)
1.11      millert   266: {
1.18      nicm      267:        char *t1, *t2;
1.11      millert   268:        char *term;
                    269:
1.1       etheisen  270:        /*
                    271:         * Find out what kind of terminal this is.
                    272:         */
1.18      nicm      273:        if ((term = lgetenv("TERM")) == NULL)
                    274:                term = DEFAULT_TERM;
1.11      millert   275:        hardcopy = 0;
1.18      nicm      276:
                    277:        if (setupterm(term, 1, NULL) < 0) {
                    278:                hardcopy = 1;
                    279:        }
                    280:        if (hard_copy == 1)
1.11      millert   281:                hardcopy = 1;
1.1       etheisen  282:
                    283:        /*
                    284:         * Get size of the screen.
                    285:         */
                    286:        scrsize();
                    287:        pos_init();
                    288:
1.18      nicm      289:        auto_wrap = hardcopy ? 0 : auto_right_margin;
                    290:        ignaw = hardcopy ? 0 : eat_newline_glitch;
                    291:        above_mem = hardcopy ? 0 : memory_above;
                    292:        below_mem = hardcopy ? 0 : memory_below;
1.1       etheisen  293:
                    294:        /*
                    295:         * Assumes termcap variable "sg" is the printing width of:
                    296:         * the standout sequence, the end standout sequence,
                    297:         * the underline sequence, the end underline sequence,
                    298:         * the boldface sequence, and the end boldface sequence.
                    299:         */
1.18      nicm      300:        if (hardcopy || (so_s_width = magic_cookie_glitch) < 0)
1.1       etheisen  301:                so_s_width = 0;
                    302:        so_e_width = so_s_width;
                    303:
                    304:        bo_s_width = bo_e_width = so_s_width;
                    305:        ul_s_width = ul_e_width = so_s_width;
                    306:        bl_s_width = bl_e_width = so_s_width;
                    307:
                    308:        if (so_s_width > 0 || so_e_width > 0)
                    309:                /*
                    310:                 * Disable highlighting by default on magic cookie terminals.
                    311:                 * Turning on highlighting might change the displayed width
                    312:                 * of a line, causing the display to get messed up.
1.18      nicm      313:                 * The user can turn it back on with -g,
1.1       etheisen  314:                 * but she won't like the results.
                    315:                 */
                    316:                hilite_search = 0;
                    317:
                    318:        /*
                    319:         * Get various string-valued capabilities.
                    320:         */
                    321:
1.18      nicm      322:        sc_s_keypad = keypad_xmit;
                    323:        if (hardcopy || sc_s_keypad == NULL)
1.1       etheisen  324:                sc_s_keypad = "";
1.18      nicm      325:        sc_e_keypad = keypad_local;
                    326:        if (hardcopy || sc_e_keypad == NULL)
1.1       etheisen  327:                sc_e_keypad = "";
1.18      nicm      328:
                    329:        sc_init = enter_ca_mode;
                    330:        if (hardcopy || sc_init == NULL)
1.1       etheisen  331:                sc_init = "";
1.11      millert   332:
1.18      nicm      333:        sc_deinit = exit_ca_mode;
                    334:        if (hardcopy || sc_deinit == NULL)
1.1       etheisen  335:                sc_deinit = "";
                    336:
1.18      nicm      337:        sc_eol_clear = clr_eol;
                    338:        if (hardcopy || sc_eol_clear == NULL || *sc_eol_clear == '\0') {
1.11      millert   339:                missing_cap = 1;
1.1       etheisen  340:                sc_eol_clear = "";
                    341:        }
                    342:
1.18      nicm      343:        sc_eos_clear = clr_eos;
                    344:        if (below_mem &&
                    345:            (hardcopy || sc_eos_clear == NULL || *sc_eos_clear == '\0')) {
1.11      millert   346:                missing_cap = 1;
1.15      shadchin  347:                sc_eos_clear = "";
1.1       etheisen  348:        }
                    349:
1.18      nicm      350:        sc_clear = clear_screen;
                    351:        if (hardcopy || sc_clear == NULL || *sc_clear == '\0') {
1.11      millert   352:                missing_cap = 1;
1.1       etheisen  353:                sc_clear = "\n\n";
                    354:        }
                    355:
1.18      nicm      356:        sc_move = cursor_address;
                    357:        if (hardcopy || sc_move == NULL || *sc_move == '\0') {
1.1       etheisen  358:                /*
1.18      nicm      359:                 * This is not an error here, because we don't
1.1       etheisen  360:                 * always need sc_move.
                    361:                 * We need it only if we don't have home or lower-left.
                    362:                 */
                    363:                sc_move = "";
                    364:                can_goto_line = 0;
1.18      nicm      365:        } else {
1.1       etheisen  366:                can_goto_line = 1;
1.18      nicm      367:        }
1.1       etheisen  368:
1.18      nicm      369:        tmodes(enter_standout_mode, exit_standout_mode, &sc_s_in, &sc_s_out,
                    370:            "", "");
                    371:        tmodes(enter_underline_mode, exit_underline_mode, &sc_u_in, &sc_u_out,
                    372:            sc_s_in, sc_s_out);
                    373:        tmodes(enter_bold_mode, exit_attribute_mode, &sc_b_in, &sc_b_out,
                    374:            sc_s_in, sc_s_out);
                    375:        tmodes(enter_blink_mode, exit_attribute_mode, &sc_bl_in, &sc_bl_out,
                    376:            sc_s_in, sc_s_out);
1.1       etheisen  377:
1.18      nicm      378:        sc_visual_bell = flash_screen;
                    379:        if (hardcopy || sc_visual_bell == NULL)
1.1       etheisen  380:                sc_visual_bell = "";
                    381:
1.18      nicm      382:        sc_backspace = "\b";
1.1       etheisen  383:
                    384:        /*
                    385:         * Choose between using "ho" and "cm" ("home" and "cursor move")
                    386:         * to move the cursor to the upper left corner of the screen.
                    387:         */
1.18      nicm      388:        t1 = cursor_home;
                    389:        if (hardcopy || t1 == NULL)
1.1       etheisen  390:                t1 = "";
1.18      nicm      391:        if (*sc_move == '\0') {
1.1       etheisen  392:                t2 = "";
1.18      nicm      393:        } else {
                    394:                t2 = estrdup(tparm(sc_move, 0, 0, 0, 0, 0, 0, 0, 0, 0));
1.1       etheisen  395:        }
1.11      millert   396:        sc_home = cheaper(t1, t2, "|\b^");
1.1       etheisen  397:
                    398:        /*
                    399:         * Choose between using "ll" and "cm"  ("lower left" and "cursor move")
                    400:         * to move the cursor to the lower left corner of the screen.
                    401:         */
1.18      nicm      402:        t1 = cursor_to_ll;
                    403:        if (hardcopy || t1 == NULL)
1.1       etheisen  404:                t1 = "";
1.18      nicm      405:        if (*sc_move == '\0') {
1.1       etheisen  406:                t2 = "";
1.18      nicm      407:        } else {
                    408:                t2 = estrdup(tparm(sc_move, sc_height-1,
                    409:                    0, 0, 0, 0, 0, 0, 0, 0));
1.1       etheisen  410:        }
1.11      millert   411:        sc_lower_left = cheaper(t1, t2, "\r");
1.1       etheisen  412:
                    413:        /*
1.15      shadchin  414:         * Get carriage return string.
                    415:         */
1.18      nicm      416:        sc_return = carriage_return;
                    417:        if (hardcopy || sc_return == NULL)
1.15      shadchin  418:                sc_return = "\r";
                    419:
                    420:        /*
1.18      nicm      421:         * Choose between using insert_line or scroll_reverse
1.1       etheisen  422:         * to add a line at the top of the screen.
                    423:         */
1.18      nicm      424:        t1 = insert_line;
                    425:        if (hardcopy || t1 == NULL)
1.1       etheisen  426:                t1 = "";
1.18      nicm      427:        t2 = scroll_reverse;
                    428:        if (hardcopy || t2 == NULL)
1.1       etheisen  429:                t2 = "";
                    430:        if (above_mem)
                    431:                sc_addline = t1;
                    432:        else
1.11      millert   433:                sc_addline = cheaper(t1, t2, "");
1.18      nicm      434:        if (*sc_addline == '\0') {
1.1       etheisen  435:                /*
                    436:                 * Force repaint on any backward movement.
                    437:                 */
1.11      millert   438:                no_back_scroll = 1;
1.1       etheisen  439:        }
                    440: }
                    441:
                    442: /*
                    443:  * Return the cost of displaying a termcap string.
                    444:  * We use the trick of calling tputs, but as a char printing function
                    445:  * we give it inc_costcount, which just increments "costcount".
                    446:  * This tells us how many chars would be printed by using this string.
                    447:  * {{ Couldn't we just use strlen? }}
                    448:  */
                    449: static int costcount;
                    450:
1.18      nicm      451: static int
                    452: inc_costcount(int c)
1.1       etheisen  453: {
                    454:        costcount++;
                    455:        return (c);
                    456: }
                    457:
1.18      nicm      458: static int
                    459: cost(char *t)
1.1       etheisen  460: {
                    461:        costcount = 0;
1.18      nicm      462:        (void) tputs(t, sc_height, inc_costcount);
1.1       etheisen  463:        return (costcount);
                    464: }
                    465:
                    466: /*
                    467:  * Return the "best" of the two given termcap strings.
1.18      nicm      468:  * The best, if both exist, is the one with the lower
1.1       etheisen  469:  * cost (see cost() function).
                    470:  */
1.18      nicm      471: static char *
                    472: cheaper(char *t1, char *t2, char *def)
1.1       etheisen  473: {
1.18      nicm      474:        if (*t1 == '\0' && *t2 == '\0') {
1.11      millert   475:                missing_cap = 1;
1.1       etheisen  476:                return (def);
                    477:        }
                    478:        if (*t1 == '\0')
                    479:                return (t2);
                    480:        if (*t2 == '\0')
                    481:                return (t1);
                    482:        if (cost(t1) < cost(t2))
                    483:                return (t1);
                    484:        return (t2);
                    485: }
                    486:
1.18      nicm      487: static void
                    488: tmodes(char *incap, char *outcap, char **instr, char **outstr,
                    489:     char *def_instr, char *def_outstr)
1.11      millert   490: {
1.18      nicm      491:        if (hardcopy) {
                    492:                *instr = "";
                    493:                *outstr = "";
                    494:                return;
                    495:        }
                    496:
                    497:        *instr = incap;
                    498:        *outstr = outcap;
                    499:
                    500:        if (*instr == NULL) {
1.11      millert   501:                /* Use defaults. */
                    502:                *instr = def_instr;
                    503:                *outstr = def_outstr;
                    504:                return;
                    505:        }
                    506:
                    507:        if (*outstr == NULL)
1.18      nicm      508:                /* No specific out capability; use exit_attribute_mode. */
                    509:                *outstr = exit_attribute_mode;
1.11      millert   510:        if (*outstr == NULL)
1.18      nicm      511:                /* Don't even have that, use an empty string */
1.11      millert   512:                *outstr = "";
                    513: }
                    514:
1.1       etheisen  515: /*
1.18      nicm      516:  * Below are the functions which perform all the
1.1       etheisen  517:  * terminal-specific screen manipulation.
                    518:  */
                    519:
                    520: /*
                    521:  * Initialize terminal
                    522:  */
1.18      nicm      523: void
                    524: init(void)
1.1       etheisen  525: {
1.11      millert   526:        if (!no_init)
1.18      nicm      527:                (void) tputs(sc_init, sc_height, putchr);
1.11      millert   528:        if (!no_keypad)
1.18      nicm      529:                (void) tputs(sc_s_keypad, sc_height, putchr);
                    530:        if (top_scroll) {
1.15      shadchin  531:                int i;
                    532:
                    533:                /*
                    534:                 * This is nice to terminals with no alternate screen,
                    535:                 * but with saved scrolled-off-the-top lines.  This way,
                    536:                 * no previous line is lost, but we start with a whole
                    537:                 * screen to ourself.
                    538:                 */
                    539:                for (i = 1; i < sc_height; i++)
1.18      nicm      540:                        (void) putchr('\n');
1.15      shadchin  541:        } else
                    542:                line_left();
1.1       etheisen  543:        init_done = 1;
                    544: }
                    545:
                    546: /*
                    547:  * Deinitialize terminal
                    548:  */
1.18      nicm      549: void
                    550: deinit(void)
1.1       etheisen  551: {
                    552:        if (!init_done)
                    553:                return;
1.11      millert   554:        if (!no_keypad)
1.18      nicm      555:                (void) tputs(sc_e_keypad, sc_height, putchr);
1.11      millert   556:        if (!no_init)
1.18      nicm      557:                (void) tputs(sc_deinit, sc_height, putchr);
1.1       etheisen  558:        init_done = 0;
                    559: }
                    560:
                    561: /*
                    562:  * Home cursor (move to upper left corner of screen).
                    563:  */
1.18      nicm      564: void
                    565: home(void)
1.1       etheisen  566: {
1.18      nicm      567:        (void) tputs(sc_home, 1, putchr);
1.1       etheisen  568: }
                    569:
                    570: /*
                    571:  * Add a blank line (called with cursor at home).
                    572:  * Should scroll the display down.
                    573:  */
1.18      nicm      574: void
                    575: add_line(void)
1.11      millert   576: {
1.18      nicm      577:        (void) tputs(sc_addline, sc_height, putchr);
1.11      millert   578: }
1.1       etheisen  579:
                    580: /*
                    581:  * Move cursor to lower left corner of screen.
                    582:  */
1.18      nicm      583: void
                    584: lower_left(void)
1.1       etheisen  585: {
1.18      nicm      586:        (void) tputs(sc_lower_left, 1, putchr);
1.11      millert   587: }
                    588:
                    589: /*
1.15      shadchin  590:  * Move cursor to left position of current line.
                    591:  */
1.18      nicm      592: void
                    593: line_left(void)
1.15      shadchin  594: {
1.18      nicm      595:        (void) tputs(sc_return, 1, putchr);
1.15      shadchin  596: }
                    597:
                    598: /*
1.18      nicm      599:  * Goto a specific line on the screen.
1.11      millert   600:  */
1.18      nicm      601: void
                    602: goto_line(int slinenum)
1.11      millert   603: {
1.18      nicm      604:        (void) tputs(tparm(sc_move, slinenum, 0, 0, 0, 0, 0, 0, 0, 0), 1,
                    605:            putchr);
1.1       etheisen  606: }
                    607:
                    608: /*
                    609:  * Output the "visual bell", if there is one.
                    610:  */
1.18      nicm      611: void
                    612: vbell(void)
1.1       etheisen  613: {
                    614:        if (*sc_visual_bell == '\0')
                    615:                return;
1.18      nicm      616:        (void) tputs(sc_visual_bell, sc_height, putchr);
1.11      millert   617: }
                    618:
                    619: /*
                    620:  * Make a noise.
                    621:  */
1.18      nicm      622: static void
                    623: beep(void)
1.11      millert   624: {
1.18      nicm      625:        (void) putchr(CONTROL('G'));
1.11      millert   626: }
                    627:
                    628: /*
                    629:  * Ring the terminal bell.
                    630:  */
1.18      nicm      631: void
                    632: ring_bell(void)
1.11      millert   633: {
                    634:        if (quiet == VERY_QUIET)
                    635:                vbell();
                    636:        else
                    637:                beep();
1.1       etheisen  638: }
                    639:
                    640: /*
                    641:  * Clear the screen.
                    642:  */
1.18      nicm      643: void
                    644: do_clear(void)
1.1       etheisen  645: {
1.18      nicm      646:        (void) tputs(sc_clear, sc_height, putchr);
1.1       etheisen  647: }
                    648:
                    649: /*
                    650:  * Clear from the cursor to the end of the cursor's line.
                    651:  * {{ This must not move the cursor. }}
                    652:  */
1.18      nicm      653: void
                    654: clear_eol(void)
1.1       etheisen  655: {
1.18      nicm      656:        (void) tputs(sc_eol_clear, 1, putchr);
1.11      millert   657: }
                    658:
                    659: /*
                    660:  * Clear the current line.
                    661:  * Clear the screen if there's off-screen memory below the display.
                    662:  */
1.18      nicm      663: static void
                    664: clear_eol_bot(void)
1.11      millert   665: {
                    666:        if (below_mem)
1.18      nicm      667:                (void) tputs(sc_eos_clear, 1, putchr);
1.11      millert   668:        else
1.18      nicm      669:                (void) tputs(sc_eol_clear, 1, putchr);
1.1       etheisen  670: }
                    671:
                    672: /*
                    673:  * Clear the bottom line of the display.
                    674:  * Leave the cursor at the beginning of the bottom line.
                    675:  */
1.18      nicm      676: void
                    677: clear_bot(void)
1.1       etheisen  678: {
1.11      millert   679:        /*
                    680:         * If we're in a non-normal attribute mode, temporarily exit
                    681:         * the mode while we do the clear.  Some terminals fill the
                    682:         * cleared area with the current attribute.
                    683:         */
1.15      shadchin  684:        if (oldbot)
                    685:                lower_left();
                    686:        else
                    687:                line_left();
                    688:
                    689:        if (attrmode == AT_NORMAL)
                    690:                clear_eol_bot();
                    691:        else
1.11      millert   692:        {
1.15      shadchin  693:                int saved_attrmode = attrmode;
                    694:
                    695:                at_exit();
1.11      millert   696:                clear_eol_bot();
1.15      shadchin  697:                at_enter(saved_attrmode);
1.11      millert   698:        }
1.1       etheisen  699: }
                    700:
1.18      nicm      701: void
                    702: at_enter(int attr)
1.1       etheisen  703: {
1.15      shadchin  704:        attr = apply_at_specials(attr);
                    705:
                    706:        /* The one with the most priority is last.  */
                    707:        if (attr & AT_UNDERLINE)
1.18      nicm      708:                (void) tputs(sc_u_in, 1, putchr);
1.15      shadchin  709:        if (attr & AT_BOLD)
1.18      nicm      710:                (void) tputs(sc_b_in, 1, putchr);
1.15      shadchin  711:        if (attr & AT_BLINK)
1.18      nicm      712:                (void) tputs(sc_bl_in, 1, putchr);
1.15      shadchin  713:        if (attr & AT_STANDOUT)
1.18      nicm      714:                (void) tputs(sc_s_in, 1, putchr);
1.15      shadchin  715:
                    716:        attrmode = attr;
1.1       etheisen  717: }
                    718:
1.18      nicm      719: void
                    720: at_exit(void)
1.1       etheisen  721: {
1.15      shadchin  722:        /* Undo things in the reverse order we did them.  */
                    723:        if (attrmode & AT_STANDOUT)
1.18      nicm      724:                (void) tputs(sc_s_out, 1, putchr);
1.15      shadchin  725:        if (attrmode & AT_BLINK)
1.18      nicm      726:                (void) tputs(sc_bl_out, 1, putchr);
1.15      shadchin  727:        if (attrmode & AT_BOLD)
1.18      nicm      728:                (void) tputs(sc_b_out, 1, putchr);
1.15      shadchin  729:        if (attrmode & AT_UNDERLINE)
1.18      nicm      730:                (void) tputs(sc_u_out, 1, putchr);
1.15      shadchin  731:
1.11      millert   732:        attrmode = AT_NORMAL;
1.1       etheisen  733: }
                    734:
1.18      nicm      735: void
                    736: at_switch(int attr)
1.1       etheisen  737: {
1.15      shadchin  738:        int new_attrmode = apply_at_specials(attr);
                    739:        int ignore_modes = AT_ANSI;
1.1       etheisen  740:
1.18      nicm      741:        if ((new_attrmode & ~ignore_modes) != (attrmode & ~ignore_modes)) {
1.15      shadchin  742:                at_exit();
                    743:                at_enter(attr);
                    744:        }
1.1       etheisen  745: }
                    746:
1.18      nicm      747: int
                    748: is_at_equiv(int attr1, int attr2)
1.1       etheisen  749: {
1.15      shadchin  750:        attr1 = apply_at_specials(attr1);
                    751:        attr2 = apply_at_specials(attr2);
1.1       etheisen  752:
1.15      shadchin  753:        return (attr1 == attr2);
1.1       etheisen  754: }
                    755:
1.18      nicm      756: int
                    757: apply_at_specials(int attr)
1.1       etheisen  758: {
1.15      shadchin  759:        if (attr & AT_BINARY)
                    760:                attr |= binattr;
                    761:        if (attr & AT_HILITE)
                    762:                attr |= AT_STANDOUT;
                    763:        attr &= ~(AT_BINARY|AT_HILITE);
1.1       etheisen  764:
1.18      nicm      765:        return (attr);
1.1       etheisen  766: }
                    767:
                    768: /*
                    769:  * Output a plain backspace, without erasing the previous char.
                    770:  */
1.18      nicm      771: void
                    772: putbs(void)
1.15      shadchin  773: {
1.18      nicm      774:        (void) tputs(sc_backspace, 1, putchr);
1.11      millert   775: }