scanner.c 48.8 KB
Newer Older
1
/*
2
3
 * Copyright © 2008-2011 Kristian Høgsberg
 * Copyright © 2011 Intel Corporation
4
 * Copyright © 2015 Red Hat, Inc.
5
 *
6
7
8
9
10
11
12
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
13
 *
14
15
16
17
18
19
20
21
22
23
24
25
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
26
27
 */

28
#include "wayland-version.h"
29

30
#include <stdbool.h>
31
#include <stdio.h>
32
#include <stdarg.h>
Yong Bakos's avatar
Yong Bakos committed
33
#include <stdint.h>
34
35
36
#include <string.h>
#include <errno.h>
#include <ctype.h>
37
#include <getopt.h>
38
#include <limits.h>
39
40
41
42
#include <unistd.h>

#if HAVE_LIBXML
#include <libxml/parser.h>
43

44
45
46
/* Embedded wayland.dtd file, see dtddata.S */
extern char DTD_DATA_begin;
extern int DTD_DATA_len;
47
48
#endif

49
50
51
52
/* Expat must be included after libxml as both want to declare XMLCALL; see
 * the Git commit that 'git blame' for this comment points to for more. */
#include <expat.h>

53
#include "wayland-util.h"
54

Yong Bakos's avatar
Yong Bakos committed
55
56
#define PROGRAM_NAME "wayland-scanner"

57
58
59
60
61
enum side {
	CLIENT,
	SERVER,
};

62
63
64
65
66
enum visibility {
	PRIVATE,
	PUBLIC,
};

67
68
69
static int
usage(int ret)
{
70
	fprintf(stderr, "usage: %s [OPTION] [client-header|server-header|private-code|public-code]"
71
		" [input_file output_file]\n", PROGRAM_NAME);
Peter Hutterer's avatar
Peter Hutterer committed
72
73
	fprintf(stderr, "\n");
	fprintf(stderr, "Converts XML protocol descriptions supplied on "
74
			"stdin or input file to client\n"
75
76
77
78
			"headers, server headers, or protocol marshalling code.\n\n"
			"Use \"public-code\" only if the marshalling code will be public - "
			"aka DSO will export it while other components will be using it.\n"
			"Using \"private-code\" is strongly recommended.\n\n");
79
80
	fprintf(stderr, "options:\n");
	fprintf(stderr, "    -h,  --help                  display this help and exit.\n"
81
82
			"    -v,  --version               print the wayland library version that\n"
			"                                 the scanner was built against.\n"
83
84
			"    -c,  --include-core-only     include the core version of the headers,\n"
			"                                 that is e.g. wayland-client-core.h instead\n"
Jonas Ådahl's avatar
Jonas Ådahl committed
85
86
87
			"                                 of wayland-client.h.\n"
			"    -s,  --strict                exit immediately with an error if DTD\n"
			"                                 verification fails.\n");
88
89
90
	exit(ret);
}

91
92
93
static int
scanner_version(int ret)
{
94
	fprintf(stderr, "%s %s\n", PROGRAM_NAME, WAYLAND_VERSION);
95
96
97
	exit(ret);
}

98
static bool
99
is_dtd_valid(FILE *input, const char *filename)
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
{
	bool rc = true;
#if HAVE_LIBXML
	xmlParserCtxtPtr ctx = NULL;
	xmlDocPtr doc = NULL;
	xmlDtdPtr dtd = NULL;
	xmlValidCtxtPtr	dtdctx;
	xmlParserInputBufferPtr	buffer;
	int fd = fileno(input);

	dtdctx = xmlNewValidCtxt();
	ctx = xmlNewParserCtxt();
	if (!ctx || !dtdctx)
		abort();

	buffer = xmlParserInputBufferCreateMem(&DTD_DATA_begin,
					       DTD_DATA_len,
					       XML_CHAR_ENCODING_UTF8);
	if (!buffer) {
		fprintf(stderr, "Failed to init buffer for DTD.\n");
		abort();
	}

	dtd = xmlIOParseDTD(NULL, buffer, XML_CHAR_ENCODING_UTF8);
	if (!dtd) {
		fprintf(stderr, "Failed to parse DTD.\n");
		abort();
	}

129
	doc = xmlCtxtReadFd(ctx, fd, filename, NULL, 0);
130
131
132
133
134
135
136
137
	if (!doc) {
		fprintf(stderr, "Failed to read XML\n");
		abort();
	}

	rc = xmlValidateDtd(dtdctx, doc, dtd);
	xmlFreeDoc(doc);
	xmlFreeParserCtxt(ctx);
Daniel Stone's avatar
Daniel Stone committed
138
	xmlFreeDtd(dtd);
139
140
141
142
143
144
145
146
147
148
149
	xmlFreeValidCtxt(dtdctx);
	/* xmlIOParseDTD consumes buffer */

	if (lseek(fd, 0, SEEK_SET) != 0) {
		fprintf(stderr, "Failed to reset fd, output would be garbage.\n");
		abort();
	}
#endif
	return rc;
}

