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
);
97 error ("definition keyword expected");
99 scan (TOK_SEMICOLON
, &tok
);
105 isdefined (definition
* defp
)
107 STOREVAL (&defined
, defp
);
111 def_struct (definition
* defp
)
118 defp
->def_kind
= DEF_STRUCT
;
120 scan (TOK_IDENT
, &tok
);
121 defp
->def_name
= tok
.str
;
122 scan (TOK_LBRACE
, &tok
);
123 tailp
= &defp
->def
.st
.decls
;
126 get_declaration (&dec
, DEF_STRUCT
);
127 decls
= ALLOC (decl_list
);
130 tailp
= &decls
->next
;
131 scan (TOK_SEMICOLON
, &tok
);
134 while (tok
.kind
!= TOK_RBRACE
);
140 def_program (definition
* defp
)
147 version_list
**vtailp
;
151 bool_t isvoid
= FALSE
; /* whether first argument is void */
152 defp
->def_kind
= DEF_PROGRAM
;
153 scan (TOK_IDENT
, &tok
);
154 defp
->def_name
= tok
.str
;
155 scan (TOK_LBRACE
, &tok
);
156 vtailp
= &defp
->def
.pr
.versions
;
157 tailp
= &defp
->def
.st
.decls
;
158 scan (TOK_VERSION
, &tok
);
161 scan (TOK_IDENT
, &tok
);
162 vlist
= ALLOC (version_list
);
163 vlist
->vers_name
= tok
.str
;
164 scan (TOK_LBRACE
, &tok
);
165 ptailp
= &vlist
->procs
;
168 /* get result type */
169 plist
= ALLOC (proc_list
);
170 get_type (&plist
->res_prefix
, &plist
->res_type
,
172 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 */
182 /* type of DEF_PROGRAM in the first
183 * get_prog_declaration and DEF_STURCT in the next
184 * allows void as argument if it is the only argument
186 get_prog_declaration (&dec
, DEF_PROGRAM
, num_args
);
187 if (streq (dec
.type
, "void"))
189 decls
= ALLOC (decl_list
);
190 plist
->args
.decls
= decls
;
192 tailp
= &decls
->next
;
194 while (peekscan (TOK_COMMA
, &tok
))
197 get_prog_declaration (&dec
, DEF_STRUCT
,
199 decls
= ALLOC (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)
209 error ("only one argument is allowed");
211 if (isvoid
&& num_args
> 1)
213 error ("illegal use of void in program definition");
216 scan (TOK_RPAREN
, &tok
);
217 scan (TOK_EQUAL
, &tok
);
219 scan (TOK_SEMICOLON
, &tok
);
220 plist
->proc_num
= tok
.str
;
221 plist
->arg_num
= num_args
;
223 ptailp
= &plist
->next
;
226 while (tok
.kind
!= TOK_RBRACE
);
229 vtailp
= &vlist
->next
;
230 scan (TOK_RBRACE
, &tok
);
231 scan (TOK_EQUAL
, &tok
);
233 vlist
->vers_num
= tok
.str
;
234 /* make the argument structure name for each arg */
235 for (plist
= vlist
->procs
; plist
!= NULL
;
238 plist
->args
.argname
= make_argname (plist
->proc_name
,
240 /* free the memory ?? */
242 scan (TOK_SEMICOLON
, &tok
);
243 scan2 (TOK_VERSION
, TOK_RBRACE
, &tok
);
245 while (tok
.kind
== TOK_VERSION
);
246 scan (TOK_EQUAL
, &tok
);
248 defp
->def
.pr
.prog_num
= tok
.str
;
254 def_enum (definition
* defp
)
258 enumval_list
**tailp
;
260 defp
->def_kind
= DEF_ENUM
;
261 scan (TOK_IDENT
, &tok
);
262 defp
->def_name
= tok
.str
;
263 scan (TOK_LBRACE
, &tok
);
264 tailp
= &defp
->def
.en
.vals
;
267 scan (TOK_IDENT
, &tok
);
268 elist
= ALLOC (enumval_list
);
269 elist
->name
= tok
.str
;
270 elist
->assignment
= NULL
;
271 scan3 (TOK_COMMA
, TOK_RBRACE
, TOK_EQUAL
, &tok
);
272 if (tok
.kind
== TOK_EQUAL
)
275 elist
->assignment
= tok
.str
;
276 scan2 (TOK_COMMA
, TOK_RBRACE
, &tok
);
279 tailp
= &elist
->next
;
281 while (tok
.kind
!= TOK_RBRACE
);
286 def_const (definition
* defp
)
290 defp
->def_kind
= DEF_CONST
;
291 scan (TOK_IDENT
, &tok
);
292 defp
->def_name
= tok
.str
;
293 scan (TOK_EQUAL
, &tok
);
294 scan2 (TOK_IDENT
, TOK_STRCONST
, &tok
);
295 defp
->def
.co
= tok
.str
;
299 def_union (definition
*defp
)
304 /* case_list *tcase; */
310 defp
->def_kind
= DEF_UNION
;
311 scan (TOK_IDENT
, &tok
);
312 defp
->def_name
= tok
.str
;
313 scan (TOK_SWITCH
, &tok
);
314 scan (TOK_LPAREN
, &tok
);
315 get_declaration (&dec
, DEF_UNION
);
316 defp
->def
.un
.enum_decl
= dec
;
317 tailp
= &defp
->def
.un
.cases
;
318 scan (TOK_RPAREN
, &tok
);
319 scan (TOK_LBRACE
, &tok
);
320 scan (TOK_CASE
, &tok
);
321 while (tok
.kind
== TOK_CASE
)
323 scan2 (TOK_IDENT
, TOK_CHARCONST
, &tok
);
324 cases
= ALLOC (case_list
);
325 cases
->case_name
= tok
.str
;
326 scan (TOK_COLON
, &tok
);
327 /* now peek at next token */
331 if (peekscan (TOK_CASE
, &tok
))
336 scan2 (TOK_IDENT
, TOK_CHARCONST
, &tok
);
337 cases
->contflag
= 1; /* continued case statement */
339 tailp
= &cases
->next
;
340 cases
= ALLOC (case_list
);
341 cases
->case_name
= tok
.str
;
342 scan (TOK_COLON
, &tok
);
345 while (peekscan (TOK_CASE
, &tok
));
352 tailp
= &cases
->next
;
353 cases
= ALLOC (case_list
);
357 get_declaration (&dec
, DEF_UNION
);
358 cases
->case_decl
= dec
;
359 cases
->contflag
= 0; /* no continued case statement */
361 tailp
= &cases
->next
;
362 scan (TOK_SEMICOLON
, &tok
);
364 scan3 (TOK_CASE
, TOK_DEFAULT
, TOK_RBRACE
, &tok
);
367 if (tok
.kind
== TOK_DEFAULT
)
369 scan (TOK_COLON
, &tok
);
370 get_declaration (&dec
, DEF_UNION
);
371 defp
->def
.un
.default_decl
= ALLOC (declaration
);
372 *defp
->def
.un
.default_decl
= dec
;
373 scan (TOK_SEMICOLON
, &tok
);
374 scan (TOK_RBRACE
, &tok
);
378 defp
->def
.un
.default_decl
= NULL
;
382 static const char *reserved_words
[] =
399 static const char *reserved_types
[] =
407 * check that the given name is not one that would eventually result in
408 * xdr routines that would conflict with internal XDR routines.
411 check_type_name (const char *name
, int new_type
)
416 for (i
= 0; reserved_words
[i
] != NULL
; i
++)
418 if (strcmp (name
, reserved_words
[i
]) == 0)
421 "illegal (reserved) name :\'%s\' in type definition", name
);
427 for (i
= 0; reserved_types
[i
] != NULL
; i
++)
429 if (strcmp (name
, reserved_types
[i
]) == 0)
432 "illegal (reserved) name :\'%s\' in type definition", name
);
442 def_typedef (definition
* defp
)
446 defp
->def_kind
= DEF_TYPEDEF
;
447 get_declaration (&dec
, DEF_TYPEDEF
);
448 defp
->def_name
= dec
.name
;
449 check_type_name (dec
.name
, 1);
450 defp
->def
.ty
.old_prefix
= dec
.prefix
;
451 defp
->def
.ty
.old_type
= dec
.type
;
452 defp
->def
.ty
.rel
= dec
.rel
;
453 defp
->def
.ty
.array_max
= dec
.array_max
;
457 get_declaration (declaration
* dec
, defkind dkind
)
461 get_type (&dec
->prefix
, &dec
->type
, dkind
);
462 dec
->rel
= REL_ALIAS
;
463 if (streq (dec
->type
, "void"))
468 check_type_name (dec
->type
, 0);
470 scan2 (TOK_STAR
, TOK_IDENT
, &tok
);
471 if (tok
.kind
== TOK_STAR
)
473 dec
->rel
= REL_POINTER
;
474 scan (TOK_IDENT
, &tok
);
477 if (peekscan (TOK_LBRACKET
, &tok
))
479 if (dec
->rel
== REL_POINTER
)
481 error ("no array-of-pointer declarations -- use typedef");
483 dec
->rel
= REL_VECTOR
;
485 dec
->array_max
= tok
.str
;
486 scan (TOK_RBRACKET
, &tok
);
488 else if (peekscan (TOK_LANGLE
, &tok
))
490 if (dec
->rel
== REL_POINTER
)
492 error ("no array-of-pointer declarations -- use typedef");
494 dec
->rel
= REL_ARRAY
;
495 if (peekscan (TOK_RANGLE
, &tok
))
497 dec
->array_max
= "~0"; /* unspecified size, use max */
502 dec
->array_max
= tok
.str
;
503 scan (TOK_RANGLE
, &tok
);
506 if (streq (dec
->type
, "opaque"))
508 if (dec
->rel
!= REL_ARRAY
&& dec
->rel
!= REL_VECTOR
)
510 error ("array declaration expected");
513 else if (streq (dec
->type
, "string"))
515 if (dec
->rel
!= REL_ARRAY
)
517 error ("variable-length array declaration expected");
523 get_prog_declaration (declaration
* dec
, defkind dkind
, int num
/* arg number */ )
526 char name
[10]; /* argument name */
528 if (dkind
== DEF_PROGRAM
)
531 if (tok
.kind
== TOK_RPAREN
)
533 dec
->rel
= REL_ALIAS
;
540 get_type (&dec
->prefix
, &dec
->type
, dkind
);
541 dec
->rel
= REL_ALIAS
;
542 if (peekscan (TOK_IDENT
, &tok
)) /* optional name of argument */
543 strcpy (name
, tok
.str
);
545 sprintf (name
, "%s%d", ARGNAME
, num
); /* default name of argument */
547 dec
->name
= (char *) strdup (name
);
549 if (streq (dec
->type
, "void"))
554 if (streq (dec
->type
, "opaque"))
556 error ("opaque -- illegal argument type");
558 if (peekscan (TOK_STAR
, &tok
))
560 if (streq (dec
->type
, "string"))
562 error ("pointer to string not allowed in program arguments\n");
564 dec
->rel
= REL_POINTER
;
565 if (peekscan (TOK_IDENT
, &tok
)) /* optional name of argument */
566 dec
->name
= strdup (tok
.str
);
568 if (peekscan (TOK_LANGLE
, &tok
))
570 if (!streq (dec
->type
, "string"))
572 error ("arrays cannot be declared as arguments to procedures -- use typedef");
574 dec
->rel
= REL_ARRAY
;
575 if (peekscan (TOK_RANGLE
, &tok
))
577 dec
->array_max
= "~0"; /* unspecified size, use max */
582 dec
->array_max
= tok
.str
;
583 scan (TOK_RANGLE
, &tok
);
586 if (streq (dec
->type
, "string"))
588 if (dec
->rel
!= REL_ARRAY
)
589 { /* .x specifies just string as
593 dec
->rel
= REL_ARRAY
;
594 dec
->array_max
= "~0"; /* unspecified size, use max */
600 get_type (const char **prefixp
, const char **typep
, defkind dkind
)
615 scan (TOK_IDENT
, &tok
);
619 unsigned_dec (typep
);
623 (void) peekscan (TOK_INT
, &tok
);
627 (void) peekscan (TOK_INT
, &tok
);
631 (void) peekscan(TOK_INT
, &tok
);
634 if (dkind
!= DEF_UNION
&& dkind
!= DEF_PROGRAM
)
636 error ("voids allowed only inside union and program definitions with one argument");
650 error ("expected type specifier");
655 unsigned_dec (const char **typep
)
669 (void) peekscan (TOK_INT
, &tok
);
674 (void) peekscan (TOK_INT
, &tok
);
679 (void) peekscan(TOK_INT
, &tok
);