]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - 6/usr.bin/rpcgen/rpc_parse.c
merge fix for boot-time hang on centos' xen
[FreeBSD/FreeBSD.git] / 6 / usr.bin / 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.
8  *
9  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12  *
13  * Sun RPC is provided with no support and without any obligation on the
14  * part of Sun Microsystems, Inc. to assist in its use, correction,
15  * modification or enhancement.
16  *
17  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19  * OR ANY PART THEREOF.
20  *
21  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22  * or profits or other special, indirect and consequential damages, even if
23  * Sun has been advised of the possibility of such damages.
24  *
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  */
29
30 #if 0
31 #ifndef lint
32 #ident  "@(#)rpc_parse.c        1.12    93/07/05 SMI"
33 static char sccsid[] = "@(#)rpc_parse.c 1.8 89/02/22 (C) 1987 SMI";
34 #endif
35 #endif
36
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39
40 /*
41  * rpc_parse.c, Parser for the RPC protocol compiler
42  * Copyright (C) 1987 Sun Microsystems, Inc.
43  */
44 #include <stdio.h>
45 #include <string.h>
46 #include "rpc/types.h"
47 #include "rpc_scan.h"
48 #include "rpc_parse.h"
49 #include "rpc_util.h"
50
51 #define ARGNAME "arg"
52
53 extern char *make_argname( char *, char * );
54 static void isdefined( definition * );
55 static void def_struct( definition * );
56 static void def_program( definition * );
57 static void def_enum( definition * );
58 static void def_const( definition * );
59 static void def_union( definition * );
60 static void def_typedef( definition * );
61 static void get_declaration( declaration *, defkind );
62 static void get_prog_declaration( declaration *, defkind, int );
63 static void get_type( char **, char **, defkind );
64 static void unsigned_dec( char ** );
65
66 /*
67  * return the next definition you see
68  */
69 definition *
70 get_definition()
71 {
72         definition *defp;
73         token tok;
74
75         defp = XALLOC(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(defp)
108         definition *defp;
109 {
110         STOREVAL(&defined, defp);
111 }
112
113 static void
114 def_struct(defp)
115         definition *defp;
116 {
117         token tok;
118         declaration dec;
119         decl_list *decls;
120         decl_list **tailp;
121
122         defp->def_kind = DEF_STRUCT;
123
124         scan(TOK_IDENT, &tok);
125         defp->def_name = tok.str;
126         scan(TOK_LBRACE, &tok);
127         tailp = &defp->def.st.decls;
128         do {
129                 get_declaration(&dec, DEF_STRUCT);
130                 decls = XALLOC(decl_list);
131                 decls->decl = dec;
132                 *tailp = decls;
133                 tailp = &decls->next;
134                 scan(TOK_SEMICOLON, &tok);
135                 peek(&tok);
136         } while (tok.kind != TOK_RBRACE);
137         get_token(&tok);
138         *tailp = NULL;
139 }
140
141 static void
142 def_program(defp)
143         definition *defp;
144 {
145         token tok;
146         declaration dec;
147         decl_list *decls;
148         decl_list **tailp;
149         version_list *vlist;
150         version_list **vtailp;
151         proc_list *plist;
152         proc_list **ptailp;
153         int num_args;
154         bool_t isvoid = FALSE;  /* whether first argument is void */
155         defp->def_kind = DEF_PROGRAM;
156         scan(TOK_IDENT, &tok);
157         defp->def_name = tok.str;
158         scan(TOK_LBRACE, &tok);
159         vtailp = &defp->def.pr.versions;
160         tailp = &defp->def.st.decls;
161         scan(TOK_VERSION, &tok);
162         do {
163                 scan(TOK_IDENT, &tok);
164                 vlist = XALLOC(version_list);
165                 vlist->vers_name = tok.str;
166                 scan(TOK_LBRACE, &tok);
167                 ptailp = &vlist->procs;
168                 do {
169                         /* get result type */
170                         plist = XALLOC(proc_list);
171                         get_type(&plist->res_prefix, &plist->res_type,
172                                  DEF_PROGRAM);
173                         if (streq(plist->res_type, "opaque")) {
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                         /*
183                          * type of DEF_PROGRAM in the first
184                          * get_prog_declaration and DEF_STURCT in the next
185                          * allows void as argument if it is the only argument
186                          */
187                         get_prog_declaration(&dec, DEF_PROGRAM, num_args);
188                         if (streq(dec.type, "void"))
189                                 isvoid = TRUE;
190                         decls = XALLOC(decl_list);
191                         plist->args.decls = decls;
192                         decls->decl = dec;
193                         tailp = &decls->next;
194                         /* get args */
195                         while (peekscan(TOK_COMMA, &tok)) {
196                                 num_args++;
197                                 get_prog_declaration(&dec, DEF_STRUCT,
198                                                      num_args);
199                                 decls = XALLOC(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                                 error("only one argument is allowed");
209                         }
210                         if (isvoid && num_args > 1) {
211                                 error("illegal use of void in program definition");
212                         }
213                         *tailp = NULL;
214                         scan(TOK_RPAREN, &tok);
215                         scan(TOK_EQUAL, &tok);
216                         scan_num(&tok);
217                         scan(TOK_SEMICOLON, &tok);
218                         plist->proc_num = tok.str;
219                         plist->arg_num = num_args;
220                         *ptailp = plist;
221                         ptailp = &plist->next;
222                         peek(&tok);
223                 } while (tok.kind != TOK_RBRACE);
224                 *ptailp = NULL;
225                 *vtailp = vlist;
226                 vtailp = &vlist->next;
227                 scan(TOK_RBRACE, &tok);
228                 scan(TOK_EQUAL, &tok);
229                 scan_num(&tok);
230                 vlist->vers_num = tok.str;
231                 /* make the argument structure name for each arg */
232                 for (plist = vlist->procs; plist != NULL;
233                      plist = plist->next) {
234                         plist->args.argname = make_argname(plist->proc_name,
235                                                            vlist->vers_num);
236                         /* free the memory ?? */
237                 }
238                 scan(TOK_SEMICOLON, &tok);
239                 scan2(TOK_VERSION, TOK_RBRACE, &tok);
240         } while (tok.kind == TOK_VERSION);
241         scan(TOK_EQUAL, &tok);
242         scan_num(&tok);
243         defp->def.pr.prog_num = tok.str;
244         *vtailp = NULL;
245 }
246
247
248 static void
249 def_enum(defp)
250         definition *defp;
251 {
252         token tok;
253         enumval_list *elist;
254         enumval_list **tailp;
255
256         defp->def_kind = DEF_ENUM;
257         scan(TOK_IDENT, &tok);
258         defp->def_name = tok.str;
259         scan(TOK_LBRACE, &tok);
260         tailp = &defp->def.en.vals;
261         do {
262                 scan(TOK_IDENT, &tok);
263                 elist = XALLOC(enumval_list);
264                 elist->name = tok.str;
265                 elist->assignment = NULL;
266                 scan3(TOK_COMMA, TOK_RBRACE, TOK_EQUAL, &tok);
267                 if (tok.kind == TOK_EQUAL) {
268                         scan_num(&tok);
269                         elist->assignment = tok.str;
270                         scan2(TOK_COMMA, TOK_RBRACE, &tok);
271                 }
272                 *tailp = elist;
273                 tailp = &elist->next;
274         } while (tok.kind != TOK_RBRACE);
275         *tailp = NULL;
276 }
277
278 static void
279 def_const(defp)
280         definition *defp;
281 {
282         token tok;
283
284         defp->def_kind = DEF_CONST;
285         scan(TOK_IDENT, &tok);
286         defp->def_name = tok.str;
287         scan(TOK_EQUAL, &tok);
288         scan2(TOK_IDENT, TOK_STRCONST, &tok);
289         defp->def.co = tok.str;
290 }
291
292 static void
293 def_union(defp)
294         definition *defp;
295 {
296         token tok;
297         declaration dec;
298         case_list *cases;
299         case_list **tailp;
300         int flag;
301
302         defp->def_kind = DEF_UNION;
303         scan(TOK_IDENT, &tok);
304         defp->def_name = tok.str;
305         scan(TOK_SWITCH, &tok);
306         scan(TOK_LPAREN, &tok);
307         get_declaration(&dec, DEF_UNION);
308         defp->def.un.enum_decl = dec;
309         tailp = &defp->def.un.cases;
310         scan(TOK_RPAREN, &tok);
311         scan(TOK_LBRACE, &tok);
312         scan(TOK_CASE, &tok);
313         while (tok.kind == TOK_CASE) {
314                 scan2(TOK_IDENT, TOK_CHARCONST, &tok);
315                 cases = XALLOC(case_list);
316                 cases->case_name = tok.str;
317                 scan(TOK_COLON, &tok);
318                 /* now peek at next token */
319                 flag = 0;
320                 if (peekscan(TOK_CASE, &tok)){
321                         do {
322                                 scan2(TOK_IDENT, TOK_CHARCONST, &tok);
323                                 cases->contflag = 1;
324                                 /* continued case statement */
325                                 *tailp = cases;
326                                 tailp = &cases->next;
327                                 cases = XALLOC(case_list);
328                                 cases->case_name = tok.str;
329                                 scan(TOK_COLON, &tok);
330                         } while (peekscan(TOK_CASE, &tok));
331                 }
332                 else
333                         if (flag)
334                         {
335
336                                 *tailp = cases;
337                                 tailp = &cases->next;
338                                 cases = XALLOC(case_list);
339                         };
340
341                 get_declaration(&dec, DEF_UNION);
342                 cases->case_decl = dec;
343                 cases->contflag = 0; /* no continued case statement */
344                 *tailp = cases;
345                 tailp = &cases->next;
346                 scan(TOK_SEMICOLON, &tok);
347
348                 scan3(TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok);
349         }
350         *tailp = NULL;
351         if (tok.kind == TOK_DEFAULT) {
352                 scan(TOK_COLON, &tok);
353                 get_declaration(&dec, DEF_UNION);
354                 defp->def.un.default_decl = XALLOC(declaration);
355                 *defp->def.un.default_decl = dec;
356                 scan(TOK_SEMICOLON, &tok);
357                 scan(TOK_RBRACE, &tok);
358         } else {
359                 defp->def.un.default_decl = NULL;
360         }
361 }
362
363 static char* reserved_words[] =
364 {
365         "array",
366         "bytes",
367         "destroy",
368         "free",
369         "getpos",
370         "inline",
371         "pointer",
372         "reference",
373         "setpos",
374         "sizeof",
375         "union",
376         "vector",
377         NULL
378         };
379
380 static char* reserved_types[] =
381 {
382         "opaque",
383         "string",
384         NULL
385         };
386
387 /*
388  * check that the given name is not one that would eventually result in
389  * xdr routines that would conflict with internal XDR routines.
390  */
391 static void
392 check_type_name(name, new_type)
393 int new_type;
394 char* name;
395 {
396         int i;
397         char tmp[100];
398
399         for (i = 0; reserved_words[i] != NULL; i++) {
400                 if (strcmp(name, reserved_words[i]) == 0) {
401                         sprintf(tmp,
402                                 "illegal (reserved) name :\'%s\' in type definition",
403                                 name);
404                         error(tmp);
405                 }
406         }
407         if (new_type) {
408                 for (i = 0; reserved_types[i] != NULL; i++) {
409                         if (strcmp(name, reserved_types[i]) == 0) {
410                                 sprintf(tmp,
411                                         "illegal (reserved) name :\'%s\' in type definition",
412                                         name);
413                                 error(tmp);
414                         }
415                 }
416         }
417 }
418
419
420
421 static void
422 def_typedef(defp)
423         definition *defp;
424 {
425         declaration dec;
426
427         defp->def_kind = DEF_TYPEDEF;
428         get_declaration(&dec, DEF_TYPEDEF);
429         defp->def_name = dec.name;
430         check_type_name(dec.name, 1);
431         defp->def.ty.old_prefix = dec.prefix;
432         defp->def.ty.old_type = dec.type;
433         defp->def.ty.rel = dec.rel;
434         defp->def.ty.array_max = dec.array_max;
435 }
436
437 static void
438 get_declaration(dec, dkind)
439         declaration *dec;
440         defkind dkind;
441 {
442         token tok;
443
444         get_type(&dec->prefix, &dec->type, dkind);
445         dec->rel = REL_ALIAS;
446         if (streq(dec->type, "void")) {
447                 return;
448         }
449
450         check_type_name(dec->type, 0);
451         scan2(TOK_STAR, TOK_IDENT, &tok);
452         if (tok.kind == TOK_STAR) {
453                 dec->rel = REL_POINTER;
454                 scan(TOK_IDENT, &tok);
455         }
456         dec->name = tok.str;
457         if (peekscan(TOK_LBRACKET, &tok)) {
458                 if (dec->rel == REL_POINTER) {
459                         error("no array-of-pointer declarations -- use typedef");
460                 }
461                 dec->rel = REL_VECTOR;
462                 scan_num(&tok);
463                 dec->array_max = tok.str;
464                 scan(TOK_RBRACKET, &tok);
465         } else if (peekscan(TOK_LANGLE, &tok)) {
466                 if (dec->rel == REL_POINTER) {
467                         error("no array-of-pointer declarations -- use typedef");
468                 }
469                 dec->rel = REL_ARRAY;
470                 if (peekscan(TOK_RANGLE, &tok)) {
471                         dec->array_max = "~0";  /* unspecified size, use max */
472                 } else {
473                         scan_num(&tok);
474                         dec->array_max = tok.str;
475                         scan(TOK_RANGLE, &tok);
476                 }
477         }
478         if (streq(dec->type, "opaque")) {
479                 if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR) {
480                         error("array declaration expected");
481                 }
482         } else if (streq(dec->type, "string")) {
483                 if (dec->rel != REL_ARRAY) {
484                         error("variable-length array declaration expected");
485                 }
486         }
487 }
488
489
490 static void
491 get_prog_declaration(dec, dkind, num)
492         declaration *dec;
493         defkind dkind;
494         int num;  /* arg number */
495 {
496         token tok;
497         char name[10];          /* argument name */
498
499         if (dkind == DEF_PROGRAM) {
500                 peek(&tok);
501                 if (tok.kind == TOK_RPAREN) { /* no arguments */
502                         dec->rel = REL_ALIAS;
503                         dec->type = "void";
504                         dec->prefix = NULL;
505                         dec->name = NULL;
506                         return;
507                 }
508         }
509         get_type(&dec->prefix, &dec->type, dkind);
510         dec->rel = REL_ALIAS;
511         if (peekscan(TOK_IDENT, &tok)) /* optional name of argument */
512                 strcpy(name, tok.str);
513         else
514                 sprintf(name, "%s%d", ARGNAME, num);
515         /* default name of argument */
516
517         dec->name = (char *) xstrdup(name);
518         if (streq(dec->type, "void")) {
519                 return;
520         }
521
522         if (streq(dec->type, "opaque")) {
523                 error("opaque -- illegal argument type");
524         }
525         if (peekscan(TOK_STAR, &tok)) {
526                 if (streq(dec->type, "string")) {
527                         error("pointer to string not allowed in program arguments");
528                 }
529                 dec->rel = REL_POINTER;
530                 if (peekscan(TOK_IDENT, &tok)) {
531                         /* optional name of argument */
532                         dec->name = xstrdup(tok.str);
533                 }
534         }
535         if (peekscan(TOK_LANGLE, &tok)) {
536                 if (!streq(dec->type, "string")) {
537                         error("arrays cannot be declared as arguments to procedures -- use typedef");
538                 }
539                 dec->rel = REL_ARRAY;
540                 if (peekscan(TOK_RANGLE, &tok)) {
541                         dec->array_max = "~0";
542                         /* unspecified size, use max */
543                 } else {
544                         scan_num(&tok);
545                         dec->array_max = tok.str;
546                         scan(TOK_RANGLE, &tok);
547                 }
548         }
549         if (streq(dec->type, "string")) {
550                 if (dec->rel != REL_ARRAY) {
551                         /*
552                          * .x specifies just string as
553                          * type of argument
554                          * - make it string<>
555                          */
556                         dec->rel = REL_ARRAY;
557                         dec->array_max = "~0"; /* unspecified size, use max */
558                 }
559         }
560 }
561
562
563
564 static void
565 get_type(prefixp, typep, dkind)
566         char **prefixp;
567         char **typep;
568         defkind dkind;
569 {
570         token tok;
571
572         *prefixp = NULL;
573         get_token(&tok);
574         switch (tok.kind) {
575         case TOK_IDENT:
576                 *typep = tok.str;
577                 break;
578         case TOK_STRUCT:
579         case TOK_ENUM:
580         case TOK_UNION:
581                 *prefixp = tok.str;
582                 scan(TOK_IDENT, &tok);
583                 *typep = tok.str;
584                 break;
585         case TOK_UNSIGNED:
586                 unsigned_dec(typep);
587                 break;
588         case TOK_SHORT:
589                 *typep = "short";
590                 (void) peekscan(TOK_INT, &tok);
591                 break;
592         case TOK_LONG:
593                 *typep = "long";
594                 (void) peekscan(TOK_INT, &tok);
595                 break;
596         case TOK_HYPER:
597                 *typep = "int64_t";
598                 (void) peekscan(TOK_INT, &tok);
599                 break;
600
601         case TOK_VOID:
602                 if (dkind != DEF_UNION && dkind != DEF_PROGRAM) {
603                         error("voids allowed only inside union and program definitions with one argument");
604                 }
605                 *typep = tok.str;
606                 break;
607         case TOK_STRING:
608         case TOK_OPAQUE:
609         case TOK_CHAR:
610         case TOK_INT:
611         case TOK_FLOAT:
612         case TOK_DOUBLE:
613         case TOK_BOOL:
614         case TOK_QUAD:
615                 *typep = tok.str;
616                 break;
617         default:
618                 error("expected type specifier");
619         }
620 }
621
622 static void
623 unsigned_dec(typep)
624         char **typep;
625 {
626         token tok;
627
628         peek(&tok);
629         switch (tok.kind) {
630         case TOK_CHAR:
631                 get_token(&tok);
632                 *typep = "u_char";
633                 break;
634         case TOK_SHORT:
635                 get_token(&tok);
636                 *typep = "u_short";
637                 (void) peekscan(TOK_INT, &tok);
638                 break;
639         case TOK_LONG:
640                 get_token(&tok);
641                 *typep = "u_long";
642                 (void) peekscan(TOK_INT, &tok);
643                 break;
644         case TOK_HYPER:
645                 get_token(&tok);
646                 *typep = "u_int64_t";
647
648                 (void) peekscan(TOK_INT, &tok);
649                 break;
650         case TOK_INT:
651                 get_token(&tok);
652                 *typep = "u_int";
653                 break;
654         default:
655                 *typep = "u_int";
656                 break;
657         }
658 }