.
[glibc/history.git] / sunrpc / rpc_parse.c
blobf66538701f0be241062dd2ef2ae0de5728a19c42
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 free (defp);
95 return (NULL);
96 default:
97 error ("definition keyword expected");
99 scan (TOK_SEMICOLON, &tok);
100 isdefined (defp);
101 return (defp);
104 static void
105 isdefined (definition * defp)
107 STOREVAL (&defined, defp);
110 static void
111 def_struct (definition * defp)
113 token tok;
114 declaration dec;
115 decl_list *decls;
116 decl_list **tailp;
118 defp->def_kind = DEF_STRUCT;
120 scan (TOK_IDENT, &tok);
121 defp->def_name = tok.str;
122 scan (TOK_LBRACE, &tok);
123 tailp = &defp->def.st.decls;
126 get_declaration (&dec, DEF_STRUCT);
127 decls = ALLOC (decl_list);
128 decls->decl = dec;
129 *tailp = decls;
130 tailp = &decls->next;
131 scan (TOK_SEMICOLON, &tok);
132 peek (&tok);
134 while (tok.kind != TOK_RBRACE);
135 get_token (&tok);
136 *tailp = NULL;
139 static void
140 def_program (definition * defp)
142 token tok;
143 declaration dec;
144 decl_list *decls;
145 decl_list **tailp;
146 version_list *vlist;
147 version_list **vtailp;
148 proc_list *plist;
149 proc_list **ptailp;
150 int num_args;
151 bool_t isvoid = FALSE; /* whether first argument is void */
152 defp->def_kind = DEF_PROGRAM;
153 scan (TOK_IDENT, &tok);
154 defp->def_name = tok.str;
155 scan (TOK_LBRACE, &tok);
156 vtailp = &defp->def.pr.versions;
157 tailp = &defp->def.st.decls;
158 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;
168 /* get result type */
169 plist = ALLOC (proc_list);
170 get_type (&plist->res_prefix, &plist->res_type,
171 DEF_PROGRAM);
172 if (streq (plist->res_type, "opaque"))
174 error ("illegal result type");
176 scan (TOK_IDENT, &tok);
177 plist->proc_name = tok.str;
178 scan (TOK_LPAREN, &tok);
179 /* get args - first one */
180 num_args = 1;
181 isvoid = FALSE;
182 /* type of DEF_PROGRAM in the first
183 * get_prog_declaration and DEF_STURCT in the next
184 * allows void as argument if it is the only argument
186 get_prog_declaration (&dec, DEF_PROGRAM, num_args);
187 if (streq (dec.type, "void"))
188 isvoid = TRUE;
189 decls = ALLOC (decl_list);
190 plist->args.decls = decls;
191 decls->decl = dec;
192 tailp = &decls->next;
193 /* get args */
194 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;
206 /* multiple arguments are only allowed in newstyle */
207 if (!newstyle && num_args > 1)
209 error ("only one argument is allowed");
211 if (isvoid && num_args > 1)
213 error ("illegal use of void in program definition");
215 *tailp = NULL;
216 scan (TOK_RPAREN, &tok);
217 scan (TOK_EQUAL, &tok);
218 scan_num (&tok);
219 scan (TOK_SEMICOLON, &tok);
220 plist->proc_num = tok.str;
221 plist->arg_num = num_args;
222 *ptailp = plist;
223 ptailp = &plist->next;
224 peek (&tok);
226 while (tok.kind != TOK_RBRACE);
227 *ptailp = NULL;
228 *vtailp = vlist;
229 vtailp = &vlist->next;
230 scan (TOK_RBRACE, &tok);
231 scan (TOK_EQUAL, &tok);
232 scan_num (&tok);
233 vlist->vers_num = tok.str;
234 /* make the argument structure name for each arg */
235 for (plist = vlist->procs; plist != NULL;
236 plist = plist->next)
238 plist->args.argname = make_argname (plist->proc_name,
239 vlist->vers_num);
240 /* free the memory ?? */
242 scan (TOK_SEMICOLON, &tok);
243 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 (definition * defp)
256 token tok;
257 enumval_list *elist;
258 enumval_list **tailp;
260 defp->def_kind = DEF_ENUM;
261 scan (TOK_IDENT, &tok);
262 defp->def_name = tok.str;
263 scan (TOK_LBRACE, &tok);
264 tailp = &defp->def.en.vals;
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)
274 scan_num (&tok);
275 elist->assignment = tok.str;
276 scan2 (TOK_COMMA, TOK_RBRACE, &tok);
278 *tailp = elist;
279 tailp = &elist->next;
281 while (tok.kind != TOK_RBRACE);
282 *tailp = NULL;
285 static void
286 def_const (definition * defp)
288 token tok;
290 defp->def_kind = DEF_CONST;
291 scan (TOK_IDENT, &tok);
292 defp->def_name = tok.str;
293 scan (TOK_EQUAL, &tok);
294 scan2 (TOK_IDENT, TOK_STRCONST, &tok);
295 defp->def.co = tok.str;
298 static void
299 def_union (definition *defp)
301 token tok;
302 declaration dec;
303 case_list *cases;
304 /* case_list *tcase; */
305 case_list **tailp;
306 #if 0
307 int flag;
308 #endif
310 defp->def_kind = DEF_UNION;
311 scan (TOK_IDENT, &tok);
312 defp->def_name = tok.str;
313 scan (TOK_SWITCH, &tok);
314 scan (TOK_LPAREN, &tok);
315 get_declaration (&dec, DEF_UNION);
316 defp->def.un.enum_decl = dec;
317 tailp = &defp->def.un.cases;
318 scan (TOK_RPAREN, &tok);
319 scan (TOK_LBRACE, &tok);
320 scan (TOK_CASE, &tok);
321 while (tok.kind == TOK_CASE)
323 scan2 (TOK_IDENT, TOK_CHARCONST, &tok);
324 cases = ALLOC (case_list);
325 cases->case_name = tok.str;
326 scan (TOK_COLON, &tok);
327 /* now peek at next token */
328 #if 0
329 flag = 0;
330 #endif
331 if (peekscan (TOK_CASE, &tok))
336 scan2 (TOK_IDENT, TOK_CHARCONST, &tok);
337 cases->contflag = 1; /* continued case statement */
338 *tailp = cases;
339 tailp = &cases->next;
340 cases = ALLOC (case_list);
341 cases->case_name = tok.str;
342 scan (TOK_COLON, &tok);
345 while (peekscan (TOK_CASE, &tok));
347 #if 0
348 else if (flag)
351 *tailp = cases;
352 tailp = &cases->next;
353 cases = ALLOC (case_list);
355 #endif
357 get_declaration (&dec, DEF_UNION);
358 cases->case_decl = dec;
359 cases->contflag = 0; /* no continued case statement */
360 *tailp = cases;
361 tailp = &cases->next;
362 scan (TOK_SEMICOLON, &tok);
364 scan3 (TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok);
366 *tailp = NULL;
367 if (tok.kind == TOK_DEFAULT)
369 scan (TOK_COLON, &tok);
370 get_declaration (&dec, DEF_UNION);
371 defp->def.un.default_decl = ALLOC (declaration);
372 *defp->def.un.default_decl = dec;
373 scan (TOK_SEMICOLON, &tok);
374 scan (TOK_RBRACE, &tok);
376 else
378 defp->def.un.default_decl = NULL;
382 static const char *reserved_words[] =
384 "array",
385 "bytes",
386 "destroy",
387 "free",
388 "getpos",
389 "inline",
390 "pointer",
391 "reference",
392 "setpos",
393 "sizeof",
394 "union",
395 "vector",
396 NULL
399 static const char *reserved_types[] =
401 "opaque",
402 "string",
403 NULL
407 * check that the given name is not one that would eventually result in
408 * xdr routines that would conflict with internal XDR routines.
410 static void
411 check_type_name (const char *name, int new_type)
413 int i;
414 char tmp[100];
416 for (i = 0; reserved_words[i] != NULL; i++)
418 if (strcmp (name, reserved_words[i]) == 0)
420 sprintf (tmp,
421 "illegal (reserved) name :\'%s\' in type definition", name);
422 error (tmp);
425 if (new_type)
427 for (i = 0; reserved_types[i] != NULL; i++)
429 if (strcmp (name, reserved_types[i]) == 0)
431 sprintf (tmp,
432 "illegal (reserved) name :\'%s\' in type definition", name);
433 error (tmp);
441 static void
442 def_typedef (definition * defp)
444 declaration dec;
446 defp->def_kind = DEF_TYPEDEF;
447 get_declaration (&dec, DEF_TYPEDEF);
448 defp->def_name = dec.name;
449 check_type_name (dec.name, 1);
450 defp->def.ty.old_prefix = dec.prefix;
451 defp->def.ty.old_type = dec.type;
452 defp->def.ty.rel = dec.rel;
453 defp->def.ty.array_max = dec.array_max;
456 static void
457 get_declaration (declaration * dec, defkind dkind)
459 token tok;
461 get_type (&dec->prefix, &dec->type, dkind);
462 dec->rel = REL_ALIAS;
463 if (streq (dec->type, "void"))
465 return;
468 check_type_name (dec->type, 0);
470 scan2 (TOK_STAR, TOK_IDENT, &tok);
471 if (tok.kind == TOK_STAR)
473 dec->rel = REL_POINTER;
474 scan (TOK_IDENT, &tok);
476 dec->name = tok.str;
477 if (peekscan (TOK_LBRACKET, &tok))
479 if (dec->rel == REL_POINTER)
481 error ("no array-of-pointer declarations -- use typedef");
483 dec->rel = REL_VECTOR;
484 scan_num (&tok);
485 dec->array_max = tok.str;
486 scan (TOK_RBRACKET, &tok);
488 else if (peekscan (TOK_LANGLE, &tok))
490 if (dec->rel == REL_POINTER)
492 error ("no array-of-pointer declarations -- use typedef");
494 dec->rel = REL_ARRAY;
495 if (peekscan (TOK_RANGLE, &tok))
497 dec->array_max = "~0"; /* unspecified size, use max */
499 else
501 scan_num (&tok);
502 dec->array_max = tok.str;
503 scan (TOK_RANGLE, &tok);
506 if (streq (dec->type, "opaque"))
508 if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR)
510 error ("array declaration expected");
513 else if (streq (dec->type, "string"))
515 if (dec->rel != REL_ARRAY)
517 error ("variable-length array declaration expected");
522 static void
523 get_prog_declaration (declaration * dec, defkind dkind, int num /* arg number */ )
525 token tok;
526 char name[10]; /* argument name */
528 if (dkind == DEF_PROGRAM)
530 peek (&tok);
531 if (tok.kind == TOK_RPAREN)
532 { /* no arguments */
533 dec->rel = REL_ALIAS;
534 dec->type = "void";
535 dec->prefix = NULL;
536 dec->name = NULL;
537 return;
540 get_type (&dec->prefix, &dec->type, dkind);
541 dec->rel = REL_ALIAS;
542 if (peekscan (TOK_IDENT, &tok)) /* optional name of argument */
543 strcpy (name, tok.str);
544 else
545 sprintf (name, "%s%d", ARGNAME, num); /* default name of argument */
547 dec->name = (char *) strdup (name);
549 if (streq (dec->type, "void"))
551 return;
554 if (streq (dec->type, "opaque"))
556 error ("opaque -- illegal argument type");
558 if (peekscan (TOK_STAR, &tok))
560 if (streq (dec->type, "string"))
562 error ("pointer to string not allowed in program arguments\n");
564 dec->rel = REL_POINTER;
565 if (peekscan (TOK_IDENT, &tok)) /* optional name of argument */
566 dec->name = strdup (tok.str);
568 if (peekscan (TOK_LANGLE, &tok))
570 if (!streq (dec->type, "string"))
572 error ("arrays cannot be declared as arguments to procedures -- use typedef");
574 dec->rel = REL_ARRAY;
575 if (peekscan (TOK_RANGLE, &tok))
577 dec->array_max = "~0"; /* unspecified size, use max */
579 else
581 scan_num (&tok);
582 dec->array_max = tok.str;
583 scan (TOK_RANGLE, &tok);
586 if (streq (dec->type, "string"))
588 if (dec->rel != REL_ARRAY)
589 { /* .x specifies just string as
590 * type of argument
591 * - make it string<>
593 dec->rel = REL_ARRAY;
594 dec->array_max = "~0"; /* unspecified size, use max */
599 static void
600 get_type (const char **prefixp, const char **typep, defkind dkind)
602 token tok;
604 *prefixp = NULL;
605 get_token (&tok);
606 switch (tok.kind)
608 case TOK_IDENT:
609 *typep = tok.str;
610 break;
611 case TOK_STRUCT:
612 case TOK_ENUM:
613 case TOK_UNION:
614 *prefixp = tok.str;
615 scan (TOK_IDENT, &tok);
616 *typep = tok.str;
617 break;
618 case TOK_UNSIGNED:
619 unsigned_dec (typep);
620 break;
621 case TOK_SHORT:
622 *typep = "short";
623 (void) peekscan (TOK_INT, &tok);
624 break;
625 case TOK_LONG:
626 *typep = "long";
627 (void) peekscan (TOK_INT, &tok);
628 break;
629 case TOK_HYPER:
630 *typep = "quad_t";
631 (void) peekscan(TOK_INT, &tok);
632 break;
633 case TOK_VOID:
634 if (dkind != DEF_UNION && dkind != DEF_PROGRAM)
636 error ("voids allowed only inside union and program definitions with one argument");
638 *typep = tok.str;
639 break;
640 case TOK_STRING:
641 case TOK_OPAQUE:
642 case TOK_CHAR:
643 case TOK_INT:
644 case TOK_FLOAT:
645 case TOK_DOUBLE:
646 case TOK_BOOL:
647 *typep = tok.str;
648 break;
649 default:
650 error ("expected type specifier");
654 static void
655 unsigned_dec (const char **typep)
657 token tok;
659 peek (&tok);
660 switch (tok.kind)
662 case TOK_CHAR:
663 get_token (&tok);
664 *typep = "u_char";
665 break;
666 case TOK_SHORT:
667 get_token (&tok);
668 *typep = "u_short";
669 (void) peekscan (TOK_INT, &tok);
670 break;
671 case TOK_LONG:
672 get_token (&tok);
673 *typep = "u_long";
674 (void) peekscan (TOK_INT, &tok);
675 break;
676 case TOK_HYPER:
677 get_token (&tok);
678 *typep = "u_quad_t";
679 (void) peekscan(TOK_INT, &tok);
680 break;
681 case TOK_INT:
682 get_token (&tok);
683 *typep = "u_int";
684 break;
685 default:
686 *typep = "u_int";
687 break;