2 * From: @(#)rpc_parse.c 1.8 89/02/22
4 * Copyright (c) 2010, Oracle America, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 * * Neither the name of the "Oracle America, Inc." nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
26 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * rpc_parse.c, Parser for the RPC protocol compiler
35 * Copyright (C) 1987 Sun Microsystems, Inc.
39 #include "rpc/types.h"
41 #include "rpc_parse.h"
47 static void isdefined (definition
* defp
);
48 static void def_struct (definition
* defp
);
49 static void def_program (definition
* defp
);
50 static void def_enum (definition
* defp
);
51 static void def_const (definition
* defp
);
52 static void def_union (definition
* defp
);
53 static void check_type_name (const char *name
, int new_type
);
54 static void def_typedef (definition
* defp
);
55 static void get_declaration (declaration
* dec
, defkind dkind
);
56 static void get_prog_declaration (declaration
* dec
, defkind dkind
, int num
);
57 static void get_type (const char **prefixp
, const char **typep
, defkind dkind
);
58 static void unsigned_dec (const char **typep
);
61 * return the next definition you see
69 defp
= ALLOC (definition
);
95 error ("definition keyword expected");
97 scan (TOK_SEMICOLON
, &tok
);
103 isdefined (definition
* defp
)
105 STOREVAL (&defined
, defp
);
109 def_struct (definition
* defp
)
116 defp
->def_kind
= DEF_STRUCT
;
118 scan (TOK_IDENT
, &tok
);
119 defp
->def_name
= tok
.str
;
120 scan (TOK_LBRACE
, &tok
);
121 tailp
= &defp
->def
.st
.decls
;
124 get_declaration (&dec
, DEF_STRUCT
);
125 decls
= ALLOC (decl_list
);
128 tailp
= &decls
->next
;
129 scan (TOK_SEMICOLON
, &tok
);
132 while (tok
.kind
!= TOK_RBRACE
);
138 def_program (definition
* defp
)
145 version_list
**vtailp
;
149 bool_t isvoid
= FALSE
; /* whether first argument is void */
150 defp
->def_kind
= DEF_PROGRAM
;
151 scan (TOK_IDENT
, &tok
);
152 defp
->def_name
= tok
.str
;
153 scan (TOK_LBRACE
, &tok
);
154 vtailp
= &defp
->def
.pr
.versions
;
155 tailp
= &defp
->def
.st
.decls
;
156 scan (TOK_VERSION
, &tok
);
159 scan (TOK_IDENT
, &tok
);
160 vlist
= ALLOC (version_list
);
161 vlist
->vers_name
= tok
.str
;
162 scan (TOK_LBRACE
, &tok
);
163 ptailp
= &vlist
->procs
;
166 /* get result type */
167 plist
= ALLOC (proc_list
);
168 get_type (&plist
->res_prefix
, &plist
->res_type
,
170 if (streq (plist
->res_type
, "opaque"))
172 error ("illegal result type");
174 scan (TOK_IDENT
, &tok
);
175 plist
->proc_name
= tok
.str
;
176 scan (TOK_LPAREN
, &tok
);
177 /* get args - first one */
180 /* type of DEF_PROGRAM in the first
181 * get_prog_declaration and DEF_STURCT in the next
182 * allows void as argument if it is the only argument
184 get_prog_declaration (&dec
, DEF_PROGRAM
, num_args
);
185 if (streq (dec
.type
, "void"))
187 decls
= ALLOC (decl_list
);
188 plist
->args
.decls
= decls
;
190 tailp
= &decls
->next
;
192 while (peekscan (TOK_COMMA
, &tok
))
195 get_prog_declaration (&dec
, DEF_STRUCT
,
197 decls
= ALLOC (decl_list
);
200 if (streq (dec
.type
, "void"))
202 tailp
= &decls
->next
;
204 /* multiple arguments are only allowed in newstyle */
205 if (!newstyle
&& num_args
> 1)
207 error ("only one argument is allowed");
209 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
;
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
;
236 plist
->args
.argname
= make_argname (plist
->proc_name
,
238 /* free the memory ?? */
240 scan (TOK_SEMICOLON
, &tok
);
241 scan2 (TOK_VERSION
, TOK_RBRACE
, &tok
);
243 while (tok
.kind
== TOK_VERSION
);
244 scan (TOK_EQUAL
, &tok
);
246 defp
->def
.pr
.prog_num
= tok
.str
;
252 def_enum (definition
* defp
)
256 enumval_list
**tailp
;
258 defp
->def_kind
= DEF_ENUM
;
259 scan (TOK_IDENT
, &tok
);
260 defp
->def_name
= tok
.str
;
261 scan (TOK_LBRACE
, &tok
);
262 tailp
= &defp
->def
.en
.vals
;
265 scan (TOK_IDENT
, &tok
);
266 elist
= ALLOC (enumval_list
);
267 elist
->name
= tok
.str
;
268 elist
->assignment
= NULL
;
269 scan3 (TOK_COMMA
, TOK_RBRACE
, TOK_EQUAL
, &tok
);
270 if (tok
.kind
== TOK_EQUAL
)
273 elist
->assignment
= tok
.str
;
274 scan2 (TOK_COMMA
, TOK_RBRACE
, &tok
);
277 tailp
= &elist
->next
;
279 while (tok
.kind
!= TOK_RBRACE
);
284 def_const (definition
* defp
)
288 defp
->def_kind
= DEF_CONST
;
289 scan (TOK_IDENT
, &tok
);
290 defp
->def_name
= tok
.str
;
291 scan (TOK_EQUAL
, &tok
);
292 scan2 (TOK_IDENT
, TOK_STRCONST
, &tok
);
293 defp
->def
.co
= tok
.str
;
297 def_union (definition
*defp
)
302 /* case_list *tcase; */
308 defp
->def_kind
= DEF_UNION
;
309 scan (TOK_IDENT
, &tok
);
310 defp
->def_name
= tok
.str
;
311 scan (TOK_SWITCH
, &tok
);
312 scan (TOK_LPAREN
, &tok
);
313 get_declaration (&dec
, DEF_UNION
);
314 defp
->def
.un
.enum_decl
= dec
;
315 tailp
= &defp
->def
.un
.cases
;
316 scan (TOK_RPAREN
, &tok
);
317 scan (TOK_LBRACE
, &tok
);
318 scan (TOK_CASE
, &tok
);
319 while (tok
.kind
== TOK_CASE
)
321 scan2 (TOK_IDENT
, TOK_CHARCONST
, &tok
);
322 cases
= ALLOC (case_list
);
323 cases
->case_name
= tok
.str
;
324 scan (TOK_COLON
, &tok
);
325 /* now peek at next token */
329 if (peekscan (TOK_CASE
, &tok
))
334 scan2 (TOK_IDENT
, TOK_CHARCONST
, &tok
);
335 cases
->contflag
= 1; /* continued case statement */
337 tailp
= &cases
->next
;
338 cases
= ALLOC (case_list
);
339 cases
->case_name
= tok
.str
;
340 scan (TOK_COLON
, &tok
);
343 while (peekscan (TOK_CASE
, &tok
));
350 tailp
= &cases
->next
;
351 cases
= ALLOC (case_list
);
355 get_declaration (&dec
, DEF_UNION
);
356 cases
->case_decl
= dec
;
357 cases
->contflag
= 0; /* no continued case statement */
359 tailp
= &cases
->next
;
360 scan (TOK_SEMICOLON
, &tok
);
362 scan3 (TOK_CASE
, TOK_DEFAULT
, TOK_RBRACE
, &tok
);
365 if (tok
.kind
== TOK_DEFAULT
)
367 scan (TOK_COLON
, &tok
);
368 get_declaration (&dec
, DEF_UNION
);
369 defp
->def
.un
.default_decl
= ALLOC (declaration
);
370 *defp
->def
.un
.default_decl
= dec
;
371 scan (TOK_SEMICOLON
, &tok
);
372 scan (TOK_RBRACE
, &tok
);
376 defp
->def
.un
.default_decl
= NULL
;
380 static const char *reserved_words
[] =
397 static const char *reserved_types
[] =
405 * check that the given name is not one that would eventually result in
406 * xdr routines that would conflict with internal XDR routines.
409 check_type_name (const char *name
, int new_type
)
414 for (i
= 0; reserved_words
[i
] != NULL
; i
++)
416 if (strcmp (name
, reserved_words
[i
]) == 0)
419 "illegal (reserved) name :\'%s\' in type definition", name
);
425 for (i
= 0; reserved_types
[i
] != NULL
; i
++)
427 if (strcmp (name
, reserved_types
[i
]) == 0)
430 "illegal (reserved) name :\'%s\' in type definition", name
);
440 def_typedef (definition
* defp
)
444 defp
->def_kind
= DEF_TYPEDEF
;
445 get_declaration (&dec
, DEF_TYPEDEF
);
446 defp
->def_name
= dec
.name
;
447 check_type_name (dec
.name
, 1);
448 defp
->def
.ty
.old_prefix
= dec
.prefix
;
449 defp
->def
.ty
.old_type
= dec
.type
;
450 defp
->def
.ty
.rel
= dec
.rel
;
451 defp
->def
.ty
.array_max
= dec
.array_max
;
455 get_declaration (declaration
* dec
, defkind dkind
)
459 get_type (&dec
->prefix
, &dec
->type
, dkind
);
460 dec
->rel
= REL_ALIAS
;
461 if (streq (dec
->type
, "void"))
466 check_type_name (dec
->type
, 0);
468 scan2 (TOK_STAR
, TOK_IDENT
, &tok
);
469 if (tok
.kind
== TOK_STAR
)
471 dec
->rel
= REL_POINTER
;
472 scan (TOK_IDENT
, &tok
);
475 if (peekscan (TOK_LBRACKET
, &tok
))
477 if (dec
->rel
== REL_POINTER
)
479 error ("no array-of-pointer declarations -- use typedef");
481 dec
->rel
= REL_VECTOR
;
483 dec
->array_max
= tok
.str
;
484 scan (TOK_RBRACKET
, &tok
);
486 else if (peekscan (TOK_LANGLE
, &tok
))
488 if (dec
->rel
== REL_POINTER
)
490 error ("no array-of-pointer declarations -- use typedef");
492 dec
->rel
= REL_ARRAY
;
493 if (peekscan (TOK_RANGLE
, &tok
))
495 dec
->array_max
= "~0"; /* unspecified size, use max */
500 dec
->array_max
= tok
.str
;
501 scan (TOK_RANGLE
, &tok
);
504 if (streq (dec
->type
, "opaque"))
506 if (dec
->rel
!= REL_ARRAY
&& dec
->rel
!= REL_VECTOR
)
508 error ("array declaration expected");
511 else if (streq (dec
->type
, "string"))
513 if (dec
->rel
!= REL_ARRAY
)
515 error ("variable-length array declaration expected");
521 get_prog_declaration (declaration
* dec
, defkind dkind
, int num
/* arg number */ )
524 char name
[MAXLINESIZE
]; /* argument name */
526 if (dkind
== DEF_PROGRAM
)
529 if (tok
.kind
== TOK_RPAREN
)
531 dec
->rel
= REL_ALIAS
;
538 get_type (&dec
->prefix
, &dec
->type
, dkind
);
539 dec
->rel
= REL_ALIAS
;
540 if (peekscan (TOK_IDENT
, &tok
)) /* optional name of argument */
541 strcpy (name
, tok
.str
);
543 sprintf (name
, "%s%d", ARGNAME
, num
); /* default name of argument */
545 dec
->name
= (char *) strdup (name
);
547 if (streq (dec
->type
, "void"))
552 if (streq (dec
->type
, "opaque"))
554 error ("opaque -- illegal argument type");
556 if (peekscan (TOK_STAR
, &tok
))
558 if (streq (dec
->type
, "string"))
560 error ("pointer to string not allowed in program arguments\n");
562 dec
->rel
= REL_POINTER
;
563 if (peekscan (TOK_IDENT
, &tok
)) /* optional name of argument */
564 dec
->name
= strdup (tok
.str
);
566 if (peekscan (TOK_LANGLE
, &tok
))
568 if (!streq (dec
->type
, "string"))
570 error ("arrays cannot be declared as arguments to procedures -- use typedef");
572 dec
->rel
= REL_ARRAY
;
573 if (peekscan (TOK_RANGLE
, &tok
))
575 dec
->array_max
= "~0"; /* unspecified size, use max */
580 dec
->array_max
= tok
.str
;
581 scan (TOK_RANGLE
, &tok
);
584 if (streq (dec
->type
, "string"))
586 if (dec
->rel
!= REL_ARRAY
)
587 { /* .x specifies just string as
591 dec
->rel
= REL_ARRAY
;
592 dec
->array_max
= "~0"; /* unspecified size, use max */
598 get_type (const char **prefixp
, const char **typep
, defkind dkind
)
613 scan (TOK_IDENT
, &tok
);
617 unsigned_dec (typep
);
621 (void) peekscan (TOK_INT
, &tok
);
625 (void) peekscan (TOK_INT
, &tok
);
629 (void) peekscan(TOK_INT
, &tok
);
632 if (dkind
!= DEF_UNION
&& dkind
!= DEF_PROGRAM
)
634 error ("voids allowed only inside union and program definitions with one argument");
648 error ("expected type specifier");
653 unsigned_dec (const char **typep
)
667 (void) peekscan (TOK_INT
, &tok
);
672 (void) peekscan (TOK_INT
, &tok
);
677 (void) peekscan(TOK_INT
, &tok
);