File: cmdline.c

package info (click to toggle)
xconq 7.2.2-2
  • links: PTS
  • area: main
  • in suites: slink
  • size: 8,296 kB
  • ctags: 9,199
  • sloc: ansic: 107,849; sh: 2,108; perl: 2,057; makefile: 1,177; sed: 161; csh: 50; awk: 49; lisp: 39
file content (815 lines) | stat: -rw-r--r-- 23,689 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
/* Command line parsing for Xconq.
   Copyright (C) 1987-1989, 1991-1998 Stanley T. Shebs.

Xconq is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.  See the file COPYING.  */

/* This is a command-line parser that may be used in implementations that
   get a command line from somewhere. */

/* Command lines get parsed in several stages, since for instance the
   choice of game module will decide which variants are available. */

#include "conq.h"
extern short initially_no_ai;
#include "cmdline.h"

static void version_info PARAMS ((void));
static void general_usage_info PARAMS ((void));
static void game_usage_info PARAMS ((void));
static void unixify_variant_name PARAMS ((char *buf, char *varname));
static void player_usage_info PARAMS ((void));
static void parse_world_option PARAMS ((char *str));
static void parse_realtime_option PARAMS ((char *subopt, char *arg));
static void parse_variant PARAMS ((char *str));
static void add_a_raw_player_spec PARAMS ((char *specstr));

/* The startup-settable options. */

static int option_totalgametime;
static int option_perturntime;
static int option_persidetime;
static int option_add_default_player;

char *option_game_to_host = NULL;

char *option_game_to_join = NULL;

int option_num_to_wait_for = 0;

static char *default_ai_type = ",mplayer";

/* This says whether the command line arguments actually chose a
   specific game to play (as opposed to setting random flags). */

static int game_chosen = FALSE;

char *args_used;

/* The list of asked-for players. */

struct raw_spec {
  char *spec;
  struct raw_spec *next;
} *raw_player_specs, *last_raw_player_spec;

static char *raw_default_player_spec;

char *default_player_spec;

/* The list of modules to loaded in addition to the main one. */

struct module_spec {
  Module *module;
  struct module_spec *next;
} *extra_modules, *last_extra_module;

/* The lists of GDL forms to interpret before and after loading. */

struct gdl_spec {
  char *spec;
  struct gdl_spec *next;
} *pre_gdl_specs, *last_pre_gdl_spec, *post_gdl_specs, *last_post_gdl_spec;

static void add_a_gdl_spec PARAMS ((char *specstr, struct gdl_spec **first,
				    struct gdl_spec **last));

/* The list of accumulated variant choices. */

static Obj *variant_settings;

static char *program_name = "";

static int help_wanted = FALSE;

static int variant_help_wanted = FALSE;

static int version_wanted = FALSE;

static int had_error = FALSE;

/* Set the most basic of defaults on the dynamically-settable options. */

/* (need flags to indicate which options were actually used, so variant
   handling can warn about improper use) */

void
init_options()
{
    option_add_default_player = TRUE;
    variant_settings = lispnil;
}

/* Generic command line parsing.  This is used by several different programs,
   so it just collects info, doesn't process it much.  This is called
   several times, because the validity of some args depends on which
   game modules are loaded and which players are to be in the game,
   and interfaces may need to do some of their own processing in between. */

