NetBSD/games/hack/hack.pager.c

438 lines
8.4 KiB
C
Raw Normal View History

/* $NetBSD: hack.pager.c,v 1.6 2001/03/25 20:44:02 jsm Exp $ */
1997-10-19 20:56:41 +04:00
/*
* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
*/
1997-10-19 20:56:41 +04:00
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: hack.pager.c,v 1.6 2001/03/25 20:44:02 jsm Exp $");
1997-10-19 20:56:41 +04:00
#endif /* not lint */
1993-03-21 12:45:37 +03:00
/* This file contains the command routine dowhatis() and a pager. */
1997-10-19 20:56:41 +04:00
/*
* Also readmail() and doshell(), and generally the things that contact the
* outside world.
*/
#include <sys/types.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
1993-03-21 12:45:37 +03:00
#include "hack.h"
1997-10-19 20:56:41 +04:00
#include "extern.h"
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
int
1993-03-21 12:45:37 +03:00
dowhatis()
{
1997-10-19 20:56:41 +04:00
FILE *fp;
char bufr[BUFSZ + 6];
char *buf = &bufr[6], *ep, q;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
if (!(fp = fopen(DATAFILE, "r")))
1993-03-21 12:45:37 +03:00
pline("Cannot open data file!");
else {
pline("Specify what? ");
q = readchar();
1997-10-19 20:56:41 +04:00
if (q != '\t')
while (fgets(buf, BUFSZ, fp))
if (*buf == q) {
ep = strchr(buf, '\n');
if (ep)
*ep = 0;
/* else: bad data file */
/* Expand tab 'by hand' */
if (buf[1] == '\t') {
buf = bufr;
buf[0] = q;
(void) strncpy(buf + 1, " ", 7);
}
pline(buf);
if (ep[-1] == ';') {
pline("More info? ");
if (readchar() == 'y') {
page_more(fp, 1); /* does fclose() */
return (0);
}
}
(void) fclose(fp); /* kopper@psuvax1 */
return (0);
1993-03-21 12:45:37 +03:00
}
pline("I've never heard of such things.");
(void) fclose(fp);
}
1997-10-19 20:56:41 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
/* make the paging of a file interruptible */
1997-10-19 20:56:41 +04:00
static int got_intrup;
1993-03-21 12:45:37 +03:00
void
1997-10-19 20:56:41 +04:00
intruph(n)
int n __attribute__((__unused__));
1997-10-19 20:56:41 +04:00
{
1993-03-21 12:45:37 +03:00
got_intrup++;
}
/* simple pager, also used from dohelp() */
1997-10-19 20:56:41 +04:00
void
page_more(fp, strip)
FILE *fp;
int strip; /* nr of chars to be stripped from each line
* (0 or 1) */
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
char *bufr, *ep;
sig_t prevsig = signal(SIGINT, intruph);
1993-03-21 12:45:37 +03:00
set_pager(0);
bufr = (char *) alloc((unsigned) CO);
1997-10-19 20:56:41 +04:00
bufr[CO - 1] = 0;
while (fgets(bufr, CO - 1, fp) && (!strip || *bufr == '\t') && !got_intrup) {
ep = strchr(bufr, '\n');
if (ep)
1993-03-21 12:45:37 +03:00
*ep = 0;
1997-10-19 20:56:41 +04:00
if (page_line(bufr + strip)) {
1993-03-21 12:45:37 +03:00
set_pager(2);
goto ret;
}
}
set_pager(1);
ret:
free(bufr);
(void) fclose(fp);
(void) signal(SIGINT, prevsig);
got_intrup = 0;
}
1997-10-19 20:56:41 +04:00
static boolean whole_screen = TRUE;
#define PAGMIN 12 /* minimum # of lines for page below level
* map */
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
void
set_whole_screen()
{ /* called in termcap as soon as LI is known */
whole_screen = (LI - ROWNO - 2 <= PAGMIN || !CD);
1993-03-21 12:45:37 +03:00
}
#ifdef NEWS
1997-10-19 20:56:41 +04:00
int
readnews()
{
int ret;
1993-03-21 12:45:37 +03:00
whole_screen = TRUE; /* force a docrt(), our first */
ret = page_file(NEWS, TRUE);
set_whole_screen();
1997-10-19 20:56:41 +04:00
return (ret); /* report whether we did docrt() */
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
#endif /* NEWS */
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
void
1993-03-21 12:45:37 +03:00
set_pager(mode)
1997-10-19 20:56:41 +04:00
int mode; /* 0: open 1: wait+close 2: close */
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
static boolean so;
if (mode == 0) {
if (!whole_screen) {
1993-03-21 12:45:37 +03:00
/* clear topline */
clrlin();
/* use part of screen below level map */
1997-10-19 20:56:41 +04:00
curs(1, ROWNO + 4);
1993-03-21 12:45:37 +03:00
} else {
cls();
}
so = flags.standout;
flags.standout = 1;
} else {
1997-10-19 20:56:41 +04:00
if (mode == 1) {
1993-03-21 12:45:37 +03:00
curs(1, LI);
more();
}
flags.standout = so;
1997-10-19 20:56:41 +04:00
if (whole_screen)
1993-03-21 12:45:37 +03:00
docrt();
else {
1997-10-19 20:56:41 +04:00
curs(1, ROWNO + 4);
1993-03-21 12:45:37 +03:00
cl_eos();
}
}
}
1997-10-19 20:56:41 +04:00
int
page_line(s) /* returns 1 if we should quit */
const char *s;
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
if (cury == LI - 1) {
if (!*s)
return (0); /* suppress blank lines at top */
1993-03-21 12:45:37 +03:00
putchar('\n');
cury++;
cmore("q\033");
1997-10-19 20:56:41 +04:00
if (morc) {
1993-03-21 12:45:37 +03:00
morc = 0;
1997-10-19 20:56:41 +04:00
return (1);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
if (whole_screen)
1993-03-21 12:45:37 +03:00
cls();
else {
1997-10-19 20:56:41 +04:00
curs(1, ROWNO + 4);
1993-03-21 12:45:37 +03:00
cl_eos();
}
}
puts(s);
cury++;
1997-10-19 20:56:41 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
/*
* Flexible pager: feed it with a number of lines and it will decide
* whether these should be fed to the pager above, or displayed in a
* corner.
* Call:
* cornline(0, title or 0) : initialize
* cornline(1, text) : add text to the chain of texts
* cornline(2, morcs) : output everything and cleanup
* cornline(3, 0) : cleanup
*/
1997-10-19 20:56:41 +04:00
void
1993-03-21 12:45:37 +03:00
cornline(mode, text)
1997-10-19 20:56:41 +04:00
int mode;
const char *text;
1993-03-21 12:45:37 +03:00
{
static struct line {
1997-10-19 20:56:41 +04:00
struct line *next_line;
char *line_text;
} *texthead, *texttail;
static int maxlen;
static int linect;
struct line *tl;
if (mode == 0) {
1993-03-21 12:45:37 +03:00
texthead = 0;
maxlen = 0;
linect = 0;
1997-10-19 20:56:41 +04:00
if (text) {
1993-03-21 12:45:37 +03:00
cornline(1, text); /* title */
cornline(1, ""); /* blank line */
}
return;
}
1997-10-19 20:56:41 +04:00
if (mode == 1) {
int len;
if (!text)
return; /* superfluous, just to be sure */
linect++;
len = strlen(text);
if (len > maxlen)
maxlen = len;
tl = (struct line *)
alloc((unsigned) (len + sizeof(struct line) + 1));
tl->next_line = 0;
tl->line_text = (char *) (tl + 1);
(void) strcpy(tl->line_text, text);
if (!texthead)
texthead = tl;
else
texttail->next_line = tl;
texttail = tl;
return;
1993-03-21 12:45:37 +03:00
}
/* --- now we really do it --- */
1997-10-19 20:56:41 +04:00
if (mode == 2 && linect == 1) /* topline only */
1993-03-21 12:45:37 +03:00
pline(texthead->line_text);
1997-10-19 20:56:41 +04:00
else if (mode == 2) {
int curline, lth;
if (flags.toplin == 1)
more(); /* ab@unido */
remember_topl();
lth = CO - maxlen - 2; /* Use full screen width */
if (linect < LI && lth >= 10) { /* in a corner */
home();
cl_end();
flags.toplin = 0;
curline = 1;
for (tl = texthead; tl; tl = tl->next_line) {
curs(lth, curline);
if (curline > 1)
cl_end();
putsym(' ');
putstr(tl->line_text);
curline++;
}
curs(lth, curline);
cl_end();
cmore(text);
home();
cl_end();
docorner(lth, curline - 1);
} else { /* feed to pager */
set_pager(0);
for (tl = texthead; tl; tl = tl->next_line) {
if (page_line(tl->line_text)) {
set_pager(2);
goto cleanup;
}
}
if (text) {
cgetret(text);
set_pager(2);
} else
set_pager(1);
1993-03-21 12:45:37 +03:00
}
}
cleanup:
1997-10-19 20:56:41 +04:00
while ((tl = texthead) != NULL) {
1993-03-21 12:45:37 +03:00
texthead = tl->next_line;
free((char *) tl);
}
}
1997-10-19 20:56:41 +04:00
int
1993-03-21 12:45:37 +03:00
dohelp()
{
1997-10-19 20:56:41 +04:00
char c;
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
pline("Long or short help? ");
while (((c = readchar()) != 'l') && (c != 's') && !strchr(quitchars, c))
bell();
if (!strchr(quitchars, c))
1993-03-21 12:45:37 +03:00
(void) page_file((c == 'l') ? HELP : SHELP, FALSE);
1997-10-19 20:56:41 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
int
page_file(fnam, silent) /* return: 0 - cannot open fnam; 1 -
* otherwise */
const char *fnam;
1997-10-19 20:56:41 +04:00
boolean silent;
1993-03-21 12:45:37 +03:00
{
1997-10-19 20:56:41 +04:00
#ifdef DEF_PAGER /* this implies that UNIX is defined */
{
/* use external pager; this may give security problems */
1993-03-21 12:45:37 +03:00
int fd = open(fnam, O_RDONLY);
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
if (fd < 0) {
if (!silent)
pline("Cannot open %s.", fnam);
return (0);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
if (child(1)) {
/*
* Now that child() does a setuid(getuid()) and a
* chdir(), we may not be able to open file fnam
* anymore, so make it stdin.
*/
(void) close(0);
if (dup(fd)) {
if (!silent)
printf("Cannot open %s as stdin.\n", fnam);
} else {
execl(catmore, "page", (char *) 0);
if (!silent)
printf("Cannot exec %s.\n", catmore);
}
exit(1);
}
(void) close(fd);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
#else /* DEF_PAGER */
{
FILE *f; /* free after Robert Viduya */
if ((f = fopen(fnam, "r")) == (FILE *) 0) {
if (!silent) {
home();
perror(fnam);
flags.toplin = 1;
pline("Cannot open %s.", fnam);
}
return (0);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
page_more(f, 0);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
#endif /* DEF_PAGER */
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
return (1);
1993-03-21 12:45:37 +03:00
}
#ifdef UNIX
#ifdef SHELL
1997-10-19 20:56:41 +04:00
int
dosh()
{
char *str;
if (child(0)) {
if ((str = getenv("SHELL")) != NULL)
1993-03-21 12:45:37 +03:00
execl(str, str, (char *) 0);
else
execl("/bin/sh", "sh", (char *) 0);
pline("sh: cannot execute.");
exit(1);
}
1997-10-19 20:56:41 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
#endif /* SHELL */
1993-03-21 12:45:37 +03:00
#ifdef NOWAITINCLUDE
1997-10-19 20:56:41 +04:00
union wait { /* used only for the cast (union wait *) 0 */
int w_status;
1993-03-21 12:45:37 +03:00
struct {
1997-10-19 20:56:41 +04:00
unsigned short w_Termsig:7;
unsigned short w_Coredump:1;
unsigned short w_Retcode:8;
} w_T;
1993-03-21 12:45:37 +03:00
};
#else
#ifdef BSD
#include <sys/wait.h>
#else
#include <wait.h>
1997-10-19 20:56:41 +04:00
#endif /* BSD */
#endif /* NOWAITINCLUDE */
1993-03-21 12:45:37 +03:00
1997-10-19 20:56:41 +04:00
int
child(int wt)
1997-10-19 20:56:41 +04:00
{
int status;
int f;
1993-03-21 12:45:37 +03:00
f = fork();
1997-10-19 20:56:41 +04:00
if (f == 0) { /* child */
settty((char *) 0); /* also calls end_screen() */
1993-03-21 12:45:37 +03:00
(void) setuid(getuid());
(void) setgid(getgid());
#ifdef CHDIR
(void) chdir(getenv("HOME"));
1997-10-19 20:56:41 +04:00
#endif /* CHDIR */
return (1);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
if (f == -1) { /* cannot fork */
1993-03-21 12:45:37 +03:00
pline("Fork failed. Try again.");
1997-10-19 20:56:41 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
/* fork succeeded; wait for child to exit */
1997-10-19 20:56:41 +04:00
(void) signal(SIGINT, SIG_IGN);
(void) signal(SIGQUIT, SIG_IGN);
1993-03-21 12:45:37 +03:00
(void) wait(&status);
gettty();
setftty();
1997-10-19 20:56:41 +04:00
(void) signal(SIGINT, done1);
1993-03-21 12:45:37 +03:00
#ifdef WIZARD
1997-10-19 20:56:41 +04:00
if (wizard)
(void) signal(SIGQUIT, SIG_DFL);
#endif /* WIZARD */
if (wt)
getret();
1993-03-21 12:45:37 +03:00
docrt();
1997-10-19 20:56:41 +04:00
return (0);
1993-03-21 12:45:37 +03:00
}
1997-10-19 20:56:41 +04:00
#endif /* UNIX */