Home | History | Annotate | Line # | Download | only in common
pager.c revision 1.1
      1  1.1  cherry /*	$NetBSD: pager.c,v 1.1 2006/04/07 14:21:29 cherry Exp $	*/
      2  1.1  cherry 
      3  1.1  cherry /*-
      4  1.1  cherry  * Copyright (c) 1998 Michael Smith <msmith (at) freebsd.org>
      5  1.1  cherry  * All rights reserved.
      6  1.1  cherry  *
      7  1.1  cherry  * Redistribution and use in source and binary forms, with or without
      8  1.1  cherry  * modification, are permitted provided that the following conditions
      9  1.1  cherry  * are met:
     10  1.1  cherry  * 1. Redistributions of source code must retain the above copyright
     11  1.1  cherry  *    notice, this list of conditions and the following disclaimer.
     12  1.1  cherry  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  cherry  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  cherry  *    documentation and/or other materials provided with the distribution.
     15  1.1  cherry  *
     16  1.1  cherry  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.1  cherry  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1  cherry  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1  cherry  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.1  cherry  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1  cherry  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1  cherry  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1  cherry  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1  cherry  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  cherry  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  cherry  * SUCH DAMAGE.
     27  1.1  cherry  */
     28  1.1  cherry /*
     29  1.1  cherry  * Simple paged-output and paged-viewing functions
     30  1.1  cherry  */
     31  1.1  cherry 
     32  1.1  cherry #include <sys/cdefs.h>
     33  1.1  cherry 
     34  1.1  cherry #include "lib/libsa/stand.h"
     35  1.1  cherry 
     36  1.1  cherry #include <bootstrap.h>
     37  1.1  cherry 
     38  1.1  cherry static int	p_maxlines = -1;
     39  1.1  cherry static int	p_freelines;
     40  1.1  cherry 
     41  1.1  cherry static char *pager_prompt1 = " --more--  <space> page down <enter> line down <q> quit ";
     42  1.1  cherry static char *pager_blank   = "                                                        ";
     43  1.1  cherry 
     44  1.1  cherry /*
     45  1.1  cherry  * 'open' the pager
     46  1.1  cherry  */
     47  1.1  cherry void
     48  1.1  cherry pager_open(void)
     49  1.1  cherry {
     50  1.1  cherry     int		nlines;
     51  1.1  cherry     char	*cp, *lp;
     52  1.1  cherry 
     53  1.1  cherry     nlines = 24;		/* sensible default */
     54  1.1  cherry     if ((cp = getenv("LINES")) != NULL) {
     55  1.1  cherry 	nlines = strtol(cp, &lp, 0);
     56  1.1  cherry     }
     57  1.1  cherry 
     58  1.1  cherry     p_maxlines = nlines - 1;
     59  1.1  cherry     if (p_maxlines < 1)
     60  1.1  cherry 	p_maxlines = 1;
     61  1.1  cherry     p_freelines = p_maxlines;
     62  1.1  cherry }
     63  1.1  cherry 
     64  1.1  cherry /*
     65  1.1  cherry  * 'close' the pager
     66  1.1  cherry  */
     67  1.1  cherry void
     68  1.1  cherry pager_close(void)
     69  1.1  cherry {
     70  1.1  cherry     p_maxlines = -1;
     71  1.1  cherry }
     72  1.1  cherry 
     73  1.1  cherry /*
     74  1.1  cherry  * Emit lines to the pager; may not return until the user
     75  1.1  cherry  * has responded to the prompt.
     76  1.1  cherry  *
     77  1.1  cherry  * Will return nonzero if the user enters 'q' or 'Q' at the prompt.
     78  1.1  cherry  *
     79  1.1  cherry  * XXX note that this watches outgoing newlines (and eats them), but
     80  1.1  cherry  *     does not handle wrap detection (req. count of columns).
     81  1.1  cherry  */
     82  1.1  cherry 
     83  1.1  cherry int
     84  1.1  cherry pager_output(const char *cp)
     85  1.1  cherry {
     86  1.1  cherry     int		action;
     87  1.1  cherry 
     88  1.1  cherry     if (cp == NULL)
     89  1.1  cherry 	return(0);
     90  1.1  cherry 
     91  1.1  cherry     for (;;) {
     92  1.1  cherry 	if (*cp == 0)
     93  1.1  cherry 	    return(0);
     94  1.1  cherry 
     95  1.1  cherry 	putchar(*cp);			/* always emit character */
     96  1.1  cherry 
     97  1.1  cherry 	if (*(cp++) == '\n') {		/* got a newline? */
     98  1.1  cherry 	    p_freelines--;
     99  1.1  cherry 	    if (p_freelines <= 0) {
    100  1.1  cherry 		printf("%s", pager_prompt1);
    101  1.1  cherry 		action = 0;
    102  1.1  cherry 		while (action == 0) {
    103  1.1  cherry 		    switch(getchar()) {
    104  1.1  cherry 		    case '\r':
    105  1.1  cherry 		    case '\n':
    106  1.1  cherry 			p_freelines = 1;
    107  1.1  cherry 			action = 1;
    108  1.1  cherry 			break;
    109  1.1  cherry 		    case ' ':
    110  1.1  cherry 			p_freelines = p_maxlines;
    111  1.1  cherry 			action = 1;
    112  1.1  cherry 			break;
    113  1.1  cherry 		    case 'q':
    114  1.1  cherry 		    case 'Q':
    115  1.1  cherry 			action = 2;
    116  1.1  cherry 			break;
    117  1.1  cherry 		    default:
    118  1.1  cherry 			break;
    119  1.1  cherry 		    }
    120  1.1  cherry 		}
    121  1.1  cherry 		printf("\r%s\r", pager_blank);
    122  1.1  cherry 		if (action == 2)
    123  1.1  cherry 		    return(1);
    124  1.1  cherry 	    }
    125  1.1  cherry 	}
    126  1.1  cherry     }
    127  1.1  cherry }
    128  1.1  cherry 
    129  1.1  cherry /*
    130  1.1  cherry  * Display from (fd).
    131  1.1  cherry  */
    132  1.1  cherry int
    133  1.1  cherry pager_file(const char *fname)
    134  1.1  cherry {
    135  1.1  cherry     char	buf[80];
    136  1.1  cherry     size_t	hmuch;
    137  1.1  cherry     int		fd;
    138  1.1  cherry     int		result;
    139  1.1  cherry 
    140  1.1  cherry     if ((fd = open(fname, O_RDONLY)) == -1) {
    141  1.1  cherry 	printf("can't open '%s': %s\n", fname, strerror(errno));
    142  1.1  cherry 	return(-1);
    143  1.1  cherry     }
    144  1.1  cherry 
    145  1.1  cherry     for (;;) {
    146  1.1  cherry 	hmuch = read(fd, buf, sizeof(buf) - 1);
    147  1.1  cherry 	if (hmuch == -1) {
    148  1.1  cherry 	    result = -1;
    149  1.1  cherry 	    break;
    150  1.1  cherry 	}
    151  1.1  cherry 	if (hmuch == 0) {
    152  1.1  cherry 	    result = 0;
    153  1.1  cherry 	    break;
    154  1.1  cherry 	}
    155  1.1  cherry 	buf[hmuch] = 0;
    156  1.1  cherry 	if (pager_output(buf)) {
    157  1.1  cherry 	    result = 1;
    158  1.1  cherry 	    break;
    159  1.1  cherry 	}
    160  1.1  cherry     }
    161  1.1  cherry     close(fd);
    162  1.1  cherry     return(result);
    163  1.1  cherry }
    164