]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/rpcgen/rpc_main.c
Teach rpcgen to generate .h files properly when the input filename contains
[FreeBSD/FreeBSD.git] / usr.bin / rpcgen / rpc_main.c
1 /*
2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3  * unrestricted use provided that this legend is included on all tape
4  * media and as a part of the software program in whole or part.  Users
5  * may copy or modify Sun RPC without charge, but are not authorized
6  * to license or distribute it to anyone else except as part of a product or
7  * program developed by the user.
8  *
9  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12  *
13  * Sun RPC is provided with no support and without any obligation on the
14  * part of Sun Microsystems, Inc. to assist in its use, correction,
15  * modification or enhancement.
16  *
17  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19  * OR ANY PART THEREOF.
20  *
21  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22  * or profits or other special, indirect and consequential damages, even if
23  * Sun has been advised of the possibility of such damages.
24  *
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  */
29
30
31 #ident  "@(#)rpc_main.c 1.21    94/04/25 SMI"
32
33 #if 0
34 #ifndef lint
35 static char sccsid[] = "@(#)rpc_main.c 1.30 89/03/30 (C) 1987 SMI";
36 #endif
37 #endif
38
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41
42 /*
43  * rpc_main.c, Top level of the RPC protocol compiler.
44  * Copyright (C) 1987, Sun Microsystems, Inc.
45  */
46
47 #include <err.h>
48 #include <ctype.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <unistd.h>
52 #include <sys/types.h>
53 #include <sys/param.h>
54 #include <sys/file.h>
55 #include <sys/stat.h>
56 #include "rpc_parse.h"
57 #include "rpc_util.h"
58 #include "rpc_scan.h"
59
60 extern void write_sample_svc( definition * );
61 extern int write_sample_clnt( definition * );
62 extern void write_sample_clnt_main( void );
63 extern void add_sample_msg( void );
64 static void c_output( char *, char *, int, char * );
65 static void h_output( char *, char *, int, char * );
66 static void l_output( char *, char *, int, char * );
67 static void t_output( char *, char *, int, char * );
68 static void clnt_output( char *, char *, int, char * );
69 void c_initialize( void );
70
71 #if !defined(__FreeBSD__) && !defined(__NetBSD__)
72 char * rindex();
73 #endif
74
75 static void usage(void);
76 static void options_usage(void);
77 static int do_registers(int, char **);
78 static int parseargs(int, char **, struct commandline *);
79 static void svc_output(char *, char *, int, char *);
80 static void mkfile_output(struct commandline *);
81 static void s_output(int, char **, char *, char *, int, char *, int, int);
82
83 #define EXTEND  1               /* alias for TRUE */
84 #define DONT_EXTEND     0               /* alias for FALSE */
85
86 #define SVR4_CPP "/usr/ccs/lib/cpp"
87 #define SUNOS_CPP "/usr/bin/cpp"
88
89 static int cppDefined = 0;      /* explicit path for C preprocessor */
90
91 static char *svcclosetime = "120";
92 static char *CPP = SVR4_CPP;
93 static char CPPFLAGS[] = "-C";
94 static char pathbuf[MAXPATHLEN + 1];
95 static char *allv[] = {
96         "rpcgen", "-s", "udp", "-s", "tcp",
97 };
98 static int allc = sizeof (allv)/sizeof (allv[0]);
99 static char *allnv[] = {
100         "rpcgen", "-s", "netpath",
101 };
102 static int allnc = sizeof (allnv)/sizeof (allnv[0]);
103
104 /*
105  * machinations for handling expanding argument list
106  */
107 static void addarg();           /* add another argument to the list */
108 static void putarg();           /* put argument at specified location  */
109 static void clear_args();       /* clear argument list */
110 static void checkfiles();       /* check if out file already exists */
111
112
113
114 #define ARGLISTLEN      20
115 #define FIXEDARGS       2
116
117 static char *arglist[ARGLISTLEN];
118 static int argcount = FIXEDARGS;
119
120
121 int nonfatalerrors;     /* errors */
122 int inetdflag = 0;      /* Support for inetd is disabled by default, use -I */
123 int pmflag = 0;         /* Support for port monitors is disabled by default */
124 int tirpc_socket = 1;   /* TI-RPC on socket, no TLI library */
125 int logflag;            /* Use syslog instead of fprintf for errors */
126 int tblflag;            /* Support for dispatch table file */
127 int mtflag = 0;         /* Support for MT */
128
129 #define INLINE 0
130 /* length at which to start doing an inline */
131
132 int inline = INLINE;
133 /*
134  * Length at which to start doing an inline. INLINE = default
135  * if 0, no xdr_inline code
136  */
137
138 int indefinitewait;     /* If started by port monitors, hang till it wants */
139 int exitnow;            /* If started by port monitors, exit after the call */
140 int timerflag;          /* TRUE if !indefinite && !exitnow */
141 int newstyle;           /* newstyle of passing arguments (by value) */
142 int Cflag = 0;          /* ANSI C syntax */
143 int CCflag = 0;         /* C++ files */
144 static int allfiles;   /* generate all files */
145 int tirpcflag = 1;    /* generating code for tirpc, by default */
146 xdrfunc *xdrfunc_head = NULL; /* xdr function list */
147 xdrfunc *xdrfunc_tail = NULL; /* xdr function list */
148 pid_t childpid;
149
150
151 int
152 main(argc, argv)
153         int argc;
154         char *argv[];
155 {
156         struct commandline cmd;
157
158         (void) memset((char *)&cmd, 0, sizeof (struct commandline));
159         clear_args();
160         if (!parseargs(argc, argv, &cmd))
161                 usage();
162         /*
163          * Only the client and server side stubs are likely to be customized,
164          *  so in that case only, check if the outfile exists, and if so,
165          *  print an error message and exit.
166          */
167         if (cmd.Ssflag || cmd.Scflag || cmd.makefileflag) {
168                 checkfiles(cmd.infile, cmd.outfile);
169         }
170         else
171                 checkfiles(cmd.infile, NULL);
172
173         if (cmd.cflag) {
174                 c_output(cmd.infile, "-DRPC_XDR", DONT_EXTEND, cmd.outfile);
175         } else if (cmd.hflag) {
176                 h_output(cmd.infile, "-DRPC_HDR", DONT_EXTEND, cmd.outfile);
177         } else if (cmd.lflag) {
178                 l_output(cmd.infile, "-DRPC_CLNT", DONT_EXTEND, cmd.outfile);
179         } else if (cmd.sflag || cmd.mflag || (cmd.nflag)) {
180                 s_output(argc, argv, cmd.infile, "-DRPC_SVC", DONT_EXTEND,
181                         cmd.outfile, cmd.mflag, cmd.nflag);
182         } else if (cmd.tflag) {
183                 t_output(cmd.infile, "-DRPC_TBL", DONT_EXTEND, cmd.outfile);
184         } else if  (cmd.Ssflag) {
185                 svc_output(cmd.infile, "-DRPC_SERVER", DONT_EXTEND,
186                         cmd.outfile);
187         } else if (cmd.Scflag) {
188                 clnt_output(cmd.infile, "-DRPC_CLIENT", DONT_EXTEND,
189                             cmd.outfile);
190         } else if (cmd.makefileflag) {
191                 mkfile_output(&cmd);
192         } else {
193                 /* the rescans are required, since cpp may effect input */
194                 c_output(cmd.infile, "-DRPC_XDR", EXTEND, "_xdr.c");
195                 reinitialize();
196                 h_output(cmd.infile, "-DRPC_HDR", EXTEND, ".h");
197                 reinitialize();
198                 l_output(cmd.infile, "-DRPC_CLNT", EXTEND, "_clnt.c");
199                 reinitialize();
200                 if (inetdflag || !tirpcflag)
201                         s_output(allc, allv, cmd.infile, "-DRPC_SVC", EXTEND,
202                         "_svc.c", cmd.mflag, cmd.nflag);
203                 else
204                         s_output(allnc, allnv, cmd.infile, "-DRPC_SVC",
205                                 EXTEND, "_svc.c", cmd.mflag, cmd.nflag);
206                 if (tblflag) {
207                         reinitialize();
208                         t_output(cmd.infile, "-DRPC_TBL", EXTEND, "_tbl.i");
209                 }
210
211                 if (allfiles) {
212                         reinitialize();
213                         svc_output(cmd.infile, "-DRPC_SERVER", EXTEND,
214                                 "_server.c");
215                         reinitialize();
216                         clnt_output(cmd.infile, "-DRPC_CLIENT", EXTEND,
217                                 "_client.c");
218
219                 }
220                 if (allfiles || (cmd.makefileflag == 1)){
221                         reinitialize();
222                         mkfile_output(&cmd);
223                 }
224
225         }
226         exit(nonfatalerrors);
227         /* NOTREACHED */
228 }
229
230
231 /*
232  * add extension to filename
233  */
234 static char *
235 extendfile(path, ext)
236         char *path;
237         char *ext;
238 {
239         char *res;
240         char *p;
241         char *file;
242
243         if ((file = rindex(path, '/')) == NULL)
244                 file = path;
245         else
246                 file++;
247         res = xmalloc(strlen(file) + strlen(ext) + 1);
248         p = strrchr(file, '.');
249         if (p == NULL) {
250                 p = file + strlen(file);
251         }
252         (void) strcpy(res, file);
253         (void) strcpy(res + (p - file), ext);
254         return (res);
255 }
256
257 /*
258  * Open output file with given extension
259  */
260 static void
261 open_output(infile, outfile)
262         char *infile;
263         char *outfile;
264 {
265
266         if (outfile == NULL) {
267                 fout = stdout;
268                 return;
269         }
270
271         if (infile != NULL && streq(outfile, infile)) {
272                 warnx("%s already exists. No output generated", infile);
273                 crash();
274         }
275         fout = fopen(outfile, "w");
276         if (fout == NULL) {
277                 warn("unable to open %s", outfile);
278                 crash();
279         }
280         record_open(outfile);
281
282         return;
283 }
284
285 static void
286 add_warning()
287 {
288         f_print(fout, "/*\n");
289         f_print(fout, " * Please do not edit this file.\n");
290         f_print(fout, " * It was generated using rpcgen.\n");
291         f_print(fout, " */\n\n");
292 }
293
294 /* clear list of arguments */
295 static void clear_args()
296 {
297         int i;
298         for (i = FIXEDARGS; i < ARGLISTLEN; i++)
299                 arglist[i] = NULL;
300         argcount = FIXEDARGS;
301 }
302
303 /* make sure that a CPP exists */
304 static void find_cpp()
305 {
306         struct stat buf;
307
308         if (stat(CPP, &buf) < 0)  { /* SVR4 or explicit cpp does not exist */
309                 if (cppDefined) {
310                         warnx("cannot find C preprocessor: %s", CPP);
311                         crash();
312                 } else {        /* try the other one */
313                         CPP = SUNOS_CPP;
314                         if (stat(CPP, &buf) < 0) { /* can't find any cpp */
315                                 warnx("cannot find C preprocessor: %s", CPP);
316                                 crash();
317                         }
318                 }
319         }
320 }
321
322 /*
323  * Open input file with given define for C-preprocessor
324  */
325 static void
326 open_input(infile, define)
327         char *infile;
328         char *define;
329 {
330         int pd[2];
331
332         infilename = (infile == NULL) ? "<stdin>" : infile;
333         (void) pipe(pd);
334         switch (childpid = fork()) {
335         case 0:
336                 find_cpp();
337                 putarg(0, CPP);
338                 putarg(1, CPPFLAGS);
339                 addarg(define);
340                 if (infile)
341                         addarg(infile);
342                 addarg((char *)NULL);
343                 (void) close(1);
344                 (void) dup2(pd[1], 1);
345                 (void) close(pd[0]);
346                 execv(arglist[0], arglist);
347                 err(1, "execv");
348         case -1:
349                 err(1, "fork");
350         }
351         (void) close(pd[1]);
352         fin = fdopen(pd[0], "r");
353         if (fin == NULL) {
354                 warn("%s", infilename);
355                 crash();
356         }
357 }
358
359 /* valid tirpc nettypes */
360 static char* valid_ti_nettypes[] =
361 {
362         "netpath",
363         "visible",
364         "circuit_v",
365         "datagram_v",
366         "circuit_n",
367         "datagram_n",
368         "udp",
369         "tcp",
370         "raw",
371         NULL
372         };
373
374 /* valid inetd nettypes */
375 static char* valid_i_nettypes[] =
376 {
377         "udp",
378         "tcp",
379         NULL
380         };
381
382 static int check_nettype(name, list_to_check)
383 char* name;
384 char* list_to_check[];
385 {
386         int i;
387         for (i = 0; list_to_check[i] != NULL; i++) {
388                 if (strcmp(name, list_to_check[i]) == 0) {
389                         return (1);
390                 }
391         }
392         warnx("illegal nettype :\'%s\'", name);
393         return (0);
394 }
395
396 static char *
397 file_name(file, ext)
398 char *file;
399 char *ext;
400 {
401         char *temp;
402         temp = extendfile(file, ext);
403
404         if (access(temp, F_OK) != -1)
405                 return (temp);
406         else
407                 return ((char *)" ");
408
409 }
410
411
412 static void
413 c_output(infile, define, extend, outfile)
414         char *infile;
415         char *define;
416         int extend;
417         char *outfile;
418 {
419         definition *def;
420         char *include;
421         char *outfilename;
422         long tell;
423
424         c_initialize();
425         open_input(infile, define);
426         outfilename = extend ? extendfile(infile, outfile) : outfile;
427         open_output(infile, outfilename);
428         add_warning();
429         if (infile && (include = extendfile(infile, ".h"))) {
430                 f_print(fout, "#include \"%s\"\n", include);
431                 free(include);
432                 /* .h file already contains rpc/rpc.h */
433         } else
434                 f_print(fout, "#include <rpc/rpc.h>\n");
435         tell = ftell(fout);
436         while ( (def = get_definition()) ) {
437                 emit(def);
438         }
439         if (extend && tell == ftell(fout)) {
440                 (void) unlink(outfilename);
441         }
442 }
443
444
445 void
446 c_initialize()
447 {
448
449         /* add all the starting basic types */
450         add_type(1, "int");
451         add_type(1, "long");
452         add_type(1, "short");
453         add_type(1, "bool");
454         add_type(1, "u_int");
455         add_type(1, "u_long");
456         add_type(1, "u_short");
457
458 }
459
460 char rpcgen_table_dcl[] = "struct rpcgen_table {\n\
461         char    *(*proc)(); \n\
462         xdrproc_t       xdr_arg; \n\
463         unsigned        len_arg; \n\
464         xdrproc_t       xdr_res; \n\
465         unsigned        len_res; \n\
466 }; \n";
467
468
469 char *generate_guard(pathname)
470         char* pathname;
471 {
472         char* filename, *guard, *tmp;
473
474         filename = strrchr(pathname, '/');  /* find last component */
475         filename = ((filename == 0) ? pathname : filename+1);
476         guard = xstrdup(filename);
477         /* convert to a valid C macro name, and upper case
478          * =~ m,[A-Za-z_][A-Za-z_0-9]*,   else map other chars to '_'.
479          */
480         for (tmp = guard; '\000' != *tmp; ++tmp) {
481                 if (islower(*tmp))
482                         *tmp = toupper(*tmp);
483                 else if (isupper(*tmp) || '_' == *tmp)
484                         /* OK for C */;
485                 else if (tmp == guard)
486                         *tmp = '_';
487                 else if (isdigit(*tmp))
488                         /* OK for all but first character */;
489                 else if ('.' == *tmp) {
490                         *tmp = '\000';
491                         break;
492                 } else
493                         *tmp = '_';
494         }
495         /* When the filename started with "." (wow, the is Poor Form)
496          * lets put in the word "DOT" so we don't violate ANSI's reservation
497          * of macros that start with "_" -- rpc at ksb.npcguild.org
498          */
499         if ('\000' == *guard) {
500                 guard = "DOT";
501         }
502         guard = extendfile(guard, "_H_RPCGEN");
503         return (guard);
504 }
505
506 /*
507  * Compile into an XDR header file
508  */
509
510
511 static void
512 h_output(infile, define, extend, outfile)
513         char *infile;
514         char *define;
515         int extend;
516         char *outfile;
517 {
518         definition *def;
519         char *outfilename;
520         long tell;
521         char *guard;
522         list *l;
523         xdrfunc *xdrfuncp;
524         int i;
525
526         open_input(infile, define);
527         outfilename =  extend ? extendfile(infile, outfile) : outfile;
528         open_output(infile, outfilename);
529         add_warning();
530         if (outfilename || infile){
531                 guard = generate_guard(outfilename ? outfilename: infile);
532         } else
533                 guard = "STDIN_";
534
535         f_print(fout, "#ifndef _%s\n#define     _%s\n\n", guard,
536                 guard);
537
538         f_print(fout, "#include <rpc/rpc.h>\n");
539
540         if (mtflag)
541                 f_print(fout, "#include <pthread.h>\n");
542
543         /* put the C++ support */
544         if (Cflag && !CCflag){
545                 f_print(fout, "\n#ifdef __cplusplus\n");
546                 f_print(fout, "extern \"C\" {\n");
547                 f_print(fout, "#endif\n\n");
548         }
549
550         /* put in a typedef for quadprecision. Only with Cflag */
551
552         tell = ftell(fout);
553
554         /* print data definitions */
555         while ( (def = get_definition()) ) {
556                 print_datadef(def);
557         }
558
559         /*
560          * print function declarations.
561          *  Do this after data definitions because they might be used as
562          *  arguments for functions
563          */
564         for (l = defined; l != NULL; l = l->next) {
565                 print_funcdef(l->val);
566         }
567         /* Now  print all xdr func declarations */
568         if (xdrfunc_head != NULL){
569
570                 f_print(fout,
571                         "\n/* the xdr functions */\n");
572
573                 if (CCflag){
574                 f_print(fout, "\n#ifdef __cplusplus\n");
575                 f_print(fout, "extern \"C\" {\n");
576                 f_print(fout, "#endif\n");
577         }
578
579                 if (!Cflag){
580                         xdrfuncp = xdrfunc_head;
581                         while (xdrfuncp != NULL){
582                                 print_xdr_func_def(xdrfuncp->name,
583                                 xdrfuncp->pointerp, 2);
584                                 xdrfuncp = xdrfuncp->next;
585                         }
586                 } else {
587
588                         for (i = 1; i < 3; i++){
589                                 if (i == 1)
590         f_print(fout, "\n#if defined(__STDC__) || defined(__cplusplus)\n");
591
592                                 else
593                                         f_print(fout, "\n#else /* K&R C */\n");
594
595                                 xdrfuncp = xdrfunc_head;
596                                 while (xdrfuncp != NULL){
597                                         print_xdr_func_def(xdrfuncp->name,
598         xdrfuncp->pointerp, i);
599                                         xdrfuncp = xdrfuncp->next;
600                                 }
601                         }
602                 f_print(fout, "\n#endif /* K&R C */\n");
603                 }
604         }
605
606         if (extend && tell == ftell(fout)) {
607                 (void) unlink(outfilename);
608         } else if (tblflag) {
609                 f_print(fout, rpcgen_table_dcl);
610         }
611
612         if (Cflag){
613                 f_print(fout, "\n#ifdef __cplusplus\n");
614                 f_print(fout, "}\n");
615                 f_print(fout, "#endif\n");
616         }
617
618         f_print(fout, "\n#endif /* !_%s */\n", guard);
619 }
620
621 /*
622  * Compile into an RPC service
623  */
624 static void
625 s_output(argc, argv, infile, define, extend, outfile, nomain, netflag)
626         int argc;
627         char *argv[];
628         char *infile;
629         char *define;
630         int extend;
631         char *outfile;
632         int nomain;
633         int netflag;
634 {
635         char *include;
636         definition *def;
637         int foundprogram = 0;
638         char *outfilename;
639
640         open_input(infile, define);
641         outfilename = extend ? extendfile(infile, outfile) : outfile;
642         open_output(infile, outfilename);
643         add_warning();
644         if (infile && (include = extendfile(infile, ".h"))) {
645                 f_print(fout, "#include \"%s\"\n", include);
646                 free(include);
647         } else
648                 f_print(fout, "#include <rpc/rpc.h>\n");
649
650         f_print(fout, "#include <stdio.h>\n");
651         f_print(fout, "#include <stdlib.h> /* getenv, exit */\n");
652         if (Cflag) {
653                 f_print (fout,
654                 "#include <rpc/pmap_clnt.h> /* for pmap_unset */\n");
655                 f_print (fout, "#include <string.h> /* strcmp */\n");
656         }
657         if (tirpcflag)
658                 f_print(fout, "#include <rpc/rpc_com.h>\n");
659         if (strcmp(svcclosetime, "-1") == 0)
660                 indefinitewait = 1;
661         else if (strcmp(svcclosetime, "0") == 0)
662                 exitnow = 1;
663         else if (inetdflag || pmflag) {
664                 f_print(fout, "#include <signal.h>\n");
665                 timerflag = 1;
666         }
667
668         if (!tirpcflag && inetdflag)
669                 f_print(fout, "#include <sys/ttycom.h> /* TIOCNOTTY */\n");
670         if (Cflag && (inetdflag || pmflag)) {
671                 f_print(fout, "#ifdef __cplusplus\n");
672                 f_print(fout,
673                         "#include <sysent.h> /* getdtablesize, open */\n");
674                 f_print(fout, "#endif /* __cplusplus */\n");
675         }
676         if (tirpcflag) {
677                 f_print(fout, "#include <fcntl.h> /* open */\n");
678                 f_print(fout, "#include <unistd.h> /* fork / setsid */\n");
679                 f_print(fout, "#include <sys/types.h>\n");
680         }
681
682         f_print(fout, "#include <memory.h>\n");
683         if (inetdflag || !tirpcflag) {
684                 f_print(fout, "#include <sys/socket.h>\n");
685                 f_print(fout, "#include <netinet/in.h>\n");
686         }
687
688         if ((netflag || pmflag) && tirpcflag && !nomain) {
689                 f_print(fout, "#include <netconfig.h>\n");
690         }
691         if (tirpcflag)
692                 f_print(fout, "#include <sys/resource.h> /* rlimit */\n");
693         if (logflag || inetdflag || pmflag || tirpcflag)
694                 f_print(fout, "#include <syslog.h>\n");
695
696         /* for ANSI-C */
697         if (Cflag)
698                 f_print(fout,
699                         "\n#ifndef SIG_PF\n#define      SIG_PF void(*)\
700 (int)\n#endif\n");
701
702         f_print(fout, "\n#ifdef DEBUG\n#define  RPC_SVC_FG\n#endif\n");
703         if (timerflag)
704                 f_print(fout, "\n#define        _RPCSVC_CLOSEDOWN %s\n",
705                         svcclosetime);
706         while ( (def = get_definition()) ) {
707                 foundprogram |= (def->def_kind == DEF_PROGRAM);
708         }
709         if (extend && !foundprogram) {
710                 (void) unlink(outfilename);
711                 return;
712         }
713         write_most(infile, netflag, nomain);
714         if (!nomain) {
715                 if (!do_registers(argc, argv)) {
716                         if (outfilename)
717                                 (void) unlink(outfilename);
718                         usage();
719                 }
720                 write_rest();
721         }
722 }
723
724 /*
725  * generate client side stubs
726  */
727 static void
728 l_output(infile, define, extend, outfile)
729         char *infile;
730         char *define;
731         int extend;
732         char *outfile;
733 {
734         char *include;
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         if (Cflag)
744                 f_print (fout, "#include <memory.h> /* for memset */\n");
745         if (infile && (include = extendfile(infile, ".h"))) {
746                 f_print(fout, "#include \"%s\"\n", include);
747                 free(include);
748         } else
749                 f_print(fout, "#include <rpc/rpc.h>\n");
750         while ( (def = get_definition()) ) {
751                 foundprogram |= (def->def_kind == DEF_PROGRAM);
752         }
753         if (extend && !foundprogram) {
754                 (void) unlink(outfilename);
755                 return;
756         }
757         write_stubs();
758 }
759
760 /*
761  * generate the dispatch table
762  */
763 static void
764 t_output(infile, define, extend, outfile)
765         char *infile;
766         char *define;
767         int extend;
768         char *outfile;
769 {
770         definition *def;
771         int foundprogram = 0;
772         char *outfilename;
773
774         open_input(infile, define);
775         outfilename = extend ? extendfile(infile, outfile) : outfile;
776         open_output(infile, outfilename);
777         add_warning();
778         while ( (def = get_definition()) ) {
779                 foundprogram |= (def->def_kind == DEF_PROGRAM);
780         }
781         if (extend && !foundprogram) {
782                 (void) unlink(outfilename);
783                 return;
784         }
785         write_tables();
786 }
787
788 /* sample routine for the server template */
789 static void
790 svc_output(infile, define, extend, outfile)
791         char *infile;
792         char *define;
793         int extend;
794         char *outfile;
795 {
796         definition *def;
797         char *include;
798         char *outfilename;
799         long tell;
800         open_input(infile, define);
801         outfilename = extend ? extendfile(infile, outfile) : outfile;
802         checkfiles(infile, outfilename);
803         /*
804          * Check if outfile already exists.
805          * if so, print an error message and exit
806          */
807         open_output(infile, outfilename);
808         add_sample_msg();
809
810         if (infile && (include = extendfile(infile, ".h"))) {
811                 f_print(fout, "#include \"%s\"\n", include);
812                 free(include);
813         } else
814                 f_print(fout, "#include <rpc/rpc.h>\n");
815
816         tell = ftell(fout);
817         while ( (def = get_definition()) ) {
818                 write_sample_svc(def);
819         }
820         if (extend && tell == ftell(fout)) {
821                 (void) unlink(outfilename);
822         }
823 }
824
825 /* sample main routine for client */
826 static void
827 clnt_output(infile, define, extend, outfile)
828         char *infile;
829         char *define;
830         int extend;
831         char *outfile;
832 {
833         definition *def;
834         char *include;
835         char *outfilename;
836         long tell;
837         int has_program = 0;
838
839         open_input(infile, define);
840         outfilename = extend ? extendfile(infile, outfile) : outfile;
841         checkfiles(infile, outfilename);
842         /*
843          * Check if outfile already exists.
844          * if so, print an error message and exit
845          */
846
847         open_output(infile, outfilename);
848         add_sample_msg();
849         if (infile && (include = extendfile(infile, ".h"))) {
850                 f_print(fout, "#include \"%s\"\n", include);
851                 free(include);
852         } else
853                 f_print(fout, "#include <rpc/rpc.h>\n");
854         tell = ftell(fout);
855         while ( (def = get_definition()) ) {
856                 has_program += write_sample_clnt(def);
857         }
858
859         if (has_program)
860                 write_sample_clnt_main();
861
862         if (extend && tell == ftell(fout)) {
863                 (void) unlink(outfilename);
864         }
865 }
866
867
868 static void mkfile_output(cmd)
869 struct commandline *cmd;
870 {
871         char *mkfilename, *clientname, *clntname, *xdrname, *hdrname;
872         char *servername, *svcname, *servprogname, *clntprogname;
873         char *temp;
874
875         svcname = file_name(cmd->infile, "_svc.c");
876         clntname = file_name(cmd->infile, "_clnt.c");
877         xdrname = file_name(cmd->infile, "_xdr.c");
878         hdrname = file_name(cmd->infile, ".h");
879
880
881         if (allfiles){
882                 servername = extendfile(cmd->infile, "_server.c");
883                 clientname = extendfile(cmd->infile, "_client.c");
884         }else{
885                 servername = " ";
886                 clientname = " ";
887         }
888         servprogname = extendfile(cmd->infile, "_server");
889         clntprogname = extendfile(cmd->infile, "_client");
890
891         if (allfiles){
892                 mkfilename = xmalloc(strlen("makefile.") +
893                                      strlen(cmd->infile) + 1);
894                 temp = (char *)rindex(cmd->infile, '.');
895                 strcpy(mkfilename, "makefile.");
896                 (void) strncat(mkfilename, cmd->infile,
897                         (temp - cmd->infile));
898         } else
899                 mkfilename = cmd->outfile;
900
901
902         checkfiles(NULL, mkfilename);
903         open_output(NULL, mkfilename);
904
905         f_print(fout, "\n# This is a template makefile generated\
906                 by rpcgen \n");
907
908         f_print(fout, "\n# Parameters \n\n");
909
910         f_print(fout, "CLIENT = %s\nSERVER = %s\n\n",
911                 clntprogname, servprogname);
912         f_print(fout, "SOURCES_CLNT.c = \nSOURCES_CLNT.h = \n");
913         f_print(fout, "SOURCES_SVC.c = \nSOURCES_SVC.h = \n");
914         f_print(fout, "SOURCES.x = %s\n\n", cmd->infile);
915         f_print(fout, "TARGETS_SVC.c = %s %s %s \n",
916                 svcname, servername, xdrname);
917         f_print(fout, "TARGETS_CLNT.c = %s %s %s \n",
918                 clntname, clientname, xdrname);
919         f_print(fout, "TARGETS = %s %s %s %s %s %s\n\n",
920                 hdrname, xdrname, clntname,
921                 svcname, clientname, servername);
922
923         f_print(fout, "OBJECTS_CLNT = $(SOURCES_CLNT.c:%%.c=%%.o) \
924 $(TARGETS_CLNT.c:%%.c=%%.o) ");
925
926         f_print(fout, "\nOBJECTS_SVC = $(SOURCES_SVC.c:%%.c=%%.o) \
927 $(TARGETS_SVC.c:%%.c=%%.o) ");
928
929
930         f_print(fout, "\n# Compiler flags \n");
931         if (mtflag)
932                 f_print(fout, "\nCFLAGS += -D_REENTRANT -D_THEAD_SAFE \nLDLIBS += -pthread\n");
933
934         f_print(fout, "RPCGENFLAGS = \n");
935
936         f_print(fout, "\n# Targets \n\n");
937
938         f_print(fout, "all : $(CLIENT) $(SERVER)\n\n");
939         f_print(fout, "$(TARGETS) : $(SOURCES.x) \n");
940         f_print(fout, "\trpcgen $(RPCGENFLAGS) $(SOURCES.x)\n\n");
941         f_print(fout, "$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) \
942 $(TARGETS_CLNT.c) \n\n");
943
944         f_print(fout, "$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) \
945 $(TARGETS_SVC.c) \n\n");
946         f_print(fout, "$(CLIENT) : $(OBJECTS_CLNT) \n");
947         f_print(fout, "\t$(CC) -o $(CLIENT) $(OBJECTS_CLNT) \
948 $(LDLIBS) \n\n");
949         f_print(fout, "$(SERVER) : $(OBJECTS_SVC) \n");
950         f_print(fout, "\t$(CC) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)\n\n ");
951         f_print(fout, "clean:\n\t $(RM) -f core $(TARGETS) $(OBJECTS_CLNT) \
952 $(OBJECTS_SVC) $(CLIENT) $(SERVER)\n\n");
953 }
954
955
956
957 /*
958  * Perform registrations for service output
959  * Return 0 if failed; 1 otherwise.
960  */
961 static int
962 do_registers(argc, argv)
963         int argc;
964         char *argv[];
965 {
966         int i;
967
968         if (inetdflag || !tirpcflag) {
969                 for (i = 1; i < argc; i++) {
970                         if (streq(argv[i], "-s")) {
971                                 if (!check_nettype(argv[i + 1],
972                                                     valid_i_nettypes))
973                                         return (0);
974                                 write_inetd_register(argv[i + 1]);
975                                 i++;
976                         }
977                 }
978         } else {
979                 for (i = 1; i < argc; i++)
980                         if (streq(argv[i], "-s")) {
981                                 if (!check_nettype(argv[i + 1],
982                                                     valid_ti_nettypes))
983                                         return (0);
984                                 write_nettype_register(argv[i + 1]);
985                                 i++;
986                         } else if (streq(argv[i], "-n")) {
987                                 write_netid_register(argv[i + 1]);
988                                 i++;
989                         }
990         }
991         return (1);
992 }
993
994 /*
995  * Add another argument to the arg list
996  */
997 static void
998 addarg(cp)
999         char *cp;
1000 {
1001         if (argcount >= ARGLISTLEN) {
1002                 warnx("too many defines");
1003                 crash();
1004                 /*NOTREACHED*/
1005         }
1006         arglist[argcount++] = cp;
1007
1008 }
1009
1010 static void
1011 putarg(where, cp)
1012         char *cp;
1013         int where;
1014 {
1015         if (where >= ARGLISTLEN) {
1016                 warnx("arglist coding error");
1017                 crash();
1018                 /*NOTREACHED*/
1019         }
1020         arglist[where] = cp;
1021 }
1022
1023 /*
1024  * if input file is stdin and an output file is specified then complain
1025  * if the file already exists. Otherwise the file may get overwritten
1026  * If input file does not exist, exit with an error
1027  */
1028
1029 static void
1030 checkfiles(infile, outfile)
1031 char *infile;
1032 char *outfile;
1033 {
1034
1035         struct stat buf;
1036
1037         if (infile)             /* infile ! = NULL */
1038                 if (stat(infile, &buf) < 0)
1039                 {
1040                         warn("%s", infile);
1041                         crash();
1042                 };
1043         if (outfile) {
1044                 if (stat(outfile, &buf) < 0)
1045                         return; /* file does not exist */
1046                 else {
1047                         warnx("file '%s' already exists and may be overwritten", outfile);
1048                         crash();
1049                 }
1050         }
1051 }
1052
1053 /*
1054  * Parse command line arguments
1055  */
1056 static int
1057 parseargs(argc, argv, cmd)
1058         int argc;
1059         char *argv[];
1060         struct commandline *cmd;
1061 {
1062         int i;
1063         int j;
1064         char c, ch;
1065         char flag[(1 << 8 * sizeof (char))];
1066         int nflags;
1067
1068         cmd->infile = cmd->outfile = NULL;
1069         if (argc < 2) {
1070                 return (0);
1071         }
1072         allfiles = 0;
1073         flag['c'] = 0;
1074         flag['h'] = 0;
1075         flag['l'] = 0;
1076         flag['m'] = 0;
1077         flag['o'] = 0;
1078         flag['s'] = 0;
1079         flag['n'] = 0;
1080         flag['t'] = 0;
1081         flag['S'] = 0;
1082         flag['C'] = 0;
1083         flag['M'] = 0;
1084
1085         for (i = 1; i < argc; i++) {
1086                 if (argv[i][0] != '-') {
1087                         if (cmd->infile) {
1088                                 warnx("cannot specify more than one input file");
1089                                 return (0);
1090                         }
1091                         cmd->infile = argv[i];
1092                 } else {
1093                         for (j = 1; argv[i][j] != 0; j++) {
1094                                 c = argv[i][j];
1095                                 switch (c) {
1096                                 case 'a':
1097                                         allfiles = 1;
1098                                         break;
1099                                 case 'c':
1100                                 case 'h':
1101                                 case 'l':
1102                                 case 'm':
1103                                 case 't':
1104                                         if (flag[(int)c]) {
1105                                                 return (0);
1106                                         }
1107                                         flag[(int)c] = 1;
1108                                         break;
1109                                 case 'S':
1110                                         /*
1111                                          * sample flag: Ss or Sc.
1112                                          *  Ss means set flag['S'];
1113                                          *  Sc means set flag['C'];
1114                                          *  Sm means set flag['M'];
1115                                          */
1116                                         ch = argv[i][++j]; /* get next char */
1117                                         if (ch == 's')
1118                                                 ch = 'S';
1119                                         else if (ch == 'c')
1120                                                 ch = 'C';
1121                                         else if (ch == 'm')
1122                                                 ch = 'M';
1123                                         else
1124                                                 return (0);
1125
1126                                         if (flag[(int)ch]) {
1127                                                 return (0);
1128                                         }
1129                                         flag[(int)ch] = 1;
1130                                         break;
1131                                 case 'C': /* ANSI C syntax */
1132                                         Cflag = 1;
1133                                         ch = argv[i][j+1]; /* get next char */
1134
1135                                         if (ch != 'C')
1136                                                 break;
1137                                         CCflag = 1;
1138                                         break;
1139                                 case 'b':
1140                                         /*
1141                                          *  Turn TIRPC flag off for
1142                                          *  generating backward compatible
1143                                          *  code
1144                                          */
1145                                         tirpcflag = 0;
1146                                         break;
1147
1148                                 case 'I':
1149                                         inetdflag = 1;
1150                                         break;
1151                                 case 'N':
1152                                         newstyle = 1;
1153                                         break;
1154                                 case 'L':
1155                                         logflag = 1;
1156                                         break;
1157                                 case 'P':
1158                                         pmflag = 1;
1159                                         break;
1160                                 case 'K':
1161                                         if (++i == argc) {
1162                                                 return (0);
1163                                         }
1164                                         svcclosetime = argv[i];
1165                                         goto nextarg;
1166                                 case 'T':
1167                                         tblflag = 1;
1168                                         break;
1169                                 case 'M':
1170                                         mtflag = 1;
1171                                         break;
1172                                 case 'i' :
1173                                         if (++i == argc) {
1174                                                 return (0);
1175                                         }
1176                                         inline = atoi(argv[i]);
1177                                         goto nextarg;
1178                                 case 'n':
1179                                 case 'o':
1180                                 case 's':
1181                                         if (argv[i][j - 1] != '-' ||
1182                                             argv[i][j + 1] != 0) {
1183                                                 return (0);
1184                                         }
1185                                         flag[(int)c] = 1;
1186                                         if (++i == argc) {
1187                                                 return (0);
1188                                         }
1189                                         if (c == 'o') {
1190                                                 if (cmd->outfile) {
1191                                                         return (0);
1192                                                 }
1193                                                 cmd->outfile = argv[i];
1194                                         }
1195                                         goto nextarg;
1196                                 case 'D':
1197                                         if (argv[i][j - 1] != '-') {
1198                                                 return (0);
1199                                         }
1200                                         (void) addarg(argv[i]);
1201                                         goto nextarg;
1202                                 case 'Y':
1203                                         if (++i == argc) {
1204                                                 return (0);
1205                                         }
1206                                         (void) strlcpy(pathbuf, argv[i], sizeof(pathbuf));
1207                                         if (strlcat(pathbuf, "/cpp", sizeof(pathbuf))
1208                                             >= sizeof(pathbuf)) {
1209                                                 warnx("argument too long");
1210                                                 return (0);
1211                                         }
1212                                         CPP = pathbuf;
1213                                         cppDefined = 1;
1214                                         goto nextarg;
1215
1216
1217
1218                                 default:
1219                                         return (0);
1220                                 }
1221                         }
1222                 nextarg:
1223                         ;
1224                 }
1225         }
1226
1227         cmd->cflag = flag['c'];
1228         cmd->hflag = flag['h'];
1229         cmd->lflag = flag['l'];
1230         cmd->mflag = flag['m'];
1231         cmd->nflag = flag['n'];
1232         cmd->sflag = flag['s'];
1233         cmd->tflag = flag['t'];
1234         cmd->Ssflag = flag['S'];
1235         cmd->Scflag = flag['C'];
1236         cmd->makefileflag = flag['M'];
1237
1238         if (tirpcflag) {
1239                 if (inetdflag)
1240                         pmflag = 0;
1241                 if ((inetdflag && cmd->nflag)) {
1242                         /* netid not allowed with inetdflag */
1243                         warnx("cannot use netid flag with inetd flag");
1244                         return (0);
1245                 }
1246         } else {                /* 4.1 mode */
1247                 pmflag = 0;     /* set pmflag only in tirpcmode */
1248                 if (cmd->nflag) { /* netid needs TIRPC */
1249                         warnx("cannot use netid flag without TIRPC");
1250                         return (0);
1251                 }
1252         }
1253
1254         if (newstyle && (tblflag || cmd->tflag)) {
1255                 warnx("cannot use table flags with newstyle");
1256                 return (0);
1257         }
1258
1259         /* check no conflicts with file generation flags */
1260         nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag +
1261                 cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag +
1262                         cmd->Scflag + cmd->makefileflag;
1263
1264         if (nflags == 0) {
1265                 if (cmd->outfile != NULL || cmd->infile == NULL) {
1266                         return (0);
1267                 }
1268         } else if (cmd->infile == NULL &&
1269             (cmd->Ssflag || cmd->Scflag || cmd->makefileflag)) {
1270                 warnx("\"infile\" is required for template generation flags");
1271                 return (0);
1272         } if (nflags > 1) {
1273                 warnx("cannot have more than one file generation flag");
1274                 return (0);
1275         }
1276         return (1);
1277 }
1278
1279 static void
1280 usage()
1281 {
1282         f_print(stderr, "%s\n%s\n%s\n%s\n%s\n",
1283                 "usage: rpcgen infile",
1284                 "       rpcgen [-abCLNTM] [-Dname[=value]] [-i size]\
1285 [-I -P [-K seconds]] [-Y path] infile",
1286                 "       rpcgen [-c | -h | -l | -m | -t | -Sc | -Ss | -Sm]\
1287 [-o outfile] [infile]",
1288                 "       rpcgen [-s nettype]* [-o outfile] [infile]",
1289                 "       rpcgen [-n netid]* [-o outfile] [infile]");
1290         options_usage();
1291         exit(1);
1292 }
1293
1294 static void
1295 options_usage()
1296 {
1297         f_print(stderr, "options:\n");
1298         f_print(stderr, "-a\t\tgenerate all files, including samples\n");
1299         f_print(stderr, "-b\t\tbackward compatibility mode (generates code \
1300 for FreeBSD 4.X)\n");
1301         f_print(stderr, "-c\t\tgenerate XDR routines\n");
1302         f_print(stderr, "-C\t\tANSI C mode\n");
1303         f_print(stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n");
1304         f_print(stderr, "-h\t\tgenerate header file\n");
1305         f_print(stderr, "-i size\t\tsize at which to start generating\
1306 inline code\n");
1307         f_print(stderr, "-I\t\tgenerate code for inetd support in server\n");
1308         f_print(stderr, "-K seconds\tserver exits after K seconds of\
1309 inactivity\n");
1310         f_print(stderr, "-l\t\tgenerate client side stubs\n");
1311         f_print(stderr, "-L\t\tserver errors will be printed to syslog\n");
1312         f_print(stderr, "-m\t\tgenerate server side stubs\n");
1313         f_print(stderr, "-M\t\tgenerate MT-safe code\n");
1314         f_print(stderr, "-n netid\tgenerate server code that supports\
1315 named netid\n");
1316         f_print(stderr, "-N\t\tsupports multiple arguments and\
1317 call-by-value\n");
1318         f_print(stderr, "-o outfile\tname of the output file\n");
1319         f_print(stderr, "-P\t\tgenerate code for port monitoring support in server\n");
1320         f_print(stderr, "-s nettype\tgenerate server code that supports named\
1321 nettype\n");
1322         f_print(stderr, "-Sc\t\tgenerate sample client code that uses remote\
1323 procedures\n");
1324         f_print(stderr, "-Ss\t\tgenerate sample server code that defines\
1325 remote procedures\n");
1326         f_print(stderr, "-Sm \t\tgenerate makefile template \n");
1327
1328         f_print(stderr, "-t\t\tgenerate RPC dispatch table\n");
1329         f_print(stderr, "-T\t\tgenerate code to support RPC dispatch tables\n");
1330         f_print(stderr, "-Y path\t\tpath where cpp is found\n");
1331         exit(1);
1332 }