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);
98 error("definition keyword expected");
100 scan(TOK_SEMICOLON, &tok);
106 isdefined(definition *defp)
108 STOREVAL(&defined, defp);
112 def_struct(definition *defp)
119 defp->def_kind = DEF_STRUCT;
121 scan(TOK_IDENT, &tok);
122 defp->def_name = tok.str;
123 scan(TOK_LBRACE, &tok);
124 tailp = &defp->def.st.decls;
126 get_declaration(&dec, DEF_STRUCT);
127 decls = XALLOC(decl_list);
130 tailp = &decls->next;
131 scan(TOK_SEMICOLON, &tok);
133 } while (tok.kind != TOK_RBRACE);
139 def_program(definition *defp)
146 version_list **vtailp;
150 bool_t isvoid = FALSE; /* whether first argument is void */
151 defp->def_kind = DEF_PROGRAM;
152 scan(TOK_IDENT, &tok);
153 defp->def_name = tok.str;
154 scan(TOK_LBRACE, &tok);
155 vtailp = &defp->def.pr.versions;
156 tailp = &defp->def.st.decls;
157 scan(TOK_VERSION, &tok);
159 scan(TOK_IDENT, &tok);
160 vlist = XALLOC(version_list);
161 vlist->vers_name = tok.str;
162 scan(TOK_LBRACE, &tok);
163 ptailp = &vlist->procs;
165 /* get result type */
166 plist = XALLOC(proc_list);
167 get_type(&plist->res_prefix, &plist->res_type,
169 if (streq(plist->res_type, "opaque")) {
170 error("illegal result type");
172 scan(TOK_IDENT, &tok);
173 plist->proc_name = tok.str;
174 scan(TOK_LPAREN, &tok);
175 /* get args - first one */
179 * type of DEF_PROGRAM in the first
180 * get_prog_declaration and DEF_STURCT in the next
181 * allows void as argument if it is the only argument
183 get_prog_declaration(&dec, DEF_PROGRAM, num_args);
184 if (streq(dec.type, "void"))
186 decls = XALLOC(decl_list);
187 plist->args.decls = decls;
189 tailp = &decls->next;
191 while (peekscan(TOK_COMMA, &tok)) {
193 get_prog_declaration(&dec, DEF_STRUCT,
195 decls = XALLOC(decl_list);
198 if (streq(dec.type, "void"))
200 tailp = &decls->next;
202 /* multiple arguments are only allowed in newstyle */
203 if (!newstyle && num_args > 1) {
204 error("only one argument is allowed");
206 if (isvoid && num_args > 1) {
207 error("illegal use of void in program definition");
210 scan(TOK_RPAREN, &tok);
211 scan(TOK_EQUAL, &tok);
213 scan(TOK_SEMICOLON, &tok);
214 plist->proc_num = tok.str;
215 plist->arg_num = num_args;
217 ptailp = &plist->next;
219 } while (tok.kind != TOK_RBRACE);
222 vtailp = &vlist->next;
223 scan(TOK_RBRACE, &tok);
224 scan(TOK_EQUAL, &tok);
226 vlist->vers_num = tok.str;
227 /* make the argument structure name for each arg */
228 for (plist = vlist->procs; plist != NULL;
229 plist = plist->next) {
230 plist->args.argname = make_argname(plist->proc_name,
232 /* free the memory ?? */
234 scan(TOK_SEMICOLON, &tok);
235 scan2(TOK_VERSION, TOK_RBRACE, &tok);
236 } while (tok.kind == TOK_VERSION);
237 scan(TOK_EQUAL, &tok);
239 defp->def.pr.prog_num = tok.str;
245 def_enum(definition *defp)
249 enumval_list **tailp;
251 defp->def_kind = DEF_ENUM;
252 scan(TOK_IDENT, &tok);
253 defp->def_name = tok.str;
254 scan(TOK_LBRACE, &tok);
255 tailp = &defp->def.en.vals;
257 scan(TOK_IDENT, &tok);
258 elist = XALLOC(enumval_list);
259 elist->name = tok.str;
260 elist->assignment = NULL;
261 scan3(TOK_COMMA, TOK_RBRACE, TOK_EQUAL, &tok);
262 if (tok.kind == TOK_EQUAL) {
264 elist->assignment = tok.str;
265 scan2(TOK_COMMA, TOK_RBRACE, &tok);
268 tailp = &elist->next;
269 } while (tok.kind != TOK_RBRACE);
274 def_const(definition *defp)
278 defp->def_kind = DEF_CONST;
279 scan(TOK_IDENT, &tok);
280 defp->def_name = tok.str;
281 scan(TOK_EQUAL, &tok);
282 scan2(TOK_IDENT, TOK_STRCONST, &tok);
283 defp->def.co = tok.str;
287 def_union(definition *defp)
294 defp->def_kind = DEF_UNION;
295 scan(TOK_IDENT, &tok);
296 defp->def_name = tok.str;
297 scan(TOK_SWITCH, &tok);
298 scan(TOK_LPAREN, &tok);
299 get_declaration(&dec, DEF_UNION);
300 defp->def.un.enum_decl = dec;
301 tailp = &defp->def.un.cases;
302 scan(TOK_RPAREN, &tok);
303 scan(TOK_LBRACE, &tok);
304 scan(TOK_CASE, &tok);
305 while (tok.kind == TOK_CASE) {
306 scan2(TOK_IDENT, TOK_CHARCONST, &tok);
307 cases = XALLOC(case_list);
308 cases->case_name = tok.str;
309 scan(TOK_COLON, &tok);
310 /* now peek at next token */
311 if (peekscan(TOK_CASE, &tok)){
313 scan2(TOK_IDENT, TOK_CHARCONST, &tok);
315 /* continued case statement */
317 tailp = &cases->next;
318 cases = XALLOC(case_list);
319 cases->case_name = tok.str;
320 scan(TOK_COLON, &tok);
321 } while (peekscan(TOK_CASE, &tok));
324 get_declaration(&dec, DEF_UNION);
325 cases->case_decl = dec;
326 cases->contflag = 0; /* no continued case statement */
328 tailp = &cases->next;
329 scan(TOK_SEMICOLON, &tok);
331 scan3(TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok);
334 if (tok.kind == TOK_DEFAULT) {
335 scan(TOK_COLON, &tok);
336 get_declaration(&dec, DEF_UNION);
337 defp->def.un.default_decl = XALLOC(declaration);
338 *defp->def.un.default_decl = dec;
339 scan(TOK_SEMICOLON, &tok);
340 scan(TOK_RBRACE, &tok);
342 defp->def.un.default_decl = NULL;
346 static const char *reserved_words[] =
363 static const char *reserved_types[] =
371 * check that the given name is not one that would eventually result in
372 * xdr routines that would conflict with internal XDR routines.
375 check_type_name(const char *name, int new_type)
380 for (i = 0; reserved_words[i] != NULL; i++) {
381 if (strcmp(name, reserved_words[i]) == 0) {
383 "illegal (reserved) name :\'%s\' in type definition",
389 for (i = 0; reserved_types[i] != NULL; i++) {
390 if (strcmp(name, reserved_types[i]) == 0) {
392 "illegal (reserved) name :\'%s\' in type definition",
403 def_typedef(definition *defp)
407 defp->def_kind = DEF_TYPEDEF;
408 get_declaration(&dec, DEF_TYPEDEF);
409 defp->def_name = dec.name;
410 check_type_name(dec.name, 1);
411 defp->def.ty.old_prefix = dec.prefix;
412 defp->def.ty.old_type = dec.type;
413 defp->def.ty.rel = dec.rel;
414 defp->def.ty.array_max = dec.array_max;
418 get_declaration(declaration *dec, defkind dkind)
422 get_type(&dec->prefix, &dec->type, dkind);
423 dec->rel = REL_ALIAS;
424 if (streq(dec->type, "void")) {
428 check_type_name(dec->type, 0);
429 scan2(TOK_STAR, TOK_IDENT, &tok);
430 if (tok.kind == TOK_STAR) {
431 dec->rel = REL_POINTER;
432 scan(TOK_IDENT, &tok);
435 if (peekscan(TOK_LBRACKET, &tok)) {
436 if (dec->rel == REL_POINTER) {
437 error("no array-of-pointer declarations -- use typedef");
439 dec->rel = REL_VECTOR;
441 dec->array_max = tok.str;
442 scan(TOK_RBRACKET, &tok);
443 } else if (peekscan(TOK_LANGLE, &tok)) {
444 if (dec->rel == REL_POINTER) {
445 error("no array-of-pointer declarations -- use typedef");
447 dec->rel = REL_ARRAY;
448 if (peekscan(TOK_RANGLE, &tok)) {
449 dec->array_max = "~0"; /* unspecified size, use max */
452 dec->array_max = tok.str;
453 scan(TOK_RANGLE, &tok);
456 if (streq(dec->type, "opaque")) {
457 if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR) {
458 error("array declaration expected");
460 } else if (streq(dec->type, "string")) {
461 if (dec->rel != REL_ARRAY) {
462 error("variable-length array declaration expected");
469 get_prog_declaration(declaration *dec, defkind dkind, int num)
472 char name[10]; /* argument name */
474 if (dkind == DEF_PROGRAM) {
476 if (tok.kind == TOK_RPAREN) { /* no arguments */
477 dec->rel = REL_ALIAS;
484 get_type(&dec->prefix, &dec->type, dkind);
485 dec->rel = REL_ALIAS;
486 if (peekscan(TOK_IDENT, &tok)) /* optional name of argument */
487 strcpy(name, tok.str);
489 sprintf(name, "%s%d", ARGNAME, num);
490 /* default name of argument */
492 dec->name = (char *) xstrdup(name);
493 if (streq(dec->type, "void")) {
497 if (streq(dec->type, "opaque")) {
498 error("opaque -- illegal argument type");
500 if (peekscan(TOK_STAR, &tok)) {
501 if (streq(dec->type, "string")) {
502 error("pointer to string not allowed in program arguments");
504 dec->rel = REL_POINTER;
505 if (peekscan(TOK_IDENT, &tok)) {
506 /* optional name of argument */
507 dec->name = xstrdup(tok.str);
510 if (peekscan(TOK_LANGLE, &tok)) {
511 if (!streq(dec->type, "string")) {
512 error("arrays cannot be declared as arguments to procedures -- use typedef");
514 dec->rel = REL_ARRAY;
515 if (peekscan(TOK_RANGLE, &tok)) {
516 dec->array_max = "~0";
517 /* unspecified size, use max */
520 dec->array_max = tok.str;
521 scan(TOK_RANGLE, &tok);
524 if (streq(dec->type, "string")) {
525 if (dec->rel != REL_ARRAY) {
527 * .x specifies just string as
531 dec->rel = REL_ARRAY;
532 dec->array_max = "~0"; /* unspecified size, use max */
540 get_type(const char **prefixp, const char **typep, defkind dkind)
554 scan(TOK_IDENT, &tok);
562 (void) peekscan(TOK_INT, &tok);
566 (void) peekscan(TOK_INT, &tok);
570 (void) peekscan(TOK_INT, &tok);
574 if (dkind != DEF_UNION && dkind != DEF_PROGRAM) {
575 error("voids allowed only inside union and program definitions with one argument");
590 error("expected type specifier");
595 unsigned_dec(const char **typep)
608 (void) peekscan(TOK_INT, &tok);
613 (void) peekscan(TOK_INT, &tok);
617 *typep = "u_int64_t";
619 (void) peekscan(TOK_INT, &tok);