|
root / base / usr / src / cmd / pools / poolcfg
poolcfg Plain Text 2029 lines 49.3 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
#
# 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 2007 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Copyright (c) 2018, Joyent, Inc.

PROG =		poolcfg
OBJS =		poolcfg_lex.o poolcfg_grammar.o
SRCS =		$(OBJS:%.o=%.c) $(COMMON_SRCS)
POFILES =	$(OBJS:.o=.po)
POFILE =	poolcfg.po

include ../../Makefile.cmd
include ../Makefile.pools

# poolcf has a name clash with main() and libl.so.1.  However, poolcfg must
# still export a number of "yy*" (libl) interfaces.  Reduce all other symbols
# to local scope.
MAPFILES +=	$(MAPFILE.LEX) $(MAPFILE.NGB)
MAPOPTS =	$(MAPFILES:%=-Wl,-M%)

LFLAGS =	-t
YFLAGS =	-d
CFLAGS +=	-I$(POOLSCOMMONDIR)
LDFLAGS +=	$(MAPOPTS)
LDLIBS +=	-lpool -ll
XGETFLAGS =	-a
CLOBBERFILES +=	poolcfg_lex.c poolcfg_grammar.c poolcfg_grammar.h

# not linted
SMATCH=off

.KEEP_STATE:

all: $(PROG)

$(PROG): $(OBJS) $(COMMON_OBJS) $(MAPFILES)
	$(LINK.c) -o $@ $(OBJS) $(COMMON_OBJS) $(LDLIBS)
	$(POST_PROCESS)

%.o : $(POOLSCOMMONDIR)/%.c
	$(COMPILE.c) -o $@ $<
	$(POST_PROCESS_O)

install: all $(ROOTUSRSBINPROG)

poolcfg_lex.c: poolcfg.l poolcfg_grammar.h
	$(LEX) $(LFLAGS) poolcfg.l > $@

poolcfg_grammar.h poolcfg_grammar.c: poolcfg.y
	$(YACC) $(YFLAGS) poolcfg.y
	@$(MV) y.tab.h poolcfg_grammar.h
	@$(MV) y.tab.c poolcfg_grammar.c

poolcfg_lex.o poolcfg_grammar.o : CCVERBOSE =
# because of labels from yacc
poolcfg_lex.o poolcfg_grammar.o : CERRWARN += -Wno-unused-label

clean:
	$(RM) $(OBJS) $(COMMON_OBJS) $(POFILES)

$(POFILE):	$(POFILES)
	$(RM) $@; cat $(POFILES) > $@

