remove COPYING* from .gitignore files
[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 #if 0
307   int flag;
308 #endif
309
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)
322     {
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))
332         {
333
334           do
335             {
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);
343
344             }
345           while (peekscan (TOK_CASE, &tok));
346         }
347 #if 0
348       else if (flag)
349         {
350
351           *tailp = cases;
352           tailp = &cases->next;
353           cases = ALLOC (case_list);
354         };
355 #endif
356
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);
363
364       scan3 (TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok);
365     }
366   *tailp = NULL;
367   if (tok.kind == TOK_DEFAULT)
368     {
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);
375     }
376   else
377     {
378       defp->def.un.default_decl = NULL;
379     }
380 }
381
382 static const char *reserved_words[] =
383 {
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
397 };
398
399 static const char *reserved_types[] =
400 {
401   "opaque",
402   "string",
403   NULL
404 };
405
406 /*
407  * check that the given name is not one that would eventually result in
408  * xdr routines that would conflict with internal XDR routines.
409  */
410 static void
411 check_type_name (const char *name, int new_type)
412 {
413   int i;
414   char tmp[100];
415
416   for (i = 0; reserved_words[i] != NULL; i++)
417     {
418       if (strcmp (name, reserved_words[i]) == 0)
419         {
420           sprintf (tmp,
421                 "illegal (reserved) name :\'%s\' in type definition", name);
422           error (tmp);
423         }
424     }
425   if (new_type)
426     {
427       for (i = 0; reserved_types[i] != NULL; i++)
428         {
429           if (strcmp (name, reserved_types[i]) == 0)
430             {
431               sprintf (tmp,
432                 "illegal (reserved) name :\'%s\' in type definition", name);
433               error (tmp);
434             }
435         }
436     }
437 }
438
439
440
441 static void
442 def_typedef (definition * defp)
443 {
444   declaration dec;
445
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;
454 }
455
456 static void
457 get_declaration (declaration * dec, defkind dkind)
458 {
459   token tok;
460
461   get_type (&dec->prefix, &dec->type, dkind);
462   dec->rel = REL_ALIAS;
463   if (streq (dec->type, "void"))
464     {
465       return;
466     }
467
468   check_type_name (dec->type, 0);
469
470   scan2 (TOK_STAR, TOK_IDENT, &tok);
471   if (tok.kind == TOK_STAR)
472     {
473       dec->rel = REL_POINTER;
474       scan (TOK_IDENT, &tok);
475     }
476   dec->name = tok.str;
477   if (peekscan (TOK_LBRACKET, &tok))
478     {
479       if (dec->rel == REL_POINTER)
480         {
481           error ("no array-of-pointer declarations -- use typedef");
482         }
483       dec->rel = REL_VECTOR;
484       scan_num (&tok);
485       dec->array_max = tok.str;
486       scan (TOK_RBRACKET, &tok);
487     }
488   else if (peekscan (TOK_LANGLE, &tok))
489     {
490       if (dec->rel == REL_POINTER)
491         {
492           error ("no array-of-pointer declarations -- use typedef");
493         }
494       dec->rel = REL_ARRAY;
495       if (peekscan (TOK_RANGLE, &tok))
496         {
497           dec->array_max = "~0";        /* unspecified size, use max */
498         }
499       else
500         {
501           scan_num (&tok);
502           dec->array_max = tok.str;
503           scan (TOK_RANGLE, &tok);
504         }
505     }
506   if (streq (dec->type, "opaque"))
507     {
508       if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR)
509         {
510           error ("array declaration expected");
511         }
512     }
513   else if (streq (dec->type, "string"))
514     {
515       if (dec->rel != REL_ARRAY)
516         {
517           error ("variable-length array declaration expected");
518         }
519     }
520 }
521
522 static void
523 get_prog_declaration (declaration * dec, defkind dkind, int num /* arg number */ )
524 {
525   token tok;
526   char name[10];                /* argument name */
527
528   if (dkind == DEF_PROGRAM)
529     {
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;
538         }
539     }
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 */
546
547   dec->name = (char *) strdup (name);
548
549   if (streq (dec->type, "void"))
550     {
551       return;
552     }
553
554   if (streq (dec->type, "opaque"))
555     {
556       error ("opaque -- illegal argument type");
557     }
558   if (peekscan (TOK_STAR, &tok))
559     {
560       if (streq (dec->type, "string"))
561         {
562           error ("pointer to string not allowed in program arguments\n");
563         }
564       dec->rel = REL_POINTER;
565       if (peekscan (TOK_IDENT, &tok))   /* optional name of argument */
566         dec->name = strdup (tok.str);
567     }
568   if (peekscan (TOK_LANGLE, &tok))
569     {
570       if (!streq (dec->type, "string"))
571         {
572           error ("arrays cannot be declared as arguments to procedures -- use typedef");
573         }
574       dec->rel = REL_ARRAY;
575       if (peekscan (TOK_RANGLE, &tok))
576         {
577           dec->array_max = "~0";        /* unspecified size, use max */
578         }
579       else
580         {
581           scan_num (&tok);
582           dec->array_max = tok.str;
583           scan (TOK_RANGLE, &tok);
584         }
585     }
586   if (streq (dec->type, "string"))
587     {
588       if (dec->rel != REL_ARRAY)
589         {                       /* .x specifies just string as
590                                  * type of argument
591                                  * - make it string<>
592                                  */
593           dec->rel = REL_ARRAY;
594           dec->array_max = "~0";        /* unspecified size, use max */
595         }
596     }
597 }
598
599 static void
600 get_type (const char **prefixp, const char **typep, defkind dkind)
601 {
602   token tok;
603
604   *prefixp = NULL;
605   get_token (&tok);
606   switch (tok.kind)
607     {
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)
635         {
636           error ("voids allowed only inside union and program definitions with one argument");
637         }
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");
651     }
652 }
653
654 static void
655 unsigned_dec (const char **typep)
656 {
657   token tok;
658
659   peek (&tok);
660   switch (tok.kind)
661     {
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;
688     }
689 }