150
151
#define XML_BUFFER_SIZE 4096

152
153
154
155
156
struct location {
	const char *filename;
	int line_number;
};

157
158
159
160
161
struct description {
	char *summary;
	char *text;
};

162
struct protocol {
163
164
	char *name;
	char *uppercase_name;
165
	struct wl_list interface_list;
166
167
	int type_index;
	int null_run_length;
168
	char *copyright;
169
	struct description *description;
170
	bool core_headers;
171
172
};

173
struct interface {
174
	struct location loc;
175
176
177
	char *name;
	char *uppercase_name;
	int version;
178
	int since;
179
180
	struct wl_list request_list;
	struct wl_list event_list;
181
	struct wl_list enumeration_list;
182
	struct wl_list link;
183
	struct description *description;
184
185
186
};

struct message {
187
	struct location loc;
188
189
190
191
	char *name;
	char *uppercase_name;
	struct wl_list arg_list;
	struct wl_list link;
192
	int arg_count;
193
	int new_id_count;
194
195
	int type_index;
	int all_null;
196
	int destructor;
197
	int since;
198
	struct description *description;
199
200
201
202
203
204
};

enum arg_type {
	NEW_ID,
	INT,
	UNSIGNED,
205
	FIXED,
206
207
	STRING,
	OBJECT,
208
209
	ARRAY,
	FD
210
211
212
213
214
};

struct arg {
	char *name;
	enum arg_type type;
215
	int nullable;
216
	char *interface_name;
217
	struct wl_list link;
218
	char *summary;
219
	char *enumeration_name;
220
221
};

222
223
224
225
226
struct enumeration {
	char *name;
	char *uppercase_name;
	struct wl_list entry_list;
	struct wl_list link;
227
	struct description *description;
228
	bool bitfield;
229
	int since;
230
231
232
233
234
235
};

struct entry {
	char *name;
	char *uppercase_name;
	char *value;
236
	char *summary;
237
	int since;
238
239
240
	struct wl_list link;
};

241
struct parse_context {
242
	struct location loc;
243
	XML_Parser parser;
244
245
246
	struct protocol *protocol;
	struct interface *interface;
	struct message *message;
247
	struct enumeration *enumeration;
248
	struct description *description;
249
	char character_data[8192];
250
	unsigned int character_data_length;
251
252
};

253
254
255
256
257
enum identifier_role {
	STANDALONE_IDENT,
	TRAILING_IDENT
};

258
259
260
261
static void *
fail_on_null(void *p)
{
	if (p == NULL) {
262
		fprintf(stderr, "%s: out of memory\n", PROGRAM_NAME);
263
264
265
266
267
268
269
		exit(EXIT_FAILURE);
	}

	return p;
}

static void *
Marek Chalupa's avatar
Marek Chalupa committed
270
zalloc(size_t s)
271
{
Marek Chalupa's avatar
Marek Chalupa committed
272
273
274
275
276
277
278
	return calloc(s, 1);
}

static void *
xzalloc(size_t s)
{
	return fail_on_null(zalloc(s));
279
280
281
282
283
284
285
286
}

static char *
xstrdup(const char *s)
{
	return fail_on_null(strdup(s));
}

287
288
289
290
291
292
static char *
uppercase_dup(const char *src)
{
	char *u;
	int i;

293
	u = xstrdup(src);
294
295
296
297
298
299
300
	for (i = 0; u[i]; i++)
		u[i] = toupper(u[i]);
	u[i] = '\0';

	return u;
}

301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
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;
}

317
static void
318
desc_dump(char *desc, const char *fmt, ...) WL_PRINTF(2, 3);
319

320
static void
321
desc_dump(char *desc, const char *fmt, ...)
322
{
323
	va_list ap;
324
	char buf[128], hang;
325
	int col, i, j, k, startcol, newlines;
326
327
328
329
330
331
332
333
334
335
336
337
338

	va_start(ap, fmt);
	vsnprintf(buf, sizeof buf, fmt, ap);
	va_end(ap);

	for (i = 0, col = 0; buf[i] != '*'; i++) {
		if (buf[i] == '\t')
			col = (col + 8) & ~7;
		else
			col++;
	}

	printf("%s", buf);
339

340
341
342
343
344
345
346
347
348
349
350
351
352
353
	if (!desc) {
		printf("(none)\n");
		return;
	}

	startcol = col;
	col += strlen(&buf[i]);
	if (col - startcol > 2)
		hang = '\t';
	else
		hang = ' ';

	for (i = 0; desc[i]; ) {
		k = i;
354
355
356
357
		newlines = 0;
		while (desc[i] && isspace(desc[i])) {
			if (desc[i] == '\n')
				newlines++;
358
			i++;
359
		}
360
361
		if (!desc[i])
			break;
362

363
		j = i;
364
		while (desc[i] && !isspace(desc[i]))
365
			i++;
366

367
368
369
		if (newlines > 1)
			printf("\n%s*", indent(startcol));
		if (newlines > 1 || col + i - j > 72) {
370
			printf("\n%s*%c", indent(startcol), hang);
371
			col = startcol;
372
		}
373

374
375
376
		if (col > startcol && k > 0)
			col += printf(" ");
		col += printf("%.*s", i - j, &desc[j]);
377
	}
378
	putchar('\n');
379
380
}

