1 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
2 /* hack.pager.c - version 1.0.3 */
4 /* This file contains the command routine dowhatis() and a pager. */
5 /* Also readmail() and doshell(), and generally the things that
6 contact the outside world. */
9 #include <sys/signal.h>
14 extern int CO, LI; /* usually COLNO and ROWNO+2 */
16 extern char quitchars[];
23 register char *buf = &bufr[6], *ep, q;
24 extern char readchar();
26 if(!(fp = fopen(DATAFILE, "r")))
27 pline("Cannot open data file!");
29 pline("Specify what? ");
32 while(fgets(buf,BUFSZ,fp))
34 ep = index(buf, '\n');
36 /* else: bad data file */
37 /* Expand tab 'by hand' */
41 (void) strncpy(buf+1, " ", 7);
46 if(readchar() == 'y') {
47 page_more(fp,1); /* does fclose() */
51 (void) fclose(fp); /* kopper@psuvax1 */
54 pline("I've never heard of such things.");
60 /* make the paging of a file interruptible */
61 static int got_intrup;
68 /* simple pager, also used from dohelp() */
71 int strip; /* nr of chars to be stripped from each line (0 or 1) */
73 register char *bufr, *ep;
74 sig_t prevsig = signal(SIGINT, intruph);
77 bufr = (char *) alloc((unsigned) CO);
79 while(fgets(bufr,CO-1,fp) && (!strip || *bufr == '\t') && !got_intrup){
80 ep = index(bufr, '\n');
83 if(page_line(bufr+strip)) {
92 (void) signal(SIGINT, prevsig);
96 static boolean whole_screen = TRUE;
97 #define PAGMIN 12 /* minimum # of lines for page below level map */
99 set_whole_screen() { /* called in termcap as soon as LI is known */
100 whole_screen = (LI-ROWNO-2 <= PAGMIN || !CD);
107 whole_screen = TRUE; /* force a docrt(), our first */
108 ret = page_file(NEWS, TRUE);
110 return(ret); /* report whether we did docrt() */
115 register int mode; /* 0: open 1: wait+close 2: close */
122 /* use part of screen below level map */
144 page_line(s) /* returns 1 if we should quit */
151 return(0); /* suppress blank lines at top */
172 * Flexible pager: feed it with a number of lines and it will decide
173 * whether these should be fed to the pager above, or displayed in a
176 * cornline(0, title or 0) : initialize
177 * cornline(1, text) : add text to the chain of texts
178 * cornline(2, morcs) : output everything and cleanup
179 * cornline(3, 0) : cleanup
187 struct line *next_line;
189 } *texthead, *texttail;
192 register struct line *tl;
199 cornline(1, text); /* title */
200 cornline(1, ""); /* blank line */
208 if(!text) return; /* superfluous, just to be sure */
214 alloc((unsigned)(len + sizeof(struct line) + 1));
216 tl->line_text = (char *)(tl + 1);
217 (void) strcpy(tl->line_text, text);
221 texttail->next_line = tl;
226 /* --- now we really do it --- */
227 if(mode == 2 && linect == 1) /* topline only */
228 pline(texthead->line_text);
231 register int curline, lth;
233 if(flags.toplin == 1) more(); /* ab@unido */
236 lth = CO - maxlen - 2; /* Use full screen width */
237 if (linect < LI && lth >= 10) { /* in a corner */
242 for (tl = texthead; tl; tl = tl->next_line) {
247 putstr (tl->line_text);
255 docorner (lth, curline-1);
256 } else { /* feed to pager */
258 for (tl = texthead; tl; tl = tl->next_line) {
259 if (page_line (tl->line_text)) {
273 while(tl = texthead) {
274 texthead = tl->next_line;
283 pline ("Long or short help? ");
284 while (((c = readchar ()) != 'l') && (c != 's') && !index(quitchars,c))
286 if (!index(quitchars, c))
287 (void) page_file((c == 'l') ? HELP : SHELP, FALSE);
291 page_file(fnam, silent) /* return: 0 - cannot open fnam; 1 - otherwise */
295 #ifdef DEF_PAGER /* this implies that UNIX is defined */
297 /* use external pager; this may give security problems */
299 register int fd = open(fnam, 0);
302 if(!silent) pline("Cannot open %s.", fnam);
306 extern char *catmore;
308 /* Now that child() does a setuid(getuid()) and a chdir(),
309 we may not be able to open file fnam anymore, so make
313 if(!silent) printf("Cannot open %s as stdin.\n", fnam);
315 execl(catmore, "page", (char *) 0);
316 if(!silent) printf("Cannot exec %s.\n", catmore);
324 FILE *f; /* free after Robert Viduya */
326 if ((f = fopen (fnam, "r")) == (FILE *) 0) {
328 home(); perror (fnam); flags.toplin = 1;
329 pline ("Cannot open %s.", fnam);
345 if(str = getenv("SHELL"))
346 execl(str, str, (char *) 0);
348 execl("/bin/sh", "sh", (char *) 0);
349 pline("sh: cannot execute.");
357 union wait { /* used only for the cast (union wait *) 0 */
360 unsigned short w_Termsig:7;
361 unsigned short w_Coredump:1;
362 unsigned short w_Retcode:8;
369 #include <sys/wait.h>
380 if(f == 0){ /* child */
381 settty((char *) 0); /* also calls end_screen() */
385 (void) chdir(getenv("HOME"));
389 if(f == -1) { /* cannot fork */
390 pline("Fork failed. Try again.");
393 /* fork succeeded; wait for child to exit */
394 (void) signal(SIGINT,SIG_IGN);
395 (void) signal(SIGQUIT,SIG_IGN);
396 (void) wait(&status);
399 (void) signal(SIGINT,done1);
401 if(wizard) (void) signal(SIGQUIT,SIG_DFL);