Source:NetHack 3.6.7/src/pager.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to pager.c from the source code of NetHack 3.6.7. To link to a particular line, write [[Source:NetHack 3.6.7/src/pager.c#line12]], for example.

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

1. /* NetHack 3.6	pager.c	$NHDT-Date: 1574722864 2019/11/25 23:01:04 $  $NHDT-Branch: NetHack-3.6 $:$NHDT-Revision: 1.162 $ */
2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3. /*-Copyright (c) Robert Patrick Rankin, 2018. */
4. /* NetHack may be freely redistributed.  See license for details. */
5. 
6. /* This file contains the command routines dowhatis() and dohelp() and */
7. /* a few other help related facilities */
8. 
9. #include "hack.h"
10. #include "dlb.h"
11. 
12. STATIC_DCL boolean FDECL(is_swallow_sym, (int));
13. STATIC_DCL int FDECL(append_str, (char *, const char *));
14. STATIC_DCL void FDECL(look_at_object, (char *, int, int, int));
15. STATIC_DCL void FDECL(look_at_monster, (char *, char *,
16.                                         struct monst *, int, int));
17. STATIC_DCL struct permonst *FDECL(lookat, (int, int, char *, char *));
18. STATIC_DCL void FDECL(checkfile, (char *, struct permonst *,
19.                                   BOOLEAN_P, BOOLEAN_P, char *));
20. STATIC_DCL void FDECL(look_all, (BOOLEAN_P,BOOLEAN_P));
21. STATIC_DCL void FDECL(do_supplemental_info, (char *, struct permonst *,
22.                                              BOOLEAN_P));
23. STATIC_DCL void NDECL(whatdoes_help);
24. STATIC_DCL void NDECL(docontact);
25. STATIC_DCL void NDECL(dispfile_help);
26. STATIC_DCL void NDECL(dispfile_shelp);
27. STATIC_DCL void NDECL(dispfile_optionfile);
28. STATIC_DCL void NDECL(dispfile_license);
29. STATIC_DCL void NDECL(dispfile_debughelp);
30. STATIC_DCL void NDECL(hmenu_doextversion);
31. STATIC_DCL void NDECL(hmenu_dohistory);
32. STATIC_DCL void NDECL(hmenu_dowhatis);
33. STATIC_DCL void NDECL(hmenu_dowhatdoes);
34. STATIC_DCL void NDECL(hmenu_doextlist);
35. #ifdef PORT_HELP
36. extern void NDECL(port_help);
37. #endif
38. 
39. /* Returns "true" for characters that could represent a monster's stomach. */
40. STATIC_OVL boolean
41. is_swallow_sym(c)
42. int c;
43. {
44.     int i;
45. 
46.     for (i = S_sw_tl; i <= S_sw_br; i++)
47.         if ((int) showsyms[i] == c)
48.             return TRUE;
49.     return FALSE;
50. }
51. 
52. /* Append " or "+new_str to the end of buf if new_str doesn't already exist
53.    as a substring of buf.  Return 1 if the string was appended, 0 otherwise.
54.    It is expected that buf is of size BUFSZ. */
55. STATIC_OVL int
56. append_str(buf, new_str)
57. char *buf;
58. const char *new_str;
59. {
60.     static const char sep[] = " or ";
61.     size_t oldlen, space_left;
62.  
63.     if (strstri(buf, new_str))
64.         return 0; /* already present */
65. 
66.     oldlen = strlen(buf);
67.     if (oldlen >= BUFSZ - 1) {
68.         if (oldlen > BUFSZ - 1)
69.             impossible("append_str: 'buf' contains %lu characters.",
70.                        (unsigned long) oldlen);
71.         return 0; /* no space available */
72.     }
73.  
74.     /* some space available, but not necessarily enough for full append */
75.     space_left = BUFSZ - 1 - oldlen;  /* space remaining in buf */
76.     (void) strncat(buf, sep, space_left);
77.     if (space_left > sizeof sep - 1)
78.         (void) strncat(buf, new_str, space_left - (sizeof sep - 1));
79.     return 1; /* something was appended, possibly just part of " or " */
80. }
81. 
82. /* shared by monster probing (via query_objlist!) as well as lookat() */
83. char *
84. self_lookat(outbuf)
85. char *outbuf;
86. {
87.     char race[QBUFSZ];
88. 
89.     /* include race with role unless polymorphed */
90.     race[0] = '\0';
91.     if (!Upolyd)
92.         Sprintf(race, "%s ", urace.adj);
93.     Sprintf(outbuf, "%s%s%s called %s",
94.             /* being blinded may hide invisibility from self */
95.             (Invis && (senseself() || !Blind)) ? "invisible " : "", race,
96.             mons[u.umonnum].mname, plname);
97.     if (u.usteed)
98.         Sprintf(eos(outbuf), ", mounted on %s", y_monnam(u.usteed));
99.     if (u.uundetected || (Upolyd && U_AP_TYPE))
100.         mhidden_description(&youmonst, FALSE, eos(outbuf));
101.     return outbuf;
102. }
103. 
104. /* describe a hidden monster; used for look_at during extended monster
105.    detection and for probing; also when looking at self */
106. void
107. mhidden_description(mon, altmon, outbuf)
108. struct monst *mon;
109. boolean altmon; /* for probing: if mimicking a monster, say so */
110. char *outbuf;
111. {
112.     struct obj *otmp;
113.     boolean fakeobj, isyou = (mon == &youmonst);
114.     int x = isyou ? u.ux : mon->mx, y = isyou ? u.uy : mon->my,
115.         glyph = (level.flags.hero_memory && !isyou) ? levl[x][y].glyph
116.                                                     : glyph_at(x, y);
117. 
118.     *outbuf = '\0';
119.     if (M_AP_TYPE(mon) == M_AP_FURNITURE
120.         || M_AP_TYPE(mon) == M_AP_OBJECT) {
121.         Strcpy(outbuf, ", mimicking ");
122.         if (M_AP_TYPE(mon) == M_AP_FURNITURE) {
123.             Strcat(outbuf, an(defsyms[mon->mappearance].explanation));
124.         } else if (M_AP_TYPE(mon) == M_AP_OBJECT
125.                    /* remembered glyph, not glyph_at() which is 'mon' */
126.                    && glyph_is_object(glyph)) {
127.  objfrommap:
128.             otmp = (struct obj *) 0;
129.             fakeobj = object_from_map(glyph, x, y, &otmp);
130.             Strcat(outbuf, (otmp && otmp->otyp != STRANGE_OBJECT)
131.                               ? ansimpleoname(otmp)
132.                               : an(obj_descr[STRANGE_OBJECT].oc_name));
133.             if (fakeobj) {
134.                 otmp->where = OBJ_FREE; /* object_from_map set to OBJ_FLOOR */
135.                 dealloc_obj(otmp);
136.             }
137.         } else {
138.             Strcat(outbuf, something);
139.         }
140.     } else if (M_AP_TYPE(mon) == M_AP_MONSTER) {
141.         if (altmon)
142.             Sprintf(outbuf, ", masquerading as %s",
143.                     an(mons[mon->mappearance].mname));
144.     } else if (isyou ? u.uundetected : mon->mundetected) {
145.         Strcpy(outbuf, ", hiding");
146.         if (hides_under(mon->data)) {
147.             Strcat(outbuf, " under ");
148.             /* remembered glyph, not glyph_at() which is 'mon' */
149.             if (glyph_is_object(glyph))
150.                 goto objfrommap;
151.             Strcat(outbuf, something);
152.         } else if (is_hider(mon->data)) {
153.             Sprintf(eos(outbuf), " on the %s",
154.                     (is_flyer(mon->data) || mon->data->mlet == S_PIERCER)
155.                        ? "ceiling"
156.                        : surface(x, y)); /* trapper */
157.         } else {
158.             if (mon->data->mlet == S_EEL && is_pool(x, y))
159.                 Strcat(outbuf, " in murky water");
160.         }
161.     }
162. }
163. 
164. /* extracted from lookat(); also used by namefloorobj() */
165. boolean
166. object_from_map(glyph, x, y, obj_p)
167. int glyph, x, y;
168. struct obj **obj_p;
169. {
170.     boolean fakeobj = FALSE, mimic_obj = FALSE;
171.     struct monst *mtmp;
172.     struct obj *otmp;
173.     int glyphotyp = glyph_to_obj(glyph);
174. 
175.     *obj_p = (struct obj *) 0;
176.     /* TODO: check inside containers in case glyph came from detection */
177.     if ((otmp = sobj_at(glyphotyp, x, y)) == 0)
178.         for (otmp = level.buriedobjlist; otmp; otmp = otmp->nobj)
179.             if (otmp->ox == x && otmp->oy == y && otmp->otyp == glyphotyp)
180.                 break;
181. 
182.     /* there might be a mimic here posing as an object */
183.     mtmp = m_at(x, y);
184.     if (mtmp && is_obj_mappear(mtmp, (unsigned) glyphotyp)) {
185.         otmp = 0;
186.         mimic_obj = TRUE;
187.     } else
188.         mtmp = 0;
189. 
190.     if (!otmp || otmp->otyp != glyphotyp) {
191.         /* this used to exclude STRANGE_OBJECT; now caller deals with it */
192.         otmp = mksobj(glyphotyp, FALSE, FALSE);
193.         if (!otmp)
194.             return FALSE;
195.         fakeobj = TRUE;
196.         if (otmp->oclass == COIN_CLASS)
197.             otmp->quan = 2L; /* to force pluralization */
198.         else if (otmp->otyp == SLIME_MOLD)
199.             otmp->spe = context.current_fruit; /* give it a type */
200.         if (mtmp && has_mcorpsenm(mtmp)) { /* mimic as corpse/statue */
201.             if (otmp->otyp == SLIME_MOLD)
202.                 /* override context.current_fruit to avoid
203.                      look, use 'O' to make new named fruit, look again
204.                    giving different results when current_fruit changes */
205.                 otmp->spe = MCORPSENM(mtmp);
206.             else
207.                 otmp->corpsenm = MCORPSENM(mtmp);
208.         } else if (otmp->otyp == CORPSE && glyph_is_body(glyph)) {
209.             otmp->corpsenm = glyph - GLYPH_BODY_OFF;
210.         } else if (otmp->otyp == STATUE && glyph_is_statue(glyph)) {
211.             otmp->corpsenm = glyph - GLYPH_STATUE_OFF;
212.         }
213.         if (otmp->otyp == LEASH)
214.             otmp->leashmon = 0;
215.         /* extra fields needed for shop price with doname() formatting */
216.         otmp->where = OBJ_FLOOR;
217.         otmp->ox = x, otmp->oy = y;
218.         otmp->no_charge = (otmp->otyp == STRANGE_OBJECT && costly_spot(x, y));
219.     }
220.     /* if located at adjacent spot, mark it as having been seen up close
221.        (corpse type will be known even if dknown is 0, so we don't need a
222.        touch check for cockatrice corpse--we're looking without touching) */
223.     if (otmp && distu(x, y) <= 2 && !Blind && !Hallucination
224.         /* redundant: we only look for an object which matches current
225.            glyph among floor and buried objects; when !Blind, any buried
226.            object's glyph will have been replaced by whatever is present
227.            on the surface as soon as we moved next to its spot */
228.         && (fakeobj || otmp->where == OBJ_FLOOR) /* not buried */
229.         /* terrain mode views what's already known, doesn't learn new stuff */
230.         && !iflags.terrainmode) /* so don't set dknown when in terrain mode */
231.         otmp->dknown = 1; /* if a pile, clearly see the top item only */
232.     if (fakeobj && mtmp && mimic_obj &&
233.         (otmp->dknown || (M_AP_FLAG(mtmp) & M_AP_F_DKNOWN))) {
234.             mtmp->m_ap_type |= M_AP_F_DKNOWN;
235.             otmp->dknown = 1;
236.     }
237.     *obj_p = otmp;
238.     return fakeobj; /* when True, caller needs to dealloc *obj_p */
239. }
240. 
241. STATIC_OVL void
242. look_at_object(buf, x, y, glyph)
243. char *buf; /* output buffer */
244. int x, y, glyph;
245. {
246.     struct obj *otmp = 0;
247.     boolean fakeobj = object_from_map(glyph, x, y, &otmp);
248. 
249.     if (otmp) {
250.         Strcpy(buf, (otmp->otyp != STRANGE_OBJECT)
251.                      ? distant_name(otmp, otmp->dknown ? doname_with_price
252.                                                        : doname_vague_quan)
253.                      : obj_descr[STRANGE_OBJECT].oc_name);
254.         if (fakeobj) {
255.             otmp->where = OBJ_FREE; /* object_from_map set it to OBJ_FLOOR */
256.             dealloc_obj(otmp), otmp = 0;
257.         }
258.     } else
259.         Strcpy(buf, something); /* sanity precaution */
260. 
261.     if (otmp && otmp->where == OBJ_BURIED)
262.         Strcat(buf, " (buried)");
263.     else if (levl[x][y].typ == STONE || levl[x][y].typ == SCORR)
264.         Strcat(buf, " embedded in stone");
265.     else if (IS_WALL(levl[x][y].typ) || levl[x][y].typ == SDOOR)
266.         Strcat(buf, " embedded in a wall");
267.     else if (closed_door(x, y))
268.         Strcat(buf, " embedded in a door");
269.     else if (is_pool(x, y))
270.         Strcat(buf, " in water");
271.     else if (is_lava(x, y))
272.         Strcat(buf, " in molten lava"); /* [can this ever happen?] */
273.     return;
274. }
275. 
276. STATIC_OVL void
277. look_at_monster(buf, monbuf, mtmp, x, y)
278. char *buf, *monbuf; /* buf: output, monbuf: optional output */
279. struct monst *mtmp;
280. int x, y;
281. {
282.     char *name, monnambuf[BUFSZ];
283.     boolean accurate = !Hallucination;
284. 
285.     name = (mtmp->data == &mons[PM_COYOTE] && accurate)
286.               ? coyotename(mtmp, monnambuf)
287.               : distant_monnam(mtmp, ARTICLE_NONE, monnambuf);
288.     Sprintf(buf, "%s%s%s",
289.             (mtmp->mx != x || mtmp->my != y)
290.                 ? ((mtmp->isshk && accurate) ? "tail of " : "tail of a ")
291.                 : "",
292.             (mtmp->mtame && accurate)
293.                 ? "tame "
294.                 : (mtmp->mpeaceful && accurate)
295.                     ? "peaceful "
296.                     : "",
297.             name);
298.     if (u.ustuck == mtmp) {
299.         if (u.uswallow || iflags.save_uswallow) /* monster detection */
300.             Strcat(buf, is_animal(mtmp->data)
301.                           ? ", swallowing you" : ", engulfing you");
302.         else
303.             Strcat(buf, (Upolyd && sticks(youmonst.data))
304.                           ? ", being held" : ", holding you");
305.     }
306.     if (mtmp->mleashed)
307.         Strcat(buf, ", leashed to you");
308. 
309.     if (mtmp->mtrapped && cansee(mtmp->mx, mtmp->my)) {
310.         struct trap *t = t_at(mtmp->mx, mtmp->my);
311.         int tt = t ? t->ttyp : NO_TRAP;
312. 
313.         /* newsym lets you know of the trap, so mention it here */
314.         if (tt == BEAR_TRAP || is_pit(tt) || tt == WEB) {
315.             Sprintf(eos(buf), ", trapped in %s",
316.                     an(defsyms[trap_to_defsym(tt)].explanation));
317.             t->tseen = 1;
318.         }
319.     }
320. 
321.     /* we know the hero sees a monster at this location, but if it's shown
322.        due to persistant monster detection he might remember something else */
323.     if (mtmp->mundetected || M_AP_TYPE(mtmp))
324.         mhidden_description(mtmp, FALSE, eos(buf));
325. 
326.     if (monbuf) {
327.         unsigned how_seen = howmonseen(mtmp);
328. 
329.         monbuf[0] = '\0';
330.         if (how_seen != 0 && how_seen != MONSEEN_NORMAL) {
331.             if (how_seen & MONSEEN_NORMAL) {
332.                 Strcat(monbuf, "normal vision");
333.                 how_seen &= ~MONSEEN_NORMAL;
334.                 /* how_seen can't be 0 yet... */
335.                 if (how_seen)
336.                     Strcat(monbuf, ", ");
337.             }
338.             if (how_seen & MONSEEN_SEEINVIS) {
339.                 Strcat(monbuf, "see invisible");
340.                 how_seen &= ~MONSEEN_SEEINVIS;
341.                 if (how_seen)
342.                     Strcat(monbuf, ", ");
343.             }
344.             if (how_seen & MONSEEN_INFRAVIS) {
345.                 Strcat(monbuf, "infravision");
346.                 how_seen &= ~MONSEEN_INFRAVIS;
347.                 if (how_seen)
348.                     Strcat(monbuf, ", ");
349.             }
350.             if (how_seen & MONSEEN_TELEPAT) {
351.                 Strcat(monbuf, "telepathy");
352.                 how_seen &= ~MONSEEN_TELEPAT;
353.                 if (how_seen)
354.                     Strcat(monbuf, ", ");
355.             }
356.             if (how_seen & MONSEEN_XRAYVIS) {
357.                 /* Eyes of the Overworld */
358.                 Strcat(monbuf, "astral vision");
359.                 how_seen &= ~MONSEEN_XRAYVIS;
360.                 if (how_seen)
361.                     Strcat(monbuf, ", ");
362.             }
363.             if (how_seen & MONSEEN_DETECT) {
364.                 Strcat(monbuf, "monster detection");
365.                 how_seen &= ~MONSEEN_DETECT;
366.                 if (how_seen)
367.                     Strcat(monbuf, ", ");
368.             }
369.             if (how_seen & MONSEEN_WARNMON) {
370.                 if (Hallucination) {
371.                     Strcat(monbuf, "paranoid delusion");
372.                 } else {
373.                     unsigned long mW = (context.warntype.obj
374.                                         | context.warntype.polyd),
375.                                   m2 = mtmp->data->mflags2;
376.                     const char *whom = ((mW & M2_HUMAN & m2) ? "human"
377.                                         : (mW & M2_ELF & m2) ? "elf"
378.                                           : (mW & M2_ORC & m2) ? "orc"
379.                                             : (mW & M2_DEMON & m2) ? "demon"
380.                                               : mtmp->data->mname);
381. 
382.                     Sprintf(eos(monbuf), "warned of %s", makeplural(whom));
383.                 }
384.                 how_seen &= ~MONSEEN_WARNMON;
385.                 if (how_seen)
386.                     Strcat(monbuf, ", ");
387.             }
388.             /* should have used up all the how_seen bits by now */
389.             if (how_seen) {
390.                 impossible("lookat: unknown method of seeing monster");
391.                 Sprintf(eos(monbuf), "(%u)", how_seen);
392.             }
393.         } /* seen by something other than normal vision */
394.     } /* monbuf is non-null */
395. }
396. 
397. /*
398.  * Return the name of the glyph found at (x,y).
399.  * If not hallucinating and the glyph is a monster, also monster data.
400.  */
401. STATIC_OVL struct permonst *
402. lookat(x, y, buf, monbuf)
403. int x, y;
404. char *buf, *monbuf;
405. {
406.     struct monst *mtmp = (struct monst *) 0;
407.     struct permonst *pm = (struct permonst *) 0;
408.     int glyph;
409. 
410.     buf[0] = monbuf[0] = '\0';
411.     glyph = glyph_at(x, y);
412.     if (u.ux == x && u.uy == y && canspotself()
413.         && !(iflags.save_uswallow &&
414.              glyph == mon_to_glyph(u.ustuck, rn2_on_display_rng))
415.         && (!iflags.terrainmode || (iflags.terrainmode & TER_MON) != 0)) {
416.         /* fill in buf[] */
417.         (void) self_lookat(buf);
418. 
419.         /* file lookup can't distinguish between "gnomish wizard" monster
420.            and correspondingly named player character, always picking the
421.            former; force it to find the general "wizard" entry instead */
422.         if (Role_if(PM_WIZARD) && Race_if(PM_GNOME) && !Upolyd)
423.             pm = &mons[PM_WIZARD];
424. 
425.         /* When you see yourself normally, no explanation is appended
426.            (even if you could also see yourself via other means).
427.            Sensing self while blind or swallowed is treated as if it
428.            were by normal vision (cf canseeself()). */
429.         if ((Invisible || u.uundetected) && !Blind
430.             && !(u.uswallow || iflags.save_uswallow)) {
431.             unsigned how = 0;
432. 
433.             if (Infravision)
434.                 how |= 1;
435.             if (Unblind_telepat)
436.                 how |= 2;
437.             if (Detect_monsters)
438.                 how |= 4;
439. 
440.             if (how)
441.                 Sprintf(eos(buf), " [seen: %s%s%s%s%s]",
442.                         (how & 1) ? "infravision" : "",
443.                         /* add comma if telep and infrav */
444.                         ((how & 3) > 2) ? ", " : "",
445.                         (how & 2) ? "telepathy" : "",
446.                         /* add comma if detect and (infrav or telep or both) */
447.                         ((how & 7) > 4) ? ", " : "",
448.                         (how & 4) ? "monster detection" : "");
449.         }
450.     } else if (u.uswallow) {
451.         /* when swallowed, we're only called for spots adjacent to hero,
452.            and blindness doesn't prevent hero from feeling what holds him */
453.         Sprintf(buf, "interior of %s", a_monnam(u.ustuck));
454.         pm = u.ustuck->data;
455.     } else if (glyph_is_monster(glyph)) {
456.         bhitpos.x = x;
457.         bhitpos.y = y;
458.         if ((mtmp = m_at(x, y)) != 0) {
459.             look_at_monster(buf, monbuf, mtmp, x, y);
460.             pm = mtmp->data;
461.         } else if (Hallucination) {
462.             /* 'monster' must actually be a statue */
463.             Strcpy(buf, rndmonnam((char *) 0));
464.         }
465.     } else if (glyph_is_object(glyph)) {
466.         look_at_object(buf, x, y, glyph); /* fill in buf[] */
467.     } else if (glyph_is_trap(glyph)) {
468.         int tnum = what_trap(glyph_to_trap(glyph), rn2_on_display_rng);
469. 
470.         /* Trap detection displays a bear trap at locations having
471.          * a trapped door or trapped container or both.
472.          * TODO: we should create actual trap types for doors and
473.          * chests so that they can have their own glyphs and tiles.
474.          */
475.         if (trapped_chest_at(tnum, x, y))
476.             Strcpy(buf, "trapped chest"); /* might actually be a large box */
477.         else if (trapped_door_at(tnum, x, y))
478.             Strcpy(buf, "trapped door"); /* not "trap door"... */
479.         else
480.             Strcpy(buf, defsyms[trap_to_defsym(tnum)].explanation);
481.     } else if (glyph_is_warning(glyph)) {
482.         int warnindx = glyph_to_warning(glyph);
483. 
484.         Strcpy(buf, def_warnsyms[warnindx].explanation);
485.     } else if (!glyph_is_cmap(glyph)) {
486.         Strcpy(buf, "unexplored area");
487.     } else {
488.         int amsk;
489.         aligntyp algn;
490. 
491.         switch (glyph_to_cmap(glyph)) {
492.         case S_altar:
493.             amsk = ((mtmp = m_at(x, y)) != 0 && has_mcorpsenm(mtmp)
494.                     && M_AP_TYPE(mtmp) == M_AP_FURNITURE
495.                     && mtmp->mappearance == S_altar) ? MCORPSENM(mtmp)
496.                    : levl[x][y].altarmask;
497.             algn = Amask2align(amsk & ~AM_SHRINE);
498.             Sprintf(buf, "%s %saltar",
499.                     /* like endgame high priests, endgame high altars
500.                        are only recognizable when immediately adjacent */
501.                     (Is_astralevel(&u.uz) && distu(x, y) > 2)
502.                         ? "aligned"
503.                         : align_str(algn),
504.                     ((amsk & AM_SHRINE) != 0
505.                      && (Is_astralevel(&u.uz) || Is_sanctum(&u.uz)))
506.                         ? "high "
507.                         : "");
508.             break;
509.         case S_ndoor:
510.             if (is_drawbridge_wall(x, y) >= 0)
511.                 Strcpy(buf, "open drawbridge portcullis");
512.             else if ((levl[x][y].doormask & ~D_TRAPPED) == D_BROKEN)
513.                 Strcpy(buf, "broken door");
514.             else
515.                 Strcpy(buf, "doorway");
516.             break;
517.         case S_cloud:
518.             Strcpy(buf,
519.                    Is_airlevel(&u.uz) ? "cloudy area" : "fog/vapor cloud");
520.             break;
521.         case S_stone:
522.             if (!levl[x][y].seenv) {
523.                 Strcpy(buf, "unexplored");
524.                 break;
525.             } else if (Underwater && !Is_waterlevel(&u.uz)) {
526.                 /* "unknown" == previously mapped but not visible when
527.                    submerged; better terminology appreciated... */
528.                 Strcpy(buf, (distu(x, y) <= 2) ? "land" : "unknown");
529.                 break;
530.             } else if (levl[x][y].typ == STONE || levl[x][y].typ == SCORR) {
531.                 Strcpy(buf, "stone");
532.                 break;
533.             }
534.             /*FALLTHRU*/
535.         default:
536.             Strcpy(buf, defsyms[glyph_to_cmap(glyph)].explanation);
537.             break;
538.         }
539.     }
540.     return (pm && !Hallucination) ? pm : (struct permonst *) 0;
541. }
542. 
543. /*
544.  * Look in the "data" file for more info.  Called if the user typed in the
545.  * whole name (user_typed_name == TRUE), or we've found a possible match
546.  * with a character/glyph and flags.help is TRUE.
547.  *
548.  * NOTE: when (user_typed_name == FALSE), inp is considered read-only and
549.  *       must not be changed directly, e.g. via lcase(). We want to force
550.  *       lcase() for data.base lookup so that we can have a clean key.
551.  *       Therefore, we create a copy of inp _just_ for data.base lookup.
552.  */
553. STATIC_OVL void
554. checkfile(inp, pm, user_typed_name, without_asking, supplemental_name)
555. char *inp;
556. struct permonst *pm;
557. boolean user_typed_name, without_asking;
558. char *supplemental_name;
559. {
560.     dlb *fp;
561.     char buf[BUFSZ], newstr[BUFSZ], givenname[BUFSZ];
562.     char *ep, *dbase_str;
563.     unsigned long txt_offset = 0L;
564.     winid datawin = WIN_ERR;
565. 
566.     fp = dlb_fopen(DATAFILE, "r");
567.     if (!fp) {
568.         pline("Cannot open 'data' file!");
569.         return;
570.     }
571.     /* If someone passed us garbage, prevent fault. */
572.     if (!inp || strlen(inp) > (BUFSZ - 1)) {
573.         impossible("bad do_look buffer passed (%s)!",
574.                    !inp ? "null" : "too long");
575.         goto checkfile_done;
576.     }
577. 
578.     /* To prevent the need for entries in data.base like *ngel to account
579.      * for Angel and angel, make the lookup string the same for both
580.      * user_typed_name and picked name.
581.      */
582.     if (pm != (struct permonst *) 0 && !user_typed_name)
583.         dbase_str = strcpy(newstr, pm->mname);
584.     else
585.         dbase_str = strcpy(newstr, inp);
586.     (void) lcase(dbase_str);
587. 
588.     /*
589.      * TODO:
590.      * The switch from xname() to doname_vague_quan() in look_at_obj()
591.      * had the unintendded side-effect of making names picked from
592.      * pointing at map objects become harder to simplify for lookup.
593.      * We should split the prefix and suffix handling used by wish
594.      * parsing and also wizmode monster generation out into separate
595.      * routines and use those routines here.  This currently lacks
596.      * erosion handling and probably lots of other bits and pieces
597.      * that wishing already understands and most of this duplicates
598.      * stuff already done for wish handling or monster generation.
599.      */
600.     if (!strncmp(dbase_str, "interior of ", 12))
601.         dbase_str += 12;
602.     if (!strncmp(dbase_str, "a ", 2))
603.         dbase_str += 2;
604.     else if (!strncmp(dbase_str, "an ", 3))
605.         dbase_str += 3;
606.     else if (!strncmp(dbase_str, "the ", 4))
607.         dbase_str += 4;
608.     else if (!strncmp(dbase_str, "some ", 5))
609.         dbase_str += 5;
610.     else if (digit(*dbase_str)) {
611.         /* remove count prefix ("2 ya") which can come from looking at map */
612.         while (digit(*dbase_str))
613.             ++dbase_str;
614.         if (*dbase_str == ' ')
615.             ++dbase_str;
616.     }
617.     if (!strncmp(dbase_str, "pair of ", 8))
618.         dbase_str += 8;
619.     if (!strncmp(dbase_str, "tame ", 5))
620.         dbase_str += 5;
621.     else if (!strncmp(dbase_str, "peaceful ", 9))
622.         dbase_str += 9;
623.     if (!strncmp(dbase_str, "invisible ", 10))
624.         dbase_str += 10;
625.     if (!strncmp(dbase_str, "saddled ", 8))
626.         dbase_str += 8;
627.     if (!strncmp(dbase_str, "blessed ", 8))
628.         dbase_str += 8;
629.     else if (!strncmp(dbase_str, "uncursed ", 9))
630.         dbase_str += 9;
631.     else if (!strncmp(dbase_str, "cursed ", 7))
632.         dbase_str += 7;
633.     if (!strncmp(dbase_str, "empty ", 6))
634.         dbase_str += 6;
635.     if (!strncmp(dbase_str, "partly used ", 12))
636.         dbase_str += 12;
637.     else if (!strncmp(dbase_str, "partly eaten ", 13))
638.         dbase_str += 13;
639.     if (!strncmp(dbase_str, "statue of ", 10))
640.         dbase_str[6] = '\0';
641.     else if (!strncmp(dbase_str, "figurine of ", 12))
642.         dbase_str[8] = '\0';
643.     /* remove enchantment ("+0 aklys"); [for 3.6.0 and earlier, this wasn't
644.        needed because looking at items on the map used xname() rather than
645.        doname() hence known enchantment was implicitly suppressed] */
646.     if (*dbase_str && index("+-", dbase_str[0]) && digit(dbase_str[1])) {
647.         ++dbase_str; /* skip sign */
648.         while (digit(*dbase_str))
649.             ++dbase_str;
650.         if (*dbase_str == ' ')
651.             ++dbase_str;
652.     }
653.     /* "towel", "wet towel", and "moist towel" share one data.base entry;
654.        for "wet towel", we keep prefix so that the prompt will ask about
655.        "wet towel"; for "moist towel", we also want to ask about "wet towel".
656.        (note: strncpy() only terminates output string if the specified
657.        count is bigger than the length of the substring being copied) */
658.     if (!strncmp(dbase_str, "moist towel", 11))
659.         (void) strncpy(dbase_str += 2, "wet", 3); /* skip "mo" replace "ist" */
660. 
661.     /* Make sure the name is non-empty. */
662.     if (*dbase_str) {
663.         long pass1offset = -1L;
664.         int chk_skip, pass = 1;
665.         boolean yes_to_moreinfo, found_in_file, pass1found_in_file,
666.                 skipping_entry;
667.         char *sp, *ap, *alt = 0; /* alternate description */
668. 
669.         /* adjust the input to remove "named " and "called " */
670.         if ((ep = strstri(dbase_str, " named ")) != 0) {
671.             alt = ep + 7;
672.             if ((ap = strstri(dbase_str, " called ")) != 0 && ap < ep)
673.                 ep = ap; /* "named" is alt but truncate at "called" */
674.         } else if ((ep = strstri(dbase_str, " called ")) != 0) {
675.             copynchars(givenname, ep + 8, BUFSZ - 1);
676.             alt = givenname;
677.             if (supplemental_name && (sp = strstri(inp, " called ")) != 0)
678.                 copynchars(supplemental_name, sp + 8, BUFSZ - 1);
679.         } else
680.             ep = strstri(dbase_str, ", ");
681.         if (ep && ep > dbase_str)
682.             *ep = '\0';
683.         /* remove article from 'alt' name ("a pair of lenses named
684.            The Eyes of the Overworld" simplified above to "lenses named
685.            The Eyes of the Overworld", now reduced to "The Eyes of the
686.            Overworld", skip "The" as with base name processing) */
687.         if (alt && (!strncmpi(alt, "a ", 2)
688.                     || !strncmpi(alt, "an ", 3)
689.                     || !strncmpi(alt, "the ", 4)))
690.             alt = index(alt, ' ') + 1;
691.         /* remove charges or "(lit)" or wizmode "(N aum)" */
692.         if ((ep = strstri(dbase_str, " (")) != 0 && ep > dbase_str)
693.             *ep = '\0';
694.         if (alt && (ap = strstri(alt, " (")) != 0 && ap > alt)
695.             *ap = '\0';
696. 
697.         /*
698.          * If the object is named, then the name is the alternate description;
699.          * otherwise, the result of makesingular() applied to the name is.
700.          * This isn't strictly optimal, but named objects of interest to the
701.          * user will usually be found under their name, rather than under
702.          * their object type, so looking for a singular form is pointless.
703.          */
704.         if (!alt)
705.             alt = makesingular(dbase_str);
706. 
707.         pass1found_in_file = FALSE;
708.         for (pass = !strcmp(alt, dbase_str) ? 0 : 1; pass >= 0; --pass) {
709.             found_in_file = skipping_entry = FALSE;
710.             txt_offset = 0L;
711.             if (dlb_fseek(fp, txt_offset, SEEK_SET) < 0 ) {
712.                 impossible("can't get to start of 'data' file");
713.                 goto checkfile_done;
714.             }
715.             /* skip first record; read second */
716.             if (!dlb_fgets(buf, BUFSZ, fp) || !dlb_fgets(buf, BUFSZ, fp)) {
717.                 impossible("can't read 'data' file");
718.                 goto checkfile_done;
719.             } else if (sscanf(buf, "%8lx\n", &txt_offset) < 1
720.                        || txt_offset == 0L)
721.                 goto bad_data_file;
722. 
723.             /* look for the appropriate entry */
724.             while (dlb_fgets(buf, BUFSZ, fp)) {
725.                 if (*buf == '.')
726.                     break; /* we passed last entry without success */
727. 
728.                 if (digit(*buf)) {
729.                     /* a number indicates the end of current entry */
730.                     skipping_entry = FALSE;
731.                 } else if (!skipping_entry) {
732.                     if (!(ep = index(buf, '\n')))
733.                         goto bad_data_file;
734.                     (void) strip_newline((ep > buf) ? ep - 1 : ep);
735.                     /* if we match a key that begins with "~", skip
736.                        this entry */
737.                     chk_skip = (*buf == '~') ? 1 : 0;
738.                     if ((pass == 0 && pmatch(&buf[chk_skip], dbase_str))
739.                         || (pass == 1 && alt && pmatch(&buf[chk_skip], alt))) {
740.                         if (chk_skip) {
741.                             skipping_entry = TRUE;
742.                             continue;
743.                         } else {
744.                             found_in_file = TRUE;
745.                             if (pass == 1)
746.                                 pass1found_in_file = TRUE;
747.                             break;
748.                         }
749.                     }
750.                 }
751.             }
752.             if (found_in_file) {
753.                 long entry_offset, fseekoffset;
754.                 int entry_count;
755.                 int i;
756. 
757.                 /* skip over other possible matches for the info */
758.                 do {
759.                     if (!dlb_fgets(buf, BUFSZ, fp))
760.                         goto bad_data_file;
761.                 } while (!digit(*buf));
762.                 if (sscanf(buf, "%ld,%d\n", &entry_offset, &entry_count) < 2)
763.                     goto bad_data_file;
764.                 fseekoffset = (long) txt_offset + entry_offset;
765.                 if (pass == 1)
766.                     pass1offset = fseekoffset;
767.                 else if (fseekoffset == pass1offset)
768.                     goto checkfile_done;
769. 
770.                 yes_to_moreinfo = FALSE;
771.                 if (!user_typed_name && !without_asking) {
772.                     char *entrytext = pass ? alt : dbase_str;
773.                     char question[QBUFSZ];
774. 
775.                     Strcpy(question, "More info about \"");
776.                     /* +2 => length of "\"?" */
777.                     copynchars(eos(question), entrytext,
778.                                (int) (sizeof question - 1
779.                                       - (strlen(question) + 2)));
780.                     Strcat(question, "\"?");
781.                     if (yn(question) == 'y')
782.                         yes_to_moreinfo = TRUE;
783.                 }
784. 
785.                 if (user_typed_name || without_asking || yes_to_moreinfo) {
786.                     if (dlb_fseek(fp, fseekoffset, SEEK_SET) < 0) {
787.                         pline("? Seek error on 'data' file!");
788.                         goto checkfile_done;
789.                     }
790.                     datawin = create_nhwindow(NHW_MENU);
791.                     for (i = 0; i < entry_count; i++) {
792.                         if (!dlb_fgets(buf, BUFSZ, fp))
793.                             goto bad_data_file;
794.                         (void) strip_newline(buf);
795.                         if (index(buf + 1, '\t') != 0)
796.                             (void) tabexpand(buf + 1);
797.                         putstr(datawin, 0, buf + 1);
798.                     }
799.                     display_nhwindow(datawin, FALSE);
800.                     destroy_nhwindow(datawin), datawin = WIN_ERR;
801.                 }
802.             } else if (user_typed_name && pass == 0 && !pass1found_in_file)
803.                 pline("I don't have any information on those things.");
804.         }
805.     }
806.     goto checkfile_done; /* skip error feedback */
807. 
808.  bad_data_file:
809.     impossible("'data' file in wrong format or corrupted");
810.  checkfile_done:
811.     if (datawin != WIN_ERR)
812.         destroy_nhwindow(datawin);
813.     (void) dlb_fclose(fp);
814.     return;
815. }
816. 
817. int
818. do_screen_description(cc, looked, sym, out_str, firstmatch, for_supplement)
819. coord cc;
820. boolean looked;
821. int sym;
822. char *out_str;
823. const char **firstmatch;
824. struct permonst **for_supplement;
825. {
826.     static const char mon_interior[] = "the interior of a monster",
827.                       unreconnoitered[] = "unreconnoitered";
828.     static char look_buf[BUFSZ];
829.     char prefix[BUFSZ];
830.     int i, alt_i, j, glyph = NO_GLYPH,
831.         skipped_venom = 0, found = 0; /* count of matching syms found */
832.     boolean hit_trap, need_to_look = FALSE,
833.             submerged = (Underwater && !Is_waterlevel(&u.uz));
834.     const char *x_str;
835.     nhsym tmpsym;
836. 
837.     if (looked) {
838.         int oc;
839.         unsigned os;
840. 
841.         glyph = glyph_at(cc.x, cc.y);
842.         /* Convert glyph at selected position to a symbol for use below. */
843.         (void) mapglyph(glyph, &sym, &oc, &os, cc.x, cc.y, 0);
844. 
845.         Sprintf(prefix, "%s        ", encglyph(glyph));
846.     } else
847.         Sprintf(prefix, "%c        ", sym);
848. 
849.     /*
850.      * Check all the possibilities, saving all explanations in a buffer.
851.      * When all have been checked then the string is printed.
852.      */
853. 
854.     /*
855.      * Handle restricted vision range (limited to adjacent spots when
856.      * swallowed or underwater) cases first.
857.      *
858.      * 3.6.0 listed anywhere on map, other than self, as "interior
859.      * of a monster" when swallowed, and non-adjacent water or
860.      * non-water anywhere as "dark part of a room" when underwater.
861.      * "unreconnoitered" is an attempt to convey "even if you knew
862.      * what was there earlier, you don't know what is there in the
863.      * current circumstance".
864.      *
865.      * (Note: 'self' will always be visible when swallowed so we don't
866.      * need special swallow handling for <ux,uy>.
867.      * Another note: for '#terrain' without monsters, u.uswallow and
868.      * submerged will always both be False and skip this code.)
869.      */
870.     x_str = 0;
871.     if (!looked) {
872.         ; /* skip special handling */
873.     } else if (((u.uswallow || submerged) && distu(cc.x, cc.y) > 2)
874.                /* detection showing some category, so mostly background */
875.                || ((iflags.terrainmode & (TER_DETECT | TER_MAP)) == TER_DETECT
876.                    && glyph == cmap_to_glyph(S_stone))) {
877.         x_str = unreconnoitered;
878.         need_to_look = FALSE;
879.     } else if (is_swallow_sym(sym)) {
880.         x_str = mon_interior;
881.         need_to_look = TRUE; /* for specific monster type */
882.     }
883.     if (x_str) {
884.         /* we know 'found' is zero here, but guard against some other
885.            special case being inserted ahead of us someday */
886.         if (!found) {
887.             Sprintf(out_str, "%s%s", prefix, x_str);
888.             *firstmatch = x_str;
889.             found++;
890.         } else {
891.             found += append_str(out_str, x_str); /* not 'an(x_str)' */
892.         }
893.         /* for is_swallow_sym(), we want to list the current symbol's
894.            other possibilities (wand for '/', throne for '\\', &c) so
895.            don't jump to the end for the x_str==mon_interior case */
896.         if (x_str == unreconnoitered)
897.             goto didlook;
898.     }
899.  check_monsters:
900.     /* Check for monsters */
901.     if (!iflags.terrainmode || (iflags.terrainmode & TER_MON) != 0) {
902.         for (i = 1; i < MAXMCLASSES; i++) {
903.             if (sym == (looked ? showsyms[i + SYM_OFF_M] : def_monsyms[i].sym)
904.                 && def_monsyms[i].explain && *def_monsyms[i].explain) {
905.                 need_to_look = TRUE;
906.                 if (!found) {
907.                     Sprintf(out_str, "%s%s",
908.                             prefix, an(def_monsyms[i].explain));
909.                     *firstmatch = def_monsyms[i].explain;
910.                     found++;
911.                 } else {
912.                     found += append_str(out_str, an(def_monsyms[i].explain));
913.                 }
914.             }
915.         }
916.         /* handle '@' as a special case if it refers to you and you're
917.            playing a character which isn't normally displayed by that
918.            symbol; firstmatch is assumed to already be set for '@' */
919.         if ((looked ? (sym == showsyms[S_HUMAN + SYM_OFF_M]
920.                        && cc.x == u.ux && cc.y == u.uy)
921.                     : (sym == def_monsyms[S_HUMAN].sym && !flags.showrace))
922.             && !(Race_if(PM_HUMAN) || Race_if(PM_ELF)) && !Upolyd)
923.             found += append_str(out_str, "you"); /* tack on "or you" */
924.     }
925. 
926.     /* Now check for objects */
927.     if (!iflags.terrainmode || (iflags.terrainmode & TER_OBJ) != 0) {
928.         for (i = 1; i < MAXOCLASSES; i++) {
929.             if (sym == (looked ? showsyms[i + SYM_OFF_O]
930.                                : def_oc_syms[i].sym)
931.                 || (looked && i == ROCK_CLASS && glyph_is_statue(glyph))) {
932.                 need_to_look = TRUE;
933.                 if (looked && i == VENOM_CLASS) {
934.                     skipped_venom++;
935.                     continue;
936.                 }
937.                 if (!found) {
938.                     Sprintf(out_str, "%s%s",
939.                             prefix, an(def_oc_syms[i].explain));
940.                     *firstmatch = def_oc_syms[i].explain;
941.                     found++;
942.                 } else {
943.                     found += append_str(out_str, an(def_oc_syms[i].explain));
944.                 }
945.             }
946.         }
947.     }
948. 
949.     if (sym == DEF_INVISIBLE) {
950.         extern const char altinvisexplain[]; /* drawing.c */
951.         /* for active clairvoyance, use alternate "unseen creature" */
952.         boolean usealt = (EDetect_monsters & I_SPECIAL) != 0L;
953.         const char *unseen_explain = !usealt ? invisexplain : altinvisexplain;
954. 
955.         if (!found) {
956.             Sprintf(out_str, "%s%s", prefix, an(unseen_explain));
957.             *firstmatch = unseen_explain;
958.             found++;
959.         } else {
960.             found += append_str(out_str, an(unseen_explain));
961.         }
962.     }
963. 
964.     /* Now check for graphics symbols */
965.     alt_i = (sym == (looked ? showsyms[0] : defsyms[0].sym)) ? 0 : (2 + 1);
966.     for (hit_trap = FALSE, i = 0; i < MAXPCHARS; i++) {
967.         /* when sym is the default background character, we process
968.            i == 0 three times: unexplored, stone, dark part of a room */
969.         if (alt_i < 2) {
970.             x_str = !alt_i++ ? "unexplored" : submerged ? "unknown" : "stone";
971.             i = 0; /* for second iteration, undo loop increment */
972.             /* alt_i is now 1 or 2 */
973.         } else {
974.             if (alt_i++ == 2)
975.                 i = 0; /* undo loop increment */
976.             x_str = defsyms[i].explanation;
977.             if (submerged && !strcmp(x_str, defsyms[0].explanation))
978.                 x_str = "land"; /* replace "dark part of a room" */
979.             /* alt_i is now 3 or more and no longer of interest */
980.         }
981.         if (sym == (looked ? showsyms[i] : defsyms[i].sym) && *x_str) {
982.             /* avoid "an unexplored", "an stone", "an air", "a water",
983.                "a floor of a room", "a dark part of a room";
984.                article==2 => "the", 1 => "an", 0 => (none) */
985.             int article = strstri(x_str, " of a room") ? 2
986.                           : !(alt_i <= 2
987.                               || strcmp(x_str, "air") == 0
988.                               || strcmp(x_str, "land") == 0
989.                               || strcmp(x_str, "water") == 0);
990. 
991.             if (!found) {
992.                 if (is_cmap_trap(i)) {
993.                     Sprintf(out_str, "%sa trap", prefix);
994.                     hit_trap = TRUE;
995.                 } else {
996.                     Sprintf(out_str, "%s%s", prefix,
997.                             article == 2 ? the(x_str)
998.                             : article == 1 ? an(x_str) : x_str);
999.                 }
1000.                 *firstmatch = x_str;
1001.                 found++;
1002.             } else if (!(hit_trap && is_cmap_trap(i))
1003.                        && !(found >= 3 && is_cmap_drawbridge(i))
1004.                        /* don't mention vibrating square outside of Gehennom
1005.                           unless this happens to be one (hallucination?) */
1006.                        && (i != S_vibrating_square || Inhell
1007.                            || (looked && glyph_is_trap(glyph)
1008.                                && glyph_to_trap(glyph) == VIBRATING_SQUARE))) {
1009.                 found += append_str(out_str, (article == 2) ? the(x_str)
1010.                                              : (article == 1) ? an(x_str)
1011.                                                : x_str);
1012.                 if (is_cmap_trap(i))
1013.                     hit_trap = TRUE;
1014.             }
1015. 
1016.             if (i == S_altar || is_cmap_trap(i))
1017.                 need_to_look = TRUE;
1018.         }
1019.     }
1020. 
1021.     /* Now check for warning symbols */
1022.     for (i = 1; i < WARNCOUNT; i++) {
1023.         x_str = def_warnsyms[i].explanation;
1024.         if (sym == (looked ? warnsyms[i] : def_warnsyms[i].sym)) {
1025.             if (!found) {
1026.                 Sprintf(out_str, "%s%s", prefix, def_warnsyms[i].explanation);
1027.                 *firstmatch = def_warnsyms[i].explanation;
1028.                 found++;
1029.             } else {
1030.                 found += append_str(out_str, def_warnsyms[i].explanation);
1031.             }
1032.             /* Kludge: warning trumps boulders on the display.
1033.                Reveal the boulder too or player can get confused */
1034.             if (looked && sobj_at(BOULDER, cc.x, cc.y))
1035.                 Strcat(out_str, " co-located with a boulder");
1036.             break; /* out of for loop*/
1037.         }
1038.     }
1039. 
1040.     /* if we ignored venom and list turned out to be short, put it back */
1041.     if (skipped_venom && found < 2) {
1042.         x_str = def_oc_syms[VENOM_CLASS].explain;
1043.         if (!found) {
1044.             Sprintf(out_str, "%s%s", prefix, an(x_str));
1045.             *firstmatch = x_str;
1046.             found++;
1047.         } else {
1048.             found += append_str(out_str, an(x_str));
1049.         }
1050.     }
1051. 
1052.     /* Finally, handle some optional overriding symbols */
1053.     for (j = SYM_OFF_X; j < SYM_MAX; ++j) {
1054.         if (j == (SYM_INVISIBLE + SYM_OFF_X))
1055.             continue;       /* already handled above */
1056.         tmpsym = Is_rogue_level(&u.uz) ? ov_rogue_syms[j]
1057.                                        : ov_primary_syms[j];
1058.         if (tmpsym && sym == tmpsym) {
1059.             switch (j) {
1060.             case SYM_BOULDER + SYM_OFF_X:
1061.                 if (!found) {
1062.                     *firstmatch = "boulder";
1063.                     Sprintf(out_str, "%s%s", prefix, an(*firstmatch));
1064.                     found++;
1065.                 } else {
1066.                     found += append_str(out_str, "boulder");
1067.                 }
1068.                 break;
1069.             case SYM_PET_OVERRIDE + SYM_OFF_X:
1070.                 if (looked) {
1071.                     int oc = 0;
1072.                     unsigned os = 0;
1073. 
1074.                     /* convert to symbol without override in effect */
1075.                     (void) mapglyph(glyph, &sym, &oc, &os,
1076.                                     cc.x, cc.y, MG_FLAG_NOOVERRIDE);
1077.                     goto check_monsters;
1078.                 }
1079.                 break;
1080.             case SYM_HERO_OVERRIDE + SYM_OFF_X:
1081.                 sym = showsyms[S_HUMAN + SYM_OFF_M];
1082.                 goto check_monsters;
1083.             }
1084.         }
1085.     }
1086. #if 0
1087.     /* handle optional boulder symbol as a special case */
1088.     if (o_syms[SYM_BOULDER + SYM_OFF_X]
1089.         && sym == o_syms[SYM_BOULDER + SYM_OFF_X]) {
1090.         if (!found) {
1091.             *firstmatch = "boulder";
1092.             Sprintf(out_str, "%s%s", prefix, an(*firstmatch));
1093.             found++;
1094.         } else {
1095.             found += append_str(out_str, "boulder");
1096.         }
1097.     }
1098. #endif
1099. 
1100.     /*
1101.      * If we are looking at the screen, follow multiple possibilities or
1102.      * an ambiguous explanation by something more detailed.
1103.      */
1104. 
1105.     if (found > 4)
1106.         /* 3.6.3: this used to be "That can be many things" (without prefix)
1107.            which turned it into a sentence that lacked its terminating period;
1108.            we could add one below but reinstating the prefix here is better */
1109.         Sprintf(out_str, "%scan be many things", prefix);
1110. 
1111.  didlook:
1112.     if (looked) {
1113.         struct permonst *pm = (struct permonst *)0;
1114. 
1115.         if (found > 1 || need_to_look) {
1116.             char monbuf[BUFSZ];
1117.             char temp_buf[BUFSZ];
1118. 
1119.             pm = lookat(cc.x, cc.y, look_buf, monbuf);
1120.             if (pm && for_supplement)
1121.                 *for_supplement = pm;
1122.             *firstmatch = look_buf;
1123.             if (*(*firstmatch)) {
1124.                 Sprintf(temp_buf, " (%s)", *firstmatch);
1125.                 (void) strncat(out_str, temp_buf,
1126.                                BUFSZ - strlen(out_str) - 1);
1127.                 found = 1; /* we have something to look up */
1128.             }
1129.             if (monbuf[0]) {
1130.                 Sprintf(temp_buf, " [seen: %s]", monbuf);
1131.                 (void) strncat(out_str, temp_buf,
1132.                                BUFSZ - strlen(out_str) - 1);
1133.             }
1134.         }
1135.     }
1136. 
1137.     return found;
1138. }
1139. 
1140. /* also used by getpos hack in do_name.c */
1141. const char what_is_an_unknown_object[] = "an unknown object";
1142. 
1143. int
1144. do_look(mode, click_cc)
1145. int mode;
1146. coord *click_cc;
1147. {
1148.     boolean quick = (mode == 1); /* use cursor; don't search for "more info" */
1149.     boolean clicklook = (mode == 2); /* right mouse-click method */
1150.     char out_str[BUFSZ] = DUMMY;
1151.     const char *firstmatch = 0;
1152.     struct permonst *pm = 0, *supplemental_pm = 0;
1153.     int i = '\0', ans = 0;
1154.     int sym;              /* typed symbol or converted glyph */
1155.     int found;            /* count of matching syms found */
1156.     coord cc;             /* screen pos of unknown glyph */
1157.     boolean save_verbose; /* saved value of flags.verbose */
1158.     boolean from_screen;  /* question from the screen */
1159. 
1160.     cc.x = 0;
1161.     cc.y = 0;
1162. 
1163.     if (!clicklook) {
1164.         if (quick) {
1165.             from_screen = TRUE; /* yes, we want to use the cursor */
1166.             i = 'y';
1167.         } else {
1168.             menu_item *pick_list = (menu_item *) 0;
1169.             winid win;
1170.             anything any;
1171. 
1172.             any = zeroany;
1173.             win = create_nhwindow(NHW_MENU);
1174.             start_menu(win);
1175.             any.a_char = '/';
1176.             /* 'y' and 'n' to keep backwards compatibility with previous
1177.                versions: "Specify unknown object by cursor?" */
1178.             add_menu(win, NO_GLYPH, &any,
1179.                      flags.lootabc ? 0 : any.a_char, 'y', ATR_NONE,
1180.                      "something on the map", MENU_UNSELECTED);
1181.             any.a_char = 'i';
1182.             add_menu(win, NO_GLYPH, &any,
1183.                      flags.lootabc ? 0 : any.a_char, 0, ATR_NONE,
1184.                      "something you're carrying", MENU_UNSELECTED);
1185.             any.a_char = '?';
1186.             add_menu(win, NO_GLYPH, &any,
1187.                      flags.lootabc ? 0 : any.a_char, 'n', ATR_NONE,
1188.                      "something else (by symbol or name)", MENU_UNSELECTED);
1189.             if (!u.uswallow && !Hallucination) {
1190.                 any = zeroany;
1191.                 add_menu(win, NO_GLYPH, &any, 0, 0, ATR_NONE,
1192.                          "", MENU_UNSELECTED);
1193.                 /* these options work sensibly for the swallowed case,
1194.                    but there's no reason for the player to use them then;
1195.                    objects work fine when hallucinating, but screen
1196.                    symbol/monster class letter doesn't match up with
1197.                    bogus monster type, so suppress when hallucinating */
1198.                 any.a_char = 'm';
1199.                 add_menu(win, NO_GLYPH, &any,
1200.                          flags.lootabc ? 0 : any.a_char, 0, ATR_NONE,
1201.                          "nearby monsters", MENU_UNSELECTED);
1202.                 any.a_char = 'M';
1203.                 add_menu(win, NO_GLYPH, &any,
1204.                          flags.lootabc ? 0 : any.a_char, 0, ATR_NONE,
1205.                          "all monsters shown on map", MENU_UNSELECTED);
1206.                 any.a_char = 'o';
1207.                 add_menu(win, NO_GLYPH, &any,
1208.                          flags.lootabc ? 0 : any.a_char, 0, ATR_NONE,
1209.                          "nearby objects", MENU_UNSELECTED);
1210.                 any.a_char = 'O';
1211.                 add_menu(win, NO_GLYPH, &any,
1212.                          flags.lootabc ? 0 : any.a_char, 0, ATR_NONE,
1213.                          "all objects shown on map", MENU_UNSELECTED);
1214.             }
1215.             end_menu(win, "What do you want to look at:");
1216.             if (select_menu(win, PICK_ONE, &pick_list) > 0) {
1217.                 i = pick_list->item.a_char;
1218.                 free((genericptr_t) pick_list);
1219.             }
1220.             destroy_nhwindow(win);
1221.         }
1222. 
1223.         switch (i) {
1224.         default:
1225.         case 'q':
1226.             return 0;
1227.         case 'y':
1228.         case '/':
1229.             from_screen = TRUE;
1230.             sym = 0;
1231.             cc.x = u.ux;
1232.             cc.y = u.uy;
1233.             break;
1234.         case 'i':
1235.           {
1236.             char invlet;
1237.             struct obj *invobj;
1238. 
1239.             invlet = display_inventory((const char *) 0, TRUE);
1240.             if (!invlet || invlet == '\033')
1241.                 return 0;
1242.             *out_str = '\0';
1243.             for (invobj = invent; invobj; invobj = invobj->nobj)
1244.                 if (invobj->invlet == invlet) {
1245.                     strcpy(out_str, singular(invobj, xname));
1246.                     break;
1247.                 }
1248.             if (*out_str)
1249.                 checkfile(out_str, pm, TRUE, TRUE, (char *) 0);
1250.             return 0;
1251.           }
1252.         case '?':
1253.             from_screen = FALSE;
1254.             getlin("Specify what? (type the word)", out_str);
1255.             if (strcmp(out_str, " ")) /* keep single space as-is */
1256.                 /* remove leading and trailing whitespace and
1257.                    condense consecutive internal whitespace */
1258.                 mungspaces(out_str);
1259.             if (out_str[0] == '\0' || out_str[0] == '\033')
1260.                 return 0;
1261. 
1262.             if (out_str[1]) { /* user typed in a complete string */
1263.                 checkfile(out_str, pm, TRUE, TRUE, (char *) 0);
1264.                 return 0;
1265.             }
1266.             sym = out_str[0];
1267.             break;
1268.         case 'm':
1269.             look_all(TRUE, TRUE); /* list nearby monsters */
1270.             return 0;
1271.         case 'M':
1272.             look_all(FALSE, TRUE); /* list all monsters */
1273.             return 0;
1274.         case 'o':
1275.             look_all(TRUE, FALSE); /* list nearby objects */
1276.             return 0;
1277.         case 'O':
1278.             look_all(FALSE, FALSE); /* list all objects */
1279.             return 0;
1280.         }
1281.     } else { /* clicklook */
1282.         cc.x = click_cc->x;
1283.         cc.y = click_cc->y;
1284.         sym = 0;
1285.         from_screen = FALSE;
1286.     }
1287. 
1288.     /* Save the verbose flag, we change it later. */
1289.     save_verbose = flags.verbose;
1290.     flags.verbose = flags.verbose && !quick;
1291.     /*
1292.      * The user typed one letter, or we're identifying from the screen.
1293.      */
1294.     do {
1295.         /* Reset some variables. */
1296.         pm = (struct permonst *) 0;
1297.         found = 0;
1298.         out_str[0] = '\0';
1299. 
1300.         if (from_screen || clicklook) {
1301.             if (from_screen) {
1302.                 if (flags.verbose)
1303.                     pline("Please move the cursor to %s.",
1304.                           what_is_an_unknown_object);
1305.                 else
1306.                     pline("Pick an object.");
1307. 
1308.                 ans = getpos(&cc, quick, what_is_an_unknown_object);
1309.                 if (ans < 0 || cc.x < 0)
1310.                     break; /* done */
1311.                 flags.verbose = FALSE; /* only print long question once */
1312.             }
1313.         }
1314. 
1315.         found = do_screen_description(cc, (from_screen || clicklook), sym,
1316.                                       out_str, &firstmatch, &supplemental_pm);
1317. 
1318.         /* Finally, print out our explanation. */
1319.         if (found) {
1320.             /* use putmixed() because there may be an encoded glyph present */
1321.             putmixed(WIN_MESSAGE, 0, out_str);
1322. #ifdef DUMPLOG
1323.             {
1324.                 char dmpbuf[BUFSZ];
1325. 
1326.                 /* putmixed() bypasses pline() so doesn't write to DUMPLOG;
1327.                    tty puts it into ^P recall, so it ought to be there;
1328.                    DUMPLOG is plain text, so override graphics character;
1329.                    at present, force space, but we ought to use defsyms[]
1330.                    value for the glyph the graphics character came from */
1331.                 (void) decode_mixed(dmpbuf, out_str);
1332.                 if (dmpbuf[0] < ' ' || dmpbuf[0] >= 127) /* ASCII isprint() */
1333.                     dmpbuf[0] = ' ';
1334.                 dumplogmsg(dmpbuf);
1335.             }
1336. #endif
1337. 
1338.             /* check the data file for information about this thing */
1339.             if (found == 1 && ans != LOOK_QUICK && ans != LOOK_ONCE
1340.                 && (ans == LOOK_VERBOSE || (flags.help && !quick))
1341.                 && !clicklook) {
1342.                 char temp_buf[BUFSZ], supplemental_name[BUFSZ];
1343. 
1344.                 supplemental_name[0] = '\0';
1345.                 Strcpy(temp_buf, firstmatch);
1346.                 checkfile(temp_buf, pm, FALSE,
1347.                           (boolean) (ans == LOOK_VERBOSE), supplemental_name);
1348.                 if (supplemental_pm)
1349.                     do_supplemental_info(supplemental_name, supplemental_pm,
1350.                                          (boolean) (ans == LOOK_VERBOSE));
1351.             }
1352.         } else {
1353.             pline("I've never heard of such things.");
1354.         }
1355.     } while (from_screen && !quick && ans != LOOK_ONCE && !clicklook);
1356. 
1357.     flags.verbose = save_verbose;
1358.     return 0;
1359. }
1360. 
1361. STATIC_OVL void
1362. look_all(nearby, do_mons)
1363. boolean nearby; /* True => within BOLTLIM, False => entire map */
1364. boolean do_mons; /* True => monsters, False => objects */
1365. {
1366.     winid win;
1367.     int x, y, lo_x, lo_y, hi_x, hi_y, glyph, count = 0;
1368.     char lookbuf[BUFSZ], outbuf[BUFSZ];
1369. 
1370.     win = create_nhwindow(NHW_TEXT);
1371.     lo_y = nearby ? max(u.uy - BOLT_LIM, 0) : 0;
1372.     lo_x = nearby ? max(u.ux - BOLT_LIM, 1) : 1;
1373.     hi_y = nearby ? min(u.uy + BOLT_LIM, ROWNO - 1) : ROWNO - 1;
1374.     hi_x = nearby ? min(u.ux + BOLT_LIM, COLNO - 1) : COLNO - 1;
1375.     for (y = lo_y; y <= hi_y; y++) {
1376.         for (x = lo_x; x <= hi_x; x++) {
1377.             lookbuf[0] = '\0';
1378.             glyph = glyph_at(x, y);
1379.             if (do_mons) {
1380.                 if (glyph_is_monster(glyph)) {
1381.                     struct monst *mtmp;
1382. 
1383.                     bhitpos.x = x; /* [is this actually necessary?] */
1384.                     bhitpos.y = y;
1385.                     if (x == u.ux && y == u.uy && canspotself()) {
1386.                         (void) self_lookat(lookbuf);
1387.                         ++count;
1388.                     } else if ((mtmp = m_at(x, y)) != 0) {
1389.                         look_at_monster(lookbuf, (char *) 0, mtmp, x, y);
1390.                         ++count;
1391.                     }
1392.                 } else if (glyph_is_invisible(glyph)) {
1393.                     /* remembered, unseen, creature */
1394.                     Strcpy(lookbuf, invisexplain);
1395.                     ++count;
1396.                 } else if (glyph_is_warning(glyph)) {
1397.                     int warnindx = glyph_to_warning(glyph);
1398. 
1399.                     Strcpy(lookbuf, def_warnsyms[warnindx].explanation);
1400.                     ++count;
1401.                 }
1402.             } else { /* !do_mons */
1403.                 if (glyph_is_object(glyph)) {
1404.                     look_at_object(lookbuf, x, y, glyph);
1405.                     ++count;
1406.                 }
1407.             }
1408.             if (*lookbuf) {
1409.                 char coordbuf[20], which[12], cmode;
1410. 
1411.                 cmode = (iflags.getpos_coords != GPCOORDS_NONE)
1412.                            ? iflags.getpos_coords : GPCOORDS_MAP;
1413.                 if (count == 1) {
1414.                     Strcpy(which, do_mons ? "monsters" : "objects");
1415.                     if (nearby)
1416.                         Sprintf(outbuf, "%s currently shown near %s:",
1417.                                 upstart(which),
1418.                                 (cmode != GPCOORDS_COMPASS)
1419.                                   ? coord_desc(u.ux, u.uy, coordbuf, cmode)
1420.                                   : !canspotself() ? "your position" : "you");
1421.                     else
1422.                         Sprintf(outbuf, "All %s currently shown on the map:",
1423.                                 which);
1424.                     putstr(win, 0, outbuf);
1425.                     putstr(win, 0, "");
1426.                 }
1427.                 /* prefix: "coords  C  " where 'C' is mon or obj symbol */
1428.                 Sprintf(outbuf, (cmode == GPCOORDS_SCREEN) ? "%s  "
1429.                                   : (cmode == GPCOORDS_MAP) ? "%8s  "
1430.                                       : "%12s  ",
1431.                         coord_desc(x, y, coordbuf, cmode));
1432.                 Sprintf(eos(outbuf), "%s  ", encglyph(glyph));
1433.                 /* guard against potential overflow */
1434.                 lookbuf[sizeof lookbuf - 1 - strlen(outbuf)] = '\0';
1435.                 Strcat(outbuf, lookbuf);
1436.                 putmixed(win, 0, outbuf);
1437.             }
1438.         }
1439.     }
1440.     if (count)
1441.         display_nhwindow(win, TRUE);
1442.     else
1443.         pline("No %s are currently shown %s.",
1444.               do_mons ? "monsters" : "objects",
1445.               nearby ? "nearby" : "on the map");
1446.     destroy_nhwindow(win);
1447. }
1448. 
1449. static const char *suptext1[] = {
1450.     "%s is a member of a marauding horde of orcs",
1451.     "rumored to have brutally attacked and plundered",
1452.     "the ordinarily sheltered town that is located ",
1453.     "deep within The Gnomish Mines.",
1454.     "",
1455.     "The members of that vicious horde proudly and ",
1456.     "defiantly acclaim their allegiance to their",
1457.     "leader %s in their names.",
1458.     (char *) 0,
1459. };
1460. 
1461. static const char *suptext2[] = {
1462.     "\"%s\" is the common dungeon name of",
1463.     "a nefarious orc who is known to acquire property",
1464.     "from thieves and sell it off for profit.",
1465.     "",
1466.     "The perpetrator was last seen hanging around the",
1467.     "stairs leading to the Gnomish Mines.",
1468.     (char *) 0,
1469. };
1470. 
1471. STATIC_OVL void
1472. do_supplemental_info(name, pm, without_asking)
1473. char *name;
1474. struct permonst *pm;
1475. boolean without_asking;
1476. {
1477.     const char **textp;
1478.     winid datawin = WIN_ERR;
1479.     char *entrytext = name, *bp = (char *) 0, *bp2 = (char *) 0;
1480.     char question[QBUFSZ];
1481.     boolean yes_to_moreinfo = FALSE;
1482.     boolean is_marauder = (name && pm && is_orc(pm));
1483. 
1484.     /*
1485.      * Provide some info on some specific things
1486.      * meant to support in-game mythology, and not
1487.      * available from data.base or other sources.
1488.      */
1489.     if (is_marauder && (strlen(name) < (BUFSZ - 1))) {
1490.         char fullname[BUFSZ];
1491. 
1492.         bp = strstri(name, " of ");
1493.         bp2 = strstri(name, " the Fence");
1494. 
1495.         if (bp || bp2) {
1496.             Strcpy(fullname, name);
1497.             if (!without_asking) {
1498.                 Strcpy(question, "More info about \"");
1499.                 /* +2 => length of "\"?" */
1500.                 copynchars(eos(question), entrytext,
1501.                     (int) (sizeof question - 1 - (strlen(question) + 2)));
1502.                 Strcat(question, "\"?");
1503.                 if (yn(question) == 'y')
1504.                 yes_to_moreinfo = TRUE;
1505.             }
1506.             if (yes_to_moreinfo) {
1507.                 int i, subs = 0;
1508.                 const char *gang = (char *) 0;
1509. 
1510.                 if (bp) {
1511.                     textp = suptext1;
1512.                     gang = bp + 4;
1513.                     *bp = '\0';
1514.                 } else {
1515.                     textp = suptext2;
1516.                     gang = "";
1517.                 }
1518.                 datawin = create_nhwindow(NHW_MENU);
1519.                 for (i = 0; textp[i]; i++) {
1520.                     char buf[BUFSZ];
1521.                     const char *txt;
1522. 
1523.                     if (strstri(textp[i], "%s") != 0) {
1524.                         Sprintf(buf, textp[i], subs++ ? gang : fullname);
1525.                         txt = buf;
1526.                     } else
1527.                         txt = textp[i];
1528.                     putstr(datawin, 0, txt);
1529.                 }
1530.                 display_nhwindow(datawin, FALSE);
1531.                 destroy_nhwindow(datawin), datawin = WIN_ERR;
1532.             }
1533.         }
1534.     }
1535. }
1536. 
1537. /* the '/' command */
1538. int
1539. dowhatis()
1540. {
1541.     return do_look(0, (coord *) 0);
1542. }
1543. 
1544. /* the ';' command */
1545. int
1546. doquickwhatis()
1547. {
1548.     return do_look(1, (coord *) 0);
1549. }
1550. 
1551. /* the '^' command */
1552. int
1553. doidtrap()
1554. {
1555.     register struct trap *trap;
1556.     int x, y, tt, glyph;
1557. 
1558.     if (!getdir("^"))
1559.         return 0;
1560.     x = u.ux + u.dx;
1561.     y = u.uy + u.dy;
1562. 
1563.     /* check fake bear trap from confused gold detection */
1564.     glyph = glyph_at(x, y);
1565.     if (glyph_is_trap(glyph) && (tt = glyph_to_trap(glyph)) == BEAR_TRAP) {
1566.         boolean chesttrap = trapped_chest_at(tt, x, y);
1567. 
1568.         if (chesttrap || trapped_door_at(tt, x, y)) {
1569.             pline("That is a trapped %s.", chesttrap ? "chest" : "door");
1570.             return 0; /* trap ID'd, but no time elapses */
1571.         }
1572.     }
1573. 
1574.     for (trap = ftrap; trap; trap = trap->ntrap)
1575.         if (trap->tx == x && trap->ty == y) {
1576.             if (!trap->tseen)
1577.                 break;
1578.             tt = trap->ttyp;
1579.             if (u.dz) {
1580.                 if (u.dz < 0 ? is_hole(tt) : tt == ROCKTRAP)
1581.                     break;
1582.             }
1583.             tt = what_trap(tt, rn2_on_display_rng);
1584.             pline("That is %s%s%s.",
1585.                   an(defsyms[trap_to_defsym(tt)].explanation),
1586.                   !trap->madeby_u
1587.                      ? ""
1588.                      : (tt == WEB)
1589.                         ? " woven"
1590.                         /* trap doors & spiked pits can't be made by
1591.                            player, and should be considered at least
1592.                            as much "set" as "dug" anyway */
1593.                         : (tt == HOLE || tt == PIT)
1594.                            ? " dug"
1595.                            : " set",
1596.                   !trap->madeby_u ? "" : " by you");
1597.             return 0;
1598.         }
1599.     pline("I can't see a trap there.");
1600.     return 0;
1601. }
1602. 
1603. /*
1604.     Implements a rudimentary if/elif/else/endif interpretor and use
1605.     conditionals in dat/cmdhelp to describe what command each keystroke
1606.     currently invokes, so that there isn't a lot of "(debug mode only)"
1607.     and "(if number_pad is off)" cluttering the feedback that the user
1608.     sees.  (The conditionals add quite a bit of clutter to the raw data
1609.     but users don't see that.  number_pad produces a lot of conditional
1610.     commands:  basic letters vs digits, 'g' vs 'G' for '5', phone
1611.     keypad vs normal layout of digits, and QWERTZ keyboard swap between
1612.     y/Y/^Y/M-y/M-Y/M-^Y and z/Z/^Z/M-z/M-Z/M-^Z.)
1613. 
1614.     The interpretor understands
1615.      '&#' for comment,
1616.      '&? option' for 'if' (also '&? !option'
1617.                            or '&? option=value[,value2,...]'
1618.                            or '&? !option=value[,value2,...]'),
1619.      '&: option' for 'elif' (with argument variations same as 'if';
1620.                              any number of instances for each 'if'),
1621.      '&:' for 'else' (also '&: #comment';
1622.                       0 or 1 instance for a given 'if'), and
1623.      '&.' for 'endif' (also '&. #comment'; required for each 'if').
1624. 
1625.     The option handling is a bit of a mess, with no generality for
1626.     which options to deal with and only a comma separated list of
1627.     integer values for the '=value' part.  number_pad is the only
1628.     supported option that has a value; the few others (wizard/debug,
1629.     rest_on_space, #if SHELL, #if SUSPEND) are booleans.
1630. */
1631. 
1632. STATIC_DCL void
1633. whatdoes_help()
1634. {
1635.     dlb *fp;
1636.     char *p, buf[BUFSZ];
1637.     winid tmpwin;
1638. 
1639.     fp = dlb_fopen(KEYHELP, "r");
1640.     if (!fp) {
1641.         pline("Cannot open \"%s\" data file!", KEYHELP);
1642.         display_nhwindow(WIN_MESSAGE, TRUE);
1643.         return;
1644.     }
1645.     tmpwin = create_nhwindow(NHW_TEXT);
1646.     while (dlb_fgets(buf, (int) sizeof buf, fp)) {
1647.         if (*buf == '#')
1648.             continue;
1649.         for (p = buf; *p; p++)
1650.             if (*p != ' ' && *p != '\t')
1651.                 break;
1652.         putstr(tmpwin, 0, p);
1653.     }
1654.     (void) dlb_fclose(fp);
1655.     display_nhwindow(tmpwin, TRUE);
1656.     destroy_nhwindow(tmpwin);
1657. }
1658. 
1659. #if 0
1660. #define WD_STACKLIMIT 5
1661. struct wd_stack_frame {
1662.     Bitfield(active, 1);
1663.     Bitfield(been_true, 1);
1664.     Bitfield(else_seen, 1);
1665. };
1666. 
1667. STATIC_DCL boolean FDECL(whatdoes_cond, (char *, struct wd_stack_frame *,
1668.                                          int *, int));
1669. 
1670. STATIC_OVL boolean
1671. whatdoes_cond(buf, stack, depth, lnum)
1672. char *buf;
1673. struct wd_stack_frame *stack;
1674. int *depth, lnum;
1675. {
1676.     const char badstackfmt[] = "cmdhlp: too many &%c directives at line %d.";
1677.     boolean newcond, neg, gotopt;
1678.     char *p, *q, act = buf[1];
1679.     int np = 0;
1680. 
1681.     newcond = (act == '?' || !stack[*depth].been_true);
1682.     buf += 2;
1683.     mungspaces(buf);
1684.     if (act == '#' || *buf == '#' || !*buf || !newcond) {
1685.         gotopt = (*buf && *buf != '#');
1686.         *buf = '\0';
1687.         neg = FALSE; /* lint suppression */
1688.         p = q = (char *) 0;
1689.     } else {
1690.         gotopt = TRUE;
1691.         if ((neg = (*buf == '!')) != 0)
1692.             if (*++buf == ' ')
1693.                 ++buf;
1694.         p = index(buf, '='), q = index(buf, ':');
1695.         if (!p || (q && q < p))
1696.             p = q;
1697.         if (p) { /* we have a value specified */
1698.             /* handle a space before or after (or both) '=' (or ':') */
1699.             if (p > buf && p[-1] == ' ')
1700.                 p[-1] = '\0'; /* end of keyword in buf[] */
1701.             *p++ = '\0'; /* terminate keyword, advance to start of value */
1702.             if (*p == ' ')
1703.                 p++;
1704.         }
1705.     }
1706.     if (*buf && (act == '?' || act == ':')) {
1707.         if (!strcmpi(buf, "number_pad")) {
1708.             if (!p) {
1709.                 newcond = iflags.num_pad;
1710.             } else {
1711.                 /* convert internal encoding (separate yes/no and 0..3)
1712.                    back to user-visible one (-1..4) */
1713.                 np = iflags.num_pad ? (1 + iflags.num_pad_mode) /* 1..4 */
1714.                                     : (-1 * iflags.num_pad_mode); /* -1..0 */
1715.                 newcond = FALSE;
1716.                 for (; p; p = q) {
1717.                     q = index(p, ',');
1718.                     if (q)
1719.                         *q++ = '\0';
1720.                     if (atoi(p) == np) {
1721.                         newcond = TRUE;
1722.                         break;
1723.                     }
1724.                 }
1725.             }
1726.         } else if (!strcmpi(buf, "rest_on_space")) {
1727.             newcond = flags.rest_on_space;
1728.         } else if (!strcmpi(buf, "debug") || !strcmpi(buf, "wizard")) {
1729.             newcond = flags.debug; /* == wizard */
1730.         } else if (!strcmpi(buf, "shell")) {
1731. #ifdef SHELL
1732.             /* should we also check sysopt.shellers? */
1733.             newcond = TRUE;
1734. #else
1735.             newcond = FALSE;
1736. #endif
1737.         } else if (!strcmpi(buf, "suspend")) {
1738. #ifdef SUSPEND
1739.             /* sysopt.shellers is also used for dosuspend()... */
1740.             newcond = TRUE;
1741. #else
1742.             newcond = FALSE;
1743. #endif
1744.         } else {
1745.             impossible(
1746.                 "cmdhelp: unrecognized &%c conditional at line %d: \"%.20s\"",
1747.                        act, lnum, buf);
1748.             neg = FALSE;
1749.         }
1750.         /* this works for number_pad too: &? !number_pad:-1,0
1751.            would be true for 1..4 after negation */
1752.         if (neg)
1753.             newcond = !newcond;
1754.     }
1755.     switch (act) {
1756.     default:
1757.     case '#': /* comment */
1758.         break;
1759.     case '.': /* endif */
1760.         if (--*depth < 0) {
1761.             impossible(badstackfmt, '.', lnum);
1762.             *depth = 0;
1763.         }
1764.         break;
1765.     case ':': /* else or elif */
1766.         if (*depth == 0 || stack[*depth].else_seen) {
1767.             impossible(badstackfmt, ':', lnum);
1768.             *depth = 1; /* so that stack[*depth - 1] is a valid access */
1769.         }
1770.         if (stack[*depth].active || stack[*depth].been_true
1771.             || !stack[*depth - 1].active)
1772.             stack[*depth].active = 0;
1773.         else if (newcond)
1774.             stack[*depth].active = stack[*depth].been_true = 1;
1775.         if (!gotopt)
1776.             stack[*depth].else_seen = 1;
1777.         break;
1778.     case '?': /* if */
1779.         if (++*depth >= WD_STACKLIMIT) {
1780.             impossible(badstackfmt, '?', lnum);
1781.             *depth = WD_STACKLIMIT - 1;
1782.         }
1783.         stack[*depth].active = (newcond && stack[*depth - 1].active) ? 1 : 0;
1784.         stack[*depth].been_true = stack[*depth].active;
1785.         stack[*depth].else_seen = 0;
1786.         break;
1787.     }
1788.     return stack[*depth].active ? TRUE : FALSE;
1789. }
1790. #endif /* 0 */
1791. 
1792. char *
1793. dowhatdoes_core(q, cbuf)
1794. char q;
1795. char *cbuf;
1796. {
1797.     char buf[BUFSZ];
1798. #if 0
1799.     dlb *fp;
1800.     struct wd_stack_frame stack[WD_STACKLIMIT];
1801.     boolean cond;
1802.     int ctrl, meta, depth = 0, lnum = 0;
1803. #endif /* 0 */
1804.     const char *ec_desc;
1805. 
1806.     if ((ec_desc = key2extcmddesc(q)) != NULL) {
1807.         char keybuf[QBUFSZ];
1808. 
1809.         Sprintf(buf, "%-8s%s.", key2txt(q, keybuf), ec_desc);
1810.         Strcpy(cbuf, buf);
1811.         return cbuf;
1812.     }
1813.     return 0;
1814. #if 0
1815.     fp = dlb_fopen(CMDHELPFILE, "r");
1816.     if (!fp) {
1817.         pline("Cannot open \"%s\" data file!", CMDHELPFILE);
1818.         return 0;
1819.     }
1820. 
1821.     meta = (0x80 & (uchar) q) != 0;
1822.     if (meta)
1823.         q &= 0x7f;
1824.     ctrl = (0x1f & (uchar) q) == (uchar) q;
1825.     if (ctrl)
1826.         q |= 0x40; /* NUL -> '@', ^A -> 'A', ... ^Z -> 'Z', ^[ -> '[', ... */
1827.     else if (q == 0x7f)
1828.         ctrl = 1, q = '?';
1829. 
1830.     (void) memset((genericptr_t) stack, 0, sizeof stack);
1831.     cond = stack[0].active = 1;
1832.     while (dlb_fgets(buf, sizeof buf, fp)) {
1833.         ++lnum;
1834.         if (buf[0] == '&' && buf[1] && index("?:.#", buf[1])) {
1835.             cond = whatdoes_cond(buf, stack, &depth, lnum);
1836.             continue;
1837.         }
1838.         if (!cond)
1839.             continue;
1840.         if (meta ? (buf[0] == 'M' && buf[1] == '-'
1841.                     && (ctrl ? buf[2] == '^' && highc(buf[3]) == q
1842.                              : buf[2] == q))
1843.                  : (ctrl ? buf[0] == '^' && highc(buf[1]) == q
1844.                          : buf[0] == q)) {
1845.             (void) strip_newline(buf);
1846.             if (index(buf, '\t'))
1847.                 (void) tabexpand(buf);
1848.             if (meta && ctrl && buf[4] == ' ') {
1849.                 (void) strncpy(buf, "M-^?    ", 8);
1850.                 buf[3] = q;
1851.             } else if (meta && buf[3] == ' ') {
1852.                 (void) strncpy(buf, "M-?     ", 8);
1853.                 buf[2] = q;
1854.             } else if (ctrl && buf[2] == ' ') {
1855.                 (void) strncpy(buf, "^?      ", 8);
1856.                 buf[1] = q;
1857.             } else if (buf[1] == ' ') {
1858.                 (void) strncpy(buf, "?       ", 8);
1859.                 buf[0] = q;
1860.             }
1861.             (void) dlb_fclose(fp);
1862.             Strcpy(cbuf, buf);
1863.             return cbuf;
1864.         }
1865.     }
1866.     (void) dlb_fclose(fp);
1867.     if (depth != 0)
1868.         impossible("cmdhelp: mismatched &? &: &. conditionals.");
1869.     return (char *) 0;
1870. #endif /* 0 */
1871. }
1872. 
1873. int
1874. dowhatdoes()
1875. {
1876.     static boolean once = FALSE;
1877.     char bufr[BUFSZ];
1878.     char q, *reslt;
1879. 
1880.     if (!once) {
1881.         pline("Ask about '&' or '?' to get more info.%s",
1882. #ifdef ALTMETA
1883.               iflags.altmeta ? "  (For ESC, type it twice.)" :
1884. #endif
1885.               "");
1886.         once = TRUE;
1887.     }
1888. #if defined(UNIX) || defined(VMS)
1889.     introff(); /* disables ^C but not ^\ */
1890. #endif
1891.     q = yn_function("What command?", (char *) 0, '\0');
1892. #ifdef ALTMETA
1893.     if (q == '\033' && iflags.altmeta) {
1894.         /* in an ideal world, we would know whether another keystroke
1895.            was already pending, but this is not an ideal world...
1896.            if user typed ESC, we'll essentially hang until another
1897.            character is typed */
1898.         q = yn_function("]", (char *) 0, '\0');
1899.         if (q != '\033')
1900.             q = (char) ((uchar) q | 0200);
1901.     }
1902. #endif /*ALTMETA*/
1903. #if defined(UNIX) || defined(VMS)
1904.     intron(); /* reenables ^C */
1905. #endif
1906.     reslt = dowhatdoes_core(q, bufr);
1907.     if (reslt) {
1908.         if (q == '&' || q == '?')
1909.             whatdoes_help();
1910.         pline("%s", reslt);
1911.     } else {
1912.         pline("No such command '%s', char code %d (0%03o or 0x%02x).",
1913.               visctrl(q), (uchar) q, (uchar) q, (uchar) q);
1914.     }
1915.     return 0;
1916. }
1917. 
1918. STATIC_OVL void
1919. docontact(VOID_ARGS)
1920. {
1921.     winid cwin = create_nhwindow(NHW_TEXT);
1922.     char buf[BUFSZ];
1923. 
1924.     if (sysopt.support) {
1925.         /*XXX overflow possibilities*/
1926.         Sprintf(buf, "To contact local support, %s", sysopt.support);
1927.         putstr(cwin, 0, buf);
1928.         putstr(cwin, 0, "");
1929.     } else if (sysopt.fmtd_wizard_list) { /* formatted SYSCF WIZARDS */
1930.         Sprintf(buf, "To contact local support, contact %s.",
1931.                 sysopt.fmtd_wizard_list);
1932.         putstr(cwin, 0, buf);
1933.         putstr(cwin, 0, "");
1934.     }
1935.     putstr(cwin, 0, "To contact the NetHack development team directly,");
1936.     /*XXX overflow possibilities*/
1937.     Sprintf(buf, "see the 'Contact' form on our website or email <%s>.",
1938.             DEVTEAM_EMAIL);
1939.     putstr(cwin, 0, buf);
1940.     putstr(cwin, 0, "");
1941.     putstr(cwin, 0, "For more information on NetHack, or to report a bug,");
1942.     Sprintf(buf, "visit our website \"%s\".", DEVTEAM_URL);
1943.     putstr(cwin, 0, buf);
1944.     display_nhwindow(cwin, FALSE);
1945.     destroy_nhwindow(cwin);
1946. }
1947. 
1948. STATIC_OVL void
1949. dispfile_help(VOID_ARGS)
1950. {
1951.     display_file(HELP, TRUE);
1952. }
1953. 
1954. STATIC_OVL void
1955. dispfile_shelp(VOID_ARGS)
1956. {
1957.     display_file(SHELP, TRUE);
1958. }
1959. 
1960. STATIC_OVL void
1961. dispfile_optionfile(VOID_ARGS)
1962. {
1963.     display_file(OPTIONFILE, TRUE);
1964. }
1965. 
1966. STATIC_OVL void
1967. dispfile_license(VOID_ARGS)
1968. {
1969.     display_file(LICENSE, TRUE);
1970. }
1971. 
1972. STATIC_OVL void
1973. dispfile_debughelp(VOID_ARGS)
1974. {
1975.     display_file(DEBUGHELP, TRUE);
1976. }
1977. 
1978. STATIC_OVL void
1979. hmenu_doextversion(VOID_ARGS)
1980. {
1981.     (void) doextversion();
1982. }
1983. 
1984. STATIC_OVL void
1985. hmenu_dohistory(VOID_ARGS)
1986. {
1987.     (void) dohistory();
1988. }
1989. 
1990. STATIC_OVL void
1991. hmenu_dowhatis(VOID_ARGS)
1992. {
1993.     (void) dowhatis();
1994. }
1995. 
1996. STATIC_OVL void
1997. hmenu_dowhatdoes(VOID_ARGS)
1998. {
1999.     (void) dowhatdoes();
2000. }
2001. 
2002. STATIC_OVL void
2003. hmenu_doextlist(VOID_ARGS)
2004. {
2005.     (void) doextlist();
2006. }
2007. 
2008. void
2009. domenucontrols(VOID_ARGS)
2010. {
2011.     winid cwin = create_nhwindow(NHW_TEXT);
2012.     show_menu_controls(cwin, FALSE);
2013.     display_nhwindow(cwin, FALSE);
2014.     destroy_nhwindow(cwin);
2015. }
2016. 
2017. /* data for dohelp() */
2018. static struct {
2019.     void NDECL((*f));
2020.     const char *text;
2021. } help_menu_items[] = {
2022.     { hmenu_doextversion, "About NetHack (version information)." },
2023.     { dispfile_help, "Long description of the game and commands." },
2024.     { dispfile_shelp, "List of game commands." },
2025.     { hmenu_dohistory, "Concise history of NetHack." },
2026.     { hmenu_dowhatis, "Info on a character in the game display." },
2027.     { hmenu_dowhatdoes, "Info on what a given key does." },
2028.     { option_help, "List of game options." },
2029.     { dispfile_optionfile, "Longer explanation of game options." },
2030.     { dokeylist, "Full list of keyboard commands" },
2031.     { hmenu_doextlist, "List of extended commands." },
2032.     { domenucontrols, "List menu control keys" },
2033.     { dispfile_license, "The NetHack license." },
2034.     { docontact, "Support information." },
2035. #ifdef PORT_HELP
2036.     { port_help, "%s-specific help and commands." },
2037. #endif
2038.     { dispfile_debughelp, "List of wizard-mode commands." },
2039.     { (void NDECL((*))) 0, (char *) 0 }
2040. };
2041. 
2042. /* the '?' command */
2043. int
2044. dohelp()
2045. {
2046.     winid tmpwin = create_nhwindow(NHW_MENU);
2047.     char helpbuf[QBUFSZ];
2048.     int i, n;
2049.     menu_item *selected;
2050.     anything any;
2051.     int sel;
2052. 
2053.     any = zeroany; /* zero all bits */
2054.     start_menu(tmpwin);
2055. 
2056.     for (i = 0; help_menu_items[i].text; i++) {
2057.         if (!wizard && help_menu_items[i].f == dispfile_debughelp)
2058.             continue;
2059.         if (help_menu_items[i].text[0] == '%') {
2060.             Sprintf(helpbuf, help_menu_items[i].text, PORT_ID);
2061.         } else {
2062.             Strcpy(helpbuf, help_menu_items[i].text);
2063.         }
2064.         any.a_int = i + 1;
2065.         add_menu(tmpwin, NO_GLYPH, &any, 0, 0, ATR_NONE,
2066.                  helpbuf, MENU_UNSELECTED);
2067.     }
2068.     end_menu(tmpwin, "Select one item:");
2069.     n = select_menu(tmpwin, PICK_ONE, &selected);
2070.     destroy_nhwindow(tmpwin);
2071.     if (n > 0) {
2072.         sel = selected[0].item.a_int - 1;
2073.         free((genericptr_t) selected);
2074.         (void) (*help_menu_items[sel].f)();
2075.     }
2076.     return 0;
2077. }
2078. 
2079. /* the 'V' command; also a choice for '?' */
2080. int
2081. dohistory()
2082. {
2083.     display_file(HISTORY, TRUE);
2084.     return 0;
2085. }
2086. 
2087. /*pager.c*/