File: parse.c

package info (click to toggle)
xbattle 5.4.1-5
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 700 kB
  • ctags: 695
  • sloc: ansic: 9,103; sh: 847; makefile: 66
file content (1851 lines) | stat: -rw-r--r-- 47,195 bytes parent folder | download | duplicates (3)
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
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "constant.h"
#include "options.h"
  
/**** x include files ****/
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xatom.h>
#include <X11/keysym.h>
#include <X11/keysymdef.h>

#if USE_LONGJMP
#include <setjmp.h>
#endif

#include "extern.h"
#include "parse.h"


/******************************************************************************
  load_options (argc, argv)

  Initialize all variables, parse the command line arguments, and clean up the
  variables for later use.
******************************************************************************/

load_options (argc, argv)
  int argc;
  char *argv[];
{
  char *argv_new[MAX_TEXT];
  int argc_new;

  check_options (argc, argv, TRUE);
 
  argc_new = load_command_line (argc, argv, argv_new);

  preload_options (argc_new, argv_new);

  check_options (argc_new, argv_new, FALSE);

  parse_options (argc_new, argv_new);
 
  clean_options ();
}



/******************************************************************************
  check_options (argc, argv, ignore_colors)

  Step through each of the command line arguments specified in <argv[]>, 
  checking for compliance with list of options in global <Options[]>.  Flag
  any unknown options or argument count mismatches.  Call find_color_match()
  to check for side declarations (which are never in <Options[]>).  If
  <ignore_colors> then all unknown options are assumed to be colors and are
  treated as such (basically just makes sure parameter counts match).
******************************************************************************/

check_options (argc, argv, ignore_colors)
  int argc,
      ignore_colors;
  char *argv[];
{
  int i, j,
      all_valid,
      option_index,
      side_index,
      active_side_index,
      option_count,
      in_bracket,
      is_enabled,
      parameter_count,
      target_parameter_count;
  char color_name[200],
       load_name[200];

  in_bracket = FALSE;
  active_side_index = -1;
  all_valid = TRUE;

  /** Step through each command line argument **/

  for (i=1; i<argc; i++)
  {
    /** Check to see if argument is valid option **/

    option_index = find_option (argv[i]);

    if (option_index >= OPTION_COUNT)
    {
      is_enabled = FALSE;
      option_index -= OPTION_COUNT;
    }
    else
      is_enabled = TRUE;

    /** Check to see if it is a short-hand "-options x.xbo" = "-x.xbo" **/

    if (option_index < 0)
      option_index = find_load_filename (argv[i], load_name);

    /** If within side brackets ("-red { <options> } me") **/

    if (active_side_index >= 0)
    {
      /** Set state of brackethood **/

      if (option_index == 0)
        in_bracket = TRUE;
      else if (option_index == 1)
      {
        in_bracket = FALSE;
        side_index = active_side_index;
        active_side_index = -1;
      }
    }

    /** If not a valid option, and not in brackets, find color **/

    if (option_index < 0 && !in_bracket)
    {
      if (ignore_colors)
        side_index = 0;
      else
        side_index = find_color_match (argv[i], color_name, 0);
    }
    else
      side_index = -1;

    /** Find number of parameters **/

    parameter_count = find_parameter_count (argc, argv, i);

    /** If argument is some type of valid color **/

    if (side_index >= 0)
    {
      /** Make sure that next <argv[]> entry is display name or '{' **/

      if (parameter_count == 0 && (i+1)<argc && argv[i+1][0] == '{')
        active_side_index = side_index;
      else if (parameter_count != 1)
      {
        all_valid = FALSE;
        throw_warning ("Unresolvable color %s", argv[i]);
      }
    }
    else if (option_index < 0)
    {
      /** Else invalid option **/

      all_valid = FALSE;
      throw_warning ("Unresolvable argument %s", argv[i]);
    }
    else
    {
      /** Else valid option **/

      target_parameter_count = Options[option_index].count;
      if (!is_enabled && target_parameter_count == 1)
        target_parameter_count = 99;

      /** If option supports either 0 or 1 parameters **/

      if (target_parameter_count > 98)
      {
        /** If parameter count mismatch **/

        if (parameter_count > 1)
        {
          all_valid = FALSE;
          throw_warning ("Parameter miscount %s", argv[i]);
        }
      }
      else if (parameter_count != target_parameter_count)
      {
        /** Else if parameter count mismatch **/

        all_valid = FALSE;
        throw_warning ("Parameter miscount %s", argv[i]);
      }
    }

    /** Step to next option **/

    i += parameter_count;
  }

  /** If still in bracket, something is wrong with command line **/

  if (in_bracket)
  {
    all_valid = FALSE;
    throw_warning ("Bracket mismatch", NULL);
  }

  /** If not all arguments are valid, exit the program **/

  if (!all_valid)
    throw_error ("Poorly formed command line", NULL);
}



/******************************************************************************
  int
  load_command_line (argc, argv, command_line)

  Copy over all arguments from <argv[]> into <command_line[]>, expanding any
  default and option files inline.  Return the new number of arguments.
******************************************************************************/

