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
30 * $DragonFly: src/usr.bin/rpcgen/rpc_parse.c,v 1.3 2003/11/03 19:14:35 eirikn Exp $
33 #ident "@(#)rpc_parse.c 1.12 93/07/05 SMI"
36 * rpc_parse.c, Parser for the RPC protocol compiler
37 * Copyright (C) 1987 Sun Microsystems, Inc.
41 #include "rpc/types.h"
43 #include "rpc_parse.h"
48 extern char *make_argname __P(( char *, char * ));
49 static void isdefined __P(( definition * ));
50 static void def_struct __P(( definition * ));
51 static void def_program __P(( definition * ));
52 static void def_enum __P(( definition * ));
53 static void def_const __P(( definition * ));
54 static void def_union __P(( definition * ));
55 static void def_typedef __P(( definition * ));
56 static void get_declaration __P(( declaration *, defkind ));
57 static void get_prog_declaration __P(( declaration *, defkind, int ));
58 static void get_type __P(( char **, char **, defkind ));
59 static void unsigned_dec __P(( char ** ));
62 extern char *strdup();
66 * return the next definition you see
74 defp = ALLOC(definition);
98 error("definition keyword expected");
100 scan(TOK_SEMICOLON, &tok);
109 STOREVAL(&defined, defp);
121 defp->def_kind = DEF_STRUCT;
123 scan(TOK_IDENT, &tok);
124 defp->def_name = tok.str;
125 scan(TOK_LBRACE, &tok);
126 tailp = &defp->def.st.decls;
128 get_declaration(&dec, DEF_STRUCT);
129 decls = ALLOC(decl_list);
132 tailp = &decls->next;
133 scan(TOK_SEMICOLON, &tok);
135 } while (tok.kind != TOK_RBRACE);
149 version_list **vtailp;
153 bool_t isvoid = FALSE; /* whether first argument is void */
154 defp->def_kind = DEF_PROGRAM;
155 scan(TOK_IDENT, &tok);
156 defp->def_name = tok.str;
157 scan(TOK_LBRACE, &tok);
158 vtailp = &defp->def.pr.versions;
159 tailp = &defp->def.st.decls;
160 scan(TOK_VERSION, &tok);
162 scan(TOK_IDENT, &tok);
163 vlist = ALLOC(version_list);
164 vlist->vers_name = tok.str;
165 scan(TOK_LBRACE, &tok);
166 ptailp = &vlist->procs;
168 /* get result type */
169 plist = ALLOC(proc_list);
170 get_type(&plist->res_prefix, &plist->res_type,
172 if (streq(plist->res_type, "opaque")) {
173 error("illegal result type");
175 scan(TOK_IDENT, &tok);
176 plist->proc_name = tok.str;
177 scan(TOK_LPAREN, &tok);
178 /* get args - first one */
182 * type of DEF_PROGRAM in the first
183 * get_prog_declaration and DEF_STURCT in the next
184 * allows void as argument if it is the only argument
186 get_prog_declaration(&dec, DEF_PROGRAM, num_args);
187 if (streq(dec.type, "void"))
189 decls = ALLOC(decl_list);
190 plist->args.decls = decls;
192 tailp = &decls->next;
194 while (peekscan(TOK_COMMA, &tok)) {
196 get_prog_declaration(&dec, DEF_STRUCT,
198 decls = ALLOC(decl_list);
201 if (streq(dec.type, "void"))
203 tailp = &decls->next;
205 /* multiple arguments are only allowed in newstyle */
206 if (!newstyle && num_args > 1) {
207 error("only one argument is allowed");
209 if (isvoid && num_args > 1) {
210 error("illegal use of void in program definition");
213 scan(TOK_RPAREN, &tok);
214 scan(TOK_EQUAL, &tok);
216 scan(TOK_SEMICOLON, &tok);
217 plist->proc_num = tok.str;
218 plist->arg_num = num_args;
220 ptailp = &plist->next;
222 } while (tok.kind != TOK_RBRACE);
225 vtailp = &vlist->next;
226 scan(TOK_RBRACE, &tok);
227 scan(TOK_EQUAL, &tok);
229 vlist->vers_num = tok.str;
230 /* make the argument structure name for each arg */
231 for (plist = vlist->procs; plist != NULL;
232 plist = plist->next) {
233 plist->args.argname = make_argname(plist->proc_name,
235 /* free the memory ?? */
237 scan(TOK_SEMICOLON, &tok);
238 scan2(TOK_VERSION, TOK_RBRACE, &tok);
239 } while (tok.kind == TOK_VERSION);
240 scan(TOK_EQUAL, &tok);
242 defp->def.pr.prog_num = tok.str;
253 enumval_list **tailp;
255 defp->def_kind = DEF_ENUM;
256 scan(TOK_IDENT, &tok);
257 defp->def_name = tok.str;
258 scan(TOK_LBRACE, &tok);
259 tailp = &defp->def.en.vals;
261 scan(TOK_IDENT, &tok);
262 elist = ALLOC(enumval_list);
263 elist->name = tok.str;
264 elist->assignment = NULL;
265 scan3(TOK_COMMA, TOK_RBRACE, TOK_EQUAL, &tok);
266 if (tok.kind == TOK_EQUAL) {
268 elist->assignment = tok.str;
269 scan2(TOK_COMMA, TOK_RBRACE, &tok);
272 tailp = &elist->next;
273 } while (tok.kind != TOK_RBRACE);
283 defp->def_kind = DEF_CONST;
284 scan(TOK_IDENT, &tok);
285 defp->def_name = tok.str;
286 scan(TOK_EQUAL, &tok);
287 scan2(TOK_IDENT, TOK_STRCONST, &tok);
288 defp->def.co = tok.str;
301 defp->def_kind = DEF_UNION;
302 scan(TOK_IDENT, &tok);
303 defp->def_name = tok.str;
304 scan(TOK_SWITCH, &tok);
305 scan(TOK_LPAREN, &tok);
306 get_declaration(&dec, DEF_UNION);
307 defp->def.un.enum_decl = dec;
308 tailp = &defp->def.un.cases;
309 scan(TOK_RPAREN, &tok);
310 scan(TOK_LBRACE, &tok);
311 scan(TOK_CASE, &tok);
312 while (tok.kind == TOK_CASE) {
313 scan2(TOK_IDENT, TOK_CHARCONST, &tok);
314 cases = ALLOC(case_list);
315 cases->case_name = tok.str;
316 scan(TOK_COLON, &tok);
317 /* now peek at next token */
319 if (peekscan(TOK_CASE, &tok)){
321 scan2(TOK_IDENT, TOK_CHARCONST, &tok);
323 /* continued case statement */
325 tailp = &cases->next;
326 cases = ALLOC(case_list);
327 cases->case_name = tok.str;
328 scan(TOK_COLON, &tok);
329 } while (peekscan(TOK_CASE, &tok));
336 tailp = &cases->next;
337 cases = ALLOC(case_list);
340 get_declaration(&dec, DEF_UNION);
341 cases->case_decl = dec;
342 cases->contflag = 0; /* no continued case statement */
344 tailp = &cases->next;
345 scan(TOK_SEMICOLON, &tok);
347 scan3(TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok);
350 if (tok.kind == TOK_DEFAULT) {
351 scan(TOK_COLON, &tok);
352 get_declaration(&dec, DEF_UNION);
353 defp->def.un.default_decl = ALLOC(declaration);
354 *defp->def.un.default_decl = dec;
355 scan(TOK_SEMICOLON, &tok);
356 scan(TOK_RBRACE, &tok);
358 defp->def.un.default_decl = NULL;
362 static char* reserved_words[] =
379 static char* reserved_types[] =
387 * check that the given name is not one that would eventually result in
388 * xdr routines that would conflict with internal XDR routines.
391 check_type_name(name, new_type)
398 for (i = 0; reserved_words[i] != NULL; i++) {
399 if (strcmp(name, reserved_words[i]) == 0) {
401 "illegal (reserved) name :\'%s\' in type definition",
407 for (i = 0; reserved_types[i] != NULL; i++) {
408 if (strcmp(name, reserved_types[i]) == 0) {
410 "illegal (reserved) name :\'%s\' in type definition",
426 defp->def_kind = DEF_TYPEDEF;
427 get_declaration(&dec, DEF_TYPEDEF);
428 defp->def_name = dec.name;
429 check_type_name(dec.name, 1);
430 defp->def.ty.old_prefix = dec.prefix;
431 defp->def.ty.old_type = dec.type;
432 defp->def.ty.rel = dec.rel;
433 defp->def.ty.array_max = dec.array_max;
437 get_declaration(dec, dkind)
443 get_type(&dec->prefix, &dec->type, dkind);
444 dec->rel = REL_ALIAS;
445 if (streq(dec->type, "void")) {
449 check_type_name(dec->type, 0);
450 scan2(TOK_STAR, TOK_IDENT, &tok);
451 if (tok.kind == TOK_STAR) {
452 dec->rel = REL_POINTER;
453 scan(TOK_IDENT, &tok);
456 if (peekscan(TOK_LBRACKET, &tok)) {
457 if (dec->rel == REL_POINTER) {
458 error("no array-of-pointer declarations -- use typedef");
460 dec->rel = REL_VECTOR;
462 dec->array_max = tok.str;
463 scan(TOK_RBRACKET, &tok);
464 } else if (peekscan(TOK_LANGLE, &tok)) {
465 if (dec->rel == REL_POINTER) {
466 error("no array-of-pointer declarations -- use typedef");
468 dec->rel = REL_ARRAY;
469 if (peekscan(TOK_RANGLE, &tok)) {
470 dec->array_max = "~0"; /* unspecified size, use max */
473 dec->array_max = tok.str;
474 scan(TOK_RANGLE, &tok);
477 if (streq(dec->type, "opaque")) {
478 if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR) {
479 error("array declaration expected");
481 } else if (streq(dec->type, "string")) {
482 if (dec->rel != REL_ARRAY) {
483 error("variable-length array declaration expected");
490 get_prog_declaration(dec, dkind, num)
493 int num; /* arg number */
496 char name[10]; /* argument name */
498 if (dkind == DEF_PROGRAM) {
500 if (tok.kind == TOK_RPAREN) { /* no arguments */
501 dec->rel = REL_ALIAS;
508 get_type(&dec->prefix, &dec->type, dkind);
509 dec->rel = REL_ALIAS;
510 if (peekscan(TOK_IDENT, &tok)) /* optional name of argument */
511 strcpy(name, tok.str);
513 sprintf(name, "%s%d", ARGNAME, num);
514 /* default name of argument */
516 dec->name = (char *) strdup(name);
517 if (streq(dec->type, "void")) {
521 if (streq(dec->type, "opaque")) {
522 error("opaque -- illegal argument type");
524 if (peekscan(TOK_STAR, &tok)) {
525 if (streq(dec->type, "string")) {
526 error("pointer to string not allowed in program arguments\n");
528 dec->rel = REL_POINTER;
529 if (peekscan(TOK_IDENT, &tok))
530 /* optional name of argument */
531 dec->name = strdup(tok.str);
533 if (peekscan(TOK_LANGLE, &tok)) {
534 if (!streq(dec->type, "string")) {
535 error("arrays cannot be declared as arguments to procedures -- use typedef");
537 dec->rel = REL_ARRAY;
538 if (peekscan(TOK_RANGLE, &tok)) {
539 dec->array_max = "~0";
540 /* unspecified size, use max */
543 dec->array_max = tok.str;
544 scan(TOK_RANGLE, &tok);
547 if (streq(dec->type, "string")) {
548 if (dec->rel != REL_ARRAY) {
550 * .x specifies just string as
554 dec->rel = REL_ARRAY;
555 dec->array_max = "~0"; /* unspecified size, use max */
563 get_type(prefixp, typep, dkind)
580 scan(TOK_IDENT, &tok);
588 (void) peekscan(TOK_INT, &tok);
592 (void) peekscan(TOK_INT, &tok);
596 (void) peekscan(TOK_INT, &tok);
600 if (dkind != DEF_UNION && dkind != DEF_PROGRAM) {
601 error("voids allowed only inside union and program definitions with one argument");
616 error("expected type specifier");
635 (void) peekscan(TOK_INT, &tok);
640 (void) peekscan(TOK_INT, &tok);
644 *typep = "u_int64_t";
645 (void) peekscan(TOK_INT, &tok);