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.3 2004/11/06 12:29:17 eirikn 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>
16 extern int CO
, LI
; /* usually COLNO and ROWNO+2 */
18 extern char quitchars
[];
25 char *buf
= &bufr
[6], *ep
, q
;
26 extern char readchar();
28 if(!(fp
= fopen(DATAFILE
, "r")))
29 pline("Cannot open data file!");
31 pline("Specify what? ");
34 while(fgets(buf
,BUFSZ
,fp
))
36 ep
= index(buf
, '\n');
38 /* else: bad data file */
39 /* Expand tab 'by hand' */
43 (void) strncpy(buf
+1, " ", 7);
48 if(readchar() == 'y') {
49 page_more(fp
,1); /* does fclose() */
53 (void) fclose(fp
); /* kopper@psuvax1 */
56 pline("I've never heard of such things.");
62 /* make the paging of a file interruptible */
63 static int got_intrup
;
70 /* simple pager, also used from dohelp() */
73 int strip
; /* nr of chars to be stripped from each line (0 or 1) */
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 (void) signal(SIGINT
, prevsig
);
98 static boolean whole_screen
= TRUE
;
99 #define PAGMIN 12 /* minimum # of lines for page below level map */
101 set_whole_screen() { /* called in termcap as soon as LI is known */
102 whole_screen
= (LI
-ROWNO
-2 <= PAGMIN
|| !CD
);
109 whole_screen
= TRUE
; /* force a docrt(), our first */
110 ret
= page_file(NEWS
, TRUE
);
112 return(ret
); /* report whether we did docrt() */
117 int mode
; /* 0: open 1: wait+close 2: close */
124 /* use part of screen below level map */
146 page_line(s
) /* returns 1 if we should quit */
153 return(0); /* suppress blank lines at top */
174 * Flexible pager: feed it with a number of lines and it will decide
175 * whether these should be fed to the pager above, or displayed in a
178 * cornline(0, title or 0) : initialize
179 * cornline(1, text) : add text to the chain of texts
180 * cornline(2, morcs) : output everything and cleanup
181 * cornline(3, 0) : cleanup
189 struct line
*next_line
;
191 } *texthead
, *texttail
;
201 cornline(1, text
); /* title */
202 cornline(1, ""); /* blank line */
210 if(!text
) return; /* superfluous, just to be sure */
216 alloc((unsigned)(len
+ sizeof(struct line
) + 1));
218 tl
->line_text
= (char *)(tl
+ 1);
219 (void) strcpy(tl
->line_text
, text
);
223 texttail
->next_line
= tl
;
228 /* --- now we really do it --- */
229 if(mode
== 2 && linect
== 1) /* topline only */
230 pline(texthead
->line_text
);
235 if(flags
.toplin
== 1) more(); /* ab@unido */
238 lth
= CO
- maxlen
- 2; /* Use full screen width */
239 if (linect
< LI
&& lth
>= 10) { /* in a corner */
244 for (tl
= texthead
; tl
; tl
= tl
->next_line
) {
249 putstr (tl
->line_text
);
257 docorner (lth
, curline
-1);
258 } else { /* feed to pager */
260 for (tl
= texthead
; tl
; tl
= tl
->next_line
) {
261 if (page_line (tl
->line_text
)) {
275 while(tl
= texthead
) {
276 texthead
= tl
->next_line
;
285 pline ("Long or short help? ");
286 while (((c
= readchar ()) != 'l') && (c
!= 's') && !index(quitchars
,c
))
288 if (!index(quitchars
, c
))
289 (void) page_file((c
== 'l') ? HELP
: SHELP
, FALSE
);
293 page_file(fnam
, silent
) /* return: 0 - cannot open fnam; 1 - otherwise */
297 #ifdef DEF_PAGER /* this implies that UNIX is defined */
299 /* use external pager; this may give security problems */
301 int fd
= open(fnam
, 0);
304 if(!silent
) pline("Cannot open %s.", fnam
);
308 extern char *catmore
;
310 /* Now that child() does a setuid(getuid()) and a chdir(),
311 we may not be able to open file fnam anymore, so make
315 if(!silent
) printf("Cannot open %s as stdin.\n", fnam
);
317 execl(catmore
, "page", (char *) 0);
318 if(!silent
) printf("Cannot exec %s.\n", catmore
);
324 #else /* DEF_PAGER */
326 FILE *f
; /* free after Robert Viduya */
328 if ((f
= fopen (fnam
, "r")) == (FILE *) 0) {
330 home(); perror (fnam
); flags
.toplin
= 1;
331 pline ("Cannot open %s.", fnam
);
337 #endif /* DEF_PAGER */
347 if(str
= getenv("SHELL"))
348 execl(str
, str
, (char *) 0);
350 execl("/bin/sh", "sh", (char *) 0);
351 pline("sh: cannot execute.");
359 union wait
{ /* used only for the cast (union wait *) 0 */
362 unsigned short w_Termsig
:7;
363 unsigned short w_Coredump
:1;
364 unsigned short w_Retcode
:8;
371 #include <sys/wait.h>
375 #endif /* NOWAITINCLUDE */
382 if(f
== 0){ /* child */
383 settty((char *) 0); /* also calls end_screen() */
387 (void) chdir(getenv("HOME"));
391 if(f
== -1) { /* cannot fork */
392 pline("Fork failed. Try again.");
395 /* fork succeeded; wait for child to exit */
396 (void) signal(SIGINT
,SIG_IGN
);
397 (void) signal(SIGQUIT
,SIG_IGN
);
398 (void) wait(&status
);
401 (void) signal(SIGINT
,done1
);
403 if(wizard
) (void) signal(SIGQUIT
,SIG_DFL
);