File: interface.c

package info (click to toggle)
libnih 1.0.3-4.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 10,544 kB
  • sloc: ansic: 188,634; sh: 11,217; makefile: 1,116; yacc: 291; xml: 239; sed: 16
file content (2712 lines) | stat: -rw-r--r-- 77,264 bytes parent folder | download | duplicates (6)
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
/* nih-dbus-tool
 *
 * interface.c - interface parsing and handling
 *
 * Copyright © 2009 Scott James Remnant <scott@netsplit.com>.
 * Copyright © 2009 Canonical Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2, as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */

#include <string.h>

#include <nih/macros.h>
#include <nih/alloc.h>
#include <nih/list.h>
#include <nih/string.h>
#include <nih/logging.h>
#include <nih/error.h>

#include "symbol.h"
#include "indent.h"
#include "type.h"
#include "demarshal.h"
#include "node.h"
#include "interface.h"
#include "method.h"
#include "property.h"
#include "parse.h"
#include "errors.h"


/**
 * interface_name_valid:
 * @name: Interface name to verify.
 *
 * Verifies whether @name matches the specification for D-Bus interface
 * names.
 *
 * Returns: TRUE if valid, FALSE if not.
 **/
int
interface_name_valid (const char *name)
{
	size_t parts = 1;

	nih_assert (name != NULL);

	/* Name must not begin with a '.' */
	if (name[0] == '.')
		return FALSE;

	/* We can get away with just using strlen() here even through name
	 * is in UTF-8 because all the valid characters are ASCII.
	 */
	for (size_t i = 0; i < strlen (name); i++) {
		/* Name components may be separated by single '.' characters,
		 * multiple ones are not allowed.  Keep a count of how many
		 * parts we have, since there's a defined minimum.
		 */
		if (name[i] == '.') {
			if (name[i-1] == '.')
				return FALSE;

			parts++;
			continue;
		}

		/* Names may contain digits, but not at the beginning of the
		 * name or any part of it.
		 */
		if ((name[i] >= '0') && (name[i] <= '9')) {
			if (i == 0)
				return FALSE;
			if (name[i-1] == '.')
				return FALSE;

			continue;
		}

		/* Valid characters anywhere are [A-Za-z_] */
		if (   ((name[i] < 'A') || (name[i] > 'Z'))
		    && ((name[i] < 'a') || (name[i] > 'z'))
		    && (name[i] != '_'))
			return FALSE;
	}

	/* Name must consist of at least two parts */
	if (parts < 2)
		return FALSE;

	/* Final character may not be '.' */
	if (name[strlen (name) - 1] == '.')
		return FALSE;

	/* Name must be no more than 255 characters */
	if (strlen (name) > 255)
		return FALSE;

	return TRUE;
}


/**
 * interface_new:
 * @parent: parent object for new interface,
 * @name: D-Bus name of interface.
 *
 * Allocates a new D-Bus object Interface data structure, with the D-Bus
 * name set to @name.  The returned structure is not placed into any list.
 *
 * If @parent is not NULL, it should be a pointer to another object which
 * will be used as a parent for the returned interface.  When all parents
 * of the returned interface are freed, the returned interface will also be
 * freed.
 *
 * Returns: the new interface or NULL if the allocation failed.
 **/
Interface *
interface_new (const void *parent,
	       const char *name)
{
	Interface *interface;

	nih_assert (name != NULL);

	interface = nih_new (parent, Interface);
	if (! interface)
		return NULL;

	nih_list_init (&interface->entry);

	nih_alloc_set_destructor (interface, nih_list_destroy);

	interface->name = nih_strdup (interface, name);
	if (! interface->name) {
		nih_free (interface);
		return NULL;
	}

	interface->symbol = NULL;
	interface->deprecated = FALSE;

	nih_list_init (&interface->methods);
	nih_list_init (&interface->signals);
	nih_list_init (&interface->properties);

	return interface;
}


/**
 * interface_start_tag:
 * @xmlp: XML parser,
 * @tag: name of XML tag being parsed,
 * @attr: NULL-terminated array of attribute name and value pairs.
 *
 * This function is called by parse_start_tag() for an "interface"
 * start tag, a child of the "node" tag that defines a D-Bus interface
 * implemented by that object.
 *
 * If the interface does not appear within a node tag a warning is emitted
 * and the tag will be ignored.
 *
 * Interfaces must have a "name" attribute containing the D-Bus name
 * of the interface.
 *
 * Any unknown attributes result in a warning and will be ignored.
 *
 * An Interface object will be allocated and pushed onto the stack, this is
 * not added to the node until the end tag is found.
 *
 * Returns: zero on success, negative value on raised error.
 **/
int
interface_start_tag (XML_Parser    xmlp,
		     const char *  tag,
		     char * const *attr)
{
	ParseContext *       context;
	ParseStack *         parent;
	nih_local Interface *interface = NULL;
	char * const *       key;
	char * const *       value;
	const char *         name = NULL;

	nih_assert (xmlp != NULL);
	nih_assert (tag != NULL);
	nih_assert (attr != NULL);

	context = XML_GetUserData (xmlp);
	nih_assert (context != NULL);

	/* Interfaces should only appear inside nodes. */
	parent = parse_stack_top (&context->stack);
	if ((! parent) || (parent->type != PARSE_NODE)) {
		nih_warn ("%s:%zu:%zu: %s", context->filename,
			  (size_t)XML_GetCurrentLineNumber (xmlp),
			  (size_t)XML_GetCurrentColumnNumber (xmlp),
			  _("Ignored unexpected <interface> tag"));

		if (! parse_stack_push (NULL, &context->stack,
					PARSE_IGNORED, NULL))
			nih_return_system_error (-1);

		return 0;
	}

	/* Retrieve the name from the attributes */
	for (key = attr; key && *key; key += 2) {
		value = key + 1;
		nih_assert (value && *value);

		if (! strcmp (*key, "name")) {
			name = *value;
		} else {
			nih_warn ("%s:%zu:%zu: %s: %s", context->filename,
				  (size_t)XML_GetCurrentLineNumber (xmlp),
				  (size_t)XML_GetCurrentColumnNumber (xmlp),
				  _("Ignored unknown <interface> attribute"),
				  *key);
		}
	}

	/* Check we have a name and that it's valid */
	if (! name)
		nih_return_error (-1, INTERFACE_MISSING_NAME,
				  _(INTERFACE_MISSING_NAME_STR));
	if (! interface_name_valid (name))
		nih_return_error (-1, INTERFACE_INVALID_NAME,
				  _(INTERFACE_INVALID_NAME_STR));

	/* Allocate an Interface object and push onto the stack */
	interface = interface_new (NULL, name);
	if (! interface)
		nih_return_system_error (-1);

	if (! parse_stack_push (NULL, &context->stack,
				PARSE_INTERFACE, interface)) {
		nih_error_raise_system ();
		return -1;
	}

	return 0;
}

/**
 * interface_end_tag:
 * @xmlp: XML parser,
 * @tag: name of XML tag being parsed.
 *
 * This function is called by parse_end_tag() for an "interface" end
 * tag, and matches a call to interface_start_tag() made at the same
 * parsing level.
 *
 * The interface is added to the list of interfaces defined by its parent
 * node.
 *
 * Returns: zero on success, negative value on raised error.
 **/
int
interface_end_tag (XML_Parser  xmlp,
		   const char *tag)
{
	ParseContext *context;
	ParseStack *  entry;
	ParseStack *  parent;
	Interface *   interface;
	Interface *   conflict;
	Node *        node;

	nih_assert (xmlp != NULL);
	nih_assert (tag != NULL);

	context = XML_GetUserData (xmlp);
	nih_assert (context != NULL);

	entry = parse_stack_top (&context->stack);
	nih_assert (entry != NULL);
	nih_assert (entry->type == PARSE_INTERFACE);
	interface = entry->interface;

	/* Generate a symbol from the trailing part of the name */
	if (! interface->symbol) {
		char *trail;

		trail = strrchr (interface->name, '.');
		nih_assert (trail != NULL);
		trail++;

		interface->symbol = symbol_from_name (interface, trail);
		if (! interface->symbol)
			nih_return_no_memory_error (-1);
	}

	nih_list_remove (&entry->entry);
	parent = parse_stack_top (&context->stack);
	nih_assert (parent != NULL);
	nih_assert (parent->type == PARSE_NODE);
	node = parent->node;

	/* Make sure there's not a conflict before adding the interface */
	conflict = node_lookup_interface (node, interface->symbol);
	if (conflict) {
		nih_error_raise_printf (INTERFACE_DUPLICATE_SYMBOL,
					_(INTERFACE_DUPLICATE_SYMBOL_STR),
					interface->symbol, conflict->name);
		return -1;
	}

	nih_debug ("Add %s interface to %s node",
		   interface->name, node->path ?: "(unknown)");
	nih_ref (interface, node);
	nih_list_add (&node->interfaces, &interface->entry);

	nih_free (entry);

	return 0;
}


/**
 * interface_annotation:
 * @interface: interface object annotation applies to,
 * @name: annotation name,
 * @value: annotation value.
 *
 * Handles applying the annotation @name with value @value to the interface
 * @interface.  Interfaces may be annotated as deprecated or may have an
 * alternate symbol name specified.
 *
 * Unknown annotations or illegal values to the known annotations result
 * in an error being raised.
 *
 * Returns: zero on success, negative value on raised error.
 **/
int
interface_annotation (Interface * interface,
		      const char *name,
		      const char *value)
{
	nih_assert (interface != NULL);
	nih_assert (name != NULL);
	nih_assert (value != NULL);

	if (! strcmp (name, "org.freedesktop.DBus.Deprecated")) {
		if (! strcmp (value, "true")) {
			nih_debug ("Marked %s interface as deprecated",
				   interface->name);
			interface->deprecated = TRUE;
		} else if (! strcmp (value, "false")) {
			nih_debug ("Marked %s interface as not deprecated",
				   interface->name);
			interface->deprecated = FALSE;
		} else {
			nih_return_error (-1, INTERFACE_ILLEGAL_DEPRECATED,
					  _(INTERFACE_ILLEGAL_DEPRECATED_STR));
		}

	} else if (! strcmp (name, "com.netsplit.Nih.Symbol")) {
		if (symbol_valid (value)) {
			if (interface->symbol)
				nih_unref (interface->symbol, interface);

			interface->symbol = nih_strdup (interface, value);
			if (! interface->symbol)
				nih_return_no_memory_error (-1);

			nih_debug ("Set %s interface symbol to %s",
				   interface->name, interface->symbol);
		} else {
			nih_return_error (-1, INTERFACE_INVALID_SYMBOL,
					  _(INTERFACE_INVALID_SYMBOL_STR));
		}

	} else {
		nih_error_raise_printf (INTERFACE_UNKNOWN_ANNOTATION,
					"%s: %s: %s",
					_(INTERFACE_UNKNOWN_ANNOTATION_STR),
					interface->name, name);
		return -1;
	}

	return 0;
}


