forked from lattera/glibc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpc_main.c
1451 lines (1313 loc) · 35.8 KB
/
rpc_main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* From @(#)rpc_main.c 1.30 89/03/30
*
* Copyright (c) 2010, Oracle America, Inc.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
* * Neither the name of the "Oracle America, Inc." nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* rpc_main.c, Top level of the RPC protocol compiler.
*/
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <libintl.h>
#include <locale.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include "rpc_parse.h"
#include "rpc_util.h"
#include "rpc_scan.h"
#include "proto.h"
#include "../version.h"
#define PACKAGE _libc_intl_domainname
#define EXTEND 1 /* alias for TRUE */
#define DONT_EXTEND 0 /* alias for FALSE */
struct commandline
{
int cflag; /* xdr C routines */
int hflag; /* header file */
int lflag; /* client side stubs */
int mflag; /* server side stubs */
int nflag; /* netid flag */
int sflag; /* server stubs for the given transport */
int tflag; /* dispatch Table file */
int Ssflag; /* produce server sample code */
int Scflag; /* produce client sample code */
int makefileflag; /* Generate a template Makefile */
const char *infile; /* input module name */
const char *outfile; /* output module name */
};
static const char *cmdname;
static const char *svcclosetime = "120";
static int cppDefined; /* explicit path for C preprocessor */
static const char *CPP = "/lib/cpp";
static const char CPPFLAGS[] = "-C";
static char *pathbuf;
static int cpp_pid;
static const char *allv[] =
{
"rpcgen", "-s", "udp", "-s", "tcp"
};
static int allc = sizeof (allv) / sizeof (allv[0]);
static const char *allnv[] =
{
"rpcgen", "-s", "netpath",
};
static int allnc = sizeof (allnv) / sizeof (allnv[0]);
/*
* machinations for handling expanding argument list
*/
static void addarg (const char *); /* add another argument to the list */
static void putarg (int, const char *); /* put argument at specified location */
static void clear_args (void); /* clear argument list */
static void checkfiles (const char *, const char *);
/* check if out file already exists */
static void clear_args (void);
static char *extendfile (const char *file, const char *ext);
static void open_output (const char *infile, const char *outfile);
static void add_warning (void);
static void clear_args (void);
static void find_cpp (void);
static void open_input (const char *infile, const char *define);
static int check_nettype (const char *name, const char *list_to_check[]);
static void c_output (const char *infile, const char *define,
int extend, const char *outfile);
static void h_output (const char *infile, const char *define,
int extend, const char *outfile);
static void s_output (int argc, const char *argv[], const char *infile,
const char *define, int extend,
const char *outfile, int nomain, int netflag);
static void l_output (const char *infile, const char *define,
int extend, const char *outfile);
static void t_output (const char *infile, const char *define,
int extend, const char *outfile);
static void svc_output (const char *infile, const char *define,
int extend, const char *outfile);
static void clnt_output (const char *infile, const char *define,
int extend, const char *outfile);
static void mkfile_output (struct commandline *cmd);
static int do_registers (int argc, const char *argv[]);
static void addarg (const char *cp);
static void putarg (int whereto, const char *cp);
static void checkfiles (const char *infile, const char *outfile);
static int parseargs (int argc, const char *argv[], struct commandline *cmd);
static void usage (FILE *stream, int status) __attribute__ ((noreturn));
static void options_usage (FILE *stream, int status) __attribute__ ((noreturn));
static void print_version (void);
static void c_initialize (void);
static char *generate_guard (const char *pathname);
#define ARGLISTLEN 20
#define FIXEDARGS 2
static const char *arglist[ARGLISTLEN];
static int argcount = FIXEDARGS;
int nonfatalerrors; /* errors */
int inetdflag /* = 1 */ ; /* Support for inetd *//* is now the default */
int pmflag; /* Support for port monitors */
int logflag; /* Use syslog instead of fprintf for errors */
int tblflag; /* Support for dispatch table file */
int mtflag; /* Support for MT */
#define INLINE 3
/*length at which to start doing an inline */
int inlineflag = INLINE; /* length at which to start doing an inline. 3 = default
if 0, no xdr_inline code */
int indefinitewait; /* If started by port monitors, hang till it wants */
int exitnow; /* If started by port monitors, exit after the call */
int timerflag; /* TRUE if !indefinite && !exitnow */
int newstyle; /* newstyle of passing arguments (by value) */
int Cflag = 1; /* ANSI C syntax */
int CCflag; /* C++ files */
static int allfiles; /* generate all files */
int tirpcflag; /* generating code for tirpc, by default */
xdrfunc *xdrfunc_head; /* xdr function list */
xdrfunc *xdrfunc_tail; /* xdr function list */
int
main (int argc, const char *argv[])
{
struct commandline cmd;
setlocale (LC_ALL, "");
textdomain (_libc_intl_domainname);
(void) memset ((char *) &cmd, 0, sizeof (struct commandline));
clear_args ();
if (!parseargs (argc, argv, &cmd))
usage (stderr, 1);
if (cmd.cflag || cmd.hflag || cmd.lflag || cmd.tflag || cmd.sflag ||
cmd.mflag || cmd.nflag || cmd.Ssflag || cmd.Scflag)
{
checkfiles (cmd.infile, cmd.outfile);
}
else
checkfiles (cmd.infile, NULL);
if (cmd.cflag)
c_output (cmd.infile, "-DRPC_XDR", DONT_EXTEND, cmd.outfile);
else if (cmd.hflag)
h_output (cmd.infile, "-DRPC_HDR", DONT_EXTEND, cmd.outfile);
else if (cmd.lflag)
l_output (cmd.infile, "-DRPC_CLNT", DONT_EXTEND, cmd.outfile);
else if (cmd.sflag || cmd.mflag || (cmd.nflag))
s_output (argc, argv, cmd.infile, "-DRPC_SVC", DONT_EXTEND,
cmd.outfile, cmd.mflag, cmd.nflag);
else if (cmd.tflag)
t_output (cmd.infile, "-DRPC_TBL", DONT_EXTEND, cmd.outfile);
else if (cmd.Ssflag)
svc_output (cmd.infile, "-DRPC_SERVER", DONT_EXTEND, cmd.outfile);
else if (cmd.Scflag)
clnt_output (cmd.infile, "-DRPC_CLIENT", DONT_EXTEND, cmd.outfile);
else if (cmd.makefileflag)
mkfile_output (&cmd);
else
{
/* the rescans are required, since cpp may effect input */
c_output (cmd.infile, "-DRPC_XDR", EXTEND, "_xdr.c");
reinitialize ();
h_output (cmd.infile, "-DRPC_HDR", EXTEND, ".h");
reinitialize ();
l_output (cmd.infile, "-DRPC_CLNT", EXTEND, "_clnt.c");
reinitialize ();
if (inetdflag || !tirpcflag)
s_output (allc, allv, cmd.infile, "-DRPC_SVC", EXTEND,
"_svc.c", cmd.mflag, cmd.nflag);
else
s_output (allnc, allnv, cmd.infile, "-DRPC_SVC",
EXTEND, "_svc.c", cmd.mflag, cmd.nflag);
if (tblflag)
{
reinitialize ();
t_output (cmd.infile, "-DRPC_TBL", EXTEND, "_tbl.i");
}
if (allfiles)
{
reinitialize ();
svc_output (cmd.infile, "-DRPC_SERVER", EXTEND, "_server.c");
reinitialize ();
clnt_output (cmd.infile, "-DRPC_CLIENT", EXTEND, "_client.c");
}
if (allfiles || (cmd.makefileflag == 1))
{
reinitialize ();
mkfile_output (&cmd);
}
}
return nonfatalerrors;
}
/*
* add extension to filename
*/
static char *
extendfile (const char *file, const char *ext)
{
char *res;
const char *p;
res = alloc (strlen (file) + strlen (ext) + 1);
if (res == NULL)
abort ();
p = strrchr (file, '.');
if (p == NULL)
p = file + strlen (file);
strcpy (res, file);
strcpy (res + (p - file), ext);
return res;
}
/*
* Open output file with given extension
*/
static void
open_output (const char *infile, const char *outfile)
{
if (outfile == NULL)
{
fout = stdout;
return;
}
if (infile != NULL && streq (outfile, infile))
{
fprintf (stderr, _ ("%s: output would overwrite %s\n"), cmdname,
infile);
crash ();
}
fout = fopen (outfile, "w");
if (fout == NULL)
{
fprintf (stderr, _ ("%s: unable to open %s: %m\n"), cmdname, outfile);
crash ();
}
record_open (outfile);
}
/* Close the output file and check for write errors. */
static void
close_output (const char *outfile)
{
if (fclose (fout) == EOF)
{
fprintf (stderr, _("%s: while writing output %s: %m"), cmdname,
outfile ?: "<stdout>");
crash ();
}
}
static void
add_warning (void)
{
fprintf (fout, "/*\n");
fprintf (fout, " * Please do not edit this file.\n");
fprintf (fout, " * It was generated using rpcgen.\n");
fprintf (fout, " */\n\n");
}
/* clear list of arguments */
static void
clear_args (void)
{
int i;
for (i = FIXEDARGS; i < ARGLISTLEN; ++i)
arglist[i] = NULL;
argcount = FIXEDARGS;
}
/* make sure that a CPP exists */
static void
find_cpp (void)
{
struct stat64 buf;
if (stat64 (CPP, &buf) == 0)
return;
if (cppDefined) /* user specified cpp but it does not exist */
{
fprintf (stderr, _ ("cannot find C preprocessor: %s\n"), CPP);
crash ();
}
/* fall back to system CPP */
CPP = "cpp";
}
/*
* Open input file with given define for C-preprocessor
*/
static void
open_input (const char *infile, const char *define)
{
int pd[2];
infilename = (infile == NULL) ? "<stdin>" : infile;
if (pipe (pd) != 0)
{
perror ("pipe");
exit (1);
}
cpp_pid = fork ();
switch (cpp_pid)
{
case 0:
find_cpp ();
putarg (0, CPP);
putarg (1, CPPFLAGS);
addarg (define);
if (infile)
addarg (infile);
addarg ((char *) NULL);
close (1);
dup2 (pd[1], 1);
close (pd[0]);
execvp (arglist[0], (char **) arglist);
if (errno == ENOENT)
{
fprintf (stderr, _ ("cannot find C preprocessor: %s\n"), CPP);
exit (1);
}
perror ("execvp");
exit (1);
case -1:
perror ("fork");
exit (1);
}
close (pd[1]);
fin = fdopen (pd[0], "r");
if (fin == NULL)
{
fprintf (stderr, "%s: ", cmdname);
perror (infilename);
crash ();
}
}
/* Close the connection to the C-preprocessor and check for successfull
termination. */
static void
close_input (void)
{
int status;
fclose (fin);
/* Check the termination status. */
if (waitpid (cpp_pid, &status, 0) < 0)
{
perror ("waitpid");
crash ();
}
if (WIFSIGNALED (status) || WEXITSTATUS (status) != 0)
{
if (WIFSIGNALED (status))
fprintf (stderr, _("%s: C preprocessor failed with signal %d\n"),
cmdname, WTERMSIG (status));
else
fprintf (stderr, _("%s: C preprocessor failed with exit code %d\n"),
cmdname, WEXITSTATUS (status));
crash ();
}
}
/* valid tirpc nettypes */
static const char *valid_ti_nettypes[] =
{
"netpath",
"visible",
"circuit_v",
"datagram_v",
"circuit_n",
"datagram_n",
"udp",
"tcp",
"raw",
NULL
};
/* valid inetd nettypes */
static const char *valid_i_nettypes[] =
{
"udp",
"tcp",
NULL
};
static int
check_nettype (const char *name, const char *list_to_check[])
{
int i;
for (i = 0; list_to_check[i] != NULL; i++)
{
if (strcmp (name, list_to_check[i]) == 0)
{
return 1;
}
}
fprintf (stderr, _ ("illegal nettype: `%s'\n"), name);
return 0;
}
/*
* Compile into an XDR routine output file
*/
static void
c_output (const char *infile, const char *define, int extend,
const char *outfile)
{
definition *def;
char *include;
const char *outfilename;
long tell;
c_initialize ();
open_input (infile, define);
outfilename = extend ? extendfile (infile, outfile) : outfile;
open_output (infile, outfilename);
add_warning ();
if (infile && (include = extendfile (infile, ".h")))
{
fprintf (fout, "#include \"%s\"\n", include);
free (include);
/* .h file already contains rpc/rpc.h */
}
else
fprintf (fout, "#include <rpc/rpc.h>\n");
tell = ftell (fout);
while ((def = get_definition ()) != NULL)
emit (def);
if (extend && tell == ftell (fout))
unlink (outfilename);
close_input ();
close_output (outfilename);
}
void
c_initialize (void)
{
/* add all the starting basic types */
add_type (1, "int");
add_type (1, "long");
add_type (1, "short");
add_type (1, "bool");
add_type (1, "u_int");
add_type (1, "u_long");
add_type (1, "u_short");
}
char rpcgen_table_dcl[] = "struct rpcgen_table {\n\
char *(*proc)();\n\
xdrproc_t xdr_arg;\n\
unsigned len_arg;\n\
xdrproc_t xdr_res;\n\
unsigned len_res;\n\
};\n";
static char *
generate_guard (const char *pathname)
{
const char *filename;
char *guard, *tmp;
filename = strrchr (pathname, '/'); /* find last component */
filename = ((filename == NULL) ? pathname : filename + 1);
guard = extendfile (filename, "_H_RPCGEN");
/* convert to upper case */
tmp = guard;
while (*tmp)
{
if (islower (*tmp))
*tmp = toupper (*tmp);
tmp++;
}
return guard;
}
/*
* Compile into an XDR header file
*/
static void
h_output (const char *infile, const char *define, int extend,
const char *outfile)
{
xdrfunc *xdrfuncp;
definition *def;
const char *ifilename;
const char *outfilename;
long tell;
char *guard;
list *l;
open_input (infile, define);
outfilename = extend ? extendfile (infile, outfile) : outfile;
open_output (infile, outfilename);
add_warning ();
ifilename = (infile == NULL) ? "STDIN" : infile;
guard = generate_guard (outfilename ? outfilename : ifilename);
fprintf (fout, "#ifndef _%s\n#define _%s\n\n", guard,
guard);
fprintf (fout, "#include <rpc/rpc.h>\n\n");
if (mtflag)
{
fprintf (fout, "#include <pthread.h>\n");
}
/* put the C++ support */
if (Cflag && !CCflag)
{
fprintf (fout, "\n#ifdef __cplusplus\n");
fprintf (fout, "extern \"C\" {\n");
fprintf (fout, "#endif\n\n");
}
tell = ftell (fout);
/* print data definitions */
while ((def = get_definition ()) != NULL)
{
print_datadef (def);
}
/* print function declarations.
Do this after data definitions because they might be used as
arguments for functions */
for (l = defined; l != NULL; l = l->next)
{
print_funcdef (l->val);
}
/* Now print all xdr func declarations */
if (xdrfunc_head != NULL)
{
fprintf (fout, "\n/* the xdr functions */\n");
if (CCflag)
{
fprintf (fout, "\n#ifdef __cplusplus\n");
fprintf (fout, "extern \"C\" {\n");
fprintf (fout, "#endif\n");
}
if (!Cflag)
{
xdrfuncp = xdrfunc_head;
while (xdrfuncp != NULL)
{
print_xdr_func_def (xdrfuncp->name,
xdrfuncp->pointerp, 2);
xdrfuncp = xdrfuncp->next;
}
}
else
{
int i;
for (i = 1; i < 3; ++i)
{
if (i == 1)
fprintf (fout, "\n#if defined(__STDC__) || defined(__cplusplus)\n");
else
fprintf (fout, "\n#else /* K&R C */\n");
xdrfuncp = xdrfunc_head;
while (xdrfuncp != NULL)
{
print_xdr_func_def (xdrfuncp->name,
xdrfuncp->pointerp, i);
xdrfuncp = xdrfuncp->next;
}
}
fprintf (fout, "\n#endif /* K&R C */\n");
}
}
if (extend && tell == ftell (fout))
{
unlink (outfilename);
}
else if (tblflag)
{
fprintf (fout, "%s", rpcgen_table_dcl);
}
if (Cflag)
{
fprintf (fout, "\n#ifdef __cplusplus\n");
fprintf (fout, "}\n");
fprintf (fout, "#endif\n");
}
fprintf (fout, "\n#endif /* !_%s */\n", guard);
free (guard);
close_input ();
close_output (outfilename);
}
/*
* Compile into an RPC service
*/
static void
s_output (int argc, const char *argv[], const char *infile, const char *define,
int extend, const char *outfile, int nomain, int netflag)
{
char *include;
definition *def;
int foundprogram = 0;
const char *outfilename;
open_input (infile, define);
outfilename = extend ? extendfile (infile, outfile) : outfile;
open_output (infile, outfilename);
add_warning ();
if (infile && (include = extendfile (infile, ".h")))
{
fprintf (fout, "#include \"%s\"\n", include);
free (include);
}
else
fprintf (fout, "#include <rpc/rpc.h>\n");
fprintf (fout, "#include <stdio.h>\n");
fprintf (fout, "#include <stdlib.h>\n");
fprintf (fout, "#include <rpc/pmap_clnt.h>\n");
if (Cflag)
fprintf (fout, "#include <string.h>\n");
if (strcmp (svcclosetime, "-1") == 0)
indefinitewait = 1;
else if (strcmp (svcclosetime, "0") == 0)
exitnow = 1;
else if (inetdflag || pmflag)
{
fprintf (fout, "#include <signal.h>\n");
timerflag = 1;
}
if (!tirpcflag && inetdflag)
fprintf (fout, "#include <sys/ioctl.h> /* ioctl, TIOCNOTTY */\n");
if (Cflag && (inetdflag || pmflag))
{
fprintf (fout, "#include <sys/types.h> /* open */\n");
fprintf (fout, "#include <sys/stat.h> /* open */\n");
fprintf (fout, "#include <fcntl.h> /* open */\n");
fprintf (fout, "#include <unistd.h> /* getdtablesize */\n");
}
if (tirpcflag && !(Cflag && (inetdflag || pmflag)))
fprintf (fout, "#include <sys/types.h>\n");
fprintf (fout, "#include <memory.h>\n");
if (inetdflag || !tirpcflag)
{
fprintf (fout, "#include <sys/socket.h>\n");
fprintf (fout, "#include <netinet/in.h>\n");
}
if ((netflag || pmflag) && tirpcflag && !nomain)
{
fprintf (fout, "#include <netconfig.h>\n");
}
if ( /*timerflag && */ tirpcflag)
fprintf (fout, "#include <sys/resource.h> /* rlimit */\n");
if (logflag || inetdflag || pmflag)
{
fprintf (fout, "#include <syslog.h>\n");
}
/* for ANSI-C */
if (Cflag)
fprintf (fout, "\n#ifndef SIG_PF\n#define SIG_PF void(*)(int)\n#endif\n");
if (timerflag)
fprintf (fout, "\n#define _RPCSVC_CLOSEDOWN %s\n", svcclosetime);
while ((def = get_definition ()) != NULL)
{
foundprogram |= (def->def_kind == DEF_PROGRAM);
}
if (extend && !foundprogram)
{
unlink (outfilename);
return;
}
write_most (infile, netflag, nomain);
if (!nomain)
{
if (!do_registers (argc, argv))
{
if (outfilename)
unlink (outfilename);
usage (stderr, 1);
}
write_rest ();
}
close_input ();
close_output (outfilename);
}
/*
* generate client side stubs
*/
static void
l_output (const char *infile, const char *define, int extend,
const char *outfile)
{
char *include;
definition *def;
int foundprogram = 0;
const char *outfilename;
open_input (infile, define);
outfilename = extend ? extendfile (infile, outfile) : outfile;
open_output (infile, outfilename);
add_warning ();
if (Cflag)
fprintf (fout, "#include <memory.h> /* for memset */\n");
if (infile && (include = extendfile (infile, ".h")))
{
fprintf (fout, "#include \"%s\"\n", include);
free (include);
}
else
fprintf (fout, "#include <rpc/rpc.h>\n");
while ((def = get_definition ()) != NULL)
{
foundprogram |= (def->def_kind == DEF_PROGRAM);
}
if (extend && !foundprogram)
{
unlink (outfilename);
return;
}
write_stubs ();
close_input ();
close_output (outfilename);
}
/*
* generate the dispatch table
*/
static void
t_output (const char *infile, const char *define, int extend,
const char *outfile)
{
definition *def;
int foundprogram = 0;
const char *outfilename;
open_input (infile, define);
outfilename = extend ? extendfile (infile, outfile) : outfile;
open_output (infile, outfilename);
add_warning ();
while ((def = get_definition ()) != NULL)
{
foundprogram |= (def->def_kind == DEF_PROGRAM);
}
if (extend && !foundprogram)
{
unlink (outfilename);
return;
}
write_tables ();
close_input ();
close_output (outfilename);
}
/* sample routine for the server template */
static void
svc_output (const char *infile, const char *define, int extend,
const char *outfile)
{
definition *def;
char *include;
const char *outfilename;
long tell;
open_input (infile, define);
outfilename = extend ? extendfile (infile, outfile) : outfile;
checkfiles (infile, outfilename);
/*check if outfile already exists.
if so, print an error message and exit */
open_output (infile, outfilename);
add_sample_msg ();
if (infile && (include = extendfile (infile, ".h")))
{
fprintf (fout, "#include \"%s\"\n", include);
free (include);
}
else
fprintf (fout, "#include <rpc/rpc.h>\n");
tell = ftell (fout);
while ((def = get_definition ()) != NULL)
{
write_sample_svc (def);
}
if (extend && tell == ftell (fout))
{
unlink (outfilename);
}
close_input ();
close_output (outfilename);
}
/* sample main routine for client */
static void
clnt_output (const char *infile, const char *define, int extend,
const char *outfile)
{
definition *def;
char *include;
const char *outfilename;
long tell;
int has_program = 0;
open_input (infile, define);
outfilename = extend ? extendfile (infile, outfile) : outfile;
checkfiles (infile, outfilename);
/*check if outfile already exists.
if so, print an error message and exit */
open_output (infile, outfilename);
add_sample_msg ();
if (infile && (include = extendfile (infile, ".h")))
{
fprintf (fout, "#include \"%s\"\n", include);
free (include);
}
else
fprintf (fout, "#include <rpc/rpc.h>\n");
tell = ftell (fout);
while ((def = get_definition ()) != NULL)
{
has_program += write_sample_clnt (def);
}
if (has_program)
write_sample_clnt_main ();
if (extend && tell == ftell (fout))
{
unlink (outfilename);
}
close_input ();
close_output (outfilename);
}
static const char space[] = " ";
static char *
file_name (const char *file, const char *ext)
{
char *temp;
temp = extendfile (file, ext);
if (access (temp, F_OK) != -1)
return (temp);
free (temp);
return (char *) space;
}
static void
mkfile_output (struct commandline *cmd)
{
char *mkfilename;
char *clientname, *clntname, *xdrname, *hdrname;
char *servername, *svcname, *servprogname, *clntprogname;
svcname = file_name (cmd->infile, "_svc.c");
clntname = file_name (cmd->infile, "_clnt.c");
xdrname = file_name (cmd->infile, "_xdr.c");
hdrname = file_name (cmd->infile, ".h");
if (allfiles)
{
servername = extendfile (cmd->infile, "_server.c");
clientname = extendfile (cmd->infile, "_client.c");
}
else
{
servername = (char *) space;
clientname = (char *) space;
}
servprogname = extendfile (cmd->infile, "_server");
clntprogname = extendfile (cmd->infile, "_client");
if (allfiles)
{
char *cp, *temp;
mkfilename = alloc (strlen ("Makefile.") + strlen (cmd->infile) + 1);
if (mkfilename == NULL)
abort ();
temp = strrchr (cmd->infile, '.');
cp = stpcpy (mkfilename, "Makefile.");
if (temp != NULL)
*((char *) stpncpy (cp, cmd->infile, temp - cmd->infile)) = '\0';
else
stpcpy (cp, cmd->infile);
}
else
mkfilename = (char *) cmd->outfile;
checkfiles (NULL, mkfilename);
open_output (NULL, mkfilename);
fprintf (fout, "\n# This is a template Makefile generated by rpcgen\n");
f_print (fout, "\n# Parameters\n\n");
f_print (fout, "CLIENT = %s\nSERVER = %s\n\n", clntprogname, servprogname);
f_print (fout, "SOURCES_CLNT.c = \nSOURCES_CLNT.h = \n");
f_print (fout, "SOURCES_SVC.c = \nSOURCES_SVC.h = \n");
f_print (fout, "SOURCES.x = %s\n\n", cmd->infile);
f_print (fout, "TARGETS_SVC.c = %s %s %s \n",
svcname, servername, xdrname);
f_print (fout, "TARGETS_CLNT.c = %s %s %s \n",
clntname, clientname, xdrname);
f_print (fout, "TARGETS = %s %s %s %s %s %s\n\n",
hdrname, xdrname, clntname,
svcname, clientname, servername);
f_print (fout, "OBJECTS_CLNT = $(SOURCES_CLNT.c:%%.c=%%.o) \
$(TARGETS_CLNT.c:%%.c=%%.o)");
f_print (fout, "\nOBJECTS_SVC = $(SOURCES_SVC.c:%%.c=%%.o) \
$(TARGETS_SVC.c:%%.c=%%.o)");
f_print (fout, "\n# Compiler flags \n");
if (mtflag)
fprintf (fout, "\nCPPFLAGS += -D_REENTRANT\nCFLAGS += -g \nLDLIBS \
+= -lnsl -lpthread \n ");
else
f_print (fout, "\nCFLAGS += -g \nLDLIBS += -lnsl\n");