180354f26Sbostic /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
280354f26Sbostic /* hack.pager.c - version 1.0.3 */
380354f26Sbostic
480354f26Sbostic /* This file contains the command routine dowhatis() and a pager. */
580354f26Sbostic /* Also readmail() and doshell(), and generally the things that
680354f26Sbostic contact the outside world. */
780354f26Sbostic
8fbcc2dedSbostic #include <sys/types.h>
9fbcc2dedSbostic #include <sys/signal.h>
1080354f26Sbostic #include <stdio.h>
1180354f26Sbostic #include "hack.h"
1280354f26Sbostic extern int CO, LI; /* usually COLNO and ROWNO+2 */
1380354f26Sbostic extern char *CD;
1480354f26Sbostic extern char quitchars[];
1580354f26Sbostic extern char *getenv(), *getlogin();
16*a8c302d4Sbostic void done1();
1780354f26Sbostic
dowhatis()1880354f26Sbostic dowhatis()
1980354f26Sbostic {
2080354f26Sbostic FILE *fp;
2180354f26Sbostic char bufr[BUFSZ+6];
2280354f26Sbostic register char *buf = &bufr[6], *ep, q;
2380354f26Sbostic extern char readchar();
2480354f26Sbostic
2580354f26Sbostic if(!(fp = fopen(DATAFILE, "r")))
2680354f26Sbostic pline("Cannot open data file!");
2780354f26Sbostic else {
2880354f26Sbostic pline("Specify what? ");
2980354f26Sbostic q = readchar();
3080354f26Sbostic if(q != '\t')
3180354f26Sbostic while(fgets(buf,BUFSZ,fp))
3280354f26Sbostic if(*buf == q) {
3380354f26Sbostic ep = index(buf, '\n');
3480354f26Sbostic if(ep) *ep = 0;
3580354f26Sbostic /* else: bad data file */
3680354f26Sbostic /* Expand tab 'by hand' */
3780354f26Sbostic if(buf[1] == '\t'){
3880354f26Sbostic buf = bufr;
3980354f26Sbostic buf[0] = q;
4080354f26Sbostic (void) strncpy(buf+1, " ", 7);
4180354f26Sbostic }
4280354f26Sbostic pline(buf);
4380354f26Sbostic if(ep[-1] == ';') {
4480354f26Sbostic pline("More info? ");
4580354f26Sbostic if(readchar() == 'y') {
4680354f26Sbostic page_more(fp,1); /* does fclose() */
4780354f26Sbostic return(0);
4880354f26Sbostic }
4980354f26Sbostic }
5080354f26Sbostic (void) fclose(fp); /* kopper@psuvax1 */
5180354f26Sbostic return(0);
5280354f26Sbostic }
5380354f26Sbostic pline("I've never heard of such things.");
5480354f26Sbostic (void) fclose(fp);
5580354f26Sbostic }
5680354f26Sbostic return(0);
5780354f26Sbostic }
5880354f26Sbostic
5980354f26Sbostic /* make the paging of a file interruptible */
6080354f26Sbostic static int got_intrup;
6180354f26Sbostic
625eb98a4dSbostic void
intruph()6380354f26Sbostic intruph(){
6480354f26Sbostic got_intrup++;
6580354f26Sbostic }
6680354f26Sbostic
6780354f26Sbostic /* simple pager, also used from dohelp() */
page_more(fp,strip)6880354f26Sbostic page_more(fp,strip)
6980354f26Sbostic FILE *fp;
7080354f26Sbostic int strip; /* nr of chars to be stripped from each line (0 or 1) */
7180354f26Sbostic {
7280354f26Sbostic register char *bufr, *ep;
735eb98a4dSbostic sig_t prevsig = signal(SIGINT, intruph);
7480354f26Sbostic
7580354f26Sbostic set_pager(0);
7680354f26Sbostic bufr = (char *) alloc((unsigned) CO);
7780354f26Sbostic bufr[CO-1] = 0;
7880354f26Sbostic while(fgets(bufr,CO-1,fp) && (!strip || *bufr == '\t') && !got_intrup){
7980354f26Sbostic ep = index(bufr, '\n');
8080354f26Sbostic if(ep)
8180354f26Sbostic *ep = 0;
8280354f26Sbostic if(page_line(bufr+strip)) {
8380354f26Sbostic set_pager(2);
8480354f26Sbostic goto ret;
8580354f26Sbostic }
8680354f26Sbostic }
8780354f26Sbostic set_pager(1);
8880354f26Sbostic ret:
8980354f26Sbostic free(bufr);
9080354f26Sbostic (void) fclose(fp);
9180354f26Sbostic (void) signal(SIGINT, prevsig);
9280354f26Sbostic got_intrup = 0;
9380354f26Sbostic }
9480354f26Sbostic
9580354f26Sbostic static boolean whole_screen = TRUE;
9680354f26Sbostic #define PAGMIN 12 /* minimum # of lines for page below level map */
9780354f26Sbostic
set_whole_screen()9880354f26Sbostic set_whole_screen() { /* called in termcap as soon as LI is known */
9980354f26Sbostic whole_screen = (LI-ROWNO-2 <= PAGMIN || !CD);
10080354f26Sbostic }
10180354f26Sbostic
10280354f26Sbostic #ifdef NEWS
readnews()10380354f26Sbostic readnews() {
10480354f26Sbostic register int ret;
10580354f26Sbostic
10680354f26Sbostic whole_screen = TRUE; /* force a docrt(), our first */
10780354f26Sbostic ret = page_file(NEWS, TRUE);
10880354f26Sbostic set_whole_screen();
10980354f26Sbostic return(ret); /* report whether we did docrt() */
11080354f26Sbostic }
11180354f26Sbostic #endif NEWS
11280354f26Sbostic
set_pager(mode)11380354f26Sbostic set_pager(mode)
11480354f26Sbostic register int mode; /* 0: open 1: wait+close 2: close */
11580354f26Sbostic {
11680354f26Sbostic static boolean so;
11780354f26Sbostic if(mode == 0) {
11880354f26Sbostic if(!whole_screen) {
11980354f26Sbostic /* clear topline */
12080354f26Sbostic clrlin();
12180354f26Sbostic /* use part of screen below level map */
12280354f26Sbostic curs(1, ROWNO+4);
12380354f26Sbostic } else {
12480354f26Sbostic cls();
12580354f26Sbostic }
12680354f26Sbostic so = flags.standout;
12780354f26Sbostic flags.standout = 1;
12880354f26Sbostic } else {
12980354f26Sbostic if(mode == 1) {
13080354f26Sbostic curs(1, LI);
13180354f26Sbostic more();
13280354f26Sbostic }
13380354f26Sbostic flags.standout = so;
13480354f26Sbostic if(whole_screen)
13580354f26Sbostic docrt();
13680354f26Sbostic else {
13780354f26Sbostic curs(1, ROWNO+4);
13880354f26Sbostic cl_eos();
13980354f26Sbostic }
14080354f26Sbostic }
14180354f26Sbostic }
14280354f26Sbostic
page_line(s)14380354f26Sbostic page_line(s) /* returns 1 if we should quit */
14480354f26Sbostic register char *s;
14580354f26Sbostic {
14680354f26Sbostic extern char morc;
14780354f26Sbostic
14880354f26Sbostic if(cury == LI-1) {
14980354f26Sbostic if(!*s)
15080354f26Sbostic return(0); /* suppress blank lines at top */
15180354f26Sbostic putchar('\n');
15280354f26Sbostic cury++;
15380354f26Sbostic cmore("q\033");
15480354f26Sbostic if(morc) {
15580354f26Sbostic morc = 0;
15680354f26Sbostic return(1);
15780354f26Sbostic }
15880354f26Sbostic if(whole_screen)
15980354f26Sbostic cls();
16080354f26Sbostic else {
16180354f26Sbostic curs(1, ROWNO+4);
16280354f26Sbostic cl_eos();
16380354f26Sbostic }
16480354f26Sbostic }
16580354f26Sbostic puts(s);
16680354f26Sbostic cury++;
16780354f26Sbostic return(0);
16880354f26Sbostic }
16980354f26Sbostic
17080354f26Sbostic /*
17180354f26Sbostic * Flexible pager: feed it with a number of lines and it will decide
17280354f26Sbostic * whether these should be fed to the pager above, or displayed in a
17380354f26Sbostic * corner.
17480354f26Sbostic * Call:
17580354f26Sbostic * cornline(0, title or 0) : initialize
17680354f26Sbostic * cornline(1, text) : add text to the chain of texts
17780354f26Sbostic * cornline(2, morcs) : output everything and cleanup
17880354f26Sbostic * cornline(3, 0) : cleanup
17980354f26Sbostic */
18080354f26Sbostic
cornline(mode,text)18180354f26Sbostic cornline(mode, text)
18280354f26Sbostic int mode;
18380354f26Sbostic char *text;
18480354f26Sbostic {
18580354f26Sbostic static struct line {
18680354f26Sbostic struct line *next_line;
18780354f26Sbostic char *line_text;
18880354f26Sbostic } *texthead, *texttail;
18980354f26Sbostic static int maxlen;
19080354f26Sbostic static int linect;
19180354f26Sbostic register struct line *tl;
19280354f26Sbostic
19380354f26Sbostic if(mode == 0) {
19480354f26Sbostic texthead = 0;
19580354f26Sbostic maxlen = 0;
19680354f26Sbostic linect = 0;
19780354f26Sbostic if(text) {
19880354f26Sbostic cornline(1, text); /* title */
19980354f26Sbostic cornline(1, ""); /* blank line */
20080354f26Sbostic }
20180354f26Sbostic return;
20280354f26Sbostic }
20380354f26Sbostic
20480354f26Sbostic if(mode == 1) {
20580354f26Sbostic register int len;
20680354f26Sbostic
20780354f26Sbostic if(!text) return; /* superfluous, just to be sure */
20880354f26Sbostic linect++;
20980354f26Sbostic len = strlen(text);
21080354f26Sbostic if(len > maxlen)
21180354f26Sbostic maxlen = len;
21280354f26Sbostic tl = (struct line *)
21380354f26Sbostic alloc((unsigned)(len + sizeof(struct line) + 1));
21480354f26Sbostic tl->next_line = 0;
21580354f26Sbostic tl->line_text = (char *)(tl + 1);
21680354f26Sbostic (void) strcpy(tl->line_text, text);
21780354f26Sbostic if(!texthead)
21880354f26Sbostic texthead = tl;
21980354f26Sbostic else
22080354f26Sbostic texttail->next_line = tl;
22180354f26Sbostic texttail = tl;
22280354f26Sbostic return;
22380354f26Sbostic }
22480354f26Sbostic
22580354f26Sbostic /* --- now we really do it --- */
22680354f26Sbostic if(mode == 2 && linect == 1) /* topline only */
22780354f26Sbostic pline(texthead->line_text);
22880354f26Sbostic else
22980354f26Sbostic if(mode == 2) {
23080354f26Sbostic register int curline, lth;
23180354f26Sbostic
23280354f26Sbostic if(flags.toplin == 1) more(); /* ab@unido */
23380354f26Sbostic remember_topl();
23480354f26Sbostic
23580354f26Sbostic lth = CO - maxlen - 2; /* Use full screen width */
23680354f26Sbostic if (linect < LI && lth >= 10) { /* in a corner */
23780354f26Sbostic home ();
23880354f26Sbostic cl_end ();
23980354f26Sbostic flags.toplin = 0;
24080354f26Sbostic curline = 1;
24180354f26Sbostic for (tl = texthead; tl; tl = tl->next_line) {
24280354f26Sbostic curs (lth, curline);
24380354f26Sbostic if(curline > 1)
24480354f26Sbostic cl_end ();
24580354f26Sbostic putsym(' ');
24680354f26Sbostic putstr (tl->line_text);
24780354f26Sbostic curline++;
24880354f26Sbostic }
24980354f26Sbostic curs (lth, curline);
25080354f26Sbostic cl_end ();
25180354f26Sbostic cmore (text);
25280354f26Sbostic home ();
25380354f26Sbostic cl_end ();
25480354f26Sbostic docorner (lth, curline-1);
25580354f26Sbostic } else { /* feed to pager */
25680354f26Sbostic set_pager(0);
25780354f26Sbostic for (tl = texthead; tl; tl = tl->next_line) {
25880354f26Sbostic if (page_line (tl->line_text)) {
25980354f26Sbostic set_pager(2);
26080354f26Sbostic goto cleanup;
26180354f26Sbostic }
26280354f26Sbostic }
26380354f26Sbostic if(text) {
26480354f26Sbostic cgetret(text);
26580354f26Sbostic set_pager(2);
26680354f26Sbostic } else
26780354f26Sbostic set_pager(1);
26880354f26Sbostic }
26980354f26Sbostic }
27080354f26Sbostic
27180354f26Sbostic cleanup:
27280354f26Sbostic while(tl = texthead) {
27380354f26Sbostic texthead = tl->next_line;
27480354f26Sbostic free((char *) tl);
27580354f26Sbostic }
27680354f26Sbostic }
27780354f26Sbostic
dohelp()27880354f26Sbostic dohelp()
27980354f26Sbostic {
28080354f26Sbostic char c;
28180354f26Sbostic
28280354f26Sbostic pline ("Long or short help? ");
28380354f26Sbostic while (((c = readchar ()) != 'l') && (c != 's') && !index(quitchars,c))
28480354f26Sbostic bell ();
28580354f26Sbostic if (!index(quitchars, c))
28680354f26Sbostic (void) page_file((c == 'l') ? HELP : SHELP, FALSE);
28780354f26Sbostic return(0);
28880354f26Sbostic }
28980354f26Sbostic
page_file(fnam,silent)29080354f26Sbostic page_file(fnam, silent) /* return: 0 - cannot open fnam; 1 - otherwise */
29180354f26Sbostic register char *fnam;
29280354f26Sbostic boolean silent;
29380354f26Sbostic {
29480354f26Sbostic #ifdef DEF_PAGER /* this implies that UNIX is defined */
29580354f26Sbostic {
29680354f26Sbostic /* use external pager; this may give security problems */
29780354f26Sbostic
29880354f26Sbostic register int fd = open(fnam, 0);
29980354f26Sbostic
30080354f26Sbostic if(fd < 0) {
30180354f26Sbostic if(!silent) pline("Cannot open %s.", fnam);
30280354f26Sbostic return(0);
30380354f26Sbostic }
30480354f26Sbostic if(child(1)){
30580354f26Sbostic extern char *catmore;
30680354f26Sbostic
30780354f26Sbostic /* Now that child() does a setuid(getuid()) and a chdir(),
30880354f26Sbostic we may not be able to open file fnam anymore, so make
30980354f26Sbostic it stdin. */
31080354f26Sbostic (void) close(0);
31180354f26Sbostic if(dup(fd)) {
31280354f26Sbostic if(!silent) printf("Cannot open %s as stdin.\n", fnam);
31380354f26Sbostic } else {
31480354f26Sbostic execl(catmore, "page", (char *) 0);
31580354f26Sbostic if(!silent) printf("Cannot exec %s.\n", catmore);
31680354f26Sbostic }
31780354f26Sbostic exit(1);
31880354f26Sbostic }
31980354f26Sbostic (void) close(fd);
32080354f26Sbostic }
32180354f26Sbostic #else DEF_PAGER
32280354f26Sbostic {
32380354f26Sbostic FILE *f; /* free after Robert Viduya */
32480354f26Sbostic
32580354f26Sbostic if ((f = fopen (fnam, "r")) == (FILE *) 0) {
32680354f26Sbostic if(!silent) {
32780354f26Sbostic home(); perror (fnam); flags.toplin = 1;
32880354f26Sbostic pline ("Cannot open %s.", fnam);
32980354f26Sbostic }
33080354f26Sbostic return(0);
33180354f26Sbostic }
33280354f26Sbostic page_more(f, 0);
33380354f26Sbostic }
33480354f26Sbostic #endif DEF_PAGER
33580354f26Sbostic
33680354f26Sbostic return(1);
33780354f26Sbostic }
33880354f26Sbostic
33980354f26Sbostic #ifdef UNIX
34080354f26Sbostic #ifdef SHELL
dosh()34180354f26Sbostic dosh(){
34280354f26Sbostic register char *str;
34380354f26Sbostic if(child(0)) {
34480354f26Sbostic if(str = getenv("SHELL"))
34580354f26Sbostic execl(str, str, (char *) 0);
34680354f26Sbostic else
34780354f26Sbostic execl("/bin/sh", "sh", (char *) 0);
34880354f26Sbostic pline("sh: cannot execute.");
34980354f26Sbostic exit(1);
35080354f26Sbostic }
35180354f26Sbostic return(0);
35280354f26Sbostic }
35380354f26Sbostic #endif SHELL
35480354f26Sbostic
35580354f26Sbostic #ifdef NOWAITINCLUDE
35680354f26Sbostic union wait { /* used only for the cast (union wait *) 0 */
35780354f26Sbostic int w_status;
35880354f26Sbostic struct {
35980354f26Sbostic unsigned short w_Termsig:7;
36080354f26Sbostic unsigned short w_Coredump:1;
36180354f26Sbostic unsigned short w_Retcode:8;
36280354f26Sbostic } w_T;
36380354f26Sbostic };
36480354f26Sbostic
36580354f26Sbostic #else
36680354f26Sbostic
36780354f26Sbostic #ifdef BSD
36880354f26Sbostic #include <sys/wait.h>
36980354f26Sbostic #else
37080354f26Sbostic #include <wait.h>
37180354f26Sbostic #endif BSD
37280354f26Sbostic #endif NOWAITINCLUDE
37380354f26Sbostic
child(wt)37480354f26Sbostic child(wt) {
375*a8c302d4Sbostic int status;
376*a8c302d4Sbostic register int f;
377*a8c302d4Sbostic
378*a8c302d4Sbostic f = fork();
37980354f26Sbostic if(f == 0){ /* child */
38080354f26Sbostic settty((char *) 0); /* also calls end_screen() */
38180354f26Sbostic (void) setuid(getuid());
38280354f26Sbostic (void) setgid(getgid());
38380354f26Sbostic #ifdef CHDIR
38480354f26Sbostic (void) chdir(getenv("HOME"));
38580354f26Sbostic #endif CHDIR
38680354f26Sbostic return(1);
38780354f26Sbostic }
38880354f26Sbostic if(f == -1) { /* cannot fork */
38980354f26Sbostic pline("Fork failed. Try again.");
39080354f26Sbostic return(0);
39180354f26Sbostic }
39280354f26Sbostic /* fork succeeded; wait for child to exit */
39380354f26Sbostic (void) signal(SIGINT,SIG_IGN);
39480354f26Sbostic (void) signal(SIGQUIT,SIG_IGN);
395*a8c302d4Sbostic (void) wait(&status);
39680354f26Sbostic gettty();
39780354f26Sbostic setftty();
39880354f26Sbostic (void) signal(SIGINT,done1);
39980354f26Sbostic #ifdef WIZARD
40080354f26Sbostic if(wizard) (void) signal(SIGQUIT,SIG_DFL);
40180354f26Sbostic #endif WIZARD
40280354f26Sbostic if(wt) getret();
40380354f26Sbostic docrt();
40480354f26Sbostic return(0);
40580354f26Sbostic }
40680354f26Sbostic #endif UNIX
407