File: plargs.c

package info (click to toggle)
plplot 5.15.0%2Bdfsg-19
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 31,312 kB
  • sloc: ansic: 79,707; xml: 28,583; cpp: 20,033; ada: 19,456; tcl: 12,081; f90: 11,431; ml: 7,276; java: 6,863; python: 6,792; sh: 3,274; perl: 828; lisp: 75; makefile: 50; sed: 34; fortran: 5
file content (3013 lines) | stat: -rw-r--r-- 88,048 bytes parent folder | download | duplicates (6)
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
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
//  Maurice LeBrun			mjl@dino.ph.utexas.edu
//  Institute for Fusion Studies	University of Texas at Austin
//
//  Copyright (C) 1993-2005 Maurice LeBrun
//  Copyright (C) 1995 Rady Shouman
//  Copyright (C) 1998-2000 Geoffrey Furnish
//  Copyright (C) 2000-2019 Alan W. Irwin
//  Copyright (C) 2001 Joao Cardoso
//  Copyright (C) 2004-2011 Andrew Ross
//  Copyright (C) 2004-2005 Rafael Laboissiere
//  Copyright (C) 2007 Andrew Roach
//  Copyright (C) 2008-2009 Werner Smekal
//  Copyright (C) 2009-2011 Hazen Babcock
//  Copyright (C) 2009-2010 Hezekiah M. Carty
//  Copyright (C) 2015 Jim Dishaw
//  Copyright (C) 2017 Phil Rosenberg
//
//  This file is part of PLplot.
//
//  PLplot is free software; you can redistribute it and/or modify
//  it under the terms of the GNU Library General Public License as published
//  by the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  PLplot is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Library General Public License for more details.
//
//  You should have received a copy of the GNU Library General Public License
//  along with PLplot; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
//  Some parts of this code were derived from "xterm.c" and "ParseCmd.c" of
//  the X-windows Version 11 distribution.  The copyright notice is
//  reproduced here:
//
// Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts,
// and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
//
//                      All Rights Reserved
//
//  The full permission notice is given in the PLplot documentation.
//
//--------------------------------------------------------------------------
//
//! @file
//!  This file contains routines to extract & process command flags.  The
//!  command flags recognized by PLplot are stored in the "ploption_table"
//!  structure, along with strings giving the syntax, long help message, and
//!  option handler.
//!
//!  The command line parser -- plparseopts() -- removes all recognized flags
//!  (decreasing argc accordingly), so that invalid input may be readily
//!  detected.  It can also be used to process user command line flags.  The
//!  user can merge an option table of type PLOptionTable into the internal
//!  option table info structure using plMergeOpts().  Or, the user can
//!  specify that ONLY the external table(s) be parsed by calling
//!  plClearOpts() before plMergeOpts().
//!
//!  The default action taken by plparseopts() is as follows:
//!      - Returns with an error if an unrecognized option or badly formed
//!        option-value pair are encountered.
//!      - Returns immediately (return code 0) when the first non-option
//!        command line argument is found.
//!      - Returns with the return code of the option handler, if one
//!        was called.
//!      - Deletes command line arguments from argv list as they are found,
//!        and decrements argc accordingly.
//!      - Does not show "invisible" options in usage or help messages.
//!      - Assumes the program name is contained in argv[0].
//!
//!  These behaviors may be controlled through the "mode" argument, which can
//!  have the following bits set:
//!
//!  PL_PARSE_FULL -- Full parsing of command line and all error messages
//!  enabled, including program exit when an error occurs.  Anything on the
//!  command line that isn't recognized as a valid option or option argument
//!  is flagged as an error.
//!
//!  PL_PARSE_QUIET -- Turns off all output except in the case of
//!  errors.
//!
//!  PL_PARSE_NODELETE -- Turns off deletion of processed arguments.
//!
//!  PL_PARSE_SHOWALL -- Show invisible options
//!
//!  PL_PARSE_NOPROGRAM -- Specified if argv[0] is NOT a pointer to the
//!  program name.
//!
//!  PL_PARSE_NODASH -- Set if leading dash is NOT required.
//!
//!  PL_PARSE_SKIP -- Set to quietly skip over any unrecognized args.
//!
//!  Note: if you want to have both your option and a PLplot option of the
//!  same name processed (e.g. the -v option in plrender), do the following:
//!      1. Tag your option with PL_OPT_NODELETE
//!      2. Give it an option handler that uses a return code of 1.
//!      3. Merge your option table in.
//!  By merging your table, your option will be processed before the PLplot
//!  one.  The PL_OPT_NODELETE ensures that the option string is not deleted
//!  from the argv list, and the return code of 1 ensures that the parser
//!  continues looking for it.
//!
//!  See plrender.c for examples of actual usage.
//!

#include "plplotP.h"
#include <ctype.h>
#include <errno.h>

#ifdef HAVE_CRT_EXTERNS_H
//
// This include file has the declaration for _NSGetArgc().  See below.
//
#include <crt_externs.h>
#endif

// Support functions

static int  ParseOpt( int *, char ***, int *, char ***, PLOptionTable * );
static int  ProcessOpt( char *, PLOptionTable *, int *, char ***, int * );
static int  GetOptarg( char **, int *, char ***, int * );
static void Help( void );
static void Syntax( void );

// Option handlers

