1 /*- 2 * Copyright 1986, Larry Wall 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following condition is met: 6 * 1. Redistributions of source code must retain the above copyright notice, 7 * this condition and the following disclaimer. 8 * 9 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY 10 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 11 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 12 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 13 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 14 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 15 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 16 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 17 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 18 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 19 * SUCH DAMAGE. 20 * 21 * patch - a program to apply diffs to original files 22 * 23 * -C option added in 1998, original code by Marc Espie, based on FreeBSD 24 * behaviour 25 * 26 * $OpenBSD: patch.c,v 1.54 2014/12/13 10:31:07 tobias Exp $ 27 * $FreeBSD$ 28 * 29 */ 30 31 #include <sys/types.h> 32 #include <sys/stat.h> 33 34 #include <ctype.h> 35 #include <getopt.h> 36 #include <limits.h> 37 #include <stdio.h> 38 #include <string.h> 39 #include <stdlib.h> 40 #include <unistd.h> 41 42 #include "common.h" 43 #include "util.h" 44 #include "pch.h" 45 #include "inp.h" 46 #include "backupfile.h" 47 #include "pathnames.h" 48 49 mode_t filemode = 0644; 50 51 char *buf; /* general purpose buffer */ 52 size_t buf_size; /* size of the general purpose buffer */ 53 54 bool using_plan_a = true; /* try to keep everything in memory */ 55 bool out_of_mem = false; /* ran out of memory in plan a */ 56 57 #define MAXFILEC 2 58 59 char *filearg[MAXFILEC]; 60 bool ok_to_create_file = false; 61 char *outname = NULL; 62 char *origprae = NULL; 63 char *TMPOUTNAME; 64 char *TMPINNAME; 65 char *TMPREJNAME; 66 char *TMPPATNAME; 67 bool toutkeep = false; 68 bool trejkeep = false; 69 bool warn_on_invalid_line; 70 bool last_line_missing_eol; 71 72 #ifdef DEBUGGING 73 int debug = 0; 74 #endif 75 76 bool force = false; 77 bool batch = false; 78 bool verbose = true; 79 bool reverse = false; 80 bool noreverse = false; 81 bool skip_rest_of_patch = false; 82 int strippath = 957; 83 bool canonicalize = false; 84 bool check_only = false; 85 int diff_type = 0; 86 char *revision = NULL; /* prerequisite revision, if any */ 87 LINENUM input_lines = 0; /* how long is input file in lines */ 88 int posix = 0; /* strict POSIX mode? */ 89 90 static void reinitialize_almost_everything(void); 91 static void get_some_switches(void); 92 static LINENUM locate_hunk(LINENUM); 93 static void abort_context_hunk(void); 94 static void rej_line(int, LINENUM); 95 static void abort_hunk(void); 96 static void apply_hunk(LINENUM); 97 static void init_output(const char *); 98 static void init_reject(const char *); 99 static void copy_till(LINENUM, bool); 100 static bool spew_output(void); 101 static void dump_line(LINENUM, bool); 102 static bool patch_match(LINENUM, LINENUM, LINENUM); 103 static bool similar(const char *, const char *, int); 104 static void usage(void); 105 106 /* true if -E was specified on command line. */ 107 static bool remove_empty_files = false; 108 109 /* true if -R was specified on command line. */ 110 static bool reverse_flag_specified = false; 111 112 static bool Vflag = false; 113 114 /* buffer holding the name of the rejected patch file. */ 115 static char rejname[NAME_MAX + 1]; 116 117 /* how many input lines have been irretractibly output */ 118 static LINENUM last_frozen_line = 0; 119 120 static int Argc; /* guess */ 121 static char **Argv; 122 static int Argc_last; /* for restarting plan_b */ 123 static char **Argv_last; 124 125 static FILE *ofp = NULL; /* output file pointer */ 126 static FILE *rejfp = NULL; /* reject file pointer */ 127 128 static int filec = 0; /* how many file arguments? */ 129 static LINENUM last_offset = 0; 130 static LINENUM maxfuzz = 2; 131 132 /* patch using ifdef, ifndef, etc. */ 133 static bool do_defines = false; 134 /* #ifdef xyzzy */ 135 static char if_defined[128]; 136 /* #ifndef xyzzy */ 137 static char not_defined[128]; 138 /* #else */ 139 static const char else_defined[] = "#else\n"; 140 /* #endif xyzzy */ 141 static char end_defined[128]; 142 143 144 /* Apply a set of diffs as appropriate. */ 145 146 int 147 main(int argc, char *argv[]) 148 { 149 int error = 0, hunk, failed, i, fd; 150 bool patch_seen, reverse_seen; 151 LINENUM where = 0, newwhere, fuzz, mymaxfuzz; 152 const char *tmpdir; 153 char *v; 154 155 setvbuf(stdout, NULL, _IOLBF, 0); 156 setvbuf(stderr, NULL, _IOLBF, 0); 157 for (i = 0; i < MAXFILEC; i++) 158 filearg[i] = NULL; 159 160 buf_size = INITLINELEN; 161 buf = malloc((unsigned)(buf_size)); 162 if (buf == NULL) 163 fatal("out of memory\n"); 164 165 /* Cons up the names of the temporary files. */ 166 if ((tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0') 167 tmpdir = _PATH_TMP; 168 for (i = strlen(tmpdir) - 1; i > 0 && tmpdir[i] == '/'; i--) 169 ; 170 i++; 171 if (asprintf(&TMPOUTNAME, "%.*s/patchoXXXXXXXXXX", i, tmpdir) == -1) 172 fatal("cannot allocate memory"); 173 if ((fd = mkstemp(TMPOUTNAME)) < 0) 174 pfatal("can't create %s", TMPOUTNAME); 175 close(fd); 176 177 if (asprintf(&TMPINNAME, "%.*s/patchiXXXXXXXXXX", i, tmpdir) == -1) 178 fatal("cannot allocate memory"); 179 if ((fd = mkstemp(TMPINNAME)) < 0) 180 pfatal("can't create %s", TMPINNAME); 181 close(fd); 182 183 if (asprintf(&TMPREJNAME, "%.*s/patchrXXXXXXXXXX", i, tmpdir) == -1) 184 fatal("cannot allocate memory"); 185 if ((fd = mkstemp(TMPREJNAME)) < 0) 186 pfatal("can't create %s", TMPREJNAME); 187 close(fd); 188 189 if (asprintf(&TMPPATNAME, "%.*s/patchpXXXXXXXXXX", i, tmpdir) == -1) 190 fatal("cannot allocate memory"); 191 if ((fd = mkstemp(TMPPATNAME)) < 0) 192 pfatal("can't create %s", TMPPATNAME); 193 close(fd); 194 195 v = getenv("SIMPLE_BACKUP_SUFFIX"); 196 if (v) 197 simple_backup_suffix = v; 198 else 199 simple_backup_suffix = ORIGEXT; 200 201 /* parse switches */ 202 Argc = argc; 203 Argv = argv; 204 get_some_switches(); 205 206 if (!Vflag) { 207 if ((v = getenv("PATCH_VERSION_CONTROL")) == NULL) 208 v = getenv("VERSION_CONTROL"); 209 if (v != NULL || !posix) 210 backup_type = get_version(v); /* OK to pass NULL. */ 211 } 212 213 /* make sure we clean up /tmp in case of disaster */ 214 set_signals(0); 215 216 patch_seen = false; 217 for (open_patch_file(filearg[1]); there_is_another_patch(); 218 reinitialize_almost_everything()) { 219 /* for each patch in patch file */ 220 221 patch_seen = true; 222 223 warn_on_invalid_line = true; 224 225 if (outname == NULL) 226 outname = xstrdup(filearg[0]); 227 228 /* for ed script just up and do it and exit */ 229 if (diff_type == ED_DIFF) { 230 do_ed_script(); 231 continue; 232 } 233 /* initialize the patched file */ 234 if (!skip_rest_of_patch) 235 init_output(TMPOUTNAME); 236 237 /* initialize reject file */ 238 init_reject(TMPREJNAME); 239 240 /* find out where all the lines are */ 241 if (!skip_rest_of_patch) 242 scan_input(filearg[0]); 243 244 /* 245 * from here on, open no standard i/o files, because 246 * malloc might misfire and we can't catch it easily 247 */ 248 249 /* apply each hunk of patch */ 250 hunk = 0; 251 failed = 0; 252 reverse_seen = false; 253 out_of_mem = false; 254 while (another_hunk()) { 255 hunk++; 256 fuzz = 0; 257 mymaxfuzz = pch_context(); 258 if (maxfuzz < mymaxfuzz) 259 mymaxfuzz = maxfuzz; 260 if (!skip_rest_of_patch) { 261 do { 262 where = locate_hunk(fuzz); 263 if (hunk == 1 && where == 0 && !force && !reverse_seen) { 264 /* dwim for reversed patch? */ 265 if (!pch_swap()) { 266 if (fuzz == 0) 267 say("Not enough memory to try swapped hunk! Assuming unswapped.\n"); 268 continue; 269 } 270 reverse = !reverse; 271 /* try again */ 272 where = locate_hunk(fuzz); 273 if (where == 0) { 274 /* didn't find it swapped */ 275 if (!pch_swap()) 276 /* put it back to normal */ 277 fatal("lost hunk on alloc error!\n"); 278 reverse = !reverse; 279 } else if (noreverse) { 280 if (!pch_swap()) 281 /* put it back to normal */ 282 fatal("lost hunk on alloc error!\n"); 283 reverse = !reverse; 284 say("Ignoring previously applied (or reversed) patch.\n"); 285 skip_rest_of_patch = true; 286 } else if (batch) { 287 if (verbose) 288 say("%seversed (or previously applied) patch detected! %s -R.", 289 reverse ? "R" : "Unr", 290 reverse ? "Assuming" : "Ignoring"); 291 } else { 292 ask("%seversed (or previously applied) patch detected! %s -R? [y] ", 293 reverse ? "R" : "Unr", 294 reverse ? "Assume" : "Ignore"); 295 if (*buf == 'n') { 296 ask("Apply anyway? [n] "); 297 if (*buf != 'y') 298 skip_rest_of_patch = true; 299 else 300 reverse_seen = true; 301 where = 0; 302 reverse = !reverse; 303 if (!pch_swap()) 304 /* put it back to normal */ 305 fatal("lost hunk on alloc error!\n"); 306 } 307 } 308 } 309 } while (!skip_rest_of_patch && where == 0 && 310 ++fuzz <= mymaxfuzz); 311 312 if (skip_rest_of_patch) { /* just got decided */ 313 if (ferror(ofp) || fclose(ofp)) { 314 say("Error writing %s\n", 315 TMPOUTNAME); 316 error = 1; 317 } 318 ofp = NULL; 319 } 320 } 321 newwhere = pch_newfirst() + last_offset; 322 if (skip_rest_of_patch) { 323 abort_hunk(); 324 failed++; 325 if (verbose) 326 say("Hunk #%d ignored at %ld.\n", 327 hunk, newwhere); 328 } else if (where == 0) { 329 abort_hunk(); 330 failed++; 331 if (verbose) 332 say("Hunk #%d failed at %ld.\n", 333 hunk, newwhere); 334 } else { 335 apply_hunk(where); 336 if (verbose) { 337 say("Hunk #%d succeeded at %ld", 338 hunk, newwhere); 339 if (fuzz != 0) 340 say(" with fuzz %ld", fuzz); 341 if (last_offset) 342 say(" (offset %ld line%s)", 343 last_offset, 344 last_offset == 1L ? "" : "s"); 345 say(".\n"); 346 } 347 } 348 } 349 350 if (out_of_mem && using_plan_a) { 351 Argc = Argc_last; 352 Argv = Argv_last; 353 say("\n\nRan out of memory using Plan A--trying again...\n\n"); 354 if (ofp) 355 fclose(ofp); 356 ofp = NULL; 357 if (rejfp) 358 fclose(rejfp); 359 rejfp = NULL; 360 continue; 361 } 362 if (hunk == 0) 363 fatal("Internal error: hunk should not be 0\n"); 364 365 /* finish spewing out the new file */ 366 if (!skip_rest_of_patch && !spew_output()) { 367 say("Can't write %s\n", TMPOUTNAME); 368 error = 1; 369 } 370 371 /* and put the output where desired */ 372 ignore_signals(); 373 if (!skip_rest_of_patch) { 374 struct stat statbuf; 375 char *realout = outname; 376 377 if (!check_only) { 378 if (move_file(TMPOUTNAME, outname) < 0) { 379 toutkeep = true; 380 realout = TMPOUTNAME; 381 chmod(TMPOUTNAME, filemode); 382 } else 383 chmod(outname, filemode); 384 385 if (remove_empty_files && 386 stat(realout, &statbuf) == 0 && 387 statbuf.st_size == 0) { 388 if (verbose) 389 say("Removing %s (empty after patching).\n", 390 realout); 391 unlink(realout); 392 } 393 } 394 } 395 if (ferror(rejfp) || fclose(rejfp)) { 396 say("Error writing %s\n", rejname); 397 error = 1; 398 } 399 rejfp = NULL; 400 if (failed) { 401 error = 1; 402 if (*rejname == '\0') { 403 if (strlcpy(rejname, outname, 404 sizeof(rejname)) >= sizeof(rejname)) 405 fatal("filename %s is too long\n", outname); 406 if (strlcat(rejname, REJEXT, 407 sizeof(rejname)) >= sizeof(rejname)) 408 fatal("filename %s is too long\n", outname); 409 } 410 if (!check_only) 411 say("%d out of %d hunks %s--saving rejects to %s\n", 412 failed, hunk, skip_rest_of_patch ? "ignored" : "failed", rejname); 413 else 414 say("%d out of %d hunks %s while patching %s\n", 415 failed, hunk, skip_rest_of_patch ? "ignored" : "failed", filearg[0]); 416 if (!check_only && move_file(TMPREJNAME, rejname) < 0) 417 trejkeep = true; 418 } 419 set_signals(1); 420 } 421 422 if (!patch_seen) 423 error = 2; 424 425 my_exit(error); 426 /* NOTREACHED */ 427 } 428 429 /* Prepare to find the next patch to do in the patch file. */ 430 431 static void 432 reinitialize_almost_everything(void) 433 { 434 re_patch(); 435 re_input(); 436 437 input_lines = 0; 438 last_frozen_line = 0; 439 440 filec = 0; 441 if (!out_of_mem) { 442 free(filearg[0]); 443 filearg[0] = NULL; 444 } 445 446 free(outname); 447 outname = NULL; 448 449 last_offset = 0; 450 diff_type = 0; 451 452 free(revision); 453 revision = NULL; 454 455 reverse = reverse_flag_specified; 456 skip_rest_of_patch = false; 457 458 get_some_switches(); 459 } 460 461 /* Process switches and filenames. */ 462 463 static void 464 get_some_switches(void) 465 { 466 const char *options = "b::B:cCd:D:eEfF:i:lnNo:p:r:RstuvV:x:z:"; 467 static struct option longopts[] = { 468 {"backup", no_argument, 0, 'b'}, 469 {"batch", no_argument, 0, 't'}, 470 {"check", no_argument, 0, 'C'}, 471 {"context", no_argument, 0, 'c'}, 472 {"debug", required_argument, 0, 'x'}, 473 {"directory", required_argument, 0, 'd'}, 474 {"dry-run", no_argument, 0, 'C'}, 475 {"ed", no_argument, 0, 'e'}, 476 {"force", no_argument, 0, 'f'}, 477 {"forward", no_argument, 0, 'N'}, 478 {"fuzz", required_argument, 0, 'F'}, 479 {"ifdef", required_argument, 0, 'D'}, 480 {"input", required_argument, 0, 'i'}, 481 {"ignore-whitespace", no_argument, 0, 'l'}, 482 {"normal", no_argument, 0, 'n'}, 483 {"output", required_argument, 0, 'o'}, 484 {"prefix", required_argument, 0, 'B'}, 485 {"quiet", no_argument, 0, 's'}, 486 {"reject-file", required_argument, 0, 'r'}, 487 {"remove-empty-files", no_argument, 0, 'E'}, 488 {"reverse", no_argument, 0, 'R'}, 489 {"silent", no_argument, 0, 's'}, 490 {"strip", required_argument, 0, 'p'}, 491 {"suffix", required_argument, 0, 'z'}, 492 {"unified", no_argument, 0, 'u'}, 493 {"version", no_argument, 0, 'v'}, 494 {"version-control", required_argument, 0, 'V'}, 495 {"posix", no_argument, &posix, 1}, 496 {NULL, 0, 0, 0} 497 }; 498 int ch; 499 500 rejname[0] = '\0'; 501 Argc_last = Argc; 502 Argv_last = Argv; 503 if (!Argc) 504 return; 505 optreset = optind = 1; 506 while ((ch = getopt_long(Argc, Argv, options, longopts, NULL)) != -1) { 507 switch (ch) { 508 case 'b': 509 if (backup_type == none) 510 backup_type = numbered_existing; 511 if (optarg == NULL) 512 break; 513 if (verbose) 514 say("Warning, the ``-b suffix'' option has been" 515 " obsoleted by the -z option.\n"); 516 /* FALLTHROUGH */ 517 case 'z': 518 /* must directly follow 'b' case for backwards compat */ 519 simple_backup_suffix = xstrdup(optarg); 520 break; 521 case 'B': 522 origprae = xstrdup(optarg); 523 break; 524 case 'c': 525 diff_type = CONTEXT_DIFF; 526 break; 527 case 'C': 528 check_only = true; 529 break; 530 case 'd': 531 if (chdir(optarg) < 0) 532 pfatal("can't cd to %s", optarg); 533 break; 534 case 'D': 535 do_defines = true; 536 if (!isalpha((unsigned char)*optarg) && *optarg != '_') 537 fatal("argument to -D is not an identifier\n"); 538 snprintf(if_defined, sizeof if_defined, 539 "#ifdef %s\n", optarg); 540 snprintf(not_defined, sizeof not_defined, 541 "#ifndef %s\n", optarg); 542 snprintf(end_defined, sizeof end_defined, 543 "#endif /* %s */\n", optarg); 544 break; 545 case 'e': 546 diff_type = ED_DIFF; 547 break; 548 case 'E': 549 remove_empty_files = true; 550 break; 551 case 'f': 552 force = true; 553 break; 554 case 'F': 555 maxfuzz = atoi(optarg); 556 break; 557 case 'i': 558 if (++filec == MAXFILEC) 559 fatal("too many file arguments\n"); 560 filearg[filec] = xstrdup(optarg); 561 break; 562 case 'l': 563 canonicalize = true; 564 break; 565 case 'n': 566 diff_type = NORMAL_DIFF; 567 break; 568 case 'N': 569 noreverse = true; 570 break; 571 case 'o': 572 outname = xstrdup(optarg); 573 break; 574 case 'p': 575 strippath = atoi(optarg); 576 break; 577 case 'r': 578 if (strlcpy(rejname, optarg, 579 sizeof(rejname)) >= sizeof(rejname)) 580 fatal("argument for -r is too long\n"); 581 break; 582 case 'R': 583 reverse = true; 584 reverse_flag_specified = true; 585 break; 586 case 's': 587 verbose = false; 588 break; 589 case 't': 590 batch = true; 591 break; 592 case 'u': 593 diff_type = UNI_DIFF; 594 break; 595 case 'v': 596 version(); 597 break; 598 case 'V': 599 backup_type = get_version(optarg); 600 Vflag = true; 601 break; 602 #ifdef DEBUGGING 603 case 'x': 604 debug = atoi(optarg); 605 break; 606 #endif 607 default: 608 if (ch != '\0') 609 usage(); 610 break; 611 } 612 } 613 Argc -= optind; 614 Argv += optind; 615 616 if (Argc > 0) { 617 filearg[0] = xstrdup(*Argv++); 618 Argc--; 619 while (Argc > 0) { 620 if (++filec == MAXFILEC) 621 fatal("too many file arguments\n"); 622 filearg[filec] = xstrdup(*Argv++); 623 Argc--; 624 } 625 } 626 627 if (getenv("POSIXLY_CORRECT") != NULL) 628 posix = 1; 629 } 630 631 static void 632 usage(void) 633 { 634 fprintf(stderr, 635 "usage: patch [-bCcEeflNnRstuv] [-B backup-prefix] [-D symbol] [-d directory]\n" 636 " [-F max-fuzz] [-i patchfile] [-o out-file] [-p strip-count]\n" 637 " [-r rej-name] [-V t | nil | never | none] [-x number]\n" 638 " [-z backup-ext] [--posix] [origfile [patchfile]]\n" 639 " patch <patchfile\n"); 640 my_exit(EXIT_FAILURE); 641 } 642 643 /* 644 * Attempt to find the right place to apply this hunk of patch. 645 */ 646 static LINENUM 647 locate_hunk(LINENUM fuzz) 648 { 649 LINENUM first_guess = pch_first() + last_offset; 650 LINENUM offset; 651 LINENUM pat_lines = pch_ptrn_lines(); 652 LINENUM max_pos_offset = input_lines - first_guess - pat_lines + 1; 653 LINENUM max_neg_offset = first_guess - last_frozen_line - 1 + pch_context(); 654 655 if (pat_lines == 0) { /* null range matches always */ 656 if (verbose && fuzz == 0 && (diff_type == CONTEXT_DIFF 657 || diff_type == NEW_CONTEXT_DIFF 658 || diff_type == UNI_DIFF)) { 659 say("Empty context always matches.\n"); 660 } 661 return (first_guess); 662 } 663 if (max_neg_offset >= first_guess) /* do not try lines < 0 */ 664 max_neg_offset = first_guess - 1; 665 if (first_guess <= input_lines && patch_match(first_guess, 0, fuzz)) 666 return first_guess; 667 for (offset = 1; ; offset++) { 668 bool check_after = (offset <= max_pos_offset); 669 bool check_before = (offset <= max_neg_offset); 670 671 if (check_after && patch_match(first_guess, offset, fuzz)) { 672 #ifdef DEBUGGING 673 if (debug & 1) 674 say("Offset changing from %ld to %ld\n", 675 last_offset, offset); 676 #endif 677 last_offset = offset; 678 return first_guess + offset; 679 } else if (check_before && patch_match(first_guess, -offset, fuzz)) { 680 #ifdef DEBUGGING 681 if (debug & 1) 682 say("Offset changing from %ld to %ld\n", 683 last_offset, -offset); 684 #endif 685 last_offset = -offset; 686 return first_guess - offset; 687 } else if (!check_before && !check_after) 688 return 0; 689 } 690 } 691 692 /* We did not find the pattern, dump out the hunk so they can handle it. */ 693 694 static void 695 abort_context_hunk(void) 696 { 697 LINENUM i; 698 const LINENUM pat_end = pch_end(); 699 /* 700 * add in last_offset to guess the same as the previous successful 701 * hunk 702 */ 703 const LINENUM oldfirst = pch_first() + last_offset; 704 const LINENUM newfirst = pch_newfirst() + last_offset; 705 const LINENUM oldlast = oldfirst + pch_ptrn_lines() - 1; 706 const LINENUM newlast = newfirst + pch_repl_lines() - 1; 707 const char *stars = (diff_type >= NEW_CONTEXT_DIFF ? " ****" : ""); 708 const char *minuses = (diff_type >= NEW_CONTEXT_DIFF ? " ----" : " -----"); 709 710 fprintf(rejfp, "***************\n"); 711 for (i = 0; i <= pat_end; i++) { 712 switch (pch_char(i)) { 713 case '*': 714 if (oldlast < oldfirst) 715 fprintf(rejfp, "*** 0%s\n", stars); 716 else if (oldlast == oldfirst) 717 fprintf(rejfp, "*** %ld%s\n", oldfirst, stars); 718 else 719 fprintf(rejfp, "*** %ld,%ld%s\n", oldfirst, 720 oldlast, stars); 721 break; 722 case '=': 723 if (newlast < newfirst) 724 fprintf(rejfp, "--- 0%s\n", minuses); 725 else if (newlast == newfirst) 726 fprintf(rejfp, "--- %ld%s\n", newfirst, minuses); 727 else 728 fprintf(rejfp, "--- %ld,%ld%s\n", newfirst, 729 newlast, minuses); 730 break; 731 case '\n': 732 fprintf(rejfp, "%s", pfetch(i)); 733 break; 734 case ' ': 735 case '-': 736 case '+': 737 case '!': 738 fprintf(rejfp, "%c %s", pch_char(i), pfetch(i)); 739 break; 740 default: 741 fatal("fatal internal error in abort_context_hunk\n"); 742 } 743 } 744 } 745 746 static void 747 rej_line(int ch, LINENUM i) 748 { 749 size_t len; 750 const char *line = pfetch(i); 751 752 len = strnlen(line, UINT_MAX); 753 754 fprintf(rejfp, "%c%s", ch, line); 755 if (len == 0 || line[len-1] != '\n') 756 fprintf(rejfp, "\n\\ No newline at end of line\n"); 757 else if (len >= UINT_MAX) 758 fprintf(rejfp, "\n\\ Line too long\n"); 759 } 760 761 static void 762 abort_hunk(void) 763 { 764 LINENUM i, j, split; 765 int ch1, ch2; 766 const LINENUM pat_end = pch_end(); 767 const LINENUM oldfirst = pch_first() + last_offset; 768 const LINENUM newfirst = pch_newfirst() + last_offset; 769 770 if (diff_type != UNI_DIFF) { 771 abort_context_hunk(); 772 return; 773 } 774 split = -1; 775 for (i = 0; i <= pat_end; i++) { 776 if (pch_char(i) == '=') { 777 split = i; 778 break; 779 } 780 } 781 if (split == -1) { 782 fprintf(rejfp, "malformed hunk: no split found\n"); 783 return; 784 } 785 i = 0; 786 j = split + 1; 787 fprintf(rejfp, "@@ -%ld,%ld +%ld,%ld @@\n", 788 pch_ptrn_lines() ? oldfirst : 0, 789 pch_ptrn_lines(), newfirst, pch_repl_lines()); 790 while (i < split || j <= pat_end) { 791 ch1 = i < split ? pch_char(i) : -1; 792 ch2 = j <= pat_end ? pch_char(j) : -1; 793 if (ch1 == '-') { 794 rej_line('-', i); 795 i++; 796 } else if (ch1 == ' ' && ch2 == ' ') { 797 rej_line(' ', i); 798 i++; 799 j++; 800 } else if (ch1 == '!' && ch2 == '!') { 801 while (i < split && ch1 == '!') { 802 rej_line('-', i); 803 i++; 804 ch1 = i < split ? pch_char(i) : -1; 805 } 806 while (j <= pat_end && ch2 == '!') { 807 rej_line('+', j); 808 j++; 809 ch2 = j <= pat_end ? pch_char(j) : -1; 810 } 811 } else if (ch1 == '*') { 812 i++; 813 } else if (ch2 == '+' || ch2 == ' ') { 814 rej_line(ch2, j); 815 j++; 816 } else { 817 fprintf(rejfp, "internal error on (%ld %ld %ld)\n", 818 i, split, j); 819 rej_line(ch1, i); 820 rej_line(ch2, j); 821 return; 822 } 823 } 824 } 825 826 /* We found where to apply it (we hope), so do it. */ 827 828 static void 829 apply_hunk(LINENUM where) 830 { 831 LINENUM old = 1; 832 const LINENUM lastline = pch_ptrn_lines(); 833 LINENUM new = lastline + 1; 834 #define OUTSIDE 0 835 #define IN_IFNDEF 1 836 #define IN_IFDEF 2 837 #define IN_ELSE 3 838 int def_state = OUTSIDE; 839 const LINENUM pat_end = pch_end(); 840 841 where--; 842 while (pch_char(new) == '=' || pch_char(new) == '\n') 843 new++; 844 845 while (old <= lastline) { 846 if (pch_char(old) == '-') { 847 copy_till(where + old - 1, false); 848 if (do_defines) { 849 if (def_state == OUTSIDE) { 850 fputs(not_defined, ofp); 851 def_state = IN_IFNDEF; 852 } else if (def_state == IN_IFDEF) { 853 fputs(else_defined, ofp); 854 def_state = IN_ELSE; 855 } 856 fputs(pfetch(old), ofp); 857 } 858 last_frozen_line++; 859 old++; 860 } else if (new > pat_end) { 861 break; 862 } else if (pch_char(new) == '+') { 863 copy_till(where + old - 1, false); 864 if (do_defines) { 865 if (def_state == IN_IFNDEF) { 866 fputs(else_defined, ofp); 867 def_state = IN_ELSE; 868 } else if (def_state == OUTSIDE) { 869 fputs(if_defined, ofp); 870 def_state = IN_IFDEF; 871 } 872 } 873 fputs(pfetch(new), ofp); 874 new++; 875 } else if (pch_char(new) != pch_char(old)) { 876 say("Out-of-sync patch, lines %ld,%ld--mangled text or line numbers, maybe?\n", 877 pch_hunk_beg() + old, 878 pch_hunk_beg() + new); 879 #ifdef DEBUGGING 880 say("oldchar = '%c', newchar = '%c'\n", 881 pch_char(old), pch_char(new)); 882 #endif 883 my_exit(2); 884 } else if (pch_char(new) == '!') { 885 copy_till(where + old - 1, false); 886 if (do_defines) { 887 fputs(not_defined, ofp); 888 def_state = IN_IFNDEF; 889 } 890 while (pch_char(old) == '!') { 891 if (do_defines) { 892 fputs(pfetch(old), ofp); 893 } 894 last_frozen_line++; 895 old++; 896 } 897 if (do_defines) { 898 fputs(else_defined, ofp); 899 def_state = IN_ELSE; 900 } 901 while (pch_char(new) == '!') { 902 fputs(pfetch(new), ofp); 903 new++; 904 } 905 } else { 906 if (pch_char(new) != ' ') 907 fatal("Internal error: expected ' '\n"); 908 old++; 909 new++; 910 if (do_defines && def_state != OUTSIDE) { 911 fputs(end_defined, ofp); 912 def_state = OUTSIDE; 913 } 914 } 915 } 916 if (new <= pat_end && pch_char(new) == '+') { 917 copy_till(where + old - 1, false); 918 if (do_defines) { 919 if (def_state == OUTSIDE) { 920 fputs(if_defined, ofp); 921 def_state = IN_IFDEF; 922 } else if (def_state == IN_IFNDEF) { 923 fputs(else_defined, ofp); 924 def_state = IN_ELSE; 925 } 926 } 927 while (new <= pat_end && pch_char(new) == '+') { 928 fputs(pfetch(new), ofp); 929 new++; 930 } 931 } 932 if (do_defines && def_state != OUTSIDE) { 933 fputs(end_defined, ofp); 934 } 935 } 936 937 /* 938 * Open the new file. 939 */ 940 static void 941 init_output(const char *name) 942 { 943 ofp = fopen(name, "w"); 944 if (ofp == NULL) 945 pfatal("can't create %s", name); 946 } 947 948 /* 949 * Open a file to put hunks we can't locate. 950 */ 951 static void 952 init_reject(const char *name) 953 { 954 rejfp = fopen(name, "w"); 955 if (rejfp == NULL) 956 pfatal("can't create %s", name); 957 } 958 959 /* 960 * Copy input file to output, up to wherever hunk is to be applied. 961 * If endoffile is true, treat the last line specially since it may 962 * lack a newline. 963 */ 964 static void 965 copy_till(LINENUM lastline, bool endoffile) 966 { 967 if (last_frozen_line > lastline) 968 fatal("misordered hunks! output would be garbled\n"); 969 while (last_frozen_line < lastline) { 970 if (++last_frozen_line == lastline && endoffile) 971 dump_line(last_frozen_line, !last_line_missing_eol); 972 else 973 dump_line(last_frozen_line, true); 974 } 975 } 976 977 /* 978 * Finish copying the input file to the output file. 979 */ 980 static bool 981 spew_output(void) 982 { 983 int rv; 984 985 #ifdef DEBUGGING 986 if (debug & 256) 987 say("il=%ld lfl=%ld\n", input_lines, last_frozen_line); 988 #endif 989 if (input_lines) 990 copy_till(input_lines, true); /* dump remainder of file */ 991 rv = ferror(ofp) == 0 && fclose(ofp) == 0; 992 ofp = NULL; 993 return rv; 994 } 995 996 /* 997 * Copy one line from input to output. 998 */ 999 static void 1000 dump_line(LINENUM line, bool write_newline) 1001 { 1002 char *s; 1003 1004 s = ifetch(line, 0); 1005 if (s == NULL) 1006 return; 1007 /* Note: string is not NUL terminated. */ 1008 for (; *s != '\n'; s++) 1009 putc(*s, ofp); 1010 if (write_newline) 1011 putc('\n', ofp); 1012 } 1013 1014 /* 1015 * Does the patch pattern match at line base+offset? 1016 */ 1017 static bool 1018 patch_match(LINENUM base, LINENUM offset, LINENUM fuzz) 1019 { 1020 LINENUM pline = 1 + fuzz; 1021 LINENUM iline; 1022 LINENUM pat_lines = pch_ptrn_lines() - fuzz; 1023 const char *ilineptr; 1024 const char *plineptr; 1025 u_int plinelen; 1026 1027 for (iline = base + offset + fuzz; pline <= pat_lines; pline++, iline++) { 1028 ilineptr = ifetch(iline, offset >= 0); 1029 if (ilineptr == NULL) 1030 return false; 1031 plineptr = pfetch(pline); 1032 plinelen = pch_line_len(pline); 1033 if (canonicalize) { 1034 if (!similar(ilineptr, plineptr, plinelen)) 1035 return false; 1036 } else if (strnNE(ilineptr, plineptr, plinelen)) 1037 return false; 1038 if (iline == input_lines) { 1039 /* 1040 * We are looking at the last line of the file. 1041 * If the file has no eol, the patch line should 1042 * not have one either and vice-versa. Note that 1043 * plinelen > 0. 1044 */ 1045 if (last_line_missing_eol) { 1046 if (plineptr[plinelen - 1] == '\n') 1047 return false; 1048 } else { 1049 if (plineptr[plinelen - 1] != '\n') 1050 return false; 1051 } 1052 } 1053 } 1054 return true; 1055 } 1056 1057 /* 1058 * Do two lines match with canonicalized white space? 1059 */ 1060 static bool 1061 similar(const char *a, const char *b, int len) 1062 { 1063 while (len) { 1064 if (isspace((unsigned char)*b)) { /* whitespace (or \n) to match? */ 1065 if (!isspace((unsigned char)*a)) /* no corresponding whitespace? */ 1066 return false; 1067 while (len && isspace((unsigned char)*b) && *b != '\n') 1068 b++, len--; /* skip pattern whitespace */ 1069 while (isspace((unsigned char)*a) && *a != '\n') 1070 a++; /* skip target whitespace */ 1071 if (*a == '\n' || *b == '\n') 1072 return (*a == *b); /* should end in sync */ 1073 } else if (*a++ != *b++) /* match non-whitespace chars */ 1074 return false; 1075 else 1076 len--; /* probably not necessary */ 1077 } 1078 return true; /* actually, this is not reached */ 1079 /* since there is always a \n */ 1080 } 1081