load_command_line (argc, argv, command_line)
  int argc;
  char *argv[],
       *command_line[];
{
  int i, j,
      argc_new,
      option_index,
      parameter_count;

  char filename[200],
       alternative_filename[500],
       line[200],
       argument[200],
       *home_dir,
       *ptr,
       *getenv();

  char *strip_first();

  FILE *fp;

  /** Copy over invocation argument **/

  argc_new = 0;
  command_line[argc_new++] = (char *)(strdup (argv[0]));


  /** Open up default file, if it exists **/

  if ((fp = fopen (".xbattle", "r")) == NULL)
  {
    /** Not in current directory, so check home directory **/

#if UNIX
    home_dir = getenv ("HOME");
    if (home_dir != NULL)
    {
      sprintf (line, "%s/.xbattle", home_dir);
      fp = fopen (line, "r");
    }
#endif
  }

  /** If there is a default file, copy all options into command line **/

  if (fp)
  {
    while (fgets (line, 200, fp) != NULL)
    {
      ptr = line;
      while (copy_first (argument, ptr))
      {
        command_line[argc_new++] = (char *)(strdup (argument));
        ptr = strip_first (ptr);
      }
    }
    fclose (fp);
  }

  /** Step through all original command line arguments **/

  for (i=1; i<argc; i++)
  {
    option_index = find_option (argv[i]);

    /** Check to see if it is a short-hand "-options x.xbo" = "-x.xbo" **/

    if (option_index < 0)
      option_index = find_load_filename (argv[i], filename);
    else
      filename[0] = '\0';

    /** Find the parameter count **/

    parameter_count = find_parameter_count (argc, argv, i);

    /** If the argument is "-options", load option file **/

    if (option_index == OPTION_OPTIONS)
    {
      /** If no parameter load default, else load specified file **/

      if (parameter_count == 0)
      {
        if (filename[0] == '\0')
          strcpy (filename, "default.xbo");
      }
      else
        strcpy (filename, argv[i+1]);

      /** If cannot open file, try default directory **/

      if ((fp = fopen (filename, "r")) == NULL)
      {
        sprintf (alternative_filename, "%s/%s", DEFAULT_XBO_DIR, filename);
        if ((fp = fopen (alternative_filename, "r")) == NULL)
          throw_error ("Cannot open option file %s", filename);
      }

      /** Copy all options into command line **/ 

      while (fgets (line, 200, fp) != NULL)
      {
        ptr = line;
        while (copy_first (argument, ptr))
        {
          command_line[argc_new++] = (char *)(strdup (argument));
          ptr = strip_first (ptr);
        }
      }
      fclose (fp);
    }
    else
    {
      /** Else not "-options" argument so copy directly to command line **/

      command_line[argc_new++] = (char *)(strdup (argv[i]));
      for (j=0; j<parameter_count; j++)
        command_line[argc_new++] = (char *)(strdup (argv[i+j+1]));
    }

    /** Increment <argv[]> list pointer to next option **/

    i += parameter_count;
  }

  /** Return new number of command line arguments **/

  return (argc_new);
}



/******************************************************************************
  preload_options (argc, argv)

  Handle the preloading of certain options which are necessary for the
  proper installation of other options.  For example, custom colors must be
  preloaded so that they are not flagged as illegal options when used.
******************************************************************************/

preload_options (argc, argv)
  int argc;
  char *argv[];
{
  int i, j,
      parameter_count,
      index,
      option_index;
  double atof();

  for (i=1; i<argc; i++)
  {
    /** Find option index and parameter count **/

    option_index = find_option (argv[i]);
    parameter_count = find_parameter_count (argc, argv, i);

    switch (option_index)
    {
      case OPTION_COLOR:

        load_color (argv[i+1], atoi(argv[i+2]),
				atoi(argv[i+3]), atoi(argv[i+4]));
        break;

      case OPTION_COLOR_INVERSE:

        load_color_inverse (argv[i+1], argv[i+2]);
        break;

      case OPTION_STIPPLE:

        load_stipple (argv[i+1], &argv[i+2]);
        break;

      case OPTION_HILL_COLOR:

        index = atoi (argv[i+1]);
        if (index >= MAX_HILL_TONES)
          index = MAX_HILL_TONES-1;
        for (j=0; j<3; j++)
          Config->palette_hills[index][j] = atoi (argv[i+j+2]);
        break;

      case OPTION_FOREST_COLOR:

        index = atoi (argv[i+1]);
        if (index >= MAX_FOREST_TONES)
          index = MAX_FOREST_TONES-1;
        for (j=0; j<3; j++)
          Config->palette_forest[index][j] = atoi (argv[i+j+2]);
        break;

      case OPTION_SEA_COLOR:

        index = atoi (argv[i+1]);
        if (index >= MAX_SEA_TONES)
          index = MAX_SEA_TONES-1;
        for (j=0; j<3; j++)
          Config->palette_sea[index][j] = atoi (argv[i+j+2]);
        break;
    }

    i += parameter_count;
  }
}




/******************************************************************************
  parse_options (argc, argv)

  Given the command line arguments, with default and option files already
  expanded, parse the options and assign player sides.
******************************************************************************/