void
parse_command_line(argc, argv, spec)
int argc, spec;
char *argv[];
{
    char *arg, *aispec, tmpspec[100];
    int i, n, numused, total_arg_space;

/* This macro just checks that a required next argument is actually there. */

#define REQUIRE_ONE_ARG  \
  if (i + 1 >= argc) {  \
    fprintf(stderr, "Error: `%s' requires an argument, exiting now\n", argv[i]);  \
    had_error = TRUE;  \
    continue;  \
  }  \
  numused = 2;

/* Each of these causes argument parsing to skip over the option if it's
   not the right time to look at it. */

#define GENERAL_OPTION if (spec != general_options) continue;
#define VARIANT_OPTION if (spec != variant_options) continue;
#define PLAYER_OPTION  if (spec != player_options)  continue;

    /* (should peel off any path stuff) */
    program_name = argv[0];

    if (spec == general_options)
      init_options();

    total_arg_space = 0;
    for (i = 0; i < argc; ++i)
      if (!empty_string(argv[i]))
	total_arg_space += strlen(argv[i]) + 2;
    if (args_used == NULL)
      args_used = xmalloc (total_arg_space);

    for (i = 1; i < argc; ++i) {
	if (argv[i] == NULL || (argv[i])[0] == '\0') {
	    /* Empty or already munched, nothing to do. */
	} else if ((argv[i])[0] == '-') {
	    arg = argv[i];
	    Dprintf("%s\n", arg);
	    numused = 1;
	    if (strcmp(arg, "-c") == 0) {
		REQUIRE_ONE_ARG;
		GENERAL_OPTION;
		checkpoint_interval = atoi(argv[i+1]);
	    } else if (strcmp(arg, "-design") == 0) {
		GENERAL_OPTION;
#ifdef DESIGNERS
		allbedesigners = TRUE;
#else
		fprintf(stderr,
			"No designing available, ignoring option `%s'\n", arg);
#endif /* DESIGNERS */
	    } else if (strncmp(arg, "-D", 2) == 0) {
		GENERAL_OPTION;
#ifdef DEBUGGING
		Debug = TRUE;
		if (strchr(arg+2, '-'))
		  Debug = FALSE;
		if (strchr(arg+2, 'M'))
		  DebugM = TRUE;
		if (strchr(arg+2, 'G'))
		  DebugG = TRUE;
#else
		fprintf(stderr,
			"No debugging available, ignoring option `%s'\n", arg);
#endif /* DEBUGGING */
	    } else if (strncmp(arg, "-e", 2) == 0) {
		REQUIRE_ONE_ARG;
		PLAYER_OPTION;
		n = atoi(argv[i+1]);
		/* A comma indicates that the name of a particular desired
		   AI type follows. */
		if (strlen(arg) > 2) {
		    aispec = arg + 2;
		    if (*aispec != ',') {
			sprintf(tmpspec, ",mplayer%s", aispec);
			aispec = tmpspec;
		    }
		} else {
		    aispec = default_ai_type;
		}
		while (n-- > 0)
		  add_a_raw_player_spec(aispec);
	    } else if (strcmp(arg, "-f") == 0) {
		REQUIRE_ONE_ARG;
		GENERAL_OPTION;
		add_a_module(NULL, argv[i+1]); 
	    } else if (strcmp(arg, "-g") == 0) {
		REQUIRE_ONE_ARG;
		GENERAL_OPTION;
		add_a_module(copy_string(argv[i+1]), NULL);
	    } else if (strcmp(arg, "-h") == 0) {
		REQUIRE_ONE_ARG;
		PLAYER_OPTION;
		option_num_to_wait_for = atoi(argv[i+1]);
	    } else if (strcmp(arg, "-help") == 0) {
		GENERAL_OPTION;
		help_wanted = TRUE;
		/* Will display help info later. */
	    } else if (strcmp(arg, "-host") == 0) {
		REQUIRE_ONE_ARG;
		PLAYER_OPTION;
		option_game_to_host = copy_string(argv[i+1]);
	    } else if (strcmp(arg, "-join") == 0) {
		REQUIRE_ONE_ARG;
		GENERAL_OPTION;
		option_game_to_join = copy_string(argv[i+1]);
	    } else if (strcmp(arg, "-L") == 0) {
		REQUIRE_ONE_ARG;
		GENERAL_OPTION;
		if (strcmp(argv[i+1], "-") == 0)
		  add_library_path(NULL);
		else
		  add_library_path(argv[i+1]);
	    } else if (strcmp(arg, "-M") == 0) {
		REQUIRE_ONE_ARG;
		VARIANT_OPTION;
		parse_world_option(argv[i+1]);
	    } else if (strcmp(arg, "-noai") == 0) {
		PLAYER_OPTION;
		initially_no_ai = TRUE;
	    } else if (strcmp(arg, "-pre") == 0) {
		REQUIRE_ONE_ARG;
		GENERAL_OPTION;
		add_a_gdl_spec(argv[i+1], &pre_gdl_specs, &last_pre_gdl_spec);
	    } else if (strcmp(arg, "-post") == 0) {
		REQUIRE_ONE_ARG;
		GENERAL_OPTION;
		add_a_gdl_spec(argv[i+1], &post_gdl_specs, &last_post_gdl_spec);
	    } else if (strcmp(arg, "-r") == 0) {
		PLAYER_OPTION;
		option_add_default_player = FALSE;
	    } else if (strcmp(arg, "-R") == 0) {
		REQUIRE_ONE_ARG;
		GENERAL_OPTION;
#ifdef DEBUGGING
		init_xrandom(atoi(argv[i+1]));
#else
		fprintf(stderr,
			"No debugging available, ignoring option `%s'\n", arg);
#endif /* DEBUGGING */
	    } else if (strcmp(arg, "-seq") == 0) {
		VARIANT_OPTION;
		push_key_int_binding(&variant_settings, K_SEQUENTIAL, 1);
	    } else if (strcmp(arg, "-sim") == 0) {
		VARIANT_OPTION;
		push_key_int_binding(&variant_settings, K_SEQUENTIAL, 0);
	    } else if (strncmp(arg, "-t", 2) == 0) {
		REQUIRE_ONE_ARG;
		VARIANT_OPTION;
		parse_realtime_option(arg, argv[i+1]);
	    } else if (strncmp(arg, "-v", 2) == 0) {
		VARIANT_OPTION;
		parse_variant(arg + 2);
	    } else if (strcmp(arg, "-V") == 0) {
		VARIANT_OPTION;
		push_key_int_binding(&variant_settings, K_SEE_ALL, 1);
	    } else if (strcmp(arg, "-w") == 0) {
		GENERAL_OPTION;
		warnings_suppressed = TRUE;
	    } else if (strcmp(arg, "-x") == 0) {
		GENERAL_OPTION;
		option_popup_new_game_dialog = TRUE;
	    } else if (strcmp(arg, "--help") == 0) {
		GENERAL_OPTION;
		help_wanted = TRUE;
		/* Will display help info later. */
	    } else if (strcmp(arg, "--version") == 0) {
		GENERAL_OPTION;
		version_wanted = TRUE;
	    } else {
		numused = 0;
		/* Anything unrecognized during the last parse is an error. */
		if (spec >= leftover_options) {
		    fprintf(stderr, "Unrecognized option `%s'\n", arg);
		    had_error = TRUE;
		}
	    }
	    if (numused >= 1) {
		strcat(args_used, " ");
		strcat(args_used, argv[i]);
		argv[i] = "";
	    }
	    if (numused >= 2) {
		strcat(args_used, " ");
		strcat(args_used, argv[i+1]);
		argv[i+1] = "";
	    }
	    if (numused >= 1)
	      i += (numused - 1);
	} else {
	    if (spec == player_options) {
		if (*(argv[i]) == '+' || *(argv[i]) == '@') {
		    raw_default_player_spec = argv[i];
		} else {
		    add_a_raw_player_spec(argv[i]);
		}
		strcat(args_used, " ");
		strcat(args_used, argv[i]);
		argv[i] = NULL;
	    }
	}
    }
    if (version_wanted) {
	version_info();
    }
    if (had_error || help_wanted || variant_help_wanted) {
	/* If we want to get help about a particular game, have to
           load it first. */
	if (help_wanted || variant_help_wanted)
	  load_all_modules();
	if (had_error || help_wanted)
	  general_usage_info();
	if (had_error || help_wanted || variant_help_wanted)
	  game_usage_info();
	if (had_error || help_wanted)
	  player_usage_info();
	if (help_wanted && mainmodule != NULL) {
	    /* (should display other random info about the game here) */
	}
    }
    if (had_error || help_wanted || variant_help_wanted || version_wanted) {
	exit(had_error);
    }
}