381
static void __attribute__ ((noreturn))
382
fail(struct location *loc, const char *msg, ...)
383
{
384
385
386
387
388
389
390
391
	va_list ap;

	va_start(ap, msg);
	fprintf(stderr, "%s:%d: error: ",
		loc->filename, loc->line_number);
	vfprintf(stderr, msg, ap);
	fprintf(stderr, "\n");
	va_end(ap);
392
393
394
	exit(EXIT_FAILURE);
}

395
396
397
398
399
400
401
402
403
404
405
406
407
static void
warn(struct location *loc, const char *msg, ...)
{
	va_list ap;

	va_start(ap, msg);
	fprintf(stderr, "%s:%d: warning: ",
		loc->filename, loc->line_number);
	vfprintf(stderr, msg, ap);
	fprintf(stderr, "\n");
	va_end(ap);
}

408
static bool
409
410
411
412
413
414
is_nullable_type(struct arg *arg)
{
	switch (arg->type) {
	/* Strings, objects, and arrays are possibly nullable */
	case STRING:
	case OBJECT:
Daniel Stone's avatar
Daniel Stone committed
415
	case NEW_ID:
416
	case ARRAY:
417
		return true;
418
	default:
419
		return false;
420
421
422
	}
}

423
424
425
426
427
static struct message *
create_message(struct location loc, const char *name)
{
	struct message *message;

Marek Chalupa's avatar
Marek Chalupa committed
428
	message = xzalloc(sizeof *message);
429
430
431
432
433
434
435
436
	message->loc = loc;
	message->name = xstrdup(name);
	message->uppercase_name = uppercase_dup(name);
	wl_list_init(&message->arg_list);

	return message;
}

Marek Chalupa's avatar
Marek Chalupa committed
437
438
439
440
441
442
static void
free_arg(struct arg *arg)
{
	free(arg->name);
	free(arg->interface_name);
	free(arg->summary);
Daniel Stone's avatar
Daniel Stone committed
443
	free(arg->enumeration_name);
Marek Chalupa's avatar
Marek Chalupa committed
444
445
446
	free(arg);
}

447
448
449
450
451
static struct arg *
create_arg(const char *name)
{
	struct arg *arg;

Marek Chalupa's avatar
Marek Chalupa committed
452
	arg = xzalloc(sizeof *arg);
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
	arg->name = xstrdup(name);

	return arg;
}

static bool
set_arg_type(struct arg *arg, const char *type)
{
	if (strcmp(type, "int") == 0)
		arg->type = INT;
	else if (strcmp(type, "uint") == 0)
		arg->type = UNSIGNED;
	else if (strcmp(type, "fixed") == 0)
		arg->type = FIXED;
	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)
		arg->type = NEW_ID;
	else if (strcmp(type, "object") == 0)
		arg->type = OBJECT;
	else
		return false;

	return true;
}

Marek Chalupa's avatar
Marek Chalupa committed
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
static void
free_description(struct description *desc)
{
	if (!desc)
		return;

	free(desc->summary);
	free(desc->text);

	free(desc);
}

static void
free_message(struct message *message)
{
	struct arg *a, *a_next;

	free(message->name);
	free(message->uppercase_name);
	free_description(message->description);

	wl_list_for_each_safe(a, a_next, &message->arg_list, link)
		free_arg(a);

	free(message);
}

510
511
512
513
514
static struct enumeration *
create_enumeration(const char *name)
{
	struct enumeration *enumeration;

Marek Chalupa's avatar
Marek Chalupa committed
515
	enumeration = xzalloc(sizeof *enumeration);
516
517
	enumeration->name = xstrdup(name);
	enumeration->uppercase_name = uppercase_dup(name);
518
	enumeration->since = 1;
519
520
521
522
523
524
525
526
527
528
529

	wl_list_init(&enumeration->entry_list);

	return enumeration;
}

static struct entry *
create_entry(const char *name, const char *value)
{
	struct entry *entry;

Marek Chalupa's avatar
Marek Chalupa committed
530
	entry = xzalloc(sizeof *entry);
531
532
533
534
535
536
537
	entry->name = xstrdup(name);
	entry->uppercase_name = uppercase_dup(name);
	entry->value = xstrdup(value);

	return entry;
}