/**
 * interface_methods_array:
 * @parent: parent object for new string,
 * @prefix: prefix for array name,
 * @interface: interface to generate array for,
 * @with_handlers: whether to include handler pointers,
 * @prototypes: list to append prototype to.
 *
 * Generates C code to declare an array of NihDBusMethod variables
 * containing information about the methods of the interface @interface;
 * this will also include array definitions for the arguments of each
 * method, since these are referred to by the returned array.
 *
 * If @with_handlers is TRUE the returned array will contain pointers to
 * handler functions that should be already defined (or at least prototyped);
 * when FALSE this member will be NULL.
 *
 * The prototype of the returned variable declaration is returned as a
 * TypeVar object appended to the @prototypes list.  The arguments array
 * prototypes are not returned since they are made static.
 *
 * If @parent is not NULL, it should be a pointer to another object which
 * will be used as a parent for the returned string.  When all parents
 * of the returned string are freed, the return string will also be
 * freed.
 *
 * Returns: newly allocated string or NULL if insufficient memory.
 **/
char *
interface_methods_array (const void *parent,
			 const char *prefix,
			 Interface * interface,
			 int         with_handlers,
			 NihList *   prototypes)
{
	nih_local char *name = NULL;
	NihList         vars;
	size_t          max_name = 0;
	size_t          max_args = 0;
	size_t          max_handler = 0;
	nih_local char *args = NULL;
	TypeVar *       var;
	nih_local char *block = NULL;
	char *          code;

	nih_assert (prefix != NULL);
	nih_assert (interface != NULL);
	nih_assert (prototypes != NULL);

	name = symbol_impl (NULL, prefix, interface->name, NULL, "methods");
	if (! name)
		return NULL;

	nih_list_init (&vars);

	/* Figure out the longest method name, arguments array variable name
	 * and handler function name.
	 */
	NIH_LIST_FOREACH (&interface->methods, iter) {
		Method *        method = (Method *)iter;
		NihList         args_prototypes;
		nih_local char *args_array = NULL;
		nih_local char *handler_name = NULL;

		/* Obtain the arguments array for the method, giving us the
		 * name of the array.  Append it as a static to the block
		 * we prepend to our code.
		 */
		nih_list_init (&args_prototypes);
		args_array = method_args_array (NULL, prefix, interface,
						method, &args_prototypes);
		if (! args_array)
			return NULL;

		if (! nih_strcat_sprintf (&args, NULL,
					  "static %s"
					  "\n",
					  args_array))
			return NULL;

		nih_assert (! NIH_LIST_EMPTY (&args_prototypes));

		var = (TypeVar *)args_prototypes.next;
		nih_list_add (&vars, &var->entry);
		nih_ref (var, args);

		/* Calculate size of method name and args var name */
		if (strlen (method->name) > max_name)
			max_name = strlen (method->name);

		if (strlen (var->name) > max_args)
			max_args = strlen (var->name);

		/* Work out name of handler or leave space for "NULL" */
		if (with_handlers) {
			handler_name = symbol_impl (NULL, prefix,
						    interface->name,
						    method->name, "method");
			if (! handler_name)
				return NULL;

			if (strlen (handler_name) > max_handler)
				max_handler = strlen (handler_name);
		} else {
			if (max_handler < 4)
				max_handler = 4;
		}
	}

	/* Append each method such that the names, args variable names and
	 * handler function names are all lined up with each other.
	 */
	var = (TypeVar *)vars.next;
	NIH_LIST_FOREACH (&interface->methods, iter) {
		Method *        method = (Method *)iter;
		nih_local char *line = NULL;
		char *          dest;
		nih_local char *handler_name = NULL;

		nih_assert (&var->entry != &vars);

		/* Allocate the line and fill in the values, padding out
		 * where necessary.
		 */
		line = nih_alloc (NULL, max_name + max_args + max_handler + 13);
		if (! line)
			return NULL;

		dest = line;

		memcpy (dest, "{ \"", 3);
		dest += 3;

		memcpy (dest, method->name, strlen (method->name));
		dest += strlen (method->name);

		memcpy (dest, "\", ", 3);
		dest += 3;

		memset (dest, ' ', max_name - strlen (method->name));
		dest += max_name - strlen (method->name);

		memcpy (dest, var->name, strlen (var->name));
		dest += strlen (var->name);

		memcpy (dest, ", ", 2);
		dest += 2;

		memset (dest, ' ', max_args - strlen (var->name));
		dest += max_args - strlen (var->name);

		if (with_handlers) {
			handler_name = symbol_impl (NULL, prefix,
						    interface->name,
						    method->name, "method");
			if (! handler_name)
				return NULL;

			memcpy (dest, handler_name, strlen (handler_name));
			dest += strlen (handler_name);

			memset (dest, ' ', max_handler - strlen (handler_name));
			dest += max_handler - strlen (handler_name);
		} else {
			memcpy (dest, "NULL", 4);
			dest += 4;

			memset (dest, ' ', max_handler - 4);
			dest += max_handler - 4;
		}

		memcpy (dest, " },\n", 4);
		dest += 4;

		*dest = '\0';

		if (! nih_strcat (&block, NULL, line))
			return NULL;

		var = (TypeVar *)var->entry.next;
	}

	/* Append the final element to the block of elements, indent and
	 * surround with the structure definition.
	 */
	if (! nih_strcat (&block, NULL, "{ NULL }\n"))
		return NULL;

	if (! indent (&block, NULL, 1))
		return NULL;

	code = nih_sprintf (parent,
			    "%s"
			    "const NihDBusMethod %s[] = {\n"
			    "%s"
			    "};\n",
			    args ?: "",
			    name,
			    block);
	if (! code)
		return NULL;

	/* Append the prototype to the list */
	var = type_var_new (code, "const NihDBusMethod", name);
	if (! var) {
		nih_free (code);
		return NULL;
	}

	var->array = TRUE;

	nih_list_add (prototypes, &var->entry);

	return code;
}

/**
 * interface_signals_array:
 * @parent: parent object for new string,
 * @prefix: prefix for array name,
 * @interface: interface to generate array for,
 * @with_filter: whether to include filter function pointers,
 * @prototypes: list to append prototype to.
 *
 * Generates C code to declare an array of NihDBusSignal variables
 * containing information about the signals of the interface @interface;
 * this will also include array definitions for the arguments of each
 * signals, since these are referred to by the returned array.
 *
 * If @with_filters is TRUE the returned array will contain pointers to
 * filter functions that should be already defined (or at least prototyped);
 * when FALSE this member will be NULL.
 *
 * The prototype of the returned variable declaration is returned as a
 * TypeVar object appended to the @prototypes list.  The arguments array
 * prototypes are not returned since they are made static.
 *
 * If @parent is not NULL, it should be a pointer to another object which
 * will be used as a parent for the returned string.  When all parents
 * of the returned string are freed, the return string will also be
 * freed.
 *
 * Returns: newly allocated string or NULL if insufficient memory.
 **/
char *
interface_signals_array (const void *parent,
			 const char *prefix,
			 Interface * interface,
			 int         with_filters,
			 NihList *   prototypes)
{
	nih_local char *name = NULL;
	NihList         vars;
	size_t          max_name = 0;
	size_t          max_args = 0;
	size_t          max_filter = 0;
	nih_local char *args = NULL;
	TypeVar *       var;
	nih_local char *block = NULL;
	char *          code;

	nih_assert (prefix != NULL);
	nih_assert (interface != NULL);
	nih_assert (prototypes != NULL);

	name = symbol_impl (NULL, prefix, interface->name, NULL, "signals");
	if (! name)
		return NULL;

	nih_list_init (&vars);

	/* Figure out the longest signal name, arguments array variable name
	 * and filter function name.
	 */
	NIH_LIST_FOREACH (&interface->signals, iter) {
		Signal *        signal = (Signal *)iter;
		NihList         args_prototypes;
		nih_local char *args_array = NULL;
		nih_local char *filter_name = NULL;

		/* Obtain the arguments array for the signal, giving us the
		 * name of the array.  Append it as a static to the block
		 * we prepend to our code.
		 */
		nih_list_init (&args_prototypes);
		args_array = signal_args_array (NULL, prefix, interface,
						signal, &args_prototypes);
		if (! args_array)
			return NULL;

		if (! nih_strcat_sprintf (&args, NULL,
					  "static %s"
					  "\n",
					  args_array))
			return NULL;

		nih_assert (! NIH_LIST_EMPTY (&args_prototypes));

		var = (TypeVar *)args_prototypes.next;
		nih_list_add (&vars, &var->entry);
		nih_ref (var, args);

		/* Calculate size of signal name and args var name */
		if (strlen (signal->name) > max_name)
			max_name = strlen (signal->name);

		if (strlen (var->name) > max_args)
			max_args = strlen (var->name);

		/* Work out name of filter or leave space for "NULL" */
		if (with_filters) {
			filter_name = symbol_impl (NULL, prefix,
						    interface->name,
						    signal->name, "signal");
			if (! filter_name)
				return NULL;

			if (strlen (filter_name) > max_filter)
				max_filter = strlen (filter_name);
		} else {
			if (max_filter < 4)
				max_filter = 4;
		}
	}

	/* Append each signal such that the names, args variable names and
	 * filter function names are all lined up with each other.
	 */
	var = (TypeVar *)vars.next;
	NIH_LIST_FOREACH (&interface->signals, iter) {
		Signal *        signal = (Signal *)iter;
		nih_local char *line = NULL;
		char *          dest;
		nih_local char *filter_name = NULL;

		nih_assert (&var->entry != &vars);

		/* Allocate the line and fill in the values, padding out
		 * where necessary.
		 */
		line = nih_alloc (NULL, max_name + max_args + max_filter + 13);
		if (! line)
			return NULL;

		dest = line;

		memcpy (dest, "{ \"", 3);
		dest += 3;

		memcpy (dest, signal->name, strlen (signal->name));
		dest += strlen (signal->name);

		memcpy (dest, "\", ", 3);
		dest += 3;

		memset (dest, ' ', max_name - strlen (signal->name));
		dest += max_name - strlen (signal->name);

		memcpy (dest, var->name, strlen (var->name));
		dest += strlen (var->name);

		memcpy (dest, ", ", 2);
		dest += 2;

		memset (dest, ' ', max_args - strlen (var->name));
		dest += max_args - strlen (var->name);

		if (with_filters) {
			filter_name = symbol_impl (NULL, prefix,
						    interface->name,
						    signal->name, "signal");
			if (! filter_name)
				return NULL;

			memcpy (dest, filter_name, strlen (filter_name));
			dest += strlen (filter_name);

			memset (dest, ' ', max_filter - strlen (filter_name));
			dest += max_filter - strlen (filter_name);
		} else {
			memcpy (dest, "NULL", 4);
			dest += 4;

			memset (dest, ' ', max_filter - 4);
			dest += max_filter - 4;
		}

		memcpy (dest, " },\n", 4);
		dest += 4;

		*dest = '\0';

		if (! nih_strcat (&block, NULL, line))
			return NULL;

		var = (TypeVar *)var->entry.next;
	}

	/* Append the final element to the block of elements, indent and
	 * surround with the structure definition.
	 */
	if (! nih_strcat (&block, NULL, "{ NULL }\n"))
		return NULL;

	if (! indent (&block, NULL, 1))
		return NULL;

	code = nih_sprintf (parent,
			    "%s"
			    "const NihDBusSignal %s[] = {\n"
			    "%s"
			    "};\n",
			    args ?: "",
			    name,
			    block);
	if (! code)
		return NULL;

	/* Append the prototype to the list */
	var = type_var_new (code, "const NihDBusSignal", name);
	if (! var) {
		nih_free (code);
		return NULL;
	}

	var->array = TRUE;

	nih_list_add (prototypes, &var->entry);

	return code;
}

