xref: /netbsd-src/usr.bin/rpcgen/rpc_parse.c (revision f81322cf185a4db50f71fcf7701f20198272620e)
1 /*	$NetBSD: rpc_parse.c,v 1.14 2006/03/20 17:03:08 elad Exp $	*/
2 /*
3  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4  * unrestricted use provided that this legend is included on all tape
5  * media and as a part of the software program in whole or part.  Users
6  * may copy or modify Sun RPC without charge, but are not authorized
7  * to license or distribute it to anyone else except as part of a product or
8  * program developed by the user or with the express written consent of
9  * Sun Microsystems, Inc.
10  *
11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14  *
15  * Sun RPC is provided with no support and without any obligation on the
16  * part of Sun Microsystems, Inc. to assist in its use, correction,
17  * modification or enhancement.
18  *
19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21  * OR ANY PART THEREOF.
22  *
23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24  * or profits or other special, indirect and consequential damages, even if
25  * Sun has been advised of the possibility of such damages.
26  *
27  * Sun Microsystems, Inc.
28  * 2550 Garcia Avenue
29  * Mountain View, California  94043
30  */
31 
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
35 
36 #include <sys/cdefs.h>
37 #if defined(__RCSID) && !defined(lint)
38 #if 0
39 static char sccsid[] = "@(#)rpc_parse.c 1.8 89/02/22 (C) 1987 SMI";
40 #else
41 __RCSID("$NetBSD: rpc_parse.c,v 1.14 2006/03/20 17:03:08 elad Exp $");
42 #endif
43 #endif
44 
45 /*
46  * rpc_parse.c, Parser for the RPC protocol compiler
47  * Copyright (C) 1987 Sun Microsystems, Inc.
48  */
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include "rpc/types.h"
53 #include "rpc_scan.h"
54 #include "rpc_parse.h"
55 #include "rpc_util.h"
56 
57 #define ARGNAME "arg"
58 
59 static void isdefined __P((definition *));
60 static void def_struct __P((definition *));
61 static void def_program __P((definition *));
62 static void def_enum __P((definition *));
63 static void def_const __P((definition *));
64 static void def_union __P((definition *));
65 static void check_type_name __P((char *, int));
66 static void def_typedef __P((definition *));
67 static void get_declaration __P((declaration *, defkind));
68 static void get_prog_declaration __P((declaration *, defkind, int));
69 static void get_type __P((char **, char **, defkind));
70 static void unsigned_dec __P((char **));
71 
72 /*
73  * return the next definition you see
74  */
75 definition *
76 get_definition()
77 {
78 	definition *defp;
79 	token   tok;
80 
81 	defp = ALLOC(definition);
82 	get_token(&tok);
83 	switch (tok.kind) {
84 	case TOK_STRUCT:
85 		def_struct(defp);
86 		break;
87 	case TOK_UNION:
88 		def_union(defp);
89 		break;
90 	case TOK_TYPEDEF:
91 		def_typedef(defp);
92 		break;
93 	case TOK_ENUM:
94 		def_enum(defp);
95 		break;
96 	case TOK_PROGRAM:
97 		def_program(defp);
98 		break;
99 	case TOK_CONST:
100 		def_const(defp);
101 		break;
102 	case TOK_EOF:
103 		free(defp);
104 		return (NULL);
105 	default:
106 		error("definition keyword expected");
107 	}
108 	scan(TOK_SEMICOLON, &tok);
109 	isdefined(defp);
110 	return (defp);
111 }
112 
113 static void
114 isdefined(defp)
115 	definition *defp;
116 {
117 	STOREVAL(&defined, defp);
118 }
119 
120 static void
121 def_struct(defp)
122 	definition *defp;
123 {
124 	token   tok;
125 	declaration dec;
126 	decl_list *decls;
127 	decl_list **tailp;
128 
129 	defp->def_kind = DEF_STRUCT;
130 
131 	scan(TOK_IDENT, &tok);
132 	defp->def_name = tok.str;
133 	scan(TOK_LBRACE, &tok);
134 	tailp = &defp->def.st.decls;
135 	do {
136 		get_declaration(&dec, DEF_STRUCT);
137 		decls = ALLOC(decl_list);
138 		decls->decl = dec;
139 		*tailp = decls;
140 		tailp = &decls->next;
141 		scan(TOK_SEMICOLON, &tok);
142 		peek(&tok);
143 	} while (tok.kind != TOK_RBRACE);
144 	get_token(&tok);
145 	*tailp = NULL;
146 }
147 
148 static void
149 def_program(defp)
150 	definition *defp;
151 {
152 	token   tok;
153 	declaration dec;
154 	decl_list *decls;
155 	decl_list **tailp;
156 	version_list *vlist;
157 	version_list **vtailp;
158 	proc_list *plist;
159 	proc_list **ptailp;
160 	int     num_args;
161 	bool_t  isvoid = FALSE;	/* whether first argument is void */
162 	defp->def_kind = DEF_PROGRAM;
163 	scan(TOK_IDENT, &tok);
164 	defp->def_name = tok.str;
165 	scan(TOK_LBRACE, &tok);
166 	vtailp = &defp->def.pr.versions;
167 	tailp = &defp->def.st.decls;
168 	scan(TOK_VERSION, &tok);
169 	do {
170 		scan(TOK_IDENT, &tok);
171 		vlist = ALLOC(version_list);
172 		vlist->vers_name = tok.str;
173 		scan(TOK_LBRACE, &tok);
174 		ptailp = &vlist->procs;
175 		do {
176 			/* get result type */
177 			plist = ALLOC(proc_list);
178 			get_type(&plist->res_prefix, &plist->res_type,
179 			    DEF_PROGRAM);
180 			if (streq(plist->res_type, "opaque")) {
181 				error("illegal result type");
182 			}
183 			scan(TOK_IDENT, &tok);
184 			plist->proc_name = tok.str;
185 			scan(TOK_LPAREN, &tok);
186 			/* get args - first one */
187 			num_args = 1;
188 			isvoid = FALSE;
189 			/* type of DEF_PROGRAM in the first
190 			 * get_prog_declaration and DEF_STURCT in the next
191 			 * allows void as argument if it is the only argument */
192 			get_prog_declaration(&dec, DEF_PROGRAM, num_args);
193 			if (streq(dec.type, "void"))
194 				isvoid = TRUE;
195 			decls = ALLOC(decl_list);
196 			plist->args.decls = decls;
197 			decls->decl = dec;
198 			tailp = &decls->next;
199 			/* get args */
200 			while (peekscan(TOK_COMMA, &tok)) {
201 				num_args++;
202 				get_prog_declaration(&dec, DEF_STRUCT,
203 				    num_args);
204 				decls = ALLOC(decl_list);
205 				decls->decl = dec;
206 				*tailp = decls;
207 				if (streq(dec.type, "void"))
208 					isvoid = TRUE;
209 				tailp = &decls->next;
210 			}
211 			/* multiple arguments are only allowed in newstyle */
212 			if (!newstyle && num_args > 1) {
213 				error("only one argument is allowed");
214 			}
215 			if (isvoid && num_args > 1) {
216 				error("illegal use of void in program definition");
217 			}
218 			*tailp = NULL;
219 			scan(TOK_RPAREN, &tok);
220 			scan(TOK_EQUAL, &tok);
221 			scan_num(&tok);
222 			scan(TOK_SEMICOLON, &tok);
223 			plist->proc_num = tok.str;
224 			plist->arg_num = num_args;
225 			*ptailp = plist;
226 			ptailp = &plist->next;
227 			peek(&tok);
228 		} while (tok.kind != TOK_RBRACE);
229 		*ptailp = NULL;
230 		*vtailp = vlist;
231 		vtailp = &vlist->next;
232 		scan(TOK_RBRACE, &tok);
233 		scan(TOK_EQUAL, &tok);
234 		scan_num(&tok);
235 		vlist->vers_num = tok.str;
236 		/* make the argument structure name for each arg */
237 		for (plist = vlist->procs; plist != NULL;
238 		    plist = plist->next) {
239 			plist->args.argname = make_argname(plist->proc_name,
240 			    vlist->vers_num);
241 			/* free the memory ?? */
242 		}
243 		scan(TOK_SEMICOLON, &tok);
244 		scan2(TOK_VERSION, TOK_RBRACE, &tok);
245 	} while (tok.kind == TOK_VERSION);
246 	scan(TOK_EQUAL, &tok);
247 	scan_num(&tok);
248 	defp->def.pr.prog_num = tok.str;
249 	*vtailp = NULL;
250 }
251 
252 
253 static void
254 def_enum(defp)
255 	definition *defp;
256 {
257 	token   tok;
258 	enumval_list *elist;
259 	enumval_list **tailp;
260 
261 	defp->def_kind = DEF_ENUM;
262 	scan(TOK_IDENT, &tok);
263 	defp->def_name = tok.str;
264 	scan(TOK_LBRACE, &tok);
265 	tailp = &defp->def.en.vals;
266 	do {
267 		scan(TOK_IDENT, &tok);
268 		elist = ALLOC(enumval_list);
269 		elist->name = tok.str;
270 		elist->assignment = NULL;
271 		scan3(TOK_COMMA, TOK_RBRACE, TOK_EQUAL, &tok);
272 		if (tok.kind == TOK_EQUAL) {
273 			scan_num(&tok);
274 			elist->assignment = tok.str;
275 			scan2(TOK_COMMA, TOK_RBRACE, &tok);
276 		}
277 		*tailp = elist;
278 		tailp = &elist->next;
279 	} while (tok.kind != TOK_RBRACE);
280 	*tailp = NULL;
281 }
282 
283 static void
284 def_const(defp)
285 	definition *defp;
286 {
287 	token   tok;
288 
289 	defp->def_kind = DEF_CONST;
290 	scan(TOK_IDENT, &tok);
291 	defp->def_name = tok.str;
292 	scan(TOK_EQUAL, &tok);
293 	scan2(TOK_IDENT, TOK_STRCONST, &tok);
294 	defp->def.co = tok.str;
295 }
296 
297 static void
298 def_union(defp)
299 	definition *defp;
300 {
301 	token   tok;
302 	declaration dec;
303 	case_list *cases;
304 	case_list **tailp;
305 	int     flag;
306 
307 	defp->def_kind = DEF_UNION;
308 	scan(TOK_IDENT, &tok);
309 	defp->def_name = tok.str;
310 	scan(TOK_SWITCH, &tok);
311 	scan(TOK_LPAREN, &tok);
312 	get_declaration(&dec, DEF_UNION);
313 	defp->def.un.enum_decl = dec;
314 	tailp = &defp->def.un.cases;
315 	scan(TOK_RPAREN, &tok);
316 	scan(TOK_LBRACE, &tok);
317 	scan(TOK_CASE, &tok);
318 	while (tok.kind == TOK_CASE) {
319 		scan2(TOK_IDENT, TOK_CHARCONST, &tok);
320 		cases = ALLOC(case_list);
321 		cases->case_name = tok.str;
322 		scan(TOK_COLON, &tok);
323 		/* now peek at next token */
324 		flag = 0;
325 		if (peekscan(TOK_CASE, &tok)) {
326 
327 			do {
328 				scan2(TOK_IDENT, TOK_CHARCONST, &tok);
329 				cases->contflag = 1;	/* continued case
330 							 * statement */
331 				*tailp = cases;
332 				tailp = &cases->next;
333 				cases = ALLOC(case_list);
334 				cases->case_name = tok.str;
335 				scan(TOK_COLON, &tok);
336 
337 			} while (peekscan(TOK_CASE, &tok));
338 		} else
339 			if (flag) {
340 
341 				*tailp = cases;
342 				tailp = &cases->next;
343 				cases = ALLOC(case_list);
344 			};
345 
346 		get_declaration(&dec, DEF_UNION);
347 		cases->case_decl = dec;
348 		cases->contflag = 0;	/* no continued case statement */
349 		*tailp = cases;
350 		tailp = &cases->next;
351 		scan(TOK_SEMICOLON, &tok);
352 
353 		scan3(TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok);
354 	}
355 	*tailp = NULL;
356 	if (tok.kind == TOK_DEFAULT) {
357 		scan(TOK_COLON, &tok);
358 		get_declaration(&dec, DEF_UNION);
359 		defp->def.un.default_decl = ALLOC(declaration);
360 		*defp->def.un.default_decl = dec;
361 		scan(TOK_SEMICOLON, &tok);
362 		scan(TOK_RBRACE, &tok);
363 	} else {
364 		defp->def.un.default_decl = NULL;
365 	}
366 }
367 
368 static char *reserved_words[] = {
369 	"array",
370 	"bytes",
371 	"destroy",
372 	"free",
373 	"getpos",
374 	"inline",
375 	"pointer",
376 	"reference",
377 	"setpos",
378 	"sizeof",
379 	"union",
380 	"vector",
381 	NULL
382 };
383 
384 static char *reserved_types[] = {
385 	"opaque",
386 	"string",
387 	NULL
388 };
389 /* check that the given name is not one that would eventually result in
390    xdr routines that would conflict with internal XDR routines. */
391 static void
392 check_type_name(name, new_type)
393 	int     new_type;
394 	char   *name;
395 {
396 	int     i;
397 	char    tmp[100];
398 
399 	for (i = 0; reserved_words[i] != NULL; i++) {
400 		if (strcmp(name, reserved_words[i]) == 0) {
401 			sprintf(tmp,
402 			    "illegal (reserved) name :\'%s\' in type definition", name);
403 			error(tmp);
404 		}
405 	}
406 	if (new_type) {
407 		for (i = 0; reserved_types[i] != NULL; i++) {
408 			if (strcmp(name, reserved_types[i]) == 0) {
409 				sprintf(tmp,
410 				    "illegal (reserved) name :\'%s\' in type definition", name);
411 				error(tmp);
412 			}
413 		}
414 	}
415 }
416 
417 static void
418 def_typedef(defp)
419 	definition *defp;
420 {
421 	declaration dec;
422 
423 	defp->def_kind = DEF_TYPEDEF;
424 	get_declaration(&dec, DEF_TYPEDEF);
425 	defp->def_name = dec.name;
426 	check_type_name(dec.name, 1);
427 	defp->def.ty.old_prefix = dec.prefix;
428 	defp->def.ty.old_type = dec.type;
429 	defp->def.ty.rel = dec.rel;
430 	defp->def.ty.array_max = dec.array_max;
431 }
432 
433 static void
434 get_declaration(dec, dkind)
435 	declaration *dec;
436 	defkind dkind;
437 {
438 	token   tok;
439 
440 	get_type(&dec->prefix, &dec->type, dkind);
441 	dec->rel = REL_ALIAS;
442 	if (streq(dec->type, "void")) {
443 		return;
444 	}
445 	check_type_name(dec->type, 0);
446 
447 	scan2(TOK_STAR, TOK_IDENT, &tok);
448 	if (tok.kind == TOK_STAR) {
449 		dec->rel = REL_POINTER;
450 		scan(TOK_IDENT, &tok);
451 	}
452 	dec->name = tok.str;
453 	if (peekscan(TOK_LBRACKET, &tok)) {
454 		if (dec->rel == REL_POINTER) {
455 			error("no array-of-pointer declarations -- use typedef");
456 		}
457 		dec->rel = REL_VECTOR;
458 		scan_num(&tok);
459 		dec->array_max = tok.str;
460 		scan(TOK_RBRACKET, &tok);
461 	} else
462 		if (peekscan(TOK_LANGLE, &tok)) {
463 			if (dec->rel == REL_POINTER) {
464 				error("no array-of-pointer declarations -- use typedef");
465 			}
466 			dec->rel = REL_ARRAY;
467 			if (peekscan(TOK_RANGLE, &tok)) {
468 				dec->array_max = "(u_int)~0";
469 				/* unspecified size, use * max */
470 			} else {
471 				scan_num(&tok);
472 				dec->array_max = tok.str;
473 				scan(TOK_RANGLE, &tok);
474 			}
475 		}
476 	if (streq(dec->type, "opaque")) {
477 		if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR) {
478 			error("array declaration expected");
479 		}
480 	} else
481 		if (streq(dec->type, "string")) {
482 			if (dec->rel != REL_ARRAY) {
483 				error("variable-length array declaration expected");
484 			}
485 		}
486 }
487 
488 static void
489 get_prog_declaration(dec, dkind, num)
490 	declaration *dec;
491 	defkind dkind;
492 	int     num;		/* arg number */
493 {
494 	token   tok;
495 	char    name[255];	/* argument name */
496 
497 	if (dkind == DEF_PROGRAM) {
498 		peek(&tok);
499 		if (tok.kind == TOK_RPAREN) {	/* no arguments */
500 			dec->rel = REL_ALIAS;
501 			dec->type = "void";
502 			dec->prefix = NULL;
503 			dec->name = NULL;
504 			return;
505 		}
506 	}
507 	get_type(&dec->prefix, &dec->type, dkind);
508 	dec->rel = REL_ALIAS;
509 	if (peekscan(TOK_IDENT, &tok))	/* optional name of argument */
510 		strcpy(name, tok.str);
511 	else
512 		sprintf(name, "%s%d", ARGNAME, num);	/* default name of
513 							 * argument */
514 
515 	dec->name = (char *) strdup(name);
516 
517 	if (streq(dec->type, "void")) {
518 		return;
519 	}
520 	if (streq(dec->type, "opaque")) {
521 		error("opaque -- illegal argument type");
522 	}
523 	if (peekscan(TOK_STAR, &tok)) {
524 		if (streq(dec->type, "string")) {
525 			error("pointer to string not allowed in program arguments\n");
526 		}
527 		dec->rel = REL_POINTER;
528 		if (peekscan(TOK_IDENT, &tok))	/* optional name of argument */
529 			dec->name = (char *) strdup(tok.str);
530 	}
531 	if (peekscan(TOK_LANGLE, &tok)) {
532 		if (!streq(dec->type, "string")) {
533 			error("arrays cannot be declared as arguments to procedures -- use typedef");
534 		}
535 		dec->rel = REL_ARRAY;
536 		if (peekscan(TOK_RANGLE, &tok)) {
537 			dec->array_max = "(u_int)~0";
538 			/* unspecified size, use max */
539 		} else {
540 			scan_num(&tok);
541 			dec->array_max = tok.str;
542 			scan(TOK_RANGLE, &tok);
543 		}
544 	}
545 	if (streq(dec->type, "string")) {
546 		if (dec->rel != REL_ARRAY) {	/* .x specifies just string as
547 						 * type of argument - make it
548 						 * string<> */
549 			dec->rel = REL_ARRAY;
550 			dec->array_max = "(u_int)~0";
551 			/* unspecified size, use max */
552 		}
553 	}
554 }
555 
556 
557 
558 static void
559 get_type(prefixp, typep, dkind)
560 	char  **prefixp;
561 	char  **typep;
562 	defkind dkind;
563 {
564 	token   tok;
565 
566 	*prefixp = NULL;
567 	get_token(&tok);
568 	switch (tok.kind) {
569 	case TOK_IDENT:
570 		*typep = tok.str;
571 		break;
572 	case TOK_STRUCT:
573 	case TOK_ENUM:
574 	case TOK_UNION:
575 		*prefixp = tok.str;
576 		scan(TOK_IDENT, &tok);
577 		*typep = tok.str;
578 		break;
579 	case TOK_UNSIGNED:
580 		unsigned_dec(typep);
581 		break;
582 	case TOK_SHORT:
583 		*typep = "short";
584 		(void) peekscan(TOK_INT, &tok);
585 		break;
586 	case TOK_LONG:
587 		*typep = "long";
588 		(void) peekscan(TOK_INT, &tok);
589 		break;
590 	case TOK_VOID:
591 		if (dkind != DEF_UNION && dkind != DEF_PROGRAM) {
592 			error("voids allowed only inside union and program definitions with one argument");
593 		}
594 		*typep = tok.str;
595 		break;
596 	case TOK_STRING:
597 	case TOK_OPAQUE:
598 	case TOK_CHAR:
599 	case TOK_INT:
600 	case TOK_FLOAT:
601 	case TOK_DOUBLE:
602 	case TOK_BOOL:
603 		*typep = tok.str;
604 		break;
605 	default:
606 		error("expected type specifier");
607 	}
608 }
609 
610 static void
611 unsigned_dec(typep)
612 	char  **typep;
613 {
614 	token   tok;
615 
616 	peek(&tok);
617 	switch (tok.kind) {
618 	case TOK_CHAR:
619 		get_token(&tok);
620 		*typep = "u_char";
621 		break;
622 	case TOK_SHORT:
623 		get_token(&tok);
624 		*typep = "u_short";
625 		(void) peekscan(TOK_INT, &tok);
626 		break;
627 	case TOK_LONG:
628 		get_token(&tok);
629 		*typep = "u_long";
630 		(void) peekscan(TOK_INT, &tok);
631 		break;
632 	case TOK_INT:
633 		get_token(&tok);
634 		*typep = "u_int";
635 		break;
636 	default:
637 		*typep = "u_int";
638 		break;
639 	}
640 }
641