File: marshal.c

package info (click to toggle)
libnih 1.0.3-11
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 11,824 kB
  • sloc: ansic: 188,730; sh: 11,217; makefile: 1,117; yacc: 291; xml: 239; sed: 16
file content (965 lines) | stat: -rw-r--r-- 29,018 bytes parent folder | download | duplicates (4)
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
/* nih-dbus-tool
 *
 * marshal.c - type marshalling
 *
 * 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 <dbus/dbus.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 "indent.h"
#include "symbol.h"
#include "type.h"
#include "marshal.h"


/* Prototypes for static functions */
static char *marshal_basic  (const void *parent,
			     DBusSignatureIter *iter,
			     const char *iter_name, const char *name,
			     const char *oom_error_code,
			     NihList *inputs, NihList *locals,
			     const char *prefix, const char *interface_symbol,
			     const char *member_symbol, const char *symbol,
			     NihList *structs)
	__attribute__ ((warn_unused_result, malloc));
static char *marshal_array  (const void *parent,
			     DBusSignatureIter *iter,
			     const char *iter_name, const char *name,
			     const char *oom_error_code,
			     NihList *inputs, NihList *locals,
			     const char *prefix, const char *interface_symbol,
			     const char *member_symbol, const char *symbol,
			     NihList *structs)
	__attribute__ ((warn_unused_result, malloc));
static char *marshal_struct (const void *parent,
			     DBusSignatureIter *iter,
			     const char *iter_name, const char *name,
			     const char *oom_error_code,
			     NihList *inputs, NihList *locals,
			     const char *prefix, const char *interface_symbol,
			     const char *member_symbol, const char *symbol,
			     NihList *structs)
	__attribute__ ((warn_unused_result, malloc));


/**
 * marshal:
 * @parent: parent object for new string,
 * @signature: signature of type,
 * @iter_name: name of iterator variable,
 * @name: name of variable,
 * @oom_error_code: code to execute on OOM Error,
 * @inputs: list to append input variables to,
 * @locals: list to append local variables to,
 * @prefix: prefix for structure names,
 * @interface_symbol: symbol of interface for structure names,
 * @member_symbol: symbol of interface member for structure names,
 * @symbol: symbol of argument or variable for structure names,
 * @structs: list to append structure definitions to.
 *
 * Generates C code to marshal any D-Bus type from an appropriately typed
 * variable named @name into the D-Bus iterator variable named @iter_name.
 *
 * The type should be the current element of the signature iterator @iter.
 * This then simply calls marshal_fixed(), marshal_string(),
 * marshal_fixed_array(), marshal_flexible_array() or marshal_struct()
 * as appropriate.
 *
 * The generated code detects out-of-memory conditions but does not know
 * how to handle them, therefore you need to pass the appropriate handling
 * code in @oom_error_code.  This code will be inserted wherever an OOM
 * condition is detected.
 *
 * The expected input variable types and names are given as TypeVar objects
 * appended to the @inputs list, each name is guaranteed to begin with @name
 * and the first member will always be @name itself.  Should the C code
 * require local variables, similar TypeVar objects will be appended to
 * the @locals list.
 *
 * If the variable requires 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_symbol, @member_symbol and @symbol.
 *
 * 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 returned string will also be
 * freed.
 *
 * Returns: newly allocated string or NULL if insufficient memory.
 **/
