xref: /titanic_53/usr/src/cmd/rpcgen/rpc_parse.c (revision 61961e0f20c7637a3846bb39786bb9dffa91dfb9)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*61961e0fSrobinson 
237c478bd9Sstevel@tonic-gate /*
247c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * rpc_parse.c, Parser for the RPC protocol compiler
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate #include <stdio.h>
45*61961e0fSrobinson #include <stdlib.h>
467c478bd9Sstevel@tonic-gate #include <string.h>
477c478bd9Sstevel@tonic-gate #include "rpc/types.h"
487c478bd9Sstevel@tonic-gate #include "rpc_scan.h"
497c478bd9Sstevel@tonic-gate #include "rpc_parse.h"
507c478bd9Sstevel@tonic-gate #include "rpc_util.h"
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #define	ARGNAME "arg"
537c478bd9Sstevel@tonic-gate 
54*61961e0fSrobinson extern char *make_argname(char *, char *);
55*61961e0fSrobinson 
56*61961e0fSrobinson static void isdefined(definition *);
57*61961e0fSrobinson static void def_struct(definition *);
58*61961e0fSrobinson static void def_program(definition *);
59*61961e0fSrobinson static void def_enum(definition *);
60*61961e0fSrobinson static void def_const(definition *);
61*61961e0fSrobinson static void def_union(definition *);
62*61961e0fSrobinson static void def_typedef(definition *);
63*61961e0fSrobinson static void get_declaration(declaration *, defkind);
64*61961e0fSrobinson static void get_prog_declaration(declaration *, defkind, int);
65*61961e0fSrobinson static void get_type(char **, char **, defkind);
66*61961e0fSrobinson static void unsigned_dec(char **);
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate  * return the next definition you see
707c478bd9Sstevel@tonic-gate  */
717c478bd9Sstevel@tonic-gate definition *
72*61961e0fSrobinson get_definition(void)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate 	definition *defp;
757c478bd9Sstevel@tonic-gate 	token tok;
767c478bd9Sstevel@tonic-gate 
77*61961e0fSrobinson 	defp = calloc(1, sizeof (definition));
787c478bd9Sstevel@tonic-gate 	get_token(&tok);
797c478bd9Sstevel@tonic-gate 	switch (tok.kind) {
807c478bd9Sstevel@tonic-gate 	case TOK_STRUCT:
817c478bd9Sstevel@tonic-gate 		def_struct(defp);
827c478bd9Sstevel@tonic-gate 		break;
837c478bd9Sstevel@tonic-gate 	case TOK_UNION:
847c478bd9Sstevel@tonic-gate 		def_union(defp);
857c478bd9Sstevel@tonic-gate 		break;
867c478bd9Sstevel@tonic-gate 	case TOK_TYPEDEF:
877c478bd9Sstevel@tonic-gate 		def_typedef(defp);
887c478bd9Sstevel@tonic-gate 		break;
897c478bd9Sstevel@tonic-gate 	case TOK_ENUM:
907c478bd9Sstevel@tonic-gate 		def_enum(defp);
917c478bd9Sstevel@tonic-gate 		break;
927c478bd9Sstevel@tonic-gate 	case TOK_PROGRAM:
937c478bd9Sstevel@tonic-gate 		def_program(defp);
947c478bd9Sstevel@tonic-gate 		break;
957c478bd9Sstevel@tonic-gate 	case TOK_CONST:
967c478bd9Sstevel@tonic-gate 		def_const(defp);
977c478bd9Sstevel@tonic-gate 		break;
987c478bd9Sstevel@tonic-gate 	case TOK_EOF:
997c478bd9Sstevel@tonic-gate 		return (NULL);
1007c478bd9Sstevel@tonic-gate 	default:
1017c478bd9Sstevel@tonic-gate 		error("definition keyword expected");
1027c478bd9Sstevel@tonic-gate 	}
1037c478bd9Sstevel@tonic-gate 	scan(TOK_SEMICOLON, &tok);
1047c478bd9Sstevel@tonic-gate 	isdefined(defp);
1057c478bd9Sstevel@tonic-gate 	return (defp);
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate 
108*61961e0fSrobinson static void
109*61961e0fSrobinson isdefined(definition *defp)
1107c478bd9Sstevel@tonic-gate {
1117c478bd9Sstevel@tonic-gate 	STOREVAL(&defined, defp);
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate  * We treat s == NULL the same as *s == '\0'
1167c478bd9Sstevel@tonic-gate  */
117*61961e0fSrobinson static int
1187c478bd9Sstevel@tonic-gate streqn(const char *s1, const char *s2)
1197c478bd9Sstevel@tonic-gate {
1207c478bd9Sstevel@tonic-gate 	if (s1 == NULL)
1217c478bd9Sstevel@tonic-gate 		s1 = "";
1227c478bd9Sstevel@tonic-gate 	if (s2 == NULL)
1237c478bd9Sstevel@tonic-gate 		s2 = "";
1247c478bd9Sstevel@tonic-gate 	if (s1 == s2)
1257c478bd9Sstevel@tonic-gate 		return (1);
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	return (strcmp(s1, s2) == 0);
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate static int
1317c478bd9Sstevel@tonic-gate cmptype(definition *defp, char *type)
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate 	/* We only want typedef definitions */
1347c478bd9Sstevel@tonic-gate 	if (streq(defp->def_name, type) && defp->def_kind == DEF_TYPEDEF)
1357c478bd9Sstevel@tonic-gate 		return (1);
1367c478bd9Sstevel@tonic-gate 	return (0);
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate static int
1407c478bd9Sstevel@tonic-gate check_self_reference(const char *name, const declaration *decp, int first)
1417c478bd9Sstevel@tonic-gate {
1427c478bd9Sstevel@tonic-gate 	/*
1437c478bd9Sstevel@tonic-gate 	 * Now check for the following special case if first is true:
1447c478bd9Sstevel@tonic-gate 	 *
1457c478bd9Sstevel@tonic-gate 	 * struct foo {
1467c478bd9Sstevel@tonic-gate 	 *	...
1477c478bd9Sstevel@tonic-gate 	 *	foo *next;
1487c478bd9Sstevel@tonic-gate 	 * };
1497c478bd9Sstevel@tonic-gate 	 *
1507c478bd9Sstevel@tonic-gate 	 *
1517c478bd9Sstevel@tonic-gate 	 * In the above cases foo has not yet been entered in the type list,
1527c478bd9Sstevel@tonic-gate 	 * defined. So there is no typedef entry. The prefix in that case
1537c478bd9Sstevel@tonic-gate 	 * could be empty.
1547c478bd9Sstevel@tonic-gate 	 */
1557c478bd9Sstevel@tonic-gate 	if (decp->rel == REL_POINTER &&
1567c478bd9Sstevel@tonic-gate 	    (streqn(decp->prefix, "struct") ||
1577c478bd9Sstevel@tonic-gate 	    (first && streqn(decp->prefix, ""))) &&
1587c478bd9Sstevel@tonic-gate 	    streqn(name, decp->type))
1597c478bd9Sstevel@tonic-gate 		return (1);
1607c478bd9Sstevel@tonic-gate 	return (0);
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate static int
1647c478bd9Sstevel@tonic-gate is_self_reference(definition *defp, declaration *decp)
1657c478bd9Sstevel@tonic-gate {
1667c478bd9Sstevel@tonic-gate 	declaration current;
1677c478bd9Sstevel@tonic-gate 	definition *dp;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	if (check_self_reference(defp->def_name, decp, 1))
1707c478bd9Sstevel@tonic-gate 		return (1);
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	/*
1737c478bd9Sstevel@tonic-gate 	 * Check for valid declaration:
1747c478bd9Sstevel@tonic-gate 	 * Only prefixes allowed are none and struct.
1757c478bd9Sstevel@tonic-gate 	 * Only relations allowed are pointer or alias.
1767c478bd9Sstevel@tonic-gate 	 */
1777c478bd9Sstevel@tonic-gate 	if (!streqn(decp->prefix, "struct") && !streqn(decp->prefix, ""))
1787c478bd9Sstevel@tonic-gate 		return (0);
1797c478bd9Sstevel@tonic-gate 	if (decp->rel != REL_POINTER && decp->rel != REL_ALIAS)
1807c478bd9Sstevel@tonic-gate 		return (0);
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	current.rel = decp->rel;
1837c478bd9Sstevel@tonic-gate 	current.prefix = decp->prefix;
1847c478bd9Sstevel@tonic-gate 	current.type = decp->type;
1857c478bd9Sstevel@tonic-gate 	current.name = decp->name;
1867c478bd9Sstevel@tonic-gate 	decp = &current;
1877c478bd9Sstevel@tonic-gate 	while (!check_self_reference(defp->def_name, decp, 0)) {
1887c478bd9Sstevel@tonic-gate 		dp = FINDVAL(defined, decp->type, cmptype);
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 		/*
1917c478bd9Sstevel@tonic-gate 		 * Check if we found a definition.
1927c478bd9Sstevel@tonic-gate 		 */
1937c478bd9Sstevel@tonic-gate 		if (dp == NULL)
1947c478bd9Sstevel@tonic-gate 			return (0);
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 		/*
1977c478bd9Sstevel@tonic-gate 		 * Check for valid prefix. We eventually need to see one
1987c478bd9Sstevel@tonic-gate 		 * and only one struct.
1997c478bd9Sstevel@tonic-gate 		 */
2007c478bd9Sstevel@tonic-gate 		if (streqn(decp->prefix, "")) {
2017c478bd9Sstevel@tonic-gate 			/*
2027c478bd9Sstevel@tonic-gate 			 * If the current declaration prefix in empty
2037c478bd9Sstevel@tonic-gate 			 * then the definition found must have an empty
2047c478bd9Sstevel@tonic-gate 			 * prefix or a struct prefix
2057c478bd9Sstevel@tonic-gate 			 */
2067c478bd9Sstevel@tonic-gate 			if (!streqn(dp->def.ty.old_prefix, "") &&
2077c478bd9Sstevel@tonic-gate 			    !streqn(dp->def.ty.old_prefix, "struct"))
2087c478bd9Sstevel@tonic-gate 				return (0);
2097c478bd9Sstevel@tonic-gate 		} else if (streqn(decp->prefix, "struct") &&
2107c478bd9Sstevel@tonic-gate 			!streqn(dp->def.ty.old_prefix, ""))
2117c478bd9Sstevel@tonic-gate 			/*
2127c478bd9Sstevel@tonic-gate 			 * if the current prefix is struct tne new prefix
2137c478bd9Sstevel@tonic-gate 			 * must be empty
2147c478bd9Sstevel@tonic-gate 			 */
2157c478bd9Sstevel@tonic-gate 			return (0);
2167c478bd9Sstevel@tonic-gate 		else if (!streqn(decp->prefix, "struct"))
2177c478bd9Sstevel@tonic-gate 			/* Should never get here */
2187c478bd9Sstevel@tonic-gate 			return (0);
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		/*
2217c478bd9Sstevel@tonic-gate 		 * Check for valid relation. We need to see one and
2227c478bd9Sstevel@tonic-gate 		 * only one REL_POINTER. The only valid relation types
2237c478bd9Sstevel@tonic-gate 		 * are REL_POINTER and REL_ALIAS.
2247c478bd9Sstevel@tonic-gate 		 */
2257c478bd9Sstevel@tonic-gate 		if (decp->rel == REL_POINTER && dp->def.ty.rel != REL_ALIAS)
2267c478bd9Sstevel@tonic-gate 			return (0);
227*61961e0fSrobinson 		if (decp->rel == REL_ALIAS &&
2287c478bd9Sstevel@tonic-gate 			(dp->def.ty.rel != REL_ALIAS &&
2297c478bd9Sstevel@tonic-gate 			dp->def.ty.rel != REL_POINTER))
2307c478bd9Sstevel@tonic-gate 			return (0);
231*61961e0fSrobinson 		if (decp->rel != REL_ALIAS && decp->rel != REL_POINTER)
2327c478bd9Sstevel@tonic-gate 			/* Should never get here */
2337c478bd9Sstevel@tonic-gate 			return (0);
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 		/* Set up the current declaration */
2367c478bd9Sstevel@tonic-gate 		if (streqn(decp->prefix, ""))
2377c478bd9Sstevel@tonic-gate 			decp->prefix = dp->def.ty.old_prefix;
2387c478bd9Sstevel@tonic-gate 		decp->type = dp->def.ty.old_type;
2397c478bd9Sstevel@tonic-gate 		if (decp->rel == REL_ALIAS)
2407c478bd9Sstevel@tonic-gate 			decp->rel = dp->def.ty.rel;
2417c478bd9Sstevel@tonic-gate 	}
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	/* We have a self reference type */
2447c478bd9Sstevel@tonic-gate 	return (1);
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate 
247*61961e0fSrobinson static void
248*61961e0fSrobinson def_struct(definition *defp)
2497c478bd9Sstevel@tonic-gate {
2507c478bd9Sstevel@tonic-gate 	token tok;
2517c478bd9Sstevel@tonic-gate 	declaration dec;
2527c478bd9Sstevel@tonic-gate 	decl_list *decls;
2537c478bd9Sstevel@tonic-gate 	decl_list **tailp, *endp;
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	defp->def_kind = DEF_STRUCT;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	scan(TOK_IDENT, &tok);
2587c478bd9Sstevel@tonic-gate 	defp->def_name = tok.str;
2597c478bd9Sstevel@tonic-gate 	scan(TOK_LBRACE, &tok);
2607c478bd9Sstevel@tonic-gate 	tailp = &defp->def.st.decls;
2617c478bd9Sstevel@tonic-gate 	defp->def.st.tail = NULL;
2627c478bd9Sstevel@tonic-gate 	do {
2637c478bd9Sstevel@tonic-gate 		get_declaration(&dec, DEF_STRUCT);
264*61961e0fSrobinson 		decls = calloc(1, sizeof (decl_list));
2657c478bd9Sstevel@tonic-gate 		decls->decl = dec;
2667c478bd9Sstevel@tonic-gate 		/*
2677c478bd9Sstevel@tonic-gate 		 * Keep a referenct to the last declaration to check for
2687c478bd9Sstevel@tonic-gate 		 * tail recurrsion.
2697c478bd9Sstevel@tonic-gate 		 */
2707c478bd9Sstevel@tonic-gate 		endp = *tailp = decls;
2717c478bd9Sstevel@tonic-gate 		tailp = &decls->next;
2727c478bd9Sstevel@tonic-gate 		scan(TOK_SEMICOLON, &tok);
2737c478bd9Sstevel@tonic-gate 		peek(&tok);
2747c478bd9Sstevel@tonic-gate 	} while (tok.kind != TOK_RBRACE);
2757c478bd9Sstevel@tonic-gate 	*tailp = NULL;
2767c478bd9Sstevel@tonic-gate 	/*
2777c478bd9Sstevel@tonic-gate 	 * Check for tail recurse. If the last declaration refers to this
2787c478bd9Sstevel@tonic-gate 	 * structure then mark this stucture to convert the tail recursion
2797c478bd9Sstevel@tonic-gate 	 * to itteration.
2807c478bd9Sstevel@tonic-gate 	 */
2817c478bd9Sstevel@tonic-gate 	defp->def.st.self_pointer = is_self_reference(defp, &endp->decl);
2827c478bd9Sstevel@tonic-gate 	get_token(&tok);
2837c478bd9Sstevel@tonic-gate 	defp->def.st.tail = endp;
2847c478bd9Sstevel@tonic-gate }
2857c478bd9Sstevel@tonic-gate 
286*61961e0fSrobinson static void
287*61961e0fSrobinson def_program(definition *defp)
2887c478bd9Sstevel@tonic-gate {
2897c478bd9Sstevel@tonic-gate 	token tok;
2907c478bd9Sstevel@tonic-gate 	declaration dec;
2917c478bd9Sstevel@tonic-gate 	decl_list *decls;
2927c478bd9Sstevel@tonic-gate 	decl_list **tailp;
2937c478bd9Sstevel@tonic-gate 	version_list *vlist;
2947c478bd9Sstevel@tonic-gate 	version_list **vtailp;
2957c478bd9Sstevel@tonic-gate 	proc_list *plist;
2967c478bd9Sstevel@tonic-gate 	proc_list **ptailp;
2977c478bd9Sstevel@tonic-gate 	int num_args;
2987c478bd9Sstevel@tonic-gate 	bool_t isvoid = FALSE;	/* whether first argument is void */
2997c478bd9Sstevel@tonic-gate 	defp->def_kind = DEF_PROGRAM;
3007c478bd9Sstevel@tonic-gate 	scan(TOK_IDENT, &tok);
3017c478bd9Sstevel@tonic-gate 	defp->def_name = tok.str;
3027c478bd9Sstevel@tonic-gate 	scan(TOK_LBRACE, &tok);
3037c478bd9Sstevel@tonic-gate 	vtailp = &defp->def.pr.versions;
3047c478bd9Sstevel@tonic-gate 	tailp = &defp->def.st.decls;
3057c478bd9Sstevel@tonic-gate 	scan(TOK_VERSION, &tok);
3067c478bd9Sstevel@tonic-gate 	do {
3077c478bd9Sstevel@tonic-gate 		scan(TOK_IDENT, &tok);
308*61961e0fSrobinson 		vlist = calloc(1, sizeof (version_list));
3097c478bd9Sstevel@tonic-gate 		vlist->vers_name = tok.str;
3107c478bd9Sstevel@tonic-gate 		scan(TOK_LBRACE, &tok);
3117c478bd9Sstevel@tonic-gate 		ptailp = &vlist->procs;
3127c478bd9Sstevel@tonic-gate 		do {
3137c478bd9Sstevel@tonic-gate 			/* get result type */
314*61961e0fSrobinson 			plist = calloc(1, sizeof (proc_list));
3157c478bd9Sstevel@tonic-gate 			get_type(&plist->res_prefix, &plist->res_type,
3167c478bd9Sstevel@tonic-gate 			    DEF_RESULT);
3177c478bd9Sstevel@tonic-gate 			if (streq(plist->res_type, "opaque")) {
3187c478bd9Sstevel@tonic-gate 				error("illegal result type");
3197c478bd9Sstevel@tonic-gate 			}
3207c478bd9Sstevel@tonic-gate 			scan(TOK_IDENT, &tok);
3217c478bd9Sstevel@tonic-gate 			plist->proc_name = tok.str;
3227c478bd9Sstevel@tonic-gate 			scan(TOK_LPAREN, &tok);
3237c478bd9Sstevel@tonic-gate 			/* get args - first one */
3247c478bd9Sstevel@tonic-gate 			num_args = 1;
3257c478bd9Sstevel@tonic-gate 			isvoid = FALSE;
3267c478bd9Sstevel@tonic-gate 			/*
3277c478bd9Sstevel@tonic-gate 			 * type of DEF_PROGRAM in the first
3287c478bd9Sstevel@tonic-gate 			 * get_prog_declaration and DEF_STURCT in the next
3297c478bd9Sstevel@tonic-gate 			 * allows void as argument if it is the only argument
3307c478bd9Sstevel@tonic-gate 			 */
3317c478bd9Sstevel@tonic-gate 			get_prog_declaration(&dec, DEF_PROGRAM, num_args);
3327c478bd9Sstevel@tonic-gate 			if (streq(dec.type, "void"))
3337c478bd9Sstevel@tonic-gate 				isvoid = TRUE;
334*61961e0fSrobinson 			decls = calloc(1, sizeof (decl_list));
3357c478bd9Sstevel@tonic-gate 			plist->args.decls = decls;
3367c478bd9Sstevel@tonic-gate 			decls->decl = dec;
3377c478bd9Sstevel@tonic-gate 			tailp = &decls->next;
3387c478bd9Sstevel@tonic-gate 			/* get args */
3397c478bd9Sstevel@tonic-gate 			while (peekscan(TOK_COMMA, &tok)) {
3407c478bd9Sstevel@tonic-gate 				num_args++;
3417c478bd9Sstevel@tonic-gate 				get_prog_declaration(&dec, DEF_STRUCT,
3427c478bd9Sstevel@tonic-gate 						    num_args);
343*61961e0fSrobinson 				decls = calloc(1, sizeof (decl_list));
3447c478bd9Sstevel@tonic-gate 				decls->decl = dec;
3457c478bd9Sstevel@tonic-gate 				*tailp = decls;
3467c478bd9Sstevel@tonic-gate 				if (streq(dec.type, "void"))
3477c478bd9Sstevel@tonic-gate 					isvoid = TRUE;
3487c478bd9Sstevel@tonic-gate 				tailp = &decls->next;
3497c478bd9Sstevel@tonic-gate 			}
3507c478bd9Sstevel@tonic-gate 			/* multiple arguments are only allowed in newstyle */
3517c478bd9Sstevel@tonic-gate 			if (!newstyle && num_args > 1) {
3527c478bd9Sstevel@tonic-gate 				error("only one argument is allowed");
3537c478bd9Sstevel@tonic-gate 			}
3547c478bd9Sstevel@tonic-gate 			if (isvoid && num_args > 1) {
3557c478bd9Sstevel@tonic-gate 				error("illegal use of void "
3567c478bd9Sstevel@tonic-gate 				    "in program definition");
3577c478bd9Sstevel@tonic-gate 			}
3587c478bd9Sstevel@tonic-gate 			*tailp = NULL;
3597c478bd9Sstevel@tonic-gate 			scan(TOK_RPAREN, &tok);
3607c478bd9Sstevel@tonic-gate 			scan(TOK_EQUAL, &tok);
3617c478bd9Sstevel@tonic-gate 			scan_num(&tok);
3627c478bd9Sstevel@tonic-gate 			scan(TOK_SEMICOLON, &tok);
3637c478bd9Sstevel@tonic-gate 			plist->proc_num = tok.str;
3647c478bd9Sstevel@tonic-gate 			plist->arg_num = num_args;
3657c478bd9Sstevel@tonic-gate 			*ptailp = plist;
3667c478bd9Sstevel@tonic-gate 			ptailp = &plist->next;
3677c478bd9Sstevel@tonic-gate 			peek(&tok);
3687c478bd9Sstevel@tonic-gate 		} while (tok.kind != TOK_RBRACE);
3697c478bd9Sstevel@tonic-gate 		*ptailp = NULL;
3707c478bd9Sstevel@tonic-gate 		*vtailp = vlist;
3717c478bd9Sstevel@tonic-gate 		vtailp = &vlist->next;
3727c478bd9Sstevel@tonic-gate 		scan(TOK_RBRACE, &tok);
3737c478bd9Sstevel@tonic-gate 		scan(TOK_EQUAL, &tok);
3747c478bd9Sstevel@tonic-gate 		scan_num(&tok);
3757c478bd9Sstevel@tonic-gate 		vlist->vers_num = tok.str;
3767c478bd9Sstevel@tonic-gate 		/* make the argument structure name for each arg */
377*61961e0fSrobinson 		for (plist = vlist->procs; plist != NULL; plist = plist->next) {
3787c478bd9Sstevel@tonic-gate 			plist->args.argname = make_argname(plist->proc_name,
3797c478bd9Sstevel@tonic-gate 							vlist->vers_num);
3807c478bd9Sstevel@tonic-gate 			/* free the memory ?? */
3817c478bd9Sstevel@tonic-gate 		}
3827c478bd9Sstevel@tonic-gate 		scan(TOK_SEMICOLON, &tok);
3837c478bd9Sstevel@tonic-gate 		scan2(TOK_VERSION, TOK_RBRACE, &tok);
3847c478bd9Sstevel@tonic-gate 	} while (tok.kind == TOK_VERSION);
3857c478bd9Sstevel@tonic-gate 	scan(TOK_EQUAL, &tok);
3867c478bd9Sstevel@tonic-gate 	scan_num(&tok);
3877c478bd9Sstevel@tonic-gate 	defp->def.pr.prog_num = tok.str;
3887c478bd9Sstevel@tonic-gate 	*vtailp = NULL;
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate 
391*61961e0fSrobinson static void
392*61961e0fSrobinson def_enum(definition *defp)
3937c478bd9Sstevel@tonic-gate {
3947c478bd9Sstevel@tonic-gate 	token tok;
3957c478bd9Sstevel@tonic-gate 	enumval_list *elist;
3967c478bd9Sstevel@tonic-gate 	enumval_list **tailp;
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	defp->def_kind = DEF_ENUM;
3997c478bd9Sstevel@tonic-gate 	scan(TOK_IDENT, &tok);
4007c478bd9Sstevel@tonic-gate 	defp->def_name = tok.str;
4017c478bd9Sstevel@tonic-gate 	scan(TOK_LBRACE, &tok);
4027c478bd9Sstevel@tonic-gate 	tailp = &defp->def.en.vals;
4037c478bd9Sstevel@tonic-gate 	do {
4047c478bd9Sstevel@tonic-gate 		scan(TOK_IDENT, &tok);
405*61961e0fSrobinson 		elist = calloc(1, sizeof (enumval_list));
4067c478bd9Sstevel@tonic-gate 		elist->name = tok.str;
4077c478bd9Sstevel@tonic-gate 		elist->assignment = NULL;
4087c478bd9Sstevel@tonic-gate 		scan3(TOK_COMMA, TOK_RBRACE, TOK_EQUAL, &tok);
4097c478bd9Sstevel@tonic-gate 		if (tok.kind == TOK_EQUAL) {
4107c478bd9Sstevel@tonic-gate 			scan_num(&tok);
4117c478bd9Sstevel@tonic-gate 			elist->assignment = tok.str;
4127c478bd9Sstevel@tonic-gate 			scan2(TOK_COMMA, TOK_RBRACE, &tok);
4137c478bd9Sstevel@tonic-gate 		}
4147c478bd9Sstevel@tonic-gate 		*tailp = elist;
4157c478bd9Sstevel@tonic-gate 		tailp = &elist->next;
4167c478bd9Sstevel@tonic-gate 	} while (tok.kind != TOK_RBRACE);
4177c478bd9Sstevel@tonic-gate 	*tailp = NULL;
4187c478bd9Sstevel@tonic-gate }
4197c478bd9Sstevel@tonic-gate 
420*61961e0fSrobinson static void
421*61961e0fSrobinson def_const(definition *defp)
4227c478bd9Sstevel@tonic-gate {
4237c478bd9Sstevel@tonic-gate 	token tok;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	defp->def_kind = DEF_CONST;
4267c478bd9Sstevel@tonic-gate 	scan(TOK_IDENT, &tok);
4277c478bd9Sstevel@tonic-gate 	defp->def_name = tok.str;
4287c478bd9Sstevel@tonic-gate 	scan(TOK_EQUAL, &tok);
4297c478bd9Sstevel@tonic-gate 	scan2(TOK_IDENT, TOK_STRCONST, &tok);
4307c478bd9Sstevel@tonic-gate 	defp->def.co = tok.str;
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate 
433*61961e0fSrobinson static void
434*61961e0fSrobinson def_union(definition *defp)
4357c478bd9Sstevel@tonic-gate {
4367c478bd9Sstevel@tonic-gate 	token tok;
4377c478bd9Sstevel@tonic-gate 	declaration dec;
438*61961e0fSrobinson 	case_list *cases;
4397c478bd9Sstevel@tonic-gate 	case_list **tailp;
4407c478bd9Sstevel@tonic-gate 	int flag;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	defp->def_kind = DEF_UNION;
4437c478bd9Sstevel@tonic-gate 	scan(TOK_IDENT, &tok);
4447c478bd9Sstevel@tonic-gate 	defp->def_name = tok.str;
4457c478bd9Sstevel@tonic-gate 	scan(TOK_SWITCH, &tok);
4467c478bd9Sstevel@tonic-gate 	scan(TOK_LPAREN, &tok);
4477c478bd9Sstevel@tonic-gate 	get_declaration(&dec, DEF_UNION);
4487c478bd9Sstevel@tonic-gate 	defp->def.un.enum_decl = dec;
4497c478bd9Sstevel@tonic-gate 	tailp = &defp->def.un.cases;
4507c478bd9Sstevel@tonic-gate 	scan(TOK_RPAREN, &tok);
4517c478bd9Sstevel@tonic-gate 	scan(TOK_LBRACE, &tok);
4527c478bd9Sstevel@tonic-gate 	scan(TOK_CASE, &tok);
4537c478bd9Sstevel@tonic-gate 	while (tok.kind == TOK_CASE) {
4547c478bd9Sstevel@tonic-gate 		scan2(TOK_IDENT, TOK_CHARCONST, &tok);
455*61961e0fSrobinson 		cases = calloc(1, sizeof (case_list));
4567c478bd9Sstevel@tonic-gate 		cases->case_name = tok.str;
4577c478bd9Sstevel@tonic-gate 		scan(TOK_COLON, &tok);
4587c478bd9Sstevel@tonic-gate 		/* now peek at next token */
4597c478bd9Sstevel@tonic-gate 		flag = 0;
4607c478bd9Sstevel@tonic-gate 		if (peekscan(TOK_CASE, &tok)) {
4617c478bd9Sstevel@tonic-gate 			do {
4627c478bd9Sstevel@tonic-gate 				scan2(TOK_IDENT, TOK_CHARCONST, &tok);
4637c478bd9Sstevel@tonic-gate 				cases->contflag = 1;
4647c478bd9Sstevel@tonic-gate 				/* continued case statement */
4657c478bd9Sstevel@tonic-gate 				*tailp = cases;
4667c478bd9Sstevel@tonic-gate 				tailp = &cases->next;
467*61961e0fSrobinson 				cases = calloc(1, sizeof (case_list));
4687c478bd9Sstevel@tonic-gate 				cases->case_name = tok.str;
4697c478bd9Sstevel@tonic-gate 				scan(TOK_COLON, &tok);
4707c478bd9Sstevel@tonic-gate 			} while (peekscan(TOK_CASE, &tok));
471*61961e0fSrobinson 		} else if (flag) {
4727c478bd9Sstevel@tonic-gate 			*tailp = cases;
4737c478bd9Sstevel@tonic-gate 			tailp = &cases->next;
474*61961e0fSrobinson 			cases = calloc(1, sizeof (case_list));
475*61961e0fSrobinson 		}
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 		get_declaration(&dec, DEF_UNION);
4787c478bd9Sstevel@tonic-gate 		cases->case_decl = dec;
4797c478bd9Sstevel@tonic-gate 		cases->contflag = 0; /* no continued case statement */
4807c478bd9Sstevel@tonic-gate 		*tailp = cases;
4817c478bd9Sstevel@tonic-gate 		tailp = &cases->next;
4827c478bd9Sstevel@tonic-gate 		scan(TOK_SEMICOLON, &tok);
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 		scan3(TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok);
4857c478bd9Sstevel@tonic-gate 	}
4867c478bd9Sstevel@tonic-gate 	*tailp = NULL;
4877c478bd9Sstevel@tonic-gate 	if (tok.kind == TOK_DEFAULT) {
4887c478bd9Sstevel@tonic-gate 		scan(TOK_COLON, &tok);
4897c478bd9Sstevel@tonic-gate 		get_declaration(&dec, DEF_UNION);
490*61961e0fSrobinson 		defp->def.un.default_decl = calloc(1, sizeof (declaration));
4917c478bd9Sstevel@tonic-gate 		*defp->def.un.default_decl = dec;
4927c478bd9Sstevel@tonic-gate 		scan(TOK_SEMICOLON, &tok);
4937c478bd9Sstevel@tonic-gate 		scan(TOK_RBRACE, &tok);
4947c478bd9Sstevel@tonic-gate 	} else {
4957c478bd9Sstevel@tonic-gate 		defp->def.un.default_decl = NULL;
4967c478bd9Sstevel@tonic-gate 	}
4977c478bd9Sstevel@tonic-gate }
4987c478bd9Sstevel@tonic-gate 
499*61961e0fSrobinson static char *reserved_words[] = {
5007c478bd9Sstevel@tonic-gate 	"array",
5017c478bd9Sstevel@tonic-gate 	"bytes",
5027c478bd9Sstevel@tonic-gate 	"destroy",
5037c478bd9Sstevel@tonic-gate 	"free",
5047c478bd9Sstevel@tonic-gate 	"getpos",
5057c478bd9Sstevel@tonic-gate 	"inline",
5067c478bd9Sstevel@tonic-gate 	"pointer",
5077c478bd9Sstevel@tonic-gate 	"reference",
5087c478bd9Sstevel@tonic-gate 	"setpos",
5097c478bd9Sstevel@tonic-gate 	"sizeof",
5107c478bd9Sstevel@tonic-gate 	"union",
5117c478bd9Sstevel@tonic-gate 	"vector",
5127c478bd9Sstevel@tonic-gate 	NULL
5137c478bd9Sstevel@tonic-gate };
5147c478bd9Sstevel@tonic-gate 
515*61961e0fSrobinson static char *reserved_types[] = {
5167c478bd9Sstevel@tonic-gate 	"opaque",
5177c478bd9Sstevel@tonic-gate 	"string",
5187c478bd9Sstevel@tonic-gate 	NULL
5197c478bd9Sstevel@tonic-gate };
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate /*
5227c478bd9Sstevel@tonic-gate  * check that the given name is not one that would eventually result in
5237c478bd9Sstevel@tonic-gate  * xdr routines that would conflict with internal XDR routines.
5247c478bd9Sstevel@tonic-gate  */
525*61961e0fSrobinson static void
526*61961e0fSrobinson check_type_name(char *name, int new_type)
5277c478bd9Sstevel@tonic-gate {
5287c478bd9Sstevel@tonic-gate 	int i;
5297c478bd9Sstevel@tonic-gate 	char tmp[100];
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	for (i = 0; reserved_words[i] != NULL; i++) {
5327c478bd9Sstevel@tonic-gate 		if (strcmp(name, reserved_words[i]) == 0) {
533*61961e0fSrobinson 			(void) snprintf(tmp, sizeof (tmp),
5347c478bd9Sstevel@tonic-gate 				"illegal (reserved) name :\'%s\' "
5357c478bd9Sstevel@tonic-gate 				"in type definition",
5367c478bd9Sstevel@tonic-gate 				name);
5377c478bd9Sstevel@tonic-gate 			error(tmp);
5387c478bd9Sstevel@tonic-gate 		}
5397c478bd9Sstevel@tonic-gate 	}
5407c478bd9Sstevel@tonic-gate 	if (new_type) {
5417c478bd9Sstevel@tonic-gate 		for (i = 0; reserved_types[i] != NULL; i++) {
5427c478bd9Sstevel@tonic-gate 			if (strcmp(name, reserved_types[i]) == 0) {
543*61961e0fSrobinson 				(void) snprintf(tmp, sizeof (tmp),
5447c478bd9Sstevel@tonic-gate 					"illegal (reserved) name :\'%s\' "
5457c478bd9Sstevel@tonic-gate 					"in type definition",
5467c478bd9Sstevel@tonic-gate 					name);
5477c478bd9Sstevel@tonic-gate 				error(tmp);
5487c478bd9Sstevel@tonic-gate 			}
5497c478bd9Sstevel@tonic-gate 		}
5507c478bd9Sstevel@tonic-gate 	}
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate 
553*61961e0fSrobinson static void
554*61961e0fSrobinson def_typedef(definition *defp)
5557c478bd9Sstevel@tonic-gate {
5567c478bd9Sstevel@tonic-gate 	declaration dec;
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	defp->def_kind = DEF_TYPEDEF;
5597c478bd9Sstevel@tonic-gate 	get_declaration(&dec, DEF_TYPEDEF);
5607c478bd9Sstevel@tonic-gate 	defp->def_name = dec.name;
5617c478bd9Sstevel@tonic-gate 	check_type_name(dec.name, 1);
5627c478bd9Sstevel@tonic-gate 	defp->def.ty.old_prefix = dec.prefix;
5637c478bd9Sstevel@tonic-gate 	defp->def.ty.old_type = dec.type;
5647c478bd9Sstevel@tonic-gate 	defp->def.ty.rel = dec.rel;
5657c478bd9Sstevel@tonic-gate 	defp->def.ty.array_max = dec.array_max;
5667c478bd9Sstevel@tonic-gate }
5677c478bd9Sstevel@tonic-gate 
568*61961e0fSrobinson static void
569*61961e0fSrobinson get_declaration(declaration *dec, defkind dkind)
5707c478bd9Sstevel@tonic-gate {
5717c478bd9Sstevel@tonic-gate 	token tok;
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 	get_type(&dec->prefix, &dec->type, dkind);
5747c478bd9Sstevel@tonic-gate 	dec->rel = REL_ALIAS;
575*61961e0fSrobinson 	if (streq(dec->type, "void"))
5767c478bd9Sstevel@tonic-gate 		return;
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate 	check_type_name(dec->type, 0);
5797c478bd9Sstevel@tonic-gate 	scan2(TOK_STAR, TOK_IDENT, &tok);
5807c478bd9Sstevel@tonic-gate 	if (tok.kind == TOK_STAR) {
5817c478bd9Sstevel@tonic-gate 		dec->rel = REL_POINTER;
5827c478bd9Sstevel@tonic-gate 		scan(TOK_IDENT, &tok);
5837c478bd9Sstevel@tonic-gate 	}
5847c478bd9Sstevel@tonic-gate 	dec->name = tok.str;
5857c478bd9Sstevel@tonic-gate 	if (peekscan(TOK_LBRACKET, &tok)) {
586*61961e0fSrobinson 		if (dec->rel == REL_POINTER)
5877c478bd9Sstevel@tonic-gate 			error("no array-of-pointer declarations "
5887c478bd9Sstevel@tonic-gate 			    "-- use typedef");
5897c478bd9Sstevel@tonic-gate 		dec->rel = REL_VECTOR;
5907c478bd9Sstevel@tonic-gate 		scan_num(&tok);
5917c478bd9Sstevel@tonic-gate 		dec->array_max = tok.str;
5927c478bd9Sstevel@tonic-gate 		scan(TOK_RBRACKET, &tok);
5937c478bd9Sstevel@tonic-gate 	} else if (peekscan(TOK_LANGLE, &tok)) {
594*61961e0fSrobinson 		if (dec->rel == REL_POINTER)
5957c478bd9Sstevel@tonic-gate 			error("no array-of-pointer declarations "
5967c478bd9Sstevel@tonic-gate 			    "-- use typedef");
5977c478bd9Sstevel@tonic-gate 		dec->rel = REL_ARRAY;
5987c478bd9Sstevel@tonic-gate 		if (peekscan(TOK_RANGLE, &tok)) {
5997c478bd9Sstevel@tonic-gate 			dec->array_max = "~0";	/* unspecified size, use max */
6007c478bd9Sstevel@tonic-gate 		} else {
6017c478bd9Sstevel@tonic-gate 			scan_num(&tok);
6027c478bd9Sstevel@tonic-gate 			dec->array_max = tok.str;
6037c478bd9Sstevel@tonic-gate 			scan(TOK_RANGLE, &tok);
6047c478bd9Sstevel@tonic-gate 		}
6057c478bd9Sstevel@tonic-gate 	}
6067c478bd9Sstevel@tonic-gate 	if (streq(dec->type, "opaque")) {
6077c478bd9Sstevel@tonic-gate 		if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR) {
6087c478bd9Sstevel@tonic-gate 			error("array declaration expected");
6097c478bd9Sstevel@tonic-gate 		}
6107c478bd9Sstevel@tonic-gate 	} else if (streq(dec->type, "string")) {
6117c478bd9Sstevel@tonic-gate 		if (dec->rel != REL_ARRAY) {
6127c478bd9Sstevel@tonic-gate 			error("variable-length array declaration expected");
6137c478bd9Sstevel@tonic-gate 		}
6147c478bd9Sstevel@tonic-gate 	}
6157c478bd9Sstevel@tonic-gate }
6167c478bd9Sstevel@tonic-gate 
617*61961e0fSrobinson static void
618*61961e0fSrobinson get_prog_declaration(declaration *dec, defkind dkind, int num)
6197c478bd9Sstevel@tonic-gate {
6207c478bd9Sstevel@tonic-gate 	token tok;
6217c478bd9Sstevel@tonic-gate 	char name[sizeof (ARGNAME) + 10];
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	if (dkind == DEF_PROGRAM) {
6247c478bd9Sstevel@tonic-gate 		peek(&tok);
6257c478bd9Sstevel@tonic-gate 		if (tok.kind == TOK_RPAREN) { /* no arguments */
6267c478bd9Sstevel@tonic-gate 			dec->rel = REL_ALIAS;
6277c478bd9Sstevel@tonic-gate 			dec->type = "void";
6287c478bd9Sstevel@tonic-gate 			dec->prefix = NULL;
6297c478bd9Sstevel@tonic-gate 			dec->name = NULL;
6307c478bd9Sstevel@tonic-gate 			return;
6317c478bd9Sstevel@tonic-gate 		}
6327c478bd9Sstevel@tonic-gate 	}
6337c478bd9Sstevel@tonic-gate 	get_type(&dec->prefix, &dec->type, dkind);
6347c478bd9Sstevel@tonic-gate 	dec->rel = REL_ALIAS;
6357c478bd9Sstevel@tonic-gate 	if (peekscan(TOK_IDENT, &tok)) /* optional name of argument */
6367c478bd9Sstevel@tonic-gate 		dec->name = strdup(tok.str);
6377c478bd9Sstevel@tonic-gate 	else {
6387c478bd9Sstevel@tonic-gate 		/* default name of argument */
639*61961e0fSrobinson 		(void) snprintf(name, sizeof (name), "%s%d", ARGNAME, num);
6407c478bd9Sstevel@tonic-gate 		dec->name = strdup(name);
6417c478bd9Sstevel@tonic-gate 	}
6427c478bd9Sstevel@tonic-gate 	if (dec->name == NULL)
6437c478bd9Sstevel@tonic-gate 		error("internal error -- out of memory");
6447c478bd9Sstevel@tonic-gate 
645*61961e0fSrobinson 	if (streq(dec->type, "void"))
6467c478bd9Sstevel@tonic-gate 		return;
6477c478bd9Sstevel@tonic-gate 
648*61961e0fSrobinson 	if (streq(dec->type, "opaque"))
6497c478bd9Sstevel@tonic-gate 		error("opaque -- illegal argument type");
6507c478bd9Sstevel@tonic-gate 	if (peekscan(TOK_STAR, &tok)) {
6517c478bd9Sstevel@tonic-gate 		if (streq(dec->type, "string")) {
6527c478bd9Sstevel@tonic-gate 			error("pointer to string not allowed "
6537c478bd9Sstevel@tonic-gate 			    "in program arguments\n");
6547c478bd9Sstevel@tonic-gate 		}
6557c478bd9Sstevel@tonic-gate 		dec->rel = REL_POINTER;
6567c478bd9Sstevel@tonic-gate 		if (peekscan(TOK_IDENT, &tok))
6577c478bd9Sstevel@tonic-gate 			/* optional name of argument */
6587c478bd9Sstevel@tonic-gate 			dec->name = strdup(tok.str);
6597c478bd9Sstevel@tonic-gate 	}
6607c478bd9Sstevel@tonic-gate 	if (peekscan(TOK_LANGLE, &tok)) {
6617c478bd9Sstevel@tonic-gate 		if (!streq(dec->type, "string")) {
6627c478bd9Sstevel@tonic-gate 			error("arrays cannot be declared as arguments "
6637c478bd9Sstevel@tonic-gate 			    "to procedures -- use typedef");
6647c478bd9Sstevel@tonic-gate 		}
6657c478bd9Sstevel@tonic-gate 		dec->rel = REL_ARRAY;
6667c478bd9Sstevel@tonic-gate 		if (peekscan(TOK_RANGLE, &tok)) {
6677c478bd9Sstevel@tonic-gate 			dec->array_max = "~0";
6687c478bd9Sstevel@tonic-gate 			/* unspecified size, use max */
6697c478bd9Sstevel@tonic-gate 		} else {
6707c478bd9Sstevel@tonic-gate 			scan_num(&tok);
6717c478bd9Sstevel@tonic-gate 			dec->array_max = tok.str;
6727c478bd9Sstevel@tonic-gate 			scan(TOK_RANGLE, &tok);
6737c478bd9Sstevel@tonic-gate 		}
6747c478bd9Sstevel@tonic-gate 	}
6757c478bd9Sstevel@tonic-gate 	if (streq(dec->type, "string")) {
6767c478bd9Sstevel@tonic-gate 		if (dec->rel != REL_ARRAY) {
6777c478bd9Sstevel@tonic-gate 			/*
6787c478bd9Sstevel@tonic-gate 			 * .x specifies just string as
6797c478bd9Sstevel@tonic-gate 			 * type of argument
6807c478bd9Sstevel@tonic-gate 			 * - make it string<>
6817c478bd9Sstevel@tonic-gate 			 */
6827c478bd9Sstevel@tonic-gate 			dec->rel = REL_ARRAY;
6837c478bd9Sstevel@tonic-gate 			dec->array_max = "~0"; /* unspecified size, use max */
6847c478bd9Sstevel@tonic-gate 		}
6857c478bd9Sstevel@tonic-gate 	}
6867c478bd9Sstevel@tonic-gate }
6877c478bd9Sstevel@tonic-gate 
688*61961e0fSrobinson static void
689*61961e0fSrobinson get_type(char **prefixp, char **typep, defkind dkind)
6907c478bd9Sstevel@tonic-gate {
6917c478bd9Sstevel@tonic-gate 	token tok;
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	*prefixp = NULL;
6947c478bd9Sstevel@tonic-gate 	get_token(&tok);
6957c478bd9Sstevel@tonic-gate 	switch (tok.kind) {
6967c478bd9Sstevel@tonic-gate 	case TOK_IDENT:
6977c478bd9Sstevel@tonic-gate 		*typep = tok.str;
6987c478bd9Sstevel@tonic-gate 		break;
6997c478bd9Sstevel@tonic-gate 	case TOK_STRUCT:
7007c478bd9Sstevel@tonic-gate 	case TOK_ENUM:
7017c478bd9Sstevel@tonic-gate 	case TOK_UNION:
7027c478bd9Sstevel@tonic-gate 		*prefixp = tok.str;
7037c478bd9Sstevel@tonic-gate 		scan(TOK_IDENT, &tok);
7047c478bd9Sstevel@tonic-gate 		*typep = tok.str;
7057c478bd9Sstevel@tonic-gate 		break;
7067c478bd9Sstevel@tonic-gate 	case TOK_UNSIGNED:
7077c478bd9Sstevel@tonic-gate 		unsigned_dec(typep);
7087c478bd9Sstevel@tonic-gate 		break;
7097c478bd9Sstevel@tonic-gate 	case TOK_SHORT:
7107c478bd9Sstevel@tonic-gate 		*typep = "short";
7117c478bd9Sstevel@tonic-gate 		(void) peekscan(TOK_INT, &tok);
7127c478bd9Sstevel@tonic-gate 		break;
7137c478bd9Sstevel@tonic-gate 	case TOK_LONG:
7147c478bd9Sstevel@tonic-gate 		*typep = "long";
7157c478bd9Sstevel@tonic-gate 		(void) peekscan(TOK_INT, &tok);
7167c478bd9Sstevel@tonic-gate 		break;
7177c478bd9Sstevel@tonic-gate 	case TOK_HYPER:
7187c478bd9Sstevel@tonic-gate 		*typep = "longlong_t";
7197c478bd9Sstevel@tonic-gate 		(void) peekscan(TOK_INT, &tok);
7207c478bd9Sstevel@tonic-gate 		break;
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	case TOK_VOID:
7237c478bd9Sstevel@tonic-gate 		if (dkind != DEF_UNION && dkind != DEF_PROGRAM &&
7247c478bd9Sstevel@tonic-gate 		    dkind != DEF_RESULT) {
7257c478bd9Sstevel@tonic-gate 			error("voids allowed only inside union and "
7267c478bd9Sstevel@tonic-gate 			    "program definitions with one argument");
7277c478bd9Sstevel@tonic-gate 		}
7287c478bd9Sstevel@tonic-gate 		*typep = tok.str;
7297c478bd9Sstevel@tonic-gate 		break;
7307c478bd9Sstevel@tonic-gate 	case TOK_ONEWAY:
7317c478bd9Sstevel@tonic-gate 		if (dkind != DEF_RESULT) {
7327c478bd9Sstevel@tonic-gate 			error("oneways allowed only inside result definitions");
7337c478bd9Sstevel@tonic-gate 		}
7347c478bd9Sstevel@tonic-gate 		*typep = tok.str;
7357c478bd9Sstevel@tonic-gate 		break;
7367c478bd9Sstevel@tonic-gate 	case TOK_STRING:
7377c478bd9Sstevel@tonic-gate 	case TOK_OPAQUE:
7387c478bd9Sstevel@tonic-gate 	case TOK_CHAR:
7397c478bd9Sstevel@tonic-gate 	case TOK_INT:
7407c478bd9Sstevel@tonic-gate 	case TOK_FLOAT:
7417c478bd9Sstevel@tonic-gate 	case TOK_DOUBLE:
7427c478bd9Sstevel@tonic-gate 	case TOK_BOOL:
7437c478bd9Sstevel@tonic-gate 	case TOK_QUAD:
7447c478bd9Sstevel@tonic-gate 		*typep = tok.str;
7457c478bd9Sstevel@tonic-gate 		break;
7467c478bd9Sstevel@tonic-gate 	default:
7477c478bd9Sstevel@tonic-gate 		error("expected type specifier");
7487c478bd9Sstevel@tonic-gate 	}
7497c478bd9Sstevel@tonic-gate }
7507c478bd9Sstevel@tonic-gate 
751*61961e0fSrobinson static void
752*61961e0fSrobinson unsigned_dec(char **typep)
7537c478bd9Sstevel@tonic-gate {
7547c478bd9Sstevel@tonic-gate 	token tok;
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 	peek(&tok);
7577c478bd9Sstevel@tonic-gate 	switch (tok.kind) {
7587c478bd9Sstevel@tonic-gate 	case TOK_CHAR:
7597c478bd9Sstevel@tonic-gate 		get_token(&tok);
7607c478bd9Sstevel@tonic-gate 		*typep = "u_char";
7617c478bd9Sstevel@tonic-gate 		break;
7627c478bd9Sstevel@tonic-gate 	case TOK_SHORT:
7637c478bd9Sstevel@tonic-gate 		get_token(&tok);
7647c478bd9Sstevel@tonic-gate 		*typep = "u_short";
7657c478bd9Sstevel@tonic-gate 		(void) peekscan(TOK_INT, &tok);
7667c478bd9Sstevel@tonic-gate 		break;
7677c478bd9Sstevel@tonic-gate 	case TOK_LONG:
7687c478bd9Sstevel@tonic-gate 		get_token(&tok);
7697c478bd9Sstevel@tonic-gate 		*typep = "u_long";
7707c478bd9Sstevel@tonic-gate 		(void) peekscan(TOK_INT, &tok);
7717c478bd9Sstevel@tonic-gate 		break;
7727c478bd9Sstevel@tonic-gate 	case TOK_HYPER:
7737c478bd9Sstevel@tonic-gate 		get_token(&tok);
7747c478bd9Sstevel@tonic-gate 		*typep = "u_longlong_t";
7757c478bd9Sstevel@tonic-gate 		(void) peekscan(TOK_INT, &tok);
7767c478bd9Sstevel@tonic-gate 		break;
7777c478bd9Sstevel@tonic-gate 	case TOK_INT:
7787c478bd9Sstevel@tonic-gate 		get_token(&tok);
7797c478bd9Sstevel@tonic-gate 		*typep = "u_int";
7807c478bd9Sstevel@tonic-gate 		break;
7817c478bd9Sstevel@tonic-gate 	default:
7827c478bd9Sstevel@tonic-gate 		*typep = "u_int";
7837c478bd9Sstevel@tonic-gate 		break;
7847c478bd9Sstevel@tonic-gate 	}
7857c478bd9Sstevel@tonic-gate }
786