1757e8328SLionel Sambuc /*
2757e8328SLionel Sambuc * $OpenBSD: pch.c,v 1.37 2007/09/02 15:19:33 deraadt Exp $
3757e8328SLionel Sambuc * $DragonFly: src/usr.bin/patch/pch.c,v 1.6 2008/08/10 23:35:40 joerg Exp $
4*0a6a1f1dSLionel Sambuc * $NetBSD: pch.c,v 1.28 2015/07/30 21:47:51 christos Exp $
5757e8328SLionel Sambuc */
6757e8328SLionel Sambuc
7757e8328SLionel Sambuc /*
8757e8328SLionel Sambuc * patch - a program to apply diffs to original files
9757e8328SLionel Sambuc *
10757e8328SLionel Sambuc * Copyright 1986, Larry Wall
11757e8328SLionel Sambuc *
12757e8328SLionel Sambuc * Redistribution and use in source and binary forms, with or without
13757e8328SLionel Sambuc * modification, are permitted provided that the following condition is met:
14757e8328SLionel Sambuc * 1. Redistributions of source code must retain the above copyright notice,
15757e8328SLionel Sambuc * this condition and the following disclaimer.
16757e8328SLionel Sambuc *
17757e8328SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
18757e8328SLionel Sambuc * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19757e8328SLionel Sambuc * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20757e8328SLionel Sambuc * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21757e8328SLionel Sambuc * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22757e8328SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23757e8328SLionel Sambuc * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24757e8328SLionel Sambuc * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25757e8328SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26757e8328SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27757e8328SLionel Sambuc * SUCH DAMAGE.
28757e8328SLionel Sambuc *
29757e8328SLionel Sambuc * -C option added in 1998, original code by Marc Espie, based on FreeBSD
30757e8328SLionel Sambuc * behaviour
31757e8328SLionel Sambuc */
32757e8328SLionel Sambuc
33757e8328SLionel Sambuc #include <sys/cdefs.h>
34*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: pch.c,v 1.28 2015/07/30 21:47:51 christos Exp $");
35757e8328SLionel Sambuc
36757e8328SLionel Sambuc #include <sys/types.h>
37757e8328SLionel Sambuc #include <sys/stat.h>
38757e8328SLionel Sambuc
39757e8328SLionel Sambuc #include <ctype.h>
40757e8328SLionel Sambuc #include <libgen.h>
41757e8328SLionel Sambuc #include <limits.h>
42757e8328SLionel Sambuc #include <stdio.h>
43757e8328SLionel Sambuc #include <stdlib.h>
44757e8328SLionel Sambuc #include <string.h>
45757e8328SLionel Sambuc #include <unistd.h>
46757e8328SLionel Sambuc
47757e8328SLionel Sambuc #include "common.h"
48757e8328SLionel Sambuc #include "util.h"
49757e8328SLionel Sambuc #include "pch.h"
50757e8328SLionel Sambuc #include "pathnames.h"
51757e8328SLionel Sambuc
52757e8328SLionel Sambuc /* Patch (diff listing) abstract type. */
53757e8328SLionel Sambuc
54757e8328SLionel Sambuc static long p_filesize; /* size of the patch file */
55757e8328SLionel Sambuc static LINENUM p_first; /* 1st line number */
56757e8328SLionel Sambuc static LINENUM p_newfirst; /* 1st line number of replacement */
57757e8328SLionel Sambuc static LINENUM p_ptrn_lines; /* # lines in pattern */
58757e8328SLionel Sambuc static LINENUM p_repl_lines; /* # lines in replacement text */
59757e8328SLionel Sambuc static LINENUM p_end = -1; /* last line in hunk */
60757e8328SLionel Sambuc static LINENUM p_max; /* max allowed value of p_end */
61757e8328SLionel Sambuc static LINENUM p_context = 3; /* # of context lines */
62757e8328SLionel Sambuc static LINENUM p_input_line = 0; /* current line # from patch file */
63757e8328SLionel Sambuc static char **p_line = NULL;/* the text of the hunk */
64757e8328SLionel Sambuc static short *p_len = NULL; /* length of each line */
65757e8328SLionel Sambuc static char *p_char = NULL; /* +, -, and ! */
66757e8328SLionel Sambuc static int hunkmax = INITHUNKMAX; /* size of above arrays to begin with */
67757e8328SLionel Sambuc static int p_indent; /* indent to patch */
68757e8328SLionel Sambuc static LINENUM p_base; /* where to intuit this time */
69757e8328SLionel Sambuc static LINENUM p_bline; /* line # of p_base */
70757e8328SLionel Sambuc static LINENUM p_start; /* where intuit found a patch */
71757e8328SLionel Sambuc static LINENUM p_sline; /* and the line number for it */
72757e8328SLionel Sambuc static LINENUM p_hunk_beg; /* line number of current hunk */
73757e8328SLionel Sambuc static LINENUM p_efake = -1; /* end of faked up lines--don't free */
74757e8328SLionel Sambuc static LINENUM p_bfake = -1; /* beg of faked up lines */
75757e8328SLionel Sambuc static FILE *pfp = NULL; /* patch file pointer */
76757e8328SLionel Sambuc static char *bestguess = NULL; /* guess at correct filename */
77757e8328SLionel Sambuc
78757e8328SLionel Sambuc static void grow_hunkmax(void);
79757e8328SLionel Sambuc static int intuit_diff_type(void);
80757e8328SLionel Sambuc static void next_intuit_at(LINENUM, LINENUM);
81757e8328SLionel Sambuc static void skip_to(LINENUM, LINENUM);
82757e8328SLionel Sambuc static char *pgets(char *, int, FILE *);
83757e8328SLionel Sambuc static char *best_name(const struct file_name *, bool);
84757e8328SLionel Sambuc static char *posix_name(const struct file_name *, bool);
85757e8328SLionel Sambuc static size_t num_components(const char *);
86757e8328SLionel Sambuc
87757e8328SLionel Sambuc /*
88757e8328SLionel Sambuc * Prepare to look for the next patch in the patch file.
89757e8328SLionel Sambuc */
90757e8328SLionel Sambuc void
re_patch(void)91757e8328SLionel Sambuc re_patch(void)
92757e8328SLionel Sambuc {
93757e8328SLionel Sambuc p_first = 0;
94757e8328SLionel Sambuc p_newfirst = 0;
95757e8328SLionel Sambuc p_ptrn_lines = 0;
96757e8328SLionel Sambuc p_repl_lines = 0;
97757e8328SLionel Sambuc p_end = (LINENUM) - 1;
98757e8328SLionel Sambuc p_max = 0;
99757e8328SLionel Sambuc p_indent = 0;
100757e8328SLionel Sambuc }
101757e8328SLionel Sambuc
102757e8328SLionel Sambuc /*
103757e8328SLionel Sambuc * Open the patch file at the beginning of time.
104757e8328SLionel Sambuc */
105757e8328SLionel Sambuc void
open_patch_file(const char * filename)106757e8328SLionel Sambuc open_patch_file(const char *filename)
107757e8328SLionel Sambuc {
108757e8328SLionel Sambuc struct stat filestat;
109757e8328SLionel Sambuc
110757e8328SLionel Sambuc if (filename == NULL || *filename == '\0' || strEQ(filename, "-")) {
111757e8328SLionel Sambuc pfp = fopen(TMPPATNAME, "w");
112757e8328SLionel Sambuc if (pfp == NULL)
113757e8328SLionel Sambuc pfatal("can't create %s", TMPPATNAME);
114757e8328SLionel Sambuc while (fgets(buf, buf_len, stdin) != NULL)
115757e8328SLionel Sambuc fputs(buf, pfp);
116757e8328SLionel Sambuc if (ferror(pfp) || fclose(pfp))
117757e8328SLionel Sambuc pfatal("can't write %s", TMPPATNAME);
118757e8328SLionel Sambuc filename = TMPPATNAME;
119757e8328SLionel Sambuc }
120757e8328SLionel Sambuc pfp = fopen(filename, "r");
121757e8328SLionel Sambuc if (pfp == NULL)
122757e8328SLionel Sambuc pfatal("patch file %s not found", filename);
123757e8328SLionel Sambuc fstat(fileno(pfp), &filestat);
124757e8328SLionel Sambuc p_filesize = filestat.st_size;
125757e8328SLionel Sambuc next_intuit_at(0L, 1L); /* start at the beginning */
126757e8328SLionel Sambuc set_hunkmax();
127757e8328SLionel Sambuc }
128757e8328SLionel Sambuc
129757e8328SLionel Sambuc /*
130757e8328SLionel Sambuc * Make sure our dynamically realloced tables are malloced to begin with.
131757e8328SLionel Sambuc */
132757e8328SLionel Sambuc void
set_hunkmax(void)133757e8328SLionel Sambuc set_hunkmax(void)
134757e8328SLionel Sambuc {
135757e8328SLionel Sambuc if (p_line == NULL)
136757e8328SLionel Sambuc p_line = calloc((size_t) hunkmax, sizeof(char *));
137757e8328SLionel Sambuc if (p_len == NULL)
138757e8328SLionel Sambuc p_len = calloc((size_t) hunkmax, sizeof(short));
139757e8328SLionel Sambuc if (p_char == NULL)
140757e8328SLionel Sambuc p_char = calloc((size_t) hunkmax, sizeof(char));
141757e8328SLionel Sambuc }
142757e8328SLionel Sambuc
143757e8328SLionel Sambuc /*
144757e8328SLionel Sambuc * Enlarge the arrays containing the current hunk of patch.
145757e8328SLionel Sambuc */
146757e8328SLionel Sambuc static void
grow_hunkmax(void)147757e8328SLionel Sambuc grow_hunkmax(void)
148757e8328SLionel Sambuc {
149757e8328SLionel Sambuc int new_hunkmax;
150757e8328SLionel Sambuc char **new_p_line;
151757e8328SLionel Sambuc short *new_p_len;
152757e8328SLionel Sambuc char *new_p_char;
153757e8328SLionel Sambuc
154757e8328SLionel Sambuc new_hunkmax = hunkmax * 2;
155757e8328SLionel Sambuc
156757e8328SLionel Sambuc if (p_line == NULL || p_len == NULL || p_char == NULL)
157757e8328SLionel Sambuc fatal("Internal memory allocation error\n");
158757e8328SLionel Sambuc
159757e8328SLionel Sambuc new_p_line = realloc(p_line, new_hunkmax * sizeof(char *));
160757e8328SLionel Sambuc if (new_p_line == NULL)
161757e8328SLionel Sambuc free(p_line);
162757e8328SLionel Sambuc
163757e8328SLionel Sambuc new_p_len = realloc(p_len, new_hunkmax * sizeof(short));
164757e8328SLionel Sambuc if (new_p_len == NULL)
165757e8328SLionel Sambuc free(p_len);
166757e8328SLionel Sambuc
167757e8328SLionel Sambuc new_p_char = realloc(p_char, new_hunkmax * sizeof(char));
168757e8328SLionel Sambuc if (new_p_char == NULL)
169757e8328SLionel Sambuc free(p_char);
170757e8328SLionel Sambuc
171757e8328SLionel Sambuc p_char = new_p_char;
172757e8328SLionel Sambuc p_len = new_p_len;
173757e8328SLionel Sambuc p_line = new_p_line;
174757e8328SLionel Sambuc
175757e8328SLionel Sambuc if (p_line != NULL && p_len != NULL && p_char != NULL) {
176757e8328SLionel Sambuc hunkmax = new_hunkmax;
177757e8328SLionel Sambuc return;
178757e8328SLionel Sambuc }
179757e8328SLionel Sambuc
180757e8328SLionel Sambuc if (!using_plan_a)
181757e8328SLionel Sambuc fatal("out of memory\n");
182757e8328SLionel Sambuc out_of_mem = true; /* whatever is null will be allocated again */
183757e8328SLionel Sambuc /* from within plan_a(), of all places */
184757e8328SLionel Sambuc }
185757e8328SLionel Sambuc
186757e8328SLionel Sambuc /* True if the remainder of the patch file contains a diff of some sort. */
187757e8328SLionel Sambuc
188757e8328SLionel Sambuc bool
there_is_another_patch(void)189757e8328SLionel Sambuc there_is_another_patch(void)
190757e8328SLionel Sambuc {
191757e8328SLionel Sambuc bool exists = false;
192757e8328SLionel Sambuc
193757e8328SLionel Sambuc if (p_base != 0L && p_base >= p_filesize) {
194757e8328SLionel Sambuc if (verbose)
195757e8328SLionel Sambuc say("done\n");
196757e8328SLionel Sambuc return false;
197757e8328SLionel Sambuc }
198757e8328SLionel Sambuc if (verbose)
199757e8328SLionel Sambuc say("Hmm...");
200757e8328SLionel Sambuc diff_type = intuit_diff_type();
201757e8328SLionel Sambuc if (!diff_type) {
202757e8328SLionel Sambuc if (p_base != 0L) {
203757e8328SLionel Sambuc if (verbose)
204757e8328SLionel Sambuc say(" Ignoring the trailing garbage.\ndone\n");
205757e8328SLionel Sambuc } else
206757e8328SLionel Sambuc say(" I can't seem to find a patch in there anywhere.\n");
207757e8328SLionel Sambuc return false;
208757e8328SLionel Sambuc }
209757e8328SLionel Sambuc if (verbose)
210757e8328SLionel Sambuc say(" %sooks like %s to me...\n",
211757e8328SLionel Sambuc (p_base == 0L ? "L" : "The next patch l"),
212757e8328SLionel Sambuc diff_type == UNI_DIFF ? "a unified diff" :
213757e8328SLionel Sambuc diff_type == CONTEXT_DIFF ? "a context diff" :
214757e8328SLionel Sambuc diff_type == NEW_CONTEXT_DIFF ? "a new-style context diff" :
215757e8328SLionel Sambuc diff_type == NORMAL_DIFF ? "a normal diff" :
216757e8328SLionel Sambuc "an ed script");
217757e8328SLionel Sambuc if (p_indent && verbose)
218757e8328SLionel Sambuc say("(Patch is indented %d space%s.)\n", p_indent,
219757e8328SLionel Sambuc p_indent == 1 ? "" : "s");
220757e8328SLionel Sambuc skip_to(p_start, p_sline);
221757e8328SLionel Sambuc while (filearg[0] == NULL) {
222757e8328SLionel Sambuc if (force || batch) {
223757e8328SLionel Sambuc say("No file to patch. Skipping...\n");
224757e8328SLionel Sambuc filearg[0] = savestr(bestguess);
225757e8328SLionel Sambuc skip_rest_of_patch = true;
226757e8328SLionel Sambuc return true;
227757e8328SLionel Sambuc }
228757e8328SLionel Sambuc ask("File to patch: ");
229757e8328SLionel Sambuc if (*buf != '\n') {
230757e8328SLionel Sambuc free(bestguess);
231757e8328SLionel Sambuc bestguess = savestr(buf);
232757e8328SLionel Sambuc filearg[0] = fetchname(buf, &exists, 0);
233757e8328SLionel Sambuc }
234757e8328SLionel Sambuc if (!exists) {
235757e8328SLionel Sambuc ask("No file found--skip this patch? [n] ");
236757e8328SLionel Sambuc if (*buf != 'y')
237757e8328SLionel Sambuc continue;
238757e8328SLionel Sambuc if (verbose)
239757e8328SLionel Sambuc say("Skipping patch...\n");
240757e8328SLionel Sambuc free(filearg[0]);
241757e8328SLionel Sambuc filearg[0] = fetchname(bestguess, &exists, 0);
242757e8328SLionel Sambuc skip_rest_of_patch = true;
243757e8328SLionel Sambuc return true;
244757e8328SLionel Sambuc }
245757e8328SLionel Sambuc }
246757e8328SLionel Sambuc return true;
247757e8328SLionel Sambuc }
248757e8328SLionel Sambuc
249757e8328SLionel Sambuc /* Determine what kind of diff is in the remaining part of the patch file. */
250757e8328SLionel Sambuc
251757e8328SLionel Sambuc static int
intuit_diff_type(void)252757e8328SLionel Sambuc intuit_diff_type(void)
253757e8328SLionel Sambuc {
254757e8328SLionel Sambuc long this_line = 0, previous_line;
255757e8328SLionel Sambuc long first_command_line = -1;
256757e8328SLionel Sambuc LINENUM fcl_line = -1;
257757e8328SLionel Sambuc bool last_line_was_command = false, this_is_a_command = false;
258757e8328SLionel Sambuc bool stars_last_line = false, stars_this_line = false;
259757e8328SLionel Sambuc char *s, *t;
260757e8328SLionel Sambuc int indent, retval;
261757e8328SLionel Sambuc struct file_name names[MAX_FILE];
262757e8328SLionel Sambuc
263757e8328SLionel Sambuc memset(names, 0, sizeof(names));
264757e8328SLionel Sambuc ok_to_create_file = false;
265757e8328SLionel Sambuc fseek(pfp, p_base, SEEK_SET);
266757e8328SLionel Sambuc p_input_line = p_bline - 1;
267757e8328SLionel Sambuc for (;;) {
268757e8328SLionel Sambuc previous_line = this_line;
269757e8328SLionel Sambuc last_line_was_command = this_is_a_command;
270757e8328SLionel Sambuc stars_last_line = stars_this_line;
271757e8328SLionel Sambuc this_line = ftell(pfp);
272757e8328SLionel Sambuc indent = 0;
273757e8328SLionel Sambuc p_input_line++;
274757e8328SLionel Sambuc if (fgets(buf, buf_len, pfp) == NULL) {
275757e8328SLionel Sambuc if (first_command_line >= 0L) {
276757e8328SLionel Sambuc /* nothing but deletes!? */
277757e8328SLionel Sambuc p_start = first_command_line;
278757e8328SLionel Sambuc p_sline = fcl_line;
279757e8328SLionel Sambuc retval = ED_DIFF;
280757e8328SLionel Sambuc goto scan_exit;
281757e8328SLionel Sambuc } else {
282757e8328SLionel Sambuc p_start = this_line;
283757e8328SLionel Sambuc p_sline = p_input_line;
284757e8328SLionel Sambuc retval = 0;
285757e8328SLionel Sambuc goto scan_exit;
286757e8328SLionel Sambuc }
287757e8328SLionel Sambuc }
288757e8328SLionel Sambuc for (s = buf; *s == ' ' || *s == '\t' || *s == 'X'; s++) {
289757e8328SLionel Sambuc if (*s == '\t')
290757e8328SLionel Sambuc indent += 8 - (indent % 8);
291757e8328SLionel Sambuc else
292757e8328SLionel Sambuc indent++;
293757e8328SLionel Sambuc }
294757e8328SLionel Sambuc for (t = s; isdigit((unsigned char)*t) || *t == ','; t++)
295757e8328SLionel Sambuc ;
296757e8328SLionel Sambuc this_is_a_command = (isdigit((unsigned char)*s) &&
297757e8328SLionel Sambuc (*t == 'd' || *t == 'c' || *t == 'a'));
298757e8328SLionel Sambuc if (first_command_line < 0L && this_is_a_command) {
299757e8328SLionel Sambuc first_command_line = this_line;
300757e8328SLionel Sambuc fcl_line = p_input_line;
301757e8328SLionel Sambuc p_indent = indent; /* assume this for now */
302757e8328SLionel Sambuc }
303757e8328SLionel Sambuc if (!stars_last_line && strnEQ(s, "*** ", 4))
304757e8328SLionel Sambuc names[OLD_FILE].path = fetchname(s + 4,
305757e8328SLionel Sambuc &names[OLD_FILE].exists, strippath);
306757e8328SLionel Sambuc else if (strnEQ(s, "--- ", 4))
307757e8328SLionel Sambuc names[NEW_FILE].path = fetchname(s + 4,
308757e8328SLionel Sambuc &names[NEW_FILE].exists, strippath);
309757e8328SLionel Sambuc else if (strnEQ(s, "+++ ", 4))
310757e8328SLionel Sambuc /* pretend it is the old name */
311757e8328SLionel Sambuc names[OLD_FILE].path = fetchname(s + 4,
312757e8328SLionel Sambuc &names[OLD_FILE].exists, strippath);
313757e8328SLionel Sambuc else if (strnEQ(s, "Index:", 6))
314757e8328SLionel Sambuc names[INDEX_FILE].path = fetchname(s + 6,
315757e8328SLionel Sambuc &names[INDEX_FILE].exists, strippath);
316757e8328SLionel Sambuc else if (strnEQ(s, "Prereq:", 7)) {
317757e8328SLionel Sambuc for (t = s + 7; isspace((unsigned char)*t); t++)
318757e8328SLionel Sambuc ;
319757e8328SLionel Sambuc revision = savestr(t);
320757e8328SLionel Sambuc for (t = revision; *t && !isspace((unsigned char)*t); t++)
321757e8328SLionel Sambuc ;
322757e8328SLionel Sambuc *t = '\0';
323757e8328SLionel Sambuc if (*revision == '\0') {
324757e8328SLionel Sambuc free(revision);
325757e8328SLionel Sambuc revision = NULL;
326757e8328SLionel Sambuc }
327757e8328SLionel Sambuc }
328757e8328SLionel Sambuc if ((!diff_type || diff_type == ED_DIFF) &&
329757e8328SLionel Sambuc first_command_line >= 0L &&
330757e8328SLionel Sambuc strEQ(s, ".\n")) {
331757e8328SLionel Sambuc p_indent = indent;
332757e8328SLionel Sambuc p_start = first_command_line;
333757e8328SLionel Sambuc p_sline = fcl_line;
334757e8328SLionel Sambuc retval = ED_DIFF;
335757e8328SLionel Sambuc goto scan_exit;
336757e8328SLionel Sambuc }
337757e8328SLionel Sambuc if ((!diff_type || diff_type == UNI_DIFF) && strnEQ(s, "@@ -", 4)) {
338757e8328SLionel Sambuc if (strnEQ(s + 4, "0,0", 3))
339757e8328SLionel Sambuc ok_to_create_file = true;
340757e8328SLionel Sambuc p_indent = indent;
341757e8328SLionel Sambuc p_start = this_line;
342757e8328SLionel Sambuc p_sline = p_input_line;
343757e8328SLionel Sambuc retval = UNI_DIFF;
344757e8328SLionel Sambuc goto scan_exit;
345757e8328SLionel Sambuc }
346757e8328SLionel Sambuc stars_this_line = strnEQ(s, "********", 8);
347757e8328SLionel Sambuc if ((!diff_type || diff_type == CONTEXT_DIFF) && stars_last_line &&
348757e8328SLionel Sambuc strnEQ(s, "*** ", 4)) {
349757e8328SLionel Sambuc if (atol(s + 4) == 0)
350757e8328SLionel Sambuc ok_to_create_file = true;
351757e8328SLionel Sambuc /*
352757e8328SLionel Sambuc * If this is a new context diff the character just
353757e8328SLionel Sambuc * before the newline is a '*'.
354757e8328SLionel Sambuc */
355757e8328SLionel Sambuc while (*s != '\n')
356757e8328SLionel Sambuc s++;
357757e8328SLionel Sambuc p_indent = indent;
358757e8328SLionel Sambuc p_start = previous_line;
359757e8328SLionel Sambuc p_sline = p_input_line - 1;
360757e8328SLionel Sambuc retval = (*(s - 1) == '*' ? NEW_CONTEXT_DIFF : CONTEXT_DIFF);
361757e8328SLionel Sambuc goto scan_exit;
362757e8328SLionel Sambuc }
363757e8328SLionel Sambuc if ((!diff_type || diff_type == NORMAL_DIFF) &&
364757e8328SLionel Sambuc last_line_was_command &&
365757e8328SLionel Sambuc (strnEQ(s, "< ", 2) || strnEQ(s, "> ", 2))) {
366757e8328SLionel Sambuc p_start = previous_line;
367757e8328SLionel Sambuc p_sline = p_input_line - 1;
368757e8328SLionel Sambuc p_indent = indent;
369757e8328SLionel Sambuc retval = NORMAL_DIFF;
370757e8328SLionel Sambuc goto scan_exit;
371757e8328SLionel Sambuc }
372757e8328SLionel Sambuc }
373757e8328SLionel Sambuc scan_exit:
374757e8328SLionel Sambuc if (retval == UNI_DIFF) {
375757e8328SLionel Sambuc /* unswap old and new */
376757e8328SLionel Sambuc struct file_name tmp = names[OLD_FILE];
377757e8328SLionel Sambuc names[OLD_FILE] = names[NEW_FILE];
378757e8328SLionel Sambuc names[NEW_FILE] = tmp;
379757e8328SLionel Sambuc }
380757e8328SLionel Sambuc if (filearg[0] == NULL) {
381757e8328SLionel Sambuc if (posix)
382757e8328SLionel Sambuc filearg[0] = posix_name(names, ok_to_create_file);
383757e8328SLionel Sambuc else {
384757e8328SLionel Sambuc /* Ignore the Index: name for context diffs, like GNU */
385757e8328SLionel Sambuc if (names[OLD_FILE].path != NULL ||
386757e8328SLionel Sambuc names[NEW_FILE].path != NULL) {
387757e8328SLionel Sambuc free(names[INDEX_FILE].path);
388757e8328SLionel Sambuc names[INDEX_FILE].path = NULL;
389757e8328SLionel Sambuc }
390757e8328SLionel Sambuc filearg[0] = best_name(names, ok_to_create_file);
391757e8328SLionel Sambuc }
392757e8328SLionel Sambuc }
393757e8328SLionel Sambuc
394757e8328SLionel Sambuc free(bestguess);
395757e8328SLionel Sambuc bestguess = NULL;
396757e8328SLionel Sambuc if (filearg[0] != NULL)
397757e8328SLionel Sambuc bestguess = savestr(filearg[0]);
398757e8328SLionel Sambuc else if (!ok_to_create_file) {
399757e8328SLionel Sambuc /*
400757e8328SLionel Sambuc * We don't want to create a new file but we need a
401757e8328SLionel Sambuc * filename to set bestguess. Avoid setting filearg[0]
402757e8328SLionel Sambuc * so the file is not created automatically.
403757e8328SLionel Sambuc */
404757e8328SLionel Sambuc if (posix)
405757e8328SLionel Sambuc bestguess = posix_name(names, true);
406757e8328SLionel Sambuc else
407757e8328SLionel Sambuc bestguess = best_name(names, true);
408757e8328SLionel Sambuc }
409757e8328SLionel Sambuc free(names[OLD_FILE].path);
410757e8328SLionel Sambuc free(names[NEW_FILE].path);
411757e8328SLionel Sambuc free(names[INDEX_FILE].path);
412757e8328SLionel Sambuc return retval;
413757e8328SLionel Sambuc }
414757e8328SLionel Sambuc
415757e8328SLionel Sambuc /*
416757e8328SLionel Sambuc * Remember where this patch ends so we know where to start up again.
417757e8328SLionel Sambuc */
418757e8328SLionel Sambuc static void
next_intuit_at(LINENUM file_pos,LINENUM file_line)419757e8328SLionel Sambuc next_intuit_at(LINENUM file_pos, LINENUM file_line)
420757e8328SLionel Sambuc {
421757e8328SLionel Sambuc p_base = file_pos;
422757e8328SLionel Sambuc p_bline = file_line;
423757e8328SLionel Sambuc }
424757e8328SLionel Sambuc
425757e8328SLionel Sambuc /*
426757e8328SLionel Sambuc * Basically a verbose fseek() to the actual diff listing.
427757e8328SLionel Sambuc */
428757e8328SLionel Sambuc static void
skip_to(LINENUM file_pos,LINENUM file_line)429757e8328SLionel Sambuc skip_to(LINENUM file_pos, LINENUM file_line)
430757e8328SLionel Sambuc {
431757e8328SLionel Sambuc char *ret;
432757e8328SLionel Sambuc
433757e8328SLionel Sambuc if (p_base > file_pos)
434757e8328SLionel Sambuc fatal("Internal error: seek %ld>%ld\n", p_base, file_pos);
435757e8328SLionel Sambuc if (verbose && p_base < file_pos) {
436757e8328SLionel Sambuc fseek(pfp, p_base, SEEK_SET);
437757e8328SLionel Sambuc say("The text leading up to this was:\n--------------------------\n");
438757e8328SLionel Sambuc while (ftell(pfp) < file_pos) {
439757e8328SLionel Sambuc ret = fgets(buf, buf_len, pfp);
440757e8328SLionel Sambuc if (ret == NULL)
441757e8328SLionel Sambuc fatal("Unexpected end of file\n");
442757e8328SLionel Sambuc say("|%s", buf);
443757e8328SLionel Sambuc }
444757e8328SLionel Sambuc say("--------------------------\n");
445757e8328SLionel Sambuc } else
446757e8328SLionel Sambuc fseek(pfp, file_pos, SEEK_SET);
447757e8328SLionel Sambuc p_input_line = file_line - 1;
448757e8328SLionel Sambuc }
449757e8328SLionel Sambuc
450757e8328SLionel Sambuc /* Make this a function for better debugging. */
451757e8328SLionel Sambuc __dead static void
malformed(void)452757e8328SLionel Sambuc malformed(void)
453757e8328SLionel Sambuc {
454757e8328SLionel Sambuc fatal("malformed patch at line %ld: %s", p_input_line, buf);
455757e8328SLionel Sambuc /* about as informative as "Syntax error" in C */
456757e8328SLionel Sambuc }
457757e8328SLionel Sambuc
458*0a6a1f1dSLionel Sambuc static LINENUM
getlinenum(const char * s)459*0a6a1f1dSLionel Sambuc getlinenum(const char *s)
460*0a6a1f1dSLionel Sambuc {
461*0a6a1f1dSLionel Sambuc LINENUM l = (LINENUM)atol(s);
462*0a6a1f1dSLionel Sambuc if (l < 0) {
463*0a6a1f1dSLionel Sambuc l = 0;
464*0a6a1f1dSLionel Sambuc malformed();
465*0a6a1f1dSLionel Sambuc }
466*0a6a1f1dSLionel Sambuc return l;
467*0a6a1f1dSLionel Sambuc }
468*0a6a1f1dSLionel Sambuc
469*0a6a1f1dSLionel Sambuc static LINENUM
getskiplinenum(char ** p)470*0a6a1f1dSLionel Sambuc getskiplinenum(char **p)
471*0a6a1f1dSLionel Sambuc {
472*0a6a1f1dSLionel Sambuc char *s = *p;
473*0a6a1f1dSLionel Sambuc LINENUM l = getlinenum(s);
474*0a6a1f1dSLionel Sambuc while (isdigit((unsigned char)*s))
475*0a6a1f1dSLionel Sambuc s++;
476*0a6a1f1dSLionel Sambuc *p = s;
477*0a6a1f1dSLionel Sambuc return l;
478*0a6a1f1dSLionel Sambuc }
479*0a6a1f1dSLionel Sambuc
480757e8328SLionel Sambuc /*
481757e8328SLionel Sambuc * True if the line has been discarded (i.e., it is a line saying
482757e8328SLionel Sambuc * "\ No newline at end of file".)
483757e8328SLionel Sambuc */
484757e8328SLionel Sambuc static bool
remove_special_line(void)485757e8328SLionel Sambuc remove_special_line(void)
486757e8328SLionel Sambuc {
487757e8328SLionel Sambuc int c;
488757e8328SLionel Sambuc
489757e8328SLionel Sambuc c = fgetc(pfp);
490757e8328SLionel Sambuc if (c == '\\') {
491757e8328SLionel Sambuc do {
492757e8328SLionel Sambuc c = fgetc(pfp);
493757e8328SLionel Sambuc } while (c != EOF && c != '\n');
494757e8328SLionel Sambuc
495757e8328SLionel Sambuc return true;
496757e8328SLionel Sambuc }
497757e8328SLionel Sambuc if (c != EOF)
498757e8328SLionel Sambuc fseek(pfp, -1L, SEEK_CUR);
499757e8328SLionel Sambuc
500757e8328SLionel Sambuc return false;
501757e8328SLionel Sambuc }
502757e8328SLionel Sambuc
503757e8328SLionel Sambuc /*
504757e8328SLionel Sambuc * True if there is more of the current diff listing to process.
505757e8328SLionel Sambuc */
506757e8328SLionel Sambuc bool
another_hunk(void)507757e8328SLionel Sambuc another_hunk(void)
508757e8328SLionel Sambuc {
509757e8328SLionel Sambuc long line_beginning; /* file pos of the current line */
510757e8328SLionel Sambuc LINENUM repl_beginning; /* index of --- line */
511757e8328SLionel Sambuc LINENUM fillcnt; /* #lines of missing ptrn or repl */
512757e8328SLionel Sambuc LINENUM fillsrc; /* index of first line to copy */
513757e8328SLionel Sambuc LINENUM filldst; /* index of first missing line */
514757e8328SLionel Sambuc bool ptrn_spaces_eaten; /* ptrn was slightly misformed */
515757e8328SLionel Sambuc bool repl_could_be_missing; /* no + or ! lines in this hunk */
516757e8328SLionel Sambuc bool repl_missing; /* we are now backtracking */
517757e8328SLionel Sambuc long repl_backtrack_position; /* file pos of first repl line */
518757e8328SLionel Sambuc LINENUM repl_patch_line; /* input line number for same */
519757e8328SLionel Sambuc LINENUM ptrn_copiable; /* # of copiable lines in ptrn */
520757e8328SLionel Sambuc char *s, *ret;
521757e8328SLionel Sambuc int context = 0;
522757e8328SLionel Sambuc
523757e8328SLionel Sambuc while (p_end >= 0) {
524757e8328SLionel Sambuc if (p_end == p_efake)
525757e8328SLionel Sambuc p_end = p_bfake; /* don't free twice */
526757e8328SLionel Sambuc else
527757e8328SLionel Sambuc free(p_line[p_end]);
528757e8328SLionel Sambuc p_end--;
529757e8328SLionel Sambuc }
530757e8328SLionel Sambuc p_efake = -1;
531757e8328SLionel Sambuc
532757e8328SLionel Sambuc p_max = hunkmax; /* gets reduced when --- found */
533757e8328SLionel Sambuc if (diff_type == CONTEXT_DIFF || diff_type == NEW_CONTEXT_DIFF) {
534757e8328SLionel Sambuc line_beginning = ftell(pfp);
535757e8328SLionel Sambuc repl_beginning = 0;
536757e8328SLionel Sambuc fillcnt = 0;
537757e8328SLionel Sambuc fillsrc = 0;
538757e8328SLionel Sambuc filldst = 0;
539757e8328SLionel Sambuc ptrn_spaces_eaten = false;
540757e8328SLionel Sambuc repl_could_be_missing = true;
541757e8328SLionel Sambuc repl_missing = false;
542757e8328SLionel Sambuc repl_backtrack_position = 0;
543757e8328SLionel Sambuc repl_patch_line = 0;
544757e8328SLionel Sambuc ptrn_copiable = 0;
545757e8328SLionel Sambuc
546757e8328SLionel Sambuc ret = pgets(buf, buf_len, pfp);
547757e8328SLionel Sambuc p_input_line++;
548757e8328SLionel Sambuc if (ret == NULL || strnNE(buf, "********", 8)) {
549757e8328SLionel Sambuc next_intuit_at(line_beginning, p_input_line);
550757e8328SLionel Sambuc return false;
551757e8328SLionel Sambuc }
552757e8328SLionel Sambuc p_context = 100;
553757e8328SLionel Sambuc p_hunk_beg = p_input_line + 1;
554757e8328SLionel Sambuc while (p_end < p_max) {
555757e8328SLionel Sambuc line_beginning = ftell(pfp);
556757e8328SLionel Sambuc ret = pgets(buf, buf_len, pfp);
557757e8328SLionel Sambuc p_input_line++;
558757e8328SLionel Sambuc if (ret == NULL) {
559757e8328SLionel Sambuc if (p_max - p_end < 4) {
560757e8328SLionel Sambuc /* assume blank lines got chopped */
561757e8328SLionel Sambuc strlcpy(buf, " \n", buf_len);
562757e8328SLionel Sambuc } else {
563757e8328SLionel Sambuc if (repl_beginning && repl_could_be_missing) {
564757e8328SLionel Sambuc repl_missing = true;
565757e8328SLionel Sambuc goto hunk_done;
566757e8328SLionel Sambuc }
567757e8328SLionel Sambuc fatal("unexpected end of file in patch\n");
568757e8328SLionel Sambuc }
569757e8328SLionel Sambuc }
570757e8328SLionel Sambuc p_end++;
571757e8328SLionel Sambuc if (p_end >= hunkmax)
572757e8328SLionel Sambuc fatal("Internal error: hunk larger than hunk "
573757e8328SLionel Sambuc "buffer size");
574757e8328SLionel Sambuc p_char[p_end] = *buf;
575757e8328SLionel Sambuc p_line[p_end] = NULL;
576757e8328SLionel Sambuc switch (*buf) {
577757e8328SLionel Sambuc case '*':
578757e8328SLionel Sambuc if (strnEQ(buf, "********", 8)) {
579757e8328SLionel Sambuc if (repl_beginning && repl_could_be_missing) {
580757e8328SLionel Sambuc repl_missing = true;
581757e8328SLionel Sambuc goto hunk_done;
582757e8328SLionel Sambuc } else
583757e8328SLionel Sambuc fatal("unexpected end of hunk "
584757e8328SLionel Sambuc "at line %ld\n",
585757e8328SLionel Sambuc p_input_line);
586757e8328SLionel Sambuc }
587757e8328SLionel Sambuc if (p_end != 0) {
588757e8328SLionel Sambuc if (repl_beginning && repl_could_be_missing) {
589757e8328SLionel Sambuc repl_missing = true;
590757e8328SLionel Sambuc goto hunk_done;
591757e8328SLionel Sambuc }
592757e8328SLionel Sambuc fatal("unexpected *** at line %ld: %s",
593757e8328SLionel Sambuc p_input_line, buf);
594757e8328SLionel Sambuc }
595757e8328SLionel Sambuc context = 0;
596757e8328SLionel Sambuc p_line[p_end] = savestr(buf);
597757e8328SLionel Sambuc if (out_of_mem) {
598757e8328SLionel Sambuc p_end--;
599757e8328SLionel Sambuc return false;
600757e8328SLionel Sambuc }
601757e8328SLionel Sambuc for (s = buf; *s && !isdigit((unsigned char)*s); s++)
602757e8328SLionel Sambuc ;
603757e8328SLionel Sambuc if (!*s)
604757e8328SLionel Sambuc malformed();
605757e8328SLionel Sambuc if (strnEQ(s, "0,0", 3))
606757e8328SLionel Sambuc memmove(s, s + 2, strlen(s + 2) + 1);
607*0a6a1f1dSLionel Sambuc p_first = getskiplinenum(&s);
608757e8328SLionel Sambuc if (*s == ',') {
609757e8328SLionel Sambuc for (; *s && !isdigit((unsigned char)*s); s++)
610757e8328SLionel Sambuc ;
611757e8328SLionel Sambuc if (!*s)
612757e8328SLionel Sambuc malformed();
613*0a6a1f1dSLionel Sambuc p_ptrn_lines = (getlinenum(s)) - p_first + 1;
614*0a6a1f1dSLionel Sambuc if (p_ptrn_lines < 0)
615*0a6a1f1dSLionel Sambuc malformed();
616757e8328SLionel Sambuc } else if (p_first)
617757e8328SLionel Sambuc p_ptrn_lines = 1;
618757e8328SLionel Sambuc else {
619757e8328SLionel Sambuc p_ptrn_lines = 0;
620757e8328SLionel Sambuc p_first = 1;
621757e8328SLionel Sambuc }
622*0a6a1f1dSLionel Sambuc if (p_first >= LINENUM_MAX - p_ptrn_lines ||
623*0a6a1f1dSLionel Sambuc p_ptrn_lines >= LINENUM_MAX - 6)
624*0a6a1f1dSLionel Sambuc malformed();
625757e8328SLionel Sambuc
626757e8328SLionel Sambuc /* we need this much at least */
627757e8328SLionel Sambuc p_max = p_ptrn_lines + 6;
628757e8328SLionel Sambuc while (p_max >= hunkmax)
629757e8328SLionel Sambuc grow_hunkmax();
630757e8328SLionel Sambuc p_max = hunkmax;
631757e8328SLionel Sambuc break;
632757e8328SLionel Sambuc case '-':
633757e8328SLionel Sambuc if (buf[1] == '-') {
634757e8328SLionel Sambuc if (repl_beginning ||
635757e8328SLionel Sambuc (p_end != p_ptrn_lines + 1 +
636757e8328SLionel Sambuc (p_char[p_end - 1] == '\n'))) {
637757e8328SLionel Sambuc if (p_end == 1) {
638757e8328SLionel Sambuc /*
639757e8328SLionel Sambuc * `old' lines were omitted;
640757e8328SLionel Sambuc * set up to fill them in
641757e8328SLionel Sambuc * from 'new' context lines.
642757e8328SLionel Sambuc */
643757e8328SLionel Sambuc p_end = p_ptrn_lines + 1;
644757e8328SLionel Sambuc fillsrc = p_end + 1;
645757e8328SLionel Sambuc filldst = 1;
646757e8328SLionel Sambuc fillcnt = p_ptrn_lines;
647757e8328SLionel Sambuc } else {
648757e8328SLionel Sambuc if (repl_beginning) {
649757e8328SLionel Sambuc if (repl_could_be_missing) {
650757e8328SLionel Sambuc repl_missing = true;
651757e8328SLionel Sambuc goto hunk_done;
652757e8328SLionel Sambuc }
653757e8328SLionel Sambuc fatal("duplicate \"---\" at line %ld--check line numbers at line %ld\n",
654757e8328SLionel Sambuc p_input_line, p_hunk_beg + repl_beginning);
655757e8328SLionel Sambuc } else {
656757e8328SLionel Sambuc fatal("%s \"---\" at line %ld--check line numbers at line %ld\n",
657757e8328SLionel Sambuc (p_end <= p_ptrn_lines
658757e8328SLionel Sambuc ? "Premature"
659757e8328SLionel Sambuc : "Overdue"),
660757e8328SLionel Sambuc p_input_line, p_hunk_beg);
661757e8328SLionel Sambuc }
662757e8328SLionel Sambuc }
663757e8328SLionel Sambuc }
664757e8328SLionel Sambuc repl_beginning = p_end;
665757e8328SLionel Sambuc repl_backtrack_position = ftell(pfp);
666757e8328SLionel Sambuc repl_patch_line = p_input_line;
667757e8328SLionel Sambuc p_line[p_end] = savestr(buf);
668757e8328SLionel Sambuc if (out_of_mem) {
669757e8328SLionel Sambuc p_end--;
670757e8328SLionel Sambuc return false;
671757e8328SLionel Sambuc }
672757e8328SLionel Sambuc p_char[p_end] = '=';
673757e8328SLionel Sambuc for (s = buf; *s && !isdigit((unsigned char)*s); s++)
674757e8328SLionel Sambuc ;
675757e8328SLionel Sambuc if (!*s)
676757e8328SLionel Sambuc malformed();
677*0a6a1f1dSLionel Sambuc p_newfirst = getskiplinenum(&s);
678757e8328SLionel Sambuc if (*s == ',') {
679757e8328SLionel Sambuc for (; *s && !isdigit((unsigned char)*s); s++)
680757e8328SLionel Sambuc ;
681757e8328SLionel Sambuc if (!*s)
682757e8328SLionel Sambuc malformed();
683*0a6a1f1dSLionel Sambuc p_repl_lines = (getlinenum(s)) -
684757e8328SLionel Sambuc p_newfirst + 1;
685*0a6a1f1dSLionel Sambuc if (p_repl_lines < 0)
686*0a6a1f1dSLionel Sambuc malformed();
687757e8328SLionel Sambuc } else if (p_newfirst)
688757e8328SLionel Sambuc p_repl_lines = 1;
689757e8328SLionel Sambuc else {
690757e8328SLionel Sambuc p_repl_lines = 0;
691757e8328SLionel Sambuc p_newfirst = 1;
692757e8328SLionel Sambuc }
693*0a6a1f1dSLionel Sambuc if (p_newfirst >= LINENUM_MAX - p_repl_lines ||
694*0a6a1f1dSLionel Sambuc p_repl_lines >= LINENUM_MAX - p_end)
695*0a6a1f1dSLionel Sambuc malformed();
696757e8328SLionel Sambuc p_max = p_repl_lines + p_end;
697757e8328SLionel Sambuc if (p_max > MAXHUNKSIZE)
698757e8328SLionel Sambuc fatal("hunk too large (%ld lines) at line %ld: %s",
699757e8328SLionel Sambuc p_max, p_input_line, buf);
700757e8328SLionel Sambuc while (p_max >= hunkmax)
701757e8328SLionel Sambuc grow_hunkmax();
702757e8328SLionel Sambuc if (p_repl_lines != ptrn_copiable &&
703757e8328SLionel Sambuc (p_context != 0 || p_repl_lines != 1))
704757e8328SLionel Sambuc repl_could_be_missing = false;
705757e8328SLionel Sambuc break;
706757e8328SLionel Sambuc }
707757e8328SLionel Sambuc goto change_line;
708757e8328SLionel Sambuc case '+':
709757e8328SLionel Sambuc case '!':
710757e8328SLionel Sambuc repl_could_be_missing = false;
711757e8328SLionel Sambuc change_line:
712757e8328SLionel Sambuc if (buf[1] == '\n' && canonicalize)
713757e8328SLionel Sambuc strlcpy(buf + 1, " \n", buf_len - 1);
714757e8328SLionel Sambuc if (!isspace((unsigned char)buf[1]) && buf[1] != '>' &&
715757e8328SLionel Sambuc buf[1] != '<' &&
716757e8328SLionel Sambuc repl_beginning && repl_could_be_missing) {
717757e8328SLionel Sambuc repl_missing = true;
718757e8328SLionel Sambuc goto hunk_done;
719757e8328SLionel Sambuc }
720757e8328SLionel Sambuc if (context >= 0) {
721757e8328SLionel Sambuc if (context < p_context)
722757e8328SLionel Sambuc p_context = context;
723757e8328SLionel Sambuc context = -1000;
724757e8328SLionel Sambuc }
725757e8328SLionel Sambuc p_line[p_end] = savestr(buf + 2);
726757e8328SLionel Sambuc if (out_of_mem) {
727757e8328SLionel Sambuc p_end--;
728757e8328SLionel Sambuc return false;
729757e8328SLionel Sambuc }
730757e8328SLionel Sambuc if (p_end == p_ptrn_lines) {
731757e8328SLionel Sambuc if (remove_special_line()) {
732757e8328SLionel Sambuc int len;
733757e8328SLionel Sambuc
734757e8328SLionel Sambuc len = strlen(p_line[p_end]) - 1;
735757e8328SLionel Sambuc (p_line[p_end])[len] = 0;
736757e8328SLionel Sambuc }
737757e8328SLionel Sambuc }
738757e8328SLionel Sambuc break;
739757e8328SLionel Sambuc case '\t':
740757e8328SLionel Sambuc case '\n': /* assume the 2 spaces got eaten */
741757e8328SLionel Sambuc if (repl_beginning && repl_could_be_missing &&
742757e8328SLionel Sambuc (!ptrn_spaces_eaten ||
743757e8328SLionel Sambuc diff_type == NEW_CONTEXT_DIFF)) {
744757e8328SLionel Sambuc repl_missing = true;
745757e8328SLionel Sambuc goto hunk_done;
746757e8328SLionel Sambuc }
747757e8328SLionel Sambuc p_line[p_end] = savestr(buf);
748757e8328SLionel Sambuc if (out_of_mem) {
749757e8328SLionel Sambuc p_end--;
750757e8328SLionel Sambuc return false;
751757e8328SLionel Sambuc }
752757e8328SLionel Sambuc if (p_end != p_ptrn_lines + 1) {
753757e8328SLionel Sambuc ptrn_spaces_eaten |= (repl_beginning != 0);
754757e8328SLionel Sambuc context++;
755757e8328SLionel Sambuc if (!repl_beginning)
756757e8328SLionel Sambuc ptrn_copiable++;
757757e8328SLionel Sambuc p_char[p_end] = ' ';
758757e8328SLionel Sambuc }
759757e8328SLionel Sambuc break;
760757e8328SLionel Sambuc case ' ':
761757e8328SLionel Sambuc if (!isspace((unsigned char)buf[1]) &&
762757e8328SLionel Sambuc repl_beginning && repl_could_be_missing) {
763757e8328SLionel Sambuc repl_missing = true;
764757e8328SLionel Sambuc goto hunk_done;
765757e8328SLionel Sambuc }
766757e8328SLionel Sambuc context++;
767757e8328SLionel Sambuc if (!repl_beginning)
768757e8328SLionel Sambuc ptrn_copiable++;
769757e8328SLionel Sambuc p_line[p_end] = savestr(buf + 2);
770757e8328SLionel Sambuc if (out_of_mem) {
771757e8328SLionel Sambuc p_end--;
772757e8328SLionel Sambuc return false;
773757e8328SLionel Sambuc }
774757e8328SLionel Sambuc break;
775757e8328SLionel Sambuc default:
776757e8328SLionel Sambuc if (repl_beginning && repl_could_be_missing) {
777757e8328SLionel Sambuc repl_missing = true;
778757e8328SLionel Sambuc goto hunk_done;
779757e8328SLionel Sambuc }
780757e8328SLionel Sambuc malformed();
781757e8328SLionel Sambuc }
782757e8328SLionel Sambuc /* set up p_len for strncmp() so we don't have to */
783757e8328SLionel Sambuc /* assume null termination */
784757e8328SLionel Sambuc if (p_line[p_end])
785757e8328SLionel Sambuc p_len[p_end] = strlen(p_line[p_end]);
786757e8328SLionel Sambuc else
787757e8328SLionel Sambuc p_len[p_end] = 0;
788757e8328SLionel Sambuc }
789757e8328SLionel Sambuc
790757e8328SLionel Sambuc hunk_done:
791757e8328SLionel Sambuc if (p_end >= 0 && !repl_beginning)
792757e8328SLionel Sambuc fatal("no --- found in patch at line %ld\n", pch_hunk_beg());
793757e8328SLionel Sambuc
794757e8328SLionel Sambuc if (repl_missing) {
795757e8328SLionel Sambuc
796757e8328SLionel Sambuc /* reset state back to just after --- */
797757e8328SLionel Sambuc p_input_line = repl_patch_line;
798757e8328SLionel Sambuc for (p_end--; p_end > repl_beginning; p_end--)
799757e8328SLionel Sambuc free(p_line[p_end]);
800757e8328SLionel Sambuc fseek(pfp, repl_backtrack_position, SEEK_SET);
801757e8328SLionel Sambuc
802757e8328SLionel Sambuc /* redundant 'new' context lines were omitted - set */
803757e8328SLionel Sambuc /* up to fill them in from the old file context */
804757e8328SLionel Sambuc if (!p_context && p_repl_lines == 1) {
805757e8328SLionel Sambuc p_repl_lines = 0;
806757e8328SLionel Sambuc p_max--;
807757e8328SLionel Sambuc }
808757e8328SLionel Sambuc fillsrc = 1;
809757e8328SLionel Sambuc filldst = repl_beginning + 1;
810757e8328SLionel Sambuc fillcnt = p_repl_lines;
811757e8328SLionel Sambuc p_end = p_max;
812757e8328SLionel Sambuc } else if (!p_context && fillcnt == 1) {
813757e8328SLionel Sambuc /* the first hunk was a null hunk with no context */
814757e8328SLionel Sambuc /* and we were expecting one line -- fix it up. */
815757e8328SLionel Sambuc while (filldst < p_end) {
816757e8328SLionel Sambuc p_line[filldst] = p_line[filldst + 1];
817757e8328SLionel Sambuc p_char[filldst] = p_char[filldst + 1];
818757e8328SLionel Sambuc p_len[filldst] = p_len[filldst + 1];
819757e8328SLionel Sambuc filldst++;
820757e8328SLionel Sambuc }
821757e8328SLionel Sambuc #if 0
822757e8328SLionel Sambuc repl_beginning--; /* this doesn't need to be fixed */
823757e8328SLionel Sambuc #endif
824757e8328SLionel Sambuc p_end--;
825757e8328SLionel Sambuc p_first++; /* do append rather than insert */
826757e8328SLionel Sambuc fillcnt = 0;
827757e8328SLionel Sambuc p_ptrn_lines = 0;
828757e8328SLionel Sambuc }
829757e8328SLionel Sambuc if (diff_type == CONTEXT_DIFF &&
830757e8328SLionel Sambuc (fillcnt || (p_first > 1 && ptrn_copiable > 2 * p_context))) {
831757e8328SLionel Sambuc if (verbose)
832757e8328SLionel Sambuc say("%s\n%s\n%s\n",
833757e8328SLionel Sambuc "(Fascinating--this is really a new-style context diff but without",
834757e8328SLionel Sambuc "the telltale extra asterisks on the *** line that usually indicate",
835757e8328SLionel Sambuc "the new style...)");
836757e8328SLionel Sambuc diff_type = NEW_CONTEXT_DIFF;
837757e8328SLionel Sambuc }
838757e8328SLionel Sambuc /* if there were omitted context lines, fill them in now */
839757e8328SLionel Sambuc if (fillcnt) {
840757e8328SLionel Sambuc p_bfake = filldst; /* remember where not to free() */
841757e8328SLionel Sambuc p_efake = filldst + fillcnt - 1;
842757e8328SLionel Sambuc while (fillcnt-- > 0) {
843757e8328SLionel Sambuc while (fillsrc <= p_end && p_char[fillsrc] != ' ')
844757e8328SLionel Sambuc fillsrc++;
845757e8328SLionel Sambuc if (fillsrc > p_end)
846757e8328SLionel Sambuc fatal("replacement text or line numbers mangled in hunk at line %ld\n",
847757e8328SLionel Sambuc p_hunk_beg);
848757e8328SLionel Sambuc p_line[filldst] = p_line[fillsrc];
849757e8328SLionel Sambuc p_char[filldst] = p_char[fillsrc];
850757e8328SLionel Sambuc p_len[filldst] = p_len[fillsrc];
851757e8328SLionel Sambuc fillsrc++;
852757e8328SLionel Sambuc filldst++;
853757e8328SLionel Sambuc }
854757e8328SLionel Sambuc while (fillsrc <= p_end && fillsrc != repl_beginning &&
855757e8328SLionel Sambuc p_char[fillsrc] != ' ')
856757e8328SLionel Sambuc fillsrc++;
857757e8328SLionel Sambuc #ifdef DEBUGGING
858757e8328SLionel Sambuc if (debug & 64)
859757e8328SLionel Sambuc printf("fillsrc %ld, filldst %ld, rb %ld, e+1 %ld\n",
860757e8328SLionel Sambuc fillsrc, filldst, repl_beginning, p_end + 1);
861757e8328SLionel Sambuc #endif
862757e8328SLionel Sambuc if (fillsrc != p_end + 1 && fillsrc != repl_beginning)
863757e8328SLionel Sambuc malformed();
864757e8328SLionel Sambuc if (filldst != p_end + 1 && filldst != repl_beginning)
865757e8328SLionel Sambuc malformed();
866757e8328SLionel Sambuc }
867757e8328SLionel Sambuc if (p_line[p_end] != NULL) {
868757e8328SLionel Sambuc if (remove_special_line()) {
869757e8328SLionel Sambuc p_len[p_end] -= 1;
870757e8328SLionel Sambuc (p_line[p_end])[p_len[p_end]] = 0;
871757e8328SLionel Sambuc }
872757e8328SLionel Sambuc }
873757e8328SLionel Sambuc } else if (diff_type == UNI_DIFF) {
874757e8328SLionel Sambuc LINENUM fillold; /* index of old lines */
875757e8328SLionel Sambuc LINENUM fillnew; /* index of new lines */
876757e8328SLionel Sambuc char ch;
877757e8328SLionel Sambuc
878757e8328SLionel Sambuc line_beginning = ftell(pfp); /* file pos of the current line */
879757e8328SLionel Sambuc ret = pgets(buf, buf_len, pfp);
880757e8328SLionel Sambuc p_input_line++;
881757e8328SLionel Sambuc if (ret == NULL || strnNE(buf, "@@ -", 4)) {
882757e8328SLionel Sambuc next_intuit_at(line_beginning, p_input_line);
883757e8328SLionel Sambuc return false;
884757e8328SLionel Sambuc }
885757e8328SLionel Sambuc s = buf + 4;
886757e8328SLionel Sambuc if (!*s)
887757e8328SLionel Sambuc malformed();
888*0a6a1f1dSLionel Sambuc p_first = getskiplinenum(&s);
889757e8328SLionel Sambuc if (*s == ',') {
890757e8328SLionel Sambuc s++;
891*0a6a1f1dSLionel Sambuc p_ptrn_lines = getskiplinenum(&s);
892757e8328SLionel Sambuc } else
893757e8328SLionel Sambuc p_ptrn_lines = 1;
894*0a6a1f1dSLionel Sambuc if (p_first >= LINENUM_MAX - p_ptrn_lines)
895*0a6a1f1dSLionel Sambuc malformed();
896757e8328SLionel Sambuc if (*s == ' ')
897757e8328SLionel Sambuc s++;
898757e8328SLionel Sambuc if (*s != '+' || !*++s)
899757e8328SLionel Sambuc malformed();
900*0a6a1f1dSLionel Sambuc p_newfirst = getskiplinenum(&s);
901757e8328SLionel Sambuc if (*s == ',') {
902757e8328SLionel Sambuc s++;
903*0a6a1f1dSLionel Sambuc p_repl_lines = getskiplinenum(&s);
904757e8328SLionel Sambuc } else
905757e8328SLionel Sambuc p_repl_lines = 1;
906757e8328SLionel Sambuc if (*s == ' ')
907757e8328SLionel Sambuc s++;
908757e8328SLionel Sambuc if (*s != '@')
909757e8328SLionel Sambuc malformed();
910*0a6a1f1dSLionel Sambuc if (p_first >= LINENUM_MAX - p_ptrn_lines ||
911*0a6a1f1dSLionel Sambuc p_newfirst > LINENUM_MAX - p_repl_lines ||
912*0a6a1f1dSLionel Sambuc p_ptrn_lines >= LINENUM_MAX - p_repl_lines - 1)
913*0a6a1f1dSLionel Sambuc malformed();
914757e8328SLionel Sambuc if (!p_ptrn_lines)
915757e8328SLionel Sambuc p_first++; /* do append rather than insert */
916757e8328SLionel Sambuc p_max = p_ptrn_lines + p_repl_lines + 1;
917757e8328SLionel Sambuc while (p_max >= hunkmax)
918757e8328SLionel Sambuc grow_hunkmax();
919757e8328SLionel Sambuc fillold = 1;
920757e8328SLionel Sambuc fillnew = fillold + p_ptrn_lines;
921757e8328SLionel Sambuc p_end = fillnew + p_repl_lines;
922757e8328SLionel Sambuc snprintf(buf, buf_len, "*** %ld,%ld ****\n", p_first,
923757e8328SLionel Sambuc p_first + p_ptrn_lines - 1);
924757e8328SLionel Sambuc p_line[0] = savestr(buf);
925757e8328SLionel Sambuc if (out_of_mem) {
926757e8328SLionel Sambuc p_end = -1;
927757e8328SLionel Sambuc return false;
928757e8328SLionel Sambuc }
929757e8328SLionel Sambuc p_char[0] = '*';
930757e8328SLionel Sambuc snprintf(buf, buf_len, "--- %ld,%ld ----\n", p_newfirst,
931757e8328SLionel Sambuc p_newfirst + p_repl_lines - 1);
932757e8328SLionel Sambuc p_line[fillnew] = savestr(buf);
933757e8328SLionel Sambuc if (out_of_mem) {
934757e8328SLionel Sambuc p_end = 0;
935757e8328SLionel Sambuc return false;
936757e8328SLionel Sambuc }
937757e8328SLionel Sambuc p_char[fillnew++] = '=';
938757e8328SLionel Sambuc p_context = 100;
939757e8328SLionel Sambuc context = 0;
940757e8328SLionel Sambuc p_hunk_beg = p_input_line + 1;
941757e8328SLionel Sambuc while (fillold <= p_ptrn_lines || fillnew <= p_end) {
942757e8328SLionel Sambuc line_beginning = ftell(pfp);
943757e8328SLionel Sambuc ret = pgets(buf, buf_len, pfp);
944757e8328SLionel Sambuc p_input_line++;
945757e8328SLionel Sambuc if (ret == NULL) {
946757e8328SLionel Sambuc if (p_max - fillnew < 3) {
947757e8328SLionel Sambuc /* assume blank lines got chopped */
948757e8328SLionel Sambuc strlcpy(buf, " \n", buf_len);
949757e8328SLionel Sambuc } else {
950757e8328SLionel Sambuc fatal("unexpected end of file in patch\n");
951757e8328SLionel Sambuc }
952757e8328SLionel Sambuc }
953757e8328SLionel Sambuc if (*buf == '\t' || *buf == '\n') {
954757e8328SLionel Sambuc ch = ' '; /* assume the space got eaten */
955757e8328SLionel Sambuc s = savestr(buf);
956757e8328SLionel Sambuc } else {
957757e8328SLionel Sambuc ch = *buf;
958757e8328SLionel Sambuc s = savestr(buf + 1);
959757e8328SLionel Sambuc }
960757e8328SLionel Sambuc if (out_of_mem) {
961757e8328SLionel Sambuc while (--fillnew > p_ptrn_lines)
962757e8328SLionel Sambuc free(p_line[fillnew]);
963757e8328SLionel Sambuc p_end = fillold - 1;
964757e8328SLionel Sambuc return false;
965757e8328SLionel Sambuc }
966757e8328SLionel Sambuc switch (ch) {
967757e8328SLionel Sambuc case '-':
968757e8328SLionel Sambuc if (fillold > p_ptrn_lines) {
969757e8328SLionel Sambuc free(s);
970757e8328SLionel Sambuc p_end = fillnew - 1;
971757e8328SLionel Sambuc malformed();
972757e8328SLionel Sambuc }
973757e8328SLionel Sambuc p_char[fillold] = ch;
974757e8328SLionel Sambuc p_line[fillold] = s;
975757e8328SLionel Sambuc p_len[fillold++] = strlen(s);
976757e8328SLionel Sambuc if (fillold > p_ptrn_lines) {
977757e8328SLionel Sambuc if (remove_special_line()) {
978757e8328SLionel Sambuc p_len[fillold - 1] -= 1;
979757e8328SLionel Sambuc s[p_len[fillold - 1]] = 0;
980757e8328SLionel Sambuc }
981757e8328SLionel Sambuc }
982757e8328SLionel Sambuc break;
983757e8328SLionel Sambuc case '=':
984757e8328SLionel Sambuc ch = ' ';
985757e8328SLionel Sambuc /* FALL THROUGH */
986757e8328SLionel Sambuc case ' ':
987757e8328SLionel Sambuc if (fillold > p_ptrn_lines) {
988757e8328SLionel Sambuc free(s);
989757e8328SLionel Sambuc while (--fillnew > p_ptrn_lines)
990757e8328SLionel Sambuc free(p_line[fillnew]);
991757e8328SLionel Sambuc p_end = fillold - 1;
992757e8328SLionel Sambuc malformed();
993757e8328SLionel Sambuc }
994757e8328SLionel Sambuc context++;
995757e8328SLionel Sambuc p_char[fillold] = ch;
996757e8328SLionel Sambuc p_line[fillold] = s;
997757e8328SLionel Sambuc p_len[fillold++] = strlen(s);
998757e8328SLionel Sambuc s = savestr(s);
999757e8328SLionel Sambuc if (out_of_mem) {
1000757e8328SLionel Sambuc while (--fillnew > p_ptrn_lines)
1001757e8328SLionel Sambuc free(p_line[fillnew]);
1002757e8328SLionel Sambuc p_end = fillold - 1;
1003757e8328SLionel Sambuc return false;
1004757e8328SLionel Sambuc }
1005757e8328SLionel Sambuc if (fillold > p_ptrn_lines) {
1006757e8328SLionel Sambuc if (remove_special_line()) {
1007757e8328SLionel Sambuc p_len[fillold - 1] -= 1;
1008757e8328SLionel Sambuc s[p_len[fillold - 1]] = 0;
1009757e8328SLionel Sambuc }
1010757e8328SLionel Sambuc }
1011757e8328SLionel Sambuc /* FALL THROUGH */
1012757e8328SLionel Sambuc case '+':
1013757e8328SLionel Sambuc if (fillnew > p_end) {
1014757e8328SLionel Sambuc free(s);
1015757e8328SLionel Sambuc while (--fillnew > p_ptrn_lines)
1016757e8328SLionel Sambuc free(p_line[fillnew]);
1017757e8328SLionel Sambuc p_end = fillold - 1;
1018757e8328SLionel Sambuc malformed();
1019757e8328SLionel Sambuc }
1020757e8328SLionel Sambuc p_char[fillnew] = ch;
1021757e8328SLionel Sambuc p_line[fillnew] = s;
1022757e8328SLionel Sambuc p_len[fillnew++] = strlen(s);
1023757e8328SLionel Sambuc if (fillold > p_ptrn_lines) {
1024757e8328SLionel Sambuc if (remove_special_line()) {
1025757e8328SLionel Sambuc p_len[fillnew - 1] -= 1;
1026757e8328SLionel Sambuc s[p_len[fillnew - 1]] = 0;
1027757e8328SLionel Sambuc }
1028757e8328SLionel Sambuc }
1029757e8328SLionel Sambuc break;
1030757e8328SLionel Sambuc default:
1031757e8328SLionel Sambuc p_end = fillnew;
1032757e8328SLionel Sambuc malformed();
1033757e8328SLionel Sambuc }
1034757e8328SLionel Sambuc if (ch != ' ' && context > 0) {
1035757e8328SLionel Sambuc if (context < p_context)
1036757e8328SLionel Sambuc p_context = context;
1037757e8328SLionel Sambuc context = -1000;
1038757e8328SLionel Sambuc }
1039757e8328SLionel Sambuc } /* while */
1040757e8328SLionel Sambuc } else { /* normal diff--fake it up */
1041757e8328SLionel Sambuc char hunk_type;
1042757e8328SLionel Sambuc int i;
1043757e8328SLionel Sambuc LINENUM min, max;
1044757e8328SLionel Sambuc
1045757e8328SLionel Sambuc line_beginning = ftell(pfp);
1046757e8328SLionel Sambuc p_context = 0;
1047757e8328SLionel Sambuc ret = pgets(buf, buf_len, pfp);
1048757e8328SLionel Sambuc p_input_line++;
1049757e8328SLionel Sambuc if (ret == NULL || !isdigit((unsigned char)*buf)) {
1050757e8328SLionel Sambuc next_intuit_at(line_beginning, p_input_line);
1051757e8328SLionel Sambuc return false;
1052757e8328SLionel Sambuc }
1053*0a6a1f1dSLionel Sambuc s = buf;
1054*0a6a1f1dSLionel Sambuc p_first = getskiplinenum(&s);
1055757e8328SLionel Sambuc if (*s == ',') {
1056757e8328SLionel Sambuc s++;
1057*0a6a1f1dSLionel Sambuc p_ptrn_lines = getskiplinenum(&s) - p_first + 1;
1058757e8328SLionel Sambuc } else
1059757e8328SLionel Sambuc p_ptrn_lines = (*s != 'a');
1060*0a6a1f1dSLionel Sambuc if (p_first >= LINENUM_MAX - p_ptrn_lines)
1061*0a6a1f1dSLionel Sambuc malformed();
1062*0a6a1f1dSLionel Sambuc hunk_type = *s++;
1063757e8328SLionel Sambuc if (hunk_type == 'a')
1064757e8328SLionel Sambuc p_first++; /* do append rather than insert */
1065*0a6a1f1dSLionel Sambuc min = getskiplinenum(&s);
1066757e8328SLionel Sambuc if (*s == ',')
1067*0a6a1f1dSLionel Sambuc max = getlinenum(++s);
1068757e8328SLionel Sambuc else
1069757e8328SLionel Sambuc max = min;
1070*0a6a1f1dSLionel Sambuc if (min < 0 || min > max || max - min == LINENUM_MAX)
1071*0a6a1f1dSLionel Sambuc malformed();
1072757e8328SLionel Sambuc if (hunk_type == 'd')
1073757e8328SLionel Sambuc min++;
1074757e8328SLionel Sambuc p_end = p_ptrn_lines + 1 + max - min + 1;
1075*0a6a1f1dSLionel Sambuc p_newfirst = min;
1076*0a6a1f1dSLionel Sambuc p_repl_lines = max - min + 1;
1077*0a6a1f1dSLionel Sambuc if (p_newfirst > LINENUM_MAX - p_repl_lines ||
1078*0a6a1f1dSLionel Sambuc p_ptrn_lines >= LINENUM_MAX - p_repl_lines - 1)
1079*0a6a1f1dSLionel Sambuc malformed();
1080*0a6a1f1dSLionel Sambuc p_end = p_ptrn_lines + p_repl_lines + 1;
1081757e8328SLionel Sambuc if (p_end > MAXHUNKSIZE)
1082757e8328SLionel Sambuc fatal("hunk too large (%ld lines) at line %ld: %s",
1083757e8328SLionel Sambuc p_end, p_input_line, buf);
1084757e8328SLionel Sambuc while (p_end >= hunkmax)
1085757e8328SLionel Sambuc grow_hunkmax();
1086757e8328SLionel Sambuc snprintf(buf, buf_len, "*** %ld,%ld\n", p_first,
1087757e8328SLionel Sambuc p_first + p_ptrn_lines - 1);
1088757e8328SLionel Sambuc p_line[0] = savestr(buf);
1089757e8328SLionel Sambuc if (out_of_mem) {
1090757e8328SLionel Sambuc p_end = -1;
1091757e8328SLionel Sambuc return false;
1092757e8328SLionel Sambuc }
1093757e8328SLionel Sambuc p_char[0] = '*';
1094757e8328SLionel Sambuc for (i = 1; i <= p_ptrn_lines; i++) {
1095757e8328SLionel Sambuc ret = pgets(buf, buf_len, pfp);
1096757e8328SLionel Sambuc p_input_line++;
1097757e8328SLionel Sambuc if (ret == NULL)
1098757e8328SLionel Sambuc fatal("unexpected end of file in patch at line %ld\n",
1099757e8328SLionel Sambuc p_input_line);
1100757e8328SLionel Sambuc if (*buf != '<')
1101757e8328SLionel Sambuc fatal("< expected at line %ld of patch\n",
1102757e8328SLionel Sambuc p_input_line);
1103757e8328SLionel Sambuc p_line[i] = savestr(buf + 2);
1104757e8328SLionel Sambuc if (out_of_mem) {
1105757e8328SLionel Sambuc p_end = i - 1;
1106757e8328SLionel Sambuc return false;
1107757e8328SLionel Sambuc }
1108757e8328SLionel Sambuc p_len[i] = strlen(p_line[i]);
1109757e8328SLionel Sambuc p_char[i] = '-';
1110757e8328SLionel Sambuc }
1111757e8328SLionel Sambuc
1112757e8328SLionel Sambuc if (remove_special_line()) {
1113757e8328SLionel Sambuc p_len[i - 1] -= 1;
1114757e8328SLionel Sambuc (p_line[i - 1])[p_len[i - 1]] = 0;
1115757e8328SLionel Sambuc }
1116757e8328SLionel Sambuc if (hunk_type == 'c') {
1117757e8328SLionel Sambuc ret = pgets(buf, buf_len, pfp);
1118757e8328SLionel Sambuc p_input_line++;
1119757e8328SLionel Sambuc if (ret == NULL)
1120757e8328SLionel Sambuc fatal("unexpected end of file in patch at line %ld\n",
1121757e8328SLionel Sambuc p_input_line);
1122757e8328SLionel Sambuc if (*buf != '-')
1123757e8328SLionel Sambuc fatal("--- expected at line %ld of patch\n",
1124757e8328SLionel Sambuc p_input_line);
1125757e8328SLionel Sambuc }
1126757e8328SLionel Sambuc snprintf(buf, buf_len, "--- %ld,%ld\n", min, max);
1127757e8328SLionel Sambuc p_line[i] = savestr(buf);
1128757e8328SLionel Sambuc if (out_of_mem) {
1129757e8328SLionel Sambuc p_end = i - 1;
1130757e8328SLionel Sambuc return false;
1131757e8328SLionel Sambuc }
1132757e8328SLionel Sambuc p_char[i] = '=';
1133757e8328SLionel Sambuc for (i++; i <= p_end; i++) {
1134757e8328SLionel Sambuc ret = pgets(buf, buf_len, pfp);
1135757e8328SLionel Sambuc p_input_line++;
1136757e8328SLionel Sambuc if (ret == NULL)
1137757e8328SLionel Sambuc fatal("unexpected end of file in patch at line %ld\n",
1138757e8328SLionel Sambuc p_input_line);
1139757e8328SLionel Sambuc if (*buf != '>')
1140757e8328SLionel Sambuc fatal("> expected at line %ld of patch\n",
1141757e8328SLionel Sambuc p_input_line);
1142757e8328SLionel Sambuc p_line[i] = savestr(buf + 2);
1143757e8328SLionel Sambuc if (out_of_mem) {
1144757e8328SLionel Sambuc p_end = i - 1;
1145757e8328SLionel Sambuc return false;
1146757e8328SLionel Sambuc }
1147757e8328SLionel Sambuc p_len[i] = strlen(p_line[i]);
1148757e8328SLionel Sambuc p_char[i] = '+';
1149757e8328SLionel Sambuc }
1150757e8328SLionel Sambuc
1151757e8328SLionel Sambuc if (remove_special_line()) {
1152757e8328SLionel Sambuc p_len[i - 1] -= 1;
1153757e8328SLionel Sambuc (p_line[i - 1])[p_len[i - 1]] = 0;
1154757e8328SLionel Sambuc }
1155757e8328SLionel Sambuc }
1156757e8328SLionel Sambuc if (reverse) /* backwards patch? */
1157757e8328SLionel Sambuc if (!pch_swap())
1158757e8328SLionel Sambuc say("Not enough memory to swap next hunk!\n");
1159757e8328SLionel Sambuc #ifdef DEBUGGING
1160757e8328SLionel Sambuc if (debug & 2) {
1161757e8328SLionel Sambuc int i;
1162757e8328SLionel Sambuc char special;
1163757e8328SLionel Sambuc
1164757e8328SLionel Sambuc for (i = 0; i <= p_end; i++) {
1165757e8328SLionel Sambuc if (i == p_ptrn_lines)
1166757e8328SLionel Sambuc special = '^';
1167757e8328SLionel Sambuc else
1168757e8328SLionel Sambuc special = ' ';
1169757e8328SLionel Sambuc fprintf(stderr, "%3d %c %c %s", i, p_char[i],
1170757e8328SLionel Sambuc special, p_line[i]);
1171757e8328SLionel Sambuc fflush(stderr);
1172757e8328SLionel Sambuc }
1173757e8328SLionel Sambuc }
1174757e8328SLionel Sambuc #endif
1175757e8328SLionel Sambuc if (p_end + 1 < hunkmax)/* paranoia reigns supreme... */
1176757e8328SLionel Sambuc p_char[p_end + 1] = '^'; /* add a stopper for apply_hunk */
1177757e8328SLionel Sambuc return true;
1178757e8328SLionel Sambuc }
1179757e8328SLionel Sambuc
1180757e8328SLionel Sambuc /*
1181757e8328SLionel Sambuc * Input a line from the patch file, worrying about indentation.
1182757e8328SLionel Sambuc */
1183757e8328SLionel Sambuc static char *
pgets(char * bf,int sz,FILE * fp)1184757e8328SLionel Sambuc pgets(char *bf, int sz, FILE *fp)
1185757e8328SLionel Sambuc {
1186757e8328SLionel Sambuc char *s, *ret = fgets(bf, sz, fp);
1187757e8328SLionel Sambuc int indent = 0;
1188757e8328SLionel Sambuc
1189757e8328SLionel Sambuc if (p_indent && ret != NULL) {
1190757e8328SLionel Sambuc for (s = buf;
1191757e8328SLionel Sambuc indent < p_indent && (*s == ' ' || *s == '\t' || *s == 'X');
1192757e8328SLionel Sambuc s++) {
1193757e8328SLionel Sambuc if (*s == '\t')
1194757e8328SLionel Sambuc indent += 8 - (indent % 7);
1195757e8328SLionel Sambuc else
1196757e8328SLionel Sambuc indent++;
1197757e8328SLionel Sambuc }
1198757e8328SLionel Sambuc if (buf != s && strlcpy(buf, s, buf_len) >= buf_len)
1199757e8328SLionel Sambuc fatal("buffer too small in pgets()\n");
1200757e8328SLionel Sambuc }
1201757e8328SLionel Sambuc return ret;
1202757e8328SLionel Sambuc }
1203757e8328SLionel Sambuc
1204757e8328SLionel Sambuc /*
1205757e8328SLionel Sambuc * Reverse the old and new portions of the current hunk.
1206757e8328SLionel Sambuc */
1207757e8328SLionel Sambuc bool
pch_swap(void)1208757e8328SLionel Sambuc pch_swap(void)
1209757e8328SLionel Sambuc {
1210757e8328SLionel Sambuc char **tp_line; /* the text of the hunk */
1211757e8328SLionel Sambuc short *tp_len; /* length of each line */
1212757e8328SLionel Sambuc char *tp_char; /* +, -, and ! */
1213757e8328SLionel Sambuc LINENUM i;
1214757e8328SLionel Sambuc LINENUM n;
1215757e8328SLionel Sambuc bool blankline = false;
1216757e8328SLionel Sambuc char *s;
1217757e8328SLionel Sambuc
1218757e8328SLionel Sambuc i = p_first;
1219757e8328SLionel Sambuc p_first = p_newfirst;
1220757e8328SLionel Sambuc p_newfirst = i;
1221757e8328SLionel Sambuc
1222757e8328SLionel Sambuc /* make a scratch copy */
1223757e8328SLionel Sambuc
1224757e8328SLionel Sambuc tp_line = p_line;
1225757e8328SLionel Sambuc tp_len = p_len;
1226757e8328SLionel Sambuc tp_char = p_char;
1227757e8328SLionel Sambuc p_line = NULL; /* force set_hunkmax to allocate again */
1228757e8328SLionel Sambuc p_len = NULL;
1229757e8328SLionel Sambuc p_char = NULL;
1230757e8328SLionel Sambuc set_hunkmax();
1231757e8328SLionel Sambuc if (p_line == NULL || p_len == NULL || p_char == NULL) {
1232757e8328SLionel Sambuc
1233757e8328SLionel Sambuc free(p_line);
1234757e8328SLionel Sambuc p_line = tp_line;
1235757e8328SLionel Sambuc free(p_len);
1236757e8328SLionel Sambuc p_len = tp_len;
1237757e8328SLionel Sambuc free(p_char);
1238757e8328SLionel Sambuc p_char = tp_char;
1239757e8328SLionel Sambuc return false; /* not enough memory to swap hunk! */
1240757e8328SLionel Sambuc }
1241757e8328SLionel Sambuc /* now turn the new into the old */
1242757e8328SLionel Sambuc
1243757e8328SLionel Sambuc i = p_ptrn_lines + 1;
1244757e8328SLionel Sambuc if (tp_char[i] == '\n') { /* account for possible blank line */
1245757e8328SLionel Sambuc blankline = true;
1246757e8328SLionel Sambuc i++;
1247757e8328SLionel Sambuc }
1248757e8328SLionel Sambuc if (p_efake >= 0) { /* fix non-freeable ptr range */
1249757e8328SLionel Sambuc if (p_efake <= i)
1250757e8328SLionel Sambuc n = p_end - i + 1;
1251757e8328SLionel Sambuc else
1252757e8328SLionel Sambuc n = -i;
1253757e8328SLionel Sambuc p_efake += n;
1254757e8328SLionel Sambuc p_bfake += n;
1255757e8328SLionel Sambuc }
1256757e8328SLionel Sambuc for (n = 0; i <= p_end; i++, n++) {
1257757e8328SLionel Sambuc p_line[n] = tp_line[i];
1258757e8328SLionel Sambuc p_char[n] = tp_char[i];
1259757e8328SLionel Sambuc if (p_char[n] == '+')
1260757e8328SLionel Sambuc p_char[n] = '-';
1261757e8328SLionel Sambuc p_len[n] = tp_len[i];
1262757e8328SLionel Sambuc }
1263757e8328SLionel Sambuc if (blankline) {
1264757e8328SLionel Sambuc i = p_ptrn_lines + 1;
1265757e8328SLionel Sambuc p_line[n] = tp_line[i];
1266757e8328SLionel Sambuc p_char[n] = tp_char[i];
1267757e8328SLionel Sambuc p_len[n] = tp_len[i];
1268757e8328SLionel Sambuc n++;
1269757e8328SLionel Sambuc }
1270757e8328SLionel Sambuc if (p_char[0] != '=')
1271757e8328SLionel Sambuc fatal("Malformed patch at line %ld: expected '=' found '%c'\n",
1272757e8328SLionel Sambuc p_input_line, p_char[0]);
1273757e8328SLionel Sambuc p_char[0] = '*';
1274757e8328SLionel Sambuc for (s = p_line[0]; *s; s++)
1275757e8328SLionel Sambuc if (*s == '-')
1276757e8328SLionel Sambuc *s = '*';
1277757e8328SLionel Sambuc
1278757e8328SLionel Sambuc /* now turn the old into the new */
1279757e8328SLionel Sambuc
1280757e8328SLionel Sambuc if (p_char[0] != '*')
1281757e8328SLionel Sambuc fatal("Malformed patch at line %ld: expected '*' found '%c'\n",
1282757e8328SLionel Sambuc p_input_line, p_char[0]);
1283757e8328SLionel Sambuc tp_char[0] = '=';
1284757e8328SLionel Sambuc for (s = tp_line[0]; *s; s++)
1285757e8328SLionel Sambuc if (*s == '*')
1286757e8328SLionel Sambuc *s = '-';
1287757e8328SLionel Sambuc for (i = 0; n <= p_end; i++, n++) {
1288757e8328SLionel Sambuc p_line[n] = tp_line[i];
1289757e8328SLionel Sambuc p_char[n] = tp_char[i];
1290757e8328SLionel Sambuc if (p_char[n] == '-')
1291757e8328SLionel Sambuc p_char[n] = '+';
1292757e8328SLionel Sambuc p_len[n] = tp_len[i];
1293757e8328SLionel Sambuc }
1294757e8328SLionel Sambuc
1295757e8328SLionel Sambuc if (i != p_ptrn_lines + 1)
1296757e8328SLionel Sambuc fatal("Malformed patch at line %ld: expected %ld lines, "
1297757e8328SLionel Sambuc "got %ld\n",
1298757e8328SLionel Sambuc p_input_line, p_ptrn_lines + 1, i);
1299757e8328SLionel Sambuc
1300757e8328SLionel Sambuc i = p_ptrn_lines;
1301757e8328SLionel Sambuc p_ptrn_lines = p_repl_lines;
1302757e8328SLionel Sambuc p_repl_lines = i;
1303757e8328SLionel Sambuc
1304757e8328SLionel Sambuc free(tp_line);
1305757e8328SLionel Sambuc free(tp_len);
1306757e8328SLionel Sambuc free(tp_char);
1307757e8328SLionel Sambuc
1308757e8328SLionel Sambuc return true;
1309757e8328SLionel Sambuc }
1310757e8328SLionel Sambuc
1311757e8328SLionel Sambuc /*
1312757e8328SLionel Sambuc * Return the specified line position in the old file of the old context.
1313757e8328SLionel Sambuc */
1314757e8328SLionel Sambuc LINENUM
pch_first(void)1315757e8328SLionel Sambuc pch_first(void)
1316757e8328SLionel Sambuc {
1317757e8328SLionel Sambuc return p_first;
1318757e8328SLionel Sambuc }
1319757e8328SLionel Sambuc
1320757e8328SLionel Sambuc /*
1321757e8328SLionel Sambuc * Return the number of lines of old context.
1322757e8328SLionel Sambuc */
1323757e8328SLionel Sambuc LINENUM
pch_ptrn_lines(void)1324757e8328SLionel Sambuc pch_ptrn_lines(void)
1325757e8328SLionel Sambuc {
1326757e8328SLionel Sambuc return p_ptrn_lines;
1327757e8328SLionel Sambuc }
1328757e8328SLionel Sambuc
1329757e8328SLionel Sambuc /*
1330757e8328SLionel Sambuc * Return the probable line position in the new file of the first line.
1331757e8328SLionel Sambuc */
1332757e8328SLionel Sambuc LINENUM
pch_newfirst(void)1333757e8328SLionel Sambuc pch_newfirst(void)
1334757e8328SLionel Sambuc {
1335757e8328SLionel Sambuc return p_newfirst;
1336757e8328SLionel Sambuc }
1337757e8328SLionel Sambuc
1338757e8328SLionel Sambuc /*
1339757e8328SLionel Sambuc * Return the number of lines in the replacement text including context.
1340757e8328SLionel Sambuc */
1341757e8328SLionel Sambuc LINENUM
pch_repl_lines(void)1342757e8328SLionel Sambuc pch_repl_lines(void)
1343757e8328SLionel Sambuc {
1344757e8328SLionel Sambuc return p_repl_lines;
1345757e8328SLionel Sambuc }
1346757e8328SLionel Sambuc
1347757e8328SLionel Sambuc /*
1348757e8328SLionel Sambuc * Return the number of lines in the whole hunk.
1349757e8328SLionel Sambuc */
1350757e8328SLionel Sambuc LINENUM
pch_end(void)1351757e8328SLionel Sambuc pch_end(void)
1352757e8328SLionel Sambuc {
1353757e8328SLionel Sambuc return p_end;
1354757e8328SLionel Sambuc }
1355757e8328SLionel Sambuc
1356757e8328SLionel Sambuc /*
1357757e8328SLionel Sambuc * Return the number of context lines before the first changed line.
1358757e8328SLionel Sambuc */
1359757e8328SLionel Sambuc LINENUM
pch_context(void)1360757e8328SLionel Sambuc pch_context(void)
1361757e8328SLionel Sambuc {
1362757e8328SLionel Sambuc return p_context;
1363757e8328SLionel Sambuc }
1364757e8328SLionel Sambuc
1365757e8328SLionel Sambuc /*
1366757e8328SLionel Sambuc * Return the length of a particular patch line.
1367757e8328SLionel Sambuc */
1368757e8328SLionel Sambuc short
pch_line_len(LINENUM line)1369757e8328SLionel Sambuc pch_line_len(LINENUM line)
1370757e8328SLionel Sambuc {
1371757e8328SLionel Sambuc return p_len[line];
1372757e8328SLionel Sambuc }
1373757e8328SLionel Sambuc
1374757e8328SLionel Sambuc /*
1375757e8328SLionel Sambuc * Return the control character (+, -, *, !, etc) for a patch line.
1376757e8328SLionel Sambuc */
1377757e8328SLionel Sambuc char
pch_char(LINENUM line)1378757e8328SLionel Sambuc pch_char(LINENUM line)
1379757e8328SLionel Sambuc {
1380757e8328SLionel Sambuc return p_char[line];
1381757e8328SLionel Sambuc }
1382757e8328SLionel Sambuc
1383757e8328SLionel Sambuc /*
1384757e8328SLionel Sambuc * Return a pointer to a particular patch line.
1385757e8328SLionel Sambuc */
1386757e8328SLionel Sambuc char *
pfetch(LINENUM line)1387757e8328SLionel Sambuc pfetch(LINENUM line)
1388757e8328SLionel Sambuc {
1389757e8328SLionel Sambuc return p_line[line];
1390757e8328SLionel Sambuc }
1391757e8328SLionel Sambuc
1392757e8328SLionel Sambuc /*
1393757e8328SLionel Sambuc * Return where in the patch file this hunk began, for error messages.
1394757e8328SLionel Sambuc */
1395757e8328SLionel Sambuc LINENUM
pch_hunk_beg(void)1396757e8328SLionel Sambuc pch_hunk_beg(void)
1397757e8328SLionel Sambuc {
1398757e8328SLionel Sambuc return p_hunk_beg;
1399757e8328SLionel Sambuc }
1400757e8328SLionel Sambuc
1401757e8328SLionel Sambuc /*
1402757e8328SLionel Sambuc * Apply an ed script by feeding ed itself.
1403757e8328SLionel Sambuc */
1404757e8328SLionel Sambuc void
do_ed_script(void)1405757e8328SLionel Sambuc do_ed_script(void)
1406757e8328SLionel Sambuc {
1407757e8328SLionel Sambuc char *t;
1408757e8328SLionel Sambuc long beginning_of_this_line;
1409757e8328SLionel Sambuc FILE *pipefp = NULL;
1410*0a6a1f1dSLionel Sambuc int continuation;
1411757e8328SLionel Sambuc
1412757e8328SLionel Sambuc if (!skip_rest_of_patch) {
1413757e8328SLionel Sambuc if (copy_file(filearg[0], TMPOUTNAME) < 0) {
1414757e8328SLionel Sambuc unlink(TMPOUTNAME);
1415757e8328SLionel Sambuc fatal("can't create temp file %s", TMPOUTNAME);
1416757e8328SLionel Sambuc }
1417757e8328SLionel Sambuc snprintf(buf, buf_len, "%s%s%s", _PATH_ED,
1418757e8328SLionel Sambuc verbose ? " " : " -s ", TMPOUTNAME);
1419757e8328SLionel Sambuc pipefp = popen(buf, "w");
1420757e8328SLionel Sambuc }
1421757e8328SLionel Sambuc for (;;) {
1422757e8328SLionel Sambuc beginning_of_this_line = ftell(pfp);
1423757e8328SLionel Sambuc if (pgets(buf, buf_len, pfp) == NULL) {
1424757e8328SLionel Sambuc next_intuit_at(beginning_of_this_line, p_input_line);
1425757e8328SLionel Sambuc break;
1426757e8328SLionel Sambuc }
1427757e8328SLionel Sambuc p_input_line++;
1428757e8328SLionel Sambuc for (t = buf; isdigit((unsigned char)*t) || *t == ','; t++)
1429757e8328SLionel Sambuc ;
1430757e8328SLionel Sambuc /* POSIX defines allowed commands as {a,c,d,i,s} */
1431757e8328SLionel Sambuc if (isdigit((unsigned char)*buf) && (*t == 'a' || *t == 'c' ||
1432757e8328SLionel Sambuc *t == 'd' || *t == 'i' || *t == 's')) {
1433757e8328SLionel Sambuc if (pipefp != NULL)
1434757e8328SLionel Sambuc fputs(buf, pipefp);
1435*0a6a1f1dSLionel Sambuc if (*t == 's') {
1436*0a6a1f1dSLionel Sambuc for (;;) {
1437*0a6a1f1dSLionel Sambuc continuation = 0;
1438*0a6a1f1dSLionel Sambuc t = strchr(buf, '\0') - 1;
1439*0a6a1f1dSLionel Sambuc while (--t >= buf && *t == '\\')
1440*0a6a1f1dSLionel Sambuc continuation = !continuation;
1441*0a6a1f1dSLionel Sambuc if (!continuation ||
1442*0a6a1f1dSLionel Sambuc pgets(buf, sizeof buf, pfp) == NULL)
1443*0a6a1f1dSLionel Sambuc break;
1444*0a6a1f1dSLionel Sambuc if (pipefp != NULL)
1445*0a6a1f1dSLionel Sambuc fputs(buf, pipefp);
1446*0a6a1f1dSLionel Sambuc }
1447*0a6a1f1dSLionel Sambuc } else if (*t != 'd') {
1448757e8328SLionel Sambuc while (pgets(buf, buf_len, pfp) != NULL) {
1449757e8328SLionel Sambuc p_input_line++;
1450757e8328SLionel Sambuc if (pipefp != NULL)
1451757e8328SLionel Sambuc fputs(buf, pipefp);
1452757e8328SLionel Sambuc if (strEQ(buf, ".\n"))
1453757e8328SLionel Sambuc break;
1454757e8328SLionel Sambuc }
1455757e8328SLionel Sambuc }
1456757e8328SLionel Sambuc } else {
1457757e8328SLionel Sambuc next_intuit_at(beginning_of_this_line, p_input_line);
1458757e8328SLionel Sambuc break;
1459757e8328SLionel Sambuc }
1460757e8328SLionel Sambuc }
1461757e8328SLionel Sambuc if (pipefp == NULL)
1462757e8328SLionel Sambuc return;
1463757e8328SLionel Sambuc fprintf(pipefp, "w\n");
1464757e8328SLionel Sambuc fprintf(pipefp, "q\n");
1465757e8328SLionel Sambuc fflush(pipefp);
1466757e8328SLionel Sambuc pclose(pipefp);
1467757e8328SLionel Sambuc ignore_signals();
1468757e8328SLionel Sambuc if (!check_only) {
1469757e8328SLionel Sambuc if (move_file(TMPOUTNAME, outname) < 0) {
1470757e8328SLionel Sambuc toutkeep = true;
1471757e8328SLionel Sambuc chmod(TMPOUTNAME, filemode);
1472757e8328SLionel Sambuc } else
1473757e8328SLionel Sambuc chmod(outname, filemode);
1474757e8328SLionel Sambuc }
1475757e8328SLionel Sambuc set_signals(1);
1476757e8328SLionel Sambuc }
1477757e8328SLionel Sambuc
1478757e8328SLionel Sambuc /*
1479757e8328SLionel Sambuc * Choose the name of the file to be patched based on POSIX rules.
1480757e8328SLionel Sambuc * NOTE: the POSIX rules are amazingly stupid and we only follow them
1481757e8328SLionel Sambuc * if the user specified --posix or set POSIXLY_CORRECT.
1482757e8328SLionel Sambuc */
1483757e8328SLionel Sambuc static char *
posix_name(const struct file_name * names,bool assume_exists)1484757e8328SLionel Sambuc posix_name(const struct file_name *names, bool assume_exists)
1485757e8328SLionel Sambuc {
1486757e8328SLionel Sambuc char *path = NULL;
1487757e8328SLionel Sambuc int i;
1488757e8328SLionel Sambuc
1489757e8328SLionel Sambuc /*
1490757e8328SLionel Sambuc * POSIX states that the filename will be chosen from one
1491757e8328SLionel Sambuc * of the old, new and index names (in that order) if
1492757e8328SLionel Sambuc * the file exists relative to CWD after -p stripping.
1493757e8328SLionel Sambuc */
1494757e8328SLionel Sambuc for (i = 0; i < MAX_FILE; i++) {
1495757e8328SLionel Sambuc if (names[i].path != NULL && names[i].exists) {
1496757e8328SLionel Sambuc path = names[i].path;
1497757e8328SLionel Sambuc break;
1498757e8328SLionel Sambuc }
1499757e8328SLionel Sambuc }
1500757e8328SLionel Sambuc if (path == NULL && !assume_exists) {
1501757e8328SLionel Sambuc /*
1502757e8328SLionel Sambuc * No files found, look for something we can checkout from
1503757e8328SLionel Sambuc * RCS/SCCS dirs. Same order as above.
1504757e8328SLionel Sambuc */
1505757e8328SLionel Sambuc for (i = 0; i < MAX_FILE; i++) {
1506757e8328SLionel Sambuc if (names[i].path != NULL &&
1507757e8328SLionel Sambuc (path = checked_in(names[i].path)) != NULL)
1508757e8328SLionel Sambuc break;
1509757e8328SLionel Sambuc }
1510757e8328SLionel Sambuc /*
1511757e8328SLionel Sambuc * Still no match? Check to see if the diff could be creating
1512757e8328SLionel Sambuc * a new file.
1513757e8328SLionel Sambuc */
1514757e8328SLionel Sambuc if (path == NULL && ok_to_create_file &&
1515757e8328SLionel Sambuc names[NEW_FILE].path != NULL)
1516757e8328SLionel Sambuc path = names[NEW_FILE].path;
1517757e8328SLionel Sambuc }
1518757e8328SLionel Sambuc
1519757e8328SLionel Sambuc return path ? savestr(path) : NULL;
1520757e8328SLionel Sambuc }
1521757e8328SLionel Sambuc
1522757e8328SLionel Sambuc /*
1523757e8328SLionel Sambuc * Choose the name of the file to be patched based the "best" one
1524757e8328SLionel Sambuc * available.
1525757e8328SLionel Sambuc */
1526757e8328SLionel Sambuc static char *
best_name(const struct file_name * names,bool assume_exists)1527757e8328SLionel Sambuc best_name(const struct file_name *names, bool assume_exists)
1528757e8328SLionel Sambuc {
1529757e8328SLionel Sambuc size_t min_components, min_baselen, min_len, tmp;
1530757e8328SLionel Sambuc char *best = NULL;
1531757e8328SLionel Sambuc int i;
1532757e8328SLionel Sambuc
1533757e8328SLionel Sambuc /*
1534757e8328SLionel Sambuc * The "best" name is the one with the fewest number of path
1535757e8328SLionel Sambuc * components, the shortest basename length, and the shortest
1536757e8328SLionel Sambuc * overall length (in that order). We only use the Index: file
1537757e8328SLionel Sambuc * if neither of the old or new files could be intuited from
1538757e8328SLionel Sambuc * the diff header.
1539757e8328SLionel Sambuc */
1540757e8328SLionel Sambuc min_components = min_baselen = min_len = SIZE_MAX;
1541757e8328SLionel Sambuc for (i = INDEX_FILE; i >= OLD_FILE; i--) {
1542757e8328SLionel Sambuc if (names[i].path == NULL ||
1543757e8328SLionel Sambuc (!names[i].exists && !assume_exists))
1544757e8328SLionel Sambuc continue;
1545757e8328SLionel Sambuc if ((tmp = num_components(names[i].path)) > min_components)
1546757e8328SLionel Sambuc continue;
1547757e8328SLionel Sambuc min_components = tmp;
1548757e8328SLionel Sambuc if ((tmp = strlen(basename(names[i].path))) > min_baselen)
1549757e8328SLionel Sambuc continue;
1550757e8328SLionel Sambuc min_baselen = tmp;
1551757e8328SLionel Sambuc if ((tmp = strlen(names[i].path)) > min_len)
1552757e8328SLionel Sambuc continue;
1553757e8328SLionel Sambuc min_len = tmp;
1554757e8328SLionel Sambuc best = names[i].path;
1555757e8328SLionel Sambuc }
1556757e8328SLionel Sambuc if (best == NULL) {
1557757e8328SLionel Sambuc /*
1558757e8328SLionel Sambuc * No files found, look for something we can checkout from
1559757e8328SLionel Sambuc * RCS/SCCS dirs. Logic is identical to that above...
1560757e8328SLionel Sambuc */
1561757e8328SLionel Sambuc min_components = min_baselen = min_len = SIZE_MAX;
1562757e8328SLionel Sambuc for (i = INDEX_FILE; i >= OLD_FILE; i--) {
1563757e8328SLionel Sambuc if (names[i].path == NULL ||
1564757e8328SLionel Sambuc checked_in(names[i].path) == NULL)
1565757e8328SLionel Sambuc continue;
1566757e8328SLionel Sambuc if ((tmp = num_components(names[i].path)) > min_components)
1567757e8328SLionel Sambuc continue;
1568757e8328SLionel Sambuc min_components = tmp;
1569757e8328SLionel Sambuc if ((tmp = strlen(basename(names[i].path))) > min_baselen)
1570757e8328SLionel Sambuc continue;
1571757e8328SLionel Sambuc min_baselen = tmp;
1572757e8328SLionel Sambuc if ((tmp = strlen(names[i].path)) > min_len)
1573757e8328SLionel Sambuc continue;
1574757e8328SLionel Sambuc min_len = tmp;
1575757e8328SLionel Sambuc best = names[i].path;
1576757e8328SLionel Sambuc }
1577757e8328SLionel Sambuc /*
1578757e8328SLionel Sambuc * Still no match? Check to see if the diff could be creating
1579757e8328SLionel Sambuc * a new file.
1580757e8328SLionel Sambuc */
1581757e8328SLionel Sambuc if (best == NULL && ok_to_create_file &&
1582757e8328SLionel Sambuc names[NEW_FILE].path != NULL)
1583757e8328SLionel Sambuc best = names[NEW_FILE].path;
1584757e8328SLionel Sambuc }
1585757e8328SLionel Sambuc
1586757e8328SLionel Sambuc return best ? savestr(best) : NULL;
1587757e8328SLionel Sambuc }
1588757e8328SLionel Sambuc
1589757e8328SLionel Sambuc static size_t
num_components(const char * path)1590757e8328SLionel Sambuc num_components(const char *path)
1591757e8328SLionel Sambuc {
1592757e8328SLionel Sambuc size_t n;
1593757e8328SLionel Sambuc const char *cp;
1594757e8328SLionel Sambuc
1595757e8328SLionel Sambuc for (n = 0, cp = path; (cp = strchr(cp, '/')) != NULL; n++, cp++) {
1596757e8328SLionel Sambuc while (*cp == '/')
1597757e8328SLionel Sambuc cp++; /* skip consecutive slashes */
1598757e8328SLionel Sambuc }
1599757e8328SLionel Sambuc return n;
1600757e8328SLionel Sambuc }
1601