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