char *
marshal (const void *       parent,
	 DBusSignatureIter *iter,
	 const char *       iter_name,
	 const char *       name,
	 const char *       oom_error_code,
	 NihList *          inputs,
	 NihList *          locals,
	 const char *       prefix,
	 const char *       interface_symbol,
	 const char *       member_symbol,
	 const char *       symbol,
	 NihList *          structs)
{
	int dbus_type;

	nih_assert (iter != NULL);
	nih_assert (iter_name != NULL);
	nih_assert (name != NULL);
	nih_assert (oom_error_code != NULL);
	nih_assert (inputs != NULL);
	nih_assert (locals != NULL);
	nih_assert (prefix != NULL);
	nih_assert (member_symbol != NULL);
	nih_assert (structs != NULL);

	dbus_type = dbus_signature_iter_get_current_type (iter);

	if (dbus_type_is_basic (dbus_type)) {
		return marshal_basic (parent, iter,
				      iter_name, name,
				      oom_error_code,
				      inputs, locals,
				      prefix, interface_symbol,
				      member_symbol, symbol,
				      structs);
	} else if (dbus_type == DBUS_TYPE_ARRAY) {
		return marshal_array (parent, iter,
				      iter_name, name,
				      oom_error_code,
				      inputs, locals,
				      prefix, interface_symbol,
				      member_symbol, symbol,
				      structs);
	} else if ((dbus_type == DBUS_TYPE_STRUCT)
		   || (dbus_type == DBUS_TYPE_DICT_ENTRY)) {
		return marshal_struct (parent, iter,
				       iter_name, name,
				       oom_error_code,
				       inputs, locals,
				       prefix, interface_symbol,
				       member_symbol, symbol,
				       structs);
	} else {
		nih_assert_not_reached ();
	}
}


/**
 * marshal_basic:
 * @parent: parent object for new string,
 * @iter: D-Bus signature iterator,
 * @iter_name: name of iterator variable,
 * @name: name of variable,
 * @oom_error_code: code to execute on OOM Error,
 * @inputs: list to append input variables to,
 * @locals: list to append local variables to,
 * @interface_symbol: symbol of interface for structure names,
 * @member_symbol: symbol of interface member for structure names,
 * @symbol: symbol of argument or variable for structure names,
 * @structs: list to append structure definitions to.
 *
 * Generates C code to marshal a D-Bus basic type (ie. numerics and strings)
 * from an appropriately typed variable named @name into the D-Bus iterator
 * variable named @iter_name.
 *
 * The type should be the current element of the signature iterator @iter.
 *
 * The generated code detects out-of-memory conditions but does not know
 * how to handle them, therefore you need to pass the appropriate handling
 * code in @oom_error_code.  This code will be inserted wherever an OOM
 * condition is detected.
 *
 * The expected input variable types and names are given as TypeVar objects
 * appended to the @inputs list, each name is guaranteed to begin with @name
 * and the first member will always be @name itself.  Should the C code
 * require local variables, similar TypeVar objects will be appended to
 * the @locals list.
 *
 * If the variable requires 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_symbol, @member_symbol and @symbol.
 *
 * 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 returned string will also be
 * freed.
 *
 * Returns: newly allocated string or NULL if insufficient memory.
 **/
static char *
marshal_basic (const void *       parent,
	       DBusSignatureIter *iter,
	       const char *       iter_name,
	       const char *       name,
	       const char *       oom_error_code,
	       NihList *          inputs,
	       NihList *          locals,
	       const char *       prefix,
	       const char *       interface_symbol,
	       const char *       member_symbol,
	       const char *       symbol,
	       NihList *          structs)
{
	int             dbus_type;
	const char *    dbus_const;
	nih_local char *oom_error_block = NULL;
	nih_local char *c_type = NULL;
	char *          code = NULL;
	TypeVar *       var;

	nih_assert (iter != NULL);
	nih_assert (iter_name != NULL);
	nih_assert (name != NULL);
	nih_assert (oom_error_code != NULL);
	nih_assert (inputs != NULL);
	nih_assert (locals != NULL);
	nih_assert (prefix != NULL);
	nih_assert (member_symbol != NULL);
	nih_assert (structs != NULL);

	dbus_type = dbus_signature_iter_get_current_type (iter);
	dbus_const = type_const (dbus_type);

	oom_error_block = nih_strdup (NULL, oom_error_code);
	if (! oom_error_block)
		return NULL;
	if (! indent (&oom_error_block, NULL, 1))
		return NULL;

	c_type = type_of (NULL, iter);
	if (! c_type)
		return NULL;

	if (! nih_strcat_sprintf (&code, parent,
				  "/* Marshal a %s onto the message */\n"
				  "if (! dbus_message_iter_append_basic (&%s, %s, &%s)) {\n"
				  "%s"
				  "}\n",
				  c_type,
				  iter_name, dbus_const, name,
				  oom_error_block))
		return NULL;

	/* Append our required input variable */
	var = type_var_new (code, c_type, name);
	if (! var) {
		nih_free (code);
		return NULL;
	}

	nih_list_add (inputs, &var->entry);

	return code;
}


