File: hostlist.c

package info (click to toggle)
diod 1.0.24-5.2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 29,200 kB
  • sloc: ansic: 33,159; sh: 6,545; makefile: 363; perl: 80
file content (2759 lines) | stat: -rw-r--r-- 71,224 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
/*****************************************************************************\
 *  $Id: hostlist.c 9378 2009-12-16 05:34:32Z grondo $
 *****************************************************************************
 *  Copyright (C) 2002 The Regents of the University of California.
 *  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
 *  Written by Mark Grondona <mgrondona@llnl.gov>
 *  UCRL-CODE-2002-040.
 *  
 *  This file is part of SLURM, a resource management program.
 *  For details, see <http://www.llnl.gov/linux/slurm/>.
 *  
 *  SLURM is free software; you can redistribute it and/or modify it under
 *  the terms of the GNU General Public License as published by the Free
 *  Software Foundation; either version 2 of the License, or (at your option)
 *  any later version.
 *  
 *  SLURM is distributed in the hope that it will be useful, but WITHOUT ANY
 *  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 *  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 *  details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with SLURM; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
\*****************************************************************************/

#ifdef HAVE_CONFIG_H
#  include "config.h"
#  if HAVE_STRING_H
#    include <string.h>
#  endif
#  if HAVE_PTHREAD_H
#    include <pthread.h>
#  endif
#else                /* !HAVE_CONFIG_H */
#  include <string.h>
#  include <pthread.h>
#endif                /* HAVE_CONFIG_H */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <assert.h>
#include <errno.h>
#include <ctype.h>
#include <sys/param.h>
#include <unistd.h>

#include "hostlist.h"

/*
 * lsd_fatal_error : fatal error macro
 */
#ifdef WITH_LSD_FATAL_ERROR_FUNC
#  undef lsd_fatal_error
   extern void lsd_fatal_error(char *file, int line, char *mesg);
#else /* !WITH_LSD_FATAL_ERROR_FUNC */
#  ifndef lsd_fatal_error
#    define lsd_fatal_error(file, line, mesg)                                \
       do {                                                                  \
           fprintf(stderr, "ERROR: [%s:%d] %s: %s\n",                        \
           file, line, mesg, strerror(errno));                               \
       } while (0)
#  endif /* !lsd_fatal_error */
#endif /* !WITH_LSD_FATAL_ERROR_FUNC */

/*
 * lsd_nomem_error
 */
#ifdef WITH_LSD_NOMEM_ERROR_FUNC
#  undef lsd_nomem_error
   extern void * lsd_nomem_error(char *file, int line, char *mesg);
#else /* !WITH_LSD_NOMEM_ERROR_FUNC */
#  ifndef lsd_nomem_error
#    define lsd_nomem_error(file, line, mesg) (NULL)
#  endif /* !lsd_nomem_error */
#endif /* !WITH_LSD_NOMEM_ERROR_FUNC */

/*
 * OOM helper function
 *  Automatically call lsd_nomem_error with appropriate args
 *  and set errno to ENOMEM
 */
#define out_of_memory(mesg)                                                  \
    do {                                                                     \
        errno = ENOMEM;                                                      \
        return(lsd_nomem_error(__FILE__, __LINE__, mesg));                   \
    } while (0)

/* 
 * Some constants and tunables:
 */

/* number of elements to allocate when extending the hostlist array */
#define HOSTLIST_CHUNK    16

/* max host range: anything larger will be assumed to be an error */
#define MAX_RANGE    16384    /* 16K Hosts */

/* max host suffix value */
#define MAX_HOST_SUFFIX 1<<25

/* max number of ranges that will be processed between brackets */
#define MAX_RANGES    10240    /* 10K Ranges */

/* size of internal hostname buffer (+ some slop), hostnames will probably
 * be truncated if longer than MAXHOSTNAMELEN */
#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN    64
#endif

/* max size of internal hostrange buffer */
#define MAXHOSTRANGELEN 1024

/* ----[ Internal Data Structures ]---- */

/* hostname type: A convenience structure used in parsing single hostnames */
struct hostname_components {
    char *hostname;         /* cache of initialized hostname        */
    char *prefix;           /* hostname prefix                      */
    unsigned long num;      /* numeric suffix                       */

    /* string representation of numeric suffix
     * points into `hostname'                                       */
    char *suffix;
};

typedef struct hostname_components *hostname_t;

/* hostrange type: A single prefix with `hi' and `lo' numeric suffix values */
struct hostrange_components {
    char *prefix;        /* alphanumeric prefix: */

    /* beginning (lo) and end (hi) of suffix range */
    unsigned long lo, hi;

    /* width of numeric output format
     * (pad with zeros up to this width) */
    int width;

    /* If singlehost is 1, `lo' and `hi' are invalid */
    unsigned singlehost:1;
};

typedef struct hostrange_components *hostrange_t;

/* The hostlist type: An array based list of hostrange_t's */
struct hostlist {
#ifndef NDEBUG
#define HOSTLIST_MAGIC    57005
    int magic;
#endif
#if    WITH_PTHREADS
    pthread_mutex_t mutex;
#endif                /* WITH_PTHREADS */

    /* current number of elements available in array */
    int size;

    /* current number of ranges stored in array */
    int nranges;

    /* current number of hosts stored in hostlist */
    int nhosts;

    /* pointer to hostrange array */
    hostrange_t *hr;

    /* list of iterators */
    struct hostlist_iterator *ilist;

};


/* a hostset is a wrapper around a hostlist */
struct hostset {
    hostlist_t hl;
};

struct hostlist_iterator {
#ifndef NDEBUG
    int magic;
#endif
    /* hostlist we are traversing */
    hostlist_t hl;

    /* current index of iterator in hl->hr[] */
    int idx;

    /* current hostrange object in list hl, i.e. hl->hr[idx] */
    hostrange_t hr;

    /* current depth we've traversed into range hr */
    int depth;

    /* next ptr for lists of iterators */
    struct hostlist_iterator *next;
};


/* ---- ---- */

/* ------[ static function prototypes ]------ */

static void _error(char *file, int line, char *mesg, ...);
static char * _next_tok(char *, char **);
static int    _zero_padded(unsigned long, int);
static int    _width_equiv(unsigned long, int *, unsigned long, int *);

static int           host_prefix_end(const char *);
static hostname_t    hostname_create(const char *);
static void          hostname_destroy(hostname_t);
static int           hostname_suffix_is_valid(hostname_t);
static int           hostname_suffix_width(hostname_t);

static hostrange_t   hostrange_new(void);
static hostrange_t   hostrange_create_single(const char *);
static hostrange_t   hostrange_create(char *, unsigned long, unsigned long, int);
static unsigned long hostrange_count(hostrange_t);
static hostrange_t   hostrange_copy(hostrange_t);
static void          hostrange_destroy(hostrange_t);
static hostrange_t   hostrange_delete_host(hostrange_t, unsigned long);
static int           hostrange_cmp(hostrange_t, hostrange_t);
static int           hostrange_prefix_cmp(hostrange_t, hostrange_t);
static int           hostrange_within_range(hostrange_t, hostrange_t);
static int           hostrange_width_combine(hostrange_t, hostrange_t);
static int           hostrange_empty(hostrange_t);
static char *        hostrange_pop(hostrange_t);
static char *        hostrange_shift(hostrange_t);
static int           hostrange_join(hostrange_t, hostrange_t);
static hostrange_t   hostrange_intersect(hostrange_t, hostrange_t);
static int           hostrange_hn_within(hostrange_t, hostname_t);
static size_t        hostrange_to_string(hostrange_t hr, size_t, char *, char *);
static size_t        hostrange_numstr(hostrange_t, size_t, char *);

static hostlist_t  hostlist_new(void);
static hostlist_t _hostlist_create_bracketed(const char *, char *, char *);
static int         hostlist_resize(hostlist_t, size_t);
static int         hostlist_expand(hostlist_t);
static int         hostlist_push_range(hostlist_t, hostrange_t);
static int         hostlist_push_hr(hostlist_t, char *, unsigned long,
                                    unsigned long, int);
static int         hostlist_insert_range(hostlist_t, hostrange_t, int);
static void        hostlist_delete_range(hostlist_t, int n);
static void        hostlist_coalesce(hostlist_t hl);
static void        hostlist_collapse(hostlist_t hl);
static hostlist_t _hostlist_create(const char *, char *, char *);
static void        hostlist_shift_iterators(hostlist_t, int, int, int);
static int        _attempt_range_join(hostlist_t, int);
static int        _is_bracket_needed(hostlist_t, int);

static hostlist_iterator_t hostlist_iterator_new(void);
static void               _iterator_advance(hostlist_iterator_t);
static void               _iterator_advance_range(hostlist_iterator_t);

static int hostset_find_host(hostset_t, const char *);

/* ------[ macros ]------ */

#ifdef WITH_PTHREADS
#  define mutex_init(mutex)                                                  \
     do {                                                                    \
        int e = pthread_mutex_init(mutex, NULL);                             \
        if (e) {                                                             \
            errno = e;                                                       \
            lsd_fatal_error(__FILE__, __LINE__, "hostlist mutex init:");     \
            abort();                                                         \
        }                                                                    \
     } while (0)

#  define mutex_lock(mutex)                                                  \
     do {                                                                    \
        int e = pthread_mutex_lock(mutex);                                   \
        if (e) {                                                             \
           errno = e;                                                        \
           lsd_fatal_error(__FILE__, __LINE__, "hostlist mutex lock:");      \
           abort();                                                          \
        }                                                                    \
     } while (0)

