aboutsummaryrefslogtreecommitdiff
path: root/libdm/libdm-report.c
blob: 10ab7df5c6c1844e278ef46e4864804894b2c515 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
/*
 * Copyright (C) 2002-2004 Sistina Software, Inc. All rights reserved.
 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
 *
 * This file is part of the device-mapper userspace tools.
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU Lesser General Public License v.2.1.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "dmlib.h"

#include <ctype.h>
#include <math.h>  /* fabs() */
#include <float.h> /* DBL_EPSILON */
#include <time.h>

/*
 * Internal flags
 */
#define RH_SORT_REQUIRED	0x00000100
#define RH_HEADINGS_PRINTED	0x00000200
#define RH_ALREADY_REPORTED	0x00000400

struct selection {
	struct dm_pool *mem;
	struct selection_node *selection_root;
};

struct dm_report {
	struct dm_pool *mem;

	/**
	 * Cache the first row allocated so that all rows and fields
	 * can be disposed of in a single dm_pool_free() call.
	 */
	struct row *first_row;

	/* To report all available types */
#define REPORT_TYPES_ALL	UINT32_MAX
	uint32_t report_types;
	const char *output_field_name_prefix;
	const char *field_prefix;
	uint32_t flags;
	const char *separator;

	uint32_t keys_count;

	/* Ordered list of fields needed for this report */
	struct dm_list field_props;

	/* Rows of report data */
	struct dm_list rows;

	/* Array of field definitions */
	const struct dm_report_field_type *fields;
	const char **canonical_field_ids;
	const struct dm_report_object_type *types;

	/* To store caller private data */
	void *private;

	/* Selection handle */
	struct selection *selection;

	/* Null-terminated array of reserved values */
	const struct dm_report_reserved_value *reserved_values;
	struct dm_hash_table *value_cache;
};

/*
 * Internal per-field flags
 */
#define FLD_HIDDEN	0x00001000
#define FLD_SORT_KEY	0x00002000
#define FLD_ASCENDING	0x00004000
#define FLD_DESCENDING	0x00008000
#define FLD_COMPACTED	0x00010000

struct field_properties {
	struct dm_list list;
	uint32_t field_num;
	uint32_t sort_posn;
	int32_t initial_width;
	int32_t width; /* current width: adjusted by dm_report_object() */
	const struct dm_report_object_type *type;
	uint32_t flags;
	int implicit;
};

/*
 * Report selection
 */
struct op_def {
	const char *string;
	uint32_t flags;
	const char *desc;
};

#define FLD_CMP_MASK		0x0FF00000
#define FLD_CMP_UNCOMPARABLE	0x00100000
#define FLD_CMP_EQUAL		0x00200000
#define FLD_CMP_NOT		0x00400000
#define FLD_CMP_GT		0x00800000
#define FLD_CMP_LT		0x01000000
#define FLD_CMP_REGEX		0x02000000
#define FLD_CMP_NUMBER		0x04000000
#define FLD_CMP_TIME		0x08000000
/*
 * #define FLD_CMP_STRING 0x10000000
 * We could define FLD_CMP_STRING here for completeness here,
 * but it's not needed - we can check operator compatibility with
 * field type by using FLD_CMP_REGEX, FLD_CMP_NUMBER and
 * FLD_CMP_TIME flags only.
 */

/*
 * When defining operators, always define longer one before
 * shorter one if one is a prefix of another!
 * (e.g. =~ comes before =)
*/
static struct op_def _op_cmp[] = {
	{ "=~", FLD_CMP_REGEX, "Matching regular expression. [regex]" },
	{ "!~", FLD_CMP_REGEX|FLD_CMP_NOT, "Not matching regular expression. [regex]" },
	{ "=", FLD_CMP_EQUAL, "Equal to. [number, size, percent, string, string list, time]" },
	{ "!=", FLD_CMP_NOT|FLD_CMP_EQUAL, "Not equal to. [number, size, percent, string, string_list, time]" },
	{ ">=", FLD_CMP_NUMBER|FLD_CMP_TIME|FLD_CMP_GT|FLD_CMP_EQUAL, "Greater than or equal to. [number, size, percent, time]" },
	{ ">", FLD_CMP_NUMBER|FLD_CMP_TIME|FLD_CMP_GT, "Greater than. [number, size, percent, time]" },
	{ "<=", FLD_CMP_NUMBER|FLD_CMP_TIME|FLD_CMP_LT|FLD_CMP_EQUAL, "Less than or equal to. [number, size, percent, time]" },
	{ "<", FLD_CMP_NUMBER|FLD_CMP_TIME|FLD_CMP_LT, "Less than. [number, size, percent, time]" },
	{ "since", FLD_CMP_TIME|FLD_CMP_GT|FLD_CMP_EQUAL, "Since specified time (same as '>='). [time]" },
	{ "after", FLD_CMP_TIME|FLD_CMP_GT, "After specified time (same as '>'). [time]"},
	{ "until", FLD_CMP_TIME|FLD_CMP_LT|FLD_CMP_EQUAL, "Until specified time (same as '<='). [time]"},
	{ "before", FLD_CMP_TIME|FLD_CMP_LT, "Before specified time (same as '<'). [time]"},
	{ NULL, 0, NULL }
};

#define SEL_MASK		0x000000FF
#define SEL_ITEM		0x00000001
#define SEL_AND 		0x00000002
#define SEL_OR			0x00000004

#define SEL_MODIFIER_MASK	0x00000F00
#define SEL_MODIFIER_NOT	0x00000100

#define SEL_PRECEDENCE_MASK	0x0000F000
#define SEL_PRECEDENCE_PS	0x00001000
#define SEL_PRECEDENCE_PE	0x00002000

#define SEL_LIST_MASK		0x000F0000
#define SEL_LIST_LS		0x00010000
#define SEL_LIST_LE		0x00020000
#define SEL_LIST_SUBSET_LS	0x00040000
#define SEL_LIST_SUBSET_LE	0x00080000

static struct op_def _op_log[] = {
	{ "&&", SEL_AND, "All fields must match" },
	{ ",", SEL_AND, "All fields must match" },
	{ "||", SEL_OR, "At least one field must match" },
	{ "#", SEL_OR, "At least one field must match" },
	{ "!", SEL_MODIFIER_NOT, "Logical negation" },
	{ "(", SEL_PRECEDENCE_PS, "Left parenthesis" },
	{ ")", SEL_PRECEDENCE_PE, "Right parenthesis" },
	{ "[", SEL_LIST_LS, "List start" },
	{ "]", SEL_LIST_LE, "List end"},
	{ "{", SEL_LIST_SUBSET_LS, "List subset start"},
	{ "}", SEL_LIST_SUBSET_LE, "List subset end"},
	{ NULL,  0, NULL},
};

struct selection_str_list {
	unsigned type;			/* either SEL_AND or SEL_OR */
	struct dm_list *list;
};

struct field_selection_value {
	union {
		const char *s;
		uint64_t i;
		time_t t;
		double d;
		struct dm_regex *r;
		struct selection_str_list *l;
	} v;
	struct field_selection_value *next;
};

struct field_selection {
	struct field_properties *fp;
	uint32_t flags;
	struct field_selection_value *value;
};

struct selection_node {
	struct dm_list list;
	uint32_t type;
	union {
		struct field_selection *item;
		struct dm_list set;
	} selection;
};

struct reserved_value_wrapper {
	const char *matched_name;
	const struct dm_report_reserved_value *reserved;
	const void *value;
};

/*
 * Report data field
 */
struct dm_report_field {
	struct dm_list list;
	struct field_properties *props;

	const char *report_string;	/* Formatted ready for display */
	const void *sort_value;		/* Raw value for sorting */
};

struct row {
	struct dm_list list;
	struct dm_report *rh;
	struct dm_list fields;			  /* Fields in display order */
	struct dm_report_field *(*sort_fields)[]; /* Fields in sort order */
	int selected;
};

/*
 * Implicit report types and fields.
 */
#define SPECIAL_REPORT_TYPE 0x80000000
#define SPECIAL_FIELD_SELECTED_ID "selected"
#define SPECIAL_FIELD_HELP_ID "help"
#define SPECIAL_FIELD_HELP_ALT_ID "?"

static void *_null_returning_fn(void *obj __attribute__((unused)))
{
	return NULL;
}

static int _no_report_fn(struct dm_report *rh __attribute__((unused)),
			 struct dm_pool *mem __attribute__((unused)),
			 struct dm_report_field *field __attribute__((unused)),
			 const void *data __attribute__((unused)),
			 void *private __attribute__((unused)))
{
	return 1;
}

static int _selected_disp(struct dm_report *rh,
			  struct dm_pool *mem __attribute__((unused)),
			  struct dm_report_field *field,
			  const void *data,
			  void *private __attribute__((unused)))
{
	const struct row *row = (const struct row *)data;
	return dm_report_field_int(rh, field, &row->selected);
}

static const struct dm_report_object_type _implicit_special_report_types[] = {
	{ SPECIAL_REPORT_TYPE, "Special", "special_", _null_returning_fn },
	{ 0, "", "", NULL }
};

static const struct dm_report_field_type _implicit_special_report_fields[] = {
	{ SPECIAL_REPORT_TYPE, DM_REPORT_FIELD_TYPE_NUMBER | FLD_CMP_UNCOMPARABLE , 0, 8, SPECIAL_FIELD_HELP_ID, "Help", _no_report_fn, "Show help." },
	{ SPECIAL_REPORT_TYPE, DM_REPORT_FIELD_TYPE_NUMBER | FLD_CMP_UNCOMPARABLE , 0, 8, SPECIAL_FIELD_HELP_ALT_ID, "Help", _no_report_fn, "Show help." },
	{ 0, 0, 0, 0, "", "", 0, 0}
};

static const struct dm_report_field_type _implicit_special_report_fields_with_selection[] = {
	{ SPECIAL_REPORT_TYPE, DM_REPORT_FIELD_TYPE_NUMBER, 0, 8, SPECIAL_FIELD_SELECTED_ID, "Selected", _selected_disp, "Set if item passes selection criteria." },
	{ SPECIAL_REPORT_TYPE, DM_REPORT_FIELD_TYPE_NUMBER | FLD_CMP_UNCOMPARABLE , 0, 8, SPECIAL_FIELD_HELP_ID, "Help", _no_report_fn, "Show help." },
	{ SPECIAL_REPORT_TYPE, DM_REPORT_FIELD_TYPE_NUMBER | FLD_CMP_UNCOMPARABLE , 0, 8, SPECIAL_FIELD_HELP_ALT_ID, "Help", _no_report_fn, "Show help." },
	{ 0, 0, 0, 0, "", "", 0, 0}
};

static const struct dm_report_object_type *_implicit_report_types = _implicit_special_report_types;
static const struct dm_report_field_type *_implicit_report_fields = _implicit_special_report_fields;

static const struct dm_report_object_type *_find_type(struct dm_report *rh,
						      uint32_t report_type)
{
	const struct dm_report_object_type *t;

	for (t = _implicit_report_types; t->data_fn; t++)
		if (t->id == report_type)
			return t;

	for (t = rh->types; t->data_fn; t++)
		if (t->id == report_type)
			return t;

	return NULL;
}

/*
 * Data-munging functions to prepare each data type for display and sorting
 */

int dm_report_field_string(struct dm_report *rh,
			   struct dm_report_field *field, const char *const *data)
{
	char *repstr;

	if (!(repstr = dm_pool_strdup(rh->mem, *data))) {
		log_error("dm_report_field_string: dm_pool_strdup failed");
		return 0;
	}

	field->report_string = repstr;
	field->sort_value = (const void *) field->report_string;

	return 1;
}

int dm_report_field_percent(struct dm_report *rh,
			    struct dm_report_field *field,
			    const dm_percent_t *data)
{
	char *repstr;
	uint64_t *sortval;

	if (!(sortval = dm_pool_alloc(rh->mem, sizeof(uint64_t)))) {
		log_error("dm_report_field_percent: dm_pool_alloc failed for sort_value.");
		return 0;
	}

	*sortval = (uint64_t)(*data);

	if (*data == DM_PERCENT_INVALID) {
		dm_report_field_set_value(field, "", sortval);
		return 1;
	}

	if (!(repstr = dm_pool_alloc(rh->mem, 8))) {
		dm_pool_free(rh->mem, sortval);
		log_error("dm_report_field_percent: dm_pool_alloc failed for percent report string.");
		return 0;
	}

	if (dm_snprintf(repstr, 7, "%.2f", dm_percent_to_float(*data)) < 0) {
		dm_pool_free(rh->mem, sortval);
		log_error("dm_report_field_percent: percentage too large.");
		return 0;
	}

	dm_report_field_set_value(field, repstr, sortval);
	return 1;
}

struct str_list_sort_value_item {
	unsigned pos;
	size_t len;
};

struct str_list_sort_value {
	const char *value;
	struct str_list_sort_value_item *items;
};

struct str_list_sort_item {
	const char *str;
	struct str_list_sort_value_item item;
};

static int _str_list_sort_item_cmp(const void *a, const void *b)
{
	const struct str_list_sort_item *slsi_a = (const struct str_list_sort_item *) a;
	const struct str_list_sort_item *slsi_b = (const struct str_list_sort_item *) b;

	return strcmp(slsi_a->str, slsi_b->str);
}

static int _report_field_string_list(struct dm_report *rh,
				     struct dm_report_field *field,
				     const struct dm_list *data,
				     const char *delimiter,
				     int sort)
{
	static const char _string_list_grow_object_failed_msg[] = "dm_report_field_string_list: dm_pool_grow_object_failed";
	struct str_list_sort_value *sort_value = NULL;
	unsigned int list_size, pos, i;
	struct str_list_sort_item *arr = NULL;
	struct dm_str_list *sl;
	size_t delimiter_len, len;
	void *object;
	int r = 0;

	if (!(sort_value = dm_pool_zalloc(rh->mem, sizeof(struct str_list_sort_value)))) {
		log_error("dm_report_field_string_list: dm_pool_zalloc failed for sort_value");
		return 0;
	}

	list_size = dm_list_size(data);

	/*
	 * Sort value stores the pointer to the report_string and then
	 * position and length for each list element withing the report_string.
	 * The first element stores number of elements in 'len' (therefore
	 * list_size + 1 is used below for the extra element).
	 * For example, with this input:
	 *   sort = 0;  (we don't want to report sorted)
	 *   report_string = "abc,xy,defgh";  (this is reported)
	 *
	 * ...we end up with:
	 *   sort_value->value = report_string; (we'll use the original report_string for indices)
	 *   sort_value->items[0] = {0,3};  (we have 3 items)
	 *   sort_value->items[1] = {0,3};  ("abc")
	 *   sort_value->items[2] = {7,5};  ("defgh")
	 *   sort_value->items[3] = {4,2};  ("xy")
	 *
	 *   The items alone are always sorted while in report_string they can be
	 *   sorted or not (based on "sort" arg) - it depends on how we prefer to
	 *   display the list. Having items sorted internally helps with searching
	 *   through them.
	 */
	if (!(sort_value->items = dm_pool_zalloc(rh->mem, (list_size + 1) * sizeof(struct str_list_sort_value_item)))) {
		log_error("dm_report_fiel_string_list: dm_pool_zalloc failed for sort value items");
		goto out;
	}
	sort_value->items[0].len = list_size;

	/* zero items */
	if (!list_size) {
		sort_value->value = field->report_string = "";
		field->sort_value = sort_value;
		return 1;
	}

	/* one item */
	if (list_size == 1) {
		sl = (struct dm_str_list *) dm_list_first(data);
		if (!(sort_value->value = field->report_string = dm_pool_strdup(rh->mem, sl->str))) {
			log_error("dm_report_field_string_list: dm_pool_strdup failed");
			goto out;
		}
		sort_value->items[1].pos = 0;
		sort_value->items[1].len = strlen(sl->str);
		field->sort_value = sort_value;
		return 1;
	}

	/* more than one item - sort the list */
	if (!(arr = dm_malloc(sizeof(struct str_list_sort_item) * list_size))) {
		log_error("dm_report_field_string_list: dm_malloc failed");
		goto out;
	}

	if (!(dm_pool_begin_object(rh->mem, 256))) {
		log_error(_string_list_grow_object_failed_msg);
		goto out;
	}

	if (!delimiter)
		delimiter = ",";
	delimiter_len = strlen(delimiter);

	i = pos = len = 0;
	dm_list_iterate_items(sl, data) {
		arr[i].str = sl->str;
		if (!sort) {
			/* sorted outpud not required - report the list as it is */
			len = strlen(sl->str);
			if (!dm_pool_grow_object(rh->mem, arr[i].str, len) ||
			    (i+1 != list_size && !dm_pool_grow_object(rh->mem, delimiter, delimiter_len))) {
				log_error(_string_list_grow_object_failed_msg);
				goto out;
			}
			arr[i].item.pos = pos;
			arr[i].item.len = len;
			pos = i+1 == list_size ? pos+len : pos+len+delimiter_len;
		}
		i++;
	}

	qsort(arr, i, sizeof(struct str_list_sort_item), _str_list_sort_item_cmp);

	for (i = 0, pos = 0; i < list_size; i++) {
		if (sort) {
			/* sorted output required - report the list as sorted */
			len = strlen(arr[i].str);
			if (!dm_pool_grow_object(rh->mem, arr[i].str, len) ||
			    (i+1 != list_size && !dm_pool_grow_object(rh->mem, delimiter, delimiter_len))) {
				log_error(_string_list_grow_object_failed_msg);
				goto out;
			}
			/*
			 * Save position and length of the string
			 * element in report_string for sort_value.
			 * Use i+1 here since items[0] stores list size!!!
			 */
			sort_value->items[i+1].pos = pos;
			sort_value->items[i+1].len = len;
			pos = i+1 == list_size ? pos+len : pos+len+delimiter_len;
		} else {
			sort_value->items[i+1].pos = arr[i].item.pos;
			sort_value->items[i+1].len = arr[i].item.len;
		}
	}

	if (!dm_pool_grow_object(rh->mem, "\0", 1)) {
		log_error(_string_list_grow_object_failed_msg);
		goto out;
	}

	object = dm_pool_end_object(rh->mem);
	sort_value->value = object;
	field->sort_value = sort_value;
	field->report_string = object;
	r = 1;
out:
	if (!r && sort_value)
		dm_pool_free(rh->mem, sort_value);
	if (arr)
		dm_free(arr);
	return r;
}

