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
34 const char parse_rcsid
[] =
38 * rpc_parse.c, Parser for the RPC protocol compiler
39 * Copyright (C) 1987 Sun Microsystems, Inc.
43 #include "rpc/types.h"
45 #include "rpc_parse.h"
51 static void isdefined (definition
* defp
);
52 static void def_struct (definition
* defp
);
53 static void def_program (definition
* defp
);
54 static void def_enum (definition
* defp
);
55 static void def_const (definition
* defp
);
56 static void def_union (definition
* defp
);
57 static void check_type_name (const char *name
, int new_type
);
58 static void def_typedef (definition
* defp
);
59 static void get_declaration (declaration
* dec
, defkind dkind
);
60 static void get_prog_declaration (declaration
* dec
, defkind dkind
, int num
);
61 static void get_type (const char **prefixp
, const char **typep
, defkind dkind
);
62 static void unsigned_dec (const char **typep
);
65 * return the next definition you see
73 defp
= ALLOC (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
;
127 get_declaration (&dec
, DEF_STRUCT
);
128 decls
= ALLOC (decl_list
);
131 tailp
= &decls
->next
;
132 scan (TOK_SEMICOLON
, &tok
);
135 while (tok
.kind
!= TOK_RBRACE
);
141 def_program (definition
* defp
)
148 version_list
**vtailp
;
152 bool_t isvoid
= FALSE
; /* whether first argument is void */
153 defp
->def_kind
= DEF_PROGRAM
;
154 scan (TOK_IDENT
, &tok
);
155 defp
->def_name
= tok
.str
;
156 scan (TOK_LBRACE
, &tok
);
157 vtailp
= &defp
->def
.pr
.versions
;
158 tailp
= &defp
->def
.st
.decls
;
159 scan (TOK_VERSION
, &tok
);
162 scan (TOK_IDENT
, &tok
);
163 vlist
= ALLOC (version_list
);
164 vlist
->vers_name
= tok
.str
;
165 scan (TOK_LBRACE
, &tok
);
166 ptailp
= &vlist
->procs
;
169 /* get result type */
170 plist
= ALLOC (proc_list
);
171 get_type (&plist
->res_prefix
, &plist
->res_type
,
173 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 */
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
= ALLOC (decl_list
);
191 plist
->args
.decls
= decls
;
193 tailp
= &decls
->next
;
195 while (peekscan (TOK_COMMA
, &tok
))
198 get_prog_declaration (&dec
, DEF_STRUCT
,
200 decls
= ALLOC (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)
210 error ("only one argument is allowed");
212 if (isvoid
&& num_args
> 1)
214 error ("illegal use of void in program definition");
217 scan (TOK_RPAREN
, &tok
);
218 scan (TOK_EQUAL
, &tok
);
220 scan (TOK_SEMICOLON
, &tok
);
221 plist
->proc_num
= tok
.str
;
222 plist
->arg_num
= num_args
;
224 ptailp
= &plist
->next
;
227 while (tok
.kind
!= TOK_RBRACE
);
230 vtailp
= &vlist
->next
;
231 scan (TOK_RBRACE
, &tok
);
232 scan (TOK_EQUAL
, &tok
);
234 vlist
->vers_num
= tok
.str
;
235 /* make the argument structure name for each arg */
236 for (plist
= vlist
->procs
; plist
!= NULL
;
239 plist
->args
.argname
= make_argname (plist
->proc_name
,
241 /* free the memory ?? */
243 scan (TOK_SEMICOLON
, &tok
);
244 scan2 (TOK_VERSION
, TOK_RBRACE
, &tok
);
246 while (tok
.kind
== TOK_VERSION
);
247 scan (TOK_EQUAL
, &tok
);
249 defp
->def
.pr
.prog_num
= tok
.str
;
255 def_enum (definition
* defp
)
259 enumval_list
**tailp
;
261 defp
->def_kind
= DEF_ENUM
;
262 scan (TOK_IDENT
, &tok
);
263 defp
->def_name
= tok
.str
;
264 scan (TOK_LBRACE
, &tok
);
265 tailp
= &defp
->def
.en
.vals
;
268 scan (TOK_IDENT
, &tok
);
269 elist
= ALLOC (enumval_list
);
270 elist
->name
= tok
.str
;
271 elist
->assignment
= NULL
;
272 scan3 (TOK_COMMA
, TOK_RBRACE
, TOK_EQUAL
, &tok
);
273 if (tok
.kind
== TOK_EQUAL
)
276 elist
->assignment
= tok
.str
;
277 scan2 (TOK_COMMA
, TOK_RBRACE
, &tok
);
280 tailp
= &elist
->next
;
282 while (tok
.kind
!= TOK_RBRACE
);
287 def_const (definition
* defp
)
291 defp
->def_kind
= DEF_CONST
;
292 scan (TOK_IDENT
, &tok
);
293 defp
->def_name
= tok
.str
;
294 scan (TOK_EQUAL
, &tok
);
295 scan2 (TOK_IDENT
, TOK_STRCONST
, &tok
);
296 defp
->def
.co
= tok
.str
;
300 def_union (definition
*defp
)
305 /* case_list *tcase; */
309 defp
->def_kind
= DEF_UNION
;
310 scan (TOK_IDENT
, &tok
);
311 defp
->def_name
= tok
.str
;
312 scan (TOK_SWITCH
, &tok
);
313 scan (TOK_LPAREN
, &tok
);
314 get_declaration (&dec
, DEF_UNION
);
315 defp
->def
.un
.enum_decl
= dec
;
316 tailp
= &defp
->def
.un
.cases
;
317 scan (TOK_RPAREN
, &tok
);
318 scan (TOK_LBRACE
, &tok
);
319 scan (TOK_CASE
, &tok
);
320 while (tok
.kind
== TOK_CASE
)
322 scan2 (TOK_IDENT
, TOK_CHARCONST
, &tok
);
323 cases
= ALLOC (case_list
);
324 cases
->case_name
= tok
.str
;
325 scan (TOK_COLON
, &tok
);
326 /* now peek at next token */
328 if (peekscan (TOK_CASE
, &tok
))
333 scan2 (TOK_IDENT
, TOK_CHARCONST
, &tok
);
334 cases
->contflag
= 1; /* continued case statement */
336 tailp
= &cases
->next
;
337 cases
= ALLOC (case_list
);
338 cases
->case_name
= tok
.str
;
339 scan (TOK_COLON
, &tok
);
342 while (peekscan (TOK_CASE
, &tok
));
348 tailp
= &cases
->next
;
349 cases
= ALLOC (case_list
);
352 get_declaration (&dec
, DEF_UNION
);
353 cases
->case_decl
= dec
;
354 cases
->contflag
= 0; /* no continued case statement */
356 tailp
= &cases
->next
;
357 scan (TOK_SEMICOLON
, &tok
);
359 scan3 (TOK_CASE
, TOK_DEFAULT
, TOK_RBRACE
, &tok
);
362 if (tok
.kind
== TOK_DEFAULT
)
364 scan (TOK_COLON
, &tok
);
365 get_declaration (&dec
, DEF_UNION
);
366 defp
->def
.un
.default_decl
= ALLOC (declaration
);
367 *defp
->def
.un
.default_decl
= dec
;
368 scan (TOK_SEMICOLON
, &tok
);
369 scan (TOK_RBRACE
, &tok
);
373 defp
->def
.un
.default_decl
= NULL
;
377 static const char *reserved_words
[] =
394 static const char *reserved_types
[] =
402 * check that the given name is not one that would eventually result in
403 * xdr routines that would conflict with internal XDR routines.
406 check_type_name (const char *name
, int new_type
)
411 for (i
= 0; reserved_words
[i
] != NULL
; i
++)
413 if (strcmp (name
, reserved_words
[i
]) == 0)
416 "illegal (reserved) name :\'%s\' in type definition", name
);
422 for (i
= 0; reserved_types
[i
] != NULL
; i
++)
424 if (strcmp (name
, reserved_types
[i
]) == 0)
427 "illegal (reserved) name :\'%s\' in type definition", name
);
437 def_typedef (definition
* defp
)
441 defp
->def_kind
= DEF_TYPEDEF
;
442 get_declaration (&dec
, DEF_TYPEDEF
);
443 defp
->def_name
= dec
.name
;
444 check_type_name (dec
.name
, 1);
445 defp
->def
.ty
.old_prefix
= dec
.prefix
;
446 defp
->def
.ty
.old_type
= dec
.type
;
447 defp
->def
.ty
.rel
= dec
.rel
;
448 defp
->def
.ty
.array_max
= dec
.array_max
;
452 get_declaration (declaration
* dec
, defkind dkind
)
456 get_type (&dec
->prefix
, &dec
->type
, dkind
);
457 dec
->rel
= REL_ALIAS
;
458 if (streq (dec
->type
, "void"))
463 check_type_name (dec
->type
, 0);
465 scan2 (TOK_STAR
, TOK_IDENT
, &tok
);
466 if (tok
.kind
== TOK_STAR
)
468 dec
->rel
= REL_POINTER
;
469 scan (TOK_IDENT
, &tok
);
472 if (peekscan (TOK_LBRACKET
, &tok
))
474 if (dec
->rel
== REL_POINTER
)
476 error ("no array-of-pointer declarations -- use typedef");
478 dec
->rel
= REL_VECTOR
;
480 dec
->array_max
= tok
.str
;
481 scan (TOK_RBRACKET
, &tok
);
483 else if (peekscan (TOK_LANGLE
, &tok
))
485 if (dec
->rel
== REL_POINTER
)
487 error ("no array-of-pointer declarations -- use typedef");
489 dec
->rel
= REL_ARRAY
;
490 if (peekscan (TOK_RANGLE
, &tok
))
492 dec
->array_max
= "~0"; /* unspecified size, use max */
497 dec
->array_max
= tok
.str
;
498 scan (TOK_RANGLE
, &tok
);
501 if (streq (dec
->type
, "opaque"))
503 if (dec
->rel
!= REL_ARRAY
&& dec
->rel
!= REL_VECTOR
)
505 error ("array declaration expected");
508 else if (streq (dec
->type
, "string"))
510 if (dec
->rel
!= REL_ARRAY
)
512 error ("variable-length array declaration expected");
518 get_prog_declaration (declaration
* dec
, defkind dkind
, int num
/* arg number */ )
521 char name
[10]; /* argument name */
523 if (dkind
== DEF_PROGRAM
)
526 if (tok
.kind
== TOK_RPAREN
)
528 dec
->rel
= REL_ALIAS
;
535 get_type (&dec
->prefix
, &dec
->type
, dkind
);
536 dec
->rel
= REL_ALIAS
;
537 if (peekscan (TOK_IDENT
, &tok
)) /* optional name of argument */
538 strcpy (name
, tok
.str
);
540 sprintf (name
, "%s%d", ARGNAME
, num
); /* default name of argument */
542 dec
->name
= (char *) strdup (name
);
544 if (streq (dec
->type
, "void"))
549 if (streq (dec
->type
, "opaque"))
551 error ("opaque -- illegal argument type");
553 if (peekscan (TOK_STAR
, &tok
))
555 if (streq (dec
->type
, "string"))
557 error ("pointer to string not allowed in program arguments\n");
559 dec
->rel
= REL_POINTER
;
560 if (peekscan (TOK_IDENT
, &tok
)) /* optional name of argument */
561 dec
->name
= strdup (tok
.str
);
563 if (peekscan (TOK_LANGLE
, &tok
))
565 if (!streq (dec
->type
, "string"))
567 error ("arrays cannot be declared as arguments to procedures -- use typedef");
569 dec
->rel
= REL_ARRAY
;
570 if (peekscan (TOK_RANGLE
, &tok
))
572 dec
->array_max
= "~0"; /* unspecified size, use max */
577 dec
->array_max
= tok
.str
;
578 scan (TOK_RANGLE
, &tok
);
581 if (streq (dec
->type
, "string"))
583 if (dec
->rel
!= REL_ARRAY
)
584 { /* .x specifies just string as
588 dec
->rel
= REL_ARRAY
;
589 dec
->array_max
= "~0"; /* unspecified size, use max */
595 get_type (const char **prefixp
, const char **typep
, defkind dkind
)
610 scan (TOK_IDENT
, &tok
);
614 unsigned_dec (typep
);
618 (void) peekscan (TOK_INT
, &tok
);
622 (void) peekscan (TOK_INT
, &tok
);
625 if (dkind
!= DEF_UNION
&& dkind
!= DEF_PROGRAM
)
627 error ("voids allowed only inside union and program definitions with one argument");
641 error ("expected type specifier");
646 unsigned_dec (const char **typep
)
660 (void) peekscan (TOK_INT
, &tok
);
665 (void) peekscan (TOK_INT
, &tok
);