GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/rpcgen/rpc_parse.c Lines: 245 338 72.5 %
Date: 2016-12-06 Branches: 69 123 56.1 %

Line Branch Exec Source
1
/*	$OpenBSD: rpc_parse.c,v 1.20 2016/01/15 10:14:32 jasper Exp $	*/
2
/*	$NetBSD: rpc_parse.c,v 1.5 1995/08/29 23:05:55 cgd Exp $	*/
3
4
/*
5
 * Copyright (c) 2010, Oracle America, Inc.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions are
9
 * met:
10
 *
11
 *     * Redistributions of source code must retain the above copyright
12
 *       notice, this list of conditions and the following disclaimer.
13
 *     * Redistributions in binary form must reproduce the above
14
 *       copyright notice, this list of conditions and the following
15
 *       disclaimer in the documentation and/or other materials
16
 *       provided with the distribution.
17
 *     * Neither the name of the "Oracle America, Inc." nor the names of its
18
 *       contributors may be used to endorse or promote products derived
19
 *       from this software without specific prior written permission.
20
 *
21
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24
 *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
 *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
26
 *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27
 *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
28
 *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
 *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30
 *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31
 *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
 */
34
35
/*
36
 * rpc_parse.c, Parser for the RPC protocol compiler
37
 */
38
#include <stdio.h>
39
#include <stdlib.h>
40
#include <string.h>
41
#include "rpc/types.h"
42
#include "rpc_scan.h"
43
#include "rpc_parse.h"
44
#include "rpc_util.h"
45
46
#define ARGNAME "arg"
47
48
static void isdefined(definition *);
49
static void def_struct(definition *);
50
static void def_program(definition *);
51
static void def_enum(definition *);
52
static void def_const(definition *);
53
static void def_union(definition *);
54
static void def_typedef(definition *);
55
static void get_declaration(declaration *, defkind);
56
static void get_prog_declaration(declaration *, defkind, int);
57
static void get_type(char **, char **, defkind);
58
static void unsigned_dec(char **);
59
60
/*
61
 * return the next definition you see
62
 */
63
definition *
64
get_definition(void)
65
83
{
66
	definition *defp;
67
	token tok;
68
69
83
	defp = malloc(sizeof(definition));
70
83
	get_token(&tok);
71


83
	switch (tok.kind) {
72
	case TOK_STRUCT:
73
57
		def_struct(defp);
74
57
		break;
75
	case TOK_UNION:
76
5
		def_union(defp);
77
5
		break;
78
	case TOK_TYPEDEF:
79
3
		def_typedef(defp);
80
3
		break;
81
	case TOK_ENUM:
82
8
		def_enum(defp);
83
8
		break;
84
	case TOK_PROGRAM:
85
3
		def_program(defp);
86
3
		break;
87
	case TOK_CONST:
88
4
		def_const(defp);
89
4
		break;
90
	case TOK_EOF:
91
3
		free(defp);
92
3
		return (NULL);
93
	default:
94
		error("definition keyword expected");
95
	}
96
80
	scan(TOK_SEMICOLON, &tok);
97
80
	isdefined(defp);
98
80
	return (defp);
99
}
100
101
static void
102
isdefined(defp)
103
	definition *defp;
104
80
{
105
80
	STOREVAL(&defined, defp);
106
80
}
107
108
static void
109
def_struct(defp)
110
	definition *defp;
111
57
{
112
	token tok;
113
	declaration dec;
114
	decl_list *decls;
115
	decl_list **tailp;
116
117
57
	defp->def_kind = DEF_STRUCT;
118
119
57
	scan(TOK_IDENT, &tok);
120
57
	defp->def_name = tok.str;
121
57
	scan(TOK_LBRACE, &tok);
122
57
	tailp = &defp->def.st.decls;
123
	do {
124
191
		get_declaration(&dec, DEF_STRUCT);
125
191
		decls = malloc(sizeof(decl_list));
126
191
		decls->decl = dec;
127
191
		*tailp = decls;
128
191
		tailp = &decls->next;
129
191
		scan(TOK_SEMICOLON, &tok);
130
191
		peek(&tok);
131
191
	} while (tok.kind != TOK_RBRACE);
132
57
	get_token(&tok);
133
57
	*tailp = NULL;
134
57
}
135
136
static void
137
def_program(defp)
138
	definition *defp;
