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