Define USE_NSCD.
[kopensolaris-gnu/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 char parse_rcsid[] = 
35   "$Id$";
36
37 /*
38  * rpc_parse.c, Parser for the RPC protocol compiler 
39  * Copyright (C) 1987 Sun Microsystems, Inc.
40  */
41 #include <stdio.h>
42 #include <string.h>
43 #include "rpc/types.h"
44 #include "rpc_scan.h"
45 #include "rpc_parse.h"
46 #include "rpc_util.h"
47 #include "proto.h"
48
49 #define ARGNAME "arg"
50
51 static void isdefined(definition *defp);
52 static void def_struct(definition *defp);
53 static void def_program(definition *defp);
54 static void def_enum(definition *defp);
55 static void def_const(definition *defp);
56 static void def_union(definition *defp);
57 static void check_type_name(const char *name, int new_type);
58 static void def_typedef(definition *defp);
59 static void get_declaration(declaration *dec, defkind dkind);
60 static void get_prog_declaration(declaration *dec, defkind dkind, int num);
61 static void get_type(const char **prefixp, const char **typep, defkind dkind);
62 static void unsigned_dec(const char **typep);
63
64 /*
65  * return the next definition you see
66  */
67 definition *
68 get_definition(void)
69 {
70         definition *defp;
71         token tok;
72
73         defp = ALLOC(definition);
74         get_token(&tok);
75         switch (tok.kind) {
76         case TOK_STRUCT:
77                 def_struct(defp);
78                 break;
79         case TOK_UNION:
80                 def_union(defp);
81                 break;
82         case TOK_TYPEDEF:
83                 def_typedef(defp);
84                 break;
85         case TOK_ENUM:
86                 def_enum(defp);
87                 break;
88         case TOK_PROGRAM:
89                 def_program(defp);
90                 break;
91         case TOK_CONST:
92                 def_const(defp);
93                 break;
94         case TOK_EOF:
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                 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);
132         } while (tok.kind != TOK_RBRACE);
133         get_token(&tok);
134         *tailp = NULL;
135 }
136
137 static void
138 def_program(definition *defp)
139 {
140         token tok;
141         declaration dec;
142         decl_list *decls;
143         decl_list **tailp;
144         version_list *vlist;
145         version_list **vtailp;
146         proc_list *plist;
147         proc_list **ptailp;
148         int num_args;
149         bool_t isvoid = FALSE; /* whether first argument is void */
150         defp->def_kind = DEF_PROGRAM;
151         scan(TOK_IDENT, &tok);
152         defp->def_name = tok.str;
153         scan(TOK_LBRACE, &tok);
154         vtailp = &defp->def.pr.versions;
155         tailp = &defp->def.st.decls;
156         scan(TOK_VERSION, &tok);
157         do {
158                 scan(TOK_IDENT, &tok);
159                 vlist = ALLOC(version_list);
160                 vlist->vers_name = tok.str;
161                 scan(TOK_LBRACE, &tok);
162                 ptailp = &vlist->procs;
163                 do {
164                         /* get result type */
165                         plist = ALLOC(proc_list);
166                         get_type(&plist->res_prefix, &plist->res_type, 
167                                  DEF_PROGRAM);
168                         if (streq(plist->res_type, "opaque")) {
169                                 error("illegal result type");
170                         }
171                         scan(TOK_IDENT, &tok);
172                         plist->proc_name = tok.str;
173                         scan(TOK_LPAREN, &tok);
174                         /* get args - first one*/
175                         num_args = 1;
176                         isvoid = FALSE;
177                         /* type of DEF_PROGRAM in the first 
178                          * get_prog_declaration and DEF_STURCT in the next
179                          * allows void as argument if it is the only argument
180                          */
181                         get_prog_declaration(&dec, DEF_PROGRAM, num_args);
182                         if (streq(dec.type, "void"))
183                           isvoid = TRUE;
184                         decls = ALLOC(decl_list);
185                         plist->args.decls = decls;
186                         decls->decl = dec;
187                         tailp = &decls->next;
188                         /* get args */
189                         while(peekscan(TOK_COMMA, &tok)) {
190                           num_args++;
191                           get_prog_declaration(&dec, DEF_STRUCT, 
192                                                num_args);
193                           decls = ALLOC(decl_list);
194                           decls->decl = dec;
195                           *tailp = decls;
196                           if (streq(dec.type, "void"))
197                             isvoid = TRUE;
198                           tailp = &decls->next;
199                         }
200                         /* multiple arguments are only allowed in newstyle */
201                         if( !newstyle && num_args > 1 ) {
202                           error("only one argument is allowed" );
203                         }
204                         if (isvoid && num_args > 1) { 
205                           error("illegal use of void in program definition");
206                         }
207                         *tailp = NULL;
208                         scan(TOK_RPAREN, &tok);
209                         scan(TOK_EQUAL, &tok);
210                         scan_num(&tok);
211                         scan(TOK_SEMICOLON, &tok);
212                         plist->proc_num = tok.str;
213                         plist->arg_num = num_args;
214                         *ptailp = plist;
215                         ptailp = &plist->next;
216                         peek(&tok);
217                 } while (tok.kind != TOK_RBRACE);
218                 *ptailp = NULL;
219                 *vtailp = vlist;
220                 vtailp = &vlist->next;
221                 scan(TOK_RBRACE, &tok);
222                 scan(TOK_EQUAL, &tok);
223                 scan_num(&tok);
224                 vlist->vers_num = tok.str;
225                 /* make the argument structure name for each arg*/
226                 for(plist = vlist->procs; plist != NULL; 
227                     plist = plist->next) {
228                         plist->args.argname = make_argname(plist->proc_name,
229                                                            vlist->vers_num); 
230                         /* free the memory ??*/
231                 }
232                 scan(TOK_SEMICOLON, &tok);
233                 scan2(TOK_VERSION, TOK_RBRACE, &tok);
234         } while (tok.kind == TOK_VERSION);
235         scan(TOK_EQUAL, &tok);
236         scan_num(&tok);
237         defp->def.pr.prog_num = tok.str;
238         *vtailp = NULL;
239 }
240
241
242 static void
243 def_enum(definition *defp)
244 {
245         token tok;
246         enumval_list *elist;
247         enumval_list **tailp;
248
249         defp->def_kind = DEF_ENUM;
250         scan(TOK_IDENT, &tok);
251         defp->def_name = tok.str;
252         scan(TOK_LBRACE, &tok);
253         tailp = &defp->def.en.vals;
254         do {
255                 scan(TOK_IDENT, &tok);
256                 elist = ALLOC(enumval_list);
257                 elist->name = tok.str;
258                 elist->assignment = NULL;
259                 scan3(TOK_COMMA, TOK_RBRACE, TOK_EQUAL, &tok);
260                 if (tok.kind == TOK_EQUAL) {
261                         scan_num(&tok);
262                         elist->assignment = tok.str;
263                         scan2(TOK_COMMA, TOK_RBRACE, &tok);
264                 }
265                 *tailp = elist;
266                 tailp = &elist->next;
267         } while (tok.kind != TOK_RBRACE);
268         *tailp = NULL;
269 }
270
271 static void
272 def_const(definition *defp)
273 {
274         token tok;
275
276         defp->def_kind = DEF_CONST;
277         scan(TOK_IDENT, &tok);
278         defp->def_name = tok.str;
279         scan(TOK_EQUAL, &tok);
280         scan2(TOK_IDENT, TOK_STRCONST, &tok);
281         defp->def.co = tok.str;
282 }
283
284 static void
285 def_union(definition *defp)
286 {
287   token tok;
288   declaration dec;
289   case_list *cases;
290 /*  case_list *tcase; */
291   case_list **tailp;
292   int flag;
293
294   defp->def_kind = DEF_UNION;
295   scan(TOK_IDENT, &tok);
296   defp->def_name = tok.str;
297   scan(TOK_SWITCH, &tok);
298   scan(TOK_LPAREN, &tok);
299   get_declaration(&dec, DEF_UNION);
300   defp->def.un.enum_decl = dec;
301   tailp = &defp->def.un.cases;
302   scan(TOK_RPAREN, &tok);
303   scan(TOK_LBRACE, &tok);
304   scan(TOK_CASE, &tok);
305   while (tok.kind == TOK_CASE) {
306     scan2(TOK_IDENT, TOK_CHARCONST, &tok);
307     cases = ALLOC(case_list);
308     cases->case_name = tok.str;
309     scan(TOK_COLON, &tok);
310     /* now peek at next token */
311     flag=0;
312     if(peekscan(TOK_CASE,&tok))
313       {
314
315         do 
316           {
317             scan2(TOK_IDENT, TOK_CHARCONST, &tok);
318             cases->contflag=1;  /* continued case statement */
319             *tailp = cases;
320             tailp = &cases->next;
321             cases = ALLOC(case_list);
322             cases->case_name = tok.str;
323             scan(TOK_COLON, &tok);
324       
325           }while(peekscan(TOK_CASE,&tok));
326       }
327     else
328       if(flag)
329         {
330
331           *tailp = cases;
332           tailp = &cases->next;
333           cases = ALLOC(case_list);
334         };
335
336     get_declaration(&dec, DEF_UNION);
337     cases->case_decl = dec;
338     cases->contflag=0;          /* no continued case statement */
339     *tailp = cases;
340     tailp = &cases->next;
341     scan(TOK_SEMICOLON, &tok);
342
343     scan3(TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok);
344   }
345   *tailp = NULL;
346   if (tok.kind == TOK_DEFAULT) {
347     scan(TOK_COLON, &tok);
348     get_declaration(&dec, DEF_UNION);
349     defp->def.un.default_decl = ALLOC(declaration);
350     *defp->def.un.default_decl = dec;
351     scan(TOK_SEMICOLON, &tok);
352     scan(TOK_RBRACE, &tok);
353   } else {
354     defp->def.un.default_decl = NULL;
355   }
356 }
357
358 static const char *reserved_words[] =
359 {
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
373   };
374
375 static const char *reserved_types[] =
376 {
377   "opaque",
378   "string",
379   NULL
380   };
381
382 /*
383  * check that the given name is not one that would eventually result in
384  * xdr routines that would conflict with internal XDR routines. 
385  */
386 static void check_type_name(const char *name, int new_type)
387 {
388   int i;
389   char tmp[100];
390
391   for( i = 0; reserved_words[i] != NULL; i++ ) {
392     if( strcmp( name, reserved_words[i] ) == 0 ) {
393       sprintf(tmp, 
394               "illegal (reserved) name :\'%s\' in type definition", name );
395       error(tmp);
396     }
397   }
398   if( new_type ) {
399     for( i = 0; reserved_types[i] != NULL; i++ ) {
400       if( strcmp( name, reserved_types[i] ) == 0 ) {
401         sprintf(tmp, 
402                 "illegal (reserved) name :\'%s\' in type definition", name );
403         error(tmp);
404       }
405     }
406   }
407 }
408
409
410
411 static void
412 def_typedef(definition *defp)
413 {
414         declaration dec;
415
416         defp->def_kind = DEF_TYPEDEF;
417         get_declaration(&dec, DEF_TYPEDEF);
418         defp->def_name = dec.name;
419         check_type_name(dec.name, 1);
420         defp->def.ty.old_prefix = dec.prefix;
421         defp->def.ty.old_type = dec.type;
422         defp->def.ty.rel = dec.rel;
423         defp->def.ty.array_max = dec.array_max;
424 }
425
426 static void
427 get_declaration(declaration *dec, defkind dkind)
428 {
429         token tok;
430
431         get_type(&dec->prefix, &dec->type, dkind);
432         dec->rel = REL_ALIAS;
433         if (streq(dec->type, "void")) {
434                 return;
435         }
436
437         check_type_name(dec->type, 0);
438
439         scan2(TOK_STAR, TOK_IDENT, &tok);
440         if (tok.kind == TOK_STAR) {
441                 dec->rel = REL_POINTER;
442                 scan(TOK_IDENT, &tok);
443         }
444         dec->name = tok.str;
445         if (peekscan(TOK_LBRACKET, &tok)) {
446                 if (dec->rel == REL_POINTER) {
447                         error("no array-of-pointer declarations -- use typedef");
448                 }
449                 dec->rel = REL_VECTOR;
450                 scan_num(&tok);
451                 dec->array_max = tok.str;
452                 scan(TOK_RBRACKET, &tok);
453         } else if (peekscan(TOK_LANGLE, &tok)) {
454                 if (dec->rel == REL_POINTER) {
455                         error("no array-of-pointer declarations -- use typedef");
456                 }
457                 dec->rel = REL_ARRAY;
458                 if (peekscan(TOK_RANGLE, &tok)) {
459                         dec->array_max = "~0";  /* unspecified size, use max */
460                 } else {
461                         scan_num(&tok);
462                         dec->array_max = tok.str;
463                         scan(TOK_RANGLE, &tok);
464                 }
465         }
466         if (streq(dec->type, "opaque")) {
467                 if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR) {
468                         error("array declaration expected");
469                 }
470         } else if (streq(dec->type, "string")) {
471                 if (dec->rel != REL_ARRAY) {
472                         error("variable-length array declaration expected");
473                 }
474         }
475 }
476
477
478 static void
479 get_prog_declaration(declaration *dec, defkind dkind, int num /* arg number */)
480 {
481         token tok;
482         char name[10]; /* argument name */
483
484         if (dkind == DEF_PROGRAM) { 
485           peek(&tok);
486           if (tok.kind == TOK_RPAREN) { /* no arguments */
487                 dec->rel = REL_ALIAS;
488                 dec->type = "void";
489                 dec->prefix = NULL;
490                 dec->name = NULL;
491                 return;
492               }
493         }
494         get_type(&dec->prefix, &dec->type, dkind);
495         dec->rel = REL_ALIAS;
496         if (peekscan(TOK_IDENT, &tok))  /* optional name of argument */
497                 strcpy(name, tok.str);
498         else 
499                 sprintf(name, "%s%d", ARGNAME, num); /* default name of argument */
500
501         dec->name = (char *) strdup(name); 
502         
503         if (streq(dec->type, "void")) {
504                 return;
505         }
506
507         if (streq(dec->type, "opaque")) {
508                 error("opaque -- illegal argument type");
509         }
510         if (peekscan(TOK_STAR, &tok)) { 
511           if (streq(dec->type, "string")) {
512             error("pointer to string not allowed in program arguments\n");
513           }
514                 dec->rel = REL_POINTER;
515                 if (peekscan(TOK_IDENT, &tok))  /* optional name of argument */
516                   dec->name = strdup(tok.str);
517       }
518           if (peekscan(TOK_LANGLE, &tok)) {
519             if (!streq(dec->type, "string")) {
520               error("arrays cannot be declared as arguments to procedures -- use typedef");
521             }
522                 dec->rel = REL_ARRAY;
523                 if (peekscan(TOK_RANGLE, &tok)) {
524                         dec->array_max = "~0";/* unspecified size, use max */
525                 } else {
526                         scan_num(&tok);
527                         dec->array_max = tok.str;
528                         scan(TOK_RANGLE, &tok);
529                 }
530         }
531         if (streq(dec->type, "string")) {
532                 if (dec->rel != REL_ARRAY) {  /* .x specifies just string as
533                                                * type of argument 
534                                                * - make it string<>
535                                                */
536                         dec->rel = REL_ARRAY;
537                         dec->array_max = "~0";/* unspecified size, use max */
538                 }
539         }
540 }
541
542
543
544 static void
545 get_type(const char **prefixp, const char **typep, defkind dkind)
546 {
547         token tok;
548
549         *prefixp = NULL;
550         get_token(&tok);
551         switch (tok.kind) {
552         case TOK_IDENT:
553                 *typep = tok.str;
554                 break;
555         case TOK_STRUCT:
556         case TOK_ENUM:
557         case TOK_UNION:
558                 *prefixp = tok.str;
559                 scan(TOK_IDENT, &tok);
560                 *typep = tok.str;
561                 break;
562         case TOK_UNSIGNED:
563                 unsigned_dec(typep);
564                 break;
565         case TOK_SHORT:
566                 *typep = "short";
567                 (void) peekscan(TOK_INT, &tok);
568                 break;
569         case TOK_LONG:
570                 *typep = "long";
571                 (void) peekscan(TOK_INT, &tok);
572                 break;
573         case TOK_VOID:
574                 if (dkind != DEF_UNION && dkind != DEF_PROGRAM) {
575                         error("voids allowed only inside union and program definitions with one argument");
576                 }
577                 *typep = tok.str;
578                 break;
579         case TOK_STRING:
580         case TOK_OPAQUE:
581         case TOK_CHAR:
582         case TOK_INT:
583         case TOK_FLOAT:
584         case TOK_DOUBLE:
585         case TOK_BOOL:
586                 *typep = tok.str;
587                 break;
588         default:
589                 error("expected type specifier");
590         }
591 }
592
593 static void
594 unsigned_dec(const char **typep)
595 {
596         token tok;
597
598         peek(&tok);
599         switch (tok.kind) {
600         case TOK_CHAR:
601                 get_token(&tok);
602                 *typep = "u_char";
603                 break;
604         case TOK_SHORT:
605                 get_token(&tok);
606                 *typep = "u_short";
607                 (void) peekscan(TOK_INT, &tok);
608                 break;
609         case TOK_LONG:
610                 get_token(&tok);
611                 *typep = "u_long";
612                 (void) peekscan(TOK_INT, &tok);
613                 break;
614         case TOK_INT:
615                 get_token(&tok);
616                 *typep = "u_int";
617                 break;
618         default:
619                 *typep = "u_int";
620                 break;
621         }
622 }