/* Given a module name and/or filename, add it to the list of modules
   to load. */

void
add_a_module(name, filename)
char *name, *filename;
{
    Module *module;
    struct module_spec *extra;

    module = get_game_module(name);
    if (module == NULL)
      exit(1);  /* bad error if happens */
    if (filename)
      module->filename = copy_string(filename);
    if (mainmodule == NULL) {
	mainmodule = module;
    } else {
	extra = (struct module_spec *) xmalloc(sizeof(struct module_spec));
	extra->module = module;
	/* Add the module at the end of the list. */
	if (extra_modules == NULL)
	  extra_modules = extra;
	else
	  last_extra_module->next = extra;
	last_extra_module = extra;
    }
    game_chosen = TRUE;
}

static void
add_a_raw_player_spec(specstr)
char *specstr;
{
    struct raw_spec *spec =
      (struct raw_spec *) xmalloc (sizeof(struct raw_spec));

    spec->spec = copy_string(specstr);
    if (raw_player_specs == NULL)
      raw_player_specs = spec;
    else
      last_raw_player_spec->next = spec;
    last_raw_player_spec = spec;
}

static void
add_a_gdl_spec(specstr, firstp, lastp)
char *specstr;
struct gdl_spec **firstp, **lastp;
{
    struct gdl_spec *spec =
      (struct gdl_spec *) xmalloc (sizeof(struct gdl_spec));

