Annotation of src/usr.bin/patch/pch.c, Revision 1.43
1.43 ! tobias 1: /* $OpenBSD: pch.c,v 1.42 2014/11/17 10:58:09 tobias Exp $ */
1.30 otto 2:
3: /*
4: * patch - a program to apply diffs to original files
5: *
6: * Copyright 1986, Larry Wall
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following condition is met:
10: * 1. Redistributions of source code must retain the above copyright notice,
11: * this condition and the following disclaimer.
12: *
13: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
14: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16: * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
17: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23: * SUCH DAMAGE.
24: *
25: * -C option added in 1998, original code by Marc Espie, based on FreeBSD
26: * behaviour
27: */
1.1 deraadt 28:
1.21 otto 29: #include <sys/types.h>
30: #include <sys/stat.h>
31:
32: #include <ctype.h>
1.32 millert 33: #include <libgen.h>
34: #include <limits.h>
1.25 otto 35: #include <stdio.h>
1.21 otto 36: #include <stdlib.h>
37: #include <string.h>
38: #include <unistd.h>
39:
1.1 deraadt 40: #include "common.h"
41: #include "util.h"
42: #include "pch.h"
1.27 millert 43: #include "pathnames.h"
1.1 deraadt 44:
45: /* Patch (diff listing) abstract type. */
46:
1.42 tobias 47: static off_t p_filesize; /* size of the patch file */
1.17 deraadt 48: static LINENUM p_first; /* 1st line number */
49: static LINENUM p_newfirst; /* 1st line number of replacement */
50: static LINENUM p_ptrn_lines; /* # lines in pattern */
51: static LINENUM p_repl_lines; /* # lines in replacement text */
52: static LINENUM p_end = -1; /* last line in hunk */
53: static LINENUM p_max; /* max allowed value of p_end */
54: static LINENUM p_context = 3; /* # of context lines */
55: static LINENUM p_input_line = 0; /* current line # from patch file */
1.25 otto 56: static char **p_line = NULL;/* the text of the hunk */
1.21 otto 57: static short *p_len = NULL; /* length of each line */
1.25 otto 58: static char *p_char = NULL; /* +, -, and ! */
1.17 deraadt 59: static int hunkmax = INITHUNKMAX; /* size of above arrays to begin with */
60: static int p_indent; /* indent to patch */
1.42 tobias 61: static off_t p_base; /* where to intuit this time */
1.17 deraadt 62: static LINENUM p_bline; /* line # of p_base */
1.42 tobias 63: static off_t p_start; /* where intuit found a patch */
1.17 deraadt 64: static LINENUM p_sline; /* and the line number for it */
65: static LINENUM p_hunk_beg; /* line number of current hunk */
66: static LINENUM p_efake = -1; /* end of faked up lines--don't free */
67: static LINENUM p_bfake = -1; /* beg of faked up lines */
1.25 otto 68: static FILE *pfp = NULL; /* patch file pointer */
69: static char *bestguess = NULL; /* guess at correct filename */
1.1 deraadt 70:
1.21 otto 71: static void grow_hunkmax(void);
72: static int intuit_diff_type(void);
1.42 tobias 73: static void next_intuit_at(off_t, LINENUM);
74: static void skip_to(off_t, LINENUM);
1.23 deraadt 75: static char *pgets(char *, int, FILE *);
1.32 millert 76: static char *best_name(const struct file_name *, bool);
77: static char *posix_name(const struct file_name *, bool);
78: static size_t num_components(const char *);
1.25 otto 79:
1.17 deraadt 80: /*
81: * Prepare to look for the next patch in the patch file.
82: */
1.1 deraadt 83: void
1.17 deraadt 84: re_patch(void)
1.1 deraadt 85: {
1.25 otto 86: p_first = 0;
87: p_newfirst = 0;
88: p_ptrn_lines = 0;
89: p_repl_lines = 0;
1.17 deraadt 90: p_end = (LINENUM) - 1;
1.25 otto 91: p_max = 0;
1.17 deraadt 92: p_indent = 0;
1.1 deraadt 93: }
94:
1.17 deraadt 95: /*
96: * Open the patch file at the beginning of time.
97: */
1.1 deraadt 98: void
1.25 otto 99: open_patch_file(const char *filename)
1.1 deraadt 100: {
1.25 otto 101: struct stat filestat;
102:
103: if (filename == NULL || *filename == '\0' || strEQ(filename, "-")) {
1.17 deraadt 104: pfp = fopen(TMPPATNAME, "w");
1.21 otto 105: if (pfp == NULL)
1.17 deraadt 106: pfatal("can't create %s", TMPPATNAME);
1.21 otto 107: while (fgets(buf, sizeof buf, stdin) != NULL)
1.17 deraadt 108: fputs(buf, pfp);
109: fclose(pfp);
110: filename = TMPPATNAME;
111: }
112: pfp = fopen(filename, "r");
1.21 otto 113: if (pfp == NULL)
1.17 deraadt 114: pfatal("patch file %s not found", filename);
1.42 tobias 115: if (fstat(fileno(pfp), &filestat))
116: pfatal("can't stat %s", filename);
1.17 deraadt 117: p_filesize = filestat.st_size;
1.42 tobias 118: next_intuit_at(0, 1L); /* start at the beginning */
1.17 deraadt 119: set_hunkmax();
1.1 deraadt 120: }
121:
1.17 deraadt 122: /*
123: * Make sure our dynamically realloced tables are malloced to begin with.
124: */
1.1 deraadt 125: void
1.17 deraadt 126: set_hunkmax(void)
1.1 deraadt 127: {
1.21 otto 128: if (p_line == NULL)
1.37 deraadt 129: p_line = calloc((size_t) hunkmax, sizeof(char *));
1.21 otto 130: if (p_len == NULL)
1.37 deraadt 131: p_len = calloc((size_t) hunkmax, sizeof(short));
1.21 otto 132: if (p_char == NULL)
1.37 deraadt 133: p_char = calloc((size_t) hunkmax, sizeof(char));
1.1 deraadt 134: }
135:
1.17 deraadt 136: /*
137: * Enlarge the arrays containing the current hunk of patch.
138: */
1.21 otto 139: static void
1.17 deraadt 140: grow_hunkmax(void)
1.1 deraadt 141: {
1.31 otto 142: int new_hunkmax;
1.35 deraadt 143: char **new_p_line;
1.31 otto 144: short *new_p_len;
145: char *new_p_char;
146:
147: new_hunkmax = hunkmax * 2;
1.17 deraadt 148:
1.28 otto 149: if (p_line == NULL || p_len == NULL || p_char == NULL)
150: fatal("Internal memory allocation error\n");
1.21 otto 151:
1.31 otto 152: new_p_line = realloc(p_line, new_hunkmax * sizeof(char *));
153: if (new_p_line == NULL)
154: free(p_line);
155:
156: new_p_len = realloc(p_len, new_hunkmax * sizeof(short));
157: if (new_p_len == NULL)
158: free(p_len);
159:
160: new_p_char = realloc(p_char, new_hunkmax * sizeof(char));
161: if (new_p_char == NULL)
162: free(p_char);
163:
164: p_char = new_p_char;
165: p_len = new_p_len;
166: p_line = new_p_line;
1.21 otto 167:
1.31 otto 168: if (p_line != NULL && p_len != NULL && p_char != NULL) {
169: hunkmax = new_hunkmax;
1.17 deraadt 170: return;
1.31 otto 171: }
172:
1.17 deraadt 173: if (!using_plan_a)
174: fatal("out of memory\n");
1.28 otto 175: out_of_mem = true; /* whatever is null will be allocated again */
176: /* from within plan_a(), of all places */
1.1 deraadt 177: }
178:
179: /* True if the remainder of the patch file contains a diff of some sort. */
180:
181: bool
1.17 deraadt 182: there_is_another_patch(void)
1.1 deraadt 183: {
1.32 millert 184: bool exists = false;
185:
1.42 tobias 186: if (p_base != 0 && p_base >= p_filesize) {
1.17 deraadt 187: if (verbose)
188: say("done\n");
1.28 otto 189: return false;
1.17 deraadt 190: }
1.1 deraadt 191: if (verbose)
1.17 deraadt 192: say("Hmm...");
193: diff_type = intuit_diff_type();
194: if (!diff_type) {
1.42 tobias 195: if (p_base != 0) {
1.17 deraadt 196: if (verbose)
197: say(" Ignoring the trailing garbage.\ndone\n");
198: } else
199: say(" I can't seem to find a patch in there anywhere.\n");
1.28 otto 200: return false;
1.1 deraadt 201: }
1.17 deraadt 202: if (verbose)
203: say(" %sooks like %s to me...\n",
1.42 tobias 204: (p_base == 0 ? "L" : "The next patch l"),
1.17 deraadt 205: diff_type == UNI_DIFF ? "a unified diff" :
206: diff_type == CONTEXT_DIFF ? "a context diff" :
207: diff_type == NEW_CONTEXT_DIFF ? "a new-style context diff" :
208: diff_type == NORMAL_DIFF ? "a normal diff" :
209: "an ed script");
210: if (p_indent && verbose)
211: say("(Patch is indented %d space%s.)\n", p_indent,
212: p_indent == 1 ? "" : "s");
213: skip_to(p_start, p_sline);
1.21 otto 214: while (filearg[0] == NULL) {
1.17 deraadt 215: if (force || batch) {
216: say("No file to patch. Skipping...\n");
217: filearg[0] = savestr(bestguess);
1.28 otto 218: skip_rest_of_patch = true;
219: return true;
1.17 deraadt 220: }
221: ask("File to patch: ");
222: if (*buf != '\n') {
1.25 otto 223: free(bestguess);
1.17 deraadt 224: bestguess = savestr(buf);
1.32 millert 225: filearg[0] = fetchname(buf, &exists, 0);
1.17 deraadt 226: }
1.32 millert 227: if (!exists) {
1.17 deraadt 228: ask("No file found--skip this patch? [n] ");
229: if (*buf != 'y')
230: continue;
231: if (verbose)
232: say("Skipping patch...\n");
1.33 otto 233: free(filearg[0]);
1.32 millert 234: filearg[0] = fetchname(bestguess, &exists, 0);
1.28 otto 235: skip_rest_of_patch = true;
236: return true;
1.17 deraadt 237: }
1.1 deraadt 238: }
1.28 otto 239: return true;
1.1 deraadt 240: }
241:
242: /* Determine what kind of diff is in the remaining part of the patch file. */
243:
1.21 otto 244: static int
1.17 deraadt 245: intuit_diff_type(void)
1.1 deraadt 246: {
1.42 tobias 247: off_t this_line = 0, previous_line;
248: off_t first_command_line = -1;
1.28 otto 249: LINENUM fcl_line = -1;
250: bool last_line_was_command = false, this_is_a_command = false;
251: bool stars_last_line = false, stars_this_line = false;
1.17 deraadt 252: char *s, *t;
253: int indent, retval;
1.32 millert 254: struct file_name names[MAX_FILE];
1.17 deraadt 255:
1.32 millert 256: memset(names, 0, sizeof(names));
1.28 otto 257: ok_to_create_file = false;
1.42 tobias 258: fseeko(pfp, p_base, SEEK_SET);
1.17 deraadt 259: p_input_line = p_bline - 1;
260: for (;;) {
261: previous_line = this_line;
262: last_line_was_command = this_is_a_command;
263: stars_last_line = stars_this_line;
1.42 tobias 264: this_line = ftello(pfp);
1.17 deraadt 265: indent = 0;
266: p_input_line++;
1.21 otto 267: if (fgets(buf, sizeof buf, pfp) == NULL) {
1.42 tobias 268: if (first_command_line >= 0) {
1.17 deraadt 269: /* nothing but deletes!? */
270: p_start = first_command_line;
271: p_sline = fcl_line;
272: retval = ED_DIFF;
273: goto scan_exit;
274: } else {
275: p_start = this_line;
276: p_sline = p_input_line;
277: retval = 0;
278: goto scan_exit;
279: }
280: }
281: for (s = buf; *s == ' ' || *s == '\t' || *s == 'X'; s++) {
282: if (*s == '\t')
283: indent += 8 - (indent % 8);
284: else
285: indent++;
286: }
1.41 deraadt 287: for (t = s; isdigit((unsigned char)*t) || *t == ','; t++)
1.17 deraadt 288: ;
1.41 deraadt 289: this_is_a_command = (isdigit((unsigned char)*s) &&
1.17 deraadt 290: (*t == 'd' || *t == 'c' || *t == 'a'));
1.42 tobias 291: if (first_command_line < 0 && this_is_a_command) {
1.17 deraadt 292: first_command_line = this_line;
293: fcl_line = p_input_line;
294: p_indent = indent; /* assume this for now */
295: }
296: if (!stars_last_line && strnEQ(s, "*** ", 4))
1.32 millert 297: names[OLD_FILE].path = fetchname(s + 4,
298: &names[OLD_FILE].exists, strippath);
1.17 deraadt 299: else if (strnEQ(s, "--- ", 4))
1.32 millert 300: names[NEW_FILE].path = fetchname(s + 4,
301: &names[NEW_FILE].exists, strippath);
1.17 deraadt 302: else if (strnEQ(s, "+++ ", 4))
1.32 millert 303: /* pretend it is the old name */
304: names[OLD_FILE].path = fetchname(s + 4,
305: &names[OLD_FILE].exists, strippath);
1.17 deraadt 306: else if (strnEQ(s, "Index:", 6))
1.32 millert 307: names[INDEX_FILE].path = fetchname(s + 6,
308: &names[INDEX_FILE].exists, strippath);
1.17 deraadt 309: else if (strnEQ(s, "Prereq:", 7)) {
1.41 deraadt 310: for (t = s + 7; isspace((unsigned char)*t); t++)
1.18 deraadt 311: ;
1.17 deraadt 312: revision = savestr(t);
1.41 deraadt 313: for (t = revision;
314: *t && !isspace((unsigned char)*t); t++)
1.18 deraadt 315: ;
1.17 deraadt 316: *t = '\0';
1.25 otto 317: if (*revision == '\0') {
1.17 deraadt 318: free(revision);
1.21 otto 319: revision = NULL;
1.17 deraadt 320: }
321: }
322: if ((!diff_type || diff_type == ED_DIFF) &&
1.42 tobias 323: first_command_line >= 0 &&
1.17 deraadt 324: strEQ(s, ".\n")) {
325: p_indent = indent;
326: p_start = first_command_line;
327: p_sline = fcl_line;
328: retval = ED_DIFF;
329: goto scan_exit;
330: }
331: if ((!diff_type || diff_type == UNI_DIFF) && strnEQ(s, "@@ -", 4)) {
1.32 millert 332: if (strnEQ(s + 4, "0,0", 3))
1.28 otto 333: ok_to_create_file = true;
1.17 deraadt 334: p_indent = indent;
335: p_start = this_line;
336: p_sline = p_input_line;
337: retval = UNI_DIFF;
338: goto scan_exit;
339: }
340: stars_this_line = strnEQ(s, "********", 8);
341: if ((!diff_type || diff_type == CONTEXT_DIFF) && stars_last_line &&
342: strnEQ(s, "*** ", 4)) {
1.32 millert 343: if (atol(s + 4) == 0)
1.28 otto 344: ok_to_create_file = true;
1.17 deraadt 345: /*
1.32 millert 346: * If this is a new context diff the character just
1.43 ! tobias 347: * at the end of the line is a '*'.
1.17 deraadt 348: */
1.43 ! tobias 349: while (*s && *s != '\n')
1.17 deraadt 350: s++;
351: p_indent = indent;
352: p_start = previous_line;
353: p_sline = p_input_line - 1;
354: retval = (*(s - 1) == '*' ? NEW_CONTEXT_DIFF : CONTEXT_DIFF);
355: goto scan_exit;
356: }
357: if ((!diff_type || diff_type == NORMAL_DIFF) &&
358: last_line_was_command &&
359: (strnEQ(s, "< ", 2) || strnEQ(s, "> ", 2))) {
360: p_start = previous_line;
361: p_sline = p_input_line - 1;
362: p_indent = indent;
363: retval = NORMAL_DIFF;
364: goto scan_exit;
365: }
366: }
367: scan_exit:
1.32 millert 368: if (retval == UNI_DIFF) {
369: /* unswap old and new */
370: struct file_name tmp = names[OLD_FILE];
371: names[OLD_FILE] = names[NEW_FILE];
372: names[NEW_FILE] = tmp;
373: }
374: if (filearg[0] == NULL) {
375: if (posix)
376: filearg[0] = posix_name(names, ok_to_create_file);
377: else {
378: /* Ignore the Index: name for context diffs, like GNU */
379: if (names[OLD_FILE].path != NULL ||
380: names[NEW_FILE].path != NULL) {
381: free(names[INDEX_FILE].path);
382: names[INDEX_FILE].path = NULL;
383: }
384: filearg[0] = best_name(names, ok_to_create_file);
385: }
1.17 deraadt 386: }
1.25 otto 387:
388: free(bestguess);
389: bestguess = NULL;
1.21 otto 390: if (filearg[0] != NULL)
1.17 deraadt 391: bestguess = savestr(filearg[0]);
1.32 millert 392: else if (!ok_to_create_file) {
393: /*
394: * We don't want to create a new file but we need a
395: * filename to set bestguess. Avoid setting filearg[0]
396: * so the file is not created automatically.
397: */
398: if (posix)
399: bestguess = posix_name(names, true);
400: else
401: bestguess = best_name(names, true);
402: }
403: free(names[OLD_FILE].path);
404: free(names[NEW_FILE].path);
405: free(names[INDEX_FILE].path);
1.17 deraadt 406: return retval;
1.1 deraadt 407: }
408:
1.17 deraadt 409: /*
410: * Remember where this patch ends so we know where to start up again.
411: */
1.21 otto 412: static void
1.42 tobias 413: next_intuit_at(off_t file_pos, LINENUM file_line)
1.1 deraadt 414: {
1.17 deraadt 415: p_base = file_pos;
416: p_bline = file_line;
1.1 deraadt 417: }
418:
1.17 deraadt 419: /*
1.42 tobias 420: * Basically a verbose fseeko() to the actual diff listing.
1.17 deraadt 421: */
1.21 otto 422: static void
1.42 tobias 423: skip_to(off_t file_pos, LINENUM file_line)
1.1 deraadt 424: {
1.17 deraadt 425: char *ret;
1.1 deraadt 426:
1.28 otto 427: if (p_base > file_pos)
1.42 tobias 428: fatal("Internal error: seek %lld>%lld\n",
429: (long long)p_base, (long long)file_pos);
1.17 deraadt 430: if (verbose && p_base < file_pos) {
1.42 tobias 431: fseeko(pfp, p_base, SEEK_SET);
1.17 deraadt 432: say("The text leading up to this was:\n--------------------------\n");
1.42 tobias 433: while (ftello(pfp) < file_pos) {
1.17 deraadt 434: ret = fgets(buf, sizeof buf, pfp);
1.28 otto 435: if (ret == NULL)
436: fatal("Unexpected end of file\n");
1.17 deraadt 437: say("|%s", buf);
438: }
439: say("--------------------------\n");
440: } else
1.42 tobias 441: fseeko(pfp, file_pos, SEEK_SET);
1.17 deraadt 442: p_input_line = file_line - 1;
1.1 deraadt 443: }
444:
445: /* Make this a function for better debugging. */
446: static void
1.16 deraadt 447: malformed(void)
1.1 deraadt 448: {
1.17 deraadt 449: fatal("malformed patch at line %ld: %s", p_input_line, buf);
450: /* about as informative as "Syntax error" in C */
1.1 deraadt 451: }
452:
1.14 otto 453: /*
454: * True if the line has been discarded (i.e. it is a line saying
455: * "\ No newline at end of file".)
456: */
457: static bool
458: remove_special_line(void)
459: {
1.17 deraadt 460: int c;
1.14 otto 461:
462: c = fgetc(pfp);
463: if (c == '\\') {
464: do {
465: c = fgetc(pfp);
466: } while (c != EOF && c != '\n');
467:
1.28 otto 468: return true;
1.14 otto 469: }
470: if (c != EOF)
1.42 tobias 471: fseeko(pfp, -1, SEEK_CUR);
1.14 otto 472:
1.28 otto 473: return false;
1.14 otto 474: }
475:
1.17 deraadt 476: /*
477: * True if there is more of the current diff listing to process.
478: */
1.1 deraadt 479: bool
1.17 deraadt 480: another_hunk(void)
1.1 deraadt 481: {
1.42 tobias 482: off_t line_beginning; /* file pos of the current line */
1.17 deraadt 483: LINENUM repl_beginning; /* index of --- line */
484: LINENUM fillcnt; /* #lines of missing ptrn or repl */
485: LINENUM fillsrc; /* index of first line to copy */
486: LINENUM filldst; /* index of first missing line */
487: bool ptrn_spaces_eaten; /* ptrn was slightly misformed */
488: bool repl_could_be_missing; /* no + or ! lines in this hunk */
489: bool repl_missing; /* we are now backtracking */
1.42 tobias 490: off_t repl_backtrack_position; /* file pos of first repl line */
1.17 deraadt 491: LINENUM repl_patch_line; /* input line number for same */
492: LINENUM ptrn_copiable; /* # of copiable lines in ptrn */
493: char *s, *ret;
494: int context = 0;
495:
496: while (p_end >= 0) {
497: if (p_end == p_efake)
498: p_end = p_bfake; /* don't free twice */
499: else
500: free(p_line[p_end]);
501: p_end--;
502: }
503: p_efake = -1;
504:
505: p_max = hunkmax; /* gets reduced when --- found */
506: if (diff_type == CONTEXT_DIFF || diff_type == NEW_CONTEXT_DIFF) {
1.42 tobias 507: line_beginning = ftello(pfp);
1.17 deraadt 508: repl_beginning = 0;
509: fillcnt = 0;
1.39 ajacouto 510: fillsrc = 0;
1.28 otto 511: ptrn_spaces_eaten = false;
512: repl_could_be_missing = true;
513: repl_missing = false;
1.17 deraadt 514: repl_backtrack_position = 0;
1.39 ajacouto 515: repl_patch_line = 0;
1.17 deraadt 516: ptrn_copiable = 0;
517:
518: ret = pgets(buf, sizeof buf, pfp);
519: p_input_line++;
1.21 otto 520: if (ret == NULL || strnNE(buf, "********", 8)) {
1.17 deraadt 521: next_intuit_at(line_beginning, p_input_line);
1.28 otto 522: return false;
1.17 deraadt 523: }
524: p_context = 100;
525: p_hunk_beg = p_input_line + 1;
526: while (p_end < p_max) {
1.42 tobias 527: line_beginning = ftello(pfp);
1.17 deraadt 528: ret = pgets(buf, sizeof buf, pfp);
529: p_input_line++;
1.21 otto 530: if (ret == NULL) {
1.17 deraadt 531: if (p_max - p_end < 4) {
532: /* assume blank lines got chopped */
533: strlcpy(buf, " \n", sizeof buf);
534: } else {
535: if (repl_beginning && repl_could_be_missing) {
1.28 otto 536: repl_missing = true;
1.17 deraadt 537: goto hunk_done;
538: }
539: fatal("unexpected end of file in patch\n");
540: }
541: }
542: p_end++;
1.28 otto 543: if (p_end >= hunkmax)
544: fatal("Internal error: hunk larger than hunk "
1.29 deraadt 545: "buffer size");
1.17 deraadt 546: p_char[p_end] = *buf;
1.21 otto 547: p_line[p_end] = NULL;
1.17 deraadt 548: switch (*buf) {
549: case '*':
550: if (strnEQ(buf, "********", 8)) {
551: if (repl_beginning && repl_could_be_missing) {
1.28 otto 552: repl_missing = true;
1.17 deraadt 553: goto hunk_done;
554: } else
555: fatal("unexpected end of hunk "
556: "at line %ld\n",
557: p_input_line);
558: }
559: if (p_end != 0) {
560: if (repl_beginning && repl_could_be_missing) {
1.28 otto 561: repl_missing = true;
1.17 deraadt 562: goto hunk_done;
563: }
564: fatal("unexpected *** at line %ld: %s",
565: p_input_line, buf);
566: }
567: context = 0;
568: p_line[p_end] = savestr(buf);
569: if (out_of_mem) {
570: p_end--;
1.28 otto 571: return false;
1.17 deraadt 572: }
1.41 deraadt 573: for (s = buf;
574: *s && !isdigit((unsigned char)*s); s++)
1.17 deraadt 575: ;
576: if (!*s)
577: malformed();
578: if (strnEQ(s, "0,0", 3))
579: memmove(s, s + 2, strlen(s + 2) + 1);
580: p_first = (LINENUM) atol(s);
1.41 deraadt 581: while (isdigit((unsigned char)*s))
1.17 deraadt 582: s++;
583: if (*s == ',') {
1.41 deraadt 584: for (; *s && !isdigit((unsigned char)*s); s++)
1.18 deraadt 585: ;
1.17 deraadt 586: if (!*s)
587: malformed();
588: p_ptrn_lines = ((LINENUM) atol(s)) - p_first + 1;
589: } else if (p_first)
590: p_ptrn_lines = 1;
591: else {
592: p_ptrn_lines = 0;
593: p_first = 1;
594: }
1.26 deraadt 595:
596: /* we need this much at least */
597: p_max = p_ptrn_lines + 6;
1.17 deraadt 598: while (p_max >= hunkmax)
599: grow_hunkmax();
600: p_max = hunkmax;
601: break;
602: case '-':
603: if (buf[1] == '-') {
604: if (repl_beginning ||
605: (p_end != p_ptrn_lines + 1 +
606: (p_char[p_end - 1] == '\n'))) {
607: if (p_end == 1) {
608: /*
609: * `old' lines were omitted;
610: * set up to fill them in
611: * from 'new' context lines.
612: */
613: p_end = p_ptrn_lines + 1;
614: fillsrc = p_end + 1;
615: filldst = 1;
616: fillcnt = p_ptrn_lines;
617: } else {
618: if (repl_beginning) {
619: if (repl_could_be_missing) {
1.28 otto 620: repl_missing = true;
1.17 deraadt 621: goto hunk_done;
622: }
623: fatal("duplicate \"---\" at line %ld--check line numbers at line %ld\n",
624: p_input_line, p_hunk_beg + repl_beginning);
625: } else {
626: fatal("%s \"---\" at line %ld--check line numbers at line %ld\n",
627: (p_end <= p_ptrn_lines
628: ? "Premature"
629: : "Overdue"),
630: p_input_line, p_hunk_beg);
631: }
632: }
633: }
634: repl_beginning = p_end;
1.42 tobias 635: repl_backtrack_position = ftello(pfp);
1.17 deraadt 636: repl_patch_line = p_input_line;
637: p_line[p_end] = savestr(buf);
638: if (out_of_mem) {
639: p_end--;
1.28 otto 640: return false;
1.17 deraadt 641: }
642: p_char[p_end] = '=';
1.41 deraadt 643: for (s = buf;
644: *s && !isdigit((unsigned char)*s); s++)
1.17 deraadt 645: ;
646: if (!*s)
647: malformed();
648: p_newfirst = (LINENUM) atol(s);
1.41 deraadt 649: while (isdigit((unsigned char)*s))
1.17 deraadt 650: s++;
651: if (*s == ',') {
1.41 deraadt 652: for (; *s && !isdigit((unsigned char)*s); s++)
1.17 deraadt 653: ;
654: if (!*s)
655: malformed();
656: p_repl_lines = ((LINENUM) atol(s)) -
657: p_newfirst + 1;
658: } else if (p_newfirst)
659: p_repl_lines = 1;
660: else {
661: p_repl_lines = 0;
662: p_newfirst = 1;
663: }
664: p_max = p_repl_lines + p_end;
665: if (p_max > MAXHUNKSIZE)
666: fatal("hunk too large (%ld lines) at line %ld: %s",
667: p_max, p_input_line, buf);
668: while (p_max >= hunkmax)
669: grow_hunkmax();
670: if (p_repl_lines != ptrn_copiable &&
671: (p_context != 0 || p_repl_lines != 1))
1.28 otto 672: repl_could_be_missing = false;
1.17 deraadt 673: break;
674: }
675: goto change_line;
676: case '+':
677: case '!':
1.28 otto 678: repl_could_be_missing = false;
1.17 deraadt 679: change_line:
680: if (buf[1] == '\n' && canonicalize)
681: strlcpy(buf + 1, " \n", sizeof buf - 1);
1.41 deraadt 682: if (!isspace((unsigned char)buf[1]) &&
683: buf[1] != '>' && buf[1] != '<' &&
1.17 deraadt 684: repl_beginning && repl_could_be_missing) {
1.28 otto 685: repl_missing = true;
1.17 deraadt 686: goto hunk_done;
687: }
688: if (context >= 0) {
689: if (context < p_context)
690: p_context = context;
691: context = -1000;
692: }
693: p_line[p_end] = savestr(buf + 2);
694: if (out_of_mem) {
695: p_end--;
1.28 otto 696: return false;
1.17 deraadt 697: }
698: if (p_end == p_ptrn_lines) {
699: if (remove_special_line()) {
700: int len;
701:
702: len = strlen(p_line[p_end]) - 1;
703: (p_line[p_end])[len] = 0;
704: }
705: }
706: break;
707: case '\t':
708: case '\n': /* assume the 2 spaces got eaten */
709: if (repl_beginning && repl_could_be_missing &&
710: (!ptrn_spaces_eaten ||
711: diff_type == NEW_CONTEXT_DIFF)) {
1.28 otto 712: repl_missing = true;
1.17 deraadt 713: goto hunk_done;
714: }
715: p_line[p_end] = savestr(buf);
716: if (out_of_mem) {
717: p_end--;
1.28 otto 718: return false;
1.17 deraadt 719: }
720: if (p_end != p_ptrn_lines + 1) {
721: ptrn_spaces_eaten |= (repl_beginning != 0);
722: context++;
723: if (!repl_beginning)
724: ptrn_copiable++;
725: p_char[p_end] = ' ';
726: }
727: break;
728: case ' ':
1.41 deraadt 729: if (!isspace((unsigned char)buf[1]) &&
1.23 deraadt 730: repl_beginning && repl_could_be_missing) {
1.28 otto 731: repl_missing = true;
1.17 deraadt 732: goto hunk_done;
733: }
734: context++;
735: if (!repl_beginning)
736: ptrn_copiable++;
737: p_line[p_end] = savestr(buf + 2);
738: if (out_of_mem) {
739: p_end--;
1.28 otto 740: return false;
1.17 deraadt 741: }
742: break;
743: default:
744: if (repl_beginning && repl_could_be_missing) {
1.28 otto 745: repl_missing = true;
1.17 deraadt 746: goto hunk_done;
747: }
748: malformed();
749: }
750: /* set up p_len for strncmp() so we don't have to */
751: /* assume null termination */
752: if (p_line[p_end])
753: p_len[p_end] = strlen(p_line[p_end]);
754: else
755: p_len[p_end] = 0;
756: }
757:
758: hunk_done:
759: if (p_end >= 0 && !repl_beginning)
760: fatal("no --- found in patch at line %ld\n", pch_hunk_beg());
761:
762: if (repl_missing) {
763:
764: /* reset state back to just after --- */
765: p_input_line = repl_patch_line;
766: for (p_end--; p_end > repl_beginning; p_end--)
767: free(p_line[p_end]);
1.42 tobias 768: fseeko(pfp, repl_backtrack_position, SEEK_SET);
1.17 deraadt 769:
770: /* redundant 'new' context lines were omitted - set */
771: /* up to fill them in from the old file context */
772: if (!p_context && p_repl_lines == 1) {
773: p_repl_lines = 0;
774: p_max--;
775: }
776: fillsrc = 1;
777: filldst = repl_beginning + 1;
778: fillcnt = p_repl_lines;
779: p_end = p_max;
780: } else if (!p_context && fillcnt == 1) {
781: /* the first hunk was a null hunk with no context */
782: /* and we were expecting one line -- fix it up. */
783: while (filldst < p_end) {
784: p_line[filldst] = p_line[filldst + 1];
785: p_char[filldst] = p_char[filldst + 1];
786: p_len[filldst] = p_len[filldst + 1];
787: filldst++;
788: }
789: #if 0
1.26 deraadt 790: repl_beginning--; /* this doesn't need to be fixed */
1.17 deraadt 791: #endif
792: p_end--;
793: p_first++; /* do append rather than insert */
794: fillcnt = 0;
795: p_ptrn_lines = 0;
796: }
797: if (diff_type == CONTEXT_DIFF &&
798: (fillcnt || (p_first > 1 && ptrn_copiable > 2 * p_context))) {
799: if (verbose)
800: say("%s\n%s\n%s\n",
801: "(Fascinating--this is really a new-style context diff but without",
802: "the telltale extra asterisks on the *** line that usually indicate",
803: "the new style...)");
804: diff_type = NEW_CONTEXT_DIFF;
805: }
806: /* if there were omitted context lines, fill them in now */
807: if (fillcnt) {
1.26 deraadt 808: p_bfake = filldst; /* remember where not to free() */
1.17 deraadt 809: p_efake = filldst + fillcnt - 1;
810: while (fillcnt-- > 0) {
811: while (fillsrc <= p_end && p_char[fillsrc] != ' ')
812: fillsrc++;
813: if (fillsrc > p_end)
814: fatal("replacement text or line numbers mangled in hunk at line %ld\n",
815: p_hunk_beg);
816: p_line[filldst] = p_line[fillsrc];
817: p_char[filldst] = p_char[fillsrc];
818: p_len[filldst] = p_len[fillsrc];
819: fillsrc++;
820: filldst++;
821: }
822: while (fillsrc <= p_end && fillsrc != repl_beginning &&
823: p_char[fillsrc] != ' ')
824: fillsrc++;
825: #ifdef DEBUGGING
826: if (debug & 64)
827: printf("fillsrc %ld, filldst %ld, rb %ld, e+1 %ld\n",
828: fillsrc, filldst, repl_beginning, p_end + 1);
1.1 deraadt 829: #endif
1.28 otto 830: if (fillsrc != p_end + 1 && fillsrc != repl_beginning)
831: malformed();
832: if (filldst != p_end + 1 && filldst != repl_beginning)
833: malformed();
1.1 deraadt 834: }
1.17 deraadt 835: if (p_line[p_end] != NULL) {
836: if (remove_special_line()) {
837: p_len[p_end] -= 1;
838: (p_line[p_end])[p_len[p_end]] = 0;
839: }
840: }
841: } else if (diff_type == UNI_DIFF) {
1.42 tobias 842: off_t line_beginning = ftello(pfp); /* file pos of the current line */
1.17 deraadt 843: LINENUM fillsrc; /* index of old lines */
844: LINENUM filldst; /* index of new lines */
845: char ch;
846:
847: ret = pgets(buf, sizeof buf, pfp);
848: p_input_line++;
1.21 otto 849: if (ret == NULL || strnNE(buf, "@@ -", 4)) {
1.17 deraadt 850: next_intuit_at(line_beginning, p_input_line);
1.28 otto 851: return false;
1.1 deraadt 852: }
1.17 deraadt 853: s = buf + 4;
1.1 deraadt 854: if (!*s)
1.17 deraadt 855: malformed();
1.1 deraadt 856: p_first = (LINENUM) atol(s);
1.41 deraadt 857: while (isdigit((unsigned char)*s))
1.17 deraadt 858: s++;
859: if (*s == ',') {
860: p_ptrn_lines = (LINENUM) atol(++s);
1.41 deraadt 861: while (isdigit((unsigned char)*s))
1.17 deraadt 862: s++;
863: } else
864: p_ptrn_lines = 1;
865: if (*s == ' ')
866: s++;
867: if (*s != '+' || !*++s)
868: malformed();
869: p_newfirst = (LINENUM) atol(s);
1.41 deraadt 870: while (isdigit((unsigned char)*s))
1.17 deraadt 871: s++;
1.1 deraadt 872: if (*s == ',') {
1.17 deraadt 873: p_repl_lines = (LINENUM) atol(++s);
1.41 deraadt 874: while (isdigit((unsigned char)*s))
1.17 deraadt 875: s++;
876: } else
877: p_repl_lines = 1;
878: if (*s == ' ')
879: s++;
880: if (*s != '@')
881: malformed();
882: if (!p_ptrn_lines)
883: p_first++; /* do append rather than insert */
884: p_max = p_ptrn_lines + p_repl_lines + 1;
885: while (p_max >= hunkmax)
886: grow_hunkmax();
887: fillsrc = 1;
888: filldst = fillsrc + p_ptrn_lines;
889: p_end = filldst + p_repl_lines;
890: snprintf(buf, sizeof buf, "*** %ld,%ld ****\n", p_first,
1.26 deraadt 891: p_first + p_ptrn_lines - 1);
1.17 deraadt 892: p_line[0] = savestr(buf);
893: if (out_of_mem) {
894: p_end = -1;
1.28 otto 895: return false;
1.17 deraadt 896: }
897: p_char[0] = '*';
898: snprintf(buf, sizeof buf, "--- %ld,%ld ----\n", p_newfirst,
1.26 deraadt 899: p_newfirst + p_repl_lines - 1);
1.17 deraadt 900: p_line[filldst] = savestr(buf);
901: if (out_of_mem) {
902: p_end = 0;
1.28 otto 903: return false;
1.1 deraadt 904: }
1.17 deraadt 905: p_char[filldst++] = '=';
906: p_context = 100;
907: context = 0;
908: p_hunk_beg = p_input_line + 1;
909: while (fillsrc <= p_ptrn_lines || filldst <= p_end) {
1.42 tobias 910: line_beginning = ftello(pfp);
1.17 deraadt 911: ret = pgets(buf, sizeof buf, pfp);
912: p_input_line++;
1.21 otto 913: if (ret == NULL) {
1.17 deraadt 914: if (p_max - filldst < 3) {
915: /* assume blank lines got chopped */
916: strlcpy(buf, " \n", sizeof buf);
917: } else {
918: fatal("unexpected end of file in patch\n");
919: }
920: }
921: if (*buf == '\t' || *buf == '\n') {
922: ch = ' '; /* assume the space got eaten */
923: s = savestr(buf);
924: } else {
925: ch = *buf;
926: s = savestr(buf + 1);
927: }
928: if (out_of_mem) {
929: while (--filldst > p_ptrn_lines)
930: free(p_line[filldst]);
931: p_end = fillsrc - 1;
1.28 otto 932: return false;
1.17 deraadt 933: }
934: switch (ch) {
935: case '-':
936: if (fillsrc > p_ptrn_lines) {
937: free(s);
938: p_end = filldst - 1;
939: malformed();
940: }
941: p_char[fillsrc] = ch;
942: p_line[fillsrc] = s;
943: p_len[fillsrc++] = strlen(s);
944: if (fillsrc > p_ptrn_lines) {
945: if (remove_special_line()) {
946: p_len[fillsrc - 1] -= 1;
947: s[p_len[fillsrc - 1]] = 0;
948: }
949: }
950: break;
951: case '=':
952: ch = ' ';
953: /* FALL THROUGH */
954: case ' ':
955: if (fillsrc > p_ptrn_lines) {
956: free(s);
957: while (--filldst > p_ptrn_lines)
958: free(p_line[filldst]);
959: p_end = fillsrc - 1;
960: malformed();
961: }
962: context++;
963: p_char[fillsrc] = ch;
964: p_line[fillsrc] = s;
965: p_len[fillsrc++] = strlen(s);
966: s = savestr(s);
967: if (out_of_mem) {
968: while (--filldst > p_ptrn_lines)
969: free(p_line[filldst]);
970: p_end = fillsrc - 1;
1.28 otto 971: return false;
1.34 otto 972: }
973: if (fillsrc > p_ptrn_lines) {
974: if (remove_special_line()) {
975: p_len[fillsrc - 1] -= 1;
976: s[p_len[fillsrc - 1]] = 0;
977: }
1.17 deraadt 978: }
979: /* FALL THROUGH */
980: case '+':
981: if (filldst > p_end) {
982: free(s);
983: while (--filldst > p_ptrn_lines)
984: free(p_line[filldst]);
985: p_end = fillsrc - 1;
986: malformed();
987: }
988: p_char[filldst] = ch;
989: p_line[filldst] = s;
990: p_len[filldst++] = strlen(s);
991: if (fillsrc > p_ptrn_lines) {
992: if (remove_special_line()) {
993: p_len[filldst - 1] -= 1;
994: s[p_len[filldst - 1]] = 0;
995: }
996: }
997: break;
998: default:
999: p_end = filldst;
1000: malformed();
1.1 deraadt 1001: }
1.17 deraadt 1002: if (ch != ' ' && context > 0) {
1003: if (context < p_context)
1004: p_context = context;
1005: context = -1000;
1.1 deraadt 1006: }
1.17 deraadt 1007: } /* while */
1008: } else { /* normal diff--fake it up */
1009: char hunk_type;
1010: int i;
1011: LINENUM min, max;
1.42 tobias 1012: off_t line_beginning = ftello(pfp);
1.17 deraadt 1013:
1014: p_context = 0;
1015: ret = pgets(buf, sizeof buf, pfp);
1016: p_input_line++;
1.41 deraadt 1017: if (ret == NULL || !isdigit((unsigned char)*buf)) {
1.17 deraadt 1018: next_intuit_at(line_beginning, p_input_line);
1.28 otto 1019: return false;
1.17 deraadt 1020: }
1021: p_first = (LINENUM) atol(buf);
1.41 deraadt 1022: for (s = buf; isdigit((unsigned char)*s); s++)
1.18 deraadt 1023: ;
1.17 deraadt 1024: if (*s == ',') {
1025: p_ptrn_lines = (LINENUM) atol(++s) - p_first + 1;
1.41 deraadt 1026: while (isdigit((unsigned char)*s))
1.17 deraadt 1027: s++;
1028: } else
1029: p_ptrn_lines = (*s != 'a');
1030: hunk_type = *s;
1031: if (hunk_type == 'a')
1032: p_first++; /* do append rather than insert */
1033: min = (LINENUM) atol(++s);
1.41 deraadt 1034: for (; isdigit((unsigned char)*s); s++)
1.18 deraadt 1035: ;
1.17 deraadt 1036: if (*s == ',')
1037: max = (LINENUM) atol(++s);
1038: else
1039: max = min;
1040: if (hunk_type == 'd')
1041: min++;
1042: p_end = p_ptrn_lines + 1 + max - min + 1;
1043: if (p_end > MAXHUNKSIZE)
1.16 deraadt 1044: fatal("hunk too large (%ld lines) at line %ld: %s",
1.17 deraadt 1045: p_end, p_input_line, buf);
1046: while (p_end >= hunkmax)
1.1 deraadt 1047: grow_hunkmax();
1.17 deraadt 1048: p_newfirst = min;
1049: p_repl_lines = max - min + 1;
1050: snprintf(buf, sizeof buf, "*** %ld,%ld\n", p_first,
1.26 deraadt 1051: p_first + p_ptrn_lines - 1);
1.17 deraadt 1052: p_line[0] = savestr(buf);
1.1 deraadt 1053: if (out_of_mem) {
1.17 deraadt 1054: p_end = -1;
1.28 otto 1055: return false;
1.1 deraadt 1056: }
1.17 deraadt 1057: p_char[0] = '*';
1058: for (i = 1; i <= p_ptrn_lines; i++) {
1059: ret = pgets(buf, sizeof buf, pfp);
1060: p_input_line++;
1.21 otto 1061: if (ret == NULL)
1.17 deraadt 1062: fatal("unexpected end of file in patch at line %ld\n",
1063: p_input_line);
1064: if (*buf != '<')
1065: fatal("< expected at line %ld of patch\n",
1066: p_input_line);
1067: p_line[i] = savestr(buf + 2);
1068: if (out_of_mem) {
1069: p_end = i - 1;
1.28 otto 1070: return false;
1.14 otto 1071: }
1.17 deraadt 1072: p_len[i] = strlen(p_line[i]);
1073: p_char[i] = '-';
1.14 otto 1074: }
1.17 deraadt 1075:
1076: if (remove_special_line()) {
1077: p_len[i - 1] -= 1;
1078: (p_line[i - 1])[p_len[i - 1]] = 0;
1.1 deraadt 1079: }
1.17 deraadt 1080: if (hunk_type == 'c') {
1081: ret = pgets(buf, sizeof buf, pfp);
1082: p_input_line++;
1.21 otto 1083: if (ret == NULL)
1.17 deraadt 1084: fatal("unexpected end of file in patch at line %ld\n",
1085: p_input_line);
1086: if (*buf != '-')
1087: fatal("--- expected at line %ld of patch\n",
1088: p_input_line);
1.1 deraadt 1089: }
1.17 deraadt 1090: snprintf(buf, sizeof(buf), "--- %ld,%ld\n", min, max);
1091: p_line[i] = savestr(buf);
1.1 deraadt 1092: if (out_of_mem) {
1.17 deraadt 1093: p_end = i - 1;
1.28 otto 1094: return false;
1.1 deraadt 1095: }
1.17 deraadt 1096: p_char[i] = '=';
1097: for (i++; i <= p_end; i++) {
1098: ret = pgets(buf, sizeof buf, pfp);
1099: p_input_line++;
1.21 otto 1100: if (ret == NULL)
1.17 deraadt 1101: fatal("unexpected end of file in patch at line %ld\n",
1102: p_input_line);
1103: if (*buf != '>')
1104: fatal("> expected at line %ld of patch\n",
1105: p_input_line);
1106: p_line[i] = savestr(buf + 2);
1107: if (out_of_mem) {
1108: p_end = i - 1;
1.28 otto 1109: return false;
1.14 otto 1110: }
1.17 deraadt 1111: p_len[i] = strlen(p_line[i]);
1112: p_char[i] = '+';
1.14 otto 1113: }
1.17 deraadt 1114:
1115: if (remove_special_line()) {
1116: p_len[i - 1] -= 1;
1117: (p_line[i - 1])[p_len[i - 1]] = 0;
1.14 otto 1118: }
1.1 deraadt 1119: }
1.17 deraadt 1120: if (reverse) /* backwards patch? */
1121: if (!pch_swap())
1122: say("Not enough memory to swap next hunk!\n");
1.1 deraadt 1123: #ifdef DEBUGGING
1.17 deraadt 1124: if (debug & 2) {
1125: int i;
1126: char special;
1127:
1128: for (i = 0; i <= p_end; i++) {
1129: if (i == p_ptrn_lines)
1130: special = '^';
1131: else
1132: special = ' ';
1133: fprintf(stderr, "%3d %c %c %s", i, p_char[i],
1134: special, p_line[i]);
1135: fflush(stderr);
1136: }
1.1 deraadt 1137: }
1138: #endif
1.17 deraadt 1139: if (p_end + 1 < hunkmax)/* paranoia reigns supreme... */
1140: p_char[p_end + 1] = '^'; /* add a stopper for apply_hunk */
1.28 otto 1141: return true;
1.1 deraadt 1142: }
1143:
1.17 deraadt 1144: /*
1145: * Input a line from the patch file, worrying about indentation.
1146: */
1.21 otto 1147: static char *
1.17 deraadt 1148: pgets(char *bf, int sz, FILE *fp)
1149: {
1150: char *s, *ret = fgets(bf, sz, fp);
1151: int indent = 0;
1.1 deraadt 1152:
1.21 otto 1153: if (p_indent && ret != NULL) {
1.17 deraadt 1154: for (s = buf;
1155: indent < p_indent && (*s == ' ' || *s == '\t' || *s == 'X');
1156: s++) {
1157: if (*s == '\t')
1158: indent += 8 - (indent % 7);
1159: else
1160: indent++;
1161: }
1162: if (buf != s && strlcpy(buf, s, sizeof(buf)) >= sizeof(buf))
1163: fatal("buffer too small in pgets()\n");
1164: }
1165: return ret;
1.1 deraadt 1166: }
1167:
1.17 deraadt 1168: /*
1169: * Reverse the old and new portions of the current hunk.
1170: */
1.1 deraadt 1171: bool
1.17 deraadt 1172: pch_swap(void)
1.1 deraadt 1173: {
1.17 deraadt 1174: char **tp_line; /* the text of the hunk */
1175: short *tp_len; /* length of each line */
1176: char *tp_char; /* +, -, and ! */
1177: LINENUM i;
1178: LINENUM n;
1.28 otto 1179: bool blankline = false;
1.17 deraadt 1180: char *s;
1181:
1182: i = p_first;
1183: p_first = p_newfirst;
1184: p_newfirst = i;
1185:
1186: /* make a scratch copy */
1187:
1188: tp_line = p_line;
1189: tp_len = p_len;
1190: tp_char = p_char;
1.21 otto 1191: p_line = NULL; /* force set_hunkmax to allocate again */
1192: p_len = NULL;
1193: p_char = NULL;
1.17 deraadt 1194: set_hunkmax();
1.28 otto 1195: if (p_line == NULL || p_len == NULL || p_char == NULL) {
1.21 otto 1196:
1.28 otto 1197: free(p_line);
1.17 deraadt 1198: p_line = tp_line;
1.28 otto 1199: free(p_len);
1.17 deraadt 1200: p_len = tp_len;
1.28 otto 1201: free(p_char);
1.17 deraadt 1202: p_char = tp_char;
1.28 otto 1203: return false; /* not enough memory to swap hunk! */
1.17 deraadt 1204: }
1205: /* now turn the new into the old */
1206:
1.1 deraadt 1207: i = p_ptrn_lines + 1;
1.17 deraadt 1208: if (tp_char[i] == '\n') { /* account for possible blank line */
1.28 otto 1209: blankline = true;
1.17 deraadt 1210: i++;
1211: }
1212: if (p_efake >= 0) { /* fix non-freeable ptr range */
1213: if (p_efake <= i)
1214: n = p_end - i + 1;
1215: else
1216: n = -i;
1217: p_efake += n;
1218: p_bfake += n;
1219: }
1220: for (n = 0; i <= p_end; i++, n++) {
1221: p_line[n] = tp_line[i];
1222: p_char[n] = tp_char[i];
1223: if (p_char[n] == '+')
1224: p_char[n] = '-';
1225: p_len[n] = tp_len[i];
1226: }
1227: if (blankline) {
1228: i = p_ptrn_lines + 1;
1229: p_line[n] = tp_line[i];
1230: p_char[n] = tp_char[i];
1231: p_len[n] = tp_len[i];
1232: n++;
1233: }
1.28 otto 1234: if (p_char[0] != '=')
1.29 deraadt 1235: fatal("Malformed patch at line %ld: expected '=' found '%c'\n",
1.28 otto 1236: p_input_line, p_char[0]);
1.17 deraadt 1237: p_char[0] = '*';
1238: for (s = p_line[0]; *s; s++)
1239: if (*s == '-')
1240: *s = '*';
1241:
1242: /* now turn the old into the new */
1243:
1.28 otto 1244: if (p_char[0] != '*')
1.29 deraadt 1245: fatal("Malformed patch at line %ld: expected '*' found '%c'\n",
1246: p_input_line, p_char[0]);
1.17 deraadt 1247: tp_char[0] = '=';
1248: for (s = tp_line[0]; *s; s++)
1249: if (*s == '*')
1250: *s = '-';
1251: for (i = 0; n <= p_end; i++, n++) {
1252: p_line[n] = tp_line[i];
1253: p_char[n] = tp_char[i];
1254: if (p_char[n] == '-')
1255: p_char[n] = '+';
1256: p_len[n] = tp_len[i];
1257: }
1.28 otto 1258:
1259: if (i != p_ptrn_lines + 1)
1260: fatal("Malformed patch at line %ld: expected %ld lines, "
1.29 deraadt 1261: "got %ld\n",
1262: p_input_line, p_ptrn_lines + 1, i);
1.28 otto 1263:
1.17 deraadt 1264: i = p_ptrn_lines;
1265: p_ptrn_lines = p_repl_lines;
1266: p_repl_lines = i;
1.21 otto 1267:
1.28 otto 1268: free(tp_line);
1269: free(tp_len);
1270: free(tp_char);
1271:
1272: return true;
1.1 deraadt 1273: }
1274:
1.17 deraadt 1275: /*
1276: * Return the specified line position in the old file of the old context.
1277: */
1.1 deraadt 1278: LINENUM
1.17 deraadt 1279: pch_first(void)
1.1 deraadt 1280: {
1.17 deraadt 1281: return p_first;
1.1 deraadt 1282: }
1283:
1.17 deraadt 1284: /*
1285: * Return the number of lines of old context.
1286: */
1.1 deraadt 1287: LINENUM
1.17 deraadt 1288: pch_ptrn_lines(void)
1.1 deraadt 1289: {
1.17 deraadt 1290: return p_ptrn_lines;
1.1 deraadt 1291: }
1292:
1.17 deraadt 1293: /*
1294: * Return the probable line position in the new file of the first line.
1295: */
1.1 deraadt 1296: LINENUM
1.17 deraadt 1297: pch_newfirst(void)
1.1 deraadt 1298: {
1.17 deraadt 1299: return p_newfirst;
1.1 deraadt 1300: }
1301:
1.17 deraadt 1302: /*
1303: * Return the number of lines in the replacement text including context.
1304: */
1.1 deraadt 1305: LINENUM
1.17 deraadt 1306: pch_repl_lines(void)
1.1 deraadt 1307: {
1.17 deraadt 1308: return p_repl_lines;
1.1 deraadt 1309: }
1310:
1.17 deraadt 1311: /*
1312: * Return the number of lines in the whole hunk.
1313: */
1.1 deraadt 1314: LINENUM
1.17 deraadt 1315: pch_end(void)
1.1 deraadt 1316: {
1.17 deraadt 1317: return p_end;
1.1 deraadt 1318: }
1319:
1.17 deraadt 1320: /*
1321: * Return the number of context lines before the first changed line.
1322: */
1.1 deraadt 1323: LINENUM
1.17 deraadt 1324: pch_context(void)
1.1 deraadt 1325: {
1.17 deraadt 1326: return p_context;
1.1 deraadt 1327: }
1328:
1.17 deraadt 1329: /*
1330: * Return the length of a particular patch line.
1331: */
1.1 deraadt 1332: short
1.17 deraadt 1333: pch_line_len(LINENUM line)
1.1 deraadt 1334: {
1.17 deraadt 1335: return p_len[line];
1.1 deraadt 1336: }
1337:
1.17 deraadt 1338: /*
1339: * Return the control character (+, -, *, !, etc) for a patch line.
1340: */
1.1 deraadt 1341: char
1.17 deraadt 1342: pch_char(LINENUM line)
1.1 deraadt 1343: {
1.17 deraadt 1344: return p_char[line];
1.1 deraadt 1345: }
1346:
1.17 deraadt 1347: /*
1348: * Return a pointer to a particular patch line.
1349: */
1.1 deraadt 1350: char *
1.17 deraadt 1351: pfetch(LINENUM line)
1.1 deraadt 1352: {
1.17 deraadt 1353: return p_line[line];
1.1 deraadt 1354: }
1355:
1.17 deraadt 1356: /*
1357: * Return where in the patch file this hunk began, for error messages.
1358: */
1.1 deraadt 1359: LINENUM
1.17 deraadt 1360: pch_hunk_beg(void)
1.1 deraadt 1361: {
1.17 deraadt 1362: return p_hunk_beg;
1.1 deraadt 1363: }
1364:
1.17 deraadt 1365: /*
1366: * Apply an ed script by feeding ed itself.
1367: */
1.1 deraadt 1368: void
1.17 deraadt 1369: do_ed_script(void)
1.1 deraadt 1370: {
1.17 deraadt 1371: char *t;
1.42 tobias 1372: off_t beginning_of_this_line;
1.36 otto 1373: FILE *pipefp = NULL;
1.17 deraadt 1374:
1375: if (!skip_rest_of_patch) {
1.24 millert 1376: if (copy_file(filearg[0], TMPOUTNAME) < 0) {
1377: unlink(TMPOUTNAME);
1378: fatal("can't create temp file %s", TMPOUTNAME);
1379: }
1.27 millert 1380: snprintf(buf, sizeof buf, "%s%s%s", _PATH_ED,
1381: verbose ? " " : " -s ", TMPOUTNAME);
1.17 deraadt 1382: pipefp = popen(buf, "w");
1383: }
1384: for (;;) {
1.42 tobias 1385: beginning_of_this_line = ftello(pfp);
1.21 otto 1386: if (pgets(buf, sizeof buf, pfp) == NULL) {
1.17 deraadt 1387: next_intuit_at(beginning_of_this_line, p_input_line);
1388: break;
1389: }
1390: p_input_line++;
1.41 deraadt 1391: for (t = buf; isdigit((unsigned char)*t) || *t == ','; t++)
1.18 deraadt 1392: ;
1.22 millert 1393: /* POSIX defines allowed commands as {a,c,d,i,s} */
1.41 deraadt 1394: if (isdigit((unsigned char)*buf) &&
1395: (*t == 'a' || *t == 'c' || *t == 'd' || *t == 'i' || *t == 's')) {
1.28 otto 1396: if (pipefp != NULL)
1.17 deraadt 1397: fputs(buf, pipefp);
1398: if (*t != 'd') {
1.21 otto 1399: while (pgets(buf, sizeof buf, pfp) != NULL) {
1.17 deraadt 1400: p_input_line++;
1.28 otto 1401: if (pipefp != NULL)
1.17 deraadt 1402: fputs(buf, pipefp);
1403: if (strEQ(buf, ".\n"))
1404: break;
1405: }
1406: }
1407: } else {
1408: next_intuit_at(beginning_of_this_line, p_input_line);
1.1 deraadt 1409: break;
1410: }
1411: }
1.28 otto 1412: if (pipefp == NULL)
1.17 deraadt 1413: return;
1414: fprintf(pipefp, "w\n");
1415: fprintf(pipefp, "q\n");
1416: fflush(pipefp);
1417: pclose(pipefp);
1418: ignore_signals();
1419: if (!check_only) {
1420: if (move_file(TMPOUTNAME, outname) < 0) {
1.28 otto 1421: toutkeep = true;
1.17 deraadt 1422: chmod(TMPOUTNAME, filemode);
1423: } else
1424: chmod(outname, filemode);
1.1 deraadt 1425: }
1.17 deraadt 1426: set_signals(1);
1.32 millert 1427: }
1428:
1429: /*
1430: * Choose the name of the file to be patched based on POSIX rules.
1431: * NOTE: the POSIX rules are amazingly stupid and we only follow them
1432: * if the user specified --posix or set POSIXLY_CORRECT.
1433: */
1434: static char *
1435: posix_name(const struct file_name *names, bool assume_exists)
1436: {
1437: char *path = NULL;
1438: int i;
1439:
1440: /*
1441: * POSIX states that the filename will be chosen from one
1442: * of the old, new and index names (in that order) if
1443: * the file exists relative to CWD after -p stripping.
1444: */
1445: for (i = 0; i < MAX_FILE; i++) {
1446: if (names[i].path != NULL && names[i].exists) {
1447: path = names[i].path;
1448: break;
1449: }
1450: }
1451: if (path == NULL && !assume_exists) {
1452: /*
1453: * No files found, look for something we can checkout from
1454: * RCS/SCCS dirs. Same order as above.
1455: */
1456: for (i = 0; i < MAX_FILE; i++) {
1457: if (names[i].path != NULL &&
1458: (path = checked_in(names[i].path)) != NULL)
1459: break;
1460: }
1461: /*
1462: * Still no match? Check to see if the diff could be creating
1463: * a new file.
1464: */
1465: if (path == NULL && ok_to_create_file &&
1466: names[NEW_FILE].path != NULL)
1467: path = names[NEW_FILE].path;
1468: }
1469:
1470: return path ? savestr(path) : NULL;
1471: }
1472:
1473: static char *
1.40 otto 1474: compare_names(const struct file_name *names, bool assume_exists, int phase)
1.32 millert 1475: {
1476: size_t min_components, min_baselen, min_len, tmp;
1477: char *best = NULL;
1.40 otto 1478: char *path;
1.32 millert 1479: int i;
1480:
1481: /*
1482: * The "best" name is the one with the fewest number of path
1483: * components, the shortest basename length, and the shortest
1484: * overall length (in that order). We only use the Index: file
1485: * if neither of the old or new files could be intuited from
1486: * the diff header.
1487: */
1488: min_components = min_baselen = min_len = SIZE_MAX;
1489: for (i = INDEX_FILE; i >= OLD_FILE; i--) {
1.40 otto 1490: path = names[i].path;
1491: if (path == NULL ||
1492: (phase == 1 && !names[i].exists && !assume_exists) ||
1493: (phase == 2 && checked_in(path) == NULL))
1.32 millert 1494: continue;
1.40 otto 1495: if ((tmp = num_components(path)) > min_components)
1.32 millert 1496: continue;
1.40 otto 1497: if (tmp < min_components) {
1498: min_components = tmp;
1499: best = path;
1500: }
1501: if ((tmp = strlen(basename(path))) > min_baselen)
1.32 millert 1502: continue;
1.40 otto 1503: if (tmp < min_baselen) {
1504: min_baselen = tmp;
1505: best = path;
1506: }
1507: if ((tmp = strlen(path)) > min_len)
1.32 millert 1508: continue;
1509: min_len = tmp;
1.40 otto 1510: best = path;
1.32 millert 1511: }
1.40 otto 1512: return best;
1513: }
1514:
1515: /*
1516: * Choose the name of the file to be patched based the "best" one
1517: * available.
1518: */
1519: static char *
1520: best_name(const struct file_name *names, bool assume_exists)
1521: {
1522: char *best;
1523:
1524: best = compare_names(names, assume_exists, 1);
1.32 millert 1525: if (best == NULL) {
1.40 otto 1526: best = compare_names(names, assume_exists, 2);
1.32 millert 1527: /*
1528: * Still no match? Check to see if the diff could be creating
1529: * a new file.
1530: */
1531: if (best == NULL && ok_to_create_file &&
1532: names[NEW_FILE].path != NULL)
1533: best = names[NEW_FILE].path;
1534: }
1535: return best ? savestr(best) : NULL;
1536: }
1537:
1538: static size_t
1539: num_components(const char *path)
1540: {
1541: size_t n;
1542: const char *cp;
1543:
1544: for (n = 0, cp = path; (cp = strchr(cp, '/')) != NULL; n++, cp++) {
1545: while (*cp == '/')
1546: cp++; /* skip consecutive slashes */
1547: }
1548: return n;
1.1 deraadt 1549: }