/**
 * interface_properties_array:
 * @parent: parent object for new string,
 * @prefix: prefix for array name,
 * @interface: interface to generate array for,
 * @with_handlers: whether to include handler pointers,
 * @prototypes: list to append prototype to.
 *
 * Generates C code to declare an array of NihDBusProperty variables
 * containing information about the properties of the interface @interface.
 *
 * If @with_handlers is TRUE the returned array will contain pointers to
 * getter and setter functions that should be already defined (or at least
 * prototyped); when FALSE these members will be NULL.
 *
 * The prototype of the returned variable declaration is returned as a
 * TypeVar object appended to the @prototypes list.
 *
 * If @parent is not NULL, it should be a pointer to another object which
 * will be used as a parent for the returned string.  When all parents
 * of the returned string are freed, the return string will also be
 * freed.
 *
 * Returns: newly allocated string or NULL if insufficient memory.
 **/
char *
interface_properties_array (const void *parent,
			    const char *prefix,
			    Interface * interface,
			    int         with_handlers,
			    NihList *   prototypes)
{
	nih_local char *name = NULL;
	size_t          max_name = 0;
	size_t          max_type = 0;
	size_t          max_access = 0;
	size_t          max_getter = 0;
	size_t          max_setter = 0;
	nih_local char *block = NULL;
	char *          code;
	TypeVar *       var;

	nih_assert (prefix != NULL);
	nih_assert (interface != NULL);
	nih_assert (prototypes != NULL);

	name = symbol_impl (NULL, prefix, interface->name, NULL, "properties");
	if (! name)
		return NULL;

	/* Figure out the longest property name, type, access variable,
	 * getter and setter function names.
	 */
	NIH_LIST_FOREACH (&interface->properties, iter) {
		Property *      property = (Property *)iter;
		nih_local char *getter_name = NULL;
		nih_local char *setter_name = NULL;

		if (strlen (property->name) > max_name)
			max_name = strlen (property->name);

		if (strlen (property->type) > max_type)
			max_type = strlen (property->type);

		switch (property->access) {
		case NIH_DBUS_READ:
			if (max_access < 13)
				max_access = 13;
			break;
		case NIH_DBUS_WRITE:
			if (max_access < 14)
				max_access = 14;
			break;
		case NIH_DBUS_READWRITE:
			if (max_access < 18)
				max_access = 18;
			break;
		default:
			nih_assert_not_reached ();
		}

		if (with_handlers && (property->access != NIH_DBUS_WRITE)) {
			getter_name = symbol_impl (NULL, prefix,
						   interface->name,
						   property->name, "get");
			if (! getter_name)
				return NULL;

			if (strlen (getter_name) > max_getter)
				max_getter = strlen (getter_name);
		} else {
			if (max_getter < 4)
				max_getter = 4;
		}

		if (with_handlers && (property->access != NIH_DBUS_READ)) {
			setter_name = symbol_impl (NULL, prefix,
						   interface->name,
						   property->name, "set");
			if (! setter_name)
				return NULL;

			if (strlen (setter_name) > max_setter)
				max_setter = strlen (setter_name);
		} else {
			if (max_setter < 4)
				max_setter = 4;
		}
	}

	/* Append each property such that the names, types, access enum,
	 * getter and setter function names are all lined up with each other.
	 */
	NIH_LIST_FOREACH (&interface->properties, iter) {
		Property *      property = (Property *)iter;
		nih_local char *line = NULL;
		char *          dest;
		nih_local char *getter_name = NULL;
		nih_local char *setter_name = NULL;

		line = nih_alloc (NULL, (max_name + max_type + max_access
					 + max_getter + max_setter + 19));
		if (! line)
			return NULL;

		dest = line;

		memcpy (dest, "{ \"", 3);
		dest += 3;

		memcpy (dest, property->name, strlen (property->name));
		dest += strlen (property->name);

		memcpy (dest, "\", ", 3);
		dest += 3;

		memset (dest, ' ', max_name - strlen (property->name));
		dest += max_name - strlen (property->name);


		memcpy (dest, "\"", 1);
		dest += 1;

		memcpy (dest, property->type, strlen (property->type));
		dest += strlen (property->type);

		memcpy (dest, "\", ", 3);
		dest += 3;

		memset (dest, ' ', max_type - strlen (property->type));
		dest += max_type - strlen (property->type);

		switch (property->access) {
		case NIH_DBUS_READ:
			memcpy (dest, "NIH_DBUS_READ, ", 15);
			dest += 15;

			memset (dest, ' ', max_access - 13);
			dest += max_access - 13;
			break;
		case NIH_DBUS_WRITE:
			memcpy (dest, "NIH_DBUS_WRITE, ", 16);
			dest += 16;

			memset (dest, ' ', max_access - 14);
			dest += max_access - 14;
			break;
		case NIH_DBUS_READWRITE:
			memcpy (dest, "NIH_DBUS_READWRITE, ", 20);
			dest += 20;

			memset (dest, ' ', max_access - 18);
			dest += max_access - 18;
			break;
		default:
			nih_assert_not_reached ();
		}

		if (with_handlers && (property->access != NIH_DBUS_WRITE)) {
			getter_name = symbol_impl (NULL, prefix,
						   interface->name,
						   property->name, "get");
			if (! getter_name)
				return NULL;

			memcpy (dest, getter_name, strlen (getter_name));
			dest += strlen (getter_name);

			memcpy (dest, ", ", 2);
			dest += 2;

			memset (dest, ' ', max_getter - strlen (getter_name));
			dest += max_getter - strlen (getter_name);
		} else {
			memcpy (dest, "NULL, ", 6);
			dest += 6;

			memset (dest, ' ', max_getter - 4);
			dest += max_getter - 4;
		}

		if (with_handlers && (property->access != NIH_DBUS_READ)) {
			setter_name = symbol_impl (NULL, prefix,
						   interface->name,
						   property->name, "set");
			if (! setter_name)
				return NULL;

			memcpy (dest, setter_name, strlen (setter_name));
			dest += strlen (setter_name);

			memset (dest, ' ', max_setter - strlen (setter_name));
			dest += max_setter - strlen (setter_name);
		} else {
			memcpy (dest, "NULL", 4);
			dest += 4;

			memset (dest, ' ', max_setter - 4);
			dest += max_setter - 4;
		}

		memcpy (dest, " },\n", 4);
		dest += 4;

		*dest = '\0';

		if (! nih_strcat (&block, NULL, line))
			return NULL;
	}

	/* Append the final element to the block of elements, indent and
	 * surround with the structure definition.
	 */
	if (! nih_strcat (&block, NULL, "{ NULL }\n"))
		return NULL;

	if (! indent (&block, NULL, 1))
		return NULL;

	code = nih_sprintf (parent,
			    "const NihDBusProperty %s[] = {\n"
			    "%s"
			    "};\n",
			    name,
			    block);
	if (! code)
		return NULL;

	/* Append the prototype to the list */
	var = type_var_new (code, "const NihDBusProperty", name);
	if (! var) {
		nih_free (code);
		return NULL;
	}

	var->array = TRUE;

	nih_list_add (prototypes, &var->entry);

	return code;
}


/**
 * interface_struct:
 * @parent: parent object for new string,
 * @prefix: prefix for struct name,
 * @interface: interface to generate struct for,
 * @object: whether struct is for an object or proxy,
 * @prototypes: list to append prototype to.
 *
 * Generates C code to declare an NihDBusInterface structure variable for
 * the given interface @interface, the code includes the array definitions
 * for methods, signals, properties and their arguments.
 *
 * If @object is TRUE, the struct will be for an object definition so method
 * handler function and property getter and setter function pointers will
 * be filled in.  If @object is FALSE, the struct will be for a proxy
 * definition so the signal filter function pointers will be filled in.
 *
 * The prototype of the returned variable declaration is returned as a
 * TypeVar object appended to the @prototypes list.  The methods, signals
 * and properties array prototypes are not returned since they are made
 * static.
 *
 * If @parent is not NULL, it should be a pointer to another object which
 * will be used as a parent for the returned string.  When all parents
 * of the returned string are freed, the return string will also be
 * freed.
 *
 * Returns: newly allocated string or NULL if insufficient memory.
 **/
