Expand PMF_FN_* macros.
[netbsd-mini2440.git] / usr.bin / rpcgen / rpc_parse.c
blob84c298d77946f209da5931dc2bcd71fea9ef00f9
1 /* $NetBSD: rpc_parse.c,v 1.15 2006/04/04 21:27:42 christos 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.
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.
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.
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.
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.
27 * Sun Microsystems, Inc.
28 * 2550 Garcia Avenue
29 * Mountain View, California 94043
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
36 #include <sys/cdefs.h>
37 #if defined(__RCSID) && !defined(lint)
38 #if 0
39 static char sccsid[] = "@(#)rpc_parse.c 1.8 89/02/22 (C) 1987 SMI";
40 #else
41 __RCSID("$NetBSD: rpc_parse.c,v 1.15 2006/04/04 21:27:42 christos Exp $");
42 #endif
43 #endif
46 * rpc_parse.c, Parser for the RPC protocol compiler
47 * Copyright (C) 1987 Sun Microsystems, Inc.
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include "rpc/types.h"
53 #include "rpc_scan.h"
54 #include "rpc_parse.h"
55 #include "rpc_util.h"
57 #define ARGNAME "arg"
59 static void isdefined __P((definition *));
60 static void def_struct __P((definition *));
61 static void def_program __P((definition *));
62 static void def_enum __P((definition *));
63 static void def_const __P((definition *));
64 static void def_union __P((definition *));
65 static void check_type_name __P((char *, int));
66 static void def_typedef __P((definition *));
67 static void get_declaration __P((declaration *, defkind));
68 static void get_prog_declaration __P((declaration *, defkind, int));
69 static void get_type __P((char **, char **, defkind));
70 static void unsigned_dec __P((char **));
73 * return the next definition you see
75 definition *
76 get_definition()
78 definition *defp;
79 token tok;
81 defp = ALLOC(definition);
82 get_token(&tok);
83 switch (tok.kind) {
84 case TOK_STRUCT:
85 def_struct(defp);
86 break;
87 case TOK_UNION:
88 def_union(defp);
89 break;
90 case TOK_TYPEDEF:
91 def_typedef(defp);
92 break;
93 case TOK_ENUM:
94 def_enum(defp);
95 break;
96 case TOK_PROGRAM:
97 def_program(defp);
98 break;
99 case TOK_CONST:
100 def_const(defp);
101 break;
102 case TOK_EOF:
103 free(defp);
104 return (NULL);
105 default:
106 error("definition keyword expected");
108 scan(TOK_SEMICOLON, &tok);
109 isdefined(defp);
110 return (defp);
113 static void
114 isdefined(defp)
115 definition *defp;
117 STOREVAL(&defined, defp);
120 static void
121 def_struct(defp)
122 definition *defp;
124 token tok;
125 declaration dec;
126 decl_list *decls;
127 decl_list **tailp;
129 defp->def_kind = DEF_STRUCT;
131 scan(TOK_IDENT, &tok);
132 defp->def_name = tok.str;
133 scan(TOK_LBRACE, &tok);
134 tailp = &defp->def.st.decls;
135 do {
136 get_declaration(&dec, DEF_STRUCT);
137 decls = ALLOC(decl_list);
138 decls->decl = dec;
139 *tailp = decls;
140 tailp = &decls->next;
141 scan(TOK_SEMICOLON, &tok);
142 peek(&tok);
143 } while (tok.kind != TOK_RBRACE);
144 get_token(&tok);
145 *tailp = NULL;
148 static void
149 def_program(defp)
150 definition *defp;
152 token tok;
153 declaration dec;
154 decl_list *decls;
155 decl_list **tailp;
156 version_list *vlist;
157 version_list **vtailp;
158 proc_list *plist;
159 proc_list **ptailp;
160 int num_args;
161 bool_t isvoid = FALSE; /* whether first argument is void */
162 defp->def_kind = DEF_PROGRAM;
163 scan(TOK_IDENT, &tok);
164 defp->def_name = tok.str;
165 scan(TOK_LBRACE, &tok);
166 vtailp = &defp->def.pr.versions;
167 tailp = &defp->def.st.decls;
168 scan(TOK_VERSION, &tok);
169 do {
170 scan(TOK_IDENT, &tok);
171 vlist = ALLOC(version_list);
172 vlist->vers_name = tok.str;
173 scan(TOK_LBRACE, &tok);
174 ptailp = &vlist->procs;
175 do {
176 /* get result type */
177 plist = ALLOC(proc_list);
178 get_type(&plist->res_prefix, &plist->res_type,
179 DEF_PROGRAM);
180 if (streq(plist->res_type, "opaque")) {
181 error("illegal result type");
183 scan(TOK_IDENT, &tok);
184 plist->proc_name = tok.str;
185 scan(TOK_LPAREN, &tok);
186 /* get args - first one */
187 num_args = 1;
188 isvoid = FALSE;
189 /* type of DEF_PROGRAM in the first
190 * get_prog_declaration and DEF_STURCT in the next
191 * allows void as argument if it is the only argument */
192 get_prog_declaration(&dec, DEF_PROGRAM, num_args);
193 if (streq(dec.type, "void"))
194 isvoid = TRUE;
195 decls = ALLOC(decl_list);
196 plist->args.decls = decls;
197 decls->decl = dec;
198 tailp = &decls->next;
199 /* get args */
200 while (peekscan(TOK_COMMA, &tok)) {
201 num_args++;
202 get_prog_declaration(&dec, DEF_STRUCT,
203 num_args);
204 decls = ALLOC(decl_list);
205 decls->decl = dec;
206 *tailp = decls;
207 if (streq(dec.type, "void"))
208 isvoid = TRUE;
209 tailp = &decls->next;
211 /* multiple arguments are only allowed in newstyle */
212 if (!newstyle && num_args > 1) {
213 error("only one argument is allowed");
215 if (isvoid && num_args > 1) {
216 error("illegal use of void in program definition");
218 *tailp = NULL;
219 scan(TOK_RPAREN, &tok);
220 scan(TOK_EQUAL, &tok);
221 scan_num(&tok);
222 scan(TOK_SEMICOLON, &tok);
223 plist->proc_num = tok.str;
224 plist->arg_num = num_args;
225 *ptailp = plist;
226 ptailp = &plist->next;
227 peek(&tok);
228 } while (tok.kind != TOK_RBRACE);
229 *ptailp = NULL;
230 *vtailp = vlist;
231 vtailp = &vlist->next;
232 scan(TOK_RBRACE, &tok);
233 scan(TOK_EQUAL, &tok);
234 scan_num(&tok);
235 vlist->vers_num = tok.str;
236 /* make the argument structure name for each arg */
237 for (plist = vlist->procs; plist != NULL;
238 plist = plist->next) {
239 plist->args.argname = make_argname(plist->proc_name,
240 vlist->vers_num);
241 /* free the memory ?? */
243 scan(TOK_SEMICOLON, &tok);
244 scan2(TOK_VERSION, TOK_RBRACE, &tok);
245 } while (tok.kind == TOK_VERSION);
246 scan(TOK_EQUAL, &tok);
247 scan_num(&tok);
248 defp->def.pr.prog_num = tok.str;
249 *vtailp = NULL;
253 static void
254 def_enum(defp)
255 definition *defp;
257 token tok;
258 enumval_list *elist;
259 enumval_list **tailp;
261 defp->def_kind = DEF_ENUM;
262 scan(TOK_IDENT, &tok);
263 defp->def_name = tok.str;
264 scan(TOK_LBRACE, &tok);
265 tailp = &defp->def.en.vals;
266 do {
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) {
273 scan_num(&tok);
274 elist->assignment = tok.str;
275 scan2(TOK_COMMA, TOK_RBRACE, &tok);
277 *tailp = elist;
278 tailp = &elist->next;
279 } while (tok.kind != TOK_RBRACE);
280 *tailp = NULL;
283 static void
284 def_const(defp)
285 definition *defp;
287 token tok;
289 defp->def_kind = DEF_CONST;
290 scan(TOK_IDENT, &tok);
291 defp->def_name = tok.str;
292 scan(TOK_EQUAL, &tok);
293 scan2(TOK_IDENT, TOK_STRCONST, &tok);
294 defp->def.co = tok.str;
297 static void
298 def_union(defp)
299 definition *defp;
301 token tok;
302 declaration dec;
303 case_list *cases;
304 case_list **tailp;
306 defp->def_kind = DEF_UNION;
307 scan(TOK_IDENT, &tok);
308 defp->def_name = tok.str;
309 scan(TOK_SWITCH, &tok);
310 scan(TOK_LPAREN, &tok);
311 get_declaration(&dec, DEF_UNION);
312 defp->def.un.enum_decl = dec;
313 tailp = &defp->def.un.cases;
314 scan(TOK_RPAREN, &tok);
315 scan(TOK_LBRACE, &tok);
316 scan(TOK_CASE, &tok);
317 while (tok.kind == TOK_CASE) {
318 scan2(TOK_IDENT, TOK_CHARCONST, &tok);
319 cases = ALLOC(case_list);
320 cases->case_name = tok.str;
321 scan(TOK_COLON, &tok);
322 /* now peek at next token */
323 if (peekscan(TOK_CASE, &tok)) {
325 do {
326 scan2(TOK_IDENT, TOK_CHARCONST, &tok);
327 cases->contflag = 1; /* continued case
328 * statement */
329 *tailp = cases;
330 tailp = &cases->next;
331 cases = ALLOC(case_list);
332 cases->case_name = tok.str;
333 scan(TOK_COLON, &tok);
335 } while (peekscan(TOK_CASE, &tok));
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);
344 scan3(TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok);
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;
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
375 static char *reserved_types[] = {
376 "opaque",
377 "string",
378 NULL
380 /* check that the given name is not one that would eventually result in
381 xdr routines that would conflict with internal XDR routines. */
382 static void
383 check_type_name(name, new_type)
384 int new_type;
385 char *name;
387 int i;
388 char tmp[100];
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);
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);
408 static void
409 def_typedef(defp)
410 definition *defp;
412 declaration dec;
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;
424 static void
425 get_declaration(dec, dkind)
426 declaration *dec;
427 defkind dkind;
429 token tok;
431 get_type(&dec->prefix, &dec->type, dkind);
432 dec->rel = REL_ALIAS;
433 if (streq(dec->type, "void")) {
434 return;
436 check_type_name(dec->type, 0);
438 scan2(TOK_STAR, TOK_IDENT, &tok);
439 if (tok.kind == TOK_STAR) {
440 dec->rel = REL_POINTER;
441 scan(TOK_IDENT, &tok);
443 dec->name = tok.str;
444 if (peekscan(TOK_LBRACKET, &tok)) {
445 if (dec->rel == REL_POINTER) {
446 error("no array-of-pointer declarations -- use typedef");
448 dec->rel = REL_VECTOR;
449 scan_num(&tok);
450 dec->array_max = tok.str;
451 scan(TOK_RBRACKET, &tok);
452 } else
453 if (peekscan(TOK_LANGLE, &tok)) {
454 if (dec->rel == REL_POINTER) {
455 error("no array-of-pointer declarations -- use typedef");
457 dec->rel = REL_ARRAY;
458 if (peekscan(TOK_RANGLE, &tok)) {
459 dec->array_max = "(u_int)~0";
460 /* unspecified size, use * max */
461 } else {
462 scan_num(&tok);
463 dec->array_max = tok.str;
464 scan(TOK_RANGLE, &tok);
467 if (streq(dec->type, "opaque")) {
468 if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR) {
469 error("array declaration expected");
471 } else
472 if (streq(dec->type, "string")) {
473 if (dec->rel != REL_ARRAY) {
474 error("variable-length array declaration expected");
479 static void
480 get_prog_declaration(dec, dkind, num)
481 declaration *dec;
482 defkind dkind;
483 int num; /* arg number */
485 token tok;
486 char name[255]; /* argument name */
488 if (dkind == DEF_PROGRAM) {
489 peek(&tok);
490 if (tok.kind == TOK_RPAREN) { /* no arguments */
491 dec->rel = REL_ALIAS;
492 dec->type = "void";
493 dec->prefix = NULL;
494 dec->name = NULL;
495 return;
498 get_type(&dec->prefix, &dec->type, dkind);
499 dec->rel = REL_ALIAS;
500 if (peekscan(TOK_IDENT, &tok)) /* optional name of argument */
501 strcpy(name, tok.str);
502 else
503 sprintf(name, "%s%d", ARGNAME, num); /* default name of
504 * argument */
506 dec->name = (char *) strdup(name);
508 if (streq(dec->type, "void")) {
509 return;
511 if (streq(dec->type, "opaque")) {
512 error("opaque -- illegal argument type");
514 if (peekscan(TOK_STAR, &tok)) {
515 if (streq(dec->type, "string")) {
516 error("pointer to string not allowed in program arguments\n");
518 dec->rel = REL_POINTER;
519 if (peekscan(TOK_IDENT, &tok)) /* optional name of argument */
520 dec->name = (char *) strdup(tok.str);
522 if (peekscan(TOK_LANGLE, &tok)) {
523 if (!streq(dec->type, "string")) {
524 error("arrays cannot be declared as arguments to procedures -- use typedef");
526 dec->rel = REL_ARRAY;
527 if (peekscan(TOK_RANGLE, &tok)) {
528 dec->array_max = "(u_int)~0";
529 /* unspecified size, use max */
530 } else {
531 scan_num(&tok);
532 dec->array_max = tok.str;
533 scan(TOK_RANGLE, &tok);
536 if (streq(dec->type, "string")) {
537 if (dec->rel != REL_ARRAY) { /* .x specifies just string as
538 * type of argument - make it
539 * string<> */
540 dec->rel = REL_ARRAY;
541 dec->array_max = "(u_int)~0";
542 /* unspecified size, use max */
549 static void
550 get_type(prefixp, typep, dkind)
551 char **prefixp;
552 char **typep;
553 defkind dkind;
555 token tok;
557 *prefixp = NULL;
558 get_token(&tok);
559 switch (tok.kind) {
560 case TOK_IDENT:
561 *typep = tok.str;
562 break;
563 case TOK_STRUCT:
564 case TOK_ENUM:
565 case TOK_UNION:
566 *prefixp = tok.str;
567 scan(TOK_IDENT, &tok);
568 *typep = tok.str;
569 break;
570 case TOK_UNSIGNED:
571 unsigned_dec(typep);
572 break;
573 case TOK_SHORT:
574 *typep = "short";
575 (void) peekscan(TOK_INT, &tok);
576 break;
577 case TOK_LONG:
578 *typep = "long";
579 (void) peekscan(TOK_INT, &tok);
580 break;
581 case TOK_HYPER:
582 *typep = "longlong_t";
583 (void) peekscan(TOK_INT, &tok);
584 break;
585 case TOK_VOID:
586 if (dkind != DEF_UNION && dkind != DEF_PROGRAM) {
587 error("voids allowed only inside union and program definitions with one argument");
589 *typep = tok.str;
590 break;
591 case TOK_STRING:
592 case TOK_OPAQUE:
593 case TOK_CHAR:
594 case TOK_INT:
595 case TOK_FLOAT:
596 case TOK_DOUBLE:
597 case TOK_BOOL:
598 case TOK_QUAD:
599 *typep = tok.str;
600 break;
601 default:
602 error("expected type specifier");
606 static void
607 unsigned_dec(typep)
608 char **typep;
610 token tok;
612 peek(&tok);
613 switch (tok.kind) {
614 case TOK_CHAR:
615 get_token(&tok);
616 *typep = "u_char";
617 break;
618 case TOK_SHORT:
619 get_token(&tok);
620 *typep = "u_short";
621 (void) peekscan(TOK_INT, &tok);
622 break;
623 case TOK_LONG:
624 get_token(&tok);
625 *typep = "u_long";
626 (void) peekscan(TOK_INT, &tok);
627 break;
628 case TOK_HYPER:
629 get_token(&tok);
630 *typep = "u_longlong_t";
631 (void) peekscan(TOK_INT, &tok);
632 break;
633 case TOK_INT:
634 get_token(&tok);
635 *typep = "u_int";
636 break;
637 default:
638 *typep = "u_int";
639 break;