afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome/*
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * All rights reserved.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome *
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Redistribution and use in source and binary forms, with or without
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * modification, are permitted provided that the following conditions
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * are met:
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * 1. Redistributions of source code must retain the above copyright
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * notice, this list of conditions and the following disclaimer.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * notice, this list of conditions and the following disclaimer in the
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * documentation and/or other materials provided with the distribution.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome *
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * SUCH DAMAGE.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome */
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome/*
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Simple paged-output and paged-viewing functions
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome */
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome#include <stdio.h>
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome#include <stdlib.h>
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome#include <string.h>
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome#include <sys/types.h>
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome#include <sys/stat.h>
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome#include <fcntl.h>
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome#include <errno.h>
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome#include <termios.h>
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome#include <unistd.h>
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome#include <stropts.h>
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomestatic int p_maxlines = -1;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomestatic int p_freelines;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomestatic struct termios orig_termios;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomestatic char *pager_prompt1 = \
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome " --more-- <space> page down <enter> line down <q> quit ";
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomestatic char *pager_blank = \
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome " ";
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome/*
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * 'open' the pager
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome */
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomevoid
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomepager_open(void)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome{
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome int nlines;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome char *cp, *lp;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome struct termios raw;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome struct winsize ws;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome tcgetattr(0, &orig_termios);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome raw = orig_termios;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome raw.c_lflag &= ~(ICANON | ECHO);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome raw.c_cc[VMIN] = 1;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome raw.c_cc[VTIME] = 0;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome (void) tcsetattr(0, TCSAFLUSH, &raw);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome nlines = 24; /* sensible default */
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome if (ioctl(1, TIOCGWINSZ, &ws) == -1 || ws.ws_row == 0) {
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome if ((cp = getenv("LINES")) != NULL) {
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome nlines = strtol(cp, &lp, 0);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome }
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome } else
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome nlines = ws.ws_row;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome p_maxlines = nlines - 1;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome if (p_maxlines < 1)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome p_maxlines = 1;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome p_freelines = p_maxlines;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome}
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome/*
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * 'close' the pager
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome */
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomevoid
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomepager_close(void)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome{
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome (void) fflush(stdout);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome p_maxlines = -1;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome (void) tcsetattr(0, TCSAFLUSH, &orig_termios);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome}
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome/*
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Emit lines to the pager; may not return until the user
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * has responded to the prompt.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome *
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Will return nonzero if the user enters 'q' or 'Q' at the prompt.
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome *
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * XXX note that this watches outgoing newlines (and eats them), but
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * does not handle wrap detection (req. count of columns).
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome */
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomeint
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomepager_output(const char *cp)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome{
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome int action;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome if (cp == NULL)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome return (0);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome for (;;) {
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome if (*cp == 0)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome return (0);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome putchar(*cp); /* always emit character */
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome if (*(cp++) == '\n') { /* got a newline? */
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome p_freelines--;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome if (p_freelines <= 0) {
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome printf("%s", pager_prompt1);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome action = 0;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome while (action == 0) {
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome switch (getchar()) {
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome case '\r':
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome case '\n':
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome p_freelines = 1;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome action = 1;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome break;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome case ' ':
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome p_freelines = p_maxlines;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome action = 1;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome break;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome case 'q':
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome case 'Q':
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome action = 2;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome break;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome default:
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome break;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome }
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome }
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome printf("\r%s\r", pager_blank);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome if (action == 2)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome return (1);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome }
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome }
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome }
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome}
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome/*
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome * Display from (fd).
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome */
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomeint
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soomepager_file(const char *fname)
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome{
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome char buf[80];
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome size_t hmuch;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome int fd;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome int result;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome if ((fd = open(fname, O_RDONLY)) == -1) {
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome printf("can't open '%s': %s\n", fname, strerror(errno));
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome return (-1);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome }
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome for (;;) {
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome hmuch = read(fd, buf, sizeof (buf) - 1);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome if (hmuch == -1) {
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome result = -1;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome break;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome }
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome if (hmuch == 0) {
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome result = 0;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome break;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome }
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome buf[hmuch] = 0;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome if (pager_output(buf)) {
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome result = 1;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome break;
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome }
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome }
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome close(fd);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome return (result);
afc2ba1deb75b323afde536f2dd18bcafdaa308dToomas Soome}