int dm_report_field_string_list(struct dm_report *rh,
				struct dm_report_field *field,
				const struct dm_list *data,
				const char *delimiter)
{
	return _report_field_string_list(rh, field, data, delimiter, 1);
}

int dm_report_field_string_list_unsorted(struct dm_report *rh,
					 struct dm_report_field *field,
					 const struct dm_list *data,
					 const char *delimiter)
{
	/*
	 * The raw value is always sorted, just the string reported is unsorted.
	 * Having the raw value always sorted helps when matching selection list
	 * with selection criteria.
	 */
	return _report_field_string_list(rh, field, data, delimiter, 0);
}

int dm_report_field_int(struct dm_report *rh,
			struct dm_report_field *field, const int *data)
{
	const int value = *data;
	uint64_t *sortval;
	char *repstr;

	if (!(repstr = dm_pool_zalloc(rh->mem, 13))) {
		log_error("dm_report_field_int: dm_pool_alloc failed");
		return 0;
	}

	if (!(sortval = dm_pool_alloc(rh->mem, sizeof(int64_t)))) {
		log_error("dm_report_field_int: dm_pool_alloc failed");
		return 0;
	}

	if (dm_snprintf(repstr, 12, "%d", value) < 0) {
		log_error("dm_report_field_int: int too big: %d", value);
		return 0;
	}

	*sortval = (uint64_t) value;
	field->sort_value = sortval;
	field->report_string = repstr;

	return 1;
}

int dm_report_field_uint32(struct dm_report *rh,
			   struct dm_report_field *field, const uint32_t *data)
{
	const uint32_t value = *data;
	uint64_t *sortval;
	char *repstr;

	if (!(repstr = dm_pool_zalloc(rh->mem, 12))) {
		log_error("dm_report_field_uint32: dm_pool_alloc failed");
		return 0;
	}

	if (!(sortval = dm_pool_alloc(rh->mem, sizeof(uint64_t)))) {
		log_error("dm_report_field_uint32: dm_pool_alloc failed");
		return 0;
	}

	if (dm_snprintf(repstr, 11, "%u", value) < 0) {
		log_error("dm_report_field_uint32: uint32 too big: %u", value);
		return 0;
	}

	*sortval = (uint64_t) value;
	field->sort_value = sortval;
	field->report_string = repstr;

	return 1;
}

int dm_report_field_int32(struct dm_report *rh,
			  struct dm_report_field *field, const int32_t *data)
{
	const int32_t value = *data;
	uint64_t *sortval;
	char *repstr;

	if (!(repstr = dm_pool_zalloc(rh->mem, 13))) {
		log_error("dm_report_field_int32: dm_pool_alloc failed");
		return 0;
	}

	if (!(sortval = dm_pool_alloc(rh->mem, sizeof(int64_t)))) {
		log_error("dm_report_field_int32: dm_pool_alloc failed");
		return 0;
	}

	if (dm_snprintf(repstr, 12, "%d", value) < 0) {
		log_error("dm_report_field_int32: int32 too big: %d", value);
		return 0;
	}

	*sortval = (uint64_t) value;
	field->sort_value = sortval;
	field->report_string = repstr;

	return 1;
}

int dm_report_field_uint64(struct dm_report *rh,
			   struct dm_report_field *field, const uint64_t *data)
{
	const uint64_t value = *data;
	uint64_t *sortval;
	char *repstr;

	if (!(repstr = dm_pool_zalloc(rh->mem, 22))) {
		log_error("dm_report_field_uint64: dm_pool_alloc failed");
		return 0;
	}

	if (!(sortval = dm_pool_alloc(rh->mem, sizeof(uint64_t)))) {
		log_error("dm_report_field_uint64: dm_pool_alloc failed");
		return 0;
	}

	if (dm_snprintf(repstr, 21, FMTu64 , value) < 0) {
		log_error("dm_report_field_uint64: uint64 too big: %" PRIu64, value);
		return 0;
	}

	*sortval = value;
	field->sort_value = sortval;
	field->report_string = repstr;

	return 1;
}

/*
 * Helper functions for custom report functions
 */
void dm_report_field_set_value(struct dm_report_field *field, const void *value, const void *sortvalue)
{
	field->report_string = (const char *) value;
	field->sort_value = sortvalue ? : value;

	if ((field->sort_value == value) &&
	    (field->props->flags & DM_REPORT_FIELD_TYPE_NUMBER))
		log_warn(INTERNAL_ERROR "Using string as sort value for numerical field.");
}

static const char *_get_field_type_name(unsigned field_type)
{
	switch (field_type) {
		case DM_REPORT_FIELD_TYPE_STRING: return "string";
		case DM_REPORT_FIELD_TYPE_NUMBER: return "number";
		case DM_REPORT_FIELD_TYPE_SIZE: return "size";
		case DM_REPORT_FIELD_TYPE_PERCENT: return "percent";
		case DM_REPORT_FIELD_TYPE_TIME: return "time";
		case DM_REPORT_FIELD_TYPE_STRING_LIST: return "string list";
		default: return "unknown";
	}
}

/*
 * show help message
 */
static size_t _get_longest_field_id_len(const struct dm_report_field_type *fields)
{
	uint32_t f;
	size_t id_len = 0;

	for (f = 0; fields[f].report_fn; f++)
		if (strlen(fields[f].id) > id_len)
			id_len = strlen(fields[f].id);

	return id_len;
}

static void _display_fields_more(struct dm_report *rh,
				 const struct dm_report_field_type *fields,
				 size_t id_len, int display_all_fields_item,
				 int display_field_types)
{
	uint32_t f;
	const struct dm_report_object_type *type;
	const char *desc, *last_desc = "";

	for (f = 0; fields[f].report_fn; f++)
		if (strlen(fields[f].id) > id_len)
			id_len = strlen(fields[f].id);

	for (type = rh->types; type->data_fn; type++)
		if (strlen(type->prefix) + 3 > id_len)
			id_len = strlen(type->prefix) + 3;

	for (f = 0; fields[f].report_fn; f++) {
		if ((type = _find_type(rh, fields[f].type)) && type->desc)
			desc = type->desc;
		else
			desc = " ";
		if (desc != last_desc) {
			if (*last_desc)
				log_warn(" ");
			log_warn("%s Fields", desc);
			log_warn("%*.*s", (int) strlen(desc) + 7,
				 (int) strlen(desc) + 7,
				 "-------------------------------------------------------------------------------");
			if (display_all_fields_item && type->id != SPECIAL_REPORT_TYPE)
				log_warn("  %sall%-*s - %s", type->prefix,
					 (int) (id_len - 3 - strlen(type->prefix)), "",
					 "All fields in this section.");
		}
		/* FIXME Add line-wrapping at terminal width (or 80 cols) */
		log_warn("  %-*s - %s%s%s%s%s", (int) id_len, fields[f].id, fields[f].desc,
					      display_field_types ? " [" : "",
					      display_field_types ? fields[f].flags & FLD_CMP_UNCOMPARABLE ? "unselectable " : "" : "",
					      display_field_types ? _get_field_type_name(fields[f].flags & DM_REPORT_FIELD_TYPE_MASK) : "",
					      display_field_types ? "]" : "");
		last_desc = desc;
	}
}

/*
 * show help message
 */
static void _display_fields(struct dm_report *rh, int display_all_fields_item,
			    int display_field_types)
{
	size_t tmp, id_len = 0;

	if ((tmp = _get_longest_field_id_len(_implicit_report_fields)) > id_len)
		id_len = tmp;
	if ((tmp = _get_longest_field_id_len(rh->fields)) > id_len)
		id_len = tmp;

	_display_fields_more(rh, rh->fields, id_len, display_all_fields_item,
			     display_field_types);
	log_warn(" ");
	_display_fields_more(rh, _implicit_report_fields, id_len,
			     display_all_fields_item, display_field_types);

}

/*
 * Initialise report handle
 */
static int _copy_field(struct dm_report *rh, struct field_properties *dest,
		       uint32_t field_num, int implicit)
{
	const struct dm_report_field_type *fields = implicit ? _implicit_report_fields
							     : rh->fields;

	dest->field_num = field_num;
	dest->initial_width = fields[field_num].width;
	dest->width = fields[field_num].width; /* adjusted in _do_report_object() */
	dest->flags = fields[field_num].flags & DM_REPORT_FIELD_MASK;
	dest->implicit = implicit;

	/* set object type method */
	dest->type = _find_type(rh, fields[field_num].type);
	if (!dest->type) {
		log_error("dm_report: field not match: %s",
			  fields[field_num].id);
		return 0;
	}

	return 1;
}

static struct field_properties * _add_field(struct dm_report *rh,
					    uint32_t field_num, int implicit,
					    uint32_t flags)
{
	struct field_properties *fp;

	if (!(fp = dm_pool_zalloc(rh->mem, sizeof(*fp)))) {
		log_error("dm_report: struct field_properties allocation "
			  "failed");
		return NULL;
	}

	if (!_copy_field(rh, fp, field_num, implicit)) {
		stack;
		dm_pool_free(rh->mem, fp);
		return NULL;
	}

	fp->flags |= flags;

	/*
	 * Place hidden fields at the front so dm_list_end() will
	 * tell us when we've reached the last visible field.
	 */
	if (fp->flags & FLD_HIDDEN)
		dm_list_add_h(&rh->field_props, &fp->list);
	else
		dm_list_add(&rh->field_props, &fp->list);

	return fp;
}

static int _get_canonical_field_name(const char *field,
				     size_t flen,
				     char *canonical_field,
				     size_t fcanonical_len,
				     int *differs)
{
	size_t i;
	int diff = 0;

	for (i = 0; *field && flen; field++, flen--) {
		if (*field == '_') {
			diff = 1;
			continue;
		}
		if (i >= fcanonical_len) {
			log_error("%s: field name too long", field);
			return 0;
		}
		canonical_field[i++] = *field;
	}

	canonical_field[i] = '\0';
	if (differs)
		*differs = diff;
	return 1;
}

/*
 * Compare canonical_name1 against canonical_name2 or prefix
 * plus canonical_name2. Canonical name is a name where all
 * superfluous characters are removed (underscores for now).
 * Both names are always null-terminated.
 */
static int _is_same_field(const char *canonical_name1, const char *canonical_name2,
			  const char *prefix)
{
	size_t prefix_len;

	/* Exact match? */
	if (!strcasecmp(canonical_name1, canonical_name2))
		return 1;

	/* Match including prefix? */
	prefix_len = strlen(prefix) - 1;
	if (!strncasecmp(prefix, canonical_name1, prefix_len) &&
	    !strcasecmp(canonical_name1 + prefix_len, canonical_name2))
		return 1;

	return 0;
}

/*
 * Check for a report type prefix + "all" match.
 */
static void _all_match_combine(const struct dm_report_object_type *types,
			       unsigned unprefixed_all_matched,
			       const char *field, size_t flen,
			       uint32_t *report_types)
{
	char field_canon[DM_REPORT_FIELD_TYPE_ID_LEN];
	const struct dm_report_object_type *t;
	size_t prefix_len;

	if (!_get_canonical_field_name(field, flen, field_canon, DM_REPORT_FIELD_TYPE_ID_LEN, NULL))
		return;
	flen = strlen(field_canon);

	for (t = types; t->data_fn; t++) {
		prefix_len = strlen(t->prefix) - 1;

		if (!strncasecmp(t->prefix, field_canon, prefix_len) &&
		    ((unprefixed_all_matched && (flen == prefix_len)) ||
		     (!strncasecmp(field_canon + prefix_len, "all", 3) &&
		      (flen == prefix_len + 3))))
			*report_types |= t->id;
	}
}

static uint32_t _all_match(struct dm_report *rh, const char *field, size_t flen)
{
	uint32_t report_types = 0;
	unsigned unprefixed_all_matched = 0;

	if (!strncasecmp(field, "all", 3) && flen == 3) {
		/* If there's no report prefix, match all report types */
		if (!(flen = strlen(rh->field_prefix)))
			return rh->report_types ? : REPORT_TYPES_ALL;

		/* otherwise include all fields beginning with the report prefix. */
		unprefixed_all_matched = 1;
		field = rh->field_prefix;
		report_types = rh->report_types;
	}

	/* Combine all report types that have a matching prefix. */
	_all_match_combine(rh->types, unprefixed_all_matched, field, flen, &report_types);

	return report_types;
}

/*
 * Add all fields with a matching type.
 */
static int _add_all_fields(struct dm_report *rh, uint32_t type)
{
	uint32_t f;

	for (f = 0; rh->fields[f].report_fn; f++)
		if ((rh->fields[f].type & type) && !_add_field(rh, f, 0, 0))
			return 0;

	return 1;
}

static int _get_field(struct dm_report *rh, const char *field, size_t flen,
		      uint32_t *f_ret, int *implicit)
{
	char field_canon[DM_REPORT_FIELD_TYPE_ID_LEN];
	uint32_t f;

	if (!flen)
		return 0;

	if (!_get_canonical_field_name(field, flen, field_canon, DM_REPORT_FIELD_TYPE_ID_LEN, NULL))
		return 0;

	for (f = 0; _implicit_report_fields[f].report_fn; f++) {
		if (_is_same_field(_implicit_report_fields[f].id, field_canon, rh->field_prefix)) {
			*f_ret = f;
			*implicit = 1;
			return 1;
		}
	}

	for (f = 0; rh->fields[f].report_fn; f++) {
		if (_is_same_field(rh->canonical_field_ids[f], field_canon, rh->field_prefix)) {
			*f_ret = f;
			*implicit = 0;
			return 1;
		}
	}

	return 0;
}

static int _field_match(struct dm_report *rh, const char *field, size_t flen,
			unsigned report_type_only)
{
	uint32_t f, type;
	int implicit;

	if (!flen)
		return 0;

	if ((_get_field(rh, field, flen, &f, &implicit))) {
		if (report_type_only) {
			rh->report_types |= implicit ? _implicit_report_fields[f].type
						     : rh->fields[f].type;
			return 1;
		} else
			return _add_field(rh, f, implicit, 0) ? 1 : 0;
	}

	if ((type = _all_match(rh, field, flen))) {
		if (report_type_only) {
			rh->report_types |= type;
			return 1;
		} else
			return  _add_all_fields(rh, type);
	}

	return 0;
}

static int _add_sort_key(struct dm_report *rh, uint32_t field_num, int implicit,
			 uint32_t flags, unsigned report_type_only)
{
	struct field_properties *fp, *found = NULL;
	const struct dm_report_field_type *fields = implicit ? _implicit_report_fields
							     : rh->fields;

	dm_list_iterate_items(fp, &rh->field_props) {
		if ((fp->implicit == implicit) && (fp->field_num == field_num)) {
			found = fp;
			break;
		}
	}

	if (!found) {
		if (report_type_only)
			rh->report_types |= fields[field_num].type;
		else if (!(found = _add_field(rh, field_num, implicit, FLD_HIDDEN)))
			return_0;
	}

	if (report_type_only)
		return 1;

	if (found->flags & FLD_SORT_KEY) {
		log_warn("dm_report: Ignoring duplicate sort field: %s.",
			 fields[field_num].id);
		return 1;
	}

	found->flags |= FLD_SORT_KEY;
	found->sort_posn = rh->keys_count++;
	found->flags |= flags;

	return 1;
}

static int _key_match(struct dm_report *rh, const char *key, size_t len,
		      unsigned report_type_only)
{
	char key_canon[DM_REPORT_FIELD_TYPE_ID_LEN];
	uint32_t f;
	uint32_t flags;

	if (!len)
		return 0;

	if (*key == '+') {
		key++;
		len--;
		flags = FLD_ASCENDING;
	} else if (*key == '-') {
		key++;
		len--;
		flags = FLD_DESCENDING;
	} else
		flags = FLD_ASCENDING;

	if (!len) {
		log_error("dm_report: Missing sort field name");
		return 0;
	}

	if (!_get_canonical_field_name(key, len, key_canon, DM_REPORT_FIELD_TYPE_ID_LEN, NULL))
		return 0;

	for (f = 0; _implicit_report_fields[f].report_fn; f++)
		if (_is_same_field(_implicit_report_fields[f].id, key_canon, rh->field_prefix))
			return _add_sort_key(rh, f, 1, flags, report_type_only);

	for (f = 0; rh->fields[f].report_fn; f++)
		if (_is_same_field(rh->canonical_field_ids[f], key_canon, rh->field_prefix))
			return _add_sort_key(rh, f, 0, flags, report_type_only);

	return 0;
}

static int _parse_fields(struct dm_report *rh, const char *format,
			 unsigned report_type_only)
{
	const char *ws;		/* Word start */
	const char *we = format;	/* Word end */

	while (*we) {
		/* Allow consecutive commas */
		while (*we && *we == ',')
			we++;

		/* start of the field name */
		ws = we;
		while (*we && *we != ',')
			we++;

		if (!_field_match(rh, ws, (size_t) (we - ws), report_type_only)) {
			_display_fields(rh, 1, 0);
			log_warn(" ");
			log_error("Unrecognised field: %.*s", (int) (we - ws), ws);
			return 0;
		}
	}

	return 1;
}