139
3
{
140
	token tok;
141
	declaration dec;
142
	decl_list *decls;
143
	decl_list **tailp;
144
	version_list *vlist;
145
	version_list **vtailp;
146
	proc_list *plist;
147
	proc_list **ptailp;
148
	int num_args;
149
3
	bool_t isvoid = FALSE; /* whether first argument is void */
150
3
	defp->def_kind = DEF_PROGRAM;
151
3
	scan(TOK_IDENT, &tok);
152
3
	defp->def_name = tok.str;
153
3
	scan(TOK_LBRACE, &tok);
154
3
	vtailp = &defp->def.pr.versions;
155
3
	tailp = &defp->def.st.decls;
156
3
	scan(TOK_VERSION, &tok);
157
	do {
158
9
		scan(TOK_IDENT, &tok);
159
9
		vlist = malloc(sizeof(version_list));
160
9
		vlist->vers_name = tok.str;
161
9
		scan(TOK_LBRACE, &tok);
162
9
		ptailp = &vlist->procs;
163
		do {
164
			/* get result type */
165
80
			plist = malloc(sizeof(proc_list));
166
80
			get_type(&plist->res_prefix, &plist->res_type,
167
			    DEF_PROGRAM);
168
80
			if (streq(plist->res_type, "opaque")) {
169
				error("illegal result type");
170
			}
171
80
			scan(TOK_IDENT, &tok);
172
80
			plist->proc_name = tok.str;
173
80
			scan(TOK_LPAREN, &tok);
174
			/* get args - first one*/
175
80
			num_args = 1;
176
80
			isvoid = FALSE;
177
			/* type of DEF_PROGRAM in the first
178
			 * get_prog_declaration and DEF_STURCT in the next
179
			 * allows void as argument if it is the only argument
180
			 */
181
80
			get_prog_declaration(&dec, DEF_PROGRAM, num_args);
182
80
			if (streq(dec.type, "void"))
183
				isvoid = TRUE;
184
80
			decls = malloc(sizeof(decl_list));
185
80
			plist->args.decls = decls;
186
80
			decls->decl = dec;
187
80
			tailp = &decls->next;
188
			/* get args */
189
160
			while (peekscan(TOK_COMMA, &tok)) {
190
				num_args++;
191
				get_prog_declaration(&dec, DEF_STRUCT,
192
				    num_args);
193
				decls = malloc(sizeof(decl_list));
194
				decls->decl = dec;
195
				*tailp = decls;
196
				if (streq(dec.type, "void"))
197
					isvoid = TRUE;
198
				tailp = &decls->next;
199
			}
200
			/* multiple arguments are only allowed in newstyle */
201
80
			if (!newstyle && num_args > 1) {
202
				error("only one argument is allowed");
203
			}
204
80
			if (isvoid && num_args > 1) {
205
				error("illegal use of void in program definition");
206
			}
207
80
			*tailp = NULL;
208
80
			scan(TOK_RPAREN, &tok);
209
80
			scan(TOK_EQUAL, &tok);
210
80
			scan_num(&tok);
211
80
			scan(TOK_SEMICOLON, &tok);
212
80
			plist->proc_num = tok.str;
213
80
			plist->arg_num = num_args;
214
80
			*ptailp = plist;
215
80
			ptailp = &plist->next;
216
80
			peek(&tok);
217
80
		} while (tok.kind != TOK_RBRACE);
218
9
		*ptailp = NULL;
219
9
		*vtailp = vlist;
220
9
		vtailp = &vlist->next;
221
9
		scan(TOK_RBRACE, &tok);
222
9
		scan(TOK_EQUAL, &tok);
223
9
		scan_num(&tok);
224
9
		vlist->vers_num = tok.str;
225
		/* make the argument structure name for each arg*/
226
98
		for (plist = vlist->procs; plist != NULL;
227
80
		    plist = plist->next) {
228
80
			plist->args.argname = make_argname(plist->proc_name,
229
			    vlist->vers_num);
230
			/* free the memory ??*/
231
		}
232
9
		scan(TOK_SEMICOLON, &tok);
233
9
		scan2(TOK_VERSION, TOK_RBRACE, &tok);
234
9
	} while (tok.kind == TOK_VERSION);
235
3
	scan(TOK_EQUAL, &tok);
236
3
	scan_num(&tok);
237
3
	defp->def.pr.prog_num = tok.str;
238
3
	*vtailp = NULL;
239
3
}
240
241
242
static void
243
def_enum(defp)
244
	definition *defp;