    spec->spec = copy_string(specstr);
    if (*firstp == NULL)
      *firstp = spec;
    else
      (*lastp)->next = spec;
    *lastp = spec;
}

static void
version_info()
{
    printf("Xconq (%s) version %s\n", program_name, version_string());
}

/* This routine prints out help info about all the possible arguments. */

static void
general_usage_info()
{
    printf("Usage:\n\t%s [ -options ... ]\n\n", program_name);
    printf("General options:\n\n");
    printf("    -c n\t\tcheckpoint every <n> turns\n");
    printf("    -f filename\t\trun <filename> as a game\n");
    printf("    -g gamename\t\tfind <gamename> in library and run it\n");
    printf("    -help\t\tdisplay this help info\n");
    printf("    -host <game@host>\thost the given game\n");
    printf("    -join <game@host>\tconnect to the given game\n");
    printf("    -L pathname\t\tset <pathname> to be library location\n");
    printf("    -mail\t\tset up game as play-by-email\n");
    printf("    -w\t\t\tsuppress warnings\n");
    printf("    -design\t\tmake every player a designer");
#ifndef DESIGNERS
    printf(" (not available)");
#endif
    printf("\n");
    printf("Long options:\n");
    printf("    --help\t\tdisplay this help info\n");
    printf("    --version\t\tdisplay version info\n");
}

/* Describe the available variants for a game. */

