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) { 219*12300d3aSPedro F. Giffuni int def_skip = *bestguess == '\0'; 220*12300d3aSPedro F. Giffuni ask("No file found--skip this patch? [%c] ", 221*12300d3aSPedro F. Giffuni def_skip ? 'y' : 'n'); 222*12300d3aSPedro 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]; 2672dd076b8SGabor Kovesdan 2682dd076b8SGabor Kovesdan memset(names, 0, sizeof(names)); 2692dd076b8SGabor Kovesdan ok_to_create_file = false; 270e91a64deSPedro F. Giffuni fseeko(pfp, p_base, SEEK_SET); 2712dd076b8SGabor Kovesdan p_input_line = p_bline - 1; 2722dd076b8SGabor Kovesdan for (;;) { 2732dd076b8SGabor Kovesdan previous_line = this_line; 2742dd076b8SGabor Kovesdan last_line_was_command = this_is_a_command; 2752dd076b8SGabor Kovesdan stars_last_line = stars_this_line; 276e91a64deSPedro F. Giffuni this_line = ftello(pfp); 2772dd076b8SGabor Kovesdan indent = 0; 2782dd076b8SGabor Kovesdan p_input_line++; 2792dd076b8SGabor Kovesdan if (pgets(false) == 0) { 280e91a64deSPedro F. Giffuni if (first_command_line >= 0) { 2812dd076b8SGabor Kovesdan /* nothing but deletes!? */ 2822dd076b8SGabor Kovesdan p_start = first_command_line; 2832dd076b8SGabor Kovesdan p_sline = fcl_line; 2842dd076b8SGabor Kovesdan retval = ED_DIFF; 2852dd076b8SGabor Kovesdan goto scan_exit; 2862dd076b8SGabor Kovesdan } else { 2872dd076b8SGabor Kovesdan p_start = this_line; 2882dd076b8SGabor Kovesdan p_sline = p_input_line; 2892dd076b8SGabor Kovesdan retval = 0; 2902dd076b8SGabor Kovesdan goto scan_exit; 2912dd076b8SGabor Kovesdan } 2922dd076b8SGabor Kovesdan } 2932dd076b8SGabor Kovesdan for (s = buf; *s == ' ' || *s == '\t' || *s == 'X'; s++) { 2942dd076b8SGabor Kovesdan if (*s == '\t') 2952dd076b8SGabor Kovesdan indent += 8 - (indent % 8); 2962dd076b8SGabor Kovesdan else 2972dd076b8SGabor Kovesdan indent++; 2982dd076b8SGabor Kovesdan } 2992dd076b8SGabor Kovesdan for (t = s; isdigit((unsigned char)*t) || *t == ','; t++) 3002dd076b8SGabor Kovesdan ; 3012dd076b8SGabor Kovesdan this_is_a_command = (isdigit((unsigned char)*s) && 3022dd076b8SGabor Kovesdan (*t == 'd' || *t == 'c' || *t == 'a')); 303e91a64deSPedro F. Giffuni if (first_command_line < 0 && this_is_a_command) { 3042dd076b8SGabor Kovesdan first_command_line = this_line; 3052dd076b8SGabor Kovesdan fcl_line = p_input_line; 3062dd076b8SGabor Kovesdan p_indent = indent; /* assume this for now */ 3072dd076b8SGabor Kovesdan } 3082dd076b8SGabor Kovesdan if (!stars_last_line && strnEQ(s, "*** ", 4)) 3092dd076b8SGabor Kovesdan names[OLD_FILE].path = fetchname(s + 4, 3102dd076b8SGabor Kovesdan &names[OLD_FILE].exists, strippath); 3112dd076b8SGabor Kovesdan else if (strnEQ(s, "--- ", 4)) 3122dd076b8SGabor Kovesdan names[NEW_FILE].path = fetchname(s + 4, 3132dd076b8SGabor Kovesdan &names[NEW_FILE].exists, strippath); 3142dd076b8SGabor Kovesdan else if (strnEQ(s, "+++ ", 4)) 3152dd076b8SGabor Kovesdan /* pretend it is the old name */ 3162dd076b8SGabor Kovesdan names[OLD_FILE].path = fetchname(s + 4, 3172dd076b8SGabor Kovesdan &names[OLD_FILE].exists, strippath); 3182dd076b8SGabor Kovesdan else if (strnEQ(s, "Index:", 6)) 3192dd076b8SGabor Kovesdan names[INDEX_FILE].path = fetchname(s + 6, 3202dd076b8SGabor Kovesdan &names[INDEX_FILE].exists, strippath); 3212dd076b8SGabor Kovesdan else if (strnEQ(s, "Prereq:", 7)) { 3222dd076b8SGabor Kovesdan for (t = s + 7; isspace((unsigned char)*t); t++) 3232dd076b8SGabor Kovesdan ; 324547e0acbSPedro F. Giffuni revision = xstrdup(t); 3252b468ebaSPedro F. Giffuni for (t = revision; 3262b468ebaSPedro F. Giffuni *t && !isspace((unsigned char)*t); t++) 3272dd076b8SGabor Kovesdan ; 3282dd076b8SGabor Kovesdan *t = '\0'; 3292dd076b8SGabor Kovesdan if (*revision == '\0') { 3302dd076b8SGabor Kovesdan free(revision); 3312dd076b8SGabor Kovesdan revision = NULL; 3322dd076b8SGabor Kovesdan } 3332dd076b8SGabor Kovesdan } else if (strnEQ(s, "==== ", 5)) { 3342dd076b8SGabor Kovesdan /* Perforce-style diffs. */ 3352dd076b8SGabor Kovesdan if ((t = strstr(s + 5, " - ")) != NULL) 3362dd076b8SGabor Kovesdan p4_fetchname(&names[NEW_FILE], t + 3); 3372dd076b8SGabor Kovesdan p4_fetchname(&names[OLD_FILE], s + 5); 3382dd076b8SGabor Kovesdan } 3392dd076b8SGabor Kovesdan if ((!diff_type || diff_type == ED_DIFF) && 340e91a64deSPedro F. Giffuni first_command_line >= 0 && 3412dd076b8SGabor Kovesdan strEQ(s, ".\n")) { 3422dd076b8SGabor Kovesdan p_indent = indent; 3432dd076b8SGabor Kovesdan p_start = first_command_line; 3442dd076b8SGabor Kovesdan p_sline = fcl_line; 3452dd076b8SGabor Kovesdan retval = ED_DIFF; 3462dd076b8SGabor Kovesdan goto scan_exit; 3472dd076b8SGabor Kovesdan } 3482dd076b8SGabor Kovesdan if ((!diff_type || diff_type == UNI_DIFF) && strnEQ(s, "@@ -", 4)) { 3492dd076b8SGabor Kovesdan if (strnEQ(s + 4, "0,0", 3)) 3502dd076b8SGabor Kovesdan ok_to_create_file = true; 3512dd076b8SGabor Kovesdan p_indent = indent; 3522dd076b8SGabor Kovesdan p_start = this_line; 3532dd076b8SGabor Kovesdan p_sline = p_input_line; 3542dd076b8SGabor Kovesdan retval = UNI_DIFF; 3552dd076b8SGabor Kovesdan goto scan_exit; 3562dd076b8SGabor Kovesdan } 3572dd076b8SGabor Kovesdan stars_this_line = strnEQ(s, "********", 8); 3582dd076b8SGabor Kovesdan if ((!diff_type || diff_type == CONTEXT_DIFF) && stars_last_line && 3592dd076b8SGabor Kovesdan strnEQ(s, "*** ", 4)) { 360d3fc0cb8SPedro F. Giffuni if (strtolinenum(s + 4, &s) == 0) 3612dd076b8SGabor Kovesdan ok_to_create_file = true; 3622dd076b8SGabor Kovesdan /* 3632dd076b8SGabor Kovesdan * If this is a new context diff the character just 364e91a64deSPedro F. Giffuni * at the end of the line is a '*'. 3652dd076b8SGabor Kovesdan */ 366e91a64deSPedro F. Giffuni while (*s && *s != '\n') 3672dd076b8SGabor Kovesdan s++; 3682dd076b8SGabor Kovesdan p_indent = indent; 3692dd076b8SGabor Kovesdan p_start = previous_line; 3702dd076b8SGabor Kovesdan p_sline = p_input_line - 1; 3712dd076b8SGabor Kovesdan retval = (*(s - 1) == '*' ? NEW_CONTEXT_DIFF : CONTEXT_DIFF); 3722dd076b8SGabor Kovesdan goto scan_exit; 3732dd076b8SGabor Kovesdan } 3742dd076b8SGabor Kovesdan if ((!diff_type || diff_type == NORMAL_DIFF) && 3752dd076b8SGabor Kovesdan last_line_was_command && 3762dd076b8SGabor Kovesdan (strnEQ(s, "< ", 2) || strnEQ(s, "> ", 2))) { 3772dd076b8SGabor Kovesdan p_start = previous_line; 3782dd076b8SGabor Kovesdan p_sline = p_input_line - 1; 3792dd076b8SGabor Kovesdan p_indent = indent; 3802dd076b8SGabor Kovesdan retval = NORMAL_DIFF; 3812dd076b8SGabor Kovesdan goto scan_exit; 3822dd076b8SGabor Kovesdan } 3832dd076b8SGabor Kovesdan } 3842dd076b8SGabor Kovesdan scan_exit: 3852dd076b8SGabor Kovesdan if (retval == UNI_DIFF) { 3862dd076b8SGabor Kovesdan /* unswap old and new */ 3872dd076b8SGabor Kovesdan struct file_name tmp = names[OLD_FILE]; 3882dd076b8SGabor Kovesdan names[OLD_FILE] = names[NEW_FILE]; 3892dd076b8SGabor Kovesdan names[NEW_FILE] = tmp; 3902dd076b8SGabor Kovesdan } 3912dd076b8SGabor Kovesdan if (filearg[0] == NULL) { 3922dd076b8SGabor Kovesdan if (posix) 3932dd076b8SGabor Kovesdan filearg[0] = posix_name(names, ok_to_create_file); 3942dd076b8SGabor Kovesdan else { 3952dd076b8SGabor Kovesdan /* Ignore the Index: name for context diffs, like GNU */ 3962dd076b8SGabor Kovesdan if (names[OLD_FILE].path != NULL || 3972dd076b8SGabor Kovesdan names[NEW_FILE].path != NULL) { 3982dd076b8SGabor Kovesdan free(names[INDEX_FILE].path); 3992dd076b8SGabor Kovesdan names[INDEX_FILE].path = NULL; 4002dd076b8SGabor Kovesdan } 4012dd076b8SGabor Kovesdan filearg[0] = best_name(names, ok_to_create_file); 4022dd076b8SGabor Kovesdan } 4032dd076b8SGabor Kovesdan } 4042dd076b8SGabor Kovesdan 4052dd076b8SGabor Kovesdan free(bestguess); 4062dd076b8SGabor Kovesdan bestguess = NULL; 4072dd076b8SGabor Kovesdan if (filearg[0] != NULL) 408547e0acbSPedro F. Giffuni bestguess = xstrdup(filearg[0]); 4092dd076b8SGabor Kovesdan else if (!ok_to_create_file) { 4102dd076b8SGabor Kovesdan /* 4112dd076b8SGabor Kovesdan * We don't want to create a new file but we need a 4122dd076b8SGabor Kovesdan * filename to set bestguess. Avoid setting filearg[0] 4132dd076b8SGabor Kovesdan * so the file is not created automatically. 4142dd076b8SGabor Kovesdan */ 4152dd076b8SGabor Kovesdan if (posix) 4162dd076b8SGabor Kovesdan bestguess = posix_name(names, true); 4172dd076b8SGabor Kovesdan else 4182dd076b8SGabor Kovesdan bestguess = best_name(names, true); 4192dd076b8SGabor Kovesdan } 4202dd076b8SGabor Kovesdan free(names[OLD_FILE].path); 4212dd076b8SGabor Kovesdan free(names[NEW_FILE].path); 4222dd076b8SGabor Kovesdan free(names[INDEX_FILE].path); 4232dd076b8SGabor Kovesdan return retval; 4242dd076b8SGabor Kovesdan } 4252dd076b8SGabor Kovesdan 4262dd076b8SGabor Kovesdan /* 4272dd076b8SGabor Kovesdan * Remember where this patch ends so we know where to start up again. 4282dd076b8SGabor Kovesdan */ 4292dd076b8SGabor Kovesdan static void 430e91a64deSPedro F. Giffuni next_intuit_at(off_t file_pos, LINENUM file_line) 4312dd076b8SGabor Kovesdan { 4322dd076b8SGabor Kovesdan p_base = file_pos; 4332dd076b8SGabor Kovesdan p_bline = file_line; 4342dd076b8SGabor Kovesdan } 4352dd076b8SGabor Kovesdan 4362dd076b8SGabor Kovesdan /* 437e91a64deSPedro F. Giffuni * Basically a verbose fseeko() to the actual diff listing. 4382dd076b8SGabor Kovesdan */ 4392dd076b8SGabor Kovesdan static void 440e91a64deSPedro F. Giffuni skip_to(off_t file_pos, LINENUM file_line) 4412dd076b8SGabor Kovesdan { 4422dd076b8SGabor Kovesdan size_t len; 4432dd076b8SGabor Kovesdan 4442dd076b8SGabor Kovesdan if (p_base > file_pos) 445e91a64deSPedro F. Giffuni fatal("Internal error: seek %lld>%lld\n", 446e91a64deSPedro F. Giffuni (long long)p_base, (long long)file_pos); 4472dd076b8SGabor Kovesdan if (verbose && p_base < file_pos) { 448e91a64deSPedro F. Giffuni fseeko(pfp, p_base, SEEK_SET); 4492dd076b8SGabor Kovesdan say("The text leading up to this was:\n--------------------------\n"); 450e91a64deSPedro F. Giffuni while (ftello(pfp) < file_pos) { 4512dd076b8SGabor Kovesdan len = pgets(false); 4522dd076b8SGabor Kovesdan if (len == 0) 4532dd076b8SGabor Kovesdan fatal("Unexpected end of file\n"); 4542dd076b8SGabor Kovesdan say("|%s", buf); 4552dd076b8SGabor Kovesdan } 4562dd076b8SGabor Kovesdan say("--------------------------\n"); 4572dd076b8SGabor Kovesdan } else 458e91a64deSPedro F. Giffuni fseeko(pfp, file_pos, SEEK_SET); 4592dd076b8SGabor Kovesdan p_input_line = file_line - 1; 4602dd076b8SGabor Kovesdan } 4612dd076b8SGabor Kovesdan 4622dd076b8SGabor Kovesdan /* Make this a function for better debugging. */ 4632dd076b8SGabor Kovesdan static void 4642dd076b8SGabor Kovesdan malformed(void) 4652dd076b8SGabor Kovesdan { 4662dd076b8SGabor Kovesdan fatal("malformed patch at line %ld: %s", p_input_line, buf); 4672dd076b8SGabor Kovesdan /* about as informative as "Syntax error" in C */ 4682dd076b8SGabor Kovesdan } 4692dd076b8SGabor Kovesdan 4702dd076b8SGabor Kovesdan /* 4712dd076b8SGabor Kovesdan * True if the line has been discarded (i.e. it is a line saying 4722dd076b8SGabor Kovesdan * "\ No newline at end of file".) 4732dd076b8SGabor Kovesdan */ 4742dd076b8SGabor Kovesdan static bool 4752dd076b8SGabor Kovesdan remove_special_line(void) 4762dd076b8SGabor Kovesdan { 4772dd076b8SGabor Kovesdan int c; 4782dd076b8SGabor Kovesdan 4792dd076b8SGabor Kovesdan c = fgetc(pfp); 4802dd076b8SGabor Kovesdan if (c == '\\') { 4812dd076b8SGabor Kovesdan do { 4822dd076b8SGabor Kovesdan c = fgetc(pfp); 4832dd076b8SGabor Kovesdan } while (c != EOF && c != '\n'); 4842dd076b8SGabor Kovesdan 4852dd076b8SGabor Kovesdan return true; 4862dd076b8SGabor Kovesdan } 4872dd076b8SGabor Kovesdan if (c != EOF) 488e91a64deSPedro F. Giffuni fseeko(pfp, -1, SEEK_CUR); 4892dd076b8SGabor Kovesdan 4902dd076b8SGabor Kovesdan return false; 4912dd076b8SGabor Kovesdan } 4922dd076b8SGabor Kovesdan 4932dd076b8SGabor Kovesdan /* 4942dd076b8SGabor Kovesdan * True if there is more of the current diff listing to process. 4952dd076b8SGabor Kovesdan */ 4962dd076b8SGabor Kovesdan bool 4972dd076b8SGabor Kovesdan another_hunk(void) 4982dd076b8SGabor Kovesdan { 499e91a64deSPedro F. Giffuni off_t line_beginning; /* file pos of the current line */ 5002dd076b8SGabor Kovesdan LINENUM repl_beginning; /* index of --- line */ 5012dd076b8SGabor Kovesdan LINENUM fillcnt; /* #lines of missing ptrn or repl */ 5022dd076b8SGabor Kovesdan LINENUM fillsrc; /* index of first line to copy */ 5032dd076b8SGabor Kovesdan LINENUM filldst; /* index of first missing line */ 504463a577bSEitan Adler bool ptrn_spaces_eaten; /* ptrn was slightly malformed */ 5052dd076b8SGabor Kovesdan bool repl_could_be_missing; /* no + or ! lines in this hunk */ 5062dd076b8SGabor Kovesdan bool repl_missing; /* we are now backtracking */ 507e91a64deSPedro F. Giffuni off_t repl_backtrack_position; /* file pos of first repl line */ 5082dd076b8SGabor Kovesdan LINENUM repl_patch_line; /* input line number for same */ 5092dd076b8SGabor Kovesdan LINENUM ptrn_copiable; /* # of copiable lines in ptrn */ 5102dd076b8SGabor Kovesdan char *s; 5112dd076b8SGabor Kovesdan size_t len; 5122dd076b8SGabor Kovesdan int context = 0; 5132dd076b8SGabor Kovesdan 5142dd076b8SGabor Kovesdan while (p_end >= 0) { 5152dd076b8SGabor Kovesdan if (p_end == p_efake) 5162dd076b8SGabor Kovesdan p_end = p_bfake; /* don't free twice */ 5172dd076b8SGabor Kovesdan else 5182dd076b8SGabor Kovesdan free(p_line[p_end]); 5192dd076b8SGabor Kovesdan p_end--; 5202dd076b8SGabor Kovesdan } 5212dd076b8SGabor Kovesdan p_efake = -1; 5222dd076b8SGabor Kovesdan 5232dd076b8SGabor Kovesdan p_max = hunkmax; /* gets reduced when --- found */ 5242dd076b8SGabor Kovesdan if (diff_type == CONTEXT_DIFF || diff_type == NEW_CONTEXT_DIFF) { 525e91a64deSPedro F. Giffuni line_beginning = ftello(pfp); 5262dd076b8SGabor Kovesdan repl_beginning = 0; 5272dd076b8SGabor Kovesdan fillcnt = 0; 5282dd076b8SGabor Kovesdan fillsrc = 0; 5292dd076b8SGabor Kovesdan filldst = 0; 5302dd076b8SGabor Kovesdan ptrn_spaces_eaten = false; 5312dd076b8SGabor Kovesdan repl_could_be_missing = true; 5322dd076b8SGabor Kovesdan repl_missing = false; 5332dd076b8SGabor Kovesdan repl_backtrack_position = 0; 5342dd076b8SGabor Kovesdan repl_patch_line = 0; 5352dd076b8SGabor Kovesdan ptrn_copiable = 0; 5362dd076b8SGabor Kovesdan 5372dd076b8SGabor Kovesdan len = pgets(true); 5382dd076b8SGabor Kovesdan p_input_line++; 5392dd076b8SGabor Kovesdan if (len == 0 || strnNE(buf, "********", 8)) { 5402dd076b8SGabor Kovesdan next_intuit_at(line_beginning, p_input_line); 5412dd076b8SGabor Kovesdan return false; 5422dd076b8SGabor Kovesdan } 5432dd076b8SGabor Kovesdan p_context = 100; 5442dd076b8SGabor Kovesdan p_hunk_beg = p_input_line + 1; 5452dd076b8SGabor Kovesdan while (p_end < p_max) { 546e91a64deSPedro F. Giffuni line_beginning = ftello(pfp); 5472dd076b8SGabor Kovesdan len = pgets(true); 5482dd076b8SGabor Kovesdan p_input_line++; 5492dd076b8SGabor Kovesdan if (len == 0) { 5502dd076b8SGabor Kovesdan if (p_max - p_end < 4) { 5512dd076b8SGabor Kovesdan /* assume blank lines got chopped */ 5522dd076b8SGabor Kovesdan strlcpy(buf, " \n", buf_size); 5532dd076b8SGabor Kovesdan } else { 5542dd076b8SGabor Kovesdan if (repl_beginning && repl_could_be_missing) { 5552dd076b8SGabor Kovesdan repl_missing = true; 5562dd076b8SGabor Kovesdan goto hunk_done; 5572dd076b8SGabor Kovesdan } 5582dd076b8SGabor Kovesdan fatal("unexpected end of file in patch\n"); 5592dd076b8SGabor Kovesdan } 5602dd076b8SGabor Kovesdan } 5612dd076b8SGabor Kovesdan p_end++; 5622dd076b8SGabor Kovesdan if (p_end >= hunkmax) 5632dd076b8SGabor Kovesdan fatal("Internal error: hunk larger than hunk " 5642dd076b8SGabor Kovesdan "buffer size"); 5652dd076b8SGabor Kovesdan p_char[p_end] = *buf; 5662dd076b8SGabor Kovesdan p_line[p_end] = NULL; 5672dd076b8SGabor Kovesdan switch (*buf) { 5682dd076b8SGabor Kovesdan case '*': 5692dd076b8SGabor Kovesdan if (strnEQ(buf, "********", 8)) { 5702dd076b8SGabor Kovesdan if (repl_beginning && repl_could_be_missing) { 5712dd076b8SGabor Kovesdan repl_missing = true; 5722dd076b8SGabor Kovesdan goto hunk_done; 5732dd076b8SGabor Kovesdan } else 5742dd076b8SGabor Kovesdan fatal("unexpected end of hunk " 5752dd076b8SGabor Kovesdan "at line %ld\n", 5762dd076b8SGabor Kovesdan p_input_line); 5772dd076b8SGabor Kovesdan } 5782dd076b8SGabor Kovesdan if (p_end != 0) { 5792dd076b8SGabor Kovesdan if (repl_beginning && repl_could_be_missing) { 5802dd076b8SGabor Kovesdan repl_missing = true; 5812dd076b8SGabor Kovesdan goto hunk_done; 5822dd076b8SGabor Kovesdan } 5832dd076b8SGabor Kovesdan fatal("unexpected *** at line %ld: %s", 5842dd076b8SGabor Kovesdan p_input_line, buf); 5852dd076b8SGabor Kovesdan } 5862dd076b8SGabor Kovesdan context = 0; 5872dd076b8SGabor Kovesdan p_line[p_end] = savestr(buf); 5882dd076b8SGabor Kovesdan if (out_of_mem) { 5892dd076b8SGabor Kovesdan p_end--; 5902dd076b8SGabor Kovesdan return false; 5912dd076b8SGabor Kovesdan } 5922b468ebaSPedro F. Giffuni for (s = buf; 5932b468ebaSPedro F. Giffuni *s && !isdigit((unsigned char)*s); s++) 5942dd076b8SGabor Kovesdan ; 5952dd076b8SGabor Kovesdan if (!*s) 5962dd076b8SGabor Kovesdan malformed(); 5972dd076b8SGabor Kovesdan if (strnEQ(s, "0,0", 3)) 5982dd076b8SGabor Kovesdan memmove(s, s + 2, strlen(s + 2) + 1); 599d3fc0cb8SPedro F. Giffuni p_first = strtolinenum(s, &s); 6002dd076b8SGabor Kovesdan if (*s == ',') { 6012b468ebaSPedro F. Giffuni for (; 6022b468ebaSPedro F. Giffuni *s && !isdigit((unsigned char)*s); s++) 6032dd076b8SGabor Kovesdan ; 6042dd076b8SGabor Kovesdan if (!*s) 6052dd076b8SGabor Kovesdan malformed(); 606d3fc0cb8SPedro F. Giffuni p_ptrn_lines = strtolinenum(s, &s) - p_first + 1; 607d3fc0cb8SPedro F. Giffuni if (p_ptrn_lines < 0) 608d3fc0cb8SPedro F. Giffuni malformed(); 6092dd076b8SGabor Kovesdan } else if (p_first) 6102dd076b8SGabor Kovesdan p_ptrn_lines = 1; 6112dd076b8SGabor Kovesdan else { 6122dd076b8SGabor Kovesdan p_ptrn_lines = 0; 6132dd076b8SGabor Kovesdan p_first = 1; 6142dd076b8SGabor Kovesdan } 615d3fc0cb8SPedro F. Giffuni if (p_first >= LINENUM_MAX - p_ptrn_lines || 616d3fc0cb8SPedro F. Giffuni p_ptrn_lines >= LINENUM_MAX - 6) 617d3fc0cb8SPedro F. Giffuni malformed(); 6182dd076b8SGabor Kovesdan 6192dd076b8SGabor Kovesdan /* we need this much at least */ 6202dd076b8SGabor Kovesdan p_max = p_ptrn_lines + 6; 6212dd076b8SGabor Kovesdan while (p_max >= hunkmax) 6222dd076b8SGabor Kovesdan grow_hunkmax(); 6232dd076b8SGabor Kovesdan p_max = hunkmax; 6242dd076b8SGabor Kovesdan break; 6252dd076b8SGabor Kovesdan case '-': 6262dd076b8SGabor Kovesdan if (buf[1] == '-') { 6272dd076b8SGabor Kovesdan if (repl_beginning || 6282dd076b8SGabor Kovesdan (p_end != p_ptrn_lines + 1 + 6292dd076b8SGabor Kovesdan (p_char[p_end - 1] == '\n'))) { 6302dd076b8SGabor Kovesdan if (p_end == 1) { 6312dd076b8SGabor Kovesdan /* 6322dd076b8SGabor Kovesdan * `old' lines were omitted; 6332dd076b8SGabor Kovesdan * set up to fill them in 6342dd076b8SGabor Kovesdan * from 'new' context lines. 6352dd076b8SGabor Kovesdan */ 6362dd076b8SGabor Kovesdan p_end = p_ptrn_lines + 1; 6372dd076b8SGabor Kovesdan fillsrc = p_end + 1; 6382dd076b8SGabor Kovesdan filldst = 1; 6392dd076b8SGabor Kovesdan fillcnt = p_ptrn_lines; 6402dd076b8SGabor Kovesdan } else { 6412dd076b8SGabor Kovesdan if (repl_beginning) { 6422dd076b8SGabor Kovesdan if (repl_could_be_missing) { 6432dd076b8SGabor Kovesdan repl_missing = true; 6442dd076b8SGabor Kovesdan goto hunk_done; 6452dd076b8SGabor Kovesdan } 6462dd076b8SGabor Kovesdan fatal("duplicate \"---\" at line %ld--check line numbers at line %ld\n", 6472dd076b8SGabor Kovesdan p_input_line, p_hunk_beg + repl_beginning); 6482dd076b8SGabor Kovesdan } else { 6492dd076b8SGabor Kovesdan fatal("%s \"---\" at line %ld--check line numbers at line %ld\n", 6502dd076b8SGabor Kovesdan (p_end <= p_ptrn_lines 6512dd076b8SGabor Kovesdan ? "Premature" 6522dd076b8SGabor Kovesdan : "Overdue"), 6532dd076b8SGabor Kovesdan p_input_line, p_hunk_beg); 6542dd076b8SGabor Kovesdan } 6552dd076b8SGabor Kovesdan } 6562dd076b8SGabor Kovesdan } 6572dd076b8SGabor Kovesdan repl_beginning = p_end; 658e91a64deSPedro F. Giffuni repl_backtrack_position = ftello(pfp); 6592dd076b8SGabor Kovesdan repl_patch_line = p_input_line; 6602dd076b8SGabor Kovesdan p_line[p_end] = savestr(buf); 6612dd076b8SGabor Kovesdan if (out_of_mem) { 6622dd076b8SGabor Kovesdan p_end--; 6632dd076b8SGabor Kovesdan return false; 6642dd076b8SGabor Kovesdan } 6652dd076b8SGabor Kovesdan p_char[p_end] = '='; 6662dd076b8SGabor Kovesdan for (s = buf; *s && !isdigit((unsigned char)*s); s++) 6672dd076b8SGabor Kovesdan ; 6682dd076b8SGabor Kovesdan if (!*s) 6692dd076b8SGabor Kovesdan malformed(); 670d3fc0cb8SPedro F. Giffuni p_newfirst = strtolinenum(s, &s); 6712dd076b8SGabor Kovesdan if (*s == ',') { 6722dd076b8SGabor Kovesdan for (; *s && !isdigit((unsigned char)*s); s++) 6732dd076b8SGabor Kovesdan ; 6742dd076b8SGabor Kovesdan if (!*s) 6752dd076b8SGabor Kovesdan malformed(); 676d3fc0cb8SPedro F. Giffuni p_repl_lines = strtolinenum(s, &s) - 6772dd076b8SGabor Kovesdan p_newfirst + 1; 678d3fc0cb8SPedro F. Giffuni if (p_repl_lines < 0) 679d3fc0cb8SPedro F. Giffuni malformed(); 6802dd076b8SGabor Kovesdan } else if (p_newfirst) 6812dd076b8SGabor Kovesdan p_repl_lines = 1; 6822dd076b8SGabor Kovesdan else { 6832dd076b8SGabor Kovesdan p_repl_lines = 0; 6842dd076b8SGabor Kovesdan p_newfirst = 1; 6852dd076b8SGabor Kovesdan } 686d3fc0cb8SPedro F. Giffuni if (p_newfirst >= LINENUM_MAX - p_repl_lines || 687d3fc0cb8SPedro F. Giffuni p_repl_lines >= LINENUM_MAX - p_end) 688d3fc0cb8SPedro F. Giffuni malformed(); 6892dd076b8SGabor Kovesdan p_max = p_repl_lines + p_end; 6902dd076b8SGabor Kovesdan if (p_max > MAXHUNKSIZE) 6912dd076b8SGabor Kovesdan fatal("hunk too large (%ld lines) at line %ld: %s", 6922dd076b8SGabor Kovesdan p_max, p_input_line, buf); 6932dd076b8SGabor Kovesdan while (p_max >= hunkmax) 6942dd076b8SGabor Kovesdan grow_hunkmax(); 6952dd076b8SGabor Kovesdan if (p_repl_lines != ptrn_copiable && 6962dd076b8SGabor Kovesdan (p_context != 0 || p_repl_lines != 1)) 6972dd076b8SGabor Kovesdan repl_could_be_missing = false; 6982dd076b8SGabor Kovesdan break; 6992dd076b8SGabor Kovesdan } 7002dd076b8SGabor Kovesdan goto change_line; 7012dd076b8SGabor Kovesdan case '+': 7022dd076b8SGabor Kovesdan case '!': 7032dd076b8SGabor Kovesdan repl_could_be_missing = false; 7042dd076b8SGabor Kovesdan change_line: 7052dd076b8SGabor Kovesdan if (buf[1] == '\n' && canonicalize) 7062dd076b8SGabor Kovesdan strlcpy(buf + 1, " \n", buf_size - 1); 7072b468ebaSPedro F. Giffuni if (!isspace((unsigned char)buf[1]) && 7082b468ebaSPedro F. Giffuni buf[1] != '>' && buf[1] != '<' && 7092dd076b8SGabor Kovesdan repl_beginning && repl_could_be_missing) { 7102dd076b8SGabor Kovesdan repl_missing = true; 7112dd076b8SGabor Kovesdan goto hunk_done; 7122dd076b8SGabor Kovesdan } 7132dd076b8SGabor Kovesdan if (context >= 0) { 7142dd076b8SGabor Kovesdan if (context < p_context) 7152dd076b8SGabor Kovesdan p_context = context; 7162dd076b8SGabor Kovesdan context = -1000; 7172dd076b8SGabor Kovesdan } 7182dd076b8SGabor Kovesdan p_line[p_end] = savestr(buf + 2); 7192dd076b8SGabor Kovesdan if (out_of_mem) { 7202dd076b8SGabor Kovesdan p_end--; 7212dd076b8SGabor Kovesdan return false; 7222dd076b8SGabor Kovesdan } 7232dd076b8SGabor Kovesdan if (p_end == p_ptrn_lines) { 7242dd076b8SGabor Kovesdan if (remove_special_line()) { 7252dd076b8SGabor Kovesdan int l; 7262dd076b8SGabor Kovesdan 7272dd076b8SGabor Kovesdan l = strlen(p_line[p_end]) - 1; 7282dd076b8SGabor Kovesdan (p_line[p_end])[l] = 0; 7292dd076b8SGabor Kovesdan } 7302dd076b8SGabor Kovesdan } 7312dd076b8SGabor Kovesdan break; 7322dd076b8SGabor Kovesdan case '\t': 7332dd076b8SGabor Kovesdan case '\n': /* assume the 2 spaces got eaten */ 7342dd076b8SGabor Kovesdan if (repl_beginning && repl_could_be_missing && 7352dd076b8SGabor Kovesdan (!ptrn_spaces_eaten || 7362dd076b8SGabor Kovesdan diff_type == NEW_CONTEXT_DIFF)) { 7372dd076b8SGabor Kovesdan repl_missing = true; 7382dd076b8SGabor Kovesdan goto hunk_done; 7392dd076b8SGabor Kovesdan } 7402dd076b8SGabor Kovesdan p_line[p_end] = savestr(buf); 7412dd076b8SGabor Kovesdan if (out_of_mem) { 7422dd076b8SGabor Kovesdan p_end--; 7432dd076b8SGabor Kovesdan return false; 7442dd076b8SGabor Kovesdan } 7452dd076b8SGabor Kovesdan if (p_end != p_ptrn_lines + 1) { 7462dd076b8SGabor Kovesdan ptrn_spaces_eaten |= (repl_beginning != 0); 7472dd076b8SGabor Kovesdan context++; 7482dd076b8SGabor Kovesdan if (!repl_beginning) 7492dd076b8SGabor Kovesdan ptrn_copiable++; 7502dd076b8SGabor Kovesdan p_char[p_end] = ' '; 7512dd076b8SGabor Kovesdan } 7522dd076b8SGabor Kovesdan break; 7532dd076b8SGabor Kovesdan case ' ': 7542dd076b8SGabor Kovesdan if (!isspace((unsigned char)buf[1]) && 7552dd076b8SGabor Kovesdan repl_beginning && repl_could_be_missing) { 7562dd076b8SGabor Kovesdan repl_missing = true; 7572dd076b8SGabor Kovesdan goto hunk_done; 7582dd076b8SGabor Kovesdan } 7592dd076b8SGabor Kovesdan context++; 7602dd076b8SGabor Kovesdan if (!repl_beginning) 7612dd076b8SGabor Kovesdan ptrn_copiable++; 7622dd076b8SGabor Kovesdan p_line[p_end] = savestr(buf + 2); 7632dd076b8SGabor Kovesdan if (out_of_mem) { 7642dd076b8SGabor Kovesdan p_end--; 7652dd076b8SGabor Kovesdan return false; 7662dd076b8SGabor Kovesdan } 7672dd076b8SGabor Kovesdan break; 7682dd076b8SGabor Kovesdan default: 7692dd076b8SGabor Kovesdan if (repl_beginning && repl_could_be_missing) { 7702dd076b8SGabor Kovesdan repl_missing = true; 7712dd076b8SGabor Kovesdan goto hunk_done; 7722dd076b8SGabor Kovesdan } 7732dd076b8SGabor Kovesdan malformed(); 7742dd076b8SGabor Kovesdan } 7752dd076b8SGabor Kovesdan /* set up p_len for strncmp() so we don't have to */ 7762dd076b8SGabor Kovesdan /* assume null termination */ 7772dd076b8SGabor Kovesdan if (p_line[p_end]) 7782dd076b8SGabor Kovesdan p_len[p_end] = strlen(p_line[p_end]); 7792dd076b8SGabor Kovesdan else 7802dd076b8SGabor Kovesdan p_len[p_end] = 0; 7812dd076b8SGabor Kovesdan } 7822dd076b8SGabor Kovesdan 7832dd076b8SGabor Kovesdan hunk_done: 7842dd076b8SGabor Kovesdan if (p_end >= 0 && !repl_beginning) 7852dd076b8SGabor Kovesdan fatal("no --- found in patch at line %ld\n", pch_hunk_beg()); 7862dd076b8SGabor Kovesdan 7872dd076b8SGabor Kovesdan if (repl_missing) { 7882dd076b8SGabor Kovesdan 7892dd076b8SGabor Kovesdan /* reset state back to just after --- */ 7902dd076b8SGabor Kovesdan p_input_line = repl_patch_line; 7912dd076b8SGabor Kovesdan for (p_end--; p_end > repl_beginning; p_end--) 7922dd076b8SGabor Kovesdan free(p_line[p_end]); 793e91a64deSPedro F. Giffuni fseeko(pfp, repl_backtrack_position, SEEK_SET); 7942dd076b8SGabor Kovesdan 7952dd076b8SGabor Kovesdan /* redundant 'new' context lines were omitted - set */ 7962dd076b8SGabor Kovesdan /* up to fill them in from the old file context */ 7972dd076b8SGabor Kovesdan if (!p_context && p_repl_lines == 1) { 7982dd076b8SGabor Kovesdan p_repl_lines = 0; 7992dd076b8SGabor Kovesdan p_max--; 8002dd076b8SGabor Kovesdan } 8012dd076b8SGabor Kovesdan fillsrc = 1; 8022dd076b8SGabor Kovesdan filldst = repl_beginning + 1; 8032dd076b8SGabor Kovesdan fillcnt = p_repl_lines; 8042dd076b8SGabor Kovesdan p_end = p_max; 8052dd076b8SGabor Kovesdan } else if (!p_context && fillcnt == 1) { 8062dd076b8SGabor Kovesdan /* the first hunk was a null hunk with no context */ 8072dd076b8SGabor Kovesdan /* and we were expecting one line -- fix it up. */ 8082dd076b8SGabor Kovesdan while (filldst < p_end) { 8092dd076b8SGabor Kovesdan p_line[filldst] = p_line[filldst + 1]; 8102dd076b8SGabor Kovesdan p_char[filldst] = p_char[filldst + 1]; 8112dd076b8SGabor Kovesdan p_len[filldst] = p_len[filldst + 1]; 8122dd076b8SGabor Kovesdan filldst++; 8132dd076b8SGabor Kovesdan } 8142dd076b8SGabor Kovesdan #if 0 8152dd076b8SGabor Kovesdan repl_beginning--; /* this doesn't need to be fixed */ 8162dd076b8SGabor Kovesdan #endif 8172dd076b8SGabor Kovesdan p_end--; 8182dd076b8SGabor Kovesdan p_first++; /* do append rather than insert */ 8192dd076b8SGabor Kovesdan fillcnt = 0; 8202dd076b8SGabor Kovesdan p_ptrn_lines = 0; 8212dd076b8SGabor Kovesdan } 8222dd076b8SGabor Kovesdan if (diff_type == CONTEXT_DIFF && 8232dd076b8SGabor Kovesdan (fillcnt || (p_first > 1 && ptrn_copiable > 2 * p_context))) { 8242dd076b8SGabor Kovesdan if (verbose) 8252dd076b8SGabor Kovesdan say("%s\n%s\n%s\n", 8262dd076b8SGabor Kovesdan "(Fascinating--this is really a new-style context diff but without", 8272dd076b8SGabor Kovesdan "the telltale extra asterisks on the *** line that usually indicate", 8282dd076b8SGabor Kovesdan "the new style...)"); 8292dd076b8SGabor Kovesdan diff_type = NEW_CONTEXT_DIFF; 8302dd076b8SGabor Kovesdan } 8312dd076b8SGabor Kovesdan /* if there were omitted context lines, fill them in now */ 8322dd076b8SGabor Kovesdan if (fillcnt) { 8332dd076b8SGabor Kovesdan p_bfake = filldst; /* remember where not to free() */ 8342dd076b8SGabor Kovesdan p_efake = filldst + fillcnt - 1; 8352dd076b8SGabor Kovesdan while (fillcnt-- > 0) { 8362dd076b8SGabor Kovesdan while (fillsrc <= p_end && p_char[fillsrc] != ' ') 8372dd076b8SGabor Kovesdan fillsrc++; 8382dd076b8SGabor Kovesdan if (fillsrc > p_end) 8392dd076b8SGabor Kovesdan fatal("replacement text or line numbers mangled in hunk at line %ld\n", 8402dd076b8SGabor Kovesdan p_hunk_beg); 8412dd076b8SGabor Kovesdan p_line[filldst] = p_line[fillsrc]; 8422dd076b8SGabor Kovesdan p_char[filldst] = p_char[fillsrc]; 8432dd076b8SGabor Kovesdan p_len[filldst] = p_len[fillsrc]; 8442dd076b8SGabor Kovesdan fillsrc++; 8452dd076b8SGabor Kovesdan filldst++; 8462dd076b8SGabor Kovesdan } 8472dd076b8SGabor Kovesdan while (fillsrc <= p_end && fillsrc != repl_beginning && 8482dd076b8SGabor Kovesdan p_char[fillsrc] != ' ') 8492dd076b8SGabor Kovesdan fillsrc++; 8502dd076b8SGabor Kovesdan #ifdef DEBUGGING 8512dd076b8SGabor Kovesdan if (debug & 64) 8522dd076b8SGabor Kovesdan printf("fillsrc %ld, filldst %ld, rb %ld, e+1 %ld\n", 8532dd076b8SGabor Kovesdan fillsrc, filldst, repl_beginning, p_end + 1); 8542dd076b8SGabor Kovesdan #endif 8552dd076b8SGabor Kovesdan if (fillsrc != p_end + 1 && fillsrc != repl_beginning) 8562dd076b8SGabor Kovesdan malformed(); 8572dd076b8SGabor Kovesdan if (filldst != p_end + 1 && filldst != repl_beginning) 8582dd076b8SGabor Kovesdan malformed(); 8592dd076b8SGabor Kovesdan } 8602dd076b8SGabor Kovesdan if (p_line[p_end] != NULL) { 8612dd076b8SGabor Kovesdan if (remove_special_line()) { 8622dd076b8SGabor Kovesdan p_len[p_end] -= 1; 8632dd076b8SGabor Kovesdan (p_line[p_end])[p_len[p_end]] = 0; 8642dd076b8SGabor Kovesdan } 8652dd076b8SGabor Kovesdan } 8662dd076b8SGabor Kovesdan } else if (diff_type == UNI_DIFF) { 8672dd076b8SGabor Kovesdan LINENUM fillold; /* index of old lines */ 8682dd076b8SGabor Kovesdan LINENUM fillnew; /* index of new lines */ 8692dd076b8SGabor Kovesdan char ch; 8702dd076b8SGabor Kovesdan 871e91a64deSPedro F. Giffuni line_beginning = ftello(pfp); /* file pos of the current line */ 8722dd076b8SGabor Kovesdan len = pgets(true); 8732dd076b8SGabor Kovesdan p_input_line++; 8742dd076b8SGabor Kovesdan if (len == 0 || strnNE(buf, "@@ -", 4)) { 8752dd076b8SGabor Kovesdan next_intuit_at(line_beginning, p_input_line); 8762dd076b8SGabor Kovesdan return false; 8772dd076b8SGabor Kovesdan } 8782dd076b8SGabor Kovesdan s = buf + 4; 8792dd076b8SGabor Kovesdan if (!*s) 8802dd076b8SGabor Kovesdan malformed(); 881d3fc0cb8SPedro F. Giffuni p_first = strtolinenum(s, &s); 8822dd076b8SGabor Kovesdan if (*s == ',') { 883d3fc0cb8SPedro F. Giffuni p_ptrn_lines = strtolinenum(s + 1, &s); 8842dd076b8SGabor Kovesdan } else 8852dd076b8SGabor Kovesdan p_ptrn_lines = 1; 8862dd076b8SGabor Kovesdan if (*s == ' ') 8872dd076b8SGabor Kovesdan s++; 8882dd076b8SGabor Kovesdan if (*s != '+' || !*++s) 8892dd076b8SGabor Kovesdan malformed(); 890d3fc0cb8SPedro F. Giffuni p_newfirst = strtolinenum(s, &s); 8912dd076b8SGabor Kovesdan if (*s == ',') { 892d3fc0cb8SPedro F. Giffuni p_repl_lines = strtolinenum(s + 1, &s); 8932dd076b8SGabor Kovesdan } else 8942dd076b8SGabor Kovesdan p_repl_lines = 1; 8952dd076b8SGabor Kovesdan if (*s == ' ') 8962dd076b8SGabor Kovesdan s++; 8972dd076b8SGabor Kovesdan if (*s != '@') 8982dd076b8SGabor Kovesdan malformed(); 899d3fc0cb8SPedro F. Giffuni if (p_first >= LINENUM_MAX - p_ptrn_lines || 900d3fc0cb8SPedro F. Giffuni p_newfirst > LINENUM_MAX - p_repl_lines || 901d3fc0cb8SPedro F. Giffuni p_ptrn_lines >= LINENUM_MAX - p_repl_lines - 1) 902d3fc0cb8SPedro F. Giffuni malformed(); 9032dd076b8SGabor Kovesdan if (!p_ptrn_lines) 9042dd076b8SGabor Kovesdan p_first++; /* do append rather than insert */ 9052dd076b8SGabor Kovesdan p_max = p_ptrn_lines + p_repl_lines + 1; 9062dd076b8SGabor Kovesdan while (p_max >= hunkmax) 9072dd076b8SGabor Kovesdan grow_hunkmax(); 9082dd076b8SGabor Kovesdan fillold = 1; 9092dd076b8SGabor Kovesdan fillnew = fillold + p_ptrn_lines; 9102dd076b8SGabor Kovesdan p_end = fillnew + p_repl_lines; 9112dd076b8SGabor Kovesdan snprintf(buf, buf_size, "*** %ld,%ld ****\n", p_first, 9122dd076b8SGabor Kovesdan p_first + p_ptrn_lines - 1); 9132dd076b8SGabor Kovesdan p_line[0] = savestr(buf); 9142dd076b8SGabor Kovesdan if (out_of_mem) { 9152dd076b8SGabor Kovesdan p_end = -1; 9162dd076b8SGabor Kovesdan return false; 9172dd076b8SGabor Kovesdan } 9182dd076b8SGabor Kovesdan p_char[0] = '*'; 9192dd076b8SGabor Kovesdan snprintf(buf, buf_size, "--- %ld,%ld ----\n", p_newfirst, 9202dd076b8SGabor Kovesdan p_newfirst + p_repl_lines - 1); 9212dd076b8SGabor Kovesdan p_line[fillnew] = savestr(buf); 9222dd076b8SGabor Kovesdan if (out_of_mem) { 9232dd076b8SGabor Kovesdan p_end = 0; 9242dd076b8SGabor Kovesdan return false; 9252dd076b8SGabor Kovesdan } 9262dd076b8SGabor Kovesdan p_char[fillnew++] = '='; 9272dd076b8SGabor Kovesdan p_context = 100; 9282dd076b8SGabor Kovesdan context = 0; 9292dd076b8SGabor Kovesdan p_hunk_beg = p_input_line + 1; 9302dd076b8SGabor Kovesdan while (fillold <= p_ptrn_lines || fillnew <= p_end) { 931e91a64deSPedro F. Giffuni line_beginning = ftello(pfp); 9322dd076b8SGabor Kovesdan len = pgets(true); 9332dd076b8SGabor Kovesdan p_input_line++; 9342dd076b8SGabor Kovesdan if (len == 0) { 9352dd076b8SGabor Kovesdan if (p_max - fillnew < 3) { 9362dd076b8SGabor Kovesdan /* assume blank lines got chopped */ 9372dd076b8SGabor Kovesdan strlcpy(buf, " \n", buf_size); 9382dd076b8SGabor Kovesdan } else { 9392dd076b8SGabor Kovesdan fatal("unexpected end of file in patch\n"); 9402dd076b8SGabor Kovesdan } 9412dd076b8SGabor Kovesdan } 9422dd076b8SGabor Kovesdan if (*buf == '\t' || *buf == '\n') { 9432dd076b8SGabor Kovesdan ch = ' '; /* assume the space got eaten */ 9442dd076b8SGabor Kovesdan s = savestr(buf); 9452dd076b8SGabor Kovesdan } else { 9462dd076b8SGabor Kovesdan ch = *buf; 9472dd076b8SGabor Kovesdan s = savestr(buf + 1); 9482dd076b8SGabor Kovesdan } 9492dd076b8SGabor Kovesdan if (out_of_mem) { 9502dd076b8SGabor Kovesdan while (--fillnew > p_ptrn_lines) 9512dd076b8SGabor Kovesdan free(p_line[fillnew]); 9522dd076b8SGabor Kovesdan p_end = fillold - 1; 9532dd076b8SGabor Kovesdan return false; 9542dd076b8SGabor Kovesdan } 9552dd076b8SGabor Kovesdan switch (ch) { 9562dd076b8SGabor Kovesdan case '-': 9572dd076b8SGabor Kovesdan if (fillold > p_ptrn_lines) { 9582dd076b8SGabor Kovesdan free(s); 9592dd076b8SGabor Kovesdan p_end = fillnew - 1; 9602dd076b8SGabor Kovesdan malformed(); 9612dd076b8SGabor Kovesdan } 9622dd076b8SGabor Kovesdan p_char[fillold] = ch; 9632dd076b8SGabor Kovesdan p_line[fillold] = s; 9642dd076b8SGabor Kovesdan p_len[fillold++] = strlen(s); 9652dd076b8SGabor Kovesdan if (fillold > p_ptrn_lines) { 9662dd076b8SGabor Kovesdan if (remove_special_line()) { 9672dd076b8SGabor Kovesdan p_len[fillold - 1] -= 1; 9682dd076b8SGabor Kovesdan s[p_len[fillold - 1]] = 0; 9692dd076b8SGabor Kovesdan } 9702dd076b8SGabor Kovesdan } 9712dd076b8SGabor Kovesdan break; 9722dd076b8SGabor Kovesdan case '=': 9732dd076b8SGabor Kovesdan ch = ' '; 9742dd076b8SGabor Kovesdan /* FALL THROUGH */ 9752dd076b8SGabor Kovesdan case ' ': 9762dd076b8SGabor Kovesdan if (fillold > p_ptrn_lines) { 9772dd076b8SGabor Kovesdan free(s); 9782dd076b8SGabor Kovesdan while (--fillnew > p_ptrn_lines) 9792dd076b8SGabor Kovesdan free(p_line[fillnew]); 9802dd076b8SGabor Kovesdan p_end = fillold - 1; 9812dd076b8SGabor Kovesdan malformed(); 9822dd076b8SGabor Kovesdan } 9832dd076b8SGabor Kovesdan context++; 9842dd076b8SGabor Kovesdan p_char[fillold] = ch; 9852dd076b8SGabor Kovesdan p_line[fillold] = s; 9862dd076b8SGabor Kovesdan p_len[fillold++] = strlen(s); 9872dd076b8SGabor Kovesdan s = savestr(s); 9882dd076b8SGabor Kovesdan if (out_of_mem) { 9892dd076b8SGabor Kovesdan while (--fillnew > p_ptrn_lines) 9902dd076b8SGabor Kovesdan free(p_line[fillnew]); 9912dd076b8SGabor Kovesdan p_end = fillold - 1; 9922dd076b8SGabor Kovesdan return false; 9932dd076b8SGabor Kovesdan } 9942dd076b8SGabor Kovesdan if (fillold > p_ptrn_lines) { 9952dd076b8SGabor Kovesdan if (remove_special_line()) { 9962dd076b8SGabor Kovesdan p_len[fillold - 1] -= 1; 9972dd076b8SGabor Kovesdan s[p_len[fillold - 1]] = 0; 9982dd076b8SGabor Kovesdan } 9992dd076b8SGabor Kovesdan } 10002dd076b8SGabor Kovesdan /* FALL THROUGH */ 10012dd076b8SGabor Kovesdan case '+': 10022dd076b8SGabor Kovesdan if (fillnew > p_end) { 10032dd076b8SGabor Kovesdan free(s); 10042dd076b8SGabor Kovesdan while (--fillnew > p_ptrn_lines) 10052dd076b8SGabor Kovesdan free(p_line[fillnew]); 10062dd076b8SGabor Kovesdan p_end = fillold - 1; 10072dd076b8SGabor Kovesdan malformed(); 10082dd076b8SGabor Kovesdan } 10092dd076b8SGabor Kovesdan p_char[fillnew] = ch; 10102dd076b8SGabor Kovesdan p_line[fillnew] = s; 10112dd076b8SGabor Kovesdan p_len[fillnew++] = strlen(s); 10122dd076b8SGabor Kovesdan if (fillold > p_ptrn_lines) { 10132dd076b8SGabor Kovesdan if (remove_special_line()) { 10142dd076b8SGabor Kovesdan p_len[fillnew - 1] -= 1; 10152dd076b8SGabor Kovesdan s[p_len[fillnew - 1]] = 0; 10162dd076b8SGabor Kovesdan } 10172dd076b8SGabor Kovesdan } 10182dd076b8SGabor Kovesdan break; 10192dd076b8SGabor Kovesdan default: 10202dd076b8SGabor Kovesdan p_end = fillnew; 10212dd076b8SGabor Kovesdan malformed(); 10222dd076b8SGabor Kovesdan } 10232dd076b8SGabor Kovesdan if (ch != ' ' && context > 0) { 10242dd076b8SGabor Kovesdan if (context < p_context) 10252dd076b8SGabor Kovesdan p_context = context; 10262dd076b8SGabor Kovesdan context = -1000; 10272dd076b8SGabor Kovesdan } 10282dd076b8SGabor Kovesdan } /* while */ 10292dd076b8SGabor Kovesdan } else { /* normal diff--fake it up */ 10302dd076b8SGabor Kovesdan char hunk_type; 10312dd076b8SGabor Kovesdan int i; 10322dd076b8SGabor Kovesdan LINENUM min, max; 10332dd076b8SGabor Kovesdan 1034e91a64deSPedro F. Giffuni line_beginning = ftello(pfp); 10352dd076b8SGabor Kovesdan p_context = 0; 10362dd076b8SGabor Kovesdan len = pgets(true); 10372dd076b8SGabor Kovesdan p_input_line++; 10382dd076b8SGabor Kovesdan if (len == 0 || !isdigit((unsigned char)*buf)) { 10392dd076b8SGabor Kovesdan next_intuit_at(line_beginning, p_input_line); 10402dd076b8SGabor Kovesdan return false; 10412dd076b8SGabor Kovesdan } 1042d3fc0cb8SPedro F. Giffuni p_first = strtolinenum(buf, &s); 10432dd076b8SGabor Kovesdan if (*s == ',') { 1044d3fc0cb8SPedro F. Giffuni p_ptrn_lines = strtolinenum(s + 1, &s) - p_first + 1; 1045d3fc0cb8SPedro F. Giffuni if (p_ptrn_lines < 0) 1046d3fc0cb8SPedro F. Giffuni malformed(); 10472dd076b8SGabor Kovesdan } else 10482dd076b8SGabor Kovesdan p_ptrn_lines = (*s != 'a'); 10492dd076b8SGabor Kovesdan hunk_type = *s; 10502dd076b8SGabor Kovesdan if (hunk_type == 'a') 10512dd076b8SGabor Kovesdan p_first++; /* do append rather than insert */ 1052d3fc0cb8SPedro F. Giffuni min = strtolinenum(s + 1, &s); 10532dd076b8SGabor Kovesdan if (*s == ',') 1054d3fc0cb8SPedro F. Giffuni max = strtolinenum(s + 1, &s); 10552dd076b8SGabor Kovesdan else 10562dd076b8SGabor Kovesdan max = min; 1057d3fc0cb8SPedro F. Giffuni if (min < 0 || min > max || max - min == LINENUM_MAX) 1058d3fc0cb8SPedro F. Giffuni malformed(); 10592dd076b8SGabor Kovesdan if (hunk_type == 'd') 10602dd076b8SGabor Kovesdan min++; 1061d3fc0cb8SPedro F. Giffuni p_newfirst = min; 1062d3fc0cb8SPedro F. Giffuni p_repl_lines = max - min + 1; 1063d3fc0cb8SPedro F. Giffuni if (p_newfirst > LINENUM_MAX - p_repl_lines || 1064d3fc0cb8SPedro F. Giffuni p_ptrn_lines >= LINENUM_MAX - p_repl_lines - 1) 1065d3fc0cb8SPedro F. Giffuni malformed(); 1066d3fc0cb8SPedro F. Giffuni p_end = p_ptrn_lines + p_repl_lines + 1; 10672dd076b8SGabor Kovesdan if (p_end > MAXHUNKSIZE) 10682dd076b8SGabor Kovesdan fatal("hunk too large (%ld lines) at line %ld: %s", 10692dd076b8SGabor Kovesdan p_end, p_input_line, buf); 10702dd076b8SGabor Kovesdan while (p_end >= hunkmax) 10712dd076b8SGabor Kovesdan grow_hunkmax(); 10722dd076b8SGabor Kovesdan snprintf(buf, buf_size, "*** %ld,%ld\n", p_first, 10732dd076b8SGabor Kovesdan p_first + p_ptrn_lines - 1); 10742dd076b8SGabor Kovesdan p_line[0] = savestr(buf); 10752dd076b8SGabor Kovesdan if (out_of_mem) { 10762dd076b8SGabor Kovesdan p_end = -1; 10772dd076b8SGabor Kovesdan return false; 10782dd076b8SGabor Kovesdan } 10792dd076b8SGabor Kovesdan p_char[0] = '*'; 10802dd076b8SGabor Kovesdan for (i = 1; i <= p_ptrn_lines; i++) { 10812dd076b8SGabor Kovesdan len = pgets(true); 10822dd076b8SGabor Kovesdan p_input_line++; 10832dd076b8SGabor Kovesdan if (len == 0) 10842dd076b8SGabor Kovesdan fatal("unexpected end of file in patch at line %ld\n", 10852dd076b8SGabor Kovesdan p_input_line); 10862dd076b8SGabor Kovesdan if (*buf != '<') 10872dd076b8SGabor Kovesdan fatal("< expected at line %ld of patch\n", 10882dd076b8SGabor Kovesdan p_input_line); 10892dd076b8SGabor Kovesdan p_line[i] = savestr(buf + 2); 10902dd076b8SGabor Kovesdan if (out_of_mem) { 10912dd076b8SGabor Kovesdan p_end = i - 1; 10922dd076b8SGabor Kovesdan return false; 10932dd076b8SGabor Kovesdan } 10942dd076b8SGabor Kovesdan p_len[i] = strlen(p_line[i]); 10952dd076b8SGabor Kovesdan p_char[i] = '-'; 10962dd076b8SGabor Kovesdan } 10972dd076b8SGabor Kovesdan 10982dd076b8SGabor Kovesdan if (remove_special_line()) { 10992dd076b8SGabor Kovesdan p_len[i - 1] -= 1; 11002dd076b8SGabor Kovesdan (p_line[i - 1])[p_len[i - 1]] = 0; 11012dd076b8SGabor Kovesdan } 11022dd076b8SGabor Kovesdan if (hunk_type == 'c') { 11032dd076b8SGabor Kovesdan len = pgets(true); 11042dd076b8SGabor Kovesdan p_input_line++; 11052dd076b8SGabor Kovesdan if (len == 0) 11062dd076b8SGabor Kovesdan fatal("unexpected end of file in patch at line %ld\n", 11072dd076b8SGabor Kovesdan p_input_line); 11082dd076b8SGabor Kovesdan if (*buf != '-') 11092dd076b8SGabor Kovesdan fatal("--- expected at line %ld of patch\n", 11102dd076b8SGabor Kovesdan p_input_line); 11112dd076b8SGabor Kovesdan } 11122dd076b8SGabor Kovesdan snprintf(buf, buf_size, "--- %ld,%ld\n", min, max); 11132dd076b8SGabor Kovesdan p_line[i] = savestr(buf); 11142dd076b8SGabor Kovesdan if (out_of_mem) { 11152dd076b8SGabor Kovesdan p_end = i - 1; 11162dd076b8SGabor Kovesdan return false; 11172dd076b8SGabor Kovesdan } 11182dd076b8SGabor Kovesdan p_char[i] = '='; 11192dd076b8SGabor Kovesdan for (i++; i <= p_end; i++) { 11202dd076b8SGabor Kovesdan len = pgets(true); 11212dd076b8SGabor Kovesdan p_input_line++; 11222dd076b8SGabor Kovesdan if (len == 0) 11232dd076b8SGabor Kovesdan fatal("unexpected end of file in patch at line %ld\n", 11242dd076b8SGabor Kovesdan p_input_line); 11252dd076b8SGabor Kovesdan if (*buf != '>') 11262dd076b8SGabor Kovesdan fatal("> expected at line %ld of patch\n", 11272dd076b8SGabor Kovesdan p_input_line); 11282dd076b8SGabor Kovesdan p_line[i] = savestr(buf + 2); 11292dd076b8SGabor Kovesdan if (out_of_mem) { 11302dd076b8SGabor Kovesdan p_end = i - 1; 11312dd076b8SGabor Kovesdan return false; 11322dd076b8SGabor Kovesdan } 11332dd076b8SGabor Kovesdan p_len[i] = strlen(p_line[i]); 11342dd076b8SGabor Kovesdan p_char[i] = '+'; 11352dd076b8SGabor Kovesdan } 11362dd076b8SGabor Kovesdan 11372dd076b8SGabor Kovesdan if (remove_special_line()) { 11382dd076b8SGabor Kovesdan p_len[i - 1] -= 1; 11392dd076b8SGabor Kovesdan (p_line[i - 1])[p_len[i - 1]] = 0; 11402dd076b8SGabor Kovesdan } 11412dd076b8SGabor Kovesdan } 11422dd076b8SGabor Kovesdan if (reverse) /* backwards patch? */ 11432dd076b8SGabor Kovesdan if (!pch_swap()) 11442dd076b8SGabor Kovesdan say("Not enough memory to swap next hunk!\n"); 11452dd076b8SGabor Kovesdan #ifdef DEBUGGING 11462dd076b8SGabor Kovesdan if (debug & 2) { 11472c4eed47SPedro F. Giffuni LINENUM i; 11482dd076b8SGabor Kovesdan char special; 11492dd076b8SGabor Kovesdan 11502dd076b8SGabor Kovesdan for (i = 0; i <= p_end; i++) { 11512dd076b8SGabor Kovesdan if (i == p_ptrn_lines) 11522dd076b8SGabor Kovesdan special = '^'; 11532dd076b8SGabor Kovesdan else 11542dd076b8SGabor Kovesdan special = ' '; 11552c4eed47SPedro F. Giffuni fprintf(stderr, "%3ld %c %c %s", i, p_char[i], 11562dd076b8SGabor Kovesdan special, p_line[i]); 11572dd076b8SGabor Kovesdan fflush(stderr); 11582dd076b8SGabor Kovesdan } 11592dd076b8SGabor Kovesdan } 11602dd076b8SGabor Kovesdan #endif 11612dd076b8SGabor Kovesdan if (p_end + 1 < hunkmax)/* paranoia reigns supreme... */ 11622dd076b8SGabor Kovesdan p_char[p_end + 1] = '^'; /* add a stopper for apply_hunk */ 11632dd076b8SGabor Kovesdan return true; 11642dd076b8SGabor Kovesdan } 11652dd076b8SGabor Kovesdan 11662dd076b8SGabor Kovesdan /* 11672dd076b8SGabor Kovesdan * Input a line from the patch file. 11682dd076b8SGabor Kovesdan * Worry about indentation if do_indent is true. 11692dd076b8SGabor Kovesdan * The line is read directly into the buf global variable which 11702dd076b8SGabor Kovesdan * is resized if necessary in order to hold the complete line. 11712dd076b8SGabor Kovesdan * Returns the number of characters read including the terminating 11722dd076b8SGabor Kovesdan * '\n', if any. 11732dd076b8SGabor Kovesdan */ 11742dd076b8SGabor Kovesdan size_t 11752dd076b8SGabor Kovesdan pgets(bool do_indent) 11762dd076b8SGabor Kovesdan { 11772dd076b8SGabor Kovesdan char *line; 11782dd076b8SGabor Kovesdan size_t len; 11792dd076b8SGabor Kovesdan int indent = 0, skipped = 0; 11802dd076b8SGabor Kovesdan 11812dd076b8SGabor Kovesdan line = fgetln(pfp, &len); 11822dd076b8SGabor Kovesdan if (line != NULL) { 11832dd076b8SGabor Kovesdan if (len + 1 > buf_size) { 11842dd076b8SGabor Kovesdan while (len + 1 > buf_size) 11852dd076b8SGabor Kovesdan buf_size *= 2; 11862dd076b8SGabor Kovesdan free(buf); 11872dd076b8SGabor Kovesdan buf = malloc(buf_size); 11882dd076b8SGabor Kovesdan if (buf == NULL) 11892dd076b8SGabor Kovesdan fatal("out of memory\n"); 11902dd076b8SGabor Kovesdan } 11912dd076b8SGabor Kovesdan if (do_indent == 1 && p_indent) { 11922dd076b8SGabor Kovesdan for (; 11932dd076b8SGabor Kovesdan indent < p_indent && (*line == ' ' || *line == '\t' || *line == 'X'); 11942dd076b8SGabor Kovesdan line++, skipped++) { 11952dd076b8SGabor Kovesdan if (*line == '\t') 11962dd076b8SGabor Kovesdan indent += 8 - (indent %7); 11972dd076b8SGabor Kovesdan else 11982dd076b8SGabor Kovesdan indent++; 11992dd076b8SGabor Kovesdan } 12002dd076b8SGabor Kovesdan } 12016d85e39bSDavid E. O'Brien memcpy(buf, line, len - skipped); 12022dd076b8SGabor Kovesdan buf[len - skipped] = '\0'; 12032dd076b8SGabor Kovesdan } 12042dd076b8SGabor Kovesdan return len; 12052dd076b8SGabor Kovesdan } 12062dd076b8SGabor Kovesdan 12072dd076b8SGabor Kovesdan 12082dd076b8SGabor Kovesdan /* 12092dd076b8SGabor Kovesdan * Reverse the old and new portions of the current hunk. 12102dd076b8SGabor Kovesdan */ 12112dd076b8SGabor Kovesdan bool 12122dd076b8SGabor Kovesdan pch_swap(void) 12132dd076b8SGabor Kovesdan { 12142dd076b8SGabor Kovesdan char **tp_line; /* the text of the hunk */ 12154f548c19SPedro F. Giffuni unsigned short *tp_len;/* length of each line */ 12162dd076b8SGabor Kovesdan char *tp_char; /* +, -, and ! */ 12172dd076b8SGabor Kovesdan LINENUM i; 12182dd076b8SGabor Kovesdan LINENUM n; 12192dd076b8SGabor Kovesdan bool blankline = false; 12202dd076b8SGabor Kovesdan char *s; 12212dd076b8SGabor Kovesdan 12222dd076b8SGabor Kovesdan i = p_first; 12232dd076b8SGabor Kovesdan p_first = p_newfirst; 12242dd076b8SGabor Kovesdan p_newfirst = i; 12252dd076b8SGabor Kovesdan 12262dd076b8SGabor Kovesdan /* make a scratch copy */ 12272dd076b8SGabor Kovesdan 12282dd076b8SGabor Kovesdan tp_line = p_line; 12292dd076b8SGabor Kovesdan tp_len = p_len; 12302dd076b8SGabor Kovesdan tp_char = p_char; 12312dd076b8SGabor Kovesdan p_line = NULL; /* force set_hunkmax to allocate again */ 12322dd076b8SGabor Kovesdan p_len = NULL; 12332dd076b8SGabor Kovesdan p_char = NULL; 12342dd076b8SGabor Kovesdan set_hunkmax(); 12352dd076b8SGabor Kovesdan if (p_line == NULL || p_len == NULL || p_char == NULL) { 12362dd076b8SGabor Kovesdan 12372dd076b8SGabor Kovesdan free(p_line); 12382dd076b8SGabor Kovesdan p_line = tp_line; 12392dd076b8SGabor Kovesdan free(p_len); 12402dd076b8SGabor Kovesdan p_len = tp_len; 12412dd076b8SGabor Kovesdan free(p_char); 12422dd076b8SGabor Kovesdan p_char = tp_char; 12432dd076b8SGabor Kovesdan return false; /* not enough memory to swap hunk! */ 12442dd076b8SGabor Kovesdan } 12452dd076b8SGabor Kovesdan /* now turn the new into the old */ 12462dd076b8SGabor Kovesdan 12472dd076b8SGabor Kovesdan i = p_ptrn_lines + 1; 12482dd076b8SGabor Kovesdan if (tp_char[i] == '\n') { /* account for possible blank line */ 12492dd076b8SGabor Kovesdan blankline = true; 12502dd076b8SGabor Kovesdan i++; 12512dd076b8SGabor Kovesdan } 12522dd076b8SGabor Kovesdan if (p_efake >= 0) { /* fix non-freeable ptr range */ 12532dd076b8SGabor Kovesdan if (p_efake <= i) 12542dd076b8SGabor Kovesdan n = p_end - i + 1; 12552dd076b8SGabor Kovesdan else 12562dd076b8SGabor Kovesdan n = -i; 12572dd076b8SGabor Kovesdan p_efake += n; 12582dd076b8SGabor Kovesdan p_bfake += n; 12592dd076b8SGabor Kovesdan } 12602dd076b8SGabor Kovesdan for (n = 0; i <= p_end; i++, n++) { 12612dd076b8SGabor Kovesdan p_line[n] = tp_line[i]; 12622dd076b8SGabor Kovesdan p_char[n] = tp_char[i]; 12632dd076b8SGabor Kovesdan if (p_char[n] == '+') 12642dd076b8SGabor Kovesdan p_char[n] = '-'; 12652dd076b8SGabor Kovesdan p_len[n] = tp_len[i]; 12662dd076b8SGabor Kovesdan } 12672dd076b8SGabor Kovesdan if (blankline) { 12682dd076b8SGabor Kovesdan i = p_ptrn_lines + 1; 12692dd076b8SGabor Kovesdan p_line[n] = tp_line[i]; 12702dd076b8SGabor Kovesdan p_char[n] = tp_char[i]; 12712dd076b8SGabor Kovesdan p_len[n] = tp_len[i]; 12722dd076b8SGabor Kovesdan n++; 12732dd076b8SGabor Kovesdan } 12742dd076b8SGabor Kovesdan if (p_char[0] != '=') 12752dd076b8SGabor Kovesdan fatal("Malformed patch at line %ld: expected '=' found '%c'\n", 12762dd076b8SGabor Kovesdan p_input_line, p_char[0]); 12772dd076b8SGabor Kovesdan p_char[0] = '*'; 12782dd076b8SGabor Kovesdan for (s = p_line[0]; *s; s++) 12792dd076b8SGabor Kovesdan if (*s == '-') 12802dd076b8SGabor Kovesdan *s = '*'; 12812dd076b8SGabor Kovesdan 12822dd076b8SGabor Kovesdan /* now turn the old into the new */ 12832dd076b8SGabor Kovesdan 12842dd076b8SGabor Kovesdan if (p_char[0] != '*') 12852dd076b8SGabor Kovesdan fatal("Malformed patch at line %ld: expected '*' found '%c'\n", 12862dd076b8SGabor Kovesdan p_input_line, p_char[0]); 12872dd076b8SGabor Kovesdan tp_char[0] = '='; 12882dd076b8SGabor Kovesdan for (s = tp_line[0]; *s; s++) 12892dd076b8SGabor Kovesdan if (*s == '*') 12902dd076b8SGabor Kovesdan *s = '-'; 12912dd076b8SGabor Kovesdan for (i = 0; n <= p_end; i++, n++) { 12922dd076b8SGabor Kovesdan p_line[n] = tp_line[i]; 12932dd076b8SGabor Kovesdan p_char[n] = tp_char[i]; 12942dd076b8SGabor Kovesdan if (p_char[n] == '-') 12952dd076b8SGabor Kovesdan p_char[n] = '+'; 12962dd076b8SGabor Kovesdan p_len[n] = tp_len[i]; 12972dd076b8SGabor Kovesdan } 12982dd076b8SGabor Kovesdan 12992dd076b8SGabor Kovesdan if (i != p_ptrn_lines + 1) 13002dd076b8SGabor Kovesdan fatal("Malformed patch at line %ld: expected %ld lines, " 13012dd076b8SGabor Kovesdan "got %ld\n", 13022dd076b8SGabor Kovesdan p_input_line, p_ptrn_lines + 1, i); 13032dd076b8SGabor Kovesdan 13042dd076b8SGabor Kovesdan i = p_ptrn_lines; 13052dd076b8SGabor Kovesdan p_ptrn_lines = p_repl_lines; 13062dd076b8SGabor Kovesdan p_repl_lines = i; 13072dd076b8SGabor Kovesdan 13082dd076b8SGabor Kovesdan free(tp_line); 13092dd076b8SGabor Kovesdan free(tp_len); 13102dd076b8SGabor Kovesdan free(tp_char); 13112dd076b8SGabor Kovesdan 13122dd076b8SGabor Kovesdan return true; 13132dd076b8SGabor Kovesdan } 13142dd076b8SGabor Kovesdan 13152dd076b8SGabor Kovesdan /* 13162dd076b8SGabor Kovesdan * Return the specified line position in the old file of the old context. 13172dd076b8SGabor Kovesdan */ 13182dd076b8SGabor Kovesdan LINENUM 13192dd076b8SGabor Kovesdan pch_first(void) 13202dd076b8SGabor Kovesdan { 13212dd076b8SGabor Kovesdan return p_first; 13222dd076b8SGabor Kovesdan } 13232dd076b8SGabor Kovesdan 13242dd076b8SGabor Kovesdan /* 13252dd076b8SGabor Kovesdan * Return the number of lines of old context. 13262dd076b8SGabor Kovesdan */ 13272dd076b8SGabor Kovesdan LINENUM 13282dd076b8SGabor Kovesdan pch_ptrn_lines(void) 13292dd076b8SGabor Kovesdan { 13302dd076b8SGabor Kovesdan return p_ptrn_lines; 13312dd076b8SGabor Kovesdan } 13322dd076b8SGabor Kovesdan 13332dd076b8SGabor Kovesdan /* 13342dd076b8SGabor Kovesdan * Return the probable line position in the new file of the first line. 13352dd076b8SGabor Kovesdan */ 13362dd076b8SGabor Kovesdan LINENUM 13372dd076b8SGabor Kovesdan pch_newfirst(void) 13382dd076b8SGabor Kovesdan { 13392dd076b8SGabor Kovesdan return p_newfirst; 13402dd076b8SGabor Kovesdan } 13412dd076b8SGabor Kovesdan 13422dd076b8SGabor Kovesdan /* 13432dd076b8SGabor Kovesdan * Return the number of lines in the replacement text including context. 13442dd076b8SGabor Kovesdan */ 13452dd076b8SGabor Kovesdan LINENUM 13462dd076b8SGabor Kovesdan pch_repl_lines(void) 13472dd076b8SGabor Kovesdan { 13482dd076b8SGabor Kovesdan return p_repl_lines; 13492dd076b8SGabor Kovesdan } 13502dd076b8SGabor Kovesdan 13512dd076b8SGabor Kovesdan /* 13522dd076b8SGabor Kovesdan * Return the number of lines in the whole hunk. 13532dd076b8SGabor Kovesdan */ 13542dd076b8SGabor Kovesdan LINENUM 13552dd076b8SGabor Kovesdan pch_end(void) 13562dd076b8SGabor Kovesdan { 13572dd076b8SGabor Kovesdan return p_end; 13582dd076b8SGabor Kovesdan } 13592dd076b8SGabor Kovesdan 13602dd076b8SGabor Kovesdan /* 13612dd076b8SGabor Kovesdan * Return the number of context lines before the first changed line. 13622dd076b8SGabor Kovesdan */ 13632dd076b8SGabor Kovesdan LINENUM 13642dd076b8SGabor Kovesdan pch_context(void) 13652dd076b8SGabor Kovesdan { 13662dd076b8SGabor Kovesdan return p_context; 13672dd076b8SGabor Kovesdan } 13682dd076b8SGabor Kovesdan 13692dd076b8SGabor Kovesdan /* 13702dd076b8SGabor Kovesdan * Return the length of a particular patch line. 13712dd076b8SGabor Kovesdan */ 13724f548c19SPedro F. Giffuni unsigned short 13732dd076b8SGabor Kovesdan pch_line_len(LINENUM line) 13742dd076b8SGabor Kovesdan { 13752dd076b8SGabor Kovesdan return p_len[line]; 13762dd076b8SGabor Kovesdan } 13772dd076b8SGabor Kovesdan 13782dd076b8SGabor Kovesdan /* 13792dd076b8SGabor Kovesdan * Return the control character (+, -, *, !, etc) for a patch line. 13802dd076b8SGabor Kovesdan */ 13812dd076b8SGabor Kovesdan char 13822dd076b8SGabor Kovesdan pch_char(LINENUM line) 13832dd076b8SGabor Kovesdan { 13842dd076b8SGabor Kovesdan return p_char[line]; 13852dd076b8SGabor Kovesdan } 13862dd076b8SGabor Kovesdan 13872dd076b8SGabor Kovesdan /* 13882dd076b8SGabor Kovesdan * Return a pointer to a particular patch line. 13892dd076b8SGabor Kovesdan */ 13902dd076b8SGabor Kovesdan char * 13912dd076b8SGabor Kovesdan pfetch(LINENUM line) 13922dd076b8SGabor Kovesdan { 13932dd076b8SGabor Kovesdan return p_line[line]; 13942dd076b8SGabor Kovesdan } 13952dd076b8SGabor Kovesdan 13962dd076b8SGabor Kovesdan /* 13972dd076b8SGabor Kovesdan * Return where in the patch file this hunk began, for error messages. 13982dd076b8SGabor Kovesdan */ 13992dd076b8SGabor Kovesdan LINENUM 14002dd076b8SGabor Kovesdan pch_hunk_beg(void) 14012dd076b8SGabor Kovesdan { 14022dd076b8SGabor Kovesdan return p_hunk_beg; 14032dd076b8SGabor Kovesdan } 14042dd076b8SGabor Kovesdan 14052dd076b8SGabor Kovesdan /* 14062dd076b8SGabor Kovesdan * Apply an ed script by feeding ed itself. 14072dd076b8SGabor Kovesdan */ 14082dd076b8SGabor Kovesdan void 14092dd076b8SGabor Kovesdan do_ed_script(void) 14102dd076b8SGabor Kovesdan { 14112dd076b8SGabor Kovesdan char *t; 1412e91a64deSPedro F. Giffuni off_t beginning_of_this_line; 14132dd076b8SGabor Kovesdan FILE *pipefp = NULL; 14143fd78bfaSXin LI int continuation; 14152dd076b8SGabor Kovesdan 14162dd076b8SGabor Kovesdan if (!skip_rest_of_patch) { 14172dd076b8SGabor Kovesdan if (copy_file(filearg[0], TMPOUTNAME) < 0) { 14182dd076b8SGabor Kovesdan unlink(TMPOUTNAME); 14192dd076b8SGabor Kovesdan fatal("can't create temp file %s", TMPOUTNAME); 14202dd076b8SGabor Kovesdan } 14213fd78bfaSXin LI snprintf(buf, buf_size, "%s%s%s", _PATH_RED, 14222dd076b8SGabor Kovesdan verbose ? " " : " -s ", TMPOUTNAME); 14232dd076b8SGabor Kovesdan pipefp = popen(buf, "w"); 14242dd076b8SGabor Kovesdan } 14252dd076b8SGabor Kovesdan for (;;) { 1426e91a64deSPedro F. Giffuni beginning_of_this_line = ftello(pfp); 14272dd076b8SGabor Kovesdan if (pgets(true) == 0) { 14282dd076b8SGabor Kovesdan next_intuit_at(beginning_of_this_line, p_input_line); 14292dd076b8SGabor Kovesdan break; 14302dd076b8SGabor Kovesdan } 14312dd076b8SGabor Kovesdan p_input_line++; 14322dd076b8SGabor Kovesdan for (t = buf; isdigit((unsigned char)*t) || *t == ','; t++) 14332dd076b8SGabor Kovesdan ; 14342dd076b8SGabor Kovesdan /* POSIX defines allowed commands as {a,c,d,i,s} */ 14352b468ebaSPedro F. Giffuni if (isdigit((unsigned char)*buf) && 14362b468ebaSPedro F. Giffuni (*t == 'a' || *t == 'c' || *t == 'd' || *t == 'i' || *t == 's')) { 14372dd076b8SGabor Kovesdan if (pipefp != NULL) 14382dd076b8SGabor Kovesdan fputs(buf, pipefp); 14393fd78bfaSXin LI if (*t == 's') { 14403fd78bfaSXin LI for (;;) { 14413fd78bfaSXin LI continuation = 0; 14423fd78bfaSXin LI t = strchr(buf, '\0') - 1; 14433fd78bfaSXin LI while (--t >= buf && *t == '\\') 14443fd78bfaSXin LI continuation = !continuation; 14453fd78bfaSXin LI if (!continuation || 14463fd78bfaSXin LI pgets(true) == 0) 14473fd78bfaSXin LI break; 14483fd78bfaSXin LI if (pipefp != NULL) 14493fd78bfaSXin LI fputs(buf, pipefp); 14503fd78bfaSXin LI } 14513fd78bfaSXin LI } else if (*t != 'd') { 14522dd076b8SGabor Kovesdan while (pgets(true)) { 14532dd076b8SGabor Kovesdan p_input_line++; 14542dd076b8SGabor Kovesdan if (pipefp != NULL) 14552dd076b8SGabor Kovesdan fputs(buf, pipefp); 14562dd076b8SGabor Kovesdan if (strEQ(buf, ".\n")) 14572dd076b8SGabor Kovesdan break; 14582dd076b8SGabor Kovesdan } 14592dd076b8SGabor Kovesdan } 14602dd076b8SGabor Kovesdan } else { 14612dd076b8SGabor Kovesdan next_intuit_at(beginning_of_this_line, p_input_line); 14622dd076b8SGabor Kovesdan break; 14632dd076b8SGabor Kovesdan } 14642dd076b8SGabor Kovesdan } 14652dd076b8SGabor Kovesdan if (pipefp == NULL) 14662dd076b8SGabor Kovesdan return; 14672dd076b8SGabor Kovesdan fprintf(pipefp, "w\n"); 14682dd076b8SGabor Kovesdan fprintf(pipefp, "q\n"); 14692dd076b8SGabor Kovesdan fflush(pipefp); 14702dd076b8SGabor Kovesdan pclose(pipefp); 14712dd076b8SGabor Kovesdan ignore_signals(); 14722dd076b8SGabor Kovesdan if (!check_only) { 14732dd076b8SGabor Kovesdan if (move_file(TMPOUTNAME, outname) < 0) { 14742dd076b8SGabor Kovesdan toutkeep = true; 14752dd076b8SGabor Kovesdan chmod(TMPOUTNAME, filemode); 14762dd076b8SGabor Kovesdan } else 14772dd076b8SGabor Kovesdan chmod(outname, filemode); 14782dd076b8SGabor Kovesdan } 14792dd076b8SGabor Kovesdan set_signals(1); 14802dd076b8SGabor Kovesdan } 14812dd076b8SGabor Kovesdan 14822dd076b8SGabor Kovesdan /* 14832dd076b8SGabor Kovesdan * Choose the name of the file to be patched based on POSIX rules. 14842dd076b8SGabor Kovesdan * NOTE: the POSIX rules are amazingly stupid and we only follow them 14852dd076b8SGabor Kovesdan * if the user specified --posix or set POSIXLY_CORRECT. 14862dd076b8SGabor Kovesdan */ 14872dd076b8SGabor Kovesdan static char * 14882dd076b8SGabor Kovesdan posix_name(const struct file_name *names, bool assume_exists) 14892dd076b8SGabor Kovesdan { 14902dd076b8SGabor Kovesdan char *path = NULL; 14912dd076b8SGabor Kovesdan int i; 14922dd076b8SGabor Kovesdan 14932dd076b8SGabor Kovesdan /* 14942dd076b8SGabor Kovesdan * POSIX states that the filename will be chosen from one 14952dd076b8SGabor Kovesdan * of the old, new and index names (in that order) if 14962dd076b8SGabor Kovesdan * the file exists relative to CWD after -p stripping. 14972dd076b8SGabor Kovesdan */ 14982dd076b8SGabor Kovesdan for (i = 0; i < MAX_FILE; i++) { 14992dd076b8SGabor Kovesdan if (names[i].path != NULL && names[i].exists) { 15002dd076b8SGabor Kovesdan path = names[i].path; 15012dd076b8SGabor Kovesdan break; 15022dd076b8SGabor Kovesdan } 15032dd076b8SGabor Kovesdan } 15042dd076b8SGabor Kovesdan if (path == NULL && !assume_exists) { 15052dd076b8SGabor Kovesdan /* 1506e678759cSXin LI * No files found, check to see if the diff could be 1507e678759cSXin LI * creating a new file. 15082dd076b8SGabor Kovesdan */ 15092dd076b8SGabor Kovesdan if (path == NULL && ok_to_create_file && 15102dd076b8SGabor Kovesdan names[NEW_FILE].path != NULL) 15112dd076b8SGabor Kovesdan path = names[NEW_FILE].path; 15122dd076b8SGabor Kovesdan } 15132dd076b8SGabor Kovesdan 1514547e0acbSPedro F. Giffuni return path ? xstrdup(path) : NULL; 15152dd076b8SGabor Kovesdan } 15162dd076b8SGabor Kovesdan 15172dd076b8SGabor Kovesdan static char * 1518e678759cSXin LI compare_names(const struct file_name *names, bool assume_exists) 15192dd076b8SGabor Kovesdan { 15202dd076b8SGabor Kovesdan size_t min_components, min_baselen, min_len, tmp; 15212dd076b8SGabor Kovesdan char *best = NULL; 152279d8aaa9SStefan Eßer char *path; 15232dd076b8SGabor Kovesdan int i; 15242dd076b8SGabor Kovesdan 15252dd076b8SGabor Kovesdan /* 15262dd076b8SGabor Kovesdan * The "best" name is the one with the fewest number of path 15272dd076b8SGabor Kovesdan * components, the shortest basename length, and the shortest 15282dd076b8SGabor Kovesdan * overall length (in that order). We only use the Index: file 15292dd076b8SGabor Kovesdan * if neither of the old or new files could be intuited from 15302dd076b8SGabor Kovesdan * the diff header. 15312dd076b8SGabor Kovesdan */ 15322dd076b8SGabor Kovesdan min_components = min_baselen = min_len = SIZE_MAX; 15332dd076b8SGabor Kovesdan for (i = INDEX_FILE; i >= OLD_FILE; i--) { 153479d8aaa9SStefan Eßer path = names[i].path; 1535e678759cSXin LI if (path == NULL || (!names[i].exists && !assume_exists)) 15362dd076b8SGabor Kovesdan continue; 153779d8aaa9SStefan Eßer if ((tmp = num_components(path)) > min_components) 15382dd076b8SGabor Kovesdan continue; 15396b239879SStefan Eßer if (tmp < min_components) { 15402dd076b8SGabor Kovesdan min_components = tmp; 154179d8aaa9SStefan Eßer best = path; 15426b239879SStefan Eßer } 154379d8aaa9SStefan Eßer if ((tmp = strlen(basename(path))) > min_baselen) 15442dd076b8SGabor Kovesdan continue; 15456b239879SStefan Eßer if (tmp < min_baselen) { 15462dd076b8SGabor Kovesdan min_baselen = tmp; 154779d8aaa9SStefan Eßer best = path; 15486b239879SStefan Eßer } 154979d8aaa9SStefan Eßer if ((tmp = strlen(path)) > min_len) 15502dd076b8SGabor Kovesdan continue; 15512dd076b8SGabor Kovesdan min_len = tmp; 155279d8aaa9SStefan Eßer best = path; 15532dd076b8SGabor Kovesdan } 155479d8aaa9SStefan Eßer return best; 155579d8aaa9SStefan Eßer } 155679d8aaa9SStefan Eßer 15572dd076b8SGabor Kovesdan /* 155879d8aaa9SStefan Eßer * Choose the name of the file to be patched based the "best" one 155979d8aaa9SStefan Eßer * available. 15602dd076b8SGabor Kovesdan */ 156179d8aaa9SStefan Eßer static char * 156279d8aaa9SStefan Eßer best_name(const struct file_name *names, bool assume_exists) 156379d8aaa9SStefan Eßer { 156479d8aaa9SStefan Eßer char *best; 156579d8aaa9SStefan Eßer 1566e678759cSXin LI best = compare_names(names, assume_exists); 1567e678759cSXin LI 1568e678759cSXin LI /* No match? Check to see if the diff could be creating a new file. */ 1569e678759cSXin LI if (best == NULL && ok_to_create_file) 15702dd076b8SGabor Kovesdan best = names[NEW_FILE].path; 15712dd076b8SGabor Kovesdan 1572547e0acbSPedro F. Giffuni return best ? xstrdup(best) : NULL; 15732dd076b8SGabor Kovesdan } 15742dd076b8SGabor Kovesdan 15752dd076b8SGabor Kovesdan static size_t 15762dd076b8SGabor Kovesdan num_components(const char *path) 15772dd076b8SGabor Kovesdan { 15782dd076b8SGabor Kovesdan size_t n; 15792dd076b8SGabor Kovesdan const char *cp; 15802dd076b8SGabor Kovesdan 15812dd076b8SGabor Kovesdan for (n = 0, cp = path; (cp = strchr(cp, '/')) != NULL; n++, cp++) { 15822dd076b8SGabor Kovesdan while (*cp == '/') 15832dd076b8SGabor Kovesdan cp++; /* skip consecutive slashes */ 15842dd076b8SGabor Kovesdan } 15852dd076b8SGabor Kovesdan return n; 15862dd076b8SGabor Kovesdan } 1587d3fc0cb8SPedro F. Giffuni 1588d3fc0cb8SPedro F. Giffuni /* 1589d3fc0cb8SPedro F. Giffuni * Convert number at NPTR into LINENUM and save address of first 1590d3fc0cb8SPedro F. Giffuni * character that is not a digit in ENDPTR. If conversion is not 1591d3fc0cb8SPedro F. Giffuni * possible, call fatal. 1592d3fc0cb8SPedro F. Giffuni */ 1593d3fc0cb8SPedro F. Giffuni static LINENUM 1594d3fc0cb8SPedro F. Giffuni strtolinenum(char *nptr, char **endptr) 1595d3fc0cb8SPedro F. Giffuni { 1596d3fc0cb8SPedro F. Giffuni LINENUM rv; 1597d3fc0cb8SPedro F. Giffuni char c; 1598d3fc0cb8SPedro F. Giffuni char *p; 1599d3fc0cb8SPedro F. Giffuni const char *errstr; 1600d3fc0cb8SPedro F. Giffuni 1601d3fc0cb8SPedro F. Giffuni for (p = nptr; isdigit((unsigned char)*p); p++) 1602d3fc0cb8SPedro F. Giffuni ; 1603d3fc0cb8SPedro F. Giffuni 1604d3fc0cb8SPedro F. Giffuni if (p == nptr) 1605d3fc0cb8SPedro F. Giffuni malformed(); 1606d3fc0cb8SPedro F. Giffuni 1607d3fc0cb8SPedro F. Giffuni c = *p; 1608d3fc0cb8SPedro F. Giffuni *p = '\0'; 1609d3fc0cb8SPedro F. Giffuni 1610d3fc0cb8SPedro F. Giffuni rv = strtonum(nptr, 0, LINENUM_MAX, &errstr); 1611d3fc0cb8SPedro F. Giffuni if (errstr != NULL) 1612d3fc0cb8SPedro F. Giffuni fatal("invalid line number at line %ld: `%s' is %s\n", 1613d3fc0cb8SPedro F. Giffuni p_input_line, nptr, errstr); 1614d3fc0cb8SPedro F. Giffuni 1615d3fc0cb8SPedro F. Giffuni *p = c; 1616d3fc0cb8SPedro F. Giffuni *endptr = p; 1617d3fc0cb8SPedro F. Giffuni 1618d3fc0cb8SPedro F. Giffuni return rv; 1619d3fc0cb8SPedro F. Giffuni } 1620