]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - hack/hack.pager.c
2 * Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985.
6 static char rcsid
[] = "$NetBSD: hack.pager.c,v 1.4 1995/03/23 08:31:16 cgd Exp $";
9 /* This file contains the command routine dowhatis() and a pager. */
10 /* Also readmail() and doshell(), and generally the things that
11 contact the outside world. */
13 #include <sys/types.h>
19 extern int CO
, LI
; /* usually COLNO and ROWNO+2 */
21 extern char quitchars
[];
28 register char *buf
= &bufr
[6], *ep
, q
;
29 extern char readchar();
31 if(!(fp
= fopen(DATAFILE
, "r")))
32 pline("Cannot open data file!");
34 pline("Specify what? ");
37 while(fgets(buf
,BUFSZ
,fp
))
39 ep
= index(buf
, '\n');
41 /* else: bad data file */
42 /* Expand tab 'by hand' */
46 (void) strncpy(buf
+1, " ", 7);
51 if(readchar() == 'y') {
52 page_more(fp
,1); /* does fclose() */
56 (void) fclose(fp
); /* kopper@psuvax1 */
59 pline("I've never heard of such things.");
65 /* make the paging of a file interruptible */
66 static int got_intrup
;
73 /* simple pager, also used from dohelp() */
76 int strip
; /* nr of chars to be stripped from each line (0 or 1) */
78 register char *bufr
, *ep
;
79 sig_t prevsig
= signal(SIGINT
, intruph
);
82 bufr
= (char *) alloc((unsigned) CO
);
84 while(fgets(bufr
,CO
-1,fp
) && (!strip
|| *bufr
== '\t') && !got_intrup
){
85 ep
= index(bufr
, '\n');
88 if(page_line(bufr
+strip
)) {
97 (void) signal(SIGINT
, prevsig
);
101 static boolean whole_screen
= TRUE
;
102 #define PAGMIN 12 /* minimum # of lines for page below level map */
104 set_whole_screen() { /* called in termcap as soon as LI is known */
105 whole_screen
= (LI
-ROWNO
-2 <= PAGMIN
|| !CD
);
112 whole_screen
= TRUE
; /* force a docrt(), our first */
113 ret
= page_file(NEWS
, TRUE
);
115 return(ret
); /* report whether we did docrt() */
120 register int mode
; /* 0: open 1: wait+close 2: close */
127 /* use part of screen below level map */
149 page_line(s
) /* returns 1 if we should quit */
156 return(0); /* suppress blank lines at top */
177 * Flexible pager: feed it with a number of lines and it will decide
178 * whether these should be fed to the pager above, or displayed in a
181 * cornline(0, title or 0) : initialize
182 * cornline(1, text) : add text to the chain of texts
183 * cornline(2, morcs) : output everything and cleanup
184 * cornline(3, 0) : cleanup
192 struct line
*next_line
;
194 } *texthead
, *texttail
;
197 register struct line
*tl
;
204 cornline(1, text
); /* title */
205 cornline(1, ""); /* blank line */
213 if(!text
) return; /* superfluous, just to be sure */
219 alloc((unsigned)(len
+ sizeof(struct line
) + 1));
221 tl
->line_text
= (char *)(tl
+ 1);
222 (void) strcpy(tl
->line_text
, text
);
226 texttail
->next_line
= tl
;
231 /* --- now we really do it --- */
232 if(mode
== 2 && linect
== 1) /* topline only */
233 pline(texthead
->line_text
);
236 register int curline
, lth
;
238 if(flags
.toplin
== 1) more(); /* ab@unido */
241 lth
= CO
- maxlen
- 2; /* Use full screen width */
242 if (linect
< LI
&& lth
>= 10) { /* in a corner */
247 for (tl
= texthead
; tl
; tl
= tl
->next_line
) {
252 putstr (tl
->line_text
);
260 docorner (lth
, curline
-1);
261 } else { /* feed to pager */
263 for (tl
= texthead
; tl
; tl
= tl
->next_line
) {
264 if (page_line (tl
->line_text
)) {
278 while(tl
= texthead
) {
279 texthead
= tl
->next_line
;
288 pline ("Long or short help? ");
289 while (((c
= readchar ()) != 'l') && (c
!= 's') && !index(quitchars
,c
))
291 if (!index(quitchars
, c
))
292 (void) page_file((c
== 'l') ? HELP
: SHELP
, FALSE
);
296 page_file(fnam
, silent
) /* return: 0 - cannot open fnam; 1 - otherwise */
300 #ifdef DEF_PAGER /* this implies that UNIX is defined */
302 /* use external pager; this may give security problems */
304 register int fd
= open(fnam
, 0);
307 if(!silent
) pline("Cannot open %s.", fnam
);
311 extern char *catmore
;
313 /* Now that child() does a setuid(getuid()) and a chdir(),
314 we may not be able to open file fnam anymore, so make
318 if(!silent
) printf("Cannot open %s as stdin.\n", fnam
);
320 execl(catmore
, "page", (char *) 0);
321 if(!silent
) printf("Cannot exec %s.\n", catmore
);
329 FILE *f
; /* free after Robert Viduya */
331 if ((f
= fopen (fnam
, "r")) == (FILE *) 0) {
333 home(); perror (fnam
); flags
.toplin
= 1;
334 pline ("Cannot open %s.", fnam
);
350 if(str
= getenv("SHELL"))
351 execl(str
, str
, (char *) 0);
353 execl("/bin/sh", "sh", (char *) 0);
354 pline("sh: cannot execute.");
362 union wait
{ /* used only for the cast (union wait *) 0 */
365 unsigned short w_Termsig
:7;
366 unsigned short w_Coredump
:1;
367 unsigned short w_Retcode
:8;
374 #include <sys/wait.h>
385 if(f
== 0){ /* child */
386 settty((char *) 0); /* also calls end_screen() */
387 (void) setuid(getuid());
388 (void) setgid(getgid());
390 (void) chdir(getenv("HOME"));
394 if(f
== -1) { /* cannot fork */
395 pline("Fork failed. Try again.");
398 /* fork succeeded; wait for child to exit */
399 (void) signal(SIGINT
,SIG_IGN
);
400 (void) signal(SIGQUIT
,SIG_IGN
);
401 (void) wait(&status
);
404 (void) signal(SIGINT
,done1
);
406 if(wizard
) (void) signal(SIGQUIT
,SIG_DFL
);