14e115012SGarrett Wollman /* 24e115012SGarrett Wollman * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 34e115012SGarrett Wollman * unrestricted use provided that this legend is included on all tape 44e115012SGarrett Wollman * media and as a part of the software program in whole or part. Users 54e115012SGarrett Wollman * may copy or modify Sun RPC without charge, but are not authorized 64e115012SGarrett Wollman * to license or distribute it to anyone else except as part of a product or 74e115012SGarrett Wollman * program developed by the user. 84e115012SGarrett Wollman * 94e115012SGarrett Wollman * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 104e115012SGarrett Wollman * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 114e115012SGarrett Wollman * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 124e115012SGarrett Wollman * 134e115012SGarrett Wollman * Sun RPC is provided with no support and without any obligation on the 144e115012SGarrett Wollman * part of Sun Microsystems, Inc. to assist in its use, correction, 154e115012SGarrett Wollman * modification or enhancement. 164e115012SGarrett Wollman * 174e115012SGarrett Wollman * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 184e115012SGarrett Wollman * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 194e115012SGarrett Wollman * OR ANY PART THEREOF. 204e115012SGarrett Wollman * 214e115012SGarrett Wollman * In no event will Sun Microsystems, Inc. be liable for any lost revenue 224e115012SGarrett Wollman * or profits or other special, indirect and consequential damages, even if 234e115012SGarrett Wollman * Sun has been advised of the possibility of such damages. 244e115012SGarrett Wollman * 254e115012SGarrett Wollman * Sun Microsystems, Inc. 264e115012SGarrett Wollman * 2550 Garcia Avenue 274e115012SGarrett Wollman * Mountain View, California 94043 284e115012SGarrett Wollman */ 29ff49530fSBill Paul 30ff49530fSBill Paul #ident "@(#)rpc_parse.c 1.12 93/07/05 SMI" 31ff49530fSBill Paul 3240ad8885SAlfred Perlstein #if 0 3375863a6dSPhilippe Charnier #ifndef lint 34ff49530fSBill Paul static char sccsid[] = "@(#)rpc_parse.c 1.8 89/02/22 (C) 1987 SMI"; 354e115012SGarrett Wollman #endif 3640ad8885SAlfred Perlstein #endif 374e115012SGarrett Wollman 3875863a6dSPhilippe Charnier #include <sys/cdefs.h> 3975863a6dSPhilippe Charnier __FBSDID("$FreeBSD$"); 4075863a6dSPhilippe Charnier 414e115012SGarrett Wollman /* 424e115012SGarrett Wollman * rpc_parse.c, Parser for the RPC protocol compiler 434e115012SGarrett Wollman * Copyright (C) 1987 Sun Microsystems, Inc. 444e115012SGarrett Wollman */ 454e115012SGarrett Wollman #include <stdio.h> 46ff49530fSBill Paul #include <string.h> 47ff49530fSBill Paul #include "rpc/types.h" 484e115012SGarrett Wollman #include "rpc_scan.h" 494e115012SGarrett Wollman #include "rpc_parse.h" 50ff49530fSBill Paul #include "rpc_util.h" 514e115012SGarrett Wollman 52ff49530fSBill Paul #define ARGNAME "arg" 53ff49530fSBill Paul 54d3cb5dedSWarner Losh extern char *make_argname( char *, char * ); 55d3cb5dedSWarner Losh static void isdefined( definition * ); 56d3cb5dedSWarner Losh static void def_struct( definition * ); 57d3cb5dedSWarner Losh static void def_program( definition * ); 58d3cb5dedSWarner Losh static void def_enum( definition * ); 59d3cb5dedSWarner Losh static void def_const( definition * ); 60d3cb5dedSWarner Losh static void def_union( definition * ); 61d3cb5dedSWarner Losh static void def_typedef( definition * ); 62d3cb5dedSWarner Losh static void get_declaration( declaration *, defkind ); 63d3cb5dedSWarner Losh static void get_prog_declaration( declaration *, defkind, int ); 64d3cb5dedSWarner Losh static void get_type( char **, char **, defkind ); 65d3cb5dedSWarner Losh static void unsigned_dec( char ** ); 66ff49530fSBill Paul 674e115012SGarrett Wollman /* 684e115012SGarrett Wollman * return the next definition you see 694e115012SGarrett Wollman */ 704e115012SGarrett Wollman definition * 714e115012SGarrett Wollman get_definition() 724e115012SGarrett Wollman { 734e115012SGarrett Wollman definition *defp; 744e115012SGarrett Wollman token tok; 754e115012SGarrett Wollman 7675863a6dSPhilippe Charnier defp = XALLOC(definition); 774e115012SGarrett Wollman get_token(&tok); 784e115012SGarrett Wollman switch (tok.kind) { 794e115012SGarrett Wollman case TOK_STRUCT: 804e115012SGarrett Wollman def_struct(defp); 814e115012SGarrett Wollman break; 824e115012SGarrett Wollman case TOK_UNION: 834e115012SGarrett Wollman def_union(defp); 844e115012SGarrett Wollman break; 854e115012SGarrett Wollman case TOK_TYPEDEF: 864e115012SGarrett Wollman def_typedef(defp); 874e115012SGarrett Wollman break; 884e115012SGarrett Wollman case TOK_ENUM: 894e115012SGarrett Wollman def_enum(defp); 904e115012SGarrett Wollman break; 914e115012SGarrett Wollman case TOK_PROGRAM: 924e115012SGarrett Wollman def_program(defp); 934e115012SGarrett Wollman break; 944e115012SGarrett Wollman case TOK_CONST: 954e115012SGarrett Wollman def_const(defp); 964e115012SGarrett Wollman break; 974e115012SGarrett Wollman case TOK_EOF: 984e115012SGarrett Wollman return (NULL); 994e115012SGarrett Wollman default: 1004e115012SGarrett Wollman error("definition keyword expected"); 1014e115012SGarrett Wollman } 1024e115012SGarrett Wollman scan(TOK_SEMICOLON, &tok); 1034e115012SGarrett Wollman isdefined(defp); 1044e115012SGarrett Wollman return (defp); 1054e115012SGarrett Wollman } 1064e115012SGarrett Wollman 107526195adSJordan K. Hubbard static void 1084e115012SGarrett Wollman isdefined(defp) 1094e115012SGarrett Wollman definition *defp; 1104e115012SGarrett Wollman { 1114e115012SGarrett Wollman STOREVAL(&defined, defp); 1124e115012SGarrett Wollman } 1134e115012SGarrett Wollman 114526195adSJordan K. Hubbard static void 1154e115012SGarrett Wollman def_struct(defp) 1164e115012SGarrett Wollman definition *defp; 1174e115012SGarrett Wollman { 1184e115012SGarrett Wollman token tok; 1194e115012SGarrett Wollman declaration dec; 1204e115012SGarrett Wollman decl_list *decls; 1214e115012SGarrett Wollman decl_list **tailp; 1224e115012SGarrett Wollman 1234e115012SGarrett Wollman defp->def_kind = DEF_STRUCT; 1244e115012SGarrett Wollman 1254e115012SGarrett Wollman scan(TOK_IDENT, &tok); 1264e115012SGarrett Wollman defp->def_name = tok.str; 1274e115012SGarrett Wollman scan(TOK_LBRACE, &tok); 1284e115012SGarrett Wollman tailp = &defp->def.st.decls; 1294e115012SGarrett Wollman do { 1304e115012SGarrett Wollman get_declaration(&dec, DEF_STRUCT); 13175863a6dSPhilippe Charnier decls = XALLOC(decl_list); 1324e115012SGarrett Wollman decls->decl = dec; 1334e115012SGarrett Wollman *tailp = decls; 1344e115012SGarrett Wollman tailp = &decls->next; 1354e115012SGarrett Wollman scan(TOK_SEMICOLON, &tok); 1364e115012SGarrett Wollman peek(&tok); 1374e115012SGarrett Wollman } while (tok.kind != TOK_RBRACE); 1384e115012SGarrett Wollman get_token(&tok); 1394e115012SGarrett Wollman *tailp = NULL; 1404e115012SGarrett Wollman } 1414e115012SGarrett Wollman 142526195adSJordan K. Hubbard static void 1434e115012SGarrett Wollman def_program(defp) 1444e115012SGarrett Wollman definition *defp; 1454e115012SGarrett Wollman { 1464e115012SGarrett Wollman token tok; 147ff49530fSBill Paul declaration dec; 148ff49530fSBill Paul decl_list *decls; 149ff49530fSBill Paul decl_list **tailp; 1504e115012SGarrett Wollman version_list *vlist; 1514e115012SGarrett Wollman version_list **vtailp; 1524e115012SGarrett Wollman proc_list *plist; 1534e115012SGarrett Wollman proc_list **ptailp; 154ff49530fSBill Paul int num_args; 155ff49530fSBill Paul bool_t isvoid = FALSE; /* whether first argument is void */ 1564e115012SGarrett Wollman defp->def_kind = DEF_PROGRAM; 1574e115012SGarrett Wollman scan(TOK_IDENT, &tok); 1584e115012SGarrett Wollman defp->def_name = tok.str; 1594e115012SGarrett Wollman scan(TOK_LBRACE, &tok); 1604e115012SGarrett Wollman vtailp = &defp->def.pr.versions; 161ff49530fSBill Paul tailp = &defp->def.st.decls; 1624e115012SGarrett Wollman scan(TOK_VERSION, &tok); 1634e115012SGarrett Wollman do { 1644e115012SGarrett Wollman scan(TOK_IDENT, &tok); 16575863a6dSPhilippe Charnier vlist = XALLOC(version_list); 1664e115012SGarrett Wollman vlist->vers_name = tok.str; 1674e115012SGarrett Wollman scan(TOK_LBRACE, &tok); 1684e115012SGarrett Wollman ptailp = &vlist->procs; 1694e115012SGarrett Wollman do { 170ff49530fSBill Paul /* get result type */ 17175863a6dSPhilippe Charnier plist = XALLOC(proc_list); 172ff49530fSBill Paul get_type(&plist->res_prefix, &plist->res_type, 173ff49530fSBill Paul DEF_PROGRAM); 1744e115012SGarrett Wollman if (streq(plist->res_type, "opaque")) { 1754e115012SGarrett Wollman error("illegal result type"); 1764e115012SGarrett Wollman } 1774e115012SGarrett Wollman scan(TOK_IDENT, &tok); 1784e115012SGarrett Wollman plist->proc_name = tok.str; 1794e115012SGarrett Wollman scan(TOK_LPAREN, &tok); 180ff49530fSBill Paul /* get args - first one */ 181ff49530fSBill Paul num_args = 1; 182ff49530fSBill Paul isvoid = FALSE; 183ff49530fSBill Paul /* 184ff49530fSBill Paul * type of DEF_PROGRAM in the first 185ff49530fSBill Paul * get_prog_declaration and DEF_STURCT in the next 186ff49530fSBill Paul * allows void as argument if it is the only argument 187ff49530fSBill Paul */ 188ff49530fSBill Paul get_prog_declaration(&dec, DEF_PROGRAM, num_args); 189ff49530fSBill Paul if (streq(dec.type, "void")) 190ff49530fSBill Paul isvoid = TRUE; 19175863a6dSPhilippe Charnier decls = XALLOC(decl_list); 192ff49530fSBill Paul plist->args.decls = decls; 193ff49530fSBill Paul decls->decl = dec; 194ff49530fSBill Paul tailp = &decls->next; 195ff49530fSBill Paul /* get args */ 196ff49530fSBill Paul while (peekscan(TOK_COMMA, &tok)) { 197ff49530fSBill Paul num_args++; 198ff49530fSBill Paul get_prog_declaration(&dec, DEF_STRUCT, 199ff49530fSBill Paul num_args); 20075863a6dSPhilippe Charnier decls = XALLOC(decl_list); 201ff49530fSBill Paul decls->decl = dec; 202ff49530fSBill Paul *tailp = decls; 203ff49530fSBill Paul if (streq(dec.type, "void")) 204ff49530fSBill Paul isvoid = TRUE; 205ff49530fSBill Paul tailp = &decls->next; 2064e115012SGarrett Wollman } 207ff49530fSBill Paul /* multiple arguments are only allowed in newstyle */ 208ff49530fSBill Paul if (!newstyle && num_args > 1) { 209ff49530fSBill Paul error("only one argument is allowed"); 210ff49530fSBill Paul } 211ff49530fSBill Paul if (isvoid && num_args > 1) { 212ff49530fSBill Paul error("illegal use of void in program definition"); 213ff49530fSBill Paul } 214ff49530fSBill Paul *tailp = NULL; 2154e115012SGarrett Wollman scan(TOK_RPAREN, &tok); 2164e115012SGarrett Wollman scan(TOK_EQUAL, &tok); 2174e115012SGarrett Wollman scan_num(&tok); 2184e115012SGarrett Wollman scan(TOK_SEMICOLON, &tok); 2194e115012SGarrett Wollman plist->proc_num = tok.str; 220ff49530fSBill Paul plist->arg_num = num_args; 2214e115012SGarrett Wollman *ptailp = plist; 2224e115012SGarrett Wollman ptailp = &plist->next; 2234e115012SGarrett Wollman peek(&tok); 2244e115012SGarrett Wollman } while (tok.kind != TOK_RBRACE); 2250b455160SAndrey A. Chernov *ptailp = NULL; 2264e115012SGarrett Wollman *vtailp = vlist; 2274e115012SGarrett Wollman vtailp = &vlist->next; 2284e115012SGarrett Wollman scan(TOK_RBRACE, &tok); 2294e115012SGarrett Wollman scan(TOK_EQUAL, &tok); 2304e115012SGarrett Wollman scan_num(&tok); 2314e115012SGarrett Wollman vlist->vers_num = tok.str; 232ff49530fSBill Paul /* make the argument structure name for each arg */ 233ff49530fSBill Paul for (plist = vlist->procs; plist != NULL; 234ff49530fSBill Paul plist = plist->next) { 235ff49530fSBill Paul plist->args.argname = make_argname(plist->proc_name, 236ff49530fSBill Paul vlist->vers_num); 237ff49530fSBill Paul /* free the memory ?? */ 238ff49530fSBill Paul } 2394e115012SGarrett Wollman scan(TOK_SEMICOLON, &tok); 2404e115012SGarrett Wollman scan2(TOK_VERSION, TOK_RBRACE, &tok); 2414e115012SGarrett Wollman } while (tok.kind == TOK_VERSION); 2424e115012SGarrett Wollman scan(TOK_EQUAL, &tok); 2434e115012SGarrett Wollman scan_num(&tok); 2444e115012SGarrett Wollman defp->def.pr.prog_num = tok.str; 2454e115012SGarrett Wollman *vtailp = NULL; 2464e115012SGarrett Wollman } 2474e115012SGarrett Wollman 248ff49530fSBill Paul 249526195adSJordan K. Hubbard static void 2504e115012SGarrett Wollman def_enum(defp) 2514e115012SGarrett Wollman definition *defp; 2524e115012SGarrett Wollman { 2534e115012SGarrett Wollman token tok; 2544e115012SGarrett Wollman enumval_list *elist; 2554e115012SGarrett Wollman enumval_list **tailp; 2564e115012SGarrett Wollman 2574e115012SGarrett Wollman defp->def_kind = DEF_ENUM; 2584e115012SGarrett Wollman scan(TOK_IDENT, &tok); 2594e115012SGarrett Wollman defp->def_name = tok.str; 2604e115012SGarrett Wollman scan(TOK_LBRACE, &tok); 2614e115012SGarrett Wollman tailp = &defp->def.en.vals; 2624e115012SGarrett Wollman do { 2634e115012SGarrett Wollman scan(TOK_IDENT, &tok); 26475863a6dSPhilippe Charnier elist = XALLOC(enumval_list); 2654e115012SGarrett Wollman elist->name = tok.str; 2664e115012SGarrett Wollman elist->assignment = NULL; 2674e115012SGarrett Wollman scan3(TOK_COMMA, TOK_RBRACE, TOK_EQUAL, &tok); 2684e115012SGarrett Wollman if (tok.kind == TOK_EQUAL) { 2694e115012SGarrett Wollman scan_num(&tok); 2704e115012SGarrett Wollman elist->assignment = tok.str; 2714e115012SGarrett Wollman scan2(TOK_COMMA, TOK_RBRACE, &tok); 2724e115012SGarrett Wollman } 2734e115012SGarrett Wollman *tailp = elist; 2744e115012SGarrett Wollman tailp = &elist->next; 2754e115012SGarrett Wollman } while (tok.kind != TOK_RBRACE); 2764e115012SGarrett Wollman *tailp = NULL; 2774e115012SGarrett Wollman } 2784e115012SGarrett Wollman 279526195adSJordan K. Hubbard static void 2804e115012SGarrett Wollman def_const(defp) 2814e115012SGarrett Wollman definition *defp; 2824e115012SGarrett Wollman { 2834e115012SGarrett Wollman token tok; 2844e115012SGarrett Wollman 2854e115012SGarrett Wollman defp->def_kind = DEF_CONST; 2864e115012SGarrett Wollman scan(TOK_IDENT, &tok); 2874e115012SGarrett Wollman defp->def_name = tok.str; 2884e115012SGarrett Wollman scan(TOK_EQUAL, &tok); 2894e115012SGarrett Wollman scan2(TOK_IDENT, TOK_STRCONST, &tok); 2904e115012SGarrett Wollman defp->def.co = tok.str; 2914e115012SGarrett Wollman } 2924e115012SGarrett Wollman 293526195adSJordan K. Hubbard static void 2944e115012SGarrett Wollman def_union(defp) 2954e115012SGarrett Wollman definition *defp; 2964e115012SGarrett Wollman { 2974e115012SGarrett Wollman token tok; 2984e115012SGarrett Wollman declaration dec; 299526195adSJordan K. Hubbard case_list *cases; 3004e115012SGarrett Wollman case_list **tailp; 301ff49530fSBill Paul int flag; 3024e115012SGarrett Wollman 3034e115012SGarrett Wollman defp->def_kind = DEF_UNION; 3044e115012SGarrett Wollman scan(TOK_IDENT, &tok); 3054e115012SGarrett Wollman defp->def_name = tok.str; 3064e115012SGarrett Wollman scan(TOK_SWITCH, &tok); 3074e115012SGarrett Wollman scan(TOK_LPAREN, &tok); 3084e115012SGarrett Wollman get_declaration(&dec, DEF_UNION); 3094e115012SGarrett Wollman defp->def.un.enum_decl = dec; 3104e115012SGarrett Wollman tailp = &defp->def.un.cases; 3114e115012SGarrett Wollman scan(TOK_RPAREN, &tok); 3124e115012SGarrett Wollman scan(TOK_LBRACE, &tok); 3134e115012SGarrett Wollman scan(TOK_CASE, &tok); 3144e115012SGarrett Wollman while (tok.kind == TOK_CASE) { 315ff49530fSBill Paul scan2(TOK_IDENT, TOK_CHARCONST, &tok); 31675863a6dSPhilippe Charnier cases = XALLOC(case_list); 3174e115012SGarrett Wollman cases->case_name = tok.str; 3184e115012SGarrett Wollman scan(TOK_COLON, &tok); 319ff49530fSBill Paul /* now peek at next token */ 320ff49530fSBill Paul flag = 0; 321ff49530fSBill Paul if (peekscan(TOK_CASE, &tok)){ 322ff49530fSBill Paul do { 323ff49530fSBill Paul scan2(TOK_IDENT, TOK_CHARCONST, &tok); 324ff49530fSBill Paul cases->contflag = 1; 325ff49530fSBill Paul /* continued case statement */ 326ff49530fSBill Paul *tailp = cases; 327ff49530fSBill Paul tailp = &cases->next; 32875863a6dSPhilippe Charnier cases = XALLOC(case_list); 329ff49530fSBill Paul cases->case_name = tok.str; 330ff49530fSBill Paul scan(TOK_COLON, &tok); 331ff49530fSBill Paul } while (peekscan(TOK_CASE, &tok)); 332ff49530fSBill Paul } 333ff49530fSBill Paul else 334ff49530fSBill Paul if (flag) 335ff49530fSBill Paul { 336ff49530fSBill Paul 337ff49530fSBill Paul *tailp = cases; 338ff49530fSBill Paul tailp = &cases->next; 33975863a6dSPhilippe Charnier cases = XALLOC(case_list); 340ff49530fSBill Paul }; 341ff49530fSBill Paul 3424e115012SGarrett Wollman get_declaration(&dec, DEF_UNION); 3434e115012SGarrett Wollman cases->case_decl = dec; 344ff49530fSBill Paul cases->contflag = 0; /* no continued case statement */ 3454e115012SGarrett Wollman *tailp = cases; 3464e115012SGarrett Wollman tailp = &cases->next; 3474e115012SGarrett Wollman scan(TOK_SEMICOLON, &tok); 348ff49530fSBill Paul 3494e115012SGarrett Wollman scan3(TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok); 3504e115012SGarrett Wollman } 3514e115012SGarrett Wollman *tailp = NULL; 3524e115012SGarrett Wollman if (tok.kind == TOK_DEFAULT) { 3534e115012SGarrett Wollman scan(TOK_COLON, &tok); 3544e115012SGarrett Wollman get_declaration(&dec, DEF_UNION); 35575863a6dSPhilippe Charnier defp->def.un.default_decl = XALLOC(declaration); 3564e115012SGarrett Wollman *defp->def.un.default_decl = dec; 3574e115012SGarrett Wollman scan(TOK_SEMICOLON, &tok); 3584e115012SGarrett Wollman scan(TOK_RBRACE, &tok); 3594e115012SGarrett Wollman } else { 3604e115012SGarrett Wollman defp->def.un.default_decl = NULL; 3614e115012SGarrett Wollman } 3624e115012SGarrett Wollman } 3634e115012SGarrett Wollman 364ff49530fSBill Paul static char* reserved_words[] = 365ff49530fSBill Paul { 366ff49530fSBill Paul "array", 367ff49530fSBill Paul "bytes", 368ff49530fSBill Paul "destroy", 369ff49530fSBill Paul "free", 370ff49530fSBill Paul "getpos", 371ff49530fSBill Paul "inline", 372ff49530fSBill Paul "pointer", 373ff49530fSBill Paul "reference", 374ff49530fSBill Paul "setpos", 375ff49530fSBill Paul "sizeof", 376ff49530fSBill Paul "union", 377ff49530fSBill Paul "vector", 378ff49530fSBill Paul NULL 379ff49530fSBill Paul }; 380ff49530fSBill Paul 381ff49530fSBill Paul static char* reserved_types[] = 382ff49530fSBill Paul { 383ff49530fSBill Paul "opaque", 384ff49530fSBill Paul "string", 385ff49530fSBill Paul NULL 386ff49530fSBill Paul }; 387ff49530fSBill Paul 388ff49530fSBill Paul /* 389ff49530fSBill Paul * check that the given name is not one that would eventually result in 390ff49530fSBill Paul * xdr routines that would conflict with internal XDR routines. 391ff49530fSBill Paul */ 392526195adSJordan K. Hubbard static void 393526195adSJordan K. Hubbard check_type_name(name, new_type) 394ff49530fSBill Paul int new_type; 395ff49530fSBill Paul char* name; 396ff49530fSBill Paul { 397ff49530fSBill Paul int i; 398ff49530fSBill Paul char tmp[100]; 399ff49530fSBill Paul 400ff49530fSBill Paul for (i = 0; reserved_words[i] != NULL; i++) { 401ff49530fSBill Paul if (strcmp(name, reserved_words[i]) == 0) { 402ff49530fSBill Paul sprintf(tmp, 403ff49530fSBill Paul "illegal (reserved) name :\'%s\' in type definition", 404ff49530fSBill Paul name); 405ff49530fSBill Paul error(tmp); 406ff49530fSBill Paul } 407ff49530fSBill Paul } 408ff49530fSBill Paul if (new_type) { 409ff49530fSBill Paul for (i = 0; reserved_types[i] != NULL; i++) { 410ff49530fSBill Paul if (strcmp(name, reserved_types[i]) == 0) { 411ff49530fSBill Paul sprintf(tmp, 412ff49530fSBill Paul "illegal (reserved) name :\'%s\' in type definition", 413ff49530fSBill Paul name); 414ff49530fSBill Paul error(tmp); 415ff49530fSBill Paul } 416ff49530fSBill Paul } 417ff49530fSBill Paul } 418ff49530fSBill Paul } 419ff49530fSBill Paul 420ff49530fSBill Paul 4214e115012SGarrett Wollman 422526195adSJordan K. Hubbard static void 4234e115012SGarrett Wollman def_typedef(defp) 4244e115012SGarrett Wollman definition *defp; 4254e115012SGarrett Wollman { 4264e115012SGarrett Wollman declaration dec; 4274e115012SGarrett Wollman 4284e115012SGarrett Wollman defp->def_kind = DEF_TYPEDEF; 4294e115012SGarrett Wollman get_declaration(&dec, DEF_TYPEDEF); 4304e115012SGarrett Wollman defp->def_name = dec.name; 431ff49530fSBill Paul check_type_name(dec.name, 1); 4324e115012SGarrett Wollman defp->def.ty.old_prefix = dec.prefix; 4334e115012SGarrett Wollman defp->def.ty.old_type = dec.type; 4344e115012SGarrett Wollman defp->def.ty.rel = dec.rel; 4354e115012SGarrett Wollman defp->def.ty.array_max = dec.array_max; 4364e115012SGarrett Wollman } 4374e115012SGarrett Wollman 438526195adSJordan K. Hubbard static void 4394e115012SGarrett Wollman get_declaration(dec, dkind) 4404e115012SGarrett Wollman declaration *dec; 4414e115012SGarrett Wollman defkind dkind; 4424e115012SGarrett Wollman { 4434e115012SGarrett Wollman token tok; 4444e115012SGarrett Wollman 4454e115012SGarrett Wollman get_type(&dec->prefix, &dec->type, dkind); 4464e115012SGarrett Wollman dec->rel = REL_ALIAS; 4474e115012SGarrett Wollman if (streq(dec->type, "void")) { 4484e115012SGarrett Wollman return; 4494e115012SGarrett Wollman } 450ff49530fSBill Paul 451ff49530fSBill Paul check_type_name(dec->type, 0); 4524e115012SGarrett Wollman scan2(TOK_STAR, TOK_IDENT, &tok); 4534e115012SGarrett Wollman if (tok.kind == TOK_STAR) { 4544e115012SGarrett Wollman dec->rel = REL_POINTER; 4554e115012SGarrett Wollman scan(TOK_IDENT, &tok); 4564e115012SGarrett Wollman } 4574e115012SGarrett Wollman dec->name = tok.str; 4584e115012SGarrett Wollman if (peekscan(TOK_LBRACKET, &tok)) { 4594e115012SGarrett Wollman if (dec->rel == REL_POINTER) { 4604e115012SGarrett Wollman error("no array-of-pointer declarations -- use typedef"); 4614e115012SGarrett Wollman } 4624e115012SGarrett Wollman dec->rel = REL_VECTOR; 4634e115012SGarrett Wollman scan_num(&tok); 4644e115012SGarrett Wollman dec->array_max = tok.str; 4654e115012SGarrett Wollman scan(TOK_RBRACKET, &tok); 4664e115012SGarrett Wollman } else if (peekscan(TOK_LANGLE, &tok)) { 4674e115012SGarrett Wollman if (dec->rel == REL_POINTER) { 4684e115012SGarrett Wollman error("no array-of-pointer declarations -- use typedef"); 4694e115012SGarrett Wollman } 4704e115012SGarrett Wollman dec->rel = REL_ARRAY; 4714e115012SGarrett Wollman if (peekscan(TOK_RANGLE, &tok)) { 4724e115012SGarrett Wollman dec->array_max = "~0"; /* unspecified size, use max */ 4734e115012SGarrett Wollman } else { 4744e115012SGarrett Wollman scan_num(&tok); 4754e115012SGarrett Wollman dec->array_max = tok.str; 4764e115012SGarrett Wollman scan(TOK_RANGLE, &tok); 4774e115012SGarrett Wollman } 4784e115012SGarrett Wollman } 4794e115012SGarrett Wollman if (streq(dec->type, "opaque")) { 4804e115012SGarrett Wollman if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR) { 4814e115012SGarrett Wollman error("array declaration expected"); 4824e115012SGarrett Wollman } 4834e115012SGarrett Wollman } else if (streq(dec->type, "string")) { 4844e115012SGarrett Wollman if (dec->rel != REL_ARRAY) { 4854e115012SGarrett Wollman error("variable-length array declaration expected"); 4864e115012SGarrett Wollman } 4874e115012SGarrett Wollman } 4884e115012SGarrett Wollman } 4894e115012SGarrett Wollman 4904e115012SGarrett Wollman 491526195adSJordan K. Hubbard static void 492ff49530fSBill Paul get_prog_declaration(dec, dkind, num) 493ff49530fSBill Paul declaration *dec; 494ff49530fSBill Paul defkind dkind; 495ff49530fSBill Paul int num; /* arg number */ 496ff49530fSBill Paul { 497ff49530fSBill Paul token tok; 498ff49530fSBill Paul char name[10]; /* argument name */ 499ff49530fSBill Paul 500ff49530fSBill Paul if (dkind == DEF_PROGRAM) { 501ff49530fSBill Paul peek(&tok); 502ff49530fSBill Paul if (tok.kind == TOK_RPAREN) { /* no arguments */ 503ff49530fSBill Paul dec->rel = REL_ALIAS; 504ff49530fSBill Paul dec->type = "void"; 505ff49530fSBill Paul dec->prefix = NULL; 506ff49530fSBill Paul dec->name = NULL; 507ff49530fSBill Paul return; 508ff49530fSBill Paul } 509ff49530fSBill Paul } 510ff49530fSBill Paul get_type(&dec->prefix, &dec->type, dkind); 511ff49530fSBill Paul dec->rel = REL_ALIAS; 512ff49530fSBill Paul if (peekscan(TOK_IDENT, &tok)) /* optional name of argument */ 513ff49530fSBill Paul strcpy(name, tok.str); 514ff49530fSBill Paul else 515ff49530fSBill Paul sprintf(name, "%s%d", ARGNAME, num); 516ff49530fSBill Paul /* default name of argument */ 517ff49530fSBill Paul 51875863a6dSPhilippe Charnier dec->name = (char *) xstrdup(name); 519ff49530fSBill Paul if (streq(dec->type, "void")) { 520ff49530fSBill Paul return; 521ff49530fSBill Paul } 522ff49530fSBill Paul 523ff49530fSBill Paul if (streq(dec->type, "opaque")) { 524ff49530fSBill Paul error("opaque -- illegal argument type"); 525ff49530fSBill Paul } 526ff49530fSBill Paul if (peekscan(TOK_STAR, &tok)) { 527ff49530fSBill Paul if (streq(dec->type, "string")) { 52875863a6dSPhilippe Charnier error("pointer to string not allowed in program arguments"); 529ff49530fSBill Paul } 530ff49530fSBill Paul dec->rel = REL_POINTER; 53175863a6dSPhilippe Charnier if (peekscan(TOK_IDENT, &tok)) { 532ff49530fSBill Paul /* optional name of argument */ 53375863a6dSPhilippe Charnier dec->name = xstrdup(tok.str); 53475863a6dSPhilippe Charnier } 535ff49530fSBill Paul } 536ff49530fSBill Paul if (peekscan(TOK_LANGLE, &tok)) { 537ff49530fSBill Paul if (!streq(dec->type, "string")) { 538ff49530fSBill Paul error("arrays cannot be declared as arguments to procedures -- use typedef"); 539ff49530fSBill Paul } 540ff49530fSBill Paul dec->rel = REL_ARRAY; 541ff49530fSBill Paul if (peekscan(TOK_RANGLE, &tok)) { 542ff49530fSBill Paul dec->array_max = "~0"; 543ff49530fSBill Paul /* unspecified size, use max */ 544ff49530fSBill Paul } else { 545ff49530fSBill Paul scan_num(&tok); 546ff49530fSBill Paul dec->array_max = tok.str; 547ff49530fSBill Paul scan(TOK_RANGLE, &tok); 548ff49530fSBill Paul } 549ff49530fSBill Paul } 550ff49530fSBill Paul if (streq(dec->type, "string")) { 551ff49530fSBill Paul if (dec->rel != REL_ARRAY) { 552ff49530fSBill Paul /* 553ff49530fSBill Paul * .x specifies just string as 554ff49530fSBill Paul * type of argument 555ff49530fSBill Paul * - make it string<> 556ff49530fSBill Paul */ 557ff49530fSBill Paul dec->rel = REL_ARRAY; 558ff49530fSBill Paul dec->array_max = "~0"; /* unspecified size, use max */ 559ff49530fSBill Paul } 560ff49530fSBill Paul } 561ff49530fSBill Paul } 562ff49530fSBill Paul 563ff49530fSBill Paul 564ff49530fSBill Paul 565526195adSJordan K. Hubbard static void 5664e115012SGarrett Wollman get_type(prefixp, typep, dkind) 5674e115012SGarrett Wollman char **prefixp; 5684e115012SGarrett Wollman char **typep; 5694e115012SGarrett Wollman defkind dkind; 5704e115012SGarrett Wollman { 5714e115012SGarrett Wollman token tok; 5724e115012SGarrett Wollman 5734e115012SGarrett Wollman *prefixp = NULL; 5744e115012SGarrett Wollman get_token(&tok); 5754e115012SGarrett Wollman switch (tok.kind) { 5764e115012SGarrett Wollman case TOK_IDENT: 5774e115012SGarrett Wollman *typep = tok.str; 5784e115012SGarrett Wollman break; 5794e115012SGarrett Wollman case TOK_STRUCT: 5804e115012SGarrett Wollman case TOK_ENUM: 5814e115012SGarrett Wollman case TOK_UNION: 5824e115012SGarrett Wollman *prefixp = tok.str; 5834e115012SGarrett Wollman scan(TOK_IDENT, &tok); 5844e115012SGarrett Wollman *typep = tok.str; 5854e115012SGarrett Wollman break; 5864e115012SGarrett Wollman case TOK_UNSIGNED: 5874e115012SGarrett Wollman unsigned_dec(typep); 5884e115012SGarrett Wollman break; 5894e115012SGarrett Wollman case TOK_SHORT: 5904e115012SGarrett Wollman *typep = "short"; 5914e115012SGarrett Wollman (void) peekscan(TOK_INT, &tok); 5924e115012SGarrett Wollman break; 5934e115012SGarrett Wollman case TOK_LONG: 5944e115012SGarrett Wollman *typep = "long"; 5954e115012SGarrett Wollman (void) peekscan(TOK_INT, &tok); 5964e115012SGarrett Wollman break; 597ff49530fSBill Paul case TOK_HYPER: 598c304ad8aSDavid E. O'Brien *typep = "int64_t"; 599ff49530fSBill Paul (void) peekscan(TOK_INT, &tok); 600ff49530fSBill Paul break; 601ff49530fSBill Paul 6024e115012SGarrett Wollman case TOK_VOID: 6034e115012SGarrett Wollman if (dkind != DEF_UNION && dkind != DEF_PROGRAM) { 604ff49530fSBill Paul error("voids allowed only inside union and program definitions with one argument"); 6054e115012SGarrett Wollman } 6064e115012SGarrett Wollman *typep = tok.str; 6074e115012SGarrett Wollman break; 6084e115012SGarrett Wollman case TOK_STRING: 6094e115012SGarrett Wollman case TOK_OPAQUE: 6104e115012SGarrett Wollman case TOK_CHAR: 6114e115012SGarrett Wollman case TOK_INT: 6124e115012SGarrett Wollman case TOK_FLOAT: 6134e115012SGarrett Wollman case TOK_DOUBLE: 6144e115012SGarrett Wollman case TOK_BOOL: 615ff49530fSBill Paul case TOK_QUAD: 6164e115012SGarrett Wollman *typep = tok.str; 6174e115012SGarrett Wollman break; 6184e115012SGarrett Wollman default: 6194e115012SGarrett Wollman error("expected type specifier"); 6204e115012SGarrett Wollman } 6214e115012SGarrett Wollman } 6224e115012SGarrett Wollman 623526195adSJordan K. Hubbard static void 6244e115012SGarrett Wollman unsigned_dec(typep) 6254e115012SGarrett Wollman char **typep; 6264e115012SGarrett Wollman { 6274e115012SGarrett Wollman token tok; 6284e115012SGarrett Wollman 6294e115012SGarrett Wollman peek(&tok); 6304e115012SGarrett Wollman switch (tok.kind) { 6314e115012SGarrett Wollman case TOK_CHAR: 6324e115012SGarrett Wollman get_token(&tok); 6334e115012SGarrett Wollman *typep = "u_char"; 6344e115012SGarrett Wollman break; 6354e115012SGarrett Wollman case TOK_SHORT: 6364e115012SGarrett Wollman get_token(&tok); 6374e115012SGarrett Wollman *typep = "u_short"; 6384e115012SGarrett Wollman (void) peekscan(TOK_INT, &tok); 6394e115012SGarrett Wollman break; 6404e115012SGarrett Wollman case TOK_LONG: 6414e115012SGarrett Wollman get_token(&tok); 6424e115012SGarrett Wollman *typep = "u_long"; 6434e115012SGarrett Wollman (void) peekscan(TOK_INT, &tok); 6444e115012SGarrett Wollman break; 645ff49530fSBill Paul case TOK_HYPER: 646ff49530fSBill Paul get_token(&tok); 647c304ad8aSDavid E. O'Brien *typep = "u_int64_t"; 6488360efbdSAlfred Perlstein 649ff49530fSBill Paul (void) peekscan(TOK_INT, &tok); 650ff49530fSBill Paul break; 6514e115012SGarrett Wollman case TOK_INT: 6524e115012SGarrett Wollman get_token(&tok); 6534e115012SGarrett Wollman *typep = "u_int"; 6544e115012SGarrett Wollman break; 6554e115012SGarrett Wollman default: 6564e115012SGarrett Wollman *typep = "u_int"; 6574e115012SGarrett Wollman break; 6584e115012SGarrett Wollman } 6594e115012SGarrett Wollman } 660