char *
interface_struct (const void *parent,
		  const char *prefix,
		  Interface * interface,
		  int         object,
		  NihList *   prototypes)
{
	nih_local char *name = NULL;
	nih_local char *block = NULL;
	nih_local char *arrays = NULL;
	TypeVar *       var;
	char *          ptr;
	NihList         methods_prototypes;
	nih_local char *methods_array = NULL;
	NihList         signals_prototypes;
	nih_local char *signals_array = NULL;
	NihList         properties_prototypes;
	nih_local char *properties_array = NULL;
	char *          code;

	nih_assert (prefix != NULL);
	nih_assert (interface != NULL);
	nih_assert (prototypes != NULL);

	/* Work out the structure name, and append the interface name to the
	 * definition.
	 */
	name = symbol_impl (NULL, prefix, interface->name, NULL, NULL);
	if (! name)
		return NULL;

	if (! nih_strcat_sprintf (&block, NULL, "\"%s\",\n", interface->name))
		return NULL;

	/* Append the methods array to the arrays block, making it static
	 * in the process.
	 */
	nih_list_init (&methods_prototypes);

	methods_array = interface_methods_array (NULL, prefix, interface,
						 object ? TRUE : FALSE,
						 &methods_prototypes);
	if (! methods_array)
		return NULL;

	nih_assert (! NIH_LIST_EMPTY (&methods_prototypes));

	var = (TypeVar *)methods_prototypes.next;
	ptr = strstr (methods_array, var->type);
	nih_assert (ptr != NULL);

	if (! nih_strncat (&arrays, NULL, methods_array, ptr - methods_array))
		return NULL;

	if (! nih_strcat_sprintf (&arrays, NULL, "static %s\n", ptr))
		return NULL;

	if (! nih_strcat_sprintf (&block, NULL, "%s,\n", var->name))
		return NULL;

	/* Append the signals array to the arrays block, making it static
	 * in the process.
	 */
	nih_list_init (&signals_prototypes);

	signals_array = interface_signals_array (NULL, prefix, interface,
						 object ? FALSE : TRUE,
						 &signals_prototypes);
	if (! signals_array)
		return NULL;

	nih_assert (! NIH_LIST_EMPTY (&signals_prototypes));

	var = (TypeVar *)signals_prototypes.next;
	ptr = strstr (signals_array, var->type);
	nih_assert (ptr != NULL);

	if (! nih_strncat (&arrays, NULL, signals_array, ptr - signals_array))
		return NULL;

	if (! nih_strcat_sprintf (&arrays, NULL, "static %s\n", ptr))
		return NULL;

	if (! nih_strcat_sprintf (&block, NULL, "%s,\n", var->name))
		return NULL;

	/* Append the properties array to the arrays block, making it static
	 * in the process.
	 */
	nih_list_init (&properties_prototypes);

	properties_array = interface_properties_array (NULL, prefix, interface,
						       object ? TRUE : FALSE,
						       &properties_prototypes);
	if (! properties_array)
		return NULL;

	nih_assert (! NIH_LIST_EMPTY (&properties_prototypes));

	var = (TypeVar *)properties_prototypes.next;

	if (! nih_strcat_sprintf (&arrays, NULL, "static %s\n",
				  properties_array))
		return NULL;

	if (! nih_strcat_sprintf (&block, NULL, "%s\n", var->name))
		return NULL;

	/* Output the code */
	if (! indent (&block, NULL, 1))
		return NULL;

	code = nih_sprintf (parent,
			    "%s"
			    "const NihDBusInterface %s = {\n"
			    "%s"
			    "};\n",
			    arrays,
			    name,
			    block);
	if (! code)
		return NULL;

	/* Append the prototype to the list */
	var = type_var_new (code, "const NihDBusInterface", name);
	if (! var) {
		nih_free (code);
		return NULL;
	}

	nih_list_add (prototypes, &var->entry);

	return code;
}


/**
 * interface_proxy_get_all_function:
 * @parent: parent object for new string.
 * @prefix: prefix for function name,
 * @interface: interface to generate function for,
 * @prototypes: list to append function prototypes to,
 * @structs: list to append structure definitions to.
 *
 * Generates C code for a function that will make an asynchronous method
 * call to obtain the value of all of the properties on @interface,
 * calling a notify function when the method call completes.
 *
 * The prototype of the returned function is returned as a TypeFunc object
 * appended to the @prototypes list.
 *
 * If any of the arguments require a structure to be defined, the
 * definition is returned as a TypeStruct object appended to the @structs
 * list.  The name is generated from @prefix, @interface and the
 * property.
 *
 * The names of both the returned function and notify function prototype
 * will be generated using information in @interface, prefixed with @prefix.
 *
 * The notify function will call a handler function passed in if the
 * reply is valid.  The name and type for this can be obtained from
 * interface_proxy_get_all_notify_function().
 *
 * If @parent is not NULL, it should be a pointer to another object which
 * will be used as a parent for the returned string.  When all parents
 * of the returned string are freed, the return string will also be
 * freed.
 *
 * Returns: newly allocated string or NULL if insufficient memory.
 **/
char *
interface_proxy_get_all_function (const void *parent,
				  const char *prefix,
				  Interface * interface,
				  NihList *   prototypes,
				  NihList *   structs)
{
	NihList             locals;
	NihList             all_structs;
	nih_local char *    name = NULL;
	nih_local TypeFunc *func = NULL;
	TypeVar *           arg;
	NihListEntry *      attrib;
	nih_local char *    assert_block = NULL;
	nih_local char *    handler_type = NULL;
	nih_local TypeVar * message_var = NULL;
	nih_local TypeVar * iter_var = NULL;
	nih_local TypeVar * pending_var = NULL;
	nih_local TypeVar * data_var = NULL;
	nih_local TypeVar * interface_var = NULL;
	nih_local char *    call_block = NULL;
	nih_local char *    notify_name = NULL;
	nih_local char *    block = NULL;
	nih_local char *    vars_block = NULL;
	nih_local char *    body = NULL;
	char *              code = NULL;

	nih_assert (prefix != NULL);
	nih_assert (interface != NULL);
	nih_assert (prototypes != NULL);
	nih_assert (structs != NULL);

	nih_list_init (&locals);
	nih_list_init (&all_structs);

	/* The function returns a pending call, and takes the proxy object
	 * as the only argument.  The pending call also indicates whether
	 * an error occurred, so we want warning if the result isn't used.
	 * We don't have a malloc attribute, since we can't guarantee that
	 * D-Bus doesn't cache them.
	 */
	name = symbol_extern (NULL, prefix, interface->symbol, NULL,
			      "get_all", NULL);
	if (! name)
		return NULL;

	func = type_func_new (NULL, "DBusPendingCall *", name);
	if (! func)
		return NULL;

	attrib = nih_list_entry_new (func);
	if (! attrib)
		return NULL;

	attrib->str = nih_strdup (attrib, "warn_unused_result");
	if (! attrib->str)
		return NULL;

	nih_list_add (&func->attribs, &attrib->entry);

	arg = type_var_new (func, "NihDBusProxy *", "proxy");
	if (! arg)
		return NULL;

	nih_list_add (&func->args, &arg->entry);

	if (! nih_strcat (&assert_block, NULL,
			  "nih_assert (proxy != NULL);\n"))
		return NULL;

	/* We also require a handler (which receives the property value),
	 * error handler (in case of error) and data arguments to pass to
	 * both as well as a timeout for the method call.  Unlike the
	 * method call case, we don't allow for no-reply calls since
	 * they're nonsensical.
	 */
	handler_type = symbol_typedef (NULL, prefix, interface->symbol,
				       NULL, "GetAll", "Reply");
	if (! handler_type)
		return NULL;

	arg = type_var_new (func, handler_type, "handler");
	if (! arg)
		return NULL;

	nih_list_add (&func->args, &arg->entry);

	arg = type_var_new (func, "NihDBusErrorHandler", "error_handler");
	if (! arg)
		return NULL;

	nih_list_add (&func->args, &arg->entry);

	arg = type_var_new (func, "void *", "data");
	if (! arg)
		return NULL;

	nih_list_add (&func->args, &arg->entry);

	if (! nih_strcat (&assert_block, NULL,
			  "nih_assert ((handler != NULL) && (error_handler != NULL));\n"))
		return NULL;

	arg = type_var_new (func, "int", "timeout");
	if (! arg)
		return NULL;

	nih_list_add (&func->args, &arg->entry);


	/* The function requires a message pointer, which we allocate,
	 * and an iterator for it to append the arguments.  We also need
	 * a return pending call pointer and data structure as well.
	 * Rather than deal with these by hand, it's far easier to put them
	 * on the locals list and deal with them along with the rest.
	 */
	message_var = type_var_new (NULL, "DBusMessage *", "method_call");
	if (! message_var)
		return NULL;

	nih_list_add (&locals, &message_var->entry);

	iter_var = type_var_new (NULL, "DBusMessageIter", "iter");
	if (! iter_var)
		return NULL;

	nih_list_add (&locals, &iter_var->entry);

	pending_var = type_var_new (NULL, "DBusPendingCall *", "pending_call");
	if (! pending_var)
		return NULL;

	nih_list_add (&locals, &pending_var->entry);

	data_var = type_var_new (NULL, "NihDBusPendingData *", "pending_data");
	if (! data_var)
		return NULL;

	nih_list_add (&locals, &data_var->entry);

	/* Annoyingly we also need a variable for the interface name,
	 * since D-Bus wants its address and can't just take a constant
	 * string.
	 */
	interface_var = type_var_new (NULL, "const char *", "interface");
	if (! interface_var)
		return NULL;

	nih_list_add (&locals, &interface_var->entry);


	/* Create the method call to get the property, the property
	 * interface gets specified as an argument - the method call
	 * interface is the D-Bus properties one.
	 */
	if (! nih_strcat_sprintf (&call_block, NULL,
				  "/* Construct the method call message. */\n"
				  "method_call = dbus_message_new_method_call (proxy->name, proxy->path, \"%s\", \"GetAll\");\n"
				  "if (! method_call)\n"
				  "\tnih_return_no_memory_error (NULL);\n"
				  "\n"
				  "dbus_message_set_auto_start (method_call, proxy->auto_start);\n"
				  "\n"
				  "dbus_message_iter_init_append (method_call, &iter);\n"
				  "\n"
				  "interface = \"%s\";\n"
				  "if (! dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &interface)) {\n"
				  "\tdbus_message_unref (method_call);\n"
				  "\tnih_return_no_memory_error (NULL);\n"
				  "}\n"
				  "\n",
				  DBUS_INTERFACE_PROPERTIES,
				  interface->name))
		return NULL;

	/* FIXME autostart? */

	/* Complete the marshalling block by sending the message and checking
	 * for error replies.
	 */
	notify_name = symbol_impl (NULL, prefix, interface->name,
				   "get_all", "notify");
	if (! notify_name)
		return NULL;

	if (! nih_strcat_sprintf (&call_block, NULL,
				  "/* Send the message and set up the reply notification. */\n"
				  "pending_data = nih_dbus_pending_data_new (NULL, proxy->connection,\n"
				  "                                          (NihDBusReplyHandler)handler,\n"
				  "                                          error_handler, data);\n"
				  "if (! pending_data) {\n"
				  "\tdbus_message_unref (method_call);\n"
				  "\tnih_return_no_memory_error (NULL);\n"
				  "}\n"
				  "\n"
				  "pending_call = NULL;\n"
				  "if (! dbus_connection_send_with_reply (proxy->connection, method_call,\n"
				  "                                       &pending_call, timeout)) {\n"
				  "\tdbus_message_unref (method_call);\n"
				  "\tnih_free (pending_data);\n"
				  "\tnih_return_no_memory_error (NULL);\n"
				  "}\n"
				  "\n"
				  "dbus_message_unref (method_call);\n"
				  "\n"
				  "if (! pending_call) {\n"
				  "\tnih_dbus_error_raise (DBUS_ERROR_DISCONNECTED,\n"
				  "\t                      \"Connection is closed\");\n"
				  "\tnih_free (pending_data);\n"
				  "\treturn NULL;\n"
				  "}\n"
				  "\n"
				  "NIH_MUST (dbus_pending_call_set_notify (pending_call, (DBusPendingCallNotifyFunction)%s,\n"
				  "                                        pending_data, (DBusFreeFunction)nih_discard));\n",
				  notify_name))
		return NULL;

	/* Lay out the function body, indenting it all before placing it
	 * in the function code.
	 */
	vars_block = type_var_layout (NULL, &locals);
	if (! vars_block)
		return NULL;

	if (! nih_strcat_sprintf (&body, NULL,
				  "%s"
				  "\n"
				  "%s"
				  "\n"
				  "%s"
				  "\n"
				  "return pending_call;\n",
				  vars_block,
				  assert_block,
				  call_block))
		return NULL;

	if (! indent (&body, NULL, 1))
		return NULL;

	/* Function header */
	code = type_func_to_string (parent, func);
	if (! code)
		return NULL;

	if (! nih_strcat_sprintf (&code, parent,
				  "{\n"
				  "%s"
				  "}\n",
				  body)) {
		nih_free (code);
		return NULL;
	}

	/* Append the function to the prototypes list */
	nih_list_add (prototypes, &func->entry);
	nih_ref (func, code);

	return code;
}

