10Sstevel@tonic-gate /*
2*2881Smp153739 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
30Sstevel@tonic-gate * Use is subject to license terms.
40Sstevel@tonic-gate */
50Sstevel@tonic-gate
60Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
70Sstevel@tonic-gate
80Sstevel@tonic-gate /*
90Sstevel@tonic-gate * Pager: Routines to create a "more" running out of a particular file
100Sstevel@tonic-gate * descriptor.
110Sstevel@tonic-gate *
120Sstevel@tonic-gate * Copyright 1987, 1988 by MIT Student Information Processing Board
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * For copyright information, see copyright.h.
150Sstevel@tonic-gate */
160Sstevel@tonic-gate
170Sstevel@tonic-gate #include "ss_internal.h"
180Sstevel@tonic-gate #include "copyright.h"
19*2881Smp153739 #include <errno.h>
200Sstevel@tonic-gate #include <stdio.h>
210Sstevel@tonic-gate #include <sys/types.h>
220Sstevel@tonic-gate #include <sys/file.h>
230Sstevel@tonic-gate #include <signal.h>
240Sstevel@tonic-gate
250Sstevel@tonic-gate static char MORE[] = "more";
260Sstevel@tonic-gate extern char *_ss_pager_name;
270Sstevel@tonic-gate extern char *getenv();
280Sstevel@tonic-gate
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate * this needs a *lot* of work....
310Sstevel@tonic-gate *
320Sstevel@tonic-gate * run in same process
330Sstevel@tonic-gate * handle SIGINT sensibly
340Sstevel@tonic-gate * allow finer control -- put-page-break-here
350Sstevel@tonic-gate */
360Sstevel@tonic-gate void ss_page_stdin();
370Sstevel@tonic-gate
380Sstevel@tonic-gate #ifndef NO_FORK
ss_pager_create()390Sstevel@tonic-gate int ss_pager_create()
400Sstevel@tonic-gate {
410Sstevel@tonic-gate int filedes[2];
420Sstevel@tonic-gate
430Sstevel@tonic-gate if (pipe(filedes) != 0)
440Sstevel@tonic-gate return(-1);
450Sstevel@tonic-gate
46*2881Smp153739 switch((int) fork()) {
470Sstevel@tonic-gate case -1:
480Sstevel@tonic-gate return(-1);
490Sstevel@tonic-gate case 0:
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate * Child; dup read half to 0, close all but 0, 1, and 2
520Sstevel@tonic-gate */
530Sstevel@tonic-gate if (dup2(filedes[0], 0) == -1)
540Sstevel@tonic-gate exit(1);
550Sstevel@tonic-gate ss_page_stdin();
560Sstevel@tonic-gate default:
570Sstevel@tonic-gate /*
580Sstevel@tonic-gate * Parent: close "read" side of pipe, return
590Sstevel@tonic-gate * "write" side.
600Sstevel@tonic-gate */
610Sstevel@tonic-gate (void) close(filedes[0]);
620Sstevel@tonic-gate return(filedes[1]);
630Sstevel@tonic-gate }
640Sstevel@tonic-gate }
650Sstevel@tonic-gate #else /* don't fork */
ss_pager_create()660Sstevel@tonic-gate int ss_pager_create()
670Sstevel@tonic-gate {
680Sstevel@tonic-gate int fd;
690Sstevel@tonic-gate fd = open("/dev/tty", O_WRONLY, 0);
700Sstevel@tonic-gate return fd;
710Sstevel@tonic-gate }
720Sstevel@tonic-gate #endif
730Sstevel@tonic-gate
ss_page_stdin()740Sstevel@tonic-gate void ss_page_stdin()
750Sstevel@tonic-gate {
760Sstevel@tonic-gate int i;
770Sstevel@tonic-gate #ifdef POSIX_SIGNALS
780Sstevel@tonic-gate struct sigaction sa;
790Sstevel@tonic-gate sigset_t mask;
800Sstevel@tonic-gate #endif
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate * Close all fd's, excepting stdin/stdout/stderr
830Sstevel@tonic-gate */
840Sstevel@tonic-gate closefrom(3);
850Sstevel@tonic-gate
860Sstevel@tonic-gate #ifdef POSIX_SIGNALS
870Sstevel@tonic-gate sa.sa_handler = SIG_DFL;
880Sstevel@tonic-gate sigemptyset(&sa.sa_mask);
890Sstevel@tonic-gate sa.sa_flags = 0;
900Sstevel@tonic-gate sigaction(SIGINT, &sa, (struct sigaction *)0);
910Sstevel@tonic-gate #else
920Sstevel@tonic-gate (void) signal(SIGINT, SIG_DFL);
930Sstevel@tonic-gate #endif
940Sstevel@tonic-gate {
950Sstevel@tonic-gate #ifdef POSIX_SIGNALS
960Sstevel@tonic-gate sigemptyset(&mask);
970Sstevel@tonic-gate sigaddset(&mask, SIGINT);
980Sstevel@tonic-gate sigprocmask(SIG_UNBLOCK, &mask, (sigset_t *)0);
990Sstevel@tonic-gate #else
1000Sstevel@tonic-gate int mask = sigblock(0);
1010Sstevel@tonic-gate mask &= ~sigmask(SIGINT);
1020Sstevel@tonic-gate sigsetmask(mask);
1030Sstevel@tonic-gate #endif
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate if (_ss_pager_name == (char *)NULL) {
1060Sstevel@tonic-gate if ((_ss_pager_name = getenv("PAGER")) == (char *)NULL)
1070Sstevel@tonic-gate _ss_pager_name = MORE;
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate (void) execlp(_ss_pager_name, _ss_pager_name, (char *) NULL);
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate /* minimal recovery if pager program isn't found */
1120Sstevel@tonic-gate char buf[80];
1130Sstevel@tonic-gate register int n;
1140Sstevel@tonic-gate while ((n = read(0, buf, 80)) > 0)
115*2881Smp153739 write(1, buf, (unsigned) n);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate exit(errno);
1180Sstevel@tonic-gate }
119