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
|
/*
* Copyright © 2008-2011 Kristian Høgsberg
* Copyright © 2011 Intel Corporation
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that copyright
* notice and this permission notice appear in supporting documentation, and
* that the name of the copyright holders not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. The copyright holders make no representations
* about the suitability of this software for any purpose. It is provided "as
* is" without express or implied warranty.
*
* THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
* EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <expat.h>
#include "wayland-util.h"
static int
usage(int ret)
{
fprintf(stderr, "usage: ./scanner [client-header|server-header|code]\n");
exit(ret);
}
#define XML_BUFFER_SIZE 4096
struct protocol {
char *name;
char *uppercase_name;
struct wl_list interface_list;
int type_index;
int null_run_length;
char *copyright;
};
struct interface {
char *name;
char *uppercase_name;
int version;
struct wl_list request_list;
struct wl_list event_list;
struct wl_list enumeration_list;
struct wl_list link;
};
struct message {
char *name;
char *uppercase_name;
struct wl_list arg_list;
struct wl_list link;
int arg_count;
int type_index;
int all_null;
int destructor;
};
enum arg_type {
NEW_ID,
INT,
UNSIGNED,
STRING,
OBJECT,
ARRAY,
FD
};
struct arg {
char *name;
enum arg_type type;
char *interface_name;
struct wl_list link;
};
struct enumeration {
char *name;
char *uppercase_name;
struct wl_list entry_list;
struct wl_list link;
};
struct entry {
char *name;
char *uppercase_name;
char *value;
struct wl_list link;
};
struct parse_context {
const char *filename;
XML_Parser parser;
struct protocol *protocol;
struct interface *interface;
struct message *message;
struct enumeration *enumeration;
char character_data[8192];
int character_data_length;
};
static char *
uppercase_dup(const char *src)
{
char *u;
int i;
u = strdup(src);
for (i = 0; u[i]; i++)
u[i] = toupper(u[i]);
u[i] = '\0';
return u;
}
static void
fail(struct parse_context *ctx, const char *msg)
{
fprintf(stderr, "%s:%ld: %s\n",
ctx->filename, XML_GetCurrentLineNumber(ctx->parser), msg);
exit(EXIT_FAILURE);
}
static void
start_element(void *data, const char *element_name, const char **atts)
{
struct parse_context *ctx = data;
struct interface *interface;
struct message *message;
struct arg *arg;
struct enumeration *enumeration;
struct entry *entry;
const char *name, *type, *interface_name, *value;
int i, version;
name = NULL;
type = NULL;
version = 0;
interface_name = NULL;
value = NULL;
for (i = 0; atts[i]; i += 2) {
if (strcmp(atts[i], "name") == 0)
name = atts[i + 1];
if (strcmp(atts[i], "version") == 0)
version = atoi(atts[i + 1]);
if (strcmp(atts[i], "type") == 0)
type = atts[i + 1];
if (strcmp(atts[i], "value") == 0)
value = atts[i + 1];
if (strcmp(atts[i], "interface") == 0)
interface_name = atts[i + 1];
}
ctx->character_data_length = 0;
if (strcmp(element_name, "protocol") == 0) {
if (name == NULL)
fail(ctx, "no protocol name given");
ctx->protocol->name = strdup(name);
ctx->protocol->uppercase_name = uppercase_dup(name);
} else if (strcmp(element_name, "copyright") == 0) {
} else if (strcmp(element_name, "interface") == 0) {
if (name == NULL)
fail(ctx, "no interface name given");
if (version == 0)
fail(ctx, "no interface version given");
interface = malloc(sizeof *interface);
interface->name = strdup(name);
interface->uppercase_name = uppercase_dup(name);
interface->version = version;
wl_list_init(&interface->request_list);
wl_list_init(&interface->event_list);
wl_list_init(&interface->enumeration_list);
wl_list_insert(ctx->protocol->interface_list.prev,
&interface->link);
ctx->interface = interface;
} else if (strcmp(element_name, "request") == 0 ||
strcmp(element_name, "event") == 0) {
if (name == NULL)
fail(ctx, "no request name given");
message = malloc(sizeof *message);
message->name = strdup(name);
message->uppercase_name = uppercase_dup(name);
wl_list_init(&message->arg_list);
message->arg_count = 0;
if (strcmp(element_name, "request") == 0)
wl_list_insert(ctx->interface->request_list.prev,
&message->link);
else
wl_list_insert(ctx->interface->event_list.prev,
&message->link);
if (type != NULL && strcmp(type, "destructor") == 0)
message->destructor = 1;
else
message->destructor = 0;
if (strcmp(name, "destroy") == 0 && !message->destructor)
fail(ctx, "destroy request should be destructor type");
ctx->message = message;
} else if (strcmp(element_name, "arg") == 0) {
arg = malloc(sizeof *arg);
arg->name = strdup(name);
if (strcmp(type, "int") == 0)
arg->type = INT;
else if (strcmp(type, "uint") == 0)
arg->type = UNSIGNED;
else if (strcmp(type, "string") == 0)
arg->type = STRING;
else if (strcmp(type, "array") == 0)
arg->type = ARRAY;
else if (strcmp(type, "fd") == 0)
arg->type = FD;
else if (strcmp(type, "new_id") == 0) {
if (interface_name == NULL)
fail(ctx, "no interface name given");
arg->type = NEW_ID;
arg->interface_name = strdup(interface_name);
} else if (strcmp(type, "object") == 0) {
if (interface_name == NULL)
fail(ctx, "no interface name given");
arg->type = OBJECT;
arg->interface_name = strdup(interface_name);
} else {
fail(ctx, "unknown type");
}
wl_list_insert(ctx->message->arg_list.prev, &arg->link);
ctx->message->arg_count++;
} else if (strcmp(element_name, "enum") == 0) {
if (name == NULL)
fail(ctx, "no enum name given");
enumeration = malloc(sizeof *enumeration);
enumeration->name = strdup(name);
enumeration->uppercase_name = uppercase_dup(name);
wl_list_init(&enumeration->entry_list);
wl_list_insert(ctx->interface->enumeration_list.prev,
&enumeration->link);
ctx->enumeration = enumeration;
} else if (strcmp(element_name, "entry") == 0) {
entry = malloc(sizeof *entry);
entry->name = strdup(name);
entry->uppercase_name = uppercase_dup(name);
entry->value = strdup(value);
wl_list_insert(ctx->enumeration->entry_list.prev,
&entry->link);
}
}
static void
end_element(void *data, const XML_Char *name)
{
struct parse_context *ctx = data;
if (strcmp(name, "copyright") == 0) {
ctx->protocol->copyright =
strndup(ctx->character_data,
ctx->character_data_length);
}
}
static void
character_data(void *data, const XML_Char *s, int len)
{
struct parse_context *ctx = data;
if (ctx->character_data_length + len > sizeof (ctx->character_data)) {
fprintf(stderr, "too much character data");
exit(EXIT_FAILURE);
}
memcpy(ctx->character_data + ctx->character_data_length, s, len);
ctx->character_data_length += len;
}
static void
emit_opcodes(struct wl_list *message_list, struct interface *interface)
{
struct message *m;
int opcode;
if (wl_list_empty(message_list))
return;
opcode = 0;
wl_list_for_each(m, message_list, link)
printf("#define %s_%s\t%d\n",
interface->uppercase_name, m->uppercase_name, opcode++);
printf("\n");
}
static void
emit_type(struct arg *a)
{
switch (a->type) {
default:
case INT:
case FD:
printf("int32_t ");
break;
case NEW_ID:
case UNSIGNED:
printf("uint32_t ");
break;
case STRING:
printf("const char *");
break;
case OBJECT:
printf("struct %s *", a->interface_name);
break;
case ARRAY:
printf("struct wl_array *");
break;
}
}
static void
emit_stubs(struct wl_list *message_list, struct interface *interface)
{
struct message *m;
struct arg *a, *ret;
int has_destructor, has_destroy;
/* We provide a hand written constructor for the display object */
if (strcmp(interface->name, "wl_display") != 0)
printf("static inline struct %s *\n"
"%s_create(struct wl_display *display, uint32_t id, uint32_t version)\n"
"{\n"
"\twl_display_bind(display, id, \"%s\", version);\n\n"
"\treturn (struct %s *)\n"
"\t\twl_proxy_create_for_id(display, &%s_interface, id);\n"
"}\n\n",
interface->name,
interface->name,
interface->name,
interface->name,
interface->name);
printf("static inline void\n"
"%s_set_user_data(struct %s *%s, void *user_data)\n"
"{\n"
"\twl_proxy_set_user_data((struct wl_proxy *) %s, user_data);\n"
"}\n\n",
interface->name, interface->name, interface->name,
interface->name);
printf("static inline void *\n"
"%s_get_user_data(struct %s *%s)\n"
"{\n"
"\treturn wl_proxy_get_user_data((struct wl_proxy *) %s);\n"
"}\n\n",
interface->name, interface->name, interface->name,
interface->name);
has_destructor = 0;
has_destroy = 0;
wl_list_for_each(m, message_list, link) {
if (m->destructor)
has_destructor = 1;
if (strcmp(m->name, "destroy)") == 0)
has_destroy = 1;
}
if (!has_destructor && has_destroy) {
fprintf(stderr,
"interface %s has method named destroy but"
"no destructor", interface->name);
exit(EXIT_FAILURE);
}
/* And we have a hand-written display destructor */
if (!has_destructor && strcmp(interface->name, "wl_display") != 0)
printf("static inline void\n"
"%s_destroy(struct %s *%s)\n"
"{\n"
"\twl_proxy_destroy("
"(struct wl_proxy *) %s);\n"
"}\n\n",
interface->name, interface->name, interface->name,
interface->name);
if (wl_list_empty(message_list))
return;
wl_list_for_each(m, message_list, link) {
ret = NULL;
wl_list_for_each(a, &m->arg_list, link) {
if (a->type == NEW_ID)
ret = a;
}
if (ret)
printf("static inline struct %s *\n",
ret->interface_name);
else
printf("static inline void\n");
printf("%s_%s(struct %s *%s",
interface->name, m->name,
interface->name, interface->name);
wl_list_for_each(a, &m->arg_list, link) {
if (a->type == NEW_ID)
continue;
printf(", ");
emit_type(a);
printf("%s", a->name);
}
printf(")\n"
"{\n");
if (ret)
printf("\tstruct wl_proxy *%s;\n\n"
"\t%s = wl_proxy_create("
"(struct wl_proxy *) %s,\n"
"\t\t\t &%s_interface);\n"
"\tif (!%s)\n"
"\t\treturn NULL;\n\n",
ret->name,
ret->name,
interface->name, ret->interface_name,
ret->name);
printf("\twl_proxy_marshal((struct wl_proxy *) %s,\n"
"\t\t\t %s_%s",
interface->name,
interface->uppercase_name,
m->uppercase_name);
wl_list_for_each(a, &m->arg_list, link) {
printf(", ");
printf("%s", a->name);
}
printf(");\n");
if (m->destructor)
printf("\n\twl_proxy_destroy("
"(struct wl_proxy *) %s);\n",
interface->name);
if (ret)
printf("\n\treturn (struct %s *) %s;\n",
ret->interface_name, ret->name);
printf("}\n\n");
}
}
static const char *indent(int n)
{
const char *whitespace[] = {
"\t\t\t\t\t\t\t\t\t\t\t\t",
"\t\t\t\t\t\t\t\t\t\t\t\t ",
"\t\t\t\t\t\t\t\t\t\t\t\t ",
"\t\t\t\t\t\t\t\t\t\t\t\t ",
"\t\t\t\t\t\t\t\t\t\t\t\t ",
"\t\t\t\t\t\t\t\t\t\t\t\t ",
"\t\t\t\t\t\t\t\t\t\t\t\t ",
"\t\t\t\t\t\t\t\t\t\t\t\t "
};
return whitespace[n % 8] + 12 - n / 8;
}
static void
emit_enumerations(struct interface *interface)
{
struct enumeration *e;
struct entry *entry;
wl_list_for_each(e, &interface->enumeration_list, link) {
printf("#ifndef %s_%s_ENUM\n",
interface->uppercase_name, e->uppercase_name);
printf("#define %s_%s_ENUM\n",
interface->uppercase_name, e->uppercase_name);
printf("enum %s_%s {\n", interface->name, e->name);
wl_list_for_each(entry, &e->entry_list, link)
printf("\t%s_%s_%s = %s,\n",
interface->uppercase_name,
e->uppercase_name,
entry->uppercase_name, entry->value);
printf("};\n");
printf("#endif /* %s_%s_ENUM */\n\n",
interface->uppercase_name, e->uppercase_name);
}
}
static void
emit_structs(struct wl_list *message_list, struct interface *interface)
{
struct message *m;
struct arg *a;
int is_interface, n;
if (wl_list_empty(message_list))
return;
is_interface = message_list == &interface->request_list;
printf("struct %s_%s {\n", interface->name,
is_interface ? "interface" : "listener");
wl_list_for_each(m, message_list, link) {
printf("\tvoid (*%s)(", m->name);
n = strlen(m->name) + 17;
if (is_interface) {
printf("struct wl_client *client,\n"
"%sstruct %s *%s",
indent(n),
interface->name, interface->name);
} else {
printf("void *data,\n"),
printf("%sstruct %s *%s",
indent(n), interface->name, interface->name);
}
wl_list_for_each(a, &m->arg_list, link) {
printf(",\n%s", indent(n));
emit_type(a);
printf("%s", a->name);
}
printf(");\n");
}
printf("};\n\n");
if (!is_interface) {
printf("static inline int\n"
"%s_add_listener(struct %s *%s,\n"
"%sconst struct %s_listener *listener, void *data)\n"
"{\n"
"\treturn wl_proxy_add_listener((struct wl_proxy *) %s,\n"
"%s(void (**)(void)) listener, data);\n"
"}\n\n",
interface->name, interface->name, interface->name,
indent(17 + strlen(interface->name)),
interface->name,
interface->name,
indent(37));
}
}
static void
format_copyright(const char *copyright)
{
int bol = 1, start = 0, i;
for (i = 0; copyright[i]; i++) {
if (bol && (copyright[i] == ' ' || copyright[i] == '\t')) {
continue;
} else if (bol) {
bol = 0;
start = i;
}
if (copyright[i] == '\n' || copyright[i] == '\0') {
printf("%s %.*s\n",
i == 0 ? "/*" : " *",
i - start, copyright + start);
bol = 1;
}
}
printf(" */\n\n");
}
static void
emit_header(struct protocol *protocol, int server)
{
struct interface *i;
const char *s = server ? "SERVER" : "CLIENT";
if (protocol->copyright)
format_copyright(protocol->copyright);
printf("#ifndef %s_%s_PROTOCOL_H\n"
"#define %s_%s_PROTOCOL_H\n"
"\n"
"#ifdef __cplusplus\n"
"extern \"C\" {\n"
"#endif\n"
"\n"
"#include <stdint.h>\n"
"#include <stddef.h>\n"
"#include \"wayland-util.h\"\n\n"
"struct wl_client;\n\n",
protocol->uppercase_name, s,
protocol->uppercase_name, s);
wl_list_for_each(i, &protocol->interface_list, link)
printf("struct %s;\n", i->name);
printf("\n");
wl_list_for_each(i, &protocol->interface_list, link) {
printf("extern const struct wl_interface "
"%s_interface;\n",
i->name);
}
printf("\n");
wl_list_for_each(i, &protocol->interface_list, link) {
emit_enumerations(i);
if (server) {
emit_structs(&i->request_list, i);
emit_opcodes(&i->event_list, i);
} else {
emit_structs(&i->event_list, i);
emit_opcodes(&i->request_list, i);
emit_stubs(&i->request_list, i);
}
}
printf("#ifdef __cplusplus\n"
"}\n"
"#endif\n"
"\n"
"#endif\n");
}
static void
emit_types_forward_declarations(struct protocol *protocol,
struct wl_list *message_list)
{
struct message *m;
struct arg *a;
int length;
wl_list_for_each(m, message_list, link) {
length = 0;
m->all_null = 1;
wl_list_for_each(a, &m->arg_list, link) {
length++;
switch (a->type) {
case NEW_ID:
case OBJECT:
m->all_null = 0;
printf("extern const struct wl_interface %s_interface;\n",
a->interface_name);
break;
default:
break;
}
}
if (m->all_null && length > protocol->null_run_length)
protocol->null_run_length = length;
}
}
static void
emit_null_run(struct protocol *protocol)
{
int i;
for (i = 0; i < protocol->null_run_length; i++)
printf("\tNULL,\n");
}
static void
emit_types(struct protocol *protocol, struct wl_list *message_list)
{
struct message *m;
struct arg *a;
wl_list_for_each(m, message_list, link) {
if (m->all_null) {
m->type_index = 0;
continue;
}
m->type_index =
protocol->null_run_length + protocol->type_index;
protocol->type_index += m->arg_count;
wl_list_for_each(a, &m->arg_list, link) {
switch (a->type) {
case NEW_ID:
case OBJECT:
if (strcmp(a->interface_name,
"wl_object") != 0)
printf("\t&%s_interface,\n",
a->interface_name);
else
printf("\tNULL,\n");
break;
default:
printf("\tNULL,\n");
break;
}
}
}
}
static void
emit_messages(struct wl_list *message_list,
struct interface *interface, const char *suffix)
{
struct message *m;
struct arg *a;
if (wl_list_empty(message_list))
return;
printf("static const struct wl_message "
"%s_%s[] = {\n",
interface->name, suffix);
wl_list_for_each(m, message_list, link) {
printf("\t{ \"%s\", \"", m->name);
wl_list_for_each(a, &m->arg_list, link) {
switch (a->type) {
default:
case INT:
printf("i");
break;
case NEW_ID:
printf("n");
break;
case UNSIGNED:
printf("u");
break;
case STRING:
printf("s");
break;
case OBJECT:
printf("o");
break;
case ARRAY:
printf("a");
break;
case FD:
printf("h");
break;
}
}
printf("\", types + %d },\n", m->type_index);
}
printf("};\n\n");
}
static void
emit_code(struct protocol *protocol)
{
struct interface *i;
if (protocol->copyright)
format_copyright(protocol->copyright);
printf("#include <stdlib.h>\n"
"#include <stdint.h>\n"
"#include \"wayland-util.h\"\n\n");
wl_list_for_each(i, &protocol->interface_list, link) {
emit_types_forward_declarations(protocol, &i->request_list);
emit_types_forward_declarations(protocol, &i->event_list);
}
printf("\n");
printf("static const struct wl_interface *types[] = {\n");
emit_null_run(protocol);
wl_list_for_each(i, &protocol->interface_list, link) {
emit_types(protocol, &i->request_list);
emit_types(protocol, &i->event_list);
}
printf("};\n\n");
wl_list_for_each(i, &protocol->interface_list, link) {
emit_messages(&i->request_list, i, "requests");
emit_messages(&i->event_list, i, "events");
printf("WL_EXPORT const struct wl_interface "
"%s_interface = {\n"
"\t\"%s\", %d,\n",
i->name, i->name, i->version);
if (!wl_list_empty(&i->request_list))
printf("\tARRAY_LENGTH(%s_requests), %s_requests,\n",
i->name, i->name);
else
printf("\t0, NULL,\n");
if (!wl_list_empty(&i->event_list))
printf("\tARRAY_LENGTH(%s_events), %s_events,\n",
i->name, i->name);
else
printf("\t0, NULL,\n");
printf("};\n\n");
}
}
int main(int argc, char *argv[])
{
struct parse_context ctx;
struct protocol protocol;
int len;
void *buf;
if (argc != 2)
usage(EXIT_FAILURE);
wl_list_init(&protocol.interface_list);
protocol.type_index = 0;
protocol.null_run_length = 0;
protocol.copyright = NULL;
ctx.protocol = &protocol;
ctx.filename = "<stdin>";
ctx.parser = XML_ParserCreate(NULL);
XML_SetUserData(ctx.parser, &ctx);
if (ctx.parser == NULL) {
fprintf(stderr, "failed to create parser\n");
exit(EXIT_FAILURE);
}
XML_SetElementHandler(ctx.parser, start_element, end_element);
XML_SetCharacterDataHandler(ctx.parser, character_data);
do {
buf = XML_GetBuffer(ctx.parser, XML_BUFFER_SIZE);
len = fread(buf, 1, XML_BUFFER_SIZE, stdin);
if (len < 0) {
fprintf(stderr, "fread: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
XML_ParseBuffer(ctx.parser, len, len == 0);
} while (len > 0);
XML_ParserFree(ctx.parser);
if (strcmp(argv[1], "client-header") == 0) {
emit_header(&protocol, 0);
} else if (strcmp(argv[1], "server-header") == 0) {
emit_header(&protocol, 1);
} else if (strcmp(argv[1], "code") == 0) {
emit_code(&protocol);
}
return 0;
}
|