12dd076b8SGabor Kovesdan /*- 22dd076b8SGabor Kovesdan * Copyright 1986, Larry Wall 32dd076b8SGabor Kovesdan * 42dd076b8SGabor Kovesdan * Redistribution and use in source and binary forms, with or without 52dd076b8SGabor Kovesdan * modification, are permitted provided that the following condition is met: 62dd076b8SGabor Kovesdan * 1. Redistributions of source code must retain the above copyright notice, 72dd076b8SGabor Kovesdan * this condition and the following disclaimer. 82dd076b8SGabor Kovesdan * 92dd076b8SGabor Kovesdan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY 102dd076b8SGabor Kovesdan * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 112dd076b8SGabor Kovesdan * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 122dd076b8SGabor Kovesdan * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 132dd076b8SGabor Kovesdan * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 142dd076b8SGabor Kovesdan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 152dd076b8SGabor Kovesdan * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 162dd076b8SGabor Kovesdan * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 172dd076b8SGabor Kovesdan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 182dd076b8SGabor Kovesdan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 192dd076b8SGabor Kovesdan * SUCH DAMAGE. 202dd076b8SGabor Kovesdan * 212dd076b8SGabor Kovesdan * patch - a program to apply diffs to original files 222dd076b8SGabor Kovesdan * 232dd076b8SGabor Kovesdan * -C option added in 1998, original code by Marc Espie, based on FreeBSD 242dd076b8SGabor Kovesdan * behaviour 252dd076b8SGabor Kovesdan * 262b468ebaSPedro F. Giffuni * $OpenBSD: pch.c,v 1.43 2014/11/18 17:03:35 tobias Exp $ 27e56ef7d3SXin LI * $FreeBSD$ 282dd076b8SGabor Kovesdan */ 292dd076b8SGabor Kovesdan 302dd076b8SGabor Kovesdan #include <sys/types.h> 312dd076b8SGabor Kovesdan #include <sys/stat.h> 322dd076b8SGabor Kovesdan 332dd076b8SGabor Kovesdan #include <ctype.h> 342dd076b8SGabor Kovesdan #include <libgen.h> 352dd076b8SGabor Kovesdan #include <limits.h> 36df6e4074SPedro F. Giffuni #include <stdint.h> 372dd076b8SGabor Kovesdan #include <stdio.h> 382dd076b8SGabor Kovesdan #include <stdlib.h> 392dd076b8SGabor Kovesdan #include <string.h> 402dd076b8SGabor Kovesdan #include <unistd.h> 412dd076b8SGabor Kovesdan 422dd076b8SGabor Kovesdan #include "common.h" 432dd076b8SGabor Kovesdan #include "util.h" 442dd076b8SGabor Kovesdan #include "pch.h" 452dd076b8SGabor Kovesdan #include "pathnames.h" 462dd076b8SGabor Kovesdan 472dd076b8SGabor Kovesdan /* Patch (diff listing) abstract type. */ 482dd076b8SGabor Kovesdan 49e91a64deSPedro F. Giffuni static off_t p_filesize; /* size of the patch file */ 502dd076b8SGabor Kovesdan static LINENUM p_first; /* 1st line number */ 512dd076b8SGabor Kovesdan static LINENUM p_newfirst; /* 1st line number of replacement */ 522dd076b8SGabor Kovesdan static LINENUM p_ptrn_lines; /* # lines in pattern */ 532dd076b8SGabor Kovesdan static LINENUM p_repl_lines; /* # lines in replacement text */ 542dd076b8SGabor Kovesdan static LINENUM p_end = -1; /* last line in hunk */ 552dd076b8SGabor Kovesdan static LINENUM p_max; /* max allowed value of p_end */ 562dd076b8SGabor Kovesdan static LINENUM p_context = 3; /* # of context lines */ 572dd076b8SGabor Kovesdan static LINENUM p_input_line = 0; /* current line # from patch file */ 582dd076b8SGabor Kovesdan static char **p_line = NULL;/* the text of the hunk */ 594f548c19SPedro F. Giffuni static unsigned short *p_len = NULL; /* length of each line */ 602dd076b8SGabor Kovesdan static char *p_char = NULL; /* +, -, and ! */ 612dd076b8SGabor Kovesdan static int hunkmax = INITHUNKMAX; /* size of above arrays to begin with */ 622dd076b8SGabor Kovesdan static int p_indent; /* indent to patch */ 63e91a64deSPedro F. Giffuni static off_t p_base; /* where to intuit this time */ 642dd076b8SGabor Kovesdan static LINENUM p_bline; /* line # of p_base */ 65e91a64deSPedro F. Giffuni static off_t p_start; /* where intuit found a patch */ 662dd076b8SGabor Kovesdan static LINENUM p_sline; /* and the line number for it */ 672dd076b8SGabor Kovesdan static LINENUM p_hunk_beg; /* line number of current hunk */ 682dd076b8SGabor Kovesdan static LINENUM p_efake = -1; /* end of faked up lines--don't free */ 692dd076b8SGabor Kovesdan static LINENUM p_bfake = -1; /* beg of faked up lines */ 702dd076b8SGabor Kovesdan static FILE *pfp = NULL; /* patch file pointer */ 712dd076b8SGabor Kovesdan static char *bestguess = NULL; /* guess at correct filename */ 722dd076b8SGabor Kovesdan 732dd076b8SGabor Kovesdan static void grow_hunkmax(void); 742dd076b8SGabor Kovesdan static int intuit_diff_type(void); 75e91a64deSPedro F. Giffuni static void next_intuit_at(off_t, LINENUM); 76e91a64deSPedro F. Giffuni static void skip_to(off_t, LINENUM); 772dd076b8SGabor Kovesdan static size_t pgets(bool _do_indent); 782dd076b8SGabor Kovesdan static char *best_name(const struct file_name *, bool); 792dd076b8SGabor Kovesdan static char *posix_name(const struct file_name *, bool); 802dd076b8SGabor Kovesdan static size_t num_components(const char *); 81d3fc0cb8SPedro F. Giffuni static LINENUM strtolinenum(char *, char **); 822dd076b8SGabor Kovesdan 832dd076b8SGabor Kovesdan /* 842dd076b8SGabor Kovesdan * Prepare to look for the next patch in the patch file. 852dd076b8SGabor Kovesdan */ 862dd076b8SGabor Kovesdan void 872dd076b8SGabor Kovesdan re_patch(void) 882dd076b8SGabor Kovesdan { 892dd076b8SGabor Kovesdan p_first = 0; 902dd076b8SGabor Kovesdan p_newfirst = 0; 912dd076b8SGabor Kovesdan p_ptrn_lines = 0; 922dd076b8SGabor Kovesdan p_repl_lines = 0; 932dd076b8SGabor Kovesdan p_end = (LINENUM) - 1; 942dd076b8SGabor Kovesdan p_max = 0; 952dd076b8SGabor Kovesdan p_indent = 0; 962dd076b8SGabor Kovesdan } 972dd076b8SGabor Kovesdan 982dd076b8SGabor Kovesdan /* 992dd076b8SGabor Kovesdan * Open the patch file at the beginning of time. 1002dd076b8SGabor Kovesdan */ 1012dd076b8SGabor Kovesdan void 1022dd076b8SGabor Kovesdan open_patch_file(const char *filename) 1032dd076b8SGabor Kovesdan { 1042dd076b8SGabor Kovesdan struct stat filestat; 1050571fd57SDavid E. O'Brien int nr, nw; 1062dd076b8SGabor Kovesdan 1072dd076b8SGabor Kovesdan if (filename == NULL || *filename == '\0' || strEQ(filename, "-")) { 1082dd076b8SGabor Kovesdan pfp = fopen(TMPPATNAME, "w"); 1092dd076b8SGabor Kovesdan if (pfp == NULL) 1102dd076b8SGabor Kovesdan pfatal("can't create %s", TMPPATNAME); 1110571fd57SDavid E. O'Brien while ((nr = fread(buf, 1, buf_size, stdin)) > 0) { 1120571fd57SDavid E. O'Brien nw = fwrite(buf, 1, nr, pfp); 1130571fd57SDavid E. O'Brien if (nr != nw) 1140571fd57SDavid E. O'Brien pfatal("write error to %s", TMPPATNAME); 1150571fd57SDavid E. O'Brien } 1162dd076b8SGabor Kovesdan if (ferror(pfp) || fclose(pfp)) 1172dd076b8SGabor Kovesdan pfatal("can't write %s", TMPPATNAME); 1182dd076b8SGabor Kovesdan filename = TMPPATNAME; 1192dd076b8SGabor Kovesdan } 1202dd076b8SGabor Kovesdan pfp = fopen(filename, "r"); 1212dd076b8SGabor Kovesdan if (pfp == NULL) 1222dd076b8SGabor Kovesdan pfatal("patch file %s not found", filename); 123e91a64deSPedro F. Giffuni if (fstat(fileno(pfp), &filestat)) 124e91a64deSPedro F. Giffuni pfatal("can't stat %s", filename); 1252dd076b8SGabor Kovesdan p_filesize = filestat.st_size; 126e91a64deSPedro F. Giffuni next_intuit_at(0, 1L); /* start at the beginning */ 1272dd076b8SGabor Kovesdan set_hunkmax(); 1282dd076b8SGabor Kovesdan } 1292dd076b8SGabor Kovesdan 1302dd076b8SGabor Kovesdan /* 1312dd076b8SGabor Kovesdan * Make sure our dynamically realloced tables are malloced to begin with. 1322dd076b8SGabor Kovesdan */ 1332dd076b8SGabor Kovesdan void 1342dd076b8SGabor Kovesdan set_hunkmax(void) 1352dd076b8SGabor Kovesdan { 1362dd076b8SGabor Kovesdan if (p_line == NULL) 137c44f94d0SPedro F. Giffuni p_line = malloc(hunkmax * sizeof(char *)); 1382dd076b8SGabor Kovesdan if (p_len == NULL) 1394f548c19SPedro F. Giffuni p_len = malloc(hunkmax * sizeof(unsigned short)); 1402dd076b8SGabor Kovesdan if (p_char == NULL) 141c44f94d0SPedro F. Giffuni p_char = malloc(hunkmax * sizeof(char)); 1422dd076b8SGabor Kovesdan } 1432dd076b8SGabor Kovesdan 1442dd076b8SGabor Kovesdan /* 1452dd076b8SGabor Kovesdan * Enlarge the arrays containing the current hunk of patch. 1462dd076b8SGabor Kovesdan */ 1472dd076b8SGabor Kovesdan static void 1482dd076b8SGabor Kovesdan grow_hunkmax(void) 1492dd076b8SGabor Kovesdan { 150c44f94d0SPedro F. Giffuni int new_hunkmax = hunkmax * 2; 1512dd076b8SGabor Kovesdan 1522dd076b8SGabor Kovesdan if (p_line == NULL || p_len == NULL || p_char == NULL) 1532dd076b8SGabor Kovesdan fatal("Internal memory allocation error\n"); 1542dd076b8SGabor Kovesdan 155c44f94d0SPedro F. Giffuni p_line = reallocf(p_line, new_hunkmax * sizeof(char *)); 1564f548c19SPedro F. Giffuni p_len = reallocf(p_len, new_hunkmax * sizeof(unsigned short)); 157c44f94d0SPedro F. Giffuni p_char = reallocf(p_char, new_hunkmax * sizeof(char)); 1582dd076b8SGabor Kovesdan 1592dd076b8SGabor Kovesdan if (p_line != NULL && p_len != NULL && p_char != NULL) { 1602dd076b8SGabor Kovesdan hunkmax = new_hunkmax; 1612dd076b8SGabor Kovesdan return; 1622dd076b8SGabor Kovesdan } 1632dd076b8SGabor Kovesdan 1642dd076b8SGabor Kovesdan if (!using_plan_a) 1652dd076b8SGabor Kovesdan fatal("out of memory\n"); 1662dd076b8SGabor Kovesdan out_of_mem = true; /* whatever is null will be allocated again */ 1672dd076b8SGabor Kovesdan /* from within plan_a(), of all places */ 1682dd076b8SGabor Kovesdan } 1692dd076b8SGabor Kovesdan 1702dd076b8SGabor Kovesdan /* True if the remainder of the patch file contains a diff of some sort. */ 1712dd076b8SGabor Kovesdan 1722dd076b8SGabor Kovesdan bool 1732dd076b8SGabor Kovesdan there_is_another_patch(void) 1742dd076b8SGabor Kovesdan { 1752dd076b8SGabor Kovesdan bool exists = false; 1762dd076b8SGabor Kovesdan 177e91a64deSPedro F. Giffuni if (p_base != 0 && p_base >= p_filesize) { 1782dd076b8SGabor Kovesdan if (verbose) 1792dd076b8SGabor Kovesdan say("done\n"); 1802dd076b8SGabor Kovesdan return false; 1812dd076b8SGabor Kovesdan } 1822dd076b8SGabor Kovesdan if (verbose) 1832dd076b8SGabor Kovesdan say("Hmm..."); 1842dd076b8SGabor Kovesdan diff_type = intuit_diff_type(); 1852dd076b8SGabor Kovesdan if (!diff_type) { 186e91a64deSPedro F. Giffuni if (p_base != 0) { 1872dd076b8SGabor Kovesdan if (verbose) 1882dd076b8SGabor Kovesdan say(" Ignoring the trailing garbage.\ndone\n"); 1892dd076b8SGabor Kovesdan } else 1902dd076b8SGabor Kovesdan say(" I can't seem to find a patch in there anywhere.\n"); 1912dd076b8SGabor Kovesdan return false; 1922dd076b8SGabor Kovesdan } 1932dd076b8SGabor Kovesdan if (verbose) 1942dd076b8SGabor Kovesdan say(" %sooks like %s to me...\n", 195e91a64deSPedro F. Giffuni (p_base == 0 ? "L" : "The next patch l"), 1962dd076b8SGabor Kovesdan diff_type == UNI_DIFF ? "a unified diff" : 1972dd076b8SGabor Kovesdan diff_type == CONTEXT_DIFF ? "a context diff" : 1982dd076b8SGabor Kovesdan diff_type == NEW_CONTEXT_DIFF ? "a new-style context diff" : 1992dd076b8SGabor Kovesdan diff_type == NORMAL_DIFF ? "a normal diff" : 2002dd076b8SGabor Kovesdan "an ed script"); 2012dd076b8SGabor Kovesdan if (p_indent && verbose) 2022dd076b8SGabor Kovesdan say("(Patch is indented %d space%s.)\n", p_indent, 2032dd076b8SGabor Kovesdan p_indent == 1 ? "" : "s"); 2042dd076b8SGabor Kovesdan skip_to(p_start, p_sline); 2052dd076b8SGabor Kovesdan while (filearg[0] == NULL) { 2062dd076b8SGabor Kovesdan if (force || batch) { 2072dd076b8SGabor Kovesdan say("No file to patch. Skipping...\n"); 208547e0acbSPedro F. Giffuni filearg[0] = xstrdup(bestguess); 2092dd076b8SGabor Kovesdan skip_rest_of_patch = true; 2102dd076b8SGabor Kovesdan return true; 2112dd076b8SGabor Kovesdan } 2122dd076b8SGabor Kovesdan ask("File to patch: "); 2132dd076b8SGabor Kovesdan if (*buf != '\n') { 2142dd076b8SGabor Kovesdan free(bestguess); 215547e0acbSPedro F. Giffuni bestguess = xstrdup(buf); 2162dd076b8SGabor Kovesdan filearg[0] = fetchname(buf, &exists, 0); 2172dd076b8SGabor Kovesdan } 2182dd076b8SGabor Kovesdan if (!exists) { 21912300d3aSPedro F. Giffuni int def_skip = *bestguess == '\0'; 22012300d3aSPedro F. Giffuni ask("No file found--skip this patch? [%c] ", 22112300d3aSPedro F. Giffuni def_skip ? 'y' : 'n'); 22212300d3aSPedro F. Giffuni if (*buf == 'n' || (!def_skip && *buf != 'y')) 2232dd076b8SGabor Kovesdan continue; 2242dd076b8SGabor Kovesdan if (verbose) 2252dd076b8SGabor Kovesdan say("Skipping patch...\n"); 2262dd076b8SGabor Kovesdan free(filearg[0]); 2272dd076b8SGabor Kovesdan filearg[0] = fetchname(bestguess, &exists, 0); 2282dd076b8SGabor Kovesdan skip_rest_of_patch = true; 2292dd076b8SGabor Kovesdan return true; 2302dd076b8SGabor Kovesdan } 2312dd076b8SGabor Kovesdan } 2322dd076b8SGabor Kovesdan return true; 2332dd076b8SGabor Kovesdan } 2342dd076b8SGabor Kovesdan 2352dd076b8SGabor Kovesdan static void 2362dd076b8SGabor Kovesdan p4_fetchname(struct file_name *name, char *str) 2372dd076b8SGabor Kovesdan { 2382dd076b8SGabor Kovesdan char *t, *h; 2392dd076b8SGabor Kovesdan 2402dd076b8SGabor Kovesdan /* Skip leading whitespace. */ 2412dd076b8SGabor Kovesdan while (isspace((unsigned char)*str)) 2422dd076b8SGabor Kovesdan str++; 2432dd076b8SGabor Kovesdan 2442dd076b8SGabor Kovesdan /* Remove the file revision number. */ 2452dd076b8SGabor Kovesdan for (t = str, h = NULL; *t != '\0' && !isspace((unsigned char)*t); t++) 2462dd076b8SGabor Kovesdan if (*t == '#') 2472dd076b8SGabor Kovesdan h = t; 2482dd076b8SGabor Kovesdan if (h != NULL) 2492dd076b8SGabor Kovesdan *h = '\0'; 2502dd076b8SGabor Kovesdan 2512dd076b8SGabor Kovesdan name->path = fetchname(str, &name->exists, strippath); 2522dd076b8SGabor Kovesdan } 2532dd076b8SGabor Kovesdan 2542dd076b8SGabor Kovesdan /* Determine what kind of diff is in the remaining part of the patch file. */ 2552dd076b8SGabor Kovesdan 2562dd076b8SGabor Kovesdan static int 2572dd076b8SGabor Kovesdan intuit_diff_type(void) 2582dd076b8SGabor Kovesdan { 259e91a64deSPedro F. Giffuni off_t this_line = 0, previous_line; 260e91a64deSPedro F. Giffuni off_t first_command_line = -1; 2612dd076b8SGabor Kovesdan LINENUM fcl_line = -1; 2622dd076b8SGabor Kovesdan bool last_line_was_command = false, this_is_a_command = false; 2632dd076b8SGabor Kovesdan bool stars_last_line = false, stars_this_line = false; 2642dd076b8SGabor Kovesdan char *s, *t; 2652dd076b8SGabor Kovesdan int indent, retval; 2662dd076b8SGabor Kovesdan struct file_name names[MAX_FILE]; 26785823a60SPedro F. Giffuni int piece_of_git = 0; 2682dd076b8SGabor Kovesdan 2692dd076b8SGabor Kovesdan memset(names, 0, sizeof(names)); 2702dd076b8SGabor Kovesdan ok_to_create_file = false; 271e91a64deSPedro F. Giffuni fseeko(pfp, p_base, SEEK_SET); 2722dd076b8SGabor Kovesdan p_input_line = p_bline - 1; 2732dd076b8SGabor Kovesdan for (;;) { 2742dd076b8SGabor Kovesdan previous_line = this_line; 2752dd076b8SGabor Kovesdan last_line_was_command = this_is_a_command; 2762dd076b8SGabor Kovesdan stars_last_line = stars_this_line; 277e91a64deSPedro F. Giffuni this_line = ftello(pfp); 2782dd076b8SGabor Kovesdan indent = 0; 2792dd076b8SGabor Kovesdan p_input_line++; 2802dd076b8SGabor Kovesdan if (pgets(false) == 0) { 281e91a64deSPedro F. Giffuni if (first_command_line >= 0) { 2822dd076b8SGabor Kovesdan /* nothing but deletes!? */ 2832dd076b8SGabor Kovesdan p_start = first_command_line; 2842dd076b8SGabor Kovesdan p_sline = fcl_line; 2852dd076b8SGabor Kovesdan retval = ED_DIFF; 2862dd076b8SGabor Kovesdan goto scan_exit; 2872dd076b8SGabor Kovesdan } else { 2882dd076b8SGabor Kovesdan p_start = this_line; 2892dd076b8SGabor Kovesdan p_sline = p_input_line; 2902dd076b8SGabor Kovesdan retval = 0; 2912dd076b8SGabor Kovesdan goto scan_exit; 2922dd076b8SGabor Kovesdan } 2932dd076b8SGabor Kovesdan } 2942dd076b8SGabor Kovesdan for (s = buf; *s == ' ' || *s == '\t' || *s == 'X'; s++) { 2952dd076b8SGabor Kovesdan if (*s == '\t') 2962dd076b8SGabor Kovesdan indent += 8 - (indent % 8); 2972dd076b8SGabor Kovesdan else 2982dd076b8SGabor Kovesdan indent++; 2992dd076b8SGabor Kovesdan } 3002dd076b8SGabor Kovesdan for (t = s; isdigit((unsigned char)*t) || *t == ','; t++) 3012dd076b8SGabor Kovesdan ; 3022dd076b8SGabor Kovesdan this_is_a_command = (isdigit((unsigned char)*s) && 3032dd076b8SGabor Kovesdan (*t == 'd' || *t == 'c' || *t == 'a')); 304e91a64deSPedro F. Giffuni if (first_command_line < 0 && this_is_a_command) { 3052dd076b8SGabor Kovesdan first_command_line = this_line; 3062dd076b8SGabor Kovesdan fcl_line = p_input_line; 3072dd076b8SGabor Kovesdan p_indent = indent; /* assume this for now */ 3082dd076b8SGabor Kovesdan } 3092dd076b8SGabor Kovesdan if (!stars_last_line && strnEQ(s, "*** ", 4)) 3102dd076b8SGabor Kovesdan names[OLD_FILE].path = fetchname(s + 4, 3112dd076b8SGabor Kovesdan &names[OLD_FILE].exists, strippath); 31285823a60SPedro F. Giffuni else if (strnEQ(s, "--- ", 4)) { 31385823a60SPedro F. Giffuni size_t off = 4; 314*76df519fSPedro F. Giffuni if (piece_of_git && strippath == 957 && 315*76df519fSPedro F. Giffuni strnEQ(s, "--- a/", 6)) 31685823a60SPedro F. Giffuni off = 6; 31785823a60SPedro F. Giffuni names[NEW_FILE].path = fetchname(s + off, 3182dd076b8SGabor Kovesdan &names[NEW_FILE].exists, strippath); 31985823a60SPedro F. Giffuni } else if (strnEQ(s, "+++ ", 4)) { 3202dd076b8SGabor Kovesdan /* pretend it is the old name */ 32185823a60SPedro F. Giffuni size_t off = 4; 322*76df519fSPedro F. Giffuni if (piece_of_git && strippath == 957 && 323*76df519fSPedro F. Giffuni strnEQ(s, "+++ b/", 6)) 32485823a60SPedro F. Giffuni off = 6; 32585823a60SPedro F. Giffuni names[OLD_FILE].path = fetchname(s + off, 3262dd076b8SGabor Kovesdan &names[OLD_FILE].exists, strippath); 32785823a60SPedro F. Giffuni } else if (strnEQ(s, "Index:", 6)) 3282dd076b8SGabor Kovesdan names[INDEX_FILE].path = fetchname(s + 6, 3292dd076b8SGabor Kovesdan &names[INDEX_FILE].exists, strippath); 3302dd076b8SGabor Kovesdan else if (strnEQ(s, "Prereq:", 7)) { 3312dd076b8SGabor Kovesdan for (t = s + 7; isspace((unsigned char)*t); t++) 3322dd076b8SGabor Kovesdan ; 333547e0acbSPedro F. Giffuni revision = xstrdup(t); 3342b468ebaSPedro F. Giffuni for (t = revision; 3352b468ebaSPedro F. Giffuni *t && !isspace((unsigned char)*t); t++) 3362dd076b8SGabor Kovesdan ; 3372dd076b8SGabor Kovesdan *t = '\0'; 3382dd076b8SGabor Kovesdan if (*revision == '\0') { 3392dd076b8SGabor Kovesdan free(revision); 3402dd076b8SGabor Kovesdan revision = NULL; 3412dd076b8SGabor Kovesdan } 34285823a60SPedro F. Giffuni } else if (strnEQ(s, "diff --git a/", 13)) { 34385823a60SPedro F. Giffuni /* Git-style diffs. */ 34485823a60SPedro F. Giffuni piece_of_git = 1; 3452dd076b8SGabor Kovesdan } else if (strnEQ(s, "==== ", 5)) { 3462dd076b8SGabor Kovesdan /* Perforce-style diffs. */ 3472dd076b8SGabor Kovesdan if ((t = strstr(s + 5, " - ")) != NULL) 3482dd076b8SGabor Kovesdan p4_fetchname(&names[NEW_FILE], t + 3); 3492dd076b8SGabor Kovesdan p4_fetchname(&names[OLD_FILE], s + 5); 3502dd076b8SGabor Kovesdan } 3512dd076b8SGabor Kovesdan if ((!diff_type || diff_type == ED_DIFF) && 352e91a64deSPedro F. Giffuni first_command_line >= 0 && 3532dd076b8SGabor Kovesdan strEQ(s, ".\n")) { 3542dd076b8SGabor Kovesdan p_indent = indent; 3552dd076b8SGabor Kovesdan p_start = first_command_line; 3562dd076b8SGabor Kovesdan p_sline = fcl_line; 3572dd076b8SGabor Kovesdan retval = ED_DIFF; 3582dd076b8SGabor Kovesdan goto scan_exit; 3592dd076b8SGabor Kovesdan } 3602dd076b8SGabor Kovesdan if ((!diff_type || diff_type == UNI_DIFF) && strnEQ(s, "@@ -", 4)) { 3612dd076b8SGabor Kovesdan if (strnEQ(s + 4, "0,0", 3)) 3622dd076b8SGabor Kovesdan ok_to_create_file = true; 3632dd076b8SGabor Kovesdan p_indent = indent; 3642dd076b8SGabor Kovesdan p_start = this_line; 3652dd076b8SGabor Kovesdan p_sline = p_input_line; 3662dd076b8SGabor Kovesdan retval = UNI_DIFF; 3672dd076b8SGabor Kovesdan goto scan_exit; 3682dd076b8SGabor Kovesdan } 3692dd076b8SGabor Kovesdan stars_this_line = strnEQ(s, "********", 8); 3702dd076b8SGabor Kovesdan if ((!diff_type || diff_type == CONTEXT_DIFF) && stars_last_line && 3712dd076b8SGabor Kovesdan strnEQ(s, "*** ", 4)) { 372d3fc0cb8SPedro F. Giffuni if (strtolinenum(s + 4, &s) == 0) 3732dd076b8SGabor Kovesdan ok_to_create_file = true; 3742dd076b8SGabor Kovesdan /* 3752dd076b8SGabor Kovesdan * If this is a new context diff the character just 376e91a64deSPedro F. Giffuni * at the end of the line is a '*'. 3772dd076b8SGabor Kovesdan */ 378e91a64deSPedro F. Giffuni while (*s && *s != '\n') 3792dd076b8SGabor Kovesdan s++; 3802dd076b8SGabor Kovesdan p_indent = indent; 3812dd076b8SGabor Kovesdan p_start = previous_line; 3822dd076b8SGabor Kovesdan p_sline = p_input_line - 1; 3832dd076b8SGabor Kovesdan retval = (*(s - 1) == '*' ? NEW_CONTEXT_DIFF : CONTEXT_DIFF); 3842dd076b8SGabor Kovesdan goto scan_exit; 3852dd076b8SGabor Kovesdan } 3862dd076b8SGabor Kovesdan if ((!diff_type || diff_type == NORMAL_DIFF) && 3872dd076b8SGabor Kovesdan last_line_was_command && 3882dd076b8SGabor Kovesdan (strnEQ(s, "< ", 2) || strnEQ(s, "> ", 2))) { 3892dd076b8SGabor Kovesdan p_start = previous_line; 3902dd076b8SGabor Kovesdan p_sline = p_input_line - 1; 3912dd076b8SGabor Kovesdan p_indent = indent; 3922dd076b8SGabor Kovesdan retval = NORMAL_DIFF; 3932dd076b8SGabor Kovesdan goto scan_exit; 3942dd076b8SGabor Kovesdan } 3952dd076b8SGabor Kovesdan } 3962dd076b8SGabor Kovesdan scan_exit: 3972dd076b8SGabor Kovesdan if (retval == UNI_DIFF) { 3982dd076b8SGabor Kovesdan /* unswap old and new */ 3992dd076b8SGabor Kovesdan struct file_name tmp = names[OLD_FILE]; 4002dd076b8SGabor Kovesdan names[OLD_FILE] = names[NEW_FILE]; 4012dd076b8SGabor Kovesdan names[NEW_FILE] = tmp; 4022dd076b8SGabor Kovesdan } 4032dd076b8SGabor Kovesdan if (filearg[0] == NULL) { 4042dd076b8SGabor Kovesdan if (posix) 4052dd076b8SGabor Kovesdan filearg[0] = posix_name(names, ok_to_create_file); 4062dd076b8SGabor Kovesdan else { 4072dd076b8SGabor Kovesdan /* Ignore the Index: name for context diffs, like GNU */ 4082dd076b8SGabor Kovesdan if (names[OLD_FILE].path != NULL || 4092dd076b8SGabor Kovesdan names[NEW_FILE].path != NULL) { 4102dd076b8SGabor Kovesdan free(names[INDEX_FILE].path); 4112dd076b8SGabor Kovesdan names[INDEX_FILE].path = NULL; 4122dd076b8SGabor Kovesdan } 4132dd076b8SGabor Kovesdan filearg[0] = best_name(names, ok_to_create_file); 4142dd076b8SGabor Kovesdan } 4152dd076b8SGabor Kovesdan } 4162dd076b8SGabor Kovesdan 4172dd076b8SGabor Kovesdan free(bestguess); 4182dd076b8SGabor Kovesdan bestguess = NULL; 4192dd076b8SGabor Kovesdan if (filearg[0] != NULL) 420547e0acbSPedro F. Giffuni bestguess = xstrdup(filearg[0]); 4212dd076b8SGabor Kovesdan else if (!ok_to_create_file) { 4222dd076b8SGabor Kovesdan /* 4232dd076b8SGabor Kovesdan * We don't want to create a new file but we need a 4242dd076b8SGabor Kovesdan * filename to set bestguess. Avoid setting filearg[0] 4252dd076b8SGabor Kovesdan * so the file is not created automatically. 4262dd076b8SGabor Kovesdan */ 4272dd076b8SGabor Kovesdan if (posix) 4282dd076b8SGabor Kovesdan bestguess = posix_name(names, true); 4292dd076b8SGabor Kovesdan else 4302dd076b8SGabor Kovesdan bestguess = best_name(names, true); 4312dd076b8SGabor Kovesdan } 4322dd076b8SGabor Kovesdan free(names[OLD_FILE].path); 4332dd076b8SGabor Kovesdan free(names[NEW_FILE].path); 4342dd076b8SGabor Kovesdan free(names[INDEX_FILE].path); 4352dd076b8SGabor Kovesdan return retval; 4362dd076b8SGabor Kovesdan } 4372dd076b8SGabor Kovesdan 4382dd076b8SGabor Kovesdan /* 4392dd076b8SGabor Kovesdan * Remember where this patch ends so we know where to start up again. 4402dd076b8SGabor Kovesdan */ 4412dd076b8SGabor Kovesdan static void 442e91a64deSPedro F. Giffuni next_intuit_at(off_t file_pos, LINENUM file_line) 4432dd076b8SGabor Kovesdan { 4442dd076b8SGabor Kovesdan p_base = file_pos; 4452dd076b8SGabor Kovesdan p_bline = file_line; 4462dd076b8SGabor Kovesdan } 4472dd076b8SGabor Kovesdan 4482dd076b8SGabor Kovesdan /* 449e91a64deSPedro F. Giffuni * Basically a verbose fseeko() to the actual diff listing. 4502dd076b8SGabor Kovesdan */ 4512dd076b8SGabor Kovesdan static void 452e91a64deSPedro F. Giffuni skip_to(off_t file_pos, LINENUM file_line) 4532dd076b8SGabor Kovesdan { 4542dd076b8SGabor Kovesdan size_t len; 4552dd076b8SGabor Kovesdan 4562dd076b8SGabor Kovesdan if (p_base > file_pos) 457e91a64deSPedro F. Giffuni fatal("Internal error: seek %lld>%lld\n", 458e91a64deSPedro F. Giffuni (long long)p_base, (long long)file_pos); 4592dd076b8SGabor Kovesdan if (verbose && p_base < file_pos) { 460e91a64deSPedro F. Giffuni fseeko(pfp, p_base, SEEK_SET); 4612dd076b8SGabor Kovesdan say("The text leading up to this was:\n--------------------------\n"); 462e91a64deSPedro F. Giffuni while (ftello(pfp) < file_pos) { 4632dd076b8SGabor Kovesdan len = pgets(false); 4642dd076b8SGabor Kovesdan if (len == 0) 4652dd076b8SGabor Kovesdan fatal("Unexpected end of file\n"); 4662dd076b8SGabor Kovesdan say("|%s", buf); 4672dd076b8SGabor Kovesdan } 4682dd076b8SGabor Kovesdan say("--------------------------\n"); 4692dd076b8SGabor Kovesdan } else 470e91a64deSPedro F. Giffuni fseeko(pfp, file_pos, SEEK_SET); 4712dd076b8SGabor Kovesdan p_input_line = file_line - 1; 4722dd076b8SGabor Kovesdan } 4732dd076b8SGabor Kovesdan 4742dd076b8SGabor Kovesdan /* Make this a function for better debugging. */ 4752dd076b8SGabor Kovesdan static void 4762dd076b8SGabor Kovesdan malformed(void) 4772dd076b8SGabor Kovesdan { 4782dd076b8SGabor Kovesdan fatal("malformed patch at line %ld: %s", p_input_line, buf); 4792dd076b8SGabor Kovesdan /* about as informative as "Syntax error" in C */ 4802dd076b8SGabor Kovesdan } 4812dd076b8SGabor Kovesdan 4822dd076b8SGabor Kovesdan /* 4832dd076b8SGabor Kovesdan * True if the line has been discarded (i.e. it is a line saying 4842dd076b8SGabor Kovesdan * "\ No newline at end of file".) 4852dd076b8SGabor Kovesdan */ 4862dd076b8SGabor Kovesdan static bool 4872dd076b8SGabor Kovesdan remove_special_line(void) 4882dd076b8SGabor Kovesdan { 4892dd076b8SGabor Kovesdan int c; 4902dd076b8SGabor Kovesdan 4912dd076b8SGabor Kovesdan c = fgetc(pfp); 4922dd076b8SGabor Kovesdan if (c == '\\') { 4932dd076b8SGabor Kovesdan do { 4942dd076b8SGabor Kovesdan c = fgetc(pfp); 4952dd076b8SGabor Kovesdan } while (c != EOF && c != '\n'); 4962dd076b8SGabor Kovesdan 4972dd076b8SGabor Kovesdan return true; 4982dd076b8SGabor Kovesdan } 4992dd076b8SGabor Kovesdan if (c != EOF) 500e91a64deSPedro F. Giffuni fseeko(pfp, -1, SEEK_CUR); 5012dd076b8SGabor Kovesdan 5022dd076b8SGabor Kovesdan return false; 5032dd076b8SGabor Kovesdan } 5042dd076b8SGabor Kovesdan 5052dd076b8SGabor Kovesdan /* 5062dd076b8SGabor Kovesdan * True if there is more of the current diff listing to process. 5072dd076b8SGabor Kovesdan */ 5082dd076b8SGabor Kovesdan bool 5092dd076b8SGabor Kovesdan another_hunk(void) 5102dd076b8SGabor Kovesdan { 511e91a64deSPedro F. Giffuni off_t line_beginning; /* file pos of the current line */ 5122dd076b8SGabor Kovesdan LINENUM repl_beginning; /* index of --- line */ 5132dd076b8SGabor Kovesdan LINENUM fillcnt; /* #lines of missing ptrn or repl */ 5142dd076b8SGabor Kovesdan LINENUM fillsrc; /* index of first line to copy */ 5152dd076b8SGabor Kovesdan LINENUM filldst; /* index of first missing line */ 516463a577bSEitan Adler bool ptrn_spaces_eaten; /* ptrn was slightly malformed */ 5172dd076b8SGabor Kovesdan bool repl_could_be_missing; /* no + or ! lines in this hunk */ 5182dd076b8SGabor Kovesdan bool repl_missing; /* we are now backtracking */ 519e91a64deSPedro F. Giffuni off_t repl_backtrack_position; /* file pos of first repl line */ 5202dd076b8SGabor Kovesdan LINENUM repl_patch_line; /* input line number for same */ 5212dd076b8SGabor Kovesdan LINENUM ptrn_copiable; /* # of copiable lines in ptrn */ 5222dd076b8SGabor Kovesdan char *s; 5232dd076b8SGabor Kovesdan size_t len; 5242dd076b8SGabor Kovesdan int context = 0; 5252dd076b8SGabor Kovesdan 5262dd076b8SGabor Kovesdan while (p_end >= 0) { 5272dd076b8SGabor Kovesdan if (p_end == p_efake) 5282dd076b8SGabor Kovesdan p_end = p_bfake; /* don't free twice */ 5292dd076b8SGabor Kovesdan else 5302dd076b8SGabor Kovesdan free(p_line[p_end]); 5312dd076b8SGabor Kovesdan p_end--; 5322dd076b8SGabor Kovesdan } 5332dd076b8SGabor Kovesdan p_efake = -1; 5342dd076b8SGabor Kovesdan 5352dd076b8SGabor Kovesdan p_max = hunkmax; /* gets reduced when --- found */ 5362dd076b8SGabor Kovesdan if (diff_type == CONTEXT_DIFF || diff_type == NEW_CONTEXT_DIFF) { 537e91a64deSPedro F. Giffuni line_beginning = ftello(pfp); 5382dd076b8SGabor Kovesdan repl_beginning = 0; 5392dd076b8SGabor Kovesdan fillcnt = 0; 5402dd076b8SGabor Kovesdan fillsrc = 0; 5412dd076b8SGabor Kovesdan filldst = 0; 5422dd076b8SGabor Kovesdan ptrn_spaces_eaten = false; 5432dd076b8SGabor Kovesdan repl_could_be_missing = true; 5442dd076b8SGabor Kovesdan repl_missing = false; 5452dd076b8SGabor Kovesdan repl_backtrack_position = 0; 5462dd076b8SGabor Kovesdan repl_patch_line = 0; 5472dd076b8SGabor Kovesdan ptrn_copiable = 0; 5482dd076b8SGabor Kovesdan 5492dd076b8SGabor Kovesdan len = pgets(true); 5502dd076b8SGabor Kovesdan p_input_line++; 5512dd076b8SGabor Kovesdan if (len == 0 || strnNE(buf, "********", 8)) { 5522dd076b8SGabor Kovesdan next_intuit_at(line_beginning, p_input_line); 5532dd076b8SGabor Kovesdan return false; 5542dd076b8SGabor Kovesdan } 5552dd076b8SGabor Kovesdan p_context = 100; 5562dd076b8SGabor Kovesdan p_hunk_beg = p_input_line + 1; 5572dd076b8SGabor Kovesdan while (p_end < p_max) { 558e91a64deSPedro F. Giffuni line_beginning = ftello(pfp); 5592dd076b8SGabor Kovesdan len = pgets(true); 5602dd076b8SGabor Kovesdan p_input_line++; 5612dd076b8SGabor Kovesdan if (len == 0) { 5622dd076b8SGabor Kovesdan if (p_max - p_end < 4) { 5632dd076b8SGabor Kovesdan /* assume blank lines got chopped */ 5642dd076b8SGabor Kovesdan strlcpy(buf, " \n", buf_size); 5652dd076b8SGabor Kovesdan } else { 5662dd076b8SGabor Kovesdan if (repl_beginning && repl_could_be_missing) { 5672dd076b8SGabor Kovesdan repl_missing = true; 5682dd076b8SGabor Kovesdan goto hunk_done; 5692dd076b8SGabor Kovesdan } 5702dd076b8SGabor Kovesdan fatal("unexpected end of file in patch\n"); 5712dd076b8SGabor Kovesdan } 5722dd076b8SGabor Kovesdan } 5732dd076b8SGabor Kovesdan p_end++; 5742dd076b8SGabor Kovesdan if (p_end >= hunkmax) 5752dd076b8SGabor Kovesdan fatal("Internal error: hunk larger than hunk " 5762dd076b8SGabor Kovesdan "buffer size"); 5772dd076b8SGabor Kovesdan p_char[p_end] = *buf; 5782dd076b8SGabor Kovesdan p_line[p_end] = NULL; 5792dd076b8SGabor Kovesdan switch (*buf) { 5802dd076b8SGabor Kovesdan case '*': 5812dd076b8SGabor Kovesdan if (strnEQ(buf, "********", 8)) { 5822dd076b8SGabor Kovesdan if (repl_beginning && repl_could_be_missing) { 5832dd076b8SGabor Kovesdan repl_missing = true; 5842dd076b8SGabor Kovesdan goto hunk_done; 5852dd076b8SGabor Kovesdan } else 5862dd076b8SGabor Kovesdan fatal("unexpected end of hunk " 5872dd076b8SGabor Kovesdan "at line %ld\n", 5882dd076b8SGabor Kovesdan p_input_line); 5892dd076b8SGabor Kovesdan } 5902dd076b8SGabor Kovesdan if (p_end != 0) { 5912dd076b8SGabor Kovesdan if (repl_beginning && repl_could_be_missing) { 5922dd076b8SGabor Kovesdan repl_missing = true; 5932dd076b8SGabor Kovesdan goto hunk_done; 5942dd076b8SGabor Kovesdan } 5952dd076b8SGabor Kovesdan fatal("unexpected *** at line %ld: %s", 5962dd076b8SGabor Kovesdan p_input_line, buf); 5972dd076b8SGabor Kovesdan } 5982dd076b8SGabor Kovesdan context = 0; 5992dd076b8SGabor Kovesdan p_line[p_end] = savestr(buf); 6002dd076b8SGabor Kovesdan if (out_of_mem) { 6012dd076b8SGabor Kovesdan p_end--; 6022dd076b8SGabor Kovesdan return false; 6032dd076b8SGabor Kovesdan } 6042b468ebaSPedro F. Giffuni for (s = buf; 6052b468ebaSPedro F. Giffuni *s && !isdigit((unsigned char)*s); s++) 6062dd076b8SGabor Kovesdan ; 6072dd076b8SGabor Kovesdan if (!*s) 6082dd076b8SGabor Kovesdan malformed(); 6092dd076b8SGabor Kovesdan if (strnEQ(s, "0,0", 3)) 6102dd076b8SGabor Kovesdan memmove(s, s + 2, strlen(s + 2) + 1); 611d3fc0cb8SPedro F. Giffuni p_first = strtolinenum(s, &s); 6122dd076b8SGabor Kovesdan if (*s == ',') { 6132b468ebaSPedro F. Giffuni for (; 6142b468ebaSPedro F. Giffuni *s && !isdigit((unsigned char)*s); s++) 6152dd076b8SGabor Kovesdan ; 6162dd076b8SGabor Kovesdan if (!*s) 6172dd076b8SGabor Kovesdan malformed(); 618d3fc0cb8SPedro F. Giffuni p_ptrn_lines = strtolinenum(s, &s) - p_first + 1; 619d3fc0cb8SPedro F. Giffuni if (p_ptrn_lines < 0) 620d3fc0cb8SPedro F. Giffuni malformed(); 6212dd076b8SGabor Kovesdan } else if (p_first) 6222dd076b8SGabor Kovesdan p_ptrn_lines = 1; 6232dd076b8SGabor Kovesdan else { 6242dd076b8SGabor Kovesdan p_ptrn_lines = 0; 6252dd076b8SGabor Kovesdan p_first = 1; 6262dd076b8SGabor Kovesdan } 627d3fc0cb8SPedro F. Giffuni if (p_first >= LINENUM_MAX - p_ptrn_lines || 628d3fc0cb8SPedro F. Giffuni p_ptrn_lines >= LINENUM_MAX - 6) 629d3fc0cb8SPedro F. Giffuni malformed(); 6302dd076b8SGabor Kovesdan 6312dd076b8SGabor Kovesdan /* we need this much at least */ 6322dd076b8SGabor Kovesdan p_max = p_ptrn_lines + 6; 6332dd076b8SGabor Kovesdan while (p_max >= hunkmax) 6342dd076b8SGabor Kovesdan grow_hunkmax(); 6352dd076b8SGabor Kovesdan p_max = hunkmax; 6362dd076b8SGabor Kovesdan break; 6372dd076b8SGabor Kovesdan case '-': 6382dd076b8SGabor Kovesdan if (buf[1] == '-') { 6392dd076b8SGabor Kovesdan if (repl_beginning || 6402dd076b8SGabor Kovesdan (p_end != p_ptrn_lines + 1 + 6412dd076b8SGabor Kovesdan (p_char[p_end - 1] == '\n'))) { 6422dd076b8SGabor Kovesdan if (p_end == 1) { 6432dd076b8SGabor Kovesdan /* 6442dd076b8SGabor Kovesdan * `old' lines were omitted; 6452dd076b8SGabor Kovesdan * set up to fill them in 6462dd076b8SGabor Kovesdan * from 'new' context lines. 6472dd076b8SGabor Kovesdan */ 6482dd076b8SGabor Kovesdan p_end = p_ptrn_lines + 1; 6492dd076b8SGabor Kovesdan fillsrc = p_end + 1; 6502dd076b8SGabor Kovesdan filldst = 1; 6512dd076b8SGabor Kovesdan fillcnt = p_ptrn_lines; 6522dd076b8SGabor Kovesdan } else { 6532dd076b8SGabor Kovesdan if (repl_beginning) { 6542dd076b8SGabor Kovesdan if (repl_could_be_missing) { 6552dd076b8SGabor Kovesdan repl_missing = true; 6562dd076b8SGabor Kovesdan goto hunk_done; 6572dd076b8SGabor Kovesdan } 6582dd076b8SGabor Kovesdan fatal("duplicate \"---\" at line %ld--check line numbers at line %ld\n", 6592dd076b8SGabor Kovesdan p_input_line, p_hunk_beg + repl_beginning); 6602dd076b8SGabor Kovesdan } else { 6612dd076b8SGabor Kovesdan fatal("%s \"---\" at line %ld--check line numbers at line %ld\n", 6622dd076b8SGabor Kovesdan (p_end <= p_ptrn_lines 6632dd076b8SGabor Kovesdan ? "Premature" 6642dd076b8SGabor Kovesdan : "Overdue"), 6652dd076b8SGabor Kovesdan p_input_line, p_hunk_beg); 6662dd076b8SGabor Kovesdan } 6672dd076b8SGabor Kovesdan } 6682dd076b8SGabor Kovesdan } 6692dd076b8SGabor Kovesdan repl_beginning = p_end; 670e91a64deSPedro F. Giffuni repl_backtrack_position = ftello(pfp); 6712dd076b8SGabor Kovesdan repl_patch_line = p_input_line; 6722dd076b8SGabor Kovesdan p_line[p_end] = savestr(buf); 6732dd076b8SGabor Kovesdan if (out_of_mem) { 6742dd076b8SGabor Kovesdan p_end--; 6752dd076b8SGabor Kovesdan return false; 6762dd076b8SGabor Kovesdan } 6772dd076b8SGabor Kovesdan p_char[p_end] = '='; 6782dd076b8SGabor Kovesdan for (s = buf; *s && !isdigit((unsigned char)*s); s++) 6792dd076b8SGabor Kovesdan ; 6802dd076b8SGabor Kovesdan if (!*s) 6812dd076b8SGabor Kovesdan malformed(); 682d3fc0cb8SPedro F. Giffuni p_newfirst = strtolinenum(s, &s); 6832dd076b8SGabor Kovesdan if (*s == ',') { 6842dd076b8SGabor Kovesdan for (; *s && !isdigit((unsigned char)*s); s++) 6852dd076b8SGabor Kovesdan ; 6862dd076b8SGabor Kovesdan if (!*s) 6872dd076b8SGabor Kovesdan malformed(); 688d3fc0cb8SPedro F. Giffuni p_repl_lines = strtolinenum(s, &s) - 6892dd076b8SGabor Kovesdan p_newfirst + 1; 690d3fc0cb8SPedro F. Giffuni if (p_repl_lines < 0) 691d3fc0cb8SPedro F. Giffuni malformed(); 6922dd076b8SGabor Kovesdan } else if (p_newfirst) 6932dd076b8SGabor Kovesdan p_repl_lines = 1; 6942dd076b8SGabor Kovesdan else { 6952dd076b8SGabor Kovesdan p_repl_lines = 0; 6962dd076b8SGabor Kovesdan p_newfirst = 1; 6972dd076b8SGabor Kovesdan } 698d3fc0cb8SPedro F. Giffuni if (p_newfirst >= LINENUM_MAX - p_repl_lines || 699d3fc0cb8SPedro F. Giffuni p_repl_lines >= LINENUM_MAX - p_end) 700d3fc0cb8SPedro F. Giffuni malformed(); 7012dd076b8SGabor Kovesdan p_max = p_repl_lines + p_end; 7022dd076b8SGabor Kovesdan if (p_max > MAXHUNKSIZE) 7032dd076b8SGabor Kovesdan fatal("hunk too large (%ld lines) at line %ld: %s", 7042dd076b8SGabor Kovesdan p_max, p_input_line, buf); 7052dd076b8SGabor Kovesdan while (p_max >= hunkmax) 7062dd076b8SGabor Kovesdan grow_hunkmax(); 7072dd076b8SGabor Kovesdan if (p_repl_lines != ptrn_copiable && 7082dd076b8SGabor Kovesdan (p_context != 0 || p_repl_lines != 1)) 7092dd076b8SGabor Kovesdan repl_could_be_missing = false; 7102dd076b8SGabor Kovesdan break; 7112dd076b8SGabor Kovesdan } 7122dd076b8SGabor Kovesdan goto change_line; 7132dd076b8SGabor Kovesdan case '+': 7142dd076b8SGabor Kovesdan case '!': 7152dd076b8SGabor Kovesdan repl_could_be_missing = false; 7162dd076b8SGabor Kovesdan change_line: 7172dd076b8SGabor Kovesdan if (buf[1] == '\n' && canonicalize) 7182dd076b8SGabor Kovesdan strlcpy(buf + 1, " \n", buf_size - 1); 7192b468ebaSPedro F. Giffuni if (!isspace((unsigned char)buf[1]) && 7202b468ebaSPedro F. Giffuni buf[1] != '>' && buf[1] != '<' && 7212dd076b8SGabor Kovesdan repl_beginning && repl_could_be_missing) { 7222dd076b8SGabor Kovesdan repl_missing = true; 7232dd076b8SGabor Kovesdan goto hunk_done; 7242dd076b8SGabor Kovesdan } 7252dd076b8SGabor Kovesdan if (context >= 0) { 7262dd076b8SGabor Kovesdan if (context < p_context) 7272dd076b8SGabor Kovesdan p_context = context; 7282dd076b8SGabor Kovesdan context = -1000; 7292dd076b8SGabor Kovesdan } 7302dd076b8SGabor Kovesdan p_line[p_end] = savestr(buf + 2); 7312dd076b8SGabor Kovesdan if (out_of_mem) { 7322dd076b8SGabor Kovesdan p_end--; 7332dd076b8SGabor Kovesdan return false; 7342dd076b8SGabor Kovesdan } 7352dd076b8SGabor Kovesdan if (p_end == p_ptrn_lines) { 7362dd076b8SGabor Kovesdan if (remove_special_line()) { 7372dd076b8SGabor Kovesdan int l; 7382dd076b8SGabor Kovesdan 7392dd076b8SGabor Kovesdan l = strlen(p_line[p_end]) - 1; 7402dd076b8SGabor Kovesdan (p_line[p_end])[l] = 0; 7412dd076b8SGabor Kovesdan } 7422dd076b8SGabor Kovesdan } 7432dd076b8SGabor Kovesdan break; 7442dd076b8SGabor Kovesdan case '\t': 7452dd076b8SGabor Kovesdan case '\n': /* assume the 2 spaces got eaten */ 7462dd076b8SGabor Kovesdan if (repl_beginning && repl_could_be_missing && 7472dd076b8SGabor Kovesdan (!ptrn_spaces_eaten || 7482dd076b8SGabor Kovesdan diff_type == NEW_CONTEXT_DIFF)) { 7492dd076b8SGabor Kovesdan repl_missing = true; 7502dd076b8SGabor Kovesdan goto hunk_done; 7512dd076b8SGabor Kovesdan } 7522dd076b8SGabor Kovesdan p_line[p_end] = savestr(buf); 7532dd076b8SGabor Kovesdan if (out_of_mem) { 7542dd076b8SGabor Kovesdan p_end--; 7552dd076b8SGabor Kovesdan return false; 7562dd076b8SGabor Kovesdan } 7572dd076b8SGabor Kovesdan if (p_end != p_ptrn_lines + 1) { 7582dd076b8SGabor Kovesdan ptrn_spaces_eaten |= (repl_beginning != 0); 7592dd076b8SGabor Kovesdan context++; 7602dd076b8SGabor Kovesdan if (!repl_beginning) 7612dd076b8SGabor Kovesdan ptrn_copiable++; 7622dd076b8SGabor Kovesdan p_char[p_end] = ' '; 7632dd076b8SGabor Kovesdan } 7642dd076b8SGabor Kovesdan break; 7652dd076b8SGabor Kovesdan case ' ': 7662dd076b8SGabor Kovesdan if (!isspace((unsigned char)buf[1]) && 7672dd076b8SGabor Kovesdan repl_beginning && repl_could_be_missing) { 7682dd076b8SGabor Kovesdan repl_missing = true; 7692dd076b8SGabor Kovesdan goto hunk_done; 7702dd076b8SGabor Kovesdan } 7712dd076b8SGabor Kovesdan context++; 7722dd076b8SGabor Kovesdan if (!repl_beginning) 7732dd076b8SGabor Kovesdan ptrn_copiable++; 7742dd076b8SGabor Kovesdan p_line[p_end] = savestr(buf + 2); 7752dd076b8SGabor Kovesdan if (out_of_mem) { 7762dd076b8SGabor Kovesdan p_end--; 7772dd076b8SGabor Kovesdan return false; 7782dd076b8SGabor Kovesdan } 7792dd076b8SGabor Kovesdan break; 7802dd076b8SGabor Kovesdan default: 7812dd076b8SGabor Kovesdan if (repl_beginning && repl_could_be_missing) { 7822dd076b8SGabor Kovesdan repl_missing = true; 7832dd076b8SGabor Kovesdan goto hunk_done; 7842dd076b8SGabor Kovesdan } 7852dd076b8SGabor Kovesdan malformed(); 7862dd076b8SGabor Kovesdan } 7872dd076b8SGabor Kovesdan /* set up p_len for strncmp() so we don't have to */ 7882dd076b8SGabor Kovesdan /* assume null termination */ 7892dd076b8SGabor Kovesdan if (p_line[p_end]) 7902dd076b8SGabor Kovesdan p_len[p_end] = strlen(p_line[p_end]); 7912dd076b8SGabor Kovesdan else 7922dd076b8SGabor Kovesdan p_len[p_end] = 0; 7932dd076b8SGabor Kovesdan } 7942dd076b8SGabor Kovesdan 7952dd076b8SGabor Kovesdan hunk_done: 7962dd076b8SGabor Kovesdan if (p_end >= 0 && !repl_beginning) 7972dd076b8SGabor Kovesdan fatal("no --- found in patch at line %ld\n", pch_hunk_beg()); 7982dd076b8SGabor Kovesdan 7992dd076b8SGabor Kovesdan if (repl_missing) { 8002dd076b8SGabor Kovesdan 8012dd076b8SGabor Kovesdan /* reset state back to just after --- */ 8022dd076b8SGabor Kovesdan p_input_line = repl_patch_line; 8032dd076b8SGabor Kovesdan for (p_end--; p_end > repl_beginning; p_end--) 8042dd076b8SGabor Kovesdan free(p_line[p_end]); 805e91a64deSPedro F. Giffuni fseeko(pfp, repl_backtrack_position, SEEK_SET); 8062dd076b8SGabor Kovesdan 8072dd076b8SGabor Kovesdan /* redundant 'new' context lines were omitted - set */ 8082dd076b8SGabor Kovesdan /* up to fill them in from the old file context */ 8092dd076b8SGabor Kovesdan if (!p_context && p_repl_lines == 1) { 8102dd076b8SGabor Kovesdan p_repl_lines = 0; 8112dd076b8SGabor Kovesdan p_max--; 8122dd076b8SGabor Kovesdan } 8132dd076b8SGabor Kovesdan fillsrc = 1; 8142dd076b8SGabor Kovesdan filldst = repl_beginning + 1; 8152dd076b8SGabor Kovesdan fillcnt = p_repl_lines; 8162dd076b8SGabor Kovesdan p_end = p_max; 8172dd076b8SGabor Kovesdan } else if (!p_context && fillcnt == 1) { 8182dd076b8SGabor Kovesdan /* the first hunk was a null hunk with no context */ 8192dd076b8SGabor Kovesdan /* and we were expecting one line -- fix it up. */ 8202dd076b8SGabor Kovesdan while (filldst < p_end) { 8212dd076b8SGabor Kovesdan p_line[filldst] = p_line[filldst + 1]; 8222dd076b8SGabor Kovesdan p_char[filldst] = p_char[filldst + 1]; 8232dd076b8SGabor Kovesdan p_len[filldst] = p_len[filldst + 1]; 8242dd076b8SGabor Kovesdan filldst++; 8252dd076b8SGabor Kovesdan } 8262dd076b8SGabor Kovesdan #if 0 8272dd076b8SGabor Kovesdan repl_beginning--; /* this doesn't need to be fixed */ 8282dd076b8SGabor Kovesdan #endif 8292dd076b8SGabor Kovesdan p_end--; 8302dd076b8SGabor Kovesdan p_first++; /* do append rather than insert */ 8312dd076b8SGabor Kovesdan fillcnt = 0; 8322dd076b8SGabor Kovesdan p_ptrn_lines = 0; 8332dd076b8SGabor Kovesdan } 8342dd076b8SGabor Kovesdan if (diff_type == CONTEXT_DIFF && 8352dd076b8SGabor Kovesdan (fillcnt || (p_first > 1 && ptrn_copiable > 2 * p_context))) { 8362dd076b8SGabor Kovesdan if (verbose) 8372dd076b8SGabor Kovesdan say("%s\n%s\n%s\n", 8382dd076b8SGabor Kovesdan "(Fascinating--this is really a new-style context diff but without", 8392dd076b8SGabor Kovesdan "the telltale extra asterisks on the *** line that usually indicate", 8402dd076b8SGabor Kovesdan "the new style...)"); 8412dd076b8SGabor Kovesdan diff_type = NEW_CONTEXT_DIFF; 8422dd076b8SGabor Kovesdan } 8432dd076b8SGabor Kovesdan /* if there were omitted context lines, fill them in now */ 8442dd076b8SGabor Kovesdan if (fillcnt) { 8452dd076b8SGabor Kovesdan p_bfake = filldst; /* remember where not to free() */ 8462dd076b8SGabor Kovesdan p_efake = filldst + fillcnt - 1; 8472dd076b8SGabor Kovesdan while (fillcnt-- > 0) { 8482dd076b8SGabor Kovesdan while (fillsrc <= p_end && p_char[fillsrc] != ' ') 8492dd076b8SGabor Kovesdan fillsrc++; 8502dd076b8SGabor Kovesdan if (fillsrc > p_end) 8512dd076b8SGabor Kovesdan fatal("replacement text or line numbers mangled in hunk at line %ld\n", 8522dd076b8SGabor Kovesdan p_hunk_beg); 8532dd076b8SGabor Kovesdan p_line[filldst] = p_line[fillsrc]; 8542dd076b8SGabor Kovesdan p_char[filldst] = p_char[fillsrc]; 8552dd076b8SGabor Kovesdan p_len[filldst] = p_len[fillsrc]; 8562dd076b8SGabor Kovesdan fillsrc++; 8572dd076b8SGabor Kovesdan filldst++; 8582dd076b8SGabor Kovesdan } 8592dd076b8SGabor Kovesdan while (fillsrc <= p_end && fillsrc != repl_beginning && 8602dd076b8SGabor Kovesdan p_char[fillsrc] != ' ') 8612dd076b8SGabor Kovesdan fillsrc++; 8622dd076b8SGabor Kovesdan #ifdef DEBUGGING 8632dd076b8SGabor Kovesdan if (debug & 64) 8642dd076b8SGabor Kovesdan printf("fillsrc %ld, filldst %ld, rb %ld, e+1 %ld\n", 8652dd076b8SGabor Kovesdan fillsrc, filldst, repl_beginning, p_end + 1); 8662dd076b8SGabor Kovesdan #endif 8672dd076b8SGabor Kovesdan if (fillsrc != p_end + 1 && fillsrc != repl_beginning) 8682dd076b8SGabor Kovesdan malformed(); 8692dd076b8SGabor Kovesdan if (filldst != p_end + 1 && filldst != repl_beginning) 8702dd076b8SGabor Kovesdan malformed(); 8712dd076b8SGabor Kovesdan } 8722dd076b8SGabor Kovesdan if (p_line[p_end] != NULL) { 8732dd076b8SGabor Kovesdan if (remove_special_line()) { 8742dd076b8SGabor Kovesdan p_len[p_end] -= 1; 8752dd076b8SGabor Kovesdan (p_line[p_end])[p_len[p_end]] = 0; 8762dd076b8SGabor Kovesdan } 8772dd076b8SGabor Kovesdan } 8782dd076b8SGabor Kovesdan } else if (diff_type == UNI_DIFF) { 8792dd076b8SGabor Kovesdan LINENUM fillold; /* index of old lines */ 8802dd076b8SGabor Kovesdan LINENUM fillnew; /* index of new lines */ 8812dd076b8SGabor Kovesdan char ch; 8822dd076b8SGabor Kovesdan 883e91a64deSPedro F. Giffuni line_beginning = ftello(pfp); /* file pos of the current line */ 8842dd076b8SGabor Kovesdan len = pgets(true); 8852dd076b8SGabor Kovesdan p_input_line++; 8862dd076b8SGabor Kovesdan if (len == 0 || strnNE(buf, "@@ -", 4)) { 8872dd076b8SGabor Kovesdan next_intuit_at(line_beginning, p_input_line); 8882dd076b8SGabor Kovesdan return false; 8892dd076b8SGabor Kovesdan } 8902dd076b8SGabor Kovesdan s = buf + 4; 8912dd076b8SGabor Kovesdan if (!*s) 8922dd076b8SGabor Kovesdan malformed(); 893d3fc0cb8SPedro F. Giffuni p_first = strtolinenum(s, &s); 8942dd076b8SGabor Kovesdan if (*s == ',') { 895d3fc0cb8SPedro F. Giffuni p_ptrn_lines = strtolinenum(s + 1, &s); 8962dd076b8SGabor Kovesdan } else 8972dd076b8SGabor Kovesdan p_ptrn_lines = 1; 8982dd076b8SGabor Kovesdan if (*s == ' ') 8992dd076b8SGabor Kovesdan s++; 9002dd076b8SGabor Kovesdan if (*s != '+' || !*++s) 9012dd076b8SGabor Kovesdan malformed(); 902d3fc0cb8SPedro F. Giffuni p_newfirst = strtolinenum(s, &s); 9032dd076b8SGabor Kovesdan if (*s == ',') { 904d3fc0cb8SPedro F. Giffuni p_repl_lines = strtolinenum(s + 1, &s); 9052dd076b8SGabor Kovesdan } else 9062dd076b8SGabor Kovesdan p_repl_lines = 1; 9072dd076b8SGabor Kovesdan if (*s == ' ') 9082dd076b8SGabor Kovesdan s++; 9092dd076b8SGabor Kovesdan if (*s != '@') 9102dd076b8SGabor Kovesdan malformed(); 911d3fc0cb8SPedro F. Giffuni if (p_first >= LINENUM_MAX - p_ptrn_lines || 912d3fc0cb8SPedro F. Giffuni p_newfirst > LINENUM_MAX - p_repl_lines || 913d3fc0cb8SPedro F. Giffuni p_ptrn_lines >= LINENUM_MAX - p_repl_lines - 1) 914d3fc0cb8SPedro F. Giffuni malformed(); 9152dd076b8SGabor Kovesdan if (!p_ptrn_lines) 9162dd076b8SGabor Kovesdan p_first++; /* do append rather than insert */ 9172dd076b8SGabor Kovesdan p_max = p_ptrn_lines + p_repl_lines + 1; 9182dd076b8SGabor Kovesdan while (p_max >= hunkmax) 9192dd076b8SGabor Kovesdan grow_hunkmax(); 9202dd076b8SGabor Kovesdan fillold = 1; 9212dd076b8SGabor Kovesdan fillnew = fillold + p_ptrn_lines; 9222dd076b8SGabor Kovesdan p_end = fillnew + p_repl_lines; 9232dd076b8SGabor Kovesdan snprintf(buf, buf_size, "*** %ld,%ld ****\n", p_first, 9242dd076b8SGabor Kovesdan p_first + p_ptrn_lines - 1); 9252dd076b8SGabor Kovesdan p_line[0] = savestr(buf); 9262dd076b8SGabor Kovesdan if (out_of_mem) { 9272dd076b8SGabor Kovesdan p_end = -1; 9282dd076b8SGabor Kovesdan return false; 9292dd076b8SGabor Kovesdan } 9302dd076b8SGabor Kovesdan p_char[0] = '*'; 9312dd076b8SGabor Kovesdan snprintf(buf, buf_size, "--- %ld,%ld ----\n", p_newfirst, 9322dd076b8SGabor Kovesdan p_newfirst + p_repl_lines - 1); 9332dd076b8SGabor Kovesdan p_line[fillnew] = savestr(buf); 9342dd076b8SGabor Kovesdan if (out_of_mem) { 9352dd076b8SGabor Kovesdan p_end = 0; 9362dd076b8SGabor Kovesdan return false; 9372dd076b8SGabor Kovesdan } 9382dd076b8SGabor Kovesdan p_char[fillnew++] = '='; 9392dd076b8SGabor Kovesdan p_context = 100; 9402dd076b8SGabor Kovesdan context = 0; 9412dd076b8SGabor Kovesdan p_hunk_beg = p_input_line + 1; 9422dd076b8SGabor Kovesdan while (fillold <= p_ptrn_lines || fillnew <= p_end) { 943e91a64deSPedro F. Giffuni line_beginning = ftello(pfp); 9442dd076b8SGabor Kovesdan len = pgets(true); 9452dd076b8SGabor Kovesdan p_input_line++; 9462dd076b8SGabor Kovesdan if (len == 0) { 9472dd076b8SGabor Kovesdan if (p_max - fillnew < 3) { 9482dd076b8SGabor Kovesdan /* assume blank lines got chopped */ 9492dd076b8SGabor Kovesdan strlcpy(buf, " \n", buf_size); 9502dd076b8SGabor Kovesdan } else { 9512dd076b8SGabor Kovesdan fatal("unexpected end of file in patch\n"); 9522dd076b8SGabor Kovesdan } 9532dd076b8SGabor Kovesdan } 9542dd076b8SGabor Kovesdan if (*buf == '\t' || *buf == '\n') { 9552dd076b8SGabor Kovesdan ch = ' '; /* assume the space got eaten */ 9562dd076b8SGabor Kovesdan s = savestr(buf); 9572dd076b8SGabor Kovesdan } else { 9582dd076b8SGabor Kovesdan ch = *buf; 9592dd076b8SGabor Kovesdan s = savestr(buf + 1); 9602dd076b8SGabor Kovesdan } 9612dd076b8SGabor Kovesdan if (out_of_mem) { 9622dd076b8SGabor Kovesdan while (--fillnew > p_ptrn_lines) 9632dd076b8SGabor Kovesdan free(p_line[fillnew]); 9642dd076b8SGabor Kovesdan p_end = fillold - 1; 9652dd076b8SGabor Kovesdan return false; 9662dd076b8SGabor Kovesdan } 9672dd076b8SGabor Kovesdan switch (ch) { 9682dd076b8SGabor Kovesdan case '-': 9692dd076b8SGabor Kovesdan if (fillold > p_ptrn_lines) { 9702dd076b8SGabor Kovesdan free(s); 9712dd076b8SGabor Kovesdan p_end = fillnew - 1; 9722dd076b8SGabor Kovesdan malformed(); 9732dd076b8SGabor Kovesdan } 9742dd076b8SGabor Kovesdan p_char[fillold] = ch; 9752dd076b8SGabor Kovesdan p_line[fillold] = s; 9762dd076b8SGabor Kovesdan p_len[fillold++] = strlen(s); 9772dd076b8SGabor Kovesdan if (fillold > p_ptrn_lines) { 9782dd076b8SGabor Kovesdan if (remove_special_line()) { 9792dd076b8SGabor Kovesdan p_len[fillold - 1] -= 1; 9802dd076b8SGabor Kovesdan s[p_len[fillold - 1]] = 0; 9812dd076b8SGabor Kovesdan } 9822dd076b8SGabor Kovesdan } 9832dd076b8SGabor Kovesdan break; 9842dd076b8SGabor Kovesdan case '=': 9852dd076b8SGabor Kovesdan ch = ' '; 9862dd076b8SGabor Kovesdan /* FALL THROUGH */ 9872dd076b8SGabor Kovesdan case ' ': 9882dd076b8SGabor Kovesdan if (fillold > p_ptrn_lines) { 9892dd076b8SGabor Kovesdan free(s); 9902dd076b8SGabor Kovesdan while (--fillnew > p_ptrn_lines) 9912dd076b8SGabor Kovesdan free(p_line[fillnew]); 9922dd076b8SGabor Kovesdan p_end = fillold - 1; 9932dd076b8SGabor Kovesdan malformed(); 9942dd076b8SGabor Kovesdan } 9952dd076b8SGabor Kovesdan context++; 9962dd076b8SGabor Kovesdan p_char[fillold] = ch; 9972dd076b8SGabor Kovesdan p_line[fillold] = s; 9982dd076b8SGabor Kovesdan p_len[fillold++] = strlen(s); 9992dd076b8SGabor Kovesdan s = savestr(s); 10002dd076b8SGabor Kovesdan if (out_of_mem) { 10012dd076b8SGabor Kovesdan while (--fillnew > p_ptrn_lines) 10022dd076b8SGabor Kovesdan free(p_line[fillnew]); 10032dd076b8SGabor Kovesdan p_end = fillold - 1; 10042dd076b8SGabor Kovesdan return false; 10052dd076b8SGabor Kovesdan } 10062dd076b8SGabor Kovesdan if (fillold > p_ptrn_lines) { 10072dd076b8SGabor Kovesdan if (remove_special_line()) { 10082dd076b8SGabor Kovesdan p_len[fillold - 1] -= 1; 10092dd076b8SGabor Kovesdan s[p_len[fillold - 1]] = 0; 10102dd076b8SGabor Kovesdan } 10112dd076b8SGabor Kovesdan } 10122dd076b8SGabor Kovesdan /* FALL THROUGH */ 10132dd076b8SGabor Kovesdan case '+': 10142dd076b8SGabor Kovesdan if (fillnew > p_end) { 10152dd076b8SGabor Kovesdan free(s); 10162dd076b8SGabor Kovesdan while (--fillnew > p_ptrn_lines) 10172dd076b8SGabor Kovesdan free(p_line[fillnew]); 10182dd076b8SGabor Kovesdan p_end = fillold - 1; 10192dd076b8SGabor Kovesdan malformed(); 10202dd076b8SGabor Kovesdan } 10212dd076b8SGabor Kovesdan p_char[fillnew] = ch; 10222dd076b8SGabor Kovesdan p_line[fillnew] = s; 10232dd076b8SGabor Kovesdan p_len[fillnew++] = strlen(s); 10242dd076b8SGabor Kovesdan if (fillold > p_ptrn_lines) { 10252dd076b8SGabor Kovesdan if (remove_special_line()) { 10262dd076b8SGabor Kovesdan p_len[fillnew - 1] -= 1; 10272dd076b8SGabor Kovesdan s[p_len[fillnew - 1]] = 0; 10282dd076b8SGabor Kovesdan } 10292dd076b8SGabor Kovesdan } 10302dd076b8SGabor Kovesdan break; 10312dd076b8SGabor Kovesdan default: 10322dd076b8SGabor Kovesdan p_end = fillnew; 10332dd076b8SGabor Kovesdan malformed(); 10342dd076b8SGabor Kovesdan } 10352dd076b8SGabor Kovesdan if (ch != ' ' && context > 0) { 10362dd076b8SGabor Kovesdan if (context < p_context) 10372dd076b8SGabor Kovesdan p_context = context; 10382dd076b8SGabor Kovesdan context = -1000; 10392dd076b8SGabor Kovesdan } 10402dd076b8SGabor Kovesdan } /* while */ 10412dd076b8SGabor Kovesdan } else { /* normal diff--fake it up */ 10422dd076b8SGabor Kovesdan char hunk_type; 10432dd076b8SGabor Kovesdan int i; 10442dd076b8SGabor Kovesdan LINENUM min, max; 10452dd076b8SGabor Kovesdan 1046e91a64deSPedro F. Giffuni line_beginning = ftello(pfp); 10472dd076b8SGabor Kovesdan p_context = 0; 10482dd076b8SGabor Kovesdan len = pgets(true); 10492dd076b8SGabor Kovesdan p_input_line++; 10502dd076b8SGabor Kovesdan if (len == 0 || !isdigit((unsigned char)*buf)) { 10512dd076b8SGabor Kovesdan next_intuit_at(line_beginning, p_input_line); 10522dd076b8SGabor Kovesdan return false; 10532dd076b8SGabor Kovesdan } 1054d3fc0cb8SPedro F. Giffuni p_first = strtolinenum(buf, &s); 10552dd076b8SGabor Kovesdan if (*s == ',') { 1056d3fc0cb8SPedro F. Giffuni p_ptrn_lines = strtolinenum(s + 1, &s) - p_first + 1; 1057d3fc0cb8SPedro F. Giffuni if (p_ptrn_lines < 0) 1058d3fc0cb8SPedro F. Giffuni malformed(); 10592dd076b8SGabor Kovesdan } else 10602dd076b8SGabor Kovesdan p_ptrn_lines = (*s != 'a'); 10612dd076b8SGabor Kovesdan hunk_type = *s; 10622dd076b8SGabor Kovesdan if (hunk_type == 'a') 10632dd076b8SGabor Kovesdan p_first++; /* do append rather than insert */ 1064d3fc0cb8SPedro F. Giffuni min = strtolinenum(s + 1, &s); 10652dd076b8SGabor Kovesdan if (*s == ',') 1066d3fc0cb8SPedro F. Giffuni max = strtolinenum(s + 1, &s); 10672dd076b8SGabor Kovesdan else 10682dd076b8SGabor Kovesdan max = min; 1069d3fc0cb8SPedro F. Giffuni if (min < 0 || min > max || max - min == LINENUM_MAX) 1070d3fc0cb8SPedro F. Giffuni malformed(); 10712dd076b8SGabor Kovesdan if (hunk_type == 'd') 10722dd076b8SGabor Kovesdan min++; 1073d3fc0cb8SPedro F. Giffuni p_newfirst = min; 1074d3fc0cb8SPedro F. Giffuni p_repl_lines = max - min + 1; 1075d3fc0cb8SPedro F. Giffuni if (p_newfirst > LINENUM_MAX - p_repl_lines || 1076d3fc0cb8SPedro F. Giffuni p_ptrn_lines >= LINENUM_MAX - p_repl_lines - 1) 1077d3fc0cb8SPedro F. Giffuni malformed(); 1078d3fc0cb8SPedro F. Giffuni p_end = p_ptrn_lines + p_repl_lines + 1; 10792dd076b8SGabor Kovesdan if (p_end > MAXHUNKSIZE) 10802dd076b8SGabor Kovesdan fatal("hunk too large (%ld lines) at line %ld: %s", 10812dd076b8SGabor Kovesdan p_end, p_input_line, buf); 10822dd076b8SGabor Kovesdan while (p_end >= hunkmax) 10832dd076b8SGabor Kovesdan grow_hunkmax(); 10842dd076b8SGabor Kovesdan snprintf(buf, buf_size, "*** %ld,%ld\n", p_first, 10852dd076b8SGabor Kovesdan p_first + p_ptrn_lines - 1); 10862dd076b8SGabor Kovesdan p_line[0] = savestr(buf); 10872dd076b8SGabor Kovesdan if (out_of_mem) { 10882dd076b8SGabor Kovesdan p_end = -1; 10892dd076b8SGabor Kovesdan return false; 10902dd076b8SGabor Kovesdan } 10912dd076b8SGabor Kovesdan p_char[0] = '*'; 10922dd076b8SGabor Kovesdan for (i = 1; i <= p_ptrn_lines; i++) { 10932dd076b8SGabor Kovesdan len = pgets(true); 10942dd076b8SGabor Kovesdan p_input_line++; 10952dd076b8SGabor Kovesdan if (len == 0) 10962dd076b8SGabor Kovesdan fatal("unexpected end of file in patch at line %ld\n", 10972dd076b8SGabor Kovesdan p_input_line); 10982dd076b8SGabor Kovesdan if (*buf != '<') 10992dd076b8SGabor Kovesdan fatal("< expected at line %ld of patch\n", 11002dd076b8SGabor Kovesdan p_input_line); 11012dd076b8SGabor Kovesdan p_line[i] = savestr(buf + 2); 11022dd076b8SGabor Kovesdan if (out_of_mem) { 11032dd076b8SGabor Kovesdan p_end = i - 1; 11042dd076b8SGabor Kovesdan return false; 11052dd076b8SGabor Kovesdan } 11062dd076b8SGabor Kovesdan p_len[i] = strlen(p_line[i]); 11072dd076b8SGabor Kovesdan p_char[i] = '-'; 11082dd076b8SGabor Kovesdan } 11092dd076b8SGabor Kovesdan 11102dd076b8SGabor Kovesdan if (remove_special_line()) { 11112dd076b8SGabor Kovesdan p_len[i - 1] -= 1; 11122dd076b8SGabor Kovesdan (p_line[i - 1])[p_len[i - 1]] = 0; 11132dd076b8SGabor Kovesdan } 11142dd076b8SGabor Kovesdan if (hunk_type == 'c') { 11152dd076b8SGabor Kovesdan len = pgets(true); 11162dd076b8SGabor Kovesdan p_input_line++; 11172dd076b8SGabor Kovesdan if (len == 0) 11182dd076b8SGabor Kovesdan fatal("unexpected end of file in patch at line %ld\n", 11192dd076b8SGabor Kovesdan p_input_line); 11202dd076b8SGabor Kovesdan if (*buf != '-') 11212dd076b8SGabor Kovesdan fatal("--- expected at line %ld of patch\n", 11222dd076b8SGabor Kovesdan p_input_line); 11232dd076b8SGabor Kovesdan } 11242dd076b8SGabor Kovesdan snprintf(buf, buf_size, "--- %ld,%ld\n", min, max); 11252dd076b8SGabor Kovesdan p_line[i] = savestr(buf); 11262dd076b8SGabor Kovesdan if (out_of_mem) { 11272dd076b8SGabor Kovesdan p_end = i - 1; 11282dd076b8SGabor Kovesdan return false; 11292dd076b8SGabor Kovesdan } 11302dd076b8SGabor Kovesdan p_char[i] = '='; 11312dd076b8SGabor Kovesdan for (i++; i <= p_end; i++) { 11322dd076b8SGabor Kovesdan len = pgets(true); 11332dd076b8SGabor Kovesdan p_input_line++; 11342dd076b8SGabor Kovesdan if (len == 0) 11352dd076b8SGabor Kovesdan fatal("unexpected end of file in patch at line %ld\n", 11362dd076b8SGabor Kovesdan p_input_line); 11372dd076b8SGabor Kovesdan if (*buf != '>') 11382dd076b8SGabor Kovesdan fatal("> expected at line %ld of patch\n", 11392dd076b8SGabor Kovesdan p_input_line); 11404835edfaSKyle Evans /* Don't overrun if we don't have enough line */ 11414835edfaSKyle Evans if (len > 2) 11422dd076b8SGabor Kovesdan p_line[i] = savestr(buf + 2); 11434835edfaSKyle Evans else 11444835edfaSKyle Evans p_line[i] = savestr(""); 11454835edfaSKyle Evans 11462dd076b8SGabor Kovesdan if (out_of_mem) { 11472dd076b8SGabor Kovesdan p_end = i - 1; 11482dd076b8SGabor Kovesdan return false; 11492dd076b8SGabor Kovesdan } 11502dd076b8SGabor Kovesdan p_len[i] = strlen(p_line[i]); 11512dd076b8SGabor Kovesdan p_char[i] = '+'; 11522dd076b8SGabor Kovesdan } 11532dd076b8SGabor Kovesdan 11542dd076b8SGabor Kovesdan if (remove_special_line()) { 11552dd076b8SGabor Kovesdan p_len[i - 1] -= 1; 11562dd076b8SGabor Kovesdan (p_line[i - 1])[p_len[i - 1]] = 0; 11572dd076b8SGabor Kovesdan } 11582dd076b8SGabor Kovesdan } 11592dd076b8SGabor Kovesdan if (reverse) /* backwards patch? */ 11602dd076b8SGabor Kovesdan if (!pch_swap()) 11612dd076b8SGabor Kovesdan say("Not enough memory to swap next hunk!\n"); 11622dd076b8SGabor Kovesdan #ifdef DEBUGGING 11632dd076b8SGabor Kovesdan if (debug & 2) { 11642c4eed47SPedro F. Giffuni LINENUM i; 11652dd076b8SGabor Kovesdan char special; 11662dd076b8SGabor Kovesdan 11672dd076b8SGabor Kovesdan for (i = 0; i <= p_end; i++) { 11682dd076b8SGabor Kovesdan if (i == p_ptrn_lines) 11692dd076b8SGabor Kovesdan special = '^'; 11702dd076b8SGabor Kovesdan else 11712dd076b8SGabor Kovesdan special = ' '; 11722c4eed47SPedro F. Giffuni fprintf(stderr, "%3ld %c %c %s", i, p_char[i], 11732dd076b8SGabor Kovesdan special, p_line[i]); 11742dd076b8SGabor Kovesdan fflush(stderr); 11752dd076b8SGabor Kovesdan } 11762dd076b8SGabor Kovesdan } 11772dd076b8SGabor Kovesdan #endif 11782dd076b8SGabor Kovesdan if (p_end + 1 < hunkmax)/* paranoia reigns supreme... */ 11792dd076b8SGabor Kovesdan p_char[p_end + 1] = '^'; /* add a stopper for apply_hunk */ 11802dd076b8SGabor Kovesdan return true; 11812dd076b8SGabor Kovesdan } 11822dd076b8SGabor Kovesdan 11832dd076b8SGabor Kovesdan /* 11842dd076b8SGabor Kovesdan * Input a line from the patch file. 11852dd076b8SGabor Kovesdan * Worry about indentation if do_indent is true. 11862dd076b8SGabor Kovesdan * The line is read directly into the buf global variable which 11872dd076b8SGabor Kovesdan * is resized if necessary in order to hold the complete line. 11882dd076b8SGabor Kovesdan * Returns the number of characters read including the terminating 11892dd076b8SGabor Kovesdan * '\n', if any. 11902dd076b8SGabor Kovesdan */ 11912dd076b8SGabor Kovesdan size_t 11922dd076b8SGabor Kovesdan pgets(bool do_indent) 11932dd076b8SGabor Kovesdan { 11942dd076b8SGabor Kovesdan char *line; 11952dd076b8SGabor Kovesdan size_t len; 11962dd076b8SGabor Kovesdan int indent = 0, skipped = 0; 11972dd076b8SGabor Kovesdan 11982dd076b8SGabor Kovesdan line = fgetln(pfp, &len); 11992dd076b8SGabor Kovesdan if (line != NULL) { 12002dd076b8SGabor Kovesdan if (len + 1 > buf_size) { 12012dd076b8SGabor Kovesdan while (len + 1 > buf_size) 12022dd076b8SGabor Kovesdan buf_size *= 2; 12032dd076b8SGabor Kovesdan free(buf); 12042dd076b8SGabor Kovesdan buf = malloc(buf_size); 12052dd076b8SGabor Kovesdan if (buf == NULL) 12062dd076b8SGabor Kovesdan fatal("out of memory\n"); 12072dd076b8SGabor Kovesdan } 12082dd076b8SGabor Kovesdan if (do_indent == 1 && p_indent) { 12092dd076b8SGabor Kovesdan for (; 12102dd076b8SGabor Kovesdan indent < p_indent && (*line == ' ' || *line == '\t' || *line == 'X'); 12112dd076b8SGabor Kovesdan line++, skipped++) { 12122dd076b8SGabor Kovesdan if (*line == '\t') 12132dd076b8SGabor Kovesdan indent += 8 - (indent %7); 12142dd076b8SGabor Kovesdan else 12152dd076b8SGabor Kovesdan indent++; 12162dd076b8SGabor Kovesdan } 12172dd076b8SGabor Kovesdan } 12186d85e39bSDavid E. O'Brien memcpy(buf, line, len - skipped); 12192dd076b8SGabor Kovesdan buf[len - skipped] = '\0'; 12202dd076b8SGabor Kovesdan } 12212dd076b8SGabor Kovesdan return len; 12222dd076b8SGabor Kovesdan } 12232dd076b8SGabor Kovesdan 12242dd076b8SGabor Kovesdan 12252dd076b8SGabor Kovesdan /* 12262dd076b8SGabor Kovesdan * Reverse the old and new portions of the current hunk. 12272dd076b8SGabor Kovesdan */ 12282dd076b8SGabor Kovesdan bool 12292dd076b8SGabor Kovesdan pch_swap(void) 12302dd076b8SGabor Kovesdan { 12312dd076b8SGabor Kovesdan char **tp_line; /* the text of the hunk */ 12324f548c19SPedro F. Giffuni unsigned short *tp_len;/* length of each line */ 12332dd076b8SGabor Kovesdan char *tp_char; /* +, -, and ! */ 12342dd076b8SGabor Kovesdan LINENUM i; 12352dd076b8SGabor Kovesdan LINENUM n; 12362dd076b8SGabor Kovesdan bool blankline = false; 12372dd076b8SGabor Kovesdan char *s; 12382dd076b8SGabor Kovesdan 12392dd076b8SGabor Kovesdan i = p_first; 12402dd076b8SGabor Kovesdan p_first = p_newfirst; 12412dd076b8SGabor Kovesdan p_newfirst = i; 12422dd076b8SGabor Kovesdan 12432dd076b8SGabor Kovesdan /* make a scratch copy */ 12442dd076b8SGabor Kovesdan 12452dd076b8SGabor Kovesdan tp_line = p_line; 12462dd076b8SGabor Kovesdan tp_len = p_len; 12472dd076b8SGabor Kovesdan tp_char = p_char; 12482dd076b8SGabor Kovesdan p_line = NULL; /* force set_hunkmax to allocate again */ 12492dd076b8SGabor Kovesdan p_len = NULL; 12502dd076b8SGabor Kovesdan p_char = NULL; 12512dd076b8SGabor Kovesdan set_hunkmax(); 12522dd076b8SGabor Kovesdan if (p_line == NULL || p_len == NULL || p_char == NULL) { 12532dd076b8SGabor Kovesdan 12542dd076b8SGabor Kovesdan free(p_line); 12552dd076b8SGabor Kovesdan p_line = tp_line; 12562dd076b8SGabor Kovesdan free(p_len); 12572dd076b8SGabor Kovesdan p_len = tp_len; 12582dd076b8SGabor Kovesdan free(p_char); 12592dd076b8SGabor Kovesdan p_char = tp_char; 12602dd076b8SGabor Kovesdan return false; /* not enough memory to swap hunk! */ 12612dd076b8SGabor Kovesdan } 12622dd076b8SGabor Kovesdan /* now turn the new into the old */ 12632dd076b8SGabor Kovesdan 12642dd076b8SGabor Kovesdan i = p_ptrn_lines + 1; 12652dd076b8SGabor Kovesdan if (tp_char[i] == '\n') { /* account for possible blank line */ 12662dd076b8SGabor Kovesdan blankline = true; 12672dd076b8SGabor Kovesdan i++; 12682dd076b8SGabor Kovesdan } 12692dd076b8SGabor Kovesdan if (p_efake >= 0) { /* fix non-freeable ptr range */ 12702dd076b8SGabor Kovesdan if (p_efake <= i) 12712dd076b8SGabor Kovesdan n = p_end - i + 1; 12722dd076b8SGabor Kovesdan else 12732dd076b8SGabor Kovesdan n = -i; 12742dd076b8SGabor Kovesdan p_efake += n; 12752dd076b8SGabor Kovesdan p_bfake += n; 12762dd076b8SGabor Kovesdan } 12772dd076b8SGabor Kovesdan for (n = 0; i <= p_end; i++, n++) { 12782dd076b8SGabor Kovesdan p_line[n] = tp_line[i]; 12792dd076b8SGabor Kovesdan p_char[n] = tp_char[i]; 12802dd076b8SGabor Kovesdan if (p_char[n] == '+') 12812dd076b8SGabor Kovesdan p_char[n] = '-'; 12822dd076b8SGabor Kovesdan p_len[n] = tp_len[i]; 12832dd076b8SGabor Kovesdan } 12842dd076b8SGabor Kovesdan if (blankline) { 12852dd076b8SGabor Kovesdan i = p_ptrn_lines + 1; 12862dd076b8SGabor Kovesdan p_line[n] = tp_line[i]; 12872dd076b8SGabor Kovesdan p_char[n] = tp_char[i]; 12882dd076b8SGabor Kovesdan p_len[n] = tp_len[i]; 12892dd076b8SGabor Kovesdan n++; 12902dd076b8SGabor Kovesdan } 12912dd076b8SGabor Kovesdan if (p_char[0] != '=') 12922dd076b8SGabor Kovesdan fatal("Malformed patch at line %ld: expected '=' found '%c'\n", 12932dd076b8SGabor Kovesdan p_input_line, p_char[0]); 12942dd076b8SGabor Kovesdan p_char[0] = '*'; 12952dd076b8SGabor Kovesdan for (s = p_line[0]; *s; s++) 12962dd076b8SGabor Kovesdan if (*s == '-') 12972dd076b8SGabor Kovesdan *s = '*'; 12982dd076b8SGabor Kovesdan 12992dd076b8SGabor Kovesdan /* now turn the old into the new */ 13002dd076b8SGabor Kovesdan 13012dd076b8SGabor Kovesdan if (p_char[0] != '*') 13022dd076b8SGabor Kovesdan fatal("Malformed patch at line %ld: expected '*' found '%c'\n", 13032dd076b8SGabor Kovesdan p_input_line, p_char[0]); 13042dd076b8SGabor Kovesdan tp_char[0] = '='; 13052dd076b8SGabor Kovesdan for (s = tp_line[0]; *s; s++) 13062dd076b8SGabor Kovesdan if (*s == '*') 13072dd076b8SGabor Kovesdan *s = '-'; 13082dd076b8SGabor Kovesdan for (i = 0; n <= p_end; i++, n++) { 13092dd076b8SGabor Kovesdan p_line[n] = tp_line[i]; 13102dd076b8SGabor Kovesdan p_char[n] = tp_char[i]; 13112dd076b8SGabor Kovesdan if (p_char[n] == '-') 13122dd076b8SGabor Kovesdan p_char[n] = '+'; 13132dd076b8SGabor Kovesdan p_len[n] = tp_len[i]; 13142dd076b8SGabor Kovesdan } 13152dd076b8SGabor Kovesdan 13162dd076b8SGabor Kovesdan if (i != p_ptrn_lines + 1) 13172dd076b8SGabor Kovesdan fatal("Malformed patch at line %ld: expected %ld lines, " 13182dd076b8SGabor Kovesdan "got %ld\n", 13192dd076b8SGabor Kovesdan p_input_line, p_ptrn_lines + 1, i); 13202dd076b8SGabor Kovesdan 13212dd076b8SGabor Kovesdan i = p_ptrn_lines; 13222dd076b8SGabor Kovesdan p_ptrn_lines = p_repl_lines; 13232dd076b8SGabor Kovesdan p_repl_lines = i; 13242dd076b8SGabor Kovesdan 13252dd076b8SGabor Kovesdan free(tp_line); 13262dd076b8SGabor Kovesdan free(tp_len); 13272dd076b8SGabor Kovesdan free(tp_char); 13282dd076b8SGabor Kovesdan 13292dd076b8SGabor Kovesdan return true; 13302dd076b8SGabor Kovesdan } 13312dd076b8SGabor Kovesdan 13322dd076b8SGabor Kovesdan /* 13332dd076b8SGabor Kovesdan * Return the specified line position in the old file of the old context. 13342dd076b8SGabor Kovesdan */ 13352dd076b8SGabor Kovesdan LINENUM 13362dd076b8SGabor Kovesdan pch_first(void) 13372dd076b8SGabor Kovesdan { 13382dd076b8SGabor Kovesdan return p_first; 13392dd076b8SGabor Kovesdan } 13402dd076b8SGabor Kovesdan 13412dd076b8SGabor Kovesdan /* 13422dd076b8SGabor Kovesdan * Return the number of lines of old context. 13432dd076b8SGabor Kovesdan */ 13442dd076b8SGabor Kovesdan LINENUM 13452dd076b8SGabor Kovesdan pch_ptrn_lines(void) 13462dd076b8SGabor Kovesdan { 13472dd076b8SGabor Kovesdan return p_ptrn_lines; 13482dd076b8SGabor Kovesdan } 13492dd076b8SGabor Kovesdan 13502dd076b8SGabor Kovesdan /* 13512dd076b8SGabor Kovesdan * Return the probable line position in the new file of the first line. 13522dd076b8SGabor Kovesdan */ 13532dd076b8SGabor Kovesdan LINENUM 13542dd076b8SGabor Kovesdan pch_newfirst(void) 13552dd076b8SGabor Kovesdan { 13562dd076b8SGabor Kovesdan return p_newfirst; 13572dd076b8SGabor Kovesdan } 13582dd076b8SGabor Kovesdan 13592dd076b8SGabor Kovesdan /* 13602dd076b8SGabor Kovesdan * Return the number of lines in the replacement text including context. 13612dd076b8SGabor Kovesdan */ 13622dd076b8SGabor Kovesdan LINENUM 13632dd076b8SGabor Kovesdan pch_repl_lines(void) 13642dd076b8SGabor Kovesdan { 13652dd076b8SGabor Kovesdan return p_repl_lines; 13662dd076b8SGabor Kovesdan } 13672dd076b8SGabor Kovesdan 13682dd076b8SGabor Kovesdan /* 13692dd076b8SGabor Kovesdan * Return the number of lines in the whole hunk. 13702dd076b8SGabor Kovesdan */ 13712dd076b8SGabor Kovesdan LINENUM 13722dd076b8SGabor Kovesdan pch_end(void) 13732dd076b8SGabor Kovesdan { 13742dd076b8SGabor Kovesdan return p_end; 13752dd076b8SGabor Kovesdan } 13762dd076b8SGabor Kovesdan 13772dd076b8SGabor Kovesdan /* 13782dd076b8SGabor Kovesdan * Return the number of context lines before the first changed line. 13792dd076b8SGabor Kovesdan */ 13802dd076b8SGabor Kovesdan LINENUM 13812dd076b8SGabor Kovesdan pch_context(void) 13822dd076b8SGabor Kovesdan { 13832dd076b8SGabor Kovesdan return p_context; 13842dd076b8SGabor Kovesdan } 13852dd076b8SGabor Kovesdan 13862dd076b8SGabor Kovesdan /* 13872dd076b8SGabor Kovesdan * Return the length of a particular patch line. 13882dd076b8SGabor Kovesdan */ 13894f548c19SPedro F. Giffuni unsigned short 13902dd076b8SGabor Kovesdan pch_line_len(LINENUM line) 13912dd076b8SGabor Kovesdan { 13922dd076b8SGabor Kovesdan return p_len[line]; 13932dd076b8SGabor Kovesdan } 13942dd076b8SGabor Kovesdan 13952dd076b8SGabor Kovesdan /* 13962dd076b8SGabor Kovesdan * Return the control character (+, -, *, !, etc) for a patch line. 13972dd076b8SGabor Kovesdan */ 13982dd076b8SGabor Kovesdan char 13992dd076b8SGabor Kovesdan pch_char(LINENUM line) 14002dd076b8SGabor Kovesdan { 14012dd076b8SGabor Kovesdan return p_char[line]; 14022dd076b8SGabor Kovesdan } 14032dd076b8SGabor Kovesdan 14042dd076b8SGabor Kovesdan /* 14052dd076b8SGabor Kovesdan * Return a pointer to a particular patch line. 14062dd076b8SGabor Kovesdan */ 14072dd076b8SGabor Kovesdan char * 14082dd076b8SGabor Kovesdan pfetch(LINENUM line) 14092dd076b8SGabor Kovesdan { 14102dd076b8SGabor Kovesdan return p_line[line]; 14112dd076b8SGabor Kovesdan } 14122dd076b8SGabor Kovesdan 14132dd076b8SGabor Kovesdan /* 14142dd076b8SGabor Kovesdan * Return where in the patch file this hunk began, for error messages. 14152dd076b8SGabor Kovesdan */ 14162dd076b8SGabor Kovesdan LINENUM 14172dd076b8SGabor Kovesdan pch_hunk_beg(void) 14182dd076b8SGabor Kovesdan { 14192dd076b8SGabor Kovesdan return p_hunk_beg; 14202dd076b8SGabor Kovesdan } 14212dd076b8SGabor Kovesdan 14222dd076b8SGabor Kovesdan /* 14232dd076b8SGabor Kovesdan * Apply an ed script by feeding ed itself. 14242dd076b8SGabor Kovesdan */ 14252dd076b8SGabor Kovesdan void 14262dd076b8SGabor Kovesdan do_ed_script(void) 14272dd076b8SGabor Kovesdan { 14282dd076b8SGabor Kovesdan char *t; 1429e91a64deSPedro F. Giffuni off_t beginning_of_this_line; 14302dd076b8SGabor Kovesdan FILE *pipefp = NULL; 14313fd78bfaSXin LI int continuation; 14322dd076b8SGabor Kovesdan 14332dd076b8SGabor Kovesdan if (!skip_rest_of_patch) { 14342dd076b8SGabor Kovesdan if (copy_file(filearg[0], TMPOUTNAME) < 0) { 14352dd076b8SGabor Kovesdan unlink(TMPOUTNAME); 14362dd076b8SGabor Kovesdan fatal("can't create temp file %s", TMPOUTNAME); 14372dd076b8SGabor Kovesdan } 14383fd78bfaSXin LI snprintf(buf, buf_size, "%s%s%s", _PATH_RED, 14392dd076b8SGabor Kovesdan verbose ? " " : " -s ", TMPOUTNAME); 14402dd076b8SGabor Kovesdan pipefp = popen(buf, "w"); 14412dd076b8SGabor Kovesdan } 14422dd076b8SGabor Kovesdan for (;;) { 1443e91a64deSPedro F. Giffuni beginning_of_this_line = ftello(pfp); 14442dd076b8SGabor Kovesdan if (pgets(true) == 0) { 14452dd076b8SGabor Kovesdan next_intuit_at(beginning_of_this_line, p_input_line); 14462dd076b8SGabor Kovesdan break; 14472dd076b8SGabor Kovesdan } 14482dd076b8SGabor Kovesdan p_input_line++; 14492dd076b8SGabor Kovesdan for (t = buf; isdigit((unsigned char)*t) || *t == ','; t++) 14502dd076b8SGabor Kovesdan ; 14512dd076b8SGabor Kovesdan /* POSIX defines allowed commands as {a,c,d,i,s} */ 14522b468ebaSPedro F. Giffuni if (isdigit((unsigned char)*buf) && 14532b468ebaSPedro F. Giffuni (*t == 'a' || *t == 'c' || *t == 'd' || *t == 'i' || *t == 's')) { 14542dd076b8SGabor Kovesdan if (pipefp != NULL) 14552dd076b8SGabor Kovesdan fputs(buf, pipefp); 14563fd78bfaSXin LI if (*t == 's') { 14573fd78bfaSXin LI for (;;) { 14583fd78bfaSXin LI continuation = 0; 14593fd78bfaSXin LI t = strchr(buf, '\0') - 1; 14603fd78bfaSXin LI while (--t >= buf && *t == '\\') 14613fd78bfaSXin LI continuation = !continuation; 14623fd78bfaSXin LI if (!continuation || 14633fd78bfaSXin LI pgets(true) == 0) 14643fd78bfaSXin LI break; 14653fd78bfaSXin LI if (pipefp != NULL) 14663fd78bfaSXin LI fputs(buf, pipefp); 14673fd78bfaSXin LI } 14683fd78bfaSXin LI } else if (*t != 'd') { 14692dd076b8SGabor Kovesdan while (pgets(true)) { 14702dd076b8SGabor Kovesdan p_input_line++; 14712dd076b8SGabor Kovesdan if (pipefp != NULL) 14722dd076b8SGabor Kovesdan fputs(buf, pipefp); 14732dd076b8SGabor Kovesdan if (strEQ(buf, ".\n")) 14742dd076b8SGabor Kovesdan break; 14752dd076b8SGabor Kovesdan } 14762dd076b8SGabor Kovesdan } 14772dd076b8SGabor Kovesdan } else { 14782dd076b8SGabor Kovesdan next_intuit_at(beginning_of_this_line, p_input_line); 14792dd076b8SGabor Kovesdan break; 14802dd076b8SGabor Kovesdan } 14812dd076b8SGabor Kovesdan } 14822dd076b8SGabor Kovesdan if (pipefp == NULL) 14832dd076b8SGabor Kovesdan return; 14842dd076b8SGabor Kovesdan fprintf(pipefp, "w\n"); 14852dd076b8SGabor Kovesdan fprintf(pipefp, "q\n"); 14862dd076b8SGabor Kovesdan fflush(pipefp); 14872dd076b8SGabor Kovesdan pclose(pipefp); 14882dd076b8SGabor Kovesdan ignore_signals(); 14892dd076b8SGabor Kovesdan if (!check_only) { 14902dd076b8SGabor Kovesdan if (move_file(TMPOUTNAME, outname) < 0) { 14912dd076b8SGabor Kovesdan toutkeep = true; 14922dd076b8SGabor Kovesdan chmod(TMPOUTNAME, filemode); 14932dd076b8SGabor Kovesdan } else 14942dd076b8SGabor Kovesdan chmod(outname, filemode); 14952dd076b8SGabor Kovesdan } 14962dd076b8SGabor Kovesdan set_signals(1); 14972dd076b8SGabor Kovesdan } 14982dd076b8SGabor Kovesdan 14992dd076b8SGabor Kovesdan /* 15002dd076b8SGabor Kovesdan * Choose the name of the file to be patched based on POSIX rules. 15012dd076b8SGabor Kovesdan * NOTE: the POSIX rules are amazingly stupid and we only follow them 15022dd076b8SGabor Kovesdan * if the user specified --posix or set POSIXLY_CORRECT. 15032dd076b8SGabor Kovesdan */ 15042dd076b8SGabor Kovesdan static char * 15052dd076b8SGabor Kovesdan posix_name(const struct file_name *names, bool assume_exists) 15062dd076b8SGabor Kovesdan { 15072dd076b8SGabor Kovesdan char *path = NULL; 15082dd076b8SGabor Kovesdan int i; 15092dd076b8SGabor Kovesdan 15102dd076b8SGabor Kovesdan /* 15112dd076b8SGabor Kovesdan * POSIX states that the filename will be chosen from one 15122dd076b8SGabor Kovesdan * of the old, new and index names (in that order) if 15132dd076b8SGabor Kovesdan * the file exists relative to CWD after -p stripping. 15142dd076b8SGabor Kovesdan */ 15152dd076b8SGabor Kovesdan for (i = 0; i < MAX_FILE; i++) { 15162dd076b8SGabor Kovesdan if (names[i].path != NULL && names[i].exists) { 15172dd076b8SGabor Kovesdan path = names[i].path; 15182dd076b8SGabor Kovesdan break; 15192dd076b8SGabor Kovesdan } 15202dd076b8SGabor Kovesdan } 15212dd076b8SGabor Kovesdan if (path == NULL && !assume_exists) { 15222dd076b8SGabor Kovesdan /* 1523e678759cSXin LI * No files found, check to see if the diff could be 1524e678759cSXin LI * creating a new file. 15252dd076b8SGabor Kovesdan */ 15262dd076b8SGabor Kovesdan if (path == NULL && ok_to_create_file && 15272dd076b8SGabor Kovesdan names[NEW_FILE].path != NULL) 15282dd076b8SGabor Kovesdan path = names[NEW_FILE].path; 15292dd076b8SGabor Kovesdan } 15302dd076b8SGabor Kovesdan 1531547e0acbSPedro F. Giffuni return path ? xstrdup(path) : NULL; 15322dd076b8SGabor Kovesdan } 15332dd076b8SGabor Kovesdan 15342dd076b8SGabor Kovesdan static char * 1535e678759cSXin LI compare_names(const struct file_name *names, bool assume_exists) 15362dd076b8SGabor Kovesdan { 15372dd076b8SGabor Kovesdan size_t min_components, min_baselen, min_len, tmp; 15382dd076b8SGabor Kovesdan char *best = NULL; 153979d8aaa9SStefan Eßer char *path; 15402dd076b8SGabor Kovesdan int i; 15412dd076b8SGabor Kovesdan 15422dd076b8SGabor Kovesdan /* 15432dd076b8SGabor Kovesdan * The "best" name is the one with the fewest number of path 15442dd076b8SGabor Kovesdan * components, the shortest basename length, and the shortest 15452dd076b8SGabor Kovesdan * overall length (in that order). We only use the Index: file 15462dd076b8SGabor Kovesdan * if neither of the old or new files could be intuited from 15472dd076b8SGabor Kovesdan * the diff header. 15482dd076b8SGabor Kovesdan */ 15492dd076b8SGabor Kovesdan min_components = min_baselen = min_len = SIZE_MAX; 15502dd076b8SGabor Kovesdan for (i = INDEX_FILE; i >= OLD_FILE; i--) { 155179d8aaa9SStefan Eßer path = names[i].path; 1552e678759cSXin LI if (path == NULL || (!names[i].exists && !assume_exists)) 15532dd076b8SGabor Kovesdan continue; 155479d8aaa9SStefan Eßer if ((tmp = num_components(path)) > min_components) 15552dd076b8SGabor Kovesdan continue; 15566b239879SStefan Eßer if (tmp < min_components) { 15572dd076b8SGabor Kovesdan min_components = tmp; 155879d8aaa9SStefan Eßer best = path; 15596b239879SStefan Eßer } 156079d8aaa9SStefan Eßer if ((tmp = strlen(basename(path))) > min_baselen) 15612dd076b8SGabor Kovesdan continue; 15626b239879SStefan Eßer if (tmp < min_baselen) { 15632dd076b8SGabor Kovesdan min_baselen = tmp; 156479d8aaa9SStefan Eßer best = path; 15656b239879SStefan Eßer } 156679d8aaa9SStefan Eßer if ((tmp = strlen(path)) > min_len) 15672dd076b8SGabor Kovesdan continue; 15682dd076b8SGabor Kovesdan min_len = tmp; 156979d8aaa9SStefan Eßer best = path; 15702dd076b8SGabor Kovesdan } 157179d8aaa9SStefan Eßer return best; 157279d8aaa9SStefan Eßer } 157379d8aaa9SStefan Eßer 15742dd076b8SGabor Kovesdan /* 157579d8aaa9SStefan Eßer * Choose the name of the file to be patched based the "best" one 157679d8aaa9SStefan Eßer * available. 15772dd076b8SGabor Kovesdan */ 157879d8aaa9SStefan Eßer static char * 157979d8aaa9SStefan Eßer best_name(const struct file_name *names, bool assume_exists) 158079d8aaa9SStefan Eßer { 158179d8aaa9SStefan Eßer char *best; 158279d8aaa9SStefan Eßer 1583e678759cSXin LI best = compare_names(names, assume_exists); 1584e678759cSXin LI 1585e678759cSXin LI /* No match? Check to see if the diff could be creating a new file. */ 1586e678759cSXin LI if (best == NULL && ok_to_create_file) 15872dd076b8SGabor Kovesdan best = names[NEW_FILE].path; 15882dd076b8SGabor Kovesdan 1589547e0acbSPedro F. Giffuni return best ? xstrdup(best) : NULL; 15902dd076b8SGabor Kovesdan } 15912dd076b8SGabor Kovesdan 15922dd076b8SGabor Kovesdan static size_t 15932dd076b8SGabor Kovesdan num_components(const char *path) 15942dd076b8SGabor Kovesdan { 15952dd076b8SGabor Kovesdan size_t n; 15962dd076b8SGabor Kovesdan const char *cp; 15972dd076b8SGabor Kovesdan 15982dd076b8SGabor Kovesdan for (n = 0, cp = path; (cp = strchr(cp, '/')) != NULL; n++, cp++) { 15992dd076b8SGabor Kovesdan while (*cp == '/') 16002dd076b8SGabor Kovesdan cp++; /* skip consecutive slashes */ 16012dd076b8SGabor Kovesdan } 16022dd076b8SGabor Kovesdan return n; 16032dd076b8SGabor Kovesdan } 1604d3fc0cb8SPedro F. Giffuni 1605d3fc0cb8SPedro F. Giffuni /* 1606d3fc0cb8SPedro F. Giffuni * Convert number at NPTR into LINENUM and save address of first 1607d3fc0cb8SPedro F. Giffuni * character that is not a digit in ENDPTR. If conversion is not 1608d3fc0cb8SPedro F. Giffuni * possible, call fatal. 1609d3fc0cb8SPedro F. Giffuni */ 1610d3fc0cb8SPedro F. Giffuni static LINENUM 1611d3fc0cb8SPedro F. Giffuni strtolinenum(char *nptr, char **endptr) 1612d3fc0cb8SPedro F. Giffuni { 1613d3fc0cb8SPedro F. Giffuni LINENUM rv; 1614d3fc0cb8SPedro F. Giffuni char c; 1615d3fc0cb8SPedro F. Giffuni char *p; 1616d3fc0cb8SPedro F. Giffuni const char *errstr; 1617d3fc0cb8SPedro F. Giffuni 1618d3fc0cb8SPedro F. Giffuni for (p = nptr; isdigit((unsigned char)*p); p++) 1619d3fc0cb8SPedro F. Giffuni ; 1620d3fc0cb8SPedro F. Giffuni 1621d3fc0cb8SPedro F. Giffuni if (p == nptr) 1622d3fc0cb8SPedro F. Giffuni malformed(); 1623d3fc0cb8SPedro F. Giffuni 1624d3fc0cb8SPedro F. Giffuni c = *p; 1625d3fc0cb8SPedro F. Giffuni *p = '\0'; 1626d3fc0cb8SPedro F. Giffuni 1627d3fc0cb8SPedro F. Giffuni rv = strtonum(nptr, 0, LINENUM_MAX, &errstr); 1628d3fc0cb8SPedro F. Giffuni if (errstr != NULL) 1629d3fc0cb8SPedro F. Giffuni fatal("invalid line number at line %ld: `%s' is %s\n", 1630d3fc0cb8SPedro F. Giffuni p_input_line, nptr, errstr); 1631d3fc0cb8SPedro F. Giffuni 1632d3fc0cb8SPedro F. Giffuni *p = c; 1633d3fc0cb8SPedro F. Giffuni *endptr = p; 1634d3fc0cb8SPedro F. Giffuni 1635d3fc0cb8SPedro F. Giffuni return rv; 1636d3fc0cb8SPedro F. Giffuni } 1637