]> git.decadent.org.uk Git - nfs-utils.git/blob - tools/rpcgen/rpc_parse.c
Fix a couple of problems that crept into mount
[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 #if 0
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                 free(defp);
98                 return (NULL);
99         default:
100                 error("definition keyword expected");
101         }
102         scan(TOK_SEMICOLON, &tok);
103         isdefined(defp);
104         return (defp);
105 }
106
107 static void
108 isdefined(definition *defp)
109 {
110         STOREVAL(&defined, defp);
111 }
112
113 static void
114 def_struct(definition *defp)
115 {
116         token tok;
117         declaration dec;
118         decl_list *decls;
119         decl_list **tailp;
120
121         defp->def_kind = DEF_STRUCT;
122
123         scan(TOK_IDENT, &tok);
124         defp->def_name = tok.str;
125         scan(TOK_LBRACE, &tok);
126         tailp = &defp->def.st.decls;
127         do {
128                 get_declaration(&dec, DEF_STRUCT);
129                 decls = ALLOC(decl_list);
130                 decls->decl = dec;
131                 *tailp = decls;
132                 tailp = &decls->next;
133                 scan(TOK_SEMICOLON, &tok);
134                 peek(&tok);
135         } while (tok.kind != TOK_RBRACE);
136         get_token(&tok);
137         *tailp = NULL;
138 }
139
140 static void
141 def_program(definition *defp)
142 {
143         token tok;
144         declaration dec;
145         decl_list *decls;
146         decl_list **tailp;
147         version_list *vlist;
148         version_list **vtailp;
149         proc_list *plist;
150         proc_list **ptailp;
151         int num_args;
152         bool_t isvoid = FALSE; /* whether first argument is void */
153         defp->def_kind = DEF_PROGRAM;
154         scan(TOK_IDENT, &tok);
155         defp->def_name = tok.str;
156         scan(TOK_LBRACE, &tok);
157         vtailp = &defp->def.pr.versions;
158         tailp = &defp->def.st.decls;
159         scan(TOK_VERSION, &tok);
160         do {
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                         /* get result type */
168                         plist = ALLOC(proc_list);
169                         get_type(&plist->res_prefix, &plist->res_type, 
170                                  DEF_PROGRAM);
171                         if (streq(plist->res_type, "opaque")) {
172                                 error("illegal result type");
173                         }
174                         scan(TOK_IDENT, &tok);
175                         plist->proc_name = tok.str;
176                         scan(TOK_LPAREN, &tok);
177                         /* get args - first one*/
178                         num_args = 1;
179                         isvoid = FALSE;
180                         /* type of DEF_PROGRAM in the first 
181                          * get_prog_declaration and DEF_STURCT in the next
182                          * allows void as argument if it is the only argument
183                          */
184                         get_prog_declaration(&dec, DEF_PROGRAM, num_args);
185                         if (streq(dec.type, "void"))
186                           isvoid = TRUE;
187                         decls = ALLOC(decl_list);
188                         plist->args.decls = decls;
189                         decls->decl = dec;
190                         tailp = &decls->next;
191                         /* get args */
192                         while(peekscan(TOK_COMMA, &tok)) {
193                           num_args++;
194                           get_prog_declaration(&dec, DEF_STRUCT, 
195                                                num_args);
196                           decls = ALLOC(decl_list);
197                           decls->decl = dec;
198                           *tailp = decls;
199                           if (streq(dec.type, "void"))
200                             isvoid = TRUE;
201                           tailp = &decls->next;
202                         }
203                         /* multiple arguments are only allowed in newstyle */
204                         if( !newstyle && num_args > 1 ) {
205                           error("only one argument is allowed" );
206                         }
207                         if (isvoid && num_args > 1) { 
208                           error("illegal use of void in program definition");
209                         }
210                         *tailp = NULL;
211                         scan(TOK_RPAREN, &tok);
212                         scan(TOK_EQUAL, &tok);
213                         scan_num(&tok);
214                         scan(TOK_SEMICOLON, &tok);
215                         plist->proc_num = tok.str;
216                         plist->arg_num = num_args;
217                         *ptailp = plist;
218                         ptailp = &plist->next;
219                         peek(&tok);
220                 } while (tok.kind != TOK_RBRACE);
221                 *ptailp = NULL;
222                 *vtailp = vlist;
223                 vtailp = &vlist->next;
224                 scan(TOK_RBRACE, &tok);
225                 scan(TOK_EQUAL, &tok);
226                 scan_num(&tok);
227                 vlist->vers_num = tok.str;
228                 /* make the argument structure name for each arg*/
229                 for(plist = vlist->procs; plist != NULL; 
230                     plist = plist->next) {
231                         plist->args.argname = make_argname(plist->proc_name,
232                                                            vlist->vers_num); 
233                         /* free the memory ??*/
234                 }
235                 scan(TOK_SEMICOLON, &tok);
236                 scan2(TOK_VERSION, TOK_RBRACE, &tok);
237         } while (tok.kind == TOK_VERSION);
238         scan(TOK_EQUAL, &tok);
239         scan_num(&tok);
240         defp->def.pr.prog_num = tok.str;
241         *vtailp = NULL;
242 }
243
244
245 static void
246 def_enum(definition *defp)
247 {
248         token tok;
249         enumval_list *elist;
250         enumval_list **tailp;
251
252         defp->def_kind = DEF_ENUM;
253         scan(TOK_IDENT, &tok);
254         defp->def_name = tok.str;
255         scan(TOK_LBRACE, &tok);
256         tailp = &defp->def.en.vals;
257         do {
258                 scan(TOK_IDENT, &tok);
259                 elist = ALLOC(enumval_list);
260                 elist->name = tok.str;
261                 elist->assignment = NULL;
262                 scan3(TOK_COMMA, TOK_RBRACE, TOK_EQUAL, &tok);
263                 if (tok.kind == TOK_EQUAL) {
264                         scan_num(&tok);
265                         elist->assignment = tok.str;
266                         scan2(TOK_COMMA, TOK_RBRACE, &tok);
267                 }
268                 *tailp = elist;
269                 tailp = &elist->next;
270         } while (tok.kind != TOK_RBRACE);
271         *tailp = NULL;
272 }
273
274 static void
275 def_const(definition *defp)
276 {
277         token tok;
278
279         defp->def_kind = DEF_CONST;
280         scan(TOK_IDENT, &tok);
281         defp->def_name = tok.str;
282         scan(TOK_EQUAL, &tok);
283         scan2(TOK_IDENT, TOK_STRCONST, &tok);
284         defp->def.co = tok.str;
285 }
286
287 static void
288 def_union(definition *defp)
289 {
290   token tok;
291   declaration dec;
292   case_list *cases;
293   case_list **tailp;
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     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
328     get_declaration(&dec, DEF_UNION);
329     cases->case_decl = dec;
330     cases->contflag=0;          /* no continued case statement */
331     *tailp = cases;
332     tailp = &cases->next;
333     scan(TOK_SEMICOLON, &tok);
334
335     scan3(TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok);
336   }
337   *tailp = NULL;
338   if (tok.kind == TOK_DEFAULT) {
339     scan(TOK_COLON, &tok);
340     get_declaration(&dec, DEF_UNION);
341     defp->def.un.default_decl = ALLOC(declaration);
342     *defp->def.un.default_decl = dec;
343     scan(TOK_SEMICOLON, &tok);
344     scan(TOK_RBRACE, &tok);
345   } else {
346     defp->def.un.default_decl = NULL;
347   }
348 }
349
350 static char* reserved_words[] =
351 {
352   "array",
353   "bytes",
354   "destroy",
355   "free",
356   "getpos",
357   "inline",
358   "pointer",
359   "reference",
360   "setpos",
361   "sizeof",
362   "union",
363   "vector",
364   NULL
365   };
366
367 static char* reserved_types[] =
368 {
369   "opaque",
370   "string",
371   NULL
372   };
373
374 /* check that the given name is not one that would eventually result in
375    xdr routines that would conflict with internal XDR routines. */
376 static void
377 check_type_name(char *name, int new_type)
378 {
379   int i;
380   char tmp[100];
381
382   for( i = 0; reserved_words[i] != NULL; i++ ) {
383     if( strcmp( name, reserved_words[i] ) == 0 ) {
384       sprintf(tmp, 
385               "illegal (reserved) name :\'%s\' in type definition", name );
386       error(tmp);
387     }
388   }
389   if( new_type ) {
390     for( i = 0; reserved_types[i] != NULL; i++ ) {
391       if( strcmp( name, reserved_types[i] ) == 0 ) {
392         sprintf(tmp, 
393                 "illegal (reserved) name :\'%s\' in type definition", name );
394         error(tmp);
395       }
396     }
397   }
398 }
399
400 static void
401 def_typedef(definition *defp)
402 {
403         declaration dec;
404
405         defp->def_kind = DEF_TYPEDEF;
406         get_declaration(&dec, DEF_TYPEDEF);
407         defp->def_name = dec.name;
408         check_type_name( dec.name, 1 );
409         defp->def.ty.old_prefix = dec.prefix;
410         defp->def.ty.old_type = dec.type;
411         defp->def.ty.rel = dec.rel;
412         defp->def.ty.array_max = dec.array_max;
413 }
414
415 static void
416 get_declaration(declaration *dec, defkind dkind)
417 {
418         token tok;
419
420         get_type(&dec->prefix, &dec->type, dkind);
421         dec->rel = REL_ALIAS;
422         if (streq(dec->type, "void")) {
423                 return;
424         }
425
426         check_type_name( dec->type, 0 );
427
428         scan2(TOK_STAR, TOK_IDENT, &tok);
429         if (tok.kind == TOK_STAR) {
430                 dec->rel = REL_POINTER;
431                 scan(TOK_IDENT, &tok);
432         }
433         dec->name = tok.str;
434         if (peekscan(TOK_LBRACKET, &tok)) {
435                 if (dec->rel == REL_POINTER) {
436                         error("no array-of-pointer declarations -- use typedef");
437                 }
438                 dec->rel = REL_VECTOR;
439                 scan_num(&tok);
440                 dec->array_max = tok.str;
441                 scan(TOK_RBRACKET, &tok);
442         } else if (peekscan(TOK_LANGLE, &tok)) {
443                 if (dec->rel == REL_POINTER) {
444                         error("no array-of-pointer declarations -- use typedef");
445                 }
446                 dec->rel = REL_ARRAY;
447                 if (peekscan(TOK_RANGLE, &tok)) {
448                         dec->array_max = "~0";  /* unspecified size, use max */
449                 } else {
450                         scan_num(&tok);
451                         dec->array_max = tok.str;
452                         scan(TOK_RANGLE, &tok);
453                 }
454         }
455         if (streq(dec->type, "opaque")) {
456                 if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR) {
457                         error("array declaration expected");
458                 }
459         } else if (streq(dec->type, "string")) {
460                 if (dec->rel != REL_ARRAY) {
461                         error("variable-length array declaration expected");
462                 }
463         }
464 }
465
466
467 static void
468 get_prog_declaration(declaration *dec, defkind dkind, int num)
469 {
470         token tok;
471         char name[10]; /* argument name */
472
473         if (dkind == DEF_PROGRAM) { 
474           peek(&tok);
475           if (tok.kind == TOK_RPAREN) { /* no arguments */
476                 dec->rel = REL_ALIAS;
477                 dec->type = "void";
478                 dec->prefix = NULL;
479                 dec->name = NULL;
480                 return;
481               }
482         }
483         get_type(&dec->prefix, &dec->type, dkind);
484         dec->rel = REL_ALIAS;
485         if (peekscan(TOK_IDENT, &tok))  /* optional name of argument */
486                 strcpy(name, tok.str);
487         else 
488                 sprintf(name, "%s%d", ARGNAME, num); /* default name of argument */
489
490         dec->name = (char *) strdup(name); 
491         
492         if (streq(dec->type, "void")) {
493                 return;
494         }
495
496         if (streq(dec->type, "opaque")) {
497                 error("opaque -- illegal argument type");
498         }
499         if (peekscan(TOK_STAR, &tok)) { 
500           if (streq(dec->type, "string")) {
501             error("pointer to string not allowed in program arguments\n");
502           }
503                 dec->rel = REL_POINTER;
504                 if (peekscan(TOK_IDENT, &tok))  /* optional name of argument */
505                   dec->name = strdup(tok.str);
506       }
507           if (peekscan(TOK_LANGLE, &tok)) {
508             if (!streq(dec->type, "string")) {
509               error("arrays cannot be declared as arguments to procedures -- use typedef");
510             }
511                 dec->rel = REL_ARRAY;
512                 if (peekscan(TOK_RANGLE, &tok)) {
513                         dec->array_max = "~0";/* unspecified size, use max */
514                 } else {
515                         scan_num(&tok);
516                         dec->array_max = tok.str;
517                         scan(TOK_RANGLE, &tok);
518                 }
519         }
520         if (streq(dec->type, "string")) {
521                 if (dec->rel != REL_ARRAY) {  /* .x specifies just string as
522                                                * type of argument 
523                                                * - make it string<>
524                                                */
525                         dec->rel = REL_ARRAY;
526                         dec->array_max = "~0";/* unspecified size, use max */
527                 }
528         }
529 }
530
531
532
533 static void
534 get_type(char **prefixp, char **typep, defkind dkind)
535 {
536         token tok;
537
538         *prefixp = NULL;
539         get_token(&tok);
540         switch (tok.kind) {
541         case TOK_IDENT:
542                 *typep = tok.str;
543                 break;
544         case TOK_STRUCT:
545         case TOK_ENUM:
546         case TOK_UNION:
547                 *prefixp = tok.str;
548                 scan(TOK_IDENT, &tok);
549                 *typep = tok.str;
550                 break;
551         case TOK_UNSIGNED:
552                 unsigned_dec(typep);
553                 break;
554         case TOK_SHORT:
555                 *typep = "short";
556                 (void) peekscan(TOK_INT, &tok);
557                 break;
558         case TOK_INT32:
559                 *typep = "int32_t";
560                 (void) peekscan(TOK_INT, &tok);
561                 break;
562         case TOK_VOID:
563                 if (dkind != DEF_UNION && dkind != DEF_PROGRAM) {
564                         error("voids allowed only inside union and program definitions with one argument");
565                 }
566                 *typep = tok.str;
567                 break;
568         case TOK_STRING:
569         case TOK_OPAQUE:
570         case TOK_CHAR:
571         case TOK_INT:
572         case TOK_FLOAT:
573         case TOK_DOUBLE:
574         case TOK_BOOL:
575                 *typep = tok.str;
576                 break;
577         default:
578                 error("expected type specifier");
579         }
580 }
581
582 static void
583 unsigned_dec(char **typep)
584 {
585         token tok;
586
587         peek(&tok);
588         switch (tok.kind) {
589         case TOK_CHAR:
590                 get_token(&tok);
591                 *typep = "u_char";
592                 break;
593         case TOK_SHORT:
594                 get_token(&tok);
595                 *typep = "u_short";
596                 (void) peekscan(TOK_INT, &tok);
597                 break;
598         case TOK_INT32:
599                 get_token(&tok);
600                 *typep = "u_int32_";
601                 (void) peekscan(TOK_INT, &tok);
602                 break;
603         case TOK_INT:
604                 get_token(&tok);
605                 *typep = "u_int";
606                 break;
607         default:
608                 *typep = "u_int";
609                 break;
610         }
611 }