1 /* @(#)rpc_main.c 2.2 88/08/01 4.0 RPCSRC */
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
28 * Mountain View, California 94043
31 static char sccsid
[] = "@(#)rpc_main.c 1.7 87/06/24 (C) 1987 SMI";
35 * rpc_main.c, Top level of the RPC protocol compiler.
36 * Copyright (C) 1987, Sun Microsystems, Inc.
43 #include "rpc_parse.h"
46 #define EXTEND 1 /* alias for TRUE */
59 static char CPP
[] = "/lib/cpp";
60 static char CPPFLAGS
[] = "-C";
61 static char *allv
[] = {
62 "rpcgen", "-s", "udp", "-s", "tcp",
64 static int allc
= sizeof(allv
)/sizeof(allv
[0]);
71 struct commandline cmd
;
73 if (!parseargs(argc
, argv
, &cmd
)) {
75 "usage: %s infile\n", cmdname
);
77 " %s [-c | -h | -l | -m] [-o outfile] [infile]\n",
80 " %s [-s udp|tcp]* [-o outfile] [infile]\n",
85 c_output(cmd
.infile
, "-DRPC_XDR", !EXTEND
, cmd
.outfile
);
86 } else if (cmd
.hflag
) {
87 h_output(cmd
.infile
, "-DRPC_HDR", !EXTEND
, cmd
.outfile
);
88 } else if (cmd
.lflag
) {
89 l_output(cmd
.infile
, "-DRPC_CLNT", !EXTEND
, cmd
.outfile
);
90 } else if (cmd
.sflag
|| cmd
.mflag
) {
91 s_output(argc
, argv
, cmd
.infile
, "-DRPC_SVC", !EXTEND
,
92 cmd
.outfile
, cmd
.mflag
);
94 c_output(cmd
.infile
, "-DRPC_XDR", EXTEND
, "_xdr.c");
96 h_output(cmd
.infile
, "-DRPC_HDR", EXTEND
, ".h");
98 l_output(cmd
.infile
, "-DRPC_CLNT", EXTEND
, "_clnt.c");
100 s_output(allc
, allv
, cmd
.infile
, "-DRPC_SVC", EXTEND
,
101 "_svc.c", cmd
.mflag
);
107 * add extension to filename
110 extendfile(file
, ext
)
117 res
= alloc(strlen(file
) + strlen(ext
) + 1);
121 p
= rindex(file
, '.');
123 p
= file
+ strlen(file
);
125 (void) strcpy(res
, file
);
126 (void) strcpy(res
+ (p
- file
), ext
);
131 * Open output file with given extension
134 open_output(infile
, outfile
)
138 if (outfile
== NULL
) {
142 if (infile
!= NULL
&& streq(outfile
, infile
)) {
143 f_print(stderr
, "%s: output would overwrite %s\n", cmdname
,
147 fout
= fopen(outfile
, "w");
149 f_print(stderr
, "%s: unable to open ", cmdname
);
153 record_open(outfile
);
157 * Open input file with given define for C-preprocessor
160 open_input(infile
, define
)
166 infilename
= (infile
== NULL
) ? "<stdin>" : infile
;
171 (void) dup2(pd
[1], 1);
173 execl(CPP
, CPP
, CPPFLAGS
, define
, infile
, NULL
);
181 fin
= fdopen(pd
[0], "r");
183 f_print(stderr
, "%s: ", cmdname
);
190 * Compile into an XDR routine output file
193 c_output(infile
, define
, extend
, outfile
)
204 open_input(infile
, define
);
205 outfilename
= extend
? extendfile(infile
, outfile
) : outfile
;
206 open_output(infile
, outfilename
);
207 f_print(fout
, "#include <rpc/rpc.h>\n");
208 if (infile
&& (include
= extendfile(infile
, ".h"))) {
209 f_print(fout
, "#include \"%s\"\n", include
);
213 while (def
= get_definition()) {
216 if (extend
&& tell
== ftell(fout
)) {
217 (void) unlink(outfilename
);
222 * Compile into an XDR header file
225 h_output(infile
, define
, extend
, outfile
)
235 open_input(infile
, define
);
236 outfilename
= extend
? extendfile(infile
, outfile
) : outfile
;
237 open_output(infile
, outfilename
);
239 while (def
= get_definition()) {
242 if (extend
&& tell
== ftell(fout
)) {
243 (void) unlink(outfilename
);
248 * Compile into an RPC service
251 s_output(argc
, argv
, infile
, define
, extend
, outfile
, nomain
)
265 open_input(infile
, define
);
266 outfilename
= extend
? extendfile(infile
, outfile
) : outfile
;
267 open_output(infile
, outfilename
);
268 f_print(fout
, "#include <stdio.h>\n");
269 f_print(fout
, "#include <rpc/rpc.h>\n");
270 if (infile
&& (include
= extendfile(infile
, ".h"))) {
271 f_print(fout
, "#include \"%s\"\n", include
);
275 while (def
= get_definition()) {
276 foundprogram
|= (def
->def_kind
== DEF_PROGRAM
);
278 if (extend
&& !foundprogram
) {
279 (void) unlink(outfilename
);
283 write_programs((char *)NULL
);
286 do_registers(argc
, argv
);
288 write_programs("static");
293 l_output(infile
, define
, extend
, outfile
)
304 open_input(infile
, define
);
305 outfilename
= extend
? extendfile(infile
, outfile
) : outfile
;
306 open_output(infile
, outfilename
);
307 f_print(fout
, "#include <rpc/rpc.h>\n");
308 if (infile
&& (include
= extendfile(infile
, ".h"))) {
309 f_print(fout
, "#include \"%s\"\n", include
);
313 while (def
= get_definition()) {
314 foundprogram
|= (def
->def_kind
== DEF_PROGRAM
);
316 if (extend
&& !foundprogram
) {
317 (void) unlink(outfilename
);
324 * Perform registrations for service output
327 do_registers(argc
, argv
)
334 for (i
= 1; i
< argc
; i
++) {
335 if (streq(argv
[i
], "-s")) {
336 write_register(argv
[i
+ 1]);
343 * Parse command line arguments
346 parseargs(argc
, argv
, cmd
)
349 struct commandline
*cmd
;
355 char flag
[(1 << 8 * sizeof(char))];
359 cmd
->infile
= cmd
->outfile
= NULL
;
369 for (i
= 1; i
< argc
; i
++) {
370 if (argv
[i
][0] != '-') {
374 cmd
->infile
= argv
[i
];
376 for (j
= 1; argv
[i
][j
] != 0; j
++) {
390 if (argv
[i
][j
- 1] != '-' ||
391 argv
[i
][j
+ 1] != 0) {
399 if (!streq(argv
[i
], "udp") &&
400 !streq(argv
[i
], "tcp")) {
403 } else if (c
== 'o') {
407 cmd
->outfile
= argv
[i
];
419 cmd
->cflag
= flag
['c'];
420 cmd
->hflag
= flag
['h'];
421 cmd
->sflag
= flag
['s'];
422 cmd
->lflag
= flag
['l'];
423 cmd
->mflag
= flag
['m'];
424 nflags
= cmd
->cflag
+ cmd
->hflag
+ cmd
->sflag
+ cmd
->lflag
+ cmd
->mflag
;
426 if (cmd
->outfile
!= NULL
|| cmd
->infile
== NULL
) {
429 } else if (nflags
> 1) {