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