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 #if defined(LIBC_SCCS) && !defined(lint)
35 static const char parse_rcsid[] =
40 * rpc_parse.c, Parser for the RPC protocol compiler
41 * Copyright (C) 1987 Sun Microsystems, Inc.
45 #include "rpc/types.h"
47 #include "rpc_parse.h"
53 static void isdefined (definition * defp);
54 static void def_struct (definition * defp);
55 static void def_program (definition * defp);
56 static void def_enum (definition * defp);
57 static void def_const (definition * defp);
58 static void def_union (definition * defp);
59 static void check_type_name (const char *name, int new_type);
60 static void def_typedef (definition * defp);
61 static void get_declaration (declaration * dec, defkind dkind);
62 static void get_prog_declaration (declaration * dec, defkind dkind, int num);
63 static void get_type (const char **prefixp, const char **typep, defkind dkind);
64 static void unsigned_dec (const char **typep);
67 * return the next definition you see
75 defp = ALLOC (definition);
100 error ("definition keyword expected");
102 scan (TOK_SEMICOLON, &tok);
108 isdefined (definition * defp)
110 STOREVAL (&defined, defp);
114 def_struct (definition * defp)
121 defp->def_kind = DEF_STRUCT;
123 scan (TOK_IDENT, &tok);
124 defp->def_name = tok.str;
125 scan (TOK_LBRACE, &tok);
126 tailp = &defp->def.st.decls;
129 get_declaration (&dec, DEF_STRUCT);
130 decls = ALLOC (decl_list);
133 tailp = &decls->next;
134 scan (TOK_SEMICOLON, &tok);
137 while (tok.kind != TOK_RBRACE);
143 def_program (definition * defp)
150 version_list **vtailp;
154 bool_t isvoid = FALSE; /* whether first argument is void */
155 defp->def_kind = DEF_PROGRAM;
156 scan (TOK_IDENT, &tok);
157 defp->def_name = tok.str;
158 scan (TOK_LBRACE, &tok);
159 vtailp = &defp->def.pr.versions;
160 tailp = &defp->def.st.decls;
161 scan (TOK_VERSION, &tok);
164 scan (TOK_IDENT, &tok);
165 vlist = ALLOC (version_list);
166 vlist->vers_name = tok.str;
167 scan (TOK_LBRACE, &tok);
168 ptailp = &vlist->procs;
171 /* get result type */
172 plist = ALLOC (proc_list);
173 get_type (&plist->res_prefix, &plist->res_type,
175 if (streq (plist->res_type, "opaque"))
177 error ("illegal result type");
179 scan (TOK_IDENT, &tok);
180 plist->proc_name = tok.str;
181 scan (TOK_LPAREN, &tok);
182 /* get args - first one */
185 /* type of DEF_PROGRAM in the first
186 * get_prog_declaration and DEF_STURCT in the next
187 * allows void as argument if it is the only argument
189 get_prog_declaration (&dec, DEF_PROGRAM, num_args);
190 if (streq (dec.type, "void"))
192 decls = ALLOC (decl_list);
193 plist->args.decls = decls;
195 tailp = &decls->next;
197 while (peekscan (TOK_COMMA, &tok))
200 get_prog_declaration (&dec, DEF_STRUCT,
202 decls = ALLOC (decl_list);
205 if (streq (dec.type, "void"))
207 tailp = &decls->next;
209 /* multiple arguments are only allowed in newstyle */
210 if (!newstyle && num_args > 1)
212 error ("only one argument is allowed");
214 if (isvoid && num_args > 1)
216 error ("illegal use of void in program definition");
219 scan (TOK_RPAREN, &tok);
220 scan (TOK_EQUAL, &tok);
222 scan (TOK_SEMICOLON, &tok);
223 plist->proc_num = tok.str;
224 plist->arg_num = num_args;
226 ptailp = &plist->next;
229 while (tok.kind != TOK_RBRACE);
232 vtailp = &vlist->next;
233 scan (TOK_RBRACE, &tok);
234 scan (TOK_EQUAL, &tok);
236 vlist->vers_num = tok.str;
237 /* make the argument structure name for each arg */
238 for (plist = vlist->procs; plist != NULL;
241 plist->args.argname = make_argname (plist->proc_name,
243 /* free the memory ?? */
245 scan (TOK_SEMICOLON, &tok);
246 scan2 (TOK_VERSION, TOK_RBRACE, &tok);
248 while (tok.kind == TOK_VERSION);
249 scan (TOK_EQUAL, &tok);
251 defp->def.pr.prog_num = tok.str;
257 def_enum (definition * defp)
261 enumval_list **tailp;
263 defp->def_kind = DEF_ENUM;
264 scan (TOK_IDENT, &tok);
265 defp->def_name = tok.str;
266 scan (TOK_LBRACE, &tok);
267 tailp = &defp->def.en.vals;
270 scan (TOK_IDENT, &tok);
271 elist = ALLOC (enumval_list);
272 elist->name = tok.str;
273 elist->assignment = NULL;
274 scan3 (TOK_COMMA, TOK_RBRACE, TOK_EQUAL, &tok);
275 if (tok.kind == TOK_EQUAL)
278 elist->assignment = tok.str;
279 scan2 (TOK_COMMA, TOK_RBRACE, &tok);
282 tailp = &elist->next;
284 while (tok.kind != TOK_RBRACE);
289 def_const (definition * defp)
293 defp->def_kind = DEF_CONST;
294 scan (TOK_IDENT, &tok);
295 defp->def_name = tok.str;
296 scan (TOK_EQUAL, &tok);
297 scan2 (TOK_IDENT, TOK_STRCONST, &tok);
298 defp->def.co = tok.str;
302 def_union (definition *defp)
307 /* case_list *tcase; */
311 defp->def_kind = DEF_UNION;
312 scan (TOK_IDENT, &tok);
313 defp->def_name = tok.str;
314 scan (TOK_SWITCH, &tok);
315 scan (TOK_LPAREN, &tok);
316 get_declaration (&dec, DEF_UNION);
317 defp->def.un.enum_decl = dec;
318 tailp = &defp->def.un.cases;
319 scan (TOK_RPAREN, &tok);
320 scan (TOK_LBRACE, &tok);
321 scan (TOK_CASE, &tok);
322 while (tok.kind == TOK_CASE)
324 scan2 (TOK_IDENT, TOK_CHARCONST, &tok);
325 cases = ALLOC (case_list);
326 cases->case_name = tok.str;
327 scan (TOK_COLON, &tok);
328 /* now peek at next token */
330 if (peekscan (TOK_CASE, &tok))
335 scan2 (TOK_IDENT, TOK_CHARCONST, &tok);
336 cases->contflag = 1; /* continued case statement */
338 tailp = &cases->next;
339 cases = ALLOC (case_list);
340 cases->case_name = tok.str;
341 scan (TOK_COLON, &tok);
344 while (peekscan (TOK_CASE, &tok));
350 tailp = &cases->next;
351 cases = ALLOC (case_list);
354 get_declaration (&dec, DEF_UNION);
355 cases->case_decl = dec;
356 cases->contflag = 0; /* no continued case statement */
358 tailp = &cases->next;
359 scan (TOK_SEMICOLON, &tok);
361 scan3 (TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok);
364 if (tok.kind == TOK_DEFAULT)
366 scan (TOK_COLON, &tok);
367 get_declaration (&dec, DEF_UNION);
368 defp->def.un.default_decl = ALLOC (declaration);
369 *defp->def.un.default_decl = dec;
370 scan (TOK_SEMICOLON, &tok);
371 scan (TOK_RBRACE, &tok);
375 defp->def.un.default_decl = NULL;
379 static const char *reserved_words[] =
396 static const char *reserved_types[] =
404 * check that the given name is not one that would eventually result in
405 * xdr routines that would conflict with internal XDR routines.
408 check_type_name (const char *name, int new_type)
413 for (i = 0; reserved_words[i] != NULL; i++)
415 if (strcmp (name, reserved_words[i]) == 0)
418 "illegal (reserved) name :\'%s\' in type definition", name);
424 for (i = 0; reserved_types[i] != NULL; i++)
426 if (strcmp (name, reserved_types[i]) == 0)
429 "illegal (reserved) name :\'%s\' in type definition", name);
439 def_typedef (definition * defp)
443 defp->def_kind = DEF_TYPEDEF;
444 get_declaration (&dec, DEF_TYPEDEF);
445 defp->def_name = dec.name;
446 check_type_name (dec.name, 1);
447 defp->def.ty.old_prefix = dec.prefix;
448 defp->def.ty.old_type = dec.type;
449 defp->def.ty.rel = dec.rel;
450 defp->def.ty.array_max = dec.array_max;
454 get_declaration (declaration * dec, defkind dkind)
458 get_type (&dec->prefix, &dec->type, dkind);
459 dec->rel = REL_ALIAS;
460 if (streq (dec->type, "void"))
465 check_type_name (dec->type, 0);
467 scan2 (TOK_STAR, TOK_IDENT, &tok);
468 if (tok.kind == TOK_STAR)
470 dec->rel = REL_POINTER;
471 scan (TOK_IDENT, &tok);
474 if (peekscan (TOK_LBRACKET, &tok))
476 if (dec->rel == REL_POINTER)
478 error ("no array-of-pointer declarations -- use typedef");
480 dec->rel = REL_VECTOR;
482 dec->array_max = tok.str;
483 scan (TOK_RBRACKET, &tok);
485 else if (peekscan (TOK_LANGLE, &tok))
487 if (dec->rel == REL_POINTER)
489 error ("no array-of-pointer declarations -- use typedef");
491 dec->rel = REL_ARRAY;
492 if (peekscan (TOK_RANGLE, &tok))
494 dec->array_max = "~0"; /* unspecified size, use max */
499 dec->array_max = tok.str;
500 scan (TOK_RANGLE, &tok);
503 if (streq (dec->type, "opaque"))
505 if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR)
507 error ("array declaration expected");
510 else if (streq (dec->type, "string"))
512 if (dec->rel != REL_ARRAY)
514 error ("variable-length array declaration expected");
520 get_prog_declaration (declaration * dec, defkind dkind, int num /* arg number */ )
523 char name[10]; /* argument name */
525 if (dkind == DEF_PROGRAM)
528 if (tok.kind == TOK_RPAREN)
530 dec->rel = REL_ALIAS;
537 get_type (&dec->prefix, &dec->type, dkind);
538 dec->rel = REL_ALIAS;
539 if (peekscan (TOK_IDENT, &tok)) /* optional name of argument */
540 strcpy (name, tok.str);
542 sprintf (name, "%s%d", ARGNAME, num); /* default name of argument */
544 dec->name = (char *) strdup (name);
546 if (streq (dec->type, "void"))
551 if (streq (dec->type, "opaque"))
553 error ("opaque -- illegal argument type");
555 if (peekscan (TOK_STAR, &tok))
557 if (streq (dec->type, "string"))
559 error ("pointer to string not allowed in program arguments\n");
561 dec->rel = REL_POINTER;
562 if (peekscan (TOK_IDENT, &tok)) /* optional name of argument */
563 dec->name = strdup (tok.str);
565 if (peekscan (TOK_LANGLE, &tok))
567 if (!streq (dec->type, "string"))
569 error ("arrays cannot be declared as arguments to procedures -- use typedef");
571 dec->rel = REL_ARRAY;
572 if (peekscan (TOK_RANGLE, &tok))
574 dec->array_max = "~0"; /* unspecified size, use max */
579 dec->array_max = tok.str;
580 scan (TOK_RANGLE, &tok);
583 if (streq (dec->type, "string"))
585 if (dec->rel != REL_ARRAY)
586 { /* .x specifies just string as
590 dec->rel = REL_ARRAY;
591 dec->array_max = "~0"; /* unspecified size, use max */
597 get_type (const char **prefixp, const char **typep, defkind dkind)
612 scan (TOK_IDENT, &tok);
616 unsigned_dec (typep);
620 (void) peekscan (TOK_INT, &tok);
624 (void) peekscan (TOK_INT, &tok);
628 (void) peekscan(TOK_INT, &tok);
631 if (dkind != DEF_UNION && dkind != DEF_PROGRAM)
633 error ("voids allowed only inside union and program definitions with one argument");
647 error ("expected type specifier");
652 unsigned_dec (const char **typep)
666 (void) peekscan (TOK_INT, &tok);
671 (void) peekscan (TOK_INT, &tok);
676 (void) peekscan(TOK_INT, &tok);