]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - hack/hack.pager.c
1 /* $NetBSD: hack.pager.c,v 1.6 2001/03/25 20:44:02 jsm Exp $ */
4 * Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
9 __RCSID("$NetBSD: hack.pager.c,v 1.6 2001/03/25 20:44:02 jsm Exp $");
12 /* This file contains the command routine dowhatis() and a pager. */
14 * Also readmail() and doshell(), and generally the things that contact the
18 #include <sys/types.h>
30 char *buf
= &bufr
[6], *ep
, q
;
32 if (!(fp
= fopen(DATAFILE
, "r")))
33 pline("Cannot open data file!");
35 pline("Specify what? ");
38 while (fgets(buf
, BUFSZ
, fp
))
40 ep
= strchr(buf
, '\n');
43 /* else: bad data file */
44 /* Expand tab 'by hand' */
48 (void) strncpy(buf
+ 1, " ", 7);
53 if (readchar() == 'y') {
54 page_more(fp
, 1); /* does fclose() */
58 (void) fclose(fp
); /* kopper@psuvax1 */
61 pline("I've never heard of such things.");
67 /* make the paging of a file interruptible */
68 static int got_intrup
;
72 int n
__attribute__((__unused__
));
77 /* simple pager, also used from dohelp() */
81 int strip
; /* nr of chars to be stripped from each line
85 sig_t prevsig
= signal(SIGINT
, intruph
);
88 bufr
= (char *) alloc((unsigned) CO
);
90 while (fgets(bufr
, CO
- 1, fp
) && (!strip
|| *bufr
== '\t') && !got_intrup
) {
91 ep
= strchr(bufr
, '\n');
94 if (page_line(bufr
+ strip
)) {
103 (void) signal(SIGINT
, prevsig
);
107 static boolean whole_screen
= TRUE
;
108 #define PAGMIN 12 /* minimum # of lines for page below level
113 { /* called in termcap as soon as LI is known */
114 whole_screen
= (LI
- ROWNO
- 2 <= PAGMIN
|| !CD
);
123 whole_screen
= TRUE
; /* force a docrt(), our first */
124 ret
= page_file(NEWS
, TRUE
);
126 return (ret
); /* report whether we did docrt() */
132 int mode
; /* 0: open 1: wait+close 2: close */
139 /* use part of screen below level map */
162 page_line(s
) /* returns 1 if we should quit */
165 if (cury
== LI
- 1) {
167 return (0); /* suppress blank lines at top */
188 * Flexible pager: feed it with a number of lines and it will decide
189 * whether these should be fed to the pager above, or displayed in a
192 * cornline(0, title or 0) : initialize
193 * cornline(1, text) : add text to the chain of texts
194 * cornline(2, morcs) : output everything and cleanup
195 * cornline(3, 0) : cleanup
204 struct line
*next_line
;
206 } *texthead
, *texttail
;
216 cornline(1, text
); /* title */
217 cornline(1, ""); /* blank line */
225 return; /* superfluous, just to be sure */
231 alloc((unsigned) (len
+ sizeof(struct line
) + 1));
233 tl
->line_text
= (char *) (tl
+ 1);
234 (void) strcpy(tl
->line_text
, text
);
238 texttail
->next_line
= tl
;
242 /* --- now we really do it --- */
243 if (mode
== 2 && linect
== 1) /* topline only */
244 pline(texthead
->line_text
);
245 else if (mode
== 2) {
248 if (flags
.toplin
== 1)
249 more(); /* ab@unido */
252 lth
= CO
- maxlen
- 2; /* Use full screen width */
253 if (linect
< LI
&& lth
>= 10) { /* in a corner */
258 for (tl
= texthead
; tl
; tl
= tl
->next_line
) {
263 putstr(tl
->line_text
);
271 docorner(lth
, curline
- 1);
272 } else { /* feed to pager */
274 for (tl
= texthead
; tl
; tl
= tl
->next_line
) {
275 if (page_line(tl
->line_text
)) {
288 while ((tl
= texthead
) != NULL
) {
289 texthead
= tl
->next_line
;
299 pline("Long or short help? ");
300 while (((c
= readchar()) != 'l') && (c
!= 's') && !strchr(quitchars
, c
))
302 if (!strchr(quitchars
, c
))
303 (void) page_file((c
== 'l') ? HELP
: SHELP
, FALSE
);
308 page_file(fnam
, silent
) /* return: 0 - cannot open fnam; 1 -
313 #ifdef DEF_PAGER /* this implies that UNIX is defined */
315 /* use external pager; this may give security problems */
317 int fd
= open(fnam
, O_RDONLY
);
321 pline("Cannot open %s.", fnam
);
327 * Now that child() does a setuid(getuid()) and a
328 * chdir(), we may not be able to open file fnam
329 * anymore, so make it stdin.
334 printf("Cannot open %s as stdin.\n", fnam
);
336 execl(catmore
, "page", (char *) 0);
338 printf("Cannot exec %s.\n", catmore
);
344 #else /* DEF_PAGER */
346 FILE *f
; /* free after Robert Viduya */
348 if ((f
= fopen(fnam
, "r")) == (FILE *) 0) {
353 pline("Cannot open %s.", fnam
);
359 #endif /* DEF_PAGER */
371 if ((str
= getenv("SHELL")) != NULL
)
372 execl(str
, str
, (char *) 0);
374 execl("/bin/sh", "sh", (char *) 0);
375 pline("sh: cannot execute.");
383 union wait
{ /* used only for the cast (union wait *) 0 */
386 unsigned short w_Termsig
:7;
387 unsigned short w_Coredump
:1;
388 unsigned short w_Retcode
:8;
395 #include <sys/wait.h>
399 #endif /* NOWAITINCLUDE */
408 if (f
== 0) { /* child */
409 settty((char *) 0); /* also calls end_screen() */
410 (void) setuid(getuid());
411 (void) setgid(getgid());
413 (void) chdir(getenv("HOME"));
417 if (f
== -1) { /* cannot fork */
418 pline("Fork failed. Try again.");
421 /* fork succeeded; wait for child to exit */
422 (void) signal(SIGINT
, SIG_IGN
);
423 (void) signal(SIGQUIT
, SIG_IGN
);
424 (void) wait(&status
);
427 (void) signal(SIGINT
, done1
);
430 (void) signal(SIGQUIT
, SIG_DFL
);