xref: /illumos-gate/usr/src/cmd/more/more.c (revision ca3f9453)
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
5*ca3f9453Sas  * Common Development and Distribution License (the "License").
6*ca3f9453Sas  * 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  */
217c478bd9Sstevel@tonic-gate /*
22*ca3f9453Sas  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate  *      @(#) more.c 1.1 88/03/29 more:more.c
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate ** more.c - General purpose tty output filter and file perusal program
507c478bd9Sstevel@tonic-gate **
517c478bd9Sstevel@tonic-gate **      by Eric Shienbrood, UC Berkeley
527c478bd9Sstevel@tonic-gate **
537c478bd9Sstevel@tonic-gate **      modified by Geoff Peck, UCB to add underlining, single spacing
547c478bd9Sstevel@tonic-gate **      modified by John Foderaro, UCB to add -c and MORE environment variable
557c478bd9Sstevel@tonic-gate **      modified by Hans Spiller, Microsoft to handle \r better July 23, 82
567c478bd9Sstevel@tonic-gate **              added ? help command, and -w
577c478bd9Sstevel@tonic-gate **
587c478bd9Sstevel@tonic-gate **      vwh     11 Jan 83       M001
597c478bd9Sstevel@tonic-gate **              modified to handle x.out magic number and magic number
607c478bd9Sstevel@tonic-gate **              byte ordering OTHER than the vax and pdp11.
617c478bd9Sstevel@tonic-gate **      JJD     19 Jan 83       M002
627c478bd9Sstevel@tonic-gate **              - fix distributed on USENET
637c478bd9Sstevel@tonic-gate **                From decvax!ucbvax!dist2 Sun Dec  6 02:58:31 1981
647c478bd9Sstevel@tonic-gate **                Subject: FIXED:  bug in src/more/more.c
657c478bd9Sstevel@tonic-gate **              - fixed bug on terminal with "magic cookie" standout
667c478bd9Sstevel@tonic-gate **                sequences.
677c478bd9Sstevel@tonic-gate **      JJD     14 Feb 83       M003
687c478bd9Sstevel@tonic-gate **              - fix exit status of more
697c478bd9Sstevel@tonic-gate **              - Made first letter of "no more" message uppercase
707c478bd9Sstevel@tonic-gate **      andyp   03 Aug 83       M004    3.0 upgrade
717c478bd9Sstevel@tonic-gate **      - moved <local/uparm.h> to cmd/include.
727c478bd9Sstevel@tonic-gate **      - use UCB, rather than XENIX, stty(2).
737c478bd9Sstevel@tonic-gate **      andyp   30 Nov 83       M005
747c478bd9Sstevel@tonic-gate **      - (thanks to reubenb).  Changed frame variable to static, it is
757c478bd9Sstevel@tonic-gate **        used as a global buffer.  We never saw the bug before because
767c478bd9Sstevel@tonic-gate **        of the depth of the stack.
777c478bd9Sstevel@tonic-gate **      barrys  03 Jul 84       M006
787c478bd9Sstevel@tonic-gate **      - Updated the usage message to include the 's' and 'w' options
797c478bd9Sstevel@tonic-gate **        and to make the 'n' option a separate entry (uncommented).
807c478bd9Sstevel@tonic-gate **      ericc   26 Dec 84       M007
817c478bd9Sstevel@tonic-gate **      - Replaced the constant 0x7fffffffffffffffL with MAXLONG.
827c478bd9Sstevel@tonic-gate **      ericc   25 Jul 85       M008
837c478bd9Sstevel@tonic-gate **      - made "-r" option display control characters as '^x', as documented.
847c478bd9Sstevel@tonic-gate **      - fixed processing of '\b' so that more doesn't terminate when
857c478bd9Sstevel@tonic-gate **        the sequence "\b\n" is encountered.
867c478bd9Sstevel@tonic-gate **      - changed "Hit Rubout ..." to "Hit Del ...", for ibm keyboards.
877c478bd9Sstevel@tonic-gate **	davidby 9 March 1988	Unmarked
887c478bd9Sstevel@tonic-gate **	- replaced all locally defined functions with library equivalents,
897c478bd9Sstevel@tonic-gate **	- changed from termcap to terminfo
907c478bd9Sstevel@tonic-gate **	- included <values.h> for MAXLONG value
917c478bd9Sstevel@tonic-gate **	- removed most ifdef code for V6, V7, and BSD
927c478bd9Sstevel@tonic-gate **	- added /etc/magic support for file type checking
937c478bd9Sstevel@tonic-gate */
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate #include <ctype.h>
967c478bd9Sstevel@tonic-gate #include <signal.h>
977c478bd9Sstevel@tonic-gate #include <errno.h>
987c478bd9Sstevel@tonic-gate #include <sys/types.h>
997c478bd9Sstevel@tonic-gate #include <sys/wait.h>
1007c478bd9Sstevel@tonic-gate #include <curses.h>
1017c478bd9Sstevel@tonic-gate #include <term.h>
1027c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
1037c478bd9Sstevel@tonic-gate #include <setjmp.h>
1047c478bd9Sstevel@tonic-gate #include <sys/stat.h>
1057c478bd9Sstevel@tonic-gate #include <values.h>
1067c478bd9Sstevel@tonic-gate #include <stdlib.h>
1077c478bd9Sstevel@tonic-gate #include <stdarg.h>
1087c478bd9Sstevel@tonic-gate #include <string.h>
1097c478bd9Sstevel@tonic-gate #include <unistd.h>
1107c478bd9Sstevel@tonic-gate #include <libgen.h>
1117c478bd9Sstevel@tonic-gate #include <euc.h>
1127c478bd9Sstevel@tonic-gate #include <getwidth.h>
1137c478bd9Sstevel@tonic-gate #include <locale.h>
1147c478bd9Sstevel@tonic-gate #include <widec.h>
1157c478bd9Sstevel@tonic-gate #include <wctype.h>
1167c478bd9Sstevel@tonic-gate #include <limits.h>
1177c478bd9Sstevel@tonic-gate eucwidth_t wp;
1187c478bd9Sstevel@tonic-gate int     cw[4];
1197c478bd9Sstevel@tonic-gate int     scw[4];
1207c478bd9Sstevel@tonic-gate #include <locale.h>
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /* Help file will eventually go in libpath(more.help) on all systems */
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate #ifdef INGRES
1257c478bd9Sstevel@tonic-gate #define VI              "/usr/bin/vi"
1267c478bd9Sstevel@tonic-gate #define HELPFILE        "/mntp/doucette/more/more.help"
1277c478bd9Sstevel@tonic-gate #define LOCAL_HELP	"/usr/lib/locale/%s/LC_MESSAGES/more.help"
1287c478bd9Sstevel@tonic-gate #endif
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate #ifndef INGRES
1317c478bd9Sstevel@tonic-gate #ifndef HELPFILE
1327c478bd9Sstevel@tonic-gate #define HELPFILE        "/usr/lib/more.help"
1337c478bd9Sstevel@tonic-gate #define LOCAL_HELP	"/usr/lib/locale/%s/LC_MESSAGES/more.help"
1347c478bd9Sstevel@tonic-gate #endif
1357c478bd9Sstevel@tonic-gate #define VI              "vi"
1367c478bd9Sstevel@tonic-gate #endif
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate #define Fopen(s,m)      (Currline = 0,file_pos=0,fopen(s,m))
1397c478bd9Sstevel@tonic-gate #define Ftell(f)        file_pos
1407c478bd9Sstevel@tonic-gate #define Fseek(f,off)    (file_pos=off,fseeko(f,off,0))
1417c478bd9Sstevel@tonic-gate #define Getc(f)         (++file_pos, getc(f))
1427c478bd9Sstevel@tonic-gate #define Ungetc(c,f)     (--file_pos, ungetc(c,f))
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate #define pr(s1)		fputs(s1, stdout)
1457c478bd9Sstevel@tonic-gate #define clreos()	putp(clr_eos)
1467c478bd9Sstevel@tonic-gate #define cleareol()	putp(clr_eol)
1477c478bd9Sstevel@tonic-gate #define home()		putp(cursor_home)
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate #define LINSIZ  512
1507c478bd9Sstevel@tonic-gate #define ctrl(letter)    ((letter) & 077)
1517c478bd9Sstevel@tonic-gate #define RUBOUT  '\177'
1527c478bd9Sstevel@tonic-gate #define ESC     '\033'
1537c478bd9Sstevel@tonic-gate #define QUIT    '\034'
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate struct termio   otty;           /* old tty modes */
1567c478bd9Sstevel@tonic-gate struct termio   ntty;           /* new tty modes */
1577c478bd9Sstevel@tonic-gate off_t           file_pos, file_size;
1587c478bd9Sstevel@tonic-gate int             fnum, no_intty, no_tty;
1597c478bd9Sstevel@tonic-gate int             dum_opt;
1607c478bd9Sstevel@tonic-gate off_t           dlines;
1617c478bd9Sstevel@tonic-gate void end_it(int sig);
1627c478bd9Sstevel@tonic-gate void onquit(int sig);
1637c478bd9Sstevel@tonic-gate void chgwinsz(int sig);
1647c478bd9Sstevel@tonic-gate #ifdef SIGTSTP
1657c478bd9Sstevel@tonic-gate void             onsusp(int sig);
1667c478bd9Sstevel@tonic-gate #endif
1677c478bd9Sstevel@tonic-gate int             nscroll = 11;   /* Number of lines scrolled by 'd' */
1687c478bd9Sstevel@tonic-gate int             fold_opt = 1;   /* Fold long lines */
1697c478bd9Sstevel@tonic-gate int             stop_opt = 1;   /* Stop after form feeds */
1707c478bd9Sstevel@tonic-gate int             ssp_opt = 0;    /* Suppress white space */
1717c478bd9Sstevel@tonic-gate int             ul_opt = 1;     /* Underline as best we can */
1727c478bd9Sstevel@tonic-gate int             cr_opt = 0;     /* show ctrl characters as '^c' */
1737c478bd9Sstevel@tonic-gate int             wait_opt = 0;   /* prompt for exit at eof */
1747c478bd9Sstevel@tonic-gate int             promptlen;
1757c478bd9Sstevel@tonic-gate off_t		Currline;       /* Line we are currently at */
1767c478bd9Sstevel@tonic-gate int             startup = 1;
1777c478bd9Sstevel@tonic-gate int             firstf = 1;
1787c478bd9Sstevel@tonic-gate int             notell = 1;
1797c478bd9Sstevel@tonic-gate int             inwait, Pause, errors;
1807c478bd9Sstevel@tonic-gate int             within; /* true if we are within a file,
1817c478bd9Sstevel@tonic-gate                         false if we are between files */
1827c478bd9Sstevel@tonic-gate int             hard, dumb, noscroll, hardtabs, clreol;
1837c478bd9Sstevel@tonic-gate int             catch_susp;     /* We should catch the SIGTSTP signal */
1847c478bd9Sstevel@tonic-gate char            **fnames;       /* The list of file names */
1857c478bd9Sstevel@tonic-gate int             nfiles;         /* Number of files left to process */
1867c478bd9Sstevel@tonic-gate char            *shell;         /* The name of the shell to use */
1877c478bd9Sstevel@tonic-gate int             shellp;         /* A previous shell command exists */
1887c478bd9Sstevel@tonic-gate char            ch;
1897c478bd9Sstevel@tonic-gate jmp_buf         restore;
1907c478bd9Sstevel@tonic-gate char            obuf[BUFSIZ];   /* stdout buffer */
1917c478bd9Sstevel@tonic-gate char            Line[LINSIZ];   /* Line buffer */
1927c478bd9Sstevel@tonic-gate int             Lpp = 24;       /* lines per page */
1937c478bd9Sstevel@tonic-gate char            *ULenter, *ULexit;      /* enter and exit underline mode */
1947c478bd9Sstevel@tonic-gate int             Mcol = 80;      /* number of columns */
1957c478bd9Sstevel@tonic-gate int             Wrap = 1;       /* set if automargins */
1967c478bd9Sstevel@tonic-gate int		fseeko();
1977c478bd9Sstevel@tonic-gate struct {
1987c478bd9Sstevel@tonic-gate     off_t chrctr, line;
1997c478bd9Sstevel@tonic-gate } context, screen_start;
2007c478bd9Sstevel@tonic-gate int             exitstat = 0;   /* status to use when exiting more */   /*M003*/
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate static void execute(char *filename, char *cmd, ...);
2037c478bd9Sstevel@tonic-gate static void error(char *mess);
2047c478bd9Sstevel@tonic-gate static void wait_eof(void);
2057c478bd9Sstevel@tonic-gate static void prompt(char *filename);
2067c478bd9Sstevel@tonic-gate static void argscan(char *s);
2077c478bd9Sstevel@tonic-gate static void copy_file(register FILE *f);
2087c478bd9Sstevel@tonic-gate static void initterm(void);
2097c478bd9Sstevel@tonic-gate static void do_shell(char *filename);
2107c478bd9Sstevel@tonic-gate static FILE *checkf(register char *fs, int *clearfirst);
2117c478bd9Sstevel@tonic-gate static void screen(register FILE *f, register off_t num_lines);
2127c478bd9Sstevel@tonic-gate static void skiplns(register off_t n, register FILE *f);
2137c478bd9Sstevel@tonic-gate static void skipf(register int nskip);
2147c478bd9Sstevel@tonic-gate static int readch(void);
2157c478bd9Sstevel@tonic-gate static void prmpt_erase(register int col);
2167c478bd9Sstevel@tonic-gate static void kill_line(void);
2177c478bd9Sstevel@tonic-gate static void prbuf(register char *s, register int n);
2187c478bd9Sstevel@tonic-gate static void search(char buf[], FILE *file, register off_t n);
2197c478bd9Sstevel@tonic-gate static void doclear(void);
2207c478bd9Sstevel@tonic-gate static void ttyin(char buf[], register int nmax, char pchar);
2217c478bd9Sstevel@tonic-gate static int expand(char *outbuf, char *inbuf);
2227c478bd9Sstevel@tonic-gate static void show(register char ch);
2237c478bd9Sstevel@tonic-gate static void set_tty(void);
2247c478bd9Sstevel@tonic-gate static void reset_tty(void);
2257c478bd9Sstevel@tonic-gate static void rdline(register FILE *f);
2267c478bd9Sstevel@tonic-gate static off_t command(char *filename, register FILE *f);
2277c478bd9Sstevel@tonic-gate static int getline(register FILE *f, int *length);
2287c478bd9Sstevel@tonic-gate static int number(char *cmd);
2297c478bd9Sstevel@tonic-gate static int colon(char *filename, int cmd, off_t nlines);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate int
2327c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
2337c478bd9Sstevel@tonic-gate {
2347c478bd9Sstevel@tonic-gate     register FILE       *f;
2357c478bd9Sstevel@tonic-gate     register char       *s;
2367c478bd9Sstevel@tonic-gate     register char       *p;
2377c478bd9Sstevel@tonic-gate     register int	ch;
2387c478bd9Sstevel@tonic-gate     register off_t      left;
2397c478bd9Sstevel@tonic-gate     int                 prnames = 0;
2407c478bd9Sstevel@tonic-gate     int                 initopt = 0;
2417c478bd9Sstevel@tonic-gate     int                 srchopt = 0;
2427c478bd9Sstevel@tonic-gate     int                 clearit = 0;
2437c478bd9Sstevel@tonic-gate     off_t               initline;
2447c478bd9Sstevel@tonic-gate     char                initbuf[80];
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate     setlocale( LC_ALL, "" );
2477c478bd9Sstevel@tonic-gate     getwidth(&wp);
2487c478bd9Sstevel@tonic-gate     cw[0] = 1;
2497c478bd9Sstevel@tonic-gate     cw[1] = wp._eucw1;
2507c478bd9Sstevel@tonic-gate     cw[2] = wp._eucw2+1;
2517c478bd9Sstevel@tonic-gate     cw[3] = wp._eucw3+1;
2527c478bd9Sstevel@tonic-gate     scw[0] = 1;
2537c478bd9Sstevel@tonic-gate     scw[1] = wp._scrw1;
2547c478bd9Sstevel@tonic-gate     scw[2] = wp._scrw2;
2557c478bd9Sstevel@tonic-gate     scw[3] = wp._scrw3;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate     nfiles = argc;
2587c478bd9Sstevel@tonic-gate     fnames = argv;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate     (void) setlocale(LC_ALL,"");
2617c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
2627c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
2637c478bd9Sstevel@tonic-gate #endif
2647c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate     initterm ();
2677c478bd9Sstevel@tonic-gate     if(s = getenv("MORE")) argscan(s);
2687c478bd9Sstevel@tonic-gate     while (--nfiles > 0) {
2697c478bd9Sstevel@tonic-gate         if ((ch = (*++fnames)[0]) == '-') {
2707c478bd9Sstevel@tonic-gate             argscan(*fnames+1);
2717c478bd9Sstevel@tonic-gate         }
2727c478bd9Sstevel@tonic-gate         else if (ch == '+') {
2737c478bd9Sstevel@tonic-gate             s = *fnames;
2747c478bd9Sstevel@tonic-gate             if (*++s == '/') {
2757c478bd9Sstevel@tonic-gate                 srchopt++;
2767c478bd9Sstevel@tonic-gate                 for (++s, p = initbuf; p < initbuf + 79 && *s != '\0';)
2777c478bd9Sstevel@tonic-gate                     *p++ = *s++;
2787c478bd9Sstevel@tonic-gate                 *p = '\0';
2797c478bd9Sstevel@tonic-gate             }
2807c478bd9Sstevel@tonic-gate             else {
2817c478bd9Sstevel@tonic-gate                 initopt++;
2827c478bd9Sstevel@tonic-gate                 for (initline = 0; *s != '\0'; s++)
2837c478bd9Sstevel@tonic-gate                     if (isdigit (*s))
2847c478bd9Sstevel@tonic-gate                         initline = initline*10 + *s -'0';
2857c478bd9Sstevel@tonic-gate                 --initline;
2867c478bd9Sstevel@tonic-gate             }
2877c478bd9Sstevel@tonic-gate         }
2887c478bd9Sstevel@tonic-gate         else break;
2897c478bd9Sstevel@tonic-gate     }
2907c478bd9Sstevel@tonic-gate     /* allow clreol only if cursor_home and clr_eol and clr_eos strings are
2917c478bd9Sstevel@tonic-gate      *  defined, and in that case, make sure we are in noscroll mode
2927c478bd9Sstevel@tonic-gate      */
2937c478bd9Sstevel@tonic-gate     if(clreol)
2947c478bd9Sstevel@tonic-gate     {
2957c478bd9Sstevel@tonic-gate         if (!cursor_home || !clr_eol || !clr_eos) {
2967c478bd9Sstevel@tonic-gate            	clreol = 0;
2977c478bd9Sstevel@tonic-gate 	}
2987c478bd9Sstevel@tonic-gate         else noscroll = 1;
2997c478bd9Sstevel@tonic-gate     }
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate     if (dlines == 0)
3027c478bd9Sstevel@tonic-gate         dlines =(off_t) (Lpp - (noscroll ? 1 : 2));
3037c478bd9Sstevel@tonic-gate     left = dlines;
3047c478bd9Sstevel@tonic-gate     if (nfiles > 1)
3057c478bd9Sstevel@tonic-gate         prnames++;
3067c478bd9Sstevel@tonic-gate     if (!no_intty && nfiles == 0) {
3077c478bd9Sstevel@tonic-gate 	fprintf(stderr, gettext("Usage: %s\
3087c478bd9Sstevel@tonic-gate  [-cdflrsuw] [-lines] [+linenumber] [+/pattern] [filename ...].\n")
3097c478bd9Sstevel@tonic-gate 	, argv[0]);
3107c478bd9Sstevel@tonic-gate         exit(1);
3117c478bd9Sstevel@tonic-gate     }
3127c478bd9Sstevel@tonic-gate     else
3137c478bd9Sstevel@tonic-gate         f = stdin;
3147c478bd9Sstevel@tonic-gate     if (!no_tty) {
3157c478bd9Sstevel@tonic-gate         signal(SIGQUIT, onquit);
3167c478bd9Sstevel@tonic-gate         signal(SIGINT, end_it);
3177c478bd9Sstevel@tonic-gate         signal(SIGWINCH, chgwinsz);
3187c478bd9Sstevel@tonic-gate #ifdef SIGTSTP
3197c478bd9Sstevel@tonic-gate         if (signal (SIGTSTP, SIG_IGN) == SIG_DFL) {
3207c478bd9Sstevel@tonic-gate             signal(SIGTSTP, onsusp);
3217c478bd9Sstevel@tonic-gate             catch_susp++;
3227c478bd9Sstevel@tonic-gate         }
3237c478bd9Sstevel@tonic-gate #endif
3247c478bd9Sstevel@tonic-gate         set_tty();
3257c478bd9Sstevel@tonic-gate     }
3267c478bd9Sstevel@tonic-gate     if (no_intty) {
3277c478bd9Sstevel@tonic-gate         if (no_tty)
3287c478bd9Sstevel@tonic-gate             copy_file (stdin);
3297c478bd9Sstevel@tonic-gate         else {
3307c478bd9Sstevel@tonic-gate             if ((ch = Getc (f)) == '\f')
3317c478bd9Sstevel@tonic-gate                 doclear();
3327c478bd9Sstevel@tonic-gate             else {
3337c478bd9Sstevel@tonic-gate                 Ungetc (ch, f);
3347c478bd9Sstevel@tonic-gate                 if (noscroll && (ch != EOF)) {
3357c478bd9Sstevel@tonic-gate                     if (clreol)
3367c478bd9Sstevel@tonic-gate                         home ();
3377c478bd9Sstevel@tonic-gate                     else
3387c478bd9Sstevel@tonic-gate                         doclear ();
3397c478bd9Sstevel@tonic-gate                 }
3407c478bd9Sstevel@tonic-gate             }
3417c478bd9Sstevel@tonic-gate             if (!setjmp(restore)) {
3427c478bd9Sstevel@tonic-gate                 if (srchopt) {
3437c478bd9Sstevel@tonic-gate                     search (initbuf, stdin,(off_t) 1);
3447c478bd9Sstevel@tonic-gate                     if (noscroll)
3457c478bd9Sstevel@tonic-gate                         left--;
3467c478bd9Sstevel@tonic-gate                     }
3477c478bd9Sstevel@tonic-gate                 else if (initopt)
3487c478bd9Sstevel@tonic-gate                     skiplns (initline, stdin);
3497c478bd9Sstevel@tonic-gate                 }
3507c478bd9Sstevel@tonic-gate             else
3517c478bd9Sstevel@tonic-gate                 left = command(NULL, f);
3527c478bd9Sstevel@tonic-gate             screen (stdin, left);
3537c478bd9Sstevel@tonic-gate         }
3547c478bd9Sstevel@tonic-gate         no_intty = 0;
3557c478bd9Sstevel@tonic-gate         prnames++;
3567c478bd9Sstevel@tonic-gate         firstf = 0;
3577c478bd9Sstevel@tonic-gate     }
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate     while (fnum < nfiles) {
3607c478bd9Sstevel@tonic-gate         if ((f = checkf (fnames[fnum], &clearit)) != NULL) {
3617c478bd9Sstevel@tonic-gate             context.line = context.chrctr = 0;
3627c478bd9Sstevel@tonic-gate             Currline = 0;
3637c478bd9Sstevel@tonic-gate             if (firstf) setjmp (restore);
3647c478bd9Sstevel@tonic-gate             if (firstf) {
3657c478bd9Sstevel@tonic-gate                 firstf = 0;
3667c478bd9Sstevel@tonic-gate                 if (srchopt)
3677c478bd9Sstevel@tonic-gate                 {
3687c478bd9Sstevel@tonic-gate                     search (initbuf, f,(off_t) 1);
3697c478bd9Sstevel@tonic-gate                     if (noscroll)
3707c478bd9Sstevel@tonic-gate                         left--;
3717c478bd9Sstevel@tonic-gate                 }
3727c478bd9Sstevel@tonic-gate                 else if (initopt)
3737c478bd9Sstevel@tonic-gate                     skiplns (initline, f);
3747c478bd9Sstevel@tonic-gate             }
3757c478bd9Sstevel@tonic-gate             else if (fnum < nfiles && !no_tty) {
3767c478bd9Sstevel@tonic-gate                 setjmp (restore);
3777c478bd9Sstevel@tonic-gate                 left = command (fnames[fnum], f);
3787c478bd9Sstevel@tonic-gate             }
3797c478bd9Sstevel@tonic-gate             if (left != 0) {
3807c478bd9Sstevel@tonic-gate                 if ((noscroll || clearit) && (file_size != LLONG_MAX))
3817c478bd9Sstevel@tonic-gate                     if (clreol)
3827c478bd9Sstevel@tonic-gate                         home ();
3837c478bd9Sstevel@tonic-gate                     else
3847c478bd9Sstevel@tonic-gate                         doclear ();
3857c478bd9Sstevel@tonic-gate                 if (prnames) {
3867c478bd9Sstevel@tonic-gate                     if (ceol_standout_glitch)
3877c478bd9Sstevel@tonic-gate                         prmpt_erase (0);
3887c478bd9Sstevel@tonic-gate                     if (clreol)
3897c478bd9Sstevel@tonic-gate                         cleareol ();
3907c478bd9Sstevel@tonic-gate                     pr("::::::::::::::");
3917c478bd9Sstevel@tonic-gate                     if (promptlen > 14)
3927c478bd9Sstevel@tonic-gate                         prmpt_erase (14);
3937c478bd9Sstevel@tonic-gate                     printf ("\n");
3947c478bd9Sstevel@tonic-gate                     if(clreol) cleareol();
3957c478bd9Sstevel@tonic-gate                     printf("%s\n", fnames[fnum]);
3967c478bd9Sstevel@tonic-gate                     if(clreol) cleareol();
3977c478bd9Sstevel@tonic-gate                     pr("::::::::::::::\n");
3987c478bd9Sstevel@tonic-gate                     if (left > (off_t)(Lpp - 4))
3997c478bd9Sstevel@tonic-gate                         left =(off_t)(Lpp - 4);
4007c478bd9Sstevel@tonic-gate                 }
4017c478bd9Sstevel@tonic-gate                 if (no_tty)
4027c478bd9Sstevel@tonic-gate                     copy_file (f);
4037c478bd9Sstevel@tonic-gate                 else {
4047c478bd9Sstevel@tonic-gate                     within++;
4057c478bd9Sstevel@tonic-gate                     screen(f, left);
4067c478bd9Sstevel@tonic-gate                     within = 0;
4077c478bd9Sstevel@tonic-gate                 }
4087c478bd9Sstevel@tonic-gate             }
4097c478bd9Sstevel@tonic-gate             setjmp (restore);
4107c478bd9Sstevel@tonic-gate             fflush(stdout);
4117c478bd9Sstevel@tonic-gate             fclose(f);
4127c478bd9Sstevel@tonic-gate             screen_start.line = screen_start.chrctr = 0LL;
4137c478bd9Sstevel@tonic-gate             context.line = context.chrctr = 0LL;
4147c478bd9Sstevel@tonic-gate         } else
4157c478bd9Sstevel@tonic-gate             exitstat |= 1;                      /*M003*/
4167c478bd9Sstevel@tonic-gate         fnum++;
4177c478bd9Sstevel@tonic-gate         firstf = 0;
4187c478bd9Sstevel@tonic-gate     }
4197c478bd9Sstevel@tonic-gate     if (wait_opt) wait_eof();
4207c478bd9Sstevel@tonic-gate     reset_tty ();
4217c478bd9Sstevel@tonic-gate     return (exitstat);                             /*M003*/
4227c478bd9Sstevel@tonic-gate }
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate static void
4257c478bd9Sstevel@tonic-gate argscan(char *s)
4267c478bd9Sstevel@tonic-gate {
4277c478bd9Sstevel@tonic-gate             for (dlines = 0; *s != '\0'; s++)
4287c478bd9Sstevel@tonic-gate                 if (isdigit(*s))
4297c478bd9Sstevel@tonic-gate                     dlines = dlines*10 + *s - '0';
4307c478bd9Sstevel@tonic-gate                 else if (*s == 'd')
4317c478bd9Sstevel@tonic-gate                     dum_opt = 1;
4327c478bd9Sstevel@tonic-gate                 else if (*s == 'l')
4337c478bd9Sstevel@tonic-gate                     stop_opt = 0;
4347c478bd9Sstevel@tonic-gate                 else if (*s == 'f')
4357c478bd9Sstevel@tonic-gate                     fold_opt = 0;
4367c478bd9Sstevel@tonic-gate                 else if (*s == 'p')
4377c478bd9Sstevel@tonic-gate                     noscroll++;
4387c478bd9Sstevel@tonic-gate                 else if (*s == 'c')
4397c478bd9Sstevel@tonic-gate                     clreol++;
4407c478bd9Sstevel@tonic-gate                 else if (*s == 's')
4417c478bd9Sstevel@tonic-gate                     ssp_opt = 1;
4427c478bd9Sstevel@tonic-gate                 else if (*s == 'u')
4437c478bd9Sstevel@tonic-gate                     ul_opt = 0;
4447c478bd9Sstevel@tonic-gate                 else if (*s == 'r')
4457c478bd9Sstevel@tonic-gate                     cr_opt = 1;
4467c478bd9Sstevel@tonic-gate                 else if (*s == 'w')
4477c478bd9Sstevel@tonic-gate                     wait_opt = 1;
4487c478bd9Sstevel@tonic-gate }
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate /*
4527c478bd9Sstevel@tonic-gate ** Check whether the file named by fs is a file which the user may
4537c478bd9Sstevel@tonic-gate ** access.  If it is, return the opened file. Otherwise return NULL.
4547c478bd9Sstevel@tonic-gate */
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate static FILE *
4577c478bd9Sstevel@tonic-gate checkf(register char *fs, int *clearfirst)
4587c478bd9Sstevel@tonic-gate {
4597c478bd9Sstevel@tonic-gate     struct stat stbuf;
4607c478bd9Sstevel@tonic-gate     register FILE *f;
4617c478bd9Sstevel@tonic-gate     int c;
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate     if (stat (fs, &stbuf) == -1) {
4647c478bd9Sstevel@tonic-gate         fflush(stdout);
4657c478bd9Sstevel@tonic-gate         if (clreol)
4667c478bd9Sstevel@tonic-gate             cleareol ();
4677c478bd9Sstevel@tonic-gate         perror(fs);
4687c478bd9Sstevel@tonic-gate         return (NULL);
4697c478bd9Sstevel@tonic-gate     }
4707c478bd9Sstevel@tonic-gate     if ((stbuf.st_mode & S_IFMT) == S_IFDIR) {
4717c478bd9Sstevel@tonic-gate         printf(gettext("\n*** %s: directory ***\n\n"), fs);
4727c478bd9Sstevel@tonic-gate         return (NULL);
4737c478bd9Sstevel@tonic-gate     }
4747c478bd9Sstevel@tonic-gate     if ((f=Fopen(fs, "r")) == NULL) {
4757c478bd9Sstevel@tonic-gate         fflush(stdout);
4767c478bd9Sstevel@tonic-gate         perror(fs);
4777c478bd9Sstevel@tonic-gate         return (NULL);
4787c478bd9Sstevel@tonic-gate     }
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate     if ((c = Getc(f)) == '\f')                  /* end M001 */
4817c478bd9Sstevel@tonic-gate         *clearfirst = 1;
4827c478bd9Sstevel@tonic-gate     else {
4837c478bd9Sstevel@tonic-gate         *clearfirst = 0;
4847c478bd9Sstevel@tonic-gate         Ungetc (c, f);
4857c478bd9Sstevel@tonic-gate     }
4867c478bd9Sstevel@tonic-gate     if ((file_size = (off_t)stbuf.st_size) == 0)
4877c478bd9Sstevel@tonic-gate         file_size = LLONG_MAX;
4887c478bd9Sstevel@tonic-gate     return (f);
4897c478bd9Sstevel@tonic-gate }
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate /*
4927c478bd9Sstevel@tonic-gate ** Print out the contents of the file f, one screenful at a time.
4937c478bd9Sstevel@tonic-gate */
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate #define STOP -10
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate static void
4987c478bd9Sstevel@tonic-gate screen(register FILE *f, register off_t num_lines)
4997c478bd9Sstevel@tonic-gate {
5007c478bd9Sstevel@tonic-gate     register int c;
5017c478bd9Sstevel@tonic-gate     register int nchars;
5027c478bd9Sstevel@tonic-gate     int length;                 /* length of current line */
5037c478bd9Sstevel@tonic-gate     static int prev_len = 1;    /* length of previous line */
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate     for (;;) {
5067c478bd9Sstevel@tonic-gate         while (num_lines > 0 && !Pause) {
5077c478bd9Sstevel@tonic-gate             if ((nchars = getline (f, &length)) == EOF)
5087c478bd9Sstevel@tonic-gate             {
5097c478bd9Sstevel@tonic-gate                 if (clreol) clreos();
5107c478bd9Sstevel@tonic-gate                 return;
5117c478bd9Sstevel@tonic-gate             }
5127c478bd9Sstevel@tonic-gate             if (ssp_opt && length == 0 && prev_len == 0)
5137c478bd9Sstevel@tonic-gate                 continue;
5147c478bd9Sstevel@tonic-gate             prev_len = length;
5157c478bd9Sstevel@tonic-gate             if (ceol_standout_glitch ||
5167c478bd9Sstevel@tonic-gate 		(enter_standout_mode && *enter_standout_mode == ' ')
5177c478bd9Sstevel@tonic-gate 		&& promptlen > 0)
5187c478bd9Sstevel@tonic-gate                 prmpt_erase (0);
5197c478bd9Sstevel@tonic-gate             /* must clear before drawing line since tabs on some terminals
5207c478bd9Sstevel@tonic-gate              * do not erase what they tab over.
5217c478bd9Sstevel@tonic-gate              */
5227c478bd9Sstevel@tonic-gate             if (clreol)
5237c478bd9Sstevel@tonic-gate                 cleareol ();
5247c478bd9Sstevel@tonic-gate             prbuf (Line, length);
5257c478bd9Sstevel@tonic-gate             if (nchars < promptlen)
5267c478bd9Sstevel@tonic-gate                 prmpt_erase (nchars); /* prmpt_erase () sets promptlen to 0 */
5277c478bd9Sstevel@tonic-gate             else promptlen = 0;
5287c478bd9Sstevel@tonic-gate             /* is this needed?
5297c478bd9Sstevel@tonic-gate              * if (clreol)
5307c478bd9Sstevel@tonic-gate              *  cleareol(); */    /* must clear again in case we wrapped */
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate             if (nchars < Mcol || !fold_opt)
5337c478bd9Sstevel@tonic-gate                 putchar('\n');
5347c478bd9Sstevel@tonic-gate             if (nchars == STOP)
5357c478bd9Sstevel@tonic-gate                 break;
5367c478bd9Sstevel@tonic-gate             num_lines--;
5377c478bd9Sstevel@tonic-gate         }
5387c478bd9Sstevel@tonic-gate         fflush(stdout);
5397c478bd9Sstevel@tonic-gate         if ((c = Getc(f)) == EOF)
5407c478bd9Sstevel@tonic-gate         {
5417c478bd9Sstevel@tonic-gate             if (clreol) clreos ();
5427c478bd9Sstevel@tonic-gate             return;
5437c478bd9Sstevel@tonic-gate         }
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate         if (Pause && clreol)
5467c478bd9Sstevel@tonic-gate             clreos ();
5477c478bd9Sstevel@tonic-gate         Ungetc (c, f);
5487c478bd9Sstevel@tonic-gate         setjmp (restore);
5497c478bd9Sstevel@tonic-gate         Pause = 0; startup = 0;
5507c478bd9Sstevel@tonic-gate         if ((num_lines = command (NULL, f)) == 0)
5517c478bd9Sstevel@tonic-gate             return;
5527c478bd9Sstevel@tonic-gate         if (hard && promptlen > 0)
5537c478bd9Sstevel@tonic-gate                 prmpt_erase (0);
5547c478bd9Sstevel@tonic-gate         if (noscroll && num_lines == dlines) {
5557c478bd9Sstevel@tonic-gate             if (clreol)
5567c478bd9Sstevel@tonic-gate                 home();
5577c478bd9Sstevel@tonic-gate             else
5587c478bd9Sstevel@tonic-gate                 doclear ();
5597c478bd9Sstevel@tonic-gate         }
5607c478bd9Sstevel@tonic-gate         screen_start.line = Currline;
5617c478bd9Sstevel@tonic-gate         screen_start.chrctr = Ftell (f);
5627c478bd9Sstevel@tonic-gate     }
5637c478bd9Sstevel@tonic-gate }
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate /*
5667c478bd9Sstevel@tonic-gate ** Come here if a quit signal is received
5677c478bd9Sstevel@tonic-gate */
5687c478bd9Sstevel@tonic-gate /*
5697c478bd9Sstevel@tonic-gate  * sig is put in as a dummy arg to have the compiler not to complain
5707c478bd9Sstevel@tonic-gate  */
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate /* ARGSUSED */
5737c478bd9Sstevel@tonic-gate void
5747c478bd9Sstevel@tonic-gate onquit(int sig)
5757c478bd9Sstevel@tonic-gate {
5767c478bd9Sstevel@tonic-gate     signal(SIGQUIT, SIG_IGN);
5777c478bd9Sstevel@tonic-gate     if (!inwait) {
5787c478bd9Sstevel@tonic-gate         putchar ('\n');
5797c478bd9Sstevel@tonic-gate         if (!startup) {
5807c478bd9Sstevel@tonic-gate             signal(SIGQUIT, onquit);
5817c478bd9Sstevel@tonic-gate             longjmp (restore, 1);
5827c478bd9Sstevel@tonic-gate         }
5837c478bd9Sstevel@tonic-gate         else
5847c478bd9Sstevel@tonic-gate             Pause++;
5857c478bd9Sstevel@tonic-gate     }
5867c478bd9Sstevel@tonic-gate     else if (!dum_opt && notell) {
5877c478bd9Sstevel@tonic-gate         write (2, gettext("[Use q or Q to quit]"), 20);
5887c478bd9Sstevel@tonic-gate         promptlen += 20;
5897c478bd9Sstevel@tonic-gate         notell = 0;
5907c478bd9Sstevel@tonic-gate     }
5917c478bd9Sstevel@tonic-gate     signal(SIGQUIT, onquit);
5927c478bd9Sstevel@tonic-gate }
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate /*
5957c478bd9Sstevel@tonic-gate ** Come here if a signal for a window size change is received
5967c478bd9Sstevel@tonic-gate */
5977c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5987c478bd9Sstevel@tonic-gate void
5997c478bd9Sstevel@tonic-gate chgwinsz(int sig)
6007c478bd9Sstevel@tonic-gate {
6017c478bd9Sstevel@tonic-gate     struct winsize win;
6027c478bd9Sstevel@tonic-gate 
6037c478bd9Sstevel@tonic-gate     (void) signal(SIGWINCH, SIG_IGN);
6047c478bd9Sstevel@tonic-gate     if (ioctl(fileno(stdout), TIOCGWINSZ, &win) != -1) {
6057c478bd9Sstevel@tonic-gate 	if (win.ws_row != 0) {
6067c478bd9Sstevel@tonic-gate 	    Lpp = win.ws_row;
6077c478bd9Sstevel@tonic-gate 	    nscroll = Lpp/2 - 1;
6087c478bd9Sstevel@tonic-gate 	    if (nscroll <= 0)
6097c478bd9Sstevel@tonic-gate 		nscroll = 1;
6107c478bd9Sstevel@tonic-gate 	    dlines = (off_t)(Lpp - (noscroll ? 1 : 2));
6117c478bd9Sstevel@tonic-gate 	}
6127c478bd9Sstevel@tonic-gate 	if (win.ws_col != 0)
6137c478bd9Sstevel@tonic-gate 	    Mcol = win.ws_col;
6147c478bd9Sstevel@tonic-gate     }
6157c478bd9Sstevel@tonic-gate     (void) signal(SIGWINCH, chgwinsz);
6167c478bd9Sstevel@tonic-gate }
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate /*
6197c478bd9Sstevel@tonic-gate ** Clean up terminal state and exit. Also come here if interrupt signal received
6207c478bd9Sstevel@tonic-gate */
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate /*
6237c478bd9Sstevel@tonic-gate  * sig is put in as a dummy arg to have the compiler not to complain
6247c478bd9Sstevel@tonic-gate  */
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate /* ARGSUSED */
6277c478bd9Sstevel@tonic-gate void
6287c478bd9Sstevel@tonic-gate end_it(int sig)
6297c478bd9Sstevel@tonic-gate {
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate     reset_tty ();
6327c478bd9Sstevel@tonic-gate     if (clreol) {
6337c478bd9Sstevel@tonic-gate         putchar ('\r');
6347c478bd9Sstevel@tonic-gate         clreos ();
6357c478bd9Sstevel@tonic-gate         fflush (stdout);
6367c478bd9Sstevel@tonic-gate     }
6377c478bd9Sstevel@tonic-gate     else if (!clreol && (promptlen > 0)) {
6387c478bd9Sstevel@tonic-gate         kill_line ();
6397c478bd9Sstevel@tonic-gate         fflush (stdout);
6407c478bd9Sstevel@tonic-gate     }
6417c478bd9Sstevel@tonic-gate     else
6427c478bd9Sstevel@tonic-gate         write (2, "\n", 1);
6437c478bd9Sstevel@tonic-gate     _exit(exitstat);                    /*M003*/
6447c478bd9Sstevel@tonic-gate }
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate static void
6477c478bd9Sstevel@tonic-gate copy_file(register FILE *f)
6487c478bd9Sstevel@tonic-gate {
6497c478bd9Sstevel@tonic-gate     register int c;
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate     while ((c = getc(f)) != EOF)
6527c478bd9Sstevel@tonic-gate         putchar(c);
6537c478bd9Sstevel@tonic-gate }
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate static char Bell = ctrl('G');
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate /* See whether the last component of the path name "path" is equal to the
6597c478bd9Sstevel@tonic-gate ** string "string"
6607c478bd9Sstevel@tonic-gate */
6617c478bd9Sstevel@tonic-gate 
66278eb75caSchin int
66378eb75caSchin tailequ(char *path, char *string)
6647c478bd9Sstevel@tonic-gate {
6657c478bd9Sstevel@tonic-gate 	return (!strcmp(basename(path), string));
6667c478bd9Sstevel@tonic-gate }
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate static void
6697c478bd9Sstevel@tonic-gate prompt(char *filename)
6707c478bd9Sstevel@tonic-gate {
6717c478bd9Sstevel@tonic-gate     if (clreol)
6727c478bd9Sstevel@tonic-gate         cleareol ();
6737c478bd9Sstevel@tonic-gate     else if (promptlen > 0)
6747c478bd9Sstevel@tonic-gate         kill_line ();
6757c478bd9Sstevel@tonic-gate     if (!hard) {
6767c478bd9Sstevel@tonic-gate         promptlen = 8;
6777c478bd9Sstevel@tonic-gate         if (enter_standout_mode && exit_standout_mode)
6787c478bd9Sstevel@tonic-gate             putp (enter_standout_mode);
6797c478bd9Sstevel@tonic-gate         if (clreol)
6807c478bd9Sstevel@tonic-gate             cleareol ();
6817c478bd9Sstevel@tonic-gate         pr(gettext("--More--"));
6827c478bd9Sstevel@tonic-gate         if (filename != NULL) {
6837c478bd9Sstevel@tonic-gate             promptlen += printf (gettext("(Next file: %s)"), filename);
6847c478bd9Sstevel@tonic-gate         }
6857c478bd9Sstevel@tonic-gate         else if (!no_intty) {
6867c478bd9Sstevel@tonic-gate             promptlen += printf ("(%d%%)", (int)((file_pos * 100) / file_size));
6877c478bd9Sstevel@tonic-gate         }
6887c478bd9Sstevel@tonic-gate         if (dum_opt) {
6897c478bd9Sstevel@tonic-gate             promptlen += pr(gettext("[Hit space to continue, Del to abort]"));
6907c478bd9Sstevel@tonic-gate         }
6917c478bd9Sstevel@tonic-gate         if (enter_standout_mode && exit_standout_mode)
6927c478bd9Sstevel@tonic-gate             putp (exit_standout_mode);
6937c478bd9Sstevel@tonic-gate         if (clreol) clreos ();
6947c478bd9Sstevel@tonic-gate         fflush(stdout);
6957c478bd9Sstevel@tonic-gate     }
6967c478bd9Sstevel@tonic-gate     else
6977c478bd9Sstevel@tonic-gate         write (2, &Bell, 1);
6987c478bd9Sstevel@tonic-gate     inwait++;
6997c478bd9Sstevel@tonic-gate }
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate /*
7027c478bd9Sstevel@tonic-gate  * when run from another program or a shell script, it is
7037c478bd9Sstevel@tonic-gate  * sometimes useful to prevent the next program from scrolling
7047c478bd9Sstevel@tonic-gate  * us off the screen before we get a chance to read this page.
7057c478bd9Sstevel@tonic-gate  *                      -Hans, July 24, 1982
7067c478bd9Sstevel@tonic-gate  */
7077c478bd9Sstevel@tonic-gate static void
7087c478bd9Sstevel@tonic-gate wait_eof(void)
7097c478bd9Sstevel@tonic-gate {
7107c478bd9Sstevel@tonic-gate         if (enter_standout_mode && exit_standout_mode)
7117c478bd9Sstevel@tonic-gate                 putp (enter_standout_mode);
7127c478bd9Sstevel@tonic-gate         promptlen = pr(gettext("--No more--"));          /*M003*/
7137c478bd9Sstevel@tonic-gate         if (dum_opt)
7147c478bd9Sstevel@tonic-gate                 promptlen += pr(gettext("[Hit any key to continue]"));
7157c478bd9Sstevel@tonic-gate         if (enter_standout_mode && exit_standout_mode)
7167c478bd9Sstevel@tonic-gate                 putp(exit_standout_mode);
7177c478bd9Sstevel@tonic-gate         if (clreol) clreos();
7187c478bd9Sstevel@tonic-gate         fflush(stdout);
7197c478bd9Sstevel@tonic-gate         readch();
7207c478bd9Sstevel@tonic-gate         prmpt_erase (0);
7217c478bd9Sstevel@tonic-gate         fflush(stdout);
7227c478bd9Sstevel@tonic-gate }
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate /*
7257c478bd9Sstevel@tonic-gate ** Get a logical line
7267c478bd9Sstevel@tonic-gate */
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate static int
7297c478bd9Sstevel@tonic-gate getline(register FILE *f, int *length)
7307c478bd9Sstevel@tonic-gate {
7317c478bd9Sstevel@tonic-gate     register int        c;
7327c478bd9Sstevel@tonic-gate     register char       *p;
7337c478bd9Sstevel@tonic-gate     register int        column;
7347c478bd9Sstevel@tonic-gate     static int          colflg;
7357c478bd9Sstevel@tonic-gate     register int        oldcolumn;
7367c478bd9Sstevel@tonic-gate     int			csno;
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate     p = Line;
7397c478bd9Sstevel@tonic-gate     column = 0;
7407c478bd9Sstevel@tonic-gate     oldcolumn = 0;
7417c478bd9Sstevel@tonic-gate     c = Getc (f);
7427c478bd9Sstevel@tonic-gate     if (colflg && c == '\n') {
7437c478bd9Sstevel@tonic-gate         Currline++;
7447c478bd9Sstevel@tonic-gate         c = Getc (f);
7457c478bd9Sstevel@tonic-gate     }
7467c478bd9Sstevel@tonic-gate     while (p < &Line[LINSIZ - 1]) {
7477c478bd9Sstevel@tonic-gate 	csno = csetno(c);
7487c478bd9Sstevel@tonic-gate         if (c == EOF) {
7497c478bd9Sstevel@tonic-gate             if (p > Line) {
7507c478bd9Sstevel@tonic-gate                 *p = '\0';
7517c478bd9Sstevel@tonic-gate                 *length = p - Line;
7527c478bd9Sstevel@tonic-gate                 return (column);
7537c478bd9Sstevel@tonic-gate             }
7547c478bd9Sstevel@tonic-gate             *length = p - Line;
7557c478bd9Sstevel@tonic-gate             return (EOF);
7567c478bd9Sstevel@tonic-gate         }
7577c478bd9Sstevel@tonic-gate 	if (!csno) {
7587c478bd9Sstevel@tonic-gate 	        if (c == '\n') {
7597c478bd9Sstevel@tonic-gate 	            /* detect \r\n.  -Hans */
7607c478bd9Sstevel@tonic-gate 	            if (p>Line && p[-1] == '\r') {
7617c478bd9Sstevel@tonic-gate 	                column = oldcolumn;
7627c478bd9Sstevel@tonic-gate 	                p--;
7637c478bd9Sstevel@tonic-gate 	            }
7647c478bd9Sstevel@tonic-gate 	            Currline++;
7657c478bd9Sstevel@tonic-gate 	            break;
7667c478bd9Sstevel@tonic-gate 	        }
7677c478bd9Sstevel@tonic-gate 	        *p++ = c;
7687c478bd9Sstevel@tonic-gate 	        if (c == '\t')
7697c478bd9Sstevel@tonic-gate 	            if (hardtabs && column < promptlen && !hard) {
7707c478bd9Sstevel@tonic-gate 	                if (clr_eol && !dumb) {
7717c478bd9Sstevel@tonic-gate 	                    column = 1 + (column | 7);
7727c478bd9Sstevel@tonic-gate 	                    putp (clr_eol);
7737c478bd9Sstevel@tonic-gate 	                    promptlen = 0;
7747c478bd9Sstevel@tonic-gate 	                }
7757c478bd9Sstevel@tonic-gate 	                else {
7767c478bd9Sstevel@tonic-gate 	                    for (--p; column & 7 && p < &Line[LINSIZ - 1]; column++) {
7777c478bd9Sstevel@tonic-gate 	                        *p++ = ' ';
7787c478bd9Sstevel@tonic-gate 	                    }
7797c478bd9Sstevel@tonic-gate 	                    if (column >= promptlen) promptlen = 0;
7807c478bd9Sstevel@tonic-gate 	                }
7817c478bd9Sstevel@tonic-gate 	            }
7827c478bd9Sstevel@tonic-gate 	            else
7837c478bd9Sstevel@tonic-gate 	                column = 1 + (column | 7);
7847c478bd9Sstevel@tonic-gate 	        else if ((c == '\b') && (ul_opt || !cr_opt) && (column > 0))  /* M008 */
7857c478bd9Sstevel@tonic-gate 	                column--;
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	        /* this is sort of strange.  what was here before was that
7887c478bd9Sstevel@tonic-gate 	           \r always set column to zero, and the hack above to
7897c478bd9Sstevel@tonic-gate 	           detect \r\n didnt exist.  the net effect is to make
7907c478bd9Sstevel@tonic-gate 	           the current line be overwritten by the prompt if it
7917c478bd9Sstevel@tonic-gate 	           had a \r at the end, and the line start after the \r
7927c478bd9Sstevel@tonic-gate 	           otherwise.  I suppose this is useful for overstriking
7937c478bd9Sstevel@tonic-gate 	           on hard copy terminals, but not on anything glass
7947c478bd9Sstevel@tonic-gate 	           -Hans */
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	        else if ((c == '\r') && !cr_opt) {
7977c478bd9Sstevel@tonic-gate 	                oldcolumn = column;
7987c478bd9Sstevel@tonic-gate 	                column = 0;
7997c478bd9Sstevel@tonic-gate 	        }
8007c478bd9Sstevel@tonic-gate 	        else if (c == '\f' && stop_opt) {
8017c478bd9Sstevel@tonic-gate 	                p[-1] = '^';
8027c478bd9Sstevel@tonic-gate 	                *p++ = 'L';
8037c478bd9Sstevel@tonic-gate 	                column += 2;
8047c478bd9Sstevel@tonic-gate 	                Pause++;
8057c478bd9Sstevel@tonic-gate 	        }
8067c478bd9Sstevel@tonic-gate 	        else if (c == EOF) {
8077c478bd9Sstevel@tonic-gate 	            *length = p - Line;
8087c478bd9Sstevel@tonic-gate 	            return (column);
8097c478bd9Sstevel@tonic-gate 	        }
8107c478bd9Sstevel@tonic-gate 	        else if (c < ' ' && cr_opt){                    /* M008 begin */
8117c478bd9Sstevel@tonic-gate 	                p[-1] = '^';
8127c478bd9Sstevel@tonic-gate 	                *p++ = c | ('A' - 1);
8137c478bd9Sstevel@tonic-gate 	                column += 2;
8147c478bd9Sstevel@tonic-gate 	        }                                               /* M008 end */
8157c478bd9Sstevel@tonic-gate 	        else if (c >= ' ' && c != RUBOUT)
8167c478bd9Sstevel@tonic-gate 	            column++;
8177c478bd9Sstevel@tonic-gate 	} /* end of code set 0 */
8187c478bd9Sstevel@tonic-gate 	else {
8197c478bd9Sstevel@tonic-gate 		column += scw[csno];
8207c478bd9Sstevel@tonic-gate 		if ( column > Mcol && fold_opt ) {
8217c478bd9Sstevel@tonic-gate 		    column -= scw[csno];
8227c478bd9Sstevel@tonic-gate 		    while ( column < Mcol ) {
8237c478bd9Sstevel@tonic-gate 		        column++;
8247c478bd9Sstevel@tonic-gate 		        *p++ = ' ';
8257c478bd9Sstevel@tonic-gate 		    }
8267c478bd9Sstevel@tonic-gate 		    column = Mcol;
8277c478bd9Sstevel@tonic-gate 		    Ungetc(c,f);
8287c478bd9Sstevel@tonic-gate 		} else {
8297c478bd9Sstevel@tonic-gate 		    int i;
8307c478bd9Sstevel@tonic-gate 		    *p++ = c;
8317c478bd9Sstevel@tonic-gate 		    for(i=1; i<cw[csno];i++)
8327c478bd9Sstevel@tonic-gate 			*p++ = Getc(f);
8337c478bd9Sstevel@tonic-gate 		}
8347c478bd9Sstevel@tonic-gate 	} /* end of codeset 1 ~ 3 */
8357c478bd9Sstevel@tonic-gate         if (column >= Mcol && fold_opt) break;
8367c478bd9Sstevel@tonic-gate         c = Getc (f);
8377c478bd9Sstevel@tonic-gate     }
8387c478bd9Sstevel@tonic-gate     if (column >= Mcol && Mcol > 0) {
8397c478bd9Sstevel@tonic-gate         if (!Wrap) {
8407c478bd9Sstevel@tonic-gate             *p++ = '\n';
8417c478bd9Sstevel@tonic-gate         }
8427c478bd9Sstevel@tonic-gate     }
8437c478bd9Sstevel@tonic-gate     colflg = column == Mcol && fold_opt;
8447c478bd9Sstevel@tonic-gate     if (colflg && eat_newline_glitch && Wrap) {
8457c478bd9Sstevel@tonic-gate 	*p++ = '\n'; /* simulate normal wrap */
8467c478bd9Sstevel@tonic-gate     }
8477c478bd9Sstevel@tonic-gate     *length = p - Line;
8487c478bd9Sstevel@tonic-gate     *p = 0;
8497c478bd9Sstevel@tonic-gate     return (column);
8507c478bd9Sstevel@tonic-gate }
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate /*
8537c478bd9Sstevel@tonic-gate ** Erase the rest of the prompt, assuming we are starting at column col.
8547c478bd9Sstevel@tonic-gate */
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate static void
8577c478bd9Sstevel@tonic-gate prmpt_erase(register int col)
8587c478bd9Sstevel@tonic-gate {
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate     if (promptlen == 0)
8617c478bd9Sstevel@tonic-gate         return;
8627c478bd9Sstevel@tonic-gate     if (hard) {
8637c478bd9Sstevel@tonic-gate         putchar ('\n');
8647c478bd9Sstevel@tonic-gate     }
8657c478bd9Sstevel@tonic-gate     else {
8667c478bd9Sstevel@tonic-gate         if (col == 0)
8677c478bd9Sstevel@tonic-gate             putchar ('\r');
8687c478bd9Sstevel@tonic-gate         if (!dumb && clr_eol)
8697c478bd9Sstevel@tonic-gate             putp (clr_eol);
8707c478bd9Sstevel@tonic-gate         else
8717c478bd9Sstevel@tonic-gate             for (col = promptlen - col; col > 0; col--)
8727c478bd9Sstevel@tonic-gate                 putchar (' ');
8737c478bd9Sstevel@tonic-gate     }
8747c478bd9Sstevel@tonic-gate     promptlen = 0;
8757c478bd9Sstevel@tonic-gate }
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate /*
8787c478bd9Sstevel@tonic-gate ** Erase the current line entirely
8797c478bd9Sstevel@tonic-gate */
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate static void
8827c478bd9Sstevel@tonic-gate kill_line(void)
8837c478bd9Sstevel@tonic-gate {
8847c478bd9Sstevel@tonic-gate     prmpt_erase (0);
8857c478bd9Sstevel@tonic-gate     if (!clr_eol || dumb) putchar ('\r');
8867c478bd9Sstevel@tonic-gate }
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate /* Print a buffer of n characters */
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate static void
8917c478bd9Sstevel@tonic-gate prbuf(register char *s, register int n)
8927c478bd9Sstevel@tonic-gate {
8937c478bd9Sstevel@tonic-gate     char c;                             /* next ouput character */
8947c478bd9Sstevel@tonic-gate     register int state = 0;             /* next output char's UL state */
8957c478bd9Sstevel@tonic-gate     static int pstate = 0;              /* current terminal UL state (off) */
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate     while (--n >= 0)
8987c478bd9Sstevel@tonic-gate         if (!ul_opt)
8997c478bd9Sstevel@tonic-gate             putchar (*s++);
9007c478bd9Sstevel@tonic-gate         else {
9017c478bd9Sstevel@tonic-gate             if (n >= 2 && s[0] == '_' && s[1] == '\b') {
9027c478bd9Sstevel@tonic-gate                 n -= 2;
9037c478bd9Sstevel@tonic-gate                 s += 2;
9047c478bd9Sstevel@tonic-gate                 c = *s++;
9057c478bd9Sstevel@tonic-gate                 state = 1;
9067c478bd9Sstevel@tonic-gate             } else if (n >= 2 && s[1] == '\b' && s[2] == '_') {
9077c478bd9Sstevel@tonic-gate                 n -= 2;
9087c478bd9Sstevel@tonic-gate                 c = *s++;
9097c478bd9Sstevel@tonic-gate                 s += 2;
9107c478bd9Sstevel@tonic-gate                 state = 1;
9117c478bd9Sstevel@tonic-gate             } else {
9127c478bd9Sstevel@tonic-gate                 c = *s++;
9137c478bd9Sstevel@tonic-gate                 state = 0;
9147c478bd9Sstevel@tonic-gate             }
9157c478bd9Sstevel@tonic-gate             if (state != pstate)
9167c478bd9Sstevel@tonic-gate                 putp(state ? ULenter : ULexit);
9177c478bd9Sstevel@tonic-gate             pstate = state;
9187c478bd9Sstevel@tonic-gate             putchar(c);
9197c478bd9Sstevel@tonic-gate             if (state && underline_char) {
9207c478bd9Sstevel@tonic-gate                 putp(cursor_left);
9217c478bd9Sstevel@tonic-gate                 putp(underline_char);
9227c478bd9Sstevel@tonic-gate             }
9237c478bd9Sstevel@tonic-gate         }
9247c478bd9Sstevel@tonic-gate     /*
9257c478bd9Sstevel@tonic-gate      * M002
9267c478bd9Sstevel@tonic-gate      * You don't want to stay in standout mode at the end of the line;
9277c478bd9Sstevel@tonic-gate      * on some terminals, this will leave all of the remaining blank
9287c478bd9Sstevel@tonic-gate      * space on the line in standout mode.
9297c478bd9Sstevel@tonic-gate      */
9307c478bd9Sstevel@tonic-gate     if (state && !underline_char) {                       /*M002*/
9317c478bd9Sstevel@tonic-gate             putp(ULexit);                    /*M002*/
9327c478bd9Sstevel@tonic-gate             pstate = 0;                                 /*M002*/
9337c478bd9Sstevel@tonic-gate     }                                                   /*M002*/
9347c478bd9Sstevel@tonic-gate }
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate /*
9377c478bd9Sstevel@tonic-gate **  Clear the screen
9387c478bd9Sstevel@tonic-gate */
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate static void
9417c478bd9Sstevel@tonic-gate doclear(void)
9427c478bd9Sstevel@tonic-gate {
9437c478bd9Sstevel@tonic-gate     if (clear_screen && !hard) {
9447c478bd9Sstevel@tonic-gate         putp(clear_screen);
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate         /* Put out carriage return so that system doesn't
9477c478bd9Sstevel@tonic-gate         ** get confused by escape sequences when expanding tabs
9487c478bd9Sstevel@tonic-gate         */
9497c478bd9Sstevel@tonic-gate         putchar ('\r');
9507c478bd9Sstevel@tonic-gate         promptlen = 0;
9517c478bd9Sstevel@tonic-gate     }
9527c478bd9Sstevel@tonic-gate }
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate static int lastcmd, lastp;
9567c478bd9Sstevel@tonic-gate static off_t lastarg;
9577c478bd9Sstevel@tonic-gate static int lastcolon;
958*ca3f9453Sas char shell_line[PATH_MAX];
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate /*
9617c478bd9Sstevel@tonic-gate ** Read a command and do it. A command consists of an optional integer
9627c478bd9Sstevel@tonic-gate ** argument followed by the command character.  Return the number of lines
9637c478bd9Sstevel@tonic-gate ** to display in the next screenful.  If there is nothing more to display
9647c478bd9Sstevel@tonic-gate ** in the current file, zero is returned.
9657c478bd9Sstevel@tonic-gate */
9667c478bd9Sstevel@tonic-gate 
9677c478bd9Sstevel@tonic-gate static off_t
9687c478bd9Sstevel@tonic-gate command(char *filename, register FILE *f)
9697c478bd9Sstevel@tonic-gate {
9707c478bd9Sstevel@tonic-gate     register off_t nlines;
9717c478bd9Sstevel@tonic-gate     register off_t retval;
9727c478bd9Sstevel@tonic-gate     register int c;
9737c478bd9Sstevel@tonic-gate     char colonch;
9747c478bd9Sstevel@tonic-gate     FILE *helpf;
9757c478bd9Sstevel@tonic-gate     int done;
9767c478bd9Sstevel@tonic-gate     char comchar, cmdbuf[80];
9777c478bd9Sstevel@tonic-gate     char filebuf[128];
9787c478bd9Sstevel@tonic-gate     char *loc;
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate #define ret(val) retval=val;done++;break
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate     done = 0;
9837c478bd9Sstevel@tonic-gate     if (!errors)
9847c478bd9Sstevel@tonic-gate         prompt (filename);
9857c478bd9Sstevel@tonic-gate     else
9867c478bd9Sstevel@tonic-gate         errors = 0;
9877c478bd9Sstevel@tonic-gate     for (;;) {
9887c478bd9Sstevel@tonic-gate         nlines = number (&comchar);
9897c478bd9Sstevel@tonic-gate         lastp = colonch = 0;
9907c478bd9Sstevel@tonic-gate         if (comchar == '.') {   /* Repeat last command */
9917c478bd9Sstevel@tonic-gate                 lastp++;
9927c478bd9Sstevel@tonic-gate                 comchar = lastcmd;
9937c478bd9Sstevel@tonic-gate                 nlines = lastarg;
9947c478bd9Sstevel@tonic-gate                 if (lastcmd == ':')
9957c478bd9Sstevel@tonic-gate                         colonch = lastcolon;
9967c478bd9Sstevel@tonic-gate         }
9977c478bd9Sstevel@tonic-gate         lastcmd = comchar;
9987c478bd9Sstevel@tonic-gate         lastarg = nlines;
9997c478bd9Sstevel@tonic-gate 	if((comchar != RUBOUT) && !dum_opt) {
10007c478bd9Sstevel@tonic-gate         	if (comchar == otty.c_cc[VERASE]) {
10017c478bd9Sstevel@tonic-gate            		 kill_line ();
10027c478bd9Sstevel@tonic-gate             		prompt (filename);
10037c478bd9Sstevel@tonic-gate             		continue;
10047c478bd9Sstevel@tonic-gate         	}
10057c478bd9Sstevel@tonic-gate 	}
10067c478bd9Sstevel@tonic-gate         switch (comchar) {
10077c478bd9Sstevel@tonic-gate         case ':':
10087c478bd9Sstevel@tonic-gate             retval = colon (filename, colonch, nlines);
10097c478bd9Sstevel@tonic-gate             if (retval >= 0)
10107c478bd9Sstevel@tonic-gate                 done++;
10117c478bd9Sstevel@tonic-gate             break;
10127c478bd9Sstevel@tonic-gate 	case 'b':
10137c478bd9Sstevel@tonic-gate 	case ctrl('B'):
10147c478bd9Sstevel@tonic-gate 	    {
10157c478bd9Sstevel@tonic-gate 		register off_t initline;
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 		if (no_intty) {
10187c478bd9Sstevel@tonic-gate 		    write(2, &bell, 1);
10197c478bd9Sstevel@tonic-gate 		    return (-1);
10207c478bd9Sstevel@tonic-gate 		}
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 		if (nlines == 0) nlines++;
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 		putchar ('\r');
10257c478bd9Sstevel@tonic-gate 		prmpt_erase (0);
10267c478bd9Sstevel@tonic-gate 		printf ("\n");
10277c478bd9Sstevel@tonic-gate 		if (clreol)
10287c478bd9Sstevel@tonic-gate 			cleareol ();
10297c478bd9Sstevel@tonic-gate 		printf (gettext("...back %lld page"), nlines);
10307c478bd9Sstevel@tonic-gate 		if (nlines > 1)
10317c478bd9Sstevel@tonic-gate 			pr ("s\n");
10327c478bd9Sstevel@tonic-gate 		else
10337c478bd9Sstevel@tonic-gate 			pr ("\n");
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 		if (clreol)
10367c478bd9Sstevel@tonic-gate 			cleareol ();
10377c478bd9Sstevel@tonic-gate 		pr ("\n");
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 		initline = Currline - dlines * (nlines + 1);
10407c478bd9Sstevel@tonic-gate 		if (! noscroll)
10417c478bd9Sstevel@tonic-gate 		    --initline;
10427c478bd9Sstevel@tonic-gate 		if (initline < 0) initline = 0;
10437c478bd9Sstevel@tonic-gate 		Fseek(f, 0LL);
10447c478bd9Sstevel@tonic-gate 		Currline = 0;	/* skiplns() will make Currline correct */
10457c478bd9Sstevel@tonic-gate 		skiplns(initline, f);
10467c478bd9Sstevel@tonic-gate 		if (! noscroll) {
10477c478bd9Sstevel@tonic-gate 		    ret(dlines + 1);
10487c478bd9Sstevel@tonic-gate 		}
10497c478bd9Sstevel@tonic-gate 		else {
10507c478bd9Sstevel@tonic-gate 		    ret(dlines);
10517c478bd9Sstevel@tonic-gate 		}
10527c478bd9Sstevel@tonic-gate 	    }
10537c478bd9Sstevel@tonic-gate         case ' ':
10547c478bd9Sstevel@tonic-gate         case 'z':
10557c478bd9Sstevel@tonic-gate             if (nlines == 0) nlines = dlines;
10567c478bd9Sstevel@tonic-gate             else if (comchar == 'z') dlines = nlines;
10577c478bd9Sstevel@tonic-gate             ret (nlines);
10587c478bd9Sstevel@tonic-gate         case 'd':
10597c478bd9Sstevel@tonic-gate         case ctrl('D'):
10607c478bd9Sstevel@tonic-gate             if (nlines != 0) nscroll = nlines;
10617c478bd9Sstevel@tonic-gate             ret (nscroll);
10627c478bd9Sstevel@tonic-gate         case RUBOUT:
10637c478bd9Sstevel@tonic-gate         case 'q':
10647c478bd9Sstevel@tonic-gate         case 'Q':
10657c478bd9Sstevel@tonic-gate             end_it(0);
10667c478bd9Sstevel@tonic-gate 	    /*NOTREACHED*/
10677c478bd9Sstevel@tonic-gate         case 's':
10687c478bd9Sstevel@tonic-gate         case 'f':
10697c478bd9Sstevel@tonic-gate             if (nlines == 0) nlines++;
10707c478bd9Sstevel@tonic-gate             if (comchar == 'f')
10717c478bd9Sstevel@tonic-gate                 nlines *= dlines;
10727c478bd9Sstevel@tonic-gate             putchar ('\r');
10737c478bd9Sstevel@tonic-gate             prmpt_erase (0);
10747c478bd9Sstevel@tonic-gate             printf ("\n");
10757c478bd9Sstevel@tonic-gate             if (clreol)
10767c478bd9Sstevel@tonic-gate                 cleareol ();
10777c478bd9Sstevel@tonic-gate             printf (gettext("...skipping %lld line"), nlines);
10787c478bd9Sstevel@tonic-gate             if (nlines > 1)
10797c478bd9Sstevel@tonic-gate                 pr ("s\n");
10807c478bd9Sstevel@tonic-gate             else
10817c478bd9Sstevel@tonic-gate                 pr ("\n");
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate             if (clreol)
10847c478bd9Sstevel@tonic-gate                 cleareol ();
10857c478bd9Sstevel@tonic-gate             pr ("\n");
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate             while (nlines > 0) {
10887c478bd9Sstevel@tonic-gate                 while ((c = Getc (f)) != '\n')
10897c478bd9Sstevel@tonic-gate                     if (c == EOF) {
10907c478bd9Sstevel@tonic-gate                         retval = 0;
10917c478bd9Sstevel@tonic-gate                         done++;
10927c478bd9Sstevel@tonic-gate                         goto endsw;
10937c478bd9Sstevel@tonic-gate                     }
10947c478bd9Sstevel@tonic-gate                     Currline++;
10957c478bd9Sstevel@tonic-gate                     nlines--;
10967c478bd9Sstevel@tonic-gate             }
10977c478bd9Sstevel@tonic-gate             ret (dlines);
10987c478bd9Sstevel@tonic-gate         case '\n':
10997c478bd9Sstevel@tonic-gate 	    if (nlines != 0)
11007c478bd9Sstevel@tonic-gate                 dlines = nlines;
11017c478bd9Sstevel@tonic-gate             else
11027c478bd9Sstevel@tonic-gate                 nlines = 1;
11037c478bd9Sstevel@tonic-gate             ret (nlines);
11047c478bd9Sstevel@tonic-gate         case '\f':
11057c478bd9Sstevel@tonic-gate             if (!no_intty) {
11067c478bd9Sstevel@tonic-gate                 doclear ();
11077c478bd9Sstevel@tonic-gate                 Fseek (f, screen_start.chrctr);
11087c478bd9Sstevel@tonic-gate                 Currline = screen_start.line;
11097c478bd9Sstevel@tonic-gate                 ret (dlines);
11107c478bd9Sstevel@tonic-gate             }
11117c478bd9Sstevel@tonic-gate             else {
11127c478bd9Sstevel@tonic-gate                 write (2, &Bell, 1);
11137c478bd9Sstevel@tonic-gate                 break;
11147c478bd9Sstevel@tonic-gate             }
11157c478bd9Sstevel@tonic-gate         case '\'':
11167c478bd9Sstevel@tonic-gate             if (!no_intty) {
11177c478bd9Sstevel@tonic-gate                 kill_line ();
11187c478bd9Sstevel@tonic-gate                 pr (gettext("\n***Back***\n\n"));
11197c478bd9Sstevel@tonic-gate                 Fseek (f, context.chrctr);
11207c478bd9Sstevel@tonic-gate                 Currline = context.line;
11217c478bd9Sstevel@tonic-gate                 ret (dlines);
11227c478bd9Sstevel@tonic-gate             }
11237c478bd9Sstevel@tonic-gate             else {
11247c478bd9Sstevel@tonic-gate                 write (2, &Bell, 1);
11257c478bd9Sstevel@tonic-gate                 break;
11267c478bd9Sstevel@tonic-gate             }
11277c478bd9Sstevel@tonic-gate         case '=':
11287c478bd9Sstevel@tonic-gate             kill_line ();
11297c478bd9Sstevel@tonic-gate             promptlen = printf ("%lld", Currline);
11307c478bd9Sstevel@tonic-gate             fflush (stdout);
11317c478bd9Sstevel@tonic-gate             break;
11327c478bd9Sstevel@tonic-gate         case 'n':
11337c478bd9Sstevel@tonic-gate             lastp++;
11347c478bd9Sstevel@tonic-gate 	    /*FALLTHROUGH*/
11357c478bd9Sstevel@tonic-gate         case '/':
11367c478bd9Sstevel@tonic-gate             if (nlines == 0) nlines++;
11377c478bd9Sstevel@tonic-gate             kill_line ();
11387c478bd9Sstevel@tonic-gate             pr ("/");
11397c478bd9Sstevel@tonic-gate             promptlen = 1;
11407c478bd9Sstevel@tonic-gate             fflush (stdout);
11417c478bd9Sstevel@tonic-gate             if (lastp) {
11427c478bd9Sstevel@tonic-gate                 write (2,"\r", 1);
11437c478bd9Sstevel@tonic-gate                 search (NULL, f, nlines);       /* Use previous r.e. */
11447c478bd9Sstevel@tonic-gate             }
11457c478bd9Sstevel@tonic-gate             else {
11467c478bd9Sstevel@tonic-gate                 ttyin (cmdbuf, 78, '/');
11477c478bd9Sstevel@tonic-gate                 write (2, "\r", 1);
11487c478bd9Sstevel@tonic-gate                 search (cmdbuf, f, nlines);
11497c478bd9Sstevel@tonic-gate             }
11507c478bd9Sstevel@tonic-gate             ret (dlines-1);
11517c478bd9Sstevel@tonic-gate         case '!':
11527c478bd9Sstevel@tonic-gate             do_shell (filename);
11537c478bd9Sstevel@tonic-gate             break;
11547c478bd9Sstevel@tonic-gate         case 'h':
11557c478bd9Sstevel@tonic-gate         case '?':
11567c478bd9Sstevel@tonic-gate 	    /*
11577c478bd9Sstevel@tonic-gate 	     * First get local help file.
11587c478bd9Sstevel@tonic-gate 	     */
11597c478bd9Sstevel@tonic-gate 	    loc = setlocale(LC_MESSAGES, 0);
11607c478bd9Sstevel@tonic-gate 	    sprintf(filebuf, LOCAL_HELP, loc);
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate             if  ((strcmp(loc, "C") == 0) || (helpf = fopen (filebuf, "r")) == NULL) {
11637c478bd9Sstevel@tonic-gate 		    if  ((helpf = fopen (HELPFILE, "r")) == NULL)
11647c478bd9Sstevel@tonic-gate 			error (gettext("Can't open help file"));
11657c478bd9Sstevel@tonic-gate 	    }
11667c478bd9Sstevel@tonic-gate             if (noscroll) doclear ();
11677c478bd9Sstevel@tonic-gate             copy_file (helpf);
11687c478bd9Sstevel@tonic-gate             fclose (helpf);
11697c478bd9Sstevel@tonic-gate             prompt (filename);
11707c478bd9Sstevel@tonic-gate             break;
11717c478bd9Sstevel@tonic-gate         case 'v':       /* This case should go right before default */
11727c478bd9Sstevel@tonic-gate             if (!no_intty) {
11737c478bd9Sstevel@tonic-gate                 kill_line ();
11747c478bd9Sstevel@tonic-gate                 cmdbuf[0] = '+';
11757c478bd9Sstevel@tonic-gate                 sprintf(&cmdbuf[1], "%lld", Currline);
11767c478bd9Sstevel@tonic-gate                 pr ("vi "); pr (cmdbuf); putchar (' '); pr (fnames[fnum]);
11777c478bd9Sstevel@tonic-gate                 execute (filename, VI, "vi", cmdbuf, fnames[fnum], 0);
11787c478bd9Sstevel@tonic-gate                 break;
11797c478bd9Sstevel@tonic-gate             }
11807c478bd9Sstevel@tonic-gate         default:
11817c478bd9Sstevel@tonic-gate 		if (dum_opt) {
11827c478bd9Sstevel@tonic-gate 			kill_line ();
11837c478bd9Sstevel@tonic-gate 		    	promptlen = pr(gettext("[Press 'h' for instructions.]"));
11847c478bd9Sstevel@tonic-gate 			fflush (stdout);
11857c478bd9Sstevel@tonic-gate 	    	}
11867c478bd9Sstevel@tonic-gate 	    	else
11877c478bd9Sstevel@tonic-gate             		write (2, &Bell, 1);
11887c478bd9Sstevel@tonic-gate             break;
11897c478bd9Sstevel@tonic-gate         }
11907c478bd9Sstevel@tonic-gate         if (done) break;
11917c478bd9Sstevel@tonic-gate     }
11927c478bd9Sstevel@tonic-gate     putchar ('\r');
11937c478bd9Sstevel@tonic-gate endsw:
11947c478bd9Sstevel@tonic-gate     inwait = 0;
11957c478bd9Sstevel@tonic-gate     notell++;
11967c478bd9Sstevel@tonic-gate     return (retval);
11977c478bd9Sstevel@tonic-gate }
11987c478bd9Sstevel@tonic-gate 
11997c478bd9Sstevel@tonic-gate char ch;
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate /*
12027c478bd9Sstevel@tonic-gate  * Execute a colon-prefixed command.
12037c478bd9Sstevel@tonic-gate  * Returns <0 if not a command that should cause
12047c478bd9Sstevel@tonic-gate  * more of the file to be printed.
12057c478bd9Sstevel@tonic-gate  */
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate static int
12087c478bd9Sstevel@tonic-gate colon(char *filename, int cmd, off_t nlines)
12097c478bd9Sstevel@tonic-gate {
12107c478bd9Sstevel@tonic-gate         if (cmd == 0)
12117c478bd9Sstevel@tonic-gate                 ch = readch ();
12127c478bd9Sstevel@tonic-gate         else
12137c478bd9Sstevel@tonic-gate                 ch = cmd;
12147c478bd9Sstevel@tonic-gate         lastcolon = ch;
12157c478bd9Sstevel@tonic-gate         switch (ch) {
12167c478bd9Sstevel@tonic-gate         case 'f':
12177c478bd9Sstevel@tonic-gate                 kill_line ();
12187c478bd9Sstevel@tonic-gate                 if (!no_intty)
12197c478bd9Sstevel@tonic-gate                         promptlen = printf (gettext("\"%s\" line %lld"),
12207c478bd9Sstevel@tonic-gate 					    fnames[fnum], Currline);
12217c478bd9Sstevel@tonic-gate                 else
12227c478bd9Sstevel@tonic-gate                         promptlen = printf(
12237c478bd9Sstevel@tonic-gate 			 gettext("[Not a file] line %lld"), Currline);
12247c478bd9Sstevel@tonic-gate                 fflush (stdout);
12257c478bd9Sstevel@tonic-gate                 return (-1);
12267c478bd9Sstevel@tonic-gate         case 'n':
12277c478bd9Sstevel@tonic-gate                 if (nlines == 0) {
12287c478bd9Sstevel@tonic-gate                         if (fnum >= nfiles - 1)
12297c478bd9Sstevel@tonic-gate                                 end_it(0);
12307c478bd9Sstevel@tonic-gate                         nlines++;
12317c478bd9Sstevel@tonic-gate                 }
12327c478bd9Sstevel@tonic-gate                 putchar ('\r');
12337c478bd9Sstevel@tonic-gate                 prmpt_erase (0);
12347c478bd9Sstevel@tonic-gate                 skipf ((int)nlines);
12357c478bd9Sstevel@tonic-gate                 return (0);
12367c478bd9Sstevel@tonic-gate         case 'p':
12377c478bd9Sstevel@tonic-gate                 if (no_intty) {
12387c478bd9Sstevel@tonic-gate                         write (2, &Bell, 1);
12397c478bd9Sstevel@tonic-gate                         return (-1);
12407c478bd9Sstevel@tonic-gate                 }
12417c478bd9Sstevel@tonic-gate                 putchar ('\r');
12427c478bd9Sstevel@tonic-gate                 prmpt_erase (0);
12437c478bd9Sstevel@tonic-gate                 if (nlines == 0)
12447c478bd9Sstevel@tonic-gate                         nlines++;
12457c478bd9Sstevel@tonic-gate                 skipf ((int)-nlines);
12467c478bd9Sstevel@tonic-gate                 return (0);
12477c478bd9Sstevel@tonic-gate         case '!':
12487c478bd9Sstevel@tonic-gate                 do_shell (filename);
12497c478bd9Sstevel@tonic-gate                 return (-1);
12507c478bd9Sstevel@tonic-gate         case 'q':
12517c478bd9Sstevel@tonic-gate         case 'Q':
12527c478bd9Sstevel@tonic-gate                 end_it(0);
12537c478bd9Sstevel@tonic-gate         default:
12547c478bd9Sstevel@tonic-gate                 write (2, &Bell, 1);
12557c478bd9Sstevel@tonic-gate                 return (-1);
12567c478bd9Sstevel@tonic-gate         }
12577c478bd9Sstevel@tonic-gate }
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate /*
12607c478bd9Sstevel@tonic-gate ** Read a decimal number from the terminal. Set cmd to the non-digit which
12617c478bd9Sstevel@tonic-gate ** terminates the number.
12627c478bd9Sstevel@tonic-gate */
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate static int
12657c478bd9Sstevel@tonic-gate number(char *cmd)
12667c478bd9Sstevel@tonic-gate {
12677c478bd9Sstevel@tonic-gate         register int i;
12687c478bd9Sstevel@tonic-gate 
12697c478bd9Sstevel@tonic-gate         i = 0; ch = otty.c_cc[VKILL];
12707c478bd9Sstevel@tonic-gate         for (;;) {
12717c478bd9Sstevel@tonic-gate                 ch = readch ();
12727c478bd9Sstevel@tonic-gate                 if (ch >= '0' && ch <= '9') {
12737c478bd9Sstevel@tonic-gate                         i = i*10 + ch - '0';
12747c478bd9Sstevel@tonic-gate                 } else if (ch == RUBOUT) {
12757c478bd9Sstevel@tonic-gate                         i = 0;
12767c478bd9Sstevel@tonic-gate                         *cmd = ch;
12777c478bd9Sstevel@tonic-gate                         break;
12787c478bd9Sstevel@tonic-gate                 } else if (ch == otty.c_cc[VKILL]) {
12797c478bd9Sstevel@tonic-gate                         i = 0;
12807c478bd9Sstevel@tonic-gate                 } else {
12817c478bd9Sstevel@tonic-gate                         *cmd = ch;
12827c478bd9Sstevel@tonic-gate                         break;
12837c478bd9Sstevel@tonic-gate                 }
12847c478bd9Sstevel@tonic-gate         }
12857c478bd9Sstevel@tonic-gate         return (i);
12867c478bd9Sstevel@tonic-gate }
12877c478bd9Sstevel@tonic-gate 
12887c478bd9Sstevel@tonic-gate static void
12897c478bd9Sstevel@tonic-gate do_shell(char *filename)
12907c478bd9Sstevel@tonic-gate {
12917c478bd9Sstevel@tonic-gate         char cmdbuf[80];
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate         kill_line ();
12947c478bd9Sstevel@tonic-gate         pr ("!");
12957c478bd9Sstevel@tonic-gate         fflush (stdout);
12967c478bd9Sstevel@tonic-gate         promptlen = 1;
12977c478bd9Sstevel@tonic-gate         if (lastp)
12987c478bd9Sstevel@tonic-gate                 pr (shell_line);
12997c478bd9Sstevel@tonic-gate         else {
13007c478bd9Sstevel@tonic-gate                 ttyin (cmdbuf, 78, '!');
13017c478bd9Sstevel@tonic-gate                 if (expand (shell_line, cmdbuf)) {
13027c478bd9Sstevel@tonic-gate                         kill_line ();
13037c478bd9Sstevel@tonic-gate                         promptlen = printf ("!%s", shell_line);
13047c478bd9Sstevel@tonic-gate                 }
13057c478bd9Sstevel@tonic-gate         }
13067c478bd9Sstevel@tonic-gate         fflush (stdout);
13077c478bd9Sstevel@tonic-gate         write (2, "\n", 1);
13087c478bd9Sstevel@tonic-gate         promptlen = 0;
13097c478bd9Sstevel@tonic-gate         shellp = 1;
13107c478bd9Sstevel@tonic-gate         execute (filename, shell, shell, "-c", shell_line, 0);
13117c478bd9Sstevel@tonic-gate }
13127c478bd9Sstevel@tonic-gate 
13137c478bd9Sstevel@tonic-gate /*
13147c478bd9Sstevel@tonic-gate ** Search for nth ocurrence of regular expression contained in buf in the file
13157c478bd9Sstevel@tonic-gate */
13167c478bd9Sstevel@tonic-gate 
13177c478bd9Sstevel@tonic-gate static void
13187c478bd9Sstevel@tonic-gate search(char buf[], FILE *file, register off_t n)
13197c478bd9Sstevel@tonic-gate {
13207c478bd9Sstevel@tonic-gate     off_t startline = Ftell (file);
13217c478bd9Sstevel@tonic-gate     register off_t line1 = startline;
13227c478bd9Sstevel@tonic-gate     register off_t line2 = startline;
13237c478bd9Sstevel@tonic-gate     register off_t line3 = startline;
13247c478bd9Sstevel@tonic-gate     register off_t lncount;
13257c478bd9Sstevel@tonic-gate     off_t saveln;
13267c478bd9Sstevel@tonic-gate     static char *s = NULL;
13277c478bd9Sstevel@tonic-gate     static char lastbuf[80];
13287c478bd9Sstevel@tonic-gate 
13297c478bd9Sstevel@tonic-gate     if (buf != NULL) {
13307c478bd9Sstevel@tonic-gate 	if (s != NULL)
13317c478bd9Sstevel@tonic-gate 		free(s);
13327c478bd9Sstevel@tonic-gate 	if (*buf != '\0') {
13337c478bd9Sstevel@tonic-gate 		if ((s = regcmp(buf, (char *) NULL)) == NULL)
13347c478bd9Sstevel@tonic-gate 			error(gettext("Regular expression botch"));
13357c478bd9Sstevel@tonic-gate 		else
13367c478bd9Sstevel@tonic-gate 			strcpy(lastbuf, buf);
13377c478bd9Sstevel@tonic-gate 	} else {
13387c478bd9Sstevel@tonic-gate 		if ((s = regcmp(lastbuf, (char *) NULL)) == NULL)
13397c478bd9Sstevel@tonic-gate 			error(gettext("No previous regular expression"));
13407c478bd9Sstevel@tonic-gate 	}
13417c478bd9Sstevel@tonic-gate     } else {
13427c478bd9Sstevel@tonic-gate 	if (s == NULL)
13437c478bd9Sstevel@tonic-gate 	    error(gettext("No previous regular expression"));
13447c478bd9Sstevel@tonic-gate     }
13457c478bd9Sstevel@tonic-gate     context.line = saveln = Currline;
13467c478bd9Sstevel@tonic-gate     context.chrctr = startline;
13477c478bd9Sstevel@tonic-gate     lncount = 0;
13487c478bd9Sstevel@tonic-gate     while (!feof (file)) {
13497c478bd9Sstevel@tonic-gate         line3 = line2;
13507c478bd9Sstevel@tonic-gate         line2 = line1;
13517c478bd9Sstevel@tonic-gate         line1 = Ftell (file);
13527c478bd9Sstevel@tonic-gate         rdline (file);
13537c478bd9Sstevel@tonic-gate         lncount++;
13547c478bd9Sstevel@tonic-gate         if (regex(s, Line) != NULL)
13557c478bd9Sstevel@tonic-gate                 if (--n == 0) {
13567c478bd9Sstevel@tonic-gate                     if (lncount > 3 || (lncount > 1 && no_intty))
13577c478bd9Sstevel@tonic-gate                     {
13587c478bd9Sstevel@tonic-gate                         pr ("\n");
13597c478bd9Sstevel@tonic-gate                         if (clreol)
13607c478bd9Sstevel@tonic-gate                             cleareol ();
13617c478bd9Sstevel@tonic-gate                         pr(gettext("...skipping\n"));
13627c478bd9Sstevel@tonic-gate                     }
13637c478bd9Sstevel@tonic-gate                     if (!no_intty) {
13647c478bd9Sstevel@tonic-gate                         Currline -= (lncount >= 3 ? 3 : lncount);
13657c478bd9Sstevel@tonic-gate                         Fseek (file, line3);
13667c478bd9Sstevel@tonic-gate                         if (noscroll)
13677c478bd9Sstevel@tonic-gate                             if (clreol) {
13687c478bd9Sstevel@tonic-gate                                 home ();
13697c478bd9Sstevel@tonic-gate                                 cleareol ();
13707c478bd9Sstevel@tonic-gate                             }
13717c478bd9Sstevel@tonic-gate                             else
13727c478bd9Sstevel@tonic-gate                                 doclear ();
13737c478bd9Sstevel@tonic-gate                     }
13747c478bd9Sstevel@tonic-gate                     else {
13757c478bd9Sstevel@tonic-gate                         kill_line ();
13767c478bd9Sstevel@tonic-gate                         if (noscroll)
13777c478bd9Sstevel@tonic-gate                             if (clreol) {
13787c478bd9Sstevel@tonic-gate                                 home ();
13797c478bd9Sstevel@tonic-gate                                 cleareol ();
13807c478bd9Sstevel@tonic-gate                             } else
13817c478bd9Sstevel@tonic-gate                                 doclear ();
13827c478bd9Sstevel@tonic-gate                         pr (Line);
13837c478bd9Sstevel@tonic-gate                         putchar ('\n');
13847c478bd9Sstevel@tonic-gate                     }
13857c478bd9Sstevel@tonic-gate                     break;
13867c478bd9Sstevel@tonic-gate                 }
13877c478bd9Sstevel@tonic-gate     }
13887c478bd9Sstevel@tonic-gate     if (feof (file)) {
13897c478bd9Sstevel@tonic-gate         if (!no_intty) {
13907c478bd9Sstevel@tonic-gate             Currline = saveln;
13917c478bd9Sstevel@tonic-gate             Fseek (file, startline);
13927c478bd9Sstevel@tonic-gate         }
13937c478bd9Sstevel@tonic-gate         else {
13947c478bd9Sstevel@tonic-gate             pr (gettext("\nPattern not found\n"));
13957c478bd9Sstevel@tonic-gate             end_it (0);
13967c478bd9Sstevel@tonic-gate         }
13977c478bd9Sstevel@tonic-gate         error (gettext("Pattern not found"));
13987c478bd9Sstevel@tonic-gate     }
13997c478bd9Sstevel@tonic-gate }
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate #define MAXARGS 10 /* enough for 9 args. We are only passed 4 now */
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate static void
14047c478bd9Sstevel@tonic-gate execute (char *filename, char *cmd, ...)
14057c478bd9Sstevel@tonic-gate {
14067c478bd9Sstevel@tonic-gate         pid_t id;
14077c478bd9Sstevel@tonic-gate 	va_list ap;
14087c478bd9Sstevel@tonic-gate 	char *argp[MAXARGS];
14097c478bd9Sstevel@tonic-gate 	int  count;
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate         fflush (stdout);
14127c478bd9Sstevel@tonic-gate         reset_tty ();
14137c478bd9Sstevel@tonic-gate         while ((id = fork ()) < 0)
14147c478bd9Sstevel@tonic-gate             sleep (5);
14157c478bd9Sstevel@tonic-gate         if (id == 0) {
14167c478bd9Sstevel@tonic-gate             if (no_intty) {     /*M002*/
14177c478bd9Sstevel@tonic-gate                 close(0);       /*M002*/
14187c478bd9Sstevel@tonic-gate                 dup(2);         /*M002*/
14197c478bd9Sstevel@tonic-gate             }                   /*M002*/
14207c478bd9Sstevel@tonic-gate 	    va_start(ap, cmd);
14217c478bd9Sstevel@tonic-gate 	    count = 0;
14227c478bd9Sstevel@tonic-gate 	    do {
14237c478bd9Sstevel@tonic-gate #ifndef lint
14247c478bd9Sstevel@tonic-gate 		argp[count] = va_arg(ap, char *);
14257c478bd9Sstevel@tonic-gate #else
14267c478bd9Sstevel@tonic-gate 		ap = ap;
14277c478bd9Sstevel@tonic-gate #endif
14287c478bd9Sstevel@tonic-gate 		count++;
14297c478bd9Sstevel@tonic-gate 		if (count > MAXARGS)
14307c478bd9Sstevel@tonic-gate 			error (gettext("Too many arguments in execute()\n"));
14317c478bd9Sstevel@tonic-gate 	    } while (argp[count - 1] != NULL);
14327c478bd9Sstevel@tonic-gate 	    va_end(ap);
14337c478bd9Sstevel@tonic-gate 	    execvp(cmd, argp);
14347c478bd9Sstevel@tonic-gate             write (2, "exec failed\n", 12);
14357c478bd9Sstevel@tonic-gate             exit (1);
14367c478bd9Sstevel@tonic-gate         }
14377c478bd9Sstevel@tonic-gate         signal (SIGINT, SIG_IGN);
14387c478bd9Sstevel@tonic-gate         signal (SIGQUIT, SIG_IGN);
14397c478bd9Sstevel@tonic-gate 	signal (SIGWINCH, SIG_IGN);
14407c478bd9Sstevel@tonic-gate #ifdef SIGTSTP
14417c478bd9Sstevel@tonic-gate         if (catch_susp)
14427c478bd9Sstevel@tonic-gate             signal(SIGTSTP, SIG_DFL);
14437c478bd9Sstevel@tonic-gate #endif
14447c478bd9Sstevel@tonic-gate         wait ((pid_t)0);
14457c478bd9Sstevel@tonic-gate         signal (SIGINT, end_it);
14467c478bd9Sstevel@tonic-gate         signal (SIGQUIT, onquit);
14477c478bd9Sstevel@tonic-gate 	signal (SIGWINCH, chgwinsz);
14487c478bd9Sstevel@tonic-gate #ifdef SIGTSTP
14497c478bd9Sstevel@tonic-gate         if (catch_susp)
14507c478bd9Sstevel@tonic-gate             signal(SIGTSTP, onsusp);
14517c478bd9Sstevel@tonic-gate #endif
14527c478bd9Sstevel@tonic-gate 	/*
14537c478bd9Sstevel@tonic-gate 	 * Since we were ignoring window change signals while we executed
14547c478bd9Sstevel@tonic-gate 	 * the command, we must assume the window changed.
14557c478bd9Sstevel@tonic-gate 	 */
14567c478bd9Sstevel@tonic-gate 	(void) chgwinsz(0);
14577c478bd9Sstevel@tonic-gate 	set_tty ();
14587c478bd9Sstevel@tonic-gate 
14597c478bd9Sstevel@tonic-gate         pr ("------------------------\n");
14607c478bd9Sstevel@tonic-gate         prompt (filename);
14617c478bd9Sstevel@tonic-gate }
14627c478bd9Sstevel@tonic-gate /*
14637c478bd9Sstevel@tonic-gate ** Skip n lines in the file f
14647c478bd9Sstevel@tonic-gate */
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate static void
14677c478bd9Sstevel@tonic-gate skiplns(register off_t n, register FILE *f)
14687c478bd9Sstevel@tonic-gate {
14697c478bd9Sstevel@tonic-gate     register int c;
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate     while (n > 0) {
14727c478bd9Sstevel@tonic-gate         while ((c = Getc (f)) != '\n')
14737c478bd9Sstevel@tonic-gate             if (c == EOF)
14747c478bd9Sstevel@tonic-gate                 return;
14757c478bd9Sstevel@tonic-gate             n--;
14767c478bd9Sstevel@tonic-gate             Currline++;
14777c478bd9Sstevel@tonic-gate     }
14787c478bd9Sstevel@tonic-gate }
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate /*
14817c478bd9Sstevel@tonic-gate ** Skip nskip files in the file list (from the command line). Nskip may be
14827c478bd9Sstevel@tonic-gate ** negative.
14837c478bd9Sstevel@tonic-gate */
14847c478bd9Sstevel@tonic-gate 
14857c478bd9Sstevel@tonic-gate static void
14867c478bd9Sstevel@tonic-gate skipf(register int nskip)
14877c478bd9Sstevel@tonic-gate {
14887c478bd9Sstevel@tonic-gate     if (nskip == 0) return;
14897c478bd9Sstevel@tonic-gate     if (nskip > 0) {
14907c478bd9Sstevel@tonic-gate         if (fnum + nskip > nfiles - 1)
14917c478bd9Sstevel@tonic-gate             nskip = nfiles - fnum - 1;
14927c478bd9Sstevel@tonic-gate     }
14937c478bd9Sstevel@tonic-gate     else if (within)
14947c478bd9Sstevel@tonic-gate         ++fnum;
14957c478bd9Sstevel@tonic-gate     fnum += nskip;
14967c478bd9Sstevel@tonic-gate     if (fnum < 0)
14977c478bd9Sstevel@tonic-gate         fnum = 0;
14987c478bd9Sstevel@tonic-gate     pr (gettext("\n...Skipping "));
14997c478bd9Sstevel@tonic-gate     pr ("\n");
15007c478bd9Sstevel@tonic-gate     if (clreol)
15017c478bd9Sstevel@tonic-gate         cleareol ();
15027c478bd9Sstevel@tonic-gate     if (nskip > 0)
15037c478bd9Sstevel@tonic-gate 	printf(gettext("...Skipping to file %s\n"), fnames[fnum]);
15047c478bd9Sstevel@tonic-gate     else
15057c478bd9Sstevel@tonic-gate 	printf(gettext("...Skipping back to file %s\n"), fnames[fnum]);
15067c478bd9Sstevel@tonic-gate     if (clreol)
15077c478bd9Sstevel@tonic-gate         cleareol ();
15087c478bd9Sstevel@tonic-gate     pr ("\n");
15097c478bd9Sstevel@tonic-gate     --fnum;
15107c478bd9Sstevel@tonic-gate }
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate /*----------------------------- Terminal I/O -------------------------------*/
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate static void
15157c478bd9Sstevel@tonic-gate initterm(void)
15167c478bd9Sstevel@tonic-gate {
15177c478bd9Sstevel@tonic-gate     int         erret = 0;
15187c478bd9Sstevel@tonic-gate 
15197c478bd9Sstevel@tonic-gate     setbuf(stdout, obuf);
15207c478bd9Sstevel@tonic-gate     if (!(no_tty = ioctl(1, TCGETA, &otty))) {
15217c478bd9Sstevel@tonic-gate 	if (setupterm(NULL, 1, &erret) != OK) {
15227c478bd9Sstevel@tonic-gate             dumb++; ul_opt = 0;
15237c478bd9Sstevel@tonic-gate         }
15247c478bd9Sstevel@tonic-gate         else {
15257c478bd9Sstevel@tonic-gate 	    reset_shell_mode();
15267c478bd9Sstevel@tonic-gate             if (((Lpp = lines) < 0) || hard_copy) {
15277c478bd9Sstevel@tonic-gate                 hard++; /* Hard copy terminal */
15287c478bd9Sstevel@tonic-gate                 Lpp = 24;
15297c478bd9Sstevel@tonic-gate             }
15307c478bd9Sstevel@tonic-gate             if (tailequ(fnames[0], "page") || !hard && (scroll_forward == NULL))
15317c478bd9Sstevel@tonic-gate                 noscroll++;
15327c478bd9Sstevel@tonic-gate             if ((Mcol = columns) < 0)
15337c478bd9Sstevel@tonic-gate                 Mcol = 80;
15347c478bd9Sstevel@tonic-gate             Wrap = tigetflag("am");
15357c478bd9Sstevel@tonic-gate             /*
15367c478bd9Sstevel@tonic-gate              *  Set up for underlining:  some terminals don't need it;
15377c478bd9Sstevel@tonic-gate              *  others have start/stop sequences, still others have an
15387c478bd9Sstevel@tonic-gate              *  underline char sequence which is assumed to move the
15397c478bd9Sstevel@tonic-gate              *  cursor forward one character.  If underline sequence
15407c478bd9Sstevel@tonic-gate              *  isn't available, settle for standout sequence.
15417c478bd9Sstevel@tonic-gate              */
15427c478bd9Sstevel@tonic-gate 
15437c478bd9Sstevel@tonic-gate             if (transparent_underline || over_strike)
15447c478bd9Sstevel@tonic-gate                 ul_opt = 0;
15457c478bd9Sstevel@tonic-gate             if ((ULenter = tigetstr("smul")) == NULL &&
15467c478bd9Sstevel@tonic-gate                 (!underline_char) && (ULenter = tigetstr("smso")) == NULL)
15477c478bd9Sstevel@tonic-gate                 ULenter = "";
15487c478bd9Sstevel@tonic-gate             if ((ULexit = tigetstr("rmul")) == NULL &&
15497c478bd9Sstevel@tonic-gate                 (!underline_char) && (ULexit = tigetstr("rmso")) == NULL)
15507c478bd9Sstevel@tonic-gate                 ULexit = "";
15517c478bd9Sstevel@tonic-gate         }
15527c478bd9Sstevel@tonic-gate         if ((shell = getenv("SHELL")) == NULL)
15537c478bd9Sstevel@tonic-gate             shell = "/usr/bin/sh";
15547c478bd9Sstevel@tonic-gate     }
15557c478bd9Sstevel@tonic-gate     no_intty = ioctl(0, TCGETA, &otty);
15567c478bd9Sstevel@tonic-gate     ioctl(2, TCGETA, &otty);
15577c478bd9Sstevel@tonic-gate     hardtabs = !(otty.c_oflag & TAB3);
15587c478bd9Sstevel@tonic-gate }
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate static int
15617c478bd9Sstevel@tonic-gate readch(void)
15627c478bd9Sstevel@tonic-gate {
15637c478bd9Sstevel@tonic-gate         char ch;
15647c478bd9Sstevel@tonic-gate         extern int errno;
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate         if (read (2, &ch, 1) <= 0)
15677c478bd9Sstevel@tonic-gate                 if (errno != EINTR)
15687c478bd9Sstevel@tonic-gate                         end_it(0);		/* clean up before exiting */
15697c478bd9Sstevel@tonic-gate                 else
15707c478bd9Sstevel@tonic-gate                         ch = otty.c_cc[VKILL];
15717c478bd9Sstevel@tonic-gate         return (ch);
15727c478bd9Sstevel@tonic-gate }
15737c478bd9Sstevel@tonic-gate 
15747c478bd9Sstevel@tonic-gate static char BS = '\b';
15757c478bd9Sstevel@tonic-gate static char CARAT = '^';
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate static void
15787c478bd9Sstevel@tonic-gate ttyin(char buf[], register int nmax, char pchar)
15797c478bd9Sstevel@tonic-gate {
15807c478bd9Sstevel@tonic-gate     register char *sptr;
15817c478bd9Sstevel@tonic-gate     register unsigned char ch;
15827c478bd9Sstevel@tonic-gate     int LengthBuffer[80];
15837c478bd9Sstevel@tonic-gate     int *BufferPointer;
15847c478bd9Sstevel@tonic-gate     int csno;
15857c478bd9Sstevel@tonic-gate     register int slash = 0;
15867c478bd9Sstevel@tonic-gate     int maxlen;
15877c478bd9Sstevel@tonic-gate     char cbuf;
15887c478bd9Sstevel@tonic-gate 
15897c478bd9Sstevel@tonic-gate     BufferPointer = LengthBuffer;
15907c478bd9Sstevel@tonic-gate     sptr = buf;
15917c478bd9Sstevel@tonic-gate     maxlen = 0;
15927c478bd9Sstevel@tonic-gate     while (sptr - buf < nmax) {
15937c478bd9Sstevel@tonic-gate         if (promptlen > maxlen)
15947c478bd9Sstevel@tonic-gate 	    maxlen = promptlen;
15957c478bd9Sstevel@tonic-gate         ch = readch ();
15967c478bd9Sstevel@tonic-gate         csno = csetno(ch);
15977c478bd9Sstevel@tonic-gate         if (!csno) {
15987c478bd9Sstevel@tonic-gate             if (ch == '\\') {
15997c478bd9Sstevel@tonic-gate                 slash++;
16007c478bd9Sstevel@tonic-gate             } else if ((ch == otty.c_cc[VERASE]) && !slash) {
16017c478bd9Sstevel@tonic-gate                 if (sptr > buf) {
16027c478bd9Sstevel@tonic-gate                     --promptlen;
16037c478bd9Sstevel@tonic-gate                     write (2, &BS, 1);
16047c478bd9Sstevel@tonic-gate 		    sptr -= (*--BufferPointer);
16057c478bd9Sstevel@tonic-gate                     if ((*sptr < ' ' && *sptr != '\n') || *sptr == RUBOUT) {
16067c478bd9Sstevel@tonic-gate                         --promptlen;
16077c478bd9Sstevel@tonic-gate                         write (2, &BS, 1);
16087c478bd9Sstevel@tonic-gate                     }
16097c478bd9Sstevel@tonic-gate                     continue;
16107c478bd9Sstevel@tonic-gate                 } else {
16117c478bd9Sstevel@tonic-gate                     if (!clr_eol)
16127c478bd9Sstevel@tonic-gate 			promptlen = maxlen;
16137c478bd9Sstevel@tonic-gate                     longjmp (restore, 1);
16147c478bd9Sstevel@tonic-gate                 }
16157c478bd9Sstevel@tonic-gate             } else if ((ch == otty.c_cc[VKILL]) && !slash) {
16167c478bd9Sstevel@tonic-gate                 if (hard) {
16177c478bd9Sstevel@tonic-gate                     show(ch);
16187c478bd9Sstevel@tonic-gate                     putchar ('\n');
16197c478bd9Sstevel@tonic-gate                     putchar (pchar);
16207c478bd9Sstevel@tonic-gate                 } else {
16217c478bd9Sstevel@tonic-gate                     putchar ('\r');
16227c478bd9Sstevel@tonic-gate 		    putchar (pchar);
16237c478bd9Sstevel@tonic-gate                     if (clr_eol)
16247c478bd9Sstevel@tonic-gate                         prmpt_erase (1);
16257c478bd9Sstevel@tonic-gate                     promptlen = 1;
16267c478bd9Sstevel@tonic-gate                 }
16277c478bd9Sstevel@tonic-gate                 sptr = buf;
16287c478bd9Sstevel@tonic-gate                 fflush (stdout);
16297c478bd9Sstevel@tonic-gate                 continue;
16307c478bd9Sstevel@tonic-gate             }
16317c478bd9Sstevel@tonic-gate             if (slash && (ch == otty.c_cc[VKILL] || ch == otty.c_cc[VERASE])) {
16327c478bd9Sstevel@tonic-gate                 write (2, &BS, 1);
16337c478bd9Sstevel@tonic-gate 	        sptr -= (*--BufferPointer);
16347c478bd9Sstevel@tonic-gate             }
16357c478bd9Sstevel@tonic-gate             if (ch != '\\')
16367c478bd9Sstevel@tonic-gate                 slash = 0;
16377c478bd9Sstevel@tonic-gate 	    *BufferPointer++ = 1;
16387c478bd9Sstevel@tonic-gate             *sptr++ = ch;
16397c478bd9Sstevel@tonic-gate             if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) {
16407c478bd9Sstevel@tonic-gate                 ch += ch == RUBOUT ? -0100 : 0100;
16417c478bd9Sstevel@tonic-gate                 write (2, &CARAT, 1);
16427c478bd9Sstevel@tonic-gate                 promptlen++;
16437c478bd9Sstevel@tonic-gate             }
16447c478bd9Sstevel@tonic-gate             cbuf = ch;
16457c478bd9Sstevel@tonic-gate             if (ch != '\n' && ch != ESC) {
16467c478bd9Sstevel@tonic-gate                 write (2, &cbuf, 1);
16477c478bd9Sstevel@tonic-gate                 promptlen++;
16487c478bd9Sstevel@tonic-gate             } else
16497c478bd9Sstevel@tonic-gate                 break;
16507c478bd9Sstevel@tonic-gate             /* end of code set 0 */
16517c478bd9Sstevel@tonic-gate         } else {
16527c478bd9Sstevel@tonic-gate 	    int i;
16537c478bd9Sstevel@tonic-gate 	    u_char buffer[5];
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 	    *BufferPointer++ = cw[csno];
16567c478bd9Sstevel@tonic-gate 	    buffer[0] = *sptr++ = ch;
16577c478bd9Sstevel@tonic-gate 	    for(i=1; i<cw[csno]; i++) {
16587c478bd9Sstevel@tonic-gate 	        buffer[i] = *sptr++ = readch();
16597c478bd9Sstevel@tonic-gate 	    }
16607c478bd9Sstevel@tonic-gate 	    buffer[i]='\0';
16617c478bd9Sstevel@tonic-gate 	    write(2, buffer, strlen((char *)buffer));
16627c478bd9Sstevel@tonic-gate 	}
16637c478bd9Sstevel@tonic-gate     }
16647c478bd9Sstevel@tonic-gate     *--sptr = '\0';
16657c478bd9Sstevel@tonic-gate     if (!clr_eol) promptlen = maxlen;
16667c478bd9Sstevel@tonic-gate     if (sptr - buf >= nmax - 1)
16677c478bd9Sstevel@tonic-gate         error (gettext("Line too long"));
16687c478bd9Sstevel@tonic-gate }
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate static int
16717c478bd9Sstevel@tonic-gate expand(char *outbuf, char *inbuf)
16727c478bd9Sstevel@tonic-gate {
1673*ca3f9453Sas 	char *in_str;
1674*ca3f9453Sas 	char *out_str;
1675*ca3f9453Sas 	char ch;
1676*ca3f9453Sas 	char temp[PATH_MAX];
1677*ca3f9453Sas 	int changed = 0;
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate     in_str = inbuf;
16807c478bd9Sstevel@tonic-gate     out_str = temp;
16817c478bd9Sstevel@tonic-gate     while ((ch = *in_str++) != '\0')
16827c478bd9Sstevel@tonic-gate         switch (ch) {
16837c478bd9Sstevel@tonic-gate         case '%':
16847c478bd9Sstevel@tonic-gate             if (!no_intty) {
1685*ca3f9453Sas 		if (strlcpy(out_str, fnames[fnum], sizeof (temp))
1686*ca3f9453Sas 		    >= sizeof (temp))
1687*ca3f9453Sas 			error(gettext("Command too long"));
16887c478bd9Sstevel@tonic-gate                 out_str += strlen (fnames[fnum]);
16897c478bd9Sstevel@tonic-gate                 changed++;
16907c478bd9Sstevel@tonic-gate             }
16917c478bd9Sstevel@tonic-gate             else
16927c478bd9Sstevel@tonic-gate                 *out_str++ = ch;
16937c478bd9Sstevel@tonic-gate             break;
16947c478bd9Sstevel@tonic-gate         case '!':
16957c478bd9Sstevel@tonic-gate             if (!shellp)
16967c478bd9Sstevel@tonic-gate                 error (gettext("No previous command to substitute for"));
1697*ca3f9453Sas 	    if (strlcpy(out_str, shell_line, sizeof (temp)) >= sizeof (temp))
1698*ca3f9453Sas 		error(gettext("Command too long"));
16997c478bd9Sstevel@tonic-gate             out_str += strlen (shell_line);
17007c478bd9Sstevel@tonic-gate             changed++;
17017c478bd9Sstevel@tonic-gate             break;
17027c478bd9Sstevel@tonic-gate         case '\\':
17037c478bd9Sstevel@tonic-gate             if (*in_str == '%' || *in_str == '!') {
17047c478bd9Sstevel@tonic-gate                 *out_str++ = *in_str++;
17057c478bd9Sstevel@tonic-gate                 break;
17067c478bd9Sstevel@tonic-gate             }
17077c478bd9Sstevel@tonic-gate         default:
17087c478bd9Sstevel@tonic-gate             *out_str++ = ch;
17097c478bd9Sstevel@tonic-gate         }
17107c478bd9Sstevel@tonic-gate     *out_str++ = '\0';
1711*ca3f9453Sas 	if (strlcpy(outbuf, temp, sizeof (shell_line)) >= sizeof (shell_line))
1712*ca3f9453Sas 		error(gettext("Command too long"));
17137c478bd9Sstevel@tonic-gate     return (changed);
17147c478bd9Sstevel@tonic-gate }
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate static void
17177c478bd9Sstevel@tonic-gate show(register char ch)
17187c478bd9Sstevel@tonic-gate {
17197c478bd9Sstevel@tonic-gate     char cbuf;
17207c478bd9Sstevel@tonic-gate 
17217c478bd9Sstevel@tonic-gate     if ((ch < ' ' && ch != '\n' && ch != ESC) || ch == RUBOUT) {
17227c478bd9Sstevel@tonic-gate         ch += ch == RUBOUT ? -0100 : 0100;
17237c478bd9Sstevel@tonic-gate         write (2, &CARAT, 1);
17247c478bd9Sstevel@tonic-gate         promptlen++;
17257c478bd9Sstevel@tonic-gate     }
17267c478bd9Sstevel@tonic-gate     cbuf = ch;
17277c478bd9Sstevel@tonic-gate     write (2, &cbuf, 1);
17287c478bd9Sstevel@tonic-gate     promptlen++;
17297c478bd9Sstevel@tonic-gate }
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate static void
17327c478bd9Sstevel@tonic-gate error (char *mess)
17337c478bd9Sstevel@tonic-gate {
17347c478bd9Sstevel@tonic-gate     if (clreol)
17357c478bd9Sstevel@tonic-gate         cleareol ();
17367c478bd9Sstevel@tonic-gate     else
17377c478bd9Sstevel@tonic-gate         kill_line ();
17387c478bd9Sstevel@tonic-gate     promptlen += strlen (mess);
17397c478bd9Sstevel@tonic-gate     if (enter_standout_mode && exit_standout_mode) {
17407c478bd9Sstevel@tonic-gate         putp (enter_standout_mode);
17417c478bd9Sstevel@tonic-gate         pr(mess);
17427c478bd9Sstevel@tonic-gate         putp (exit_standout_mode);
17437c478bd9Sstevel@tonic-gate     }
17447c478bd9Sstevel@tonic-gate     else
17457c478bd9Sstevel@tonic-gate         pr (mess);
17467c478bd9Sstevel@tonic-gate     fflush(stdout);
17477c478bd9Sstevel@tonic-gate     errors++;
17487c478bd9Sstevel@tonic-gate     longjmp (restore, 1);
17497c478bd9Sstevel@tonic-gate }
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate 
17527c478bd9Sstevel@tonic-gate static void
17537c478bd9Sstevel@tonic-gate set_tty(void)
17547c478bd9Sstevel@tonic-gate {
17557c478bd9Sstevel@tonic-gate         ioctl(2, TCGETA, &otty);     /* save old tty modes */
17567c478bd9Sstevel@tonic-gate         ioctl(2, TCGETA, &ntty);
17577c478bd9Sstevel@tonic-gate         ntty.c_lflag &= ~ECHO & ~ICANON;
17587c478bd9Sstevel@tonic-gate         ntty.c_cc[VMIN] = (char)1;
17597c478bd9Sstevel@tonic-gate         ntty.c_cc[VTIME] = (char)0;
17607c478bd9Sstevel@tonic-gate         ioctl (2, TCSETAF, &ntty);        /* set new tty modes */
17617c478bd9Sstevel@tonic-gate }
17627c478bd9Sstevel@tonic-gate 
17637c478bd9Sstevel@tonic-gate static void
17647c478bd9Sstevel@tonic-gate reset_tty(void)
17657c478bd9Sstevel@tonic-gate {
17667c478bd9Sstevel@tonic-gate         ioctl (2, TCSETAF, &otty);        /* reset tty modes */
17677c478bd9Sstevel@tonic-gate }
17687c478bd9Sstevel@tonic-gate 
17697c478bd9Sstevel@tonic-gate static void
17707c478bd9Sstevel@tonic-gate rdline(register FILE *f)
17717c478bd9Sstevel@tonic-gate {
17727c478bd9Sstevel@tonic-gate     register int c;
17737c478bd9Sstevel@tonic-gate     register char *p;
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate     p = Line;
17767c478bd9Sstevel@tonic-gate     while ((c = Getc (f)) != '\n' && c != EOF && p - Line < LINSIZ - 1)
17777c478bd9Sstevel@tonic-gate         *p++ = c;
17787c478bd9Sstevel@tonic-gate     if (c == '\n')
17797c478bd9Sstevel@tonic-gate         Currline++;
17807c478bd9Sstevel@tonic-gate     *p = '\0';
17817c478bd9Sstevel@tonic-gate }
17827c478bd9Sstevel@tonic-gate 
17837c478bd9Sstevel@tonic-gate /* Come here when we get a suspend signal from the terminal */
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate /*
17867c478bd9Sstevel@tonic-gate  * sig is put in as a dummy arg to have the compiler not to complain
17877c478bd9Sstevel@tonic-gate  */
17887c478bd9Sstevel@tonic-gate #ifdef SIGTSTP
17897c478bd9Sstevel@tonic-gate /* ARGSUSED */
17907c478bd9Sstevel@tonic-gate void
17917c478bd9Sstevel@tonic-gate onsusp(int sig)
17927c478bd9Sstevel@tonic-gate {
17937c478bd9Sstevel@tonic-gate     /* ignore SIGTTOU so we don't get stopped if csh grabs the tty */
17947c478bd9Sstevel@tonic-gate     signal(SIGTTOU, SIG_IGN);
17957c478bd9Sstevel@tonic-gate     reset_tty ();
17967c478bd9Sstevel@tonic-gate     fflush (stdout);
17977c478bd9Sstevel@tonic-gate     signal(SIGTTOU, SIG_DFL);
17987c478bd9Sstevel@tonic-gate 
17997c478bd9Sstevel@tonic-gate     /* Send the TSTP signal to suspend our process group */
18007c478bd9Sstevel@tonic-gate     kill (0, SIGTSTP);
18017c478bd9Sstevel@tonic-gate     /* Pause for station break */
18027c478bd9Sstevel@tonic-gate 
18037c478bd9Sstevel@tonic-gate     /* We're back */
18047c478bd9Sstevel@tonic-gate     signal (SIGTSTP, onsusp);
18057c478bd9Sstevel@tonic-gate     set_tty ();
18067c478bd9Sstevel@tonic-gate     if (inwait)
18077c478bd9Sstevel@tonic-gate             longjmp (restore, 1);
18087c478bd9Sstevel@tonic-gate }
18097c478bd9Sstevel@tonic-gate #endif
1810