clang -cc1 -cc1 -triple amd64-unknown-openbsd7.4 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name rpc_main.c -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 -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -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/llvm16/lib/clang/16 -internal-isystem /usr/local/llvm16/lib/clang/16/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 -fcf-protection=branch -fno-jump-tables -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/scan/2024-01-11-140451-98009-1 -x c /usr/src/usr.bin/rpcgen/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 | #define RPCGEN_VERSION "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 EXTEND 1 /* alias for TRUE */ |
| 53 | #define DONT_EXTEND 0 /* alias for FALSE */ |
| 54 | |
| 55 | #define SVR4_CPP "/usr/ccs/lib/cpp" |
| 56 | #define SUNOS_CPP "/lib/cpp" |
| 57 | static int cppDefined = 0; |
| 58 | |
| 59 | struct commandline { |
| 60 | int cflag; |
| 61 | int hflag; |
| 62 | int lflag; |
| 63 | int mflag; |
| 64 | int nflag; |
| 65 | int sflag; |
| 66 | int tflag; |
| 67 | int Ssflag; |
| 68 | int Scflag; |
| 69 | char *infile; |
| 70 | char *outfile; |
| 71 | }; |
| 72 | |
| 73 | static char *cmdname; |
| 74 | |
| 75 | static char *svcclosetime = "120"; |
| 76 | static char *CPP = "/usr/bin/cpp"; |
| 77 | static char CPPFLAGS[] = "-C"; |
| 78 | static char pathbuf[PATH_MAX]; |
| 79 | static char *allv[] = { |
| 80 | "rpcgen", "-s", "udp", "-s", "tcp", |
| 81 | }; |
| 82 | static int allc = sizeof(allv) / sizeof(allv[0]); |
| 83 | static char *allnv[] = { |
| 84 | "rpcgen", "-s", "netpath", |
| 85 | }; |
| 86 | static int allnc = sizeof(allnv) / sizeof(allnv[0]); |
| 87 | |
| 88 | #define ARGLISTLEN 20 |
| 89 | #define FIXEDARGS 2 |
| 90 | |
| 91 | static char *arglist[ARGLISTLEN]; |
| 92 | static int argcount = FIXEDARGS; |
| 93 | |
| 94 | |
| 95 | int nonfatalerrors; |
| 96 | int inetdflag ; |
| 97 | |
| 98 | int pmflag; |
| 99 | int logflag; |
| 100 | int tblflag; |
| 101 | int callerflag; |
| 102 | |
| 103 | #define INLINE 3 |
| 104 | |
| 105 | |
| 106 | int doinline = INLINE; |
| 107 | |
| 108 | |
| 109 | |
| 110 | int indefinitewait; |
| 111 | |
| 112 | int exitnow; |
| 113 | |
| 114 | int timerflag; |
| 115 | int newstyle; |
| 116 | int Cflag = 0; |
| 117 | static int allfiles; |
| 118 | int tirpcflag = 0; |
| 119 | |
| 120 | static void c_output(char *, char *, int, char *); |
| 121 | static void h_output(char *, char *, int, char *); |
| 122 | static void s_output(int, char **, char *, char *, int, char *, int, int); |
| 123 | static void l_output(char *, char *, int, char *); |
| 124 | static void t_output(char *, char *, int, char *); |
| 125 | static void svc_output(char *, char *, int, char *); |
| 126 | static void clnt_output(char *, char *, int, char *); |
| 127 | static int do_registers(int, char **); |
| 128 | static void addarg(char *); |
| 129 | static void putarg(int, char *); |
| 130 | static void clear_args(void); |
| 131 | static void checkfiles(char *, char *); |
| 132 | static int parseargs(int, char **, struct commandline *); |
| 133 | static void usage(void); |
| 134 | void c_initialize(void); |
| 135 | |
| 136 | int |
| 137 | main(int argc, char *argv[]) |
| 138 | { |
| 139 | struct commandline cmd; |
| 140 | |
| 141 | if (pledge("stdio rpath wpath cpath proc exec", NULL) == -1) { |
| 1 | Assuming the condition is false | |
|
| |
| 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)) |
| 149 | usage(); |
| 150 | |
| 151 | if (cmd.cflag || cmd.hflag || cmd.lflag || cmd.tflag || cmd.sflag || |
| |
| 152 | cmd.mflag || cmd.nflag || cmd.Ssflag || cmd.Scflag) { |
| 153 | checkfiles(cmd.infile, cmd.outfile); |
| 154 | } else |
| 155 | checkfiles(cmd.infile, NULL); |
| 156 | |
| 157 | if (cmd.cflag) { |
| |
| 158 | c_output(cmd.infile, "-DRPC_XDR", DONT_EXTEND, cmd.outfile); |
| 159 | } else if (cmd.hflag) { |
| |
| 160 | h_output(cmd.infile, "-DRPC_HDR", DONT_EXTEND, cmd.outfile); |
| 161 | } else if (cmd.lflag) { |
| 162 | l_output(cmd.infile, "-DRPC_CLNT", DONT_EXTEND, cmd.outfile); |
| 163 | } else if (cmd.sflag || cmd.mflag || (cmd.nflag)) { |
| |
| 164 | s_output(argc, argv, cmd.infile, "-DRPC_SVC", DONT_EXTEND, |
| 165 | cmd.outfile, cmd.mflag, cmd.nflag); |
| 166 | } else if (cmd.tflag) { |
| |
| 167 | t_output(cmd.infile, "-DRPC_TBL", DONT_EXTEND, cmd.outfile); |
| 168 | } else if (cmd.Ssflag) { |
| |
| 169 | svc_output(cmd.infile, "-DRPC_SERVER", DONT_EXTEND, cmd.outfile); |
| 170 | } else if (cmd.Scflag) { |
| |
| 171 | clnt_output(cmd.infile, "-DRPC_CLIENT", DONT_EXTEND, cmd.outfile); |
| 172 | } else { |
| 173 | |
| 174 | c_output(cmd.infile, "-DRPC_XDR", EXTEND, "_xdr.c"); |
| 175 | reinitialize(); |
| 176 | h_output(cmd.infile, "-DRPC_HDR", EXTEND, ".h"); |
| |
| 177 | reinitialize(); |
| 178 | l_output(cmd.infile, "-DRPC_CLNT", EXTEND, "_clnt.c"); |
| 179 | reinitialize(); |
| 180 | if (inetdflag || !tirpcflag) |
| 181 | s_output(allc, allv, cmd.infile, "-DRPC_SVC", EXTEND, |
| 182 | "_svc.c", cmd.mflag, cmd.nflag); |
| 183 | else |
| 184 | s_output(allnc, allnv, cmd.infile, "-DRPC_SVC", |
| 185 | EXTEND, "_svc.c", cmd.mflag, cmd.nflag); |
| 186 | if (tblflag) { |
| 187 | reinitialize(); |
| 188 | t_output(cmd.infile, "-DRPC_TBL", EXTEND, "_tbl.i"); |
| 189 | } |
| 190 | if (allfiles) { |
| 191 | reinitialize(); |
| 192 | svc_output(cmd.infile, "-DRPC_SERVER", EXTEND, "_server.c"); |
| 193 | } |
| 194 | if (allfiles) { |
| 195 | reinitialize(); |
| 196 | clnt_output(cmd.infile, "-DRPC_CLIENT", EXTEND, "_client.c"); |
| 197 | } |
| 198 | } |
| 199 | exit(nonfatalerrors); |
| 200 | |
| 201 | } |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | static char * |
| 207 | extendfile(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) |
| 215 | file = path; |
| 216 | else |
| 217 | file++; |
| 218 | |
| 219 | len = strlen(file) + strlen(ext) + 1; |
| 220 | res = malloc(len); |
| 221 | if (res == NULL) { |
| 222 | fprintf(stderr, "could not allocate memory\n"); |
| 223 | exit(1); |
| 224 | } |
| 225 | p = strrchr(file, '.'); |
| 226 | if (p == NULL) |
| 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 | |
| 235 | |
| 236 | static void |
| 237 | open_output(char *infile, char *outfile) |
| 238 | { |
| 239 | |
| 240 | if (outfile == NULL) { |
| 241 | fout = stdout; |
| 242 | return; |
| 243 | } |
| 244 | if (infile != NULL && streq(outfile, infile)) { |
| 245 | fprintf(stderr, "%s: output would overwrite %s\n", cmdname, |
| 246 | infile); |
| 247 | crash(); |
| 248 | } |
| 249 | fout = fopen(outfile, "w"); |
| 250 | if (fout == NULL) { |
| 251 | fprintf(stderr, "%s: unable to open ", cmdname); |
| 252 | perror(outfile); |
| 253 | crash(); |
| 254 | } |
| 255 | record_open(outfile); |
| 256 | |
| 257 | } |
| 258 | |
| 259 | static void |
| 260 | add_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 | |
| 269 | static void |
| 270 | clear_args(void) |
| 271 | { |
| 272 | int i; |
| 273 | for (i = FIXEDARGS; i < ARGLISTLEN; i++) |
| 274 | arglist[i] = NULL; |
| 275 | argcount = FIXEDARGS; |
| 276 | } |
| 277 | |
| 278 | |
| 279 | static void |
| 280 | find_cpp(void) |
| 281 | { |
| 282 | struct stat buf; |
| 283 | |
| 284 | |
| 285 | if (stat(CPP, &buf) == -1) { |
| 286 | if (cppDefined) { |
| 287 | fprintf(stderr, "cannot find C preprocessor: %s \n", CPP); |
| 288 | crash(); |
| 289 | } else { |
| 290 | |
| 291 | CPP = SUNOS_CPP; |
| 292 | if (stat(CPP, &buf) == -1) { |
| 293 | fprintf(stderr, |
| 294 | "cannot find any C preprocessor: %s\n", CPP); |
| 295 | crash(); |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | static void |
| 305 | open_input(char *infile, char *define) |
| 306 | { |
| 307 | int pd[2]; |
| 308 | |
| 309 | infilename = (infile == NULL) ? "<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); |
| 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) { |
| 332 | fprintf(stderr, "%s: ", cmdname); |
| 333 | perror(infilename); |
| 334 | crash(); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | |
| 339 | static 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 |
| 350 | }; |
| 351 | |
| 352 | |
| 353 | static char *valid_i_nettypes[] = { |
| 354 | "udp", |
| 355 | "tcp", |
| 356 | NULL |
| 357 | }; |
| 358 | |
| 359 | static int |
| 360 | check_nettype(char *name, char *list_to_check[]) |
| 361 | { |
| 362 | int i; |
| 363 | for (i = 0; list_to_check[i] != NULL; i++) { |
| 364 | if (strcmp(name, list_to_check[i]) == 0) |
| 365 | return 1; |
| 366 | } |
| 367 | fprintf(stderr, "illegal nettype :\'%s\'\n", name); |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | static void |
| 376 | c_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 | |
| 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 | |
| 408 | void |
| 409 | c_initialize(void) |
| 410 | { |
| 411 | |
| 412 | |
| 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 | |
| 425 | static 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 | |
| 434 | static char * |
| 435 | generate_guard(char *pathname) |
| 436 | { |
| 437 | char *filename, *guard, *tmp, *tmp2; |
| 438 | |
| 439 | filename = strrchr(pathname, '/'); |
| 440 | filename = ((filename == 0) ? pathname : filename + 1); |
| 14 | | Assuming 'filename' is not equal to null | |
|
| |
| 441 | guard = strdup(filename); |
| 16 | | Value assigned to 'guard' | |
|
| 442 | if (guard == NULL) { |
| 17 | | Assuming 'guard' is equal to NULL | |
|
| |
| 443 | fprintf(stderr, "out of memory while processing %s\n", filename); |
| 444 | crash(); |
| 445 | } |
| 446 | |
| 447 | |
| 448 | tmp = guard; |
| 19 | | Null pointer value stored to 'tmp' | |
|
| 449 | while (*tmp) { |
| 20 | | 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 | |
| 464 | |
| 465 | |
| 466 | static void |
| 467 | h_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 ? extendfile(infile, outfile) : outfile; |
| |
| 481 | open_output(infile, outfilename); |
| 482 | add_warning(); |
| 483 | guard = generate_guard(outfilename ? outfilename : infile); |
| |
| 13 | | 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); |
| 489 | fprintf(fout, "#include <rpc/rpc.h>\n\n"); |
| 490 | |
| 491 | tell = ftell(fout); |
| 492 | |
| 493 | while ((def = get_definition())) { |
| 494 | print_datadef(def); |
| 495 | } |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | |
| 501 | for (l = defined; l != NULL; 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 | |
| 516 | |
| 517 | static void |
| 518 | s_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"); |
| 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 ( 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 | |
| 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) |
| 604 | fprintf(fout, "\nstatic SVCXPRT *caller;\n"); |
| 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 | |
| 618 | |
| 619 | static void |
| 620 | l_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 | |
| 654 | |
| 655 | static void |
| 656 | t_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 | |
| 681 | static void |
| 682 | svc_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); |
| 696 | |
| 697 | |
| 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 | |
| 717 | static void |
| 718 | clnt_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 | |
| 734 | |
| 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 | |
| 758 | |
| 759 | |
| 760 | static int |
| 761 | do_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 | |
| 793 | |
| 794 | static void |
| 795 | addarg(cp) |
| 796 | char *cp; |
| 797 | { |
| 798 | if (argcount >= ARGLISTLEN) { |
| 799 | fprintf(stderr, "rpcgen: too many defines\n"); |
| 800 | crash(); |
| 801 | |
| 802 | } |
| 803 | arglist[argcount++] = cp; |
| 804 | |
| 805 | } |
| 806 | |
| 807 | static void |
| 808 | putarg(where, cp) |
| 809 | char *cp; |
| 810 | int where; |
| 811 | { |
| 812 | if (where >= ARGLISTLEN) { |
| 813 | fprintf(stderr, "rpcgen: arglist coding error\n"); |
| 814 | crash(); |
| 815 | |
| 816 | } |
| 817 | arglist[where] = cp; |
| 818 | } |
| 819 | |
| 820 | |
| 821 | |
| 822 | |
| 823 | |
| 824 | |
| 825 | static void |
| 826 | checkfiles(infile, outfile) |
| 827 | char *infile; |
| 828 | char *outfile; |
| 829 | { |
| 830 | struct stat buf; |
| 831 | |
| 832 | if (infile) |
| 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; |
| 841 | else { |
| 842 | fprintf(stderr, |
| 843 | "file '%s' already exists and may be overwritten\n", |
| 844 | outfile); |
| 845 | crash(); |
| 846 | } |
| 847 | } |
| 848 | #endif |
| 849 | } |
| 850 | |
| 851 | |
| 852 | |
| 853 | |
| 854 | static int |
| 855 | parseargs(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; |
| 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, |
| 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 | |
| 909 | |
| 910 | |
| 911 | |
| 912 | c = argv[i][++j]; |
| 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': |
| 925 | Cflag = 1; |
| 926 | break; |
| 927 | |
| 928 | case 'b': |
| 929 | |
| 930 | |
| 931 | |
| 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; |
| 1012 | |
| 1013 | if (inetdflag && cmd->nflag) { |
| 1014 | |
| 1015 | fprintf(stderr, "Cannot use netid flag with inetd flag!\n"); |
| 1016 | return (0); |
| 1017 | } |
| 1018 | } else { |
| 1019 | |
| 1020 | pmflag = 0; |
| 1021 | inetdflag = 1; |
| 1022 | if (cmd->nflag) { |
| 1023 | |
| 1024 | fprintf(stderr, "Cannot use netid flag without TIRPC!\n"); |
| 1025 | return (0); |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | if (newstyle && (tblflag || cmd->tflag)) { |
| 1030 | fprintf(stderr, "Cannot use table flags with newstyle!\n"); |
| 1031 | return (0); |
| 1032 | } |
| 1033 | |
| 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 || cmd->infile == NULL) |
| 1039 | return (0); |
| 1040 | } else if (nflags > 1) { |
| 1041 | fprintf(stderr, "Cannot have more than one file generation flag!\n"); |
| 1042 | return (0); |
| 1043 | } |
| 1044 | return (1); |
| 1045 | } |
| 1046 | |
| 1047 | static void |
| 1048 | usage(void) |
| 1049 | { |
| 1050 | fprintf(stderr, "usage: %s [-abACILNT] [-Dname[=value]] [-i lines] " |
| 1051 | "[-K seconds] infile\n", cmdname); |
| 1052 | fprintf(stderr, " %s [-c | -h | -l | -m | -t | -Sc | -Ss] " |
| 1053 | "[-o outfile] [infile]\n", cmdname); |
| 1054 | fprintf(stderr, " %s [-s nettype]* [-o outfile] [infile]\n", cmdname); |
| 1055 | exit(1); |
| 1056 | } |