static void
game_usage_info()
{
    int i, wid, hgt, circumf, lat, lon, pergame, perside, perturn;
    char *varname = "?", *vartypename = NULL;
    char buf[BUFSIZE];
    Variant *var;
    Obj *vardflt;

    printf("\nGame variant options");
    if (mainmodule == NULL) {
	printf(":\n\n    No game loaded, no information available.\n\n");
	return;
    }
    printf(" for \"%s\":\n\n", mainmodule->name);
    if (mainmodule->variants == NULL) {
	printf("    No variants available.\n\n");
	return;
    }
    for (i = 0; mainmodule->variants[i].id != lispnil; ++i) {
	var = &(mainmodule->variants[i]);
	varname = var->name;
	vartypename = c_string(var->id);
	vardflt = var->dflt;
	switch (keyword_code(vartypename)) {
	  case K_SEE_ALL:
	    printf("    -V\t\t\tmake everything be always seen (default %s)\n",
		   (vardflt == lispnil ? "true" :
		    (c_number(eval(vardflt)) ? "true" : "false")));
	    break;
	  case K_SEQUENTIAL:
	    printf("    -seq\t\t\tmove sequentially (default %s)\n",
		   (vardflt == lispnil ? "false" :
		    (c_number(eval(vardflt)) ? "false" : "true")));
	    printf("    -sim\t\t\tmove simultaneously (default %s)\n",
		   (vardflt == lispnil ? "true" :
		    (c_number(eval(vardflt)) ? "true" : "false")));
	    break;
	  case K_WORLD_SEEN:
	    printf("    -v\t\t\tmake the world be seen already (default %s)\n",
		   (vardflt == lispnil ? "true" :
		    (c_number(eval(vardflt)) ? "true" : "false")));
	    break;
	  case K_WORLD_SIZE:
	    printf("    -M wid[xhgt][Wcircumf][+lat][+long]\tset world size (default ");
	    /* Note that if the game definition sets these values
	       directly using world or area forms, this is misleading;
	       but that's the fault of the game designer for including
	       both preset values and a variant whose defaults don't
	       match those presets. */
	    circumf = DEFAULTCIRCUMFERENCE;
	    wid = DEFAULTWIDTH;  hgt = DEFAULTHEIGHT;
	    lat = lon = 0;
	    /* Pick the width and height out of the list. */
	    if (vardflt != lispnil) {
		wid = c_number(eval(car(vardflt)));
		vardflt = cdr(vardflt);
	    }
	    if (vardflt != lispnil) {
		hgt = c_number(eval(car(vardflt)));
		vardflt = cdr(vardflt);
	    } else {
		hgt = wid;
	    }
	    /* Pick up a circumference etc if given. */
	    if (vardflt != lispnil) {
		circumf = c_number(eval(car(vardflt)));
		vardflt = cdr(vardflt);
	    }
	    if (vardflt != lispnil) {
		lat = c_number(eval(car(vardflt)));
		vardflt = cdr(vardflt);
	    }
	    if (vardflt != lispnil) {
		lon = c_number(eval(car(vardflt)));
	    }
	    printf("%dx%dW%d", wid, hgt, circumf);
	    if (lat != 0 || lon != 0)
	      printf("+%d+%d", lat, lon);
	    printf(")\n");
	    break;
	  case K_REAL_TIME:
	    pergame = perside = perturn = 0;
	    if (vardflt != lispnil) {
		pergame = c_number(eval(car(vardflt)));
		vardflt = cdr(vardflt);
	    }
	    if (vardflt != lispnil) {
		perside = c_number(eval(car(vardflt)));
		vardflt = cdr(vardflt);
	    }
	    if (vardflt != lispnil) {
		perturn = c_number(eval(car(vardflt)));
	    }
	    printf("    -tgame mins\t\tlimit game time to <mins> minutes (default %d)\n",
		   pergame);
	    printf("    -tside mins\t\tlimit each player <mins> minutes in all (default %d)\n",
		   perside);
	    printf("    -tturn mins\t\tlimit each turn to <mins> minutes (default %d)\n",
		   perturn);
	    break;
	  default:
	    unixify_variant_name(buf, varname);
	    printf("    -v%s[=value] (default ", buf);
	    if (vardflt == lispnil
	        || (numberp(vardflt) && c_number(vardflt) == 0)) {
		printf("false");
	    } else if (numberp(vardflt) && c_number(vardflt) == 1) {
		printf("true");
	    } else {
		sprintlisp(buf, vardflt, BUFSIZE);
		printf("%s", buf);
	    }
	    printf(")\n");
	    break;
	}
    }
}

/* Replace blanks in a variant's name with hyphens, and put the whole
   name in lowercase. */