/**
 * marshal_array:
 * @parent: parent object for new string,
 * @iter: D-Bus signature iterator,
 * @iter_name: name of iterator variable,
 * @name: name of variable,
 * @oom_error_code: code to execute on OOM Error,
 * @inputs: list to append input variables to,
 * @locals: list to append local variables to,
 * @interface_symbol: symbol of interface for structure names,
 * @member_symbol: symbol of interface member for structure names,
 * @symbol: symbol of argument or variable for structure names,
 * @structs: list to append structure definitions to.
 *
 * Generates C code to marshal a D-Bus array type from an appropriately
 * typed, NULL-terminated, array variable named @name into the D-Bus
 * iterator variable named @iter_name.  In the case of arrays (of any
 * number of levels) ultimately to a fixed type, an additional input
 * named "@name"_len is required of size_t type or an appropriate number
 * of pointers to it.
 *
 * The type should be the current element of the signature iterator @iter.
 *
 * The generated code detects out-of-memory conditions but does not know
 * how to handle them, therefore you need to pass the appropriate handling
 * code in @oom_error_code.  This code will be inserted wherever an OOM
 * condition is detected.
 *
 * The expected input variable types and names are given as TypeVar objects
 * appended to the @inputs list, each name is guaranteed to begin with @name
 * and the first member will always be @name itself.  Should the C code
 * require local variables, similar TypeVar objects will be appended to
 * the @locals list.
 *
 * If the variable requires 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_symbol, @member_symbol and @symbol.
 *
 * 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 returned string will also be
 * freed.
 *
 * Returns: newly allocated string or NULL if insufficient memory.
 **/