static int _parse_keys(struct dm_report *rh, const char *keys,
		       unsigned report_type_only)
{
	const char *ws;		/* Word start */
	const char *we = keys;	/* Word end */

	if (!keys)
		return 1;

	while (*we) {
		/* Allow consecutive commas */
		while (*we && *we == ',')
			we++;
		ws = we;
		while (*we && *we != ',')
			we++;
		if (!_key_match(rh, ws, (size_t) (we - ws), report_type_only)) {
			_display_fields(rh, 1, 0);
			log_warn(" ");
			log_error("dm_report: Unrecognised field: %.*s", (int) (we - ws), ws);
			return 0;
		}
	}

	return 1;
}

static int _contains_reserved_report_type(const struct dm_report_object_type *types)
{
	const struct dm_report_object_type *type, *implicit_type;

	for (implicit_type = _implicit_report_types; implicit_type->data_fn; implicit_type++) {
		for (type = types; type->data_fn; type++) {
			if (implicit_type->id & type->id) {
				log_error(INTERNAL_ERROR "dm_report_init: definition of report "
					  "types given contains reserved identifier");
				return 1;
			}
		}
	}

	return 0;
}

static void _dm_report_init_update_types(struct dm_report *rh, uint32_t *report_types)
{
	const struct dm_report_object_type *type;

	if (!report_types)
		return;

	*report_types = rh->report_types;
	/*
	 * Do not include implicit types as these are not understood by
	 * dm_report_init caller - the caller doesn't know how to check
	 * these types anyway.
	 */
	for (type = _implicit_report_types; type->data_fn; type++)
		*report_types &= ~type->id;
}

static int _help_requested(struct dm_report *rh)
{
	struct field_properties *fp;

	dm_list_iterate_items(fp, &rh->field_props) {
		if (fp->implicit &&
		    (!strcmp(_implicit_report_fields[fp->field_num].id, SPECIAL_FIELD_HELP_ID) ||
		     !strcmp(_implicit_report_fields[fp->field_num].id, SPECIAL_FIELD_HELP_ALT_ID)))
			return 1;
	}

	return 0;
}

static int _canonicalize_field_ids(struct dm_report *rh)
{
	size_t registered_field_count = 0, i;
	char canonical_field[DM_REPORT_FIELD_TYPE_ID_LEN];
	char *canonical_field_dup;
	int differs;

	while (*rh->fields[registered_field_count].id)
		registered_field_count++;

	if (!(rh->canonical_field_ids = dm_pool_alloc(rh->mem, registered_field_count * sizeof(const char *)))) {
		log_error("_canonicalize_field_ids: dm_pool_alloc failed");
		return 0;
	}

	for (i = 0; i < registered_field_count; i++) {
		if (!_get_canonical_field_name(rh->fields[i].id, strlen(rh->fields[i].id),
					       canonical_field, DM_REPORT_FIELD_TYPE_ID_LEN, &differs))
			return_0;

		if (differs) {
			canonical_field_dup = dm_pool_strdup(rh->mem, canonical_field);
			rh->canonical_field_ids[i] = canonical_field_dup;
		} else
			rh->canonical_field_ids[i] = rh->fields[i].id;
	}

	return 1;
}

struct dm_report *dm_report_init(uint32_t *report_types,
				 const struct dm_report_object_type *types,
				 const struct dm_report_field_type *fields,
				 const char *output_fields,
				 const char *output_separator,
				 uint32_t output_flags,
				 const char *sort_keys,
				 void *private_data)
{
	struct dm_report *rh;
	const struct dm_report_object_type *type;

	if (_contains_reserved_report_type(types))
		return_NULL;

	if (!(rh = dm_zalloc(sizeof(*rh)))) {
		log_error("dm_report_init: dm_malloc failed");
		return NULL;
	}

	/*
	 * rh->report_types is updated in _parse_fields() and _parse_keys()
	 * to contain all types corresponding to the fields specified by
	 * fields or keys.
	 */
	if (report_types)
		rh->report_types = *report_types;

	rh->separator = output_separator;
	rh->fields = fields;
	rh->types = types;
	rh->private = private_data;

	rh->flags |= output_flags & DM_REPORT_OUTPUT_MASK;

	/* With columns_as_rows we must buffer and not align. */
	if (output_flags & DM_REPORT_OUTPUT_COLUMNS_AS_ROWS) {
		if (!(output_flags & DM_REPORT_OUTPUT_BUFFERED))
			rh->flags |= DM_REPORT_OUTPUT_BUFFERED;
		if (output_flags & DM_REPORT_OUTPUT_ALIGNED)
			rh->flags &= ~DM_REPORT_OUTPUT_ALIGNED;
	}

	if (output_flags & DM_REPORT_OUTPUT_BUFFERED)
		rh->flags |= RH_SORT_REQUIRED;

	dm_list_init(&rh->field_props);
	dm_list_init(&rh->rows);

	if ((type = _find_type(rh, rh->report_types)) && type->prefix)
		rh->field_prefix = type->prefix;
	else
		rh->field_prefix = "";

	if (!(rh->mem = dm_pool_create("report", 10 * 1024))) {
		log_error("dm_report_init: allocation of memory pool failed");
		dm_free(rh);
		return NULL;
	}

	if (!_canonicalize_field_ids(rh)) {
		dm_report_free(rh);
		return NULL;
	}

	/*
	 * To keep the code needed to add the "all" field to a minimum, we parse
	 * the field lists twice.  The first time we only update the report type.
	 * FIXME Use one pass instead and expand the "all" field afterwards.
	 */
	if (!_parse_fields(rh, output_fields, 1) ||
	    !_parse_keys(rh, sort_keys, 1)) {
		dm_report_free(rh);
		return NULL;
	}

	/* Generate list of fields for output based on format string & flags */
	if (!_parse_fields(rh, output_fields, 0) ||
	    !_parse_keys(rh, sort_keys, 0)) {
		dm_report_free(rh);
		return NULL;
	}

	/*
	 * Return updated types value for further compatility check by caller.
	 */
	_dm_report_init_update_types(rh, report_types);

	if (_help_requested(rh)) {
		_display_fields(rh, 1, 0);
		log_warn(" ");
		rh->flags |= RH_ALREADY_REPORTED;
	}

	return rh;
}

void dm_report_free(struct dm_report *rh)
{
	if (rh->selection)
		dm_pool_destroy(rh->selection->mem);
	if (rh->value_cache)
		dm_hash_destroy(rh->value_cache);
	dm_pool_destroy(rh->mem);
	dm_free(rh);
}

static char *_toupperstr(char *str)
{
	char *u = str;

	do
		*u = toupper(*u);
	while (*u++);

	return str;
}

int dm_report_set_output_field_name_prefix(struct dm_report *rh, const char *output_field_name_prefix)
{
	char *prefix;

	if (!(prefix = dm_pool_strdup(rh->mem, output_field_name_prefix))) {
		log_error("dm_report_set_output_field_name_prefix: dm_pool_strdup failed");
		return 0;
	}

	rh->output_field_name_prefix = _toupperstr(prefix);
	
	return 1;
}

/*
 * Create a row of data for an object
 */
static void *_report_get_field_data(struct dm_report *rh,
				    struct field_properties *fp, void *object)
{
	const struct dm_report_field_type *fields = fp->implicit ? _implicit_report_fields
								 : rh->fields;

	char *ret = fp->type->data_fn(object);

	if (!ret)
		return NULL;

	return (void *)(ret + fields[fp->field_num].offset);
}

static void *_report_get_implicit_field_data(struct dm_report *rh __attribute__((unused)),
					     struct field_properties *fp, struct row *row)
{
	if (!strcmp(_implicit_report_fields[fp->field_num].id, SPECIAL_FIELD_SELECTED_ID))
		return row;

	return NULL;
}

static int _dbl_equal(double d1, double d2)
{
	return fabs(d1 - d2) < DBL_EPSILON;
}

static int _dbl_greater(double d1, double d2)
{
	return (d1 > d2) && !_dbl_equal(d1, d2);
}

static int _dbl_less(double d1, double d2)
{
	return (d1 < d2) && !_dbl_equal(d1, d2);
}

static int _dbl_greater_or_equal(double d1, double d2)
{
	return _dbl_greater(d1, d2) || _dbl_equal(d1, d2);
}

static int _dbl_less_or_equal(double d1, double d2)
{
	return _dbl_less(d1, d2) || _dbl_equal(d1, d2);
}

#define _uint64 *(const uint64_t *)
#define _uint64arr(var,index) ((const uint64_t *)var)[index]
#define _str (const char *)
#define _dbl *(const double *)
#define _dblarr(var,index) ((const double *)var)[index]

static int _do_check_value_is_strictly_reserved(unsigned type, const void *res_val, int res_range,
						const void *val, struct field_selection *fs)
{
	int sel_range = fs ? fs->value->next != NULL : 0;

	switch (type & DM_REPORT_FIELD_TYPE_MASK) {
		case DM_REPORT_FIELD_TYPE_NUMBER:
			if (res_range && sel_range) {
				/* both reserved value and selection value are ranges */
				if (((_uint64 val >= _uint64arr(res_val,0)) && (_uint64 val <= _uint64arr(res_val,1))) ||
				    (fs && ((fs->value->v.i == _uint64arr(res_val,0)) && (fs->value->next->v.i == _uint64arr(res_val,1)))))
					return 1;
			} else if (res_range) {
				/* only reserved value is a range */
				if (((_uint64 val >= _uint64arr(res_val,0)) && (_uint64 val <= _uint64arr(res_val,1))) ||
				    (fs && ((fs->value->v.i >= _uint64arr(res_val,0)) && (fs->value->v.i <= _uint64arr(res_val,1)))))
					return 1;
			} else if (sel_range) {
				/* only selection value is a range */
				if (((_uint64 val >= _uint64 res_val) && (_uint64 val <= _uint64 res_val)) ||
				    (fs && ((fs->value->v.i >= _uint64 res_val) && (fs->value->next->v.i <= _uint64 res_val))))
					return 1;
			} else {
				/* neither selection value nor reserved value is a range */
				if ((_uint64 val == _uint64 res_val) ||
				    (fs && (fs->value->v.i == _uint64 res_val)))
					return 1;
			}
			break;

		case DM_REPORT_FIELD_TYPE_STRING:
			/* there are no ranges for string type yet */
			if ((!strcmp(_str val, _str res_val)) ||
			    (fs && (!strcmp(fs->value->v.s, _str res_val))))
				return 1;
			break;

		case DM_REPORT_FIELD_TYPE_SIZE:
			if (res_range && sel_range) {
				/* both reserved value and selection value are ranges */
				if ((_dbl_greater_or_equal(_dbl val, _dblarr(res_val,0)) && _dbl_less_or_equal(_dbl val, _dblarr(res_val,1))) ||
				    (fs && (_dbl_equal(fs->value->v.d, _dblarr(res_val,0)) && (_dbl_equal(fs->value->next->v.d, _dblarr(res_val,1))))))
					return 1;
			} else if (res_range) {
				/* only reserved value is a range */
				if ((_dbl_greater_or_equal(_dbl val, _dblarr(res_val,0)) && _dbl_less_or_equal(_dbl val, _dblarr(res_val,1))) ||
				    (fs && (_dbl_greater_or_equal(fs->value->v.d, _dblarr(res_val,0)) && _dbl_less_or_equal(fs->value->v.d, _dblarr(res_val,1)))))
					return 1;
			} else if (sel_range) {
				/* only selection value is a range */
				if ((_dbl_greater_or_equal(_dbl val, _dbl res_val) && (_dbl_less_or_equal(_dbl val, _dbl res_val))) ||
				    (fs && (_dbl_greater_or_equal(fs->value->v.d, _dbl res_val) && _dbl_less_or_equal(fs->value->next->v.d, _dbl res_val))))
					return 1;
			} else {
				/* neither selection value nor reserved value is a range */
				if ((_dbl_equal(_dbl val, _dbl res_val)) ||
				    (fs && (_dbl_equal(fs->value->v.d, _dbl res_val))))
					return 1;
			}
			break;

		case DM_REPORT_FIELD_TYPE_STRING_LIST:
			/* FIXME Add comparison for string list */
			break;
		case DM_REPORT_FIELD_TYPE_TIME:
			/* FIXME Add comparison for time */
			break;
	}

	return 0;
}

/*
 * Used to check whether a value of certain type used in selection is reserved.
 */
static int _check_value_is_strictly_reserved(struct dm_report *rh, uint32_t field_num, unsigned type,
					     const void *val, struct field_selection *fs)
{
	const struct dm_report_reserved_value *iter = rh->reserved_values;
	const struct dm_report_field_reserved_value *frv;
	int res_range;

	if (!iter)
		return 0;

	while (iter->value) {
		/* Only check strict reserved values, not the weaker form ("named" reserved value). */
		if (!(iter->type & DM_REPORT_FIELD_RESERVED_VALUE_NAMED)) {
			res_range = iter->type & DM_REPORT_FIELD_RESERVED_VALUE_RANGE;
			if ((iter->type & DM_REPORT_FIELD_TYPE_MASK) == DM_REPORT_FIELD_TYPE_NONE) {
				frv = (const struct dm_report_field_reserved_value *) iter->value;
				if (frv->field_num == field_num && _do_check_value_is_strictly_reserved(type, frv->value, res_range, val, fs))
					return 1;
			} else if (iter->type & type && _do_check_value_is_strictly_reserved(type, iter->value, res_range, val, fs))
				return 1;
		}
		iter++;
	}

	return 0;
}

static int _cmp_field_int(struct dm_report *rh, uint32_t field_num, const char *field_id,
			  uint64_t val, struct field_selection *fs)
{
	int range = fs->value->next != NULL;
	const uint64_t sel1 = fs->value->v.i;
	const uint64_t sel2 = range ? fs->value->next->v.i : 0;

	switch(fs->flags & FLD_CMP_MASK) {
		case FLD_CMP_EQUAL:
			return range ? ((val >= sel1) && (val <= sel2)) : val == sel1;

		case FLD_CMP_NOT|FLD_CMP_EQUAL:
			return range ? !((val >= sel1) && (val <= sel2)) : val != sel1;

		case FLD_CMP_NUMBER|FLD_CMP_GT:
			if (_check_value_is_strictly_reserved(rh, field_num, DM_REPORT_FIELD_TYPE_NUMBER, &val, fs))
				return 0;
			return range ? val > sel2 : val > sel1;

		case FLD_CMP_NUMBER|FLD_CMP_GT|FLD_CMP_EQUAL:
			if (_check_value_is_strictly_reserved(rh, field_num, DM_REPORT_FIELD_TYPE_NUMBER, &val, fs))
				return 0;
			return val >= sel1;

		case FLD_CMP_NUMBER|FLD_CMP_LT:
			if (_check_value_is_strictly_reserved(rh, field_num, DM_REPORT_FIELD_TYPE_NUMBER, &val, fs))
				return 0;
			return val < sel1;

		case FLD_CMP_NUMBER|FLD_CMP_LT|FLD_CMP_EQUAL:
			if (_check_value_is_strictly_reserved(rh, field_num, DM_REPORT_FIELD_TYPE_NUMBER, &val, fs))
				return 0;
			return range ? val <= sel2 : val <= sel1;

		default:
			log_error(INTERNAL_ERROR "_cmp_field_int: unsupported number "
				  "comparison type for field %s", field_id);
	}

	return 0;
}

static int _cmp_field_double(struct dm_report *rh, uint32_t field_num, const char *field_id,
			     double val, struct field_selection *fs)
{
	int range = fs->value->next != NULL;
	double sel1 = fs->value->v.d;
	double sel2 = range ? fs->value->next->v.d : 0;

	switch(fs->flags & FLD_CMP_MASK) {
		case FLD_CMP_EQUAL:
			return range ? (_dbl_greater_or_equal(val, sel1) && _dbl_less_or_equal(val, sel2))
				     : _dbl_equal(val, sel1);

		case FLD_CMP_NOT|FLD_CMP_EQUAL:
			return range ? !(_dbl_greater_or_equal(val, sel1) && _dbl_less_or_equal(val, sel2))
				     : !_dbl_equal(val, sel1);

		case FLD_CMP_NUMBER|FLD_CMP_GT:
			if (_check_value_is_strictly_reserved(rh, field_num, DM_REPORT_FIELD_TYPE_SIZE, &val, fs))
				return 0;
			return range ? _dbl_greater(val, sel2)
				     : _dbl_greater(val, sel1);

		case FLD_CMP_NUMBER|FLD_CMP_GT|FLD_CMP_EQUAL:
			if (_check_value_is_strictly_reserved(rh, field_num, DM_REPORT_FIELD_TYPE_SIZE, &val, fs))
				return 0;
			return _dbl_greater_or_equal(val, sel1);

		case FLD_CMP_NUMBER|FLD_CMP_LT:
			if (_check_value_is_strictly_reserved(rh, field_num, DM_REPORT_FIELD_TYPE_SIZE, &val, fs))
				return 0;
			return _dbl_less(val, sel1);

		case FLD_CMP_NUMBER|FLD_CMP_LT|FLD_CMP_EQUAL:
			if (_check_value_is_strictly_reserved(rh, field_num, DM_REPORT_FIELD_TYPE_SIZE, &val, fs))
				return 0;
			return range ? _dbl_less_or_equal(val, sel2) : _dbl_less_or_equal(val, sel1); 

		default:
			log_error(INTERNAL_ERROR "_cmp_field_double: unsupported number "
				  "comparison type for selection field %s", field_id);
	}

	return 0;
}