static void
unixify_variant_name(buf, varname)
char *buf, *varname;
{
    int i, slen;

    strcpy(buf, varname);
    slen = (int) strlen(buf);
    for (i = 0; i < slen; ++i) {
	if (buf[i] == ' ')
	  buf[i] = '-';
	if (isupper(buf[i]))
	  buf[i] = tolower(buf[i]);
    }
}

static void
player_usage_info()
{
    printf("\nPlayer setup options:\n\n");
    printf("    [[name][,ai][/config]@]host[+advantage]\tadd player\n");
    printf("        ai\t\t= name of AI type or \"ai\" for default type\n");
    printf("        config\t\t= name of config file\n");
    printf("        host\t\t= name of player's host machine or display\n");
    printf("        advantage\t= numerical initial advantage (default 1)\n");
    printf("    -e number[,ai]\tadd <number> computer players\n");
    printf("    -h number[,ai]\tadd <number> human players\n");
    printf("    -noai\tsuppress all AIs\n");
    printf("    -r\t\t\tno default player on local display\n");
    printf("    -wait minutes\t\twait time for players to join\n");
}

/* Given a string representing world dimensions, extract various components
   and compose a variant setting. */

static void
parse_world_option(str)
char *str;
{
    int width, height, circumference;
    char *str2;
    Obj *varval;

    width = atoi(str);
    str2 = strchr(str, 'x');
    if (str2 != NULL) {
	height = atoi(str2 + 1);
    } else {
	height = width;
    }
    varval = lispnil;
    /* Add a world circumference setting if present. */
    str2 = strchr(str, 'W');
    if (str2 != NULL) {
	circumference = atoi(str2 + 1);
	if (circumference > 0)
	  varval = cons(new_number(circumference), varval);
	else
	  fprintf(stderr, "Requested circumference is bad, ignoring\n");
    }
    varval = cons(new_number(width), cons(new_number(height), varval));
    /* Glue onto the list of variant_settings. */
    push_key_cdr_binding(&variant_settings, K_WORLD_SIZE, varval);
}

static void
parse_realtime_option(subopt, arg)
char *subopt, *arg;
{
    if (strcmp(subopt, "-tgame") == 0) {
	option_totalgametime = 60 * atoi(arg);
    } else if (strcmp(subopt, "-tside") == 0) {
	option_persidetime = 60 * atoi(arg);
    } else if (strcmp(subopt, "-tturn") == 0) {
	option_perturntime = 60 * atoi(arg);
    } else {
	fprintf(stderr, "Realtime option not one of -tgame, -tside, -tturn, ignoring\n");
    }
}

/* Given a variant, turn it into a list "(name val)". */

static void
parse_variant(str)
char *str;
{
    char *varname = NULL, *str2;
    Obj *varval = lispnil;

    if (strcmp(str, "") == 0) {
	push_key_int_binding(&variant_settings, K_WORLD_SEEN, 1);
    } else if (strcmp(str, "help") == 0) {
	variant_help_wanted = TRUE;
    } else {
	str2 = strchr(str, '=');
	if (str2 != NULL && str2 != str) {
	    /* (should interp val as string or number) */
	    varval = new_number(atoi(str2 + 1));
	    varname = copy_string(str);
	    varname[str2 - str] = '\0';
	} else {
	    /* Assume varname by itself means "enable" (set to value of 1). */
	    varname = str;
	    varval = new_number(1);
	}
	if (varname)
	  push_binding(&variant_settings, intern_symbol(varname), varval);
    }
}

/* Load all the game modules that were asked for on cmd line. */

void
load_all_modules()
{
    struct module_spec *extra;
    struct gdl_spec *gdlspec;
    Obj *gdlform;

    for (gdlspec = pre_gdl_specs; gdlspec != NULL; gdlspec = gdlspec->next) {
	gdlform = read_form_from_string(gdlspec->spec, NULL, NULL, NULL);
	interp_form(NULL, gdlform);
    }
    if (mainmodule != NULL) {
	load_game_module(mainmodule, TRUE);
	for (extra = extra_modules; extra != NULL; extra = extra->next) {
	    load_game_module(extra->module, TRUE);
	}
    } else {
	/* If we've got absolutely no files to load, the standard game is
	   the one to go for.  It will direct the remainder of random gen. */
	load_default_game();
    }
    for (gdlspec = post_gdl_specs; gdlspec != NULL; gdlspec = gdlspec->next) {
	gdlform = read_form_from_string(gdlspec->spec, NULL, NULL, NULL);
	interp_form(NULL, gdlform);
    }
}