parse_options (argc, argv)
  int argc;
  char *argv[];
{
  int i, j,
      parameter_count,
      option_index,
      primary_index,
      secondary_index,
      is_enabled,
      side,
      active_side,
      side_count,
      player_count,
      has_colon;

  char primary_color_name[200],
       secondary_color_name[200],
       display_name[200];

  /** No players, no sides, no active side **/

  active_side = -1;
  player_count = 0;
  side_count = 0;

  /** Set all sides names to nothingness **/ 

  for (side=0; side<MAX_SIDES; side++)
  {
    strcpy (Config->side_to_hue_name[side], "");
    strcpy (Config->side_to_bw_name[side], "");
  }
  Config->message_all[0] = '\0';

  /** Step through each argument in <argv[]> **/

  for (i=1; i<argc; i++)
  {
    /** Check to see if argument is option or negated option **/

    option_index = find_option (argv[i]);
    if (option_index >= OPTION_COUNT)
    {
      is_enabled = FALSE;
      option_index -= OPTION_COUNT;
    }
    else
      is_enabled = TRUE;

    /** Find the number of paramters the argument takes **/

    parameter_count = find_parameter_count (argc, argv, i);

    /** If argument is option or negated option, install it **/

    if (option_index >= 0)
    {
      install_option (option_index, &argv[i],
		parameter_count, active_side, is_enabled);

    }
    else if (active_side == -1)
    {
      /** Else if there is no active side yet **/

      /** Set primary and secondary color indices **/

      primary_index = find_color_match (argv[i], primary_color_name, 0);
      if (primary_index >= 0)
        secondary_index = find_color_match (argv[i], secondary_color_name, 1);
      else
        secondary_index = -1;

      /** Set active side, checking to see if side color already observed **/

      active_side = side_count;
      for (side=0; side<side_count; side++)
        if (strcmp (primary_color_name, Config->side_to_hue_name[side]) == 0)
          active_side = side;

      /** If there is a single parameter, we're dealing with a case	**/	
      /** like "-red cnsx45", as opposed to "-red { -guns 5 } cnsx45",	**/
      /** so artificially set the option_index to "}" (1).		**/

      if (parameter_count == 1)
        option_index = 1;
    }

    /** If there is an active side and option is a "}" **/

    if (active_side >= 0 && option_index == 1)
    {
      /** Copy the display name **/

      strcpy (display_name, argv[i+1]);

      /** If the display name isn't the official dummy display "you" **/

      if (strcmp (display_name, "you") != 0)
      {
        /** Initialize the xwindow_type structure for the player **/

        XWindow[player_count] = (xwindow_type *)(malloc(sizeof(xwindow_type)));
 
        if (strcmp (argv[i+1], "me") == 0)
          strcpy (XWindow[player_count]->display_name, "");
        else
        {
          /** If no ":x.y" suffix given in display name add ":0.0" ****/

          has_colon = FALSE;
          for (j=0; display_name[j] != '\0'; j++)
            if (display_name[j] == ':')
              has_colon = TRUE;
          strcpy (display_name, argv[i+1]);
          if (!has_colon)
            strcat (display_name, ":0.0");
 
          strcpy (XWindow[player_count]->display_name, display_name);
        }
      }
      
      /** If player represents a new side **/

      if (active_side == side_count)
      {
        /** Set player_to_side and side_to_hue_name mappings **/ 

        strcpy (Config->side_to_hue_name[side_count], primary_color_name);
        Config->player_to_side[player_count] = side_count;

        /** Assign palette index to side **/

        Config->side_to_hue[side_count] = primary_index;

        /** If no secondary color specified, set side_to_bw mapping **/

        if (secondary_index < 0)
        {
          if (Config->hue_has_bw[primary_index])
          {
            Config->side_to_bw[side_count] = primary_index;
          }
          else
          {
            /** Else primary color was a non b&w custom color **/

            if (Config->palette[primary_index][0] > 128)
              Config->side_to_bw[side_count] = 2;
            else
              Config->side_to_bw[side_count] = 1;
          }

          /** Set side_to_bw_name mapping **/
 
          strcpy (Config->side_to_bw_name[side_count], primary_color_name);
        }
        else
        {
          /** Else a secondary color was specified, set side_to_bw mapping **/

          strcpy (Config->side_to_bw_name[side_count], secondary_color_name);

          if (Config->hue_has_bw[secondary_index])
            Config->side_to_bw[side_count] = secondary_index;
          else
            Config->side_to_bw[side_count] = secondary_index%Config->bw_count;
        }

        /** If not secondary color and non b&w primary color **/

        if (secondary_index < 0 && !Config->hue_has_bw[primary_index])
        {
          /** Set up side_to_letter mappings for letter-in-troop display **/

          Config->side_to_letter[side_count][0] =
			Config->side_to_hue_name[side_count][0];
          Config->side_to_letter[side_count][1] = '\0';
        }
        else
          Config->side_to_letter[side_count][0] = FALSE;

        side_count++;
      }
      else
      {
        /** Player represents old side, so set mapping **/

        Config->player_to_side[player_count] = active_side;
      }

      /** If not a dummy player, increment player count **/

      if (strcmp (display_name, "you") != 0)
        player_count++;

      /** Reset to no active side **/

      active_side = -1;
    }

    /** Step ahead to next option/color in <argv[]> **/

    i += parameter_count;
  }

  /** Set global count values **/

  Config->side_count = side_count;
  Config->player_count = player_count;

  /** If there are no valid sides, show usage message **/

  if (Config->side_count < 1 && !Config->enable_all[OPTION_REPLAY])
  {
    print_usage_message ();
    exit (0);
  }
}



/******************************************************************************
  install_option (option_index, argv, parameter_count, side, is_enabled)

  Given the option indexed by <option_index>, with <parameter_count> arguments
  specified in <argv>, install (or disinstall if NOT <is_enabled>) the option
  and related variables.  Option is installed to all sides if <side> is < 0,
  else it is installed to just <side>.
******************************************************************************/