Marek Chalupa's avatar
Marek Chalupa committed
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
static void
free_entry(struct entry *entry)
{
	free(entry->name);
	free(entry->uppercase_name);
	free(entry->value);
	free(entry->summary);

	free(entry);
}

static void
free_enumeration(struct enumeration *enumeration)
{
	struct entry *e, *e_next;

	free(enumeration->name);
	free(enumeration->uppercase_name);
	free_description(enumeration->description);

	wl_list_for_each_safe(e, e_next, &enumeration->entry_list, link)
		free_entry(e);

	free(enumeration);
}

564
565
566
567
568
static struct interface *
create_interface(struct location loc, const char *name, int version)
{
	struct interface *interface;

Marek Chalupa's avatar
Marek Chalupa committed
569
	interface = xzalloc(sizeof *interface);
570
571
572
573
574
575
576
577
578
579
580
581
	interface->loc = loc;
	interface->name = xstrdup(name);
	interface->uppercase_name = uppercase_dup(name);
	interface->version = version;
	interface->since = 1;
	wl_list_init(&interface->request_list);
	wl_list_init(&interface->event_list);
	wl_list_init(&interface->enumeration_list);

	return interface;
}

Marek Chalupa's avatar
Marek Chalupa committed
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
static void
free_interface(struct interface *interface)
{
	struct message *m, *next_m;
	struct enumeration *e, *next_e;

	free(interface->name);
	free(interface->uppercase_name);
	free_description(interface->description);

	wl_list_for_each_safe(m, next_m, &interface->request_list, link)
		free_message(m);
	wl_list_for_each_safe(m, next_m, &interface->event_list, link)
		free_message(m);
	wl_list_for_each_safe(e, next_e, &interface->enumeration_list, link)
		free_enumeration(e);

	free(interface);
}

602
603
604
605
606
607
608
609
610
/* Convert string to unsigned integer
 *
 * Parses a non-negative base-10 number from the given string.  If the
 * specified string is blank, contains non-numerical characters, is out
 * of range, or results in a negative number, -1 is returned to indicate
 * an error.
 *
 * Upon error, this routine does not modify or set errno.
 *
611
 * Returns -1 on error, or a non-negative integer on success
612
 */
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
static int
strtouint(const char *str)
{
	long int ret;
	char *end;
	int prev_errno = errno;

	errno = 0;
	ret = strtol(str, &end, 10);
	if (errno != 0 || end == str || *end != '\0')
		return -1;

	/* check range */
	if (ret < 0 || ret > INT_MAX) {
		return -1;
	}

	errno = prev_errno;
	return (int)ret;
}

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
/* Check that the provided string will produce valid "C" identifiers.
 *
 * If the string will form the prefix of an identifier in the
 * generated C code, then it must match [_a-zA-Z][_0-9a-zA-Z]*.
 *
 * If the string will form the suffix of an identifier, then
 * it must match [_0-9a-zA-Z]+.
 *
 * Unicode characters or escape sequences are not permitted,
 * since not all C compilers support them.
 *
 * If the above conditions are not met, then fail()
 */
static void
validate_identifier(struct location *loc,
		const char *str,
		enum identifier_role role)
{
	const char *scan;

	if (!*str) {
		fail(loc, "element name is empty");
	}

	for (scan = str; *scan; scan++) {
		char c = *scan;

		/* we do not use the locale-dependent `isalpha` */
		bool is_alpha = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
		bool is_digit = c >= '0' && c <= '9';
		bool leading_char = (scan == str) && role == STANDALONE_IDENT;

		if (is_alpha || c == '_' || (!leading_char && is_digit))
			continue;

		if (role == TRAILING_IDENT)
			fail(loc,
			     "'%s' is not a valid trailing identifier part", str);
		else
			fail(loc,
			     "'%s' is not a valid standalone identifier", str);
	}
}

678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
static int
version_from_since(struct parse_context *ctx, const char *since)
{
	int version;

	if (since != NULL) {
		version = strtouint(since);
		if (version == -1) {
			fail(&ctx->loc, "invalid integer (%s)\n", since);
		} else if (version > ctx->interface->version) {
			fail(&ctx->loc, "since (%u) larger than version (%u)\n",
			     version, ctx->interface->version);
		}
	} else {
		version = 1;
	}


	return version;
}

699
700
701
702
703
704
705
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;
706
707
	struct enumeration *enumeration;
	struct entry *entry;
Marek Chalupa's avatar
Marek Chalupa committed
708
709
710
711
712
713
714
715
	struct description *description = NULL;
	const char *name = NULL;
	const char *type = NULL;
	const char *interface_name = NULL;
	const char *value = NULL;
	const char *summary = NULL;
	const char *since = NULL;
	const char *allow_null = NULL;
