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; */
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 */
327 if (peekscan (TOK_CASE, &tok))
332 scan2 (TOK_IDENT, TOK_CHARCONST, &tok);
333 cases->contflag = 1; /* continued case statement */
335 tailp = &cases->next;
336 cases = ALLOC (case_list);
337 cases->case_name = tok.str;
338 scan (TOK_COLON, &tok);
341 while (peekscan (TOK_CASE, &tok));
347 tailp = &cases->next;
348 cases = ALLOC (case_list);
351 get_declaration (&dec, DEF_UNION);
352 cases->case_decl = dec;
353 cases->contflag = 0; /* no continued case statement */
355 tailp = &cases->next;
356 scan (TOK_SEMICOLON, &tok);
358 scan3 (TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok);
361 if (tok.kind == TOK_DEFAULT)
363 scan (TOK_COLON, &tok);
364 get_declaration (&dec, DEF_UNION);
365 defp->def.un.default_decl = ALLOC (declaration);
366 *defp->def.un.default_decl = dec;
367 scan (TOK_SEMICOLON, &tok);
368 scan (TOK_RBRACE, &tok);
372 defp->def.un.default_decl = NULL;
376 static const char *reserved_words[] =
393 static const char *reserved_types[] =
401 * check that the given name is not one that would eventually result in
402 * xdr routines that would conflict with internal XDR routines.
405 check_type_name (const char *name, int new_type)
410 for (i = 0; reserved_words[i] != NULL; i++)
412 if (strcmp (name, reserved_words[i]) == 0)
415 "illegal (reserved) name :\'%s\' in type definition", name);
421 for (i = 0; reserved_types[i] != NULL; i++)
423 if (strcmp (name, reserved_types[i]) == 0)
426 "illegal (reserved) name :\'%s\' in type definition", name);
436 def_typedef (definition * defp)
440 defp->def_kind = DEF_TYPEDEF;
441 get_declaration (&dec, DEF_TYPEDEF);
442 defp->def_name = dec.name;
443 check_type_name (dec.name, 1);
444 defp->def.ty.old_prefix = dec.prefix;
445 defp->def.ty.old_type = dec.type;
446 defp->def.ty.rel = dec.rel;
447 defp->def.ty.array_max = dec.array_max;
451 get_declaration (declaration * dec, defkind dkind)
455 get_type (&dec->prefix, &dec->type, dkind);
456 dec->rel = REL_ALIAS;
457 if (streq (dec->type, "void"))
462 check_type_name (dec->type, 0);
464 scan2 (TOK_STAR, TOK_IDENT, &tok);
465 if (tok.kind == TOK_STAR)
467 dec->rel = REL_POINTER;
468 scan (TOK_IDENT, &tok);
471 if (peekscan (TOK_LBRACKET, &tok))
473 if (dec->rel == REL_POINTER)
475 error ("no array-of-pointer declarations -- use typedef");
477 dec->rel = REL_VECTOR;
479 dec->array_max = tok.str;
480 scan (TOK_RBRACKET, &tok);
482 else if (peekscan (TOK_LANGLE, &tok))
484 if (dec->rel == REL_POINTER)
486 error ("no array-of-pointer declarations -- use typedef");
488 dec->rel = REL_ARRAY;
489 if (peekscan (TOK_RANGLE, &tok))
491 dec->array_max = "~0"; /* unspecified size, use max */
496 dec->array_max = tok.str;
497 scan (TOK_RANGLE, &tok);
500 if (streq (dec->type, "opaque"))
502 if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR)
504 error ("array declaration expected");
507 else if (streq (dec->type, "string"))
509 if (dec->rel != REL_ARRAY)
511 error ("variable-length array declaration expected");
517 get_prog_declaration (declaration * dec, defkind dkind, int num /* arg number */ )
520 char name[10]; /* argument name */
522 if (dkind == DEF_PROGRAM)
525 if (tok.kind == TOK_RPAREN)
527 dec->rel = REL_ALIAS;
534 get_type (&dec->prefix, &dec->type, dkind);
535 dec->rel = REL_ALIAS;
536 if (peekscan (TOK_IDENT, &tok)) /* optional name of argument */
537 strcpy (name, tok.str);
539 sprintf (name, "%s%d", ARGNAME, num); /* default name of argument */
541 dec->name = (char *) strdup (name);
543 if (streq (dec->type, "void"))
548 if (streq (dec->type, "opaque"))
550 error ("opaque -- illegal argument type");
552 if (peekscan (TOK_STAR, &tok))
554 if (streq (dec->type, "string"))
556 error ("pointer to string not allowed in program arguments\n");
558 dec->rel = REL_POINTER;
559 if (peekscan (TOK_IDENT, &tok)) /* optional name of argument */
560 dec->name = strdup (tok.str);
562 if (peekscan (TOK_LANGLE, &tok))
564 if (!streq (dec->type, "string"))
566 error ("arrays cannot be declared as arguments to procedures -- use typedef");
568 dec->rel = REL_ARRAY;
569 if (peekscan (TOK_RANGLE, &tok))
571 dec->array_max = "~0"; /* unspecified size, use max */
576 dec->array_max = tok.str;
577 scan (TOK_RANGLE, &tok);
580 if (streq (dec->type, "string"))
582 if (dec->rel != REL_ARRAY)
583 { /* .x specifies just string as
587 dec->rel = REL_ARRAY;
588 dec->array_max = "~0"; /* unspecified size, use max */
594 get_type (const char **prefixp, const char **typep, defkind dkind)
609 scan (TOK_IDENT, &tok);
613 unsigned_dec (typep);
617 (void) peekscan (TOK_INT, &tok);
621 (void) peekscan (TOK_INT, &tok);
625 (void) peekscan(TOK_INT, &tok);
628 if (dkind != DEF_UNION && dkind != DEF_PROGRAM)
630 error ("voids allowed only inside union and program definitions with one argument");
644 error ("expected type specifier");
649 unsigned_dec (const char **typep)
663 (void) peekscan (TOK_INT, &tok);
668 (void) peekscan (TOK_INT, &tok);
673 (void) peekscan(TOK_INT, &tok);