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"
48 #include "rpc_parse.h"
53 extern char *make_argname( char *, char * );
54 static void isdefined( definition * );
55 static void def_struct( definition * );
56 static void def_program( definition * );
57 static void def_enum( definition * );
58 static void def_const( definition * );
59 static void def_union( definition * );
60 static void def_typedef( definition * );
61 static void get_declaration( declaration *, defkind );
62 static void get_prog_declaration( declaration *, defkind, int );
63 static void get_type( char **, char **, defkind );
64 static void unsigned_dec( char ** );
67 * return the next definition you see
75 defp = XALLOC(definition);
99 error("definition keyword expected");
101 scan(TOK_SEMICOLON, &tok);
110 STOREVAL(&defined, defp);
122 defp->def_kind = DEF_STRUCT;
124 scan(TOK_IDENT, &tok);
125 defp->def_name = tok.str;
126 scan(TOK_LBRACE, &tok);
127 tailp = &defp->def.st.decls;
129 get_declaration(&dec, DEF_STRUCT);
130 decls = XALLOC(decl_list);
133 tailp = &decls->next;
134 scan(TOK_SEMICOLON, &tok);
136 } while (tok.kind != TOK_RBRACE);
150 version_list **vtailp;
154 bool_t isvoid = FALSE; /* whether first argument is void */
155 defp->def_kind = DEF_PROGRAM;
156 scan(TOK_IDENT, &tok);
157 defp->def_name = tok.str;
158 scan(TOK_LBRACE, &tok);
159 vtailp = &defp->def.pr.versions;
160 tailp = &defp->def.st.decls;
161 scan(TOK_VERSION, &tok);
163 scan(TOK_IDENT, &tok);
164 vlist = XALLOC(version_list);
165 vlist->vers_name = tok.str;
166 scan(TOK_LBRACE, &tok);
167 ptailp = &vlist->procs;
169 /* get result type */
170 plist = XALLOC(proc_list);
171 get_type(&plist->res_prefix, &plist->res_type,
173 if (streq(plist->res_type, "opaque")) {
174 error("illegal result type");
176 scan(TOK_IDENT, &tok);
177 plist->proc_name = tok.str;
178 scan(TOK_LPAREN, &tok);
179 /* get args - first one */
183 * type of DEF_PROGRAM in the first
184 * get_prog_declaration and DEF_STURCT in the next
185 * allows void as argument if it is the only argument
187 get_prog_declaration(&dec, DEF_PROGRAM, num_args);
188 if (streq(dec.type, "void"))
190 decls = XALLOC(decl_list);
191 plist->args.decls = decls;
193 tailp = &decls->next;
195 while (peekscan(TOK_COMMA, &tok)) {
197 get_prog_declaration(&dec, DEF_STRUCT,
199 decls = XALLOC(decl_list);
202 if (streq(dec.type, "void"))
204 tailp = &decls->next;
206 /* multiple arguments are only allowed in newstyle */
207 if (!newstyle && num_args > 1) {
208 error("only one argument is allowed");
210 if (isvoid && num_args > 1) {
211 error("illegal use of void in program definition");
214 scan(TOK_RPAREN, &tok);
215 scan(TOK_EQUAL, &tok);
217 scan(TOK_SEMICOLON, &tok);
218 plist->proc_num = tok.str;
219 plist->arg_num = num_args;
221 ptailp = &plist->next;
223 } while (tok.kind != TOK_RBRACE);
226 vtailp = &vlist->next;
227 scan(TOK_RBRACE, &tok);
228 scan(TOK_EQUAL, &tok);
230 vlist->vers_num = tok.str;
231 /* make the argument structure name for each arg */
232 for (plist = vlist->procs; plist != NULL;
233 plist = plist->next) {
234 plist->args.argname = make_argname(plist->proc_name,
236 /* free the memory ?? */
238 scan(TOK_SEMICOLON, &tok);
239 scan2(TOK_VERSION, TOK_RBRACE, &tok);
240 } while (tok.kind == TOK_VERSION);
241 scan(TOK_EQUAL, &tok);
243 defp->def.pr.prog_num = tok.str;
254 enumval_list **tailp;
256 defp->def_kind = DEF_ENUM;
257 scan(TOK_IDENT, &tok);
258 defp->def_name = tok.str;
259 scan(TOK_LBRACE, &tok);
260 tailp = &defp->def.en.vals;
262 scan(TOK_IDENT, &tok);
263 elist = XALLOC(enumval_list);
264 elist->name = tok.str;
265 elist->assignment = NULL;
266 scan3(TOK_COMMA, TOK_RBRACE, TOK_EQUAL, &tok);
267 if (tok.kind == TOK_EQUAL) {
269 elist->assignment = tok.str;
270 scan2(TOK_COMMA, TOK_RBRACE, &tok);
273 tailp = &elist->next;
274 } while (tok.kind != TOK_RBRACE);
284 defp->def_kind = DEF_CONST;
285 scan(TOK_IDENT, &tok);
286 defp->def_name = tok.str;
287 scan(TOK_EQUAL, &tok);
288 scan2(TOK_IDENT, TOK_STRCONST, &tok);
289 defp->def.co = tok.str;
302 defp->def_kind = DEF_UNION;
303 scan(TOK_IDENT, &tok);
304 defp->def_name = tok.str;
305 scan(TOK_SWITCH, &tok);
306 scan(TOK_LPAREN, &tok);
307 get_declaration(&dec, DEF_UNION);
308 defp->def.un.enum_decl = dec;
309 tailp = &defp->def.un.cases;
310 scan(TOK_RPAREN, &tok);
311 scan(TOK_LBRACE, &tok);
312 scan(TOK_CASE, &tok);
313 while (tok.kind == TOK_CASE) {
314 scan2(TOK_IDENT, TOK_CHARCONST, &tok);
315 cases = XALLOC(case_list);
316 cases->case_name = tok.str;
317 scan(TOK_COLON, &tok);
318 /* now peek at next token */
320 if (peekscan(TOK_CASE, &tok)){
322 scan2(TOK_IDENT, TOK_CHARCONST, &tok);
324 /* continued case statement */
326 tailp = &cases->next;
327 cases = XALLOC(case_list);
328 cases->case_name = tok.str;
329 scan(TOK_COLON, &tok);
330 } while (peekscan(TOK_CASE, &tok));
337 tailp = &cases->next;
338 cases = XALLOC(case_list);
341 get_declaration(&dec, DEF_UNION);
342 cases->case_decl = dec;
343 cases->contflag = 0; /* no continued case statement */
345 tailp = &cases->next;
346 scan(TOK_SEMICOLON, &tok);
348 scan3(TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok);
351 if (tok.kind == TOK_DEFAULT) {
352 scan(TOK_COLON, &tok);
353 get_declaration(&dec, DEF_UNION);
354 defp->def.un.default_decl = XALLOC(declaration);
355 *defp->def.un.default_decl = dec;
356 scan(TOK_SEMICOLON, &tok);
357 scan(TOK_RBRACE, &tok);
359 defp->def.un.default_decl = NULL;
363 static char* reserved_words[] =
380 static char* reserved_types[] =
388 * check that the given name is not one that would eventually result in
389 * xdr routines that would conflict with internal XDR routines.
392 check_type_name(name, new_type)
399 for (i = 0; reserved_words[i] != NULL; i++) {
400 if (strcmp(name, reserved_words[i]) == 0) {
402 "illegal (reserved) name :\'%s\' in type definition",
408 for (i = 0; reserved_types[i] != NULL; i++) {
409 if (strcmp(name, reserved_types[i]) == 0) {
411 "illegal (reserved) name :\'%s\' in type definition",
427 defp->def_kind = DEF_TYPEDEF;
428 get_declaration(&dec, DEF_TYPEDEF);
429 defp->def_name = dec.name;
430 check_type_name(dec.name, 1);
431 defp->def.ty.old_prefix = dec.prefix;
432 defp->def.ty.old_type = dec.type;
433 defp->def.ty.rel = dec.rel;
434 defp->def.ty.array_max = dec.array_max;
438 get_declaration(dec, dkind)
444 get_type(&dec->prefix, &dec->type, dkind);
445 dec->rel = REL_ALIAS;
446 if (streq(dec->type, "void")) {
450 check_type_name(dec->type, 0);
451 scan2(TOK_STAR, TOK_IDENT, &tok);
452 if (tok.kind == TOK_STAR) {
453 dec->rel = REL_POINTER;
454 scan(TOK_IDENT, &tok);
457 if (peekscan(TOK_LBRACKET, &tok)) {
458 if (dec->rel == REL_POINTER) {
459 error("no array-of-pointer declarations -- use typedef");
461 dec->rel = REL_VECTOR;
463 dec->array_max = tok.str;
464 scan(TOK_RBRACKET, &tok);
465 } else if (peekscan(TOK_LANGLE, &tok)) {
466 if (dec->rel == REL_POINTER) {
467 error("no array-of-pointer declarations -- use typedef");
469 dec->rel = REL_ARRAY;
470 if (peekscan(TOK_RANGLE, &tok)) {
471 dec->array_max = "~0"; /* unspecified size, use max */
474 dec->array_max = tok.str;
475 scan(TOK_RANGLE, &tok);
478 if (streq(dec->type, "opaque")) {
479 if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR) {
480 error("array declaration expected");
482 } else if (streq(dec->type, "string")) {
483 if (dec->rel != REL_ARRAY) {
484 error("variable-length array declaration expected");
491 get_prog_declaration(dec, dkind, num)
494 int num; /* arg number */
497 char name[10]; /* argument name */
499 if (dkind == DEF_PROGRAM) {
501 if (tok.kind == TOK_RPAREN) { /* no arguments */
502 dec->rel = REL_ALIAS;
509 get_type(&dec->prefix, &dec->type, dkind);
510 dec->rel = REL_ALIAS;
511 if (peekscan(TOK_IDENT, &tok)) /* optional name of argument */
512 strcpy(name, tok.str);
514 sprintf(name, "%s%d", ARGNAME, num);
515 /* default name of argument */
517 dec->name = (char *) xstrdup(name);
518 if (streq(dec->type, "void")) {
522 if (streq(dec->type, "opaque")) {
523 error("opaque -- illegal argument type");
525 if (peekscan(TOK_STAR, &tok)) {
526 if (streq(dec->type, "string")) {
527 error("pointer to string not allowed in program arguments");
529 dec->rel = REL_POINTER;
530 if (peekscan(TOK_IDENT, &tok)) {
531 /* optional name of argument */
532 dec->name = xstrdup(tok.str);
535 if (peekscan(TOK_LANGLE, &tok)) {
536 if (!streq(dec->type, "string")) {
537 error("arrays cannot be declared as arguments to procedures -- use typedef");
539 dec->rel = REL_ARRAY;
540 if (peekscan(TOK_RANGLE, &tok)) {
541 dec->array_max = "~0";
542 /* unspecified size, use max */
545 dec->array_max = tok.str;
546 scan(TOK_RANGLE, &tok);
549 if (streq(dec->type, "string")) {
550 if (dec->rel != REL_ARRAY) {
552 * .x specifies just string as
556 dec->rel = REL_ARRAY;
557 dec->array_max = "~0"; /* unspecified size, use max */
565 get_type(prefixp, typep, dkind)
582 scan(TOK_IDENT, &tok);
590 (void) peekscan(TOK_INT, &tok);
594 (void) peekscan(TOK_INT, &tok);
598 (void) peekscan(TOK_INT, &tok);
602 if (dkind != DEF_UNION && dkind != DEF_PROGRAM) {
603 error("voids allowed only inside union and program definitions with one argument");
618 error("expected type specifier");
637 (void) peekscan(TOK_INT, &tok);
642 (void) peekscan(TOK_INT, &tok);
646 *typep = "u_int64_t";
648 (void) peekscan(TOK_INT, &tok);