rpc_main.c revision 1.40 1 /* $NetBSD: rpc_main.c,v 1.40 2015/05/09 21:44:47 christos Exp $ */
2
3 /*
4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user or with the express written consent of
10 * Sun Microsystems, Inc.
11 *
12 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
13 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
14 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 *
16 * Sun RPC is provided with no support and without any obligation on the
17 * part of Sun Microsystems, Inc. to assist in its use, correction,
18 * modification or enhancement.
19 *
20 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
21 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
22 * OR ANY PART THEREOF.
23 *
24 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
25 * or profits or other special, indirect and consequential damages, even if
26 * Sun has been advised of the possibility of such damages.
27 *
28 * Sun Microsystems, Inc.
29 * 2550 Garcia Avenue
30 * Mountain View, California 94043
31 */
32
33 #if HAVE_NBTOOL_CONFIG_H
34 #include "nbtool_config.h"
35 #endif
36
37 #include <sys/cdefs.h>
38 #if defined(__RCSID) && !defined(lint)
39 #if 0
40 static char sccsid[] = "@(#)rpc_main.c 1.30 89/03/30 (C) 1987 SMI";
41 #else
42 __RCSID("$NetBSD: rpc_main.c,v 1.40 2015/05/09 21:44:47 christos Exp $");
43 #endif
44 #endif
45
46 /*
47 * rpc_main.c, Top level of the RPC protocol compiler.
48 */
49
50 #define RPCGEN_VERSION "199506"/* This program's version (year & month) */
51
52 #include <sys/types.h>
53 #include <sys/param.h>
54 #include <sys/file.h>
55 #include <sys/stat.h>
56 #include <ctype.h>
57 #include <err.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62 #include "rpc_scan.h"
63 #include "rpc_parse.h"
64 #include "rpc_util.h"
65
66 #define EXTEND 1 /* alias for TRUE */
67 #define DONT_EXTEND 0 /* alias for FALSE */
68
69 struct commandline {
70 int cflag; /* xdr C routines */
71 int hflag; /* header file */
72 int lflag; /* client side stubs */
73 int mflag; /* server side stubs */
74 int nflag; /* netid flag */
75 int sflag; /* server stubs for the given transport */
76 int tflag; /* dispatch Table file */
77 int Ssflag; /* produce server sample code */
78 int Scflag; /* produce client sample code */
79 char *infile; /* input module name */
80 char *outfile; /* output module name */
81 };
82
83
84 static char *cmdname;
85
86 static const char *svcclosetime = "120";
87 static const char *CPP;
88 static char CPPFLAGS[] = "-C";
89 static char pathbuf[MAXPATHLEN + 1];
90
91 /* these cannot be const */
92 static char allv0[] = "rpcgen";
93 static char allv1[] = "-s";
94 static char allv2[] = "udp";
95 static char allv3[] = "-s";
96 static char allv4[] = "tcp";
97 static char *allv[] = {
98 allv0,
99 allv1,
100 allv2,
101 allv3,
102 allv4,
103 };
104 static int allc = sizeof(allv) / sizeof(allv[0]);
105
106 /* these cannot be const */
107 static char allnv0[] = "rpcgen";
108 static char allnv1[] = "-s";
109 static char allnv2[] = "netpath";
110 static char *allnv[] = {
111 allnv0,
112 allnv1,
113 allnv2,
114 };
115 static int allnc = sizeof(allnv) / sizeof(allnv[0]);
116
117 #define ARGLISTLEN 20
118 #define FIXEDARGS 2
119
120 static const char *arglist[ARGLISTLEN];
121 static int argcount = FIXEDARGS;
122
123
124 int nonfatalerrors; /* errors */
125 int inetdflag /* = 1 */ ; /* Support for inetd *//* is now the default */
126 int pmflag; /* Support for port monitors */
127 int logflag; /* Use syslog instead of fprintf for errors */
128 int tblflag; /* Support for dispatch table file */
129 int BSDflag; /* use BSD cplusplus macros */
130 int callerflag; /* Generate svc_caller() function */
131 int docleanup = 1; /* cause atexit to remove files */
132
133 #define INLINE 3
134 /*length at which to start doing an inline */
135
136 int doinline = INLINE; /* length at which to start doing an inline. 3
137 * = default if 0, no xdr_inline code */
138
139 int indefinitewait; /* If started by port monitors, hang till it
140 * wants */
141 int exitnow; /* If started by port monitors, exit after the
142 * call */
143 int timerflag; /* TRUE if !indefinite && !exitnow */
144 int newstyle; /* newstyle of passing arguments (by value) */
145 int Mflag = 0; /* multithread safe */
146 static int allfiles; /* generate all files */
147 int tirpcflag = 1; /* generating code for tirpc, by default */
148
149 #ifdef __MSDOS__
150 static char *dos_cppfile = NULL;
151 #endif
152
153 static char *extendfile(const char *, const char *);
154 static void open_output(const char *, const char *);
155 static void add_warning(void);
156 static void clear_args(void);
157 static void open_input(const char *, const char *);
158 static int check_nettype(const char *, const char *[]);
159 static void c_output(const char *, const char *, int, const char *);
160 static void c_initialize(void);
161 static char *generate_guard(const char *);
162 static void h_output(const char *, const char *, int, const char *);
163 static void s_output(int, char *[], char *, const char *, int, const char *,
164 int, int);
165 static void l_output(const char *, const char *, int, const char *);
166 static void t_output(const char *, const char *, int, const char *);
167 static void svc_output(const char *, const char *, int, const char *);
168 static void clnt_output(const char *, const char *, int, const char *);
169 static int do_registers(int, char *[]);
170 static void addarg(const char *);
171 static void putarg(int, const char *);
172 static void checkfiles(const char *, const char *);
173 static int parseargs(int, char *[], struct commandline *);
174 static void usage(void) __dead;
175 static void options_usage(void) __dead;
176
177 int
178 main(int argc, char *argv[])
179 {
180 struct commandline cmd;
181
182 setprogname(argv[0]);
183 if ((CPP = getenv("RPCGEN_CPP")) == NULL) {
184 CPP = "/usr/bin/cpp";
185 if (access(CPP, X_OK))
186 CPP = "/usr/bin/clang-cpp";
187 }
188
189 (void) memset((char *) &cmd, 0, sizeof(struct commandline));
190 clear_args();
191 atexit(crash);
192 if (!parseargs(argc, argv, &cmd))
193 usage();
194
195 if (cmd.cflag || cmd.hflag || cmd.lflag || cmd.tflag || cmd.sflag ||
196 cmd.mflag || cmd.nflag || cmd.Ssflag || cmd.Scflag) {
197 checkfiles(cmd.infile, cmd.outfile);
198 } else
199 checkfiles(cmd.infile, NULL);
200
201 if (cmd.cflag) {
202 c_output(cmd.infile, "-DRPC_XDR", DONT_EXTEND, cmd.outfile);
203 } else
204 if (cmd.hflag) {
205 h_output(cmd.infile, "-DRPC_HDR", DONT_EXTEND, cmd.outfile);
206 } else
207 if (cmd.lflag) {
208 l_output(cmd.infile, "-DRPC_CLNT", DONT_EXTEND, cmd.outfile);
209 } else
210 if (cmd.sflag || cmd.mflag || (cmd.nflag)) {
211 s_output(argc, argv, cmd.infile, "-DRPC_SVC", DONT_EXTEND,
212 cmd.outfile, cmd.mflag, cmd.nflag);
213 } else
214 if (cmd.tflag) {
215 t_output(cmd.infile, "-DRPC_TBL", DONT_EXTEND, cmd.outfile);
216 } else
217 if (cmd.Ssflag) {
218 svc_output(cmd.infile, "-DRPC_SERVER", DONT_EXTEND, cmd.outfile);
219 } else
220 if (cmd.Scflag) {
221 clnt_output(cmd.infile, "-DRPC_CLIENT", DONT_EXTEND, cmd.outfile);
222 } else {
223 /* the rescans
224 * are
225 * required,
226 * since cpp
227 * may effect
228 * input */
229 c_output(cmd.infile, "-DRPC_XDR", EXTEND, "_xdr.c");
230 reinitialize();
231 h_output(cmd.infile, "-DRPC_HDR", EXTEND, ".h");
232 reinitialize();
233 l_output(cmd.infile, "-DRPC_CLNT", EXTEND, "_clnt.c");
234 reinitialize();
235 if (inetdflag || !tirpcflag)
236 s_output(allc, allv, cmd.infile, "-DRPC_SVC", EXTEND,
237 "_svc.c", cmd.mflag, cmd.nflag);
238 else
239 s_output(allnc, allnv, cmd.infile, "-DRPC_SVC",
240 EXTEND, "_svc.c", cmd.mflag, cmd.nflag);
241 if (tblflag) {
242 reinitialize();
243 t_output(cmd.infile, "-DRPC_TBL", EXTEND, "_tbl.i");
244 }
245 if (allfiles) {
246 reinitialize();
247 svc_output(cmd.infile, "-DRPC_SERVER", EXTEND, "_server.c");
248 }
249 if (allfiles) {
250 reinitialize();
251 clnt_output(cmd.infile, "-DRPC_CLIENT", EXTEND, "_client.c");
252 }
253 }
254 #ifdef __MSDOS__
255 if (dos_cppfile != NULL) {
256 (void) fclose(fin);
257 (void) unlink(dos_cppfile);
258 }
259 #endif
260 docleanup = 0;
261 exit(nonfatalerrors);
262 /* NOTREACHED */
263 }
264 /*
265 * add extension to filename
266 */
267 static char *
268 extendfile(const char *path, const char *ext)
269 {
270 const char *file;
271 char *res;
272 const char *p;
273
274 if ((file = strrchr(path, '/')) == NULL)
275 file = path;
276 else
277 file++;
278
279 res = alloc(strlen(file) + strlen(ext) + 1);
280 if (res == NULL) {
281 errx(1, "Out of memory");
282 }
283 p = strrchr(file, '.');
284 if (p == NULL) {
285 p = file + strlen(file);
286 }
287 (void) strcpy(res, file);
288 (void) strcpy(res + (p - file), ext);
289 return (res);
290 }
291 /*
292 * Open output file with given extension
293 */
294 static void
295 open_output(const char *infile, const char *outfile)
296 {
297
298 if (outfile == NULL) {
299 fout = stdout;
300 return;
301 }
302 if (infile != NULL && streq(outfile, infile)) {
303 errx(EXIT_FAILURE, "output would overwrite `%s'", infile);
304 }
305 fout = fopen(outfile, "w");
306 if (fout == NULL) {
307 err(EXIT_FAILURE, "Can't to open `%s'", outfile);
308 }
309 record_open(outfile);
310
311 }
312
313 static void
314 add_warning(void)
315 {
316 f_print(fout, "/*\n");
317 f_print(fout, " * Please do not edit this file.\n");
318 f_print(fout, " * It was generated using rpcgen.\n");
319 f_print(fout, " */\n\n");
320 }
321
322 /* clear list of arguments */
323 static void
324 clear_args(void)
325 {
326 int i;
327 for (i = FIXEDARGS; i < ARGLISTLEN; i++)
328 arglist[i] = NULL;
329 argcount = FIXEDARGS;
330 }
331
332 /*
333 * Open input file with given define for C-preprocessor
334 */
335 static void
336 open_input(const char *infile, const char *define)
337 {
338 int pd[2];
339
340 infilename = (infile == NULL) ? "<stdin>" : infile;
341 #ifdef __MSDOS__
342 #define DOSCPP "\\prog\\bc31\\bin\\cpp.exe"
343 {
344 int retval;
345 char drive[MAXDRIVE], dir[MAXDIR], name[MAXFILE], ext[MAXEXT];
346 char cppfile[MAXPATH];
347 char *cpp;
348
349 if ((cpp = getenv("RPCGEN_CPP")) == NULL &&
350 (cpp = searchpath("cpp.exe")) == NULL)
351 cpp = DOSCPP;
352
353 putarg(0, cpp);
354 putarg(1, "-P-");
355 putarg(2, CPPFLAGS);
356 addarg(define);
357 addarg(infile);
358 addarg(NULL);
359
360 retval = spawnvp(P_WAIT, arglist[0], arglist);
361 if (retval != 0) {
362 err(EXIT_FAILURE, "C PreProcessor failed");
363 }
364 fnsplit(infile, drive, dir, name, ext);
365 fnmerge(cppfile, drive, dir, name, ".i");
366
367 fin = fopen(cppfile, "r");
368 if (fin == NULL) {
369 err(EXIT_FAILURE, "Can't open `%s'", cppfile);
370 }
371 dos_cppfile = strdup(cppfile);
372 if (dos_cppfile == NULL) {
373 err(EXIT_FAILURE, "Can't copy `%s'", cppfile);
374 }
375 }
376 #else
377 (void) pipe(pd);
378 switch (fork()) {
379 case 0:
380 putarg(0, CPP);
381 putarg(1, CPPFLAGS);
382 addarg(define);
383 addarg(infile);
384 addarg(NULL);
385 (void) close(1);
386 (void) dup2(pd[1], 1);
387 (void) close(pd[0]);
388 execvp(arglist[0], __UNCONST(arglist));
389 err(EXIT_FAILURE, "$RPCGEN_CPP: %s", CPP);
390 case -1:
391 err(EXIT_FAILURE, "fork");
392 }
393 (void) close(pd[1]);
394 fin = fdopen(pd[0], "r");
395 #endif
396 if (fin == NULL) {
397 err(EXIT_FAILURE, "Can't open `%s'", infilename);
398 }
399 }
400 /* valid tirpc nettypes */
401 static const char *valid_ti_nettypes[] =
402 {
403 "netpath",
404 "visible",
405 "circuit_v",
406 "datagram_v",
407 "circuit_n",
408 "datagram_n",
409 "udp",
410 "tcp",
411 "raw",
412 NULL
413 };
414 /* valid inetd nettypes */
415 static const char *valid_i_nettypes[] =
416 {
417 "udp",
418 "tcp",
419 NULL
420 };
421
422 static int
423 check_nettype(const char *name, const char *list_to_check[])
424 {
425 int i;
426 for (i = 0; list_to_check[i] != NULL; i++) {
427 if (strcmp(name, list_to_check[i]) == 0) {
428 return 1;
429 }
430 }
431 f_print(stderr, "illegal nettype :\'%s\'\n", name);
432 return 0;
433 }
434 /*
435 * Compile into an XDR routine output file
436 */
437
438 static void
439 c_output(const char *infile, const char *define, int extend,
440 const char *outfile)
441 {
442 definition *def;
443 char *include;
444 const char *outfilename;
445 long tell;
446
447 c_initialize();
448 open_input(infile, define);
449 outfilename = extend ? extendfile(infile, outfile) : outfile;
450 open_output(infile, outfilename);
451 add_warning();
452 if (infile && (include = extendfile(infile, ".h"))) {
453 f_print(fout, "#include \"%s\"\n", include);
454 free(include);
455 /* .h file already contains rpc/rpc.h */
456 } else
457 f_print(fout, "#include <rpc/rpc.h>\n");
458 tell = ftell(fout);
459 while ((def = get_definition()) != NULL) {
460 emit(def);
461 }
462 if (extend && tell == ftell(fout)) {
463 (void) unlink(outfilename);
464 }
465 }
466
467
468 static void
469 c_initialize(void)
470 {
471
472 /* add all the starting basic types */
473
474 add_type(1, "int");
475 add_type(1, "long");
476 add_type(1, "short");
477 add_type(1, "bool");
478
479 add_type(1, "u_int");
480 add_type(1, "u_long");
481 add_type(1, "u_short");
482
483 }
484
485 const char rpcgen_table_dcl[] = "struct rpcgen_table {\n\
486 char *(*proc)();\n\
487 xdrproc_t xdr_arg;\n\
488 unsigned len_arg;\n\
489 xdrproc_t xdr_res;\n\
490 unsigned len_res;\n\
491 };\n";
492
493
494 static char *
495 generate_guard(const char *pathname)
496 {
497 const char *filename;
498 char *guard, *tmp, *tmp2;
499
500 filename = strrchr(pathname, '/'); /* find last component */
501 filename = ((filename == 0) ? pathname : filename + 1);
502 guard = strdup(filename);
503 /* convert to upper case */
504 tmp = guard;
505 while (*tmp) {
506 *tmp = toupper((unsigned char)*tmp);
507 tmp++;
508 }
509
510 tmp2 = extendfile(guard, "_H_RPCGEN");
511 free(guard);
512 guard = tmp2;
513 return (guard);
514 }
515
516 /*
517 * Compile into an XDR header file
518 */
519 static void
520 h_output(const char *infile, const char *define, int extend,
521 const char *outfile)
522 {
523 definition *def;
524 const char *outfilename;
525 long tell;
526 char *guard;
527 list *l;
528 int did;
529
530 open_input(infile, define);
531 outfilename = extend ? extendfile(infile, outfile) : outfile;
532 open_output(infile, outfilename);
533 add_warning();
534 if (outfilename || infile)
535 guard = generate_guard(outfilename ? outfilename : infile);
536 else {
537 guard = strdup("STDIN_");
538 if (guard == NULL) {
539 err(EXIT_FAILURE, "strdup");
540 }
541 }
542
543 f_print(fout, "#ifndef _%s\n#define _%s\n\n", guard,
544 guard);
545
546 f_print(fout, "#define RPCGEN_VERSION\t%s\n\n", RPCGEN_VERSION);
547 f_print(fout, "#include <rpc/rpc.h>\n\n");
548
549 tell = ftell(fout);
550 /* print data definitions */
551 while ((def = get_definition()) != NULL) {
552 print_datadef(def);
553 }
554
555 /* print function declarations. Do this after data definitions
556 * because they might be used as arguments for functions */
557 did = 0;
558 for (l = defined; l != NULL; l = l->next) {
559 print_funcdef(l->val, &did);
560 }
561 print_funcend(did);
562
563 for (l = defined; l != NULL; l = l->next) {
564 print_progdef(l->val);
565 }
566
567 if (extend && tell == ftell(fout)) {
568 (void) unlink(outfilename);
569 } else
570 if (tblflag) {
571 f_print(fout, rpcgen_table_dcl);
572 }
573 f_print(fout, "\n#endif /* !_%s */\n", guard);
574
575 free(guard);
576 }
577
578 /*
579 * Compile into an RPC service
580 */
581 static void
582 s_output(int argc, char *argv[], char *infile,
583 const char *define, int extend, const char *outfile, int nomain,
584 int netflag)
585 {
586 char *include;
587 definition *def;
588 int foundprogram = 0;
589 const char *outfilename;
590
591 open_input(infile, define);
592 outfilename = extend ? extendfile(infile, outfile) : outfile;
593 open_output(infile, outfilename);
594 add_warning();
595 if (infile && (include = extendfile(infile, ".h"))) {
596 f_print(fout, "#include \"%s\"\n", include);
597 free(include);
598 } else
599 f_print(fout, "#include <rpc/rpc.h>\n");
600
601 f_print(fout, "#include <sys/ioctl.h>\n");
602 f_print(fout, "#include <fcntl.h>\n");
603 f_print(fout, "#include <stdio.h>\n");
604 f_print(fout, "#include <err.h>\n");
605 f_print(fout, "#include <stdlib.h>\n");
606 f_print(fout, "#include <unistd.h>\n");
607 f_print(fout, "#include <rpc/pmap_clnt.h>\n");
608 f_print(fout, "#include <string.h>\n");
609 f_print(fout, "#include <netdb.h>\n");
610 if (strcmp(svcclosetime, "-1") == 0)
611 indefinitewait = 1;
612 else
613 if (strcmp(svcclosetime, "0") == 0)
614 exitnow = 1;
615 else
616 if (inetdflag || pmflag) {
617 f_print(fout, "#include <signal.h>\n");
618 timerflag = 1;
619 }
620 if (!tirpcflag && inetdflag)
621 f_print(fout, "#include <sys/ttycom.h>\n");
622 if (inetdflag || pmflag) {
623 f_print(fout, "#ifdef __cplusplus\n");
624 f_print(fout, "#include <sysent.h>\n");
625 f_print(fout, "#endif /* __cplusplus */\n");
626 }
627 if (tirpcflag)
628 f_print(fout, "#include <sys/types.h>\n");
629
630 f_print(fout, "#include <memory.h>\n");
631
632 if (inetdflag || !tirpcflag) {
633 f_print(fout, "#include <sys/socket.h>\n");
634 f_print(fout, "#include <netinet/in.h>\n");
635 }
636 if ((netflag || pmflag) && tirpcflag) {
637 f_print(fout, "#include <netconfig.h>\n");
638 }
639 if ( /* timerflag && */ tirpcflag)
640 f_print(fout, "#include <sys/resource.h>\n");
641 if (logflag || inetdflag || pmflag)
642 f_print(fout, "#include <syslog.h>\n");
643
644 f_print(fout, "\n#define SIG_PF void(*)(int)\n");
645
646 f_print(fout, "\n#ifdef DEBUG\n#define RPC_SVC_FG\n#endif\n");
647 if (timerflag)
648 f_print(fout, "\n#define _RPCSVC_CLOSEDOWN %s\n", svcclosetime);
649 while ((def = get_definition()) != NULL) {
650 foundprogram |= (def->def_kind == DEF_PROGRAM);
651 }
652 if (extend && !foundprogram) {
653 (void) unlink(outfilename);
654 return;
655 }
656 if (callerflag) /* EVAS */
657 f_print(fout, "\nstatic SVCXPRT *caller;\n"); /* EVAS */
658 write_most(infile, netflag, nomain);
659 if (!nomain) {
660 if (!do_registers(argc, argv)) {
661 if (outfilename)
662 (void) unlink(outfilename);
663 usage();
664 }
665 write_rest();
666 }
667 }
668 /*
669 * generate client side stubs
670 */
671 static void
672 l_output(const char *infile, const char *define, int extend,
673 const char *outfile)
674 {
675 char *include;
676 definition *def;
677 int foundprogram = 0;
678 const char *outfilename;
679
680 open_input(infile, define);
681 outfilename = extend ? extendfile(infile, outfile) : outfile;
682 open_output(infile, outfilename);
683 add_warning();
684 f_print(fout, "#include <memory.h>\n");
685 if (infile && (include = extendfile(infile, ".h"))) {
686 f_print(fout, "#include \"%s\"\n", include);
687 free(include);
688 } else
689 f_print(fout, "#include <rpc/rpc.h>\n");
690 while ((def = get_definition()) != NULL) {
691 foundprogram |= (def->def_kind == DEF_PROGRAM);
692 }
693 if (extend && !foundprogram) {
694 (void) unlink(outfilename);
695 return;
696 }
697 write_stubs();
698 }
699 /*
700 * generate the dispatch table
701 */
702 static void
703 t_output(const char *infile, const char *define, int extend,
704 const char *outfile)
705 {
706 definition *def;
707 int foundprogram = 0;
708 const char *outfilename;
709
710 open_input(infile, define);
711 outfilename = extend ? extendfile(infile, outfile) : outfile;
712 open_output(infile, outfilename);
713 add_warning();
714 while ((def = get_definition()) != NULL) {
715 foundprogram |= (def->def_kind == DEF_PROGRAM);
716 }
717 if (extend && !foundprogram) {
718 (void) unlink(outfilename);
719 return;
720 }
721 write_tables();
722 }
723 /* sample routine for the server template */
724 static void
725 svc_output(const char *infile, const char *define, int extend,
726 const char *outfile)
727 {
728 definition *def;
729 char *include;
730 const char *outfilename;
731 long tell;
732
733 open_input(infile, define);
734 outfilename = extend ? extendfile(infile, outfile) : outfile;
735 checkfiles(infile, outfilename); /* check if outfile already
736 * exists. if so, print an
737 * error message and exit */
738 open_output(infile, outfilename);
739 add_sample_msg();
740
741 if (infile && (include = extendfile(infile, ".h"))) {
742 f_print(fout, "#include \"%s\"\n", include);
743 free(include);
744 } else
745 f_print(fout, "#include <rpc/rpc.h>\n");
746
747 tell = ftell(fout);
748 while ((def = get_definition()) != NULL) {
749 write_sample_svc(def);
750 }
751 if (extend && tell == ftell(fout)) {
752 (void) unlink(outfilename);
753 }
754 }
755
756
757 /* sample main routine for client */
758 static void
759 clnt_output(const char *infile, const char *define, int extend,
760 const char *outfile)
761 {
762 definition *def;
763 char *include;
764 const char *outfilename;
765 long tell;
766 int has_program = 0;
767
768 open_input(infile, define);
769 outfilename = extend ? extendfile(infile, outfile) : outfile;
770 checkfiles(infile, outfilename); /* check if outfile already
771 * exists. if so, print an
772 * error message and exit */
773
774 open_output(infile, outfilename);
775 add_sample_msg();
776 f_print(fout, "#include <stdio.h>\n");
777 f_print(fout, "#include <err.h>\n");
778 if (infile && (include = extendfile(infile, ".h"))) {
779 f_print(fout, "#include \"%s\"\n", include);
780 free(include);
781 } else
782 f_print(fout, "#include <rpc/rpc.h>\n");
783 tell = ftell(fout);
784 while ((def = get_definition()) != NULL) {
785 has_program += write_sample_clnt(def);
786 }
787
788 if (has_program)
789 write_sample_clnt_main();
790
791 if (extend && tell == ftell(fout)) {
792 (void) unlink(outfilename);
793 }
794 }
795 /*
796 * Perform registrations for service output
797 * Return 0 if failed; 1 otherwise.
798 */
799 static int
800 do_registers(int argc, char *argv[])
801 {
802 int i;
803
804 if (inetdflag || !tirpcflag) {
805 for (i = 1; i < argc; i++) {
806 if (streq(argv[i], "-s")) {
807 if (!check_nettype(argv[i + 1], valid_i_nettypes))
808 return 0;
809 write_inetd_register(argv[i + 1]);
810 i++;
811 }
812 }
813 } else {
814 for (i = 1; i < argc; i++)
815 if (streq(argv[i], "-s")) {
816 if (!check_nettype(argv[i + 1], valid_ti_nettypes))
817 return 0;
818 write_nettype_register(argv[i + 1]);
819 i++;
820 } else
821 if (streq(argv[i], "-n")) {
822 write_netid_register(argv[i + 1]);
823 i++;
824 }
825 }
826 return 1;
827 }
828 /*
829 * Add another argument to the arg list
830 */
831 static void
832 addarg(const char *cp)
833 {
834 if (argcount >= ARGLISTLEN) {
835 errx(EXIT_FAILURE, "too many defines");
836 /* NOTREACHED */
837 }
838 arglist[argcount++] = cp;
839
840 }
841
842 static void
843 putarg(int pwhere, const char *cp)
844 {
845 if (pwhere >= ARGLISTLEN) {
846 errx(EXIT_FAILURE, "arglist coding error");
847 /* NOTREACHED */
848 }
849 arglist[pwhere] = cp;
850
851 }
852 /*
853 * if input file is stdin and an output file is specified then complain
854 * if the file already exists. Otherwise the file may get overwritten
855 * If input file does not exist, exit with an error
856 */
857
858 static void
859 checkfiles(const char *infile, const char *outfile)
860 {
861
862 struct stat buf;
863
864 if (infile) /* infile ! = NULL */
865 if (stat(infile, &buf) < 0) {
866 err(EXIT_FAILURE, "Can't stat `%s'", infile);
867 };
868 #if 0
869 if (outfile) {
870 if (stat(outfile, &buf) < 0)
871 return; /* file does not exist */
872 else {
873 errx("`%s' already exists and may be overwritten",
874 outfile);
875 }
876 }
877 #endif
878 }
879 /*
880 * Parse command line arguments
881 */
882 static int
883 parseargs(int argc, char *argv[], struct commandline *cmd)
884 {
885 int i;
886 int j;
887 int c;
888 char flag[1 << CHAR_BIT];
889 int nflags;
890
891 cmdname = argv[0];
892 cmd->infile = cmd->outfile = NULL;
893 if (argc < 2) {
894 return (0);
895 }
896 allfiles = 0;
897 flag['c'] = 0;
898 flag['h'] = 0;
899 flag['l'] = 0;
900 flag['m'] = 0;
901 flag['o'] = 0;
902 flag['s'] = 0;
903 flag['n'] = 0;
904 flag['t'] = 0;
905 flag['S'] = 0;
906 flag['C'] = 0;
907 for (i = 1; i < argc; i++) {
908 if (argv[i][0] != '-') {
909 if (cmd->infile) {
910 f_print(stderr, "Cannot specify more than one input file!\n");
911
912 return (0);
913 }
914 cmd->infile = argv[i];
915 } else {
916 for (j = 1; argv[i][j] != 0; j++) {
917 c = argv[i][j];
918 switch (c) {
919 case 'A':
920 callerflag = 1;
921 break;
922 case 'a':
923 allfiles = 1;
924 break;
925 case 'B':
926 BSDflag = 1;
927 break;
928 case 'c':
929 case 'h':
930 case 'l':
931 case 'm':
932 case 't':
933 if (flag[c]) {
934 return (0);
935 }
936 flag[c] = 1;
937 break;
938 case 'S':
939 /* sample flag: Ss or Sc. Ss means set
940 * flag['S']; Sc means set flag['C']; */
941 c = argv[i][++j]; /* get next char */
942 if (c == 's')
943 c = 'S';
944 else
945 if (c == 'c')
946 c = 'C';
947 else
948 return (0);
949
950 if (flag[c]) {
951 return (0);
952 }
953 flag[c] = 1;
954 break;
955 case 'C': /* deprecated ANSI C syntax */
956 break;
957
958 case 'b': /* turn TIRPC flag off for
959 * generating backward
960 * compatible */
961 tirpcflag = 0;
962 break;
963
964 case 'I':
965 inetdflag = 1;
966 break;
967 case 'M':
968 Mflag = 1;
969 break;
970 case 'N':
971 newstyle = 1;
972 break;
973 case 'L':
974 logflag = 1;
975 break;
976 case 'K':
977 if (++i == argc) {
978 return (0);
979 }
980 svcclosetime = argv[i];
981 goto nextarg;
982 case 'T':
983 tblflag = 1;
984 break;
985 case 'i':
986 if (++i == argc) {
987 return (0);
988 }
989 doinline = atoi(argv[i]);
990 goto nextarg;
991 case 'n':
992 case 'o':
993 case 's':
994 if (argv[i][j - 1] != '-' ||
995 argv[i][j + 1] != 0) {
996 return (0);
997 }
998 flag[c] = 1;
999 if (++i == argc) {
1000 return (0);
1001 }
1002 if (c == 's') {
1003 if (!streq(argv[i], "udp") &&
1004 !streq(argv[i], "tcp")) {
1005 return (0);
1006 }
1007 } else
1008 if (c == 'o') {
1009 if (cmd->outfile) {
1010 return (0);
1011 }
1012 cmd->outfile = argv[i];
1013 }
1014 goto nextarg;
1015 case 'D':
1016 if (argv[i][j - 1] != '-') {
1017 return (0);
1018 }
1019 (void) addarg(argv[i]);
1020 goto nextarg;
1021 case 'Y':
1022 if (++i == argc) {
1023 return (0);
1024 }
1025 (void) strlcpy(pathbuf, argv[i],
1026 sizeof(pathbuf));
1027 (void) strlcat(pathbuf, "/cpp",
1028 sizeof(pathbuf));
1029 CPP = pathbuf;
1030 goto nextarg;
1031
1032 case 'v':
1033 printf("version 1.0\n");
1034 exit(0);
1035
1036 default:
1037 return (0);
1038 }
1039 }
1040 nextarg:
1041 ;
1042 }
1043 }
1044
1045 cmd->cflag = flag['c'];
1046 cmd->hflag = flag['h'];
1047 cmd->lflag = flag['l'];
1048 cmd->mflag = flag['m'];
1049 cmd->nflag = flag['n'];
1050 cmd->sflag = flag['s'];
1051 cmd->tflag = flag['t'];
1052 cmd->Ssflag = flag['S'];
1053 cmd->Scflag = flag['C'];
1054
1055 if (tirpcflag) {
1056 pmflag = inetdflag ? 0 : 1; /* pmflag or inetdflag is
1057 * always TRUE */
1058 if ((inetdflag && cmd->nflag)) { /* netid not allowed
1059 * with inetdflag */
1060 f_print(stderr, "Cannot use netid flag with inetd flag!\n");
1061 return (0);
1062 }
1063 } else { /* 4.1 mode */
1064 pmflag = 0; /* set pmflag only in tirpcmode */
1065 inetdflag = 1; /* inetdflag is TRUE by default */
1066 if (cmd->nflag) { /* netid needs TIRPC */
1067 f_print(stderr, "Cannot use netid flag without TIRPC!\n");
1068 return (0);
1069 }
1070 }
1071
1072 if (newstyle && (tblflag || cmd->tflag)) {
1073 f_print(stderr, "Cannot use table flags with newstyle!\n");
1074 return (0);
1075 }
1076 /* check no conflicts with file generation flags */
1077 nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag +
1078 cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag + cmd->Scflag;
1079
1080 if (nflags == 0) {
1081 if (cmd->outfile != NULL || cmd->infile == NULL) {
1082 return (0);
1083 }
1084 } else
1085 if (nflags > 1) {
1086 f_print(stderr, "Cannot have more than one file generation flag!\n");
1087 return (0);
1088 }
1089 return (1);
1090 }
1091
1092 static void
1093 usage(void)
1094 {
1095 f_print(stderr, "usage: %s infile\n", cmdname);
1096 f_print(stderr, "\t%s [-AaBbILMNTv] [-Dname[=value]] [-i size] [-K seconds] [-Y pathname] infile\n",
1097 cmdname);
1098 f_print(stderr, "\t%s [-c | -h | -l | -m | -t | -Sc | -Ss] [-o outfile] [infile]\n",
1099 cmdname);
1100 f_print(stderr, "\t%s [-s nettype] [-o outfile] [infile]\n", cmdname);
1101 f_print(stderr, "\t%s [-n netid] [-o outfile] [infile]\n", cmdname);
1102 options_usage();
1103 exit(1);
1104 }
1105
1106 static void
1107 options_usage(void)
1108 {
1109 f_print(stderr, "options:\n");
1110 f_print(stderr, "-A\t\tgenerate svc_caller() function\n");
1111 f_print(stderr, "-a\t\tgenerate all files, including samples\n");
1112 f_print(stderr, "-B\t\tgenerate BSD c++ macros\n");
1113 f_print(stderr, "-b\t\tbackward compatibility mode (generates code for SunOS 4.1)\n");
1114 f_print(stderr, "-c\t\tgenerate XDR routines\n");
1115 f_print(stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n");
1116 f_print(stderr, "-h\t\tgenerate header file\n");
1117 f_print(stderr, "-I\t\tgenerate code for inetd support in server (for SunOS 4.1)\n");
1118 f_print(stderr, "-i size\t\tsize at which to start generating inline code\n");
1119 f_print(stderr, "-K seconds\tserver exits after K seconds of inactivity\n");
1120 f_print(stderr, "-L\t\tserver errors will be printed to syslog\n");
1121 f_print(stderr, "-l\t\tgenerate client side stubs\n");
1122 f_print(stderr, "-M\t\tgenerate thread-safe stubs\n");
1123 f_print(stderr, "-m\t\tgenerate server side stubs\n");
1124 f_print(stderr, "-N\t\tsupports multiple arguments and call-by-value\n");
1125 f_print(stderr, "-n netid\tgenerate server code that supports named netid\n");
1126 f_print(stderr, "-o outfile\tname of the output file\n");
1127 f_print(stderr, "-s nettype\tgenerate server code that supports named nettype\n");
1128 f_print(stderr, "-Sc\t\tgenerate sample client code that uses remote procedures\n");
1129 f_print(stderr, "-Ss\t\tgenerate sample server code that defines remote procedures\n");
1130 f_print(stderr, "-T\t\tgenerate code to support RPC dispatch tables\n");
1131 f_print(stderr, "-t\t\tgenerate RPC dispatch table\n");
1132 f_print(stderr, "-v\t\tdisplay version number\n");
1133 f_print(stderr, "-Y path\t\tdirectory name to find C preprocessor (cpp)\n");
1134
1135 exit(1);
1136 }
1137