716
717
	const char *enumeration_name = NULL;
	const char *bitfield = NULL;
Marek Chalupa's avatar
Marek Chalupa committed
718
	int i, version = 0;
719

720
	ctx->loc.line_number = XML_GetCurrentLineNumber(ctx->parser);
721
722
723
	for (i = 0; atts[i]; i += 2) {
		if (strcmp(atts[i], "name") == 0)
			name = atts[i + 1];
Jonas Ådahl's avatar
Jonas Ådahl committed
724
		if (strcmp(atts[i], "version") == 0) {
725
726
727
			version = strtouint(atts[i + 1]);
			if (version == -1)
				fail(&ctx->loc, "wrong version (%s)", atts[i + 1]);
Jonas Ådahl's avatar
Jonas Ådahl committed
728
		}
729
730
		if (strcmp(atts[i], "type") == 0)
			type = atts[i + 1];
731
732
		if (strcmp(atts[i], "value") == 0)
			value = atts[i + 1];
733
734
		if (strcmp(atts[i], "interface") == 0)
			interface_name = atts[i + 1];
735
736
		if (strcmp(atts[i], "summary") == 0)
			summary = atts[i + 1];
737
738
		if (strcmp(atts[i], "since") == 0)
			since = atts[i + 1];
739
740
		if (strcmp(atts[i], "allow-null") == 0)
			allow_null = atts[i + 1];
741
742
743
744
		if (strcmp(atts[i], "enum") == 0)
			enumeration_name = atts[i + 1];
		if (strcmp(atts[i], "bitfield") == 0)
			bitfield = atts[i + 1];
745
746
	}

747
	ctx->character_data_length = 0;
748
	if (strcmp(element_name, "protocol") == 0) {
749
		if (name == NULL)
750
			fail(&ctx->loc, "no protocol name given");
751

752
		validate_identifier(&ctx->loc, name, STANDALONE_IDENT);
753
		ctx->protocol->name = xstrdup(name);
754
		ctx->protocol->uppercase_name = uppercase_dup(name);
755
	} else if (strcmp(element_name, "copyright") == 0) {
Michael Vetter's avatar
Michael Vetter committed
756

757
	} else if (strcmp(element_name, "interface") == 0) {
758
		if (name == NULL)
759
			fail(&ctx->loc, "no interface name given");
760

761
		if (version == 0)
762
			fail(&ctx->loc, "no interface version given");
763

764
		validate_identifier(&ctx->loc, name, STANDALONE_IDENT);
765
766
		interface = create_interface(ctx->loc, name, version);
		ctx->interface = interface;
767
768
769
770
		wl_list_insert(ctx->protocol->interface_list.prev,
			       &interface->link);
	} else if (strcmp(element_name, "request") == 0 ||
		   strcmp(element_name, "event") == 0) {
771
		if (name == NULL)
772
			fail(&ctx->loc, "no request name given");
773

774
		validate_identifier(&ctx->loc, name, STANDALONE_IDENT);
775
		message = create_message(ctx->loc, name);
776
777
778
779
780
781
782
783

		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);

784
785
786
		if (type != NULL && strcmp(type, "destructor") == 0)
			message->destructor = 1;

787
		version = version_from_since(ctx, since);
788

789
		if (version < ctx->interface->since)
790
			warn(&ctx->loc, "since version not increasing\n");
791
792
		ctx->interface->since = version;
		message->since = version;
793

794
		if (strcmp(name, "destroy") == 0 && !message->destructor)
795
			fail(&ctx->loc, "destroy request should be destructor type");
796

