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
29 * @(#)rpc_parse.c 1.12 93/07/05 SMI; 1.8 89/02/22 (C) 1987 SMI
30 * $FreeBSD: src/usr.bin/rpcgen/rpc_parse.c,v 1.12 2005/11/13 21:17:24 dwmalone Exp $
31 * $DragonFly: src/usr.bin/rpcgen/rpc_parse.c,v 1.7 2004/06/19 16:40:36 joerg Exp $
35 * rpc_parse.c, Parser for the RPC protocol compiler
36 * Copyright (C) 1987 Sun Microsystems, Inc.
40 #include "rpc/types.h"
41 #include "rpc_parse.h"
47 static void isdefined(definition
*);
48 static void def_struct(definition
*);
49 static void def_program(definition
*);
50 static void def_enum(definition
*);
51 static void def_const(definition
*);
52 static void def_union(definition
*);
53 static void def_typedef(definition
*);
54 static void get_declaration(declaration
*, defkind
);
55 static void get_prog_declaration(declaration
*, defkind
, int);
56 static void get_type(const char **, const char **, defkind
);
57 static void unsigned_dec(const char **);
60 * return the next definition you see
68 defp
= XALLOC(definition
);
92 error("definition keyword expected");
94 scan(TOK_SEMICOLON
, &tok
);
100 isdefined(definition
*defp
)
102 STOREVAL(&defined
, defp
);
106 def_struct(definition
*defp
)
113 defp
->def_kind
= DEF_STRUCT
;
115 scan(TOK_IDENT
, &tok
);
116 defp
->def_name
= tok
.str
;
117 scan(TOK_LBRACE
, &tok
);
118 tailp
= &defp
->def
.st
.decls
;
120 get_declaration(&dec
, DEF_STRUCT
);
121 decls
= XALLOC(decl_list
);
124 tailp
= &decls
->next
;
125 scan(TOK_SEMICOLON
, &tok
);
127 } while (tok
.kind
!= TOK_RBRACE
);
133 def_program(definition
*defp
)
140 version_list
**vtailp
;
144 bool_t isvoid
= FALSE
; /* whether first argument is void */
145 defp
->def_kind
= DEF_PROGRAM
;
146 scan(TOK_IDENT
, &tok
);
147 defp
->def_name
= tok
.str
;
148 scan(TOK_LBRACE
, &tok
);
149 vtailp
= &defp
->def
.pr
.versions
;
150 tailp
= &defp
->def
.st
.decls
;
151 scan(TOK_VERSION
, &tok
);
153 scan(TOK_IDENT
, &tok
);
154 vlist
= XALLOC(version_list
);
155 vlist
->vers_name
= tok
.str
;
156 scan(TOK_LBRACE
, &tok
);
157 ptailp
= &vlist
->procs
;
159 /* get result type */
160 plist
= XALLOC(proc_list
);
161 get_type(&plist
->res_prefix
, &plist
->res_type
,
163 if (streq(plist
->res_type
, "opaque"))
164 error("illegal result type");
165 scan(TOK_IDENT
, &tok
);
166 plist
->proc_name
= tok
.str
;
167 scan(TOK_LPAREN
, &tok
);
168 /* get args - first one */
172 * type of DEF_PROGRAM in the first
173 * get_prog_declaration and DEF_STURCT in the next
174 * allows void as argument if it is the only argument
176 get_prog_declaration(&dec
, DEF_PROGRAM
, num_args
);
177 if (streq(dec
.type
, "void"))
179 decls
= XALLOC(decl_list
);
180 plist
->args
.decls
= decls
;
182 tailp
= &decls
->next
;
184 while (peekscan(TOK_COMMA
, &tok
)) {
186 get_prog_declaration(&dec
, DEF_STRUCT
,
188 decls
= XALLOC(decl_list
);
191 if (streq(dec
.type
, "void"))
193 tailp
= &decls
->next
;
195 /* multiple arguments are only allowed in newstyle */
196 if (!newstyle
&& num_args
> 1)
197 error("only one argument is allowed");
198 if (isvoid
&& num_args
> 1)
199 error("illegal use of void in program definition");
201 scan(TOK_RPAREN
, &tok
);
202 scan(TOK_EQUAL
, &tok
);
204 scan(TOK_SEMICOLON
, &tok
);
205 plist
->proc_num
= tok
.str
;
206 plist
->arg_num
= num_args
;
208 ptailp
= &plist
->next
;
210 } while (tok
.kind
!= TOK_RBRACE
);
213 vtailp
= &vlist
->next
;
214 scan(TOK_RBRACE
, &tok
);
215 scan(TOK_EQUAL
, &tok
);
217 vlist
->vers_num
= tok
.str
;
218 /* make the argument structure name for each arg */
219 for (plist
= vlist
->procs
; plist
!= NULL
; plist
= plist
->next
) {
220 plist
->args
.argname
= make_argname(plist
->proc_name
,
222 /* free the memory ?? */
224 scan(TOK_SEMICOLON
, &tok
);
225 scan2(TOK_VERSION
, TOK_RBRACE
, &tok
);
226 } while (tok
.kind
== TOK_VERSION
);
227 scan(TOK_EQUAL
, &tok
);
229 defp
->def
.pr
.prog_num
= tok
.str
;
235 def_enum(definition
*defp
)
239 enumval_list
**tailp
;
241 defp
->def_kind
= DEF_ENUM
;
242 scan(TOK_IDENT
, &tok
);
243 defp
->def_name
= tok
.str
;
244 scan(TOK_LBRACE
, &tok
);
245 tailp
= &defp
->def
.en
.vals
;
247 scan(TOK_IDENT
, &tok
);
248 elist
= XALLOC(enumval_list
);
249 elist
->name
= tok
.str
;
250 elist
->assignment
= NULL
;
251 scan3(TOK_COMMA
, TOK_RBRACE
, TOK_EQUAL
, &tok
);
252 if (tok
.kind
== TOK_EQUAL
) {
254 elist
->assignment
= tok
.str
;
255 scan2(TOK_COMMA
, TOK_RBRACE
, &tok
);
258 tailp
= &elist
->next
;
259 } while (tok
.kind
!= TOK_RBRACE
);
264 def_const(definition
*defp
)
268 defp
->def_kind
= DEF_CONST
;
269 scan(TOK_IDENT
, &tok
);
270 defp
->def_name
= tok
.str
;
271 scan(TOK_EQUAL
, &tok
);
272 scan2(TOK_IDENT
, TOK_STRCONST
, &tok
);
273 defp
->def
.co
= tok
.str
;
277 def_union(definition
*defp
)
285 defp
->def_kind
= DEF_UNION
;
286 scan(TOK_IDENT
, &tok
);
287 defp
->def_name
= tok
.str
;
288 scan(TOK_SWITCH
, &tok
);
289 scan(TOK_LPAREN
, &tok
);
290 get_declaration(&dec
, DEF_UNION
);
291 defp
->def
.un
.enum_decl
= dec
;
292 tailp
= &defp
->def
.un
.cases
;
293 scan(TOK_RPAREN
, &tok
);
294 scan(TOK_LBRACE
, &tok
);
295 scan(TOK_CASE
, &tok
);
296 while (tok
.kind
== TOK_CASE
) {
297 scan2(TOK_IDENT
, TOK_CHARCONST
, &tok
);
298 cases
= XALLOC(case_list
);
299 cases
->case_name
= tok
.str
;
300 scan(TOK_COLON
, &tok
);
301 /* now peek at next token */
303 if (peekscan(TOK_CASE
, &tok
)){
305 scan2(TOK_IDENT
, TOK_CHARCONST
, &tok
);
307 /* continued case statement */
309 tailp
= &cases
->next
;
310 cases
= XALLOC(case_list
);
311 cases
->case_name
= tok
.str
;
312 scan(TOK_COLON
, &tok
);
313 } while (peekscan(TOK_CASE
, &tok
));
317 tailp
= &cases
->next
;
318 cases
= XALLOC(case_list
);
321 get_declaration(&dec
, DEF_UNION
);
322 cases
->case_decl
= dec
;
323 cases
->contflag
= 0; /* no continued case statement */
325 tailp
= &cases
->next
;
326 scan(TOK_SEMICOLON
, &tok
);
328 scan3(TOK_CASE
, TOK_DEFAULT
, TOK_RBRACE
, &tok
);
331 if (tok
.kind
== TOK_DEFAULT
) {
332 scan(TOK_COLON
, &tok
);
333 get_declaration(&dec
, DEF_UNION
);
334 defp
->def
.un
.default_decl
= XALLOC(declaration
);
335 *defp
->def
.un
.default_decl
= dec
;
336 scan(TOK_SEMICOLON
, &tok
);
337 scan(TOK_RBRACE
, &tok
);
339 defp
->def
.un
.default_decl
= NULL
;
343 static const char *reserved_words
[] =
360 static const char *reserved_types
[] =
368 * check that the given name is not one that would eventually result in
369 * xdr routines that would conflict with internal XDR routines.
372 check_type_name(const char *name
, int new_type
)
377 for (i
= 0; reserved_words
[i
] != NULL
; i
++) {
378 if (strcmp(name
, reserved_words
[i
]) == 0) {
380 "illegal (reserved) name :\'%s\' in type definition",
386 for (i
= 0; reserved_types
[i
] != NULL
; i
++) {
387 if (strcmp(name
, reserved_types
[i
]) == 0) {
389 "illegal (reserved) name :\'%s\' in type definition",
400 def_typedef(definition
*defp
)
404 defp
->def_kind
= DEF_TYPEDEF
;
405 get_declaration(&dec
, DEF_TYPEDEF
);
406 defp
->def_name
= dec
.name
;
407 check_type_name(dec
.name
, 1);
408 defp
->def
.ty
.old_prefix
= dec
.prefix
;
409 defp
->def
.ty
.old_type
= dec
.type
;
410 defp
->def
.ty
.rel
= dec
.rel
;
411 defp
->def
.ty
.array_max
= dec
.array_max
;
415 get_declaration(declaration
*dec
, defkind dkind
)
419 get_type(&dec
->prefix
, &dec
->type
, dkind
);
420 dec
->rel
= REL_ALIAS
;
421 if (streq(dec
->type
, "void"))
424 check_type_name(dec
->type
, 0);
425 scan2(TOK_STAR
, TOK_IDENT
, &tok
);
426 if (tok
.kind
== TOK_STAR
) {
427 dec
->rel
= REL_POINTER
;
428 scan(TOK_IDENT
, &tok
);
431 if (peekscan(TOK_LBRACKET
, &tok
)) {
432 if (dec
->rel
== REL_POINTER
)
433 error("no array-of-pointer declarations -- use typedef");
434 dec
->rel
= REL_VECTOR
;
436 dec
->array_max
= tok
.str
;
437 scan(TOK_RBRACKET
, &tok
);
438 } else if (peekscan(TOK_LANGLE
, &tok
)) {
439 if (dec
->rel
== REL_POINTER
)
440 error("no array-of-pointer declarations -- use typedef");
441 dec
->rel
= REL_ARRAY
;
442 if (peekscan(TOK_RANGLE
, &tok
)) {
443 dec
->array_max
= "~0"; /* unspecified size, use max */
446 dec
->array_max
= tok
.str
;
447 scan(TOK_RANGLE
, &tok
);
450 if (streq(dec
->type
, "opaque")) {
451 if (dec
->rel
!= REL_ARRAY
&& dec
->rel
!= REL_VECTOR
) {
452 error("array declaration expected");
454 } else if (streq(dec
->type
, "string")) {
455 if (dec
->rel
!= REL_ARRAY
) {
456 error("variable-length array declaration expected");
463 get_prog_declaration(declaration
*dec
, defkind dkind
, int num
)
466 char name
[10]; /* argument name */
468 if (dkind
== DEF_PROGRAM
) {
470 if (tok
.kind
== TOK_RPAREN
) { /* no arguments */
471 dec
->rel
= REL_ALIAS
;
478 get_type(&dec
->prefix
, &dec
->type
, dkind
);
479 dec
->rel
= REL_ALIAS
;
480 if (peekscan(TOK_IDENT
, &tok
)) /* optional name of argument */
481 strcpy(name
, tok
.str
);
483 sprintf(name
, "%s%d", ARGNAME
, num
);
484 /* default name of argument */
486 dec
->name
= (char *) xstrdup(name
);
487 if (streq(dec
->type
, "void")) {
491 if (streq(dec
->type
, "opaque"))
492 error("opaque -- illegal argument type");
493 if (peekscan(TOK_STAR
, &tok
)) {
494 if (streq(dec
->type
, "string"))
495 error("pointer to string not allowed in program arguments");
496 dec
->rel
= REL_POINTER
;
497 if (peekscan(TOK_IDENT
, &tok
)) {
498 /* optional name of argument */
499 dec
->name
= xstrdup(tok
.str
);
502 if (peekscan(TOK_LANGLE
, &tok
)) {
503 if (!streq(dec
->type
, "string"))
504 error("arrays cannot be declared as arguments to procedures -- use typedef");
505 dec
->rel
= REL_ARRAY
;
506 if (peekscan(TOK_RANGLE
, &tok
)) {
507 dec
->array_max
= "~0";
508 /* unspecified size, use max */
511 dec
->array_max
= tok
.str
;
512 scan(TOK_RANGLE
, &tok
);
515 if (streq(dec
->type
, "string")) {
516 if (dec
->rel
!= REL_ARRAY
) {
518 * .x specifies just string as
522 dec
->rel
= REL_ARRAY
;
523 dec
->array_max
= "~0"; /* unspecified size, use max */
529 get_type(const char **prefixp
, const char **typep
, defkind dkind
)
543 scan(TOK_IDENT
, &tok
);
551 peekscan(TOK_INT
, &tok
);
555 peekscan(TOK_INT
, &tok
);
559 peekscan(TOK_INT
, &tok
);
563 if (dkind
!= DEF_UNION
&& dkind
!= DEF_PROGRAM
)
564 error("voids allowed only inside union and program definitions with one argument");
578 error("expected type specifier");
583 unsigned_dec(const char **typep
)
596 peekscan(TOK_INT
, &tok
);
601 peekscan(TOK_INT
, &tok
);
605 *typep
= "u_int64_t";
606 peekscan(TOK_INT
, &tok
);