static int opt_a( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_auto_path( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_bg( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_bufmax( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_bufmax( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_cmap0( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_cmap1( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_db( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_debug( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_dev( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_dev_compression( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_dpi( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_drvopt( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_eofill( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_fam( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_fbeg( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_fflen( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_finc( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_freeaspect( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_fsiz( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_geo( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_h( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_hack( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_jx( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_jy( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_locale( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_mar( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_mfi( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_mfo( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_ncol0( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_ncol1( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_nopixmap( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_np( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_o( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_ori( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_plserver( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_plwindow( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_portrait( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_px( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_py( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_server_name( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_tk_file( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_v( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_verbose( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_width( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );
static int opt_wplt( PLCHAR_VECTOR, PLCHAR_VECTOR, void * );

// Global variables

static PLCHAR_VECTOR program = NULL;
static PLCHAR_VECTOR usage   = NULL;

static int           mode_full;
static int           mode_quiet;
static int           mode_nodelete;
static int           mode_showall;
static int           mode_noprogram;
static int           mode_nodash;
static int           mode_skip;

// Temporary buffer used for parsing

#define OPTMAX    1024
static char opttmp[OPTMAX];

//--------------------------------------------------------------------------
//!
//! PLPLOT options data structure definition.
//!
//! The table is defined as follows
//!
//! typedef struct {
//!     PLCHAR_VECTOR opt;
//!     int  (*handler)	(PLCHAR_VECTOR, PLCHAR_VECTOR, void *);
//!     void *client_data;
//!     void *var;
//!     long mode;
//!     PLCHAR_VECTOR syntax;
//!     PLCHAR_VECTOR desc;
//! } PLOptionTable;
//!
//! where each entry has the following meaning:
//!
//! opt		option string
//! handler	pointer to function for processing the option and
//!		 (optionally) its argument
//! client_data	pointer to data that gets passed to (*handler)
//! var		address of variable to set based on "mode"
//! mode		governs handling of option (see below)
//! syntax	short syntax description
//! desc		long syntax description
//!
//! The syntax and or desc strings can be NULL if the option is never to be
//! described.  Usually this is only used for obsolete arguments; those we
//! just wish to hide from normal use are better made invisible (which are
//! made visible by either specifying -showall first or PL_PARSE_SHOWALL).
//!
//! The mode bits are:
//!
//! PL_OPT_ARG		Option has an argument
//! PL_OPT_NODELETE	Don't delete after processing
//! PL_OPT_INVISIBLE	Make invisible (usually for debugging)
//! PL_OPT_DISABLED	Ignore this option
//!
//! The following mode bits cause the option to be processed as specified:
//!
//! PL_OPT_FUNC		Call function handler (opt, opt_arg)
//! PL_OPT_BOOL		Set *var=1
//! PL_OPT_INT		Set *var=atoi(opt_arg)
//! PL_OPT_FLOAT		Set *var=atof(opt_arg)
//! PL_OPT_STRING	Set *var=opt_arg
//!
//! where opt points to the option string and opt_arg points to the
//! argument string.
//!
//--------------------------------------------------------------------------

static PLOptionTable ploption_table[] = {
    {
        "showall",              // Turns on invisible options
        NULL,
        NULL,
        &mode_showall,
        PL_OPT_BOOL | PL_OPT_INVISIBLE,
        "-showall",
        "Turns on invisible options"
    },
    {
        "h",                    // Help
        opt_h,
        NULL,
        NULL,
        PL_OPT_FUNC,
        "-h",
        "Print out this message"
    },
    {
        "v",                    // Version
        opt_v,
        NULL,
        NULL,
        PL_OPT_FUNC,
        "-v",
        "Print out the PLplot library version number"
    },
    {
        "verbose",              // Be more verbose than usual
        opt_verbose,
        NULL,
        NULL,
        PL_OPT_FUNC,
        "-verbose",
        "Be more verbose than usual"
    },
    {
        "debug",                // Print debugging info
        opt_debug,
        NULL,
        NULL,
        PL_OPT_FUNC,
        "-debug",
        "Print debugging info (implies -verbose)"
    },
    {
        "hack",                 // Enable driver-specific hack(s)
        opt_hack,
        NULL,
        NULL,
        PL_OPT_FUNC | PL_OPT_INVISIBLE,
        "-hack",
        "Enable driver-specific hack(s)"
    },
    {
        "dev",                  // Output device
        opt_dev,
        NULL,
        NULL,
        PL_OPT_FUNC | PL_OPT_ARG,
        "-dev name",
        "Output device name"
    },
    {
        "o",                    // Output filename
        opt_o,
        NULL,
        NULL,
        PL_OPT_FUNC | PL_OPT_ARG,
        "-o name",
        "Output filename"
    },
    {
        "display",              // X server
        opt_o,
        NULL,
        NULL,
        PL_OPT_FUNC | PL_OPT_ARG,
        "-display name",
        "X server to contact"
    },
    {
        "px",                   // Plots per page in x
        opt_px,
        NULL,
        NULL,
        PL_OPT_FUNC | PL_OPT_ARG,
        "-px number",
        "Plots per page in x"
    },
    {
        "py",                   // Plots per page in y
        opt_py,
        NULL,
        NULL,
        PL_OPT_FUNC | PL_OPT_ARG,
        "-py number",
        "Plots per page in y"
    },
    {
        "geometry",             // Geometry
        opt_geo,
        NULL,
        NULL,
        PL_OPT_FUNC | PL_OPT_ARG,
        "-geometry geom",
        "Window size/position specified as in X, e.g., 400x300, 400x300-100+200, +100-200, etc."
    },
    {
        "geo",                  // Geometry (alias)
        opt_geo,
        NULL,
        NULL,
        PL_OPT_FUNC | PL_OPT_ARG | PL_OPT_INVISIBLE,
        "-geo geom",
        "Window size/position specified as in X, e.g., 400x300, 400x300-100+200, +100-200, etc."
    },
    {
        "wplt",                 // Plot window
        opt_wplt,
        NULL,
        NULL,
        PL_OPT_FUNC | PL_OPT_ARG,
        "-wplt xl,yl,xr,yr",
        "Relative coordinates [0-1] of window into plot"
    },
    {
        "mar",                  // Margin
        opt_mar,
        NULL,
        NULL,
        PL_OPT_FUNC | PL_OPT_ARG,
        "-mar margin",
        "Margin space in relative coordinates (0 to 0.5, def 0)"
    },
    {
        "a",                    // Aspect ratio
        opt_a,
        NULL,
        NULL,
        PL_OPT_FUNC | PL_OPT_ARG,
        "-a aspect",
        "Page aspect ratio (def: same as output device)"
    },
    {
        "jx",                   // Justification in x
        opt_jx,
        NULL,
        NULL,
        PL_OPT_FUNC | PL_OPT_ARG,
        "-jx justx",
        "Page justification in x (-0.5 to 0.5, def 0)"
    },
    {
        "jy",                   // Justification in y
        opt_jy,
        NULL,
        NULL,
        PL_OPT_FUNC | PL_OPT_ARG,
        "-jy justy",
        "Page justification in y (-0.5 to 0.5, def 0)"
    },
    {
        "ori",                  // Orientation
        opt_ori,
        NULL,
        NULL,
        PL_OPT_FUNC | PL_OPT_ARG,
        "-ori orient",
        "Plot orientation (0,1,2,3=landscape,portrait,seascape,upside-down)"
    },
    {
        "freeaspect",           // floating aspect ratio
        opt_freeaspect,
        NULL,
        NULL,
        PL_OPT_FUNC,
        "-freeaspect",
        "Allow aspect ratio to adjust to orientation swaps"
    },
    {
        "portrait",             // floating aspect ratio
        opt_portrait,
        NULL,
        NULL,
        PL_OPT_FUNC,
        "-portrait",
        "Sets portrait mode (both orientation and aspect ratio)"
    },
    {
        "width",                // Pen width
        opt_width,
        NULL,
        NULL,
        PL_OPT_FUNC | PL_OPT_ARG,
        "-width width",
        "Sets pen width (0 <= width)"
    },
    {
        "bg",                   // Background color
        opt_bg,
        NULL,
        NULL,
        PL_OPT_FUNC | PL_OPT_ARG,
        "-bg color",
        "Background color (e.g., FF0000=opaque red, 0000FF_0.1=blue with alpha of 0.1)"
    },
    {
        "ncol0",                // Allocated colors in cmap 0
        opt_ncol0,
        NULL,
        NULL,
        PL_OPT_FUNC | PL_OPT_ARG,
        "-ncol0 n",
        "Number of colors to allocate in cmap 0 (upper bound)"
    },
    {
        "ncol1",                // Allocated colors in cmap 1
        opt_ncol1,
        NULL,
        NULL,
        PL_OPT_FUNC | PL_OPT_ARG,
        "-ncol1 n",
        "Number of colors to allocate in cmap 1 (upper bound)"
    },
    {
        "fam",                  // Familying on switch
        opt_fam,
        NULL,
        NULL,
        PL_OPT_FUNC,
        "-fam",
        "Create a family of output files"
    },
    {
        "fsiz",                 // Family file size
        opt_fsiz,
        NULL,
        NULL,
        PL_OPT_FUNC | PL_OPT_ARG,
        "-fsiz size[kKmMgG]",
        "Output family file size (e.g. -fsiz 0.5G, def MB)"
    },
    {
        "fbeg",                 // Family starting member
        opt_fbeg,
        NULL,
        NULL,
        PL_OPT_FUNC | PL_OPT_ARG,
        "-fbeg number",
        "First family member number on output"
    },
    {
        "finc",                 // Family member increment
        opt_finc,
        NULL,
        NULL,
        PL_OPT_FUNC | PL_OPT_ARG,
        "-finc number",
        "Increment between family members"
    },
    {
        "fflen",                // Family member min field width
        opt_fflen,
        NULL,
        NULL,
        PL_OPT_FUNC | PL_OPT_ARG,
        "-fflen length",
        "Family member number minimum field width"
    },
    {
        "nopixmap",             // Do not use pixmaps
        opt_nopixmap,
        NULL,
        NULL,
        PL_OPT_FUNC,
        "-nopixmap",
        "Don't use pixmaps in X-based drivers"
    },
    {
        "db",                   // Double buffering on switch
        opt_db,
        NULL,
        NULL,
        PL_OPT_FUNC,
        "-db",
        "Double buffer X window output"
    },
    {
        "np",                   // Page pause off switch
        opt_np,
        NULL,
        NULL,
        PL_OPT_FUNC,
        "-np",
        "No pause between pages"
    },
    {
        "bufmax",               // # bytes sent before flushing output
        opt_bufmax,
        NULL,
        NULL,
        PL_OPT_FUNC | PL_OPT_ARG | PL_OPT_INVISIBLE,
        "-bufmax",
        "bytes sent before flushing output"
    },
    {
        "server_name",          // Main window name of server
        opt_server_name,
        NULL,
        NULL,
        PL_OPT_FUNC | PL_OPT_ARG,
        "-server_name name",
        "Main window name of PLplot server (tk driver)"
    },
    {
        "plserver",             // PLplot server name
        opt_plserver,
        NULL,
        NULL,
        PL_OPT_FUNC | PL_OPT_ARG | PL_OPT_INVISIBLE,
        "-plserver name",
        "Invoked name of PLplot server (tk driver)"
    },
    {
        "plwindow",             // PLplot container window name
        opt_plwindow,
        NULL,
        NULL,
        PL_OPT_FUNC | PL_OPT_ARG | PL_OPT_INVISIBLE,
        "-plwindow name",
        "Name of PLplot container window (tk driver)"
    },
    {
        "auto_path",            // Additional directory(s) to autoload
        opt_auto_path,
        NULL,
        NULL,
        PL_OPT_FUNC | PL_OPT_ARG | PL_OPT_INVISIBLE,
        "-auto_path dir",
        "Additional directory(s) to autoload (tk driver)"
    },
    {
        "tk_file",  // -file option for plserver
        opt_tk_file,
        NULL,
        NULL,
        PL_OPT_FUNC | PL_OPT_ARG | PL_OPT_INVISIBLE,
        "-tk_file file",
        "file for plserver (tk driver)"
    },
    {
        "dpi",                  // Dots per inch
        opt_dpi,
        NULL,
        NULL,
        PL_OPT_FUNC | PL_OPT_ARG,
        "-dpi dpi",
        "Resolution, in dots per inch (e.g. -dpi 360x360)"
    },
    {
        "compression",                  // compression
        opt_dev_compression,
        NULL,
        NULL,
        PL_OPT_FUNC | PL_OPT_ARG,
        "-compression num",
        "Sets compression level in supporting devices"
    },
    {
        "cmap0",
        opt_cmap0,
        NULL,
        NULL,
        PL_OPT_ARG | PL_OPT_FUNC,
        "-cmap0 file name",
        "Initializes color table 0 from a cmap0.pal format file in one of standard PLplot paths."
    },
    {
        "cmap1",
        opt_cmap1,
        NULL,
        NULL,
        PL_OPT_ARG | PL_OPT_FUNC,
        "-cmap1 file name",
        "Initializes color table 1 from a cmap1.pal format file in one of standard PLplot paths."
    },
    {
        "locale",
        opt_locale,
        NULL,
        NULL,
        PL_OPT_FUNC,
        "-locale",
        "Use locale environment (e.g., LC_ALL, LC_NUMERIC, or LANG) to set LC_NUMERIC locale (which affects decimal point separator)."
    },
    {
        "eofill",
        opt_eofill,
        NULL,
        NULL,
        PL_OPT_FUNC,
        "-eofill",
        "For the case where the boundary of the filled region is self-intersecting, use the even-odd fill rule rather than the default nonzero fill rule."
    },
    {
        "drvopt",               // Driver specific options
        opt_drvopt,
        NULL,
        NULL,
        PL_OPT_ARG | PL_OPT_FUNC,
        "-drvopt option[=value][,option[=value]]*",
        "Driver specific options"
    },
    {
        "mfo",                  // Metafile output option
        opt_mfo,
        NULL,
        NULL,
        PL_OPT_ARG | PL_OPT_FUNC,
        "-mfo PLplot metafile name",
        "Write the plot to the specified PLplot metafile"
    },
    {
        "mfi",                  // Metafile output option
        opt_mfi,
        NULL,
        NULL,
        PL_OPT_ARG | PL_OPT_FUNC,
        "-mfi PLplot metafile name",
        "Read the specified PLplot metafile"
    },
    {
        NULL,                   // option
        NULL,                   // handler
        NULL,                   // client data
        NULL,                   // address of variable to set
        0,                      // mode flag
        NULL,                   // short syntax
        NULL
    }                           // long syntax
};

static PLCHAR_VECTOR plplot_notes[] = {
    "All parameters must be white-space delimited.  Some options are driver",
    "dependent.  Please see the PLplot reference document for more detail.",
    NULL
};

//--------------------------------------------------------------------------
//! @struct PLOptionInfo
//!
//! Array of option tables and associated info.
//!
//! The user may merge up to PL_MAX_OPT_TABLES custom option tables (of type
//! PLOptionTable) with the internal one.  The resulting treatment is simple,
//! powerful, and robust.  The tables are parsed in the order of last added
//! first, to the internal table last.  If multiple options of the same name
//! occur, only the first parsed is "seen", thus, the user can easily
//! override any PLplot internal option merely by providing the same option.
//! This same precedence is followed when printing help and usage messages,
//! with each set of options given separately.  See example usage in
//! plrender.c.
//--------------------------------------------------------------------------

typedef struct
{
    PLOptionTable *options;
    PLCHAR_VECTOR name;
    const char    **notes;
} PLOptionInfo;

PLOptionInfo ploption_info_default = {
    ploption_table,
    "PLplot options",
    plplot_notes
};

#define PL_MAX_OPT_TABLES    10
PLOptionInfo ploption_info[PL_MAX_OPT_TABLES] = {
    {
        ploption_table,
        "PLplot options",
        plplot_notes
    }
};

// The structure that hold the driver specific command line options

typedef struct DrvOptCmd
{
    char *option;
    char *value;
    struct DrvOptCmd *next;
} DrvOptCmd;

// the variable where opt_drvopt() stores the driver specific command line options
static DrvOptCmd drv_opt = { NULL, NULL, NULL };

static int       tables = 1;

PLINT
c_plsetopt( PLCHAR_VECTOR opt, PLCHAR_VECTOR opt_arg )
{
    int  mode = 0, argc = 2, status;
    char *argv[3];

    argv[0] = (char *) opt;
    argv[1] = (char *) opt_arg;
    argv[2] = NULL;
    mode    =
        PL_PARSE_QUIET |
        PL_PARSE_NODELETE |
        PL_PARSE_NOPROGRAM |
        PL_PARSE_NODASH;

    status = plparseopts( &argc, argv, mode );
    if ( status )
    {
        fprintf( stderr, "plsetopt: Unrecognized option %s\n", opt );
    }
    return status;
}

//--------------------------------------------------------------------------
// plMergeOpts()
//
//! Merge user option table info structure with internal one.
//!
//! @param options ?
//! @param name ?
//! @param notes ?
//!
//--------------------------------------------------------------------------

PLINT
plMergeOpts( PLOptionTable *options, PLCHAR_VECTOR name, PLCHAR_VECTOR *notes )
{
    PLOptionTable *tab;

    pllib_init();

// Check to make sure option table has been terminated correctly

    for ( tab = (PLOptionTable *) options; tab->opt; tab++ )
        ;

// We've reached the last table entry.  All the subentries must be NULL or 0

    if ( ( tab->handler != NULL ) ||
         ( tab->client_data != NULL ) ||
         ( tab->var != NULL ) ||
         ( tab->mode != 0 ) ||
         ( tab->syntax != NULL ) ||
         ( tab->desc != NULL ) )
    {
        plabort( "plMergeOpts: input table improperly terminated" );
        return 1;
    }

// No room for more tables

    if ( tables++ >= PL_MAX_OPT_TABLES )
    {
        plabort( "plMergeOpts: max tables limit exceeded, table not merged" );
        return 1;
    }

    ploption_info[tables - 1].options = options;
    ploption_info[tables - 1].name    = name;
    ploption_info[tables - 1].notes   = notes;

    return 0;
}

//--------------------------------------------------------------------------
// plClearOpts()
//
//! Clear internal option table info structure.
//!
//--------------------------------------------------------------------------

void
plClearOpts( void )
{
    tables = 0;
}

//--------------------------------------------------------------------------
// plResetOpts()
//
//! Reset internal option table info structure.
//!
//--------------------------------------------------------------------------

void
plResetOpts( void )
{
    ploption_info[0] = ploption_info_default;
    tables           = 1;
}

//--------------------------------------------------------------------------
// plparseopts()
//
//! Process options list using current ploptions_info structure.
//! An error in parsing the argument list causes a program exit if
//! mode_full is set, otherwise the function returns with an error.
//!
//! @param p_argc pointer to a value that ONLY keeps track of number of arguments after processing.
//! @param argv ?
//! @param mode ?
//!
//! @returns 0 if successful.
//!
//--------------------------------------------------------------------------

PLINT
c_plparseopts( int *p_argc, char **argv, PLINT mode )
{
    char **argsave, **argend;
    int  i, myargc, myargcsave, status = 0;


// Initialize

    mode_full      = mode & PL_PARSE_FULL;
    mode_quiet     = mode & PL_PARSE_QUIET;
    mode_nodelete  = mode & PL_PARSE_NODELETE;
    mode_showall   = mode & PL_PARSE_SHOWALL;
    mode_noprogram = mode & PL_PARSE_NOPROGRAM;
    mode_nodash    = mode & PL_PARSE_NODASH;
    mode_skip      = mode & PL_PARSE_SKIP;

    myargc = ( *p_argc );
    argend = argv + myargc;

// If program name is first argument, save and advance

    if ( !mode_noprogram )
    {
        // Just in case plparseopts has been called previously (e.g., with PL_PARSE_NODELETE).
        if ( plsc->program )
            free_mem( plsc->program );

        // If plparseopts is not called again, this is freed in plend1.
        plsc->program = plstrdup( argv[0] );
        program       = (PLCHAR_VECTOR) plsc->program;
        --myargc; ++argv;
    }
    if ( myargc == 0 )
        return 0;

    // Process the command line

    // Special hack to deal with -debug option before
    // pllib_init() is called.
    argsave    = argv;
    myargcsave = myargc;
    for (; myargc > 0; --myargc, ++argv )
    {
        // Allow for "holes" in argv list
        if ( *argv == NULL || *argv[0] == '\0' )
            continue;

        if ( ( !mode_nodash && !strcmp( *argv, "-debug" ) ) || ( mode_nodash && !strcmp( *argv, "debug" ) ) )
        {
            //fprintf(stderr, "Found debug option in argv\n");
            // Loop over all options tables, starting with the last
            for ( i = tables - 1; i >= 0; i-- )
            {
                // Check option table for option

                status = ParseOpt( &myargc, &argv, p_argc, &argsave,
                    ploption_info[i].options );

                if ( !status )
                    break;
            }
            break;
        }
    }
    // Restore pointers to condition before the above loop
    // Although array length and content stored in those pointers
    // is likely changed.
    myargc = myargcsave;
    argv   = argsave;

    pllib_init();

    argsave = argv;
    for (; myargc > 0; --myargc, ++argv )
    {
        // Allow for "holes" in argv list
        if ( *argv == NULL || *argv[0] == '\0' )
            continue;

        // Loop over all options tables, starting with the last
        for ( i = tables - 1; i >= 0; i-- )
        {
            // Check option table for option

            status = ParseOpt( &myargc, &argv, p_argc, &argsave,
                ploption_info[i].options );

            if ( !status )
                break;
        }

        // Handle error return as specified by the mode flag

        if ( status == -1 )
        {
            // No match.  Keep going if mode_skip is set, otherwise abort if
            // fully parsing, else return without error.

            status = 0;

            if ( mode_skip )
            {
                if ( !mode_nodelete )
                    *argsave++ = *argv;
                continue;
            }
            if ( !mode_quiet && mode_full )
            {
                fprintf( stderr, "\nBad command line option \"%s\"\n", argv[0] );
                plOptUsage();
            }
            if ( mode_full )
                exit( 1 );

            break;
        }
        else if ( status == 1 )
        {
            // Illegal or badly formed

            if ( !mode_quiet )
            {
                fprintf( stderr, "\nBad command line option \"%s\"\n", argv[0] );
                plOptUsage();
            }
            if ( mode_full )
                exit( 1 );

            break;
        }
        else if ( status == 2 )
        {
            // Informational option encountered (-h or -v)

            exit( 0 );
        }
    }

// Compress and NULL-terminate argv

    if ( !mode_nodelete )
    {
        for ( i = 0; i < myargc; i++ )
            *argsave++ = *argv++;

        if ( argsave < argend )
        {
            *argsave = NULL;
#ifdef HAVE_NSGETARGC
            //
            // Modify the global argc variable to match the shortened argv.
            // The global argc and argv must be kept consistent so that future
            // users of them (e.g. libraries loaded later with a device driver)
            // will not try to dereference the null pointer at the end of the
            // shortened argv array.
            //
            *_NSGetArgc() = *p_argc;
#endif
        }
    }

    return status;
}

//--------------------------------------------------------------------------
// ParseOpt()
//
//! Parses & determines appropriate action for input flag.
//!
//! @param p_myargc ?
//! @param p_argv ?
//! @param p_argc pointer to a value that ONLY keeps track of number of arguments after processing.
//! @param p_argsave ?
//! @param option_table ?
//!
//! @returns ?
//!
//--------------------------------------------------------------------------

static int
ParseOpt( int *p_myargc, char ***p_argv, int *p_argc, char ***p_argsave,
          PLOptionTable *option_table )
{
    PLOptionTable *tab;
    char          *opt;

// Only handle actual flags and their arguments

    if ( mode_nodash || ( *p_argv )[0][0] == '-' )
    {
        opt = ( *p_argv )[0];
        if ( *opt == '-' )
            opt++;

        for ( tab = option_table; tab->opt; tab++ )
        {
            // Skip if option not enabled

            if ( tab->mode & PL_OPT_DISABLED )
                continue;

            // Try to match it

            if ( *opt == *tab->opt && !strcmp( opt, tab->opt ) )
            {
                // Option matched, so remove from argv list if applicable.

                if ( !mode_nodelete )
                {
                    if ( tab->mode & PL_OPT_NODELETE )
                        ( *( *p_argsave )++ ) = ( **p_argv );
                    else
                        --( *p_argc );
                }

                // Process option (and argument if applicable)

                return ( ProcessOpt( opt, tab, p_myargc, p_argv, p_argc ) );
            }
        }
    }

    return -1;
}

//--------------------------------------------------------------------------
// ProcessOpt()
//
//! Process option (and argument if applicable).
//!
//! @param opt ?
//! @param tab ?
//! @param p_myargc ?
//! @param p_argv ?
//! @param p_argc pointer to a value that ONLY keeps track of number of arguments after processing.
//!
//! @returns 0 if successful.
//--------------------------------------------------------------------------

static int
ProcessOpt( char * opt, PLOptionTable *tab, int *p_myargc, char ***p_argv,
            int *p_argc )
{
    int  need_arg, res;
    char *opt_arg = NULL;

// Get option argument if necessary

    need_arg = PL_OPT_ARG | PL_OPT_INT | PL_OPT_FLOAT | PL_OPT_STRING;

    if ( tab->mode & need_arg )
    {
        if ( GetOptarg( &opt_arg, p_myargc, p_argv, p_argc ) )
            return 1;
    }

// Process argument

    switch ( tab->mode & 0xFF00 )
    {
    case PL_OPT_FUNC:

        // Call function handler to do the job

        if ( tab->handler == NULL )
        {
            fprintf( stderr,
                "ProcessOpt: no handler specified for option %s\n",
                tab->opt );
            return 1;
        }

        if ( mode_nodelete && opt_arg )
        {
            // Make a copy, since handler may mung opt_arg with strtok()
            char *copy =
                (char *) malloc( (size_t) ( 1 + strlen( opt_arg ) ) * sizeof ( char ) );
            if ( copy == NULL )
            {
                plabort( "ProcessOpt: out of memory" );
                return 1;
            }
            strcpy( copy, opt_arg );
            res = ( ( *tab->handler )( opt, copy, tab->client_data ) );
            free( (void *) copy );
            return res;
        }
        else
        {
            return ( ( *tab->handler )( opt, opt_arg, tab->client_data ) );
        }

    case PL_OPT_BOOL:

        // Set *var as a boolean

        if ( tab->var == NULL )
        {
            fprintf( stderr,
                "ProcessOpt: no variable specified for option %s\n",
                tab->opt );
            return 1;
        }
        *(int *) tab->var = 1;
        break;

    case PL_OPT_INT:

        // Set *var as an int

        if ( tab->var == NULL )
        {
            fprintf( stderr,
                "ProcessOpt: no variable specified for option %s\n",
                tab->opt );
            return 1;
        }
        *(int *) tab->var = atoi( opt_arg );
        break;

    case PL_OPT_FLOAT:

        // Set *var as a float

        if ( tab->var == NULL )
        {
            fprintf( stderr,
                "ProcessOpt: no variable specified for option %s\n",
                tab->opt );
            return 1;
        }
        *(PLFLT *) tab->var = atof( opt_arg );
        break;

    case PL_OPT_STRING:

        // Set var (can be NULL initially) to point to opt_arg string

        *(char **) tab->var = opt_arg;
        break;

    default:

        // Somebody messed up..

        fprintf( stderr,
            "ProcessOpt: invalid processing mode for option %s\n",
            tab->opt );
        return 1;
    }
    return 0;
}

//--------------------------------------------------------------------------
// GetOptarg()
//
//! Retrieves an option argument.
//! If an error occurs here it is a true syntax error.
//!
//! @param popt_arg ?
//! @param p_myargc ?
//! @param p_argv ?
//! @param p_argc pointer to a value that ONLY keeps track of number of arguments after processing.
//!
//! @returns 0 if successful.
//!
//--------------------------------------------------------------------------

static int
GetOptarg( char **popt_arg, int *p_myargc, char ***p_argv, int *p_argc )
{
    int result = 0;

    --( *p_myargc );

    if ( ( *p_myargc ) <= 0 )           // oops, no more arguments
        result = 1;

    if ( !result )
    {
        ( *p_argv )++;
        // Skip -bg argument checking since, for example, "-ffffff" is
        // valid but would be considered invalid by the crude test at
        // the end of the if.  Instead, -bg always consumes the next
        // argument (which exists according to the test above) in any
        // form, and that argument is checked for validity by the
        //  opt_bg routine.
        if ( strstr( ( ( *p_argv ) - 1 )[0], "-bg" ) != ( ( *p_argv ) - 1 )[0] && ( *p_argv )[0][0] == '-' && isalpha( ( *p_argv )[0][1] ) )
        {
            ( *p_argv )--;                // oops, next arg is a flag
            result = 1;
        }
    }

    if ( !result )                      // yeah, the user got it right
    {
        if ( !mode_nodelete )
            ( *p_argc )--;
        *popt_arg = ( *p_argv )[0];
    }
    else
    {
        if ( !mode_quiet )
        {
            fprintf( stderr, "Argument missing for %s option.\n", ( *p_argv )[0] );
            plOptUsage();
        }
    }
    return result;
}

//--------------------------------------------------------------------------
// plSetUsage()
//
//! Set the strings used in usage and syntax messages.
//!
//! @param program_string The program name.
//! @param usage_string String describing how to use the program.
//!
//--------------------------------------------------------------------------

void
plSetUsage( PLCHAR_VECTOR program_string, PLCHAR_VECTOR usage_string )
{
    if ( program_string != NULL )
        program = program_string;

    if ( usage_string != NULL )
        usage = usage_string;
}

//--------------------------------------------------------------------------
// plOptUsage()
//
//! Print usage & syntax message.
//!
//--------------------------------------------------------------------------

void
plOptUsage( void )
{
    if ( usage == NULL )
        fprintf( stderr, "\nUsage:\n        %s [options]\n", program );
    else
        fputs( usage, stderr );

    Syntax();

    fprintf( stderr, "\n\nType %s -h for a full description.\n\n",
        program );
}

//--------------------------------------------------------------------------
// Syntax()
//
//! Print short syntax message.
//!
//--------------------------------------------------------------------------

static void
Syntax( void )
{
    PLOptionTable *tab;
    int           i, col, len;

// Loop over all options tables

    for ( i = tables - 1; i >= 0; i-- )
    {
        // Introducer

        if ( ploption_info[i].name )
            fprintf( stderr, "\n%s:", ploption_info[i].name );
        else
            fputs( "\nUser options:", stderr );

        // Print syntax for each option

        col = 80;
        for ( tab = ploption_info[i].options; tab->opt; tab++ )
        {
            if ( tab->mode & PL_OPT_DISABLED )
                continue;

            if ( !mode_showall && ( tab->mode & PL_OPT_INVISIBLE ) )
                continue;

            if ( tab->syntax == NULL )
                continue;

            len = 3 + (int) strlen( tab->syntax );              // space [ string ]
            if ( col + len > 79 )
            {
                fprintf( stderr, "\n   " );               // 3 spaces
                col = 3;
            }
            fprintf( stderr, " [%s]", tab->syntax );
            col += len;
        }
        fprintf( stderr, "\n" );
    }
}

//--------------------------------------------------------------------------
// Help()
//
//! Print long help message.
//!
//--------------------------------------------------------------------------

static void
Help( void )
{
    PLOptionTable *tab;
    const char    **note;
    int           i;
    FILE          *outfile = stderr;

#ifdef HAVE_POPEN
    FILE *pager = NULL;
    if ( getenv( "PAGER" ) != NULL )
        pager = (FILE *) popen( "$PAGER", "w" );
    if ( pager == NULL )
        pager = (FILE *) popen( "more", "w" );
    if ( pager != NULL )
        outfile = pager;
#endif

// Usage line

    if ( usage == NULL )
        fprintf( outfile, "\nUsage:\n        %s [options]\n", program );
    else
        fputs( usage, outfile );

// Loop over all options tables

    for ( i = tables - 1; i >= 0; i-- )
    {
        // Introducer

        if ( ploption_info[i].name )
            fprintf( outfile, "\n%s:\n", ploption_info[i].name );
        else
            fputs( "\nUser options:\n", outfile );

        // Print description for each option

        for ( tab = ploption_info[i].options; tab->opt; tab++ )
        {
            if ( tab->mode & PL_OPT_DISABLED )
                continue;

            if ( !mode_showall && ( tab->mode & PL_OPT_INVISIBLE ) )
                continue;

            if ( tab->desc == NULL )
                continue;

            if ( tab->mode & PL_OPT_INVISIBLE )
                fprintf( outfile, " *  %-20s %s\n", tab->syntax, tab->desc );
            else
                fprintf( outfile, "    %-20s %s\n", tab->syntax, tab->desc );
        }

        // Usage notes

        if ( ploption_info[i].notes )
        {
            putc( '\n', outfile );
            for ( note = ploption_info[i].notes; *note; note++ )
            {
                fputs( *note, outfile );
                putc( '\n', outfile );
            }
        }
    }

#ifdef HAVE_POPEN
    if ( pager != NULL )
        pclose( pager );
#endif
}

//--------------------------------------------------------------------------
// plParseDrvOpts
//
//! Parse driver specific options
//!
//! @param acc_opt ?
//!
//! @returns 0 if successful.
//!
//--------------------------------------------------------------------------

int
plParseDrvOpts( DrvOpt *acc_opt )
{
    DrvOptCmd *drvp;
    DrvOpt    *t;
    int       fl;
    char      msg[80];
    memset( msg, '\0', sizeof ( msg ) );

    if ( !drv_opt.option )
        return 1;

    drvp = &drv_opt;
    do
    {
        t = acc_opt; fl = 0;
        while ( t->opt )
        {
            if ( strcmp( drvp->option, t->opt ) == 0 )
            {
                fl = 1;
                switch ( t->type )
                {
                case DRV_STR:
                    *(char **) ( t->var_ptr ) = ( drvp->value );
#ifdef DEBUG
                    fprintf( stderr, "plParseDrvOpts: %s %s\n", t->opt, *(char **) t->var_ptr );
#endif
                    break;

                case DRV_INT:
                    if ( sscanf( drvp->value, "%d", (int *) t->var_ptr ) != 1 )
                    {
                        snprintf( msg, sizeof ( msg ) - 1, "Incorrect argument to '%s' option", drvp->option );
                        plexit( msg );
                    }
#ifdef DEBUG
                    fprintf( stderr, "plParseDrvOpts: %s %d\n", t->opt, *(int *) t->var_ptr );
#endif
                    break;

                case DRV_FLT:
                    if ( sscanf( drvp->value, "%f", (float *) t->var_ptr ) != 1 )
                    {
                        snprintf( msg, sizeof ( msg ) - 1, "Incorrect argument to '%s' option", drvp->option );
                        plexit( msg );
                    }
#ifdef DEBUG
                    fprintf( stderr, "plParseDrvOpts: %s %f\n", t->opt, *(float *) t->var_ptr );
#endif
                    break;
                }
            }
            t++;
        }

        if ( !fl )
        {
            snprintf( msg, sizeof ( msg ) - 1, "Option '%s' not recognized.\n\nRecognized options for this driver are:\n", drvp->option );
            plwarn( msg );
            plHelpDrvOpts( acc_opt );
            plexit( "" );
        }
    }
    while ( ( drvp = drvp->next ) )
    ;

    return 0;
}

//--------------------------------------------------------------------------
// plHelpDrvOpts
//
//! Give driver specific help
//!
//! @param acc_opt ?
//!
//--------------------------------------------------------------------------

void
plHelpDrvOpts( DrvOpt *acc_opt )
{
    DrvOpt *t;

    t = acc_opt;
    while ( t->opt )
    {
        fprintf( stderr, "%s:\t%s\n", t->opt, t->hlp_msg );
        t++;
    }
}

//--------------------------------------------------------------------------
// tidyDrvOpts
//
//! Tidy up and free memory associated with driver options
//!
//--------------------------------------------------------------------------

void
plP_FreeDrvOpts()
{
    DrvOptCmd *drvp, *drvpl;

    drvp = &drv_opt;
    do
    {
        drvpl = drvp;
        drvp  = drvpl->next;

        free( drvpl->option );
        free( drvpl->value );
        // Free additional DrvOptCmd variables -
        // first entry in list is a static global variable
        if ( drvpl != &drv_opt )
            free( drvpl );
    } while ( drvp != NULL );

    // initialize drv_opt if it's used again
    drv_opt.option = NULL;
    drv_opt.value  = NULL;
    drv_opt.next   = NULL;
}


//--------------------------------------------------------------------------
// Option handlers
//--------------------------------------------------------------------------

//--------------------------------------------------------------------------
// opt_h()
//
//! Performs appropriate action for option "h":
//! Issues help message
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param PL_UNUSED( opt_arg ) Not used.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 2.
//!
//--------------------------------------------------------------------------

static int
opt_h( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR PL_UNUSED( opt_arg ), void * PL_UNUSED( client_data ) )
{
    if ( !mode_quiet )
        Help();

    return 2;
}

//--------------------------------------------------------------------------
// opt_v()
//
//! Performs appropriate action for option "v":
//! Issues version message
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param PL_UNUSED( opt_arg ) Not used.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 2.
//!
//--------------------------------------------------------------------------

static int
opt_v( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR PL_UNUSED( opt_arg ), void * PL_UNUSED( client_data ) )
{
    if ( !mode_quiet )
        fprintf( stderr, "PLplot library version: %s\n", PLPLOT_VERSION );

    return 2;
}

//--------------------------------------------------------------------------
// opt_verbose()
//
//! Performs appropriate action for option "verbose":
//! Turn on verbosity flag
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param PL_UNUSED( opt_arg ) Not used.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_verbose( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR PL_UNUSED( opt_arg ), void * PL_UNUSED( client_data ) )
{
    plsc->verbose = 1;
    return 0;
}

//--------------------------------------------------------------------------
// opt_debug()
//
//! Performs appropriate action for option "debug":
//! Turn on debugging flag
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param PL_UNUSED( opt_arg ) Not used.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_debug( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR PL_UNUSED( opt_arg ), void * PL_UNUSED( client_data ) )
{
    plsc->debug   = 1;
    plsc->verbose = 1;
    return 0;
}

//--------------------------------------------------------------------------
// opt_hack()
//
//! Performs appropriate action for option "hack":
//! Enables driver-specific hack(s)
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param PL_UNUSED( opt_arg ) Not used.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_hack( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR PL_UNUSED( opt_arg ), void * PL_UNUSED( client_data ) )
{
    plsc->hack = 1;
    return 0;
}

//--------------------------------------------------------------------------
// opt_dev()
//
//! Performs appropriate action for option "dev":
//! Sets output device keyword
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg The name of the output device.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_dev( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    plsdev( opt_arg );
    return 0;
}

//--------------------------------------------------------------------------
// opt_o()
//
//! Performs appropriate action for option "o":
//! Sets output file name
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg The file family name.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_o( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    plsfnam( opt_arg );
    return 0;
}

//--------------------------------------------------------------------------
// opt_mar()
//
//! Performs appropriate action for option "mar":
//! Sets relative margin width
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg Plot margin width.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_mar( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    plsdidev( atof( opt_arg ), PL_NOTSET, PL_NOTSET, PL_NOTSET );
    return 0;
}

//--------------------------------------------------------------------------
// opt_a()
//
//! Performs appropriate action for option "a":
//! Sets plot aspect ratio on page
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg Plot aspect ratio.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_a( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    plsdidev( PL_NOTSET, atof( opt_arg ), PL_NOTSET, PL_NOTSET );
    return 0;
}

//--------------------------------------------------------------------------
// opt_jx()
//
//! Performs appropriate action for option "jx":
//! Sets relative justification in x
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg Plot relative justification in x(?)
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_jx( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    plsdidev( PL_NOTSET, PL_NOTSET, atof( opt_arg ), PL_NOTSET );
    return 0;
}

//--------------------------------------------------------------------------
// opt_jy()
//
//! Performs appropriate action for option "jy":
//! Sets relative justification in y
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg Plot relative justification in y(?)
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_jy( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    plsdidev( PL_NOTSET, PL_NOTSET, PL_NOTSET, atof( opt_arg ) );
    return 0;
}

//--------------------------------------------------------------------------
// opt_ori()
//
//! Performs appropriate action for option "ori":
//! Sets orientation
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg Plot orientation.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_ori( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    plsdiori( atof( opt_arg ) );
    return 0;
}

//--------------------------------------------------------------------------
// opt_freeaspect()
//
//! Performs appropriate action for option "freeaspect":
//! Allow aspect ratio to adjust to orientation swaps.
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param PL_UNUSED( opt_arg ) Not used.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_freeaspect( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR PL_UNUSED( opt_arg ), void * PL_UNUSED( client_data ) )
{
    plsc->freeaspect = 1;
    return 0;
}

//--------------------------------------------------------------------------
// opt_portrait()
//
//! Performs appropriate action for option "portrait":
//! Set portrait mode.  If plsc->portrait = 1, then the orientation for certain
//! drivers is changed by 90 deg to portrait orientation from the default
//! landscape orientation used by PLplot while the aspect ratio allowed to
//! adjust using freeaspect.
//! N.B. the driver list where this flag is honored is currently limited
//! to psc, ps, and pstex.  A 90 deg rotation is just not
//! appropriate for certain other drivers.  These drivers where portrait
//! mode is ignored include display drivers (e.g., xwin, tk), drivers
//! which are subequently going to be transformed to another form
//! (e.g., meta), or drivers which are normally used for web
//! publishing (e.g., png, jpeg).  That said, the case is not entirely clear
//! for all drivers so the list of drivers where portrait mode is honored
//! may increase in the future. To add to the list simply copy the small
//! bit of code from  ps.c that has to do with pls->portrait to the
//! appropriate driver file.
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param PL_UNUSED( opt_arg ) Not used.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_portrait( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR PL_UNUSED( opt_arg ), void * PL_UNUSED( client_data ) )
{
    plsc->portrait = 1;
    return 0;
}

//--------------------------------------------------------------------------
// opt_width()
//
//! Performs appropriate action for option "width":
//! Sets pen width
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg Plot pen width.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_width( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    double width;

    width = atof( opt_arg );
    if ( width < 0. )
    {
        fprintf( stderr, "?invalid width\n" );
        return 1;
    }
    else
    {
        plwidth( width );
        plsc->widthlock = 1;
    }
    return 0;
}

//--------------------------------------------------------------------------
// opt_bg()
//
//! Performs appropriate action for option "bg":
//! Sets background color (rgb represented in hex on command line) and alpha
//! (represented as floating point on the command line with underscore
//! delimiter), e.g.,
//! -bg ff0000 (set background to red with an alpha value of MAX_PLFLT_ALPHA ==> opaque)
//! -bg ff0000_0.1 (set background to red with an alpha value of 0.1)
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg Background RGB color in hex (in 3-digit of 6-digit format)
//! followed by optional combination of "_" + floating-point alpha value.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_bg( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    PLCHAR_VECTOR rgb;
    char          *color_field, *color_field_wp, *color_field_wp_alt, *alpha_field;
    char          *endptr;
    long          bgcolor;
    PLINT         r, g, b;
    PLFLT         a;
    int           save_errno;

    // Strip off leading "#" (TK-ism) if present.

    if ( *opt_arg == '#' )
        rgb = opt_arg + 1;
    else
        rgb = opt_arg;

    strncpy( opttmp, rgb, OPTMAX - 1 );
    opttmp[OPTMAX - 1] = '\0';

    //fprintf( stderr, "-bg option = %s\n", opttmp );
    color_field = opttmp;
    alpha_field = strchr( opttmp, '_' );
    if ( alpha_field != NULL )
    {
        // null-terminate color_field at the position of the delimiter.
        *alpha_field = '\0';
        // point alpha_field at the position one beyond the delimiter.
        alpha_field++;
    }
    else
    {
        // If no delimiter, then assume opaque.
        alpha_field = "MAX_PLFLT_ALPHA";
    }

    //fprintf( stderr, "color_field = %s\n", color_field );
    //fprintf( stderr, "alpha_field = %s\n", alpha_field );

    // Parse color_field
    errno      = 0; // To distinguish success/failure after call
    bgcolor    = strtol( color_field, &endptr, 16 );
    save_errno = errno;

    // Check for various possible errors

    if ( ( errno == ERANGE && ( bgcolor == LONG_MIN || bgcolor == LONG_MAX ) ) || ( errno != 0 && bgcolor == 0 ) )
    {
        plwarn( "opt_bg: parsing of color_field as hex to a long caused integer overflow so use (warning) red" );
        fprintf( stderr, "%s\n", "Further information relevant to this warning:" );
        errno = save_errno;
        perror( "opt_bg strtol issue" );
        fprintf( stderr, "color_field = %s\n", color_field );
        color_field = "ff0000";
        fprintf( stderr, "derived color_field = %s\n", color_field );
        bgcolor = strtol( color_field, &endptr, 16 );
        fprintf( stderr, "derived bgcolor = %#lx\n", bgcolor );
    }
    else if ( endptr == color_field )
    {
        plwarn( "opt_bg: color_field could not be parsed as hex to a long so use (warning) red" );
        fprintf( stderr, "%s\n", "Further information relevant to this warning:" );
        fprintf( stderr, "color_field = %s\n", color_field );
        color_field = "ff0000";
        fprintf( stderr, "derived color_field = %s\n", color_field );
        bgcolor = strtol( color_field, &endptr, 16 );
        fprintf( stderr, "derived bgcolor = %#lx\n", bgcolor );
    }
    else if ( *endptr != '\0' )
    {
        plwarn( "opt_bg: color_field could be parsed as hex to a long but there was trailing garbage which was ignored" );
        fprintf( stderr, "%s\n", "Further information relevant to this warning:" );
        fprintf( stderr, "color_field = %s\n", color_field );
        // Trim trailing garbage off of color_field.
        *endptr = '\0';
        fprintf( stderr, "derived color_field = %s\n", color_field );
        fprintf( stderr, "derived bgcolor = %#lx\n", bgcolor );
    }

    // If bgcolor has 3 digits, each is "doubled" (i.e. ABC becomes AABBCC).

    // Find color_field without prefix where that prefix consists of optional whitespace followed
    // by optional sign followed by optional 0x or 0X.
    color_field_wp = color_field;
    while ( isspace( (unsigned char) *color_field_wp ) )
        color_field_wp++;
    if ( ( color_field_wp_alt = strstr( color_field_wp, "+" ) ) == color_field_wp ||
         ( color_field_wp_alt = strstr( color_field_wp, "-" ) ) == color_field_wp )
        color_field_wp += 1;
    if ( ( color_field_wp_alt = strstr( color_field_wp, "0x" ) ) == color_field_wp ||
         ( color_field_wp_alt = strstr( color_field_wp, "0X" ) ) == color_field_wp )
        color_field_wp += 2;

    switch ( strlen( color_field_wp ) )
    {
    case 3:
        r = (PLINT) ( ( bgcolor & 0xF00 ) >> 8 );
        g = (PLINT) ( ( bgcolor & 0x0F0 ) >> 4 );
        b = (PLINT) ( bgcolor & 0x00F );

        r = r | ( r << 4 );
        g = g | ( g << 4 );       // doubling
        b = b | ( b << 4 );
        break;

    case 6:
        r = (PLINT) ( ( bgcolor & 0xFF0000 ) >> 16 );
        g = (PLINT) ( ( bgcolor & 0x00FF00 ) >> 8 );
        b = (PLINT) ( bgcolor & 0x0000FF );
        break;

    default:
        plwarn( "opt_bg: color_field without prefix is not of the correct form.  Therefore use (warning) red" );
        fprintf( stderr, "%s\n", "Further information relevant to this warning:" );
        fprintf( stderr, "color_field = %s\n", color_field );
        fprintf( stderr, "%s\n", "The correct form of color_field without prefix is exactly 3 or 6 hex digits" );
        fprintf( stderr, "color_field without prefix = %s\n", color_field_wp );
        r = MAX_PLINT_RGB;
        g = 0;
        b = 0;
        fprintf( stderr, "derived r, g, b = %d, %d, %d\n", r, g, b );
    }

    // Parse alpha_field using strtod and checking for all potential issues.
    errno      = 0; // To distinguish success/failure after call
    a          = (PLFLT) strtod( alpha_field, &endptr );
    save_errno = errno;

    // Check for various possible errors

    if ( errno == ERANGE && ( a == HUGE_VAL || a == -HUGE_VAL ) )
    {
        plwarn( "opt_bg: parsing of alpha_field to a double caused floating overflow so use opaque" );
        fprintf( stderr, "%s\n", "Further information relevant to this warning:" );
        errno = save_errno;
        perror( "opt_bg strtod issue" );
        fprintf( stderr, "alpha_field = %s\n", alpha_field );
        a = MAX_PLFLT_ALPHA;
        fprintf( stderr, "derived alpha value = %e\n", a );
    }
    else if ( endptr == alpha_field )
    {
        plwarn( "opt_bg: alpha_field could not be parsed to a double so use opaque" );
        fprintf( stderr, "%s\n", "Further information relevant to this warning:" );
        fprintf( stderr, "alpha_field = %s\n", alpha_field );
        a = MAX_PLFLT_ALPHA;
        fprintf( stderr, "derived alpha value = %e\n", a );
    }
    else if ( *endptr != '\0' )
    {
        plwarn( "opt_bg: alpha_field could be parsed to a double but there was trailing garbage which was ignored" );
        fprintf( stderr, "%s\n", "Further information relevant to this warning:" );
        fprintf( stderr, "alpha_field = %s\n", alpha_field );
        fprintf( stderr, "derived alpha value = %e\n", a );
    }

    //fprintf( stderr, "r, g, b, alpha = %d, %d, %d, %e\n", r, g, b, a );
    plscolbga( r, g, b, a );

    return 0;
}

//--------------------------------------------------------------------------
// opt_ncol0()
//
//! Performs appropriate action for option "ncol0":
//! Sets number of colors to allocate in cmap 0 (upper bound).
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg Number of color map 0 colors.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_ncol0( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    plsc->ncol0 = atoi( opt_arg );
    return 0;
}

//--------------------------------------------------------------------------
// opt_ncol1()
//
//! Performs appropriate action for option "ncol1":
//! Sets number of colors to allocate in cmap 1 (upper bound).
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg Number of color map 1 colors.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_ncol1( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    plsc->ncol1 = atoi( opt_arg );
    return 0;
}

//--------------------------------------------------------------------------
// opt_wplt()
//
//! Performs appropriate action for option "wplt":
//! Sets (zoom) window into plot (e.g. "0,0,0.5,0.5")
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg Zoom setting.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_wplt( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    char  *field;
    PLFLT xl, yl, xr, yr;

    strncpy( opttmp, opt_arg, OPTMAX - 1 );
    opttmp[OPTMAX - 1] = '\0';

    if ( ( field = strtok( opttmp, "," ) ) == NULL )
        return 1;

    xl = atof( field );

    if ( ( field = strtok( NULL, "," ) ) == NULL )
        return 1;

    yl = atof( field );

    if ( ( field = strtok( NULL, "," ) ) == NULL )
        return 1;

    xr = atof( field );

    if ( ( field = strtok( NULL, "," ) ) == NULL )
        return 1;

    yr = atof( field );

    plsdiplt( xl, yl, xr, yr );
    return 0;
}

//--------------------------------------------------------------------------
// opt_drvopt()
//
//! Get driver specific options in the form <option[=value]>[,option[=value]]*
//! If "value" is not specified, it defaults to "1".
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg The driver specific option.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_drvopt( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    char      t, *tt, *option, *value;
    int       fl = 0;
    DrvOptCmd *drvp;

    option = (char *) malloc( (size_t) ( 1 + strlen( opt_arg ) ) * sizeof ( char ) );
    if ( option == NULL )
        plexit( "opt_drvopt: Out of memory!?" );

    value = (char *) malloc( (size_t) ( 1 + strlen( opt_arg ) ) * sizeof ( char ) );
    if ( value == NULL )
        plexit( "opt_drvopt: Out of memory!?" );

    drvp    = &drv_opt;
    *option = *value = '\0';
    tt      = option;
    while ( ( t = *opt_arg++ ) )
    {
        switch ( t )
        {
        case ',':
            if ( fl )
                fl = 0;
            else
            {
                value[0] = '1';
                value[1] = '\0';
            }

            *tt          = '\0'; tt = option;
            drvp->option = plstrdup( option );                                               // it should not be release, because of familying
            drvp->value  = plstrdup( value );                                                // don't release
            drvp->next   = (DrvOptCmd *) malloc( sizeof ( DrvOptCmd ) );                     // don't release
            if ( drvp->next == NULL )
                plexit( "opt_drvopt: Out of memory!?\n" );

            drvp = drvp->next;
            break;

        case '=':
            fl  = 1;
            *tt = '\0'; tt = value;
            break;

        default:
            *tt++ = t;
        }
    }

    *tt = '\0';
    if ( !fl )
    {
        value[0] = '1';
        value[1] = '\0';
    }

    drvp->option = plstrdup( option );                       // don't release
    drvp->value  = plstrdup( value );                        // don't release
    drvp->next   = NULL;

#ifdef DEBUG
    fprintf( stderr, "\nopt_drvopt: -drvopt parsed options:\n" );
    drvp = &drv_opt;
    do
        fprintf( stderr, "%s %s\n", drvp->option, drvp->value );
    while ( drvp = drvp->next );
    fprintf( stderr, "\n" );
#endif

    free( option ); free( value );

    return 0;
}

//--------------------------------------------------------------------------
// opt_fam()
//
//! Performs appropriate action for option "fam":
//! Enables family output files
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param PL_UNUSED( opt_arg ) Not used.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_fam( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR PL_UNUSED( opt_arg ), void * PL_UNUSED( client_data ) )
{
    plsfam( 1, -1, -1 );
    return 0;
}

//--------------------------------------------------------------------------
// opt_fsiz()
//
//! Performs appropriate action for option "fsiz":
//! Sets size of a family member file (may be somewhat larger since eof must
//! occur at a page break).  Also turns on familying.  Example usage:
//!
//!	-fsiz 5M	(5 MB)
//!	-fsiz 300K	(300 KB)
//!	-fsiz .3M	(same)
//!	-fsiz .5G	(half a GB)
//!
//! Note case of the trailing suffix doesn't matter.
//! If no suffix, defaults to MB.
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg Family size setting.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_fsiz( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    PLINT  bytemax;
    size_t len        = strlen( opt_arg );
    char   lastchar   = opt_arg[len - 1];
    PLFLT  multiplier = 1.0e6;
    char   *spec      = (char *) malloc( len + 1 );

    if ( spec == NULL )
        plexit( "opt_fsiz: Insufficient memory" );

// Interpret optional suffix

    switch ( lastchar )
    {
    case 'k':
    case 'K':
        multiplier = 1.0e3; len--;
        break;
    case 'm':
    case 'M':
        multiplier = 1.0e6; len--;
        break;
    case 'g':
    case 'G':
        multiplier = 1.0e9; len--;
        break;
    }
    strncpy( spec, opt_arg, len );
    spec[len] = '\0';

    bytemax = (PLINT) ( multiplier * atof( spec ) );
    if ( bytemax <= 0 )
    {
        fprintf( stderr, "?invalid file size %d. 2.14G is the maximum.\n", bytemax );
        return 1;
    }
    plsfam( 1, -1, bytemax );

    free( spec );
    return 0;
}

//--------------------------------------------------------------------------
// opt_fbeg()
//
//! Performs appropriate action for option "fbeg":
//! Starts with the specified family member number.
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg Number of the first plot.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_fbeg( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    plsc->member = atoi( opt_arg );

    return 0;
}

//--------------------------------------------------------------------------
// opt_finc()
//
//! Performs appropriate action for option "finc":
//! Specify increment between family members.
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg Amount to increment the plot number between plots.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_finc( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    plsc->finc = atoi( opt_arg );

    return 0;
}

//--------------------------------------------------------------------------
// opt_fflen()
//
//! Performs appropriate action for option "fflen":
//! Specify minimum field length for family member number.
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg Size of the family number field (e.g. "1", "01", "001" ?)
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_fflen( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    plsc->fflen = atoi( opt_arg );

    return 0;
}

//--------------------------------------------------------------------------
// opt_np()
//
//! Performs appropriate action for option "np":
//! Disables pause between pages
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param PL_UNUSED( opt_arg ) Not used.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_np( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR PL_UNUSED( opt_arg ), void * PL_UNUSED( client_data ) )
{
    plspause( 0 );
    return 0;
}

//--------------------------------------------------------------------------
// opt_nopixmap()
//
//! Performs appropriate action for option "nopixmap":
//! Disables use of pixmaps in X drivers
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param PL_UNUSED( opt_arg ) Not used.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_nopixmap( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR PL_UNUSED( opt_arg ), void * PL_UNUSED( client_data ) )
{
    plsc->nopixmap = 1;
    return 0;
}

//--------------------------------------------------------------------------
// opt_db()
//
//! Performs appropriate action for option "db":
//! Double buffer X output (update only done on eop or Expose)
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param PL_UNUSED( opt_arg ) Not used.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_db( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR PL_UNUSED( opt_arg ), void * PL_UNUSED( client_data ) )
{
    plsc->db = 1;
    return 0;
}

//--------------------------------------------------------------------------
// opt_bufmax()
//
//! Performs appropriate action for option "bufmax":
//! Sets size of data buffer for tk driver
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg Size of the data buffer for the tk driver.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_bufmax( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    plsc->bufmax = atoi( opt_arg );
    return 0;
}

//--------------------------------------------------------------------------
// opt_server_name()
//
//! Performs appropriate action for option "server_name":
//! Sets main window name of server (Tcl/TK driver only)
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg The name of the main window.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_server_name( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    plsc->server_name = plstrdup( opt_arg );
    return 0;
}

//--------------------------------------------------------------------------
// opt_plserver()
//
//! Performs appropriate action for option "plserver":
//! Sets name to use when invoking server (Tcl/TK driver only)
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg Name of Tcl/TK server (?).
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_plserver( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    plsc->plserver = plstrdup( opt_arg );
    return 0;
}

//--------------------------------------------------------------------------
// opt_plwindow()
//
//! Performs appropriate action for option "plwindow":
//! Sets PLplot window name
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg Name of the window.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_plwindow( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    if ( ( plsc->plwindow = (char *) malloc( (size_t) ( 1 + strlen( opt_arg ) ) * sizeof ( char ) ) ) == NULL )
    {
        plexit( "opt_plwindow: Insufficient memory" );
    }
    strcpy( plsc->plwindow, opt_arg );
    return 0;
}

//--------------------------------------------------------------------------
// opt_auto_path()
//
//! Performs appropriate action for option "auto_path":
//! Sets additional directories to autoload
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg Additional directories to add the the load path (?).
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_auto_path( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    plsc->auto_path = plstrdup( opt_arg );
    return 0;
}

//--------------------------------------------------------------------------
// opt_px()
//
//! Performs appropriate action for option "px":
//! Set packing in x
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg X packing (?).
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_px( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    plssub( atoi( opt_arg ), -1 );
    return 0;
}

//--------------------------------------------------------------------------
// opt_py()
//
//! Performs appropriate action for option "py":
//! Set packing in y
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg Y packing (?).
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_py( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    plssub( -1, atoi( opt_arg ) );
    return 0;
}

//--------------------------------------------------------------------------
// opt_geo()
//
//! Performs appropriate action for option "geo": Set geometry for
//! output window, i.e., "-geometry WIDTHxHEIGHT+XOFF+YOFF" where
//! WIDTHxHEIGHT, +XOFF+YOFF, or both must be present, and +XOFF+YOFF
//! stands for one of the four combinations +XOFF+YOFF, +XOFF-YOFF,
//! -XOFF+YOFF, and -XOFF-YOFF.  Some examples are the following:
//! -geometry 400x300, -geometry -100+200, and -geometry 400x300-100+200.
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg Plot geometry descriptor.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_geo( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    int   numargs;
    PLFLT xdpi = 0., ydpi = 0.;
    PLINT xwid, ywid, xoff, yoff;

// The TK driver uses the geometry string directly

    if ( ( plsc->geometry = (char *) malloc( (size_t) ( 1 + strlen( opt_arg ) ) * sizeof ( char ) ) ) == NULL )
    {
        plexit( "opt_geo: Insufficient memory" );
    }

    strcpy( plsc->geometry, opt_arg );

    numargs = sscanf( opt_arg, "%dx%d%d%d", &xwid, &ywid, &xoff, &yoff );
    if ( numargs == 2 )
    {
        xoff = 0;
        yoff = 0;
        if ( xwid == 0 )
            fprintf( stderr, "?invalid xwid in -geometry %s\n", opt_arg );
        if ( ywid == 0 )
            fprintf( stderr, "?invalid ywid in -geometry %s\n", opt_arg );
        if ( xwid < 0 )
        {
            fprintf( stderr, "?invalid xwid in -geometry %s\n", opt_arg );
            return 1;
        }
        if ( ywid < 0 )
        {
            fprintf( stderr, "?invalid ywid in -geometry %s\n", opt_arg );
            return 1;
        }
    }
    else if ( numargs == 4 )
    {
        if ( xwid == 0 )
            fprintf( stderr, "?invalid xwid in -geometry %s\n", opt_arg );
        if ( ywid == 0 )
            fprintf( stderr, "?invalid ywid in -geometry %s\n", opt_arg );
        if ( xwid < 0 )
        {
            fprintf( stderr, "?invalid xwid in -geometry %s\n", opt_arg );
            return 1;
        }
        if ( ywid < 0 )
        {
            fprintf( stderr, "?invalid ywid in -geometry %s\n", opt_arg );
            return 1;
        }
        if ( abs( xoff ) == 0 )
            fprintf( stderr, "?invalid xoff in -geometry %s\n", opt_arg );
        if ( abs( yoff ) == 0 )
            fprintf( stderr, "?invalid yoff in -geometry %s\n", opt_arg );
    }
    else
    {
        numargs = sscanf( opt_arg, "%d%d", &xoff, &yoff );
        if ( numargs == 2 )
        {
            xwid = 0;
            ywid = 0;
            if ( abs( xoff ) == 0 )
                fprintf( stderr, "?invalid xoff in -geometry %s\n", opt_arg );
            if ( abs( yoff ) == 0 )
                fprintf( stderr, "?invalid yoff in -geometry %s\n", opt_arg );
        }
        else
        {
            fprintf( stderr, "?invalid -geometry %s\n", opt_arg );
            return 1;
        }
    }
    //fprintf( stderr, "xwid, ywid, xoff, yoff = %d, %d, %d, %d\n", xwid, ywid, xoff, yoff );
    plspage( xdpi, ydpi, xwid, ywid, xoff, yoff );
    return 0;
}

//--------------------------------------------------------------------------
// opt_tk_file()
//
//! File name for plserver tk_file option
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg Tk file name.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_tk_file( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    if ( ( plsc->tk_file = (char *) malloc( (size_t) ( 1 + strlen( opt_arg ) ) * sizeof ( char ) ) ) == NULL )
    {
        plexit( "opt_tk_file: Insufficient memory" );
    }

    strcpy( plsc->tk_file, opt_arg );
    return 0;
}

//--------------------------------------------------------------------------
// opt_dpi()
//
//! Performs appropriate action for option "dpi":
//! Set dpi resolution for output device
//!   e.g.,  "-dpi 600x300", will set X dpi to 600 and Y dpi to 300
//!              or
//!   e.g., "-dpi 1200"
//! Will set both X and Y dpi to 1200 dpi
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg DPI descriptor string.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_dpi( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    char  *field;
    PLFLT xdpi = 0., ydpi = 0.;
    PLINT xwid = 0, ywid = 0, xoff = 0, yoff = 0;

    strncpy( opttmp, opt_arg, OPTMAX - 1 );
    opttmp[OPTMAX - 1] = '\0';
    if ( strchr( opttmp, 'x' ) )
    {
        field = strtok( opttmp, "x" );
        xdpi  = atof( field );
        if ( xdpi == 0 )
            fprintf( stderr, "?invalid xdpi\n" );

        if ( ( field = strtok( NULL, " " ) ) == NULL )
            return 1;

        ydpi = atof( field );
        if ( ydpi == 0 )
            fprintf( stderr, "?invalid ydpi\n" );
    }
    else
    {
        xdpi = atof( opttmp );
        ydpi = xdpi;
        if ( xdpi == 0 )
            return 1;
    }

    plspage( xdpi, ydpi, xwid, ywid, xoff, yoff );
    return 0;
}

//--------------------------------------------------------------------------
// opt_dev_compression()
//
//! Sets device compression
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg Device compression (?).
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_dev_compression( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    PLINT comp = 0;

    comp = atoi( opt_arg );
    if ( comp == 0 )
    {
        fprintf( stderr, "?invalid compression\n" );
        return 1;
    }
    plscompression( comp );

    return 0;
}

//--------------------------------------------------------------------------
// opt_cmap0()
//
//! Sets color table 0 based on a cmap0.pal file.
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg Name of color map 0 .pal file.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_cmap0( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    plspal0( opt_arg );
    return 0;
}

//--------------------------------------------------------------------------
// opt_cmap1()
//
//! Sets color table 1 based on a cmap1.pal file.
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg Name of a color map 1 .pal file.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_cmap1( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    plspal1( opt_arg, TRUE );
    return 0;
}

//--------------------------------------------------------------------------
// opt_locale()
//
//! Make PLplot portable to all LC_NUMERIC locales.
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param PL_UNUSED( opt_arg ) Not used.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_locale( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR PL_UNUSED( opt_arg ), void * PL_UNUSED( client_data ) )
{
    char *locale;
    if ( ( locale = setlocale( LC_NUMERIC, "" ) ) )
    {
        printf( "LC_NUMERIC locale set to \"%s\"\n", locale );
    }
    else
    {
        plwarn( "Could not use invalid environment (e.g., LC_ALL, LC_NUMERIC, or LANG) to set LC_NUMERIC locale.  Falling back to LC_NUMERIC \"C\" locale instead.\n" );
        if ( !( locale = setlocale( LC_NUMERIC, "C" ) ) )
        {
            plexit( "Your platform is seriously broken.  Not even a \"C\" locale could be set." );
        }
    }
    return 0;
}

//--------------------------------------------------------------------------
// opt_eofill()
//
//! For the case where the boundary of the filled region is
//! self-intersecting, use the even-odd fill rule rather than the
//! default nonzero fill rule.
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param PL_UNUSED( opt_arg ) Not used.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_eofill( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR PL_UNUSED( opt_arg ), void * PL_UNUSED( client_data ) )
{
    plsc->dev_eofill = 1;
    if ( plsc->level > 0 )
        plP_state( PLSTATE_EOFILL );
    return 0;
}

//--------------------------------------------------------------------------
// opt_mfo()
//
//! Sets the filename of the PLplot metafile that will be written.
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg  Output PLplot metafile.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_mfo( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    if ( ( plsc->mf_outfile = (char *) malloc( (size_t) ( 1 + strlen( opt_arg ) ) * sizeof ( char ) ) ) == NULL )
    {
        plexit( "opt_mfo: Insufficient memory" );
    }

    strcpy( plsc->mf_outfile, opt_arg );
    return 0;
}

//--------------------------------------------------------------------------
// opt_mfi()
//
//! Sets the filename of the PLplot metafile that will be read.
//!
//! @param PL_UNUSED( opt ) Not used.
//! @param opt_arg Input PLplot metafile.
//! @param PL_UNUSED( client_data ) Not used.
//!
//! returns 0.
//!
//--------------------------------------------------------------------------

static int
opt_mfi( PLCHAR_VECTOR PL_UNUSED( opt ), PLCHAR_VECTOR opt_arg, void * PL_UNUSED( client_data ) )
{
    if ( ( plsc->mf_infile = (char *) malloc( (size_t) ( 1 + strlen( opt_arg ) ) * sizeof ( char ) ) ) == NULL )
    {
        plexit( "opt_mfi: Insufficient memory" );
    }

    strcpy( plsc->mf_infile, opt_arg );
    return 0;
}