#  define mutex_unlock(mutex)                                                \
     do {                                                                    \
        int e = pthread_mutex_unlock(mutex);                                 \
        if (e) {                                                             \
            errno = e;                                                       \
            lsd_fatal_error(__FILE__, __LINE__, "hostlist mutex unlock:");   \
            abort();                                                         \
        }                                                                    \
     } while (0)

#  define mutex_destroy(mutex)                                               \
     do {                                                                    \
        int e = pthread_mutex_destroy(mutex);                                \
        if (e) {                                                             \
            errno = e;                                                       \
            lsd_fatal_error(__FILE__, __LINE__, "hostlist mutex destroy:");  \
            abort();                                                         \
        }                                                                    \
     } while (0)

#else                /* !WITH_PTHREADS */

#  define mutex_init(mutex)
#  define mutex_lock(mutex)
#  define mutex_unlock(mutex)
#  define mutex_destroy(mutex)

#endif                /* WITH_PTHREADS */

#define LOCK_HOSTLIST(_hl)                                                   \
      do {                                                                   \
          assert(_hl != NULL);                                               \
          mutex_lock(&(_hl)->mutex);                                         \
          assert((_hl)->magic == HOSTLIST_MAGIC);                            \
      } while (0)

#define UNLOCK_HOSTLIST(_hl)                                                 \
      do {                                                                   \
          mutex_unlock(&(_hl)->mutex);                                       \
      } while (0)                       

#define seterrno_ret(_errno, _rc)                                            \
      do {                                                                   \
          errno = _errno;                                                    \
          return _rc;                                                        \
      } while (0)

/* ------[ Function Definitions ]------ */

/* ----[ general utility functions ]---- */


/*
 *  Varargs capable error reporting via lsd_fatal_error()
 */
static void _error(char *file, int line, char *msg, ...)
{
    va_list ap;
    char    buf[1024];
    int     len = 0;
    va_start(ap, msg);

    len = vsnprintf(buf, 1024, msg, ap);
    if ((len < 0) || (len > 1024)) 
        buf[1023] = '\0';

    lsd_fatal_error(file, line, buf);

    va_end(ap);
    return;
}

static int _advance_past_brackets (char *tok, char **str)
{
    /* if _single_ opening bracket exists b/w tok and str, push str
     * past first closing bracket to next seperator */
    if (   memchr(tok, '[', *str - tok) != NULL
        && memchr(tok, ']', *str - tok) == NULL ) {
        char *q = strchr(*str, ']');
        if (q && memchr(*str, '[', q - *str) == NULL) {
            *str = q + 1;
            return (1);
        }
    }

    return 0;
}

/* 
 * Helper function for host list string parsing routines 
 * Returns a pointer to the next token; additionally advance *str
 * to the next separator.
 *
 * next_tok was taken directly from pdsh courtesy of Jim Garlick.
 * (with modifications to support bracketed hostlists, i.e.:
 *  xxx[xx,xx,xx] is a single token)
 *
 */
static char * _next_tok(char *sep, char **str)
{
    char *tok;

    /* push str past any leading separators */
    while (**str != '\0' && strchr(sep, **str) != '\0')
        (*str)++;

    if (**str == '\0')
        return NULL;

    /* assign token ptr */
    tok = *str;

    /*
     * Advance str past any separators, but if a separator occurs between
     *  brackets, e.g. foo[0-3,5], then advance str past closing brackets and
     *  try again.
     */
    do {
        /* push str past token and leave pointing to first separator */
        while (**str != '\0' && strchr(sep, **str) == '\0')
            (*str)++;
    } while (_advance_past_brackets (tok, str));

   /* nullify consecutive separators and push str beyond them */
    while (**str != '\0' && strchr(sep, **str) != '\0')
        *(*str)++ = '\0';

    return tok;
}


/* return the number of zeros needed to pad "num" to "width"
 */
static int _zero_padded(unsigned long num, int width)
{
    int n = 1;
    while (num /= 10L)
        n++;
    return width > n ? width - n : 0;
}

/* test whether two format `width' parameters are "equivalent"
 * The width arguments "wn" and "wm" for integers "n" and "m" 
 * are equivalent if:
 *  
 *  o  wn == wm  OR
 *
 *  o  applying the same format width (either wn or wm) to both of  
 *     'n' and 'm' will not change the zero padding of *either* 'm' nor 'n'.
 *
 *  If this function returns 1 (or true), the appropriate width value
 *  (either 'wm' or 'wn') will have been adjusted such that both format
 *  widths are equivalent.
 */
static int _width_equiv(unsigned long n, int *wn, unsigned long m, int *wm)
{
    int npad, nmpad, mpad, mnpad;

    if (wn == wm)
        return 1;

    npad = _zero_padded(n, *wn);
    nmpad = _zero_padded(n, *wm);
    mpad = _zero_padded(m, *wm);
    mnpad = _zero_padded(m, *wn);

    if (npad != nmpad && mpad != mnpad)
        return 0;

    if (npad != nmpad) {
        if (mpad == mnpad) {
            *wm = *wn;
            return 1;
        } else
            return 0;
    } else {        /* mpad != mnpad */
        if (npad == nmpad) {
            *wn = *wm;
            return 1;
        } else
            return 0;
    }

    /* not reached */
}


/* ----[ hostname_t functions ]---- */

/* 
 * return the location of the last char in the hostname prefix
 */
static int host_prefix_end(const char *hostname)
{
    int idx = strlen(hostname) - 1;

    while (idx >= 0 && isdigit((char) hostname[idx])) 
        idx--;
    return idx;
}

static hostname_t hostname_create_with_suffix (const char *hostname, int idx)
{
    hostname_t hn = NULL;
    char *p = '\0';

    assert(hostname != NULL);

    if (!(hn = (hostname_t) malloc(sizeof(*hn))))
        out_of_memory("hostname create");

    if (!(hn->hostname = strdup(hostname))) {
        free(hn);
        out_of_memory("hostname create");
    }

    hn->num = 0;
    hn->prefix = NULL;
    hn->suffix = NULL;

    if (idx == strlen(hostname) - 1) {
        if ((hn->prefix = strdup(hostname)) == NULL) {
            hostname_destroy(hn);
            out_of_memory("hostname prefix create");
        }
        return hn;
    }

    hn->suffix = hn->hostname + idx + 1;
    hn->num = strtoul(hn->suffix, &p, 10);

    if ((*p == '\0') && (hn->num <= MAX_HOST_SUFFIX)) {
        if (!(hn->prefix = malloc((idx + 2) * sizeof(char)))) {
            hostname_destroy(hn);
            out_of_memory("hostname prefix create");
        }
        memcpy(hn->prefix, hostname, idx + 1);
        hn->prefix[idx + 1] = '\0';
    } else {
        if (!(hn->prefix = strdup(hostname))) {
            hostname_destroy(hn);
            out_of_memory("hostname prefix create");
        }
        hn->suffix = NULL;
    }

    return hn;

}


/* 
 * create a hostname_t object from a string hostname
 */
static hostname_t hostname_create(const char *hostname)
{
    int idx = host_prefix_end (hostname);
    return hostname_create_with_suffix (hostname, idx);
}

/* free a hostname object
 */
static void hostname_destroy(hostname_t hn)
{
    if (hn == NULL)
        return;
    hn->suffix = NULL;
    if (hn->hostname)
        free(hn->hostname);
    if (hn->prefix)
        free(hn->prefix);
    free(hn);
}

/* return true if the hostname has a valid numeric suffix 
 */
static int hostname_suffix_is_valid(hostname_t hn)
{
    return hn->suffix != NULL;
}

/* return the width (in characters) of the numeric part of the hostname
 */
static int hostname_suffix_width(hostname_t hn)
{
    assert(hn->suffix != NULL);
    return (int) strlen(hn->suffix);
}


/* ----[ hostrange_t functions ]---- */

/* allocate a new hostrange object 
 */
static hostrange_t hostrange_new(void)
{
    hostrange_t new = (hostrange_t) malloc(sizeof(*new));
    if (!new) 
        out_of_memory("hostrange create");
    return new;
}

/* Create a hostrange_t containing a single host without a valid suffix
 * hr->prefix will represent the entire hostname.
 */
static hostrange_t hostrange_create_single(const char *prefix)
{
    hostrange_t new;

    assert(prefix != NULL);

    if ((new = hostrange_new()) == NULL)
        goto error1;

    if ((new->prefix = strdup(prefix)) == NULL)
        goto error2;

    new->singlehost = 1;
    new->lo = 0L;
    new->hi = 0L;
    new->width = 0;

    return new;

  error2:
    free(new);
  error1:
    out_of_memory("hostrange create single");
}


/* Create a hostrange object with a prefix, hi, lo, and format width
 */
static hostrange_t
hostrange_create(char *prefix, unsigned long lo, unsigned long hi, int width)
{
    hostrange_t new;

    assert(prefix != NULL);

    if ((new = hostrange_new()) == NULL)
        goto error1;

    if ((new->prefix = strdup(prefix)) == NULL)
        goto error2;

    new->lo = lo;
    new->hi = hi;
    new->width = width;

    new->singlehost = 0;

    return new;

  error2:
    free(new);
  error1:
    out_of_memory("hostrange create");
}


/* Return the number of hosts stored in the hostrange object
 */
static unsigned long hostrange_count(hostrange_t hr)
{
    assert(hr != NULL);
    if (hr->singlehost)
        return 1;
    else
        return hr->hi - hr->lo + 1;
}

/* Copy a hostrange object
 */
static hostrange_t hostrange_copy(hostrange_t hr)
{
    assert(hr != NULL);

    if (hr->singlehost)
        return hostrange_create_single(hr->prefix);
    else
        return hostrange_create(hr->prefix, hr->lo, hr->hi,
                    hr->width);
}


