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