static int _cmp_field_string(struct dm_report *rh __attribute__((unused)),
			     uint32_t field_num, const char *field_id,
			     const char *val, struct field_selection *fs)
{
	const char *sel = fs->value->v.s;

	switch (fs->flags & FLD_CMP_MASK) {
		case FLD_CMP_EQUAL:
			return !strcmp(val, sel);
		case FLD_CMP_NOT|FLD_CMP_EQUAL:
			return strcmp(val, sel);
		default:
			log_error(INTERNAL_ERROR "_cmp_field_string: unsupported string "
				  "comparison type for selection field %s", field_id);
	}

	return 0;
}

static int _cmp_field_time(struct dm_report *rh,
			   uint32_t field_num, const char *field_id,
			   time_t val, struct field_selection *fs)
{
	int range = fs->value->next != NULL;
	time_t sel1 = fs->value->v.t;
	time_t sel2 = range ? fs->value->next->v.t : 0;

	switch(fs->flags & FLD_CMP_MASK) {
		case FLD_CMP_EQUAL:
			return range ? ((val >= sel1) && (val <= sel2)) : val == sel1;
		case FLD_CMP_NOT|FLD_CMP_EQUAL:
			return range ? ((val >= sel1) && (val <= sel2)) : val != sel1;
		case FLD_CMP_TIME|FLD_CMP_GT:
			if (_check_value_is_strictly_reserved(rh, field_num, DM_REPORT_FIELD_TYPE_TIME, &val, fs))
				return 0;
			return range ? val > sel2 : val > sel1;
		case FLD_CMP_TIME|FLD_CMP_GT|FLD_CMP_EQUAL:
			if (_check_value_is_strictly_reserved(rh, field_num, DM_REPORT_FIELD_TYPE_TIME, &val, fs))
				return 0;
			return val >= sel1;
		case FLD_CMP_TIME|FLD_CMP_LT:
			if (_check_value_is_strictly_reserved(rh, field_num, DM_REPORT_FIELD_TYPE_TIME, &val, fs))
				return 0;
			return val < sel1;
		case FLD_CMP_TIME|FLD_CMP_LT|FLD_CMP_EQUAL:
			if (_check_value_is_strictly_reserved(rh, field_num, DM_REPORT_FIELD_TYPE_TIME, &val, fs))
				return 0;
			return range ? val <= sel2 : val <= sel1;
		default:
			log_error(INTERNAL_ERROR "_cmp_field_time: unsupported time "
				  "comparison type for field %s", field_id);
	}

	return 0;
}

/* Matches if all items from selection string list match list value strictly 1:1. */
static int _cmp_field_string_list_strict_all(const struct str_list_sort_value *val,
					     const struct selection_str_list *sel)
{
	struct dm_str_list *sel_item;
	unsigned int i = 1;

	/* if item count differs, it's clear the lists do not match */
	if (val->items[0].len != dm_list_size(sel->list))
		return 0;

	/* both lists are sorted so they either match 1:1 or not */
	dm_list_iterate_items(sel_item, sel->list) {
		if ((strlen(sel_item->str) != val->items[i].len) ||
		    strncmp(sel_item->str, val->value + val->items[i].pos, val->items[i].len))
			return 0;
		i++;
	}

	return 1;
}

/* Matches if all items from selection string list match a subset of list value. */
static int _cmp_field_string_list_subset_all(const struct str_list_sort_value *val,
					     const struct selection_str_list *sel)
{
	struct dm_str_list *sel_item;
	unsigned int i, last_found = 1;
	int r = 0;

	/* if value has no items and selection has at leas one, it's clear there's no match */
	if ((val->items[0].len == 0) && dm_list_size(sel->list))
		return 0;

	/* Check selection is a subset of the value. */
	dm_list_iterate_items(sel_item, sel->list) {
		r = 0;
		for (i = last_found; i <= val->items[0].len; i++) {
			if ((strlen(sel_item->str) == val->items[i].len) &&
			    !strncmp(sel_item->str, val->value + val->items[i].pos, val->items[i].len)) {
				last_found = i;
				r = 1;
			}
		}
		if (!r)
			break;
	}

	return r;
}

/* Matches if any item from selection string list matches list value. */
static int _cmp_field_string_list_any(const struct str_list_sort_value *val,
				      const struct selection_str_list *sel)
{
	struct dm_str_list *sel_item;
	unsigned int i;

	/* if value has no items and selection has at least one, it's clear there's no match */
	if ((val->items[0].len == 0) && dm_list_size(sel->list))
		return 0;

	dm_list_iterate_items(sel_item, sel->list) {
		/*
		 * TODO: Optimize this so we don't need to compare the whole lists' content.
		 *       Make use of the fact that the lists are sorted!
		 */
		for (i = 1; i <= val->items[0].len; i++) {
			if ((strlen(sel_item->str) == val->items[i].len) &&
			    !strncmp(sel_item->str, val->value + val->items[i].pos, val->items[i].len))
				return 1;
		}
	}

	return 0;
}

static int _cmp_field_string_list(struct dm_report *rh __attribute__((unused)),
				  uint32_t field_num, const char *field_id,
				  const struct str_list_sort_value *val,
				  struct field_selection *fs)
{
	const struct selection_str_list *sel = fs->value->v.l;
	int subset, r;

	switch (sel->type & SEL_LIST_MASK) {
		case SEL_LIST_LS:
			subset = 0;
			break;
		case SEL_LIST_SUBSET_LS:
			subset = 1;
			break;
		default:
			log_error(INTERNAL_ERROR "_cmp_field_string_list: unknown list type");
			return 0;
	}

	switch (sel->type & SEL_MASK) {
		case SEL_AND:
			r = subset ? _cmp_field_string_list_subset_all(val, sel)
				   : _cmp_field_string_list_strict_all(val, sel);
			break;
		case SEL_OR:
			r = _cmp_field_string_list_any(val, sel);
			break;
		default:
			log_error(INTERNAL_ERROR "_cmp_field_string_list: unsupported string "
				  "list type found, expecting either AND or OR list for "
				  "selection field %s", field_id);
			return 0;
	}

	return fs->flags & FLD_CMP_NOT ? !r : r;
}

static int _cmp_field_regex(const char *s, struct field_selection *fs)
{
	int match = dm_regex_match(fs->value->v.r, s) >= 0;
	return fs->flags & FLD_CMP_NOT ? !match : match;
}

static int _compare_selection_field(struct dm_report *rh,
				    struct dm_report_field *f,
				    struct field_selection *fs)
{
	const struct dm_report_field_type *fields = f->props->implicit ? _implicit_report_fields
								       : rh->fields;
	const char *field_id = fields[f->props->field_num].id;
	int r = 0;

	if (!f->sort_value) {
		log_error("_compare_selection_field: field without value :%d",
			  f->props->field_num);
		return 0;
	}

	if (fs->flags & FLD_CMP_REGEX)
		r = _cmp_field_regex((const char *) f->sort_value, fs);
	else {
		switch(f->props->flags & DM_REPORT_FIELD_TYPE_MASK) {
			case DM_REPORT_FIELD_TYPE_PERCENT:
				/*
				 * Check against real percent values only.
				 * That means DM_PERCENT_0 <= percent <= DM_PERCENT_100.
				 */
				if (*(const uint64_t *) f->sort_value > DM_PERCENT_100)
					return 0;
				/* fall through */
			case DM_REPORT_FIELD_TYPE_NUMBER:
				r = _cmp_field_int(rh, f->props->field_num, field_id, *(const uint64_t *) f->sort_value, fs);
				break;
			case DM_REPORT_FIELD_TYPE_SIZE:
				r = _cmp_field_double(rh, f->props->field_num, field_id, *(const double *) f->sort_value, fs);
				break;
			case DM_REPORT_FIELD_TYPE_STRING:
				r = _cmp_field_string(rh, f->props->field_num, field_id, (const char *) f->sort_value, fs);
				break;
			case DM_REPORT_FIELD_TYPE_STRING_LIST:
				r = _cmp_field_string_list(rh, f->props->field_num, field_id, (const struct str_list_sort_value *) f->sort_value, fs);
				break;
			case DM_REPORT_FIELD_TYPE_TIME:
				r = _cmp_field_time(rh, f->props->field_num, field_id, *(const time_t *) f->sort_value, fs);
				break;
			default:
				log_error(INTERNAL_ERROR "_compare_selection_field: unknown field type for field %s", field_id);
		}
	}

	return r;
}

static int _check_selection(struct dm_report *rh, struct selection_node *sn,
			    struct dm_list *fields)
{
	int r;
	struct selection_node *iter_n;
	struct dm_report_field *f;

	switch (sn->type & SEL_MASK) {
		case SEL_ITEM:
			r = 1;
			dm_list_iterate_items(f, fields) {
				if (sn->selection.item->fp != f->props)
					continue;
				if (!_compare_selection_field(rh, f, sn->selection.item))
					r = 0;
			}
			break;
		case SEL_OR:
			r = 0;
			dm_list_iterate_items(iter_n, &sn->selection.set)
				if ((r |= _check_selection(rh, iter_n, fields)))
					break;
			break;
		case SEL_AND:
			r = 1;
			dm_list_iterate_items(iter_n, &sn->selection.set)
				if (!(r &= _check_selection(rh, iter_n, fields)))
					break;
			break;
		default:
			log_error("Unsupported selection type");
			return 0;
	}

	return (sn->type & SEL_MODIFIER_NOT) ? !r : r;
}

static int _check_report_selection(struct dm_report *rh, struct dm_list *fields)
{
	if (!rh->selection)
		return 1;

	return _check_selection(rh, rh->selection->selection_root, fields);
}

static int _do_report_object(struct dm_report *rh, void *object, int do_output, int *selected)
{
	const struct dm_report_field_type *fields;
	struct field_properties *fp;
	struct row *row = NULL;
	struct dm_report_field *field, *field_sel_status = NULL;
	void *data = NULL;
	int len;
	int r = 0;

	if (!rh) {
		log_error(INTERNAL_ERROR "_do_report_object: dm_report handler is NULL.");
		return 0;
	}

	if (!do_output && !selected) {
		log_error(INTERNAL_ERROR "_do_report_object: output not requested and "
					 "selected output variable is NULL too.");
		return 0;
	}

	if (rh->flags & RH_ALREADY_REPORTED)
		return 1;

	if (!(row = dm_pool_zalloc(rh->mem, sizeof(*row)))) {
		log_error("_do_report_object: struct row allocation failed");
		return 0;
	}

	if (!rh->first_row)
		rh->first_row = row;

	row->rh = rh;

	if ((rh->flags & RH_SORT_REQUIRED) &&
	    !(row->sort_fields =
		dm_pool_zalloc(rh->mem, sizeof(struct dm_report_field *) *
			       rh->keys_count))) {
		log_error("_do_report_object: "
			  "row sort value structure allocation failed");
		goto out;
	}

	dm_list_init(&row->fields);
	row->selected = 1;

	/* For each field to be displayed, call its report_fn */
	dm_list_iterate_items(fp, &rh->field_props) {
		if (!(field = dm_pool_zalloc(rh->mem, sizeof(*field)))) {
			log_error("_do_report_object: "
				  "struct dm_report_field allocation failed");
			goto out;
		}

		if (fp->implicit) {
			fields = _implicit_report_fields;
			if (!strcmp(fields[fp->field_num].id, SPECIAL_FIELD_SELECTED_ID))
				field_sel_status = field;
		} else
			fields = rh->fields;

		field->props = fp;

		data = fp->implicit ? _report_get_implicit_field_data(rh, fp, row)
				    : _report_get_field_data(rh, fp, object);
		if (!data) {
			log_error("_do_report_object: "
				  "no data assigned to field %s",
				  fields[fp->field_num].id);
			goto out;
		}

		if (!fields[fp->field_num].report_fn(rh, rh->mem,
							 field, data,
							 rh->private)) {
			log_error("_do_report_object: "
				  "report function failed for field %s",
				  fields[fp->field_num].id);
			goto out;
		}

		dm_list_add(&row->fields, &field->list);
	}

	r = 1;

	if (!_check_report_selection(rh, &row->fields)) {
		row->selected = 0;

		if (!field_sel_status)
			goto out;

		/*
		 * If field with id "selected" is reported,
		 * report the row although it does not pass
		 * the selection criteria.
		 * The "selected" field reports the result
		 * of the selection.
		 */
		_implicit_report_fields[field_sel_status->props->field_num].report_fn(rh,
						rh->mem, field_sel_status, row, rh->private);
		/*
		 * If the "selected" field is not displayed, e.g.
		 * because it is part of the sort field list,
		 * skip the display of the row as usual.
		 */
		if (field_sel_status->props->flags & FLD_HIDDEN)
			goto out;
	}

	if (!do_output)
		goto out;

	dm_list_add(&rh->rows, &row->list);

	dm_list_iterate_items(field, &row->fields) {
		len = (int) strlen(field->report_string);
		if ((len > field->props->width))
			field->props->width = len;

		if ((rh->flags & RH_SORT_REQUIRED) &&
		    (field->props->flags & FLD_SORT_KEY)) {
			(*row->sort_fields)[field->props->sort_posn] = field;
		}
	}

	if (!(rh->flags & DM_REPORT_OUTPUT_BUFFERED))
		return dm_report_output(rh);
out:
	if (selected)
		*selected = row->selected;
	if (!do_output || !r)
		dm_pool_free(rh->mem, row);
	return r;
}

int dm_report_compact_fields(struct dm_report *rh)
{
	struct dm_report_field *field;
	struct field_properties *fp;
	struct row *row;

	if (!rh) {
		log_error("dm_report_enable_compact_output: dm report handler is NULL.");
		return 0;
	}

	if (!(rh->flags & DM_REPORT_OUTPUT_BUFFERED) ||
	      dm_list_empty(&rh->rows))
		return 1;

	/*
	 * At first, mark all fields with FLD_HIDDEN flag.
	 * Also, mark field with FLD_COMPACTED flag, but only
	 * the ones that didn't have FLD_HIDDEN set before.
	 * This prevents losing the original FLD_HIDDEN flag
	 * in next step...
	 */
	dm_list_iterate_items(fp, &rh->field_props) {
		if (!(fp->flags & FLD_HIDDEN))
			fp->flags |= (FLD_COMPACTED | FLD_HIDDEN);
	}

	/*
	 * ...check each field in a row and if its report value
	 * is not empty, drop the FLD_COMPACTED and FLD_HIDDEN
	 * flag if FLD_COMPACTED flag is set. It's important
	 * to keep FLD_HIDDEN flag for the fields that were
	 * already marked with FLD_HIDDEN before - these don't
	 * have FLD_COMPACTED set - check this condition!
	 */
	dm_list_iterate_items(row, &rh->rows) {
		dm_list_iterate_items(field, &row->fields) {
			if ((field->report_string && *field->report_string) &&
			     field->props->flags & FLD_COMPACTED)
					field->props->flags &= ~(FLD_COMPACTED | FLD_HIDDEN);
			}
	}

	/*
	 * The fields left with FLD_COMPACTED and FLD_HIDDEN flag are
	 * the ones which have blank value in all rows. The FLD_HIDDEN
	 * will cause such field to not be reported on output at all.
	 */

	return 1;
}

int dm_report_object(struct dm_report *rh, void *object)
{
	return _do_report_object(rh, object, 1, NULL);
}

int dm_report_object_is_selected(struct dm_report *rh, void *object, int do_output, int *selected)
{
	return _do_report_object(rh, object, do_output, selected);
}

/*
 * Selection parsing
 */

/*
 * Other tokens (FIELD, VALUE, STRING, NUMBER, REGEX)
 *     FIELD := <strings of alphabet, number and '_'>
 *     VALUE := NUMBER | STRING
 *     REGEX := <strings quoted by '"', '\'', '(', '{', '[' or unquoted>
 *     NUMBER := <strings of [0-9]> (because sort_value is unsigned)
 *     STRING := <strings quoted by '"', '\'' or unquoted>
 */

static const char * _skip_space(const char *s)
{
	while (*s && isspace(*s))
		s++;
	return s;
}

static int _tok_op(struct op_def *t, const char *s, const char **end,
		   uint32_t expect)
{
	size_t len;

	s = _skip_space(s);

	for (; t->string; t++) {
		if (expect && !(t->flags & expect))
			continue;

		len = strlen(t->string);
		if (!strncmp(s, t->string, len)) {
			if (end)
				*end = s + len;
			return t->flags;
		}
	}

	if (end)
		*end = s;
	return 0;
}

static int _tok_op_log(const char *s, const char **end, uint32_t expect)
{
	return _tok_op(_op_log, s, end, expect);
}

static int _tok_op_cmp(const char *s, const char **end)
{
	return _tok_op(_op_cmp, s, end, 0);
}

static char _get_and_skip_quote_char(char const **s)
{
	char c = 0;

	if (**s == '"' || **s == '\'') {
		c = **s;
		(*s)++;
	}

	return c;
}

 /*
  *
  * Input:
  *   s             - a pointer to the parsed string
  * Output:
  *   begin         - a pointer to the beginning of the token
  *   end           - a pointer to the end of the token + 1
  *                   or undefined if return value is NULL
  *   return value  - a starting point of the next parsing or
  *                   NULL if 's' doesn't match with token type
  *                   (the parsing should be terminated)
  */
static const char *_tok_value_number(const char *s,
				     const char **begin, const char **end)

{
	int is_float = 0;

	*begin = s;
	while ((!is_float && (*s == '.') && ((is_float = 1))) || isdigit(*s))
		s++;
	*end = s;

	if (*begin == *end)
		return NULL;

	return s;
}

/*
 * Input:
 *   s               - a pointer to the parsed string
 *   endchar         - terminating character
 *   end_op_flags    - terminating operator flags (see _op_log)
 *                     (if endchar is non-zero then endflags is ignored)
 * Output:
 *   begin           - a pointer to the beginning of the token
 *   end             - a pointer to the end of the token + 1
 *   end_op_flag_hit - the flag from endflags hit during parsing
 *   return value    - a starting point of the next parsing
 */
