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