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
30 #ident "@(#)rpc_parse.c 1.12 93/07/05 SMI"
34 static char sccsid[] = "@(#)rpc_parse.c 1.8 89/02/22 (C) 1987 SMI";
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * rpc_parse.c, Parser for the RPC protocol compiler
43 * Copyright (C) 1987 Sun Microsystems, Inc.
47 #include "rpc/types.h"
49 #include "rpc_parse.h"
54 extern char *make_argname( char *, char * );
55 static void isdefined( definition * );
56 static void def_struct( definition * );
57 static void def_program( definition * );
58 static void def_enum( definition * );
59 static void def_const( definition * );
60 static void def_union( definition * );
61 static void def_typedef( definition * );
62 static void get_declaration( declaration *, defkind );
63 static void get_prog_declaration( declaration *, defkind, int );
64 static void get_type( char **, char **, defkind );
65 static void unsigned_dec( char ** );
68 * return the next definition you see
76 defp = XALLOC(definition);
100 error("definition keyword expected");
102 scan(TOK_SEMICOLON, &tok);
111 STOREVAL(&defined, defp);
123 defp->def_kind = DEF_STRUCT;
125 scan(TOK_IDENT, &tok);
126 defp->def_name = tok.str;
127 scan(TOK_LBRACE, &tok);
128 tailp = &defp->def.st.decls;
130 get_declaration(&dec, DEF_STRUCT);
131 decls = XALLOC(decl_list);
134 tailp = &decls->next;
135 scan(TOK_SEMICOLON, &tok);
137 } while (tok.kind != TOK_RBRACE);
151 version_list **vtailp;
155 bool_t isvoid = FALSE; /* whether first argument is void */
156 defp->def_kind = DEF_PROGRAM;
157 scan(TOK_IDENT, &tok);
158 defp->def_name = tok.str;
159 scan(TOK_LBRACE, &tok);
160 vtailp = &defp->def.pr.versions;
161 tailp = &defp->def.st.decls;
162 scan(TOK_VERSION, &tok);
164 scan(TOK_IDENT, &tok);
165 vlist = XALLOC(version_list);
166 vlist->vers_name = tok.str;
167 scan(TOK_LBRACE, &tok);
168 ptailp = &vlist->procs;
170 /* get result type */
171 plist = XALLOC(proc_list);
172 get_type(&plist->res_prefix, &plist->res_type,
174 if (streq(plist->res_type, "opaque")) {
175 error("illegal result type");
177 scan(TOK_IDENT, &tok);
178 plist->proc_name = tok.str;
179 scan(TOK_LPAREN, &tok);
180 /* get args - first one */
184 * type of DEF_PROGRAM in the first
185 * get_prog_declaration and DEF_STURCT in the next
186 * allows void as argument if it is the only argument
188 get_prog_declaration(&dec, DEF_PROGRAM, num_args);
189 if (streq(dec.type, "void"))
191 decls = XALLOC(decl_list);
192 plist->args.decls = decls;
194 tailp = &decls->next;
196 while (peekscan(TOK_COMMA, &tok)) {
198 get_prog_declaration(&dec, DEF_STRUCT,
200 decls = XALLOC(decl_list);
203 if (streq(dec.type, "void"))
205 tailp = &decls->next;
207 /* multiple arguments are only allowed in newstyle */
208 if (!newstyle && num_args > 1) {
209 error("only one argument is allowed");
211 if (isvoid && num_args > 1) {
212 error("illegal use of void in program definition");
215 scan(TOK_RPAREN, &tok);
216 scan(TOK_EQUAL, &tok);
218 scan(TOK_SEMICOLON, &tok);
219 plist->proc_num = tok.str;
220 plist->arg_num = num_args;
222 ptailp = &plist->next;
224 } while (tok.kind != TOK_RBRACE);
227 vtailp = &vlist->next;
228 scan(TOK_RBRACE, &tok);
229 scan(TOK_EQUAL, &tok);
231 vlist->vers_num = tok.str;
232 /* make the argument structure name for each arg */
233 for (plist = vlist->procs; plist != NULL;
234 plist = plist->next) {
235 plist->args.argname = make_argname(plist->proc_name,
237 /* free the memory ?? */
239 scan(TOK_SEMICOLON, &tok);
240 scan2(TOK_VERSION, TOK_RBRACE, &tok);
241 } while (tok.kind == TOK_VERSION);
242 scan(TOK_EQUAL, &tok);
244 defp->def.pr.prog_num = tok.str;
255 enumval_list **tailp;
257 defp->def_kind = DEF_ENUM;
258 scan(TOK_IDENT, &tok);
259 defp->def_name = tok.str;
260 scan(TOK_LBRACE, &tok);
261 tailp = &defp->def.en.vals;
263 scan(TOK_IDENT, &tok);
264 elist = XALLOC(enumval_list);
265 elist->name = tok.str;
266 elist->assignment = NULL;
267 scan3(TOK_COMMA, TOK_RBRACE, TOK_EQUAL, &tok);
268 if (tok.kind == TOK_EQUAL) {
270 elist->assignment = tok.str;
271 scan2(TOK_COMMA, TOK_RBRACE, &tok);
274 tailp = &elist->next;
275 } while (tok.kind != TOK_RBRACE);
285 defp->def_kind = DEF_CONST;
286 scan(TOK_IDENT, &tok);
287 defp->def_name = tok.str;
288 scan(TOK_EQUAL, &tok);
289 scan2(TOK_IDENT, TOK_STRCONST, &tok);
290 defp->def.co = tok.str;
303 defp->def_kind = DEF_UNION;
304 scan(TOK_IDENT, &tok);
305 defp->def_name = tok.str;
306 scan(TOK_SWITCH, &tok);
307 scan(TOK_LPAREN, &tok);
308 get_declaration(&dec, DEF_UNION);
309 defp->def.un.enum_decl = dec;
310 tailp = &defp->def.un.cases;
311 scan(TOK_RPAREN, &tok);
312 scan(TOK_LBRACE, &tok);
313 scan(TOK_CASE, &tok);
314 while (tok.kind == TOK_CASE) {
315 scan2(TOK_IDENT, TOK_CHARCONST, &tok);
316 cases = XALLOC(case_list);
317 cases->case_name = tok.str;
318 scan(TOK_COLON, &tok);
319 /* now peek at next token */
321 if (peekscan(TOK_CASE, &tok)){
323 scan2(TOK_IDENT, TOK_CHARCONST, &tok);
325 /* continued case statement */
327 tailp = &cases->next;
328 cases = XALLOC(case_list);
329 cases->case_name = tok.str;
330 scan(TOK_COLON, &tok);
331 } while (peekscan(TOK_CASE, &tok));
338 tailp = &cases->next;
339 cases = XALLOC(case_list);
342 get_declaration(&dec, DEF_UNION);
343 cases->case_decl = dec;
344 cases->contflag = 0; /* no continued case statement */
346 tailp = &cases->next;
347 scan(TOK_SEMICOLON, &tok);
349 scan3(TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok);
352 if (tok.kind == TOK_DEFAULT) {
353 scan(TOK_COLON, &tok);
354 get_declaration(&dec, DEF_UNION);
355 defp->def.un.default_decl = XALLOC(declaration);
356 *defp->def.un.default_decl = dec;
357 scan(TOK_SEMICOLON, &tok);
358 scan(TOK_RBRACE, &tok);
360 defp->def.un.default_decl = NULL;
364 static char* reserved_words[] =
381 static char* reserved_types[] =
389 * check that the given name is not one that would eventually result in
390 * xdr routines that would conflict with internal XDR routines.
393 check_type_name(name, new_type)
400 for (i = 0; reserved_words[i] != NULL; i++) {
401 if (strcmp(name, reserved_words[i]) == 0) {
403 "illegal (reserved) name :\'%s\' in type definition",
409 for (i = 0; reserved_types[i] != NULL; i++) {
410 if (strcmp(name, reserved_types[i]) == 0) {
412 "illegal (reserved) name :\'%s\' in type definition",
428 defp->def_kind = DEF_TYPEDEF;
429 get_declaration(&dec, DEF_TYPEDEF);
430 defp->def_name = dec.name;
431 check_type_name(dec.name, 1);
432 defp->def.ty.old_prefix = dec.prefix;
433 defp->def.ty.old_type = dec.type;
434 defp->def.ty.rel = dec.rel;
435 defp->def.ty.array_max = dec.array_max;
439 get_declaration(dec, dkind)
445 get_type(&dec->prefix, &dec->type, dkind);
446 dec->rel = REL_ALIAS;
447 if (streq(dec->type, "void")) {
451 check_type_name(dec->type, 0);
452 scan2(TOK_STAR, TOK_IDENT, &tok);
453 if (tok.kind == TOK_STAR) {
454 dec->rel = REL_POINTER;
455 scan(TOK_IDENT, &tok);
458 if (peekscan(TOK_LBRACKET, &tok)) {
459 if (dec->rel == REL_POINTER) {
460 error("no array-of-pointer declarations -- use typedef");
462 dec->rel = REL_VECTOR;
464 dec->array_max = tok.str;
465 scan(TOK_RBRACKET, &tok);
466 } else if (peekscan(TOK_LANGLE, &tok)) {
467 if (dec->rel == REL_POINTER) {
468 error("no array-of-pointer declarations -- use typedef");
470 dec->rel = REL_ARRAY;
471 if (peekscan(TOK_RANGLE, &tok)) {
472 dec->array_max = "~0"; /* unspecified size, use max */
475 dec->array_max = tok.str;
476 scan(TOK_RANGLE, &tok);
479 if (streq(dec->type, "opaque")) {
480 if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR) {
481 error("array declaration expected");
483 } else if (streq(dec->type, "string")) {
484 if (dec->rel != REL_ARRAY) {
485 error("variable-length array declaration expected");
492 get_prog_declaration(dec, dkind, num)
495 int num; /* arg number */
498 char name[10]; /* argument name */
500 if (dkind == DEF_PROGRAM) {
502 if (tok.kind == TOK_RPAREN) { /* no arguments */
503 dec->rel = REL_ALIAS;
510 get_type(&dec->prefix, &dec->type, dkind);
511 dec->rel = REL_ALIAS;
512 if (peekscan(TOK_IDENT, &tok)) /* optional name of argument */
513 strcpy(name, tok.str);
515 sprintf(name, "%s%d", ARGNAME, num);
516 /* default name of argument */
518 dec->name = (char *) xstrdup(name);
519 if (streq(dec->type, "void")) {
523 if (streq(dec->type, "opaque")) {
524 error("opaque -- illegal argument type");
526 if (peekscan(TOK_STAR, &tok)) {
527 if (streq(dec->type, "string")) {
528 error("pointer to string not allowed in program arguments");
530 dec->rel = REL_POINTER;
531 if (peekscan(TOK_IDENT, &tok)) {
532 /* optional name of argument */
533 dec->name = xstrdup(tok.str);
536 if (peekscan(TOK_LANGLE, &tok)) {
537 if (!streq(dec->type, "string")) {
538 error("arrays cannot be declared as arguments to procedures -- use typedef");
540 dec->rel = REL_ARRAY;
541 if (peekscan(TOK_RANGLE, &tok)) {
542 dec->array_max = "~0";
543 /* unspecified size, use max */
546 dec->array_max = tok.str;
547 scan(TOK_RANGLE, &tok);
550 if (streq(dec->type, "string")) {
551 if (dec->rel != REL_ARRAY) {
553 * .x specifies just string as
557 dec->rel = REL_ARRAY;
558 dec->array_max = "~0"; /* unspecified size, use max */
566 get_type(prefixp, typep, dkind)
583 scan(TOK_IDENT, &tok);
591 (void) peekscan(TOK_INT, &tok);
595 (void) peekscan(TOK_INT, &tok);
599 (void) peekscan(TOK_INT, &tok);
603 if (dkind != DEF_UNION && dkind != DEF_PROGRAM) {
604 error("voids allowed only inside union and program definitions with one argument");
619 error("expected type specifier");
638 (void) peekscan(TOK_INT, &tok);
643 (void) peekscan(TOK_INT, &tok);
647 *typep = "u_int64_t";
649 (void) peekscan(TOK_INT, &tok);