static const char *_tok_value_string(const char *s,
				     const char **begin, const char **end,
				     const char endchar, uint32_t end_op_flags,
				     uint32_t *end_op_flag_hit)
{
	uint32_t flag_hit = 0;

	*begin = s;

	/*
	 * If endchar is defined, scan the string till
	 * the endchar or the end of string is hit.
	 * This is in case the string is quoted and we
	 * know exact character that is the stopper.
	 */
	if (endchar) {
		while (*s && *s != endchar)
			s++;
		if (*s != endchar) {
			log_error("Missing end quote.");
			return NULL;
		}
		*end = s;
		s++;
	} else {
		/*
		 * If endchar is not defined then endchar is/are the
		 * operator/s as defined by 'endflags' arg or space char.
		 * This is in case the string is not quoted and
		 * we don't know which character is the exact stopper.
		 */
		while (*s) {
			if ((flag_hit = _tok_op(_op_log, s, NULL, end_op_flags)) || *s == ' ')
				break;
			s++;
		}
		*end = s;
		/*
		 * If we hit one of the strings as defined by 'endflags'
		 * and if 'endflag_hit' arg is provided, save the exact
		 * string flag that was hit.
		 */
		if (end_op_flag_hit)
			*end_op_flag_hit = flag_hit;
	}

	return s;
}

static const char *_reserved_name(struct dm_report *rh,
				  const struct dm_report_reserved_value *reserved,
				  const struct dm_report_field_reserved_value *frv,
				  uint32_t field_num, const char *s, size_t len)
{
	dm_report_reserved_handler handler;
	const char *canonical_name;
	const char **name;
	char *tmp_s;
	char c;
	int r;

	name = reserved->names;
	while (*name) {
		if ((strlen(*name) == len) && !strncmp(*name, s, len))
			return *name;
		name++;
	}

	if (reserved->type & DM_REPORT_FIELD_RESERVED_VALUE_FUZZY_NAMES) {
		handler = (dm_report_reserved_handler) frv ? frv->value : reserved->value;
		c = s[len];
		tmp_s = (char *) s;
		tmp_s[len] = '\0';
		if ((r = handler(rh, rh->selection->mem, field_num,
				 DM_REPORT_RESERVED_PARSE_FUZZY_NAME,
				 tmp_s, (const void **) &canonical_name)) <= 0) {
			if (r == -1)
				log_error(INTERNAL_ERROR "%s reserved value handler for field %s has missing "
					  "implementation of DM_REPORT_RESERVED_PARSE_FUZZY_NAME action",
					  (reserved->type & DM_REPORT_FIELD_TYPE_MASK) ? "type-specific" : "field-specific",
					   rh->fields[field_num].id);
			else
				log_error("Error occured while processing %s reserved value handler for field %s",
					  (reserved->type & DM_REPORT_FIELD_TYPE_MASK) ? "type-specific" : "field-specific",
					   rh->fields[field_num].id);
		}
		tmp_s[len] = c;
		if (r && canonical_name)
			return canonical_name;
	}

	return NULL;
}

/*
 * Used to replace a string representation of the reserved value
 * found in selection with the exact reserved value of certain type.
 */
static const char *_get_reserved(struct dm_report *rh, unsigned type,
				 uint32_t field_num, int implicit,
				 const char *s, const char **begin, const char **end,
				 struct reserved_value_wrapper *rvw)
{
	const struct dm_report_reserved_value *iter = implicit ? NULL : rh->reserved_values;
	const struct dm_report_field_reserved_value *frv;
	const char *tmp_begin, *tmp_end, *tmp_s = s;
	const char *name = NULL;
	char c;

	rvw->reserved = NULL;

	if (!iter)
		return s;

	c = _get_and_skip_quote_char(&tmp_s);
	if (!(tmp_s = _tok_value_string(tmp_s, &tmp_begin, &tmp_end, c, SEL_AND | SEL_OR | SEL_PRECEDENCE_PE, NULL)))
		return s;

	while (iter->value) {
		if (!(iter->type & DM_REPORT_FIELD_TYPE_MASK)) {
			/* DM_REPORT_FIELD_TYPE_NONE - per-field reserved value */
			frv = (const struct dm_report_field_reserved_value *) iter->value;
			if ((frv->field_num == field_num) && (name = _reserved_name(rh, iter, frv, field_num,
										    tmp_begin, tmp_end - tmp_begin)))
				break;
		} else if (iter->type & type) {
			/* DM_REPORT_FIELD_TYPE_* - per-type reserved value */
			if ((name = _reserved_name(rh, iter, NULL, field_num,
						   tmp_begin, tmp_end - tmp_begin)))
				break;
		}
		iter++;
	}

	if (name) {
		/* found! */
		*begin = tmp_begin;
		*end = tmp_end;
		s = tmp_s;
		rvw->reserved = iter;
		rvw->matched_name = name;
	}

	return s;
}

float dm_percent_to_float(dm_percent_t percent)
{
	/* Add 0.f to prevent returning -0.00 */
	return (float) percent / DM_PERCENT_1 + 0.f;
}

dm_percent_t dm_make_percent(uint64_t numerator, uint64_t denominator)
{
	dm_percent_t percent;

	if (!denominator)
		return DM_PERCENT_100; /* FIXME? */
	if (!numerator)
		return DM_PERCENT_0;
	if (numerator == denominator)
		return DM_PERCENT_100;
	switch (percent = DM_PERCENT_100 * ((double) numerator / (double) denominator)) {
		case DM_PERCENT_100:
			return DM_PERCENT_100 - 1;
		case DM_PERCENT_0:
			return DM_PERCENT_0 + 1;
		default:
			return percent;
	}
}

int dm_report_value_cache_set(struct dm_report *rh, const char *name, const void *data)
{
	if (!rh->value_cache && (!(rh->value_cache = dm_hash_create(64)))) {
		log_error("Failed to create cache for values used during reporting.");
		return 0;
	}

	return dm_hash_insert(rh->value_cache, name, (void *) data);
}

const void *dm_report_value_cache_get(struct dm_report *rh, const char *name)
{
	return (rh->value_cache) ? dm_hash_lookup(rh->value_cache, name) : NULL;
}

/*
 * Used to check whether the reserved_values definition passed to
 * dm_report_init_with_selection contains only supported reserved value types.
 */
static int _check_reserved_values_supported(const struct dm_report_field_type fields[],
					    const struct dm_report_reserved_value reserved_values[])
{
	const struct dm_report_reserved_value *iter;
	const struct dm_report_field_reserved_value *field_res;
	const struct dm_report_field_type *field;
	static uint32_t supported_reserved_types = DM_REPORT_FIELD_TYPE_NUMBER |
						   DM_REPORT_FIELD_TYPE_SIZE |
						   DM_REPORT_FIELD_TYPE_PERCENT |
						   DM_REPORT_FIELD_TYPE_STRING |
						   DM_REPORT_FIELD_TYPE_TIME;
	static uint32_t supported_reserved_types_with_range = DM_REPORT_FIELD_RESERVED_VALUE_RANGE |
							      DM_REPORT_FIELD_TYPE_NUMBER |
							      DM_REPORT_FIELD_TYPE_SIZE |
							      DM_REPORT_FIELD_TYPE_PERCENT |
							      DM_REPORT_FIELD_TYPE_TIME;


	if (!reserved_values)
		return 1;

	iter = reserved_values;

	while (iter->value) {
		if (iter->type & DM_REPORT_FIELD_TYPE_MASK) {
			if (!(iter->type & supported_reserved_types) ||
			    ((iter->type & DM_REPORT_FIELD_RESERVED_VALUE_RANGE) &&
			     !(iter->type & supported_reserved_types_with_range))) {
				log_error(INTERNAL_ERROR "_check_reserved_values_supported: "
					  "global reserved value for type 0x%x not supported",
					   iter->type);
				return 0;
			}
		} else {
			field_res = (const struct dm_report_field_reserved_value *) iter->value;
			field = &fields[field_res->field_num];
			if (!(field->flags & supported_reserved_types) ||
			    ((iter->type & DM_REPORT_FIELD_RESERVED_VALUE_RANGE) &&
			     !(iter->type & supported_reserved_types_with_range))) {
				log_error(INTERNAL_ERROR "_check_reserved_values_supported: "
					  "field-specific reserved value of type 0x%x for "
					  "field %s not supported",
					   field->flags & DM_REPORT_FIELD_TYPE_MASK, field->id);
				return 0;
			}
		}
		iter++;
	}

	return 1;
}

/*
 * Input:
 *   ft              - field type for which the value is parsed
 *   s               - a pointer to the parsed string
 * Output:
 *   begin           - a pointer to the beginning of the token
 *   end             - a pointer to the end of the token + 1
 *   flags           - parsing flags
 */
static const char *_tok_value_regex(struct dm_report *rh,
				    const struct dm_report_field_type *ft,
				    const char *s, const char **begin,
				    const char **end, uint32_t *flags,
				    struct reserved_value_wrapper *rvw)
{
	char c;
	rvw->reserved = NULL;

	s = _skip_space(s);

	if (!*s) {
		log_error("Regular expression expected for selection field %s", ft->id);
		return NULL;
	}

	switch (*s) {
		case '(': c = ')'; break;
		case '{': c = '}'; break;
		case '[': c = ']'; break;
		case '"': /* fall through */
		case '\'': c = *s; break;
		default:  c = 0;
	}

	if (!(s = _tok_value_string(c ? s + 1 : s, begin, end, c, SEL_AND | SEL_OR | SEL_PRECEDENCE_PE, NULL))) {
		log_error("Failed to parse regex value for selection field %s.", ft->id);
		return NULL;
	}

	*flags |= DM_REPORT_FIELD_TYPE_STRING;
	return s;
}

static int _str_list_item_cmp(const void *a, const void *b)
{
	const struct dm_str_list * const *item_a = (const struct dm_str_list * const *) a;
	const struct dm_str_list * const *item_b = (const struct dm_str_list * const *) b;

	return strcmp((*item_a)->str, (*item_b)->str);
}

static int _add_item_to_string_list(struct dm_pool *mem, const char *begin,
				    const char *end, struct dm_list *list)
{
	struct dm_str_list *item;

	if (begin == end)
		return_0;

	if (!(item = dm_pool_zalloc(mem, sizeof(*item))) ||
	    !(item->str = dm_pool_strndup(mem, begin, end - begin))) {
		log_error("_add_item_to_string_list: memory allocation failed for string list item");
		return 0;
	}
	dm_list_add(list, &item->list);

	return 1;
}

/*
 * Input:
 *   ft              - field type for which the value is parsed
 *   mem             - memory pool to allocate from
 *   s               - a pointer to the parsed string
 * Output:
 *   begin           - a pointer to the beginning of the token (whole list)
 *   end             - a pointer to the end of the token + 1 (whole list)
 *   sel_str_list    - the list of strings parsed
 */
static const char *_tok_value_string_list(const struct dm_report_field_type *ft,
					  struct dm_pool *mem, const char *s,
					  const char **begin, const char **end,
					  struct selection_str_list **sel_str_list)
{
	static const char _str_list_item_parsing_failed[] = "Failed to parse string list value "
							    "for selection field %s.";
	struct selection_str_list *ssl = NULL;
	struct dm_str_list *item;
	const char *begin_item, *end_item, *tmp;
	uint32_t op_flags, end_op_flag_expected, end_op_flag_hit = 0;
	struct dm_str_list **arr;
	size_t list_size;
	unsigned int i;
	int list_end = 0;
	char c;

	if (!(ssl = dm_pool_alloc(mem, sizeof(*ssl))) ||
	    !(ssl->list = dm_pool_alloc(mem, sizeof(*ssl->list)))) {
		log_error("_tok_value_string_list: memory allocation failed for selection list");
		goto bad;
	}
	dm_list_init(ssl->list);
	ssl->type = 0;
	*begin = s;

	if (!(op_flags = _tok_op_log(s, &tmp, SEL_LIST_LS | SEL_LIST_SUBSET_LS))) {
		/* Only one item - SEL_LIST_{SUBSET_}LS and SEL_LIST_{SUBSET_}LE not used */
		c = _get_and_skip_quote_char(&s);
		if (!(s = _tok_value_string(s, &begin_item, &end_item, c, SEL_AND | SEL_OR | SEL_PRECEDENCE_PE, NULL))) {
			log_error(_str_list_item_parsing_failed, ft->id);
			goto bad;
		}
		if (!_add_item_to_string_list(mem, begin_item, end_item, ssl->list))
			goto_bad;
		ssl->type = SEL_OR | SEL_LIST_LS;
		goto out;
	}

	/* More than one item - items enclosed in SEL_LIST_LS and SEL_LIST_LE
	 * or SEL_LIST_SUBSET_LS and SEL_LIST_SUBSET_LE.
	 * Each element is terminated by AND or OR operator or 'list end'.
	 * The first operator hit is then the one allowed for the whole list,
	 * no mixing allowed!
	 */

	/* Are we using [] or {} for the list? */
	end_op_flag_expected = (op_flags == SEL_LIST_LS) ? SEL_LIST_LE : SEL_LIST_SUBSET_LE;

	op_flags = SEL_LIST_LE | SEL_LIST_SUBSET_LE | SEL_AND | SEL_OR;
	s++;
	while (*s) {
		s = _skip_space(s);
		c = _get_and_skip_quote_char(&s);
		if (!(s = _tok_value_string(s, &begin_item, &end_item, c, op_flags, NULL))) {
			log_error(_str_list_item_parsing_failed, ft->id);
			goto bad;
		}
		s = _skip_space(s);

		if (!(end_op_flag_hit = _tok_op_log(s, &tmp, op_flags))) {
			log_error("Invalid operator in selection list.");
			goto bad;
		}

		if (end_op_flag_hit & (SEL_LIST_LE | SEL_LIST_SUBSET_LE)) {
			list_end = 1;
			if (end_op_flag_hit != end_op_flag_expected) {
				for (i = 0; _op_log[i].string; i++)
					if (_op_log[i].flags == end_op_flag_expected)
						break;
				log_error("List ended with incorrect character, "
					  "expecting \'%s\'.", _op_log[i].string);
				goto bad;
			}
		}

		if (ssl->type) {
			if (!list_end && !(ssl->type & end_op_flag_hit)) {
				log_error("Only one type of logical operator allowed "
					  "in selection list at a time.");
				goto bad;
			}
		} else {
			if (list_end)
				ssl->type = end_op_flag_expected == SEL_LIST_LE ? SEL_AND : SEL_OR;
			else
				ssl->type = end_op_flag_hit;
		}

		if (!_add_item_to_string_list(mem, begin_item, end_item, ssl->list))
			goto_bad;

		s = tmp;

		if (list_end)
			break;
	}

	if (!(end_op_flag_hit & (SEL_LIST_LE | SEL_LIST_SUBSET_LE))) {
		log_error("Missing list end for selection field %s", ft->id);
		goto bad;
	}

	/* Store information whether [] or {} was used. */
	if (end_op_flag_expected == SEL_LIST_LE)
		ssl->type |= SEL_LIST_LS;
	else
		ssl->type |= SEL_LIST_SUBSET_LS;

	/* Sort the list. */
	if (!(list_size = dm_list_size(ssl->list))) {
		log_error(INTERNAL_ERROR "_tok_value_string_list: list has no items");
		goto bad;
	} else if (list_size == 1)
		goto out;
	if (!(arr = dm_malloc(sizeof(item) * list_size))) {
		log_error("_tok_value_string_list: memory allocation failed for sort array");
		goto bad;
	}

	i = 0;
	dm_list_iterate_items(item, ssl->list)
		arr[i++] = item;
	qsort(arr, list_size, sizeof(item), _str_list_item_cmp);
	dm_list_init(ssl->list);
	for (i = 0; i < list_size; i++)
		dm_list_add(ssl->list, &arr[i]->list);

	dm_free(arr);
out:
	*end = s;
	*sel_str_list = ssl;
	return s;
bad:
	*end = s;
	if (ssl)
		dm_pool_free(mem, ssl);
	*sel_str_list = NULL;
	return s;
}

struct time_value {
	int range;
	time_t t1;
	time_t t2;
};

static const char *_out_of_range_msg = "Field selection value %s out of supported range for field %s.";

/*
 * Standard formatted date and time - ISO8601.
 *
 * date time timezone
 *
 * date:
 * YYYY-MM-DD (or shortly YYYYMMDD)
 * YYYY-MM (shortly YYYYMM), auto DD=1
 * YYYY, auto MM=01 and DD=01
 *
 * time:
 * hh:mm:ss (or shortly hhmmss)
 * hh:mm (or shortly hhmm), auto ss=0
 * hh (or shortly hh), auto mm=0, auto ss=0
 *
 * timezone:
 * +hh:mm or -hh:mm (or shortly +hhmm or -hhmm)
 * +hh or -hh
*/

#define DELIM_DATE '-'
#define DELIM_TIME ':'

static int _days_in_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

static int _is_leap_year(long year)
{
	return (((year % 4==0) && (year % 100 != 0)) || (year % 400 == 0));
}

static int _get_days_in_month(long month, long year)
{
	return (month == 2 && _is_leap_year(year)) ? _days_in_month[month-1] + 1
						   : _days_in_month[month-1];
}

typedef enum {
	RANGE_NONE,
	RANGE_SECOND,
	RANGE_MINUTE,
	RANGE_HOUR,
	RANGE_DAY,
	RANGE_MONTH,
	RANGE_YEAR
} time_range_t;

