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