797
798
		ctx->message = message;
	} else if (strcmp(element_name, "arg") == 0) {
799
		if (name == NULL)
800
			fail(&ctx->loc, "no argument name given");
801

802
		validate_identifier(&ctx->loc, name, STANDALONE_IDENT);
803
804
		arg = create_arg(name);
		if (!set_arg_type(arg, type))
805
			fail(&ctx->loc, "unknown type (%s)", type);
806

807
808
		switch (arg->type) {
		case NEW_ID:
809
			ctx->message->new_id_count++;
810
			/* fallthrough */
811
		case OBJECT:
812
813
814
815
			if (interface_name) {
				validate_identifier(&ctx->loc,
						    interface_name,
						    STANDALONE_IDENT);
816
				arg->interface_name = xstrdup(interface_name);
817
			}
818
819
820
			break;
		default:
			if (interface_name != NULL)
821
				fail(&ctx->loc, "interface attribute not allowed for type %s", type);
822
823
824
			break;
		}

Marek Chalupa's avatar
Marek Chalupa committed
825
826
827
828
829
830
831
		if (allow_null) {
			if (strcmp(allow_null, "true") == 0)
				arg->nullable = 1;
			else if (strcmp(allow_null, "false") != 0)
				fail(&ctx->loc,
				     "invalid value for allow-null attribute (%s)",
				     allow_null);
832

Marek Chalupa's avatar
Marek Chalupa committed
833
834
835
836
			if (!is_nullable_type(arg))
				fail(&ctx->loc,
				     "allow-null is only valid for objects, strings, and arrays");
		}
837

838
839
840
841
842
		if (enumeration_name == NULL || strcmp(enumeration_name, "") == 0)
			arg->enumeration_name = NULL;
		else
			arg->enumeration_name = xstrdup(enumeration_name);

843
		if (summary)
844
			arg->summary = xstrdup(summary);
845

846
		wl_list_insert(ctx->message->arg_list.prev, &arg->link);
847
		ctx->message->arg_count++;
848
	} else if (strcmp(element_name, "enum") == 0) {
849
		if (name == NULL)
850
			fail(&ctx->loc, "no enum name given");
851

852
		validate_identifier(&ctx->loc, name, TRAILING_IDENT);
853
		enumeration = create_enumeration(name);
854
855
856
857
858
859
860

		if (bitfield == NULL || strcmp(bitfield, "false") == 0)
			enumeration->bitfield = false;
		else if (strcmp(bitfield, "true") == 0)
			enumeration->bitfield = true;
		else
			fail(&ctx->loc,
861
862
			     "invalid value (%s) for bitfield attribute (only true/false are accepted)",
			     bitfield);
863

864
865
866
867
868
		wl_list_insert(ctx->interface->enumeration_list.prev,
			       &enumeration->link);

		ctx->enumeration = enumeration;
	} else if (strcmp(element_name, "entry") == 0) {
869
		if (name == NULL)
870
			fail(&ctx->loc, "no entry name given");
871

872
		validate_identifier(&ctx->loc, name, TRAILING_IDENT);
873
		entry = create_entry(name, value);
874
875
876
877
878
879
		version = version_from_since(ctx, since);

		if (version < ctx->enumeration->since)
			warn(&ctx->loc, "since version not increasing\n");
		ctx->enumeration->since = version;
		entry->since = version;
880

881
		if (summary)
882
			entry->summary = xstrdup(summary);
883
884
		else
			entry->summary = NULL;
885
886
		wl_list_insert(ctx->enumeration->entry_list.prev,
			       &entry->link);
887
888
	} else if (strcmp(element_name, "description") == 0) {
		if (summary == NULL)
889
			fail(&ctx->loc, "description without summary");
890

Marek Chalupa's avatar
Marek Chalupa committed
891
		description = xzalloc(sizeof *description);
892
		description->summary = xstrdup(summary);
893
894
895
896
897
898
899
900

		if (ctx->message)
			ctx->message->description = description;
		else if (ctx->enumeration)
			ctx->enumeration->description = description;
		else if (ctx->interface)
			ctx->interface->description = description;
		else
901
			ctx->protocol->description = description;
902
		ctx->description = description;
903
904
905
	}
}

906
907
908
909
910
911
912
913
static struct enumeration *
find_enumeration(struct protocol *protocol,
		 struct interface *interface,
		 char *enum_attribute)
{
	struct interface *i;
	struct enumeration *e;
	char *enum_name;
914
	uint32_t idx = 0, j;
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

	for (j = 0; j + 1 < strlen(enum_attribute); j++) {
		if (enum_attribute[j] == '.') {
			idx = j;
		}
	}

	if (idx > 0) {
		enum_name = enum_attribute + idx + 1;

		wl_list_for_each(i, &protocol->interface_list, link)
			if (strncmp(i->name, enum_attribute, idx) == 0)
				wl_list_for_each(e, &i->enumeration_list, link)
					if (strcmp(e->name, enum_name) == 0)
						return e;
	} else if (interface) {
		enum_name = enum_attribute;

		wl_list_for_each(e, &interface->enumeration_list, link)
			if (strcmp(e->name, enum_name) == 0)
				return e;
	}

	return NULL;
}

941
static void
942
943
944
945
verify_arguments(struct parse_context *ctx,
		 struct interface *interface,
		 struct wl_list *messages,
		 struct wl_list *enumerations)
946
947
948
949
950
{
	struct message *m;
	wl_list_for_each(m, messages, link) {
		struct arg *a;
		wl_list_for_each(a, &m->arg_list, link) {
951
			struct enumeration *e;
952
953
954
955
956

			if (!a->enumeration_name)
				continue;


957
958
959
			e = find_enumeration(ctx->protocol, interface,
					     a->enumeration_name);

960
961
			switch (a->type) {
			case INT:
962
				if (e && e->bitfield)
963
964
965
966
967
968
969
970
971
972
973
974
975
976
					fail(&ctx->loc,
					     "bitfield-style enum must only be referenced by uint");
				break;
			case UNSIGNED:
				break;
			default:
				fail(&ctx->loc,
				     "enumeration-style argument has wrong type");
			}
		}
	}

}

