* libio/fmemopen.c (fmemopen): Free stream memory in case of
[platform/upstream/glibc.git] / sunrpc / rpc_parse.c
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.
9  *
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.
13  *
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.
17  *
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.
21  *
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.
25  *
26  * Sun Microsystems, Inc.
27  * 2550 Garcia Avenue
28  * Mountain View, California  94043
29  */
30
31 /*
32  * From: @(#)rpc_parse.c 1.8 89/02/22 (C) 1987 SMI
33  */
34
35 /*
36  * rpc_parse.c, Parser for the RPC protocol compiler
37  * Copyright (C) 1987 Sun Microsystems, Inc.
38  */
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"
46
47 #define ARGNAME "arg"
48
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);
61
62 /*
63  * return the next definition you see
64  */
65 definition *
66 get_definition (void)
67 {
68   definition *defp;
69   token tok;
70
71   defp = ALLOC (definition);
72   get_token (&tok);
73   switch (tok.kind)
74     {
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");
98     }
99   scan (TOK_SEMICOLON, &tok);
100   isdefined (defp);
101   return (defp);
102 }
103
104 static void
105 isdefined (definition * defp)
106 {
107   STOREVAL (&defined, defp);
108 }
109
110 static void
111 def_struct (definition * defp)
112 {
113   token tok;
114   declaration dec;
115   decl_list *decls;
116   decl_list **tailp;
117
118   defp->def_kind = DEF_STRUCT;
119
120   scan (TOK_IDENT, &tok);
121   defp->def_name = tok.str;
122   scan (TOK_LBRACE, &tok);
123   tailp = &defp->def.st.decls;
124   do
125     {
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);
133     }
134   while (tok.kind != TOK_RBRACE);
135   get_token (&tok);
136   *tailp = NULL;
137 }
138
139 static void
140 def_program (definition * defp)
141 {
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);
159   do
160     {
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;
166       do
167         {
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"))
173             {
174               error ("illegal result type");
175             }
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
185            */
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))
195             {
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             {
209               error ("only one argument is allowed");
210             }
211           if (isvoid && num_args > 1)
212             {
213               error ("illegal use of void in program definition");
214             }
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);
225         }
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)
237         {
238           plist->args.argname = make_argname (plist->proc_name,
239                                               vlist->vers_num);
240           /* free the memory ?? */
241         }
242       scan (TOK_SEMICOLON, &tok);
243       scan2 (TOK_VERSION, TOK_RBRACE, &tok);
244     }
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;
250 }
251
252
253 static void
254 def_enum (definition * defp)
255 {
256   token tok;
257   enumval_list *elist;
258   enumval_list **tailp;
259
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;
265   do
266     {
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         {
274           scan_num (&tok);
275           elist->assignment = tok.str;
276           scan2 (TOK_COMMA, TOK_RBRACE, &tok);
277         }
278       *tailp = elist;
279       tailp = &elist->next;
280     }
281   while (tok.kind != TOK_RBRACE);
282   *tailp = NULL;
283 }
284
285 static void
286 def_const (definition * defp)
287 {
288   token tok;
289
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;
296 }
297
298 static void
299 def_union (definition *defp)
300 {
301   token tok;
302   declaration dec;
303   case_list *cases;
304 /*  case_list *tcase; */
305   case_list **tailp;
306   int flag;
307
308   defp->def_kind = DEF_UNION;
309   scan (TOK_IDENT, &tok);
310   defp->def_name = tok.str;
311   scan (TOK_SWITCH, &tok);
312   scan (TOK_LPAREN, &tok);
313   get_declaration (&dec, DEF_UNION);
314   defp->def.un.enum_decl = dec;
315   tailp = &defp->def.un.cases;
316   scan (TOK_RPAREN, &tok);
317   scan (TOK_LBRACE, &tok);
318   scan (TOK_CASE, &tok);
319   while (tok.kind == TOK_CASE)
320     {
321       scan2 (TOK_IDENT, TOK_CHARCONST, &tok);
322       cases = ALLOC (case_list);
323       cases->case_name = tok.str;
324       scan (TOK_COLON, &tok);
325       /* now peek at next token */
326       flag = 0;
327       if (peekscan (TOK_CASE, &tok))
328         {
329
330           do
331             {
332               scan2 (TOK_IDENT, TOK_CHARCONST, &tok);
333               cases->contflag = 1;      /* continued case statement */
334               *tailp = cases;
335               tailp = &cases->next;
336               cases = ALLOC (case_list);
337               cases->case_name = tok.str;
338               scan (TOK_COLON, &tok);
339
340             }
341           while (peekscan (TOK_CASE, &tok));
342         }
343       else if (flag)
344         {
345
346           *tailp = cases;
347           tailp = &cases->next;
348           cases = ALLOC (case_list);
349         };
350
351       get_declaration (&dec, DEF_UNION);
352       cases->case_decl = dec;
353       cases->contflag = 0;      /* no continued case statement */
354       *tailp = cases;
355       tailp = &cases->next;
356       scan (TOK_SEMICOLON, &tok);
357
358       scan3 (TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok);
359     }
360   *tailp = NULL;
361   if (tok.kind == TOK_DEFAULT)
362     {
363       scan (TOK_COLON, &tok);
364       get_declaration (&dec, DEF_UNION);
365       defp->def.un.default_decl = ALLOC (declaration);
366       *defp->def.un.default_decl = dec;
367       scan (TOK_SEMICOLON, &tok);
368       scan (TOK_RBRACE, &tok);
369     }
370   else
371     {
372       defp->def.un.default_decl = NULL;
373     }
374 }
375
376 static const char *reserved_words[] =
377 {
378   "array",
379   "bytes",
380   "destroy",
381   "free",
382   "getpos",
383   "inline",
384   "pointer",
385   "reference",
386   "setpos",
387   "sizeof",
388   "union",
389   "vector",
390   NULL
391 };
392
393 static const char *reserved_types[] =
394 {
395   "opaque",
396   "string",
397   NULL
398 };
399
400 /*
401  * check that the given name is not one that would eventually result in
402  * xdr routines that would conflict with internal XDR routines.
403  */
404 static void
405 check_type_name (const char *name, int new_type)
406 {
407   int i;
408   char tmp[100];
409
410   for (i = 0; reserved_words[i] != NULL; i++)
411     {
412       if (strcmp (name, reserved_words[i]) == 0)
413         {
414           sprintf (tmp,
415                 "illegal (reserved) name :\'%s\' in type definition", name);
416           error (tmp);
417         }
418     }
419   if (new_type)
420     {
421       for (i = 0; reserved_types[i] != NULL; i++)
422         {
423           if (strcmp (name, reserved_types[i]) == 0)
424             {
425               sprintf (tmp,
426                 "illegal (reserved) name :\'%s\' in type definition", name);
427               error (tmp);
428             }
429         }
430     }
431 }
432
433
434
435 static void
436 def_typedef (definition * defp)
437 {
438   declaration dec;
439
440   defp->def_kind = DEF_TYPEDEF;
441   get_declaration (&dec, DEF_TYPEDEF);
442   defp->def_name = dec.name;
443   check_type_name (dec.name, 1);
444   defp->def.ty.old_prefix = dec.prefix;
445   defp->def.ty.old_type = dec.type;
446   defp->def.ty.rel = dec.rel;
447   defp->def.ty.array_max = dec.array_max;
448 }
449
450 static void
451 get_declaration (declaration * dec, defkind dkind)
452 {
453   token tok;
454
455   get_type (&dec->prefix, &dec->type, dkind);
456   dec->rel = REL_ALIAS;
457   if (streq (dec->type, "void"))
458     {
459       return;
460     }
461
462   check_type_name (dec->type, 0);
463
464   scan2 (TOK_STAR, TOK_IDENT, &tok);
465   if (tok.kind == TOK_STAR)
466     {
467       dec->rel = REL_POINTER;
468       scan (TOK_IDENT, &tok);
469     }
470   dec->name = tok.str;
471   if (peekscan (TOK_LBRACKET, &tok))
472     {
473       if (dec->rel == REL_POINTER)
474         {
475           error ("no array-of-pointer declarations -- use typedef");
476         }
477       dec->rel = REL_VECTOR;
478       scan_num (&tok);
479       dec->array_max = tok.str;
480       scan (TOK_RBRACKET, &tok);
481     }
482   else if (peekscan (TOK_LANGLE, &tok))
483     {
484       if (dec->rel == REL_POINTER)
485         {
486           error ("no array-of-pointer declarations -- use typedef");
487         }
488       dec->rel = REL_ARRAY;
489       if (peekscan (TOK_RANGLE, &tok))
490         {
491           dec->array_max = "~0";        /* unspecified size, use max */
492         }
493       else
494         {
495           scan_num (&tok);
496           dec->array_max = tok.str;
497           scan (TOK_RANGLE, &tok);
498         }
499     }
500   if (streq (dec->type, "opaque"))
501     {
502       if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR)
503         {
504           error ("array declaration expected");
505         }
506     }
507   else if (streq (dec->type, "string"))
508     {
509       if (dec->rel != REL_ARRAY)
510         {
511           error ("variable-length array declaration expected");
512         }
513     }
514 }
515
516 static void
517 get_prog_declaration (declaration * dec, defkind dkind, int num /* arg number */ )
518 {
519   token tok;
520   char name[10];                /* argument name */
521
522   if (dkind == DEF_PROGRAM)
523     {
524       peek (&tok);
525       if (tok.kind == TOK_RPAREN)
526         {                       /* no arguments */
527           dec->rel = REL_ALIAS;
528           dec->type = "void";
529           dec->prefix = NULL;
530           dec->name = NULL;
531           return;
532         }
533     }
534   get_type (&dec->prefix, &dec->type, dkind);
535   dec->rel = REL_ALIAS;
536   if (peekscan (TOK_IDENT, &tok))       /* optional name of argument */
537     strcpy (name, tok.str);
538   else
539     sprintf (name, "%s%d", ARGNAME, num);       /* default name of argument */
540
541   dec->name = (char *) strdup (name);
542
543   if (streq (dec->type, "void"))
544     {
545       return;
546     }
547
548   if (streq (dec->type, "opaque"))
549     {
550       error ("opaque -- illegal argument type");
551     }
552   if (peekscan (TOK_STAR, &tok))
553     {
554       if (streq (dec->type, "string"))
555         {
556           error ("pointer to string not allowed in program arguments\n");
557         }
558       dec->rel = REL_POINTER;
559       if (peekscan (TOK_IDENT, &tok))   /* optional name of argument */
560         dec->name = strdup (tok.str);
561     }
562   if (peekscan (TOK_LANGLE, &tok))
563     {
564       if (!streq (dec->type, "string"))
565         {
566           error ("arrays cannot be declared as arguments to procedures -- use typedef");
567         }
568       dec->rel = REL_ARRAY;
569       if (peekscan (TOK_RANGLE, &tok))
570         {
571           dec->array_max = "~0";        /* unspecified size, use max */
572         }
573       else
574         {
575           scan_num (&tok);
576           dec->array_max = tok.str;
577           scan (TOK_RANGLE, &tok);
578         }
579     }
580   if (streq (dec->type, "string"))
581     {
582       if (dec->rel != REL_ARRAY)
583         {                       /* .x specifies just string as
584                                  * type of argument
585                                  * - make it string<>
586                                  */
587           dec->rel = REL_ARRAY;
588           dec->array_max = "~0";        /* unspecified size, use max */
589         }
590     }
591 }
592
593 static void
594 get_type (const char **prefixp, const char **typep, defkind dkind)
595 {
596   token tok;
597
598   *prefixp = NULL;
599   get_token (&tok);
600   switch (tok.kind)
601     {
602     case TOK_IDENT:
603       *typep = tok.str;
604       break;
605     case TOK_STRUCT:
606     case TOK_ENUM:
607     case TOK_UNION:
608       *prefixp = tok.str;
609       scan (TOK_IDENT, &tok);
610       *typep = tok.str;
611       break;
612     case TOK_UNSIGNED:
613       unsigned_dec (typep);
614       break;
615     case TOK_SHORT:
616       *typep = "short";
617       (void) peekscan (TOK_INT, &tok);
618       break;
619     case TOK_LONG:
620       *typep = "long";
621       (void) peekscan (TOK_INT, &tok);
622       break;
623     case TOK_HYPER:
624       *typep = "quad_t";
625       (void) peekscan(TOK_INT, &tok);
626       break;
627     case TOK_VOID:
628       if (dkind != DEF_UNION && dkind != DEF_PROGRAM)
629         {
630           error ("voids allowed only inside union and program definitions with one argument");
631         }
632       *typep = tok.str;
633       break;
634     case TOK_STRING:
635     case TOK_OPAQUE:
636     case TOK_CHAR:
637     case TOK_INT:
638     case TOK_FLOAT:
639     case TOK_DOUBLE:
640     case TOK_BOOL:
641       *typep = tok.str;
642       break;
643     default:
644       error ("expected type specifier");
645     }
646 }
647
648 static void
649 unsigned_dec (const char **typep)
650 {
651   token tok;
652
653   peek (&tok);
654   switch (tok.kind)
655     {
656     case TOK_CHAR:
657       get_token (&tok);
658       *typep = "u_char";
659       break;
660     case TOK_SHORT:
661       get_token (&tok);
662       *typep = "u_short";
663       (void) peekscan (TOK_INT, &tok);
664       break;
665     case TOK_LONG:
666       get_token (&tok);
667       *typep = "u_long";
668       (void) peekscan (TOK_INT, &tok);
669       break;
670     case TOK_HYPER:
671       get_token (&tok);
672       *typep = "u_quad_t";
673       (void) peekscan(TOK_INT, &tok);
674       break;
675     case TOK_INT:
676       get_token (&tok);
677       *typep = "u_int";
678       break;
679     default:
680       *typep = "u_int";
681       break;
682     }
683 }