install_option (option_index, argv, parameter_count, side, is_enabled)
  int option_index,
      parameter_count,
      side,
      is_enabled;
  char *argv[];
{
  int i,
      value_int;
  double value_double;
  char option[MAX_TEXT];

  /** If there are parameters, set int and double values **/

  if (parameter_count)
  {
    value_int = atoi (argv[1]);
    value_double = atof (argv[1]);
  }
  else
  {
    value_int = Options[option_index].value_int;
    value_double = Options[option_index].value_double;
  }

  switch (option_index)
  {
    case OPTION_LEFT_BRACKET:
    case OPTION_RIGHT_BRACKET:

      break;

    case OPTION_COLOR:
    case OPTION_HILL_COLOR:
    case OPTION_FOREST_COLOR:
    case OPTION_SEA_COLOR:

      break;

    case OPTION_STORE:
    case OPTION_REPLAY:
    case OPTION_LOAD:
    case OPTION_DUMP:
    case OPTION_EDIT:

      break;

    /** Only add certain options to player text message **/

    case OPTION_AREA:
    case OPTION_ATTACK:
    case OPTION_ARMIES:
    case OPTION_BASEMAP:
    case OPTION_BASES:
    case OPTION_BOUND:
    case OPTION_BUILD:
    case OPTION_BUILD_COST:
    case OPTION_BUILD_LIMIT:
    case OPTION_DECAY:
    case OPTION_DIG:
    case OPTION_DIG_COST:
    case OPTION_DIGIN:
    case OPTION_DISRUPT:
    case OPTION_ERODE:
    case OPTION_ERODE_THRESH:
    case OPTION_FARMS:
    case OPTION_FIGHT:
    case OPTION_FILL:
    case OPTION_FILL_COST:
    case OPTION_FOREST:
    case OPTION_ARTILLERY:
    case OPTION_ARTILLERY_COST:
    case OPTION_ARTILLERY_DAMAGE:
    case OPTION_HIDDEN:
    case OPTION_HILLS:
    case OPTION_HORIZON:
    case OPTION_LOCALMAP:
    case OPTION_MANAGE:
    case OPTION_MARCH:
    case OPTION_MAXVAL:
    case OPTION_MILITIA:
    case OPTION_MOVE:
    case OPTION_NOSPIGOT:
    case OPTION_PARATROOPS:
    case OPTION_PARATROOPS_COST:
    case OPTION_PARATROOPS_DAMAGE:
    case OPTION_RBASES:
    case OPTION_RBASE_RANGE:
    case OPTION_REPEAT:
    case OPTION_RESERVE:
    case OPTION_SCUTTLE:
    case OPTION_SCUTTLE_COST:
    case OPTION_SPEED:
    case OPTION_WRAP:

      if (is_enabled)
      {
        if (parameter_count)
          sprintf (option, "%s=%d ",
			&(Options[option_index].option[1]), value_int);
        else
          sprintf (option, "%s ", &(Options[option_index].option[1]));
      }
      else
        sprintf (option, "no_%s ", &(Options[option_index].option[1]));
      strcat (Config->message_all, option);

      break;

    default:

      break;
  }

  /** Following if allows something like "-red { -no_guns } me" without	**/
  /** disabling guns for everyone.					**/

  if (is_enabled || side < 0)
    Config->enable_all[option_index] = is_enabled;

  /** Set all and single enables and values **/

  set_uchar_array (Config->enable[option_index], is_enabled, side);
  Config->value_int_all[option_index] = value_int;
  set_int_array (Config->value_int[option_index], value_int, side);
  Config->value_double_all[option_index] = value_double;
  set_double_array (Config->value_double[option_index], value_double, side);

  /** Handle some of the peskier options that require special treatment **/

  switch (option_index)
  {
    /** Need to set x and y board sizes **/

    case OPTION_BOARD:

      Config->value_int_all[OPTION_BOARDX] = value_int;
      Config->value_int_all[OPTION_BOARDY] = value_int;
      break;

    /** Always must be linked to fill and sea tones **/

    case OPTION_DIG:

      if (parameter_count)
      {
        Config->value_int_all[OPTION_FILL] = value_int;
        Config->value_int_all[OPTION_SEA_TONES] = value_int;
      }
      break;

    /** If has parameter, link to sea tones **/

    case OPTION_FILL:

      if (parameter_count)
        Config->value_int_all[OPTION_SEA_TONES] = value_int;

      break;

    /** Have to enable the terrain **/

    case OPTION_FOREST:
    case OPTION_HILLS:
    case OPTION_SEA:

      Config->enable_terrain = TRUE;
      break;

    /** Just print usage message and quit **/

    case OPTION_HELP:

      if (is_enabled)
      {
        print_usage_message ();
        exit (0);
      }
      break;

    /** Handle the four options which take file name parameters **/

    case OPTION_EDIT:

      Config->use_brief_load = FALSE;
      if (parameter_count)
        strcpy (Config->file_store_map, argv[1]);
      else
        strcpy (Config->file_store_map, "xbattle.xbt");
      break;

    case OPTION_LOAD:

      if (parameter_count)
        strcpy (Config->file_map, argv[1]);
      else
        strcpy (Config->file_map, "xbattle.xbt");
      break;

    case OPTION_REPLAY:

      if (parameter_count)
        strcpy (Config->file_replay, argv[1]);
      else
        strcpy (Config->file_replay, "xbattle.xba");
      break;

    case OPTION_STORE:

      if (parameter_count)
        strcpy (Config->file_store, argv[1]);
      else
        strcpy (Config->file_store, "xbattle.xba");
      break;
  }
}



/******************************************************************************
  clean_options ()

  Set global variables base on previously installed options.  Reconcile any
  paradoxical settings and initialize any variables that couldn't be set
  before all other options were installed.
******************************************************************************/