977
978
979
980
981
982
983
984
985
986
987
#ifndef HAVE_STRNDUP
char *
strndup(const char *s, size_t size)
{
	char *r = malloc(size + 1);
	strncpy(r, s, size);
	r[size] = '\0';
	return r;
}
#endif

988
989
990
991
992
993
994
995
996
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);
997
	} else if (strcmp(name, "description") == 0) {
998
999
1000
		ctx->description->text =
			strndup(ctx->character_data,
				ctx->character_data_length);
1001
1002
1003
1004
1005
		ctx->description = NULL;
	} else if (strcmp(name, "request") == 0 ||
		   strcmp(name, "event") == 0) {
		ctx->message = NULL;
	} else if (strcmp(name, "enum") == 0) {
1006
1007
1008
1009
		if (wl_list_empty(&ctx->enumeration->entry_list)) {
			fail(&ctx->loc, "enumeration %s was empty",
			     ctx->enumeration->name);
		}
1010
		ctx->enumeration = NULL;
1011
1012
	} else if (strcmp(name, "protocol") == 0) {
		struct interface *i;
1013

1014
1015
1016
1017
		wl_list_for_each(i, &ctx->protocol->interface_list, link) {
			verify_arguments(ctx, i, &i->request_list, &i->enumeration_list);
			verify_arguments(ctx, i, &i->event_list, &i->enumeration_list);
		}
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
	}
}

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;
}

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
static void
format_text_to_comment(const char *text, bool standalone_comment)
{
	int bol = 1, start = 0, i, length;
	bool comment_started = !standalone_comment;

	length = strlen(text);
	for (i = 0; i <= length; i++) {
		if (bol && (text[i] == ' ' || text[i] == '\t')) {
			continue;
		} else if (bol) {
			bol = 0;
			start = i;
		}
		if (text[i] == '\n' ||
		    (text[i] == '\0' && !(start == i))) {
			printf("%s%s%.*s\n",
			       comment_started ? " *" : "/*",
			       i > start ? " " : "",
			       i - start, text + start);
			bol = 1;
			comment_started = true;
		}
	}
	if (comment_started && standalone_comment)
		printf(" */\n\n");
}

1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
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)
1074
		printf("#define %s_%s %d\n",
1075
1076
1077
1078
1079
		       interface->uppercase_name, m->uppercase_name, opcode++);

	printf("\n");
}

1080
1081
1082
1083
1084
static void
emit_opcode_versions(struct wl_list *message_list, struct interface *interface)
{
	struct message *m;

1085
1086
	wl_list_for_each(m, message_list, link) {
		printf("/**\n * @ingroup iface_%s\n */\n", interface->name);
1087
		printf("#define %s_%s_SINCE_VERSION %d\n",
1088
		       interface->uppercase_name, m->uppercase_name, m->since);
1089
	}
1090
1091
1092
1093

	printf("\n");
}