/* free memory allocated by the hostrange object
 */
static void hostrange_destroy(hostrange_t hr)
{
    if (hr == NULL)
        return;
    if (hr->prefix)
        free(hr->prefix);
    free(hr);
}

/* hostrange_delete_host() deletes a specific host from the range.
 * If the range is split into two, the greater range is returned,
 * and `hi' of the lesser range is adjusted accordingly. If the
 * highest or lowest host is deleted from a range, NULL is returned
 * and the hostrange hr is adjusted properly.
 */
static hostrange_t hostrange_delete_host(hostrange_t hr, unsigned long n)
{
    hostrange_t new = NULL;

    assert(hr != NULL);
    assert(n >= hr->lo && n <= hr->hi);

    if (n == hr->lo)
        hr->lo++;
    else if (n == hr->hi)
        hr->hi--;
    else {
        if (!(new = hostrange_copy(hr)))
            out_of_memory("hostrange copy");
        hr->hi = n - 1;
        new->lo = n + 1;
    }

    return new;
}

/* hostrange_cmp() is used to sort hostrange objects. It will
 * sort based on the following (in order):
 *  o result of strcmp on prefixes
 *  o if widths are compatible, then: 
 *       sort based on lowest suffix in range
 *    else
 *       sort based on width                     */
static int hostrange_cmp(hostrange_t h1, hostrange_t h2)
{
    int retval;

    assert(h1 != NULL);
    assert(h2 != NULL);

    if ((retval = hostrange_prefix_cmp(h1, h2)) == 0)
        retval = hostrange_width_combine(h1, h2) ?
            h1->lo - h2->lo : h1->width - h2->width;

    return retval;
}


/* compare the prefixes of two hostrange objects. 
 * returns:
 *    < 0   if h1 prefix is less than h2 OR h1 == NULL.
 *
 *      0   if h1's prefix and h2's prefix match, 
 *          UNLESS, either h1 or h2 (NOT both) do not have a valid suffix.
 *
 *    > 0   if h1's prefix is greater than h2's OR h2 == NULL. */
static int hostrange_prefix_cmp(hostrange_t h1, hostrange_t h2)
{
    int retval;
    if (h1 == NULL)
        return 1;
    if (h2 == NULL)
        return -1;

    retval = strcmp(h1->prefix, h2->prefix);
    return retval == 0 ? h2->singlehost - h1->singlehost : retval;
}

/* returns true if h1 and h2 would be included in the same bracketed hostlist.
 * h1 and h2 will be in the same bracketed list iff:
 *
 *  1. h1 and h2 have same prefix
 *  2. neither h1 nor h2 are singlet hosts (i.e. invalid suffix)
 *
 *  (XXX: Should incompatible widths be placed in the same bracketed list?
 *        There's no good reason not to, except maybe aesthetics)
 */
static int hostrange_within_range(hostrange_t h1, hostrange_t h2)
{
    if (hostrange_prefix_cmp(h1, h2) == 0)
        return h1->singlehost || h2->singlehost ? 0 : 1;
    else
        return 0;
}


/* compare two hostrange objects to determine if they are width 
 * compatible,  returns:
 *  1 if widths can safely be combined
 *  0 if widths cannot be safely combined
 */
static int hostrange_width_combine(hostrange_t h0, hostrange_t h1)
{
    assert(h0 != NULL);
    assert(h1 != NULL);

    return _width_equiv(h0->lo, &h0->width, h1->lo, &h1->width);
}


/* Return true if hostrange hr contains no hosts, i.e. hi < lo
 */
static int hostrange_empty(hostrange_t hr)
{
    assert(hr != NULL);
    return ((hr->hi < hr->lo) || (hr->hi == (unsigned long) -1));
}

/* return the string representation of the last host in hostrange hr
 * and remove that host from the range (i.e. decrement hi if possible)
 *
 * Returns NULL if malloc fails OR there are no more hosts left
 */
static char *hostrange_pop(hostrange_t hr)
{
    size_t size = 0;
    char *host = NULL;

    assert(hr != NULL);

    if (hr->singlehost) {
        hr->lo++;    /* effectively set count == 0 */
        host = strdup(hr->prefix);
    } else if (hostrange_count(hr) > 0) {
        size = strlen(hr->prefix) + hr->width + 16;    
        if (!(host = (char *) malloc(size * sizeof(char))))
            out_of_memory("hostrange pop");
        snprintf(host, size, "%s%0*lu", hr->prefix, 
             hr->width, hr->hi--);
    }

    return host;
}

/* Same as hostrange_pop(), but remove host from start of range */
static char *hostrange_shift(hostrange_t hr)
{
    size_t size = 0;
    char *host = NULL;

    assert(hr != NULL);

    if (hr->singlehost) {
        hr->lo++;
        if (!(host = strdup(hr->prefix)))
            out_of_memory("hostrange shift");
    } else if (hostrange_count(hr) > 0) {
        size = strlen(hr->prefix) + hr->width + 16;
        if (!(host = (char *) malloc(size * sizeof(char))))
            out_of_memory("hostrange shift");
        snprintf(host, size, "%s%0*lu", hr->prefix,
             hr->width, hr->lo++);
    }

    return host;
}


/* join two hostrange objects.
 *
 * returns:
 *
 * -1 if ranges do not overlap (including incompatible zero padding)
 *  0 if ranges join perfectly
 * >0 number of hosts that were duplicated in  h1 and h2 
 *
 * h2 will be coalesced into h1 if rc >= 0
 *
 * it is assumed that h1->lo <= h2->lo, i.e. hr1 <= hr2
 *
 */
static int hostrange_join(hostrange_t h1, hostrange_t h2)
{
    int duplicated = -1;

    assert(h1 != NULL);
    assert(h2 != NULL);
    assert(hostrange_cmp(h1, h2) <= 0);

    if (hostrange_prefix_cmp(h1, h2) == 0 &&
        hostrange_width_combine(h1, h2)) {

        if (h1->singlehost && h2->singlehost) {    /* matching singlets  */
            duplicated = 1;
        } else if (h1->hi == h2->lo - 1) {    /* perfect join       */
            h1->hi = h2->hi;
            duplicated = 0;
        } else if (h1->hi >= h2->lo) {    /* some duplication   */
            if (h1->hi < h2->hi) {
                duplicated = h1->hi - h2->lo + 1;
                h1->hi = h2->hi;
            } else
                duplicated = hostrange_count(h2);
        }
    }

    return duplicated;
}

/* hostrange intersect returns the intersection (common hosts)
 * of hostrange objects h1 and h2. If there is no intersection,
 * NULL is returned.
 *
 * It is assumed that h1 <= h2 (i.e. h1->lo <= h2->lo)
 */
static hostrange_t hostrange_intersect(hostrange_t h1, hostrange_t h2)
{
    hostrange_t new = NULL;

    assert(h1 != NULL);
    assert(h2 != NULL);

    if (h1->singlehost || h2->singlehost)
        return NULL;

    assert(hostrange_cmp(h1, h2) <= 0);

    if ((hostrange_prefix_cmp(h1, h2) == 0)
        && (h1->hi > h2->lo) 
        && (hostrange_width_combine(h1, h2))) {

        if (!(new = hostrange_copy(h1)))
            return NULL;
        new->lo = h2->lo;
        new->hi = h2->hi < h1->hi ? h2->hi : h1->hi;
    }

    return new;
}

/* return offset of hn if it is in the hostlist or
 *        -1 if not.
 */
static int hostrange_hn_within(hostrange_t hr, hostname_t hn)
{
    int len_hr;
    int len_hn;
    int width;

    if (hr->singlehost) {
        /*
         *  If the current hostrange [hr] is a `singlehost' (no valid
         *   numeric suffix (lo and hi)), then the hostrange [hr]
         *   stores just one host with name == hr->prefix.
         *
         *  Thus the full hostname in [hn] must match hr->prefix, in
         *   which case we return true. Otherwise, there is no
         *   possibility that [hn] matches [hr].
         */
        if (strcmp (hn->hostname, hr->prefix) == 0)
            return 0;
        else
            return -1;
    }

    /*
     *  Now we know [hr] is not a "singlehost", so hostname
     *   better have a valid numeric suffix, or there is no
     *   way we can match
     */
    if (!hostname_suffix_is_valid (hn))
        return -1;

    len_hn = strlen (hn->prefix);

    /*
     *  If hostrange and hostname prefixes don't match to at least
     *   the length of the hostname object (which will have the min
     *   possible prefix length), then there is no way the hostname
     *   falls within the range [hr].
     */
    if (strncmp (hr->prefix, hn->prefix, len_hn) != 0)
        return -1;

    /*
     *  Now we know hostrange and hostname prefixes match up to the
     *   length of the hostname prefix.  If the hostrange and hostname
     *   prefix lengths do not match (specifically if the hostname prefix
     *   length is less than the hostrange prefix length) and the
     *   hostrange prefix contains trailing digits, then it might be
     *   the case that the hostrange was created by forcing the prefix
     *   to contain digits a la f00[1-2]. So we try adjusting the
     *   hostname with the longer prefix and calling this function
     *   again with the new hostname. (Yes, this is ugly, sorry)
     */
    len_hr = strlen (hr->prefix);
    width = hostname_suffix_width (hn);

    if ((len_hn < len_hr)
         && (width > 1)
         && (isdigit (hr->prefix [len_hr - 1]))
         && (hr->prefix [len_hn] == hn->suffix[0]) ) {
        int rc;
        /*
         *  Create new hostname object with its prefix offset by one
         */
        hostname_t h = hostname_create_with_suffix (hn->hostname, len_hn);
        /*
         *  Recursive call :-o
         */
        rc = hostrange_hn_within (hr, h);
        hostname_destroy (h);
        return rc;
    }


    /*
     *  Finally, check whether [hn], with a valid numeric suffix,
     *   falls within the range of [hr].
     */
    if (hn->num <= hr->hi && hn->num >= hr->lo) {
        int width = hostname_suffix_width (hn);
        if (!_width_equiv(hr->lo, &hr->width, hn->num, &width))
            return -1;
        return (hn->num - hr->lo);
    }

    return -1;
}