clean_options ()
{
  int i,
      side,
      value_int;

  double full,
         value_double,
         game_speed,
         sqrt();

  char dummy[500];

  FILE *fptemp,
       *fopen();

  game_speed =				Config->value_double_all[OPTION_SPEED];
  if (game_speed == 0.0)
    game_speed = 0.5;
  Config->delay =			(int)(25000.0/game_speed + 0.5);

  if (Config->value_int_all[OPTION_SEED] == 0)
    Config->value_int_all[OPTION_SEED] = getpid() + time(NULL);

  Config->board_x_size =		Config->value_int_all[OPTION_BOARDX];
  Config->board_y_size =		Config->value_int_all[OPTION_BOARDY];
  Config->fill_number =			Config->value_int_all[OPTION_SEA_TONES];
  Config->value_int_all[OPTION_FILL] =	Config->value_int_all[OPTION_SEA_TONES];
  Config->value_int_all[OPTION_DIG] =	Config->value_int_all[OPTION_SEA_TONES];
  Config->value_int_all[OPTION_SEED] =	Config->value_int_all[OPTION_SEED];

  for (side=0; side<Config->side_count; side++)
    Config->max_value[side] = Config->value_int[OPTION_MAXVAL][side];

  /** Find and set the maximum max_value - used as baseline **/

  Config->max_max_value = 0;
  for (side=0; side<Config->side_count; side++)
    if (Config->max_value[side] > Config->max_max_value)
      Config->max_max_value = Config->max_value[side];

  for (side=0; side<Config->side_count; side++)
  {
    /** Set costs as fractional parts of maxval, if necessary **/

    if (Config->value_int[OPTION_DIG_COST][side] <= 1)
      Config->value_int[OPTION_DIG_COST][side] = (int)
        (Config->value_double[OPTION_DIG_COST][side] * Config->max_max_value);

    if (Config->value_int[OPTION_FILL_COST][side] <= 1)
      Config->value_int[OPTION_FILL_COST][side] = (int)
	(Config->value_double[OPTION_FILL_COST][side] * Config->max_max_value);

    if (Config->value_int[OPTION_BUILD_COST][side] <= 1)
      Config->value_int[OPTION_BUILD_COST][side] = (int)
	(Config->value_double[OPTION_BUILD_COST][side] * Config->max_max_value);

    if (Config->value_int[OPTION_SCUTTLE_COST][side] < 1)
      Config->value_int[OPTION_SCUTTLE_COST][side] = (int)
  	(Config->value_double[OPTION_SCUTTLE_COST][side] * Config->max_max_value);

    Config->max_value[side] = Config->value_int[OPTION_MAXVAL][side];

    Config->view_range[side] =	Config->value_int[OPTION_HORIZON][side];
    Config->cell_size[side] =	Config->value_int[OPTION_CELL][side];

    value_int = 	Config->value_int[OPTION_BUILD][side];
    Config->value_int[OPTION_BUILD][side] = ANGLE_FULL/value_int;

    value_double = 	Config->value_double[OPTION_DECAY][side];
    Config->value_double[OPTION_DECAY][side] =
			10.0*value_double/((double)(Config->max_max_value));

    value_double = 	Config->value_double[OPTION_FIGHT][side];
    Config->value_double[OPTION_FIGHT][side] =
		value_double/(1.0+(game_speed/5.0));

    value_double = 	Config->value_double[OPTION_MOVE][side];
    Config->value_double[OPTION_MOVE][side] = (10.0-value_double)*MOVE_FACTOR;

    value_double = 	Config->value_double[OPTION_HILLS][side];
    Config->value_double[OPTION_HILLS][side] = value_double*HILL_FACTOR;

    value_double = 	Config->value_double[OPTION_FOREST][side];
    Config->value_double[OPTION_FOREST][side] = value_double*FOREST_FACTOR;
  }

  /** Eliminate hills vs. forest paradox, hills get precedence **/

  if (Config->enable_all[OPTION_HILLS] && Config->enable_all[OPTION_FOREST])
    Config->enable_all[OPTION_FOREST] = FALSE;

  /** Eliminate any inconsistencies in mapping/horizon techniques **/

  for (side=0; side<Config->side_count; side++)
  {
    if (Config->enable[OPTION_MAP][side] ||
		Config->enable[OPTION_LOCALMAP][side])
    {
      Config->enable_all[OPTION_HORIZON] = TRUE;
      Config->enable[OPTION_HORIZON][side] = TRUE;
    }

    if (Config->enable[OPTION_LOCALMAP][side])
    {
      Config->enable[OPTION_MAP][side] = TRUE;
      Config->enable[OPTION_BASEMAP][side] = FALSE;
    }
  }

  /** Make sure maximum view range accurately reflects all view ranges **/

  if (Config->enable_all[OPTION_HORIZON])
  { 
    for (side=0; side<Config->side_count; side++)
    {
      if (Config->enable[OPTION_HORIZON][side])
        if (Config->view_range[side] > Config->view_range_max)
          Config->view_range_max = Config->view_range[side];
    }
  }

  /** Set up level (elevation) limits **/

  if (Config->enable_all[OPTION_SEA])
    Config->level_min = -(Config->value_int_all[OPTION_FILL]);
  else
    Config->level_min = 0;

  if (Config->enable_all[OPTION_FOREST])
    Config->level_max = Config->value_int_all[OPTION_FOREST_TONES]-1;
  else if (Config->enable_all[OPTION_HILLS])
    Config->level_max = Config->value_int_all[OPTION_HILL_TONES]-1;
  else
    Config->level_max = 0;


  /** Set up game storage **/

  if (Config->enable_all[OPTION_STORE])
  {
    if ((Config->fp = fopen (Config->file_store, "w")) == NULL)
      throw_error ("Cannot open storage file %s", Config->file_store);
    store_parameters ();
  }

  /** Set up game replay **/

  if (Config->enable_all[OPTION_REPLAY])
  {
    if (strcmp (".Z", (char *)(strchr (Config->file_replay, '\0'))-2) == 0)
    {
      sprintf (dummy, "zcat %s", Config->file_replay);
      Config->fp = popen (dummy, "r");
    }
    else
      Config->fp = fopen (Config->file_replay, "r");

    /** If cannot open file, try default directory **/

    if (Config->fp == NULL)
    {
      if (strcmp (".Z", (char *)(strchr (Config->file_replay, '\0'))-2) == 0)
      {
        sprintf (dummy, "zcat %s/%s", DEFAULT_XBA_DIR, Config->file_replay);
        Config->fp = popen (dummy, "r");
      }
      else
      {
        sprintf (dummy, "%s/%s", DEFAULT_XBA_DIR, Config->file_replay);
        Config->fp = fopen (dummy, "r");
      }

      if (Config->fp == NULL)
        throw_error ("Cannot open replay file %s", Config->file_replay);
    }

    load_parameters ();

    /** Don't want to load the board when replaying **/

    Config->enable_all[OPTION_LOAD] = FALSE;
  }

  /** Set up game load **/

  if (Config->enable_all[OPTION_LOAD])
  {
    /** If cannot open file, try default directory **/

    if ((Config->fp = fopen (Config->file_map, "r")) == NULL)
    {
      sprintf (dummy, "%s/%s", DEFAULT_XBT_DIR, Config->file_map);
      if ((Config->fp = fopen (dummy, "r")) == NULL)
        throw_error ("Cannot open map file %s", Config->file_map);
    }

    load_board_header (Config->file_map);
  }
  else
  {
    if (Config->enable_all[OPTION_SQUARE])
      Config->tile_type = TILE_SQUARE;
    else if (Config->enable_all[OPTION_HEX])
      Config->tile_type = TILE_HEX;
    else if (Config->enable_all[OPTION_OCTAGON])
      Config->tile_type = TILE_OCTAGON;
    else if (Config->enable_all[OPTION_DIAMOND])
      Config->tile_type = TILE_DIAMOND;
    else if (Config->enable_all[OPTION_TRIANGLE])
      Config->tile_type = TILE_TRIANGLE;
    else
      Config->tile_type = TILE_SQUARE;
  }

  /** Set full hill/forest/sea palette **/

  set_palette (Config->palette_hills,
			 Config->value_int_all[OPTION_HILL_TONES],
			 MAX_HILL_TONES);
  set_palette (Config->palette_forest,
			Config->value_int_all[OPTION_FOREST_TONES],
			MAX_FOREST_TONES);
  set_palette (Config->palette_sea,
			Config->value_int_all[OPTION_SEA_TONES],
			MAX_SEA_TONES);

  /** Set movement biasing (due to hills, forests, etc.) **/

  set_move_parameters ();

  /** Need to set maximum directions before cell allocation **/

  switch (Config->tile_type)
  {
    case TILE_HEX:
      Config->direction_count = 6;
      break;

    case TILE_OCTAGON:
      Config->direction_count = 8;
      break;

    case TILE_SQUARE:
      Config->direction_count = 4;
      break;

    case TILE_DIAMOND:
      Config->direction_count = 4;
      break;

    case TILE_TRIANGLE:
      Config->direction_count = 3;
      break;
  }

  /** Copy global message to each player's local message **/

#if USE_MULTITEXT
  for (side=0; side<Config->side_count; side++)
    strcpy (Config->message_single[side], Config->message_all);
#endif
}



