Bug Summary

File:src/usr.bin/rpcgen/rpc_main.c
Warning:line 449, column 9
Dereference of null pointer (loaded from variable 'tmp')

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name rpc_main.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/usr.bin/rpcgen/obj -resource-dir /usr/local/lib/clang/13.0.0 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/usr.bin/rpcgen/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c /usr/src/usr.bin/rpcgen/rpc_main.c
1/* $OpenBSD: rpc_main.c,v 1.35 2019/06/28 13:35:03 deraadt Exp $ */
2/* $NetBSD: rpc_main.c,v 1.9 1996/02/19 11:12:43 pk Exp $ */
3
4/*
5 * Copyright (c) 2010, Oracle America, Inc.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials
16 * provided with the distribution.
17 * * Neither the name of the "Oracle America, Inc." nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
26 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
28 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35/*
36 * rpc_main.c, Top level of the RPC protocol compiler.
37 */
38
39#define RPCGEN_VERSION"199506" "199506"/* This program's version (year & month) */
40
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44#include <unistd.h>
45#include <limits.h>
46#include <ctype.h>
47#include <sys/stat.h>
48#include "rpc_parse.h"
49#include "rpc_util.h"
50#include "rpc_scan.h"
51
52#define EXTEND1 1 /* alias for TRUE */
53#define DONT_EXTEND0 0 /* alias for FALSE */
54
55#define SVR4_CPP"/usr/ccs/lib/cpp" "/usr/ccs/lib/cpp"
56#define SUNOS_CPP"/lib/cpp" "/lib/cpp"
57static int cppDefined = 0; /* explicit path for C preprocessor */
58
59struct commandline {
60 int cflag; /* xdr C routines */
61 int hflag; /* header file */
62 int lflag; /* client side stubs */
63 int mflag; /* server side stubs */
64 int nflag; /* netid flag */
65 int sflag; /* server stubs for the given transport */
66 int tflag; /* dispatch Table file */
67 int Ssflag; /* produce server sample code */
68 int Scflag; /* produce client sample code */
69 char *infile; /* input module name */
70 char *outfile;/* output module name */
71};
72
73static char *cmdname;
74
75static char *svcclosetime = "120";
76static char *CPP = "/usr/bin/cpp";
77static char CPPFLAGS[] = "-C";
78static char pathbuf[PATH_MAX1024];
79static char *allv[] = {
80 "rpcgen", "-s", "udp", "-s", "tcp",
81};
82static int allc = sizeof(allv) / sizeof(allv[0]);
83static char *allnv[] = {
84 "rpcgen", "-s", "netpath",
85};
86static int allnc = sizeof(allnv) / sizeof(allnv[0]);
87
88#define ARGLISTLEN20 20
89#define FIXEDARGS2 2
90
91static char *arglist[ARGLISTLEN20];
92static int argcount = FIXEDARGS2;
93
94
95int nonfatalerrors; /* errors */
96int inetdflag /* = 1 */ ; /* Support for inetd *//* is now the
97 * default */
98int pmflag; /* Support for port monitors */
99int logflag; /* Use syslog instead of fprintf for errors */
100int tblflag; /* Support for dispatch table file */
101int callerflag; /* Generate svc_caller() function */
102
103#define INLINE3 3
104/* length at which to start doing an inline */
105
106int doinline = INLINE3; /* length at which to start doing an
107 * inline. 3 = default if 0, no
108 * xdr_inline code */
109
110int indefinitewait; /* If started by port monitors, hang till it
111 * wants */
112int exitnow; /* If started by port monitors, exit after
113 * the call */
114int timerflag; /* TRUE if !indefinite && !exitnow */
115int newstyle; /* newstyle of passing arguments (by value) */
116int Cflag = 0; /* ANSI C syntax */
117static int allfiles; /* generate all files */
118int tirpcflag = 0; /* generating code for tirpc, by default */
119
120static void c_output(char *, char *, int, char *);
121static void h_output(char *, char *, int, char *);
122static void s_output(int, char **, char *, char *, int, char *, int, int);
123static void l_output(char *, char *, int, char *);
124static void t_output(char *, char *, int, char *);
125static void svc_output(char *, char *, int, char *);
126static void clnt_output(char *, char *, int, char *);
127static int do_registers(int, char **);
128static void addarg(char *);
129static void putarg(int, char *);
130static void clear_args(void);
131static void checkfiles(char *, char *);
132static int parseargs(int, char **, struct commandline *);
133static void usage(void);
134void c_initialize(void);
135
136int
137main(int argc, char *argv[])
138{
139 struct commandline cmd;
140
141 if (pledge("stdio rpath wpath cpath proc exec", NULL((void *)0)) == -1) {
1
Assuming the condition is false
2
Taking false branch
142 perror("pledge");
143 exit(1);
144 }
145
146 (void) memset((char *) &cmd, 0, sizeof(struct commandline));
147 clear_args();
148 if (!parseargs(argc, argv, &cmd))
3
Taking false branch
149 usage();
150
151 if (cmd.cflag
3.1
Field 'cflag' is 0
|| cmd.hflag
3.2
Field 'hflag' is 0
|| cmd.lflag
3.3
Field 'lflag' is 0
|| cmd.tflag
3.4
Field 'tflag' is 0
|| cmd.sflag
3.5
Field 'sflag' is 0
||
4
Taking false branch
152 cmd.mflag
3.6
Field 'mflag' is 0
|| cmd.nflag
3.7
Field 'nflag' is 0
|| cmd.Ssflag
3.8
Field 'Ssflag' is 0
|| cmd.Scflag
3.9
Field 'Scflag' is 0
) {
153 checkfiles(cmd.infile, cmd.outfile);
154 } else
155 checkfiles(cmd.infile, NULL((void *)0));
156
157 if (cmd.cflag
4.1
Field 'cflag' is 0
) {
5
Taking false branch
158 c_output(cmd.infile, "-DRPC_XDR", DONT_EXTEND0, cmd.outfile);
159 } else if (cmd.hflag
5.1
Field 'hflag' is 0
) {
6
Taking false branch
160 h_output(cmd.infile, "-DRPC_HDR", DONT_EXTEND0, cmd.outfile);
161 } else if (cmd.lflag
6.1
Field 'lflag' is 0
) {
7
Taking false branch
162 l_output(cmd.infile, "-DRPC_CLNT", DONT_EXTEND0, cmd.outfile);
163 } else if (cmd.sflag
7.1
Field 'sflag' is 0
|| cmd.mflag
7.2
Field 'mflag' is 0
|| (cmd.nflag
7.3
Field 'nflag' is 0
)) {
8
Taking false branch
164 s_output(argc, argv, cmd.infile, "-DRPC_SVC", DONT_EXTEND0,
165 cmd.outfile, cmd.mflag, cmd.nflag);
166 } else if (cmd.tflag
8.1
Field 'tflag' is 0
) {
9
Taking false branch
167 t_output(cmd.infile, "-DRPC_TBL", DONT_EXTEND0, cmd.outfile);
168 } else if (cmd.Ssflag
9.1
Field 'Ssflag' is 0
) {
10
Taking false branch
169 svc_output(cmd.infile, "-DRPC_SERVER", DONT_EXTEND0, cmd.outfile);
170 } else if (cmd.Scflag
10.1
Field 'Scflag' is 0
) {
11
Taking false branch
171 clnt_output(cmd.infile, "-DRPC_CLIENT", DONT_EXTEND0, cmd.outfile);
172 } else {
173 /* the rescans are required, since cpp may effect input */
174 c_output(cmd.infile, "-DRPC_XDR", EXTEND1, "_xdr.c");
175 reinitialize();
176 h_output(cmd.infile, "-DRPC_HDR", EXTEND1, ".h");
12
Calling 'h_output'
177 reinitialize();
178 l_output(cmd.infile, "-DRPC_CLNT", EXTEND1, "_clnt.c");
179 reinitialize();
180 if (inetdflag || !tirpcflag)
181 s_output(allc, allv, cmd.infile, "-DRPC_SVC", EXTEND1,
182 "_svc.c", cmd.mflag, cmd.nflag);
183 else
184 s_output(allnc, allnv, cmd.infile, "-DRPC_SVC",
185 EXTEND1, "_svc.c", cmd.mflag, cmd.nflag);
186 if (tblflag) {
187 reinitialize();
188 t_output(cmd.infile, "-DRPC_TBL", EXTEND1, "_tbl.i");
189 }
190 if (allfiles) {
191 reinitialize();
192 svc_output(cmd.infile, "-DRPC_SERVER", EXTEND1, "_server.c");
193 }
194 if (allfiles) {
195 reinitialize();
196 clnt_output(cmd.infile, "-DRPC_CLIENT", EXTEND1, "_client.c");
197 }
198 }
199 exit(nonfatalerrors);
200 /* NOTREACHED */
201}
202
203/*
204 * add extension to filename
205 */
206static char *
207extendfile(char *path, char *ext)
208{
209 char *file;
210 char *res;
211 char *p;
212 size_t len;
213
214 if ((file = strrchr(path, '/')) == NULL((void *)0))
215 file = path;
216 else
217 file++;
218
219 len = strlen(file) + strlen(ext) + 1;
220 res = malloc(len);
221 if (res == NULL((void *)0)) {
222 fprintf(stderr(&__sF[2]), "could not allocate memory\n");
223 exit(1);
224 }
225 p = strrchr(file, '.');
226 if (p == NULL((void *)0))
227 p = file + strlen(file);
228 (void) strlcpy(res, file, len);
229 (void) strlcpy(res + (p - file), ext, len - (p - file));
230 return (res);
231}
232
233/*
234 * Open output file with given extension
235 */
236static void
237open_output(char *infile, char *outfile)
238{
239
240 if (outfile == NULL((void *)0)) {
241 fout = stdout(&__sF[1]);
242 return;
243 }
244 if (infile != NULL((void *)0) && streq(outfile, infile)) {
245 fprintf(stderr(&__sF[2]), "%s: output would overwrite %s\n", cmdname,
246 infile);
247 crash();
248 }
249 fout = fopen(outfile, "w");
250 if (fout == NULL((void *)0)) {
251 fprintf(stderr(&__sF[2]), "%s: unable to open ", cmdname);
252 perror(outfile);
253 crash();
254 }
255 record_open(outfile);
256
257}
258
259static void
260add_warning(void)
261{
262 fprintf(fout, "/*\n");
263 fprintf(fout, " * Please do not edit this file.\n");
264 fprintf(fout, " * It was generated using rpcgen.\n");
265 fprintf(fout, " */\n\n");
266}
267
268/* clear list of arguments */
269static void
270clear_args(void)
271{
272 int i;
273 for (i = FIXEDARGS2; i < ARGLISTLEN20; i++)
274 arglist[i] = NULL((void *)0);
275 argcount = FIXEDARGS2;
276}
277
278/* make sure that a CPP exists */
279static void
280find_cpp(void)
281{
282 struct stat buf;
283
284 /* SVR4 or explicit cpp does not exist */
285 if (stat(CPP, &buf) == -1) {
286 if (cppDefined) {
287 fprintf(stderr(&__sF[2]), "cannot find C preprocessor: %s \n", CPP);
288 crash();
289 } else {
290 /* try the other one */
291 CPP = SUNOS_CPP"/lib/cpp";
292 if (stat(CPP, &buf) == -1) { /* can't find any cpp */
293 fprintf(stderr(&__sF[2]),
294 "cannot find any C preprocessor: %s\n", CPP);
295 crash();
296 }
297 }
298 }
299}
300
301/*
302 * Open input file with given define for C-preprocessor
303 */
304static void
305open_input(char *infile, char *define)
306{
307 int pd[2];
308
309 infilename = (infile == NULL((void *)0)) ? "<stdin>" : infile;
310 (void) pipe(pd);
311 switch (fork()) {
312 case 0:
313 find_cpp();
314 putarg(0, CPP);
315 putarg(1, CPPFLAGS);
316 addarg(define);
317 addarg(infile);
318 addarg((char *) NULL((void *)0));
319 (void) close(1);
320 (void) dup2(pd[1], 1);
321 (void) close(pd[0]);
322 execv(arglist[0], arglist);
323 perror("execv");
324 exit(1);
325 case -1:
326 perror("fork");
327 exit(1);
328 }
329 (void) close(pd[1]);
330 fin = fdopen(pd[0], "r");
331 if (fin == NULL((void *)0)) {
332 fprintf(stderr(&__sF[2]), "%s: ", cmdname);
333 perror(infilename);
334 crash();
335 }
336}
337
338/* valid tirpc nettypes */
339static char *valid_ti_nettypes[] = {
340 "netpath",
341 "visible",
342 "circuit_v",
343 "datagram_v",
344 "circuit_n",
345 "datagram_n",
346 "udp",
347 "tcp",
348 "raw",
349 NULL((void *)0)
350};
351
352/* valid inetd nettypes */
353static char *valid_i_nettypes[] = {
354 "udp",
355 "tcp",
356 NULL((void *)0)
357};
358
359static int
360check_nettype(char *name, char *list_to_check[])
361{
362 int i;
363 for (i = 0; list_to_check[i] != NULL((void *)0); i++) {
364 if (strcmp(name, list_to_check[i]) == 0)
365 return 1;
366 }
367 fprintf(stderr(&__sF[2]), "illegal nettype :\'%s\'\n", name);
368 return 0;
369}
370
371/*
372 * Compile into an XDR routine output file
373 */
374
375static void
376c_output(infile, define, extend, outfile)
377 char *infile;
378 char *define;
379 int extend;
380 char *outfile;
381{
382 definition *def;
383 char *include;
384 char *outfilename;
385 long tell;
386
387 c_initialize();
388 open_input(infile, define);
389 outfilename = extend ? extendfile(infile, outfile) : outfile;
390 open_output(infile, outfilename);
391 add_warning();
392 if (infile && (include = extendfile(infile, ".h"))) {
393 fprintf(fout, "#include \"%s\"\n", include);
394 free(include);
395 /* .h file already contains rpc/rpc.h */
396 } else
397 fprintf(fout, "#include <rpc/rpc.h>\n");
398 tell = ftell(fout);
399 while ((def = get_definition())) {
400 emit(def);
401 }
402 if (extend && tell == ftell(fout)) {
403 (void) unlink(outfilename);
404 }
405}
406
407
408void
409c_initialize(void)
410{
411
412 /* add all the starting basic types */
413
414 add_type(1, "int");
415 add_type(1, "long");
416 add_type(1, "short");
417 add_type(1, "bool");
418
419 add_type(1, "u_int");
420 add_type(1, "u_long");
421 add_type(1, "u_short");
422
423}
424
425static const char rpcgen_table_dcl[] = "struct rpcgen_table {\n\
426 char *(*proc)();\n\
427 xdrproc_t xdr_arg;\n\
428 unsigned int len_arg;\n\
429 xdrproc_t xdr_res;\n\
430 unsigned int len_res;\n\
431};\n";
432
433
434static char *
435generate_guard(char *pathname)
436{
437 char *filename, *guard, *tmp, *tmp2;
438
439 filename = strrchr(pathname, '/'); /* find last component */
440 filename = ((filename == 0) ? pathname : filename + 1);
16
Assuming 'filename' is not equal to null
17
'?' condition is false
441 guard = strdup(filename);
18
Value assigned to 'guard'
442 if (guard == NULL((void *)0)) {
19
Assuming 'guard' is equal to NULL
20
Taking true branch
443 fprintf(stderr(&__sF[2]), "out of memory while processing %s\n", filename);
444 crash();
445 }
446
447 /* convert to upper case */
448 tmp = guard;
21
Null pointer value stored to 'tmp'
449 while (*tmp) {
22
Dereference of null pointer (loaded from variable 'tmp')
450 if (islower((unsigned char)*tmp))
451 *tmp = toupper((unsigned char)*tmp);
452 tmp++;
453 }
454
455 tmp2 = extendfile(guard, "_H_RPCGEN");
456 free(guard);
457 guard = tmp2;
458
459 return (guard);
460}
461
462/*
463 * Compile into an XDR header file
464 */
465
466static void
467h_output(infile, define, extend, outfile)
468 char *infile;
469 char *define;
470 int extend;
471 char *outfile;
472{
473 definition *def;
474 char *outfilename;
475 long tell;
476 char *guard;
477 list *l;
478
479 open_input(infile, define);
480 outfilename = extend
12.1
'extend' is 1
? extendfile(infile, outfile) : outfile;
13
'?' condition is true
481 open_output(infile, outfilename);
482 add_warning();
483 guard = generate_guard(outfilename
13.1
'outfilename' is non-null
? outfilename : infile)
;
14
'?' condition is true
15
Calling 'generate_guard'
484
485 fprintf(fout, "#ifndef _%s\n#define _%s\n\n", guard,
486 guard);
487
488 fprintf(fout, "#define RPCGEN_VERSION\t%s\n\n", RPCGEN_VERSION"199506");
489 fprintf(fout, "#include <rpc/rpc.h>\n\n");
490
491 tell = ftell(fout);
492 /* print data definitions */
493 while ((def = get_definition())) {
494 print_datadef(def);
495 }
496
497 /*
498 * print function declarations. Do this after data definitions
499 * because they might be used as arguments for functions
500 */
501 for (l = defined; l != NULL((void *)0); l = l->next) {
502 print_funcdef(l->val);
503 }
504 if (extend && tell == ftell(fout)) {
505 (void) unlink(outfilename);
506 } else if (tblflag) {
507 fprintf(fout, rpcgen_table_dcl);
508 }
509 fprintf(fout, "\n#endif /* !_%s */\n", guard);
510
511 free(guard);
512}
513
514/*
515 * Compile into an RPC service
516 */
517static void
518s_output(argc, argv, infile, define, extend, outfile, nomain, netflag)
519 int argc;
520 char *argv[];
521 char *infile;
522 char *define;
523 int extend;
524 char *outfile;
525 int nomain;
526 int netflag;
527{
528 char *include;
529 definition *def;
530 int foundprogram = 0;
531 char *outfilename;
532
533 open_input(infile, define);
534 outfilename = extend ? extendfile(infile, outfile) : outfile;
535 open_output(infile, outfilename);
536 add_warning();
537 if (infile && (include = extendfile(infile, ".h"))) {
538 fprintf(fout, "#include \"%s\"\n", include);
539 free(include);
540 } else
541 fprintf(fout, "#include <rpc/rpc.h>\n");
542
543 fprintf(fout, "#include <unistd.h>\n");
544 fprintf(fout, "#include <stdio.h>\n");
545 fprintf(fout, "#include <stdlib.h>/* getenv, exit */\n");
546 if (Cflag) {
547 fprintf(fout,
548 "#include <rpc/pmap_clnt.h> /* for pmap_unset */\n");
549 fprintf(fout, "#include <string.h> /* strcmp */ \n");
550 }
551 fprintf(fout, "#include <netdb.h>\n"); /* evas */
552 if (strcmp(svcclosetime, "-1") == 0)
553 indefinitewait = 1;
554 else if (strcmp(svcclosetime, "0") == 0)
555 exitnow = 1;
556 else if (inetdflag || pmflag) {
557 fprintf(fout, "#include <signal.h>\n");
558 timerflag = 1;
559 }
560 if (!tirpcflag && inetdflag)
561 fprintf(fout, "#include <sys/ttycom.h>/* TIOCNOTTY */\n");
562 if (Cflag && (inetdflag || pmflag)) {
563 fprintf(fout, "#ifdef __cplusplus\n");
564 fprintf(fout, "#include <sysent.h> /* getdtablesize, open */\n");
565 fprintf(fout, "#endif /* __cplusplus */\n");
566
567 if (tirpcflag)
568 fprintf(fout, "#include <unistd.h> /* setsid */\n");
569 }
570 if (tirpcflag)
571 fprintf(fout, "#include <sys/types.h>\n");
572
573 fprintf(fout, "#include <memory.h>\n");
574 if (tirpcflag)
575 fprintf(fout, "#include <stropts.h>\n");
576
577 if (inetdflag || !tirpcflag) {
578 fprintf(fout, "#include <sys/socket.h>\n");
579 fprintf(fout, "#include <netinet/in.h>\n");
580 }
581 if ((netflag || pmflag) && tirpcflag) {
582 fprintf(fout, "#include <netconfig.h>\n");
583 }
584 if (/* timerflag && */ tirpcflag)
585 fprintf(fout, "#include <sys/resource.h> /* rlimit */\n");
586 if (logflag || inetdflag || pmflag) {
587 fprintf(fout, "#include <syslog.h>\n");
588 fprintf(fout, "#include <errno.h>\n");
589 }
590 /* for ANSI-C */
591 fprintf(fout, "\n#ifdef __STDC__\n#define SIG_PF void(*)(int)\n#endif\n");
592
593 fprintf(fout, "\n#ifdef DEBUG\n#define RPC_SVC_FG\n#endif\n");
594 if (timerflag)
595 fprintf(fout, "\n#define _RPCSVC_CLOSEDOWN %s\n", svcclosetime);
596 while ((def = get_definition())) {
597 foundprogram |= (def->def_kind == DEF_PROGRAM);
598 }
599 if (extend && !foundprogram) {
600 (void) unlink(outfilename);
601 return;
602 }
603 if (callerflag) /* EVAS */
604 fprintf(fout, "\nstatic SVCXPRT *caller;\n"); /* EVAS */
605 write_most(infile, netflag, nomain);
606 if (!nomain) {
607 if (!do_registers(argc, argv)) {
608 if (outfilename)
609 (void) unlink(outfilename);
610 usage();
611 }
612 write_rest();
613 }
614}
615
616/*
617 * generate client side stubs
618 */
619static void
620l_output(infile, define, extend, outfile)
621 char *infile;
622 char *define;
623 int extend;
624 char *outfile;
625{
626 char *include;
627 definition *def;
628 int foundprogram = 0;
629 char *outfilename;
630
631 open_input(infile, define);
632 outfilename = extend ? extendfile(infile, outfile) : outfile;
633 open_output(infile, outfilename);
634 add_warning();
635 if (Cflag)
636 fprintf(fout, "#include <memory.h> /* for memset */\n");
637 if (infile && (include = extendfile(infile, ".h"))) {
638 fprintf(fout, "#include \"%s\"\n", include);
639 free(include);
640 } else
641 fprintf(fout, "#include <rpc/rpc.h>\n");
642 while ((def = get_definition()))
643 foundprogram |= (def->def_kind == DEF_PROGRAM);
644
645 if (extend && !foundprogram) {
646 (void) unlink(outfilename);
647 return;
648 }
649 write_stubs();
650}
651
652/*
653 * generate the dispatch table
654 */
655static void
656t_output(infile, define, extend, outfile)
657 char *infile;
658 char *define;
659 int extend;
660 char *outfile;
661{
662 definition *def;
663 int foundprogram = 0;
664 char *outfilename;
665
666 open_input(infile, define);
667 outfilename = extend ? extendfile(infile, outfile) : outfile;
668 open_output(infile, outfilename);
669 add_warning();
670 while ((def = get_definition()))
671 foundprogram |= (def->def_kind == DEF_PROGRAM);
672
673 if (extend && !foundprogram) {
674 (void) unlink(outfilename);
675 return;
676 }
677 write_tables();
678}
679
680/* sample routine for the server template */
681static void
682svc_output(infile, define, extend, outfile)
683 char *infile;
684 char *define;
685 int extend;
686 char *outfile;
687{
688 definition *def;
689 char *include;
690 char *outfilename;
691 long tell;
692
693 open_input(infile, define);
694 outfilename = extend ? extendfile(infile, outfile) : outfile;
695 checkfiles(infile, outfilename); /* check if outfile already
696 * exists. if so, print an
697 * error message and exit */
698 open_output(infile, outfilename);
699 add_sample_msg();
700
701 if (infile && (include = extendfile(infile, ".h"))) {
702 fprintf(fout, "#include \"%s\"\n", include);
703 free(include);
704 } else
705 fprintf(fout, "#include <rpc/rpc.h>\n");
706
707 tell = ftell(fout);
708 while ((def = get_definition()))
709 write_sample_svc(def);
710
711 if (extend && tell == ftell(fout))
712 (void) unlink(outfilename);
713}
714
715
716/* sample main routine for client */
717static void
718clnt_output(infile, define, extend, outfile)
719 char *infile;
720 char *define;
721 int extend;
722 char *outfile;
723{
724 definition *def;
725 char *include, *outfilename;
726 long tell;
727 int has_program = 0;
728
729 open_input(infile, define);
730 outfilename = extend ? extendfile(infile, outfile) : outfile;
731
732 /*
733 * check if outfile already exists. if so,
734 * print an error message and exit
735 */
736 checkfiles(infile, outfilename);
737
738 open_output(infile, outfilename);
739 add_sample_msg();
740 if (infile && (include = extendfile(infile, ".h"))) {
741 fprintf(fout, "#include \"%s\"\n", include);
742 free(include);
743 } else
744 fprintf(fout, "#include <rpc/rpc.h>\n");
745 tell = ftell(fout);
746 while ((def = get_definition()))
747 has_program += write_sample_clnt(def);
748
749 if (has_program)
750 write_sample_clnt_main();
751
752 if (extend && tell == ftell(fout))
753 (void) unlink(outfilename);
754}
755
756/*
757 * Perform registrations for service output
758 * Return 0 if failed; 1 otherwise.
759 */
760static int
761do_registers(argc, argv)
762 int argc;
763 char *argv[];
764{
765 int i;
766
767 if (inetdflag || !tirpcflag) {
768 for (i = 1; i < argc; i++) {
769 if (streq(argv[i], "-s")) {
770 if (!check_nettype(argv[i + 1], valid_i_nettypes))
771 return 0;
772 write_inetd_register(argv[i + 1]);
773 i++;
774 }
775 }
776 } else {
777 for (i = 1; i < argc; i++)
778 if (streq(argv[i], "-s")) {
779 if (!check_nettype(argv[i + 1], valid_ti_nettypes))
780 return 0;
781 write_nettype_register(argv[i + 1]);
782 i++;
783 } else if (streq(argv[i], "-n")) {
784 write_netid_register(argv[i + 1]);
785 i++;
786 }
787 }
788 return 1;
789}
790
791/*
792 * Add another argument to the arg list
793 */
794static void
795addarg(cp)
796 char *cp;
797{
798 if (argcount >= ARGLISTLEN20) {
799 fprintf(stderr(&__sF[2]), "rpcgen: too many defines\n");
800 crash();
801 /* NOTREACHED */
802 }
803 arglist[argcount++] = cp;
804
805}
806
807static void
808putarg(where, cp)
809 char *cp;
810 int where;
811{
812 if (where >= ARGLISTLEN20) {
813 fprintf(stderr(&__sF[2]), "rpcgen: arglist coding error\n");
814 crash();
815 /* NOTREACHED */
816 }
817 arglist[where] = cp;
818}
819
820/*
821 * if input file is stdin and an output file is specified then complain
822 * if the file already exists. Otherwise the file may get overwritten
823 * If input file does not exist, exit with an error
824 */
825static void
826checkfiles(infile, outfile)
827 char *infile;
828 char *outfile;
829{
830 struct stat buf;
831
832 if (infile) /* infile ! = NULL */
833 if (stat(infile, &buf) == -1) {
834 perror(infile);
835 crash();
836 }
837#if 0
838 if (outfile) {
839 if (stat(outfile, &buf) == -1)
840 return; /* file does not exist */
841 else {
842 fprintf(stderr(&__sF[2]),
843 "file '%s' already exists and may be overwritten\n",
844 outfile);
845 crash();
846 }
847 }
848#endif
849}
850
851/*
852 * Parse command line arguments
853 */
854static int
855parseargs(argc, argv, cmd)
856 int argc;
857 char *argv[];
858 struct commandline *cmd;
859{
860 int i, j, nflags;
861 char c, flag[(1 << 8 * sizeof(char))];
862
863 cmdname = argv[0];
864 cmd->infile = cmd->outfile = NULL((void *)0);
865 if (argc < 2)
866 return (0);
867
868 allfiles = 0;
869 flag['c'] = 0;
870 flag['h'] = 0;
871 flag['l'] = 0;
872 flag['m'] = 0;
873 flag['o'] = 0;
874 flag['s'] = 0;
875 flag['n'] = 0;
876 flag['t'] = 0;
877 flag['S'] = 0;
878 flag['C'] = 0;
879 for (i = 1; i < argc; i++) {
880 if (argv[i][0] != '-') {
881 if (cmd->infile) {
882 fprintf(stderr(&__sF[2]),
883 "Cannot specify more than one input file!\n");
884 return (0);
885 }
886 cmd->infile = argv[i];
887 } else {
888 for (j = 1; argv[i][j] != 0; j++) {
889 c = argv[i][j];
890 switch (c) {
891 case 'A':
892 callerflag = 1;
893 break;
894 case 'a':
895 allfiles = 1;
896 break;
897 case 'c':
898 case 'h':
899 case 'l':
900 case 'm':
901 case 't':
902 if (flag[(unsigned char)c])
903 return (0);
904 flag[(unsigned char)c] = 1;
905 break;
906 case 'S':
907 /*
908 * sample flag: Ss or Sc. Ss means
909 * set flag['S']; Sc means set
910 * flag['C'];
911 */
912 c = argv[i][++j]; /* get next char */
913 if (c == 's')
914 c = 'S';
915 else if (c == 'c')
916 c = 'C';
917 else
918 return (0);
919
920 if (flag[(unsigned char)c])
921 return (0);
922 flag[(unsigned char)c] = 1;
923 break;
924 case 'C': /* ANSI C syntax */
925 Cflag = 1;
926 break;
927
928 case 'b':
929 /*
930 * turn TIRPC flag off for
931 * generating backward compatible
932 */
933 tirpcflag = 0;
934 break;
935
936 case 'I':
937 inetdflag = 1;
938 break;
939 case 'N':
940 newstyle = 1;
941 break;
942 case 'L':
943 logflag = 1;
944 break;
945 case 'K':
946 if (++i == argc)
947 return (0);
948 svcclosetime = argv[i];
949 goto nextarg;
950 case 'T':
951 tblflag = 1;
952 break;
953 case 'i':
954 if (++i == argc)
955 return (0);
956 doinline = atoi(argv[i]);
957 goto nextarg;
958 case 'n':
959 case 'o':
960 case 's':
961 if (argv[i][j - 1] != '-' ||
962 argv[i][j + 1] != 0)
963 return (0);
964 flag[(unsigned char)c] = 1;
965 if (++i == argc)
966 return (0);
967 if (c == 's') {
968 if (!streq(argv[i], "udp") &&
969 !streq(argv[i], "tcp"))
970 return (0);
971 } else if (c == 'o') {
972 if (cmd->outfile)
973 return (0);
974 cmd->outfile = argv[i];
975 }
976 goto nextarg;
977 case 'D':
978 if (argv[i][j - 1] != '-')
979 return (0);
980 (void) addarg(argv[i]);
981 goto nextarg;
982 case 'Y':
983 if (++i == argc)
984 return (0);
985 if (snprintf(pathbuf, sizeof pathbuf,
986 "%s/cpp", argv[i]) >= sizeof pathbuf)
987 usage();
988 CPP = pathbuf;
989 cppDefined = 1;
990 goto nextarg;
991 default:
992 return (0);
993 }
994 }
995 nextarg:
996 ;
997 }
998 }
999
1000 cmd->cflag = flag['c'];
1001 cmd->hflag = flag['h'];
1002 cmd->lflag = flag['l'];
1003 cmd->mflag = flag['m'];
1004 cmd->nflag = flag['n'];
1005 cmd->sflag = flag['s'];
1006 cmd->tflag = flag['t'];
1007 cmd->Ssflag = flag['S'];
1008 cmd->Scflag = flag['C'];
1009
1010 if (tirpcflag) {
1011 pmflag = inetdflag ? 0 : 1; /* pmflag or inetdflag is
1012 * always TRUE */
1013 if (inetdflag && cmd->nflag) {
1014 /* netid not allowed with inetdflag */
1015 fprintf(stderr(&__sF[2]), "Cannot use netid flag with inetd flag!\n");
1016 return (0);
1017 }
1018 } else {
1019 /* 4.1 mode */
1020 pmflag = 0; /* set pmflag only in tirpcmode */
1021 inetdflag = 1; /* inetdflag is TRUE by default */
1022 if (cmd->nflag) {
1023 /* netid needs TIRPC */
1024 fprintf(stderr(&__sF[2]), "Cannot use netid flag without TIRPC!\n");
1025 return (0);
1026 }
1027 }
1028
1029 if (newstyle && (tblflag || cmd->tflag)) {
1030 fprintf(stderr(&__sF[2]), "Cannot use table flags with newstyle!\n");
1031 return (0);
1032 }
1033 /* check no conflicts with file generation flags */
1034 nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag +
1035 cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag + cmd->Scflag;
1036
1037 if (nflags == 0) {
1038 if (cmd->outfile != NULL((void *)0) || cmd->infile == NULL((void *)0))
1039 return (0);
1040 } else if (nflags > 1) {
1041 fprintf(stderr(&__sF[2]), "Cannot have more than one file generation flag!\n");
1042 return (0);
1043 }
1044 return (1);
1045}
1046
1047static void
1048usage(void)
1049{
1050 fprintf(stderr(&__sF[2]), "usage: %s [-abACILNT] [-Dname[=value]] [-i lines] "
1051 "[-K seconds] infile\n", cmdname);
1052 fprintf(stderr(&__sF[2]), " %s [-c | -h | -l | -m | -t | -Sc | -Ss] "
1053 "[-o outfile] [infile]\n", cmdname);
1054 fprintf(stderr(&__sF[2]), " %s [-s nettype]* [-o outfile] [infile]\n", cmdname);
1055 exit(1);
1056}