Source:NetHack 5.0.0/src/earlyarg.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to earlyarg.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/earlyarg.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	earlyarg.c	$NHDT-Date: 1771213100 2026/02/15 19:38:20 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.286 $ */
2. /* Copyright (c) Robert Patrick Rankin, 2012. */
3. /* NetHack may be freely redistributed.  See license for details. */
4. 
5. #include "hack.h"
6. #include "dlb.h"
7. 
8. staticfn void debug_fields(char *);
9. #ifndef NODUMPENUMS
10. staticfn void dump_enums(void);
11. #endif
12. ATTRNORETURN staticfn void opt_terminate(void) NORETURN;
13. ATTRNORETURN staticfn void opt_usage(const char *) NORETURN;
14. ATTRNORETURN staticfn void scores_only(int, char **, const char *) NORETURN;
15. staticfn char *lopt(char *, int, const char *, const char *, int *, char ***);
16. staticfn void consume_arg(int, int *, char ***);
17. staticfn void consume_two_args(int, int *, char ***);
18. 
19. #ifdef UNIX
20. extern boolean whoami(void);
21. #endif
22. 
23. /*
24.  * Argument processing helpers - for xxmain() to share
25.  * and call.
26.  *
27.  * These should return TRUE if the argument matched,
28.  * whether the processing of the argument was
29.  * successful or not.
30.  *
31.  * Most of these do their thing, then after returning
32.  * to xxmain(), the code exits without starting a game.
33.  *
34.  */
35. 
36. static const struct early_opt earlyopts[] = {
37.     { ARG_DEBUG, "debug", 5, TRUE },
38.     { ARG_VERSION, "version", 4, TRUE },
39.     { ARG_SHOWPATHS, "showpaths", 8, FALSE },
40. #ifndef NODUMPENUMS
41.     { ARG_DUMPENUMS, "dumpenums", 9, FALSE },
42. #endif
43.     { ARG_DUMPGLYPHIDS, "dumpglyphids", 12, FALSE },
44.     { ARG_DUMPMONGEN, "dumpmongen", 10, FALSE },
45.     { ARG_DUMPWEIGHTS, "dumpweights", 11, FALSE },
46. #ifdef WIN32
47.     { ARG_WINDOWS, "windows", 4, TRUE },
48. #endif
49. #if defined(CRASHREPORT)
50.     { ARG_BIDSHOW, "bidshow", 7, FALSE },
51. #endif
52. };
53. 
54. static char ArgVal_novalue[] = "[nothing]"; /* note: not 'const' */
55. 
56. enum cmdlinearg {
57.     ArgValRequired = 0,
58.     ArgValOptional = 1,
59.     ArgValDisallowed = 2,
60.     ArgVal_mask = (1 | 2),
61.     ArgNamOneLetter = 4,
62.     ArgNam_mask = 4,
63.     ArgErrSilent = 0,
64.     ArgErrComplain = 8,
65.     ArgErr_mask = 8
66. };
67. 
68. /* approximate 'getopt_long()' for one option; all the comments refer to
69.    "-windowtype" but the code isn't specific to that  */
70. staticfn char *
71. lopt(char *arg,  /* command line token; beginning matches 'optname' */
72.      int lflags, /* cmdlinearg | errorhandling */
73.      const char *optname, /* option's name; "-windowtype" in examples below */
74.      const char *origarg, /* 'arg' might have had a dash prefix removed */
75.      int *argc_p,         /* argc that can have changes passed to caller */
76.      char ***argv_p)      /* argv[] ditto */
77. {
78.     int argc = *argc_p;
79.     char **argv = *argv_p;
80.     char *p, *nextarg = (argc > 1 && argv[1][0] != '-') ? argv[1] : 0;
81.     int l, opttype = (lflags & ArgVal_mask);
82.     boolean oneletterok = ((lflags & ArgNam_mask) == ArgNamOneLetter),
83.             complain = ((lflags & ArgErr_mask) == ArgErrComplain);
84. 
85.     /* first letter must match */
86.     if (arg[1] != optname[1]) {
87.     loptbail:
88.         if (complain)
89.             config_error_add("Unknown option: %.60s", origarg);
90.         return (char *) 0;
91.     loptnotallowed:
92.         if (complain)
93.             config_error_add("Value not allowed: %.60s", origarg);
94.         return (char *) 0;
95.     loptrequired:
96.         if (complain)
97.             config_error_add("Missing required value: %.60s", origarg);
98.         return (char *) 0;
99.     }
100. 
101.     if ((p = strchr(arg, '=')) == 0)
102.         p = strchr(arg, ':');
103.     if (p && opttype == ArgValDisallowed)
104.         goto loptnotallowed;
105. 
106.     l = (int) (p ? (long) (p - arg) : (long) strlen(arg));
107.     if ((l > 2 || oneletterok) && !strncmp(arg, optname, l)) {
108.         /* "-windowtype[=foo]" */
109.         if (p)
110.             ++p; /* past '=' or ':' */
111.         else if (opttype == ArgValRequired)
112.             p = eos(arg); /* we have "-w[indowtype]" w/o "=foo"
113.                            * so we'll take foo from next element */
114.         else
115.             return ArgVal_novalue;
116.     } else if (oneletterok) {
117.         /* "-w..." but not "-w[indowtype[=foo]]" */
118.         if (!p) {
119.             p = &arg[2]; /* past 'w' of "-wfoo" */
120. #if 0 /* -x:value could work but is not supported (callers don't expect it) \
121.        */
122.         } else if (p == arg + 2) {
123.             ++p; /* past ':' of "-w:foo" */
124. #endif
125.         } else {
126.             /* "-w...=foo" but not "-w[indowtype]=foo" */
127.             goto loptbail;
128.         }
129.     } else {
130.         goto loptbail;
131.     }
132.     if (!p || !*p) {
133.         /* "-w[indowtype]" w/o '='/':' if there is a next element, use
134.            it for "foo"; if not, supply a non-Null bogus value */
135.         if (nextarg
136.             && (opttype == ArgValRequired || opttype == ArgValOptional))
137.             p = nextarg, --(*argc_p), ++(*argv_p);
138.         else if (opttype == ArgValRequired)
139.             goto loptrequired;
140.         else
141.             p = ArgVal_novalue; /* there is no next element */
142.     }
143.     return p;
144. }
145. /* move argv[ndx] to end of argv[] array, then reduce argc to hide it;
146.    prevents process_options() from encountering it after early_options()
147.    has processed it; elements get reordered but all remain intact */
148. staticfn void
149. consume_arg(int ndx, int *ac_p, char ***av_p)
150. {
151.     char *gone, **av = *av_p;
152.     int i, ac = *ac_p;
153. 
154.     /* "-one -two -three -four" -> "-two -three -four -one" */
155.     if (ac > 2) {
156.         gone = av[ndx];
157.         for (i = ndx + 1; i < ac; ++i)
158.             av[i - 1] = av[i];
159.         av[ac - 1] = gone;
160.     }
161.     --(*ac_p);
162. }
163. 
164. /* consume two tokens for '-argname value' w/o '=' or ':' */
165. staticfn void
166. consume_two_args(int ndx, int *ac_p, char ***av_p)
167. {
168.     /* when consuming "-two arg" from "-two arg -three -four",
169.        the *ac_p manipulation results in "-three -four -two arg"
170.        rather than the "-three -four arg -two" that would happen
171.        with just two ordinary consume_arg() calls */
172.     consume_arg(ndx, ac_p, av_p);
173.     ++(*ac_p); /* bring the final slot back into view */
174.     consume_arg(ndx, ac_p, av_p);
175.     --(*ac_p); /* take away restored slot */
176. }
177. 
178. /* process some command line arguments before loading options */
179. void
180. early_options(int *argc_p, char ***argv_p, char **hackdir_p)
181. {
182.     char **argv, *arg, *origarg;
183.     int argc, oldargc, ndx = 0, consumed = 0;
184. 
185. #ifdef ENHANCED_SYMBOLS
186.     if (argcheck(*argc_p, *argv_p, ARG_DUMPGLYPHIDS) == 2)
187.         opt_terminate();
188. #endif
189. 
190.     config_error_init(FALSE, "command line", FALSE);
191. 
192.     /* treat "nethack ?" as a request for usage info; due to shell
193.        processing, player likely has to use "nethack \?" or "nethack '?'"
194.        [won't work if used as "nethack -dpath ?" or "nethack -d path ?"] */
195.     if (*argc_p > 1 && !strcmp((*argv_p)[1], "?"))
196.         opt_usage(*hackdir_p); /* doesn't return */
197. 
198.     /*
199.      * Both *argc_p and *argv_p account for the program name as (*argv_p)[0];
200.      * local argc and argv implicitly discard that (by starting 'ndx' at 1).
201.      * argcheck() doesn't mind, prscore() (via scores_only()) does (for the
202.      * number of args it gets passed, not for the value of argv[0]).
203.      */
204.     for (ndx = 1; ndx < *argc_p; ndx += (consumed ? 0 : 1)) {
205.         consumed = 0;
206.         argc = *argc_p - ndx;
207.         argv = *argv_p + ndx;
208. 
209.         arg = origarg = argv[0];
210.         /* skip any args intended for deferred options */
211.         if (*arg != '-')
212.             continue;
213.         /* allow second dash if arg name is longer than one character */
214.         if (arg[0] == '-' && arg[1] == '-' && arg[2] != '\0'
215.             && (arg[3] != '\0' && arg[3] != '=' && arg[3] != ':'))
216.             ++arg;
217. 
218.         switch (arg[1]) { /* char after leading dash */
219.         case 'b':
220. #ifdef CRASHREPORT
221.             // --bidshow
222.             if (argcheck(argc, argv, ARG_BIDSHOW) == 2) {
223.                 opt_terminate();
224.                 /*NOTREACHED*/
225.             }
226. #endif
227.             break;
228.         case 'd':
229.             if (argcheck(argc, argv, ARG_DEBUG) == 1) {
230.                 consume_arg(ndx, argc_p, argv_p), consumed = 1;
231. #ifndef NODUMPENUMS
232.             } else if (argcheck(argc, argv, ARG_DUMPENUMS) == 2) {
233.                 opt_terminate();
234.                 /*NOTREACHED*/
235. #endif
236.             } else if (argcheck(argc, argv, ARG_DUMPMONGEN) == 2) {
237.                 opt_terminate();
238.                 /*NOTREACHED*/
239.             } else if (argcheck(argc, argv, ARG_DUMPWEIGHTS) == 2) {
240.                 opt_terminate();
241.                 /*NOTREACHED*/
242.             } else {
243. #ifdef CHDIR
244.                 oldargc = argc;
245.                 arg = lopt(arg,
246.                            (ArgValRequired | ArgNamOneLetter | ArgErrSilent),
247.                            "-directory", origarg, &argc, &argv);
248.                 if (!arg)
249.                     error("Flag -d must be followed by a directory name.");
250.                 if (*arg != 'e') { /* avoid matching -decgraphics or -debug */
251.                     *hackdir_p = arg;
252.                     if (oldargc == argc)
253.                         consume_arg(ndx, argc_p, argv_p), consumed = 1;
254.                     else
255.                         consume_two_args(ndx, argc_p, argv_p), consumed = 2;
256.                 }
257. #endif /* CHDIR */
258.             }
259.             break;
260.         case 'h':
261.         case '?':
262.             if (lopt(arg, ArgValDisallowed, "-help", origarg, &argc, &argv)
263.                 || lopt(arg, ArgValDisallowed | ArgNamOneLetter, "-?",
264.                         origarg, &argc, &argv))
265.                 opt_usage(*hackdir_p); /* doesn't return */
266.             break;
267.         case 'n':
268.             oldargc = argc;
269.             if (!strcmp(arg, "-no-nethackrc")) /* no abbreviation allowed */
270.                 arg = nhStr("/dev/null");
271.             else
272.                 arg = lopt(arg, (ArgValRequired | ArgErrComplain),
273.                            "-nethackrc", origarg, &argc, &argv);
274.             if (arg) {
275.                 gc.cmdline_rcfile = dupstr(arg);
276.                 if (oldargc == argc)
277.                     consume_arg(ndx, argc_p, argv_p), consumed = 1;
278.                 else
279.                     consume_two_args(ndx, argc_p, argv_p), consumed = 2;
280.             }
281.             break;
282.         case 's':
283.             if (argcheck(argc, argv, ARG_SHOWPATHS) == 2) {
284.                 gd.deferred_showpaths = TRUE;
285.                 gd.deferred_showpaths_dir = *hackdir_p;
286.                 config_error_done();
287.                 return;
288.             }
289.             /* check for "-s" request to show scores */
290.             if (lopt(arg,
291.                      ((ArgValDisallowed | ArgErrComplain)
292.                       /* only accept one-letter if there is just one
293.                          dash; reject "--s" because prscore() via
294.                          scores_only() doesn't understand it */
295.                       | ((origarg[1] != '-') ? ArgNamOneLetter : 0)),
296.                      /* [ought to omit val-disallowed and accept
297.                         --scores=foo since -s foo and -sfoo are
298.                         allowed, but -s form can take more than one
299.                         space-separated argument and --scores=foo
300.                         isn't suited for that] */
301.                      "-scores", origarg, &argc, &argv)) {
302.                 /* at this point, argv[0] contains "-scores" or a leading
303.                    substring of it; prscore() (via scores_only()) expects
304.                    that to be in argv[1] so we adjust the pointer to make
305.                    that be the case; if there are any non-early args waiting
306.                    to be passed along to process_options(), the resulting
307.                    argv[0] will be one of those rather than the program
308.                    name but prscore() doesn't care */
309.                 scores_only(argc + 1, argv - 1, *hackdir_p);
310.                 /*NOTREACHED*/
311.             }
312.             break;
313.         case 'u':
314. #if defined(UNIX)
315.             if (lopt(arg, ArgValDisallowed, "-usage", origarg, &argc, &argv))
316.                 opt_usage(*hackdir_p);
317. #elif defined(WIN32) || defined(MSDOS) || defined(AMIGA)
318.             if (arg[2]) {
319.                 (void) strncpy(svp.plname, arg + 2, sizeof(svp.plname) - 1);
320.             } else if (ndx + 1 < *argc_p) {
321.                 const char *nextarg = (*argv_p)[ndx + 1];
322. 
323.                 if (nextarg[0] != '-') {
324.                     (void) strncpy(svp.plname, nextarg, sizeof(svp.plname) - 1);
325.                 } else {
326.                     raw_print("Player name expected after -u\n");
327.                 }
328.             }
329. #endif
330.             break;
331.         case 'v':
332.             if (argcheck(argc, argv, ARG_VERSION) == 2) {
333.                 opt_terminate();
334.                 /*NOTREACHED*/
335.             }
336.             break;
337.         case 'w': /* windowtype: "-wfoo" or "-w[indowtype]=foo"
338.                    * or "-w[indowtype]:foo" or "-w[indowtype] foo" */
339.             arg =
340.                 lopt(arg, (ArgValRequired | ArgNamOneLetter | ArgErrComplain),
341.                      "-windowtype", origarg, &argc, &argv);
342.             if (gc.cmdline_windowsys)
343.                 free((genericptr_t) gc.cmdline_windowsys);
344.             gc.cmdline_windowsys = arg ? dupstr(arg) : NULL;
345.             break;
346.  #if !defined(UNIX) && !defined(VMS)
347.         case 'D':
348.             wizard = TRUE, discover = FALSE;
349.             break;
350.         case 'X':
351.             discover = TRUE, wizard = FALSE;
352.             break;
353. #endif
354.         default:
355.             break;
356.         }
357.     }
358.     /* empty or "N errors on command line" */
359.     config_error_done();
360.     return;
361. }
362. /* for command-line options that perform some immediate action and then
363.    terminate the program without starting play, like 'nethack --version'
364.    or 'nethack -s Zelda'; do some cleanup before that termination */
365. ATTRNORETURN staticfn void
366. opt_terminate(void)
367. {
368.     program_state.early_options = 0;
369.     config_error_done(); /* free memory allocated by config_error_init() */
370. 
371.     nh_terminate(EXIT_SUCCESS);
372.     /*NOTREACHED*/
373. }
374. 
375. ATTRNORETURN staticfn void
376. opt_usage(const char *hackdir)
377. {
378. #ifdef CHDIR
379.     chdirx(hackdir, TRUE);
380. #else
381.     nhUse(hackdir);
382. #endif
383.     dlb_init();
384. 
385.     genl_display_file(USAGEHELP, TRUE);
386.     opt_terminate();
387. }
388. /* show the sysconf file name, playground directory, run-time configuration
389.    file name, dumplog file name if applicable, and some other things */
390. ATTRNORETURN void
391. after_opt_showpaths(const char *dir)
392. {
393. #ifdef CHDIR
394.     chdirx(dir, FALSE);
395. #else
396.     nhUse(dir);
397. #endif
398.     opt_terminate();
399.     /*NOTREACHED*/
400. }
401. 
402. /* handle "-s <score options> [character-names]" to show all the entries
403.    in the high scores file ('record') belonging to particular characters;
404.    nethack will end after doing so without starting play */
405. ATTRNORETURN staticfn void
406. scores_only(int argc, char **argv, const char *dir)
407. {
408.     /* do this now rather than waiting for final termination, in case there
409.        is an error summary coming */
410.     config_error_done();
411. 
412. #ifdef CHDIR
413.     chdirx(dir, FALSE);
414. #else
415.     nhUse(dir);
416. #endif
417. #ifdef SYSCF
418.     iflags.initoptions_noterminate = TRUE;
419.     initoptions(); /* sysconf options affect whether panictrace is enabled */
420.     iflags.initoptions_noterminate = FALSE;
421. #endif
422. #ifdef PANICTRACE
423.     ARGV0 = gh.hname; /* save for possible stack trace */
424. #ifndef NO_SIGNAL
425.     panictrace_setsignals(TRUE);
426. #endif
427. #endif
428. #ifdef UNIX
429.     (void) whoami(); /* set up default plname[] */
430. #endif
431.     prscore(argc, argv);
432. #ifdef MSWIN_GRAPHICS
433.     /* NetHackW can also support WINDOWPORT(curses) now, so check */
434.     if (WINDOWPORT(mswin)) {
435.         wait_synch();
436.     }
437. #endif
438. 
439.     nh_terminate(EXIT_SUCCESS); /* bypass opt_terminate() */
440.     /*NOTREACHED*/
441. }
442. 
443. /*
444.  * Returns:
445.  *    0 = no match
446.  *    1 = found and skip past this argument
447.  *    2 = found and trigger immediate exit
448.  */
449. int
450. argcheck(int argc, char *argv[], enum earlyarg e_arg)
451. {
452.     int i, idx;
453.     boolean match = FALSE;
454.     char *userea = (char *) 0;
455.     const char *dashdash = "";
456. 
457.     for (idx = 0; idx < SIZE(earlyopts); idx++) {
458.         if (earlyopts[idx].e == e_arg){
459.             break;
460.         }
461.     }
462.     if (idx >= SIZE(earlyopts) || argc < 1)
463.         return 0;
464. 
465.     for (i = 0; i < argc; ++i) {
466.         if (argv[i][0] != '-')
467.             continue;
468.         if (argv[i][1] == '-') {
469.             userea = &argv[i][2];
470.             dashdash = "-";
471.         } else {
472.             userea = &argv[i][1];
473.         }
474.         match = match_optname(userea, earlyopts[idx].name,
475.                               earlyopts[idx].minlength,
476.                               earlyopts[idx].valallowed);
477.         if (match)
478.             break;
479.     }
480. 
481.     if (match) {
482.         const char *extended_opt = strchr(userea, ':');
483. 
484.         if (!extended_opt)
485.             extended_opt = strchr(userea, '=');
486.         switch(e_arg) {
487.         case ARG_DEBUG:
488.             if (extended_opt) {
489.                 char *cpy_extended_opt;
490. 
491.                 cpy_extended_opt = dupstr(extended_opt);
492.                 debug_fields(cpy_extended_opt + 1);
493.                 free((genericptr_t) cpy_extended_opt);
494.             }
495.             return 1;
496.         case ARG_VERSION: {
497.             boolean insert_into_pastebuf = FALSE;
498. 
499.             if (extended_opt) {
500.                 extended_opt++;
501.                     /* Deprecated in favor of "copy" - remove no later
502.                        than  next major version */
503.                 if (match_optname(extended_opt, "paste", 5, FALSE)) {
504.                     insert_into_pastebuf = TRUE;
505.                 } else if (match_optname(extended_opt, "copy", 4, FALSE)) {
506.                     insert_into_pastebuf = TRUE;
507.                 } else if (match_optname(extended_opt, "dump", 4, FALSE)) {
508.                     /* version number plus enabled features and sanity
509.                        values that the program compares against the same
510.                        thing recorded in save and bones files to check
511.                        whether they're being used compatibly */
512.                     dump_version_info();
513.                     return 2; /* done */
514.                 } else if (!match_optname(extended_opt, "show", 4, FALSE)) {
515.                     raw_printf("-%sversion can only be extended with"
516.                                " -%sversion:copy or :dump or :show.\n",
517.                                dashdash, dashdash);
518.                     /* exit after we've reported bad command line argument */
519.                     return 2;
520.                 }
521.             }
522.             early_version_info(insert_into_pastebuf);
523.             return 2;
524.         }
525.         case ARG_SHOWPATHS:
526.             return 2;
527. #ifndef NODUMPENUMS
528.         case ARG_DUMPENUMS:
529.             dump_enums();
530.             return 2;
531. #endif
532.         case ARG_DUMPGLYPHIDS:
533.             dump_glyphids();
534.             return 2;
535.         case ARG_DUMPMONGEN:
536.             dump_mongen();
537.             return 2;
538.         case ARG_DUMPWEIGHTS:
539.             dump_weights();
540.             return 2;
541. #ifdef CRASHREPORT
542.         case ARG_BIDSHOW:
543.             crashreport_bidshow();
544.             return 2;
545. #endif
546. #ifdef WIN32
547.         case ARG_WINDOWS:
548.             if (extended_opt) {
549.                 extended_opt++;
550.                 return windows_early_options(extended_opt);
551.             }
552.         FALLTHROUGH;
553.         /*FALLTHRU*/
554. #endif
555.         default:
556.             break;
557.         }
558.     };
559.     return 0;
560. }
561. 
562. /*
563.  * These are internal controls to aid developers with
564.  * testing and debugging particular aspects of the code.
565.  * They are not player options and the only place they
566.  * are documented is right here. No gameplay is altered.
567.  *
568.  * test             - test whether this parser is working
569.  * ttystatus        - TTY:
570.  * immediateflips   - WIN32: turn off display performance
571.  *                    optimization so that display output
572.  *                    can be debugged without buffering.
573.  * fuzzer           - enable fuzzer without debugger intervention.
574.  */
575. staticfn void
576. debug_fields(char *opts)
577. {
578.     char *op;
579.     boolean negated = FALSE;
580. 
581.     while ((op = strchr(opts, ',')) != 0) {
582.         *op++ = 0;
583.         /* recurse */
584.         debug_fields(op);
585.     }
586.     if (strlen(opts) > BUFSZ / 2)
587.         return;
588. 
589. 
590.     /* strip leading and trailing white space */
591.     while (isspace((uchar) *opts))
592.         opts++;
593.     op = eos((char *) opts);
594.     while (--op >= opts && isspace((uchar) *op))
595.         *op = '\0';
596. 
597.     if (!*opts) {
598.         /* empty */
599.         return;
600.     }
601.     while ((*opts == '!') || !strncmpi(opts, "no", 2)) {
602.         if (*opts == '!')
603.             opts++;
604.         else
605.             opts += 2;
606.         negated = !negated;
607.     }
608.     if (match_optname(opts, "test", 4, FALSE))
609.         iflags.debug.test = negated ? FALSE : TRUE;
610. #ifdef TTY_GRAPHICS
611.     if (match_optname(opts, "ttystatus", 9, FALSE))
612.         iflags.debug.ttystatus = negated ? FALSE : TRUE;
613. #endif
614. #ifdef WIN32
615.     if (match_optname(opts, "immediateflips", 14, FALSE))
616.         iflags.debug.immediateflips = negated ? FALSE : TRUE;
617. #endif
618.     if (match_optname(opts, "fuzzer", 4, FALSE))
619.         iflags.fuzzerpending = TRUE;
620.     return;
621. }
622. 
623. #if !defined(NODUMPENUMS)
624. /* monsdump[] and objdump[] are also used in utf8map.c */
625. 
626. #define DUMP_ENUMS
627. #define UNPREFIXED_COUNT (5)
628. struct enum_dump monsdump[] = {
629. #include "monsters.h"
630.     { NUMMONS, "NUMMONS" },
631.     { NON_PM, "NON_PM" },
632.     { LOW_PM, "LOW_PM" },
633.     { HIGH_PM, "HIGH_PM" },
634.     { SPECIAL_PM, "SPECIAL_PM" }
635. };
636. struct enum_dump objdump[] = {
637. #include "objects.h"
638.     { NUM_OBJECTS, "NUM_OBJECTS" },
639. };
640. 
641. #define DUMP_ENUMS_PCHAR
642. static struct enum_dump defsym_cmap_dump[] = {
643. #include "defsym.h"
644.     { MAXPCHARS, "MAXPCHARS" },
645. };
646. #undef DUMP_ENUMS_PCHAR
647. 
648. #define DUMP_ENUMS_MONSYMS
649. static struct enum_dump defsym_mon_syms_dump[] = {
650. #include "defsym.h"
651.     { MAXMCLASSES, "MAXMCLASSES" },
652. };
653. #undef DUMP_ENUMS_MONSYMS
654. 
655. #define DUMP_ENUMS_MONSYMS_DEFCHAR
656. static struct enum_dump defsym_mon_defchars_dump[] = {
657. #include "defsym.h"
658. };
659. #undef DUMP_ENUMS_MONSYMS_DEFCHAR
660. 
661. #define DUMP_ENUMS_OBJCLASS_DEFCHARS
662. static struct enum_dump objclass_defchars_dump[] = {
663. #include "defsym.h"
664. };
665. #undef DUMP_ENUMS_OBJCLASS_DEFCHARS
666. 
667. #define DUMP_ENUMS_OBJCLASS_CLASSES
668. static struct enum_dump objclass_classes_dump[] = {
669. #include "defsym.h"
670.     { MAXOCLASSES, "MAXOCLASSES" },
671. };
672. #undef DUMP_ENUMS_OBJCLASS_CLASSES
673. 
674. #define DUMP_ENUMS_OBJCLASS_SYMS
675. static struct enum_dump objclass_syms_dump[] = {
676. #include "defsym.h"
677. };
678. #undef DUMP_ENUMS_OBJCLASS_SYMS
679. 
680. #define DUMP_ARTI_ENUM
681. static struct enum_dump arti_enum_dump[] = {
682. #include "artilist.h"
683.     { AFTER_LAST_ARTIFACT, "AFTER_LAST_ARTIFACT" }
684. };
685. #undef DUMP_ARTI_ENUM
686. 
687. /* the enums are not part of hack.h for this one */
688. #define DUMP_MCASTU_ENUM1
689. enum mcast_dumpenum_spells {
690.     #include "mcastu.h"
691. };
692. #undef DUMP_MCASTU_ENUM1
693. 
694. #define DUMP_MCASTU_ENUM2
695. static struct enum_dump mcastu_enum_dump[] = {
696. #include "mcastu.h"
697. };
698. #undef DUMP_MCASTU_ENUM2
699. 
700. #undef DUMP_ENUMS
701. 
702. 
703. #ifndef NODUMPENUMS
704. 
705. staticfn void
706. dump_enums(void)
707. {
708.     enum enum_dumps {
709.         monsters_enum,
710.         objects_enum,
711.         objects_misc_enum,
712.         defsym_cmap_enum,
713.         defsym_mon_syms_enum,
714.         defsym_mon_defchars_enum,
715.         objclass_defchars_enum,
716.         objclass_classes_enum,
717.         objclass_syms_enum,
718.         arti_enum,
719.         mcastu_enum,
720.         NUM_ENUM_DUMPS
721.     };
722. 
723. #define dump_om(om) { om, #om }
724.     static const struct enum_dump omdump[] = {
725.         dump_om(LAST_GENERIC),
726.         dump_om(OBJCLASS_HACK),
727.         dump_om(FIRST_OBJECT),
728.         dump_om(FIRST_AMULET),
729.         dump_om(LAST_AMULET),
730.         dump_om(FIRST_SPELL),
731.         dump_om(LAST_SPELL),
732.         dump_om(MAXSPELL),
733.         dump_om(FIRST_REAL_GEM),
734.         dump_om(LAST_REAL_GEM),
735.         dump_om(FIRST_GLASS_GEM),
736.         dump_om(LAST_GLASS_GEM),
737.         dump_om(NUM_REAL_GEMS),
738.         dump_om(NUM_GLASS_GEMS),
739.         dump_om(MAX_GLYPH),
740.     };
741. #undef dump_om
742. 
743.     static const struct enum_dump *const ed[NUM_ENUM_DUMPS] = {
744.         monsdump, objdump, omdump,
745.         defsym_cmap_dump, defsym_mon_syms_dump,
746.         defsym_mon_defchars_dump,
747.         objclass_defchars_dump,
748.         objclass_classes_dump,
749.         objclass_syms_dump,
750.         arti_enum_dump,
751.         mcastu_enum_dump,
752.     };
753. 
754.     static const struct de_params {
755.         const char *const title;
756.         const char *const pfx;
757.         int unprefixed_count;
758.         int dumpflgs;  /* 0 = dump numerically only, 1 = add 'char' comment */
759.         int szd;
760.     } edmp[NUM_ENUM_DUMPS] = {
761.         { "monnums", "PM_", UNPREFIXED_COUNT, 0, SIZE(monsdump) },
762.         { "objects_nums", "", 1, 0, SIZE(objdump) },
763.         { "misc_object_nums", "", 1, 0, SIZE(omdump) },
764.         { "cmap_symbols", "", 1, 0, SIZE(defsym_cmap_dump) },
765.         { "mon_syms", "", 1, 0, SIZE(defsym_mon_syms_dump) },
766.         { "mon_defchars", "", 1, 1, SIZE(defsym_mon_defchars_dump) },
767.         { "objclass_defchars", "", 1, 1, SIZE(objclass_defchars_dump) },
768.         { "objclass_classes", "", 1, 0, SIZE(objclass_classes_dump) },
769.         { "objclass_syms", "", 1, 0, SIZE(objclass_syms_dump) },
770.         { "artifacts_nums", "", 1, 0, SIZE(arti_enum_dump) },
771.         { "mcast_spells", "MCAST_", 0, 0, SIZE(mcastu_enum_dump) },
772.     };
773. 
774.     const char *nmprefix;
775.     int i, j, nmwidth;
776.     char comment[BUFSZ];
777. 
778.     for (i = 0; i < NUM_ENUM_DUMPS; ++ i) {
779.         raw_printf("enum %s = {", edmp[i].title);
780.         for (j = 0; j < edmp[i].szd; ++j) {
781.             nmprefix = (j >= edmp[i].szd - edmp[i].unprefixed_count)
782.                            ? "" : edmp[i].pfx; /* "" or "PM_" */
783.             nmwidth = 27 - (int) strlen(nmprefix); /* 27 or 24 */
784.             if (edmp[i].dumpflgs > 0) {
785.                 Snprintf(comment, sizeof comment,
786.                          "    /* '%c' */",
787.                          (ed[i][j].val >= 32 && ed[i][j].val <= 126)
788.                          ? ed[i][j].val : ' ');
789.             } else {
790.                 comment[0] = '\0';
791.             }
792.             raw_printf("    %s%*s = %3d,%s",
793.                        nmprefix, -nmwidth,
794.                        ed[i][j].nm, ed[i][j].val,
795.                        comment);
796.        }
797.         raw_print("};");
798.         raw_print("");
799.     }
800.     raw_print("");
801. }
802. #undef UNPREFIXED_COUNT
803. #endif /* NODUMPENUMS */
804. 
805. void
806. dump_glyphids(void)
807. {
808.     dump_all_glyphids(stdout);
809. }
810. #endif /* !NODUMPENUMS */
811. 
812. /*allmain.c*/