pch.c revision 1.32 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.32 cjep * $NetBSD: pch.c,v 1.32 2021/05/25 11:25:59 cjep 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.32 cjep __RCSID("$NetBSD: pch.c,v 1.32 2021/05/25 11:25:59 cjep 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.32 cjep static int pgetline(char **, size_t *, 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.32 cjep while (getline(&buf, &bufsz, stdin) != -1)
115 1.32 cjep fprintf(pfp, "%s", buf);
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.30 christos new_p_line = pch_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.30 christos new_p_len = pch_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.30 christos new_p_char = pch_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.32 cjep if (getline(&buf, &bufsz, pfp) == -1) {
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.32 cjep int 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.32 cjep ret = getline(&buf, &bufsz, pfp);
440 1.32 cjep if (ret == -1)
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.24 joerg __dead 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.26 christos static LINENUM
459 1.26 christos getlinenum(const char *s)
460 1.26 christos {
461 1.26 christos LINENUM l = (LINENUM)atol(s);
462 1.26 christos if (l < 0) {
463 1.26 christos l = 0;
464 1.26 christos malformed();
465 1.26 christos }
466 1.26 christos return l;
467 1.26 christos }
468 1.26 christos
469 1.26 christos static LINENUM
470 1.26 christos getskiplinenum(char **p)
471 1.26 christos {
472 1.26 christos char *s = *p;
473 1.26 christos LINENUM l = getlinenum(s);
474 1.26 christos while (isdigit((unsigned char)*s))
475 1.26 christos s++;
476 1.26 christos *p = s;
477 1.26 christos return l;
478 1.26 christos }
479 1.26 christos
480 1.10 kristerw /*
481 1.25 wiz * True if the line has been discarded (i.e., it is a line saying
482 1.13 kristerw * "\ No newline at end of file".)
483 1.13 kristerw */
484 1.13 kristerw static bool
485 1.13 kristerw remove_special_line(void)
486 1.13 kristerw {
487 1.23 joerg int c;
488 1.13 kristerw
489 1.13 kristerw c = fgetc(pfp);
490 1.13 kristerw if (c == '\\') {
491 1.13 kristerw do {
492 1.13 kristerw c = fgetc(pfp);
493 1.13 kristerw } while (c != EOF && c != '\n');
494 1.13 kristerw
495 1.23 joerg return true;
496 1.13 kristerw }
497 1.13 kristerw if (c != EOF)
498 1.15 kristerw fseek(pfp, -1L, SEEK_CUR);
499 1.13 kristerw
500 1.23 joerg return false;
501 1.13 kristerw }
502 1.13 kristerw
503 1.13 kristerw /*
504 1.10 kristerw * True if there is more of the current diff listing to process.
505 1.10 kristerw */
506 1.1 cgd bool
507 1.8 kristerw another_hunk(void)
508 1.1 cgd {
509 1.23 joerg long line_beginning; /* file pos of the current line */
510 1.23 joerg LINENUM repl_beginning; /* index of --- line */
511 1.23 joerg LINENUM fillcnt; /* #lines of missing ptrn or repl */
512 1.23 joerg LINENUM fillsrc; /* index of first line to copy */
513 1.23 joerg LINENUM filldst; /* index of first missing line */
514 1.23 joerg bool ptrn_spaces_eaten; /* ptrn was slightly misformed */
515 1.23 joerg bool repl_could_be_missing; /* no + or ! lines in this hunk */
516 1.23 joerg bool repl_missing; /* we are now backtracking */
517 1.23 joerg long repl_backtrack_position; /* file pos of first repl line */
518 1.23 joerg LINENUM repl_patch_line; /* input line number for same */
519 1.23 joerg LINENUM ptrn_copiable; /* # of copiable lines in ptrn */
520 1.32 cjep char *s;
521 1.23 joerg int context = 0;
522 1.32 cjep int ret;
523 1.23 joerg
524 1.23 joerg while (p_end >= 0) {
525 1.23 joerg if (p_end == p_efake)
526 1.23 joerg p_end = p_bfake; /* don't free twice */
527 1.23 joerg else
528 1.23 joerg free(p_line[p_end]);
529 1.23 joerg p_end--;
530 1.23 joerg }
531 1.23 joerg p_efake = -1;
532 1.23 joerg
533 1.23 joerg p_max = hunkmax; /* gets reduced when --- found */
534 1.23 joerg if (diff_type == CONTEXT_DIFF || diff_type == NEW_CONTEXT_DIFF) {
535 1.23 joerg line_beginning = ftell(pfp);
536 1.23 joerg repl_beginning = 0;
537 1.23 joerg fillcnt = 0;
538 1.23 joerg fillsrc = 0;
539 1.23 joerg filldst = 0;
540 1.23 joerg ptrn_spaces_eaten = false;
541 1.23 joerg repl_could_be_missing = true;
542 1.23 joerg repl_missing = false;
543 1.23 joerg repl_backtrack_position = 0;
544 1.23 joerg repl_patch_line = 0;
545 1.23 joerg ptrn_copiable = 0;
546 1.23 joerg
547 1.32 cjep ret = pgetline(&buf, &bufsz, pfp);
548 1.23 joerg p_input_line++;
549 1.32 cjep if (ret == -1 || strnNE(buf, "********", 8)) {
550 1.23 joerg next_intuit_at(line_beginning, p_input_line);
551 1.23 joerg return false;
552 1.23 joerg }
553 1.23 joerg p_context = 100;
554 1.23 joerg p_hunk_beg = p_input_line + 1;
555 1.23 joerg while (p_end < p_max) {
556 1.23 joerg line_beginning = ftell(pfp);
557 1.32 cjep ret = pgetline(&buf, &bufsz, pfp);
558 1.23 joerg p_input_line++;
559 1.32 cjep if (ret == -1) {
560 1.31 rhialto if (repl_beginning && repl_could_be_missing) {
561 1.31 rhialto repl_missing = true;
562 1.31 rhialto goto hunk_done;
563 1.23 joerg }
564 1.31 rhialto fatal("unexpected end of file in patch\n");
565 1.23 joerg }
566 1.23 joerg p_end++;
567 1.23 joerg if (p_end >= hunkmax)
568 1.23 joerg fatal("Internal error: hunk larger than hunk "
569 1.23 joerg "buffer size");
570 1.23 joerg p_char[p_end] = *buf;
571 1.23 joerg p_line[p_end] = NULL;
572 1.23 joerg switch (*buf) {
573 1.23 joerg case '*':
574 1.23 joerg if (strnEQ(buf, "********", 8)) {
575 1.23 joerg if (repl_beginning && repl_could_be_missing) {
576 1.23 joerg repl_missing = true;
577 1.23 joerg goto hunk_done;
578 1.23 joerg } else
579 1.23 joerg fatal("unexpected end of hunk "
580 1.23 joerg "at line %ld\n",
581 1.23 joerg p_input_line);
582 1.23 joerg }
583 1.23 joerg if (p_end != 0) {
584 1.23 joerg if (repl_beginning && repl_could_be_missing) {
585 1.23 joerg repl_missing = true;
586 1.23 joerg goto hunk_done;
587 1.23 joerg }
588 1.23 joerg fatal("unexpected *** at line %ld: %s",
589 1.23 joerg p_input_line, buf);
590 1.23 joerg }
591 1.23 joerg context = 0;
592 1.23 joerg p_line[p_end] = savestr(buf);
593 1.23 joerg if (out_of_mem) {
594 1.23 joerg p_end--;
595 1.23 joerg return false;
596 1.23 joerg }
597 1.23 joerg for (s = buf; *s && !isdigit((unsigned char)*s); s++)
598 1.23 joerg ;
599 1.23 joerg if (!*s)
600 1.23 joerg malformed();
601 1.23 joerg if (strnEQ(s, "0,0", 3))
602 1.23 joerg memmove(s, s + 2, strlen(s + 2) + 1);
603 1.26 christos p_first = getskiplinenum(&s);
604 1.23 joerg if (*s == ',') {
605 1.23 joerg for (; *s && !isdigit((unsigned char)*s); s++)
606 1.23 joerg ;
607 1.23 joerg if (!*s)
608 1.23 joerg malformed();
609 1.26 christos p_ptrn_lines = (getlinenum(s)) - p_first + 1;
610 1.26 christos if (p_ptrn_lines < 0)
611 1.26 christos malformed();
612 1.23 joerg } else if (p_first)
613 1.23 joerg p_ptrn_lines = 1;
614 1.23 joerg else {
615 1.23 joerg p_ptrn_lines = 0;
616 1.23 joerg p_first = 1;
617 1.23 joerg }
618 1.27 christos if (p_first >= LINENUM_MAX - p_ptrn_lines ||
619 1.27 christos p_ptrn_lines >= LINENUM_MAX - 6)
620 1.26 christos malformed();
621 1.23 joerg
622 1.23 joerg /* we need this much at least */
623 1.23 joerg p_max = p_ptrn_lines + 6;
624 1.23 joerg while (p_max >= hunkmax)
625 1.23 joerg grow_hunkmax();
626 1.23 joerg p_max = hunkmax;
627 1.23 joerg break;
628 1.23 joerg case '-':
629 1.23 joerg if (buf[1] == '-') {
630 1.23 joerg if (repl_beginning ||
631 1.23 joerg (p_end != p_ptrn_lines + 1 +
632 1.23 joerg (p_char[p_end - 1] == '\n'))) {
633 1.23 joerg if (p_end == 1) {
634 1.23 joerg /*
635 1.23 joerg * `old' lines were omitted;
636 1.23 joerg * set up to fill them in
637 1.23 joerg * from 'new' context lines.
638 1.23 joerg */
639 1.23 joerg p_end = p_ptrn_lines + 1;
640 1.23 joerg fillsrc = p_end + 1;
641 1.23 joerg filldst = 1;
642 1.23 joerg fillcnt = p_ptrn_lines;
643 1.23 joerg } else {
644 1.23 joerg if (repl_beginning) {
645 1.23 joerg if (repl_could_be_missing) {
646 1.23 joerg repl_missing = true;
647 1.23 joerg goto hunk_done;
648 1.23 joerg }
649 1.23 joerg fatal("duplicate \"---\" at line %ld--check line numbers at line %ld\n",
650 1.23 joerg p_input_line, p_hunk_beg + repl_beginning);
651 1.23 joerg } else {
652 1.23 joerg fatal("%s \"---\" at line %ld--check line numbers at line %ld\n",
653 1.23 joerg (p_end <= p_ptrn_lines
654 1.23 joerg ? "Premature"
655 1.23 joerg : "Overdue"),
656 1.23 joerg p_input_line, p_hunk_beg);
657 1.23 joerg }
658 1.23 joerg }
659 1.23 joerg }
660 1.23 joerg repl_beginning = p_end;
661 1.23 joerg repl_backtrack_position = ftell(pfp);
662 1.23 joerg repl_patch_line = p_input_line;
663 1.23 joerg p_line[p_end] = savestr(buf);
664 1.23 joerg if (out_of_mem) {
665 1.23 joerg p_end--;
666 1.23 joerg return false;
667 1.23 joerg }
668 1.23 joerg p_char[p_end] = '=';
669 1.23 joerg for (s = buf; *s && !isdigit((unsigned char)*s); s++)
670 1.23 joerg ;
671 1.23 joerg if (!*s)
672 1.23 joerg malformed();
673 1.26 christos p_newfirst = getskiplinenum(&s);
674 1.23 joerg if (*s == ',') {
675 1.23 joerg for (; *s && !isdigit((unsigned char)*s); s++)
676 1.23 joerg ;
677 1.23 joerg if (!*s)
678 1.23 joerg malformed();
679 1.26 christos p_repl_lines = (getlinenum(s)) -
680 1.23 joerg p_newfirst + 1;
681 1.26 christos if (p_repl_lines < 0)
682 1.26 christos malformed();
683 1.23 joerg } else if (p_newfirst)
684 1.23 joerg p_repl_lines = 1;
685 1.23 joerg else {
686 1.23 joerg p_repl_lines = 0;
687 1.23 joerg p_newfirst = 1;
688 1.23 joerg }
689 1.26 christos if (p_newfirst >= LINENUM_MAX - p_repl_lines ||
690 1.26 christos p_repl_lines >= LINENUM_MAX - p_end)
691 1.26 christos malformed();
692 1.23 joerg p_max = p_repl_lines + p_end;
693 1.23 joerg if (p_max > MAXHUNKSIZE)
694 1.23 joerg fatal("hunk too large (%ld lines) at line %ld: %s",
695 1.23 joerg p_max, p_input_line, buf);
696 1.23 joerg while (p_max >= hunkmax)
697 1.23 joerg grow_hunkmax();
698 1.23 joerg if (p_repl_lines != ptrn_copiable &&
699 1.23 joerg (p_context != 0 || p_repl_lines != 1))
700 1.23 joerg repl_could_be_missing = false;
701 1.23 joerg break;
702 1.23 joerg }
703 1.23 joerg goto change_line;
704 1.23 joerg case '+':
705 1.23 joerg case '!':
706 1.23 joerg repl_could_be_missing = false;
707 1.23 joerg change_line:
708 1.23 joerg if (buf[1] == '\n' && canonicalize)
709 1.32 cjep strlcpy(buf + 1, " \n", bufsz - 1);
710 1.23 joerg if (!isspace((unsigned char)buf[1]) && buf[1] != '>' &&
711 1.23 joerg buf[1] != '<' &&
712 1.23 joerg repl_beginning && repl_could_be_missing) {
713 1.23 joerg repl_missing = true;
714 1.23 joerg goto hunk_done;
715 1.23 joerg }
716 1.23 joerg if (context >= 0) {
717 1.23 joerg if (context < p_context)
718 1.23 joerg p_context = context;
719 1.23 joerg context = -1000;
720 1.23 joerg }
721 1.23 joerg p_line[p_end] = savestr(buf + 2);
722 1.23 joerg if (out_of_mem) {
723 1.23 joerg p_end--;
724 1.23 joerg return false;
725 1.23 joerg }
726 1.23 joerg if (p_end == p_ptrn_lines) {
727 1.23 joerg if (remove_special_line()) {
728 1.23 joerg int len;
729 1.23 joerg
730 1.23 joerg len = strlen(p_line[p_end]) - 1;
731 1.23 joerg (p_line[p_end])[len] = 0;
732 1.23 joerg }
733 1.23 joerg }
734 1.23 joerg break;
735 1.23 joerg case '\t':
736 1.23 joerg case '\n': /* assume the 2 spaces got eaten */
737 1.23 joerg if (repl_beginning && repl_could_be_missing &&
738 1.23 joerg (!ptrn_spaces_eaten ||
739 1.23 joerg diff_type == NEW_CONTEXT_DIFF)) {
740 1.23 joerg repl_missing = true;
741 1.23 joerg goto hunk_done;
742 1.23 joerg }
743 1.23 joerg p_line[p_end] = savestr(buf);
744 1.23 joerg if (out_of_mem) {
745 1.23 joerg p_end--;
746 1.23 joerg return false;
747 1.23 joerg }
748 1.23 joerg if (p_end != p_ptrn_lines + 1) {
749 1.23 joerg ptrn_spaces_eaten |= (repl_beginning != 0);
750 1.23 joerg context++;
751 1.23 joerg if (!repl_beginning)
752 1.23 joerg ptrn_copiable++;
753 1.23 joerg p_char[p_end] = ' ';
754 1.23 joerg }
755 1.23 joerg break;
756 1.23 joerg case ' ':
757 1.23 joerg if (!isspace((unsigned char)buf[1]) &&
758 1.23 joerg repl_beginning && repl_could_be_missing) {
759 1.23 joerg repl_missing = true;
760 1.23 joerg goto hunk_done;
761 1.23 joerg }
762 1.23 joerg context++;
763 1.23 joerg if (!repl_beginning)
764 1.23 joerg ptrn_copiable++;
765 1.23 joerg p_line[p_end] = savestr(buf + 2);
766 1.23 joerg if (out_of_mem) {
767 1.23 joerg p_end--;
768 1.23 joerg return false;
769 1.23 joerg }
770 1.23 joerg break;
771 1.23 joerg default:
772 1.23 joerg if (repl_beginning && repl_could_be_missing) {
773 1.23 joerg repl_missing = true;
774 1.23 joerg goto hunk_done;
775 1.23 joerg }
776 1.23 joerg malformed();
777 1.23 joerg }
778 1.23 joerg /* set up p_len for strncmp() so we don't have to */
779 1.23 joerg /* assume null termination */
780 1.23 joerg if (p_line[p_end])
781 1.23 joerg p_len[p_end] = strlen(p_line[p_end]);
782 1.23 joerg else
783 1.23 joerg p_len[p_end] = 0;
784 1.23 joerg }
785 1.23 joerg
786 1.23 joerg hunk_done:
787 1.23 joerg if (p_end >= 0 && !repl_beginning)
788 1.23 joerg fatal("no --- found in patch at line %ld\n", pch_hunk_beg());
789 1.23 joerg
790 1.23 joerg if (repl_missing) {
791 1.23 joerg
792 1.23 joerg /* reset state back to just after --- */
793 1.23 joerg p_input_line = repl_patch_line;
794 1.23 joerg for (p_end--; p_end > repl_beginning; p_end--)
795 1.23 joerg free(p_line[p_end]);
796 1.23 joerg fseek(pfp, repl_backtrack_position, SEEK_SET);
797 1.23 joerg
798 1.23 joerg /* redundant 'new' context lines were omitted - set */
799 1.23 joerg /* up to fill them in from the old file context */
800 1.23 joerg if (!p_context && p_repl_lines == 1) {
801 1.23 joerg p_repl_lines = 0;
802 1.23 joerg p_max--;
803 1.23 joerg }
804 1.23 joerg fillsrc = 1;
805 1.23 joerg filldst = repl_beginning + 1;
806 1.23 joerg fillcnt = p_repl_lines;
807 1.23 joerg p_end = p_max;
808 1.23 joerg } else if (!p_context && fillcnt == 1) {
809 1.23 joerg /* the first hunk was a null hunk with no context */
810 1.23 joerg /* and we were expecting one line -- fix it up. */
811 1.23 joerg while (filldst < p_end) {
812 1.23 joerg p_line[filldst] = p_line[filldst + 1];
813 1.23 joerg p_char[filldst] = p_char[filldst + 1];
814 1.23 joerg p_len[filldst] = p_len[filldst + 1];
815 1.23 joerg filldst++;
816 1.23 joerg }
817 1.23 joerg #if 0
818 1.23 joerg repl_beginning--; /* this doesn't need to be fixed */
819 1.23 joerg #endif
820 1.23 joerg p_end--;
821 1.23 joerg p_first++; /* do append rather than insert */
822 1.23 joerg fillcnt = 0;
823 1.23 joerg p_ptrn_lines = 0;
824 1.23 joerg }
825 1.23 joerg if (diff_type == CONTEXT_DIFF &&
826 1.23 joerg (fillcnt || (p_first > 1 && ptrn_copiable > 2 * p_context))) {
827 1.23 joerg if (verbose)
828 1.23 joerg say("%s\n%s\n%s\n",
829 1.23 joerg "(Fascinating--this is really a new-style context diff but without",
830 1.23 joerg "the telltale extra asterisks on the *** line that usually indicate",
831 1.23 joerg "the new style...)");
832 1.23 joerg diff_type = NEW_CONTEXT_DIFF;
833 1.23 joerg }
834 1.23 joerg /* if there were omitted context lines, fill them in now */
835 1.23 joerg if (fillcnt) {
836 1.23 joerg p_bfake = filldst; /* remember where not to free() */
837 1.23 joerg p_efake = filldst + fillcnt - 1;
838 1.23 joerg while (fillcnt-- > 0) {
839 1.23 joerg while (fillsrc <= p_end && p_char[fillsrc] != ' ')
840 1.23 joerg fillsrc++;
841 1.23 joerg if (fillsrc > p_end)
842 1.23 joerg fatal("replacement text or line numbers mangled in hunk at line %ld\n",
843 1.23 joerg p_hunk_beg);
844 1.23 joerg p_line[filldst] = p_line[fillsrc];
845 1.23 joerg p_char[filldst] = p_char[fillsrc];
846 1.23 joerg p_len[filldst] = p_len[fillsrc];
847 1.23 joerg fillsrc++;
848 1.23 joerg filldst++;
849 1.23 joerg }
850 1.23 joerg while (fillsrc <= p_end && fillsrc != repl_beginning &&
851 1.23 joerg p_char[fillsrc] != ' ')
852 1.23 joerg fillsrc++;
853 1.23 joerg #ifdef DEBUGGING
854 1.23 joerg if (debug & 64)
855 1.23 joerg printf("fillsrc %ld, filldst %ld, rb %ld, e+1 %ld\n",
856 1.23 joerg fillsrc, filldst, repl_beginning, p_end + 1);
857 1.23 joerg #endif
858 1.23 joerg if (fillsrc != p_end + 1 && fillsrc != repl_beginning)
859 1.23 joerg malformed();
860 1.23 joerg if (filldst != p_end + 1 && filldst != repl_beginning)
861 1.23 joerg malformed();
862 1.23 joerg }
863 1.23 joerg if (p_line[p_end] != NULL) {
864 1.23 joerg if (remove_special_line()) {
865 1.23 joerg p_len[p_end] -= 1;
866 1.23 joerg (p_line[p_end])[p_len[p_end]] = 0;
867 1.23 joerg }
868 1.23 joerg }
869 1.23 joerg } else if (diff_type == UNI_DIFF) {
870 1.23 joerg LINENUM fillold; /* index of old lines */
871 1.23 joerg LINENUM fillnew; /* index of new lines */
872 1.23 joerg char ch;
873 1.23 joerg
874 1.23 joerg line_beginning = ftell(pfp); /* file pos of the current line */
875 1.32 cjep ret = pgetline(&buf, &bufsz, pfp);
876 1.23 joerg p_input_line++;
877 1.32 cjep if (ret == -1 || strnNE(buf, "@@ -", 4)) {
878 1.23 joerg next_intuit_at(line_beginning, p_input_line);
879 1.23 joerg return false;
880 1.1 cgd }
881 1.23 joerg s = buf + 4;
882 1.1 cgd if (!*s)
883 1.23 joerg malformed();
884 1.26 christos p_first = getskiplinenum(&s);
885 1.26 christos if (*s == ',') {
886 1.23 joerg s++;
887 1.26 christos p_ptrn_lines = getskiplinenum(&s);
888 1.23 joerg } else
889 1.23 joerg p_ptrn_lines = 1;
890 1.26 christos if (p_first >= LINENUM_MAX - p_ptrn_lines)
891 1.26 christos malformed();
892 1.23 joerg if (*s == ' ')
893 1.23 joerg s++;
894 1.23 joerg if (*s != '+' || !*++s)
895 1.23 joerg malformed();
896 1.26 christos p_newfirst = getskiplinenum(&s);
897 1.26 christos if (*s == ',') {
898 1.17 kristerw s++;
899 1.26 christos p_repl_lines = getskiplinenum(&s);
900 1.23 joerg } else
901 1.23 joerg p_repl_lines = 1;
902 1.23 joerg if (*s == ' ')
903 1.23 joerg s++;
904 1.23 joerg if (*s != '@')
905 1.17 kristerw malformed();
906 1.26 christos if (p_first >= LINENUM_MAX - p_ptrn_lines ||
907 1.26 christos p_newfirst > LINENUM_MAX - p_repl_lines ||
908 1.26 christos p_ptrn_lines >= LINENUM_MAX - p_repl_lines - 1)
909 1.26 christos malformed();
910 1.23 joerg if (!p_ptrn_lines)
911 1.23 joerg p_first++; /* do append rather than insert */
912 1.23 joerg p_max = p_ptrn_lines + p_repl_lines + 1;
913 1.23 joerg while (p_max >= hunkmax)
914 1.23 joerg grow_hunkmax();
915 1.23 joerg fillold = 1;
916 1.23 joerg fillnew = fillold + p_ptrn_lines;
917 1.23 joerg p_end = fillnew + p_repl_lines;
918 1.32 cjep snprintf(buf, bufsz, "*** %ld,%ld ****\n", p_first,
919 1.23 joerg p_first + p_ptrn_lines - 1);
920 1.23 joerg p_line[0] = savestr(buf);
921 1.23 joerg if (out_of_mem) {
922 1.23 joerg p_end = -1;
923 1.23 joerg return false;
924 1.23 joerg }
925 1.23 joerg p_char[0] = '*';
926 1.32 cjep snprintf(buf, bufsz, "--- %ld,%ld ----\n", p_newfirst,
927 1.23 joerg p_newfirst + p_repl_lines - 1);
928 1.23 joerg p_line[fillnew] = savestr(buf);
929 1.23 joerg if (out_of_mem) {
930 1.23 joerg p_end = 0;
931 1.23 joerg return false;
932 1.1 cgd }
933 1.23 joerg p_char[fillnew++] = '=';
934 1.23 joerg p_context = 100;
935 1.23 joerg context = 0;
936 1.23 joerg p_hunk_beg = p_input_line + 1;
937 1.23 joerg while (fillold <= p_ptrn_lines || fillnew <= p_end) {
938 1.23 joerg line_beginning = ftell(pfp);
939 1.32 cjep ret = pgetline(&buf, &bufsz, pfp);
940 1.23 joerg p_input_line++;
941 1.32 cjep if (ret == -1) {
942 1.23 joerg if (p_max - fillnew < 3) {
943 1.23 joerg /* assume blank lines got chopped */
944 1.32 cjep strlcpy(buf, " \n", bufsz);
945 1.23 joerg } else {
946 1.23 joerg fatal("unexpected end of file in patch\n");
947 1.23 joerg }
948 1.23 joerg }
949 1.23 joerg if (*buf == '\t' || *buf == '\n') {
950 1.23 joerg ch = ' '; /* assume the space got eaten */
951 1.23 joerg s = savestr(buf);
952 1.17 kristerw } else {
953 1.23 joerg ch = *buf;
954 1.23 joerg s = savestr(buf + 1);
955 1.23 joerg }
956 1.23 joerg if (out_of_mem) {
957 1.23 joerg while (--fillnew > p_ptrn_lines)
958 1.23 joerg free(p_line[fillnew]);
959 1.23 joerg p_end = fillold - 1;
960 1.23 joerg return false;
961 1.23 joerg }
962 1.23 joerg switch (ch) {
963 1.23 joerg case '-':
964 1.23 joerg if (fillold > p_ptrn_lines) {
965 1.23 joerg free(s);
966 1.23 joerg p_end = fillnew - 1;
967 1.23 joerg malformed();
968 1.23 joerg }
969 1.23 joerg p_char[fillold] = ch;
970 1.23 joerg p_line[fillold] = s;
971 1.23 joerg p_len[fillold++] = strlen(s);
972 1.23 joerg if (fillold > p_ptrn_lines) {
973 1.23 joerg if (remove_special_line()) {
974 1.23 joerg p_len[fillold - 1] -= 1;
975 1.23 joerg s[p_len[fillold - 1]] = 0;
976 1.23 joerg }
977 1.23 joerg }
978 1.23 joerg break;
979 1.23 joerg case '=':
980 1.23 joerg ch = ' ';
981 1.23 joerg /* FALL THROUGH */
982 1.23 joerg case ' ':
983 1.23 joerg if (fillold > p_ptrn_lines) {
984 1.23 joerg free(s);
985 1.23 joerg while (--fillnew > p_ptrn_lines)
986 1.23 joerg free(p_line[fillnew]);
987 1.23 joerg p_end = fillold - 1;
988 1.23 joerg malformed();
989 1.23 joerg }
990 1.23 joerg context++;
991 1.23 joerg p_char[fillold] = ch;
992 1.23 joerg p_line[fillold] = s;
993 1.23 joerg p_len[fillold++] = strlen(s);
994 1.23 joerg s = savestr(s);
995 1.23 joerg if (out_of_mem) {
996 1.23 joerg while (--fillnew > p_ptrn_lines)
997 1.23 joerg free(p_line[fillnew]);
998 1.23 joerg p_end = fillold - 1;
999 1.23 joerg return false;
1000 1.23 joerg }
1001 1.23 joerg if (fillold > p_ptrn_lines) {
1002 1.23 joerg if (remove_special_line()) {
1003 1.23 joerg p_len[fillold - 1] -= 1;
1004 1.23 joerg s[p_len[fillold - 1]] = 0;
1005 1.23 joerg }
1006 1.23 joerg }
1007 1.23 joerg /* FALL THROUGH */
1008 1.23 joerg case '+':
1009 1.23 joerg if (fillnew > p_end) {
1010 1.23 joerg free(s);
1011 1.23 joerg while (--fillnew > p_ptrn_lines)
1012 1.23 joerg free(p_line[fillnew]);
1013 1.23 joerg p_end = fillold - 1;
1014 1.23 joerg malformed();
1015 1.23 joerg }
1016 1.23 joerg p_char[fillnew] = ch;
1017 1.23 joerg p_line[fillnew] = s;
1018 1.23 joerg p_len[fillnew++] = strlen(s);
1019 1.23 joerg if (fillold > p_ptrn_lines) {
1020 1.23 joerg if (remove_special_line()) {
1021 1.23 joerg p_len[fillnew - 1] -= 1;
1022 1.23 joerg s[p_len[fillnew - 1]] = 0;
1023 1.23 joerg }
1024 1.23 joerg }
1025 1.23 joerg break;
1026 1.23 joerg default:
1027 1.23 joerg p_end = fillnew;
1028 1.23 joerg malformed();
1029 1.1 cgd }
1030 1.23 joerg if (ch != ' ' && context > 0) {
1031 1.23 joerg if (context < p_context)
1032 1.23 joerg p_context = context;
1033 1.23 joerg context = -1000;
1034 1.23 joerg }
1035 1.23 joerg } /* while */
1036 1.23 joerg } else { /* normal diff--fake it up */
1037 1.23 joerg char hunk_type;
1038 1.23 joerg int i;
1039 1.23 joerg LINENUM min, max;
1040 1.23 joerg
1041 1.23 joerg line_beginning = ftell(pfp);
1042 1.23 joerg p_context = 0;
1043 1.32 cjep ret = pgetline(&buf, &bufsz, pfp);
1044 1.23 joerg p_input_line++;
1045 1.32 cjep if (ret == -1 || !isdigit((unsigned char)*buf)) {
1046 1.23 joerg next_intuit_at(line_beginning, p_input_line);
1047 1.23 joerg return false;
1048 1.23 joerg }
1049 1.26 christos s = buf;
1050 1.26 christos p_first = getskiplinenum(&s);
1051 1.23 joerg if (*s == ',') {
1052 1.26 christos s++;
1053 1.26 christos p_ptrn_lines = getskiplinenum(&s) - p_first + 1;
1054 1.23 joerg } else
1055 1.23 joerg p_ptrn_lines = (*s != 'a');
1056 1.26 christos if (p_first >= LINENUM_MAX - p_ptrn_lines)
1057 1.26 christos malformed();
1058 1.26 christos hunk_type = *s++;
1059 1.23 joerg if (hunk_type == 'a')
1060 1.23 joerg p_first++; /* do append rather than insert */
1061 1.26 christos min = getskiplinenum(&s);
1062 1.23 joerg if (*s == ',')
1063 1.26 christos max = getlinenum(++s);
1064 1.23 joerg else
1065 1.23 joerg max = min;
1066 1.26 christos if (min < 0 || min > max || max - min == LINENUM_MAX)
1067 1.26 christos malformed();
1068 1.23 joerg if (hunk_type == 'd')
1069 1.23 joerg min++;
1070 1.23 joerg p_end = p_ptrn_lines + 1 + max - min + 1;
1071 1.26 christos p_newfirst = min;
1072 1.26 christos p_repl_lines = max - min + 1;
1073 1.26 christos if (p_newfirst > LINENUM_MAX - p_repl_lines ||
1074 1.26 christos p_ptrn_lines >= LINENUM_MAX - p_repl_lines - 1)
1075 1.26 christos malformed();
1076 1.26 christos p_end = p_ptrn_lines + p_repl_lines + 1;
1077 1.23 joerg if (p_end > MAXHUNKSIZE)
1078 1.23 joerg fatal("hunk too large (%ld lines) at line %ld: %s",
1079 1.23 joerg p_end, p_input_line, buf);
1080 1.23 joerg while (p_end >= hunkmax)
1081 1.1 cgd grow_hunkmax();
1082 1.32 cjep snprintf(buf, bufsz, "*** %ld,%ld\n", p_first,
1083 1.23 joerg p_first + p_ptrn_lines - 1);
1084 1.23 joerg p_line[0] = savestr(buf);
1085 1.23 joerg if (out_of_mem) {
1086 1.23 joerg p_end = -1;
1087 1.23 joerg return false;
1088 1.23 joerg }
1089 1.23 joerg p_char[0] = '*';
1090 1.23 joerg for (i = 1; i <= p_ptrn_lines; i++) {
1091 1.32 cjep ret = pgetline(&buf, &bufsz, pfp);
1092 1.23 joerg p_input_line++;
1093 1.32 cjep if (ret == -1)
1094 1.23 joerg fatal("unexpected end of file in patch at line %ld\n",
1095 1.23 joerg p_input_line);
1096 1.23 joerg if (*buf != '<')
1097 1.23 joerg fatal("< expected at line %ld of patch\n",
1098 1.23 joerg p_input_line);
1099 1.23 joerg p_line[i] = savestr(buf + 2);
1100 1.23 joerg if (out_of_mem) {
1101 1.23 joerg p_end = i - 1;
1102 1.23 joerg return false;
1103 1.13 kristerw }
1104 1.23 joerg p_len[i] = strlen(p_line[i]);
1105 1.23 joerg p_char[i] = '-';
1106 1.13 kristerw }
1107 1.13 kristerw
1108 1.13 kristerw if (remove_special_line()) {
1109 1.23 joerg p_len[i - 1] -= 1;
1110 1.23 joerg (p_line[i - 1])[p_len[i - 1]] = 0;
1111 1.13 kristerw }
1112 1.23 joerg if (hunk_type == 'c') {
1113 1.32 cjep ret = pgetline(&buf, &bufsz, pfp);
1114 1.23 joerg p_input_line++;
1115 1.32 cjep if (ret == -1)
1116 1.23 joerg fatal("unexpected end of file in patch at line %ld\n",
1117 1.23 joerg p_input_line);
1118 1.23 joerg if (*buf != '-')
1119 1.23 joerg fatal("--- expected at line %ld of patch\n",
1120 1.23 joerg p_input_line);
1121 1.23 joerg }
1122 1.32 cjep snprintf(buf, bufsz, "--- %ld,%ld\n", min, max);
1123 1.23 joerg p_line[i] = savestr(buf);
1124 1.23 joerg if (out_of_mem) {
1125 1.23 joerg p_end = i - 1;
1126 1.23 joerg return false;
1127 1.23 joerg }
1128 1.23 joerg p_char[i] = '=';
1129 1.23 joerg for (i++; i <= p_end; i++) {
1130 1.32 cjep ret = pgetline(&buf, &bufsz, pfp);
1131 1.23 joerg p_input_line++;
1132 1.32 cjep if (ret == -1)
1133 1.23 joerg fatal("unexpected end of file in patch at line %ld\n",
1134 1.23 joerg p_input_line);
1135 1.23 joerg if (*buf != '>')
1136 1.23 joerg fatal("> expected at line %ld of patch\n",
1137 1.23 joerg p_input_line);
1138 1.23 joerg p_line[i] = savestr(buf + 2);
1139 1.23 joerg if (out_of_mem) {
1140 1.23 joerg p_end = i - 1;
1141 1.23 joerg return false;
1142 1.13 kristerw }
1143 1.23 joerg p_len[i] = strlen(p_line[i]);
1144 1.23 joerg p_char[i] = '+';
1145 1.13 kristerw }
1146 1.23 joerg
1147 1.23 joerg if (remove_special_line()) {
1148 1.23 joerg p_len[i - 1] -= 1;
1149 1.23 joerg (p_line[i - 1])[p_len[i - 1]] = 0;
1150 1.13 kristerw }
1151 1.23 joerg }
1152 1.23 joerg if (reverse) /* backwards patch? */
1153 1.23 joerg if (!pch_swap())
1154 1.23 joerg say("Not enough memory to swap next hunk!\n");
1155 1.1 cgd #ifdef DEBUGGING
1156 1.23 joerg if (debug & 2) {
1157 1.23 joerg int i;
1158 1.23 joerg char special;
1159 1.23 joerg
1160 1.23 joerg for (i = 0; i <= p_end; i++) {
1161 1.23 joerg if (i == p_ptrn_lines)
1162 1.23 joerg special = '^';
1163 1.23 joerg else
1164 1.23 joerg special = ' ';
1165 1.23 joerg fprintf(stderr, "%3d %c %c %s", i, p_char[i],
1166 1.23 joerg special, p_line[i]);
1167 1.23 joerg fflush(stderr);
1168 1.23 joerg }
1169 1.1 cgd }
1170 1.1 cgd #endif
1171 1.23 joerg if (p_end + 1 < hunkmax)/* paranoia reigns supreme... */
1172 1.23 joerg p_char[p_end + 1] = '^'; /* add a stopper for apply_hunk */
1173 1.23 joerg return true;
1174 1.1 cgd }
1175 1.1 cgd
1176 1.10 kristerw /*
1177 1.10 kristerw * Input a line from the patch file, worrying about indentation.
1178 1.10 kristerw */
1179 1.32 cjep int
1180 1.32 cjep pgetline(char **bf, size_t *sz, FILE *fp)
1181 1.1 cgd {
1182 1.32 cjep char *s;
1183 1.23 joerg int indent = 0;
1184 1.32 cjep int ret;
1185 1.32 cjep
1186 1.32 cjep ret = getline(bf, sz, fp);
1187 1.10 kristerw
1188 1.32 cjep if (p_indent && ret != -1) {
1189 1.17 kristerw for (s = buf;
1190 1.23 joerg indent < p_indent && (*s == ' ' || *s == '\t' || *s == 'X');
1191 1.23 joerg s++) {
1192 1.10 kristerw if (*s == '\t')
1193 1.10 kristerw indent += 8 - (indent % 7);
1194 1.10 kristerw else
1195 1.10 kristerw indent++;
1196 1.10 kristerw }
1197 1.32 cjep if (buf != s && strlcpy(buf, s, bufsz) >= bufsz)
1198 1.32 cjep fatal("buffer too small in pgetline()\n");
1199 1.10 kristerw }
1200 1.10 kristerw return ret;
1201 1.10 kristerw }
1202 1.10 kristerw
1203 1.10 kristerw /*
1204 1.10 kristerw * Reverse the old and new portions of the current hunk.
1205 1.10 kristerw */
1206 1.1 cgd bool
1207 1.8 kristerw pch_swap(void)
1208 1.1 cgd {
1209 1.23 joerg char **tp_line; /* the text of the hunk */
1210 1.23 joerg short *tp_len; /* length of each line */
1211 1.23 joerg char *tp_char; /* +, -, and ! */
1212 1.23 joerg LINENUM i;
1213 1.23 joerg LINENUM n;
1214 1.23 joerg bool blankline = false;
1215 1.23 joerg char *s;
1216 1.10 kristerw
1217 1.10 kristerw i = p_first;
1218 1.10 kristerw p_first = p_newfirst;
1219 1.10 kristerw p_newfirst = i;
1220 1.23 joerg
1221 1.10 kristerw /* make a scratch copy */
1222 1.1 cgd
1223 1.10 kristerw tp_line = p_line;
1224 1.10 kristerw tp_len = p_len;
1225 1.10 kristerw tp_char = p_char;
1226 1.23 joerg p_line = NULL; /* force set_hunkmax to allocate again */
1227 1.10 kristerw p_len = NULL;
1228 1.10 kristerw p_char = NULL;
1229 1.10 kristerw set_hunkmax();
1230 1.10 kristerw if (p_line == NULL || p_len == NULL || p_char == NULL) {
1231 1.23 joerg
1232 1.23 joerg free(p_line);
1233 1.10 kristerw p_line = tp_line;
1234 1.23 joerg free(p_len);
1235 1.10 kristerw p_len = tp_len;
1236 1.23 joerg free(p_char);
1237 1.10 kristerw p_char = tp_char;
1238 1.23 joerg return false; /* not enough memory to swap hunk! */
1239 1.10 kristerw }
1240 1.10 kristerw /* now turn the new into the old */
1241 1.1 cgd
1242 1.1 cgd i = p_ptrn_lines + 1;
1243 1.10 kristerw if (tp_char[i] == '\n') { /* account for possible blank line */
1244 1.23 joerg blankline = true;
1245 1.10 kristerw i++;
1246 1.10 kristerw }
1247 1.23 joerg if (p_efake >= 0) { /* fix non-freeable ptr range */
1248 1.10 kristerw if (p_efake <= i)
1249 1.10 kristerw n = p_end - i + 1;
1250 1.10 kristerw else
1251 1.10 kristerw n = -i;
1252 1.10 kristerw p_efake += n;
1253 1.10 kristerw p_bfake += n;
1254 1.10 kristerw }
1255 1.17 kristerw for (n = 0; i <= 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.10 kristerw if (blankline) {
1263 1.10 kristerw i = p_ptrn_lines + 1;
1264 1.10 kristerw p_line[n] = tp_line[i];
1265 1.10 kristerw p_char[n] = tp_char[i];
1266 1.10 kristerw p_len[n] = tp_len[i];
1267 1.10 kristerw n++;
1268 1.10 kristerw }
1269 1.14 christos if (p_char[0] != '=')
1270 1.23 joerg fatal("Malformed patch at line %ld: expected '=' found '%c'\n",
1271 1.14 christos p_input_line, p_char[0]);
1272 1.10 kristerw p_char[0] = '*';
1273 1.17 kristerw for (s = p_line[0]; *s; s++)
1274 1.10 kristerw if (*s == '-')
1275 1.10 kristerw *s = '*';
1276 1.10 kristerw
1277 1.10 kristerw /* now turn the old into the new */
1278 1.10 kristerw
1279 1.23 joerg if (p_char[0] != '*')
1280 1.23 joerg fatal("Malformed patch at line %ld: expected '*' found '%c'\n",
1281 1.23 joerg p_input_line, p_char[0]);
1282 1.10 kristerw tp_char[0] = '=';
1283 1.17 kristerw for (s = tp_line[0]; *s; s++)
1284 1.10 kristerw if (*s == '*')
1285 1.10 kristerw *s = '-';
1286 1.17 kristerw for (i = 0; n <= p_end; i++, n++) {
1287 1.10 kristerw p_line[n] = tp_line[i];
1288 1.10 kristerw p_char[n] = tp_char[i];
1289 1.10 kristerw if (p_char[n] == '-')
1290 1.10 kristerw p_char[n] = '+';
1291 1.10 kristerw p_len[n] = tp_len[i];
1292 1.10 kristerw }
1293 1.23 joerg
1294 1.14 christos if (i != p_ptrn_lines + 1)
1295 1.23 joerg fatal("Malformed patch at line %ld: expected %ld lines, "
1296 1.23 joerg "got %ld\n",
1297 1.14 christos p_input_line, p_ptrn_lines + 1, i);
1298 1.23 joerg
1299 1.10 kristerw i = p_ptrn_lines;
1300 1.10 kristerw p_ptrn_lines = p_repl_lines;
1301 1.10 kristerw p_repl_lines = i;
1302 1.23 joerg
1303 1.23 joerg free(tp_line);
1304 1.23 joerg free(tp_len);
1305 1.23 joerg free(tp_char);
1306 1.23 joerg
1307 1.23 joerg return true;
1308 1.10 kristerw }
1309 1.10 kristerw
1310 1.10 kristerw /*
1311 1.10 kristerw * Return the specified line position in the old file of the old context.
1312 1.10 kristerw */
1313 1.1 cgd LINENUM
1314 1.8 kristerw pch_first(void)
1315 1.1 cgd {
1316 1.10 kristerw return p_first;
1317 1.1 cgd }
1318 1.1 cgd
1319 1.10 kristerw /*
1320 1.10 kristerw * Return the number of lines of old context.
1321 1.10 kristerw */
1322 1.1 cgd LINENUM
1323 1.8 kristerw pch_ptrn_lines(void)
1324 1.1 cgd {
1325 1.10 kristerw return p_ptrn_lines;
1326 1.1 cgd }
1327 1.1 cgd
1328 1.10 kristerw /*
1329 1.10 kristerw * Return the probable line position in the new file of the first line.
1330 1.10 kristerw */
1331 1.1 cgd LINENUM
1332 1.8 kristerw pch_newfirst(void)
1333 1.1 cgd {
1334 1.10 kristerw return p_newfirst;
1335 1.1 cgd }
1336 1.1 cgd
1337 1.10 kristerw /*
1338 1.10 kristerw * Return the number of lines in the replacement text including context.
1339 1.10 kristerw */
1340 1.1 cgd LINENUM
1341 1.8 kristerw pch_repl_lines(void)
1342 1.1 cgd {
1343 1.10 kristerw return p_repl_lines;
1344 1.1 cgd }
1345 1.1 cgd
1346 1.10 kristerw /*
1347 1.10 kristerw * Return the number of lines in the whole hunk.
1348 1.10 kristerw */
1349 1.1 cgd LINENUM
1350 1.8 kristerw pch_end(void)
1351 1.1 cgd {
1352 1.10 kristerw return p_end;
1353 1.1 cgd }
1354 1.1 cgd
1355 1.10 kristerw /*
1356 1.10 kristerw * Return the number of context lines before the first changed line.
1357 1.10 kristerw */
1358 1.1 cgd LINENUM
1359 1.8 kristerw pch_context(void)
1360 1.1 cgd {
1361 1.10 kristerw return p_context;
1362 1.1 cgd }
1363 1.1 cgd
1364 1.10 kristerw /*
1365 1.10 kristerw * Return the length of a particular patch line.
1366 1.10 kristerw */
1367 1.23 joerg short
1368 1.8 kristerw pch_line_len(LINENUM line)
1369 1.1 cgd {
1370 1.10 kristerw return p_len[line];
1371 1.1 cgd }
1372 1.1 cgd
1373 1.10 kristerw /*
1374 1.10 kristerw * Return the control character (+, -, *, !, etc) for a patch line.
1375 1.10 kristerw */
1376 1.1 cgd char
1377 1.8 kristerw pch_char(LINENUM line)
1378 1.1 cgd {
1379 1.10 kristerw return p_char[line];
1380 1.1 cgd }
1381 1.1 cgd
1382 1.10 kristerw /*
1383 1.10 kristerw * Return a pointer to a particular patch line.
1384 1.10 kristerw */
1385 1.1 cgd char *
1386 1.8 kristerw pfetch(LINENUM line)
1387 1.1 cgd {
1388 1.10 kristerw return p_line[line];
1389 1.1 cgd }
1390 1.1 cgd
1391 1.10 kristerw /*
1392 1.10 kristerw * Return where in the patch file this hunk began, for error messages.
1393 1.10 kristerw */
1394 1.1 cgd LINENUM
1395 1.8 kristerw pch_hunk_beg(void)
1396 1.1 cgd {
1397 1.10 kristerw return p_hunk_beg;
1398 1.1 cgd }
1399 1.1 cgd
1400 1.10 kristerw /*
1401 1.10 kristerw * Apply an ed script by feeding ed itself.
1402 1.10 kristerw */
1403 1.1 cgd void
1404 1.8 kristerw do_ed_script(void)
1405 1.1 cgd {
1406 1.23 joerg char *t;
1407 1.23 joerg long beginning_of_this_line;
1408 1.23 joerg FILE *pipefp = NULL;
1409 1.28 christos int continuation;
1410 1.10 kristerw
1411 1.10 kristerw if (!skip_rest_of_patch) {
1412 1.23 joerg if (copy_file(filearg[0], TMPOUTNAME) < 0) {
1413 1.23 joerg unlink(TMPOUTNAME);
1414 1.23 joerg fatal("can't create temp file %s", TMPOUTNAME);
1415 1.23 joerg }
1416 1.32 cjep snprintf(buf, bufsz, "%s -S%s %s", _PATH_ED,
1417 1.29 christos verbose ? "" : "s", TMPOUTNAME);
1418 1.10 kristerw pipefp = popen(buf, "w");
1419 1.10 kristerw }
1420 1.10 kristerw for (;;) {
1421 1.10 kristerw beginning_of_this_line = ftell(pfp);
1422 1.32 cjep if (pgetline(&buf, &bufsz, pfp) == -1) {
1423 1.17 kristerw next_intuit_at(beginning_of_this_line, p_input_line);
1424 1.10 kristerw break;
1425 1.10 kristerw }
1426 1.10 kristerw p_input_line++;
1427 1.17 kristerw for (t = buf; isdigit((unsigned char)*t) || *t == ','; t++)
1428 1.17 kristerw ;
1429 1.23 joerg /* POSIX defines allowed commands as {a,c,d,i,s} */
1430 1.23 joerg if (isdigit((unsigned char)*buf) && (*t == 'a' || *t == 'c' ||
1431 1.23 joerg *t == 'd' || *t == 'i' || *t == 's')) {
1432 1.23 joerg if (pipefp != NULL)
1433 1.32 cjep fprintf(pipefp, "%s", buf);
1434 1.28 christos if (*t == 's') {
1435 1.28 christos for (;;) {
1436 1.28 christos continuation = 0;
1437 1.28 christos t = strchr(buf, '\0') - 1;
1438 1.28 christos while (--t >= buf && *t == '\\')
1439 1.28 christos continuation = !continuation;
1440 1.28 christos if (!continuation ||
1441 1.32 cjep pgetline(&buf, &bufsz, pfp) == -1)
1442 1.28 christos break;
1443 1.28 christos if (pipefp != NULL)
1444 1.32 cjep fprintf(pipefp, "%s", buf);
1445 1.28 christos }
1446 1.28 christos } else if (*t != 'd') {
1447 1.32 cjep while (pgetline(&buf, &bufsz, pfp) != -1) {
1448 1.10 kristerw p_input_line++;
1449 1.23 joerg if (pipefp != NULL)
1450 1.32 cjep fprintf(pipefp, "%s", buf);
1451 1.10 kristerw if (strEQ(buf, ".\n"))
1452 1.10 kristerw break;
1453 1.10 kristerw }
1454 1.10 kristerw }
1455 1.17 kristerw } else {
1456 1.23 joerg next_intuit_at(beginning_of_this_line, p_input_line);
1457 1.1 cgd break;
1458 1.1 cgd }
1459 1.1 cgd }
1460 1.23 joerg if (pipefp == NULL)
1461 1.10 kristerw return;
1462 1.10 kristerw fprintf(pipefp, "w\n");
1463 1.10 kristerw fprintf(pipefp, "q\n");
1464 1.23 joerg fflush(pipefp);
1465 1.23 joerg pclose(pipefp);
1466 1.10 kristerw ignore_signals();
1467 1.23 joerg if (!check_only) {
1468 1.23 joerg if (move_file(TMPOUTNAME, outname) < 0) {
1469 1.23 joerg toutkeep = true;
1470 1.23 joerg chmod(TMPOUTNAME, filemode);
1471 1.23 joerg } else
1472 1.23 joerg chmod(outname, filemode);
1473 1.23 joerg }
1474 1.10 kristerw set_signals(1);
1475 1.1 cgd }
1476 1.23 joerg
1477 1.23 joerg /*
1478 1.23 joerg * Choose the name of the file to be patched based on POSIX rules.
1479 1.23 joerg * NOTE: the POSIX rules are amazingly stupid and we only follow them
1480 1.23 joerg * if the user specified --posix or set POSIXLY_CORRECT.
1481 1.23 joerg */
1482 1.23 joerg static char *
1483 1.23 joerg posix_name(const struct file_name *names, bool assume_exists)
1484 1.23 joerg {
1485 1.23 joerg char *path = NULL;
1486 1.23 joerg int i;
1487 1.23 joerg
1488 1.23 joerg /*
1489 1.23 joerg * POSIX states that the filename will be chosen from one
1490 1.23 joerg * of the old, new and index names (in that order) if
1491 1.23 joerg * the file exists relative to CWD after -p stripping.
1492 1.23 joerg */
1493 1.23 joerg for (i = 0; i < MAX_FILE; i++) {
1494 1.23 joerg if (names[i].path != NULL && names[i].exists) {
1495 1.23 joerg path = names[i].path;
1496 1.23 joerg break;
1497 1.23 joerg }
1498 1.23 joerg }
1499 1.23 joerg if (path == NULL && !assume_exists) {
1500 1.23 joerg /*
1501 1.23 joerg * No files found, look for something we can checkout from
1502 1.23 joerg * RCS/SCCS dirs. Same order as above.
1503 1.23 joerg */
1504 1.23 joerg for (i = 0; i < MAX_FILE; i++) {
1505 1.23 joerg if (names[i].path != NULL &&
1506 1.23 joerg (path = checked_in(names[i].path)) != NULL)
1507 1.23 joerg break;
1508 1.23 joerg }
1509 1.23 joerg /*
1510 1.23 joerg * Still no match? Check to see if the diff could be creating
1511 1.23 joerg * a new file.
1512 1.23 joerg */
1513 1.23 joerg if (path == NULL && ok_to_create_file &&
1514 1.23 joerg names[NEW_FILE].path != NULL)
1515 1.23 joerg path = names[NEW_FILE].path;
1516 1.23 joerg }
1517 1.23 joerg
1518 1.23 joerg return path ? savestr(path) : NULL;
1519 1.23 joerg }
1520 1.23 joerg
1521 1.23 joerg /*
1522 1.23 joerg * Choose the name of the file to be patched based the "best" one
1523 1.23 joerg * available.
1524 1.23 joerg */
1525 1.23 joerg static char *
1526 1.23 joerg best_name(const struct file_name *names, bool assume_exists)
1527 1.23 joerg {
1528 1.23 joerg size_t min_components, min_baselen, min_len, tmp;
1529 1.23 joerg char *best = NULL;
1530 1.23 joerg int i;
1531 1.23 joerg
1532 1.23 joerg /*
1533 1.23 joerg * The "best" name is the one with the fewest number of path
1534 1.23 joerg * components, the shortest basename length, and the shortest
1535 1.23 joerg * overall length (in that order). We only use the Index: file
1536 1.23 joerg * if neither of the old or new files could be intuited from
1537 1.23 joerg * the diff header.
1538 1.23 joerg */
1539 1.23 joerg min_components = min_baselen = min_len = SIZE_MAX;
1540 1.23 joerg for (i = INDEX_FILE; i >= OLD_FILE; i--) {
1541 1.23 joerg if (names[i].path == NULL ||
1542 1.23 joerg (!names[i].exists && !assume_exists))
1543 1.23 joerg continue;
1544 1.23 joerg if ((tmp = num_components(names[i].path)) > min_components)
1545 1.23 joerg continue;
1546 1.23 joerg min_components = tmp;
1547 1.23 joerg if ((tmp = strlen(basename(names[i].path))) > min_baselen)
1548 1.23 joerg continue;
1549 1.23 joerg min_baselen = tmp;
1550 1.23 joerg if ((tmp = strlen(names[i].path)) > min_len)
1551 1.23 joerg continue;
1552 1.23 joerg min_len = tmp;
1553 1.23 joerg best = names[i].path;
1554 1.23 joerg }
1555 1.23 joerg if (best == NULL) {
1556 1.23 joerg /*
1557 1.23 joerg * No files found, look for something we can checkout from
1558 1.23 joerg * RCS/SCCS dirs. Logic is identical to that above...
1559 1.23 joerg */
1560 1.23 joerg min_components = min_baselen = min_len = SIZE_MAX;
1561 1.23 joerg for (i = INDEX_FILE; i >= OLD_FILE; i--) {
1562 1.23 joerg if (names[i].path == NULL ||
1563 1.23 joerg checked_in(names[i].path) == NULL)
1564 1.23 joerg continue;
1565 1.23 joerg if ((tmp = num_components(names[i].path)) > min_components)
1566 1.23 joerg continue;
1567 1.23 joerg min_components = tmp;
1568 1.23 joerg if ((tmp = strlen(basename(names[i].path))) > min_baselen)
1569 1.23 joerg continue;
1570 1.23 joerg min_baselen = tmp;
1571 1.23 joerg if ((tmp = strlen(names[i].path)) > min_len)
1572 1.23 joerg continue;
1573 1.23 joerg min_len = tmp;
1574 1.23 joerg best = names[i].path;
1575 1.23 joerg }
1576 1.23 joerg /*
1577 1.23 joerg * Still no match? Check to see if the diff could be creating
1578 1.23 joerg * a new file.
1579 1.23 joerg */
1580 1.23 joerg if (best == NULL && ok_to_create_file &&
1581 1.23 joerg names[NEW_FILE].path != NULL)
1582 1.23 joerg best = names[NEW_FILE].path;
1583 1.23 joerg }
1584 1.23 joerg
1585 1.23 joerg return best ? savestr(best) : NULL;
1586 1.23 joerg }
1587 1.23 joerg
1588 1.23 joerg static size_t
1589 1.23 joerg num_components(const char *path)
1590 1.23 joerg {
1591 1.23 joerg size_t n;
1592 1.23 joerg const char *cp;
1593 1.23 joerg
1594 1.23 joerg for (n = 0, cp = path; (cp = strchr(cp, '/')) != NULL; n++, cp++) {
1595 1.23 joerg while (*cp == '/')
1596 1.23 joerg cp++; /* skip consecutive slashes */
1597 1.23 joerg }
1598 1.23 joerg return n;
1599 1.23 joerg }
1600