/* Set module variants from command line options. */

void
set_variants_from_options()
{
    Obj *varval;

    /* Only the host of a networked game can set variants. */
    if (option_game_to_join != NULL) {
	if (variant_settings != lispnil)
	  fprintf(stderr, "Not the host, ignoring variant settings\n");
	return;
    }
    /* Build and add a real-time variant if any times were supplied. */
    if (option_totalgametime != 0
	|| option_persidetime != 0
	|| option_perturntime != 0) {
	varval = cons(new_number(option_totalgametime),
		      cons(new_number(option_persidetime),
			   cons(new_number(option_perturntime), lispnil)));
	/* Glue onto the list of variant_settings. */
	push_key_cdr_binding(&variant_settings, K_REAL_TIME, varval);
    }
    do_module_variants(mainmodule, variant_settings);
}

/* Set player characteristics from command-line options. */

void
set_players_from_options()
{
    int mergespecs;
    Player *player;
    struct raw_spec *spec;

    /* Only the host of a networked game can set up players. */
    if (option_game_to_join != NULL) {
	if (default_player_spec == NULL)
	  default_player_spec = xmalloc(BUFSIZE);
	default_player_spec[0] = '\0';
	if (!empty_string(getenv("USER"))) {
	    strcpy(default_player_spec, getenv("USER"));
	    strcat(default_player_spec, "@");
	}
	if (!empty_string(raw_default_player_spec)
	    && raw_default_player_spec[0] == '@') {
	    strcat(default_player_spec, raw_default_player_spec);
	} else if (!empty_string(getenv("DISPLAY"))) {
	    strcat(default_player_spec, getenv("DISPLAY"));
	}
	return;
    }
    player = NULL;
    /* Assume that if any players exist already, then this is a restored
       game of some sort, and merge instead of adding new players. */
    mergespecs = (numplayers > 0);
    if (mergespecs)
      player = playerlist;
    /* Add the default player. */
    if (raw_player_specs == NULL || option_add_default_player) {
	if (!mergespecs)
	  player = add_default_player();
	parse_player_spec(player, raw_default_player_spec);
	canonicalize_player(player);
	player = player->next;
    }
    /* Add the explicitly listed players. */
    for (spec = raw_player_specs; spec != NULL; spec = spec->next) {
	if (mergespecs) {
	    if (player == NULL) {
		fprintf(stderr, "Excess player spec \"%s\", ignoring\n",
			spec->spec);
		continue;
	    }
	} else {
	    player = add_player();
	}
	parse_player_spec(player, spec->spec);
	canonicalize_player(player);
	player = player->next;
    }
    /* If we made a special request to suppress AIs, go through all
       players existing at this time. */
    if (initially_no_ai) {
	for_all_players(player) {
	    player->aitypename = NULL;
	}
    }
}

/* This is not, strictly speaking, part of command line processing,
   but command-line programs are also the ones for which stdio is
   useful. */

void
print_instructions()
{
    Obj *instructions = mainmodule->instructions, *rest;

    printf("\n");
    if (instructions != lispnil) {
	if (stringp(instructions)) {
	    printf("%s\n", c_string(instructions));
	} else if (consp(instructions)) {
	    for_all_list(instructions, rest) {
		if (stringp(car(rest))) {
		    printf("%s\n", c_string(car(rest)));
		} else {
		    run_warning("Instructions are of wrong type");
		}
	    }
	} else {
	    run_warning("Instructions are of wrong type");
	}
    } else {
	printf("(no instructions supplied)\n");
    }
}