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