|
root / base / usr / src / cmd / cfgadm / cfgadm.c
cfgadm.c C 2428 lines 50.5 KB
   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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * This is the main program file for the configuration administration
 * command as set out in manual page cfgadm(8).  It uses the configuration
 * administration library interface, libcfgadm, as set out in manual
 * page config_admin(3CFGADM).
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <langinfo.h>
#include <time.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/sunddi.h>
#include <sys/openpromio.h>
#include <sys/ddi_impldefs.h>
#include <sys/systeminfo.h>
#include <ctype.h>
#include <zone.h>

#include <config_admin.h>
#include "cfgadm.h"

#define	S_FREE(x)	(((x) != NULL) ? (free(x), (x) = NULL) : (void *)0)
#define	GET_DYN(a)	(strstr((a), CFGA_DYN_SEP))
/*
 * forward declarations
 */
static char *basename(char *);
static void cfgadm_error(int, char *);
static int confirm_interactive(void *, const char *);
static int confirm_no(void *, const char *);
static int confirm_yes(void *, const char *);
static void usage(void);
static void usage_field(void);
static int extract_list_suboptions(char *, char **, char **, char **,
    int *, char **, char **, char **);
static int message_output(void *appdata_ptr, const char *message);
static void *config_calloc_check(size_t, size_t);
static cfga_ap_types_t find_arg_type(const char *);
static int yesno(char *, char *);