/* copy a string representation of the hostrange hr into buffer buf,
 * writing at most n chars including NUL termination
 */
static size_t
hostrange_to_string(hostrange_t hr, size_t n, char *buf, char *separator)
{
    unsigned long i;
    int truncated = 0;
    int len = 0;
    char sep = separator == NULL ? ',' : separator[0];

    if (n == 0)
        return 0;

    if (hr->singlehost)
        return snprintf(buf, n, "%s", hr->prefix);

    for (i = hr->lo; i <= hr->hi; i++) {
        size_t m = (n - len) <= n ? n - len : 0; /* check for < 0 */
        int ret = snprintf(buf + len, m, "%s%0*lu",
                   hr->prefix, hr->width, i);
        if (ret < 0 || ret >= m) {
            len = n;
            truncated = 1;
            break;
        }
        len+=ret;
        buf[len++] = sep;
    }

    if (truncated) {
        buf[n-1] = '\0';
        return -1;
    } else {
        /* back up over final separator */
        buf[--len] = '\0';
        return len;
    }
}

/* Place the string representation of the numeric part of hostrange into buf
 * writing at most n chars including NUL termination.
 */
static size_t hostrange_numstr(hostrange_t hr, size_t n, char *buf)
{
    int len = 0;

    assert(buf != NULL);

    if (hr->singlehost || n == 0)
        return 0;

    len = snprintf(buf, n, "%0*lu", hr->width, hr->lo);

    if ((len >= 0) && (len < n) && (hr->lo < hr->hi)) {
        int len2 = snprintf(buf+len, n-len, "-%0*lu", hr->width, hr->hi);
        if (len2 < 0) 
            len = -1;
        else
            len += len2;
    }

    return len;
}


/* ----[ hostlist functions ]---- */

/* Create a new hostlist object. 
 * Returns an empty hostlist, or NULL if memory allocation fails.
 */
static hostlist_t hostlist_new(void)
{
    int i;
    hostlist_t new = (hostlist_t) malloc(sizeof(*new));
    if (!new)
        goto fail1;

    assert(new->magic = HOSTLIST_MAGIC);
    mutex_init(&new->mutex);

    new->hr = (hostrange_t *) malloc(HOSTLIST_CHUNK * sizeof(hostrange_t));
    if (!new->hr)
        goto fail2;

    /* set entries in hostrange array to NULL */
    for (i = 0; i < HOSTLIST_CHUNK; i++)
        new->hr[i] = NULL;

    new->size = HOSTLIST_CHUNK;
    new->nranges = 0;
    new->nhosts = 0;
    new->ilist = NULL;
    return new;

  fail2:
    free(new);
  fail1:
    out_of_memory("hostlist_create");
}


/* Resize the internal array used to store the list of hostrange objects.
 *
 * returns 1 for a successful resize,
 *         0 if call to _realloc fails    
 *
 * It is assumed that the caller has the hostlist hl locked 
 */
static int hostlist_resize(hostlist_t hl, size_t newsize)
{
    int i;
    size_t oldsize;
    assert(hl != NULL);
    assert(hl->magic == HOSTLIST_MAGIC);
    oldsize = hl->size;
    hl->size = newsize;
    hl->hr = realloc((void *) hl->hr, hl->size*sizeof(hostrange_t));
    if (!(hl->hr)) 
        return 0;

    for (i = oldsize; i < newsize; i++)
        hl->hr[i] = NULL;

    return 1;
}

/* Resize hostlist by one HOSTLIST_CHUNK
 * Assumes that hostlist hl is locked by caller
 */
static int hostlist_expand(hostlist_t hl)
{
    if (!hostlist_resize(hl, hl->size + HOSTLIST_CHUNK))
        return 0;
    else
        return 1;
}

/* Push a hostrange object onto hostlist hl
 * Returns the number of hosts successfully pushed onto hl
 * or -1 if there was an error allocating memory
 */
static int hostlist_push_range(hostlist_t hl, hostrange_t hr)
{
    hostrange_t tail;
    int retval;

    assert(hr != NULL);
    LOCK_HOSTLIST(hl);

    tail = (hl->nranges > 0) ? hl->hr[hl->nranges-1] : hl->hr[0];

    if (hl->size == hl->nranges && !hostlist_expand(hl))
        goto error;

    if (hl->nranges > 0
        && hostrange_prefix_cmp(tail, hr) == 0
        && tail->hi == hr->lo - 1
        && hostrange_width_combine(tail, hr)) {
        tail->hi = hr->hi;
    } else {
        if ((hl->hr[hl->nranges++] = hostrange_copy(hr)) == NULL)
            goto error;
    }

    retval = hl->nhosts += hostrange_count(hr);

    UNLOCK_HOSTLIST(hl);

    return retval;

  error:
    UNLOCK_HOSTLIST(hl);
    return -1;
}



/* Same as hostlist_push_range() above, but prefix, lo, hi, and width
 * are passed as args 
 */
static int
hostlist_push_hr(hostlist_t hl, char *prefix, unsigned long lo,
         unsigned long hi, int width)
{
    hostrange_t hr = hostrange_create(prefix, lo, hi, width);
    int retval = hostlist_push_range(hl, hr);
    hostrange_destroy(hr);
    return retval;
}

/* Insert a range object hr into position n of the hostlist hl
 * Assumes that hl->mutex is already held by calling process
 */
static int hostlist_insert_range(hostlist_t hl, hostrange_t hr, int n)
{
    int i;
    hostrange_t tmp;
    hostlist_iterator_t hli;

    assert(hl != NULL);
    assert(hl->magic == HOSTLIST_MAGIC);
    assert(hr != NULL);

    if (n > hl->nranges)
        return 0;

    if (hl->size == hl->nranges && !hostlist_expand(hl))
        return 0;

    /* copy new hostrange into slot "n" in array */
    tmp = hl->hr[n];
    hl->hr[n] = hostrange_copy(hr);

    /* push remaining hostrange entries up */
    for (i = n + 1; i < hl->nranges + 1; i++) {
        hostrange_t last = hl->hr[i];
        hl->hr[i] = tmp;
        tmp = last;
    }
    hl->nranges++;

    /* adjust hostlist iterators if needed */
    for (hli = hl->ilist; hli; hli = hli->next) {
        if (hli->idx >= n)
            hli->hr = hli->hl->hr[++hli->idx];
    }

    return 1;
}

/* Delete the range at position n in the range array
 * Assumes the hostlist lock is already held.
 */
static void hostlist_delete_range(hostlist_t hl, int n)
{
    int i;
    hostrange_t old;

    assert(hl != NULL);
    assert(hl->magic == HOSTLIST_MAGIC);
    assert(n < hl->nranges && n >= 0);

    old = hl->hr[n];
    for (i = n; i < hl->nranges - 1; i++)
        hl->hr[i] = hl->hr[i + 1];
    hl->nranges--;
    hl->hr[hl->nranges] = NULL;
    hostlist_shift_iterators(hl, n, 0, 1);

    /* XXX caller responsible for adjusting nhosts */
    /* hl->nhosts -= hostrange_count(old) */

    hostrange_destroy(old);
}

#if WANT_RECKLESS_HOSTRANGE_EXPANSION

/* The reckless hostrange expansion function.
 * See comment in hostlist.h:hostlist_create() for more info on
 * the different choices for hostlist notation.
 */
hostlist_t _hostlist_create(const char *hostlist, char *sep, char *r_op)
{
    char *str, *orig;
    char *tok, *cur;
    int high, low, fmt = 0;
    char prefix[256] = "";
    int pos = 0;
    int error = 0;
    char range_op = r_op[0];/* XXX support > 1 char range ops in future? */

    hostlist_t new = hostlist_new();

    orig = str = strdup(hostlist);

    /* return an empty list if an empty string was passed in */
    if (str == NULL || strlen(str) == 0)
        goto done;

    /* Use hostlist_create_bracketed if we see "[" */
    if (strchr(str, '[') != NULL)
        return _hostlist_create_bracketed(hostlist, sep, r_op);

    while ((tok = _next_tok(sep, &str)) != NULL) {

        /* save the current string for error messages */
        cur = tok;

        high = low = 0;

        /* find end of alpha part 
         *   do this by finding last occurence of range_op in str */
        pos = strlen(tok) - 1;
        if (strstr(tok, r_op) != '\0') {
            while (pos >= 0 && (char) tok[pos] != range_op) 
                pos--;
        }

        /* now back up past any digits */
        while (pos >= 0 && isdigit((char) tok[--pos])) {;}

        /* Check for valid x-y range (x must be a digit) 
         *   Reset pos if the range is not valid         */
        if (!isdigit((char) tok[++pos]))
            pos = strlen(tok) - 1;

        /* create prefix string 
         * if prefix will be zero length, but prefix already exists
         * use the previous prefix and fmt
         */
        if ((pos > 0) || (prefix[0] == '\0')) {
            memcpy(prefix, tok, (size_t) pos * sizeof(char));
            prefix[pos] = '\0';

            /* push pointer past prefix */
            tok += pos;

            /* count number of digits for ouput fmt */
            for (fmt = 0; isdigit(tok[fmt]); ++fmt) {;}

            if (fmt == 0)
                error = 1;

        } else
            tok += pos;

        /* get lower bound */
        low = strtoul(tok, (char **) &tok, 10);

        if (*tok == range_op) {    /* now get range upper bound */
            /* push pointer past range op */
            ++tok;

            /* find length of alpha part */
            for (pos = 0; tok[pos] && !isdigit(tok[pos]); ++pos) {;}

            /* alpha part must match prefix or error
             * this could mean we've got something like "rtr1-a2"
             * so just record an error
             */
            if (pos > 0) {
                if (pos != strlen(prefix) ||
                    strncmp(prefix, tok, pos) != 0)
                    error = 1;
            }

            if (*tok != '\0')
                tok += pos;

            /* make sure we have digits to the end */
            for (pos = 0; tok[pos] && isdigit((char) tok[pos]); ++pos) {;}

            if (pos > 0) {    /* we have digits to process */
                high = strtoul(tok, (char **) &tok, 10);
            } else {    /* bad boy, no digits */
                error = 1;
            }

            if ((low > high) || (high - low > MAX_RANGE))
                error = 1;

        } else {    /* single value */
            high = 0;    /* special case, ugh. */
        }

        /* error if: 
         * 1. we are not at end of string
         * 2. upper bound equals lower bound
         */
        if (*tok != '\0' || high == low)
            error = 1;

        if (error) {    /* assume this is not a range on any error */
            hostlist_push_host(new, cur);
        } else {
            if (high < low)
                high = low;
            hostlist_push_hr(new, prefix, low, high, fmt);
        }

        error = 0;
    }

  done:
    free(orig);

    return new;
}

