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