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.
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.
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.
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.
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.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
29 * @(#)rpc_parse.c 1.8 89/02/22 (C) 1987 SMI
32 #ident "@(#)rpc_parse.c 1.12 93/07/05 SMI"
35 * rpc_parse.c, Parser for the RPC protocol compiler
36 * Copyright (C) 1987 Sun Microsystems, Inc.
40 #include "rpc/types.h"
42 #include "rpc_parse.h"
47 extern char *make_argname __P(( char *, char * ));
48 static void isdefined __P(( definition * ));
49 static void def_struct __P(( definition * ));
50 static void def_program __P(( definition * ));
51 static void def_enum __P(( definition * ));
52 static void def_const __P(( definition * ));
53 static void def_union __P(( definition * ));
54 static void def_typedef __P(( definition * ));
55 static void get_declaration __P(( declaration *, defkind ));
56 static void get_prog_declaration __P(( declaration *, defkind, int ));
57 static void get_type __P(( char **, char **, defkind ));
58 static void unsigned_dec __P(( char ** ));
61 extern char *strdup();
65 * return the next definition you see
73 defp = ALLOC(definition);
97 error("definition keyword expected");
99 scan(TOK_SEMICOLON, &tok);
108 STOREVAL(&defined, defp);
120 defp->def_kind = DEF_STRUCT;
122 scan(TOK_IDENT, &tok);
123 defp->def_name = tok.str;
124 scan(TOK_LBRACE, &tok);
125 tailp = &defp->def.st.decls;
127 get_declaration(&dec, DEF_STRUCT);
128 decls = ALLOC(decl_list);
131 tailp = &decls->next;
132 scan(TOK_SEMICOLON, &tok);
134 } while (tok.kind != TOK_RBRACE);
148 version_list **vtailp;
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);
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;
167 /* get result type */
168 plist = ALLOC(proc_list);
169 get_type(&plist->res_prefix, &plist->res_type,
171 if (streq(plist->res_type, "opaque")) {
172 error("illegal result type");
174 scan(TOK_IDENT, &tok);
175 plist->proc_name = tok.str;
176 scan(TOK_LPAREN, &tok);
177 /* get args - first one */
181 * type of DEF_PROGRAM in the first
182 * get_prog_declaration and DEF_STURCT in the next
183 * allows void as argument if it is the only argument
185 get_prog_declaration(&dec, DEF_PROGRAM, num_args);
186 if (streq(dec.type, "void"))
188 decls = ALLOC(decl_list);
189 plist->args.decls = decls;
191 tailp = &decls->next;
193 while (peekscan(TOK_COMMA, &tok)) {
195 get_prog_declaration(&dec, DEF_STRUCT,
197 decls = ALLOC(decl_list);
200 if (streq(dec.type, "void"))
202 tailp = &decls->next;
204 /* multiple arguments are only allowed in newstyle */
205 if (!newstyle && num_args > 1) {
206 error("only one argument is allowed");
208 if (isvoid && num_args > 1) {
209 error("illegal use of void in program definition");
212 scan(TOK_RPAREN, &tok);
213 scan(TOK_EQUAL, &tok);
215 scan(TOK_SEMICOLON, &tok);
216 plist->proc_num = tok.str;
217 plist->arg_num = num_args;
219 ptailp = &plist->next;
221 } while (tok.kind != TOK_RBRACE);
224 vtailp = &vlist->next;
225 scan(TOK_RBRACE, &tok);
226 scan(TOK_EQUAL, &tok);
228 vlist->vers_num = tok.str;
229 /* make the argument structure name for each arg */
230 for (plist = vlist->procs; plist != NULL;
231 plist = plist->next) {
232 plist->args.argname = make_argname(plist->proc_name,
234 /* free the memory ?? */
236 scan(TOK_SEMICOLON, &tok);
237 scan2(TOK_VERSION, TOK_RBRACE, &tok);
238 } while (tok.kind == TOK_VERSION);
239 scan(TOK_EQUAL, &tok);
241 defp->def.pr.prog_num = tok.str;
252 enumval_list **tailp;
254 defp->def_kind = DEF_ENUM;
255 scan(TOK_IDENT, &tok);
256 defp->def_name = tok.str;
257 scan(TOK_LBRACE, &tok);
258 tailp = &defp->def.en.vals;
260 scan(TOK_IDENT, &tok);
261 elist = ALLOC(enumval_list);
262 elist->name = tok.str;
263 elist->assignment = NULL;
264 scan3(TOK_COMMA, TOK_RBRACE, TOK_EQUAL, &tok);
265 if (tok.kind == TOK_EQUAL) {
267 elist->assignment = tok.str;
268 scan2(TOK_COMMA, TOK_RBRACE, &tok);
271 tailp = &elist->next;
272 } while (tok.kind != TOK_RBRACE);
282 defp->def_kind = DEF_CONST;
283 scan(TOK_IDENT, &tok);
284 defp->def_name = tok.str;
285 scan(TOK_EQUAL, &tok);
286 scan2(TOK_IDENT, TOK_STRCONST, &tok);
287 defp->def.co = tok.str;
300 defp->def_kind = DEF_UNION;
301 scan(TOK_IDENT, &tok);
302 defp->def_name = tok.str;
303 scan(TOK_SWITCH, &tok);
304 scan(TOK_LPAREN, &tok);
305 get_declaration(&dec, DEF_UNION);
306 defp->def.un.enum_decl = dec;
307 tailp = &defp->def.un.cases;
308 scan(TOK_RPAREN, &tok);
309 scan(TOK_LBRACE, &tok);
310 scan(TOK_CASE, &tok);
311 while (tok.kind == TOK_CASE) {
312 scan2(TOK_IDENT, TOK_CHARCONST, &tok);
313 cases = ALLOC(case_list);
314 cases->case_name = tok.str;
315 scan(TOK_COLON, &tok);
316 /* now peek at next token */
318 if (peekscan(TOK_CASE, &tok)){
320 scan2(TOK_IDENT, TOK_CHARCONST, &tok);
322 /* continued case statement */
324 tailp = &cases->next;
325 cases = ALLOC(case_list);
326 cases->case_name = tok.str;
327 scan(TOK_COLON, &tok);
328 } while (peekscan(TOK_CASE, &tok));
335 tailp = &cases->next;
336 cases = ALLOC(case_list);
339 get_declaration(&dec, DEF_UNION);
340 cases->case_decl = dec;
341 cases->contflag = 0; /* no continued case statement */
343 tailp = &cases->next;
344 scan(TOK_SEMICOLON, &tok);
346 scan3(TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok);
349 if (tok.kind == TOK_DEFAULT) {
350 scan(TOK_COLON, &tok);
351 get_declaration(&dec, DEF_UNION);
352 defp->def.un.default_decl = ALLOC(declaration);
353 *defp->def.un.default_decl = dec;
354 scan(TOK_SEMICOLON, &tok);
355 scan(TOK_RBRACE, &tok);
357 defp->def.un.default_decl = NULL;
361 static char* reserved_words[] =
378 static char* reserved_types[] =
386 * check that the given name is not one that would eventually result in
387 * xdr routines that would conflict with internal XDR routines.
390 check_type_name(name, new_type)
397 for (i = 0; reserved_words[i] != NULL; i++) {
398 if (strcmp(name, reserved_words[i]) == 0) {
400 "illegal (reserved) name :\'%s\' in type definition",
406 for (i = 0; reserved_types[i] != NULL; i++) {
407 if (strcmp(name, reserved_types[i]) == 0) {
409 "illegal (reserved) name :\'%s\' in type definition",
425 defp->def_kind = DEF_TYPEDEF;
426 get_declaration(&dec, DEF_TYPEDEF);
427 defp->def_name = dec.name;
428 check_type_name(dec.name, 1);
429 defp->def.ty.old_prefix = dec.prefix;
430 defp->def.ty.old_type = dec.type;
431 defp->def.ty.rel = dec.rel;
432 defp->def.ty.array_max = dec.array_max;
436 get_declaration(dec, dkind)
442 get_type(&dec->prefix, &dec->type, dkind);
443 dec->rel = REL_ALIAS;
444 if (streq(dec->type, "void")) {
448 check_type_name(dec->type, 0);
449 scan2(TOK_STAR, TOK_IDENT, &tok);
450 if (tok.kind == TOK_STAR) {
451 dec->rel = REL_POINTER;
452 scan(TOK_IDENT, &tok);
455 if (peekscan(TOK_LBRACKET, &tok)) {
456 if (dec->rel == REL_POINTER) {
457 error("no array-of-pointer declarations -- use typedef");
459 dec->rel = REL_VECTOR;
461 dec->array_max = tok.str;
462 scan(TOK_RBRACKET, &tok);
463 } else if (peekscan(TOK_LANGLE, &tok)) {
464 if (dec->rel == REL_POINTER) {
465 error("no array-of-pointer declarations -- use typedef");
467 dec->rel = REL_ARRAY;
468 if (peekscan(TOK_RANGLE, &tok)) {
469 dec->array_max = "~0"; /* unspecified size, use max */
472 dec->array_max = tok.str;
473 scan(TOK_RANGLE, &tok);
476 if (streq(dec->type, "opaque")) {
477 if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR) {
478 error("array declaration expected");
480 } else if (streq(dec->type, "string")) {
481 if (dec->rel != REL_ARRAY) {
482 error("variable-length array declaration expected");
489 get_prog_declaration(dec, dkind, num)
492 int num; /* arg number */
495 char name[10]; /* argument name */
497 if (dkind == DEF_PROGRAM) {
499 if (tok.kind == TOK_RPAREN) { /* no arguments */
500 dec->rel = REL_ALIAS;
507 get_type(&dec->prefix, &dec->type, dkind);
508 dec->rel = REL_ALIAS;
509 if (peekscan(TOK_IDENT, &tok)) /* optional name of argument */
510 strcpy(name, tok.str);
512 sprintf(name, "%s%d", ARGNAME, num);
513 /* default name of argument */
515 dec->name = (char *) strdup(name);
516 if (streq(dec->type, "void")) {
520 if (streq(dec->type, "opaque")) {
521 error("opaque -- illegal argument type");
523 if (peekscan(TOK_STAR, &tok)) {
524 if (streq(dec->type, "string")) {
525 error("pointer to string not allowed in program arguments\n");
527 dec->rel = REL_POINTER;
528 if (peekscan(TOK_IDENT, &tok))
529 /* optional name of argument */
530 dec->name = strdup(tok.str);
532 if (peekscan(TOK_LANGLE, &tok)) {
533 if (!streq(dec->type, "string")) {
534 error("arrays cannot be declared as arguments to procedures -- use typedef");
536 dec->rel = REL_ARRAY;
537 if (peekscan(TOK_RANGLE, &tok)) {
538 dec->array_max = "~0";
539 /* unspecified size, use max */
542 dec->array_max = tok.str;
543 scan(TOK_RANGLE, &tok);
546 if (streq(dec->type, "string")) {
547 if (dec->rel != REL_ARRAY) {
549 * .x specifies just string as
553 dec->rel = REL_ARRAY;
554 dec->array_max = "~0"; /* unspecified size, use max */
562 get_type(prefixp, typep, dkind)
579 scan(TOK_IDENT, &tok);
587 (void) peekscan(TOK_INT, &tok);
591 (void) peekscan(TOK_INT, &tok);
595 (void) peekscan(TOK_INT, &tok);
599 if (dkind != DEF_UNION && dkind != DEF_PROGRAM) {
600 error("voids allowed only inside union and program definitions with one argument");
615 error("expected type specifier");
634 (void) peekscan(TOK_INT, &tok);
639 (void) peekscan(TOK_INT, &tok);
643 *typep = "u_int64_t";
644 (void) peekscan(TOK_INT, &tok);