/******************************************************************************
  set_move_parameters ()

  Set movement factors which determine how troops move between cells.  This
  includes handling hills, forests, moves, and digins.  Eases the computational
  load on update_slope().
******************************************************************************/

set_move_parameters ()
{
  int side,
      level,
      value,
      moves;
  double *ptr;

  /** For each side **/

  for (side=0; side<Config->side_count; side++)
  {
    /** Handle level differences due to HILLS **/

    ptr = (double *)(malloc(sizeof(double)*(2*Config->level_max+1)));
    Config->move_slope[side] = ptr + Config->level_max;

    if (Config->level_max == 0)
      Config->move_slope[side][0] = 0.0;
    else
      for (level=-Config->level_max; level<=Config->level_max; level++)
        Config->move_slope[side][level] = Config->value_int[OPTION_HILLS][side] *
                ((double)(level))/Config->level_max/HILLS_DIVISOR;

    /** Handle level effect due to MOVE and FOREST **/

    for (level=0; level<=Config->level_max; level++)
    {
      Config->move_hinder[side][level] = Config->value_int[OPTION_MOVE][side] +
		Config->value_int[OPTION_FOREST][side] * level;
      Config->move_hinder[side][level] = 1.0/Config->move_hinder[side][level];
    }

    /** Handle troop effect due to DIGIN **/

    for (value=0; value<=MAX_MAXVAL+1; value++)
    {
      Config->move_shunt[side][value] = Config->value_int[OPTION_DIGIN][side] *
		((double)(value))/Config->max_value[side] + 1.0;
      Config->move_shunt[side][value] = 1.0/Config->move_shunt[side][value];
    }
  }

  /** Take inverse of moves values **/

  for (moves=1; moves<MAX_DIRECTIONS; moves++)
    Config->move_moves[moves] = 1.0/moves;
  Config->move_moves[0] = 0.0;
}



/******************************************************************************
  set_palette (palette, count, max_count)

  Fill in <palette> with <count> entries by interpolating between entries
  which are already in the palette.  Use the <max_count> entry as the top
  of the palette if there isn't a valid top entry already.
******************************************************************************/

set_palette (palette, count, max_count)
  short palette[][3],
      count,
      max_count;
{
  int i, j, k,
      index,
      last_solid_index,
      miss_count,
      source[3],
      target[3];

  double fraction;

  if (count < 2)
    return;

  /** Set the top entry if there isn't one already **/

  if (palette[count-1][0] < 0)  
  {
    for (j=0; j<3; j++)
      palette[count-1][j] = palette[max_count-1][j];
  }

  /** For each palette entry **/

  for (i=0; i<count; i++)
  {
    /** If there is an entry **/

    if (palette[i][0] >= 0)
    {
      last_solid_index = i;

      /** Set interpolation anchor point **/

      for (j=0; j<3; j++)
        source[j] = palette[i][j];

      /** Find out how many missing entries there are in a row **/

      miss_count = 0;
      for (k=1; palette[i+k][0] < 0; k++)
        miss_count++;

      /** Set the other interpolation anchor point **/

      for (j=0; j<3; j++)
        target[j] = palette[i+k][j];
    }
    else
    {
      /** Else there is no entry, interpolate one **/

      fraction = ((double)(i - last_solid_index))/(miss_count+1);

      for (j=0; j<3; j++)
        palette[i][j] = source[j] + (int)(fraction*(target[j] - source[j]));
    }
  }
}



/******************************************************************************
  init_defaults ()

  Initialize all global variables, via the global <Option[]> and through
  explicit assignments.
******************************************************************************/

