pg.c revision 4fce32e15d9c284a06aea2e40852f6674f03b63e
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#pragma ident "%Z%%M% %I% %E% SMI"
#include <signal.h>
#include <setjmp.h>
#include <fcntl.h>
#include <ctype.h>
#include <stdio.h>
#include <wchar.h>
#include <curses.h>
#include <term.h>
#include <errno.h>
#include <stdlib.h>
#include <regexpr.h>
#include <limits.h>
#include <locale.h>
#include <wctype.h> /* iswprint() */
#include <string.h>
#include <unistd.h>
#include <wait.h>
#include <libw.h>
#include <regexpr.h>
/*
* pg -- paginator for crt terminals
*
* Includes the ability to display pages that have
* already passed by. Also gives the user the ability
* to search forward and backwards for regular expressions.
* This works for piped input by copying to a temporary file,
* and resolving backreferences from there.
*
* Note: The reason that there are so many commands to do
* the same types of things is to try to accommodate
* users of other paginators.
*/
#define LINSIZ 1024
#define QUIT '\034'
#define PROMPTSIZE 256
/*
* Function definitions
*/
static void lineset(int);
static char *setprompt();
static void help();
static void re_error(int);
static void save_input(FILE *);
static void save_pipe();
static void erase_line(int);
static void kill_line();
static void doclear();
static void sopr(char *, int);
static void prompt(char *);
static void error(char *);
static void terminit();
static void compact();
static int mrdchar();
static int skipf(int);
static int readch();
static int ttyin();
static int number();
static int command(char *);
static int screen(char *);
static int fgetputc();
static char *pg_strchr();
struct line { /* how line addresses are stored */
};
*dot, /* current line */
*dol, /* last line */
*contig; /* where contiguous (non-aged) lines start */
static long nlall; /* room for how many LINEs in memory */
*tmp_fin, /* pipe temporary file in */
*tmp_fou; /* pipe temporary file out */
static short sign; /* sign of command input */
static int fnum, /* which file argument we're in */
pipe_in, /* set when stdin is a pipe */
out_is_tty; /* set if stdout is a tty */
static void on_brk(),
end_it();
static short brk_hit; /* interrupt handling is pending flag */
static int window = 0; /* window size in lines */
static short rmode = 0; /* deny shell escape in restricted mode */
static short soflag = 0; /* output all messages in standout mode */
static short promptlen; /* length of the current prompt */
static short inwait, /* set while waiting for user input */
errors; /* set if error message has been printed. */
/* if so, need to erase it and prompt */
static char **fnames;
static short status = 0; /* set > 0 if error detected */
static short fflag = 0; /* set if the f option is used */
static short nflag = 0; /* set for "no newline" input option */
static short clropt = 0; /* set if the clear option is used */
static int initopt = 0; /* set if the line option is used */
static int srchopt = 0; /* set if the search option is used */
static int initline;
/* where on the page to leave a found string */
static short nfiles;
static char *shell;
static char *promptstr = ":";
static int catch_susp;
static void onsusp();
struct screen_stat {
short is_eof;
};
static struct screen_stat old_ss = { 0, 0, 0 };
static struct screen_stat new_ss;
static short termflg = 0; /* set once terminal is initialized */
static short eoflag; /* set whenever at end of current file */
static short doliseof; /* set when last line of file is known */
static void usage(void);
int
{
char *s;
char *p;
int prnames = 0;
int opt;
int i;
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#endif
(void) textdomain(TEXT_DOMAIN);
/* check for non-standard "-#" option */
for (i = 1; i < argc; i++) {
break;
"pg: Badly formed number\n"));
usage();
}
while (i < argc) {
i++;
}
i--;
argc--;
}
}
/* check for non-standard + option */
for (i = 1; i < argc; i++) {
break;
if (argv[i][0] == '+') {
srchopt++;
initopt = 0;
*p++ = *s++;
else {
"pg: pattern too long\n"));
return (1);
}
*p = '\0';
} else {
initopt++;
srchopt = 0;
s = &argv[i][2];
for (; isdigit(*s); s++)
if (*s != '\0')
usage();
}
while (i < argc) {
i++;
}
i--;
argc--;
}
}
switch (opt) {
case 'c':
clropt = 1;
break;
case 'e':
eof_pause = 0;
break;
case 'f':
fflag = 1;
break;
case 'n':
nflag = 1;
break;
case 'r':
break;
case 's':
break;
case 'p':
break;
default:
usage();
}
}
if (out_is_tty) {
terminit();
catch_susp++;
}
}
if (window == 0)
if (window <= 1)
window = 2;
if (initline <= 0)
initline = 1;
if (nfiles > 1)
prnames++;
if (nfiles == 0) {
fnames[0] = "-";
nfiles++;
}
status = 2;
fnum++;
} else {
status = 0;
if (out_is_tty)
else {
if (prnames) {
(void) fputs("::::::::::::::\n",
stdout);
(void) fputs("\n::::::::::::::\n",
stdout);
}
fnum++;
}
if (pipe_in)
save_pipe();
else
}
}
end_it();
/*NOTREACHED*/
return (0);
}
static char *
setprompt(s)
char *s;
{
int i = 0;
int pct_d = 0;
static char pstr[PROMPTSIZE];
while (i < PROMPTSIZE - 2)
switch (pstr[i++] = *s++) {
case '\0':
return (pstr);
case '%':
if (*s == 'd' && !pct_d) {
pct_d++;
} else if (*s != '%')
pstr[i++] = '%';
if ((pstr[i++] = *s++) == '\0')
return (pstr);
break;
default:
break;
}
exit(1);
/*NOTREACHED*/
}
/*
* Print out the contents of the file f, one screenful at a time.
*/
static int
char *file_name;
{
int cmd_ret = 0;
short hadchance = 0;
old_ss.first_line = 0;
if (!firstf)
else {
firstf = 0;
if (initopt) {
initopt = 0;
} else if (srchopt) {
srchopt = 0;
} else {
}
}
for (;;) {
if (cmd_ret)
return (cmd_ret);
return (1);
hadchance = 0;
else {
}
if (clropt) {
doclear();
} else {
else
else
else
else
}
if (brk_hit) {
break;
}
}
brk_hit = 0;
return (1);
hadchance++;
error("(EOF)");
}
}
}
/*
* Read a command and do it. A command consists of an optional integer
* argument followed by the command character. Return the number of files
* to skip, 0 if we're still talking about the same file.
*/
static int
char *filename;
{
char *cmdend;
int skip;
int len;
char *p;
for (;;) {
/*
* Wait for output to drain before going on.
* This is done so that the user will not hit
* break and quit before he has seen the prompt.
*/
end_it();
inwait = 1;
brk_hit = 0;
if (errors)
errors = 0;
else {
kill_line();
}
if (ttyin())
continue;
len = 1;
}
switch (wc) {
case 'h':
CHECKEND();
help();
break;
case '\014': /* ^L */
case '.': /* redisplay current window */
CHECKEND();
inwait = 0;
return (0);
case 'w': /* set window size */
case 'z':
if (sign == -1) {
BEEP();
break;
}
CHECKEND();
if (nlines == 0)
else
if (nlines > 1)
else {
BEEP();
break;
}
inwait = 0;
return (0);
case '\004': /* ^D */
case 'd':
CHECKEND();
if (sign == 0)
sign = 1;
inwait = 0;
return (0);
case 's':
/*
* save input in filename.
* Check for filename, access, etc.
*/
if (!*cmdptr) {
BEEP();
break;
}
BEEP();
} else {
char outstr[PROMPTSIZE];
error("cannot open save file");
break;
}
kill_line();
"saving file %s"), cmdptr);
save_input(sf);
error("saved");
}
break;
case 'q':
case 'Q':
CHECKEND();
inwait = 0;
end_it();
/*FALLTHROUGH*/
case 'f': /* skip forward screenfuls */
CHECKEND();
if (sign == 0)
sign++; /* skips are always relative */
if (nlines == 0)
nlines++;
if (sign == 1)
else
inwait = 0;
return (0);
case 'l': /* get a line */
CHECKEND();
if (nlines == 0) {
nlines++;
if (sign == 0)
sign = 1;
}
switch (sign) {
case 1:
break;
case 0: /* leave addressed line at top */
break;
case -1:
break;
}
inwait = 0;
return (0);
case '\0': /* \n or blank */
if (nlines == 0) {
nlines++;
if (sign == 0)
sign = 1;
}
switch (sign) {
case 1:
break;
case 0:
/*
* This if statement is to fix the obscure bug
* where you have a file that has less lines
* than a screen holds, and the user types '1',
* expecting to have the 1st page (re)displayed.
* If we didn't set the new last_line to
* eofl_no-1, the screen() routine
* would cause pg to exit.
*/
break;
case -1:
break;
}
inwait = 0;
return (0);
case 'n': /* switch to next file in arglist */
CHECKEND();
if (sign == 0)
sign = 1;
if (nlines == 0)
nlines++;
BEEP();
break;
}
inwait = 0;
return (skip);
case 'p': /* switch to previous file in arglist */
CHECKEND();
if (sign == 0)
sign = 1;
if (nlines == 0)
nlines++;
BEEP();
break;
}
inwait = 0;
return (skip);
case '$': /* go to end of file */
CHECKEND();
sign = 1;
/* any large number will do */;
inwait = 0;
return (0);
case '/': /* search forward for r.e. */
case '?': /* " backwards */
case '^': /* this ones a ? for regent100s */
if (sign < 0) {
BEEP();
break;
}
if (nlines == 0)
nlines++;
cmdptr--;
wc_e1 = -1;
wc_e = -1;
wc_e = *p;
len = 1;
}
}
((wc_e == L't') ||
leave_search = wc_e;
cmdend--;
}
}
*cmdend = '\0';
break;
else {
inwait = 0;
return (0);
}
case '!': /* shell escape */
if (rmode) { /* restricted mode */
"!command not allowed in restricted mode.\n"));
break;
}
if (!hard_copy) { /* redisplay the command */
}
error("cannot fork, try again later");
break;
}
/*
* if stdin is a pipe, need to close it so
* that the terminal is really stdin for
* the command
*/
(void) perror("exec");
exit(1);
}
if (catch_susp)
{
break;
else
errno = 0;
}
if (catch_susp)
break;
default:
BEEP();
break;
}
}
}
static int
number()
{
int i;
char *p;
i = 0;
sign = 0;
p = cmdptr;
BLANKS(p);
if (*p == '+') {
p++;
sign = 1;
}
else
if (*p == '-') {
p++;
sign = -1;
}
while (isdigit(*p))
i = i * 10 + *p++ - '0';
cmdptr = p;
return (i);
}
static int
ttyin()
{
char *sptr, *p;
int slash = 0;
int state = 0;
char multic[MB_LEN_MAX];
static int readch();
int len;
(void) fixterm();
/* initialize state processing */
while (state != 10) {
BEEP();
continue;
}
length = 0;
break;
p = cmdbuf;
while (p < sptr) {
oldp = p;
if (len <= 0) {
wchar = (unsigned char)*p;
len = 1;
}
p += len;
}
/* ascii control character */
width = 2;
while (width--)
}
continue;
}
else
if (hard_copy)
(void) resetterm();
return (1);
}
if (ch < ' ')
width = 2;
else
width = 0;
if (slash) {
slash = 0;
sptr--;
promptlen--;
} else /* is there room to keep this character? */
BEEP();
continue;
}
else
if (ch == '\\')
slash++;
BEEP();
continue;
}
if (ch < ' ') {
ch += 0100;
multic[0] = '^';
length = 2;
}
p = multic;
while (length--)
(void) putchar(*p++);
}
*sptr = '\0';
kill_line();
(void) resetterm();
return (0);
}
static int
int *pstate;
wchar_t c;
char *pc;
{
static char *psign;
static char *pnumber;
static char *pcommand;
static int slash;
if (*pstate == 0) {
*pstate = 1;
slash = 0;
return (1);
}
if (c == '\\' && !slash) {
slash++;
return (1);
}
switch (*pstate) {
case 4:
return (1);
/*FALLTHROUGH*/
case 3:
*pstate = 3;
return (1);
}
/*FALLTHROUGH*/
case 2:
*pstate = 2;
return (1);
}
/*FALLTHROUGH*/
case 1:
*pstate = 1;
return (1);
}
slash = 0;
switch (*pstate) {
case 1: /* before recieving anything interesting */
return (1);
if (c == '+' || c == '-') {
*pstate = 2;
return (1);
}
/*FALLTHROUGH*/
case 2: /* recieved sign, waiting for digit */
*pstate = 3;
return (1);
}
/*FALLTHROUGH*/
case 3: /* recieved digit, waiting for the rest of the number */
return (1);
if (nflag)
*pstate = 10;
else
*pstate = 4;
return (1);
}
*pstate = 4;
return (1);
}
return (0);
case 4:
return (1);
}
return (0);
}
static int
readch()
{
}
static void
help()
{
if (clropt)
doclear();
"-------------------------------------------------------\n"
" h help\n"
" q or Q quit\n"
" <blank> or <newline> next page\n"
" l next line\n"
" d or <^D> display half a page more\n"
" . or <^L> redisplay current page\n"
" f skip the next page forward\n"
" n next file\n"
" p previous file\n"
" $ last page\n"
" w or z set window size and display next page\n"
" s savefile save current file in savefile\n"
" /pattern/ search forward for pattern\n"
" ?pattern? or\n"
" ^pattern^ search backward for pattern\n"
" !command execute command\n"
"\n"
"Most commands can be preceeded by a number, as in:\n"
"+1<newline> (next page); -1<newline> (previous page); 1<newline> (page 1).\n"
"\n"
"See the manual page for more detail.\n"
"-------------------------------------------------------\n"),
stdout);
}
/*
* Skip nskip files in the file list (from the command line). Nskip may be
* negative.
*/
static int
int nskip;
{
if (nskip == 0)
error("No previous file");
}
else
if (nskip == 0)
error("No next file");
}
return (nskip);
}
/*
* Check whether the file named by fs is a file which the user may
* access. If it is, return the opened file. Otherwise return NULL.
*/
static FILE *
char *fs;
{
FILE *f;
int fd;
int f_was_opened;
pipe_in = 0;
f = stdin;
else {
f = tmp_fin;
}
f_was_opened = 0;
} else {
}
f_was_opened = 1;
}
if (f_was_opened)
(void) fclose(f);
}
if (f_was_opened)
(void) fclose(f);
}
if (f == stdin) /* It may have been read from */
rewind(f); /* already, and not reopened */
} else {
if (f != stdin) {
if (f_was_opened)
(void) fclose(f);
"special files only handled as standard input\n"));
} else {
}
}
}
pipe_in = 1;
}
}
return (f);
}
static void
{
int c;
}
static void
re_error(i)
int i;
{
int j;
static struct messages {
char *message;
int number;
} re_errmsg[] = {
"Pattern not found", 1,
"Range endpoint too large", 11,
"Bad number", 16,
"`\\digit' out of range", 25,
"No remembered search string", 41,
"\\( \\) imbalance", 42,
"Too many \\(", 43,
"More than two numbers given in \\{ \\}", 44,
"} expected after \\", 45,
"First number exceeds second in \\{ \\}", 46,
"[] imbalance", 49,
"Regular expression overflow", 50,
"Illegal byte sequence", 67,
"Bad regular expression", 0
};
break;
}
/*
* Search for nth ocurrence of regular expression contained in buf in the file
* negative n implies backward search
* n 'guaranteed' non-zero
*/
static int
char buf[];
off_t n;
{
int direction;
static char *expbuf;
char *nexpbuf;
int END_COND;
if (regerrno) {
} else {
if (expbuf)
}
if (n < 0) { /* search back */
direction = -1;
} else {
direction = 1;
}
if (brk_hit)
break;
if ((n -= direction) == 0) {
switch (leave_search) {
case 't':
- 1;
break;
case 'b':
+ 1;
break;
case 'm':
- 1;
break;
}
return (1);
}
}
}
BEEP();
return (0);
}
/*
* find -- find line in file f, subject to certain constraints.
*
* This is the reason for all the funny stuff with sign and nlines.
* We need to be able to differentiate between relative and abosolute
* address specifications.
*
* So...there are basically three cases that this routine
* handles. Either line is zero, which means there is to be
* no motion (because line numbers start at one), or
* how and line specify a number, or line itself is negative,
* which is the same as having how == -1 and line == abs(line).
*
* Then, figure where exactly it is that we are going (an absolute
* line number). Find out if it is within what we have read,
* if so, go there without further ado. Otherwise, do some
* magic to get there, saving all the intervening lines,
* in case the user wants to see them some time later.
*
* In any case, return the line number that we end up at.
* (This is used by search() and screen()). If we go past EOF,
* return EOF.
* This EOF will go away eventually, as pg is expanded to
* handle multiple files as one huge one. Then EOF will
* mean we have run off the file list.
* If the requested line number is too far back, return BOF.
*/
static off_t
int how;
{
/* no compacted memory yet */
if (how == 0)
else
else
/* now, where is either at, before, or after dol */
/* most likely case is after, so do it first */
eoflag = 0;
if (doliseof) {
eoflag++;
return (EOF);
}
if (pipe_in)
else
dot++;
newdol(f);
break;
}
else { /* EOF */
eoflag++;
doliseof++;
return (EOF);
}
} else { /* where < dol->l_no */
if (pipe_in) {
}
return (BOF);
} else {
}
}
}
static int (*rdchar)();
static int
mrdchar()
{
}
/*
* Get a logical line
*/
static off_t
getline(f)
FILE *f;
{
char *p;
int column;
static char multic[MB_LEN_MAX];
static int savlength;
wchar_t c;
else
fileptr = f;
/* copy overlap from previous call to getline */
if (savlength)
if ((c = mrdchar()) <= 0) {
clearerr(f);
if (p > Line) { /* last line doesn't have '\n', */
*p++ = '\n';
*p = '\0'; /* print it any way */
return (column);
}
return (EOF);
}
if (length < 0) {
c = 0;
}
width = 0;
break;
break;
p += length;
/* don't have any overlap here */
length = 0;
switch (c) {
case '\t': /* just a guess */
break;
case '\b':
if (column > 0)
column--;
break;
case '\r':
column = 0;
break;
}
if (c == '\n')
break;
break;
}
if (c != '\n') { /* We're stopping in the middle of the line */
*p++ = '\n'; /* for the display */
/* save overlap for next call to getline */
if (savlength == 0) {
/*
* check if following byte is newline and get
* it if it is
*/
c = fgetwc(f);
if (c == '\n') {
/* gobble and copy (if necessary) newline */
(void) ungetwc(c, f);
(void) (*rdchar)(f);
} else if (c == EOF)
clearerr(f);
else
(void) ungetwc(c, f);
}
} else
savlength = 0;
*p = 0;
return (column);
}
static void
save_input(f)
FILE *f;
{
if (pipe_in) {
save_pipe();
pipe_in = 0;
}
}
static void
{
if (!doliseof)
if (brk_hit) {
brk_hit = 0;
error("Piped input only partially saved");
break;
}
}
static int
fgetputc(f) /* copy anything read from a pipe to tmp_fou */
FILE *f;
{
int c;
return (c);
}
static void
int how;
{
nlall = 128;
}
eoflag = 0;
doliseof = 0;
eofl_no = -1;
} else {
eoflag = 1;
doliseof = 1;
eofl_no = 1;
}
}
static void
newdol(f) /* add address of new 'dol' */
/* assumes that f is currently at beginning of said line */
/* updates dol */
FILE *f;
{
int diff;
nlall += 512;
compact();
}
}
dol++;
if (!pipe_in)
else {
}
}
static void
compact()
{
(void) perror("realloc");
end_it();
}
static void
terminit() /* set up terminal dependencies from termlib */
{
int err_ret;
for (;;) {
break;
}
(void) perror("open");
exit(1);
}
termflg = 1;
(void) saveterm();
(void) resetterm();
hard_copy = 1;
lines = 24;
}
if (columns <= 0)
columns = 80;
if (clropt && !clear_screen)
clropt = 0;
}
static void
char *mess;
{
kill_line();
errors++;
}
static void
char *filename;
{
int pagenum;
/*
* TRANSLATION_NOTE
* %s is a filename.
*/
} else {
> 999999)
pagenum = 999999;
}
}
/*
* sopr puts out the message (please no \n's) surrounded by standout
* begins and ends
*/
static void
char *m;
int count;
{
int len, n;
char *p;
if (count) {
p = m;
for (; *p; p += len) {
len = 1;
continue;
}
promptlen += n;
}
}
(void) putp(enter_standout_mode);
(void) putp(exit_standout_mode);
}
else
}
static void
doclear()
{
if (clear_screen)
(void) putp(clear_screen);
/* count in case it is trying to expand tabs */
}
static void
{
erase_line(0);
}
/* erase from after col to end of prompt */
static void
int col;
{
if (promptlen == 0)
return;
if (hard_copy)
(void) putchar('\n');
else {
if (col == 0)
(void) putchar('\r');
if (clr_eol) {
/* for the terminal driver again */
(void) putchar('\r');
}
else
(void) putchar(' ');
}
promptlen = 0;
}
/*
* Come here if a quit or interrupt signal is received
*/
static void
int sno; /* signal number generated */
{
if (!inwait) {
BEEP();
brk_hit = 1;
} else {
brk_hit = 0;
}
}
/*
* Clean up terminal state and exit.
*/
void
end_it()
{
if (out_is_tty) {
kill_line();
(void) resetterm();
if (termflg)
}
if (tmp_fin)
if (tmp_fou)
}
void
onsusp()
{
int ttou_is_dfl;
/* ignore SIGTTOU so following resetterm and flush works */
(void) resetterm();
if (ttou_is_dfl)
/* send SIGTSTP to stop this process group */
/* continued - reset the terminal */
#ifdef __STDC__
#else
#endif
(void) resetterm();
if (inwait)
}
static char *
char *str;
wchar_t c;
{
while (*str) {
if (c == *str)
return (str);
str++;
}
return (0);
}
void
usage()
{
"Usage: pg [-number] [-p string] [-cefnrs] [+line] [+/pattern/] files\n"));
exit(1);
}