10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*9497SJordan.Brown@Sun.COM * Common Development and Distribution License (the "License").
6*9497SJordan.Brown@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21132Srobinson
220Sstevel@tonic-gate /*
23*9497SJordan.Brown@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
270Sstevel@tonic-gate /* All Rights Reserved */
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
300Sstevel@tonic-gate * The Regents of the University of California
310Sstevel@tonic-gate * All Rights Reserved
320Sstevel@tonic-gate *
330Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
340Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
350Sstevel@tonic-gate * contributors.
360Sstevel@tonic-gate */
370Sstevel@tonic-gate
380Sstevel@tonic-gate /*
390Sstevel@tonic-gate * rpc_parse.c, Parser for the RPC protocol compiler
400Sstevel@tonic-gate */
410Sstevel@tonic-gate #include <stdio.h>
42132Srobinson #include <stdlib.h>
430Sstevel@tonic-gate #include <string.h>
440Sstevel@tonic-gate #include "rpc/types.h"
450Sstevel@tonic-gate #include "rpc_scan.h"
460Sstevel@tonic-gate #include "rpc_parse.h"
470Sstevel@tonic-gate #include "rpc_util.h"
480Sstevel@tonic-gate
490Sstevel@tonic-gate #define ARGNAME "arg"
500Sstevel@tonic-gate
51132Srobinson extern char *make_argname(char *, char *);
52132Srobinson
53132Srobinson static void isdefined(definition *);
54132Srobinson static void def_struct(definition *);
55132Srobinson static void def_program(definition *);
56132Srobinson static void def_enum(definition *);
57132Srobinson static void def_const(definition *);
58132Srobinson static void def_union(definition *);
59132Srobinson static void def_typedef(definition *);
60132Srobinson static void get_declaration(declaration *, defkind);
61132Srobinson static void get_prog_declaration(declaration *, defkind, int);
62132Srobinson static void get_type(char **, char **, defkind);
63132Srobinson static void unsigned_dec(char **);
640Sstevel@tonic-gate
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate * return the next definition you see
670Sstevel@tonic-gate */
680Sstevel@tonic-gate definition *
get_definition(void)69132Srobinson get_definition(void)
700Sstevel@tonic-gate {
710Sstevel@tonic-gate definition *defp;
720Sstevel@tonic-gate token tok;
730Sstevel@tonic-gate
74132Srobinson defp = calloc(1, sizeof (definition));
750Sstevel@tonic-gate get_token(&tok);
760Sstevel@tonic-gate switch (tok.kind) {
770Sstevel@tonic-gate case TOK_STRUCT:
780Sstevel@tonic-gate def_struct(defp);
790Sstevel@tonic-gate break;
800Sstevel@tonic-gate case TOK_UNION:
810Sstevel@tonic-gate def_union(defp);
820Sstevel@tonic-gate break;
830Sstevel@tonic-gate case TOK_TYPEDEF:
840Sstevel@tonic-gate def_typedef(defp);
850Sstevel@tonic-gate break;
860Sstevel@tonic-gate case TOK_ENUM:
870Sstevel@tonic-gate def_enum(defp);
880Sstevel@tonic-gate break;
890Sstevel@tonic-gate case TOK_PROGRAM:
900Sstevel@tonic-gate def_program(defp);
910Sstevel@tonic-gate break;
920Sstevel@tonic-gate case TOK_CONST:
930Sstevel@tonic-gate def_const(defp);
940Sstevel@tonic-gate break;
950Sstevel@tonic-gate case TOK_EOF:
960Sstevel@tonic-gate return (NULL);
970Sstevel@tonic-gate default:
980Sstevel@tonic-gate error("definition keyword expected");
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate scan(TOK_SEMICOLON, &tok);
1010Sstevel@tonic-gate isdefined(defp);
1020Sstevel@tonic-gate return (defp);
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate
105132Srobinson static void
isdefined(definition * defp)106132Srobinson isdefined(definition *defp)
1070Sstevel@tonic-gate {
1080Sstevel@tonic-gate STOREVAL(&defined, defp);
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate /*
1120Sstevel@tonic-gate * We treat s == NULL the same as *s == '\0'
1130Sstevel@tonic-gate */
114132Srobinson static int
streqn(const char * s1,const char * s2)1150Sstevel@tonic-gate streqn(const char *s1, const char *s2)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate if (s1 == NULL)
1180Sstevel@tonic-gate s1 = "";
1190Sstevel@tonic-gate if (s2 == NULL)
1200Sstevel@tonic-gate s2 = "";
1210Sstevel@tonic-gate if (s1 == s2)
1220Sstevel@tonic-gate return (1);
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate return (strcmp(s1, s2) == 0);
1250Sstevel@tonic-gate }
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate static int
cmptype(definition * defp,char * type)1280Sstevel@tonic-gate cmptype(definition *defp, char *type)
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate /* We only want typedef definitions */
1310Sstevel@tonic-gate if (streq(defp->def_name, type) && defp->def_kind == DEF_TYPEDEF)
1320Sstevel@tonic-gate return (1);
1330Sstevel@tonic-gate return (0);
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate static int
check_self_reference(const char * name,const declaration * decp,int first)1370Sstevel@tonic-gate check_self_reference(const char *name, const declaration *decp, int first)
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate /*
1400Sstevel@tonic-gate * Now check for the following special case if first is true:
1410Sstevel@tonic-gate *
1420Sstevel@tonic-gate * struct foo {
1430Sstevel@tonic-gate * ...
1440Sstevel@tonic-gate * foo *next;
1450Sstevel@tonic-gate * };
1460Sstevel@tonic-gate *
1470Sstevel@tonic-gate *
1480Sstevel@tonic-gate * In the above cases foo has not yet been entered in the type list,
1490Sstevel@tonic-gate * defined. So there is no typedef entry. The prefix in that case
1500Sstevel@tonic-gate * could be empty.
1510Sstevel@tonic-gate */
1520Sstevel@tonic-gate if (decp->rel == REL_POINTER &&
1530Sstevel@tonic-gate (streqn(decp->prefix, "struct") ||
1540Sstevel@tonic-gate (first && streqn(decp->prefix, ""))) &&
1550Sstevel@tonic-gate streqn(name, decp->type))
1560Sstevel@tonic-gate return (1);
1570Sstevel@tonic-gate return (0);
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate static int
is_self_reference(definition * defp,declaration * decp)1610Sstevel@tonic-gate is_self_reference(definition *defp, declaration *decp)
1620Sstevel@tonic-gate {
1630Sstevel@tonic-gate declaration current;
1640Sstevel@tonic-gate definition *dp;
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate if (check_self_reference(defp->def_name, decp, 1))
1670Sstevel@tonic-gate return (1);
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate /*
1700Sstevel@tonic-gate * Check for valid declaration:
1710Sstevel@tonic-gate * Only prefixes allowed are none and struct.
1720Sstevel@tonic-gate * Only relations allowed are pointer or alias.
1730Sstevel@tonic-gate */
1740Sstevel@tonic-gate if (!streqn(decp->prefix, "struct") && !streqn(decp->prefix, ""))
1750Sstevel@tonic-gate return (0);
1760Sstevel@tonic-gate if (decp->rel != REL_POINTER && decp->rel != REL_ALIAS)
1770Sstevel@tonic-gate return (0);
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate current.rel = decp->rel;
1800Sstevel@tonic-gate current.prefix = decp->prefix;
1810Sstevel@tonic-gate current.type = decp->type;
1820Sstevel@tonic-gate current.name = decp->name;
1830Sstevel@tonic-gate decp = ¤t;
1840Sstevel@tonic-gate while (!check_self_reference(defp->def_name, decp, 0)) {
1850Sstevel@tonic-gate dp = FINDVAL(defined, decp->type, cmptype);
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate /*
1880Sstevel@tonic-gate * Check if we found a definition.
1890Sstevel@tonic-gate */
1900Sstevel@tonic-gate if (dp == NULL)
1910Sstevel@tonic-gate return (0);
1920Sstevel@tonic-gate
1930Sstevel@tonic-gate /*
1940Sstevel@tonic-gate * Check for valid prefix. We eventually need to see one
1950Sstevel@tonic-gate * and only one struct.
1960Sstevel@tonic-gate */
1970Sstevel@tonic-gate if (streqn(decp->prefix, "")) {
1980Sstevel@tonic-gate /*
1990Sstevel@tonic-gate * If the current declaration prefix in empty
2000Sstevel@tonic-gate * then the definition found must have an empty
2010Sstevel@tonic-gate * prefix or a struct prefix
2020Sstevel@tonic-gate */
2030Sstevel@tonic-gate if (!streqn(dp->def.ty.old_prefix, "") &&
2040Sstevel@tonic-gate !streqn(dp->def.ty.old_prefix, "struct"))
2050Sstevel@tonic-gate return (0);
2060Sstevel@tonic-gate } else if (streqn(decp->prefix, "struct") &&
207*9497SJordan.Brown@Sun.COM !streqn(dp->def.ty.old_prefix, ""))
2080Sstevel@tonic-gate /*
2090Sstevel@tonic-gate * if the current prefix is struct tne new prefix
2100Sstevel@tonic-gate * must be empty
2110Sstevel@tonic-gate */
2120Sstevel@tonic-gate return (0);
2130Sstevel@tonic-gate else if (!streqn(decp->prefix, "struct"))
2140Sstevel@tonic-gate /* Should never get here */
2150Sstevel@tonic-gate return (0);
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate /*
2180Sstevel@tonic-gate * Check for valid relation. We need to see one and
2190Sstevel@tonic-gate * only one REL_POINTER. The only valid relation types
2200Sstevel@tonic-gate * are REL_POINTER and REL_ALIAS.
2210Sstevel@tonic-gate */
2220Sstevel@tonic-gate if (decp->rel == REL_POINTER && dp->def.ty.rel != REL_ALIAS)
2230Sstevel@tonic-gate return (0);
224132Srobinson if (decp->rel == REL_ALIAS &&
225*9497SJordan.Brown@Sun.COM (dp->def.ty.rel != REL_ALIAS &&
226*9497SJordan.Brown@Sun.COM dp->def.ty.rel != REL_POINTER))
2270Sstevel@tonic-gate return (0);
228132Srobinson if (decp->rel != REL_ALIAS && decp->rel != REL_POINTER)
2290Sstevel@tonic-gate /* Should never get here */
2300Sstevel@tonic-gate return (0);
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate /* Set up the current declaration */
2330Sstevel@tonic-gate if (streqn(decp->prefix, ""))
2340Sstevel@tonic-gate decp->prefix = dp->def.ty.old_prefix;
2350Sstevel@tonic-gate decp->type = dp->def.ty.old_type;
2360Sstevel@tonic-gate if (decp->rel == REL_ALIAS)
2370Sstevel@tonic-gate decp->rel = dp->def.ty.rel;
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate /* We have a self reference type */
2410Sstevel@tonic-gate return (1);
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate
244132Srobinson static void
def_struct(definition * defp)245132Srobinson def_struct(definition *defp)
2460Sstevel@tonic-gate {
2470Sstevel@tonic-gate token tok;
2480Sstevel@tonic-gate declaration dec;
2490Sstevel@tonic-gate decl_list *decls;
2500Sstevel@tonic-gate decl_list **tailp, *endp;
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate defp->def_kind = DEF_STRUCT;
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate scan(TOK_IDENT, &tok);
2550Sstevel@tonic-gate defp->def_name = tok.str;
2560Sstevel@tonic-gate scan(TOK_LBRACE, &tok);
2570Sstevel@tonic-gate tailp = &defp->def.st.decls;
2580Sstevel@tonic-gate defp->def.st.tail = NULL;
2590Sstevel@tonic-gate do {
2600Sstevel@tonic-gate get_declaration(&dec, DEF_STRUCT);
261132Srobinson decls = calloc(1, sizeof (decl_list));
2620Sstevel@tonic-gate decls->decl = dec;
2630Sstevel@tonic-gate /*
2640Sstevel@tonic-gate * Keep a referenct to the last declaration to check for
2650Sstevel@tonic-gate * tail recurrsion.
2660Sstevel@tonic-gate */
2670Sstevel@tonic-gate endp = *tailp = decls;
2680Sstevel@tonic-gate tailp = &decls->next;
2690Sstevel@tonic-gate scan(TOK_SEMICOLON, &tok);
2700Sstevel@tonic-gate peek(&tok);
2710Sstevel@tonic-gate } while (tok.kind != TOK_RBRACE);
2720Sstevel@tonic-gate *tailp = NULL;
2730Sstevel@tonic-gate /*
2740Sstevel@tonic-gate * Check for tail recurse. If the last declaration refers to this
2750Sstevel@tonic-gate * structure then mark this stucture to convert the tail recursion
2760Sstevel@tonic-gate * to itteration.
2770Sstevel@tonic-gate */
2780Sstevel@tonic-gate defp->def.st.self_pointer = is_self_reference(defp, &endp->decl);
2790Sstevel@tonic-gate get_token(&tok);
2800Sstevel@tonic-gate defp->def.st.tail = endp;
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate
283132Srobinson static void
def_program(definition * defp)284132Srobinson def_program(definition *defp)
2850Sstevel@tonic-gate {
2860Sstevel@tonic-gate token tok;
2870Sstevel@tonic-gate declaration dec;
2880Sstevel@tonic-gate decl_list *decls;
2890Sstevel@tonic-gate decl_list **tailp;
2900Sstevel@tonic-gate version_list *vlist;
2910Sstevel@tonic-gate version_list **vtailp;
2920Sstevel@tonic-gate proc_list *plist;
2930Sstevel@tonic-gate proc_list **ptailp;
2940Sstevel@tonic-gate int num_args;
2950Sstevel@tonic-gate bool_t isvoid = FALSE; /* whether first argument is void */
2960Sstevel@tonic-gate defp->def_kind = DEF_PROGRAM;
2970Sstevel@tonic-gate scan(TOK_IDENT, &tok);
2980Sstevel@tonic-gate defp->def_name = tok.str;
2990Sstevel@tonic-gate scan(TOK_LBRACE, &tok);
3000Sstevel@tonic-gate vtailp = &defp->def.pr.versions;
3010Sstevel@tonic-gate tailp = &defp->def.st.decls;
3020Sstevel@tonic-gate scan(TOK_VERSION, &tok);
3030Sstevel@tonic-gate do {
3040Sstevel@tonic-gate scan(TOK_IDENT, &tok);
305132Srobinson vlist = calloc(1, sizeof (version_list));
3060Sstevel@tonic-gate vlist->vers_name = tok.str;
3070Sstevel@tonic-gate scan(TOK_LBRACE, &tok);
3080Sstevel@tonic-gate ptailp = &vlist->procs;
3090Sstevel@tonic-gate do {
3100Sstevel@tonic-gate /* get result type */
311132Srobinson plist = calloc(1, sizeof (proc_list));
3120Sstevel@tonic-gate get_type(&plist->res_prefix, &plist->res_type,
3130Sstevel@tonic-gate DEF_RESULT);
3140Sstevel@tonic-gate if (streq(plist->res_type, "opaque")) {
3150Sstevel@tonic-gate error("illegal result type");
3160Sstevel@tonic-gate }
3170Sstevel@tonic-gate scan(TOK_IDENT, &tok);
3180Sstevel@tonic-gate plist->proc_name = tok.str;
3190Sstevel@tonic-gate scan(TOK_LPAREN, &tok);
3200Sstevel@tonic-gate /* get args - first one */
3210Sstevel@tonic-gate num_args = 1;
3220Sstevel@tonic-gate isvoid = FALSE;
3230Sstevel@tonic-gate /*
3240Sstevel@tonic-gate * type of DEF_PROGRAM in the first
3250Sstevel@tonic-gate * get_prog_declaration and DEF_STURCT in the next
3260Sstevel@tonic-gate * allows void as argument if it is the only argument
3270Sstevel@tonic-gate */
3280Sstevel@tonic-gate get_prog_declaration(&dec, DEF_PROGRAM, num_args);
3290Sstevel@tonic-gate if (streq(dec.type, "void"))
3300Sstevel@tonic-gate isvoid = TRUE;
331132Srobinson decls = calloc(1, sizeof (decl_list));
3320Sstevel@tonic-gate plist->args.decls = decls;
3330Sstevel@tonic-gate decls->decl = dec;
3340Sstevel@tonic-gate tailp = &decls->next;
3350Sstevel@tonic-gate /* get args */
3360Sstevel@tonic-gate while (peekscan(TOK_COMMA, &tok)) {
3370Sstevel@tonic-gate num_args++;
3380Sstevel@tonic-gate get_prog_declaration(&dec, DEF_STRUCT,
339*9497SJordan.Brown@Sun.COM num_args);
340132Srobinson decls = calloc(1, sizeof (decl_list));
3410Sstevel@tonic-gate decls->decl = dec;
3420Sstevel@tonic-gate *tailp = decls;
3430Sstevel@tonic-gate if (streq(dec.type, "void"))
3440Sstevel@tonic-gate isvoid = TRUE;
3450Sstevel@tonic-gate tailp = &decls->next;
3460Sstevel@tonic-gate }
3470Sstevel@tonic-gate /* multiple arguments are only allowed in newstyle */
3480Sstevel@tonic-gate if (!newstyle && num_args > 1) {
3490Sstevel@tonic-gate error("only one argument is allowed");
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate if (isvoid && num_args > 1) {
3520Sstevel@tonic-gate error("illegal use of void "
3530Sstevel@tonic-gate "in program definition");
3540Sstevel@tonic-gate }
3550Sstevel@tonic-gate *tailp = NULL;
3560Sstevel@tonic-gate scan(TOK_RPAREN, &tok);
3570Sstevel@tonic-gate scan(TOK_EQUAL, &tok);
3580Sstevel@tonic-gate scan_num(&tok);
3590Sstevel@tonic-gate scan(TOK_SEMICOLON, &tok);
3600Sstevel@tonic-gate plist->proc_num = tok.str;
3610Sstevel@tonic-gate plist->arg_num = num_args;
3620Sstevel@tonic-gate *ptailp = plist;
3630Sstevel@tonic-gate ptailp = &plist->next;
3640Sstevel@tonic-gate peek(&tok);
3650Sstevel@tonic-gate } while (tok.kind != TOK_RBRACE);
3660Sstevel@tonic-gate *ptailp = NULL;
3670Sstevel@tonic-gate *vtailp = vlist;
3680Sstevel@tonic-gate vtailp = &vlist->next;
3690Sstevel@tonic-gate scan(TOK_RBRACE, &tok);
3700Sstevel@tonic-gate scan(TOK_EQUAL, &tok);
3710Sstevel@tonic-gate scan_num(&tok);
3720Sstevel@tonic-gate vlist->vers_num = tok.str;
3730Sstevel@tonic-gate /* make the argument structure name for each arg */
374132Srobinson for (plist = vlist->procs; plist != NULL; plist = plist->next) {
3750Sstevel@tonic-gate plist->args.argname = make_argname(plist->proc_name,
376*9497SJordan.Brown@Sun.COM vlist->vers_num);
3770Sstevel@tonic-gate /* free the memory ?? */
3780Sstevel@tonic-gate }
3790Sstevel@tonic-gate scan(TOK_SEMICOLON, &tok);
3800Sstevel@tonic-gate scan2(TOK_VERSION, TOK_RBRACE, &tok);
3810Sstevel@tonic-gate } while (tok.kind == TOK_VERSION);
3820Sstevel@tonic-gate scan(TOK_EQUAL, &tok);
3830Sstevel@tonic-gate scan_num(&tok);
3840Sstevel@tonic-gate defp->def.pr.prog_num = tok.str;
3850Sstevel@tonic-gate *vtailp = NULL;
3860Sstevel@tonic-gate }
3870Sstevel@tonic-gate
388132Srobinson static void
def_enum(definition * defp)389132Srobinson def_enum(definition *defp)
3900Sstevel@tonic-gate {
3910Sstevel@tonic-gate token tok;
3920Sstevel@tonic-gate enumval_list *elist;
3930Sstevel@tonic-gate enumval_list **tailp;
3940Sstevel@tonic-gate
3950Sstevel@tonic-gate defp->def_kind = DEF_ENUM;
3960Sstevel@tonic-gate scan(TOK_IDENT, &tok);
3970Sstevel@tonic-gate defp->def_name = tok.str;
3980Sstevel@tonic-gate scan(TOK_LBRACE, &tok);
3990Sstevel@tonic-gate tailp = &defp->def.en.vals;
4000Sstevel@tonic-gate do {
4010Sstevel@tonic-gate scan(TOK_IDENT, &tok);
402132Srobinson elist = calloc(1, sizeof (enumval_list));
4030Sstevel@tonic-gate elist->name = tok.str;
4040Sstevel@tonic-gate elist->assignment = NULL;
4050Sstevel@tonic-gate scan3(TOK_COMMA, TOK_RBRACE, TOK_EQUAL, &tok);
4060Sstevel@tonic-gate if (tok.kind == TOK_EQUAL) {
4070Sstevel@tonic-gate scan_num(&tok);
4080Sstevel@tonic-gate elist->assignment = tok.str;
4090Sstevel@tonic-gate scan2(TOK_COMMA, TOK_RBRACE, &tok);
4100Sstevel@tonic-gate }
4110Sstevel@tonic-gate *tailp = elist;
4120Sstevel@tonic-gate tailp = &elist->next;
4130Sstevel@tonic-gate } while (tok.kind != TOK_RBRACE);
4140Sstevel@tonic-gate *tailp = NULL;
4150Sstevel@tonic-gate }
4160Sstevel@tonic-gate
417132Srobinson static void
def_const(definition * defp)418132Srobinson def_const(definition *defp)
4190Sstevel@tonic-gate {
4200Sstevel@tonic-gate token tok;
4210Sstevel@tonic-gate
4220Sstevel@tonic-gate defp->def_kind = DEF_CONST;
4230Sstevel@tonic-gate scan(TOK_IDENT, &tok);
4240Sstevel@tonic-gate defp->def_name = tok.str;
4250Sstevel@tonic-gate scan(TOK_EQUAL, &tok);
4260Sstevel@tonic-gate scan2(TOK_IDENT, TOK_STRCONST, &tok);
4270Sstevel@tonic-gate defp->def.co = tok.str;
4280Sstevel@tonic-gate }
4290Sstevel@tonic-gate
430132Srobinson static void
def_union(definition * defp)431132Srobinson def_union(definition *defp)
4320Sstevel@tonic-gate {
4330Sstevel@tonic-gate token tok;
4340Sstevel@tonic-gate declaration dec;
435132Srobinson case_list *cases;
4360Sstevel@tonic-gate case_list **tailp;
4370Sstevel@tonic-gate int flag;
4380Sstevel@tonic-gate
4390Sstevel@tonic-gate defp->def_kind = DEF_UNION;
4400Sstevel@tonic-gate scan(TOK_IDENT, &tok);
4410Sstevel@tonic-gate defp->def_name = tok.str;
4420Sstevel@tonic-gate scan(TOK_SWITCH, &tok);
4430Sstevel@tonic-gate scan(TOK_LPAREN, &tok);
4440Sstevel@tonic-gate get_declaration(&dec, DEF_UNION);
4450Sstevel@tonic-gate defp->def.un.enum_decl = dec;
4460Sstevel@tonic-gate tailp = &defp->def.un.cases;
4470Sstevel@tonic-gate scan(TOK_RPAREN, &tok);
4480Sstevel@tonic-gate scan(TOK_LBRACE, &tok);
4490Sstevel@tonic-gate scan(TOK_CASE, &tok);
4500Sstevel@tonic-gate while (tok.kind == TOK_CASE) {
4510Sstevel@tonic-gate scan2(TOK_IDENT, TOK_CHARCONST, &tok);
452132Srobinson cases = calloc(1, sizeof (case_list));
4530Sstevel@tonic-gate cases->case_name = tok.str;
4540Sstevel@tonic-gate scan(TOK_COLON, &tok);
4550Sstevel@tonic-gate /* now peek at next token */
4560Sstevel@tonic-gate flag = 0;
4570Sstevel@tonic-gate if (peekscan(TOK_CASE, &tok)) {
4580Sstevel@tonic-gate do {
4590Sstevel@tonic-gate scan2(TOK_IDENT, TOK_CHARCONST, &tok);
4600Sstevel@tonic-gate cases->contflag = 1;
4610Sstevel@tonic-gate /* continued case statement */
4620Sstevel@tonic-gate *tailp = cases;
4630Sstevel@tonic-gate tailp = &cases->next;
464132Srobinson cases = calloc(1, sizeof (case_list));
4650Sstevel@tonic-gate cases->case_name = tok.str;
4660Sstevel@tonic-gate scan(TOK_COLON, &tok);
4670Sstevel@tonic-gate } while (peekscan(TOK_CASE, &tok));
468132Srobinson } else if (flag) {
469132Srobinson *tailp = cases;
470132Srobinson tailp = &cases->next;
471132Srobinson cases = calloc(1, sizeof (case_list));
4720Sstevel@tonic-gate }
4730Sstevel@tonic-gate
4740Sstevel@tonic-gate get_declaration(&dec, DEF_UNION);
4750Sstevel@tonic-gate cases->case_decl = dec;
4760Sstevel@tonic-gate cases->contflag = 0; /* no continued case statement */
4770Sstevel@tonic-gate *tailp = cases;
4780Sstevel@tonic-gate tailp = &cases->next;
4790Sstevel@tonic-gate scan(TOK_SEMICOLON, &tok);
4800Sstevel@tonic-gate
4810Sstevel@tonic-gate scan3(TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok);
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate *tailp = NULL;
4840Sstevel@tonic-gate if (tok.kind == TOK_DEFAULT) {
4850Sstevel@tonic-gate scan(TOK_COLON, &tok);
4860Sstevel@tonic-gate get_declaration(&dec, DEF_UNION);
487132Srobinson defp->def.un.default_decl = calloc(1, sizeof (declaration));
4880Sstevel@tonic-gate *defp->def.un.default_decl = dec;
4890Sstevel@tonic-gate scan(TOK_SEMICOLON, &tok);
4900Sstevel@tonic-gate scan(TOK_RBRACE, &tok);
4910Sstevel@tonic-gate } else {
4920Sstevel@tonic-gate defp->def.un.default_decl = NULL;
4930Sstevel@tonic-gate }
4940Sstevel@tonic-gate }
4950Sstevel@tonic-gate
496132Srobinson static char *reserved_words[] = {
4970Sstevel@tonic-gate "array",
4980Sstevel@tonic-gate "bytes",
4990Sstevel@tonic-gate "destroy",
5000Sstevel@tonic-gate "free",
5010Sstevel@tonic-gate "getpos",
5020Sstevel@tonic-gate "inline",
5030Sstevel@tonic-gate "pointer",
5040Sstevel@tonic-gate "reference",
5050Sstevel@tonic-gate "setpos",
5060Sstevel@tonic-gate "sizeof",
5070Sstevel@tonic-gate "union",
5080Sstevel@tonic-gate "vector",
5090Sstevel@tonic-gate NULL
510132Srobinson };
5110Sstevel@tonic-gate
512132Srobinson static char *reserved_types[] = {
5130Sstevel@tonic-gate "opaque",
5140Sstevel@tonic-gate "string",
5150Sstevel@tonic-gate NULL
516132Srobinson };
5170Sstevel@tonic-gate
5180Sstevel@tonic-gate /*
5190Sstevel@tonic-gate * check that the given name is not one that would eventually result in
5200Sstevel@tonic-gate * xdr routines that would conflict with internal XDR routines.
5210Sstevel@tonic-gate */
522132Srobinson static void
check_type_name(char * name,int new_type)523132Srobinson check_type_name(char *name, int new_type)
5240Sstevel@tonic-gate {
5250Sstevel@tonic-gate int i;
5260Sstevel@tonic-gate char tmp[100];
5270Sstevel@tonic-gate
5280Sstevel@tonic-gate for (i = 0; reserved_words[i] != NULL; i++) {
5290Sstevel@tonic-gate if (strcmp(name, reserved_words[i]) == 0) {
530132Srobinson (void) snprintf(tmp, sizeof (tmp),
531*9497SJordan.Brown@Sun.COM "illegal (reserved) name :\'%s\' "
532*9497SJordan.Brown@Sun.COM "in type definition",
533*9497SJordan.Brown@Sun.COM name);
5340Sstevel@tonic-gate error(tmp);
5350Sstevel@tonic-gate }
5360Sstevel@tonic-gate }
5370Sstevel@tonic-gate if (new_type) {
5380Sstevel@tonic-gate for (i = 0; reserved_types[i] != NULL; i++) {
5390Sstevel@tonic-gate if (strcmp(name, reserved_types[i]) == 0) {
540132Srobinson (void) snprintf(tmp, sizeof (tmp),
541*9497SJordan.Brown@Sun.COM "illegal (reserved) name :\'%s\' "
542*9497SJordan.Brown@Sun.COM "in type definition",
543*9497SJordan.Brown@Sun.COM name);
5440Sstevel@tonic-gate error(tmp);
5450Sstevel@tonic-gate }
5460Sstevel@tonic-gate }
5470Sstevel@tonic-gate }
5480Sstevel@tonic-gate }
5490Sstevel@tonic-gate
550132Srobinson static void
def_typedef(definition * defp)551132Srobinson def_typedef(definition *defp)
5520Sstevel@tonic-gate {
5530Sstevel@tonic-gate declaration dec;
5540Sstevel@tonic-gate
5550Sstevel@tonic-gate defp->def_kind = DEF_TYPEDEF;
5560Sstevel@tonic-gate get_declaration(&dec, DEF_TYPEDEF);
5570Sstevel@tonic-gate defp->def_name = dec.name;
5580Sstevel@tonic-gate check_type_name(dec.name, 1);
5590Sstevel@tonic-gate defp->def.ty.old_prefix = dec.prefix;
5600Sstevel@tonic-gate defp->def.ty.old_type = dec.type;
5610Sstevel@tonic-gate defp->def.ty.rel = dec.rel;
5620Sstevel@tonic-gate defp->def.ty.array_max = dec.array_max;
5630Sstevel@tonic-gate }
5640Sstevel@tonic-gate
565132Srobinson static void
get_declaration(declaration * dec,defkind dkind)566132Srobinson get_declaration(declaration *dec, defkind dkind)
5670Sstevel@tonic-gate {
5680Sstevel@tonic-gate token tok;
5690Sstevel@tonic-gate
5700Sstevel@tonic-gate get_type(&dec->prefix, &dec->type, dkind);
5710Sstevel@tonic-gate dec->rel = REL_ALIAS;
572132Srobinson if (streq(dec->type, "void"))
5730Sstevel@tonic-gate return;
5740Sstevel@tonic-gate
5750Sstevel@tonic-gate check_type_name(dec->type, 0);
5760Sstevel@tonic-gate scan2(TOK_STAR, TOK_IDENT, &tok);
5770Sstevel@tonic-gate if (tok.kind == TOK_STAR) {
5780Sstevel@tonic-gate dec->rel = REL_POINTER;
5790Sstevel@tonic-gate scan(TOK_IDENT, &tok);
5800Sstevel@tonic-gate }
5810Sstevel@tonic-gate dec->name = tok.str;
5820Sstevel@tonic-gate if (peekscan(TOK_LBRACKET, &tok)) {
583132Srobinson if (dec->rel == REL_POINTER)
5840Sstevel@tonic-gate error("no array-of-pointer declarations "
5850Sstevel@tonic-gate "-- use typedef");
5860Sstevel@tonic-gate dec->rel = REL_VECTOR;
5870Sstevel@tonic-gate scan_num(&tok);
5880Sstevel@tonic-gate dec->array_max = tok.str;
5890Sstevel@tonic-gate scan(TOK_RBRACKET, &tok);
5900Sstevel@tonic-gate } else if (peekscan(TOK_LANGLE, &tok)) {
591132Srobinson if (dec->rel == REL_POINTER)
5920Sstevel@tonic-gate error("no array-of-pointer declarations "
5930Sstevel@tonic-gate "-- use typedef");
5940Sstevel@tonic-gate dec->rel = REL_ARRAY;
5950Sstevel@tonic-gate if (peekscan(TOK_RANGLE, &tok)) {
5960Sstevel@tonic-gate dec->array_max = "~0"; /* unspecified size, use max */
5970Sstevel@tonic-gate } else {
5980Sstevel@tonic-gate scan_num(&tok);
5990Sstevel@tonic-gate dec->array_max = tok.str;
6000Sstevel@tonic-gate scan(TOK_RANGLE, &tok);
6010Sstevel@tonic-gate }
6020Sstevel@tonic-gate }
6030Sstevel@tonic-gate if (streq(dec->type, "opaque")) {
6040Sstevel@tonic-gate if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR) {
6050Sstevel@tonic-gate error("array declaration expected");
6060Sstevel@tonic-gate }
6070Sstevel@tonic-gate } else if (streq(dec->type, "string")) {
6080Sstevel@tonic-gate if (dec->rel != REL_ARRAY) {
6090Sstevel@tonic-gate error("variable-length array declaration expected");
6100Sstevel@tonic-gate }
6110Sstevel@tonic-gate }
6120Sstevel@tonic-gate }
6130Sstevel@tonic-gate
614132Srobinson static void
get_prog_declaration(declaration * dec,defkind dkind,int num)615132Srobinson get_prog_declaration(declaration *dec, defkind dkind, int num)
6160Sstevel@tonic-gate {
6170Sstevel@tonic-gate token tok;
6180Sstevel@tonic-gate char name[sizeof (ARGNAME) + 10];
6190Sstevel@tonic-gate
6200Sstevel@tonic-gate if (dkind == DEF_PROGRAM) {
6210Sstevel@tonic-gate peek(&tok);
6220Sstevel@tonic-gate if (tok.kind == TOK_RPAREN) { /* no arguments */
6230Sstevel@tonic-gate dec->rel = REL_ALIAS;
6240Sstevel@tonic-gate dec->type = "void";
6250Sstevel@tonic-gate dec->prefix = NULL;
6260Sstevel@tonic-gate dec->name = NULL;
6270Sstevel@tonic-gate return;
6280Sstevel@tonic-gate }
6290Sstevel@tonic-gate }
6300Sstevel@tonic-gate get_type(&dec->prefix, &dec->type, dkind);
6310Sstevel@tonic-gate dec->rel = REL_ALIAS;
6320Sstevel@tonic-gate if (peekscan(TOK_IDENT, &tok)) /* optional name of argument */
6330Sstevel@tonic-gate dec->name = strdup(tok.str);
6340Sstevel@tonic-gate else {
6350Sstevel@tonic-gate /* default name of argument */
636132Srobinson (void) snprintf(name, sizeof (name), "%s%d", ARGNAME, num);
6370Sstevel@tonic-gate dec->name = strdup(name);
6380Sstevel@tonic-gate }
6390Sstevel@tonic-gate if (dec->name == NULL)
6400Sstevel@tonic-gate error("internal error -- out of memory");
6410Sstevel@tonic-gate
642132Srobinson if (streq(dec->type, "void"))
6430Sstevel@tonic-gate return;
6440Sstevel@tonic-gate
645132Srobinson if (streq(dec->type, "opaque"))
6460Sstevel@tonic-gate error("opaque -- illegal argument type");
6470Sstevel@tonic-gate if (peekscan(TOK_STAR, &tok)) {
6480Sstevel@tonic-gate if (streq(dec->type, "string")) {
6490Sstevel@tonic-gate error("pointer to string not allowed "
6500Sstevel@tonic-gate "in program arguments\n");
6510Sstevel@tonic-gate }
6520Sstevel@tonic-gate dec->rel = REL_POINTER;
6530Sstevel@tonic-gate if (peekscan(TOK_IDENT, &tok))
6540Sstevel@tonic-gate /* optional name of argument */
6550Sstevel@tonic-gate dec->name = strdup(tok.str);
6560Sstevel@tonic-gate }
6570Sstevel@tonic-gate if (peekscan(TOK_LANGLE, &tok)) {
6580Sstevel@tonic-gate if (!streq(dec->type, "string")) {
6590Sstevel@tonic-gate error("arrays cannot be declared as arguments "
6600Sstevel@tonic-gate "to procedures -- use typedef");
6610Sstevel@tonic-gate }
6620Sstevel@tonic-gate dec->rel = REL_ARRAY;
6630Sstevel@tonic-gate if (peekscan(TOK_RANGLE, &tok)) {
6640Sstevel@tonic-gate dec->array_max = "~0";
6650Sstevel@tonic-gate /* unspecified size, use max */
6660Sstevel@tonic-gate } else {
6670Sstevel@tonic-gate scan_num(&tok);
6680Sstevel@tonic-gate dec->array_max = tok.str;
6690Sstevel@tonic-gate scan(TOK_RANGLE, &tok);
6700Sstevel@tonic-gate }
6710Sstevel@tonic-gate }
6720Sstevel@tonic-gate if (streq(dec->type, "string")) {
6730Sstevel@tonic-gate if (dec->rel != REL_ARRAY) {
6740Sstevel@tonic-gate /*
6750Sstevel@tonic-gate * .x specifies just string as
6760Sstevel@tonic-gate * type of argument
6770Sstevel@tonic-gate * - make it string<>
6780Sstevel@tonic-gate */
6790Sstevel@tonic-gate dec->rel = REL_ARRAY;
6800Sstevel@tonic-gate dec->array_max = "~0"; /* unspecified size, use max */
6810Sstevel@tonic-gate }
6820Sstevel@tonic-gate }
6830Sstevel@tonic-gate }
6840Sstevel@tonic-gate
685132Srobinson static void
get_type(char ** prefixp,char ** typep,defkind dkind)686132Srobinson get_type(char **prefixp, char **typep, defkind dkind)
6870Sstevel@tonic-gate {
6880Sstevel@tonic-gate token tok;
6890Sstevel@tonic-gate
6900Sstevel@tonic-gate *prefixp = NULL;
6910Sstevel@tonic-gate get_token(&tok);
6920Sstevel@tonic-gate switch (tok.kind) {
6930Sstevel@tonic-gate case TOK_IDENT:
6940Sstevel@tonic-gate *typep = tok.str;
6950Sstevel@tonic-gate break;
6960Sstevel@tonic-gate case TOK_STRUCT:
6970Sstevel@tonic-gate case TOK_ENUM:
6980Sstevel@tonic-gate case TOK_UNION:
6990Sstevel@tonic-gate *prefixp = tok.str;
7000Sstevel@tonic-gate scan(TOK_IDENT, &tok);
7010Sstevel@tonic-gate *typep = tok.str;
7020Sstevel@tonic-gate break;
7030Sstevel@tonic-gate case TOK_UNSIGNED:
7040Sstevel@tonic-gate unsigned_dec(typep);
7050Sstevel@tonic-gate break;
7060Sstevel@tonic-gate case TOK_SHORT:
7070Sstevel@tonic-gate *typep = "short";
7080Sstevel@tonic-gate (void) peekscan(TOK_INT, &tok);
7090Sstevel@tonic-gate break;
7100Sstevel@tonic-gate case TOK_LONG:
7110Sstevel@tonic-gate *typep = "long";
7120Sstevel@tonic-gate (void) peekscan(TOK_INT, &tok);
7130Sstevel@tonic-gate break;
7140Sstevel@tonic-gate case TOK_HYPER:
7150Sstevel@tonic-gate *typep = "longlong_t";
7160Sstevel@tonic-gate (void) peekscan(TOK_INT, &tok);
7170Sstevel@tonic-gate break;
7180Sstevel@tonic-gate
7190Sstevel@tonic-gate case TOK_VOID:
7200Sstevel@tonic-gate if (dkind != DEF_UNION && dkind != DEF_PROGRAM &&
7210Sstevel@tonic-gate dkind != DEF_RESULT) {
7220Sstevel@tonic-gate error("voids allowed only inside union and "
7230Sstevel@tonic-gate "program definitions with one argument");
7240Sstevel@tonic-gate }
7250Sstevel@tonic-gate *typep = tok.str;
7260Sstevel@tonic-gate break;
7270Sstevel@tonic-gate case TOK_ONEWAY:
7280Sstevel@tonic-gate if (dkind != DEF_RESULT) {
7290Sstevel@tonic-gate error("oneways allowed only inside result definitions");
7300Sstevel@tonic-gate }
7310Sstevel@tonic-gate *typep = tok.str;
7320Sstevel@tonic-gate break;
7330Sstevel@tonic-gate case TOK_STRING:
7340Sstevel@tonic-gate case TOK_OPAQUE:
7350Sstevel@tonic-gate case TOK_CHAR:
7360Sstevel@tonic-gate case TOK_INT:
7370Sstevel@tonic-gate case TOK_FLOAT:
7380Sstevel@tonic-gate case TOK_DOUBLE:
7390Sstevel@tonic-gate case TOK_BOOL:
7400Sstevel@tonic-gate case TOK_QUAD:
7410Sstevel@tonic-gate *typep = tok.str;
7420Sstevel@tonic-gate break;
7430Sstevel@tonic-gate default:
7440Sstevel@tonic-gate error("expected type specifier");
7450Sstevel@tonic-gate }
7460Sstevel@tonic-gate }
7470Sstevel@tonic-gate
748132Srobinson static void
unsigned_dec(char ** typep)749132Srobinson unsigned_dec(char **typep)
7500Sstevel@tonic-gate {
7510Sstevel@tonic-gate token tok;
7520Sstevel@tonic-gate
7530Sstevel@tonic-gate peek(&tok);
7540Sstevel@tonic-gate switch (tok.kind) {
7550Sstevel@tonic-gate case TOK_CHAR:
7560Sstevel@tonic-gate get_token(&tok);
7570Sstevel@tonic-gate *typep = "u_char";
7580Sstevel@tonic-gate break;
7590Sstevel@tonic-gate case TOK_SHORT:
7600Sstevel@tonic-gate get_token(&tok);
7610Sstevel@tonic-gate *typep = "u_short";
7620Sstevel@tonic-gate (void) peekscan(TOK_INT, &tok);
7630Sstevel@tonic-gate break;
7640Sstevel@tonic-gate case TOK_LONG:
7650Sstevel@tonic-gate get_token(&tok);
7660Sstevel@tonic-gate *typep = "u_long";
7670Sstevel@tonic-gate (void) peekscan(TOK_INT, &tok);
7680Sstevel@tonic-gate break;
7690Sstevel@tonic-gate case TOK_HYPER:
7700Sstevel@tonic-gate get_token(&tok);
7710Sstevel@tonic-gate *typep = "u_longlong_t";
7720Sstevel@tonic-gate (void) peekscan(TOK_INT, &tok);
7730Sstevel@tonic-gate break;
7740Sstevel@tonic-gate case TOK_INT:
7750Sstevel@tonic-gate get_token(&tok);
7760Sstevel@tonic-gate *typep = "u_int";
7770Sstevel@tonic-gate break;
7780Sstevel@tonic-gate default:
7790Sstevel@tonic-gate *typep = "u_int";
7800Sstevel@tonic-gate break;
7810Sstevel@tonic-gate }
7820Sstevel@tonic-gate }
783