init_defaults ()
{
  int i, j;

  char str[MAX_LINE];

  /** Initialize global <Config> structure **/

  Config = (config_info *)(malloc(sizeof(config_info)));

  /** Initialize all of the values from global <Options[]> **/

  for (i=0; i<OPTION_COUNT; i++)
  {
    Config->enable_all[i] =			Options[i].enable;
    Config->value_int_all[i] =			Options[i].value_int;
    Config->value_double_all[i] =		Options[i].value_double;

    for (j=0; j<MAX_SIDES; j++)
    {
      Config->enable[i][j] =		Options[i].enable;
      Config->value_int[i][j] =		Options[i].value_int;
      Config->value_double[i][j] =	Options[i].value_double;
    }
  }

  /** Initialize directional arrays **/

  for (i=0; i<MAX_SIDES; i++)
  {
    Config->dir_type[i] =			MOVE_FORCE;
    for (j=0; j<4; j++)
      Config->dir[i][j] =			0;
  }

  /** Initialize other global variables **/

  Config->view_range_max =			0;
  Config->is_paused =				FALSE;

  Config->direction_count =			4;

  Config->text_size =				DEFAULT_TEXTSIZE;
  Config->text_offset =				DEFAULT_TEXT_X_OFFSET;

  Config->center_size =				DEFAULT_CENTERSIZE;
  Config->march_size =				DEFAULT_MARCHSIZE;

  /** Initialize hue and bw to inverse mappings **/

  Config->hue_count =				0;
  Config->bw_count =				0;

  for (i=0; i<MAX_HUES; i++)
    Config->hue_has_bw[i] = FALSE;

  for (i=0; Hues[i].hue_inverse != HUE_NONE; i++)
  {
    strcpy (Config->hue_name[i], Hues[i].name);
    for (j=0; j<3; j++)
      Config->palette[i][j] =			Hues[i].hue_triplet[j];
    Config->hue_to_inverse[i] =			Hues[i].hue_inverse;

    Config->hue_count++;

    if (Hues[i].bw_inverse != BW_NONE)
    {
      for (j=0; j<8; j++)
        Config->palette_gray[i][j] =		Hues[i].bw_octet[j];

      Config->bw_to_inverse[i] =		Hues[i].bw_inverse;
      Config->hue_has_bw[i] =			TRUE;

      Config->bw_count++;
    }
  }

  for (i=0; i<MAX_HILL_TONES; i++)
    Config->palette_hills[i][0] =		-1;
  for (i=0; i<MAX_FOREST_TONES; i++)
    Config->palette_forest[i][0] =		-1;
  for (i=0; i<MAX_SEA_TONES; i++)
    Config->palette_sea[i][0] =			-1;

  for (j=0; j<3; j++)
  {
    Config->palette_hills[0][j] =			Palette_Hills[0][j];
    Config->palette_hills[MAX_HILL_TONES-1][j] =	Palette_Hills[1][j];

    Config->palette_forest[0][j] =			Palette_Forest[0][j];
    Config->palette_forest[MAX_FOREST_TONES-1][j] =	Palette_Forest[1][j];

    Config->palette_sea[0][j] =				Palette_Sea[0][j];
    Config->palette_sea[MAX_SEA_TONES-1][j] =		Palette_Sea[1][j];
  }

  strcpy (Config->font, DEFAULT_FONT);
  sprintf (str, "seed=%d ", Config->value_int_all[OPTION_SEED]);
  strcat (Config->message_all, str);

  strcpy (Config->file_store_map, "xbattle.xbt");
}



/******************************************************************************
  int
  find_option (option)

  Search through the global <Options[]> for an option or negated option which
  matches <option> (eg "-no_grid" is negation of "-grid").  Return -1 if there
  is no match, <Options[]> index if there is a match, or <Options[]> index
  plus OPTION_COUNT if there is a negation match.
******************************************************************************/

find_option (option)
  char *option;
{
  int i;
  char string[MAX_TEXT];

  /** Search through all the normal options **/

  for (i=0; i<OPTION_COUNT; i++)
    if (!strcmp (option, Options[i].option))
      return (i);

  /** If <option> starts with "-no_" or "-no", remove and search again **/

  if (option[1] == 'n' && option[2] == 'o')
  {
    if (option[3] == '_')
      sprintf (string, "-%s", &option[4]);
    else
      sprintf (string, "-%s", &option[3]);

    for (i=0; i<OPTION_COUNT; i++)
      if (!strcmp (string, Options[i].option))
        return (i+OPTION_COUNT);
  }

  /** No option or negated option match, so return -1 **/

  return (-1);
}



/******************************************************************************
  int
  find_parameter_count (argc, argv, offset)

  Given a list of arguments in <argv[]>, and an <offset> into the list,
  return the number of parameters for that entry, where a parameter is defined
  as a consecutive entry which does not start with one of ('-','{','}').
******************************************************************************/

int
find_parameter_count (argc, argv, offset)
  int argc,
      offset;
  char *argv[];
{
  int count;

  for (count=0; (offset+count+1)<argc &&
			argv[offset+count+1][0] != '-' &&
			argv[offset+count+1][0] != '{' &&
			argv[offset+count+1][0] != '}'; count++);
  return (count);
}



/******************************************************************************
  int
  find_load_filename (option, filename)

  Determine if the option is actually a .xbo file, returning TRUE if so.
******************************************************************************/

int
find_load_filename (option, filename)
  char *option,
       *filename;
{
  int i;
  char *suffix,
       *strstr();
  FILE *fp;

  strcpy (filename, &option[1]);

  suffix = strstr (filename, "xbo");
  if (suffix == NULL)
    return (-1);

  if ((fp=fopen(filename, "r")) == NULL)
    return (-1);
  else
  {
    fclose (fp);
    return (OPTION_OPTIONS);
  }
}



/******************************************************************************
  find_color_match (option, color_name, use_second_color)

  Given an unknown argument <option>, presumably a color, search the custom
  color list and the X color list for a match, setting <color_name> to the
  name of the matched color.  If <use_second_color>, skip over any charcters
  before a "_" (ie, "red_black" becomes "black"), else just use characters
  before the "_" (ie, "red_black" becomes "red").
******************************************************************************/

find_color_match (option, color_name, use_second_color)
  char *option,
       *color_name;
  int use_second_color;
{
  int i, j,
      index;
  char *line;
  Display *display;
  int screen;
  Colormap cmap;
  XColor color;

  /** Either set pointer to first or second color **/

  if (use_second_color)
  {
    for (i=0; option[i] != '_' && option[i] != '\0'; i++);
    if (option[i] == '_')
      line = &(option[i+1]);
    else
      return (-1);
  }
  else
    line = &(option[1]);

  /** Check for match with custom colors **/

  for (j=0; j<Config->hue_count; j++)
  {
    if (matchstr (line, Config->hue_name[j]))
    {
      strcpy (color_name, Config->hue_name[j]);
      return (j);
    }
  }

  /** No custom color match, open up a dummy display for X color check **/

  display = XOpenDisplay ("");
  screen  = DefaultScreen (display);
  cmap = DefaultColormap (display, screen);

  /** Copy color name into dedicated string **/

  for (i=0; line[i] != '_' && line[i] != '\0'; i++)
    color_name[i] = line[i];
  color_name[i] = '\0';

  /** If color matches some X color, assign that X color to the custom	**/
  /** color palette and return the index.				**/

  if (XParseColor (display, cmap, color_name, &color))
  {
    return (load_color (color_name,
		color.red>>8, color.green>>8, color.blue>>8));
  }

  /** No custom or X color match, so return -1 **/

  return (-1);
}