/**
 * interface_proxy_get_all_notify_function:
 * @parent: parent object for new string.
 * @prefix: prefix for function name,
 * @interface: interface to generate function for,
 * @prototypes: list to append function prototypes to,
 * @typedefs: list to append function pointer typedef definitions to,
 * @structs: list to append structure definitions to.
 *
 * Generates C code for a function to handle the notification of
 * a complete pending call to obtain the value of all of the properties
 * of @interface by calling either the handler function on success or
 * error function on failure.
 *
 * The notify function will call a handler function passed in if the
 * reply is valid, the typedef name for this handler must be passed as
 * @handler_type.  The actual type for this can be obtained from the
 * entry added to @typedefs.
 *
 * The prototype of the returned function is returned as a TypeFunc object
 * appended to the @prototypes list.
 *
 * The typedef for the handler function is returned as a TypeFunc object
 * added to the @typedefs list.
 *
 * If any of the arguments require a structure to be defined, the
 * definition is returned as a TypeStruct object appended to the @structs
 * list.  The name is generated from @prefix, @interface and the
 * property.
 *
 * If @parent is not NULL, it should be a pointer to another object which
 * will be used as a parent for the returned string.  When all parents
 * of the returned string are freed, the return string will also be
 * freed.
 *
 * Returns: newly allocated string or NULL if insufficient memory.
 **/
