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 /* Use the libc message catalog for translations. */
74 textdomain (_libc_intl_domainname
);
76 if (!parseargs(argc
, argv
, &cmd
)) {
78 _("usage: %s infile\n"), cmdname
);
80 _(" %s [-c | -h | -l | -m] [-o outfile] [infile]\n"),
83 _(" %s [-s udp|tcp]* [-o outfile] [infile]\n"),
88 c_output(cmd
.infile
, "-DRPC_XDR", !EXTEND
, cmd
.outfile
);
89 } else if (cmd
.hflag
) {
90 h_output(cmd
.infile
, "-DRPC_HDR", !EXTEND
, cmd
.outfile
);
91 } else if (cmd
.lflag
) {
92 l_output(cmd
.infile
, "-DRPC_CLNT", !EXTEND
, cmd
.outfile
);
93 } else if (cmd
.sflag
|| cmd
.mflag
) {
94 s_output(argc
, argv
, cmd
.infile
, "-DRPC_SVC", !EXTEND
,
95 cmd
.outfile
, cmd
.mflag
);
97 c_output(cmd
.infile
, "-DRPC_XDR", EXTEND
, "_xdr.c");
99 h_output(cmd
.infile
, "-DRPC_HDR", EXTEND
, ".h");
101 l_output(cmd
.infile
, "-DRPC_CLNT", EXTEND
, "_clnt.c");
103 s_output(allc
, allv
, cmd
.infile
, "-DRPC_SVC", EXTEND
,
104 "_svc.c", cmd
.mflag
);
110 * add extension to filename
113 extendfile(file
, ext
)
120 res
= alloc(strlen(file
) + strlen(ext
) + 1);
124 p
= rindex(file
, '.');
126 p
= file
+ strlen(file
);
128 (void) strcpy(res
, file
);
129 (void) strcpy(res
+ (p
- file
), ext
);
134 * Open output file with given extension
137 open_output(infile
, outfile
)
141 if (outfile
== NULL
) {
145 if (infile
!= NULL
&& streq(outfile
, infile
)) {
146 f_print(stderr
, _("%s: output would overwrite %s\n"), cmdname
,
150 fout
= fopen(outfile
, "w");
152 f_print(stderr
, _("%s: unable to open "), cmdname
);
156 record_open(outfile
);
160 * Open input file with given define for C-preprocessor
163 open_input(infile
, define
)
169 infilename
= (infile
== NULL
) ? "<stdin>" : infile
;
174 (void) dup2(pd
[1], 1);
176 execl(CPP
, CPP
, CPPFLAGS
, define
, infile
, NULL
);
184 fin
= fdopen(pd
[0], "r");
186 f_print(stderr
, "%s: ", cmdname
);
193 * Compile into an XDR routine output file
196 c_output(infile
, define
, extend
, outfile
)
207 open_input(infile
, define
);
208 outfilename
= extend
? extendfile(infile
, outfile
) : outfile
;
209 open_output(infile
, outfilename
);
210 f_print(fout
, "#include <rpc/rpc.h>\n");
211 if (infile
&& (include
= extendfile(infile
, ".h"))) {
212 f_print(fout
, "#include \"%s\"\n", include
);
216 while (def
= get_definition()) {
219 if (extend
&& tell
== ftell(fout
)) {
220 (void) unlink(outfilename
);
225 * Compile into an XDR header file
228 h_output(infile
, define
, extend
, outfile
)
238 open_input(infile
, define
);
239 outfilename
= extend
? extendfile(infile
, outfile
) : outfile
;
240 open_output(infile
, outfilename
);
242 while (def
= get_definition()) {
245 if (extend
&& tell
== ftell(fout
)) {
246 (void) unlink(outfilename
);
251 * Compile into an RPC service
254 s_output(argc
, argv
, infile
, define
, extend
, outfile
, nomain
)
268 open_input(infile
, define
);
269 outfilename
= extend
? extendfile(infile
, outfile
) : outfile
;
270 open_output(infile
, outfilename
);
271 f_print(fout
, "#include <stdio.h>\n");
272 f_print(fout
, "#include <rpc/rpc.h>\n");
273 if (infile
&& (include
= extendfile(infile
, ".h"))) {
274 f_print(fout
, "#include \"%s\"\n", include
);
278 while (def
= get_definition()) {
279 foundprogram
|= (def
->def_kind
== DEF_PROGRAM
);
281 if (extend
&& !foundprogram
) {
282 (void) unlink(outfilename
);
286 write_programs((char *)NULL
);
289 do_registers(argc
, argv
);
291 write_programs("static");
296 l_output(infile
, define
, extend
, outfile
)
307 open_input(infile
, define
);
308 outfilename
= extend
? extendfile(infile
, outfile
) : outfile
;
309 open_output(infile
, outfilename
);
310 f_print(fout
, "#include <rpc/rpc.h>\n");
311 if (infile
&& (include
= extendfile(infile
, ".h"))) {
312 f_print(fout
, "#include \"%s\"\n", include
);
316 while (def
= get_definition()) {
317 foundprogram
|= (def
->def_kind
== DEF_PROGRAM
);
319 if (extend
&& !foundprogram
) {
320 (void) unlink(outfilename
);
327 * Perform registrations for service output
330 do_registers(argc
, argv
)
337 for (i
= 1; i
< argc
; i
++) {
338 if (streq(argv
[i
], "-s")) {
339 write_register(argv
[i
+ 1]);
346 * Parse command line arguments
349 parseargs(argc
, argv
, cmd
)
352 struct commandline
*cmd
;
358 char flag
[(1 << 8 * sizeof(char))];
362 cmd
->infile
= cmd
->outfile
= NULL
;
372 for (i
= 1; i
< argc
; i
++) {
373 if (argv
[i
][0] != '-') {
377 cmd
->infile
= argv
[i
];
379 for (j
= 1; argv
[i
][j
] != 0; j
++) {
393 if (argv
[i
][j
- 1] != '-' ||
394 argv
[i
][j
+ 1] != 0) {
402 if (!streq(argv
[i
], "udp") &&
403 !streq(argv
[i
], "tcp")) {
406 } else if (c
== 'o') {
410 cmd
->outfile
= argv
[i
];
422 cmd
->cflag
= flag
['c'];
423 cmd
->hflag
= flag
['h'];
424 cmd
->sflag
= flag
['s'];
425 cmd
->lflag
= flag
['l'];
426 cmd
->mflag
= flag
['m'];
427 nflags
= cmd
->cflag
+ cmd
->hflag
+ cmd
->sflag
+ cmd
->lflag
+ cmd
->mflag
;
429 if (cmd
->outfile
!= NULL
|| cmd
->infile
== NULL
) {
432 } else if (nflags
> 1) {