245
8
{
246
	token tok;
247
	enumval_list *elist;
248
	enumval_list **tailp;
249
250
8
	defp->def_kind = DEF_ENUM;
251
8
	scan(TOK_IDENT, &tok);
252
8
	defp->def_name = tok.str;
253
8
	scan(TOK_LBRACE, &tok);
254
8
	tailp = &defp->def.en.vals;
255
	do {
256
46
		scan(TOK_IDENT, &tok);
257
46
		elist = malloc(sizeof(enumval_list));
258
46
		elist->name = tok.str;
259
46
		elist->assignment = NULL;
260
46
		scan3(TOK_COMMA, TOK_RBRACE, TOK_EQUAL, &tok);
261
46
		if (tok.kind == TOK_EQUAL) {
262
46
			scan_num(&tok);
263
46
			elist->assignment = tok.str;
264
46
			scan2(TOK_COMMA, TOK_RBRACE, &tok);
265
		}
266
46
		*tailp = elist;
267
46
		tailp = &elist->next;
268
46
	} while (tok.kind != TOK_RBRACE);
269
8
	*tailp = NULL;
270
8
}
271
272
static void
273
def_const(defp)
274
	definition *defp;
275
4
{
276
	token tok;
277
278
4
	defp->def_kind = DEF_CONST;
279
4
	scan(TOK_IDENT, &tok);
280
4
	defp->def_name = tok.str;
281
4
	scan(TOK_EQUAL, &tok);
282
4
	scan2(TOK_IDENT, TOK_STRCONST, &tok);
283
4
	defp->def.co = tok.str;
284
4
}
285
286
static void
287
def_union(defp)
288
	definition *defp;
289
5
{
290
	token tok;
291
	declaration dec;
292
	case_list *cases;
293
	case_list **tailp;
294
	int flag;
295
296
5
	defp->def_kind = DEF_UNION;
297
5
	scan(TOK_IDENT, &tok);
298
5
	defp->def_name = tok.str;
299
5
	scan(TOK_SWITCH, &tok);
300
5
	scan(TOK_LPAREN, &tok);
301
5
	get_declaration(&dec, DEF_UNION);
302
5
	defp->def.un.enum_decl = dec;
303
5
	tailp = &defp->def.un.cases;
304
5
	scan(TOK_RPAREN, &tok);
305
5
	scan(TOK_LBRACE, &tok);
306
5
	scan(TOK_CASE, &tok);
307
15
	while (tok.kind == TOK_CASE) {
308
5
		scan2(TOK_IDENT, TOK_CHARCONST, &tok);
309
5
		cases = malloc(sizeof(case_list));
310
5
		cases->case_name = tok.str;
311
5
		scan(TOK_COLON, &tok);
312
		/* now peek at next token */
313
5
		flag=0;
314
5
		if (peekscan(TOK_CASE,&tok)) {
315
			do {
316
				scan2(TOK_IDENT, TOK_CHARCONST, &tok);
317
				cases->contflag=1;	/* continued case statement */
318
				*tailp = cases;
319
				tailp = &cases->next;
320
				cases = malloc(sizeof(case_list));
321
				cases->case_name = tok.str;
322
				scan(TOK_COLON, &tok);
323
			} while (peekscan(TOK_CASE,&tok));
324
5
		} else if (flag) {
325
			*tailp = cases;
326
			tailp = &cases->next;
327
			cases = malloc(sizeof(case_list));
328
		}
329
5
		get_declaration(&dec, DEF_UNION);
330
5
		cases->case_decl = dec;
331
5
		cases->contflag=0;		/* no continued case statement */
332
5
		*tailp = cases;
333
5
		tailp = &cases->next;
334
5
		scan(TOK_SEMICOLON, &tok);
335
336
5
		scan3(TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok);
337
	}
338
5
	*tailp = NULL;
339
340
5
	if (tok.kind == TOK_DEFAULT) {
341
4
		scan(TOK_COLON, &tok);
342
4
		get_declaration(&dec, DEF_UNION);
343
4
		defp->def.un.default_decl = malloc(sizeof(declaration));
344
4
		*defp->def.un.default_decl = dec;
345
4
		scan(TOK_SEMICOLON, &tok);
346
4
		scan(TOK_RBRACE, &tok);
347
	} else {
348
1
		defp->def.un.default_decl = NULL;
349
	}
350
5
}
351
352
static char *reserved_words[] = {
353
	"array",
354
	"bytes",
355
	"destroy",
356
	"free",
357
	"getpos",
358
	"inline",
359
	"pointer",
360
	"reference",
361
	"setpos",
362
	"sizeof",
363
	"union",
364
	"vector",
365
	NULL
366
};
367
368
static char *reserved_types[] = {
369
	"opaque",
370
	"string",
371
	NULL
372
};
373
374
/* check that the given name is not one that would eventually result in
375
   xdr routines that would conflict with internal XDR routines. */
