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 or with the express written consent of
8 * Sun Microsystems, Inc.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
28 * Mountain View, California 94043
32 * From: @(#)rpc_parse.c 1.8 89/02/22 (C) 1987 SMI
36 * rpc_parse.c, Parser for the RPC protocol compiler
37 * Copyright (C) 1987 Sun Microsystems, Inc.
41 #include "rpc/types.h"
43 #include "rpc_parse.h"
49 static void isdefined (definition
* defp
);
50 static void def_struct (definition
* defp
);
51 static void def_program (definition
* defp
);
52 static void def_enum (definition
* defp
);
53 static void def_const (definition
* defp
);
54 static void def_union (definition
* defp
);
55 static void check_type_name (const char *name
, int new_type
);
56 static void def_typedef (definition
* defp
);
57 static void get_declaration (declaration
* dec
, defkind dkind
);
58 static void get_prog_declaration (declaration
* dec
, defkind dkind
, int num
);
59 static void get_type (const char **prefixp
, const char **typep
, defkind dkind
);
60 static void unsigned_dec (const char **typep
);
63 * return the next definition you see
71 defp
= ALLOC (definition
);
96 error ("definition keyword expected");
98 scan (TOK_SEMICOLON
, &tok
);
104 isdefined (definition
* defp
)
106 STOREVAL (&defined
, defp
);
110 def_struct (definition
* defp
)
117 defp
->def_kind
= DEF_STRUCT
;
119 scan (TOK_IDENT
, &tok
);
120 defp
->def_name
= tok
.str
;
121 scan (TOK_LBRACE
, &tok
);
122 tailp
= &defp
->def
.st
.decls
;
125 get_declaration (&dec
, DEF_STRUCT
);
126 decls
= ALLOC (decl_list
);
129 tailp
= &decls
->next
;
130 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
);
160 scan (TOK_IDENT
, &tok
);
161 vlist
= ALLOC (version_list
);
162 vlist
->vers_name
= tok
.str
;
163 scan (TOK_LBRACE
, &tok
);
164 ptailp
= &vlist
->procs
;
167 /* get result type */
168 plist
= ALLOC (proc_list
);
169 get_type (&plist
->res_prefix
, &plist
->res_type
,
171 if (streq (plist
->res_type
, "opaque"))
173 error ("illegal result type");
175 scan (TOK_IDENT
, &tok
);
176 plist
->proc_name
= tok
.str
;
177 scan (TOK_LPAREN
, &tok
);
178 /* get args - first one */
181 /* type of DEF_PROGRAM in the first
182 * get_prog_declaration and DEF_STURCT in the next
183 * allows void as argument if it is the only argument
185 get_prog_declaration (&dec
, DEF_PROGRAM
, num_args
);
186 if (streq (dec
.type
, "void"))
188 decls
= ALLOC (decl_list
);
189 plist
->args
.decls
= decls
;
191 tailp
= &decls
->next
;
193 while (peekscan (TOK_COMMA
, &tok
))
196 get_prog_declaration (&dec
, DEF_STRUCT
,
198 decls
= ALLOC (decl_list
);
201 if (streq (dec
.type
, "void"))
203 tailp
= &decls
->next
;
205 /* multiple arguments are only allowed in newstyle */
206 if (!newstyle
&& num_args
> 1)
208 error ("only one argument is allowed");
210 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
;
225 while (tok
.kind
!= TOK_RBRACE
);
228 vtailp
= &vlist
->next
;
229 scan (TOK_RBRACE
, &tok
);
230 scan (TOK_EQUAL
, &tok
);
232 vlist
->vers_num
= tok
.str
;
233 /* make the argument structure name for each arg */
234 for (plist
= vlist
->procs
; plist
!= NULL
;
237 plist
->args
.argname
= make_argname (plist
->proc_name
,
239 /* free the memory ?? */
241 scan (TOK_SEMICOLON
, &tok
);
242 scan2 (TOK_VERSION
, TOK_RBRACE
, &tok
);
244 while (tok
.kind
== TOK_VERSION
);
245 scan (TOK_EQUAL
, &tok
);
247 defp
->def
.pr
.prog_num
= tok
.str
;
253 def_enum (definition
* defp
)
257 enumval_list
**tailp
;
259 defp
->def_kind
= DEF_ENUM
;
260 scan (TOK_IDENT
, &tok
);
261 defp
->def_name
= tok
.str
;
262 scan (TOK_LBRACE
, &tok
);
263 tailp
= &defp
->def
.en
.vals
;
266 scan (TOK_IDENT
, &tok
);
267 elist
= ALLOC (enumval_list
);
268 elist
->name
= tok
.str
;
269 elist
->assignment
= NULL
;
270 scan3 (TOK_COMMA
, TOK_RBRACE
, TOK_EQUAL
, &tok
);
271 if (tok
.kind
== TOK_EQUAL
)
274 elist
->assignment
= tok
.str
;
275 scan2 (TOK_COMMA
, TOK_RBRACE
, &tok
);
278 tailp
= &elist
->next
;
280 while (tok
.kind
!= TOK_RBRACE
);
285 def_const (definition
* defp
)
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
;
298 def_union (definition
*defp
)
303 /* case_list *tcase; */
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
)
320 scan2 (TOK_IDENT
, TOK_CHARCONST
, &tok
);
321 cases
= ALLOC (case_list
);
322 cases
->case_name
= tok
.str
;
323 scan (TOK_COLON
, &tok
);
324 /* now peek at next token */
326 if (peekscan (TOK_CASE
, &tok
))
331 scan2 (TOK_IDENT
, TOK_CHARCONST
, &tok
);
332 cases
->contflag
= 1; /* continued case statement */
334 tailp
= &cases
->next
;
335 cases
= ALLOC (case_list
);
336 cases
->case_name
= tok
.str
;
337 scan (TOK_COLON
, &tok
);
340 while (peekscan (TOK_CASE
, &tok
));
346 tailp
= &cases
->next
;
347 cases
= ALLOC (case_list
);
350 get_declaration (&dec
, DEF_UNION
);
351 cases
->case_decl
= dec
;
352 cases
->contflag
= 0; /* no continued case statement */
354 tailp
= &cases
->next
;
355 scan (TOK_SEMICOLON
, &tok
);
357 scan3 (TOK_CASE
, TOK_DEFAULT
, TOK_RBRACE
, &tok
);
360 if (tok
.kind
== TOK_DEFAULT
)
362 scan (TOK_COLON
, &tok
);
363 get_declaration (&dec
, DEF_UNION
);
364 defp
->def
.un
.default_decl
= ALLOC (declaration
);
365 *defp
->def
.un
.default_decl
= dec
;
366 scan (TOK_SEMICOLON
, &tok
);
367 scan (TOK_RBRACE
, &tok
);
371 defp
->def
.un
.default_decl
= NULL
;
375 static const char *reserved_words
[] =
392 static const char *reserved_types
[] =
400 * check that the given name is not one that would eventually result in
401 * xdr routines that would conflict with internal XDR routines.
404 check_type_name (const char *name
, int new_type
)
409 for (i
= 0; reserved_words
[i
] != NULL
; i
++)
411 if (strcmp (name
, reserved_words
[i
]) == 0)
414 "illegal (reserved) name :\'%s\' in type definition", name
);
420 for (i
= 0; reserved_types
[i
] != NULL
; i
++)
422 if (strcmp (name
, reserved_types
[i
]) == 0)
425 "illegal (reserved) name :\'%s\' in type definition", name
);
435 def_typedef (definition
* defp
)
439 defp
->def_kind
= DEF_TYPEDEF
;
440 get_declaration (&dec
, DEF_TYPEDEF
);
441 defp
->def_name
= dec
.name
;
442 check_type_name (dec
.name
, 1);
443 defp
->def
.ty
.old_prefix
= dec
.prefix
;
444 defp
->def
.ty
.old_type
= dec
.type
;
445 defp
->def
.ty
.rel
= dec
.rel
;
446 defp
->def
.ty
.array_max
= dec
.array_max
;
450 get_declaration (declaration
* dec
, defkind dkind
)
454 get_type (&dec
->prefix
, &dec
->type
, dkind
);
455 dec
->rel
= REL_ALIAS
;
456 if (streq (dec
->type
, "void"))
461 check_type_name (dec
->type
, 0);
463 scan2 (TOK_STAR
, TOK_IDENT
, &tok
);
464 if (tok
.kind
== TOK_STAR
)
466 dec
->rel
= REL_POINTER
;
467 scan (TOK_IDENT
, &tok
);
470 if (peekscan (TOK_LBRACKET
, &tok
))
472 if (dec
->rel
== REL_POINTER
)
474 error ("no array-of-pointer declarations -- use typedef");
476 dec
->rel
= REL_VECTOR
;
478 dec
->array_max
= tok
.str
;
479 scan (TOK_RBRACKET
, &tok
);
481 else if (peekscan (TOK_LANGLE
, &tok
))
483 if (dec
->rel
== REL_POINTER
)
485 error ("no array-of-pointer declarations -- use typedef");
487 dec
->rel
= REL_ARRAY
;
488 if (peekscan (TOK_RANGLE
, &tok
))
490 dec
->array_max
= "~0"; /* unspecified size, use max */
495 dec
->array_max
= tok
.str
;
496 scan (TOK_RANGLE
, &tok
);
499 if (streq (dec
->type
, "opaque"))
501 if (dec
->rel
!= REL_ARRAY
&& dec
->rel
!= REL_VECTOR
)
503 error ("array declaration expected");
506 else if (streq (dec
->type
, "string"))
508 if (dec
->rel
!= REL_ARRAY
)
510 error ("variable-length array declaration expected");
516 get_prog_declaration (declaration
* dec
, defkind dkind
, int num
/* arg number */ )
519 char name
[10]; /* argument name */
521 if (dkind
== DEF_PROGRAM
)
524 if (tok
.kind
== TOK_RPAREN
)
526 dec
->rel
= REL_ALIAS
;
533 get_type (&dec
->prefix
, &dec
->type
, dkind
);
534 dec
->rel
= REL_ALIAS
;
535 if (peekscan (TOK_IDENT
, &tok
)) /* optional name of argument */
536 strcpy (name
, tok
.str
);
538 sprintf (name
, "%s%d", ARGNAME
, num
); /* default name of argument */
540 dec
->name
= (char *) strdup (name
);
542 if (streq (dec
->type
, "void"))
547 if (streq (dec
->type
, "opaque"))
549 error ("opaque -- illegal argument type");
551 if (peekscan (TOK_STAR
, &tok
))
553 if (streq (dec
->type
, "string"))
555 error ("pointer to string not allowed in program arguments\n");
557 dec
->rel
= REL_POINTER
;
558 if (peekscan (TOK_IDENT
, &tok
)) /* optional name of argument */
559 dec
->name
= strdup (tok
.str
);
561 if (peekscan (TOK_LANGLE
, &tok
))
563 if (!streq (dec
->type
, "string"))
565 error ("arrays cannot be declared as arguments to procedures -- use typedef");
567 dec
->rel
= REL_ARRAY
;
568 if (peekscan (TOK_RANGLE
, &tok
))
570 dec
->array_max
= "~0"; /* unspecified size, use max */
575 dec
->array_max
= tok
.str
;
576 scan (TOK_RANGLE
, &tok
);
579 if (streq (dec
->type
, "string"))
581 if (dec
->rel
!= REL_ARRAY
)
582 { /* .x specifies just string as
586 dec
->rel
= REL_ARRAY
;
587 dec
->array_max
= "~0"; /* unspecified size, use max */
593 get_type (const char **prefixp
, const char **typep
, defkind dkind
)
608 scan (TOK_IDENT
, &tok
);
612 unsigned_dec (typep
);
616 (void) peekscan (TOK_INT
, &tok
);
620 (void) peekscan (TOK_INT
, &tok
);
624 (void) peekscan(TOK_INT
, &tok
);
627 if (dkind
!= DEF_UNION
&& dkind
!= DEF_PROGRAM
)
629 error ("voids allowed only inside union and program definitions with one argument");
643 error ("expected type specifier");
648 unsigned_dec (const char **typep
)
662 (void) peekscan (TOK_INT
, &tok
);
667 (void) peekscan (TOK_INT
, &tok
);
672 (void) peekscan(TOK_INT
, &tok
);