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
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
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 *
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 ();
12167c478bd9Sstevel@tonic-gate                 if (!no_intty)
12177c478bd9Sstevel@tonic-gate                         promptlen = printf (gettext("\"%s\" line %lld"),
12187c478bd9Sstevel@tonic-gate 					    fnames[fnum], Currline);
12197c478bd9Sstevel@tonic-gate                 else
12207c478bd9Sstevel@tonic-gate                         promptlen = printf(
12217c478bd9Sstevel@tonic-gate 			 gettext("[Not a file] line %lld"), Currline);
12227c478bd9Sstevel@tonic-gate                 fflush (stdout);
12237c478bd9Sstevel@tonic-gate                 return (-1);
12247c478bd9Sstevel@tonic-gate         case 'n':
12257c478bd9Sstevel@tonic-gate                 if (nlines == 0) {
12267c478bd9Sstevel@tonic-gate                         if (fnum >= nfiles - 1)
12277c478bd9Sstevel@tonic-gate                                 end_it(0);
12287c478bd9Sstevel@tonic-gate                         nlines++;
12297c478bd9Sstevel@tonic-gate                 }
12307c478bd9Sstevel@tonic-gate                 putchar ('\r');
12317c478bd9Sstevel@tonic-gate                 prmpt_erase (0);
12327c478bd9Sstevel@tonic-gate                 skipf ((int)nlines);
12337c478bd9Sstevel@tonic-gate                 return (0);
12347c478bd9Sstevel@tonic-gate         case 'p':
12357c478bd9Sstevel@tonic-gate                 if (no_intty) {
12367c478bd9Sstevel@tonic-gate                         write (2, &Bell, 1);
12377c478bd9Sstevel@tonic-gate                         return (-1);
12387c478bd9Sstevel@tonic-gate                 }
12397c478bd9Sstevel@tonic-gate                 putchar ('\r');
12407c478bd9Sstevel@tonic-gate                 prmpt_erase (0);
12417c478bd9Sstevel@tonic-gate                 if (nlines == 0)
12427c478bd9Sstevel@tonic-gate                         nlines++;
12437c478bd9Sstevel@tonic-gate                 skipf ((int)-nlines);
12447c478bd9Sstevel@tonic-gate                 return (0);
12457c478bd9Sstevel@tonic-gate         case '!':
12467c478bd9Sstevel@tonic-gate                 do_shell (filename);
12477c478bd9Sstevel@tonic-gate                 return (-1);
12487c478bd9Sstevel@tonic-gate         case 'q':
12497c478bd9Sstevel@tonic-gate         case 'Q':
12507c478bd9Sstevel@tonic-gate                 end_it(0);
12517c478bd9Sstevel@tonic-gate         default:
12527c478bd9Sstevel@tonic-gate                 write (2, &Bell, 1);
12537c478bd9Sstevel@tonic-gate                 return (-1);
12547c478bd9Sstevel@tonic-gate         }
12557c478bd9Sstevel@tonic-gate }
12567c478bd9Sstevel@tonic-gate 
12577c478bd9Sstevel@tonic-gate /*
12587c478bd9Sstevel@tonic-gate ** Read a decimal number from the terminal. Set cmd to the non-digit which
12597c478bd9Sstevel@tonic-gate ** terminates the number.
12607c478bd9Sstevel@tonic-gate */
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate static int
12637c478bd9Sstevel@tonic-gate number(char *cmd)
12647c478bd9Sstevel@tonic-gate {
12657c478bd9Sstevel@tonic-gate         register int i;
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate         i = 0; ch = otty.c_cc[VKILL];
12687c478bd9Sstevel@tonic-gate         for (;;) {
12697c478bd9Sstevel@tonic-gate                 ch = readch ();
12707c478bd9Sstevel@tonic-gate                 if (ch >= '0' && ch <= '9') {
12717c478bd9Sstevel@tonic-gate                         i = i*10 + ch - '0';
12727c478bd9Sstevel@tonic-gate                 } else if (ch == RUBOUT) {
12737c478bd9Sstevel@tonic-gate                         i = 0;
12747c478bd9Sstevel@tonic-gate                         *cmd = ch;
12757c478bd9Sstevel@tonic-gate                         break;
12767c478bd9Sstevel@tonic-gate                 } else if (ch == otty.c_cc[VKILL]) {
12777c478bd9Sstevel@tonic-gate                         i = 0;
12787c478bd9Sstevel@tonic-gate                 } else {
12797c478bd9Sstevel@tonic-gate                         *cmd = ch;
12807c478bd9Sstevel@tonic-gate                         break;
12817c478bd9Sstevel@tonic-gate                 }
12827c478bd9Sstevel@tonic-gate         }
12837c478bd9Sstevel@tonic-gate         return (i);
12847c478bd9Sstevel@tonic-gate }
12857c478bd9Sstevel@tonic-gate 
12867c478bd9Sstevel@tonic-gate static void
12877c478bd9Sstevel@tonic-gate do_shell(char *filename)
12887c478bd9Sstevel@tonic-gate {
12897c478bd9Sstevel@tonic-gate         char cmdbuf[80];
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate         kill_line ();
12927c478bd9Sstevel@tonic-gate         pr ("!");
12937c478bd9Sstevel@tonic-gate         fflush (stdout);
12947c478bd9Sstevel@tonic-gate         promptlen = 1;
12957c478bd9Sstevel@tonic-gate         if (lastp)
12967c478bd9Sstevel@tonic-gate                 pr (shell_line);
12977c478bd9Sstevel@tonic-gate         else {
12987c478bd9Sstevel@tonic-gate                 ttyin (cmdbuf, 78, '!');
12997c478bd9Sstevel@tonic-gate                 if (expand (shell_line, cmdbuf)) {
13007c478bd9Sstevel@tonic-gate                         kill_line ();
13017c478bd9Sstevel@tonic-gate                         promptlen = printf ("!%s", shell_line);
13027c478bd9Sstevel@tonic-gate                 }
13037c478bd9Sstevel@tonic-gate         }
13047c478bd9Sstevel@tonic-gate         fflush (stdout);
13057c478bd9Sstevel@tonic-gate         write (2, "\n", 1);
13067c478bd9Sstevel@tonic-gate         promptlen = 0;
13077c478bd9Sstevel@tonic-gate         shellp = 1;
13087c478bd9Sstevel@tonic-gate         execute (filename, shell, shell, "-c", shell_line, 0);
13097c478bd9Sstevel@tonic-gate }
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate /*
13127c478bd9Sstevel@tonic-gate ** Search for nth ocurrence of regular expression contained in buf in the file
13137c478bd9Sstevel@tonic-gate */
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate static void
13167c478bd9Sstevel@tonic-gate search(char buf[], FILE *file, register off_t n)
13177c478bd9Sstevel@tonic-gate {
13187c478bd9Sstevel@tonic-gate     off_t startline = Ftell (file);
13197c478bd9Sstevel@tonic-gate     register off_t line1 = startline;
13207c478bd9Sstevel@tonic-gate     register off_t line2 = startline;
13217c478bd9Sstevel@tonic-gate     register off_t line3 = startline;
13227c478bd9Sstevel@tonic-gate     register off_t lncount;
13237c478bd9Sstevel@tonic-gate     off_t saveln;
13247c478bd9Sstevel@tonic-gate     static char *s = NULL;
13257c478bd9Sstevel@tonic-gate     static char lastbuf[80];
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate     if (buf != NULL) {
13287c478bd9Sstevel@tonic-gate 	if (s != NULL)
13297c478bd9Sstevel@tonic-gate 		free(s);
13307c478bd9Sstevel@tonic-gate 	if (*buf != '\0') {
13317c478bd9Sstevel@tonic-gate 		if ((s = regcmp(buf, (char *) NULL)) == NULL)
13327c478bd9Sstevel@tonic-gate 			error(gettext("Regular expression botch"));
13337c478bd9Sstevel@tonic-gate 		else
13347c478bd9Sstevel@tonic-gate 			strcpy(lastbuf, buf);
13357c478bd9Sstevel@tonic-gate 	} else {
13367c478bd9Sstevel@tonic-gate 		if ((s = regcmp(lastbuf, (char *) NULL)) == NULL)
13377c478bd9Sstevel@tonic-gate 			error(gettext("No previous regular expression"));
13387c478bd9Sstevel@tonic-gate 	}
13397c478bd9Sstevel@tonic-gate     } else {
13407c478bd9Sstevel@tonic-gate 	if (s == NULL)
13417c478bd9Sstevel@tonic-gate 	    error(gettext("No previous regular expression"));
13427c478bd9Sstevel@tonic-gate     }
13437c478bd9Sstevel@tonic-gate     context.line = saveln = Currline;
13447c478bd9Sstevel@tonic-gate     context.chrctr = startline;
13457c478bd9Sstevel@tonic-gate     lncount = 0;
13467c478bd9Sstevel@tonic-gate     while (!feof (file)) {
13477c478bd9Sstevel@tonic-gate         line3 = line2;
13487c478bd9Sstevel@tonic-gate         line2 = line1;
13497c478bd9Sstevel@tonic-gate         line1 = Ftell (file);
13507c478bd9Sstevel@tonic-gate         rdline (file);
13517c478bd9Sstevel@tonic-gate         lncount++;
13527c478bd9Sstevel@tonic-gate         if (regex(s, Line) != NULL)
13537c478bd9Sstevel@tonic-gate                 if (--n == 0) {
13547c478bd9Sstevel@tonic-gate                     if (lncount > 3 || (lncount > 1 && no_intty))
13557c478bd9Sstevel@tonic-gate                     {
13567c478bd9Sstevel@tonic-gate                         pr ("\n");
13577c478bd9Sstevel@tonic-gate                         if (clreol)
13587c478bd9Sstevel@tonic-gate                             cleareol ();
13597c478bd9Sstevel@tonic-gate                         pr(gettext("...skipping\n"));
13607c478bd9Sstevel@tonic-gate                     }
13617c478bd9Sstevel@tonic-gate                     if (!no_intty) {
13627c478bd9Sstevel@tonic-gate                         Currline -= (lncount >= 3 ? 3 : lncount);
13637c478bd9Sstevel@tonic-gate                         Fseek (file, line3);
13647c478bd9Sstevel@tonic-gate                         if (noscroll)
13657c478bd9Sstevel@tonic-gate                             if (clreol) {
13667c478bd9Sstevel@tonic-gate                                 home ();
13677c478bd9Sstevel@tonic-gate                                 cleareol ();
13687c478bd9Sstevel@tonic-gate                             }
13697c478bd9Sstevel@tonic-gate                             else
13707c478bd9Sstevel@tonic-gate                                 doclear ();
13717c478bd9Sstevel@tonic-gate                     }
13727c478bd9Sstevel@tonic-gate                     else {
13737c478bd9Sstevel@tonic-gate                         kill_line ();
13747c478bd9Sstevel@tonic-gate                         if (noscroll)
13757c478bd9Sstevel@tonic-gate                             if (clreol) {
13767c478bd9Sstevel@tonic-gate                                 home ();
13777c478bd9Sstevel@tonic-gate                                 cleareol ();
13787c478bd9Sstevel@tonic-gate                             } else
13797c478bd9Sstevel@tonic-gate                                 doclear ();
13807c478bd9Sstevel@tonic-gate                         pr (Line);
13817c478bd9Sstevel@tonic-gate                         putchar ('\n');
13827c478bd9Sstevel@tonic-gate                     }
13837c478bd9Sstevel@tonic-gate                     break;
13847c478bd9Sstevel@tonic-gate                 }
13857c478bd9Sstevel@tonic-gate     }
13867c478bd9Sstevel@tonic-gate     if (feof (file)) {
13877c478bd9Sstevel@tonic-gate         if (!no_intty) {
13887c478bd9Sstevel@tonic-gate             Currline = saveln;
13897c478bd9Sstevel@tonic-gate             Fseek (file, startline);
13907c478bd9Sstevel@tonic-gate         }
13917c478bd9Sstevel@tonic-gate         else {
13927c478bd9Sstevel@tonic-gate             pr (gettext("\nPattern not found\n"));
13937c478bd9Sstevel@tonic-gate             end_it (0);
13947c478bd9Sstevel@tonic-gate         }
13957c478bd9Sstevel@tonic-gate         error (gettext("Pattern not found"));
13967c478bd9Sstevel@tonic-gate     }
13977c478bd9Sstevel@tonic-gate }
13987c478bd9Sstevel@tonic-gate 
13997c478bd9Sstevel@tonic-gate #define MAXARGS 10 /* enough for 9 args. We are only passed 4 now */
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate static void
14027c478bd9Sstevel@tonic-gate execute (char *filename, char *cmd, ...)
14037c478bd9Sstevel@tonic-gate {
14047c478bd9Sstevel@tonic-gate         pid_t id;
14057c478bd9Sstevel@tonic-gate 	va_list ap;
14067c478bd9Sstevel@tonic-gate 	char *argp[MAXARGS];
14077c478bd9Sstevel@tonic-gate 	int  count;
14087c478bd9Sstevel@tonic-gate 
14097c478bd9Sstevel@tonic-gate         fflush (stdout);
14107c478bd9Sstevel@tonic-gate         reset_tty ();
14117c478bd9Sstevel@tonic-gate         while ((id = fork ()) < 0)
14127c478bd9Sstevel@tonic-gate             sleep (5);
14137c478bd9Sstevel@tonic-gate         if (id == 0) {
14147c478bd9Sstevel@tonic-gate             if (no_intty) {     /*M002*/
14157c478bd9Sstevel@tonic-gate                 close(0);       /*M002*/
14167c478bd9Sstevel@tonic-gate                 dup(2);         /*M002*/
14177c478bd9Sstevel@tonic-gate             }                   /*M002*/
14187c478bd9Sstevel@tonic-gate 	    va_start(ap, cmd);
14197c478bd9Sstevel@tonic-gate 	    count = 0;
14207c478bd9Sstevel@tonic-gate 	    do {
14217c478bd9Sstevel@tonic-gate #ifndef lint
14227c478bd9Sstevel@tonic-gate 		argp[count] = va_arg(ap, char *);
14237c478bd9Sstevel@tonic-gate #else
14247c478bd9Sstevel@tonic-gate 		ap = ap;
14257c478bd9Sstevel@tonic-gate #endif
14267c478bd9Sstevel@tonic-gate 		count++;
14277c478bd9Sstevel@tonic-gate 		if (count > MAXARGS)
14287c478bd9Sstevel@tonic-gate 			error (gettext("Too many arguments in execute()\n"));
14297c478bd9Sstevel@tonic-gate 	    } while (argp[count - 1] != NULL);
14307c478bd9Sstevel@tonic-gate 	    va_end(ap);
14317c478bd9Sstevel@tonic-gate 	    execvp(cmd, argp);
14327c478bd9Sstevel@tonic-gate             write (2, "exec failed\n", 12);
14337c478bd9Sstevel@tonic-gate             exit (1);
14347c478bd9Sstevel@tonic-gate         }
14357c478bd9Sstevel@tonic-gate         signal (SIGINT, SIG_IGN);
14367c478bd9Sstevel@tonic-gate         signal (SIGQUIT, SIG_IGN);
14377c478bd9Sstevel@tonic-gate 	signal (SIGWINCH, SIG_IGN);
14387c478bd9Sstevel@tonic-gate #ifdef SIGTSTP
14397c478bd9Sstevel@tonic-gate         if (catch_susp)
14407c478bd9Sstevel@tonic-gate             signal(SIGTSTP, SIG_DFL);
14417c478bd9Sstevel@tonic-gate #endif
14427c478bd9Sstevel@tonic-gate         wait ((pid_t)0);
14437c478bd9Sstevel@tonic-gate         signal (SIGINT, end_it);
14447c478bd9Sstevel@tonic-gate         signal (SIGQUIT, onquit);
14457c478bd9Sstevel@tonic-gate 	signal (SIGWINCH, chgwinsz);
14467c478bd9Sstevel@tonic-gate #ifdef SIGTSTP
14477c478bd9Sstevel@tonic-gate         if (catch_susp)
14487c478bd9Sstevel@tonic-gate             signal(SIGTSTP, onsusp);
14497c478bd9Sstevel@tonic-gate #endif
14507c478bd9Sstevel@tonic-gate 	/*
14517c478bd9Sstevel@tonic-gate 	 * Since we were ignoring window change signals while we executed
14527c478bd9Sstevel@tonic-gate 	 * the command, we must assume the window changed.
14537c478bd9Sstevel@tonic-gate 	 */
14547c478bd9Sstevel@tonic-gate 	(void) chgwinsz(0);
14557c478bd9Sstevel@tonic-gate 	set_tty ();
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate         pr ("------------------------\n");
14587c478bd9Sstevel@tonic-gate         prompt (filename);
14597c478bd9Sstevel@tonic-gate }
14607c478bd9Sstevel@tonic-gate /*
14617c478bd9Sstevel@tonic-gate ** Skip n lines in the file f
14627c478bd9Sstevel@tonic-gate */
14637c478bd9Sstevel@tonic-gate 
14647c478bd9Sstevel@tonic-gate static void
14657c478bd9Sstevel@tonic-gate skiplns(register off_t n, register FILE *f)
14667c478bd9Sstevel@tonic-gate {
14677c478bd9Sstevel@tonic-gate     register int c;
14687c478bd9Sstevel@tonic-gate 
14697c478bd9Sstevel@tonic-gate     while (n > 0) {
14707c478bd9Sstevel@tonic-gate         while ((c = Getc (f)) != '\n')
14717c478bd9Sstevel@tonic-gate             if (c == EOF)
14727c478bd9Sstevel@tonic-gate                 return;
1473f5352230SToomas Soome         n--;
1474f5352230SToomas Soome         Currline++;
14757c478bd9Sstevel@tonic-gate     }
14767c478bd9Sstevel@tonic-gate }
14777c478bd9Sstevel@tonic-gate 
14787c478bd9Sstevel@tonic-gate /*
14797c478bd9Sstevel@tonic-gate ** Skip nskip files in the file list (from the command line). Nskip may be
14807c478bd9Sstevel@tonic-gate ** negative.
14817c478bd9Sstevel@tonic-gate */
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate static void
14847c478bd9Sstevel@tonic-gate skipf(register int nskip)
14857c478bd9Sstevel@tonic-gate {
14867c478bd9Sstevel@tonic-gate     if (nskip == 0) return;
14877c478bd9Sstevel@tonic-gate     if (nskip > 0) {
14887c478bd9Sstevel@tonic-gate         if (fnum + nskip > nfiles - 1)
14897c478bd9Sstevel@tonic-gate             nskip = nfiles - fnum - 1;
14907c478bd9Sstevel@tonic-gate     }
14917c478bd9Sstevel@tonic-gate     else if (within)
14927c478bd9Sstevel@tonic-gate         ++fnum;
14937c478bd9Sstevel@tonic-gate     fnum += nskip;
14947c478bd9Sstevel@tonic-gate     if (fnum < 0)
14957c478bd9Sstevel@tonic-gate         fnum = 0;
14967c478bd9Sstevel@tonic-gate     pr (gettext("\n...Skipping "));
14977c478bd9Sstevel@tonic-gate     pr ("\n");
14987c478bd9Sstevel@tonic-gate     if (clreol)
14997c478bd9Sstevel@tonic-gate         cleareol ();
15007c478bd9Sstevel@tonic-gate     if (nskip > 0)
15017c478bd9Sstevel@tonic-gate 	printf(gettext("...Skipping to file %s\n"), fnames[fnum]);
15027c478bd9Sstevel@tonic-gate     else
15037c478bd9Sstevel@tonic-gate 	printf(gettext("...Skipping back to file %s\n"), fnames[fnum]);
15047c478bd9Sstevel@tonic-gate     if (clreol)
15057c478bd9Sstevel@tonic-gate         cleareol ();
15067c478bd9Sstevel@tonic-gate     pr ("\n");
15077c478bd9Sstevel@tonic-gate     --fnum;
15087c478bd9Sstevel@tonic-gate }
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate /*----------------------------- Terminal I/O -------------------------------*/
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate static void
15137c478bd9Sstevel@tonic-gate initterm(void)
15147c478bd9Sstevel@tonic-gate {
15157c478bd9Sstevel@tonic-gate     int         erret = 0;
15167c478bd9Sstevel@tonic-gate 
15177c478bd9Sstevel@tonic-gate     setbuf(stdout, obuf);
15187c478bd9Sstevel@tonic-gate     if (!(no_tty = ioctl(1, TCGETA, &otty))) {
15197c478bd9Sstevel@tonic-gate 	if (setupterm(NULL, 1, &erret) != OK) {
15207c478bd9Sstevel@tonic-gate             dumb++; ul_opt = 0;
15217c478bd9Sstevel@tonic-gate         }
15227c478bd9Sstevel@tonic-gate         else {
1523*e24b44e5SToomas Soome 	    int row, col;
1524*e24b44e5SToomas Soome 	    struct winsize w;
1525*e24b44e5SToomas Soome 	    char *s;
1526*e24b44e5SToomas Soome 
15277c478bd9Sstevel@tonic-gate 	    reset_shell_mode();
1528*e24b44e5SToomas Soome 	    row = col = -1;
1529*e24b44e5SToomas Soome 	    if (ioctl(fileno(stdout), TIOCGWINSZ, &w) == 0) {
1530*e24b44e5SToomas Soome 		if (w.ws_row > 0)
1531*e24b44e5SToomas Soome 		    row = w.ws_row;
1532*e24b44e5SToomas Soome 		if (w.ws_col > 0)
1533*e24b44e5SToomas Soome 		    col = w.ws_col;
1534*e24b44e5SToomas Soome 	    }
1535*e24b44e5SToomas Soome 	    if (row < 0) {
1536*e24b44e5SToomas Soome 		if ((s = getenv("LINES")) != NULL)
1537*e24b44e5SToomas Soome 		    row = atoi(s);
1538*e24b44e5SToomas Soome 	    }
1539*e24b44e5SToomas Soome 	    if (col < 0) {
1540*e24b44e5SToomas Soome 		if ((s = getenv("COLUMNS")) != NULL)
1541*e24b44e5SToomas Soome 		    col = atoi(s);
1542*e24b44e5SToomas Soome 	    }
1543*e24b44e5SToomas Soome 
1544*e24b44e5SToomas Soome 	    if (row > 0)
1545*e24b44e5SToomas Soome 		Lpp = row;
1546*e24b44e5SToomas Soome 	    else if (((Lpp = lines) < 0) || hard_copy) {
15477c478bd9Sstevel@tonic-gate                 hard++; /* Hard copy terminal */
15487c478bd9Sstevel@tonic-gate                 Lpp = 24;
15497c478bd9Sstevel@tonic-gate             }
1550*e24b44e5SToomas Soome 	    if (col > 0)
1551*e24b44e5SToomas Soome 		Mcol = col;
1552*e24b44e5SToomas Soome 	    else if ((Mcol = columns) < 0)
1553*e24b44e5SToomas Soome                 Mcol = 80;
1554*e24b44e5SToomas Soome 
15557c478bd9Sstevel@tonic-gate             if (tailequ(fnames[0], "page") || !hard && (scroll_forward == NULL))
15567c478bd9Sstevel@tonic-gate                 noscroll++;
15577c478bd9Sstevel@tonic-gate             Wrap = tigetflag("am");
15587c478bd9Sstevel@tonic-gate             /*
15597c478bd9Sstevel@tonic-gate              *  Set up for underlining:  some terminals don't need it;
15607c478bd9Sstevel@tonic-gate              *  others have start/stop sequences, still others have an
15617c478bd9Sstevel@tonic-gate              *  underline char sequence which is assumed to move the
15627c478bd9Sstevel@tonic-gate              *  cursor forward one character.  If underline sequence
15637c478bd9Sstevel@tonic-gate              *  isn't available, settle for standout sequence.
15647c478bd9Sstevel@tonic-gate              */
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate             if (transparent_underline || over_strike)
15677c478bd9Sstevel@tonic-gate                 ul_opt = 0;
15687c478bd9Sstevel@tonic-gate             if ((ULenter = tigetstr("smul")) == NULL &&
15697c478bd9Sstevel@tonic-gate                 (!underline_char) && (ULenter = tigetstr("smso")) == NULL)
15707c478bd9Sstevel@tonic-gate                 ULenter = "";
15717c478bd9Sstevel@tonic-gate             if ((ULexit = tigetstr("rmul")) == NULL &&
15727c478bd9Sstevel@tonic-gate                 (!underline_char) && (ULexit = tigetstr("rmso")) == NULL)
15737c478bd9Sstevel@tonic-gate                 ULexit = "";
15747c478bd9Sstevel@tonic-gate         }
15757c478bd9Sstevel@tonic-gate         if ((shell = getenv("SHELL")) == NULL)
15767c478bd9Sstevel@tonic-gate             shell = "/usr/bin/sh";
15777c478bd9Sstevel@tonic-gate     }
15787c478bd9Sstevel@tonic-gate     no_intty = ioctl(0, TCGETA, &otty);
15797c478bd9Sstevel@tonic-gate     ioctl(2, TCGETA, &otty);
15807c478bd9Sstevel@tonic-gate     hardtabs = !(otty.c_oflag & TAB3);
15817c478bd9Sstevel@tonic-gate }
15827c478bd9Sstevel@tonic-gate 
15837c478bd9Sstevel@tonic-gate static int
15847c478bd9Sstevel@tonic-gate readch(void)
15857c478bd9Sstevel@tonic-gate {
15867c478bd9Sstevel@tonic-gate         char ch;
15877c478bd9Sstevel@tonic-gate         extern int errno;
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate         if (read (2, &ch, 1) <= 0)
15907c478bd9Sstevel@tonic-gate                 if (errno != EINTR)
15917c478bd9Sstevel@tonic-gate                         end_it(0);		/* clean up before exiting */
15927c478bd9Sstevel@tonic-gate                 else
15937c478bd9Sstevel@tonic-gate                         ch = otty.c_cc[VKILL];
15947c478bd9Sstevel@tonic-gate         return (ch);
15957c478bd9Sstevel@tonic-gate }
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate static char BS = '\b';
15987c478bd9Sstevel@tonic-gate static char CARAT = '^';
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate static void
16017c478bd9Sstevel@tonic-gate ttyin(char buf[], register int nmax, char pchar)
16027c478bd9Sstevel@tonic-gate {
16037c478bd9Sstevel@tonic-gate     register char *sptr;
16047c478bd9Sstevel@tonic-gate     register unsigned char ch;
16057c478bd9Sstevel@tonic-gate     int LengthBuffer[80];
16067c478bd9Sstevel@tonic-gate     int *BufferPointer;
16077c478bd9Sstevel@tonic-gate     int csno;
16087c478bd9Sstevel@tonic-gate     register int slash = 0;
16097c478bd9Sstevel@tonic-gate     int maxlen;
16107c478bd9Sstevel@tonic-gate     char cbuf;
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate     BufferPointer = LengthBuffer;
16137c478bd9Sstevel@tonic-gate     sptr = buf;
16147c478bd9Sstevel@tonic-gate     maxlen = 0;
16157c478bd9Sstevel@tonic-gate     while (sptr - buf < nmax) {
16167c478bd9Sstevel@tonic-gate         if (promptlen > maxlen)
16177c478bd9Sstevel@tonic-gate 	    maxlen = promptlen;
16187c478bd9Sstevel@tonic-gate         ch = readch ();
16197c478bd9Sstevel@tonic-gate         csno = csetno(ch);
16207c478bd9Sstevel@tonic-gate         if (!csno) {
16217c478bd9Sstevel@tonic-gate             if (ch == '\\') {
16227c478bd9Sstevel@tonic-gate                 slash++;
16237c478bd9Sstevel@tonic-gate             } else if ((ch == otty.c_cc[VERASE]) && !slash) {
16247c478bd9Sstevel@tonic-gate                 if (sptr > buf) {
16257c478bd9Sstevel@tonic-gate                     --promptlen;
16267c478bd9Sstevel@tonic-gate                     write (2, &BS, 1);
16277c478bd9Sstevel@tonic-gate 		    sptr -= (*--BufferPointer);
16287c478bd9Sstevel@tonic-gate                     if ((*sptr < ' ' && *sptr != '\n') || *sptr == RUBOUT) {
16297c478bd9Sstevel@tonic-gate                         --promptlen;
16307c478bd9Sstevel@tonic-gate                         write (2, &BS, 1);
16317c478bd9Sstevel@tonic-gate                     }
16327c478bd9Sstevel@tonic-gate                     continue;
16337c478bd9Sstevel@tonic-gate                 } else {
16347c478bd9Sstevel@tonic-gate                     if (!clr_eol)
16357c478bd9Sstevel@tonic-gate 			promptlen = maxlen;
16367c478bd9Sstevel@tonic-gate                     longjmp (restore, 1);
16377c478bd9Sstevel@tonic-gate                 }
16387c478bd9Sstevel@tonic-gate             } else if ((ch == otty.c_cc[VKILL]) && !slash) {
16397c478bd9Sstevel@tonic-gate                 if (hard) {
16407c478bd9Sstevel@tonic-gate                     show(ch);
16417c478bd9Sstevel@tonic-gate                     putchar ('\n');
16427c478bd9Sstevel@tonic-gate                     putchar (pchar);
16437c478bd9Sstevel@tonic-gate                 } else {
16447c478bd9Sstevel@tonic-gate                     putchar ('\r');
16457c478bd9Sstevel@tonic-gate 		    putchar (pchar);
16467c478bd9Sstevel@tonic-gate                     if (clr_eol)
16477c478bd9Sstevel@tonic-gate                         prmpt_erase (1);
16487c478bd9Sstevel@tonic-gate                     promptlen = 1;
16497c478bd9Sstevel@tonic-gate                 }
16507c478bd9Sstevel@tonic-gate                 sptr = buf;
16517c478bd9Sstevel@tonic-gate                 fflush (stdout);
16527c478bd9Sstevel@tonic-gate                 continue;
16537c478bd9Sstevel@tonic-gate             }
16547c478bd9Sstevel@tonic-gate             if (slash && (ch == otty.c_cc[VKILL] || ch == otty.c_cc[VERASE])) {
16557c478bd9Sstevel@tonic-gate                 write (2, &BS, 1);
16567c478bd9Sstevel@tonic-gate 	        sptr -= (*--BufferPointer);
16577c478bd9Sstevel@tonic-gate             }
16587c478bd9Sstevel@tonic-gate             if (ch != '\\')
16597c478bd9Sstevel@tonic-gate                 slash = 0;
16607c478bd9Sstevel@tonic-gate 	    *BufferPointer++ = 1;
16617c478bd9Sstevel@tonic-gate             *sptr++ = ch;
16627c478bd9Sstevel@tonic-gate             if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) {
16637c478bd9Sstevel@tonic-gate                 ch += ch == RUBOUT ? -0100 : 0100;
16647c478bd9Sstevel@tonic-gate                 write (2, &CARAT, 1);
16657c478bd9Sstevel@tonic-gate                 promptlen++;
16667c478bd9Sstevel@tonic-gate             }
16677c478bd9Sstevel@tonic-gate             cbuf = ch;
16687c478bd9Sstevel@tonic-gate             if (ch != '\n' && ch != ESC) {
16697c478bd9Sstevel@tonic-gate                 write (2, &cbuf, 1);
16707c478bd9Sstevel@tonic-gate                 promptlen++;
16717c478bd9Sstevel@tonic-gate             } else
16727c478bd9Sstevel@tonic-gate                 break;
16737c478bd9Sstevel@tonic-gate             /* end of code set 0 */
16747c478bd9Sstevel@tonic-gate         } else {
16757c478bd9Sstevel@tonic-gate 	    int i;
16767c478bd9Sstevel@tonic-gate 	    u_char buffer[5];
16777c478bd9Sstevel@tonic-gate 
16787c478bd9Sstevel@tonic-gate 	    *BufferPointer++ = cw[csno];
16797c478bd9Sstevel@tonic-gate 	    buffer[0] = *sptr++ = ch;
16807c478bd9Sstevel@tonic-gate 	    for(i=1; i<cw[csno]; i++) {
16817c478bd9Sstevel@tonic-gate 	        buffer[i] = *sptr++ = readch();
16827c478bd9Sstevel@tonic-gate 	    }
16837c478bd9Sstevel@tonic-gate 	    buffer[i]='\0';
16847c478bd9Sstevel@tonic-gate 	    write(2, buffer, strlen((char *)buffer));
16857c478bd9Sstevel@tonic-gate 	}
16867c478bd9Sstevel@tonic-gate     }
16877c478bd9Sstevel@tonic-gate     *--sptr = '\0';
16887c478bd9Sstevel@tonic-gate     if (!clr_eol) promptlen = maxlen;
16897c478bd9Sstevel@tonic-gate     if (sptr - buf >= nmax - 1)
16907c478bd9Sstevel@tonic-gate         error (gettext("Line too long"));
16917c478bd9Sstevel@tonic-gate }
16927c478bd9Sstevel@tonic-gate 
16937c478bd9Sstevel@tonic-gate static int
16947c478bd9Sstevel@tonic-gate expand(char *outbuf, char *inbuf)
16957c478bd9Sstevel@tonic-gate {
1696ca3f9453Sas 	char *in_str;
1697ca3f9453Sas 	char *out_str;
1698ca3f9453Sas 	char ch;
1699ca3f9453Sas 	char temp[PATH_MAX];
1700ca3f9453Sas 	int changed = 0;
17017c478bd9Sstevel@tonic-gate 
17027c478bd9Sstevel@tonic-gate     in_str = inbuf;
17037c478bd9Sstevel@tonic-gate     out_str = temp;
17047c478bd9Sstevel@tonic-gate     while ((ch = *in_str++) != '\0')
17057c478bd9Sstevel@tonic-gate         switch (ch) {
17067c478bd9Sstevel@tonic-gate         case '%':
17077c478bd9Sstevel@tonic-gate             if (!no_intty) {
1708ca3f9453Sas 		if (strlcpy(out_str, fnames[fnum], sizeof (temp))
1709ca3f9453Sas 		    >= sizeof (temp))
1710ca3f9453Sas 			error(gettext("Command too long"));
17117c478bd9Sstevel@tonic-gate                 out_str += strlen (fnames[fnum]);
17127c478bd9Sstevel@tonic-gate                 changed++;
17137c478bd9Sstevel@tonic-gate             }
17147c478bd9Sstevel@tonic-gate             else
17157c478bd9Sstevel@tonic-gate                 *out_str++ = ch;
17167c478bd9Sstevel@tonic-gate             break;
17177c478bd9Sstevel@tonic-gate         case '!':
17187c478bd9Sstevel@tonic-gate             if (!shellp)
17197c478bd9Sstevel@tonic-gate                 error (gettext("No previous command to substitute for"));
1720ca3f9453Sas 	    if (strlcpy(out_str, shell_line, sizeof (temp)) >= sizeof (temp))
1721ca3f9453Sas 		error(gettext("Command too long"));
17227c478bd9Sstevel@tonic-gate             out_str += strlen (shell_line);
17237c478bd9Sstevel@tonic-gate             changed++;
17247c478bd9Sstevel@tonic-gate             break;
17257c478bd9Sstevel@tonic-gate         case '\\':
17267c478bd9Sstevel@tonic-gate             if (*in_str == '%' || *in_str == '!') {
17277c478bd9Sstevel@tonic-gate                 *out_str++ = *in_str++;
17287c478bd9Sstevel@tonic-gate                 break;
17297c478bd9Sstevel@tonic-gate             }
1730e608d74dSToomas Soome 	    /* FALLTHROUGH */
17317c478bd9Sstevel@tonic-gate         default:
17327c478bd9Sstevel@tonic-gate             *out_str++ = ch;
17337c478bd9Sstevel@tonic-gate         }
17347c478bd9Sstevel@tonic-gate     *out_str++ = '\0';
1735ca3f9453Sas 	if (strlcpy(outbuf, temp, sizeof (shell_line)) >= sizeof (shell_line))
1736ca3f9453Sas 		error(gettext("Command too long"));
17377c478bd9Sstevel@tonic-gate     return (changed);
17387c478bd9Sstevel@tonic-gate }
17397c478bd9Sstevel@tonic-gate 
17407c478bd9Sstevel@tonic-gate static void
17417c478bd9Sstevel@tonic-gate show(register char ch)
17427c478bd9Sstevel@tonic-gate {
17437c478bd9Sstevel@tonic-gate     char cbuf;
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate     if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) {
17467c478bd9Sstevel@tonic-gate         ch += ch == RUBOUT ? -0100 : 0100;
17477c478bd9Sstevel@tonic-gate         write (2, &CARAT, 1);
17487c478bd9Sstevel@tonic-gate         promptlen++;
17497c478bd9Sstevel@tonic-gate     }
17507c478bd9Sstevel@tonic-gate     cbuf = ch;
17517c478bd9Sstevel@tonic-gate     write (2, &cbuf, 1);
17527c478bd9Sstevel@tonic-gate     promptlen++;
17537c478bd9Sstevel@tonic-gate }
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate static void
17567c478bd9Sstevel@tonic-gate error (char *mess)
17577c478bd9Sstevel@tonic-gate {
17587c478bd9Sstevel@tonic-gate     if (clreol)
17597c478bd9Sstevel@tonic-gate         cleareol ();
17607c478bd9Sstevel@tonic-gate     else
17617c478bd9Sstevel@tonic-gate         kill_line ();
17627c478bd9Sstevel@tonic-gate     promptlen += strlen (mess);
17637c478bd9Sstevel@tonic-gate     if (enter_standout_mode && exit_standout_mode) {
17647c478bd9Sstevel@tonic-gate         putp (enter_standout_mode);
17657c478bd9Sstevel@tonic-gate         pr(mess);
17667c478bd9Sstevel@tonic-gate         putp (exit_standout_mode);
17677c478bd9Sstevel@tonic-gate     }
17687c478bd9Sstevel@tonic-gate     else
17697c478bd9Sstevel@tonic-gate         pr (mess);
17707c478bd9Sstevel@tonic-gate     fflush(stdout);
17717c478bd9Sstevel@tonic-gate     errors++;
17727c478bd9Sstevel@tonic-gate     longjmp (restore, 1);
17737c478bd9Sstevel@tonic-gate }
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate 
17767c478bd9Sstevel@tonic-gate static void
17777c478bd9Sstevel@tonic-gate set_tty(void)
17787c478bd9Sstevel@tonic-gate {
17797c478bd9Sstevel@tonic-gate         ioctl(2, TCGETA, &otty);     /* save old tty modes */
17807c478bd9Sstevel@tonic-gate         ioctl(2, TCGETA, &ntty);
17817c478bd9Sstevel@tonic-gate         ntty.c_lflag &= ~ECHO & ~ICANON;
17827c478bd9Sstevel@tonic-gate         ntty.c_cc[VMIN] = (char)1;
17837c478bd9Sstevel@tonic-gate         ntty.c_cc[VTIME] = (char)0;
17847c478bd9Sstevel@tonic-gate         ioctl (2, TCSETAF, &ntty);        /* set new tty modes */
17857c478bd9Sstevel@tonic-gate }
17867c478bd9Sstevel@tonic-gate 
17877c478bd9Sstevel@tonic-gate static void
17887c478bd9Sstevel@tonic-gate reset_tty(void)
17897c478bd9Sstevel@tonic-gate {
17907c478bd9Sstevel@tonic-gate         ioctl (2, TCSETAF, &otty);        /* reset tty modes */
17917c478bd9Sstevel@tonic-gate }
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate static void
17947c478bd9Sstevel@tonic-gate rdline(register FILE *f)
17957c478bd9Sstevel@tonic-gate {
17967c478bd9Sstevel@tonic-gate     register int c;
17977c478bd9Sstevel@tonic-gate     register char *p;
17987c478bd9Sstevel@tonic-gate 
17997c478bd9Sstevel@tonic-gate     p = Line;
18007c478bd9Sstevel@tonic-gate     while ((c = Getc (f)) != '\n' && c != EOF && p - Line < LINSIZ - 1)
18017c478bd9Sstevel@tonic-gate         *p++ = c;
18027c478bd9Sstevel@tonic-gate     if (c == '\n')
18037c478bd9Sstevel@tonic-gate         Currline++;
18047c478bd9Sstevel@tonic-gate     *p = '\0';
18057c478bd9Sstevel@tonic-gate }
18067c478bd9Sstevel@tonic-gate 
18077c478bd9Sstevel@tonic-gate /* Come here when we get a suspend signal from the terminal */
18087c478bd9Sstevel@tonic-gate 
18097c478bd9Sstevel@tonic-gate /*
18107c478bd9Sstevel@tonic-gate  * sig is put in as a dummy arg to have the compiler not to complain
18117c478bd9Sstevel@tonic-gate  */
18127c478bd9Sstevel@tonic-gate #ifdef SIGTSTP
18137c478bd9Sstevel@tonic-gate /* ARGSUSED */
18147c478bd9Sstevel@tonic-gate void
18157c478bd9Sstevel@tonic-gate onsusp(int sig)
18167c478bd9Sstevel@tonic-gate {
18177c478bd9Sstevel@tonic-gate     /* ignore SIGTTOU so we don't get stopped if csh grabs the tty */
18187c478bd9Sstevel@tonic-gate     signal(SIGTTOU, SIG_IGN);
18197c478bd9Sstevel@tonic-gate     reset_tty ();
18207c478bd9Sstevel@tonic-gate     fflush (stdout);
18217c478bd9Sstevel@tonic-gate     signal(SIGTTOU, SIG_DFL);
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate     /* Send the TSTP signal to suspend our process group */
18247c478bd9Sstevel@tonic-gate     kill (0, SIGTSTP);
18257c478bd9Sstevel@tonic-gate     /* Pause for station break */
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate     /* We're back */
18287c478bd9Sstevel@tonic-gate     signal (SIGTSTP, onsusp);
18297c478bd9Sstevel@tonic-gate     set_tty ();
18307c478bd9Sstevel@tonic-gate     if (inwait)
18317c478bd9Sstevel@tonic-gate             longjmp (restore, 1);
18327c478bd9Sstevel@tonic-gate }
18337c478bd9Sstevel@tonic-gate #endif
1834