#else                /* !WANT_RECKLESS_HOSTRANGE_EXPANSION */

hostlist_t _hostlist_create(const char *hostlist, char *sep, char *r_op) 
{
    return _hostlist_create_bracketed(hostlist, sep, r_op);
}

#endif                /* WANT_RECKLESS_HOSTRANGE_EXPANSION */

struct _range {
    unsigned long lo, hi;
    int width;
};

/* Grab a single range from str 
 * returns 1 if str contained a valid number or range,
 *         0 if conversion of str to a range failed.
 */
static int _parse_single_range(const char *str, struct _range *range)
{
    char *p, *q;
    char *orig = strdup(str);
    if (!orig) 
        seterrno_ret(ENOMEM, 0);

    if ((p = strchr(str, '-'))) {
        *p++ = '\0';
        if (*p == '-')     /* do NOT allow negative numbers */
            goto error;
    }
    range->lo = strtoul(str, &q, 10);
    if (q == str) 
        goto error;

    range->hi = (p && *p) ? strtoul(p, &q, 10) : range->lo;

    if (q == p || *q != '\0') 
        goto error;

    if (range->lo > range->hi) 
        goto error;

    if (range->hi - range->lo + 1 > MAX_RANGE ) {
        _error(__FILE__, __LINE__, "Too many hosts in range `%s'", orig);
        free(orig);
        seterrno_ret(ERANGE, 0);
    }

    free(orig);
    range->width = strlen(str);
    return 1;

  error:
    _error(__FILE__, __LINE__, "Invalid range: `%s'", orig);
    free(orig);
    seterrno_ret(EINVAL, 0);
}


/*
 * Convert 'str' containing comma separated digits and ranges into an array
 *  of struct _range types (max 'len' elements).  
 *
 * Return number of ranges created, or -1 on error.
 */
static int _parse_range_list(char *str, struct _range *ranges, int len)
{
    char *p;
    int count = 0;

    while (str) {
        if (count == len)
            return -1;
        if ((p = strchr(str, ',')))
            *p++ = '\0';
        if (!_parse_single_range(str, &ranges[count++])) 
            return -1;  
        str = p;
    }
    return count;
}

static void
_push_range_list(hostlist_t hl, char *pfx, struct _range *rng,
             int n)
{
    int i;
    for (i = 0; i < n; i++) {
        hostlist_push_hr(hl, pfx, rng->lo, rng->hi, rng->width);
        rng++;
    }
}

static void
_push_range_list_with_suffix(hostlist_t hl, char *pfx, char *sfx, 
                             struct _range *rng, int n)
{
    int i;
    unsigned long j;
    for (i = 0; i < n; i++) {
        for (j = rng->lo; j <= rng->hi; j++) {
            char host[4096];
            hostrange_t hr;
            snprintf (host, 4096, "%s%0*lu%s", pfx, rng->width, j, sfx);
            hr = hostrange_create_single (host);
            hostlist_push_range (hl, hr);
            /*
             * hr is copied in hostlist_push_range. Need to free here.
             */
            hostrange_destroy (hr);
        }
        rng++;
    }
}

/*
 * Create a hostlist from a string with brackets '[' ']' to aid 
 * detection of ranges and compressed lists
 */
static hostlist_t 
_hostlist_create_bracketed(const char *hostlist, char *sep, char *r_op)
{
    hostlist_t new = hostlist_new();
    struct _range ranges[MAX_RANGES];
    int nr, err;
    char *p, *tok, *str, *orig;
    char cur_tok[1024];

    if (hostlist == NULL)
        return new;

    if (!(orig = str = strdup(hostlist))) {
        hostlist_destroy(new);
        return NULL;
    }

    while ((tok = _next_tok(sep, &str)) != NULL) {
        strncpy(cur_tok, tok, 1024);

        if ((p = strchr(tok, '[')) != NULL) {
            char *q, *prefix = tok;
            *p++ = '\0';

            if ((q = strchr(p, ']'))) {
                *q = '\0';
                nr = _parse_range_list(p, ranges, MAX_RANGES);
                if (nr < 0) 
                    goto error;

                if (*(++q) != '\0')
                    _push_range_list_with_suffix (new, prefix, q, ranges, nr);
                else
                    _push_range_list(new, prefix, ranges, nr);

                
            } else
                hostlist_push_host(new, cur_tok);

        } else
            hostlist_push_host(new, cur_tok);
    }

    free(orig);
    return new;

  error:
    err = errno;
    hostlist_destroy(new);
    free(orig);
    seterrno_ret(err, NULL);
}



hostlist_t hostlist_create(const char *str)
{
    return _hostlist_create(str, "\t, ", "-");
}


hostlist_t hostlist_copy(const hostlist_t hl)
{
    int i;
    hostlist_t new;

    if (hl == NULL)
        return NULL;

    LOCK_HOSTLIST(hl);
    if (!(new = hostlist_new()))
        goto done;

    new->nranges = hl->nranges;
    new->nhosts = hl->nhosts;
    if (new->nranges > new->size)
        hostlist_resize(new, new->nranges);

    for (i = 0; i < hl->nranges; i++)
        new->hr[i] = hostrange_copy(hl->hr[i]);

  done:
    UNLOCK_HOSTLIST(hl);
    return new;
}


void hostlist_destroy(hostlist_t hl)
{
    int i;
    if (hl == NULL)
        return;
    LOCK_HOSTLIST(hl);
    while (hl->ilist) {
        mutex_unlock(&hl->mutex);
        hostlist_iterator_destroy(hl->ilist);
        mutex_lock(&hl->mutex);
    }
    for (i = 0; i < hl->nranges; i++)
        hostrange_destroy(hl->hr[i]);
    free(hl->hr);
    assert(hl->magic = 0x1);
    UNLOCK_HOSTLIST(hl);
    mutex_destroy(&hl->mutex);
    free(hl);
}


int hostlist_push(hostlist_t hl, const char *hosts)
{
    hostlist_t new;
    int retval;
    if (hosts == NULL)
        return 0;
    new = hostlist_create(hosts);
    if (!new)
        return 0;
    mutex_lock(&new->mutex);
    retval = new->nhosts;
    mutex_unlock(&new->mutex);
    hostlist_push_list(hl, new);
    hostlist_destroy(new);
    return retval;
}

int hostlist_push_host(hostlist_t hl, const char *str)
{
    hostrange_t hr;
    hostname_t hn;

    if (str == NULL)
        return 0;

    hn = hostname_create(str);

    if (hostname_suffix_is_valid(hn)) {
        hr = hostrange_create(hn->prefix, hn->num, hn->num,
                      hostname_suffix_width(hn));
    } else
        hr = hostrange_create_single(str);

    hostlist_push_range(hl, hr);

    hostrange_destroy(hr);
    hostname_destroy(hn);

    return 1;
}

int hostlist_push_list(hostlist_t h1, hostlist_t h2)
{
    int i, n = 0;

    if (h2 == NULL)
        return 0;

    LOCK_HOSTLIST(h2);

    for (i = 0; i < h2->nranges; i++)
        n += hostlist_push_range(h1, h2->hr[i]);

    UNLOCK_HOSTLIST(h2);

    return n;
}


char *hostlist_pop(hostlist_t hl)
{
    char *host = NULL;

    LOCK_HOSTLIST(hl);
    if (hl->nhosts > 0) {
        hostrange_t hr = hl->hr[hl->nranges - 1];
        host = hostrange_pop(hr);
        hl->nhosts--;
        if (hostrange_empty(hr)) {
            hostrange_destroy(hl->hr[--hl->nranges]);
            hl->hr[hl->nranges] = NULL;
        }
    }
    UNLOCK_HOSTLIST(hl);
    return host;
}

/* find all iterators affected by a shift (or deletion) at 
 * hl->hr[idx], depth, with the deletion of n ranges */
