1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.pager.c - version 1.0.3 */
3 /* $FreeBSD: src/games/hack/hack.pager.c,v 1.7 1999/11/16 02:57:09 billf Exp $ */
4 /* $DragonFly: src/games/hack/hack.pager.c,v 1.4 2006/08/21 19:45:32 pavalos Exp $ */
6 /* This file contains the command routine dowhatis() and a pager. */
7 /* Also readmail() and doshell(), and generally the things that
8 contact the outside world. */
10 #include <sys/types.h>
11 #include <sys/signal.h>
13 extern int CO
, LI
; /* usually COLNO and ROWNO+2 */
15 extern char quitchars
[];
17 static void intruph(int);
18 static void page_more(FILE *, int);
25 char *buf
= &bufr
[6], *ep
, q
;
27 if(!(fp
= fopen(DATAFILE
, "r")))
28 pline("Cannot open data file!");
30 pline("Specify what? ");
33 while(fgets(buf
,BUFSZ
,fp
))
35 ep
= index(buf
, '\n');
37 /* else: bad data file */
38 /* Expand tab 'by hand' */
42 strncpy(buf
+1, " ", 7);
47 if(readchar() == 'y') {
48 page_more(fp
,1); /* does fclose() */
52 fclose(fp
); /* kopper@psuvax1 */
55 pline("I've never heard of such things.");
61 /* make the paging of a file interruptible */
62 static int got_intrup
;
65 intruph(__unused
int unused
)
70 /* simple pager, also used from dohelp() */
71 /* strip = nr of chars to be stripped from each line (0 or 1) */
73 page_more(FILE *fp
, int strip
)
76 sig_t prevsig
= signal(SIGINT
, intruph
);
79 bufr
= (char *) alloc((unsigned) CO
);
81 while(fgets(bufr
,CO
-1,fp
) && (!strip
|| *bufr
== '\t') && !got_intrup
){
82 ep
= index(bufr
, '\n');
85 if(page_line(bufr
+strip
)) {
94 signal(SIGINT
, prevsig
);
98 static boolean whole_screen
= TRUE
;
99 #define PAGMIN 12 /* minimum # of lines for page below level map */
102 set_whole_screen(void) /* called in termcap as soon as LI is known */
104 whole_screen
= (LI
-ROWNO
-2 <= PAGMIN
|| !CD
);
113 whole_screen
= TRUE
; /* force a docrt(), our first */
114 ret
= page_file(NEWS
, TRUE
);
116 return(ret
); /* report whether we did docrt() */
121 set_pager(int mode
) /* 0: open 1: wait+close 2: close */
128 /* use part of screen below level map */
151 page_line(const char *s
) /* returns 1 if we should quit */
155 return(0); /* suppress blank lines at top */
176 * Flexible pager: feed it with a number of lines and it will decide
177 * whether these should be fed to the pager above, or displayed in a
180 * cornline(0, title or 0) : initialize
181 * cornline(1, text) : add text to the chain of texts
182 * cornline(2, morcs) : output everything and cleanup
183 * cornline(3, 0) : cleanup
187 cornline(int mode
, const char *text
)
190 struct line
*next_line
;
192 } *texthead
, *texttail
;
202 cornline(1, text
); /* title */
203 cornline(1, ""); /* blank line */
211 if(!text
) return; /* superfluous, just to be sure */
217 alloc((unsigned)(len
+ sizeof(struct line
) + 1));
219 tl
->line_text
= (char *)(tl
+ 1);
220 strcpy(tl
->line_text
, text
);
224 texttail
->next_line
= tl
;
229 /* --- now we really do it --- */
230 if(mode
== 2 && linect
== 1) /* topline only */
231 pline(texthead
->line_text
);
236 if(flags
.toplin
== 1) more(); /* ab@unido */
239 lth
= CO
- maxlen
- 2; /* Use full screen width */
240 if (linect
< LI
&& lth
>= 10) { /* in a corner */
245 for (tl
= texthead
; tl
; tl
= tl
->next_line
) {
250 putstr (tl
->line_text
);
258 docorner (lth
, curline
-1);
259 } else { /* feed to pager */
261 for (tl
= texthead
; tl
; tl
= tl
->next_line
) {
262 if (page_line (tl
->line_text
)) {
276 while((tl
= texthead
)) {
277 texthead
= tl
->next_line
;
287 pline ("Long or short help? ");
288 while (((c
= readchar ()) != 'l') && (c
!= 's') && !index(quitchars
,c
))
290 if (!index(quitchars
, c
))
291 page_file((c
== 'l') ? HELP
: SHELP
, FALSE
);
295 /* return: 0 - cannot open fnam; 1 - otherwise */
297 page_file(const char *fnam
, bool silent
)
299 #ifdef DEF_PAGER /* this implies that UNIX is defined */
301 /* use external pager; this may give security problems */
303 int fd
= open(fnam
, 0);
306 if(!silent
) pline("Cannot open %s.", fnam
);
310 extern char *catmore
;
312 /* Now that child() does a setuid(getuid()) and a chdir(),
313 we may not be able to open file fnam anymore, so make
317 if(!silent
) printf("Cannot open %s as stdin.\n", fnam
);
319 execl(catmore
, "page", (char *) 0);
320 if(!silent
) printf("Cannot exec %s.\n", catmore
);
326 #else /* DEF_PAGER */
328 FILE *f
; /* free after Robert Viduya */
330 if ((f
= fopen (fnam
, "r")) == (FILE *) 0) {
332 home(); perror (fnam
); flags
.toplin
= 1;
333 pline ("Cannot open %s.", fnam
);
339 #endif /* DEF_PAGER */
351 if((str
= getenv("SHELL")))
352 execl(str
, str
, (char *) 0);
354 execl("/bin/sh", "sh", (char *) 0);
355 pline("sh: cannot execute.");
363 union wait
{ /* used only for the cast (union wait *) 0 */
366 unsigned short w_Termsig
:7;
367 unsigned short w_Coredump
:1;
368 unsigned short w_Retcode
:8;
375 #include <sys/wait.h>
379 #endif /* NOWAITINCLUDE */
388 if(f
== 0){ /* child */
389 settty((char *) 0); /* also calls end_screen() */
393 chdir(getenv("HOME"));
397 if(f
== -1) { /* cannot fork */
398 pline("Fork failed. Try again.");
401 /* fork succeeded; wait for child to exit */
402 signal(SIGINT
,SIG_IGN
);
403 signal(SIGQUIT
,SIG_IGN
);
407 signal(SIGINT
,done1
);
409 if(wizard
) signal(SIGQUIT
,SIG_DFL
);