char *
interface_proxy_get_all_notify_function (const void *parent,
					 const char *prefix,
					 Interface * interface,
					 NihList *   prototypes,
					 NihList *   typedefs,
					 NihList *   structs)
{
	nih_local char *      c_type = NULL;
	nih_local TypeStruct *structure = NULL;
	NihList               locals;
	NihList               all_structs;
	nih_local char *      name = NULL;
	nih_local TypeFunc *  func = NULL;
	TypeVar *             arg;
	nih_local char *      assert_block = NULL;
	nih_local TypeVar *   reply_var = NULL;
	nih_local TypeVar *   iter_var = NULL;
	nih_local TypeVar *   arrayiter_var = NULL;
	nih_local TypeVar *   dictiter_var = NULL;
	nih_local TypeVar *   variter_var = NULL;
	nih_local TypeVar *   error_var = NULL;
	nih_local TypeVar *   property_var = NULL;
	nih_local TypeVar *   parent_var = NULL;
	nih_local TypeVar *   structure_var = NULL;
	nih_local TypeVar *   count_var = NULL;
	nih_local char *      steal_block = NULL;
	nih_local char *      demarshal_block = NULL;
	nih_local char *      property_block = NULL;
	size_t                property_count;
	nih_local char *      oom_error_code = NULL;
	nih_local char *      type_error_code = NULL;
	nih_local char *      call_block = NULL;
	nih_local char *      handler_type = NULL;
	nih_local char *      handler_name = NULL;
	nih_local TypeFunc *  handler_func = NULL;
	nih_local char *      vars_block = NULL;
	nih_local char *      body = NULL;
	char *                code = NULL;

	nih_assert (prefix != NULL);
	nih_assert (interface != NULL);
	nih_assert (prototypes != NULL);
	nih_assert (typedefs != NULL);
	nih_assert (structs != NULL);

	nih_list_init (&locals);
	nih_list_init (&all_structs);

	/* The function takes the pending call being notified and the
	 * associated data structure.  We don't mark the function deprecated
	 * since it's used internally, it's enough to mark the method
	 * call function deprecated.
	 */
	name = symbol_impl (NULL, prefix, interface->name,
			    "get_all", "notify");
	if (! name)
		return NULL;

	func = type_func_new (NULL, "void", name);
	if (! func)
		return NULL;

	arg = type_var_new (func, "DBusPendingCall *", "pending_call");
	if (! arg)
		return NULL;

	nih_list_add (&func->args, &arg->entry);

	if (! nih_strcat (&assert_block, NULL,
			  "nih_assert (pending_call != NULL);\n"))
		return NULL;

	arg = type_var_new (func, "NihDBusPendingData *", "pending_data");
	if (! arg)
		return NULL;

	nih_list_add (&func->args, &arg->entry);

	if (! nih_strcat (&assert_block, NULL,
			  "nih_assert (pending_data != NULL);\n"))
		return NULL;


	/* The function requires a message pointer, stolen from the
	 * pending call and iterators for the message and variant.  We
	 * also need a parent message context for any allocations we make,
	 * as well as an error object.
	 */
	reply_var = type_var_new (NULL, "DBusMessage *", "reply");
	if (! reply_var)
		return NULL;

	nih_list_add (&locals, &reply_var->entry);

	iter_var = type_var_new (NULL, "DBusMessageIter", "iter");
	if (! iter_var)
		return NULL;

	nih_list_add (&locals, &iter_var->entry);

	arrayiter_var = type_var_new (NULL, "DBusMessageIter", "arrayiter");
	if (! arrayiter_var)
		return NULL;

	nih_list_add (&locals, &arrayiter_var->entry);

	dictiter_var = type_var_new (NULL, "DBusMessageIter", "dictiter");
	if (! dictiter_var)
		return NULL;

	nih_list_add (&locals, &dictiter_var->entry);

	variter_var = type_var_new (NULL, "DBusMessageIter", "variter");
	if (! variter_var)
		return NULL;

	nih_list_add (&locals, &variter_var->entry);

	parent_var = type_var_new (NULL, "NihDBusMessage *", "message");
	if (! parent_var)
		return NULL;

	nih_list_add (&locals, &parent_var->entry);

	error_var = type_var_new (NULL, "DBusError", "error");
	if (! error_var)
		return NULL;

	nih_list_add (&locals, &error_var->entry);

	/* Need a variable to hold the property name */
	property_var = type_var_new (NULL, "const char *", "property");
	if (! property_var)
		return NULL;

	nih_list_add (&locals, &property_var->entry);

	/* We also need a local for the structure we pass to the handler */
	c_type = symbol_typedef (NULL, prefix, interface->symbol, NULL,
				 "properties", NULL);
	if (! c_type)
		return NULL;

	structure = type_struct_new (NULL, c_type);
	if (! structure)
		return NULL;

	if (! type_to_pointer (&c_type, NULL))
		return NULL;

	structure_var = type_var_new (NULL, c_type, "properties");
	if (! structure_var)
		return NULL;

	nih_list_add (&locals, &structure_var->entry);

	count_var = type_var_new (NULL, "size_t", "property_count");
	if (! count_var)
		return NULL;

	nih_list_add (&locals, &count_var->entry);


	/* Assert that the pending call is, in fact, complete then
	 * steal the message from it; handling it immediately if it's an
	 * error.
	 */
	if (! nih_strcat (&steal_block, NULL,
			  "nih_assert (dbus_pending_call_get_completed (pending_call));\n"
			  "\n"
			  "/* Steal the reply from the pending call. */\n"
			  "reply = dbus_pending_call_steal_reply (pending_call);\n"
			  "nih_assert (reply != NULL);\n"
			  "\n"
			  "/* Handle error replies */\n"
			  "if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR) {\n"
			  "\tmessage = NIH_MUST (nih_dbus_message_new (pending_data, pending_data->connection, reply));\n"
			  "\n"
			  "\tdbus_error_init (&error);\n"
			  "\tdbus_set_error_from_message (&error, message->message);\n"
			  "\n"
			  "\tnih_error_push_context ();\n"
			  "\tnih_dbus_error_raise (error.name, error.message);\n"
			  "\tpending_data->error_handler (pending_data->data, message);\n"
			  "\tnih_error_pop_context ();\n"
			  "\n"
			  "\tdbus_error_free (&error);\n"
			  "\tnih_free (message);\n"
			  "\tdbus_message_unref (reply);\n"
			  "\treturn;\n"
			  "}\n"
			  "\n"
			  "nih_assert (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_METHOD_RETURN);\n"
			  "\n"))
		return NULL;

	/* Begin the demarshalling block, making sure that the first
	 * argument is an array and recursing into it and looping over
	 * the values.
	 */
	if (! nih_strcat_sprintf (&demarshal_block, NULL,
				  "/* Create a message context for the reply, and iterate\n"
				  " * over and recurse into the arguments.\n"
				  " */\n"
				  "message = NIH_MUST (nih_dbus_message_new (pending_data, pending_data->connection, reply));\n"
				  "\n"
				  "/* Iterate the method arguments, recursing into the array */\n"
				  "dbus_message_iter_init (reply, &iter);\n"
				  "\n"
				  "if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY) {\n"
				  "\tnih_error_push_context ();\n"
				  "\tnih_error_raise (NIH_DBUS_INVALID_ARGS,\n"
				  "\t                 _(NIH_DBUS_INVALID_ARGS_STR));\n"
				  "\tpending_data->error_handler (pending_data->data, message);\n"
				  "\tnih_error_pop_context ();\n"
				  "\n"
				  "\tnih_free (message);\n"
				  "\tdbus_message_unref (reply);\n"
				  "\treturn;\n"
				  "}\n"
				  "\n"
				  "properties = NIH_MUST (nih_new (message, %s));\n"
				  "property_count = 0;\n"
				  "\n"
				  "dbus_message_iter_recurse (&iter, &arrayiter);\n"
				  "\n"
				  "while (dbus_message_iter_get_arg_type (&arrayiter) != DBUS_TYPE_INVALID) {\n"
				  "\t__label__ enomem;\n"
				  "\n",
				  structure->name))
		return NULL;

	/* Begin the property checking block, each array item should be
	 * a dictionary entry which we recurse into; the first value of
	 * which should be a string containing the property name.
	 */
	if (! nih_strcat_sprintf (&property_block, NULL,
				  "if (dbus_message_iter_get_arg_type (&arrayiter) != DBUS_TYPE_DICT_ENTRY) {\n"
				  "\tnih_error_push_context ();\n"
				  "\tnih_error_raise (NIH_DBUS_INVALID_ARGS,\n"
				  "\t                 _(NIH_DBUS_INVALID_ARGS_STR));\n"
				  "\tpending_data->error_handler (pending_data->data, message);\n"
				  "\tnih_error_pop_context ();\n"
				  "\n"
				  "\tnih_free (message);\n"
				  "\tdbus_message_unref (reply);\n"
				  "\treturn;\n"
				  "}\n"
				  "\n"
				  "dbus_message_iter_recurse (&arrayiter, &dictiter);\n"
				  "\n"
				  "if (dbus_message_iter_get_arg_type (&dictiter) != DBUS_TYPE_STRING) {\n"
				  "\tnih_error_push_context ();\n"
				  "\tnih_error_raise (NIH_DBUS_INVALID_ARGS,\n"
				  "\t                 _(NIH_DBUS_INVALID_ARGS_STR));\n"
				  "\tpending_data->error_handler (pending_data->data, message);\n"
				  "\tnih_error_pop_context ();\n"
				  "\n"
				  "\tnih_free (message);\n"
				  "\tdbus_message_unref (reply);\n"
				  "\treturn;\n"
				  "}\n"
				  "\n"
				  "dbus_message_iter_get_basic (&dictiter, &property);\n"
				  "\n"
				  "dbus_message_iter_next (&dictiter);\n"
				  "\n"
				  "if (dbus_message_iter_get_arg_type (&dictiter) != DBUS_TYPE_VARIANT) {\n"
				  "\tnih_error_push_context ();\n"
				  "\tnih_error_raise (NIH_DBUS_INVALID_ARGS,\n"
				  "\t                 _(NIH_DBUS_INVALID_ARGS_STR));\n"
				  "\tpending_data->error_handler (pending_data->data, message);\n"
				  "\tnih_error_pop_context ();\n"
				  "\n"
				  "\tnih_free (message);\n"
				  "\tdbus_message_unref (reply);\n"
				  "\treturn;\n"
				  "}\n"
				  "\n"
				  "dbus_message_iter_recurse (&dictiter, &variter);\n"))
		return NULL;

	/* In case of out of memory, we can't just return because we've
	 * already made the method call so we loop over the code instead.
	 * But in case of type error in the returned arguments, all we
	 * can do is return an error.
	 */
	oom_error_code = nih_strdup (NULL, "goto enomem;\n");
	if (! oom_error_code)
		return NULL;

	type_error_code = nih_strdup (NULL,
				      "nih_error_push_context ();\n"
				      "nih_error_raise (NIH_DBUS_INVALID_ARGS,\n"
				      "                 _(NIH_DBUS_INVALID_ARGS_STR));\n"
				      "pending_data->error_handler (pending_data->data, message);\n"
				      "nih_error_pop_context ();\n"
				      "\n"
				      "nih_free (message);\n"
				      "dbus_message_unref (reply);\n"
				      "return;\n");
	if (! type_error_code)
		return NULL;

	property_count = 0;
	NIH_LIST_FOREACH (&interface->properties, iter) {
		Property *        property = (Property *)iter;
		DBusSignatureIter iter;
		NihList           property_outputs;
		NihList           property_locals;
		NihList           property_structs;
		nih_local char *  block = NULL;

		if (property->access == NIH_DBUS_WRITE)
			continue;

		dbus_signature_iter_init (&iter, property->type);

		nih_list_init (&property_outputs);
		nih_list_init (&property_locals);
		nih_list_init (&property_structs);

		block = demarshal (NULL, &iter, "properties", "variter",
				   property->symbol,
				   oom_error_code,
				   type_error_code,
				   &property_outputs, &property_locals,
				   prefix, interface->symbol,
				   property->symbol, NULL,
				   &property_structs);
		if (! block)
			return NULL;

		if (! nih_strcat (&block, NULL, "\n"))
			return NULL;

		/* Each of the outputs from the demarshalling code becomes a
		 * local variable to our function that we store the value in,
		 * and that we copy into the structure.
		 */
		NIH_LIST_FOREACH_SAFE (&property_outputs, iter) {
			TypeVar *var = (TypeVar *)iter;
			TypeVar *member_var;

			/* Create the structure member entry */
			member_var = type_var_new (structure,
						   var->type, var->name);
			if (! member_var)
				return NULL;

			nih_list_add (&structure->members, &member_var->entry);

			/* Add code to copy from local variable */
			if (! nih_strcat_sprintf (&block, NULL,
						  "properties->%s = %s;\n",
						  var->name, var->name))
				return NULL;

			/* Add to locals */
			nih_list_add (&locals, &var->entry);
			nih_ref (var, demarshal_block);
		}

		NIH_LIST_FOREACH_SAFE (&property_locals, iter) {
			TypeVar *var = (TypeVar *)iter;

			nih_list_add (&locals, &var->entry);
			nih_ref (var, demarshal_block);
		}

		NIH_LIST_FOREACH_SAFE (&property_structs, iter) {
			TypeStruct *structure = (TypeStruct *)iter;

			nih_list_add (&all_structs, &structure->entry);
			nih_ref (structure, demarshal_block);
		}

		if (! nih_strcat (&block, NULL,
				  "\n"
				  "nih_assert (++property_count);\n"))
			return NULL;

		/* Wrap the code in a test for the property by name */
		if (! indent (&block, NULL, 1))
			return NULL;

		if (! nih_strcat_sprintf (&property_block, NULL,
					  "\n"
					  "if (! strcmp (property, \"%s\")) {\n"
					  "%s"
					  "}\n",
					  property->name,
					  block))
			return NULL;

		property_count++;
	}

	/* Finish the property checking block, making sure that there are
	 * no further items in the dictionary entry before moving on to the
	 * next array item.
	 */
	if (! nih_strcat_sprintf (&property_block, NULL,
				  "\n"
				  "dbus_message_iter_next (&dictiter);\n"
				  "\n"
				  "if (dbus_message_iter_get_arg_type (&dictiter) != DBUS_TYPE_INVALID) {\n"
				  "\tnih_error_push_context ();\n"
				  "\tnih_error_raise (NIH_DBUS_INVALID_ARGS,\n"
				  "\t                 _(NIH_DBUS_INVALID_ARGS_STR));\n"
				  "\tpending_data->error_handler (pending_data->data, message);\n"
				  "\tnih_error_pop_context ();\n"
				  "\n"
				  "\tnih_free (message);\n"
				  "\tdbus_message_unref (reply);\n"
				  "\treturn;\n"
				  "}\n"
				  "\n"
				  "dbus_message_iter_next (&arrayiter);\n"))
		return NULL;

	/* Indent the property block and append to the demarshal block */
	if (! indent (&property_block, NULL, 1))
		return NULL;

	if (! nih_strcat (&demarshal_block, NULL, property_block))
		return NULL;

	/* Finish off the demarshalling block by ending the loop and
	 * making sure that there are no further arguments in the reply.
	 */
	if (! nih_strcat_sprintf (&demarshal_block, NULL,
				  "enomem: __attribute__ ((unused));\n"
				  "}\n"
				  "\n"
				  "dbus_message_iter_next (&iter);\n"
				  "\n"
				  "if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INVALID) {\n"
				  "\tnih_error_push_context ();\n"
				  "\tnih_error_raise (NIH_DBUS_INVALID_ARGS,\n"
				  "\t                 _(NIH_DBUS_INVALID_ARGS_STR));\n"
				  "\tpending_data->error_handler (pending_data->data, message);\n"
				  "\tnih_error_pop_context ();\n"
				  "\n"
				  "\tnih_free (message);\n"
				  "\tdbus_message_unref (reply);\n"
				  "\treturn;\n"
				  "}\n"
				  "\n"
				  "if (property_count < %zu) {\n"
				  "\tnih_error_push_context ();\n"
				  "\tnih_error_raise (NIH_DBUS_INVALID_ARGS,\n"
				  "\t                 _(NIH_DBUS_INVALID_ARGS_STR));\n"
				  "\tpending_data->error_handler (pending_data->data, message);\n"
				  "\tnih_error_pop_context ();\n"
				  "\n"
				  "\tnih_free (message);\n"
				  "\tdbus_message_unref (reply);\n"
				  "\treturn;\n"
				  "}\n",
				  property_count))
		return NULL;

	/* Create the handler calling block, the handler is not permitted
	 * to reply.
	 */
	handler_type = symbol_typedef (NULL, prefix, interface->symbol, NULL,
				       "GetAll", "Reply");
	if (! handler_type)
		return NULL;

	if (! nih_strcat_sprintf (&call_block, NULL,
				  "/* Call the handler function */\n"
				  "nih_error_push_context ();\n"
				  "((%s)pending_data->handler) (pending_data->data, message, properties);\n"
				  "nih_error_pop_context ();\n"
				  "\n"
				  "nih_free (message);\n"
				  "dbus_message_unref (reply);\n",
				  handler_type))
		return NULL;

	handler_name = nih_sprintf (NULL, "(*%s)", handler_type);
	if (! handler_name)
		return NULL;

	handler_func = type_func_new (NULL, "typedef void", handler_name);
	if (! handler_func)
		return NULL;

	arg = type_var_new (handler_func, "void *", "data");
	if (! arg)
		return NULL;

	nih_list_add (&handler_func->args, &arg->entry);

	arg = type_var_new (handler_func, "NihDBusMessage *", "message");
	if (! arg)
		return NULL;

	nih_list_add (&handler_func->args, &arg->entry);

	arg = type_var_new (handler_func, c_type, "properties");
	if (! arg)
		return NULL;

	if (! type_to_const (&arg->type, arg))
		return NULL;

	nih_list_add (&handler_func->args, &arg->entry);

	/* Lay out the function body, indenting it all before placing it
	 * in the function code.
	 */
	vars_block = type_var_layout (NULL, &locals);
	if (! vars_block)
		return NULL;

	if (! nih_strcat_sprintf (&body, NULL,
				  "%s"
				  "\n"
				  "%s"
				  "\n"
				  "%s"
				  "%s"
				  "\n"
				  "%s",
				  vars_block,
				  assert_block,
				  steal_block,
				  demarshal_block,
				  call_block))
		return NULL;

	if (! indent (&body, NULL, 1))
		return NULL;

	/* Function header */
	code = type_func_to_string (parent, func);
	if (! code)
		return NULL;

	if (! nih_strcat_sprintf (&code, parent,
				  "{\n"
				  "%s"
				  "}\n",
				  body)) {
		nih_free (code);
		return NULL;
	}

	/* Append the functions to the prototypes and typedefs list */
	nih_list_add (prototypes, &func->entry);
	nih_ref (func, code);

	nih_list_add (typedefs, &handler_func->entry);
	nih_ref (handler_func, code);

	NIH_LIST_FOREACH_SAFE (&all_structs, iter) {
		TypeStruct *structure = (TypeStruct *)iter;

		nih_ref (structure, code);
		nih_list_add (structs, &structure->entry);
	}

	nih_ref (structure, code);
	nih_list_add (structs, &structure->entry);

	return code;
}


