Debian Codegraph @

prboom2/src/dsda/args.c

raw
1 //
2 // Copyright(C) 2022 by Ryan Krafnick
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // DESCRIPTION:
15 // DSDA (Command Line) Args
16 //
17
18 #include <stdio.h>
19 #include <string.h>
20
21 #include "lprintf.h"
22 #include "z_zone.h"
23
24 #include "dsda/args.h"
25
26 int dsda_argc;
27 char** dsda_argv;
28
29 typedef enum {
30 arg_null,
31 arg_int,
32 arg_string,
33 arg_int_array,
34 arg_string_array,
35 } arg_type_t;
36
37 typedef struct {
38 const char* name;
39 const char* alias;
40 const char* default_value;
41 const char* description;
42
43 arg_type_t type;
44
45 int lower_limit;
46 int upper_limit;
47 int min_count;
48 int max_count;
49 } arg_config_t;
50
51 #define AT_LEAST_ONE_STRING 0, 0, 1, INT_MAX
52 #define AT_LEAST_ONE_NONNEGATIVE_INT 0, INT_MAX, 1, INT_MAX
53 #define EXACT_ARRAY_LENGTH(x) 0, 0, x, x
54
55 static arg_config_t arg_config[dsda_arg_count] = {
56 [dsda_arg_help] = {
57 "-help", "--help", NULL,
58 "prints out command line argument information",
59 arg_null,
60 },
61 [dsda_arg_iwad] = {
62 "-iwad", NULL, NULL,
63 "loads the given iwad file",
64 arg_string,
65 },
66 [dsda_arg_file] = {
67 "-file", NULL, NULL,
68 "loads additional wad files",
69 arg_string_array, AT_LEAST_ONE_STRING,
70 },
71 [dsda_arg_deh] = {
72 "-deh", "-bex", NULL,
73 "loads additional deh files",
74 arg_string_array, AT_LEAST_ONE_STRING,
75 },
76 [dsda_arg_playdemo] = {
77 "-playdemo", NULL, NULL,
78 "plays the given demo file",
79 arg_string,
80 },
81 [dsda_arg_playlump] = {
82 "-playlump", NULL, NULL,
83 "plays the given internal demo lump (e.g., DEMO1)",
84 arg_string,
85 },
86 [dsda_arg_timedemo] = {
87 "-timedemo", NULL, NULL,
88 "plays the given demo file as fast as possible, timing the process",
89 arg_string,
90 },
91 [dsda_arg_fastdemo] = {
92 "-fastdemo", NULL, NULL,
93 "plays the given demo file as fast as possible, skipping some frames",
94 arg_string,
95 },
96 [dsda_arg_record] = {
97 "-record", NULL, NULL,
98 "records a demo to the given file",
99 arg_string,
100 },
101 [dsda_arg_recordfromto] = {
102 "-recordfromto", NULL, NULL,
103 "plays back the first file while writing to the second",
104 arg_string_array, EXACT_ARRAY_LENGTH(2),
105 },
106 [dsda_arg_from_key_frame] = {
107 "-from_key_frame", NULL, NULL,
108 "restores state and demo buffer from a key frame file",
109 arg_string,
110 },
111 [dsda_arg_warp] = {
112 "-warp", NULL, NULL,
113 "warp to the given episode and / or map",
114 arg_int_array, 0, 99, 0, 2,
115 },
116 [dsda_arg_skill] = {
117 "-skill", NULL, NULL,
118 "sets the skill level",
119 arg_int, 1, 255,
120 },
121 [dsda_arg_uv] = {
122 "-uv", NULL, NULL,
123 "sets the skill level to 4",
124 arg_null,
125 },
126 [dsda_arg_nm] = {
127 "-nm", NULL, NULL,
128 "sets the skill level to 5",
129 arg_null,
130 },
131 [dsda_arg_episode] = {
132 "-episode", NULL, NULL,
133 "warp to the first map in the given episode",
134 arg_int, 0, 9,
135 },
136 [dsda_arg_complevel] = {
137 "-complevel", "-cl", NULL,
138 "sets the compatibility level",
139 arg_int, -1, mbf21_compatibility,
140 },
141 [dsda_arg_fast] = {
142 "-fast", NULL, NULL,
143 "turns on fast monsters",
144 arg_null,
145 },
146 [dsda_arg_respawn] = {
147 "-respawn", NULL, NULL,
148 "turns on monster respawning",
149 arg_null,
150 },
151 [dsda_arg_nomonsters] = {
152 "-nomonsters", "-nomo", NULL,
153 "turns off monster spawning",
154 arg_null,
155 },
156 [dsda_arg_longtics] = {
157 "-longtics", NULL, NULL,
158 "enables high precision turn angles (in supported formats)",
159 arg_null,
160 },
161 [dsda_arg_shorttics] = {
162 "-shorttics", NULL, NULL,
163 "restricts turn angles to lower precision",
164 arg_null,
165 },
166 [dsda_arg_heretic] = {
167 "-heretic", NULL, NULL,
168 "sets the game to heretic",
169 arg_null,
170 },
171 [dsda_arg_hexen] = {
172 "-hexen", NULL, NULL,
173 "sets the game to hexen",
174 arg_null,
175 },
176 [dsda_arg_class] = {
177 "-class", NULL, NULL,
178 "sets the player class in hexen",
179 arg_int, 0, 2,
180 },
181 [dsda_arg_randclass] = {
182 "-randclass", NULL, NULL,
183 "sets a random player class in hexen deathmatch",
184 arg_null,
185 },
186 [dsda_arg_dsdademo] = {
187 "-dsdademo", NULL, NULL,
188 "turns on extended demo format (for testing)",
189 arg_null,
190 },
191 [dsda_arg_solo_net] = {
192 "-solo-net", NULL, NULL,
193 "play a net game with one player",
194 arg_null,
195 },
196 [dsda_arg_coop_spawns] = {
197 "-coop_spawns", NULL, NULL,
198 "play single player with coop thing spawns",
199 arg_null,
200 },
201 [dsda_arg_pistol_start] = {
202 "-pistolstart", "-wandstart", NULL,
203 "automatically pistol start each map",
204 arg_null,
205 },
206 [dsda_arg_chain_episodes] = {
207 "-chain_episodes", NULL, NULL,
208 "completing one episode leads to the next without interruption",
209 arg_null,
210 },
211 [dsda_arg_stroller] = {
212 "-stroller", NULL, NULL,
213 "applies stroller category limitations",
214 arg_null,
215 },
216 [dsda_arg_turbo] = {
217 "-turbo", NULL, "255",
218 "sets player speed percent",
219 arg_int, 10, 255,
220 },
221 [dsda_arg_game_speed] = {
222 "-game_speed", NULL, NULL,
223 "sets game speed percent",
224 arg_int, 10, 10000,
225 },
226 [dsda_arg_tas] = {
227 "-tas", NULL, NULL,
228 "lifts strict mode restrictions",
229 arg_null,
230 },
231 [dsda_arg_build] = {
232 "-build", NULL, NULL,
233 "starts in build mode",
234 arg_null,
235 },
236 [dsda_arg_quit_after_brute_force] = {
237 "-quit_after_brute_force", NULL, NULL,
238 "quits the game when brute force ends",
239 arg_null,
240 },
241 [dsda_arg_first_input] = {
242 "-first_input", NULL, NULL,
243 "builds the first frame F S T",
244 arg_int_array, -128, 127, 3, 3
245 },
246 [dsda_arg_command] = {
247 "-command", NULL, NULL,
248 "runs a console command",
249 arg_string
250 },
251 [dsda_arg_skipsec] = {
252 "-skipsec", NULL, NULL,
253 "skip to the given time (mm:ss or ss) - negative times seek from the end",
254 arg_string,
255 },
256 [dsda_arg_skiptic] = {
257 "-skiptic", NULL, NULL,
258 "skip to the given tic - negative tics seek from the end",
259 arg_int, INT_MIN, INT_MAX,
260 },
261 [dsda_arg_track_pacifist] = {
262 "-track_pacifist", NULL, NULL,
263 "tracks pacifist category restrictions",
264 arg_null,
265 },
266 [dsda_arg_track_100k] = {
267 "-track_100k", NULL, NULL,
268 "tracks when 100% kills is reached",
269 arg_null,
270 },
271 [dsda_arg_track_reality] = {
272 "-track_reality", NULL, NULL,
273 "tracks reality and almost reality categories restrictions",
274 arg_null,
275 },
276 [dsda_arg_time_keys] = {
277 "-time_keys", NULL, "105",
278 "announces the time when keys are picked up",
279 arg_int, 0, 350
280 },
281 [dsda_arg_time_use] = {
282 "-time_use", NULL, "105",
283 "announces the time when the use command is activated",
284 arg_int, 0, 350
285 },
286 [dsda_arg_time_secrets] = {
287 "-time_secrets", NULL, "105",
288 "announces the time when a secret is collected",
289 arg_int, 0, 350
290 },
291 [dsda_arg_time_all] = {
292 "-time_all", NULL, "105",
293 "announces the time when any -time_* event happens",
294 arg_int, 0, 350
295 },
296 [dsda_arg_track_player] = {
297 "-track_player", NULL, NULL,
298 "adds player info to the tracker",
299 arg_null,
300 },
301 [dsda_arg_track_line] = {
302 "-track_line", NULL, NULL,
303 "adds at least one line to the tracker",
304 arg_int_array, AT_LEAST_ONE_NONNEGATIVE_INT,
305 },
306 [dsda_arg_track_line_distance] = {
307 "-track_line_distance", NULL, NULL,
308 "adds at least one line distance to the tracker",
309 arg_int_array, AT_LEAST_ONE_NONNEGATIVE_INT,
310 },
311 [dsda_arg_track_sector] = {
312 "-track_sector", NULL, NULL,
313 "adds at least one sector to the tracker",
314 arg_int_array, AT_LEAST_ONE_NONNEGATIVE_INT,
315 },
316 [dsda_arg_track_mobj] = {
317 "-track_mobj", NULL, NULL,
318 "adds at least one mobj to the tracker",
319 arg_int_array, AT_LEAST_ONE_NONNEGATIVE_INT,
320 },
321 [dsda_arg_assign] = {
322 "-assign", NULL, NULL,
323 "temporarily assign config variables",
324 arg_string_array, AT_LEAST_ONE_STRING,
325 },
326 [dsda_arg_update] = {
327 "-update", NULL, NULL,
328 "permanently update config variables",
329 arg_string_array, AT_LEAST_ONE_STRING,
330 },
331 [dsda_arg_analysis] = {
332 "-analysis", NULL, NULL,
333 "writes various data to analysis.txt",
334 arg_null,
335 },
336 [dsda_arg_levelstat] = {
337 "-levelstat", NULL, NULL,
338 "writes level stats to levelstat.txt",
339 arg_null,
340 },
341 [dsda_arg_export_text_file] = {
342 "-export_text_file", NULL, NULL,
343 "export a dsda-format text file template",
344 arg_null,
345 },
346 [dsda_arg_track_playback] = {
347 "-track_playback", NULL, NULL,
348 "treat demo playback as an attempt for the given split file base",
349 arg_string,
350 },
351 [dsda_arg_export_ghost] = {
352 "-export_ghost", NULL, NULL,
353 "exports a ghost file",
354 arg_string,
355 },
356 [dsda_arg_import_ghost] = {
357 "-import_ghost", NULL, NULL,
358 "imports at least one ghost file",
359 arg_string_array, AT_LEAST_ONE_STRING,
360 },
361 [dsda_arg_consoleplayer] = {
362 "-consoleplayer", NULL, NULL,
363 "sets the console player (for coop playback)",
364 arg_int, 0, 7,
365 },
366 [dsda_arg_spechit] = {
367 "-spechit", NULL, NULL,
368 "sets a magic spechit base address for certain overrun demos",
369 arg_int, INT_MIN, INT_MAX,
370 },
371 [dsda_arg_setmem] = {
372 "-setmem", NULL, NULL,
373 "sets a magic block of memory for certain overrun demos",
374 arg_string_array, 0, 0, 1, 10,
375 },
376 [dsda_arg_data] = {
377 "-data", NULL, NULL,
378 "sets the data directory",
379 arg_string,
380 },
381 [dsda_arg_save] = {
382 "-save", NULL, NULL,
383 "sets the save directory",
384 arg_string,
385 },
386 [dsda_arg_config] = {
387 "-config", NULL, NULL,
388 "sets the config file",
389 arg_string,
390 },
391 [dsda_arg_hud] = {
392 "-hud", NULL, NULL,
393 "sets the hud config file",
394 arg_string,
395 },
396 [dsda_arg_shotdir] = {
397 "-shotdir", NULL, NULL,
398 "sets the screenshot directory",
399 arg_string,
400 },
401 [dsda_arg_movie] = {
402 "-movie", NULL, NULL,
403 "sets the target final level for movie demos (for automatic exit detection)",
404 arg_int, 0, 99,
405 },
406 [dsda_arg_viddump] = {
407 "-viddump", NULL, NULL,
408 "dumps a video to the chosen file name",
409 arg_string,
410 },
411 [dsda_arg_dehout] = {
412 "-dehout", "-bexout", NULL,
413 "sets dehacked log file",
414 arg_string,
415 },
416 [dsda_arg_verbose] = {
417 "-verbose", NULL, NULL,
418 "enable all logging",
419 arg_null,
420 },
421 [dsda_arg_quiet] = {
422 "-quiet", NULL, NULL,
423 "disable all logging",
424 arg_null,
425 },
426 [dsda_arg_v] = {
427 "-v", NULL, NULL,
428 "print the version and exit",
429 arg_null,
430 },
431 [dsda_arg_resetgamma] = {
432 "-resetgamma", NULL, NULL,
433 "reset gamma and exit",
434 arg_null,
435 },
436 [dsda_arg_force_old_zdoom_nodes] = {
437 "-force_old_zdoom_nodes", NULL, NULL,
438 "force extended (non-gl) zdoom nodes",
439 arg_null,
440 },
441 [dsda_arg_sigsegv] = {
442 "-sigsegv", NULL, NULL,
443 "disable the SIGSEGV signal handler",
444 arg_null,
445 },
446 [dsda_arg_deathmatch] = {
447 "-deathmatch", NULL, NULL,
448 "turn on deathmatch mode",
449 arg_null,
450 },
451 [dsda_arg_altdeath] = {
452 "-altdeath", NULL, NULL,
453 "turn on altdeath mode",
454 arg_null,
455 },
456 [dsda_arg_timer] = {
457 "-timer", NULL, NULL,
458 "sets the level time limit (in minutes) for deathmatch",
459 arg_int, 1, INT_MAX,
460 },
461 [dsda_arg_frags] = {
462 "-frags", NULL, "10",
463 "sets the level frag limit for deathmatch",
464 arg_int, 1, INT_MAX,
465 },
466 [dsda_arg_nosound] = {
467 "-nosound", NULL, NULL,
468 "turn off sound",
469 arg_null,
470 },
471 [dsda_arg_nomusic] = {
472 "-nomusic", NULL, NULL,
473 "turn off music",
474 arg_null,
475 },
476 [dsda_arg_nosfx] = {
477 "-nosfx", NULL, NULL,
478 "turn off sfx",
479 arg_null,
480 },
481 [dsda_arg_nodraw] = {
482 "-nodraw", NULL, NULL,
483 "turn off drawing",
484 arg_null,
485 },
486 [dsda_arg_nodeh] = {
487 "-nodeh", NULL, NULL,
488 "skip dehacked lumps inside wads",
489 arg_null,
490 },
491 [dsda_arg_nomapinfo] = {
492 "-nomapinfo", NULL, NULL,
493 "skip *MAPINFO lumps",
494 arg_null,
495 },
496 [dsda_arg_noautoload] = {
497 "-noautoload", "-noload", NULL,
498 "ignore autoload files",
499 arg_null,
500 },
501 [dsda_arg_nocheats] = {
502 "-nocheats", NULL, NULL,
503 "ignore dehacked cheats",
504 arg_null,
505 },
506 [dsda_arg_nojoy] = {
507 "-nojoy", NULL, NULL,
508 "disable joystick input",
509 arg_null,
510 },
511 [dsda_arg_nomouse] = {
512 "-nomouse", NULL, NULL,
513 "disable mouse input",
514 arg_null,
515 },
516 [dsda_arg_no_message_box] = {
517 "-no_message_box", NULL, NULL,
518 "disable message boxes",
519 arg_null,
520 },
521 [dsda_arg_fullscreen] = {
522 "-fullscreen", NULL, NULL,
523 "temporarily turns on fullscreen mode",
524 arg_null,
525 },
526 [dsda_arg_window] = {
527 "-window", NULL, NULL,
528 "temporarily turns on windowed mode",
529 arg_null,
530 },
531 [dsda_arg_width] = {
532 "-width", NULL, NULL,
533 "temporarily sets the resolution width",
534 arg_int, 320, INT_MAX,
535 },
536 [dsda_arg_height] = {
537 "-height", NULL, NULL,
538 "temporarily sets the resolution height",
539 arg_int, 200, INT_MAX,
540 },
541 [dsda_arg_geometry] = {
542 "-geometry", "-geom", NULL,
543 "temporarily sets the resolution and, optionally, the window mode WxH[w|f]",
544 arg_string,
545 },
546 [dsda_arg_vidmode] = {
547 "-vidmode", NULL, NULL,
548 "temporarily sets the graphics renderer (sw or gl)",
549 arg_string,
550 },
551 [dsda_arg_aspect] = {
552 "-aspect", NULL, NULL,
553 "sets the fov aspect ratio WxH",
554 arg_string, 0, 21,
555 },
556 [dsda_arg_emulate] = {
557 "-emulate", NULL, NULL,
558 "emulates errors from a version of prboom+ (a.b.c.d)",
559 arg_string,
560 },
561 [dsda_arg_doom95] = {
562 "-doom95", NULL, NULL,
563 "use doom95's adjacent sector limit",
564 arg_null,
565 },
566 [dsda_arg_blockmap] = {
567 "-blockmap", NULL, NULL,
568 "rebuild the blockmap (ignore BLOCKMAP lump)",
569 arg_null,
570 },
571 [dsda_arg_force_monster_avoid_hazards] = {
572 "-force_monster_avoid_hazards", NULL, NULL,
573 "sets a special flag to compensate for sync errors in certain demos",
574 arg_null,
575 },
576 [dsda_arg_force_remove_slime_trails] = {
577 "-force_remove_slime_trails", NULL, NULL,
578 "sets a special flag to compensate for sync errors in certain demos",
579 arg_null,
580 },
581 [dsda_arg_force_no_dropoff] = {
582 "-force_no_dropoff", NULL, NULL,
583 "sets a special flag to compensate for sync errors in certain demos",
584 arg_null,
585 },
586 [dsda_arg_force_truncated_sector_specials] = {
587 "-force_truncated_sector_specials", NULL, NULL,
588 "sets a special flag to compensate for sync errors in certain demos",
589 arg_null,
590 },
591 [dsda_arg_force_boom_brainawake] = {
592 "-force_boom_brainawake", NULL, NULL,
593 "sets a special flag to compensate for sync errors in certain demos",
594 arg_null,
595 },
596 [dsda_arg_force_prboom_friction] = {
597 "-force_prboom_friction", NULL, NULL,
598 "sets a special flag to compensate for sync errors in certain demos",
599 arg_null,
600 },
601 [dsda_arg_reject_pad_with_ff] = {
602 "-reject_pad_with_ff", NULL, NULL,
603 "sets a special flag to compensate for sync errors in certain demos",
604 arg_null,
605 },
606 [dsda_arg_force_lxdoom_demo_compatibility] = {
607 "-force_lxdoom_demo_compatibility", NULL, NULL,
608 "sets a special flag to compensate for sync errors in certain demos",
609 arg_null,
610 },
611 [dsda_arg_allow_ssg_direct] = {
612 "-allow_ssg_direct", NULL, NULL,
613 "sets a special flag to compensate for sync errors in certain demos",
614 arg_null,
615 },
616 [dsda_arg_treat_no_clipping_things_as_not_blocking] = {
617 "-treat_no_clipping_things_as_not_blocking", NULL, NULL,
618 "sets a special flag to compensate for sync errors in certain demos",
619 arg_null,
620 },
621 [dsda_arg_force_incorrect_processing_of_respawn_frame_entry] = {
622 "-force_incorrect_processing_of_respawn_frame_entry", NULL, NULL,
623 "sets a special flag to compensate for sync errors in certain demos",
624 arg_null,
625 },
626 [dsda_arg_force_correct_code_for_3_keys_doors_in_mbf] = {
627 "-force_correct_code_for_3_keys_doors_in_mbf", NULL, NULL,
628 "sets a special flag to compensate for sync errors in certain demos",
629 arg_null,
630 },
631 [dsda_arg_uninitialize_crush_field_for_stairs] = {
632 "-uninitialize_crush_field_for_stairs", NULL, NULL,
633 "sets a special flag to compensate for sync errors in certain demos",
634 arg_null,
635 },
636 [dsda_arg_force_boom_findnexthighestfloor] = {
637 "-force_boom_findnexthighestfloor", NULL, NULL,
638 "sets a special flag to compensate for sync errors in certain demos",
639 arg_null,
640 },
641 [dsda_arg_allow_sky_transfer_in_boom] = {
642 "-allow_sky_transfer_in_boom", NULL, NULL,
643 "sets a special flag to compensate for sync errors in certain demos",
644 arg_null,
645 },
646 [dsda_arg_apply_green_armor_class_to_armor_bonuses] = {
647 "-apply_green_armor_class_to_armor_bonuses", NULL, NULL,
648 "sets a special flag to compensate for sync errors in certain demos",
649 arg_null,
650 },
651 [dsda_arg_apply_blue_armor_class_to_megasphere] = {
652 "-apply_blue_armor_class_to_megasphere", NULL, NULL,
653 "sets a special flag to compensate for sync errors in certain demos",
654 arg_null,
655 },
656 [dsda_arg_force_incorrect_bobbing_in_boom] = {
657 "-force_incorrect_bobbing_in_boom", NULL, NULL,
658 "sets a special flag to compensate for sync errors in certain demos",
659 arg_null,
660 },
661 [dsda_arg_boom_deh_parser] = {
662 "-boom_deh_parser", NULL, NULL,
663 "sets a special flag to compensate for sync errors in certain demos",
664 arg_null,
665 },
666 [dsda_arg_mbf_remove_thinker_in_killmobj] = {
667 "-mbf_remove_thinker_in_killmobj", NULL, NULL,
668 "sets a special flag to compensate for sync errors in certain demos",
669 arg_null,
670 },
671 [dsda_arg_do_not_inherit_friendlyness_flag_on_spawn] = {
672 "-do_not_inherit_friendlyness_flag_on_spawn", NULL, NULL,
673 "sets a special flag to compensate for sync errors in certain demos",
674 arg_null,
675 },
676 [dsda_arg_do_not_use_misc12_frame_parameters_in_a_mushroom] = {
677 "-do_not_use_misc12_frame_parameters_in_a_mushroom", NULL, NULL,
678 "sets a special flag to compensate for sync errors in certain demos",
679 arg_null,
680 },
681 [dsda_arg_apply_mbf_codepointers_to_any_complevel] = {
682 "-apply_mbf_codepointers_to_any_complevel", NULL, NULL,
683 "sets a special flag to compensate for sync errors in certain demos",
684 arg_null,
685 },
686 [dsda_arg_reset_monsterspawner_params_after_loading] = {
687 "-reset_monsterspawner_params_after_loading", NULL, NULL,
688 "sets a special flag to compensate for sync errors in certain demos",
689 arg_null,
690 },
691 [dsda_arg_debug_mapinfo] = {
692 "-debug_mapinfo", NULL, NULL,
693 "turns on mapinfo parsing in doom (temporary arg for testing)",
694 arg_null,
695 },
696 };
697
698 static dsda_arg_t arg_value[dsda_arg_count];
699
700 static void dsda_ParseIntArg(arg_config_t* config, int* value, const char* param) {
701 if (sscanf(param, "%d", value) != 1) {
702 if (config->type == arg_int)
703 I_Error("%s requires an integer argument", config->name);
704 else
705 I_Error("%s requires integer arguments", config->name);
706 }
707 if (*value < config->lower_limit)
708 I_Error("%s argument too low (min is %d)", config->name, config->lower_limit);
709 if (*value > config->upper_limit)
710 I_Error("%s argument too high (max is %d)", config->name, config->upper_limit);
711 }
712
713 static void dsda_ParseStringArg(arg_config_t* config, const char** value, const char* param) {
714 if (config->upper_limit && strlen(param) > config->upper_limit)
715 I_Error("%s argument too long (max is %d)", config->name, config->upper_limit);
716
717 *value = param;
718 }
719
720 static void dsda_ValidateArrayArg(arg_config_t* config, dsda_arg_t* arg) {
721 if (config->min_count == config->max_count) {
722 if (arg->count != config->min_count)
723 I_Error("%s requires exactly %d arguments", config->name, config->min_count);
724 }
725 else {
726 if (arg->count < config->min_count || arg->count > config->max_count) {
727 if (config->max_count == INT_MAX)
728 I_Error("%s requires at least %d argument(s)", config->name, config->min_count);
729 else
730 I_Error("%s requires %d to %d argument(s)", config->name, config->min_count, config->max_count);
731 }
732 }
733 }
734
735 static void dsda_ParseArg(arg_config_t* config, dsda_arg_t* arg, int argv_i) {
736 for (arg->count = 0; argv_i + arg->count < dsda_argc - 1; ++arg->count) {
737 int x;
738 dboolean is_integer;
739
740 is_integer = sscanf(dsda_argv[argv_i + arg->count + 1], "%d", &x);
741
742 if (dsda_argv[argv_i + arg->count + 1][0] == '-' && !is_integer)
743 break;
744
745 if (config->type == arg_int || config->type == arg_int_array) {
746 // only valid integers should be interpreted as arguments
747 if (!is_integer)
748 I_Error("%s does not accept string arguments", config->name);
749 }
750 }
751
752 arg->found = true;
753
754 switch (config->type) {
755 case arg_null:
756 if (arg->count)
757 I_Error("%s does not take an argument", config->name);
758
759 break;
760 case arg_int:
761 if (arg->count == 0) {
762 if (config->default_value) {
763 dsda_ParseIntArg(config, &arg->value.v_int, config->default_value);
764
765 break;
766 }
767
768 I_Error("%s requires an integer argument", config->name);
769 }
770 if (arg->count > 1)
771 I_Error("%s takes only one argument", config->name);
772
773 dsda_ParseIntArg(config, &arg->value.v_int, dsda_argv[argv_i + 1]);
774
775 break;
776 case arg_string:
777 if (arg->count == 0) {
778 if (config->default_value) {
779 arg->value.v_string = config->default_value;
780
781 break;
782 }
783
784 I_Error("%s requires a string argument", config->name);
785 }
786 if (arg->count > 1)
787 I_Error("%s takes only one argument", config->name);
788
789 dsda_ParseStringArg(config, &arg->value.v_string, dsda_argv[argv_i + 1]);
790
791 break;
792 case arg_int_array:
793 dsda_ValidateArrayArg(config, arg);
794
795 {
796 int i;
797
798 arg->value.v_int_array = Z_Malloc(arg->count * sizeof(int));
799
800 for (i = argv_i; i < argv_i + arg->count; ++i)
801 dsda_ParseIntArg(config, &arg->value.v_int_array[i - argv_i], dsda_argv[i + 1]);
802 }
803
804 break;
805
806 case arg_string_array:
807 dsda_ValidateArrayArg(config, arg);
808
809 {
810 int i;
811
812 arg->value.v_string_array = Z_Malloc(arg->count * sizeof(char *));
813
814 for (i = argv_i; i < argv_i + arg->count; ++i)
815 dsda_ParseStringArg(config, &arg->value.v_string_array[i - argv_i], dsda_argv[i + 1]);
816 }
817
818 break;
819 }
820 }
821
822 void dsda_ParseCommandLineArgs(int argc, char** argv) {
823 int i;
824 int argv_i;
825 arg_config_t* config;
826
827 dsda_argc = argc;
828 dsda_argv = argv;
829
830 for (argv_i = dsda_argc - 1; argv_i > 0; --argv_i) {
831 int x;
832 dboolean is_integer;
833
834 is_integer = sscanf(dsda_argv[argv_i], "%d", &x);
835
836 if (dsda_argv[argv_i][0] == '-' && !is_integer) {
837 for (i = 0; i < dsda_arg_count; ++i) {
838 config = &arg_config[i];
839
840 if (
841 !strcasecmp(config->name, dsda_argv[argv_i]) ||
842 (config->alias && !strcasecmp(config->alias, dsda_argv[argv_i]))
843 ) {
844 if (arg_value[i].found)
845 lprintf(LO_WARN, "Warning: ignoring duplicate argument %s\n", config->name);
846 else
847 dsda_ParseArg(config, &arg_value[i], argv_i);
848 break;
849 }
850 }
851
852 if (i == dsda_arg_count)
853 I_Error("Unknown command line option %s\n", dsda_argv[argv_i]);
854 }
855 }
856
857 if (dsda_Flag(dsda_arg_uv))
858 dsda_UpdateIntArg(dsda_arg_skill, "4");
859
860 if (dsda_Flag(dsda_arg_nm))
861 dsda_UpdateIntArg(dsda_arg_skill, "5");
862 }
863
864 void dsda_UpdateIntArg(dsda_arg_identifier_t id, const char* param) {
865 arg_value[id].count = 1;
866 arg_value[id].found = true;
867 dsda_ParseIntArg(&arg_config[id], &arg_value[id].value.v_int, param);
868 }
869
870 void dsda_UpdateStringArg(dsda_arg_identifier_t id, const char* param) {
871 param = Z_Strdup(param);
872 arg_value[id].count = 1;
873 arg_value[id].found = true;
874 dsda_ParseStringArg(&arg_config[id], &arg_value[id].value.v_string, param);
875 }
876
877 void dsda_AppendStringArg(dsda_arg_identifier_t id, const char* param) {
878 if (arg_config[id].type == arg_string)
879 return dsda_UpdateStringArg(id, param);
880
881 param = Z_Strdup(param);
882
883 ++arg_value[id].count;
884 arg_value[id].found = true;
885 arg_value[id].value.v_string_array =
886 Z_Realloc(arg_value[id].value.v_string_array, arg_value[id].count * sizeof(char *));
887
888 dsda_ParseStringArg(
889 &arg_config[id],
890 &arg_value[id].value.v_string_array[arg_value[id].count - 1],
891 param
892 );
893
894 dsda_ValidateArrayArg(&arg_config[id], &arg_value[id]);
895 }
896
897 dsda_arg_t* dsda_Arg(dsda_arg_identifier_t id) {
898 return &arg_value[id];
899 }
900
901 void dsda_UpdateFlag(dsda_arg_identifier_t id, dboolean found) {
902 arg_value[id].found = found;
903 }
904
905 dboolean dsda_Flag(dsda_arg_identifier_t id) {
906 return arg_value[id].found;
907 }
908
909 int dsda_SimpleIntArg(dsda_arg_identifier_t id) {
910 if (arg_value[id].found)
911 return arg_value[id].value.v_int;
912
913 return 0;
914 }
915
916 void dsda_PrintArgHelp(void) {
917 int i;
918
919 lprintf(LO_INFO, "\nCommand Line Arguments:\n\n");
920
921 for (i = 0; i < dsda_arg_count; ++i) {
922 arg_config_t* config;
923
924 config = &arg_config[i];
925
926 lprintf(LO_INFO, " %s", config->name);
927 if (config->alias)
928 lprintf(LO_INFO, " / %s", config->alias);
929 lprintf(LO_INFO, ":\n");
930 lprintf(LO_INFO, " %s\n", config->description);
931 if (config->default_value)
932 lprintf(LO_INFO, " Default: %s\n", config->default_value);
933 lprintf(LO_INFO, "\n");
934 }
935 }