include ../../Makefile.targ
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (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 2003 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef	_POOLCFG_H
#define	_POOLCFG_H

#include <sys/types.h>

#ifdef	__cplusplus
extern "C" {
#endif

typedef enum {
	po_create,
	po_remove
} prop_op_t;

typedef union {
	uint64_t	u;
	int64_t		i;
	double		d;
	uchar_t		b;
	const char	*s;
} pv_u;

typedef struct prop {
	const char *prop_name;
	pool_value_t *prop_value;
	prop_op_t prop_op;
	struct prop *prop_next;
} prop_t;

typedef struct assoc {
	int assoc_type;
	const char *assoc_name;
	struct assoc *assoc_next;
} assoc_t;

typedef struct cmd {
	void (*cmd)(struct cmd *);
	const char *cmd_tgt1;
	const char *cmd_tgt2;
	uint64_t cmd_qty;
	prop_t *cmd_prop_list;
	assoc_t *cmd_assoc_list;
} cmd_t;

typedef void (*cmdfunc)(cmd_t *);

#ifdef	__cplusplus
}
#endif

#endif	/* _POOLCFG_H */
%{
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (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 2003 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * poolcfg.l
 *
 * Overview
 * poolcfg.l implements a lexer for the poolcfg(1) utility.The lexer uses
 * the token definitions generated by YACC in the file poolcfg_Grammar.h.
 * To make token recognition simpler, the lexer uses a separate state for
 * each of the different data types supported by poolcfg(1).
 *
 * States
 * Lex provides the ability to minimise conflict between qualifying regexps
 * by providing states. A regexp that is qualified by a state will only be
 * used when the state is entered (using the BEGIN <state> command). (The
 * exception to this rule, is that rules defined in the default state are
 * available in all states.)
 *
 * poolcfg.l makes use of type states, one for each of the poolcfg(1)
 * supported data types:
 *
 * ISTATE => int64_t
 * USTATE => uint64_t
 * BSTATE => uchar_t
 * SSTATE => const char *
 * DSTATE => double
 *
 * and a further state, CPUSTATE, to indicate the difference between matching
 * a valid "name" (i.e. id) for a cpu and a valid name for other components of
 * libpool.
 *
 * When a token indicating a variable declaration is matched, the corresponding
 * state is saved in the state variable. Once the assignment ('=') token is
 * matched, the stored state is entered and the additional state specific
 * matching regular expressions become available. Once a state specific
 * token is matched, the default state is restored.
 *
 */
#include <stdlib.h>
#include <sys/types.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <libintl.h>

#include <pool.h>
#include "utils.h"
#include "poolcfg.h"
#include "poolcfg_grammar.h"

static int lex_lineno = 1;		/* line-number for error reporting */
static int state = INITIAL;		/* state to be used */
extern int yyerror(const char *s);
extern int dofile;			/* are we processing a file? */
%}

%s ISTATE
%s USTATE
%s BSTATE
%s SSTATE
%s DSTATE
%s CPUSTATE

%%

\n			lex_lineno++;

[ \t]+			;

#.*			;

info			{ return PCC_INFO; }

create			{ return PCC_CREATE; }

destroy			{ return PCC_DESTROY; }

modify			{ return PCC_MODIFY; }

associate		{ return PCC_ASSOC; }

transfer		{
				BEGIN USTATE;
				return PCC_TRANSFER;
			}

discover		{ return PCC_DISC; }

rename			{ return PCC_RENAME; }

to			{ return PCK_TO; }

from			{ return PCK_FROM; }

int			{
				state=ISTATE;
				return PCT_INT;
			}

uint			{
				state=USTATE;
				return PCT_UINT;
			}

boolean			{
				state=BSTATE;
				return PCT_BOOLEAN;
			}

string			{
				state=SSTATE;
				return PCT_STRING;
			}

float			{
				state=DSTATE;
				return PCT_FLOAT;
			}

cpu			{
				BEGIN CPUSTATE;
				return PCE_CPU;
			}

pset			{ return PCE_PSET; }

pool			{ return PCE_POOL; }

system			{ return PCE_SYSTEM; }

\(			{ return PCK_OPENLST; }

\)			{ return PCK_CLOSELST; }

=			{
				BEGIN state;
				return PCK_ASSIGN;
			}

\;			{ return PCK_SEPLST; }

~			{ return PCK_UNDEF; }

<ISTATE>-?[0-9]+	{
				yylval.ival = strtoll(yytext, NULL, 0);
				if (errno == EINVAL || errno == ERANGE) {
					(void) yyerror(gettext(
					    "Invalid value"));
					exit(E_ERROR);
				}
				BEGIN INITIAL;
				return PCV_VAL_INT;
			}

<USTATE>[0-9]+		{
				yylval.uval = strtoull(yytext, NULL, 0);
				if (errno == EINVAL || errno == ERANGE) {
					(void) yyerror(gettext(
					    "Invalid value"));
					exit(E_ERROR);
				}
				BEGIN INITIAL;
				return PCV_VAL_UINT;
			}


<BSTATE>true|false	{
				if (strcmp(yytext, "true") == 0)
					yylval.bval = 1;
				else
					yylval.bval = 0;
				BEGIN INITIAL;
				return PCV_VAL_BOOLEAN;
			}

<SSTATE>\"[^\"\n]*[\"\n] {
				if((yylval.sval = strdup(yytext+1)) == NULL) {
					(void) yyerror(gettext(
					    "Out of memory"));
					exit(E_ERROR);
				}
				if (yylval.sval[yyleng-2] =='"')
					yylval.sval[yyleng-2] = 0;
				BEGIN INITIAL;
				return PCV_VAL_STRING;
			}

<DSTATE>([0-9]+|([0-9]*\.[0-9]+)([eE][-+]?[0-9]+)?) {
				yylval.dval = strtod(yytext, (char **)NULL);
				if (errno == EINVAL || errno == ERANGE) {
					(void) yyerror(gettext(
					    "Invalid value"));
					exit(E_ERROR);
				}
				BEGIN INITIAL;
				return PCV_VAL_FLOAT;
			}

[A-Za-z][A-Za-z0-9,._-]*	{
				if ((yylval.sval = strdup(yytext)) == NULL) {
					(void) yyerror(gettext(
					    "Out of memory"));
					exit(E_ERROR);
				}
				return PCV_SYMBOL;
			}

<CPUSTATE>[0-9]+	{
				if ((yylval.sval = strdup(yytext)) == NULL) {
					(void) yyerror(gettext(
					    "Out of memory"));
					exit(E_ERROR);
				}
				BEGIN INITIAL;
				return PCV_SYMBOL;
			}
.			{
				(void) yyerror(gettext("Illegal character"));
				exit(E_ERROR);
			}

%%

int
yyerror(const char *s)
{
	if (dofile == PO_TRUE) {
		if (yytext[0] == '\0') {
			(void) warn(gettext("line %d, %s, token expected\n"),
			    lex_lineno, s);
			return (0);
		}
		(void) warn(gettext("line %d, %s at '%s'\n"), lex_lineno, s,
		    yytext);
	} else {
		if (yytext[0] == '\0') {
			(void) warn(gettext("%s, token expected\n"), s);
			return (0);
		}
		(void) warn(gettext("%s at '%s'\n"), s, yytext);
	}
	return (0);
}
%{
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (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 2005 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Overview of poolcfg(1)
 *
 * poolcfg(1) implements a small grammar for manipulating pools configurations.
 * yacc(1) is used to generate the parser and poolcfg.l contains a simple lexer
 * (generted by lex(1)) to perform lexical processsing of the input.
 *
 * Refer to the poolcfg(1) manpage for more details of the grammar.
 *
 * The parser is designed so that all operations implement the same interface.
 * This allows the parser to simply build up the command (using the cmd
 * variable) by storing arguments and a pointer to the desired function in the
 * cmd. The command is executed when the commands production is matched.
 *
 * Properties and associations are stored in simple linked lists and processed
 * in the order submitted by the user.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <locale.h>
#include <libintl.h>
#include <sys/utsname.h>

#include <pool.h>
#include "utils.h"
#include "poolcfg.h"



#define	USAGE1	\
"Usage:\n" \
"%s -h\n" \
"%s -c command [ -d | [ file ] ]\n" \
"%s -f command-file [-d | [ file ] ]\n\n"

#define	USAGE2	\
"command:\n" \
"  info [entity name]\n" \
"         display configuration (or specified portion) in readable form\n" \
"  create entity name [property-list]\n" \
"         make an entity of the specified type and name\n" \
"  destroy entity name\n" \
"         remove the specified entity\n" \
"  modify entity name [property-list]\n" \
"         change the listed properties on the named entity\n" \
"  associate pool name [resource-list]\n" \
"         connect one or more resources to a pool, or replace one or more\n" \
"         existing connections\n" \
"  transfer to resource name [component-list]\n" \
"         transfer one or more discreet components to a resource\n" \
"  transfer [quantity] from resource src to tgt\n" \
"         transfer a resource quantity from src to tgt\n" \
"  transfer [quantity] to resource tgt from src\n" \
"         transfer a resource quantity to tgt from src\n" \
"  discover\n" \
"         create a system entity, with one pool entity and resources to\n" \
"         match current system configuration\n" \
"  rename entity old_name to new_name\n" \
"         change the name of the entity on the system to its new name\n\n" \
"property-list:\n" \
"  ( proptype name = value [ ; proptype name = value ]* )\n" \
"         where multiple definitions in the sentence for a given\n" \
"         proptype, name pair are ignored; the last one provided is used.\n" \
"         For property deletion, use \"~ proptype name\"\n\n" \
"resource-list:\n" \
"  ( resource name [; resource name ] )\n" \
"         where multiple uses of a resource are ignored; the last provided\n" \
"         is the one used.\n" \
"         There is no deletion syntax for resource lists.\n" \
"component-list:\n" \
"  ( cpu id [; cpu id ] )\n" \
"         where multiple uses of the same component cause the last provided\n" \
"         to be the one used.\n" \
"         There is no deletion syntax for component lists.\n" \
"entity:\n" \
"  system | pool | pset | cpu\n" \
"         where cpu is only valid for transfer, info and modify commands.\n" \
"resource:\n" \
"  pset\n\n" \
"proptype:\n" \
"  boolean | int | uint | string | float\n\n"

int dofile = PO_FALSE;			/* poolcfg.l uses this for errors */
int conf_edit_error = POE_OK;		/* cached error for error reporting */
int conf_edit_errno = 0;		/* cached errno for error reporting */
int conf_list_error = POE_OK;		/* cached error for error reporting */
int conf_list_errno = 0;		/* cached errno for error reporting */
static const char cmdname[] = "poolcfg";
static const char cmd_options[] = "c:df:h";
static void usage(int);
static const char *max_suffix = ".max";
static const char *min_suffix = ".min";

static const char *conf_file = NULL;	/* Location of target config */
static cmd_t *cmd = NULL;		/* Command being processed */
static pool_conf_t *conf = NULL;	/* Config to be processed */
static int edited = PO_FALSE;		/* Has the configuration been changed */

/* yacc externals */
extern FILE *yyin;
extern int yydebug;
extern int yyerror(const char *s);

/* Utility functions */
static void arg_parse(const char *);
static void file_parse(const char *);
static cmd_t *alloc_cmd(void);
static prop_t *alloc_prop(prop_op_t);
static assoc_t *alloc_assoc(int, const char *);
static void free_cmd(cmd_t *);
static void check_conf_name(cmd_t *);
static void prop_list_walk(cmd_t *, pool_elem_t *);
static void assoc_list_walk(cmd_t *, pool_t *);
static void transfer_list_walk(cmd_t *, pool_resource_t *);
static void terminate(void);
static pool_component_t *get_cpu(const char *);
static void process_min_max(pool_resource_t *);

/* Info Commands */
static void parser_conf_info(cmd_t *);
static void parser_pool_info(cmd_t *);
static void parser_resource_info(cmd_t *, const char *);
static void parser_pset_info(cmd_t *);
static void parser_cpu_info(cmd_t *);

/* Create Commands */
static void parser_conf_create(cmd_t *);
static void parser_pool_create(cmd_t *);
static void parser_resource_create(cmd_t *, const char *);
static void parser_pset_create(cmd_t *);

/* Destroy Commands */
static void parser_conf_destroy(cmd_t *);
static void parser_pool_destroy(cmd_t *);
static void parser_resource_destroy(cmd_t *, const char *);
static void parser_pset_destroy(cmd_t *);

/* Modify Commands */
static void parser_conf_modify(cmd_t *);
static void parser_pool_modify(cmd_t *);
static void parser_resource_modify(cmd_t *, const char *);
static void parser_pset_modify(cmd_t *);
static void parser_cpu_modify(cmd_t *);

/* Associate Commands */
static void parser_pool_associate(cmd_t *);

/* Assign Commands */
static void parser_resource_xtransfer(cmd_t *);
static void parser_resource_transfer(cmd_t *);

/* Discover Commands */
static void parser_conf_discover(cmd_t *);

/* Rename Commands */
static void parser_rename(cmd_t *, pool_elem_t *, const char *);
static void parser_conf_rename(cmd_t *);
static void parser_pool_rename(cmd_t *);
static void parser_pset_rename(cmd_t *);


%}

%union {
	double dval;
	uint64_t uval;
	int64_t ival;
	char *sval;
	uchar_t bval;
	cmd_t *cmd;
	prop_t *prop;
	pv_u val;
	assoc_t *assoc;
}

%start commands

%token PCC_INFO PCC_CREATE PCC_DESTROY PCC_MODIFY PCC_ASSOC PCC_DISC PCC_RENAME
%token PCC_TRANSFER
%token PCK_FROM PCK_TO PCK_OPENLST PCK_CLOSELST PCK_SEPLST PCK_ASSIGN PCK_UNDEF
PCK_COMMAND
%token PCV_FILENAME PCV_SYMBOL PCV_VAL_INT PCV_VAL_UINT PCV_VAL_FLOAT
PCV_VAL_STRING PCV_VAL_BOOLEAN
%token PCT_INT PCT_UINT PCT_BOOLEAN PCT_FLOAT PCT_STRING
%token PCE_SYSTEM PCE_POOL PCE_PSET PCE_CPU

%type <ival> PCV_VAL_INT
%type <uval> PCV_VAL_UINT
%type <bval> PCV_VAL_BOOLEAN
%type <dval> PCV_VAL_FLOAT
%type <sval> PCV_VAL_STRING
%type <sval> PCV_SYMBOL
%type <sval> PCV_FILENAME

%type <ival> PCC_INFO
%type <ival> PCE_SYSTEM PCE_POOL PCE_PSET PCE_CPU
%type <ival> entity proptype info_entity modify_entity
%type <sval> name src tgt
%type <cmd> command
%type <cmd> list_command info_command edit_command create_command
destroy_command modify_command associate_command discover_command
rename_command transfer_command transfer_qty transfer_components
%type <prop> prop_remove prop_assign prop_op prop_ops property_list
%type <assoc> resource_assign resource_assigns resource_list
%type <assoc> component_assign component_assigns component_list
%type <val> value
%type <ival> resource component

%%

commands: command
	{
		if ($1->cmd != NULL)
			$1->cmd($1);
		free_cmd($1);
	}
	| commands command
	{
		if ($2->cmd != NULL)
			$2->cmd($2);
		free_cmd($2);
	}
	| command error { YYERROR;};

command: list_command
	| edit_command
	{
		if (conf_edit_error != POE_OK) {
			if ($1->cmd != parser_conf_create &&
			    $1->cmd != parser_conf_discover) {
				die(gettext(ERR_CONF_LOAD), conf_file,
				    get_errstr_err(conf_edit_error,
				        conf_edit_errno));
			}
		}
		edited = PO_TRUE;
	};

list_command: info_command
	{
		if (conf_list_error != POE_OK) {
			if ($1->cmd != parser_conf_create &&
			    $1->cmd != parser_conf_discover) {
				die(gettext(ERR_CONF_LOAD), conf_file,
				    get_errstr_err(conf_list_error,
				        conf_list_errno));
			}
		}
	}
	| discover_command {conf_list_error = conf_edit_error = POE_OK;};

edit_command: create_command
	| destroy_command
	| modify_command
	| associate_command
	| transfer_command
	| rename_command;

info_command: PCC_INFO
	{
		if (($$ = alloc_cmd()) == NULL)
			YYERROR;
		cmd = $$;
		$$->cmd = &parser_conf_info;
	}
	| PCC_INFO info_entity name
	{
		if (($$ = alloc_cmd()) == NULL)
			YYERROR;
		cmd = $$;
		switch ($2) {
		case PCE_SYSTEM:
			$$->cmd = &parser_conf_info;
			break;
		case PCE_POOL:
			$$->cmd = &parser_pool_info;
			break;
		case PCE_PSET:
			$$->cmd = &parser_pset_info;
			break;
		case PCE_CPU:
			$$->cmd = &parser_cpu_info;
			break;
		default:
			warn(gettext(ERR_UNKNOWN_ENTITY), $2);
			YYERROR;
		}
		$$->cmd_tgt1 = $3;
	};

create_command: PCC_CREATE entity name
	{
		if (($$ = alloc_cmd()) == NULL)
			YYERROR;
		cmd = $$;
		switch ($2) {
		case PCE_SYSTEM:
			$$->cmd = &parser_conf_create;
			/*
			 * When creating a new system element, ensure
			 * pre-existing errors are ignored.
			 */
			conf_list_error = conf_edit_error = POE_OK;
			break;
		case PCE_POOL:
			$$->cmd = &parser_pool_create;
			break;
		case PCE_PSET:
			$$->cmd = &parser_pset_create;
			break;
		default:
			warn(gettext(ERR_UNKNOWN_ENTITY), $2);
			YYERROR;
		}
		$$->cmd_tgt1 = $3;
	}
	| create_command property_list;

destroy_command: PCC_DESTROY entity name
	{
		if (($$ = alloc_cmd()) == NULL)
			YYERROR;
		cmd = $$;
		switch ($2) {
		case PCE_SYSTEM:
			$$->cmd = &parser_conf_destroy;
			break;
		case PCE_POOL:
			$$->cmd = &parser_pool_destroy;
			break;
		case PCE_PSET:
			$$->cmd = &parser_pset_destroy;
			break;
		default:
			warn(gettext(ERR_UNKNOWN_ENTITY), $2);
			YYERROR;
		}
		$$->cmd_tgt1 = $3;
	};

modify_command: PCC_MODIFY modify_entity name
	{
		if (($$ = alloc_cmd()) == NULL)
			YYERROR;
		cmd = $$;
		switch ($2) {
		case PCE_SYSTEM:
			$$->cmd = &parser_conf_modify;
			break;
		case PCE_POOL:
			$$->cmd = &parser_pool_modify;
			break;
		case PCE_PSET:
			$$->cmd = &parser_pset_modify;
			break;
		case PCE_CPU:
			$$->cmd = &parser_cpu_modify;
			break;
		default:
			warn(gettext(ERR_UNKNOWN_ENTITY), $2);
			YYERROR;
		}
		$$->cmd_tgt1 = $3;
	}
	| modify_command property_list;

associate_command: PCC_ASSOC PCE_POOL name
	{
		if (($$ = alloc_cmd()) == NULL)
			YYERROR;
		cmd = $$;
		$$->cmd = &parser_pool_associate;
		cmd->cmd_tgt1 = $3;
	}
	| associate_command resource_list;

transfer_command: transfer_qty
	| transfer_components;

transfer_components: PCC_TRANSFER PCK_TO PCE_PSET name
	{
		if (($$ = alloc_cmd()) == NULL)
			YYERROR;
		cmd = $$;
		$$->cmd = &parser_resource_xtransfer;
		cmd->cmd_tgt1 = $4;
	}
	| transfer_components component_list;

transfer_qty: PCC_TRANSFER PCV_VAL_UINT PCK_FROM PCE_PSET src PCK_TO tgt
	{
		if (($$ = alloc_cmd()) == NULL)
			YYERROR;
		cmd = $$;
		$$->cmd = &parser_resource_transfer;
		cmd->cmd_tgt1 = $5;
		cmd->cmd_tgt2 = $7;
		cmd->cmd_qty = $2;
	}
	| PCC_TRANSFER  PCV_VAL_UINT PCK_TO PCE_PSET tgt PCK_FROM src
	{
		if (($$ = alloc_cmd()) == NULL)
			YYERROR;
		cmd = $$;
		$$->cmd = &parser_resource_transfer;
		cmd->cmd_tgt1 = $7;
		cmd->cmd_tgt2 = $5;
		cmd->cmd_qty = $2;
	};

discover_command: PCC_DISC
	{
		if (($$ = alloc_cmd()) == NULL)
			YYERROR;
		cmd = $$;
		$$->cmd = &parser_conf_discover;
	};

rename_command: PCC_RENAME entity name PCK_TO name
	{
		if (($$ = alloc_cmd()) == NULL)
			YYERROR;
		cmd = $$;
		switch ($2) {
		case PCE_SYSTEM:
			$$->cmd = &parser_conf_rename;
			break;
		case PCE_POOL:
			$$->cmd = &parser_pool_rename;
			break;
		case PCE_PSET:
			$$->cmd = &parser_pset_rename;
			break;
		default:
			warn(gettext(ERR_UNKNOWN_ENTITY), $2);
			YYERROR;
		}
		$$->cmd_tgt1 = $3;
		$$->cmd_tgt2 = $5;
	};

modify_entity: entity
	| PCE_CPU  {$$ = PCE_CPU;};

info_entity: entity
	| PCE_CPU  {$$ = PCE_CPU;};

entity: PCE_SYSTEM {$$ = PCE_SYSTEM;}
	| PCE_POOL {$$ = PCE_POOL;}
	| PCE_PSET {$$ = PCE_PSET;};

name: PCV_SYMBOL;

src: PCV_SYMBOL;

tgt: PCV_SYMBOL;

value: PCV_VAL_INT { $$.i = $1;}
	| PCV_VAL_UINT { $$.u = $1;}
	| PCV_VAL_FLOAT { $$.d = $1;}
	| PCV_VAL_BOOLEAN { $$.b = $1;}
	| PCV_VAL_STRING { $$.s = $1;};

prop_remove: PCK_UNDEF proptype name
	{
		if (($$ = alloc_prop(po_remove)) == NULL)
			YYERROR;
		$$->prop_name = $3;
	};

prop_op: prop_assign
	| prop_remove;

prop_ops: prop_op
	{
		prop_t *prop = NULL;
		prop_t *prev = NULL;

		for (prop = cmd->cmd_prop_list; prop != NULL;
		    prop = prop->prop_next)
			prev = prop; /* Find end of list */
		if (prev != NULL)
			prev->prop_next = $1;
		else
			cmd->cmd_prop_list = $1;
		$$ = cmd->cmd_prop_list;
	}
	| prop_ops PCK_SEPLST prop_op
	{
		prop_t *prop = NULL;
		prop_t *prev = NULL;

		for (prop = cmd->cmd_prop_list; prop != NULL;
		    prop = prop->prop_next)
			prev = prop; /* Find end of list */
		if (prev != NULL)
			prev->prop_next = $3;
		else
			cmd->cmd_prop_list = $3;
		$$ = cmd->cmd_prop_list;

	};

prop_assign: proptype name PCK_ASSIGN value
	{
		if (($$ = alloc_prop(po_create)) == NULL)
			YYERROR;
		$$->prop_name = $2;
		switch ($1) {
		case PCT_INT:
			pool_value_set_int64($$->prop_value, $4.i);
			break;
		case PCT_UINT:
			pool_value_set_uint64($$->prop_value, $4.u);
			break;
		case PCT_BOOLEAN:
			pool_value_set_bool($$->prop_value, $4.b);
			break;
		case PCT_FLOAT:
			pool_value_set_double($$->prop_value, $4.d);
			break;
		case PCT_STRING:
			pool_value_set_string($$->prop_value, $4.s);
			break;
		}
	};

property_list: PCK_OPENLST prop_ops PCK_CLOSELST
	{
		$$ = $2;
	};

resource_assigns: resource_assign
	{
		assoc_t *assoc = NULL;
		assoc_t *prev = NULL;

		for (assoc = cmd->cmd_assoc_list; assoc != NULL;
		    assoc = assoc->assoc_next)
			prev = assoc; /* Find end of list */
		if (prev != NULL)
			prev->assoc_next = $1;
		else
			cmd->cmd_assoc_list = $1;
		$$ = cmd->cmd_assoc_list;
	}

	| resource_assigns PCK_SEPLST resource_assign
	{
		assoc_t *assoc = NULL;
		assoc_t *prev = NULL;

		for (assoc = cmd->cmd_assoc_list; assoc != NULL;
		    assoc = assoc->assoc_next)
			prev = assoc; /* Find end of list */
		if (prev != NULL)
			prev->assoc_next = $3;
		$$ = $3;
	};

resource_assign: resource name
	{
		if (($$ = alloc_assoc($1, $2)) == NULL)
			YYERROR;
	};

resource: PCE_PSET {$$ = PCE_PSET;};

resource_list: PCK_OPENLST resource_assigns PCK_CLOSELST
	{
		$$ = $2;
	};

component_assigns: component_assign
	{
		assoc_t *assoc = NULL;
		assoc_t *prev = NULL;

		for (assoc = cmd->cmd_assoc_list; assoc != NULL;
		    assoc = assoc->assoc_next)
			prev = assoc; /* Find end of list */
		if (prev != NULL)
			prev->assoc_next = $1;
		else
			cmd->cmd_assoc_list = $1;
		$$ = cmd->cmd_assoc_list;
	}

	| component_assigns PCK_SEPLST component_assign
	{
		assoc_t *assoc = NULL;
		assoc_t *prev = NULL;

		for (assoc = cmd->cmd_assoc_list; assoc != NULL;
		    assoc = assoc->assoc_next)
			prev = assoc; /* Find end of list */
		if (prev != NULL)
			prev->assoc_next = $3;
		$$ = $3;
	};

component_list: PCK_OPENLST component_assigns PCK_CLOSELST
	{
		$$ = $2;
	};

component_assign: component name
	{
		if (($$ = alloc_assoc($1, $2)) == NULL)
			YYERROR;
	};

component: PCE_CPU {$$ = PCE_CPU;};

proptype: PCT_INT {$$ = PCT_INT;}
	| PCT_UINT {$$ = PCT_UINT;}
	| PCT_BOOLEAN {$$ = PCT_BOOLEAN;}
	| PCT_FLOAT {$$ = PCT_FLOAT;}
	| PCT_STRING {$$ = PCT_STRING;};

%%

#ifndef	TEXT_DOMAIN
#define	TEXT_DOMAIN "SYS_TEST"
#endif

int
main(int argc, char *argv[])
{
	int opt;
	int docmd = PO_FALSE;

	(void) getpname(argv[0]);
	(void) setlocale(LC_ALL, "");
	(void) textdomain(TEXT_DOMAIN);
	if (atexit(terminate) != 0) {
		die(gettext(ERR_SET_TERM), get_errstr());
	}

	conf_file = pool_static_location();

	yydebug = 0;
	while ((opt = getopt(argc, argv, cmd_options)) != (int)EOF) {

		switch (opt) {
		case 'c': /* Process command line */
			if (dofile == PO_TRUE)
				usage(1);
			arg_parse(optarg);
			docmd = PO_TRUE;
			break;
		case 'd': /* Manipulate dynamic configuration */
			conf_file = pool_dynamic_location();
			break;
		case 'f': /* Process command file */
			if (docmd == PO_TRUE)
				usage(1);
			file_parse(optarg);
			dofile = PO_TRUE;
			break;
		case 'h':
			usage(2);
			break;
		case '?':
		default:
			usage(1);
			break;
		}
	}
	if (docmd == PO_FALSE && dofile == PO_FALSE)
		usage(1);

	if (optind == argc - 1) {
		if (strcmp(conf_file, pool_dynamic_location()) == 0)
			usage(1);
		conf_file = argv[optind];
	} else if (optind <  argc - 1)
		usage(1);

	if ((conf = pool_conf_alloc()) == NULL) {
		die(gettext(ERR_ALLOC_ELEMENT), gettext(CONFIGURATION),
		    get_errstr());
	}
	/*
	 * Opening a conf is complex, since we may be opening one of the
	 * following:
	 *	- An existing configuration that we can modify
	 *	- An existing configuration that we can't modify
	 *	- A new configuration that we can modify
	 *	- A new configuration that we can't modify
	 * The parser_conf_discover() function closes the file and reopens
	 * in PO_CREAT mode, so we only need be concerned here with the
	 * first two cases.
	 * Always try to open RDWR, if fail try RDONLY. Don't check
	 * if that fails, since we may be trying to discover a configuration
	 * in which case it's valid for both open attempts to fail. Later, when
	 * processing commands, if we don't have a valid configuration and
	 * we are trying to process a command which isn't a create or a discover
	 * we will fail the command as there is no valid configuration to
	 * work with.
	 */
	if (pool_conf_open(conf, conf_file, PO_RDWR) != 0) {
		conf_edit_error = pool_error();
		conf_edit_errno = errno;
		if (pool_conf_open(conf, conf_file, PO_RDONLY) != 0) {
			conf_list_error = pool_error();
			conf_list_errno = errno;
		}
	}

	if (yyparse() == 0) {
		if (pool_conf_status(conf) >= POF_VALID) {
			if (pool_conf_validate(conf, POV_STRICT) == PO_FAIL) {
				die(gettext(ERR_VALIDATION_FAILED),
				    get_errstr());
			}
			/*
			 * If the user attempted to change the configuration,
			 * then we should try to save the changes.
			 */
			if (edited == PO_TRUE) {
				if (pool_conf_commit(conf, 0) == PO_FAIL) {
					die(gettext(ERR_CONFIG_SAVE_FAILED),
					    get_errstr());
				}
			}
			pool_conf_close(conf);
		}
	} else {
		die(gettext(ERR_CMDPARSE_FAILED));
	}

	/*
	 * Cleanup is performed in terminate(), using atexit
	 */
	return (0);
}

/*
 * Info Commands
 * Invoke the appropriate libpool info function and display the returned
 * information.
 */
static void
parser_conf_info(cmd_t *cmd)
{
	char *info_buf;
	const char *tgt = cmd->cmd_tgt1;
	pool_value_t *pv = NULL;
	pool_elem_t *pe;

	if ((pe = pool_conf_to_elem(conf)) == NULL)
		die(gettext(ERR_GET_ELEMENT_DETAILS),
		    gettext(CONFIGURATION), "unknown", get_errstr());

	if (tgt != NULL)
		check_conf_name(cmd);
	else {
		if ((pv = pool_value_alloc()) == NULL)
			die(gettext(ERR_GET_ELEMENT_DETAILS),
			    gettext(CONFIGURATION), "unknown", get_errstr());
		if (pool_get_property(conf, pe, "system.name", pv) ==
		    POC_INVAL ||
		    pool_value_get_string(pv, &tgt) != PO_SUCCESS)
			die(gettext(ERR_GET_ELEMENT_DETAILS),
			    gettext(CONFIGURATION), "unknown", get_errstr());
	}
	if ((info_buf = pool_conf_info(conf, PO_TRUE)) == NULL) {
		die(gettext(ERR_GET_ELEMENT_DETAILS), gettext(CONFIGURATION),
		    tgt, get_errstr());
	}
	if (pv != NULL) {
		pool_value_free(pv);
	}
	(void) printf("%s\n", info_buf);
	free(info_buf);
}

static void
parser_pool_info(cmd_t *cmd)
{
	pool_t *pool;
	char *info_buf;

	if ((pool = pool_get_pool(conf, cmd->cmd_tgt1)) == NULL)
		die(gettext(ERR_LOCATE_ELEMENT), gettext(POOL), cmd->cmd_tgt1,
		    get_errstr());

	if ((info_buf = pool_info(conf, pool, PO_TRUE)) == NULL)
		die(gettext(ERR_GET_ELEMENT_DETAILS), gettext(POOL),
		    cmd->cmd_tgt1, get_errstr());
	(void) printf("%s\n", info_buf);
	free(info_buf);
}

static void
parser_resource_info(cmd_t *cmd, const char *type)
{
	pool_resource_t *resource;
	char *info_buf;

	if ((resource = pool_get_resource(conf, type, cmd->cmd_tgt1)) == NULL)
		die(gettext(ERR_LOCATE_ELEMENT), gettext(RESOURCE),
		    cmd->cmd_tgt1, get_errstr());

	if ((info_buf = pool_resource_info(conf, resource, PO_TRUE)) == NULL)
		die(gettext(ERR_GET_ELEMENT_DETAILS), gettext(RESOURCE),
		    cmd->cmd_tgt1, get_errstr());
	(void) printf("%s\n", info_buf);
	free(info_buf);
}

static void
parser_pset_info(cmd_t *cmd)
{
	parser_resource_info(cmd, PSET);
}

static void
parser_cpu_info(cmd_t *cmd)
{
	pool_component_t *comp;
	char *info_buf;

	if ((comp = get_cpu(cmd->cmd_tgt1)) == NULL)
		die(gettext(ERR_LOCATE_ELEMENT), gettext(CPU),
		    cmd->cmd_tgt1, get_errstr());
	if ((info_buf = pool_component_info(conf, comp, PO_TRUE)) == NULL) {
		die(gettext(ERR_GET_ELEMENT_DETAILS), gettext(CPU),
		    cmd->cmd_tgt1, get_errstr());
	}
	(void) printf("%s\n", info_buf);
	free(info_buf);
}

/*
 * Create Commands
 * Invoke the appropriate libpool create function and perform any requested
 * property operations.
 */
static void
parser_conf_create(cmd_t *cmd)
{
	const char *tmp_name;
	pool_elem_t *pe;

	if (conf != NULL && pool_conf_status(conf) >= POF_VALID)
		pool_conf_close(conf);
	if (pool_conf_open(conf, conf_file, PO_CREAT) != 0) {
		die(gettext(ERR_CREATE_ELEMENT), gettext(CONFIGURATION),
		    cmd->cmd_tgt1, get_errstr());
	}
	tmp_name = cmd->cmd_tgt1;
	cmd->cmd_tgt1 = cmd->cmd_tgt2;
	cmd->cmd_tgt2 = tmp_name;
	parser_conf_rename(cmd);
	if ((pe = pool_conf_to_elem(conf)) == NULL)
		die(gettext(ERR_GET_ELEMENT_DETAILS),
		    gettext(CONFIGURATION), "unknown", get_errstr());
	prop_list_walk(cmd, pe);
}

static void
parser_pool_create(cmd_t *cmd)
{
	pool_t *pool;

	if ((pool = pool_create(conf, cmd->cmd_tgt1)) == NULL)
		die(gettext(ERR_CREATE_ELEMENT), gettext(POOL), cmd->cmd_tgt1,
		    get_errstr());
	prop_list_walk(cmd, pool_to_elem(conf, pool));
}

static void
parser_resource_create(cmd_t *cmd, const char *type)
{
	pool_resource_t *resource;

	if ((resource = pool_resource_create(conf, type, cmd->cmd_tgt1))
	    == NULL)
		die(gettext(ERR_CREATE_ELEMENT), type, cmd->cmd_tgt1,
		    get_errstr());

	process_min_max(resource);

	prop_list_walk(cmd, pool_resource_to_elem(conf, resource));
}

static void
parser_pset_create(cmd_t *cmd)
{
	parser_resource_create(cmd, PSET);
}

/*
 * Rename Commands
 * Rename the target by calling pool_put_property for the name property.
 */
static void
parser_rename(cmd_t *cmd, pool_elem_t *pe, const char *name)
{
	pool_value_t *pv;

	if ((pv = pool_value_alloc()) == NULL) {
		die(gettext(ERR_ALLOC_ELEMENT), gettext(RESOURCE),
		    get_errstr());
	}
	pool_value_set_string(pv, cmd->cmd_tgt2);
	if (pool_put_property(conf, pe, name, pv) != 0)
		die(gettext(ERR_PUT_PROPERTY), name, get_errstr());
	pool_value_free(pv);
}

static void
parser_conf_rename(cmd_t *cmd)
{
	pool_elem_t *pe;

	if ((pe = pool_conf_to_elem(conf)) == NULL)
		die(gettext(ERR_GET_ELEMENT_DETAILS),
		    gettext(CONFIGURATION), "unknown", get_errstr());

	if (cmd->cmd_tgt1 != NULL)
		check_conf_name(cmd);

	parser_rename(cmd, pe, SYSTEM_NAME);
}

static void
parser_pool_rename(cmd_t *cmd)
{
	pool_t *pool;

	if ((pool = pool_get_pool(conf, cmd->cmd_tgt1)) == NULL)
		die(gettext(ERR_LOCATE_ELEMENT), gettext(POOL), cmd->cmd_tgt1,
		    get_errstr());

	parser_rename(cmd, pool_to_elem(conf, pool), POOL_NAME);
}

static void
parser_pset_rename(cmd_t *cmd)
{
	pool_resource_t *resource;

	if ((resource = pool_get_resource(conf, PSET, cmd->cmd_tgt1)) == NULL)
		die(gettext(ERR_LOCATE_ELEMENT), gettext(PSET), cmd->cmd_tgt1,
		    get_errstr());

	parser_rename(cmd, pool_resource_to_elem(conf, resource), PSET_NAME);
}

/*
 * Destroy Commands
 * Invoke the appropriate libpool destroy function to remove the target of the
 * command from the configuration.
 */
static void
parser_conf_destroy(cmd_t *cmd)
{
	if (cmd->cmd_tgt1 != NULL)
		check_conf_name(cmd);

	if (pool_conf_remove(conf) != 0)
		die(gettext(ERR_DESTROY_ELEMENT), gettext(CONFIGURATION),
		    cmd->cmd_tgt1, get_errstr());
}

static void
parser_pool_destroy(cmd_t *cmd)
{
	pool_t *pool;

	if ((pool = pool_get_pool(conf, cmd->cmd_tgt1)) == NULL)
		die(gettext(ERR_LOCATE_ELEMENT), gettext(POOL), cmd->cmd_tgt1,
		    get_errstr());

	if (pool_destroy(conf, pool) != 0)
		die(gettext(ERR_DESTROY_ELEMENT), gettext(POOL), cmd->cmd_tgt1,
		    get_errstr());
}

static void
parser_resource_destroy(cmd_t *cmd, const char *type)
{
	pool_resource_t *resource;

	if ((resource = pool_get_resource(conf, type, cmd->cmd_tgt1)) == NULL)
		die(gettext(ERR_LOCATE_ELEMENT), type, cmd->cmd_tgt1,
		    get_errstr());

	if (pool_resource_destroy(conf, resource) != 0)
		die(gettext(ERR_DESTROY_ELEMENT), type, cmd->cmd_tgt1,
		    get_errstr());
}

static void
parser_pset_destroy(cmd_t *cmd)
{
	parser_resource_destroy(cmd, PSET);
}

/*
 * Modify Commands
 * Perform any requested property operations.
 */
static void
parser_conf_modify(cmd_t *cmd)
{
	pool_elem_t *pe;

	if ((pe = pool_conf_to_elem(conf)) == NULL)
		die(gettext(ERR_GET_ELEMENT_DETAILS),
		    gettext(CONFIGURATION), "unknown", get_errstr());

	if (cmd->cmd_tgt1 != NULL)
		check_conf_name(cmd);

	prop_list_walk(cmd, pe);
}

static void
parser_pool_modify(cmd_t *cmd)
{
	pool_t *pool;

	if ((pool = pool_get_pool(conf, cmd->cmd_tgt1)) == NULL)
		die(gettext(ERR_LOCATE_ELEMENT), gettext(POOL), cmd->cmd_tgt1,
		    get_errstr());
	prop_list_walk(cmd, pool_to_elem(conf, pool));
}

static void
parser_resource_modify(cmd_t *cmd, const char *type)
{
	pool_resource_t *resource;

	if ((resource = pool_get_resource(conf, type, cmd->cmd_tgt1)) == NULL)
		die(gettext(ERR_LOCATE_ELEMENT), gettext(RESOURCE),
		    cmd->cmd_tgt1, get_errstr());

	process_min_max(resource);

	prop_list_walk(cmd, pool_resource_to_elem(conf, resource));
}

static void
parser_pset_modify(cmd_t *cmd)
{
	parser_resource_modify(cmd, PSET);
}

static void
parser_cpu_modify(cmd_t *cmd)
{
	pool_component_t *comp;

	if ((comp = get_cpu(cmd->cmd_tgt1)) == NULL)
		die(gettext(ERR_LOCATE_ELEMENT), gettext(CPU),
		    cmd->cmd_tgt1, get_errstr());
	prop_list_walk(cmd, pool_component_to_elem(conf, comp));
}

/*
 * Discover Commands
 * Invoke the libpool pool_conf_open function so that discovery will be
 * performed.
 */

/*ARGSUSED*/
static void
parser_conf_discover(cmd_t *cmd)
{
	struct utsname utsname;

	if (strcmp(conf_file, pool_dynamic_location()) == 0)
		return;

	if (uname(&utsname) < 0)
		die(gettext(ERR_CREATE_ELEMENT), gettext(CONFIGURATION),
		    "unknown", get_errstr());

	if (conf != NULL && pool_conf_status(conf) >= POF_VALID)
		pool_conf_close(conf);
	if (pool_conf_open(conf, pool_dynamic_location(), PO_RDONLY) != 0) {
		die(gettext(ERR_CREATE_ELEMENT), gettext(CONFIGURATION),
		    utsname.nodename, get_errstr());
	}
	if (pool_conf_export(conf, conf_file, POX_NATIVE) != 0) {
		die(gettext(ERR_CREATE_ELEMENT), gettext(CONFIGURATION),
		    utsname.nodename, get_errstr());
	}
	(void) pool_conf_close(conf);
	if (pool_conf_open(conf, conf_file, PO_RDWR) != 0) {
		die(gettext(ERR_CREATE_ELEMENT), gettext(CONFIGURATION),
		    utsname.nodename, get_errstr());
	}
}

/*
 * Associate Commands
 * Walk the list of specified associations so that the target pool will be
 * associated with the required resources.
 */

static void
parser_pool_associate(cmd_t *cmd)
{
	pool_t *pool;

	if ((pool = pool_get_pool(conf, cmd->cmd_tgt1)) == NULL)
		die(gettext(ERR_LOCATE_ELEMENT), gettext(POOL), cmd->cmd_tgt1,
		    get_errstr());
	assoc_list_walk(cmd, pool);
}

/*
 * Assign Commands
 * Walk the list of specified assignations so that the required
 * components will be assigned to the target resource.
 */

static void
parser_resource_xtransfer(cmd_t *cmd)
{
	pool_resource_t *resource;

	if ((resource = pool_get_resource(conf, PSET, cmd->cmd_tgt1)) == NULL)
		die(gettext(ERR_LOCATE_ELEMENT), gettext(RESOURCE),
		    cmd->cmd_tgt1, get_errstr());
	transfer_list_walk(cmd, resource);
}

/*
 * Transfer Commands
 * Transfer the specified quantity of resource between the src and the tgt.
 */

static void
parser_resource_transfer(cmd_t *cmd)
{
	pool_resource_t *src;
	pool_resource_t *tgt;

	if ((src = pool_get_resource(conf, PSET, cmd->cmd_tgt1)) == NULL)
		die(gettext(ERR_LOCATE_ELEMENT), gettext(RESOURCE),
		    cmd->cmd_tgt1, get_errstr());
	if ((tgt = pool_get_resource(conf, PSET, cmd->cmd_tgt2)) == NULL)
		die(gettext(ERR_LOCATE_ELEMENT), gettext(RESOURCE),
		    cmd->cmd_tgt2, get_errstr());
	if (pool_resource_transfer(conf, src, tgt, cmd->cmd_qty) != PO_SUCCESS)
		die(gettext(ERR_XFER_QUANTITY), cmd->cmd_qty,
		    cmd->cmd_tgt1, cmd->cmd_tgt2, get_errstr());
}

/*
 * arg_parse() puts the parser into command parsing mode. Create a tmpfile
 * and instruct the parser to read instructions from this location by setting
 * yyin to the value returned by tmpfile. Write the command into the file.
 * Then seek back to to the start of the file so that the parser can read
 * the instructions.
 */
static void
arg_parse(const char *command)
{
	if ((yyin = tmpfile()) == NULL)
		die(gettext(ERR_CMD_FILE_INIT), strerror(errno));
	if (fwrite(command, strlen(command), 1, yyin) != 1)
		die(gettext(ERR_CMD_FILE_INIT), strerror(errno));
	if (fseek(yyin, 0, SEEK_SET) != 0)
		die(gettext(ERR_CMD_FILE_INIT), strerror(errno));
}

/*
 * file_parse() puts the parser into command file parsing mode. Firstly check
 * to see if the user wishes to parse from standard input, if so do nothing.
 * Attempt to open the specified file and instruct the parser to read
 * instructions from this location by setting yyin to the value returned by
 * fopen.
 */
static void
file_parse(const char *file)
{
	if (strcmp(file, "-") == 0)
		return;

	if ((yyin = fopen(file, "r")) == NULL) {
		die(gettext(ERR_CMD_FILE_INIT), strerror(errno));
	}
}

/*
 * free_cmd() releases the resources associated with the supplied cmd parameter.
 */
static void
free_cmd(cmd_t *cmd)
{
	prop_t *prop = cmd->cmd_prop_list;
	assoc_t *assoc = cmd->cmd_assoc_list;

	free((void *)cmd->cmd_tgt1);
	free((void *)cmd->cmd_tgt2);
	while (prop != NULL) {
		prop_t *tmp = prop;
		prop = prop->prop_next;
		pool_value_free(tmp->prop_value);
		free((void *)tmp->prop_name);
		free(tmp);
	}
	while (assoc != NULL) {
		assoc_t *tmp = assoc;
		assoc = assoc->assoc_next;
		free((void *)tmp->assoc_name);
		free(tmp);
	}
	free(cmd);
}

/*
 * alloc_cmd() allocates the required resources for a cmd_t. On failure, a
 * warning is issued and NULL is returned.
 */
static cmd_t *
alloc_cmd(void)
{
	cmd_t *cmd;

	if ((cmd = malloc(sizeof (cmd_t))) == NULL) {
		warn(gettext(ERR_CMD_LINE_ALLOC));
		return (NULL);
	}

	(void) memset(cmd, 0, sizeof (cmd_t));

	return (cmd);
}

/*
 * alloc_prop() allocates the required resources for a prop_t. On failure, a
 * warning is issued and NULL is returned. The prop_t is initialised with
 * the prop_op_t parameter.
 */
static prop_t *
alloc_prop(prop_op_t op)
{
	prop_t *prop;

	if ((prop = malloc(sizeof (prop_t))) == NULL) {
		warn(gettext(ERR_PROP_ALLOC));
		return (NULL);
	}

	(void) memset(prop, 0, sizeof (prop_t));
	if ((prop->prop_value = pool_value_alloc()) == NULL) {
		warn(gettext(ERR_PROP_ALLOC));
		free(prop);
		return (NULL);
	}
	prop->prop_op = op;
	return (prop);
}

/*
 * alloc_assoc() allocates the required resources for an assoc_t. On failure, a
 * warning is issued and NULL is returned. The assoc_t is initialised with
 * the type and name of the association.
 */
static assoc_t *
alloc_assoc(int type, const char *name)
{
	assoc_t *assoc;

	if ((assoc = malloc(sizeof (assoc_t))) == NULL) {
		warn(gettext(ERR_ASSOC_ALLOC));
		return (NULL);
	}
	(void) memset(assoc, 0, sizeof (assoc_t));
	assoc->assoc_type = type;
	assoc->assoc_name = name;
	return (assoc);
}

/*
 * check_conf_name() ensures the the name of the system in the configuration
 * which is being manipulated matches the name of the system in the command.
 * If not, the command is terminated with an appropriate error message.
 */
static void
check_conf_name(cmd_t *cmd)
{
	pool_value_t *pv;
	const char *name;
	pool_elem_t *pe;

	if ((pe = pool_conf_to_elem(conf)) == NULL)
		die(gettext(ERR_GET_ELEMENT_DETAILS),
		    gettext(CONFIGURATION), "unknown", get_errstr());


	if ((pv = pool_value_alloc()) == NULL) {
		die(gettext(ERR_ALLOC_ELEMENT), gettext(RESOURCE),
		    get_errstr());
	}

	if (pool_get_property(conf, pe, SYSTEM_NAME, pv)
	    == POC_INVAL)
		die(gettext(ERR_GET_PROPERTY), gettext(SYSTEM_NAME),
		    get_errstr());

	if (pool_value_get_string(pv, &name) == PO_FAIL)
		die(gettext(ERR_GET_PROPERTY), gettext(SYSTEM_NAME),
		    get_errstr());

	if (strcmp(cmd->cmd_tgt1, name) != 0) {
		die(gettext(ERR_WRONG_SYSTEM_NAME), cmd->cmd_tgt1);
	}
	pool_value_free(pv);
}

/*
 * usage() display brief or verbose help for the poolcfg(1) command.
 */
static void
usage(int help)
{
	if (help >= 1)
		(void) fprintf(stderr, gettext(USAGE1), cmdname, cmdname,
		    cmdname);
	if (help >= 2)
		(void) fprintf(stderr, gettext(USAGE2));
	exit(E_USAGE);
}

/*
 * prop_list_walk() walks the property manipulation requests and either puts
 * or removes the property as appropriate.
 */
static void
prop_list_walk(cmd_t *cmd, pool_elem_t *pe)
{
	prop_t *prop;

	for (prop = cmd->cmd_prop_list; prop != NULL; prop = prop->prop_next) {
		switch (prop->prop_op) {
		case po_create:
			if (pool_put_property(conf, pe, prop->prop_name,
			    prop->prop_value) != 0)
				die(gettext(ERR_PUT_PROPERTY),
				    prop->prop_name, get_errstr());
			break;
		case po_remove:
			if (pool_rm_property(conf, pe, prop->prop_name) != 0)
				die(gettext(ERR_REMOVE_PROPERTY),
				    prop->prop_name, get_errstr());
			break;
		}
	}
}

/*
 * assoc_list_walk() walks the resource association requests and attempts
 * to associate the pool with the specified resource.
 */
static void
assoc_list_walk(cmd_t *cmd, pool_t *pool)
{
	assoc_t *assoc;

	for (assoc = cmd->cmd_assoc_list; assoc != NULL;
	    assoc = assoc->assoc_next) {
		pool_resource_t *resource;

		switch (assoc->assoc_type) {
		case PCE_PSET:
			if ((resource = pool_get_resource(conf,
			    PSET, assoc->assoc_name)) == NULL)
				die(gettext(ERR_LOCATE_ELEMENT), gettext(PSET),
				    assoc->assoc_name, get_errstr());
			break;
		default:
			die(gettext(ERR_UNKNOWN_RESOURCE),
			    assoc->assoc_type);
			break;
		}
		if (pool_associate(conf, pool, resource) != 0)
			die(gettext(ERR_ASSOC_RESOURCE), assoc->assoc_name,
			    get_errstr());
	}
}

/*
 * transfer_list_walk() walks the component assign requests and attempts
 * to assign the component with the specified resource.
 */
static void
transfer_list_walk(cmd_t *cmd, pool_resource_t *tgt)
{
	assoc_t *assoc;

	for (assoc = cmd->cmd_assoc_list; assoc != NULL;
	    assoc = assoc->assoc_next) {
		pool_component_t *comp;
		pool_resource_t *src;
		pool_component_t *xfer[2] = {NULL};

		if ((comp = get_cpu(assoc->assoc_name)) == NULL)
			die(gettext(ERR_LOCATE_ELEMENT), gettext(CPU),
			    assoc->assoc_name, get_errstr());
		if ((src = pool_get_owning_resource(conf, comp)) == NULL)
			die(gettext(ERR_XFER_COMPONENT), gettext(COMPONENT),
			    assoc->assoc_name, cmd->cmd_tgt1, get_errstr());
		xfer[0] = comp;
		if (pool_resource_xtransfer(conf, src, tgt, xfer) !=
		    PO_SUCCESS)
			die(gettext(ERR_XFER_COMPONENT), gettext(COMPONENT),
			    assoc->assoc_name, cmd->cmd_tgt1, get_errstr());
	}
}

/*
 * terminate() is invoked when poolcfg exits. It cleans up
 * configurations and closes the parser input stream.
 */
static void
terminate(void)
{
	if (conf != NULL) {
		(void) pool_conf_close(conf);
		pool_conf_free(conf);
	}
	if (yyin != stdin)
		(void) fclose(yyin);
}

/*
 * get_cpu() takes the name of a CPU components and attempts to locate
 * the element with that name. If the name is not formatted correctly
 * (i.e. contains non-numeric characters) then the function terminates
 * execution. If the components cannot be uniquely identified by the
 * name, then NULL is returned.
 */
static pool_component_t *
get_cpu(const char *name)
{
	pool_component_t **components;
	uint_t nelem;
	int64_t sysid;
	pool_value_t *vals[3] = {NULL};
	pool_component_t *ret;
	const char *c;

	if ((vals[0] = pool_value_alloc()) == NULL)
		return (NULL);
	if ((vals[1] = pool_value_alloc()) == NULL) {
		pool_value_free(vals[0]);
		return (NULL);
	}
	if (pool_value_set_string(vals[0], "cpu") != PO_SUCCESS ||
	    pool_value_set_name(vals[0], "type") != PO_SUCCESS) {
		pool_value_free(vals[0]);
		pool_value_free(vals[1]);
		return (NULL);
	}

	for (c = name; *c != '\0'; c++) {
		if (!isdigit(*c)){
			pool_value_free(vals[0]);
			pool_value_free(vals[1]);
			die(gettext(ERR_LOCATE_ELEMENT), gettext(CPU),
			    cmd->cmd_tgt1, gettext("CPU id should only contain "
			    "digits"));
		}
	}
	sysid = strtoll(name, NULL, 0);
	if (errno == ERANGE || errno == EINVAL) {
		pool_value_free(vals[0]);
		pool_value_free(vals[1]);
		return (NULL);
	}
	pool_value_set_int64(vals[1], sysid);
	if (pool_value_set_name(vals[1], CPU_SYSID) != PO_SUCCESS) {
		pool_value_free(vals[0]);
		pool_value_free(vals[1]);
		return (NULL);
	}
	if ((components = pool_query_components(conf, &nelem, vals)) ==
	    NULL) {
		pool_value_free(vals[0]);
		pool_value_free(vals[1]);
		return (NULL);
	}
	if (nelem != 1) {
		free(components);
		pool_value_free(vals[0]);
		pool_value_free(vals[1]);
		return (NULL);
	}
	pool_value_free(vals[0]);
	pool_value_free(vals[1]);
	ret = components[0];
	free(components);
	return (ret);
}

/*
 * process_min_max() ensures that "min" and "max" properties are
 * processed correctly by poolcfg. libpool enforces validity
 * constraints on these properties and so it's important that changes
 * to them are supplied to the library in the correct order.
 */
void
process_min_max(pool_resource_t *resource)
{
	prop_t *minprop = NULL;
	prop_t *maxprop = NULL;
	prop_t *prop;

	/*
	 * Before walking the list of properties, it has to be checked
	 * to ensure there are no clashes between min and max. If
	 * there are, then process these properties immediately.
	 */
	for (prop = cmd->cmd_prop_list; prop != NULL; prop = prop->prop_next) {
		const char *pos;

		if ((pos = strstr(prop->prop_name, min_suffix)) != NULL)
			if (pos == prop->prop_name + strlen(prop->prop_name)
			    - 4)
				minprop = prop;
		if ((pos = strstr(prop->prop_name, max_suffix)) != NULL)
			if (pos == prop->prop_name + strlen(prop->prop_name)
			    - 4)
				maxprop = prop;
	}
	if (minprop && maxprop) {
		pool_value_t *pv;
		uint64_t smin, smax, dmax;
		const char *type;
		char *prop_name;
		pool_elem_t *pe = pool_resource_to_elem(conf, resource);

		if ((pv = pool_value_alloc()) == NULL)
			die(gettext(ERR_NOMEM));

		(void) pool_get_property(conf, pe, "type", pv);
		(void) pool_value_get_string(pv, &type);

		if ((prop_name = malloc(strlen(type) + strlen(max_suffix)
		    + 1)) == NULL)
			die(gettext(ERR_NOMEM));

		(void) sprintf(prop_name, "%s%s", type, max_suffix);
		(void) pool_get_property(conf, pe, prop_name, pv);
		(void) pool_value_get_uint64(pv, &dmax);

		(void) pool_value_get_uint64(minprop->prop_value, &smin);

		(void) pool_value_get_uint64(maxprop->prop_value, &smax);
		if (smin < dmax) {
			(void) pool_put_property(conf, pe,
			minprop->prop_name, minprop->prop_value);
		} else {
			(void) pool_put_property(conf, pe,
			maxprop->prop_name, maxprop->prop_value);
		}
		free((void *)prop_name);
		pool_value_free(pv);
	}
}