MFC: Make apps using '#define _POSIX_C_SOURCE' compile.
[dragonfly.git] / games / hack / hack.pager.c
blob7c411066e329dd17e3b87a34d3acbb5cb82faad5
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>
12 #include "hack.h"
13 extern int CO, LI; /* usually COLNO and ROWNO+2 */
14 extern char *CD;
15 extern char quitchars[];
17 static void intruph(int);
18 static void page_more(FILE *, int);
20 int
21 dowhatis(void)
23 FILE *fp;
24 char bufr[BUFSZ+6];
25 char *buf = &bufr[6], *ep, q;
27 if(!(fp = fopen(DATAFILE, "r")))
28 pline("Cannot open data file!");
29 else {
30 pline("Specify what? ");
31 q = readchar();
32 if(q != '\t')
33 while(fgets(buf,BUFSZ,fp))
34 if(*buf == q) {
35 ep = index(buf, '\n');
36 if(ep) *ep = 0;
37 /* else: bad data file */
38 /* Expand tab 'by hand' */
39 if(buf[1] == '\t'){
40 buf = bufr;
41 buf[0] = q;
42 strncpy(buf+1, " ", 7);
44 pline(buf);
45 if(ep[-1] == ';') {
46 pline("More info? ");
47 if(readchar() == 'y') {
48 page_more(fp,1); /* does fclose() */
49 return(0);
52 fclose(fp); /* kopper@psuvax1 */
53 return(0);
55 pline("I've never heard of such things.");
56 fclose(fp);
58 return(0);
61 /* make the paging of a file interruptible */
62 static int got_intrup;
64 static void
65 intruph(__unused int unused)
67 got_intrup++;
70 /* simple pager, also used from dohelp() */
71 /* strip = nr of chars to be stripped from each line (0 or 1) */
72 static void
73 page_more(FILE *fp, int strip)
75 char *bufr, *ep;
76 sig_t prevsig = signal(SIGINT, intruph);
78 set_pager(0);
79 bufr = (char *) alloc((unsigned) CO);
80 bufr[CO-1] = 0;
81 while(fgets(bufr,CO-1,fp) && (!strip || *bufr == '\t') && !got_intrup){
82 ep = index(bufr, '\n');
83 if(ep)
84 *ep = 0;
85 if(page_line(bufr+strip)) {
86 set_pager(2);
87 goto ret;
90 set_pager(1);
91 ret:
92 free(bufr);
93 fclose(fp);
94 signal(SIGINT, prevsig);
95 got_intrup = 0;
98 static boolean whole_screen = TRUE;
99 #define PAGMIN 12 /* minimum # of lines for page below level map */
101 void
102 set_whole_screen(void) /* called in termcap as soon as LI is known */
104 whole_screen = (LI-ROWNO-2 <= PAGMIN || !CD);
107 #ifdef NEWS
108 bool
109 readnews(void)
111 int ret;
113 whole_screen = TRUE; /* force a docrt(), our first */
114 ret = page_file(NEWS, TRUE);
115 set_whole_screen();
116 return(ret); /* report whether we did docrt() */
118 #endif /* NEWS */
120 void
121 set_pager(int mode) /* 0: open 1: wait+close 2: close */
123 static boolean so;
124 if(mode == 0) {
125 if(!whole_screen) {
126 /* clear topline */
127 clrlin();
128 /* use part of screen below level map */
129 curs(1, ROWNO+4);
130 } else {
131 cls();
133 so = flags.standout;
134 flags.standout = 1;
135 } else {
136 if(mode == 1) {
137 curs(1, LI);
138 more();
140 flags.standout = so;
141 if(whole_screen)
142 docrt();
143 else {
144 curs(1, ROWNO+4);
145 cl_eos();
150 bool
151 page_line(const char *s) /* returns 1 if we should quit */
153 if(cury == LI-1) {
154 if(!*s)
155 return(0); /* suppress blank lines at top */
156 putchar('\n');
157 cury++;
158 cmore("q\033");
159 if(morc) {
160 morc = 0;
161 return(1);
163 if(whole_screen)
164 cls();
165 else {
166 curs(1, ROWNO+4);
167 cl_eos();
170 puts(s);
171 cury++;
172 return(0);
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
178 * corner.
179 * Call:
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
186 void
187 cornline(int mode, const char *text)
189 static struct line {
190 struct line *next_line;
191 char *line_text;
192 } *texthead, *texttail;
193 static int maxlen;
194 static int linect;
195 struct line *tl;
197 if(mode == 0) {
198 texthead = 0;
199 maxlen = 0;
200 linect = 0;
201 if(text) {
202 cornline(1, text); /* title */
203 cornline(1, ""); /* blank line */
205 return;
208 if(mode == 1) {
209 int len;
211 if(!text) return; /* superfluous, just to be sure */
212 linect++;
213 len = strlen(text);
214 if(len > maxlen)
215 maxlen = len;
216 tl = (struct line *)
217 alloc((unsigned)(len + sizeof(struct line) + 1));
218 tl->next_line = 0;
219 tl->line_text = (char *)(tl + 1);
220 strcpy(tl->line_text, text);
221 if(!texthead)
222 texthead = tl;
223 else
224 texttail->next_line = tl;
225 texttail = tl;
226 return;
229 /* --- now we really do it --- */
230 if(mode == 2 && linect == 1) /* topline only */
231 pline(texthead->line_text);
232 else
233 if(mode == 2) {
234 int curline, lth;
236 if(flags.toplin == 1) more(); /* ab@unido */
237 remember_topl();
239 lth = CO - maxlen - 2; /* Use full screen width */
240 if (linect < LI && lth >= 10) { /* in a corner */
241 home ();
242 cl_end ();
243 flags.toplin = 0;
244 curline = 1;
245 for (tl = texthead; tl; tl = tl->next_line) {
246 curs (lth, curline);
247 if(curline > 1)
248 cl_end ();
249 putsym(' ');
250 putstr (tl->line_text);
251 curline++;
253 curs (lth, curline);
254 cl_end ();
255 cmore (text);
256 home ();
257 cl_end ();
258 docorner (lth, curline-1);
259 } else { /* feed to pager */
260 set_pager(0);
261 for (tl = texthead; tl; tl = tl->next_line) {
262 if (page_line (tl->line_text)) {
263 set_pager(2);
264 goto cleanup;
267 if(text) {
268 cgetret(text);
269 set_pager(2);
270 } else
271 set_pager(1);
275 cleanup:
276 while((tl = texthead)) {
277 texthead = tl->next_line;
278 free((char *) tl);
283 dohelp(void)
285 char c;
287 pline ("Long or short help? ");
288 while (((c = readchar ()) != 'l') && (c != 's') && !index(quitchars,c))
289 bell ();
290 if (!index(quitchars, c))
291 page_file((c == 'l') ? HELP : SHELP, FALSE);
292 return(0);
295 /* return: 0 - cannot open fnam; 1 - otherwise */
296 bool
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);
305 if(fd < 0) {
306 if(!silent) pline("Cannot open %s.", fnam);
307 return(0);
309 if(child(1)){
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
314 it stdin. */
315 close(0);
316 if(dup(fd)) {
317 if(!silent) printf("Cannot open %s as stdin.\n", fnam);
318 } else {
319 execl(catmore, "page", (char *) 0);
320 if(!silent) printf("Cannot exec %s.\n", catmore);
322 exit(1);
324 close(fd);
326 #else /* DEF_PAGER */
328 FILE *f; /* free after Robert Viduya */
330 if ((f = fopen (fnam, "r")) == (FILE *) 0) {
331 if(!silent) {
332 home(); perror (fnam); flags.toplin = 1;
333 pline ("Cannot open %s.", fnam);
335 return(0);
337 page_more(f, 0);
339 #endif /* DEF_PAGER */
341 return(1);
344 #ifdef UNIX
345 #ifdef SHELL
347 dosh(void)
349 char *str;
350 if(child(0)) {
351 if((str = getenv("SHELL")))
352 execl(str, str, (char *) 0);
353 else
354 execl("/bin/sh", "sh", (char *) 0);
355 pline("sh: cannot execute.");
356 exit(1);
358 return(0);
360 #endif /* SHELL */
362 #ifdef NOWAITINCLUDE
363 union wait { /* used only for the cast (union wait *) 0 */
364 int w_status;
365 struct {
366 unsigned short w_Termsig:7;
367 unsigned short w_Coredump:1;
368 unsigned short w_Retcode:8;
369 } w_T;
372 #else
374 #ifdef BSD
375 #include <sys/wait.h>
376 #else
377 #include <wait.h>
378 #endif /* BSD */
379 #endif /* NOWAITINCLUDE */
381 bool
382 child(bool wt)
384 int status;
385 int f;
387 f = fork();
388 if(f == 0){ /* child */
389 settty((char *) 0); /* also calls end_screen() */
390 /* revoke */
391 setgid(getgid());
392 #ifdef CHDIR
393 chdir(getenv("HOME"));
394 #endif /* CHDIR */
395 return(1);
397 if(f == -1) { /* cannot fork */
398 pline("Fork failed. Try again.");
399 return(0);
401 /* fork succeeded; wait for child to exit */
402 signal(SIGINT,SIG_IGN);
403 signal(SIGQUIT,SIG_IGN);
404 wait(&status);
405 gettty();
406 setftty();
407 signal(SIGINT,done1);
408 #ifdef WIZARD
409 if(wizard) signal(SIGQUIT,SIG_DFL);
410 #endif /* WIZARD */
411 if(wt) getret();
412 docrt();
413 return(0);
415 #endif /* UNIX */