static char *_get_date(char *str, struct tm *tm, time_range_t *range)
{
	static const char incorrect_date_format_msg[] = "Incorrect date format.";
	time_range_t tmp_range = RANGE_NONE;
	long n1, n2 = -1, n3 = -1;
	char *s = str, *end;
	size_t len = 0;

	if (!isdigit(*s))
		/* we need a year at least */
		return NULL;

	n1 = strtol(s, &end, 10);
	if (*end == DELIM_DATE) {
		len += (4 - (end - s)); /* diff in length from standard YYYY */
		s = end + 1;
		if (isdigit(*s)) {
			n2 = strtol(s, &end, 10);
			len += (2 - (end - s)); /* diff in length from standard MM */
			if (*end == DELIM_DATE) {
				s = end + 1;
				n3 = strtol(s, &end, 10);
				len += (2 - (end - s)); /* diff in length from standard DD */
			}
		}
	}

	len = len + end - str;

	/* variations from standard YYYY-MM-DD */
	if (n3 == -1) {
		if (n2 == -1) {
			if (len == 4) {
				/* YYYY */
				tmp_range = RANGE_YEAR;
				n3 = n2 = 1;
			} else if (len == 6) {
				/* YYYYMM */
				tmp_range = RANGE_MONTH;
				n3 = 1;
				n2 = n1 % 100;
				n1 = n1 / 100;
			} else if (len == 8) {
				tmp_range = RANGE_DAY;
				/* YYYYMMDD */
				n3 = n1 % 100;
				n2 = (n1 / 100) % 100;
				n1 = n1 / 10000;
			} else {
				log_error(incorrect_date_format_msg);
				return NULL;
			}
		} else {
			if (len == 7) {
				tmp_range = RANGE_MONTH;
				/* YYYY-MM */
				n3 = 1;
			} else {
				log_error(incorrect_date_format_msg);
				return NULL;
			}
		}
	}

	if (n2 < 1 || n2 > 12) {
		log_error("Specified month out of range.");
		return NULL;
	}

	if (n3 < 1 || n3 > _get_days_in_month(n2, n1)) {
		log_error("Specified day out of range.");
		return NULL;
	}

	if (tmp_range == RANGE_NONE)
		tmp_range = RANGE_DAY;

	tm->tm_year = n1 - 1900;
	tm->tm_mon = n2 - 1;
	tm->tm_mday = n3;
	*range = tmp_range;

	return (char *) _skip_space(end);
}

static char *_get_time(char *str, struct tm *tm, time_range_t *range)
{
	static const char incorrect_time_format_msg[] = "Incorrect time format.";
	time_range_t tmp_range = RANGE_NONE;
	long n1, n2 = -1, n3 = -1;
	char *s = str, *end;
	size_t len = 0;

	if (!isdigit(*s)) {
		/* time is not compulsory */
		tm->tm_hour = tm->tm_min = tm->tm_sec = 0;
		return (char *) _skip_space(s);
	}

	n1 = strtol(s, &end, 10);
	if (*end == DELIM_TIME) {
		len += (2 - (end - s)); /* diff in length from standard HH */
		s = end + 1;
		if (isdigit(*s)) {
			n2 = strtol(s, &end, 10);
			len += (2 - (end - s)); /* diff in length from standard MM */
			if (*end == DELIM_TIME) {
				s = end + 1;
				n3 = strtol(s, &end, 10);
				len += (2 - (end - s)); /* diff in length from standard SS */
			}
		}
	}

	len = len + end - str;

	/* variations from standard HH:MM:SS */
	if (n3 == -1) {
		if (n2 == -1) {
			if (len == 2) {
				/* HH */
				tmp_range = RANGE_HOUR;
				n3 = n2 = 0;
			} else if (len == 4) {
				/* HHMM */
				tmp_range = RANGE_MINUTE;
				n3 = 0;
				n2 = n1 % 100;
				n1 = n1 / 100;
			} else if (len == 6) {
				/* HHMMSS */
				tmp_range = RANGE_SECOND;
				n3 = n1 % 100;
				n2 = (n1 / 100) % 100;
				n1 = n1 / 10000;
			} else {
				log_error(incorrect_time_format_msg);
				return NULL;
			}
		} else {
			if (len == 5) {
				/* HH:MM */
				tmp_range = RANGE_MINUTE;
				n3 = 0;
			} else {
				log_error(incorrect_time_format_msg);
				return NULL;
			}
		}
	}

	if (n1 < 0 || n1 > 23) {
		log_error("Specified hours out of range.");
		return NULL;
	}

	if (n2 < 0 || n2 > 60) {
		log_error("Specified minutes out of range.");
		return NULL;
	}

	if (n3 < 0 || n3 > 60) {
		log_error("Specified seconds out of range.");
		return NULL;
	}

	/* Just time without exact date is incomplete! */
	if (*range != RANGE_DAY) {
		log_error("Full date specification needed.");
		return NULL;
	}

	tm->tm_hour = n1;
	tm->tm_min = n2;
	tm->tm_sec = n3;
	*range = tmp_range;

	return (char *) _skip_space(end);
}

/* The offset is always an absolute offset against GMT! */
static char *_get_tz(char *str, int *tz_supplied, int *offset)
{
	long n1, n2 = -1;
	char *s = str, *end;
	int sign = 1; /* +HH:MM by default */
	size_t len = 0;

	*tz_supplied = 0;
	*offset = 0;

	if (!isdigit(*s)) {
		if (*s == '+')  {
			sign = 1;
			s = s + 1;
		} else if (*s == '-') {
			sign = -1;
			s = s + 1;
		} else
			return (char *) _skip_space(s);
	}

	n1 = strtol(s, &end, 10);
	if (*end == DELIM_TIME) {
		len = (2 - (end - s)); /* diff in length from standard HH */
		s = end + 1;
		if (isdigit(*s)) {
			n2 = strtol(s, &end, 10);
			len = (2 - (end - s)); /* diff in length from standard MM */
		}
	}

	len = len + end - s;

	/* variations from standard HH:MM */
	if (n2 == -1) {
		if (len == 2) {
			/* HH */
			n2 = 0;
		} else if (len == 4) {
			/* HHMM */
			n2 = n1 % 100;
			n1 = n1 / 100;
		} else
			return NULL;
	}

	if (n2 < 0 || n2 > 60)
		return NULL;

	if (n1 < 0 || n1 > 14)
		return NULL;

	/* timezone offset in seconds */
	*offset = sign * ((n1 * 3600) + (n2 * 60));
	*tz_supplied = 1;
	return (char *) _skip_space(end);
}

static int _local_tz_offset(time_t t_local)
{
	struct tm tm_gmt;
	time_t t_gmt;

	gmtime_r(&t_local, &tm_gmt);
	t_gmt = mktime(&tm_gmt);

	/*
	 * gmtime returns time that is adjusted
	 * for DST.Subtract this adjustment back
	 * to give us proper *absolute* offset
	 * for our local timezone.
	 */
	if (tm_gmt.tm_isdst)
		t_gmt -= 3600;

	return t_local - t_gmt;
}

static void _get_final_time(time_range_t range, struct tm *tm,
			    int tz_supplied, int offset,
			    struct time_value *tval)
{

	struct tm tm_up = *tm;

	switch (range) {
		case RANGE_SECOND:
			if (tm_up.tm_sec < 59) {
				tm_up.tm_sec += 1;
				break;
			}
		case RANGE_MINUTE:
			if (tm_up.tm_min < 59) {
				tm_up.tm_min += 1;
				break;
			}
		case RANGE_HOUR:
			if (tm_up.tm_hour < 23) {
				tm_up.tm_hour += 1;
				break;
			}
		case RANGE_DAY:
			if (tm_up.tm_mday < _get_days_in_month(tm_up.tm_mon, tm_up.tm_year)) {
				tm_up.tm_mday += 1;
				break;
			}
		case RANGE_MONTH:
			if (tm_up.tm_mon < 11) {
				tm_up.tm_mon += 1;
				break;
			}
		case RANGE_YEAR:
			tm_up.tm_year += 1;
			break;
		case RANGE_NONE:
			/* nothing to do here */
			break;
	}

	tval->range = (range != RANGE_NONE);
	tval->t1 = mktime(tm);
	tval->t2 = mktime(&tm_up) - 1;

	if (tz_supplied) {
		/*
		 * The 'offset' is with respect to the GMT.
		 * Calculate what the offset is with respect
		 * to our local timezone and adjust times
		 * so they represent time in our local timezone.
		 */
		offset -= _local_tz_offset(tval->t1);
		tval->t1 -= offset;
		tval->t2 -= offset;
	}
}

static int _parse_formatted_date_time(char *str, struct time_value *tval)
{
	time_range_t range = RANGE_NONE;
	struct tm tm = {0};
	int gmt_offset;
	int tz_supplied;

	tm.tm_year = tm.tm_mday = tm.tm_mon = -1;
	tm.tm_hour = tm.tm_min = tm.tm_sec = -1;
	tm.tm_isdst = tm.tm_wday = tm.tm_yday = -1;

	if (!(str = _get_date(str, &tm, &range)))
		return 0;

	if (!(str = _get_time(str, &tm, &range)))
		return 0;

	if (!(str = _get_tz(str, &tz_supplied, &gmt_offset)))
		return 0;

	if (*str)
		return 0;

	_get_final_time(range, &tm, tz_supplied, gmt_offset, tval);

	return 1;
}

static const char *_tok_value_time(const struct dm_report_field_type *ft,
				   struct dm_pool *mem, const char *s,
				   const char **begin, const char **end,
				   struct time_value *tval)
{
	char *time_str = NULL;
	const char *r = NULL;
	uint64_t t;
	char c;

	s = _skip_space(s);

	if (*s == '@') {
		/* Absolute time value in number of seconds since epoch. */
		if (!(s = _tok_value_number(s+1, begin, end)))
			goto_out;

		if (!(time_str = dm_pool_strndup(mem, *begin, *end - *begin))) {
			log_error("_tok_value_time: dm_pool_strndup failed");
			goto out;
		}

		if (((t = strtoull(time_str, NULL, 10)) == ULLONG_MAX) && errno == ERANGE) {
			log_error(_out_of_range_msg, time_str, ft->id);
			goto out;
		}

		tval->range = 0;
		tval->t1 = (time_t) t;
		tval->t2 = 0;
		r = s;
	} else {
		c = _get_and_skip_quote_char(&s);
		if (!(s = _tok_value_string(s, begin, end, c, SEL_AND | SEL_OR | SEL_PRECEDENCE_PE, NULL)))
			goto_out;

		if (!(time_str = dm_pool_strndup(mem, *begin, *end - *begin))) {
			log_error("tok_value_time: dm_pool_strndup failed");
			goto out;
		}

		if (!_parse_formatted_date_time(time_str, tval))
			goto_out;
		r = s;
	}
out:
	if (time_str)
		dm_pool_free(mem, time_str);
	return r;
}

/*
 * Input:
 *   ft              - field type for which the value is parsed
 *   s               - a pointer to the parsed string
 *   mem             - memory pool to allocate from
 * Output:
 *   begin           - a pointer to the beginning of the token
 *   end             - a pointer to the end of the token + 1
 *   flags           - parsing flags
 *   custom          - custom data specific to token type
 *                     (e.g. size unit factor)
 */
static const char *_tok_value(struct dm_report *rh,
			      const struct dm_report_field_type *ft,
			      uint32_t field_num, int implicit,
			      const char *s,
			      const char **begin, const char **end,
			      uint32_t *flags,
			      struct reserved_value_wrapper *rvw,
			      struct dm_pool *mem, void *custom)
{
	int expected_type = ft->flags & DM_REPORT_FIELD_TYPE_MASK;
	struct selection_str_list **str_list;
	struct time_value *tval;
	uint64_t *factor;
	const char *tmp;
	char c;

	s = _skip_space(s);

	s = _get_reserved(rh, expected_type, field_num, implicit, s, begin, end, rvw);
	if (rvw->reserved) {
		/*
		 * FLD_CMP_NUMBER shares operators with FLD_CMP_TIME,
		 * so adjust flags here based on expected type.
		 */
		if (expected_type == DM_REPORT_FIELD_TYPE_TIME)
			*flags &= ~FLD_CMP_NUMBER;
		else if (expected_type == DM_REPORT_FIELD_TYPE_NUMBER)
			*flags &= ~FLD_CMP_TIME;
		*flags |= expected_type;
		return s;
	}

	switch (expected_type) {

		case DM_REPORT_FIELD_TYPE_STRING:
			c = _get_and_skip_quote_char(&s);
			if (!(s = _tok_value_string(s, begin, end, c, SEL_AND | SEL_OR | SEL_PRECEDENCE_PE, NULL))) {
				log_error("Failed to parse string value "
					  "for selection field %s.", ft->id);
				return NULL;
			}
			*flags |= DM_REPORT_FIELD_TYPE_STRING;
			break;

		case DM_REPORT_FIELD_TYPE_STRING_LIST:
			str_list = (struct selection_str_list **) custom;
			s = _tok_value_string_list(ft, mem, s, begin, end, str_list);
			if (!(*str_list)) {
				log_error("Failed to parse string list value "
					  "for selection field %s.", ft->id);
				return NULL;
			}
			*flags |= DM_REPORT_FIELD_TYPE_STRING_LIST;
			break;

		case DM_REPORT_FIELD_TYPE_NUMBER:
			/* fall through */
		case DM_REPORT_FIELD_TYPE_SIZE:
			/* fall through */
		case DM_REPORT_FIELD_TYPE_PERCENT:
			if (!(s = _tok_value_number(s, begin, end))) {
				log_error("Failed to parse numeric value "
					  "for selection field %s.", ft->id);
				return NULL;
			}

			factor = (uint64_t *) custom;

			if (*s == DM_PERCENT_CHAR) {
				s++;
				c = DM_PERCENT_CHAR;
				if (expected_type != DM_REPORT_FIELD_TYPE_PERCENT) {
					log_error("Found percent value but %s value "
						  "expected for selection field %s.",
						  expected_type == DM_REPORT_FIELD_TYPE_NUMBER ?
							"numeric" : "size", ft->id);
					return NULL;
				}
			} else if ((*factor = dm_units_to_factor(s, &c, 0, &tmp))) {
				s = tmp;
				if (expected_type != DM_REPORT_FIELD_TYPE_SIZE) {
					log_error("Found size unit specifier "
						  "but %s value expected for "
						  "selection field %s.",
						  expected_type == DM_REPORT_FIELD_TYPE_NUMBER ?
							"numeric" : "percent", ft->id);
					return NULL;
				}
			} else if (expected_type == DM_REPORT_FIELD_TYPE_SIZE) {
				/*
				 * If size unit is not defined in the selection
				 * and the type expected is size, use use 'm'
				 * (1 MiB) for the unit by default. This is the
				 * same behaviour as seen in lvcreate -L <size>.
				 */
				*factor = 1024*1024;
			}

			*flags |= expected_type;
			/*
			 * FLD_CMP_NUMBER shares operators with FLD_CMP_TIME,
			 * but we have NUMBER here, so remove FLD_CMP_TIME.
			 */
			*flags &= ~FLD_CMP_TIME;
			break;

		case DM_REPORT_FIELD_TYPE_TIME:
			tval = (struct time_value *) custom;
			if (!(s = _tok_value_time(ft, mem, s, begin, end, tval))) {
				log_error("Failed to parse time value "
					  "for selection field %s.", ft->id);
				return NULL;
			}

			*flags |= DM_REPORT_FIELD_TYPE_TIME;
			/*
			 * FLD_CMP_TIME shares operators with FLD_CMP_NUMBER,
			 * but we have TIME here, so remove FLD_CMP_NUMBER.
			 */
			*flags &= ~FLD_CMP_NUMBER;
			break;
	}

	return s;
}

/*
 * Input:
 *   s               - a pointer to the parsed string
 * Output:
 *   begin           - a pointer to the beginning of the token
 *   end             - a pointer to the end of the token + 1
 */
static const char *_tok_field_name(const char *s,
				    const char **begin, const char **end)
{
	char c;
	s = _skip_space(s);

	*begin = s;
	while ((c = *s) &&
	       (isalnum(c) || c == '_' || c == '-'))
		s++;
	*end = s;

	if (*begin == *end)
		return NULL;

	return s;
}

static int _get_reserved_value(struct dm_report *rh, uint32_t field_num,
			       struct reserved_value_wrapper *rvw)
{
	const void *tmp_value;
	dm_report_reserved_handler handler;
	int r;

	if (!rvw->reserved) {
		rvw->value = NULL;
		return 1;
	}

	if (rvw->reserved->type & DM_REPORT_FIELD_TYPE_MASK)
		/* type reserved value */
		tmp_value = rvw->reserved->value;
	else
		/* per-field reserved value */
		tmp_value = ((const struct dm_report_field_reserved_value *) rvw->reserved->value)->value;

	if (rvw->reserved->type & (DM_REPORT_FIELD_RESERVED_VALUE_DYNAMIC_VALUE | DM_REPORT_FIELD_RESERVED_VALUE_FUZZY_NAMES)) {
		handler = (dm_report_reserved_handler) tmp_value;
		if ((r = handler(rh, rh->selection->mem, field_num,
				 DM_REPORT_RESERVED_GET_DYNAMIC_VALUE,
				 rvw->matched_name, &tmp_value) <= 0)) {
			if (r == -1)
				log_error(INTERNAL_ERROR "%s reserved value handler for field %s has missing"
					  "implementation of DM_REPORT_RESERVED_GET_DYNAMIC_VALUE action",
					  (rvw->reserved->type) & DM_REPORT_FIELD_TYPE_MASK ? "type-specific" : "field-specific",
					  rh->fields[field_num].id);
			else
				log_error("Error occured while processing %s reserved value handler for field %s",
					  (rvw->reserved->type) & DM_REPORT_FIELD_TYPE_MASK ? "type-specific" : "field-specific",
					  rh->fields[field_num].id);
			return 0;
		}
	}

	rvw->value = tmp_value;
	return 1;
}

