]> git.decadent.org.uk Git - nfs-utils.git/blob - tools/rpcgen/rpc_parse.c
nfs-iostat.py: divide by zero with fresh mount
[nfs-utils.git] / tools / rpcgen / rpc_parse.c
1 /*
2  * Copyright (c) 2009, Sun Microsystems, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * - Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  *   this list of conditions and the following disclaimer in the documentation
11  *   and/or other materials provided with the distribution.
12  * - Neither the name of Sun Microsystems, Inc. nor the names of its
13  *   contributors may be used to endorse or promote products derived
14  *   from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #if 0
30 static char sccsid[] = "@(#)rpc_parse.c 1.8 89/02/22 (C) 1987 SMI";
31 #endif
32
33 /*
34  * rpc_parse.c, Parser for the RPC protocol compiler 
35  * Copyright (C) 1987 Sun Microsystems, Inc.
36  */
37 #include <stdio.h>
38 #include <string.h>
39 #include "rpc/types.h"
40 #include "rpc_scan.h"
41 #include "rpc_parse.h"
42 #include "rpc_util.h"
43
44 #define ARGNAME "arg"
45
46 /*
47 extern char *make_argname();
48 extern char *strdup();
49  */
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(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(char **prefixp, char **typep, defkind dkind);
62 static void     unsigned_dec(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                 free(defp);
96                 return (NULL);
97         default:
98                 error("definition keyword expected");
99         }
100         scan(TOK_SEMICOLON, &tok);
101         isdefined(defp);
102         return (defp);
103 }
104
105 static void
106 isdefined(definition *defp)
107 {
108         STOREVAL(&defined, defp);
109 }
110
111 static void
112 def_struct(definition *defp)
113 {
114         token tok;
115         declaration dec;
116         decl_list *decls;
117         decl_list **tailp;
118
119         defp->def_kind = DEF_STRUCT;
120
121         scan(TOK_IDENT, &tok);
122         defp->def_name = tok.str;
123         scan(TOK_LBRACE, &tok);
124         tailp = &defp->def.st.decls;
125         do {
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         } while (tok.kind != TOK_RBRACE);
134         get_token(&tok);
135         *tailp = NULL;
136 }
137
138 static void
139 def_program(definition *defp)
140 {
141         token tok;
142         declaration dec;
143         decl_list *decls;
144         decl_list **tailp;
145         version_list *vlist;
146         version_list **vtailp;
147         proc_list *plist;
148         proc_list **ptailp;
149         int num_args;
150         bool_t isvoid = FALSE; /* whether first argument is void */
151         defp->def_kind = DEF_PROGRAM;
152         scan(TOK_IDENT, &tok);
153         defp->def_name = tok.str;
154         scan(TOK_LBRACE, &tok);
155         vtailp = &defp->def.pr.versions;
156         tailp = &defp->def.st.decls;
157         scan(TOK_VERSION, &tok);
158         do {
159                 scan(TOK_IDENT, &tok);
160                 vlist = ALLOC(version_list);
161                 vlist->vers_name = tok.str;
162                 scan(TOK_LBRACE, &tok);
163                 ptailp = &vlist->procs;
164                 do {
165                         /* get result type */
166                         plist = ALLOC(proc_list);
167                         get_type(&plist->res_prefix, &plist->res_type, 
168                                  DEF_PROGRAM);
169                         if (streq(plist->res_type, "opaque")) {
170                                 error("illegal result type");
171                         }
172                         scan(TOK_IDENT, &tok);
173                         plist->proc_name = tok.str;
174                         scan(TOK_LPAREN, &tok);
175                         /* get args - first one*/
176                         num_args = 1;
177                         isvoid = FALSE;
178                         /* type of DEF_PROGRAM in the first 
179                          * get_prog_declaration and DEF_STURCT in the next
180                          * allows void as argument if it is the only argument
181                          */
182                         get_prog_declaration(&dec, DEF_PROGRAM, num_args);
183                         if (streq(dec.type, "void"))
184                           isvoid = TRUE;
185                         decls = ALLOC(decl_list);
186                         plist->args.decls = decls;
187                         decls->decl = dec;
188                         tailp = &decls->next;
189                         /* get args */
190                         while(peekscan(TOK_COMMA, &tok)) {
191                           num_args++;
192                           get_prog_declaration(&dec, DEF_STRUCT, 
193                                                num_args);
194                           decls = ALLOC(decl_list);
195                           decls->decl = dec;
196                           *tailp = decls;
197                           if (streq(dec.type, "void"))
198                             isvoid = TRUE;
199                           tailp = &decls->next;
200                         }
201                         /* multiple arguments are only allowed in newstyle */
202                         if( !newstyle && num_args > 1 ) {
203                           error("only one argument is allowed" );
204                         }
205                         if (isvoid && num_args > 1) { 
206                           error("illegal use of void in program definition");
207                         }
208                         *tailp = NULL;
209                         scan(TOK_RPAREN, &tok);
210                         scan(TOK_EQUAL, &tok);
211                         scan_num(&tok);
212                         scan(TOK_SEMICOLON, &tok);
213                         plist->proc_num = tok.str;
214                         plist->arg_num = num_args;
215                         *ptailp = plist;
216                         ptailp = &plist->next;
217                         peek(&tok);
218                 } while (tok.kind != TOK_RBRACE);
219                 *ptailp = NULL;
220                 *vtailp = vlist;
221                 vtailp = &vlist->next;
222                 scan(TOK_RBRACE, &tok);
223                 scan(TOK_EQUAL, &tok);
224                 scan_num(&tok);
225                 vlist->vers_num = tok.str;
226                 /* make the argument structure name for each arg*/
227                 for(plist = vlist->procs; plist != NULL; 
228                     plist = plist->next) {
229                         plist->args.argname = make_argname(plist->proc_name,
230                                                            vlist->vers_num); 
231                         /* free the memory ??*/
232                 }
233                 scan(TOK_SEMICOLON, &tok);
234                 scan2(TOK_VERSION, TOK_RBRACE, &tok);
235         } while (tok.kind == TOK_VERSION);
236         scan(TOK_EQUAL, &tok);
237         scan_num(&tok);
238         defp->def.pr.prog_num = tok.str;
239         *vtailp = NULL;
240 }
241
242
243 static void
244 def_enum(definition *defp)
245 {
246         token tok;
247         enumval_list *elist;
248         enumval_list **tailp;
249
250         defp->def_kind = DEF_ENUM;
251         scan(TOK_IDENT, &tok);
252         defp->def_name = tok.str;
253         scan(TOK_LBRACE, &tok);
254         tailp = &defp->def.en.vals;
255         do {
256                 scan(TOK_IDENT, &tok);
257                 elist = ALLOC(enumval_list);
258                 elist->name = tok.str;
259                 elist->assignment = NULL;
260                 scan3(TOK_COMMA, TOK_RBRACE, TOK_EQUAL, &tok);
261                 if (tok.kind == TOK_EQUAL) {
262                         scan_num(&tok);
263                         elist->assignment = tok.str;
264                         scan2(TOK_COMMA, TOK_RBRACE, &tok);
265                 }
266                 *tailp = elist;
267                 tailp = &elist->next;
268         } while (tok.kind != TOK_RBRACE);
269         *tailp = NULL;
270 }
271
272 static void
273 def_const(definition *defp)
274 {
275         token tok;
276
277         defp->def_kind = DEF_CONST;
278         scan(TOK_IDENT, &tok);
279         defp->def_name = tok.str;
280         scan(TOK_EQUAL, &tok);
281         scan2(TOK_IDENT, TOK_STRCONST, &tok);
282         defp->def.co = tok.str;
283 }
284
285 static void
286 def_union(definition *defp)
287 {
288   token tok;
289   declaration dec;
290   case_list *cases;
291   case_list **tailp;
292
293   defp->def_kind = DEF_UNION;
294   scan(TOK_IDENT, &tok);
295   defp->def_name = tok.str;
296   scan(TOK_SWITCH, &tok);
297   scan(TOK_LPAREN, &tok);
298   get_declaration(&dec, DEF_UNION);
299   defp->def.un.enum_decl = dec;
300   tailp = &defp->def.un.cases;
301   scan(TOK_RPAREN, &tok);
302   scan(TOK_LBRACE, &tok);
303   scan(TOK_CASE, &tok);
304   while (tok.kind == TOK_CASE) {
305     scan2(TOK_IDENT, TOK_CHARCONST, &tok);
306     cases = ALLOC(case_list);
307     cases->case_name = tok.str;
308     scan(TOK_COLON, &tok);
309     /* now peek at next token */
310     if(peekscan(TOK_CASE,&tok))
311       {
312
313         do 
314           {
315             scan2(TOK_IDENT, TOK_CHARCONST, &tok);
316             cases->contflag=1;  /* continued case statement */
317             *tailp = cases;
318             tailp = &cases->next;
319             cases = ALLOC(case_list);
320             cases->case_name = tok.str;
321             scan(TOK_COLON, &tok);
322       
323           }while(peekscan(TOK_CASE,&tok));
324       }
325
326     get_declaration(&dec, DEF_UNION);
327     cases->case_decl = dec;
328     cases->contflag=0;          /* no continued case statement */
329     *tailp = cases;
330     tailp = &cases->next;
331     scan(TOK_SEMICOLON, &tok);
332
333     scan3(TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok);
334   }
335   *tailp = NULL;
336   if (tok.kind == TOK_DEFAULT) {
337     scan(TOK_COLON, &tok);
338     get_declaration(&dec, DEF_UNION);
339     defp->def.un.default_decl = ALLOC(declaration);
340     *defp->def.un.default_decl = dec;
341     scan(TOK_SEMICOLON, &tok);
342     scan(TOK_RBRACE, &tok);
343   } else {
344     defp->def.un.default_decl = NULL;
345   }
346 }
347
348 static char* reserved_words[] =
349 {
350   "array",
351   "bytes",
352   "destroy",
353   "free",
354   "getpos",
355   "inline",
356   "pointer",
357   "reference",
358   "setpos",
359   "sizeof",
360   "union",
361   "vector",
362   NULL
363   };
364
365 static char* reserved_types[] =
366 {
367   "opaque",
368   "string",
369   NULL
370   };
371
372 /* check that the given name is not one that would eventually result in
373    xdr routines that would conflict with internal XDR routines. */
374 static void
375 check_type_name(char *name, int new_type)
376 {
377   int i;
378   char tmp[100];
379
380   for( i = 0; reserved_words[i] != NULL; i++ ) {
381     if( strcmp( name, reserved_words[i] ) == 0 ) {
382       sprintf(tmp, 
383               "illegal (reserved) name :\'%s\' in type definition", name );
384       error(tmp);
385     }
386   }
387   if( new_type ) {
388     for( i = 0; reserved_types[i] != NULL; i++ ) {
389       if( strcmp( name, reserved_types[i] ) == 0 ) {
390         sprintf(tmp, 
391                 "illegal (reserved) name :\'%s\' in type definition", name );
392         error(tmp);
393       }
394     }
395   }
396 }
397
398 static void
399 def_typedef(definition *defp)
400 {
401         declaration dec;
402
403         defp->def_kind = DEF_TYPEDEF;
404         get_declaration(&dec, DEF_TYPEDEF);
405         defp->def_name = dec.name;
406         check_type_name( dec.name, 1 );
407         defp->def.ty.old_prefix = dec.prefix;
408         defp->def.ty.old_type = dec.type;
409         defp->def.ty.rel = dec.rel;
410         defp->def.ty.array_max = dec.array_max;
411 }
412
413 static void
414 get_declaration(declaration *dec, defkind dkind)
415 {
416         token tok;
417
418         get_type(&dec->prefix, &dec->type, dkind);
419         dec->rel = REL_ALIAS;
420         if (streq(dec->type, "void")) {
421                 return;
422         }
423
424         check_type_name( dec->type, 0 );
425
426         scan2(TOK_STAR, TOK_IDENT, &tok);
427         if (tok.kind == TOK_STAR) {
428                 dec->rel = REL_POINTER;
429                 scan(TOK_IDENT, &tok);
430         }
431         dec->name = tok.str;
432         if (peekscan(TOK_LBRACKET, &tok)) {
433                 if (dec->rel == REL_POINTER) {
434                         error("no array-of-pointer declarations -- use typedef");
435                 }
436                 dec->rel = REL_VECTOR;
437                 scan_num(&tok);
438                 dec->array_max = tok.str;
439                 scan(TOK_RBRACKET, &tok);
440         } else if (peekscan(TOK_LANGLE, &tok)) {
441                 if (dec->rel == REL_POINTER) {
442                         error("no array-of-pointer declarations -- use typedef");
443                 }
444                 dec->rel = REL_ARRAY;
445                 if (peekscan(TOK_RANGLE, &tok)) {
446                         dec->array_max = "~0";  /* unspecified size, use max */
447                 } else {
448                         scan_num(&tok);
449                         dec->array_max = tok.str;
450                         scan(TOK_RANGLE, &tok);
451                 }
452         }
453         if (streq(dec->type, "opaque")) {
454                 if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR) {
455                         error("array declaration expected");
456                 }
457         } else if (streq(dec->type, "string")) {
458                 if (dec->rel != REL_ARRAY) {
459                         error("variable-length array declaration expected");
460                 }
461         }
462 }
463
464
465 static void
466 get_prog_declaration(declaration *dec, defkind dkind, int num)
467 {
468         token tok;
469         char name[10]; /* argument name */
470
471         if (dkind == DEF_PROGRAM) { 
472           peek(&tok);
473           if (tok.kind == TOK_RPAREN) { /* no arguments */
474                 dec->rel = REL_ALIAS;
475                 dec->type = "void";
476                 dec->prefix = NULL;
477                 dec->name = NULL;
478                 return;
479               }
480         }
481         get_type(&dec->prefix, &dec->type, dkind);
482         dec->rel = REL_ALIAS;
483         if (peekscan(TOK_IDENT, &tok))  /* optional name of argument */
484                 strcpy(name, tok.str);
485         else 
486                 sprintf(name, "%s%d", ARGNAME, num); /* default name of argument */
487
488         dec->name = (char *) strdup(name); 
489         
490         if (streq(dec->type, "void")) {
491                 return;
492         }
493
494         if (streq(dec->type, "opaque")) {
495                 error("opaque -- illegal argument type");
496         }
497         if (peekscan(TOK_STAR, &tok)) { 
498           if (streq(dec->type, "string")) {
499             error("pointer to string not allowed in program arguments\n");
500           }
501                 dec->rel = REL_POINTER;
502                 if (peekscan(TOK_IDENT, &tok))  /* optional name of argument */
503                   dec->name = strdup(tok.str);
504       }
505           if (peekscan(TOK_LANGLE, &tok)) {
506             if (!streq(dec->type, "string")) {
507               error("arrays cannot be declared as arguments to procedures -- use typedef");
508             }
509                 dec->rel = REL_ARRAY;
510                 if (peekscan(TOK_RANGLE, &tok)) {
511                         dec->array_max = "~0";/* unspecified size, use max */
512                 } else {
513                         scan_num(&tok);
514                         dec->array_max = tok.str;
515                         scan(TOK_RANGLE, &tok);
516                 }
517         }
518         if (streq(dec->type, "string")) {
519                 if (dec->rel != REL_ARRAY) {  /* .x specifies just string as
520                                                * type of argument 
521                                                * - make it string<>
522                                                */
523                         dec->rel = REL_ARRAY;
524                         dec->array_max = "~0";/* unspecified size, use max */
525                 }
526         }
527 }
528
529
530
531 static void
532 get_type(char **prefixp, char **typep, defkind dkind)
533 {
534         token tok;
535
536         *prefixp = NULL;
537         get_token(&tok);
538         switch (tok.kind) {
539         case TOK_IDENT:
540                 *typep = tok.str;
541                 break;
542         case TOK_STRUCT:
543         case TOK_ENUM:
544         case TOK_UNION:
545                 *prefixp = tok.str;
546                 scan(TOK_IDENT, &tok);
547                 *typep = tok.str;
548                 break;
549         case TOK_UNSIGNED:
550                 unsigned_dec(typep);
551                 break;
552         case TOK_SHORT:
553                 *typep = "short";
554                 (void) peekscan(TOK_INT, &tok);
555                 break;
556         case TOK_INT32:
557                 *typep = "int32_t";
558                 (void) peekscan(TOK_INT, &tok);
559                 break;
560         case TOK_VOID:
561                 if (dkind != DEF_UNION && dkind != DEF_PROGRAM) {
562                         error("voids allowed only inside union and program definitions with one argument");
563                 }
564                 *typep = tok.str;
565                 break;
566         case TOK_STRING:
567         case TOK_OPAQUE:
568         case TOK_CHAR:
569         case TOK_INT:
570         case TOK_FLOAT:
571         case TOK_DOUBLE:
572         case TOK_BOOL:
573                 *typep = tok.str;
574                 break;
575         default:
576                 error("expected type specifier");
577         }
578 }
579
580 static void
581 unsigned_dec(char **typep)
582 {
583         token tok;
584
585         peek(&tok);
586         switch (tok.kind) {
587         case TOK_CHAR:
588                 get_token(&tok);
589                 *typep = "u_char";
590                 break;
591         case TOK_SHORT:
592                 get_token(&tok);
593                 *typep = "u_short";
594                 (void) peekscan(TOK_INT, &tok);
595                 break;
596         case TOK_INT32:
597                 get_token(&tok);
598                 *typep = "u_int32_";
599                 (void) peekscan(TOK_INT, &tok);
600                 break;
601         case TOK_INT:
602                 get_token(&tok);
603                 *typep = "u_int";
604                 break;
605         default:
606                 *typep = "u_int";
607                 break;
608         }
609 }