rpc_parse.c revision 1.4 1 /* $NetBSD: rpc_parse.c,v 1.4 1995/06/11 21:49:58 pk Exp $ */
2 /*
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user or with the express written consent of
9 * Sun Microsystems, Inc.
10 *
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 *
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
18 *
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
22 *
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
26 *
27 * Sun Microsystems, Inc.
28 * 2550 Garcia Avenue
29 * Mountain View, California 94043
30 */
31
32 #ifndef lint
33 static char sccsid[] = "@(#)rpc_parse.c 1.8 89/02/22 (C) 1987 SMI";
34 #endif
35
36 /*
37 * rpc_parse.c, Parser for the RPC protocol compiler
38 * Copyright (C) 1987 Sun Microsystems, Inc.
39 */
40 #include <stdio.h>
41 #include "rpc/types.h"
42 #include "rpc_scan.h"
43 #include "rpc_parse.h"
44 #include "rpc_util.h"
45
46 #define ARGNAME "arg"
47
48 static isdefined __P((definition *));
49 static def_struct __P((definition *));
50 static def_program __P((definition *));
51 static def_enum __P((definition *));
52 static def_const __P((definition *));
53 static def_union __P((definition *));
54 static def_typedef __P((definition *));
55 static get_declaration __P((declaration *, defkind));
56 static get_prog_declaration __P((declaration *, defkind, int));
57 static get_type __P((char **, char **, defkind));
58 static unsigned_dec __P((char **));
59
60 /*
61 * return the next definition you see
62 */
63 definition *
64 get_definition()
65 {
66 definition *defp;
67 token tok;
68
69 defp = ALLOC(definition);
70 get_token(&tok);
71 switch (tok.kind) {
72 case TOK_STRUCT:
73 def_struct(defp);
74 break;
75 case TOK_UNION:
76 def_union(defp);
77 break;
78 case TOK_TYPEDEF:
79 def_typedef(defp);
80 break;
81 case TOK_ENUM:
82 def_enum(defp);
83 break;
84 case TOK_PROGRAM:
85 def_program(defp);
86 break;
87 case TOK_CONST:
88 def_const(defp);
89 break;
90 case TOK_EOF:
91 return (NULL);
92 default:
93 error("definition keyword expected");
94 }
95 scan(TOK_SEMICOLON, &tok);
96 isdefined(defp);
97 return (defp);
98 }
99
100 static
101 isdefined(defp)
102 definition *defp;
103 {
104 STOREVAL(&defined, defp);
105 }
106
107 static
108 def_struct(defp)
109 definition *defp;
110 {
111 token tok;
112 declaration dec;
113 decl_list *decls;
114 decl_list **tailp;
115
116 defp->def_kind = DEF_STRUCT;
117
118 scan(TOK_IDENT, &tok);
119 defp->def_name = tok.str;
120 scan(TOK_LBRACE, &tok);
121 tailp = &defp->def.st.decls;
122 do {
123 get_declaration(&dec, DEF_STRUCT);
124 decls = ALLOC(decl_list);
125 decls->decl = dec;
126 *tailp = decls;
127 tailp = &decls->next;
128 scan(TOK_SEMICOLON, &tok);
129 peek(&tok);
130 } while (tok.kind != TOK_RBRACE);
131 get_token(&tok);
132 *tailp = NULL;
133 }
134
135 static
136 def_program(defp)
137 definition *defp;
138 {
139 token tok;
140 declaration dec;
141 decl_list *decls;
142 decl_list **tailp;
143 version_list *vlist;
144 version_list **vtailp;
145 proc_list *plist;
146 proc_list **ptailp;
147 int num_args;
148 bool_t isvoid = FALSE; /* whether first argument is void */
149 defp->def_kind = DEF_PROGRAM;
150 scan(TOK_IDENT, &tok);
151 defp->def_name = tok.str;
152 scan(TOK_LBRACE, &tok);
153 vtailp = &defp->def.pr.versions;
154 tailp = &defp->def.st.decls;
155 scan(TOK_VERSION, &tok);
156 do {
157 scan(TOK_IDENT, &tok);
158 vlist = ALLOC(version_list);
159 vlist->vers_name = tok.str;
160 scan(TOK_LBRACE, &tok);
161 ptailp = &vlist->procs;
162 do {
163 /* get result type */
164 plist = ALLOC(proc_list);
165 get_type(&plist->res_prefix, &plist->res_type,
166 DEF_PROGRAM);
167 if (streq(plist->res_type, "opaque")) {
168 error("illegal result type");
169 }
170 scan(TOK_IDENT, &tok);
171 plist->proc_name = tok.str;
172 scan(TOK_LPAREN, &tok);
173 /* get args - first one*/
174 num_args = 1;
175 isvoid = FALSE;
176 /* type of DEF_PROGRAM in the first
177 * get_prog_declaration and DEF_STURCT in the next
178 * allows void as argument if it is the only argument
179 */
180 get_prog_declaration(&dec, DEF_PROGRAM, num_args);
181 if (streq(dec.type, "void"))
182 isvoid = TRUE;
183 decls = ALLOC(decl_list);
184 plist->args.decls = decls;
185 decls->decl = dec;
186 tailp = &decls->next;
187 /* get args */
188 while(peekscan(TOK_COMMA, &tok)) {
189 num_args++;
190 get_prog_declaration(&dec, DEF_STRUCT,
191 num_args);
192 decls = ALLOC(decl_list);
193 decls->decl = dec;
194 *tailp = decls;
195 if (streq(dec.type, "void"))
196 isvoid = TRUE;
197 tailp = &decls->next;
198 }
199 /* multiple arguments are only allowed in newstyle */
200 if( !newstyle && num_args > 1 ) {
201 error("only one argument is allowed" );
202 }
203 if (isvoid && num_args > 1) {
204 error("illegal use of void in program definition");
205 }
206 *tailp = NULL;
207 scan(TOK_RPAREN, &tok);
208 scan(TOK_EQUAL, &tok);
209 scan_num(&tok);
210 scan(TOK_SEMICOLON, &tok);
211 plist->proc_num = tok.str;
212 plist->arg_num = num_args;
213 *ptailp = plist;
214 ptailp = &plist->next;
215 peek(&tok);
216 } while (tok.kind != TOK_RBRACE);
217 *ptailp = NULL;
218 *vtailp = vlist;
219 vtailp = &vlist->next;
220 scan(TOK_RBRACE, &tok);
221 scan(TOK_EQUAL, &tok);
222 scan_num(&tok);
223 vlist->vers_num = tok.str;
224 /* make the argument structure name for each arg*/
225 for(plist = vlist->procs; plist != NULL;
226 plist = plist->next) {
227 plist->args.argname = make_argname(plist->proc_name,
228 vlist->vers_num);
229 /* free the memory ??*/
230 }
231 scan(TOK_SEMICOLON, &tok);
232 scan2(TOK_VERSION, TOK_RBRACE, &tok);
233 } while (tok.kind == TOK_VERSION);
234 scan(TOK_EQUAL, &tok);
235 scan_num(&tok);
236 defp->def.pr.prog_num = tok.str;
237 *vtailp = NULL;
238 }
239
240
241 static
242 def_enum(defp)
243 definition *defp;
244 {
245 token tok;
246 enumval_list *elist;
247 enumval_list **tailp;
248
249 defp->def_kind = DEF_ENUM;
250 scan(TOK_IDENT, &tok);
251 defp->def_name = tok.str;
252 scan(TOK_LBRACE, &tok);
253 tailp = &defp->def.en.vals;
254 do {
255 scan(TOK_IDENT, &tok);
256 elist = ALLOC(enumval_list);
257 elist->name = tok.str;
258 elist->assignment = NULL;
259 scan3(TOK_COMMA, TOK_RBRACE, TOK_EQUAL, &tok);
260 if (tok.kind == TOK_EQUAL) {
261 scan_num(&tok);
262 elist->assignment = tok.str;
263 scan2(TOK_COMMA, TOK_RBRACE, &tok);
264 }
265 *tailp = elist;
266 tailp = &elist->next;
267 } while (tok.kind != TOK_RBRACE);
268 *tailp = NULL;
269 }
270
271 static
272 def_const(defp)
273 definition *defp;
274 {
275 token tok;
276
277 defp->def_kind = DEF_CONST;
278 scan(TOK_IDENT, &tok);
279 defp->def_name = tok.str;
280 scan(TOK_EQUAL, &tok);
281 scan2(TOK_IDENT, TOK_STRCONST, &tok);
282 defp->def.co = tok.str;
283 }
284
285 static
286 def_union(defp)
287 definition *defp;
288 {
289 token tok;
290 declaration dec;
291 case_list *cases,*tcase;
292 case_list **tailp;
293 int flag;
294
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 flag=0;
313 if(peekscan(TOK_CASE,&tok))
314 {
315
316 do
317 {
318 scan2(TOK_IDENT, TOK_CHARCONST, &tok);
319 cases->contflag=1; /* continued case statement */
320 *tailp = cases;
321 tailp = &cases->next;
322 cases = ALLOC(case_list);
323 cases->case_name = tok.str;
324 scan(TOK_COLON, &tok);
325
326 }while(peekscan(TOK_CASE,&tok));
327 }
328 else
329 if(flag)
330 {
331
332 *tailp = cases;
333 tailp = &cases->next;
334 cases = ALLOC(case_list);
335 };
336
337 get_declaration(&dec, DEF_UNION);
338 cases->case_decl = dec;
339 cases->contflag=0; /* no continued case statement */
340 *tailp = cases;
341 tailp = &cases->next;
342 scan(TOK_SEMICOLON, &tok);
343
344 scan3(TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok);
345 }
346 *tailp = NULL;
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);
354 } else {
355 defp->def.un.default_decl = NULL;
356 }
357 }
358
359 static char* reserved_words[] = {
360 "array",
361 "bytes",
362 "destroy",
363 "free",
364 "getpos",
365 "inline",
366 "pointer",
367 "reference",
368 "setpos",
369 "sizeof",
370 "union",
371 "vector",
372 NULL
373 };
374
375 static char* reserved_types[] = {
376 "opaque",
377 "string",
378 NULL
379 };
380
381 /* check that the given name is not one that would eventually result in
382 xdr routines that would conflict with internal XDR routines. */
383 static check_type_name( name, new_type )
384 int new_type;
385 char* name;
386 {
387 int i;
388 char tmp[100];
389
390 for( i = 0; reserved_words[i] != NULL; i++ ) {
391 if( strcmp( name, reserved_words[i] ) == 0 ) {
392 sprintf(tmp,
393 "illegal (reserved) name :\'%s\' in type definition", name );
394 error(tmp);
395 }
396 }
397 if( new_type ) {
398 for( i = 0; reserved_types[i] != NULL; i++ ) {
399 if( strcmp( name, reserved_types[i] ) == 0 ) {
400 sprintf(tmp,
401 "illegal (reserved) name :\'%s\' in type definition", name );
402 error(tmp);
403 }
404 }
405 }
406 }
407
408 static
409 def_typedef(defp)
410 definition *defp;
411 {
412 declaration dec;
413
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;
422 }
423
424 static
425 get_declaration(dec, dkind)
426 declaration *dec;
427 defkind dkind;
428 {
429 token tok;
430
431 get_type(&dec->prefix, &dec->type, dkind);
432 dec->rel = REL_ALIAS;
433 if (streq(dec->type, "void")) {
434 return;
435 }
436
437 check_type_name( dec->type, 0 );
438
439 scan2(TOK_STAR, TOK_IDENT, &tok);
440 if (tok.kind == TOK_STAR) {
441 dec->rel = REL_POINTER;
442 scan(TOK_IDENT, &tok);
443 }
444 dec->name = tok.str;
445 if (peekscan(TOK_LBRACKET, &tok)) {
446 if (dec->rel == REL_POINTER) {
447 error("no array-of-pointer declarations -- use typedef");
448 }
449 dec->rel = REL_VECTOR;
450 scan_num(&tok);
451 dec->array_max = tok.str;
452 scan(TOK_RBRACKET, &tok);
453 } else if (peekscan(TOK_LANGLE, &tok)) {
454 if (dec->rel == REL_POINTER) {
455 error("no array-of-pointer declarations -- use typedef");
456 }
457 dec->rel = REL_ARRAY;
458 if (peekscan(TOK_RANGLE, &tok)) {
459 dec->array_max = "~0"; /* unspecified size, use max */
460 } else {
461 scan_num(&tok);
462 dec->array_max = tok.str;
463 scan(TOK_RANGLE, &tok);
464 }
465 }
466 if (streq(dec->type, "opaque")) {
467 if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR) {
468 error("array declaration expected");
469 }
470 } else if (streq(dec->type, "string")) {
471 if (dec->rel != REL_ARRAY) {
472 error("variable-length array declaration expected");
473 }
474 }
475 }
476
477 static
478 get_prog_declaration(dec, dkind, num)
479 declaration *dec;
480 defkind dkind;
481 int num; /* arg number */
482 {
483 token tok;
484 char name[10]; /* argument name */
485
486 if (dkind == DEF_PROGRAM) {
487 peek(&tok);
488 if (tok.kind == TOK_RPAREN) { /* no arguments */
489 dec->rel = REL_ALIAS;
490 dec->type = "void";
491 dec->prefix = NULL;
492 dec->name = NULL;
493 return;
494 }
495 }
496 get_type(&dec->prefix, &dec->type, dkind);
497 dec->rel = REL_ALIAS;
498 if (peekscan(TOK_IDENT, &tok)) /* optional name of argument */
499 strcpy(name, tok.str);
500 else
501 sprintf(name, "%s%d", ARGNAME, num); /* default name of argument */
502
503 dec->name = (char *)strdup(name);
504
505 if (streq(dec->type, "void")) {
506 return;
507 }
508
509 if (streq(dec->type, "opaque")) {
510 error("opaque -- illegal argument type");
511 }
512 if (peekscan(TOK_STAR, &tok)) {
513 if (streq(dec->type, "string")) {
514 error("pointer to string not allowed in program arguments\n");
515 }
516 dec->rel = REL_POINTER;
517 if (peekscan(TOK_IDENT, &tok)) /* optional name of argument */
518 dec->name = (char *)strdup(tok.str);
519 }
520 if (peekscan(TOK_LANGLE, &tok)) {
521 if (!streq(dec->type, "string")) {
522 error("arrays cannot be declared as arguments to procedures -- use typedef");
523 }
524 dec->rel = REL_ARRAY;
525 if (peekscan(TOK_RANGLE, &tok)) {
526 dec->array_max = "~0";/* unspecified size, use max */
527 } else {
528 scan_num(&tok);
529 dec->array_max = tok.str;
530 scan(TOK_RANGLE, &tok);
531 }
532 }
533 if (streq(dec->type, "string")) {
534 if (dec->rel != REL_ARRAY) { /* .x specifies just string as
535 * type of argument
536 * - make it string<>
537 */
538 dec->rel = REL_ARRAY;
539 dec->array_max = "~0";/* unspecified size, use max */
540 }
541 }
542 }
543
544
545
546 static
547 get_type(prefixp, typep, dkind)
548 char **prefixp;
549 char **typep;
550 defkind dkind;
551 {
552 token tok;
553
554 *prefixp = NULL;
555 get_token(&tok);
556 switch (tok.kind) {
557 case TOK_IDENT:
558 *typep = tok.str;
559 break;
560 case TOK_STRUCT:
561 case TOK_ENUM:
562 case TOK_UNION:
563 *prefixp = tok.str;
564 scan(TOK_IDENT, &tok);
565 *typep = tok.str;
566 break;
567 case TOK_UNSIGNED:
568 unsigned_dec(typep);
569 break;
570 case TOK_SHORT:
571 *typep = "short";
572 (void) peekscan(TOK_INT, &tok);
573 break;
574 case TOK_LONG:
575 *typep = "long";
576 (void) peekscan(TOK_INT, &tok);
577 break;
578 case TOK_VOID:
579 if (dkind != DEF_UNION && dkind != DEF_PROGRAM) {
580 error("voids allowed only inside union and program definitions with one argument");
581 }
582 *typep = tok.str;
583 break;
584 case TOK_STRING:
585 case TOK_OPAQUE:
586 case TOK_CHAR:
587 case TOK_INT:
588 case TOK_FLOAT:
589 case TOK_DOUBLE:
590 case TOK_BOOL:
591 *typep = tok.str;
592 break;
593 default:
594 error("expected type specifier");
595 }
596 }
597
598 static
599 unsigned_dec(typep)
600 char **typep;
601 {
602 token tok;
603
604 peek(&tok);
605 switch (tok.kind) {
606 case TOK_CHAR:
607 get_token(&tok);
608 *typep = "u_char";
609 break;
610 case TOK_SHORT:
611 get_token(&tok);
612 *typep = "u_short";
613 (void) peekscan(TOK_INT, &tok);
614 break;
615 case TOK_LONG:
616 get_token(&tok);
617 *typep = "u_long";
618 (void) peekscan(TOK_INT, &tok);
619 break;
620 case TOK_INT:
621 get_token(&tok);
622 *typep = "u_int";
623 break;
624 default:
625 *typep = "u_int";
626 break;
627 }
628 }
629