376
static void
377
check_type_name(char *name, int new_type)
378
207
{
379
	int i;
380
	char tmp[100];
381
382
2691
	for (i = 0; reserved_words[i] != NULL; i++) {
383
2484
		if (strcmp(name, reserved_words[i]) == 0) {
384
			snprintf(tmp, sizeof tmp,
385
			    "illegal (reserved) name :\'%s\' in type definition", name);
386
			error(tmp);
387
		}
388
	}
389
207
	if (new_type) {
390
9
		for (i = 0; reserved_types[i] != NULL; i++) {
391
6
			if (strcmp(name, reserved_types[i]) == 0) {
392
				snprintf(tmp, sizeof tmp,
393
				    "illegal (reserved) name :\'%s\' in"
394
				    " type definition", name);
395
				error(tmp);
396
			}
397
		}
398
	}
399
207
}
400
401
static void
402
def_typedef(defp)
403
	definition *defp;
404
3
{
405
	declaration dec;
406
407
3
	defp->def_kind = DEF_TYPEDEF;
408
3
	get_declaration(&dec, DEF_TYPEDEF);
409
3
	defp->def_name = dec.name;
410
3
	check_type_name(dec.name, 1);
411
3
	defp->def.ty.old_prefix = dec.prefix;
412
3
	defp->def.ty.old_type = dec.type;
413
3
	defp->def.ty.rel = dec.rel;
414
3
	defp->def.ty.array_max = dec.array_max;
415
3
}
416
417
static void
418
get_declaration(dec, dkind)
419
	declaration *dec;
420
	defkind dkind;
421
208
{
422
	token tok;
423
424
208
	get_type(&dec->prefix, &dec->type, dkind);
425
208
	dec->rel = REL_ALIAS;
426
208
	if (streq(dec->type, "void")) {
427
4
		return;
428
	}
429
430
204
	check_type_name(dec->type, 0);
431
432
204
	scan2(TOK_STAR, TOK_IDENT, &tok);
433
204
	if (tok.kind == TOK_STAR) {
434
		dec->rel = REL_POINTER;
435
		scan(TOK_IDENT, &tok);
436
	}
437
204
	dec->name = tok.str;
438
204
	if (peekscan(TOK_LBRACKET, &tok)) {
439
2
		if (dec->rel == REL_POINTER) {
440
			error("no array-of-pointer declarations -- use typedef");
441
		}
442
2
		dec->rel = REL_VECTOR;
443
2
		scan_num(&tok);
444
2
		dec->array_max = tok.str;
445
2
		scan(TOK_RBRACKET, &tok);
446
202
	} else if (peekscan(TOK_LANGLE, &tok)) {
447
15
		if (dec->rel == REL_POINTER) {
448
			error("no array-of-pointer declarations -- use typedef");
449
		}
450
15
		dec->rel = REL_ARRAY;
451
15
		if (peekscan(TOK_RANGLE, &tok)) {
452
			dec->array_max = "~0";	/* unspecified size, use max */
453
		} else {
454
15
			scan_num(&tok);
455
15
			dec->array_max = tok.str;
456
15
			scan(TOK_RANGLE, &tok);
457
		}
458
	}
459
204
	if (streq(dec->type, "opaque")) {
460
2
		if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR) {
461
			error("array declaration expected");
462
		}
463
202
	} else if (streq(dec->type, "string")) {
464
15
		if (dec->rel != REL_ARRAY) {
465
			error("variable-length array declaration expected");
466
		}
467
	}
468
}
469
470
static void
471
get_prog_declaration(dec, dkind, num)
472
	declaration *dec;
473
	defkind dkind;
474
	int num;  /* arg number */
