rpcgen: include sys/ioctl.h on linux systems
[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 #else
552         if( !tirpcflag )
553           f_print(fout, "#include <sys/ttycom.h>/* TIOCNOTTY */\n");
554 #endif
555         if( Cflag && (inetdflag || pmflag ) ) {
556           f_print(fout, "#ifdef __cplusplus\n");
557           f_print(fout, "#include <sysent.h> /* getdtablesize, open */\n"); 
558           f_print(fout, "#endif /* __cplusplus */\n");
559           
560           if( tirpcflag )
561             f_print(fout, "#include <unistd.h> /* setsid */\n");
562         }
563         if( tirpcflag )
564           f_print(fout, "#include <sys/types.h>\n");
565
566         f_print(fout, "#include <memory.h>\n");
567 #ifndef linux
568         f_print(fout, "#include <stropts.h>\n");
569 #endif
570         if (inetdflag || !tirpcflag ) {
571                 f_print(fout, "#include <sys/socket.h>\n");
572                 f_print(fout, "#include <netinet/in.h>\n");
573         } 
574
575         if ( (netflag || pmflag) && tirpcflag ) {
576                 f_print(fout, "#include <netconfig.h>\n");
577         }
578         if (/*timerflag &&*/ tirpcflag)
579                 f_print(fout, "#include <sys/resource.h> /* rlimit */\n");
580         if (logflag || inetdflag || pmflag) {
581 #ifdef linux
582                 f_print(fout, "#include <syslog.h>\n");
583 #else
584                 f_print(fout, "#ifdef SYSLOG\n");
585                 f_print(fout, "#include <syslog.h>\n");
586                 f_print(fout, "#else\n");
587                 f_print(fout, "#define LOG_ERR 1\n");
588                 f_print(fout, "#define openlog(a, b, c)\n");
589                 f_print(fout, "#endif\n");
590 #endif
591         }
592
593         /* for ANSI-C */
594         f_print(fout, "\n#ifdef __STDC__\n#define SIG_PF void(*)(int)\n#endif\n");
595
596         f_print(fout, "\n#ifdef DEBUG\n#define RPC_SVC_FG\n#endif\n");
597         if (timerflag)
598                 f_print(fout, "\n#define _RPCSVC_CLOSEDOWN %s\n", svcclosetime);
599         while ((def = get_definition()) != NULL) {
600                 foundprogram |= (def->def_kind == DEF_PROGRAM);
601         }
602         if (extend && !foundprogram) {
603                 (void) unlink(outfilename);
604                 return;
605         }
606         write_most(infile, netflag, nomain);
607         if (!nomain) {
608                 if( !do_registers(argc, argv) ) {
609                   if (outfilename)
610                     (void) unlink(outfilename);
611                   usage();
612                 }
613                 write_rest();
614         }
615 }
616
617 /*
618  * generate client side stubs
619  */
620 static void
621 l_output(char *infile, char *define, int extend, char *outfile)
622 {
623         char *include;
624         definition *def;
625         int foundprogram = 0;
626         char *outfilename;
627
628         open_input(infile, define);
629         outfilename = extend ? extendfile(infile, outfile) : outfile;
630         open_output(infile, outfilename);
631         add_warning();
632         if (Cflag)
633           f_print (fout, "#include <memory.h> /* for memset */\n");
634         if (infile && (include = extendfile(infile, ".h"))) {
635                 f_print(fout, "#include \"%s\"\n", include);
636                 free(include);
637         } else
638           f_print(fout, "#include <rpc/rpc.h>\n");
639         while ((def = get_definition()) != NULL) {
640                 foundprogram |= (def->def_kind == DEF_PROGRAM);
641         }
642         if (extend && !foundprogram) {
643                 (void) unlink(outfilename);
644                 return;
645         }
646         write_stubs();
647 }
648
649 /*
650  * generate the dispatch table
651  */
652 static void
653 t_output(char *infile, char *define, int extend, char *outfile)
654 {
655         definition *def;
656         int foundprogram = 0;
657         char *outfilename;
658
659         open_input(infile, define);
660         outfilename = extend ? extendfile(infile, outfile) : outfile;
661         open_output(infile, outfilename);
662         add_warning();
663         while ((def = get_definition()) != NULL) {
664                 foundprogram |= (def->def_kind == DEF_PROGRAM);
665         }
666         if (extend && !foundprogram) {
667                 (void) unlink(outfilename);
668                 return;
669         }
670         write_tables();
671 }
672
673 /* sample routine for the server template */
674 static  void
675 svc_output(char *infile, char *define, int extend, char *outfile)
676 {
677   definition *def;
678   char *include;
679   char *outfilename;
680   long tell;
681   
682   open_input(infile, define);   
683   outfilename = extend ? extendfile(infile, outfile) : outfile;
684   checkfiles(infile,outfilename); /*check if outfile already exists.
685                                   if so, print an error message and exit*/
686   open_output(infile, outfilename);
687   add_sample_msg();
688
689   if (infile && (include = extendfile(infile, ".h"))) {
690     f_print(fout, "#include \"%s\"\n", include);
691     free(include);
692   } else
693     f_print(fout, "#include <rpc/rpc.h>\n");
694
695   tell = ftell(fout);
696   while ((def = get_definition()) != NULL) {
697           write_sample_svc(def);
698   }
699   if (extend && tell == ftell(fout)) {
700           (void) unlink(outfilename);
701   }
702 }
703
704
705 /* sample main routine for client */
706 static  void
707 clnt_output(char *infile, char *define, int extend, char *outfile)
708 {
709         definition     *def;
710         char           *include;
711         char           *outfilename;
712         long            tell;
713         int             has_program = 0;
714
715         open_input(infile, define);
716         outfilename = extend ? extendfile(infile, outfile) : outfile;
717         checkfiles(infile, outfilename);        /*check if outfile already exists.
718                                   if so, print an error message and exit*/
719
720         open_output(infile, outfilename);
721         add_sample_msg();
722         if (infile && (include = extendfile(infile, ".h"))) {
723                 f_print(fout, "#include \"%s\"\n", include);
724                 free(include);
725         } else
726                 f_print(fout, "#include <rpc/rpc.h>\n");
727         tell = ftell(fout);
728         while ((def = get_definition()) != NULL) {
729                 has_program += write_sample_clnt(def);
730         }
731
732         if (has_program)
733                 write_sample_clnt_main();
734
735         if (extend && tell == ftell(fout)) {
736                 (void) unlink(outfilename);
737         }
738 }
739
740 /*
741  * Perform registrations for service output 
742  * Return 0 if failed; 1 otherwise.
743  */
744 static int
745 do_registers(int argc, char **argv)
746 {
747         int             i;
748
749         if (inetdflag || !tirpcflag) {
750                 for (i = 1; i < argc; i++) {
751                         if (streq(argv[i], "-s")) {
752                                 if (!check_nettype(argv[i + 1], valid_i_nettypes))
753                                         return 0;
754                                 write_inetd_register(argv[i + 1]);
755                                 i++;
756                         }
757                 }
758         } else {
759                 for (i = 1; i < argc; i++)
760                         if (streq(argv[i], "-s")) {
761                                 if (!check_nettype(argv[i + 1], valid_ti_nettypes))
762                                         return 0;
763                                 write_nettype_register(argv[i + 1]);
764                                 i++;
765                         } else if (streq(argv[i], "-n")) {
766                                 write_netid_register(argv[i + 1]);
767                                 i++;
768                         }
769         }
770         return 1;
771 }
772
773 /*
774  * Add another argument to the arg list
775  */
776 static void
777 addarg(char *cp)
778 {
779         if (argcount >= ARGLISTLEN) {
780                 f_print(stderr, "rpcgen: too many defines\n");
781                 crash();
782                 /*NOTREACHED*/
783         }
784         arglist[argcount++] = cp;
785
786 }
787
788 static void
789 putarg(int where, char *cp)
790 {
791         if (where >= ARGLISTLEN) {
792                 f_print(stderr, "rpcgen: arglist coding error\n");
793                 crash();
794                 /*NOTREACHED*/
795         }
796         arglist[where] = cp;
797         
798 }
799
800 /*
801  * if input file is stdin and an output file is specified then complain
802  * if the file already exists. Otherwise the file may get overwritten
803  * If input file does not exist, exit with an error 
804  */
805
806 static void
807 checkfiles(char *infile, char *outfile) 
808 {
809
810   struct stat buf;
811
812   if(infile)                    /* infile ! = NULL */
813     if(stat(infile,&buf) < 0)
814       {
815         perror(infile);
816         crash();
817       };
818   if (outfile) {
819     if (stat(outfile, &buf) < 0) 
820       return;                   /* file does not exist */
821     else {
822       f_print(stderr, 
823               "file '%s' already exists and may be overwritten\n", outfile);
824       crash();
825     }
826   }
827 }
828
829 /*
830  * Parse command line arguments 
831  */
832 static int
833 parseargs(int argc, char **argv, struct commandline *cmd)
834 {
835         int i;
836         int j;
837         char c;
838         char flag[(1 << 8 * sizeof(char))];
839         int nflags;
840
841         cmdname = argv[0];
842         cmd->infile = cmd->outfile = NULL;
843         if (argc < 2) {
844                 return (0);
845         }
846         allfiles = 0;
847         flag['c'] = 0;
848         flag['h'] = 0;
849         flag['l'] = 0;
850         flag['m'] = 0;
851         flag['o'] = 0;
852         flag['s'] = 0;
853         flag['n'] = 0;
854         flag['t'] = 0;
855         flag['S'] = 0;
856         flag['C'] = 0;
857         for (i = 1; i < argc; i++) {
858                 if (argv[i][0] != '-') {
859                         if (cmd->infile) {
860                                 f_print( stderr, "Cannot specify more than one input file!\n");
861
862                                 return (0);
863                         }
864                         cmd->infile = argv[i];
865                 } else {
866                         for (j = 1; argv[i][j] != 0; j++) {
867                                 c = argv[i][j];
868                                 switch (c) {
869                                 case 'a':
870                                         allfiles = 1;
871                                         break;
872                                 case 'c':
873                                 case 'h':
874                                 case 'l':
875                                 case 'm':
876                                 case 't':
877                                         if (flag[(int) c]) {
878                                                 return (0);
879                                         }
880                                         flag[(int) c] = 1;
881                                         break;
882                                 case 'S':  
883                                         /* sample flag: Ss or Sc.
884                                            Ss means set flag['S'];
885                                            Sc means set flag['C']; */
886                                         c = argv[i][++j];  /* get next char */
887                                         if( c == 's' )
888                                           c = 'S';
889                                         else if( c == 'c' )
890                                           c = 'C';
891                                         else
892                                           return( 0 );
893
894                                         if (flag[(int) c]) {
895                                                 return (0);
896                                         }
897                                         flag[(int) c] = 1;
898                                         break;
899                                 case 'C':  /* ANSI C syntax */
900                                         Cflag = 1;
901                                         break;
902
903                                 case 'b':  /* turn TIRPC flag off for
904                                             generating backward compatible
905                                             */
906                                         tirpcflag = 0;
907                                         break;
908
909                                 case 'I':
910                                         inetdflag = 1;
911                                         break;
912                                 case 'N':
913                                         newstyle = 1;
914                                         break;
915                                 case 'L':
916                                         logflag = 1;
917                                         break;
918                                 case 'K':
919                                         if (++i == argc) {
920                                                 return (0);
921                                         }
922                                         svcclosetime = argv[i];
923                                         goto nextarg;
924                                 case 'T':
925                                         tblflag = 1;
926                                         break;
927                                 case 'i' :
928                                         if (++i == argc) {
929                                                 return (0);
930                                         }
931                                         Inline = atoi(argv[i]);
932                                         goto nextarg;
933                                 case 'n':
934                                 case 'o':
935                                 case 's':
936                                         if (argv[i][j - 1] != '-' || 
937                                             argv[i][j + 1] != 0) {
938                                                 return (0);
939                                         }
940                                         flag[(int) c] = 1;
941                                         if (++i == argc) {
942                                                 return (0);
943                                         }
944                                         if (c == 's') {
945                                                 if (!streq(argv[i], "udp") &&
946                                                     !streq(argv[i], "tcp")) {
947                                                         return (0);
948                                                 }
949                                         } else if (c == 'o') {
950                                                 if (cmd->outfile) {
951                                                         return (0);
952                                                 }
953                                                 cmd->outfile = argv[i];
954                                         }
955                                         goto nextarg;
956                                 case 'D':
957                                         if (argv[i][j - 1] != '-') {
958                                                 return (0);
959                                         }
960                                         (void) addarg(argv[i]);
961                                         goto nextarg;
962                                 case 'Y':
963                                         if (++i == argc) {
964                                                 return (0);
965                                         }
966                                         (void) strcpy(pathbuf, argv[i]);
967                                         (void) strcat(pathbuf, "/cpp");
968                                         CPP = pathbuf;
969                                         cppDefined = 1;
970                                         goto nextarg;
971
972
973
974                                 default:
975                                         return (0);
976                                 }
977                         }
978         nextarg:
979                         ;
980                 }
981         }
982
983         cmd->cflag = flag['c'];
984         cmd->hflag = flag['h'];
985         cmd->lflag = flag['l'];
986         cmd->mflag = flag['m'];
987         cmd->nflag = flag['n'];
988         cmd->sflag = flag['s'];
989         cmd->tflag = flag['t'];
990         cmd->Ssflag = flag['S'];
991         cmd->Scflag = flag['C'];
992
993         if( tirpcflag ) {
994           pmflag = inetdflag ? 0 : 1;     /* pmflag or inetdflag is always TRUE */
995           if( (inetdflag && cmd->nflag)) { /* netid not allowed with inetdflag */
996             f_print(stderr, "Cannot use netid flag with inetd flag!\n");
997             return (0);
998           }
999         } else {  /* 4.1 mode */
1000           pmflag = 0;               /* set pmflag only in tirpcmode */
1001           inetdflag = 1;            /* inetdflag is TRUE by default */
1002           if( cmd->nflag ) {          /* netid needs TIRPC */
1003             f_print( stderr, "Cannot use netid flag without TIRPC!\n");
1004             return( 0 );
1005           }
1006         }
1007
1008         if( newstyle && ( tblflag || cmd->tflag) ) {
1009           f_print( stderr, "Cannot use table flags with newstyle!\n");
1010           return( 0 );
1011         }
1012
1013         /* check no conflicts with file generation flags */
1014         nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag +
1015                 cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag + cmd->Scflag;
1016
1017         if (nflags == 0) {
1018                 if (cmd->outfile != NULL || cmd->infile == NULL) {
1019                         return (0);
1020                 }
1021         } else if (nflags > 1) {
1022                 f_print( stderr, "Cannot have more than one file generation flag!\n");
1023                 return (0);
1024         }
1025         return (1);
1026 }
1027
1028 static void
1029 usage(void)
1030 {
1031         f_print(stderr, "usage:  %s infile\n", cmdname);
1032         f_print(stderr, "\t%s [-a][-b][-C][-Dname[=value]] -i size  [-I [-K seconds]] [-L][-N][-T] infile\n",
1033                         cmdname);
1034         f_print(stderr, "\t%s [-c | -h | -l | -m | -t | -Sc | -Ss] [-o outfile] [infile]\n",
1035                         cmdname);
1036         f_print(stderr, "\t%s [-s nettype]* [-o outfile] [infile]\n", cmdname);
1037         f_print(stderr, "\t%s [-n netid]* [-o outfile] [infile]\n", cmdname);
1038         options_usage();
1039         exit(1);
1040 }
1041
1042 static void
1043 options_usage(void)
1044 {
1045         f_print(stderr, "options:\n");
1046         f_print(stderr, "-a\t\tgenerate all files, including samples\n");
1047         f_print(stderr, "-b\t\tbackward compatibility mode (generates code for SunOS 4.1)\n");
1048         f_print(stderr, "-c\t\tgenerate XDR routines\n");
1049         f_print(stderr, "-C\t\tANSI C mode\n");
1050         f_print(stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n");
1051         f_print(stderr, "-h\t\tgenerate header file\n");
1052         f_print(stderr, "-i size\t\tsize at which to start generating inline code\n");
1053         f_print(stderr, "-I\t\tgenerate code for inetd support in server (for SunOS 4.1)\n");
1054         f_print(stderr, "-K seconds\tserver exits after K seconds of inactivity\n");
1055         f_print(stderr, "-l\t\tgenerate client side stubs\n");
1056         f_print(stderr, "-L\t\tserver errors will be printed to syslog\n");
1057         f_print(stderr, "-m\t\tgenerate server side stubs\n");
1058         f_print(stderr, "-n netid\tgenerate server code that supports named netid\n");
1059         f_print(stderr, "-N\t\tsupports multiple arguments and call-by-value\n");
1060         f_print(stderr, "-o outfile\tname of the output file\n");
1061         f_print(stderr, "-s nettype\tgenerate server code that supports named nettype\n");
1062         f_print(stderr, "-Sc\t\tgenerate sample client code that uses remote procedures\n");
1063         f_print(stderr, "-Ss\t\tgenerate sample server code that defines remote procedures\n");
1064         f_print(stderr, "-t\t\tgenerate RPC dispatch table\n");
1065         f_print(stderr, "-T\t\tgenerate code to support RPC dispatch tables\n");
1066         f_print(stderr, "-Y path\t\tdirectory name to find C preprocessor (cpp)\n");
1067
1068         exit(1);
1069 }