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 5a2f144d1SJordan Brown * Common Development and Distribution License (the "License"). 6a2f144d1SJordan Brown * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 2161961e0fSrobinson 227c478bd9Sstevel@tonic-gate /* 23a2f144d1SJordan Brown * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 277c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 287c478bd9Sstevel@tonic-gate /* 297c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 307c478bd9Sstevel@tonic-gate * The Regents of the University of California 317c478bd9Sstevel@tonic-gate * All Rights Reserved 327c478bd9Sstevel@tonic-gate * 337c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 347c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 357c478bd9Sstevel@tonic-gate * contributors. 367c478bd9Sstevel@tonic-gate */ 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate /* 397c478bd9Sstevel@tonic-gate * rpc_main.c, Top level of the RPC protocol compiler. 407c478bd9Sstevel@tonic-gate */ 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate #include <stdio.h> 4361961e0fSrobinson #include <stdlib.h> 447c478bd9Sstevel@tonic-gate #include <string.h> 4561961e0fSrobinson #include <strings.h> 467c478bd9Sstevel@tonic-gate #include <unistd.h> 4761961e0fSrobinson #include <ctype.h> 487c478bd9Sstevel@tonic-gate #include <sys/types.h> 497c478bd9Sstevel@tonic-gate #include <sys/param.h> 507c478bd9Sstevel@tonic-gate #include <sys/file.h> 517c478bd9Sstevel@tonic-gate #include <sys/stat.h> 527c478bd9Sstevel@tonic-gate #include "rpc_parse.h" 537c478bd9Sstevel@tonic-gate #include "rpc_util.h" 547c478bd9Sstevel@tonic-gate #include "rpc_scan.h" 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate 5761961e0fSrobinson extern void write_sample_svc(definition *); 5861961e0fSrobinson extern int write_sample_clnt(definition *); 5961961e0fSrobinson extern void write_sample_clnt_main(void); 6061961e0fSrobinson extern void reinitialize(void); 6161961e0fSrobinson extern void crash(void); 6261961e0fSrobinson extern void add_type(int, char *); 6361961e0fSrobinson extern void add_sample_msg(void); 647c478bd9Sstevel@tonic-gate 6561961e0fSrobinson static void svc_output(char *, char *, int, char *); 6661961e0fSrobinson static void clnt_output(char *, char *, int, char *); 6761961e0fSrobinson static void c_output(char *, char *, int, char *); 6861961e0fSrobinson static void mkfile_output(struct commandline *); 6961961e0fSrobinson static void c_initialize(void); 7061961e0fSrobinson static void h_output(char *, char *, int, char *); 7161961e0fSrobinson static void s_output(int, char *[], char *, char *, int, char *, int, int); 7261961e0fSrobinson static void l_output(char *, char *, int, char *); 7361961e0fSrobinson static void t_output(char *, char *, int, char *); 7461961e0fSrobinson static int do_registers(int, char *[]); 7561961e0fSrobinson static uint_t parseargs(int, char *[], struct commandline *); 7661961e0fSrobinson static void usage(void); 7761961e0fSrobinson static void version_info(void); 7861961e0fSrobinson static void options_usage(void); 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate #define EXTEND 1 /* alias for TRUE */ 817c478bd9Sstevel@tonic-gate #define DONT_EXTEND 0 /* alias for FALSE */ 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate #define SUNOS_CPP "/usr/lib/cpp" 847c478bd9Sstevel@tonic-gate static int cppDefined = 0; /* explicit path for C preprocessor */ 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate static char *cmdname; 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate static char *svcclosetime = "120"; 907c478bd9Sstevel@tonic-gate static char *CPP = SUNOS_CPP; 917c478bd9Sstevel@tonic-gate static char CPPFLAGS[] = "-C"; 927c478bd9Sstevel@tonic-gate static char pathbuf[MAXPATHLEN + 1]; 937c478bd9Sstevel@tonic-gate static char *allv[] = { 947c478bd9Sstevel@tonic-gate "rpcgen", "-s", "udp", "-s", "tcp", 957c478bd9Sstevel@tonic-gate }; 967c478bd9Sstevel@tonic-gate static int allc = sizeof (allv)/sizeof (allv[0]); 977c478bd9Sstevel@tonic-gate static char *allnv[] = { 987c478bd9Sstevel@tonic-gate "rpcgen", "-s", "netpath", 997c478bd9Sstevel@tonic-gate }; 1007c478bd9Sstevel@tonic-gate static int allnc = sizeof (allnv)/sizeof (allnv[0]); 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gate /* 1037c478bd9Sstevel@tonic-gate * machinations for handling expanding argument list 1047c478bd9Sstevel@tonic-gate */ 10561961e0fSrobinson static void addarg(char *); /* add another argument to the list */ 10661961e0fSrobinson static void putarg(int, char *); /* put argument at specified location */ 10761961e0fSrobinson static void clear_args(void); /* clear argument list */ 10861961e0fSrobinson static void checkfiles(char *, char *); /* check if out file already exists */ 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate #define ARGLISTLEN 20 1127c478bd9Sstevel@tonic-gate #define FIXEDARGS 2 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate static char *arglist[ARGLISTLEN]; 1157c478bd9Sstevel@tonic-gate static int argcount = FIXEDARGS; 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate int nonfatalerrors; /* errors */ 1197c478bd9Sstevel@tonic-gate int inetdflag; /* Support for inetd is now the default */ 1207c478bd9Sstevel@tonic-gate int pmflag; /* Support for port monitors */ 1217c478bd9Sstevel@tonic-gate int logflag; /* Use syslog instead of fprintf for errors */ 1227c478bd9Sstevel@tonic-gate int tblflag; /* Support for dispatch table file */ 1237c478bd9Sstevel@tonic-gate int mtflag = 0; /* Support for MT */ 1247c478bd9Sstevel@tonic-gate int mtauto = 0; /* Enable automatic mode */ 1257c478bd9Sstevel@tonic-gate int rflag = 1; /* Eliminate tail recursion from structures */ 1267c478bd9Sstevel@tonic-gate #define INLINE 5 1277c478bd9Sstevel@tonic-gate /* length at which to start doing an inline */ 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate int inlinelen = INLINE; 1307c478bd9Sstevel@tonic-gate /* 1317c478bd9Sstevel@tonic-gate * Length at which to start doing an inline. INLINE = default 1327c478bd9Sstevel@tonic-gate * if 0, no xdr_inline code 1337c478bd9Sstevel@tonic-gate */ 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate int indefinitewait; /* If started by port monitors, hang till it wants */ 1367c478bd9Sstevel@tonic-gate int exitnow; /* If started by port monitors, exit after the call */ 1377c478bd9Sstevel@tonic-gate int timerflag; /* TRUE if !indefinite && !exitnow */ 1387c478bd9Sstevel@tonic-gate int newstyle; /* newstyle of passing arguments (by value) */ 1397c478bd9Sstevel@tonic-gate int Cflag = 0; /* ANSI C syntax */ 1407c478bd9Sstevel@tonic-gate int CCflag = 0; /* C++ files */ 1417c478bd9Sstevel@tonic-gate static int allfiles; /* generate all files */ 1427c478bd9Sstevel@tonic-gate int tirpcflag = 1; /* generating code for tirpc, by default */ 1437c478bd9Sstevel@tonic-gate xdrfunc *xdrfunc_head = NULL; /* xdr function list */ 1447c478bd9Sstevel@tonic-gate xdrfunc *xdrfunc_tail = NULL; /* xdr function list */ 1457c478bd9Sstevel@tonic-gate pid_t childpid; 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate 14861961e0fSrobinson int 14961961e0fSrobinson main(int argc, char *argv[]) 1507c478bd9Sstevel@tonic-gate { 1517c478bd9Sstevel@tonic-gate struct commandline cmd; 1527c478bd9Sstevel@tonic-gate 15361961e0fSrobinson (void) memset(&cmd, 0, sizeof (struct commandline)); 1547c478bd9Sstevel@tonic-gate clear_args(); 1557c478bd9Sstevel@tonic-gate if (!parseargs(argc, argv, &cmd)) 1567c478bd9Sstevel@tonic-gate usage(); 1577c478bd9Sstevel@tonic-gate /* 1587c478bd9Sstevel@tonic-gate * Only the client and server side stubs are likely to be customized, 1597c478bd9Sstevel@tonic-gate * so in that case only, check if the outfile exists, and if so, 1607c478bd9Sstevel@tonic-gate * print an error message and exit. 1617c478bd9Sstevel@tonic-gate */ 16261961e0fSrobinson if (cmd.Ssflag || cmd.Scflag || cmd.makefileflag) 1637c478bd9Sstevel@tonic-gate checkfiles(cmd.infile, cmd.outfile); 1647c478bd9Sstevel@tonic-gate else 1657c478bd9Sstevel@tonic-gate checkfiles(cmd.infile, NULL); 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate if (cmd.cflag) { 1687c478bd9Sstevel@tonic-gate c_output(cmd.infile, "-DRPC_XDR", DONT_EXTEND, cmd.outfile); 1697c478bd9Sstevel@tonic-gate } else if (cmd.hflag) { 1707c478bd9Sstevel@tonic-gate h_output(cmd.infile, "-DRPC_HDR", DONT_EXTEND, cmd.outfile); 1717c478bd9Sstevel@tonic-gate } else if (cmd.lflag) { 1727c478bd9Sstevel@tonic-gate l_output(cmd.infile, "-DRPC_CLNT", DONT_EXTEND, cmd.outfile); 1737c478bd9Sstevel@tonic-gate } else if (cmd.sflag || cmd.mflag || (cmd.nflag)) { 1747c478bd9Sstevel@tonic-gate s_output(argc, argv, cmd.infile, "-DRPC_SVC", DONT_EXTEND, 1757c478bd9Sstevel@tonic-gate cmd.outfile, cmd.mflag, cmd.nflag); 1767c478bd9Sstevel@tonic-gate } else if (cmd.tflag) { 1777c478bd9Sstevel@tonic-gate t_output(cmd.infile, "-DRPC_TBL", DONT_EXTEND, cmd.outfile); 1787c478bd9Sstevel@tonic-gate } else if (cmd.Ssflag) { 1797c478bd9Sstevel@tonic-gate svc_output(cmd.infile, "-DRPC_SERVER", DONT_EXTEND, 1807c478bd9Sstevel@tonic-gate cmd.outfile); 1817c478bd9Sstevel@tonic-gate } else if (cmd.Scflag) { 1827c478bd9Sstevel@tonic-gate clnt_output(cmd.infile, "-DRPC_CLIENT", DONT_EXTEND, 1837c478bd9Sstevel@tonic-gate cmd.outfile); 1847c478bd9Sstevel@tonic-gate } else if (cmd.makefileflag) { 1857c478bd9Sstevel@tonic-gate mkfile_output(&cmd); 1867c478bd9Sstevel@tonic-gate } else { 1877c478bd9Sstevel@tonic-gate /* the rescans are required, since cpp may effect input */ 1887c478bd9Sstevel@tonic-gate c_output(cmd.infile, "-DRPC_XDR", EXTEND, "_xdr.c"); 1897c478bd9Sstevel@tonic-gate reinitialize(); 1907c478bd9Sstevel@tonic-gate h_output(cmd.infile, "-DRPC_HDR", EXTEND, ".h"); 1917c478bd9Sstevel@tonic-gate reinitialize(); 1927c478bd9Sstevel@tonic-gate l_output(cmd.infile, "-DRPC_CLNT", EXTEND, "_clnt.c"); 1937c478bd9Sstevel@tonic-gate reinitialize(); 1947c478bd9Sstevel@tonic-gate if (inetdflag || !tirpcflag) 1957c478bd9Sstevel@tonic-gate s_output(allc, allv, cmd.infile, "-DRPC_SVC", EXTEND, 1967c478bd9Sstevel@tonic-gate "_svc.c", cmd.mflag, cmd.nflag); 1977c478bd9Sstevel@tonic-gate else 1987c478bd9Sstevel@tonic-gate s_output(allnc, allnv, cmd.infile, "-DRPC_SVC", 1997c478bd9Sstevel@tonic-gate EXTEND, "_svc.c", cmd.mflag, cmd.nflag); 2007c478bd9Sstevel@tonic-gate if (tblflag) { 2017c478bd9Sstevel@tonic-gate reinitialize(); 2027c478bd9Sstevel@tonic-gate t_output(cmd.infile, "-DRPC_TBL", EXTEND, "_tbl.i"); 2037c478bd9Sstevel@tonic-gate } 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate if (allfiles) { 2067c478bd9Sstevel@tonic-gate reinitialize(); 2077c478bd9Sstevel@tonic-gate svc_output(cmd.infile, "-DRPC_SERVER", EXTEND, 2087c478bd9Sstevel@tonic-gate "_server.c"); 2097c478bd9Sstevel@tonic-gate reinitialize(); 2107c478bd9Sstevel@tonic-gate clnt_output(cmd.infile, "-DRPC_CLIENT", EXTEND, 2117c478bd9Sstevel@tonic-gate "_client.c"); 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate } 2147c478bd9Sstevel@tonic-gate if (allfiles || (cmd.makefileflag == 1)) { 2157c478bd9Sstevel@tonic-gate reinitialize(); 2167c478bd9Sstevel@tonic-gate mkfile_output(&cmd); 2177c478bd9Sstevel@tonic-gate } 2187c478bd9Sstevel@tonic-gate 2197c478bd9Sstevel@tonic-gate } 22061961e0fSrobinson return (nonfatalerrors); 2217c478bd9Sstevel@tonic-gate } 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate 2247c478bd9Sstevel@tonic-gate /* 2257c478bd9Sstevel@tonic-gate * add extension to filename 2267c478bd9Sstevel@tonic-gate */ 2277c478bd9Sstevel@tonic-gate static char * 22861961e0fSrobinson extendfile(char *file, char *ext) 2297c478bd9Sstevel@tonic-gate { 2307c478bd9Sstevel@tonic-gate char *res; 2317c478bd9Sstevel@tonic-gate char *p; 2327c478bd9Sstevel@tonic-gate 23361961e0fSrobinson res = malloc(strlen(file) + strlen(ext) + 1); 23461961e0fSrobinson if (res == NULL) 2357c478bd9Sstevel@tonic-gate abort(); 2367c478bd9Sstevel@tonic-gate p = strrchr(file, '.'); 23761961e0fSrobinson if (p == NULL) 2387c478bd9Sstevel@tonic-gate p = file + strlen(file); 2397c478bd9Sstevel@tonic-gate (void) strcpy(res, file); 2407c478bd9Sstevel@tonic-gate (void) strcpy(res + (p - file), ext); 2417c478bd9Sstevel@tonic-gate return (res); 2427c478bd9Sstevel@tonic-gate } 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate /* 2457c478bd9Sstevel@tonic-gate * Open output file with given extension 2467c478bd9Sstevel@tonic-gate */ 24761961e0fSrobinson static void 24861961e0fSrobinson open_output(char *infile, char *outfile) 2497c478bd9Sstevel@tonic-gate { 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate if (outfile == NULL) { 2527c478bd9Sstevel@tonic-gate fout = stdout; 2537c478bd9Sstevel@tonic-gate return; 2547c478bd9Sstevel@tonic-gate } 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate if (infile != NULL && streq(outfile, infile)) { 257a2f144d1SJordan Brown f_print(stderr, 258a2f144d1SJordan Brown "%s: %s already exists. No output generated.\n", 2597c478bd9Sstevel@tonic-gate cmdname, infile); 2607c478bd9Sstevel@tonic-gate crash(); 2617c478bd9Sstevel@tonic-gate } 2627c478bd9Sstevel@tonic-gate fout = fopen(outfile, "w"); 2637c478bd9Sstevel@tonic-gate if (fout == NULL) { 2647c478bd9Sstevel@tonic-gate f_print(stderr, "%s: unable to open ", cmdname); 2657c478bd9Sstevel@tonic-gate perror(outfile); 2667c478bd9Sstevel@tonic-gate crash(); 2677c478bd9Sstevel@tonic-gate } 2687c478bd9Sstevel@tonic-gate record_open(outfile); 2697c478bd9Sstevel@tonic-gate 2707c478bd9Sstevel@tonic-gate } 2717c478bd9Sstevel@tonic-gate 27261961e0fSrobinson static void 27361961e0fSrobinson add_warning(void) 2747c478bd9Sstevel@tonic-gate { 2757c478bd9Sstevel@tonic-gate f_print(fout, "/*\n"); 2767c478bd9Sstevel@tonic-gate f_print(fout, " * Please do not edit this file.\n"); 2777c478bd9Sstevel@tonic-gate f_print(fout, " * It was generated using rpcgen.\n"); 2787c478bd9Sstevel@tonic-gate f_print(fout, " */\n\n"); 2797c478bd9Sstevel@tonic-gate } 2807c478bd9Sstevel@tonic-gate 2817c478bd9Sstevel@tonic-gate /* clear list of arguments */ 28261961e0fSrobinson static void 28361961e0fSrobinson clear_args(void) 2847c478bd9Sstevel@tonic-gate { 2857c478bd9Sstevel@tonic-gate int i; 28661961e0fSrobinson 2877c478bd9Sstevel@tonic-gate for (i = FIXEDARGS; i < ARGLISTLEN; i++) 2887c478bd9Sstevel@tonic-gate arglist[i] = NULL; 2897c478bd9Sstevel@tonic-gate argcount = FIXEDARGS; 2907c478bd9Sstevel@tonic-gate } 2917c478bd9Sstevel@tonic-gate 2927c478bd9Sstevel@tonic-gate /* make sure that a CPP exists */ 29361961e0fSrobinson static void 29461961e0fSrobinson find_cpp(void) 2957c478bd9Sstevel@tonic-gate { 2967c478bd9Sstevel@tonic-gate struct stat buf; 2977c478bd9Sstevel@tonic-gate 2987c478bd9Sstevel@tonic-gate if (stat(CPP, &buf) < 0) { /* SVR4 or explicit cpp does not exist */ 2997c478bd9Sstevel@tonic-gate if (cppDefined) { 30061961e0fSrobinson (void) fprintf(stderr, 3017c478bd9Sstevel@tonic-gate "cannot find C preprocessor: %s \n", CPP); 3027c478bd9Sstevel@tonic-gate crash(); 3037c478bd9Sstevel@tonic-gate } else { /* try the other one */ 3047c478bd9Sstevel@tonic-gate CPP = SUNOS_CPP; 3057c478bd9Sstevel@tonic-gate if (stat(CPP, &buf) < 0) { /* can't find any cpp */ 30661961e0fSrobinson (void) fprintf(stderr, 3077c478bd9Sstevel@tonic-gate "cannot find any C preprocessor (cpp)\n"); 3087c478bd9Sstevel@tonic-gate crash(); 3097c478bd9Sstevel@tonic-gate } 3107c478bd9Sstevel@tonic-gate } 3117c478bd9Sstevel@tonic-gate } 3127c478bd9Sstevel@tonic-gate } 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate /* 3157c478bd9Sstevel@tonic-gate * Open input file with given define for C-preprocessor 3167c478bd9Sstevel@tonic-gate */ 31761961e0fSrobinson static void 31861961e0fSrobinson open_input(char *infile, char *define) 3197c478bd9Sstevel@tonic-gate { 3207c478bd9Sstevel@tonic-gate int pd[2]; 3217c478bd9Sstevel@tonic-gate 3227c478bd9Sstevel@tonic-gate infilename = (infile == NULL) ? "<stdin>" : infile; 3237c478bd9Sstevel@tonic-gate (void) pipe(pd); 3247c478bd9Sstevel@tonic-gate switch (childpid = fork()) { 3257c478bd9Sstevel@tonic-gate case 0: 3267c478bd9Sstevel@tonic-gate find_cpp(); 3277c478bd9Sstevel@tonic-gate putarg(0, CPP); 3287c478bd9Sstevel@tonic-gate putarg(1, CPPFLAGS); 3297c478bd9Sstevel@tonic-gate addarg(define); 3307c478bd9Sstevel@tonic-gate if (infile) 3317c478bd9Sstevel@tonic-gate addarg(infile); 3327c478bd9Sstevel@tonic-gate addarg((char *)NULL); 3337c478bd9Sstevel@tonic-gate (void) close(1); 3347c478bd9Sstevel@tonic-gate (void) dup2(pd[1], 1); 3357c478bd9Sstevel@tonic-gate (void) close(pd[0]); 33661961e0fSrobinson (void) execv(arglist[0], arglist); 3377c478bd9Sstevel@tonic-gate perror("execv"); 3387c478bd9Sstevel@tonic-gate exit(1); 33961961e0fSrobinson /* NOTREACHED */ 3407c478bd9Sstevel@tonic-gate case -1: 3417c478bd9Sstevel@tonic-gate perror("fork"); 3427c478bd9Sstevel@tonic-gate exit(1); 3437c478bd9Sstevel@tonic-gate } 3447c478bd9Sstevel@tonic-gate (void) close(pd[1]); 3457c478bd9Sstevel@tonic-gate fin = fdopen(pd[0], "r"); 3467c478bd9Sstevel@tonic-gate if (fin == NULL) { 3477c478bd9Sstevel@tonic-gate f_print(stderr, "%s: ", cmdname); 3487c478bd9Sstevel@tonic-gate perror(infilename); 3497c478bd9Sstevel@tonic-gate crash(); 3507c478bd9Sstevel@tonic-gate } 3517c478bd9Sstevel@tonic-gate } 3527c478bd9Sstevel@tonic-gate 3537c478bd9Sstevel@tonic-gate /* valid tirpc nettypes */ 35461961e0fSrobinson static char *valid_ti_nettypes[] = { 3557c478bd9Sstevel@tonic-gate "netpath", 3567c478bd9Sstevel@tonic-gate "visible", 3577c478bd9Sstevel@tonic-gate "circuit_v", 3587c478bd9Sstevel@tonic-gate "datagram_v", 3597c478bd9Sstevel@tonic-gate "circuit_n", 3607c478bd9Sstevel@tonic-gate "datagram_n", 3617c478bd9Sstevel@tonic-gate "udp", 3627c478bd9Sstevel@tonic-gate "tcp", 3637c478bd9Sstevel@tonic-gate "raw", 3647c478bd9Sstevel@tonic-gate NULL 3657c478bd9Sstevel@tonic-gate }; 3667c478bd9Sstevel@tonic-gate 3677c478bd9Sstevel@tonic-gate /* valid inetd nettypes */ 36861961e0fSrobinson static char *valid_i_nettypes[] = { 3697c478bd9Sstevel@tonic-gate "udp", 3707c478bd9Sstevel@tonic-gate "tcp", 3717c478bd9Sstevel@tonic-gate NULL 3727c478bd9Sstevel@tonic-gate }; 3737c478bd9Sstevel@tonic-gate 37461961e0fSrobinson static int 37561961e0fSrobinson check_nettype(char *name, char *list_to_check[]) 3767c478bd9Sstevel@tonic-gate { 3777c478bd9Sstevel@tonic-gate int i; 3787c478bd9Sstevel@tonic-gate for (i = 0; list_to_check[i] != NULL; i++) { 3797c478bd9Sstevel@tonic-gate if (strcmp(name, list_to_check[i]) == 0) { 3807c478bd9Sstevel@tonic-gate return (1); 3817c478bd9Sstevel@tonic-gate } 3827c478bd9Sstevel@tonic-gate } 3837c478bd9Sstevel@tonic-gate f_print(stderr, "illegal nettype :\'%s\'\n", name); 3847c478bd9Sstevel@tonic-gate return (0); 3857c478bd9Sstevel@tonic-gate } 3867c478bd9Sstevel@tonic-gate 3877c478bd9Sstevel@tonic-gate static char * 38861961e0fSrobinson file_name(char *file, char *ext) 3897c478bd9Sstevel@tonic-gate { 3907c478bd9Sstevel@tonic-gate char *temp; 3917c478bd9Sstevel@tonic-gate temp = extendfile(file, ext); 3927c478bd9Sstevel@tonic-gate 3937c478bd9Sstevel@tonic-gate if (access(temp, F_OK) != -1) 3947c478bd9Sstevel@tonic-gate return (temp); 3957c478bd9Sstevel@tonic-gate else 3967c478bd9Sstevel@tonic-gate return ((char *)" "); 3977c478bd9Sstevel@tonic-gate } 3987c478bd9Sstevel@tonic-gate 3997c478bd9Sstevel@tonic-gate 40061961e0fSrobinson static void 40161961e0fSrobinson c_output(char *infile, char *define, int extend, char *outfile) 4027c478bd9Sstevel@tonic-gate { 4037c478bd9Sstevel@tonic-gate definition *def; 4047c478bd9Sstevel@tonic-gate char *include; 4057c478bd9Sstevel@tonic-gate char *outfilename; 4067c478bd9Sstevel@tonic-gate long tell; 4077c478bd9Sstevel@tonic-gate 4087c478bd9Sstevel@tonic-gate c_initialize(); 4097c478bd9Sstevel@tonic-gate open_input(infile, define); 4107c478bd9Sstevel@tonic-gate outfilename = extend ? extendfile(infile, outfile) : outfile; 4117c478bd9Sstevel@tonic-gate open_output(infile, outfilename); 4127c478bd9Sstevel@tonic-gate add_warning(); 4137c478bd9Sstevel@tonic-gate if (infile && (include = extendfile(infile, ".h"))) { 4147c478bd9Sstevel@tonic-gate f_print(fout, "#include \"%s\"\n", include); 4157c478bd9Sstevel@tonic-gate free(include); 4167c478bd9Sstevel@tonic-gate /* .h file already contains rpc/rpc.h */ 4177c478bd9Sstevel@tonic-gate } else 4187c478bd9Sstevel@tonic-gate f_print(fout, "#include <rpc/rpc.h>\n"); 4197c478bd9Sstevel@tonic-gate /* 42061961e0fSrobinson * Include stdlib.h to support mem_alloc calls. 4217c478bd9Sstevel@tonic-gate */ 4227c478bd9Sstevel@tonic-gate f_print(fout, "\n#ifndef _KERNEL\n"); 42361961e0fSrobinson f_print(fout, "#include <stdlib.h>\n"); 4247c478bd9Sstevel@tonic-gate f_print(fout, "#endif /* !_KERNEL */\n\n"); 4257c478bd9Sstevel@tonic-gate tell = ftell(fout); 4267c478bd9Sstevel@tonic-gate while (def = get_definition()) { 4277c478bd9Sstevel@tonic-gate emit(def); 4287c478bd9Sstevel@tonic-gate } 4297c478bd9Sstevel@tonic-gate if (extend && tell == ftell(fout)) { 4307c478bd9Sstevel@tonic-gate (void) unlink(outfilename); 4317c478bd9Sstevel@tonic-gate } 4327c478bd9Sstevel@tonic-gate } 4337c478bd9Sstevel@tonic-gate 4347c478bd9Sstevel@tonic-gate 43561961e0fSrobinson static void 43661961e0fSrobinson c_initialize(void) 4377c478bd9Sstevel@tonic-gate { 4387c478bd9Sstevel@tonic-gate /* 4397c478bd9Sstevel@tonic-gate * add all the starting basic types. 4407c478bd9Sstevel@tonic-gate * We may need to add some derived types 4417c478bd9Sstevel@tonic-gate * if we need to generate INLINE macros. 4427c478bd9Sstevel@tonic-gate * These types are defined in rpc/types.h 4437c478bd9Sstevel@tonic-gate */ 4447c478bd9Sstevel@tonic-gate add_type(1, "int"); 4457c478bd9Sstevel@tonic-gate add_type(1, "long"); 4467c478bd9Sstevel@tonic-gate add_type(1, "short"); 4477c478bd9Sstevel@tonic-gate add_type(1, "bool"); 4487c478bd9Sstevel@tonic-gate add_type(1, "u_int"); 4497c478bd9Sstevel@tonic-gate add_type(1, "u_long"); 4507c478bd9Sstevel@tonic-gate add_type(1, "u_short"); 4517c478bd9Sstevel@tonic-gate add_type(1, "rpcprog_t"); 4527c478bd9Sstevel@tonic-gate add_type(1, "rpcvers_t"); 4537c478bd9Sstevel@tonic-gate add_type(1, "rpcproc_t"); 4547c478bd9Sstevel@tonic-gate add_type(1, "rpcprot_t"); 4557c478bd9Sstevel@tonic-gate add_type(1, "rpcport_t"); 4567c478bd9Sstevel@tonic-gate } 4577c478bd9Sstevel@tonic-gate 4587c478bd9Sstevel@tonic-gate char rpcgen_table_dcl1[] = "struct rpcgen_table {\n"; 4597c478bd9Sstevel@tonic-gate 4607c478bd9Sstevel@tonic-gate char rpcgen_table_dcl2[] = "\txdrproc_t\txdr_arg;\n" 4617c478bd9Sstevel@tonic-gate "\tunsigned\tlen_arg;\n" 4627c478bd9Sstevel@tonic-gate "\txdrproc_t\txdr_res;\n" 4637c478bd9Sstevel@tonic-gate "\tunsigned\tlen_res;\n" 4647c478bd9Sstevel@tonic-gate "};\n"; 4657c478bd9Sstevel@tonic-gate 4667c478bd9Sstevel@tonic-gate char rpcgen_table_proc[] = "\tvoid\t*(*proc)();\n"; 4677c478bd9Sstevel@tonic-gate 4687c478bd9Sstevel@tonic-gate char rpcgen_table_proc_b[] = "\tchar\t*(*proc)();\n"; 4697c478bd9Sstevel@tonic-gate 4707c478bd9Sstevel@tonic-gate 4717c478bd9Sstevel@tonic-gate char * 47261961e0fSrobinson generate_guard(char *pathname) 4737c478bd9Sstevel@tonic-gate { 4747c478bd9Sstevel@tonic-gate char *filename, *guard, *tmp; 4757c478bd9Sstevel@tonic-gate 4767c478bd9Sstevel@tonic-gate filename = strrchr(pathname, '/'); /* find last component */ 4777c478bd9Sstevel@tonic-gate filename = ((filename == 0) ? pathname : filename+1); 4787c478bd9Sstevel@tonic-gate guard = extendfile(filename, "_H_RPCGEN"); 4797c478bd9Sstevel@tonic-gate 4807c478bd9Sstevel@tonic-gate /* 4817c478bd9Sstevel@tonic-gate * Guard must be an ANSI C identifier composed of 4827c478bd9Sstevel@tonic-gate * upper case letters, digits, or '_'. 4837c478bd9Sstevel@tonic-gate * Convert invalid characters to '_'. 4847c478bd9Sstevel@tonic-gate */ 4857c478bd9Sstevel@tonic-gate for (tmp = guard; *tmp; tmp++) { 4867c478bd9Sstevel@tonic-gate if (!isalpha(*tmp) && !isdigit(*tmp)) { 4877c478bd9Sstevel@tonic-gate *tmp = '_'; 4887c478bd9Sstevel@tonic-gate continue; 4897c478bd9Sstevel@tonic-gate } 4907c478bd9Sstevel@tonic-gate if (islower(*tmp)) 4917c478bd9Sstevel@tonic-gate *tmp = toupper(*tmp); 4927c478bd9Sstevel@tonic-gate } 4937c478bd9Sstevel@tonic-gate 4947c478bd9Sstevel@tonic-gate /* 4957c478bd9Sstevel@tonic-gate * The first character must be a letter; the underscore '_' 4967c478bd9Sstevel@tonic-gate * counts as a letter. 4977c478bd9Sstevel@tonic-gate */ 4987c478bd9Sstevel@tonic-gate if (!isalpha(guard[0])) 4997c478bd9Sstevel@tonic-gate guard[0] = '_'; 5007c478bd9Sstevel@tonic-gate 5017c478bd9Sstevel@tonic-gate return (guard); 5027c478bd9Sstevel@tonic-gate } 5037c478bd9Sstevel@tonic-gate 5047c478bd9Sstevel@tonic-gate /* 5057c478bd9Sstevel@tonic-gate * Compile into an XDR header file 5067c478bd9Sstevel@tonic-gate */ 5077c478bd9Sstevel@tonic-gate 5087c478bd9Sstevel@tonic-gate 50961961e0fSrobinson static void 51061961e0fSrobinson h_output(char *infile, char *define, int extend, char *outfile) 5117c478bd9Sstevel@tonic-gate { 5127c478bd9Sstevel@tonic-gate definition *def; 5137c478bd9Sstevel@tonic-gate char *outfilename; 5147c478bd9Sstevel@tonic-gate long tell; 5157c478bd9Sstevel@tonic-gate char *guard; 5167c478bd9Sstevel@tonic-gate list *l; 5177c478bd9Sstevel@tonic-gate xdrfunc *xdrfuncp; 5187c478bd9Sstevel@tonic-gate int i; 5197c478bd9Sstevel@tonic-gate 5207c478bd9Sstevel@tonic-gate open_input(infile, define); 5217c478bd9Sstevel@tonic-gate outfilename = extend ? extendfile(infile, outfile) : outfile; 5227c478bd9Sstevel@tonic-gate open_output(infile, outfilename); 5237c478bd9Sstevel@tonic-gate add_warning(); 52461961e0fSrobinson if (outfilename || infile) 5257c478bd9Sstevel@tonic-gate guard = generate_guard(outfilename ? outfilename: infile); 52661961e0fSrobinson else 5277c478bd9Sstevel@tonic-gate guard = "STDIN_"; 5287c478bd9Sstevel@tonic-gate 52961961e0fSrobinson f_print(fout, "#ifndef _%s\n#define _%s\n\n", guard, guard); 5307c478bd9Sstevel@tonic-gate 5317c478bd9Sstevel@tonic-gate f_print(fout, "#include <rpc/rpc.h>\n"); 5327c478bd9Sstevel@tonic-gate 5337c478bd9Sstevel@tonic-gate if (mtflag) { 5347c478bd9Sstevel@tonic-gate f_print(fout, "#ifndef _KERNEL\n"); 5357c478bd9Sstevel@tonic-gate f_print(fout, "#include <synch.h>\n"); 5367c478bd9Sstevel@tonic-gate f_print(fout, "#include <thread.h>\n"); 5377c478bd9Sstevel@tonic-gate f_print(fout, "#endif /* !_KERNEL */\n"); 5387c478bd9Sstevel@tonic-gate }; 5397c478bd9Sstevel@tonic-gate 5407c478bd9Sstevel@tonic-gate /* put the C++ support */ 5417c478bd9Sstevel@tonic-gate if (Cflag && !CCflag) { 5427c478bd9Sstevel@tonic-gate f_print(fout, "\n#ifdef __cplusplus\n"); 5437c478bd9Sstevel@tonic-gate f_print(fout, "extern \"C\" {\n"); 5447c478bd9Sstevel@tonic-gate f_print(fout, "#endif\n\n"); 5457c478bd9Sstevel@tonic-gate } 5467c478bd9Sstevel@tonic-gate 5477c478bd9Sstevel@tonic-gate /* put in a typedef for quadprecision. Only with Cflag */ 5487c478bd9Sstevel@tonic-gate 549*d67944fbSScott Rotondo /* 550*d67944fbSScott Rotondo * declaration of struct rpcgen_table must go before 551*d67944fbSScott Rotondo * the definition of arrays like *_1_table[] 552*d67944fbSScott Rotondo */ 553*d67944fbSScott Rotondo if (tblflag) { 554*d67944fbSScott Rotondo f_print(fout, rpcgen_table_dcl1); 555*d67944fbSScott Rotondo if (tirpcflag) 556*d67944fbSScott Rotondo f_print(fout, rpcgen_table_proc); 557*d67944fbSScott Rotondo else 558*d67944fbSScott Rotondo f_print(fout, rpcgen_table_proc_b); 559*d67944fbSScott Rotondo f_print(fout, rpcgen_table_dcl2); 560*d67944fbSScott Rotondo } 561*d67944fbSScott Rotondo 5627c478bd9Sstevel@tonic-gate tell = ftell(fout); 5637c478bd9Sstevel@tonic-gate 5647c478bd9Sstevel@tonic-gate /* print data definitions */ 56561961e0fSrobinson while (def = get_definition()) 5667c478bd9Sstevel@tonic-gate print_datadef(def); 5677c478bd9Sstevel@tonic-gate 5687c478bd9Sstevel@tonic-gate /* 5697c478bd9Sstevel@tonic-gate * print function declarations. 5707c478bd9Sstevel@tonic-gate * Do this after data definitions because they might be used as 5717c478bd9Sstevel@tonic-gate * arguments for functions 5727c478bd9Sstevel@tonic-gate */ 57361961e0fSrobinson for (l = defined; l != NULL; l = l->next) 5747c478bd9Sstevel@tonic-gate print_funcdef(l->val); 5757c478bd9Sstevel@tonic-gate /* Now print all xdr func declarations */ 5767c478bd9Sstevel@tonic-gate if (xdrfunc_head != NULL) { 57761961e0fSrobinson f_print(fout, "\n/* the xdr functions */\n"); 5787c478bd9Sstevel@tonic-gate 5797c478bd9Sstevel@tonic-gate if (CCflag) { 5807c478bd9Sstevel@tonic-gate f_print(fout, "\n#ifdef __cplusplus\n"); 5817c478bd9Sstevel@tonic-gate f_print(fout, "extern \"C\" {\n"); 5827c478bd9Sstevel@tonic-gate f_print(fout, "#endif\n"); 5837c478bd9Sstevel@tonic-gate } 5847c478bd9Sstevel@tonic-gate 5857c478bd9Sstevel@tonic-gate if (!Cflag) { 5867c478bd9Sstevel@tonic-gate xdrfuncp = xdrfunc_head; 5877c478bd9Sstevel@tonic-gate while (xdrfuncp != NULL) { 5887c478bd9Sstevel@tonic-gate print_xdr_func_def(xdrfuncp->name, 5897c478bd9Sstevel@tonic-gate xdrfuncp->pointerp, 2); 5907c478bd9Sstevel@tonic-gate xdrfuncp = xdrfuncp->next; 5917c478bd9Sstevel@tonic-gate } 5927c478bd9Sstevel@tonic-gate } else { 5937c478bd9Sstevel@tonic-gate for (i = 1; i < 3; i++) { 5947c478bd9Sstevel@tonic-gate if (i == 1) 59561961e0fSrobinson f_print(fout, 59661961e0fSrobinson "\n#if defined(__STDC__) || defined(__cplusplus)\n"); 5977c478bd9Sstevel@tonic-gate else 5987c478bd9Sstevel@tonic-gate f_print(fout, "\n#else /* K&R C */\n"); 5997c478bd9Sstevel@tonic-gate 6007c478bd9Sstevel@tonic-gate xdrfuncp = xdrfunc_head; 6017c478bd9Sstevel@tonic-gate while (xdrfuncp != NULL) { 6027c478bd9Sstevel@tonic-gate print_xdr_func_def(xdrfuncp->name, 6037c478bd9Sstevel@tonic-gate xdrfuncp->pointerp, i); 6047c478bd9Sstevel@tonic-gate xdrfuncp = xdrfuncp->next; 6057c478bd9Sstevel@tonic-gate } 6067c478bd9Sstevel@tonic-gate } 6077c478bd9Sstevel@tonic-gate f_print(fout, "\n#endif /* K&R C */\n"); 6087c478bd9Sstevel@tonic-gate } 6097c478bd9Sstevel@tonic-gate } 6107c478bd9Sstevel@tonic-gate 6117c478bd9Sstevel@tonic-gate if (extend && tell == ftell(fout)) { 6127c478bd9Sstevel@tonic-gate (void) unlink(outfilename); 6137c478bd9Sstevel@tonic-gate } 6147c478bd9Sstevel@tonic-gate 6157c478bd9Sstevel@tonic-gate if (Cflag) { 6167c478bd9Sstevel@tonic-gate f_print(fout, "\n#ifdef __cplusplus\n"); 6177c478bd9Sstevel@tonic-gate f_print(fout, "}\n"); 6187c478bd9Sstevel@tonic-gate f_print(fout, "#endif\n"); 6197c478bd9Sstevel@tonic-gate } 6207c478bd9Sstevel@tonic-gate 6217c478bd9Sstevel@tonic-gate f_print(fout, "\n#endif /* !_%s */\n", guard); 6227c478bd9Sstevel@tonic-gate } 6237c478bd9Sstevel@tonic-gate 6247c478bd9Sstevel@tonic-gate /* 6257c478bd9Sstevel@tonic-gate * Compile into an RPC service 6267c478bd9Sstevel@tonic-gate */ 62761961e0fSrobinson static void 62861961e0fSrobinson s_output(int argc, char *argv[], char *infile, char *define, int extend, 62961961e0fSrobinson char *outfile, int nomain, int netflag) 6307c478bd9Sstevel@tonic-gate { 6317c478bd9Sstevel@tonic-gate char *include; 6327c478bd9Sstevel@tonic-gate definition *def; 6337c478bd9Sstevel@tonic-gate int foundprogram = 0; 6347c478bd9Sstevel@tonic-gate char *outfilename; 6357c478bd9Sstevel@tonic-gate 6367c478bd9Sstevel@tonic-gate open_input(infile, define); 6377c478bd9Sstevel@tonic-gate outfilename = extend ? extendfile(infile, outfile) : outfile; 6387c478bd9Sstevel@tonic-gate open_output(infile, outfilename); 6397c478bd9Sstevel@tonic-gate add_warning(); 6407c478bd9Sstevel@tonic-gate if (infile && (include = extendfile(infile, ".h"))) { 6417c478bd9Sstevel@tonic-gate f_print(fout, "#include \"%s\"\n", include); 6427c478bd9Sstevel@tonic-gate free(include); 6437c478bd9Sstevel@tonic-gate } else 6447c478bd9Sstevel@tonic-gate f_print(fout, "#include <rpc/rpc.h>\n"); 6457c478bd9Sstevel@tonic-gate 6467c478bd9Sstevel@tonic-gate f_print(fout, "#include <stdio.h>\n"); 6477c478bd9Sstevel@tonic-gate f_print(fout, "#include <stdlib.h> /* getenv, exit */\n"); 6487c478bd9Sstevel@tonic-gate f_print(fout, "#include <signal.h>\n"); 6497c478bd9Sstevel@tonic-gate 6507c478bd9Sstevel@tonic-gate if (Cflag) { 6517c478bd9Sstevel@tonic-gate f_print(fout, 6527c478bd9Sstevel@tonic-gate "#include <rpc/pmap_clnt.h> /* for pmap_unset */\n"); 6537c478bd9Sstevel@tonic-gate f_print(fout, "#include <string.h> /* strcmp */\n"); 6547c478bd9Sstevel@tonic-gate } 6557c478bd9Sstevel@tonic-gate if (strcmp(svcclosetime, "-1") == 0) 6567c478bd9Sstevel@tonic-gate indefinitewait = 1; 6577c478bd9Sstevel@tonic-gate else if (strcmp(svcclosetime, "0") == 0) 6587c478bd9Sstevel@tonic-gate exitnow = 1; 65961961e0fSrobinson else if (inetdflag || pmflag) 6607c478bd9Sstevel@tonic-gate timerflag = 1; 6617c478bd9Sstevel@tonic-gate 6627c478bd9Sstevel@tonic-gate if (!tirpcflag && inetdflag) 6637c478bd9Sstevel@tonic-gate f_print(fout, "#include <sys/termios.h> /* TIOCNOTTY */\n"); 66461961e0fSrobinson if (Cflag && (inetdflag || pmflag)) 6657c478bd9Sstevel@tonic-gate if (tirpcflag) 6667c478bd9Sstevel@tonic-gate f_print(fout, "#include <unistd.h> /* setsid */\n"); 6677c478bd9Sstevel@tonic-gate if (tirpcflag) 6687c478bd9Sstevel@tonic-gate f_print(fout, "#include <sys/types.h>\n"); 6697c478bd9Sstevel@tonic-gate 6707c478bd9Sstevel@tonic-gate f_print(fout, "#include <memory.h>\n"); 6717c478bd9Sstevel@tonic-gate f_print(fout, "#include <stropts.h>\n"); 6727c478bd9Sstevel@tonic-gate if (inetdflag || !tirpcflag) { 6737c478bd9Sstevel@tonic-gate f_print(fout, "#include <sys/socket.h>\n"); 6747c478bd9Sstevel@tonic-gate f_print(fout, "#include <netinet/in.h>\n"); 6757c478bd9Sstevel@tonic-gate f_print(fout, "#include <rpc/svc_soc.h>\n"); 6767c478bd9Sstevel@tonic-gate } 6777c478bd9Sstevel@tonic-gate 67861961e0fSrobinson if ((netflag || pmflag) && tirpcflag && !nomain) 6797c478bd9Sstevel@tonic-gate f_print(fout, "#include <netconfig.h>\n"); 6807c478bd9Sstevel@tonic-gate if (tirpcflag) 6817c478bd9Sstevel@tonic-gate f_print(fout, "#include <sys/resource.h> /* rlimit */\n"); 6827c478bd9Sstevel@tonic-gate if (logflag || inetdflag || pmflag) 6837c478bd9Sstevel@tonic-gate f_print(fout, "#include <syslog.h>\n"); 6847c478bd9Sstevel@tonic-gate 6857c478bd9Sstevel@tonic-gate /* for ANSI-C */ 6867c478bd9Sstevel@tonic-gate if (Cflag) 6877c478bd9Sstevel@tonic-gate f_print(fout, 6887c478bd9Sstevel@tonic-gate "\n#ifndef SIG_PF\n#define SIG_PF void(*)\ 6897c478bd9Sstevel@tonic-gate (int)\n#endif\n"); 6907c478bd9Sstevel@tonic-gate 6917c478bd9Sstevel@tonic-gate f_print(fout, "\n#ifdef DEBUG\n#define RPC_SVC_FG\n#endif\n"); 6927c478bd9Sstevel@tonic-gate if (timerflag) 6937c478bd9Sstevel@tonic-gate f_print(fout, "\n#define _RPCSVC_CLOSEDOWN %s\n", 6947c478bd9Sstevel@tonic-gate svcclosetime); 69561961e0fSrobinson while (def = get_definition()) 6967c478bd9Sstevel@tonic-gate foundprogram |= (def->def_kind == DEF_PROGRAM); 6977c478bd9Sstevel@tonic-gate if (extend && !foundprogram) { 6987c478bd9Sstevel@tonic-gate (void) unlink(outfilename); 6997c478bd9Sstevel@tonic-gate return; 7007c478bd9Sstevel@tonic-gate } 7017c478bd9Sstevel@tonic-gate write_most(infile, netflag, nomain); 7027c478bd9Sstevel@tonic-gate if (!nomain) { 7037c478bd9Sstevel@tonic-gate if (!do_registers(argc, argv)) { 7047c478bd9Sstevel@tonic-gate if (outfilename) 7057c478bd9Sstevel@tonic-gate (void) unlink(outfilename); 7067c478bd9Sstevel@tonic-gate usage(); 7077c478bd9Sstevel@tonic-gate } 7087c478bd9Sstevel@tonic-gate write_rest(); 7097c478bd9Sstevel@tonic-gate } 7107c478bd9Sstevel@tonic-gate } 7117c478bd9Sstevel@tonic-gate 7127c478bd9Sstevel@tonic-gate /* 7137c478bd9Sstevel@tonic-gate * generate client side stubs 7147c478bd9Sstevel@tonic-gate */ 71561961e0fSrobinson static void 71661961e0fSrobinson l_output(char *infile, char *define, int extend, char *outfile) 7177c478bd9Sstevel@tonic-gate { 7187c478bd9Sstevel@tonic-gate char *include; 7197c478bd9Sstevel@tonic-gate definition *def; 7207c478bd9Sstevel@tonic-gate int foundprogram = 0; 7217c478bd9Sstevel@tonic-gate char *outfilename; 7227c478bd9Sstevel@tonic-gate 7237c478bd9Sstevel@tonic-gate open_input(infile, define); 7247c478bd9Sstevel@tonic-gate outfilename = extend ? extendfile(infile, outfile) : outfile; 7257c478bd9Sstevel@tonic-gate open_output(infile, outfilename); 7267c478bd9Sstevel@tonic-gate add_warning(); 7277c478bd9Sstevel@tonic-gate if (Cflag) 7287c478bd9Sstevel@tonic-gate f_print(fout, "#include <memory.h> /* for memset */\n"); 7297c478bd9Sstevel@tonic-gate if (infile && (include = extendfile(infile, ".h"))) { 7307c478bd9Sstevel@tonic-gate f_print(fout, "#include \"%s\"\n", include); 7317c478bd9Sstevel@tonic-gate free(include); 7327c478bd9Sstevel@tonic-gate } else 7337c478bd9Sstevel@tonic-gate f_print(fout, "#include <rpc/rpc.h>\n"); 7347c478bd9Sstevel@tonic-gate 7357c478bd9Sstevel@tonic-gate f_print(fout, "#ifndef _KERNEL\n"); 7367c478bd9Sstevel@tonic-gate f_print(fout, "#include <stdio.h>\n"); 7377c478bd9Sstevel@tonic-gate f_print(fout, "#include <stdlib.h> /* getenv, exit */\n"); 7387c478bd9Sstevel@tonic-gate f_print(fout, "#endif /* !_KERNEL */\n"); 7397c478bd9Sstevel@tonic-gate 74061961e0fSrobinson while (def = get_definition()) 7417c478bd9Sstevel@tonic-gate foundprogram |= (def->def_kind == DEF_PROGRAM); 7427c478bd9Sstevel@tonic-gate if (extend && !foundprogram) { 7437c478bd9Sstevel@tonic-gate (void) unlink(outfilename); 7447c478bd9Sstevel@tonic-gate return; 7457c478bd9Sstevel@tonic-gate } 7467c478bd9Sstevel@tonic-gate write_stubs(); 7477c478bd9Sstevel@tonic-gate } 7487c478bd9Sstevel@tonic-gate 7497c478bd9Sstevel@tonic-gate /* 7507c478bd9Sstevel@tonic-gate * generate the dispatch table 7517c478bd9Sstevel@tonic-gate */ 75261961e0fSrobinson static void 75361961e0fSrobinson t_output(char *infile, char *define, int extend, char *outfile) 7547c478bd9Sstevel@tonic-gate { 7557c478bd9Sstevel@tonic-gate definition *def; 7567c478bd9Sstevel@tonic-gate int foundprogram = 0; 7577c478bd9Sstevel@tonic-gate char *outfilename; 7587c478bd9Sstevel@tonic-gate 7597c478bd9Sstevel@tonic-gate open_input(infile, define); 7607c478bd9Sstevel@tonic-gate outfilename = extend ? extendfile(infile, outfile) : outfile; 7617c478bd9Sstevel@tonic-gate open_output(infile, outfilename); 7627c478bd9Sstevel@tonic-gate add_warning(); 7637c478bd9Sstevel@tonic-gate while (def = get_definition()) { 7647c478bd9Sstevel@tonic-gate foundprogram |= (def->def_kind == DEF_PROGRAM); 7657c478bd9Sstevel@tonic-gate } 7667c478bd9Sstevel@tonic-gate if (extend && !foundprogram) { 7677c478bd9Sstevel@tonic-gate (void) unlink(outfilename); 7687c478bd9Sstevel@tonic-gate return; 7697c478bd9Sstevel@tonic-gate } 7707c478bd9Sstevel@tonic-gate write_tables(); 7717c478bd9Sstevel@tonic-gate } 7727c478bd9Sstevel@tonic-gate 7737c478bd9Sstevel@tonic-gate /* sample routine for the server template */ 77461961e0fSrobinson static void 77561961e0fSrobinson svc_output(char *infile, char *define, int extend, char *outfile) 7767c478bd9Sstevel@tonic-gate { 7777c478bd9Sstevel@tonic-gate definition *def; 7787c478bd9Sstevel@tonic-gate char *include; 7797c478bd9Sstevel@tonic-gate char *outfilename; 7807c478bd9Sstevel@tonic-gate long tell; 7817c478bd9Sstevel@tonic-gate open_input(infile, define); 7827c478bd9Sstevel@tonic-gate outfilename = extend ? extendfile(infile, outfile) : outfile; 7837c478bd9Sstevel@tonic-gate checkfiles(infile, outfilename); 7847c478bd9Sstevel@tonic-gate /* 7857c478bd9Sstevel@tonic-gate * Check if outfile already exists. 7867c478bd9Sstevel@tonic-gate * if so, print an error message and exit 7877c478bd9Sstevel@tonic-gate */ 7887c478bd9Sstevel@tonic-gate open_output(infile, outfilename); 7897c478bd9Sstevel@tonic-gate add_sample_msg(); 7907c478bd9Sstevel@tonic-gate 7917c478bd9Sstevel@tonic-gate if (infile && (include = extendfile(infile, ".h"))) { 7927c478bd9Sstevel@tonic-gate f_print(fout, "#include \"%s\"\n", include); 7937c478bd9Sstevel@tonic-gate free(include); 7947c478bd9Sstevel@tonic-gate } else { 7957c478bd9Sstevel@tonic-gate f_print(fout, "#include <rpc/rpc.h>\n"); 7967c478bd9Sstevel@tonic-gate } 7977c478bd9Sstevel@tonic-gate 7987c478bd9Sstevel@tonic-gate f_print(fout, "#include <stdio.h>\n"); 7997c478bd9Sstevel@tonic-gate f_print(fout, "#include <stdlib.h> /* getenv, exit */\n"); 8007c478bd9Sstevel@tonic-gate f_print(fout, "#include <signal.h>\n"); 8017c478bd9Sstevel@tonic-gate 8027c478bd9Sstevel@tonic-gate tell = ftell(fout); 80361961e0fSrobinson while (def = get_definition()) 8047c478bd9Sstevel@tonic-gate write_sample_svc(def); 80561961e0fSrobinson if (extend && tell == ftell(fout)) 8067c478bd9Sstevel@tonic-gate (void) unlink(outfilename); 8077c478bd9Sstevel@tonic-gate } 8087c478bd9Sstevel@tonic-gate 8097c478bd9Sstevel@tonic-gate /* sample main routine for client */ 81061961e0fSrobinson static void 81161961e0fSrobinson clnt_output(char *infile, char *define, int extend, char *outfile) 8127c478bd9Sstevel@tonic-gate { 8137c478bd9Sstevel@tonic-gate definition *def; 8147c478bd9Sstevel@tonic-gate char *include; 8157c478bd9Sstevel@tonic-gate char *outfilename; 8167c478bd9Sstevel@tonic-gate long tell; 8177c478bd9Sstevel@tonic-gate int has_program = 0; 8187c478bd9Sstevel@tonic-gate 8197c478bd9Sstevel@tonic-gate open_input(infile, define); 8207c478bd9Sstevel@tonic-gate outfilename = extend ? extendfile(infile, outfile) : outfile; 8217c478bd9Sstevel@tonic-gate checkfiles(infile, outfilename); 8227c478bd9Sstevel@tonic-gate /* 8237c478bd9Sstevel@tonic-gate * Check if outfile already exists. 8247c478bd9Sstevel@tonic-gate * if so, print an error message and exit 8257c478bd9Sstevel@tonic-gate */ 8267c478bd9Sstevel@tonic-gate 8277c478bd9Sstevel@tonic-gate open_output(infile, outfilename); 8287c478bd9Sstevel@tonic-gate add_sample_msg(); 8297c478bd9Sstevel@tonic-gate if (infile && (include = extendfile(infile, ".h"))) { 8307c478bd9Sstevel@tonic-gate f_print(fout, "#include \"%s\"\n", include); 8317c478bd9Sstevel@tonic-gate free(include); 8327c478bd9Sstevel@tonic-gate } else 8337c478bd9Sstevel@tonic-gate f_print(fout, "#include <rpc/rpc.h>\n"); 8347c478bd9Sstevel@tonic-gate 8357c478bd9Sstevel@tonic-gate f_print(fout, "#include <stdio.h>\n"); 8367c478bd9Sstevel@tonic-gate f_print(fout, "#include <stdlib.h> /* getenv, exit */\n"); 8377c478bd9Sstevel@tonic-gate 8387c478bd9Sstevel@tonic-gate tell = ftell(fout); 83961961e0fSrobinson while (def = get_definition()) 8407c478bd9Sstevel@tonic-gate has_program += write_sample_clnt(def); 8417c478bd9Sstevel@tonic-gate 8427c478bd9Sstevel@tonic-gate if (has_program) 8437c478bd9Sstevel@tonic-gate write_sample_clnt_main(); 8447c478bd9Sstevel@tonic-gate 84561961e0fSrobinson if (extend && tell == ftell(fout)) 8467c478bd9Sstevel@tonic-gate (void) unlink(outfilename); 8477c478bd9Sstevel@tonic-gate } 8487c478bd9Sstevel@tonic-gate 8497c478bd9Sstevel@tonic-gate 85061961e0fSrobinson static void 85161961e0fSrobinson mkfile_output(struct commandline *cmd) 8527c478bd9Sstevel@tonic-gate { 8537c478bd9Sstevel@tonic-gate char *mkfilename, *clientname, *clntname, *xdrname, *hdrname; 8547c478bd9Sstevel@tonic-gate char *servername, *svcname, *servprogname, *clntprogname; 8557c478bd9Sstevel@tonic-gate char *temp; 8567c478bd9Sstevel@tonic-gate 8577c478bd9Sstevel@tonic-gate svcname = file_name(cmd->infile, "_svc.c"); 8587c478bd9Sstevel@tonic-gate clntname = file_name(cmd->infile, "_clnt.c"); 8597c478bd9Sstevel@tonic-gate xdrname = file_name(cmd->infile, "_xdr.c"); 8607c478bd9Sstevel@tonic-gate hdrname = file_name(cmd->infile, ".h"); 8617c478bd9Sstevel@tonic-gate 8627c478bd9Sstevel@tonic-gate 8637c478bd9Sstevel@tonic-gate if (allfiles) { 8647c478bd9Sstevel@tonic-gate servername = extendfile(cmd->infile, "_server.c"); 8657c478bd9Sstevel@tonic-gate clientname = extendfile(cmd->infile, "_client.c"); 8667c478bd9Sstevel@tonic-gate } else { 8677c478bd9Sstevel@tonic-gate servername = " "; 8687c478bd9Sstevel@tonic-gate clientname = " "; 8697c478bd9Sstevel@tonic-gate } 8707c478bd9Sstevel@tonic-gate servprogname = extendfile(cmd->infile, "_server"); 8717c478bd9Sstevel@tonic-gate clntprogname = extendfile(cmd->infile, "_client"); 8727c478bd9Sstevel@tonic-gate 8737c478bd9Sstevel@tonic-gate if (allfiles) { 87461961e0fSrobinson mkfilename = malloc(strlen("makefile.") + 8757c478bd9Sstevel@tonic-gate strlen(cmd->infile) + 1); 8767c478bd9Sstevel@tonic-gate if (mkfilename == NULL) { 8777c478bd9Sstevel@tonic-gate f_print(stderr, "Out of memory!\n"); 8787c478bd9Sstevel@tonic-gate return; 8797c478bd9Sstevel@tonic-gate } 8807c478bd9Sstevel@tonic-gate temp = (char *)rindex(cmd->infile, '.'); 88161961e0fSrobinson (void) strcpy(mkfilename, "makefile."); 8827c478bd9Sstevel@tonic-gate (void) strncat(mkfilename, cmd->infile, 8837c478bd9Sstevel@tonic-gate (temp - cmd->infile)); 8847c478bd9Sstevel@tonic-gate } else 8857c478bd9Sstevel@tonic-gate mkfilename = cmd->outfile; 8867c478bd9Sstevel@tonic-gate 8877c478bd9Sstevel@tonic-gate 8887c478bd9Sstevel@tonic-gate checkfiles(NULL, mkfilename); 8897c478bd9Sstevel@tonic-gate open_output(NULL, mkfilename); 8907c478bd9Sstevel@tonic-gate 8917c478bd9Sstevel@tonic-gate f_print(fout, "\n# This is a template makefile generated\ 8927c478bd9Sstevel@tonic-gate by rpcgen \n"); 8937c478bd9Sstevel@tonic-gate 8947c478bd9Sstevel@tonic-gate f_print(fout, "\n# Parameters \n\n"); 8957c478bd9Sstevel@tonic-gate 8967c478bd9Sstevel@tonic-gate f_print(fout, "CLIENT = %s\nSERVER = %s\n\n", 8977c478bd9Sstevel@tonic-gate clntprogname, servprogname); 8987c478bd9Sstevel@tonic-gate f_print(fout, "SOURCES_CLNT.c = \nSOURCES_CLNT.h = \n"); 8997c478bd9Sstevel@tonic-gate f_print(fout, "SOURCES_SVC.c = \nSOURCES_SVC.h = \n"); 9007c478bd9Sstevel@tonic-gate f_print(fout, "SOURCES.x = %s\n\n", cmd->infile); 9017c478bd9Sstevel@tonic-gate f_print(fout, "TARGETS_SVC.c = %s %s %s \n", 9027c478bd9Sstevel@tonic-gate svcname, servername, xdrname); 9037c478bd9Sstevel@tonic-gate f_print(fout, "TARGETS_CLNT.c = %s %s %s \n", 9047c478bd9Sstevel@tonic-gate clntname, clientname, xdrname); 9057c478bd9Sstevel@tonic-gate f_print(fout, "TARGETS = %s %s %s %s %s %s\n\n", 9067c478bd9Sstevel@tonic-gate hdrname, xdrname, clntname, 9077c478bd9Sstevel@tonic-gate svcname, clientname, servername); 9087c478bd9Sstevel@tonic-gate 9097c478bd9Sstevel@tonic-gate f_print(fout, "OBJECTS_CLNT = $(SOURCES_CLNT.c:%%.c=%%.o) " 9107c478bd9Sstevel@tonic-gate "$(TARGETS_CLNT.c:%%.c=%%.o) "); 9117c478bd9Sstevel@tonic-gate 9127c478bd9Sstevel@tonic-gate f_print(fout, "\nOBJECTS_SVC = $(SOURCES_SVC.c:%%.c=%%.o) " 9137c478bd9Sstevel@tonic-gate "$(TARGETS_SVC.c:%%.c=%%.o) "); 9147c478bd9Sstevel@tonic-gate 9157c478bd9Sstevel@tonic-gate 9167c478bd9Sstevel@tonic-gate f_print(fout, "\n# Compiler flags \n"); 9177c478bd9Sstevel@tonic-gate if (mtflag) 9187c478bd9Sstevel@tonic-gate f_print(fout, "\nCPPFLAGS += -D_REENTRANT\n" 9197c478bd9Sstevel@tonic-gate "CFLAGS += -g\nLDLIBS += -lnsl\n"); 9207c478bd9Sstevel@tonic-gate else 9217c478bd9Sstevel@tonic-gate f_print(fout, "\nCFLAGS += -g \nLDLIBS += -lnsl\n"); 9227c478bd9Sstevel@tonic-gate f_print(fout, "RPCGENFLAGS = \n"); 9237c478bd9Sstevel@tonic-gate 9247c478bd9Sstevel@tonic-gate f_print(fout, "\n# Targets \n\n"); 9257c478bd9Sstevel@tonic-gate 9267c478bd9Sstevel@tonic-gate f_print(fout, "all : $(CLIENT) $(SERVER)\n\n"); 9277c478bd9Sstevel@tonic-gate f_print(fout, "$(TARGETS) : $(SOURCES.x) \n"); 9287c478bd9Sstevel@tonic-gate f_print(fout, "\trpcgen $(RPCGENFLAGS) $(SOURCES.x)\n\n"); 9297c478bd9Sstevel@tonic-gate f_print(fout, "$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) \ 9307c478bd9Sstevel@tonic-gate $(TARGETS_CLNT.c) \n\n"); 9317c478bd9Sstevel@tonic-gate 9327c478bd9Sstevel@tonic-gate f_print(fout, "$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) \ 9337c478bd9Sstevel@tonic-gate $(TARGETS_SVC.c) \n\n"); 9347c478bd9Sstevel@tonic-gate f_print(fout, "$(CLIENT) : $(OBJECTS_CLNT) \n"); 9357c478bd9Sstevel@tonic-gate f_print(fout, "\t$(LINK.c) -o $(CLIENT) $(OBJECTS_CLNT) \ 9367c478bd9Sstevel@tonic-gate $(LDLIBS) \n\n"); 9377c478bd9Sstevel@tonic-gate f_print(fout, "$(SERVER) : $(OBJECTS_SVC) \n"); 9387c478bd9Sstevel@tonic-gate f_print(fout, "\t$(LINK.c) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)\n\n "); 9397c478bd9Sstevel@tonic-gate f_print(fout, "clean:\n\t $(RM) core $(TARGETS) $(OBJECTS_CLNT) \ 9407c478bd9Sstevel@tonic-gate $(OBJECTS_SVC) $(CLIENT) $(SERVER)\n\n"); 9417c478bd9Sstevel@tonic-gate } 9427c478bd9Sstevel@tonic-gate 9437c478bd9Sstevel@tonic-gate 9447c478bd9Sstevel@tonic-gate /* 9457c478bd9Sstevel@tonic-gate * Perform registrations for service output 9467c478bd9Sstevel@tonic-gate * Return 0 if failed; 1 otherwise. 9477c478bd9Sstevel@tonic-gate */ 9487c478bd9Sstevel@tonic-gate static int 94961961e0fSrobinson do_registers(int argc, char *argv[]) 9507c478bd9Sstevel@tonic-gate { 9517c478bd9Sstevel@tonic-gate int i; 9527c478bd9Sstevel@tonic-gate 9537c478bd9Sstevel@tonic-gate if (inetdflag || !tirpcflag) { 9547c478bd9Sstevel@tonic-gate for (i = 1; i < argc; i++) { 9557c478bd9Sstevel@tonic-gate if (streq(argv[i], "-s")) { 9567c478bd9Sstevel@tonic-gate if (!check_nettype(argv[i + 1], 9577c478bd9Sstevel@tonic-gate valid_i_nettypes)) 9587c478bd9Sstevel@tonic-gate return (0); 9597c478bd9Sstevel@tonic-gate write_inetd_register(argv[i + 1]); 9607c478bd9Sstevel@tonic-gate i++; 9617c478bd9Sstevel@tonic-gate } 9627c478bd9Sstevel@tonic-gate } 9637c478bd9Sstevel@tonic-gate } else { 9647c478bd9Sstevel@tonic-gate for (i = 1; i < argc; i++) 9657c478bd9Sstevel@tonic-gate if (streq(argv[i], "-s")) { 9667c478bd9Sstevel@tonic-gate if (!check_nettype(argv[i + 1], 9677c478bd9Sstevel@tonic-gate valid_ti_nettypes)) 9687c478bd9Sstevel@tonic-gate return (0); 9697c478bd9Sstevel@tonic-gate write_nettype_register(argv[i + 1]); 9707c478bd9Sstevel@tonic-gate i++; 9717c478bd9Sstevel@tonic-gate } else if (streq(argv[i], "-n")) { 9727c478bd9Sstevel@tonic-gate write_netid_register(argv[i + 1]); 9737c478bd9Sstevel@tonic-gate i++; 9747c478bd9Sstevel@tonic-gate } 9757c478bd9Sstevel@tonic-gate } 9767c478bd9Sstevel@tonic-gate return (1); 9777c478bd9Sstevel@tonic-gate } 9787c478bd9Sstevel@tonic-gate 9797c478bd9Sstevel@tonic-gate /* 9807c478bd9Sstevel@tonic-gate * Add another argument to the arg list 9817c478bd9Sstevel@tonic-gate */ 9827c478bd9Sstevel@tonic-gate static void 98361961e0fSrobinson addarg(char *cp) 9847c478bd9Sstevel@tonic-gate { 9857c478bd9Sstevel@tonic-gate if (argcount >= ARGLISTLEN) { 9867c478bd9Sstevel@tonic-gate f_print(stderr, "rpcgen: too many defines\n"); 9877c478bd9Sstevel@tonic-gate crash(); 9887c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 9897c478bd9Sstevel@tonic-gate } 9907c478bd9Sstevel@tonic-gate arglist[argcount++] = cp; 9917c478bd9Sstevel@tonic-gate } 9927c478bd9Sstevel@tonic-gate 9937c478bd9Sstevel@tonic-gate static void 99461961e0fSrobinson putarg(int where, char *cp) 9957c478bd9Sstevel@tonic-gate { 9967c478bd9Sstevel@tonic-gate if (where >= ARGLISTLEN) { 9977c478bd9Sstevel@tonic-gate f_print(stderr, "rpcgen: arglist coding error\n"); 9987c478bd9Sstevel@tonic-gate crash(); 9997c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 10007c478bd9Sstevel@tonic-gate } 10017c478bd9Sstevel@tonic-gate arglist[where] = cp; 10027c478bd9Sstevel@tonic-gate } 10037c478bd9Sstevel@tonic-gate 10047c478bd9Sstevel@tonic-gate /* 10057c478bd9Sstevel@tonic-gate * if input file is stdin and an output file is specified then complain 10067c478bd9Sstevel@tonic-gate * if the file already exists. Otherwise the file may get overwritten 10077c478bd9Sstevel@tonic-gate * If input file does not exist, exit with an error 10087c478bd9Sstevel@tonic-gate */ 10097c478bd9Sstevel@tonic-gate static void 101061961e0fSrobinson checkfiles(char *infile, char *outfile) 10117c478bd9Sstevel@tonic-gate { 10127c478bd9Sstevel@tonic-gate struct stat buf; 10137c478bd9Sstevel@tonic-gate 101461961e0fSrobinson if (infile) { /* infile ! = NULL */ 101561961e0fSrobinson if (stat(infile, &buf) < 0) { 10167c478bd9Sstevel@tonic-gate perror(infile); 10177c478bd9Sstevel@tonic-gate crash(); 101861961e0fSrobinson } 101961961e0fSrobinson } 10207c478bd9Sstevel@tonic-gate if (outfile) { 10217c478bd9Sstevel@tonic-gate if (stat(outfile, &buf) < 0) 10227c478bd9Sstevel@tonic-gate return; /* file does not exist */ 10237c478bd9Sstevel@tonic-gate f_print(stderr, 10247c478bd9Sstevel@tonic-gate "file '%s' already exists and may be overwritten\n", 10257c478bd9Sstevel@tonic-gate outfile); 10267c478bd9Sstevel@tonic-gate crash(); 10277c478bd9Sstevel@tonic-gate } 10287c478bd9Sstevel@tonic-gate } 10297c478bd9Sstevel@tonic-gate 10307c478bd9Sstevel@tonic-gate /* 10317c478bd9Sstevel@tonic-gate * Parse command line arguments 10327c478bd9Sstevel@tonic-gate */ 103361961e0fSrobinson static uint_t 103461961e0fSrobinson parseargs(int argc, char *argv[], struct commandline *cmd) 10357c478bd9Sstevel@tonic-gate { 10367c478bd9Sstevel@tonic-gate int i; 10377c478bd9Sstevel@tonic-gate int j; 10387c478bd9Sstevel@tonic-gate char c, ch; 10397c478bd9Sstevel@tonic-gate char flag[(1 << 8 * sizeof (char))]; 10407c478bd9Sstevel@tonic-gate int nflags; 10417c478bd9Sstevel@tonic-gate 10427c478bd9Sstevel@tonic-gate cmdname = argv[0]; 10437c478bd9Sstevel@tonic-gate cmd->infile = cmd->outfile = NULL; 104461961e0fSrobinson if (argc < 2) 10457c478bd9Sstevel@tonic-gate return (0); 10467c478bd9Sstevel@tonic-gate allfiles = 0; 10477c478bd9Sstevel@tonic-gate flag['c'] = 0; 10487c478bd9Sstevel@tonic-gate flag['h'] = 0; 10497c478bd9Sstevel@tonic-gate flag['l'] = 0; 10507c478bd9Sstevel@tonic-gate flag['m'] = 0; 10517c478bd9Sstevel@tonic-gate flag['o'] = 0; 10527c478bd9Sstevel@tonic-gate flag['s'] = 0; 10537c478bd9Sstevel@tonic-gate flag['n'] = 0; 10547c478bd9Sstevel@tonic-gate flag['t'] = 0; 10557c478bd9Sstevel@tonic-gate flag['S'] = 0; 10567c478bd9Sstevel@tonic-gate flag['C'] = 0; 10577c478bd9Sstevel@tonic-gate flag['M'] = 0; 10587c478bd9Sstevel@tonic-gate 10597c478bd9Sstevel@tonic-gate for (i = 1; i < argc; i++) { 10607c478bd9Sstevel@tonic-gate if (argv[i][0] != '-') { 10617c478bd9Sstevel@tonic-gate if (cmd->infile) { 10627c478bd9Sstevel@tonic-gate f_print(stderr, 10637c478bd9Sstevel@tonic-gate "Cannot specify more than one input file.\n"); 10647c478bd9Sstevel@tonic-gate 10657c478bd9Sstevel@tonic-gate return (0); 10667c478bd9Sstevel@tonic-gate } 10677c478bd9Sstevel@tonic-gate cmd->infile = argv[i]; 10687c478bd9Sstevel@tonic-gate } else { 10697c478bd9Sstevel@tonic-gate for (j = 1; argv[i][j] != 0; j++) { 10707c478bd9Sstevel@tonic-gate c = argv[i][j]; 10717c478bd9Sstevel@tonic-gate switch (c) { 10727c478bd9Sstevel@tonic-gate case 'a': 10737c478bd9Sstevel@tonic-gate allfiles = 1; 10747c478bd9Sstevel@tonic-gate break; 10757c478bd9Sstevel@tonic-gate case 'c': 10767c478bd9Sstevel@tonic-gate case 'h': 10777c478bd9Sstevel@tonic-gate case 'l': 10787c478bd9Sstevel@tonic-gate case 'm': 10797c478bd9Sstevel@tonic-gate case 't': 108061961e0fSrobinson if (flag[c]) 10817c478bd9Sstevel@tonic-gate return (0); 10827c478bd9Sstevel@tonic-gate flag[c] = 1; 10837c478bd9Sstevel@tonic-gate break; 10847c478bd9Sstevel@tonic-gate case 'S': 10857c478bd9Sstevel@tonic-gate /* 10867c478bd9Sstevel@tonic-gate * sample flag: Ss or Sc. 10877c478bd9Sstevel@tonic-gate * Ss means set flag['S']; 10887c478bd9Sstevel@tonic-gate * Sc means set flag['C']; 10897c478bd9Sstevel@tonic-gate * Sm means set flag['M']; 10907c478bd9Sstevel@tonic-gate */ 10917c478bd9Sstevel@tonic-gate ch = argv[i][++j]; /* get next char */ 10927c478bd9Sstevel@tonic-gate if (ch == 's') 10937c478bd9Sstevel@tonic-gate ch = 'S'; 10947c478bd9Sstevel@tonic-gate else if (ch == 'c') 10957c478bd9Sstevel@tonic-gate ch = 'C'; 10967c478bd9Sstevel@tonic-gate else if (ch == 'm') 10977c478bd9Sstevel@tonic-gate ch = 'M'; 10987c478bd9Sstevel@tonic-gate else 10997c478bd9Sstevel@tonic-gate return (0); 11007c478bd9Sstevel@tonic-gate 110161961e0fSrobinson if (flag[ch]) 11027c478bd9Sstevel@tonic-gate return (0); 11037c478bd9Sstevel@tonic-gate flag[ch] = 1; 11047c478bd9Sstevel@tonic-gate break; 11057c478bd9Sstevel@tonic-gate case 'C': /* ANSI C syntax */ 11067c478bd9Sstevel@tonic-gate Cflag = 1; 11077c478bd9Sstevel@tonic-gate ch = argv[i][j+1]; /* get next char */ 11087c478bd9Sstevel@tonic-gate 11097c478bd9Sstevel@tonic-gate if (ch != 'C') 11107c478bd9Sstevel@tonic-gate break; 11117c478bd9Sstevel@tonic-gate CCflag = 1; 11127c478bd9Sstevel@tonic-gate break; 11137c478bd9Sstevel@tonic-gate case 'b': 11147c478bd9Sstevel@tonic-gate /* 11157c478bd9Sstevel@tonic-gate * Turn TIRPC flag off for 11167c478bd9Sstevel@tonic-gate * generating backward compatible 11177c478bd9Sstevel@tonic-gate * code 11187c478bd9Sstevel@tonic-gate */ 11197c478bd9Sstevel@tonic-gate tirpcflag = 0; 11207c478bd9Sstevel@tonic-gate break; 11217c478bd9Sstevel@tonic-gate 11227c478bd9Sstevel@tonic-gate case 'I': 11237c478bd9Sstevel@tonic-gate inetdflag = 1; 11247c478bd9Sstevel@tonic-gate break; 11257c478bd9Sstevel@tonic-gate case 'N': 11267c478bd9Sstevel@tonic-gate newstyle = 1; 11277c478bd9Sstevel@tonic-gate break; 11287c478bd9Sstevel@tonic-gate case 'L': 11297c478bd9Sstevel@tonic-gate logflag = 1; 11307c478bd9Sstevel@tonic-gate break; 11317c478bd9Sstevel@tonic-gate case 'K': 113261961e0fSrobinson if (++i == argc) 11337c478bd9Sstevel@tonic-gate return (0); 11347c478bd9Sstevel@tonic-gate svcclosetime = argv[i]; 11357c478bd9Sstevel@tonic-gate goto nextarg; 11367c478bd9Sstevel@tonic-gate case 'T': 11377c478bd9Sstevel@tonic-gate tblflag = 1; 11387c478bd9Sstevel@tonic-gate break; 11397c478bd9Sstevel@tonic-gate case 'A': 11407c478bd9Sstevel@tonic-gate mtauto = 1; 114161961e0fSrobinson /* FALLTHRU */ 11427c478bd9Sstevel@tonic-gate case 'M': 11437c478bd9Sstevel@tonic-gate mtflag = 1; 11447c478bd9Sstevel@tonic-gate break; 11457c478bd9Sstevel@tonic-gate case 'i' : 114661961e0fSrobinson if (++i == argc) 11477c478bd9Sstevel@tonic-gate return (0); 11487c478bd9Sstevel@tonic-gate inlinelen = atoi(argv[i]); 11497c478bd9Sstevel@tonic-gate goto nextarg; 11507c478bd9Sstevel@tonic-gate case 'n': 11517c478bd9Sstevel@tonic-gate case 'o': 11527c478bd9Sstevel@tonic-gate case 's': 11537c478bd9Sstevel@tonic-gate if (argv[i][j - 1] != '-' || 115461961e0fSrobinson argv[i][j + 1] != 0) 11557c478bd9Sstevel@tonic-gate return (0); 11567c478bd9Sstevel@tonic-gate flag[c] = 1; 115761961e0fSrobinson if (++i == argc) 11587c478bd9Sstevel@tonic-gate return (0); 11597c478bd9Sstevel@tonic-gate if (c == 'o') { 116061961e0fSrobinson if (cmd->outfile) 11617c478bd9Sstevel@tonic-gate return (0); 11627c478bd9Sstevel@tonic-gate cmd->outfile = argv[i]; 11637c478bd9Sstevel@tonic-gate } 11647c478bd9Sstevel@tonic-gate goto nextarg; 11657c478bd9Sstevel@tonic-gate case 'D': 116661961e0fSrobinson if (argv[i][j - 1] != '-') 11677c478bd9Sstevel@tonic-gate return (0); 11687c478bd9Sstevel@tonic-gate (void) addarg(argv[i]); 11697c478bd9Sstevel@tonic-gate goto nextarg; 11707c478bd9Sstevel@tonic-gate case 'v': 11717c478bd9Sstevel@tonic-gate version_info(); 11727c478bd9Sstevel@tonic-gate return (0); 11737c478bd9Sstevel@tonic-gate case 'Y': 117461961e0fSrobinson if (++i == argc) 11757c478bd9Sstevel@tonic-gate return (0); 11767c478bd9Sstevel@tonic-gate (void) strcpy(pathbuf, argv[i]); 11777c478bd9Sstevel@tonic-gate (void) strcat(pathbuf, "/cpp"); 11787c478bd9Sstevel@tonic-gate CPP = pathbuf; 11797c478bd9Sstevel@tonic-gate cppDefined = 1; 11807c478bd9Sstevel@tonic-gate goto nextarg; 11817c478bd9Sstevel@tonic-gate case 'r': 11827c478bd9Sstevel@tonic-gate rflag = !rflag; 11837c478bd9Sstevel@tonic-gate break; 11847c478bd9Sstevel@tonic-gate default: 11857c478bd9Sstevel@tonic-gate return (0); 11867c478bd9Sstevel@tonic-gate } 11877c478bd9Sstevel@tonic-gate } 11887c478bd9Sstevel@tonic-gate nextarg: 11897c478bd9Sstevel@tonic-gate ; 11907c478bd9Sstevel@tonic-gate } 11917c478bd9Sstevel@tonic-gate } 11927c478bd9Sstevel@tonic-gate 11937c478bd9Sstevel@tonic-gate cmd->cflag = flag['c']; 11947c478bd9Sstevel@tonic-gate cmd->hflag = flag['h']; 11957c478bd9Sstevel@tonic-gate cmd->lflag = flag['l']; 11967c478bd9Sstevel@tonic-gate cmd->mflag = flag['m']; 11977c478bd9Sstevel@tonic-gate cmd->nflag = flag['n']; 11987c478bd9Sstevel@tonic-gate cmd->sflag = flag['s']; 11997c478bd9Sstevel@tonic-gate cmd->tflag = flag['t']; 12007c478bd9Sstevel@tonic-gate cmd->Ssflag = flag['S']; 12017c478bd9Sstevel@tonic-gate cmd->Scflag = flag['C']; 12027c478bd9Sstevel@tonic-gate cmd->makefileflag = flag['M']; 12037c478bd9Sstevel@tonic-gate 12047c478bd9Sstevel@tonic-gate if (tirpcflag) { 12057c478bd9Sstevel@tonic-gate if (inetdflag) { 12067c478bd9Sstevel@tonic-gate f_print(stderr, 12077c478bd9Sstevel@tonic-gate "Cannot use -I flag without -b flag.\n"); 12087c478bd9Sstevel@tonic-gate return (0); 12097c478bd9Sstevel@tonic-gate } 12107c478bd9Sstevel@tonic-gate pmflag = 1; 12117c478bd9Sstevel@tonic-gate } else { /* 4.1 mode */ 12127c478bd9Sstevel@tonic-gate pmflag = 0; /* set pmflag only in tirpcmode */ 12137c478bd9Sstevel@tonic-gate inetdflag = 1; /* inetdflag is TRUE by default */ 12147c478bd9Sstevel@tonic-gate if (cmd->nflag) { /* netid needs TIRPC */ 121561961e0fSrobinson f_print(stderr, 121661961e0fSrobinson "Cannot use netid flag without TIRPC.\n"); 12177c478bd9Sstevel@tonic-gate return (0); 12187c478bd9Sstevel@tonic-gate } 12197c478bd9Sstevel@tonic-gate } 12207c478bd9Sstevel@tonic-gate 12217c478bd9Sstevel@tonic-gate if (newstyle && (tblflag || cmd->tflag)) { 12227c478bd9Sstevel@tonic-gate f_print(stderr, "Cannot use table flags with newstyle.\n"); 12237c478bd9Sstevel@tonic-gate return (0); 12247c478bd9Sstevel@tonic-gate } 12257c478bd9Sstevel@tonic-gate 12267c478bd9Sstevel@tonic-gate /* check no conflicts with file generation flags */ 12277c478bd9Sstevel@tonic-gate nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag + 12287c478bd9Sstevel@tonic-gate cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag + 12297c478bd9Sstevel@tonic-gate cmd->Scflag + cmd->makefileflag; 12307c478bd9Sstevel@tonic-gate 12317c478bd9Sstevel@tonic-gate if (nflags == 0) { 123261961e0fSrobinson if (cmd->outfile != NULL || cmd->infile == NULL) 12337c478bd9Sstevel@tonic-gate return (0); 12347c478bd9Sstevel@tonic-gate } else if (cmd->infile == NULL && 12357c478bd9Sstevel@tonic-gate (cmd->Ssflag || cmd->Scflag || cmd->makefileflag)) { 12367c478bd9Sstevel@tonic-gate f_print(stderr, "\"infile\" is required for template" 12377c478bd9Sstevel@tonic-gate " generation flags.\n"); 12387c478bd9Sstevel@tonic-gate return (0); 123961961e0fSrobinson } 124061961e0fSrobinson if (nflags > 1) { 12417c478bd9Sstevel@tonic-gate f_print(stderr, 12427c478bd9Sstevel@tonic-gate "Cannot have more than one file generation flag.\n"); 12437c478bd9Sstevel@tonic-gate return (0); 12447c478bd9Sstevel@tonic-gate } 12457c478bd9Sstevel@tonic-gate return (1); 12467c478bd9Sstevel@tonic-gate } 12477c478bd9Sstevel@tonic-gate 124861961e0fSrobinson static void 124961961e0fSrobinson usage(void) 12507c478bd9Sstevel@tonic-gate { 12517c478bd9Sstevel@tonic-gate f_print(stderr, "%s (%d.%d)\n", cmdname, RPCGEN_MAJOR, RPCGEN_MINOR); 12527c478bd9Sstevel@tonic-gate f_print(stderr, "usage: %s infile\n", cmdname); 12537c478bd9Sstevel@tonic-gate f_print(stderr, "\t%s [-abCLNTMA] [-Dname[=value]] [-i size]" 12547c478bd9Sstevel@tonic-gate " [-I [-K seconds]] [-Y path] infile\n", cmdname); 12557c478bd9Sstevel@tonic-gate f_print(stderr, "\t%s [-c | -h | -l | -m | -t | -Sc | -Ss | -Sm]" 12567c478bd9Sstevel@tonic-gate " [-o outfile] [infile]\n", cmdname); 12577c478bd9Sstevel@tonic-gate f_print(stderr, "\t%s [-s nettype]* [-o outfile] [infile]\n", cmdname); 12587c478bd9Sstevel@tonic-gate f_print(stderr, "\t%s [-n netid]* [-o outfile] [infile]\n", cmdname); 12597c478bd9Sstevel@tonic-gate options_usage(); 12607c478bd9Sstevel@tonic-gate exit(1); 12617c478bd9Sstevel@tonic-gate } 12627c478bd9Sstevel@tonic-gate 126361961e0fSrobinson static void 126461961e0fSrobinson version_info(void) 12657c478bd9Sstevel@tonic-gate { 12667c478bd9Sstevel@tonic-gate f_print(stderr, "%s %d.%d\n", cmdname, RPCGEN_MAJOR, RPCGEN_MINOR); 12677c478bd9Sstevel@tonic-gate exit(1); 12687c478bd9Sstevel@tonic-gate } 12697c478bd9Sstevel@tonic-gate 127061961e0fSrobinson static void 127161961e0fSrobinson options_usage(void) 12727c478bd9Sstevel@tonic-gate { 1273a2f144d1SJordan Brown /* BEGIN CSTYLED */ 12747c478bd9Sstevel@tonic-gate f_print(stderr, "options:\n"); 12757c478bd9Sstevel@tonic-gate f_print(stderr, "-a\t\tgenerate all files, including samples\n"); 12767c478bd9Sstevel@tonic-gate f_print(stderr, "-A\t\tgenerate code to enable automatic MT mode\n"); 12777c478bd9Sstevel@tonic-gate f_print(stderr, "-b\t\tbackward compatibility mode (generates code" 12787c478bd9Sstevel@tonic-gate " for SunOS 4.X)\n"); 12797c478bd9Sstevel@tonic-gate f_print(stderr, "-c\t\tgenerate XDR routines\n"); 12807c478bd9Sstevel@tonic-gate f_print(stderr, "-C\t\tANSI C mode\n"); 12817c478bd9Sstevel@tonic-gate f_print(stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n"); 12827c478bd9Sstevel@tonic-gate f_print(stderr, "-h\t\tgenerate header file\n"); 12837c478bd9Sstevel@tonic-gate f_print(stderr, "-i size\t\tsize at which to start generating" 12847c478bd9Sstevel@tonic-gate " inline code\n"); 12857c478bd9Sstevel@tonic-gate f_print(stderr, "-I\t\tgenerate code for inetd support in server" 12867c478bd9Sstevel@tonic-gate " (for SunOS 4.X)\n"); 12877c478bd9Sstevel@tonic-gate f_print(stderr, "-K seconds\tserver exits after K seconds of" 12887c478bd9Sstevel@tonic-gate " inactivity\n"); 12897c478bd9Sstevel@tonic-gate f_print(stderr, "-l\t\tgenerate client side stubs\n"); 12907c478bd9Sstevel@tonic-gate f_print(stderr, "-L\t\tserver errors will be printed to syslog\n"); 12917c478bd9Sstevel@tonic-gate f_print(stderr, "-m\t\tgenerate server side stubs\n"); 12927c478bd9Sstevel@tonic-gate f_print(stderr, "-M\t\tgenerate MT-safe code\n"); 12937c478bd9Sstevel@tonic-gate f_print(stderr, "-n netid\tgenerate server code that supports" 12947c478bd9Sstevel@tonic-gate " named netid\n"); 12957c478bd9Sstevel@tonic-gate f_print(stderr, "-N\t\tsupports multiple arguments and" 12967c478bd9Sstevel@tonic-gate " call-by-value\n"); 12977c478bd9Sstevel@tonic-gate f_print(stderr, "-o outfile\tname of the output file\n"); 12987c478bd9Sstevel@tonic-gate f_print(stderr, "-s nettype\tgenerate server code that supports named" 12997c478bd9Sstevel@tonic-gate " nettype\n"); 13007c478bd9Sstevel@tonic-gate f_print(stderr, "-Sc\t\tgenerate sample client code that uses remote" 13017c478bd9Sstevel@tonic-gate " procedures\n"); 13027c478bd9Sstevel@tonic-gate f_print(stderr, "-Ss\t\tgenerate sample server code that defines" 13037c478bd9Sstevel@tonic-gate " remote procedures\n"); 13047c478bd9Sstevel@tonic-gate f_print(stderr, "-Sm \t\tgenerate makefile template \n"); 13057c478bd9Sstevel@tonic-gate 13067c478bd9Sstevel@tonic-gate f_print(stderr, "-t\t\tgenerate RPC dispatch table\n"); 13077c478bd9Sstevel@tonic-gate f_print(stderr, "-T\t\tgenerate code to support RPC dispatch tables\n"); 13087c478bd9Sstevel@tonic-gate f_print(stderr, "-v\t\tprint version information and exit\n"); 13097c478bd9Sstevel@tonic-gate f_print(stderr, "-Y path\t\tpath where cpp is found\n"); 1310a2f144d1SJordan Brown /* END CSTYLED */ 13117c478bd9Sstevel@tonic-gate exit(1); 13127c478bd9Sstevel@tonic-gate } 1313