static void
hostlist_shift_iterators(hostlist_t hl, int idx, int depth, int n)
{
    hostlist_iterator_t i;
    for (i = hl->ilist; i; i = i->next) {
        if (n == 0) {
            if (i->idx == idx && i->depth >= depth)
                i->depth = i->depth > -1 ? i->depth - 1 : -1;
        } else {
            if (i->idx >= idx) {
                if ((i->idx -= n) >= 0)
                    i->hr = i->hl->hr[i->idx];
                else
                    hostlist_iterator_reset(i);
            }
        }
    }
}

char *hostlist_shift(hostlist_t hl)
{
    char *host = NULL;

    LOCK_HOSTLIST(hl);

    if (hl->nhosts > 0) {
        hostrange_t hr = hl->hr[0];

        host = hostrange_shift(hr);
        hl->nhosts--;

        if (hostrange_empty(hr)) {
            hostlist_delete_range(hl, 0);
            /* hl->nranges--; */
        } else
            hostlist_shift_iterators(hl, 0, 0, 0);
    }

    UNLOCK_HOSTLIST(hl);

    return host;
}


char *hostlist_pop_range(hostlist_t hl)
{
    int i;
    char buf[MAXHOSTRANGELEN + 1];
    hostlist_t hltmp;
    hostrange_t tail;

    LOCK_HOSTLIST(hl);
    if (hl->nranges < 1 || !(hltmp = hostlist_new())) {
        UNLOCK_HOSTLIST(hl);
        return NULL;
    }

    i = hl->nranges - 2;
    tail = hl->hr[hl->nranges - 1];
    while (i >= 0 && hostrange_within_range(tail, hl->hr[i]))
        i--;

    for (i++; i < hl->nranges; i++) {
        hostlist_push_range(hltmp, hl->hr[i]);
        hostrange_destroy(hl->hr[i]);
        hl->hr[i] = NULL;
    }
    hl->nhosts -= hltmp->nhosts;
    hl->nranges -= hltmp->nranges;

    UNLOCK_HOSTLIST(hl);
    hostlist_ranged_string(hltmp, MAXHOSTRANGELEN, buf);
    hostlist_destroy(hltmp);
    return strdup(buf);
}


char *hostlist_shift_range(hostlist_t hl)
{
    int i;
    char buf[1024];
    hostlist_t hltmp = hostlist_new();
    if (!hltmp)
        return NULL;

    LOCK_HOSTLIST(hl);

    if (hl->nranges == 0) {
        hostlist_destroy(hltmp);
        UNLOCK_HOSTLIST(hl);
        return NULL;
    }

    i = 0;
    do {
        hostlist_push_range(hltmp, hl->hr[i]);
        hostrange_destroy(hl->hr[i]);
    } while ( (++i < hl->nranges) 
            && hostrange_within_range(hltmp->hr[0], hl->hr[i]) );

    hostlist_shift_iterators(hl, i, 0, hltmp->nranges);

    /* shift rest of ranges back in hl */
    for (; i < hl->nranges; i++) {
        hl->hr[i - hltmp->nranges] = hl->hr[i];
        hl->hr[i] = NULL;
    }
    hl->nhosts -= hltmp->nhosts;
    hl->nranges -= hltmp->nranges;

    UNLOCK_HOSTLIST(hl);

    hostlist_ranged_string(hltmp, 1024, buf);
    hostlist_destroy(hltmp);

    return strdup(buf);
}

/* XXX: Note: efficiency improvements needed */
int hostlist_delete(hostlist_t hl, const char *hosts)
{
    int n = 0;
    char *hostname = NULL;
    hostlist_t hltmp;

    if (!(hltmp = hostlist_create(hosts)))
        seterrno_ret(EINVAL, 0);

    while ((hostname = hostlist_pop(hltmp)) != NULL) {
        n += hostlist_delete_host(hl, hostname);
        free(hostname);
    }
    hostlist_destroy(hltmp);

    return n;
}


/* XXX watch out! poor implementation follows! (fix it at some point) */
int hostlist_delete_host(hostlist_t hl, const char *hostname)
{
    int n = hostlist_find(hl, hostname);
    if (n >= 0)
        hostlist_delete_nth(hl, n);
    return n >= 0 ? 1 : 0;
}


static char *
_hostrange_string(hostrange_t hr, int depth)
{
    char buf[MAXHOSTNAMELEN + 16];
    int  len = snprintf(buf, MAXHOSTNAMELEN + 15, "%s", hr->prefix);

    if (!hr->singlehost)
        snprintf(buf+len, MAXHOSTNAMELEN+15 - len, "%0*lu", 
                 hr->width, hr->lo + depth);
    return strdup(buf);
}

char * hostlist_nth(hostlist_t hl, int n)
{
    char *host = NULL;
    int   i, count;

    LOCK_HOSTLIST(hl);
    count = 0;
    for (i = 0; i < hl->nranges; i++) {
        int num_in_range = hostrange_count(hl->hr[i]);

        if (n <= (num_in_range - 1 + count)) {
            host = _hostrange_string(hl->hr[i], n - count);
            break;
        } else
            count += num_in_range;
    }

    UNLOCK_HOSTLIST(hl);

    return host;
}


int hostlist_delete_nth(hostlist_t hl, int n)
{
    int i, count;

    LOCK_HOSTLIST(hl);
    assert(n >= 0 && n <= hl->nhosts);

    count = 0;

    for (i = 0; i < hl->nranges; i++) {
        int num_in_range = hostrange_count(hl->hr[i]);
        hostrange_t hr = hl->hr[i];

        if (n <= (num_in_range - 1 + count)) {
            unsigned long num = hr->lo + n - count;
            hostrange_t new;

            if (hr->singlehost) { /* this wasn't a range */
                hostlist_delete_range(hl, i);
            } else if ((new = hostrange_delete_host(hr, num))) {
                hostlist_insert_range(hl, new, i + 1);
                hostrange_destroy(new);
            } else if (hostrange_empty(hr))
                hostlist_delete_range(hl, i);

            goto done;
        } else
            count += num_in_range;

    }

  done:
    hl->nhosts--;
    UNLOCK_HOSTLIST(hl);
    return 1;
}

int hostlist_count(hostlist_t hl)
{
    int retval;
    LOCK_HOSTLIST(hl);
    retval = hl->nhosts;
    UNLOCK_HOSTLIST(hl);
    return retval;
}

int hostlist_find(hostlist_t hl, const char *hostname)
{
    int i, count, ret = -1;
    hostname_t hn;

    if (!hostname)
        return -1;

    hn = hostname_create(hostname);

    LOCK_HOSTLIST(hl);

    for (i = 0, count = 0; i < hl->nranges; i++) {
        int offset = hostrange_hn_within(hl->hr[i], hn);
        if (offset >= 0) {
            ret = count + offset;
            break;
        }
        else
            count += hostrange_count(hl->hr[i]);
    }

    UNLOCK_HOSTLIST(hl);
    hostname_destroy(hn);
    return ret;
}

/* hostrange compare with void * arguments to allow use with 
 * libc qsort()
 */
int _cmp(const void *hr1, const void *hr2)
{
    hostrange_t *h1 = (hostrange_t *) hr1;
    hostrange_t *h2 = (hostrange_t *) hr2;
    return hostrange_cmp((hostrange_t) * h1, (hostrange_t) * h2);
}


void hostlist_sort(hostlist_t hl)
{
    hostlist_iterator_t i;
    LOCK_HOSTLIST(hl);

    if (hl->nranges <= 1) {
        UNLOCK_HOSTLIST(hl);
        return;
    }

    qsort(hl->hr, hl->nranges, sizeof(hostrange_t), &_cmp);

    /* reset all iterators */
    for (i = hl->ilist; i; i = i->next)
        hostlist_iterator_reset(i);

    UNLOCK_HOSTLIST(hl);

    hostlist_coalesce(hl);

}


/* search through hostlist for ranges that can be collapsed 
 * does =not= delete any hosts
 */
static void hostlist_collapse(hostlist_t hl)
{
    int i;

    LOCK_HOSTLIST(hl);
    for (i = hl->nranges - 1; i > 0; i--) {
        hostrange_t hprev = hl->hr[i - 1];
        hostrange_t hnext = hl->hr[i];

        if (hostrange_prefix_cmp(hprev, hnext) == 0 &&
            hprev->hi == hnext->lo - 1 &&
            hostrange_width_combine(hprev, hnext)) {
            hprev->hi = hnext->hi;
            hostlist_delete_range(hl, i);
        }
    }
    UNLOCK_HOSTLIST(hl);
}

/* search through hostlist (hl) for intersecting ranges 
 * split up duplicates and coalesce ranges where possible
 */
static void hostlist_coalesce(hostlist_t hl)
{
    int i, j;
    hostrange_t new;

    LOCK_HOSTLIST(hl);

    for (i = hl->nranges - 1; i > 0; i--) {

        new = hostrange_intersect(hl->hr[i - 1], hl->hr[i]);

        if (new) {
            hostrange_t hprev = hl->hr[i - 1];
            hostrange_t hnext = hl->hr[i];
            j = i;

            if (new->hi < hprev->hi)
                hnext->hi = hprev->hi;

            hprev->hi = new->lo;
            hnext->lo = new->hi;

            if (hostrange_empty(hprev))
                hostlist_delete_range(hl, i);

            while (new->lo <= new->hi) {
                hostrange_t hr = hostrange_create( new->prefix,
                                                   new->lo, new->lo,
                                                   new->width );

                if (new->lo > hprev->hi)
                    hostlist_insert_range(hl, hr, j++);

                if (new->lo < hnext->lo)
                    hostlist_insert_range(hl, hr, j++);

                hostrange_destroy(hr);

                new->lo++;
            }
            i = hl->nranges;
            hostrange_destroy(new);
        }
    }
    UNLOCK_HOSTLIST(hl);

    hostlist_collapse(hl);

}

