Home | History | Annotate | Line # | Download | only in hack
hack.pager.c revision 1.8
      1 /*	$NetBSD: hack.pager.c,v 1.8 2006/03/29 01:21:07 jnemeth Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1985, Stichting Centrum voor Wiskunde en Informatica,
      5  * Amsterdam
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions are
     10  * met:
     11  *
     12  * - Redistributions of source code must retain the above copyright notice,
     13  * this list of conditions and the following disclaimer.
     14  *
     15  * - Redistributions in binary form must reproduce the above copyright
     16  * notice, this list of conditions and the following disclaimer in the
     17  * documentation and/or other materials provided with the distribution.
     18  *
     19  * - Neither the name of the Stichting Centrum voor Wiskunde en
     20  * Informatica, nor the names of its contributors may be used to endorse or
     21  * promote products derived from this software without specific prior
     22  * written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
     25  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     26  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
     27  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
     28  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     30  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     31  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     32  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     33  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     34  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     35  */
     36 
     37 /*
     38  * Copyright (c) 1982 Jay Fenlason <hack (at) gnu.org>
     39  * All rights reserved.
     40  *
     41  * Redistribution and use in source and binary forms, with or without
     42  * modification, are permitted provided that the following conditions
     43  * are met:
     44  * 1. Redistributions of source code must retain the above copyright
     45  *    notice, this list of conditions and the following disclaimer.
     46  * 2. Redistributions in binary form must reproduce the above copyright
     47  *    notice, this list of conditions and the following disclaimer in the
     48  *    documentation and/or other materials provided with the distribution.
     49  * 3. The name of the author may not be used to endorse or promote products
     50  *    derived from this software without specific prior written permission.
     51  *
     52  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     53  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
     54  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
     55  * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     56  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     57  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     58  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     59  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     60  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     61  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     62  */
     63 
     64 #include <sys/cdefs.h>
     65 #ifndef lint
     66 __RCSID("$NetBSD: hack.pager.c,v 1.8 2006/03/29 01:21:07 jnemeth Exp $");
     67 #endif				/* not lint */
     68 
     69 /* This file contains the command routine dowhatis() and a pager. */
     70 /*
     71  * Also readmail() and doshell(), and generally the things that contact the
     72  * outside world.
     73  */
     74 
     75 #include <sys/types.h>
     76 #include <signal.h>
     77 #include <stdlib.h>
     78 #include <unistd.h>
     79 #include "hack.h"
     80 #include "extern.h"
     81 
     82 int
     83 dowhatis()
     84 {
     85 	FILE           *fp;
     86 	char            bufr[BUFSZ + 6];
     87 	char           *buf = &bufr[6], *ep, q;
     88 
     89 	if (!(fp = fopen(DATAFILE, "r")))
     90 		pline("Cannot open data file!");
     91 	else {
     92 		pline("Specify what? ");
     93 		q = readchar();
     94 		if (q != '\t')
     95 			while (fgets(buf, BUFSZ, fp))
     96 				if (*buf == q) {
     97 					ep = strchr(buf, '\n');
     98 					if (ep)
     99 						*ep = 0;
    100 					/* else: bad data file */
    101 					else {
    102 						pline("Bad data file!");
    103 						return(0);
    104 					}
    105 					/* Expand tab 'by hand' */
    106 					if (buf[1] == '\t') {
    107 						buf = bufr;
    108 						buf[0] = q;
    109 						(void) strncpy(buf + 1, "       ", 7);
    110 					}
    111 					pline(buf);
    112 					if (ep[-1] == ';') {
    113 						pline("More info? ");
    114 						if (readchar() == 'y') {
    115 							page_more(fp, 1);	/* does fclose() */
    116 							return (0);
    117 						}
    118 					}
    119 					(void) fclose(fp);	/* kopper@psuvax1 */
    120 					return (0);
    121 				}
    122 		pline("I've never heard of such things.");
    123 		(void) fclose(fp);
    124 	}
    125 	return (0);
    126 }
    127 
    128 /* make the paging of a file interruptible */
    129 static int      got_intrup;
    130 
    131 void
    132 intruph(n)
    133 	int n __attribute__((__unused__));
    134 {
    135 	got_intrup++;
    136 }
    137 
    138 /* simple pager, also used from dohelp() */
    139 void
    140 page_more(fp, strip)
    141 	FILE           *fp;
    142 	int             strip;	/* nr of chars to be stripped from each line
    143 				 * (0 or 1) */
    144 {
    145 	char           *bufr, *ep;
    146 	sig_t           prevsig = signal(SIGINT, intruph);
    147 
    148 	set_pager(0);
    149 	bufr = (char *) alloc((unsigned) CO);
    150 	bufr[CO - 1] = 0;
    151 	while (fgets(bufr, CO - 1, fp) && (!strip || *bufr == '\t') && !got_intrup) {
    152 		ep = strchr(bufr, '\n');
    153 		if (ep)
    154 			*ep = 0;
    155 		if (page_line(bufr + strip)) {
    156 			set_pager(2);
    157 			goto ret;
    158 		}
    159 	}
    160 	set_pager(1);
    161 ret:
    162 	free(bufr);
    163 	(void) fclose(fp);
    164 	(void) signal(SIGINT, prevsig);
    165 	got_intrup = 0;
    166 }
    167 
    168 static boolean  whole_screen = TRUE;
    169 #define	PAGMIN	12		/* minimum # of lines for page below level
    170 				 * map */
    171 
    172 void
    173 set_whole_screen()
    174 {				/* called in termcap as soon as LI is known */
    175 	whole_screen = (LI - ROWNO - 2 <= PAGMIN || !CD);
    176 }
    177 
    178 #ifdef NEWS
    179 int
    180 readnews()
    181 {
    182 	int             ret;
    183 
    184 	whole_screen = TRUE;	/* force a docrt(), our first */
    185 	ret = page_file(NEWS, TRUE);
    186 	set_whole_screen();
    187 	return (ret);		/* report whether we did docrt() */
    188 }
    189 #endif	/* NEWS */
    190 
    191 void
    192 set_pager(mode)
    193 	int             mode;	/* 0: open  1: wait+close  2: close */
    194 {
    195 	static boolean  so;
    196 	if (mode == 0) {
    197 		if (!whole_screen) {
    198 			/* clear topline */
    199 			clrlin();
    200 			/* use part of screen below level map */
    201 			curs(1, ROWNO + 4);
    202 		} else {
    203 			cls();
    204 		}
    205 		so = flags.standout;
    206 		flags.standout = 1;
    207 	} else {
    208 		if (mode == 1) {
    209 			curs(1, LI);
    210 			more();
    211 		}
    212 		flags.standout = so;
    213 		if (whole_screen)
    214 			docrt();
    215 		else {
    216 			curs(1, ROWNO + 4);
    217 			cl_eos();
    218 		}
    219 	}
    220 }
    221 
    222 int
    223 page_line(s)			/* returns 1 if we should quit */
    224 	const char           *s;
    225 {
    226 	if (cury == LI - 1) {
    227 		if (!*s)
    228 			return (0);	/* suppress blank lines at top */
    229 		putchar('\n');
    230 		cury++;
    231 		cmore("q\033");
    232 		if (morc) {
    233 			morc = 0;
    234 			return (1);
    235 		}
    236 		if (whole_screen)
    237 			cls();
    238 		else {
    239 			curs(1, ROWNO + 4);
    240 			cl_eos();
    241 		}
    242 	}
    243 	puts(s);
    244 	cury++;
    245 	return (0);
    246 }
    247 
    248 /*
    249  * Flexible pager: feed it with a number of lines and it will decide
    250  * whether these should be fed to the pager above, or displayed in a
    251  * corner.
    252  * Call:
    253  *	cornline(0, title or 0)	: initialize
    254  *	cornline(1, text)	: add text to the chain of texts
    255  *	cornline(2, morcs)	: output everything and cleanup
    256  *	cornline(3, 0)		: cleanup
    257  */
    258 
    259 void
    260 cornline(mode, text)
    261 	int             mode;
    262 	const char           *text;
    263 {
    264 	static struct line {
    265 		struct line    *next_line;
    266 		char           *line_text;
    267 	}              *texthead, *texttail;
    268 	static int      maxlen;
    269 	static int      linect;
    270 	struct line    *tl;
    271 
    272 	if (mode == 0) {
    273 		texthead = 0;
    274 		maxlen = 0;
    275 		linect = 0;
    276 		if (text) {
    277 			cornline(1, text);	/* title */
    278 			cornline(1, "");	/* blank line */
    279 		}
    280 		return;
    281 	}
    282 	if (mode == 1) {
    283 		int             len;
    284 
    285 		if (!text)
    286 			return;	/* superfluous, just to be sure */
    287 		linect++;
    288 		len = strlen(text);
    289 		if (len > maxlen)
    290 			maxlen = len;
    291 		tl = (struct line *)
    292 			alloc((unsigned) (len + sizeof(struct line) + 1));
    293 		tl->next_line = 0;
    294 		tl->line_text = (char *) (tl + 1);
    295 		(void) strcpy(tl->line_text, text);
    296 		if (!texthead)
    297 			texthead = tl;
    298 		else
    299 			texttail->next_line = tl;
    300 		texttail = tl;
    301 		return;
    302 	}
    303 	/* --- now we really do it --- */
    304 	if (mode == 2 && linect == 1)	/* topline only */
    305 		pline(texthead->line_text);
    306 	else if (mode == 2) {
    307 		int             curline, lth;
    308 
    309 		if (flags.toplin == 1)
    310 			more();	/* ab@unido */
    311 		remember_topl();
    312 
    313 		lth = CO - maxlen - 2;	/* Use full screen width */
    314 		if (linect < LI && lth >= 10) {	/* in a corner */
    315 			home();
    316 			cl_end();
    317 			flags.toplin = 0;
    318 			curline = 1;
    319 			for (tl = texthead; tl; tl = tl->next_line) {
    320 				curs(lth, curline);
    321 				if (curline > 1)
    322 					cl_end();
    323 				putsym(' ');
    324 				putstr(tl->line_text);
    325 				curline++;
    326 			}
    327 			curs(lth, curline);
    328 			cl_end();
    329 			cmore(text);
    330 			home();
    331 			cl_end();
    332 			docorner(lth, curline - 1);
    333 		} else {	/* feed to pager */
    334 			set_pager(0);
    335 			for (tl = texthead; tl; tl = tl->next_line) {
    336 				if (page_line(tl->line_text)) {
    337 					set_pager(2);
    338 					goto cleanup;
    339 				}
    340 			}
    341 			if (text) {
    342 				cgetret(text);
    343 				set_pager(2);
    344 			} else
    345 				set_pager(1);
    346 		}
    347 	}
    348 cleanup:
    349 	while ((tl = texthead) != NULL) {
    350 		texthead = tl->next_line;
    351 		free((char *) tl);
    352 	}
    353 }
    354 
    355 int
    356 dohelp()
    357 {
    358 	char            c;
    359 
    360 	pline("Long or short help? ");
    361 	while (((c = readchar()) != 'l') && (c != 's') && !strchr(quitchars, c))
    362 		bell();
    363 	if (!strchr(quitchars, c))
    364 		(void) page_file((c == 'l') ? HELP : SHELP, FALSE);
    365 	return (0);
    366 }
    367 
    368 int
    369 page_file(fnam, silent)		/* return: 0 - cannot open fnam; 1 -
    370 				 * otherwise */
    371 	const char           *fnam;
    372 	boolean         silent;
    373 {
    374 #ifdef DEF_PAGER		/* this implies that UNIX is defined */
    375 	{
    376 		/* use external pager; this may give security problems */
    377 
    378 		int             fd = open(fnam, O_RDONLY);
    379 
    380 		if (fd < 0) {
    381 			if (!silent)
    382 				pline("Cannot open %s.", fnam);
    383 			return (0);
    384 		}
    385 		if (child(1)) {
    386 
    387 			/*
    388 			 * Now that child() does a setuid(getuid()) and a
    389 			 * chdir(), we may not be able to open file fnam
    390 			 * anymore, so make it stdin.
    391 			 */
    392 			(void) close(0);
    393 			if (dup(fd)) {
    394 				if (!silent)
    395 					printf("Cannot open %s as stdin.\n", fnam);
    396 			} else {
    397 				execl(catmore, "page", (char *) 0);
    398 				if (!silent)
    399 					printf("Cannot exec %s.\n", catmore);
    400 			}
    401 			exit(1);
    402 		}
    403 		(void) close(fd);
    404 	}
    405 #else	/* DEF_PAGER */
    406 	{
    407 		FILE           *f;	/* free after Robert Viduya */
    408 
    409 		if ((f = fopen(fnam, "r")) == (FILE *) 0) {
    410 			if (!silent) {
    411 				home();
    412 				perror(fnam);
    413 				flags.toplin = 1;
    414 				pline("Cannot open %s.", fnam);
    415 			}
    416 			return (0);
    417 		}
    418 		page_more(f, 0);
    419 	}
    420 #endif	/* DEF_PAGER */
    421 
    422 	return (1);
    423 }
    424 
    425 #ifdef UNIX
    426 #ifdef SHELL
    427 int
    428 dosh()
    429 {
    430 	char           *str;
    431 	if (child(0)) {
    432 		if ((str = getenv("SHELL")) != NULL)
    433 			execl(str, str, (char *) 0);
    434 		else
    435 			execl("/bin/sh", "sh", (char *) 0);
    436 		pline("sh: cannot execute.");
    437 		exit(1);
    438 	}
    439 	return (0);
    440 }
    441 #endif	/* SHELL */
    442 
    443 #ifdef NOWAITINCLUDE
    444 union wait {			/* used only for the cast  (union wait *) 0  */
    445 	int             w_status;
    446 	struct {
    447 		unsigned short  w_Termsig:7;
    448 		unsigned short  w_Coredump:1;
    449 		unsigned short  w_Retcode:8;
    450 	}               w_T;
    451 };
    452 
    453 #else
    454 
    455 #ifdef BSD
    456 #include	<sys/wait.h>
    457 #else
    458 #include	<wait.h>
    459 #endif	/* BSD */
    460 #endif	/* NOWAITINCLUDE */
    461 
    462 int
    463 child(int wt)
    464 {
    465 	int             status;
    466 	int             f;
    467 
    468 	f = fork();
    469 	if (f == 0) {		/* child */
    470 		settty((char *) 0);	/* also calls end_screen() */
    471 		(void) setuid(getuid());
    472 		(void) setgid(getgid());
    473 #ifdef CHDIR
    474 		(void) chdir(getenv("HOME"));
    475 #endif	/* CHDIR */
    476 		return (1);
    477 	}
    478 	if (f == -1) {		/* cannot fork */
    479 		pline("Fork failed. Try again.");
    480 		return (0);
    481 	}
    482 	/* fork succeeded; wait for child to exit */
    483 	(void) signal(SIGINT, SIG_IGN);
    484 	(void) signal(SIGQUIT, SIG_IGN);
    485 	(void) wait(&status);
    486 	gettty();
    487 	setftty();
    488 	(void) signal(SIGINT, done1);
    489 #ifdef WIZARD
    490 	if (wizard)
    491 		(void) signal(SIGQUIT, SIG_DFL);
    492 #endif	/* WIZARD */
    493 	if (wt)
    494 		getret();
    495 	docrt();
    496 	return (0);
    497 }
    498 #endif	/* UNIX */
    499