* sysdeps/unix/sysv/linux/aio_misc.h: Various cleanups.
[glibc.git] / sunrpc / rpc_parse.c
blob2a29878d6aefb8dc25eb34c2670abe6166ceccd4
1 /*
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.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
32 * From: @(#)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.
39 #include <stdio.h>
40 #include <string.h>
41 #include "rpc/types.h"
42 #include "rpc_scan.h"
43 #include "rpc_parse.h"
44 #include "rpc_util.h"
45 #include "proto.h"
47 #define ARGNAME "arg"
49 static void isdefined (definition * defp);
50 static void def_struct (definition * defp);
51 static void def_program (definition * defp);
52 static void def_enum (definition * defp);
53 static void def_const (definition * defp);
54 static void def_union (definition * defp);
55 static void check_type_name (const char *name, int new_type);
56 static void def_typedef (definition * defp);
57 static void get_declaration (declaration * dec, defkind dkind);
58 static void get_prog_declaration (declaration * dec, defkind dkind, int num);
59 static void get_type (const char **prefixp, const char **typep, defkind dkind);
60 static void unsigned_dec (const char **typep);
63 * return the next definition you see
65 definition *
66 get_definition (void)
68 definition *defp;
69 token tok;
71 defp = ALLOC (definition);
72 get_token (&tok);
73 switch (tok.kind)
75 case TOK_STRUCT:
76 def_struct (defp);
77 break;
78 case TOK_UNION:
79 def_union (defp);
80 break;
81 case TOK_TYPEDEF:
82 def_typedef (defp);
83 break;
84 case TOK_ENUM:
85 def_enum (defp);
86 break;
87 case TOK_PROGRAM:
88 def_program (defp);
89 break;
90 case TOK_CONST:
91 def_const (defp);
92 break;
93 case TOK_EOF:
94 return (NULL);
95 default:
96 error ("definition keyword expected");
98 scan (TOK_SEMICOLON, &tok);
99 isdefined (defp);
100 return (defp);
103 static void
104 isdefined (definition * defp)
106 STOREVAL (&defined, defp);
109 static void
110 def_struct (definition * defp)
112 token tok;
113 declaration dec;
114 decl_list *decls;
115 decl_list **tailp;
117 defp->def_kind = DEF_STRUCT;
119 scan (TOK_IDENT, &tok);
120 defp->def_name = tok.str;
121 scan (TOK_LBRACE, &tok);
122 tailp = &defp->def.st.decls;
125 get_declaration (&dec, DEF_STRUCT);
126 decls = ALLOC (decl_list);
127 decls->decl = dec;
128 *tailp = decls;
129 tailp = &decls->next;
130 scan (TOK_SEMICOLON, &tok);
131 peek (&tok);
133 while (tok.kind != TOK_RBRACE);
134 get_token (&tok);
135 *tailp = NULL;
138 static void
139 def_program (definition * defp)
141 token tok;
142 declaration dec;
143 decl_list *decls;
144 decl_list **tailp;
145 version_list *vlist;
146 version_list **vtailp;
147 proc_list *plist;
148 proc_list **ptailp;
149 int num_args;
150 bool_t isvoid = FALSE; /* whether first argument is void */
151 defp->def_kind = DEF_PROGRAM;
152 scan (TOK_IDENT, &tok);
153 defp->def_name = tok.str;
154 scan (TOK_LBRACE, &tok);
155 vtailp = &defp->def.pr.versions;
156 tailp = &defp->def.st.decls;
157 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;
167 /* get result type */
168 plist = ALLOC (proc_list);
169 get_type (&plist->res_prefix, &plist->res_type,
170 DEF_PROGRAM);
171 if (streq (plist->res_type, "opaque"))
173 error ("illegal result type");
175 scan (TOK_IDENT, &tok);
176 plist->proc_name = tok.str;
177 scan (TOK_LPAREN, &tok);
178 /* get args - first one */
179 num_args = 1;
180 isvoid = FALSE;
181 /* type of DEF_PROGRAM in the first
182 * get_prog_declaration and DEF_STURCT in the next
183 * allows void as argument if it is the only argument
185 get_prog_declaration (&dec, DEF_PROGRAM, num_args);
186 if (streq (dec.type, "void"))
187 isvoid = TRUE;
188 decls = ALLOC (decl_list);
189 plist->args.decls = decls;
190 decls->decl = dec;
191 tailp = &decls->next;
192 /* get args */
193 while (peekscan (TOK_COMMA, &tok))
195 num_args++;
196 get_prog_declaration (&dec, DEF_STRUCT,
197 num_args);
198 decls = ALLOC (decl_list);
199 decls->decl = dec;
200 *tailp = decls;
201 if (streq (dec.type, "void"))
202 isvoid = TRUE;
203 tailp = &decls->next;
205 /* multiple arguments are only allowed in newstyle */
206 if (!newstyle && num_args > 1)
208 error ("only one argument is allowed");
210 if (isvoid && num_args > 1)
212 error ("illegal use of void in program definition");
214 *tailp = NULL;
215 scan (TOK_RPAREN, &tok);
216 scan (TOK_EQUAL, &tok);
217 scan_num (&tok);
218 scan (TOK_SEMICOLON, &tok);
219 plist->proc_num = tok.str;
220 plist->arg_num = num_args;
221 *ptailp = plist;
222 ptailp = &plist->next;
223 peek (&tok);
225 while (tok.kind != TOK_RBRACE);
226 *ptailp = NULL;
227 *vtailp = vlist;
228 vtailp = &vlist->next;
229 scan (TOK_RBRACE, &tok);
230 scan (TOK_EQUAL, &tok);
231 scan_num (&tok);
232 vlist->vers_num = tok.str;
233 /* make the argument structure name for each arg */
234 for (plist = vlist->procs; plist != NULL;
235 plist = plist->next)
237 plist->args.argname = make_argname (plist->proc_name,
238 vlist->vers_num);
239 /* free the memory ?? */
241 scan (TOK_SEMICOLON, &tok);
242 scan2 (TOK_VERSION, TOK_RBRACE, &tok);
244 while (tok.kind == TOK_VERSION);
245 scan (TOK_EQUAL, &tok);
246 scan_num (&tok);
247 defp->def.pr.prog_num = tok.str;
248 *vtailp = NULL;
252 static void
253 def_enum (definition * defp)
255 token tok;
256 enumval_list *elist;
257 enumval_list **tailp;
259 defp->def_kind = DEF_ENUM;
260 scan (TOK_IDENT, &tok);
261 defp->def_name = tok.str;
262 scan (TOK_LBRACE, &tok);
263 tailp = &defp->def.en.vals;
266 scan (TOK_IDENT, &tok);
267 elist = ALLOC (enumval_list);
268 elist->name = tok.str;
269 elist->assignment = NULL;
270 scan3 (TOK_COMMA, TOK_RBRACE, TOK_EQUAL, &tok);
271 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;
280 while (tok.kind != TOK_RBRACE);
281 *tailp = NULL;
284 static void
285 def_const (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 (definition *defp)
300 token tok;
301 declaration dec;
302 case_list *cases;
303 /* case_list *tcase; */
304 case_list **tailp;
305 int flag;
307 defp->def_kind = DEF_UNION;
308 scan (TOK_IDENT, &tok);
309 defp->def_name = tok.str;
310 scan (TOK_SWITCH, &tok);
311 scan (TOK_LPAREN, &tok);
312 get_declaration (&dec, DEF_UNION);
313 defp->def.un.enum_decl = dec;
314 tailp = &defp->def.un.cases;
315 scan (TOK_RPAREN, &tok);
316 scan (TOK_LBRACE, &tok);
317 scan (TOK_CASE, &tok);
318 while (tok.kind == TOK_CASE)
320 scan2 (TOK_IDENT, TOK_CHARCONST, &tok);
321 cases = ALLOC (case_list);
322 cases->case_name = tok.str;
323 scan (TOK_COLON, &tok);
324 /* now peek at next token */
325 flag = 0;
326 if (peekscan (TOK_CASE, &tok))
331 scan2 (TOK_IDENT, TOK_CHARCONST, &tok);
332 cases->contflag = 1; /* continued case statement */
333 *tailp = cases;
334 tailp = &cases->next;
335 cases = ALLOC (case_list);
336 cases->case_name = tok.str;
337 scan (TOK_COLON, &tok);
340 while (peekscan (TOK_CASE, &tok));
342 else if (flag)
345 *tailp = cases;
346 tailp = &cases->next;
347 cases = ALLOC (case_list);
350 get_declaration (&dec, DEF_UNION);
351 cases->case_decl = dec;
352 cases->contflag = 0; /* no continued case statement */
353 *tailp = cases;
354 tailp = &cases->next;
355 scan (TOK_SEMICOLON, &tok);
357 scan3 (TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok);
359 *tailp = NULL;
360 if (tok.kind == TOK_DEFAULT)
362 scan (TOK_COLON, &tok);
363 get_declaration (&dec, DEF_UNION);
364 defp->def.un.default_decl = ALLOC (declaration);
365 *defp->def.un.default_decl = dec;
366 scan (TOK_SEMICOLON, &tok);
367 scan (TOK_RBRACE, &tok);
369 else
371 defp->def.un.default_decl = NULL;
375 static const char *reserved_words[] =
377 "array",
378 "bytes",
379 "destroy",
380 "free",
381 "getpos",
382 "inline",
383 "pointer",
384 "reference",
385 "setpos",
386 "sizeof",
387 "union",
388 "vector",
389 NULL
392 static const char *reserved_types[] =
394 "opaque",
395 "string",
396 NULL
400 * check that the given name is not one that would eventually result in
401 * xdr routines that would conflict with internal XDR routines.
403 static void
404 check_type_name (const char *name, int new_type)
406 int i;
407 char tmp[100];
409 for (i = 0; reserved_words[i] != NULL; i++)
411 if (strcmp (name, reserved_words[i]) == 0)
413 sprintf (tmp,
414 "illegal (reserved) name :\'%s\' in type definition", name);
415 error (tmp);
418 if (new_type)
420 for (i = 0; reserved_types[i] != NULL; i++)
422 if (strcmp (name, reserved_types[i]) == 0)
424 sprintf (tmp,
425 "illegal (reserved) name :\'%s\' in type definition", name);
426 error (tmp);
434 static void
435 def_typedef (definition * defp)
437 declaration dec;
439 defp->def_kind = DEF_TYPEDEF;
440 get_declaration (&dec, DEF_TYPEDEF);
441 defp->def_name = dec.name;
442 check_type_name (dec.name, 1);
443 defp->def.ty.old_prefix = dec.prefix;
444 defp->def.ty.old_type = dec.type;
445 defp->def.ty.rel = dec.rel;
446 defp->def.ty.array_max = dec.array_max;
449 static void
450 get_declaration (declaration * dec, defkind dkind)
452 token tok;
454 get_type (&dec->prefix, &dec->type, dkind);
455 dec->rel = REL_ALIAS;
456 if (streq (dec->type, "void"))
458 return;
461 check_type_name (dec->type, 0);
463 scan2 (TOK_STAR, TOK_IDENT, &tok);
464 if (tok.kind == TOK_STAR)
466 dec->rel = REL_POINTER;
467 scan (TOK_IDENT, &tok);
469 dec->name = tok.str;
470 if (peekscan (TOK_LBRACKET, &tok))
472 if (dec->rel == REL_POINTER)
474 error ("no array-of-pointer declarations -- use typedef");
476 dec->rel = REL_VECTOR;
477 scan_num (&tok);
478 dec->array_max = tok.str;
479 scan (TOK_RBRACKET, &tok);
481 else if (peekscan (TOK_LANGLE, &tok))
483 if (dec->rel == REL_POINTER)
485 error ("no array-of-pointer declarations -- use typedef");
487 dec->rel = REL_ARRAY;
488 if (peekscan (TOK_RANGLE, &tok))
490 dec->array_max = "~0"; /* unspecified size, use max */
492 else
494 scan_num (&tok);
495 dec->array_max = tok.str;
496 scan (TOK_RANGLE, &tok);
499 if (streq (dec->type, "opaque"))
501 if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR)
503 error ("array declaration expected");
506 else if (streq (dec->type, "string"))
508 if (dec->rel != REL_ARRAY)
510 error ("variable-length array declaration expected");
515 static void
516 get_prog_declaration (declaration * dec, defkind dkind, int num /* arg number */ )
518 token tok;
519 char name[10]; /* argument name */
521 if (dkind == DEF_PROGRAM)
523 peek (&tok);
524 if (tok.kind == TOK_RPAREN)
525 { /* no arguments */
526 dec->rel = REL_ALIAS;
527 dec->type = "void";
528 dec->prefix = NULL;
529 dec->name = NULL;
530 return;
533 get_type (&dec->prefix, &dec->type, dkind);
534 dec->rel = REL_ALIAS;
535 if (peekscan (TOK_IDENT, &tok)) /* optional name of argument */
536 strcpy (name, tok.str);
537 else
538 sprintf (name, "%s%d", ARGNAME, num); /* default name of argument */
540 dec->name = (char *) strdup (name);
542 if (streq (dec->type, "void"))
544 return;
547 if (streq (dec->type, "opaque"))
549 error ("opaque -- illegal argument type");
551 if (peekscan (TOK_STAR, &tok))
553 if (streq (dec->type, "string"))
555 error ("pointer to string not allowed in program arguments\n");
557 dec->rel = REL_POINTER;
558 if (peekscan (TOK_IDENT, &tok)) /* optional name of argument */
559 dec->name = strdup (tok.str);
561 if (peekscan (TOK_LANGLE, &tok))
563 if (!streq (dec->type, "string"))
565 error ("arrays cannot be declared as arguments to procedures -- use typedef");
567 dec->rel = REL_ARRAY;
568 if (peekscan (TOK_RANGLE, &tok))
570 dec->array_max = "~0"; /* unspecified size, use max */
572 else
574 scan_num (&tok);
575 dec->array_max = tok.str;
576 scan (TOK_RANGLE, &tok);
579 if (streq (dec->type, "string"))
581 if (dec->rel != REL_ARRAY)
582 { /* .x specifies just string as
583 * type of argument
584 * - make it string<>
586 dec->rel = REL_ARRAY;
587 dec->array_max = "~0"; /* unspecified size, use max */
592 static void
593 get_type (const char **prefixp, const char **typep, defkind dkind)
595 token tok;
597 *prefixp = NULL;
598 get_token (&tok);
599 switch (tok.kind)
601 case TOK_IDENT:
602 *typep = tok.str;
603 break;
604 case TOK_STRUCT:
605 case TOK_ENUM:
606 case TOK_UNION:
607 *prefixp = tok.str;
608 scan (TOK_IDENT, &tok);
609 *typep = tok.str;
610 break;
611 case TOK_UNSIGNED:
612 unsigned_dec (typep);
613 break;
614 case TOK_SHORT:
615 *typep = "short";
616 (void) peekscan (TOK_INT, &tok);
617 break;
618 case TOK_LONG:
619 *typep = "long";
620 (void) peekscan (TOK_INT, &tok);
621 break;
622 case TOK_HYPER:
623 *typep = "quad_t";
624 (void) peekscan(TOK_INT, &tok);
625 break;
626 case TOK_VOID:
627 if (dkind != DEF_UNION && dkind != DEF_PROGRAM)
629 error ("voids allowed only inside union and program definitions with one argument");
631 *typep = tok.str;
632 break;
633 case TOK_STRING:
634 case TOK_OPAQUE:
635 case TOK_CHAR:
636 case TOK_INT:
637 case TOK_FLOAT:
638 case TOK_DOUBLE:
639 case TOK_BOOL:
640 *typep = tok.str;
641 break;
642 default:
643 error ("expected type specifier");
647 static void
648 unsigned_dec (const char **typep)
650 token tok;
652 peek (&tok);
653 switch (tok.kind)
655 case TOK_CHAR:
656 get_token (&tok);
657 *typep = "u_char";
658 break;
659 case TOK_SHORT:
660 get_token (&tok);
661 *typep = "u_short";
662 (void) peekscan (TOK_INT, &tok);
663 break;
664 case TOK_LONG:
665 get_token (&tok);
666 *typep = "u_long";
667 (void) peekscan (TOK_INT, &tok);
668 break;
669 case TOK_HYPER:
670 get_token (&tok);
671 *typep = "u_quad_t";
672 (void) peekscan(TOK_INT, &tok);
673 break;
674 case TOK_INT:
675 get_token (&tok);
676 *typep = "u_int";
677 break;
678 default:
679 *typep = "u_int";
680 break;