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
30 #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";
37 * rpc_parse.c, Parser for the RPC protocol compiler
38 * Copyright (C) 1987 Sun Microsystems, Inc.
42 #include "rpc/types.h"
44 #include "rpc_parse.h"
49 extern char *make_argname __P(( char *, char * ));
50 static void isdefined __P(( definition * ));
51 static void def_struct __P(( definition * ));
52 static void def_program __P(( definition * ));
53 static void def_enum __P(( definition * ));
54 static void def_const __P(( definition * ));
55 static void def_union __P(( definition * ));
56 static void def_typedef __P(( definition * ));
57 static void get_declaration __P(( declaration *, defkind ));
58 static void get_prog_declaration __P(( declaration *, defkind, int ));
59 static void get_type __P(( char **, char **, defkind ));
60 static void unsigned_dec __P(( char ** ));
63 extern char *strdup();
67 * return the next definition you see
75 defp = ALLOC(definition);
99 error("definition keyword expected");
101 scan(TOK_SEMICOLON, &tok);
110 STOREVAL(&defined, defp);
122 defp->def_kind = DEF_STRUCT;
124 scan(TOK_IDENT, &tok);
125 defp->def_name = tok.str;
126 scan(TOK_LBRACE, &tok);
127 tailp = &defp->def.st.decls;
129 get_declaration(&dec, DEF_STRUCT);
130 decls = ALLOC(decl_list);
133 tailp = &decls->next;
134 scan(TOK_SEMICOLON, &tok);
136 } while (tok.kind != TOK_RBRACE);
150 version_list **vtailp;
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);
163 scan(TOK_IDENT, &tok);
164 vlist = ALLOC(version_list);
165 vlist->vers_name = tok.str;
166 scan(TOK_LBRACE, &tok);
167 ptailp = &vlist->procs;
169 /* get result type */
170 plist = ALLOC(proc_list);
171 get_type(&plist->res_prefix, &plist->res_type,
173 if (streq(plist->res_type, "opaque")) {
174 error("illegal result type");
176 scan(TOK_IDENT, &tok);
177 plist->proc_name = tok.str;
178 scan(TOK_LPAREN, &tok);
179 /* get args - first one */
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
187 get_prog_declaration(&dec, DEF_PROGRAM, num_args);
188 if (streq(dec.type, "void"))
190 decls = ALLOC(decl_list);
191 plist->args.decls = decls;
193 tailp = &decls->next;
195 while (peekscan(TOK_COMMA, &tok)) {
197 get_prog_declaration(&dec, DEF_STRUCT,
199 decls = ALLOC(decl_list);
202 if (streq(dec.type, "void"))
204 tailp = &decls->next;
206 /* multiple arguments are only allowed in newstyle */
207 if (!newstyle && num_args > 1) {
208 error("only one argument is allowed");
210 if (isvoid && num_args > 1) {
211 error("illegal use of void in program definition");
214 scan(TOK_RPAREN, &tok);
215 scan(TOK_EQUAL, &tok);
217 scan(TOK_SEMICOLON, &tok);
218 plist->proc_num = tok.str;
219 plist->arg_num = num_args;
221 ptailp = &plist->next;
223 } while (tok.kind != TOK_RBRACE);
226 vtailp = &vlist->next;
227 scan(TOK_RBRACE, &tok);
228 scan(TOK_EQUAL, &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,
236 /* free the memory ?? */
238 scan(TOK_SEMICOLON, &tok);
239 scan2(TOK_VERSION, TOK_RBRACE, &tok);
240 } while (tok.kind == TOK_VERSION);
241 scan(TOK_EQUAL, &tok);
243 defp->def.pr.prog_num = tok.str;
254 enumval_list **tailp;
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;
262 scan(TOK_IDENT, &tok);
263 elist = ALLOC(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) {
269 elist->assignment = tok.str;
270 scan2(TOK_COMMA, TOK_RBRACE, &tok);
273 tailp = &elist->next;
274 } while (tok.kind != TOK_RBRACE);
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;
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 = ALLOC(case_list);
316 cases->case_name = tok.str;
317 scan(TOK_COLON, &tok);
318 /* now peek at next token */
320 if (peekscan(TOK_CASE, &tok)){
322 scan2(TOK_IDENT, TOK_CHARCONST, &tok);
324 /* continued case statement */
326 tailp = &cases->next;
327 cases = ALLOC(case_list);
328 cases->case_name = tok.str;
329 scan(TOK_COLON, &tok);
330 } while (peekscan(TOK_CASE, &tok));
337 tailp = &cases->next;
338 cases = ALLOC(case_list);
341 get_declaration(&dec, DEF_UNION);
342 cases->case_decl = dec;
343 cases->contflag = 0; /* no continued case statement */
345 tailp = &cases->next;
346 scan(TOK_SEMICOLON, &tok);
348 scan3(TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok);
351 if (tok.kind == TOK_DEFAULT) {
352 scan(TOK_COLON, &tok);
353 get_declaration(&dec, DEF_UNION);
354 defp->def.un.default_decl = ALLOC(declaration);
355 *defp->def.un.default_decl = dec;
356 scan(TOK_SEMICOLON, &tok);
357 scan(TOK_RBRACE, &tok);
359 defp->def.un.default_decl = NULL;
363 static char* reserved_words[] =
380 static char* reserved_types[] =
388 * check that the given name is not one that would eventually result in
389 * xdr routines that would conflict with internal XDR routines.
392 check_type_name(name, new_type)
399 for (i = 0; reserved_words[i] != NULL; i++) {
400 if (strcmp(name, reserved_words[i]) == 0) {
402 "illegal (reserved) name :\'%s\' in type definition",
408 for (i = 0; reserved_types[i] != NULL; i++) {
409 if (strcmp(name, reserved_types[i]) == 0) {
411 "illegal (reserved) name :\'%s\' in type definition",
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;
438 get_declaration(dec, dkind)
444 get_type(&dec->prefix, &dec->type, dkind);
445 dec->rel = REL_ALIAS;
446 if (streq(dec->type, "void")) {
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);
457 if (peekscan(TOK_LBRACKET, &tok)) {
458 if (dec->rel == REL_POINTER) {
459 error("no array-of-pointer declarations -- use typedef");
461 dec->rel = REL_VECTOR;
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");
469 dec->rel = REL_ARRAY;
470 if (peekscan(TOK_RANGLE, &tok)) {
471 dec->array_max = "~0"; /* unspecified size, use max */
474 dec->array_max = tok.str;
475 scan(TOK_RANGLE, &tok);
478 if (streq(dec->type, "opaque")) {
479 if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR) {
480 error("array declaration expected");
482 } else if (streq(dec->type, "string")) {
483 if (dec->rel != REL_ARRAY) {
484 error("variable-length array declaration expected");
491 get_prog_declaration(dec, dkind, num)
494 int num; /* arg number */
497 char name[10]; /* argument name */
499 if (dkind == DEF_PROGRAM) {
501 if (tok.kind == TOK_RPAREN) { /* no arguments */
502 dec->rel = REL_ALIAS;
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);
514 sprintf(name, "%s%d", ARGNAME, num);
515 /* default name of argument */
517 dec->name = (char *) strdup(name);
518 if (streq(dec->type, "void")) {
522 if (streq(dec->type, "opaque")) {
523 error("opaque -- illegal argument type");
525 if (peekscan(TOK_STAR, &tok)) {
526 if (streq(dec->type, "string")) {
527 error("pointer to string not allowed in program arguments\n");
529 dec->rel = REL_POINTER;
530 if (peekscan(TOK_IDENT, &tok))
531 /* optional name of argument */
532 dec->name = strdup(tok.str);
534 if (peekscan(TOK_LANGLE, &tok)) {
535 if (!streq(dec->type, "string")) {
536 error("arrays cannot be declared as arguments to procedures -- use typedef");
538 dec->rel = REL_ARRAY;
539 if (peekscan(TOK_RANGLE, &tok)) {
540 dec->array_max = "~0";
541 /* unspecified size, use max */
544 dec->array_max = tok.str;
545 scan(TOK_RANGLE, &tok);
548 if (streq(dec->type, "string")) {
549 if (dec->rel != REL_ARRAY) {
551 * .x specifies just string as
555 dec->rel = REL_ARRAY;
556 dec->array_max = "~0"; /* unspecified size, use max */
564 get_type(prefixp, typep, dkind)
581 scan(TOK_IDENT, &tok);
589 (void) peekscan(TOK_INT, &tok);
593 (void) peekscan(TOK_INT, &tok);
597 (void) peekscan(TOK_INT, &tok);
601 if (dkind != DEF_UNION && dkind != DEF_PROGRAM) {
602 error("voids allowed only inside union and program definitions with one argument");
617 error("expected type specifier");
636 (void) peekscan(TOK_INT, &tok);
641 (void) peekscan(TOK_INT, &tok);
645 *typep = "u_int64_t";
646 (void) peekscan(TOK_INT, &tok);