2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
32 #ident "@(#)rpc_parse.c 1.12 93/07/05 SMI"
33 static char sccsid[] = "@(#)rpc_parse.c 1.8 89/02/22 (C) 1987 SMI";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 * rpc_parse.c, Parser for the RPC protocol compiler
42 * Copyright (C) 1987 Sun Microsystems, Inc.
46 #include "rpc/types.h"
47 #include "rpc_parse.h"
53 static void isdefined( definition * );
54 static void def_struct( definition * );
55 static void def_program( definition * );
56 static void def_enum( definition * );
57 static void def_const( definition * );
58 static void def_union( definition * );
59 static void def_typedef( definition * );
60 static void get_declaration( declaration *, defkind );
61 static void get_prog_declaration( declaration *, defkind, int );
62 static void get_type(const char **, const char **, defkind);
63 static void unsigned_dec(const char ** );
66 * return the next definition you see
74 defp = XALLOC(definition);
99 error("definition keyword expected");
101 scan(TOK_SEMICOLON, &tok);
107 isdefined(definition *defp)
109 STOREVAL(&defined, defp);
113 def_struct(definition *defp)
120 defp->def_kind = DEF_STRUCT;
122 scan(TOK_IDENT, &tok);
123 defp->def_name = tok.str;
124 scan(TOK_LBRACE, &tok);
125 tailp = &defp->def.st.decls;
127 get_declaration(&dec, DEF_STRUCT);
128 decls = XALLOC(decl_list);
131 tailp = &decls->next;
132 scan(TOK_SEMICOLON, &tok);
134 } while (tok.kind != TOK_RBRACE);
140 def_program(definition *defp)
147 version_list **vtailp;
151 bool_t isvoid = FALSE; /* whether first argument is void */
152 defp->def_kind = DEF_PROGRAM;
153 scan(TOK_IDENT, &tok);
154 defp->def_name = tok.str;
155 scan(TOK_LBRACE, &tok);
156 vtailp = &defp->def.pr.versions;
157 tailp = &defp->def.st.decls;
158 scan(TOK_VERSION, &tok);
160 scan(TOK_IDENT, &tok);
161 vlist = XALLOC(version_list);
162 vlist->vers_name = tok.str;
163 scan(TOK_LBRACE, &tok);
164 ptailp = &vlist->procs;
166 /* get result type */
167 plist = XALLOC(proc_list);
168 get_type(&plist->res_prefix, &plist->res_type,
170 if (streq(plist->res_type, "opaque")) {
171 error("illegal result type");
173 scan(TOK_IDENT, &tok);
174 plist->proc_name = tok.str;
175 scan(TOK_LPAREN, &tok);
176 /* get args - first one */
180 * type of DEF_PROGRAM in the first
181 * get_prog_declaration and DEF_STURCT in the next
182 * allows void as argument if it is the only argument
184 get_prog_declaration(&dec, DEF_PROGRAM, num_args);
185 if (streq(dec.type, "void"))
187 decls = XALLOC(decl_list);
188 plist->args.decls = decls;
190 tailp = &decls->next;
192 while (peekscan(TOK_COMMA, &tok)) {
194 get_prog_declaration(&dec, DEF_STRUCT,
196 decls = XALLOC(decl_list);
199 if (streq(dec.type, "void"))
201 tailp = &decls->next;
203 /* multiple arguments are only allowed in newstyle */
204 if (!newstyle && num_args > 1) {
205 error("only one argument is allowed");
207 if (isvoid && num_args > 1) {
208 error("illegal use of void in program definition");
211 scan(TOK_RPAREN, &tok);
212 scan(TOK_EQUAL, &tok);
214 scan(TOK_SEMICOLON, &tok);
215 plist->proc_num = tok.str;
216 plist->arg_num = num_args;
218 ptailp = &plist->next;
220 } while (tok.kind != TOK_RBRACE);
223 vtailp = &vlist->next;
224 scan(TOK_RBRACE, &tok);
225 scan(TOK_EQUAL, &tok);
227 vlist->vers_num = tok.str;
228 /* make the argument structure name for each arg */
229 for (plist = vlist->procs; plist != NULL;
230 plist = plist->next) {
231 plist->args.argname = make_argname(plist->proc_name,
233 /* free the memory ?? */
235 scan(TOK_SEMICOLON, &tok);
236 scan2(TOK_VERSION, TOK_RBRACE, &tok);
237 } while (tok.kind == TOK_VERSION);
238 scan(TOK_EQUAL, &tok);
240 defp->def.pr.prog_num = tok.str;
246 def_enum(definition *defp)
250 enumval_list **tailp;
252 defp->def_kind = DEF_ENUM;
253 scan(TOK_IDENT, &tok);
254 defp->def_name = tok.str;
255 scan(TOK_LBRACE, &tok);
256 tailp = &defp->def.en.vals;
258 scan(TOK_IDENT, &tok);
259 elist = XALLOC(enumval_list);
260 elist->name = tok.str;
261 elist->assignment = NULL;
262 scan3(TOK_COMMA, TOK_RBRACE, TOK_EQUAL, &tok);
263 if (tok.kind == TOK_EQUAL) {
265 elist->assignment = tok.str;
266 scan2(TOK_COMMA, TOK_RBRACE, &tok);
269 tailp = &elist->next;
270 } while (tok.kind != TOK_RBRACE);
275 def_const(definition *defp)
279 defp->def_kind = DEF_CONST;
280 scan(TOK_IDENT, &tok);
281 defp->def_name = tok.str;
282 scan(TOK_EQUAL, &tok);
283 scan2(TOK_IDENT, TOK_STRCONST, &tok);
284 defp->def.co = tok.str;
288 def_union(definition *defp)
295 defp->def_kind = DEF_UNION;
296 scan(TOK_IDENT, &tok);
297 defp->def_name = tok.str;
298 scan(TOK_SWITCH, &tok);
299 scan(TOK_LPAREN, &tok);
300 get_declaration(&dec, DEF_UNION);
301 defp->def.un.enum_decl = dec;
302 tailp = &defp->def.un.cases;
303 scan(TOK_RPAREN, &tok);
304 scan(TOK_LBRACE, &tok);
305 scan(TOK_CASE, &tok);
306 while (tok.kind == TOK_CASE) {
307 scan2(TOK_IDENT, TOK_CHARCONST, &tok);
308 cases = XALLOC(case_list);
309 cases->case_name = tok.str;
310 scan(TOK_COLON, &tok);
311 /* now peek at next token */
312 if (peekscan(TOK_CASE, &tok)){
314 scan2(TOK_IDENT, TOK_CHARCONST, &tok);
316 /* continued case statement */
318 tailp = &cases->next;
319 cases = XALLOC(case_list);
320 cases->case_name = tok.str;
321 scan(TOK_COLON, &tok);
322 } while (peekscan(TOK_CASE, &tok));
325 get_declaration(&dec, DEF_UNION);
326 cases->case_decl = dec;
327 cases->contflag = 0; /* no continued case statement */
329 tailp = &cases->next;
330 scan(TOK_SEMICOLON, &tok);
332 scan3(TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok);
335 if (tok.kind == TOK_DEFAULT) {
336 scan(TOK_COLON, &tok);
337 get_declaration(&dec, DEF_UNION);
338 defp->def.un.default_decl = XALLOC(declaration);
339 *defp->def.un.default_decl = dec;
340 scan(TOK_SEMICOLON, &tok);
341 scan(TOK_RBRACE, &tok);
343 defp->def.un.default_decl = NULL;
347 static const char *reserved_words[] =
364 static const char *reserved_types[] =
372 * check that the given name is not one that would eventually result in
373 * xdr routines that would conflict with internal XDR routines.
376 check_type_name(const char *name, int new_type)
381 for (i = 0; reserved_words[i] != NULL; i++) {
382 if (strcmp(name, reserved_words[i]) == 0) {
384 "illegal (reserved) name :\'%s\' in type definition",
390 for (i = 0; reserved_types[i] != NULL; i++) {
391 if (strcmp(name, reserved_types[i]) == 0) {
393 "illegal (reserved) name :\'%s\' in type definition",
404 def_typedef(definition *defp)
408 defp->def_kind = DEF_TYPEDEF;
409 get_declaration(&dec, DEF_TYPEDEF);
410 defp->def_name = dec.name;
411 check_type_name(dec.name, 1);
412 defp->def.ty.old_prefix = dec.prefix;
413 defp->def.ty.old_type = dec.type;
414 defp->def.ty.rel = dec.rel;
415 defp->def.ty.array_max = dec.array_max;
419 get_declaration(declaration *dec, defkind dkind)
423 get_type(&dec->prefix, &dec->type, dkind);
424 dec->rel = REL_ALIAS;
425 if (streq(dec->type, "void")) {
429 check_type_name(dec->type, 0);
430 scan2(TOK_STAR, TOK_IDENT, &tok);
431 if (tok.kind == TOK_STAR) {
432 dec->rel = REL_POINTER;
433 scan(TOK_IDENT, &tok);
436 if (peekscan(TOK_LBRACKET, &tok)) {
437 if (dec->rel == REL_POINTER) {
438 error("no array-of-pointer declarations -- use typedef");
440 dec->rel = REL_VECTOR;
442 dec->array_max = tok.str;
443 scan(TOK_RBRACKET, &tok);
444 } else if (peekscan(TOK_LANGLE, &tok)) {
445 if (dec->rel == REL_POINTER) {
446 error("no array-of-pointer declarations -- use typedef");
448 dec->rel = REL_ARRAY;
449 if (peekscan(TOK_RANGLE, &tok)) {
450 dec->array_max = "~0"; /* unspecified size, use max */
453 dec->array_max = tok.str;
454 scan(TOK_RANGLE, &tok);
457 if (streq(dec->type, "opaque")) {
458 if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR) {
459 error("array declaration expected");
461 } else if (streq(dec->type, "string")) {
462 if (dec->rel != REL_ARRAY) {
463 error("variable-length array declaration expected");
470 get_prog_declaration(declaration *dec, defkind dkind, int num)
473 char name[10]; /* argument name */
475 if (dkind == DEF_PROGRAM) {
477 if (tok.kind == TOK_RPAREN) { /* no arguments */
478 dec->rel = REL_ALIAS;
485 get_type(&dec->prefix, &dec->type, dkind);
486 dec->rel = REL_ALIAS;
487 if (peekscan(TOK_IDENT, &tok)) /* optional name of argument */
488 strcpy(name, tok.str);
490 sprintf(name, "%s%d", ARGNAME, num);
491 /* default name of argument */
493 dec->name = (char *) xstrdup(name);
494 if (streq(dec->type, "void")) {
498 if (streq(dec->type, "opaque")) {
499 error("opaque -- illegal argument type");
501 if (peekscan(TOK_STAR, &tok)) {
502 if (streq(dec->type, "string")) {
503 error("pointer to string not allowed in program arguments");
505 dec->rel = REL_POINTER;
506 if (peekscan(TOK_IDENT, &tok)) {
507 /* optional name of argument */
508 dec->name = xstrdup(tok.str);
511 if (peekscan(TOK_LANGLE, &tok)) {
512 if (!streq(dec->type, "string")) {
513 error("arrays cannot be declared as arguments to procedures -- use typedef");
515 dec->rel = REL_ARRAY;
516 if (peekscan(TOK_RANGLE, &tok)) {
517 dec->array_max = "~0";
518 /* unspecified size, use max */
521 dec->array_max = tok.str;
522 scan(TOK_RANGLE, &tok);
525 if (streq(dec->type, "string")) {
526 if (dec->rel != REL_ARRAY) {
528 * .x specifies just string as
532 dec->rel = REL_ARRAY;
533 dec->array_max = "~0"; /* unspecified size, use max */
541 get_type(const char **prefixp, const char **typep, defkind dkind)
555 scan(TOK_IDENT, &tok);
563 (void) peekscan(TOK_INT, &tok);
567 (void) peekscan(TOK_INT, &tok);
571 (void) peekscan(TOK_INT, &tok);
575 if (dkind != DEF_UNION && dkind != DEF_PROGRAM) {
576 error("voids allowed only inside union and program definitions with one argument");
591 error("expected type specifier");
596 unsigned_dec(const char **typep)
609 (void) peekscan(TOK_INT, &tok);
614 (void) peekscan(TOK_INT, &tok);
618 *typep = "u_int64_t";
620 (void) peekscan(TOK_INT, &tok);