static struct field_selection *_create_field_selection(struct dm_report *rh,
						       uint32_t field_num,
						       int implicit,
						       const char *v,
						       size_t len,
						       uint32_t flags,
						       struct reserved_value_wrapper *rvw,
						       void *custom)
{
	static const char *_field_selection_value_alloc_failed_msg = "dm_report: struct field_selection_value allocation failed for selection field %s";
	const struct dm_report_field_type *fields = implicit ? _implicit_report_fields
							     : rh->fields;
	struct field_properties *fp, *found = NULL;
	struct field_selection *fs;
	const char *field_id;
	struct time_value *tval;
	uint64_t factor;
	char *s;

	dm_list_iterate_items(fp, &rh->field_props) {
		if ((fp->implicit == implicit) && (fp->field_num == field_num)) {
			found = fp;
			break;
		}
	}

	/* The field is neither used in display options nor sort keys. */
	if (!found) {
		if (!(found = _add_field(rh, field_num, implicit, FLD_HIDDEN)))
			return NULL;
		rh->report_types |= fields[field_num].type;
	}

	field_id = fields[found->field_num].id;

	if (!(found->flags & flags & DM_REPORT_FIELD_TYPE_MASK)) {
		log_error("dm_report: incompatible comparison "
			  "type for selection field %s", field_id);
		return NULL;
	}

	/* set up selection */
	if (!(fs = dm_pool_zalloc(rh->selection->mem, sizeof(struct field_selection)))) {
		log_error("dm_report: struct field_selection "
			  "allocation failed for selection field %s", field_id);
		return NULL;
	}

	if (!(fs->value = dm_pool_zalloc(rh->selection->mem, sizeof(struct field_selection_value)))) {
		log_error(_field_selection_value_alloc_failed_msg, field_id);
		goto error;
	}

	if (((rvw->reserved && (rvw->reserved->type & DM_REPORT_FIELD_RESERVED_VALUE_RANGE)) ||
	    (((flags & DM_REPORT_FIELD_TYPE_MASK) == DM_REPORT_FIELD_TYPE_TIME) && ((struct time_value *) custom)->range))
		 &&
	    !(fs->value->next = dm_pool_zalloc(rh->selection->mem, sizeof(struct field_selection_value)))) {
		log_error(_field_selection_value_alloc_failed_msg, field_id);
		goto error;
	}

	fs->fp = found;
	fs->flags = flags;

	if (!_get_reserved_value(rh, field_num, rvw)) {
		log_error("dm_report: could not get reserved value "
			  "while processing selection field %s", field_id);
		goto error;
	}

	/* store comparison operand */
	if (flags & FLD_CMP_REGEX) {
		/* REGEX */
		if (!(s = dm_malloc(len + 1))) {
			log_error("dm_report: dm_malloc failed to store "
				  "regex value for selection field %s", field_id);
			goto error;
		}
		memcpy(s, v, len);
		s[len] = '\0';

		fs->value->v.r = dm_regex_create(rh->selection->mem, (const char * const *) &s, 1);
		dm_free(s);
		if (!fs->value->v.r) {
			log_error("dm_report: failed to create regex "
				  "matcher for selection field %s", field_id);
			goto error;
		}
	} else {
		/* STRING, NUMBER, SIZE, PERCENT, STRING_LIST, TIME */
		if (!(s = dm_pool_strndup(rh->selection->mem, v, len))) {
			log_error("dm_report: dm_pool_strndup for value "
				  "of selection field %s", field_id);
			goto error;
		}

		switch (flags & DM_REPORT_FIELD_TYPE_MASK) {
			case DM_REPORT_FIELD_TYPE_STRING:
				if (rvw->value) {
					fs->value->v.s = (const char *) rvw->value;
					if (rvw->reserved->type & DM_REPORT_FIELD_RESERVED_VALUE_RANGE)
						fs->value->next->v.s = (((const char * const *) rvw->value)[1]);
					dm_pool_free(rh->selection->mem, s);
				} else {
					fs->value->v.s = s;
					if (_check_value_is_strictly_reserved(rh, field_num, DM_REPORT_FIELD_TYPE_STRING, fs->value->v.s, NULL)) {
						log_error("String value %s found in selection is reserved.", fs->value->v.s);
						goto error;
					}
				}
				break;
			case DM_REPORT_FIELD_TYPE_NUMBER:
				if (rvw->value) {
					fs->value->v.i = *(const uint64_t *) rvw->value;
					if (rvw->reserved->type & DM_REPORT_FIELD_RESERVED_VALUE_RANGE)
						fs->value->next->v.i = (((const uint64_t *) rvw->value)[1]);
				} else {
					if (((fs->value->v.i = strtoull(s, NULL, 10)) == ULLONG_MAX) &&
						 (errno == ERANGE)) {
						log_error(_out_of_range_msg, s, field_id);
						goto error;
					}
					if (_check_value_is_strictly_reserved(rh, field_num, DM_REPORT_FIELD_TYPE_NUMBER, &fs->value->v.i, NULL)) {
						log_error("Numeric value %" PRIu64 " found in selection is reserved.", fs->value->v.i);
						goto error;
					}
				}
				dm_pool_free(rh->selection->mem, s);
				break;
			case DM_REPORT_FIELD_TYPE_SIZE:
				if (rvw->value) {
					fs->value->v.d = *(const double *) rvw->value;
					if (rvw->reserved->type & DM_REPORT_FIELD_RESERVED_VALUE_RANGE)
						fs->value->next->v.d = (((const double *) rvw->value)[1]);
				} else {
					fs->value->v.d = strtod(s, NULL);
					if (errno == ERANGE) {
						log_error(_out_of_range_msg, s, field_id);
						goto error;
					}
					if (custom && (factor = *((const uint64_t *)custom)))
						fs->value->v.d *= factor;
					fs->value->v.d /= 512; /* store size in sectors! */
					if (_check_value_is_strictly_reserved(rh, field_num, DM_REPORT_FIELD_TYPE_SIZE, &fs->value->v.d, NULL)) {
						log_error("Size value %f found in selection is reserved.", fs->value->v.d);
						goto error;
					}
				}
				dm_pool_free(rh->selection->mem, s);
				break;
			case DM_REPORT_FIELD_TYPE_PERCENT:
				if (rvw->value) {
					fs->value->v.i = *(const uint64_t *) rvw->value;
					if (rvw->reserved->type & DM_REPORT_FIELD_RESERVED_VALUE_RANGE)
						fs->value->next->v.i = (((const uint64_t *) rvw->value)[1]);
				} else {
					fs->value->v.d = strtod(s, NULL);
					if ((errno == ERANGE) || (fs->value->v.d < 0) || (fs->value->v.d > 100)) {
						log_error(_out_of_range_msg, s, field_id);
						goto error;
					}

					fs->value->v.i = (dm_percent_t) (DM_PERCENT_1 * fs->value->v.d);

					if (_check_value_is_strictly_reserved(rh, field_num, DM_REPORT_FIELD_TYPE_PERCENT, &fs->value->v.i, NULL)) {
						log_error("Percent value %s found in selection is reserved.", s);
						goto error;
					}
				}
				break;
			case DM_REPORT_FIELD_TYPE_STRING_LIST:
				fs->value->v.l = *(struct selection_str_list **)custom;
				if (_check_value_is_strictly_reserved(rh, field_num, DM_REPORT_FIELD_TYPE_STRING_LIST, fs->value->v.l, NULL)) {
					log_error("String list value found in selection is reserved.");
					goto error;
				}
				break;
			case DM_REPORT_FIELD_TYPE_TIME:
				if (rvw->value) {
					fs->value->v.t = *(const time_t *) rvw->value;
					if (rvw->reserved->type & DM_REPORT_FIELD_RESERVED_VALUE_RANGE)
						fs->value->next->v.t = (((const time_t *) rvw->value)[1]);
				} else {
					tval = (struct time_value *) custom;
					fs->value->v.t = tval->t1;
					if (tval->range)
						fs->value->next->v.t = tval->t2;
					if (_check_value_is_strictly_reserved(rh, field_num, DM_REPORT_FIELD_TYPE_TIME, &fs->value->v.t, NULL)) {
						log_error("Time value found in selection is reserved.");
						goto error;
					}
				}
				break;
			default:
				log_error(INTERNAL_ERROR "_create_field_selection: "
					  "unknown type of selection field %s", field_id);
				goto error;
		}
	}

	return fs;
error:
	dm_pool_free(rh->selection->mem, fs);
	return NULL;
}

static struct selection_node *_alloc_selection_node(struct dm_pool *mem, uint32_t type)
{
	struct selection_node *sn;

	if (!(sn = dm_pool_zalloc(mem, sizeof(struct selection_node)))) {
		log_error("dm_report: struct selection_node allocation failed");
		return NULL;
	}

	dm_list_init(&sn->list);
	sn->type = type;
	if (!(type & SEL_ITEM))
		dm_list_init(&sn->selection.set);

	return sn;
}

static void _display_selection_help(struct dm_report *rh)
{
	static const char _grow_object_failed_msg[] = "_display_selection_help: dm_pool_grow_object failed";
	struct op_def *t;
	const struct dm_report_reserved_value *rv;
	size_t len_all, len_final = 0;
	const char **rvs;
	char *rvs_all;

	log_warn("Selection operands");
	log_warn("------------------");
	log_warn("  field               - Reporting field.");
	log_warn("  number              - Non-negative integer value.");
	log_warn("  size                - Floating point value with units, 'm' unit used by default if not specified.");
	log_warn("  percent             - Non-negative integer with or without %% suffix.");
	log_warn("  string              - Characters quoted by \' or \" or unquoted.");
	log_warn("  string list         - Strings enclosed by [ ] or { } and elements delimited by either");
	log_warn("                        \"all items must match\" or \"at least one item must match\" operator.");
	log_warn("  regular expression  - Characters quoted by \' or \" or unquoted.");
	log_warn(" ");
	if (rh->reserved_values) {
		log_warn("Reserved values");
		log_warn("---------------");

		for (rv = rh->reserved_values; rv->type; rv++) {
			for (len_all = 0, rvs = rv->names; *rvs; rvs++)
				len_all += strlen(*rvs) + 2;
			if (len_all > len_final)
				len_final = len_all;
		}

		for (rv = rh->reserved_values; rv->type; rv++) {
			if (!dm_pool_begin_object(rh->mem, 256)) {
				log_error("_display_selection_help: dm_pool_begin_object failed");
				break;
			}
			for (rvs = rv->names; *rvs; rvs++) {
				if (((rvs != rv->names) && !dm_pool_grow_object(rh->mem, ", ", 2)) ||
				    !dm_pool_grow_object(rh->mem, *rvs, strlen(*rvs))) {
					log_error(_grow_object_failed_msg);
					goto out_reserved_values;
				}
			}
			if (!dm_pool_grow_object(rh->mem, "\0", 1)) {
				log_error(_grow_object_failed_msg);
				goto out_reserved_values;
			}
			rvs_all = dm_pool_end_object(rh->mem);

			log_warn("  %-*s - %s [%s]", (int) len_final, rvs_all, rv->description,
						     _get_field_type_name(rv->type));
			dm_pool_free(rh->mem, rvs_all);
		}
		log_warn(" ");
	}
out_reserved_values:
	log_warn("Selection operators");
	log_warn("-------------------");
	log_warn("  Comparison operators:");
	t = _op_cmp;
	for (; t->string; t++)
		log_warn("    %6s  - %s", t->string, t->desc);
	log_warn(" ");
	log_warn("  Logical and grouping operators:");
	t = _op_log;
	for (; t->string; t++)
		log_warn("    %4s  - %s", t->string, t->desc);
	log_warn(" ");
}

static const char _sel_syntax_error_at_msg[] = "Selection syntax error at '%s'.";
static const char _sel_help_ref_msg[] = "Use \'help\' for selection to get more help.";

/*
 * Selection parser
 *
 * _parse_* functions
 *
 *   Input:
 *     s             - a pointer to the parsed string
 *   Output:
 *     next          - a pointer used for next _parse_*'s input,
 *                     next == s if return value is NULL
 *     return value  - a filter node pointer,
 *                     NULL if s doesn't match
 */

/*
 * SELECTION := FIELD_NAME OP_CMP STRING |
 *              FIELD_NAME OP_CMP NUMBER  |
 *              FIELD_NAME OP_REGEX REGEX
 */
static struct selection_node *_parse_selection(struct dm_report *rh,
					       const char *s,
					       const char **next)
{
	struct field_selection *fs;
	struct selection_node *sn;
	const char *ws, *we; /* field name */
	const char *vs, *ve; /* value */
	const char *last;
	uint32_t flags, field_num;
	int implicit;
	const struct dm_report_field_type *ft;
	struct selection_str_list *str_list;
	struct reserved_value_wrapper rvw = {0};
	struct time_value tval;
	uint64_t factor;
	void *custom = NULL;
	char *tmp;
	char c;

	/* field name */
	if (!(last = _tok_field_name(s, &ws, &we))) {
		log_error("Expecting field name");
		goto bad;
	}

	/* check if the field with given name exists */
	if (!_get_field(rh, ws, (size_t) (we - ws), &field_num, &implicit)) {
		c = we[0];
		tmp = (char *) we;
		tmp[0] = '\0';
		_display_fields(rh, 0, 1);
		log_warn(" ");
		log_error("Unrecognised selection field: %s", ws);
		tmp[0] = c;
		goto bad;
	}

	if (implicit) {
		ft = &_implicit_report_fields[field_num];
		if (ft->flags & FLD_CMP_UNCOMPARABLE) {
			c = we[0];
			tmp = (char *) we;
			tmp[0] = '\0';
			_display_fields(rh, 0, 1);
			log_warn(" ");
			log_error("Selection field is uncomparable: %s.", ws);
			tmp[0] = c;
			goto bad;
		}
	} else
		ft = &rh->fields[field_num];

	/* comparison operator */
	if (!(flags = _tok_op_cmp(we, &last))) {
		_display_selection_help(rh);
		log_error("Unrecognised comparison operator: %s", we);
		goto bad;
	}
	if (!last) {
		_display_selection_help(rh);
		log_error("Missing value after operator");
		goto bad;
	}

	/* comparison value */
	if (flags & FLD_CMP_REGEX) {
		/*
		 * REGEX value
		 */
		if (!(last = _tok_value_regex(rh, ft, last, &vs, &ve, &flags, &rvw)))
			goto_bad;
	} else {
		/*
		 * STRING, NUMBER, SIZE, PERCENT, STRING_LIST, TIME value
		 */
		if (flags & FLD_CMP_NUMBER) {
			if (!(ft->flags & (DM_REPORT_FIELD_TYPE_NUMBER |
					   DM_REPORT_FIELD_TYPE_SIZE |
					   DM_REPORT_FIELD_TYPE_PERCENT |
					   DM_REPORT_FIELD_TYPE_TIME))) {
				_display_selection_help(rh);
				log_error("Operator can be used only with number, size, time or percent fields: %s", ws);
				goto bad;
			}
		} else if (flags & FLD_CMP_TIME) {
			if (!(ft->flags & DM_REPORT_FIELD_TYPE_TIME)) {
				_display_selection_help(rh);
				log_error("Operator can be used only with time fields: %s", ws);
				goto bad;
			}
		}

		if (ft->flags == DM_REPORT_FIELD_TYPE_SIZE ||
		    ft->flags == DM_REPORT_FIELD_TYPE_NUMBER ||
		    ft->flags == DM_REPORT_FIELD_TYPE_PERCENT)
			custom = &factor;
		else if (ft->flags & DM_REPORT_FIELD_TYPE_TIME)
			custom = &tval;
		else if (ft->flags == DM_REPORT_FIELD_TYPE_STRING_LIST)
			custom = &str_list;
		else
			custom = NULL;
		if (!(last = _tok_value(rh, ft, field_num, implicit,
					last, &vs, &ve, &flags,
					&rvw, rh->selection->mem, custom)))
			goto_bad;
	}

	*next = _skip_space(last);

	/* create selection */
	if (!(fs = _create_field_selection(rh, field_num, implicit, vs, (size_t) (ve - vs), flags, &rvw, custom)))
		return_NULL;

	/* create selection node */
	if (!(sn = _alloc_selection_node(rh->selection->mem, SEL_ITEM)))
		return_NULL;

	/* add selection to selection node */
	sn->selection.item = fs;

	return sn;
bad:
	log_error(_sel_syntax_error_at_msg, s);
	log_error(_sel_help_ref_msg);
	*next = s;
	return NULL;
}

static struct selection_node *_parse_or_ex(struct dm_report *rh,
					   const char *s,
					   const char **next,
					   struct selection_node *or_sn);

static struct selection_node *_parse_ex(struct dm_report *rh,
					const char *s,
					const char **next)
{
	static const char _ps_expected_msg[] = "Syntax error: left parenthesis expected at \'%s\'";
	static const char _pe_expected_msg[] = "Syntax error: right parenthesis expected at \'%s\'";
	struct selection_node *sn = NULL;
	uint32_t t;
	const char *tmp;

	t = _tok_op_log(s, next, SEL_MODIFIER_NOT | SEL_PRECEDENCE_PS);
	if (t == SEL_MODIFIER_NOT) {
		/* '!' '(' EXPRESSION ')' */
		if (!_tok_op_log(*next, &tmp, SEL_PRECEDENCE_PS)) {
			log_error(_ps_expected_msg, *next);
			goto error;
		}
		if (!(sn = _parse_or_ex(rh, tmp, next, NULL)))
			goto error;
		sn->type |= SEL_MODIFIER_NOT;
		if (!_tok_op_log(*next, &tmp, SEL_PRECEDENCE_PE)) {
			log_error(_pe_expected_msg, *next);
			goto error;
		}
		*next = tmp;
	} else if (t == SEL_PRECEDENCE_PS) {
		/* '(' EXPRESSION ')' */
		if (!(sn = _parse_or_ex(rh, *next, &tmp, NULL)))
			goto error;
		if (!_tok_op_log(tmp, next, SEL_PRECEDENCE_PE)) {
			log_error(_pe_expected_msg, *next);
			goto error;
		}
	} else if ((s = _skip_space(s))) {
		/* SELECTION */
		sn = _parse_selection(rh, s, next);
	} else {
		sn = NULL;
		*next = s;
	}

	return sn;
error:
	*next = s;
	return NULL;
}

