Initial revision
[nfs-utils.git] / tools / rpcgen / 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 #ifndef lint
32 static char sccsid[] = "@(#)rpc_parse.c 1.8 89/02/22 (C) 1987 SMI";
33 #endif
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
46 #define ARGNAME "arg"
47
48 /*
49 extern char *make_argname();
50 extern char *strdup();
51  */
52
53 static void     isdefined(definition *defp);
54 static void     def_struct(definition *defp);
55 static void     def_program(definition *defp);
56 static void     def_enum(definition *defp);
57 static void     def_const(definition *defp);
58 static void     def_union(definition *defp);
59 static void     check_type_name(char *name, int new_type);
60 static void     def_typedef(definition *defp);
61 static void     get_declaration(declaration *dec, defkind dkind);
62 static void     get_prog_declaration(declaration *dec, defkind dkind, int num);
63 static void     get_type(char **prefixp, char **typep, defkind dkind);
64 static void     unsigned_dec(char **typep);
65
66 /*
67  * return the next definition you see
68  */
69 definition *
70 get_definition(void)
71 {
72         definition *defp;
73         token tok;
74
75         defp = ALLOC(definition);
76         get_token(&tok);
77         switch (tok.kind) {
78         case TOK_STRUCT:
79                 def_struct(defp);
80                 break;
81         case TOK_UNION:
82                 def_union(defp);
83                 break;
84         case TOK_TYPEDEF:
85                 def_typedef(defp);
86                 break;
87         case TOK_ENUM:
88                 def_enum(defp);
89                 break;
90         case TOK_PROGRAM:
91                 def_program(defp);
92                 break;
93         case TOK_CONST:
94                 def_const(defp);
95                 break;
96         case TOK_EOF:
97                 return (NULL);
98         default:
99                 error("definition keyword expected");
100         }
101         scan(TOK_SEMICOLON, &tok);
102         isdefined(defp);
103         return (defp);
104 }
105
106 static void
107 isdefined(definition *defp)
108 {
109         STOREVAL(&defined, defp);
110 }
111
112 static void
113 def_struct(definition *defp)
114 {
115         token tok;
116         declaration dec;
117         decl_list *decls;
118         decl_list **tailp;
119
120         defp->def_kind = DEF_STRUCT;
121
122         scan(TOK_IDENT, &tok);
123         defp->def_name = tok.str;
124         scan(TOK_LBRACE, &tok);
125         tailp = &defp->def.st.decls;
126         do {
127                 get_declaration(&dec, DEF_STRUCT);
128                 decls = ALLOC(decl_list);
129                 decls->decl = dec;
130                 *tailp = decls;
131                 tailp = &decls->next;
132                 scan(TOK_SEMICOLON, &tok);
133                 peek(&tok);
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                 scan(TOK_IDENT, &tok);
161                 vlist = ALLOC(version_list);
162                 vlist->vers_name = tok.str;
163                 scan(TOK_LBRACE, &tok);
164                 ptailp = &vlist->procs;
165                 do {
166                         /* get result type */
167                         plist = ALLOC(proc_list);
168                         get_type(&plist->res_prefix, &plist->res_type, 
169                                  DEF_PROGRAM);
170                         if (streq(plist->res_type, "opaque")) {
171                                 error("illegal result type");
172                         }
173                         scan(TOK_IDENT, &tok);
174                         plist->proc_name = tok.str;
175                         scan(TOK_LPAREN, &tok);
176                         /* get args - first one*/
177                         num_args = 1;
178                         isvoid = FALSE;
179                         /* type of DEF_PROGRAM in the first 
180                          * get_prog_declaration and DEF_STURCT in the next
181                          * allows void as argument if it is the only argument
182                          */
183                         get_prog_declaration(&dec, DEF_PROGRAM, num_args);
184                         if (streq(dec.type, "void"))
185                           isvoid = TRUE;
186                         decls = ALLOC(decl_list);
187                         plist->args.decls = decls;
188                         decls->decl = dec;
189                         tailp = &decls->next;
190                         /* get args */
191                         while(peekscan(TOK_COMMA, &tok)) {
192                           num_args++;
193                           get_prog_declaration(&dec, DEF_STRUCT, 
194                                                num_args);
195                           decls = ALLOC(decl_list);
196                           decls->decl = dec;
197                           *tailp = decls;
198                           if (streq(dec.type, "void"))
199                             isvoid = TRUE;
200                           tailp = &decls->next;
201                         }
202                         /* multiple arguments are only allowed in newstyle */
203                         if( !newstyle && num_args > 1 ) {
204                           error("only one argument is allowed" );
205                         }
206                         if (isvoid && num_args > 1) { 
207                           error("illegal use of void in program definition");
208                         }
209                         *tailp = NULL;
210                         scan(TOK_RPAREN, &tok);
211                         scan(TOK_EQUAL, &tok);
212                         scan_num(&tok);
213                         scan(TOK_SEMICOLON, &tok);
214                         plist->proc_num = tok.str;
215                         plist->arg_num = num_args;
216                         *ptailp = plist;
217                         ptailp = &plist->next;
218                         peek(&tok);
219                 } while (tok.kind != TOK_RBRACE);
220                 *ptailp = NULL;
221                 *vtailp = vlist;
222                 vtailp = &vlist->next;
223                 scan(TOK_RBRACE, &tok);
224                 scan(TOK_EQUAL, &tok);
225                 scan_num(&tok);
226                 vlist->vers_num = tok.str;
227                 /* make the argument structure name for each arg*/
228                 for(plist = vlist->procs; plist != NULL; 
229                     plist = plist->next) {
230                         plist->args.argname = make_argname(plist->proc_name,
231                                                            vlist->vers_num); 
232                         /* free the memory ??*/
233                 }
234                 scan(TOK_SEMICOLON, &tok);
235                 scan2(TOK_VERSION, TOK_RBRACE, &tok);
236         } while (tok.kind == TOK_VERSION);
237         scan(TOK_EQUAL, &tok);
238         scan_num(&tok);
239         defp->def.pr.prog_num = tok.str;
240         *vtailp = NULL;
241 }
242
243
244 static void
245 def_enum(definition *defp)
246 {
247         token tok;
248         enumval_list *elist;
249         enumval_list **tailp;
250
251         defp->def_kind = DEF_ENUM;
252         scan(TOK_IDENT, &tok);
253         defp->def_name = tok.str;
254         scan(TOK_LBRACE, &tok);
255         tailp = &defp->def.en.vals;
256         do {
257                 scan(TOK_IDENT, &tok);
258                 elist = ALLOC(enumval_list);
259                 elist->name = tok.str;
260                 elist->assignment = NULL;
261                 scan3(TOK_COMMA, TOK_RBRACE, TOK_EQUAL, &tok);
262                 if (tok.kind == TOK_EQUAL) {
263                         scan_num(&tok);
264                         elist->assignment = tok.str;
265                         scan2(TOK_COMMA, TOK_RBRACE, &tok);
266                 }
267                 *tailp = elist;
268                 tailp = &elist->next;
269         } while (tok.kind != TOK_RBRACE);
270         *tailp = NULL;
271 }
272
273 static void
274 def_const(definition *defp)
275 {
276         token tok;
277
278         defp->def_kind = DEF_CONST;
279         scan(TOK_IDENT, &tok);
280         defp->def_name = tok.str;
281         scan(TOK_EQUAL, &tok);
282         scan2(TOK_IDENT, TOK_STRCONST, &tok);
283         defp->def.co = tok.str;
284 }
285
286 static void
287 def_union(definition *defp)
288 {
289   token tok;
290   declaration dec;
291   case_list *cases,*tcase;
292   case_list **tailp;
293   int flag;
294
295   defp->def_kind = DEF_UNION;
296   scan(TOK_IDENT, &tok);
297   defp->def_name = tok.str;
298   scan(TOK_SWITCH, &tok);
299   scan(TOK_LPAREN, &tok);
300   get_declaration(&dec, DEF_UNION);
301   defp->def.un.enum_decl = dec;
302   tailp = &defp->def.un.cases;
303   scan(TOK_RPAREN, &tok);
304   scan(TOK_LBRACE, &tok);
305   scan(TOK_CASE, &tok);
306   while (tok.kind == TOK_CASE) {
307     scan2(TOK_IDENT, TOK_CHARCONST, &tok);
308     cases = ALLOC(case_list);
309     cases->case_name = tok.str;
310     scan(TOK_COLON, &tok);
311     /* now peek at next token */
312     flag=0;
313     if(peekscan(TOK_CASE,&tok))
314       {
315
316         do 
317           {
318             scan2(TOK_IDENT, TOK_CHARCONST, &tok);
319             cases->contflag=1;  /* continued case statement */
320             *tailp = cases;
321             tailp = &cases->next;
322             cases = ALLOC(case_list);
323             cases->case_name = tok.str;
324             scan(TOK_COLON, &tok);
325       
326           }while(peekscan(TOK_CASE,&tok));
327       }
328     else
329       if(flag)
330         {
331
332           *tailp = cases;
333           tailp = &cases->next;
334           cases = ALLOC(case_list);
335         };
336
337     get_declaration(&dec, DEF_UNION);
338     cases->case_decl = dec;
339     cases->contflag=0;          /* no continued case statement */
340     *tailp = cases;
341     tailp = &cases->next;
342     scan(TOK_SEMICOLON, &tok);
343
344     scan3(TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok);
345   }
346   *tailp = NULL;
347   if (tok.kind == TOK_DEFAULT) {
348     scan(TOK_COLON, &tok);
349     get_declaration(&dec, DEF_UNION);
350     defp->def.un.default_decl = ALLOC(declaration);
351     *defp->def.un.default_decl = dec;
352     scan(TOK_SEMICOLON, &tok);
353     scan(TOK_RBRACE, &tok);
354   } else {
355     defp->def.un.default_decl = NULL;
356   }
357 }
358
359 static char* reserved_words[] =
360 {
361   "array",
362   "bytes",
363   "destroy",
364   "free",
365   "getpos",
366   "inline",
367   "pointer",
368   "reference",
369   "setpos",
370   "sizeof",
371   "union",
372   "vector",
373   NULL
374   };
375
376 static char* reserved_types[] =
377 {
378   "opaque",
379   "string",
380   NULL
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 static void
386 check_type_name(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 static void
410 def_typedef(definition *defp)
411 {
412         declaration dec;
413
414         defp->def_kind = DEF_TYPEDEF;
415         get_declaration(&dec, DEF_TYPEDEF);
416         defp->def_name = dec.name;
417         check_type_name( dec.name, 1 );
418         defp->def.ty.old_prefix = dec.prefix;
419         defp->def.ty.old_type = dec.type;
420         defp->def.ty.rel = dec.rel;
421         defp->def.ty.array_max = dec.array_max;
422 }
423
424 static void
425 get_declaration(declaration *dec, defkind dkind)
426 {
427         token tok;
428
429         get_type(&dec->prefix, &dec->type, dkind);
430         dec->rel = REL_ALIAS;
431         if (streq(dec->type, "void")) {
432                 return;
433         }
434
435         check_type_name( dec->type, 0 );
436
437         scan2(TOK_STAR, TOK_IDENT, &tok);
438         if (tok.kind == TOK_STAR) {
439                 dec->rel = REL_POINTER;
440                 scan(TOK_IDENT, &tok);
441         }
442         dec->name = tok.str;
443         if (peekscan(TOK_LBRACKET, &tok)) {
444                 if (dec->rel == REL_POINTER) {
445                         error("no array-of-pointer declarations -- use typedef");
446                 }
447                 dec->rel = REL_VECTOR;
448                 scan_num(&tok);
449                 dec->array_max = tok.str;
450                 scan(TOK_RBRACKET, &tok);
451         } else if (peekscan(TOK_LANGLE, &tok)) {
452                 if (dec->rel == REL_POINTER) {
453                         error("no array-of-pointer declarations -- use typedef");
454                 }
455                 dec->rel = REL_ARRAY;
456                 if (peekscan(TOK_RANGLE, &tok)) {
457                         dec->array_max = "~0";  /* unspecified size, use max */
458                 } else {
459                         scan_num(&tok);
460                         dec->array_max = tok.str;
461                         scan(TOK_RANGLE, &tok);
462                 }
463         }
464         if (streq(dec->type, "opaque")) {
465                 if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR) {
466                         error("array declaration expected");
467                 }
468         } else if (streq(dec->type, "string")) {
469                 if (dec->rel != REL_ARRAY) {
470                         error("variable-length array declaration expected");
471                 }
472         }
473 }
474
475
476 static void
477 get_prog_declaration(declaration *dec, defkind dkind, int num)
478 {
479         token tok;
480         char name[10]; /* argument name */
481
482         if (dkind == DEF_PROGRAM) { 
483           peek(&tok);
484           if (tok.kind == TOK_RPAREN) { /* no arguments */
485                 dec->rel = REL_ALIAS;
486                 dec->type = "void";
487                 dec->prefix = NULL;
488                 dec->name = NULL;
489                 return;
490               }
491         }
492         get_type(&dec->prefix, &dec->type, dkind);
493         dec->rel = REL_ALIAS;
494         if (peekscan(TOK_IDENT, &tok))  /* optional name of argument */
495                 strcpy(name, tok.str);
496         else 
497                 sprintf(name, "%s%d", ARGNAME, num); /* default name of argument */
498
499         dec->name = (char *) strdup(name); 
500         
501         if (streq(dec->type, "void")) {
502                 return;
503         }
504
505         if (streq(dec->type, "opaque")) {
506                 error("opaque -- illegal argument type");
507         }
508         if (peekscan(TOK_STAR, &tok)) { 
509           if (streq(dec->type, "string")) {
510             error("pointer to string not allowed in program arguments\n");
511           }
512                 dec->rel = REL_POINTER;
513                 if (peekscan(TOK_IDENT, &tok))  /* optional name of argument */
514                   dec->name = strdup(tok.str);
515       }
516           if (peekscan(TOK_LANGLE, &tok)) {
517             if (!streq(dec->type, "string")) {
518               error("arrays cannot be declared as arguments to procedures -- use typedef");
519             }
520                 dec->rel = REL_ARRAY;
521                 if (peekscan(TOK_RANGLE, &tok)) {
522                         dec->array_max = "~0";/* unspecified size, use max */
523                 } else {
524                         scan_num(&tok);
525                         dec->array_max = tok.str;
526                         scan(TOK_RANGLE, &tok);
527                 }
528         }
529         if (streq(dec->type, "string")) {
530                 if (dec->rel != REL_ARRAY) {  /* .x specifies just string as
531                                                * type of argument 
532                                                * - make it string<>
533                                                */
534                         dec->rel = REL_ARRAY;
535                         dec->array_max = "~0";/* unspecified size, use max */
536                 }
537         }
538 }
539
540
541
542 static void
543 get_type(char **prefixp, char **typep, defkind dkind)
544 {
545         token tok;
546
547         *prefixp = NULL;
548         get_token(&tok);
549         switch (tok.kind) {
550         case TOK_IDENT:
551                 *typep = tok.str;
552                 break;
553         case TOK_STRUCT:
554         case TOK_ENUM:
555         case TOK_UNION:
556                 *prefixp = tok.str;
557                 scan(TOK_IDENT, &tok);
558                 *typep = tok.str;
559                 break;
560         case TOK_UNSIGNED:
561                 unsigned_dec(typep);
562                 break;
563         case TOK_SHORT:
564                 *typep = "short";
565                 (void) peekscan(TOK_INT, &tok);
566                 break;
567         case TOK_LONG:
568                 *typep = "long";
569                 (void) peekscan(TOK_INT, &tok);
570                 break;
571         case TOK_VOID:
572                 if (dkind != DEF_UNION && dkind != DEF_PROGRAM) {
573                         error("voids allowed only inside union and program definitions with one argument");
574                 }
575                 *typep = tok.str;
576                 break;
577         case TOK_STRING:
578         case TOK_OPAQUE:
579         case TOK_CHAR:
580         case TOK_INT:
581         case TOK_FLOAT:
582         case TOK_DOUBLE:
583         case TOK_BOOL:
584                 *typep = tok.str;
585                 break;
586         default:
587                 error("expected type specifier");
588         }
589 }
590
591 static void
592 unsigned_dec(char **typep)
593 {
594         token tok;
595
596         peek(&tok);
597         switch (tok.kind) {
598         case TOK_CHAR:
599                 get_token(&tok);
600                 *typep = "u_char";
601                 break;
602         case TOK_SHORT:
603                 get_token(&tok);
604                 *typep = "u_short";
605                 (void) peekscan(TOK_INT, &tok);
606                 break;
607         case TOK_LONG:
608                 get_token(&tok);
609                 *typep = "u_long";
610                 (void) peekscan(TOK_INT, &tok);
611                 break;
612         case TOK_INT:
613                 get_token(&tok);
614                 *typep = "u_int";
615                 break;
616         default:
617                 *typep = "u_int";
618                 break;
619         }
620 }