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 static char sccsid[] = "@(#)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 extern char *make_argname();
50 extern char *strdup();
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(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(char **prefixp, char **typep, defkind dkind);
64 static void unsigned_dec(char **typep);
67 * return the next definition you see
75 defp = ALLOC(definition);
99 error("definition keyword expected");
101 scan(TOK_SEMICOLON, &tok);
107 isdefined(definition *defp)
109 STOREVAL(&defined, defp);
113 def_struct(definition *defp)
120 defp->def_kind = DEF_STRUCT;
122 scan(TOK_IDENT, &tok);
123 defp->def_name = tok.str;
124 scan(TOK_LBRACE, &tok);
125 tailp = &defp->def.st.decls;
127 get_declaration(&dec, DEF_STRUCT);
128 decls = ALLOC(decl_list);
131 tailp = &decls->next;
132 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);
160 scan(TOK_IDENT, &tok);
161 vlist = ALLOC(version_list);
162 vlist->vers_name = tok.str;
163 scan(TOK_LBRACE, &tok);
164 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")) {
171 error("illegal result type");
173 scan(TOK_IDENT, &tok);
174 plist->proc_name = tok.str;
175 scan(TOK_LPAREN, &tok);
176 /* get args - first one*/
179 /* type of DEF_PROGRAM in the first
180 * get_prog_declaration and DEF_STURCT in the next
181 * allows void as argument if it is the only argument
183 get_prog_declaration(&dec, DEF_PROGRAM, num_args);
184 if (streq(dec.type, "void"))
186 decls = ALLOC(decl_list);
187 plist->args.decls = decls;
189 tailp = &decls->next;
191 while(peekscan(TOK_COMMA, &tok)) {
193 get_prog_declaration(&dec, DEF_STRUCT,
195 decls = ALLOC(decl_list);
198 if (streq(dec.type, "void"))
200 tailp = &decls->next;
202 /* multiple arguments are only allowed in newstyle */
203 if( !newstyle && num_args > 1 ) {
204 error("only one argument is allowed" );
206 if (isvoid && num_args > 1) {
207 error("illegal use of void in program definition");
210 scan(TOK_RPAREN, &tok);
211 scan(TOK_EQUAL, &tok);
213 scan(TOK_SEMICOLON, &tok);
214 plist->proc_num = tok.str;
215 plist->arg_num = num_args;
217 ptailp = &plist->next;
219 } while (tok.kind != TOK_RBRACE);
222 vtailp = &vlist->next;
223 scan(TOK_RBRACE, &tok);
224 scan(TOK_EQUAL, &tok);
226 vlist->vers_num = tok.str;
227 /* make the argument structure name for each arg*/
228 for(plist = vlist->procs; plist != NULL;
229 plist = plist->next) {
230 plist->args.argname = make_argname(plist->proc_name,
232 /* free the memory ??*/
234 scan(TOK_SEMICOLON, &tok);
235 scan2(TOK_VERSION, TOK_RBRACE, &tok);
236 } while (tok.kind == TOK_VERSION);
237 scan(TOK_EQUAL, &tok);
239 defp->def.pr.prog_num = tok.str;
245 def_enum(definition *defp)
249 enumval_list **tailp;
251 defp->def_kind = DEF_ENUM;
252 scan(TOK_IDENT, &tok);
253 defp->def_name = tok.str;
254 scan(TOK_LBRACE, &tok);
255 tailp = &defp->def.en.vals;
257 scan(TOK_IDENT, &tok);
258 elist = ALLOC(enumval_list);
259 elist->name = tok.str;
260 elist->assignment = NULL;
261 scan3(TOK_COMMA, TOK_RBRACE, TOK_EQUAL, &tok);
262 if (tok.kind == TOK_EQUAL) {
264 elist->assignment = tok.str;
265 scan2(TOK_COMMA, TOK_RBRACE, &tok);
268 tailp = &elist->next;
269 } while (tok.kind != TOK_RBRACE);
274 def_const(definition *defp)
278 defp->def_kind = DEF_CONST;
279 scan(TOK_IDENT, &tok);
280 defp->def_name = tok.str;
281 scan(TOK_EQUAL, &tok);
282 scan2(TOK_IDENT, TOK_STRCONST, &tok);
283 defp->def.co = tok.str;
287 def_union(definition *defp)
295 defp->def_kind = DEF_UNION;
296 scan(TOK_IDENT, &tok);
297 defp->def_name = tok.str;
298 scan(TOK_SWITCH, &tok);
299 scan(TOK_LPAREN, &tok);
300 get_declaration(&dec, DEF_UNION);
301 defp->def.un.enum_decl = dec;
302 tailp = &defp->def.un.cases;
303 scan(TOK_RPAREN, &tok);
304 scan(TOK_LBRACE, &tok);
305 scan(TOK_CASE, &tok);
306 while (tok.kind == TOK_CASE) {
307 scan2(TOK_IDENT, TOK_CHARCONST, &tok);
308 cases = ALLOC(case_list);
309 cases->case_name = tok.str;
310 scan(TOK_COLON, &tok);
311 /* now peek at next token */
313 if(peekscan(TOK_CASE,&tok))
318 scan2(TOK_IDENT, TOK_CHARCONST, &tok);
319 cases->contflag=1; /* continued case statement */
321 tailp = &cases->next;
322 cases = ALLOC(case_list);
323 cases->case_name = tok.str;
324 scan(TOK_COLON, &tok);
326 }while(peekscan(TOK_CASE,&tok));
333 tailp = &cases->next;
334 cases = ALLOC(case_list);
337 get_declaration(&dec, DEF_UNION);
338 cases->case_decl = dec;
339 cases->contflag=0; /* no continued case statement */
341 tailp = &cases->next;
342 scan(TOK_SEMICOLON, &tok);
344 scan3(TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok);
347 if (tok.kind == TOK_DEFAULT) {
348 scan(TOK_COLON, &tok);
349 get_declaration(&dec, DEF_UNION);
350 defp->def.un.default_decl = ALLOC(declaration);
351 *defp->def.un.default_decl = dec;
352 scan(TOK_SEMICOLON, &tok);
353 scan(TOK_RBRACE, &tok);
355 defp->def.un.default_decl = NULL;
359 static char* reserved_words[] =
376 static char* reserved_types[] =
383 /* check that the given name is not one that would eventually result in
384 xdr routines that would conflict with internal XDR routines. */
386 check_type_name(char *name, int new_type)
391 for( i = 0; reserved_words[i] != NULL; i++ ) {
392 if( strcmp( name, reserved_words[i] ) == 0 ) {
394 "illegal (reserved) name :\'%s\' in type definition", name );
399 for( i = 0; reserved_types[i] != NULL; i++ ) {
400 if( strcmp( name, reserved_types[i] ) == 0 ) {
402 "illegal (reserved) name :\'%s\' in type definition", name );
410 def_typedef(definition *defp)
414 defp->def_kind = DEF_TYPEDEF;
415 get_declaration(&dec, DEF_TYPEDEF);
416 defp->def_name = dec.name;
417 check_type_name( dec.name, 1 );
418 defp->def.ty.old_prefix = dec.prefix;
419 defp->def.ty.old_type = dec.type;
420 defp->def.ty.rel = dec.rel;
421 defp->def.ty.array_max = dec.array_max;
425 get_declaration(declaration *dec, defkind dkind)
429 get_type(&dec->prefix, &dec->type, dkind);
430 dec->rel = REL_ALIAS;
431 if (streq(dec->type, "void")) {
435 check_type_name( dec->type, 0 );
437 scan2(TOK_STAR, TOK_IDENT, &tok);
438 if (tok.kind == TOK_STAR) {
439 dec->rel = REL_POINTER;
440 scan(TOK_IDENT, &tok);
443 if (peekscan(TOK_LBRACKET, &tok)) {
444 if (dec->rel == REL_POINTER) {
445 error("no array-of-pointer declarations -- use typedef");
447 dec->rel = REL_VECTOR;
449 dec->array_max = tok.str;
450 scan(TOK_RBRACKET, &tok);
451 } else if (peekscan(TOK_LANGLE, &tok)) {
452 if (dec->rel == REL_POINTER) {
453 error("no array-of-pointer declarations -- use typedef");
455 dec->rel = REL_ARRAY;
456 if (peekscan(TOK_RANGLE, &tok)) {
457 dec->array_max = "~0"; /* unspecified size, use max */
460 dec->array_max = tok.str;
461 scan(TOK_RANGLE, &tok);
464 if (streq(dec->type, "opaque")) {
465 if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR) {
466 error("array declaration expected");
468 } else if (streq(dec->type, "string")) {
469 if (dec->rel != REL_ARRAY) {
470 error("variable-length array declaration expected");
477 get_prog_declaration(declaration *dec, defkind dkind, int num)
480 char name[10]; /* argument name */
482 if (dkind == DEF_PROGRAM) {
484 if (tok.kind == TOK_RPAREN) { /* no arguments */
485 dec->rel = REL_ALIAS;
492 get_type(&dec->prefix, &dec->type, dkind);
493 dec->rel = REL_ALIAS;
494 if (peekscan(TOK_IDENT, &tok)) /* optional name of argument */
495 strcpy(name, tok.str);
497 sprintf(name, "%s%d", ARGNAME, num); /* default name of argument */
499 dec->name = (char *) strdup(name);
501 if (streq(dec->type, "void")) {
505 if (streq(dec->type, "opaque")) {
506 error("opaque -- illegal argument type");
508 if (peekscan(TOK_STAR, &tok)) {
509 if (streq(dec->type, "string")) {
510 error("pointer to string not allowed in program arguments\n");
512 dec->rel = REL_POINTER;
513 if (peekscan(TOK_IDENT, &tok)) /* optional name of argument */
514 dec->name = strdup(tok.str);
516 if (peekscan(TOK_LANGLE, &tok)) {
517 if (!streq(dec->type, "string")) {
518 error("arrays cannot be declared as arguments to procedures -- use typedef");
520 dec->rel = REL_ARRAY;
521 if (peekscan(TOK_RANGLE, &tok)) {
522 dec->array_max = "~0";/* unspecified size, use max */
525 dec->array_max = tok.str;
526 scan(TOK_RANGLE, &tok);
529 if (streq(dec->type, "string")) {
530 if (dec->rel != REL_ARRAY) { /* .x specifies just string as
534 dec->rel = REL_ARRAY;
535 dec->array_max = "~0";/* unspecified size, use max */
543 get_type(char **prefixp, char **typep, defkind dkind)
557 scan(TOK_IDENT, &tok);
565 (void) peekscan(TOK_INT, &tok);
569 (void) peekscan(TOK_INT, &tok);
572 if (dkind != DEF_UNION && dkind != DEF_PROGRAM) {
573 error("voids allowed only inside union and program definitions with one argument");
587 error("expected type specifier");
592 unsigned_dec(char **typep)
605 (void) peekscan(TOK_INT, &tok);
610 (void) peekscan(TOK_INT, &tok);