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