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
31 * rpc_parse.c, Parser for the RPC protocol compiler
32 * Copyright (C) 1987 Sun Microsystems, Inc.
36 #include "rpc/types.h"
37 #include "rpc_parse.h"
43 static void isdefined( definition * );
44 static void def_struct( definition * );
45 static void def_program( definition * );
46 static void def_enum( definition * );
47 static void def_const( definition * );
48 static void def_union( definition * );
49 static void def_typedef( definition * );
50 static void get_declaration( declaration *, defkind );
51 static void get_prog_declaration( declaration *, defkind, int );
52 static void get_type(const char **, const char **, defkind);
53 static void unsigned_dec(const char ** );
56 * return the next definition you see
64 defp = XALLOC(definition);
89 error("definition keyword expected");
91 scan(TOK_SEMICOLON, &tok);
97 isdefined(definition *defp)
99 STOREVAL(&defined, defp);
103 def_struct(definition *defp)
110 defp->def_kind = DEF_STRUCT;
112 scan(TOK_IDENT, &tok);
113 defp->def_name = tok.str;
114 scan(TOK_LBRACE, &tok);
115 tailp = &defp->def.st.decls;
117 get_declaration(&dec, DEF_STRUCT);
118 decls = XALLOC(decl_list);
121 tailp = &decls->next;
122 scan(TOK_SEMICOLON, &tok);
124 } while (tok.kind != TOK_RBRACE);
130 def_program(definition *defp)
137 version_list **vtailp;
141 bool_t isvoid = FALSE; /* whether first argument is void */
142 defp->def_kind = DEF_PROGRAM;
143 scan(TOK_IDENT, &tok);
144 defp->def_name = tok.str;
145 scan(TOK_LBRACE, &tok);
146 vtailp = &defp->def.pr.versions;
147 tailp = &defp->def.st.decls;
148 scan(TOK_VERSION, &tok);
150 scan(TOK_IDENT, &tok);
151 vlist = XALLOC(version_list);
152 vlist->vers_name = tok.str;
153 scan(TOK_LBRACE, &tok);
154 ptailp = &vlist->procs;
156 /* get result type */
157 plist = XALLOC(proc_list);
158 get_type(&plist->res_prefix, &plist->res_type,
160 if (streq(plist->res_type, "opaque")) {
161 error("illegal result type");
163 scan(TOK_IDENT, &tok);
164 plist->proc_name = tok.str;
165 scan(TOK_LPAREN, &tok);
166 /* get args - first one */
170 * type of DEF_PROGRAM in the first
171 * get_prog_declaration and DEF_STURCT in the next
172 * allows void as argument if it is the only argument
174 get_prog_declaration(&dec, DEF_PROGRAM, num_args);
175 if (streq(dec.type, "void"))
177 decls = XALLOC(decl_list);
178 plist->args.decls = decls;
180 tailp = &decls->next;
182 while (peekscan(TOK_COMMA, &tok)) {
184 get_prog_declaration(&dec, DEF_STRUCT,
186 decls = XALLOC(decl_list);
189 if (streq(dec.type, "void"))
191 tailp = &decls->next;
193 /* multiple arguments are only allowed in newstyle */
194 if (!newstyle && num_args > 1) {
195 error("only one argument is allowed");
197 if (isvoid && num_args > 1) {
198 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;
220 plist = plist->next) {
221 plist->args.argname = make_argname(plist->proc_name,
223 /* free the memory ?? */
225 scan(TOK_SEMICOLON, &tok);
226 scan2(TOK_VERSION, TOK_RBRACE, &tok);
227 } while (tok.kind == TOK_VERSION);
228 scan(TOK_EQUAL, &tok);
230 defp->def.pr.prog_num = tok.str;
236 def_enum(definition *defp)
240 enumval_list **tailp;
242 defp->def_kind = DEF_ENUM;
243 scan(TOK_IDENT, &tok);
244 defp->def_name = tok.str;
245 scan(TOK_LBRACE, &tok);
246 tailp = &defp->def.en.vals;
248 scan(TOK_IDENT, &tok);
249 elist = XALLOC(enumval_list);
250 elist->name = tok.str;
251 elist->assignment = NULL;
252 scan3(TOK_COMMA, TOK_RBRACE, TOK_EQUAL, &tok);
253 if (tok.kind == TOK_EQUAL) {
255 elist->assignment = tok.str;
256 scan2(TOK_COMMA, TOK_RBRACE, &tok);
259 tailp = &elist->next;
260 } while (tok.kind != TOK_RBRACE);
265 def_const(definition *defp)
269 defp->def_kind = DEF_CONST;
270 scan(TOK_IDENT, &tok);
271 defp->def_name = tok.str;
272 scan(TOK_EQUAL, &tok);
273 scan2(TOK_IDENT, TOK_STRCONST, &tok);
274 defp->def.co = tok.str;
278 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 */
302 if (peekscan(TOK_CASE, &tok)){
304 scan2(TOK_IDENT, TOK_CHARCONST, &tok);
306 /* continued case statement */
308 tailp = &cases->next;
309 cases = XALLOC(case_list);
310 cases->case_name = tok.str;
311 scan(TOK_COLON, &tok);
312 } while (peekscan(TOK_CASE, &tok));
315 get_declaration(&dec, DEF_UNION);
316 cases->case_decl = dec;
317 cases->contflag = 0; /* no continued case statement */
319 tailp = &cases->next;
320 scan(TOK_SEMICOLON, &tok);
322 scan3(TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok);
325 if (tok.kind == TOK_DEFAULT) {
326 scan(TOK_COLON, &tok);
327 get_declaration(&dec, DEF_UNION);
328 defp->def.un.default_decl = XALLOC(declaration);
329 *defp->def.un.default_decl = dec;
330 scan(TOK_SEMICOLON, &tok);
331 scan(TOK_RBRACE, &tok);
333 defp->def.un.default_decl = NULL;
337 static const char *reserved_words[] =
354 static const char *reserved_types[] =
362 * check that the given name is not one that would eventually result in
363 * xdr routines that would conflict with internal XDR routines.
366 check_type_name(const char *name, int new_type)
371 for (i = 0; reserved_words[i] != NULL; i++) {
372 if (strcmp(name, reserved_words[i]) == 0) {
374 "illegal (reserved) name :\'%s\' in type definition",
380 for (i = 0; reserved_types[i] != NULL; i++) {
381 if (strcmp(name, reserved_types[i]) == 0) {
383 "illegal (reserved) name :\'%s\' in type definition",
394 def_typedef(definition *defp)
398 defp->def_kind = DEF_TYPEDEF;
399 get_declaration(&dec, DEF_TYPEDEF);
400 defp->def_name = dec.name;
401 check_type_name(dec.name, 1);
402 defp->def.ty.old_prefix = dec.prefix;
403 defp->def.ty.old_type = dec.type;
404 defp->def.ty.rel = dec.rel;
405 defp->def.ty.array_max = dec.array_max;
409 get_declaration(declaration *dec, defkind dkind)
413 get_type(&dec->prefix, &dec->type, dkind);
414 dec->rel = REL_ALIAS;
415 if (streq(dec->type, "void")) {
419 check_type_name(dec->type, 0);
420 scan2(TOK_STAR, TOK_IDENT, &tok);
421 if (tok.kind == TOK_STAR) {
422 dec->rel = REL_POINTER;
423 scan(TOK_IDENT, &tok);
426 if (peekscan(TOK_LBRACKET, &tok)) {
427 if (dec->rel == REL_POINTER) {
428 error("no array-of-pointer declarations -- use typedef");
430 dec->rel = REL_VECTOR;
432 dec->array_max = tok.str;
433 scan(TOK_RBRACKET, &tok);
434 } else if (peekscan(TOK_LANGLE, &tok)) {
435 if (dec->rel == REL_POINTER) {
436 error("no array-of-pointer declarations -- use typedef");
438 dec->rel = REL_ARRAY;
439 if (peekscan(TOK_RANGLE, &tok)) {
440 dec->array_max = "~0"; /* unspecified size, use max */
443 dec->array_max = tok.str;
444 scan(TOK_RANGLE, &tok);
447 if (streq(dec->type, "opaque")) {
448 if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR) {
449 error("array declaration expected");
451 } else if (streq(dec->type, "string")) {
452 if (dec->rel != REL_ARRAY) {
453 error("variable-length array declaration expected");
460 get_prog_declaration(declaration *dec, defkind dkind, int num)
463 char name[10]; /* argument name */
465 if (dkind == DEF_PROGRAM) {
467 if (tok.kind == TOK_RPAREN) { /* no arguments */
468 dec->rel = REL_ALIAS;
475 get_type(&dec->prefix, &dec->type, dkind);
476 dec->rel = REL_ALIAS;
477 if (peekscan(TOK_IDENT, &tok)) /* optional name of argument */
478 strcpy(name, tok.str);
480 sprintf(name, "%s%d", ARGNAME, num);
481 /* default name of argument */
483 dec->name = (char *) xstrdup(name);
484 if (streq(dec->type, "void")) {
488 if (streq(dec->type, "opaque")) {
489 error("opaque -- illegal argument type");
491 if (peekscan(TOK_STAR, &tok)) {
492 if (streq(dec->type, "string")) {
493 error("pointer to string not allowed in program arguments");
495 dec->rel = REL_POINTER;
496 if (peekscan(TOK_IDENT, &tok)) {
497 /* optional name of argument */
498 dec->name = xstrdup(tok.str);
501 if (peekscan(TOK_LANGLE, &tok)) {
502 if (!streq(dec->type, "string")) {
503 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 */
531 get_type(const char **prefixp, const char **typep, defkind dkind)
545 scan(TOK_IDENT, &tok);
553 (void) peekscan(TOK_INT, &tok);
557 (void) peekscan(TOK_INT, &tok);
561 (void) peekscan(TOK_INT, &tok);
565 if (dkind != DEF_UNION && dkind != DEF_PROGRAM) {
566 error("voids allowed only inside union and program definitions with one argument");
581 error("expected type specifier");
586 unsigned_dec(const char **typep)
599 (void) peekscan(TOK_INT, &tok);
604 (void) peekscan(TOK_INT, &tok);
608 *typep = "u_int64_t";
610 (void) peekscan(TOK_INT, &tok);