Annotation of src/usr.bin/more/output.c, Revision 1.1
1.1 ! deraadt 1: /*
! 2: * Copyright (c) 1988 Mark Nudleman
! 3: * Copyright (c) 1988 Regents of the University of California.
! 4: * All rights reserved.
! 5: *
! 6: * Redistribution and use in source and binary forms, with or without
! 7: * modification, are permitted provided that the following conditions
! 8: * are met:
! 9: * 1. Redistributions of source code must retain the above copyright
! 10: * notice, this list of conditions and the following disclaimer.
! 11: * 2. Redistributions in binary form must reproduce the above copyright
! 12: * notice, this list of conditions and the following disclaimer in the
! 13: * documentation and/or other materials provided with the distribution.
! 14: * 3. All advertising materials mentioning features or use of this software
! 15: * must display the following acknowledgement:
! 16: * This product includes software developed by the University of
! 17: * California, Berkeley and its contributors.
! 18: * 4. Neither the name of the University nor the names of its contributors
! 19: * may be used to endorse or promote products derived from this software
! 20: * without specific prior written permission.
! 21: *
! 22: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
! 23: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
! 24: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
! 25: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
! 26: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
! 27: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
! 28: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
! 29: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
! 30: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
! 31: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
! 32: * SUCH DAMAGE.
! 33: */
! 34:
! 35: #ifndef lint
! 36: /* from: static char sccsid[] = "@(#)output.c 5.10 (Berkeley) 7/24/91"; */
! 37: static char *rcsid = "$Id: output.c,v 1.4 1994/12/24 17:17:11 cgd Exp $";
! 38: #endif /* not lint */
! 39:
! 40: /*
! 41: * High level routines dealing with the output to the screen.
! 42: */
! 43:
! 44: #include <stdio.h>
! 45: #include <string.h>
! 46: #include <less.h>
! 47:
! 48: int errmsgs; /* Count of messages displayed by error() */
! 49:
! 50: extern int sigs;
! 51: extern int sc_width, sc_height;
! 52: extern int ul_width, ue_width;
! 53: extern int so_width, se_width;
! 54: extern int bo_width, be_width;
! 55: extern int tabstop;
! 56: extern int screen_trashed;
! 57: extern int any_display;
! 58: extern char *line;
! 59:
! 60: /* display the line which is in the line buffer. */
! 61: put_line()
! 62: {
! 63: register char *p;
! 64: register int c;
! 65: register int column;
! 66: extern int auto_wrap, ignaw;
! 67:
! 68: if (sigs)
! 69: {
! 70: /*
! 71: * Don't output if a signal is pending.
! 72: */
! 73: screen_trashed = 1;
! 74: return;
! 75: }
! 76:
! 77: if (line == NULL)
! 78: line = "";
! 79:
! 80: column = 0;
! 81: for (p = line; *p != '\0'; p++)
! 82: {
! 83: switch (c = *p)
! 84: {
! 85: case UL_CHAR:
! 86: ul_enter();
! 87: column += ul_width; /* +1; XXX */
! 88: break;
! 89: case UE_CHAR:
! 90: ul_exit();
! 91: column += ue_width;
! 92: break;
! 93: case BO_CHAR:
! 94: bo_enter();
! 95: column += bo_width; /* +1; XXX */
! 96: break;
! 97: case BE_CHAR:
! 98: bo_exit();
! 99: column += be_width;
! 100: break;
! 101: case '\t':
! 102: do
! 103: {
! 104: putchr(' ');
! 105: column++;
! 106: } while ((column % tabstop) != 0);
! 107: break;
! 108: case '\b':
! 109: putbs();
! 110: column--;
! 111: break;
! 112: default:
! 113: if (c & 0200)
! 114: {
! 115: /*
! 116: * Control characters arrive here as the
! 117: * normal character [CARAT_CHAR(c)] with
! 118: * the 0200 bit set. See pappend().
! 119: */
! 120: putchr('^');
! 121: putchr(c & 0177);
! 122: column += 2;
! 123: } else
! 124: {
! 125: putchr(c);
! 126: column++;
! 127: }
! 128: }
! 129: }
! 130: if (column < sc_width || !auto_wrap || ignaw)
! 131: putchr('\n');
! 132: }
! 133:
! 134: static char obuf[1024];
! 135: static char *ob = obuf;
! 136:
! 137: /*
! 138: * Flush buffered output.
! 139: */
! 140: flush()
! 141: {
! 142: register int n;
! 143:
! 144: n = ob - obuf;
! 145: if (n == 0)
! 146: return;
! 147: if (write(1, obuf, n) != n)
! 148: screen_trashed = 1;
! 149: ob = obuf;
! 150: }
! 151:
! 152: /*
! 153: * Purge any pending output.
! 154: */
! 155: purge()
! 156: {
! 157:
! 158: ob = obuf;
! 159: }
! 160:
! 161: /*
! 162: * Output a character.
! 163: */
! 164: putchr(c)
! 165: int c;
! 166: {
! 167: if (ob >= &obuf[sizeof(obuf)])
! 168: flush();
! 169: *ob++ = c;
! 170: }
! 171:
! 172: /*
! 173: * Output a string.
! 174: */
! 175: putstr(s)
! 176: register char *s;
! 177: {
! 178: while (*s != '\0')
! 179: putchr(*s++);
! 180: }
! 181:
! 182: int cmdstack;
! 183: static char return_to_continue[] = "(press RETURN)";
! 184:
! 185: /*
! 186: * Output a message in the lower left corner of the screen
! 187: * and wait for carriage return.
! 188: */
! 189: error(s)
! 190: char *s;
! 191: {
! 192: int ch;
! 193:
! 194: ++errmsgs;
! 195: if (!any_display) {
! 196: /*
! 197: * Nothing has been displayed yet. Output this message on
! 198: * error output (file descriptor 2) and don't wait for a
! 199: * keystroke to continue.
! 200: *
! 201: * This has the desirable effect of producing all error
! 202: * messages on error output if standard output is directed
! 203: * to a file. It also does the same if we never produce
! 204: * any real output; for example, if the input file(s) cannot
! 205: * be opened. If we do eventually produce output, code in
! 206: * edit() makes sure these messages can be seen before they
! 207: * are overwritten or scrolled away.
! 208: */
! 209: if (s != NULL) {
! 210: (void)write(2, s, strlen(s));
! 211: (void)write(2, "\n", 1);
! 212: }
! 213: return;
! 214: }
! 215:
! 216: lower_left();
! 217: clear_eol();
! 218: so_enter();
! 219: if (s != NULL) {
! 220: putstr(s);
! 221: putstr(" ");
! 222: }
! 223: putstr(return_to_continue);
! 224: so_exit();
! 225:
! 226: if ((ch = getchr()) != '\n') {
! 227: if (ch == 'q')
! 228: quit();
! 229: cmdstack = ch;
! 230: }
! 231: lower_left();
! 232:
! 233: if ((s != NULL ? strlen(s) : 0) + sizeof(return_to_continue) +
! 234: so_width + se_width + 1 > sc_width)
! 235: /*
! 236: * Printing the message has probably scrolled the screen.
! 237: * {{ Unless the terminal doesn't have auto margins,
! 238: * in which case we just hammered on the right margin. }}
! 239: */
! 240: /* repaint(); */
! 241: screen_trashed = 1; /* XXX */
! 242: flush();
! 243: }
! 244:
! 245: static char intr_to_abort[] = "... (interrupt to abort)";
! 246:
! 247: ierror(s)
! 248: char *s;
! 249: {
! 250: lower_left();
! 251: clear_eol();
! 252: so_enter();
! 253: putstr(s);
! 254: putstr(intr_to_abort);
! 255: so_exit();
! 256: flush();
! 257: }