/* AND_EXPRESSION := EX (AND_OP AND_EXPRSSION) */
static struct selection_node *_parse_and_ex(struct dm_report *rh,
					    const char *s,
					    const char **next,
					    struct selection_node *and_sn)
{
	struct selection_node *n;
	const char *tmp;

	n = _parse_ex(rh, s, next);
	if (!n)
		goto error;

	if (!_tok_op_log(*next, &tmp, SEL_AND)) {
		if (!and_sn)
			return n;
		dm_list_add(&and_sn->selection.set, &n->list);
		return and_sn;
	}

	if (!and_sn) {
		if (!(and_sn = _alloc_selection_node(rh->selection->mem, SEL_AND)))
			goto error;
	}
	dm_list_add(&and_sn->selection.set, &n->list);

	return _parse_and_ex(rh, tmp, next, and_sn);
error:
	*next = s;
	return NULL;
}

/* OR_EXPRESSION := AND_EXPRESSION (OR_OP OR_EXPRESSION) */
static struct selection_node *_parse_or_ex(struct dm_report *rh,
					   const char *s,
					   const char **next,
					   struct selection_node *or_sn)
{
	struct selection_node *n;
	const char *tmp;

	n = _parse_and_ex(rh, s, next, NULL);
	if (!n)
		goto error;

	if (!_tok_op_log(*next, &tmp, SEL_OR)) {
		if (!or_sn)
			return n;
		dm_list_add(&or_sn->selection.set, &n->list);
		return or_sn;
	}

	if (!or_sn) {
		if (!(or_sn = _alloc_selection_node(rh->selection->mem, SEL_OR)))
			goto error;
	}
	dm_list_add(&or_sn->selection.set, &n->list);

	return _parse_or_ex(rh, tmp, next, or_sn);
error:
	*next = s;
	return NULL;
}

struct dm_report *dm_report_init_with_selection(uint32_t *report_types,
						const struct dm_report_object_type *types,
						const struct dm_report_field_type *fields,
						const char *output_fields,
						const char *output_separator,
						uint32_t output_flags,
						const char *sort_keys,
						const char *selection,
						const struct dm_report_reserved_value reserved_values[],
						void *private_data)
{
	struct dm_report *rh;
	struct selection_node *root = NULL;
	const char *fin, *next;

	_implicit_report_fields = _implicit_special_report_fields_with_selection;

	if (!(rh = dm_report_init(report_types, types, fields, output_fields,
			output_separator, output_flags, sort_keys, private_data)))
		return NULL;

	if (!selection || !selection[0]) {
		rh->selection = NULL;
		return rh;
	}

	if (!_check_reserved_values_supported(fields, reserved_values)) {
		log_error(INTERNAL_ERROR "dm_report_init_with_selection: "
			  "trying to register unsupported reserved value type, "
			  "skipping report selection");
		return rh;
	}
	rh->reserved_values = reserved_values;

	if (!strcasecmp(selection, SPECIAL_FIELD_HELP_ID) ||
	    !strcmp(selection, SPECIAL_FIELD_HELP_ALT_ID)) {
		_display_fields(rh, 0, 1);
		log_warn(" ");
		_display_selection_help(rh);
		rh->flags |= RH_ALREADY_REPORTED;
		return rh;
	}

	if (!(rh->selection = dm_pool_zalloc(rh->mem, sizeof(struct selection))) ||
	    !(rh->selection->mem = dm_pool_create("report selection", 10 * 1024))) {
		log_error("Failed to allocate report selection structure.");
		goto bad;
	}

	if (!(root = _alloc_selection_node(rh->selection->mem, SEL_OR)))
		goto_bad;

	if (!_parse_or_ex(rh, selection, &fin, root))
		goto_bad;

	next = _skip_space(fin);
	if (*next) {
		log_error("Expecting logical operator");
		log_error(_sel_syntax_error_at_msg, next);
		log_error(_sel_help_ref_msg);
		goto bad;
	}

	_dm_report_init_update_types(rh, report_types);

	rh->selection->selection_root = root;
	return rh;
bad:
	dm_report_free(rh);
	return NULL;
}

/*
 * Print row of headings
 */
static int _report_headings(struct dm_report *rh)
{
	const struct dm_report_field_type *fields;
	struct field_properties *fp;
	const char *heading;
	char *buf = NULL;
	size_t buf_size = 0;

	rh->flags |= RH_HEADINGS_PRINTED;

	if (!(rh->flags & DM_REPORT_OUTPUT_HEADINGS))
		return 1;

	if (!dm_pool_begin_object(rh->mem, 128)) {
		log_error("dm_report: "
			  "dm_pool_begin_object failed for headings");
		return 0;
	}

	dm_list_iterate_items(fp, &rh->field_props) {
		if ((int) buf_size < fp->width)
			buf_size = (size_t) fp->width;
	}
	/* Including trailing '\0'! */
	buf_size++;

	if (!(buf = dm_malloc(buf_size))) {
		log_error("dm_report: Could not allocate memory for heading buffer.");
		goto bad;
	}

	/* First heading line */
	dm_list_iterate_items(fp, &rh->field_props) {
		if (fp->flags & FLD_HIDDEN)
			continue;

		fields = fp->implicit ? _implicit_report_fields : rh->fields;

		heading = fields[fp->field_num].heading;
		if (rh->flags & DM_REPORT_OUTPUT_ALIGNED) {
			if (dm_snprintf(buf, buf_size, "%-*.*s",
					 fp->width, fp->width, heading) < 0) {
				log_error("dm_report: snprintf heading failed");
				goto bad;
			}
			if (!dm_pool_grow_object(rh->mem, buf, fp->width)) {
				log_error("dm_report: Failed to generate report headings for printing");
				goto bad;
			}
		} else if (!dm_pool_grow_object(rh->mem, heading, 0)) {
			log_error("dm_report: Failed to generate report headings for printing");
			goto bad;
		}

		if (!dm_list_end(&rh->field_props, &fp->list))
			if (!dm_pool_grow_object(rh->mem, rh->separator, 0)) {
				log_error("dm_report: Failed to generate report headings for printing");
				goto bad;
			}
	}
	if (!dm_pool_grow_object(rh->mem, "\0", 1)) {
		log_error("dm_report: Failed to generate report headings for printing");
		goto bad;
	}

	/* print all headings */
	heading = (char *) dm_pool_end_object(rh->mem);
	log_print("%s", heading);

	dm_pool_free(rh->mem, (void *)heading);
	dm_free(buf);

	return 1;

      bad:
	dm_free(buf);
	dm_pool_abandon_object(rh->mem);
	return 0;
}

int dm_report_column_headings(struct dm_report *rh)
{
	/* Columns-as-rows does not use _report_headings. */
	if (rh->flags & DM_REPORT_OUTPUT_COLUMNS_AS_ROWS)
		return 1;
	return _report_headings(rh);
}

/*
 * Sort rows of data
 */
static int _row_compare(const void *a, const void *b)
{
	const struct row *rowa = *(const struct row * const *) a;
	const struct row *rowb = *(const struct row * const *) b;
	const struct dm_report_field *sfa, *sfb;
	uint32_t cnt;

	for (cnt = 0; cnt < rowa->rh->keys_count; cnt++) {
		sfa = (*rowa->sort_fields)[cnt];
		sfb = (*rowb->sort_fields)[cnt];
		if ((sfa->props->flags & DM_REPORT_FIELD_TYPE_NUMBER) ||
		    (sfa->props->flags & DM_REPORT_FIELD_TYPE_SIZE) ||
		    (sfa->props->flags & DM_REPORT_FIELD_TYPE_TIME)) {
			const uint64_t numa =
			    *(const uint64_t *) sfa->sort_value;
			const uint64_t numb =
			    *(const uint64_t *) sfb->sort_value;

			if (numa == numb)
				continue;

			if (sfa->props->flags & FLD_ASCENDING) {
				return (numa > numb) ? 1 : -1;
			} else {	/* FLD_DESCENDING */
				return (numa < numb) ? 1 : -1;
			}
		} else {
			/* DM_REPORT_FIELD_TYPE_STRING
			 * DM_REPORT_FIELD_TYPE_STRING_LIST */
			const char *stra = (const char *) sfa->sort_value;
			const char *strb = (const char *) sfb->sort_value;
			int cmp = strcmp(stra, strb);

			if (!cmp)
				continue;

			if (sfa->props->flags & FLD_ASCENDING) {
				return (cmp > 0) ? 1 : -1;
			} else {	/* FLD_DESCENDING */
				return (cmp < 0) ? 1 : -1;
			}
		}
	}

	return 0;		/* Identical */
}

static int _sort_rows(struct dm_report *rh)
{
	struct row *(*rows)[];
	uint32_t count = 0;
	struct row *row;

	if (!(rows = dm_pool_alloc(rh->mem, sizeof(**rows) *
				dm_list_size(&rh->rows)))) {
		log_error("dm_report: sort array allocation failed");
		return 0;
	}

	dm_list_iterate_items(row, &rh->rows)
		(*rows)[count++] = row;

	qsort(rows, count, sizeof(**rows), _row_compare);

	dm_list_init(&rh->rows);
	while (count--)
		dm_list_add_h(&rh->rows, &(*rows)[count]->list);

	return 1;
}

/*
 * Produce report output
 */
static int _output_field(struct dm_report *rh, struct dm_report_field *field)
{
	const struct dm_report_field_type *fields = field->props->implicit ? _implicit_report_fields
									   : rh->fields;
	char *field_id;
	int32_t width;
	uint32_t align;
	const char *repstr;
	char *buf = NULL;
	size_t buf_size = 0;

	if (rh->flags & DM_REPORT_OUTPUT_FIELD_NAME_PREFIX) {
		if (!(field_id = dm_strdup(fields[field->props->field_num].id))) {
			log_error("dm_report: Failed to copy field name");
			return 0;
		}

		if (!dm_pool_grow_object(rh->mem, rh->output_field_name_prefix, 0)) {
			log_error("dm_report: Unable to extend output line");
			dm_free(field_id);
			return 0;
		}

		if (!dm_pool_grow_object(rh->mem, _toupperstr(field_id), 0)) {
			log_error("dm_report: Unable to extend output line");
			dm_free(field_id);
			return 0;
		}

		dm_free(field_id);

		if (!dm_pool_grow_object(rh->mem, "=", 1)) {
			log_error("dm_report: Unable to extend output line");
			return 0;
		}

		if (!(rh->flags & DM_REPORT_OUTPUT_FIELD_UNQUOTED) &&
		    !dm_pool_grow_object(rh->mem, "\'", 1)) {
			log_error("dm_report: Unable to extend output line");
			return 0;
		}
	}

	repstr = field->report_string;
	width = field->props->width;
	if (!(rh->flags & DM_REPORT_OUTPUT_ALIGNED)) {
		if (!dm_pool_grow_object(rh->mem, repstr, 0)) {
			log_error("dm_report: Unable to extend output line");
			return 0;
		}
	} else {
		if (!(align = field->props->flags & DM_REPORT_FIELD_ALIGN_MASK))
			align = ((field->props->flags & DM_REPORT_FIELD_TYPE_NUMBER) ||
				 (field->props->flags & DM_REPORT_FIELD_TYPE_SIZE)) ? 
				DM_REPORT_FIELD_ALIGN_RIGHT : DM_REPORT_FIELD_ALIGN_LEFT;

		/* Including trailing '\0'! */
		buf_size = width + 1;
		if (!(buf = dm_malloc(buf_size))) {
			log_error("dm_report: Could not allocate memory for output line buffer.");
			return 0;
		}

		if (align & DM_REPORT_FIELD_ALIGN_LEFT) {
			if (dm_snprintf(buf, buf_size, "%-*.*s",
					 width, width, repstr) < 0) {
				log_error("dm_report: left-aligned snprintf() failed");
				goto bad;
			}
			if (!dm_pool_grow_object(rh->mem, buf, width)) {
				log_error("dm_report: Unable to extend output line");
				goto bad;
			}
		} else if (align & DM_REPORT_FIELD_ALIGN_RIGHT) {
			if (dm_snprintf(buf, buf_size, "%*.*s",
					 width, width, repstr) < 0) {
				log_error("dm_report: right-aligned snprintf() failed");
				goto bad;
			}
			if (!dm_pool_grow_object(rh->mem, buf, width)) {
				log_error("dm_report: Unable to extend output line");
				goto bad;
			}
		}
	}

	if ((rh->flags & DM_REPORT_OUTPUT_FIELD_NAME_PREFIX) &&
	    !(rh->flags & DM_REPORT_OUTPUT_FIELD_UNQUOTED))
		if (!dm_pool_grow_object(rh->mem, "\'", 1)) {
			log_error("dm_report: Unable to extend output line");
			goto bad;
		}

	dm_free(buf);
	return 1;

bad:
	dm_free(buf);
	return 0;
}

static void _reset_field_props(struct dm_report *rh)
{
	struct field_properties *fp;
	dm_list_iterate_items(fp, &rh->field_props)
		fp->width = fp->initial_width;
}

static void _destroy_rows(struct dm_report *rh)
{
	/*
	 * free the first row allocated to this report: since this is a
	 * pool allocation this will also free all subsequently allocated
	 * rows from the report and any associated string data.
	 */
	if (rh->first_row)
		dm_pool_free(rh->mem, rh->first_row);
	rh->first_row = NULL;
	dm_list_init(&rh->rows);

	/* Reset field widths to original values. */
	_reset_field_props(rh);
}

static int _output_as_rows(struct dm_report *rh)
{
	const struct dm_report_field_type *fields;
	struct field_properties *fp;
	struct dm_report_field *field;
	struct row *row;

	dm_list_iterate_items(fp, &rh->field_props) {
		if (fp->flags & FLD_HIDDEN) {
			dm_list_iterate_items(row, &rh->rows) {
				field = dm_list_item(dm_list_first(&row->fields), struct dm_report_field);
				dm_list_del(&field->list);
			}
			continue;
		}

		fields = fp->implicit ? _implicit_report_fields : rh->fields;

		if (!dm_pool_begin_object(rh->mem, 512)) {
			log_error("dm_report: Unable to allocate output line");
			return 0;
		}

		if ((rh->flags & DM_REPORT_OUTPUT_HEADINGS)) {
			if (!dm_pool_grow_object(rh->mem, fields[fp->field_num].heading, 0)) {
				log_error("dm_report: Failed to extend row for field name");
				goto bad;
			}
			if (!dm_pool_grow_object(rh->mem, rh->separator, 0)) {
				log_error("dm_report: Failed to extend row with separator");
				goto bad;
			}
		}

		dm_list_iterate_items(row, &rh->rows) {
			if ((field = dm_list_item(dm_list_first(&row->fields), struct dm_report_field))) {
				if (!_output_field(rh, field))
					goto bad;
				dm_list_del(&field->list);
			}

			if (!dm_list_end(&rh->rows, &row->list))
				if (!dm_pool_grow_object(rh->mem, rh->separator, 0)) {
					log_error("dm_report: Unable to extend output line");
					goto bad;
				}
		}

		if (!dm_pool_grow_object(rh->mem, "\0", 1)) {
			log_error("dm_report: Failed to terminate row");
			goto bad;
		}
		log_print("%s", (char *) dm_pool_end_object(rh->mem));
	}

	_destroy_rows(rh);

	return 1;

      bad:
	dm_pool_abandon_object(rh->mem);
	return 0;
}

static int _output_as_columns(struct dm_report *rh)
{
	struct dm_list *fh, *rowh, *ftmp, *rtmp;
	struct row *row = NULL;
	struct dm_report_field *field;

	/* If headings not printed yet, calculate field widths and print them */
	if (!(rh->flags & RH_HEADINGS_PRINTED))
		_report_headings(rh);

	/* Print and clear buffer */
	dm_list_iterate_safe(rowh, rtmp, &rh->rows) {
		if (!dm_pool_begin_object(rh->mem, 512)) {
			log_error("dm_report: Unable to allocate output line");
			return 0;
		}
		row = dm_list_item(rowh, struct row);
		dm_list_iterate_safe(fh, ftmp, &row->fields) {
			field = dm_list_item(fh, struct dm_report_field);
			if (field->props->flags & FLD_HIDDEN)
				continue;

			if (!_output_field(rh, field))
				goto bad;

			if (!dm_list_end(&row->fields, fh))
				if (!dm_pool_grow_object(rh->mem, rh->separator, 0)) {
					log_error("dm_report: Unable to extend output line");
					goto bad;
				}

			dm_list_del(&field->list);
		}
		if (!dm_pool_grow_object(rh->mem, "\0", 1)) {
			log_error("dm_report: Unable to terminate output line");
			goto bad;
		}
		log_print("%s", (char *) dm_pool_end_object(rh->mem));
		dm_list_del(&row->list);
	}

	_destroy_rows(rh);

	return 1;

      bad:
	dm_pool_abandon_object(rh->mem);
	return 0;
}

int dm_report_is_empty(struct dm_report *rh)
{
	return dm_list_empty(&rh->rows) ? 1 : 0;
}

int dm_report_output(struct dm_report *rh)
{
	if (dm_list_empty(&rh->rows))
		return 1;

	if ((rh->flags & RH_SORT_REQUIRED))
		_sort_rows(rh);

	if ((rh->flags & DM_REPORT_OUTPUT_COLUMNS_AS_ROWS))
		return _output_as_rows(rh);
	else
		return _output_as_columns(rh);
}