/******************************************************************************
  load_color (hue_name, red, green, blue)

  Load custom color from option list into global <Config->hue_name>.
******************************************************************************/

load_color (hue_name, red, green, blue)
  char *hue_name;
  int red, green, blue;
{
  int i,
      hue_index;

  /** Search through existing hues, checking for match **/

  hue_index = Config->hue_count;
  for (i=0; i<Config->hue_count; i++)
  {
    if (strcmp (hue_name, Config->hue_name[i]) == 0)
      hue_index = i;
  }

  /** If there are too many hues, just overwrite the last one **/

  if (hue_index >= MAX_HUES)
    hue_index = MAX_HUES-1;
  else if (hue_index == Config->hue_count)
    Config->hue_count++;

  /** Set the hue **/

  strcpy (Config->hue_name[hue_index], hue_name);
  Config->palette[hue_index][0] = red;
  Config->palette[hue_index][1] = green;
  Config->palette[hue_index][2] = blue;

  /** Arbitrarily set inverse hue based on blue value **/

  if (blue > 128)
    Config->hue_to_inverse[hue_index] = 1;
  else
    Config->hue_to_inverse[hue_index] = 2;

  return (hue_index);
}



/******************************************************************************
  load_color_inverse (hue_name, inverse_hue_name)

  Establish line between color <hue_name> and color <inverse_hue_name>, if
  both colors can be found in palette.
******************************************************************************/

load_color_inverse (hue_name, inverse_hue_name)
  char *hue_name,
       *inverse_hue_name;
{
  int i,
      hue_index,
      inverse_hue_index;

  hue_index = -1;
  for (i=0; i<Config->hue_count; i++)
  {
    if (strcmp (hue_name, Config->hue_name[i]) == 0)
      hue_index = i;
  }

  inverse_hue_index = -1;
  for (i=0; i<Config->hue_count; i++)
  {
    if (strcmp (inverse_hue_name, Config->hue_name[i]) == 0)
      inverse_hue_index = i;
  }

  if (hue_index < 0 || inverse_hue_index < 0)
    throw_warning ("Unable to assign inverse colors", NULL);
  else
    Config->hue_to_inverse[hue_index] = inverse_hue_index;
}



/******************************************************************************
  load_stipple (hue_name, stipples)

******************************************************************************/
load_stipple (hue_name, stipples)
  char *hue_name;
  char *stipples[];
{
  int i,
      hue_index;

  /** Search through existing hues, checking for match **/

  hue_index = Config->hue_count;
  for (i=0; i<Config->hue_count; i++)
  {
    if (strcmp (hue_name, Config->hue_name[i]) == 0)
      hue_index = i;
  }

  /** If there are too many hues, just overwrite the last one **/

  if (hue_index >= MAX_HUES)
    hue_index = MAX_HUES-1;
  else if (hue_index == Config->hue_count)
    Config->hue_count++;

  /** Set the hue name **/

  strcpy (Config->hue_name[hue_index], hue_name);

  /** Let program know that hue has a stipple equivalent **/

  Config->hue_has_bw[hue_index] = TRUE;

  /** Set the stipple **/

  for (i=0; i<8; i++)
    Config->palette_gray[hue_index][i] = strtol (stipples[i], NULL, 0);

  return (hue_index);
}



/******************************************************************************
  print_usage_message ()

  Print the xbattle usage message (in global <Usage[]>) to stdout
******************************************************************************/

print_usage_message ()
{
  int i;

  printf ("%s\n", Usage[0]);
  for (i=1; Usage[i][0] != 'D'; i++)
    printf ("\t%s\n", Usage[i]);
}



/******************************************************************************
  int
  copy_first (dest, src)

  Copy the first white space delimited entry from <src[]> to <dest[]>.  Return
  the length of the entry.
******************************************************************************/

copy_first (dest, src)
  char *dest,
       *src;
{
  int i;

  if (src == NULL)
    return (0);

  for (i=0; !isspace(src[i]); i++)
    dest[i] = src[i];
  dest[i] = '\0';

  return (i);
}



/******************************************************************************
  char *
  strip_first (src)

  Return a pointer to the second space delimited entry of <src[]>.
******************************************************************************/

char *
strip_first (src)
  char *src;
{
  int i;

  for (i=0; !isspace(src[i]); i++);
  if (src[i] == '\0' || src[i] == '\n')
    return (NULL);

  for (; isspace(src[i]); i++);
  if (src[i] == '\0')
    return (NULL);
  return (&src[i]);
}



/******************************************************************************
  set_double_array (array, value, index)

  Fill <array[]> with <value> if <index> < 0, else just set <array[index]>
******************************************************************************/

set_double_array (array, value, index)
  double *array,
         value;
  int index;
{
  int i;

  if (index < 0)
  {
    for (i=0; i<MAX_SIDES; i++)
      array[i] = value;
  }
  else
    array[index] = value;
}



/******************************************************************************
  set_int_array (array, value, index)

  Fill <array[]> with <value> if <index> < 0, else just set <array[index]>
******************************************************************************/

set_int_array (array, value, index)
  int *array,
       value;
  int index;
{
  int i;

  if (index < 0)
  {
    for (i=0; i<MAX_SIDES; i++)
      array[i] = value;
  }
  else
    array[index] = value;
}



/******************************************************************************
  set_uchar_array (array, value, index)

  Fill <array[]> with <value> if <index> < 0, else just set <array[index]>
******************************************************************************/

set_uchar_array (array, value, index)
  unsigned char *array,
                value;
  int index;
{
  int i;

  if (index < 0)
  {
    for (i=0; i<MAX_SIDES; i++)
      array[i] = value;
  }
  else
    array[index] = value;
}