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);
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;
128 get_declaration(&dec, DEF_STRUCT);
129 decls = ALLOC(decl_list);
132 tailp = &decls->next;
133 scan(TOK_SEMICOLON, &tok);
135 } while (tok.kind != TOK_RBRACE);
141 def_program(definition *defp)
148 version_list **vtailp;
152 bool_t isvoid = FALSE; /* whether first argument is void */
153 defp->def_kind = DEF_PROGRAM;
154 scan(TOK_IDENT, &tok);
155 defp->def_name = tok.str;
156 scan(TOK_LBRACE, &tok);
157 vtailp = &defp->def.pr.versions;
158 tailp = &defp->def.st.decls;
159 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;
167 /* get result type */
168 plist = ALLOC(proc_list);
169 get_type(&plist->res_prefix, &plist->res_type,
171 if (streq(plist->res_type, "opaque")) {
172 error("illegal result type");
174 scan(TOK_IDENT, &tok);
175 plist->proc_name = tok.str;
176 scan(TOK_LPAREN, &tok);
177 /* get args - first one*/
180 /* type of DEF_PROGRAM in the first
181 * get_prog_declaration and DEF_STURCT in the next
182 * allows void as argument if it is the only argument
184 get_prog_declaration(&dec, DEF_PROGRAM, num_args);
185 if (streq(dec.type, "void"))
187 decls = ALLOC(decl_list);
188 plist->args.decls = decls;
190 tailp = &decls->next;
192 while(peekscan(TOK_COMMA, &tok)) {
194 get_prog_declaration(&dec, DEF_STRUCT,
196 decls = ALLOC(decl_list);
199 if (streq(dec.type, "void"))
201 tailp = &decls->next;
203 /* multiple arguments are only allowed in newstyle */
204 if( !newstyle && num_args > 1 ) {
205 error("only one argument is allowed" );
207 if (isvoid && num_args > 1) {
208 error("illegal use of void in program definition");
211 scan(TOK_RPAREN, &tok);
212 scan(TOK_EQUAL, &tok);
214 scan(TOK_SEMICOLON, &tok);
215 plist->proc_num = tok.str;
216 plist->arg_num = num_args;
218 ptailp = &plist->next;
220 } while (tok.kind != TOK_RBRACE);
223 vtailp = &vlist->next;
224 scan(TOK_RBRACE, &tok);
225 scan(TOK_EQUAL, &tok);
227 vlist->vers_num = tok.str;
228 /* make the argument structure name for each arg*/
229 for(plist = vlist->procs; plist != NULL;
230 plist = plist->next) {
231 plist->args.argname = make_argname(plist->proc_name,
233 /* free the memory ??*/
235 scan(TOK_SEMICOLON, &tok);
236 scan2(TOK_VERSION, TOK_RBRACE, &tok);
237 } while (tok.kind == TOK_VERSION);
238 scan(TOK_EQUAL, &tok);
240 defp->def.pr.prog_num = tok.str;
246 def_enum(definition *defp)
250 enumval_list **tailp;
252 defp->def_kind = DEF_ENUM;
253 scan(TOK_IDENT, &tok);
254 defp->def_name = tok.str;
255 scan(TOK_LBRACE, &tok);
256 tailp = &defp->def.en.vals;
258 scan(TOK_IDENT, &tok);
259 elist = ALLOC(enumval_list);
260 elist->name = tok.str;
261 elist->assignment = NULL;
262 scan3(TOK_COMMA, TOK_RBRACE, TOK_EQUAL, &tok);
263 if (tok.kind == TOK_EQUAL) {
265 elist->assignment = tok.str;
266 scan2(TOK_COMMA, TOK_RBRACE, &tok);
269 tailp = &elist->next;
270 } while (tok.kind != TOK_RBRACE);
275 def_const(definition *defp)
279 defp->def_kind = DEF_CONST;
280 scan(TOK_IDENT, &tok);
281 defp->def_name = tok.str;
282 scan(TOK_EQUAL, &tok);
283 scan2(TOK_IDENT, TOK_STRCONST, &tok);
284 defp->def.co = tok.str;
288 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 */
312 if(peekscan(TOK_CASE,&tok))
317 scan2(TOK_IDENT, TOK_CHARCONST, &tok);
318 cases->contflag=1; /* continued case statement */
320 tailp = &cases->next;
321 cases = ALLOC(case_list);
322 cases->case_name = tok.str;
323 scan(TOK_COLON, &tok);
325 }while(peekscan(TOK_CASE,&tok));
328 get_declaration(&dec, DEF_UNION);
329 cases->case_decl = dec;
330 cases->contflag=0; /* no continued case statement */
332 tailp = &cases->next;
333 scan(TOK_SEMICOLON, &tok);
335 scan3(TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok);
338 if (tok.kind == TOK_DEFAULT) {
339 scan(TOK_COLON, &tok);
340 get_declaration(&dec, DEF_UNION);
341 defp->def.un.default_decl = ALLOC(declaration);
342 *defp->def.un.default_decl = dec;
343 scan(TOK_SEMICOLON, &tok);
344 scan(TOK_RBRACE, &tok);
346 defp->def.un.default_decl = NULL;
350 static char* reserved_words[] =
367 static char* reserved_types[] =
374 /* check that the given name is not one that would eventually result in
375 xdr routines that would conflict with internal XDR routines. */
377 check_type_name(char *name, int new_type)
382 for( i = 0; reserved_words[i] != NULL; i++ ) {
383 if( strcmp( name, reserved_words[i] ) == 0 ) {
385 "illegal (reserved) name :\'%s\' in type definition", name );
390 for( i = 0; reserved_types[i] != NULL; i++ ) {
391 if( strcmp( name, reserved_types[i] ) == 0 ) {
393 "illegal (reserved) name :\'%s\' in type definition", name );
401 def_typedef(definition *defp)
405 defp->def_kind = DEF_TYPEDEF;
406 get_declaration(&dec, DEF_TYPEDEF);
407 defp->def_name = dec.name;
408 check_type_name( dec.name, 1 );
409 defp->def.ty.old_prefix = dec.prefix;
410 defp->def.ty.old_type = dec.type;
411 defp->def.ty.rel = dec.rel;
412 defp->def.ty.array_max = dec.array_max;
416 get_declaration(declaration *dec, defkind dkind)
420 get_type(&dec->prefix, &dec->type, dkind);
421 dec->rel = REL_ALIAS;
422 if (streq(dec->type, "void")) {
426 check_type_name( dec->type, 0 );
428 scan2(TOK_STAR, TOK_IDENT, &tok);
429 if (tok.kind == TOK_STAR) {
430 dec->rel = REL_POINTER;
431 scan(TOK_IDENT, &tok);
434 if (peekscan(TOK_LBRACKET, &tok)) {
435 if (dec->rel == REL_POINTER) {
436 error("no array-of-pointer declarations -- use typedef");
438 dec->rel = REL_VECTOR;
440 dec->array_max = tok.str;
441 scan(TOK_RBRACKET, &tok);
442 } else if (peekscan(TOK_LANGLE, &tok)) {
443 if (dec->rel == REL_POINTER) {
444 error("no array-of-pointer declarations -- use typedef");
446 dec->rel = REL_ARRAY;
447 if (peekscan(TOK_RANGLE, &tok)) {
448 dec->array_max = "~0"; /* unspecified size, use max */
451 dec->array_max = tok.str;
452 scan(TOK_RANGLE, &tok);
455 if (streq(dec->type, "opaque")) {
456 if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR) {
457 error("array declaration expected");
459 } else if (streq(dec->type, "string")) {
460 if (dec->rel != REL_ARRAY) {
461 error("variable-length array declaration expected");
468 get_prog_declaration(declaration *dec, defkind dkind, int num)
471 char name[10]; /* argument name */
473 if (dkind == DEF_PROGRAM) {
475 if (tok.kind == TOK_RPAREN) { /* no arguments */
476 dec->rel = REL_ALIAS;
483 get_type(&dec->prefix, &dec->type, dkind);
484 dec->rel = REL_ALIAS;
485 if (peekscan(TOK_IDENT, &tok)) /* optional name of argument */
486 strcpy(name, tok.str);
488 sprintf(name, "%s%d", ARGNAME, num); /* default name of argument */
490 dec->name = (char *) strdup(name);
492 if (streq(dec->type, "void")) {
496 if (streq(dec->type, "opaque")) {
497 error("opaque -- illegal argument type");
499 if (peekscan(TOK_STAR, &tok)) {
500 if (streq(dec->type, "string")) {
501 error("pointer to string not allowed in program arguments\n");
503 dec->rel = REL_POINTER;
504 if (peekscan(TOK_IDENT, &tok)) /* optional name of argument */
505 dec->name = strdup(tok.str);
507 if (peekscan(TOK_LANGLE, &tok)) {
508 if (!streq(dec->type, "string")) {
509 error("arrays cannot be declared as arguments to procedures -- use typedef");
511 dec->rel = REL_ARRAY;
512 if (peekscan(TOK_RANGLE, &tok)) {
513 dec->array_max = "~0";/* unspecified size, use max */
516 dec->array_max = tok.str;
517 scan(TOK_RANGLE, &tok);
520 if (streq(dec->type, "string")) {
521 if (dec->rel != REL_ARRAY) { /* .x specifies just string as
525 dec->rel = REL_ARRAY;
526 dec->array_max = "~0";/* unspecified size, use max */
534 get_type(char **prefixp, char **typep, defkind dkind)
548 scan(TOK_IDENT, &tok);
556 (void) peekscan(TOK_INT, &tok);
560 (void) 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(char **typep)
596 (void) peekscan(TOK_INT, &tok);
601 (void) peekscan(TOK_INT, &tok);