Source:NetHack 5.0.0/src/windows.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to windows.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/windows.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 5.0	windows.c	$NHDT-Date: 1737345149 2025/01/19 19:52:29 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.138 $ */
2. /* Copyright (c) D. Cohrs, 1993. */
3. /* NetHack may be freely redistributed.  See license for details. */
4. 
5. #include "hack.h"
6. #include "dlb.h"
7. #ifdef TTY_GRAPHICS
8. #include "wintty.h"
9. #endif
10. #ifdef CURSES_GRAPHICS
11. extern struct window_procs curses_procs;
12. #endif
13. #ifdef X11_GRAPHICS
14. /* Cannot just blindly include winX.h without including all of X11 stuff
15.    and must get the order of include files right.  Don't bother. */
16. extern struct window_procs X11_procs;
17. extern void win_X11_init(int);
18. #endif
19. #ifdef QT_GRAPHICS
20. extern struct window_procs Qt_procs;
21. #endif
22. #ifdef GEM_GRAPHICS
23. /*#include "wingem.h"*/
24. #endif
25. #ifdef MACOS9
26. extern struct window_procs mac_procs;
27. #endif
28. #ifdef BEOS_GRAPHICS
29. extern struct window_procs beos_procs;
30. extern void be_win_init(int);
31. FAIL /* be_win_init doesn't exist? XXX*/
32. #endif
33. #ifdef AMIGA_INTUITION
34. extern struct window_procs amii_procs;
35. extern struct window_procs amiv_procs;
36. extern void ami_wininit_data(int);
37. #endif
38. #ifdef GNOME_GRAPHICS
39. /*#include "winGnome.h"*/
40. extern struct window_procs Gnome_procs;
41. #endif
42. #ifdef MSWIN_GRAPHICS
43. extern struct window_procs mswin_procs;
44. #endif
45. #ifdef SHIM_GRAPHICS
46. extern struct window_procs shim_procs;
47. #endif
48. #ifdef WINCHAIN
49. extern struct window_procs chainin_procs;
50. extern void chainin_procs_init(int);
51. extern void *chainin_procs_chain(int, int, void *, void *, void *);
52. 
53. extern struct chain_procs chainout_procs;
54. extern void chainout_procs_init(int);
55. extern void *chainout_procs_chain(int, int, void *, void *, void *);
56. 
57. extern struct chain_procs trace_procs;
58. extern void trace_procs_init(int);
59. extern void *trace_procs_chain(int, int, void *, void *, void *);
60. #endif
61. 
62. #if defined(WINCHAIN) || defined(TTY_GRAPHICS)
63. staticfn struct win_choices *win_choices_find(const char *s) NONNULLARG1;
64. #endif
65. 
66. staticfn void def_raw_print(const char *s) NONNULLARG1;
67. staticfn void def_wait_synch(void);
68. staticfn boolean get_menu_coloring(const char *, int *, int *) NONNULLPTRS;
69. 
70. staticfn winid dump_create_nhwindow(int);
71. staticfn void dump_clear_nhwindow(winid);
72. staticfn void dump_display_nhwindow(winid, boolean);
73. staticfn void dump_destroy_nhwindow(winid);
74. staticfn void dump_start_menu(winid, unsigned long);
75. staticfn void dump_add_menu(winid, const glyph_info *, const ANY_P *, char,
76.                           char, int, int, const char *, unsigned int);
77. staticfn void dump_end_menu(winid, const char *);
78. staticfn int dump_select_menu(winid, int, MENU_ITEM_P **);
79. staticfn void dump_putstr(winid, int, const char *);
80. 
81. #ifdef HANGUPHANDLING
82. volatile
83. #endif
84.     NEARDATA struct window_procs windowprocs;
85. 
86. #ifdef WINCHAIN
87. #define CHAINR(x) , x
88. #else
89. #define CHAINR(x)
90. #endif
91. 
92. static struct win_choices {
93.     struct window_procs *procs;
94.     void (*ini_routine)(int); /* optional (can be 0) */
95. #ifdef WINCHAIN
96.     void *(*chain_routine)(int, int, void *, void *, void *);
97. #endif
98. } winchoices[] = {
99. #ifdef TTY_GRAPHICS
100.     { &tty_procs, win_tty_init CHAINR(0) },
101. #endif
102. #ifdef CURSES_GRAPHICS
103.     { &curses_procs, 0 },
104. #endif
105. #ifdef X11_GRAPHICS
106.     { &X11_procs, win_X11_init CHAINR(0) },
107. #endif
108. #ifdef QT_GRAPHICS
109.     { &Qt_procs, 0 CHAINR(0) },
110. #endif
111. #ifdef GEM_GRAPHICS
112.     { &Gem_procs, win_Gem_init CHAINR(0) },
113. #endif
114. #ifdef MACOS9
115.     { &mac_procs, 0 CHAINR(0) },
116. #endif
117. #ifdef BEOS_GRAPHICS
118.     { &beos_procs, be_win_init CHAINR(0) },
119. #endif
120. #ifdef AMIGA_INTUITION
121.     { &amii_procs,
122.       ami_wininit_data CHAINR(0) }, /* Old font version of the game */
123.     { &amiv_procs,
124.       ami_wininit_data CHAINR(0) }, /* Tile version of the game */
125. #endif
126. #ifdef GNOME_GRAPHICS
127.     { &Gnome_procs, 0 CHAINR(0) },
128. #endif
129. #ifdef MSWIN_GRAPHICS
130.     { &mswin_procs, 0 CHAINR(0) },
131. #endif
132. #ifdef SHIM_GRAPHICS
133.     { &shim_procs, 0 CHAINR(0) },
134. #endif
135. #ifdef WINCHAIN
136.     { &chainin_procs, chainin_procs_init, chainin_procs_chain },
137.     { (struct window_procs *) &chainout_procs, chainout_procs_init,
138.       chainout_procs_chain },
139. 
140.     { (struct window_procs *) &trace_procs, trace_procs_init,
141.       trace_procs_chain },
142. #endif
143.     { 0, 0 CHAINR(0) } /* must be last */
144. };
145. 
146. #ifdef WINCHAIN
147. struct winlink {
148.     struct winlink *nextlink;
149.     struct win_choices *wincp;
150.     void *linkdata;
151. };
152. /* NB: this chain does not contain the terminal real window system pointer */
153. 
154. static struct winlink *chain = 0;
155. 
156. staticfn struct winlink *
157. wl_new(void)
158. {
159.     struct winlink *wl = (struct winlink *) alloc(sizeof *wl);
160. 
161.     wl->nextlink = 0;
162.     wl->wincp = 0;
163.     wl->linkdata = 0;
164. 
165.     return wl;
166. }
167. 
168. staticfn void
169. wl_addhead(struct winlink *wl)
170. {
171.     wl->nextlink = chain;
172.     chain = wl;
173. }
174. 
175. staticfn void
176. wl_addtail(struct winlink *wl)
177. {
178.     struct winlink *p = chain;
179. 
180.     if (!chain) {
181.         chain = wl;
182.         return;
183.     }
184.     while (p->nextlink) {
185.         p = p->nextlink;
186.     }
187.     p->nextlink = wl;
188.     return;
189. }
190. #endif /* WINCHAIN */
191. 
192. boolean
193. genl_can_suspend_no(void)
194. {
195.     return FALSE;
196. }
197. 
198. boolean
199. genl_can_suspend_yes(void)
200. {
201.     return TRUE;
202. }
203. 
204. staticfn
205. void
206. def_raw_print(const char *s)
207. {
208.     puts(s);
209.     if (*s)
210.         iflags.raw_printed++;
211. }
212. 
213. staticfn
214. void
215. def_wait_synch(void)
216. {
217.     /* Config file error handling routines
218.      * call wait_sync() without checking to
219.      * see if it actually has a value,
220.      * leading to spectacular violations
221.      * when you try to execute address zero.
222.      * The existence of this allows early
223.      * processing to have something to execute
224.      * even though it essentially does nothing
225.      */
226.      return;
227. }
228. 
229. #ifdef TTY_GRAPHICS
230. boolean
231. check_tty_wincap(unsigned long wincap)
232. {
233.     struct win_choices *wc = win_choices_find("tty");
234. 
235.     if (wc)
236.         return ((wc->procs->wincap & wincap) == wincap);
237.     return FALSE;
238. }
239. 
240. boolean
241. check_tty_wincap2(unsigned long wincap2)
242. {
243.     struct win_choices *wc = win_choices_find("tty");
244. 
245.     if (wc)
246.         return ((wc->procs->wincap2 & wincap2) == wincap2);
247.     return FALSE;
248. }
249. #endif
250. 
251. #if defined(WINCHAIN) || defined(TTY_GRAPHICS)
252. staticfn struct win_choices *
253. win_choices_find(const char *s)
254. {
255.     int i;
256. 
257.     for (i = 0; winchoices[i].procs; i++) {
258.         if (!strcmpi(s, winchoices[i].procs->name)) {
259.             return &winchoices[i];
260.         }
261.     }
262.     return (struct win_choices *) 0;
263. }
264. #endif
265. 
266. void
267. choose_windows(const char *s)
268. {
269.     int i;
270.     char *tmps = 0;
271. 
272.     for (i = 0; winchoices[i].procs; i++) {
273.         if ('+' == winchoices[i].procs->name[0])
274.             continue;
275.         if ('-' == winchoices[i].procs->name[0])
276.             continue;
277.         if (!strcmpi(s, winchoices[i].procs->name)) {
278.             windowprocs = *winchoices[i].procs;
279. 
280.             if (gl.last_winchoice && gl.last_winchoice->ini_routine)
281.                 (*gl.last_winchoice->ini_routine)(WININIT_UNDO);
282.             if (winchoices[i].ini_routine)
283.                 (*winchoices[i].ini_routine)(WININIT);
284.             gl.last_winchoice = &winchoices[i];
285.             return;
286.         }
287.     }
288. 
289.     if (!windowprocs.win_raw_print)
290.         windowprocs.win_raw_print = def_raw_print;
291.     if (!windowprocs.win_wait_synch)
292.         /* early config file error processing routines call this */
293.         windowprocs.win_wait_synch = def_wait_synch;
294. 
295.     if (!winchoices[0].procs) {
296.         raw_printf("No window types supported?");
297.         nh_terminate(EXIT_FAILURE);
298.     }
299.     /* 50: arbitrary, no real window_type names are anywhere near that long;
300.        used to prevent potential raw_printf() overflow if user supplies a
301.        very long string (on the order of 1200 chars) on the command line
302.        (config file options can't get that big; they're truncated at 1023) */
303. #define WINDOW_TYPE_MAXLEN 50
304.     if (strlen(s) >= WINDOW_TYPE_MAXLEN) {
305.         tmps = (char *) alloc(WINDOW_TYPE_MAXLEN);
306.         (void) strncpy(tmps, s, WINDOW_TYPE_MAXLEN - 1);
307.         tmps[WINDOW_TYPE_MAXLEN - 1] = '\0';
308.         s = tmps;
309.     }
310. #undef WINDOW_TYPE_MAXLEN
311. 
312.     if (!winchoices[1].procs) {
313.         config_error_add(
314.                      "Window type %s not recognized.  The only choice is: %s",
315.                          s, winchoices[0].procs->name);
316.     } else {
317.         char buf[BUFSZ];
318.         boolean first = TRUE;
319. 
320.         buf[0] = '\0';
321.         for (i = 0; winchoices[i].procs; i++) {
322.             if ('+' == winchoices[i].procs->name[0])
323.                 continue;
324.             if ('-' == winchoices[i].procs->name[0])
325.                 continue;
326.             Sprintf(eos(buf), "%s%s",
327.                     first ? "" : ", ", winchoices[i].procs->name);
328.             first = FALSE;
329.         }
330.         config_error_add("Window type %s not recognized.  Choices are:  %s",
331.                          s, buf);
332.     }
333.     if (tmps)
334.         free((genericptr_t) tmps) /*, tmps = 0*/ ;
335. 
336.     if (windowprocs.win_raw_print == def_raw_print)
337.         nh_terminate(EXIT_SUCCESS);
338. }
339. 
340. #ifdef WINCHAIN
341. void
342. addto_windowchain(const char *s)
343. {
344.     int i;
345. 
346.     for (i = 0; winchoices[i].procs; i++) {
347.         if ('+' != winchoices[i].procs->name[0])
348.             continue;
349.         if (!strcmpi(s, winchoices[i].procs->name)) {
350.             struct winlink *p = wl_new();
351. 
352.             p->wincp = &winchoices[i];
353.             wl_addtail(p);
354.             /* NB: The ini_routine() will be called during commit. */
355.             return;
356.         }
357.     }
358. 
359.     windowprocs.win_raw_print = def_raw_print;
360. 
361.     raw_printf("Window processor %s not recognized.  Choices are:", s);
362.     for (i = 0; winchoices[i].procs; i++) {
363.         if ('+' != winchoices[i].procs->name[0])
364.             continue;
365.         raw_printf("        %s", winchoices[i].procs->name);
366.     }
367. 
368.     nh_terminate(EXIT_FAILURE);
369. }
370. 
371. void
372. commit_windowchain(void)
373. {
374.     struct winlink *p;
375.     int n;
376.     int wincap, wincap2;
377. 
378.     if (!chain)
379.         return;
380. 
381.     /* Save wincap* from the real window system - we'll restore it below. */
382.     wincap = windowprocs.wincap;
383.     wincap2 = windowprocs.wincap2;
384. 
385.     /* add -chainin at head and -chainout at tail */
386.     p = wl_new();
387.     p->wincp = win_choices_find("-chainin");
388.     if (!p->wincp) {
389.         raw_printf("Can't locate processor '-chainin'");
390.         exit(EXIT_FAILURE);
391.     }
392.     wl_addhead(p);
393. 
394.     p = wl_new();
395.     p->wincp = win_choices_find("-chainout");
396.     if (!p->wincp) {
397.         raw_printf("Can't locate processor '-chainout'");
398.         exit(EXIT_FAILURE);
399.     }
400.     wl_addtail(p);
401. 
402.     /* Now alloc() init() similar to Objective-C. */
403.     for (n = 1, p = chain; p; n++, p = p->nextlink) {
404.         p->linkdata = (*p->wincp->chain_routine)(WINCHAIN_ALLOC, n, 0, 0, 0);
405.     }
406. 
407.     for (n = 1, p = chain; p; n++, p = p->nextlink) {
408.         if (p->nextlink) {
409.             (void) (*p->wincp->chain_routine)(WINCHAIN_INIT, n, p->linkdata,
410.                                               p->nextlink->wincp->procs,
411.                                               p->nextlink->linkdata);
412.         } else {
413.             (void) (*p->wincp->chain_routine)(WINCHAIN_INIT, n, p->linkdata,
414.                                               gl.last_winchoice->procs, 0);
415.         }
416.     }
417. 
418.     /* Restore the saved wincap* values.  We do it here to give the
419.      * ini_routine()s a chance to change or check them. */
420.     chain->wincp->procs->wincap = wincap;
421.     chain->wincp->procs->wincap2 = wincap2;
422. 
423.     /* Call the init procs.  Do not re-init the terminal real win. */
424.     p = chain;
425.     while (p->nextlink) {
426.         if (p->wincp->ini_routine) {
427.             (*p->wincp->ini_routine)(WININIT);
428.         }
429.         p = p->nextlink;
430.     }
431. 
432.     /* Install the chain into window procs very late so ini_routine()s
433.      * can raw_print on error. */
434.     windowprocs = *chain->wincp->procs;
435. 
436.     p = chain;
437.     while (p) {
438.         struct winlink *np = p->nextlink;
439.         free(p);
440.         p = np; /* assignment, not proof */
441.     }
442. }
443. #endif /* WINCHAIN */
444. 
445. /*
446.  * tty_message_menu() provides a means to get feedback from the
447.  * --More-- prompt; other interfaces generally don't need that.
448.  */
449. /*ARGSUSED*/
450. char
451. genl_message_menu(char let UNUSED,
452.                   int how UNUSED,
453.                   const char *mesg)
454. {
455.     pline("%s", mesg);
456.     return 0;
457. }
458. 
459. /*ARGSUSED*/
460. void
461. genl_preference_update(const char *pref UNUSED)
462. {
463.     /* window ports are expected to provide
464.        their own preference update routine
465.        for the preference capabilities that
466.        they support.
467.        Just return in this genl one. */
468.     return;
469. }
470. 
471. char *
472. genl_getmsghistory(boolean init UNUSED)
473. {
474.     /* window ports can provide
475.        their own getmsghistory() routine to
476.        preserve message history between games.
477.        The routine is called repeatedly from
478.        the core save routine, and the window
479.        port is expected to successively return
480.        each message that it wants saved, starting
481.        with the oldest message first, finishing
482.        with the most recent.
483.        Return null pointer when finished.
484.      */
485.     return (char *) 0;
486. }
487. 
488. void
489. genl_putmsghistory(const char *msg, boolean is_restoring)
490. {
491.     /* window ports can provide
492.        their own putmsghistory() routine to
493.        load message history from a saved game.
494.        The routine is called repeatedly from
495.        the core restore routine, starting with
496.        the oldest saved message first, and
497.        finishing with the latest.
498.        The window port routine is expected to
499.        load the message recall buffers in such
500.        a way that the ordering is preserved.
501.        The window port routine should make no
502.        assumptions about how many messages are
503.        forthcoming, nor should it assume that
504.        another message will follow this one,
505.        so it should keep all pointers/indexes
506.        intact at the end of each call.
507.     */
508. 
509.     /* this doesn't provide for reloading the message window with the
510.        previous session's messages upon restore, but it does put the quest
511.        message summary lines there by treating them as ordinary messages */
512.     if (!is_restoring)
513.         pline("%s", msg);
514.     return;
515. }
516. 
517. #ifdef HANGUPHANDLING
518. /*
519.  * Dummy windowing scheme used to replace current one with no-ops
520.  * in order to avoid all terminal I/O after hangup/disconnect.
521.  */
522. 
523. staticfn int hup_nhgetch(void);
524. staticfn char hup_yn_function(const char *, const char *, char);
525. staticfn int hup_nh_poskey(coordxy *, coordxy *, int *);
526. staticfn void hup_getlin(const char *, char *);
527. staticfn void hup_init_nhwindows(int *, char **);
528. staticfn void hup_exit_nhwindows(const char *);
529. staticfn winid hup_create_nhwindow(int);
530. staticfn int hup_select_menu(winid, int, MENU_ITEM_P **);
531. staticfn void hup_add_menu(winid, const glyph_info *, const anything *, char,
532.                          char, int, int, const char *, unsigned int);
533. staticfn void hup_end_menu(winid, const char *);
534. staticfn void hup_putstr(winid, int, const char *);
535. staticfn void hup_print_glyph(winid, coordxy, coordxy, const glyph_info *,
536.                             const glyph_info *);
537. staticfn void hup_outrip(winid, int, time_t);
538. staticfn void hup_curs(winid, int, int);
539. staticfn void hup_display_nhwindow(winid, boolean);
540. staticfn void hup_display_file(const char *, boolean);
541. #ifdef CLIPPING
542. staticfn void hup_cliparound(int, int);
543. #endif
544. #ifdef CHANGE_COLOR
545. staticfn void hup_change_color(int, long, int);
546. #ifdef MACOS9
547. staticfn short hup_set_font_name(winid, char *);
548. #endif
549. staticfn char *hup_get_color_string(void);
550. #endif /* CHANGE_COLOR */
551. staticfn void hup_status_update(int, genericptr_t, int, int, int,
552.                               unsigned long *);
553. 
554. staticfn int hup_int_ndecl(void);
555. staticfn void hup_void_ndecl(void);
556. staticfn void hup_void_fdecl_int(int);
557. staticfn void hup_void_fdecl_winid(winid);
558. staticfn void hup_void_fdecl_winid_ulong(winid, unsigned long);
559. staticfn void hup_void_fdecl_constchar_p(const char *);
560. staticfn win_request_info *hup_ctrl_nhwindow(winid, int, win_request_info *);
561. 
562. static struct window_procs hup_procs = {
563.     WPID(hup), 0L, 0L,
564.     { FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
565.       FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE }, /* colors */
566.     hup_init_nhwindows,
567.     hup_void_ndecl,                                    /* player_selection */
568.     hup_void_ndecl,                                    /* askname */
569.     hup_void_ndecl,                                    /* get_nh_event */
570.     hup_exit_nhwindows, hup_void_fdecl_constchar_p,    /* suspend_nhwindows */
571.     hup_void_ndecl,                                    /* resume_nhwindows */
572.     hup_create_nhwindow, hup_void_fdecl_winid,         /* clear_nhwindow */
573.     hup_display_nhwindow, hup_void_fdecl_winid,        /* destroy_nhwindow */
574.     hup_curs, hup_putstr, hup_putstr,                  /* putmixed */
575.     hup_display_file, hup_void_fdecl_winid_ulong,      /* start_menu */
576.     hup_add_menu, hup_end_menu, hup_select_menu, genl_message_menu,
577.     hup_void_ndecl,                                    /* mark_synch */
578.     hup_void_ndecl,                                    /* wait_synch */
579. #ifdef CLIPPING
580.     hup_cliparound,
581. #endif
582. #ifdef POSITIONBAR
583.     (void (*)(char *)) hup_void_fdecl_constchar_p,    /* update_positionbar */
584. #endif
585.     hup_print_glyph,
586.     hup_void_fdecl_constchar_p,                       /* raw_print */
587.     hup_void_fdecl_constchar_p,                       /* raw_print_bold */
588.     hup_nhgetch, hup_nh_poskey, hup_void_ndecl,       /* nhbell  */
589.     hup_int_ndecl,                                    /* doprev_message */
590.     hup_yn_function, hup_getlin, hup_int_ndecl,       /* get_ext_cmd */
591.     hup_void_fdecl_int,                               /* number_pad */
592.     hup_void_ndecl,                                   /* nh_delay_output  */
593. #ifdef CHANGE_COLOR
594.     hup_change_color,
595. #ifdef MACOS9
596.     hup_void_fdecl_int,                               /* change_background */
597.     hup_set_font_name,
598. #endif
599.     hup_get_color_string,
600. #endif /* CHANGE_COLOR */
601.     hup_outrip, genl_preference_update, genl_getmsghistory,
602.     genl_putmsghistory,
603.     hup_void_ndecl,                                   /* status_init */
604.     hup_void_ndecl,                                   /* status_finish */
605.     genl_status_enablefield, hup_status_update,
606.     genl_can_suspend_no,
607.     hup_void_fdecl_int,                               /* update_inventory */
608.     hup_ctrl_nhwindow,
609. };
610. 
611. static void (*previnterface_exit_nhwindows)(const char *) = 0;
612. 
613. /* hangup has occurred; switch to no-op user interface */
614. void
615. nhwindows_hangup(void)
616. {
617.     char *(*previnterface_getmsghistory)(boolean) = 0;
618. 
619. #ifdef ALTMETA
620.     /* command processor shouldn't look for 2nd char after seeing ESC */
621.     iflags.altmeta = FALSE;
622. #endif
623. 
624.     /* don't call exit_nhwindows() directly here; if a hangup occurs
625.        while interface code is executing, exit_nhwindows could knock
626.        the interface's active data structures out from under itself */
627.     if (iflags.window_inited
628.         && windowprocs.win_exit_nhwindows != hup_exit_nhwindows)
629.         previnterface_exit_nhwindows = windowprocs.win_exit_nhwindows;
630. 
631.     /* also, we have to leave the old interface's getmsghistory()
632.        in place because it will be called while saving the game */
633.     if (windowprocs.win_getmsghistory != hup_procs.win_getmsghistory)
634.         previnterface_getmsghistory = windowprocs.win_getmsghistory;
635. 
636.     windowprocs = hup_procs;
637. 
638.     if (previnterface_getmsghistory)
639.         windowprocs.win_getmsghistory = previnterface_getmsghistory;
640. }
641. 
642. staticfn void
643. hup_exit_nhwindows(const char *lastgasp)
644. {
645.     /* core has called exit_nhwindows(); call the previous interface's
646.        shutdown routine now; xxx_exit_nhwindows() needs to call other
647.        xxx_ routines directly rather than through windowprocs pointers */
648.     if (previnterface_exit_nhwindows) {
649.         lastgasp = 0; /* don't want exit routine to attempt extra output */
650.         (*previnterface_exit_nhwindows)(lastgasp);
651.         previnterface_exit_nhwindows = 0;
652.     }
653.     iflags.window_inited = FALSE;
654. }
655. 
656. staticfn int
657. hup_nhgetch(void)
658. {
659.     return '\033'; /* ESC */
660. }
661. 
662. /*ARGSUSED*/
663. staticfn char
664. hup_yn_function(
665.     const char *prompt UNUSED,
666.     const char *resp UNUSED,
667.     char deflt)
668. {
669.     if (!deflt)
670.         deflt = '\033';
671.     return deflt;
672. }
673. 
674. /*ARGSUSED*/
675. staticfn int
676. hup_nh_poskey(coordxy *x UNUSED, coordxy *y UNUSED, int *mod UNUSED)
677. {
678.     return '\033';
679. }
680. 
681. /*ARGSUSED*/
682. staticfn void
683. hup_getlin(const char *prompt UNUSED, char *outbuf)
684. {
685.     Strcpy(outbuf, "\033");
686. }
687. 
688. /*ARGSUSED*/
689. staticfn void
690. hup_init_nhwindows(int *argc_p UNUSED, char **argv UNUSED)
691. {
692.     iflags.window_inited = TRUE;
693. }
694. 
695. /*ARGUSED*/
696. staticfn winid
697. hup_create_nhwindow(int type UNUSED)
698. {
699.     return WIN_ERR;
700. }
701. 
702. /*ARGSUSED*/
703. staticfn int
704. hup_select_menu(
705.     winid window UNUSED,
706.     int how UNUSED,
707.     struct mi **menu_list UNUSED)
708. {
709.     return -1;
710. }
711. 
712. /*ARGSUSED*/
713. staticfn void
714. hup_add_menu(
715.     winid window UNUSED,
716.     const glyph_info *glyphinfo UNUSED,
717.     const anything *identifier UNUSED,
718.     char sel UNUSED,
719.     char grpsel UNUSED,
720.     int attr UNUSED,
721.     int clr UNUSED,
722.     const char *txt UNUSED,
723.     unsigned int itemflags UNUSED)
724. {
725.     return;
726. }
727. 
728. /*ARGSUSED*/
729. staticfn void
730. hup_end_menu(winid window UNUSED, const char *prompt UNUSED)
731. {
732.     return;
733. }
734. 
735. /*ARGSUSED*/
736. staticfn void
737. hup_putstr(winid window UNUSED, int attr UNUSED, const char *text UNUSED)
738. {
739.     return;
740. }
741. 
742. /*ARGSUSED*/
743. staticfn void
744. hup_print_glyph(
745.     winid window UNUSED,
746.     coordxy x UNUSED, coordxy y UNUSED,
747.     const glyph_info *glyphinfo UNUSED,
748.     const glyph_info *bkglyphinfo UNUSED)
749. {
750.     return;
751. }
752. 
753. /*ARGSUSED*/
754. staticfn void
755. hup_outrip(winid tmpwin UNUSED, int how UNUSED, time_t when UNUSED)
756. {
757.     return;
758. }
759. 
760. /*ARGSUSED*/
761. staticfn void
762. hup_curs(winid window UNUSED, int x UNUSED, int y UNUSED)
763. {
764.     return;
765. }
766. 
767. /*ARGSUSED*/
768. staticfn void
769. hup_display_nhwindow(winid window UNUSED, boolean blocking UNUSED)
770. {
771.     return;
772. }
773. 
774. /*ARGSUSED*/
775. staticfn void
776. hup_display_file(const char *fname UNUSED, boolean complain UNUSED)
777. {
778.     return;
779. }
780. 
781. #ifdef CLIPPING
782. /*ARGSUSED*/
783. staticfn void
784. hup_cliparound(int x UNUSED, int y UNUSED)
785. {
786.     return;
787. }
788. #endif
789. 
790. #ifdef CHANGE_COLOR
791. /*ARGSUSED*/
792. staticfn void
793. hup_change_color(int color UNUSED, long rgb UNUSED, int reverse UNUSED)
794. {
795.     return;
796. }
797. 
798. #ifdef MACOS9
799. /*ARGSUSED*/
800. staticfn short
801. hup_set_font_name(winid window UNUSED, char *fontname UNUSED)
802. {
803.     return 0;
804. }
805. #endif /* MACOS9 */
806. 
807. staticfn char *
808. hup_get_color_string(void)
809. {
810.     return (char *) 0;
811. }
812. #endif /* CHANGE_COLOR */
813. 
814. /*ARGSUSED*/
815. staticfn void
816. hup_status_update(
817.     int idx UNUSED, genericptr_t ptr UNUSED,
818.     int chg UNUSED, int pc UNUSED,
819.     int color UNUSED, unsigned long *colormasks UNUSED)
820. {
821.     return;
822. }
823. 
824. /*
825.  * Non-specific stubs.
826.  */
827. 
828. staticfn int
829. hup_int_ndecl(void)
830. {
831.     return -1;
832. }
833. 
834. staticfn void
835. hup_void_ndecl(void)
836. {
837.     return;
838. }
839. 
840. /*ARGUSED*/
841. staticfn void
842. hup_void_fdecl_int(int arg UNUSED)
843. {
844.     return;
845. }
846. 
847. /*ARGUSED*/
848. staticfn void
849. hup_void_fdecl_winid(winid window UNUSED)
850. {
851.     return;
852. }
853. 
854. /*ARGUSED*/
855. staticfn void
856. hup_void_fdecl_winid_ulong(
857.     winid window UNUSED,
858.     unsigned long mbehavior UNUSED)
859. {
860.     return;
861. }
862. 
863. /*ARGUSED*/
864. staticfn void
865. hup_void_fdecl_constchar_p(const char *string UNUSED)
866. {
867.     return;
868. }
869. 
870. /*ARGUSED*/
871. win_request_info *
872. hup_ctrl_nhwindow(
873.     winid window UNUSED,  /* window to use, must be of type NHW_MENU */
874.     int request UNUSED,
875.     win_request_info *wri UNUSED)
876. {
877.     return (win_request_info *) 0;
878. }
879. 
880. #endif /* HANGUPHANDLING */
881. 
882. 
883. /****************************************************************************/
884. /* genl backward compat stuff                                               */
885. /****************************************************************************/
886. 
887. const char *status_fieldnm[MAXBLSTATS];
888. const char *status_fieldfmt[MAXBLSTATS];
889. char *status_vals[MAXBLSTATS];
890. boolean status_activefields[MAXBLSTATS];
891. 
892. void
893. genl_status_init(void)
894. {
895.     int i;
896. 
897.     for (i = 0; i < MAXBLSTATS; ++i) {
898.         status_vals[i] = (char *) alloc(MAXCO);
899.         *status_vals[i] = '\0';
900.         status_activefields[i] = FALSE;
901.         status_fieldfmt[i] = (const char *) 0;
902.     }
903.     /* Use a window for the genl version; backward port compatibility */
904.     WIN_STATUS = create_nhwindow(NHW_STATUS);
905.     display_nhwindow(WIN_STATUS, FALSE);
906. }
907. 
908. void
909. genl_status_finish(void)
910. {
911.     /* tear down routine */
912.     int i;
913. 
914.     /* free alloc'd memory here */
915.     for (i = 0; i < MAXBLSTATS; ++i) {
916.         if (status_vals[i])
917.             free((genericptr_t) status_vals[i]), status_vals[i] = (char *) 0;
918.     }
919. }
920. 
921. void
922. genl_status_enablefield(
923.     int fieldidx,
924.     const char *nm,
925.     const char *fmt,
926.     boolean enable)
927. {
928.     status_fieldfmt[fieldidx] = fmt;
929.     status_fieldnm[fieldidx] = nm;
930.     status_activefields[fieldidx] = enable;
931. }
932. 
933. DISABLE_WARNING_FORMAT_NONLITERAL
934. 
935. /* call once for each field, then call with BL_FLUSH to output the result */
936. void
937. genl_status_update(
938.     int idx,
939.     genericptr_t ptr,
940.     int chg UNUSED, int percent UNUSED,
941.     int color UNUSED, unsigned long *colormasks UNUSED)
942. {
943.     char newbot1[MAXCO], newbot2[MAXCO];
944.     long cond, *condptr = (long *) ptr;
945.     int i;
946.     unsigned pass, lndelta;
947.     enum statusfields idx1, idx2, *fieldlist;
948.     char *nb, *text = (char *) ptr;
949. 
950.     static enum statusfields fieldorder[][15] = {
951.         /* line one */
952.         { BL_TITLE, BL_STR, BL_DX, BL_CO, BL_IN, BL_WI, BL_CH, BL_ALIGN,
953.           BL_SCORE, BL_FLUSH, BL_FLUSH, BL_FLUSH, BL_FLUSH, BL_FLUSH,
954.           BL_FLUSH },
955.         /* line two, default order */
956.         { BL_LEVELDESC, BL_GOLD,
957.           BL_HP, BL_HPMAX, BL_ENE, BL_ENEMAX, BL_AC,
958.           BL_XP, BL_EXP, BL_HD,
959.           BL_TIME,
960.           BL_HUNGER, BL_CAP, BL_CONDITION,
961.           BL_FLUSH },
962.         /* move time to the end */
963.         { BL_LEVELDESC, BL_GOLD,
964.           BL_HP, BL_HPMAX, BL_ENE, BL_ENEMAX, BL_AC,
965.           BL_XP, BL_EXP, BL_HD,
966.           BL_HUNGER, BL_CAP, BL_CONDITION,
967.           BL_TIME, BL_FLUSH },
968.         /* move experience and time to the end */
969.         { BL_LEVELDESC, BL_GOLD,
970.           BL_HP, BL_HPMAX, BL_ENE, BL_ENEMAX, BL_AC,
971.           BL_HUNGER, BL_CAP, BL_CONDITION,
972.           BL_XP, BL_EXP, BL_HD, BL_TIME, BL_FLUSH },
973.         /* move level description plus gold and experience and time to end */
974.         { BL_HP, BL_HPMAX, BL_ENE, BL_ENEMAX, BL_AC,
975.           BL_HUNGER, BL_CAP, BL_CONDITION,
976.           BL_LEVELDESC, BL_GOLD, BL_XP, BL_EXP, BL_HD, BL_TIME, BL_FLUSH },
977.     };
978. 
979.     /* in case interface is using genl_status_update() but has not
980.        specified WC2_FLUSH_STATUS (status_update() for field values
981.        is buffered so final BL_FLUSH is needed to produce output) */
982.     windowprocs.wincap2 |= WC2_FLUSH_STATUS;
983. 
984.     if (idx >= 0) {
985.         if (!status_activefields[idx])
986.             return;
987.         switch (idx) {
988.         case BL_CONDITION:
989.             cond = condptr ? *condptr : 0L;
990.             nb = status_vals[idx];
991.             *nb = '\0';
992.             if (cond & BL_MASK_STONE)
993.                 Strcpy(nb = eos(nb), " Stone");
994.             if (cond & BL_MASK_SLIME)
995.                 Strcpy(nb = eos(nb), " Slime");
996.             if (cond & BL_MASK_STRNGL)
997.                 Strcpy(nb = eos(nb), " Strngl");
998.             if (cond & BL_MASK_FOODPOIS)
999.                 Strcpy(nb = eos(nb), " FoodPois");
1000.             if (cond & BL_MASK_TERMILL)
1001.                 Strcpy(nb = eos(nb), " TermIll");
1002.             if (cond & BL_MASK_BLIND)
1003.                 Strcpy(nb = eos(nb), " Blind");
1004.             if (cond & BL_MASK_DEAF)
1005.                 Strcpy(nb = eos(nb), " Deaf");
1006.             if (cond & BL_MASK_STUN)
1007.                 Strcpy(nb = eos(nb), " Stun");
1008.             if (cond & BL_MASK_CONF)
1009.                 Strcpy(nb = eos(nb), " Conf");
1010.             if (cond & BL_MASK_HALLU)
1011.                 Strcpy(nb = eos(nb), " Hallu");
1012.             if (cond & BL_MASK_LEV)
1013.                 Strcpy(nb = eos(nb), " Lev");
1014.             if (cond & BL_MASK_FLY)
1015.                 Strcpy(nb = eos(nb), " Fly");
1016.             if (cond & BL_MASK_RIDE)
1017.                 Strcpy(nb = eos(nb), " Ride");
1018.             break;
1019.         default:
1020.             Sprintf(status_vals[idx],
1021.                     status_fieldfmt[idx] ? status_fieldfmt[idx] : "%s",
1022.                     text ? text : "");
1023.             break;
1024.         }
1025.         return; /* processed one field other than BL_FLUSH */
1026.     } /* (idx >= 0, thus not BL_FLUSH, BL_RESET, BL_CHARACTERISTICS) */
1027. 
1028.     /* does BL_RESET require any specific code to ensure all fields ? */
1029. 
1030.     if (!(idx == BL_FLUSH || idx == BL_RESET))
1031.         return;
1032. 
1033.     /* We've received BL_FLUSH; time to output the gathered data */
1034.     nb = newbot1;
1035.     *nb = '\0';
1036.     /* BL_FLUSH is the only pseudo-index value we need to check for
1037.        in the loop below because it is the only entry used to pad the
1038.        end of the fieldorder array. We could stop on any
1039.        negative (illegal) index, but this should be fine */
1040.     for (i = 0; (idx1 = fieldorder[0][i]) != BL_FLUSH; ++i) {
1041.         if (status_activefields[idx1])
1042.             Strcpy(nb = eos(nb), status_vals[idx1]);
1043.     }
1044.     /* if '$' is encoded, buffer length of \GXXXXNNNN is 9 greater than
1045.        single char; we want to subtract that 9 when checking display length */
1046.     lndelta = (status_activefields[BL_GOLD]
1047.                && strstr(status_vals[BL_GOLD], "\\G")) ? 9 : 0;
1048.     /* basic bot2 formats groups of second line fields into five buffers,
1049.        then decides how to order those buffers based on comparing lengths
1050.        of [sub]sets of them to the width of the map; we have more control
1051.        here but currently emulate that behavior */
1052.     for (pass = 1; pass <= 4; pass++) {
1053.         fieldlist = fieldorder[pass];
1054.         nb = newbot2;
1055.         *nb = '\0';
1056.         for (i = 0; (idx2 = fieldlist[i]) != BL_FLUSH; ++i) {
1057.             if (status_activefields[idx2]) {
1058.                 const char *val = status_vals[idx2];
1059. 
1060.                 switch (idx2) {
1061.                 case BL_HP: /* for pass 4, Hp comes first; mungspaces()
1062.                                will strip the unwanted leading spaces */
1063.                 case BL_XP: case BL_HD:
1064.                 case BL_TIME:
1065.                     Strcpy(nb = eos(nb), " ");
1066.                     break;
1067.                 case BL_LEVELDESC:
1068.                     /* leveldesc has no leading space, so if we've moved
1069.                        it past the first position, provide one */
1070.                     if (i != 0)
1071.                         Strcpy(nb = eos(nb), " ");
1072.                     break;
1073.                 /*
1074.                  * We want "  hunger encumbrance conditions"
1075.                  *   or    "  encumbrance conditions"
1076.                  *   or    "  hunger conditions"
1077.                  *   or    "  conditions"
1078.                  * 'hunger'      is either " " or " hunger_text";
1079.                  * 'encumbrance' is either " " or " encumbrance_text";
1080.                  * 'conditions'  is either ""  or " cond1 cond2...".
1081.                  */
1082.                 case BL_HUNGER:
1083.                     /* hunger==" " - keep it, end up with " ";
1084.                        hunger!=" " - insert space and get "  hunger" */
1085.                     if (strcmp(val, " "))
1086.                         Strcpy(nb = eos(nb), " ");
1087.                     break;
1088.                 case BL_CAP:
1089.                     /* cap==" " - suppress it, retain "  hunger" or " ";
1090.                        cap!=" " - use it, get "  hunger cap" or "  cap" */
1091.                     if (!strcmp(val, " "))
1092.                         ++val;
1093.                     break;
1094.                 default:
1095.                     break;
1096.                 }
1097.                 Strcpy(nb = eos(nb), val); /* status_vals[idx2] */
1098.             } /* status_activefields[idx2] */
1099. 
1100.             if (idx2 == BL_CONDITION && pass < 4
1101.                 && strlen(newbot2) - lndelta > COLNO)
1102.                 break; /* switch to next order */
1103.         } /* i */
1104. 
1105.         if (idx2 == BL_FLUSH) { /* made it past BL_CONDITION */
1106.             if (pass > 1)
1107.                 mungspaces(newbot2);
1108.             break;
1109.         }
1110.     } /* pass */
1111.     curs(WIN_STATUS, 1, 0);
1112.     putstr(WIN_STATUS, 0, newbot1);
1113.     curs(WIN_STATUS, 1, 1);
1114.     putmixed(WIN_STATUS, 0, newbot2); /* putmixed() due to GOLD glyph */
1115. }
1116. 
1117. RESTORE_WARNING_FORMAT_NONLITERAL
1118. 
1119. static struct window_procs dumplog_windowprocs_backup;
1120. static FILE *dumplog_file;
1121. 
1122. #ifdef DUMPLOG
1123. static time_t dumplog_now;
1124. 
1125. char *
1126. dump_fmtstr(
1127.     const char *fmt,
1128.     char *buf,
1129.     boolean fullsubs) /* True -> full substitution for file name,
1130.                        * False -> partial substitution for '--showpaths'
1131.                        * feedback where there's no game in progress */
1132. {
1133.     const char *fp = fmt;
1134.     char *bp = buf;
1135.     int slen, len = 0;
1136.     char tmpbuf[BUFSZ];
1137.     char verbuf[BUFSZ];
1138.     long uid;
1139.     time_t now;
1140. 
1141.     now = dumplog_now;
1142.     uid = (long) getuid();
1143. 
1144.     /*
1145.      * Note: %t and %T assume that time_t is a 'long int' number of
1146.      * seconds since some epoch value.  That's quite iffy....  The
1147.      * unit of time might be different and the datum size might be
1148.      * some variant of 'long long int'.  [Their main purpose is to
1149.      * construct a unique file name rather than record the date and
1150.      * time; violating the 'long seconds since base-date' assumption
1151.      * may or may not interfere with that usage.]
1152.      */
1153. 
1154.     while (fp && *fp && len < BUFSZ - 1) {
1155.         if (*fp == '%') {
1156.             fp++;
1157.             switch (*fp) {
1158.             default:
1159.                 goto finish;
1160.             case '\0': /* fallthrough */
1161.             case '%':  /* literal % */
1162.                 Sprintf(tmpbuf, "%%");
1163.                 break;
1164.             case 't': /* game start, timestamp */
1165.                 if (fullsubs)
1166.                     Sprintf(tmpbuf, "%lu", (unsigned long) ubirthday);
1167.                 else
1168.                     Strcpy(tmpbuf, "{game start cookie}");
1169.                 break;
1170.             case 'T': /* current time, timestamp */
1171.                 if (fullsubs)
1172.                     Sprintf(tmpbuf, "%lu", (unsigned long) now);
1173.                 else
1174.                     Strcpy(tmpbuf, "{current time cookie}");
1175.                 break;
1176.             case 'd': /* game start, YYYYMMDDhhmmss */
1177.                 if (fullsubs)
1178.                     Sprintf(tmpbuf, "%08ld%06ld",
1179.                             yyyymmdd(ubirthday), hhmmss(ubirthday));
1180.                 else
1181.                     Strcpy(tmpbuf, "{game start date+time}");
1182.                 break;
1183.             case 'D': /* current time, YYYYMMDDhhmmss */
1184.                 if (fullsubs)
1185.                     Sprintf(tmpbuf, "%08ld%06ld", yyyymmdd(now), hhmmss(now));
1186.                 else
1187.                     Strcpy(tmpbuf, "{current date+time}");
1188.                 break;
1189.             case 'v': /* version, eg. "5.0.0,-0" */
1190.                 Sprintf(tmpbuf, "%s", version_string(verbuf, sizeof verbuf));
1191.                 break;
1192.             case 'u': /* UID */
1193.                 Sprintf(tmpbuf, "%ld", uid);
1194.                 break;
1195.             case 'n': /* player name */
1196.                 if (fullsubs)
1197.                     Sprintf(tmpbuf, "%s",
1198.                             *svp.plname ? svp.plname : "unknown");
1199.                 else
1200.                     Strcpy(tmpbuf, "{hero name}");
1201.                 break;
1202.             case 'N': /* first character of player name */
1203.                 if (fullsubs)
1204.                     Sprintf(tmpbuf, "%c", *svp.plname ? *svp.plname : 'u');
1205.                 else
1206.                     Strcpy(tmpbuf, "{hero initial}");
1207.                 break;
1208.             }
1209.             if (fullsubs) {
1210.                 /* replace potentially troublesome characters (including
1211.                    <space> even though it might be an acceptable file name
1212.                    character); user shouldn't be able to get ' ' or '/'
1213.                    or '\\' into plname[] but play things safe */
1214.                 (void) strNsubst(tmpbuf, " ", "_", 0);
1215.                 (void) strNsubst(tmpbuf, "/", "_", 0);
1216.                 (void) strNsubst(tmpbuf, "\\", "_", 0);
1217.                 /* note: replacements are only done on field substitutions,
1218.                    not on the template (from sysconf or DUMPLOG_FILE) */
1219.             }
1220. 
1221.             slen = (int) strlen(tmpbuf);
1222.             if (len + slen < BUFSZ - 1) {
1223.                 len += slen;
1224.                 Sprintf(bp, "%s", tmpbuf);
1225.                 bp += slen;
1226.                 if (*fp)
1227.                     fp++;
1228.             } else
1229.                 break;
1230.         } else {
1231.             *bp = *fp;
1232.             bp++;
1233.             fp++;
1234.             len++;
1235.         }
1236.     }
1237.  finish:
1238.     *bp = '\0';
1239.     return buf;
1240. }
1241. #endif /* DUMPLOG */
1242. 
1243. void
1244. dump_open_log(time_t now)
1245. {
1246. #ifdef DUMPLOG
1247.     char buf[BUFSZ];
1248.     char *fname;
1249. 
1250.     dumplog_now = now;
1251. #ifdef SYSCF
1252.     if (!sysopt.dumplogfile)
1253.         return;
1254.     fname = dump_fmtstr(sysopt.dumplogfile, buf, TRUE);
1255. #else
1256.     fname = dump_fmtstr(DUMPLOG_FILE, buf, TRUE);
1257. #endif
1258.     dumplog_file = fopen(fname, "w");
1259.     dumplog_windowprocs_backup = windowprocs;
1260. 
1261. #else /*!DUMPLOG*/
1262.     nhUse(now);
1263. #endif /*?DUMPLOG*/
1264. }
1265. 
1266. void
1267. dump_close_log(void)
1268. {
1269.     if (dumplog_file) {
1270.         (void) fclose(dumplog_file);
1271.         dumplog_file = (FILE *) 0;
1272.     }
1273. }
1274. 
1275. void
1276. dump_forward_putstr(winid win, int attr, const char *str, int no_forward)
1277. {
1278.     if (dumplog_file)
1279.         fprintf(dumplog_file, "%s\n", str);
1280.     if (!no_forward)
1281.         putstr(win, attr, str);
1282. }
1283. 
1284. /*ARGSUSED*/
1285. staticfn void
1286. dump_putstr(winid win UNUSED, int attr UNUSED, const char *str)
1287. {
1288.     if (dumplog_file)
1289.         fprintf(dumplog_file, "%s\n", str);
1290. }
1291. 
1292. staticfn winid
1293. dump_create_nhwindow(int type UNUSED)
1294. {
1295.     return WIN_ERR;
1296. }
1297. 
1298. /*ARGUSED*/
1299. staticfn void
1300. dump_clear_nhwindow(winid win UNUSED)
1301. {
1302.     return;
1303. }
1304. 
1305. /*ARGSUSED*/
1306. staticfn void
1307. dump_display_nhwindow(winid win UNUSED, boolean p UNUSED)
1308. {
1309.     return;
1310. }
1311. 
1312. /*ARGUSED*/
1313. staticfn void
1314. dump_destroy_nhwindow(winid win UNUSED)
1315. {
1316.     return;
1317. }
1318. 
1319. /*ARGUSED*/
1320. staticfn void
1321. dump_start_menu(winid win UNUSED, unsigned long mbehavior UNUSED)
1322. {
1323.     return;
1324. }
1325. 
1326. /*ARGSUSED*/
1327. staticfn void
1328. dump_add_menu(winid win UNUSED,
1329.               const glyph_info *glyphinfo,
1330.               const anything *identifier UNUSED,
1331.               char ch,
1332.               char gch UNUSED,
1333.               int attr UNUSED,
1334.               int clr UNUSED,
1335.               const char *str,
1336.               unsigned int itemflags UNUSED)
1337. {
1338.     if (dumplog_file) {
1339.         if (glyphinfo->glyph == NO_GLYPH)
1340.             fprintf(dumplog_file, " %s\n", str);
1341.         else
1342.             fprintf(dumplog_file, "  %c - %s\n", ch, str);
1343.     }
1344. }
1345. 
1346. /*ARGSUSED*/
1347. staticfn void
1348. dump_end_menu(winid win UNUSED, const char *str)
1349. {
1350.     if (dumplog_file) {
1351.         if (str)
1352.             fprintf(dumplog_file, "%s\n", str);
1353.         else
1354.             fputs("\n", dumplog_file);
1355.     }
1356. }
1357. 
1358. staticfn int
1359. dump_select_menu(winid win UNUSED, int how UNUSED, menu_item **item)
1360. {
1361.     *item = (menu_item *) 0;
1362.     return 0;
1363. }
1364. 
1365. void
1366. dump_redirect(boolean onoff_flag)
1367. {
1368.     if (dumplog_file) {
1369.         if (onoff_flag) {
1370.             windowprocs.win_create_nhwindow = dump_create_nhwindow;
1371.             windowprocs.win_clear_nhwindow = dump_clear_nhwindow;
1372.             windowprocs.win_display_nhwindow = dump_display_nhwindow;
1373.             windowprocs.win_destroy_nhwindow = dump_destroy_nhwindow;
1374.             windowprocs.win_start_menu = dump_start_menu;
1375.             windowprocs.win_add_menu = dump_add_menu;
1376.             windowprocs.win_end_menu = dump_end_menu;
1377.             windowprocs.win_select_menu = dump_select_menu;
1378.             windowprocs.win_putstr = dump_putstr;
1379.         } else {
1380.             windowprocs = dumplog_windowprocs_backup;
1381.         }
1382.         iflags.in_dumplog = onoff_flag;
1383.     } else {
1384.         iflags.in_dumplog = FALSE;
1385.     }
1386. }
1387. 
1388. #ifdef TTY_GRAPHICS
1389. #ifdef TOS
1390. extern const char *hilites[CLR_MAX];
1391. #else
1392. extern NEARDATA char *hilites[CLR_MAX];
1393. #endif
1394. #endif
1395. 
1396. int
1397. has_color(int color)
1398. {
1399.     return (iflags.use_color && windowprocs.name
1400.             && (windowprocs.wincap & WC_COLOR) && windowprocs.has_color[color]
1401. #ifdef TTY_GRAPHICS
1402. #if defined(TERMLIB) && !defined(NO_TERMS)
1403.              && (hilites[color] != 0)
1404. #endif
1405. #endif
1406.     );
1407. }
1408. 
1409. int
1410. glyph2ttychar(int glyph)
1411. {
1412.     glyph_info glyphinfo;
1413. 
1414.     map_glyphinfo(0, 0, glyph, 0, &glyphinfo);
1415.     return glyphinfo.ttychar;
1416. }
1417. 
1418. int
1419. glyph2symidx(int glyph)
1420. {
1421.     glyph_info glyphinfo;
1422. 
1423.     map_glyphinfo(0, 0, glyph, 0, &glyphinfo);
1424.     return glyphinfo.gm.sym.symidx;
1425. }
1426. 
1427. char *
1428. encglyph(int glyph)
1429. {
1430.     static char encbuf[20]; /* 10+1 would suffice */
1431. 
1432.     Sprintf(encbuf, "\\G%04X%04X", svc.context.rndencode, glyph);
1433.     return encbuf;
1434. }
1435. 
1436. /* hexdd[] is defined in decl.c */
1437. 
1438. int
1439. decode_glyph(const char *str, int *glyph_ptr)
1440. {
1441.     int rndchk = 0, dcount = 0, retval = 0;
1442.     const char *dp;
1443. 
1444.     for (; *str && ++dcount <= 4; ++str) {
1445.         if ((dp = strchr(hexdd, *str)) != 0) {
1446.             retval++;
1447.             rndchk = (rndchk * 16) + ((int) (dp - hexdd) / 2);
1448.         } else
1449.             break;
1450.     }
1451.     if (rndchk == svc.context.rndencode) {
1452.         *glyph_ptr = dcount = 0;
1453.         for (; *str && ++dcount <= 4; ++str) {
1454.             if ((dp = strchr(hexdd, *str)) != 0) {
1455.                 retval++;
1456.                 *glyph_ptr = (*glyph_ptr * 16) + ((int) (dp - hexdd) / 2);
1457.             } else
1458.                 break;
1459.         }
1460.         return retval;
1461.     }
1462.     return 0;
1463. }
1464. 
1465. char *
1466. decode_mixed(char *buf, const char *str)
1467. {
1468.     char *put = buf;
1469.     glyph_info glyphinfo = nul_glyphinfo;
1470. 
1471.     if (!str)
1472.         return strcpy(buf, "");
1473. 
1474.     while (*str) {
1475.         if (*str == '\\') {
1476.             int dcount, so, ggv;
1477.             const char *save_str;
1478. 
1479.             save_str = str++;
1480.             switch (*str) {
1481.             case 'G': /* glyph value \GXXXXNNNN*/
1482.                 if ((dcount = decode_glyph(str + 1, &ggv))) {
1483.                     str += (dcount + 1);
1484.                     map_glyphinfo(0, 0, ggv, 0, &glyphinfo);
1485.                     so = glyphinfo.gm.sym.symidx;
1486.                     *put++ = gs.showsyms[so];
1487.                     /* 'str' is ready for the next loop iteration and '*str'
1488.                        should not be copied at the end of this iteration */
1489.                     continue;
1490.                 } else {
1491.                     /* possible forgery - leave it the way it is */
1492.                     str = save_str;
1493.                 }
1494.                 break;
1495.             case '\\':
1496.                 break;
1497.             case '\0':
1498.                 /* String ended with '\\'.  This can happen when someone
1499.                    names an object with a name ending with '\\', drops the
1500.                    named object on the floor nearby and does a look at all
1501.                    nearby objects. */
1502.                 /* brh - should we perhaps not allow things to have names
1503.                    that contain '\\' */
1504.                 str = save_str;
1505.                 break;
1506.             }
1507.         }
1508.         *put++ = *str++;
1509.     }
1510.     *put = '\0';
1511.     return buf;
1512. }
1513. 
1514. 
1515. /*
1516.  * This differs from putstr() because the str parameter can
1517.  * contain a sequence of characters representing:
1518.  *        \GXXXXNNNN    a glyph value, encoded by encglyph().
1519.  *
1520.  * For window ports that haven't yet written their own
1521.  * XXX_putmixed() routine, this general one can be used.
1522.  * It replaces the encoded glyph sequence with a single
1523.  * showsyms[] char, then just passes that string onto
1524.  * putstr().
1525.  */
1526. 
1527. void
1528. genl_putmixed(winid window, int attr, const char *str)
1529. {
1530.     char buf[BUFSZ];
1531. 
1532.     /* now send it to the normal putstr */
1533.     putstr(window, attr, decode_mixed(buf, str));
1534. }
1535. 
1536. /* possibly called to show usage info during command line processing when
1537.    an interface hasn't yet been chosen and set up */
1538. void
1539. genl_display_file(const char *fname, boolean complain)
1540. {
1541.     char buf[BUFSZ];
1542.     dlb *f = dlb_fopen(fname, "r");
1543. 
1544.     if (!f) {
1545.         if (complain) /* send complaint to stdout rather than to stderr */
1546.             fprintf(stdout, "\nCannot open \"%s\".\n", fname);
1547.     } else {
1548.         /* straight copy to stdout, no pagination or other interaction */
1549.         while (dlb_fgets(buf, BUFSZ, f)) {
1550.             if (fputs(buf, stdout) < 0)
1551.                 break;
1552.         }
1553.         (void) dlb_fclose(f);
1554.     }
1555. }
1556. 
1557. /*
1558.  * Window port helper function for menu invert routines to move the decision
1559.  * logic into one place instead of 7 different window-port routines.
1560.  */
1561. boolean
1562. menuitem_invert_test(
1563.     int mode UNUSED,        /* 0: invert; 1: select; 2: deselect */
1564.     unsigned itemflags,     /* itemflags for the item */
1565.     boolean is_selected)    /* current selection status of the item */
1566. {
1567.     boolean skipinvert = (itemflags & MENU_ITEMFLAGS_SKIPINVERT) != 0;
1568. 
1569.     if (!skipinvert) /* if not flagged SKIPINVERT, always pass test */
1570.         return TRUE;
1571.     /*
1572.      * mode 0: inverting current on/off state;
1573.      *      1: unconditionally setting on;
1574.      *      2: unconditionally setting off.
1575.      * menuinvertmode 0: treat entries flagged with skipinvert as ordinary
1576.      *                   (same as if not flagged);
1577.      * menuinvertmode 1: don't toggle bulk invert or bulk select entries On;
1578.      *                   allow toggling to Off (for invert and deselect;
1579.      *                   select doesn't do Off);
1580.      * menuinvertmode 2: don't toggle skipinvert entries either On or Off
1581.      *                   when any bulk change is performed.
1582.      */
1583.     if (iflags.menuinvertmode == 2) {
1584.         return FALSE;
1585.     } else if (iflags.menuinvertmode == 1) {
1586.         return is_selected ? TRUE : FALSE;
1587.     }
1588.     return TRUE;
1589. }
1590. 
1591. /*
1592.  * helper routine if a window port wants to extract the glyph
1593.  * information from a glyph number representation in the string;
1594.  * the returned string is the remainder of the string after
1595.  * extracting the \GNNNNNNNN information. The glyph details,
1596.  * including the utf8 representation under ENHANCED_SYMBOLS,
1597.  * will be stored in the glyph_info struct pointed to by gip.
1598.  */
1599. const char *
1600. mixed_to_glyphinfo(const char *str, glyph_info *gip)
1601. {
1602.     int dcount, ggv;
1603. 
1604.     if (!str || !gip)
1605.         return " ";
1606. 
1607.     *gip = nul_glyphinfo;
1608.     if (*str == '\\' && *(str + 1) == 'G') {
1609.         if ((dcount = decode_glyph(str + 2, &ggv))) {
1610.             map_glyphinfo(0, 0, ggv, 0, gip);
1611.             /* 'str' is ready for the next loop iteration and
1612.                 '*str' should not be copied at the end of this
1613.                 iteration */
1614.             str += (dcount + 2);
1615.         }
1616.     }
1617.     return str;
1618. }
1619. 
1620. /*
1621.  * This is a somewhat generic menu for taking a list of NetHack style
1622.  * class choices and presenting them via a description
1623.  * rather than the traditional NetHack characters.
1624.  * (Benefits users whose first exposure to NetHack is via tiles).
1625.  *
1626.  * prompt
1627.  *           The title at the top of the menu.
1628.  *
1629.  * category: 0 = monster class
1630.  *           1 = object  class
1631.  *
1632.  * way
1633.  *           FALSE = PICK_ONE, TRUE = PICK_ANY
1634.  *
1635.  * class_list
1636.  *           a null terminated string containing the list of choices.
1637.  *
1638.  * class_selection
1639.  *           a null terminated string containing the selected characters.
1640.  *
1641.  * Returns number selected.
1642.  */
1643. int
1644. choose_classes_menu(const char *prompt,
1645.                     int category,
1646.                     boolean way,
1647.                     char *class_list,
1648.                     char *class_select)
1649. {
1650.     menu_item *pick_list = (menu_item *) 0;
1651.     winid win;
1652.     anything any;
1653.     char buf[BUFSZ];
1654.     const char *text = 0;
1655.     boolean selected;
1656.     int ret, i, n, next_accelerator, accelerator = 0;
1657.     int clr = NO_COLOR;
1658. 
1659.     if (!class_list || !class_select)
1660.         return 0;
1661.     next_accelerator = 'a';
1662.     any = cg.zeroany;
1663.     win = create_nhwindow(NHW_MENU);
1664.     start_menu(win, MENU_BEHAVE_STANDARD);
1665.     while (*class_list) {
1666.         int idx;
1667. 
1668.         selected = FALSE;
1669.         switch (category) {
1670.         case 0:
1671.             idx = def_char_to_monclass(*class_list);
1672.             if (!IndexOk(idx, def_monsyms)) {
1673.                 panic("choose_classes_menu: invalid monclass '%c'",
1674.                       *class_list);
1675.                 /*NOTREACHED*/
1676.             }
1677.             text = def_monsyms[idx].explain;
1678.             accelerator = *class_list;
1679.             Sprintf(buf, "%s", text);
1680.             break;
1681.         case 1:
1682.             idx = def_char_to_objclass(*class_list);
1683.             if (!IndexOk(idx, def_oc_syms)) {
1684.                 panic("choose_classes_menu: invalid objclass '%c'",
1685.                       *class_list);
1686.                 /*NOTREACHED*/
1687.             }
1688.             text = def_oc_syms[idx].explain;
1689.             accelerator = next_accelerator;
1690.             Sprintf(buf, "%c  %s", *class_list, text);
1691.             break;
1692.         default:
1693.             panic("choose_classes_menu: invalid category %d", category);
1694.             /*NOTREACHED*/
1695.         }
1696.         if (way && *class_select) { /* Selections there already */
1697.             if (strchr(class_select, *class_list)) {
1698.                 selected = TRUE;
1699.             }
1700.         }
1701.         any.a_int = *class_list;
1702.         add_menu(win, &nul_glyphinfo, &any, accelerator,
1703.                  category ? *class_list : 0, ATR_NONE, clr, buf,
1704.                  selected ? MENU_ITEMFLAGS_SELECTED : MENU_ITEMFLAGS_NONE);
1705.         if (category > 0) {
1706.             if (next_accelerator == 'Z')
1707.                 break;
1708.             else if (next_accelerator == 'z')
1709.                 next_accelerator = 'A';
1710.             else
1711.                 ++next_accelerator;
1712.         }
1713.         ++class_list;
1714.     }
1715.     if (category == 1 && next_accelerator <= 'z') {
1716.         /* for objects, add "A - ' '  all classes", after a separator */
1717.         add_menu_str(win, "");
1718.         any = cg.zeroany;
1719.         any.a_int = (int) ' ';
1720.         Sprintf(buf, "%c  %s", (char) any.a_int, "All classes of objects");
1721.         /* we won't preselect this even if the incoming list is empty;
1722.            having it selected means that it would have to be explicitly
1723.            de-selected in order to select anything else */
1724.         add_menu(win, &nul_glyphinfo, &any, 'A', 0,
1725.                  ATR_NONE, clr, buf, MENU_ITEMFLAGS_SKIPINVERT);
1726.         if (!strcmp(prompt, "Autopickup what?")) {
1727.             add_menu_str(win,
1728.                    "Note: when no choices are selected, \"all\" is implied.");
1729.             /* for 'O', "toggle" should be intuitive; for 'm O', it would
1730.                probably be better to say "Set 'autopickup' to true|false" */
1731.             add_menu_str(win, flags.pickup
1732.                         ? "Toggle off 'autopickup' to not pick up anything."
1733.            : "Toggle on 'autopickup' to automatically pick these things up.");
1734.         }
1735.     }
1736.     end_menu(win, prompt);
1737.     n = select_menu(win, way ? PICK_ANY : PICK_ONE, &pick_list);
1738.     destroy_nhwindow(win);
1739.     if (n > 0) {
1740.         if (category == 1) {
1741.             /* for object classes, first check for 'all'; it means 'use
1742.                a blank list' rather than 'collect every possible choice' */
1743.             for (i = 0; i < n; ++i)
1744.                 if (pick_list[i].item.a_int == ' ') {
1745.                     pick_list[0].item.a_int = ' ';
1746.                     n = 1; /* return 1; also an implicit 'break;' */
1747.                 }
1748.         }
1749.         for (i = 0; i < n; ++i)
1750.             *class_select++ = (char) pick_list[i].item.a_int;
1751.         free((genericptr_t) pick_list);
1752.         ret = n;
1753.     } else if (n == -1) {
1754.         class_select = eos(class_select);
1755.         ret = -1;
1756.     } else {
1757.         ret = 0;
1758.     }
1759.     *class_select = '\0';
1760.     return ret;
1761. }
1762. 
1763. /* enum and structs are defined in wintype.h */
1764. 
1765. win_request_info zerowri = { { 0L, 0, 0, 0, 0, 0, 0, 0 },
1766.                              { 0, 0, { NO_COLOR, ATR_NONE }}};
1767. 
1768. void
1769. adjust_menu_promptstyle(winid window, color_attr *style)
1770. {
1771.     win_request_info wri = zerowri;
1772.     wri.fromcore.menu_promptstyle.color = style->color;
1773.     wri.fromcore.menu_promptstyle.attr = style->attr;
1774.     /*  relay the style change to the window port */
1775.     (void) ctrl_nhwindow(window, set_menu_promptstyle, &wri);
1776.     go.opt_need_promptstyle = FALSE;
1777. }
1778. 
1779. /*
1780.  *   Common code point leading into the interface-specific
1781.  *   add_menu() to allow single-spot adjustments to the parameters,
1782.  *   such as those done by menu_colors.
1783.  */
1784. void
1785. add_menu(
1786.     winid window,  /* window to use, must be of type NHW_MENU */
1787.     const glyph_info *glyphinfo, /* glyph info with glyph to
1788.                                   * display with item */
1789.     const anything *identifier, /* what to return if selected */
1790.     char ch,                    /* selector letter (0 = pick our own) */
1791.     char gch,                   /* group accelerator (0 = no group) */
1792.     int attr,                   /* attribute for menu text (str) */
1793.     int color,                  /* color for menu text (str) */
1794.     const char *str,            /* menu text */
1795.     unsigned int itemflags)     /* itemflags such as MENU_ITEMFLAGS_SELECTED */
1796. {
1797.     if (!str) {
1798.         /* if 'str' is Null, just return without adding any menu entry */
1799.         debugpline0("add_menu(Null)");
1800.         return;
1801.     }
1802. 
1803.     if (iflags.use_menu_color) {
1804.         if ((itemflags & MENU_ITEMFLAGS_SKIPMENUCOLORS) == 0)
1805.             (void) get_menu_coloring(str, &color, &attr);
1806.     }
1807.     /* this is the only function that cared about this flag; remove it now */
1808.     itemflags &= ~MENU_ITEMFLAGS_SKIPMENUCOLORS;
1809. 
1810.     (*windowprocs.win_add_menu)(window, glyphinfo, identifier,
1811.                                 ch, gch, attr, color, str, itemflags);
1812. }
1813. 
1814. /* insert a non-selectable, possibly highlighted line of text into a menu */
1815. void
1816. add_menu_heading(winid tmpwin, const char *buf)
1817. {
1818.     anything any = cg.zeroany;
1819.     int attr = iflags.menu_headings.attr,
1820.         color = iflags.menu_headings.color;
1821. 
1822.     /* suppress highlighting during end-of-game disclosure */
1823.     if (program_state.gameover)
1824.         attr = ATR_NONE, color = NO_COLOR;
1825. 
1826.     add_menu(tmpwin, &nul_glyphinfo, &any, '\0', '\0', attr, color,
1827.              buf, MENU_ITEMFLAGS_SKIPMENUCOLORS);
1828. }
1829. 
1830. /* insert a non-selectable, unhighlighted line of text into a menu */
1831. void
1832. add_menu_str(winid tmpwin, const char *buf)
1833. {
1834.     anything any = cg.zeroany;
1835. 
1836.     add_menu(tmpwin, &nul_glyphinfo, &any, '\0', '\0', ATR_NONE, NO_COLOR,
1837.              buf, MENU_ITEMFLAGS_NONE);
1838. }
1839. 
1840. staticfn boolean
1841. get_menu_coloring(const char *str, int *color, int *attr)
1842. {
1843.     struct menucoloring *tmpmc;
1844. 
1845.     if (iflags.use_menu_color)
1846.         for (tmpmc = gm.menu_colorings; tmpmc; tmpmc = tmpmc->next)
1847.             if (regex_match(str, tmpmc->match)) {
1848.                 *color = tmpmc->color;
1849.                 *attr = tmpmc->attr;
1850.                 return TRUE;
1851.             }
1852.     return FALSE;
1853. }
1854. 
1855. int
1856. select_menu(winid window, int how, menu_item **menu_list)
1857. {
1858.     int reslt;
1859.     boolean old_bot_disabled = gb.bot_disabled;
1860. 
1861.     gb.bot_disabled = TRUE;
1862.     reslt = (*windowprocs.win_select_menu)(window, how, menu_list);
1863.     gb.bot_disabled = old_bot_disabled;
1864.     return reslt;
1865. }
1866. 
1867. void
1868. getlin(const char *query, char *bufp)
1869. {
1870.     boolean old_bot_disabled = gb.bot_disabled;
1871.     char *obufp = bufp;
1872.     boolean got_cmdq = FALSE;
1873.     struct _cmd_queue *cmdq = NULL;
1874. 
1875.     while ((cmdq = cmdq_pop()) != 0) {
1876.         if (cmdq->typ == CMDQ_KEY) {
1877.             got_cmdq = TRUE;
1878.             *bufp = (cmdq->key != '\n') ? cmdq->key : '\0';
1879.             bufp++;
1880.             if (cmdq->key == '\n')
1881.                 break;
1882.         } else {
1883.             break;
1884.         }
1885.         free(cmdq);
1886.         cmdq = NULL;
1887.     }
1888.     if (cmdq)
1889.         free(cmdq);
1890. 
1891.     if (got_cmdq) {
1892.         *bufp = '\0';
1893.         pline("%s %s", query, obufp);
1894.         return;
1895.     }
1896. 
1897.     program_state.in_getlin = 1;
1898.     gb.bot_disabled = TRUE;
1899.     (*windowprocs.win_getlin)(query, bufp);
1900.     gb.bot_disabled = old_bot_disabled;
1901.     program_state.in_getlin = 0;
1902. }
1903. /*windows.c*/