/**
 * interface_proxy_get_all_sync_function:
 * @parent: parent object for new string.
 * @prefix: prefix for function name,
 * @interface: interface to generate function for,
 * @prototypes: list to append function prototypes to,
 * @structs: list to append structure definitions to.
 *
 * Generates C code for a function that will make a synchronous method
 * call to obtain the value of all of the properties of @interface.
 *
 * The prototype of the returned function is returned as a TypeFunc object
 * appended to the @prototypes list, with the name as @name itself.
 *
 * If any of the arguments require a structure to be defined, the
 * definition is returned as a TypeStruct object appended to the @structs
 * list.  The name is generated from @prefix, @interface and the
 * property.
 *
 * If @parent is not NULL, it should be a pointer to another object which
 * will be used as a parent for the returned string.  When all parents
 * of the returned string are freed, the return string will also be
 * freed.
 *
 * Returns: newly allocated string or NULL if insufficient memory.
 **/
char *
interface_proxy_get_all_sync_function (const void *parent,
				       const char *prefix,
				       Interface * interface,
				       NihList *   prototypes,
				       NihList *   structs)
{
	nih_local char *      c_type = NULL;
	nih_local TypeStruct *structure = NULL;
	NihList               locals;
	NihList               all_structs;
	nih_local char *      name = NULL;
	nih_local TypeFunc *  func = NULL;
	TypeVar *             arg;
	NihListEntry *        attrib;
	nih_local char *      assert_block = NULL;
	nih_local TypeVar *   message_var = NULL;
	nih_local TypeVar *   iter_var = NULL;
	nih_local TypeVar *   arrayiter_var = NULL;
	nih_local TypeVar *   dictiter_var = NULL;
	nih_local TypeVar *   variter_var = NULL;
	nih_local TypeVar *   error_var = NULL;
	nih_local TypeVar *   reply_var = NULL;
	nih_local TypeVar *   count_var = NULL;
	nih_local TypeVar *   interface_var = NULL;
	nih_local TypeVar *   property_var = NULL;
	nih_local char *      call_block = NULL;
	nih_local char *      demarshal_block = NULL;
	nih_local char *      property_block = NULL;
	nih_local char *      oom_error_code = NULL;
	nih_local char *      type_error_code = NULL;
	size_t                property_count;
	nih_local char *      vars_block = NULL;
	nih_local char *      body = NULL;
	char *                code = NULL;

	nih_assert (prefix != NULL);
	nih_assert (interface != NULL);
	nih_assert (prototypes != NULL);
	nih_assert (structs != NULL);

	nih_list_init (&locals);
	nih_list_init (&all_structs);

	/* The function returns an integer, and takes a parent object and
	 * the proxy object as the argument along with an output structure
	 * argument for the property values.  The integer is negative if a
	 * raised error occurred, so we want warning if the result isn't
	 * used.
	 */
	name = symbol_extern (NULL, prefix, interface->symbol, NULL,
			      "get_all", "sync");
	if (! name)
		return NULL;

	func = type_func_new (NULL, "int", name);
	if (! func)
		return NULL;

	attrib = nih_list_entry_new (func);
	if (! attrib)
		return NULL;

	attrib->str = nih_strdup (attrib, "warn_unused_result");
	if (! attrib->str)
		return NULL;

	nih_list_add (&func->attribs, &attrib->entry);

	arg = type_var_new (func, "const void *", "parent");
	if (! arg)
		return NULL;

	nih_list_add (&func->args, &arg->entry);

	arg = type_var_new (func, "NihDBusProxy *", "proxy");
	if (! arg)
		return NULL;

	nih_list_add (&func->args, &arg->entry);

	if (! nih_strcat (&assert_block, NULL,
			  "nih_assert (proxy != NULL);\n"))
		return NULL;


	c_type = symbol_typedef (NULL, prefix, interface->symbol, NULL,
				 "properties", NULL);
	if (! c_type)
		return NULL;

	structure = type_struct_new (NULL, c_type);
	if (! structure)
		return NULL;

	if (! type_to_pointer (&c_type, NULL))
		return NULL;

	if (! type_to_pointer (&c_type, NULL))
		return NULL;

	arg = type_var_new (func, c_type, "properties");
	if (! arg)
		return NULL;

	nih_list_add (&func->args, &arg->entry);

	if (! nih_strcat (&assert_block, NULL,
			  "nih_assert (properties != NULL);\n"))
		return NULL;


	/* The function requires a message pointer, which we allocate,
	 * and an iterator for it to append the arguments.  We also need
	 * a reply message pointer as well and an error object.
	 * Rather than deal with these by hand, it's far easier to put them
	 * on the locals list and deal with them along with the rest.
	 */
	message_var = type_var_new (NULL, "DBusMessage *", "method_call");
	if (! message_var)
		return NULL;

	nih_list_add (&locals, &message_var->entry);

	iter_var = type_var_new (NULL, "DBusMessageIter", "iter");
	if (! iter_var)
		return NULL;

	nih_list_add (&locals, &iter_var->entry);

	arrayiter_var = type_var_new (NULL, "DBusMessageIter", "arrayiter");
	if (! arrayiter_var)
		return NULL;

	nih_list_add (&locals, &arrayiter_var->entry);

	dictiter_var = type_var_new (NULL, "DBusMessageIter", "dictiter");
	if (! dictiter_var)
		return NULL;

	nih_list_add (&locals, &dictiter_var->entry);

	variter_var = type_var_new (NULL, "DBusMessageIter", "variter");
	if (! variter_var)
		return NULL;

	nih_list_add (&locals, &variter_var->entry);

	error_var = type_var_new (NULL, "DBusError", "error");
	if (! error_var)
		return NULL;

	nih_list_add (&locals, &error_var->entry);

	reply_var = type_var_new (NULL, "DBusMessage *", "reply");
	if (! reply_var)
		return NULL;

	nih_list_add (&locals, &reply_var->entry);

	count_var = type_var_new (NULL, "size_t", "property_count");
	if (! count_var)
		return NULL;

	nih_list_add (&locals, &count_var->entry);

	/* Annoyingly we need a variable for the interface name, since
	 * D-Bus wants its address and can't just take a constant string.
	 * We also need a variable in which to store the property names
	 * we find.
	 */
	interface_var = type_var_new (NULL, "const char *", "interface");
	if (! interface_var)
		return NULL;

	nih_list_add (&locals, &interface_var->entry);

	property_var = type_var_new (NULL, "const char *", "property");
	if (! property_var)
		return NULL;

	nih_list_add (&locals, &property_var->entry);


	/* Create the method call to get the properties, the property
	 * interface gets specified as an argument - the method call
	 * interface is the D-Bus properties one.
	 */
	if (! nih_strcat_sprintf (&call_block, NULL,
				  "/* Construct the method call message. */\n"
				  "method_call = dbus_message_new_method_call (proxy->name, proxy->path, \"%s\", \"GetAll\");\n"
				  "if (! method_call)\n"
				  "\tnih_return_no_memory_error (-1);\n"
				  "\n"
				  "dbus_message_set_auto_start (method_call, proxy->auto_start);\n"
				  "\n"
				  "dbus_message_iter_init_append (method_call, &iter);\n"
				  "\n"
				  "interface = \"%s\";\n"
				  "if (! dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &interface)) {\n"
				  "\tdbus_message_unref (method_call);\n"
				  "\tnih_return_no_memory_error (-1);\n"
				  "}\n"
				  "\n",
				  DBUS_INTERFACE_PROPERTIES,
				  interface->name))
		return NULL;

	/* FIXME autostart? */

	/* Complete the marshalling block by sending the message and checking
	 * for error replies.
	 */
	if (! nih_strcat_sprintf (&call_block, NULL,
				  "/* Send the message, and wait for the reply. */\n"
				  "dbus_error_init (&error);\n"
				  "\n"
				  "reply = dbus_connection_send_with_reply_and_block (proxy->connection, method_call, -1, &error);\n"
				  "if (! reply) {\n"
				  "\tdbus_message_unref (method_call);\n"
				  "\n"
				  "\tif (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY)) {\n"
				  "\t\tnih_error_raise_no_memory ();\n"
				  "\t} else {\n"
				  "\t\tnih_dbus_error_raise (error.name, error.message);\n"
				  "\t}\n"
				  "\n"
				  "\tdbus_error_free (&error);\n"
				  "\treturn -1;\n"
				  "}\n"
				  "\n"))
		return NULL;

	/* Begin the demarshalling block, making sure that the first
	 * argument is an array and recursing into it and looping over
	 * the values.
	 */
	if (! nih_strcat_sprintf (&demarshal_block, NULL,
				  "dbus_message_unref (method_call);\n"
				  "\n"
				  "/* Iterate the method arguments, recursing into the array */\n"
				  "dbus_message_iter_init (reply, &iter);\n"
				  "\n"
				  "if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_ARRAY) {\n"
				  "\tdbus_message_unref (reply);\n"
				  "\tnih_return_error (-1, NIH_DBUS_INVALID_ARGS,\n"
				  "\t                  _(NIH_DBUS_INVALID_ARGS_STR));\n"
				  "}\n"
				  "\n"
				  "*properties = NIH_MUST (nih_new (parent, %s));\n"
				  "property_count = 0;\n"
				  "\n"
				  "dbus_message_iter_recurse (&iter, &arrayiter);\n"
				  "\n"
				  "while (dbus_message_iter_get_arg_type (&arrayiter) != DBUS_TYPE_INVALID) {\n"
				  "\t__label__ enomem;\n"
				  "\n",
				  structure->name))
		return NULL;

	/* Begin the property checking block, each array item should be
	 * a dictionary entry which we recurse into; the first value of
	 * which should be a string containing the property name.
	 */
	if (! nih_strcat_sprintf (&property_block, NULL,
				  "if (dbus_message_iter_get_arg_type (&arrayiter) != DBUS_TYPE_DICT_ENTRY) {\n"
				  "\tnih_free (*properties);\n"
				  "\t*properties = NULL;\n"
				  "\tdbus_message_unref (reply);\n"
				  "\tnih_return_error (-1, NIH_DBUS_INVALID_ARGS,\n"
				  "\t                  _(NIH_DBUS_INVALID_ARGS_STR));\n"
				  "}\n"
				  "\n"
				  "dbus_message_iter_recurse (&arrayiter, &dictiter);\n"
				  "\n"
				  "if (dbus_message_iter_get_arg_type (&dictiter) != DBUS_TYPE_STRING) {\n"
				  "\tnih_free (*properties);\n"
				  "\t*properties = NULL;\n"
				  "\tdbus_message_unref (reply);\n"
				  "\tnih_return_error (-1, NIH_DBUS_INVALID_ARGS,\n"
				  "\t                  _(NIH_DBUS_INVALID_ARGS_STR));\n"
				  "}\n"
				  "\n"
				  "dbus_message_iter_get_basic (&dictiter, &property);\n"
				  "\n"
				  "dbus_message_iter_next (&dictiter);\n"
				  "\n"
				  "if (dbus_message_iter_get_arg_type (&dictiter) != DBUS_TYPE_VARIANT) {\n"
				  "\tnih_free (*properties);\n"
				  "\t*properties = NULL;\n"
				  "\tdbus_message_unref (reply);\n"
				  "\tnih_return_error (-1, NIH_DBUS_INVALID_ARGS,\n"
				  "\t                  _(NIH_DBUS_INVALID_ARGS_STR));\n"
				  "}\n"
				  "\n"
				  "dbus_message_iter_recurse (&dictiter, &variter);\n"))
		return NULL;

	/* In case of out of memory, we can't just return because we've
	 * already made the method call so we loop over the code instead.
	 * But in case of type error in the returned arguments, all we
	 * can do is return an error.
	 */
	oom_error_code = nih_strdup (NULL, "goto enomem;\n");
	if (! oom_error_code)
		return NULL;

	type_error_code = nih_strdup (NULL,
				      "nih_free (*properties);\n"
				      "*properties = NULL;\n"
				      "dbus_message_unref (reply);\n"
				      "nih_return_error (-1, NIH_DBUS_INVALID_ARGS,\n"
				      "                  _(NIH_DBUS_INVALID_ARGS_STR));\n");
	if (! type_error_code)
		return NULL;

	property_count = 0;
	NIH_LIST_FOREACH (&interface->properties, iter) {
		Property *        property = (Property *)iter;
		DBusSignatureIter iter;
		NihList           property_outputs;
		NihList           property_locals;
		NihList           property_structs;
		nih_local char *  block = NULL;

		if (property->access == NIH_DBUS_WRITE)
			continue;

		dbus_signature_iter_init (&iter, property->type);

		nih_list_init (&property_outputs);
		nih_list_init (&property_locals);
		nih_list_init (&property_structs);

		block = demarshal (NULL, &iter, "*properties", "variter",
				   property->symbol,
				   oom_error_code,
				   type_error_code,
				   &property_outputs, &property_locals,
				   prefix, interface->symbol,
				   property->symbol, NULL,
				   &property_structs);
		if (! block)
			return NULL;

		if (! nih_strcat (&block, NULL, "\n"))
			return NULL;

		/* Each of the outputs from the demarshalling code becomes a
		 * local variable to our function that we store the value in,
		 * and that we copy into the structure.
		 */
		NIH_LIST_FOREACH_SAFE (&property_outputs, iter) {
			TypeVar *var = (TypeVar *)iter;
			TypeVar *member_var;

			/* Create the structure member entry */
			member_var = type_var_new (structure,
						   var->type, var->name);
			if (! member_var)
				return NULL;

			nih_list_add (&structure->members, &member_var->entry);

			/* Add code to copy from local variable */
			if (! nih_strcat_sprintf (&block, NULL,
						  "(*properties)->%s = %s;\n",
						  var->name, var->name))
				return NULL;

			/* Add to locals */
			nih_list_add (&locals, &var->entry);
			nih_ref (var, demarshal_block);
		}

		NIH_LIST_FOREACH_SAFE (&property_locals, iter) {
			TypeVar *var = (TypeVar *)iter;

			nih_list_add (&locals, &var->entry);
			nih_ref (var, demarshal_block);
		}

		NIH_LIST_FOREACH_SAFE (&property_structs, iter) {
			TypeStruct *structure = (TypeStruct *)iter;

			nih_list_add (&all_structs, &structure->entry);
			nih_ref (structure, demarshal_block);
		}

		if (! nih_strcat (&block, NULL,
				  "\n"
				  "nih_assert (++property_count);\n"))
			return NULL;

		/* Wrap the code in a test for the property by name */
		if (! indent (&block, NULL, 1))
			return NULL;

		if (! nih_strcat_sprintf (&property_block, NULL,
					  "\n"
					  "if (! strcmp (property, \"%s\")) {\n"
					  "%s"
					  "}\n",
					  property->name,
					  block))
			return NULL;

		property_count++;
	}

	/* Finish the property checking block, making sure that there are
	 * no further items in the dictionary entry before moving on to the
	 * next array item.
	 */
	if (! nih_strcat_sprintf (&property_block, NULL,
				  "\n"
				  "dbus_message_iter_next (&dictiter);\n"
				  "\n"
				  "if (dbus_message_iter_get_arg_type (&dictiter) != DBUS_TYPE_INVALID) {\n"
				  "\tnih_free (*properties);\n"
				  "\t*properties = NULL;\n"
				  "\tdbus_message_unref (reply);\n"
				  "\tnih_return_error (-1, NIH_DBUS_INVALID_ARGS,\n"
				  "\t                  _(NIH_DBUS_INVALID_ARGS_STR));\n"
				  "}\n"
				  "\n"
				  "dbus_message_iter_next (&arrayiter);\n"))
		return NULL;

	/* Indent the property block and append to the demarshal block */
	if (! indent (&property_block, NULL, 1))
		return NULL;

	if (! nih_strcat (&demarshal_block, NULL, property_block))
		return NULL;

	/* Finish off the demarshalling block by ending the loop and
	 * making sure that there are no further arguments in the reply.
	 */
	if (! nih_strcat_sprintf (&demarshal_block, NULL,
				  "enomem: __attribute__ ((unused));\n"
				  "}\n"
				  "\n"
				  "dbus_message_iter_next (&iter);\n"
				  "\n"
				  "if (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INVALID) {\n"
				  "\tnih_free (*properties);\n"
				  "\t*properties = NULL;\n"
				  "\tdbus_message_unref (reply);\n"
				  "\tnih_return_error (-1, NIH_DBUS_INVALID_ARGS,\n"
				  "\t                  _(NIH_DBUS_INVALID_ARGS_STR));\n"
				  "}\n"
				  "\n"
				  "if (property_count < %zu) {\n"
				  "\tnih_free (*properties);\n"
				  "\t*properties = NULL;\n"
				  "\tdbus_message_unref (reply);\n"
				  "\tnih_return_error (-1, NIH_DBUS_INVALID_ARGS,\n"
				  "\t                  _(NIH_DBUS_INVALID_ARGS_STR));\n"
				  "}\n"
				  "\n"
				  "dbus_message_unref (reply);\n",
				  property_count))
		return NULL;

	/* Lay out the function body, indenting it all before placing it
	 * in the function code.
	 */
	vars_block = type_var_layout (NULL, &locals);
	if (! vars_block)
		return NULL;

	if (! nih_strcat_sprintf (&body, NULL,
				  "%s"
				  "\n"
				  "%s"
				  "\n"
				  "%s"
				  "%s"
				  "\n"
				  "return 0;\n",
				  vars_block,
				  assert_block,
				  call_block,
				  demarshal_block))
		return NULL;

	if (! indent (&body, NULL, 1))
		return NULL;

	/* Function header */
	code = type_func_to_string (parent, func);
	if (! code)
		return NULL;

	if (! nih_strcat_sprintf (&code, parent,
				  "{\n"
				  "%s"
				  "}\n",
				  body)) {
		nih_free (code);
		return NULL;
	}

	/* Append the function to the prototypes list */
	nih_list_add (prototypes, &func->entry);
	nih_ref (func, code);

	NIH_LIST_FOREACH_SAFE (&all_structs, iter) {
		TypeStruct *structure = (TypeStruct *)iter;

		nih_ref (structure, code);
		nih_list_add (structs, &structure->entry);
	}

	nih_ref (structure, code);
	nih_list_add (structs, &structure->entry);

	return code;
}