/* attempt to join ranges at loc and loc-1 in a hostlist  */
/* delete duplicates, return the number of hosts deleted  */
/* assumes that the hostlist hl has been locked by caller */
/* returns -1 if no range join occurred */
static int _attempt_range_join(hostlist_t hl, int loc)
{
    int ndup;
    assert(hl != NULL);
    assert(hl->magic == HOSTLIST_MAGIC);
    assert(loc > 0);
    assert(loc < hl->nranges);
    ndup = hostrange_join(hl->hr[loc - 1], hl->hr[loc]);
    if (ndup >= 0) {
        hostlist_delete_range(hl, loc);
        hl->nhosts -= ndup;
    }
    return ndup;
}

void hostlist_uniq(hostlist_t hl)
{
    int i = 1;
    hostlist_iterator_t hli;
    LOCK_HOSTLIST(hl);
    if (hl->nranges <= 1) {
        UNLOCK_HOSTLIST(hl);
        return;
    }
    qsort(hl->hr, hl->nranges, sizeof(hostrange_t), &_cmp);

    while (i < hl->nranges) {
        if (_attempt_range_join(hl, i) < 0) /* No range join occurred */
            i++;
    }

    /* reset all iterators */
    for (hli = hl->ilist; hli; hli = hli->next)
        hostlist_iterator_reset(hli);

    UNLOCK_HOSTLIST(hl);
}


ssize_t hostlist_deranged_string(hostlist_t hl, size_t n, char *buf)
{
    int i;
    int len = 0;
    int truncated = 0;

    LOCK_HOSTLIST(hl);
    for (i = 0; i < hl->nranges; i++) {
        size_t m = (n - len) <= n ? n - len : 0;
        int ret = hostrange_to_string(hl->hr[i], m, buf + len, ",");
        if (ret < 0 || ret > m) {
            len = n;
            truncated = 1;
            break;
        }
        len+=ret;
        buf[len++] = ',';
    }
    UNLOCK_HOSTLIST(hl);

    buf[len > 0 ? --len : 0] = '\0';
    if (len == n)
        truncated = 1;

    return truncated ? -1 : len;
}

/* return true if a bracket is needed for the range at i in hostlist hl */
static int _is_bracket_needed(hostlist_t hl, int i)
{
    hostrange_t h1 = hl->hr[i];
    hostrange_t h2 = i < hl->nranges - 1 ? hl->hr[i + 1] : NULL;
    return hostrange_count(h1) > 1 || hostrange_within_range(h1, h2);
}

/* write the next bracketed hostlist, i.e. prefix[n-m,k,...]
 * into buf, writing at most n chars including the terminating '\0'
 *
 * leaves start pointing to one past last range object in bracketed list,
 * and returns the number of bytes written into buf.
 *
 * Assumes hostlist is locked.
 */
static int
_get_bracketed_list(hostlist_t hl, int *start, const size_t n, char *buf)
{
    hostrange_t *hr = hl->hr;
    int i = *start;
    int m, len = 0;
    int bracket_needed = _is_bracket_needed(hl, i);

    len = snprintf(buf, n, "%s", hr[i]->prefix);

    if ((len < 0) || (len > n))
        return n; /* truncated, buffer filled */

    if (bracket_needed && len < n && len >= 0)
        buf[len++] = '[';

    do {
        m = (n - len) <= n ? n - len : 0;
        len += hostrange_numstr(hr[i], m, buf + len);
        if (len >= n)
            break;
        if (bracket_needed) /* Only need commas inside brackets */
            buf[len++] = ',';
    } while (++i < hl->nranges && hostrange_within_range(hr[i], hr[i-1]));

    if (bracket_needed && len < n && len > 0) {

        /* Add trailing bracket (change trailing "," from above to "]" */
        buf[len - 1] = ']';

        /* NUL terminate for safety, but do not add terminator to len */
        buf[len]   = '\0';

    } else if (len >= n) {
        if (n > 0)
            buf[n-1] = '\0';

    } else {
        /* If len is > 0, NUL terminate (but do not add to len) */
        buf[len > 0 ? len : 0] = '\0';
    }

    *start = i;
    return len;
}

ssize_t hostlist_ranged_string(hostlist_t hl, size_t n, char *buf)
{
    int i = 0;
    int len = 0;
    int truncated = 0;

    LOCK_HOSTLIST(hl);
    while (i < hl->nranges && len < n) {
        len += _get_bracketed_list(hl, &i, n - len, buf + len);
        if ((len > 0) && (len < n) && (i < hl->nranges))
            buf[len++] = ',';
    }
    UNLOCK_HOSTLIST(hl);

    /* NUL terminate */
    if (len >= n) {
        truncated = 1;
        if (n > 0)
            buf[n-1] = '\0';
    } else
        buf[len > 0 ? len : 0] = '\0';

    return truncated ? -1 : len;
}

/* ----[ hostlist iterator functions ]---- */

static hostlist_iterator_t hostlist_iterator_new(void)
{
    hostlist_iterator_t i = (hostlist_iterator_t) malloc(sizeof(*i));
    if (!i) 
        return NULL;
    i->hl = NULL;
    i->hr = NULL;
    i->idx = 0;
    i->depth = -1;
    i->next = i;
    assert(i->magic = HOSTLIST_MAGIC);
    return i;
}

hostlist_iterator_t hostlist_iterator_create(hostlist_t hl)
{
    hostlist_iterator_t i;

    if (!(i = hostlist_iterator_new()))
        out_of_memory("hostlist_iterator_create");

    LOCK_HOSTLIST(hl);
    i->hl = hl;
    i->hr = hl->hr[0];
    i->next = hl->ilist;
    hl->ilist = i;
    UNLOCK_HOSTLIST(hl);
    return i;
}

hostlist_iterator_t hostset_iterator_create(hostset_t set)
{
    return hostlist_iterator_create(set->hl);
}

void hostlist_iterator_reset(hostlist_iterator_t i)
{
    assert(i != NULL);
    assert(i->magic == HOSTLIST_MAGIC);
    i->idx = 0;
    i->hr = i->hl->hr[0];
    i->depth = -1;
    return;
}

void hostlist_iterator_destroy(hostlist_iterator_t i)
{
    hostlist_iterator_t *pi;
    if (i == NULL)
        return;
    assert(i != NULL);
    assert(i->magic == HOSTLIST_MAGIC);
    LOCK_HOSTLIST(i->hl);
    for (pi = &i->hl->ilist; *pi; pi = &(*pi)->next) {
        assert((*pi)->magic == HOSTLIST_MAGIC);
        if (*pi == i) {
            *pi = (*pi)->next;
            break;
        }
    }
    UNLOCK_HOSTLIST(i->hl);
    assert(i->magic = 0x1);
    free(i);
}

static void _iterator_advance(hostlist_iterator_t i)
{
    assert(i != NULL);
    assert(i->magic == HOSTLIST_MAGIC);
    if (i->idx > i->hl->nranges - 1)
        return;
    if (++(i->depth) > (i->hr->hi - i->hr->lo)) {
        i->depth = 0;
        i->hr = i->hl->hr[++i->idx];
    }
}

/* advance iterator to end of current range (meaning within "[" "]")
 * i.e. advance iterator past all range objects that could be represented
 * in on bracketed hostlist.
 */
static void _iterator_advance_range(hostlist_iterator_t i)
{
    int nr, j;
    hostrange_t *hr;
    assert(i != NULL);
    assert(i->magic == HOSTLIST_MAGIC);

    nr = i->hl->nranges;
    hr = i->hl->hr;
    j = i->idx;
    if (++i->depth > 0) {
        while (++j < nr && hostrange_within_range(i->hr, hr[j])) {;}
        i->idx = j;
        i->hr = i->hl->hr[i->idx];
        i->depth = 0;
    }
}

char *hostlist_next(hostlist_iterator_t i)
{
    char *buf = NULL;
    char suffix[16];
    int len = 0;
    assert(i != NULL);
    assert(i->magic == HOSTLIST_MAGIC);
    LOCK_HOSTLIST(i->hl);
    _iterator_advance(i);

    if (i->idx > i->hl->nranges - 1) {
        UNLOCK_HOSTLIST(i->hl);
        return NULL;
    }

    suffix[0] = '\0';

    if (!i->hr->singlehost)
        snprintf (suffix, 15, "%0*lu", i->hr->width, i->hr->lo + i->depth);

    len = strlen (i->hr->prefix) + strlen (suffix) + 1;
    if (!(buf = malloc (len)))
        out_of_memory("hostlist_next");
    
    buf[0] = '\0';
    strcat (buf, i->hr->prefix);
    strcat (buf, suffix);

    UNLOCK_HOSTLIST(i->hl);
    return (buf);
}

char *hostlist_next_range(hostlist_iterator_t i)
{
    char buf[MAXHOSTRANGELEN + 1];
    int j;

    assert(i != NULL);
    assert(i->magic == HOSTLIST_MAGIC);
    LOCK_HOSTLIST(i->hl);

    _iterator_advance_range(i);

    if (i->idx > i->hl->nranges - 1) {
        UNLOCK_HOSTLIST(i->hl);
        return NULL;
    }

    j = i->idx;
    _get_bracketed_list(i->hl, &j, MAXHOSTRANGELEN, buf);

    UNLOCK_HOSTLIST(i->hl);

    return strdup(buf);
}

