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