1094
1095
1096
1097
1098
1099
static void
emit_type(struct arg *a)
{
	switch (a->type) {
	default:
	case INT:
1100
	case FD:
1101
		printf("int32_t ");
1102
1103
1104
1105
1106
		break;
	case NEW_ID:
	case UNSIGNED:
		printf("uint32_t ");
		break;
1107
1108
1109
	case FIXED:
		printf("wl_fixed_t ");
		break;
1110
1111
1112
1113
	case STRING:
		printf("const char *");
		break;
	case OBJECT:
1114
		printf("struct %s *", a->interface_name);
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
		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;
1127
	int has_destructor, has_destroy;
1128

1129
	printf("/** @ingroup iface_%s */\n", interface->name);
1130
	printf("static inline void\n"
1131
	       "%s_set_user_data(struct %s *%s, void *user_data)\n"
1132
1133
1134
1135
1136
1137
	       "{\n"
	       "\twl_proxy_set_user_data((struct wl_proxy *) %s, user_data);\n"
	       "}\n\n",
	       interface->name, interface->name, interface->name,
	       interface->name);

1138
	printf("/** @ingroup iface_%s */\n", interface->name);
1139
	printf("static inline void *\n"
1140
	       "%s_get_user_data(struct %s *%s)\n"
1141
1142
1143
1144
1145
1146
	       "{\n"
	       "\treturn wl_proxy_get_user_data((struct wl_proxy *) %s);\n"
	       "}\n\n",
	       interface->name, interface->name, interface->name,
	       interface->name);

1147
1148
1149
1150
1151
1152
1153
1154
	printf("static inline uint32_t\n"
	       "%s_get_version(struct %s *%s)\n"
	       "{\n"
	       "\treturn wl_proxy_get_version((struct wl_proxy *) %s);\n"
	       "}\n\n",
	       interface->name, interface->name, interface->name,
	       interface->name);

1155
1156
1157
1158
1159
	has_destructor = 0;
	has_destroy = 0;
	wl_list_for_each(m, message_list, link) {
		if (m->destructor)
			has_destructor = 1;
1160
		if (strcmp(m->name, "destroy") == 0)
1161
1162
1163
1164
			has_destroy = 1;
	}

	if (!has_destructor && has_destroy) {
1165
1166
1167
1168
		fail(&interface->loc,
		     "interface '%s' has method named destroy "
		     "but no destructor",
		     interface->name);
1169
1170
1171
		exit(EXIT_FAILURE);
	}

1172
1173
	if (!has_destroy && strcmp(interface->name, "wl_display") != 0) {
		printf("/** @ingroup iface_%s */\n", interface->name);
1174
		printf("static inline void\n"
1175
		       "%s_destroy(struct %s *%s)\n"
1176
1177
1178
1179
1180
1181
		       "{\n"
		       "\twl_proxy_destroy("
		       "(struct wl_proxy *) %s);\n"
		       "}\n\n",
		       interface->name, interface->name, interface->name,
		       interface->name);
1182
	}
1183

1184
1185
1186
1187
	if (wl_list_empty(message_list))
		return;

	wl_list_for_each(m, message_list, link) {
1188
		if (m->new_id_count > 1) {
1189
1190
1191
1192
			warn(&m->loc,
			     "request '%s::%s' has more than "
			     "one new_id arg, not emitting stub\n",
			     interface->name, m->name);
1193
1194
1195
			continue;
		}

1196
1197
1198
1199
1200
1201
		ret = NULL;
		wl_list_for_each(a, &m->arg_list, link) {
			if (a->type == NEW_ID)
				ret = a;
		}

1202
1203
1204
1205
1206
		printf("/**\n"
		       " * @ingroup iface_%s\n", interface->name);
		if (m->description && m->description->text)
			format_text_to_comment(m->description->text, false);
		printf(" */\n");
1207
1208
1209
		if (ret && ret->interface_name == NULL)
			printf("static inline void *\n");
		else if (ret)
1210
			printf("static inline struct %s *\n",
1211
1212
1213
1214
			       ret->interface_name);
		else
			printf("static inline void\n");

1215
		printf("%s_%s(struct %s *%s",
1216
1217
1218
1219
		       interface->name, m->name,
		       interface->name, interface->name);

		wl_list_for_each(a, &m->arg_list, link) {
1220
1221
1222
1223
1224
			if (a->type == NEW_ID && a->interface_name == NULL) {
				printf(", const struct wl_interface *interface"
				       ", uint32_t version");
				continue;
			} else if (a->type == NEW_ID)
1225
1226
1227
1228
1229
1230
1231
1232
				continue;
			printf(", ");
			emit_type(a);
			printf("%s", a->name);
		}

		printf(")\n"
		       "{\n");
1233
1234
1235
		if (ret && ret->interface_name == NULL) {
			/* an arg has type ="new_id" but interface is not
			 * provided, such as in wl_registry.bind */
1236
			printf("\tstruct wl_proxy *%s;\n\n"
1237
			       "\t%s = wl_proxy_marshal_constructor_versioned("
1238
			       "(struct wl_proxy *) %s,\n"
1239
			       "\t\t\t %s_%s, interface, version",
1240
1241
1242
1243
			       ret->name, ret->name,
			       interface->name,
			       interface->uppercase_name,
			       m->uppercase_name);
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
		} else if (ret) {
			/* Normal factory case, an arg has type="new_id" and
			 * an interface is provided */
			printf("\tstruct wl_proxy *%s;\n\n"
			       "\t%s = wl_proxy_marshal_constructor("
			       "(struct wl_proxy *) %s,\n"
			       "\t\t\t %s_%s, &%s_interface",
			       ret->name, ret->name,
			       interface->name,
			       interface->uppercase_name,
			       m->uppercase_name,
			       ret->interface_name);
1256
		} else {
1257
			/* No args have type="new_id" */
1258
1259
1260
1261
1262
			printf("\twl_proxy_marshal((struct wl_proxy *) %s,\n"
			       "\t\t\t %s_%s",
			       interface->name,
			       interface->uppercase_name,
			       m->uppercase_name);
1263
		}
1264
1265

		wl_list_for_each(a, &m->arg_list, link) {
1266
1267
1268
1269
1270
1271
1272
			if (a->type == NEW_ID) {
				if (a->interface_name == NULL)
					printf(", interface->name, version");
				printf(", NULL");
			} else {
				printf(", %s", a->name);
			}