int hostlist_remove(hostlist_iterator_t i)
{
    hostrange_t new;
    assert(i != NULL);
    assert(i->magic == HOSTLIST_MAGIC);
    LOCK_HOSTLIST(i->hl);
    new = hostrange_delete_host(i->hr, i->hr->lo + i->depth);
    if (new) {
        hostlist_insert_range(i->hl, new, i->idx + 1);
        hostrange_destroy(new);
        i->hr = i->hl->hr[++i->idx];
        i->depth = -1;
    } else if (hostrange_empty(i->hr)) {
        hostlist_delete_range(i->hl, i->idx);
    } else
        i->depth--;

    i->hl->nhosts--;
    UNLOCK_HOSTLIST(i->hl);

    return 1;
}

/* ----[ hostset functions ]---- */

hostset_t hostset_create(const char *hostlist)
{
    hostset_t new;

    if (!(new = (hostset_t) malloc(sizeof(*new))))
        goto error1;

    if (!(new->hl = hostlist_create(hostlist)))
        goto error2;

    hostlist_uniq(new->hl);
    return new;

  error2:
    free(new);
  error1:
    return NULL;
}

hostset_t hostset_copy(const hostset_t set)
{
    hostset_t new;
    if (!(new = (hostset_t) malloc(sizeof(*new))))
        goto error1;

    if (!(new->hl = hostlist_copy(set->hl)))
        goto error2;

    return new;
  error2:
    free(new);
  error1:
    return NULL;
}

void hostset_destroy(hostset_t set)
{
    if (set == NULL)
        return;
    hostlist_destroy(set->hl);
    free(set);
}

/* inserts a single range object into a hostset 
 * Assumes that the set->hl lock is already held
 * Updates hl->nhosts 
 */
static int hostset_insert_range(hostset_t set, hostrange_t hr)
{
    int i = 0;
    int inserted = 0;
    int nhosts = 0;
    int ndups = 0;
    hostlist_t hl;

    hl = set->hl;

    if (hl->size == hl->nranges && !hostlist_expand(hl))
        return 0;

    nhosts = hostrange_count(hr);

    for (i = 0; i < hl->nranges; i++) {
        if (hostrange_cmp(hr, hl->hr[i]) <= 0) {

            if ((ndups = hostrange_join(hr, hl->hr[i])) >= 0) 
                hostlist_delete_range(hl, i);
            else if (ndups < 0)
                ndups = 0;

            hostlist_insert_range(hl, hr, i);

            /* now attempt to join hr[i] and hr[i-1] */
            if (i > 0) {
                int m;
                if ((m = _attempt_range_join(hl, i)) > 0)
                    ndups += m;
            }
            hl->nhosts += nhosts - ndups;
            inserted = 1;
            break;
        }
    }

    if (inserted == 0) {
        hl->hr[hl->nranges++] = hostrange_copy(hr);
        hl->nhosts += nhosts;
        if (hl->nranges > 1) {
            if ((ndups = _attempt_range_join(hl, hl->nranges - 1)) <= 0)
                ndups = 0;
        }
    } 

    /* 
     *  Return the number of unique hosts inserted 
     */
    return nhosts - ndups;
}

int hostset_insert(hostset_t set, const char *hosts)
{
    int i, n = 0;
    hostlist_t hl = hostlist_create(hosts);
    if (!hl)
        return 0;

    hostlist_uniq(hl);
    LOCK_HOSTLIST(set->hl);
    for (i = 0; i < hl->nranges; i++) 
        n += hostset_insert_range(set, hl->hr[i]);
    UNLOCK_HOSTLIST(set->hl);
    hostlist_destroy(hl);
    return n;
}


/* linear search through N ranges for hostname "host"
 * */
static int hostset_find_host(hostset_t set, const char *host)
{
    int i;
    int retval = 0;
    hostname_t hn;
    LOCK_HOSTLIST(set->hl);
    hn = hostname_create(host);
    for (i = 0; i < set->hl->nranges; i++) {
        if (hostrange_hn_within(set->hl->hr[i], hn) >= 0) {
            retval = 1;
            goto done;
        }
    }
  done:
    UNLOCK_HOSTLIST(set->hl);
    hostname_destroy(hn);
    return retval;
}

int hostset_within(hostset_t set, const char *hosts)
{
    int nhosts, nfound;
    hostlist_t hl;
    char *hostname;

    assert(set->hl->magic == HOSTLIST_MAGIC);

    if (!(hl = hostlist_create(hosts)))
        return (0);

    nhosts = hostlist_count(hl);
    nfound = 0;

    while ((hostname = hostlist_pop(hl)) != NULL) {
        nfound += hostset_find_host(set, hostname);
        free(hostname);
    }

    hostlist_destroy(hl);

    return (nhosts == nfound);
}

int hostset_delete(hostset_t set, const char *hosts)
{
    return hostlist_delete(set->hl, hosts);
}

int hostset_delete_host(hostset_t set, const char *hostname)
{
    return hostlist_delete_host(set->hl, hostname);
}

char *hostset_shift(hostset_t set)
{
    return hostlist_shift(set->hl);
}

char *hostset_pop(hostset_t set)
{
    return hostlist_pop(set->hl);
}

char *hostset_shift_range(hostset_t set)
{
    return hostlist_shift_range(set->hl);
}

char *hostset_pop_range(hostset_t set)
{
    return hostlist_pop_range(set->hl);
}

int hostset_count(hostset_t set)
{
    return hostlist_count(set->hl);
}

ssize_t hostset_ranged_string(hostset_t set, size_t n, char *buf)
{
    return hostlist_ranged_string(set->hl, n, buf);
}

ssize_t hostset_deranged_string(hostset_t set, size_t n, char *buf)
{
    return hostlist_deranged_string(set->hl, n, buf);
}

#if TEST_MAIN 

int hostlist_nranges(hostlist_t hl)
{
    return hl->nranges;
}

int hostset_nranges(hostset_t set)
{
    return set->hl->nranges;
}

/* test iterator functionality on the list of hosts represented
 * by list
 */
int iterator_test(char *list)
{
    int j;
    char buf[1024];
    hostlist_t hl = hostlist_create(list);
    hostset_t set = hostset_create(list);

    hostlist_iterator_t i = hostlist_iterator_create(hl);
    hostlist_iterator_t seti = hostset_iterator_create(set);
    hostlist_iterator_t i2 = hostlist_iterator_create(hl);
    char *host;


    hostlist_ranged_string(hl, 1024, buf);
    printf("iterator_test: hl = `%s' passed in `%s'\n", buf, list);
    host = hostlist_next(i);
    printf("first host in list hl = `%s'\n", host);
    free(host);

    /* forge ahead three hosts with i2 */
    for (j = 0; j < 4; j++) {
        host = hostlist_next(i2);
        free(host);
    }

    host = hostlist_shift(hl);
    printf("result of shift(hl)   = `%s'\n", host);
    free(host);
    host = hostlist_next(i);
    printf("next host in list hl  = `%s'\n", host);
    free(host);
    host = hostlist_next(i2);
    printf("next host for i2      = `%s'\n", host);
    free(host);

    hostlist_iterator_destroy(i);

    hostlist_destroy(hl);
    hostset_destroy(set);
    return 1;
}

int main(int ac, char **av)
{
    char buf[1024000];
    int i;
    char *str;

    hostlist_t hl1, hl2, hl3;
    hostset_t set, set1;
    hostlist_iterator_t iter, iter2;

    if (!(hl1 = hostlist_create(ac > 1 ? av[1] : NULL)))
        perror("hostlist_create");
    if (!(set = hostset_create(ac > 1 ? av[1] : NULL)))
        perror("hostlist_create");

    hl3 = hostlist_create("f[0-5]");
    hostlist_delete(hl3, "f[1-3]");
    hostlist_ranged_string(hl3, 102400, buf);
    printf("after delete = `%s'\n", buf);
    hostlist_destroy(hl3);

    for (i = 2; i < ac; i++) {
        hostlist_push(hl1, av[i]);
        hostset_insert(set, av[i]);
    }

    hostlist_ranged_string(hl1, 102400, buf);
    printf("ranged   = `%s'\n", buf);

    iterator_test(buf);

    hostlist_deranged_string(hl1, 10240, buf);
    printf("deranged = `%s'\n", buf);

    hostset_ranged_string(set, 1024, buf);
    printf("hostset  = `%s'\n", buf);

    hostlist_sort(hl1);
    hostlist_ranged_string(hl1, 1024, buf);
    printf("sorted   = `%s'\n", buf);

    hostlist_uniq(hl1);
    hostlist_ranged_string(hl1, 1024, buf);
    printf("uniqed   = `%s'\n", buf);

    hl2 = hostlist_copy(hl1);
    printf("pop_range: ");
    while ((str = hostlist_pop_range(hl2))) {
        printf("`%s' ", str);
        free(str);
    }
    hostlist_destroy(hl2);
    printf("\n");

    hl2 = hostlist_copy(hl1);
    printf("shift_range: ");
    while ((str = hostlist_shift_range(hl2))) {
        printf("`%s' ", str);
        free(str);
    }
    hostlist_destroy(hl2);
    printf("\n");

    iter = hostset_iterator_create(set);
    iter2 = hostset_iterator_create(set);
    hostlist_iterator_destroy(iter2);

    printf("next: ");
    while ((str = hostlist_next(iter))) {
        printf("`%s' ", str);
        free(str);
    }
    printf("\n");

    hostlist_iterator_reset(iter);
    printf("next_range: ");
    while ((str = hostlist_next_range(iter))) {
        printf("`%s' ", str);
        free(str);
    }
    printf("\n");

    printf("nranges = %d\n", hostset_nranges(set));

    hostset_ranged_string(set, 1024, buf);
    printf("set = %s\n", buf);

    hostset_destroy(set);
    hostlist_destroy(hl1);
    return 0;
}

#endif                /* TEST_MAIN */

/* 
 * vi: tabstop=4 shiftwidth=4 expandtab 
 */