dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / rpcgen / rpc_main.c
blob0d3ae6fd8218f5d647d0dd2b6018cd1adaae4e3e
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
29 * University Copyright- Copyright (c) 1982, 1986, 1988
30 * The Regents of the University of California
31 * All Rights Reserved
33 * University Acknowledgment- Portions of this document are derived from
34 * software developed by the University of California, Berkeley, and its
35 * contributors.
39 * rpc_main.c, Top level of the RPC protocol compiler.
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <strings.h>
46 #include <unistd.h>
47 #include <ctype.h>
48 #include <sys/types.h>
49 #include <sys/param.h>
50 #include <sys/file.h>
51 #include <sys/stat.h>
52 #include "rpc_parse.h"
53 #include "rpc_util.h"
54 #include "rpc_scan.h"
57 extern void write_sample_svc(definition *);
58 extern int write_sample_clnt(definition *);
59 extern void write_sample_clnt_main(void);
60 extern void reinitialize(void);
61 extern void crash(void);
62 extern void add_type(int, char *);
63 extern void add_sample_msg(void);
65 static void svc_output(char *, char *, int, char *);
66 static void clnt_output(char *, char *, int, char *);
67 static void c_output(char *, char *, int, char *);
68 static void mkfile_output(struct commandline *);
69 static void c_initialize(void);
70 static void h_output(char *, char *, int, char *);
71 static void s_output(int, char *[], char *, char *, int, char *, int, int);
72 static void l_output(char *, char *, int, char *);
73 static void t_output(char *, char *, int, char *);
74 static int do_registers(int, char *[]);
75 static uint_t parseargs(int, char *[], struct commandline *);
76 static void usage(void);
77 static void version_info(void);
78 static void options_usage(void);
80 #define EXTEND 1 /* alias for TRUE */
81 #define DONT_EXTEND 0 /* alias for FALSE */
83 #define SUNOS_CPP "/opt/gcc/4.4.4/bin/cpp"
84 static int cppDefined = 0; /* explicit path for C preprocessor */
87 static char *cmdname;
89 static char *svcclosetime = "120";
90 static char *CPP = SUNOS_CPP;
91 static char CPPFLAGS[] = "-C";
92 static char pathbuf[MAXPATHLEN + 1];
93 static char *allv[] = {
94 "rpcgen", "-s", "udp", "-s", "tcp",
96 static int allc = sizeof (allv)/sizeof (allv[0]);
97 static char *allnv[] = {
98 "rpcgen", "-s", "netpath",
100 static int allnc = sizeof (allnv)/sizeof (allnv[0]);
103 * machinations for handling expanding argument list
105 static void addarg(char *); /* add another argument to the list */
106 static void putarg(int, char *); /* put argument at specified location */
107 static void clear_args(void); /* clear argument list */
108 static void checkfiles(char *, char *); /* check if out file already exists */
111 #define ARGLISTLEN 20
112 #define FIXEDARGS 2
114 static char *arglist[ARGLISTLEN];
115 static int argcount = FIXEDARGS;
118 int nonfatalerrors; /* errors */
119 int inetdflag; /* Support for inetd is now the default */
120 int pmflag; /* Support for port monitors */
121 int logflag; /* Use syslog instead of fprintf for errors */
122 int tblflag; /* Support for dispatch table file */
123 int mtflag = 0; /* Support for MT */
124 int mtauto = 0; /* Enable automatic mode */
125 int rflag = 1; /* Eliminate tail recursion from structures */
126 #define INLINE 5
127 /* length at which to start doing an inline */
129 int inlinelen = INLINE;
131 * Length at which to start doing an inline. INLINE = default
132 * if 0, no xdr_inline code
135 int indefinitewait; /* If started by port monitors, hang till it wants */
136 int exitnow; /* If started by port monitors, exit after the call */
137 int timerflag; /* TRUE if !indefinite && !exitnow */
138 int newstyle; /* newstyle of passing arguments (by value) */
139 int Cflag = 0; /* ANSI C syntax */
140 int CCflag = 0; /* C++ files */
141 static int allfiles; /* generate all files */
142 int tirpcflag = 1; /* generating code for tirpc, by default */
143 xdrfunc *xdrfunc_head = NULL; /* xdr function list */
144 xdrfunc *xdrfunc_tail = NULL; /* xdr function list */
145 pid_t childpid;
149 main(int argc, char *argv[])
151 struct commandline cmd;
153 (void) memset(&cmd, 0, sizeof (struct commandline));
154 clear_args();
155 if (!parseargs(argc, argv, &cmd))
156 usage();
158 * Only the client and server side stubs are likely to be customized,
159 * so in that case only, check if the outfile exists, and if so,
160 * print an error message and exit.
162 if (cmd.Ssflag || cmd.Scflag || cmd.makefileflag)
163 checkfiles(cmd.infile, cmd.outfile);
164 else
165 checkfiles(cmd.infile, NULL);
167 if (cmd.cflag) {
168 c_output(cmd.infile, "-DRPC_XDR", DONT_EXTEND, cmd.outfile);
169 } else if (cmd.hflag) {
170 h_output(cmd.infile, "-DRPC_HDR", DONT_EXTEND, cmd.outfile);
171 } else if (cmd.lflag) {
172 l_output(cmd.infile, "-DRPC_CLNT", DONT_EXTEND, cmd.outfile);
173 } else if (cmd.sflag || cmd.mflag || (cmd.nflag)) {
174 s_output(argc, argv, cmd.infile, "-DRPC_SVC", DONT_EXTEND,
175 cmd.outfile, cmd.mflag, cmd.nflag);
176 } else if (cmd.tflag) {
177 t_output(cmd.infile, "-DRPC_TBL", DONT_EXTEND, cmd.outfile);
178 } else if (cmd.Ssflag) {
179 svc_output(cmd.infile, "-DRPC_SERVER", DONT_EXTEND,
180 cmd.outfile);
181 } else if (cmd.Scflag) {
182 clnt_output(cmd.infile, "-DRPC_CLIENT", DONT_EXTEND,
183 cmd.outfile);
184 } else if (cmd.makefileflag) {
185 mkfile_output(&cmd);
186 } else {
187 /* the rescans are required, since cpp may effect input */
188 c_output(cmd.infile, "-DRPC_XDR", EXTEND, "_xdr.c");
189 reinitialize();
190 h_output(cmd.infile, "-DRPC_HDR", EXTEND, ".h");
191 reinitialize();
192 l_output(cmd.infile, "-DRPC_CLNT", EXTEND, "_clnt.c");
193 reinitialize();
194 if (inetdflag || !tirpcflag)
195 s_output(allc, allv, cmd.infile, "-DRPC_SVC", EXTEND,
196 "_svc.c", cmd.mflag, cmd.nflag);
197 else
198 s_output(allnc, allnv, cmd.infile, "-DRPC_SVC",
199 EXTEND, "_svc.c", cmd.mflag, cmd.nflag);
200 if (tblflag) {
201 reinitialize();
202 t_output(cmd.infile, "-DRPC_TBL", EXTEND, "_tbl.i");
205 if (allfiles) {
206 reinitialize();
207 svc_output(cmd.infile, "-DRPC_SERVER", EXTEND,
208 "_server.c");
209 reinitialize();
210 clnt_output(cmd.infile, "-DRPC_CLIENT", EXTEND,
211 "_client.c");
214 if (allfiles || (cmd.makefileflag == 1)) {
215 reinitialize();
216 mkfile_output(&cmd);
220 return (nonfatalerrors);
225 * add extension to filename
227 static char *
228 extendfile(char *file, char *ext)
230 char *res;
231 char *p;
233 res = malloc(strlen(file) + strlen(ext) + 1);
234 if (res == NULL)
235 abort();
236 p = strrchr(file, '.');
237 if (p == NULL)
238 p = file + strlen(file);
239 (void) strcpy(res, file);
240 (void) strcpy(res + (p - file), ext);
241 return (res);
245 * Open output file with given extension
247 static void
248 open_output(char *infile, char *outfile)
251 if (outfile == NULL) {
252 fout = stdout;
253 return;
256 if (infile != NULL && streq(outfile, infile)) {
257 f_print(stderr,
258 "%s: %s already exists. No output generated.\n",
259 cmdname, infile);
260 crash();
262 fout = fopen(outfile, "w");
263 if (fout == NULL) {
264 f_print(stderr, "%s: unable to open ", cmdname);
265 perror(outfile);
266 crash();
268 record_open(outfile);
272 static void
273 add_warning(void)
275 f_print(fout, "/*\n");
276 f_print(fout, " * Please do not edit this file.\n");
277 f_print(fout, " * It was generated using rpcgen.\n");
278 f_print(fout, " */\n\n");
281 /* clear list of arguments */
282 static void
283 clear_args(void)
285 int i;
287 for (i = FIXEDARGS; i < ARGLISTLEN; i++)
288 arglist[i] = NULL;
289 argcount = FIXEDARGS;
292 /* make sure that a CPP exists */
293 static void
294 find_cpp(void)
296 struct stat buf;
298 if (stat(CPP, &buf) < 0) { /* SVR4 or explicit cpp does not exist */
299 if (cppDefined) {
300 (void) fprintf(stderr,
301 "cannot find C preprocessor: %s \n", CPP);
302 crash();
303 } else { /* try the other one */
304 CPP = SUNOS_CPP;
305 if (stat(CPP, &buf) < 0) { /* can't find any cpp */
306 (void) fprintf(stderr,
307 "cannot find any C preprocessor (cpp)\n");
308 crash();
315 * Open input file with given define for C-preprocessor
317 static void
318 open_input(char *infile, char *define)
320 int pd[2];
322 infilename = (infile == NULL) ? "<stdin>" : infile;
323 (void) pipe(pd);
324 switch (childpid = fork()) {
325 case 0:
326 find_cpp();
327 putarg(0, CPP);
328 putarg(1, CPPFLAGS);
329 addarg(define);
330 if (infile)
331 addarg(infile);
332 addarg(NULL);
333 (void) close(1);
334 (void) dup2(pd[1], 1);
335 (void) close(pd[0]);
336 (void) execv(arglist[0], arglist);
337 perror("execv");
338 exit(1);
339 /* NOTREACHED */
340 case -1:
341 perror("fork");
342 exit(1);
344 (void) close(pd[1]);
345 fin = fdopen(pd[0], "r");
346 if (fin == NULL) {
347 f_print(stderr, "%s: ", cmdname);
348 perror(infilename);
349 crash();
353 /* valid tirpc nettypes */
354 static char *valid_ti_nettypes[] = {
355 "netpath",
356 "visible",
357 "circuit_v",
358 "datagram_v",
359 "circuit_n",
360 "datagram_n",
361 "udp",
362 "tcp",
363 "raw",
364 NULL
367 /* valid inetd nettypes */
368 static char *valid_i_nettypes[] = {
369 "udp",
370 "tcp",
371 NULL
374 static int
375 check_nettype(char *name, char *list_to_check[])
377 int i;
378 for (i = 0; list_to_check[i] != NULL; i++) {
379 if (strcmp(name, list_to_check[i]) == 0) {
380 return (1);
383 f_print(stderr, "illegal nettype :\'%s\'\n", name);
384 return (0);
387 static char *
388 file_name(char *file, char *ext)
390 char *temp;
391 temp = extendfile(file, ext);
393 if (access(temp, F_OK) != -1)
394 return (temp);
395 else
396 return ((char *)" ");
400 static void
401 c_output(char *infile, char *define, int extend, char *outfile)
403 definition *def;
404 char *include;
405 char *outfilename;
406 long tell;
408 c_initialize();
409 open_input(infile, define);
410 outfilename = extend ? extendfile(infile, outfile) : outfile;
411 open_output(infile, outfilename);
412 add_warning();
413 if (infile && (include = extendfile(infile, ".h"))) {
414 f_print(fout, "#include \"%s\"\n", include);
415 free(include);
416 /* .h file already contains rpc/rpc.h */
417 } else
418 f_print(fout, "#include <rpc/rpc.h>\n");
420 * Include stdlib.h to support mem_alloc calls.
422 f_print(fout, "\n#ifndef _KERNEL\n");
423 f_print(fout, "#include <stdlib.h>\n");
424 f_print(fout, "#endif /* !_KERNEL */\n\n");
425 tell = ftell(fout);
426 while (def = get_definition()) {
427 emit(def);
429 if (extend && tell == ftell(fout)) {
430 (void) unlink(outfilename);
435 static void
436 c_initialize(void)
439 * add all the starting basic types.
440 * We may need to add some derived types
441 * if we need to generate INLINE macros.
442 * These types are defined in rpc/types.h
444 add_type(1, "int");
445 add_type(1, "long");
446 add_type(1, "short");
447 add_type(1, "bool");
448 add_type(1, "u_int");
449 add_type(1, "u_long");
450 add_type(1, "u_short");
451 add_type(1, "rpcprog_t");
452 add_type(1, "rpcvers_t");
453 add_type(1, "rpcproc_t");
454 add_type(1, "rpcprot_t");
455 add_type(1, "rpcport_t");
458 char rpcgen_table_dcl1[] = "struct rpcgen_table {\n";
460 char rpcgen_table_dcl2[] = "\txdrproc_t\txdr_arg;\n"
461 "\tunsigned\tlen_arg;\n"
462 "\txdrproc_t\txdr_res;\n"
463 "\tunsigned\tlen_res;\n"
464 "};\n";
466 char rpcgen_table_proc[] = "\tvoid\t*(*proc)();\n";
468 char rpcgen_table_proc_b[] = "\tchar\t*(*proc)();\n";
471 char *
472 generate_guard(char *pathname)
474 char *filename, *guard, *tmp;
476 filename = strrchr(pathname, '/'); /* find last component */
477 filename = ((filename == 0) ? pathname : filename+1);
478 guard = extendfile(filename, "_H_RPCGEN");
481 * Guard must be an ANSI C identifier composed of
482 * upper case letters, digits, or '_'.
483 * Convert invalid characters to '_'.
485 for (tmp = guard; *tmp; tmp++) {
486 if (!isalpha(*tmp) && !isdigit(*tmp)) {
487 *tmp = '_';
488 continue;
490 if (islower(*tmp))
491 *tmp = toupper(*tmp);
495 * The first character must be a letter; the underscore '_'
496 * counts as a letter.
498 if (!isalpha(guard[0]))
499 guard[0] = '_';
501 return (guard);
505 * Compile into an XDR header file
509 static void
510 h_output(char *infile, char *define, int extend, char *outfile)
512 definition *def;
513 char *outfilename;
514 long tell;
515 char *guard;
516 list *l;
517 xdrfunc *xdrfuncp;
518 int i;
520 open_input(infile, define);
521 outfilename = extend ? extendfile(infile, outfile) : outfile;
522 open_output(infile, outfilename);
523 add_warning();
524 if (outfilename || infile)
525 guard = generate_guard(outfilename ? outfilename: infile);
526 else
527 guard = "STDIN_";
529 f_print(fout, "#ifndef _%s\n#define _%s\n\n", guard, guard);
531 f_print(fout, "#include <rpc/rpc.h>\n");
533 if (mtflag) {
534 f_print(fout, "#ifndef _KERNEL\n");
535 f_print(fout, "#include <synch.h>\n");
536 f_print(fout, "#include <thread.h>\n");
537 f_print(fout, "#endif /* !_KERNEL */\n");
540 /* put the C++ support */
541 if (Cflag && !CCflag) {
542 f_print(fout, "\n#ifdef __cplusplus\n");
543 f_print(fout, "extern \"C\" {\n");
544 f_print(fout, "#endif\n\n");
547 /* put in a typedef for quadprecision. Only with Cflag */
550 * declaration of struct rpcgen_table must go before
551 * the definition of arrays like *_1_table[]
553 if (tblflag) {
554 f_print(fout, rpcgen_table_dcl1);
555 if (tirpcflag)
556 f_print(fout, rpcgen_table_proc);
557 else
558 f_print(fout, rpcgen_table_proc_b);
559 f_print(fout, rpcgen_table_dcl2);
562 tell = ftell(fout);
564 /* print data definitions */
565 while (def = get_definition())
566 print_datadef(def);
569 * print function declarations.
570 * Do this after data definitions because they might be used as
571 * arguments for functions
573 for (l = defined; l != NULL; l = l->next)
574 print_funcdef(l->val);
575 /* Now print all xdr func declarations */
576 if (xdrfunc_head != NULL) {
577 f_print(fout, "\n/* the xdr functions */\n");
579 if (CCflag) {
580 f_print(fout, "\n#ifdef __cplusplus\n");
581 f_print(fout, "extern \"C\" {\n");
582 f_print(fout, "#endif\n");
585 if (!Cflag) {
586 xdrfuncp = xdrfunc_head;
587 while (xdrfuncp != NULL) {
588 print_xdr_func_def(xdrfuncp->name,
589 xdrfuncp->pointerp, 2);
590 xdrfuncp = xdrfuncp->next;
592 } else {
593 for (i = 1; i < 3; i++) {
594 if (i == 1)
595 f_print(fout,
596 "\n#if defined(__STDC__) || defined(__cplusplus)\n");
597 else
598 f_print(fout, "\n#else /* K&R C */\n");
600 xdrfuncp = xdrfunc_head;
601 while (xdrfuncp != NULL) {
602 print_xdr_func_def(xdrfuncp->name,
603 xdrfuncp->pointerp, i);
604 xdrfuncp = xdrfuncp->next;
607 f_print(fout, "\n#endif /* K&R C */\n");
611 if (extend && tell == ftell(fout)) {
612 (void) unlink(outfilename);
615 if (Cflag) {
616 f_print(fout, "\n#ifdef __cplusplus\n");
617 f_print(fout, "}\n");
618 f_print(fout, "#endif\n");
621 f_print(fout, "\n#endif /* !_%s */\n", guard);
625 * Compile into an RPC service
627 static void
628 s_output(int argc, char *argv[], char *infile, char *define, int extend,
629 char *outfile, int nomain, int netflag)
631 char *include;
632 definition *def;
633 int foundprogram = 0;
634 char *outfilename;
636 open_input(infile, define);
637 outfilename = extend ? extendfile(infile, outfile) : outfile;
638 open_output(infile, outfilename);
639 add_warning();
640 if (infile && (include = extendfile(infile, ".h"))) {
641 f_print(fout, "#include \"%s\"\n", include);
642 free(include);
643 } else
644 f_print(fout, "#include <rpc/rpc.h>\n");
646 f_print(fout, "#include <stdio.h>\n");
647 f_print(fout, "#include <stdlib.h> /* getenv, exit */\n");
648 f_print(fout, "#include <signal.h>\n");
650 if (Cflag) {
651 f_print(fout,
652 "#include <rpc/pmap_clnt.h> /* for pmap_unset */\n");
653 f_print(fout, "#include <string.h> /* strcmp */\n");
655 if (strcmp(svcclosetime, "-1") == 0)
656 indefinitewait = 1;
657 else if (strcmp(svcclosetime, "0") == 0)
658 exitnow = 1;
659 else if (inetdflag || pmflag)
660 timerflag = 1;
662 if (!tirpcflag && inetdflag)
663 f_print(fout, "#include <sys/termios.h> /* TIOCNOTTY */\n");
664 if (Cflag && (inetdflag || pmflag))
665 if (tirpcflag)
666 f_print(fout, "#include <unistd.h> /* setsid */\n");
667 if (tirpcflag)
668 f_print(fout, "#include <sys/types.h>\n");
670 f_print(fout, "#include <memory.h>\n");
671 f_print(fout, "#include <stropts.h>\n");
672 if (inetdflag || !tirpcflag) {
673 f_print(fout, "#include <sys/socket.h>\n");
674 f_print(fout, "#include <netinet/in.h>\n");
675 f_print(fout, "#include <rpc/svc_soc.h>\n");
678 if ((netflag || pmflag) && tirpcflag && !nomain)
679 f_print(fout, "#include <netconfig.h>\n");
680 if (tirpcflag)
681 f_print(fout, "#include <sys/resource.h> /* rlimit */\n");
682 if (logflag || inetdflag || pmflag)
683 f_print(fout, "#include <syslog.h>\n");
685 /* for ANSI-C */
686 if (Cflag)
687 f_print(fout,
688 "\n#ifndef SIG_PF\n#define SIG_PF void(*)\
689 (int)\n#endif\n");
691 f_print(fout, "\n#ifdef DEBUG\n#define RPC_SVC_FG\n#endif\n");
692 if (timerflag)
693 f_print(fout, "\n#define _RPCSVC_CLOSEDOWN %s\n",
694 svcclosetime);
695 while (def = get_definition())
696 foundprogram |= (def->def_kind == DEF_PROGRAM);
697 if (extend && !foundprogram) {
698 (void) unlink(outfilename);
699 return;
701 write_most(infile, netflag, nomain);
702 if (!nomain) {
703 if (!do_registers(argc, argv)) {
704 if (outfilename)
705 (void) unlink(outfilename);
706 usage();
708 write_rest();
713 * generate client side stubs
715 static void
716 l_output(char *infile, char *define, int extend, char *outfile)
718 char *include;
719 definition *def;
720 int foundprogram = 0;
721 char *outfilename;
723 open_input(infile, define);
724 outfilename = extend ? extendfile(infile, outfile) : outfile;
725 open_output(infile, outfilename);
726 add_warning();
727 if (Cflag)
728 f_print(fout, "#include <memory.h> /* for memset */\n");
729 if (infile && (include = extendfile(infile, ".h"))) {
730 f_print(fout, "#include \"%s\"\n", include);
731 free(include);
732 } else
733 f_print(fout, "#include <rpc/rpc.h>\n");
735 f_print(fout, "#ifndef _KERNEL\n");
736 f_print(fout, "#include <stdio.h>\n");
737 f_print(fout, "#include <stdlib.h> /* getenv, exit */\n");
738 f_print(fout, "#endif /* !_KERNEL */\n");
740 while (def = get_definition())
741 foundprogram |= (def->def_kind == DEF_PROGRAM);
742 if (extend && !foundprogram) {
743 (void) unlink(outfilename);
744 return;
746 write_stubs();
750 * generate the dispatch table
752 static void
753 t_output(char *infile, char *define, int extend, char *outfile)
755 definition *def;
756 int foundprogram = 0;
757 char *outfilename;
759 open_input(infile, define);
760 outfilename = extend ? extendfile(infile, outfile) : outfile;
761 open_output(infile, outfilename);
762 add_warning();
763 while (def = get_definition()) {
764 foundprogram |= (def->def_kind == DEF_PROGRAM);
766 if (extend && !foundprogram) {
767 (void) unlink(outfilename);
768 return;
770 write_tables();
773 /* sample routine for the server template */
774 static void
775 svc_output(char *infile, char *define, int extend, char *outfile)
777 definition *def;
778 char *include;
779 char *outfilename;
780 long tell;
781 open_input(infile, define);
782 outfilename = extend ? extendfile(infile, outfile) : outfile;
783 checkfiles(infile, outfilename);
785 * Check if outfile already exists.
786 * if so, print an error message and exit
788 open_output(infile, outfilename);
789 add_sample_msg();
791 if (infile && (include = extendfile(infile, ".h"))) {
792 f_print(fout, "#include \"%s\"\n", include);
793 free(include);
794 } else {
795 f_print(fout, "#include <rpc/rpc.h>\n");
798 f_print(fout, "#include <stdio.h>\n");
799 f_print(fout, "#include <stdlib.h> /* getenv, exit */\n");
800 f_print(fout, "#include <signal.h>\n");
802 tell = ftell(fout);
803 while (def = get_definition())
804 write_sample_svc(def);
805 if (extend && tell == ftell(fout))
806 (void) unlink(outfilename);
809 /* sample main routine for client */
810 static void
811 clnt_output(char *infile, char *define, int extend, char *outfile)
813 definition *def;
814 char *include;
815 char *outfilename;
816 long tell;
817 int has_program = 0;
819 open_input(infile, define);
820 outfilename = extend ? extendfile(infile, outfile) : outfile;
821 checkfiles(infile, outfilename);
823 * Check if outfile already exists.
824 * if so, print an error message and exit
827 open_output(infile, outfilename);
828 add_sample_msg();
829 if (infile && (include = extendfile(infile, ".h"))) {
830 f_print(fout, "#include \"%s\"\n", include);
831 free(include);
832 } else
833 f_print(fout, "#include <rpc/rpc.h>\n");
835 f_print(fout, "#include <stdio.h>\n");
836 f_print(fout, "#include <stdlib.h> /* getenv, exit */\n");
838 tell = ftell(fout);
839 while (def = get_definition())
840 has_program += write_sample_clnt(def);
842 if (has_program)
843 write_sample_clnt_main();
845 if (extend && tell == ftell(fout))
846 (void) unlink(outfilename);
850 static void
851 mkfile_output(struct commandline *cmd)
853 char *mkfilename, *clientname, *clntname, *xdrname, *hdrname;
854 char *servername, *svcname, *servprogname, *clntprogname;
855 char *temp;
857 svcname = file_name(cmd->infile, "_svc.c");
858 clntname = file_name(cmd->infile, "_clnt.c");
859 xdrname = file_name(cmd->infile, "_xdr.c");
860 hdrname = file_name(cmd->infile, ".h");
863 if (allfiles) {
864 servername = extendfile(cmd->infile, "_server.c");
865 clientname = extendfile(cmd->infile, "_client.c");
866 } else {
867 servername = " ";
868 clientname = " ";
870 servprogname = extendfile(cmd->infile, "_server");
871 clntprogname = extendfile(cmd->infile, "_client");
873 if (allfiles) {
874 mkfilename = malloc(strlen("makefile.") +
875 strlen(cmd->infile) + 1);
876 if (mkfilename == NULL) {
877 f_print(stderr, "Out of memory!\n");
878 return;
880 temp = (char *)rindex(cmd->infile, '.');
881 (void) strcpy(mkfilename, "makefile.");
882 (void) strncat(mkfilename, cmd->infile,
883 (temp - cmd->infile));
884 } else
885 mkfilename = cmd->outfile;
888 checkfiles(NULL, mkfilename);
889 open_output(NULL, mkfilename);
891 f_print(fout, "\n# This is a template makefile generated\
892 by rpcgen \n");
894 f_print(fout, "\n# Parameters \n\n");
896 f_print(fout, "CLIENT = %s\nSERVER = %s\n\n",
897 clntprogname, servprogname);
898 f_print(fout, "SOURCES_CLNT.c = \nSOURCES_CLNT.h = \n");
899 f_print(fout, "SOURCES_SVC.c = \nSOURCES_SVC.h = \n");
900 f_print(fout, "SOURCES.x = %s\n\n", cmd->infile);
901 f_print(fout, "TARGETS_SVC.c = %s %s %s \n",
902 svcname, servername, xdrname);
903 f_print(fout, "TARGETS_CLNT.c = %s %s %s \n",
904 clntname, clientname, xdrname);
905 f_print(fout, "TARGETS = %s %s %s %s %s %s\n\n",
906 hdrname, xdrname, clntname,
907 svcname, clientname, servername);
909 f_print(fout, "OBJECTS_CLNT = $(SOURCES_CLNT.c:%%.c=%%.o) "
910 "$(TARGETS_CLNT.c:%%.c=%%.o) ");
912 f_print(fout, "\nOBJECTS_SVC = $(SOURCES_SVC.c:%%.c=%%.o) "
913 "$(TARGETS_SVC.c:%%.c=%%.o) ");
916 f_print(fout, "\n# Compiler flags \n");
917 f_print(fout, "\nCFLAGS += -g \n");
918 f_print(fout, "RPCGENFLAGS = \n");
920 f_print(fout, "\n# Targets \n\n");
922 f_print(fout, "all : $(CLIENT) $(SERVER)\n\n");
923 f_print(fout, "$(TARGETS) : $(SOURCES.x) \n");
924 f_print(fout, "\trpcgen $(RPCGENFLAGS) $(SOURCES.x)\n\n");
925 f_print(fout, "$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) \
926 $(TARGETS_CLNT.c) \n\n");
928 f_print(fout, "$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) \
929 $(TARGETS_SVC.c) \n\n");
930 f_print(fout, "$(CLIENT) : $(OBJECTS_CLNT) \n");
931 f_print(fout, "\t$(LINK.c) -o $(CLIENT) $(OBJECTS_CLNT) \
932 $(LDLIBS) \n\n");
933 f_print(fout, "$(SERVER) : $(OBJECTS_SVC) \n");
934 f_print(fout, "\t$(LINK.c) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)\n\n ");
935 f_print(fout, "clean:\n\t $(RM) core $(TARGETS) $(OBJECTS_CLNT) \
936 $(OBJECTS_SVC) $(CLIENT) $(SERVER)\n\n");
941 * Perform registrations for service output
942 * Return 0 if failed; 1 otherwise.
944 static int
945 do_registers(int argc, char *argv[])
947 int i;
949 if (inetdflag || !tirpcflag) {
950 for (i = 1; i < argc; i++) {
951 if (streq(argv[i], "-s")) {
952 if (!check_nettype(argv[i + 1],
953 valid_i_nettypes))
954 return (0);
955 write_inetd_register(argv[i + 1]);
956 i++;
959 } else {
960 for (i = 1; i < argc; i++)
961 if (streq(argv[i], "-s")) {
962 if (!check_nettype(argv[i + 1],
963 valid_ti_nettypes))
964 return (0);
965 write_nettype_register(argv[i + 1]);
966 i++;
967 } else if (streq(argv[i], "-n")) {
968 write_netid_register(argv[i + 1]);
969 i++;
972 return (1);
976 * Add another argument to the arg list
978 static void
979 addarg(char *cp)
981 if (argcount >= ARGLISTLEN) {
982 f_print(stderr, "rpcgen: too many defines\n");
983 crash();
984 /*NOTREACHED*/
986 arglist[argcount++] = cp;
989 static void
990 putarg(int where, char *cp)
992 if (where >= ARGLISTLEN) {
993 f_print(stderr, "rpcgen: arglist coding error\n");
994 crash();
995 /*NOTREACHED*/
997 arglist[where] = cp;
1001 * if input file is stdin and an output file is specified then complain
1002 * if the file already exists. Otherwise the file may get overwritten
1003 * If input file does not exist, exit with an error
1005 static void
1006 checkfiles(char *infile, char *outfile)
1008 struct stat buf;
1010 if (infile) { /* infile ! = NULL */
1011 if (stat(infile, &buf) < 0) {
1012 perror(infile);
1013 crash();
1016 if (outfile) {
1017 if (stat(outfile, &buf) < 0)
1018 return; /* file does not exist */
1019 f_print(stderr,
1020 "file '%s' already exists and may be overwritten\n",
1021 outfile);
1022 crash();
1027 * Parse command line arguments
1029 static uint_t
1030 parseargs(int argc, char *argv[], struct commandline *cmd)
1032 int i;
1033 int j;
1034 char c, ch;
1035 char flag[(1 << 8 * sizeof (char))];
1036 int nflags;
1038 cmdname = argv[0];
1039 cmd->infile = cmd->outfile = NULL;
1040 if (argc < 2)
1041 return (0);
1042 allfiles = 0;
1043 flag['c'] = 0;
1044 flag['h'] = 0;
1045 flag['l'] = 0;
1046 flag['m'] = 0;
1047 flag['o'] = 0;
1048 flag['s'] = 0;
1049 flag['n'] = 0;
1050 flag['t'] = 0;
1051 flag['S'] = 0;
1052 flag['C'] = 0;
1053 flag['M'] = 0;
1055 for (i = 1; i < argc; i++) {
1056 if (argv[i][0] != '-') {
1057 if (cmd->infile) {
1058 f_print(stderr,
1059 "Cannot specify more than one input file.\n");
1061 return (0);
1063 cmd->infile = argv[i];
1064 } else {
1065 for (j = 1; argv[i][j] != 0; j++) {
1066 c = argv[i][j];
1067 switch (c) {
1068 case 'a':
1069 allfiles = 1;
1070 break;
1071 case 'c':
1072 case 'h':
1073 case 'l':
1074 case 'm':
1075 case 't':
1076 if (flag[c])
1077 return (0);
1078 flag[c] = 1;
1079 break;
1080 case 'S':
1082 * sample flag: Ss or Sc.
1083 * Ss means set flag['S'];
1084 * Sc means set flag['C'];
1085 * Sm means set flag['M'];
1087 ch = argv[i][++j]; /* get next char */
1088 if (ch == 's')
1089 ch = 'S';
1090 else if (ch == 'c')
1091 ch = 'C';
1092 else if (ch == 'm')
1093 ch = 'M';
1094 else
1095 return (0);
1097 if (flag[ch])
1098 return (0);
1099 flag[ch] = 1;
1100 break;
1101 case 'C': /* ANSI C syntax */
1102 Cflag = 1;
1103 ch = argv[i][j+1]; /* get next char */
1105 if (ch != 'C')
1106 break;
1107 CCflag = 1;
1108 break;
1109 case 'b':
1111 * Turn TIRPC flag off for
1112 * generating backward compatible
1113 * code
1115 tirpcflag = 0;
1116 break;
1118 case 'I':
1119 inetdflag = 1;
1120 break;
1121 case 'N':
1122 newstyle = 1;
1123 break;
1124 case 'L':
1125 logflag = 1;
1126 break;
1127 case 'K':
1128 if (++i == argc)
1129 return (0);
1130 svcclosetime = argv[i];
1131 goto nextarg;
1132 case 'T':
1133 tblflag = 1;
1134 break;
1135 case 'A':
1136 mtauto = 1;
1137 /* FALLTHRU */
1138 case 'M':
1139 mtflag = 1;
1140 break;
1141 case 'i' :
1142 if (++i == argc)
1143 return (0);
1144 inlinelen = atoi(argv[i]);
1145 goto nextarg;
1146 case 'n':
1147 case 'o':
1148 case 's':
1149 if (argv[i][j - 1] != '-' ||
1150 argv[i][j + 1] != 0)
1151 return (0);
1152 flag[c] = 1;
1153 if (++i == argc)
1154 return (0);
1155 if (c == 'o') {
1156 if (cmd->outfile)
1157 return (0);
1158 cmd->outfile = argv[i];
1160 goto nextarg;
1161 case 'D':
1162 if (argv[i][j - 1] != '-')
1163 return (0);
1164 (void) addarg(argv[i]);
1165 goto nextarg;
1166 case 'v':
1167 version_info();
1168 return (0);
1169 case 'Y':
1170 if (++i == argc)
1171 return (0);
1172 (void) strcpy(pathbuf, argv[i]);
1173 (void) strcat(pathbuf, "/cpp");
1174 CPP = pathbuf;
1175 cppDefined = 1;
1176 goto nextarg;
1177 case 'r':
1178 rflag = !rflag;
1179 break;
1180 default:
1181 return (0);
1184 nextarg:
1189 cmd->cflag = flag['c'];
1190 cmd->hflag = flag['h'];
1191 cmd->lflag = flag['l'];
1192 cmd->mflag = flag['m'];
1193 cmd->nflag = flag['n'];
1194 cmd->sflag = flag['s'];
1195 cmd->tflag = flag['t'];
1196 cmd->Ssflag = flag['S'];
1197 cmd->Scflag = flag['C'];
1198 cmd->makefileflag = flag['M'];
1200 if (tirpcflag) {
1201 if (inetdflag) {
1202 f_print(stderr,
1203 "Cannot use -I flag without -b flag.\n");
1204 return (0);
1206 pmflag = 1;
1207 } else { /* 4.1 mode */
1208 pmflag = 0; /* set pmflag only in tirpcmode */
1209 inetdflag = 1; /* inetdflag is TRUE by default */
1210 if (cmd->nflag) { /* netid needs TIRPC */
1211 f_print(stderr,
1212 "Cannot use netid flag without TIRPC.\n");
1213 return (0);
1217 if (newstyle && (tblflag || cmd->tflag)) {
1218 f_print(stderr, "Cannot use table flags with newstyle.\n");
1219 return (0);
1222 /* check no conflicts with file generation flags */
1223 nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag +
1224 cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag +
1225 cmd->Scflag + cmd->makefileflag;
1227 if (nflags == 0) {
1228 if (cmd->outfile != NULL || cmd->infile == NULL)
1229 return (0);
1230 } else if (cmd->infile == NULL &&
1231 (cmd->Ssflag || cmd->Scflag || cmd->makefileflag)) {
1232 f_print(stderr, "\"infile\" is required for template"
1233 " generation flags.\n");
1234 return (0);
1236 if (nflags > 1) {
1237 f_print(stderr,
1238 "Cannot have more than one file generation flag.\n");
1239 return (0);
1241 return (1);
1244 static void
1245 usage(void)
1247 f_print(stderr, "%s (%d.%d)\n", cmdname, RPCGEN_MAJOR, RPCGEN_MINOR);
1248 f_print(stderr, "usage: %s infile\n", cmdname);
1249 f_print(stderr, "\t%s [-abCLNTMA] [-Dname[=value]] [-i size]"
1250 " [-I [-K seconds]] [-Y path] infile\n", cmdname);
1251 f_print(stderr, "\t%s [-c | -h | -l | -m | -t | -Sc | -Ss | -Sm]"
1252 " [-o outfile] [infile]\n", cmdname);
1253 f_print(stderr, "\t%s [-s nettype]* [-o outfile] [infile]\n", cmdname);
1254 f_print(stderr, "\t%s [-n netid]* [-o outfile] [infile]\n", cmdname);
1255 options_usage();
1256 exit(1);
1259 static void
1260 version_info(void)
1262 f_print(stderr, "%s %d.%d\n", cmdname, RPCGEN_MAJOR, RPCGEN_MINOR);
1263 exit(1);
1266 static void
1267 options_usage(void)
1269 /* BEGIN CSTYLED */
1270 f_print(stderr, "options:\n");
1271 f_print(stderr, "-a\t\tgenerate all files, including samples\n");
1272 f_print(stderr, "-A\t\tgenerate code to enable automatic MT mode\n");
1273 f_print(stderr, "-b\t\tbackward compatibility mode (generates code"
1274 " for SunOS 4.X)\n");
1275 f_print(stderr, "-c\t\tgenerate XDR routines\n");
1276 f_print(stderr, "-C\t\tANSI C mode\n");
1277 f_print(stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n");
1278 f_print(stderr, "-h\t\tgenerate header file\n");
1279 f_print(stderr, "-i size\t\tsize at which to start generating"
1280 " inline code\n");
1281 f_print(stderr, "-I\t\tgenerate code for inetd support in server"
1282 " (for SunOS 4.X)\n");
1283 f_print(stderr, "-K seconds\tserver exits after K seconds of"
1284 " inactivity\n");
1285 f_print(stderr, "-l\t\tgenerate client side stubs\n");
1286 f_print(stderr, "-L\t\tserver errors will be printed to syslog\n");
1287 f_print(stderr, "-m\t\tgenerate server side stubs\n");
1288 f_print(stderr, "-M\t\tgenerate MT-safe code\n");
1289 f_print(stderr, "-n netid\tgenerate server code that supports"
1290 " named netid\n");
1291 f_print(stderr, "-N\t\tsupports multiple arguments and"
1292 " call-by-value\n");
1293 f_print(stderr, "-o outfile\tname of the output file\n");
1294 f_print(stderr, "-s nettype\tgenerate server code that supports named"
1295 " nettype\n");
1296 f_print(stderr, "-Sc\t\tgenerate sample client code that uses remote"
1297 " procedures\n");
1298 f_print(stderr, "-Ss\t\tgenerate sample server code that defines"
1299 " remote procedures\n");
1300 f_print(stderr, "-Sm \t\tgenerate makefile template \n");
1302 f_print(stderr, "-t\t\tgenerate RPC dispatch table\n");
1303 f_print(stderr, "-T\t\tgenerate code to support RPC dispatch tables\n");
1304 f_print(stderr, "-v\t\tprint version information and exit\n");
1305 f_print(stderr, "-Y path\t\tpath where cpp is found\n");
1306 /* END CSTYLED */
1307 exit(1);