static int compare_ap_id(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
static int compare_r_state(cfga_list_data_t *, cfga_list_data_t *,
    match_type_t);
static int compare_o_state(cfga_list_data_t *, cfga_list_data_t *,
    match_type_t);
static int compare_cond(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
static int compare_time(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
static int compare_info(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
static int compare_type(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
static int compare_busy(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
static int compare_class(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
static int compare_null(cfga_list_data_t *, cfga_list_data_t *, match_type_t);
static void print_log_id(cfga_list_data_t *, int, char *);
static void print_r_state(cfga_list_data_t *, int, char *);
static void print_o_state(cfga_list_data_t *, int, char *);
static void print_cond(cfga_list_data_t *, int, char *);
static void print_time(cfga_list_data_t *, int, char *);
static void print_time_p(cfga_list_data_t *, int, char *);
static void print_info(cfga_list_data_t *, int, char *);
static void print_type(cfga_list_data_t *, int, char *);
static void print_busy(cfga_list_data_t *, int, char *);
static void print_phys_id(cfga_list_data_t *, int, char *);
static void print_class(cfga_list_data_t *, int, char *);
static void print_null(cfga_list_data_t *, int, char *);
static int count_fields(char *, char);
static int process_sort_fields(int, struct sort_el *, char *);
static int process_fields(int, struct print_col *, int, char *);
static cfga_err_t print_fields(int, struct print_col *, int, int, char *,
    cfga_list_data_t *, FILE *);
static int ldata_compare(const void *, const void *);

static void arg_got_resp(ap_arg_t *inp, ap_out_t *out_array, int nouts,
    int dyn_exp);
static void out_was_req(ap_out_t *outp, ap_arg_t *in_array, int nargs,
    int no_dyn);
static void report_no_response(ap_arg_t *arg_array, int napids_to_list);

static cfga_err_t set_log_flt(cfga_list_data_t *p, const char *val);
static cfga_err_t set_type_flt(cfga_list_data_t *p, const char *val);
static cfga_err_t set_class_flt(cfga_list_data_t *p, const char *val);

static char *get_dyn(const char *ap_id);
static void remove_dyn(char *ap_id);
static char *s_strdup(char *str);

/*
 * global data
 */
/* command name for messages */
static char *cmdname;

/*
 * control for comparing, printing and filtering cfga_list_data
 * NOTE:Field names (i.e. member 0 of field_info struct) may not contain '('.
 *	The post filtering code depends on it.
 * NOTE:A NULL value for the set_filter member indicates that filtering based
 *	on that field is currently not supported.
 */
static struct field_info all_fields[] = {
{"ap_id", "Ap_Id", SZ_EL(ap_log_id), compare_ap_id, print_log_id, set_log_flt},
{"r_state", "Receptacle", STATE_WIDTH, compare_r_state, print_r_state, NULL},
{"o_state", "Occupant", STATE_WIDTH, compare_o_state, print_o_state, NULL},
{"condition", "Condition", COND_WIDTH, compare_cond, print_cond, NULL},
{"status_time", "When", TIME_WIDTH, compare_time, print_time, NULL},
{"status_time_p", "When", TIME_P_WIDTH, compare_time, print_time_p, NULL},
{"info", "Information", SZ_EL(ap_info), compare_info, print_info, NULL},
{"type", "Type", SZ_EL(ap_type), compare_type, print_type, set_type_flt},
{"busy", "Busy", 8, compare_busy, print_busy, NULL},
{"physid", "Phys_Id", SZ_EL(ap_phys_id), compare_ap_id, print_phys_id, NULL},
{"class", "Class", SZ_EL(ap_class), compare_class, print_class, set_class_flt}
};

#define	PREFILT_CLASS_STR	"class="

typedef struct {
	cfga_list_data_t ldata;			/* Selection criteria */
	match_type_t match_type_p[N_FIELDS];	/* Type of match */
} post_filter_t;

static struct field_info null_field =
	{"null", "", 0, compare_null, print_null, NULL};

static struct sort_el *sort_list;	/* Used in ldata_compare() */
static int nsort_list;
static char unk_field[] = "%s: field \"%s\" unknown\n";

static char aptype_no_dyn[] = "%s: Invalid ap_id: %s\n";

/* strings that make up the usage message */
static char *usage_tab[] = {
" %s [-f] [-y|-n] [-v] [-o hardware_opts ] -c function ap_id [ap_id...]\n",
" %s [-f] [-y|-n] [-v] [-o hardware_opts ] -x function ap_id [ap_id...]\n",
" %s [-v] [-s listing_options ] [-o hardware_opts ] [-a]\n"
"\t[-l [ap_id|ap_type...]]\n",
" %s [-v] [-o hardware_opts ] -t ap_id [ap_id...]\n",
" %s [-v] [-o hardware_opts ] -h [ap_id|ap_type...]\n",
};

/* Type of matches currently supported by the select sub-option */
static match_cvt_t match_type_array[] = {
	{"partial", CFGA_MATCH_PARTIAL},
	{"exact", CFGA_MATCH_EXACT}
};

#define	N_MATCH_TYPES	(sizeof (match_type_array)/sizeof (match_type_array[0]))

static cfga_err_t setup_filter(const char *selectp, const char *matchp,
    post_filter_t *post_filtp, char **prefilt_optpp);
static cfga_err_t parse_select_opt(const char *selectp,
    post_filter_t *post_filtp, match_type_t match_type);
static int do_config_list(int, char *[], cfga_list_data_t *, int, char *,
    char *, char *, int, char *, post_filter_t *, int);
static void do_post_filter(ap_out_t *outp, post_filter_t *post_filtp, int *jp);


/*
 * main - the main routine of cfgadm, processes the command line
 * and dispatches functions off to libraries.
 */
int
main(
	int argc,
	char *argv[])
{
	extern char *optarg;
	extern int optind;
	int c;
	char *subopts;
	char *subvalue;
	char *const *ap_args = NULL;
	cfga_cmd_t sc_opt = 0;
	struct cfga_confirm confirm;
	struct cfga_msg message;
	int ret = CFGA_ERROR;
	int i;
	char *estrp = NULL;
	cfga_op_t action = CFGA_OP_NONE;
	char *plat_opts = NULL;
	char *act_arg = NULL;
	enum confirm confarg = CONFIRM_DEFAULT;
	char *list_opts = NULL;
	cfga_flags_t flags = 0;
	int arg_error = 0;
	int dyn_exp = 0;

	estrp = NULL;
	if (argc > 0)
		cmdname = basename(argv[0]);
	else
		cmdname = "cfgadm";
	(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)
#define	TEXT_DOMAIN	"SYS_TEST"
#endif
	(void) textdomain(TEXT_DOMAIN);

	while ((c = getopt(argc, argv, OPTIONS)) != EOF) {
		static char dup_action[] =
"%s: more than one action specified (-c,-l,-t,-x)\n";
		static char dup_option[] =
"%s: more than one -%c option specified\n";
		switch (c) {
		case 'a':
			if (dyn_exp) {
				arg_error = 1;
				(void) fprintf(stderr, gettext(dup_option),
				    cmdname, c);
			}
			dyn_exp = 1;
			break;
		case 'c':
			if (action != CFGA_OP_NONE) {
				arg_error = 1;
				(void) fprintf(stderr, gettext(dup_action),
				    cmdname);
			}
			action = CFGA_OP_CHANGE_STATE;
			subopts = optarg;
			subvalue = NULL;
			/*
			 * Reject -c suboption if they are unrecognized
			 * or more than one or have a associated value.
			 */
			if ((sc_opt = getsubopt(&subopts, state_opts,
			    &subvalue)) == -1 || *subopts != '\0' ||
			    subvalue != NULL) {
				arg_error = 1;
				break;
			}
			break;
		case 'f':
			if ((flags & CFGA_FLAG_FORCE) != 0) {
				arg_error = 1;
				(void) fprintf(stderr, gettext(dup_option),
				    cmdname, c);
			}
			flags |= CFGA_FLAG_FORCE;
			break;
		case 'h':
			if (action != CFGA_OP_NONE) {
				arg_error = 1;
				(void) fprintf(stderr, gettext(dup_action),
				    cmdname);
			}
			action = CFGA_OP_HELP;
			break;
		case 'l':
			if (action != CFGA_OP_NONE) {
				arg_error = 1;
				(void) fprintf(stderr, gettext(dup_action),
				    cmdname);
			}
			action = CFGA_OP_LIST;
			break;
		case 'n':
			if (confarg != CONFIRM_DEFAULT) {
				arg_error = 1;
				(void) fprintf(stderr, gettext(dup_option),
				    cmdname, c);
			}
			confarg = CONFIRM_NO;
			break;
		case 'o':
			if (plat_opts != NULL) {
				arg_error = 1;
				(void) fprintf(stderr, gettext(dup_option),
				    cmdname, c);
			}
			plat_opts = optarg;
			break;
		case 's':
			if (list_opts != NULL) {
				arg_error = 1;
				(void) fprintf(stderr, gettext(dup_option),
				    cmdname, c);
			}
			list_opts = optarg;
			break;
		case 't':
			if (action != CFGA_OP_NONE) {
				arg_error = 1;
				(void) fprintf(stderr, gettext(dup_action),
				    cmdname);
			}
			action = CFGA_OP_TEST;
			break;
		case 'x':
			if (action != CFGA_OP_NONE) {
				arg_error = 1;
				(void) fprintf(stderr, gettext(dup_action),
				    cmdname);
			}
			action = CFGA_OP_PRIVATE;
			act_arg = optarg;
			break;
		case 'v':
			if ((flags & CFGA_FLAG_VERBOSE) != 0) {
				arg_error = 1;
				(void) fprintf(stderr, gettext(dup_option),
				    cmdname, c);
			}
			flags |= CFGA_FLAG_VERBOSE;
			break;
		case 'y':
			if (confarg != CONFIRM_DEFAULT) {
				arg_error = 1;
				(void) fprintf(stderr, gettext(dup_option),
				    cmdname, c);
			}
			confarg = CONFIRM_YES;
			break;
		case '?':	/* getopts issues message is this case */
		default:	/* catch programming errors */
			arg_error = 1;
			break;
		}
	}

	/* default action is list */
	if (action == CFGA_OP_NONE)
		action = CFGA_OP_LIST;

	/* -s and -a option only for list */
	if (action != CFGA_OP_LIST && (list_opts != NULL || dyn_exp)) {
		arg_error = 1;
	}

	if (arg_error) {
		usage();
		exit(EXIT_ARGERROR);
		/*NOTREACHED*/
	}

	if (getzoneid() != GLOBAL_ZONEID) {
		cfgadm_error(CFGA_NOTSUPP,
		    gettext("cfgadm can only be run from the global zone"));
		exit(EXIT_NOTSUPP);
	}

	ap_args = &argv[optind];

	/*
	 * If neither -n of -y was specified, interactive confirmation
	 * is used.  Check if the program has terminal I/O and
	 * enforce -n if not.
	 */
	(void) memset(&confirm, 0, sizeof (confirm));
	if (action == CFGA_OP_CHANGE_STATE || action == CFGA_OP_PRIVATE) {
		if (confarg == CONFIRM_DEFAULT &&
		    !(isatty(fileno(stdin)) && isatty(fileno(stderr))))
			confarg = CONFIRM_NO;
		switch (confarg) {
		case CONFIRM_DEFAULT:
			confirm.confirm = confirm_interactive;
			break;
		case CONFIRM_NO:
			confirm.confirm = confirm_no;
			break;
		case CONFIRM_YES:
			confirm.confirm = confirm_yes;
			break;
		default:	/* paranoia */
			abort();
			/*NOTREACHED*/
		}
	}

	/*
	 * set up message output routine
	 */
	message.message_routine = message_output;

	switch (action) {
	case CFGA_OP_CHANGE_STATE:
		/* Sanity check - requires an argument */
		if ((argc - optind) <= 0) {
			usage();
			break;
		}
		/* Sanity check - args cannot be ap_types */
		for (i = 0; i < (argc - optind); i++) {
			if (find_arg_type(ap_args[i]) == AP_TYPE) {
				usage();
				exit(EXIT_ARGERROR);
				/*NOTREACHED*/
			}
		}
		ret = config_change_state(sc_opt, argc - optind, ap_args,
		    plat_opts, &confirm, &message, &estrp, flags);
		if (ret != CFGA_OK)
			cfgadm_error(ret, estrp);
		break;
	case CFGA_OP_PRIVATE:
		/* Sanity check - requires an argument */
		if ((argc - optind) <= 0) {
			usage();
			break;
		}
		/* Sanity check - args cannot be ap_types */
		for (i = 0; i < (argc - optind); i++) {
			if (find_arg_type(ap_args[i]) == AP_TYPE) {
				usage();
				exit(EXIT_ARGERROR);
				/*NOTREACHED*/
			}
		}

		ret = config_private_func(act_arg, argc - optind, ap_args,
		    plat_opts, &confirm, &message, &estrp, flags);

		if (ret != CFGA_OK)
			cfgadm_error(ret, estrp);
		break;
	case CFGA_OP_TEST:
		/* Sanity check - requires an argument */
		if ((argc - optind) <= 0) {
			usage();
			break;
		}

		if ((flags & ~CFGA_FLAG_VERBOSE) != 0) {
			usage();
			exit(EXIT_ARGERROR);
			/*NOTREACHED*/
		}

		/* Sanity check - args cannot be ap_types */
		for (i = 0; i < (argc - optind); i++) {
			if (find_arg_type(ap_args[i]) == AP_TYPE) {
				usage();
				exit(EXIT_ARGERROR);
				/*NOTREACHED*/
			}
		}
		ret = config_test(argc - optind, ap_args, plat_opts, &message,
		    &estrp, flags);
		if (ret != CFGA_OK)
			cfgadm_error(ret, estrp);
		break;
	case CFGA_OP_HELP:

		if ((flags & ~CFGA_FLAG_VERBOSE) != 0) {
			usage();
			exit(EXIT_ARGERROR);
			/*NOTREACHED*/
		}

		/* always do usage? */
		usage();
		ret = config_help(argc - optind, ap_args, &message, plat_opts,
		    flags);
		if (ret != CFGA_OK)
			cfgadm_error(ret, estrp);
		break;

	case CFGA_OP_LIST: {
		/*
		 * Note that we leak the strdup strings below (we never free
		 * them). This is ok in this context since cfgadm is
		 * a short lived process that will exit shortly freeing
		 * the memory.
		 */
		cfga_list_data_t *list_array = NULL;
		int nlist = 0;
		char *sort_fields = s_strdup(DEF_SORT_FIELDS);
		char *cols = s_strdup(DEF_COLS);
		char *cols2 = s_strdup(DEF_COLS2);
		int noheadings = 0;
		char *delim = s_strdup(DEF_DELIM);
		int exitcode = EXIT_OK;
		int i;
		int type = 0;
		char *selectp = NULL, *matchp = NULL, *prefilt_optp = NULL;
		post_filter_t *post_filtp = NULL;

		if ((flags & ~CFGA_FLAG_VERBOSE) != 0) {
			usage();
			exit(EXIT_ARGERROR);
			/*NOTREACHED*/
		}

		if (flags & CFGA_FLAG_VERBOSE) {
			cols = s_strdup(DEF_COLS_VERBOSE);
			cols2 = s_strdup(DEF_COLS2_VERBOSE);
		}

		if (list_opts != NULL && !extract_list_suboptions(list_opts,
		    &sort_fields, &cols, &cols2, &noheadings, &delim,
		    &selectp, &matchp)) {
			usage_field();
			exit(EXIT_ARGERROR);
			/*NOTREACHED*/
		}

		/*
		 * Scan any args and see if there are any ap_types.
		 * If there are we get all attachment point stats and
		 * then filter what gets printed.
		 */

		type = 0;
		for (i = 0; i < (argc - optind); i++) {
			if (find_arg_type(ap_args[i]) == AP_TYPE) {
				type = 1;
				/* ap_types cannot have dynamic components */
				if (get_dyn(ap_args[i]) != NULL) {
					(void) fprintf(stderr,
					    gettext(aptype_no_dyn),
					    cmdname, ap_args[i]);
					exit(EXIT_ARGERROR);
					/*NOTREACHED*/
				}
				break;
			}
		}

		/* Setup filter */
		post_filtp = config_calloc_check(1, sizeof (*post_filtp));
		if (post_filtp == NULL) {
			exit(EXIT_OPFAILED);
			/*NOTREACHED*/
		}
		if (setup_filter(selectp, matchp, post_filtp, &prefilt_optp)
		    != CFGA_OK) {
			S_FREE(post_filtp);
			exit(EXIT_ARGERROR);
			/*NOTREACHED*/
		}

		list_array = NULL;
		exitcode = EXIT_OK;

		/*
		 * Check for args. No args means find all libs
		 * and call the cfga_list_ext routine with no ap_ids specified.
		 * With args, if any one of the args are ap_types we
		 * again find all attachment points as in the
		 * no-args case above and then select which attachment points
		 * are actually displayed.
		 */
		if (((argc - optind) == 0) || (type == 1)) {
			/*
			 * No args, or atleast 1 ap_type arg
			 */
			ret = config_list_ext(0, NULL, &list_array,
			    &nlist, plat_opts, prefilt_optp, &estrp,
			    dyn_exp ? CFGA_FLAG_LIST_ALL : 0);
		} else {
			/*
			 * If the args are all ap_ids (no ap_types) we call the
			 * cfga_list_ext routine with those specific ap_ids.
			 */
			ret = config_list_ext(argc - optind, ap_args,
			    &list_array, &nlist, plat_opts, prefilt_optp,
			    &estrp, dyn_exp ? CFGA_FLAG_LIST_ALL : 0);
		}

		S_FREE(prefilt_optp);

		if (ret == CFGA_OK) {

			if (do_config_list(
			    (argc - optind), &argv[optind], list_array, nlist,
			    sort_fields, cols, cols2, noheadings, delim,
			    post_filtp, dyn_exp) != CFGA_OK) {
				exitcode = EXIT_ARGERROR;
			} else {
				exitcode = EXIT_OK;
			}

			S_FREE(list_array);
			S_FREE(post_filtp);

			if (estrp != NULL && *estrp != '\0')
				cfgadm_error(CFGA_NOTSUPP, estrp);
			if (exitcode != EXIT_OK) {
				exit(exitcode);
				/*NOTREACHED*/
			}
		} else {

			S_FREE(post_filtp);
			cfgadm_error(ret, estrp);
		}
		break;
	}
	default:	/* paranoia */
		abort();
		/*NOTREACHED*/
	}

	if (ret == CFGA_NOTSUPP) {
		return (EXIT_NOTSUPP);
	} else if (ret != CFGA_OK) {
		return (EXIT_OPFAILED);
	} else {
		return (EXIT_OK);
	}
	/*NOTREACHED*/
}

/*
 * usage - outputs the usage help message.
 */
static void
usage(void)
{
	int i;

	(void) fprintf(stderr, "%s\n", gettext("Usage:"));
	for (i = 0; i < sizeof (usage_tab)/sizeof (usage_tab[0]); i++) {
		(void) fprintf(stderr, gettext(usage_tab[i]), cmdname);
	}
}

/*
 * Emit an error message.
 * As a side-effect the hardware specific error message is deallocated
 * as described in config_admin(3CFGADM).
 */
static void
cfgadm_error(int errnum, char *estrp)
{
	const char *ep;

	ep = config_strerror(errnum);
	if (ep == NULL)
		ep = gettext("configuration administration unknown error");
	if (estrp != NULL && *estrp != '\0') {
		(void) fprintf(stderr, "%s: %s: %s\n", cmdname, ep, estrp);
	} else {
		(void) fprintf(stderr, "%s: %s\n", cmdname, ep);
	}
	if (estrp != NULL)
		free((void *)estrp);
	if (errnum == CFGA_INVAL)
		usage();
}

/*
 * confirm_interactive - prompt user for confirmation
 */
static int
confirm_interactive(
	void *appdata_ptr,
	const char *message)
{
	static char yeschr[YESNO_STR_MAX + 2];
	static char nochr[YESNO_STR_MAX + 2];
	static int inited = 0;
	int isyes;

#ifdef lint
	appdata_ptr = appdata_ptr;
#endif /* lint */
	/*
	 * First time through initialisation.  In the original
	 * version of this command this function is only called once,
	 * but this function is generalized for the future.
	 */
	if (!inited) {
		(void) strncpy(yeschr, nl_langinfo(YESSTR), YESNO_STR_MAX + 1);
		(void) strncpy(nochr, nl_langinfo(NOSTR), YESNO_STR_MAX + 1);
		inited = 1;
	}

	do {
		(void) fprintf(stderr, "%s (%s/%s)? ", message, yeschr, nochr);
		isyes = yesno(yeschr, nochr);
	} while (isyes == -1);
	return (isyes);
}

/*
 * If any text is input it must sub-string match either yes or no.
 * Failure of this match is indicated by return of -1.
 * If an empty line is input, this is taken as no.
 */
static int
yesno(
	char *yesp,
	char *nop)
{
	int	i, b;
	char	ans[YESNO_STR_MAX + 1];

	i = 0;

	/*CONSTCOND*/
	while (1) {
		b = getc(stdin);	/* more explicit that rm.c version */
		if (b == '\n' || b == '\0' || b == EOF) {
			if (i < YESNO_STR_MAX)	/* bug fix to rm.c version */
				ans[i] = 0;
			break;
		}
		if (i < YESNO_STR_MAX)
			ans[i] = b;
		i++;
	}
	if (i >= YESNO_STR_MAX) {
		i = YESNO_STR_MAX;
		ans[YESNO_STR_MAX] = 0;
	}
	/* changes to rm.c version follow */
	if (i == 0)
		return (0);
	if (strncmp(nop, ans, i) == 0)
		return (0);
	if (strncmp(yesp, ans, i) == 0)
		return (1);
	return (-1);
}

/*ARGSUSED*/
static int
confirm_no(
	void *appdata_ptr,
	const char *message)
{
	return (0);
}

/*ARGSUSED*/
static int
confirm_yes(
	void *appdata_ptr,
	const char *message)
{
	return (1);
}

/*
 * Find base name of filename.
 */
static char *
basename(
	char *cp)
{
	char *sp;

	if ((sp = strrchr(cp, '/')) != NULL)
		return (sp + 1);
	return (cp);
}

/*ARGSUSED*/
static int
message_output(
	void *appdata_ptr,
	const char *message)
{
	(void) fprintf(stderr, "%s", message);
	return (CFGA_OK);

}

/*
 * extract_list_suboptions - process list option string
 */
static int
extract_list_suboptions(
	char *arg,
	char **sortpp,
	char **colspp,
	char **cols2pp,
	int *noheadingsp,
	char **delimpp,
	char **selectpp,
	char **matchpp)
{
	char *value = NULL;
	int subopt = 0;
	int err = 0;

	while (*arg != '\0') {
		static char need_value[] =
"%s: sub-option \"%s\" requires a value\n";
		static char no_value[] =
"%s: sub-option \"%s\" does not take a value\n";
		static char unk_subopt[] =
"%s: sub-option \"%s\" unknown\n";
		char **pptr;

		subopt = getsubopt(&arg, list_options, &value);
		switch (subopt) {
		case LIST_SORT:
			pptr = sortpp;
			goto valcom;
		case LIST_COLS:
			pptr = colspp;
			goto valcom;
		case LIST_COLS2:
			pptr = cols2pp;
			goto valcom;
		case LIST_SELECT:
			pptr = selectpp;
			goto valcom;
		case LIST_MATCH:
			pptr = matchpp;
			goto valcom;
		case LIST_DELIM:
			pptr = delimpp;
		valcom:
			if (value == NULL) {
				(void) fprintf(stderr, gettext(need_value),
				    cmdname, list_options[subopt]);
				err = 1;
			} else
				*pptr = value;
			break;
		case LIST_NOHEADINGS:
			if (value != NULL) {
				(void) fprintf(stderr, gettext(no_value),
				    cmdname, list_options[subopt]);
				err = 1;
			} else
				*noheadingsp = 1;
			break;
		default:
			(void) fprintf(stderr, gettext(unk_subopt),
			    cmdname, value);
			err = 1;
			break;
		}
	}
	return (err == 0);
}

static cfga_err_t
setup_prefilter(post_filter_t *post_filtp, char **prefilt_optpp)
{
	size_t len;
	const char *clopt = PREFILT_CLASS_STR;
	int idx;


	*prefilt_optpp = NULL;

	/* Get the index for the "class" field */
	for (idx = 0; idx < N_FIELDS; idx++) {
		if (strcmp(all_fields[idx].name, PREFILT_CLASS_STR) == 0)
			break;
	}

	/*
	 * Currently pre-filter available only for class fld w/ EXACT match
	 */
	if (idx >= N_FIELDS ||
	    post_filtp->match_type_p[idx] != CFGA_MATCH_EXACT) {
		return (CFGA_OK);
	}

	len = strlen(clopt) + strlen(post_filtp->ldata.ap_class) + 1;
	if ((*prefilt_optpp = config_calloc_check(1, len)) == NULL) {
		return (CFGA_LIB_ERROR);
	}

	(void) strcpy(*prefilt_optpp, clopt);
	(void) strcat(*prefilt_optpp, post_filtp->ldata.ap_class);

	/*
	 * Since it is being pre-filtered, this attribute does not need
	 * post-filtering.
	 */
	post_filtp->match_type_p[idx] = CFGA_MATCH_NOFILTER;
	if (all_fields[idx].set_filter != NULL) {
		(void) all_fields[idx].set_filter(&post_filtp->ldata, "");
	}

	return (CFGA_OK);
}

static cfga_err_t
set_attrval(
	const char *attr,
	const char *val,
	post_filter_t *post_filtp,
	match_type_t match_type)
{
	int fld = 0;
	cfga_err_t ret = CFGA_ERROR;

	for (fld = 0; fld < N_FIELDS; fld++) {
		if (strcmp(attr, all_fields[fld].name) == 0)
			break;
	}

	/* Valid field or is the select option supported for this field */
	if (fld >= N_FIELDS || all_fields[fld].set_filter == NULL) {
		return (CFGA_ATTR_INVAL);
	}

	if ((ret = all_fields[fld].set_filter(&post_filtp->ldata, val))
	    == CFGA_OK) {
		post_filtp->match_type_p[fld] = match_type;
	}

	return (ret);

}

static char inval_optarg[] =
	"%s: invalid value \"%s\" for %s suboption.\n";

/*
 * Parses the "select" string and fills in the post_filter structure
 */
static cfga_err_t
parse_select_opt(
	const char *selectp,
	post_filter_t *post_filtp,
	match_type_t match_type)
{
	parse_state_t state = CFGA_PSTATE_INIT;
	char *cp = NULL, *optstr = NULL, *attr = NULL, *val = NULL;
	int bal = 0;	/* Tracks balancing */
	char chr;
	cfga_err_t ret;


	if (selectp == NULL || post_filtp == NULL) {
		return (CFGA_ERROR);
	}

	optstr = config_calloc_check(1, strlen(selectp) + 1);
	if (optstr == NULL) {
		return (CFGA_LIB_ERROR);
	}

	(void) strcpy(optstr, selectp);

	/* Init */
	ret = CFGA_ATTR_INVAL;
	bal = 0;
	cp = attr = optstr;
	state = CFGA_PSTATE_INIT;

	for (; *cp != '\0'; cp++) {
		switch (state) {
		case CFGA_PSTATE_INIT:
			if (*cp != LEFT_PAREN)
				break;
			*cp = '\0';
			val = cp + 1;
			bal = 1;
			state = CFGA_PSTATE_ATTR_DONE;
			break;
		case CFGA_PSTATE_ATTR_DONE:
			chr = *cp;
			switch (chr) {
			case LEFT_PAREN:
				bal++;
				break;
			case RIGHT_PAREN:
				bal--;
				if (bal == 0) {
					*cp = '\0';
					state = CFGA_PSTATE_VAL_DONE;
				}
				break;
			}
			break;
		case CFGA_PSTATE_VAL_DONE:
			if (*cp != ':') {
				state = CFGA_PSTATE_ERR;
				goto out;
			}

			*cp = '\0';
			if (set_attrval(attr, val, post_filtp,
			    match_type) != CFGA_OK) {
				state = CFGA_PSTATE_ERR;
				goto out;
			}
			state = CFGA_PSTATE_INIT;
			attr = cp + 1;
			break;
		default:
			state = CFGA_PSTATE_ERR;
			/* FALLTHROUGH */
		case CFGA_PSTATE_ERR:
			goto out;
		}
	}

	/*FALLTHRU*/
out:
	if (state == CFGA_PSTATE_VAL_DONE) {
		ret = set_attrval(attr, val, post_filtp, match_type);
	} else {
		ret = CFGA_ATTR_INVAL;
	}

	if (ret != CFGA_OK) {
		(void) fprintf(stderr, gettext(inval_optarg), cmdname,
		    selectp, list_options[LIST_SELECT]);
	}

	S_FREE(optstr);
	return (ret);
}



static cfga_err_t
setup_filter(
	const char *selectp,
	const char *matchp,
	post_filter_t *post_filtp,
	char **prefilt_optpp)
{
	cfga_err_t ret = CFGA_ERROR;
	match_type_t match_type = CFGA_MATCH_NOFILTER;
	int i;

	static char match_needs_select[] =
	    "%s: %s suboption can only be used with %s suboption.\n";


	*prefilt_optpp = NULL;

	/*
	 * Initial: no filtering.
	 * CFGA_MATCH_NOFILTER is NOT a valid user input
	 */
	for (i = 0; i < N_FIELDS; i++) {
		post_filtp->match_type_p[i] = CFGA_MATCH_NOFILTER;
	}

	/* Determine type of match */
	if (matchp == NULL && selectp == NULL) {
		/* No filtering */
		return (CFGA_OK);
	} else if (matchp == NULL && selectp != NULL) {
		match_type = CFGA_DEFAULT_MATCH;
	} else if (matchp != NULL && selectp == NULL) {
		/* If only match specified, select criteria also needed */
		(void) fprintf(stderr, gettext(match_needs_select),
		    cmdname, list_options[LIST_MATCH],
		    list_options[LIST_SELECT]);
		return (CFGA_ERROR);
	} else {
		for (i = 0; i < N_MATCH_TYPES; i++) {
			if (strcmp(matchp, match_type_array[i].str) == 0) {
				match_type = match_type_array[i].type;
				break;
			}
		}
		if (i >= N_MATCH_TYPES) {
			(void) fprintf(stderr, gettext(inval_optarg), cmdname,
			    matchp, list_options[LIST_MATCH]);
			return (CFGA_ERROR);
		}
	}

	if ((ret = parse_select_opt(selectp, post_filtp, match_type))
	    != CFGA_OK) {
		return (ret);
	}

	/* Handle pre-filtering. */
	if ((ret = setup_prefilter(post_filtp, prefilt_optpp)) != CFGA_OK) {
		/* Cleanup */
		for (i = 0; i < N_FIELDS; i++) {
			post_filtp->match_type_p[i] = CFGA_MATCH_NOFILTER;
		}
		return (ret);
	}


	return (CFGA_OK);
}

/*
 * compare_ap_id - compare two ap_id's
 *
 * For partial matches, argument order is significant. The filtering criterion
 * should be the first argument.
 */

static int
compare_ap_id(
	cfga_list_data_t *p1,
	cfga_list_data_t *p2,
	match_type_t match_type)
{

	switch (match_type) {
		case CFGA_MATCH_NOFILTER:
			return (0);	/* No filtering. all pass */
		case CFGA_MATCH_PARTIAL:
			return (strncmp(p1->ap_log_id, p2->ap_log_id,
			    strlen(p1->ap_log_id)));
		case CFGA_MATCH_EXACT:
			return (strcmp(p1->ap_log_id, p2->ap_log_id));
		case CFGA_MATCH_ORDER:
		default:
			return (config_ap_id_cmp(p1->ap_log_id, p2->ap_log_id));
	}
}

/*
 * print_log_id - print logical ap_id
 */
static void
print_log_id(
	cfga_list_data_t *p,
	int width,
	char *lp)
{
	(void) sprintf(lp, "%-*.*s", width, sizeof (p->ap_log_id),
	    p->ap_log_id);
}

/*
 * set_log_flt - Setup filter for logical ap_id
 */
static cfga_err_t
set_log_flt(
	cfga_list_data_t *p,
	const char *val)
{
	if (strlen(val) > sizeof (p->ap_log_id) - 1)
		return (CFGA_ATTR_INVAL);

	(void) strcpy(p->ap_log_id, val);

	return (CFGA_OK);
}

/*
 * set_type_flt - Setup filter for type field
 */

static cfga_err_t
set_type_flt(
	cfga_list_data_t *p,
	const char *val)
{
	if (strlen(val) > sizeof (p->ap_type) - 1)
		return (CFGA_ATTR_INVAL);

	(void) strcpy(p->ap_type, val);

	return (CFGA_OK);
}

/*
 * set_class_flt - Setup filter for class field
 */
static cfga_err_t
set_class_flt(
	cfga_list_data_t *p,
	const char *val)
{
	if (strlen(val) > sizeof (p->ap_class) - 1)
		return (CFGA_ATTR_INVAL);

	(void) strcpy(p->ap_class, val);

	return (CFGA_OK);
}


/*
 * compare_r_state - compare receptacle state of two ap_id's
 */
static int
compare_r_state(
	cfga_list_data_t *p1,
	cfga_list_data_t *p2,
	match_type_t match_type)
{
	switch (match_type) {
	case CFGA_MATCH_NOFILTER:  /* no filtering. pass all */
		return (0);
	case CFGA_MATCH_ORDER:
	default:
		return (p1->ap_r_state - p2->ap_r_state);
	}
}

/*
 * compare_o_state - compare occupant state of two ap_id's
 */
static int
compare_o_state(
	cfga_list_data_t *p1,
	cfga_list_data_t *p2,
	match_type_t match_type)
{
	switch (match_type) {
	case CFGA_MATCH_NOFILTER:	/* no filtering. all pass */
		return (0);
	case CFGA_MATCH_ORDER:
	default:
		return (p1->ap_o_state - p2->ap_o_state);
	}
}

/*
 * compare_busy - compare busy field of two ap_id's
 */
static int
compare_busy(
	cfga_list_data_t *p1,
	cfga_list_data_t *p2,
	match_type_t match_type)
{

	switch (match_type) {
	case CFGA_MATCH_NOFILTER:	/* no filtering. all pass */
		return (0);
	case CFGA_MATCH_ORDER:
	default:
		return (p1->ap_busy - p2->ap_busy);
	}
}

/*
 * print_r_state - print receptacle state
 */
static void
print_r_state(
	cfga_list_data_t *p,
	int width,
	char *lp)
{
	char *cp;

	switch (p->ap_r_state) {
	case CFGA_STAT_EMPTY:
		cp = "empty";
		break;
	case CFGA_STAT_CONNECTED:
		cp = "connected";
		break;
	case CFGA_STAT_DISCONNECTED:
		cp = "disconnected";
		break;
	default:
		cp = "???";
		break;
	}
	(void) sprintf(lp, "%-*s", width, cp);
}

/*
 * print_o_state - print occupant state
 */
static void
print_o_state(
	cfga_list_data_t *p,
	int width,
	char *lp)
{
	char *cp;

	switch (p->ap_o_state) {
	case CFGA_STAT_UNCONFIGURED:
		cp = "unconfigured";
		break;
	case CFGA_STAT_CONFIGURED:
		cp = "configured";
		break;
	default:
		cp = "???";
		break;
	}
	(void) sprintf(lp, "%-*s", width, cp);
}

/*
 * compare_cond - compare condition field of two ap_id's
 */
static int
compare_cond(
	cfga_list_data_t *p1,
	cfga_list_data_t *p2,
	match_type_t match_type)
{

	switch (match_type) {
	case CFGA_MATCH_NOFILTER:
		return (0);
	case CFGA_MATCH_ORDER:
	default:
		return (p1->ap_cond - p2->ap_cond);
	}
}

/*
 * print_cond - print attachment point condition
 */
static void
print_cond(
	cfga_list_data_t *p,
	int width,
	char *lp)
{
	char *cp;

	switch (p->ap_cond) {
	case CFGA_COND_UNKNOWN:
		cp = "unknown";
		break;
	case CFGA_COND_UNUSABLE:
		cp = "unusable";
		break;
	case CFGA_COND_FAILING:
		cp = "failing";
		break;
	case CFGA_COND_FAILED:
		cp = "failed";
		break;
	case CFGA_COND_OK:
		cp = "ok";
		break;
	default:
		cp = "???";
		break;
	}
	(void) sprintf(lp, "%-*s", width, cp);
}

/*
 * compare_time - compare time field of two ap_id's
 */
static int
compare_time(
	cfga_list_data_t *p1,
	cfga_list_data_t *p2,
	match_type_t match_type)
{
	switch (match_type) {
	case CFGA_MATCH_NOFILTER:
		return (0);
	case CFGA_MATCH_ORDER:
	default:
		return (p1->ap_status_time - p2->ap_status_time);
	}
}


/*
 * print_time - print time from cfga_list_data.
 * Time print based on ls(1).
 */
static void
print_time(
	cfga_list_data_t *p,
	int width,
	char *lp)
{
	static time_t   year, now;
	time_t stime;
	char	time_buf[50];	/* array to hold day and time */

	if (year == 0) {
		now = time((long *)NULL);
		year = now - 6L*30L*24L*60L*60L; /* 6 months ago */
		now = now + 60;
	}
	stime = p->ap_status_time;
	if (stime == (time_t)-1) {
		(void) sprintf(lp, "%-*s", width, gettext("unavailable"));
		return;
	}

	if ((stime < year) || (stime > now)) {
		(void) strftime(time_buf, sizeof (time_buf),
		    dcgettext(NULL, FORMAT1, LC_TIME), localtime(&stime));
	} else {
		(void) strftime(time_buf, sizeof (time_buf),
		    dcgettext(NULL, FORMAT2, LC_TIME), localtime(&stime));
	}
	(void) sprintf(lp, "%-*s", width, time_buf);
}

/*
 * print_time_p - print time from cfga_list_data.
 */
static void
print_time_p(
	cfga_list_data_t *p,
	int width,
	char *lp)
{
	struct tm *tp;
	char tstr[TIME_P_WIDTH+1];

	tp = localtime(&p->ap_status_time);
	(void) sprintf(tstr, "%04d%02d%02d%02d%02d%02d", tp->tm_year + 1900,
	    tp->tm_mon + 1, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec);
	(void) sprintf(lp, "%-*s", width, tstr);
}

/*
 * compare_info - compare info from two cfga_list_data structs
 */
static int
compare_info(
	cfga_list_data_t *p1,
	cfga_list_data_t *p2,
	match_type_t match_type)
{
	switch (match_type) {
	case CFGA_MATCH_NOFILTER:
		return (0);
	case CFGA_MATCH_ORDER:
	default:
		return (strncmp(p1->ap_info, p2->ap_info,
		    sizeof (p2->ap_info)));
	}
}

/*
 * print_info - print info from cfga_list_data struct
 */
static void
print_info(
	cfga_list_data_t *p,
	int width,
	char *lp)
{
	(void) sprintf(lp, "%-*.*s", width, sizeof (p->ap_info), p->ap_info);
}

/*
 * compare_type - compare type from two cfga_list_data structs
 *
 * For partial matches, argument order is significant. The filtering criterion
 * should be the first argument.
 */
static int
compare_type(
	cfga_list_data_t *p1,
	cfga_list_data_t *p2,
	match_type_t match_type)
{
	switch (match_type) {
	case CFGA_MATCH_NOFILTER:
		return (0);
	case CFGA_MATCH_PARTIAL:
		return (strncmp(p1->ap_type, p2->ap_type, strlen(p1->ap_type)));
	case CFGA_MATCH_EXACT:
	case CFGA_MATCH_ORDER:
	default:
		return (strncmp(p1->ap_type, p2->ap_type,
		    sizeof (p2->ap_type)));
	}
}

/*
 * print_type - print type from cfga_list_data struct
 */
static void
print_type(
	cfga_list_data_t *p,
	int width,
	char *lp)
{
	(void) sprintf(lp, "%-*.*s", width, sizeof (p->ap_type), p->ap_type);
}


/*
 * compare_class - compare class from two cfga_list_data structs
 *
 * For partial matches, argument order is significant. The filtering criterion
 * should be the first argument.
 */
static int
compare_class(
	cfga_list_data_t *p1,
	cfga_list_data_t *p2,
	match_type_t match_type)
{

	switch (match_type) {
	case CFGA_MATCH_NOFILTER:
		return (0);
	case CFGA_MATCH_PARTIAL:
		return (strncmp(p1->ap_class, p2->ap_class,
		    strlen(p1->ap_class)));
	case CFGA_MATCH_EXACT:
	case CFGA_MATCH_ORDER:
	default:
		return (strncmp(p1->ap_class, p2->ap_class,
		    sizeof (p2->ap_class)));
	}
}

/*
 * print_class - print class from cfga_list_data struct
 */
static void
print_class(
	cfga_list_data_t *p,
	int width,
	char *lp)
{
	(void) sprintf(lp, "%-*.*s", width, sizeof (p->ap_class), p->ap_class);
}
/*
 * print_busy - print busy from cfga_list_data struct
 */
/* ARGSUSED */
static void
print_busy(
	cfga_list_data_t *p,
	int width,
	char *lp)
{
	if (p->ap_busy)
		(void) sprintf(lp, "%-*.*s", width, width, "y");
	else
		(void) sprintf(lp, "%-*.*s", width, width, "n");
}

/*
 * print_phys_id - print physical ap_id
 */
static void
print_phys_id(
	cfga_list_data_t *p,
	int width,
	char *lp)
{
	(void) sprintf(lp, "%-*.*s", width, sizeof (p->ap_phys_id),
	    p->ap_phys_id);
}


/*
 * find_field - find the named field
 */
static struct field_info *
find_field(char *fname)
{
	struct field_info *fldp;

	for (fldp = all_fields; fldp < &all_fields[N_FIELDS]; fldp++)
		if (strcmp(fname, fldp->name) == 0)
			return (fldp);
	return (NULL);
}

/*
 * usage_field - print field usage
 */
static void
usage_field()
{
	struct field_info *fldp = NULL;
	const char *sep;
	static char field_list[] = "%s: print or sort fields must be one of:";

	(void) fprintf(stderr, gettext(field_list), cmdname);
	sep = "";

	for (fldp = all_fields; fldp < &all_fields[N_FIELDS]; fldp++) {
		(void) fprintf(stderr, "%s %s", sep, fldp->name);
		sep = ",";
	}
	(void) fprintf(stderr, "\n");
}

/*
 * compare_null - null comparison routine
 */
/*ARGSUSED*/
static int
compare_null(
	cfga_list_data_t *p1,
	cfga_list_data_t *p2,
	match_type_t match_type)
{
	return (0);
}

/*
 * print_null - print out a field of spaces
 */
/*ARGSUSED*/
static void
print_null(
	cfga_list_data_t *p,
	int width,
	char *lp)
{
	(void) sprintf(lp, "%-*s", width, "");
}

/*
 * do_config_list - directs the output of the listing functions
 */
static int
do_config_list(
	int l_argc,
	char *l_argv[],
	cfga_list_data_t *statlist,
	int nlist,
	char *sortp,
	char *colsp,
	char *cols2p,
	int noheadings,
	char *delimp,
	post_filter_t *post_filtp,
	int dyn_exp)
{
	int nprcols = 0, ncols2 = 0;
	struct print_col *prnt_list = NULL;
	int napids_to_list = 0;
	FILE *fp = NULL;
	int f_err;
	cfga_list_data_t **sel_boards = NULL;
	int nsel = 0;
	int i, j;
	cfga_err_t ret;

	ap_arg_t *arg_array = NULL;
	ap_out_t *out_array = NULL;


	sort_list = NULL;
	f_err = 0;
	fp = stdout;
	nsort_list = count_fields(sortp, FDELIM);
	if (nsort_list != 0) {
		sort_list = config_calloc_check(nsort_list,
		    sizeof (*sort_list));
		if (sort_list == NULL) {
			ret = CFGA_LIB_ERROR;
			goto out;
		}
		f_err |= process_sort_fields(nsort_list, sort_list, sortp);
	} else
		sort_list = NULL;

	nprcols = count_fields(colsp, FDELIM);
	if ((ncols2 = count_fields(cols2p, FDELIM)) > nprcols)
		nprcols = ncols2;
	if (nprcols != 0) {
		prnt_list = config_calloc_check(nprcols, sizeof (*prnt_list));
		if (prnt_list == NULL) {
			ret = CFGA_LIB_ERROR;
			goto out;
		}
		f_err |= process_fields(nprcols, prnt_list, 0, colsp);
		if (ncols2 != 0)
			f_err |= process_fields(nprcols, prnt_list, 1, cols2p);
	} else
		prnt_list = NULL;

	if (f_err) {
		usage_field();
		ret = CFGA_ERROR;
		goto out;
	}

	/* Create an array of all user args (if any) */
	if (l_argc != 0) {
		int i, j;

		napids_to_list = 0;

		for (i = 0; i < l_argc; i++) {
			napids_to_list += count_fields(l_argv[i], ARG_DELIM);
		}

		arg_array = config_calloc_check(napids_to_list,
		    sizeof (*arg_array));
		if (arg_array == NULL) {
			ret = CFGA_LIB_ERROR;
			goto out;
		}

		for (i = 0, j = 0; i < l_argc; i++) {
			int n;

			n = count_fields(l_argv[i], ARG_DELIM);
			if (n == 0) {
				continue;
			} else if (n == 1) {
				arg_array[j].arg = l_argv[i];
				arg_array[j].resp = 0;
				j++;
			} else {
				char *cp, *ncp;

				cp = l_argv[i];
				for (;;) {
					arg_array[j].arg = cp;
					arg_array[j].resp = 0;
					j++;
					ncp = strchr(cp, ARG_DELIM);
					if (ncp == NULL)
						break;
					*ncp = '\0';
					cp = ncp + 1;
				}
			}
		}
		assert(j == napids_to_list);
	} else {
		napids_to_list = 0;
		arg_array = NULL;
	}

	assert(nlist != 0);

	out_array = config_calloc_check(nlist, sizeof (*out_array));
	if (out_array == NULL) {
		ret = CFGA_LIB_ERROR;
		goto out;
	}


	/* create a list of output stat data */
	for (i = 0; i < nlist; i++) {
		out_array[i].ldatap = &statlist[i];
		out_array[i].req = 0;
	}

	/*
	 * Mark all user input which got atleast 1 stat data in response
	 */
	for (i = 0; i < napids_to_list; i++) {
		arg_got_resp(&arg_array[i], out_array, nlist, dyn_exp);
	}

	/*
	 * Process output data
	 */
	nsel = 0;
	for (i = 0; i < nlist; i++) {
		/*
		 * Mark all the stats which were actually requested by user
		 */
		out_was_req(&out_array[i], arg_array, napids_to_list, 0);
		if (out_array[i].req == 0 && dyn_exp) {
			/*
			 * Try again without the dynamic component for the
			 * if dynamic expansion was requested.
			 */
			out_was_req(&out_array[i], arg_array,
			    napids_to_list, 1);
		}

		/*
		 * post filter data which was actually requested
		 */
		if (out_array[i].req == 1) {
			do_post_filter(&out_array[i], post_filtp, &nsel);
		}
	}

	sel_boards = config_calloc_check(nsel, sizeof (*sel_boards));
	if (sel_boards == NULL) {
		ret = CFGA_LIB_ERROR;
		goto out;
	}

	for (i = 0, j = 0; i < nlist; i++) {
		if (out_array[i].req == 1) {
			sel_boards[j] = out_array[i].ldatap;
			j++;
		}
	}

	assert(j == nsel);

	/*
	 * Print headings even if no list entries - Bug or feature ?
	 */
	if (!noheadings && prnt_list != NULL) {
		if ((ret = print_fields(nprcols, prnt_list, 1, 0,
		    delimp, NULL, fp)) != CFGA_OK) {
			goto out;
		}
		if (ncols2 != 0) {
			if ((ret = print_fields(nprcols, prnt_list, 1,
			    1, delimp, NULL, fp)) != CFGA_OK) {
				goto out;
			}
		}
	}

	if (nsel != 0) {
		if (sort_list != NULL && nsel > 1) {
			qsort(sel_boards, nsel, sizeof (sel_boards[0]),
			    ldata_compare);
		}

		if (prnt_list != NULL) {
			for (i = 0; i < nsel; i++) {
				if ((ret = print_fields(nprcols,
				    prnt_list, 0, 0, delimp, sel_boards[i], fp))
				    != CFGA_OK)
					goto out;
				if (ncols2 != 0) {
					if ((ret = print_fields(
					    nprcols, prnt_list, 0, 1, delimp,
					    sel_boards[i], fp)) != CFGA_OK)
						goto out;
				}
			}
		}
	}
	/*
	 * Go thru the argument list and notify user about args
	 * which did not have a match
	 */
	report_no_response(arg_array, napids_to_list);
	ret = CFGA_OK;
	/*FALLTHRU*/
out:
	S_FREE(sel_boards);
	S_FREE(arg_array);
	S_FREE(out_array);

	S_FREE(sort_list);
	S_FREE(prnt_list);

	return (ret);
}


/*
 * Mark all user inputs which got a response
 */
static void
arg_got_resp(ap_arg_t *inp, ap_out_t *out_array, int nouts, int dyn_exp)
{
	int i;
	cfga_ap_types_t type;


	if (nouts == 0) {
		return;
	}

	type = find_arg_type(inp->arg);

	/*
	 * Go through list of output stats and check if argument
	 * produced that output
	 */
	for (i = 0; i < nouts; i++) {
		if (type == PHYSICAL_AP_ID) {
			if (config_ap_id_cmp(out_array[i].ldatap->ap_phys_id,
			    inp->arg) == 0) {
				break;
			}
		} else if (type == LOGICAL_AP_ID) {
			if (config_ap_id_cmp(out_array[i].ldatap->ap_log_id,
			    inp->arg) == 0) {
				break;
			}
		} else if (type == AP_TYPE) {
			/*
			 * An AP_TYPE argument cannot generate dynamic
			 * attachment point stats unless dynamic expansion was
			 * requested by user.
			 */
			if (!dyn_exp && get_dyn(out_array[i].ldatap->ap_log_id)
			    != NULL) {
				continue;
			}

			if (strncmp(out_array[i].ldatap->ap_log_id, inp->arg,
			    strlen(inp->arg)) == 0) {
				break;
			}
		} else {
			return;
		}
	}

	if (i < nouts) {
		inp->resp = 1;
	}
}

/* Mark all stat data which were requested by user */
static void
out_was_req(ap_out_t *outp, ap_arg_t *in_array, int nargs, int no_dyn)
{
	int i;
	cfga_ap_types_t type = UNKNOWN_AP;
	char physid[MAXPATHLEN], logid[MAXPATHLEN];


	/* If no user args, all output is acceptable */
	if (nargs == 0) {
		outp->req = 1;
		return;
	}


	(void) snprintf(physid, sizeof (physid), "%s",
	    outp->ldatap->ap_phys_id);
	(void) snprintf(logid, sizeof (logid), "%s", outp->ldatap->ap_log_id);

	/*
	 * Do comparison with or without dynamic component as requested by
	 * user.
	 */
	if (no_dyn) {
		/* Remove the dynamic component */
		remove_dyn(physid);
		remove_dyn(logid);
	}

	for (i = 0; i < nargs; i++) {
		type = find_arg_type(in_array[i].arg);
		if (type == PHYSICAL_AP_ID) {

			if (config_ap_id_cmp(in_array[i].arg, physid) == 0) {
				break;
			}
		} else if (type == LOGICAL_AP_ID) {

			if (config_ap_id_cmp(in_array[i].arg, logid) == 0) {
				break;
			}
		} else if (type == AP_TYPE) {
			/*
			 * Aptypes cannot generate dynamic attachment
			 * points unless dynamic expansion is specified.
			 * in which case this routine would be called a
			 * 2nd time with the no_dyn flag set and there
			 * would be no dynamic ap_ids.
			 */
			if (get_dyn(logid) != NULL) {
				continue;
			}

			if (strncmp(in_array[i].arg, logid,
			    strlen(in_array[i].arg)) == 0) {
				break;
			}
		} else {
			continue;
		}
	}

	if (i < nargs) {
		/* Ok, this output was requested */
		outp->req = 1;
	}

}

static void
do_post_filter(ap_out_t *outp, post_filter_t *post_filtp, int *nselp)
{
	int i;

	if (outp->req != 1) {
		return;
	}

	/*
	 * For fields without filtering (CFGA_MATCH_NOFILTER),
	 * compare always returns 0 (success)
	 */
	for (i = 0; i < N_FIELDS; i++) {
		/*
		 * Note: Order is important for partial match (via strncmp).
		 * The first argument for compare must be the filter.
		 */
		if (all_fields[i].compare(&post_filtp->ldata, outp->ldatap,
		    post_filtp->match_type_p[i])) {
			outp->req = 0;	/* Blocked by filter */
			return;
		}
	}

	/*
	 * Passed through filter
	 */
	(*nselp)++;
}

static void
report_no_response(ap_arg_t *arg_array, int nargs)
{
	int i;

	if (nargs == 0) {
		return;
	}


	/*
	 * nop if no user arguments
	 */
	for (i = 0; i < nargs; i++) {
		if (arg_array[i].resp == 0) {
			(void) fprintf(stderr,
			    gettext("%s: No matching library found\n"),
			    arg_array[i].arg);
		}
	}
}

/*
 * ldata_compare - compare two attachment point list data structures.
 */
static int
ldata_compare(
	const void *vb1,
	const void *vb2)
{
	int i;
	int res = -1;
	cfga_list_data_t *b1, *b2;


	b1 = *(cfga_list_data_t **)vb1;
	b2 = *(cfga_list_data_t **)vb2;

	for (i = 0; i < nsort_list; i++) {
		res = (*(sort_list[i].fld->compare))(b1, b2, CFGA_MATCH_ORDER);
		if (res != 0) {
			if (sort_list[i].reverse)
				res = -res;
			break;
		}
	}

	return (res);
}

/*
 * count_fields - Count the number of fields, using supplied delimiter.
 */
static int
count_fields(char *fspec, char delim)
{
	char *cp = NULL;
	int n;

	if (fspec == 0 || *fspec == '\0')
		return (0);
	n = 1;
	for (cp = fspec; *cp != '\0'; cp++)
		if (*cp == delim)
			n++;
	return (n);
}

/*
 * get_field
 * This function is not a re-implementation of strtok().
 * There can be null fields - strtok() eats spans of delimiters.
 */
static char *
get_field(char **fspp)
{
	char *cp = NULL, *fld;

	fld = *fspp;

	if (fld != NULL && *fld == '\0')
		fld = NULL;

	if (fld != NULL) {
		cp = strchr(*fspp, FDELIM);
		if (cp == NULL) {
			*fspp = NULL;
		} else {
			*cp = '\0';
			*fspp = cp + 1;
			if (*fld == '\0')
				fld = NULL;
		}
	}
	return (fld);
}

/*
 * process_fields -
 */
static int
process_fields(
	int ncol,
	struct print_col *list,
	int line2,
	char *fmt)
{
	struct print_col *pp = NULL;
	struct field_info *fldp = NULL;
	char *fmtx;
	char *fldn;
	int err;

	err = 0;
	fmtx = fmt;
	for (pp = list; pp < &list[ncol]; pp++) {
		fldn = get_field(&fmtx);
		fldp = &null_field;
		if (fldn != NULL) {
			struct field_info *tfldp;

			tfldp = find_field(fldn);
			if (tfldp != NULL) {
				fldp = tfldp;
			} else {
				(void) fprintf(stderr, gettext(unk_field),
				    cmdname, fldn);
				err = 1;
			}
		}
		if (line2) {
			pp->line2 = fldp;
			if (fldp->width > pp->width)
				pp->width = fldp->width;
		} else {
			pp->line1 = fldp;
			pp->width = fldp->width;
		}
	}
	return (err);
}

/*
 * process_sort_fields -
 */
static int
process_sort_fields(
	int nsort,
	struct sort_el *list,
	char *fmt)
{
	int i;
	int rev;
	struct field_info *fldp = NULL;
	char *fmtx;
	char *fldn;
	int err;

	err = 0;
	fmtx = fmt;
	for (i = 0; i < nsort; i++) {
		fldn = get_field(&fmtx);
		fldp = &null_field;
		rev = 0;
		if (fldn != NULL) {
			struct field_info *tfldp = NULL;

			if (*fldn == '-') {
				rev = 1;
				fldn++;
			}
			tfldp = find_field(fldn);
			if (tfldp != NULL) {
				fldp = tfldp;
			} else {
				(void) fprintf(stderr, gettext(unk_field),
				    cmdname, fldn);
				err = 1;
			}
		}
		list[i].reverse = rev;
		list[i].fld = fldp;
	}
	return (err);
}

/*
 * print_fields -
 */
static cfga_err_t
print_fields(
	int ncol,
	struct print_col *list,
	int heading,
	int line2,
	char *delim,
	cfga_list_data_t *bdp,
	FILE *fp)
{
	char *del = NULL;
	struct print_col *pp = NULL;
	struct field_info *fldp = NULL;
	static char *outline, *end;
	char *lp;

	if (outline == NULL) {
		int out_len, delim_len;

		delim_len = strlen(delim);
		out_len = 0;
		for (pp = list; pp < &list[ncol]; pp++) {
			out_len += pp->width;
			out_len += delim_len;
		}
		out_len -= delim_len;
		outline = config_calloc_check(out_len + 1, 1);
		if (outline == NULL) {
			return (CFGA_LIB_ERROR);
		}
		end = &outline[out_len + 1];
	}

	lp = outline;
	del = "";
	for (pp = list; pp < &list[ncol]; pp++) {
		fldp = line2 ? pp->line2 : pp->line1;
		(void) snprintf(lp, end - lp, "%s", del);
		lp += strlen(lp);
		if (heading) {
			(void) snprintf(lp, end - lp, "%-*s",
			    fldp->width, fldp->heading);
		} else {
			(*fldp->printfn)(bdp, fldp->width, lp);
		}
		lp += strlen(lp);
		del = delim;
	}

	/*
	 * Trim trailing spaces
	 */
	while (--lp >= outline && *lp == ' ')
		*lp = '\0';
	(void) fprintf(fp, "%s\n", outline);
	return (CFGA_OK);
}

/*
 * config_calloc_check - perform allocation, check result and
 * set error indicator
 */
static void *
config_calloc_check(
	size_t nelem,
	size_t elsize)
{
	void *p;
	static char alloc_fail[] =
"%s: memory allocation failed (%d*%d bytes)\n";


	p = calloc(nelem, elsize);
	if (p == NULL) {
		(void) fprintf(stderr, gettext(alloc_fail), cmdname,
		    nelem, elsize);
	}
	return (p);
}

/*
 * find_arg_type - determine if an argument is an ap_id or an ap_type.
 */
static cfga_ap_types_t
find_arg_type(const char *ap_id)
{
	struct stat sbuf;
	cfga_ap_types_t type;
	char *mkr = NULL, *cp;
	int size_ap = 0, size_mkr = 0, digit = 0, i = 0;
	char path[MAXPATHLEN];
	char apbuf[MAXPATHLEN];
	size_t len;


	/*
	 * sanity checks
	 */
	if (ap_id == NULL || *ap_id == '\0') {
		return (UNKNOWN_AP);
	}

	/*
	 * Mask the dynamic component if any
	 */
	if ((cp = GET_DYN(ap_id)) != NULL) {
		len = cp - ap_id;
	} else {
		len = strlen(ap_id);
	}

	if (len >= sizeof (apbuf)) {
		return (UNKNOWN_AP);
	}

	(void) strncpy(apbuf, ap_id, len);
	apbuf[len] = '\0';

	/*
	 * If it starts with a slash and is stat-able
	 * its a physical.
	 */
	if (*apbuf == '/' && stat(apbuf, &sbuf) == 0) {
		return (PHYSICAL_AP_ID);
	}

	/*
	 * Is this a symlink in CFGA_DEV_DIR ?
	 */
	(void) snprintf(path, sizeof (path), "%s/%s", CFGA_DEV_DIR, apbuf);

	if (lstat(path, &sbuf) == 0 && S_ISLNK(sbuf.st_mode) &&
	    stat(path, &sbuf) == 0) {
		return (LOGICAL_AP_ID);
	}

	/*
	 * Check for ":" which is always present in an ap_id but not maybe
	 * present or absent in an ap_type.
	 * We need to check that the characters right before the : are digits
	 * since an ap_id is of the form <name><instance>:<specific ap name>
	 */
	if ((mkr = strchr(apbuf, ':')) == NULL)  {
		type = AP_TYPE;
	} else {
		size_ap = strlen(apbuf);
		size_mkr = strlen(mkr);
		mkr = apbuf;

		digit = 0;
		for (i = size_ap - size_mkr - 1;  i > 0; i--) {
			if ((int)isdigit(mkr[i])) {
				digit++;
				break;
			}
		}
		if (digit == 0) {
			type = AP_TYPE;
		} else {
			type = LOGICAL_AP_ID;
		}
	}

	return (type);
}


static char *
get_dyn(const char *ap_id)
{
	if (ap_id == NULL) {
		return (NULL);
	}

	return (strstr(ap_id, CFGA_DYN_SEP));
}

/*
 * removes the dynamic component
 */
static void
remove_dyn(char *ap_id)
{
	char *cp;

	if (ap_id == NULL) {
		return;
	}

	cp = strstr(ap_id, CFGA_DYN_SEP);
	if (cp != NULL) {
		*cp = '\0';
	}
}


static char *
s_strdup(char *str)
{
	char *dup;

	/*
	 * sometimes NULL strings may be passed in (see DEF_COLS2). This
	 * is not an error.
	 */
	if (str == NULL) {
		return (NULL);
	}

	dup = strdup(str);
	if (dup == NULL) {
		(void) fprintf(stderr,
		    "%s \"%s\"\n", gettext("Cannot copy string"), str);
		return (NULL);
	}

	return (dup);
}