static char *
marshal_array (const void *       parent,
	       DBusSignatureIter *iter,
	       const char *       iter_name,
	       const char *       name,
	       const char *       oom_error_code,
	       NihList *          inputs,
	       NihList *          locals,
	       const char *       prefix,
	       const char *       interface_symbol,
	       const char *       member_symbol,
	       const char *       symbol,
	       NihList *          structs)
{
	nih_local char *   array_iter_name = NULL;
	nih_local char *   loop_name = NULL;
	nih_local char *   element_name = NULL;
	nih_local char *   element_symbol = NULL;
	nih_local char *   len_name = NULL;
	nih_local char *   oom_error_block = NULL;
	nih_local char *   child_oom_error_code = NULL;
	nih_local char *   child_oom_error_block = NULL;
	DBusSignatureIter  subiter;
	int                element_type;
	char *             signature;
	char *             code = NULL;
	TypeVar *          array_iter_var;
	NihList            element_inputs;
	NihList            element_locals;
	NihList            element_structs;
	nih_local char *   element_block = NULL;
	nih_local char *   element_c_type = NULL;
	nih_local TypeVar *element_var = NULL;
	nih_local TypeVar *element_len_var = NULL;
	nih_local char *   block = NULL;
	nih_local char *   vars_block = NULL;
	nih_local char *   loop_block = NULL;

	nih_assert (iter != NULL);
	nih_assert (iter_name != NULL);
	nih_assert (name != NULL);
	nih_assert (oom_error_code != NULL);
	nih_assert (inputs != NULL);
	nih_assert (locals != NULL);
	nih_assert (prefix != NULL);
	nih_assert (member_symbol != NULL);
	nih_assert (structs != NULL);

	array_iter_name = nih_sprintf (NULL, "%s_iter", name);
	if (! array_iter_name)
		return NULL;

	loop_name = nih_sprintf (NULL, "%s_i", name);
	if (! loop_name)
		return NULL;

	element_name = nih_sprintf (NULL, "%s_element", name);
	if (! element_name)
		return NULL;

	element_symbol = (symbol
			  ? nih_sprintf (NULL, "%s_element", symbol)
			  : nih_strdup (NULL, "element"));
	if (! element_symbol)
		return NULL;

	len_name = nih_sprintf (NULL, "%s_len", name);
	if (! len_name)
		return NULL;

	oom_error_block = nih_strdup (NULL, oom_error_code);
	if (! oom_error_block)
		return NULL;
	if (! indent (&oom_error_block, NULL, 1))
		return NULL;

	child_oom_error_code = nih_sprintf (NULL, ("dbus_message_iter_abandon_container (&%s, &%s);\n"
						   "%s"),
					    iter_name, array_iter_name, oom_error_code);
	if (! child_oom_error_code)
		return NULL;

	child_oom_error_block = nih_strdup (NULL, child_oom_error_code);
	if (! child_oom_error_block)
		return NULL;
	if (! indent (&child_oom_error_block, NULL, 1))
		return NULL;

	/* Open the array container, we need to give D-Bus the container
	 * signature to do this and we need a local variable for the
	 * recursed iterator.
	 */
	dbus_signature_iter_recurse (iter, &subiter);
	element_type = dbus_signature_iter_get_current_type (&subiter);

	signature = dbus_signature_iter_get_signature (&subiter);

	if (! nih_strcat_sprintf (&code, parent,
				  "/* Marshal an array onto the message */\n"
				  "if (! dbus_message_iter_open_container (&%s, DBUS_TYPE_ARRAY, \"%s\", &%s)) {\n"
				  "%s"
				  "}\n"
				  "\n",
				  iter_name, signature, array_iter_name,
				  oom_error_block)) {
		dbus_free (signature);
		return NULL;
	}

	dbus_free (signature);

	array_iter_var = type_var_new (code, "DBusMessageIter",
				       array_iter_name);
	if (! array_iter_var) {
		nih_free (code);
		return NULL;
	}

	nih_list_add (locals, &array_iter_var->entry);

	if (dbus_type_is_fixed (element_type)) {
		if (! nih_strcat_sprintf (&loop_block, parent,
					  "for (size_t %s = 0; %s < %s; %s++) {\n",
					  loop_name, loop_name, len_name, loop_name)) {
			nih_free (code);
			return NULL;
		}
	} else {
		if (! nih_strcat_sprintf (&code, parent,
					  "if (%s) {\n",
					  name)) {
			nih_free (code);
			return NULL;
		}
		if (! nih_strcat_sprintf (&loop_block, parent,
					  "for (size_t %s = 0; %s[%s]; %s++) {\n",
					  loop_name, name, loop_name, loop_name)) {
			nih_free (code);
			return NULL;
		}
	}

	/* Get the code that will marshal the individual elements, the
	 * inputs that we need to give and any local variables we have
	 * to declare.
	 */
	nih_list_init (&element_inputs);
	nih_list_init (&element_locals);
	nih_list_init (&element_structs);
	element_block = marshal (NULL, &subiter,
				 array_iter_name, element_name,
				 child_oom_error_code,
				 &element_inputs,
				 &element_locals,
				 prefix, interface_symbol,
				 member_symbol, element_symbol,
				 &element_structs);
	if (! element_block) {
		nih_free (code);
		return NULL;
	}

	/* Each of the inputs of the marshalling code equates to one of our
	 * own inputs, except that we add another level of pointers for the
	 * array; at the same time, we keep the suffix and append it to our
	 * own name.  Instead of mucking around with pointers and structure
	 * members, we also append the inputs onto the local lists (making it
	 * const in the process) for the value to be marshalled into this
	 * variable.
	 */
	NIH_LIST_FOREACH_SAFE (&element_inputs, iter) {
		TypeVar *       input_var = (TypeVar *)iter;
		nih_local char *var_type = NULL;
		char *          suffix;
		nih_local char *var_name = NULL;
		TypeVar *       var;

		var_type = nih_strdup (NULL, input_var->type);
		if (! var_type) {
			nih_free (code);
			return NULL;
		}

		if (! type_to_pointer (&var_type, NULL)) {
			nih_free (code);
			return NULL;
		}

		nih_assert (! strncmp (input_var->name, element_name,
				       strlen (element_name)));
		suffix = input_var->name + strlen (element_name);

		var_name = nih_sprintf (NULL, "%s%s", name, suffix);
		if (! var_name) {
			nih_free (code);
			return NULL;
		}

		var = type_var_new (code, var_type, var_name);
		if (! var) {
			nih_free (code);
			return NULL;
		}

		nih_list_add (inputs, &var->entry);

		if (! nih_strcat_sprintf (&block, NULL,
					  "%s = %s[%s];\n",
					  input_var->name, var_name,
					  loop_name)) {
			nih_free (code);
			return NULL;
		}

		if (! type_to_const (&input_var->type, input_var)) {
			nih_free (code);
			return NULL;
		}

		nih_list_add (&element_locals, &input_var->entry);
	}

	vars_block = type_var_layout (NULL, &element_locals);
	if (! vars_block) {
		nih_free (code);
		return NULL;
	}

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

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

	/* Lay all that out in an indented block inside the for loop.
	 * Make sure that we initialise the individual elements from the
	 * pointer.
	 */
	if (! indent (&vars_block, NULL, 1)) {
		nih_free (code);
		return NULL;
	}

	if (! indent (&block, NULL, 1)) {
		nih_free (code);
		return NULL;
	}

	if (! indent (&element_block, NULL, 1)) {
		nih_free (code);
		return NULL;
	}


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

	/* Close the container again */
	if (! nih_strcat_sprintf (&loop_block, parent,
				  "}\n")) {
		nih_free (code);
		return NULL;
	}

	if (dbus_type_is_fixed (element_type)) {
		if (! nih_strcat_sprintf (&code, parent,
				 "%s\n", loop_block)) {
			nih_free (code);
			return NULL;
		}
	}
	else {
		if (! indent (&loop_block, NULL, 1)) {
			nih_free (code);
			return NULL;
		}

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

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

	/* When iterating a fixed type, we get an extra length input */
	if (dbus_type_is_fixed (element_type)) {
		TypeVar *var;

		var = type_var_new (code, "size_t", len_name);
		if (! var) {
			nih_free (code);
			return NULL;
		}

		nih_list_add (inputs, &var->entry);
	}

	return code;
}


/**
 * marshal_struct:
 * @parent: parent object for new string,
 * @iter: D-Bus signature iterator,
 * @iter_name: name of iterator variable,
 * @name: name of variable,
 * @oom_error_code: code to execute on OOM Error,
 * @inputs: list to append input variables to,
 * @locals: list to append local variables to,
 * @interface_symbol: symbol of interface for structure names,
 * @member_symbol: symbol of interface member for structure names,
 * @symbol: symbol of argument or variable for structure names,
 * @structs: list to append structure definitions to.
 *
 * Generates C code to marshal a D-Bus structure type, and its members,
 * from an appropriately typed variable named @name into the D-Bus iterator
 * variable named @iter_name.
 *
 * The type should be the current element of the signature iterator @iter.
 *
 * The generated code detects out-of-memory conditions but does not know
 * how to handle them, therefore you need to pass the appropriate handling
 * code in @oom_error_code.  This code will be inserted wherever an OOM
 * condition is detected.
 *
 * The expected input variable types and names are given as TypeVar objects
 * appended to the @inputs list, each name is guaranteed to begin with @name
 * and the first member will always be @name itself.  Should the C code
 * require local variables, similar TypeVar objects will be appended to
 * the @locals list.
 *
 * If the variable requires 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_symbol, @member_symbol and @symbol.
 *
 * 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 returned string will also be
 * freed.
 *
 * Returns: newly allocated string or NULL if insufficient memory.
 **/
static char *
marshal_struct (const void *       parent,
		DBusSignatureIter *iter,
		const char *       iter_name,
		const char *       name,
		const char *       oom_error_code,
		NihList *          inputs,
		NihList *          locals,
		const char *       prefix,
		const char *       interface_symbol,
		const char *       member_symbol,
		const char *       symbol,
		NihList *          structs)
{
	int               dbus_type;
	const char *      dbus_const;
	nih_local char *  struct_iter_name = NULL;
	nih_local char *  oom_error_block = NULL;
	nih_local char *  child_oom_error_code = NULL;
	nih_local char *  child_oom_error_block = NULL;
	nih_local char *  c_type = NULL;
	TypeStruct *      structure;
	DBusSignatureIter subiter;
	char *            code = NULL;
	TypeVar *         struct_iter_var;
	size_t            count = 0;
	TypeVar *         var;

	nih_assert (iter != NULL);
	nih_assert (iter_name != NULL);
	nih_assert (name != NULL);
	nih_assert (oom_error_code != NULL);
	nih_assert (inputs != NULL);
	nih_assert (locals != NULL);
	nih_assert (prefix != NULL);
	nih_assert (member_symbol != NULL);
	nih_assert (structs != NULL);

	dbus_type = dbus_signature_iter_get_current_type (iter);
	dbus_const = type_const (dbus_type);

	struct_iter_name = nih_sprintf (NULL, "%s_iter", name);
	if (! struct_iter_name)
		return NULL;

	oom_error_block = nih_strdup (NULL, oom_error_code);
	if (! oom_error_block)
		return NULL;
	if (! indent (&oom_error_block, NULL, 1))
		return NULL;

	child_oom_error_code = nih_sprintf (NULL, ("dbus_message_iter_abandon_container (&%s, &%s);\n"
						   "%s"),
					    iter_name, struct_iter_name, oom_error_code);
	if (! child_oom_error_code)
		return NULL;

	child_oom_error_block = nih_strdup (NULL, child_oom_error_code);
	if (! child_oom_error_block)
		return NULL;
	if (! indent (&child_oom_error_block, NULL, 1))
		return NULL;

	/* Open the struct container, for that we need to know whether this
	 * is a struct or a dictionary entry even though we handle the two
	 * identically.  We'll obviously need a local variable for the
	 * recursed iterator.
	 */
	dbus_signature_iter_recurse (iter, &subiter);

	if (! nih_strcat_sprintf (&code, parent,
				  "/* Marshal a structure onto the message */\n"
				  "if (! dbus_message_iter_open_container (&%s, %s, NULL, &%s)) {\n"
				  "%s"
				  "}\n"
				  "\n",
				  iter_name, dbus_const, struct_iter_name,
				  oom_error_block))
		return NULL;

	struct_iter_var = type_var_new (code, "DBusMessageIter",
					struct_iter_name);
	if (! struct_iter_var) {
		nih_free (code);
		return NULL;
	}

	nih_list_add (locals, &struct_iter_var->entry);

	/* FIXME there should be a way to override this to a different type
	 * name by annotation.
	 */
	c_type = symbol_typedef (NULL, prefix, interface_symbol, NULL,
				 member_symbol, symbol);
	if (! c_type) {
		nih_free (code);
		return NULL;
	}

	structure = type_struct_new (code, c_type);
	if (! structure) {
		nih_free (code);
		return NULL;
	}

	nih_list_add (structs, &structure->entry);

	if (! type_to_pointer (&c_type, NULL)) {
		nih_free (code);
		return NULL;
	}

	/* Deal with each structure element individually, however we have
	 * to end up with just one set of locals and one block so we
	 * append directly onto our locals.
	 */
	do {
		nih_local char *item_member = NULL;
		nih_local char *item_name = NULL;
		nih_local char *item_symbol = NULL;
		NihList         item_inputs;
		NihList         item_locals;
		NihList         item_structs;
		nih_local char *item_code = NULL;

		/* FIXME there should be a way to override the item names
		 * via an annotation, which would also show up in the
		 * structure definition itself.
		 */
		item_member = nih_sprintf (NULL, "item%zu", count);
		if (! item_member) {
			nih_free (code);
			return NULL;
		}

		item_name = nih_sprintf (NULL, "%s_%s", name, item_member);
		if (! item_name) {
			nih_free (code);
			return NULL;
		}

		item_symbol = (symbol
			       ? nih_sprintf (NULL, "%s_%s", symbol, item_member)
			       : nih_strdup (NULL, item_member));
		if (! item_symbol) {
			nih_free (code);
			return NULL;
		}

		/* Get the code to do the marshalling of this item */
		nih_list_init (&item_inputs);
		nih_list_init (&item_locals);
		nih_list_init (&item_structs);
		item_code = marshal (NULL, &subiter,
				     struct_iter_name, item_name,
				     child_oom_error_code,
				     &item_inputs,
				     &item_locals,
				     prefix, interface_symbol,
				     member_symbol, item_symbol,
				     structs);
		if (! item_code) {
			nih_free (code);
			return NULL;
		}

		/* Append the item locals onto our locals list, we have
		 * to reference these as we go.
		 */
		NIH_LIST_FOREACH_SAFE (&item_locals, iter) {
			TypeVar *local_var = (TypeVar *)iter;

			nih_list_add (locals, &local_var->entry);
			nih_ref (local_var, code);
		}

		/* Instead of mucking around with pointers and structure
		 * members, each of the marshalling code inputs is appended
		 * onto the local list (and made const) and we copy the
		 * value from the array into this variable.
		 */
		NIH_LIST_FOREACH_SAFE (&item_inputs, iter) {
			TypeVar *       input_var = (TypeVar *)iter;
			char *          suffix;
			nih_local char *member_name = NULL;
			TypeVar *       member_var;

			nih_assert (! strncmp (input_var->name, item_name,
					       strlen (item_name)));
			suffix = input_var->name + strlen (item_name);

			/* Create the structure member entry */
			member_name = nih_sprintf (NULL, "%s%s",
						   item_member, suffix);
			if (! member_name) {
				nih_free (code);
				return NULL;
			}

			member_var = type_var_new (structure,
						   input_var->type,
						   member_name);
			if (! member_var) {
				nih_free (code);
				return NULL;
			}

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

			/* Add code to copy into local variable */
			if (! nih_strcat_sprintf (&code, parent,
						  "%s = %s->%s;\n",
						  input_var->name, name,
						  member_name)) {
				nih_free (code);
				return NULL;
			}

			/* Make the input variable const and add to locals */
			if (! type_to_const (&input_var->type, input_var)) {
				nih_free (code);
				return NULL;
			}

			nih_list_add (locals, &input_var->entry);
			nih_ref (input_var, code);
		}

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

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

		/* Append item marshalling code block */
		if (! nih_strcat_sprintf (&code, parent,
					  "\n"
					  "%s"
					  "\n",
					  item_code)) {
			nih_free (code);
			return NULL;
		}

		nih_assert (++count > 0);
	} while (dbus_signature_iter_next (&subiter));

	/* Close the container again */
	if (! nih_strcat_sprintf (&code, parent,
				  "if (! dbus_message_iter_close_container (&%s, &%s)) {\n"
				  "%s"
				  "}\n",
				  iter_name, struct_iter_name,
				  oom_error_block)) {
		nih_free (code);
		return NULL;
	}

	/* Append our required input variable */
	var = type_var_new (code, c_type, name);
	if (! var) {
		nih_free (code);
		return NULL;
	}

	nih_list_add (inputs, &var->entry);

	return code;
}