Home | History | Annotate | Line # | Download | only in common
      1  1.2  kiyohara /*	$NetBSD: pager.c,v 1.2 2009/07/20 04:59:03 kiyohara 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.2  kiyohara #include "lib/libsa/loadfile.h"
     36  1.1    cherry 
     37  1.1    cherry #include <bootstrap.h>
     38  1.1    cherry 
     39  1.1    cherry static int	p_maxlines = -1;
     40  1.1    cherry static int	p_freelines;
     41  1.1    cherry 
     42  1.1    cherry static char *pager_prompt1 = " --more--  <space> page down <enter> line down <q> quit ";
     43  1.1    cherry static char *pager_blank   = "                                                        ";
     44  1.1    cherry 
     45  1.1    cherry /*
     46  1.1    cherry  * 'open' the pager
     47  1.1    cherry  */
     48  1.1    cherry void
     49  1.1    cherry pager_open(void)
     50  1.1    cherry {
     51  1.1    cherry     int		nlines;
     52  1.1    cherry     char	*cp, *lp;
     53  1.1    cherry 
     54  1.1    cherry     nlines = 24;		/* sensible default */
     55  1.1    cherry     if ((cp = getenv("LINES")) != NULL) {
     56  1.1    cherry 	nlines = strtol(cp, &lp, 0);
     57  1.1    cherry     }
     58  1.1    cherry 
     59  1.1    cherry     p_maxlines = nlines - 1;
     60  1.1    cherry     if (p_maxlines < 1)
     61  1.1    cherry 	p_maxlines = 1;
     62  1.1    cherry     p_freelines = p_maxlines;
     63  1.1    cherry }
     64  1.1    cherry 
     65  1.1    cherry /*
     66  1.1    cherry  * 'close' the pager
     67  1.1    cherry  */
     68  1.1    cherry void
     69  1.1    cherry pager_close(void)
     70  1.1    cherry {
     71  1.1    cherry     p_maxlines = -1;
     72  1.1    cherry }
     73  1.1    cherry 
     74  1.1    cherry /*
     75  1.1    cherry  * Emit lines to the pager; may not return until the user
     76  1.1    cherry  * has responded to the prompt.
     77  1.1    cherry  *
     78  1.1    cherry  * Will return nonzero if the user enters 'q' or 'Q' at the prompt.
     79  1.1    cherry  *
     80  1.1    cherry  * XXX note that this watches outgoing newlines (and eats them), but
     81  1.1    cherry  *     does not handle wrap detection (req. count of columns).
     82  1.1    cherry  */
     83  1.1    cherry 
     84  1.1    cherry int
     85  1.1    cherry pager_output(const char *cp)
     86  1.1    cherry {
     87  1.1    cherry     int		action;
     88  1.1    cherry 
     89  1.1    cherry     if (cp == NULL)
     90  1.1    cherry 	return(0);
     91  1.1    cherry 
     92  1.1    cherry     for (;;) {
     93  1.1    cherry 	if (*cp == 0)
     94  1.1    cherry 	    return(0);
     95  1.1    cherry 
     96  1.1    cherry 	putchar(*cp);			/* always emit character */
     97  1.1    cherry 
     98  1.1    cherry 	if (*(cp++) == '\n') {		/* got a newline? */
     99  1.1    cherry 	    p_freelines--;
    100  1.1    cherry 	    if (p_freelines <= 0) {
    101  1.1    cherry 		printf("%s", pager_prompt1);
    102  1.1    cherry 		action = 0;
    103  1.1    cherry 		while (action == 0) {
    104  1.1    cherry 		    switch(getchar()) {
    105  1.1    cherry 		    case '\r':
    106  1.1    cherry 		    case '\n':
    107  1.1    cherry 			p_freelines = 1;
    108  1.1    cherry 			action = 1;
    109  1.1    cherry 			break;
    110  1.1    cherry 		    case ' ':
    111  1.1    cherry 			p_freelines = p_maxlines;
    112  1.1    cherry 			action = 1;
    113  1.1    cherry 			break;
    114  1.1    cherry 		    case 'q':
    115  1.1    cherry 		    case 'Q':
    116  1.1    cherry 			action = 2;
    117  1.1    cherry 			break;
    118  1.1    cherry 		    default:
    119  1.1    cherry 			break;
    120  1.1    cherry 		    }
    121  1.1    cherry 		}
    122  1.1    cherry 		printf("\r%s\r", pager_blank);
    123  1.1    cherry 		if (action == 2)
    124  1.1    cherry 		    return(1);
    125  1.1    cherry 	    }
    126  1.1    cherry 	}
    127  1.1    cherry     }
    128  1.1    cherry }
    129  1.1    cherry 
    130  1.1    cherry /*
    131  1.1    cherry  * Display from (fd).
    132  1.1    cherry  */
    133  1.1    cherry int
    134  1.1    cherry pager_file(const char *fname)
    135  1.1    cherry {
    136  1.1    cherry     char	buf[80];
    137  1.1    cherry     size_t	hmuch;
    138  1.1    cherry     int		fd;
    139  1.1    cherry     int		result;
    140  1.1    cherry 
    141  1.1    cherry     if ((fd = open(fname, O_RDONLY)) == -1) {
    142  1.1    cherry 	printf("can't open '%s': %s\n", fname, strerror(errno));
    143  1.1    cherry 	return(-1);
    144  1.1    cherry     }
    145  1.1    cherry 
    146  1.1    cherry     for (;;) {
    147  1.1    cherry 	hmuch = read(fd, buf, sizeof(buf) - 1);
    148  1.1    cherry 	if (hmuch == -1) {
    149  1.1    cherry 	    result = -1;
    150  1.1    cherry 	    break;
    151  1.1    cherry 	}
    152  1.1    cherry 	if (hmuch == 0) {
    153  1.1    cherry 	    result = 0;
    154  1.1    cherry 	    break;
    155  1.1    cherry 	}
    156  1.1    cherry 	buf[hmuch] = 0;
    157  1.1    cherry 	if (pager_output(buf)) {
    158  1.1    cherry 	    result = 1;
    159  1.1    cherry 	    break;
    160  1.1    cherry 	}
    161  1.1    cherry     }
    162  1.1    cherry     close(fd);
    163  1.1    cherry     return(result);
    164  1.1    cherry }
    165