475
80
{
476
	token tok;
477
478
80
	if (dkind == DEF_PROGRAM) {
479
80
		peek(&tok);
480
80
		if (tok.kind == TOK_RPAREN) { /* no arguments */
481
			dec->rel = REL_ALIAS;
482
			dec->type = "void";
483
			dec->prefix = NULL;
484
			dec->name = NULL;
485
			return;
486
		}
487
	}
488
80
	get_type(&dec->prefix, &dec->type, dkind);
489
80
	dec->rel = REL_ALIAS;
490
80
	if (peekscan(TOK_IDENT, &tok)) {  /* optional name of argument */
491
		dec->name = (char *)strdup(tok.str);
492
		if (dec->name == NULL)
493
			error("out of memory");
494
	} else {
495
		/* default name of argument */
496
80
		if (asprintf(&dec->name, "%s%d", ARGNAME, num) == -1)
497
			error("out of memory");
498
	}
499
500
80
	if (streq(dec->type, "void"))
501
		return;
502
503
80
	if (streq(dec->type, "opaque"))
504
		error("opaque -- illegal argument type");
505
506
80
	if (peekscan(TOK_STAR, &tok)) {
507
		if (streq(dec->type, "string"))
508
			error("pointer to string not allowed in program arguments\n");
509
510
		dec->rel = REL_POINTER;
511
		if (peekscan(TOK_IDENT, &tok)) { /* optional name of argument */
512
			dec->name = (char *)strdup(tok.str);
513
			if (dec->name == NULL)
514
				error("out of memory");
515
		}
516
	}
517
80
	if (peekscan(TOK_LANGLE, &tok)) {
518
		if (!streq(dec->type, "string"))
519
			error("arrays cannot be declared as arguments to "
520
			    "procedures -- use typedef");
521
		dec->rel = REL_ARRAY;
522
		if (peekscan(TOK_RANGLE, &tok)) {
523
			dec->array_max = "~0";/* unspecified size, use max */
524
		} else {
525
			scan_num(&tok);
526
			dec->array_max = tok.str;
527
			scan(TOK_RANGLE, &tok);
528
		}
529
	}
530
80
	if (streq(dec->type, "string")) {
531
		/* .x specifies just string as
532
		 * type of argument
533
		 * - make it string<>
534
		 */
535
		if (dec->rel != REL_ARRAY) {
536
			dec->rel = REL_ARRAY;
537
			dec->array_max = "~0";/* unspecified size, use max */
538
		}
539
	}
540
}
541
542
static void
543
get_type(prefixp, typep, dkind)
544
	char **prefixp;
545
	char **typep;
546
	defkind dkind;
547
368
{
548
	token tok;
549
550
368
	*prefixp = NULL;
551
368
	get_token(&tok);
552


368
	switch (tok.kind) {
553
	case TOK_IDENT:
554
198
		*typep = tok.str;
555
198
		break;
556
	case TOK_STRUCT:
557
	case TOK_ENUM:
558
	case TOK_UNION:
559
42
		*prefixp = tok.str;
560
42
		scan(TOK_IDENT, &tok);
561
42
		*typep = tok.str;
562
42
		break;
563
	case TOK_UNSIGNED:
564
8
		unsigned_dec(typep);
565
8
		break;
566
	case TOK_SHORT:
567
		*typep = "short";
568
		(void) peekscan(TOK_INT, &tok);
569
		break;
570
	case TOK_LONG:
571
		*typep = "long";
572
		(void) peekscan(TOK_INT, &tok);
573
		break;
574
	case TOK_HYPER:
575
		*typep = "int64_t";
576
		(void) peekscan(TOK_INT, &tok);
577
		break;
578
	case TOK_VOID:
579
50
		if (dkind != DEF_UNION && dkind != DEF_PROGRAM) {
580
			error("voids allowed only inside union and program definitions with one argument");
581
		}
582
50
		*typep = tok.str;
583
50
		break;
584
	case TOK_STRING:
585
	case TOK_OPAQUE:
586
	case TOK_CHAR:
587
	case TOK_INT:
588
	case TOK_FLOAT:
589
	case TOK_DOUBLE:
590
	case TOK_QUAD:
591
	case TOK_BOOL:
592
70
		*typep = tok.str;
593
70
		break;
594
	default:
595
		error("expected type specifier");
596
	}
597
368
}
598
599
static void
600
unsigned_dec(typep)
601
	char **typep;
602
8
{
603
	token tok;
604
605
8
	peek(&tok);
606

8
	switch (tok.kind) {
607
	case TOK_CHAR:
608
		get_token(&tok);
609
		*typep = "u_char";
610
		break;
611
	case TOK_SHORT:
612
		get_token(&tok);
613
		*typep = "u_short";
614
		(void) peekscan(TOK_INT, &tok);
615
		break;
616
	case TOK_LONG:
617
		get_token(&tok);
618
		*typep = "u_long";
619
		(void) peekscan(TOK_INT, &tok);
620
		break;
621
	case TOK_HYPER:
622
		get_token(&tok);
623
		*typep = "u_int64_t";
624
		(void) peekscan(TOK_INT, &tok);
625
		break;
626
	case TOK_INT:
627
		get_token(&tok);
628
		*typep = "u_int";
629
		break;
630
	default:
631
8
		*typep = "u_int";
632
		break;
633
	}
634
8
}