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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Module: sml.c
* Synopsis: simplified markup language (SML) support
* Taxonomy: project private
* Debug flag: sml
* Description:
*
* This module implements methods that support the processing of a
* simplified markup language (SML). Objects that contain SML data
* can be created and manipulated, and SML can be imported into
* internal SML objects or exported from internal SML objects.
*
* Public Methods:
*
* smlAddTag - Add new tag object into existing tag object
* smlConvertStringToTag - Convert string into tag object
* smlConvertTagToString - Convert a tag object into a string
* representation of the XML
* smlDbgPrintTag - Print a representation of an XML tag if debugging
* smlDelParam - Delete a parameter from a tag object
* smlDelTag - Delete element from tag object
* smlDup - Duplicate a tag object
* smlFindAndDelTag - Delete a tag if found in tag object
* smlFreeTag - Free a tag object and all its contents when no
* longer needed
* smlFstatCompareEq - Compare file status information
* smlGetElementName - Return a tag's element name
* smlGetNumParams - Get number of parameters set in tag
* smlGetParam - Get a parameter from a tag
* smlGetParamF - Get a formatted parameter from a tag
* smlGetParamByTag - Get a parameter by tag and index
* smlGetParamByTagParam Get parameter given tag name, index,
* parameter name, and value
* smlGetParamName - Get the name of a tag parameter given its index
* smlGetParam_r - Get a parameter from a tag into fixed buffer
* smlGetTag - Get an element from a tag
* smlGetTagByName - Get an element given a name and an index
* smlGetTagByTagParam - Get element given tag name, index, parameter name,
* and value
* smlGetVerbose - get current verbose mode setting
* smlLoadTagFromFile - Load a file into a tag object
* smlNewTag - Create a new (empty) tag object
* smlParamEq - Determine if parameter is equal to a specified value
* smlParamEqF - Determine if parameter is equal to a specified value
* smlPrintTag - Print a simple XML representation of a tag to stderr
* smlReadOneTag - read one complete tag from a datastream
* smlReadTagFromDs - read tag object from datastream
* smlSetFileStatInfo - encode file status information into tag
* smlSetVerbose - set/clear verbose mode for debugging output
* smlSetParam - Set parameter value in tag object
* smlSetParamF - Set parameter value in tag object
* smlWriteTagToDs - Write an XML representation of a tag to a datastream
* smlWriteTagToFd - Write an XML representation of a tag to an open file
* descriptor
* smlWriteTagToFile - Write an XML representation of a tag to a file
*/
/*
* Unix includes
*/
#include <locale.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <libintl.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <sys/statvfs.h>
#include <errno.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#include <strings.h>
/*
* liblu Includes
*/
#include "libinst.h"
#include "messages.h"
/* Should be defined by cc -D */
#if !defined(TEXT_DOMAIN)
#define TEXT_DOMAIN "SYS_TEST"
#endif
/*
* Private Method Forward Declarations
*/
/*PRINTFLIKE2*/
static void _smlLogMsg(LogMsgType a_type, const char *a_format, ...);
static int _smlReadTag(SML_TAG **r_tag, char **a_str, char *parent);
static int _smlWriteSimpleTag(char **a_str,
SML_TAG *tag);
static int _smlWriteParamValue(char **a_str, char *value);
static void _smlFreeTag(SML_TAG *tag);
static char *_sml_fileStatInfoTag = "File-Stat-Info";
static boolean_t verbose = B_FALSE;
/*
*
* This definition controls the maximum size of any individual sml
* component, such as a tag name, tag *value*, etc. The code should
* someday be revised to dynamically allocate whatever memory is needed
* to hold such components while parsing, but that exercise is left for
* another day. Any component that exceeds this length is silently
* truncated...
*/
#define MAX_SML_COMPONENT_LENGTH 16384
/*
* Public Methods
*/
/*
* Name: smlAddTag
* Description: Add new tag object into existing tag object
* Arguments: r_tag - [RO, *RW] - (SML_TAG **)
* Pointer to handle to the tag object to update
* The handle may be updated if the tag object is
* moved in memory
* a_index - [RO] - (int)
* Add the tag after the "n"th tag in the tag object
* -1 == add the tag to the end of the tag object
* 0 == add the tag to the beginning of the tag object
* a_subTag - [RO, *RW] - (SML_TAG *)
* The tag to add to 'tag'
* Returns: SML_TAG *
* The location within "r_tag" where "a_subTag"
* has been added - this is the handle into the r_tag
* object to the tag that was just added
* Errors: If the tag object cannot be updated, the process exits
*/
SML_TAG *
smlAddTag(SML_TAG **r_tag, int a_index, SML_TAG *a_subTag)
{
SML_TAG *tag;
/* entry assertions */
assert(SML_TAG__ISVALID(a_subTag));
assert(SML_TAG__R_ISVALID(r_tag));
/* if no tag to update specified, ignore request */
tag = *r_tag;
if (tag == SML_TAG__NULL) {
return (tag);
}
/* entry debugging info */
_smlLogMsg(LOG_MSG_DEBUG, DBG_SML_ADD_TAG,
a_subTag->name, tag->name);
/* if index is out of range or -1, append to tag object */
if ((a_index > tag->tags_num) || (a_index == -1)) {
a_index = tag->tags_num;
}
/* bump number of tags in tag object */
tag->tags_num++;
/* expand tag object to hold new subtag */
tag->tags = (SML_TAG *)realloc(tag->tags,
sizeof (SML_TAG) * tag->tags_num);
/* if not appending, adjust tag object to hold new subtag */
if (a_index < (tag->tags_num - 1)) {
(void) memmove(&(tag->tags[a_index + 1]), &(tag->tags[a_index]),
sizeof (SML_TAG) * (tag->tags_num - a_index - 1));
}
/* copy new subtag into correct location in tag object */
(void) memcpy(&(tag->tags[a_index]), a_subTag,
sizeof (SML_TAG));
return (&(tag->tags[a_index]));
}
/*
* Name: smlDelTag
* Description: Delete element from tag object
* Arguments: tag - [RO, *RW] - (SML_TAG *)
* The tag object to update
* sub_tag - [RO, *RW] - (SML_TAG *)
* Element to be removed from the tag object
* Returns: void
* The sub_tag is removed from the tag object
* NOTE: The sub-tag and all elements contained within it are deallocated
* the sub-tag is no longer valid when this method returns
*/
void
smlDelTag(SML_TAG *tag, SML_TAG *sub_tag)
{
int index;
/* entry assertions */
assert(SML_TAG__ISVALID(sub_tag));
/* if no tag to update specified, ignore request */
if (tag == SML_TAG__NULL) {
return;
}
/* entry debugging info */
_smlLogMsg(LOG_MSG_DEBUG, DBG_SML_DEL_TAG,
sub_tag->name, tag->name);
/* if tag object is empty, ignore request */
if (tag->tags_num == 0) {
return;
}
/* determine index into tag object of element to remove */
for (index = 0; index < tag->tags_num; index++) {
if (sub_tag == &tag->tags[index]) {
break;
}
}
/* if element not found in tag, ignore request */
if (index >= tag->tags_num) {
return;
}
/* free up the subtag to be deleted */
_smlFreeTag(sub_tag);
/*
* if not removing last element, collapse tag object removing
* target element
*/
if (index < (tag->tags_num - 1)) {
(void) memmove(&(tag->tags[index]), &(tag->tags[index + 1]),
sizeof (SML_TAG) *(tag->tags_num - index - 1));
}
/* one less tag object in tag */
tag->tags_num --;
/*
* If only one tag left, then delete entire tag structure
* otherwise reallocate removing unneeded entry
*/
if (tag->tags_num > 0) {
/* realloc removing last element in tag object */
tag->tags = (SML_TAG *)realloc(tag->tags,
sizeof (SML_TAG) *tag->tags_num);
} else {
tag->tags = SML_TAG__NULL;
}
}
/*
* Name: smlFreeTag
* Description: Free a tag object and all its contents when no longer needed
* Arguments: tag - [RO, *RW] - (SML_TAG *)
* The tag object to be deleted
* Returns: void
* The tag object and all its contents are deallocated
*/
void
smlFreeTag(SML_TAG *tag)
{
/* entry assertions */
assert(SML_TAG__ISVALID(tag));
/* entry debugging info */
if (tag->name != (char *)NULL) {
_smlLogMsg(LOG_MSG_DEBUG, DBG_SML_FREE_TAG,
(unsigned long)tag, tag->name);
}
/* free the tag object contents */
_smlFreeTag(tag);
/* free the tag object handle */
bzero(tag, sizeof (SML_TAG));
free(tag);
}
/*
* Name: smlGetNumParams
* Synopsis: Get number of parameters set in tag
* Description: Return the number of parameters set in a tag
* Arguments: a_tag - [RO, *RO] - (SML_TAG *)
* The tag object to obtain the # params from
* Returns: int
* Number of parameters set in tag
* 0 = no parameters are set
*/
int
smlGetNumParams(SML_TAG *a_tag)
{
return (a_tag ? a_tag->params_num : 0);
}
/*
* Name: smlGetParam_r
* Description: Get a parameter from a tag into a buffer of fixed size
* Arguments: tag - [RO, *RO] - (SML_TAG *)
* The tag object to obtain the parameter from
* name - [RO, *RO] - (char *)
* Name of the parameter to retrieve
* buf - [RO, *RW] - (char *)
* Location of buffer to contain results
* bufLen - [RO, *RO] - (int)
* Maximum bytes available in buffer to contain results
* Returns: void
*/
void
smlGetParam_r(SML_TAG *tag, char *name, char *buf, int bufLen)
{
int k;
/* entry assertions */
assert(name != (char *)NULL);
assert(*name != '\0');
assert(buf != (char *)NULL);
assert(bufLen > 0);
/* terminate the buffer */
buf[0] = '\0';
buf[bufLen-1] = '\0';
bzero(buf, bufLen);
/* if no tag specified, return NULL */
if (tag == SML_TAG__NULL) {
return;
}
/* if no parameters in tag, return NULL */
if (tag->params == NULL) {
return;
}
/* entry debugging info */
_smlLogMsg(LOG_MSG_DEBUG, DBG_SML_GET_PARAM,
name, tag->name);
/* scan tag object looking for specified parameter */
for (k = 0; k < tag->params_num; k++) {
assert(tag->params[k].name != (char *)NULL);
assert(tag->params[k].value != (char *)NULL);
if (streq(tag->params[k].name, name)) {
_smlLogMsg(LOG_MSG_DEBUG,
DBG_SML_GOT_PARAM,
tag->name, name, tag->params[k].value);
(void) strncpy(buf, tag->params[k].value, bufLen-1);
return;
}
}
/* parameter not found - return */
}
/*
* Name: smlGetParam
* Description: Get a parameter from a tag
* Arguments: tag - [RO, *RO] - (SML_TAG *)
* The tag object to obtain the parameter from
* name - [RO, *RO] - (char *)
* Name of the parameter to retrieve
* Returns: char *
* Value of the specified parameter
* == (char *)NULL if the parameter does not exist
* NOTE: Any parameter returned is placed in new storage for the
* calling method. The caller must use 'free' to dispose
* of the storage once the parameter is no longer needed.
*/
char *
smlGetParam(SML_TAG *tag, char *name)
{
int k;
/* entry assertions */
assert(name != (char *)NULL);
assert(*name != '\0');
/* entry debugging info */
_smlLogMsg(LOG_MSG_DEBUG, "get param param <%s>", name);
/* if no tag specified, return NULL */
if (tag == SML_TAG__NULL) {
return ((char *)NULL);
}
/* if no parameters in tag, return NULL */
if (tag->params == NULL) {
return ((char *)NULL);
}
/* entry debugging info */
_smlLogMsg(LOG_MSG_DEBUG, DBG_SML_GET_PARAM,
name, tag->name);
/* scan tag object looking for specified parameter */
for (k = 0; k < tag->params_num; k++) {
assert(tag->params[k].name != (char *)NULL);
assert(tag->params[k].value != (char *)NULL);
if (streq(tag->params[k].name, name)) {
_smlLogMsg(LOG_MSG_DEBUG,
DBG_SML_GOT_PARAM,
tag->name, name, tag->params[k].value);
return (strdup(tag->params[k].value));
}
}
/* parameter not found - return NULL */
return ((char *)NULL);
}
/*
* Name: smlGetParamName
* Description: Get the name of a tag parameter given its index
* Arguments: tag - [RO, *RO] - (SML_TAG *)
* The tag object to obtain the parameter name from
* index - [RO] - (int)
* Index of parameter name to return
* Returns: char *
* Name of 'index'th parameter
* == (char *)NULL if no such parameter exists in tag
* NOTE: Any parameter name returned is placed in new storage for the
* calling method. The caller must use 'free' to dispose
* of the storage once the parameter name is no longer needed.
*/
char *
smlGetParamName(SML_TAG *tag, int index)
{
/* if no tag specified, return NULL */
if (tag == NULL) {
return ((char *)NULL);
}
/* entry debugging info */
_smlLogMsg(LOG_MSG_DEBUG, DBG_SML_GET_PARAM_NAME,
tag->name, index);
/* if no parameters in tag, return NULL */
if (tag->params == NULL) {
return ((char *)NULL);
}
/* if index not within range, return NULL */
if (index >= tag->params_num) {
return ((char *)NULL);
}
/* index within range - return parameter name */
assert(tag->params[index].name != (char *)NULL);
assert(tag->params[index].value != (char *)NULL);
_smlLogMsg(LOG_MSG_DEBUG, DBG_SML_GOT_PARAM_NAME,
tag->name, index, tag->params[index].name);
return (strdup(tag->params[index].name));
}
/*
* Name: smlGetParamByTag
* Synopsis: Get a parameter value from a tag by name and index
* Description: Call to look for a parameter value from a tag with
* a given name with a parameter of a given name
* Arguments: tag - [RO, *RO] - (SML_TAG *)
* The tag object to obtain the parameter
* index - [RO] - (int)
* Index of nth tag by name to look for
* tagName - [RO, *RO] - (char *)
* Name of tag to look for
* paramName - [RO, *RO] - (char *)
* Name of parameter to return value of
* Returns: char *
* == (char *)NULL - no parameter value set
* != (char *)NULL - value of parameter set
*/
char *
smlGetParamByTag(SML_TAG *tag, int index,
char *tagName, char *paramName)
{
SML_TAG *rtag;
/* entry assertions */
assert(SML_TAG__ISVALID(tag));
assert(tagName != (char *)NULL);
assert(*tagName != '\0');
assert(paramName != (char *)NULL);
assert(*paramName != '\0');
/* entry debugging info */
_smlLogMsg(LOG_MSG_DEBUG, DBG_SML_GET_PARAM_BY_TAG,
tagName, index, paramName);
/* find the requested tag by name and index */
rtag = smlGetTagByName(tag, index, tagName);
if (rtag == SML_TAG__NULL) {
return ((char *)NULL);
}
return (smlGetParam(rtag, paramName));
}
/*
* Name: smlGetTagByTagParam
* Synopsis: Get element given tag name, index, parameter name, and value
* Description: Call to look for a tag with a given nae, that has a parameter
* of a given name with a specified value
* Arguments: tag - [RO, *RO] - (SML_TAG *)
* The tag object to obtain the element from
* index - [RO] - (int)
* Index of nth name to return
* tagName - [RO, *RO] - (char *)
* Tag name to look up
* paramName - [RO, *RO] - (char *)
* Parameter name to look up
* paramValue - [RO, *RO] - (char *)
* Parameter value to match
* Returns: SML_TAG *
* The 'index'th occurance of element 'name' with
* a parameter 'name' with value specified
* == SML_TAG__NULL if no such element exists
*/
SML_TAG *
smlGetTagByTagParam(SML_TAG *tag, int index,
char *tagName, char *paramName, char *paramValue)
{
int ti; /* tag structure index */
/* entry assertions */
assert(SML_TAG__ISVALID(tag));
assert(tagName != (char *)NULL);
assert(*tagName != '\0');
assert(paramName != (char *)NULL);
assert(*paramName != '\0');
assert(paramValue != (char *)NULL);
assert(*paramValue != '\0');
/* if tag has no elements, return NULL */
if (tag->tags == NULL) {
return (SML_TAG__NULL);
}
/*
* Search algorithm:
* -> search tag structure; for each tag with element == "tagName":
* -> search tag parameters; if parameter name == "paramName"
* -> if parameter value != "paramValue"; to next tag
* -> if parameter value == "paramValue":
* -> if not the "index"th paramValue found; to next tag
* -> return tag found
*/
for (ti = 0; ti < tag->tags_num; ti++) {
int pi; /* parameter structure index */
/* if tag element does not match, go on to next tag */
if (strcmp(tag->tags[ti].name, tagName)) {
continue;
}
/* element matches: search for specified parameter name/value */
for (pi = 0; pi < tag->tags[ti].params_num; pi++) {
assert(tag->tags[ti].params[pi].name != (char *)NULL);
assert(tag->tags[ti].params[pi].value != (char *)NULL);
/* if parameter name doesnt match to next parameter */
if (strcmp(tag->tags[ti].params[pi].name, paramName)) {
continue;
}
/* if parameter value doesnt match to next tag */
if (strcmp(tag->tags[ti].params[pi].value,
paramValue)) {
break;
}
/*
* found element/paramname/paramvalue:
* -> if this is not the 'index'th one, go to next tag
*/
if (index-- != 0) {
break;
}
/*
* found specified element/paramname/paramvalue:
* -> return the tag found
*/
return (&tag->tags[ti]);
}
}
/* no such element found - return NULL */
return (SML_TAG__NULL);
}
/*
* Name: smlGetParamByTagParam
* Synopsis: Get parameter given tag name, index, parameter name, and value
* Description: Call to return the value of a parameter from a tag of a
* given name, with a parameter of a given name with a
* specified value
* Arguments: tag - [RO, *RO] - (SML_TAG *)
* The tag object to obtain the element from
* index - [RO] - (int)
* Index of nth name to return
* tagName - [RO, *RO] - (char *)
* Tag name to look up
* paramName - [RO, *RO] - (char *)
* Parameter name to look up
* paramValue - [RO, *RO] - (char *)
* Parameter value to match
* paramReturn - [RO, *RO] - (char *)
* Parameter name to return the value of
* Returns: char *
* The value of parameter 'paramReturn' from the
* The 'index'th occurance of element 'name' with
* a parameter 'name' with value specified
* == (char *)NULL if no such parameter exists
*/
char *
smlGetParamByTagParam(SML_TAG *tag, int index,
char *tagName, char *paramName, char *paramValue, char *paramReturn)
{
int ti; /* tag structure index */
/* entry assertions */
assert(SML_TAG__ISVALID(tag));
assert(tagName != (char *)NULL);
assert(*tagName != '\0');
assert(paramName != (char *)NULL);
assert(*paramName != '\0');
assert(paramValue != (char *)NULL);
assert(*paramValue != '\0');
assert(paramReturn != (char *)NULL);
assert(*paramReturn != '\0');
/* if tag has no elements, return NULL */
if (tag->tags == NULL) {
return ((char *)NULL);
}
/*
* Search algorithm:
* -> search tag structure; for each tag with element == "tagName":
* -> search tag parameters; if parameter name == "paramName"
* -> if parameter value != "paramValue"; to next tag
* -> if parameter value == "paramValue":
* -> if not the "index"th paramValue found; to next tag
* -> return value of "paramReturn"
*/
for (ti = 0; ti < tag->tags_num; ti++) {
int pi; /* parameter structure index */
/* if tag element does not match, go on to next tag */
if (strcmp(tag->tags[ti].name, tagName)) {
continue;
}
/* element matches: search for specified parameter name/value */
for (pi = 0; pi < tag->tags[ti].params_num; pi++) {
assert(tag->tags[ti].params[pi].name != (char *)NULL);
assert(tag->tags[ti].params[pi].value != (char *)NULL);
/* if parameter name doesnt match to next parameter */
if (strcmp(tag->tags[ti].params[pi].name, paramName)) {
continue;
}
/* if parameter value doesnt match to next tag */
if (strcmp(tag->tags[ti].params[pi].value,
paramValue)) {
break;
}
/*
* found element/paramname/paramvalue:
* -> if this is not the 'index'th one, go to next tag
*/
if (index-- != 0) {
break;
}
/*
* found specified element/paramname/paramvalue:
* -> return parameter requested
*/
return (smlGetParam(&tag->tags[ti], paramReturn));
}
}
/* no such element found - return NULL */
return ((char *)NULL);
}
/*
* Name: smlGetElementName
* Description: Return the name of a given tag
* Arguments: a_tag - [RO, *RO] - (SML_TAG *)
* The tag object to obtain the element name from
* Returns: char *
* Value of name of specified tag
* NOTE: Any name string returned is placed in new storage for the
* calling method. The caller must use 'free' to dispose
* of the storage once the name string is no longer needed.
*/
char *
smlGetElementName(SML_TAG *a_tag)
{
/* entry assertions */
assert(SML_TAG__ISVALID(a_tag));
assert(a_tag->name != (char *)NULL);
assert(*a_tag->name != '\0');
/* return the tag name */
return (strdup(a_tag->name));
}
/*
* Name: smlGetTag
* Description: Get an element from a tag
* Arguments: tag - [RO, *RO] - (SML_TAG *)
* The tag object to obtain the element from
* index - [RO] - (int)
* Index of element to return
* Returns: SML_TAG *
* The 'index'th element from the specified tag
* == SML_TAG__NULL if no such tag or element
*/
SML_TAG *
smlGetTag(SML_TAG *tag, int index)
{
/* if no tag specified, return NULL */
if (tag == NULL) {
return (SML_TAG__NULL);
}
/* if tag has no elements, return NULL */
if (tag->tags == NULL) {
return (SML_TAG__NULL);
}
/* if index not within range, return NULL */
if (tag->tags_num <= index) {
return (SML_TAG__NULL);
}
/* index within range, return element specified */
assert(SML_TAG__ISVALID(&tag->tags[index]));
return (&tag->tags[index]);
}
/*
* Name: smlGetTagByName
* Description: Get an element given a name and an index
* Arguments: tag - [RO, *RO] - (SML_TAG *)
* The tag object to obtain the element from
* index - [RO] - (int)
* Index of nth name to return
* name - [RO, *RO] - (char *)
* Tag name to look up
* Returns: SML_TAG *
* The 'index'th occurance of element 'name'
* == SML_TAG__NULL if no such element exists
*/
SML_TAG *
smlGetTagByName(SML_TAG *tag, int index, char *name)
{
int k;
_smlLogMsg(LOG_MSG_DEBUG, DBG_SML_GET_TAG_BY_NAME, name, index);
/* if no tag specified, return NULL */
if (tag == NULL) {
return (SML_TAG__NULL);
}
/* if this tag is the one mentioned, return it */
if (streq(tag->name, name) && (index == 0)) {
return (tag);
}
/* if tag has no elements, return NULL */
if (tag->tags == NULL) {
return (SML_TAG__NULL);
}
/* if index out of range, return NULL */
if (tag->tags_num <= index) {
return (SML_TAG__NULL);
}
/* index within range - search for specified element */
for (k = 0; k < tag->tags_num; k++) {
if (streq(tag->tags[k].name, name)) {
if (index == 0) {
assert(SML_TAG__ISVALID(&tag->tags[k]));
return (&tag->tags[k]);
} else {
index--;
}
}
}
/* no such element found - return NULL */
return (SML_TAG__NULL);
}
/*
* Name: smlConvertStringToTag
* Description: Convert string into tag object
* Arguments: err - [RO, *RW] (LU_ERR)
* Error object - used to contain any errors encountered
* and return those errors to this methods caller
* r_tag - [RW, *RW] - (SML_TAG **)
* Pointer to handle to place new tag object
* str - [RO, *RO] - (char *)
* String object to convert to tag object
* Returns: int
* RESULT_OK - string converted to tag object
* RESULT_ERR - problem converting string to tag object
* NOTE: Any tag object returned is placed in new storage for the
* calling method. The caller must use 'smlFreeTag' to dispose
* of the storage once the tag object name is no longer needed.
*/
int
smlConvertStringToTag(SML_TAG **r_tag, char *str)
{
int r;
SML_TAG *tag = SML_TAG__NULL;
SML_TAG *tmp_tag;
/* entry assertions */
assert(SML_TAG__R_ISVALID(r_tag));
assert(str != (char *)NULL);
assert(*str != '\0');
tag = smlNewTag("tagfile");
for (;;) {
r = _smlReadTag(&tmp_tag, &str, NULL);
if (r != RESULT_OK) {
smlFreeTag(tag);
return (r);
}
if (tmp_tag == SML_TAG__NULL) {
if (*str != '\0') {
continue;
}
_smlLogMsg(LOG_MSG_DEBUG,
DBG_SML_LOADED_TAGS_FROM_STR,
(unsigned long)tag, tag->name);
*r_tag = tag;
return (RESULT_OK);
}
_smlLogMsg(LOG_MSG_DEBUG, DBG_SML_READ_IN_TOP_TAG,
tmp_tag->name);
tag->tags_num++;
tag->tags = (SML_TAG *)realloc(tag->tags,
sizeof (SML_TAG) *tag->tags_num);
(void) memcpy(&(tag->tags[tag->tags_num - 1]), tmp_tag,
sizeof (SML_TAG));
}
}
/*
* Name: smlReadOneTag
* Description: read one complete tag from a datastream
* Arguments: err - [RO, *RW] (LU_ERR)
* Error object - used to contain any errors encountered
* and return those errors to this methods caller
* r_tag - [RW, *RW] - (SML_TAG **)
* Pointer to handle to place new tag object
* == SML_TAG__NULL if empty tag found (not an error)
* ds - [RO, *RO] - (LU_DS)
* Handle to datastream to read tag from
* Returns: int
* RESULT_OK - tag successfully read
* RESULT_ERR - problem reading tag
* NOTE: Any tag object returned is placed in new storage for the
* calling method. The caller must use 'smlFreeTag' to dispose
* of the storage once the tag object name is no longer needed.
*/
int
smlReadOneTag(SML_TAG **r_tag, char *a_str)
{
int r;
/* entry assertions */
assert(SML_TAG__R_ISVALID(r_tag));
assert(a_str != (char *)NULL);
/* entry debugging info */
_smlLogMsg(LOG_MSG_DEBUG, DBG_SML_READ_ONE_TAG, a_str);
/* reset return tag */
*r_tag = SML_TAG__NULL;
/* read tag from datastream, no parent tag to attach it to */
r = _smlReadTag(r_tag, &a_str, NULL);
if (r != RESULT_OK) {
_smlLogMsg(LOG_MSG_ERR, ERR_SML_CANNOT_READ_TAG);
return (r);
}
if (*r_tag != SML_TAG__NULL) {
_smlLogMsg(LOG_MSG_DEBUG, DBG_SML_ONE_TAG_READ,
(unsigned long)*r_tag,
(*r_tag)->name ? (*r_tag)->name : "<no name>");
} else {
_smlLogMsg(LOG_MSG_DEBUG, DBG_SML_READ_ONE_TAG_NOTAG);
}
/* exit debugging info */
return (RESULT_OK);
}
/*
* Name: smlNewTag
* Description: Create a new (empty) tag object
* Arguments: name - [RO, *RO] - (char *)
* Name of tag; NULL to give the tag no name
* Returns: SML_TAG *
* Tag object created
* NOTE: Any tag object returned is placed in new storage for the
* calling method. The caller must use 'smlFreeTag' to dispose
* of the storage once the tag object name is no longer needed.
* Errors: If the tag object cannot be created, the process exits
*/
SML_TAG *
smlNewTag(char *name)
{
SML_TAG *tag;
/* entry assertions */
assert((name == (char *)NULL) || (*name != '\0'));
/* entry debugging info */
_smlLogMsg(LOG_MSG_DEBUG, DBG_SML_CREATE_NEW_TAG_OBJECT,
name ? name : "<no name>");
/* allocate zeroed storage for the tag object */
tag = (SML_TAG *)calloc(1, sizeof (SML_TAG));
assert(tag != SML_TAG__NULL);
/* if name is provided, duplicate and assign it */
if (name != (char *)NULL) {
tag->name = strdup(name);
}
/* exit assertions */
assert(SML_TAG__ISVALID(tag));
/* exit debugging info */
_smlLogMsg(LOG_MSG_DEBUG, DBG_SML_CREATED_NEW_TAG_OBJECT,
(unsigned long)tag, name ? name : "<no name>");
return (tag);
}
/*
* Name: smlConvertTagToString
* Description: Convert a tag object into a string representation of the XML
* Arguments: tag - [RO, *RO] - (SML_TAG *)
* The tag object to convert to a string
* Returns: char *
* String representation (in XML) of tag object
* == (char *)NULL if conversion is not possible
* NOTE: Any string returned is placed in new storage for the
* calling method. The caller must use 'free' to dispose
* of the storage once the string is no longer needed.
*/
char *
smlConvertTagToString(SML_TAG *tag)
{
char *str = (char *)NULL;
/* entry assertions */
assert(SML_TAG__ISVALID(tag));
/* convert the tag object into the datastream */
(void) _smlWriteSimpleTag(&str, tag);
assert(str != (char *)NULL);
assert(*str != '\0');
/* return the results */
return (str);
}
/*
* Name: smlDbgPrintTag
* Synopsis: Print a representation of an XML tag if debugging
* Arguments: a_tag - [RO, *RO] - (SML_TAG *)
* Pointer to tag structure to dump
* a_format - [RO, RO*] (char *)
* printf-style format for debugging message to be output
* VARG_LIST - [RO] (?)
* arguments as appropriate to 'format' specified
* Returns: void
* If one of the debugging flags is set, the hexdump
* is output.
*/
/*PRINTFLIKE2*/
void
smlDbgPrintTag(SML_TAG *a_tag, char *a_format, ...)
{
va_list ap;
size_t vres = 0;
char bfr[1];
char *rstr = (char *)NULL;
/* entry assertions */
assert(a_format != (char *)NULL);
assert(*a_format != '\0');
assert(SML_TAG__ISVALID(a_tag));
/*
* output the message header
*/
/* determine size of the message in bytes */
va_start(ap, a_format);
vres = vsnprintf(bfr, 1, a_format, ap);
va_end(ap);
assert(vres > 0);
/* allocate storage to hold the message */
rstr = (char *)calloc(1, vres+2);
assert(rstr != (char *)NULL);
/* generate the results of the printf conversion */
va_start(ap, a_format);
vres = vsnprintf(rstr, vres+1, a_format, ap);
va_end(ap);
assert(vres > 0);
assert(*rstr != '\0');
_smlLogMsg(LOG_MSG_DEBUG, "%s", rstr);
free(rstr);
/* convert the tag into a string to be printed */
rstr = smlConvertTagToString(a_tag);
if (rstr != (char *)NULL) {
_smlLogMsg(LOG_MSG_DEBUG, DBG_SML_PRINTTAG, a_tag->name,
strlen(rstr), rstr);
}
free(rstr);
}
/*
* Name: smlDelParam
* Description: Delete a parameter from a tag object
* Arguments: tag - [RO, *RW] - (SML_TAG *)
* The tag object to delete the parameter from
* name - [RO, *RO] - (char *)
* The parameter to delete from the tag object
* Returns: void
* If the parameter exists, it is deleted from the tag
*/
void
smlDelParam(SML_TAG *tag, char *name)
{
int k;
/* entry assertions */
assert(SML_TAG__ISVALID(tag));
assert(tag->name != (char *)NULL);
assert(name != NULL);
assert(*name != '\0');
/* entry debugging info */
_smlLogMsg(LOG_MSG_DEBUG, DBG_SML_DELETE_PARAM,
tag->name, name);
/* if tag has no parameters, nothing to delete */
if (tag->params == NULL) {
_smlLogMsg(LOG_MSG_DEBUG,
DBG_SML_DELETE_PARAM_NO_PARAMS);
return;
}
assert(tag->params_num > 0);
/* search the tag for the parameter */
for (k = 0; k < tag->params_num; k++) {
if (streq(tag->params[k].name, name)) {
break;
}
}
/* if the parameter was not found, nothing to delete */
if (k >= tag->params_num) {
_smlLogMsg(LOG_MSG_DEBUG,
DBG_SML_DELETE_PARAM_NOT_FOUND,
name);
return;
}
/* parameter found - indicate deleted */
assert(tag->params[k].name != (char *)NULL);
assert(tag->params[k].value != (char *)NULL);
_smlLogMsg(LOG_MSG_DEBUG,
DBG_SML_DELETE_PARAM_FOUND,
name, tag->params[k].value);
/* free up storage fro parameter */
free(tag->params[k].name);
free(tag->params[k].value);
/* if not at end, compact parameter storage */
if (k < (tag->params_num -1)) {
(void) memmove(&(tag->params[k]), &(tag->params[k + 1]),
sizeof (SML_PARAM) *(tag->params_num - k - 1));
}
/* one less parameter object in tag */
tag->params_num --;
/*
* If only one parameter left, then delete entire parameter storage,
* otherwise reallocate removing unneeded entry
*/
if (tag->params_num > 0) {
/* realloc removing last element in tag object */
tag->params = (SML_PARAM *)
realloc(tag->params,
sizeof (SML_PARAM) *tag->params_num);
} else {
tag->params = (SML_PARAM *)NULL;
}
}
/*
* Name: smlSetParamF
* Description: Set formatted parameter value in tag object
* Arguments: tag - [RO, *RW] - (SML_TAG *)
* The tag object to set the parameter in
* name - [RO, *RO] - (char *)
* The parameter to add to the tag object
* format - [RO, RO*] (char *)
* printf-style format to create parameter value from
* ... - [RO] (?)
* arguments as appropriate to 'format' specified
* Returns: void
* The parameter value is set in the tag object
* according to the results of the format string
* and arguments
*/
/*PRINTFLIKE3*/
void
smlSetParamF(SML_TAG *tag, char *name, char *format, ...)
{
va_list ap;
size_t vres = 0;
char *bfr = NULL;
char fbfr[1];
/* entry assertions */
assert(SML_TAG__ISVALID(tag));
assert(name != (char *)NULL);
assert(*name != '\0');
assert(format != NULL);
assert(*format != '\0');
/* determine size of the parameter name in bytes */
va_start(ap, format);
vres = vsnprintf(fbfr, 1, format, ap);
va_end(ap);
assert(vres > 0);
/* allocate storage to hold the message */
bfr = (char *)calloc(1, vres+2);
assert(bfr != (char *)NULL);
/* generate the parameter name and store it in the allocated storage */
va_start(ap, format);
vres = vsnprintf(bfr, vres+1, format, ap);
va_end(ap);
assert(vres > 0);
assert(*bfr != '\0');
/* add the parameter to the tag */
smlSetParam(tag, name, bfr);
/* free up temporary storage and return */
free(bfr);
}
/*
* Name: smlGetParam
* Description: Get a format-generated parameter from a tag
* Arguments: tag - [RO, *RO] - (SML_TAG *)
* The tag object to obtain the parameter from
* format - [RO, RO*] (char *)
* printf-style format for parameter name to be
* looked up to be formatted
* ... - [RO] (?)
* arguments as appropriate to 'format' specified
* Returns: char *
* Value of the specified parameter
* == (char *)NULL if the parameter does not exist
* NOTE: Any parameter returned is placed in new storage for the
* calling method. The caller must use 'free' to dispose
* of the storage once the parameter is no longer needed.
*/
/*PRINTFLIKE2*/
char *
smlGetParamF(SML_TAG *tag, char *format, ...)
{
va_list ap;
size_t vres = 0;
char *bfr = NULL;
char fbfr[1];
char *p;
/* entry assertions */
assert(SML_TAG__ISVALID(tag));
assert(format != NULL);
assert(*format != '\0');
/* determine size of the parameter name in bytes */
va_start(ap, format);
vres = vsnprintf(fbfr, 1, format, ap);
va_end(ap);
assert(vres > 0);
/* allocate storage to hold the message */
bfr = (char *)calloc(1, vres+2);
assert(bfr != (char *)NULL);
/* generate the parameter name and store it in the allocated storage */
va_start(ap, format);
vres = vsnprintf(bfr, vres+1, format, ap);
va_end(ap);
assert(vres > 0);
assert(*bfr != '\0');
/* add the parameter to the tag */
p = smlGetParam(tag, bfr);
/* free up temporary storage and return */
free(bfr);
return (p);
}
/*
* Name: smlSetParam
* Description: Set parameter value in tag object
* Arguments: tag - [RO, *RW] - (SML_TAG *)
* The tag object to set the parameter in
* name - [RO, *RO] - (char *)
* The parameter to add to the tag object
* value - [RO, *RO] - (char *)
* The value of the parameter to set in the tag object
* Returns: void
* The parameter value is set in the tag object
*/
void
smlSetParam(SML_TAG *tag, char *name, char *value)
{
SML_PARAM *parameter;
/* entry assertions */
assert(SML_TAG__ISVALID(tag));
assert(name != (char *)NULL);
assert(*name != '\0');
assert(value != (char *)NULL);
/* entry debugging info */
_smlLogMsg(LOG_MSG_DEBUG, DBG_SML_SET_PARAM,
tag->name, name, value);
/* if parameters exist, see if modifying existing parameter */
if (tag->params != NULL) {
int k;
for (k = 0; k < tag->params_num; k++) {
assert(tag->params[k].name != (char *)NULL);
assert(tag->params[k].value != (char *)NULL);
/* if name does not match, skip */
if (!streq(tag->params[k].name, name)) {
continue;
}
/* found parameter - if value is same, leave alone */
if (streq(tag->params[k].value, value)) {
_smlLogMsg(LOG_MSG_DEBUG,
DBG_SML_SET_PARAM_LEAVE_ALONE,
tag->params[k].value);
return;
}
/* exists and has different value - change */
_smlLogMsg(LOG_MSG_DEBUG,
DBG_SML_SET_PARAM_MODIFY,
tag->params[k].value);
free(tag->params[k].value);
tag->params[k].value = strdup(value);
return;
}
}
/* not modifying existing - add new parameter */
_smlLogMsg(LOG_MSG_DEBUG,
DBG_SML_SET_PARAM_CREATE_NEW);
parameter = (SML_PARAM *)calloc(1, sizeof (SML_PARAM));
bzero(parameter, sizeof (SML_PARAM));
parameter->name = strdup(name);
parameter->value = strdup(value);
tag->params_num++;
tag->params = (SML_PARAM *)realloc(tag->params,
sizeof (SML_PARAM) *tag->params_num);
(void) memcpy(&(tag->params[tag->params_num - 1]), parameter,
sizeof (SML_PARAM));
free(parameter);
}
/*
* Name: smlParamEqF
* Description: Determine if parameter is equal to a specified formatted value
* Arguments: tag - [RO, *RO] - (SML_TAG *)
* The tag object to look for the parameter to compare
* findTag - [RO, *RO] - (char *)
* Tag within tag object to look for the parameter in
* findParam - [RO, *RO] - (char *)
* Parameter within tag to look for
* format - [RO, RO*] (char *)
* printf-style format for value to be compared against
* parameter value
* ... - [RO] (?)
* arguments as appropriate to 'format' specified to
* generate the value to compare parameter with
* Returns: boolean_t
* B_TRUE - the parameter exists and matches given value
* B_FALSE - parameter does not exist or does not match
*/
/*PRINTFLIKE4*/
boolean_t
smlParamEqF(SML_TAG *tag, char *findTag, char *findParam, char *format, ...)
{
va_list ap;
size_t vres = 0;
char *bfr = NULL;
char fbfr[1];
boolean_t b;
/* entry assertions */
assert(SML_TAG__ISVALID(tag));
assert(format != NULL);
assert(*format != '\0');
/* determine size of the parameter value in bytes */
va_start(ap, format);
vres = vsnprintf(fbfr, 1, format, ap);
va_end(ap);
assert(vres > 0);
/* allocate storage to hold the message */
bfr = (char *)calloc(1, vres+2);
assert(bfr != (char *)NULL);
/* generate the parameter value and store it in the allocated storage */
va_start(ap, format);
vres = vsnprintf(bfr, vres+1, format, ap);
va_end(ap);
assert(vres > 0);
assert(*bfr != '\0');
/* add the parameter to the tag */
b = smlParamEq(tag, findTag, findParam, bfr);
/* free up temporary storage and return */
free(bfr);
return (b);
}
/*
* Name: smlParamEq
* Description: Determine if parameter is equal to a specified value
* Arguments: tag - [RO, *RO] - (SML_TAG *)
* The tag object to look for the parameter to compare
* findTag - [RO, *RO] - (char *)
* Tag within tag object to look for the parameter in
* findParam - [RO, *RO] - (char *)
* Parameter within tag to look for
* str - [RO, *RO] - (char *)
* Value to compare parameter with
* Returns: boolean_t
* B_TRUE - the parameter exists and matches given value
* B_FALSE - parameter does not exist or does not match
*/
boolean_t
smlParamEq(SML_TAG *tag, char *findTag, char *findParam, char *str)
{
SML_TAG *rtag;
char *rparm;
boolean_t answer;
/* entry assertions */
assert(str != (char *)NULL);
assert(findParam != (char *)NULL);
assert(findTag != (char *)NULL);
assert(SML_TAG__ISVALID(tag));
/* look for the specified tag - if not found, return false */
rtag = smlGetTagByName(tag, 0, findTag);
if (rtag == SML_TAG__NULL) {
return (B_FALSE);
}
/* look for the specified parameter - if not found, return false */
rparm = smlGetParam(rtag, findParam);
if (rparm == (char *)NULL) {
return (B_FALSE);
}
/* parameter found - compare against given value */
answer = strcasecmp(str, rparm);
/* free up parameter storage */
free(rparm);
/* return results of comparison */
return (answer == 0 ? B_TRUE : B_FALSE);
}
/*
* Name: smlFindAndDelTag
* Description: Delete a tag if found in tag object
* Arguments: tag - [RO, *RW] - (SML_TAG *)
* The tag object to delete the tag from
* findTag - [RO, *RO] - (char *)
* Tag within tag object to delete
* Returns: boolean_t
* B_TRUE - tag found and deleted
* B_FALSE - tag not found
*/
boolean_t
smlFindAndDelTag(SML_TAG *tag, char *findTag)
{
SML_TAG *rtag = SML_TAG__NULL;
/* entry assertions */
assert(SML_TAG__ISVALID(tag));
assert(findTag != (char *)NULL);
assert(*findTag != '\0');
/* find the specified tag - if not found, return false */
rtag = smlGetTagByName(tag, 0, findTag);
if (rtag == SML_TAG__NULL) {
return (B_FALSE);
}
/* tag found - delete it and return true */
smlDelTag(tag, rtag);
return (B_TRUE);
}
/*
* Name: smlDup
* Description: Duplicate a tag object
* Arguments: tag - [RO, *RO] - (SML_TAG *)
* The tag object to duplicate
* Returns: SML_TAG *
* A handle to a complete duplicate of the tag provided
* NOTE: Any tag object returned is placed in new storage for the
* calling method. The caller must use 'smlFreeTag' to dispose
* of the storage once the tag object name is no longer needed.
* Errors: If the tag object cannot be duplicated, the process exits
*/
SML_TAG *
smlDup(SML_TAG *tag)
{
SML_TAG *rtag = SML_TAG__NULL;
int i;
/* entry assertions */
assert(SML_TAG__ISVALID(tag));
/* allocate zeroed storage for the tag object */
rtag = (SML_TAG *)calloc(1, sizeof (SML_TAG));
assert(rtag != SML_TAG__NULL);
/* duplicate all parameters of the tag */
rtag->name = (tag->name ? strdup(tag->name) : (char *)NULL);
rtag->params_num = tag->params_num;
if (tag->params != (SML_PARAM *)NULL) {
rtag->params = (SML_PARAM *)
calloc(1, sizeof (SML_PARAM)*rtag->params_num);
bzero(rtag->params, sizeof (SML_PARAM)*rtag->params_num);
for (i = 0; i < rtag->params_num; i++) {
rtag->params[i].name = tag->params[i].name ?
strdup(tag->params[i].name) :
(char *)NULL;
rtag->params[i].value = tag->params[i].value ?
strdup(tag->params[i].value) :
(char *)NULL;
}
}
/* duplicate all elements of the tag */
rtag->tags_num = tag->tags_num;
if (tag->tags != SML_TAG__NULL) {
rtag->tags = (SML_TAG *)
calloc(1, sizeof (SML_TAG)*rtag->tags_num);
bzero(rtag->tags, sizeof (SML_TAG)*rtag->tags_num);
for (i = 0; i < rtag->tags_num; i++) {
SML_TAG *stag;
stag = smlDup(&tag->tags[i]);
(void) memcpy(&rtag->tags[i], stag,
sizeof (SML_TAG));
free(stag);
}
}
/* exit assertions */
assert(SML_TAG__ISVALID(rtag));
/* return */
return (rtag);
}
/*
* Name: smlSetFileStatInfo
* Description; Given a file status structure and path name, encode the
* structure and place it and the name into the specified tag
* in a "_sml_fileStatInfoTag" (private) element
* Arguments: tag - [RO, *RO] - (SML_TAG *)
* The tag object to deposit the information into
* statbuf - [RO, *RO] - (struct stat *)
* Pointer to file status structure to encode
* path - [RO, *RO] - (char *)
* Pointer to path name of file to encode
* Returns: void
* The information is placed into the specified tag object
*/
void
smlSetFileStatInfo(SML_TAG **tag, struct stat *statbuf, char *path)
{
SML_TAG *rtag;
/* entry assertions */
assert(SML_TAG__R_ISVALID(tag));
assert(SML_TAG__ISVALID(*tag));
assert(statbuf != (struct stat *)NULL);
/* if stat info exists, delete it */
(void) smlFindAndDelTag(*tag, _sml_fileStatInfoTag);
/* create the file stat info inside of the top level tag */
assert(smlGetTagByName(*tag, 0, _sml_fileStatInfoTag)
== SML_TAG__NULL);
rtag = smlNewTag(_sml_fileStatInfoTag);
assert(SML_TAG__ISVALID(rtag));
(void) smlAddTag(tag, 0, rtag);
free(rtag);
/* obtain handle on newly created file stat info tag */
rtag = smlGetTagByName(*tag, 0, _sml_fileStatInfoTag);
assert(SML_TAG__ISVALID(rtag));
/* add file info as parameters to the tag */
if (path != (char *)NULL) {
smlSetParam(rtag, "st_path", path);
}
smlSetParamF(rtag, "st_ino", "0x%llx",
(unsigned long long)statbuf->st_ino);
smlSetParamF(rtag, "st_mode", "0x%llx",
(unsigned long long)statbuf->st_mode);
smlSetParamF(rtag, "st_mtime", "0x%llx",
(unsigned long long)statbuf->st_mtime);
smlSetParamF(rtag, "st_ctime", "0x%llx",
(unsigned long long)statbuf->st_ctime);
smlSetParamF(rtag, "st_size", "0x%llx",
(unsigned long long)statbuf->st_size);
}
/*
* Name: smlFstatCompareEQ
* Description: Given a file status structure and path name, look for the
* information placed into a tag object via smlSetFileStatInfo
* and if present compare the encoded information with the
* arguments provided
* Arguments: statbuf - [RO, *RO] - (struct stat *)
* Pointer to file status structure to compare
* tag - [RO, *RO] - (SML_TAG *)
* The tag object to compare against
* path - [RO, *RO] - (char *)
* Pointer to path name of file to compare
* Returns: boolean_t
* B_TRUE - both status structures are identical
* B_FALSE - the status structures are not equal
*/
boolean_t
smlFstatCompareEq(struct stat *statbuf, SML_TAG *tag, char *path)
{
if (tag == SML_TAG__NULL) {
return (B_FALSE);
}
assert(SML_TAG__ISVALID(tag));
if (statbuf == (struct stat *)NULL) {
return (B_FALSE);
}
if (path != (char *)NULL) {
if (smlParamEq(tag,
_sml_fileStatInfoTag, "st_path", path) != B_TRUE) {
return (B_FALSE);
}
}
if (smlParamEqF(tag, _sml_fileStatInfoTag, "st_ino",
"0x%llx", (unsigned long long)statbuf->st_ino) != B_TRUE) {
return (B_FALSE);
}
if (smlParamEqF(tag, _sml_fileStatInfoTag, "st_mode",
"0x%llx", (unsigned long long)statbuf->st_mode) != B_TRUE) {
return (B_FALSE);
}
if (smlParamEqF(tag, _sml_fileStatInfoTag, "st_mtime",
"0x%llx", (unsigned long long)statbuf->st_mtime) != B_TRUE) {
return (B_FALSE);
}
if (smlParamEqF(tag, _sml_fileStatInfoTag, "st_ctime",
"0x%llx", (unsigned long long)statbuf->st_ctime) != B_TRUE) {
return (B_FALSE);
}
if (smlParamEqF(tag, _sml_fileStatInfoTag, "st_size",
"0x%llx", (unsigned long long)statbuf->st_size) != B_TRUE) {
return (B_FALSE);
}
return (B_TRUE);
}
/*
* Name: set_verbose
* Description: Turns on verbose output
* Scope: public
* Arguments: verbose = B_TRUE indicates verbose mode
* Returns: none
*/
void
smlSetVerbose(boolean_t setting)
{
verbose = setting;
}
/*
* Name: get_verbose
* Description: Returns whether or not to output verbose messages
* Scope: public
* Arguments: none
* Returns: B_TRUE - verbose messages should be output
*/
boolean_t
smlGetVerbose()
{
return (verbose);
}
/*
* Name: sml_strPrintf
* Synopsis: Create string from printf style format and arguments
* Description: Call to convert a printf style format and arguments into a
* string of characters placed in allocated storage
* Arguments: format - [RO, RO*] (char *)
* printf-style format for string to be formatted
* ... - [RO] (?)
* arguments as appropriate to 'format' specified
* Returns: char *
* A string representing the printf conversion results
* NOTE: Any string returned is placed in new storage for the
* calling method. The caller must use 'free' to dispose
* of the storage once the string is no longer needed.
* Errors: If the string cannot be created, the process exits
*/
/*PRINTFLIKE1*/
char *
sml_strPrintf(char *a_format, ...)
{
va_list ap;
size_t vres = 0;
char bfr[1];
char *rstr = (char *)NULL;
/* entry assertions */
assert(a_format != (char *)NULL);
assert(*a_format != '\0');
/* determine size of the message in bytes */
va_start(ap, a_format);
vres = vsnprintf(bfr, 1, a_format, ap);
va_end(ap);
assert(vres > 0);
/* allocate storage to hold the message */
rstr = (char *)calloc(1, vres+2);
assert(rstr != (char *)NULL);
/* generate the results of the printf conversion */
va_start(ap, a_format);
vres = vsnprintf(rstr, vres+1, a_format, ap);
va_end(ap);
assert(vres > 0);
assert(*rstr != '\0');
/* return the results */
return (rstr);
}
/*
* Name: sml_strPrintf_r
* Synopsis: Create string from printf style format and arguments
* Description: Call to convert a printf style format and arguments into a
* string of characters placed in allocated storage
* Arguments: a_buf - [RO, *RW] - (char *)
* - Pointer to buffer used as storage space for the
* returned string created
* a_bufLen - [RO, *RO] - (int)
* - Size of 'a_buf' in bytes - a maximum of 'a_bufLen-1'
* bytes will be placed in 'a_buf' - the returned
* string is always null terminated
* a_format - [RO, RO*] (char *)
* printf-style format for string to be formatted
* VARG_LIST - [RO] (?)
* arguments as appropriate to 'format' specified
* Returns: void
*/
/*PRINTFLIKE3*/
void
sml_strPrintf_r(char *a_buf, int a_bufLen, char *a_format, ...)
{
va_list ap;
size_t vres = 0;
/* entry assertions */
assert(a_format != (char *)NULL);
assert(*a_format != '\0');
assert(a_buf != (char *)NULL);
assert(a_bufLen > 1);
/* generate the results of the printf conversion */
va_start(ap, a_format);
vres = vsnprintf(a_buf, a_bufLen-1, a_format, ap);
va_end(ap);
assert(vres > 0);
assert(vres < a_bufLen);
a_buf[a_bufLen-1] = '\0';
}
/*
* Name: sml_XmlEncodeString
* Description: Given a plain text string, convert that string into one that
* encoded using the XML character reference encoding format.
* Arguments: a_plain_text_string - [RO, *RO] (char *)
* The plain text string to convert (encode)
* Returns: char *
* The encoded form of the plain text string provided
* NOTE: Any string returned is placed in new storage for the
* calling method. The caller must use 'lu_memFree' to dispose
* of the storage once the string is no longer needed.
*/
char *
sml_XmlEncodeString(char *a_plainTextString)
{
char *stringHead; /* -> start of string containing encoded data */
long stringTail; /* byte pos of first free byte in stringHead */
long stringLength; /* total bytes allocd starting at stringHead */
char *p; /* temp -> to retrieve bytes from src string */
long textLength = 0; /* length of the string to convert */
/* entry assertions */
assert(a_plainTextString != (char *)NULL);
textLength = strlen(a_plainTextString);
/* Allocate initial string buffer to hold results */
stringLength = textLength*2;
stringTail = 0;
stringHead = (char *)calloc(1, (size_t)stringLength+2);
assert(stringHead != (char *)NULL);
/* Add in the encoded message text */
for (p = a_plainTextString; textLength > 0; p++, textLength--) {
/*
* Must have at least 12 bytes: this must be at least the
* maximum number of bytes that can be added for a single
* byte as the last byte of the stream. Assuming the byte
* needs to be encoded, it could be:
* &#xxxxxxxx;\0
* If not that many bytes left, grow the buffer.
*/
if ((stringLength-stringTail) < 12) {
stringLength += (textLength*2)+12;
stringHead =
realloc(stringHead,
(size_t)stringLength+2);
assert(stringHead != (char *)NULL);
}
/*
* See if this byte is a 'printable 7-bit ascii value'.
* If so just add it to the new string; otherwise, must
* output an XML character value encoding for the byte.
*/
switch (*p) {
case '!':
case '#':
case '%':
case '\'':
case '(':
case ')':
case '*':
case '+':
case ',':
case '-':
case '.':
case '/':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case ':':
case ';':
case '<':
case '=':
case '>':
case '?':
case '@':
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
case '[':
case ']':
case '^':
case '_':
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
case '{':
case '|':
case '}':
case '~':
case ' ':
/*
* It is a printable 7-bit ascii character:
* just add it to the end of the new string.
*/
stringHead[stringTail++] = *p;
break;
default:
/*
* It is not a printable 7-bit ascii character:
* add it as an xml character value encoding.
*/
stringTail += sprintf(&stringHead[stringTail], "&#%x;",
(*p)&0xFF);
break;
}
}
/* Terminate the new string */
stringHead[stringTail] = '\0';
/* realloc the string so it is only as big as it needs to be */
stringHead = realloc(stringHead, stringTail+1);
assert(stringHead != (char *)NULL);
return (stringHead);
}
/*
* Name: sml_XmlDecodeString
* Description: Given a string encoded using the XML character reference format,
* convert that string into a plain text (unencoded) string.
* Arguments: a_xml_encoded_string - [RO, *RO] (char *)
* The XML encoded string to convert to plain text
* Returns: char *
* The unencoded (plain text) form of the encoded string
* NOTE: Any string returned is placed in new storage for the
* calling method. The caller must use 'lu_memFree' to dispose
* of the storage once the string is no longer needed.
*/
char *
sml_XmlDecodeString(char *a_xmlEncodedString)
{
char *s = NULL; /* -> index into encoded bytes string */
char *d = NULL; /* -> index into decoded bytes string */
char *rs = NULL; /* -> string holding ref bytes allocated */
char *ri = NULL; /* -> index into string holding reference */
long textLength = 0; /* length of encoded string to decode */
unsigned long rv = 0; /* temp to hold scanf results of byte conv */
char *i = NULL; /* temp to hold strchr results */
char *stringHead = NULL; /* -> plain test buffer */
ptrdiff_t tmpdiff;
/*
* A finite state machine is used to convert the xml encoded string
* into plain text. The states of the machine are defined below.
*/
int fsmsState = -1; /* Finite state machine state */
#define fsms_text 0 /* Decoding plain text */
#define fsms_seenAmp 1 /* Found & */
#define fsms_seenPound 2 /* Found # following & */
#define fsms_collect 3 /* Collecting character reference bytes */
/* entry assertions */
assert(a_xmlEncodedString != (char *)NULL);
textLength = strlen(a_xmlEncodedString);
/*
* Allocate string that can contain the decoded string.
* Since decoding always results in a shorter string (bytes encoded
* using the XML character reference are larger in the encoded form)
* we can allocate a string the same size as the encoded string.
*/
stringHead = (char *)calloc(1, textLength+1);
assert(stringHead != (char *)NULL);
/*
* Convert all bytes.
*/
/* Decoding plain text */
fsmsState = fsms_text;
for (s = a_xmlEncodedString, d = stringHead; textLength > 0;
s++, textLength--) {
switch (fsmsState) {
case fsms_text: /* Decoding plain text */
if (rs != NULL) {
free(rs);
rs = NULL;
ri = NULL;
}
if (*s == '&') {
/* Found & */
fsmsState = fsms_seenAmp;
continue;
}
*d++ = *s;
continue;
case fsms_seenAmp: /* Found & */
if (*s == '#') {
/* Found # following & */
fsmsState = fsms_seenPound;
continue;
}
fsmsState = fsms_text; /* Decoding plain text */
*d++ = '&';
*d++ = *s;
continue;
case fsms_seenPound: /* Found # following & */
i = strchr(s, ';');
if (i == NULL) {
/* Decoding plain text */
fsmsState = fsms_text;
*d++ = '&';
*d++ = '#';
*d++ = *s;
continue;
}
tmpdiff = (ptrdiff_t)i - (ptrdiff_t)s;
rs = (char *)calloc(1, tmpdiff + 1);
assert(rs != (char *)NULL);
ri = rs;
/* Collecting character reference bytes */
fsmsState = fsms_collect;
/*FALLTHRU*/
/* Collecting character reference bytes */
case fsms_collect:
if (*s != ';') {
switch (*s) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
*ri++ = *s;
break;
default:
*ri = '\0';
*d++ = '&';
*d++ = '#';
tmpdiff = (ptrdiff_t)ri - (ptrdiff_t)rs;
(void) strncpy(d, rs, tmpdiff-1);
*d++ = *s;
/* Decoding plain text */
fsmsState = fsms_text;
break;
}
continue;
}
*ri = '\0';
if (sscanf(rs, "%lx", &rv) != 1) {
*d++ = '?';
} else {
*d++ = (rv & 0xFF);
}
/* Decoding plain text */
fsmsState = fsms_text;
}
}
/* Done converting bytes - deallocate reference byte storage */
free(rs);
/* terminate the converted (plain text) string */
*d = '\0';
/* exit assertions */
assert(stringHead != (char *)NULL);
return (stringHead);
}
/*
* Private Methods
*/
/*
* Name: _smlReadTag
* Description: read complete tag from a datastream
* Arguments: err - [RO, *RW] (LU_ERR)
* Error object - used to contain any errors encountered
* and return those errors to this methods caller
* r_tag - [RW, *RW] - (SML_TAG **)
* Pointer to handle to place new tag object
* == SML_TAG__NULL if empty tag found (not an error)
* ds - [RO, *RO] - (LU_DS)
* Handle to datastream to read tag from
* parent - [RO, *RO] - (char *)
* Name for parent of tag (NONE if top of tag)
* Returns: int
* RESULT_OK - tag successfully read
* RESULT_ERR - problem reading tag
* NOTE: Any tag object returned is placed in new storage for the
* calling method. The caller must use 'smlFreeTag' to dispose
* of the storage once the tag object name is no longer needed.
* Errors: If the tag object cannot be duplicated, the process exits
*/
static int
_smlReadTag(SML_TAG **r_tag, char **a_str, char *parent)
{
int r;
SML_TAG *tag;
SML_TAG *tmp_tag;
char name[MAX_SML_COMPONENT_LENGTH];
int pos = 0;
int c;
char *p = *a_str;
/* entry assertions */
assert(SML_TAG__R_ISVALID(r_tag));
assert(a_str != (char **)NULL);
/* entry debugging info */
_smlLogMsg(LOG_MSG_DEBUG, DBG_SML_READ_TAG,
parent ? parent : "<<TOP TAG>>");
/* reset return tag */
*r_tag = SML_TAG__NULL;
/* allocate zeroed storage for the tag object */
tag = (SML_TAG *)calloc(1, sizeof (SML_TAG));
assert(tag != SML_TAG__NULL);
/* reset name accumulator storage */
bzero(name, sizeof (name));
/* ignore delimters before tag */
for (;;) {
/* read tag character - handle failure/EOF */
if ((*p == '\0') || ((c = (*p++)) == '\0')) {
if (parent == NULL) {
_smlLogMsg(LOG_MSG_DEBUG,
DBG_SML_READTAG_EXPECTED_EOF,
p ? p : "?");
smlFreeTag(tag);
*a_str = p;
return (RESULT_OK);
}
/* EOF in middle of processing tag */
_smlLogMsg(LOG_MSG_ERR,
DBG_SML_READTAG_UNEXPECTED_EOF,
p ? p : "?");
smlFreeTag(tag);
*a_str = p;
return (RESULT_ERR);
}
/* if beginning of tag, break out */
if (c == '<') {
break;
}
/* not tag beginning: ignore delimiters if not inside tag yet */
if (parent == (char *)NULL) {
/* ignore delimters */
if (strchr(" \t", c) != (char *)NULL) {
continue;
}
/* on blank lines, return no tag object */
if (c == '\n') {
_smlLogMsg(LOG_MSG_DEBUG,
DBG_SML_READTAG_BLANKLINE,
p ? p : "?");
smlFreeTag(tag);
*a_str = p;
return (RESULT_OK);
}
/* invalid character before tag start */
_smlLogMsg(LOG_MSG_ERR, ERR_SML_READTAG_BAD_START_CHAR,
c, (unsigned int)c);
*a_str = p;
return (RESULT_ERR);
}
}
/*
* all delimiters have been ignored and opening tag character seen;
* process tag
*/
assert(c == '<');
c = *p;
if (*p != '\0') {
p++;
}
/* handle EOF after tag opening character found */
if (c == '\0') {
_smlLogMsg(LOG_MSG_ERR,
ERR_SML_EOF_BEFORE_TAG_NAME,
parent ? parent : "<<NONE>>");
smlFreeTag(tag);
*a_str = p;
return (RESULT_ERR);
}
/* is this a tag closure? */
if (c == '/') {
_smlLogMsg(LOG_MSG_DEBUG, DBG_SML_START_CLOSE_TAG,
parent ? parent : "<<NONE>>");
for (;;) {
/* get next character of tag name */
c = *p;
if (*p != '\0') {
p++;
}
/* EOF inside tag name? */
if (c == '\0') {
_smlLogMsg(LOG_MSG_ERR,
ERR_SML_READTAG_CLOSE_TAG_EOF,
parent ? parent : "<<NONE>>");
smlFreeTag(tag);
*a_str = p;
return (RESULT_ERR);
}
/* tag close: break out of collection loop */
if (c == '>') {
break;
}
/* see if illegal character in tag name */
/* CSTYLED */
if (strchr("/ \t\n\":<?$'\\`!@#%^&*()+=|[]{};,", c)
!= NULL) {
_smlLogMsg(LOG_MSG_ERR,
ERR_SML_READTAG_CLOSE_TAG_ILLCHAR,
c, (unsigned int)c, name);
smlFreeTag(tag);
*a_str = p;
return (RESULT_ERR);
}
/* valid character - add to name if room left */
if (pos < sizeof (name)-1) {
name[pos] = (c&0xFF);
pos++;
}
assert(pos < sizeof (name));
}
/* close of tag found */
assert(c == '>');
/* is the tag empty? If so that's an error */
if (*name == '\0') {
_smlLogMsg(LOG_MSG_ERR,
ERR_SML_READTAG_CLOSE_EMPTY_TAG);
smlFreeTag(tag);
*a_str = p;
return (RESULT_ERR);
}
/* if no parent, a close tag outside of any open tag */
if (parent == (char *)NULL) {
_smlLogMsg(LOG_MSG_ERR,
ERR_SML_READTAG_CLOSE_NO_PARENT,
name);
smlFreeTag(tag);
*a_str = p;
return (RESULT_ERR);
}
/* if not close to current parent, error */
if (!streq(parent, name)) {
_smlLogMsg(LOG_MSG_ERR,
ERR_SML_READTAG_CLOSE_WRONG_TAG,
name, parent);
smlFreeTag(tag);
*a_str = p;
return (RESULT_ERR);
}
/* close of current tag found - success */
_smlLogMsg(LOG_MSG_DEBUG, DBG_SML_READTAG_CLOSE_TAG,
name);
smlFreeTag(tag);
*a_str = p;
return (RESULT_OK);
}
/* not starting a close tag */
assert(c != '/');
assert(c != '<');
/* at start of tag - input tag name */
bzero(name, sizeof (name));
pos = 0;
for (;;) {
/* EOF inside of tag name? */
if (c == '\0') {
_smlLogMsg(LOG_MSG_ERR,
ERR_SML_READTAG_TAG_EOF,
name, parent ? parent : "<<NONE>>");
smlFreeTag(tag);
*a_str = p;
return (RESULT_ERR);
}
/* if separator or end of line then tag name collected */
if (strchr(" >\t\n", c) != NULL) {
break;
}
/* see if illegal character in tag name */
/*CSTYLED*/
if (strchr("\":<>?$'\\`!@#%^&*()+=|[]{};,", c) != NULL) {
_smlLogMsg(LOG_MSG_ERR,
ERR_SML_READTAG_TAG_ILLCHAR,
c, (unsigned int)c, name);
smlFreeTag(tag);
*a_str = p;
return (RESULT_ERR);
}
/* close current tag? */
if (c == '/') {
/* get next character of tag name */
c = *p;
if (*p != '\0') {
p++;
}
/* tag close not found? */
if (c != '>') {
_smlLogMsg(LOG_MSG_ERR,
ERR_SML_READTAG_BADTAG_CLOSE,
name, parent ? parent : "<<NONE>>");
smlFreeTag(tag);
*a_str = p;
return (RESULT_ERR);
}
/* is the tag empty? If so that's an error */
if (*name == '\0') {
_smlLogMsg(LOG_MSG_ERR,
ERR_SML_READTAG_EMPTY_TAG,
parent ? parent : "<<NONE>>");
smlFreeTag(tag);
*a_str = p;
return (RESULT_ERR);
}
/* tag closed */
_smlLogMsg(LOG_MSG_DEBUG,
DBG_SML_READTAG_CLOSED_TAG,
name, parent ? parent : "<<NONE>>");
tag->name = strdup(name);
*r_tag = tag;
*a_str = p;
return (RESULT_OK);
}
/* valid character - add to name if room left */
if (pos < sizeof (name)-1) {
name[pos] = (c&0xFF);
pos++;
}
assert(pos < sizeof (name));
/* get next character to parse */
c = *p;
if (*p != '\0') {
p++;
}
}
/* have a valid tag name: <tagname */
_smlLogMsg(LOG_MSG_DEBUG, DBG_SML_HAVE_TAG_NAME,
name, parent ? parent : "<<NONE>>");
assert(*name != '\0');
/* place tag name inside of tag object */
tag->name = strdup(name);
/* clear out name accumulator to get parameters */
bzero(name, sizeof (name));
pos = 0;
/* input parameters */
if (c != '>')
for (;;) {
char *pname;
char *pvalue;
SML_PARAM *parameter;
/* pass spaces before parameter name */
for (;;) {
/* get next character of parameter name */
c = *p;
if (*p != '\0') {
p++;
}
/* EOF inside parameter name? */
if (c == '\0') {
_smlLogMsg(LOG_MSG_ERR,
ERR_SML_READTAG_PARM_EOF,
tag->name,
parent ? parent : "<<NONE>>");
smlFreeTag(tag);
*a_str = p;
return (RESULT_ERR);
}
/* if separator/end of line tag parameter collected */
if (strchr(" \t\n", c) != NULL) {
continue;
}
/* see if illegal character in parameter name */
/*CSTYLED*/
if (strchr("\":<?$'\\`!@#%^&*()+=|[]{};,.", c) !=
(char *)NULL) {
_smlLogMsg(LOG_MSG_ERR,
ERR_SML_READTAG_PARMNAME_ILLCHAR,
c, (unsigned int)c, name, tag->name,
parent ? parent : "<<NONE>>");
smlFreeTag(tag);
*a_str = p;
return (RESULT_ERR);
}
/* tag close found? */
if (c == '>') {
break;
}
/* close tag found ? */
if (c == '/') {
c = *p;
if (*p != '\0') {
p++;
}
if (c == '>') {
_smlLogMsg(LOG_MSG_DEBUG,
DBG_SML_TAG_ONLY,
tag->name);
*r_tag = tag;
*a_str = p;
return (RESULT_OK);
}
/* / not followed by > */
_smlLogMsg(LOG_MSG_ERR,
ERR_SML_READTAG_BADPARMNAME_CLOSE,
name, tag->name,
parent ? parent : "<<NONE>>");
smlFreeTag(tag);
*a_str = p;
return (RESULT_ERR);
}
/* valid character - add to name if room left */
if (pos < sizeof (name)-1) {
name[pos] = (c&0xFF);
pos++;
}
assert(pos < sizeof (name));
break;
}
if (c == '>') {
break;
}
/* input parameter name */
for (;;) {
c = *p;
if (*p != '\0') {
p++;
}
/* EOF inside of parameter name? */
if (c == '\0') {
_smlLogMsg(LOG_MSG_ERR,
ERR_SML_READTAG_PARM_EOF,
tag->name,
parent ? parent : "<<NONE>>");
smlFreeTag(tag);
*a_str = p;
return (RESULT_ERR);
}
/*CSTYLED*/
if (strchr("\t \n\":<>?$'\\`!@%^*()+|[]{},./", c) != NULL) {
_smlLogMsg(LOG_MSG_ERR,
ERR_SML_READTAG_PARMNAME_ILLCHAR,
c, (unsigned int)c, name, tag->name,
parent ? parent : "<<NONE>>");
smlFreeTag(tag);
*a_str = p;
return (RESULT_ERR);
}
/* name - value separator found ? */
if (c == '=') {
break;
}
/* valid character - add to name if room left */
if (pos < sizeof (name)-1) {
name[pos] = (c&0xFF);
pos++;
}
assert(pos < sizeof (name));
}
/* is the parameter name empty? If so that's an error */
if (*name == '\0') {
_smlLogMsg(LOG_MSG_ERR,
ERR_SML_READTAG_EMPTY_PARMNAME,
tag->name, parent ? parent : "<<NONE>>");
smlFreeTag(tag);
*a_str = p;
return (RESULT_ERR);
}
/* have a parameter name */
_smlLogMsg(LOG_MSG_DEBUG, DBG_SML_HAVE_PARM_NAME,
name, tag->name);
/* duplicate (save) parameter name */
pname = strdup(name);
/* clear out name accumulator to get parameters */
bzero(name, sizeof (name));
pos = 0;
c = *p;
if (*p != '\0') {
p++;
}
if (c != '"') {
_smlLogMsg(LOG_MSG_ERR,
ERR_SML_PARM_SEP_BAD,
c, (unsigned int)c);
free(pname);
smlFreeTag(tag);
*a_str = p;
return (RESULT_ERR);
}
/* input parameter value */
for (;;) {
c = *p;
if (*p != '\0') {
p++;
}
/* EOF inside of parameter value? */
if (c == '\0') {
_smlLogMsg(LOG_MSG_ERR,
ERR_SML_READTAG_PARMVAL_EOF,
pname, tag->name,
parent ? parent : "<<NONE>>");
smlFreeTag(tag);
free(pname);
*a_str = p;
return (RESULT_ERR);
}
/* close of parameter value? */
if (c == '"') {
break;
}
if (strchr("\n", c) != NULL) {
_smlLogMsg(LOG_MSG_ERR,
ERR_SML_READTAG_PARMVAL_NL,
pname, tag->name,
parent ? parent : "<<NONE>>");
free(pname);
smlFreeTag(tag);
*a_str = p;
return (RESULT_ERR);
}
/* valid character - add to value if room left */
if (pos < sizeof (name)-1) {
name[pos] = (c&0xFF);
pos++;
}
assert(pos < sizeof (name));
}
/* got the value */
_smlLogMsg(LOG_MSG_DEBUG, DBG_SML_HAVE_PARM_VALUE,
pname, name, tag->name);
pvalue = sml_XmlDecodeString(name);
bzero(name, sizeof (name));
pos = 0;
parameter = (SML_PARAM *)calloc(1, sizeof (SML_PARAM));
bzero(parameter, sizeof (SML_PARAM));
parameter->name = pname;
parameter->value = pvalue;
tag->params_num++;
tag->params = (SML_PARAM *)
realloc(tag->params,
sizeof (SML_PARAM) *tag->params_num);
(void) memcpy(&(tag->params[tag->params_num - 1]), parameter,
sizeof (SML_PARAM));
free(parameter);
if (c == '>') {
break;
}
}
/* finished processing this tag element entry */
_smlLogMsg(LOG_MSG_DEBUG, DBG_SML_TAG_HEAD_DONE,
tag->name, parent ? parent : "<<NULL>>");
tag->tags = NULL;
while (((r = _smlReadTag(&tmp_tag, &p, tag->name))
== RESULT_OK) && (tmp_tag != NULL)) {
tag->tags_num++;
tag->tags = (SML_TAG *)realloc(tag->tags,
sizeof (SML_TAG) *tag->tags_num);
(void) memcpy(&(tag->tags[tag->tags_num - 1]), tmp_tag,
sizeof (SML_TAG));
free(tmp_tag);
}
c = *p;
if (*p != '\0') {
p++;
}
*r_tag = tag;
*a_str = p;
return (r);
}
/*
* Name: _smlWriteParamValue
* Description: XML Encode a plain text parameter value and write to datastream
* Arguments: ds - [RO, *RO] - (LU_DS)
* Handle to datastream to write parameter value to
* value - [RO, *RO] - (char *)
* Parameter value to be encoded and written
* Returns: int
* RESULT_OK - tag successfully read
* RESULT_ERR - problem reading tag
*/
static int
_smlWriteParamValue(char **a_str, char *value)
{
char *ns;
char *p;
/* entry assertions */
assert(a_str != (char **)NULL);
assert(value != (char *)NULL);
/* xml encode the plain text string */
p = sml_XmlEncodeString(value);
assert(p != (char *)NULL);
/* write the xml encoded parameter value to the datastream */
ns = sml_strPrintf("%s\"%s\"", *a_str ? *a_str : "", p);
/* free up xml encoded value storage */
free(p);
if (ns == NULL) {
return (RESULT_ERR);
}
if (*a_str != NULL) {
free(*a_str);
}
*a_str = ns;
/* return results */
return (RESULT_OK);
}
static int
_smlWriteSimpleTag(char **a_str, SML_TAG *tag)
{
int r;
int k;
char *ns;
char *np0;
char *np1;
if (tag == NULL) {
return (RESULT_OK);
}
if (*a_str == NULL) {
*a_str = strdup("");
}
if (tag->params_num == 0) {
if (tag->tags_num == 0) {
ns = sml_strPrintf("%s<%s/>\n", *a_str, tag->name);
free(*a_str);
*a_str = ns;
return (RESULT_OK);
} else {
ns = sml_strPrintf("%s<%s>\n", *a_str, tag->name);
if (ns == NULL) {
return (RESULT_ERR);
}
free(*a_str);
*a_str = ns;
}
} else {
ns = sml_strPrintf("%s<%s %s=", *a_str ? *a_str : "", tag->name,
tag->params[0].name);
if (ns == NULL) {
return (RESULT_ERR);
}
free(*a_str);
*a_str = ns;
np0 = NULL;
r = _smlWriteParamValue(&np0, tag->params[0].value);
if ((np0 == NULL) || (r != RESULT_OK)) {
return (RESULT_ERR);
}
ns = sml_strPrintf("%s%s", *a_str, np0);
if (ns == NULL) {
return (RESULT_ERR);
}
free(np0);
free(*a_str);
*a_str = ns;
for (k = 1; k < tag->params_num; k++) {
np0 = sml_strPrintf(" %s=", tag->params[k].name);
if (np0 == NULL) {
return (RESULT_ERR);
}
np1 = NULL;
r = _smlWriteParamValue(&np1, tag->params[k].value);
if ((np1 == NULL) || (r != RESULT_OK)) {
return (RESULT_ERR);
}
ns = sml_strPrintf("%s%s%s", *a_str, np0, np1);
if (ns == NULL) {
return (RESULT_ERR);
}
free(np0);
free(np1);
free(*a_str);
*a_str = ns;
}
if (tag->tags_num == 0) {
np0 = sml_strPrintf("/>\n");
if (np0 == NULL) {
return (RESULT_ERR);
}
ns = sml_strPrintf("%s%s", *a_str, np0);
if (ns == NULL) {
return (RESULT_ERR);
}
free(np0);
free(*a_str);
*a_str = ns;
} else {
np0 = sml_strPrintf(">\n");
if (np0 == NULL) {
return (RESULT_ERR);
}
ns = sml_strPrintf("%s%s", *a_str, np0);
if (ns == NULL) {
return (RESULT_ERR);
}
free(np0);
free(*a_str);
*a_str = ns;
}
}
for (k = 0; k < tag->tags_num; k++) {
r = _smlWriteSimpleTag(a_str, &(tag->tags[k]));
if (r != RESULT_OK) {
return (r);
}
}
if (tag->tags_num > 0) {
np0 = sml_strPrintf("</%s>\n", tag->name);
if (np0 == NULL) {
return (RESULT_ERR);
}
ns = sml_strPrintf("%s%s", *a_str ? *a_str : "", np0);
if (ns == NULL) {
return (RESULT_ERR);
}
free(np0);
free(*a_str);
*a_str = ns;
}
return (RESULT_OK);
}
static void
_smlFreeTag(SML_TAG *tag)
{
int k;
/* entry assertions */
assert(tag != SML_TAG__NULL);
/* entry debugging info */
_smlLogMsg(LOG_MSG_DEBUG, DBG_SML_INT_FREE_TAG,
(unsigned long)tag,
tag->name ? tag->name : "<<NONE>>",
tag->params_num, tag->tags_num);
for (k = 0; k < tag->params_num; k++) {
_smlLogMsg(LOG_MSG_DEBUG, DBG_SML_INT_FREE_PARAM_NAME,
(unsigned long)(&tag->params[k]),
(unsigned long)(tag->params[k].name),
tag->params[k].name);
free(tag->params[k].name);
tag->params[k].name = (char *)NULL;
_smlLogMsg(LOG_MSG_DEBUG, DBG_SML_INT_FREE_PARAM_VALUE,
(unsigned long)(&tag->params[k]),
(unsigned long)(tag->params[k].value),
tag->params[k].value);
free(tag->params[k].value);
tag->params[k].value = (char *)NULL;
}
for (k = 0; k < tag->tags_num; k++) {
_smlFreeTag(&tag->tags[k]);
}
if (tag->name != NULL) {
_smlLogMsg(LOG_MSG_DEBUG, DBG_SML_INT_FREE_TAG_NAME,
(unsigned long)tag->name, tag->name);
free(tag->name);
tag->name = NULL;
}
if (tag->params != NULL) {
assert(tag->params_num > 0);
bzero(tag->params, sizeof (SML_PARAM)*tag->params_num);
_smlLogMsg(LOG_MSG_DEBUG, DBG_SML_INT_FREE_PARAMS,
(unsigned long)tag->params);
free(tag->params);
tag->params = NULL;
tag->params_num = 0;
}
if (tag->tags != NULL) {
assert(tag->tags_num > 0);
bzero(tag->tags, sizeof (SML_TAG)*tag->tags_num);
_smlLogMsg(LOG_MSG_DEBUG, DBG_SML_INT_FREE_TAGS,
(unsigned long)tag->tags);
free(tag->tags);
tag->tags = NULL;
tag->tags_num = 0;
}
}
/*
* Name: log_msg
* Description: Outputs messages to logging facility.
* Scope: public
* Arguments: type - the severity of the message
* out - where to output the message.
* fmt - the printf format, plus its arguments
* Returns: none
*/
/*PRINTFLIKE2*/
static void
_smlLogMsg(LogMsgType a_type, const char *a_format, ...)
{
va_list ap;
size_t vres = 0;
char bfr[1];
char *rstr = (char *)NULL;
FILE *out;
char *prefix;
switch (a_type) {
case LOG_MSG_ERR:
default:
out = stderr;
prefix = MSG_LOG_ERROR;
break;
case LOG_MSG_WRN:
out = stderr;
prefix = MSG_LOG_WARNING;
break;
case LOG_MSG_INFO:
out = stdout;
prefix = NULL;
break;
case LOG_MSG_DEBUG:
if (!smlGetVerbose()) {
/* no debug messages if not verbose mode */
return;
}
out = stderr;
prefix = MSG_LOG_DEBUG;
break;
}
if (prefix != NULL) {
(void) fprintf(out, "%s: ", prefix);
}
/* determine size of the message in bytes */
va_start(ap, a_format);
vres = vsnprintf(bfr, 1, a_format, ap);
va_end(ap);
/* allocate storage to hold the message */
rstr = (char *)malloc(vres+2);
/* generate the results of the printf conversion */
va_start(ap, a_format);
vres = vsnprintf(rstr, vres+1, a_format, ap);
va_end(ap);
if (fprintf(out, "%s\n", rstr) < 0) {
/*
* nothing output, try stderr as a
* last resort
*/
(void) fprintf(stderr, ERR_LOG_FAIL, a_format);
}
free(rstr);
}
|