xref: /illumos-gate/usr/src/cmd/more/more.c (revision e24b44e5)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ca3f9453Sas  * Common Development and Distribution License (the "License").
6ca3f9453Sas  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2123a1cceaSRoger A. Faulkner 
227c478bd9Sstevel@tonic-gate /*
2323a1cceaSRoger A. Faulkner  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*	Copyright (c) 1987, 1988 Microsoft Corporation	*/
307c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
347c478bd9Sstevel@tonic-gate  * The Regents of the University of California
357c478bd9Sstevel@tonic-gate  * All Rights Reserved
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
387c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
397c478bd9Sstevel@tonic-gate  * contributors.
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate  *      @(#) more.c 1.1 88/03/29 more:more.c
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate ** more.c - General purpose tty output filter and file perusal program
487c478bd9Sstevel@tonic-gate **
497c478bd9Sstevel@tonic-gate **      by Eric Shienbrood, UC Berkeley
507c478bd9Sstevel@tonic-gate **
517c478bd9Sstevel@tonic-gate **      modified by Geoff Peck, UCB to add underlining, single spacing
527c478bd9Sstevel@tonic-gate **      modified by John Foderaro, UCB to add -c and MORE environment variable
537c478bd9Sstevel@tonic-gate **      modified by Hans Spiller, Microsoft to handle \r better July 23, 82
547c478bd9Sstevel@tonic-gate **              added ? help command, and -w
557c478bd9Sstevel@tonic-gate **
567c478bd9Sstevel@tonic-gate **      vwh     11 Jan 83       M001
577c478bd9Sstevel@tonic-gate **              modified to handle x.out magic number and magic number
587c478bd9Sstevel@tonic-gate **              byte ordering OTHER than the vax and pdp11.
597c478bd9Sstevel@tonic-gate **      JJD     19 Jan 83       M002
607c478bd9Sstevel@tonic-gate **              - fix distributed on USENET
617c478bd9Sstevel@tonic-gate **                From decvax!ucbvax!dist2 Sun Dec  6 02:58:31 1981
627c478bd9Sstevel@tonic-gate **                Subject: FIXED:  bug in src/more/more.c
637c478bd9Sstevel@tonic-gate **              - fixed bug on terminal with "magic cookie" standout
647c478bd9Sstevel@tonic-gate **                sequences.
657c478bd9Sstevel@tonic-gate **      JJD     14 Feb 83       M003
667c478bd9Sstevel@tonic-gate **              - fix exit status of more
677c478bd9Sstevel@tonic-gate **              - Made first letter of "no more" message uppercase
687c478bd9Sstevel@tonic-gate **      andyp   03 Aug 83       M004    3.0 upgrade
697c478bd9Sstevel@tonic-gate **      - moved <local/uparm.h> to cmd/include.
707c478bd9Sstevel@tonic-gate **      - use UCB, rather than XENIX, stty(2).
717c478bd9Sstevel@tonic-gate **      andyp   30 Nov 83       M005
727c478bd9Sstevel@tonic-gate **      - (thanks to reubenb).  Changed frame variable to static, it is
737c478bd9Sstevel@tonic-gate **        used as a global buffer.  We never saw the bug before because
747c478bd9Sstevel@tonic-gate **        of the depth of the stack.
757c478bd9Sstevel@tonic-gate **      barrys  03 Jul 84       M006
767c478bd9Sstevel@tonic-gate **      - Updated the usage message to include the 's' and 'w' options
777c478bd9Sstevel@tonic-gate **        and to make the 'n' option a separate entry (uncommented).
787c478bd9Sstevel@tonic-gate **      ericc   26 Dec 84       M007
797c478bd9Sstevel@tonic-gate **      - Replaced the constant 0x7fffffffffffffffL with MAXLONG.
807c478bd9Sstevel@tonic-gate **      ericc   25 Jul 85       M008
817c478bd9Sstevel@tonic-gate **      - made "-r" option display control characters as '^x', as documented.
827c478bd9Sstevel@tonic-gate **      - fixed processing of '\b' so that more doesn't terminate when
837c478bd9Sstevel@tonic-gate **        the sequence "\b\n" is encountered.
847c478bd9Sstevel@tonic-gate **      - changed "Hit Rubout ..." to "Hit Del ...", for ibm keyboards.
857c478bd9Sstevel@tonic-gate **	davidby 9 March 1988	Unmarked
867c478bd9Sstevel@tonic-gate **	- replaced all locally defined functions with library equivalents,
877c478bd9Sstevel@tonic-gate **	- changed from termcap to terminfo
887c478bd9Sstevel@tonic-gate **	- included <values.h> for MAXLONG value
897c478bd9Sstevel@tonic-gate **	- removed most ifdef code for V6, V7, and BSD
907c478bd9Sstevel@tonic-gate **	- added /etc/magic support for file type checking
917c478bd9Sstevel@tonic-gate */
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate #include <ctype.h>
947c478bd9Sstevel@tonic-gate #include <signal.h>
957c478bd9Sstevel@tonic-gate #include <errno.h>
967c478bd9Sstevel@tonic-gate #include <sys/types.h>
977c478bd9Sstevel@tonic-gate #include <sys/wait.h>
987c478bd9Sstevel@tonic-gate #include <curses.h>
997c478bd9Sstevel@tonic-gate #include <term.h>
1007c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
1017c478bd9Sstevel@tonic-gate #include <setjmp.h>
1027c478bd9Sstevel@tonic-gate #include <sys/stat.h>
1037c478bd9Sstevel@tonic-gate #include <values.h>
1047c478bd9Sstevel@tonic-gate #include <stdlib.h>
1057c478bd9Sstevel@tonic-gate #include <stdarg.h>
1067c478bd9Sstevel@tonic-gate #include <string.h>
1077c478bd9Sstevel@tonic-gate #include <unistd.h>
1087c478bd9Sstevel@tonic-gate #include <libgen.h>
1097c478bd9Sstevel@tonic-gate #include <euc.h>
1107c478bd9Sstevel@tonic-gate #include <getwidth.h>
1117c478bd9Sstevel@tonic-gate #include <locale.h>
1127c478bd9Sstevel@tonic-gate #include <widec.h>
1137c478bd9Sstevel@tonic-gate #include <wctype.h>
1147c478bd9Sstevel@tonic-gate #include <limits.h>
1157c478bd9Sstevel@tonic-gate eucwidth_t wp;
1167c478bd9Sstevel@tonic-gate int     cw[4];
1177c478bd9Sstevel@tonic-gate int     scw[4];
1187c478bd9Sstevel@tonic-gate #include <locale.h>
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate /* Help file will eventually go in libpath(more.help) on all systems */
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate #ifdef INGRES
1237c478bd9Sstevel@tonic-gate #define VI              "/usr/bin/vi"
1247c478bd9Sstevel@tonic-gate #define HELPFILE        "/mntp/doucette/more/more.help"
1257c478bd9Sstevel@tonic-gate #define LOCAL_HELP	"/usr/lib/locale/%s/LC_MESSAGES/more.help"
1267c478bd9Sstevel@tonic-gate #endif
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate #ifndef INGRES
1297c478bd9Sstevel@tonic-gate #ifndef HELPFILE
1307c478bd9Sstevel@tonic-gate #define HELPFILE        "/usr/lib/more.help"
1317c478bd9Sstevel@tonic-gate #define LOCAL_HELP	"/usr/lib/locale/%s/LC_MESSAGES/more.help"
1327c478bd9Sstevel@tonic-gate #endif
1337c478bd9Sstevel@tonic-gate #define VI              "vi"
1347c478bd9Sstevel@tonic-gate #endif
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate #define Fopen(s,m)      (Currline = 0,file_pos=0,fopen(s,m))
1377c478bd9Sstevel@tonic-gate #define Ftell(f)        file_pos
1387c478bd9Sstevel@tonic-gate #define Fseek(f,off)    (file_pos=off,fseeko(f,off,0))
1397c478bd9Sstevel@tonic-gate #define Getc(f)         (++file_pos, getc(f))
1407c478bd9Sstevel@tonic-gate #define Ungetc(c,f)     (--file_pos, ungetc(c,f))
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate #define pr(s1)		fputs(s1, stdout)
1437c478bd9Sstevel@tonic-gate #define clreos()	putp(clr_eos)
1447c478bd9Sstevel@tonic-gate #define cleareol()	putp(clr_eol)
1457c478bd9Sstevel@tonic-gate #define home()		putp(cursor_home)
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate #define LINSIZ  512
1487c478bd9Sstevel@tonic-gate #define ctrl(letter)    ((letter) & 077)
1497c478bd9Sstevel@tonic-gate #define RUBOUT  '\177'
1507c478bd9Sstevel@tonic-gate #define ESC     '\033'
1517c478bd9Sstevel@tonic-gate #define QUIT    '\034'
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate struct termio   otty;           /* old tty modes */
1547c478bd9Sstevel@tonic-gate struct termio   ntty;           /* new tty modes */
1557c478bd9Sstevel@tonic-gate off_t           file_pos, file_size;
1567c478bd9Sstevel@tonic-gate int             fnum, no_intty, no_tty;
1577c478bd9Sstevel@tonic-gate int             dum_opt;
1587c478bd9Sstevel@tonic-gate off_t           dlines;
159e608d74dSToomas Soome void end_it(int sig) __NORETURN;
1607c478bd9Sstevel@tonic-gate void onquit(int sig);
1617c478bd9Sstevel@tonic-gate void chgwinsz(int sig);
1627c478bd9Sstevel@tonic-gate #ifdef SIGTSTP
1637c478bd9Sstevel@tonic-gate void             onsusp(int sig);
1647c478bd9Sstevel@tonic-gate #endif
1657c478bd9Sstevel@tonic-gate int             nscroll = 11;   /* Number of lines scrolled by 'd' */
1667c478bd9Sstevel@tonic-gate int             fold_opt = 1;   /* Fold long lines */
1677c478bd9Sstevel@tonic-gate int             stop_opt = 1;   /* Stop after form feeds */
1687c478bd9Sstevel@tonic-gate int             ssp_opt = 0;    /* Suppress white space */
1697c478bd9Sstevel@tonic-gate int             ul_opt = 1;     /* Underline as best we can */
1707c478bd9Sstevel@tonic-gate int             cr_opt = 0;     /* show ctrl characters as '^c' */
1717c478bd9Sstevel@tonic-gate int             wait_opt = 0;   /* prompt for exit at eof */
1727c478bd9Sstevel@tonic-gate int             promptlen;
1737c478bd9Sstevel@tonic-gate off_t		Currline;       /* Line we are currently at */
1747c478bd9Sstevel@tonic-gate int             startup = 1;
1757c478bd9Sstevel@tonic-gate int             firstf = 1;
1767c478bd9Sstevel@tonic-gate int             notell = 1;
1777c478bd9Sstevel@tonic-gate int             inwait, Pause, errors;
1787c478bd9Sstevel@tonic-gate int             within; /* true if we are within a file,
1797c478bd9Sstevel@tonic-gate                         false if we are between files */
1807c478bd9Sstevel@tonic-gate int             hard, dumb, noscroll, hardtabs, clreol;
1817c478bd9Sstevel@tonic-gate int             catch_susp;     /* We should catch the SIGTSTP signal */
1827c478bd9Sstevel@tonic-gate char            **fnames;       /* The list of file names */
1837c478bd9Sstevel@tonic-gate int             nfiles;         /* Number of files left to process */
1847c478bd9Sstevel@tonic-gate char            *shell;         /* The name of the shell to use */
1857c478bd9Sstevel@tonic-gate int             shellp;         /* A previous shell command exists */
1867c478bd9Sstevel@tonic-gate char            ch;
1877c478bd9Sstevel@tonic-gate jmp_buf         restore;
1887c478bd9Sstevel@tonic-gate char            obuf[BUFSIZ];   /* stdout buffer */
1897c478bd9Sstevel@tonic-gate char            Line[LINSIZ];   /* Line buffer */
1907c478bd9Sstevel@tonic-gate int             Lpp = 24;       /* lines per page */
1917c478bd9Sstevel@tonic-gate char            *ULenter, *ULexit;      /* enter and exit underline mode */
1927c478bd9Sstevel@tonic-gate int             Mcol = 80;      /* number of columns */
1937c478bd9Sstevel@tonic-gate int             Wrap = 1;       /* set if automargins */
1947c478bd9Sstevel@tonic-gate int		fseeko();
1957c478bd9Sstevel@tonic-gate struct {
1967c478bd9Sstevel@tonic-gate     off_t chrctr, line;
1977c478bd9Sstevel@tonic-gate } context, screen_start;
1987c478bd9Sstevel@tonic-gate int             exitstat = 0;   /* status to use when exiting more */   /*M003*/
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate static void execute(char *filename, char *cmd, ...);
2017c478bd9Sstevel@tonic-gate static void error(char *mess);
2027c478bd9Sstevel@tonic-gate static void wait_eof(void);
2037c478bd9Sstevel@tonic-gate static void prompt(char *filename);
2047c478bd9Sstevel@tonic-gate static void argscan(char *s);
2057c478bd9Sstevel@tonic-gate static void copy_file(register FILE *f);
2067c478bd9Sstevel@tonic-gate static void initterm(void);
2077c478bd9Sstevel@tonic-gate static void do_shell(char *filename);
2087c478bd9Sstevel@tonic-gate static FILE *checkf(register char *fs, int *clearfirst);
2097c478bd9Sstevel@tonic-gate static void screen(register FILE *f, register off_t num_lines);
2107c478bd9Sstevel@tonic-gate static void skiplns(register off_t n, register FILE *f);
2117c478bd9Sstevel@tonic-gate static void skipf(register int nskip);
2127c478bd9Sstevel@tonic-gate static int readch(void);
2137c478bd9Sstevel@tonic-gate static void prmpt_erase(register int col);
2147c478bd9Sstevel@tonic-gate static void kill_line(void);
2157c478bd9Sstevel@tonic-gate static void prbuf(register char *s, register int n);
2167c478bd9Sstevel@tonic-gate static void search(char buf[], FILE *file, register off_t n);
2177c478bd9Sstevel@tonic-gate static void doclear(void);
2187c478bd9Sstevel@tonic-gate static void ttyin(char buf[], register int nmax, char pchar);
2197c478bd9Sstevel@tonic-gate static int expand(char *outbuf, char *inbuf);
2207c478bd9Sstevel@tonic-gate static void show(register char ch);
2217c478bd9Sstevel@tonic-gate static void set_tty(void);
2227c478bd9Sstevel@tonic-gate static void reset_tty(void);
2237c478bd9Sstevel@tonic-gate static void rdline(register FILE *f);
2247c478bd9Sstevel@tonic-gate static off_t command(char *filename, register FILE *f);
22523a1cceaSRoger A. Faulkner static int getaline(register FILE *f, int *length);
2267c478bd9Sstevel@tonic-gate static int number(char *cmd);
2277c478bd9Sstevel@tonic-gate static int colon(char *filename, int cmd, off_t nlines);
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[])2307c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate     register FILE       *f;
2337c478bd9Sstevel@tonic-gate     register char       *s;
2347c478bd9Sstevel@tonic-gate     register char       *p;
2357c478bd9Sstevel@tonic-gate     register int	ch;
2367c478bd9Sstevel@tonic-gate     register off_t      left;
2377c478bd9Sstevel@tonic-gate     int                 prnames = 0;
2387c478bd9Sstevel@tonic-gate     int                 initopt = 0;
2397c478bd9Sstevel@tonic-gate     int                 srchopt = 0;
2407c478bd9Sstevel@tonic-gate     int                 clearit = 0;
2417c478bd9Sstevel@tonic-gate     off_t               initline;
2427c478bd9Sstevel@tonic-gate     char                initbuf[80];
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate     setlocale( LC_ALL, "" );
2457c478bd9Sstevel@tonic-gate     getwidth(&wp);
2467c478bd9Sstevel@tonic-gate     cw[0] = 1;
2477c478bd9Sstevel@tonic-gate     cw[1] = wp._eucw1;
2487c478bd9Sstevel@tonic-gate     cw[2] = wp._eucw2+1;
2497c478bd9Sstevel@tonic-gate     cw[3] = wp._eucw3+1;
2507c478bd9Sstevel@tonic-gate     scw[0] = 1;
2517c478bd9Sstevel@tonic-gate     scw[1] = wp._scrw1;
2527c478bd9Sstevel@tonic-gate     scw[2] = wp._scrw2;
2537c478bd9Sstevel@tonic-gate     scw[3] = wp._scrw3;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate     nfiles = argc;
2567c478bd9Sstevel@tonic-gate     fnames = argv;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate     (void) setlocale(LC_ALL,"");
2597c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
2607c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
2617c478bd9Sstevel@tonic-gate #endif
2627c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate     initterm ();
2657c478bd9Sstevel@tonic-gate     if(s = getenv("MORE")) argscan(s);
2667c478bd9Sstevel@tonic-gate     while (--nfiles > 0) {
2677c478bd9Sstevel@tonic-gate         if ((ch = (*++fnames)[0]) == '-') {
2687c478bd9Sstevel@tonic-gate             argscan(*fnames+1);
2697c478bd9Sstevel@tonic-gate         }
2707c478bd9Sstevel@tonic-gate         else if (ch == '+') {
2717c478bd9Sstevel@tonic-gate             s = *fnames;
2727c478bd9Sstevel@tonic-gate             if (*++s == '/') {
2737c478bd9Sstevel@tonic-gate                 srchopt++;
2747c478bd9Sstevel@tonic-gate                 for (++s, p = initbuf; p < initbuf + 79 && *s != '\0';)
2757c478bd9Sstevel@tonic-gate                     *p++ = *s++;
2767c478bd9Sstevel@tonic-gate                 *p = '\0';
2777c478bd9Sstevel@tonic-gate             }
2787c478bd9Sstevel@tonic-gate             else {
2797c478bd9Sstevel@tonic-gate                 initopt++;
2807c478bd9Sstevel@tonic-gate                 for (initline = 0; *s != '\0'; s++)
2817c478bd9Sstevel@tonic-gate                     if (isdigit (*s))
2827c478bd9Sstevel@tonic-gate                         initline = initline*10 + *s -'0';
2837c478bd9Sstevel@tonic-gate                 --initline;
2847c478bd9Sstevel@tonic-gate             }
2857c478bd9Sstevel@tonic-gate         }
2867c478bd9Sstevel@tonic-gate         else break;
2877c478bd9Sstevel@tonic-gate     }
2887c478bd9Sstevel@tonic-gate     /* allow clreol only if cursor_home and clr_eol and clr_eos strings are
2897c478bd9Sstevel@tonic-gate      *  defined, and in that case, make sure we are in noscroll mode
2907c478bd9Sstevel@tonic-gate      */
2917c478bd9Sstevel@tonic-gate     if(clreol)
2927c478bd9Sstevel@tonic-gate     {
2937c478bd9Sstevel@tonic-gate         if (!cursor_home || !clr_eol || !clr_eos) {
2947c478bd9Sstevel@tonic-gate            	clreol = 0;
2957c478bd9Sstevel@tonic-gate 	}
2967c478bd9Sstevel@tonic-gate         else noscroll = 1;
2977c478bd9Sstevel@tonic-gate     }
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate     if (dlines == 0)
3007c478bd9Sstevel@tonic-gate         dlines =(off_t) (Lpp - (noscroll ? 1 : 2));
3017c478bd9Sstevel@tonic-gate     left = dlines;
3027c478bd9Sstevel@tonic-gate     if (nfiles > 1)
3037c478bd9Sstevel@tonic-gate         prnames++;
3047c478bd9Sstevel@tonic-gate     if (!no_intty && nfiles == 0) {
3057c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext("Usage: %s\
3067c478bd9Sstevel@tonic-gate  [-cdflrsuw] [-lines] [+linenumber] [+/pattern] [filename ...].\n")
3077c478bd9Sstevel@tonic-gate 	, argv[0]);
3087c478bd9Sstevel@tonic-gate         exit(1);
3097c478bd9Sstevel@tonic-gate     }
3107c478bd9Sstevel@tonic-gate     else
3117c478bd9Sstevel@tonic-gate         f = stdin;
3127c478bd9Sstevel@tonic-gate     if (!no_tty) {
3137c478bd9Sstevel@tonic-gate         signal(SIGQUIT, onquit);
3147c478bd9Sstevel@tonic-gate         signal(SIGINT, end_it);
3157c478bd9Sstevel@tonic-gate         signal(SIGWINCH, chgwinsz);
3167c478bd9Sstevel@tonic-gate #ifdef SIGTSTP
3177c478bd9Sstevel@tonic-gate         if (signal (SIGTSTP, SIG_IGN) == SIG_DFL) {
3187c478bd9Sstevel@tonic-gate             signal(SIGTSTP, onsusp);
3197c478bd9Sstevel@tonic-gate             catch_susp++;
3207c478bd9Sstevel@tonic-gate         }
3217c478bd9Sstevel@tonic-gate #endif
3227c478bd9Sstevel@tonic-gate         set_tty();
3237c478bd9Sstevel@tonic-gate     }
3247c478bd9Sstevel@tonic-gate     if (no_intty) {
3257c478bd9Sstevel@tonic-gate         if (no_tty)
3267c478bd9Sstevel@tonic-gate             copy_file (stdin);
3277c478bd9Sstevel@tonic-gate         else {
3287c478bd9Sstevel@tonic-gate             if ((ch = Getc (f)) == '\f')
3297c478bd9Sstevel@tonic-gate                 doclear();
3307c478bd9Sstevel@tonic-gate             else {
3317c478bd9Sstevel@tonic-gate                 Ungetc (ch, f);
3327c478bd9Sstevel@tonic-gate                 if (noscroll && (ch != EOF)) {
3337c478bd9Sstevel@tonic-gate                     if (clreol)
3347c478bd9Sstevel@tonic-gate                         home ();
3357c478bd9Sstevel@tonic-gate                     else
3367c478bd9Sstevel@tonic-gate                         doclear ();
3377c478bd9Sstevel@tonic-gate                 }
3387c478bd9Sstevel@tonic-gate             }
3397c478bd9Sstevel@tonic-gate             if (!setjmp(restore)) {
3407c478bd9Sstevel@tonic-gate                 if (srchopt) {
3417c478bd9Sstevel@tonic-gate                     search (initbuf, stdin,(off_t) 1);
3427c478bd9Sstevel@tonic-gate                     if (noscroll)
3437c478bd9Sstevel@tonic-gate                         left--;
3447c478bd9Sstevel@tonic-gate                     }
3457c478bd9Sstevel@tonic-gate                 else if (initopt)
3467c478bd9Sstevel@tonic-gate                     skiplns (initline, stdin);
3477c478bd9Sstevel@tonic-gate                 }
3487c478bd9Sstevel@tonic-gate             else
3497c478bd9Sstevel@tonic-gate                 left = command(NULL, f);
3507c478bd9Sstevel@tonic-gate             screen (stdin, left);
3517c478bd9Sstevel@tonic-gate         }
3527c478bd9Sstevel@tonic-gate         no_intty = 0;
3537c478bd9Sstevel@tonic-gate         prnames++;
3547c478bd9Sstevel@tonic-gate         firstf = 0;
3557c478bd9Sstevel@tonic-gate     }
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate     while (fnum < nfiles) {
3587c478bd9Sstevel@tonic-gate         if ((f = checkf (fnames[fnum], &clearit)) != NULL) {
3597c478bd9Sstevel@tonic-gate             context.line = context.chrctr = 0;
3607c478bd9Sstevel@tonic-gate             Currline = 0;
3617c478bd9Sstevel@tonic-gate             if (firstf) setjmp (restore);
3627c478bd9Sstevel@tonic-gate             if (firstf) {
3637c478bd9Sstevel@tonic-gate                 firstf = 0;
3647c478bd9Sstevel@tonic-gate                 if (srchopt)
3657c478bd9Sstevel@tonic-gate                 {
3667c478bd9Sstevel@tonic-gate                     search (initbuf, f,(off_t) 1);
3677c478bd9Sstevel@tonic-gate                     if (noscroll)
3687c478bd9Sstevel@tonic-gate                         left--;
3697c478bd9Sstevel@tonic-gate                 }
3707c478bd9Sstevel@tonic-gate                 else if (initopt)
3717c478bd9Sstevel@tonic-gate                     skiplns (initline, f);
3727c478bd9Sstevel@tonic-gate             }
3737c478bd9Sstevel@tonic-gate             else if (fnum < nfiles && !no_tty) {
3747c478bd9Sstevel@tonic-gate                 setjmp (restore);
3757c478bd9Sstevel@tonic-gate                 left = command (fnames[fnum], f);
3767c478bd9Sstevel@tonic-gate             }
3777c478bd9Sstevel@tonic-gate             if (left != 0) {
3787c478bd9Sstevel@tonic-gate                 if ((noscroll || clearit) && (file_size != LLONG_MAX))
3797c478bd9Sstevel@tonic-gate                     if (clreol)
3807c478bd9Sstevel@tonic-gate                         home ();
3817c478bd9Sstevel@tonic-gate                     else
3827c478bd9Sstevel@tonic-gate                         doclear ();
3837c478bd9Sstevel@tonic-gate                 if (prnames) {
3847c478bd9Sstevel@tonic-gate                     if (ceol_standout_glitch)
3857c478bd9Sstevel@tonic-gate                         prmpt_erase (0);
3867c478bd9Sstevel@tonic-gate                     if (clreol)
3877c478bd9Sstevel@tonic-gate                         cleareol ();
3887c478bd9Sstevel@tonic-gate                     pr("::::::::::::::");
3897c478bd9Sstevel@tonic-gate                     if (promptlen > 14)
3907c478bd9Sstevel@tonic-gate                         prmpt_erase (14);
3917c478bd9Sstevel@tonic-gate                     printf ("\n");
3927c478bd9Sstevel@tonic-gate                     if(clreol) cleareol();
3937c478bd9Sstevel@tonic-gate                     printf("%s\n", fnames[fnum]);
3947c478bd9Sstevel@tonic-gate                     if(clreol) cleareol();
3957c478bd9Sstevel@tonic-gate                     pr("::::::::::::::\n");
3967c478bd9Sstevel@tonic-gate                     if (left > (off_t)(Lpp - 4))
3977c478bd9Sstevel@tonic-gate                         left =(off_t)(Lpp - 4);
3987c478bd9Sstevel@tonic-gate                 }
3997c478bd9Sstevel@tonic-gate                 if (no_tty)
4007c478bd9Sstevel@tonic-gate                     copy_file (f);
4017c478bd9Sstevel@tonic-gate                 else {
4027c478bd9Sstevel@tonic-gate                     within++;
4037c478bd9Sstevel@tonic-gate                     screen(f, left);
4047c478bd9Sstevel@tonic-gate                     within = 0;
4057c478bd9Sstevel@tonic-gate                 }
4067c478bd9Sstevel@tonic-gate             }
4077c478bd9Sstevel@tonic-gate             setjmp (restore);
4087c478bd9Sstevel@tonic-gate             fflush(stdout);
4097c478bd9Sstevel@tonic-gate             fclose(f);
4107c478bd9Sstevel@tonic-gate             screen_start.line = screen_start.chrctr = 0LL;
4117c478bd9Sstevel@tonic-gate             context.line = context.chrctr = 0LL;
4127c478bd9Sstevel@tonic-gate         } else
4137c478bd9Sstevel@tonic-gate             exitstat |= 1;                      /*M003*/
4147c478bd9Sstevel@tonic-gate         fnum++;
4157c478bd9Sstevel@tonic-gate         firstf = 0;
4167c478bd9Sstevel@tonic-gate     }
4177c478bd9Sstevel@tonic-gate     if (wait_opt) wait_eof();
4187c478bd9Sstevel@tonic-gate     reset_tty ();
4197c478bd9Sstevel@tonic-gate     return (exitstat);                             /*M003*/
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate static void
argscan(char * s)4237c478bd9Sstevel@tonic-gate argscan(char *s)
4247c478bd9Sstevel@tonic-gate {
4257c478bd9Sstevel@tonic-gate             for (dlines = 0; *s != '\0'; s++)
4267c478bd9Sstevel@tonic-gate                 if (isdigit(*s))
4277c478bd9Sstevel@tonic-gate                     dlines = dlines*10 + *s - '0';
4287c478bd9Sstevel@tonic-gate                 else if (*s == 'd')
4297c478bd9Sstevel@tonic-gate                     dum_opt = 1;
4307c478bd9Sstevel@tonic-gate                 else if (*s == 'l')
4317c478bd9Sstevel@tonic-gate                     stop_opt = 0;
4327c478bd9Sstevel@tonic-gate                 else if (*s == 'f')
4337c478bd9Sstevel@tonic-gate                     fold_opt = 0;
4347c478bd9Sstevel@tonic-gate                 else if (*s == 'p')
4357c478bd9Sstevel@tonic-gate                     noscroll++;
4367c478bd9Sstevel@tonic-gate                 else if (*s == 'c')
4377c478bd9Sstevel@tonic-gate                     clreol++;
4387c478bd9Sstevel@tonic-gate                 else if (*s == 's')
4397c478bd9Sstevel@tonic-gate                     ssp_opt = 1;
4407c478bd9Sstevel@tonic-gate                 else if (*s == 'u')
4417c478bd9Sstevel@tonic-gate                     ul_opt = 0;
4427c478bd9Sstevel@tonic-gate                 else if (*s == 'r')
4437c478bd9Sstevel@tonic-gate                     cr_opt = 1;
4447c478bd9Sstevel@tonic-gate                 else if (*s == 'w')
4457c478bd9Sstevel@tonic-gate                     wait_opt = 1;
4467c478bd9Sstevel@tonic-gate }
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate /*
4507c478bd9Sstevel@tonic-gate ** Check whether the file named by fs is a file which the user may
4517c478bd9Sstevel@tonic-gate ** access.  If it is, return the opened file. Otherwise return NULL.
4527c478bd9Sstevel@tonic-gate */
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate static FILE *
checkf(register char * fs,int * clearfirst)4557c478bd9Sstevel@tonic-gate checkf(register char *fs, int *clearfirst)
4567c478bd9Sstevel@tonic-gate {
4577c478bd9Sstevel@tonic-gate     struct stat stbuf;
4587c478bd9Sstevel@tonic-gate     register FILE *f;
4597c478bd9Sstevel@tonic-gate     int c;
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate     if (stat (fs, &stbuf) == -1) {
4627c478bd9Sstevel@tonic-gate         fflush(stdout);
4637c478bd9Sstevel@tonic-gate         if (clreol)
4647c478bd9Sstevel@tonic-gate             cleareol ();
4657c478bd9Sstevel@tonic-gate         perror(fs);
4667c478bd9Sstevel@tonic-gate         return (NULL);
4677c478bd9Sstevel@tonic-gate     }
4687c478bd9Sstevel@tonic-gate     if ((stbuf.st_mode & S_IFMT) == S_IFDIR) {
4697c478bd9Sstevel@tonic-gate         printf(gettext("\n*** %s: directory ***\n\n"), fs);
4707c478bd9Sstevel@tonic-gate         return (NULL);
4717c478bd9Sstevel@tonic-gate     }
4727c478bd9Sstevel@tonic-gate     if ((f=Fopen(fs, "r")) == NULL) {
4737c478bd9Sstevel@tonic-gate         fflush(stdout);
4747c478bd9Sstevel@tonic-gate         perror(fs);
4757c478bd9Sstevel@tonic-gate         return (NULL);
4767c478bd9Sstevel@tonic-gate     }
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate     if ((c = Getc(f)) == '\f')                  /* end M001 */
4797c478bd9Sstevel@tonic-gate         *clearfirst = 1;
4807c478bd9Sstevel@tonic-gate     else {
4817c478bd9Sstevel@tonic-gate         *clearfirst = 0;
4827c478bd9Sstevel@tonic-gate         Ungetc (c, f);
4837c478bd9Sstevel@tonic-gate     }
4847c478bd9Sstevel@tonic-gate     if ((file_size = (off_t)stbuf.st_size) == 0)
4857c478bd9Sstevel@tonic-gate         file_size = LLONG_MAX;
4867c478bd9Sstevel@tonic-gate     return (f);
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate /*
4907c478bd9Sstevel@tonic-gate ** Print out the contents of the file f, one screenful at a time.
4917c478bd9Sstevel@tonic-gate */
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate #define STOP -10
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate static void
screen(register FILE * f,register off_t num_lines)4967c478bd9Sstevel@tonic-gate screen(register FILE *f, register off_t num_lines)
4977c478bd9Sstevel@tonic-gate {
4987c478bd9Sstevel@tonic-gate     register int c;
4997c478bd9Sstevel@tonic-gate     register int nchars;
5007c478bd9Sstevel@tonic-gate     int length;                 /* length of current line */
5017c478bd9Sstevel@tonic-gate     static int prev_len = 1;    /* length of previous line */
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate     for (;;) {
5047c478bd9Sstevel@tonic-gate         while (num_lines > 0 && !Pause) {
50523a1cceaSRoger A. Faulkner             if ((nchars = getaline (f, &length)) == EOF)
5067c478bd9Sstevel@tonic-gate             {
5077c478bd9Sstevel@tonic-gate                 if (clreol) clreos();
5087c478bd9Sstevel@tonic-gate                 return;
5097c478bd9Sstevel@tonic-gate             }
5107c478bd9Sstevel@tonic-gate             if (ssp_opt && length == 0 && prev_len == 0)
5117c478bd9Sstevel@tonic-gate                 continue;
5127c478bd9Sstevel@tonic-gate             prev_len = length;
5137c478bd9Sstevel@tonic-gate             if (ceol_standout_glitch ||
5147c478bd9Sstevel@tonic-gate 		(enter_standout_mode && *enter_standout_mode == ' ')
5157c478bd9Sstevel@tonic-gate 		&& promptlen > 0)
5167c478bd9Sstevel@tonic-gate                 prmpt_erase (0);
5177c478bd9Sstevel@tonic-gate             /* must clear before drawing line since tabs on some terminals
5187c478bd9Sstevel@tonic-gate              * do not erase what they tab over.
5197c478bd9Sstevel@tonic-gate              */
5207c478bd9Sstevel@tonic-gate             if (clreol)
5217c478bd9Sstevel@tonic-gate                 cleareol ();
5227c478bd9Sstevel@tonic-gate             prbuf (Line, length);
5237c478bd9Sstevel@tonic-gate             if (nchars < promptlen)
5247c478bd9Sstevel@tonic-gate                 prmpt_erase (nchars); /* prmpt_erase () sets promptlen to 0 */
5257c478bd9Sstevel@tonic-gate             else promptlen = 0;
5267c478bd9Sstevel@tonic-gate             /* is this needed?
5277c478bd9Sstevel@tonic-gate              * if (clreol)
5287c478bd9Sstevel@tonic-gate              *  cleareol(); */    /* must clear again in case we wrapped */
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate             if (nchars < Mcol || !fold_opt)
5317c478bd9Sstevel@tonic-gate                 putchar('\n');
5327c478bd9Sstevel@tonic-gate             if (nchars == STOP)
5337c478bd9Sstevel@tonic-gate                 break;
5347c478bd9Sstevel@tonic-gate             num_lines--;
5357c478bd9Sstevel@tonic-gate         }
5367c478bd9Sstevel@tonic-gate         fflush(stdout);
5377c478bd9Sstevel@tonic-gate         if ((c = Getc(f)) == EOF)
5387c478bd9Sstevel@tonic-gate         {
5397c478bd9Sstevel@tonic-gate             if (clreol) clreos ();
5407c478bd9Sstevel@tonic-gate             return;
5417c478bd9Sstevel@tonic-gate         }
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate         if (Pause && clreol)
5447c478bd9Sstevel@tonic-gate             clreos ();
5457c478bd9Sstevel@tonic-gate         Ungetc (c, f);
5467c478bd9Sstevel@tonic-gate         setjmp (restore);
5477c478bd9Sstevel@tonic-gate         Pause = 0; startup = 0;
5487c478bd9Sstevel@tonic-gate         if ((num_lines = command (NULL, f)) == 0)
5497c478bd9Sstevel@tonic-gate             return;
5507c478bd9Sstevel@tonic-gate         if (hard && promptlen > 0)
5517c478bd9Sstevel@tonic-gate                 prmpt_erase (0);
5527c478bd9Sstevel@tonic-gate         if (noscroll && num_lines == dlines) {
5537c478bd9Sstevel@tonic-gate             if (clreol)
5547c478bd9Sstevel@tonic-gate                 home();
5557c478bd9Sstevel@tonic-gate             else
5567c478bd9Sstevel@tonic-gate                 doclear ();
5577c478bd9Sstevel@tonic-gate         }
5587c478bd9Sstevel@tonic-gate         screen_start.line = Currline;
5597c478bd9Sstevel@tonic-gate         screen_start.chrctr = Ftell (f);
5607c478bd9Sstevel@tonic-gate     }
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate /*
5647c478bd9Sstevel@tonic-gate ** Come here if a quit signal is received
5657c478bd9Sstevel@tonic-gate */
5667c478bd9Sstevel@tonic-gate /*
5677c478bd9Sstevel@tonic-gate  * sig is put in as a dummy arg to have the compiler not to complain
5687c478bd9Sstevel@tonic-gate  */
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate /* ARGSUSED */
5717c478bd9Sstevel@tonic-gate void
onquit(int sig)5727c478bd9Sstevel@tonic-gate onquit(int sig)
5737c478bd9Sstevel@tonic-gate {
5747c478bd9Sstevel@tonic-gate     signal(SIGQUIT, SIG_IGN);
5757c478bd9Sstevel@tonic-gate     if (!inwait) {
5767c478bd9Sstevel@tonic-gate         putchar ('\n');
5777c478bd9Sstevel@tonic-gate         if (!startup) {
5787c478bd9Sstevel@tonic-gate             signal(SIGQUIT, onquit);
5797c478bd9Sstevel@tonic-gate             longjmp (restore, 1);
5807c478bd9Sstevel@tonic-gate         }
5817c478bd9Sstevel@tonic-gate         else
5827c478bd9Sstevel@tonic-gate             Pause++;
5837c478bd9Sstevel@tonic-gate     }
5847c478bd9Sstevel@tonic-gate     else if (!dum_opt && notell) {
5857c478bd9Sstevel@tonic-gate         write (2, gettext("[Use q or Q to quit]"), 20);
5867c478bd9Sstevel@tonic-gate         promptlen += 20;
5877c478bd9Sstevel@tonic-gate         notell = 0;
5887c478bd9Sstevel@tonic-gate     }
5897c478bd9Sstevel@tonic-gate     signal(SIGQUIT, onquit);
5907c478bd9Sstevel@tonic-gate }
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate /*
5937c478bd9Sstevel@tonic-gate ** Come here if a signal for a window size change is received
5947c478bd9Sstevel@tonic-gate */
5957c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5967c478bd9Sstevel@tonic-gate void
chgwinsz(int sig)5977c478bd9Sstevel@tonic-gate chgwinsz(int sig)
5987c478bd9Sstevel@tonic-gate {
5997c478bd9Sstevel@tonic-gate     struct winsize win;
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate     (void) signal(SIGWINCH, SIG_IGN);
6027c478bd9Sstevel@tonic-gate     if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1) {
6037c478bd9Sstevel@tonic-gate 	if (win.ws_row != 0) {
6047c478bd9Sstevel@tonic-gate 	    Lpp = win.ws_row;
6057c478bd9Sstevel@tonic-gate 	    nscroll = Lpp/2 - 1;
6067c478bd9Sstevel@tonic-gate 	    if (nscroll <= 0)
6077c478bd9Sstevel@tonic-gate 		nscroll = 1;
6087c478bd9Sstevel@tonic-gate 	    dlines = (off_t)(Lpp - (noscroll ? 1 : 2));
6097c478bd9Sstevel@tonic-gate 	}
6107c478bd9Sstevel@tonic-gate 	if (win.ws_col != 0)
6117c478bd9Sstevel@tonic-gate 	    Mcol = win.ws_col;
6127c478bd9Sstevel@tonic-gate     }
6137c478bd9Sstevel@tonic-gate     (void) signal(SIGWINCH, chgwinsz);
6147c478bd9Sstevel@tonic-gate }
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate /*
6177c478bd9Sstevel@tonic-gate ** Clean up terminal state and exit. Also come here if interrupt signal received
6187c478bd9Sstevel@tonic-gate */
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate /*
6217c478bd9Sstevel@tonic-gate  * sig is put in as a dummy arg to have the compiler not to complain
6227c478bd9Sstevel@tonic-gate  */
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate /* ARGSUSED */
6257c478bd9Sstevel@tonic-gate void
end_it(int sig)6267c478bd9Sstevel@tonic-gate end_it(int sig)
6277c478bd9Sstevel@tonic-gate {
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate     reset_tty ();
6307c478bd9Sstevel@tonic-gate     if (clreol) {
6317c478bd9Sstevel@tonic-gate         putchar ('\r');
6327c478bd9Sstevel@tonic-gate         clreos ();
6337c478bd9Sstevel@tonic-gate         fflush (stdout);
6347c478bd9Sstevel@tonic-gate     }
6357c478bd9Sstevel@tonic-gate     else if (!clreol && (promptlen > 0)) {
6367c478bd9Sstevel@tonic-gate         kill_line ();
6377c478bd9Sstevel@tonic-gate         fflush (stdout);
6387c478bd9Sstevel@tonic-gate     }
6397c478bd9Sstevel@tonic-gate     else
6407c478bd9Sstevel@tonic-gate         write (2, "\n", 1);
6417c478bd9Sstevel@tonic-gate     _exit(exitstat);                    /*M003*/
6427c478bd9Sstevel@tonic-gate }
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate static void
copy_file(register FILE * f)6457c478bd9Sstevel@tonic-gate copy_file(register FILE *f)
6467c478bd9Sstevel@tonic-gate {
6477c478bd9Sstevel@tonic-gate     register int c;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate     while ((c = getc(f)) != EOF)
6507c478bd9Sstevel@tonic-gate         putchar(c);
6517c478bd9Sstevel@tonic-gate }
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate static char Bell = ctrl('G');
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 
6567c478bd9Sstevel@tonic-gate /* See whether the last component of the path name "path" is equal to the
6577c478bd9Sstevel@tonic-gate ** string "string"
6587c478bd9Sstevel@tonic-gate */
6597c478bd9Sstevel@tonic-gate 
66078eb75caSchin int
tailequ(char * path,char * string)66178eb75caSchin tailequ(char *path, char *string)
6627c478bd9Sstevel@tonic-gate {
6637c478bd9Sstevel@tonic-gate 	return (!strcmp(basename(path), string));
6647c478bd9Sstevel@tonic-gate }
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate static void
prompt(char * filename)6677c478bd9Sstevel@tonic-gate prompt(char *filename)
6687c478bd9Sstevel@tonic-gate {
6697c478bd9Sstevel@tonic-gate     if (clreol)
6707c478bd9Sstevel@tonic-gate         cleareol ();
6717c478bd9Sstevel@tonic-gate     else if (promptlen > 0)
6727c478bd9Sstevel@tonic-gate         kill_line ();
6737c478bd9Sstevel@tonic-gate     if (!hard) {
6747c478bd9Sstevel@tonic-gate         promptlen = 8;
6757c478bd9Sstevel@tonic-gate         if (enter_standout_mode && exit_standout_mode)
6767c478bd9Sstevel@tonic-gate             putp (enter_standout_mode);
6777c478bd9Sstevel@tonic-gate         if (clreol)
6787c478bd9Sstevel@tonic-gate             cleareol ();
6797c478bd9Sstevel@tonic-gate         pr(gettext("--More--"));
6807c478bd9Sstevel@tonic-gate         if (filename != NULL) {
6817c478bd9Sstevel@tonic-gate             promptlen += printf (gettext("(Next file: %s)"), filename);
6827c478bd9Sstevel@tonic-gate         }
6837c478bd9Sstevel@tonic-gate         else if (!no_intty) {
6847c478bd9Sstevel@tonic-gate             promptlen += printf ("(%d%%)", (int)((file_pos * 100) / file_size));
6857c478bd9Sstevel@tonic-gate         }
6867c478bd9Sstevel@tonic-gate         if (dum_opt) {
6877c478bd9Sstevel@tonic-gate             promptlen += pr(gettext("[Hit space to continue, Del to abort]"));
6887c478bd9Sstevel@tonic-gate         }
6897c478bd9Sstevel@tonic-gate         if (enter_standout_mode && exit_standout_mode)
6907c478bd9Sstevel@tonic-gate             putp (exit_standout_mode);
6917c478bd9Sstevel@tonic-gate         if (clreol) clreos ();
6927c478bd9Sstevel@tonic-gate         fflush(stdout);
6937c478bd9Sstevel@tonic-gate     }
6947c478bd9Sstevel@tonic-gate     else
6957c478bd9Sstevel@tonic-gate         write (2, &Bell, 1);
6967c478bd9Sstevel@tonic-gate     inwait++;
6977c478bd9Sstevel@tonic-gate }
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate /*
7007c478bd9Sstevel@tonic-gate  * when run from another program or a shell script, it is
7017c478bd9Sstevel@tonic-gate  * sometimes useful to prevent the next program from scrolling
7027c478bd9Sstevel@tonic-gate  * us off the screen before we get a chance to read this page.
7037c478bd9Sstevel@tonic-gate  *                      -Hans, July 24, 1982
7047c478bd9Sstevel@tonic-gate  */
7057c478bd9Sstevel@tonic-gate static void
wait_eof(void)7067c478bd9Sstevel@tonic-gate wait_eof(void)
7077c478bd9Sstevel@tonic-gate {
7087c478bd9Sstevel@tonic-gate         if (enter_standout_mode && exit_standout_mode)
7097c478bd9Sstevel@tonic-gate                 putp (enter_standout_mode);
7107c478bd9Sstevel@tonic-gate         promptlen = pr(gettext("--No more--"));          /*M003*/
7117c478bd9Sstevel@tonic-gate         if (dum_opt)
7127c478bd9Sstevel@tonic-gate                 promptlen += pr(gettext("[Hit any key to continue]"));
7137c478bd9Sstevel@tonic-gate         if (enter_standout_mode && exit_standout_mode)
7147c478bd9Sstevel@tonic-gate                 putp(exit_standout_mode);
7157c478bd9Sstevel@tonic-gate         if (clreol) clreos();
7167c478bd9Sstevel@tonic-gate         fflush(stdout);
7177c478bd9Sstevel@tonic-gate         readch();
7187c478bd9Sstevel@tonic-gate         prmpt_erase (0);
7197c478bd9Sstevel@tonic-gate         fflush(stdout);
7207c478bd9Sstevel@tonic-gate }
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate /*
7237c478bd9Sstevel@tonic-gate ** Get a logical line
7247c478bd9Sstevel@tonic-gate */
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate static int
getaline(register FILE * f,int * length)72723a1cceaSRoger A. Faulkner getaline(register FILE *f, int *length)
7287c478bd9Sstevel@tonic-gate {
7297c478bd9Sstevel@tonic-gate     register int        c;
7307c478bd9Sstevel@tonic-gate     register char       *p;
7317c478bd9Sstevel@tonic-gate     register int        column;
7327c478bd9Sstevel@tonic-gate     static int          colflg;
7337c478bd9Sstevel@tonic-gate     register int        oldcolumn;
7347c478bd9Sstevel@tonic-gate     int			csno;
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate     p = Line;
7377c478bd9Sstevel@tonic-gate     column = 0;
7387c478bd9Sstevel@tonic-gate     oldcolumn = 0;
7397c478bd9Sstevel@tonic-gate     c = Getc (f);
7407c478bd9Sstevel@tonic-gate     if (colflg && c == '\n') {
7417c478bd9Sstevel@tonic-gate         Currline++;
7427c478bd9Sstevel@tonic-gate         c = Getc (f);
7437c478bd9Sstevel@tonic-gate     }
7447c478bd9Sstevel@tonic-gate     while (p < &Line[LINSIZ - 1]) {
7457c478bd9Sstevel@tonic-gate 	csno = csetno(c);
7467c478bd9Sstevel@tonic-gate         if (c == EOF) {
7477c478bd9Sstevel@tonic-gate             if (p > Line) {
7487c478bd9Sstevel@tonic-gate                 *p = '\0';
7497c478bd9Sstevel@tonic-gate                 *length = p - Line;
7507c478bd9Sstevel@tonic-gate                 return (column);
7517c478bd9Sstevel@tonic-gate             }
7527c478bd9Sstevel@tonic-gate             *length = p - Line;
7537c478bd9Sstevel@tonic-gate             return (EOF);
7547c478bd9Sstevel@tonic-gate         }
7557c478bd9Sstevel@tonic-gate 	if (!csno) {
7567c478bd9Sstevel@tonic-gate 	        if (c == '\n') {
7577c478bd9Sstevel@tonic-gate 	            /* detect \r\n.  -Hans */
7587c478bd9Sstevel@tonic-gate 	            if (p>Line && p[-1] == '\r') {
7597c478bd9Sstevel@tonic-gate 	                column = oldcolumn;
7607c478bd9Sstevel@tonic-gate 	                p--;
7617c478bd9Sstevel@tonic-gate 	            }
7627c478bd9Sstevel@tonic-gate 	            Currline++;
7637c478bd9Sstevel@tonic-gate 	            break;
7647c478bd9Sstevel@tonic-gate 	        }
7657c478bd9Sstevel@tonic-gate 	        *p++ = c;
7667c478bd9Sstevel@tonic-gate 	        if (c == '\t')
7677c478bd9Sstevel@tonic-gate 	            if (hardtabs && column < promptlen && !hard) {
7687c478bd9Sstevel@tonic-gate 	                if (clr_eol && !dumb) {
7697c478bd9Sstevel@tonic-gate 	                    column = 1 + (column | 7);
7707c478bd9Sstevel@tonic-gate 	                    putp (clr_eol);
7717c478bd9Sstevel@tonic-gate 	                    promptlen = 0;
7727c478bd9Sstevel@tonic-gate 	                }
7737c478bd9Sstevel@tonic-gate 	                else {
7747c478bd9Sstevel@tonic-gate 	                    for (--p; column & 7 && p < &Line[LINSIZ - 1]; column++) {
7757c478bd9Sstevel@tonic-gate 	                        *p++ = ' ';
7767c478bd9Sstevel@tonic-gate 	                    }
7777c478bd9Sstevel@tonic-gate 	                    if (column >= promptlen) promptlen = 0;
7787c478bd9Sstevel@tonic-gate 	                }
7797c478bd9Sstevel@tonic-gate 	            }
7807c478bd9Sstevel@tonic-gate 	            else
7817c478bd9Sstevel@tonic-gate 	                column = 1 + (column | 7);
7827c478bd9Sstevel@tonic-gate 	        else if ((c == '\b') && (ul_opt || !cr_opt) && (column > 0))  /* M008 */
7837c478bd9Sstevel@tonic-gate 	                column--;
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 	        /* this is sort of strange.  what was here before was that
7867c478bd9Sstevel@tonic-gate 	           \r always set column to zero, and the hack above to
7877c478bd9Sstevel@tonic-gate 	           detect \r\n didnt exist.  the net effect is to make
7887c478bd9Sstevel@tonic-gate 	           the current line be overwritten by the prompt if it
7897c478bd9Sstevel@tonic-gate 	           had a \r at the end, and the line start after the \r
7907c478bd9Sstevel@tonic-gate 	           otherwise.  I suppose this is useful for overstriking
7917c478bd9Sstevel@tonic-gate 	           on hard copy terminals, but not on anything glass
7927c478bd9Sstevel@tonic-gate 	           -Hans */
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	        else if ((c == '\r') && !cr_opt) {
7957c478bd9Sstevel@tonic-gate 	                oldcolumn = column;
7967c478bd9Sstevel@tonic-gate 	                column = 0;
7977c478bd9Sstevel@tonic-gate 	        }
7987c478bd9Sstevel@tonic-gate 	        else if (c == '\f' && stop_opt) {
7997c478bd9Sstevel@tonic-gate 	                p[-1] = '^';
8007c478bd9Sstevel@tonic-gate 	                *p++ = 'L';
8017c478bd9Sstevel@tonic-gate 	                column += 2;
8027c478bd9Sstevel@tonic-gate 	                Pause++;
8037c478bd9Sstevel@tonic-gate 	        }
8047c478bd9Sstevel@tonic-gate 	        else if (c == EOF) {
8057c478bd9Sstevel@tonic-gate 	            *length = p - Line;
8067c478bd9Sstevel@tonic-gate 	            return (column);
8077c478bd9Sstevel@tonic-gate 	        }
8087c478bd9Sstevel@tonic-gate 	        else if (c < ' ' && cr_opt){                    /* M008 begin */
8097c478bd9Sstevel@tonic-gate 	                p[-1] = '^';
8107c478bd9Sstevel@tonic-gate 	                *p++ = c | ('A' - 1);
8117c478bd9Sstevel@tonic-gate 	                column += 2;
8127c478bd9Sstevel@tonic-gate 	        }                                               /* M008 end */
8137c478bd9Sstevel@tonic-gate 	        else if (c >= ' ' && c != RUBOUT)
8147c478bd9Sstevel@tonic-gate 	            column++;
8157c478bd9Sstevel@tonic-gate 	} /* end of code set 0 */
8167c478bd9Sstevel@tonic-gate 	else {
8177c478bd9Sstevel@tonic-gate 		column += scw[csno];
8187c478bd9Sstevel@tonic-gate 		if ( column > Mcol && fold_opt ) {
8197c478bd9Sstevel@tonic-gate 		    column -= scw[csno];
8207c478bd9Sstevel@tonic-gate 		    while ( column < Mcol ) {
8217c478bd9Sstevel@tonic-gate 		        column++;
8227c478bd9Sstevel@tonic-gate 		        *p++ = ' ';
8237c478bd9Sstevel@tonic-gate 		    }
8247c478bd9Sstevel@tonic-gate 		    column = Mcol;
8257c478bd9Sstevel@tonic-gate 		    Ungetc(c,f);
8267c478bd9Sstevel@tonic-gate 		} else {
8277c478bd9Sstevel@tonic-gate 		    int i;
8287c478bd9Sstevel@tonic-gate 		    *p++ = c;
8297c478bd9Sstevel@tonic-gate 		    for(i=1; i<cw[csno];i++)
8307c478bd9Sstevel@tonic-gate 			*p++ = Getc(f);
8317c478bd9Sstevel@tonic-gate 		}
8327c478bd9Sstevel@tonic-gate 	} /* end of codeset 1 ~ 3 */
8337c478bd9Sstevel@tonic-gate         if (column >= Mcol && fold_opt) break;
8347c478bd9Sstevel@tonic-gate         c = Getc (f);
8357c478bd9Sstevel@tonic-gate     }
8367c478bd9Sstevel@tonic-gate     if (column >= Mcol && Mcol > 0) {
8377c478bd9Sstevel@tonic-gate         if (!Wrap) {
8387c478bd9Sstevel@tonic-gate             *p++ = '\n';
8397c478bd9Sstevel@tonic-gate         }
8407c478bd9Sstevel@tonic-gate     }
8417c478bd9Sstevel@tonic-gate     colflg = column == Mcol && fold_opt;
8427c478bd9Sstevel@tonic-gate     if (colflg && eat_newline_glitch && Wrap) {
8437c478bd9Sstevel@tonic-gate 	*p++ = '\n'; /* simulate normal wrap */
8447c478bd9Sstevel@tonic-gate     }
8457c478bd9Sstevel@tonic-gate     *length = p - Line;
8467c478bd9Sstevel@tonic-gate     *p = 0;
8477c478bd9Sstevel@tonic-gate     return (column);
8487c478bd9Sstevel@tonic-gate }
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate /*
8517c478bd9Sstevel@tonic-gate ** Erase the rest of the prompt, assuming we are starting at column col.
8527c478bd9Sstevel@tonic-gate */
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate static void
prmpt_erase(register int col)8557c478bd9Sstevel@tonic-gate prmpt_erase(register int col)
8567c478bd9Sstevel@tonic-gate {
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate     if (promptlen == 0)
8597c478bd9Sstevel@tonic-gate         return;
8607c478bd9Sstevel@tonic-gate     if (hard) {
8617c478bd9Sstevel@tonic-gate         putchar ('\n');
8627c478bd9Sstevel@tonic-gate     }
8637c478bd9Sstevel@tonic-gate     else {
8647c478bd9Sstevel@tonic-gate         if (col == 0)
8657c478bd9Sstevel@tonic-gate             putchar ('\r');
8667c478bd9Sstevel@tonic-gate         if (!dumb && clr_eol)
8677c478bd9Sstevel@tonic-gate             putp (clr_eol);
8687c478bd9Sstevel@tonic-gate         else
8697c478bd9Sstevel@tonic-gate             for (col = promptlen - col; col > 0; col--)
8707c478bd9Sstevel@tonic-gate                 putchar (' ');
8717c478bd9Sstevel@tonic-gate     }
8727c478bd9Sstevel@tonic-gate     promptlen = 0;
8737c478bd9Sstevel@tonic-gate }
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate /*
8767c478bd9Sstevel@tonic-gate ** Erase the current line entirely
8777c478bd9Sstevel@tonic-gate */
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate static void
kill_line(void)8807c478bd9Sstevel@tonic-gate kill_line(void)
8817c478bd9Sstevel@tonic-gate {
8827c478bd9Sstevel@tonic-gate     prmpt_erase (0);
8837c478bd9Sstevel@tonic-gate     if (!clr_eol || dumb) putchar ('\r');
8847c478bd9Sstevel@tonic-gate }
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate /* Print a buffer of n characters */
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate static void
prbuf(register char * s,register int n)8897c478bd9Sstevel@tonic-gate prbuf(register char *s, register int n)
8907c478bd9Sstevel@tonic-gate {
8917c478bd9Sstevel@tonic-gate     char c;                             /* next ouput character */
8927c478bd9Sstevel@tonic-gate     register int state = 0;             /* next output char's UL state */
8937c478bd9Sstevel@tonic-gate     static int pstate = 0;              /* current terminal UL state (off) */
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate     while (--n >= 0)
8967c478bd9Sstevel@tonic-gate         if (!ul_opt)
8977c478bd9Sstevel@tonic-gate             putchar (*s++);
8987c478bd9Sstevel@tonic-gate         else {
8997c478bd9Sstevel@tonic-gate             if (n >= 2 && s[0] == '_' && s[1] == '\b') {
9007c478bd9Sstevel@tonic-gate                 n -= 2;
9017c478bd9Sstevel@tonic-gate                 s += 2;
9027c478bd9Sstevel@tonic-gate                 c = *s++;
9037c478bd9Sstevel@tonic-gate                 state = 1;
9047c478bd9Sstevel@tonic-gate             } else if (n >= 2 && s[1] == '\b' && s[2] == '_') {
9057c478bd9Sstevel@tonic-gate                 n -= 2;
9067c478bd9Sstevel@tonic-gate                 c = *s++;
9077c478bd9Sstevel@tonic-gate                 s += 2;
9087c478bd9Sstevel@tonic-gate                 state = 1;
9097c478bd9Sstevel@tonic-gate             } else {
9107c478bd9Sstevel@tonic-gate                 c = *s++;
9117c478bd9Sstevel@tonic-gate                 state = 0;
9127c478bd9Sstevel@tonic-gate             }
9137c478bd9Sstevel@tonic-gate             if (state != pstate)
9147c478bd9Sstevel@tonic-gate                 putp(state ? ULenter : ULexit);
9157c478bd9Sstevel@tonic-gate             pstate = state;
9167c478bd9Sstevel@tonic-gate             putchar(c);
9177c478bd9Sstevel@tonic-gate             if (state && underline_char) {
9187c478bd9Sstevel@tonic-gate                 putp(cursor_left);
9197c478bd9Sstevel@tonic-gate                 putp(underline_char);
9207c478bd9Sstevel@tonic-gate             }
9217c478bd9Sstevel@tonic-gate         }
9227c478bd9Sstevel@tonic-gate     /*
9237c478bd9Sstevel@tonic-gate      * M002
9247c478bd9Sstevel@tonic-gate      * You don't want to stay in standout mode at the end of the line;
9257c478bd9Sstevel@tonic-gate      * on some terminals, this will leave all of the remaining blank
9267c478bd9Sstevel@tonic-gate      * space on the line in standout mode.
9277c478bd9Sstevel@tonic-gate      */
9287c478bd9Sstevel@tonic-gate     if (state && !underline_char) {                       /*M002*/
9297c478bd9Sstevel@tonic-gate             putp(ULexit);                    /*M002*/
9307c478bd9Sstevel@tonic-gate             pstate = 0;                                 /*M002*/
9317c478bd9Sstevel@tonic-gate     }                                                   /*M002*/
9327c478bd9Sstevel@tonic-gate }
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate /*
9357c478bd9Sstevel@tonic-gate **  Clear the screen
9367c478bd9Sstevel@tonic-gate */
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate static void
doclear(void)9397c478bd9Sstevel@tonic-gate doclear(void)
9407c478bd9Sstevel@tonic-gate {
9417c478bd9Sstevel@tonic-gate     if (clear_screen && !hard) {
9427c478bd9Sstevel@tonic-gate         putp(clear_screen);
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate         /* Put out carriage return so that system doesn't
9457c478bd9Sstevel@tonic-gate         ** get confused by escape sequences when expanding tabs
9467c478bd9Sstevel@tonic-gate         */
9477c478bd9Sstevel@tonic-gate         putchar ('\r');
9487c478bd9Sstevel@tonic-gate         promptlen = 0;
9497c478bd9Sstevel@tonic-gate     }
9507c478bd9Sstevel@tonic-gate }
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate static int lastcmd, lastp;
9547c478bd9Sstevel@tonic-gate static off_t lastarg;
9557c478bd9Sstevel@tonic-gate static int lastcolon;
956ca3f9453Sas char shell_line[PATH_MAX];
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate /*
9597c478bd9Sstevel@tonic-gate ** Read a command and do it. A command consists of an optional integer
9607c478bd9Sstevel@tonic-gate ** argument followed by the command character.  Return the number of lines
9617c478bd9Sstevel@tonic-gate ** to display in the next screenful.  If there is nothing more to display
9627c478bd9Sstevel@tonic-gate ** in the current file, zero is returned.
9637c478bd9Sstevel@tonic-gate */
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate static off_t
command(char * filename,register FILE * f)9667c478bd9Sstevel@tonic-gate command(char *filename, register FILE *f)
9677c478bd9Sstevel@tonic-gate {
9687c478bd9Sstevel@tonic-gate     register off_t nlines;
9697c478bd9Sstevel@tonic-gate     register off_t retval;
9707c478bd9Sstevel@tonic-gate     register int c;
9717c478bd9Sstevel@tonic-gate     char colonch;
9727c478bd9Sstevel@tonic-gate     FILE *helpf;
9737c478bd9Sstevel@tonic-gate     int done;
9747c478bd9Sstevel@tonic-gate     char comchar, cmdbuf[80];
9757c478bd9Sstevel@tonic-gate     char filebuf[128];
9767c478bd9Sstevel@tonic-gate     char *loc;
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate #define ret(val) retval=val;done++;break
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate     done = 0;
9817c478bd9Sstevel@tonic-gate     if (!errors)
9827c478bd9Sstevel@tonic-gate         prompt (filename);
9837c478bd9Sstevel@tonic-gate     else
9847c478bd9Sstevel@tonic-gate         errors = 0;
9857c478bd9Sstevel@tonic-gate     for (;;) {
9867c478bd9Sstevel@tonic-gate         nlines = number (&comchar);
9877c478bd9Sstevel@tonic-gate         lastp = colonch = 0;
9887c478bd9Sstevel@tonic-gate         if (comchar == '.') {   /* Repeat last command */
9897c478bd9Sstevel@tonic-gate                 lastp++;
9907c478bd9Sstevel@tonic-gate                 comchar = lastcmd;
9917c478bd9Sstevel@tonic-gate                 nlines = lastarg;
9927c478bd9Sstevel@tonic-gate                 if (lastcmd == ':')
9937c478bd9Sstevel@tonic-gate                         colonch = lastcolon;
9947c478bd9Sstevel@tonic-gate         }
9957c478bd9Sstevel@tonic-gate         lastcmd = comchar;
9967c478bd9Sstevel@tonic-gate         lastarg = nlines;
9977c478bd9Sstevel@tonic-gate 	if((comchar != RUBOUT) && !dum_opt) {
9987c478bd9Sstevel@tonic-gate         	if (comchar == otty.c_cc[VERASE]) {
9997c478bd9Sstevel@tonic-gate            		 kill_line ();
10007c478bd9Sstevel@tonic-gate             		prompt (filename);
10017c478bd9Sstevel@tonic-gate             		continue;
10027c478bd9Sstevel@tonic-gate         	}
10037c478bd9Sstevel@tonic-gate 	}
10047c478bd9Sstevel@tonic-gate         switch (comchar) {
10057c478bd9Sstevel@tonic-gate         case ':':
10067c478bd9Sstevel@tonic-gate             retval = colon (filename, colonch, nlines);
10077c478bd9Sstevel@tonic-gate             if (retval >= 0)
10087c478bd9Sstevel@tonic-gate                 done++;
10097c478bd9Sstevel@tonic-gate             break;
10107c478bd9Sstevel@tonic-gate 	case 'b':
10117c478bd9Sstevel@tonic-gate 	case ctrl('B'):
10127c478bd9Sstevel@tonic-gate 	    {
10137c478bd9Sstevel@tonic-gate 		register off_t initline;
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 		if (no_intty) {
10167c478bd9Sstevel@tonic-gate 		    write(2, &bell, 1);
10177c478bd9Sstevel@tonic-gate 		    return (-1);
10187c478bd9Sstevel@tonic-gate 		}
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 		if (nlines == 0) nlines++;
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 		putchar ('\r');
10237c478bd9Sstevel@tonic-gate 		prmpt_erase (0);
10247c478bd9Sstevel@tonic-gate 		printf ("\n");
10257c478bd9Sstevel@tonic-gate 		if (clreol)
10267c478bd9Sstevel@tonic-gate 			cleareol ();
10277c478bd9Sstevel@tonic-gate 		printf (gettext("...back %lld page"), nlines);
10287c478bd9Sstevel@tonic-gate 		if (nlines > 1)
10297c478bd9Sstevel@tonic-gate 			pr ("s\n");
10307c478bd9Sstevel@tonic-gate 		else
10317c478bd9Sstevel@tonic-gate 			pr ("\n");
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 		if (clreol)
10347c478bd9Sstevel@tonic-gate 			cleareol ();
10357c478bd9Sstevel@tonic-gate 		pr ("\n");
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 		initline = Currline - dlines * (nlines + 1);
10387c478bd9Sstevel@tonic-gate 		if (! noscroll)
10397c478bd9Sstevel@tonic-gate 		    --initline;
10407c478bd9Sstevel@tonic-gate 		if (initline < 0) initline = 0;
10417c478bd9Sstevel@tonic-gate 		Fseek(f, 0LL);
10427c478bd9Sstevel@tonic-gate 		Currline = 0;	/* skiplns() will make Currline correct */
10437c478bd9Sstevel@tonic-gate 		skiplns(initline, f);
10447c478bd9Sstevel@tonic-gate 		if (! noscroll) {
10457c478bd9Sstevel@tonic-gate 		    ret(dlines + 1);
10467c478bd9Sstevel@tonic-gate 		}
10477c478bd9Sstevel@tonic-gate 		else {
10487c478bd9Sstevel@tonic-gate 		    ret(dlines);
10497c478bd9Sstevel@tonic-gate 		}
10507c478bd9Sstevel@tonic-gate 	    }
10517c478bd9Sstevel@tonic-gate         case ' ':
10527c478bd9Sstevel@tonic-gate         case 'z':
10537c478bd9Sstevel@tonic-gate             if (nlines == 0) nlines = dlines;
10547c478bd9Sstevel@tonic-gate             else if (comchar == 'z') dlines = nlines;
10557c478bd9Sstevel@tonic-gate             ret (nlines);
10567c478bd9Sstevel@tonic-gate         case 'd':
10577c478bd9Sstevel@tonic-gate         case ctrl('D'):
10587c478bd9Sstevel@tonic-gate             if (nlines != 0) nscroll = nlines;
10597c478bd9Sstevel@tonic-gate             ret (nscroll);
10607c478bd9Sstevel@tonic-gate         case RUBOUT:
10617c478bd9Sstevel@tonic-gate         case 'q':
10627c478bd9Sstevel@tonic-gate         case 'Q':
10637c478bd9Sstevel@tonic-gate             end_it(0);
10647c478bd9Sstevel@tonic-gate         case 's':
10657c478bd9Sstevel@tonic-gate         case 'f':
10667c478bd9Sstevel@tonic-gate             if (nlines == 0) nlines++;
10677c478bd9Sstevel@tonic-gate             if (comchar == 'f')
10687c478bd9Sstevel@tonic-gate                 nlines *= dlines;
10697c478bd9Sstevel@tonic-gate             putchar ('\r');
10707c478bd9Sstevel@tonic-gate             prmpt_erase (0);
10717c478bd9Sstevel@tonic-gate             printf ("\n");
10727c478bd9Sstevel@tonic-gate             if (clreol)
10737c478bd9Sstevel@tonic-gate                 cleareol ();
10747c478bd9Sstevel@tonic-gate             printf (gettext("...skipping %lld line"), nlines);
10757c478bd9Sstevel@tonic-gate             if (nlines > 1)
10767c478bd9Sstevel@tonic-gate                 pr ("s\n");
10777c478bd9Sstevel@tonic-gate             else
10787c478bd9Sstevel@tonic-gate                 pr ("\n");
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate             if (clreol)
10817c478bd9Sstevel@tonic-gate                 cleareol ();
10827c478bd9Sstevel@tonic-gate             pr ("\n");
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate             while (nlines > 0) {
10857c478bd9Sstevel@tonic-gate                 while ((c = Getc (f)) != '\n')
10867c478bd9Sstevel@tonic-gate                     if (c == EOF) {
10877c478bd9Sstevel@tonic-gate                         retval = 0;
10887c478bd9Sstevel@tonic-gate                         done++;
10897c478bd9Sstevel@tonic-gate                         goto endsw;
10907c478bd9Sstevel@tonic-gate                     }
1091f5352230SToomas Soome                 Currline++;
1092f5352230SToomas Soome                 nlines--;
10937c478bd9Sstevel@tonic-gate             }
10947c478bd9Sstevel@tonic-gate             ret (dlines);
10957c478bd9Sstevel@tonic-gate         case '\n':
10967c478bd9Sstevel@tonic-gate 	    if (nlines != 0)
10977c478bd9Sstevel@tonic-gate                 dlines = nlines;
10987c478bd9Sstevel@tonic-gate             else
10997c478bd9Sstevel@tonic-gate                 nlines = 1;
11007c478bd9Sstevel@tonic-gate             ret (nlines);
11017c478bd9Sstevel@tonic-gate         case '\f':
11027c478bd9Sstevel@tonic-gate             if (!no_intty) {
11037c478bd9Sstevel@tonic-gate                 doclear ();
11047c478bd9Sstevel@tonic-gate                 Fseek (f, screen_start.chrctr);
11057c478bd9Sstevel@tonic-gate                 Currline = screen_start.line;
11067c478bd9Sstevel@tonic-gate                 ret (dlines);
11077c478bd9Sstevel@tonic-gate             }
11087c478bd9Sstevel@tonic-gate             else {
11097c478bd9Sstevel@tonic-gate                 write (2, &Bell, 1);
11107c478bd9Sstevel@tonic-gate                 break;
11117c478bd9Sstevel@tonic-gate             }
11127c478bd9Sstevel@tonic-gate         case '\'':
11137c478bd9Sstevel@tonic-gate             if (!no_intty) {
11147c478bd9Sstevel@tonic-gate                 kill_line ();
11157c478bd9Sstevel@tonic-gate                 pr (gettext("\n***Back***\n\n"));
11167c478bd9Sstevel@tonic-gate                 Fseek (f, context.chrctr);
11177c478bd9Sstevel@tonic-gate                 Currline = context.line;
11187c478bd9Sstevel@tonic-gate                 ret (dlines);
11197c478bd9Sstevel@tonic-gate             }
11207c478bd9Sstevel@tonic-gate             else {
11217c478bd9Sstevel@tonic-gate                 write (2, &Bell, 1);
11227c478bd9Sstevel@tonic-gate                 break;
11237c478bd9Sstevel@tonic-gate             }
11247c478bd9Sstevel@tonic-gate         case '=':
11257c478bd9Sstevel@tonic-gate             kill_line ();
11267c478bd9Sstevel@tonic-gate             promptlen = printf ("%lld", Currline);
11277c478bd9Sstevel@tonic-gate             fflush (stdout);
11287c478bd9Sstevel@tonic-gate             break;
11297c478bd9Sstevel@tonic-gate         case 'n':
11307c478bd9Sstevel@tonic-gate             lastp++;
1131e608d74dSToomas Soome 	    /* FALLTHROUGH */
11327c478bd9Sstevel@tonic-gate         case '/':
11337c478bd9Sstevel@tonic-gate             if (nlines == 0) nlines++;
11347c478bd9Sstevel@tonic-gate             kill_line ();
11357c478bd9Sstevel@tonic-gate             pr ("/");
11367c478bd9Sstevel@tonic-gate             promptlen = 1;
11377c478bd9Sstevel@tonic-gate             fflush (stdout);
11387c478bd9Sstevel@tonic-gate             if (lastp) {
11397c478bd9Sstevel@tonic-gate                 write (2,"\r", 1);
11407c478bd9Sstevel@tonic-gate                 search (NULL, f, nlines);       /* Use previous r.e. */
11417c478bd9Sstevel@tonic-gate             }
11427c478bd9Sstevel@tonic-gate             else {
11437c478bd9Sstevel@tonic-gate                 ttyin (cmdbuf, 78, '/');
11447c478bd9Sstevel@tonic-gate                 write (2, "\r", 1);
11457c478bd9Sstevel@tonic-gate                 search (cmdbuf, f, nlines);
11467c478bd9Sstevel@tonic-gate             }
11477c478bd9Sstevel@tonic-gate             ret (dlines-1);
11487c478bd9Sstevel@tonic-gate         case '!':
11497c478bd9Sstevel@tonic-gate             do_shell (filename);
11507c478bd9Sstevel@tonic-gate             break;
11517c478bd9Sstevel@tonic-gate         case 'h':
11527c478bd9Sstevel@tonic-gate         case '?':
11537c478bd9Sstevel@tonic-gate 	    /*
11547c478bd9Sstevel@tonic-gate 	     * First get local help file.
11557c478bd9Sstevel@tonic-gate 	     */
11567c478bd9Sstevel@tonic-gate 	    loc = setlocale(LC_MESSAGES, 0);
11577c478bd9Sstevel@tonic-gate 	    sprintf(filebuf, LOCAL_HELP, loc);
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate             if  ((strcmp(loc, "C") == 0) || (helpf = fopen (filebuf, "r")) == NULL) {
11607c478bd9Sstevel@tonic-gate 		    if  ((helpf = fopen (HELPFILE, "r")) == NULL)
11617c478bd9Sstevel@tonic-gate 			error (gettext("Can't open help file"));
11627c478bd9Sstevel@tonic-gate 	    }
11637c478bd9Sstevel@tonic-gate             if (noscroll) doclear ();
11647c478bd9Sstevel@tonic-gate             copy_file (helpf);
11657c478bd9Sstevel@tonic-gate             fclose (helpf);
11667c478bd9Sstevel@tonic-gate             prompt (filename);
11677c478bd9Sstevel@tonic-gate             break;
11687c478bd9Sstevel@tonic-gate         case 'v':       /* This case should go right before default */
11697c478bd9Sstevel@tonic-gate             if (!no_intty) {
11707c478bd9Sstevel@tonic-gate                 kill_line ();
11717c478bd9Sstevel@tonic-gate                 cmdbuf[0] = '+';
11727c478bd9Sstevel@tonic-gate                 sprintf(&cmdbuf[1], "%lld", Currline);
11737c478bd9Sstevel@tonic-gate                 pr ("vi "); pr (cmdbuf); putchar (' '); pr (fnames[fnum]);
11747c478bd9Sstevel@tonic-gate                 execute (filename, VI, "vi", cmdbuf, fnames[fnum], 0);
11757c478bd9Sstevel@tonic-gate                 break;
11767c478bd9Sstevel@tonic-gate             }
1177e608d74dSToomas Soome 	    /* FALLTHROUGH */
11787c478bd9Sstevel@tonic-gate         default:
11797c478bd9Sstevel@tonic-gate 		if (dum_opt) {
11807c478bd9Sstevel@tonic-gate 			kill_line ();
11817c478bd9Sstevel@tonic-gate 		    	promptlen = pr(gettext("[Press 'h' for instructions.]"));
11827c478bd9Sstevel@tonic-gate 			fflush (stdout);
11837c478bd9Sstevel@tonic-gate 	    	}
11847c478bd9Sstevel@tonic-gate 	    	else
11857c478bd9Sstevel@tonic-gate             		write (2, &Bell, 1);
11867c478bd9Sstevel@tonic-gate             break;
11877c478bd9Sstevel@tonic-gate         }
11887c478bd9Sstevel@tonic-gate         if (done) break;
11897c478bd9Sstevel@tonic-gate     }
11907c478bd9Sstevel@tonic-gate     putchar ('\r');
11917c478bd9Sstevel@tonic-gate endsw:
11927c478bd9Sstevel@tonic-gate     inwait = 0;
11937c478bd9Sstevel@tonic-gate     notell++;
11947c478bd9Sstevel@tonic-gate     return (retval);
11957c478bd9Sstevel@tonic-gate }
11967c478bd9Sstevel@tonic-gate 
11977c478bd9Sstevel@tonic-gate char ch;
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate /*
12007c478bd9Sstevel@tonic-gate  * Execute a colon-prefixed command.
12017c478bd9Sstevel@tonic-gate  * Returns <0 if not a command that should cause
12027c478bd9Sstevel@tonic-gate  * more of the file to be printed.
12037c478bd9Sstevel@tonic-gate  */
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate static int
colon(char * filename,int cmd,off_t nlines)12067c478bd9Sstevel@tonic-gate colon(char *filename, int cmd, off_t nlines)
12077c478bd9Sstevel@tonic-gate {
12087c478bd9Sstevel@tonic-gate         if (cmd == 0)
12097c478bd9Sstevel@tonic-gate                 ch = readch ();
12107c478bd9Sstevel@tonic-gate         else
12117c478bd9Sstevel@tonic-gate                 ch = cmd;
12127c478bd9Sstevel@tonic-gate         lastcolon = ch;
12137c478bd9Sstevel@tonic-gate         switch (ch) {
12147c478bd9Sstevel@tonic-gate         case 'f':
12157c478bd9Sstevel@tonic-gate                 kill_line ();
1216