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